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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
30 #include "wx/checkbox.h"
32 #include "wx/dcscreen.h"
33 #include "wx/settings.h"
36 #include "wx/msw/uxtheme.h"
37 #include "wx/msw/private.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
44 #define BST_UNCHECKED 0x0000
48 #define BST_CHECKED 0x0001
51 #ifndef BST_INDETERMINATE
52 #define BST_INDETERMINATE 0x0002
56 #define DFCS_HOT 0x1000
60 #define DT_HIDEPREFIX 0x00100000
67 // these values are defined in tmschema.h (except the first one)
74 CBS_UNCHECKEDDISABLED
,
89 CBS_PRESSED_OFFSET
= 2,
90 CBS_DISABLED_OFFSET
= 3
93 // ============================================================================
95 // ============================================================================
97 #if wxUSE_EXTENDED_RTTI
98 WX_DEFINE_FLAGS( wxCheckBoxStyle
)
100 wxBEGIN_FLAGS( wxCheckBoxStyle
)
101 // new style border flags, we put them first to
102 // use them for streaming out
103 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
104 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
105 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
106 wxFLAGS_MEMBER(wxBORDER_RAISED
)
107 wxFLAGS_MEMBER(wxBORDER_STATIC
)
108 wxFLAGS_MEMBER(wxBORDER_NONE
)
110 // old style border flags
111 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
112 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
113 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
114 wxFLAGS_MEMBER(wxRAISED_BORDER
)
115 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
116 wxFLAGS_MEMBER(wxNO_BORDER
)
118 // standard window styles
119 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
120 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
121 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
122 wxFLAGS_MEMBER(wxWANTS_CHARS
)
123 wxFLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE
)
124 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
125 wxFLAGS_MEMBER(wxVSCROLL
)
126 wxFLAGS_MEMBER(wxHSCROLL
)
128 wxEND_FLAGS( wxCheckBoxStyle
)
130 IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckBox
, wxControl
,"wx/checkbox.h")
132 wxBEGIN_PROPERTIES_TABLE(wxCheckBox
)
133 wxEVENT_PROPERTY( Click
, wxEVT_COMMAND_CHECKBOX_CLICKED
, wxCommandEvent
)
135 wxPROPERTY( Font
, wxFont
, SetFont
, GetFont
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
136 wxPROPERTY( Label
,wxString
, SetLabel
, GetLabel
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
137 wxPROPERTY( Value
,bool, SetValue
, GetValue
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
138 wxPROPERTY_FLAGS( WindowStyle
, wxCheckBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
139 wxEND_PROPERTIES_TABLE()
141 wxBEGIN_HANDLERS_TABLE(wxCheckBox
)
142 wxEND_HANDLERS_TABLE()
144 wxCONSTRUCTOR_6( wxCheckBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
146 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
, wxControl
)
150 // ----------------------------------------------------------------------------
151 // wxCheckBox creation
152 // ----------------------------------------------------------------------------
154 void wxCheckBox::Init()
156 m_state
= wxCHK_UNCHECKED
;
161 bool wxCheckBox::Create(wxWindow
*parent
,
163 const wxString
& label
,
165 const wxSize
& size
, long style
,
166 const wxValidator
& validator
,
167 const wxString
& name
)
171 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
174 long msStyle
= WS_TABSTOP
;
176 if ( style
& wxCHK_3STATE
)
178 msStyle
|= BS_3STATE
;
182 wxASSERT_MSG( !Is3rdStateAllowedForUser(),
183 wxT("Using wxCH_ALLOW_3RD_STATE_FOR_USER")
184 wxT(" style flag for a 2-state checkbox is useless") );
185 msStyle
|= BS_CHECKBOX
;
188 if ( style
& wxALIGN_RIGHT
)
190 msStyle
|= BS_LEFTTEXT
| BS_RIGHT
;
193 return MSWCreateControl(wxT("BUTTON"), msStyle
, pos
, size
, label
, 0);
196 // ----------------------------------------------------------------------------
197 // wxCheckBox geometry
198 // ----------------------------------------------------------------------------
200 wxSize
wxCheckBox::DoGetBestSize() const
202 static int s_checkSize
= 0;
207 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
209 s_checkSize
= dc
.GetCharHeight();
212 wxString str
= wxGetWindowText(GetHWND());
214 int wCheckbox
, hCheckbox
;
215 if ( !str
.IsEmpty() )
217 GetTextExtent(wxStripMenuCodes(str
), &wCheckbox
, &hCheckbox
);
218 wCheckbox
+= s_checkSize
+ GetCharWidth();
220 if ( hCheckbox
< s_checkSize
)
221 hCheckbox
= s_checkSize
;
225 wCheckbox
= s_checkSize
;
226 hCheckbox
= s_checkSize
;
232 wxSize
best(wCheckbox
, hCheckbox
);
237 // ----------------------------------------------------------------------------
238 // wxCheckBox operations
239 // ----------------------------------------------------------------------------
241 void wxCheckBox::SetValue(bool val
)
243 Set3StateValue(val
? wxCHK_CHECKED
: wxCHK_UNCHECKED
);
246 bool wxCheckBox::GetValue() const
248 return Get3StateValue() != wxCHK_UNCHECKED
;
251 void wxCheckBox::Command(wxCommandEvent
& event
)
253 int state
= event
.GetInt();
254 wxCHECK_RET( (state
== wxCHK_UNCHECKED
) || (state
== wxCHK_CHECKED
)
255 || (state
== wxCHK_UNDETERMINED
),
256 wxT("event.GetInt() returned an invalid checkbox state") );
258 Set3StateValue((wxCheckBoxState
) state
);
259 ProcessCommand(event
);
262 wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED
== BST_UNCHECKED
263 && wxCHK_CHECKED
== BST_CHECKED
264 && wxCHK_UNDETERMINED
== BST_INDETERMINATE
, EnumValuesIncorrect
);
266 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
269 if ( !IsOwnerDrawn() )
270 ::SendMessage(GetHwnd(), BM_SETCHECK
, (WPARAM
) state
, 0);
271 else // owner drawn buttons don't react to this message
275 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
280 bool wxCheckBox::MSWCommand(WXUINT cmd
, WXWORD
WXUNUSED(id
))
282 if ( cmd
!= BN_CLICKED
&& cmd
!= BN_DBLCLK
)
285 // first update the value so that user event handler gets the new checkbox
288 // ownerdrawn buttons don't manage their state themselves unlike usual
289 // auto checkboxes so do it ourselves in any case
290 wxCheckBoxState state
;
291 if ( Is3rdStateAllowedForUser() )
293 state
= (wxCheckBoxState
)((m_state
+ 1) % 3);
295 else // 2 state checkbox (at least from users point of view)
297 // note that wxCHK_UNDETERMINED also becomes unchecked when clicked
298 state
= m_state
== wxCHK_UNCHECKED
? wxCHK_CHECKED
: wxCHK_UNCHECKED
;
301 DoSet3StateValue(state
);
304 // generate the event
305 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, m_windowId
);
308 event
.SetEventObject(this);
309 ProcessCommand(event
);
314 // ----------------------------------------------------------------------------
315 // owner drawn checkboxes stuff
316 // ----------------------------------------------------------------------------
318 bool wxCheckBox::SetForegroundColour(const wxColour
& colour
)
320 if ( !wxCheckBoxBase::SetForegroundColour(colour
) )
323 // the only way to change the checkbox foreground colour under Windows XP
324 // is to owner draw it
325 if ( wxUxThemeEngine::GetIfActive() )
326 MakeOwnerDrawn(colour
.Ok());
331 bool wxCheckBox::IsOwnerDrawn() const
334 (::GetWindowLong(GetHwnd(), GWL_STYLE
) & BS_OWNERDRAW
) == BS_OWNERDRAW
;
337 void wxCheckBox::MakeOwnerDrawn(bool ownerDrawn
)
339 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
341 // note that BS_CHECKBOX & BS_OWNERDRAW != 0 so we can't operate on
342 // them as on independent style bits
345 style
&= ~(BS_CHECKBOX
| BS_3STATE
);
346 style
|= BS_OWNERDRAW
;
348 Connect(wxEVT_ENTER_WINDOW
,
349 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
350 Connect(wxEVT_LEAVE_WINDOW
,
351 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
352 Connect(wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
353 Connect(wxEVT_LEFT_UP
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
354 Connect(wxEVT_SET_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
355 Connect(wxEVT_KILL_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
357 else // reset to default colour
359 style
&= ~BS_OWNERDRAW
;
360 style
|= HasFlag(wxCHK_3STATE
) ? BS_3STATE
: BS_CHECKBOX
;
362 Disconnect(wxEVT_ENTER_WINDOW
,
363 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
364 Disconnect(wxEVT_LEAVE_WINDOW
,
365 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
366 Disconnect(wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
367 Disconnect(wxEVT_LEFT_UP
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
368 Disconnect(wxEVT_SET_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
369 Disconnect(wxEVT_KILL_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
372 ::SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
376 // ensure that controls state is consistent with internal state
377 DoSet3StateValue(m_state
);
381 void wxCheckBox::OnMouseEnterOrLeave(wxMouseEvent
& event
)
383 m_isHot
= event
.GetEventType() == wxEVT_ENTER_WINDOW
;
392 void wxCheckBox::OnMouseLeft(wxMouseEvent
& event
)
394 // TODO: we should capture the mouse here to be notified about left up
395 // event but this interferes with BN_CLICKED generation so if we
396 // want to do this we'd need to generate them ourselves
397 m_isPressed
= event
.GetEventType() == wxEVT_LEFT_DOWN
;
403 void wxCheckBox::OnFocus(wxFocusEvent
& event
)
410 bool wxCheckBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
412 DRAWITEMSTRUCT
*dis
= (DRAWITEMSTRUCT
*)item
;
414 if ( !IsOwnerDrawn() || dis
->CtlType
!= ODT_BUTTON
)
415 return wxCheckBoxBase::MSWOnDraw(item
);
417 // calculate the rectangles for the check mark itself and the label
419 RECT
& rect
= dis
->rcItem
;
423 rectLabel
.top
= rect
.top
;
425 rectLabel
.bottom
= rect
.bottom
;
426 const int checkSize
= GetBestSize().y
;
427 const int MARGIN
= 3;
429 const bool isRightAligned
= HasFlag(wxALIGN_RIGHT
);
430 if ( isRightAligned
)
432 rectCheck
.right
= rect
.right
;
433 rectCheck
.left
= rectCheck
.right
- checkSize
;
435 rectLabel
.right
= rectCheck
.left
- MARGIN
;
436 rectLabel
.left
= rect
.left
;
438 else // normal, left-aligned checkbox
440 rectCheck
.left
= rect
.left
;
441 rectCheck
.right
= rectCheck
.left
+ checkSize
;
443 rectLabel
.left
= rectCheck
.right
+ MARGIN
;
444 rectLabel
.right
= rect
.right
;
447 // show we draw a focus rect?
448 const bool isFocused
= m_isPressed
|| FindFocus() == this;
451 // draw the checkbox itself: note that this should really, really be in
452 // wxRendererNative but unfortunately we can't add a new virtual function
453 // to it without breaking backwards compatibility
455 // classic Win32 version -- this can be useful when we move this into
457 #if defined(__WXWINCE__) || !wxUSE_UXTHEME
458 UINT state
= DFCS_BUTTONCHECK
;
460 state
|= DFCS_INACTIVE
;
461 switch ( Get3StateValue() )
464 state
|= DFCS_CHECKED
;
467 case wxCHK_UNDETERMINED
:
468 state
|= DFCS_PUSHED
;
472 wxFAIL_MSG( _T("unexpected Get3StateValue() return value") );
475 case wxCHK_UNCHECKED
:
476 // no extra styles needed
480 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
483 if ( !::DrawFrameControl(hdc
, &rectCheck
, DFC_BUTTON
, state
) )
485 wxLogLastError(_T("DrawFrameControl(DFC_BUTTON)"));
488 wxUxThemeEngine
*themeEngine
= wxUxThemeEngine::GetIfActive();
492 wxUxThemeHandle
theme(this, L
"BUTTON");
497 switch ( Get3StateValue() )
500 state
= CBS_CHECKEDNORMAL
;
503 case wxCHK_UNDETERMINED
:
504 state
= CBS_MIXEDNORMAL
;
508 wxFAIL_MSG( _T("unexpected Get3StateValue() return value") );
511 case wxCHK_UNCHECKED
:
512 state
= CBS_UNCHECKEDNORMAL
;
517 state
+= CBS_DISABLED_OFFSET
;
518 else if ( m_isPressed
)
519 state
+= CBS_PRESSED_OFFSET
;
521 state
+= CBS_HOT_OFFSET
;
523 HRESULT hr
= themeEngine
->DrawThemeBackground
534 wxLogApiError(_T("DrawThemeBackground(BP_CHECKBOX)"), hr
);
539 const wxString
& label
= GetLabel();
541 // first we need to measure it
542 UINT fmt
= DT_NOCLIP
;
544 // drawing underlying doesn't look well with focus rect (and the native
545 // control doesn't do it)
547 fmt
|= DT_HIDEPREFIX
;
548 if ( isRightAligned
)
550 // TODO: also use DT_HIDEPREFIX if the system is configured so
552 // we need to get the label real size first if we have to draw a focus rect
556 if ( !::DrawText(hdc
, label
, label
.length(), &rectLabel
,
559 wxLogLastError(_T("DrawText(DT_CALCRECT)"));
565 ::SetTextColor(hdc
, ::GetSysColor(COLOR_GRAYTEXT
));
568 if ( !::DrawText(hdc
, label
, label
.length(), &rectLabel
, fmt
) )
570 wxLogLastError(_T("DrawText()"));
573 // finally draw the focus
578 if ( !::DrawFocusRect(hdc
, &rectLabel
) )
580 wxLogLastError(_T("DrawFocusRect()"));
587 #endif // wxUSE_CHECKBOX