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