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