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