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(); }
80 // lazy creation of the content
81 virtual void CreateContent();
85 void OnButtonReset(wxCommandEvent
& event
);
86 void OnButtonProgress(wxCommandEvent
& event
);
87 void OnButtonClear(wxCommandEvent
& event
);
88 void OnButtonSetValue(wxCommandEvent
& event
);
89 void OnButtonSetRange(wxCommandEvent
& event
);
91 void OnCheckOrRadioBox(wxCommandEvent
& event
);
93 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
94 void OnUpdateUIRangeButton(wxUpdateUIEvent
& event
);
95 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
97 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
99 void OnProgressTimer(wxTimerEvent
& event
);
101 // reset the gauge parameters
104 // (re)create the gauge
107 // stop the progress timer
111 unsigned long m_range
;
116 // the checkboxes for styles
117 wxCheckBox
*m_chkVert
,
121 // the gauge itself and the sizer it is in
123 wxSizer
*m_sizerGauge
;
125 // the text entries for set value/range
126 wxTextCtrl
*m_textValue
,
129 // the timer for simulating gauge progress
133 DECLARE_EVENT_TABLE()
134 DECLARE_WIDGETS_PAGE(GaugeWidgetsPage
)
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 BEGIN_EVENT_TABLE(GaugeWidgetsPage
, WidgetsPage
)
142 EVT_BUTTON(GaugePage_Reset
, GaugeWidgetsPage::OnButtonReset
)
143 EVT_BUTTON(GaugePage_Progress
, GaugeWidgetsPage::OnButtonProgress
)
144 EVT_BUTTON(GaugePage_Clear
, GaugeWidgetsPage::OnButtonClear
)
145 EVT_BUTTON(GaugePage_SetValue
, GaugeWidgetsPage::OnButtonSetValue
)
146 EVT_BUTTON(GaugePage_SetRange
, GaugeWidgetsPage::OnButtonSetRange
)
148 EVT_UPDATE_UI(GaugePage_SetValue
, GaugeWidgetsPage::OnUpdateUIValueButton
)
149 EVT_UPDATE_UI(GaugePage_SetRange
, GaugeWidgetsPage::OnUpdateUIRangeButton
)
150 EVT_UPDATE_UI(GaugePage_Reset
, GaugeWidgetsPage::OnUpdateUIResetButton
)
152 EVT_UPDATE_UI(GaugePage_CurValueText
, GaugeWidgetsPage::OnUpdateUICurValueText
)
154 EVT_CHECKBOX(wxID_ANY
, GaugeWidgetsPage::OnCheckOrRadioBox
)
155 EVT_RADIOBOX(wxID_ANY
, GaugeWidgetsPage::OnCheckOrRadioBox
)
157 EVT_TIMER(GaugePage_Timer
, GaugeWidgetsPage::OnProgressTimer
)
160 // ============================================================================
162 // ============================================================================
164 #if defined(__WXUNIVERSAL__)
165 #define FAMILY_CTRLS UNIVERSAL_CTRLS
167 #define FAMILY_CTRLS NATIVE_CTRLS
170 IMPLEMENT_WIDGETS_PAGE(GaugeWidgetsPage
, _T("Gauge"), FAMILY_CTRLS
);
172 GaugeWidgetsPage::GaugeWidgetsPage(WidgetsBookCtrl
*book
,
173 wxImageList
*imaglist
)
174 :WidgetsPage(book
, imaglist
, gauge_xpm
)
179 m_timer
= (wxTimer
*)NULL
;
183 m_chkIndeterminate
= (wxCheckBox
*)NULL
;
185 m_gauge
= (wxGauge
*)NULL
;
186 m_sizerGauge
= (wxSizer
*)NULL
;
189 void GaugeWidgetsPage::CreateContent()
191 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
194 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
196 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
198 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
199 m_chkSmooth
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Smooth"));
200 m_chkIndeterminate
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Indeterminate"));
202 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
204 wxButton
*btn
= new wxButton(this, GaugePage_Reset
, _T("&Reset"));
205 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
208 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
209 _T("&Change gauge value"));
210 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
213 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
214 GaugePage_CurValueText
,
216 text
->SetEditable(false);
218 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
220 sizerRow
= CreateSizerWithTextAndButton(GaugePage_SetValue
,
224 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
226 sizerRow
= CreateSizerWithTextAndButton(GaugePage_SetRange
,
230 m_textRange
->SetValue( wxString::Format(_T("%lu"), m_range
) );
231 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
233 btn
= new wxButton(this, GaugePage_Progress
, _T("Simulate &progress"));
234 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
236 btn
= new wxButton(this, GaugePage_Clear
, _T("&Clear"));
237 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
240 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
241 m_gauge
= new wxGauge(this, GaugePage_Gauge
, m_range
);
242 sizerRight
->Add(m_gauge
, 1, wxCENTRE
| wxALL
, 5);
243 sizerRight
->SetMinSize(150, 0);
244 m_sizerGauge
= sizerRight
; // save it to modify it later
246 // the 3 panes panes compose the window
247 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
248 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
249 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
251 // final initializations
259 GaugeWidgetsPage::~GaugeWidgetsPage()
264 // ----------------------------------------------------------------------------
266 // ----------------------------------------------------------------------------
268 void GaugeWidgetsPage::Reset()
270 m_chkVert
->SetValue(false);
271 m_chkSmooth
->SetValue(false);
272 m_chkIndeterminate
->SetValue(false);
275 void GaugeWidgetsPage::CreateGauge()
277 int flags
= ms_defaultFlags
;
279 if ( m_chkVert
->GetValue() )
280 flags
|= wxGA_VERTICAL
;
282 flags
|= wxGA_HORIZONTAL
;
284 if ( m_chkSmooth
->GetValue() )
285 flags
|= wxGA_SMOOTH
;
290 val
= m_gauge
->GetValue();
292 m_sizerGauge
->Detach( m_gauge
);
296 m_gauge
= new wxGauge(this, GaugePage_Gauge
, m_range
,
297 wxDefaultPosition
, wxDefaultSize
,
299 m_gauge
->SetValue(val
);
301 if ( m_chkIndeterminate
->GetValue() ){
305 if ( flags
& wxGA_VERTICAL
)
306 m_sizerGauge
->Add(m_gauge
, 0, wxGROW
| wxALL
, 5);
308 m_sizerGauge
->Add(m_gauge
, 1, wxCENTRE
| wxALL
, 5);
310 m_sizerGauge
->Layout();
313 // ----------------------------------------------------------------------------
315 // ----------------------------------------------------------------------------
317 void GaugeWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
324 void GaugeWidgetsPage::OnButtonProgress(wxCommandEvent
& event
)
328 static const int INTERVAL
= 300;
330 wxLogMessage(_T("Launched progress timer (interval = %d ms)"), INTERVAL
);
332 m_timer
= new wxTimer(this, GaugePage_Timer
);
333 m_timer
->Start(INTERVAL
);
335 wxButton
*btn
= (wxButton
*)event
.GetEventObject();
336 btn
->SetLabel(_T("&Stop timer"));
338 else // stop the running timer
342 wxLogMessage(_T("Stopped the timer."));
346 void GaugeWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
348 m_gauge
->SetValue(0);
351 void GaugeWidgetsPage::OnButtonSetRange(wxCommandEvent
& WXUNUSED(event
))
354 if ( !m_textRange
->GetValue().ToULong(&val
) )
358 m_gauge
->SetRange(val
);
359 m_chkIndeterminate
->SetValue(0);
362 void GaugeWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
365 if ( !m_textValue
->GetValue().ToULong(&val
) )
368 m_gauge
->SetValue(val
);
369 m_chkIndeterminate
->SetValue(0);
372 void GaugeWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
375 event
.Enable( m_textValue
->GetValue().ToULong(&val
) && (val
<= m_range
) );
378 void GaugeWidgetsPage::OnUpdateUIRangeButton(wxUpdateUIEvent
& event
)
381 event
.Enable( m_textRange
->GetValue().ToULong(&val
) );
384 void GaugeWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
386 event
.Enable( m_chkVert
->GetValue() || m_chkSmooth
->GetValue() );
389 void GaugeWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
394 void GaugeWidgetsPage::OnProgressTimer(wxTimerEvent
& WXUNUSED(event
))
396 int val
= m_gauge
->GetValue();
397 if ( (unsigned)val
< m_range
)
399 m_gauge
->SetValue(val
+ 1);
401 else // reached the end
407 void GaugeWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
409 event
.SetText( wxString::Format(_T("%d"), m_gauge
->GetValue()));
412 void GaugeWidgetsPage::StopTimer()
414 wxCHECK_RET( m_timer
, _T("shouldn't be called") );
420 wxButton
*btn
= (wxButton
*)FindWindow(GaugePage_Progress
);
421 wxCHECK_RET( btn
, _T("no progress button?") );
423 btn
->SetLabel(_T("Simulate &progress"));
425 wxLogMessage(_T("Progress finished."));