]> git.saurik.com Git - wxWidgets.git/blob - samples/layout/layout.cpp
wxMGL support
[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
29 #include "wx/sizer.h"
30 #include "wx/statline.h"
31 #include "wx/notebook.h"
32
33 #include "layout.h"
34
35 // Declare two frames
36 MyFrame *frame = (MyFrame *) NULL;
37 wxMenuBar *menu_bar = (wxMenuBar *) NULL;
38
39 IMPLEMENT_APP(MyApp)
40
41 MyApp::MyApp()
42 {
43 }
44
45 bool MyApp::OnInit()
46 {
47 // Create the main frame window
48 frame = new MyFrame((MyFrame *) NULL, (char *) "wxWindows Layout Demo", 0, 0, 550, 500);
49
50 frame->SetAutoLayout(TRUE);
51
52 // Give it a status line
53 frame->CreateStatusBar(2);
54
55 // Make a menubar
56 wxMenu *file_menu = new wxMenu;
57
58 file_menu->Append(LAYOUT_LOAD_FILE, "&Load file", "Load a text file");
59 file_menu->Append(LAYOUT_TEST_SIZER, "&Test sizers", "Test sizer");
60 file_menu->Append(LAYOUT_TEST_NB, "&Test notebook sizers", "Test notebook sizer");
61
62 file_menu->AppendSeparator();
63 file_menu->Append(LAYOUT_QUIT, "E&xit", "Quit program");
64
65 wxMenu *help_menu = new wxMenu;
66 help_menu->Append(LAYOUT_ABOUT, "&About", "About layout demo");
67
68 menu_bar = new wxMenuBar;
69
70 menu_bar->Append(file_menu, "&File");
71 menu_bar->Append(help_menu, "&Help");
72
73 // Associate the menu bar with the frame
74 frame->SetMenuBar(menu_bar);
75
76 // Make a panel
77 frame->panel = new wxPanel(frame, 0, 0, 1000, 500, wxTAB_TRAVERSAL);
78 frame->panel->SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
79 // frame->panel->SetAutoLayout(TRUE);
80
81 // Create some panel items
82 wxButton *btn1 = new wxButton(frame->panel, -1, "A button (1)") ;
83
84 wxLayoutConstraints *b1 = new wxLayoutConstraints;
85 b1->centreX.SameAs (frame->panel, wxCentreX);
86 b1->top.SameAs (frame->panel, wxTop, 5);
87 b1->width.PercentOf (frame->panel, wxWidth, 80);
88 b1->height.PercentOf (frame->panel, wxHeight, 10);
89 btn1->SetConstraints(b1);
90
91 wxListBox *list = new wxListBox(frame->panel, -1,
92 wxPoint(-1, -1), wxSize(200, 100));
93 list->Append("Apple");
94 list->Append("Pear");
95 list->Append("Orange");
96 list->Append("Banana");
97 list->Append("Fruit");
98
99 wxLayoutConstraints *b2 = new wxLayoutConstraints;
100 b2->top.Below (btn1, 5);
101 b2->left.SameAs (frame->panel, wxLeft, 5);
102 b2->width.PercentOf (frame->panel, wxWidth, 40);
103 b2->bottom.SameAs (frame->panel, wxBottom, 5);
104 list->SetConstraints(b2);
105
106 wxTextCtrl *mtext = new wxTextCtrl(frame->panel, -1, "Some text",
107 wxPoint(-1, -1), wxSize(150, 100));
108
109 wxLayoutConstraints *b3 = new wxLayoutConstraints;
110 b3->top.Below (btn1, 5);
111 b3->left.RightOf (list, 5);
112 b3->right.SameAs (frame->panel, wxRight, 5);
113 b3->bottom.SameAs (frame->panel, wxBottom, 5);
114 mtext->SetConstraints(b3);
115
116 frame->canvas = new MyWindow(frame, 0, 0, 400, 400, wxRETAINED);
117
118 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
119 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
120
121 // Make a text window
122 frame->text_window = new MyTextWindow(frame, 0, 250, 400, 250);
123
124 // Set constraints for panel subwindow
125 wxLayoutConstraints *c1 = new wxLayoutConstraints;
126
127 c1->left.SameAs (frame, wxLeft);
128 c1->top.SameAs (frame, wxTop);
129 c1->right.PercentOf (frame, wxWidth, 50);
130 c1->height.PercentOf (frame, wxHeight, 50);
131
132 frame->panel->SetConstraints(c1);
133
134 // Set constraints for canvas subwindow
135 wxLayoutConstraints *c2 = new wxLayoutConstraints;
136
137 c2->left.SameAs (frame->panel, wxRight);
138 c2->top.SameAs (frame, wxTop);
139 c2->right.SameAs (frame, wxRight);
140 c2->height.PercentOf (frame, wxHeight, 50);
141
142 frame->canvas->SetConstraints(c2);
143
144 // Set constraints for text subwindow
145 wxLayoutConstraints *c3 = new wxLayoutConstraints;
146 c3->left.SameAs (frame, wxLeft);
147 c3->top.Below (frame->panel);
148 c3->right.SameAs (frame, wxRight);
149 c3->bottom.SameAs (frame, wxBottom);
150
151 frame->text_window->SetConstraints(c3);
152
153 frame->Show(TRUE);
154
155 frame->SetStatusText("wxWindows layout demo");
156
157 SetTopWindow(frame);
158 return TRUE;
159 }
160
161 //-----------------------------------------------------------------
162 // MyFrame
163 //-----------------------------------------------------------------
164
165 // Define my frame constructor
166 MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h):
167 wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
168 {
169 panel = (wxPanel *) NULL;
170 text_window = (MyTextWindow *) NULL;
171 canvas = (MyWindow *) NULL;
172 }
173
174 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
175 EVT_MENU(LAYOUT_LOAD_FILE, MyFrame::LoadFile)
176 EVT_MENU(LAYOUT_QUIT, MyFrame::Quit)
177 EVT_MENU(LAYOUT_TEST_SIZER, MyFrame::TestSizers)
178 EVT_MENU(LAYOUT_TEST_NB, MyFrame::TestNotebookSizers)
179 EVT_MENU(LAYOUT_ABOUT, MyFrame::About)
180 EVT_SIZE(MyFrame::OnSize)
181 END_EVENT_TABLE()
182
183 void MyFrame::LoadFile(wxCommandEvent& WXUNUSED(event) )
184 {
185 wxString s = wxFileSelector( _T("Load text file"), (const wxChar *) NULL,
186 (const wxChar *) NULL, (const wxChar *) NULL, _T("*.txt") );
187 if (s != "")
188 {
189 #ifdef __WXMSW__
190 frame->text_window->LoadFile(s);
191 #endif
192 }
193 }
194
195 void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
196 {
197 this->Close(TRUE);
198 }
199
200 void MyFrame::TestSizers(wxCommandEvent& WXUNUSED(event) )
201 {
202 MySizerFrame *newFrame = new MySizerFrame((MyFrame *) NULL, "Sizer Test Frame", 50, 50 );
203 newFrame->Show(TRUE);
204 }
205
206 void MyFrame::TestNotebookSizers(wxCommandEvent& WXUNUSED(event) )
207 {
208 wxDialog dialog( this, -1, wxString("Notebook Sizer Test Dialog") );
209
210 // Begin with first hierarchy: a notebook at the top and
211 // and OK button at the bottom.
212
213 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
214
215 wxNotebook *notebook = new wxNotebook( &dialog, -1 );
216 wxNotebookSizer *nbs = new wxNotebookSizer( notebook );
217 topsizer->Add( nbs, 1, wxGROW );
218
219 wxButton *button = new wxButton( &dialog, wxID_OK, "OK" );
220 topsizer->Add( button, 0, wxALIGN_RIGHT | wxALL, 10 );
221
222 // First page: one big text ctrl
223 wxTextCtrl *multi = new wxTextCtrl( notebook, -1, "TextCtrl.", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
224 notebook->AddPage( multi, "Page One" );
225
226 // Second page: a text ctrl and a button
227 wxPanel *panel = new wxPanel( notebook, -1 );
228 notebook->AddPage( panel, "Page Two" );
229
230 wxSizer *panelsizer = new wxBoxSizer( wxVERTICAL );
231
232 wxTextCtrl *text = new wxTextCtrl( panel, -1, "TextLine 1.", wxDefaultPosition, wxSize(250,-1) );
233 panelsizer->Add( text, 0, wxGROW|wxALL, 30 );
234 text = new wxTextCtrl( panel, -1, "TextLine 2.", wxDefaultPosition, wxSize(250,-1) );
235 panelsizer->Add( text, 0, wxGROW|wxALL, 30 );
236 wxButton *button2 = new wxButton( panel, -1, "Hallo" );
237 panelsizer->Add( button2, 0, wxALIGN_RIGHT | wxLEFT|wxRIGHT|wxBOTTOM, 30 );
238
239 panel->SetAutoLayout( TRUE );
240 panel->SetSizer( panelsizer );
241
242 // Tell dialog to use sizer
243
244 dialog.SetAutoLayout( TRUE );
245 dialog.SetSizer( topsizer );
246 topsizer->Fit( &dialog );
247 topsizer->SetSizeHints( &dialog );
248
249 dialog.ShowModal();
250 }
251
252
253 void MyFrame::About(wxCommandEvent& WXUNUSED(event) )
254 {
255 (void)wxMessageBox("wxWindows GUI library layout demo\n",
256 "About Layout Demo", wxOK|wxCENTRE);
257 }
258
259 // Size the subwindows when the frame is resized
260 void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event) )
261 {
262 Layout();
263 }
264
265 void MyFrame::Draw(wxDC& dc, bool WXUNUSED(draw_bitmaps) )
266 {
267 dc.SetPen(* wxGREEN_PEN);
268 dc.DrawLine(0, 0, 200, 200);
269 dc.DrawLine(200, 0, 0, 200);
270
271 dc.SetBrush(* wxCYAN_BRUSH);
272 dc.SetPen(* wxRED_PEN);
273
274 dc.DrawRectangle(100, 100, 100, 50);
275 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
276
277 dc.DrawEllipse(250, 250, 100, 50);
278 #if wxUSE_SPLINES
279 dc.DrawSpline(50, 200, 50, 100, 200, 10);
280 #endif // wxUSE_SPLINES
281 dc.DrawLine(50, 230, 200, 230);
282
283 dc.SetPen(* wxBLACK_PEN);
284 dc.DrawArc(50, 300, 100, 250, 100, 300 );
285 }
286
287 //-----------------------------------------------------------------
288 // MyWindow
289 //-----------------------------------------------------------------
290
291 BEGIN_EVENT_TABLE(MyWindow, wxWindow)
292 EVT_PAINT(MyWindow::OnPaint)
293 END_EVENT_TABLE()
294
295 // Define a constructor for my canvas
296 MyWindow::MyWindow(wxFrame *frame, int x, int y, int w, int h, long style):
297 wxWindow(frame, -1, wxPoint(x, y), wxSize(w, h), style)
298 {
299 }
300
301 MyWindow::~MyWindow()
302 {
303 }
304
305 // Define the repainting behaviour
306 void MyWindow::OnPaint(wxPaintEvent& WXUNUSED(event) )
307 {
308 wxPaintDC dc(this);
309 frame->Draw(dc,TRUE);
310 }
311
312 //-----------------------------------------------------------------
313 // MySizerFrame
314 //-----------------------------------------------------------------
315
316 MySizerFrame::MySizerFrame(wxFrame *frame, char *title, int x, int y ):
317 wxFrame(frame, -1, title, wxPoint(x, y) )
318 {
319 // we want to get a dialog that is stretchable because it
320 // has a text ctrl in the middle. at the bottom, we have
321 // two buttons which.
322
323 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
324
325 // 1) top: create wxStaticText with minimum size equal to its default size
326 topsizer->Add(
327 new wxStaticText( this, -1, "An explanation (wxALIGN_RIGHT)." ),
328 0, // make vertically unstretchable
329 wxALIGN_RIGHT | // right align text
330 wxTOP | wxLEFT | wxRIGHT, // make border all around except wxBOTTOM
331 5 ); // set border width to 5
332
333 // 2) top: create wxTextCtrl with minimum size (100x60)
334 topsizer->Add(
335 new wxTextCtrl( this, -1, "My text (wxEXPAND).", wxDefaultPosition, wxSize(100,60), wxTE_MULTILINE),
336 1, // make vertically stretchable
337 wxEXPAND | // make horizontally stretchable
338 wxALL, // and make border all around
339 5 ); // set border width to 5
340
341 // 2.5) Gratuitous test of wxStaticBoxSizers
342 wxBoxSizer *statsizer = new wxStaticBoxSizer(
343 new wxStaticBox(this, -1, "A wxStaticBoxSizer"),
344 wxVERTICAL );
345 statsizer->Add(
346 new wxStaticText(this, -1, "And some TEXT inside it"),
347 0,
348 wxCENTER |
349 wxALL,
350 30);
351 topsizer->Add(statsizer, 1, wxEXPAND | wxALL, 10);
352
353 // 2.7) And a test of wxGridSizer
354 wxGridSizer *gridsizer = new wxGridSizer(2, 5, 5);
355 gridsizer->Add(new wxStaticText(this, -1, "Label"), 0,
356 wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
357 gridsizer->Add(new wxTextCtrl(this, -1, "Grid sizer demo"), 1,
358 wxGROW | wxALIGN_CENTER_VERTICAL);
359 gridsizer->Add(new wxStaticText(this, -1, "Another label"), 0,
360 wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
361 gridsizer->Add(new wxTextCtrl(this, -1, "More text"), 1,
362 wxGROW | wxALIGN_CENTER_VERTICAL);
363 gridsizer->Add(new wxStaticText(this, -1, "Final label"), 0,
364 wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
365 gridsizer->Add(new wxTextCtrl(this, -1, "And yet more text"), 1,
366 wxGROW | wxALIGN_CENTER_VERTICAL);
367 topsizer->Add(gridsizer, 1, wxGROW | wxALL, 10);
368
369
370 // 3) middle: create wxStaticLine with minimum size (3x3)
371 topsizer->Add(
372 new wxStaticLine( this, -1, wxDefaultPosition, wxSize(3,3), wxHORIZONTAL),
373 0, // make vertically unstretchable
374 wxEXPAND | // make horizontally stretchable
375 wxALL, // and make border all around
376 5 ); // set border width to 5
377
378
379 // 4) bottom: create two centred wxButtons
380 wxBoxSizer *button_box = new wxBoxSizer( wxHORIZONTAL );
381 button_box->Add(
382 new wxButton( this, -1, "Two buttons in a box" ),
383 0, // make horizontally unstretchable
384 wxALL, // make border all around
385 7 ); // set border width to 7
386 button_box->Add(
387 new wxButton( this, -1, "(wxCENTER)" ),
388 0, // make horizontally unstretchable
389 wxALL, // make border all around
390 7 ); // set border width to 7
391
392 topsizer->Add(
393 button_box,
394 0, // make vertically unstretchable
395 wxCENTER ); // no border and centre horizontally
396
397 SetAutoLayout( TRUE );
398
399 // set frame to minimum size
400 topsizer->Fit( this );
401
402 // don't allow frame to get smaller than what the sizers tell ye
403 topsizer->SetSizeHints( this );
404
405 SetSizer( topsizer );
406 }
407
408
409