Added wxNewSizer and wxBorderNewSizer with sample
[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 and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #if !wxUSE_CONSTRAINTS
24 #error You must set wxUSE_CONSTRAINTS to 1 in setup.h!
25 #endif
26
27 #include <ctype.h>
28 #include "wx/sizer.h"
29
30 #include "layout.h"
31
32 // Declare two frames
33 MyFrame *frame = (MyFrame *) NULL;
34 wxMenuBar *menu_bar = (wxMenuBar *) NULL;
35
36 IMPLEMENT_APP(MyApp)
37
38 MyApp::MyApp()
39 {
40 }
41
42 bool MyApp::OnInit(void)
43 {
44 // Create the main frame window
45 frame = new MyFrame((MyFrame *) NULL, (char *) "wxWindows Layout Demo", 0, 0, 550, 500);
46
47 frame->SetAutoLayout(TRUE);
48
49 // Give it a status line
50 frame->CreateStatusBar(2);
51
52 // Make a menubar
53 wxMenu *file_menu = new wxMenu;
54
55 file_menu->Append(LAYOUT_LOAD_FILE, "&Load file", "Load a text file");
56 file_menu->Append(LAYOUT_TEST, "&Test sizers", "Test sizer code");
57 file_menu->Append(LAYOUT_TEST_NEW, "&Test new sizers", "Test new sizer code");
58
59 file_menu->AppendSeparator();
60 file_menu->Append(LAYOUT_QUIT, "E&xit", "Quit program");
61
62 wxMenu *help_menu = new wxMenu;
63 help_menu->Append(LAYOUT_ABOUT, "&About", "About layout demo");
64
65 menu_bar = new wxMenuBar;
66
67 menu_bar->Append(file_menu, "&File");
68 menu_bar->Append(help_menu, "&Help");
69
70 // Associate the menu bar with the frame
71 frame->SetMenuBar(menu_bar);
72
73 // Make a panel
74 frame->panel = new wxPanel(frame, 0, 0, 1000, 500, wxTAB_TRAVERSAL);
75 frame->panel->SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
76 // frame->panel->SetAutoLayout(TRUE);
77
78 // Create some panel items
79 wxButton *btn1 = new wxButton(frame->panel, -1, "A button (1)") ;
80
81 wxLayoutConstraints *b1 = new wxLayoutConstraints;
82 b1->centreX.SameAs (frame->panel, wxCentreX);
83 b1->top.SameAs (frame->panel, wxTop, 5);
84 b1->width.PercentOf (frame->panel, wxWidth, 80);
85 b1->height.PercentOf (frame->panel, wxHeight, 10);
86 btn1->SetConstraints(b1);
87
88 wxListBox *list = new wxListBox(frame->panel, -1,
89 wxPoint(-1, -1), wxSize(200, 100));
90 list->Append("Apple");
91 list->Append("Pear");
92 list->Append("Orange");
93 list->Append("Banana");
94 list->Append("Fruit");
95
96 wxLayoutConstraints *b2 = new wxLayoutConstraints;
97 b2->top.Below (btn1, 5);
98 b2->left.SameAs (frame->panel, wxLeft, 5);
99 b2->width.PercentOf (frame->panel, wxWidth, 40);
100 b2->bottom.SameAs (frame->panel, wxBottom, 5);
101 list->SetConstraints(b2);
102
103 wxTextCtrl *mtext = new wxTextCtrl(frame->panel, -1, "Some text",
104 wxPoint(-1, -1), wxSize(150, 100));
105
106 wxLayoutConstraints *b3 = new wxLayoutConstraints;
107 b3->top.Below (btn1, 5);
108 b3->left.RightOf (list, 5);
109 b3->right.SameAs (frame->panel, wxRight, 5);
110 b3->bottom.SameAs (frame->panel, wxBottom, 5);
111 mtext->SetConstraints(b3);
112
113 frame->canvas = new MyWindow(frame, 0, 0, 400, 400, wxRETAINED);
114
115 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
116 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
117
118 // Make a text window
119 frame->text_window = new MyTextWindow(frame, 0, 250, 400, 250);
120
121 // Set constraints for panel subwindow
122 wxLayoutConstraints *c1 = new wxLayoutConstraints;
123
124 c1->left.SameAs (frame, wxLeft);
125 c1->top.SameAs (frame, wxTop);
126 c1->right.PercentOf (frame, wxWidth, 50);
127 c1->height.PercentOf (frame, wxHeight, 50);
128
129 frame->panel->SetConstraints(c1);
130
131 // Set constraints for canvas subwindow
132 wxLayoutConstraints *c2 = new wxLayoutConstraints;
133
134 c2->left.SameAs (frame->panel, wxRight);
135 c2->top.SameAs (frame, wxTop);
136 c2->right.SameAs (frame, wxRight);
137 c2->height.PercentOf (frame, wxHeight, 50);
138
139 frame->canvas->SetConstraints(c2);
140
141 // Set constraints for text subwindow
142 wxLayoutConstraints *c3 = new wxLayoutConstraints;
143 c3->left.SameAs (frame, wxLeft);
144 c3->top.Below (frame->panel);
145 c3->right.SameAs (frame, wxRight);
146 c3->bottom.SameAs (frame, wxBottom);
147
148 frame->text_window->SetConstraints(c3);
149
150 frame->Show(TRUE);
151
152 frame->SetStatusText("wxWindows layout demo");
153
154 SetTopWindow(frame);
155 return TRUE;
156 }
157
158 //-----------------------------------------------------------------
159 // MyFrame
160 //-----------------------------------------------------------------
161
162 // Define my frame constructor
163 MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h):
164 wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
165 {
166 panel = (wxPanel *) NULL;
167 text_window = (MyTextWindow *) NULL;
168 canvas = (MyWindow *) NULL;
169 }
170
171 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
172 EVT_MENU(LAYOUT_LOAD_FILE, MyFrame::LoadFile)
173 EVT_MENU(LAYOUT_QUIT, MyFrame::Quit)
174 EVT_MENU(LAYOUT_TEST, MyFrame::TestSizers)
175 EVT_MENU(LAYOUT_TEST_NEW, MyFrame::TestNewSizers)
176 EVT_MENU(LAYOUT_ABOUT, MyFrame::About)
177 EVT_SIZE(MyFrame::OnSize)
178 END_EVENT_TABLE()
179
180 void MyFrame::LoadFile(wxCommandEvent& WXUNUSED(event) )
181 {
182 wxString s = wxFileSelector( _T("Load text file"), (const wxChar *) NULL,
183 (const wxChar *) NULL, (const wxChar *) NULL, _T("*.txt") );
184 if (s != "")
185 {
186 #ifdef __WXMSW__
187 frame->text_window->LoadFile(s);
188 #endif
189 }
190 }
191
192 void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
193 {
194 this->Close(TRUE);
195 }
196
197 void MyFrame::TestSizers(wxCommandEvent& WXUNUSED(event) )
198 {
199 SizerFrame *newFrame = new SizerFrame((MyFrame *) NULL, "Sizer Test Frame", 50, 50, 500, 500);
200 newFrame->Show(TRUE);
201 }
202
203 void MyFrame::TestNewSizers(wxCommandEvent& WXUNUSED(event) )
204 {
205 NewSizerFrame *newFrame = new NewSizerFrame((MyFrame *) NULL, "Sizer Test Frame", 50, 50 );
206 newFrame->Show(TRUE);
207 }
208
209 void MyFrame::About(wxCommandEvent& WXUNUSED(event) )
210 {
211 (void)wxMessageBox("wxWindows GUI library layout demo\n",
212 "About Layout Demo", wxOK|wxCENTRE);
213 }
214
215 // Size the subwindows when the frame is resized
216 void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event) )
217 {
218 Layout();
219 }
220
221 void MyFrame::Draw(wxDC& dc, bool WXUNUSED(draw_bitmaps) )
222 {
223 dc.SetPen(* wxGREEN_PEN);
224 dc.DrawLine(0, 0, 200, 200);
225 dc.DrawLine(200, 0, 0, 200);
226
227 dc.SetBrush(* wxCYAN_BRUSH);
228 dc.SetPen(* wxRED_PEN);
229
230 dc.DrawRectangle(100, 100, 100, 50);
231 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
232
233 dc.DrawEllipse(250, 250, 100, 50);
234 dc.DrawSpline(50, 200, 50, 100, 200, 10);
235 dc.DrawLine(50, 230, 200, 230);
236
237 dc.SetPen(* wxBLACK_PEN);
238 dc.DrawArc(50, 300, 100, 250, 100, 300 );
239 }
240
241 //-----------------------------------------------------------------
242 // MyWindow
243 //-----------------------------------------------------------------
244
245 BEGIN_EVENT_TABLE(MyWindow, wxWindow)
246 EVT_PAINT(MyWindow::OnPaint)
247 END_EVENT_TABLE()
248
249 // Define a constructor for my canvas
250 MyWindow::MyWindow(wxFrame *frame, int x, int y, int w, int h, long style):
251 wxWindow(frame, -1, wxPoint(x, y), wxSize(w, h), style)
252 {
253 }
254
255 MyWindow::~MyWindow(void)
256 {
257 }
258
259 // Define the repainting behaviour
260 void MyWindow::OnPaint(wxPaintEvent& WXUNUSED(event) )
261 {
262 wxPaintDC dc(this);
263 frame->Draw(dc,TRUE);
264 }
265
266 //-----------------------------------------------------------------
267 // SizerFrame
268 //-----------------------------------------------------------------
269
270 SizerFrame::SizerFrame(wxFrame *frame, char *title, int x, int y, int w, int h):
271 wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
272 {
273 panel = new wxPanel(this, -1, wxPoint(0, 0), wxSize(-1, -1), wxTAB_TRAVERSAL);
274 panel->SetBackgroundColour(wxColour(192, 192, 192));
275
276 // A sizer to fit the whole panel, plus two sizers, one
277 // above the other. A button is centred on the lower
278 // sizer; a rowcol containing 3 buttons is centred on the upper
279 // sizer.
280 wxSizer *expandSizer = new wxSizer(panel, wxSizerExpand);
281 expandSizer->SetName("expandSizer");
282
283 wxLayoutConstraints *c;
284
285 /////// TOP OF PANEL
286 ///////
287 wxSizer *topSizer = new wxSizer(expandSizer);
288 topSizer->SetName("topSizer");
289
290 // Specify constraints for the top sizer
291 c = new wxLayoutConstraints;
292 c->left.SameAs (expandSizer, wxLeft);
293 c->top.SameAs (expandSizer, wxTop);
294 c->right.SameAs (expandSizer, wxRight);
295 c->height.PercentOf (expandSizer, wxHeight, 50);
296
297 topSizer->SetConstraints(c);
298
299 /*
300 * Add a row-col sizer and some buttons
301 */
302
303 // Default is layout by rows, 20 columns per row, shrink to fit.
304 wxRowColSizer *rowCol = new wxRowColSizer(topSizer);
305 rowCol->SetName("rowCol");
306
307 wxButton *button = new wxButton(panel, -1, "Button 1");
308 rowCol->AddSizerChild(button);
309
310 button = new wxButton(panel, -1, "Button 2");
311 rowCol->AddSizerChild(button);
312
313 button = new wxButton(panel, -1, "Button 3");
314 rowCol->AddSizerChild(button);
315
316 // Centre the rowcol in the middle of the upper sizer
317 c = new wxLayoutConstraints;
318 c->centreX.SameAs (topSizer, wxCentreX);
319 c->centreY.SameAs (topSizer, wxCentreY);
320 c->width.AsIs();
321 c->height.AsIs();
322 rowCol->SetConstraints(c);
323
324 /////// BOTTOM OF PANEL
325 ///////
326 wxSizer *bottomSizer = new wxSizer(expandSizer);
327
328 // Specify constraints for the bottom sizer
329 c = new wxLayoutConstraints;
330 c->left.SameAs (expandSizer, wxLeft);
331 c->top.PercentOf (expandSizer, wxHeight, 50);
332 c->right.SameAs (expandSizer, wxRight);
333 c->height.PercentOf (expandSizer, wxHeight, 50);
334
335 bottomSizer->SetConstraints(c);
336
337 wxButton *button2 = new wxButton(panel, -1, "Test button");
338
339 // The button should be a child of the bottom sizer
340 bottomSizer->AddSizerChild(button2);
341
342 // Centre the button on the sizer
343 c = new wxLayoutConstraints;
344 c->centreX.SameAs (bottomSizer, wxCentreX);
345 c->centreY.SameAs (bottomSizer, wxCentreY);
346 c->width.PercentOf (bottomSizer, wxWidth, 20);
347 c->height.PercentOf (bottomSizer, wxHeight, 20);
348 button2->SetConstraints(c);
349 }
350
351 BEGIN_EVENT_TABLE(SizerFrame, wxFrame)
352 EVT_SIZE(SizerFrame::OnSize)
353 END_EVENT_TABLE()
354
355
356 // Size the subwindows when the frame is resized
357 void SizerFrame::OnSize(wxSizeEvent& event)
358 {
359 wxFrame::OnSize(event);
360 panel->Layout();
361 }
362
363 //-----------------------------------------------------------------
364 // NewSizerFrame
365 //-----------------------------------------------------------------
366
367 NewSizerFrame::NewSizerFrame(wxFrame *frame, char *title, int x, int y ):
368 wxFrame(frame, -1, title, wxPoint(x, y) )
369 {
370 // no extra options means border all around
371 topsizer = new wxBorderNewSizer();
372
373 // make border 20 pixels wide
374 topsizer->Add( new wxButton( this, -1, "Hello" ), 20 );
375
376 // set frame to minimum size
377 topsizer->Fit( this );
378
379 // don't allow frame to get smaller than what the sizers tell ye
380 topsizer->SetSizeHints( this );
381
382 // layout widgets
383 topsizer->Layout();
384 }
385
386 // This can later be removed if we integrate wxNewSizers
387 // into wxWindows
388
389 BEGIN_EVENT_TABLE(NewSizerFrame, wxFrame)
390 EVT_SIZE(NewSizerFrame::OnSize)
391 END_EVENT_TABLE()
392
393 void NewSizerFrame::OnSize(wxSizeEvent& event)
394 {
395 wxFrame::OnSize(event);
396
397 wxSize client_size( GetClientSize() );
398
399 topsizer->SetDimension( 0, 0, client_size.x, client_size.y );
400 }
401
402