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