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