Resize window in runtime

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
rudolfninja
Earned some good credits
Earned some good credits
Posts: 107
Joined: Tue Aug 28, 2018 1:02 pm
Location: Belarus

Resize window in runtime

Post by rudolfninja »

I need to set window width depending on text's width.
I tried SetClientSize and SetSize methods, but window still has the same size:

Code: Select all

MainWindow::MainWindow(wxWindow* parent, const std::shared_ptr<SharingManager>& sharingManager, const std::shared_ptr<UiAgentMasterBridge>& agentBridge) :
    wxCustomFrame(parent, wxT(""), wxDEFAULT_FRAME_STYLE & ~wxMAXIMIZE_BOX & ~wxMINIMIZE_BOX & ~wxRESIZE_BORDER),
    m_sharingManager(sharingManager),
    m_allowSharing(false),
    m_hasDefaultShares(false),
    m_uiAgentBridge(agentBridge)
{
    Init();
    SetTitle("Title");

    SetMinClientSize(FromDIP(wxSize(500, 300)));
    SetMaxClientSize(FromDIP(wxSize(700, 700)));
    //SetClientSize(FromDIP(wxSize(700, 700)));
    SetBackgroundColour(StylesFactory::BackgroundColor);
    SetForegroundColour(StylesFactory::ForegroundColor);

    CenterOnScreen();
    UpdateLayout();

    Bind(wxEVT_CLOSE_WINDOW, &MainWindow::OnExit, this);
    Bind(wxEVT_SIZE, &MainWindow::OnSize, this);
    Bind(wxEVT_BUTTON, &MainWindow::OnCancel, this, wxID_CANCEL);
}
wxCustomPanel* MainWindow::CreateContent()
{
    auto contentPanel = new wxCustomPanel(m_frameContent, FromDIP(wxSize(GetSize().GetWidth(), GetSize().GetHeight())));
    contentPanel->SetBackgroundColour(StylesFactory::BackgroundColor);

    auto mainSizer = new wxBoxSizer(wxVERTICAL);
    auto messageSizer = new wxBoxSizer(wxHORIZONTAL);
    auto aboutBoxSizer = new wxBoxSizer(wxVERTICAL);
    auto buttonSizer = new wxBoxSizer(wxVERTICAL);

    auto readyTechTitle = new wxStaticText(contentPanel, wxID_ANY, wxT("ReadyTech"));
    readyTechTitle->SetFont(READY_TECH_LABEL_FONT);
    auto readyTechMsgVertSizer = new wxBoxSizer(wxVERTICAL);
    auto readyTechMessageSizer = new wxBoxSizer(wxHORIZONTAL);
    readyTechMsgVertSizer->Add(readyTechTitle, 1, wxCENTER | wxALL, FromDIP(20));

    m_aboutMessage = new wxStaticText(contentPanel, wxID_ANY, _(READY_TECH_TEXT_MSG1));
    m_readyTechLink = new wxHyperlinkCtrl(contentPanel, wxID_ANY, _(READY_TECH_TEXT_LINK), "https://readytech.com/");
    m_contactUsMessage = new wxStaticText(contentPanel, wxID_ANY, _(READY_TECH_TEXT_MSG2));
    readyTechMessageSizer->Add(m_aboutMessage, wxSizerFlags(0).Expand());

    this->SetClientSize(FromDIP(wxSize(700, 700))); // setting new size here
    int readyTechLinkBorder = 20;
    readyTechMessageSizer->Add(m_readyTechLink, wxSizerFlags(0).Expand());
    readyTechMessageSizer->Add(new wxStaticText(contentPanel, wxID_ANY, "."), wxSizerFlags(0).Expand());
    readyTechMsgVertSizer->Add(readyTechMessageSizer, wxSizerFlags().Expand().Align(wxCENTER).Border(wxLEFT | wxRIGHT, FromDIP(20)));
    readyTechMsgVertSizer->Add(m_contactUsMessage, wxSizerFlags(1).Expand().Border(wxRIGHT | wxLEFT, FromDIP(20)).Align(wxCENTER));
    aboutBoxSizer->Add(readyTechMsgVertSizer, 0, wxDOWN | wxRIGHT | wxEXPAND, FromDIP(15));

    m_readyTechCopyrightLabel = new wxStaticText(contentPanel, wxID_ANY, wxString::Format(_(READY_TECH_COPYRIGHT_MSG), COMPUTE_BUILD_YEAR));
    m_readyTechCopyrightLabel->Wrap(-1);
    aboutBoxSizer->Add(m_readyTechCopyrightLabel, 1, wxCENTER);
    messageSizer->Add(aboutBoxSizer, 1, wxALIGN_CENTER_VERTICAL);
    mainSizer->Add(messageSizer, 1, wxALIGN_CENTER_HORIZONTAL);

    auto buttonCancel = wxUtils::CreateTransparentButton(contentPanel, wxID_CANCEL, FromDIP(ButtonSize), GetFont(), wxColour(216, 30, 91), nullptr, _("btn.Close"), wxColour(216, 30, 91, 191),
                                                         wxColour(182, 25, 77), wxALIGN_CENTER);
    buttonSizer->Add(buttonCancel, wxSizerFlags(0).Border(wxALL, FromDIP(DefaultBorder)).Expand());
    mainSizer->Add(buttonSizer, 1, wxALIGN_CENTER_HORIZONTAL);

    contentPanel->SetSizer(mainSizer);
    contentPanel->Center();
    contentPanel->Fit();
    contentPanel->Layout();

    m_topMenu = new wxMenuBar();
    SetMenuBar(m_topMenu);

    Fit();
    Layout();

    return contentPanel;
}
So if I set size in constructor, then everything ok. But when I'm trying to resize windows inside createContent function, then the window isn't resized.
What might be the reason of the problem?
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Resize window in runtime

Post by doublemax »

After a quick look my first suspicion is that the final Fit() call overwrites the size you set.
Use the source, Luke!
rudolfninja
Earned some good credits
Earned some good credits
Posts: 107
Joined: Tue Aug 28, 2018 1:02 pm
Location: Belarus

Re: Resize window in runtime

Post by rudolfninja »

Commented all Fit() calls, but it didn't help
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Resize window in runtime

Post by doublemax »

I can't spot anything wrong. Put the SetClientSize() into any button event handler and check if it works from there. If not, it's restricted somewhere, e.g. my a SetMin/Max size call. Trace through the SetClientSize() method and check what happens.

It it does work from inside a button event handler, the size must be overwritten somewhere else, maybe in code you're not showing.
Use the source, Luke!
rudolfninja
Earned some good credits
Earned some good credits
Posts: 107
Joined: Tue Aug 28, 2018 1:02 pm
Location: Belarus

Re: Resize window in runtime

Post by rudolfninja »

Problem was here:

Code: Select all

auto contentPanel = new wxCustomPanel(m_frameContent, FromDIP(wxSize(GetSize().GetWidth(), GetSize().GetHeight())));
So I calculated the size before creating contenPanel and use correct size in it's constructor.
Thanks for helping.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Resize window in runtime

Post by ONEEYEMAN »

Hi,
You use sizers.
When you use sizers the size of the TLW will be calculated appropriately. No need to play with the wxSize() parameters, and all those setters.

I believe all you need to do is to call:

Code: Select all

    SetSizer( main_sizer );
    Layout();
and the TLW will be sized accordingly.

Am I missing something?

Thank you.
Post Reply