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
,
60 GaugePage_CurValueText
,
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 class GaugeWidgetsPage
: public WidgetsPage
74 GaugeWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
75 virtual ~GaugeWidgetsPage();
77 virtual wxControl
*GetWidget() const { return m_gauge
; }
78 virtual void RecreateWidget() { CreateGauge(); }
82 void OnButtonReset(wxCommandEvent
& event
);
83 void OnButtonProgress(wxCommandEvent
& event
);
84 void OnButtonClear(wxCommandEvent
& event
);
85 void OnButtonSetValue(wxCommandEvent
& event
);
86 void OnButtonSetRange(wxCommandEvent
& event
);
88 void OnCheckOrRadioBox(wxCommandEvent
& event
);
90 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
91 void OnUpdateUIRangeButton(wxUpdateUIEvent
& event
);
92 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
94 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
96 void OnProgressTimer(wxTimerEvent
& event
);
98 // reset the gauge parameters
101 // (re)create the gauge
104 // stop the progress timer
108 unsigned long m_range
;
113 // the checkboxes for styles
114 wxCheckBox
*m_chkVert
,
117 // the gauge itself and the sizer it is in
119 wxSizer
*m_sizerGauge
;
121 // the text entries for set value/range
122 wxTextCtrl
*m_textValue
,
125 // the timer for simulating gauge progress
129 DECLARE_EVENT_TABLE()
130 DECLARE_WIDGETS_PAGE(GaugeWidgetsPage
)
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 BEGIN_EVENT_TABLE(GaugeWidgetsPage
, WidgetsPage
)
138 EVT_BUTTON(GaugePage_Reset
, GaugeWidgetsPage::OnButtonReset
)
139 EVT_BUTTON(GaugePage_Progress
, GaugeWidgetsPage::OnButtonProgress
)
140 EVT_BUTTON(GaugePage_Clear
, GaugeWidgetsPage::OnButtonClear
)
141 EVT_BUTTON(GaugePage_SetValue
, GaugeWidgetsPage::OnButtonSetValue
)
142 EVT_BUTTON(GaugePage_SetRange
, GaugeWidgetsPage::OnButtonSetRange
)
144 EVT_UPDATE_UI(GaugePage_SetValue
, GaugeWidgetsPage::OnUpdateUIValueButton
)
145 EVT_UPDATE_UI(GaugePage_SetRange
, GaugeWidgetsPage::OnUpdateUIRangeButton
)
146 EVT_UPDATE_UI(GaugePage_Reset
, GaugeWidgetsPage::OnUpdateUIResetButton
)
148 EVT_UPDATE_UI(GaugePage_CurValueText
, GaugeWidgetsPage::OnUpdateUICurValueText
)
150 EVT_CHECKBOX(wxID_ANY
, GaugeWidgetsPage::OnCheckOrRadioBox
)
151 EVT_RADIOBOX(wxID_ANY
, GaugeWidgetsPage::OnCheckOrRadioBox
)
153 EVT_TIMER(GaugePage_Timer
, GaugeWidgetsPage::OnProgressTimer
)
156 // ============================================================================
158 // ============================================================================
160 #if defined(__WXUNIVERSAL__)
161 #define FAMILY_CTRLS UNIVERSAL_CTRLS
163 #define FAMILY_CTRLS NATIVE_CTRLS
166 IMPLEMENT_WIDGETS_PAGE(GaugeWidgetsPage
, _T("Gauge"), FAMILY_CTRLS
);
168 GaugeWidgetsPage::GaugeWidgetsPage(WidgetsBookCtrl
*book
,
169 wxImageList
*imaglist
)
170 :WidgetsPage(book
, imaglist
, gauge_xpm
)
175 m_timer
= (wxTimer
*)NULL
;
178 m_chkSmooth
= (wxCheckBox
*)NULL
;
180 m_gauge
= (wxGauge
*)NULL
;
181 m_sizerGauge
= (wxSizer
*)NULL
;
183 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
186 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
188 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
190 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
191 m_chkSmooth
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Smooth"));
193 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
195 wxButton
*btn
= new wxButton(this, GaugePage_Reset
, _T("&Reset"));
196 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
199 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
200 _T("&Change gauge value"));
201 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
204 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
205 GaugePage_CurValueText
,
207 text
->SetEditable(false);
209 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
211 sizerRow
= CreateSizerWithTextAndButton(GaugePage_SetValue
,
215 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
217 sizerRow
= CreateSizerWithTextAndButton(GaugePage_SetRange
,
221 m_textRange
->SetValue( wxString::Format(_T("%lu"), m_range
) );
222 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
224 btn
= new wxButton(this, GaugePage_Progress
, _T("Simulate &progress"));
225 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
227 btn
= new wxButton(this, GaugePage_Clear
, _T("&Clear"));
228 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
231 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
232 m_gauge
= new wxGauge(this, GaugePage_Gauge
, m_range
);
233 sizerRight
->Add(m_gauge
, 1, wxCENTRE
| wxALL
, 5);
234 sizerRight
->SetMinSize(150, 0);
235 m_sizerGauge
= sizerRight
; // save it to modify it later
237 // the 3 panes panes compose the window
238 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
239 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
240 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
242 // final initializations
250 GaugeWidgetsPage::~GaugeWidgetsPage()
255 // ----------------------------------------------------------------------------
257 // ----------------------------------------------------------------------------
259 void GaugeWidgetsPage::Reset()
261 m_chkVert
->SetValue(false);
262 m_chkSmooth
->SetValue(false);
265 void GaugeWidgetsPage::CreateGauge()
267 int flags
= ms_defaultFlags
;
269 if ( m_chkVert
->GetValue() )
270 flags
|= wxGA_VERTICAL
;
272 flags
|= wxGA_HORIZONTAL
;
274 if ( m_chkSmooth
->GetValue() )
275 flags
|= wxGA_SMOOTH
;
280 val
= m_gauge
->GetValue();
282 m_sizerGauge
->Detach( m_gauge
);
286 m_gauge
= new wxGauge(this, GaugePage_Gauge
, m_range
,
287 wxDefaultPosition
, wxDefaultSize
,
289 m_gauge
->SetValue(val
);
291 if ( flags
& wxGA_VERTICAL
)
292 m_sizerGauge
->Add(m_gauge
, 0, wxGROW
| wxALL
, 5);
294 m_sizerGauge
->Add(m_gauge
, 1, wxCENTRE
| wxALL
, 5);
296 m_sizerGauge
->Layout();
299 // ----------------------------------------------------------------------------
301 // ----------------------------------------------------------------------------
303 void GaugeWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
310 void GaugeWidgetsPage::OnButtonProgress(wxCommandEvent
& event
)
314 static const int INTERVAL
= 300;
316 wxLogMessage(_T("Launched progress timer (interval = %d ms)"), INTERVAL
);
318 m_timer
= new wxTimer(this, GaugePage_Timer
);
319 m_timer
->Start(INTERVAL
);
321 wxButton
*btn
= (wxButton
*)event
.GetEventObject();
322 btn
->SetLabel(_T("&Stop timer"));
324 else // stop the running timer
328 wxLogMessage(_T("Stopped the timer."));
332 void GaugeWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
334 m_gauge
->SetValue(0);
337 void GaugeWidgetsPage::OnButtonSetRange(wxCommandEvent
& WXUNUSED(event
))
340 if ( !m_textRange
->GetValue().ToULong(&val
) )
344 m_gauge
->SetRange(val
);
347 void GaugeWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
350 if ( !m_textValue
->GetValue().ToULong(&val
) )
353 m_gauge
->SetValue(val
);
356 void GaugeWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
359 event
.Enable( m_textValue
->GetValue().ToULong(&val
) && (val
<= m_range
) );
362 void GaugeWidgetsPage::OnUpdateUIRangeButton(wxUpdateUIEvent
& event
)
365 event
.Enable( m_textRange
->GetValue().ToULong(&val
) );
368 void GaugeWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
370 event
.Enable( m_chkVert
->GetValue() || m_chkSmooth
->GetValue() );
373 void GaugeWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
378 void GaugeWidgetsPage::OnProgressTimer(wxTimerEvent
& WXUNUSED(event
))
380 int val
= m_gauge
->GetValue();
381 if ( (unsigned)val
< m_range
)
383 m_gauge
->SetValue(val
+ 1);
385 else // reached the end
391 void GaugeWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
393 event
.SetText( wxString::Format(_T("%d"), m_gauge
->GetValue()));
396 void GaugeWidgetsPage::StopTimer()
398 wxCHECK_RET( m_timer
, _T("shouldn't be called") );
404 wxButton
*btn
= (wxButton
*)FindWindow(GaugePage_Progress
);
405 wxCHECK_RET( btn
, _T("no progress button?") );
407 btn
->SetLabel(_T("Simulate &progress"));
409 wxLogMessage(_T("Progress finished."));