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