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