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