]>
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!
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_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_NEW
, MyFrame::TestNewSizers
)
175 EVT_MENU(LAYOUT_ABOUT
, MyFrame::About
)
176 EVT_SIZE(MyFrame::OnSize
)
179 void MyFrame::LoadFile(wxCommandEvent
& WXUNUSED(event
) )
181 wxString s
= wxFileSelector( _T("Load text file"), (const wxChar
*) NULL
,
182 (const wxChar
*) NULL
, (const wxChar
*) NULL
, _T("*.txt") );
186 frame
->text_window
->LoadFile(s
);
191 void MyFrame::Quit(wxCommandEvent
& WXUNUSED(event
) )
196 void MyFrame::TestNewSizers(wxCommandEvent
& WXUNUSED(event
) )
198 NewSizerFrame
*newFrame
= new NewSizerFrame((MyFrame
*) NULL
, "Sizer Test Frame", 50, 50 );
199 newFrame
->Show(TRUE
);
202 void MyFrame::About(wxCommandEvent
& WXUNUSED(event
) )
204 (void)wxMessageBox("wxWindows GUI library layout demo\n",
205 "About Layout Demo", wxOK
|wxCENTRE
);
208 // Size the subwindows when the frame is resized
209 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
) )
214 void MyFrame::Draw(wxDC
& dc
, bool WXUNUSED(draw_bitmaps
) )
216 dc
.SetPen(* wxGREEN_PEN
);
217 dc
.DrawLine(0, 0, 200, 200);
218 dc
.DrawLine(200, 0, 0, 200);
220 dc
.SetBrush(* wxCYAN_BRUSH
);
221 dc
.SetPen(* wxRED_PEN
);
223 dc
.DrawRectangle(100, 100, 100, 50);
224 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
226 dc
.DrawEllipse(250, 250, 100, 50);
227 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
228 dc
.DrawLine(50, 230, 200, 230);
230 dc
.SetPen(* wxBLACK_PEN
);
231 dc
.DrawArc(50, 300, 100, 250, 100, 300 );
234 //-----------------------------------------------------------------
236 //-----------------------------------------------------------------
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
& WXUNUSED(event
) )
256 frame
->Draw(dc
,TRUE
);
259 //-----------------------------------------------------------------
261 //-----------------------------------------------------------------
263 NewSizerFrame::NewSizerFrame(wxFrame
*frame
, char *title
, int x
, int y
):
264 wxFrame(frame
, -1, title
, wxPoint(x
, y
) )
266 // we want to get a dialog that is stretchable because it
267 // has a text ctrl in the middle. at the bottom, we have
268 // two buttons which.
270 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
272 // 1) top: create wxStaticText with minimum size equal to its default size
274 new wxStaticText( this, -1, "An explanation (wxALIGN_RIGHT)." ),
275 0, // make vertically unstretchable
276 wxALIGN_RIGHT
| // right align text
277 wxTOP
| wxLEFT
| wxRIGHT
, // make border all around except wxBOTTOM
278 5 ); // set border width to 5
280 // 2) top: create wxTextCtrl with minimum size (100x60)
282 new wxTextCtrl( this, -1, "My text (wxEXPAND).", wxDefaultPosition
, wxSize(100,60), wxTE_MULTILINE
),
283 1, // make vertically stretchable
284 wxEXPAND
| // make horizontally stretchable
285 wxALL
, // and make border all around
286 5 ); // set border width to 5
289 // 3) middle: create wxStaticLine with minimum size (3x3)
291 new wxStaticLine( this, -1, wxDefaultPosition
, wxSize(3,3), wxHORIZONTAL
),
292 0, // make vertically unstretchable
293 wxEXPAND
| // make horizontally stretchable
294 wxALL
, // and make border all around
295 5 ); // set border width to 5
298 // 4) bottom: create two centred wxButtons
299 wxBoxSizer
*button_box
= new wxBoxSizer( wxHORIZONTAL
);
301 new wxButton( this, -1, "Two buttons in a box" ),
302 0, // make horizontally unstretchable
303 wxALL
, // make border all around
304 7 ); // set border width to 7
306 new wxButton( this, -1, "(wxCENTER)" ),
307 0, // make horizontally unstretchable
308 wxALL
, // make border all around
309 7 ); // set border width to 7
313 0, // make vertically unstretchable
314 wxCENTER
); // no border and centre horizontally
317 // set frame to minimum size
318 topsizer
->Fit( this );
320 // don't allow frame to get smaller than what the sizers tell ye
321 topsizer
->SetSizeHints( this );
323 SetSizer( topsizer
);
325 SetAutoLayout( TRUE
);