]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/gauge.cpp
5b950ee073286c4cac7932ff9811ce5f42b7535f
[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 // 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
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 = 100,
56 GaugePage_Progress,
57 GaugePage_Clear,
58 GaugePage_SetValue,
59 GaugePage_SetRange,
60 GaugePage_CurValueText,
61 GaugePage_ValueText,
62 GaugePage_RangeText,
63 GaugePage_Timer,
64 GaugePage_Gauge
65 };
66
67 // ----------------------------------------------------------------------------
68 // GaugeWidgetsPage
69 // ----------------------------------------------------------------------------
70
71 class GaugeWidgetsPage : public WidgetsPage
72 {
73 public:
74 GaugeWidgetsPage(wxBookCtrlBase *book, wxImageList *imaglist);
75 virtual ~GaugeWidgetsPage();
76
77 virtual wxControl *GetWidget() const { return m_gauge; }
78 virtual void RecreateWidget() { CreateGauge(); }
79
80 protected:
81 // event handlers
82 void OnButtonReset(wxCommandEvent& event);
83 void OnButtonProgress(wxCommandEvent& event);
84 void OnButtonClear(wxCommandEvent& event);
85 void OnButtonSetValue(wxCommandEvent& event);
86 void OnButtonSetRange(wxCommandEvent& event);
87
88 void OnCheckOrRadioBox(wxCommandEvent& event);
89
90 void OnUpdateUIValueButton(wxUpdateUIEvent& event);
91 void OnUpdateUIRangeButton(wxUpdateUIEvent& event);
92 void OnUpdateUIResetButton(wxUpdateUIEvent& event);
93
94 void OnUpdateUICurValueText(wxUpdateUIEvent& event);
95
96 void OnProgressTimer(wxTimerEvent& event);
97
98 // reset the gauge parameters
99 void Reset();
100
101 // (re)create the gauge
102 void CreateGauge();
103
104 // stop the progress timer
105 void StopTimer();
106
107 // the gauge range
108 unsigned long m_range;
109
110 // the controls
111 // ------------
112
113 // the checkboxes for styles
114 wxCheckBox *m_chkVert,
115 *m_chkSmooth;
116
117 // the gauge itself and the sizer it is in
118 wxGauge *m_gauge;
119 wxSizer *m_sizerGauge;
120
121 // the text entries for set value/range
122 wxTextCtrl *m_textValue,
123 *m_textRange;
124
125 // the timer for simulating gauge progress
126 wxTimer *m_timer;
127
128 private:
129 DECLARE_EVENT_TABLE()
130 DECLARE_WIDGETS_PAGE(GaugeWidgetsPage)
131 };
132
133 // ----------------------------------------------------------------------------
134 // event tables
135 // ----------------------------------------------------------------------------
136
137 BEGIN_EVENT_TABLE(GaugeWidgetsPage, WidgetsPage)
138 EVT_BUTTON(GaugePage_Reset, GaugeWidgetsPage::OnButtonReset)
139 EVT_BUTTON(GaugePage_Progress, GaugeWidgetsPage::OnButtonProgress)
140 EVT_BUTTON(GaugePage_Clear, GaugeWidgetsPage::OnButtonClear)
141 EVT_BUTTON(GaugePage_SetValue, GaugeWidgetsPage::OnButtonSetValue)
142 EVT_BUTTON(GaugePage_SetRange, GaugeWidgetsPage::OnButtonSetRange)
143
144 EVT_UPDATE_UI(GaugePage_SetValue, GaugeWidgetsPage::OnUpdateUIValueButton)
145 EVT_UPDATE_UI(GaugePage_SetRange, GaugeWidgetsPage::OnUpdateUIRangeButton)
146 EVT_UPDATE_UI(GaugePage_Reset, GaugeWidgetsPage::OnUpdateUIResetButton)
147
148 EVT_UPDATE_UI(GaugePage_CurValueText, GaugeWidgetsPage::OnUpdateUICurValueText)
149
150 EVT_CHECKBOX(wxID_ANY, GaugeWidgetsPage::OnCheckOrRadioBox)
151 EVT_RADIOBOX(wxID_ANY, GaugeWidgetsPage::OnCheckOrRadioBox)
152
153 EVT_TIMER(GaugePage_Timer, GaugeWidgetsPage::OnProgressTimer)
154 END_EVENT_TABLE()
155
156 // ============================================================================
157 // implementation
158 // ============================================================================
159
160 IMPLEMENT_WIDGETS_PAGE(GaugeWidgetsPage, _T("Gauge"));
161
162 GaugeWidgetsPage::GaugeWidgetsPage(wxBookCtrlBase *book,
163 wxImageList *imaglist)
164 :WidgetsPage(book)
165 {
166 imaglist->Add(wxBitmap(gauge_xpm));
167
168 // init everything
169 m_range = 100;
170
171 m_timer = (wxTimer *)NULL;
172
173 m_chkVert =
174 m_chkSmooth = (wxCheckBox *)NULL;
175
176 m_gauge = (wxGauge *)NULL;
177 m_sizerGauge = (wxSizer *)NULL;
178
179 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
180
181 // left pane
182 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
183
184 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
185
186 m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical"));
187 m_chkSmooth = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Smooth"));
188
189 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
190
191 wxButton *btn = new wxButton(this, GaugePage_Reset, _T("&Reset"));
192 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
193
194 // middle pane
195 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY,
196 _T("&Change gauge value"));
197 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
198
199 wxTextCtrl *text;
200 wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Current value"),
201 GaugePage_CurValueText,
202 &text);
203 text->SetEditable(false);
204
205 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
206
207 sizerRow = CreateSizerWithTextAndButton(GaugePage_SetValue,
208 _T("Set &value"),
209 GaugePage_ValueText,
210 &m_textValue);
211 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
212
213 sizerRow = CreateSizerWithTextAndButton(GaugePage_SetRange,
214 _T("Set &range"),
215 GaugePage_RangeText,
216 &m_textRange);
217 m_textRange->SetValue( wxString::Format(_T("%lu"), m_range) );
218 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
219
220 btn = new wxButton(this, GaugePage_Progress, _T("Simulate &progress"));
221 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
222
223 btn = new wxButton(this, GaugePage_Clear, _T("&Clear"));
224 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
225
226 // right pane
227 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
228 m_gauge = new wxGauge(this, GaugePage_Gauge, m_range);
229 sizerRight->Add(m_gauge, 1, wxCENTRE | wxALL, 5);
230 sizerRight->SetMinSize(150, 0);
231 m_sizerGauge = sizerRight; // save it to modify it later
232
233 // the 3 panes panes compose the window
234 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
235 sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
236 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
237
238 // final initializations
239 Reset();
240
241 SetSizer(sizerTop);
242
243 sizerTop->Fit(this);
244 }
245
246 GaugeWidgetsPage::~GaugeWidgetsPage()
247 {
248 delete m_timer;
249 }
250
251 // ----------------------------------------------------------------------------
252 // operations
253 // ----------------------------------------------------------------------------
254
255 void GaugeWidgetsPage::Reset()
256 {
257 m_chkVert->SetValue(false);
258 m_chkSmooth->SetValue(false);
259 }
260
261 void GaugeWidgetsPage::CreateGauge()
262 {
263 int flags = ms_defaultFlags;
264
265 if ( m_chkVert->GetValue() )
266 flags |= wxGA_VERTICAL;
267 else
268 flags |= wxGA_HORIZONTAL;
269
270 if ( m_chkSmooth->GetValue() )
271 flags |= wxGA_SMOOTH;
272
273 int val = 0;
274 if ( m_gauge )
275 {
276 val = m_gauge->GetValue();
277
278 m_sizerGauge->Detach( m_gauge );
279 delete m_gauge;
280 }
281
282 m_gauge = new wxGauge(this, GaugePage_Gauge, m_range,
283 wxDefaultPosition, wxDefaultSize,
284 flags);
285 m_gauge->SetValue(val);
286
287 if ( flags & wxGA_VERTICAL )
288 m_sizerGauge->Add(m_gauge, 0, wxGROW | wxALL, 5);
289 else
290 m_sizerGauge->Add(m_gauge, 1, wxCENTRE | wxALL, 5);
291
292 m_sizerGauge->Layout();
293 }
294
295 // ----------------------------------------------------------------------------
296 // event handlers
297 // ----------------------------------------------------------------------------
298
299 void GaugeWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
300 {
301 Reset();
302
303 CreateGauge();
304 }
305
306 void GaugeWidgetsPage::OnButtonProgress(wxCommandEvent& event)
307 {
308 if ( !m_timer )
309 {
310 static const int INTERVAL = 300;
311
312 wxLogMessage(_T("Launched progress timer (interval = %d ms)"), INTERVAL);
313
314 m_timer = new wxTimer(this, GaugePage_Timer);
315 m_timer->Start(INTERVAL);
316
317 wxButton *btn = (wxButton *)event.GetEventObject();
318 btn->SetLabel(_T("&Stop timer"));
319 }
320 else // stop the running timer
321 {
322 StopTimer();
323
324 wxLogMessage(_T("Stopped the timer."));
325 }
326 }
327
328 void GaugeWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event))
329 {
330 m_gauge->SetValue(0);
331 }
332
333 void GaugeWidgetsPage::OnButtonSetRange(wxCommandEvent& WXUNUSED(event))
334 {
335 unsigned long val;
336 if ( !m_textRange->GetValue().ToULong(&val) )
337 return;
338
339 m_range = val;
340 m_gauge->SetRange(val);
341 }
342
343 void GaugeWidgetsPage::OnButtonSetValue(wxCommandEvent& WXUNUSED(event))
344 {
345 unsigned long val;
346 if ( !m_textValue->GetValue().ToULong(&val) )
347 return;
348
349 m_gauge->SetValue(val);
350 }
351
352 void GaugeWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent& event)
353 {
354 unsigned long val;
355 event.Enable( m_textValue->GetValue().ToULong(&val) && (val <= m_range) );
356 }
357
358 void GaugeWidgetsPage::OnUpdateUIRangeButton(wxUpdateUIEvent& event)
359 {
360 unsigned long val;
361 event.Enable( m_textRange->GetValue().ToULong(&val) );
362 }
363
364 void GaugeWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
365 {
366 event.Enable( m_chkVert->GetValue() || m_chkSmooth->GetValue() );
367 }
368
369 void GaugeWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
370 {
371 CreateGauge();
372 }
373
374 void GaugeWidgetsPage::OnProgressTimer(wxTimerEvent& WXUNUSED(event))
375 {
376 int val = m_gauge->GetValue();
377 if ( (unsigned)val < m_range )
378 {
379 m_gauge->SetValue(val + 1);
380 }
381 else // reached the end
382 {
383 StopTimer();
384 }
385 }
386
387 void GaugeWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent& event)
388 {
389 event.SetText( wxString::Format(_T("%d"), m_gauge->GetValue()));
390 }
391
392 void GaugeWidgetsPage::StopTimer()
393 {
394 wxCHECK_RET( m_timer, _T("shouldn't be called") );
395
396 m_timer->Stop();
397 delete m_timer;
398 m_timer = NULL;
399
400 wxButton *btn = (wxButton *)FindWindow(GaugePage_Progress);
401 wxCHECK_RET( btn, _T("no progress button?") );
402
403 btn->SetLabel(_T("Simulate &progress"));
404
405 wxLogMessage(_T("Progress finished."));
406 }
407
408 #endif
409 // wxUSE_GAUGE