]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/gauge.cpp
added EVT_AUI_PANECLOSE and updated sample accordingly
[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,
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
131private:
5e173f35
GD
132 DECLARE_EVENT_TABLE()
133 DECLARE_WIDGETS_PAGE(GaugeWidgetsPage)
32b8ec41
VZ
134};
135
136// ----------------------------------------------------------------------------
137// event tables
138// ----------------------------------------------------------------------------
139
140BEGIN_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
206d3a16
JS
153 EVT_CHECKBOX(wxID_ANY, GaugeWidgetsPage::OnCheckOrRadioBox)
154 EVT_RADIOBOX(wxID_ANY, GaugeWidgetsPage::OnCheckOrRadioBox)
32b8ec41
VZ
155
156 EVT_TIMER(GaugePage_Timer, GaugeWidgetsPage::OnProgressTimer)
157END_EVENT_TABLE()
158
159// ============================================================================
160// implementation
161// ============================================================================
162
f0fa4312
WS
163#if defined(__WXUNIVERSAL__)
164 #define FAMILY_CTRLS UNIVERSAL_CTRLS
165#else
166 #define FAMILY_CTRLS NATIVE_CTRLS
167#endif
168
169IMPLEMENT_WIDGETS_PAGE(GaugeWidgetsPage, _T("Gauge"), FAMILY_CTRLS );
32b8ec41 170
f2fdc4d5 171GaugeWidgetsPage::GaugeWidgetsPage(WidgetsBookCtrl *book,
61c083e7 172 wxImageList *imaglist)
261357eb 173 :WidgetsPage(book, imaglist, gauge_xpm)
32b8ec41 174{
32b8ec41
VZ
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;
453535a7 185}
32b8ec41 186
453535a7
WS
187void GaugeWidgetsPage::CreateContent()
188{
32b8ec41
VZ
189 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
190
191 // left pane
206d3a16 192 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
32b8ec41
VZ
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
206d3a16
JS
205 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY,
206 _T("&Change gauge value"));
32b8ec41
VZ
207 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
208
209 wxTextCtrl *text;
210 wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Current value"),
211 GaugePage_CurValueText,
212 &text);
206d3a16 213 text->SetEditable(false);
32b8ec41
VZ
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);
aec18ff7 227 m_textRange->SetValue( wxString::Format(_T("%lu"), m_range) );
32b8ec41
VZ
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);
7b127900 240 sizerRight->SetMinSize(150, 0);
32b8ec41
VZ
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
32b8ec41
VZ
251 SetSizer(sizerTop);
252
253 sizerTop->Fit(this);
254}
255
256GaugeWidgetsPage::~GaugeWidgetsPage()
257{
258 delete m_timer;
259}
260
261// ----------------------------------------------------------------------------
262// operations
263// ----------------------------------------------------------------------------
264
265void GaugeWidgetsPage::Reset()
266{
206d3a16
JS
267 m_chkVert->SetValue(false);
268 m_chkSmooth->SetValue(false);
32b8ec41
VZ
269}
270
271void GaugeWidgetsPage::CreateGauge()
272{
1301e228 273 int flags = ms_defaultFlags;
32b8ec41
VZ
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
12a3f227 288 m_sizerGauge->Detach( m_gauge );
32b8ec41
VZ
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
309void GaugeWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
310{
311 Reset();
312
313 CreateGauge();
314}
315
316void 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
338void GaugeWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event))
339{
340 m_gauge->SetValue(0);
341}
342
343void GaugeWidgetsPage::OnButtonSetRange(wxCommandEvent& WXUNUSED(event))
344{
345 unsigned long val;
346 if ( !m_textRange->GetValue().ToULong(&val) )
347 return;
348
efb349e4 349 m_range = val;
32b8ec41
VZ
350 m_gauge->SetRange(val);
351}
352
353void 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
362void GaugeWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent& event)
363{
364 unsigned long val;
365 event.Enable( m_textValue->GetValue().ToULong(&val) && (val <= m_range) );
366}
367
368void GaugeWidgetsPage::OnUpdateUIRangeButton(wxUpdateUIEvent& event)
369{
370 unsigned long val;
371 event.Enable( m_textRange->GetValue().ToULong(&val) );
372}
373
374void GaugeWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
375{
376 event.Enable( m_chkVert->GetValue() || m_chkSmooth->GetValue() );
377}
378
c02e5a31 379void GaugeWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
380{
381 CreateGauge();
382}
383
384void 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
397void GaugeWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent& event)
398{
399 event.SetText( wxString::Format(_T("%d"), m_gauge->GetValue()));
400}
401
402void 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
aec18ff7 418#endif
8dfc2d98 419 // wxUSE_GAUGE