]>
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((MyFrame
*) NULL
, (char *) "wxWindows Layout Demo", 0, 0, 550, 500);
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_LOAD_FILE
, "&Load file", "Load a text file");
59 file_menu
->Append(LAYOUT_TEST_SIZER
, "&Test sizers", "Test sizer");
60 file_menu
->Append(LAYOUT_TEST_NB
, "&Test notebook sizers", "Test notebook sizer");
62 file_menu
->AppendSeparator();
63 file_menu
->Append(LAYOUT_QUIT
, "E&xit", "Quit program");
65 wxMenu
*help_menu
= new wxMenu
;
66 help_menu
->Append(LAYOUT_ABOUT
, "&About", "About layout demo");
68 menu_bar
= new wxMenuBar
;
70 menu_bar
->Append(file_menu
, "&File");
71 menu_bar
->Append(help_menu
, "&Help");
73 // Associate the menu bar with the frame
74 frame
->SetMenuBar(menu_bar
);
77 frame
->panel
= new wxPanel(frame
, 0, 0, 1000, 500, wxTAB_TRAVERSAL
);
78 frame
->panel
->SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
79 // frame->panel->SetAutoLayout(TRUE);
81 // Create some panel items
82 wxButton
*btn1
= new wxButton(frame
->panel
, -1, "A button (1)") ;
84 wxLayoutConstraints
*b1
= new wxLayoutConstraints
;
85 b1
->centreX
.SameAs (frame
->panel
, wxCentreX
);
86 b1
->top
.SameAs (frame
->panel
, wxTop
, 5);
87 b1
->width
.PercentOf (frame
->panel
, wxWidth
, 80);
88 b1
->height
.PercentOf (frame
->panel
, wxHeight
, 10);
89 btn1
->SetConstraints(b1
);
91 wxListBox
*list
= new wxListBox(frame
->panel
, -1,
92 wxPoint(-1, -1), wxSize(200, 100));
93 list
->Append("Apple");
95 list
->Append("Orange");
96 list
->Append("Banana");
97 list
->Append("Fruit");
99 wxLayoutConstraints
*b2
= new wxLayoutConstraints
;
100 b2
->top
.Below (btn1
, 5);
101 b2
->left
.SameAs (frame
->panel
, wxLeft
, 5);
102 b2
->width
.PercentOf (frame
->panel
, wxWidth
, 40);
103 b2
->bottom
.SameAs (frame
->panel
, wxBottom
, 5);
104 list
->SetConstraints(b2
);
106 wxTextCtrl
*mtext
= new wxTextCtrl(frame
->panel
, -1, "Some text",
107 wxPoint(-1, -1), wxSize(150, 100));
109 wxLayoutConstraints
*b3
= new wxLayoutConstraints
;
110 b3
->top
.Below (btn1
, 5);
111 b3
->left
.RightOf (list
, 5);
112 b3
->right
.SameAs (frame
->panel
, wxRight
, 5);
113 b3
->bottom
.SameAs (frame
->panel
, wxBottom
, 5);
114 mtext
->SetConstraints(b3
);
116 frame
->canvas
= new MyWindow(frame
, 0, 0, 400, 400, wxRETAINED
);
118 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
119 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
121 // Make a text window
122 frame
->text_window
= new MyTextWindow(frame
, 0, 250, 400, 250);
124 // Set constraints for panel subwindow
125 wxLayoutConstraints
*c1
= new wxLayoutConstraints
;
127 c1
->left
.SameAs (frame
, wxLeft
);
128 c1
->top
.SameAs (frame
, wxTop
);
129 c1
->right
.PercentOf (frame
, wxWidth
, 50);
130 c1
->height
.PercentOf (frame
, wxHeight
, 50);
132 frame
->panel
->SetConstraints(c1
);
134 // Set constraints for canvas subwindow
135 wxLayoutConstraints
*c2
= new wxLayoutConstraints
;
137 c2
->left
.SameAs (frame
->panel
, wxRight
);
138 c2
->top
.SameAs (frame
, wxTop
);
139 c2
->right
.SameAs (frame
, wxRight
);
140 c2
->height
.PercentOf (frame
, wxHeight
, 50);
142 frame
->canvas
->SetConstraints(c2
);
144 // Set constraints for text subwindow
145 wxLayoutConstraints
*c3
= new wxLayoutConstraints
;
146 c3
->left
.SameAs (frame
, wxLeft
);
147 c3
->top
.Below (frame
->panel
);
148 c3
->right
.SameAs (frame
, wxRight
);
149 c3
->bottom
.SameAs (frame
, wxBottom
);
151 frame
->text_window
->SetConstraints(c3
);
155 frame
->SetStatusText("wxWindows layout demo");
161 //-----------------------------------------------------------------
163 //-----------------------------------------------------------------
165 // Define my frame constructor
166 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
167 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
169 panel
= (wxPanel
*) NULL
;
170 text_window
= (MyTextWindow
*) NULL
;
171 canvas
= (MyWindow
*) NULL
;
174 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
175 EVT_MENU(LAYOUT_LOAD_FILE
, MyFrame::LoadFile
)
176 EVT_MENU(LAYOUT_QUIT
, MyFrame::Quit
)
177 EVT_MENU(LAYOUT_TEST_SIZER
, MyFrame::TestSizers
)
178 EVT_MENU(LAYOUT_TEST_NB
, MyFrame::TestNotebookSizers
)
179 EVT_MENU(LAYOUT_ABOUT
, MyFrame::About
)
180 EVT_SIZE(MyFrame::OnSize
)
183 void MyFrame::LoadFile(wxCommandEvent
& WXUNUSED(event
) )
185 wxString s
= wxFileSelector( _T("Load text file"), (const wxChar
*) NULL
,
186 (const wxChar
*) NULL
, (const wxChar
*) NULL
, _T("*.txt") );
190 frame
->text_window
->LoadFile(s
);
195 void MyFrame::Quit(wxCommandEvent
& WXUNUSED(event
) )
200 void MyFrame::TestSizers(wxCommandEvent
& WXUNUSED(event
) )
202 MySizerFrame
*newFrame
= new MySizerFrame((MyFrame
*) NULL
, "Sizer Test Frame", 50, 50 );
203 newFrame
->Show(TRUE
);
206 void MyFrame::TestNotebookSizers(wxCommandEvent
& WXUNUSED(event
) )
208 wxDialog
dialog( this, -1, wxString("Notebook Sizer Test Dialog") );
210 // Begin with first hierarchy: a notebook at the top and
211 // and OK button at the bottom.
213 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
215 wxNotebook
*notebook
= new wxNotebook( &dialog
, -1 );
216 wxNotebookSizer
*nbs
= new wxNotebookSizer( notebook
);
217 topsizer
->Add( nbs
, 1, wxGROW
);
219 wxButton
*button
= new wxButton( &dialog
, wxID_OK
, "OK" );
220 topsizer
->Add( button
, 0, wxALIGN_RIGHT
| wxALL
, 10 );
222 // First page: one big text ctrl
223 wxTextCtrl
*multi
= new wxTextCtrl( notebook
, -1, "TextCtrl.", wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
224 notebook
->AddPage( multi
, "Page One" );
226 // Second page: a text ctrl and a button
227 wxPanel
*panel
= new wxPanel( notebook
, -1 );
228 notebook
->AddPage( panel
, "Page Two" );
230 wxSizer
*panelsizer
= new wxBoxSizer( wxVERTICAL
);
232 wxTextCtrl
*text
= new wxTextCtrl( panel
, -1, "TextLine 1.", wxDefaultPosition
, wxSize(250,-1) );
233 panelsizer
->Add( text
, 0, wxGROW
|wxALL
, 30 );
234 text
= new wxTextCtrl( panel
, -1, "TextLine 2.", wxDefaultPosition
, wxSize(250,-1) );
235 panelsizer
->Add( text
, 0, wxGROW
|wxALL
, 30 );
236 wxButton
*button2
= new wxButton( panel
, -1, "Hallo" );
237 panelsizer
->Add( button2
, 0, wxALIGN_RIGHT
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 30 );
239 panel
->SetAutoLayout( TRUE
);
240 panel
->SetSizer( panelsizer
);
242 // Tell dialog to use sizer
244 dialog
.SetAutoLayout( TRUE
);
245 dialog
.SetSizer( topsizer
);
246 topsizer
->Fit( &dialog
);
247 topsizer
->SetSizeHints( &dialog
);
253 void MyFrame::About(wxCommandEvent
& WXUNUSED(event
) )
255 (void)wxMessageBox("wxWindows GUI library layout demo\n",
256 "About Layout Demo", wxOK
|wxCENTRE
);
259 // Size the subwindows when the frame is resized
260 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
) )
265 void MyFrame::Draw(wxDC
& dc
, bool WXUNUSED(draw_bitmaps
) )
267 dc
.SetPen(* wxGREEN_PEN
);
268 dc
.DrawLine(0, 0, 200, 200);
269 dc
.DrawLine(200, 0, 0, 200);
271 dc
.SetBrush(* wxCYAN_BRUSH
);
272 dc
.SetPen(* wxRED_PEN
);
274 dc
.DrawRectangle(100, 100, 100, 50);
275 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
277 dc
.DrawEllipse(250, 250, 100, 50);
279 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
280 #endif // wxUSE_SPLINES
281 dc
.DrawLine(50, 230, 200, 230);
283 dc
.SetPen(* wxBLACK_PEN
);
284 dc
.DrawArc(50, 300, 100, 250, 100, 300 );
287 //-----------------------------------------------------------------
289 //-----------------------------------------------------------------
291 BEGIN_EVENT_TABLE(MyWindow
, wxWindow
)
292 EVT_PAINT(MyWindow::OnPaint
)
295 // Define a constructor for my canvas
296 MyWindow::MyWindow(wxFrame
*frame
, int x
, int y
, int w
, int h
, long style
):
297 wxWindow(frame
, -1, wxPoint(x
, y
), wxSize(w
, h
), style
)
301 MyWindow::~MyWindow()
305 // Define the repainting behaviour
306 void MyWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
309 frame
->Draw(dc
,TRUE
);
312 //-----------------------------------------------------------------
314 //-----------------------------------------------------------------
316 MySizerFrame::MySizerFrame(wxFrame
*frame
, char *title
, int x
, int y
):
317 wxFrame(frame
, -1, title
, wxPoint(x
, y
) )
319 // we want to get a dialog that is stretchable because it
320 // has a text ctrl in the middle. at the bottom, we have
321 // two buttons which.
323 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
325 // 1) top: create wxStaticText with minimum size equal to its default size
327 new wxStaticText( this, -1, "An explanation (wxALIGN_RIGHT)." ),
328 0, // make vertically unstretchable
329 wxALIGN_RIGHT
| // right align text
330 wxTOP
| wxLEFT
| wxRIGHT
, // make border all around except wxBOTTOM
331 5 ); // set border width to 5
333 // 2) top: create wxTextCtrl with minimum size (100x60)
335 new wxTextCtrl( this, -1, "My text (wxEXPAND).", wxDefaultPosition
, wxSize(100,60), wxTE_MULTILINE
),
336 1, // make vertically stretchable
337 wxEXPAND
| // make horizontally stretchable
338 wxALL
, // and make border all around
339 5 ); // set border width to 5
341 // 2.5) Gratuitous test of wxStaticBoxSizers
342 wxBoxSizer
*statsizer
= new wxStaticBoxSizer(
343 new wxStaticBox(this, -1, "A wxStaticBoxSizer"),
346 new wxStaticText(this, -1, "And some TEXT inside it"),
351 topsizer
->Add(statsizer
, 1, wxEXPAND
| wxALL
, 10);
353 // 2.7) And a test of wxGridSizer
354 wxGridSizer
*gridsizer
= new wxGridSizer(2, 5, 5);
355 gridsizer
->Add(new wxStaticText(this, -1, "Label"), 0,
356 wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
);
357 gridsizer
->Add(new wxTextCtrl(this, -1, "Grid sizer demo"), 1,
358 wxGROW
| wxALIGN_CENTER_VERTICAL
);
359 gridsizer
->Add(new wxStaticText(this, -1, "Another label"), 0,
360 wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
);
361 gridsizer
->Add(new wxTextCtrl(this, -1, "More text"), 1,
362 wxGROW
| wxALIGN_CENTER_VERTICAL
);
363 gridsizer
->Add(new wxStaticText(this, -1, "Final label"), 0,
364 wxALIGN_RIGHT
| wxALIGN_CENTER_VERTICAL
);
365 gridsizer
->Add(new wxTextCtrl(this, -1, "And yet more text"), 1,
366 wxGROW
| wxALIGN_CENTER_VERTICAL
);
367 topsizer
->Add(gridsizer
, 1, wxGROW
| wxALL
, 10);
370 // 3) middle: create wxStaticLine with minimum size (3x3)
372 new wxStaticLine( this, -1, wxDefaultPosition
, wxSize(3,3), wxHORIZONTAL
),
373 0, // make vertically unstretchable
374 wxEXPAND
| // make horizontally stretchable
375 wxALL
, // and make border all around
376 5 ); // set border width to 5
379 // 4) bottom: create two centred wxButtons
380 wxBoxSizer
*button_box
= new wxBoxSizer( wxHORIZONTAL
);
382 new wxButton( this, -1, "Two buttons in a box" ),
383 0, // make horizontally unstretchable
384 wxALL
, // make border all around
385 7 ); // set border width to 7
387 new wxButton( this, -1, "(wxCENTER)" ),
388 0, // make horizontally unstretchable
389 wxALL
, // make border all around
390 7 ); // set border width to 7
394 0, // make vertically unstretchable
395 wxCENTER
); // no border and centre horizontally
397 SetAutoLayout( TRUE
);
399 // set frame to minimum size
400 topsizer
->Fit( this );
402 // don't allow frame to get smaller than what the sizers tell ye
403 topsizer
->SetSizeHints( this );
405 SetSizer( topsizer
);