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