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
,
59 // alignment radiobox values
76 // ----------------------------------------------------------------------------
77 // MyStaticText and MyStaticBox
78 // ----------------------------------------------------------------------------
80 // these 2 classes simply show that the static controls can get the mouse
81 // clicks too -- this used to be broken under MSW but works now
83 class MyStaticText
: public wxStaticText
86 MyStaticText(wxWindow
* parent
,
88 const wxString
& label
,
89 const wxPoint
& pos
= wxDefaultPosition
,
90 const wxSize
& size
= wxDefaultSize
,
92 : wxStaticText(parent
, id
, label
, pos
, size
, style
)
97 void OnMouseEvent(wxMouseEvent
& WXUNUSED(event
))
99 wxLogMessage(wxT("Clicked on static text"));
102 DECLARE_EVENT_TABLE()
105 class MyStaticBox
: public wxStaticBox
108 MyStaticBox(wxWindow
* parent
,
110 const wxString
& label
,
111 const wxPoint
& pos
= wxDefaultPosition
,
112 const wxSize
& size
= wxDefaultSize
,
114 : wxStaticBox(parent
, id
, label
, pos
, size
, style
)
119 void OnMouseEvent(wxMouseEvent
& WXUNUSED(event
))
121 wxLogMessage(wxT("Clicked on static box"));
124 DECLARE_EVENT_TABLE()
127 BEGIN_EVENT_TABLE(MyStaticText
, wxStaticText
)
128 EVT_LEFT_UP(MyStaticText::OnMouseEvent
)
131 BEGIN_EVENT_TABLE(MyStaticBox
, wxStaticBox
)
132 EVT_LEFT_UP(MyStaticBox::OnMouseEvent
)
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 class StaticWidgetsPage
: public WidgetsPage
142 StaticWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
143 virtual ~StaticWidgetsPage(){};
145 virtual wxControl
*GetWidget() const { return m_statText
; }
146 virtual void RecreateWidget() { CreateStatic(); }
150 void OnCheckOrRadioBox(wxCommandEvent
& event
);
152 void OnButtonReset(wxCommandEvent
& event
);
153 void OnButtonBoxText(wxCommandEvent
& event
);
154 void OnButtonLabelText(wxCommandEvent
& event
);
156 // reset all parameters
159 // (re)create all controls
165 // the check/radio boxes for styles
166 wxCheckBox
*m_chkVert
,
169 wxRadioBox
*m_radioHAlign
,
172 // the controls and the sizer containing them
173 wxStaticBox
*m_staticBox
;
174 wxStaticBoxSizer
*m_sizerStatBox
;
175 wxStaticText
*m_statText
;
177 wxStaticLine
*m_statLine
;
178 #endif // wxUSE_STATLINE
179 wxSizer
*m_sizerStatic
;
181 // the text entries for command parameters
182 wxTextCtrl
*m_textBox
,
186 DECLARE_EVENT_TABLE()
187 DECLARE_WIDGETS_PAGE(StaticWidgetsPage
)
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 BEGIN_EVENT_TABLE(StaticWidgetsPage
, WidgetsPage
)
195 EVT_BUTTON(StaticPage_Reset
, StaticWidgetsPage::OnButtonReset
)
196 EVT_BUTTON(StaticPage_LabelText
, StaticWidgetsPage::OnButtonLabelText
)
197 EVT_BUTTON(StaticPage_BoxText
, StaticWidgetsPage::OnButtonBoxText
)
199 EVT_CHECKBOX(wxID_ANY
, StaticWidgetsPage::OnCheckOrRadioBox
)
200 EVT_RADIOBOX(wxID_ANY
, StaticWidgetsPage::OnCheckOrRadioBox
)
203 // ============================================================================
205 // ============================================================================
207 IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage
, _T("Static"),
208 (int)wxPlatform(GENERIC_CTRLS
).If(wxMSW
,NATIVE_CTRLS
)
211 StaticWidgetsPage::StaticWidgetsPage(WidgetsBookCtrl
*book
,
212 wxImageList
*imaglist
)
213 : WidgetsPage(book
, imaglist
, statbox_xpm
)
217 m_chkAutoResize
= (wxCheckBox
*)NULL
;
220 m_radioVAlign
= (wxRadioBox
*)NULL
;
223 m_statLine
= (wxStaticLine
*)NULL
;
224 #endif // wxUSE_STATLINE
225 m_statText
= (wxStaticText
*)NULL
;
227 m_staticBox
= (wxStaticBox
*)NULL
;
228 m_sizerStatBox
= (wxStaticBoxSizer
*)NULL
;
229 m_sizerStatic
= (wxSizer
*)NULL
;
231 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
234 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
236 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
238 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical line"));
239 m_chkAutoResize
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Fit to text"));
240 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
242 static const wxString halign
[] =
249 static const wxString valign
[] =
256 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Horz alignment"),
257 wxDefaultPosition
, wxDefaultSize
,
258 WXSIZEOF(halign
), halign
);
259 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Vert alignment"),
260 wxDefaultPosition
, wxDefaultSize
,
261 WXSIZEOF(valign
), valign
);
263 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
264 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
266 wxButton
*btn
= new wxButton(this, StaticPage_Reset
, _T("&Reset"));
267 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
270 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change labels"));
271 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
275 sizerRow
= CreateSizerWithTextAndButton(StaticPage_BoxText
,
276 _T("Change &box label"),
277 wxID_ANY
, &m_textBox
);
278 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
280 sizerRow
= CreateSizerWithTextAndButton(StaticPage_LabelText
,
281 _T("Change &text label"),
282 wxID_ANY
, &m_textLabel
);
283 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
285 m_textBox
->SetValue(_T("This is a box"));
286 m_textLabel
->SetValue(_T("And this is a label\ninside the box"));
289 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
290 sizerRight
->SetMinSize(150, 0);
291 m_sizerStatic
= sizerRight
;
295 // the 3 panes panes compose the window
296 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
297 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
298 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
300 // final initializations
308 // ----------------------------------------------------------------------------
310 // ----------------------------------------------------------------------------
312 void StaticWidgetsPage::Reset()
314 m_chkVert
->SetValue(false);
315 m_chkAutoResize
->SetValue(true);
317 m_radioHAlign
->SetSelection(StaticHAlign_Left
);
318 m_radioVAlign
->SetSelection(StaticVAlign_Top
);
321 void StaticWidgetsPage::CreateStatic()
323 bool isVert
= m_chkVert
->GetValue();
325 if ( m_sizerStatBox
)
328 // delete m_sizerStatBox; -- deleted by Remove()
329 m_sizerStatic
->Remove(m_sizerStatBox
);
333 #endif // wxUSE_STATLINE
337 flagsText
= ms_defaultFlags
;
339 if ( !m_chkAutoResize
->GetValue() )
341 flagsText
|= wxST_NO_AUTORESIZE
;
345 switch ( m_radioHAlign
->GetSelection() )
348 wxFAIL_MSG(_T("unexpected radiobox selection"));
351 case StaticHAlign_Left
:
352 align
|= wxALIGN_LEFT
;
355 case StaticHAlign_Centre
:
356 align
|= wxALIGN_CENTRE_HORIZONTAL
;
359 case StaticHAlign_Right
:
360 align
|= wxALIGN_RIGHT
;
364 switch ( m_radioVAlign
->GetSelection() )
367 wxFAIL_MSG(_T("unexpected radiobox selection"));
370 case StaticVAlign_Top
:
371 align
|= wxALIGN_TOP
;
374 case StaticVAlign_Centre
:
375 align
|= wxALIGN_CENTRE_VERTICAL
;
378 case StaticVAlign_Bottom
:
379 align
|= wxALIGN_BOTTOM
;
386 m_staticBox
= new MyStaticBox(this, wxID_ANY
, m_textBox
->GetValue(),
387 wxDefaultPosition
, wxDefaultSize
,
389 m_sizerStatBox
= new wxStaticBoxSizer(m_staticBox
, isVert
? wxHORIZONTAL
392 m_statText
= new MyStaticText(this, wxID_ANY
, m_textLabel
->GetValue(),
393 wxDefaultPosition
, wxDefaultSize
,
397 m_statLine
= new wxStaticLine(this, wxID_ANY
,
398 wxDefaultPosition
, wxDefaultSize
,
399 isVert
? wxLI_VERTICAL
: wxLI_HORIZONTAL
);
400 #endif // wxUSE_STATLINE
402 m_sizerStatBox
->Add(m_statText
, 1, wxGROW
| wxALL
, 5);
404 m_sizerStatBox
->Add(m_statLine
, 0, wxGROW
| wxALL
, 5);
405 #endif // wxUSE_STATLINE
406 m_sizerStatBox
->Add(0, 0, 1);
408 m_sizerStatic
->Add(m_sizerStatBox
, 1, wxGROW
);
410 m_sizerStatic
->Layout();
413 // ----------------------------------------------------------------------------
415 // ----------------------------------------------------------------------------
417 void StaticWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
424 void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
429 void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent
& WXUNUSED(event
))
431 m_sizerStatBox
->GetStaticBox()->SetLabel(m_textBox
->GetValue());
434 void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent
& WXUNUSED(event
))
436 m_statText
->SetLabel(m_textLabel
->GetValue());