1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxGauge
5 // Author: Vadim Zeitlin
7 // Copyright: (c) 2001 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
26 // for all others, include the necessary headers
31 #include "wx/bitmap.h"
32 #include "wx/button.h"
33 #include "wx/checkbox.h"
34 #include "wx/combobox.h"
36 #include "wx/radiobox.h"
37 #include "wx/statbox.h"
38 #include "wx/textctrl.h"
45 #include "icons/gauge.xpm"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
54 GaugePage_Reset
= wxID_HIGHEST
,
56 GaugePage_IndeterminateProgress
,
60 GaugePage_CurValueText
,
64 GaugePage_IndeterminateTimer
,
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 class GaugeWidgetsPage
: public WidgetsPage
75 GaugeWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
76 virtual ~GaugeWidgetsPage();
78 virtual wxControl
*GetWidget() const { return m_gauge
; }
79 virtual void RecreateWidget() { CreateGauge(); }
81 // lazy creation of the content
82 virtual void CreateContent();
86 void OnButtonReset(wxCommandEvent
& event
);
87 void OnButtonProgress(wxCommandEvent
& event
);
88 void OnButtonIndeterminateProgress(wxCommandEvent
& event
);
89 void OnButtonClear(wxCommandEvent
& event
);
90 void OnButtonSetValue(wxCommandEvent
& event
);
91 void OnButtonSetRange(wxCommandEvent
& event
);
93 void OnCheckOrRadioBox(wxCommandEvent
& event
);
95 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
96 void OnUpdateUIRangeButton(wxUpdateUIEvent
& event
);
97 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
99 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
101 void OnProgressTimer(wxTimerEvent
& event
);
102 void OnIndeterminateProgressTimer(wxTimerEvent
& event
);
104 // reset the gauge parameters
107 // (re)create the gauge
110 // start progress timer
111 void StartTimer(wxButton
*);
113 // stop the progress timer
114 void StopTimer(wxButton
*);
117 unsigned long m_range
;
122 // the checkboxes for styles
123 wxCheckBox
*m_chkVert
,
126 // the gauge itself and the sizer it is in
128 wxSizer
*m_sizerGauge
;
130 // the text entries for set value/range
131 wxTextCtrl
*m_textValue
,
134 // the timer for simulating gauge progress
138 DECLARE_EVENT_TABLE()
139 DECLARE_WIDGETS_PAGE(GaugeWidgetsPage
)
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 BEGIN_EVENT_TABLE(GaugeWidgetsPage
, WidgetsPage
)
147 EVT_BUTTON(GaugePage_Reset
, GaugeWidgetsPage::OnButtonReset
)
148 EVT_BUTTON(GaugePage_Progress
, GaugeWidgetsPage::OnButtonProgress
)
149 EVT_BUTTON(GaugePage_IndeterminateProgress
, GaugeWidgetsPage::OnButtonIndeterminateProgress
)
150 EVT_BUTTON(GaugePage_Clear
, GaugeWidgetsPage::OnButtonClear
)
151 EVT_BUTTON(GaugePage_SetValue
, GaugeWidgetsPage::OnButtonSetValue
)
152 EVT_BUTTON(GaugePage_SetRange
, GaugeWidgetsPage::OnButtonSetRange
)
154 EVT_UPDATE_UI(GaugePage_SetValue
, GaugeWidgetsPage::OnUpdateUIValueButton
)
155 EVT_UPDATE_UI(GaugePage_SetRange
, GaugeWidgetsPage::OnUpdateUIRangeButton
)
156 EVT_UPDATE_UI(GaugePage_Reset
, GaugeWidgetsPage::OnUpdateUIResetButton
)
158 EVT_UPDATE_UI(GaugePage_CurValueText
, GaugeWidgetsPage::OnUpdateUICurValueText
)
160 EVT_CHECKBOX(wxID_ANY
, GaugeWidgetsPage::OnCheckOrRadioBox
)
161 EVT_RADIOBOX(wxID_ANY
, GaugeWidgetsPage::OnCheckOrRadioBox
)
163 EVT_TIMER(GaugePage_Timer
, GaugeWidgetsPage::OnProgressTimer
)
164 EVT_TIMER(GaugePage_IndeterminateTimer
, GaugeWidgetsPage::OnIndeterminateProgressTimer
)
167 // ============================================================================
169 // ============================================================================
171 #if defined(__WXUNIVERSAL__)
172 #define FAMILY_CTRLS UNIVERSAL_CTRLS
174 #define FAMILY_CTRLS NATIVE_CTRLS
177 IMPLEMENT_WIDGETS_PAGE(GaugeWidgetsPage
, wxT("Gauge"), FAMILY_CTRLS
);
179 GaugeWidgetsPage::GaugeWidgetsPage(WidgetsBookCtrl
*book
,
180 wxImageList
*imaglist
)
181 :WidgetsPage(book
, imaglist
, gauge_xpm
)
186 m_timer
= (wxTimer
*)NULL
;
189 m_chkSmooth
= (wxCheckBox
*)NULL
;
191 m_gauge
= (wxGauge
*)NULL
;
192 m_sizerGauge
= (wxSizer
*)NULL
;
195 void GaugeWidgetsPage::CreateContent()
197 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
200 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
202 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
204 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Vertical"));
205 m_chkSmooth
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Smooth"));
207 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
209 wxButton
*btn
= new wxButton(this, GaugePage_Reset
, wxT("&Reset"));
210 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
213 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, wxT("&Change gauge value"));
214 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
217 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(wxT("Current value"),
218 GaugePage_CurValueText
,
220 text
->SetEditable(false);
222 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
224 sizerRow
= CreateSizerWithTextAndButton(GaugePage_SetValue
,
228 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
230 sizerRow
= CreateSizerWithTextAndButton(GaugePage_SetRange
,
234 m_textRange
->SetValue( wxString::Format(wxT("%lu"), m_range
) );
235 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
237 btn
= new wxButton(this, GaugePage_Progress
, wxT("Simulate &progress"));
238 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
240 btn
= new wxButton(this, GaugePage_IndeterminateProgress
,
241 wxT("Simulate &indeterminate job"));
242 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
244 btn
= new wxButton(this, GaugePage_Clear
, wxT("&Clear"));
245 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
248 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
249 m_gauge
= new wxGauge(this, GaugePage_Gauge
, m_range
);
250 sizerRight
->Add(m_gauge
, 1, wxCENTRE
| wxALL
, 5);
251 sizerRight
->SetMinSize(150, 0);
252 m_sizerGauge
= sizerRight
; // save it to modify it later
254 // the 3 panes panes compose the window
255 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
256 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
257 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
259 // final initializations
265 GaugeWidgetsPage::~GaugeWidgetsPage()
270 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
274 void GaugeWidgetsPage::Reset()
276 m_chkVert
->SetValue(false);
277 m_chkSmooth
->SetValue(false);
280 void GaugeWidgetsPage::CreateGauge()
282 int flags
= ms_defaultFlags
;
284 if ( m_chkVert
->GetValue() )
285 flags
|= wxGA_VERTICAL
;
287 flags
|= wxGA_HORIZONTAL
;
289 if ( m_chkSmooth
->GetValue() )
290 flags
|= wxGA_SMOOTH
;
295 val
= m_gauge
->GetValue();
297 m_sizerGauge
->Detach( m_gauge
);
301 m_gauge
= new wxGauge(this, GaugePage_Gauge
, m_range
,
302 wxDefaultPosition
, wxDefaultSize
,
304 m_gauge
->SetValue(val
);
306 if ( flags
& wxGA_VERTICAL
)
307 m_sizerGauge
->Add(m_gauge
, 0, wxGROW
| wxALL
, 5);
309 m_sizerGauge
->Add(m_gauge
, 1, wxCENTRE
| wxALL
, 5);
311 m_sizerGauge
->Layout();
314 void GaugeWidgetsPage::StartTimer(wxButton
*clicked
)
316 static const int INTERVAL
= 300;
318 wxLogMessage(wxT("Launched progress timer (interval = %d ms)"), INTERVAL
);
320 m_timer
= new wxTimer(this,
321 clicked
->GetId() == GaugePage_Progress
? GaugePage_Timer
: GaugePage_IndeterminateTimer
);
322 m_timer
->Start(INTERVAL
);
324 clicked
->SetLabel(wxT("&Stop timer"));
326 if (clicked
->GetId() == GaugePage_Progress
)
327 FindWindow(GaugePage_IndeterminateProgress
)->Disable();
329 FindWindow(GaugePage_Progress
)->Disable();
332 void GaugeWidgetsPage::StopTimer(wxButton
*clicked
)
334 wxCHECK_RET( m_timer
, wxT("shouldn't be called") );
339 if (clicked
->GetId() == GaugePage_Progress
)
341 clicked
->SetLabel(wxT("Simulate &progress"));
342 FindWindow(GaugePage_IndeterminateProgress
)->Enable();
346 clicked
->SetLabel(wxT("Simulate indeterminate job"));
347 FindWindow(GaugePage_Progress
)->Enable();
350 wxLogMessage(wxT("Progress finished."));
353 // ----------------------------------------------------------------------------
355 // ----------------------------------------------------------------------------
357 void GaugeWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
364 void GaugeWidgetsPage::OnButtonProgress(wxCommandEvent
& event
)
366 wxButton
*b
= (wxButton
*)event
.GetEventObject();
371 else // stop the running timer
375 wxLogMessage(wxT("Stopped the timer."));
379 void GaugeWidgetsPage::OnButtonIndeterminateProgress(wxCommandEvent
& event
)
381 wxButton
*b
= (wxButton
*)event
.GetEventObject();
386 else // stop the running timer
390 m_gauge
->SetValue(0);
392 wxLogMessage(wxT("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
, wxT("no progress button?") );
458 void GaugeWidgetsPage::OnIndeterminateProgressTimer(wxTimerEvent
& WXUNUSED(event
))
463 void GaugeWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
465 event
.SetText( wxString::Format(wxT("%d"), m_gauge
->GetValue()));