]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/static.cpp
Don't use bitmap by default.
[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 #include "icons/statbox.xpm"
45
46 // ----------------------------------------------------------------------------
47 // constants
48 // ----------------------------------------------------------------------------
49
50 // control ids
51 enum
52 {
53 StaticPage_Reset = 100,
54 StaticPage_BoxText,
55 StaticPage_LabelText
56 };
57
58 // alignment radiobox values
59 enum
60 {
61 StaticHAlign_Left,
62 StaticHAlign_Centre,
63 StaticHAlign_Right,
64 StaticHAlign_Max
65 };
66
67 enum
68 {
69 StaticVAlign_Top,
70 StaticVAlign_Centre,
71 StaticVAlign_Bottom,
72 StaticVAlign_Max
73 };
74
75 // ----------------------------------------------------------------------------
76 // MyStaticText and MyStaticBox
77 // ----------------------------------------------------------------------------
78
79 // these 2 classes simply show that the static controls can get the mouse
80 // clicks too -- this used to be broken under MSW but works now
81
82 class MyStaticText : public wxStaticText
83 {
84 public:
85 MyStaticText(wxWindow* parent,
86 wxWindowID id,
87 const wxString& label,
88 const wxPoint& pos = wxDefaultPosition,
89 const wxSize& size = wxDefaultSize,
90 long style = 0)
91 : wxStaticText(parent, id, label, pos, size, style)
92 {
93 }
94
95 protected:
96 void OnMouseEvent(wxMouseEvent& event)
97 {
98 wxLogMessage(wxT("Clicked on static text"));
99 }
100
101 DECLARE_EVENT_TABLE()
102 };
103
104 class MyStaticBox : public wxStaticBox
105 {
106 public:
107 MyStaticBox(wxWindow* parent,
108 wxWindowID id,
109 const wxString& label,
110 const wxPoint& pos = wxDefaultPosition,
111 const wxSize& size = wxDefaultSize,
112 long style = 0)
113 : wxStaticBox(parent, id, label, pos, size, style)
114 {
115 }
116
117 protected:
118 void OnMouseEvent(wxMouseEvent& event)
119 {
120 wxLogMessage(wxT("Clicked on static box"));
121 }
122
123 DECLARE_EVENT_TABLE()
124 };
125
126 BEGIN_EVENT_TABLE(MyStaticText, wxStaticText)
127 EVT_LEFT_UP(MyStaticText::OnMouseEvent)
128 END_EVENT_TABLE()
129
130 BEGIN_EVENT_TABLE(MyStaticBox, wxStaticBox)
131 EVT_LEFT_UP(MyStaticBox::OnMouseEvent)
132 END_EVENT_TABLE()
133
134 // ----------------------------------------------------------------------------
135 // StaticWidgetsPage
136 // ----------------------------------------------------------------------------
137
138 class StaticWidgetsPage : public WidgetsPage
139 {
140 public:
141 StaticWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
142 virtual ~StaticWidgetsPage();
143
144 protected:
145 // event handlers
146 void OnCheckOrRadioBox(wxCommandEvent& event);
147
148 void OnButtonReset(wxCommandEvent& event);
149 void OnButtonBoxText(wxCommandEvent& event);
150 void OnButtonLabelText(wxCommandEvent& event);
151
152 // reset all parameters
153 void Reset();
154
155 // (re)create all controls
156 void CreateStatic();
157
158 // the controls
159 // ------------
160
161 // the check/radio boxes for styles
162 wxCheckBox *m_chkVert,
163 *m_chkAutoResize;
164
165 wxRadioBox *m_radioHAlign,
166 *m_radioVAlign;
167
168 // the controls and the sizer containing them
169 wxStaticBox *m_staticBox;
170 wxStaticBoxSizer *m_sizerStatBox;
171 wxStaticText *m_statText;
172 wxStaticLine *m_statLine;
173 wxSizer *m_sizerStatic;
174
175 // the text entries for command parameters
176 wxTextCtrl *m_textBox,
177 *m_textLabel;
178
179 private:
180 DECLARE_EVENT_TABLE()
181 DECLARE_WIDGETS_PAGE(StaticWidgetsPage)
182 };
183
184 // ----------------------------------------------------------------------------
185 // event tables
186 // ----------------------------------------------------------------------------
187
188 BEGIN_EVENT_TABLE(StaticWidgetsPage, WidgetsPage)
189 EVT_BUTTON(StaticPage_Reset, StaticWidgetsPage::OnButtonReset)
190 EVT_BUTTON(StaticPage_LabelText, StaticWidgetsPage::OnButtonLabelText)
191 EVT_BUTTON(StaticPage_BoxText, StaticWidgetsPage::OnButtonBoxText)
192
193 EVT_CHECKBOX(-1, StaticWidgetsPage::OnCheckOrRadioBox)
194 EVT_RADIOBOX(-1, StaticWidgetsPage::OnCheckOrRadioBox)
195 END_EVENT_TABLE()
196
197 // ============================================================================
198 // implementation
199 // ============================================================================
200
201 IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage, _T("Static"));
202
203 StaticWidgetsPage::StaticWidgetsPage(wxNotebook *notebook,
204 wxImageList *imaglist)
205 : WidgetsPage(notebook)
206 {
207 imaglist->Add(wxBitmap(statbox_xpm));
208
209 // init everything
210 m_chkVert =
211 m_chkAutoResize = (wxCheckBox *)NULL;
212
213 m_radioHAlign =
214 m_radioVAlign = (wxRadioBox *)NULL;
215
216 m_statLine = (wxStaticLine *)NULL;
217 m_statText = (wxStaticText *)NULL;
218
219 m_staticBox = (wxStaticBox *)NULL;
220 m_sizerStatBox = (wxStaticBoxSizer *)NULL;
221 m_sizerStatic = (wxSizer *)NULL;
222
223 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
224
225 // left pane
226 wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set style"));
227
228 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
229
230 m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical line"));
231 m_chkAutoResize = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Fit to text"));
232 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
233
234 static const wxString halign[] =
235 {
236 _T("left"),
237 _T("centre"),
238 _T("right"),
239 };
240
241 static const wxString valign[] =
242 {
243 _T("top"),
244 _T("centre"),
245 _T("bottom"),
246 };
247
248 m_radioHAlign = new wxRadioBox(this, -1, _T("&Horz alignment"),
249 wxDefaultPosition, wxDefaultSize,
250 WXSIZEOF(halign), halign);
251 m_radioVAlign = new wxRadioBox(this, -1, _T("&Vert alignment"),
252 wxDefaultPosition, wxDefaultSize,
253 WXSIZEOF(valign), valign);
254
255 sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
256 sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
257
258 wxButton *btn = new wxButton(this, StaticPage_Reset, _T("&Reset"));
259 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
260
261 // middle pane
262 wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Change labels"));
263 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
264
265 wxSizer *sizerRow;
266
267 sizerRow = CreateSizerWithTextAndButton(StaticPage_BoxText,
268 _T("Change &box label"),
269 -1, &m_textBox);
270 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
271
272 sizerRow = CreateSizerWithTextAndButton(StaticPage_LabelText,
273 _T("Change &text label"),
274 -1, &m_textLabel);
275 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
276
277 m_textBox->SetValue(_T("This is a box"));
278 m_textLabel->SetValue(_T("And this is a label\ninside the box"));
279
280 // right pane
281 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
282 sizerRight->SetMinSize(150, 0);
283 m_sizerStatic = sizerRight;
284
285 CreateStatic();
286
287 // the 3 panes panes compose the window
288 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
289 sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10);
290 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
291
292 // final initializations
293 Reset();
294
295 SetAutoLayout(TRUE);
296 SetSizer(sizerTop);
297
298 sizerTop->Fit(this);
299 }
300
301 StaticWidgetsPage::~StaticWidgetsPage()
302 {
303 }
304
305 // ----------------------------------------------------------------------------
306 // operations
307 // ----------------------------------------------------------------------------
308
309 void StaticWidgetsPage::Reset()
310 {
311 m_chkVert->SetValue(FALSE);
312 m_chkAutoResize->SetValue(TRUE);
313
314 m_radioHAlign->SetSelection(StaticHAlign_Left);
315 m_radioVAlign->SetSelection(StaticVAlign_Top);
316 }
317
318 void StaticWidgetsPage::CreateStatic()
319 {
320 bool isVert = m_chkVert->GetValue();
321
322 if ( m_sizerStatBox )
323 {
324 delete m_staticBox;
325 // delete m_sizerStatBox; -- deleted by Remove()
326 m_sizerStatic->Remove(m_sizerStatBox);
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
381 m_staticBox = new MyStaticBox(this, -1, m_textBox->GetValue(),
382 wxDefaultPosition, wxDefaultSize,
383 flagsBox);
384 m_sizerStatBox = new wxStaticBoxSizer(m_staticBox, isVert ? wxHORIZONTAL
385 : wxVERTICAL);
386
387 m_statText = new MyStaticText(this, -1, m_textLabel->GetValue(),
388 wxDefaultPosition, wxDefaultSize,
389 flagsText);
390
391 m_statLine = new wxStaticLine(this, -1,
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
408 void StaticWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
409 {
410 Reset();
411
412 CreateStatic();
413 }
414
415 void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
416 {
417 CreateStatic();
418 }
419
420 void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent& event)
421 {
422 m_sizerStatBox->GetStaticBox()->SetLabel(m_textBox->GetValue());
423 }
424
425 void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent& event)
426 {
427 m_statText->SetLabel(m_textLabel->GetValue());
428 }
429