]> git.saurik.com Git - wxWidgets.git/blob - samples/layout/layout.cpp
348054ffd999ce16c550dc66e73fecaad1da2c0a
[wxWidgets.git] / samples / layout / layout.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: layout.cpp
3 // Purpose: Layout sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ----------------------------------------------------------------------------
13 // headers
14 // ----------------------------------------------------------------------------
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #if !wxUSE_CONSTRAINTS
28 #error You must set wxUSE_CONSTRAINTS to 1 in setup.h!
29 #endif
30
31 #include "wx/sizer.h"
32 #include "wx/gbsizer.h"
33 #include "wx/statline.h"
34 #include "wx/notebook.h"
35
36 #include "layout.h"
37
38 // ----------------------------------------------------------------------------
39 // MyApp
40 // ----------------------------------------------------------------------------
41
42 IMPLEMENT_APP(MyApp)
43
44 MyApp::MyApp()
45 {
46 }
47
48 bool MyApp::OnInit()
49 {
50 // Create the main frame window
51 MyFrame *frame = new MyFrame;
52
53 frame->Show(TRUE);
54
55 return TRUE;
56 }
57
58 // ----------------------------------------------------------------------------
59 // MyFrame
60 // ----------------------------------------------------------------------------
61
62 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
63 EVT_MENU(LAYOUT_ABOUT, MyFrame::OnAbout)
64 EVT_MENU(LAYOUT_QUIT, MyFrame::OnQuit)
65
66 EVT_MENU(LAYOUT_TEST_CONSTRAINTS, MyFrame::TestConstraints)
67 EVT_MENU(LAYOUT_TEST_SIZER, MyFrame::TestFlexSizers)
68 EVT_MENU(LAYOUT_TEST_NB_SIZER, MyFrame::TestNotebookSizers)
69 EVT_MENU(LAYOUT_TEST_GB_SIZER, MyFrame::TestGridBagSizer)
70 END_EVENT_TABLE()
71
72 // Define my frame constructor
73 MyFrame::MyFrame()
74 : wxFrame(NULL, -1, _T("wxWindows 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_CONSTRAINTS, _T("Test &constraints"));
82 file_menu->Append(LAYOUT_TEST_SIZER, _T("Test wx&FlexSizer"));
83 file_menu->Append(LAYOUT_TEST_NB_SIZER, _T("&Test notebook sizers"));
84 file_menu->Append(LAYOUT_TEST_GB_SIZER, _T("Test &gridbag sizer"));
85
86 file_menu->AppendSeparator();
87 file_menu->Append(LAYOUT_QUIT, _T("E&xit"), _T("Quit program"));
88
89 wxMenu *help_menu = new wxMenu;
90 help_menu->Append(LAYOUT_ABOUT, _T("&About"), _T("About layout demo"));
91
92 wxMenuBar *menu_bar = new wxMenuBar;
93
94 menu_bar->Append(file_menu, _T("&File"));
95 menu_bar->Append(help_menu, _T("&Help"));
96
97 // Associate the menu bar with the frame
98 SetMenuBar(menu_bar);
99
100 CreateStatusBar(2);
101 SetStatusText(_T("wxWindows layout demo"));
102
103
104 // we want to get a dialog that is stretchable because it
105 // has a text ctrl in the middle. at the bottom, we have
106 // two buttons which.
107
108 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
109
110 // 1) top: create wxStaticText with minimum size equal to its default size
111 topsizer->Add(
112 new wxStaticText( this, -1, _T("An explanation (wxALIGN_RIGHT).") ),
113 0, // make vertically unstretchable
114 wxALIGN_RIGHT | // right align text
115 wxTOP | wxLEFT | wxRIGHT, // make border all around except wxBOTTOM
116 5 ); // set border width to 5
117
118 // 2) top: create wxTextCtrl with minimum size (100x60)
119 topsizer->Add(
120 new wxTextCtrl( this, -1, _T("My text (wxEXPAND)."), wxDefaultPosition, wxSize(100,60), wxTE_MULTILINE),
121 1, // make vertically stretchable
122 wxEXPAND | // make horizontally stretchable
123 wxALL, // and make border all around
124 5 ); // set border width to 5
125
126 // 2.5) Gratuitous test of wxStaticBoxSizers
127 wxBoxSizer *statsizer = new wxStaticBoxSizer(
128 new wxStaticBox(this, -1, _T("A wxStaticBoxSizer")),
129 wxVERTICAL );
130 statsizer->Add(
131 new wxStaticText(this, -1, _T("And some TEXT inside it")),
132 0,
133 wxCENTER |
134 wxALL,
135 30);
136 topsizer->Add(statsizer, 1, wxEXPAND | wxALL, 10);
137
138 // 2.7) And a test of wxGridSizer
139 wxGridSizer *gridsizer = new wxGridSizer(2, 5, 5);
140 gridsizer->Add(new wxStaticText(this, -1, _T("Label")), 0,
141 wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
142 gridsizer->Add(new wxTextCtrl(this, -1, _T("Grid sizer demo")), 1,
143 wxGROW | wxALIGN_CENTER_VERTICAL);
144 gridsizer->Add(new wxStaticText(this, -1, _T("Another label")), 0,
145 wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
146 gridsizer->Add(new wxTextCtrl(this, -1, _T("More text")), 1,
147 wxGROW | wxALIGN_CENTER_VERTICAL);
148 gridsizer->Add(new wxStaticText(this, -1, _T("Final label")), 0,
149 wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
150 gridsizer->Add(new wxTextCtrl(this, -1, _T("And yet more text")), 1,
151 wxGROW | wxALIGN_CENTER_VERTICAL);
152 topsizer->Add(gridsizer, 1, wxGROW | wxALL, 10);
153
154
155 // 3) middle: create wxStaticLine with minimum size (3x3)
156 topsizer->Add(
157 new wxStaticLine( this, -1, wxDefaultPosition, wxSize(3,3), wxHORIZONTAL),
158 0, // make vertically unstretchable
159 wxEXPAND | // make horizontally stretchable
160 wxALL, // and make border all around
161 5 ); // set border width to 5
162
163
164 // 4) bottom: create two centred wxButtons
165 wxBoxSizer *button_box = new wxBoxSizer( wxHORIZONTAL );
166 button_box->Add(
167 new wxButton( this, -1, _T("Two buttons in a box") ),
168 0, // make horizontally unstretchable
169 wxALL, // make border all around
170 7 ); // set border width to 7
171 button_box->Add(
172 new wxButton( this, -1, _T("(wxCENTER)") ),
173 0, // make horizontally unstretchable
174 wxALL, // make border all around
175 7 ); // set border width to 7
176
177 topsizer->Add(
178 button_box,
179 0, // make vertically unstretchable
180 wxCENTER ); // no border and centre horizontally
181
182 // don't allow frame to get smaller than what the sizers tell it and also set
183 // the initial size as calculated by the sizers
184 topsizer->SetSizeHints( this );
185
186 SetSizer( topsizer );
187 }
188
189 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
190 {
191 Close(TRUE);
192 }
193
194 void MyFrame::TestConstraints(wxCommandEvent& WXUNUSED(event) )
195 {
196 MyConstraintsFrame *
197 newFrame = new MyConstraintsFrame(_T("Constraints Test Frame"), 100, 100);
198 newFrame->Show(TRUE);
199 }
200
201 void MyFrame::TestFlexSizers(wxCommandEvent& WXUNUSED(event) )
202 {
203 MyFlexSizerFrame *newFrame = new MyFlexSizerFrame(_T("Flex Sizer Test Frame"), 50, 50);
204 newFrame->Show(TRUE);
205 }
206
207 void MyFrame::TestNotebookSizers(wxCommandEvent& WXUNUSED(event) )
208 {
209 MySizerDialog dialog( this, _T("Notebook Sizer Test Dialog") );
210
211 dialog.ShowModal();
212 }
213
214
215 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
216 {
217 (void)wxMessageBox(_T("wxWindows GUI library layout demo\n"),
218 _T("About Layout Demo"), wxOK|wxICON_INFORMATION);
219 }
220
221 void MyFrame::TestGridBagSizer(wxCommandEvent& WXUNUSED(event) )
222 {
223 MyGridBagSizerFrame *newFrame = new
224 MyGridBagSizerFrame(_T("wxGridBagSizer Test Frame"), 50, 50);
225 newFrame->Show(TRUE);
226 }
227
228
229 // ----------------------------------------------------------------------------
230 // MyConstraintsFrame
231 // ----------------------------------------------------------------------------
232
233 MyConstraintsFrame::MyConstraintsFrame(const wxChar *title, int x, int y)
234 : wxFrame(NULL, -1, title, wxPoint(x, y) )
235 {
236 // Make a panel
237 wxPanel *panel = new wxPanel(this);
238
239 // Create some panel items
240 wxButton *btn1 = new wxButton(panel, -1, _T("A button (1)")) ;
241
242 wxLayoutConstraints *b1 = new wxLayoutConstraints;
243 b1->centreX.SameAs (panel, wxCentreX);
244 b1->top.SameAs (panel, wxTop, 5);
245 b1->width.PercentOf (panel, wxWidth, 80);
246 b1->height.AsIs ();
247 btn1->SetConstraints(b1);
248
249 wxListBox *list = new wxListBox(panel, -1,
250 wxPoint(-1, -1), wxSize(200, 100));
251 list->Append(_T("Apple"));
252 list->Append(_T("Pear"));
253 list->Append(_T("Orange"));
254 list->Append(_T("Banana"));
255 list->Append(_T("Fruit"));
256
257 wxLayoutConstraints *b2 = new wxLayoutConstraints;
258 b2->top.Below (btn1, 5);
259 b2->left.SameAs (panel, wxLeft, 5);
260 b2->width.PercentOf (panel, wxWidth, 40);
261 b2->bottom.SameAs (panel, wxBottom, 5);
262 list->SetConstraints(b2);
263
264 wxTextCtrl *mtext = new wxTextCtrl(panel, -1,
265 _T("This frame is laid out using\n"
266 "constraints, but the preferred\n"
267 "layout mechanism now are sizers."),
268 wxDefaultPosition,
269 wxDefaultSize,
270 wxTE_MULTILINE);
271
272 wxLayoutConstraints *b3 = new wxLayoutConstraints;
273 b3->top.Below (btn1, 5);
274 b3->left.RightOf (list, 5);
275 b3->right.SameAs (panel, wxRight, 5);
276 b3->bottom.SameAs (panel, wxBottom, 5);
277 mtext->SetConstraints(b3);
278
279 wxTextCtrl *canvas = new wxTextCtrl(this, -1, _T("yet another window"));
280
281 // Make a text window
282 wxTextCtrl *text_window = new wxTextCtrl(this, -1, _T(""),
283 wxDefaultPosition,
284 wxDefaultSize,
285 wxTE_MULTILINE);
286
287 // Set constraints for panel subwindow
288 wxLayoutConstraints *c1 = new wxLayoutConstraints;
289
290 c1->left.SameAs (this, wxLeft);
291 c1->top.SameAs (this, wxTop);
292 c1->right.PercentOf (this, wxWidth, 50);
293 c1->height.PercentOf (this, wxHeight, 50);
294
295 panel->SetConstraints(c1);
296
297 // Set constraints for canvas subwindow
298 wxLayoutConstraints *c2 = new wxLayoutConstraints;
299
300 c2->left.SameAs (panel, wxRight);
301 c2->top.SameAs (this, wxTop);
302 c2->right.SameAs (this, wxRight);
303 c2->height.PercentOf (this, wxHeight, 50);
304
305 canvas->SetConstraints(c2);
306
307 // Set constraints for text subwindow
308 wxLayoutConstraints *c3 = new wxLayoutConstraints;
309 c3->left.SameAs (this, wxLeft);
310 c3->top.Below (panel);
311 c3->right.SameAs (this, wxRight);
312 c3->bottom.SameAs (this, wxBottom);
313
314 text_window->SetConstraints(c3);
315
316 SetAutoLayout(TRUE);
317 }
318
319 // ----------------------------------------------------------------------------
320 // MyFlexSizerFrame
321 // ----------------------------------------------------------------------------
322
323 void MyFlexSizerFrame::InitFlexSizer(wxFlexGridSizer *sizer)
324 {
325 for ( int i = 0; i < 3; i++ )
326 {
327 for ( int j = 0; j < 3; j++ )
328 {
329 sizer->Add(new wxStaticText
330 (
331 this,
332 -1,
333 wxString::Format(_T("(%d, %d)"), i + 1, j + 1),
334 wxDefaultPosition,
335 wxDefaultSize,
336 wxALIGN_CENTER
337 ),
338 0, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 3);
339 }
340 }
341 }
342
343 MyFlexSizerFrame::MyFlexSizerFrame(const wxChar *title, int x, int y )
344 : wxFrame(NULL, -1, title, wxPoint(x, y) )
345 {
346 wxFlexGridSizer *sizerFlex;
347
348 // consttuct the first column
349 wxSizer *sizerCol1 = new wxBoxSizer(wxVERTICAL);
350 sizerCol1->Add(new wxStaticText(this, -1, _T("Ungrowable:")), 0, wxCENTER | wxTOP, 20);
351 sizerFlex = new wxFlexGridSizer(3, 3);
352 InitFlexSizer(sizerFlex);
353 sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
354
355 sizerCol1->Add(new wxStaticText(this, -1, _T("Growable middle column:")), 0, wxCENTER | wxTOP, 20);
356 sizerFlex = new wxFlexGridSizer(3, 3);
357 InitFlexSizer(sizerFlex);
358 sizerFlex->AddGrowableCol(1);
359 sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
360
361 sizerCol1->Add(new wxStaticText(this, -1, _T("Growable middle row:")), 0, wxCENTER | wxTOP, 20);
362 sizerFlex = new wxFlexGridSizer(3, 3);
363 InitFlexSizer(sizerFlex);
364 sizerFlex->AddGrowableRow(1);
365 sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
366
367 sizerCol1->Add(new wxStaticText(this, -1, _T("All growable columns:")), 0, wxCENTER | wxTOP, 20);
368 sizerFlex = new wxFlexGridSizer(3, 3);
369 InitFlexSizer(sizerFlex);
370 sizerFlex->AddGrowableCol(0, 1);
371 sizerFlex->AddGrowableCol(1, 2);
372 sizerFlex->AddGrowableCol(2, 3);
373 sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
374
375 // the second one
376 wxSizer *sizerCol2 = new wxBoxSizer(wxVERTICAL);
377 sizerCol2->Add(new wxStaticText(this, -1, _T("Growable middle row and column:")), 0, wxCENTER | wxTOP, 20);
378 sizerFlex = new wxFlexGridSizer(3, 3);
379 InitFlexSizer(sizerFlex);
380 sizerFlex->AddGrowableCol(1);
381 sizerFlex->AddGrowableRow(1);
382 sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
383
384 sizerCol2->Add(new wxStaticText(this, -1, _T("Same with horz flex direction")), 0, wxCENTER | wxTOP, 20);
385 sizerFlex = new wxFlexGridSizer(3, 3);
386 InitFlexSizer(sizerFlex);
387 sizerFlex->AddGrowableCol(1);
388 sizerFlex->AddGrowableRow(1);
389 sizerFlex->SetFlexibleDirection(wxHORIZONTAL);
390 sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
391
392 sizerCol2->Add(new wxStaticText(this, -1, _T("Same with grow mode == \"none\"")), 0, wxCENTER | wxTOP, 20);
393 sizerFlex = new wxFlexGridSizer(3, 3);
394 InitFlexSizer(sizerFlex);
395 sizerFlex->AddGrowableCol(1);
396 sizerFlex->AddGrowableRow(1);
397 sizerFlex->SetFlexibleDirection(wxHORIZONTAL);
398 sizerFlex->SetNonFlexibleGrowMode(wxFLEX_GROWMODE_NONE);
399 sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
400
401 sizerCol2->Add(new wxStaticText(this, -1, _T("Same with grow mode == \"all\"")), 0, wxCENTER | wxTOP, 20);
402 sizerFlex = new wxFlexGridSizer(3, 3);
403 InitFlexSizer(sizerFlex);
404 sizerFlex->AddGrowableCol(1);
405 sizerFlex->AddGrowableRow(1);
406 sizerFlex->SetFlexibleDirection(wxHORIZONTAL);
407 sizerFlex->SetNonFlexibleGrowMode(wxFLEX_GROWMODE_ALL);
408 sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
409
410 // add both columns to grid sizer
411 wxGridSizer *sizerTop = new wxGridSizer(2, 0, 20);
412 sizerTop->Add(sizerCol1, 1, wxEXPAND);
413 sizerTop->Add(sizerCol2, 1, wxEXPAND);
414
415 SetSizer(sizerTop);
416 sizerTop->SetSizeHints(this);
417 }
418
419 // ----------------------------------------------------------------------------
420 // MySizerDialog
421 // ----------------------------------------------------------------------------
422
423 MySizerDialog::MySizerDialog(wxWindow *parent, const wxChar *title)
424 : wxDialog(parent, -1, wxString(title))
425 {
426 // Begin with first hierarchy: a notebook at the top and
427 // and OK button at the bottom.
428
429 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
430
431 wxNotebook *notebook = new wxNotebook( this, -1 );
432 wxNotebookSizer *nbs = new wxNotebookSizer( notebook );
433 topsizer->Add( nbs, 1, wxGROW );
434
435 wxButton *button = new wxButton( this, wxID_OK, _T("OK") );
436 topsizer->Add( button, 0, wxALIGN_RIGHT | wxALL, 10 );
437
438 // First page: one big text ctrl
439 wxTextCtrl *multi = new wxTextCtrl( notebook, -1, _T("TextCtrl."), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
440 notebook->AddPage( multi, _T("Page One") );
441
442 // Second page: a text ctrl and a button
443 wxPanel *panel = new wxPanel( notebook, -1 );
444 notebook->AddPage( panel, _T("Page Two") );
445
446 wxSizer *panelsizer = new wxBoxSizer( wxVERTICAL );
447
448 wxTextCtrl *text = new wxTextCtrl( panel, -1, _T("TextLine 1."), wxDefaultPosition, wxSize(250,-1) );
449 panelsizer->Add( text, 0, wxGROW|wxALL, 30 );
450 text = new wxTextCtrl( panel, -1, _T("TextLine 2."), wxDefaultPosition, wxSize(250,-1) );
451 panelsizer->Add( text, 0, wxGROW|wxALL, 30 );
452 wxButton *button2 = new wxButton( panel, -1, _T("Hallo") );
453 panelsizer->Add( button2, 0, wxALIGN_RIGHT | wxLEFT|wxRIGHT|wxBOTTOM, 30 );
454
455 panel->SetAutoLayout( TRUE );
456 panel->SetSizer( panelsizer );
457
458 // Tell dialog to use sizer
459 SetSizer( topsizer );
460 topsizer->SetSizeHints( this );
461 }
462
463 // ----------------------------------------------------------------------------
464 // MyGridBagSizerFrame
465 // ----------------------------------------------------------------------------
466
467 // some simple macros to help make the sample code below more clear
468 #define TEXTCTRL(text) new wxTextCtrl(p, -1, wxT(text))
469 #define MLTEXTCTRL(text) new wxTextCtrl(p, -1, wxT(text), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE)
470 #define POS(r, c) wxGBPosition(r,c)
471 #define SPAN(r, c) wxGBSpan(r,c)
472
473 wxChar* gbsDescription =wxT("\
474 The wxGridBagSizer is similar to the wxFlexGridSizer except the \
475 items are explicitly posisioned in a virtual 'cell' in the \n\
476 layout grid, and column or row spanning is allowed. For example, \
477 this static text is positioned at (0,0) and it spans 9 columns.");
478
479
480 // Some IDs
481 enum {
482 GBS_HIDE_BTN = 1212,
483 GBS_SHOW_BTN,
484 GBS_MOVE_BTN1,
485 GBS_MOVE_BTN2,
486
487 GBS_MAX,
488 };
489
490
491 BEGIN_EVENT_TABLE(MyGridBagSizerFrame, wxFrame)
492 EVT_BUTTON( GBS_HIDE_BTN, MyGridBagSizerFrame::OnHideBtn)
493 EVT_BUTTON( GBS_SHOW_BTN, MyGridBagSizerFrame::OnShowBtn)
494 EVT_BUTTON( GBS_MOVE_BTN1, MyGridBagSizerFrame::OnMoveBtn)
495 EVT_BUTTON( GBS_MOVE_BTN2, MyGridBagSizerFrame::OnMoveBtn)
496 END_EVENT_TABLE()
497
498
499 MyGridBagSizerFrame::MyGridBagSizerFrame(const wxChar *title, int x, int y )
500 : wxFrame( NULL, -1, title, wxPoint(x, y) )
501 {
502 wxPanel* p = new wxPanel(this, -1);
503 m_panel = p;
504 m_gbs = new wxGridBagSizer();
505
506
507 m_gbs->Add( new wxStaticText(p, -1, gbsDescription),
508 POS(0,0), SPAN(1, 9),
509 wxALIGN_CENTER | wxALL, 5);
510
511 m_gbs->Add( TEXTCTRL("pos(1,0)"), POS(1,0) );
512 m_gbs->Add( TEXTCTRL("pos(1,1)"), POS(1,1) );
513 m_gbs->Add( TEXTCTRL("pos(2,0)"), POS(2,0) );
514 m_gbs->Add( TEXTCTRL("pos(2,1)"), POS(2,1) );
515 m_gbs->Add( MLTEXTCTRL("pos(3,2), span(1,2)\nthis row and col are growable"),
516 POS(3,2), SPAN(1,2), wxEXPAND );
517 m_gbs->Add( MLTEXTCTRL("pos(4,3)\nspan(3,1)"),
518 POS(4,3), SPAN(3,1), wxEXPAND );
519 m_gbs->Add( TEXTCTRL("pos(5,4)"), POS(5,4), wxDefaultSpan, wxEXPAND );
520 m_gbs->Add( TEXTCTRL("pos(6,5)"), POS(6,5), wxDefaultSpan, wxEXPAND );
521 m_gbs->Add( TEXTCTRL("pos(7,6)"), POS(7,6) );
522 m_gbs->Add( TEXTCTRL("pos(8,7)"), POS(8,7) );
523 m_gbs->Add( TEXTCTRL("pos(9,8)"), POS(9,8) );
524
525 //m_gbs->Add( TEXTCTRL("bad position"), POS(4,3) ); // Test for assert
526 //m_gbs->Add( TEXTCTRL("bad position"), POS(5,3) ); // Test for assert
527
528
529 m_moveBtn1 = new wxButton(p, GBS_MOVE_BTN1, wxT("Move this to (3,6)"));
530 m_moveBtn2 = new wxButton(p, GBS_MOVE_BTN2, wxT("Move this to (3,6)"));
531 m_gbs->Add( m_moveBtn1, POS(10,2) );
532 m_gbs->Add( m_moveBtn2, POS(10,3) );
533
534 m_hideBtn = new wxButton(p, GBS_HIDE_BTN, wxT("Hide this item -->"));
535 m_gbs->Add(m_hideBtn, POS(12, 3));
536
537 m_hideTxt = new wxTextCtrl(p, -1, wxT("pos(12,4), size(250, -1)"),
538 wxDefaultPosition, wxSize(250,-1));
539 m_gbs->Add( m_hideTxt, POS(12,4) );
540
541 m_showBtn = new wxButton(p, GBS_SHOW_BTN, wxT("<-- Show it again"));
542 m_gbs->Add(m_showBtn, POS(12, 5));
543 m_showBtn->Disable();
544
545 m_gbs->Add(10,10, POS(14,0));
546
547 m_gbs->AddGrowableRow(3);
548 m_gbs->AddGrowableCol(2);
549
550 p->SetSizerAndFit(m_gbs);
551 SetClientSize(p->GetSize());
552 }
553
554
555 void MyGridBagSizerFrame::OnHideBtn(wxCommandEvent&)
556 {
557 m_gbs->Hide(m_hideTxt);
558 m_hideBtn->Disable();
559 m_showBtn->Enable();
560 m_gbs->Layout();
561 }
562
563 void MyGridBagSizerFrame::OnShowBtn(wxCommandEvent&)
564 {
565 m_gbs->Show(m_hideTxt);
566 m_hideBtn->Enable();
567 m_showBtn->Disable();
568 m_gbs->Layout();
569 }
570
571
572 void MyGridBagSizerFrame::OnMoveBtn(wxCommandEvent& event)
573 {
574 wxButton* btn = (wxButton*)event.GetEventObject();
575 wxGBPosition curPos = m_gbs->GetItemPosition(btn);
576
577 // if it's already at the "other" spot then move it back
578 if (curPos == wxGBPosition(3,6))
579 {
580 m_gbs->SetItemPosition(btn, m_lastPos);
581 btn->SetLabel(wxT("Move this to (3,6)"));
582 }
583 else if ( m_gbs->SetItemPosition(btn, wxGBPosition(3,6)) )
584 {
585 m_lastPos = curPos;
586 btn->SetLabel(wxT("Move it back"));
587 }
588 m_gbs->Layout();
589 }