Problem with sizers! [updated code] [solved] 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
Double Trouble
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Jun 14, 2008 12:42 pm
Location: Sweden
Contact:

Problem with sizers! [updated code] [solved]

Post by Double Trouble »

Hi!
First off, what I want to achieve:

I want the static text to be centered horizontally. Right now (see pic) the left border of the static text is centered and it doesn't look good.


Image


I would also want to center the static text vertically but that wouldn't work for me so I just gave it a distance from the bottom! Here is the constructor. I'm sorry about the messy code but I haven't spend anytime making it look good yet,

Code: Select all

                
                SetIcon(wxIcon(wxT("wxwin.ico"), wxBITMAP_TYPE_ICO)); 
		
		srand(time(0));

		panel = new wxPanel(this, -1);

		menubar = new wxMenuBar;
		filem = new wxMenu;
		config = new wxMenu;

		quit = new wxMenuItem(filem, wxID_CLOSE, wxT("&Quit\tCtrl+W"));
		substantiv = new wxMenuItem(config, wxID_SUBSTANTIV, wxT("&Substantiv"));
		hiragana = new wxMenuItem(config, wxID_HIRAGANA, wxT("&Hiragana"));
		katakana = new wxMenuItem(config, wxID_KATAKANA, wxT("&Katakana"));
		


		menubar->Append(filem, wxT("&File"));
		menubar->Append(config, wxT("&Config"));
		filem->Append(quit);
		config->Append(hiragana);
		config->Append(katakana);
		config->Append(substantiv);

		Connect(wxID_CLOSE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Window::OnQuit));

		Connect(wxID_SUBSTANTIV, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Window::OnSubstantiv));
		Connect(wxID_HIRAGANA, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Window::OnHiragana));
		Connect(wxID_KATAKANA, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Window::OnKatakana));


		csnd = new wxSound(_T("chimes.wav"), false);
		wsnd = new wxSound(_T("Windows_Error.wav"), false);
		
		wxColour col1;
		col1.Set(wxT("#FFFFFF"));
		panel->SetBackgroundColour(col1);
		
		
		

		vbox = new wxBoxSizer(wxVERTICAL);
		hbox1 = new wxBoxSizer(wxHORIZONTAL);
		hbox2 = new wxBoxSizer(wxHORIZONTAL);
		hbox3 = new wxBoxSizer(wxHORIZONTAL);
		
		wxFont font;
		font.SetPointSize(30);
		
		
		st = new wxStaticText(panel, wxID_ANY, wxT(""));
		st->SetFont(font);
		next = new wxButton(panel, ID_NEXT, wxT("Skip word"));

		Connect(ID_NEXT, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Window::OnNext));
		


		
		userin = new wxTextCtrl(panel, -1, wxT(""), wxPoint(-1, -1));
		submit = new wxButton(panel, ID_SUB, wxT("Submit"));
		Connect(ID_SUB, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Window::OnSub));
		userin->SetFocus();
		

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

		hbox2->Add(st,0,wxBOTTOM, 200); 
	
		hbox3->Add(userin, 0, wxEXPAND | wxRIGHT, 2);
		hbox3->Add(submit, 0, wxLEFT, 2);
		hbox3->Add(next, 0, wxEXPAND);

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

		SetMenuBar(menubar);

What would you do differently to achieve the result I want?

Thank you in advance
/DT
Last edited by Double Trouble on Tue Jun 24, 2008 9:21 pm, edited 3 times in total.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

You don't show enough code, what are hbox and vbox, where do you create them, where do you use SetSizer on hbox?
Double Trouble
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Jun 14, 2008 12:42 pm
Location: Sweden
Contact:

Post by Double Trouble »

Auria wrote:You don't show enough code, what are hbox and vbox, where do you create them, where do you use SetSizer on hbox?
Ok! I'll post more!

Here is the constructor:

