]> git.saurik.com Git - wxWidgets.git/blame - samples/layout/layout.cpp
Applied patch [ 708377 ] Make NET (smapi.cpp) UNICODE compatible
[wxWidgets.git] / samples / layout / layout.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: layout.cpp
3// Purpose: Layout sample
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6aa89a22 8// Copyright: (c) Julian Smart
2f6c54eb 9// Licence: wxWindows license
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
9e023db7
VZ
12// ----------------------------------------------------------------------------
13// headers
14// ----------------------------------------------------------------------------
15
c801d85f
KB
16// For compilers that support precompilation, includes "wx/wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#include "wx/wx.h"
25#endif
26
e4b19d9b 27#if !wxUSE_CONSTRAINTS
ad813b00 28#error You must set wxUSE_CONSTRAINTS to 1 in setup.h!
c801d85f
KB
29#endif
30
c62ac5b6 31#include "wx/sizer.h"
61d514bb 32#include "wx/statline.h"
83edc0a5 33#include "wx/notebook.h"
c62ac5b6 34
c801d85f
KB
35#include "layout.h"
36
9e023db7
VZ
37// ----------------------------------------------------------------------------
38// MyApp
39// ----------------------------------------------------------------------------
c801d85f
KB
40
41IMPLEMENT_APP(MyApp)
42
c801d85f
KB
43MyApp::MyApp()
44{
45}
46
83edc0a5 47bool MyApp::OnInit()
c801d85f
KB
48{
49 // Create the main frame window
9e023db7
VZ
50 MyFrame *frame = new MyFrame;
51
52 frame->Show(TRUE);
53
54 return TRUE;
55}
56
57// ----------------------------------------------------------------------------
58// MyFrame
59// ----------------------------------------------------------------------------
c801d85f 60
9e023db7
VZ
61BEGIN_EVENT_TABLE(MyFrame, wxFrame)
62 EVT_MENU(LAYOUT_ABOUT, MyFrame::OnAbout)
63 EVT_MENU(LAYOUT_QUIT, MyFrame::OnQuit)
c801d85f 64
9e023db7
VZ
65 EVT_MENU(LAYOUT_TEST_CONSTRAINTS, MyFrame::TestConstraints)
66 EVT_MENU(LAYOUT_TEST_SIZER, MyFrame::TestFlexSizers)
67 EVT_MENU(LAYOUT_TEST_NB_SIZER, MyFrame::TestNotebookSizers)
68END_EVENT_TABLE()
c801d85f 69
9e023db7
VZ
70// Define my frame constructor
71MyFrame::MyFrame()
72 : wxFrame(NULL, -1, _T("wxWindows Layout Demo"),
73 wxDefaultPosition, wxDefaultSize,
74 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
75{
c801d85f
KB
76 // Make a menubar
77 wxMenu *file_menu = new wxMenu;
78
9e023db7
VZ
79 file_menu->Append(LAYOUT_TEST_CONSTRAINTS, _T("Test &constraints"));
80 file_menu->Append(LAYOUT_TEST_SIZER, _T("Test wx&FlexSizer"));
81 file_menu->Append(LAYOUT_TEST_NB_SIZER, _T("&Test notebook sizers"));
c801d85f
KB
82
83 file_menu->AppendSeparator();
9e023db7 84 file_menu->Append(LAYOUT_QUIT, _T("E&xit"), _T("Quit program"));
c801d85f
KB
85
86 wxMenu *help_menu = new wxMenu;
9e023db7 87 help_menu->Append(LAYOUT_ABOUT, _T("&About"), _T("About layout demo"));
c801d85f 88
9e023db7 89 wxMenuBar *menu_bar = new wxMenuBar;
c801d85f 90
42ed7532
MB
91 menu_bar->Append(file_menu, _T("&File"));
92 menu_bar->Append(help_menu, _T("&Help"));
c801d85f
KB
93
94 // Associate the menu bar with the frame
9e023db7
VZ
95 SetMenuBar(menu_bar);
96
97 CreateStatusBar(2);
98 SetStatusText(_T("wxWindows layout demo"));
99
100
101 // we want to get a dialog that is stretchable because it
102 // has a text ctrl in the middle. at the bottom, we have
103 // two buttons which.
104
105 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
106
107 // 1) top: create wxStaticText with minimum size equal to its default size
108 topsizer->Add(
109 new wxStaticText( this, -1, _T("An explanation (wxALIGN_RIGHT).") ),
110 0, // make vertically unstretchable
111 wxALIGN_RIGHT | // right align text
112 wxTOP | wxLEFT | wxRIGHT, // make border all around except wxBOTTOM
113 5 ); // set border width to 5
114
115 // 2) top: create wxTextCtrl with minimum size (100x60)
116 topsizer->Add(
117 new wxTextCtrl( this, -1, _T("My text (wxEXPAND)."), wxDefaultPosition, wxSize(100,60), wxTE_MULTILINE),
118 1, // make vertically stretchable
119 wxEXPAND | // make horizontally stretchable
120 wxALL, // and make border all around
121 5 ); // set border width to 5
122
123 // 2.5) Gratuitous test of wxStaticBoxSizers
124 wxBoxSizer *statsizer = new wxStaticBoxSizer(
125 new wxStaticBox(this, -1, _T("A wxStaticBoxSizer")),
126 wxVERTICAL );
127 statsizer->Add(
128 new wxStaticText(this, -1, _T("And some TEXT inside it")),
129 0,
130 wxCENTER |
131 wxALL,
132 30);
133 topsizer->Add(statsizer, 1, wxEXPAND | wxALL, 10);
134
135 // 2.7) And a test of wxGridSizer
136 wxGridSizer *gridsizer = new wxGridSizer(2, 5, 5);
137 gridsizer->Add(new wxStaticText(this, -1, _T("Label")), 0,
138 wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
139 gridsizer->Add(new wxTextCtrl(this, -1, _T("Grid sizer demo")), 1,
140 wxGROW | wxALIGN_CENTER_VERTICAL);
141 gridsizer->Add(new wxStaticText(this, -1, _T("Another label")), 0,
142 wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
143 gridsizer->Add(new wxTextCtrl(this, -1, _T("More text")), 1,
144 wxGROW | wxALIGN_CENTER_VERTICAL);
145 gridsizer->Add(new wxStaticText(this, -1, _T("Final label")), 0,
146 wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
147 gridsizer->Add(new wxTextCtrl(this, -1, _T("And yet more text")), 1,
148 wxGROW | wxALIGN_CENTER_VERTICAL);
149 topsizer->Add(gridsizer, 1, wxGROW | wxALL, 10);
150
151
152 // 3) middle: create wxStaticLine with minimum size (3x3)
153 topsizer->Add(
154 new wxStaticLine( this, -1, wxDefaultPosition, wxSize(3,3), wxHORIZONTAL),
155 0, // make vertically unstretchable
156 wxEXPAND | // make horizontally stretchable
157 wxALL, // and make border all around
158 5 ); // set border width to 5
159
160
161 // 4) bottom: create two centred wxButtons
162 wxBoxSizer *button_box = new wxBoxSizer( wxHORIZONTAL );
163 button_box->Add(
164 new wxButton( this, -1, _T("Two buttons in a box") ),
165 0, // make horizontally unstretchable
166 wxALL, // make border all around
167 7 ); // set border width to 7
168 button_box->Add(
169 new wxButton( this, -1, _T("(wxCENTER)") ),
170 0, // make horizontally unstretchable
171 wxALL, // make border all around
172 7 ); // set border width to 7
173
174 topsizer->Add(
175 button_box,
176 0, // make vertically unstretchable
177 wxCENTER ); // no border and centre horizontally
178
179 // don't allow frame to get smaller than what the sizers tell it and also set
180 // the initial size as calculated by the sizers
181 topsizer->SetSizeHints( this );
182
183 SetSizer( topsizer );
184}
185
186void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
187{
188 Close(TRUE);
189}
190
191void MyFrame::TestConstraints(wxCommandEvent& WXUNUSED(event) )
192{
193 MyConstraintsFrame *
194 newFrame = new MyConstraintsFrame(_T("Constraints Test Frame"), 100, 100);
195 newFrame->Show(TRUE);
196}
197
198void MyFrame::TestFlexSizers(wxCommandEvent& WXUNUSED(event) )
199{
200 MyFlexSizerFrame *newFrame = new MyFlexSizerFrame(_T("Flex Sizer Test Frame"), 50, 50);
201 newFrame->Show(TRUE);
202}
203
204void MyFrame::TestNotebookSizers(wxCommandEvent& WXUNUSED(event) )
205{
206 MySizerDialog dialog( this, _T("Notebook Sizer Test Dialog") );
207
208 dialog.ShowModal();
209}
210
c801d85f 211
9e023db7
VZ
212void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
213{
214 (void)wxMessageBox(_T("wxWindows GUI library layout demo\n"),
215 _T("About Layout Demo"), wxOK|wxICON_INFORMATION);
216}
217
218// ----------------------------------------------------------------------------
219// MyConstraintsFrame
220// ----------------------------------------------------------------------------
221
222MyConstraintsFrame::MyConstraintsFrame(const wxChar *title, int x, int y)
223 : wxFrame(NULL, -1, title, wxPoint(x, y) )
224{
c801d85f 225 // Make a panel
9e023db7 226 wxPanel *panel = new wxPanel(this);
c801d85f
KB
227
228 // Create some panel items
42ed7532 229 wxButton *btn1 = new wxButton(panel, -1, _T("A button (1)")) ;
c801d85f
KB
230
231 wxLayoutConstraints *b1 = new wxLayoutConstraints;
2b5f62a0
VZ
232 b1->centreX.SameAs (panel, wxCentreX);
233 b1->top.SameAs (panel, wxTop, 5);
234 b1->width.PercentOf (panel, wxWidth, 80);
235 b1->height.AsIs ();
c801d85f
KB
236 btn1->SetConstraints(b1);
237
2b5f62a0 238 wxListBox *list = new wxListBox(panel, -1,
c801d85f 239 wxPoint(-1, -1), wxSize(200, 100));
42ed7532
MB
240 list->Append(_T("Apple"));
241 list->Append(_T("Pear"));
242 list->Append(_T("Orange"));
243 list->Append(_T("Banana"));
244 list->Append(_T("Fruit"));
c801d85f
KB
245
246 wxLayoutConstraints *b2 = new wxLayoutConstraints;
247 b2->top.Below (btn1, 5);
2b5f62a0
VZ
248 b2->left.SameAs (panel, wxLeft, 5);
249 b2->width.PercentOf (panel, wxWidth, 40);
250 b2->bottom.SameAs (panel, wxBottom, 5);
c801d85f
KB
251 list->SetConstraints(b2);
252
9e023db7
VZ
253 wxTextCtrl *mtext = new wxTextCtrl(panel, -1,
254 _T("This frame is laid out using\n"
255 "constraints, but the preferred\n"
256 "layout mechanism now are sizers."),
257 wxDefaultPosition,
258 wxDefaultSize,
259 wxTE_MULTILINE);
c801d85f
KB
260
261 wxLayoutConstraints *b3 = new wxLayoutConstraints;
262 b3->top.Below (btn1, 5);
263 b3->left.RightOf (list, 5);
2b5f62a0
VZ
264 b3->right.SameAs (panel, wxRight, 5);
265 b3->bottom.SameAs (panel, wxBottom, 5);
c801d85f
KB
266 mtext->SetConstraints(b3);
267
9e023db7 268 wxTextCtrl *canvas = new wxTextCtrl(this, -1, _T("yet another window"));
c801d85f
KB
269
270 // Make a text window
9e023db7
VZ
271 wxTextCtrl *text_window = new wxTextCtrl(this, -1, _T(""),
272 wxDefaultPosition,
273 wxDefaultSize,
274 wxTE_MULTILINE);
c801d85f
KB
275
276 // Set constraints for panel subwindow
277 wxLayoutConstraints *c1 = new wxLayoutConstraints;
278
9e023db7
VZ
279 c1->left.SameAs (this, wxLeft);
280 c1->top.SameAs (this, wxTop);
281 c1->right.PercentOf (this, wxWidth, 50);
282 c1->height.PercentOf (this, wxHeight, 50);
c801d85f 283
2b5f62a0 284 panel->SetConstraints(c1);
c801d85f
KB
285
286 // Set constraints for canvas subwindow
287 wxLayoutConstraints *c2 = new wxLayoutConstraints;
288
2b5f62a0 289 c2->left.SameAs (panel, wxRight);
9e023db7
VZ
290 c2->top.SameAs (this, wxTop);
291 c2->right.SameAs (this, wxRight);
292 c2->height.PercentOf (this, wxHeight, 50);
c801d85f 293
2b5f62a0 294 canvas->SetConstraints(c2);
c801d85f
KB
295
296 // Set constraints for text subwindow
297 wxLayoutConstraints *c3 = new wxLayoutConstraints;
9e023db7 298 c3->left.SameAs (this, wxLeft);
2b5f62a0 299 c3->top.Below (panel);
9e023db7
VZ
300 c3->right.SameAs (this, wxRight);
301 c3->bottom.SameAs (this, wxBottom);
c801d85f 302
2b5f62a0 303 text_window->SetConstraints(c3);
c801d85f 304
9e023db7 305 SetAutoLayout(TRUE);
c801d85f
KB
306}
307
9e023db7
VZ
308// ----------------------------------------------------------------------------
309// MyFlexSizerFrame
310// ----------------------------------------------------------------------------
c62ac5b6 311
9e023db7 312void MyFlexSizerFrame::InitFlexSizer(wxFlexGridSizer *sizer)
c801d85f 313{
9e023db7
VZ
314 for ( int i = 0; i < 3; i++ )
315 {
316 for ( int j = 0; j < 3; j++ )
317 {
318 sizer->Add(new wxStaticText
319 (
320 this,
321 -1,
322 wxString::Format(_T("(%d, %d)"), i + 1, j + 1),
323 wxDefaultPosition,
324 wxDefaultSize,
325 wxALIGN_CENTER
326 ),
327 0, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 3);
328 }
329 }
c801d85f
KB
330}
331
9e023db7
VZ
332MyFlexSizerFrame::MyFlexSizerFrame(const wxChar *title, int x, int y )
333 : wxFrame(NULL, -1, title, wxPoint(x, y) )
c801d85f 334{
9e023db7
VZ
335 wxFlexGridSizer *sizerFlex;
336
337 // consttuct the first column
338 wxSizer *sizerCol1 = new wxBoxSizer(wxVERTICAL);
339 sizerCol1->Add(new wxStaticText(this, -1, _T("Ungrowable:")), 0, wxCENTER | wxTOP, 20);
340 sizerFlex = new wxFlexGridSizer(3, 3);
341 InitFlexSizer(sizerFlex);
342 sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
343
344 sizerCol1->Add(new wxStaticText(this, -1, _T("Growable middle column:")), 0, wxCENTER | wxTOP, 20);
345 sizerFlex = new wxFlexGridSizer(3, 3);
346 InitFlexSizer(sizerFlex);
347 sizerFlex->AddGrowableCol(1);
348 sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
349
350 sizerCol1->Add(new wxStaticText(this, -1, _T("Growable middle row:")), 0, wxCENTER | wxTOP, 20);
351 sizerFlex = new wxFlexGridSizer(3, 3);
352 InitFlexSizer(sizerFlex);
353 sizerFlex->AddGrowableRow(1);
354 sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
355
356 sizerCol1->Add(new wxStaticText(this, -1, _T("All growable columns:")), 0, wxCENTER | wxTOP, 20);
357 sizerFlex = new wxFlexGridSizer(3, 3);
358 InitFlexSizer(sizerFlex);
359 sizerFlex->AddGrowableCol(0, 1);
360 sizerFlex->AddGrowableCol(1, 2);
361 sizerFlex->AddGrowableCol(2, 3);
362 sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
363
364 // the second one
365 wxSizer *sizerCol2 = new wxBoxSizer(wxVERTICAL);
366 sizerCol2->Add(new wxStaticText(this, -1, _T("Growable middle row and column:")), 0, wxCENTER | wxTOP, 20);
367 sizerFlex = new wxFlexGridSizer(3, 3);
368 InitFlexSizer(sizerFlex);
369 sizerFlex->AddGrowableCol(1);
370 sizerFlex->AddGrowableRow(1);
371 sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
372
373 sizerCol2->Add(new wxStaticText(this, -1, _T("Same with horz flex direction")), 0, wxCENTER | wxTOP, 20);
374 sizerFlex = new wxFlexGridSizer(3, 3);
375 InitFlexSizer(sizerFlex);
376 sizerFlex->AddGrowableCol(1);
377 sizerFlex->AddGrowableRow(1);
378 sizerFlex->SetFlexibleDirection(wxHORIZONTAL);
379 sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
380
381 sizerCol2->Add(new wxStaticText(this, -1, _T("Same with grow mode == \"none\"")), 0, wxCENTER | wxTOP, 20);
382 sizerFlex = new wxFlexGridSizer(3, 3);
383 InitFlexSizer(sizerFlex);
384 sizerFlex->AddGrowableCol(1);
385 sizerFlex->AddGrowableRow(1);
386 sizerFlex->SetFlexibleDirection(wxHORIZONTAL);
387 sizerFlex->SetNonFlexibleGrowMode(wxFLEX_GROWMODE_NONE);
388 sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
389
390 sizerCol2->Add(new wxStaticText(this, -1, _T("Same with grow mode == \"all\"")), 0, wxCENTER | wxTOP, 20);
391 sizerFlex = new wxFlexGridSizer(3, 3);
392 InitFlexSizer(sizerFlex);
393 sizerFlex->AddGrowableCol(1);
394 sizerFlex->AddGrowableRow(1);
395 sizerFlex->SetFlexibleDirection(wxHORIZONTAL);
396 sizerFlex->SetNonFlexibleGrowMode(wxFLEX_GROWMODE_ALL);
397 sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10);
398
399 // add both columns to grid sizer
400 wxGridSizer *sizerTop = new wxGridSizer(2, 0, 20);
401 sizerTop->Add(sizerCol1, 1, wxEXPAND);
402 sizerTop->Add(sizerCol2, 1, wxEXPAND);
403
404 SetSizer(sizerTop);
405 sizerTop->SetSizeHints(this);
c801d85f
KB
406}
407
9e023db7
VZ
408// ----------------------------------------------------------------------------
409// MySizerDialog
410// ----------------------------------------------------------------------------
c801d85f 411
9e023db7
VZ
412MySizerDialog::MySizerDialog(wxWindow *parent, const wxChar *title)
413 : wxDialog(parent, -1, wxString(title))
83edc0a5 414{
83edc0a5
RR
415 // Begin with first hierarchy: a notebook at the top and
416 // and OK button at the bottom.
417
418 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
56ac3e75 419
9e023db7 420 wxNotebook *notebook = new wxNotebook( this, -1 );
83edc0a5
RR
421 wxNotebookSizer *nbs = new wxNotebookSizer( notebook );
422 topsizer->Add( nbs, 1, wxGROW );
56ac3e75 423
9e023db7 424 wxButton *button = new wxButton( this, wxID_OK, _T("OK") );
83edc0a5
RR
425 topsizer->Add( button, 0, wxALIGN_RIGHT | wxALL, 10 );
426
427 // First page: one big text ctrl
42ed7532
MB
428 wxTextCtrl *multi = new wxTextCtrl( notebook, -1, _T("TextCtrl."), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
429 notebook->AddPage( multi, _T("Page One") );
56ac3e75 430
83edc0a5
RR
431 // Second page: a text ctrl and a button
432 wxPanel *panel = new wxPanel( notebook, -1 );
42ed7532 433 notebook->AddPage( panel, _T("Page Two") );
56ac3e75 434
f6bcfd97 435 wxSizer *panelsizer = new wxBoxSizer( wxVERTICAL );
56ac3e75 436
42ed7532 437 wxTextCtrl *text = new wxTextCtrl( panel, -1, _T("TextLine 1."), wxDefaultPosition, wxSize(250,-1) );
83edc0a5 438 panelsizer->Add( text, 0, wxGROW|wxALL, 30 );
42ed7532 439 text = new wxTextCtrl( panel, -1, _T("TextLine 2."), wxDefaultPosition, wxSize(250,-1) );
83edc0a5 440 panelsizer->Add( text, 0, wxGROW|wxALL, 30 );
42ed7532 441 wxButton *button2 = new wxButton( panel, -1, _T("Hallo") );
83edc0a5 442 panelsizer->Add( button2, 0, wxALIGN_RIGHT | wxLEFT|wxRIGHT|wxBOTTOM, 30 );
56ac3e75 443
83edc0a5
RR
444 panel->SetAutoLayout( TRUE );
445 panel->SetSizer( panelsizer );
56ac3e75 446
83edc0a5 447 // Tell dialog to use sizer
9e023db7
VZ
448 SetSizer( topsizer );
449 topsizer->SetSizeHints( this );
c62ac5b6
RR
450}
451