]>
git.saurik.com Git - wxWidgets.git/blob - samples/layout/layout.cpp
32b27c43cc80e2586a8bdd67673d934be0abb6d4
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"
23 #if !wxUSE_CONSTRAINTS
24 #error You must set wxUSE_CONSTRAINTS to 1 in setup.h!
29 #include "wx/statline.h"
34 MyFrame
*frame
= (MyFrame
*) NULL
;
35 wxMenuBar
*menu_bar
= (wxMenuBar
*) NULL
;
43 bool MyApp::OnInit(void)
45 // Create the main frame window
46 frame
= new MyFrame((MyFrame
*) NULL
, (char *) "wxWindows Layout Demo", 0, 0, 550, 500);
48 frame
->SetAutoLayout(TRUE
);
50 // Give it a status line
51 frame
->CreateStatusBar(2);
54 wxMenu
*file_menu
= new wxMenu
;
56 file_menu
->Append(LAYOUT_LOAD_FILE
, "&Load file", "Load a text file");
57 file_menu
->Append(LAYOUT_TEST
, "&Test sizers", "Test sizer code");
58 file_menu
->Append(LAYOUT_TEST_NEW
, "&Test new sizers", "Test new sizer code");
60 file_menu
->AppendSeparator();
61 file_menu
->Append(LAYOUT_QUIT
, "E&xit", "Quit program");
63 wxMenu
*help_menu
= new wxMenu
;
64 help_menu
->Append(LAYOUT_ABOUT
, "&About", "About layout demo");
66 menu_bar
= new wxMenuBar
;
68 menu_bar
->Append(file_menu
, "&File");
69 menu_bar
->Append(help_menu
, "&Help");
71 // Associate the menu bar with the frame
72 frame
->SetMenuBar(menu_bar
);
75 frame
->panel
= new wxPanel(frame
, 0, 0, 1000, 500, wxTAB_TRAVERSAL
);
76 frame
->panel
->SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
77 // frame->panel->SetAutoLayout(TRUE);
79 // Create some panel items
80 wxButton
*btn1
= new wxButton(frame
->panel
, -1, "A button (1)") ;
82 wxLayoutConstraints
*b1
= new wxLayoutConstraints
;
83 b1
->centreX
.SameAs (frame
->panel
, wxCentreX
);
84 b1
->top
.SameAs (frame
->panel
, wxTop
, 5);
85 b1
->width
.PercentOf (frame
->panel
, wxWidth
, 80);
86 b1
->height
.PercentOf (frame
->panel
, wxHeight
, 10);
87 btn1
->SetConstraints(b1
);
89 wxListBox
*list
= new wxListBox(frame
->panel
, -1,
90 wxPoint(-1, -1), wxSize(200, 100));
91 list
->Append("Apple");
93 list
->Append("Orange");
94 list
->Append("Banana");
95 list
->Append("Fruit");
97 wxLayoutConstraints
*b2
= new wxLayoutConstraints
;
98 b2
->top
.Below (btn1
, 5);
99 b2
->left
.SameAs (frame
->panel
, wxLeft
, 5);
100 b2
->width
.PercentOf (frame
->panel
, wxWidth
, 40);
101 b2
->bottom
.SameAs (frame
->panel
, wxBottom
, 5);
102 list
->SetConstraints(b2
);
104 wxTextCtrl
*mtext
= new wxTextCtrl(frame
->panel
, -1, "Some text",
105 wxPoint(-1, -1), wxSize(150, 100));
107 wxLayoutConstraints
*b3
= new wxLayoutConstraints
;
108 b3
->top
.Below (btn1
, 5);
109 b3
->left
.RightOf (list
, 5);
110 b3
->right
.SameAs (frame
->panel
, wxRight
, 5);
111 b3
->bottom
.SameAs (frame
->panel
, wxBottom
, 5);
112 mtext
->SetConstraints(b3
);
114 frame
->canvas
= new MyWindow(frame
, 0, 0, 400, 400, wxRETAINED
);
116 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
117 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
119 // Make a text window
120 frame
->text_window
= new MyTextWindow(frame
, 0, 250, 400, 250);
122 // Set constraints for panel subwindow
123 wxLayoutConstraints
*c1
= new wxLayoutConstraints
;
125 c1
->left
.SameAs (frame
, wxLeft
);
126 c1
->top
.SameAs (frame
, wxTop
);
127 c1
->right
.PercentOf (frame
, wxWidth
, 50);
128 c1
->height
.PercentOf (frame
, wxHeight
, 50);
130 frame
->panel
->SetConstraints(c1
);
132 // Set constraints for canvas subwindow
133 wxLayoutConstraints
*c2
= new wxLayoutConstraints
;
135 c2
->left
.SameAs (frame
->panel
, wxRight
);
136 c2
->top
.SameAs (frame
, wxTop
);
137 c2
->right
.SameAs (frame
, wxRight
);
138 c2
->height
.PercentOf (frame
, wxHeight
, 50);
140 frame
->canvas
->SetConstraints(c2
);
142 // Set constraints for text subwindow
143 wxLayoutConstraints
*c3
= new wxLayoutConstraints
;
144 c3
->left
.SameAs (frame
, wxLeft
);
145 c3
->top
.Below (frame
->panel
);
146 c3
->right
.SameAs (frame
, wxRight
);
147 c3
->bottom
.SameAs (frame
, wxBottom
);
149 frame
->text_window
->SetConstraints(c3
);
153 frame
->SetStatusText("wxWindows layout demo");
159 //-----------------------------------------------------------------
161 //-----------------------------------------------------------------
163 // Define my frame constructor
164 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
165 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
167 panel
= (wxPanel
*) NULL
;
168 text_window
= (MyTextWindow
*) NULL
;
169 canvas
= (MyWindow
*) NULL
;
172 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
173 EVT_MENU(LAYOUT_LOAD_FILE
, MyFrame::LoadFile
)
174 EVT_MENU(LAYOUT_QUIT
, MyFrame::Quit
)
175 EVT_MENU(LAYOUT_TEST
, MyFrame::TestSizers
)
176 EVT_MENU(LAYOUT_TEST_NEW
, MyFrame::TestNewSizers
)
177 EVT_MENU(LAYOUT_ABOUT
, MyFrame::About
)
178 EVT_SIZE(MyFrame::OnSize
)
181 void MyFrame::LoadFile(wxCommandEvent
& WXUNUSED(event
) )
183 wxString s
= wxFileSelector( _T("Load text file"), (const wxChar
*) NULL
,
184 (const wxChar
*) NULL
, (const wxChar
*) NULL
, _T("*.txt") );
188 frame
->text_window
->LoadFile(s
);
193 void MyFrame::Quit(wxCommandEvent
& WXUNUSED(event
) )
198 void MyFrame::TestSizers(wxCommandEvent
& WXUNUSED(event
) )
200 SizerFrame
*newFrame
= new SizerFrame((MyFrame
*) NULL
, "Sizer Test Frame", 50, 50, 500, 500);
201 newFrame
->Show(TRUE
);
204 void MyFrame::TestNewSizers(wxCommandEvent
& WXUNUSED(event
) )
206 NewSizerFrame
*newFrame
= new NewSizerFrame((MyFrame
*) NULL
, "Sizer Test Frame", 50, 50 );
207 newFrame
->Show(TRUE
);
210 void MyFrame::About(wxCommandEvent
& WXUNUSED(event
) )
212 (void)wxMessageBox("wxWindows GUI library layout demo\n",
213 "About Layout Demo", wxOK
|wxCENTRE
);
216 // Size the subwindows when the frame is resized
217 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
) )
222 void MyFrame::Draw(wxDC
& dc
, bool WXUNUSED(draw_bitmaps
) )
224 dc
.SetPen(* wxGREEN_PEN
);
225 dc
.DrawLine(0, 0, 200, 200);
226 dc
.DrawLine(200, 0, 0, 200);
228 dc
.SetBrush(* wxCYAN_BRUSH
);
229 dc
.SetPen(* wxRED_PEN
);
231 dc
.DrawRectangle(100, 100, 100, 50);
232 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
234 dc
.DrawEllipse(250, 250, 100, 50);
235 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
236 dc
.DrawLine(50, 230, 200, 230);
238 dc
.SetPen(* wxBLACK_PEN
);
239 dc
.DrawArc(50, 300, 100, 250, 100, 300 );
242 //-----------------------------------------------------------------
244 //-----------------------------------------------------------------
246 BEGIN_EVENT_TABLE(MyWindow
, wxWindow
)
247 EVT_PAINT(MyWindow::OnPaint
)
250 // Define a constructor for my canvas
251 MyWindow::MyWindow(wxFrame
*frame
, int x
, int y
, int w
, int h
, long style
):
252 wxWindow(frame
, -1, wxPoint(x
, y
), wxSize(w
, h
), style
)
256 MyWindow::~MyWindow(void)
260 // Define the repainting behaviour
261 void MyWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
264 frame
->Draw(dc
,TRUE
);
267 //-----------------------------------------------------------------
269 //-----------------------------------------------------------------
271 SizerFrame::SizerFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
272 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
274 panel
= new wxPanel(this, -1, wxPoint(0, 0), wxSize(-1, -1), wxTAB_TRAVERSAL
);
275 panel
->SetBackgroundColour(wxColour(192, 192, 192));
277 // A sizer to fit the whole panel, plus two sizers, one
278 // above the other. A button is centred on the lower
279 // sizer; a rowcol containing 3 buttons is centred on the upper
281 wxSizer
*expandSizer
= new wxSizer(panel
, wxSizerExpand
);
282 expandSizer
->SetName("expandSizer");
284 wxLayoutConstraints
*c
;
288 wxSizer
*topSizer
= new wxSizer(expandSizer
);
289 topSizer
->SetName("topSizer");
291 // Specify constraints for the top sizer
292 c
= new wxLayoutConstraints
;
293 c
->left
.SameAs (expandSizer
, wxLeft
);
294 c
->top
.SameAs (expandSizer
, wxTop
);
295 c
->right
.SameAs (expandSizer
, wxRight
);
296 c
->height
.PercentOf (expandSizer
, wxHeight
, 50);
298 topSizer
->SetConstraints(c
);
301 * Add a row-col sizer and some buttons
304 // Default is layout by rows, 20 columns per row, shrink to fit.
305 wxRowColSizer
*rowCol
= new wxRowColSizer(topSizer
);
306 rowCol
->SetName("rowCol");
308 wxButton
*button
= new wxButton(panel
, -1, "Button 1");
309 rowCol
->AddSizerChild(button
);
311 button
= new wxButton(panel
, -1, "Button 2");
312 rowCol
->AddSizerChild(button
);
314 button
= new wxButton(panel
, -1, "Button 3");
315 rowCol
->AddSizerChild(button
);
317 // Centre the rowcol in the middle of the upper sizer
318 c
= new wxLayoutConstraints
;
319 c
->centreX
.SameAs (topSizer
, wxCentreX
);
320 c
->centreY
.SameAs (topSizer
, wxCentreY
);
323 rowCol
->SetConstraints(c
);
325 /////// BOTTOM OF PANEL
327 wxSizer
*bottomSizer
= new wxSizer(expandSizer
);
329 // Specify constraints for the bottom sizer
330 c
= new wxLayoutConstraints
;
331 c
->left
.SameAs (expandSizer
, wxLeft
);
332 c
->top
.PercentOf (expandSizer
, wxHeight
, 50);
333 c
->right
.SameAs (expandSizer
, wxRight
);
334 c
->height
.PercentOf (expandSizer
, wxHeight
, 50);
336 bottomSizer
->SetConstraints(c
);
338 wxButton
*button2
= new wxButton(panel
, -1, "Test button");
340 // The button should be a child of the bottom sizer
341 bottomSizer
->AddSizerChild(button2
);
343 // Centre the button on the sizer
344 c
= new wxLayoutConstraints
;
345 c
->centreX
.SameAs (bottomSizer
, wxCentreX
);
346 c
->centreY
.SameAs (bottomSizer
, wxCentreY
);
347 c
->width
.PercentOf (bottomSizer
, wxWidth
, 20);
348 c
->height
.PercentOf (bottomSizer
, wxHeight
, 20);
349 button2
->SetConstraints(c
);
352 BEGIN_EVENT_TABLE(SizerFrame
, wxFrame
)
353 EVT_SIZE(SizerFrame::OnSize
)
357 // Size the subwindows when the frame is resized
358 void SizerFrame::OnSize(wxSizeEvent
& event
)
360 wxFrame::OnSize(event
);
364 //-----------------------------------------------------------------
366 //-----------------------------------------------------------------
368 NewSizerFrame::NewSizerFrame(wxFrame
*frame
, char *title
, int x
, int y
):
369 wxFrame(frame
, -1, title
, wxPoint(x
, y
) )
371 // we want to get a dialog that is stretchable because it
372 // has a text ctrl in the middle. at the bottom, we have
373 // two buttons which are not supposed to get stretched
374 // and therefore we insert two spacers next to them
376 topsizer
= new wxBoxNewSizer( wxVERTICAL
);
379 // 1) upper part: text ctrl
381 // make border around textctrl in all directions
382 wxBorderNewSizer
*text_border
= new wxBorderNewSizer();
384 // make border around text ctrl 20 pixels wide
385 // minimum size for the text ctrl is 60x30
386 text_border
->Add( new wxTextCtrl( this, -1, "My text.", wxDefaultPosition
, wxSize(170,30), wxTE_MULTILINE
), 5 );
388 // add text ctrl with border to top sizer
389 // a value of more than zero indicates that it's stretchable
390 topsizer
->Add( text_border
, 1 );
393 // 2) middle part: static line
395 // make border for beauty static line
396 wxBorderNewSizer
*line_border
= new wxBorderNewSizer();
398 // make border around static line 2 pixels wide
399 // minimum size for the static line is 3x3
400 line_border
->Add( new wxStaticLine( this, -1, wxDefaultPosition
, wxSize(170,3), wxHORIZONTAL
), 5 );
402 // add text ctrl with border to top sizer
403 topsizer
->Add( line_border
);
406 // 3) bottom: buttons
408 // make border around button in all directions
409 wxBoxNewSizer
*button_sizer
= new wxBoxNewSizer( wxHORIZONTAL
);
411 // make border around buttons 5 pixels wide
412 // minimum size for the button is its default size
413 wxBorderNewSizer
*button1_border
= new wxBorderNewSizer();
414 button1_border
->Add( new wxButton( this, -1, "Hello 1", wxDefaultPosition
, wxSize(80,30) ), 5 );
415 button_sizer
->Add( button1_border
);
417 wxBorderNewSizer
*button2_border
= new wxBorderNewSizer();
418 button2_border
->Add( new wxButton( this, -1, "Hello 2", wxDefaultPosition
, wxSize(80,30) ), 5 );
419 button_sizer
->Add( button2_border
);
421 // add buttons with border to top sizer
422 topsizer
->Add( button_sizer
);
425 // set frame to minimum size
426 topsizer
->Fit( this );
428 // don't allow frame to get smaller than what the sizers tell ye
429 topsizer
->SetSizeHints( this );
435 // This can later be removed if we integrate wxNewSizers
438 BEGIN_EVENT_TABLE(NewSizerFrame
, wxFrame
)
439 EVT_SIZE(NewSizerFrame::OnSize
)
442 void NewSizerFrame::OnSize(wxSizeEvent
& event
)
444 wxFrame::OnSize(event
);
446 wxSize
client_size( GetClientSize() );
448 topsizer
->SetDimension( 0, 0, client_size
.x
, client_size
.y
);