]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/gauge.cpp
Forgot to remove wxBackingFile from the changes.txt. It was kept internal
[wxWidgets.git] / samples / widgets / gauge.cpp
CommitLineData
32b8ec41 1/////////////////////////////////////////////////////////////////////////////
be5a51fb 2// Program: wxWidgets Widgets Sample
32b8ec41
VZ
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// 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 #include "wx/timer.h"
31
3bb70c40 32 #include "wx/bitmap.h"
32b8ec41
VZ
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"
8dfc2d98 45#if wxUSE_GAUGE
32b8ec41
VZ
46#include "icons/gauge.xpm"
47
48// ----------------------------------------------------------------------------
49// constants
50// ----------------------------------------------------------------------------
51
52// control ids
53enum
54{
f0fa4312 55 GaugePage_Reset = wxID_HIGHEST,
32b8ec41 56 GaugePage_Progress,
5c35d5c1 57 GaugePage_IndeterminateProgress,
32b8ec41
VZ
58 GaugePage_Clear,
59 GaugePage_SetValue,
60 GaugePage_SetRange,
61 GaugePage_CurValueText,
62 GaugePage_ValueText,
63 GaugePage_RangeText,
64 GaugePage_Timer,
5c35d5c1 65 GaugePage_IndeterminateTimer,
32b8ec41
VZ
66 GaugePage_Gauge
67};
68
69// ----------------------------------------------------------------------------
70// GaugeWidgetsPage
71// ----------------------------------------------------------------------------
72
73class GaugeWidgetsPage : public WidgetsPage
74{
75public:
f2fdc4d5 76 GaugeWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
32b8ec41
VZ
77 virtual ~GaugeWidgetsPage();
78
195df7a7 79 virtual wxControl *GetWidget() const { return m_gauge; }
1301e228 80 virtual void RecreateWidget() { CreateGauge(); }
195df7a7 81
453535a7
WS
82 // lazy creation of the content
83 virtual void CreateContent();
84
32b8ec41
VZ
85protected:
86 // event handlers
87 void OnButtonReset(wxCommandEvent& event);
88 void OnButtonProgress(wxCommandEvent& event);
5c35d5c1 89 void OnButtonIndeterminateProgress(wxCommandEvent& event);
32b8ec41
VZ
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);
5c35d5c1 103 void OnIndeterminateProgressTimer(wxTimerEvent& event);
32b8ec41
VZ
104
105 // reset the gauge parameters
106 void Reset();
107
108 // (re)create the gauge
109 void CreateGauge();
110
5c35d5c1
VZ
111 // start progress timer
112 void StartTimer(wxButton*);
113
32b8ec41 114 // stop the progress timer
5c35d5c1 115 void StopTimer(wxButton*);
32b8ec41
VZ
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,
5c35d5c1 125 *m_chkSmooth;
32b8ec41
VZ
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
138private:
5e173f35
GD
139 DECLARE_EVENT_TABLE()
140 DECLARE_WIDGETS_PAGE(GaugeWidgetsPage)
32b8ec41
VZ
141};
142
143// ----------------------------------------------------------------------------
144// event tables
145// ----------------------------------------------------------------------------
146
147BEGIN_EVENT_TABLE(GaugeWidgetsPage, WidgetsPage)
148 EVT_BUTTON(GaugePage_Reset, GaugeWidgetsPage::OnButtonReset)
149 EVT_BUTTON(GaugePage_Progress, GaugeWidgetsPage::OnButtonProgress)
5c35d5c1 150 EVT_BUTTON(GaugePage_IndeterminateProgress, GaugeWidgetsPage::OnButtonIndeterminateProgress)
32b8ec41
VZ
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
206d3a16
JS
161 EVT_CHECKBOX(wxID_ANY, GaugeWidgetsPage::OnCheckOrRadioBox)
162 EVT_RADIOBOX(wxID_ANY, GaugeWidgetsPage::OnCheckOrRadioBox)
32b8ec41
VZ
163
164 EVT_TIMER(GaugePage_Timer, GaugeWidgetsPage::OnProgressTimer)
5c35d5c1 165 EVT_TIMER(GaugePage_IndeterminateTimer, GaugeWidgetsPage::OnIndeterminateProgressTimer)
32b8ec41
VZ
166END_EVENT_TABLE()
167
168// ============================================================================
169// implementation
170// ============================================================================
171
f0fa4312
WS
172#if defined(__WXUNIVERSAL__)
173 #define FAMILY_CTRLS UNIVERSAL_CTRLS
174#else
175 #define FAMILY_CTRLS NATIVE_CTRLS
176#endif
177
178IMPLEMENT_WIDGETS_PAGE(GaugeWidgetsPage, _T("Gauge"), FAMILY_CTRLS );
32b8ec41 179
f2fdc4d5 180GaugeWidgetsPage::GaugeWidgetsPage(WidgetsBookCtrl *book,
61c083e7 181 wxImageList *imaglist)
261357eb 182 :WidgetsPage(book, imaglist, gauge_xpm)
32b8ec41 183{
32b8ec41
VZ
184 // init everything
185 m_range = 100;
186
187 m_timer = (wxTimer *)NULL;
188
189 m_chkVert =
5c35d5c1 190 m_chkSmooth = (wxCheckBox *)NULL;
32b8ec41
VZ
191
192 m_gauge = (wxGauge *)NULL;
193 m_sizerGauge = (wxSizer *)NULL;
453535a7 194}
32b8ec41 195
453535a7
WS
196void GaugeWidgetsPage::CreateContent()
197{
32b8ec41
VZ
198 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
199
200 // left pane
206d3a16 201 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
32b8ec41
VZ
202
203 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
204
205 m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical"));
206 m_chkSmooth = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Smooth"));
207
208 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
209
210 wxButton *btn = new wxButton(this, GaugePage_Reset, _T("&Reset"));
211 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
212
213 // middle pane
206d3a16
JS
214 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY,
215 _T("&Change gauge value"));
32b8ec41
VZ
216 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
217
218 wxTextCtrl *text;
219 wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Current value"),
220 GaugePage_CurValueText,
221 &text);
206d3a16 222 text->SetEditable(false);
32b8ec41
VZ
223
224 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
225
226 sizerRow = CreateSizerWithTextAndButton(GaugePage_SetValue,
227 _T("Set &value"),
228 GaugePage_ValueText,
229 &m_textValue);
230 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
231
232 sizerRow = CreateSizerWithTextAndButton(GaugePage_SetRange,
233 _T("Set &range"),
234 GaugePage_RangeText,
235 &m_textRange);
aec18ff7 236 m_textRange->SetValue( wxString::Format(_T("%lu"), m_range) );
32b8ec41
VZ
237 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
238
239 btn = new wxButton(this, GaugePage_Progress, _T("Simulate &progress"));
240 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
241
5c35d5c1
VZ
242 btn = new wxButton(this, GaugePage_IndeterminateProgress, _T("Simulate undeterminate job"));
243 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
244
32b8ec41
VZ
245 btn = new wxButton(this, GaugePage_Clear, _T("&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);
7b127900 252 sizerRight->SetMinSize(150, 0);
32b8ec41
VZ
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
32b8ec41
VZ
263 SetSizer(sizerTop);
264
265 sizerTop->Fit(this);
266}
267
268GaugeWidgetsPage::~GaugeWidgetsPage()
269{
270 delete m_timer;
271}
272
273// ----------------------------------------------------------------------------
274// operations
275// ----------------------------------------------------------------------------
276
277void GaugeWidgetsPage::Reset()
278{
206d3a16
JS
279 m_chkVert->SetValue(false);
280 m_chkSmooth->SetValue(false);
32b8ec41
VZ
281}
282
283void GaugeWidgetsPage::CreateGauge()
284{
1301e228 285 int flags = ms_defaultFlags;
32b8ec41
VZ
286
287 if ( m_chkVert->GetValue() )
288 flags |= wxGA_VERTICAL;
289 else
290 flags |= wxGA_HORIZONTAL;
291
292 if ( m_chkSmooth->GetValue() )
293 flags |= wxGA_SMOOTH;
294
295 int val = 0;
296 if ( m_gauge )
297 {
298 val = m_gauge->GetValue();
299
12a3f227 300 m_sizerGauge->Detach( m_gauge );
32b8ec41
VZ
301 delete m_gauge;
302 }
303
304 m_gauge = new wxGauge(this, GaugePage_Gauge, m_range,
305 wxDefaultPosition, wxDefaultSize,
306 flags);
307 m_gauge->SetValue(val);
ef78ec37 308
32b8ec41
VZ
309 if ( flags & wxGA_VERTICAL )
310 m_sizerGauge->Add(m_gauge, 0, wxGROW | wxALL, 5);
311 else
312 m_sizerGauge->Add(m_gauge, 1, wxCENTRE | wxALL, 5);
313
314 m_sizerGauge->Layout();
315}
316
5c35d5c1
VZ
317void GaugeWidgetsPage::StartTimer(wxButton *clicked)
318{
319 static const int INTERVAL = 300;
320
321 wxLogMessage(_T("Launched progress timer (interval = %d ms)"), INTERVAL);
322
323 m_timer = new wxTimer(this,
324 clicked->GetId() == GaugePage_Progress ? GaugePage_Timer : GaugePage_IndeterminateTimer);
325 m_timer->Start(INTERVAL);
326
327 clicked->SetLabel(_T("&Stop timer"));
328
329 if (clicked->GetId() == GaugePage_Progress)
330 FindWindow(GaugePage_IndeterminateProgress)->Disable();
331 else
332 FindWindow(GaugePage_Progress)->Disable();
333}
334
335void GaugeWidgetsPage::StopTimer(wxButton *clicked)
336{
337 wxCHECK_RET( m_timer, _T("shouldn't be called") );
338
339 m_timer->Stop();
340 delete m_timer;
341 m_timer = NULL;
342
343 if (clicked->GetId() == GaugePage_Progress)
344 {
345 clicked->SetLabel(_T("Simulate &progress"));
346 FindWindow(GaugePage_IndeterminateProgress)->Enable();
347 }
348 else
349 {
350 clicked->SetLabel(_T("Simulate undeterminate job"));
351 FindWindow(GaugePage_Progress)->Enable();
352 }
353
354 wxLogMessage(_T("Progress finished."));
355}
356
32b8ec41
VZ
357// ----------------------------------------------------------------------------
358// event handlers
359// ----------------------------------------------------------------------------
360
361void GaugeWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
362{
363 Reset();
364
365 CreateGauge();
366}
367
368void GaugeWidgetsPage::OnButtonProgress(wxCommandEvent& event)
369{
5c35d5c1 370 wxButton *b = (wxButton *)event.GetEventObject();
32b8ec41
VZ
371 if ( !m_timer )
372 {
5c35d5c1
VZ
373 StartTimer(b);
374 }
375 else // stop the running timer
376 {
377 StopTimer(b);
32b8ec41 378
5c35d5c1
VZ
379 wxLogMessage(_T("Stopped the timer."));
380 }
381}
32b8ec41 382
5c35d5c1
VZ
383void GaugeWidgetsPage::OnButtonIndeterminateProgress(wxCommandEvent& event)
384{
385 wxButton *b = (wxButton *)event.GetEventObject();
386 if ( !m_timer )
387 {
388 StartTimer(b);
32b8ec41
VZ
389 }
390 else // stop the running timer
391 {
5c35d5c1 392 StopTimer(b);
32b8ec41
VZ
393
394 wxLogMessage(_T("Stopped the timer."));
395 }
396}
397
398void GaugeWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event))
399{
400 m_gauge->SetValue(0);
401}
402
403void GaugeWidgetsPage::OnButtonSetRange(wxCommandEvent& WXUNUSED(event))
404{
405 unsigned long val;
406 if ( !m_textRange->GetValue().ToULong(&val) )
407 return;
408
efb349e4 409 m_range = val;
32b8ec41
VZ
410 m_gauge->SetRange(val);
411}
412
413void GaugeWidgetsPage::OnButtonSetValue(wxCommandEvent& WXUNUSED(event))
414{
415 unsigned long val;
416 if ( !m_textValue->GetValue().ToULong(&val) )
417 return;
418
419 m_gauge->SetValue(val);
420}
421
422void GaugeWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent& event)
423{
424 unsigned long val;
425 event.Enable( m_textValue->GetValue().ToULong(&val) && (val <= m_range) );
426}
427
428void GaugeWidgetsPage::OnUpdateUIRangeButton(wxUpdateUIEvent& event)
429{
430 unsigned long val;
431 event.Enable( m_textRange->GetValue().ToULong(&val) );
432}
433
434void GaugeWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
435{
436 event.Enable( m_chkVert->GetValue() || m_chkSmooth->GetValue() );
437}
438
c02e5a31 439void GaugeWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
440{
441 CreateGauge();
442}
443
444void GaugeWidgetsPage::OnProgressTimer(wxTimerEvent& WXUNUSED(event))
445{
446 int val = m_gauge->GetValue();
447 if ( (unsigned)val < m_range )
448 {
449 m_gauge->SetValue(val + 1);
450 }
451 else // reached the end
452 {
5c35d5c1
VZ
453 wxButton *btn = (wxButton *)FindWindow(GaugePage_Progress);
454 wxCHECK_RET( btn, _T("no progress button?") );
455
456 StopTimer(btn);
32b8ec41
VZ
457 }
458}
459
5c35d5c1 460void GaugeWidgetsPage::OnIndeterminateProgressTimer(wxTimerEvent& WXUNUSED(event))
32b8ec41 461{
5c35d5c1 462 m_gauge->Pulse();
32b8ec41
VZ
463}
464
5c35d5c1 465void GaugeWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent& event)
32b8ec41 466{
5c35d5c1 467 event.SetText( wxString::Format(_T("%d"), m_gauge->GetValue()));
32b8ec41
VZ
468}
469
aec18ff7 470#endif
8dfc2d98 471 // wxUSE_GAUGE