]>
Commit | Line | Data |
---|---|---|
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((MyFrame *) NULL, (char *) "wxWindows Layout Demo", 0, 0, 550, 500); | |
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_LOAD_FILE, "&Load file", "Load a text file"); | |
59 | file_menu->Append(LAYOUT_TEST_SIZER, "&Test sizers", "Test sizer"); | |
60 | file_menu->Append(LAYOUT_TEST_NB, "&Test notebook sizers", "Test notebook sizer"); | |
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); | |
78 | frame->panel->SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE)); | |
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 | ||
161 | //----------------------------------------------------------------- | |
162 | // MyFrame | |
163 | //----------------------------------------------------------------- | |
164 | ||
165 | // Define my frame constructor | |
166 | MyFrame::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 | { | |
169 | panel = (wxPanel *) NULL; | |
170 | text_window = (MyTextWindow *) NULL; | |
171 | canvas = (MyWindow *) NULL; | |
172 | } | |
173 | ||
174 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
175 | EVT_MENU(LAYOUT_LOAD_FILE, MyFrame::LoadFile) | |
176 | EVT_MENU(LAYOUT_QUIT, MyFrame::Quit) | |
177 | EVT_MENU(LAYOUT_TEST_SIZER, MyFrame::TestSizers) | |
178 | EVT_MENU(LAYOUT_TEST_NB, MyFrame::TestNotebookSizers) | |
179 | EVT_MENU(LAYOUT_ABOUT, MyFrame::About) | |
180 | EVT_SIZE(MyFrame::OnSize) | |
181 | END_EVENT_TABLE() | |
182 | ||
183 | void MyFrame::LoadFile(wxCommandEvent& WXUNUSED(event) ) | |
184 | { | |
185 | wxString s = wxFileSelector( _T("Load text file"), (const wxChar *) NULL, | |
186 | (const wxChar *) NULL, (const wxChar *) NULL, _T("*.txt") ); | |
187 | if (s != "") | |
188 | { | |
189 | #ifdef __WXMSW__ | |
190 | frame->text_window->LoadFile(s); | |
191 | #endif | |
192 | } | |
193 | } | |
194 | ||
195 | void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) ) | |
196 | { | |
197 | this->Close(TRUE); | |
198 | } | |
199 | ||
200 | void MyFrame::TestSizers(wxCommandEvent& WXUNUSED(event) ) | |
201 | { | |
202 | MySizerFrame *newFrame = new MySizerFrame((MyFrame *) NULL, "Sizer Test Frame", 50, 50 ); | |
203 | newFrame->Show(TRUE); | |
204 | } | |
205 | ||
206 | void MyFrame::TestNotebookSizers(wxCommandEvent& WXUNUSED(event) ) | |
207 | { | |
208 | wxDialog dialog( this, -1, wxString("Notebook Sizer Test Dialog") ); | |
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 ); | |
214 | ||
215 | wxNotebook *notebook = new wxNotebook( &dialog, -1 ); | |
216 | wxNotebookSizer *nbs = new wxNotebookSizer( notebook ); | |
217 | topsizer->Add( nbs, 1, wxGROW ); | |
218 | ||
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" ); | |
225 | ||
226 | // Second page: a text ctrl and a button | |
227 | wxPanel *panel = new wxPanel( notebook, -1 ); | |
228 | notebook->AddPage( panel, "Page Two" ); | |
229 | ||
230 | wxBoxSizer *panelsizer = new wxBoxSizer( wxVERTICAL ); | |
231 | ||
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 ); | |
238 | ||
239 | panel->SetAutoLayout( TRUE ); | |
240 | panel->SetSizer( panelsizer ); | |
241 | ||
242 | // Tell dialog to use sizer | |
243 | ||
244 | dialog.SetAutoLayout( TRUE ); | |
245 | dialog.SetSizer( topsizer ); | |
246 | topsizer->Fit( &dialog ); | |
247 | topsizer->SetSizeHints( &dialog ); | |
248 | ||
249 | dialog.ShowModal(); | |
250 | } | |
251 | ||
252 | ||
253 | void MyFrame::About(wxCommandEvent& WXUNUSED(event) ) | |
254 | { | |
255 | (void)wxMessageBox("wxWindows GUI library layout demo\n", | |
256 | "About Layout Demo", wxOK|wxCENTRE); | |
257 | } | |
258 | ||
259 | // Size the subwindows when the frame is resized | |
260 | void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event) ) | |
261 | { | |
262 | Layout(); | |
263 | } | |
264 | ||
265 | void MyFrame::Draw(wxDC& dc, bool WXUNUSED(draw_bitmaps) ) | |
266 | { | |
267 | dc.SetPen(* wxGREEN_PEN); | |
268 | dc.DrawLine(0, 0, 200, 200); | |
269 | dc.DrawLine(200, 0, 0, 200); | |
270 | ||
271 | dc.SetBrush(* wxCYAN_BRUSH); | |
272 | dc.SetPen(* wxRED_PEN); | |
273 | ||
274 | dc.DrawRectangle(100, 100, 100, 50); | |
275 | dc.DrawRoundedRectangle(150, 150, 100, 50, 20); | |
276 | ||
277 | dc.DrawEllipse(250, 250, 100, 50); | |
278 | dc.DrawSpline(50, 200, 50, 100, 200, 10); | |
279 | dc.DrawLine(50, 230, 200, 230); | |
280 | ||
281 | dc.SetPen(* wxBLACK_PEN); | |
282 | dc.DrawArc(50, 300, 100, 250, 100, 300 ); | |
283 | } | |
284 | ||
285 | //----------------------------------------------------------------- | |
286 | // MyWindow | |
287 | //----------------------------------------------------------------- | |
288 | ||
289 | BEGIN_EVENT_TABLE(MyWindow, wxWindow) | |
290 | EVT_PAINT(MyWindow::OnPaint) | |
291 | END_EVENT_TABLE() | |
292 | ||
293 | // Define a constructor for my canvas | |
294 | MyWindow::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 | ||
299 | MyWindow::~MyWindow() | |
300 | { | |
301 | } | |
302 | ||
303 | // Define the repainting behaviour | |
304 | void MyWindow::OnPaint(wxPaintEvent& WXUNUSED(event) ) | |
305 | { | |
306 | wxPaintDC dc(this); | |
307 | frame->Draw(dc,TRUE); | |
308 | } | |
309 | ||
310 | //----------------------------------------------------------------- | |
311 | // MySizerFrame | |
312 | //----------------------------------------------------------------- | |
313 | ||
314 | MySizerFrame::MySizerFrame(wxFrame *frame, char *title, int x, int y ): | |
315 | wxFrame(frame, -1, title, wxPoint(x, y) ) | |
316 | { | |
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 | |
319 | // two buttons which. | |
320 | ||
321 | wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); | |
322 | ||
323 | // 1) top: create wxStaticText with minimum size equal to its default size | |
324 | topsizer->Add( | |
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) | |
332 | topsizer->Add( | |
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 | ||
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 | ||
351 | ||
352 | // 3) middle: create wxStaticLine with minimum size (3x3) | |
353 | topsizer->Add( | |
354 | new wxStaticLine( this, -1, wxDefaultPosition, wxSize(3,3), wxHORIZONTAL), | |
355 | 0, // make vertically unstretchable | |
356 | wxEXPAND | // make horizontally stretchable | |
357 | wxALL, // and make border all around | |
358 | 5 ); // set border width to 5 | |
359 | ||
360 | ||
361 | // 4) bottom: create two centred wxButtons | |
362 | wxBoxSizer *button_box = new wxBoxSizer( wxHORIZONTAL ); | |
363 | button_box->Add( | |
364 | new wxButton( this, -1, "Two buttons in a box" ), | |
365 | 0, // make horizontally unstretchable | |
366 | wxALL, // make border all around | |
367 | 7 ); // set border width to 7 | |
368 | button_box->Add( | |
369 | new wxButton( this, -1, "(wxCENTER)" ), | |
370 | 0, // make horizontally unstretchable | |
371 | wxALL, // make border all around | |
372 | 7 ); // set border width to 7 | |
373 | ||
374 | topsizer->Add( | |
375 | button_box, | |
376 | 0, // make vertically unstretchable | |
377 | wxCENTER ); // no border and centre horizontally | |
378 | ||
379 | SetAutoLayout( TRUE ); | |
380 | ||
381 | // set frame to minimum size | |
382 | topsizer->Fit( this ); | |
383 | ||
384 | // don't allow frame to get smaller than what the sizers tell ye | |
385 | topsizer->SetSizeHints( this ); | |
386 | ||
387 | SetSizer( topsizer ); | |
388 | } | |
389 | ||
390 | ||
391 |