1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxGauge
5 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers
32 #include "wx/bitmap.h"
33 #include "wx/button.h"
34 #include "wx/checkbox.h"
35 #include "wx/combobox.h"
37 #include "wx/radiobox.h"
38 #include "wx/statbox.h"
39 #include "wx/textctrl.h"
46 #include "icons/gauge.xpm"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
55 GaugePage_Reset
= wxID_HIGHEST
,
57 GaugePage_IndeterminateProgress
,
61 GaugePage_CurValueText
,
65 GaugePage_IndeterminateTimer
,
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 class GaugeWidgetsPage
: public WidgetsPage
76 GaugeWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
77 virtual ~GaugeWidgetsPage();
79 virtual wxControl
*GetWidget() const { return m_gauge
; }
80 virtual void RecreateWidget() { CreateGauge(); }
82 // lazy creation of the content
83 virtual void CreateContent();
87 void OnButtonReset(wxCommandEvent
& event
);
88 void OnButtonProgress(wxCommandEvent
& event
);
89 void OnButtonIndeterminateProgress(wxCommandEvent
& event
);
90 void OnButtonClear(wxCommandEvent
& event
);
91 void OnButtonSetValue(wxCommandEvent
& event
);
92 void OnButtonSetRange(wxCommandEvent
& event
);
94 void OnCheckOrRadioBox(wxCommandEvent
& event
);
96 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
97 void OnUpdateUIRangeButton(wxUpdateUIEvent
& event
);
98 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
100 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
102 void OnProgressTimer(wxTimerEvent
& event
);
103 void OnIndeterminateProgressTimer(wxTimerEvent
& event
);
105 // reset the gauge parameters
108 // (re)create the gauge
111 // start progress timer
112 void StartTimer(wxButton
*);
114 // stop the progress timer
115 void StopTimer(wxButton
*);
118 unsigned long m_range
;
123 // the checkboxes for styles
124 wxCheckBox
*m_chkVert
,
127 // the gauge itself and the sizer it is in
129 wxSizer
*m_sizerGauge
;
131 // the text entries for set value/range
132 wxTextCtrl
*m_textValue
,
135 // the timer for simulating gauge progress
139 DECLARE_EVENT_TABLE()
140 DECLARE_WIDGETS_PAGE(GaugeWidgetsPage
)
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 BEGIN_EVENT_TABLE(GaugeWidgetsPage
, WidgetsPage
)
148 EVT_BUTTON(GaugePage_Reset
, GaugeWidgetsPage::OnButtonReset
)
149 EVT_BUTTON(GaugePage_Progress
, GaugeWidgetsPage::OnButtonProgress
)
150 EVT_BUTTON(GaugePage_IndeterminateProgress
, GaugeWidgetsPage::OnButtonIndeterminateProgress
)
151 EVT_BUTTON(GaugePage_Clear
, GaugeWidgetsPage::OnButtonClear
)
152 EVT_BUTTON(GaugePage_SetValue
, GaugeWidgetsPage::OnButtonSetValue
)
153 EVT_BUTTON(GaugePage_SetRange
, GaugeWidgetsPage::OnButtonSetRange
)
155 EVT_UPDATE_UI(GaugePage_SetValue
, GaugeWidgetsPage::OnUpdateUIValueButton
)
156 EVT_UPDATE_UI(GaugePage_SetRange
, GaugeWidgetsPage::OnUpdateUIRangeButton
)
157 EVT_UPDATE_UI(GaugePage_Reset
, GaugeWidgetsPage::OnUpdateUIResetButton
)
159 EVT_UPDATE_UI(GaugePage_CurValueText
, GaugeWidgetsPage::OnUpdateUICurValueText
)
161 EVT_CHECKBOX(wxID_ANY
, GaugeWidgetsPage::OnCheckOrRadioBox
)
162 EVT_RADIOBOX(wxID_ANY
, GaugeWidgetsPage::OnCheckOrRadioBox
)
164 EVT_TIMER(GaugePage_Timer
, GaugeWidgetsPage::OnProgressTimer
)
165 EVT_TIMER(GaugePage_IndeterminateTimer
, GaugeWidgetsPage::OnIndeterminateProgressTimer
)
168 // ============================================================================
170 // ============================================================================
172 #if defined(__WXUNIVERSAL__)
173 #define FAMILY_CTRLS UNIVERSAL_CTRLS
175 #define FAMILY_CTRLS NATIVE_CTRLS
178 IMPLEMENT_WIDGETS_PAGE(GaugeWidgetsPage
, _T("Gauge"), FAMILY_CTRLS
);
180 GaugeWidgetsPage::GaugeWidgetsPage(WidgetsBookCtrl
*book
,
181 wxImageList
*imaglist
)
182 :WidgetsPage(book
, imaglist
, gauge_xpm
)
187 m_timer
= (wxTimer
*)NULL
;
190 m_chkSmooth
= (wxCheckBox
*)NULL
;
192 m_gauge
= (wxGauge
*)NULL
;
193 m_sizerGauge
= (wxSizer
*)NULL
;
196 void GaugeWidgetsPage::CreateContent()
198 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
201 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
203 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
205 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
206 m_chkSmooth
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Smooth"));
208 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
210 wxButton
*btn
= new wxButton(this, GaugePage_Reset
, _T("&Reset"));
211 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
214 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change gauge value"));
215 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
218 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
219 GaugePage_CurValueText
,
221 text
->SetEditable(false);
223 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
225 sizerRow
= CreateSizerWithTextAndButton(GaugePage_SetValue
,
229 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
231 sizerRow
= CreateSizerWithTextAndButton(GaugePage_SetRange
,
235 m_textRange
->SetValue( wxString::Format(_T("%lu"), m_range
) );
236 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
238 btn
= new wxButton(this, GaugePage_Progress
, _T("Simulate &progress"));
239 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
241 btn
= new wxButton(this, GaugePage_IndeterminateProgress
,
242 _T("Simulate &indeterminate job"));
243 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
245 btn
= new wxButton(this, GaugePage_Clear
, _T("&Clear"));
246 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
249 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
250 m_gauge
= new wxGauge(this, GaugePage_Gauge
, m_range
);
251 sizerRight
->Add(m_gauge
, 1, wxCENTRE
| wxALL
, 5);
252 sizerRight
->SetMinSize(150, 0);
253 m_sizerGauge
= sizerRight
; // save it to modify it later
255 // the 3 panes panes compose the window
256 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
257 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
258 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
260 // final initializations
266 GaugeWidgetsPage::~GaugeWidgetsPage()
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
275 void GaugeWidgetsPage::Reset()
277 m_chkVert
->SetValue(false);
278 m_chkSmooth
->SetValue(false);
281 void GaugeWidgetsPage::CreateGauge()
283 int flags
= ms_defaultFlags
;
285 if ( m_chkVert
->GetValue() )
286 flags
|= wxGA_VERTICAL
;
288 flags
|= wxGA_HORIZONTAL
;
290 if ( m_chkSmooth
->GetValue() )
291 flags
|= wxGA_SMOOTH
;
296 val
= m_gauge
->GetValue();
298 m_sizerGauge
->Detach( m_gauge
);
302 m_gauge
= new wxGauge(this, GaugePage_Gauge
, m_range
,
303 wxDefaultPosition
, wxDefaultSize
,
305 m_gauge
->SetValue(val
);
307 if ( flags
& wxGA_VERTICAL
)
308 m_sizerGauge
->Add(m_gauge
, 0, wxGROW
| wxALL
, 5);
310 m_sizerGauge
->Add(m_gauge
, 1, wxCENTRE
| wxALL
, 5);
312 m_sizerGauge
->Layout();
315 void GaugeWidgetsPage::StartTimer(wxButton
*clicked
)
317 static const int INTERVAL
= 300;
319 wxLogMessage(_T("Launched progress timer (interval = %d ms)"), INTERVAL
);
321 m_timer
= new wxTimer(this,
322 clicked
->GetId() == GaugePage_Progress
? GaugePage_Timer
: GaugePage_IndeterminateTimer
);
323 m_timer
->Start(INTERVAL
);
325 clicked
->SetLabel(_T("&Stop timer"));
327 if (clicked
->GetId() == GaugePage_Progress
)
328 FindWindow(GaugePage_IndeterminateProgress
)->Disable();
330 FindWindow(GaugePage_Progress
)->Disable();
333 void GaugeWidgetsPage::StopTimer(wxButton
*clicked
)
335 wxCHECK_RET( m_timer
, _T("shouldn't be called") );
341 if (clicked
->GetId() == GaugePage_Progress
)
343 clicked
->SetLabel(_T("Simulate &progress"));
344 FindWindow(GaugePage_IndeterminateProgress
)->Enable();
348 clicked
->SetLabel(_T("Simulate indeterminate job"));
349 FindWindow(GaugePage_Progress
)->Enable();
352 wxLogMessage(_T("Progress finished."));
355 // ----------------------------------------------------------------------------
357 // ----------------------------------------------------------------------------
359 void GaugeWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
366 void GaugeWidgetsPage::OnButtonProgress(wxCommandEvent
& event
)
368 wxButton
*b
= (wxButton
*)event
.GetEventObject();
373 else // stop the running timer
377 wxLogMessage(_T("Stopped the timer."));
381 void GaugeWidgetsPage::OnButtonIndeterminateProgress(wxCommandEvent
& event
)
383 wxButton
*b
= (wxButton
*)event
.GetEventObject();
388 else // stop the running timer
392 wxLogMessage(_T("Stopped the timer."));
396 void GaugeWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
398 m_gauge
->SetValue(0);
401 void GaugeWidgetsPage::OnButtonSetRange(wxCommandEvent
& WXUNUSED(event
))
404 if ( !m_textRange
->GetValue().ToULong(&val
) )
408 m_gauge
->SetRange(val
);
411 void GaugeWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
414 if ( !m_textValue
->GetValue().ToULong(&val
) )
417 m_gauge
->SetValue(val
);
420 void GaugeWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
423 event
.Enable( m_textValue
->GetValue().ToULong(&val
) && (val
<= m_range
) );
426 void GaugeWidgetsPage::OnUpdateUIRangeButton(wxUpdateUIEvent
& event
)
429 event
.Enable( m_textRange
->GetValue().ToULong(&val
) );
432 void GaugeWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
434 event
.Enable( m_chkVert
->GetValue() || m_chkSmooth
->GetValue() );
437 void GaugeWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
442 void GaugeWidgetsPage::OnProgressTimer(wxTimerEvent
& WXUNUSED(event
))
444 int val
= m_gauge
->GetValue();
445 if ( (unsigned)val
< m_range
)
447 m_gauge
->SetValue(val
+ 1);
449 else // reached the end
451 wxButton
*btn
= (wxButton
*)FindWindow(GaugePage_Progress
);
452 wxCHECK_RET( btn
, _T("no progress button?") );
458 void GaugeWidgetsPage::OnIndeterminateProgressTimer(wxTimerEvent
& WXUNUSED(event
))
463 void GaugeWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
465 event
.SetText( wxString::Format(_T("%d"), m_gauge
->GetValue()));