]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/static.cpp
fix the MSVC check in the last comment and accompanying comment
[wxWidgets.git] / samples / widgets / static.cpp
CommitLineData
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"
916eabe6 43#include "wx/generic/stattextg.h"
32b8ec41
VZ
44
45#include "widgets.h"
32b8ec41
VZ
46#include "icons/statbox.xpm"
47
48// ----------------------------------------------------------------------------
49// constants
50// ----------------------------------------------------------------------------
51
52// control ids
53enum
54{
f0fa4312 55 StaticPage_Reset = wxID_HIGHEST,
32b8ec41 56 StaticPage_BoxText,
39bc0347
VZ
57 StaticPage_LabelText,
58 StaticPage_LabelTextWithMarkup
32b8ec41
VZ
59};
60
61// alignment radiobox values
62enum
63{
64 StaticHAlign_Left,
65 StaticHAlign_Centre,
66 StaticHAlign_Right,
67 StaticHAlign_Max
68};
69
70enum
71{
72 StaticVAlign_Top,
73 StaticVAlign_Centre,
74 StaticVAlign_Bottom,
75 StaticVAlign_Max
76};
77
39bc0347
VZ
78enum
79{
80 StaticEllipsize_Start,
81 StaticEllipsize_Middle,
82 StaticEllipsize_End
83};
84
a9d171bd 85
32b8ec41
VZ
86// ----------------------------------------------------------------------------
87// StaticWidgetsPage
88// ----------------------------------------------------------------------------
89
90class StaticWidgetsPage : public WidgetsPage
91{
92public:
f2fdc4d5 93 StaticWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
8f6eaec9 94 virtual ~StaticWidgetsPage(){};
32b8ec41 95
195df7a7 96 virtual wxControl *GetWidget() const { return m_statText; }
1301e228 97 virtual void RecreateWidget() { CreateStatic(); }
195df7a7 98
453535a7
WS
99 // lazy creation of the content
100 virtual void CreateContent();
101
32b8ec41
VZ
102protected:
103 // event handlers
104 void OnCheckOrRadioBox(wxCommandEvent& event);
105
106 void OnButtonReset(wxCommandEvent& event);
107 void OnButtonBoxText(wxCommandEvent& event);
108 void OnButtonLabelText(wxCommandEvent& event);
39bc0347 109 void OnButtonLabelWithMarkupText(wxCommandEvent& event);
916eabe6 110 void OnMouseEvent(wxMouseEvent& event);
32b8ec41
VZ
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,
916eabe6 123 *m_chkGeneric,
39bc0347
VZ
124 *m_chkAutoResize,
125 *m_chkEllipsize,
916eabe6
VZ
126 *m_chkMarkup,
127 *m_chkGreen;
32b8ec41
VZ
128
129 wxRadioBox *m_radioHAlign,
39bc0347
VZ
130 *m_radioVAlign,
131 *m_radioEllipsize;
32b8ec41
VZ
132
133 // the controls and the sizer containing them
134 wxStaticBoxSizer *m_sizerStatBox;
916eabe6
VZ
135 wxStaticTextBase *m_statText,
136 *m_statMarkup;
a56938e4 137#if wxUSE_STATLINE
32b8ec41 138 wxStaticLine *m_statLine;
a56938e4 139#endif // wxUSE_STATLINE
32b8ec41
VZ
140 wxSizer *m_sizerStatic;
141
142 // the text entries for command parameters
143 wxTextCtrl *m_textBox,
39bc0347
VZ
144 *m_textLabel,
145 *m_textLabelWithMarkup;
32b8ec41
VZ
146
147private:
5e173f35
GD
148 DECLARE_EVENT_TABLE()
149 DECLARE_WIDGETS_PAGE(StaticWidgetsPage)
32b8ec41
VZ
150};
151
152// ----------------------------------------------------------------------------
153// event tables
154// ----------------------------------------------------------------------------
155
156BEGIN_EVENT_TABLE(StaticWidgetsPage, WidgetsPage)
157 EVT_BUTTON(StaticPage_Reset, StaticWidgetsPage::OnButtonReset)
158 EVT_BUTTON(StaticPage_LabelText, StaticWidgetsPage::OnButtonLabelText)
39bc0347 159 EVT_BUTTON(StaticPage_LabelTextWithMarkup, StaticWidgetsPage::OnButtonLabelWithMarkupText)
32b8ec41
VZ
160 EVT_BUTTON(StaticPage_BoxText, StaticWidgetsPage::OnButtonBoxText)
161
206d3a16
JS
162 EVT_CHECKBOX(wxID_ANY, StaticWidgetsPage::OnCheckOrRadioBox)
163 EVT_RADIOBOX(wxID_ANY, StaticWidgetsPage::OnCheckOrRadioBox)
32b8ec41
VZ
164END_EVENT_TABLE()
165
166// ============================================================================
167// implementation
168// ============================================================================
169
f2fdc4d5 170IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage, _T("Static"),
cc65c5f9 171 (int)wxPlatform(GENERIC_CTRLS).If(wxOS_WINDOWS,NATIVE_CTRLS)
f2fdc4d5 172 );
32b8ec41 173
f2fdc4d5 174StaticWidgetsPage::StaticWidgetsPage(WidgetsBookCtrl *book,
61c083e7 175 wxImageList *imaglist)
261357eb 176 : WidgetsPage(book, imaglist, statbox_xpm)
32b8ec41 177{
32b8ec41
VZ
178 // init everything
179 m_chkVert =
180 m_chkAutoResize = (wxCheckBox *)NULL;
916eabe6
VZ
181 m_chkGeneric = NULL;
182 m_chkGreen = NULL;
32b8ec41
VZ
183
184 m_radioHAlign =
185 m_radioVAlign = (wxRadioBox *)NULL;
186
a56938e4 187#if wxUSE_STATLINE
32b8ec41 188 m_statLine = (wxStaticLine *)NULL;
a56938e4 189#endif // wxUSE_STATLINE
916eabe6 190 m_statText = m_statMarkup = NULL;
32b8ec41
VZ
191
192 m_sizerStatBox = (wxStaticBoxSizer *)NULL;
193 m_sizerStatic = (wxSizer *)NULL;
39bc0347
VZ
194
195 m_textBox = m_textLabel = m_textLabelWithMarkup = NULL;
453535a7 196}
32b8ec41 197
453535a7
WS
198void StaticWidgetsPage::CreateContent()
199{
32b8ec41
VZ
200 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
201
202 // left pane
916eabe6 203 wxSizer *sizerLeft = new wxStaticBoxSizer(wxVERTICAL, this, "&Set style");
32b8ec41 204
916eabe6
VZ
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");
32b8ec41
VZ
210 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
211
212 static const wxString halign[] =
213 {
214 _T("left"),
215 _T("centre"),
216 _T("right"),
217 };
218
219 static const wxString valign[] =
220 {
221 _T("top"),
222 _T("centre"),
223 _T("bottom"),
224 };
225
206d3a16 226 m_radioHAlign = new wxRadioBox(this, wxID_ANY, _T("&Horz alignment"),
32b8ec41 227 wxDefaultPosition, wxDefaultSize,
916eabe6 228 WXSIZEOF(halign), halign, 3);
206d3a16 229 m_radioVAlign = new wxRadioBox(this, wxID_ANY, _T("&Vert alignment"),
32b8ec41 230 wxDefaultPosition, wxDefaultSize,
916eabe6 231 WXSIZEOF(valign), valign, 3);
32b8ec41
VZ
232
233 sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
234 sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
235
39bc0347
VZ
236
237 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
238
239 m_chkEllipsize = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Ellipsize"));
240
241 static const wxString ellipsizeMode[] =
242 {
243 _T("&start"),
244 _T("&middle"),
245 _T("&end"),
246 };
247
248 m_radioEllipsize = new wxRadioBox(this, wxID_ANY, _T("&Ellipsize mode"),
249 wxDefaultPosition, wxDefaultSize,
916eabe6
VZ
250 WXSIZEOF(ellipsizeMode), ellipsizeMode,
251 3);
39bc0347
VZ
252
253 sizerLeft->Add(m_radioEllipsize, 0, wxGROW | wxALL, 5);
254
32b8ec41
VZ
255 wxButton *btn = new wxButton(this, StaticPage_Reset, _T("&Reset"));
256 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
257
258 // middle pane
916eabe6
VZ
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);
39bc0347
VZ
294
295 // final initializations
296 // NB: must be done _before_ calling CreateStatic()
297 Reset();
298
32b8ec41 299 m_textBox->SetValue(_T("This is a box"));
39bc0347
VZ
300 m_textLabel->SetValue(_T("And this is a\n\tlabel inside the box with a &mnemonic.\n")
301 _T("Only this text is affected by the ellipsize settings."));
302 m_textLabelWithMarkup->SetValue(_T("Another label, this time <b>decorated</b> ")
303 _T("with <u>markup</u>; here you need entities ")
304 _T("for the symbols: &lt; &gt; &amp; &apos; &quot; ")
305 _T(" but you can still place &mnemonics..."));
32b8ec41
VZ
306
307 // right pane
308 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
7b127900 309 sizerRight->SetMinSize(150, 0);
32b8ec41
VZ
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);
7b127900 316 sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10);
32b8ec41
VZ
317 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
318
32b8ec41 319 SetSizer(sizerTop);
32b8ec41
VZ
320}
321
32b8ec41
VZ
322// ----------------------------------------------------------------------------
323// operations
324// ----------------------------------------------------------------------------
325
326void StaticWidgetsPage::Reset()
327{
916eabe6 328 m_chkGeneric->SetValue(false);
206d3a16
JS
329 m_chkVert->SetValue(false);
330 m_chkAutoResize->SetValue(true);
39bc0347
VZ
331 m_chkEllipsize->SetValue(true);
332 m_chkMarkup->SetValue(true);
32b8ec41
VZ
333
334 m_radioHAlign->SetSelection(StaticHAlign_Left);
335 m_radioVAlign->SetSelection(StaticVAlign_Top);
336}
337
338void StaticWidgetsPage::CreateStatic()
339{
340 bool isVert = m_chkVert->GetValue();
341
342 if ( m_sizerStatBox )
343 {
32b8ec41 344 // delete m_sizerStatBox; -- deleted by Remove()
168954a1 345 m_sizerStatic->Remove(m_sizerStatBox);
32b8ec41 346 delete m_statText;
916eabe6 347 delete m_statMarkup;
a56938e4 348#if wxUSE_STATLINE
32b8ec41 349 delete m_statLine;
a56938e4 350#endif // wxUSE_STATLINE
32b8ec41
VZ
351 }
352
353 int flagsBox = 0,
39bc0347
VZ
354 flagsText = ms_defaultFlags,
355 flagsDummyText = ms_defaultFlags;
32b8ec41
VZ
356
357 if ( !m_chkAutoResize->GetValue() )
358 {
359 flagsText |= wxST_NO_AUTORESIZE;
39bc0347
VZ
360 flagsDummyText |= wxST_NO_AUTORESIZE;
361 }
362
363 if ( m_chkMarkup->GetValue() )
364 {
365 flagsText |= wxST_MARKUP;
366 flagsDummyText |= wxST_MARKUP;
32b8ec41
VZ
367 }
368
369 int align = 0;
370 switch ( m_radioHAlign->GetSelection() )
371 {
372 default:
373 wxFAIL_MSG(_T("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(_T("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
39bc0347
VZ
408 if ( m_chkEllipsize->GetValue() )
409 {
410 switch ( m_radioEllipsize->GetSelection() )
411 {
412 default:
413 wxFAIL_MSG(_T("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;
32b8ec41
VZ
431 flagsText |= align;
432 flagsBox |= align;
433
916eabe6
VZ
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);
32b8ec41 440
916eabe6
VZ
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);
a56938e4 465#if wxUSE_STATLINE
206d3a16 466 m_statLine = new wxStaticLine(this, wxID_ANY,
32b8ec41
VZ
467 wxDefaultPosition, wxDefaultSize,
468 isVert ? wxLI_VERTICAL : wxLI_HORIZONTAL);
a56938e4 469#endif // wxUSE_STATLINE
32b8ec41
VZ
470
471 m_sizerStatBox->Add(m_statText, 1, wxGROW | wxALL, 5);
a56938e4 472#if wxUSE_STATLINE
32b8ec41 473 m_sizerStatBox->Add(m_statLine, 0, wxGROW | wxALL, 5);
a56938e4 474#endif // wxUSE_STATLINE
916eabe6 475 m_sizerStatBox->Add(m_statMarkup, 1, wxGROW | wxALL, 5);
32b8ec41
VZ
476
477 m_sizerStatic->Add(m_sizerStatBox, 1, wxGROW);
478
479 m_sizerStatic->Layout();
916eabe6
VZ
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);
32b8ec41
VZ
487}
488
489// ----------------------------------------------------------------------------
490// event handlers
491// ----------------------------------------------------------------------------
492
493void StaticWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
494{
495 Reset();
496
497 CreateStatic();
498}
499
39bc0347 500void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
32b8ec41 501{
81d3348a 502 if (event.GetEventObject() == static_cast<wxObject*>(m_chkEllipsize))
39bc0347
VZ
503 {
504 m_radioEllipsize->Enable(event.IsChecked());
505 }
506
32b8ec41
VZ
507 CreateStatic();
508}
509
c02e5a31 510void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
511{
512 m_sizerStatBox->GetStaticBox()->SetLabel(m_textBox->GetValue());
513}
514
916eabe6 515void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent& WXUNUSED(event))
39bc0347 516{
916eabe6 517 m_statText->SetLabel(m_textLabel->GetValue());
39bc0347
VZ
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'"),
916eabe6 523 m_statText->GetLabel());
39bc0347 524 wxLogMessage(wxT("The label text is '%s'"),
916eabe6 525 m_statText->GetLabelText());
39bc0347
VZ
526}
527
916eabe6 528void StaticWidgetsPage::OnButtonLabelWithMarkupText(wxCommandEvent& WXUNUSED(event))
32b8ec41 529{
916eabe6 530 m_statMarkup->SetLabel(m_textLabelWithMarkup->GetValue());
39bc0347
VZ
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'"),
916eabe6 536 m_statMarkup->GetLabel());
39bc0347 537 wxLogMessage(wxT("The label text is '%s'"),
916eabe6
VZ
538 m_statMarkup->GetLabelText());
539}
540
541void StaticWidgetsPage::OnMouseEvent(wxMouseEvent& event)
542{
543 if ( event.GetEventObject() == m_statText )
544 wxLogMessage("Clicked on static text");
545 else
546 wxLogMessage("Clicked on static box");
32b8ec41
VZ
547}
548