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