1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/tglbtn.cpp
3 // Purpose: wxToggleButton
4 // Author: Vadim Zeitlin
5 // Modified by: David Bjorkevik
6 // Created: 16.05.06
7 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13
14 #if wxUSE_TOGGLEBTN
15
16 #include "wx/tglbtn.h"
17 #include "wx/univ/renderer.h"
18 #include "wx/univ/colschem.h"
19 #include "wx/univ/theme.h"
20
21 #include "wx/stockitem.h"
22
23 wxDEFINE_EVENT( wxEVT_TOGGLEBUTTON, wxCommandEvent );
24
25 wxIMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxToggleButtonBase);
26
wxToggleButton()27 wxToggleButton::wxToggleButton()
28 {
29 Init();
30 }
31
wxToggleButton(wxWindow * parent,wxWindowID id,const wxString & label,const wxPoint & pos,const wxSize & size,long style,const wxValidator & validator,const wxString & name)32 wxToggleButton::wxToggleButton(wxWindow *parent,
33 wxWindowID id,
34 const wxString& label,
35 const wxPoint& pos,
36 const wxSize& size,
37 long style,
38 const wxValidator& validator,
39 const wxString& name)
40 {
41 Init();
42 Create(parent, id, label, pos, size, style, validator, name);
43 }
44
Create(wxWindow * parent,wxWindowID id,const wxString & lbl,const wxPoint & pos,const wxSize & size,long style,const wxValidator & validator,const wxString & name)45 bool wxToggleButton::Create(wxWindow *parent,
46 wxWindowID id,
47 const wxString& lbl,
48 const wxPoint& pos,
49 const wxSize& size, long style,
50 const wxValidator& validator,
51 const wxString& name)
52 {
53 wxString label(lbl);
54 if (label.empty() && wxIsStockID(id))
55 label = wxGetStockLabel(id);
56
57 long ctrl_style = style & ~wxBU_ALIGN_MASK;
58 ctrl_style = ctrl_style & ~wxALIGN_MASK;
59
60 if((style & wxBU_RIGHT) == wxBU_RIGHT)
61 ctrl_style |= wxALIGN_RIGHT;
62 else if((style & wxBU_LEFT) == wxBU_LEFT)
63 ctrl_style |= wxALIGN_LEFT;
64 else
65 ctrl_style |= wxALIGN_CENTRE_HORIZONTAL;
66
67 if((style & wxBU_TOP) == wxBU_TOP)
68 ctrl_style |= wxALIGN_TOP;
69 else if((style & wxBU_BOTTOM) == wxBU_BOTTOM)
70 ctrl_style |= wxALIGN_BOTTOM;
71 else
72 ctrl_style |= wxALIGN_CENTRE_VERTICAL;
73
74 if ( !wxToggleButtonBase::Create(parent, id, pos, size, ctrl_style, validator, name) )
75 {
76 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
77 return false;
78 }
79 SetLabel(label);
80 CreateInputHandler(wxINP_HANDLER_BUTTON);
81 return true;
82 }
83
Init()84 void wxToggleButton::Init()
85 {
86 m_isPressed = false;
87 m_value = false;
88 }
89
Toggle()90 void wxToggleButton::Toggle()
91 {
92 if ( m_isPressed )
93 Release();
94 else
95 Press();
96
97 if ( !m_isPressed )
98 {
99 // releasing button after it had been pressed generates a click event
100 // and toggles value
101 m_value = !m_value;
102 Click();
103 }
104 }
105
Click()106 void wxToggleButton::Click()
107 {
108 wxCommandEvent event(wxEVT_TOGGLEBUTTON, GetId());
109 InitCommandEvent(event);
110 event.SetInt(GetValue());
111 Command(event);
112 }
113
SetValue(bool state)114 void wxToggleButton::SetValue(bool state)
115 {
116 m_value = state;
117 Refresh();
118 }
119
120 #endif // wxUSE_TOGGLEBTN
121