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/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(wxNotebook
*notebook
, wxImageList
*imaglist
);
75 virtual ~GaugeWidgetsPage();
79 void OnButtonReset(wxCommandEvent
& event
);
80 void OnButtonProgress(wxCommandEvent
& event
);
81 void OnButtonClear(wxCommandEvent
& event
);
82 void OnButtonSetValue(wxCommandEvent
& event
);
83 void OnButtonSetRange(wxCommandEvent
& event
);
85 void OnCheckOrRadioBox(wxCommandEvent
& event
);
87 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
88 void OnUpdateUIRangeButton(wxUpdateUIEvent
& event
);
89 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
91 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
93 void OnProgressTimer(wxTimerEvent
& event
);
95 // reset the gauge parameters
98 // (re)create the gauge
101 // stop the progress timer
105 unsigned long m_range
;
110 // the checkboxes for styles
111 wxCheckBox
*m_chkVert
,
114 // the gauge itself and the sizer it is in
116 wxSizer
*m_sizerGauge
;
118 // the text entries for set value/range
119 wxTextCtrl
*m_textValue
,
122 // the timer for simulating gauge progress
126 DECLARE_EVENT_TABLE()
127 DECLARE_WIDGETS_PAGE(GaugeWidgetsPage
)
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 BEGIN_EVENT_TABLE(GaugeWidgetsPage
, WidgetsPage
)
135 EVT_BUTTON(GaugePage_Reset
, GaugeWidgetsPage::OnButtonReset
)
136 EVT_BUTTON(GaugePage_Progress
, GaugeWidgetsPage::OnButtonProgress
)
137 EVT_BUTTON(GaugePage_Clear
, GaugeWidgetsPage::OnButtonClear
)
138 EVT_BUTTON(GaugePage_SetValue
, GaugeWidgetsPage::OnButtonSetValue
)
139 EVT_BUTTON(GaugePage_SetRange
, GaugeWidgetsPage::OnButtonSetRange
)
141 EVT_UPDATE_UI(GaugePage_SetValue
, GaugeWidgetsPage::OnUpdateUIValueButton
)
142 EVT_UPDATE_UI(GaugePage_SetRange
, GaugeWidgetsPage::OnUpdateUIRangeButton
)
143 EVT_UPDATE_UI(GaugePage_Reset
, GaugeWidgetsPage::OnUpdateUIResetButton
)
145 EVT_UPDATE_UI(GaugePage_CurValueText
, GaugeWidgetsPage::OnUpdateUICurValueText
)
147 EVT_CHECKBOX(wxID_ANY
, GaugeWidgetsPage::OnCheckOrRadioBox
)
148 EVT_RADIOBOX(wxID_ANY
, GaugeWidgetsPage::OnCheckOrRadioBox
)
150 EVT_TIMER(GaugePage_Timer
, GaugeWidgetsPage::OnProgressTimer
)
153 // ============================================================================
155 // ============================================================================
157 IMPLEMENT_WIDGETS_PAGE(GaugeWidgetsPage
, _T("Gauge"));
159 GaugeWidgetsPage::GaugeWidgetsPage(wxNotebook
*notebook
,
160 wxImageList
*imaglist
)
161 : WidgetsPage(notebook
)
163 imaglist
->Add(wxBitmap(gauge_xpm
));
168 m_timer
= (wxTimer
*)NULL
;
171 m_chkSmooth
= (wxCheckBox
*)NULL
;
173 m_gauge
= (wxGauge
*)NULL
;
174 m_sizerGauge
= (wxSizer
*)NULL
;
176 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
179 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
181 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
183 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
184 m_chkSmooth
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Smooth"));
186 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
188 wxButton
*btn
= new wxButton(this, GaugePage_Reset
, _T("&Reset"));
189 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
192 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
193 _T("&Change gauge value"));
194 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
197 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
198 GaugePage_CurValueText
,
200 text
->SetEditable(false);
202 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
204 sizerRow
= CreateSizerWithTextAndButton(GaugePage_SetValue
,
208 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
210 sizerRow
= CreateSizerWithTextAndButton(GaugePage_SetRange
,
214 m_textRange
->SetValue( wxString::Format(_T("%lu"), m_range
) );
215 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
217 btn
= new wxButton(this, GaugePage_Progress
, _T("Simulate &progress"));
218 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
220 btn
= new wxButton(this, GaugePage_Clear
, _T("&Clear"));
221 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
224 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
225 m_gauge
= new wxGauge(this, GaugePage_Gauge
, m_range
);
226 sizerRight
->Add(m_gauge
, 1, wxCENTRE
| wxALL
, 5);
227 sizerRight
->SetMinSize(150, 0);
228 m_sizerGauge
= sizerRight
; // save it to modify it later
230 // the 3 panes panes compose the window
231 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
232 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
233 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
235 // final initializations
243 GaugeWidgetsPage::~GaugeWidgetsPage()
248 // ----------------------------------------------------------------------------
250 // ----------------------------------------------------------------------------
252 void GaugeWidgetsPage::Reset()
254 m_chkVert
->SetValue(false);
255 m_chkSmooth
->SetValue(false);
258 void GaugeWidgetsPage::CreateGauge()
262 if ( m_chkVert
->GetValue() )
263 flags
|= wxGA_VERTICAL
;
265 flags
|= wxGA_HORIZONTAL
;
267 if ( m_chkSmooth
->GetValue() )
268 flags
|= wxGA_SMOOTH
;
273 val
= m_gauge
->GetValue();
275 m_sizerGauge
->Detach( m_gauge
);
279 m_gauge
= new wxGauge(this, GaugePage_Gauge
, m_range
,
280 wxDefaultPosition
, wxDefaultSize
,
282 m_gauge
->SetValue(val
);
284 if ( flags
& wxGA_VERTICAL
)
285 m_sizerGauge
->Add(m_gauge
, 0, wxGROW
| wxALL
, 5);
287 m_sizerGauge
->Add(m_gauge
, 1, wxCENTRE
| wxALL
, 5);
289 m_sizerGauge
->Layout();
292 // ----------------------------------------------------------------------------
294 // ----------------------------------------------------------------------------
296 void GaugeWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
303 void GaugeWidgetsPage::OnButtonProgress(wxCommandEvent
& event
)
307 static const int INTERVAL
= 300;
309 wxLogMessage(_T("Launched progress timer (interval = %d ms)"), INTERVAL
);
311 m_timer
= new wxTimer(this, GaugePage_Timer
);
312 m_timer
->Start(INTERVAL
);
314 wxButton
*btn
= (wxButton
*)event
.GetEventObject();
315 btn
->SetLabel(_T("&Stop timer"));
317 else // stop the running timer
321 wxLogMessage(_T("Stopped the timer."));
325 void GaugeWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
327 m_gauge
->SetValue(0);
330 void GaugeWidgetsPage::OnButtonSetRange(wxCommandEvent
& WXUNUSED(event
))
333 if ( !m_textRange
->GetValue().ToULong(&val
) )
337 m_gauge
->SetRange(val
);
340 void GaugeWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
343 if ( !m_textValue
->GetValue().ToULong(&val
) )
346 m_gauge
->SetValue(val
);
349 void GaugeWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
352 event
.Enable( m_textValue
->GetValue().ToULong(&val
) && (val
<= m_range
) );
355 void GaugeWidgetsPage::OnUpdateUIRangeButton(wxUpdateUIEvent
& event
)
358 event
.Enable( m_textRange
->GetValue().ToULong(&val
) );
361 void GaugeWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
363 event
.Enable( m_chkVert
->GetValue() || m_chkSmooth
->GetValue() );
366 void GaugeWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
371 void GaugeWidgetsPage::OnProgressTimer(wxTimerEvent
& WXUNUSED(event
))
373 int val
= m_gauge
->GetValue();
374 if ( (unsigned)val
< m_range
)
376 m_gauge
->SetValue(val
+ 1);
378 else // reached the end
384 void GaugeWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
386 event
.SetText( wxString::Format(_T("%d"), m_gauge
->GetValue()));
389 void GaugeWidgetsPage::StopTimer()
391 wxCHECK_RET( m_timer
, _T("shouldn't be called") );
397 wxButton
*btn
= (wxButton
*)FindWindow(GaugePage_Progress
);
398 wxCHECK_RET( btn
, _T("no progress button?") );
400 btn
->SetLabel(_T("Simulate &progress"));
402 wxLogMessage(_T("Progress finished."));