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