]>
Commit | Line | Data |
---|---|---|
32b8ec41 | 1 | ///////////////////////////////////////////////////////////////////////////// |
be5a51fb | 2 | // Program: wxWidgets Widgets Sample |
32b8ec41 VZ |
3 | // Name: static.cpp |
4 | // Purpose: Part of the widgets sample showing various static controls | |
5 | // Author: Vadim Zeitlin | |
6 | // Created: 11.04.01 | |
7 | // Id: $Id$ | |
8 | // Copyright: (c) 2001 Vadim Zeitlin | |
9 | // License: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // for compilers that support precompilation, includes "wx/wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | // for all others, include the necessary headers | |
28 | #ifndef WX_PRECOMP | |
29 | #include "wx/log.h" | |
30 | ||
3bb70c40 | 31 | #include "wx/bitmap.h" |
32b8ec41 VZ |
32 | #include "wx/button.h" |
33 | #include "wx/checkbox.h" | |
34 | #include "wx/radiobox.h" | |
35 | #include "wx/statbox.h" | |
36 | #include "wx/stattext.h" | |
37 | #include "wx/textctrl.h" | |
38 | #endif | |
39 | ||
40 | #include "wx/sizer.h" | |
41 | ||
42 | #include "wx/statline.h" | |
43 | ||
44 | #include "widgets.h" | |
32b8ec41 VZ |
45 | #include "icons/statbox.xpm" |
46 | ||
47 | // ---------------------------------------------------------------------------- | |
48 | // constants | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
51 | // control ids | |
52 | enum | |
53 | { | |
54 | StaticPage_Reset = 100, | |
55 | StaticPage_BoxText, | |
56 | StaticPage_LabelText | |
57 | }; | |
58 | ||
59 | // alignment radiobox values | |
60 | enum | |
61 | { | |
62 | StaticHAlign_Left, | |
63 | StaticHAlign_Centre, | |
64 | StaticHAlign_Right, | |
65 | StaticHAlign_Max | |
66 | }; | |
67 | ||
68 | enum | |
69 | { | |
70 | StaticVAlign_Top, | |
71 | StaticVAlign_Centre, | |
72 | StaticVAlign_Bottom, | |
73 | StaticVAlign_Max | |
74 | }; | |
75 | ||
13cd1745 VZ |
76 | // ---------------------------------------------------------------------------- |
77 | // MyStaticText and MyStaticBox | |
78 | // ---------------------------------------------------------------------------- | |
79 | ||
80 | // these 2 classes simply show that the static controls can get the mouse | |
81 | // clicks too -- this used to be broken under MSW but works now | |
82 | ||
83 | class MyStaticText : public wxStaticText | |
a9d171bd JS |
84 | { |
85 | public: | |
13cd1745 VZ |
86 | MyStaticText(wxWindow* parent, |
87 | wxWindowID id, | |
88 | const wxString& label, | |
89 | const wxPoint& pos = wxDefaultPosition, | |
90 | const wxSize& size = wxDefaultSize, | |
91 | long style = 0) | |
92 | : wxStaticText(parent, id, label, pos, size, style) | |
a9d171bd JS |
93 | { |
94 | } | |
13cd1745 VZ |
95 | |
96 | protected: | |
c02e5a31 | 97 | void OnMouseEvent(wxMouseEvent& WXUNUSED(event)) |
a9d171bd | 98 | { |
13cd1745 | 99 | wxLogMessage(wxT("Clicked on static text")); |
a9d171bd | 100 | } |
13cd1745 VZ |
101 | |
102 | DECLARE_EVENT_TABLE() | |
a9d171bd JS |
103 | }; |
104 | ||
13cd1745 VZ |
105 | class MyStaticBox : public wxStaticBox |
106 | { | |
107 | public: | |
108 | MyStaticBox(wxWindow* parent, | |
109 | wxWindowID id, | |
110 | const wxString& label, | |
111 | const wxPoint& pos = wxDefaultPosition, | |
112 | const wxSize& size = wxDefaultSize, | |
113 | long style = 0) | |
114 | : wxStaticBox(parent, id, label, pos, size, style) | |
115 | { | |
116 | } | |
117 | ||
118 | protected: | |
c02e5a31 | 119 | void OnMouseEvent(wxMouseEvent& WXUNUSED(event)) |
13cd1745 VZ |
120 | { |
121 | wxLogMessage(wxT("Clicked on static box")); | |
122 | } | |
123 | ||
124 | DECLARE_EVENT_TABLE() | |
125 | }; | |
126 | ||
127 | BEGIN_EVENT_TABLE(MyStaticText, wxStaticText) | |
128 | EVT_LEFT_UP(MyStaticText::OnMouseEvent) | |
129 | END_EVENT_TABLE() | |
130 | ||
131 | BEGIN_EVENT_TABLE(MyStaticBox, wxStaticBox) | |
132 | EVT_LEFT_UP(MyStaticBox::OnMouseEvent) | |
a9d171bd JS |
133 | END_EVENT_TABLE() |
134 | ||
32b8ec41 VZ |
135 | // ---------------------------------------------------------------------------- |
136 | // StaticWidgetsPage | |
137 | // ---------------------------------------------------------------------------- | |
138 | ||
139 | class StaticWidgetsPage : public WidgetsPage | |
140 | { | |
141 | public: | |
142 | StaticWidgetsPage(wxNotebook *notebook, wxImageList *imaglist); | |
8f6eaec9 | 143 | virtual ~StaticWidgetsPage(){}; |
32b8ec41 VZ |
144 | |
145 | protected: | |
146 | // event handlers | |
147 | void OnCheckOrRadioBox(wxCommandEvent& event); | |
148 | ||
149 | void OnButtonReset(wxCommandEvent& event); | |
150 | void OnButtonBoxText(wxCommandEvent& event); | |
151 | void OnButtonLabelText(wxCommandEvent& event); | |
152 | ||
153 | // reset all parameters | |
154 | void Reset(); | |
155 | ||
156 | // (re)create all controls | |
157 | void CreateStatic(); | |
158 | ||
159 | // the controls | |
160 | // ------------ | |
161 | ||
162 | // the check/radio boxes for styles | |
163 | wxCheckBox *m_chkVert, | |
164 | *m_chkAutoResize; | |
165 | ||
166 | wxRadioBox *m_radioHAlign, | |
167 | *m_radioVAlign; | |
168 | ||
169 | // the controls and the sizer containing them | |
168954a1 | 170 | wxStaticBox *m_staticBox; |
32b8ec41 VZ |
171 | wxStaticBoxSizer *m_sizerStatBox; |
172 | wxStaticText *m_statText; | |
a56938e4 | 173 | #if wxUSE_STATLINE |
32b8ec41 | 174 | wxStaticLine *m_statLine; |
a56938e4 | 175 | #endif // wxUSE_STATLINE |
32b8ec41 VZ |
176 | wxSizer *m_sizerStatic; |
177 | ||
178 | // the text entries for command parameters | |
179 | wxTextCtrl *m_textBox, | |
180 | *m_textLabel; | |
181 | ||
182 | private: | |
5e173f35 GD |
183 | DECLARE_EVENT_TABLE() |
184 | DECLARE_WIDGETS_PAGE(StaticWidgetsPage) | |
32b8ec41 VZ |
185 | }; |
186 | ||
187 | // ---------------------------------------------------------------------------- | |
188 | // event tables | |
189 | // ---------------------------------------------------------------------------- | |
190 | ||
191 | BEGIN_EVENT_TABLE(StaticWidgetsPage, WidgetsPage) | |
192 | EVT_BUTTON(StaticPage_Reset, StaticWidgetsPage::OnButtonReset) | |
193 | EVT_BUTTON(StaticPage_LabelText, StaticWidgetsPage::OnButtonLabelText) | |
194 | EVT_BUTTON(StaticPage_BoxText, StaticWidgetsPage::OnButtonBoxText) | |
195 | ||
206d3a16 JS |
196 | EVT_CHECKBOX(wxID_ANY, StaticWidgetsPage::OnCheckOrRadioBox) |
197 | EVT_RADIOBOX(wxID_ANY, StaticWidgetsPage::OnCheckOrRadioBox) | |
32b8ec41 VZ |
198 | END_EVENT_TABLE() |
199 | ||
200 | // ============================================================================ | |
201 | // implementation | |
202 | // ============================================================================ | |
203 | ||
204 | IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage, _T("Static")); | |
205 | ||
206 | StaticWidgetsPage::StaticWidgetsPage(wxNotebook *notebook, | |
207 | wxImageList *imaglist) | |
208 | : WidgetsPage(notebook) | |
209 | { | |
210 | imaglist->Add(wxBitmap(statbox_xpm)); | |
211 | ||
212 | // init everything | |
213 | m_chkVert = | |
214 | m_chkAutoResize = (wxCheckBox *)NULL; | |
215 | ||
216 | m_radioHAlign = | |
217 | m_radioVAlign = (wxRadioBox *)NULL; | |
218 | ||
a56938e4 | 219 | #if wxUSE_STATLINE |
32b8ec41 | 220 | m_statLine = (wxStaticLine *)NULL; |
a56938e4 | 221 | #endif // wxUSE_STATLINE |
32b8ec41 VZ |
222 | m_statText = (wxStaticText *)NULL; |
223 | ||
168954a1 | 224 | m_staticBox = (wxStaticBox *)NULL; |
32b8ec41 VZ |
225 | m_sizerStatBox = (wxStaticBoxSizer *)NULL; |
226 | m_sizerStatic = (wxSizer *)NULL; | |
227 | ||
228 | wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL); | |
229 | ||
230 | // left pane | |
206d3a16 | 231 | wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style")); |
32b8ec41 VZ |
232 | |
233 | wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL); | |
234 | ||
235 | m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical line")); | |
236 | m_chkAutoResize = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Fit to text")); | |
237 | sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer | |
238 | ||
239 | static const wxString halign[] = | |
240 | { | |
241 | _T("left"), | |
242 | _T("centre"), | |
243 | _T("right"), | |
244 | }; | |
245 | ||
246 | static const wxString valign[] = | |
247 | { | |
248 | _T("top"), | |
249 | _T("centre"), | |
250 | _T("bottom"), | |
251 | }; | |
252 | ||
206d3a16 | 253 | m_radioHAlign = new wxRadioBox(this, wxID_ANY, _T("&Horz alignment"), |
32b8ec41 VZ |
254 | wxDefaultPosition, wxDefaultSize, |
255 | WXSIZEOF(halign), halign); | |
206d3a16 | 256 | m_radioVAlign = new wxRadioBox(this, wxID_ANY, _T("&Vert alignment"), |
32b8ec41 VZ |
257 | wxDefaultPosition, wxDefaultSize, |
258 | WXSIZEOF(valign), valign); | |
259 | ||
260 | sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5); | |
261 | sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5); | |
262 | ||
263 | wxButton *btn = new wxButton(this, StaticPage_Reset, _T("&Reset")); | |
264 | sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15); | |
265 | ||
266 | // middle pane | |
206d3a16 | 267 | wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Change labels")); |
32b8ec41 VZ |
268 | wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL); |
269 | ||
270 | wxSizer *sizerRow; | |
271 | ||
272 | sizerRow = CreateSizerWithTextAndButton(StaticPage_BoxText, | |
273 | _T("Change &box label"), | |
206d3a16 | 274 | wxID_ANY, &m_textBox); |
32b8ec41 VZ |
275 | sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5); |
276 | ||
277 | sizerRow = CreateSizerWithTextAndButton(StaticPage_LabelText, | |
278 | _T("Change &text label"), | |
206d3a16 | 279 | wxID_ANY, &m_textLabel); |
32b8ec41 VZ |
280 | sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5); |
281 | ||
282 | m_textBox->SetValue(_T("This is a box")); | |
283 | m_textLabel->SetValue(_T("And this is a label\ninside the box")); | |
284 | ||
285 | // right pane | |
286 | wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL); | |
7b127900 | 287 | sizerRight->SetMinSize(150, 0); |
32b8ec41 VZ |
288 | m_sizerStatic = sizerRight; |
289 | ||
290 | CreateStatic(); | |
291 | ||
292 | // the 3 panes panes compose the window | |
293 | sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10); | |
7b127900 | 294 | sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10); |
32b8ec41 VZ |
295 | sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10); |
296 | ||
297 | // final initializations | |
298 | Reset(); | |
299 | ||
32b8ec41 VZ |
300 | SetSizer(sizerTop); |
301 | ||
302 | sizerTop->Fit(this); | |
303 | } | |
304 | ||
32b8ec41 VZ |
305 | // ---------------------------------------------------------------------------- |
306 | // operations | |
307 | // ---------------------------------------------------------------------------- | |
308 | ||
309 | void StaticWidgetsPage::Reset() | |
310 | { | |
206d3a16 JS |
311 | m_chkVert->SetValue(false); |
312 | m_chkAutoResize->SetValue(true); | |
32b8ec41 VZ |
313 | |
314 | m_radioHAlign->SetSelection(StaticHAlign_Left); | |
315 | m_radioVAlign->SetSelection(StaticVAlign_Top); | |
316 | } | |
317 | ||
318 | void StaticWidgetsPage::CreateStatic() | |
319 | { | |
320 | bool isVert = m_chkVert->GetValue(); | |
321 | ||
322 | if ( m_sizerStatBox ) | |
323 | { | |
168954a1 | 324 | delete m_staticBox; |
32b8ec41 | 325 | // delete m_sizerStatBox; -- deleted by Remove() |
168954a1 | 326 | m_sizerStatic->Remove(m_sizerStatBox); |
32b8ec41 | 327 | delete m_statText; |
a56938e4 | 328 | #if wxUSE_STATLINE |
32b8ec41 | 329 | delete m_statLine; |
a56938e4 | 330 | #endif // wxUSE_STATLINE |
32b8ec41 VZ |
331 | } |
332 | ||
333 | int flagsBox = 0, | |
334 | flagsText = 0; | |
335 | ||
336 | if ( !m_chkAutoResize->GetValue() ) | |
337 | { | |
338 | flagsText |= wxST_NO_AUTORESIZE; | |
339 | } | |
340 | ||
341 | int align = 0; | |
342 | switch ( m_radioHAlign->GetSelection() ) | |
343 | { | |
344 | default: | |
345 | wxFAIL_MSG(_T("unexpected radiobox selection")); | |
346 | // fall through | |
347 | ||
348 | case StaticHAlign_Left: | |
349 | align |= wxALIGN_LEFT; | |
350 | break; | |
351 | ||
352 | case StaticHAlign_Centre: | |
353 | align |= wxALIGN_CENTRE_HORIZONTAL; | |
354 | break; | |
355 | ||
356 | case StaticHAlign_Right: | |
357 | align |= wxALIGN_RIGHT; | |
358 | break; | |
359 | } | |
360 | ||
361 | switch ( m_radioVAlign->GetSelection() ) | |
362 | { | |
363 | default: | |
364 | wxFAIL_MSG(_T("unexpected radiobox selection")); | |
365 | // fall through | |
366 | ||
367 | case StaticVAlign_Top: | |
368 | align |= wxALIGN_TOP; | |
369 | break; | |
370 | ||
371 | case StaticVAlign_Centre: | |
372 | align |= wxALIGN_CENTRE_VERTICAL; | |
373 | break; | |
374 | ||
375 | case StaticVAlign_Bottom: | |
376 | align |= wxALIGN_BOTTOM; | |
377 | break; | |
378 | } | |
379 | ||
380 | flagsText |= align; | |
381 | flagsBox |= align; | |
382 | ||
206d3a16 | 383 | m_staticBox = new MyStaticBox(this, wxID_ANY, m_textBox->GetValue(), |
168954a1 VZ |
384 | wxDefaultPosition, wxDefaultSize, |
385 | flagsBox); | |
386 | m_sizerStatBox = new wxStaticBoxSizer(m_staticBox, isVert ? wxHORIZONTAL | |
387 | : wxVERTICAL); | |
32b8ec41 | 388 | |
206d3a16 | 389 | m_statText = new MyStaticText(this, wxID_ANY, m_textLabel->GetValue(), |
32b8ec41 VZ |
390 | wxDefaultPosition, wxDefaultSize, |
391 | flagsText); | |
392 | ||
a56938e4 | 393 | #if wxUSE_STATLINE |
206d3a16 | 394 | m_statLine = new wxStaticLine(this, wxID_ANY, |
32b8ec41 VZ |
395 | wxDefaultPosition, wxDefaultSize, |
396 | isVert ? wxLI_VERTICAL : wxLI_HORIZONTAL); | |
a56938e4 | 397 | #endif // wxUSE_STATLINE |
32b8ec41 VZ |
398 | |
399 | m_sizerStatBox->Add(m_statText, 1, wxGROW | wxALL, 5); | |
a56938e4 | 400 | #if wxUSE_STATLINE |
32b8ec41 | 401 | m_sizerStatBox->Add(m_statLine, 0, wxGROW | wxALL, 5); |
a56938e4 | 402 | #endif // wxUSE_STATLINE |
32b8ec41 VZ |
403 | m_sizerStatBox->Add(0, 0, 1); |
404 | ||
405 | m_sizerStatic->Add(m_sizerStatBox, 1, wxGROW); | |
406 | ||
407 | m_sizerStatic->Layout(); | |
408 | } | |
409 | ||
410 | // ---------------------------------------------------------------------------- | |
411 | // event handlers | |
412 | // ---------------------------------------------------------------------------- | |
413 | ||
414 | void StaticWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event)) | |
415 | { | |
416 | Reset(); | |
417 | ||
418 | CreateStatic(); | |
419 | } | |
420 | ||
c02e5a31 | 421 | void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event)) |
32b8ec41 VZ |
422 | { |
423 | CreateStatic(); | |
424 | } | |
425 | ||
c02e5a31 | 426 | void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent& WXUNUSED(event)) |
32b8ec41 VZ |
427 | { |
428 | m_sizerStatBox->GetStaticBox()->SetLabel(m_textBox->GetValue()); | |
429 | } | |
430 | ||
c02e5a31 | 431 | void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent& WXUNUSED(event)) |
32b8ec41 VZ |
432 | { |
433 | m_statText->SetLabel(m_textLabel->GetValue()); | |
434 | } | |
435 |