]> git.saurik.com Git - wxWidgets.git/blame - samples/layout/layout.cpp
small fix to the sample
[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
83edc0a5 230 wxBoxSizer *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
RR
277 dc.DrawEllipse(250, 250, 100, 50);
278 dc.DrawSpline(50, 200, 50, 100, 200, 10);
279 dc.DrawLine(50, 230, 200, 230);
c801d85f 280
c0ed460c 281 dc.SetPen(* wxBLACK_PEN);
bd7d06f2 282 dc.DrawArc(50, 300, 100, 250, 100, 300 );
c801d85f
KB
283}
284
c62ac5b6
RR
285//-----------------------------------------------------------------
286// MyWindow
287//-----------------------------------------------------------------
288
c801d85f
KB
289BEGIN_EVENT_TABLE(MyWindow, wxWindow)
290 EVT_PAINT(MyWindow::OnPaint)
291END_EVENT_TABLE()
292
293// Define a constructor for my canvas
294MyWindow::MyWindow(wxFrame *frame, int x, int y, int w, int h, long style):
295 wxWindow(frame, -1, wxPoint(x, y), wxSize(w, h), style)
296{
297}
298
83edc0a5 299MyWindow::~MyWindow()
c801d85f
KB
300{
301}
302
303// Define the repainting behaviour
bd7d06f2 304void MyWindow::OnPaint(wxPaintEvent& WXUNUSED(event) )
c801d85f
KB
305{
306 wxPaintDC dc(this);
307 frame->Draw(dc,TRUE);
308}
309
c62ac5b6 310//-----------------------------------------------------------------
83edc0a5 311// MySizerFrame
c62ac5b6
RR
312//-----------------------------------------------------------------
313
83edc0a5 314MySizerFrame::MySizerFrame(wxFrame *frame, char *title, int x, int y ):
c62ac5b6
RR
315 wxFrame(frame, -1, title, wxPoint(x, y) )
316{
61d514bb
RR
317 // we want to get a dialog that is stretchable because it
318 // has a text ctrl in the middle. at the bottom, we have
d597fcb7 319 // two buttons which.
61d514bb 320
d2befda3 321 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
56ac3e75 322
d597fcb7 323 // 1) top: create wxStaticText with minimum size equal to its default size
56ac3e75 324 topsizer->Add(
d597fcb7
RR
325 new wxStaticText( this, -1, "An explanation (wxALIGN_RIGHT)." ),
326 0, // make vertically unstretchable
327 wxALIGN_RIGHT | // right align text
328 wxTOP | wxLEFT | wxRIGHT, // make border all around except wxBOTTOM
329 5 ); // set border width to 5
330
331 // 2) top: create wxTextCtrl with minimum size (100x60)
56ac3e75 332 topsizer->Add(
d597fcb7
RR
333 new wxTextCtrl( this, -1, "My text (wxEXPAND).", wxDefaultPosition, wxSize(100,60), wxTE_MULTILINE),
334 1, // make vertically stretchable
335 wxEXPAND | // make horizontally stretchable
336 wxALL, // and make border all around
337 5 ); // set border width to 5
338
37393997
RL
339 // 2.5) Gratuitous test of wxStaticBoxSizers
340 wxBoxSizer *statsizer = new wxStaticBoxSizer(
341 new wxStaticBox(this, -1, "A wxStaticBoxSizer"),
342 wxVERTICAL );
343 statsizer->Add(
344 new wxStaticText(this, -1, "And some TEXT inside it"),
345 0,
346 wxCENTER |
347 wxALL,
348 30);
349 topsizer->Add(statsizer, 1, wxEXPAND | wxALL, 10);
350
d597fcb7
RR
351
352 // 3) middle: create wxStaticLine with minimum size (3x3)
56ac3e75
RD
353 topsizer->Add(
354 new wxStaticLine( this, -1, wxDefaultPosition, wxSize(3,3), wxHORIZONTAL),
d597fcb7
RR
355 0, // make vertically unstretchable
356 wxEXPAND | // make horizontally stretchable
357 wxALL, // and make border all around
358 5 ); // set border width to 5
d597fcb7 359
56ac3e75
RD
360
361 // 4) bottom: create two centred wxButtons
d2befda3 362 wxBoxSizer *button_box = new wxBoxSizer( wxHORIZONTAL );
d597fcb7 363 button_box->Add(
56ac3e75 364 new wxButton( this, -1, "Two buttons in a box" ),
d597fcb7
RR
365 0, // make horizontally unstretchable
366 wxALL, // make border all around
367 7 ); // set border width to 7
368 button_box->Add(
56ac3e75 369 new wxButton( this, -1, "(wxCENTER)" ),
d597fcb7
RR
370 0, // make horizontally unstretchable
371 wxALL, // make border all around
372 7 ); // set border width to 7
56ac3e75
RD
373
374 topsizer->Add(
d597fcb7
RR
375 button_box,
376 0, // make vertically unstretchable
377 wxCENTER ); // no border and centre horizontally
61d514bb 378
d8d474af 379 SetAutoLayout( TRUE );
56ac3e75 380
c62ac5b6 381 // set frame to minimum size
56ac3e75
RD
382 topsizer->Fit( this );
383
c62ac5b6 384 // don't allow frame to get smaller than what the sizers tell ye
56ac3e75
RD
385 topsizer->SetSizeHints( this );
386
3417c2cd 387 SetSizer( topsizer );
c62ac5b6
RR
388}
389
c62ac5b6
RR
390
391