]>
git.saurik.com Git - wxWidgets.git/blob - samples/layout/layout.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Layout sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
24 #error You must set USE_CONSTRAINTS to 1 in wx_setup.h!
31 MyFrame
*frame
= NULL
;
32 wxMenuBar
*menu_bar
= NULL
;
44 bool MyApp::OnInit(void)
46 // Create the main frame window
47 frame
= new MyFrame(NULL
, "wxWindows Layout Demo", 0, 0, 550, 500);
49 frame
->SetAutoLayout(TRUE
);
51 // Give it a status line
52 frame
->CreateStatusBar(2);
54 // Load icon and bitmap
56 frame
->SetIcon(wxIcon("aiai_icn"));
59 frame
->SetIcon(wxIcon(aiai_bits
, aiai_width
, aiai_height
));
63 wxMenu
*file_menu
= new wxMenu
;
65 file_menu
->Append(LAYOUT_LOAD_FILE
, "&Load file", "Load a text file");
66 file_menu
->Append(LAYOUT_TEST
, "&Test sizers", "Test sizer code");
68 file_menu
->AppendSeparator();
69 file_menu
->Append(LAYOUT_QUIT
, "E&xit", "Quit program");
71 wxMenu
*help_menu
= new wxMenu
;
72 help_menu
->Append(LAYOUT_ABOUT
, "&About", "About layout demo");
74 menu_bar
= new wxMenuBar
;
76 menu_bar
->Append(file_menu
, "&File");
77 menu_bar
->Append(help_menu
, "&Help");
79 // Associate the menu bar with the frame
80 frame
->SetMenuBar(menu_bar
);
83 frame
->panel
= new wxPanel(frame
, 0, 0, 1000, 500, wxTAB_TRAVERSAL
);
84 frame
->panel
->SetBackgroundColour(wxColour(192, 192, 192));
85 // frame->panel->SetAutoLayout(TRUE);
87 // Create some panel items
88 wxButton
*btn1
= new wxButton(frame
->panel
, -1, "A button (1)") ;
90 wxLayoutConstraints
*b1
= new wxLayoutConstraints
;
91 b1
->centreX
.SameAs (frame
->panel
, wxCentreX
);
92 b1
->top
.SameAs (frame
->panel
, wxTop
, 5);
93 b1
->width
.PercentOf (frame
->panel
, wxWidth
, 80);
94 b1
->height
.PercentOf (frame
->panel
, wxHeight
, 10);
95 btn1
->SetConstraints(b1
);
97 wxListBox
*list
= new wxListBox(frame
->panel
, -1,
98 wxPoint(-1, -1), wxSize(200, 100));
99 list
->Append("Apple");
100 list
->Append("Pear");
101 list
->Append("Orange");
102 list
->Append("Banana");
103 list
->Append("Fruit");
105 wxLayoutConstraints
*b2
= new wxLayoutConstraints
;
106 b2
->top
.Below (btn1
, 5);
107 b2
->left
.SameAs (frame
->panel
, wxLeft
, 5);
108 b2
->width
.PercentOf (frame
->panel
, wxWidth
, 40);
109 b2
->bottom
.SameAs (frame
->panel
, wxBottom
, 5);
110 list
->SetConstraints(b2
);
112 wxTextCtrl
*mtext
= new wxTextCtrl(frame
->panel
, -1, "Some text",
113 wxPoint(-1, -1), wxSize(150, 100));
115 wxLayoutConstraints
*b3
= new wxLayoutConstraints
;
116 b3
->top
.Below (btn1
, 5);
117 b3
->left
.RightOf (list
, 5);
118 b3
->right
.SameAs (frame
->panel
, wxRight
, 5);
119 b3
->bottom
.SameAs (frame
->panel
, wxBottom
, 5);
120 mtext
->SetConstraints(b3
);
122 frame
->canvas
= new MyWindow(frame
, 0, 0, 400, 400, wxRETAINED
);
124 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
125 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
127 // Make a text window
128 frame
->text_window
= new MyTextWindow(frame
, 0, 250, 400, 250);
130 // Set constraints for panel subwindow
131 wxLayoutConstraints
*c1
= new wxLayoutConstraints
;
133 c1
->left
.SameAs (frame
, wxLeft
);
134 c1
->top
.SameAs (frame
, wxTop
);
135 c1
->right
.PercentOf (frame
, wxWidth
, 50);
136 c1
->height
.PercentOf (frame
, wxHeight
, 50);
138 frame
->panel
->SetConstraints(c1
);
140 // Set constraints for canvas subwindow
141 wxLayoutConstraints
*c2
= new wxLayoutConstraints
;
143 c2
->left
.SameAs (frame
->panel
, wxRight
);
144 c2
->top
.SameAs (frame
, wxTop
);
145 c2
->right
.SameAs (frame
, wxRight
);
146 c2
->height
.PercentOf (frame
, wxHeight
, 50);
148 frame
->canvas
->SetConstraints(c2
);
150 // Set constraints for text subwindow
151 wxLayoutConstraints
*c3
= new wxLayoutConstraints
;
152 c3
->left
.SameAs (frame
, wxLeft
);
153 c3
->top
.Below (frame
->panel
);
154 c3
->right
.SameAs (frame
, wxRight
);
155 c3
->bottom
.SameAs (frame
, wxBottom
);
157 frame
->text_window
->SetConstraints(c3
);
161 frame
->SetStatusText("wxWindows layout demo");
167 // Define my frame constructor
168 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
169 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
176 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
177 EVT_MENU(LAYOUT_LOAD_FILE
, MyFrame::LoadFile
)
178 EVT_MENU(LAYOUT_QUIT
, MyFrame::Quit
)
179 EVT_MENU(LAYOUT_TEST
, MyFrame::TestSizers
)
180 EVT_MENU(LAYOUT_ABOUT
, MyFrame::About
)
181 EVT_SIZE(MyFrame::OnSize
)
184 void MyFrame::LoadFile(wxCommandEvent
& event
)
186 char *s
= wxFileSelector("Load text file", NULL
, NULL
, NULL
, "*.txt");
190 frame
->text_window
->LoadFile(s
);
195 void MyFrame::Quit(wxCommandEvent
& event
)
200 void MyFrame::TestSizers(wxCommandEvent
& event
)
202 SizerFrame
*newFrame
= new SizerFrame(NULL
, "Sizer Test Frame", 50, 50, 500, 500);
203 newFrame
->Show(TRUE
);
206 void MyFrame::About(wxCommandEvent
& event
)
208 (void)wxMessageBox("wxWindows GUI library layout demo\n",
209 "About Layout Demo", wxOK
|wxCENTRE
);
212 // Size the subwindows when the frame is resized
213 void MyFrame::OnSize(wxSizeEvent
& event
)
218 void MyFrame::Draw(wxDC
& dc
, bool draw_bitmaps
)
220 dc
.SetPen(wxGREEN_PEN
);
221 dc
.DrawLine(0.0, 0.0, 200.0, 200.0);
222 dc
.DrawLine(200.0, 0.0, 0.0, 200.0);
224 dc
.SetBrush(wxCYAN_BRUSH
);
225 dc
.SetPen(wxRED_PEN
);
227 dc
.DrawRectangle(100.0, 100.0, 100.0, 50.0);
228 dc
.DrawRoundedRectangle(150.0, 150.0, 100.0, 50.0,20.0);
230 dc
.DrawEllipse(250.0, 250.0, 100.0, 50.0);
231 dc
.DrawSpline(50.0, 200.0, 50.0, 100.0, 200.0, 10.0);
232 dc
.DrawLine(50.0, 230.0, 200.0, 230.0);
234 dc
.SetPen(wxBLACK_PEN
);
235 dc
.DrawArc(50.0, 300.0, 100.0, 250.0, 100.0, 300.0);
238 BEGIN_EVENT_TABLE(MyWindow
, wxWindow
)
239 EVT_PAINT(MyWindow::OnPaint
)
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
)
248 MyWindow::~MyWindow(void)
252 // Define the repainting behaviour
253 void MyWindow::OnPaint(wxPaintEvent
& event
)
256 frame
->Draw(dc
,TRUE
);
259 // Define the behaviour for the frame closing
260 // - must delete all frames except for the main one.
261 bool MyFrame::OnClose(void)
268 SizerFrame::SizerFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
269 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
271 panel
= new wxPanel(this, -1, wxPoint(0, 0), wxSize(-1, -1), wxTAB_TRAVERSAL
);
272 panel
->SetBackgroundColour(wxColour(192, 192, 192));
274 // A sizer to fit the whole panel, plus two sizers, one
275 // above the other. A button is centred on the lower
276 // sizer; a rowcol containing 3 buttons is centred on the upper
278 wxSizer
*expandSizer
= new wxSizer(panel
, wxSizerExpand
);
279 expandSizer
->SetName("expandSizer");
281 wxLayoutConstraints
*c
;
285 wxSizer
*topSizer
= new wxSizer(expandSizer
);
286 topSizer
->SetName("topSizer");
288 // Specify constraints for the top sizer
289 c
= new wxLayoutConstraints
;
290 c
->left
.SameAs (expandSizer
, wxLeft
);
291 c
->top
.SameAs (expandSizer
, wxTop
);
292 c
->right
.SameAs (expandSizer
, wxRight
);
293 c
->height
.PercentOf (expandSizer
, wxHeight
, 50);
295 topSizer
->SetConstraints(c
);
298 * Add a row-col sizer and some buttons
301 // Default is layout by rows, 20 columns per row, shrink to fit.
302 wxRowColSizer
*rowCol
= new wxRowColSizer(topSizer
);
303 rowCol
->SetName("rowCol");
305 wxButton
*button
= new wxButton(panel
, -1, "Button 1");
306 rowCol
->AddSizerChild(button
);
308 button
= new wxButton(panel
, -1, "Button 2");
309 rowCol
->AddSizerChild(button
);
311 button
= new wxButton(panel
, -1, "Button 3");
312 rowCol
->AddSizerChild(button
);
314 // Centre the rowcol in the middle of the upper sizer
315 c
= new wxLayoutConstraints
;
316 c
->centreX
.SameAs (topSizer
, wxCentreX
);
317 c
->centreY
.SameAs (topSizer
, wxCentreY
);
320 rowCol
->SetConstraints(c
);
322 /////// BOTTOM OF PANEL
324 wxSizer
*bottomSizer
= new wxSizer(expandSizer
);
326 // Specify constraints for the bottom sizer
327 c
= new wxLayoutConstraints
;
328 c
->left
.SameAs (expandSizer
, wxLeft
);
329 c
->top
.PercentOf (expandSizer
, wxHeight
, 50);
330 c
->right
.SameAs (expandSizer
, wxRight
);
331 c
->height
.PercentOf (expandSizer
, wxHeight
, 50);
333 bottomSizer
->SetConstraints(c
);
335 wxButton
*button2
= new wxButton(panel
, -1, "Test button");
337 // The button should be a child of the bottom sizer
338 bottomSizer
->AddSizerChild(button2
);
340 // Centre the button on the sizer
341 c
= new wxLayoutConstraints
;
342 c
->centreX
.SameAs (bottomSizer
, wxCentreX
);
343 c
->centreY
.SameAs (bottomSizer
, wxCentreY
);
344 c
->width
.PercentOf (bottomSizer
, wxWidth
, 20);
345 c
->height
.PercentOf (bottomSizer
, wxHeight
, 20);
346 button2
->SetConstraints(c
);
349 BEGIN_EVENT_TABLE(SizerFrame
, wxFrame
)
350 EVT_SIZE(SizerFrame::OnSize
)
354 // Size the subwindows when the frame is resized
355 void SizerFrame::OnSize(wxSizeEvent
& event
)
357 wxFrame::OnSize(event
);
361 bool SizerFrame::OnClose(void)