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