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
);
379 void wxCheckBox::OnMouseEnterOrLeave(wxMouseEvent
& event
)
381 m_isHot
= event
.GetEventType() == wxEVT_ENTER_WINDOW
;
390 void wxCheckBox::OnMouseLeft(wxMouseEvent
& event
)
392 // TODO: we should capture the mouse here to be notified about left up
393 // event but this interferes with BN_CLICKED generation so if we
394 // want to do this we'd need to generate them ourselves
395 m_isPressed
= event
.GetEventType() == wxEVT_LEFT_DOWN
;
401 void wxCheckBox::OnFocus(wxFocusEvent
& event
)
408 bool wxCheckBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
410 DRAWITEMSTRUCT
*dis
= (DRAWITEMSTRUCT
*)item
;
412 if ( !IsOwnerDrawn() || dis
->CtlType
!= ODT_BUTTON
)
413 return wxCheckBoxBase::MSWOnDraw(item
);
415 // calculate the rectangles for the check mark itself and the label
417 RECT
& rect
= dis
->rcItem
;
421 rectLabel
.top
= rect
.top
;
423 rectLabel
.bottom
= rect
.bottom
;
424 const int checkSize
= GetBestSize().y
;
425 const int MARGIN
= 3;
427 const bool isRightAligned
= HasFlag(wxALIGN_RIGHT
);
428 if ( isRightAligned
)
430 rectCheck
.right
= rect
.right
;
431 rectCheck
.left
= rectCheck
.right
- checkSize
;
433 rectLabel
.right
= rectCheck
.left
- MARGIN
;
434 rectLabel
.left
= rect
.left
;
436 else // normal, left-aligned checkbox
438 rectCheck
.left
= rect
.left
;
439 rectCheck
.right
= rectCheck
.left
+ checkSize
;
441 rectLabel
.left
= rectCheck
.right
+ MARGIN
;
442 rectLabel
.right
= rect
.right
;
445 // show we draw a focus rect?
446 const bool isFocused
= m_isPressed
|| FindFocus() == this;
449 // draw the checkbox itself: note that this should really, really be in
450 // wxRendererNative but unfortunately we can't add a new virtual function
451 // to it without breaking backwards compatibility
453 // classic Win32 version -- this can be useful when we move this into
455 UINT state
= DFCS_BUTTONCHECK
;
457 state
|= DFCS_INACTIVE
;
458 switch ( Get3StateValue() )
461 state
|= DFCS_CHECKED
;
464 case wxCHK_UNDETERMINED
:
465 state
|= DFCS_PUSHED
;
469 wxFAIL_MSG( _T("unexpected Get3StateValue() return value") );
472 case wxCHK_UNCHECKED
:
473 // no extra styles needed
477 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
480 if ( !::DrawFrameControl(hdc
, &rectCheck
, DFC_BUTTON
, state
) )
482 wxLogLastError(_T("DrawFrameControl(DFC_BUTTON)"));
485 wxUxThemeEngine
*themeEngine
= wxUxThemeEngine::GetIfActive();
489 wxUxThemeHandle
theme(this, L
"BUTTON");
494 switch ( Get3StateValue() )
497 state
= CBS_CHECKEDNORMAL
;
500 case wxCHK_UNDETERMINED
:
501 state
= CBS_MIXEDNORMAL
;
505 wxFAIL_MSG( _T("unexpected Get3StateValue() return value") );
508 case wxCHK_UNCHECKED
:
509 state
= CBS_UNCHECKEDNORMAL
;
514 state
+= CBS_DISABLED_OFFSET
;
515 else if ( m_isPressed
)
516 state
+= CBS_PRESSED_OFFSET
;
518 state
+= CBS_HOT_OFFSET
;
520 HRESULT hr
= themeEngine
->DrawThemeBackground
531 wxLogApiError(_T("DrawThemeBackground(BP_CHECKBOX)"), hr
);
536 const wxString
& label
= GetLabel();
538 // first we need to measure it
539 UINT fmt
= DT_NOCLIP
;
541 // drawing underlying doesn't look well with focus rect (and the native
542 // control doesn't do it)
544 fmt
|= DT_HIDEPREFIX
;
545 if ( isRightAligned
)
547 // TODO: also use DT_HIDEPREFIX if the system is configured so
549 // we need to get the label real size first if we have to draw a focus rect
553 if ( !::DrawText(hdc
, label
, label
.length(), &rectLabel
,
556 wxLogLastError(_T("DrawText(DT_CALCRECT)"));
562 ::SetTextColor(hdc
, ::GetSysColor(COLOR_GRAYTEXT
));
565 if ( !::DrawText(hdc
, label
, label
.length(), &rectLabel
, fmt
) )
567 wxLogLastError(_T("DrawText()"));
570 // finally draw the focus
575 if ( !::DrawFocusRect(hdc
, &rectLabel
) )
577 wxLogLastError(_T("DrawFocusRect()"));
584 #endif // wxUSE_CHECKBOX