1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing toggle control
5 // Author: Dimitri Schoolwerth, Vadim Zeitlin
6 // Created: 27 Sep 2003
8 // Copyright: (c) 2006 Wlodzmierz Skiba
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/tglbtn.h"
33 // for all others, include the necessary headers
35 #include "wx/button.h"
36 #include "wx/checkbox.h"
37 #include "wx/radiobox.h"
38 #include "wx/statbox.h"
39 #include "wx/textctrl.h"
42 #include "wx/artprov.h"
44 #include "wx/dcmemory.h"
46 #include "icons/toggle.xpm"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
55 TogglePage_Reset
= wxID_HIGHEST
,
56 TogglePage_ChangeLabel
,
83 // ----------------------------------------------------------------------------
84 // CheckBoxWidgetsPage
85 // ----------------------------------------------------------------------------
87 class ToggleWidgetsPage
: public WidgetsPage
90 ToggleWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
91 virtual ~ToggleWidgetsPage(){};
93 virtual wxControl
*GetWidget() const { return m_toggle
; }
94 virtual void RecreateWidget() { CreateToggle(); }
96 // lazy creation of the content
97 virtual void CreateContent();
101 void OnCheckOrRadioBox(wxCommandEvent
& event
);
104 void OnButtonReset(wxCommandEvent
& event
);
105 void OnButtonChangeLabel(wxCommandEvent
& event
);
107 // reset the toggle parameters
110 // (re)create the toggle
113 // helper function: create a bitmap for wxBitmapToggleButton
114 wxBitmap
CreateBitmap(const wxString
& label
);
120 wxCheckBox
*m_chkUseMarkup
;
121 #endif // wxUSE_MARKUP
122 #ifdef wxHAS_BITMAPTOGGLEBUTTON
123 // the check/radio boxes for styles
124 wxCheckBox
*m_chkBitmapOnly
,
127 *m_chkUseBitmapClass
;
129 // more checkboxes for wxBitmapToggleButton only
130 wxCheckBox
*m_chkUsePressed
,
135 // and an image position choice used if m_chkTextAndBitmap is on
136 wxRadioBox
*m_radioImagePos
;
138 wxRadioBox
*m_radioHAlign
,
140 #endif // wxHAS_BITMAPTOGGLEBUTTON
142 // the checkbox itself and the sizer it is in
143 #ifdef wxHAS_ANY_BUTTON
144 wxToggleButton
*m_toggle
;
146 wxToggleButtonBase
*m_toggle
;
147 #endif // wxHAS_ANY_BUTTON
148 wxSizer
*m_sizerToggle
;
150 // the text entries for command parameters
151 wxTextCtrl
*m_textLabel
;
154 DECLARE_EVENT_TABLE()
155 DECLARE_WIDGETS_PAGE(ToggleWidgetsPage
)
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 BEGIN_EVENT_TABLE(ToggleWidgetsPage
, WidgetsPage
)
163 EVT_BUTTON(TogglePage_Reset
, ToggleWidgetsPage::OnButtonReset
)
164 EVT_BUTTON(TogglePage_ChangeLabel
, ToggleWidgetsPage::OnButtonChangeLabel
)
166 EVT_CHECKBOX(wxID_ANY
, ToggleWidgetsPage::OnCheckOrRadioBox
)
167 EVT_RADIOBOX(wxID_ANY
, ToggleWidgetsPage::OnCheckOrRadioBox
)
170 // ============================================================================
172 // ============================================================================
174 #if defined(__WXUNIVERSAL__)
175 #define FAMILY_CTRLS UNIVERSAL_CTRLS
177 #define FAMILY_CTRLS NATIVE_CTRLS
180 IMPLEMENT_WIDGETS_PAGE(ToggleWidgetsPage
, wxT("ToggleButton"),
184 ToggleWidgetsPage::ToggleWidgetsPage(WidgetsBookCtrl
*book
,
185 wxImageList
*imaglist
)
186 :WidgetsPage(book
, imaglist
, toggle_xpm
)
189 m_chkUseMarkup
= (wxCheckBox
*)NULL
;
190 #endif // wxUSE_MARKUP
191 #ifdef wxHAS_BITMAPTOGGLEBUTTON
196 m_chkUseBitmapClass
=
200 m_chkUseDisabled
= (wxCheckBox
*)NULL
;
204 m_radioVAlign
= (wxRadioBox
*)NULL
;
205 #endif // wxHAS_BITMAPTOGGLEBUTTON
207 m_textLabel
= (wxTextCtrl
*)NULL
;
209 m_toggle
= (wxToggleButton
*)NULL
;
210 m_sizerToggle
= (wxSizer
*)NULL
;
213 void ToggleWidgetsPage::CreateContent()
215 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
218 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("Styles"));
220 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
222 #ifdef wxHAS_BITMAPTOGGLEBUTTON
223 m_chkBitmapOnly
= CreateCheckBoxAndAddToSizer(sizerLeft
, "&Bitmap only");
224 m_chkTextAndBitmap
= CreateCheckBoxAndAddToSizer(sizerLeft
, "Text &and bitmap");
225 m_chkFit
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Fit exactly"));
226 #endif // wxHAS_BITMAPTOGGLEBUTTON
228 m_chkUseMarkup
= CreateCheckBoxAndAddToSizer(sizerLeft
, "Interpret &markup");
229 #endif // wxUSE_MARKUP
231 #ifdef wxHAS_BITMAPTOGGLEBUTTON
232 m_chkUseBitmapClass
= CreateCheckBoxAndAddToSizer(sizerLeft
,
233 "Use wxBitmapToggleButton");
234 m_chkUseBitmapClass
->SetValue(true);
236 sizerLeft
->AddSpacer(5);
238 wxSizer
*sizerUseLabels
=
239 new wxStaticBoxSizer(wxVERTICAL
, this,
240 "&Use the following bitmaps in addition to the normal one?");
241 m_chkUsePressed
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
242 "&Pressed (small help icon)");
243 m_chkUseFocused
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
244 "&Focused (small error icon)");
245 m_chkUseCurrent
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
246 "&Current (small warning icon)");
247 m_chkUseDisabled
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
248 "&Disabled (broken image icon)");
249 sizerLeft
->Add(sizerUseLabels
, wxSizerFlags().Expand().Border());
251 sizerLeft
->AddSpacer(10);
253 static const wxString dirs
[] =
255 "left", "right", "top", "bottom",
257 m_radioImagePos
= new wxRadioBox(this, wxID_ANY
, "Image &position",
258 wxDefaultPosition
, wxDefaultSize
,
259 WXSIZEOF(dirs
), dirs
);
260 sizerLeft
->Add(m_radioImagePos
, 0, wxGROW
| wxALL
, 5);
261 sizerLeft
->AddSpacer(15);
263 // should be in sync with enums Toggle[HV]Align!
264 static const wxString halign
[] =
271 static const wxString valign
[] =
278 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, wxT("&Horz alignment"),
279 wxDefaultPosition
, wxDefaultSize
,
280 WXSIZEOF(halign
), halign
);
281 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, wxT("&Vert alignment"),
282 wxDefaultPosition
, wxDefaultSize
,
283 WXSIZEOF(valign
), valign
);
285 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
286 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
287 #endif // wxHAS_BITMAPTOGGLEBUTTON
289 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
291 wxButton
*btn
= new wxButton(this, TogglePage_Reset
, wxT("&Reset"));
292 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
295 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, wxT("&Operations"));
296 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
298 wxSizer
*sizerRow
= CreateSizerWithTextAndButton(TogglePage_ChangeLabel
,
302 m_textLabel
->SetValue(wxT("&Toggle me!"));
304 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
307 m_sizerToggle
= new wxBoxSizer(wxHORIZONTAL
);
308 m_sizerToggle
->SetMinSize(150, 0);
310 // the 3 panes panes compose the window
311 sizerTop
->Add(sizerLeft
, 0, (wxALL
& ~wxLEFT
), 10);
312 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
313 sizerTop
->Add(m_sizerToggle
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
315 // do create the main control
322 void ToggleWidgetsPage::Reset()
324 #ifdef wxHAS_BITMAPTOGGLEBUTTON
325 m_chkBitmapOnly
->SetValue(false);
326 m_chkFit
->SetValue(true);
327 m_chkTextAndBitmap
->SetValue(false);
329 m_chkUseMarkup
->SetValue(false);
330 #endif // wxUSE_MARKUP
331 m_chkUseBitmapClass
->SetValue(true);
333 m_chkUsePressed
->SetValue(true);
334 m_chkUseFocused
->SetValue(true);
335 m_chkUseCurrent
->SetValue(true);
336 m_chkUseDisabled
->SetValue(true);
338 m_radioImagePos
->SetSelection(ToggleImagePos_Left
);
339 m_radioHAlign
->SetSelection(ToggleHAlign_Centre
);
340 m_radioVAlign
->SetSelection(ToggleVAlign_Centre
);
341 #endif // wxHAS_BITMAPTOGGLEBUTTON
345 m_toggle
->SetValue(false);
349 void ToggleWidgetsPage::CreateToggle()
356 label
= m_toggle
->GetLabel();
357 value
= m_toggle
->GetValue();
358 size_t count
= m_sizerToggle
->GetChildren().GetCount();
359 for ( size_t n
= 0; n
< count
; n
++ )
361 m_sizerToggle
->Remove(0);
369 // creating for the first time or recreating a toggle button after bitmap
371 label
= m_textLabel
->GetValue();
374 int flags
= ms_defaultFlags
;
375 #ifdef wxHAS_BITMAPTOGGLEBUTTON
376 switch ( m_radioHAlign
->GetSelection() )
378 case ToggleHAlign_Left
:
383 wxFAIL_MSG(wxT("unexpected radiobox selection"));
386 case ToggleHAlign_Centre
:
389 case ToggleHAlign_Right
:
394 switch ( m_radioVAlign
->GetSelection() )
396 case ToggleVAlign_Top
:
401 wxFAIL_MSG(wxT("unexpected radiobox selection"));
404 case ToggleVAlign_Centre
:
405 // centre vertical alignment is the default (no style)
408 case ToggleVAlign_Bottom
:
409 flags
|= wxBU_BOTTOM
;
412 #endif // wxHAS_BITMAPTOGGLEBUTTON
414 #ifdef wxHAS_BITMAPTOGGLEBUTTON
415 bool showsBitmap
= false;
416 if ( m_chkBitmapOnly
->GetValue() )
420 wxToggleButton
*btgl
;
421 if ( m_chkUseBitmapClass
->GetValue() )
423 btgl
= new wxBitmapToggleButton(this, TogglePage_Picker
,
424 CreateBitmap(wxT("normal")));
428 btgl
= new wxToggleButton(this, TogglePage_Picker
, wxT(""));
429 btgl
->SetBitmapLabel(CreateBitmap(wxT("normal")));
431 #ifdef wxHAS_ANY_BUTTON
432 if ( m_chkUsePressed
->GetValue() )
433 btgl
->SetBitmapPressed(CreateBitmap(wxT("pushed")));
434 if ( m_chkUseFocused
->GetValue() )
435 btgl
->SetBitmapFocus(CreateBitmap(wxT("focused")));
436 if ( m_chkUseCurrent
->GetValue() )
437 btgl
->SetBitmapCurrent(CreateBitmap(wxT("hover")));
438 if ( m_chkUseDisabled
->GetValue() )
439 btgl
->SetBitmapDisabled(CreateBitmap(wxT("disabled")));
440 #endif // wxHAS_ANY_BUTTON
443 else // normal button
444 #endif // wxHAS_BITMAPTOGGLEBUTTON
446 m_toggle
= new wxToggleButton(this, TogglePage_Picker
, label
,
447 wxDefaultPosition
, wxDefaultSize
,
450 m_toggle
->SetValue(value
);
452 #ifdef wxHAS_BITMAPTOGGLEBUTTON
453 #ifdef wxHAS_ANY_BUTTON
454 if ( !showsBitmap
&& m_chkTextAndBitmap
->GetValue() )
458 static const wxDirection positions
[] =
460 wxLEFT
, wxRIGHT
, wxTOP
, wxBOTTOM
463 m_toggle
->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_BUTTON
),
464 positions
[m_radioImagePos
->GetSelection()]);
466 if ( m_chkUsePressed
->GetValue() )
467 m_toggle
->SetBitmapPressed(wxArtProvider::GetIcon(wxART_HELP
, wxART_BUTTON
));
468 if ( m_chkUseFocused
->GetValue() )
469 m_toggle
->SetBitmapFocus(wxArtProvider::GetIcon(wxART_ERROR
, wxART_BUTTON
));
470 if ( m_chkUseCurrent
->GetValue() )
471 m_toggle
->SetBitmapCurrent(wxArtProvider::GetIcon(wxART_WARNING
, wxART_BUTTON
));
472 if ( m_chkUseDisabled
->GetValue() )
473 m_toggle
->SetBitmapDisabled(wxArtProvider::GetIcon(wxART_MISSING_IMAGE
, wxART_BUTTON
));
475 #endif // wxHAS_ANY_BUTTON
477 m_chkUseBitmapClass
->Enable(showsBitmap
);
479 m_chkUsePressed
->Enable(showsBitmap
);
480 m_chkUseFocused
->Enable(showsBitmap
);
481 m_chkUseCurrent
->Enable(showsBitmap
);
482 m_chkUseDisabled
->Enable(showsBitmap
);
483 #endif // wxHAS_BITMAPTOGGLEBUTTON
485 m_sizerToggle
->Add(0, 0, 1, wxCENTRE
);
486 m_sizerToggle
->Add(m_toggle
, 1, wxCENTRE
);
487 m_sizerToggle
->Add(0, 0, 1, wxCENTRE
);
488 m_sizerToggle
->Layout();
491 // ----------------------------------------------------------------------------
493 // ----------------------------------------------------------------------------
495 void ToggleWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
502 void ToggleWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
507 void ToggleWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
509 const wxString labelText
= m_textLabel
->GetValue();
512 if ( m_chkUseMarkup
->GetValue() )
513 m_toggle
->SetLabelMarkup(labelText
);
515 #endif // wxUSE_MARKUP
516 m_toggle
->SetLabel(labelText
);
519 #ifdef wxHAS_BITMAPTOGGLEBUTTON
520 // ----------------------------------------------------------------------------
521 // bitmap toggle button stuff
522 // ----------------------------------------------------------------------------
524 wxBitmap
ToggleWidgetsPage::CreateBitmap(const wxString
& label
)
526 wxBitmap
bmp(180, 70); // shouldn't hardcode but it's simpler like this
528 dc
.SelectObject(bmp
);
529 dc
.SetBackground(wxBrush(*wxCYAN
));
531 dc
.SetTextForeground(*wxBLACK
);
532 dc
.DrawLabel(wxStripMenuCodes(m_textLabel
->GetValue()) + wxT("\n")
533 wxT("(") + label
+ wxT(" state)"),
534 wxArtProvider::GetBitmap(wxART_INFORMATION
),
535 wxRect(10, 10, bmp
.GetWidth() - 20, bmp
.GetHeight() - 20),
540 #endif // wxHAS_BITMAPTOGGLEBUTTON
542 #endif // wxUSE_TOGGLEBTN