]> git.saurik.com Git - wxWidgets.git/blob - src/msw/checkbox.cpp
don't allow clicking disabled buttons
[wxWidgets.git] / src / msw / checkbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: checkbox.cpp
3 // Purpose: wxCheckBox
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "checkbox.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_CHECKBOX
32
33 #ifndef WX_PRECOMP
34 #include "wx/checkbox.h"
35 #include "wx/brush.h"
36 #include "wx/dcscreen.h"
37 #include "wx/settings.h"
38 #endif
39
40 #include "wx/msw/private.h"
41
42 // ----------------------------------------------------------------------------
43 // macros
44 // ----------------------------------------------------------------------------
45
46 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
47 IMPLEMENT_DYNAMIC_CLASS(wxBitmapCheckBox, wxCheckBox)
48
49 // ============================================================================
50 // implementation
51 // ============================================================================
52
53 // ----------------------------------------------------------------------------
54 // wxCheckBox
55 // ----------------------------------------------------------------------------
56
57 bool wxCheckBox::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id))
58 {
59 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, m_windowId);
60 event.SetInt(GetValue());
61 event.SetEventObject(this);
62 ProcessCommand(event);
63 return TRUE;
64 }
65
66 // Single check box item
67 bool wxCheckBox::Create(wxWindow *parent,
68 wxWindowID id,
69 const wxString& label,
70 const wxPoint& pos,
71 const wxSize& size, long style,
72 const wxValidator& validator,
73 const wxString& name)
74 {
75 SetName(name);
76 #if wxUSE_VALIDATORS
77 SetValidator(validator);
78 #endif // wxUSE_VALIDATORS
79 if (parent) parent->AddChild(this);
80
81 SetBackgroundColour(parent->GetBackgroundColour()) ;
82 SetForegroundColour(parent->GetForegroundColour()) ;
83
84 m_windowStyle = style;
85
86 // VZ: disabling this ugliness which completely breaks checkboxes in wxGrid
87 // whoever did it, please tell me where and how does the checkbox fail
88 // to appear
89 #if 0
90 wxString Label = label;
91 if (Label == wxT(""))
92 Label = wxT(" "); // Apparently needed or checkbox won't show
93 #endif // 0
94
95 if ( id == -1 )
96 m_windowId = NewControlId();
97 else
98 m_windowId = id;
99
100 int x = pos.x;
101 int y = pos.y;
102 int width = size.x;
103 int height = size.y;
104
105 long msStyle = BS_AUTOCHECKBOX | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
106 if ( style & wxALIGN_RIGHT )
107 msStyle |= BS_LEFTTEXT;
108
109 if ( style & wxCLIP_SIBLINGS )
110 msStyle |= WS_CLIPSIBLINGS;
111
112 // We perhaps have different concepts of 3D here - a 3D border,
113 // versus a 3D button.
114 // So we only wish to give a border if this is specified
115 // in the style.
116 bool want3D;
117 WXDWORD exStyle = Determine3DEffects(0, &want3D) ;
118
119 // Even with extended styles, need to combine with WS_BORDER
120 // for them to look right.
121 /*
122 if ( want3D || wxStyleHasBorder(m_windowStyle) )
123 msStyle |= WS_BORDER;
124 */
125
126 m_hWnd = (WXHWND)CreateWindowEx(exStyle, wxT("BUTTON"), label,
127 msStyle,
128 0, 0, 0, 0,
129 (HWND)parent->GetHWND(), (HMENU)m_windowId,
130 wxGetInstance(), NULL);
131
132 #if wxUSE_CTL3D
133 if (want3D)
134 {
135 Ctl3dSubclassCtl(GetHwnd());
136 m_useCtl3D = TRUE;
137 }
138 #endif
139
140 // Subclass again for purposes of dialog editing mode
141 SubclassWin(m_hWnd);
142
143 SetFont(parent->GetFont());
144
145 SetSize(x, y, width, height);
146
147 return TRUE;
148 }
149
150 void wxCheckBox::SetLabel(const wxString& label)
151 {
152 SetWindowText(GetHwnd(), label);
153 }
154
155 wxSize wxCheckBox::DoGetBestSize() const
156 {
157 static int s_checkSize = 0;
158
159 if ( !s_checkSize )
160 {
161 wxScreenDC dc;
162 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
163
164 s_checkSize = dc.GetCharHeight();
165 }
166
167 wxString str = wxGetWindowText(GetHWND());
168
169 int wCheckbox, hCheckbox;
170 if ( !str.IsEmpty() )
171 {
172 GetTextExtent(str, &wCheckbox, &hCheckbox);
173 wCheckbox += s_checkSize + GetCharWidth();
174
175 if ( hCheckbox < s_checkSize )
176 hCheckbox = s_checkSize;
177 }
178 else
179 {
180 wCheckbox = s_checkSize;
181 hCheckbox = s_checkSize;
182 }
183
184 return wxSize(wCheckbox, hCheckbox);
185 }
186
187 void wxCheckBox::SetValue(bool val)
188 {
189 SendMessage(GetHwnd(), BM_SETCHECK, val, 0);
190 }
191
192 #ifndef BST_CHECKED
193 #define BST_CHECKED 0x0001
194 #endif
195
196 bool wxCheckBox::GetValue() const
197 {
198 #ifdef __WIN32__
199 return (SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) == BST_CHECKED);
200 #else
201 return ((0x001 & SendMessage(GetHwnd(), BM_GETCHECK, 0, 0)) == 0x001);
202 #endif
203 }
204
205 void wxCheckBox::Command (wxCommandEvent & event)
206 {
207 SetValue ((event.GetInt() != 0));
208 ProcessCommand (event);
209 }
210
211 // ----------------------------------------------------------------------------
212 // wxBitmapCheckBox
213 // ----------------------------------------------------------------------------
214
215 bool wxBitmapCheckBox::Create(wxWindow *parent, wxWindowID id, const wxBitmap *WXUNUSED(label),
216 const wxPoint& pos,
217 const wxSize& size, long style,
218 const wxValidator& validator,
219 const wxString& name)
220 {
221 SetName(name);
222 #if wxUSE_VALIDATORS
223 SetValidator(validator);
224 #endif // wxUSE_VALIDATORS
225 if (parent) parent->AddChild(this);
226
227 SetBackgroundColour(parent->GetBackgroundColour()) ;
228 SetForegroundColour(parent->GetForegroundColour()) ;
229 m_windowStyle = style;
230
231 if ( id == -1 )
232 m_windowId = NewControlId();
233 else
234 m_windowId = id;
235
236 int x = pos.x;
237 int y = pos.y;
238 int width = size.x;
239 int height = size.y;
240
241 checkWidth = -1 ;
242 checkHeight = -1 ;
243 long msStyle = CHECK_FLAGS;
244
245 HWND wx_button = CreateWindowEx(MakeExtendedStyle(m_windowStyle), CHECK_CLASS, wxT("toggle"),
246 msStyle,
247 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
248 wxGetInstance(), NULL);
249
250 #if wxUSE_CTL3D
251 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
252 {
253 Ctl3dSubclassCtl(wx_button);
254 m_useCtl3D = TRUE;
255 }
256 #endif
257
258 m_hWnd = (WXHWND)wx_button;
259
260 // Subclass again for purposes of dialog editing mode
261 SubclassWin((WXHWND)wx_button);
262
263 SetSize(x, y, width, height);
264
265 ShowWindow(wx_button, SW_SHOW);
266
267 return TRUE;
268 }
269
270 void wxBitmapCheckBox::SetLabel(const wxBitmap& WXUNUSED(bitmap))
271 {
272 wxFAIL_MSG(wxT("not implemented"));
273 }
274
275 #endif // wxUSE_CHECKBOX