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