]> git.saurik.com Git - wxWidgets.git/blame - samples/layout/layout.cpp
Rebake all the samples, demos and tests makefiles.
[wxWidgets.git] / samples / layout / layout.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: layout.cpp
3// Purpose: Layout sample
4// Author: Julian Smart
7235f8e1 5// Modified by: Robin Dunn, Vadim Zeitlin
c801d85f
KB
6// Created: 04/01/98
7// RCS-ID: $Id$
7235f8e1
VZ
8// Copyright: (c) 1998 Julian Smart
9// 2005 Vadim Zeitlin
2f6c54eb 10// Licence: wxWindows license
c801d85f
KB
11/////////////////////////////////////////////////////////////////////////////
12
9e023db7
VZ
13// ----------------------------------------------------------------------------
14// headers
15// ----------------------------------------------------------------------------
16
c801d85f
KB
17// For compilers that support precompilation, includes "wx/wx.h".
18#include "wx/wxprec.h"
19
20#ifdef __BORLANDC__
21#pragma hdrstop
22#endif
23
24#ifndef WX_PRECOMP
25#include "wx/wx.h"
26#endif
27
c62ac5b6 28#include "wx/sizer.h"
20b35a69 29#include "wx/gbsizer.h"
61d514bb 30#include "wx/statline.h"
83edc0a5 31#include "wx/notebook.h"
7235f8e1 32#include "wx/spinctrl.h"
73d1f52b 33#include "wx/wrapsizer.h"
c62ac5b6 34
c801d85f
KB
35#include "layout.h"
36
41f02b9a
FM
37#ifndef __WXMSW__
38 #include "../sample.xpm"
39#endif
40
41
9e023db7
VZ
42// ----------------------------------------------------------------------------
43// MyApp
44// ----------------------------------------------------------------------------
c801d85f
KB
45
46IMPLEMENT_APP(MyApp)
47
83edc0a5 48bool MyApp::OnInit()
c801d85f 49{
45e6e6f8
VZ
50 if ( !wxApp::OnInit() )
51 return false;
52
c801d85f 53 // Create the main frame window
9e023db7
VZ
54 MyFrame *frame = new MyFrame;
55
9230b621 56 frame->Show(true);
9e023db7 57
9230b621 58 return true;
9e023db7
VZ
59}
60
61// ----------------------------------------------------------------------------
62// MyFrame
63// ----------------------------------------------------------------------------
c801d85f 64
9e023db7
VZ
65BEGIN_EVENT_TABLE(MyFrame, wxFrame)
66 EVT_MENU(LAYOUT_ABOUT, MyFrame::OnAbout)
67 EVT_MENU(LAYOUT_QUIT, MyFrame::OnQuit)
c801d85f 68
7235f8e1 69 EVT_MENU(LAYOUT_TEST_PROPORTIONS, MyFrame::TestProportions)
9e023db7
VZ
70 EVT_MENU(LAYOUT_TEST_SIZER, MyFrame::TestFlexSizers)
71 EVT_MENU(LAYOUT_TEST_NB_SIZER, MyFrame::TestNotebookSizers)
925e9792 72 EVT_MENU(LAYOUT_TEST_GB_SIZER, MyFrame::TestGridBagSizer)
92c0c172 73 EVT_MENU(LAYOUT_TEST_SET_MINIMAL, MyFrame::TestSetMinimal)
a1a3bffc 74 EVT_MENU(LAYOUT_TEST_NESTED, MyFrame::TestNested)
15f7c305 75 EVT_MENU(LAYOUT_TEST_WRAP, MyFrame::TestWrap)
9e023db7 76END_EVENT_TABLE()
c801d85f 77
9e023db7
VZ
78// Define my frame constructor
79MyFrame::MyFrame()
9a83f860 80 : wxFrame(NULL, wxID_ANY, wxT("wxWidgets Layout Demo"),
e2512c23 81 wxPoint(30,30), wxDefaultSize,
9e023db7
VZ
82 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
83{
41f02b9a 84 SetIcon(wxICON(sample));
c801d85f 85
41f02b9a
FM
86 // Make a menubar
87 wxMenu *file_menu = new wxMenu;
c801d85f 88
9a83f860
VZ
89 file_menu->Append(LAYOUT_TEST_PROPORTIONS, wxT("&Proportions demo...\tF1"));
90 file_menu->Append(LAYOUT_TEST_SIZER, wxT("Test wx&FlexSizer...\tF2"));
91 file_menu->Append(LAYOUT_TEST_NB_SIZER, wxT("Test &notebook sizers...\tF3"));
92 file_menu->Append(LAYOUT_TEST_GB_SIZER, wxT("Test &gridbag sizer...\tF4"));
93 file_menu->Append(LAYOUT_TEST_SET_MINIMAL, wxT("Test Set&ItemMinSize...\tF5"));
94 file_menu->Append(LAYOUT_TEST_NESTED, wxT("Test nested sizer in a wxPanel...\tF6"));
95 file_menu->Append(LAYOUT_TEST_WRAP, wxT("Test wrap sizers...\tF7"));
c801d85f 96
41f02b9a 97 file_menu->AppendSeparator();
9a83f860 98 file_menu->Append(LAYOUT_QUIT, wxT("E&xit"), wxT("Quit program"));
c801d85f 99
41f02b9a 100 wxMenu *help_menu = new wxMenu;
9a83f860 101 help_menu->Append(LAYOUT_ABOUT, wxT("&About"), wxT("About layout demo..."));
c801d85f 102
41f02b9a
FM
103 wxMenuBar *menu_bar = new wxMenuBar;
104
9a83f860
VZ
105 menu_bar->Append(file_menu, wxT("&File"));
106 menu_bar->Append(help_menu, wxT("&Help"));
c801d85f 107
41f02b9a
FM
108 // Associate the menu bar with the frame
109 SetMenuBar(menu_bar);
9e023db7 110
8520f137 111#if wxUSE_STATUSBAR
41f02b9a 112 CreateStatusBar(2);
9a83f860 113 SetStatusText(wxT("wxWidgets layout demo"));
8520f137 114#endif // wxUSE_STATUSBAR
9e023db7 115
41f02b9a
FM
116 wxPanel* p = new wxPanel(this, wxID_ANY);
117
118 // we want to get a dialog that is stretchable because it
119 // has a text ctrl in the middle. at the bottom, we have
120 // two buttons which.
121
122 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
123
124 // 1) top: create wxStaticText with minimum size equal to its default size
125 topsizer->Add(
9a83f860 126 new wxStaticText( p, wxID_ANY, wxT("An explanation (wxALIGN_RIGHT).") ),
41f02b9a
FM
127 wxSizerFlags().Align(wxALIGN_RIGHT).Border(wxALL & ~wxBOTTOM, 5));
128 topsizer->Add(
9a83f860 129 new wxStaticText( p, wxID_ANY, wxT("An explanation (wxALIGN_LEFT).") ),
41f02b9a
FM
130 wxSizerFlags().Align(wxALIGN_LEFT).Border(wxALL & ~wxBOTTOM, 5));
131 topsizer->Add(
9a83f860 132 new wxStaticText( p, wxID_ANY, wxT("An explanation (wxALIGN_CENTRE_HORIZONTAL).") ),
41f02b9a
FM
133 wxSizerFlags().Align(wxALIGN_CENTRE_HORIZONTAL).Border(wxALL & ~wxBOTTOM, 5));
134
135 // 2) top: create wxTextCtrl with minimum size (100x60)
136 topsizer->Add(
9a83f860 137 new wxTextCtrl( p, wxID_ANY, wxT("My text (wxEXPAND)."), wxDefaultPosition, wxSize(100,60), wxTE_MULTILINE),
41f02b9a
FM
138 wxSizerFlags(1).Expand().Border(wxALL, 5));
139
140 // 2.5) Gratuitous test of wxStaticBoxSizers
141 wxBoxSizer *statsizer = new wxStaticBoxSizer(
9a83f860 142 new wxStaticBox(p, wxID_ANY, wxT("A wxStaticBoxSizer")), wxVERTICAL );
41f02b9a 143 statsizer->Add(
9a83f860 144 new wxStaticText(p, wxID_ANY, wxT("And some TEXT inside it")),
41f02b9a
FM
145 wxSizerFlags().Border(wxALL, 30));
146 topsizer->Add(
147 statsizer,
148 wxSizerFlags(1).Expand().Border(wxALL, 10));
9e023db7
VZ
149
150 // 2.7) And a test of wxGridSizer
151 wxGridSizer *gridsizer = new wxGridSizer(2, 5, 5);
9a83f860 152 gridsizer->Add(new wxStaticText(p, wxID_ANY, wxT("Label")),
41f02b9a 153 wxSizerFlags().Align(wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL));
9a83f860 154 gridsizer->Add(new wxTextCtrl(p, wxID_ANY, wxT("Grid sizer demo")),
41f02b9a 155 wxSizerFlags(1).Align(wxGROW | wxALIGN_CENTER_VERTICAL));
9a83f860 156 gridsizer->Add(new wxStaticText(p, wxID_ANY, wxT("Another label")),
41f02b9a 157 wxSizerFlags().Align(wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL));
9a83f860 158 gridsizer->Add(new wxTextCtrl(p, wxID_ANY, wxT("More text")),
41f02b9a 159 wxSizerFlags(1).Align(wxGROW | wxALIGN_CENTER_VERTICAL));
9a83f860 160 gridsizer->Add(new wxStaticText(p, wxID_ANY, wxT("Final label")),
41f02b9a 161 wxSizerFlags().Align(wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL));
9a83f860 162 gridsizer->Add(new wxTextCtrl(p, wxID_ANY, wxT("And yet more text")),
41f02b9a 163 wxSizerFlags().Align(wxGROW | wxALIGN_CENTER_VERTICAL));
e5544e77
VZ
164 topsizer->Add(
165 gridsizer,
166 wxSizerFlags().Proportion(1).Expand().Border(wxALL, 10));
9e023db7
VZ
167
168
4aa0bd9b 169#if wxUSE_STATLINE
41f02b9a
FM
170 // 3) middle: create wxStaticLine with minimum size (3x3)
171 topsizer->Add(
172 new wxStaticLine( p, wxID_ANY, wxDefaultPosition, wxSize(3,3), wxHORIZONTAL),
173 wxSizerFlags().Expand());
4aa0bd9b 174#endif // wxUSE_STATLINE
9e023db7
VZ
175
176
41f02b9a
FM
177 // 4) bottom: create two centred wxButtons
178 wxBoxSizer *button_box = new wxBoxSizer( wxHORIZONTAL );
179 button_box->Add(
9a83f860 180 new wxButton( p, wxID_ANY, wxT("Two buttons in a box") ),
41f02b9a
FM
181 wxSizerFlags().Border(wxALL, 7));
182 button_box->Add(
9a83f860 183 new wxButton( p, wxID_ANY, wxT("(wxCENTER)") ),
41f02b9a 184 wxSizerFlags().Border(wxALL, 7));
9e023db7 185
41f02b9a 186 topsizer->Add(button_box, wxSizerFlags().Center());
9e023db7 187
41f02b9a 188 p->SetSizer( topsizer );
6bbb8a8d 189
41f02b9a
FM
190 // don't allow frame to get smaller than what the sizers tell it and also set
191 // the initial size as calculated by the sizers
192 topsizer->SetSizeHints( this );
9e023db7
VZ
193}
194
7235f8e1 195void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
9e023db7 196{
9230b621 197 Close(true);
9e023db7
VZ
198}
199
7235f8e1
VZ
200void MyFrame::TestProportions(wxCommandEvent& WXUNUSED(event))
201{
202 (new MyProportionsFrame(this))->Show();
203}
204
9e023db7
VZ
205void MyFrame::TestFlexSizers(wxCommandEvent& WXUNUSED(event) )
206{
9a83f860 207 MyFlexSizerFrame *newFrame = new MyFlexSizerFrame(wxT("Flex Sizer Test Frame"), 50, 50);
9230b621 208 newFrame->Show(true);
9e023db7
VZ
209}
210
211void MyFrame::TestNotebookSizers(wxCommandEvent& WXUNUSED(event) )
212{
9a83f860 213 MySizerDialog dialog( this, wxT("Notebook Sizer Test Dialog") );
9e023db7
VZ
214
215 dialog.ShowModal();
216}
217
92c0c172
RR
218void MyFrame::TestSetMinimal(wxCommandEvent& WXUNUSED(event) )
219{
9a83f860 220 MySimpleSizerFrame *newFrame = new MySimpleSizerFrame(wxT("Simple Sizer Test Frame"), 50, 50);
92c0c172
RR
221 newFrame->Show(true);
222}
223
a1a3bffc
RR
224void MyFrame::TestNested(wxCommandEvent& WXUNUSED(event) )
225{
9a83f860 226 MyNestedSizerFrame *newFrame = new MyNestedSizerFrame(wxT("Nested Sizer Test Frame"), 50, 50);
a1a3bffc
RR
227 newFrame->Show(true);
228}
229
15f7c305
RR
230void MyFrame::TestWrap(wxCommandEvent& WXUNUSED(event) )
231{
9a83f860 232 MyWrapSizerFrame *newFrame = new MyWrapSizerFrame(wxT("Wrap Sizer Test Frame"), 50, 50);
15f7c305
RR
233 newFrame->Show(true);
234}
235
c801d85f 236
9e023db7
VZ
237void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
238{
9a83f860
VZ
239 (void)wxMessageBox(wxT("wxWidgets GUI library layout demo\n"),
240 wxT("About Layout Demo"), wxOK|wxICON_INFORMATION);
9e023db7
VZ
241}
242
20b35a69
RD
243void MyFrame::TestGridBagSizer(wxCommandEvent& WXUNUSED(event) )
244{
245 MyGridBagSizerFrame *newFrame = new
9a83f860 246 MyGridBagSizerFrame(wxT("wxGridBagSizer Test Frame"), 50, 50);
9230b621 247 newFrame->Show(true);
20b35a69
RD
248}
249
7235f8e1
VZ
250// ----------------------------------------------------------------------------
251// MyProportionsFrame
252// ----------------------------------------------------------------------------
253
254MyProportionsFrame::MyProportionsFrame(wxFrame *parent)
9a83f860 255 : wxFrame(parent, wxID_ANY, wxT("Box Sizer Proportions Demo"))
7235f8e1
VZ
256{
257 size_t n;
258
259 // create the controls
260 wxPanel *panel = new wxPanel(this, wxID_ANY);
261 for ( n = 0; n < WXSIZEOF(m_spins); n++ )
262 {
263 m_spins[n] = new wxSpinCtrl(panel);
264 m_spins[n]->SetValue(n);
265 }
266
267 // lay them out
268 m_sizer = new wxStaticBoxSizer(wxHORIZONTAL, panel,
9a83f860 269 wxT("Try changing elements proportions and resizing the window"));
7235f8e1
VZ
270 for ( n = 0; n < WXSIZEOF(m_spins); n++ )
271 m_sizer->Add(m_spins[n], wxSizerFlags().Border());
272
273 // put everything together
274 panel->SetSizer(m_sizer);
275 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
276 sizerTop->Add(panel, wxSizerFlags(1).Expand().Border());
277 UpdateProportions();
278 SetSizerAndFit(sizerTop);
279
280 // and connect the events
281 Connect(wxEVT_COMMAND_TEXT_UPDATED,
282 wxCommandEventHandler(MyProportionsFrame::OnProportionUpdated));
283 Connect(wxEVT_COMMAND_SPINCTRL_UPDATED,
284 wxSpinEventHandler(MyProportionsFrame::OnProportionChanged));
285}
286
287void MyProportionsFrame::UpdateProportions()
288{
289 for ( size_t n = 0; n < WXSIZEOF(m_spins); n++ )
290 {
291 m_sizer->GetItem(n)->SetProportion(m_spins[n]->GetValue());
292 }
293
294 m_sizer->Layout();
295}
296
297void MyProportionsFrame::OnProportionUpdated(wxCommandEvent& WXUNUSED(event))
298{
299 UpdateProportions();
300}
301
302void MyProportionsFrame::OnProportionChanged(wxSpinEvent& WXUNUSED(event))
303{
304 UpdateProportions();
305}
20b35a69 306
9e023db7
VZ
307// ----------------------------------------------------------------------------
308// MyFlexSizerFrame
309// ----------------------------------------------------------------------------
c62ac5b6 310
bdbb07ed 311void MyFlexSizerFrame::InitFlexSizer(wxFlexGridSizer *sizer, wxWindow* parent)
c801d85f 312{
9e023db7
VZ
313 for ( int i = 0; i < 3; i++ )
314 {
315 for ( int j = 0; j < 3; j++ )
316 {
317 sizer->Add(new wxStaticText
318 (
bdbb07ed 319 parent,
9230b621 320 wxID_ANY,
9a83f860 321 wxString::Format(wxT("(%d, %d)"), i + 1, j + 1),
9e023db7
VZ
322 wxDefaultPosition,
323 wxDefaultSize,
324 wxALIGN_CENTER
325 ),
326 0, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 3);
327 }
328 }
c801d85f
KB
329}
330
15f7c305 331MyFlexSizerFrame::MyFlexSizerFrame(const wxString &title, int x, int y )
9230b621 332 : wxFrame(NULL, wxID_ANY, title, wxPoint(x, y) )
c801d85f 333{
9e023db7 334 wxFlexGridSizer *sizerFlex;
6bbb8a8d 335 wxPanel* p = new wxPanel(this, wxID_ANY);
9e023db7
VZ
336
337 // consttuct the first column
338 wxSizer *sizerCol1 = new wxBoxSizer(wxVERTICAL);
9a83f860 339 sizerCol1->Add(new wxStaticText(p, wxID_ANY, wxT("Ungrowable:")), 0, wxCENTER | wxTOP, 20);
90666795 340 sizerFlex = new wxFlexGridSizer(3, 3, wxSize(5, 5));
bdbb07ed 341 InitFlexSizer(sizerFlex, p);
9e023db7
VZ
342 sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
343
9a83f860 344 sizerCol1->Add(new wxStaticText(p, wxID_ANY, wxT("Growable middle column:")), 0, wxCENTER | wxTOP, 20);
90666795 345 sizerFlex = new wxFlexGridSizer(3, 3, wxSize(5, 5));
bdbb07ed 346 InitFlexSizer(sizerFlex, p);
9e023db7
VZ
347 sizerFlex->AddGrowableCol(1);
348 sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
349
9a83f860 350 sizerCol1->Add(new wxStaticText(p, wxID_ANY, wxT("Growable middle row:")), 0, wxCENTER | wxTOP, 20);
90666795 351 sizerFlex = new wxFlexGridSizer(3, 3, wxSize(5, 5));
bdbb07ed 352 InitFlexSizer(sizerFlex, p);
9e023db7
VZ
353 sizerFlex->AddGrowableRow(1);
354 sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
355
9a83f860 356 sizerCol1->Add(new wxStaticText(p, wxID_ANY, wxT("All growable columns:")), 0, wxCENTER | wxTOP, 20);
90666795 357 sizerFlex = new wxFlexGridSizer(3, 3, wxSize(5, 5));
bdbb07ed 358 InitFlexSizer(sizerFlex, p);
9e023db7
VZ
359 sizerFlex->AddGrowableCol(0, 1);
360 sizerFlex->AddGrowableCol(1, 2);
361 sizerFlex->AddGrowableCol(2, 3);
362 sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
363
364 // the second one
365 wxSizer *sizerCol2 = new wxBoxSizer(wxVERTICAL);
9a83f860 366 sizerCol2->Add(new wxStaticText(p, wxID_ANY, wxT("Growable middle row and column:")), 0, wxCENTER | wxTOP, 20);
90666795 367 sizerFlex = new wxFlexGridSizer(3, 3, wxSize(5, 5));
bdbb07ed 368 InitFlexSizer(sizerFlex, p);
9e023db7
VZ
369 sizerFlex->AddGrowableCol(1);
370 sizerFlex->AddGrowableRow(1);
371 sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
372
9a83f860 373 sizerCol2->Add(new wxStaticText(p, wxID_ANY, wxT("Same with horz flex direction")), 0, wxCENTER | wxTOP, 20);
90666795 374 sizerFlex = new wxFlexGridSizer(3, 3, wxSize(5, 5));
bdbb07ed 375 InitFlexSizer(sizerFlex, p);
9e023db7
VZ
376 sizerFlex->AddGrowableCol(1);
377 sizerFlex->AddGrowableRow(1);
378 sizerFlex->SetFlexibleDirection(wxHORIZONTAL);
379 sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
380
9a83f860 381 sizerCol2->Add(new wxStaticText(p, wxID_ANY, wxT("Same with grow mode == \"none\"")), 0, wxCENTER | wxTOP, 20);
90666795 382 sizerFlex = new wxFlexGridSizer(3, 3, wxSize(5, 5));
bdbb07ed 383 InitFlexSizer(sizerFlex, p);
9e023db7
VZ
384 sizerFlex->AddGrowableCol(1);
385 sizerFlex->AddGrowableRow(1);
386 sizerFlex->SetFlexibleDirection(wxHORIZONTAL);
387 sizerFlex->SetNonFlexibleGrowMode(wxFLEX_GROWMODE_NONE);
388 sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
389
9a83f860 390 sizerCol2->Add(new wxStaticText(p, wxID_ANY, wxT("Same with grow mode == \"all\"")), 0, wxCENTER | wxTOP, 20);
90666795 391 sizerFlex = new wxFlexGridSizer(3, 3, wxSize(5, 5));
bdbb07ed 392 InitFlexSizer(sizerFlex, p);
9e023db7
VZ
393 sizerFlex->AddGrowableCol(1);
394 sizerFlex->AddGrowableRow(1);
395 sizerFlex->SetFlexibleDirection(wxHORIZONTAL);
396 sizerFlex->SetNonFlexibleGrowMode(wxFLEX_GROWMODE_ALL);
397 sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
398
399 // add both columns to grid sizer
400 wxGridSizer *sizerTop = new wxGridSizer(2, 0, 20);
401 sizerTop->Add(sizerCol1, 1, wxEXPAND);
402 sizerTop->Add(sizerCol2, 1, wxEXPAND);
403
bdbb07ed 404 p->SetSizer(sizerTop);
9e023db7 405 sizerTop->SetSizeHints(this);
c801d85f
KB
406}
407
9e023db7
VZ
408// ----------------------------------------------------------------------------
409// MySizerDialog
410// ----------------------------------------------------------------------------
c801d85f 411
15f7c305 412MySizerDialog::MySizerDialog(wxWindow *parent, const wxString &title)
9230b621 413 : wxDialog(parent, wxID_ANY, wxString(title))
83edc0a5 414{
83edc0a5
RR
415 // Begin with first hierarchy: a notebook at the top and
416 // and OK button at the bottom.
417
418 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
56ac3e75 419
9230b621 420 wxNotebook *notebook = new wxNotebook( this, wxID_ANY );
adbf2d73 421 topsizer->Add( notebook, 1, wxGROW );
56ac3e75 422
9a83f860 423 wxButton *button = new wxButton( this, wxID_OK, wxT("OK") );
83edc0a5
RR
424 topsizer->Add( button, 0, wxALIGN_RIGHT | wxALL, 10 );
425
426 // First page: one big text ctrl
9a83f860
VZ
427 wxTextCtrl *multi = new wxTextCtrl( notebook, wxID_ANY, wxT("TextCtrl."), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
428 notebook->AddPage( multi, wxT("Page One") );
56ac3e75 429
83edc0a5 430 // Second page: a text ctrl and a button
9230b621 431 wxPanel *panel = new wxPanel( notebook, wxID_ANY );
9a83f860 432 notebook->AddPage( panel, wxT("Page Two") );
56ac3e75 433
f6bcfd97 434 wxSizer *panelsizer = new wxBoxSizer( wxVERTICAL );
56ac3e75 435
9a83f860 436 wxTextCtrl *text = new wxTextCtrl( panel, wxID_ANY, wxT("TextLine 1."), wxDefaultPosition, wxSize(250,wxDefaultCoord) );
83edc0a5 437 panelsizer->Add( text, 0, wxGROW|wxALL, 30 );
9a83f860 438 text = new wxTextCtrl( panel, wxID_ANY, wxT("TextLine 2."), wxDefaultPosition, wxSize(250,wxDefaultCoord) );
83edc0a5 439 panelsizer->Add( text, 0, wxGROW|wxALL, 30 );
9a83f860 440 wxButton *button2 = new wxButton( panel, wxID_ANY, wxT("Hallo") );
83edc0a5 441 panelsizer->Add( button2, 0, wxALIGN_RIGHT | wxLEFT|wxRIGHT|wxBOTTOM, 30 );
56ac3e75 442
83edc0a5 443 panel->SetSizer( panelsizer );
56ac3e75 444
83edc0a5 445 // Tell dialog to use sizer
92c01615 446 SetSizerAndFit( topsizer );
c62ac5b6
RR
447}
448
20b35a69
RD
449// ----------------------------------------------------------------------------
450// MyGridBagSizerFrame
451// ----------------------------------------------------------------------------
452
453// some simple macros to help make the sample code below more clear
9a83f860
VZ
454#define TEXTCTRL(text) new wxTextCtrl(p, wxID_ANY, wxT(text))
455#define MLTEXTCTRL(text) new wxTextCtrl(p, wxID_ANY, wxT(text), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE)
20b35a69
RD
456#define POS(r, c) wxGBPosition(r,c)
457#define SPAN(r, c) wxGBSpan(r,c)
458
9a83f860 459const wxChar gbsDescription[] =wxT("\
77c5e923
RD
460The wxGridBagSizer is similar to the wxFlexGridSizer except the items are explicitly positioned\n\
461in a virtual cell of the layout grid, and column or row spanning is allowed. For example, this\n\
462static text is positioned at (0,0) and it spans 7 columns.");
20b35a69
RD
463
464
465// Some IDs
466enum {
467 GBS_HIDE_BTN = 1212,
468 GBS_SHOW_BTN,
469 GBS_MOVE_BTN1,
470 GBS_MOVE_BTN2,
925e9792 471
004f4002 472 GBS_MAX
20b35a69
RD
473};
474
475
476BEGIN_EVENT_TABLE(MyGridBagSizerFrame, wxFrame)
477 EVT_BUTTON( GBS_HIDE_BTN, MyGridBagSizerFrame::OnHideBtn)
478 EVT_BUTTON( GBS_SHOW_BTN, MyGridBagSizerFrame::OnShowBtn)
479 EVT_BUTTON( GBS_MOVE_BTN1, MyGridBagSizerFrame::OnMoveBtn)
480 EVT_BUTTON( GBS_MOVE_BTN2, MyGridBagSizerFrame::OnMoveBtn)
481END_EVENT_TABLE()
482
483
15f7c305 484MyGridBagSizerFrame::MyGridBagSizerFrame(const wxString &title, int x, int y )
9230b621 485 : wxFrame( NULL, wxID_ANY, title, wxPoint(x, y) )
20b35a69 486{
9230b621 487 wxPanel* p = new wxPanel(this, wxID_ANY);
20b35a69
RD
488 m_panel = p;
489 m_gbs = new wxGridBagSizer();
490
925e9792 491
9230b621 492 m_gbs->Add( new wxStaticText(p, wxID_ANY, gbsDescription),
77c5e923
RD
493 POS(0,0), SPAN(1, 7),
494 wxALIGN_CENTER | wxALL, 5);
925e9792 495
20b35a69
RD
496 m_gbs->Add( TEXTCTRL("pos(1,0)"), POS(1,0) );
497 m_gbs->Add( TEXTCTRL("pos(1,1)"), POS(1,1) );
498 m_gbs->Add( TEXTCTRL("pos(2,0)"), POS(2,0) );
499 m_gbs->Add( TEXTCTRL("pos(2,1)"), POS(2,1) );
500 m_gbs->Add( MLTEXTCTRL("pos(3,2), span(1,2)\nthis row and col are growable"),
501 POS(3,2), SPAN(1,2), wxEXPAND );
502 m_gbs->Add( MLTEXTCTRL("pos(4,3)\nspan(3,1)"),
503 POS(4,3), SPAN(3,1), wxEXPAND );
504 m_gbs->Add( TEXTCTRL("pos(5,4)"), POS(5,4), wxDefaultSpan, wxEXPAND );
505 m_gbs->Add( TEXTCTRL("pos(6,5)"), POS(6,5), wxDefaultSpan, wxEXPAND );
506 m_gbs->Add( TEXTCTRL("pos(7,6)"), POS(7,6) );
925e9792 507
20b35a69
RD
508 //m_gbs->Add( TEXTCTRL("bad position"), POS(4,3) ); // Test for assert
509 //m_gbs->Add( TEXTCTRL("bad position"), POS(5,3) ); // Test for assert
510
511
9a83f860
VZ
512 m_moveBtn1 = new wxButton(p, GBS_MOVE_BTN1, wxT("Move this to (3,6)"));
513 m_moveBtn2 = new wxButton(p, GBS_MOVE_BTN2, wxT("Move this to (3,6)"));
20b35a69
RD
514 m_gbs->Add( m_moveBtn1, POS(10,2) );
515 m_gbs->Add( m_moveBtn2, POS(10,3) );
925e9792 516
9a83f860 517 m_hideBtn = new wxButton(p, GBS_HIDE_BTN, wxT("Hide this item -->"));
20b35a69
RD
518 m_gbs->Add(m_hideBtn, POS(12, 3));
519
9a83f860 520 m_hideTxt = new wxTextCtrl(p, wxID_ANY, wxT("pos(12,4), size(150, wxDefaultCoord)"),
6bbb8a8d 521 wxDefaultPosition, wxSize(150,wxDefaultCoord));
20b35a69 522 m_gbs->Add( m_hideTxt, POS(12,4) );
925e9792 523
9a83f860 524 m_showBtn = new wxButton(p, GBS_SHOW_BTN, wxT("<-- Show it again"));
20b35a69
RD
525 m_gbs->Add(m_showBtn, POS(12, 5));
526 m_showBtn->Disable();
527
528 m_gbs->Add(10,10, POS(14,0));
925e9792 529
20b35a69
RD
530 m_gbs->AddGrowableRow(3);
531 m_gbs->AddGrowableCol(2);
925e9792 532
20b35a69
RD
533 p->SetSizerAndFit(m_gbs);
534 SetClientSize(p->GetSize());
535}
536
925e9792 537
20b35a69
RD
538void MyGridBagSizerFrame::OnHideBtn(wxCommandEvent&)
539{
540 m_gbs->Hide(m_hideTxt);
541 m_hideBtn->Disable();
542 m_showBtn->Enable();
543 m_gbs->Layout();
544}
545
546void MyGridBagSizerFrame::OnShowBtn(wxCommandEvent&)
547{
548 m_gbs->Show(m_hideTxt);
549 m_hideBtn->Enable();
550 m_showBtn->Disable();
551 m_gbs->Layout();
552}
553
554
555void MyGridBagSizerFrame::OnMoveBtn(wxCommandEvent& event)
556{
557 wxButton* btn = (wxButton*)event.GetEventObject();
558 wxGBPosition curPos = m_gbs->GetItemPosition(btn);
559
560 // if it's already at the "other" spot then move it back
561 if (curPos == wxGBPosition(3,6))
562 {
563 m_gbs->SetItemPosition(btn, m_lastPos);
9a83f860 564 btn->SetLabel(wxT("Move this to (3,6)"));
20b35a69 565 }
925e9792 566 else
20b35a69 567 {
77c5e923
RD
568 if ( m_gbs->CheckForIntersection(wxGBPosition(3,6), wxGBSpan(1,1)) )
569 wxMessageBox(
9a83f860 570wxT("wxGridBagSizer will not allow items to be in the same cell as\n\
77c5e923 571another item, so this operation will fail. You will also get an assert\n\
9a83f860 572when compiled in debug mode."), wxT("Warning"), wxOK | wxICON_INFORMATION);
925e9792 573
77c5e923
RD
574 if ( m_gbs->SetItemPosition(btn, wxGBPosition(3,6)) )
575 {
576 m_lastPos = curPos;
9a83f860 577 btn->SetLabel(wxT("Move it back"));
77c5e923 578 }
20b35a69
RD
579 }
580 m_gbs->Layout();
581}
92c0c172
RR
582
583// ----------------------------------------------------------------------------
584// MySimpleSizerFrame
585// ----------------------------------------------------------------------------
586
587// Some IDs
588enum {
589 ID_SET_SMALL = 1300,
590 ID_SET_BIG
591};
592
593BEGIN_EVENT_TABLE(MySimpleSizerFrame, wxFrame)
594 EVT_MENU( ID_SET_SMALL, MySimpleSizerFrame::OnSetSmallSize)
595 EVT_MENU( ID_SET_BIG, MySimpleSizerFrame::OnSetBigSize)
596END_EVENT_TABLE()
597
15f7c305 598MySimpleSizerFrame::MySimpleSizerFrame(const wxString &title, int x, int y )
92c0c172
RR
599 : wxFrame( NULL, wxID_ANY, title, wxPoint(x, y) )
600{
601 wxMenu *menu = new wxMenu;
6bbb8a8d 602
9a83f860
VZ
603 menu->Append(ID_SET_SMALL, wxT("Make text control small\tF4"));
604 menu->Append(ID_SET_BIG, wxT("Make text control big\tF5"));
6bbb8a8d 605
92c0c172 606 wxMenuBar *menu_bar = new wxMenuBar;
9a83f860 607 menu_bar->Append(menu, wxT("&File"));
92c0c172
RR
608
609 SetMenuBar( menu_bar );
610
611 wxBoxSizer *main_sizer = new wxBoxSizer( wxHORIZONTAL );
612
6bbb8a8d 613 m_target = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize( 80, wxDefaultCoord ) );
92c0c172 614 main_sizer->Add( m_target, 1, wxALL, 5 );
6bbb8a8d
WS
615
616 main_sizer->Add( new wxStaticText( this, wxID_ANY, wxT("Set alternating sizes using F4 and F5") ), 0, wxALL, 5 );
617
92c0c172 618 SetSizer( main_sizer);
6bbb8a8d 619
92c0c172
RR
620 Layout();
621 GetSizer()->Fit( this );
622}
623
6bbb8a8d 624void MySimpleSizerFrame::OnSetSmallSize( wxCommandEvent& WXUNUSED(event))
92c0c172
RR
625{
626 GetSizer()->SetItemMinSize( m_target, 40, -1 );
627 Layout();
628 GetSizer()->Fit( this );
629}
630
6bbb8a8d 631void MySimpleSizerFrame::OnSetBigSize( wxCommandEvent& WXUNUSED(event))
92c0c172
RR
632{
633 GetSizer()->SetItemMinSize( m_target, 140, -1 );
634 Layout();
635 GetSizer()->Fit( this );
636}
637
638
a1a3bffc
RR
639// ----------------------------------------------------------------------------
640// MyNestedSizerFrame
641// ----------------------------------------------------------------------------
642
643
15f7c305 644MyNestedSizerFrame::MyNestedSizerFrame(const wxString &title, int x, int y )
a1a3bffc
RR
645 : wxFrame( NULL, wxID_ANY, title, wxPoint(x, y) )
646{
647 wxMenu *menu = new wxMenu;
648
9a83f860 649 menu->Append(wxID_ABOUT, wxT("Do nothing"));
a1a3bffc
RR
650
651 wxMenuBar *menu_bar = new wxMenuBar;
9a83f860 652 menu_bar->Append(menu, wxT("&File"));
a1a3bffc
RR
653
654 SetMenuBar( menu_bar );
655
656 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
657
658 main_sizer->Add( new wxStaticText( this, -1, wxT("Hello outside") ), 0, wxALIGN_CENTER );
659 main_sizer->Add( new wxStaticText( this, -1, wxT("Hello outside") ), 0, wxALIGN_CENTER );
660 main_sizer->Add( new wxStaticText( this, -1, wxT("Hello outside") ), 0, wxALIGN_CENTER );
661 main_sizer->Add( new wxStaticText( this, -1, wxT("Hello outside") ), 0, wxALIGN_CENTER );
662
41f02b9a 663 wxPanel *panel = new wxPanel( this, -1, wxDefaultPosition, wxDefaultSize,
a1a3bffc
RR
664 wxTAB_TRAVERSAL | wxSUNKEN_BORDER );
665 main_sizer->Add( panel, 0, wxALIGN_CENTER );
666 wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
667 panel->SetSizer( panel_sizer );
668 panel_sizer->Add( new wxStaticText( panel, -1, wxT("Hello inside") ) );
669 panel_sizer->Add( new wxStaticText( panel, -1, wxT("Hello inside") ) );
670 panel_sizer->Add( new wxStaticText( panel, -1, wxT("Hello inside") ) );
41f02b9a 671
a1a3bffc
RR
672 main_sizer->Add( new wxStaticText( this, -1, wxT("Hello outside") ), 0, wxALIGN_CENTER );
673
674 m_target = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize( 80, wxDefaultCoord ) );
675 main_sizer->Add( m_target, 1, wxALL|wxGROW, 5 );
676
92c01615 677 SetSizerAndFit( main_sizer);
a1a3bffc
RR
678}
679
15f7c305
RR
680
681// ----------------------------------------------------------------------------
682// MyWrapSizerFrame
683// ----------------------------------------------------------------------------
684
685
686MyWrapSizerFrame::MyWrapSizerFrame(const wxString &title, int x, int y )
e2512c23 687 : wxFrame( NULL, wxID_ANY, title, wxPoint(x, y), wxSize(200,-1) )
15f7c305
RR
688{
689 wxMenu *menu = new wxMenu;
690
691 menu->Append(wxID_ABOUT, "Do nothing");
692
693 wxMenuBar *menu_bar = new wxMenuBar;
694 menu_bar->Append(menu, "&File");
695
696 SetMenuBar( menu_bar );
697
698 wxBoxSizer *root = new wxBoxSizer( wxVERTICAL );
699
e2512c23
RR
700#if 0
701 wxSizer *row = new wxWrapSizer;
702 int i;
703 for (i = 0; i < 4; i++)
704 row->Add( new wxButton( this, -1, "Hello" ), 0, wxALL, 10 );
705 root->Add( row, 0, wxGROW );
41f02b9a 706
e2512c23
RR
707 row = new wxWrapSizer;
708 for (i = 0; i < 4; i++)
709 row->Add( new wxButton( this, -1, "Hello" ) );
710 root->Add( row, 0, wxGROW );
41f02b9a 711
e2512c23 712#else
41f02b9a 713 // A number of checkboxes inside a wrap sizer
15f7c305
RR
714 wxSizer *ps_mid = new wxStaticBoxSizer( wxVERTICAL, this, "Wrapping check-boxes" );
715 wxSizer *ps_mid_wrap = new wxWrapSizer(wxHORIZONTAL);
716 ps_mid->Add( ps_mid_wrap, 100, wxEXPAND );
717 for( int ix=0; ix<6; ix++ )
e2512c23 718 ps_mid_wrap->Add( new wxCheckBox(this,wxID_ANY,wxString::Format("Option %d",ix+1)), 0, wxALIGN_CENTRE|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
15f7c305
RR
719 root->Add( ps_mid, 0, wxEXPAND | wxALL, 5 );
720
721 // A shaped item inside a box sizer
722 wxSizer *ps_bottom = new wxStaticBoxSizer( wxVERTICAL, this, "With wxSHAPED item" );
723 wxSizer *ps_bottom_box = new wxBoxSizer(wxHORIZONTAL);
724 ps_bottom->Add( ps_bottom_box, 100, wxEXPAND );
725 ps_bottom_box->Add( new wxListBox(this,wxID_ANY,wxPoint(0,0),wxSize(70,70)), 0, wxEXPAND|wxSHAPED );
726 ps_bottom_box->Add( 10,10 );
727 ps_bottom_box->Add( new wxCheckBox(this,wxID_ANY,"A much longer option..."), 100, 0, 5 );
41f02b9a 728
15f7c305 729 root->Add( ps_bottom, 1, wxEXPAND | wxALL, 5 );
e2512c23 730#endif
41f02b9a 731
15f7c305 732 // Set sizer for window
92c01615 733 SetSizerAndFit( root );
15f7c305
RR
734}
735