1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing various static controls
5 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers
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"
42 #include "wx/statline.h"
43 #include "wx/generic/stattextg.h"
44 #include "wx/wupdlock.h"
47 #include "icons/statbox.xpm"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
56 StaticPage_Reset
= wxID_HIGHEST
,
59 StaticPage_LabelTextWithMarkup
62 // alignment radiobox values
81 StaticEllipsize_Start
,
82 StaticEllipsize_Middle
,
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 class StaticWidgetsPage
: public WidgetsPage
94 StaticWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
95 virtual ~StaticWidgetsPage(){};
97 virtual wxControl
*GetWidget() const { return m_statText
; }
98 virtual Widgets
GetWidgets() const
101 widgets
.push_back(m_sizerStatBox
->GetStaticBox());
102 widgets
.push_back(m_statText
);
104 widgets
.push_back(m_statMarkup
);
105 #endif // wxUSE_MARKUP
107 widgets
.push_back(m_statLine
);
108 #endif // wxUSE_STATLINE
112 virtual void RecreateWidget() { CreateStatic(); }
114 // lazy creation of the content
115 virtual void CreateContent();
119 void OnCheckOrRadioBox(wxCommandEvent
& event
);
121 void OnButtonReset(wxCommandEvent
& event
);
122 void OnButtonBoxText(wxCommandEvent
& event
);
123 void OnButtonLabelText(wxCommandEvent
& event
);
125 void OnButtonLabelWithMarkupText(wxCommandEvent
& event
);
126 #endif // wxUSE_MARKUP
127 void OnMouseEvent(wxMouseEvent
& event
);
129 // reset all parameters
132 // (re)create all controls
138 // the check/radio boxes for styles
139 wxCheckBox
*m_chkVert
,
145 wxCheckBox
*m_chkMarkup
,
147 #endif // wxUSE_MARKUP
149 wxRadioBox
*m_radioHAlign
,
153 // the controls and the sizer containing them
154 wxStaticBoxSizer
*m_sizerStatBox
;
155 wxStaticTextBase
*m_statText
;
158 wxStaticTextBase
*m_statMarkup
;
159 #endif // wxUSE_MARKUP
162 wxStaticLine
*m_statLine
;
163 #endif // wxUSE_STATLINE
164 wxSizer
*m_sizerStatic
;
166 // the text entries for command parameters
167 wxTextCtrl
*m_textBox
,
171 wxTextCtrl
*m_textLabelWithMarkup
;
172 #endif // wxUSE_MARKUP
175 DECLARE_EVENT_TABLE()
176 DECLARE_WIDGETS_PAGE(StaticWidgetsPage
)
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 BEGIN_EVENT_TABLE(StaticWidgetsPage
, WidgetsPage
)
184 EVT_BUTTON(StaticPage_Reset
, StaticWidgetsPage::OnButtonReset
)
185 EVT_BUTTON(StaticPage_LabelText
, StaticWidgetsPage::OnButtonLabelText
)
187 EVT_BUTTON(StaticPage_LabelTextWithMarkup
, StaticWidgetsPage::OnButtonLabelWithMarkupText
)
188 #endif // wxUSE_MARKUP
189 EVT_BUTTON(StaticPage_BoxText
, StaticWidgetsPage::OnButtonBoxText
)
191 EVT_CHECKBOX(wxID_ANY
, StaticWidgetsPage::OnCheckOrRadioBox
)
192 EVT_RADIOBOX(wxID_ANY
, StaticWidgetsPage::OnCheckOrRadioBox
)
195 // ============================================================================
197 // ============================================================================
199 IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage
, wxT("Static"),
200 (int)wxPlatform(GENERIC_CTRLS
).If(wxOS_WINDOWS
,NATIVE_CTRLS
)
203 StaticWidgetsPage::StaticWidgetsPage(WidgetsBookCtrl
*book
,
204 wxImageList
*imaglist
)
205 : WidgetsPage(book
, imaglist
, statbox_xpm
)
213 #endif // wxUSE_MARKUP
217 m_radioVAlign
= (wxRadioBox
*)NULL
;
220 m_statLine
= (wxStaticLine
*)NULL
;
221 #endif // wxUSE_STATLINE
223 m_statText
= m_statMarkup
= NULL
;
224 #endif // wxUSE_MARKUP
226 m_sizerStatBox
= (wxStaticBoxSizer
*)NULL
;
227 m_sizerStatic
= (wxSizer
*)NULL
;
232 m_textLabelWithMarkup
=
233 #endif // wxUSE_MARKUP
237 void StaticWidgetsPage::CreateContent()
239 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
242 wxSizer
*sizerLeft
= new wxStaticBoxSizer(wxVERTICAL
, this, "&Set style");
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
250 static const wxString halign
[] =
257 static const wxString valign
[] =
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);
271 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
272 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
275 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
277 m_chkEllipsize
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Ellipsize"));
279 static const wxString ellipsizeMode
[] =
286 m_radioEllipsize
= new wxRadioBox(this, wxID_ANY
, wxT("&Ellipsize mode"),
287 wxDefaultPosition
, wxDefaultSize
,
288 WXSIZEOF(ellipsizeMode
), ellipsizeMode
,
291 sizerLeft
->Add(m_radioEllipsize
, 0, wxGROW
| wxALL
, 5);
293 wxButton
*btn
= new wxButton(this, StaticPage_Reset
, wxT("&Reset"));
294 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
297 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(wxVERTICAL
, this,
300 m_textBox
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
);
301 wxButton
*b1
= new wxButton(this, wxID_ANY
, "Change &box label");
302 b1
->Connect(wxEVT_BUTTON
,
303 wxCommandEventHandler(StaticWidgetsPage::OnButtonBoxText
),
305 sizerMiddle
->Add(m_textBox
, 0, wxEXPAND
|wxALL
, 5);
306 sizerMiddle
->Add(b1
, 0, wxLEFT
|wxBOTTOM
, 5);
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_BUTTON
,
313 wxCommandEventHandler(StaticWidgetsPage::OnButtonLabelText
),
315 sizerMiddle
->Add(m_textLabel
, 0, wxEXPAND
|wxALL
, 5);
316 sizerMiddle
->Add(b2
, 0, wxLEFT
|wxBOTTOM
, 5);
319 m_textLabelWithMarkup
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
320 wxDefaultPosition
, wxDefaultSize
,
321 wxTE_MULTILINE
|wxHSCROLL
);
323 wxButton
*b3
= new wxButton(this, wxID_ANY
, "Change decorated text label");
324 b3
->Connect(wxEVT_BUTTON
,
325 wxCommandEventHandler(StaticWidgetsPage::OnButtonLabelWithMarkupText
),
327 sizerMiddle
->Add(m_textLabelWithMarkup
, 0, wxEXPAND
|wxALL
, 5);
328 sizerMiddle
->Add(b3
, 0, wxLEFT
|wxBOTTOM
, 5);
330 m_chkGreen
= CreateCheckBoxAndAddToSizer(sizerMiddle
,
331 "Decorated label on g&reen");
332 #endif // wxUSE_MARKUP
334 // final initializations
335 // NB: must be done _before_ calling CreateStatic()
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."));
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: < > & ' " ")
345 wxT(" but you can still place &mnemonics..."));
346 #endif // wxUSE_MARKUP
349 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
350 sizerRight
->SetMinSize(150, 0);
351 m_sizerStatic
= sizerRight
;
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);
363 // ----------------------------------------------------------------------------
365 // ----------------------------------------------------------------------------
367 void StaticWidgetsPage::Reset()
369 m_chkGeneric
->SetValue(false);
370 m_chkVert
->SetValue(false);
371 m_chkAutoResize
->SetValue(true);
372 m_chkEllipsize
->SetValue(true);
374 m_radioHAlign
->SetSelection(StaticHAlign_Left
);
375 m_radioVAlign
->SetSelection(StaticVAlign_Top
);
378 void StaticWidgetsPage::CreateStatic()
380 wxWindowUpdateLocker
lock(this);
382 bool isVert
= m_chkVert
->GetValue();
384 if ( m_sizerStatBox
)
386 // delete m_sizerStatBox; -- deleted by Remove()
387 m_sizerStatic
->Remove(m_sizerStatBox
);
391 #endif // wxUSE_MARKUP
394 #endif // wxUSE_STATLINE
398 flagsText
= ms_defaultFlags
,
399 flagsDummyText
= ms_defaultFlags
;
401 if ( !m_chkAutoResize
->GetValue() )
403 flagsText
|= wxST_NO_AUTORESIZE
;
404 flagsDummyText
|= wxST_NO_AUTORESIZE
;
408 switch ( m_radioHAlign
->GetSelection() )
411 wxFAIL_MSG(wxT("unexpected radiobox selection"));
414 case StaticHAlign_Left
:
415 align
|= wxALIGN_LEFT
;
418 case StaticHAlign_Centre
:
419 align
|= wxALIGN_CENTRE_HORIZONTAL
;
422 case StaticHAlign_Right
:
423 align
|= wxALIGN_RIGHT
;
427 switch ( m_radioVAlign
->GetSelection() )
430 wxFAIL_MSG(wxT("unexpected radiobox selection"));
433 case StaticVAlign_Top
:
434 align
|= wxALIGN_TOP
;
437 case StaticVAlign_Centre
:
438 align
|= wxALIGN_CENTRE_VERTICAL
;
441 case StaticVAlign_Bottom
:
442 align
|= wxALIGN_BOTTOM
;
446 if ( m_chkEllipsize
->GetValue() )
448 switch ( m_radioEllipsize
->GetSelection() )
451 wxFAIL_MSG(wxT("unexpected radiobox selection"));
454 case StaticEllipsize_Start
:
455 flagsDummyText
|= wxST_ELLIPSIZE_START
;
458 case StaticEllipsize_Middle
:
459 flagsDummyText
|= wxST_ELLIPSIZE_MIDDLE
;
462 case StaticEllipsize_End
:
463 flagsDummyText
|= wxST_ELLIPSIZE_END
;
468 flagsDummyText
|= align
;
472 wxStaticBox
*staticBox
= new wxStaticBox(this, wxID_ANY
,
473 m_textBox
->GetValue(),
474 wxDefaultPosition
, wxDefaultSize
,
476 m_sizerStatBox
= new wxStaticBoxSizer(staticBox
, isVert
? wxHORIZONTAL
479 if ( m_chkGeneric
->GetValue() )
481 m_statText
= new wxGenericStaticText(this, wxID_ANY
,
482 m_textLabel
->GetValue(),
483 wxDefaultPosition
, wxDefaultSize
,
486 m_statMarkup
= new wxGenericStaticText(this, wxID_ANY
,
488 wxDefaultPosition
, wxDefaultSize
,
490 #endif // wxUSE_MARKUP
492 else // use native versions
494 m_statText
= new wxStaticText(this, wxID_ANY
,
495 m_textLabel
->GetValue(),
496 wxDefaultPosition
, wxDefaultSize
,
499 m_statMarkup
= new wxStaticText(this, wxID_ANY
,
501 wxDefaultPosition
, wxDefaultSize
,
503 #endif // wxUSE_MARKUP
507 m_statMarkup
->SetLabelMarkup(m_textLabelWithMarkup
->GetValue());
509 if ( m_chkGreen
->GetValue() )
510 m_statMarkup
->SetBackgroundColour(*wxGREEN
);
511 #endif // wxUSE_MARKUP
514 m_statLine
= new wxStaticLine(this, wxID_ANY
,
515 wxDefaultPosition
, wxDefaultSize
,
516 isVert
? wxLI_VERTICAL
: wxLI_HORIZONTAL
);
517 #endif // wxUSE_STATLINE
519 m_sizerStatBox
->Add(m_statText
, 1, wxGROW
| wxALL
, 5);
521 m_sizerStatBox
->Add(m_statLine
, 0, wxGROW
| wxALL
, 5);
522 #endif // wxUSE_STATLINE
524 m_sizerStatBox
->Add(m_statMarkup
, 1, wxGROW
| wxALL
, 5);
525 #endif // wxUSE_MARKUP
527 m_sizerStatic
->Add(m_sizerStatBox
, 1, wxGROW
);
529 m_sizerStatic
->Layout();
531 m_statText
->Connect(wxEVT_LEFT_UP
,
532 wxMouseEventHandler(StaticWidgetsPage::OnMouseEvent
),
534 staticBox
->Connect(wxEVT_LEFT_UP
,
535 wxMouseEventHandler(StaticWidgetsPage::OnMouseEvent
),
539 // ----------------------------------------------------------------------------
541 // ----------------------------------------------------------------------------
543 void StaticWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
550 void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)
552 if (event
.GetEventObject() == static_cast<wxObject
*>(m_chkEllipsize
))
554 m_radioEllipsize
->Enable(event
.IsChecked());
560 void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent
& WXUNUSED(event
))
562 m_sizerStatBox
->GetStaticBox()->SetLabel(m_textBox
->GetValue());
565 void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent
& WXUNUSED(event
))
567 m_statText
->SetLabel(m_textLabel
->GetValue());
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());
579 void StaticWidgetsPage::OnButtonLabelWithMarkupText(wxCommandEvent
& WXUNUSED(event
))
581 m_statMarkup
->SetLabelMarkup(m_textLabelWithMarkup
->GetValue());
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());
591 #endif // wxUSE_MARKUP
593 void StaticWidgetsPage::OnMouseEvent(wxMouseEvent
& event
)
595 if ( event
.GetEventObject() == m_statText
)
597 wxLogMessage("Clicked on static text");
601 wxLogMessage("Clicked on static box");