1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWindows 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
= 100,
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(wxNotebook
*notebook
, wxImageList
*imaglist
);
143 virtual ~StaticWidgetsPage();
147 void OnCheckOrRadioBox(wxCommandEvent
& event
);
149 void OnButtonReset(wxCommandEvent
& event
);
150 void OnButtonBoxText(wxCommandEvent
& event
);
151 void OnButtonLabelText(wxCommandEvent
& event
);
153 // reset all parameters
156 // (re)create all controls
162 // the check/radio boxes for styles
163 wxCheckBox
*m_chkVert
,
166 wxRadioBox
*m_radioHAlign
,
169 // the controls and the sizer containing them
170 wxStaticBox
*m_staticBox
;
171 wxStaticBoxSizer
*m_sizerStatBox
;
172 wxStaticText
*m_statText
;
173 wxStaticLine
*m_statLine
;
174 wxSizer
*m_sizerStatic
;
176 // the text entries for command parameters
177 wxTextCtrl
*m_textBox
,
181 DECLARE_EVENT_TABLE()
182 DECLARE_WIDGETS_PAGE(StaticWidgetsPage
)
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 BEGIN_EVENT_TABLE(StaticWidgetsPage
, WidgetsPage
)
190 EVT_BUTTON(StaticPage_Reset
, StaticWidgetsPage::OnButtonReset
)
191 EVT_BUTTON(StaticPage_LabelText
, StaticWidgetsPage::OnButtonLabelText
)
192 EVT_BUTTON(StaticPage_BoxText
, StaticWidgetsPage::OnButtonBoxText
)
194 EVT_CHECKBOX(-1, StaticWidgetsPage::OnCheckOrRadioBox
)
195 EVT_RADIOBOX(-1, StaticWidgetsPage::OnCheckOrRadioBox
)
198 // ============================================================================
200 // ============================================================================
202 IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage
, _T("Static"));
204 StaticWidgetsPage::StaticWidgetsPage(wxNotebook
*notebook
,
205 wxImageList
*imaglist
)
206 : WidgetsPage(notebook
)
208 imaglist
->Add(wxBitmap(statbox_xpm
));
212 m_chkAutoResize
= (wxCheckBox
*)NULL
;
215 m_radioVAlign
= (wxRadioBox
*)NULL
;
217 m_statLine
= (wxStaticLine
*)NULL
;
218 m_statText
= (wxStaticText
*)NULL
;
220 m_staticBox
= (wxStaticBox
*)NULL
;
221 m_sizerStatBox
= (wxStaticBoxSizer
*)NULL
;
222 m_sizerStatic
= (wxSizer
*)NULL
;
224 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
227 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set style"));
229 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
231 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical line"));
232 m_chkAutoResize
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Fit to text"));
233 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
235 static const wxString halign
[] =
242 static const wxString valign
[] =
249 m_radioHAlign
= new wxRadioBox(this, -1, _T("&Horz alignment"),
250 wxDefaultPosition
, wxDefaultSize
,
251 WXSIZEOF(halign
), halign
);
252 m_radioVAlign
= new wxRadioBox(this, -1, _T("&Vert alignment"),
253 wxDefaultPosition
, wxDefaultSize
,
254 WXSIZEOF(valign
), valign
);
256 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
257 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
259 wxButton
*btn
= new wxButton(this, StaticPage_Reset
, _T("&Reset"));
260 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
263 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Change labels"));
264 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
268 sizerRow
= CreateSizerWithTextAndButton(StaticPage_BoxText
,
269 _T("Change &box label"),
271 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
273 sizerRow
= CreateSizerWithTextAndButton(StaticPage_LabelText
,
274 _T("Change &text label"),
276 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
278 m_textBox
->SetValue(_T("This is a box"));
279 m_textLabel
->SetValue(_T("And this is a label\ninside the box"));
282 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
283 sizerRight
->SetMinSize(150, 0);
284 m_sizerStatic
= sizerRight
;
288 // the 3 panes panes compose the window
289 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
290 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
291 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
293 // final initializations
302 StaticWidgetsPage::~StaticWidgetsPage()
306 // ----------------------------------------------------------------------------
308 // ----------------------------------------------------------------------------
310 void StaticWidgetsPage::Reset()
312 m_chkVert
->SetValue(FALSE
);
313 m_chkAutoResize
->SetValue(TRUE
);
315 m_radioHAlign
->SetSelection(StaticHAlign_Left
);
316 m_radioVAlign
->SetSelection(StaticVAlign_Top
);
319 void StaticWidgetsPage::CreateStatic()
321 bool isVert
= m_chkVert
->GetValue();
323 if ( m_sizerStatBox
)
326 // delete m_sizerStatBox; -- deleted by Remove()
327 m_sizerStatic
->Remove(m_sizerStatBox
);
335 if ( !m_chkAutoResize
->GetValue() )
337 flagsText
|= wxST_NO_AUTORESIZE
;
341 switch ( m_radioHAlign
->GetSelection() )
344 wxFAIL_MSG(_T("unexpected radiobox selection"));
347 case StaticHAlign_Left
:
348 align
|= wxALIGN_LEFT
;
351 case StaticHAlign_Centre
:
352 align
|= wxALIGN_CENTRE_HORIZONTAL
;
355 case StaticHAlign_Right
:
356 align
|= wxALIGN_RIGHT
;
360 switch ( m_radioVAlign
->GetSelection() )
363 wxFAIL_MSG(_T("unexpected radiobox selection"));
366 case StaticVAlign_Top
:
367 align
|= wxALIGN_TOP
;
370 case StaticVAlign_Centre
:
371 align
|= wxALIGN_CENTRE_VERTICAL
;
374 case StaticVAlign_Bottom
:
375 align
|= wxALIGN_BOTTOM
;
382 m_staticBox
= new MyStaticBox(this, -1, m_textBox
->GetValue(),
383 wxDefaultPosition
, wxDefaultSize
,
385 m_sizerStatBox
= new wxStaticBoxSizer(m_staticBox
, isVert
? wxHORIZONTAL
388 m_statText
= new MyStaticText(this, -1, m_textLabel
->GetValue(),
389 wxDefaultPosition
, wxDefaultSize
,
392 m_statLine
= new wxStaticLine(this, -1,
393 wxDefaultPosition
, wxDefaultSize
,
394 isVert
? wxLI_VERTICAL
: wxLI_HORIZONTAL
);
396 m_sizerStatBox
->Add(m_statText
, 1, wxGROW
| wxALL
, 5);
397 m_sizerStatBox
->Add(m_statLine
, 0, wxGROW
| wxALL
, 5);
398 m_sizerStatBox
->Add(0, 0, 1);
400 m_sizerStatic
->Add(m_sizerStatBox
, 1, wxGROW
);
402 m_sizerStatic
->Layout();
405 // ----------------------------------------------------------------------------
407 // ----------------------------------------------------------------------------
409 void StaticWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
416 void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
421 void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent
& WXUNUSED(event
))
423 m_sizerStatBox
->GetStaticBox()->SetLabel(m_textBox
->GetValue());
426 void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent
& WXUNUSED(event
))
428 m_statText
->SetLabel(m_textLabel
->GetValue());