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