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