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"
42 #include "wx/msw/missing.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
52 // these values are defined in tmschema.h (except the first one)
59 CBS_UNCHECKEDDISABLED
,
74 CBS_PRESSED_OFFSET
= 2,
75 CBS_DISABLED_OFFSET
= 3
78 // ============================================================================
80 // ============================================================================
82 #if wxUSE_EXTENDED_RTTI
83 WX_DEFINE_FLAGS( wxCheckBoxStyle
)
85 wxBEGIN_FLAGS( wxCheckBoxStyle
)
86 // new style border flags, we put them first to
87 // use them for streaming out
88 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
89 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
90 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
91 wxFLAGS_MEMBER(wxBORDER_RAISED
)
92 wxFLAGS_MEMBER(wxBORDER_STATIC
)
93 wxFLAGS_MEMBER(wxBORDER_NONE
)
95 // old style border flags
96 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
97 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
98 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
99 wxFLAGS_MEMBER(wxRAISED_BORDER
)
100 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
101 wxFLAGS_MEMBER(wxNO_BORDER
)
103 // standard window styles
104 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
105 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
106 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
107 wxFLAGS_MEMBER(wxWANTS_CHARS
)
108 wxFLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE
)
109 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
110 wxFLAGS_MEMBER(wxVSCROLL
)
111 wxFLAGS_MEMBER(wxHSCROLL
)
113 wxEND_FLAGS( wxCheckBoxStyle
)
115 IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckBox
, wxControl
,"wx/checkbox.h")
117 wxBEGIN_PROPERTIES_TABLE(wxCheckBox
)
118 wxEVENT_PROPERTY( Click
, wxEVT_COMMAND_CHECKBOX_CLICKED
, wxCommandEvent
)
120 wxPROPERTY( Font
, wxFont
, SetFont
, GetFont
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
121 wxPROPERTY( Label
,wxString
, SetLabel
, GetLabel
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
122 wxPROPERTY( Value
,bool, SetValue
, GetValue
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
123 wxPROPERTY_FLAGS( WindowStyle
, wxCheckBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
124 wxEND_PROPERTIES_TABLE()
126 wxBEGIN_HANDLERS_TABLE(wxCheckBox
)
127 wxEND_HANDLERS_TABLE()
129 wxCONSTRUCTOR_6( wxCheckBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
131 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
, wxControl
)
135 // ----------------------------------------------------------------------------
136 // wxCheckBox creation
137 // ----------------------------------------------------------------------------
139 void wxCheckBox::Init()
141 m_state
= wxCHK_UNCHECKED
;
146 bool wxCheckBox::Create(wxWindow
*parent
,
148 const wxString
& label
,
150 const wxSize
& size
, long style
,
151 const wxValidator
& validator
,
152 const wxString
& name
)
156 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
159 long msStyle
= WS_TABSTOP
;
161 if ( style
& wxCHK_3STATE
)
163 msStyle
|= BS_3STATE
;
167 wxASSERT_MSG( !Is3rdStateAllowedForUser(),
168 wxT("Using wxCH_ALLOW_3RD_STATE_FOR_USER")
169 wxT(" style flag for a 2-state checkbox is useless") );
170 msStyle
|= BS_CHECKBOX
;
173 if ( style
& wxALIGN_RIGHT
)
175 msStyle
|= BS_LEFTTEXT
| BS_RIGHT
;
178 msStyle
|= wxMSWButton::GetMultilineStyle(label
);
180 return MSWCreateControl(wxT("BUTTON"), msStyle
, pos
, size
, label
, 0);
183 // ----------------------------------------------------------------------------
184 // wxCheckBox geometry
185 // ----------------------------------------------------------------------------
187 wxSize
wxCheckBox::DoGetBestSize() const
189 static int s_checkSize
= 0;
194 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
196 s_checkSize
= dc
.GetCharHeight();
199 wxString str
= wxGetWindowText(GetHWND());
201 int wCheckbox
, hCheckbox
;
204 wxClientDC
dc(const_cast<wxCheckBox
*>(this));
205 dc
.SetFont(GetFont());
206 dc
.GetMultiLineTextExtent(GetLabelText(str
), &wCheckbox
, &hCheckbox
);
207 wCheckbox
+= s_checkSize
+ GetCharWidth();
209 if ( hCheckbox
< s_checkSize
)
210 hCheckbox
= s_checkSize
;
214 wCheckbox
= s_checkSize
;
215 hCheckbox
= s_checkSize
;
221 wxSize
best(wCheckbox
, hCheckbox
);
226 // ----------------------------------------------------------------------------
227 // wxCheckBox operations
228 // ----------------------------------------------------------------------------
230 void wxCheckBox::SetLabel(const wxString
& label
)
232 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label
);
234 wxCheckBoxBase::SetLabel(label
);
237 void wxCheckBox::SetValue(bool val
)
239 Set3StateValue(val
? wxCHK_CHECKED
: wxCHK_UNCHECKED
);
242 bool wxCheckBox::GetValue() const
244 return Get3StateValue() != wxCHK_UNCHECKED
;
247 void wxCheckBox::Command(wxCommandEvent
& event
)
249 int state
= event
.GetInt();
250 wxCHECK_RET( (state
== wxCHK_UNCHECKED
) || (state
== wxCHK_CHECKED
)
251 || (state
== wxCHK_UNDETERMINED
),
252 wxT("event.GetInt() returned an invalid checkbox state") );
254 Set3StateValue((wxCheckBoxState
) state
);
255 ProcessCommand(event
);
258 wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED
== BST_UNCHECKED
259 && wxCHK_CHECKED
== BST_CHECKED
260 && wxCHK_UNDETERMINED
== BST_INDETERMINATE
, EnumValuesIncorrect
);
262 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
265 if ( !IsOwnerDrawn() )
266 ::SendMessage(GetHwnd(), BM_SETCHECK
, (WPARAM
) state
, 0);
267 else // owner drawn buttons don't react to this message
271 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
276 bool wxCheckBox::MSWCommand(WXUINT cmd
, WXWORD
WXUNUSED(id
))
278 if ( cmd
!= BN_CLICKED
&& cmd
!= BN_DBLCLK
)
281 // first update the value so that user event handler gets the new checkbox
284 // ownerdrawn buttons don't manage their state themselves unlike usual
285 // auto checkboxes so do it ourselves in any case
286 wxCheckBoxState state
;
287 if ( Is3rdStateAllowedForUser() )
289 state
= (wxCheckBoxState
)((m_state
+ 1) % 3);
291 else // 2 state checkbox (at least from users point of view)
293 // note that wxCHK_UNDETERMINED also becomes unchecked when clicked
294 state
= m_state
== wxCHK_UNCHECKED
? wxCHK_CHECKED
: wxCHK_UNCHECKED
;
297 DoSet3StateValue(state
);
300 // generate the event
301 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, m_windowId
);
304 event
.SetEventObject(this);
305 ProcessCommand(event
);
310 // ----------------------------------------------------------------------------
311 // owner drawn checkboxes stuff
312 // ----------------------------------------------------------------------------
314 bool wxCheckBox::SetForegroundColour(const wxColour
& colour
)
316 if ( !wxCheckBoxBase::SetForegroundColour(colour
) )
319 // the only way to change the checkbox foreground colour under Windows XP
320 // is to owner draw it
321 if ( wxUxThemeEngine::GetIfActive() )
322 MakeOwnerDrawn(colour
.Ok());
327 bool wxCheckBox::IsOwnerDrawn() const
330 (::GetWindowLong(GetHwnd(), GWL_STYLE
) & BS_OWNERDRAW
) == BS_OWNERDRAW
;
333 void wxCheckBox::MakeOwnerDrawn(bool ownerDrawn
)
335 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
337 // note that BS_CHECKBOX & BS_OWNERDRAW != 0 so we can't operate on
338 // them as on independent style bits
341 style
&= ~(BS_CHECKBOX
| BS_3STATE
);
342 style
|= BS_OWNERDRAW
;
344 Connect(wxEVT_ENTER_WINDOW
,
345 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
346 Connect(wxEVT_LEAVE_WINDOW
,
347 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
348 Connect(wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
349 Connect(wxEVT_LEFT_UP
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
350 Connect(wxEVT_SET_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
351 Connect(wxEVT_KILL_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
353 else // reset to default colour
355 style
&= ~BS_OWNERDRAW
;
356 style
|= HasFlag(wxCHK_3STATE
) ? BS_3STATE
: BS_CHECKBOX
;
358 Disconnect(wxEVT_ENTER_WINDOW
,
359 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
360 Disconnect(wxEVT_LEAVE_WINDOW
,
361 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
362 Disconnect(wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
363 Disconnect(wxEVT_LEFT_UP
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
364 Disconnect(wxEVT_SET_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
365 Disconnect(wxEVT_KILL_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
368 ::SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
372 // ensure that controls state is consistent with internal state
373 DoSet3StateValue(m_state
);
377 void wxCheckBox::OnMouseEnterOrLeave(wxMouseEvent
& event
)
379 m_isHot
= event
.GetEventType() == wxEVT_ENTER_WINDOW
;
388 void wxCheckBox::OnMouseLeft(wxMouseEvent
& event
)
390 // TODO: we should capture the mouse here to be notified about left up
391 // event but this interferes with BN_CLICKED generation so if we
392 // want to do this we'd need to generate them ourselves
393 m_isPressed
= event
.GetEventType() == wxEVT_LEFT_DOWN
;
399 void wxCheckBox::OnFocus(wxFocusEvent
& event
)
406 bool wxCheckBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
408 DRAWITEMSTRUCT
*dis
= (DRAWITEMSTRUCT
*)item
;
410 if ( !IsOwnerDrawn() || dis
->CtlType
!= ODT_BUTTON
)
411 return wxCheckBoxBase::MSWOnDraw(item
);
413 // calculate the rectangles for the check mark itself and the label
415 RECT
& rect
= dis
->rcItem
;
419 rectLabel
.top
= rect
.top
;
421 rectLabel
.bottom
= rect
.bottom
;
422 const int checkSize
= GetBestSize().y
;
423 const int MARGIN
= 3;
425 const bool isRightAligned
= HasFlag(wxALIGN_RIGHT
);
426 if ( isRightAligned
)
428 rectCheck
.right
= rect
.right
;
429 rectCheck
.left
= rectCheck
.right
- checkSize
;
431 rectLabel
.right
= rectCheck
.left
- MARGIN
;
432 rectLabel
.left
= rect
.left
;
434 else // normal, left-aligned checkbox
436 rectCheck
.left
= rect
.left
;
437 rectCheck
.right
= rectCheck
.left
+ checkSize
;
439 rectLabel
.left
= rectCheck
.right
+ MARGIN
;
440 rectLabel
.right
= rect
.right
;
443 // show we draw a focus rect?
444 const bool isFocused
= m_isPressed
|| FindFocus() == this;
447 // draw the checkbox itself
452 flags
|= wxCONTROL_DISABLED
;
453 switch ( Get3StateValue() )
456 flags
|= wxCONTROL_CHECKED
;
459 case wxCHK_UNDETERMINED
:
460 flags
|= wxCONTROL_PRESSED
;
464 wxFAIL_MSG( wxT("unexpected Get3StateValue() return value") );
467 case wxCHK_UNCHECKED
:
468 // no extra styles needed
472 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
473 flags
|= wxCONTROL_CURRENT
;
475 wxRendererNative::Get().
476 DrawCheckBox(this, dc
, wxRectFromRECT(rectCheck
), flags
);
479 const wxString
& label
= GetLabel();
481 // first we need to measure it
482 UINT fmt
= DT_NOCLIP
;
484 // drawing underlying doesn't look well with focus rect (and the native
485 // control doesn't do it)
487 fmt
|= DT_HIDEPREFIX
;
488 if ( isRightAligned
)
490 // TODO: also use DT_HIDEPREFIX if the system is configured so
492 // we need to get the label real size first if we have to draw a focus rect
496 if ( !::DrawText(hdc
, label
.wx_str(), label
.length(), &rectLabel
,
499 wxLogLastError(wxT("DrawText(DT_CALCRECT)"));
505 ::SetTextColor(hdc
, ::GetSysColor(COLOR_GRAYTEXT
));
508 if ( !::DrawText(hdc
, label
.wx_str(), label
.length(), &rectLabel
, fmt
) )
510 wxLogLastError(wxT("DrawText()"));
513 // finally draw the focus
518 if ( !::DrawFocusRect(hdc
, &rectLabel
) )
520 wxLogLastError(wxT("DrawFocusRect()"));
527 #endif // wxUSE_CHECKBOX