]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/static.cpp
Fix gcc warnings about testing undefined preprocessor symbol.
[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 #include "wx/generic/stattextg.h"
44
45 #include "widgets.h"
46 #include "icons/statbox.xpm"
47
48 // ----------------------------------------------------------------------------
49 // constants
50 // ----------------------------------------------------------------------------
51
52 // control ids
53 enum
54 {
55 StaticPage_Reset = wxID_HIGHEST,
56 StaticPage_BoxText,
57 StaticPage_LabelText,
58 StaticPage_LabelTextWithMarkup
59 };
60
61 // alignment radiobox values
62 enum
63 {
64 StaticHAlign_Left,
65 StaticHAlign_Centre,
66 StaticHAlign_Right,
67 StaticHAlign_Max
68 };
69
70 enum
71 {
72 StaticVAlign_Top,
73 StaticVAlign_Centre,
74 StaticVAlign_Bottom,
75 StaticVAlign_Max
76 };
77
78 enum
79 {
80 StaticEllipsize_Start,
81 StaticEllipsize_Middle,
82 StaticEllipsize_End
83 };
84
85
86 // ----------------------------------------------------------------------------
87 // StaticWidgetsPage
88 // ----------------------------------------------------------------------------
89
90 class StaticWidgetsPage : public WidgetsPage
91 {
92 public:
93 StaticWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
94 virtual ~StaticWidgetsPage(){};
95
96 virtual wxControl *GetWidget() const { return m_statText; }
97 virtual void RecreateWidget() { CreateStatic(); }
98
99 // lazy creation of the content
100 virtual void CreateContent();
101
102 protected:
103 // event handlers
104 void OnCheckOrRadioBox(wxCommandEvent& event);
105
106 void OnButtonReset(wxCommandEvent& event);
107 void OnButtonBoxText(wxCommandEvent& event);
108 void OnButtonLabelText(wxCommandEvent& event);
109 void OnButtonLabelWithMarkupText(wxCommandEvent& event);
110 void OnMouseEvent(wxMouseEvent& event);
111
112 // reset all parameters
113 void Reset();
114
115 // (re)create all controls
116 void CreateStatic();
117
118 // the controls
119 // ------------
120
121 // the check/radio boxes for styles
122 wxCheckBox *m_chkVert,
123 *m_chkGeneric,
124 *m_chkAutoResize,
125 *m_chkEllipsize,
126 *m_chkMarkup,
127 *m_chkGreen;
128
129 wxRadioBox *m_radioHAlign,
130 *m_radioVAlign,
131 *m_radioEllipsize;
132
133 // the controls and the sizer containing them
134 wxStaticBoxSizer *m_sizerStatBox;
135 wxStaticTextBase *m_statText,
136 *m_statMarkup;
137 #if wxUSE_STATLINE
138 wxStaticLine *m_statLine;
139 #endif // wxUSE_STATLINE
140 wxSizer *m_sizerStatic;
141
142 // the text entries for command parameters
143 wxTextCtrl *m_textBox,
144 *m_textLabel,
145 *m_textLabelWithMarkup;
146
147 private:
148 DECLARE_EVENT_TABLE()
149 DECLARE_WIDGETS_PAGE(StaticWidgetsPage)
150 };
151
152 // ----------------------------------------------------------------------------
153 // event tables
154 // ----------------------------------------------------------------------------
155
156 BEGIN_EVENT_TABLE(StaticWidgetsPage, WidgetsPage)
157 EVT_BUTTON(StaticPage_Reset, StaticWidgetsPage::OnButtonReset)
158 EVT_BUTTON(StaticPage_LabelText, StaticWidgetsPage::OnButtonLabelText)
159 EVT_BUTTON(StaticPage_LabelTextWithMarkup, StaticWidgetsPage::OnButtonLabelWithMarkupText)
160 EVT_BUTTON(StaticPage_BoxText, StaticWidgetsPage::OnButtonBoxText)
161
162 EVT_CHECKBOX(wxID_ANY, StaticWidgetsPage::OnCheckOrRadioBox)
163 EVT_RADIOBOX(wxID_ANY, StaticWidgetsPage::OnCheckOrRadioBox)
164 END_EVENT_TABLE()
165
166 // ============================================================================
167 // implementation
168 // ============================================================================
169
170 IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage, wxT("Static"),
171 (int)wxPlatform(GENERIC_CTRLS).If(wxOS_WINDOWS,NATIVE_CTRLS)
172 );
173
174 StaticWidgetsPage::StaticWidgetsPage(WidgetsBookCtrl *book,
175 wxImageList *imaglist)
176 : WidgetsPage(book, imaglist, statbox_xpm)
177 {
178 // init everything
179 m_chkVert =
180 m_chkAutoResize = (wxCheckBox *)NULL;
181 m_chkGeneric = NULL;
182 m_chkGreen = NULL;
183
184 m_radioHAlign =
185 m_radioVAlign = (wxRadioBox *)NULL;
186
187 #if wxUSE_STATLINE
188 m_statLine = (wxStaticLine *)NULL;
189 #endif // wxUSE_STATLINE
190 m_statText = m_statMarkup = NULL;
191
192 m_sizerStatBox = (wxStaticBoxSizer *)NULL;
193 m_sizerStatic = (wxSizer *)NULL;
194
195 m_textBox = m_textLabel = m_textLabelWithMarkup = NULL;
196 }
197
198 void StaticWidgetsPage::CreateContent()
199 {
200 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
201
202 // left pane
203 wxSizer *sizerLeft = new wxStaticBoxSizer(wxVERTICAL, this, "&Set style");
204
205 m_chkGeneric = CreateCheckBoxAndAddToSizer(sizerLeft,
206 "&Generic wxStaticText");
207 m_chkMarkup = CreateCheckBoxAndAddToSizer(sizerLeft, "Support &markup");
208 m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, "&Vertical line");
209 m_chkAutoResize = CreateCheckBoxAndAddToSizer(sizerLeft, "&Fit to text");
210 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
211
212 static const wxString halign[] =
213 {
214 wxT("left"),
215 wxT("centre"),
216 wxT("right"),
217 };
218
219 static const wxString valign[] =
220 {
221 wxT("top"),
222 wxT("centre"),
223 wxT("bottom"),
224 };
225
226 m_radioHAlign = new wxRadioBox(this, wxID_ANY, wxT("&Horz alignment"),
227 wxDefaultPosition, wxDefaultSize,
228 WXSIZEOF(halign), halign, 3);
229 m_radioVAlign = new wxRadioBox(this, wxID_ANY, wxT("&Vert alignment"),
230 wxDefaultPosition, wxDefaultSize,
231 WXSIZEOF(valign), valign, 3);
232
233 sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
234 sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
235
236
237 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
238
239 m_chkEllipsize = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Ellipsize"));
240
241 static const wxString ellipsizeMode[] =
242 {
243 wxT("&start"),
244 wxT("&middle"),
245 wxT("&end"),
246 };
247
248 m_radioEllipsize = new wxRadioBox(this, wxID_ANY, wxT("&Ellipsize mode"),
249 wxDefaultPosition, wxDefaultSize,
250 WXSIZEOF(ellipsizeMode), ellipsizeMode,
251 3);
252
253 sizerLeft->Add(m_radioEllipsize, 0, wxGROW | wxALL, 5);
254
255 wxButton *btn = new wxButton(this, StaticPage_Reset, wxT("&Reset"));
256 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
257
258 // middle pane
259 wxSizer *sizerMiddle = new wxStaticBoxSizer(wxVERTICAL, this,
260 "&Change labels");
261
262 m_textBox = new wxTextCtrl(this, wxID_ANY, wxEmptyString);
263 wxButton *b1 = new wxButton(this, wxID_ANY, "Change &box label");
264 b1->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
265 wxCommandEventHandler(StaticWidgetsPage::OnButtonBoxText),
266 NULL, this);
267 sizerMiddle->Add(m_textBox, 0, wxEXPAND|wxALL, 5);
268 sizerMiddle->Add(b1, 0, wxLEFT|wxBOTTOM, 5);
269
270 m_textLabel = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
271 wxDefaultPosition, wxDefaultSize,
272 wxTE_MULTILINE|wxHSCROLL);
273 wxButton *b2 = new wxButton(this, wxID_ANY, "Change &text label");
274 b2->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
275 wxCommandEventHandler(StaticWidgetsPage::OnButtonLabelText),
276 NULL, this);
277 sizerMiddle->Add(m_textLabel, 0, wxEXPAND|wxALL, 5);
278 sizerMiddle->Add(b2, 0, wxLEFT|wxBOTTOM, 5);
279
280 m_textLabelWithMarkup = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
281 wxDefaultPosition, wxDefaultSize,
282 wxTE_MULTILINE|wxHSCROLL);
283
284 wxButton *b3 = new wxButton(this, wxID_ANY, "Change decorated text label");
285 b3->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
286 wxCommandEventHandler(StaticWidgetsPage::OnButtonLabelWithMarkupText),
287 NULL, this);
288 sizerMiddle->Add(m_textLabelWithMarkup, 0, wxEXPAND|wxALL, 5);
289 sizerMiddle->Add(b3, 0, wxLEFT|wxBOTTOM, 5);
290
291 m_chkGreen = CreateCheckBoxAndAddToSizer(sizerLeft,
292 "Decorated label on g&reen");
293 sizerMiddle->Add(m_chkGreen, 0, wxALL, 5);
294
295 // final initializations
296 // NB: must be done _before_ calling CreateStatic()
297 Reset();
298
299 m_textBox->SetValue(wxT("This is a box"));
300 m_textLabel->SetValue(wxT("And this is a\n\tlabel inside the box with a &mnemonic.\n")
301 wxT("Only this text is affected by the ellipsize settings."));
302 m_textLabelWithMarkup->SetValue(wxT("Another label, this time <b>decorated</b> ")
303 wxT("with <u>markup</u>; here you need entities ")
304 wxT("for the symbols: &lt; &gt; &amp; &apos; &quot; ")
305 wxT(" but you can still place &mnemonics..."));
306
307 // right pane
308 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
309 sizerRight->SetMinSize(150, 0);
310 m_sizerStatic = sizerRight;
311
312 CreateStatic();
313
314 // the 3 panes panes compose the window
315 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
316 sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10);
317 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
318
319 SetSizer(sizerTop);
320 }
321
322 // ----------------------------------------------------------------------------
323 // operations
324 // ----------------------------------------------------------------------------
325
326 void StaticWidgetsPage::Reset()
327 {
328 m_chkGeneric->SetValue(false);
329 m_chkVert->SetValue(false);
330 m_chkAutoResize->SetValue(true);
331 m_chkEllipsize->SetValue(true);
332 m_chkMarkup->SetValue(true);
333
334 m_radioHAlign->SetSelection(StaticHAlign_Left);
335 m_radioVAlign->SetSelection(StaticVAlign_Top);
336 }
337
338 void StaticWidgetsPage::CreateStatic()
339 {
340 bool isVert = m_chkVert->GetValue();
341
342 if ( m_sizerStatBox )
343 {
344 // delete m_sizerStatBox; -- deleted by Remove()
345 m_sizerStatic->Remove(m_sizerStatBox);
346 delete m_statText;
347 delete m_statMarkup;
348 #if wxUSE_STATLINE
349 delete m_statLine;
350 #endif // wxUSE_STATLINE
351 }
352
353 int flagsBox = 0,
354 flagsText = ms_defaultFlags,
355 flagsDummyText = ms_defaultFlags;
356
357 if ( !m_chkAutoResize->GetValue() )
358 {
359 flagsText |= wxST_NO_AUTORESIZE;
360 flagsDummyText |= wxST_NO_AUTORESIZE;
361 }
362
363 if ( m_chkMarkup->GetValue() )
364 {
365 flagsText |= wxST_MARKUP;
366 flagsDummyText |= wxST_MARKUP;
367 }
368
369 int align = 0;
370 switch ( m_radioHAlign->GetSelection() )
371 {
372 default:
373 wxFAIL_MSG(wxT("unexpected radiobox selection"));
374 // fall through
375
376 case StaticHAlign_Left:
377 align |= wxALIGN_LEFT;
378 break;
379
380 case StaticHAlign_Centre:
381 align |= wxALIGN_CENTRE_HORIZONTAL;
382 break;
383
384 case StaticHAlign_Right:
385 align |= wxALIGN_RIGHT;
386 break;
387 }
388
389 switch ( m_radioVAlign->GetSelection() )
390 {
391 default:
392 wxFAIL_MSG(wxT("unexpected radiobox selection"));
393 // fall through
394
395 case StaticVAlign_Top:
396 align |= wxALIGN_TOP;
397 break;
398
399 case StaticVAlign_Centre:
400 align |= wxALIGN_CENTRE_VERTICAL;
401 break;
402
403 case StaticVAlign_Bottom:
404 align |= wxALIGN_BOTTOM;
405 break;
406 }
407
408 if ( m_chkEllipsize->GetValue() )
409 {
410 switch ( m_radioEllipsize->GetSelection() )
411 {
412 default:
413 wxFAIL_MSG(wxT("unexpected radiobox selection"));
414 // fall through
415
416 case StaticEllipsize_Start:
417 flagsDummyText |= wxST_ELLIPSIZE_START;
418 break;
419
420 case StaticEllipsize_Middle:
421 flagsDummyText |= wxST_ELLIPSIZE_MIDDLE;
422 break;
423
424 case StaticEllipsize_End:
425 flagsDummyText |= wxST_ELLIPSIZE_END;
426 break;
427 }
428 }
429
430 flagsDummyText |= align;
431 flagsText |= align;
432 flagsBox |= align;
433
434 wxStaticBox *staticBox = new wxStaticBox(this, wxID_ANY,
435 m_textBox->GetValue(),
436 wxDefaultPosition, wxDefaultSize,
437 flagsBox);
438 m_sizerStatBox = new wxStaticBoxSizer(staticBox, isVert ? wxHORIZONTAL
439 : wxVERTICAL);
440
441 if ( m_chkGeneric->GetValue() )
442 {
443 m_statText = new wxGenericStaticText(this, wxID_ANY,
444 m_textLabel->GetValue(),
445 wxDefaultPosition, wxDefaultSize,
446 flagsDummyText);
447 m_statMarkup = new wxGenericStaticText(this, wxID_ANY,
448 m_textLabelWithMarkup->GetValue(),
449 wxDefaultPosition, wxDefaultSize,
450 flagsText);
451 }
452 else // use native versions
453 {
454 m_statText = new wxStaticText(this, wxID_ANY,
455 m_textLabel->GetValue(),
456 wxDefaultPosition, wxDefaultSize,
457 flagsDummyText);
458 m_statMarkup = new wxStaticText(this, wxID_ANY,
459 m_textLabelWithMarkup->GetValue(),
460 wxDefaultPosition, wxDefaultSize,
461 flagsText);
462 }
463 if ( m_chkGreen->GetValue() )
464 m_statMarkup->SetBackgroundColour(*wxGREEN);
465 #if wxUSE_STATLINE
466 m_statLine = new wxStaticLine(this, wxID_ANY,
467 wxDefaultPosition, wxDefaultSize,
468 isVert ? wxLI_VERTICAL : wxLI_HORIZONTAL);
469 #endif // wxUSE_STATLINE
470
471 m_sizerStatBox->Add(m_statText, 1, wxGROW | wxALL, 5);
472 #if wxUSE_STATLINE
473 m_sizerStatBox->Add(m_statLine, 0, wxGROW | wxALL, 5);
474 #endif // wxUSE_STATLINE
475 m_sizerStatBox->Add(m_statMarkup, 1, wxGROW | wxALL, 5);
476
477 m_sizerStatic->Add(m_sizerStatBox, 1, wxGROW);
478
479 m_sizerStatic->Layout();
480
481 m_statText->Connect(wxEVT_LEFT_UP,
482 wxMouseEventHandler(StaticWidgetsPage::OnMouseEvent),
483 NULL, this);
484 staticBox->Connect(wxEVT_LEFT_UP,
485 wxMouseEventHandler(StaticWidgetsPage::OnMouseEvent),
486 NULL, this);
487 }
488
489 // ----------------------------------------------------------------------------
490 // event handlers
491 // ----------------------------------------------------------------------------
492
493 void StaticWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
494 {
495 Reset();
496
497 CreateStatic();
498 }
499
500 void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
501 {
502 if (event.GetEventObject() == static_cast<wxObject*>(m_chkEllipsize))
503 {
504 m_radioEllipsize->Enable(event.IsChecked());
505 }
506
507 CreateStatic();
508 }
509
510 void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent& WXUNUSED(event))
511 {
512 m_sizerStatBox->GetStaticBox()->SetLabel(m_textBox->GetValue());
513 }
514
515 void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent& WXUNUSED(event))
516 {
517 m_statText->SetLabel(m_textLabel->GetValue());
518
519 // test GetLabel() and GetLabelText(); the first should return the
520 // label as it is written in the relative text control; the second should
521 // return the label as it's shown in the wxStaticText
522 wxLogMessage(wxT("The original label should be '%s'"),
523 m_statText->GetLabel());
524 wxLogMessage(wxT("The label text is '%s'"),
525 m_statText->GetLabelText());
526 }
527
528 void StaticWidgetsPage::OnButtonLabelWithMarkupText(wxCommandEvent& WXUNUSED(event))
529 {
530 m_statMarkup->SetLabel(m_textLabelWithMarkup->GetValue());
531
532 // test GetLabel() and GetLabelText(); the first should return the
533 // label as it is written in the relative text control; the second should
534 // return the label as it's shown in the wxStaticText
535 wxLogMessage(wxT("The original label should be '%s'"),
536 m_statMarkup->GetLabel());
537 wxLogMessage(wxT("The label text is '%s'"),
538 m_statMarkup->GetLabelText());
539 }
540
541 void StaticWidgetsPage::OnMouseEvent(wxMouseEvent& event)
542 {
543 if ( event.GetEventObject() == m_statText )
544 {
545 wxLogMessage("Clicked on static text");
546 }
547 else
548 {
549 wxLogMessage("Clicked on static box");
550 }
551 }
552