c++wxwidgetsscintillawxstyledtextctrl

wxStyledTextCtrl - Size of AutoComp


I was just wondering if it is possible to find the size (in pixels) of the autocompletion control shown by the wxStyledTextCtrl.

My goal is to show a help window associated with the entry when a selection happens. Therefore, I need the location and also the width of the autocompletion control. It seems location can be found from m_STC->AutoCompPosStart() but there seems to be no way of finding the width. I am using the following code:

auto StartPos = m_STC->ToPhys(m_STC->PointFromPosition(m_STC->AutoCompPosStart()));
    
int MaxChars = m_STC->AutoCompGetMaxWidth(); //returns 0 unless set to a fixed value
int w, h;
m_STC->GetTextExtent(wxString("A", MaxChars), &w, &h);

return wxPoint(StartPos.x + w, StartPos.y);

I am using Windows and wxWidgets 3.2.


Solution

  • There is no way to get this information from the styled text control because the autocomp window is completely managed by Scintilla. And unfortunately, Scintilla doesn't make any methods available for getting this info.

    As a hack-around, the popup is currently implemented as a child window of the styled text control. So you could do something like this:

    const wxWindowList& childred = m_stc->GetChildren();
    
    for ( auto it = childred.begin() ; it != childred.end() ; ++it )
    {
        // We're assuming the styled text control has at most 1 child - 
        // namely the autocomp popup. It might be better to check that
        // the window found is in fact the auto comp popup somehow.
    
        // win->GetPosition() will return screen coordinates, so to get client
        // coordinates, ScreenToClient must be called.
        wxPoint psn = m_stc->ScreenToClient(win->GetPosition());
        wxSize sz = win->GetSize();
    
        // Do something with size and position here.
    }
    

    However, this isn't guaranteed to always work. If in the future, the auto comp popup implementation is changed to use a top level window instead of a child of the control, this method will fail.