1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Layout sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ----------------------------------------------------------------------------
14 // ----------------------------------------------------------------------------
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
27 #if !wxUSE_CONSTRAINTS
28 #error You must set wxUSE_CONSTRAINTS to 1 in setup.h!
32 #include "wx/statline.h"
33 #include "wx/notebook.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
49 // Create the main frame window
50 MyFrame
*frame
= new MyFrame
;
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
62 EVT_MENU(LAYOUT_ABOUT
, MyFrame::OnAbout
)
63 EVT_MENU(LAYOUT_QUIT
, MyFrame::OnQuit
)
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
)
70 // Define my frame constructor
72 : wxFrame(NULL
, -1, _T("wxWindows Layout Demo"),
73 wxDefaultPosition
, wxDefaultSize
,
74 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
77 wxMenu
*file_menu
= new wxMenu
;
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"));
83 file_menu
->AppendSeparator();
84 file_menu
->Append(LAYOUT_QUIT
, _T("E&xit"), _T("Quit program"));
86 wxMenu
*help_menu
= new wxMenu
;
87 help_menu
->Append(LAYOUT_ABOUT
, _T("&About"), _T("About layout demo"));
89 wxMenuBar
*menu_bar
= new wxMenuBar
;
91 menu_bar
->Append(file_menu
, _T("&File"));
92 menu_bar
->Append(help_menu
, _T("&Help"));
94 // Associate the menu bar with the frame
98 SetStatusText(_T("wxWindows layout demo"));
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.
105 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
107 // 1) top: create wxStaticText with minimum size equal to its default size
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
115 // 2) top: create wxTextCtrl with minimum size (100x60)
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
123 // 2.5) Gratuitous test of wxStaticBoxSizers
124 wxBoxSizer
*statsizer
= new wxStaticBoxSizer(
125 new wxStaticBox(this, -1, _T("A wxStaticBoxSizer")),
128 new wxStaticText(this, -1, _T("And some TEXT inside it")),
133 topsizer
->Add(statsizer
, 1, wxEXPAND
| wxALL
, 10);
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);
152 // 3) middle: create wxStaticLine with minimum size (3x3)
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
161 // 4) bottom: create two centred wxButtons
162 wxBoxSizer
*button_box
= new wxBoxSizer( wxHORIZONTAL
);
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
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
176 0, // make vertically unstretchable
177 wxCENTER
); // no border and centre horizontally
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 );
183 SetSizer( topsizer
);
186 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
191 void MyFrame::TestConstraints(wxCommandEvent
& WXUNUSED(event
) )
194 newFrame
= new MyConstraintsFrame(_T("Constraints Test Frame"), 100, 100);
195 newFrame
->Show(TRUE
);
198 void MyFrame::TestFlexSizers(wxCommandEvent
& WXUNUSED(event
) )
200 MyFlexSizerFrame
*newFrame
= new MyFlexSizerFrame(_T("Flex Sizer Test Frame"), 50, 50);
201 newFrame
->Show(TRUE
);
204 void MyFrame::TestNotebookSizers(wxCommandEvent
& WXUNUSED(event
) )
206 MySizerDialog
dialog( this, _T("Notebook Sizer Test Dialog") );
212 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
214 (void)wxMessageBox(_T("wxWindows GUI library layout demo\n"),
215 _T("About Layout Demo"), wxOK
|wxICON_INFORMATION
);
218 // ----------------------------------------------------------------------------
219 // MyConstraintsFrame
220 // ----------------------------------------------------------------------------
222 MyConstraintsFrame::MyConstraintsFrame(const wxChar
*title
, int x
, int y
)
223 : wxFrame(NULL
, -1, title
, wxPoint(x
, y
) )
226 wxPanel
*panel
= new wxPanel(this);
228 // Create some panel items
229 wxButton
*btn1
= new wxButton(panel
, -1, _T("A button (1)")) ;
231 wxLayoutConstraints
*b1
= new wxLayoutConstraints
;
232 b1
->centreX
.SameAs (panel
, wxCentreX
);
233 b1
->top
.SameAs (panel
, wxTop
, 5);
234 b1
->width
.PercentOf (panel
, wxWidth
, 80);
236 btn1
->SetConstraints(b1
);
238 wxListBox
*list
= new wxListBox(panel
, -1,
239 wxPoint(-1, -1), wxSize(200, 100));
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"));
246 wxLayoutConstraints
*b2
= new wxLayoutConstraints
;
247 b2
->top
.Below (btn1
, 5);
248 b2
->left
.SameAs (panel
, wxLeft
, 5);
249 b2
->width
.PercentOf (panel
, wxWidth
, 40);
250 b2
->bottom
.SameAs (panel
, wxBottom
, 5);
251 list
->SetConstraints(b2
);
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."),
261 wxLayoutConstraints
*b3
= new wxLayoutConstraints
;
262 b3
->top
.Below (btn1
, 5);
263 b3
->left
.RightOf (list
, 5);
264 b3
->right
.SameAs (panel
, wxRight
, 5);
265 b3
->bottom
.SameAs (panel
, wxBottom
, 5);
266 mtext
->SetConstraints(b3
);
268 wxTextCtrl
*canvas
= new wxTextCtrl(this, -1, _T("yet another window"));
270 // Make a text window
271 wxTextCtrl
*text_window
= new wxTextCtrl(this, -1, _T(""),
276 // Set constraints for panel subwindow
277 wxLayoutConstraints
*c1
= new wxLayoutConstraints
;
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);
284 panel
->SetConstraints(c1
);
286 // Set constraints for canvas subwindow
287 wxLayoutConstraints
*c2
= new wxLayoutConstraints
;
289 c2
->left
.SameAs (panel
, wxRight
);
290 c2
->top
.SameAs (this, wxTop
);
291 c2
->right
.SameAs (this, wxRight
);
292 c2
->height
.PercentOf (this, wxHeight
, 50);
294 canvas
->SetConstraints(c2
);
296 // Set constraints for text subwindow
297 wxLayoutConstraints
*c3
= new wxLayoutConstraints
;
298 c3
->left
.SameAs (this, wxLeft
);
299 c3
->top
.Below (panel
);
300 c3
->right
.SameAs (this, wxRight
);
301 c3
->bottom
.SameAs (this, wxBottom
);
303 text_window
->SetConstraints(c3
);
308 // ----------------------------------------------------------------------------
310 // ----------------------------------------------------------------------------
312 void MyFlexSizerFrame::InitFlexSizer(wxFlexGridSizer
*sizer
)
314 for ( int i
= 0; i
< 3; i
++ )
316 for ( int j
= 0; j
< 3; j
++ )
318 sizer
->Add(new wxStaticText
322 wxString::Format(_T("(%d, %d)"), i
+ 1, j
+ 1),
327 0, wxEXPAND
| wxALIGN_CENTER_VERTICAL
| wxALL
, 3);
332 MyFlexSizerFrame::MyFlexSizerFrame(const wxChar
*title
, int x
, int y
)
333 : wxFrame(NULL
, -1, title
, wxPoint(x
, y
) )
335 wxFlexGridSizer
*sizerFlex
;
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);
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);
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);
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);
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);
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);
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);
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);
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
);
405 sizerTop
->SetSizeHints(this);
408 // ----------------------------------------------------------------------------
410 // ----------------------------------------------------------------------------
412 MySizerDialog::MySizerDialog(wxWindow
*parent
, const wxChar
*title
)
413 : wxDialog(parent
, -1, wxString(title
))
415 // Begin with first hierarchy: a notebook at the top and
416 // and OK button at the bottom.
418 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
420 wxNotebook
*notebook
= new wxNotebook( this, -1 );
421 wxNotebookSizer
*nbs
= new wxNotebookSizer( notebook
);
422 topsizer
->Add( nbs
, 1, wxGROW
);
424 wxButton
*button
= new wxButton( this, wxID_OK
, _T("OK") );
425 topsizer
->Add( button
, 0, wxALIGN_RIGHT
| wxALL
, 10 );
427 // First page: one big text ctrl
428 wxTextCtrl
*multi
= new wxTextCtrl( notebook
, -1, _T("TextCtrl."), wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
429 notebook
->AddPage( multi
, _T("Page One") );
431 // Second page: a text ctrl and a button
432 wxPanel
*panel
= new wxPanel( notebook
, -1 );
433 notebook
->AddPage( panel
, _T("Page Two") );
435 wxSizer
*panelsizer
= new wxBoxSizer( wxVERTICAL
);
437 wxTextCtrl
*text
= new wxTextCtrl( panel
, -1, _T("TextLine 1."), wxDefaultPosition
, wxSize(250,-1) );
438 panelsizer
->Add( text
, 0, wxGROW
|wxALL
, 30 );
439 text
= new wxTextCtrl( panel
, -1, _T("TextLine 2."), wxDefaultPosition
, wxSize(250,-1) );
440 panelsizer
->Add( text
, 0, wxGROW
|wxALL
, 30 );
441 wxButton
*button2
= new wxButton( panel
, -1, _T("Hallo") );
442 panelsizer
->Add( button2
, 0, wxALIGN_RIGHT
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 30 );
444 panel
->SetAutoLayout( TRUE
);
445 panel
->SetSizer( panelsizer
);
447 // Tell dialog to use sizer
448 SetSizer( topsizer
);
449 topsizer
->SetSizeHints( this );