]> git.saurik.com Git - wxWidgets.git/blame - samples/layout/layout.cpp
Added makefile and resource script for building under mingw32
[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>
c62ac5b6 28#include "wx/sizer.h"
61d514bb 29#include "wx/statline.h"
c62ac5b6 30
c801d85f
KB
31#include "layout.h"
32
33// Declare two frames
c67daf87
UR
34MyFrame *frame = (MyFrame *) NULL;
35wxMenuBar *menu_bar = (wxMenuBar *) NULL;
c801d85f
KB
36
37IMPLEMENT_APP(MyApp)
38
c801d85f
KB
39MyApp::MyApp()
40{
41}
42
43bool MyApp::OnInit(void)
44{
45 // Create the main frame window
c67daf87 46 frame = new MyFrame((MyFrame *) NULL, (char *) "wxWindows Layout Demo", 0, 0, 550, 500);
c801d85f
KB
47
48 frame->SetAutoLayout(TRUE);
49
50 // Give it a status line
51 frame->CreateStatusBar(2);
52
c801d85f
KB
53 // Make a menubar
54 wxMenu *file_menu = new wxMenu;
55
56 file_menu->Append(LAYOUT_LOAD_FILE, "&Load file", "Load a text file");
c62ac5b6 57 file_menu->Append(LAYOUT_TEST_NEW, "&Test new sizers", "Test new sizer code");
c801d85f
KB
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);
17b0d794 75 frame->panel->SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
c801d85f
KB
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
c62ac5b6
RR
158//-----------------------------------------------------------------
159// MyFrame
160//-----------------------------------------------------------------
161
c801d85f
KB
162// Define my frame constructor
163MyFrame::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{
c67daf87
UR
166 panel = (wxPanel *) NULL;
167 text_window = (MyTextWindow *) NULL;
168 canvas = (MyWindow *) NULL;
c801d85f
KB
169}
170
171BEGIN_EVENT_TABLE(MyFrame, wxFrame)
172 EVT_MENU(LAYOUT_LOAD_FILE, MyFrame::LoadFile)
173 EVT_MENU(LAYOUT_QUIT, MyFrame::Quit)
c62ac5b6 174 EVT_MENU(LAYOUT_TEST_NEW, MyFrame::TestNewSizers)
c801d85f
KB
175 EVT_MENU(LAYOUT_ABOUT, MyFrame::About)
176 EVT_SIZE(MyFrame::OnSize)
177END_EVENT_TABLE()
178
bd7d06f2 179void MyFrame::LoadFile(wxCommandEvent& WXUNUSED(event) )
c801d85f 180{
ddb6bc71
RR
181 wxString s = wxFileSelector( _T("Load text file"), (const wxChar *) NULL,
182 (const wxChar *) NULL, (const wxChar *) NULL, _T("*.txt") );
227869da 183 if (s != "")
c801d85f 184 {
2049ba38 185#ifdef __WXMSW__
c801d85f
KB
186 frame->text_window->LoadFile(s);
187#endif
188 }
189}
190
bd7d06f2 191void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
c801d85f
KB
192{
193 this->Close(TRUE);
194}
195
c62ac5b6
RR
196void MyFrame::TestNewSizers(wxCommandEvent& WXUNUSED(event) )
197{
198 NewSizerFrame *newFrame = new NewSizerFrame((MyFrame *) NULL, "Sizer Test Frame", 50, 50 );
c801d85f
KB
199 newFrame->Show(TRUE);
200}
201
bd7d06f2 202void MyFrame::About(wxCommandEvent& WXUNUSED(event) )
c801d85f
KB
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
bd7d06f2 209void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event) )
c801d85f
KB
210{
211 Layout();
212}
213
bd7d06f2 214void MyFrame::Draw(wxDC& dc, bool WXUNUSED(draw_bitmaps) )
c801d85f 215{
c0ed460c 216 dc.SetPen(* wxGREEN_PEN);
bd7d06f2
RR
217 dc.DrawLine(0, 0, 200, 200);
218 dc.DrawLine(200, 0, 0, 200);
c801d85f 219
c0ed460c
JS
220 dc.SetBrush(* wxCYAN_BRUSH);
221 dc.SetPen(* wxRED_PEN);
c801d85f 222
bd7d06f2
RR
223 dc.DrawRectangle(100, 100, 100, 50);
224 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
c801d85f 225
bd7d06f2
RR
226 dc.DrawEllipse(250, 250, 100, 50);
227 dc.DrawSpline(50, 200, 50, 100, 200, 10);
228 dc.DrawLine(50, 230, 200, 230);
c801d85f 229
c0ed460c 230 dc.SetPen(* wxBLACK_PEN);
bd7d06f2 231 dc.DrawArc(50, 300, 100, 250, 100, 300 );
c801d85f
KB
232}
233
c62ac5b6
RR
234//-----------------------------------------------------------------
235// MyWindow
236//-----------------------------------------------------------------
237
c801d85f
KB
238BEGIN_EVENT_TABLE(MyWindow, wxWindow)
239 EVT_PAINT(MyWindow::OnPaint)
240END_EVENT_TABLE()
241
242// Define a constructor for my canvas
243MyWindow::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
248MyWindow::~MyWindow(void)
249{
250}
251
252// Define the repainting behaviour
bd7d06f2 253void MyWindow::OnPaint(wxPaintEvent& WXUNUSED(event) )
c801d85f
KB
254{
255 wxPaintDC dc(this);
256 frame->Draw(dc,TRUE);
257}
258
c62ac5b6
RR
259//-----------------------------------------------------------------
260// NewSizerFrame
261//-----------------------------------------------------------------
262
263NewSizerFrame::NewSizerFrame(wxFrame *frame, char *title, int x, int y ):
264 wxFrame(frame, -1, title, wxPoint(x, y) )
265{
61d514bb
RR
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
d597fcb7 268 // two buttons which.
61d514bb 269
d2befda3 270 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
61d514bb 271
d597fcb7
RR
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
289 // 3) middle: create wxStaticLine with minimum size (3x3)
290 topsizer->Add(
291 new wxStaticLine( this, -1, wxDefaultPosition, wxSize(3,3), wxHORIZONTAL),
292 0, // make vertically unstretchable
293 wxEXPAND | // make horizontally stretchable
294 wxALL, // and make border all around
295 5 ); // set border width to 5
296
297
298 // 4) bottom: create two centred wxButtons
d2befda3 299 wxBoxSizer *button_box = new wxBoxSizer( wxHORIZONTAL );
d597fcb7
RR
300 button_box->Add(
301 new wxButton( this, -1, "Two buttons in a box" ),
302 0, // make horizontally unstretchable
303 wxALL, // make border all around
304 7 ); // set border width to 7
305 button_box->Add(
306 new wxButton( this, -1, "(wxCENTER)" ),
307 0, // make horizontally unstretchable
308 wxALL, // make border all around
309 7 ); // set border width to 7
61d514bb 310
d597fcb7
RR
311 topsizer->Add(
312 button_box,
313 0, // make vertically unstretchable
314 wxCENTER ); // no border and centre horizontally
61d514bb 315
c62ac5b6
RR
316
317 // set frame to minimum size
318 topsizer->Fit( this );
319
320 // don't allow frame to get smaller than what the sizers tell ye
321 topsizer->SetSizeHints( this );
322
3417c2cd
RR
323 SetSizer( topsizer );
324
325 SetAutoLayout( TRUE );
c62ac5b6
RR
326}
327
c62ac5b6
RR
328
329