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