]>
Commit | Line | Data |
---|---|---|
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/gbsizer.h" | |
33 | #include "wx/statline.h" | |
34 | #include "wx/notebook.h" | |
35 | ||
36 | #include "layout.h" | |
37 | ||
38 | // ---------------------------------------------------------------------------- | |
39 | // MyApp | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | IMPLEMENT_APP(MyApp) | |
43 | ||
44 | MyApp::MyApp() | |
45 | { | |
46 | } | |
47 | ||
48 | bool MyApp::OnInit() | |
49 | { | |
50 | // Create the main frame window | |
51 | MyFrame *frame = new MyFrame; | |
52 | ||
53 | frame->Show(TRUE); | |
54 | ||
55 | return TRUE; | |
56 | } | |
57 | ||
58 | // ---------------------------------------------------------------------------- | |
59 | // MyFrame | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
63 | EVT_MENU(LAYOUT_ABOUT, MyFrame::OnAbout) | |
64 | EVT_MENU(LAYOUT_QUIT, MyFrame::OnQuit) | |
65 | ||
66 | EVT_MENU(LAYOUT_TEST_CONSTRAINTS, MyFrame::TestConstraints) | |
67 | EVT_MENU(LAYOUT_TEST_SIZER, MyFrame::TestFlexSizers) | |
68 | EVT_MENU(LAYOUT_TEST_NB_SIZER, MyFrame::TestNotebookSizers) | |
69 | EVT_MENU(LAYOUT_TEST_GB_SIZER, MyFrame::TestGridBagSizer) | |
70 | END_EVENT_TABLE() | |
71 | ||
72 | // Define my frame constructor | |
73 | MyFrame::MyFrame() | |
74 | : wxFrame(NULL, -1, _T("wxWindows Layout Demo"), | |
75 | wxDefaultPosition, wxDefaultSize, | |
76 | wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE) | |
77 | { | |
78 | // Make a menubar | |
79 | wxMenu *file_menu = new wxMenu; | |
80 | ||
81 | file_menu->Append(LAYOUT_TEST_CONSTRAINTS, _T("Test &constraints")); | |
82 | file_menu->Append(LAYOUT_TEST_SIZER, _T("Test wx&FlexSizer")); | |
83 | file_menu->Append(LAYOUT_TEST_NB_SIZER, _T("&Test notebook sizers")); | |
84 | file_menu->Append(LAYOUT_TEST_GB_SIZER, _T("Test &gridbag sizer")); | |
85 | ||
86 | file_menu->AppendSeparator(); | |
87 | file_menu->Append(LAYOUT_QUIT, _T("E&xit"), _T("Quit program")); | |
88 | ||
89 | wxMenu *help_menu = new wxMenu; | |
90 | help_menu->Append(LAYOUT_ABOUT, _T("&About"), _T("About layout demo")); | |
91 | ||
92 | wxMenuBar *menu_bar = new wxMenuBar; | |
93 | ||
94 | menu_bar->Append(file_menu, _T("&File")); | |
95 | menu_bar->Append(help_menu, _T("&Help")); | |
96 | ||
97 | // Associate the menu bar with the frame | |
98 | SetMenuBar(menu_bar); | |
99 | ||
100 | CreateStatusBar(2); | |
101 | SetStatusText(_T("wxWindows layout demo")); | |
102 | ||
103 | ||
104 | // we want to get a dialog that is stretchable because it | |
105 | // has a text ctrl in the middle. at the bottom, we have | |
106 | // two buttons which. | |
107 | ||
108 | wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); | |
109 | ||
110 | // 1) top: create wxStaticText with minimum size equal to its default size | |
111 | topsizer->Add( | |
112 | new wxStaticText( this, -1, _T("An explanation (wxALIGN_RIGHT).") ), | |
113 | 0, // make vertically unstretchable | |
114 | wxALIGN_RIGHT | // right align text | |
115 | wxTOP | wxLEFT | wxRIGHT, // make border all around except wxBOTTOM | |
116 | 5 ); // set border width to 5 | |
117 | ||
118 | // 2) top: create wxTextCtrl with minimum size (100x60) | |
119 | topsizer->Add( | |
120 | new wxTextCtrl( this, -1, _T("My text (wxEXPAND)."), wxDefaultPosition, wxSize(100,60), wxTE_MULTILINE), | |
121 | 1, // make vertically stretchable | |
122 | wxEXPAND | // make horizontally stretchable | |
123 | wxALL, // and make border all around | |
124 | 5 ); // set border width to 5 | |
125 | ||
126 | // 2.5) Gratuitous test of wxStaticBoxSizers | |
127 | wxBoxSizer *statsizer = new wxStaticBoxSizer( | |
128 | new wxStaticBox(this, -1, _T("A wxStaticBoxSizer")), | |
129 | wxVERTICAL ); | |
130 | statsizer->Add( | |
131 | new wxStaticText(this, -1, _T("And some TEXT inside it")), | |
132 | 0, | |
133 | wxCENTER | | |
134 | wxALL, | |
135 | 30); | |
136 | topsizer->Add(statsizer, 1, wxEXPAND | wxALL, 10); | |
137 | ||
138 | // 2.7) And a test of wxGridSizer | |
139 | wxGridSizer *gridsizer = new wxGridSizer(2, 5, 5); | |
140 | gridsizer->Add(new wxStaticText(this, -1, _T("Label")), 0, | |
141 | wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL); | |
142 | gridsizer->Add(new wxTextCtrl(this, -1, _T("Grid sizer demo")), 1, | |
143 | wxGROW | wxALIGN_CENTER_VERTICAL); | |
144 | gridsizer->Add(new wxStaticText(this, -1, _T("Another label")), 0, | |
145 | wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL); | |
146 | gridsizer->Add(new wxTextCtrl(this, -1, _T("More text")), 1, | |
147 | wxGROW | wxALIGN_CENTER_VERTICAL); | |
148 | gridsizer->Add(new wxStaticText(this, -1, _T("Final label")), 0, | |
149 | wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL); | |
150 | gridsizer->Add(new wxTextCtrl(this, -1, _T("And yet more text")), 1, | |
151 | wxGROW | wxALIGN_CENTER_VERTICAL); | |
152 | topsizer->Add(gridsizer, 1, wxGROW | wxALL, 10); | |
153 | ||
154 | ||
155 | // 3) middle: create wxStaticLine with minimum size (3x3) | |
156 | topsizer->Add( | |
157 | new wxStaticLine( this, -1, wxDefaultPosition, wxSize(3,3), wxHORIZONTAL), | |
158 | 0, // make vertically unstretchable | |
159 | wxEXPAND | // make horizontally stretchable | |
160 | wxALL, // and make border all around | |
161 | 5 ); // set border width to 5 | |
162 | ||
163 | ||
164 | // 4) bottom: create two centred wxButtons | |
165 | wxBoxSizer *button_box = new wxBoxSizer( wxHORIZONTAL ); | |
166 | button_box->Add( | |
167 | new wxButton( this, -1, _T("Two buttons in a box") ), | |
168 | 0, // make horizontally unstretchable | |
169 | wxALL, // make border all around | |
170 | 7 ); // set border width to 7 | |
171 | button_box->Add( | |
172 | new wxButton( this, -1, _T("(wxCENTER)") ), | |
173 | 0, // make horizontally unstretchable | |
174 | wxALL, // make border all around | |
175 | 7 ); // set border width to 7 | |
176 | ||
177 | topsizer->Add( | |
178 | button_box, | |
179 | 0, // make vertically unstretchable | |
180 | wxCENTER ); // no border and centre horizontally | |
181 | ||
182 | // don't allow frame to get smaller than what the sizers tell it and also set | |
183 | // the initial size as calculated by the sizers | |
184 | topsizer->SetSizeHints( this ); | |
185 | ||
186 | SetSizer( topsizer ); | |
187 | } | |
188 | ||
189 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) ) | |
190 | { | |
191 | Close(TRUE); | |
192 | } | |
193 | ||
194 | void MyFrame::TestConstraints(wxCommandEvent& WXUNUSED(event) ) | |
195 | { | |
196 | MyConstraintsFrame * | |
197 | newFrame = new MyConstraintsFrame(_T("Constraints Test Frame"), 100, 100); | |
198 | newFrame->Show(TRUE); | |
199 | } | |
200 | ||
201 | void MyFrame::TestFlexSizers(wxCommandEvent& WXUNUSED(event) ) | |
202 | { | |
203 | MyFlexSizerFrame *newFrame = new MyFlexSizerFrame(_T("Flex Sizer Test Frame"), 50, 50); | |
204 | newFrame->Show(TRUE); | |
205 | } | |
206 | ||
207 | void MyFrame::TestNotebookSizers(wxCommandEvent& WXUNUSED(event) ) | |
208 | { | |
209 | MySizerDialog dialog( this, _T("Notebook Sizer Test Dialog") ); | |
210 | ||
211 | dialog.ShowModal(); | |
212 | } | |
213 | ||
214 | ||
215 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) ) | |
216 | { | |
217 | (void)wxMessageBox(_T("wxWindows GUI library layout demo\n"), | |
218 | _T("About Layout Demo"), wxOK|wxICON_INFORMATION); | |
219 | } | |
220 | ||
221 | void MyFrame::TestGridBagSizer(wxCommandEvent& WXUNUSED(event) ) | |
222 | { | |
223 | MyGridBagSizerFrame *newFrame = new | |
224 | MyGridBagSizerFrame(_T("wxGridBagSizer Test Frame"), 50, 50); | |
225 | newFrame->Show(TRUE); | |
226 | } | |
227 | ||
228 | ||
229 | // ---------------------------------------------------------------------------- | |
230 | // MyConstraintsFrame | |
231 | // ---------------------------------------------------------------------------- | |
232 | ||
233 | MyConstraintsFrame::MyConstraintsFrame(const wxChar *title, int x, int y) | |
234 | : wxFrame(NULL, -1, title, wxPoint(x, y) ) | |
235 | { | |
236 | // Make a panel | |
237 | wxPanel *panel = new wxPanel(this); | |
238 | ||
239 | // Create some panel items | |
240 | wxButton *btn1 = new wxButton(panel, -1, _T("A button (1)")) ; | |
241 | ||
242 | wxLayoutConstraints *b1 = new wxLayoutConstraints; | |
243 | b1->centreX.SameAs (panel, wxCentreX); | |
244 | b1->top.SameAs (panel, wxTop, 5); | |
245 | b1->width.PercentOf (panel, wxWidth, 80); | |
246 | b1->height.AsIs (); | |
247 | btn1->SetConstraints(b1); | |
248 | ||
249 | wxListBox *list = new wxListBox(panel, -1, | |
250 | wxPoint(-1, -1), wxSize(200, 100)); | |
251 | list->Append(_T("Apple")); | |
252 | list->Append(_T("Pear")); | |
253 | list->Append(_T("Orange")); | |
254 | list->Append(_T("Banana")); | |
255 | list->Append(_T("Fruit")); | |
256 | ||
257 | wxLayoutConstraints *b2 = new wxLayoutConstraints; | |
258 | b2->top.Below (btn1, 5); | |
259 | b2->left.SameAs (panel, wxLeft, 5); | |
260 | b2->width.PercentOf (panel, wxWidth, 40); | |
261 | b2->bottom.SameAs (panel, wxBottom, 5); | |
262 | list->SetConstraints(b2); | |
263 | ||
264 | wxTextCtrl *mtext = new wxTextCtrl(panel, -1, | |
265 | _T("This frame is laid out using\nconstraints, but the preferred\nlayout mechanism now are sizers."), | |
266 | wxDefaultPosition, | |
267 | wxDefaultSize, | |
268 | wxTE_MULTILINE); | |
269 | ||
270 | wxLayoutConstraints *b3 = new wxLayoutConstraints; | |
271 | b3->top.Below (btn1, 5); | |
272 | b3->left.RightOf (list, 5); | |
273 | b3->right.SameAs (panel, wxRight, 5); | |
274 | b3->bottom.SameAs (panel, wxBottom, 5); | |
275 | mtext->SetConstraints(b3); | |
276 | ||
277 | wxTextCtrl *canvas = new wxTextCtrl(this, -1, _T("yet another window")); | |
278 | ||
279 | // Make a text window | |
280 | wxTextCtrl *text_window = new wxTextCtrl(this, -1, _T(""), | |
281 | wxDefaultPosition, | |
282 | wxDefaultSize, | |
283 | wxTE_MULTILINE); | |
284 | ||
285 | // Set constraints for panel subwindow | |
286 | wxLayoutConstraints *c1 = new wxLayoutConstraints; | |
287 | ||
288 | c1->left.SameAs (this, wxLeft); | |
289 | c1->top.SameAs (this, wxTop); | |
290 | c1->right.PercentOf (this, wxWidth, 50); | |
291 | c1->height.PercentOf (this, wxHeight, 50); | |
292 | ||
293 | panel->SetConstraints(c1); | |
294 | ||
295 | // Set constraints for canvas subwindow | |
296 | wxLayoutConstraints *c2 = new wxLayoutConstraints; | |
297 | ||
298 | c2->left.SameAs (panel, wxRight); | |
299 | c2->top.SameAs (this, wxTop); | |
300 | c2->right.SameAs (this, wxRight); | |
301 | c2->height.PercentOf (this, wxHeight, 50); | |
302 | ||
303 | canvas->SetConstraints(c2); | |
304 | ||
305 | // Set constraints for text subwindow | |
306 | wxLayoutConstraints *c3 = new wxLayoutConstraints; | |
307 | c3->left.SameAs (this, wxLeft); | |
308 | c3->top.Below (panel); | |
309 | c3->right.SameAs (this, wxRight); | |
310 | c3->bottom.SameAs (this, wxBottom); | |
311 | ||
312 | text_window->SetConstraints(c3); | |
313 | ||
314 | SetAutoLayout(TRUE); | |
315 | } | |
316 | ||
317 | // ---------------------------------------------------------------------------- | |
318 | // MyFlexSizerFrame | |
319 | // ---------------------------------------------------------------------------- | |
320 | ||
321 | void MyFlexSizerFrame::InitFlexSizer(wxFlexGridSizer *sizer) | |
322 | { | |
323 | for ( int i = 0; i < 3; i++ ) | |
324 | { | |
325 | for ( int j = 0; j < 3; j++ ) | |
326 | { | |
327 | sizer->Add(new wxStaticText | |
328 | ( | |
329 | this, | |
330 | -1, | |
331 | wxString::Format(_T("(%d, %d)"), i + 1, j + 1), | |
332 | wxDefaultPosition, | |
333 | wxDefaultSize, | |
334 | wxALIGN_CENTER | |
335 | ), | |
336 | 0, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 3); | |
337 | } | |
338 | } | |
339 | } | |
340 | ||
341 | MyFlexSizerFrame::MyFlexSizerFrame(const wxChar *title, int x, int y ) | |
342 | : wxFrame(NULL, -1, title, wxPoint(x, y) ) | |
343 | { | |
344 | wxFlexGridSizer *sizerFlex; | |
345 | ||
346 | // consttuct the first column | |
347 | wxSizer *sizerCol1 = new wxBoxSizer(wxVERTICAL); | |
348 | sizerCol1->Add(new wxStaticText(this, -1, _T("Ungrowable:")), 0, wxCENTER | wxTOP, 20); | |
349 | sizerFlex = new wxFlexGridSizer(3, 3); | |
350 | InitFlexSizer(sizerFlex); | |
351 | sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10); | |
352 | ||
353 | sizerCol1->Add(new wxStaticText(this, -1, _T("Growable middle column:")), 0, wxCENTER | wxTOP, 20); | |
354 | sizerFlex = new wxFlexGridSizer(3, 3); | |
355 | InitFlexSizer(sizerFlex); | |
356 | sizerFlex->AddGrowableCol(1); | |
357 | sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10); | |
358 | ||
359 | sizerCol1->Add(new wxStaticText(this, -1, _T("Growable middle row:")), 0, wxCENTER | wxTOP, 20); | |
360 | sizerFlex = new wxFlexGridSizer(3, 3); | |
361 | InitFlexSizer(sizerFlex); | |
362 | sizerFlex->AddGrowableRow(1); | |
363 | sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10); | |
364 | ||
365 | sizerCol1->Add(new wxStaticText(this, -1, _T("All growable columns:")), 0, wxCENTER | wxTOP, 20); | |
366 | sizerFlex = new wxFlexGridSizer(3, 3); | |
367 | InitFlexSizer(sizerFlex); | |
368 | sizerFlex->AddGrowableCol(0, 1); | |
369 | sizerFlex->AddGrowableCol(1, 2); | |
370 | sizerFlex->AddGrowableCol(2, 3); | |
371 | sizerCol1->Add(sizerFlex, 1, wxALL | wxEXPAND, 10); | |
372 | ||
373 | // the second one | |
374 | wxSizer *sizerCol2 = new wxBoxSizer(wxVERTICAL); | |
375 | sizerCol2->Add(new wxStaticText(this, -1, _T("Growable middle row and column:")), 0, wxCENTER | wxTOP, 20); | |
376 | sizerFlex = new wxFlexGridSizer(3, 3); | |
377 | InitFlexSizer(sizerFlex); | |
378 | sizerFlex->AddGrowableCol(1); | |
379 | sizerFlex->AddGrowableRow(1); | |
380 | sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10); | |
381 | ||
382 | sizerCol2->Add(new wxStaticText(this, -1, _T("Same with horz flex direction")), 0, wxCENTER | wxTOP, 20); | |
383 | sizerFlex = new wxFlexGridSizer(3, 3); | |
384 | InitFlexSizer(sizerFlex); | |
385 | sizerFlex->AddGrowableCol(1); | |
386 | sizerFlex->AddGrowableRow(1); | |
387 | sizerFlex->SetFlexibleDirection(wxHORIZONTAL); | |
388 | sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10); | |
389 | ||
390 | sizerCol2->Add(new wxStaticText(this, -1, _T("Same with grow mode == \"none\"")), 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_NONE); | |
397 | sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10); | |
398 | ||
399 | sizerCol2->Add(new wxStaticText(this, -1, _T("Same with grow mode == \"all\"")), 0, wxCENTER | wxTOP, 20); | |
400 | sizerFlex = new wxFlexGridSizer(3, 3); | |
401 | InitFlexSizer(sizerFlex); | |
402 | sizerFlex->AddGrowableCol(1); | |
403 | sizerFlex->AddGrowableRow(1); | |
404 | sizerFlex->SetFlexibleDirection(wxHORIZONTAL); | |
405 | sizerFlex->SetNonFlexibleGrowMode(wxFLEX_GROWMODE_ALL); | |
406 | sizerCol2->Add(sizerFlex, 1, wxALL | wxEXPAND, 10); | |
407 | ||
408 | // add both columns to grid sizer | |
409 | wxGridSizer *sizerTop = new wxGridSizer(2, 0, 20); | |
410 | sizerTop->Add(sizerCol1, 1, wxEXPAND); | |
411 | sizerTop->Add(sizerCol2, 1, wxEXPAND); | |
412 | ||
413 | SetSizer(sizerTop); | |
414 | sizerTop->SetSizeHints(this); | |
415 | } | |
416 | ||
417 | // ---------------------------------------------------------------------------- | |
418 | // MySizerDialog | |
419 | // ---------------------------------------------------------------------------- | |
420 | ||
421 | MySizerDialog::MySizerDialog(wxWindow *parent, const wxChar *title) | |
422 | : wxDialog(parent, -1, wxString(title)) | |
423 | { | |
424 | // Begin with first hierarchy: a notebook at the top and | |
425 | // and OK button at the bottom. | |
426 | ||
427 | wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); | |
428 | ||
429 | wxNotebook *notebook = new wxNotebook( this, -1 ); | |
430 | wxNotebookSizer *nbs = new wxNotebookSizer( notebook ); | |
431 | topsizer->Add( nbs, 1, wxGROW ); | |
432 | ||
433 | wxButton *button = new wxButton( this, wxID_OK, _T("OK") ); | |
434 | topsizer->Add( button, 0, wxALIGN_RIGHT | wxALL, 10 ); | |
435 | ||
436 | // First page: one big text ctrl | |
437 | wxTextCtrl *multi = new wxTextCtrl( notebook, -1, _T("TextCtrl."), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE ); | |
438 | notebook->AddPage( multi, _T("Page One") ); | |
439 | ||
440 | // Second page: a text ctrl and a button | |
441 | wxPanel *panel = new wxPanel( notebook, -1 ); | |
442 | notebook->AddPage( panel, _T("Page Two") ); | |
443 | ||
444 | wxSizer *panelsizer = new wxBoxSizer( wxVERTICAL ); | |
445 | ||
446 | wxTextCtrl *text = new wxTextCtrl( panel, -1, _T("TextLine 1."), wxDefaultPosition, wxSize(250,-1) ); | |
447 | panelsizer->Add( text, 0, wxGROW|wxALL, 30 ); | |
448 | text = new wxTextCtrl( panel, -1, _T("TextLine 2."), wxDefaultPosition, wxSize(250,-1) ); | |
449 | panelsizer->Add( text, 0, wxGROW|wxALL, 30 ); | |
450 | wxButton *button2 = new wxButton( panel, -1, _T("Hallo") ); | |
451 | panelsizer->Add( button2, 0, wxALIGN_RIGHT | wxLEFT|wxRIGHT|wxBOTTOM, 30 ); | |
452 | ||
453 | panel->SetAutoLayout( TRUE ); | |
454 | panel->SetSizer( panelsizer ); | |
455 | ||
456 | // Tell dialog to use sizer | |
457 | SetSizer( topsizer ); | |
458 | topsizer->SetSizeHints( this ); | |
459 | } | |
460 | ||
461 | // ---------------------------------------------------------------------------- | |
462 | // MyGridBagSizerFrame | |
463 | // ---------------------------------------------------------------------------- | |
464 | ||
465 | // some simple macros to help make the sample code below more clear | |
466 | #define TEXTCTRL(text) new wxTextCtrl(p, -1, _T(text)) | |
467 | #define MLTEXTCTRL(text) new wxTextCtrl(p, -1, _T(text), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE) | |
468 | #define POS(r, c) wxGBPosition(r,c) | |
469 | #define SPAN(r, c) wxGBSpan(r,c) | |
470 | ||
471 | wxChar* gbsDescription =_T("\ | |
472 | The wxGridBagSizer is similar to the wxFlexGridSizer except the items are explicitly positioned\n\ | |
473 | in a virtual cell of the layout grid, and column or row spanning is allowed. For example, this\n\ | |
474 | static text is positioned at (0,0) and it spans 7 columns."); | |
475 | ||
476 | ||
477 | // Some IDs | |
478 | enum { | |
479 | GBS_HIDE_BTN = 1212, | |
480 | GBS_SHOW_BTN, | |
481 | GBS_MOVE_BTN1, | |
482 | GBS_MOVE_BTN2, | |
483 | ||
484 | GBS_MAX, | |
485 | }; | |
486 | ||
487 | ||
488 | BEGIN_EVENT_TABLE(MyGridBagSizerFrame, wxFrame) | |
489 | EVT_BUTTON( GBS_HIDE_BTN, MyGridBagSizerFrame::OnHideBtn) | |
490 | EVT_BUTTON( GBS_SHOW_BTN, MyGridBagSizerFrame::OnShowBtn) | |
491 | EVT_BUTTON( GBS_MOVE_BTN1, MyGridBagSizerFrame::OnMoveBtn) | |
492 | EVT_BUTTON( GBS_MOVE_BTN2, MyGridBagSizerFrame::OnMoveBtn) | |
493 | END_EVENT_TABLE() | |
494 | ||
495 | ||
496 | MyGridBagSizerFrame::MyGridBagSizerFrame(const wxChar *title, int x, int y ) | |
497 | : wxFrame( NULL, -1, title, wxPoint(x, y) ) | |
498 | { | |
499 | wxPanel* p = new wxPanel(this, -1); | |
500 | m_panel = p; | |
501 | m_gbs = new wxGridBagSizer(); | |
502 | ||
503 | ||
504 | m_gbs->Add( new wxStaticText(p, -1, gbsDescription), | |
505 | POS(0,0), SPAN(1, 7), | |
506 | wxALIGN_CENTER | wxALL, 5); | |
507 | ||
508 | m_gbs->Add( TEXTCTRL("pos(1,0)"), POS(1,0) ); | |
509 | m_gbs->Add( TEXTCTRL("pos(1,1)"), POS(1,1) ); | |
510 | m_gbs->Add( TEXTCTRL("pos(2,0)"), POS(2,0) ); | |
511 | m_gbs->Add( TEXTCTRL("pos(2,1)"), POS(2,1) ); | |
512 | m_gbs->Add( MLTEXTCTRL("pos(3,2), span(1,2)\nthis row and col are growable"), | |
513 | POS(3,2), SPAN(1,2), wxEXPAND ); | |
514 | m_gbs->Add( MLTEXTCTRL("pos(4,3)\nspan(3,1)"), | |
515 | POS(4,3), SPAN(3,1), wxEXPAND ); | |
516 | m_gbs->Add( TEXTCTRL("pos(5,4)"), POS(5,4), wxDefaultSpan, wxEXPAND ); | |
517 | m_gbs->Add( TEXTCTRL("pos(6,5)"), POS(6,5), wxDefaultSpan, wxEXPAND ); | |
518 | m_gbs->Add( TEXTCTRL("pos(7,6)"), POS(7,6) ); | |
519 | ||
520 | //m_gbs->Add( TEXTCTRL("bad position"), POS(4,3) ); // Test for assert | |
521 | //m_gbs->Add( TEXTCTRL("bad position"), POS(5,3) ); // Test for assert | |
522 | ||
523 | ||
524 | m_moveBtn1 = new wxButton(p, GBS_MOVE_BTN1, _T("Move this to (3,6)")); | |
525 | m_moveBtn2 = new wxButton(p, GBS_MOVE_BTN2, _T("Move this to (3,6)")); | |
526 | m_gbs->Add( m_moveBtn1, POS(10,2) ); | |
527 | m_gbs->Add( m_moveBtn2, POS(10,3) ); | |
528 | ||
529 | m_hideBtn = new wxButton(p, GBS_HIDE_BTN, _T("Hide this item -->")); | |
530 | m_gbs->Add(m_hideBtn, POS(12, 3)); | |
531 | ||
532 | m_hideTxt = new wxTextCtrl(p, -1, _T("pos(12,4), size(150, -1)"), | |
533 | wxDefaultPosition, wxSize(150,-1)); | |
534 | m_gbs->Add( m_hideTxt, POS(12,4) ); | |
535 | ||
536 | m_showBtn = new wxButton(p, GBS_SHOW_BTN, _T("<-- Show it again")); | |
537 | m_gbs->Add(m_showBtn, POS(12, 5)); | |
538 | m_showBtn->Disable(); | |
539 | ||
540 | m_gbs->Add(10,10, POS(14,0)); | |
541 | ||
542 | m_gbs->AddGrowableRow(3); | |
543 | m_gbs->AddGrowableCol(2); | |
544 | ||
545 | p->SetSizerAndFit(m_gbs); | |
546 | SetClientSize(p->GetSize()); | |
547 | } | |
548 | ||
549 | ||
550 | void MyGridBagSizerFrame::OnHideBtn(wxCommandEvent&) | |
551 | { | |
552 | m_gbs->Hide(m_hideTxt); | |
553 | m_hideBtn->Disable(); | |
554 | m_showBtn->Enable(); | |
555 | m_gbs->Layout(); | |
556 | } | |
557 | ||
558 | void MyGridBagSizerFrame::OnShowBtn(wxCommandEvent&) | |
559 | { | |
560 | m_gbs->Show(m_hideTxt); | |
561 | m_hideBtn->Enable(); | |
562 | m_showBtn->Disable(); | |
563 | m_gbs->Layout(); | |
564 | } | |
565 | ||
566 | ||
567 | void MyGridBagSizerFrame::OnMoveBtn(wxCommandEvent& event) | |
568 | { | |
569 | wxButton* btn = (wxButton*)event.GetEventObject(); | |
570 | wxGBPosition curPos = m_gbs->GetItemPosition(btn); | |
571 | ||
572 | // if it's already at the "other" spot then move it back | |
573 | if (curPos == wxGBPosition(3,6)) | |
574 | { | |
575 | m_gbs->SetItemPosition(btn, m_lastPos); | |
576 | btn->SetLabel(_T("Move this to (3,6)")); | |
577 | } | |
578 | else | |
579 | { | |
580 | if ( m_gbs->CheckForIntersection(wxGBPosition(3,6), wxGBSpan(1,1)) ) | |
581 | wxMessageBox( | |
582 | _T("wxGridBagSizer will not allow items to be in the same cell as\n\ | |
583 | another item, so this operation will fail. You will also get an assert\n\ | |
584 | when compiled in debug mode."), _T("Warning"), wxOK | wxICON_INFORMATION); | |
585 | ||
586 | if ( m_gbs->SetItemPosition(btn, wxGBPosition(3,6)) ) | |
587 | { | |
588 | m_lastPos = curPos; | |
589 | btn->SetLabel(_T("Move it back")); | |
590 | } | |
591 | } | |
592 | m_gbs->Layout(); | |
593 | } |