]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/static.cpp
Towards other setups.
[wxWidgets.git] / samples / widgets / static.cpp
CommitLineData
32b8ec41 1/////////////////////////////////////////////////////////////////////////////
be5a51fb 2// Program: wxWidgets Widgets Sample
32b8ec41
VZ
3// Name: static.cpp
4// Purpose: Part of the widgets sample showing various static controls
5// Author: Vadim Zeitlin
6// Created: 11.04.01
7// Id: $Id$
8// Copyright: (c) 2001 Vadim Zeitlin
9// License: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// for compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27// for all others, include the necessary headers
28#ifndef WX_PRECOMP
29 #include "wx/log.h"
30
3bb70c40 31 #include "wx/bitmap.h"
32b8ec41
VZ
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"
38#endif
39
40#include "wx/sizer.h"
41
42#include "wx/statline.h"
43
44#include "widgets.h"
32b8ec41
VZ
45#include "icons/statbox.xpm"
46
47// ----------------------------------------------------------------------------
48// constants
49// ----------------------------------------------------------------------------
50
51// control ids
52enum
53{
54 StaticPage_Reset = 100,
55 StaticPage_BoxText,
56 StaticPage_LabelText
57};
58
59// alignment radiobox values
60enum
61{
62 StaticHAlign_Left,
63 StaticHAlign_Centre,
64 StaticHAlign_Right,
65 StaticHAlign_Max
66};
67
68enum
69{
70 StaticVAlign_Top,
71 StaticVAlign_Centre,
72 StaticVAlign_Bottom,
73 StaticVAlign_Max
74};
75
13cd1745
VZ
76// ----------------------------------------------------------------------------
77// MyStaticText and MyStaticBox
78// ----------------------------------------------------------------------------
79
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
82
83class MyStaticText : public wxStaticText
a9d171bd
JS
84{
85public:
13cd1745
VZ
86 MyStaticText(wxWindow* parent,
87 wxWindowID id,
88 const wxString& label,
89 const wxPoint& pos = wxDefaultPosition,
90 const wxSize& size = wxDefaultSize,
91 long style = 0)
92 : wxStaticText(parent, id, label, pos, size, style)
a9d171bd
JS
93 {
94 }
13cd1745
VZ
95
96protected:
c02e5a31 97 void OnMouseEvent(wxMouseEvent& WXUNUSED(event))
a9d171bd 98 {
13cd1745 99 wxLogMessage(wxT("Clicked on static text"));
a9d171bd 100 }
13cd1745
VZ
101
102 DECLARE_EVENT_TABLE()
a9d171bd
JS
103};
104
13cd1745
VZ
105class MyStaticBox : public wxStaticBox
106{
107public:
108 MyStaticBox(wxWindow* parent,
109 wxWindowID id,
110 const wxString& label,
111 const wxPoint& pos = wxDefaultPosition,
112 const wxSize& size = wxDefaultSize,
113 long style = 0)
114 : wxStaticBox(parent, id, label, pos, size, style)
115 {
116 }
117
118protected:
c02e5a31 119 void OnMouseEvent(wxMouseEvent& WXUNUSED(event))
13cd1745
VZ
120 {
121 wxLogMessage(wxT("Clicked on static box"));
122 }
123
124 DECLARE_EVENT_TABLE()
125};
126
127BEGIN_EVENT_TABLE(MyStaticText, wxStaticText)
128 EVT_LEFT_UP(MyStaticText::OnMouseEvent)
129END_EVENT_TABLE()
130
131BEGIN_EVENT_TABLE(MyStaticBox, wxStaticBox)
132 EVT_LEFT_UP(MyStaticBox::OnMouseEvent)
a9d171bd
JS
133END_EVENT_TABLE()
134
32b8ec41
VZ
135// ----------------------------------------------------------------------------
136// StaticWidgetsPage
137// ----------------------------------------------------------------------------
138
139class StaticWidgetsPage : public WidgetsPage
140{
141public:
5378558e 142 StaticWidgetsPage(wxBookCtrlBase *book, wxImageList *imaglist);
8f6eaec9 143 virtual ~StaticWidgetsPage(){};
32b8ec41 144
195df7a7
VZ
145 virtual wxControl *GetWidget() const { return m_statText; }
146
32b8ec41
VZ
147protected:
148 // event handlers
149 void OnCheckOrRadioBox(wxCommandEvent& event);
150
151 void OnButtonReset(wxCommandEvent& event);
152 void OnButtonBoxText(wxCommandEvent& event);
153 void OnButtonLabelText(wxCommandEvent& event);
154
155 // reset all parameters
156 void Reset();
157
158 // (re)create all controls
159 void CreateStatic();
160
161 // the controls
162 // ------------
163
164 // the check/radio boxes for styles
165 wxCheckBox *m_chkVert,
166 *m_chkAutoResize;
167
168 wxRadioBox *m_radioHAlign,
169 *m_radioVAlign;
170
171 // the controls and the sizer containing them
168954a1 172 wxStaticBox *m_staticBox;
32b8ec41
VZ
173 wxStaticBoxSizer *m_sizerStatBox;
174 wxStaticText *m_statText;
a56938e4 175#if wxUSE_STATLINE
32b8ec41 176 wxStaticLine *m_statLine;
a56938e4 177#endif // wxUSE_STATLINE
32b8ec41
VZ
178 wxSizer *m_sizerStatic;
179
180 // the text entries for command parameters
181 wxTextCtrl *m_textBox,
182 *m_textLabel;
183
184private:
5e173f35
GD
185 DECLARE_EVENT_TABLE()
186 DECLARE_WIDGETS_PAGE(StaticWidgetsPage)
32b8ec41
VZ
187};
188
189// ----------------------------------------------------------------------------
190// event tables
191// ----------------------------------------------------------------------------
192
193BEGIN_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)
197
206d3a16
JS
198 EVT_CHECKBOX(wxID_ANY, StaticWidgetsPage::OnCheckOrRadioBox)
199 EVT_RADIOBOX(wxID_ANY, StaticWidgetsPage::OnCheckOrRadioBox)
32b8ec41
VZ
200END_EVENT_TABLE()
201
202// ============================================================================
203// implementation
204// ============================================================================
205
206IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage, _T("Static"));
207
5378558e 208StaticWidgetsPage::StaticWidgetsPage(wxBookCtrlBase *book,
61c083e7
WS
209 wxImageList *imaglist)
210 : WidgetsPage(book)
32b8ec41
VZ
211{
212 imaglist->Add(wxBitmap(statbox_xpm));
213
214 // init everything
215 m_chkVert =
216 m_chkAutoResize = (wxCheckBox *)NULL;
217
218 m_radioHAlign =
219 m_radioVAlign = (wxRadioBox *)NULL;
220
a56938e4 221#if wxUSE_STATLINE
32b8ec41 222 m_statLine = (wxStaticLine *)NULL;
a56938e4 223#endif // wxUSE_STATLINE
32b8ec41
VZ
224 m_statText = (wxStaticText *)NULL;
225
168954a1 226 m_staticBox = (wxStaticBox *)NULL;
32b8ec41
VZ
227 m_sizerStatBox = (wxStaticBoxSizer *)NULL;
228 m_sizerStatic = (wxSizer *)NULL;
229
230 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
231
232 // left pane
206d3a16 233 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
32b8ec41
VZ
234
235 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
236
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
240
241 static const wxString halign[] =
242 {
243 _T("left"),
244 _T("centre"),
245 _T("right"),
246 };
247
248 static const wxString valign[] =
249 {
250 _T("top"),
251 _T("centre"),
252 _T("bottom"),
253 };
254
206d3a16 255 m_radioHAlign = new wxRadioBox(this, wxID_ANY, _T("&Horz alignment"),
32b8ec41
VZ
256 wxDefaultPosition, wxDefaultSize,
257 WXSIZEOF(halign), halign);
206d3a16 258 m_radioVAlign = new wxRadioBox(this, wxID_ANY, _T("&Vert alignment"),
32b8ec41
VZ
259 wxDefaultPosition, wxDefaultSize,
260 WXSIZEOF(valign), valign);
261
262 sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
263 sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
264
265 wxButton *btn = new wxButton(this, StaticPage_Reset, _T("&Reset"));
266 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
267
268 // middle pane
206d3a16 269 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Change labels"));
32b8ec41
VZ
270 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
271
272 wxSizer *sizerRow;
273
274 sizerRow = CreateSizerWithTextAndButton(StaticPage_BoxText,
275 _T("Change &box label"),
206d3a16 276 wxID_ANY, &m_textBox);
32b8ec41
VZ
277 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
278
279 sizerRow = CreateSizerWithTextAndButton(StaticPage_LabelText,
280 _T("Change &text label"),
206d3a16 281 wxID_ANY, &m_textLabel);
32b8ec41
VZ
282 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
283
284 m_textBox->SetValue(_T("This is a box"));
285 m_textLabel->SetValue(_T("And this is a label\ninside the box"));
286
287 // right pane
288 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
7b127900 289 sizerRight->SetMinSize(150, 0);
32b8ec41
VZ
290 m_sizerStatic = sizerRight;
291
292 CreateStatic();
293
294 // the 3 panes panes compose the window
295 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
7b127900 296 sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10);
32b8ec41
VZ
297 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
298
299 // final initializations
300 Reset();
301
32b8ec41
VZ
302 SetSizer(sizerTop);
303
304 sizerTop->Fit(this);
305}
306
32b8ec41
VZ
307// ----------------------------------------------------------------------------
308// operations
309// ----------------------------------------------------------------------------
310
311void StaticWidgetsPage::Reset()
312{
206d3a16
JS
313 m_chkVert->SetValue(false);
314 m_chkAutoResize->SetValue(true);
32b8ec41
VZ
315
316 m_radioHAlign->SetSelection(StaticHAlign_Left);
317 m_radioVAlign->SetSelection(StaticVAlign_Top);
318}
319
320void StaticWidgetsPage::CreateStatic()
321{
322 bool isVert = m_chkVert->GetValue();
323
324 if ( m_sizerStatBox )
325 {
168954a1 326 delete m_staticBox;
32b8ec41 327 // delete m_sizerStatBox; -- deleted by Remove()
168954a1 328 m_sizerStatic->Remove(m_sizerStatBox);
32b8ec41 329 delete m_statText;
a56938e4 330#if wxUSE_STATLINE
32b8ec41 331 delete m_statLine;
a56938e4 332#endif // wxUSE_STATLINE
32b8ec41
VZ
333 }
334
335 int flagsBox = 0,
336 flagsText = 0;
337
338 if ( !m_chkAutoResize->GetValue() )
339 {
340 flagsText |= wxST_NO_AUTORESIZE;
341 }
342
343 int align = 0;
344 switch ( m_radioHAlign->GetSelection() )
345 {
346 default:
347 wxFAIL_MSG(_T("unexpected radiobox selection"));
348 // fall through
349
350 case StaticHAlign_Left:
351 align |= wxALIGN_LEFT;
352 break;
353
354 case StaticHAlign_Centre:
355 align |= wxALIGN_CENTRE_HORIZONTAL;
356 break;
357
358 case StaticHAlign_Right:
359 align |= wxALIGN_RIGHT;
360 break;
361 }
362
363 switch ( m_radioVAlign->GetSelection() )
364 {
365 default:
366 wxFAIL_MSG(_T("unexpected radiobox selection"));
367 // fall through
368
369 case StaticVAlign_Top:
370 align |= wxALIGN_TOP;
371 break;
372
373 case StaticVAlign_Centre:
374 align |= wxALIGN_CENTRE_VERTICAL;
375 break;
376
377 case StaticVAlign_Bottom:
378 align |= wxALIGN_BOTTOM;
379 break;
380 }
381
382 flagsText |= align;
383 flagsBox |= align;
384
206d3a16 385 m_staticBox = new MyStaticBox(this, wxID_ANY, m_textBox->GetValue(),
168954a1
VZ
386 wxDefaultPosition, wxDefaultSize,
387 flagsBox);
388 m_sizerStatBox = new wxStaticBoxSizer(m_staticBox, isVert ? wxHORIZONTAL
389 : wxVERTICAL);
32b8ec41 390
206d3a16 391 m_statText = new MyStaticText(this, wxID_ANY, m_textLabel->GetValue(),
32b8ec41
VZ
392 wxDefaultPosition, wxDefaultSize,
393 flagsText);
394
a56938e4 395#if wxUSE_STATLINE
206d3a16 396 m_statLine = new wxStaticLine(this, wxID_ANY,
32b8ec41
VZ
397 wxDefaultPosition, wxDefaultSize,
398 isVert ? wxLI_VERTICAL : wxLI_HORIZONTAL);
a56938e4 399#endif // wxUSE_STATLINE
32b8ec41
VZ
400
401 m_sizerStatBox->Add(m_statText, 1, wxGROW | wxALL, 5);
a56938e4 402#if wxUSE_STATLINE
32b8ec41 403 m_sizerStatBox->Add(m_statLine, 0, wxGROW | wxALL, 5);
a56938e4 404#endif // wxUSE_STATLINE
32b8ec41
VZ
405 m_sizerStatBox->Add(0, 0, 1);
406
407 m_sizerStatic->Add(m_sizerStatBox, 1, wxGROW);
408
409 m_sizerStatic->Layout();
410}
411
412// ----------------------------------------------------------------------------
413// event handlers
414// ----------------------------------------------------------------------------
415
416void StaticWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
417{
418 Reset();
419
420 CreateStatic();
421}
422
c02e5a31 423void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
424{
425 CreateStatic();
426}
427
c02e5a31 428void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
429{
430 m_sizerStatBox->GetStaticBox()->SetLabel(m_textBox->GetValue());
431}
432
c02e5a31 433void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
434{
435 m_statText->SetLabel(m_textLabel->GetValue());
436}
437