Code: Select all

                
                SetIcon(wxIcon(wxT("wxwin.ico"), wxBITMAP_TYPE_ICO)); 
		
		srand(time(0));

		panel = new wxPanel(this, -1);

		menubar = new wxMenuBar;
		filem = new wxMenu;
		config = new wxMenu;

		quit = new wxMenuItem(filem, wxID_CLOSE, wxT("&Quit\tCtrl+W"));
		substantiv = new wxMenuItem(config, wxID_SUBSTANTIV, wxT("&Substantiv"));
		hiragana = new wxMenuItem(config, wxID_HIRAGANA, wxT("&Hiragana"));
		katakana = new wxMenuItem(config, wxID_KATAKANA, wxT("&Katakana"));
		


		menubar->Append(filem, wxT("&File"));
		menubar->Append(config, wxT("&Config"));
		filem->Append(quit);
		config->Append(hiragana);
		config->Append(katakana);
		config->Append(substantiv);

		Connect(wxID_CLOSE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Window::OnQuit));

		Connect(wxID_SUBSTANTIV, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Window::OnSubstantiv));
		Connect(wxID_HIRAGANA, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Window::OnHiragana));
		Connect(wxID_KATAKANA, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Window::OnKatakana));


		csnd = new wxSound(_T("chimes.wav"), false);
		wsnd = new wxSound(_T("Windows_Error.wav"), false);
		
		wxColour col1;
		col1.Set(wxT("#FFFFFF"));
		panel->SetBackgroundColour(col1);
		
		
		

		vbox = new wxBoxSizer(wxVERTICAL);
		hbox1 = new wxBoxSizer(wxHORIZONTAL);
		hbox2 = new wxBoxSizer(wxHORIZONTAL);
		hbox3 = new wxBoxSizer(wxHORIZONTAL);
		
		wxFont font;
		font.SetPointSize(30);
		
		
		st = new wxStaticText(panel, wxID_ANY, wxT(""));
		st->SetFont(font);
		next = new wxButton(panel, ID_NEXT, wxT("Skip word"));

		Connect(ID_NEXT, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Window::OnNext));
		


		
		userin = new wxTextCtrl(panel, -1, wxT(""), wxPoint(-1, -1));
		submit = new wxButton(panel, ID_SUB, wxT("Submit"));
		Connect(ID_SUB, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Window::OnSub));
		userin->SetFocus();
		

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

		hbox2->Add(st,0,wxBOTTOM, 200); 
	
		hbox3->Add(userin, 0, wxEXPAND | wxRIGHT, 2);
		hbox3->Add(submit, 0, wxLEFT, 2);
		hbox3->Add(next, 0, wxEXPAND);

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

		SetMenuBar(menubar);
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

The probably isn't the cause of your error, but I can't help but wonder what this line is supposed to do :

Code: Select all

hbox1->Add(new wxPanel(panel, -1)); 
It adds an empty panel... what use does it have?

What I do notice about your issue, however, is that you initially create the static text with no text inside it. I suppose your app fills it with text later? Do you call the sizer for a re-layout after adding text? (wxSizer::Layout())
Double Trouble
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Jun 14, 2008 12:42 pm
Location: Sweden
Contact:

Post by Double Trouble »

The problem wasn't to difficult. I just had to change from

Code: Select all

st = new wxStaticText(panel, wxID_ANY, wxT(""));
to

Code: Select all

st =  new wxStaticText(panel, wxID_ANY, wxT("いっらしゃいませ!"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE | wxST_NO_AUTORESIZE);
The only problem is that I can't use words wider than "いっらしゃいませ!" now since I disables the auto resize.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Can't you just NOT use wxST_NO_AUTORESIZE??

When you do add the label, just call ->Layout() on your sizer so that components are placed correctly
Double Trouble
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Jun 14, 2008 12:42 pm
Location: Sweden
Contact:

Post by Double Trouble »

Auria wrote:Can't you just NOT use wxST_NO_AUTORESIZE??

When you do add the label, just call ->Layout() on your sizer so that components are placed correctly
With the Layout()-func it worked perfectly! Thanks lot!
Post Reply