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