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