Search code examples
c++wxwidgets

Notifying wxSizer of dynamic layout changes


I have the following hierarchy for layout:

wxMDIChildFrame -> wxNotebook -> wxScrolledWindow -> wxBoxSizer -> wxStyledTextCtrl

GOAL: The wxStyledTextCtrl (CScriptWnd) resizes itself when user adds or deletes a line or presses Shift+Enter to add another CScriptWnd to the wxScrolledWindow.

The following code is when a line is added:

void CScriptWnd::OnNewLineAdded(wxStyledTextEvent& event)
{
    long col, line;
    PositionToXY(GetInsertionPoint(), &col, &line);

    auto ClientSize = GetClientSize();
    int Height = ClientSize.GetHeight();

    Height += TextHeight(line);

    SetClientSize(ClientSize.GetWidth(), Height);
    SetMinSize(wxSize(ClientSize.GetWidth(), Height));


    Refresh();
    
    //m_ParentWindow->FitInside(); //CScriptWnd gets smaller rather than larger
    //m_ParentWindow->Layout(); //CScriptWnd gets smaller rather than larger
    //m_ParentWindow->GetGrandParent()->Layout(); //No effect


    event.Skip();
}

Most work well such as the CScriptWnd resizes itself, the new script window added to wxScrolledWindow etc...

The problem is that the update to user interface only properly happens when the wxMDIChildFrame is resized using the mouse. For example, if there are two CScriptWnd and the top one resizes itself, it overlaps with the bottom one until the wxMDIChildFrame is resized using the mouse. Similar happens for the visibility of scrollbars such that when CScriptWnd client size gets larger the scrollbars only become visible when top-level window resized using the mouse.

Not sure what I am missing.


Solution

  • You probably need

    SetMinSize(GetSize());
    GetParent()->Layout();