1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing various static controls
5 // Author: Vadim Zeitlin
7 // Copyright: (c) 2001 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
26 // for all others, include the necessary headers
30 #include "wx/bitmap.h"
31 #include "wx/button.h"
32 #include "wx/checkbox.h"
33 #include "wx/radiobox.h"
34 #include "wx/statbox.h"
35 #include "wx/stattext.h"
36 #include "wx/textctrl.h"
41 #include "wx/statline.h"
42 #include "wx/generic/stattextg.h"
43 #include "wx/wupdlock.h"
46 #include "icons/statbox.xpm"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
55 StaticPage_Reset
= wxID_HIGHEST
,
58 StaticPage_LabelTextWithMarkup
61 // alignment radiobox values
80 StaticEllipsize_Start
,
81 StaticEllipsize_Middle
,
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 class StaticWidgetsPage
: public WidgetsPage
93 StaticWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
94 virtual ~StaticWidgetsPage(){};
96 virtual wxControl
*GetWidget() const { return m_statText
; }
97 virtual Widgets
GetWidgets() const
100 widgets
.push_back(m_sizerStatBox
->GetStaticBox());
101 widgets
.push_back(m_statText
);
103 widgets
.push_back(m_statMarkup
);
104 #endif // wxUSE_MARKUP
106 widgets
.push_back(m_statLine
);
107 #endif // wxUSE_STATLINE
111 virtual void RecreateWidget() { CreateStatic(); }
113 // lazy creation of the content
114 virtual void CreateContent();
118 void OnCheckOrRadioBox(wxCommandEvent
& event
);
120 void OnButtonReset(wxCommandEvent
& event
);
121 void OnButtonBoxText(wxCommandEvent
& event
);
122 void OnButtonLabelText(wxCommandEvent
& event
);
124 void OnButtonLabelWithMarkupText(wxCommandEvent
& event
);
125 #endif // wxUSE_MARKUP
126 void OnMouseEvent(wxMouseEvent
& event
);
128 // reset all parameters
131 // (re)create all controls
137 // the check/radio boxes for styles
138 wxCheckBox
*m_chkVert
,
144 wxCheckBox
*m_chkMarkup
,
146 #endif // wxUSE_MARKUP
148 wxRadioBox
*m_radioHAlign
,
152 // the controls and the sizer containing them
153 wxStaticBoxSizer
*m_sizerStatBox
;
154 wxStaticTextBase
*m_statText
;
157 wxStaticTextBase
*m_statMarkup
;
158 #endif // wxUSE_MARKUP
161 wxStaticLine
*m_statLine
;
162 #endif // wxUSE_STATLINE
163 wxSizer
*m_sizerStatic
;
165 // the text entries for command parameters
166 wxTextCtrl
*m_textBox
,
170 wxTextCtrl
*m_textLabelWithMarkup
;
171 #endif // wxUSE_MARKUP
174 DECLARE_EVENT_TABLE()
175 DECLARE_WIDGETS_PAGE(StaticWidgetsPage
)
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 BEGIN_EVENT_TABLE(StaticWidgetsPage
, WidgetsPage
)
183 EVT_BUTTON(StaticPage_Reset
, StaticWidgetsPage::OnButtonReset
)
184 EVT_BUTTON(StaticPage_LabelText
, StaticWidgetsPage::OnButtonLabelText
)
186 EVT_BUTTON(StaticPage_LabelTextWithMarkup
, StaticWidgetsPage::OnButtonLabelWithMarkupText
)
187 #endif // wxUSE_MARKUP
188 EVT_BUTTON(StaticPage_BoxText
, StaticWidgetsPage::OnButtonBoxText
)
190 EVT_CHECKBOX(wxID_ANY
, StaticWidgetsPage::OnCheckOrRadioBox
)
191 EVT_RADIOBOX(wxID_ANY
, StaticWidgetsPage::OnCheckOrRadioBox
)
194 // ============================================================================
196 // ============================================================================
198 IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage
, wxT("Static"),
199 (int)wxPlatform(GENERIC_CTRLS
).If(wxOS_WINDOWS
,NATIVE_CTRLS
)
202 StaticWidgetsPage::StaticWidgetsPage(WidgetsBookCtrl
*book
,
203 wxImageList
*imaglist
)
204 : WidgetsPage(book
, imaglist
, statbox_xpm
)
212 #endif // wxUSE_MARKUP
216 m_radioVAlign
= (wxRadioBox
*)NULL
;
219 m_statLine
= (wxStaticLine
*)NULL
;
220 #endif // wxUSE_STATLINE
222 m_statText
= m_statMarkup
= NULL
;
223 #endif // wxUSE_MARKUP
225 m_sizerStatBox
= (wxStaticBoxSizer
*)NULL
;
226 m_sizerStatic
= (wxSizer
*)NULL
;
231 m_textLabelWithMarkup
=
232 #endif // wxUSE_MARKUP
236 void StaticWidgetsPage::CreateContent()
238 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
241 wxSizer
*sizerLeft
= new wxStaticBoxSizer(wxVERTICAL
, this, "&Set style");
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
249 static const wxString halign
[] =
256 static const wxString valign
[] =
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);
270 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
271 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
274 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
276 m_chkEllipsize
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Ellipsize"));
278 static const wxString ellipsizeMode
[] =
285 m_radioEllipsize
= new wxRadioBox(this, wxID_ANY
, wxT("&Ellipsize mode"),
286 wxDefaultPosition
, wxDefaultSize
,
287 WXSIZEOF(ellipsizeMode
), ellipsizeMode
,
290 sizerLeft
->Add(m_radioEllipsize
, 0, wxGROW
| wxALL
, 5);
292 wxButton
*btn
= new wxButton(this, StaticPage_Reset
, wxT("&Reset"));
293 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
296 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(wxVERTICAL
, this,
299 m_textBox
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
);
300 wxButton
*b1
= new wxButton(this, wxID_ANY
, "Change &box label");
301 b1
->Connect(wxEVT_BUTTON
,
302 wxCommandEventHandler(StaticWidgetsPage::OnButtonBoxText
),
304 sizerMiddle
->Add(m_textBox
, 0, wxEXPAND
|wxALL
, 5);
305 sizerMiddle
->Add(b1
, 0, wxLEFT
|wxBOTTOM
, 5);
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_BUTTON
,
312 wxCommandEventHandler(StaticWidgetsPage::OnButtonLabelText
),
314 sizerMiddle
->Add(m_textLabel
, 0, wxEXPAND
|wxALL
, 5);
315 sizerMiddle
->Add(b2
, 0, wxLEFT
|wxBOTTOM
, 5);
318 m_textLabelWithMarkup
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
319 wxDefaultPosition
, wxDefaultSize
,
320 wxTE_MULTILINE
|wxHSCROLL
);
322 wxButton
*b3
= new wxButton(this, wxID_ANY
, "Change decorated text label");
323 b3
->Connect(wxEVT_BUTTON
,
324 wxCommandEventHandler(StaticWidgetsPage::OnButtonLabelWithMarkupText
),
326 sizerMiddle
->Add(m_textLabelWithMarkup
, 0, wxEXPAND
|wxALL
, 5);
327 sizerMiddle
->Add(b3
, 0, wxLEFT
|wxBOTTOM
, 5);
329 m_chkGreen
= CreateCheckBoxAndAddToSizer(sizerMiddle
,
330 "Decorated label on g&reen");
331 #endif // wxUSE_MARKUP
333 // final initializations
334 // NB: must be done _before_ calling CreateStatic()
337 m_textBox
->SetValue(wxT("This is a &box"));
338 m_textLabel
->SetValue(wxT("And this is a\n\tlabel inside the box with a &mnemonic.\n")
339 wxT("Only this text is affected by the ellipsize settings."));
341 m_textLabelWithMarkup
->SetValue(wxT("Another label, this time <b>decorated</b> ")
342 wxT("with <u>markup</u>; here you need entities ")
343 wxT("for the symbols: < > & ' " ")
344 wxT(" but you can still place &mnemonics..."));
345 #endif // wxUSE_MARKUP
348 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
349 sizerRight
->SetMinSize(150, 0);
350 m_sizerStatic
= sizerRight
;
354 // the 3 panes panes compose the window
355 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
356 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
357 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
362 // ----------------------------------------------------------------------------
364 // ----------------------------------------------------------------------------
366 void StaticWidgetsPage::Reset()
368 m_chkGeneric
->SetValue(false);
369 m_chkVert
->SetValue(false);
370 m_chkAutoResize
->SetValue(true);
371 m_chkEllipsize
->SetValue(true);
373 m_radioHAlign
->SetSelection(StaticHAlign_Left
);
374 m_radioVAlign
->SetSelection(StaticVAlign_Top
);
377 void StaticWidgetsPage::CreateStatic()
379 wxWindowUpdateLocker
lock(this);
381 bool isVert
= m_chkVert
->GetValue();
383 if ( m_sizerStatBox
)
385 // delete m_sizerStatBox; -- deleted by Remove()
386 m_sizerStatic
->Remove(m_sizerStatBox
);
390 #endif // wxUSE_MARKUP
393 #endif // wxUSE_STATLINE
397 flagsText
= ms_defaultFlags
,
398 flagsDummyText
= ms_defaultFlags
;
400 if ( !m_chkAutoResize
->GetValue() )
402 flagsText
|= wxST_NO_AUTORESIZE
;
403 flagsDummyText
|= wxST_NO_AUTORESIZE
;
407 switch ( m_radioHAlign
->GetSelection() )
410 wxFAIL_MSG(wxT("unexpected radiobox selection"));
413 case StaticHAlign_Left
:
414 align
|= wxALIGN_LEFT
;
417 case StaticHAlign_Centre
:
418 align
|= wxALIGN_CENTRE_HORIZONTAL
;
421 case StaticHAlign_Right
:
422 align
|= wxALIGN_RIGHT
;
426 switch ( m_radioVAlign
->GetSelection() )
429 wxFAIL_MSG(wxT("unexpected radiobox selection"));
432 case StaticVAlign_Top
:
433 align
|= wxALIGN_TOP
;
436 case StaticVAlign_Centre
:
437 align
|= wxALIGN_CENTRE_VERTICAL
;
440 case StaticVAlign_Bottom
:
441 align
|= wxALIGN_BOTTOM
;
445 if ( m_chkEllipsize
->GetValue() )
447 switch ( m_radioEllipsize
->GetSelection() )
450 wxFAIL_MSG(wxT("unexpected radiobox selection"));
453 case StaticEllipsize_Start
:
454 flagsDummyText
|= wxST_ELLIPSIZE_START
;
457 case StaticEllipsize_Middle
:
458 flagsDummyText
|= wxST_ELLIPSIZE_MIDDLE
;
461 case StaticEllipsize_End
:
462 flagsDummyText
|= wxST_ELLIPSIZE_END
;
467 flagsDummyText
|= align
;
471 wxStaticBox
*staticBox
= new wxStaticBox(this, wxID_ANY
,
472 m_textBox
->GetValue(),
473 wxDefaultPosition
, wxDefaultSize
,
475 m_sizerStatBox
= new wxStaticBoxSizer(staticBox
, isVert
? wxHORIZONTAL
478 if ( m_chkGeneric
->GetValue() )
480 m_statText
= new wxGenericStaticText(staticBox
, wxID_ANY
,
481 m_textLabel
->GetValue(),
482 wxDefaultPosition
, wxDefaultSize
,
485 m_statMarkup
= new wxGenericStaticText(staticBox
, wxID_ANY
,
487 wxDefaultPosition
, wxDefaultSize
,
489 #endif // wxUSE_MARKUP
491 else // use native versions
493 m_statText
= new wxStaticText(staticBox
, wxID_ANY
,
494 m_textLabel
->GetValue(),
495 wxDefaultPosition
, wxDefaultSize
,
498 m_statMarkup
= new wxStaticText(staticBox
, wxID_ANY
,
500 wxDefaultPosition
, wxDefaultSize
,
502 #endif // wxUSE_MARKUP
505 m_statText
->SetToolTip("Tooltip for a label inside the box");
508 m_statMarkup
->SetLabelMarkup(m_textLabelWithMarkup
->GetValue());
510 if ( m_chkGreen
->GetValue() )
511 m_statMarkup
->SetBackgroundColour(*wxGREEN
);
512 #endif // wxUSE_MARKUP
515 m_statLine
= new wxStaticLine(staticBox
, wxID_ANY
,
516 wxDefaultPosition
, wxDefaultSize
,
517 isVert
? wxLI_VERTICAL
: wxLI_HORIZONTAL
);
518 #endif // wxUSE_STATLINE
520 m_sizerStatBox
->Add(m_statText
, 0, wxGROW
| wxALL
, 5);
522 m_sizerStatBox
->Add(m_statLine
, 0, wxGROW
| wxALL
, 5);
523 #endif // wxUSE_STATLINE
525 m_sizerStatBox
->Add(m_statMarkup
, 0, wxALL
, 5);
526 #endif // wxUSE_MARKUP
528 m_sizerStatic
->Add(m_sizerStatBox
, 0, wxGROW
);
530 m_sizerStatic
->Layout();
532 m_statText
->Connect(wxEVT_LEFT_UP
,
533 wxMouseEventHandler(StaticWidgetsPage::OnMouseEvent
),
535 staticBox
->Connect(wxEVT_LEFT_UP
,
536 wxMouseEventHandler(StaticWidgetsPage::OnMouseEvent
),
540 // ----------------------------------------------------------------------------
542 // ----------------------------------------------------------------------------
544 void StaticWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
551 void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)
553 if (event
.GetEventObject() == static_cast<wxObject
*>(m_chkEllipsize
))
555 m_radioEllipsize
->Enable(event
.IsChecked());
561 void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent
& WXUNUSED(event
))
563 m_sizerStatBox
->GetStaticBox()->SetLabel(m_textBox
->GetValue());
566 void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent
& WXUNUSED(event
))
568 m_statText
->SetLabel(m_textLabel
->GetValue());
570 // test GetLabel() and GetLabelText(); the first should return the
571 // label as it is written in the relative text control; the second should
572 // return the label as it's shown in the wxStaticText
573 wxLogMessage(wxT("The original label should be '%s'"),
574 m_statText
->GetLabel());
575 wxLogMessage(wxT("The label text is '%s'"),
576 m_statText
->GetLabelText());
580 void StaticWidgetsPage::OnButtonLabelWithMarkupText(wxCommandEvent
& WXUNUSED(event
))
582 m_statMarkup
->SetLabelMarkup(m_textLabelWithMarkup
->GetValue());
584 // test GetLabel() and GetLabelText(); the first should return the
585 // label as it is written in the relative text control; the second should
586 // return the label as it's shown in the wxStaticText
587 wxLogMessage(wxT("The original label should be '%s'"),
588 m_statMarkup
->GetLabel());
589 wxLogMessage(wxT("The label text is '%s'"),
590 m_statMarkup
->GetLabelText());
592 #endif // wxUSE_MARKUP
594 void StaticWidgetsPage::OnMouseEvent(wxMouseEvent
& event
)
596 if ( event
.GetEventObject() == m_statText
)
598 wxLogMessage("Clicked on static text");
602 wxLogMessage("Clicked on static box");