1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/checkbox.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "checkbox.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/checkbox.h"
36 #include "wx/dcscreen.h"
37 #include "wx/settings.h"
40 #include "wx/msw/uxtheme.h"
41 #include "wx/msw/private.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
48 #define BST_UNCHECKED 0x0000
52 #define BST_CHECKED 0x0001
55 #ifndef BST_INDETERMINATE
56 #define BST_INDETERMINATE 0x0002
60 #define DFCS_HOT 0x1000
64 #define DT_HIDEPREFIX 0x00100000
71 // these values are defined in tmschema.h (except the first one)
78 CBS_UNCHECKEDDISABLED
,
93 CBS_PRESSED_OFFSET
= 2,
94 CBS_DISABLED_OFFSET
= 3
97 // ============================================================================
99 // ============================================================================
101 #if wxUSE_EXTENDED_RTTI
102 WX_DEFINE_FLAGS( wxCheckBoxStyle
)
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
)
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
)
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
)
132 wxEND_FLAGS( wxCheckBoxStyle
)
134 IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckBox
, wxControl
,"wx/checkbox.h")
136 wxBEGIN_PROPERTIES_TABLE(wxCheckBox
)
137 wxEVENT_PROPERTY( Click
, wxEVT_COMMAND_CHECKBOX_CLICKED
, wxCommandEvent
)
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()
145 wxBEGIN_HANDLERS_TABLE(wxCheckBox
)
146 wxEND_HANDLERS_TABLE()
148 wxCONSTRUCTOR_6( wxCheckBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
150 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
, wxControl
)
154 // ----------------------------------------------------------------------------
155 // wxCheckBox creation
156 // ----------------------------------------------------------------------------
158 void wxCheckBox::Init()
160 m_state
= wxCHK_UNCHECKED
;
165 bool wxCheckBox::Create(wxWindow
*parent
,
167 const wxString
& label
,
169 const wxSize
& size
, long style
,
170 const wxValidator
& validator
,
171 const wxString
& name
)
175 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
178 long msStyle
= WS_TABSTOP
;
180 if ( style
& wxCHK_3STATE
)
182 msStyle
|= BS_3STATE
;
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
;
192 if ( style
& wxALIGN_RIGHT
)
194 msStyle
|= BS_LEFTTEXT
| BS_RIGHT
;
197 return MSWCreateControl(wxT("BUTTON"), msStyle
, pos
, size
, label
, 0);
200 // ----------------------------------------------------------------------------
201 // wxCheckBox geometry
202 // ----------------------------------------------------------------------------
204 wxSize
wxCheckBox::DoGetBestSize() const
206 static int s_checkSize
= 0;
211 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
213 s_checkSize
= dc
.GetCharHeight();
216 wxString str
= wxGetWindowText(GetHWND());
218 int wCheckbox
, hCheckbox
;
219 if ( !str
.IsEmpty() )
221 GetTextExtent(str
, &wCheckbox
, &hCheckbox
);
222 wCheckbox
+= s_checkSize
+ GetCharWidth();
224 if ( hCheckbox
< s_checkSize
)
225 hCheckbox
= s_checkSize
;
229 wCheckbox
= s_checkSize
;
230 hCheckbox
= s_checkSize
;
236 wxSize
best(wCheckbox
, hCheckbox
);
241 // ----------------------------------------------------------------------------
242 // wxCheckBox operations
243 // ----------------------------------------------------------------------------
245 void wxCheckBox::SetValue(bool val
)
247 Set3StateValue(val
? wxCHK_CHECKED
: wxCHK_UNCHECKED
);
250 bool wxCheckBox::GetValue() const
252 return Get3StateValue() != wxCHK_UNCHECKED
;
255 void wxCheckBox::Command(wxCommandEvent
& event
)
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") );
262 Set3StateValue((wxCheckBoxState
) state
);
263 ProcessCommand(event
);
266 wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED
== BST_UNCHECKED
267 && wxCHK_CHECKED
== BST_CHECKED
268 && wxCHK_UNDETERMINED
== BST_INDETERMINATE
, EnumValuesIncorrect
);
270 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
273 if ( !IsOwnerDrawn() )
274 ::SendMessage(GetHwnd(), BM_SETCHECK
, (WPARAM
) state
, 0);
275 else // owner drawn buttons don't react to this message
279 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
284 bool wxCheckBox::MSWCommand(WXUINT cmd
, WXWORD
WXUNUSED(id
))
286 if ( cmd
!= BN_CLICKED
&& cmd
!= BN_DBLCLK
)
289 // first update the value so that user event handler gets the new checkbox
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() )
297 state
= (wxCheckBoxState
)((m_state
+ 1) % 3);
299 else // 2 state checkbox (at least from users point of view)
301 // note that wxCHK_UNDETERMINED also becomes unchecked when clicked
302 state
= m_state
== wxCHK_UNCHECKED
? wxCHK_CHECKED
: wxCHK_UNCHECKED
;
305 DoSet3StateValue(state
);
308 // generate the event
309 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, m_windowId
);
312 event
.SetEventObject(this);
313 ProcessCommand(event
);
318 // ----------------------------------------------------------------------------
319 // owner drawn checkboxes stuff
320 // ----------------------------------------------------------------------------
322 bool wxCheckBox::SetForegroundColour(const wxColour
& colour
)
324 if ( !wxCheckBoxBase::SetForegroundColour(colour
) )
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());
335 bool wxCheckBox::IsOwnerDrawn() const
338 (::GetWindowLong(GetHwnd(), GWL_STYLE
) & BS_OWNERDRAW
) == BS_OWNERDRAW
;
341 void wxCheckBox::MakeOwnerDrawn(bool ownerDrawn
)
343 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
345 // note that BS_CHECKBOX & BS_OWNERDRAW != 0 so we can't operate on
346 // them as on independent style bits
349 style
&= ~(BS_CHECKBOX
| BS_3STATE
);
350 style
|= BS_OWNERDRAW
;
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
));
361 else // reset to default colour
363 style
&= ~BS_OWNERDRAW
;
364 style
|= HasFlag(wxCHK_3STATE
) ? BS_3STATE
: BS_CHECKBOX
;
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
));
376 ::SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
380 // ensure that controls state is consistent with internal state
381 DoSet3StateValue(m_state
);
385 void wxCheckBox::OnMouseEnterOrLeave(wxMouseEvent
& event
)
387 m_isHot
= event
.GetEventType() == wxEVT_ENTER_WINDOW
;
396 void wxCheckBox::OnMouseLeft(wxMouseEvent
& event
)
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
;
407 void wxCheckBox::OnFocus(wxFocusEvent
& event
)
414 bool wxCheckBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
416 DRAWITEMSTRUCT
*dis
= (DRAWITEMSTRUCT
*)item
;
418 if ( !IsOwnerDrawn() || dis
->CtlType
!= ODT_BUTTON
)
419 return wxCheckBoxBase::MSWOnDraw(item
);
421 // calculate the rectangles for the check mark itself and the label
423 RECT
& rect
= dis
->rcItem
;
427 rectLabel
.top
= rect
.top
;
429 rectLabel
.bottom
= rect
.bottom
;
430 const int checkSize
= GetBestSize().y
;
431 const int MARGIN
= 3;
433 const bool isRightAligned
= HasFlag(wxALIGN_RIGHT
);
434 if ( isRightAligned
)
436 rectCheck
.right
= rect
.right
;
437 rectCheck
.left
= rectCheck
.right
- checkSize
;
439 rectLabel
.right
= rectCheck
.left
- MARGIN
;
440 rectLabel
.left
= rect
.left
;
442 else // normal, left-aligned checkbox
444 rectCheck
.left
= rect
.left
;
445 rectCheck
.right
= rectCheck
.left
+ checkSize
;
447 rectLabel
.left
= rectCheck
.right
+ MARGIN
;
448 rectLabel
.right
= rect
.right
;
451 // show we draw a focus rect?
452 const bool isFocused
= m_isPressed
|| FindFocus() == this;
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
459 // classic Win32 version -- this can be useful when we move this into
461 UINT state
= DFCS_BUTTONCHECK
;
463 state
|= DFCS_INACTIVE
;
464 switch ( Get3StateValue() )
467 state
|= DFCS_CHECKED
;
470 case wxCHK_UNDETERMINED
:
471 state
|= DFCS_PUSHED
;
475 wxFAIL_MSG( _T("unexpected Get3StateValue() return value") );
478 case wxCHK_UNCHECKED
:
479 // no extra styles needed
483 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
486 if ( !::DrawFrameControl(hdc
, &rectCheck
, DFC_BUTTON
, state
) )
488 wxLogLastError(_T("DrawFrameControl(DFC_BUTTON)"));
491 wxUxThemeEngine
*themeEngine
= wxUxThemeEngine::GetIfActive();
495 wxUxThemeHandle
theme(this, L
"BUTTON");
500 switch ( Get3StateValue() )
503 state
= CBS_CHECKEDNORMAL
;
506 case wxCHK_UNDETERMINED
:
507 state
= CBS_MIXEDNORMAL
;
511 wxFAIL_MSG( _T("unexpected Get3StateValue() return value") );
514 case wxCHK_UNCHECKED
:
515 state
= CBS_UNCHECKEDNORMAL
;
520 state
+= CBS_DISABLED_OFFSET
;
521 else if ( m_isPressed
)
522 state
+= CBS_PRESSED_OFFSET
;
524 state
+= CBS_HOT_OFFSET
;
526 HRESULT hr
= themeEngine
->DrawThemeBackground
537 wxLogApiError(_T("DrawThemeBackground(BP_CHECKBOX)"), hr
);
542 const wxString
& label
= GetLabel();
544 // first we need to measure it
545 UINT fmt
= DT_NOCLIP
;
547 // drawing underlying doesn't look well with focus rect (and the native
548 // control doesn't do it)
550 fmt
|= DT_HIDEPREFIX
;
551 if ( isRightAligned
)
553 // TODO: also use DT_HIDEPREFIX if the system is configured so
555 // we need to get the label real size first if we have to draw a focus rect
559 if ( !::DrawText(hdc
, label
, label
.length(), &rectLabel
,
562 wxLogLastError(_T("DrawText(DT_CALCRECT)"));
568 ::SetTextColor(hdc
, ::GetSysColor(COLOR_GRAYTEXT
));
571 if ( !::DrawText(hdc
, label
, label
.length(), &rectLabel
, fmt
) )
573 wxLogLastError(_T("DrawText()"));
576 // finally draw the focus
581 if ( !::DrawFocusRect(hdc
, &rectLabel
) )
583 wxLogLastError(_T("DrawFocusRect()"));
590 #endif // wxUSE_CHECKBOX