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/msw/uxtheme.h"
39 #include "wx/renderer.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
46 #define BST_UNCHECKED 0x0000
50 #define BST_CHECKED 0x0001
53 #ifndef BST_INDETERMINATE
54 #define BST_INDETERMINATE 0x0002
58 #define DT_HIDEPREFIX 0x00100000
65 // these values are defined in tmschema.h (except the first one)
72 CBS_UNCHECKEDDISABLED
,
87 CBS_PRESSED_OFFSET
= 2,
88 CBS_DISABLED_OFFSET
= 3
91 // ============================================================================
93 // ============================================================================
95 #if wxUSE_EXTENDED_RTTI
96 WX_DEFINE_FLAGS( wxCheckBoxStyle
)
98 wxBEGIN_FLAGS( wxCheckBoxStyle
)
99 // new style border flags, we put them first to
100 // use them for streaming out
101 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
102 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
103 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
104 wxFLAGS_MEMBER(wxBORDER_RAISED
)
105 wxFLAGS_MEMBER(wxBORDER_STATIC
)
106 wxFLAGS_MEMBER(wxBORDER_NONE
)
108 // old style border flags
109 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
110 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
111 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
112 wxFLAGS_MEMBER(wxRAISED_BORDER
)
113 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
114 wxFLAGS_MEMBER(wxNO_BORDER
)
116 // standard window styles
117 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
118 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
119 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
120 wxFLAGS_MEMBER(wxWANTS_CHARS
)
121 wxFLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE
)
122 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
123 wxFLAGS_MEMBER(wxVSCROLL
)
124 wxFLAGS_MEMBER(wxHSCROLL
)
126 wxEND_FLAGS( wxCheckBoxStyle
)
128 IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckBox
, wxControl
,"wx/checkbox.h")
130 wxBEGIN_PROPERTIES_TABLE(wxCheckBox
)
131 wxEVENT_PROPERTY( Click
, wxEVT_COMMAND_CHECKBOX_CLICKED
, wxCommandEvent
)
133 wxPROPERTY( Font
, wxFont
, SetFont
, GetFont
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
134 wxPROPERTY( Label
,wxString
, SetLabel
, GetLabel
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
135 wxPROPERTY( Value
,bool, SetValue
, GetValue
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
136 wxPROPERTY_FLAGS( WindowStyle
, wxCheckBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
137 wxEND_PROPERTIES_TABLE()
139 wxBEGIN_HANDLERS_TABLE(wxCheckBox
)
140 wxEND_HANDLERS_TABLE()
142 wxCONSTRUCTOR_6( wxCheckBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
144 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
, wxControl
)
148 // ----------------------------------------------------------------------------
149 // wxCheckBox creation
150 // ----------------------------------------------------------------------------
152 void wxCheckBox::Init()
154 m_state
= wxCHK_UNCHECKED
;
159 bool wxCheckBox::Create(wxWindow
*parent
,
161 const wxString
& label
,
163 const wxSize
& size
, long style
,
164 const wxValidator
& validator
,
165 const wxString
& name
)
169 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
172 long msStyle
= WS_TABSTOP
;
174 if ( style
& wxCHK_3STATE
)
176 msStyle
|= BS_3STATE
;
180 wxASSERT_MSG( !Is3rdStateAllowedForUser(),
181 wxT("Using wxCH_ALLOW_3RD_STATE_FOR_USER")
182 wxT(" style flag for a 2-state checkbox is useless") );
183 msStyle
|= BS_CHECKBOX
;
186 if ( style
& wxALIGN_RIGHT
)
188 msStyle
|= BS_LEFTTEXT
| BS_RIGHT
;
191 return MSWCreateControl(wxT("BUTTON"), msStyle
, pos
, size
, label
, 0);
194 // ----------------------------------------------------------------------------
195 // wxCheckBox geometry
196 // ----------------------------------------------------------------------------
198 wxSize
wxCheckBox::DoGetBestSize() const
200 static int s_checkSize
= 0;
205 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
207 s_checkSize
= dc
.GetCharHeight();
210 wxString str
= wxGetWindowText(GetHWND());
212 int wCheckbox
, hCheckbox
;
215 GetTextExtent(GetLabelText(str
), &wCheckbox
, &hCheckbox
);
216 wCheckbox
+= s_checkSize
+ GetCharWidth();
218 if ( hCheckbox
< s_checkSize
)
219 hCheckbox
= s_checkSize
;
223 wCheckbox
= s_checkSize
;
224 hCheckbox
= s_checkSize
;
230 wxSize
best(wCheckbox
, hCheckbox
);
235 // ----------------------------------------------------------------------------
236 // wxCheckBox operations
237 // ----------------------------------------------------------------------------
239 void wxCheckBox::SetValue(bool val
)
241 Set3StateValue(val
? wxCHK_CHECKED
: wxCHK_UNCHECKED
);
244 bool wxCheckBox::GetValue() const
246 return Get3StateValue() != wxCHK_UNCHECKED
;
249 void wxCheckBox::Command(wxCommandEvent
& event
)
251 int state
= event
.GetInt();
252 wxCHECK_RET( (state
== wxCHK_UNCHECKED
) || (state
== wxCHK_CHECKED
)
253 || (state
== wxCHK_UNDETERMINED
),
254 wxT("event.GetInt() returned an invalid checkbox state") );
256 Set3StateValue((wxCheckBoxState
) state
);
257 ProcessCommand(event
);
260 wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED
== BST_UNCHECKED
261 && wxCHK_CHECKED
== BST_CHECKED
262 && wxCHK_UNDETERMINED
== BST_INDETERMINATE
, EnumValuesIncorrect
);
264 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
267 if ( !IsOwnerDrawn() )
268 ::SendMessage(GetHwnd(), BM_SETCHECK
, (WPARAM
) state
, 0);
269 else // owner drawn buttons don't react to this message
273 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
278 bool wxCheckBox::MSWCommand(WXUINT cmd
, WXWORD
WXUNUSED(id
))
280 if ( cmd
!= BN_CLICKED
&& cmd
!= BN_DBLCLK
)
283 // first update the value so that user event handler gets the new checkbox
286 // ownerdrawn buttons don't manage their state themselves unlike usual
287 // auto checkboxes so do it ourselves in any case
288 wxCheckBoxState state
;
289 if ( Is3rdStateAllowedForUser() )
291 state
= (wxCheckBoxState
)((m_state
+ 1) % 3);
293 else // 2 state checkbox (at least from users point of view)
295 // note that wxCHK_UNDETERMINED also becomes unchecked when clicked
296 state
= m_state
== wxCHK_UNCHECKED
? wxCHK_CHECKED
: wxCHK_UNCHECKED
;
299 DoSet3StateValue(state
);
302 // generate the event
303 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, m_windowId
);
306 event
.SetEventObject(this);
307 ProcessCommand(event
);
312 // ----------------------------------------------------------------------------
313 // owner drawn checkboxes stuff
314 // ----------------------------------------------------------------------------
316 bool wxCheckBox::SetForegroundColour(const wxColour
& colour
)
318 if ( !wxCheckBoxBase::SetForegroundColour(colour
) )
321 // the only way to change the checkbox foreground colour under Windows XP
322 // is to owner draw it
323 if ( wxUxThemeEngine::GetIfActive() )
324 MakeOwnerDrawn(colour
.Ok());
329 bool wxCheckBox::IsOwnerDrawn() const
332 (::GetWindowLong(GetHwnd(), GWL_STYLE
) & BS_OWNERDRAW
) == BS_OWNERDRAW
;
335 void wxCheckBox::MakeOwnerDrawn(bool ownerDrawn
)
337 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
339 // note that BS_CHECKBOX & BS_OWNERDRAW != 0 so we can't operate on
340 // them as on independent style bits
343 style
&= ~(BS_CHECKBOX
| BS_3STATE
);
344 style
|= BS_OWNERDRAW
;
346 Connect(wxEVT_ENTER_WINDOW
,
347 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
348 Connect(wxEVT_LEAVE_WINDOW
,
349 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
350 Connect(wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
351 Connect(wxEVT_LEFT_UP
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
352 Connect(wxEVT_SET_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
353 Connect(wxEVT_KILL_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
355 else // reset to default colour
357 style
&= ~BS_OWNERDRAW
;
358 style
|= HasFlag(wxCHK_3STATE
) ? BS_3STATE
: BS_CHECKBOX
;
360 Disconnect(wxEVT_ENTER_WINDOW
,
361 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
362 Disconnect(wxEVT_LEAVE_WINDOW
,
363 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
364 Disconnect(wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
365 Disconnect(wxEVT_LEFT_UP
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
366 Disconnect(wxEVT_SET_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
367 Disconnect(wxEVT_KILL_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
370 ::SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
374 // ensure that controls state is consistent with internal state
375 DoSet3StateValue(m_state
);
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
454 flags
|= wxCONTROL_DISABLED
;
455 switch ( Get3StateValue() )
458 flags
|= wxCONTROL_CHECKED
;
461 case wxCHK_UNDETERMINED
:
462 flags
|= wxCONTROL_PRESSED
;
466 wxFAIL_MSG( _T("unexpected Get3StateValue() return value") );
469 case wxCHK_UNCHECKED
:
470 // no extra styles needed
474 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
475 flags
|= wxCONTROL_CURRENT
;
477 wxRendererNative::Get().
478 DrawCheckBox(this, dc
, wxRectFromRECT(rectCheck
), flags
);
481 const wxString
& label
= GetLabel();
483 // first we need to measure it
484 UINT fmt
= DT_NOCLIP
;
486 // drawing underlying doesn't look well with focus rect (and the native
487 // control doesn't do it)
489 fmt
|= DT_HIDEPREFIX
;
490 if ( isRightAligned
)
492 // TODO: also use DT_HIDEPREFIX if the system is configured so
494 // we need to get the label real size first if we have to draw a focus rect
498 if ( !::DrawText(hdc
, label
.wx_str(), label
.length(), &rectLabel
,
501 wxLogLastError(_T("DrawText(DT_CALCRECT)"));
507 ::SetTextColor(hdc
, ::GetSysColor(COLOR_GRAYTEXT
));
510 if ( !::DrawText(hdc
, label
.wx_str(), label
.length(), &rectLabel
, fmt
) )
512 wxLogLastError(_T("DrawText()"));
515 // finally draw the focus
520 if ( !::DrawFocusRect(hdc
, &rectLabel
) )
522 wxLogLastError(_T("DrawFocusRect()"));
529 #endif // wxUSE_CHECKBOX