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