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