1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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"
29 #include "wx/checkbox.h"
33 #include "wx/dcscreen.h"
34 #include "wx/settings.h"
37 #include "wx/msw/dc.h" // for wxDCTemp
38 #include "wx/renderer.h"
39 #include "wx/msw/uxtheme.h"
40 #include "wx/msw/private/button.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
47 #define BST_UNCHECKED 0x0000
51 #define BST_CHECKED 0x0001
54 #ifndef BST_INDETERMINATE
55 #define BST_INDETERMINATE 0x0002
59 #define DT_HIDEPREFIX 0x00100000
66 // these values are defined in tmschema.h (except the first one)
73 CBS_UNCHECKEDDISABLED
,
88 CBS_PRESSED_OFFSET
= 2,
89 CBS_DISABLED_OFFSET
= 3
92 // ============================================================================
94 // ============================================================================
96 #if wxUSE_EXTENDED_RTTI
97 WX_DEFINE_FLAGS( wxCheckBoxStyle
)
99 wxBEGIN_FLAGS( wxCheckBoxStyle
)
100 // new style border flags, we put them first to
101 // use them for streaming out
102 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
103 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
104 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
105 wxFLAGS_MEMBER(wxBORDER_RAISED
)
106 wxFLAGS_MEMBER(wxBORDER_STATIC
)
107 wxFLAGS_MEMBER(wxBORDER_NONE
)
109 // old style border flags
110 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
111 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
112 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
113 wxFLAGS_MEMBER(wxRAISED_BORDER
)
114 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
115 wxFLAGS_MEMBER(wxNO_BORDER
)
117 // standard window styles
118 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
119 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
120 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
121 wxFLAGS_MEMBER(wxWANTS_CHARS
)
122 wxFLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE
)
123 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
124 wxFLAGS_MEMBER(wxVSCROLL
)
125 wxFLAGS_MEMBER(wxHSCROLL
)
127 wxEND_FLAGS( wxCheckBoxStyle
)
129 IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckBox
, wxControl
,"wx/checkbox.h")
131 wxBEGIN_PROPERTIES_TABLE(wxCheckBox
)
132 wxEVENT_PROPERTY( Click
, wxEVT_COMMAND_CHECKBOX_CLICKED
, wxCommandEvent
)
134 wxPROPERTY( Font
, wxFont
, SetFont
, GetFont
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
135 wxPROPERTY( Label
,wxString
, SetLabel
, GetLabel
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
136 wxPROPERTY( Value
,bool, SetValue
, GetValue
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
137 wxPROPERTY_FLAGS( WindowStyle
, wxCheckBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
138 wxEND_PROPERTIES_TABLE()
140 wxBEGIN_HANDLERS_TABLE(wxCheckBox
)
141 wxEND_HANDLERS_TABLE()
143 wxCONSTRUCTOR_6( wxCheckBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
145 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
, wxControl
)
149 // ----------------------------------------------------------------------------
150 // wxCheckBox creation
151 // ----------------------------------------------------------------------------
153 void wxCheckBox::Init()
155 m_state
= wxCHK_UNCHECKED
;
160 bool wxCheckBox::Create(wxWindow
*parent
,
162 const wxString
& label
,
164 const wxSize
& size
, long style
,
165 const wxValidator
& validator
,
166 const wxString
& name
)
170 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
173 long msStyle
= WS_TABSTOP
;
175 if ( style
& wxCHK_3STATE
)
177 msStyle
|= BS_3STATE
;
181 wxASSERT_MSG( !Is3rdStateAllowedForUser(),
182 wxT("Using wxCH_ALLOW_3RD_STATE_FOR_USER")
183 wxT(" style flag for a 2-state checkbox is useless") );
184 msStyle
|= BS_CHECKBOX
;
187 if ( style
& wxALIGN_RIGHT
)
189 msStyle
|= BS_LEFTTEXT
| BS_RIGHT
;
192 msStyle
|= wxMSWButton::GetMultilineStyle(label
);
194 return MSWCreateControl(wxT("BUTTON"), msStyle
, pos
, size
, label
, 0);
197 // ----------------------------------------------------------------------------
198 // wxCheckBox geometry
199 // ----------------------------------------------------------------------------
201 wxSize
wxCheckBox::DoGetBestSize() const
203 static int s_checkSize
= 0;
208 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
210 s_checkSize
= dc
.GetCharHeight();
213 wxString str
= wxGetWindowText(GetHWND());
215 int wCheckbox
, hCheckbox
;
218 wxClientDC
dc(wx_const_cast(wxCheckBox
*, this));
219 dc
.SetFont(GetFont());
220 dc
.GetMultiLineTextExtent(GetLabelText(str
), &wCheckbox
, &hCheckbox
);
221 wCheckbox
+= s_checkSize
+ GetCharWidth();
223 if ( hCheckbox
< s_checkSize
)
224 hCheckbox
= s_checkSize
;
228 wCheckbox
= s_checkSize
;
229 hCheckbox
= s_checkSize
;
235 wxSize
best(wCheckbox
, hCheckbox
);
240 // ----------------------------------------------------------------------------
241 // wxCheckBox operations
242 // ----------------------------------------------------------------------------
244 void wxCheckBox::SetLabel(const wxString
& label
)
246 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label
);
248 wxCheckBoxBase::SetLabel(label
);
251 void wxCheckBox::SetValue(bool val
)
253 Set3StateValue(val
? wxCHK_CHECKED
: wxCHK_UNCHECKED
);
256 bool wxCheckBox::GetValue() const
258 return Get3StateValue() != wxCHK_UNCHECKED
;
261 void wxCheckBox::Command(wxCommandEvent
& event
)
263 int state
= event
.GetInt();
264 wxCHECK_RET( (state
== wxCHK_UNCHECKED
) || (state
== wxCHK_CHECKED
)
265 || (state
== wxCHK_UNDETERMINED
),
266 wxT("event.GetInt() returned an invalid checkbox state") );
268 Set3StateValue((wxCheckBoxState
) state
);
269 ProcessCommand(event
);
272 wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED
== BST_UNCHECKED
273 && wxCHK_CHECKED
== BST_CHECKED
274 && wxCHK_UNDETERMINED
== BST_INDETERMINATE
, EnumValuesIncorrect
);
276 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
279 if ( !IsOwnerDrawn() )
280 ::SendMessage(GetHwnd(), BM_SETCHECK
, (WPARAM
) state
, 0);
281 else // owner drawn buttons don't react to this message
285 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
290 bool wxCheckBox::MSWCommand(WXUINT cmd
, WXWORD
WXUNUSED(id
))
292 if ( cmd
!= BN_CLICKED
&& cmd
!= BN_DBLCLK
)
295 // first update the value so that user event handler gets the new checkbox
298 // ownerdrawn buttons don't manage their state themselves unlike usual
299 // auto checkboxes so do it ourselves in any case
300 wxCheckBoxState state
;
301 if ( Is3rdStateAllowedForUser() )
303 state
= (wxCheckBoxState
)((m_state
+ 1) % 3);
305 else // 2 state checkbox (at least from users point of view)
307 // note that wxCHK_UNDETERMINED also becomes unchecked when clicked
308 state
= m_state
== wxCHK_UNCHECKED
? wxCHK_CHECKED
: wxCHK_UNCHECKED
;
311 DoSet3StateValue(state
);
314 // generate the event
315 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, m_windowId
);
318 event
.SetEventObject(this);
319 ProcessCommand(event
);
324 // ----------------------------------------------------------------------------
325 // owner drawn checkboxes stuff
326 // ----------------------------------------------------------------------------
328 bool wxCheckBox::SetForegroundColour(const wxColour
& colour
)
330 if ( !wxCheckBoxBase::SetForegroundColour(colour
) )
333 // the only way to change the checkbox foreground colour under Windows XP
334 // is to owner draw it
335 if ( wxUxThemeEngine::GetIfActive() )
336 MakeOwnerDrawn(colour
.Ok());
341 bool wxCheckBox::IsOwnerDrawn() const
344 (::GetWindowLong(GetHwnd(), GWL_STYLE
) & BS_OWNERDRAW
) == BS_OWNERDRAW
;
347 void wxCheckBox::MakeOwnerDrawn(bool ownerDrawn
)
349 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
351 // note that BS_CHECKBOX & BS_OWNERDRAW != 0 so we can't operate on
352 // them as on independent style bits
355 style
&= ~(BS_CHECKBOX
| BS_3STATE
);
356 style
|= BS_OWNERDRAW
;
358 Connect(wxEVT_ENTER_WINDOW
,
359 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
360 Connect(wxEVT_LEAVE_WINDOW
,
361 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
362 Connect(wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
363 Connect(wxEVT_LEFT_UP
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
364 Connect(wxEVT_SET_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
365 Connect(wxEVT_KILL_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
367 else // reset to default colour
369 style
&= ~BS_OWNERDRAW
;
370 style
|= HasFlag(wxCHK_3STATE
) ? BS_3STATE
: BS_CHECKBOX
;
372 Disconnect(wxEVT_ENTER_WINDOW
,
373 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
374 Disconnect(wxEVT_LEAVE_WINDOW
,
375 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
376 Disconnect(wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
377 Disconnect(wxEVT_LEFT_UP
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
378 Disconnect(wxEVT_SET_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
379 Disconnect(wxEVT_KILL_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
382 ::SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
386 // ensure that controls state is consistent with internal state
387 DoSet3StateValue(m_state
);
391 void wxCheckBox::OnMouseEnterOrLeave(wxMouseEvent
& event
)
393 m_isHot
= event
.GetEventType() == wxEVT_ENTER_WINDOW
;
402 void wxCheckBox::OnMouseLeft(wxMouseEvent
& event
)
404 // TODO: we should capture the mouse here to be notified about left up
405 // event but this interferes with BN_CLICKED generation so if we
406 // want to do this we'd need to generate them ourselves
407 m_isPressed
= event
.GetEventType() == wxEVT_LEFT_DOWN
;
413 void wxCheckBox::OnFocus(wxFocusEvent
& event
)
420 bool wxCheckBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
422 DRAWITEMSTRUCT
*dis
= (DRAWITEMSTRUCT
*)item
;
424 if ( !IsOwnerDrawn() || dis
->CtlType
!= ODT_BUTTON
)
425 return wxCheckBoxBase::MSWOnDraw(item
);
427 // calculate the rectangles for the check mark itself and the label
429 RECT
& rect
= dis
->rcItem
;
433 rectLabel
.top
= rect
.top
;
435 rectLabel
.bottom
= rect
.bottom
;
436 const int checkSize
= GetBestSize().y
;
437 const int MARGIN
= 3;
439 const bool isRightAligned
= HasFlag(wxALIGN_RIGHT
);
440 if ( isRightAligned
)
442 rectCheck
.right
= rect
.right
;
443 rectCheck
.left
= rectCheck
.right
- checkSize
;
445 rectLabel
.right
= rectCheck
.left
- MARGIN
;
446 rectLabel
.left
= rect
.left
;
448 else // normal, left-aligned checkbox
450 rectCheck
.left
= rect
.left
;
451 rectCheck
.right
= rectCheck
.left
+ checkSize
;
453 rectLabel
.left
= rectCheck
.right
+ MARGIN
;
454 rectLabel
.right
= rect
.right
;
457 // show we draw a focus rect?
458 const bool isFocused
= m_isPressed
|| FindFocus() == this;
461 // draw the checkbox itself
466 flags
|= wxCONTROL_DISABLED
;
467 switch ( Get3StateValue() )
470 flags
|= wxCONTROL_CHECKED
;
473 case wxCHK_UNDETERMINED
:
474 flags
|= wxCONTROL_PRESSED
;
478 wxFAIL_MSG( _T("unexpected Get3StateValue() return value") );
481 case wxCHK_UNCHECKED
:
482 // no extra styles needed
486 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
487 flags
|= wxCONTROL_CURRENT
;
489 wxRendererNative::Get().
490 DrawCheckBox(this, dc
, wxRectFromRECT(rectCheck
), flags
);
493 const wxString
& label
= GetLabel();
495 // first we need to measure it
496 UINT fmt
= DT_NOCLIP
;
498 // drawing underlying doesn't look well with focus rect (and the native
499 // control doesn't do it)
501 fmt
|= DT_HIDEPREFIX
;
502 if ( isRightAligned
)
504 // TODO: also use DT_HIDEPREFIX if the system is configured so
506 // we need to get the label real size first if we have to draw a focus rect
510 if ( !::DrawText(hdc
, label
.wx_str(), label
.length(), &rectLabel
,
513 wxLogLastError(_T("DrawText(DT_CALCRECT)"));
519 ::SetTextColor(hdc
, ::GetSysColor(COLOR_GRAYTEXT
));
522 if ( !::DrawText(hdc
, label
.wx_str(), label
.length(), &rectLabel
, fmt
) )
524 wxLogLastError(_T("DrawText()"));
527 // finally draw the focus
532 if ( !::DrawFocusRect(hdc
, &rectLabel
) )
534 wxLogLastError(_T("DrawFocusRect()"));
541 #endif // wxUSE_CHECKBOX