Doc tweaks
[wxWidgets.git] / samples / widgets / static.cpp
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
31 #include "wx/button.h"
32 #include "wx/checkbox.h"
33 #include "wx/radiobox.h"
34 #include "wx/statbox.h"
35 #include "wx/stattext.h"
36 #include "wx/textctrl.h"
37 #endif
38
39 #include "wx/sizer.h"
40
41 #include "wx/statline.h"
42
43 #include "widgets.h"
44 #if 1
45 #include "icons/statbox.xpm"
46
47 // ----------------------------------------------------------------------------
48 // constants
49 // ----------------------------------------------------------------------------
50
51 // control ids
52 enum
53 {
54 StaticPage_Reset = 100,
55 StaticPage_BoxText,
56 StaticPage_LabelText
57 };
58
59 // alignment radiobox values
60 enum
61 {
62 StaticHAlign_Left,
63 StaticHAlign_Centre,
64 StaticHAlign_Right,
65 StaticHAlign_Max
66 };
67
68 enum
69 {
70 StaticVAlign_Top,
71 StaticVAlign_Centre,
72 StaticVAlign_Bottom,
73 StaticVAlign_Max
74 };
75
76 class DerivedStaticText: public wxStaticText
77 {
78 public:
79 DerivedStaticText(wxWindow* parent, wxWindowID id, const wxString& label,
80 const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
81 long style = 0):
82 wxStaticText(parent, id, label, pos, size, style)
83 {
84 }
85 void OnMouseEvent(wxMouseEvent& event)
86 {
87 if (event.LeftDown())
88 wxMessageBox(wxT("Clicked on static text"));
89 }
90 DECLARE_EVENT_TABLE()
91 };
92
93 BEGIN_EVENT_TABLE(DerivedStaticText, wxStaticText)
94 EVT_MOUSE_EVENTS(DerivedStaticText::OnMouseEvent)
95 END_EVENT_TABLE()
96
97 // ----------------------------------------------------------------------------
98 // StaticWidgetsPage
99 // ----------------------------------------------------------------------------
100
101 class StaticWidgetsPage : public WidgetsPage
102 {
103 public:
104 StaticWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
105 virtual ~StaticWidgetsPage();
106
107 protected:
108 // event handlers
109 void OnCheckOrRadioBox(wxCommandEvent& event);
110
111 void OnButtonReset(wxCommandEvent& event);
112 void OnButtonBoxText(wxCommandEvent& event);
113 void OnButtonLabelText(wxCommandEvent& event);
114
115 // reset all parameters
116 void Reset();
117
118 // (re)create all controls
119 void CreateStatic();
120
121 // the controls
122 // ------------
123
124 // the check/radio boxes for styles
125 wxCheckBox *m_chkVert,
126 *m_chkAutoResize;
127
128 wxRadioBox *m_radioHAlign,
129 *m_radioVAlign;
130
131 // the controls and the sizer containing them
132 wxStaticBoxSizer *m_sizerStatBox;
133 wxStaticText *m_statText;
134 wxStaticLine *m_statLine;
135 wxSizer *m_sizerStatic;
136
137 // the text entries for command parameters
138 wxTextCtrl *m_textBox,
139 *m_textLabel;
140
141 private:
142 DECLARE_EVENT_TABLE()
143 DECLARE_WIDGETS_PAGE(StaticWidgetsPage)
144 };
145
146 // ----------------------------------------------------------------------------
147 // event tables
148 // ----------------------------------------------------------------------------
149
150 BEGIN_EVENT_TABLE(StaticWidgetsPage, WidgetsPage)
151 EVT_BUTTON(StaticPage_Reset, StaticWidgetsPage::OnButtonReset)
152 EVT_BUTTON(StaticPage_LabelText, StaticWidgetsPage::OnButtonLabelText)
153 EVT_BUTTON(StaticPage_BoxText, StaticWidgetsPage::OnButtonBoxText)
154
155 EVT_CHECKBOX(-1, StaticWidgetsPage::OnCheckOrRadioBox)
156 EVT_RADIOBOX(-1, StaticWidgetsPage::OnCheckOrRadioBox)
157 END_EVENT_TABLE()
158
159 // ============================================================================
160 // implementation
161 // ============================================================================
162
163 IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage, _T("Static"));
164
165 StaticWidgetsPage::StaticWidgetsPage(wxNotebook *notebook,
166 wxImageList *imaglist)
167 : WidgetsPage(notebook)
168 {
169 imaglist->Add(wxBitmap(statbox_xpm));
170
171 // init everything
172 m_chkVert =
173 m_chkAutoResize = (wxCheckBox *)NULL;
174
175 m_radioHAlign =
176 m_radioVAlign = (wxRadioBox *)NULL;
177
178 m_statLine = (wxStaticLine *)NULL;
179 m_statText = (wxStaticText *)NULL;
180
181 m_sizerStatBox = (wxStaticBoxSizer *)NULL;
182 m_sizerStatic = (wxSizer *)NULL;
183
184 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
185
186 // left pane
187 wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set style"));
188
189 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
190
191 m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical line"));
192 m_chkAutoResize = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Fit to text"));
193 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
194
195 static const wxString halign[] =
196 {
197 _T("left"),
198 _T("centre"),
199 _T("right"),
200 };
201
202 static const wxString valign[] =
203 {
204 _T("top"),
205 _T("centre"),
206 _T("bottom"),
207 };
208
209 m_radioHAlign = new wxRadioBox(this, -1, _T("&Horz alignment"),
210 wxDefaultPosition, wxDefaultSize,
211 WXSIZEOF(halign), halign);
212 m_radioVAlign = new wxRadioBox(this, -1, _T("&Vert alignment"),
213 wxDefaultPosition, wxDefaultSize,
214 WXSIZEOF(valign), valign);
215
216 sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
217 sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
218
219 wxButton *btn = new wxButton(this, StaticPage_Reset, _T("&Reset"));
220 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
221
222 // middle pane
223 wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Change labels"));
224 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
225
226 wxSizer *sizerRow;
227
228 sizerRow = CreateSizerWithTextAndButton(StaticPage_BoxText,
229 _T("Change &box label"),
230 -1, &m_textBox);
231 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
232
233 sizerRow = CreateSizerWithTextAndButton(StaticPage_LabelText,
234 _T("Change &text label"),
235 -1, &m_textLabel);
236 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
237
238 m_textBox->SetValue(_T("This is a box"));
239 m_textLabel->SetValue(_T("And this is a label\ninside the box"));
240
241 // right pane
242 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
243 sizerRight->SetMinSize(150, 0);
244 m_sizerStatic = sizerRight;
245
246 CreateStatic();
247
248 // the 3 panes panes compose the window
249 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
250 sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10);
251 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
252
253 // final initializations
254 Reset();
255
256 SetAutoLayout(TRUE);
257 SetSizer(sizerTop);
258
259 sizerTop->Fit(this);
260 }
261
262 StaticWidgetsPage::~StaticWidgetsPage()
263 {
264 }
265
266 // ----------------------------------------------------------------------------
267 // operations
268 // ----------------------------------------------------------------------------
269
270 void StaticWidgetsPage::Reset()
271 {
272 m_chkVert->SetValue(FALSE);
273 m_chkAutoResize->SetValue(TRUE);
274
275 m_radioHAlign->SetSelection(StaticHAlign_Left);
276 m_radioVAlign->SetSelection(StaticVAlign_Top);
277 }
278
279 void StaticWidgetsPage::CreateStatic()
280 {
281 bool isVert = m_chkVert->GetValue();
282
283 if ( m_sizerStatBox )
284 {
285 m_sizerStatic->Remove(m_sizerStatBox);
286
287 // delete m_sizerStatBox; -- deleted by Remove()
288 delete m_statText;
289 delete m_statLine;
290 }
291
292 int flagsBox = 0,
293 flagsText = 0;
294
295 if ( !m_chkAutoResize->GetValue() )
296 {
297 flagsText |= wxST_NO_AUTORESIZE;
298 }
299
300 int align = 0;
301 switch ( m_radioHAlign->GetSelection() )
302 {
303 default:
304 wxFAIL_MSG(_T("unexpected radiobox selection"));
305 // fall through
306
307 case StaticHAlign_Left:
308 align |= wxALIGN_LEFT;
309 break;
310
311 case StaticHAlign_Centre:
312 align |= wxALIGN_CENTRE_HORIZONTAL;
313 break;
314
315 case StaticHAlign_Right:
316 align |= wxALIGN_RIGHT;
317 break;
318 }
319
320 switch ( m_radioVAlign->GetSelection() )
321 {
322 default:
323 wxFAIL_MSG(_T("unexpected radiobox selection"));
324 // fall through
325
326 case StaticVAlign_Top:
327 align |= wxALIGN_TOP;
328 break;
329
330 case StaticVAlign_Centre:
331 align |= wxALIGN_CENTRE_VERTICAL;
332 break;
333
334 case StaticVAlign_Bottom:
335 align |= wxALIGN_BOTTOM;
336 break;
337 }
338
339 flagsText |= align;
340 flagsBox |= align;
341
342 wxStaticBox *box = new wxStaticBox(this, -1, m_textBox->GetValue(),
343 wxDefaultPosition, wxDefaultSize,
344 flagsBox);
345 m_sizerStatBox = new wxStaticBoxSizer(box, isVert ? wxHORIZONTAL
346 : wxVERTICAL);
347
348 m_statText = new DerivedStaticText(this, -1, m_textLabel->GetValue(),
349 wxDefaultPosition, wxDefaultSize,
350 flagsText);
351
352 m_statLine = new wxStaticLine(this, -1,
353 wxDefaultPosition, wxDefaultSize,
354 isVert ? wxLI_VERTICAL : wxLI_HORIZONTAL);
355
356 m_sizerStatBox->Add(m_statText, 1, wxGROW | wxALL, 5);
357 m_sizerStatBox->Add(m_statLine, 0, wxGROW | wxALL, 5);
358 m_sizerStatBox->Add(0, 0, 1);
359
360 m_sizerStatic->Add(m_sizerStatBox, 1, wxGROW);
361
362 m_sizerStatic->Layout();
363 }
364
365 // ----------------------------------------------------------------------------
366 // event handlers
367 // ----------------------------------------------------------------------------
368
369 void StaticWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
370 {
371 Reset();
372
373 CreateStatic();
374 }
375
376 void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
377 {
378 CreateStatic();
379 }
380
381 void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent& event)
382 {
383 m_sizerStatBox->GetStaticBox()->SetLabel(m_textBox->GetValue());
384 }
385
386 void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent& event)
387 {
388 m_statText->SetLabel(m_textLabel->GetValue());
389 }
390
391 #endif