]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/gauge.cpp
Fix wrong configure test for abi::__forced_unwind in previous commit.
[wxWidgets.git] / samples / widgets / gauge.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: gauge.cpp
4 // Purpose: Part of the widgets sample showing wxGauge
5 // Author: Vadim Zeitlin
6 // Created: 27.03.01
7 // Id: $Id$
8 // Copyright: (c) 2001 Vadim Zeitlin
9 // Licence: wxWindows licence
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 #include "wx/timer.h"
31
32 #include "wx/bitmap.h"
33 #include "wx/button.h"
34 #include "wx/checkbox.h"
35 #include "wx/combobox.h"
36 #include "wx/gauge.h"
37 #include "wx/radiobox.h"
38 #include "wx/statbox.h"
39 #include "wx/textctrl.h"
40 #endif
41
42 #include "wx/sizer.h"
43
44 #include "widgets.h"
45 #if wxUSE_GAUGE
46 #include "icons/gauge.xpm"
47
48 // ----------------------------------------------------------------------------
49 // constants
50 // ----------------------------------------------------------------------------
51
52 // control ids
53 enum
54 {
55 GaugePage_Reset = wxID_HIGHEST,
56 GaugePage_Progress,
57 GaugePage_IndeterminateProgress,
58 GaugePage_Clear,
59 GaugePage_SetValue,
60 GaugePage_SetRange,
61 GaugePage_CurValueText,
62 GaugePage_ValueText,
63 GaugePage_RangeText,
64 GaugePage_Timer,
65 GaugePage_IndeterminateTimer,
66 GaugePage_Gauge
67 };
68
69 // ----------------------------------------------------------------------------
70 // GaugeWidgetsPage
71 // ----------------------------------------------------------------------------
72
73 class GaugeWidgetsPage : public WidgetsPage
74 {
75 public:
76 GaugeWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
77 virtual ~GaugeWidgetsPage();
78
79 virtual wxControl *GetWidget() const { return m_gauge; }
80 virtual void RecreateWidget() { CreateGauge(); }
81
82 // lazy creation of the content
83 virtual void CreateContent();
84
85 protected:
86 // event handlers
87 void OnButtonReset(wxCommandEvent& event);
88 void OnButtonProgress(wxCommandEvent& event);
89 void OnButtonIndeterminateProgress(wxCommandEvent& event);
90 void OnButtonClear(wxCommandEvent& event);
91 void OnButtonSetValue(wxCommandEvent& event);
92 void OnButtonSetRange(wxCommandEvent& event);
93
94 void OnCheckOrRadioBox(wxCommandEvent& event);
95
96 void OnUpdateUIValueButton(wxUpdateUIEvent& event);
97 void OnUpdateUIRangeButton(wxUpdateUIEvent& event);
98 void OnUpdateUIResetButton(wxUpdateUIEvent& event);
99
100 void OnUpdateUICurValueText(wxUpdateUIEvent& event);
101
102 void OnProgressTimer(wxTimerEvent& event);
103 void OnIndeterminateProgressTimer(wxTimerEvent& event);
104
105 // reset the gauge parameters
106 void Reset();
107
108 // (re)create the gauge
109 void CreateGauge();
110
111 // start progress timer
112 void StartTimer(wxButton*);
113
114 // stop the progress timer
115 void StopTimer(wxButton*);
116
117 // the gauge range
118 unsigned long m_range;
119
120 // the controls
121 // ------------
122
123 // the checkboxes for styles
124 wxCheckBox *m_chkVert,
125 *m_chkSmooth;
126
127 // the gauge itself and the sizer it is in
128 wxGauge *m_gauge;
129 wxSizer *m_sizerGauge;
130
131 // the text entries for set value/range
132 wxTextCtrl *m_textValue,
133 *m_textRange;
134
135 // the timer for simulating gauge progress
136 wxTimer *m_timer;
137
138 private:
139 DECLARE_EVENT_TABLE()
140 DECLARE_WIDGETS_PAGE(GaugeWidgetsPage)
141 };
142
143 // ----------------------------------------------------------------------------
144 // event tables
145 // ----------------------------------------------------------------------------
146
147 BEGIN_EVENT_TABLE(GaugeWidgetsPage, WidgetsPage)
148 EVT_BUTTON(GaugePage_Reset, GaugeWidgetsPage::OnButtonReset)
149 EVT_BUTTON(GaugePage_Progress, GaugeWidgetsPage::OnButtonProgress)
150 EVT_BUTTON(GaugePage_IndeterminateProgress, GaugeWidgetsPage::OnButtonIndeterminateProgress)
151 EVT_BUTTON(GaugePage_Clear, GaugeWidgetsPage::OnButtonClear)
152 EVT_BUTTON(GaugePage_SetValue, GaugeWidgetsPage::OnButtonSetValue)
153 EVT_BUTTON(GaugePage_SetRange, GaugeWidgetsPage::OnButtonSetRange)
154
155 EVT_UPDATE_UI(GaugePage_SetValue, GaugeWidgetsPage::OnUpdateUIValueButton)
156 EVT_UPDATE_UI(GaugePage_SetRange, GaugeWidgetsPage::OnUpdateUIRangeButton)
157 EVT_UPDATE_UI(GaugePage_Reset, GaugeWidgetsPage::OnUpdateUIResetButton)
158
159 EVT_UPDATE_UI(GaugePage_CurValueText, GaugeWidgetsPage::OnUpdateUICurValueText)
160
161 EVT_CHECKBOX(wxID_ANY, GaugeWidgetsPage::OnCheckOrRadioBox)
162 EVT_RADIOBOX(wxID_ANY, GaugeWidgetsPage::OnCheckOrRadioBox)
163
164 EVT_TIMER(GaugePage_Timer, GaugeWidgetsPage::OnProgressTimer)
165 EVT_TIMER(GaugePage_IndeterminateTimer, GaugeWidgetsPage::OnIndeterminateProgressTimer)
166 END_EVENT_TABLE()
167
168 // ============================================================================
169 // implementation
170 // ============================================================================
171
172 #if defined(__WXUNIVERSAL__)
173 #define FAMILY_CTRLS UNIVERSAL_CTRLS
174 #else
175 #define FAMILY_CTRLS NATIVE_CTRLS
176 #endif
177
178 IMPLEMENT_WIDGETS_PAGE(GaugeWidgetsPage, wxT("Gauge"), FAMILY_CTRLS );
179
180 GaugeWidgetsPage::GaugeWidgetsPage(WidgetsBookCtrl *book,
181 wxImageList *imaglist)
182 :WidgetsPage(book, imaglist, gauge_xpm)
183 {
184 // init everything
185 m_range = 100;
186
187 m_timer = (wxTimer *)NULL;
188
189 m_chkVert =
190 m_chkSmooth = (wxCheckBox *)NULL;
191
192 m_gauge = (wxGauge *)NULL;
193 m_sizerGauge = (wxSizer *)NULL;
194 }
195
196 void GaugeWidgetsPage::CreateContent()
197 {
198 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
199
200 // left pane
201 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("&Set style"));
202
203 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
204
205 m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Vertical"));
206 m_chkSmooth = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Smooth"));
207
208 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
209
210 wxButton *btn = new wxButton(this, GaugePage_Reset, wxT("&Reset"));
211 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
212
213 // middle pane
214 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, wxT("&Change gauge value"));
215 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
216
217 wxTextCtrl *text;
218 wxSizer *sizerRow = CreateSizerWithTextAndLabel(wxT("Current value"),
219 GaugePage_CurValueText,
220 &text);
221 text->SetEditable(false);
222
223 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
224
225 sizerRow = CreateSizerWithTextAndButton(GaugePage_SetValue,
226 wxT("Set &value"),
227 GaugePage_ValueText,
228 &m_textValue);
229 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
230
231 sizerRow = CreateSizerWithTextAndButton(GaugePage_SetRange,
232 wxT("Set &range"),
233 GaugePage_RangeText,
234 &m_textRange);
235 m_textRange->SetValue( wxString::Format(wxT("%lu"), m_range) );
236 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
237
238 btn = new wxButton(this, GaugePage_Progress, wxT("Simulate &progress"));
239 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
240
241 btn = new wxButton(this, GaugePage_IndeterminateProgress,
242 wxT("Simulate &indeterminate job"));
243 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
244
245 btn = new wxButton(this, GaugePage_Clear, wxT("&Clear"));
246 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
247
248 // right pane
249 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
250 m_gauge = new wxGauge(this, GaugePage_Gauge, m_range);
251 sizerRight->Add(m_gauge, 1, wxCENTRE | wxALL, 5);
252 sizerRight->SetMinSize(150, 0);
253 m_sizerGauge = sizerRight; // save it to modify it later
254
255 // the 3 panes panes compose the window
256 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
257 sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
258 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
259
260 // final initializations
261 Reset();
262
263 SetSizer(sizerTop);
264 }
265
266 GaugeWidgetsPage::~GaugeWidgetsPage()
267 {
268 delete m_timer;
269 }
270
271 // ----------------------------------------------------------------------------
272 // operations
273 // ----------------------------------------------------------------------------
274
275 void GaugeWidgetsPage::Reset()
276 {
277 m_chkVert->SetValue(false);
278 m_chkSmooth->SetValue(false);
279 }
280
281 void GaugeWidgetsPage::CreateGauge()
282 {
283 int flags = ms_defaultFlags;
284
285 if ( m_chkVert->GetValue() )
286 flags |= wxGA_VERTICAL;
287 else
288 flags |= wxGA_HORIZONTAL;
289
290 if ( m_chkSmooth->GetValue() )
291 flags |= wxGA_SMOOTH;
292
293 int val = 0;
294 if ( m_gauge )
295 {
296 val = m_gauge->GetValue();
297
298 m_sizerGauge->Detach( m_gauge );
299 delete m_gauge;
300 }
301
302 m_gauge = new wxGauge(this, GaugePage_Gauge, m_range,
303 wxDefaultPosition, wxDefaultSize,
304 flags);
305 m_gauge->SetValue(val);
306
307 if ( flags & wxGA_VERTICAL )
308 m_sizerGauge->Add(m_gauge, 0, wxGROW | wxALL, 5);
309 else
310 m_sizerGauge->Add(m_gauge, 1, wxCENTRE | wxALL, 5);
311
312 m_sizerGauge->Layout();
313 }
314
315 void GaugeWidgetsPage::StartTimer(wxButton *clicked)
316 {
317 static const int INTERVAL = 300;
318
319 wxLogMessage(wxT("Launched progress timer (interval = %d ms)"), INTERVAL);
320
321 m_timer = new wxTimer(this,
322 clicked->GetId() == GaugePage_Progress ? GaugePage_Timer : GaugePage_IndeterminateTimer);
323 m_timer->Start(INTERVAL);
324
325 clicked->SetLabel(wxT("&Stop timer"));
326
327 if (clicked->GetId() == GaugePage_Progress)
328 FindWindow(GaugePage_IndeterminateProgress)->Disable();
329 else
330 FindWindow(GaugePage_Progress)->Disable();
331 }
332
333 void GaugeWidgetsPage::StopTimer(wxButton *clicked)
334 {
335 wxCHECK_RET( m_timer, wxT("shouldn't be called") );
336
337 m_timer->Stop();
338 wxDELETE(m_timer);
339
340 if (clicked->GetId() == GaugePage_Progress)
341 {
342 clicked->SetLabel(wxT("Simulate &progress"));
343 FindWindow(GaugePage_IndeterminateProgress)->Enable();
344 }
345 else
346 {
347 clicked->SetLabel(wxT("Simulate indeterminate job"));
348 FindWindow(GaugePage_Progress)->Enable();
349 }
350
351 wxLogMessage(wxT("Progress finished."));
352 }
353
354 // ----------------------------------------------------------------------------
355 // event handlers
356 // ----------------------------------------------------------------------------
357
358 void GaugeWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
359 {
360 Reset();
361
362 CreateGauge();
363 }
364
365 void GaugeWidgetsPage::OnButtonProgress(wxCommandEvent& event)
366 {
367 wxButton *b = (wxButton *)event.GetEventObject();
368 if ( !m_timer )
369 {
370 StartTimer(b);
371 }
372 else // stop the running timer
373 {
374 StopTimer(b);
375
376 wxLogMessage(wxT("Stopped the timer."));
377 }
378 }
379
380 void GaugeWidgetsPage::OnButtonIndeterminateProgress(wxCommandEvent& event)
381 {
382 wxButton *b = (wxButton *)event.GetEventObject();
383 if ( !m_timer )
384 {
385 StartTimer(b);
386 }
387 else // stop the running timer
388 {
389 StopTimer(b);
390
391 m_gauge->SetValue(0);
392
393 wxLogMessage(wxT("Stopped the timer."));
394 }
395 }
396
397 void GaugeWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event))
398 {
399 m_gauge->SetValue(0);
400 }
401
402 void GaugeWidgetsPage::OnButtonSetRange(wxCommandEvent& WXUNUSED(event))
403 {
404 unsigned long val;
405 if ( !m_textRange->GetValue().ToULong(&val) )
406 return;
407
408 m_range = val;
409 m_gauge->SetRange(val);
410 }
411
412 void GaugeWidgetsPage::OnButtonSetValue(wxCommandEvent& WXUNUSED(event))
413 {
414 unsigned long val;
415 if ( !m_textValue->GetValue().ToULong(&val) )
416 return;
417
418 m_gauge->SetValue(val);
419 }
420
421 void GaugeWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent& event)
422 {
423 unsigned long val;
424 event.Enable( m_textValue->GetValue().ToULong(&val) && (val <= m_range) );
425 }
426
427 void GaugeWidgetsPage::OnUpdateUIRangeButton(wxUpdateUIEvent& event)
428 {
429 unsigned long val;
430 event.Enable( m_textRange->GetValue().ToULong(&val) );
431 }
432
433 void GaugeWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
434 {
435 event.Enable( m_chkVert->GetValue() || m_chkSmooth->GetValue() );
436 }
437
438 void GaugeWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
439 {
440 CreateGauge();
441 }
442
443 void GaugeWidgetsPage::OnProgressTimer(wxTimerEvent& WXUNUSED(event))
444 {
445 int val = m_gauge->GetValue();
446 if ( (unsigned)val < m_range )
447 {
448 m_gauge->SetValue(val + 1);
449 }
450 else // reached the end
451 {
452 wxButton *btn = (wxButton *)FindWindow(GaugePage_Progress);
453 wxCHECK_RET( btn, wxT("no progress button?") );
454
455 StopTimer(btn);
456 }
457 }
458
459 void GaugeWidgetsPage::OnIndeterminateProgressTimer(wxTimerEvent& WXUNUSED(event))
460 {
461 m_gauge->Pulse();
462 }
463
464 void GaugeWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent& event)
465 {
466 event.SetText( wxString::Format(wxT("%d"), m_gauge->GetValue()));
467 }
468
469 #endif
470 // wxUSE_GAUGE