]> git.saurik.com Git - wxWidgets.git/blob - src/msw/tglbtn.cpp
Add a helper wxApp::GetValidTraits() method.
[wxWidgets.git] / src / msw / tglbtn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/tglbtn.cpp
3 // Purpose: Definition of the wxToggleButton class, which implements a
4 // toggle button under wxMSW.
5 // Author: John Norris, minor changes by Axel Schlueter
6 // and William Gallafent.
7 // Modified by:
8 // Created: 08.02.01
9 // RCS-ID: $Id$
10 // Copyright: (c) 2000 Johnny C. Norris II
11 // Licence: wxWindows licence
12 /////////////////////////////////////////////////////////////////////////////
13
14 // ============================================================================
15 // declarations
16 // ============================================================================
17
18 // ----------------------------------------------------------------------------
19 // headers
20 // ----------------------------------------------------------------------------
21
22 #include "wx/wxprec.h"
23
24 #ifdef __BORLANDC__
25 #pragma hdrstop
26 #endif
27
28 #if wxUSE_TOGGLEBTN
29
30 #include "wx/tglbtn.h"
31
32 #ifndef WX_PRECOMP
33 #include "wx/button.h"
34 #include "wx/brush.h"
35 #include "wx/dcscreen.h"
36 #include "wx/settings.h"
37
38 #include "wx/log.h"
39 #endif // WX_PRECOMP
40
41 #include "wx/msw/private.h"
42 #include "wx/msw/private/button.h"
43
44 // ----------------------------------------------------------------------------
45 // macros
46 // ----------------------------------------------------------------------------
47
48 wxDEFINE_EVENT( wxEVT_TOGGLEBUTTON, wxCommandEvent );
49
50 // ============================================================================
51 // implementation
52 // ============================================================================
53
54 //-----------------------------------------------------------------------------
55 // wxBitmapToggleButton
56 //-----------------------------------------------------------------------------
57
58 IMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton, wxToggleButton)
59
60 bool wxBitmapToggleButton::Create( wxWindow *parent, wxWindowID id,
61 const wxBitmap& label,const wxPoint& pos, const wxSize& size, long style,
62 const wxValidator& validator, const wxString& name )
63 {
64 if (!wxToggleButton::Create( parent, id, wxEmptyString, pos, size, style, validator, name ))
65 return false;
66
67 SetBitmap(label);
68
69 if (size.x == -1 || size.y == -1)
70 {
71 wxSize new_size = GetBestSize();
72 if (size.x != -1)
73 new_size.x = size.x;
74 if (size.y != -1)
75 new_size.y = size.y;
76 SetSize( new_size );
77 }
78
79 return true;
80 }
81
82
83 // ----------------------------------------------------------------------------
84 // wxToggleButton
85 // ----------------------------------------------------------------------------
86
87 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
88
89 void wxToggleButton::Init()
90 {
91 m_state = false;
92 }
93
94 // Single check box item
95 bool wxToggleButton::Create(wxWindow *parent,
96 wxWindowID id,
97 const wxString& label,
98 const wxPoint& pos,
99 const wxSize& size, long style,
100 const wxValidator& validator,
101 const wxString& name)
102 {
103 Init();
104
105 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
106 return false;
107
108 // if the label contains several lines we must explicitly tell the button
109 // about it or it wouldn't draw it correctly ("\n"s would just appear as
110 // black boxes)
111 //
112 // NB: we do it here and not in MSWGetStyle() because we need the label
113 // value and the label is not set yet when MSWGetStyle() is called
114 WXDWORD exstyle;
115 WXDWORD msStyle = MSWGetStyle(style, &exstyle);
116 msStyle |= wxMSWButton::GetMultilineStyle(label);
117
118 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, exstyle);
119 }
120
121 WXDWORD wxToggleButton::MSWGetStyle(long style, WXDWORD *exstyle) const
122 {
123 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
124
125 msStyle |= BS_AUTOCHECKBOX | BS_PUSHLIKE | WS_TABSTOP;
126
127 if ( style & wxBU_LEFT )
128 msStyle |= BS_LEFT;
129 if ( style & wxBU_RIGHT )
130 msStyle |= BS_RIGHT;
131 if ( style & wxBU_TOP )
132 msStyle |= BS_TOP;
133 if ( style & wxBU_BOTTOM )
134 msStyle |= BS_BOTTOM;
135
136 return msStyle;
137 }
138
139 void wxToggleButton::SetValue(bool val)
140 {
141 m_state = val;
142 if ( IsOwnerDrawn() )
143 {
144 Refresh();
145 }
146 else
147 {
148 ::SendMessage(GetHwnd(), BM_SETCHECK, val, 0);
149 }
150 }
151
152 bool wxToggleButton::GetValue() const
153 {
154 if ( IsOwnerDrawn() )
155 {
156 return m_state;
157 }
158 else
159 {
160 return ::SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) == BST_CHECKED;
161 }
162 }
163
164 void wxToggleButton::Command(wxCommandEvent& event)
165 {
166 SetValue(event.GetInt() != 0);
167 ProcessCommand(event);
168 }
169
170 bool wxToggleButton::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
171 {
172 if ( param != BN_CLICKED && param != BN_DBLCLK )
173 return false;
174
175 // first update the value so that user event handler gets the new
176 // toggle button value
177
178 // ownerdrawn buttons don't manage their state themselves unlike usual
179 // auto checkboxes so do it ourselves in any case
180 m_state = !m_state;
181
182 wxCommandEvent event(wxEVT_TOGGLEBUTTON, m_windowId);
183 event.SetInt(GetValue());
184 event.SetEventObject(this);
185 ProcessCommand(event);
186 return true;
187 }
188
189 wxAnyButton::State wxToggleButton::GetNormalState() const
190 {
191 if ( GetValue() )
192 return State_Pressed;
193 else
194 return State_Normal;
195 }
196
197 #endif // wxUSE_TOGGLEBTN