]> git.saurik.com Git - wxWidgets.git/blame - samples/layout/layout.cpp
Correct 'markup' for GetResourceHandle after recent change in wx/gdiobj.h.
[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)
92c0c172 64 EVT_MENU(LAYOUT_TEST_SET_MINIMAL, MyFrame::TestSetMinimal)
9e023db7 65END_EVENT_TABLE()
c801d85f 66
9e023db7
VZ
67// Define my frame constructor
68MyFrame::MyFrame()
be5a51fb 69 : wxFrame(NULL, wxID_ANY, _T("wxWidgets Layout Demo"),
9e023db7
VZ
70 wxDefaultPosition, wxDefaultSize,
71 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
72{
c801d85f
KB
73 // Make a menubar
74 wxMenu *file_menu = new wxMenu;
75
7235f8e1
VZ
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"));
92c0c172 80 file_menu->Append(LAYOUT_TEST_SET_MINIMAL, _T("Test Set&ItemMinSize...\tF5"));
c801d85f
KB
81
82 file_menu->AppendSeparator();
9e023db7 83 file_menu->Append(LAYOUT_QUIT, _T("E&xit"), _T("Quit program"));
c801d85f
KB
84
85 wxMenu *help_menu = new wxMenu;
7235f8e1 86 help_menu->Append(LAYOUT_ABOUT, _T("&About"), _T("About layout demo..."));
c801d85f 87
9e023db7 88 wxMenuBar *menu_bar = new wxMenuBar;
c801d85f 89
42ed7532
MB
90 menu_bar->Append(file_menu, _T("&File"));
91 menu_bar->Append(help_menu, _T("&Help"));
c801d85f
KB
92
93 // Associate the menu bar with the frame
9e023db7
VZ
94 SetMenuBar(menu_bar);
95
8520f137 96#if wxUSE_STATUSBAR
9e023db7 97 CreateStatusBar(2);
be5a51fb 98 SetStatusText(_T("wxWidgets layout demo"));
8520f137 99#endif // wxUSE_STATUSBAR
9e023db7 100
6bbb8a8d
WS
101 wxPanel* p = new wxPanel(this, wxID_ANY);
102
9e023db7
VZ
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(
bdbb07ed 111 new wxStaticText( p, wxID_ANY, _T("An explanation (wxALIGN_RIGHT).") ),
e5544e77 112 wxSizerFlags().Align(wxALIGN_RIGHT).Border(wxALL & ~wxBOTTOM, 5));
9e023db7
VZ
113
114 // 2) top: create wxTextCtrl with minimum size (100x60)
115 topsizer->Add(
bdbb07ed 116 new wxTextCtrl( p, wxID_ANY, _T("My text (wxEXPAND)."), wxDefaultPosition, wxSize(100,60), wxTE_MULTILINE),
e5544e77 117 wxSizerFlags(1).Expand().Border(wxALL, 5));
9e023db7
VZ
118
119 // 2.5) Gratuitous test of wxStaticBoxSizers
120 wxBoxSizer *statsizer = new wxStaticBoxSizer(
bdbb07ed 121 new wxStaticBox(p, wxID_ANY, _T("A wxStaticBoxSizer")), wxVERTICAL );
9e023db7 122 statsizer->Add(
bdbb07ed 123 new wxStaticText(p, wxID_ANY, _T("And some TEXT inside it")),
e5544e77
VZ
124 wxSizerFlags().Center().Border(wxALL, 30));
125 topsizer->Add(
126 statsizer,
127 wxSizerFlags(1).Expand().Border(wxALL, 10));
9e023db7
VZ
128
129 // 2.7) And a test of wxGridSizer
130 wxGridSizer *gridsizer = new wxGridSizer(2, 5, 5);
bdbb07ed 131 gridsizer->Add(new wxStaticText(p, wxID_ANY, _T("Label")),
e5544e77 132 wxSizerFlags().Align(wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL));
bdbb07ed 133 gridsizer->Add(new wxTextCtrl(p, wxID_ANY, _T("Grid sizer demo")),
e5544e77 134 wxSizerFlags(1).Align(wxGROW | wxALIGN_CENTER_VERTICAL));
bdbb07ed 135 gridsizer->Add(new wxStaticText(p, wxID_ANY, _T("Another label")),
e5544e77 136 wxSizerFlags().Align(wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL));
bdbb07ed 137 gridsizer->Add(new wxTextCtrl(p, wxID_ANY, _T("More text")),
e5544e77 138 wxSizerFlags(1).Align(wxGROW | wxALIGN_CENTER_VERTICAL));
bdbb07ed 139 gridsizer->Add(new wxStaticText(p, wxID_ANY, _T("Final label")),
e5544e77 140 wxSizerFlags().Align(wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL));
bdbb07ed 141 gridsizer->Add(new wxTextCtrl(p, wxID_ANY, _T("And yet more text")),
e5544e77
VZ
142 wxSizerFlags().Align(wxGROW | wxALIGN_CENTER_VERTICAL));
143 topsizer->Add(
144 gridsizer,
145 wxSizerFlags().Proportion(1).Expand().Border(wxALL, 10));
9e023db7
VZ
146
147
4aa0bd9b 148#if wxUSE_STATLINE
9e023db7
VZ
149 // 3) middle: create wxStaticLine with minimum size (3x3)
150 topsizer->Add(
bdbb07ed 151 new wxStaticLine( p, wxID_ANY, wxDefaultPosition, wxSize(3,3), wxHORIZONTAL),
e5544e77 152 wxSizerFlags().Expand());
4aa0bd9b 153#endif // wxUSE_STATLINE
9e023db7
VZ
154
155
156 // 4) bottom: create two centred wxButtons
157 wxBoxSizer *button_box = new wxBoxSizer( wxHORIZONTAL );
158 button_box->Add(
bdbb07ed 159 new wxButton( p, wxID_ANY, _T("Two buttons in a box") ),
e5544e77 160 wxSizerFlags().Border(wxALL, 7));
9e023db7 161 button_box->Add(
bdbb07ed 162 new wxButton( p, wxID_ANY, _T("(wxCENTER)") ),
e5544e77 163 wxSizerFlags().Border(wxALL, 7));
9e023db7 164
e5544e77 165 topsizer->Add(button_box, wxSizerFlags().Center());
9e023db7 166
bdbb07ed 167 p->SetSizer( topsizer );
6bbb8a8d 168
9e023db7
VZ
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 );
9e023db7
VZ
172}
173
7235f8e1 174void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
9e023db7 175{
9230b621 176 Close(true);
9e023db7
VZ
177}
178
7235f8e1
VZ
179void MyFrame::TestProportions(wxCommandEvent& WXUNUSED(event))
180{
181 (new MyProportionsFrame(this))->Show();
182}
183
9e023db7
VZ
184void MyFrame::TestFlexSizers(wxCommandEvent& WXUNUSED(event) )
185{
186 MyFlexSizerFrame *newFrame = new MyFlexSizerFrame(_T("Flex Sizer Test Frame"), 50, 50);
9230b621 187 newFrame->Show(true);
9e023db7
VZ
188}
189
190void MyFrame::TestNotebookSizers(wxCommandEvent& WXUNUSED(event) )
191{
192 MySizerDialog dialog( this, _T("Notebook Sizer Test Dialog") );
193
194 dialog.ShowModal();
195}
196
92c0c172
RR
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
c801d85f 203
9e023db7
VZ
204void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
205{
be5a51fb 206 (void)wxMessageBox(_T("wxWidgets GUI library layout demo\n"),
9e023db7
VZ
207 _T("About Layout Demo"), wxOK|wxICON_INFORMATION);
208}
209
20b35a69
RD
210void MyFrame::TestGridBagSizer(wxCommandEvent& WXUNUSED(event) )
211{
212 MyGridBagSizerFrame *newFrame = new
213 MyGridBagSizerFrame(_T("wxGridBagSizer Test Frame"), 50, 50);
9230b621 214 newFrame->Show(true);
20b35a69
RD
215}
216
7235f8e1
VZ
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}
20b35a69 273
9e023db7
VZ
274// ----------------------------------------------------------------------------
275// MyFlexSizerFrame
276// ----------------------------------------------------------------------------
c62ac5b6 277
bdbb07ed 278void MyFlexSizerFrame::InitFlexSizer(wxFlexGridSizer *sizer, wxWindow* parent)
c801d85f 279{
9e023db7
VZ
280 for ( int i = 0; i < 3; i++ )
281 {
282 for ( int j = 0; j < 3; j++ )
283 {
284 sizer->Add(new wxStaticText
285 (
bdbb07ed 286 parent,
9230b621 287 wxID_ANY,
9e023db7
VZ
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 }
c801d85f
KB
296}
297
9e023db7 298MyFlexSizerFrame::MyFlexSizerFrame(const wxChar *title, int x, int y )
9230b621 299 : wxFrame(NULL, wxID_ANY, title, wxPoint(x, y) )
c801d85f 300{
9e023db7 301 wxFlexGridSizer *sizerFlex;
6bbb8a8d 302 wxPanel* p = new wxPanel(this, wxID_ANY);
9e023db7
VZ
303
304 // consttuct the first column
305 wxSizer *sizerCol1 = new wxBoxSizer(wxVERTICAL);
bdbb07ed 306 sizerCol1->Add(new wxStaticText(p, wxID_ANY, _T("Ungrowable:")), 0, wxCENTER | wxTOP, 20);
9e023db7 307 sizerFlex = new wxFlexGridSizer(3, 3);
bdbb07ed 308 InitFlexSizer(sizerFlex, p);
9e023db7
VZ
309 sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
310
bdbb07ed 311 sizerCol1->Add(new wxStaticText(p, wxID_ANY, _T("Growable middle column:")), 0, wxCENTER | wxTOP, 20);
9e023db7 312 sizerFlex = new wxFlexGridSizer(3, 3);
bdbb07ed 313 InitFlexSizer(sizerFlex, p);
9e023db7
VZ
314 sizerFlex->AddGrowableCol(1);
315 sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
316
bdbb07ed 317 sizerCol1->Add(new wxStaticText(p, wxID_ANY, _T("Growable middle row:")), 0, wxCENTER | wxTOP, 20);
9e023db7 318 sizerFlex = new wxFlexGridSizer(3, 3);
bdbb07ed 319 InitFlexSizer(sizerFlex, p);
9e023db7
VZ
320 sizerFlex->AddGrowableRow(1);
321 sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
322
bdbb07ed 323 sizerCol1->Add(new wxStaticText(p, wxID_ANY, _T("All growable columns:")), 0, wxCENTER | wxTOP, 20);
9e023db7 324 sizerFlex = new wxFlexGridSizer(3, 3);
bdbb07ed 325 InitFlexSizer(sizerFlex, p);
9e023db7
VZ
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);
bdbb07ed 333 sizerCol2->Add(new wxStaticText(p, wxID_ANY, _T("Growable middle row and column:")), 0, wxCENTER | wxTOP, 20);
9e023db7 334 sizerFlex = new wxFlexGridSizer(3, 3);
bdbb07ed 335 InitFlexSizer(sizerFlex, p);
9e023db7
VZ
336 sizerFlex->AddGrowableCol(1);
337 sizerFlex->AddGrowableRow(1);
338 sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
339
bdbb07ed 340 sizerCol2->Add(new wxStaticText(p, wxID_ANY, _T("Same with horz flex direction")), 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 sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
347
bdbb07ed 348 sizerCol2->Add(new wxStaticText(p, wxID_ANY, _T("Same with grow mode == \"none\"")), 0, wxCENTER | wxTOP, 20);
9e023db7 349 sizerFlex = new wxFlexGridSizer(3, 3);
bdbb07ed 350 InitFlexSizer(sizerFlex, p);
9e023db7
VZ
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
bdbb07ed 357 sizerCol2->Add(new wxStaticText(p, wxID_ANY, _T("Same with grow mode == \"all\"")), 0, wxCENTER | wxTOP, 20);
9e023db7 358 sizerFlex = new wxFlexGridSizer(3, 3);
bdbb07ed 359 InitFlexSizer(sizerFlex, p);
9e023db7
VZ
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
bdbb07ed 371 p->SetSizer(sizerTop);
9e023db7 372 sizerTop->SetSizeHints(this);
c801d85f
KB
373}
374
9e023db7
VZ
375// ----------------------------------------------------------------------------
376// MySizerDialog
377// ----------------------------------------------------------------------------
c801d85f 378
9e023db7 379MySizerDialog::MySizerDialog(wxWindow *parent, const wxChar *title)
9230b621 380 : wxDialog(parent, wxID_ANY, wxString(title))
83edc0a5 381{
83edc0a5
RR
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 );
56ac3e75 386
9230b621 387 wxNotebook *notebook = new wxNotebook( this, wxID_ANY );
adbf2d73 388 topsizer->Add( notebook, 1, wxGROW );
56ac3e75 389
9e023db7 390 wxButton *button = new wxButton( this, wxID_OK, _T("OK") );
83edc0a5
RR
391 topsizer->Add( button, 0, wxALIGN_RIGHT | wxALL, 10 );
392
393 // First page: one big text ctrl
9230b621 394 wxTextCtrl *multi = new wxTextCtrl( notebook, wxID_ANY, _T("TextCtrl."), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
42ed7532 395 notebook->AddPage( multi, _T("Page One") );
56ac3e75 396
83edc0a5 397 // Second page: a text ctrl and a button
9230b621 398 wxPanel *panel = new wxPanel( notebook, wxID_ANY );
42ed7532 399 notebook->AddPage( panel, _T("Page Two") );
56ac3e75 400
f6bcfd97 401 wxSizer *panelsizer = new wxBoxSizer( wxVERTICAL );
56ac3e75 402
6bbb8a8d 403 wxTextCtrl *text = new wxTextCtrl( panel, wxID_ANY, _T("TextLine 1."), wxDefaultPosition, wxSize(250,wxDefaultCoord) );
83edc0a5 404 panelsizer->Add( text, 0, wxGROW|wxALL, 30 );
6bbb8a8d 405 text = new wxTextCtrl( panel, wxID_ANY, _T("TextLine 2."), wxDefaultPosition, wxSize(250,wxDefaultCoord) );
83edc0a5 406 panelsizer->Add( text, 0, wxGROW|wxALL, 30 );
9230b621 407 wxButton *button2 = new wxButton( panel, wxID_ANY, _T("Hallo") );
83edc0a5 408 panelsizer->Add( button2, 0, wxALIGN_RIGHT | wxLEFT|wxRIGHT|wxBOTTOM, 30 );
56ac3e75 409
9230b621 410 panel->SetAutoLayout( true );
83edc0a5 411 panel->SetSizer( panelsizer );
56ac3e75 412
83edc0a5 413 // Tell dialog to use sizer
9e023db7
VZ
414 SetSizer( topsizer );
415 topsizer->SetSizeHints( this );
c62ac5b6
RR
416}
417
20b35a69
RD
418// ----------------------------------------------------------------------------
419// MyGridBagSizerFrame
420// ----------------------------------------------------------------------------
421
422// some simple macros to help make the sample code below more clear
9230b621
VS
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)
20b35a69
RD
425#define POS(r, c) wxGBPosition(r,c)
426#define SPAN(r, c) wxGBSpan(r,c)
427
fbfb8bcc 428const wxChar gbsDescription[] =_T("\
77c5e923
RD
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.");
20b35a69
RD
432
433
434// Some IDs
435enum {
436 GBS_HIDE_BTN = 1212,
437 GBS_SHOW_BTN,
438 GBS_MOVE_BTN1,
439 GBS_MOVE_BTN2,
925e9792 440
004f4002 441 GBS_MAX
20b35a69
RD
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 )
9230b621 454 : wxFrame( NULL, wxID_ANY, title, wxPoint(x, y) )
20b35a69 455{
9230b621 456 wxPanel* p = new wxPanel(this, wxID_ANY);
20b35a69
RD
457 m_panel = p;
458 m_gbs = new wxGridBagSizer();
459
925e9792 460
9230b621 461 m_gbs->Add( new wxStaticText(p, wxID_ANY, gbsDescription),
77c5e923
RD
462 POS(0,0), SPAN(1, 7),
463 wxALIGN_CENTER | wxALL, 5);
925e9792 464
20b35a69
RD
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) );
925e9792 476
20b35a69
RD
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
77c5e923
RD
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)"));
20b35a69
RD
483 m_gbs->Add( m_moveBtn1, POS(10,2) );
484 m_gbs->Add( m_moveBtn2, POS(10,3) );
925e9792 485
77c5e923 486 m_hideBtn = new wxButton(p, GBS_HIDE_BTN, _T("Hide this item -->"));
20b35a69
RD
487 m_gbs->Add(m_hideBtn, POS(12, 3));
488
6bbb8a8d
WS
489 m_hideTxt = new wxTextCtrl(p, wxID_ANY, _T("pos(12,4), size(150, wxDefaultCoord)"),
490 wxDefaultPosition, wxSize(150,wxDefaultCoord));
20b35a69 491 m_gbs->Add( m_hideTxt, POS(12,4) );
925e9792 492
77c5e923 493 m_showBtn = new wxButton(p, GBS_SHOW_BTN, _T("<-- Show it again"));
20b35a69
RD
494 m_gbs->Add(m_showBtn, POS(12, 5));
495 m_showBtn->Disable();
496
497 m_gbs->Add(10,10, POS(14,0));
925e9792 498
20b35a69
RD
499 m_gbs->AddGrowableRow(3);
500 m_gbs->AddGrowableCol(2);
925e9792 501
20b35a69
RD
502 p->SetSizerAndFit(m_gbs);
503 SetClientSize(p->GetSize());
504}
505
925e9792 506
20b35a69
RD
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);
77c5e923 533 btn->SetLabel(_T("Move this to (3,6)"));
20b35a69 534 }
925e9792 535 else
20b35a69 536 {
77c5e923
RD
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);
925e9792 542
77c5e923
RD
543 if ( m_gbs->SetItemPosition(btn, wxGBPosition(3,6)) )
544 {
545 m_lastPos = curPos;
546 btn->SetLabel(_T("Move it back"));
547 }
20b35a69
RD
548 }
549 m_gbs->Layout();
550}
92c0c172
RR
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;
6bbb8a8d 571
92c0c172
RR
572 menu->Append(ID_SET_SMALL, _T("Make text control small\tF4"));
573 menu->Append(ID_SET_BIG, _T("Make text control big\tF5"));
6bbb8a8d 574
92c0c172
RR
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
6bbb8a8d 582 m_target = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize( 80, wxDefaultCoord ) );
92c0c172 583 main_sizer->Add( m_target, 1, wxALL, 5 );
6bbb8a8d
WS
584
585 main_sizer->Add( new wxStaticText( this, wxID_ANY, wxT("Set alternating sizes using F4 and F5") ), 0, wxALL, 5 );
586
92c0c172 587 SetSizer( main_sizer);
6bbb8a8d 588
92c0c172
RR
589 Layout();
590 GetSizer()->Fit( this );
591}
592
6bbb8a8d 593void MySimpleSizerFrame::OnSetSmallSize( wxCommandEvent& WXUNUSED(event))
92c0c172
RR
594{
595 GetSizer()->SetItemMinSize( m_target, 40, -1 );
596 Layout();
597 GetSizer()->Fit( this );
598}
599
6bbb8a8d 600void MySimpleSizerFrame::OnSetBigSize( wxCommandEvent& WXUNUSED(event))
92c0c172
RR
601{
602 GetSizer()->SetItemMinSize( m_target, 140, -1 );
603 Layout();
604 GetSizer()->Fit( this );
605}
606
607