]>
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"
23 #if !wxUSE_CONSTRAINTS
24 #error You must set wxUSE_CONSTRAINTS to 1 in setup.h!
30 #include "wx/statline.h"
31 #include "wx/notebook.h"
36 MyFrame
*frame
= (MyFrame
*) NULL
;
37 wxMenuBar
*menu_bar
= (wxMenuBar
*) NULL
;
47 // Create the main frame window
48 frame
= new MyFrame(NULL
, _T("wxWindows Layout Demo"), -1, -1, 400, 300);
50 frame
->SetAutoLayout(TRUE
);
52 // Give it a status line
53 frame
->CreateStatusBar(2);
56 wxMenu
*file_menu
= new wxMenu
;
58 file_menu
->Append(LAYOUT_TEST_SIZER
, _T("&Test sizers"), _T("Test sizer"));
59 file_menu
->Append(LAYOUT_TEST_NB
, _T("&Test notebook sizers"), _T("Test notebook sizer"));
61 file_menu
->AppendSeparator();
62 file_menu
->Append(LAYOUT_QUIT
, _T("E&xit"), _T("Quit program"));
64 wxMenu
*help_menu
= new wxMenu
;
65 help_menu
->Append(LAYOUT_ABOUT
, _T("&About"), _T("About layout demo"));
67 menu_bar
= new wxMenuBar
;
69 menu_bar
->Append(file_menu
, _T("&File"));
70 menu_bar
->Append(help_menu
, _T("&Help"));
72 // Associate the menu bar with the frame
73 frame
->SetMenuBar(menu_bar
);
76 wxPanel
*panel
= new wxPanel(frame
);
78 // Create some panel items
79 wxButton
*btn1
= new wxButton(panel
, -1, _T("A button (1)")) ;
81 wxLayoutConstraints
*b1
= new wxLayoutConstraints
;
82 b1
->centreX
.SameAs (panel
, wxCentreX
);
83 b1
->top
.SameAs (panel
, wxTop
, 5);
84 b1
->width
.PercentOf (panel
, wxWidth
, 80);
86 btn1
->SetConstraints(b1
);
88 wxListBox
*list
= new wxListBox(panel
, -1,
89 wxPoint(-1, -1), wxSize(200, 100));
90 list
->Append(_T("Apple"));
91 list
->Append(_T("Pear"));
92 list
->Append(_T("Orange"));
93 list
->Append(_T("Banana"));
94 list
->Append(_T("Fruit"));
96 wxLayoutConstraints
*b2
= new wxLayoutConstraints
;
97 b2
->top
.Below (btn1
, 5);
98 b2
->left
.SameAs (panel
, wxLeft
, 5);
99 b2
->width
.PercentOf (panel
, wxWidth
, 40);
100 b2
->bottom
.SameAs (panel
, wxBottom
, 5);
101 list
->SetConstraints(b2
);
103 wxTextCtrl
*mtext
= new wxTextCtrl(panel
, -1, _T("Some text"));
105 wxLayoutConstraints
*b3
= new wxLayoutConstraints
;
106 b3
->top
.Below (btn1
, 5);
107 b3
->left
.RightOf (list
, 5);
108 b3
->right
.SameAs (panel
, wxRight
, 5);
109 b3
->bottom
.SameAs (panel
, wxBottom
, 5);
110 mtext
->SetConstraints(b3
);
112 MyWindow
*canvas
= new MyWindow(frame
, 0, 0, 400, 400, wxRETAINED
);
114 // Make a text window
115 MyTextWindow
*text_window
= new MyTextWindow(frame
, 0, 250, 400, 150);
117 // Set constraints for panel subwindow
118 wxLayoutConstraints
*c1
= new wxLayoutConstraints
;
120 c1
->left
.SameAs (frame
, wxLeft
);
121 c1
->top
.SameAs (frame
, wxTop
);
122 c1
->right
.PercentOf (frame
, wxWidth
, 50);
123 c1
->height
.PercentOf (frame
, wxHeight
, 50);
125 panel
->SetConstraints(c1
);
127 // Set constraints for canvas subwindow
128 wxLayoutConstraints
*c2
= new wxLayoutConstraints
;
130 c2
->left
.SameAs (panel
, wxRight
);
131 c2
->top
.SameAs (frame
, wxTop
);
132 c2
->right
.SameAs (frame
, wxRight
);
133 c2
->height
.PercentOf (frame
, wxHeight
, 50);
135 canvas
->SetConstraints(c2
);
137 // Set constraints for text subwindow
138 wxLayoutConstraints
*c3
= new wxLayoutConstraints
;
139 c3
->left
.SameAs (frame
, wxLeft
);
140 c3
->top
.Below (panel
);
141 c3
->right
.SameAs (frame
, wxRight
);
142 c3
->bottom
.SameAs (frame
, wxBottom
);
144 text_window
->SetConstraints(c3
);
148 frame
->SetStatusText(_T("wxWindows layout demo"));
155 //-----------------------------------------------------------------
157 //-----------------------------------------------------------------
159 // Define my frame constructor
160 MyFrame::MyFrame(wxFrame
*frame
, const wxChar
*title
, int x
, int y
, int w
, int h
)
161 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
165 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
166 EVT_MENU(LAYOUT_QUIT
, MyFrame::OnQuit
)
167 EVT_MENU(LAYOUT_TEST_SIZER
, MyFrame::TestSizers
)
168 EVT_MENU(LAYOUT_TEST_NB
, MyFrame::TestNotebookSizers
)
169 EVT_MENU(LAYOUT_ABOUT
, MyFrame::About
)
172 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
177 void MyFrame::TestSizers(wxCommandEvent
& WXUNUSED(event
) )
179 MySizerFrame
*newFrame
= new MySizerFrame(NULL
, _T("Sizer Test Frame"), 50, 50);
180 newFrame
->Show(TRUE
);
183 void MyFrame::TestNotebookSizers(wxCommandEvent
& WXUNUSED(event
) )
185 wxDialog
dialog( this, -1, wxString(_T("Notebook Sizer Test Dialog")) );
187 // Begin with first hierarchy: a notebook at the top and
188 // and OK button at the bottom.
190 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
192 wxNotebook
*notebook
= new wxNotebook( &dialog
, -1 );
193 wxNotebookSizer
*nbs
= new wxNotebookSizer( notebook
);
194 topsizer
->Add( nbs
, 1, wxGROW
);
196 wxButton
*button
= new wxButton( &dialog
, wxID_OK
, _T("OK") );
197 topsizer
->Add( button
, 0, wxALIGN_RIGHT
| wxALL
, 10 );
199 // First page: one big text ctrl
200 wxTextCtrl
*multi
= new wxTextCtrl( notebook
, -1, _T("TextCtrl."), wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
201 notebook
->AddPage( multi
, _T("Page One") );
203 // Second page: a text ctrl and a button
204 wxPanel
*panel
= new wxPanel( notebook
, -1 );
205 notebook
->AddPage( panel
, _T("Page Two") );
207 wxSizer
*panelsizer
= new wxBoxSizer( wxVERTICAL
);
209 wxTextCtrl
*text
= new wxTextCtrl( panel
, -1, _T("TextLine 1."), wxDefaultPosition
, wxSize(250,-1) );
210 panelsizer
->Add( text
, 0, wxGROW
|wxALL
, 30 );
211 text
= new wxTextCtrl( panel
, -1, _T("TextLine 2."), wxDefaultPosition
, wxSize(250,-1) );
212 panelsizer
->Add( text
, 0, wxGROW
|wxALL
, 30 );
213 wxButton
*button2
= new wxButton( panel
, -1, _T("Hallo") );
214 panelsizer
->Add( button2
, 0, wxALIGN_RIGHT
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 30 );
216 panel
->SetAutoLayout( TRUE
);
217 panel
->SetSizer( panelsizer
);
219 // Tell dialog to use sizer
221 dialog
.SetAutoLayout( TRUE
);
222 dialog
.SetSizer( topsizer
);
223 topsizer
->Fit( &dialog
);
224 topsizer
->SetSizeHints( &dialog
);
230 void MyFrame::About(wxCommandEvent
& WXUNUSED(event
) )
232 (void)wxMessageBox(_T("wxWindows GUI library layout demo\n"),
233 _T("About Layout Demo"), wxOK
|wxCENTRE
);
236 //-----------------------------------------------------------------
238 //-----------------------------------------------------------------
240 BEGIN_EVENT_TABLE(MyWindow
, wxWindow
)
241 EVT_PAINT(MyWindow::OnPaint
)
244 // Define a constructor for my canvas
245 MyWindow::MyWindow(wxFrame
*frame
, int x
, int y
, int w
, int h
, long style
)
246 : wxWindow(frame
, -1, wxPoint(x
, y
), wxSize(w
, h
), style
)
250 MyWindow::~MyWindow()
254 // Define the repainting behaviour
255 void MyWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
258 dc
.SetPen(* wxGREEN_PEN
);
259 dc
.DrawLine(0, 0, 200, 200);
260 dc
.DrawLine(200, 0, 0, 200);
262 dc
.SetBrush(* wxCYAN_BRUSH
);
263 dc
.SetPen(* wxRED_PEN
);
265 dc
.DrawRectangle(100, 100, 100, 50);
266 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
268 dc
.DrawEllipse(250, 250, 100, 50);
270 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
271 #endif // wxUSE_SPLINES
272 dc
.DrawLine(50, 230, 200, 230);
274 dc
.SetPen(* wxBLACK_PEN
);
275 dc
.DrawArc(50, 300, 100, 250, 100, 300 );
278 //-----------------------------------------------------------------
280 //-----------------------------------------------------------------
282 MySizerFrame::MySizerFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
)
283 : wxFrame(frame
, -1, title
, wxPoint(x
, y
) )
285 // we want to get a dialog that is stretchable because it
286 // has a text ctrl in the middle. at the bottom, we have
287 // two buttons which.
289 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
291 // 1) top: create wxStaticText with minimum size equal to its default size
293 new wxStaticText( this, -1, _T("An explanation (wxALIGN_RIGHT).") ),
294 0, // make vertically unstretchable
295 wxALIGN_RIGHT
| // right align text
296 wxTOP
| wxLEFT
| wxRIGHT
, // make border all around except wxBOTTOM
297 5 ); // set border width to 5
299 // 2) top: create wxTextCtrl with minimum size (100x60)
301 new wxTextCtrl( this, -1, _T("My text (wxEXPAND)."), wxDefaultPosition
, wxSize(100,60), wxTE_MULTILINE
),
302 1, // make vertically stretchable
303 wxEXPAND
| // make horizontally stretchable
304 wxALL
, // and make border all around
305 5 ); // set border width to 5
307 // 2.5) Gratuitous test of wxStaticBoxSizers
308 wxBoxSizer
*statsizer
= new wxStaticBoxSizer(
309 new wxStaticBox(this, -1, _T("A wxStaticBoxSizer")),
312 new wxStaticText(this, -1, _T("And some TEXT inside it")),
317 topsizer
->Add(statsizer
, 1, wxEXPAND
| wxALL
, 10);
319 // 2.7) And a test of wxGridSizer
320 wxGridSizer
*gridsizer
= new wxGridSizer(2, 5, 5);
321 gridsizer
->Add(new wxStaticText(this, -1, _T("Label")), 0,
322 wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
);
323 gridsizer
->Add(new wxTextCtrl(this, -1, _T("Grid sizer demo")), 1,
324 wxGROW
| wxALIGN_CENTER_VERTICAL
);
325 gridsizer
->Add(new wxStaticText(this, -1, _T("Another label")), 0,
326 wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
);
327 gridsizer
->Add(new wxTextCtrl(this, -1, _T("More text")), 1,
328 wxGROW
| wxALIGN_CENTER_VERTICAL
);
329 gridsizer
->Add(new wxStaticText(this, -1, _T("Final label")), 0,
330 wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
);
331 gridsizer
->Add(new wxTextCtrl(this, -1, _T("And yet more text")), 1,
332 wxGROW
| wxALIGN_CENTER_VERTICAL
);
333 topsizer
->Add(gridsizer
, 1, wxGROW
| wxALL
, 10);
336 // 3) middle: create wxStaticLine with minimum size (3x3)
338 new wxStaticLine( this, -1, wxDefaultPosition
, wxSize(3,3), wxHORIZONTAL
),
339 0, // make vertically unstretchable
340 wxEXPAND
| // make horizontally stretchable
341 wxALL
, // and make border all around
342 5 ); // set border width to 5
345 // 4) bottom: create two centred wxButtons
346 wxBoxSizer
*button_box
= new wxBoxSizer( wxHORIZONTAL
);
348 new wxButton( this, -1, _T("Two buttons in a box") ),
349 0, // make horizontally unstretchable
350 wxALL
, // make border all around
351 7 ); // set border width to 7
353 new wxButton( this, -1, _T("(wxCENTER)") ),
354 0, // make horizontally unstretchable
355 wxALL
, // make border all around
356 7 ); // set border width to 7
360 0, // make vertically unstretchable
361 wxCENTER
); // no border and centre horizontally
363 // don't allow frame to get smaller than what the sizers tell it and also set
364 // the initial size as calculated by the sizers
365 topsizer
->SetSizeHints( this );
367 SetSizer( topsizer
);