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