]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/static.cpp
Fixed several style, paste and undo bugs
[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
44 #include "widgets.h"
45 #include "icons/statbox.xpm"
46
47 // ----------------------------------------------------------------------------
48 // constants
49 // ----------------------------------------------------------------------------
50
51 // control ids
52 enum
53 {
54 StaticPage_Reset = wxID_HIGHEST,
55 StaticPage_BoxText,
56 StaticPage_LabelText,
57 StaticPage_LabelTextWithMarkup
58 };
59
60 // alignment radiobox values
61 enum
62 {
63 StaticHAlign_Left,
64 StaticHAlign_Centre,
65 StaticHAlign_Right,
66 StaticHAlign_Max
67 };
68
69 enum
70 {
71 StaticVAlign_Top,
72 StaticVAlign_Centre,
73 StaticVAlign_Bottom,
74 StaticVAlign_Max
75 };
76
77 enum
78 {
79 StaticEllipsize_Start,
80 StaticEllipsize_Middle,
81 StaticEllipsize_End
82 };
83
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
91 class MyStaticText : public wxStaticText
92 {
93 public:
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)
101 {
102 }
103
104 protected:
105 void OnMouseEvent(wxMouseEvent& WXUNUSED(event))
106 {
107 wxLogMessage(wxT("Clicked on static text"));
108 }
109
110 DECLARE_EVENT_TABLE()
111 };
112
113 class MyStaticBox : public wxStaticBox
114 {
115 public:
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
126 protected:
127 void OnMouseEvent(wxMouseEvent& WXUNUSED(event))
128 {
129 wxLogMessage(wxT("Clicked on static box"));
130 }
131
132 DECLARE_EVENT_TABLE()
133 };
134
135 BEGIN_EVENT_TABLE(MyStaticText, wxStaticText)
136 EVT_LEFT_UP(MyStaticText::OnMouseEvent)
137 END_EVENT_TABLE()
138
139 BEGIN_EVENT_TABLE(MyStaticBox, wxStaticBox)
140 EVT_LEFT_UP(MyStaticBox::OnMouseEvent)
141 END_EVENT_TABLE()
142
143 // ----------------------------------------------------------------------------
144 // StaticWidgetsPage
145 // ----------------------------------------------------------------------------
146
147 class StaticWidgetsPage : public WidgetsPage
148 {
149 public:
150 StaticWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
151 virtual ~StaticWidgetsPage(){};
152
153 virtual wxControl *GetWidget() const { return m_statText; }
154 virtual void RecreateWidget() { CreateStatic(); }
155
156 // lazy creation of the content
157 virtual void CreateContent();
158
159 protected:
160 // event handlers
161 void OnCheckOrRadioBox(wxCommandEvent& event);
162
163 void OnButtonReset(wxCommandEvent& event);
164 void OnButtonBoxText(wxCommandEvent& event);
165 void OnButtonLabelText(wxCommandEvent& event);
166 void OnButtonLabelWithMarkupText(wxCommandEvent& event);
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,
179 *m_chkAutoResize,
180 *m_chkEllipsize,
181 *m_chkMarkup;
182
183 wxRadioBox *m_radioHAlign,
184 *m_radioVAlign,
185 *m_radioEllipsize;
186
187 // the controls and the sizer containing them
188 wxStaticBox *m_staticBox;
189 wxStaticBoxSizer *m_sizerStatBox;
190 wxStaticText *m_statText,
191 *m_statTextWithMarkup;
192 #if wxUSE_STATLINE
193 wxStaticLine *m_statLine;
194 #endif // wxUSE_STATLINE
195 wxSizer *m_sizerStatic;
196
197 // the text entries for command parameters
198 wxTextCtrl *m_textBox,
199 *m_textLabel,
200 *m_textLabelWithMarkup;
201
202 private:
203 DECLARE_EVENT_TABLE()
204 DECLARE_WIDGETS_PAGE(StaticWidgetsPage)
205 };
206
207 // ----------------------------------------------------------------------------
208 // event tables
209 // ----------------------------------------------------------------------------
210
211 BEGIN_EVENT_TABLE(StaticWidgetsPage, WidgetsPage)
212 EVT_BUTTON(StaticPage_Reset, StaticWidgetsPage::OnButtonReset)
213 EVT_BUTTON(StaticPage_LabelText, StaticWidgetsPage::OnButtonLabelText)
214 EVT_BUTTON(StaticPage_LabelTextWithMarkup, StaticWidgetsPage::OnButtonLabelWithMarkupText)
215 EVT_BUTTON(StaticPage_BoxText, StaticWidgetsPage::OnButtonBoxText)
216
217 EVT_CHECKBOX(wxID_ANY, StaticWidgetsPage::OnCheckOrRadioBox)
218 EVT_RADIOBOX(wxID_ANY, StaticWidgetsPage::OnCheckOrRadioBox)
219 END_EVENT_TABLE()
220
221 // ============================================================================
222 // implementation
223 // ============================================================================
224
225 IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage, _T("Static"),
226 (int)wxPlatform(GENERIC_CTRLS).If(wxOS_WINDOWS,NATIVE_CTRLS)
227 );
228
229 StaticWidgetsPage::StaticWidgetsPage(WidgetsBookCtrl *book,
230 wxImageList *imaglist)
231 : WidgetsPage(book, imaglist, statbox_xpm)
232 {
233 // init everything
234 m_chkVert =
235 m_chkAutoResize = (wxCheckBox *)NULL;
236
237 m_radioHAlign =
238 m_radioVAlign = (wxRadioBox *)NULL;
239
240 #if wxUSE_STATLINE
241 m_statLine = (wxStaticLine *)NULL;
242 #endif // wxUSE_STATLINE
243 m_statText = m_statTextWithMarkup = (wxStaticText *)NULL;
244
245 m_staticBox = (wxStaticBox *)NULL;
246 m_sizerStatBox = (wxStaticBoxSizer *)NULL;
247 m_sizerStatic = (wxSizer *)NULL;
248
249 m_textBox = m_textLabel = m_textLabelWithMarkup = NULL;
250 }
251
252 void StaticWidgetsPage::CreateContent()
253 {
254 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
255
256 // left pane
257 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
258
259 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
260
261 m_chkMarkup = CreateCheckBoxAndAddToSizer(sizerLeft, _T("Support &markup"));
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
280 m_radioHAlign = new wxRadioBox(this, wxID_ANY, _T("&Horz alignment"),
281 wxDefaultPosition, wxDefaultSize,
282 WXSIZEOF(halign), halign);
283 m_radioVAlign = new wxRadioBox(this, wxID_ANY, _T("&Vert alignment"),
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
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
308 wxButton *btn = new wxButton(this, StaticPage_Reset, _T("&Reset"));
309 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
310
311 // middle pane
312 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Change labels"));
313 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
314
315 wxSizer *sizerRow;
316
317 sizerRow = CreateSizerWithTextAndButton(StaticPage_BoxText,
318 _T("Change &box label"),
319 wxID_ANY, &m_textBox);
320 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
321
322 sizerRow = CreateSizerWithTextAndButton(StaticPage_LabelText,
323 _T("Change &text label"),
324 wxID_ANY, &m_textLabel);
325 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
326
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
337 m_textBox->SetValue(_T("This is a box"));
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..."));
344
345 // right pane
346 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
347 sizerRight->SetMinSize(150, 0);
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);
354 sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10);
355 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
356
357 SetSizer(sizerTop);
358 }
359
360 // ----------------------------------------------------------------------------
361 // operations
362 // ----------------------------------------------------------------------------
363
364 void StaticWidgetsPage::Reset()
365 {
366 m_chkVert->SetValue(false);
367 m_chkAutoResize->SetValue(true);
368 m_chkEllipsize->SetValue(true);
369 m_chkMarkup->SetValue(true);
370
371 m_radioHAlign->SetSelection(StaticHAlign_Left);
372 m_radioVAlign->SetSelection(StaticVAlign_Top);
373 }
374
375 void StaticWidgetsPage::CreateStatic()
376 {
377 bool isVert = m_chkVert->GetValue();
378
379 if ( m_sizerStatBox )
380 {
381 delete m_staticBox;
382 // delete m_sizerStatBox; -- deleted by Remove()
383 m_sizerStatic->Remove(m_sizerStatBox);
384 delete m_statText;
385 delete m_statTextWithMarkup;
386 #if wxUSE_STATLINE
387 delete m_statLine;
388 #endif // wxUSE_STATLINE
389 }
390
391 int flagsBox = 0,
392 flagsText = ms_defaultFlags,
393 flagsDummyText = ms_defaultFlags;
394
395 if ( !m_chkAutoResize->GetValue() )
396 {
397 flagsText |= wxST_NO_AUTORESIZE;
398 flagsDummyText |= wxST_NO_AUTORESIZE;
399 }
400
401 if ( m_chkMarkup->GetValue() )
402 {
403 flagsText |= wxST_MARKUP;
404 flagsDummyText |= wxST_MARKUP;
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
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;
469 flagsText |= align;
470 flagsBox |= align;
471
472 m_staticBox = new MyStaticBox(this, wxID_ANY, m_textBox->GetValue(),
473 wxDefaultPosition, wxDefaultSize,
474 flagsBox);
475 m_sizerStatBox = new wxStaticBoxSizer(m_staticBox, isVert ? wxHORIZONTAL
476 : wxVERTICAL);
477
478 m_statText = new MyStaticText(this, wxID_ANY, m_textLabel->GetValue(),
479 wxDefaultPosition, wxDefaultSize,
480 flagsDummyText);
481 m_statTextWithMarkup = new wxStaticText(this, wxID_ANY,
482 m_textLabelWithMarkup->GetValue(),
483 wxDefaultPosition, wxDefaultSize,
484 flagsText);
485
486 #if wxUSE_STATLINE
487 m_statLine = new wxStaticLine(this, wxID_ANY,
488 wxDefaultPosition, wxDefaultSize,
489 isVert ? wxLI_VERTICAL : wxLI_HORIZONTAL);
490 #endif // wxUSE_STATLINE
491
492 m_sizerStatBox->Add(m_statText, 1, wxGROW | wxALL, 5);
493 #if wxUSE_STATLINE
494 m_sizerStatBox->Add(m_statLine, 0, wxGROW | wxALL, 5);
495 #endif // wxUSE_STATLINE
496 m_sizerStatBox->Add(m_statTextWithMarkup, 1, wxGROW | wxALL, 5);
497
498 m_sizerStatic->Add(m_sizerStatBox, 1, wxGROW);
499
500 m_sizerStatic->Layout();
501 }
502
503 // ----------------------------------------------------------------------------
504 // event handlers
505 // ----------------------------------------------------------------------------
506
507 void StaticWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
508 {
509 Reset();
510
511 CreateStatic();
512 }
513
514 void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
515 {
516 if (event.GetEventObject() == wx_static_cast(wxObject*, m_chkEllipsize))
517 {
518 m_radioEllipsize->Enable(event.IsChecked());
519 }
520
521 CreateStatic();
522 }
523
524 void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent& WXUNUSED(event))
525 {
526 m_sizerStatBox->GetStaticBox()->SetLabel(m_textBox->GetValue());
527 }
528
529 void 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
542 void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent& WXUNUSED(event))
543 {
544 m_statText->SetLabel(m_textLabel->GetValue());
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());
553 }
554