I am new to wxWidgets and reading the documentation i thoguht id tackle these sizer objects head on as it seems one of the most complicated areas to first get your head around.
I believe I understand them and tried to implement a dialog with 1 list box and two buttons. However when i run it everything is squashed into the top left corner. As soon as i stretch the frame (even by 1 pixel) everything suddenly jumps to the correct position and behaves correctly. It is just first load that is broke. Can someone take a look please?
Thanks
Code: Select all
MainApp.cpp /////////////////////////////////////////
#include "MainApp.h"
#include "MainDlg.h"
IMPLEMENT_APP(MainApp)
MainApp::~MainApp(void)
{
}
bool MainApp::OnInit()
{
MainDlg *frame = new MainDlg( _("MainApp"), wxPoint(-1, -1), wxSize(450, 340) );
frame->Show(true);
SetTopWindow(frame);
return true;
}
MainApp.h /////////////////////////////////////
class MainApp : public wxApp
{
public:
virtual bool OnInit();
virtual ~MainApp( ) ;
};
MainDlg.h //////////////////////////////////////
#include "wx/wx.h"
class MainDlg: public wxFrame
{
public:
MainDlg(const wxString& title, const wxPoint& pos, const wxSize& size);
void OnQuit(wxCommandEvent& event);
void OnListDblClick(wxCommandEvent& event);
void OnAdd(wxCommandEvent& event);
DECLARE_EVENT_TABLE()
enum
{
ID_LIST,
ID_ADD,
ID_CLOSE
};
private:
void CreateListBox( wxPanel* parent, wxSizer* sizer ) ;
void CreateButtons( wxPanel* parent, wxSizer* sizer ) ;
wxListBox* m_wxList ;
wxButton* m_wxBtnClose ;
wxButton* m_wxBtnAdd ;
};
MainDlg.cpp ///////////////////////////////////////////////
#include "Config.h"
#include "MainDlg.h"
BEGIN_EVENT_TABLE(MainDlg, wxFrame)
EVT_MENU(wxID_EXIT, MainDlg::OnQuit)
EVT_LISTBOX_DCLICK(MainDlg::ID_LIST, MainDlg::OnListDblClick)
EVT_BUTTON(MainDlg::ID_CLOSE, MainDlg::OnQuit)
EVT_BUTTON(MainDlg::ID_ADD, MainDlg::OnAdd)
END_EVENT_TABLE()
MainDlg::MainDlg(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, -1, title, pos, size)
, m_wxList(NULL)
{
wxPanel* panel = new wxPanel(this, -1) ;
wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL) ;
CreateListBox( panel, vbox ) ;
CreateButtons( panel, vbox ) ;
panel->SetSizer(vbox) ;
Center( ) ;
}
void MainDlg::CreateListBox( wxPanel* parent, wxSizer* sizer )
{
m_wxList = new wxListBox(parent, ID_LIST,
wxPoint(-1, -1), wxSize(-1, -1)) ;
sizer->Add(m_wxList, 15, wxEXPAND | wxALL, 20) ;
}
void MainDlg::CreateButtons( wxPanel* parent, wxSizer* sizer )
{
wxPanel* panel = new wxPanel(parent, -1);
wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);
m_wxBtnClose = new wxButton(panel, ID_CLOSE, wxT("Quit"));
m_wxBtnAdd = new wxButton(panel, ID_ADD, wxT("Add"));
hbox->Add(m_wxBtnAdd,1);
hbox->Add(m_wxBtnClose,1);
panel->SetSizer(hbox);
sizer->Add(panel, 3, wxCENTER, 20);
}
void MainDlg::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(true);
}
void MainDlg::OnListDblClick(wxCommandEvent& WXUNUSED(event))
{
// TODO
}
void MainDlg::OnAdd(wxCommandEvent& WXUNUSED(event))
{
// TODO
}