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