]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/static.cpp
A clarification
[wxWidgets.git] / samples / widgets / static.cpp
CommitLineData
32b8ec41
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Program: wxWindows Widgets Sample
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:
142 StaticWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
143 virtual ~StaticWidgetsPage();
144
145protected:
146 // event handlers
147 void OnCheckOrRadioBox(wxCommandEvent& event);
148
149 void OnButtonReset(wxCommandEvent& event);
150 void OnButtonBoxText(wxCommandEvent& event);
151 void OnButtonLabelText(wxCommandEvent& event);
152
153 // reset all parameters
154 void Reset();
155
156 // (re)create all controls
157 void CreateStatic();
158
159 // the controls
160 // ------------
161
162 // the check/radio boxes for styles
163 wxCheckBox *m_chkVert,
164 *m_chkAutoResize;
165
166 wxRadioBox *m_radioHAlign,
167 *m_radioVAlign;
168
169 // the controls and the sizer containing them
168954a1 170 wxStaticBox *m_staticBox;
32b8ec41
VZ
171 wxStaticBoxSizer *m_sizerStatBox;
172 wxStaticText *m_statText;
173 wxStaticLine *m_statLine;
174 wxSizer *m_sizerStatic;
175
176 // the text entries for command parameters
177 wxTextCtrl *m_textBox,
178 *m_textLabel;
179
180private:
5e173f35
GD
181 DECLARE_EVENT_TABLE()
182 DECLARE_WIDGETS_PAGE(StaticWidgetsPage)
32b8ec41
VZ
183};
184
185// ----------------------------------------------------------------------------
186// event tables
187// ----------------------------------------------------------------------------
188
189BEGIN_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)
193
206d3a16
JS
194 EVT_CHECKBOX(wxID_ANY, StaticWidgetsPage::OnCheckOrRadioBox)
195 EVT_RADIOBOX(wxID_ANY, StaticWidgetsPage::OnCheckOrRadioBox)
32b8ec41
VZ
196END_EVENT_TABLE()
197
198// ============================================================================
199// implementation
200// ============================================================================
201
202IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage, _T("Static"));
203
204StaticWidgetsPage::StaticWidgetsPage(wxNotebook *notebook,
205 wxImageList *imaglist)
206 : WidgetsPage(notebook)
207{
208 imaglist->Add(wxBitmap(statbox_xpm));
209
210 // init everything
211 m_chkVert =
212 m_chkAutoResize = (wxCheckBox *)NULL;
213
214 m_radioHAlign =
215 m_radioVAlign = (wxRadioBox *)NULL;
216
217 m_statLine = (wxStaticLine *)NULL;
218 m_statText = (wxStaticText *)NULL;
219
168954a1 220 m_staticBox = (wxStaticBox *)NULL;
32b8ec41
VZ
221 m_sizerStatBox = (wxStaticBoxSizer *)NULL;
222 m_sizerStatic = (wxSizer *)NULL;
223
224 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
225
226 // left pane
206d3a16 227 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
32b8ec41
VZ
228
229 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
230
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
234
235 static const wxString halign[] =
236 {
237 _T("left"),
238 _T("centre"),
239 _T("right"),
240 };
241
242 static const wxString valign[] =
243 {
244 _T("top"),
245 _T("centre"),
246 _T("bottom"),
247 };
248
206d3a16 249 m_radioHAlign = new wxRadioBox(this, wxID_ANY, _T("&Horz alignment"),
32b8ec41
VZ
250 wxDefaultPosition, wxDefaultSize,
251 WXSIZEOF(halign), halign);
206d3a16 252 m_radioVAlign = new wxRadioBox(this, wxID_ANY, _T("&Vert alignment"),
32b8ec41
VZ
253 wxDefaultPosition, wxDefaultSize,
254 WXSIZEOF(valign), valign);
255
256 sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
257 sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
258
259 wxButton *btn = new wxButton(this, StaticPage_Reset, _T("&Reset"));
260 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
261
262 // middle pane
206d3a16 263 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Change labels"));
32b8ec41
VZ
264 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
265
266 wxSizer *sizerRow;
267
268 sizerRow = CreateSizerWithTextAndButton(StaticPage_BoxText,
269 _T("Change &box label"),
206d3a16 270 wxID_ANY, &m_textBox);
32b8ec41
VZ
271 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
272
273 sizerRow = CreateSizerWithTextAndButton(StaticPage_LabelText,
274 _T("Change &text label"),
206d3a16 275 wxID_ANY, &m_textLabel);
32b8ec41
VZ
276 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
277
278 m_textBox->SetValue(_T("This is a box"));
279 m_textLabel->SetValue(_T("And this is a label\ninside the box"));
280
281 // right pane
282 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
7b127900 283 sizerRight->SetMinSize(150, 0);
32b8ec41
VZ
284 m_sizerStatic = sizerRight;
285
286 CreateStatic();
287
288 // the 3 panes panes compose the window
289 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
7b127900 290 sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10);
32b8ec41
VZ
291 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
292
293 // final initializations
294 Reset();
295
32b8ec41
VZ
296 SetSizer(sizerTop);
297
298 sizerTop->Fit(this);
299}
300
301StaticWidgetsPage::~StaticWidgetsPage()
302{
303}
304
305// ----------------------------------------------------------------------------
306// operations
307// ----------------------------------------------------------------------------
308
309void StaticWidgetsPage::Reset()
310{
206d3a16
JS
311 m_chkVert->SetValue(false);
312 m_chkAutoResize->SetValue(true);
32b8ec41
VZ
313
314 m_radioHAlign->SetSelection(StaticHAlign_Left);
315 m_radioVAlign->SetSelection(StaticVAlign_Top);
316}
317
318void StaticWidgetsPage::CreateStatic()
319{
320 bool isVert = m_chkVert->GetValue();
321
322 if ( m_sizerStatBox )
323 {
168954a1 324 delete m_staticBox;
32b8ec41 325 // delete m_sizerStatBox; -- deleted by Remove()
168954a1 326 m_sizerStatic->Remove(m_sizerStatBox);
32b8ec41
VZ
327 delete m_statText;
328 delete m_statLine;
329 }
330
331 int flagsBox = 0,
332 flagsText = 0;
333
334 if ( !m_chkAutoResize->GetValue() )
335 {
336 flagsText |= wxST_NO_AUTORESIZE;
337 }
338
339 int align = 0;
340 switch ( m_radioHAlign->GetSelection() )
341 {
342 default:
343 wxFAIL_MSG(_T("unexpected radiobox selection"));
344 // fall through
345
346 case StaticHAlign_Left:
347 align |= wxALIGN_LEFT;
348 break;
349
350 case StaticHAlign_Centre:
351 align |= wxALIGN_CENTRE_HORIZONTAL;
352 break;
353
354 case StaticHAlign_Right:
355 align |= wxALIGN_RIGHT;
356 break;
357 }
358
359 switch ( m_radioVAlign->GetSelection() )
360 {
361 default:
362 wxFAIL_MSG(_T("unexpected radiobox selection"));
363 // fall through
364
365 case StaticVAlign_Top:
366 align |= wxALIGN_TOP;
367 break;
368
369 case StaticVAlign_Centre:
370 align |= wxALIGN_CENTRE_VERTICAL;
371 break;
372
373 case StaticVAlign_Bottom:
374 align |= wxALIGN_BOTTOM;
375 break;
376 }
377
378 flagsText |= align;
379 flagsBox |= align;
380
206d3a16 381 m_staticBox = new MyStaticBox(this, wxID_ANY, m_textBox->GetValue(),
168954a1
VZ
382 wxDefaultPosition, wxDefaultSize,
383 flagsBox);
384 m_sizerStatBox = new wxStaticBoxSizer(m_staticBox, isVert ? wxHORIZONTAL
385 : wxVERTICAL);
32b8ec41 386
206d3a16 387 m_statText = new MyStaticText(this, wxID_ANY, m_textLabel->GetValue(),
32b8ec41
VZ
388 wxDefaultPosition, wxDefaultSize,
389 flagsText);
390
206d3a16 391 m_statLine = new wxStaticLine(this, wxID_ANY,
32b8ec41
VZ
392 wxDefaultPosition, wxDefaultSize,
393 isVert ? wxLI_VERTICAL : wxLI_HORIZONTAL);
394
395 m_sizerStatBox->Add(m_statText, 1, wxGROW | wxALL, 5);
396 m_sizerStatBox->Add(m_statLine, 0, wxGROW | wxALL, 5);
397 m_sizerStatBox->Add(0, 0, 1);
398
399 m_sizerStatic->Add(m_sizerStatBox, 1, wxGROW);
400
401 m_sizerStatic->Layout();
402}
403
404// ----------------------------------------------------------------------------
405// event handlers
406// ----------------------------------------------------------------------------
407
408void StaticWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
409{
410 Reset();
411
412 CreateStatic();
413}
414
c02e5a31 415void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
416{
417 CreateStatic();
418}
419
c02e5a31 420void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
421{
422 m_sizerStatBox->GetStaticBox()->SetLabel(m_textBox->GetValue());
423}
424
c02e5a31 425void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
426{
427 m_statText->SetLabel(m_textLabel->GetValue());
428}
429