]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/gauge.cpp
5c779721cbcbc7b1cd0c39e69e118fb09ad8f88e
[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(WidgetsBookCtrl *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 (int)wxPlatform(GENERIC_CTRLS).If(wxMSW,NATIVE_CTRLS)
162 );
163
164 GaugeWidgetsPage::GaugeWidgetsPage(WidgetsBookCtrl *book,
165 wxImageList *imaglist)
166 :WidgetsPage(book)
167 {
168 imaglist->Add(wxBitmap(gauge_xpm));
169
170 // init everything
171 m_range = 100;
172
173 m_timer = (wxTimer *)NULL;
174
175 m_chkVert =
176 m_chkSmooth = (wxCheckBox *)NULL;
177
178 m_gauge = (wxGauge *)NULL;
179 m_sizerGauge = (wxSizer *)NULL;
180
181 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
182
183 // left pane
184 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
185
186 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
187
188 m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical"));
189 m_chkSmooth = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Smooth"));
190
191 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
192
193 wxButton *btn = new wxButton(this, GaugePage_Reset, _T("&Reset"));
194 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
195
196 // middle pane
197 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY,
198 _T("&Change gauge value"));
199 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
200
201 wxTextCtrl *text;
202 wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Current value"),
203 GaugePage_CurValueText,
204 &text);
205 text->SetEditable(false);
206
207 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
208
209 sizerRow = CreateSizerWithTextAndButton(GaugePage_SetValue,
210 _T("Set &value"),
211 GaugePage_ValueText,
212 &m_textValue);
213 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
214
215 sizerRow = CreateSizerWithTextAndButton(GaugePage_SetRange,
216 _T("Set &range"),
217 GaugePage_RangeText,
218 &m_textRange);
219 m_textRange->SetValue( wxString::Format(_T("%lu"), m_range) );
220 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
221
222 btn = new wxButton(this, GaugePage_Progress, _T("Simulate &progress"));
223 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
224
225 btn = new wxButton(this, GaugePage_Clear, _T("&Clear"));
226 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
227
228 // right pane
229 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
230 m_gauge = new wxGauge(this, GaugePage_Gauge, m_range);
231 sizerRight->Add(m_gauge, 1, wxCENTRE | wxALL, 5);
232 sizerRight->SetMinSize(150, 0);
233 m_sizerGauge = sizerRight; // save it to modify it later
234
235 // the 3 panes panes compose the window
236 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
237 sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
238 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
239
240 // final initializations
241 Reset();
242
243 SetSizer(sizerTop);
244
245 sizerTop->Fit(this);
246 }
247
248 GaugeWidgetsPage::~GaugeWidgetsPage()
249 {
250 delete m_timer;
251 }
252
253 // ----------------------------------------------------------------------------
254 // operations
255 // ----------------------------------------------------------------------------
256
257 void GaugeWidgetsPage::Reset()
258 {
259 m_chkVert->SetValue(false);
260 m_chkSmooth->SetValue(false);
261 }
262
263 void GaugeWidgetsPage::CreateGauge()
264 {
265 int flags = ms_defaultFlags;
266
267 if ( m_chkVert->GetValue() )
268 flags |= wxGA_VERTICAL;
269 else
270 flags |= wxGA_HORIZONTAL;
271
272 if ( m_chkSmooth->GetValue() )
273 flags |= wxGA_SMOOTH;
274
275 int val = 0;
276 if ( m_gauge )
277 {
278 val = m_gauge->GetValue();
279
280 m_sizerGauge->Detach( m_gauge );
281 delete m_gauge;
282 }
283
284 m_gauge = new wxGauge(this, GaugePage_Gauge, m_range,
285 wxDefaultPosition, wxDefaultSize,
286 flags);
287 m_gauge->SetValue(val);
288
289 if ( flags & wxGA_VERTICAL )
290 m_sizerGauge->Add(m_gauge, 0, wxGROW | wxALL, 5);
291 else
292 m_sizerGauge->Add(m_gauge, 1, wxCENTRE | wxALL, 5);
293
294 m_sizerGauge->Layout();
295 }
296
297 // ----------------------------------------------------------------------------
298 // event handlers
299 // ----------------------------------------------------------------------------
300
301 void GaugeWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
302 {
303 Reset();
304
305 CreateGauge();
306 }
307
308 void GaugeWidgetsPage::OnButtonProgress(wxCommandEvent& event)
309 {
310 if ( !m_timer )
311 {
312 static const int INTERVAL = 300;
313
314 wxLogMessage(_T("Launched progress timer (interval = %d ms)"), INTERVAL);
315
316 m_timer = new wxTimer(this, GaugePage_Timer);
317 m_timer->Start(INTERVAL);
318
319 wxButton *btn = (wxButton *)event.GetEventObject();
320 btn->SetLabel(_T("&Stop timer"));
321 }
322 else // stop the running timer
323 {
324 StopTimer();
325
326 wxLogMessage(_T("Stopped the timer."));
327 }
328 }
329
330 void GaugeWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event))
331 {
332 m_gauge->SetValue(0);
333 }
334
335 void GaugeWidgetsPage::OnButtonSetRange(wxCommandEvent& WXUNUSED(event))
336 {
337 unsigned long val;
338 if ( !m_textRange->GetValue().ToULong(&val) )
339 return;
340
341 m_range = val;
342 m_gauge->SetRange(val);
343 }
344
345 void GaugeWidgetsPage::OnButtonSetValue(wxCommandEvent& WXUNUSED(event))
346 {
347 unsigned long val;
348 if ( !m_textValue->GetValue().ToULong(&val) )
349 return;
350
351 m_gauge->SetValue(val);
352 }
353
354 void GaugeWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent& event)
355 {
356 unsigned long val;
357 event.Enable( m_textValue->GetValue().ToULong(&val) && (val <= m_range) );
358 }
359
360 void GaugeWidgetsPage::OnUpdateUIRangeButton(wxUpdateUIEvent& event)
361 {
362 unsigned long val;
363 event.Enable( m_textRange->GetValue().ToULong(&val) );
364 }
365
366 void GaugeWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
367 {
368 event.Enable( m_chkVert->GetValue() || m_chkSmooth->GetValue() );
369 }
370
371 void GaugeWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
372 {
373 CreateGauge();
374 }
375
376 void GaugeWidgetsPage::OnProgressTimer(wxTimerEvent& WXUNUSED(event))
377 {
378 int val = m_gauge->GetValue();
379 if ( (unsigned)val < m_range )
380 {
381 m_gauge->SetValue(val + 1);
382 }
383 else // reached the end
384 {
385 StopTimer();
386 }
387 }
388
389 void GaugeWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent& event)
390 {
391 event.SetText( wxString::Format(_T("%d"), m_gauge->GetValue()));
392 }
393
394 void GaugeWidgetsPage::StopTimer()
395 {
396 wxCHECK_RET( m_timer, _T("shouldn't be called") );
397
398 m_timer->Stop();
399 delete m_timer;
400 m_timer = NULL;
401
402 wxButton *btn = (wxButton *)FindWindow(GaugePage_Progress);
403 wxCHECK_RET( btn, _T("no progress button?") );
404
405 btn->SetLabel(_T("Simulate &progress"));
406
407 wxLogMessage(_T("Progress finished."));
408 }
409
410 #endif
411 // wxUSE_GAUGE