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