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"
43 #include "wx/generic/stattextg.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 void RecreateWidget() { CreateStatic(); }
99 // lazy creation of the content
100 virtual void CreateContent();
104 void OnCheckOrRadioBox(wxCommandEvent
& event
);
106 void OnButtonReset(wxCommandEvent
& event
);
107 void OnButtonBoxText(wxCommandEvent
& event
);
108 void OnButtonLabelText(wxCommandEvent
& event
);
109 void OnButtonLabelWithMarkupText(wxCommandEvent
& event
);
110 void OnMouseEvent(wxMouseEvent
& event
);
112 // reset all parameters
115 // (re)create all controls
121 // the check/radio boxes for styles
122 wxCheckBox
*m_chkVert
,
129 wxRadioBox
*m_radioHAlign
,
133 // the controls and the sizer containing them
134 wxStaticBoxSizer
*m_sizerStatBox
;
135 wxStaticTextBase
*m_statText
,
138 wxStaticLine
*m_statLine
;
139 #endif // wxUSE_STATLINE
140 wxSizer
*m_sizerStatic
;
142 // the text entries for command parameters
143 wxTextCtrl
*m_textBox
,
145 *m_textLabelWithMarkup
;
148 DECLARE_EVENT_TABLE()
149 DECLARE_WIDGETS_PAGE(StaticWidgetsPage
)
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
156 BEGIN_EVENT_TABLE(StaticWidgetsPage
, WidgetsPage
)
157 EVT_BUTTON(StaticPage_Reset
, StaticWidgetsPage::OnButtonReset
)
158 EVT_BUTTON(StaticPage_LabelText
, StaticWidgetsPage::OnButtonLabelText
)
159 EVT_BUTTON(StaticPage_LabelTextWithMarkup
, StaticWidgetsPage::OnButtonLabelWithMarkupText
)
160 EVT_BUTTON(StaticPage_BoxText
, StaticWidgetsPage::OnButtonBoxText
)
162 EVT_CHECKBOX(wxID_ANY
, StaticWidgetsPage::OnCheckOrRadioBox
)
163 EVT_RADIOBOX(wxID_ANY
, StaticWidgetsPage::OnCheckOrRadioBox
)
166 // ============================================================================
168 // ============================================================================
170 IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage
, _T("Static"),
171 (int)wxPlatform(GENERIC_CTRLS
).If(wxOS_WINDOWS
,NATIVE_CTRLS
)
174 StaticWidgetsPage::StaticWidgetsPage(WidgetsBookCtrl
*book
,
175 wxImageList
*imaglist
)
176 : WidgetsPage(book
, imaglist
, statbox_xpm
)
180 m_chkAutoResize
= (wxCheckBox
*)NULL
;
185 m_radioVAlign
= (wxRadioBox
*)NULL
;
188 m_statLine
= (wxStaticLine
*)NULL
;
189 #endif // wxUSE_STATLINE
190 m_statText
= m_statMarkup
= NULL
;
192 m_sizerStatBox
= (wxStaticBoxSizer
*)NULL
;
193 m_sizerStatic
= (wxSizer
*)NULL
;
195 m_textBox
= m_textLabel
= m_textLabelWithMarkup
= NULL
;
198 void StaticWidgetsPage::CreateContent()
200 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
203 wxSizer
*sizerLeft
= new wxStaticBoxSizer(wxVERTICAL
, this, "&Set style");
205 m_chkGeneric
= CreateCheckBoxAndAddToSizer(sizerLeft
,
206 "&Generic wxStaticText");
207 m_chkMarkup
= CreateCheckBoxAndAddToSizer(sizerLeft
, "Support &markup");
208 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, "&Vertical line");
209 m_chkAutoResize
= CreateCheckBoxAndAddToSizer(sizerLeft
, "&Fit to text");
210 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
212 static const wxString halign
[] =
219 static const wxString valign
[] =
226 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Horz alignment"),
227 wxDefaultPosition
, wxDefaultSize
,
228 WXSIZEOF(halign
), halign
, 3);
229 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Vert alignment"),
230 wxDefaultPosition
, wxDefaultSize
,
231 WXSIZEOF(valign
), valign
, 3);
233 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
234 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
237 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
239 m_chkEllipsize
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Ellipsize"));
241 static const wxString ellipsizeMode
[] =
248 m_radioEllipsize
= new wxRadioBox(this, wxID_ANY
, _T("&Ellipsize mode"),
249 wxDefaultPosition
, wxDefaultSize
,
250 WXSIZEOF(ellipsizeMode
), ellipsizeMode
,
253 sizerLeft
->Add(m_radioEllipsize
, 0, wxGROW
| wxALL
, 5);
255 wxButton
*btn
= new wxButton(this, StaticPage_Reset
, _T("&Reset"));
256 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
259 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(wxVERTICAL
, this,
262 m_textBox
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
);
263 wxButton
*b1
= new wxButton(this, wxID_ANY
, "Change &box label");
264 b1
->Connect(wxEVT_COMMAND_BUTTON_CLICKED
,
265 wxCommandEventHandler(StaticWidgetsPage::OnButtonBoxText
),
267 sizerMiddle
->Add(m_textBox
, 0, wxEXPAND
|wxALL
, 5);
268 sizerMiddle
->Add(b1
, 0, wxLEFT
|wxBOTTOM
, 5);
270 m_textLabel
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
271 wxDefaultPosition
, wxDefaultSize
,
272 wxTE_MULTILINE
|wxHSCROLL
);
273 wxButton
*b2
= new wxButton(this, wxID_ANY
, "Change &text label");
274 b2
->Connect(wxEVT_COMMAND_BUTTON_CLICKED
,
275 wxCommandEventHandler(StaticWidgetsPage::OnButtonLabelText
),
277 sizerMiddle
->Add(m_textLabel
, 0, wxEXPAND
|wxALL
, 5);
278 sizerMiddle
->Add(b2
, 0, wxLEFT
|wxBOTTOM
, 5);
280 m_textLabelWithMarkup
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
281 wxDefaultPosition
, wxDefaultSize
,
282 wxTE_MULTILINE
|wxHSCROLL
);
284 wxButton
*b3
= new wxButton(this, wxID_ANY
, "Change decorated text label");
285 b3
->Connect(wxEVT_COMMAND_BUTTON_CLICKED
,
286 wxCommandEventHandler(StaticWidgetsPage::OnButtonLabelWithMarkupText
),
288 sizerMiddle
->Add(m_textLabelWithMarkup
, 0, wxEXPAND
|wxALL
, 5);
289 sizerMiddle
->Add(b3
, 0, wxLEFT
|wxBOTTOM
, 5);
291 m_chkGreen
= CreateCheckBoxAndAddToSizer(sizerLeft
,
292 "Decorated label on g&reen");
293 sizerMiddle
->Add(m_chkGreen
, 0, wxALL
, 5);
295 // final initializations
296 // NB: must be done _before_ calling CreateStatic()
299 m_textBox
->SetValue(_T("This is a box"));
300 m_textLabel
->SetValue(_T("And this is a\n\tlabel inside the box with a &mnemonic.\n")
301 _T("Only this text is affected by the ellipsize settings."));
302 m_textLabelWithMarkup
->SetValue(_T("Another label, this time <b>decorated</b> ")
303 _T("with <u>markup</u>; here you need entities ")
304 _T("for the symbols: < > & ' " ")
305 _T(" but you can still place &mnemonics..."));
308 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
309 sizerRight
->SetMinSize(150, 0);
310 m_sizerStatic
= sizerRight
;
314 // the 3 panes panes compose the window
315 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
316 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
317 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
322 // ----------------------------------------------------------------------------
324 // ----------------------------------------------------------------------------
326 void StaticWidgetsPage::Reset()
328 m_chkGeneric
->SetValue(false);
329 m_chkVert
->SetValue(false);
330 m_chkAutoResize
->SetValue(true);
331 m_chkEllipsize
->SetValue(true);
332 m_chkMarkup
->SetValue(true);
334 m_radioHAlign
->SetSelection(StaticHAlign_Left
);
335 m_radioVAlign
->SetSelection(StaticVAlign_Top
);
338 void StaticWidgetsPage::CreateStatic()
340 bool isVert
= m_chkVert
->GetValue();
342 if ( m_sizerStatBox
)
344 // delete m_sizerStatBox; -- deleted by Remove()
345 m_sizerStatic
->Remove(m_sizerStatBox
);
350 #endif // wxUSE_STATLINE
354 flagsText
= ms_defaultFlags
,
355 flagsDummyText
= ms_defaultFlags
;
357 if ( !m_chkAutoResize
->GetValue() )
359 flagsText
|= wxST_NO_AUTORESIZE
;
360 flagsDummyText
|= wxST_NO_AUTORESIZE
;
363 if ( m_chkMarkup
->GetValue() )
365 flagsText
|= wxST_MARKUP
;
366 flagsDummyText
|= wxST_MARKUP
;
370 switch ( m_radioHAlign
->GetSelection() )
373 wxFAIL_MSG(_T("unexpected radiobox selection"));
376 case StaticHAlign_Left
:
377 align
|= wxALIGN_LEFT
;
380 case StaticHAlign_Centre
:
381 align
|= wxALIGN_CENTRE_HORIZONTAL
;
384 case StaticHAlign_Right
:
385 align
|= wxALIGN_RIGHT
;
389 switch ( m_radioVAlign
->GetSelection() )
392 wxFAIL_MSG(_T("unexpected radiobox selection"));
395 case StaticVAlign_Top
:
396 align
|= wxALIGN_TOP
;
399 case StaticVAlign_Centre
:
400 align
|= wxALIGN_CENTRE_VERTICAL
;
403 case StaticVAlign_Bottom
:
404 align
|= wxALIGN_BOTTOM
;
408 if ( m_chkEllipsize
->GetValue() )
410 switch ( m_radioEllipsize
->GetSelection() )
413 wxFAIL_MSG(_T("unexpected radiobox selection"));
416 case StaticEllipsize_Start
:
417 flagsDummyText
|= wxST_ELLIPSIZE_START
;
420 case StaticEllipsize_Middle
:
421 flagsDummyText
|= wxST_ELLIPSIZE_MIDDLE
;
424 case StaticEllipsize_End
:
425 flagsDummyText
|= wxST_ELLIPSIZE_END
;
430 flagsDummyText
|= align
;
434 wxStaticBox
*staticBox
= new wxStaticBox(this, wxID_ANY
,
435 m_textBox
->GetValue(),
436 wxDefaultPosition
, wxDefaultSize
,
438 m_sizerStatBox
= new wxStaticBoxSizer(staticBox
, isVert
? wxHORIZONTAL
441 if ( m_chkGeneric
->GetValue() )
443 m_statText
= new wxGenericStaticText(this, wxID_ANY
,
444 m_textLabel
->GetValue(),
445 wxDefaultPosition
, wxDefaultSize
,
447 m_statMarkup
= new wxGenericStaticText(this, wxID_ANY
,
448 m_textLabelWithMarkup
->GetValue(),
449 wxDefaultPosition
, wxDefaultSize
,
452 else // use native versions
454 m_statText
= new wxStaticText(this, wxID_ANY
,
455 m_textLabel
->GetValue(),
456 wxDefaultPosition
, wxDefaultSize
,
458 m_statMarkup
= new wxStaticText(this, wxID_ANY
,
459 m_textLabelWithMarkup
->GetValue(),
460 wxDefaultPosition
, wxDefaultSize
,
463 if ( m_chkGreen
->GetValue() )
464 m_statMarkup
->SetBackgroundColour(*wxGREEN
);
466 m_statLine
= new wxStaticLine(this, wxID_ANY
,
467 wxDefaultPosition
, wxDefaultSize
,
468 isVert
? wxLI_VERTICAL
: wxLI_HORIZONTAL
);
469 #endif // wxUSE_STATLINE
471 m_sizerStatBox
->Add(m_statText
, 1, wxGROW
| wxALL
, 5);
473 m_sizerStatBox
->Add(m_statLine
, 0, wxGROW
| wxALL
, 5);
474 #endif // wxUSE_STATLINE
475 m_sizerStatBox
->Add(m_statMarkup
, 1, wxGROW
| wxALL
, 5);
477 m_sizerStatic
->Add(m_sizerStatBox
, 1, wxGROW
);
479 m_sizerStatic
->Layout();
481 m_statText
->Connect(wxEVT_LEFT_UP
,
482 wxMouseEventHandler(StaticWidgetsPage::OnMouseEvent
),
484 staticBox
->Connect(wxEVT_LEFT_UP
,
485 wxMouseEventHandler(StaticWidgetsPage::OnMouseEvent
),
489 // ----------------------------------------------------------------------------
491 // ----------------------------------------------------------------------------
493 void StaticWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
500 void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)
502 if (event
.GetEventObject() == static_cast<wxObject
*>(m_chkEllipsize
))
504 m_radioEllipsize
->Enable(event
.IsChecked());
510 void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent
& WXUNUSED(event
))
512 m_sizerStatBox
->GetStaticBox()->SetLabel(m_textBox
->GetValue());
515 void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent
& WXUNUSED(event
))
517 m_statText
->SetLabel(m_textLabel
->GetValue());
519 // test GetLabel() and GetLabelText(); the first should return the
520 // label as it is written in the relative text control; the second should
521 // return the label as it's shown in the wxStaticText
522 wxLogMessage(wxT("The original label should be '%s'"),
523 m_statText
->GetLabel());
524 wxLogMessage(wxT("The label text is '%s'"),
525 m_statText
->GetLabelText());
528 void StaticWidgetsPage::OnButtonLabelWithMarkupText(wxCommandEvent
& WXUNUSED(event
))
530 m_statMarkup
->SetLabel(m_textLabelWithMarkup
->GetValue());
532 // test GetLabel() and GetLabelText(); the first should return the
533 // label as it is written in the relative text control; the second should
534 // return the label as it's shown in the wxStaticText
535 wxLogMessage(wxT("The original label should be '%s'"),
536 m_statMarkup
->GetLabel());
537 wxLogMessage(wxT("The label text is '%s'"),
538 m_statMarkup
->GetLabelText());
541 void StaticWidgetsPage::OnMouseEvent(wxMouseEvent
& event
)
543 if ( event
.GetEventObject() == m_statText
)
544 wxLogMessage("Clicked on static text");
546 wxLogMessage("Clicked on static box");