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(){};
145 virtual wxControl
*GetWidget() const { return m_statText
; }
149 void OnCheckOrRadioBox(wxCommandEvent
& event
);
151 void OnButtonReset(wxCommandEvent
& event
);
152 void OnButtonBoxText(wxCommandEvent
& event
);
153 void OnButtonLabelText(wxCommandEvent
& event
);
155 // reset all parameters
158 // (re)create all controls
164 // the check/radio boxes for styles
165 wxCheckBox
*m_chkVert
,
168 wxRadioBox
*m_radioHAlign
,
171 // the controls and the sizer containing them
172 wxStaticBox
*m_staticBox
;
173 wxStaticBoxSizer
*m_sizerStatBox
;
174 wxStaticText
*m_statText
;
176 wxStaticLine
*m_statLine
;
177 #endif // wxUSE_STATLINE
178 wxSizer
*m_sizerStatic
;
180 // the text entries for command parameters
181 wxTextCtrl
*m_textBox
,
185 DECLARE_EVENT_TABLE()
186 DECLARE_WIDGETS_PAGE(StaticWidgetsPage
)
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
193 BEGIN_EVENT_TABLE(StaticWidgetsPage
, WidgetsPage
)
194 EVT_BUTTON(StaticPage_Reset
, StaticWidgetsPage::OnButtonReset
)
195 EVT_BUTTON(StaticPage_LabelText
, StaticWidgetsPage::OnButtonLabelText
)
196 EVT_BUTTON(StaticPage_BoxText
, StaticWidgetsPage::OnButtonBoxText
)
198 EVT_CHECKBOX(wxID_ANY
, StaticWidgetsPage::OnCheckOrRadioBox
)
199 EVT_RADIOBOX(wxID_ANY
, StaticWidgetsPage::OnCheckOrRadioBox
)
202 // ============================================================================
204 // ============================================================================
206 IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage
, _T("Static"));
208 StaticWidgetsPage::StaticWidgetsPage(wxNotebook
*notebook
,
209 wxImageList
*imaglist
)
210 : WidgetsPage(notebook
)
212 imaglist
->Add(wxBitmap(statbox_xpm
));
216 m_chkAutoResize
= (wxCheckBox
*)NULL
;
219 m_radioVAlign
= (wxRadioBox
*)NULL
;
222 m_statLine
= (wxStaticLine
*)NULL
;
223 #endif // wxUSE_STATLINE
224 m_statText
= (wxStaticText
*)NULL
;
226 m_staticBox
= (wxStaticBox
*)NULL
;
227 m_sizerStatBox
= (wxStaticBoxSizer
*)NULL
;
228 m_sizerStatic
= (wxSizer
*)NULL
;
230 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
233 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
235 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
237 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical line"));
238 m_chkAutoResize
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Fit to text"));
239 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
241 static const wxString halign
[] =
248 static const wxString valign
[] =
255 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Horz alignment"),
256 wxDefaultPosition
, wxDefaultSize
,
257 WXSIZEOF(halign
), halign
);
258 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Vert alignment"),
259 wxDefaultPosition
, wxDefaultSize
,
260 WXSIZEOF(valign
), valign
);
262 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
263 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
265 wxButton
*btn
= new wxButton(this, StaticPage_Reset
, _T("&Reset"));
266 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
269 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change labels"));
270 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
274 sizerRow
= CreateSizerWithTextAndButton(StaticPage_BoxText
,
275 _T("Change &box label"),
276 wxID_ANY
, &m_textBox
);
277 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
279 sizerRow
= CreateSizerWithTextAndButton(StaticPage_LabelText
,
280 _T("Change &text label"),
281 wxID_ANY
, &m_textLabel
);
282 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
284 m_textBox
->SetValue(_T("This is a box"));
285 m_textLabel
->SetValue(_T("And this is a label\ninside the box"));
288 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
289 sizerRight
->SetMinSize(150, 0);
290 m_sizerStatic
= sizerRight
;
294 // the 3 panes panes compose the window
295 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
296 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
297 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
299 // final initializations
307 // ----------------------------------------------------------------------------
309 // ----------------------------------------------------------------------------
311 void StaticWidgetsPage::Reset()
313 m_chkVert
->SetValue(false);
314 m_chkAutoResize
->SetValue(true);
316 m_radioHAlign
->SetSelection(StaticHAlign_Left
);
317 m_radioVAlign
->SetSelection(StaticVAlign_Top
);
320 void StaticWidgetsPage::CreateStatic()
322 bool isVert
= m_chkVert
->GetValue();
324 if ( m_sizerStatBox
)
327 // delete m_sizerStatBox; -- deleted by Remove()
328 m_sizerStatic
->Remove(m_sizerStatBox
);
332 #endif // wxUSE_STATLINE
338 if ( !m_chkAutoResize
->GetValue() )
340 flagsText
|= wxST_NO_AUTORESIZE
;
344 switch ( m_radioHAlign
->GetSelection() )
347 wxFAIL_MSG(_T("unexpected radiobox selection"));
350 case StaticHAlign_Left
:
351 align
|= wxALIGN_LEFT
;
354 case StaticHAlign_Centre
:
355 align
|= wxALIGN_CENTRE_HORIZONTAL
;
358 case StaticHAlign_Right
:
359 align
|= wxALIGN_RIGHT
;
363 switch ( m_radioVAlign
->GetSelection() )
366 wxFAIL_MSG(_T("unexpected radiobox selection"));
369 case StaticVAlign_Top
:
370 align
|= wxALIGN_TOP
;
373 case StaticVAlign_Centre
:
374 align
|= wxALIGN_CENTRE_VERTICAL
;
377 case StaticVAlign_Bottom
:
378 align
|= wxALIGN_BOTTOM
;
385 m_staticBox
= new MyStaticBox(this, wxID_ANY
, m_textBox
->GetValue(),
386 wxDefaultPosition
, wxDefaultSize
,
388 m_sizerStatBox
= new wxStaticBoxSizer(m_staticBox
, isVert
? wxHORIZONTAL
391 m_statText
= new MyStaticText(this, wxID_ANY
, m_textLabel
->GetValue(),
392 wxDefaultPosition
, wxDefaultSize
,
396 m_statLine
= new wxStaticLine(this, wxID_ANY
,
397 wxDefaultPosition
, wxDefaultSize
,
398 isVert
? wxLI_VERTICAL
: wxLI_HORIZONTAL
);
399 #endif // wxUSE_STATLINE
401 m_sizerStatBox
->Add(m_statText
, 1, wxGROW
| wxALL
, 5);
403 m_sizerStatBox
->Add(m_statLine
, 0, wxGROW
| wxALL
, 5);
404 #endif // wxUSE_STATLINE
405 m_sizerStatBox
->Add(0, 0, 1);
407 m_sizerStatic
->Add(m_sizerStatBox
, 1, wxGROW
);
409 m_sizerStatic
->Layout();
412 // ----------------------------------------------------------------------------
414 // ----------------------------------------------------------------------------
416 void StaticWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
423 void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
428 void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent
& WXUNUSED(event
))
430 m_sizerStatBox
->GetStaticBox()->SetLabel(m_textBox
->GetValue());
433 void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent
& WXUNUSED(event
))
435 m_statText
->SetLabel(m_textLabel
->GetValue());