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/dcclient.h"
34 #include "wx/dcscreen.h"
35 #include "wx/settings.h"
38 #include "wx/msw/dc.h" // for wxDCTemp
39 #include "wx/renderer.h"
40 #include "wx/msw/uxtheme.h"
41 #include "wx/msw/private/button.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
48 #define BST_UNCHECKED 0x0000
52 #define BST_CHECKED 0x0001
55 #ifndef BST_INDETERMINATE
56 #define BST_INDETERMINATE 0x0002
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 msStyle
|= wxMSWButton::GetMultilineStyle(label
);
195 return MSWCreateControl(wxT("BUTTON"), msStyle
, pos
, size
, label
, 0);
198 // ----------------------------------------------------------------------------
199 // wxCheckBox geometry
200 // ----------------------------------------------------------------------------
202 wxSize
wxCheckBox::DoGetBestSize() const
204 static int s_checkSize
= 0;
209 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
211 s_checkSize
= dc
.GetCharHeight();
214 wxString str
= wxGetWindowText(GetHWND());
216 int wCheckbox
, hCheckbox
;
219 wxClientDC
dc(wx_const_cast(wxCheckBox
*, this));
220 dc
.SetFont(GetFont());
221 dc
.GetMultiLineTextExtent(GetLabelText(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::SetLabel(const wxString
& label
)
247 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label
);
249 wxCheckBoxBase::SetLabel(label
);
252 void wxCheckBox::SetValue(bool val
)
254 Set3StateValue(val
? wxCHK_CHECKED
: wxCHK_UNCHECKED
);
257 bool wxCheckBox::GetValue() const
259 return Get3StateValue() != wxCHK_UNCHECKED
;
262 void wxCheckBox::Command(wxCommandEvent
& event
)
264 int state
= event
.GetInt();
265 wxCHECK_RET( (state
== wxCHK_UNCHECKED
) || (state
== wxCHK_CHECKED
)
266 || (state
== wxCHK_UNDETERMINED
),
267 wxT("event.GetInt() returned an invalid checkbox state") );
269 Set3StateValue((wxCheckBoxState
) state
);
270 ProcessCommand(event
);
273 wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED
== BST_UNCHECKED
274 && wxCHK_CHECKED
== BST_CHECKED
275 && wxCHK_UNDETERMINED
== BST_INDETERMINATE
, EnumValuesIncorrect
);
277 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
280 if ( !IsOwnerDrawn() )
281 ::SendMessage(GetHwnd(), BM_SETCHECK
, (WPARAM
) state
, 0);
282 else // owner drawn buttons don't react to this message
286 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
291 bool wxCheckBox::MSWCommand(WXUINT cmd
, WXWORD
WXUNUSED(id
))
293 if ( cmd
!= BN_CLICKED
&& cmd
!= BN_DBLCLK
)
296 // first update the value so that user event handler gets the new checkbox
299 // ownerdrawn buttons don't manage their state themselves unlike usual
300 // auto checkboxes so do it ourselves in any case
301 wxCheckBoxState state
;
302 if ( Is3rdStateAllowedForUser() )
304 state
= (wxCheckBoxState
)((m_state
+ 1) % 3);
306 else // 2 state checkbox (at least from users point of view)
308 // note that wxCHK_UNDETERMINED also becomes unchecked when clicked
309 state
= m_state
== wxCHK_UNCHECKED
? wxCHK_CHECKED
: wxCHK_UNCHECKED
;
312 DoSet3StateValue(state
);
315 // generate the event
316 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, m_windowId
);
319 event
.SetEventObject(this);
320 ProcessCommand(event
);
325 // ----------------------------------------------------------------------------
326 // owner drawn checkboxes stuff
327 // ----------------------------------------------------------------------------
329 bool wxCheckBox::SetForegroundColour(const wxColour
& colour
)
331 if ( !wxCheckBoxBase::SetForegroundColour(colour
) )
334 // the only way to change the checkbox foreground colour under Windows XP
335 // is to owner draw it
336 if ( wxUxThemeEngine::GetIfActive() )
337 MakeOwnerDrawn(colour
.Ok());
342 bool wxCheckBox::IsOwnerDrawn() const
345 (::GetWindowLong(GetHwnd(), GWL_STYLE
) & BS_OWNERDRAW
) == BS_OWNERDRAW
;
348 void wxCheckBox::MakeOwnerDrawn(bool ownerDrawn
)
350 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
352 // note that BS_CHECKBOX & BS_OWNERDRAW != 0 so we can't operate on
353 // them as on independent style bits
356 style
&= ~(BS_CHECKBOX
| BS_3STATE
);
357 style
|= BS_OWNERDRAW
;
359 Connect(wxEVT_ENTER_WINDOW
,
360 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
361 Connect(wxEVT_LEAVE_WINDOW
,
362 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
363 Connect(wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
364 Connect(wxEVT_LEFT_UP
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
365 Connect(wxEVT_SET_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
366 Connect(wxEVT_KILL_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
368 else // reset to default colour
370 style
&= ~BS_OWNERDRAW
;
371 style
|= HasFlag(wxCHK_3STATE
) ? BS_3STATE
: BS_CHECKBOX
;
373 Disconnect(wxEVT_ENTER_WINDOW
,
374 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
375 Disconnect(wxEVT_LEAVE_WINDOW
,
376 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
377 Disconnect(wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
378 Disconnect(wxEVT_LEFT_UP
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
379 Disconnect(wxEVT_SET_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
380 Disconnect(wxEVT_KILL_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
383 ::SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
387 // ensure that controls state is consistent with internal state
388 DoSet3StateValue(m_state
);
392 void wxCheckBox::OnMouseEnterOrLeave(wxMouseEvent
& event
)
394 m_isHot
= event
.GetEventType() == wxEVT_ENTER_WINDOW
;
403 void wxCheckBox::OnMouseLeft(wxMouseEvent
& event
)
405 // TODO: we should capture the mouse here to be notified about left up
406 // event but this interferes with BN_CLICKED generation so if we
407 // want to do this we'd need to generate them ourselves
408 m_isPressed
= event
.GetEventType() == wxEVT_LEFT_DOWN
;
414 void wxCheckBox::OnFocus(wxFocusEvent
& event
)
421 bool wxCheckBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
423 DRAWITEMSTRUCT
*dis
= (DRAWITEMSTRUCT
*)item
;
425 if ( !IsOwnerDrawn() || dis
->CtlType
!= ODT_BUTTON
)
426 return wxCheckBoxBase::MSWOnDraw(item
);
428 // calculate the rectangles for the check mark itself and the label
430 RECT
& rect
= dis
->rcItem
;
434 rectLabel
.top
= rect
.top
;
436 rectLabel
.bottom
= rect
.bottom
;
437 const int checkSize
= GetBestSize().y
;
438 const int MARGIN
= 3;
440 const bool isRightAligned
= HasFlag(wxALIGN_RIGHT
);
441 if ( isRightAligned
)
443 rectCheck
.right
= rect
.right
;
444 rectCheck
.left
= rectCheck
.right
- checkSize
;
446 rectLabel
.right
= rectCheck
.left
- MARGIN
;
447 rectLabel
.left
= rect
.left
;
449 else // normal, left-aligned checkbox
451 rectCheck
.left
= rect
.left
;
452 rectCheck
.right
= rectCheck
.left
+ checkSize
;
454 rectLabel
.left
= rectCheck
.right
+ MARGIN
;
455 rectLabel
.right
= rect
.right
;
458 // show we draw a focus rect?
459 const bool isFocused
= m_isPressed
|| FindFocus() == this;
462 // draw the checkbox itself
467 flags
|= wxCONTROL_DISABLED
;
468 switch ( Get3StateValue() )
471 flags
|= wxCONTROL_CHECKED
;
474 case wxCHK_UNDETERMINED
:
475 flags
|= wxCONTROL_PRESSED
;
479 wxFAIL_MSG( _T("unexpected Get3StateValue() return value") );
482 case wxCHK_UNCHECKED
:
483 // no extra styles needed
487 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
488 flags
|= wxCONTROL_CURRENT
;
490 wxRendererNative::Get().
491 DrawCheckBox(this, dc
, wxRectFromRECT(rectCheck
), flags
);
494 const wxString
& label
= GetLabel();
496 // first we need to measure it
497 UINT fmt
= DT_NOCLIP
;
499 // drawing underlying doesn't look well with focus rect (and the native
500 // control doesn't do it)
502 fmt
|= DT_HIDEPREFIX
;
503 if ( isRightAligned
)
505 // TODO: also use DT_HIDEPREFIX if the system is configured so
507 // we need to get the label real size first if we have to draw a focus rect
511 if ( !::DrawText(hdc
, label
.wx_str(), label
.length(), &rectLabel
,
514 wxLogLastError(_T("DrawText(DT_CALCRECT)"));
520 ::SetTextColor(hdc
, ::GetSysColor(COLOR_GRAYTEXT
));
523 if ( !::DrawText(hdc
, label
.wx_str(), label
.length(), &rectLabel
, fmt
) )
525 wxLogLastError(_T("DrawText()"));
528 // finally draw the focus
533 if ( !::DrawFocusRect(hdc
, &rectLabel
) )
535 wxLogLastError(_T("DrawFocusRect()"));
542 #endif // wxUSE_CHECKBOX