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