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