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
= 100,
60 GaugePage_CurValueText
,
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 class GaugeWidgetsPage
: public WidgetsPage
74 GaugeWidgetsPage(wxBookCtrl
*book
, wxImageList
*imaglist
);
75 virtual ~GaugeWidgetsPage();
77 virtual wxControl
*GetWidget() const { return m_gauge
; }
81 void OnButtonReset(wxCommandEvent
& event
);
82 void OnButtonProgress(wxCommandEvent
& event
);
83 void OnButtonClear(wxCommandEvent
& event
);
84 void OnButtonSetValue(wxCommandEvent
& event
);
85 void OnButtonSetRange(wxCommandEvent
& event
);
87 void OnCheckOrRadioBox(wxCommandEvent
& event
);
89 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
90 void OnUpdateUIRangeButton(wxUpdateUIEvent
& event
);
91 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
93 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
95 void OnProgressTimer(wxTimerEvent
& event
);
97 // reset the gauge parameters
100 // (re)create the gauge
103 // stop the progress timer
107 unsigned long m_range
;
112 // the checkboxes for styles
113 wxCheckBox
*m_chkVert
,
116 // the gauge itself and the sizer it is in
118 wxSizer
*m_sizerGauge
;
120 // the text entries for set value/range
121 wxTextCtrl
*m_textValue
,
124 // the timer for simulating gauge progress
128 DECLARE_EVENT_TABLE()
129 DECLARE_WIDGETS_PAGE(GaugeWidgetsPage
)
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 BEGIN_EVENT_TABLE(GaugeWidgetsPage
, WidgetsPage
)
137 EVT_BUTTON(GaugePage_Reset
, GaugeWidgetsPage::OnButtonReset
)
138 EVT_BUTTON(GaugePage_Progress
, GaugeWidgetsPage::OnButtonProgress
)
139 EVT_BUTTON(GaugePage_Clear
, GaugeWidgetsPage::OnButtonClear
)
140 EVT_BUTTON(GaugePage_SetValue
, GaugeWidgetsPage::OnButtonSetValue
)
141 EVT_BUTTON(GaugePage_SetRange
, GaugeWidgetsPage::OnButtonSetRange
)
143 EVT_UPDATE_UI(GaugePage_SetValue
, GaugeWidgetsPage::OnUpdateUIValueButton
)
144 EVT_UPDATE_UI(GaugePage_SetRange
, GaugeWidgetsPage::OnUpdateUIRangeButton
)
145 EVT_UPDATE_UI(GaugePage_Reset
, GaugeWidgetsPage::OnUpdateUIResetButton
)
147 EVT_UPDATE_UI(GaugePage_CurValueText
, GaugeWidgetsPage::OnUpdateUICurValueText
)
149 EVT_CHECKBOX(wxID_ANY
, GaugeWidgetsPage::OnCheckOrRadioBox
)
150 EVT_RADIOBOX(wxID_ANY
, GaugeWidgetsPage::OnCheckOrRadioBox
)
152 EVT_TIMER(GaugePage_Timer
, GaugeWidgetsPage::OnProgressTimer
)
155 // ============================================================================
157 // ============================================================================
159 IMPLEMENT_WIDGETS_PAGE(GaugeWidgetsPage
, _T("Gauge"));
161 GaugeWidgetsPage::GaugeWidgetsPage(wxBookCtrl
*book
,
162 wxImageList
*imaglist
)
165 imaglist
->Add(wxBitmap(gauge_xpm
));
170 m_timer
= (wxTimer
*)NULL
;
173 m_chkSmooth
= (wxCheckBox
*)NULL
;
175 m_gauge
= (wxGauge
*)NULL
;
176 m_sizerGauge
= (wxSizer
*)NULL
;
178 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
181 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
183 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
185 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
186 m_chkSmooth
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Smooth"));
188 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
190 wxButton
*btn
= new wxButton(this, GaugePage_Reset
, _T("&Reset"));
191 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
194 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
195 _T("&Change gauge value"));
196 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
199 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
200 GaugePage_CurValueText
,
202 text
->SetEditable(false);
204 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
206 sizerRow
= CreateSizerWithTextAndButton(GaugePage_SetValue
,
210 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
212 sizerRow
= CreateSizerWithTextAndButton(GaugePage_SetRange
,
216 m_textRange
->SetValue( wxString::Format(_T("%lu"), m_range
) );
217 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
219 btn
= new wxButton(this, GaugePage_Progress
, _T("Simulate &progress"));
220 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
222 btn
= new wxButton(this, GaugePage_Clear
, _T("&Clear"));
223 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
226 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
227 m_gauge
= new wxGauge(this, GaugePage_Gauge
, m_range
);
228 sizerRight
->Add(m_gauge
, 1, wxCENTRE
| wxALL
, 5);
229 sizerRight
->SetMinSize(150, 0);
230 m_sizerGauge
= sizerRight
; // save it to modify it later
232 // the 3 panes panes compose the window
233 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
234 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
235 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
237 // final initializations
245 GaugeWidgetsPage::~GaugeWidgetsPage()
250 // ----------------------------------------------------------------------------
252 // ----------------------------------------------------------------------------
254 void GaugeWidgetsPage::Reset()
256 m_chkVert
->SetValue(false);
257 m_chkSmooth
->SetValue(false);
260 void GaugeWidgetsPage::CreateGauge()
264 if ( m_chkVert
->GetValue() )
265 flags
|= wxGA_VERTICAL
;
267 flags
|= wxGA_HORIZONTAL
;
269 if ( m_chkSmooth
->GetValue() )
270 flags
|= wxGA_SMOOTH
;
275 val
= m_gauge
->GetValue();
277 m_sizerGauge
->Detach( m_gauge
);
281 m_gauge
= new wxGauge(this, GaugePage_Gauge
, m_range
,
282 wxDefaultPosition
, wxDefaultSize
,
284 m_gauge
->SetValue(val
);
286 if ( flags
& wxGA_VERTICAL
)
287 m_sizerGauge
->Add(m_gauge
, 0, wxGROW
| wxALL
, 5);
289 m_sizerGauge
->Add(m_gauge
, 1, wxCENTRE
| wxALL
, 5);
291 m_sizerGauge
->Layout();
294 // ----------------------------------------------------------------------------
296 // ----------------------------------------------------------------------------
298 void GaugeWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
305 void GaugeWidgetsPage::OnButtonProgress(wxCommandEvent
& event
)
309 static const int INTERVAL
= 300;
311 wxLogMessage(_T("Launched progress timer (interval = %d ms)"), INTERVAL
);
313 m_timer
= new wxTimer(this, GaugePage_Timer
);
314 m_timer
->Start(INTERVAL
);
316 wxButton
*btn
= (wxButton
*)event
.GetEventObject();
317 btn
->SetLabel(_T("&Stop timer"));
319 else // stop the running timer
323 wxLogMessage(_T("Stopped the timer."));
327 void GaugeWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
329 m_gauge
->SetValue(0);
332 void GaugeWidgetsPage::OnButtonSetRange(wxCommandEvent
& WXUNUSED(event
))
335 if ( !m_textRange
->GetValue().ToULong(&val
) )
339 m_gauge
->SetRange(val
);
342 void GaugeWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
345 if ( !m_textValue
->GetValue().ToULong(&val
) )
348 m_gauge
->SetValue(val
);
351 void GaugeWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
354 event
.Enable( m_textValue
->GetValue().ToULong(&val
) && (val
<= m_range
) );
357 void GaugeWidgetsPage::OnUpdateUIRangeButton(wxUpdateUIEvent
& event
)
360 event
.Enable( m_textRange
->GetValue().ToULong(&val
) );
363 void GaugeWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
365 event
.Enable( m_chkVert
->GetValue() || m_chkSmooth
->GetValue() );
368 void GaugeWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
373 void GaugeWidgetsPage::OnProgressTimer(wxTimerEvent
& WXUNUSED(event
))
375 int val
= m_gauge
->GetValue();
376 if ( (unsigned)val
< m_range
)
378 m_gauge
->SetValue(val
+ 1);
380 else // reached the end
386 void GaugeWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
388 event
.SetText( wxString::Format(_T("%d"), m_gauge
->GetValue()));
391 void GaugeWidgetsPage::StopTimer()
393 wxCHECK_RET( m_timer
, _T("shouldn't be called") );
399 wxButton
*btn
= (wxButton
*)FindWindow(GaugePage_Progress
);
400 wxCHECK_RET( btn
, _T("no progress button?") );
402 btn
->SetLabel(_T("Simulate &progress"));
404 wxLogMessage(_T("Progress finished."));