]> git.saurik.com Git - wxWidgets.git/blame - samples/layout/layout.cpp
added missing methods/removed duplicated base methods
[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
e4b19d9b 23#if !wxUSE_CONSTRAINTS
ad813b00 24#error You must set wxUSE_CONSTRAINTS to 1 in setup.h!
c801d85f
KB
25#endif
26
27#include <ctype.h>
83edc0a5 28
c62ac5b6 29#include "wx/sizer.h"
61d514bb 30#include "wx/statline.h"
83edc0a5 31#include "wx/notebook.h"
c62ac5b6 32
c801d85f
KB
33#include "layout.h"
34
35// Declare two frames
c67daf87
UR
36MyFrame *frame = (MyFrame *) NULL;
37wxMenuBar *menu_bar = (wxMenuBar *) NULL;
c801d85f
KB
38
39IMPLEMENT_APP(MyApp)
40
c801d85f
KB
41MyApp::MyApp()
42{
43}
44
83edc0a5 45bool MyApp::OnInit()
c801d85f
KB
46{
47 // Create the main frame window
c67daf87 48 frame = new MyFrame((MyFrame *) NULL, (char *) "wxWindows Layout Demo", 0, 0, 550, 500);
c801d85f
KB
49
50 frame->SetAutoLayout(TRUE);
51
52 // Give it a status line
53 frame->CreateStatusBar(2);
54
c801d85f
KB
55 // Make a menubar
56 wxMenu *file_menu = new wxMenu;
57
58 file_menu->Append(LAYOUT_LOAD_FILE, "&Load file", "Load a text file");
83edc0a5
RR
59 file_menu->Append(LAYOUT_TEST_SIZER, "&Test sizers", "Test sizer");
60 file_menu->Append(LAYOUT_TEST_NB, "&Test notebook sizers", "Test notebook sizer");
c801d85f
KB
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);
17b0d794 78 frame->panel->SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
c801d85f
KB
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
c62ac5b6
RR
161//-----------------------------------------------------------------
162// MyFrame
163//-----------------------------------------------------------------
164
c801d85f
KB
165// Define my frame constructor
166MyFrame::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{
c67daf87
UR
169 panel = (wxPanel *) NULL;
170 text_window = (MyTextWindow *) NULL;
171 canvas = (MyWindow *) NULL;
c801d85f
KB
172}
173
174BEGIN_EVENT_TABLE(MyFrame, wxFrame)
175 EVT_MENU(LAYOUT_LOAD_FILE, MyFrame::LoadFile)
176 EVT_MENU(LAYOUT_QUIT, MyFrame::Quit)
83edc0a5
RR
177 EVT_MENU(LAYOUT_TEST_SIZER, MyFrame::TestSizers)
178 EVT_MENU(LAYOUT_TEST_NB, MyFrame::TestNotebookSizers)
c801d85f
KB
179 EVT_MENU(LAYOUT_ABOUT, MyFrame::About)
180 EVT_SIZE(MyFrame::OnSize)
181END_EVENT_TABLE()
182
bd7d06f2 183void MyFrame::LoadFile(wxCommandEvent& WXUNUSED(event) )
c801d85f 184{
56ac3e75 185 wxString s = wxFileSelector( _T("Load text file"), (const wxChar *) NULL,
ddb6bc71 186 (const wxChar *) NULL, (const wxChar *) NULL, _T("*.txt") );
227869da 187 if (s != "")
c801d85f 188 {
2049ba38 189#ifdef __WXMSW__
c801d85f
KB
190 frame->text_window->LoadFile(s);
191#endif
192 }
193}
194
bd7d06f2 195void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
c801d85f 196{
83edc0a5 197 this->Close(TRUE);
c801d85f
KB
198}
199
83edc0a5 200void MyFrame::TestSizers(wxCommandEvent& WXUNUSED(event) )
c62ac5b6 201{
83edc0a5
RR
202 MySizerFrame *newFrame = new MySizerFrame((MyFrame *) NULL, "Sizer Test Frame", 50, 50 );
203 newFrame->Show(TRUE);
c801d85f
KB
204}
205
83edc0a5
RR
206void MyFrame::TestNotebookSizers(wxCommandEvent& WXUNUSED(event) )
207{
56ac3e75 208 wxDialog dialog( this, -1, wxString("Notebook Sizer Test Dialog") );
83edc0a5
RR
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 );
56ac3e75 214
83edc0a5
RR
215 wxNotebook *notebook = new wxNotebook( &dialog, -1 );
216 wxNotebookSizer *nbs = new wxNotebookSizer( notebook );
217 topsizer->Add( nbs, 1, wxGROW );
56ac3e75 218
83edc0a5
RR
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" );
56ac3e75 225
83edc0a5
RR
226 // Second page: a text ctrl and a button
227 wxPanel *panel = new wxPanel( notebook, -1 );
228 notebook->AddPage( panel, "Page Two" );
56ac3e75 229
f6bcfd97 230 wxSizer *panelsizer = new wxBoxSizer( wxVERTICAL );
56ac3e75 231
83edc0a5
RR
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 );
56ac3e75 238
83edc0a5
RR
239 panel->SetAutoLayout( TRUE );
240 panel->SetSizer( panelsizer );
56ac3e75 241
83edc0a5 242 // Tell dialog to use sizer
56ac3e75 243
83edc0a5 244 dialog.SetAutoLayout( TRUE );
83edc0a5 245 dialog.SetSizer( topsizer );
56ac3e75
RD
246 topsizer->Fit( &dialog );
247 topsizer->SetSizeHints( &dialog );
248
83edc0a5
RR
249 dialog.ShowModal();
250}
251
252
bd7d06f2 253void MyFrame::About(wxCommandEvent& WXUNUSED(event) )
c801d85f 254{
83edc0a5 255 (void)wxMessageBox("wxWindows GUI library layout demo\n",
c801d85f
KB
256 "About Layout Demo", wxOK|wxCENTRE);
257}
258
259// Size the subwindows when the frame is resized
bd7d06f2 260void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event) )
c801d85f
KB
261{
262 Layout();
263}
264
bd7d06f2 265void MyFrame::Draw(wxDC& dc, bool WXUNUSED(draw_bitmaps) )
c801d85f 266{
c0ed460c 267 dc.SetPen(* wxGREEN_PEN);
bd7d06f2
RR
268 dc.DrawLine(0, 0, 200, 200);
269 dc.DrawLine(200, 0, 0, 200);
c801d85f 270
c0ed460c
JS
271 dc.SetBrush(* wxCYAN_BRUSH);
272 dc.SetPen(* wxRED_PEN);
c801d85f 273
bd7d06f2
RR
274 dc.DrawRectangle(100, 100, 100, 50);
275 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
c801d85f 276
bd7d06f2 277 dc.DrawEllipse(250, 250, 100, 50);
d93c719a 278#if wxUSE_SPLINES
bd7d06f2 279 dc.DrawSpline(50, 200, 50, 100, 200, 10);
d93c719a 280#endif // wxUSE_SPLINES
bd7d06f2 281 dc.DrawLine(50, 230, 200, 230);
c801d85f 282
c0ed460c 283 dc.SetPen(* wxBLACK_PEN);
bd7d06f2 284 dc.DrawArc(50, 300, 100, 250, 100, 300 );
c801d85f
KB
285}
286
c62ac5b6
RR
287//-----------------------------------------------------------------
288// MyWindow
289//-----------------------------------------------------------------
290
c801d85f
KB
291BEGIN_EVENT_TABLE(MyWindow, wxWindow)
292 EVT_PAINT(MyWindow::OnPaint)
293END_EVENT_TABLE()
294
295// Define a constructor for my canvas
296MyWindow::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
83edc0a5 301MyWindow::~MyWindow()
c801d85f
KB
302{
303}
304
305// Define the repainting behaviour
bd7d06f2 306void MyWindow::OnPaint(wxPaintEvent& WXUNUSED(event) )
c801d85f
KB
307{
308 wxPaintDC dc(this);
309 frame->Draw(dc,TRUE);
310}
311
c62ac5b6 312//-----------------------------------------------------------------
83edc0a5 313// MySizerFrame
c62ac5b6
RR
314//-----------------------------------------------------------------
315
83edc0a5 316MySizerFrame::MySizerFrame(wxFrame *frame, char *title, int x, int y ):
c62ac5b6
RR
317 wxFrame(frame, -1, title, wxPoint(x, y) )
318{
61d514bb
RR
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
d597fcb7 321 // two buttons which.
61d514bb 322
d2befda3 323 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
56ac3e75 324
d597fcb7 325 // 1) top: create wxStaticText with minimum size equal to its default size
56ac3e75 326 topsizer->Add(
d597fcb7
RR
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)
56ac3e75 334 topsizer->Add(
d597fcb7
RR
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
37393997
RL
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
f6bcfd97
BP
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
d597fcb7
RR
369
370 // 3) middle: create wxStaticLine with minimum size (3x3)
56ac3e75
RD
371 topsizer->Add(
372 new wxStaticLine( this, -1, wxDefaultPosition, wxSize(3,3), wxHORIZONTAL),
d597fcb7
RR
373 0, // make vertically unstretchable
374 wxEXPAND | // make horizontally stretchable
375 wxALL, // and make border all around
376 5 ); // set border width to 5
d597fcb7 377
56ac3e75
RD
378
379 // 4) bottom: create two centred wxButtons
d2befda3 380 wxBoxSizer *button_box = new wxBoxSizer( wxHORIZONTAL );
d597fcb7 381 button_box->Add(
56ac3e75 382 new wxButton( this, -1, "Two buttons in a box" ),
d597fcb7
RR
383 0, // make horizontally unstretchable
384 wxALL, // make border all around
385 7 ); // set border width to 7
386 button_box->Add(
56ac3e75 387 new wxButton( this, -1, "(wxCENTER)" ),
d597fcb7
RR
388 0, // make horizontally unstretchable
389 wxALL, // make border all around
390 7 ); // set border width to 7
56ac3e75
RD
391
392 topsizer->Add(
d597fcb7
RR
393 button_box,
394 0, // make vertically unstretchable
395 wxCENTER ); // no border and centre horizontally
61d514bb 396
d8d474af 397 SetAutoLayout( TRUE );
56ac3e75 398
c62ac5b6 399 // set frame to minimum size
56ac3e75
RD
400 topsizer->Fit( this );
401
c62ac5b6 402 // don't allow frame to get smaller than what the sizers tell ye
56ac3e75
RD
403 topsizer->SetSizeHints( this );
404
3417c2cd 405 SetSizer( topsizer );
c62ac5b6
RR
406}
407
c62ac5b6
RR
408
409