Define wxWindow position relative to another window's edge

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
dohpam1ne
Earned a small fee
Earned a small fee
Posts: 13
Joined: Thu May 14, 2020 11:27 pm

Define wxWindow position relative to another window's edge

Post by dohpam1ne » Mon May 18, 2020 9:14 pm

Hi all,

I'm a beginner, and I feel like the answer to this question must be out there, but I can't seem to find it.

I'm defining two child windows within a wxFrame. They're both very basic subclasses of wxPanel. I want them to completely fill the parent wxFrame in which they reside, so I'd just have two side-by-side windows encased in a wxFrame. I figured the best way to do this was to set the position of the right-side frame based on the position of the left-side frame plus its width, so I'd have rightSideXPos = (leftSideXPos + leftSideWidth). Is there a preferred function for retrieving the current width of a wxPanel? Thanks! Here's the relevant part of my current code:

Code: Select all

ConfigPanel* cPanel = new ConfigPanel(frame, "Configuration Panel", 
	wxSize(600, 400), 0); // final arg is horizontal offset
DisplayPanel* dPanel = new DisplayPanel(frame, "Configuration Panel", 
	wxSize(600, 400), 600); // final arg is horizontal offset- want to replace with cPanel width
frame->Show(true);
frame->Fit();

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2478
Joined: Sun Jan 03, 2010 5:45 pm

Re: Define wxWindow position relative to another window's edge

Post by PB » Mon May 18, 2020 9:17 pm

You probably do not want to hear that but still: Use sizers they make things like this very easy. OTOH, with absolute positions and sizes it is virtually impossible to achieve the goal on a wide range of systems (different themes, DPI scaling etc.).

Code: Select all

#include <wx/wx.h>

class MyFrame : public wxFrame
{
public:
    MyFrame() : wxFrame(nullptr, wxID_ANY, "Test")
    {
        wxBoxSizer* mainSizer = new wxBoxSizer(wxHORIZONTAL);
        wxPanel* leftPanel = new wxPanel(this);
        wxPanel* rightPanel = new wxPanel(this);

        leftPanel->SetBackgroundColour(*wxRED);
        rightPanel->SetBackgroundColour(*wxGREEN);

        mainSizer->Add(leftPanel, wxSizerFlags().Proportion(1).Expand());
        mainSizer->Add(rightPanel, wxSizerFlags().Proportion(1).Expand());
        SetSizer(mainSizer);
    }
};

class MyApp : public wxApp
{
public:
    bool OnInit() override
    {
        (new MyFrame())->Show();
        return true;
    }
}; wxIMPLEMENT_APP(MyApp);
twopanels.png
twopanels.png (2.12 KiB) Viewed 190 times

dohpam1ne
Earned a small fee
Earned a small fee
Posts: 13
Joined: Thu May 14, 2020 11:27 pm

Re: Define wxWindow position relative to another window's edge

Post by dohpam1ne » Tue May 19, 2020 9:58 am

I didn't realize sizers were used for this type of thing, I'll try that out. Thanks!

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2478
Joined: Sun Jan 03, 2010 5:45 pm

Re: Define wxWindow position relative to another window's edge

Post by PB » Tue May 19, 2020 10:47 am

Well, sizers are used for positioning and sizing everything. This scenario with just two panels is very simple, but sizers' strength shows in more complex UI layouts.

Post Reply