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
,
120 // the gauge itself and the sizer it is in
122 wxSizer
*m_sizerGauge
;
124 // the text entries for set value/range
125 wxTextCtrl
*m_textValue
,
128 // the timer for simulating gauge progress
132 DECLARE_EVENT_TABLE()
133 DECLARE_WIDGETS_PAGE(GaugeWidgetsPage
)
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
140 BEGIN_EVENT_TABLE(GaugeWidgetsPage
, WidgetsPage
)
141 EVT_BUTTON(GaugePage_Reset
, GaugeWidgetsPage::OnButtonReset
)
142 EVT_BUTTON(GaugePage_Progress
, GaugeWidgetsPage::OnButtonProgress
)
143 EVT_BUTTON(GaugePage_Clear
, GaugeWidgetsPage::OnButtonClear
)
144 EVT_BUTTON(GaugePage_SetValue
, GaugeWidgetsPage::OnButtonSetValue
)
145 EVT_BUTTON(GaugePage_SetRange
, GaugeWidgetsPage::OnButtonSetRange
)
147 EVT_UPDATE_UI(GaugePage_SetValue
, GaugeWidgetsPage::OnUpdateUIValueButton
)
148 EVT_UPDATE_UI(GaugePage_SetRange
, GaugeWidgetsPage::OnUpdateUIRangeButton
)
149 EVT_UPDATE_UI(GaugePage_Reset
, GaugeWidgetsPage::OnUpdateUIResetButton
)
151 EVT_UPDATE_UI(GaugePage_CurValueText
, GaugeWidgetsPage::OnUpdateUICurValueText
)
153 EVT_CHECKBOX(wxID_ANY
, GaugeWidgetsPage::OnCheckOrRadioBox
)
154 EVT_RADIOBOX(wxID_ANY
, GaugeWidgetsPage::OnCheckOrRadioBox
)
156 EVT_TIMER(GaugePage_Timer
, GaugeWidgetsPage::OnProgressTimer
)
159 // ============================================================================
161 // ============================================================================
163 #if defined(__WXUNIVERSAL__)
164 #define FAMILY_CTRLS UNIVERSAL_CTRLS
166 #define FAMILY_CTRLS NATIVE_CTRLS
169 IMPLEMENT_WIDGETS_PAGE(GaugeWidgetsPage
, _T("Gauge"), FAMILY_CTRLS
);
171 GaugeWidgetsPage::GaugeWidgetsPage(WidgetsBookCtrl
*book
,
172 wxImageList
*imaglist
)
173 :WidgetsPage(book
, imaglist
, gauge_xpm
)
178 m_timer
= (wxTimer
*)NULL
;
181 m_chkSmooth
= (wxCheckBox
*)NULL
;
183 m_gauge
= (wxGauge
*)NULL
;
184 m_sizerGauge
= (wxSizer
*)NULL
;
187 void GaugeWidgetsPage::CreateContent()
189 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
192 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
194 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
196 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
197 m_chkSmooth
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Smooth"));
199 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
201 wxButton
*btn
= new wxButton(this, GaugePage_Reset
, _T("&Reset"));
202 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
205 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
206 _T("&Change gauge value"));
207 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
210 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
211 GaugePage_CurValueText
,
213 text
->SetEditable(false);
215 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
217 sizerRow
= CreateSizerWithTextAndButton(GaugePage_SetValue
,
221 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
223 sizerRow
= CreateSizerWithTextAndButton(GaugePage_SetRange
,
227 m_textRange
->SetValue( wxString::Format(_T("%lu"), m_range
) );
228 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
230 btn
= new wxButton(this, GaugePage_Progress
, _T("Simulate &progress"));
231 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
233 btn
= new wxButton(this, GaugePage_Clear
, _T("&Clear"));
234 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
237 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
238 m_gauge
= new wxGauge(this, GaugePage_Gauge
, m_range
);
239 sizerRight
->Add(m_gauge
, 1, wxCENTRE
| wxALL
, 5);
240 sizerRight
->SetMinSize(150, 0);
241 m_sizerGauge
= sizerRight
; // save it to modify it later
243 // the 3 panes panes compose the window
244 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
245 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
246 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
248 // final initializations
256 GaugeWidgetsPage::~GaugeWidgetsPage()
261 // ----------------------------------------------------------------------------
263 // ----------------------------------------------------------------------------
265 void GaugeWidgetsPage::Reset()
267 m_chkVert
->SetValue(false);
268 m_chkSmooth
->SetValue(false);
271 void GaugeWidgetsPage::CreateGauge()
273 int flags
= ms_defaultFlags
;
275 if ( m_chkVert
->GetValue() )
276 flags
|= wxGA_VERTICAL
;
278 flags
|= wxGA_HORIZONTAL
;
280 if ( m_chkSmooth
->GetValue() )
281 flags
|= wxGA_SMOOTH
;
286 val
= m_gauge
->GetValue();
288 m_sizerGauge
->Detach( m_gauge
);
292 m_gauge
= new wxGauge(this, GaugePage_Gauge
, m_range
,
293 wxDefaultPosition
, wxDefaultSize
,
295 m_gauge
->SetValue(val
);
297 if ( flags
& wxGA_VERTICAL
)
298 m_sizerGauge
->Add(m_gauge
, 0, wxGROW
| wxALL
, 5);
300 m_sizerGauge
->Add(m_gauge
, 1, wxCENTRE
| wxALL
, 5);
302 m_sizerGauge
->Layout();
305 // ----------------------------------------------------------------------------
307 // ----------------------------------------------------------------------------
309 void GaugeWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
316 void GaugeWidgetsPage::OnButtonProgress(wxCommandEvent
& event
)
320 static const int INTERVAL
= 300;
322 wxLogMessage(_T("Launched progress timer (interval = %d ms)"), INTERVAL
);
324 m_timer
= new wxTimer(this, GaugePage_Timer
);
325 m_timer
->Start(INTERVAL
);
327 wxButton
*btn
= (wxButton
*)event
.GetEventObject();
328 btn
->SetLabel(_T("&Stop timer"));
330 else // stop the running timer
334 wxLogMessage(_T("Stopped the timer."));
338 void GaugeWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
340 m_gauge
->SetValue(0);
343 void GaugeWidgetsPage::OnButtonSetRange(wxCommandEvent
& WXUNUSED(event
))
346 if ( !m_textRange
->GetValue().ToULong(&val
) )
350 m_gauge
->SetRange(val
);
353 void GaugeWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
356 if ( !m_textValue
->GetValue().ToULong(&val
) )
359 m_gauge
->SetValue(val
);
362 void GaugeWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
365 event
.Enable( m_textValue
->GetValue().ToULong(&val
) && (val
<= m_range
) );
368 void GaugeWidgetsPage::OnUpdateUIRangeButton(wxUpdateUIEvent
& event
)
371 event
.Enable( m_textRange
->GetValue().ToULong(&val
) );
374 void GaugeWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
376 event
.Enable( m_chkVert
->GetValue() || m_chkSmooth
->GetValue() );
379 void GaugeWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
384 void GaugeWidgetsPage::OnProgressTimer(wxTimerEvent
& WXUNUSED(event
))
386 int val
= m_gauge
->GetValue();
387 if ( (unsigned)val
< m_range
)
389 m_gauge
->SetValue(val
+ 1);
391 else // reached the end
397 void GaugeWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
399 event
.SetText( wxString::Format(_T("%d"), m_gauge
->GetValue()));
402 void GaugeWidgetsPage::StopTimer()
404 wxCHECK_RET( m_timer
, _T("shouldn't be called") );
410 wxButton
*btn
= (wxButton
*)FindWindow(GaugePage_Progress
);
411 wxCHECK_RET( btn
, _T("no progress button?") );
413 btn
->SetLabel(_T("Simulate &progress"));
415 wxLogMessage(_T("Progress finished."));