What is this code doing? Topic is solved

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
wx_n00b
In need of some credit
In need of some credit
Posts: 9
Joined: Thu Sep 05, 2013 12:07 am

What is this code doing?

Post by wx_n00b »

Hello. I'm following this code:
http://zetcode.com/gui/wxwidgets/layoutmanagement/

The file is align.cpp:

Code: Select all

#include "align.h"

Align::Align(const wxString& title)
       : wxFrame(NULL, -1, title, wxPoint(-1, -1), wxSize(300, 200))
{
  wxPanel *panel = new wxPanel(this, -1);

  wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
  wxBoxSizer *hbox1 = new wxBoxSizer(wxHORIZONTAL);
  wxBoxSizer *hbox2 = new wxBoxSizer(wxHORIZONTAL);

  wxButton *ok = new wxButton(panel, -1, wxT("Ok"));
  wxButton *cancel = new wxButton(panel, -1, wxT("Cancel"));

  hbox1->Add(new wxPanel(panel, -1));
  vbox->Add(hbox1, 1, wxEXPAND);

  hbox2->Add(ok);
  hbox2->Add(cancel);

  vbox->Add(hbox2, 0, wxALIGN_RIGHT | wxRIGHT | wxBOTTOM, 10);
  panel->SetSizer(vbox);

  Centre();
}
The way I see it, when you're adding to hbox1, you're adding a wxPanel who's parent is the original panel. No? Later on, you're adding that to the vbox.

To hbox2, you're adding two buttons. Then, you're adding hbox2 to vbox. Then the sizer is set to be what vbox has, yes?

Not too sure I got all that correctly :) .
wx_n00b
In need of some credit
In need of some credit
Posts: 9
Joined: Thu Sep 05, 2013 12:07 am

Re: What is this code doing?

Post by wx_n00b »

Also, why do the buttons have the panel as the parent? Do they need to be "connected" in this fashion?
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: What is this code doing?

Post by doublemax »

The parts that probably confuses you is the first wxPanel, because it's not in any sizer, but acts as parent for the other controls.

Code: Select all

wxPanel *panel = new wxPanel(this, -1);
If a wxFrame has only exactly one child (not ancestors), this child will always fill the whole client area. It's usually recommended to have a wxPanel as "background" for all other controls. It contains code for keyboard navigation and under MSW you'll also get a darker background color for the frame, if you don't have a panel as background.

The rest of the code should be clear, it's just a few nested sizers. To understand sizers, this page may also help:
http://neume.sourceforge.net/sizerdemo/
Use the source, Luke!
Post Reply