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 // License: wxWindows license
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"
45 #include "icons/statbox.xpm"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
54 StaticPage_Reset
= wxID_HIGHEST
,
57 StaticPage_LabelTextWithMarkup
60 // alignment radiobox values
79 StaticEllipsize_Start
,
80 StaticEllipsize_Middle
,
84 // ----------------------------------------------------------------------------
85 // MyStaticText and MyStaticBox
86 // ----------------------------------------------------------------------------
88 // these 2 classes simply show that the static controls can get the mouse
89 // clicks too -- this used to be broken under MSW but works now
91 class MyStaticText
: public wxStaticText
94 MyStaticText(wxWindow
* parent
,
96 const wxString
& label
,
97 const wxPoint
& pos
= wxDefaultPosition
,
98 const wxSize
& size
= wxDefaultSize
,
100 : wxStaticText(parent
, id
, label
, pos
, size
, style
)
105 void OnMouseEvent(wxMouseEvent
& WXUNUSED(event
))
107 wxLogMessage(wxT("Clicked on static text"));
110 DECLARE_EVENT_TABLE()
113 class MyStaticBox
: public wxStaticBox
116 MyStaticBox(wxWindow
* parent
,
118 const wxString
& label
,
119 const wxPoint
& pos
= wxDefaultPosition
,
120 const wxSize
& size
= wxDefaultSize
,
122 : wxStaticBox(parent
, id
, label
, pos
, size
, style
)
127 void OnMouseEvent(wxMouseEvent
& WXUNUSED(event
))
129 wxLogMessage(wxT("Clicked on static box"));
132 DECLARE_EVENT_TABLE()
135 BEGIN_EVENT_TABLE(MyStaticText
, wxStaticText
)
136 EVT_LEFT_UP(MyStaticText::OnMouseEvent
)
139 BEGIN_EVENT_TABLE(MyStaticBox
, wxStaticBox
)
140 EVT_LEFT_UP(MyStaticBox::OnMouseEvent
)
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 class StaticWidgetsPage
: public WidgetsPage
150 StaticWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
151 virtual ~StaticWidgetsPage(){};
153 virtual wxControl
*GetWidget() const { return m_statText
; }
154 virtual void RecreateWidget() { CreateStatic(); }
156 // lazy creation of the content
157 virtual void CreateContent();
161 void OnCheckOrRadioBox(wxCommandEvent
& event
);
163 void OnButtonReset(wxCommandEvent
& event
);
164 void OnButtonBoxText(wxCommandEvent
& event
);
165 void OnButtonLabelText(wxCommandEvent
& event
);
166 void OnButtonLabelWithMarkupText(wxCommandEvent
& event
);
168 // reset all parameters
171 // (re)create all controls
177 // the check/radio boxes for styles
178 wxCheckBox
*m_chkVert
,
183 wxRadioBox
*m_radioHAlign
,
187 // the controls and the sizer containing them
188 wxStaticBox
*m_staticBox
;
189 wxStaticBoxSizer
*m_sizerStatBox
;
190 wxStaticText
*m_statText
,
191 *m_statTextWithMarkup
;
193 wxStaticLine
*m_statLine
;
194 #endif // wxUSE_STATLINE
195 wxSizer
*m_sizerStatic
;
197 // the text entries for command parameters
198 wxTextCtrl
*m_textBox
,
200 *m_textLabelWithMarkup
;
203 DECLARE_EVENT_TABLE()
204 DECLARE_WIDGETS_PAGE(StaticWidgetsPage
)
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
211 BEGIN_EVENT_TABLE(StaticWidgetsPage
, WidgetsPage
)
212 EVT_BUTTON(StaticPage_Reset
, StaticWidgetsPage::OnButtonReset
)
213 EVT_BUTTON(StaticPage_LabelText
, StaticWidgetsPage::OnButtonLabelText
)
214 EVT_BUTTON(StaticPage_LabelTextWithMarkup
, StaticWidgetsPage::OnButtonLabelWithMarkupText
)
215 EVT_BUTTON(StaticPage_BoxText
, StaticWidgetsPage::OnButtonBoxText
)
217 EVT_CHECKBOX(wxID_ANY
, StaticWidgetsPage::OnCheckOrRadioBox
)
218 EVT_RADIOBOX(wxID_ANY
, StaticWidgetsPage::OnCheckOrRadioBox
)
221 // ============================================================================
223 // ============================================================================
225 IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage
, _T("Static"),
226 (int)wxPlatform(GENERIC_CTRLS
).If(wxOS_WINDOWS
,NATIVE_CTRLS
)
229 StaticWidgetsPage::StaticWidgetsPage(WidgetsBookCtrl
*book
,
230 wxImageList
*imaglist
)
231 : WidgetsPage(book
, imaglist
, statbox_xpm
)
235 m_chkAutoResize
= (wxCheckBox
*)NULL
;
238 m_radioVAlign
= (wxRadioBox
*)NULL
;
241 m_statLine
= (wxStaticLine
*)NULL
;
242 #endif // wxUSE_STATLINE
243 m_statText
= m_statTextWithMarkup
= (wxStaticText
*)NULL
;
245 m_staticBox
= (wxStaticBox
*)NULL
;
246 m_sizerStatBox
= (wxStaticBoxSizer
*)NULL
;
247 m_sizerStatic
= (wxSizer
*)NULL
;
249 m_textBox
= m_textLabel
= m_textLabelWithMarkup
= NULL
;
252 void StaticWidgetsPage::CreateContent()
254 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
257 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
259 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
261 m_chkMarkup
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Support &markup"));
262 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical line"));
263 m_chkAutoResize
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Fit to text"));
264 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
266 static const wxString halign
[] =
273 static const wxString valign
[] =
280 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Horz alignment"),
281 wxDefaultPosition
, wxDefaultSize
,
282 WXSIZEOF(halign
), halign
);
283 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Vert alignment"),
284 wxDefaultPosition
, wxDefaultSize
,
285 WXSIZEOF(valign
), valign
);
287 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
288 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
291 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
293 m_chkEllipsize
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Ellipsize"));
295 static const wxString ellipsizeMode
[] =
302 m_radioEllipsize
= new wxRadioBox(this, wxID_ANY
, _T("&Ellipsize mode"),
303 wxDefaultPosition
, wxDefaultSize
,
304 WXSIZEOF(ellipsizeMode
), ellipsizeMode
);
306 sizerLeft
->Add(m_radioEllipsize
, 0, wxGROW
| wxALL
, 5);
308 wxButton
*btn
= new wxButton(this, StaticPage_Reset
, _T("&Reset"));
309 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
312 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change labels"));
313 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
317 sizerRow
= CreateSizerWithTextAndButton(StaticPage_BoxText
,
318 _T("Change &box label"),
319 wxID_ANY
, &m_textBox
);
320 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
322 sizerRow
= CreateSizerWithTextAndButton(StaticPage_LabelText
,
323 _T("Change &text label"),
324 wxID_ANY
, &m_textLabel
);
325 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
327 sizerRow
= CreateSizerWithTextAndButton(StaticPage_LabelTextWithMarkup
,
328 _T("Change decorated text label"),
329 wxID_ANY
, &m_textLabelWithMarkup
);
330 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
333 // final initializations
334 // NB: must be done _before_ calling CreateStatic()
337 m_textBox
->SetValue(_T("This is a box"));
338 m_textLabel
->SetValue(_T("And this is a\n\tlabel inside the box with a &mnemonic.\n")
339 _T("Only this text is affected by the ellipsize settings."));
340 m_textLabelWithMarkup
->SetValue(_T("Another label, this time <b>decorated</b> ")
341 _T("with <u>markup</u>; here you need entities ")
342 _T("for the symbols: < > & ' " ")
343 _T(" but you can still place &mnemonics..."));
346 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
347 sizerRight
->SetMinSize(150, 0);
348 m_sizerStatic
= sizerRight
;
352 // the 3 panes panes compose the window
353 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
354 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
355 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
360 // ----------------------------------------------------------------------------
362 // ----------------------------------------------------------------------------
364 void StaticWidgetsPage::Reset()
366 m_chkVert
->SetValue(false);
367 m_chkAutoResize
->SetValue(true);
368 m_chkEllipsize
->SetValue(true);
369 m_chkMarkup
->SetValue(true);
371 m_radioHAlign
->SetSelection(StaticHAlign_Left
);
372 m_radioVAlign
->SetSelection(StaticVAlign_Top
);
375 void StaticWidgetsPage::CreateStatic()
377 bool isVert
= m_chkVert
->GetValue();
379 if ( m_sizerStatBox
)
382 // delete m_sizerStatBox; -- deleted by Remove()
383 m_sizerStatic
->Remove(m_sizerStatBox
);
385 delete m_statTextWithMarkup
;
388 #endif // wxUSE_STATLINE
392 flagsText
= ms_defaultFlags
,
393 flagsDummyText
= ms_defaultFlags
;
395 if ( !m_chkAutoResize
->GetValue() )
397 flagsText
|= wxST_NO_AUTORESIZE
;
398 flagsDummyText
|= wxST_NO_AUTORESIZE
;
401 if ( m_chkMarkup
->GetValue() )
403 flagsText
|= wxST_MARKUP
;
404 flagsDummyText
|= wxST_MARKUP
;
408 switch ( m_radioHAlign
->GetSelection() )
411 wxFAIL_MSG(_T("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(_T("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(_T("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 m_staticBox
= new MyStaticBox(this, wxID_ANY
, m_textBox
->GetValue(),
473 wxDefaultPosition
, wxDefaultSize
,
475 m_sizerStatBox
= new wxStaticBoxSizer(m_staticBox
, isVert
? wxHORIZONTAL
478 m_statText
= new MyStaticText(this, wxID_ANY
, m_textLabel
->GetValue(),
479 wxDefaultPosition
, wxDefaultSize
,
481 m_statTextWithMarkup
= new wxStaticText(this, wxID_ANY
,
482 m_textLabelWithMarkup
->GetValue(),
483 wxDefaultPosition
, wxDefaultSize
,
487 m_statLine
= new wxStaticLine(this, wxID_ANY
,
488 wxDefaultPosition
, wxDefaultSize
,
489 isVert
? wxLI_VERTICAL
: wxLI_HORIZONTAL
);
490 #endif // wxUSE_STATLINE
492 m_sizerStatBox
->Add(m_statText
, 1, wxGROW
| wxALL
, 5);
494 m_sizerStatBox
->Add(m_statLine
, 0, wxGROW
| wxALL
, 5);
495 #endif // wxUSE_STATLINE
496 m_sizerStatBox
->Add(m_statTextWithMarkup
, 1, wxGROW
| wxALL
, 5);
498 m_sizerStatic
->Add(m_sizerStatBox
, 1, wxGROW
);
500 m_sizerStatic
->Layout();
503 // ----------------------------------------------------------------------------
505 // ----------------------------------------------------------------------------
507 void StaticWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
514 void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)
516 if (event
.GetEventObject() == wx_static_cast(wxObject
*, m_chkEllipsize
))
518 m_radioEllipsize
->Enable(event
.IsChecked());
524 void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent
& WXUNUSED(event
))
526 m_sizerStatBox
->GetStaticBox()->SetLabel(m_textBox
->GetValue());
529 void StaticWidgetsPage::OnButtonLabelWithMarkupText(wxCommandEvent
& WXUNUSED(event
))
531 m_statTextWithMarkup
->SetLabel(m_textLabelWithMarkup
->GetValue());
533 // test GetLabel() and GetLabelText(); the first should return the
534 // label as it is written in the relative text control; the second should
535 // return the label as it's shown in the wxStaticText
536 wxLogMessage(wxT("The original label should be '%s'"),
537 m_statTextWithMarkup
->GetLabel().c_str());
538 wxLogMessage(wxT("The label text is '%s'"),
539 m_statTextWithMarkup
->GetLabelText().c_str());
542 void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent
& WXUNUSED(event
))
544 m_statText
->SetLabel(m_textLabel
->GetValue());
546 // test GetLabel() and GetLabelText(); the first should return the
547 // label as it is written in the relative text control; the second should
548 // return the label as it's shown in the wxStaticText
549 wxLogMessage(wxT("The original label should be '%s'"),
550 m_statText
->GetLabel().c_str());
551 wxLogMessage(wxT("The label text is '%s'"),
552 m_statText
->GetLabelText().c_str());