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
= 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
;
174 wxStaticLine
*m_statLine
;
175 #endif // wxUSE_STATLINE
176 wxSizer
*m_sizerStatic
;
178 // the text entries for command parameters
179 wxTextCtrl
*m_textBox
,
183 DECLARE_EVENT_TABLE()
184 DECLARE_WIDGETS_PAGE(StaticWidgetsPage
)
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
191 BEGIN_EVENT_TABLE(StaticWidgetsPage
, WidgetsPage
)
192 EVT_BUTTON(StaticPage_Reset
, StaticWidgetsPage::OnButtonReset
)
193 EVT_BUTTON(StaticPage_LabelText
, StaticWidgetsPage::OnButtonLabelText
)
194 EVT_BUTTON(StaticPage_BoxText
, StaticWidgetsPage::OnButtonBoxText
)
196 EVT_CHECKBOX(wxID_ANY
, StaticWidgetsPage::OnCheckOrRadioBox
)
197 EVT_RADIOBOX(wxID_ANY
, StaticWidgetsPage::OnCheckOrRadioBox
)
200 // ============================================================================
202 // ============================================================================
204 IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage
, _T("Static"));
206 StaticWidgetsPage::StaticWidgetsPage(wxNotebook
*notebook
,
207 wxImageList
*imaglist
)
208 : WidgetsPage(notebook
)
210 imaglist
->Add(wxBitmap(statbox_xpm
));
214 m_chkAutoResize
= (wxCheckBox
*)NULL
;
217 m_radioVAlign
= (wxRadioBox
*)NULL
;
220 m_statLine
= (wxStaticLine
*)NULL
;
221 #endif // wxUSE_STATLINE
222 m_statText
= (wxStaticText
*)NULL
;
224 m_staticBox
= (wxStaticBox
*)NULL
;
225 m_sizerStatBox
= (wxStaticBoxSizer
*)NULL
;
226 m_sizerStatic
= (wxSizer
*)NULL
;
228 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
231 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
233 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
235 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical line"));
236 m_chkAutoResize
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Fit to text"));
237 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
239 static const wxString halign
[] =
246 static const wxString valign
[] =
253 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Horz alignment"),
254 wxDefaultPosition
, wxDefaultSize
,
255 WXSIZEOF(halign
), halign
);
256 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Vert alignment"),
257 wxDefaultPosition
, wxDefaultSize
,
258 WXSIZEOF(valign
), valign
);
260 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
261 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
263 wxButton
*btn
= new wxButton(this, StaticPage_Reset
, _T("&Reset"));
264 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
267 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change labels"));
268 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
272 sizerRow
= CreateSizerWithTextAndButton(StaticPage_BoxText
,
273 _T("Change &box label"),
274 wxID_ANY
, &m_textBox
);
275 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
277 sizerRow
= CreateSizerWithTextAndButton(StaticPage_LabelText
,
278 _T("Change &text label"),
279 wxID_ANY
, &m_textLabel
);
280 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
282 m_textBox
->SetValue(_T("This is a box"));
283 m_textLabel
->SetValue(_T("And this is a label\ninside the box"));
286 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
287 sizerRight
->SetMinSize(150, 0);
288 m_sizerStatic
= sizerRight
;
292 // the 3 panes panes compose the window
293 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
294 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
295 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
297 // final initializations
305 StaticWidgetsPage::~StaticWidgetsPage()
309 // ----------------------------------------------------------------------------
311 // ----------------------------------------------------------------------------
313 void StaticWidgetsPage::Reset()
315 m_chkVert
->SetValue(false);
316 m_chkAutoResize
->SetValue(true);
318 m_radioHAlign
->SetSelection(StaticHAlign_Left
);
319 m_radioVAlign
->SetSelection(StaticVAlign_Top
);
322 void StaticWidgetsPage::CreateStatic()
324 bool isVert
= m_chkVert
->GetValue();
326 if ( m_sizerStatBox
)
329 // delete m_sizerStatBox; -- deleted by Remove()
330 m_sizerStatic
->Remove(m_sizerStatBox
);
334 #endif // wxUSE_STATLINE
340 if ( !m_chkAutoResize
->GetValue() )
342 flagsText
|= wxST_NO_AUTORESIZE
;
346 switch ( m_radioHAlign
->GetSelection() )
349 wxFAIL_MSG(_T("unexpected radiobox selection"));
352 case StaticHAlign_Left
:
353 align
|= wxALIGN_LEFT
;
356 case StaticHAlign_Centre
:
357 align
|= wxALIGN_CENTRE_HORIZONTAL
;
360 case StaticHAlign_Right
:
361 align
|= wxALIGN_RIGHT
;
365 switch ( m_radioVAlign
->GetSelection() )
368 wxFAIL_MSG(_T("unexpected radiobox selection"));
371 case StaticVAlign_Top
:
372 align
|= wxALIGN_TOP
;
375 case StaticVAlign_Centre
:
376 align
|= wxALIGN_CENTRE_VERTICAL
;
379 case StaticVAlign_Bottom
:
380 align
|= wxALIGN_BOTTOM
;
387 m_staticBox
= new MyStaticBox(this, wxID_ANY
, m_textBox
->GetValue(),
388 wxDefaultPosition
, wxDefaultSize
,
390 m_sizerStatBox
= new wxStaticBoxSizer(m_staticBox
, isVert
? wxHORIZONTAL
393 m_statText
= new MyStaticText(this, wxID_ANY
, m_textLabel
->GetValue(),
394 wxDefaultPosition
, wxDefaultSize
,
398 m_statLine
= new wxStaticLine(this, wxID_ANY
,
399 wxDefaultPosition
, wxDefaultSize
,
400 isVert
? wxLI_VERTICAL
: wxLI_HORIZONTAL
);
401 #endif // wxUSE_STATLINE
403 m_sizerStatBox
->Add(m_statText
, 1, wxGROW
| wxALL
, 5);
405 m_sizerStatBox
->Add(m_statLine
, 0, wxGROW
| wxALL
, 5);
406 #endif // wxUSE_STATLINE
407 m_sizerStatBox
->Add(0, 0, 1);
409 m_sizerStatic
->Add(m_sizerStatBox
, 1, wxGROW
);
411 m_sizerStatic
->Layout();
414 // ----------------------------------------------------------------------------
416 // ----------------------------------------------------------------------------
418 void StaticWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
425 void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
430 void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent
& WXUNUSED(event
))
432 m_sizerStatBox
->GetStaticBox()->SetLabel(m_textBox
->GetValue());
435 void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent
& WXUNUSED(event
))
437 m_statText
->SetLabel(m_textLabel
->GetValue());