]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/gauge.cpp
add compilation options to allow compiling in just the selected wxUniv themes and...
[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{
f0fa4312 55 GaugePage_Reset = wxID_HIGHEST,
32b8ec41
VZ
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:
f2fdc4d5 74 GaugeWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
32b8ec41
VZ
75 virtual ~GaugeWidgetsPage();
76
195df7a7 77 virtual wxControl *GetWidget() const { return m_gauge; }
1301e228 78 virtual void RecreateWidget() { CreateGauge(); }
195df7a7 79
453535a7
WS
80 // lazy creation of the content
81 virtual void CreateContent();
82
32b8ec41
VZ
83protected:
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,
ef78ec37
KO
118 *m_chkSmooth,
119 *m_chkIndeterminate;
32b8ec41
VZ
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
132private:
5e173f35
GD
133 DECLARE_EVENT_TABLE()
134 DECLARE_WIDGETS_PAGE(GaugeWidgetsPage)
32b8ec41
VZ
135};
136
137// ----------------------------------------------------------------------------
138// event tables
139// ----------------------------------------------------------------------------
140
141BEGIN_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
206d3a16
JS
154 EVT_CHECKBOX(wxID_ANY, GaugeWidgetsPage::OnCheckOrRadioBox)
155 EVT_RADIOBOX(wxID_ANY, GaugeWidgetsPage::OnCheckOrRadioBox)
32b8ec41
VZ
156
157 EVT_TIMER(GaugePage_Timer, GaugeWidgetsPage::OnProgressTimer)
158END_EVENT_TABLE()
159
160// ============================================================================
161// implementation
162// ============================================================================
163
f0fa4312
WS
164#if defined(__WXUNIVERSAL__)
165 #define FAMILY_CTRLS UNIVERSAL_CTRLS
166#else
167 #define FAMILY_CTRLS NATIVE_CTRLS
168#endif
169
170IMPLEMENT_WIDGETS_PAGE(GaugeWidgetsPage, _T("Gauge"), FAMILY_CTRLS );
32b8ec41 171
f2fdc4d5 172GaugeWidgetsPage::GaugeWidgetsPage(WidgetsBookCtrl *book,
61c083e7 173 wxImageList *imaglist)
261357eb 174 :WidgetsPage(book, imaglist, gauge_xpm)
32b8ec41 175{
32b8ec41
VZ
176 // init everything
177 m_range = 100;
178
179 m_timer = (wxTimer *)NULL;
180
181 m_chkVert =
ef78ec37
KO
182 m_chkSmooth =
183 m_chkIndeterminate = (wxCheckBox *)NULL;
32b8ec41
VZ
184
185 m_gauge = (wxGauge *)NULL;
186 m_sizerGauge = (wxSizer *)NULL;
453535a7 187}
32b8ec41 188
453535a7
WS
189void GaugeWidgetsPage::CreateContent()
190{
32b8ec41
VZ
191 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
192
193 // left pane
206d3a16 194 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
32b8ec41
VZ
195
196 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
197
198 m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical"));
199 m_chkSmooth = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Smooth"));
ef78ec37 200 m_chkIndeterminate = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Indeterminate"));
32b8ec41
VZ
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
206d3a16
JS
208 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY,
209 _T("&Change gauge value"));
32b8ec41
VZ
210 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
211
212 wxTextCtrl *text;
213 wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Current value"),
214 GaugePage_CurValueText,
215 &text);
206d3a16 216 text->SetEditable(false);
32b8ec41
VZ
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);
aec18ff7 230 m_textRange->SetValue( wxString::Format(_T("%lu"), m_range) );
32b8ec41
VZ
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);
7b127900 243 sizerRight->SetMinSize(150, 0);
32b8ec41
VZ
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
32b8ec41
VZ
254 SetSizer(sizerTop);
255
256 sizerTop->Fit(this);
257}
258
259GaugeWidgetsPage::~GaugeWidgetsPage()
260{
261 delete m_timer;
262}
263
264// ----------------------------------------------------------------------------
265// operations
266// ----------------------------------------------------------------------------
267
268void GaugeWidgetsPage::Reset()
269{
206d3a16
JS
270 m_chkVert->SetValue(false);
271 m_chkSmooth->SetValue(false);
ef78ec37 272 m_chkIndeterminate->SetValue(false);
32b8ec41
VZ
273}
274
275void GaugeWidgetsPage::CreateGauge()
276{
1301e228 277 int flags = ms_defaultFlags;
32b8ec41
VZ
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
12a3f227 292 m_sizerGauge->Detach( m_gauge );
32b8ec41
VZ
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);
ef78ec37
KO
300
301 if ( m_chkIndeterminate->GetValue() ){
302 m_gauge->Pulse();
303 }
32b8ec41
VZ
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
317void GaugeWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
318{
319 Reset();
320
321 CreateGauge();
322}
323
324void 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
346void GaugeWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event))
347{
348 m_gauge->SetValue(0);
349}
350
351void GaugeWidgetsPage::OnButtonSetRange(wxCommandEvent& WXUNUSED(event))
352{
353 unsigned long val;
354 if ( !m_textRange->GetValue().ToULong(&val) )
355 return;
356
efb349e4 357 m_range = val;
32b8ec41 358 m_gauge->SetRange(val);
ef78ec37 359 m_chkIndeterminate->SetValue(0);
32b8ec41
VZ
360}
361
362void 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);
ef78ec37 369 m_chkIndeterminate->SetValue(0);
32b8ec41
VZ
370}
371
372void GaugeWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent& event)
373{
374 unsigned long val;
375 event.Enable( m_textValue->GetValue().ToULong(&val) && (val <= m_range) );
376}
377
378void GaugeWidgetsPage::OnUpdateUIRangeButton(wxUpdateUIEvent& event)
379{
380 unsigned long val;
381 event.Enable( m_textRange->GetValue().ToULong(&val) );
382}
383
384void GaugeWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
385{
386 event.Enable( m_chkVert->GetValue() || m_chkSmooth->GetValue() );
387}
388
c02e5a31 389void GaugeWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
390{
391 CreateGauge();
392}
393
394void 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
407void GaugeWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent& event)
408{
409 event.SetText( wxString::Format(_T("%d"), m_gauge->GetValue()));
410}
411
412void 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
aec18ff7 428#endif
8dfc2d98 429 // wxUSE_GAUGE