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