]> git.saurik.com Git - wxWidgets.git/blob - samples/layout/layout.cpp
use DIB, not DDB, when converting from an image with alpha channel
[wxWidgets.git] / samples / layout / layout.cpp
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$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ----------------------------------------------------------------------------
13 // headers
14 // ----------------------------------------------------------------------------
15
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
27 #if !wxUSE_CONSTRAINTS
28 #error You must set wxUSE_CONSTRAINTS to 1 in setup.h!
29 #endif
30
31 #include "wx/sizer.h"
32 #include "wx/statline.h"
33 #include "wx/notebook.h"
34
35 #include "layout.h"
36
37 // ----------------------------------------------------------------------------
38 // MyApp
39 // ----------------------------------------------------------------------------
40
41 IMPLEMENT_APP(MyApp)
42
43 MyApp::MyApp()
44 {
45 }
46
47 bool MyApp::OnInit()
48 {
49 // Create the main frame window
50 MyFrame *frame = new MyFrame;
51
52 frame->Show(TRUE);
53
54 return TRUE;
55 }
56
57 // ----------------------------------------------------------------------------
58 // MyFrame
59 // ----------------------------------------------------------------------------
60
61 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
62 EVT_MENU(LAYOUT_ABOUT, MyFrame::OnAbout)
63 EVT_MENU(LAYOUT_QUIT, MyFrame::OnQuit)
64
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)
68 END_EVENT_TABLE()
69
70 // Define my frame constructor
71 MyFrame::MyFrame()
72 : wxFrame(NULL, -1, _T("wxWindows Layout Demo"),
73 wxDefaultPosition, wxDefaultSize,
74 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
75 {
76 // Make a menubar
77 wxMenu *file_menu = new wxMenu;
78
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"));
82
83 file_menu->AppendSeparator();
84 file_menu->Append(LAYOUT_QUIT, _T("E&xit"), _T("Quit program"));
85
86 wxMenu *help_menu = new wxMenu;
87 help_menu->Append(LAYOUT_ABOUT, _T("&About"), _T("About layout demo"));
88
89 wxMenuBar *menu_bar = new wxMenuBar;
90
91 menu_bar->Append(file_menu, _T("&File"));
92 menu_bar->Append(help_menu, _T("&Help"));
93
94 // Associate the menu bar with the frame
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
186 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
187 {
188 Close(TRUE);
189 }
190
191 void MyFrame::TestConstraints(wxCommandEvent& WXUNUSED(event) )
192 {
193 MyConstraintsFrame *
194 newFrame = new MyConstraintsFrame(_T("Constraints Test Frame"), 100, 100);
195 newFrame->Show(TRUE);
196 }
197
198 void MyFrame::TestFlexSizers(wxCommandEvent& WXUNUSED(event) )
199 {
200 MyFlexSizerFrame *newFrame = new MyFlexSizerFrame(_T("Flex Sizer Test Frame"), 50, 50);
201 newFrame->Show(TRUE);
202 }
203
204 void MyFrame::TestNotebookSizers(wxCommandEvent& WXUNUSED(event) )
205 {
206 MySizerDialog dialog( this, _T("Notebook Sizer Test Dialog") );
207
208 dialog.ShowModal();
209 }
210
211
212 void 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
222 MyConstraintsFrame::MyConstraintsFrame(const wxChar *title, int x, int y)
223 : wxFrame(NULL, -1, title, wxPoint(x, y) )
224 {
225 // Make a panel
226 wxPanel *panel = new wxPanel(this);
227
228 // Create some panel items
229 wxButton *btn1 = new wxButton(panel, -1, _T("A button (1)")) ;
230
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);
235 b1->height.AsIs ();
236 btn1->SetConstraints(b1);
237
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"));
245
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);
252
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);
260
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);
267
268 wxTextCtrl *canvas = new wxTextCtrl(this, -1, _T("yet another window"));
269
270 // Make a text window
271 wxTextCtrl *text_window = new wxTextCtrl(this, -1, _T(""),
272 wxDefaultPosition,
273 wxDefaultSize,
274 wxTE_MULTILINE);
275
276 // Set constraints for panel subwindow
277 wxLayoutConstraints *c1 = new wxLayoutConstraints;
278
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);
283
284 panel->SetConstraints(c1);
285
286 // Set constraints for canvas subwindow
287 wxLayoutConstraints *c2 = new wxLayoutConstraints;
288
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);
293
294 canvas->SetConstraints(c2);
295
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);
302
303 text_window->SetConstraints(c3);
304
305 SetAutoLayout(TRUE);
306 }
307
308 // ----------------------------------------------------------------------------
309 // MyFlexSizerFrame
310 // ----------------------------------------------------------------------------
311
312 void MyFlexSizerFrame::InitFlexSizer(wxFlexGridSizer *sizer)
313 {
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 }
330 }
331
332 MyFlexSizerFrame::MyFlexSizerFrame(const wxChar *title, int x, int y )
333 : wxFrame(NULL, -1, title, wxPoint(x, y) )
334 {
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);
406 }
407
408 // ----------------------------------------------------------------------------
409 // MySizerDialog
410 // ----------------------------------------------------------------------------
411
412 MySizerDialog::MySizerDialog(wxWindow *parent, const wxChar *title)
413 : wxDialog(parent, -1, wxString(title))
414 {
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 );
419
420 wxNotebook *notebook = new wxNotebook( this, -1 );
421 wxNotebookSizer *nbs = new wxNotebookSizer( notebook );
422 topsizer->Add( nbs, 1, wxGROW );
423
424 wxButton *button = new wxButton( this, wxID_OK, _T("OK") );
425 topsizer->Add( button, 0, wxALIGN_RIGHT | wxALL, 10 );
426
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") );
430
431 // Second page: a text ctrl and a button
432 wxPanel *panel = new wxPanel( notebook, -1 );
433 notebook->AddPage( panel, _T("Page Two") );
434
435 wxSizer *panelsizer = new wxBoxSizer( wxVERTICAL );
436
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 );
443
444 panel->SetAutoLayout( TRUE );
445 panel->SetSizer( panelsizer );
446
447 // Tell dialog to use sizer
448 SetSizer( topsizer );
449 topsizer->SetSizeHints( this );
450 }
451