]>
git.saurik.com Git - wxWidgets.git/blob - samples/layout/layout.cpp
fa31e8194eb1d61c6717efce9692978029fc7ef2
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!
33 MyFrame
*frame
= (MyFrame
*) NULL
;
34 wxMenuBar
*menu_bar
= (wxMenuBar
*) NULL
;
42 bool MyApp::OnInit(void)
44 // Create the main frame window
45 frame
= new MyFrame((MyFrame
*) NULL
, (char *) "wxWindows Layout Demo", 0, 0, 550, 500);
47 frame
->SetAutoLayout(TRUE
);
49 // Give it a status line
50 frame
->CreateStatusBar(2);
53 wxMenu
*file_menu
= new wxMenu
;
55 file_menu
->Append(LAYOUT_LOAD_FILE
, "&Load file", "Load a text file");
56 file_menu
->Append(LAYOUT_TEST
, "&Test sizers", "Test sizer code");
57 file_menu
->Append(LAYOUT_TEST_NEW
, "&Test new sizers", "Test new sizer code");
59 file_menu
->AppendSeparator();
60 file_menu
->Append(LAYOUT_QUIT
, "E&xit", "Quit program");
62 wxMenu
*help_menu
= new wxMenu
;
63 help_menu
->Append(LAYOUT_ABOUT
, "&About", "About layout demo");
65 menu_bar
= new wxMenuBar
;
67 menu_bar
->Append(file_menu
, "&File");
68 menu_bar
->Append(help_menu
, "&Help");
70 // Associate the menu bar with the frame
71 frame
->SetMenuBar(menu_bar
);
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);
78 // Create some panel items
79 wxButton
*btn1
= new wxButton(frame
->panel
, -1, "A button (1)") ;
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
);
88 wxListBox
*list
= new wxListBox(frame
->panel
, -1,
89 wxPoint(-1, -1), wxSize(200, 100));
90 list
->Append("Apple");
92 list
->Append("Orange");
93 list
->Append("Banana");
94 list
->Append("Fruit");
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
);
103 wxTextCtrl
*mtext
= new wxTextCtrl(frame
->panel
, -1, "Some text",
104 wxPoint(-1, -1), wxSize(150, 100));
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
);
113 frame
->canvas
= new MyWindow(frame
, 0, 0, 400, 400, wxRETAINED
);
115 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
116 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
118 // Make a text window
119 frame
->text_window
= new MyTextWindow(frame
, 0, 250, 400, 250);
121 // Set constraints for panel subwindow
122 wxLayoutConstraints
*c1
= new wxLayoutConstraints
;
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);
129 frame
->panel
->SetConstraints(c1
);
131 // Set constraints for canvas subwindow
132 wxLayoutConstraints
*c2
= new wxLayoutConstraints
;
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);
139 frame
->canvas
->SetConstraints(c2
);
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
);
148 frame
->text_window
->SetConstraints(c3
);
152 frame
->SetStatusText("wxWindows layout demo");
158 //-----------------------------------------------------------------
160 //-----------------------------------------------------------------
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
))
166 panel
= (wxPanel
*) NULL
;
167 text_window
= (MyTextWindow
*) NULL
;
168 canvas
= (MyWindow
*) NULL
;
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
, MyFrame::TestSizers
)
175 EVT_MENU(LAYOUT_TEST_NEW
, MyFrame::TestNewSizers
)
176 EVT_MENU(LAYOUT_ABOUT
, MyFrame::About
)
177 EVT_SIZE(MyFrame::OnSize
)
180 void MyFrame::LoadFile(wxCommandEvent
& WXUNUSED(event
) )
182 wxString s
= wxFileSelector( _T("Load text file"), (const wxChar
*) NULL
,
183 (const wxChar
*) NULL
, (const wxChar
*) NULL
, _T("*.txt") );
187 frame
->text_window
->LoadFile(s
);
192 void MyFrame::Quit(wxCommandEvent
& WXUNUSED(event
) )
197 void MyFrame::TestSizers(wxCommandEvent
& WXUNUSED(event
) )
199 SizerFrame
*newFrame
= new SizerFrame((MyFrame
*) NULL
, "Sizer Test Frame", 50, 50, 500, 500);
200 newFrame
->Show(TRUE
);
203 void MyFrame::TestNewSizers(wxCommandEvent
& WXUNUSED(event
) )
205 NewSizerFrame
*newFrame
= new NewSizerFrame((MyFrame
*) NULL
, "Sizer Test Frame", 50, 50 );
206 newFrame
->Show(TRUE
);
209 void MyFrame::About(wxCommandEvent
& WXUNUSED(event
) )
211 (void)wxMessageBox("wxWindows GUI library layout demo\n",
212 "About Layout Demo", wxOK
|wxCENTRE
);
215 // Size the subwindows when the frame is resized
216 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
) )
221 void MyFrame::Draw(wxDC
& dc
, bool WXUNUSED(draw_bitmaps
) )
223 dc
.SetPen(* wxGREEN_PEN
);
224 dc
.DrawLine(0, 0, 200, 200);
225 dc
.DrawLine(200, 0, 0, 200);
227 dc
.SetBrush(* wxCYAN_BRUSH
);
228 dc
.SetPen(* wxRED_PEN
);
230 dc
.DrawRectangle(100, 100, 100, 50);
231 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
233 dc
.DrawEllipse(250, 250, 100, 50);
234 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
235 dc
.DrawLine(50, 230, 200, 230);
237 dc
.SetPen(* wxBLACK_PEN
);
238 dc
.DrawArc(50, 300, 100, 250, 100, 300 );
241 //-----------------------------------------------------------------
243 //-----------------------------------------------------------------
245 BEGIN_EVENT_TABLE(MyWindow
, wxWindow
)
246 EVT_PAINT(MyWindow::OnPaint
)
249 // Define a constructor for my canvas
250 MyWindow::MyWindow(wxFrame
*frame
, int x
, int y
, int w
, int h
, long style
):
251 wxWindow(frame
, -1, wxPoint(x
, y
), wxSize(w
, h
), style
)
255 MyWindow::~MyWindow(void)
259 // Define the repainting behaviour
260 void MyWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
263 frame
->Draw(dc
,TRUE
);
266 //-----------------------------------------------------------------
268 //-----------------------------------------------------------------
270 SizerFrame::SizerFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
271 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
273 panel
= new wxPanel(this, -1, wxPoint(0, 0), wxSize(-1, -1), wxTAB_TRAVERSAL
);
274 panel
->SetBackgroundColour(wxColour(192, 192, 192));
276 // A sizer to fit the whole panel, plus two sizers, one
277 // above the other. A button is centred on the lower
278 // sizer; a rowcol containing 3 buttons is centred on the upper
280 wxSizer
*expandSizer
= new wxSizer(panel
, wxSizerExpand
);
281 expandSizer
->SetName("expandSizer");
283 wxLayoutConstraints
*c
;
287 wxSizer
*topSizer
= new wxSizer(expandSizer
);
288 topSizer
->SetName("topSizer");
290 // Specify constraints for the top sizer
291 c
= new wxLayoutConstraints
;
292 c
->left
.SameAs (expandSizer
, wxLeft
);
293 c
->top
.SameAs (expandSizer
, wxTop
);
294 c
->right
.SameAs (expandSizer
, wxRight
);
295 c
->height
.PercentOf (expandSizer
, wxHeight
, 50);
297 topSizer
->SetConstraints(c
);
300 * Add a row-col sizer and some buttons
303 // Default is layout by rows, 20 columns per row, shrink to fit.
304 wxRowColSizer
*rowCol
= new wxRowColSizer(topSizer
);
305 rowCol
->SetName("rowCol");
307 wxButton
*button
= new wxButton(panel
, -1, "Button 1");
308 rowCol
->AddSizerChild(button
);
310 button
= new wxButton(panel
, -1, "Button 2");
311 rowCol
->AddSizerChild(button
);
313 button
= new wxButton(panel
, -1, "Button 3");
314 rowCol
->AddSizerChild(button
);
316 // Centre the rowcol in the middle of the upper sizer
317 c
= new wxLayoutConstraints
;
318 c
->centreX
.SameAs (topSizer
, wxCentreX
);
319 c
->centreY
.SameAs (topSizer
, wxCentreY
);
322 rowCol
->SetConstraints(c
);
324 /////// BOTTOM OF PANEL
326 wxSizer
*bottomSizer
= new wxSizer(expandSizer
);
328 // Specify constraints for the bottom sizer
329 c
= new wxLayoutConstraints
;
330 c
->left
.SameAs (expandSizer
, wxLeft
);
331 c
->top
.PercentOf (expandSizer
, wxHeight
, 50);
332 c
->right
.SameAs (expandSizer
, wxRight
);
333 c
->height
.PercentOf (expandSizer
, wxHeight
, 50);
335 bottomSizer
->SetConstraints(c
);
337 wxButton
*button2
= new wxButton(panel
, -1, "Test button");
339 // The button should be a child of the bottom sizer
340 bottomSizer
->AddSizerChild(button2
);
342 // Centre the button on the sizer
343 c
= new wxLayoutConstraints
;
344 c
->centreX
.SameAs (bottomSizer
, wxCentreX
);
345 c
->centreY
.SameAs (bottomSizer
, wxCentreY
);
346 c
->width
.PercentOf (bottomSizer
, wxWidth
, 20);
347 c
->height
.PercentOf (bottomSizer
, wxHeight
, 20);
348 button2
->SetConstraints(c
);
351 BEGIN_EVENT_TABLE(SizerFrame
, wxFrame
)
352 EVT_SIZE(SizerFrame::OnSize
)
356 // Size the subwindows when the frame is resized
357 void SizerFrame::OnSize(wxSizeEvent
& event
)
359 wxFrame::OnSize(event
);
363 //-----------------------------------------------------------------
365 //-----------------------------------------------------------------
367 NewSizerFrame::NewSizerFrame(wxFrame
*frame
, char *title
, int x
, int y
):
368 wxFrame(frame
, -1, title
, wxPoint(x
, y
) )
370 // no extra options means border all around
371 topsizer
= new wxBorderNewSizer();
373 // make border 20 pixels wide
374 topsizer
->Add( new wxButton( this, -1, "Hello" ), 20 );
376 // set frame to minimum size
377 topsizer
->Fit( this );
379 // don't allow frame to get smaller than what the sizers tell ye
380 topsizer
->SetSizeHints( this );
386 // This can later be removed if we integrate wxNewSizers
389 BEGIN_EVENT_TABLE(NewSizerFrame
, wxFrame
)
390 EVT_SIZE(NewSizerFrame::OnSize
)
393 void NewSizerFrame::OnSize(wxSizeEvent
& event
)
395 wxFrame::OnSize(event
);
397 wxSize
client_size( GetClientSize() );
399 topsizer
->SetDimension( 0, 0, client_size
.x
, client_size
.y
);