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