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/button.h"
32 #include "wx/checkbox.h"
33 #include "wx/radiobox.h"
34 #include "wx/statbox.h"
35 #include "wx/stattext.h"
36 #include "wx/textctrl.h"
41 #include "wx/statline.h"
44 #include "icons/statbox.xpm"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
53 StaticPage_Reset
= 100,
58 // alignment radiobox values
75 // ----------------------------------------------------------------------------
76 // MyStaticText and MyStaticBox
77 // ----------------------------------------------------------------------------
79 // these 2 classes simply show that the static controls can get the mouse
80 // clicks too -- this used to be broken under MSW but works now
82 class MyStaticText
: public wxStaticText
85 MyStaticText(wxWindow
* parent
,
87 const wxString
& label
,
88 const wxPoint
& pos
= wxDefaultPosition
,
89 const wxSize
& size
= wxDefaultSize
,
91 : wxStaticText(parent
, id
, label
, pos
, size
, style
)
96 void OnMouseEvent(wxMouseEvent
& event
)
98 wxLogMessage(wxT("Clicked on static text"));
101 DECLARE_EVENT_TABLE()
104 class MyStaticBox
: public wxStaticBox
107 MyStaticBox(wxWindow
* parent
,
109 const wxString
& label
,
110 const wxPoint
& pos
= wxDefaultPosition
,
111 const wxSize
& size
= wxDefaultSize
,
113 : wxStaticBox(parent
, id
, label
, pos
, size
, style
)
118 void OnMouseEvent(wxMouseEvent
& event
)
120 wxLogMessage(wxT("Clicked on static box"));
123 DECLARE_EVENT_TABLE()
126 BEGIN_EVENT_TABLE(MyStaticText
, wxStaticText
)
127 EVT_LEFT_UP(MyStaticText::OnMouseEvent
)
130 BEGIN_EVENT_TABLE(MyStaticBox
, wxStaticBox
)
131 EVT_LEFT_UP(MyStaticBox::OnMouseEvent
)
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 class StaticWidgetsPage
: public WidgetsPage
141 StaticWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
142 virtual ~StaticWidgetsPage();
146 void OnCheckOrRadioBox(wxCommandEvent
& event
);
148 void OnButtonReset(wxCommandEvent
& event
);
149 void OnButtonBoxText(wxCommandEvent
& event
);
150 void OnButtonLabelText(wxCommandEvent
& event
);
152 // reset all parameters
155 // (re)create all controls
161 // the check/radio boxes for styles
162 wxCheckBox
*m_chkVert
,
165 wxRadioBox
*m_radioHAlign
,
168 // the controls and the sizer containing them
169 wxStaticBoxSizer
*m_sizerStatBox
;
170 wxStaticText
*m_statText
;
171 wxStaticLine
*m_statLine
;
172 wxSizer
*m_sizerStatic
;
174 // the text entries for command parameters
175 wxTextCtrl
*m_textBox
,
179 DECLARE_EVENT_TABLE()
180 DECLARE_WIDGETS_PAGE(StaticWidgetsPage
)
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
187 BEGIN_EVENT_TABLE(StaticWidgetsPage
, WidgetsPage
)
188 EVT_BUTTON(StaticPage_Reset
, StaticWidgetsPage::OnButtonReset
)
189 EVT_BUTTON(StaticPage_LabelText
, StaticWidgetsPage::OnButtonLabelText
)
190 EVT_BUTTON(StaticPage_BoxText
, StaticWidgetsPage::OnButtonBoxText
)
192 EVT_CHECKBOX(-1, StaticWidgetsPage::OnCheckOrRadioBox
)
193 EVT_RADIOBOX(-1, StaticWidgetsPage::OnCheckOrRadioBox
)
196 // ============================================================================
198 // ============================================================================
200 IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage
, _T("Static"));
202 StaticWidgetsPage::StaticWidgetsPage(wxNotebook
*notebook
,
203 wxImageList
*imaglist
)
204 : WidgetsPage(notebook
)
206 imaglist
->Add(wxBitmap(statbox_xpm
));
210 m_chkAutoResize
= (wxCheckBox
*)NULL
;
213 m_radioVAlign
= (wxRadioBox
*)NULL
;
215 m_statLine
= (wxStaticLine
*)NULL
;
216 m_statText
= (wxStaticText
*)NULL
;
218 m_sizerStatBox
= (wxStaticBoxSizer
*)NULL
;
219 m_sizerStatic
= (wxSizer
*)NULL
;
221 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
224 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set style"));
226 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
228 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical line"));
229 m_chkAutoResize
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Fit to text"));
230 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
232 static const wxString halign
[] =
239 static const wxString valign
[] =
246 m_radioHAlign
= new wxRadioBox(this, -1, _T("&Horz alignment"),
247 wxDefaultPosition
, wxDefaultSize
,
248 WXSIZEOF(halign
), halign
);
249 m_radioVAlign
= new wxRadioBox(this, -1, _T("&Vert alignment"),
250 wxDefaultPosition
, wxDefaultSize
,
251 WXSIZEOF(valign
), valign
);
253 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
254 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
256 wxButton
*btn
= new wxButton(this, StaticPage_Reset
, _T("&Reset"));
257 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
260 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Change labels"));
261 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
265 sizerRow
= CreateSizerWithTextAndButton(StaticPage_BoxText
,
266 _T("Change &box label"),
268 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
270 sizerRow
= CreateSizerWithTextAndButton(StaticPage_LabelText
,
271 _T("Change &text label"),
273 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
275 m_textBox
->SetValue(_T("This is a box"));
276 m_textLabel
->SetValue(_T("And this is a label\ninside the box"));
279 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
280 sizerRight
->SetMinSize(150, 0);
281 m_sizerStatic
= sizerRight
;
285 // the 3 panes panes compose the window
286 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
287 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
288 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
290 // final initializations
299 StaticWidgetsPage::~StaticWidgetsPage()
303 // ----------------------------------------------------------------------------
305 // ----------------------------------------------------------------------------
307 void StaticWidgetsPage::Reset()
309 m_chkVert
->SetValue(FALSE
);
310 m_chkAutoResize
->SetValue(TRUE
);
312 m_radioHAlign
->SetSelection(StaticHAlign_Left
);
313 m_radioVAlign
->SetSelection(StaticVAlign_Top
);
316 void StaticWidgetsPage::CreateStatic()
318 bool isVert
= m_chkVert
->GetValue();
320 if ( m_sizerStatBox
)
322 m_sizerStatic
->Remove(m_sizerStatBox
);
324 // delete m_sizerStatBox; -- deleted by Remove()
332 if ( !m_chkAutoResize
->GetValue() )
334 flagsText
|= wxST_NO_AUTORESIZE
;
338 switch ( m_radioHAlign
->GetSelection() )
341 wxFAIL_MSG(_T("unexpected radiobox selection"));
344 case StaticHAlign_Left
:
345 align
|= wxALIGN_LEFT
;
348 case StaticHAlign_Centre
:
349 align
|= wxALIGN_CENTRE_HORIZONTAL
;
352 case StaticHAlign_Right
:
353 align
|= wxALIGN_RIGHT
;
357 switch ( m_radioVAlign
->GetSelection() )
360 wxFAIL_MSG(_T("unexpected radiobox selection"));
363 case StaticVAlign_Top
:
364 align
|= wxALIGN_TOP
;
367 case StaticVAlign_Centre
:
368 align
|= wxALIGN_CENTRE_VERTICAL
;
371 case StaticVAlign_Bottom
:
372 align
|= wxALIGN_BOTTOM
;
379 wxStaticBox
*box
= new MyStaticBox(this, -1, m_textBox
->GetValue(),
380 wxDefaultPosition
, wxDefaultSize
,
382 m_sizerStatBox
= new wxStaticBoxSizer(box
, isVert
? wxHORIZONTAL
385 m_statText
= new MyStaticText(this, -1, m_textLabel
->GetValue(),
386 wxDefaultPosition
, wxDefaultSize
,
389 m_statLine
= new wxStaticLine(this, -1,
390 wxDefaultPosition
, wxDefaultSize
,
391 isVert
? wxLI_VERTICAL
: wxLI_HORIZONTAL
);
393 m_sizerStatBox
->Add(m_statText
, 1, wxGROW
| wxALL
, 5);
394 m_sizerStatBox
->Add(m_statLine
, 0, wxGROW
| wxALL
, 5);
395 m_sizerStatBox
->Add(0, 0, 1);
397 m_sizerStatic
->Add(m_sizerStatBox
, 1, wxGROW
);
399 m_sizerStatic
->Layout();
402 // ----------------------------------------------------------------------------
404 // ----------------------------------------------------------------------------
406 void StaticWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
413 void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)
418 void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent
& event
)
420 m_sizerStatBox
->GetStaticBox()->SetLabel(m_textBox
->GetValue());
423 void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent
& event
)
425 m_statText
->SetLabel(m_textLabel
->GetValue());