]> git.saurik.com Git - wxWidgets.git/blob - src/msw/checkbox.cpp
6b8963443ab2f9eabbbf25f339c6696272a8df35
[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 WXDWORD exstyle;
108 WXDWORD msStyle = MSWGetStyle(style, &exstyle);
109
110 msStyle |= wxMSWButton::GetMultilineStyle(label);
111
112 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, exstyle);
113 }
114
115 WXDWORD wxCheckBox::MSWGetStyle(long style, WXDWORD *exstyle) const
116 {
117 // buttons never have an external border, they draw their own one
118 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
119
120 if ( style & wxCHK_3STATE )
121 msStyle |= BS_3STATE;
122 else
123 msStyle |= BS_CHECKBOX;
124
125 if ( style & wxALIGN_RIGHT )
126 {
127 msStyle |= BS_LEFTTEXT | BS_RIGHT;
128 }
129
130 return msStyle;
131 }
132
133 // ----------------------------------------------------------------------------
134 // wxCheckBox geometry
135 // ----------------------------------------------------------------------------
136
137 wxSize wxCheckBox::DoGetBestClientSize() const
138 {
139 static int s_checkSize = 0;
140
141 if ( !s_checkSize )
142 {
143 wxScreenDC dc;
144 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
145
146 s_checkSize = dc.GetCharHeight();
147 }
148
149 wxString str = wxGetWindowText(GetHWND());
150
151 int wCheckbox, hCheckbox;
152 if ( !str.empty() )
153 {
154 wxClientDC dc(const_cast<wxCheckBox *>(this));
155 dc.SetFont(GetFont());
156 dc.GetMultiLineTextExtent(GetLabelText(str), &wCheckbox, &hCheckbox);
157 wCheckbox += s_checkSize + GetCharWidth();
158
159 if ( ::GetWindowLong(GetHwnd(), GWL_STYLE) & BS_MULTILINE )
160 {
161 // We need to make the checkbox even wider in this case because
162 // otherwise it wraps lines automatically and not only on "\n"s as
163 // we need and this makes the size computed here wrong resulting in
164 // checkbox contents being truncated when it's actually displayed.
165 // Without this hack simple checkbox with "Some thing\n and more"
166 // label appears on 3 lines, not 2, under Windows 2003 using
167 // classic look and feel (although it works fine under Windows 7,
168 // with or without themes).
169 wCheckbox += s_checkSize;
170 }
171
172 if ( hCheckbox < s_checkSize )
173 hCheckbox = s_checkSize;
174 }
175 else
176 {
177 wCheckbox = s_checkSize;
178 hCheckbox = s_checkSize;
179 }
180 #ifdef __WXWINCE__
181 hCheckbox += 1;
182 #endif
183
184 wxSize best(wCheckbox, hCheckbox);
185 CacheBestSize(best);
186 return best;
187 }
188
189 // ----------------------------------------------------------------------------
190 // wxCheckBox operations
191 // ----------------------------------------------------------------------------
192
193 void wxCheckBox::SetLabel(const wxString& label)
194 {
195 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label);
196
197 wxCheckBoxBase::SetLabel(label);
198 }
199
200 void wxCheckBox::SetValue(bool val)
201 {
202 Set3StateValue(val ? wxCHK_CHECKED : wxCHK_UNCHECKED);
203 }
204
205 bool wxCheckBox::GetValue() const
206 {
207 return Get3StateValue() != wxCHK_UNCHECKED;
208 }
209
210 void wxCheckBox::Command(wxCommandEvent& event)
211 {
212 int state = event.GetInt();
213 wxCHECK_RET( (state == wxCHK_UNCHECKED) || (state == wxCHK_CHECKED)
214 || (state == wxCHK_UNDETERMINED),
215 wxT("event.GetInt() returned an invalid checkbox state") );
216
217 Set3StateValue((wxCheckBoxState) state);
218 ProcessCommand(event);
219 }
220
221 wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED == BST_UNCHECKED
222 && wxCHK_CHECKED == BST_CHECKED
223 && wxCHK_UNDETERMINED == BST_INDETERMINATE, EnumValuesIncorrect);
224
225 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
226 {
227 m_state = state;
228 if ( !IsOwnerDrawn() )
229 ::SendMessage(GetHwnd(), BM_SETCHECK, (WPARAM) state, 0);
230 else // owner drawn buttons don't react to this message
231 Refresh();
232 }
233
234 wxCheckBoxState wxCheckBox::DoGet3StateValue() const
235 {
236 return m_state;
237 }
238
239 bool wxCheckBox::MSWCommand(WXUINT cmd, WXWORD WXUNUSED(id))
240 {
241 if ( cmd != BN_CLICKED && cmd != BN_DBLCLK )
242 return false;
243
244 // first update the value so that user event handler gets the new checkbox
245 // value
246
247 // ownerdrawn buttons don't manage their state themselves unlike usual
248 // auto checkboxes so do it ourselves in any case
249 wxCheckBoxState state;
250 if ( Is3rdStateAllowedForUser() )
251 {
252 state = (wxCheckBoxState)((m_state + 1) % 3);
253 }
254 else // 2 state checkbox (at least from users point of view)
255 {
256 // note that wxCHK_UNDETERMINED also becomes unchecked when clicked
257 state = m_state == wxCHK_UNCHECKED ? wxCHK_CHECKED : wxCHK_UNCHECKED;
258 }
259
260 DoSet3StateValue(state);
261
262
263 // generate the event
264 wxCommandEvent event(wxEVT_CHECKBOX, m_windowId);
265
266 event.SetInt(state);
267 event.SetEventObject(this);
268 ProcessCommand(event);
269
270 return true;
271 }
272
273 // ----------------------------------------------------------------------------
274 // owner drawn checkboxes stuff
275 // ----------------------------------------------------------------------------
276
277 bool wxCheckBox::SetForegroundColour(const wxColour& colour)
278 {
279 if ( !wxCheckBoxBase::SetForegroundColour(colour) )
280 return false;
281
282 // the only way to change the checkbox foreground colour under Windows XP
283 // is to owner draw it
284 if ( wxUxThemeEngine::GetIfActive() )
285 MSWMakeOwnerDrawn(colour.IsOk());
286
287 return true;
288 }
289
290 bool wxCheckBox::IsOwnerDrawn() const
291 {
292 return
293 (::GetWindowLong(GetHwnd(), GWL_STYLE) & BS_OWNERDRAW) == BS_OWNERDRAW;
294 }
295
296 void wxCheckBox::MSWMakeOwnerDrawn(bool ownerDrawn)
297 {
298 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
299
300 // note that BS_CHECKBOX & BS_OWNERDRAW != 0 so we can't operate on
301 // them as on independent style bits
302 if ( ownerDrawn )
303 {
304 style &= ~(BS_CHECKBOX | BS_3STATE);
305 style |= BS_OWNERDRAW;
306
307 Connect(wxEVT_ENTER_WINDOW,
308 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
309 Connect(wxEVT_LEAVE_WINDOW,
310 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
311 Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
312 Connect(wxEVT_LEFT_UP, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
313 Connect(wxEVT_SET_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
314 Connect(wxEVT_KILL_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
315 }
316 else // reset to default colour
317 {
318 style &= ~BS_OWNERDRAW;
319 style |= HasFlag(wxCHK_3STATE) ? BS_3STATE : BS_CHECKBOX;
320
321 Disconnect(wxEVT_ENTER_WINDOW,
322 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
323 Disconnect(wxEVT_LEAVE_WINDOW,
324 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
325 Disconnect(wxEVT_LEFT_DOWN, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
326 Disconnect(wxEVT_LEFT_UP, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
327 Disconnect(wxEVT_SET_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
328 Disconnect(wxEVT_KILL_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
329 }
330
331 ::SetWindowLong(GetHwnd(), GWL_STYLE, style);
332
333 if ( !ownerDrawn )
334 {
335 // ensure that controls state is consistent with internal state
336 DoSet3StateValue(m_state);
337 }
338 }
339
340 void wxCheckBox::OnMouseEnterOrLeave(wxMouseEvent& event)
341 {
342 m_isHot = event.GetEventType() == wxEVT_ENTER_WINDOW;
343 if ( !m_isHot )
344 m_isPressed = false;
345
346 Refresh();
347
348 event.Skip();
349 }
350
351 void wxCheckBox::OnMouseLeft(wxMouseEvent& event)
352 {
353 // TODO: we should capture the mouse here to be notified about left up
354 // event but this interferes with BN_CLICKED generation so if we
355 // want to do this we'd need to generate them ourselves
356 m_isPressed = event.GetEventType() == wxEVT_LEFT_DOWN;
357 Refresh();
358
359 event.Skip();
360 }
361
362 void wxCheckBox::OnFocus(wxFocusEvent& event)
363 {
364 Refresh();
365
366 event.Skip();
367 }
368
369 bool wxCheckBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
370 {
371 DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)item;
372
373 if ( !IsOwnerDrawn() || dis->CtlType != ODT_BUTTON )
374 return wxCheckBoxBase::MSWOnDraw(item);
375
376 // calculate the rectangles for the check mark itself and the label
377 HDC hdc = dis->hDC;
378 RECT& rect = dis->rcItem;
379 RECT rectCheck,
380 rectLabel;
381 rectLabel.top = rect.top + (rect.bottom - rect.top - GetBestSize().y) / 2;
382 rectLabel.bottom = rectLabel.top + GetBestSize().y;
383 const int MARGIN = 3;
384 const int CXMENUCHECK = ::GetSystemMetrics(SM_CXMENUCHECK);
385 // the space between the checkbox and the label is included in the
386 // check-mark bitmap
387 const int checkSize = wxMin(CXMENUCHECK - MARGIN, GetSize().y);
388 rectCheck.top = rect.top + (rect.bottom - rect.top - checkSize) / 2;
389 rectCheck.bottom = rectCheck.top + checkSize;
390
391 const bool isRightAligned = HasFlag(wxALIGN_RIGHT);
392 if ( isRightAligned )
393 {
394 rectLabel.right = rect.right - CXMENUCHECK;
395 rectLabel.left = rect.left;
396
397 rectCheck.left = rectLabel.right + ( CXMENUCHECK + MARGIN - checkSize ) / 2;
398 rectCheck.right = rectCheck.left + checkSize;
399 }
400 else // normal, left-aligned checkbox
401 {
402 rectCheck.left = rect.left + ( CXMENUCHECK - MARGIN - checkSize ) / 2;
403 rectCheck.right = rectCheck.left + checkSize;
404
405 rectLabel.left = rect.left + CXMENUCHECK;
406 rectLabel.right = rect.right;
407 }
408
409 // shall we draw a focus rect?
410 const bool isFocused = m_isPressed || FindFocus() == this;
411
412
413 // draw the checkbox itself
414 wxDCTemp dc(hdc);
415
416 int flags = 0;
417 if ( !IsEnabled() )
418 flags |= wxCONTROL_DISABLED;
419 switch ( Get3StateValue() )
420 {
421 case wxCHK_CHECKED:
422 flags |= wxCONTROL_CHECKED;
423 break;
424
425 case wxCHK_UNDETERMINED:
426 flags |= wxCONTROL_PRESSED;
427 break;
428
429 default:
430 wxFAIL_MSG( wxT("unexpected Get3StateValue() return value") );
431 // fall through
432
433 case wxCHK_UNCHECKED:
434 // no extra styles needed
435 break;
436 }
437
438 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
439 flags |= wxCONTROL_CURRENT;
440
441 wxRendererNative::Get().
442 DrawCheckBox(this, dc, wxRectFromRECT(rectCheck), flags);
443
444 // draw the text
445 const wxString& label = GetLabel();
446
447 // first we need to measure it
448 UINT fmt = DT_NOCLIP;
449
450 // drawing underlying doesn't look well with focus rect (and the native
451 // control doesn't do it)
452 if ( isFocused )
453 fmt |= DT_HIDEPREFIX;
454 if ( isRightAligned )
455 fmt |= DT_RIGHT;
456 // TODO: also use DT_HIDEPREFIX if the system is configured so
457
458 // we need to get the label real size first if we have to draw a focus rect
459 // around it
460 if ( isFocused )
461 {
462 RECT oldLabelRect = rectLabel; // needed if right aligned
463
464 if ( !::DrawText(hdc, label.t_str(), label.length(), &rectLabel,
465 fmt | DT_CALCRECT) )
466 {
467 wxLogLastError(wxT("DrawText(DT_CALCRECT)"));
468 }
469
470 if ( isRightAligned )
471 {
472 // move the label rect to the right
473 const int labelWidth = rectLabel.right - rectLabel.left;
474 rectLabel.right = oldLabelRect.right;
475 rectLabel.left = rectLabel.right - labelWidth;
476 }
477 }
478
479 if ( !IsEnabled() )
480 {
481 ::SetTextColor(hdc, ::GetSysColor(COLOR_GRAYTEXT));
482 }
483
484 if ( !::DrawText(hdc, label.t_str(), label.length(), &rectLabel, fmt) )
485 {
486 wxLogLastError(wxT("DrawText()"));
487 }
488
489 // finally draw the focus
490 if ( isFocused )
491 {
492 rectLabel.left--;
493 rectLabel.right++;
494 if ( !::DrawFocusRect(hdc, &rectLabel) )
495 {
496 wxLogLastError(wxT("DrawFocusRect()"));
497 }
498 }
499
500 return true;
501 }
502
503 #endif // wxUSE_CHECKBOX