1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWindows 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/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
= 100,
59 GaugePage_CurValueText
,
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 class GaugeWidgetsPage
: public WidgetsPage
73 GaugeWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
74 virtual ~GaugeWidgetsPage();
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
);
84 void OnCheckOrRadioBox(wxCommandEvent
& event
);
86 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
87 void OnUpdateUIRangeButton(wxUpdateUIEvent
& event
);
88 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
90 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
92 void OnProgressTimer(wxTimerEvent
& event
);
94 // reset the gauge parameters
97 // (re)create the gauge
100 // stop the progress timer
104 unsigned long m_range
;
109 // the checkboxes for styles
110 wxCheckBox
*m_chkVert
,
113 // the gauge itself and the sizer it is in
115 wxSizer
*m_sizerGauge
;
117 // the text entries for set value/range
118 wxTextCtrl
*m_textValue
,
121 // the timer for simulating gauge progress
125 DECLARE_EVENT_TABLE()
126 DECLARE_WIDGETS_PAGE(GaugeWidgetsPage
)
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 BEGIN_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
)
140 EVT_UPDATE_UI(GaugePage_SetValue
, GaugeWidgetsPage::OnUpdateUIValueButton
)
141 EVT_UPDATE_UI(GaugePage_SetRange
, GaugeWidgetsPage::OnUpdateUIRangeButton
)
142 EVT_UPDATE_UI(GaugePage_Reset
, GaugeWidgetsPage::OnUpdateUIResetButton
)
144 EVT_UPDATE_UI(GaugePage_CurValueText
, GaugeWidgetsPage::OnUpdateUICurValueText
)
146 EVT_CHECKBOX(-1, GaugeWidgetsPage::OnCheckOrRadioBox
)
147 EVT_RADIOBOX(-1, GaugeWidgetsPage::OnCheckOrRadioBox
)
149 EVT_TIMER(GaugePage_Timer
, GaugeWidgetsPage::OnProgressTimer
)
152 // ============================================================================
154 // ============================================================================
156 IMPLEMENT_WIDGETS_PAGE(GaugeWidgetsPage
, _T("Gauge"));
158 GaugeWidgetsPage::GaugeWidgetsPage(wxNotebook
*notebook
,
159 wxImageList
*imaglist
)
160 : WidgetsPage(notebook
)
162 imaglist
->Add(wxBitmap(gauge_xpm
));
167 m_timer
= (wxTimer
*)NULL
;
170 m_chkSmooth
= (wxCheckBox
*)NULL
;
172 m_gauge
= (wxGauge
*)NULL
;
173 m_sizerGauge
= (wxSizer
*)NULL
;
175 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
178 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set style"));
180 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
182 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
183 m_chkSmooth
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Smooth"));
185 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
187 wxButton
*btn
= new wxButton(this, GaugePage_Reset
, _T("&Reset"));
188 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
191 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Change gauge value"));
192 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
195 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
196 GaugePage_CurValueText
,
198 text
->SetEditable(FALSE
);
200 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
202 sizerRow
= CreateSizerWithTextAndButton(GaugePage_SetValue
,
206 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
208 sizerRow
= CreateSizerWithTextAndButton(GaugePage_SetRange
,
212 m_textRange
->SetValue(wxString::Format(_T("%lu"), m_range
));
213 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
215 btn
= new wxButton(this, GaugePage_Progress
, _T("Simulate &progress"));
216 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
218 btn
= new wxButton(this, GaugePage_Clear
, _T("&Clear"));
219 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
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(150, 0);
226 m_sizerGauge
= sizerRight
; // save it to modify it later
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);
233 // final initializations
242 GaugeWidgetsPage::~GaugeWidgetsPage()
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 void GaugeWidgetsPage::Reset()
253 m_chkVert
->SetValue(FALSE
);
254 m_chkSmooth
->SetValue(FALSE
);
257 void GaugeWidgetsPage::CreateGauge()
261 if ( m_chkVert
->GetValue() )
262 flags
|= wxGA_VERTICAL
;
264 flags
|= wxGA_HORIZONTAL
;
266 if ( m_chkSmooth
->GetValue() )
267 flags
|= wxGA_SMOOTH
;
272 val
= m_gauge
->GetValue();
274 m_sizerGauge
->Remove(m_gauge
);
278 m_gauge
= new wxGauge(this, GaugePage_Gauge
, m_range
,
279 wxDefaultPosition
, wxDefaultSize
,
281 m_gauge
->SetValue(val
);
283 if ( flags
& wxGA_VERTICAL
)
284 m_sizerGauge
->Add(m_gauge
, 0, wxGROW
| wxALL
, 5);
286 m_sizerGauge
->Add(m_gauge
, 1, wxCENTRE
| wxALL
, 5);
288 m_sizerGauge
->Layout();
291 // ----------------------------------------------------------------------------
293 // ----------------------------------------------------------------------------
295 void GaugeWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
302 void GaugeWidgetsPage::OnButtonProgress(wxCommandEvent
& event
)
306 static const int INTERVAL
= 300;
308 wxLogMessage(_T("Launched progress timer (interval = %d ms)"), INTERVAL
);
310 m_timer
= new wxTimer(this, GaugePage_Timer
);
311 m_timer
->Start(INTERVAL
);
313 wxButton
*btn
= (wxButton
*)event
.GetEventObject();
314 btn
->SetLabel(_T("&Stop timer"));
316 else // stop the running timer
320 wxLogMessage(_T("Stopped the timer."));
324 void GaugeWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
326 m_gauge
->SetValue(0);
329 void GaugeWidgetsPage::OnButtonSetRange(wxCommandEvent
& WXUNUSED(event
))
332 if ( !m_textRange
->GetValue().ToULong(&val
) )
335 m_gauge
->SetRange(val
);
338 void GaugeWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
341 if ( !m_textValue
->GetValue().ToULong(&val
) )
344 m_gauge
->SetValue(val
);
347 void GaugeWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
350 event
.Enable( m_textValue
->GetValue().ToULong(&val
) && (val
<= m_range
) );
353 void GaugeWidgetsPage::OnUpdateUIRangeButton(wxUpdateUIEvent
& event
)
356 event
.Enable( m_textRange
->GetValue().ToULong(&val
) );
359 void GaugeWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
361 event
.Enable( m_chkVert
->GetValue() || m_chkSmooth
->GetValue() );
364 void GaugeWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)
369 void GaugeWidgetsPage::OnProgressTimer(wxTimerEvent
& WXUNUSED(event
))
371 int val
= m_gauge
->GetValue();
372 if ( (unsigned)val
< m_range
)
374 m_gauge
->SetValue(val
+ 1);
376 else // reached the end
382 void GaugeWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
384 event
.SetText( wxString::Format(_T("%d"), m_gauge
->GetValue()));
387 void GaugeWidgetsPage::StopTimer()
389 wxCHECK_RET( m_timer
, _T("shouldn't be called") );
395 wxButton
*btn
= (wxButton
*)FindWindow(GaugePage_Progress
);
396 wxCHECK_RET( btn
, _T("no progress button?") );
398 btn
->SetLabel(_T("Simulate &progress"));
400 wxLogMessage(_T("Progress finished."));