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