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