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