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