]> git.saurik.com Git - wxWidgets.git/blob - src/msw/checkbox.cpp
Fix warnings for the OSX WebKit backend.
[wxWidgets.git] / src / msw / checkbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_CHECKBOX
28
29 #include "wx/checkbox.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/brush.h"
33 #include "wx/dcclient.h"
34 #include "wx/dcscreen.h"
35 #include "wx/settings.h"
36 #endif
37
38 #include "wx/msw/dc.h" // for wxDCTemp
39 #include "wx/renderer.h"
40 #include "wx/msw/uxtheme.h"
41 #include "wx/msw/private/button.h"
42 #include "wx/msw/missing.h"
43
44 // ----------------------------------------------------------------------------
45 // constants
46 // ----------------------------------------------------------------------------
47
48 #ifndef BP_CHECKBOX
49 #define BP_CHECKBOX 3
50 #endif
51
52 // these values are defined in tmschema.h (except the first one)
53 enum
54 {
55 CBS_INVALID,
56 CBS_UNCHECKEDNORMAL,
57 CBS_UNCHECKEDHOT,
58 CBS_UNCHECKEDPRESSED,
59 CBS_UNCHECKEDDISABLED,
60 CBS_CHECKEDNORMAL,
61 CBS_CHECKEDHOT,
62 CBS_CHECKEDPRESSED,
63 CBS_CHECKEDDISABLED,
64 CBS_MIXEDNORMAL,
65 CBS_MIXEDHOT,
66 CBS_MIXEDPRESSED,
67 CBS_MIXEDDISABLED
68 };
69
70 // these are our own
71 enum
72 {
73 CBS_HOT_OFFSET = 1,
74 CBS_PRESSED_OFFSET = 2,
75 CBS_DISABLED_OFFSET = 3
76 };
77
78 // ============================================================================
79 // implementation
80 // ============================================================================
81
82 // ----------------------------------------------------------------------------
83 // wxCheckBox creation
84 // ----------------------------------------------------------------------------
85
86 void wxCheckBox::Init()
87 {
88 m_state = wxCHK_UNCHECKED;
89 m_isPressed =
90 m_isHot = false;
91 }
92
93 bool wxCheckBox::Create(wxWindow *parent,
94 wxWindowID id,
95 const wxString& label,
96 const wxPoint& pos,
97 const wxSize& size, long style,
98 const wxValidator& validator,
99 const wxString& name)
100 {
101 Init();
102
103 WXValidateStyle(&style);
104 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
105 return false;
106
107 long msStyle = WS_TABSTOP;
108
109 if ( style & wxCHK_3STATE )
110 msStyle |= BS_3STATE;
111 else
112 msStyle |= BS_CHECKBOX;
113
114 if ( style & wxALIGN_RIGHT )
115 {
116 msStyle |= BS_LEFTTEXT | BS_RIGHT;
117 }
118
119 msStyle |= wxMSWButton::GetMultilineStyle(label);
120
121 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, 0);
122 }
123
124 // ----------------------------------------------------------------------------
125 // wxCheckBox geometry
126 // ----------------------------------------------------------------------------
127
128 wxSize wxCheckBox::DoGetBestSize() const
129 {
130 static int s_checkSize = 0;
131
132 if ( !s_checkSize )
133 {
134 wxScreenDC dc;
135 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
136
137 s_checkSize = dc.GetCharHeight();
138 }
139
140 wxString str = wxGetWindowText(GetHWND());
141
142 int wCheckbox, hCheckbox;
143 if ( !str.empty() )
144 {
145 wxClientDC dc(const_cast<wxCheckBox *>(this));
146 dc.SetFont(GetFont());
147 dc.GetMultiLineTextExtent(GetLabelText(str), &wCheckbox, &hCheckbox);
148 wCheckbox += s_checkSize + GetCharWidth();
149
150 if ( hCheckbox < s_checkSize )
151 hCheckbox = s_checkSize;
152 }
153 else
154 {
155 wCheckbox = s_checkSize;
156 hCheckbox = s_checkSize;
157 }
158 #ifdef __WXWINCE__
159 hCheckbox += 1;
160 #endif
161
162 wxSize best(wCheckbox, hCheckbox);
163 CacheBestSize(best);
164 return best;
165 }
166
167 // ----------------------------------------------------------------------------
168 // wxCheckBox operations
169 // ----------------------------------------------------------------------------
170
171 void wxCheckBox::SetLabel(const wxString& label)
172 {
173 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label);
174
175 wxCheckBoxBase::SetLabel(label);
176 }
177
178 void wxCheckBox::SetValue(bool val)
179 {
180 Set3StateValue(val ? wxCHK_CHECKED : wxCHK_UNCHECKED);
181 }
182
183 bool wxCheckBox::GetValue() const
184 {
185 return Get3StateValue() != wxCHK_UNCHECKED;
186 }
187
188 void wxCheckBox::Command(wxCommandEvent& event)
189 {
190 int state = event.GetInt();
191 wxCHECK_RET( (state == wxCHK_UNCHECKED) || (state == wxCHK_CHECKED)
192 || (state == wxCHK_UNDETERMINED),
193 wxT("event.GetInt() returned an invalid checkbox state") );
194
195 Set3StateValue((wxCheckBoxState) state);
196 ProcessCommand(event);
197 }
198
199 wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED == BST_UNCHECKED
200 && wxCHK_CHECKED == BST_CHECKED
201 && wxCHK_UNDETERMINED == BST_INDETERMINATE, EnumValuesIncorrect);
202
203 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
204 {
205 m_state = state;
206 if ( !IsOwnerDrawn() )
207 ::SendMessage(GetHwnd(), BM_SETCHECK, (WPARAM) state, 0);
208 else // owner drawn buttons don't react to this message
209 Refresh();
210 }
211
212 wxCheckBoxState wxCheckBox::DoGet3StateValue() const
213 {
214 return m_state;
215 }
216
217 bool wxCheckBox::MSWCommand(WXUINT cmd, WXWORD WXUNUSED(id))
218 {
219 if ( cmd != BN_CLICKED && cmd != BN_DBLCLK )
220 return false;
221
222 // first update the value so that user event handler gets the new checkbox
223 // value
224
225 // ownerdrawn buttons don't manage their state themselves unlike usual
226 // auto checkboxes so do it ourselves in any case
227 wxCheckBoxState state;
228 if ( Is3rdStateAllowedForUser() )
229 {
230 state = (wxCheckBoxState)((m_state + 1) % 3);
231 }
232 else // 2 state checkbox (at least from users point of view)
233 {
234 // note that wxCHK_UNDETERMINED also becomes unchecked when clicked
235 state = m_state == wxCHK_UNCHECKED ? wxCHK_CHECKED : wxCHK_UNCHECKED;
236 }
237
238 DoSet3StateValue(state);
239
240
241 // generate the event
242 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, m_windowId);
243
244 event.SetInt(state);
245 event.SetEventObject(this);
246 ProcessCommand(event);
247
248 return true;
249 }
250
251 // ----------------------------------------------------------------------------
252 // owner drawn checkboxes stuff
253 // ----------------------------------------------------------------------------
254
255 bool wxCheckBox::SetForegroundColour(const wxColour& colour)
256 {
257 if ( !wxCheckBoxBase::SetForegroundColour(colour) )
258 return false;
259
260 // the only way to change the checkbox foreground colour under Windows XP
261 // is to owner draw it
262 if ( wxUxThemeEngine::GetIfActive() )
263 MakeOwnerDrawn(colour.IsOk());
264
265 return true;
266 }
267
268 bool wxCheckBox::IsOwnerDrawn() const
269 {
270 return
271 (::GetWindowLong(GetHwnd(), GWL_STYLE) & BS_OWNERDRAW) == BS_OWNERDRAW;
272 }
273
274 void wxCheckBox::MakeOwnerDrawn(bool ownerDrawn)
275 {
276 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
277
278 // note that BS_CHECKBOX & BS_OWNERDRAW != 0 so we can't operate on
279 // them as on independent style bits
280 if ( ownerDrawn )
281 {
282 style &= ~(BS_CHECKBOX | BS_3STATE);
283 style |= BS_OWNERDRAW;
284
285 Connect(wxEVT_ENTER_WINDOW,
286 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
287 Connect(wxEVT_LEAVE_WINDOW,
288 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
289 Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
290 Connect(wxEVT_LEFT_UP, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
291 Connect(wxEVT_SET_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
292 Connect(wxEVT_KILL_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
293 }
294 else // reset to default colour
295 {
296 style &= ~BS_OWNERDRAW;
297 style |= HasFlag(wxCHK_3STATE) ? BS_3STATE : BS_CHECKBOX;
298
299 Disconnect(wxEVT_ENTER_WINDOW,
300 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
301 Disconnect(wxEVT_LEAVE_WINDOW,
302 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
303 Disconnect(wxEVT_LEFT_DOWN, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
304 Disconnect(wxEVT_LEFT_UP, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
305 Disconnect(wxEVT_SET_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
306 Disconnect(wxEVT_KILL_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
307 }
308
309 ::SetWindowLong(GetHwnd(), GWL_STYLE, style);
310
311 if ( !ownerDrawn )
312 {
313 // ensure that controls state is consistent with internal state
314 DoSet3StateValue(m_state);
315 }
316 }
317
318 void wxCheckBox::OnMouseEnterOrLeave(wxMouseEvent& event)
319 {
320 m_isHot = event.GetEventType() == wxEVT_ENTER_WINDOW;
321 if ( !m_isHot )
322 m_isPressed = false;
323
324 Refresh();
325
326 event.Skip();
327 }
328
329 void wxCheckBox::OnMouseLeft(wxMouseEvent& event)
330 {
331 // TODO: we should capture the mouse here to be notified about left up
332 // event but this interferes with BN_CLICKED generation so if we
333 // want to do this we'd need to generate them ourselves
334 m_isPressed = event.GetEventType() == wxEVT_LEFT_DOWN;
335 Refresh();
336
337 event.Skip();
338 }
339
340 void wxCheckBox::OnFocus(wxFocusEvent& event)
341 {
342 Refresh();
343
344 event.Skip();
345 }
346
347 bool wxCheckBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
348 {
349 DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)item;
350
351 if ( !IsOwnerDrawn() || dis->CtlType != ODT_BUTTON )
352 return wxCheckBoxBase::MSWOnDraw(item);
353
354 // calculate the rectangles for the check mark itself and the label
355 HDC hdc = dis->hDC;
356 RECT& rect = dis->rcItem;
357 RECT rectCheck,
358 rectLabel;
359 rectCheck.top =
360 rectLabel.top = rect.top;
361 rectCheck.bottom =
362 rectLabel.bottom = rect.bottom;
363 const int checkSize = GetBestSize().y;
364 const int MARGIN = 3;
365
366 const bool isRightAligned = HasFlag(wxALIGN_RIGHT);
367 if ( isRightAligned )
368 {
369 rectCheck.right = rect.right;
370 rectCheck.left = rectCheck.right - checkSize;
371
372 rectLabel.right = rectCheck.left - MARGIN;
373 rectLabel.left = rect.left;
374 }
375 else // normal, left-aligned checkbox
376 {
377 rectCheck.left = rect.left;
378 rectCheck.right = rectCheck.left + checkSize;
379
380 rectLabel.left = rectCheck.right + MARGIN;
381 rectLabel.right = rect.right;
382 }
383
384 // show we draw a focus rect?
385 const bool isFocused = m_isPressed || FindFocus() == this;
386
387
388 // draw the checkbox itself
389 wxDCTemp dc(hdc);
390
391 int flags = 0;
392 if ( !IsEnabled() )
393 flags |= wxCONTROL_DISABLED;
394 switch ( Get3StateValue() )
395 {
396 case wxCHK_CHECKED:
397 flags |= wxCONTROL_CHECKED;
398 break;
399
400 case wxCHK_UNDETERMINED:
401 flags |= wxCONTROL_PRESSED;
402 break;
403
404 default:
405 wxFAIL_MSG( wxT("unexpected Get3StateValue() return value") );
406 // fall through
407
408 case wxCHK_UNCHECKED:
409 // no extra styles needed
410 break;
411 }
412
413 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
414 flags |= wxCONTROL_CURRENT;
415
416 wxRendererNative::Get().
417 DrawCheckBox(this, dc, wxRectFromRECT(rectCheck), flags);
418
419 // draw the text
420 const wxString& label = GetLabel();
421
422 // first we need to measure it
423 UINT fmt = DT_NOCLIP;
424
425 // drawing underlying doesn't look well with focus rect (and the native
426 // control doesn't do it)
427 if ( isFocused )
428 fmt |= DT_HIDEPREFIX;
429 if ( isRightAligned )
430 fmt |= DT_RIGHT;
431 // TODO: also use DT_HIDEPREFIX if the system is configured so
432
433 // we need to get the label real size first if we have to draw a focus rect
434 // around it
435 if ( isFocused )
436 {
437 if ( !::DrawText(hdc, label.wx_str(), label.length(), &rectLabel,
438 fmt | DT_CALCRECT) )
439 {
440 wxLogLastError(wxT("DrawText(DT_CALCRECT)"));
441 }
442 }
443
444 if ( !IsEnabled() )
445 {
446 ::SetTextColor(hdc, ::GetSysColor(COLOR_GRAYTEXT));
447 }
448
449 if ( !::DrawText(hdc, label.wx_str(), label.length(), &rectLabel, fmt) )
450 {
451 wxLogLastError(wxT("DrawText()"));
452 }
453
454 // finally draw the focus
455 if ( isFocused )
456 {
457 rectLabel.left--;
458 rectLabel.right++;
459 if ( !::DrawFocusRect(hdc, &rectLabel) )
460 {
461 wxLogLastError(wxT("DrawFocusRect()"));
462 }
463 }
464
465 return true;
466 }
467
468 #endif // wxUSE_CHECKBOX