]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/static.cpp
trick the sizer code into thinking that the wizard pages are shown so that it takes...
[wxWidgets.git] / samples / widgets / static.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
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
31 #include "wx/bitmap.h"
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"
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
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
84 {
85 public:
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)
93 {
94 }
95
96 protected:
97 void OnMouseEvent(wxMouseEvent& WXUNUSED(event))
98 {
99 wxLogMessage(wxT("Clicked on static text"));
100 }
101
102 DECLARE_EVENT_TABLE()
103 };
104
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:
119 void OnMouseEvent(wxMouseEvent& WXUNUSED(event))
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)
133 END_EVENT_TABLE()
134
135 // ----------------------------------------------------------------------------
136 // StaticWidgetsPage
137 // ----------------------------------------------------------------------------
138
139 class StaticWidgetsPage : public WidgetsPage
140 {
141 public:
142 StaticWidgetsPage(wxBookCtrlBase *book, wxImageList *imaglist);
143 virtual ~StaticWidgetsPage(){};
144
145 virtual wxControl *GetWidget() const { return m_statText; }
146
147 protected:
148 // event handlers
149 void OnCheckOrRadioBox(wxCommandEvent& event);
150
151 void OnButtonReset(wxCommandEvent& event);
152 void OnButtonBoxText(wxCommandEvent& event);
153 void OnButtonLabelText(wxCommandEvent& event);
154
155 // reset all parameters
156 void Reset();
157
158 // (re)create all controls
159 void CreateStatic();
160
161 // the controls
162 // ------------
163
164 // the check/radio boxes for styles
165 wxCheckBox *m_chkVert,
166 *m_chkAutoResize;
167
168 wxRadioBox *m_radioHAlign,
169 *m_radioVAlign;
170
171 // the controls and the sizer containing them
172 wxStaticBox *m_staticBox;
173 wxStaticBoxSizer *m_sizerStatBox;
174 wxStaticText *m_statText;
175 #if wxUSE_STATLINE
176 wxStaticLine *m_statLine;
177 #endif // wxUSE_STATLINE
178 wxSizer *m_sizerStatic;
179
180 // the text entries for command parameters
181 wxTextCtrl *m_textBox,
182 *m_textLabel;
183
184 private:
185 DECLARE_EVENT_TABLE()
186 DECLARE_WIDGETS_PAGE(StaticWidgetsPage)
187 };
188
189 // ----------------------------------------------------------------------------
190 // event tables
191 // ----------------------------------------------------------------------------
192
193 BEGIN_EVENT_TABLE(StaticWidgetsPage, WidgetsPage)
194 EVT_BUTTON(StaticPage_Reset, StaticWidgetsPage::OnButtonReset)
195 EVT_BUTTON(StaticPage_LabelText, StaticWidgetsPage::OnButtonLabelText)
196 EVT_BUTTON(StaticPage_BoxText, StaticWidgetsPage::OnButtonBoxText)
197
198 EVT_CHECKBOX(wxID_ANY, StaticWidgetsPage::OnCheckOrRadioBox)
199 EVT_RADIOBOX(wxID_ANY, StaticWidgetsPage::OnCheckOrRadioBox)
200 END_EVENT_TABLE()
201
202 // ============================================================================
203 // implementation
204 // ============================================================================
205
206 IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage, _T("Static"));
207
208 StaticWidgetsPage::StaticWidgetsPage(wxBookCtrlBase *book,
209 wxImageList *imaglist)
210 : WidgetsPage(book)
211 {
212 imaglist->Add(wxBitmap(statbox_xpm));
213
214 // init everything
215 m_chkVert =
216 m_chkAutoResize = (wxCheckBox *)NULL;
217
218 m_radioHAlign =
219 m_radioVAlign = (wxRadioBox *)NULL;
220
221 #if wxUSE_STATLINE
222 m_statLine = (wxStaticLine *)NULL;
223 #endif // wxUSE_STATLINE
224 m_statText = (wxStaticText *)NULL;
225
226 m_staticBox = (wxStaticBox *)NULL;
227 m_sizerStatBox = (wxStaticBoxSizer *)NULL;
228 m_sizerStatic = (wxSizer *)NULL;
229
230 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
231
232 // left pane
233 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
234
235 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
236
237 m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical line"));
238 m_chkAutoResize = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Fit to text"));
239 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
240
241 static const wxString halign[] =
242 {
243 _T("left"),
244 _T("centre"),
245 _T("right"),
246 };
247
248 static const wxString valign[] =
249 {
250 _T("top"),
251 _T("centre"),
252 _T("bottom"),
253 };
254
255 m_radioHAlign = new wxRadioBox(this, wxID_ANY, _T("&Horz alignment"),
256 wxDefaultPosition, wxDefaultSize,
257 WXSIZEOF(halign), halign);
258 m_radioVAlign = new wxRadioBox(this, wxID_ANY, _T("&Vert alignment"),
259 wxDefaultPosition, wxDefaultSize,
260 WXSIZEOF(valign), valign);
261
262 sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
263 sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
264
265 wxButton *btn = new wxButton(this, StaticPage_Reset, _T("&Reset"));
266 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
267
268 // middle pane
269 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Change labels"));
270 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
271
272 wxSizer *sizerRow;
273
274 sizerRow = CreateSizerWithTextAndButton(StaticPage_BoxText,
275 _T("Change &box label"),
276 wxID_ANY, &m_textBox);
277 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
278
279 sizerRow = CreateSizerWithTextAndButton(StaticPage_LabelText,
280 _T("Change &text label"),
281 wxID_ANY, &m_textLabel);
282 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
283
284 m_textBox->SetValue(_T("This is a box"));
285 m_textLabel->SetValue(_T("And this is a label\ninside the box"));
286
287 // right pane
288 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
289 sizerRight->SetMinSize(150, 0);
290 m_sizerStatic = sizerRight;
291
292 CreateStatic();
293
294 // the 3 panes panes compose the window
295 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
296 sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10);
297 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
298
299 // final initializations
300 Reset();
301
302 SetSizer(sizerTop);
303
304 sizerTop->Fit(this);
305 }
306
307 // ----------------------------------------------------------------------------
308 // operations
309 // ----------------------------------------------------------------------------
310
311 void StaticWidgetsPage::Reset()
312 {
313 m_chkVert->SetValue(false);
314 m_chkAutoResize->SetValue(true);
315
316 m_radioHAlign->SetSelection(StaticHAlign_Left);
317 m_radioVAlign->SetSelection(StaticVAlign_Top);
318 }
319
320 void StaticWidgetsPage::CreateStatic()
321 {
322 bool isVert = m_chkVert->GetValue();
323
324 if ( m_sizerStatBox )
325 {
326 delete m_staticBox;
327 // delete m_sizerStatBox; -- deleted by Remove()
328 m_sizerStatic->Remove(m_sizerStatBox);
329 delete m_statText;
330 #if wxUSE_STATLINE
331 delete m_statLine;
332 #endif // wxUSE_STATLINE
333 }
334
335 int flagsBox = 0,
336 flagsText = 0;
337
338 if ( !m_chkAutoResize->GetValue() )
339 {
340 flagsText |= wxST_NO_AUTORESIZE;
341 }
342
343 int align = 0;
344 switch ( m_radioHAlign->GetSelection() )
345 {
346 default:
347 wxFAIL_MSG(_T("unexpected radiobox selection"));
348 // fall through
349
350 case StaticHAlign_Left:
351 align |= wxALIGN_LEFT;
352 break;
353
354 case StaticHAlign_Centre:
355 align |= wxALIGN_CENTRE_HORIZONTAL;
356 break;
357
358 case StaticHAlign_Right:
359 align |= wxALIGN_RIGHT;
360 break;
361 }
362
363 switch ( m_radioVAlign->GetSelection() )
364 {
365 default:
366 wxFAIL_MSG(_T("unexpected radiobox selection"));
367 // fall through
368
369 case StaticVAlign_Top:
370 align |= wxALIGN_TOP;
371 break;
372
373 case StaticVAlign_Centre:
374 align |= wxALIGN_CENTRE_VERTICAL;
375 break;
376
377 case StaticVAlign_Bottom:
378 align |= wxALIGN_BOTTOM;
379 break;
380 }
381
382 flagsText |= align;
383 flagsBox |= align;
384
385 m_staticBox = new MyStaticBox(this, wxID_ANY, m_textBox->GetValue(),
386 wxDefaultPosition, wxDefaultSize,
387 flagsBox);
388 m_sizerStatBox = new wxStaticBoxSizer(m_staticBox, isVert ? wxHORIZONTAL
389 : wxVERTICAL);
390
391 m_statText = new MyStaticText(this, wxID_ANY, m_textLabel->GetValue(),
392 wxDefaultPosition, wxDefaultSize,
393 flagsText);
394
395 #if wxUSE_STATLINE
396 m_statLine = new wxStaticLine(this, wxID_ANY,
397 wxDefaultPosition, wxDefaultSize,
398 isVert ? wxLI_VERTICAL : wxLI_HORIZONTAL);
399 #endif // wxUSE_STATLINE
400
401 m_sizerStatBox->Add(m_statText, 1, wxGROW | wxALL, 5);
402 #if wxUSE_STATLINE
403 m_sizerStatBox->Add(m_statLine, 0, wxGROW | wxALL, 5);
404 #endif // wxUSE_STATLINE
405 m_sizerStatBox->Add(0, 0, 1);
406
407 m_sizerStatic->Add(m_sizerStatBox, 1, wxGROW);
408
409 m_sizerStatic->Layout();
410 }
411
412 // ----------------------------------------------------------------------------
413 // event handlers
414 // ----------------------------------------------------------------------------
415
416 void StaticWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
417 {
418 Reset();
419
420 CreateStatic();
421 }
422
423 void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
424 {
425 CreateStatic();
426 }
427
428 void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent& WXUNUSED(event))
429 {
430 m_sizerStatBox->GetStaticBox()->SetLabel(m_textBox->GetValue());
431 }
432
433 void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent& WXUNUSED(event))
434 {
435 m_statText->SetLabel(m_textLabel->GetValue());
436 }
437