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(); }
148 // lazy creation of the content
149 virtual void CreateContent();
153 void OnCheckOrRadioBox(wxCommandEvent
& event
);
155 void OnButtonReset(wxCommandEvent
& event
);
156 void OnButtonBoxText(wxCommandEvent
& event
);
157 void OnButtonLabelText(wxCommandEvent
& event
);
159 // reset all parameters
162 // (re)create all controls
168 // the check/radio boxes for styles
169 wxCheckBox
*m_chkVert
,
172 wxRadioBox
*m_radioHAlign
,
175 // the controls and the sizer containing them
176 wxStaticBox
*m_staticBox
;
177 wxStaticBoxSizer
*m_sizerStatBox
;
178 wxStaticText
*m_statText
;
180 wxStaticLine
*m_statLine
;
181 #endif // wxUSE_STATLINE
182 wxSizer
*m_sizerStatic
;
184 // the text entries for command parameters
185 wxTextCtrl
*m_textBox
,
189 DECLARE_EVENT_TABLE()
190 DECLARE_WIDGETS_PAGE(StaticWidgetsPage
)
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
197 BEGIN_EVENT_TABLE(StaticWidgetsPage
, WidgetsPage
)
198 EVT_BUTTON(StaticPage_Reset
, StaticWidgetsPage::OnButtonReset
)
199 EVT_BUTTON(StaticPage_LabelText
, StaticWidgetsPage::OnButtonLabelText
)
200 EVT_BUTTON(StaticPage_BoxText
, StaticWidgetsPage::OnButtonBoxText
)
202 EVT_CHECKBOX(wxID_ANY
, StaticWidgetsPage::OnCheckOrRadioBox
)
203 EVT_RADIOBOX(wxID_ANY
, StaticWidgetsPage::OnCheckOrRadioBox
)
206 // ============================================================================
208 // ============================================================================
210 IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage
, _T("Static"),
211 (int)wxPlatform(GENERIC_CTRLS
).If(wxOS_WINDOWS
,NATIVE_CTRLS
)
214 StaticWidgetsPage::StaticWidgetsPage(WidgetsBookCtrl
*book
,
215 wxImageList
*imaglist
)
216 : WidgetsPage(book
, imaglist
, statbox_xpm
)
220 m_chkAutoResize
= (wxCheckBox
*)NULL
;
223 m_radioVAlign
= (wxRadioBox
*)NULL
;
226 m_statLine
= (wxStaticLine
*)NULL
;
227 #endif // wxUSE_STATLINE
228 m_statText
= (wxStaticText
*)NULL
;
230 m_staticBox
= (wxStaticBox
*)NULL
;
231 m_sizerStatBox
= (wxStaticBoxSizer
*)NULL
;
232 m_sizerStatic
= (wxSizer
*)NULL
;
235 void StaticWidgetsPage::CreateContent()
237 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
240 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
242 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
244 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical line"));
245 m_chkAutoResize
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Fit to text"));
246 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
248 static const wxString halign
[] =
255 static const wxString valign
[] =
262 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Horz alignment"),
263 wxDefaultPosition
, wxDefaultSize
,
264 WXSIZEOF(halign
), halign
);
265 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Vert alignment"),
266 wxDefaultPosition
, wxDefaultSize
,
267 WXSIZEOF(valign
), valign
);
269 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
270 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
272 wxButton
*btn
= new wxButton(this, StaticPage_Reset
, _T("&Reset"));
273 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
276 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change labels"));
277 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
281 sizerRow
= CreateSizerWithTextAndButton(StaticPage_BoxText
,
282 _T("Change &box label"),
283 wxID_ANY
, &m_textBox
);
284 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
286 sizerRow
= CreateSizerWithTextAndButton(StaticPage_LabelText
,
287 _T("Change &text label"),
288 wxID_ANY
, &m_textLabel
);
289 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
291 m_textBox
->SetValue(_T("This is a box"));
292 m_textLabel
->SetValue(_T("And this is a label\ninside the box"));
295 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
296 sizerRight
->SetMinSize(150, 0);
297 m_sizerStatic
= sizerRight
;
301 // the 3 panes panes compose the window
302 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
303 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
304 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
306 // final initializations
314 // ----------------------------------------------------------------------------
316 // ----------------------------------------------------------------------------
318 void StaticWidgetsPage::Reset()
320 m_chkVert
->SetValue(false);
321 m_chkAutoResize
->SetValue(true);
323 m_radioHAlign
->SetSelection(StaticHAlign_Left
);
324 m_radioVAlign
->SetSelection(StaticVAlign_Top
);
327 void StaticWidgetsPage::CreateStatic()
329 bool isVert
= m_chkVert
->GetValue();
331 if ( m_sizerStatBox
)
334 // delete m_sizerStatBox; -- deleted by Remove()
335 m_sizerStatic
->Remove(m_sizerStatBox
);
339 #endif // wxUSE_STATLINE
343 flagsText
= ms_defaultFlags
;
345 if ( !m_chkAutoResize
->GetValue() )
347 flagsText
|= wxST_NO_AUTORESIZE
;
351 switch ( m_radioHAlign
->GetSelection() )
354 wxFAIL_MSG(_T("unexpected radiobox selection"));
357 case StaticHAlign_Left
:
358 align
|= wxALIGN_LEFT
;
361 case StaticHAlign_Centre
:
362 align
|= wxALIGN_CENTRE_HORIZONTAL
;
365 case StaticHAlign_Right
:
366 align
|= wxALIGN_RIGHT
;
370 switch ( m_radioVAlign
->GetSelection() )
373 wxFAIL_MSG(_T("unexpected radiobox selection"));
376 case StaticVAlign_Top
:
377 align
|= wxALIGN_TOP
;
380 case StaticVAlign_Centre
:
381 align
|= wxALIGN_CENTRE_VERTICAL
;
384 case StaticVAlign_Bottom
:
385 align
|= wxALIGN_BOTTOM
;
392 m_staticBox
= new MyStaticBox(this, wxID_ANY
, m_textBox
->GetValue(),
393 wxDefaultPosition
, wxDefaultSize
,
395 m_sizerStatBox
= new wxStaticBoxSizer(m_staticBox
, isVert
? wxHORIZONTAL
398 m_statText
= new MyStaticText(this, wxID_ANY
, m_textLabel
->GetValue(),
399 wxDefaultPosition
, wxDefaultSize
,
403 m_statLine
= new wxStaticLine(this, wxID_ANY
,
404 wxDefaultPosition
, wxDefaultSize
,
405 isVert
? wxLI_VERTICAL
: wxLI_HORIZONTAL
);
406 #endif // wxUSE_STATLINE
408 m_sizerStatBox
->Add(m_statText
, 1, wxGROW
| wxALL
, 5);
410 m_sizerStatBox
->Add(m_statLine
, 0, wxGROW
| wxALL
, 5);
411 #endif // wxUSE_STATLINE
412 m_sizerStatBox
->Add(0, 0, 1);
414 m_sizerStatic
->Add(m_sizerStatBox
, 1, wxGROW
);
416 m_sizerStatic
->Layout();
419 // ----------------------------------------------------------------------------
421 // ----------------------------------------------------------------------------
423 void StaticWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
430 void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
435 void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent
& WXUNUSED(event
))
437 m_sizerStatBox
->GetStaticBox()->SetLabel(m_textBox
->GetValue());
440 void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent
& WXUNUSED(event
))
442 m_statText
->SetLabel(m_textLabel
->GetValue());