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/uxtheme.h"
38 #include "wx/renderer.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
45 #define BST_UNCHECKED 0x0000
49 #define BST_CHECKED 0x0001
52 #ifndef BST_INDETERMINATE
53 #define BST_INDETERMINATE 0x0002
57 #define DT_HIDEPREFIX 0x00100000
64 // these values are defined in tmschema.h (except the first one)
71 CBS_UNCHECKEDDISABLED
,
86 CBS_PRESSED_OFFSET
= 2,
87 CBS_DISABLED_OFFSET
= 3
90 // ============================================================================
92 // ============================================================================
94 #if wxUSE_EXTENDED_RTTI
95 WX_DEFINE_FLAGS( wxCheckBoxStyle
)
97 wxBEGIN_FLAGS( wxCheckBoxStyle
)
98 // new style border flags, we put them first to
99 // use them for streaming out
100 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
101 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
102 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
103 wxFLAGS_MEMBER(wxBORDER_RAISED
)
104 wxFLAGS_MEMBER(wxBORDER_STATIC
)
105 wxFLAGS_MEMBER(wxBORDER_NONE
)
107 // old style border flags
108 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
109 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
110 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
111 wxFLAGS_MEMBER(wxRAISED_BORDER
)
112 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
113 wxFLAGS_MEMBER(wxNO_BORDER
)
115 // standard window styles
116 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
117 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
118 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
119 wxFLAGS_MEMBER(wxWANTS_CHARS
)
120 wxFLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE
)
121 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
122 wxFLAGS_MEMBER(wxVSCROLL
)
123 wxFLAGS_MEMBER(wxHSCROLL
)
125 wxEND_FLAGS( wxCheckBoxStyle
)
127 IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckBox
, wxControl
,"wx/checkbox.h")
129 wxBEGIN_PROPERTIES_TABLE(wxCheckBox
)
130 wxEVENT_PROPERTY( Click
, wxEVT_COMMAND_CHECKBOX_CLICKED
, wxCommandEvent
)
132 wxPROPERTY( Font
, wxFont
, SetFont
, GetFont
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
133 wxPROPERTY( Label
,wxString
, SetLabel
, GetLabel
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
134 wxPROPERTY( Value
,bool, SetValue
, GetValue
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
135 wxPROPERTY_FLAGS( WindowStyle
, wxCheckBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
136 wxEND_PROPERTIES_TABLE()
138 wxBEGIN_HANDLERS_TABLE(wxCheckBox
)
139 wxEND_HANDLERS_TABLE()
141 wxCONSTRUCTOR_6( wxCheckBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
143 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
, wxControl
)
147 // ----------------------------------------------------------------------------
148 // wxCheckBox creation
149 // ----------------------------------------------------------------------------
151 void wxCheckBox::Init()
153 m_state
= wxCHK_UNCHECKED
;
158 bool wxCheckBox::Create(wxWindow
*parent
,
160 const wxString
& label
,
162 const wxSize
& size
, long style
,
163 const wxValidator
& validator
,
164 const wxString
& name
)
168 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
171 long msStyle
= WS_TABSTOP
;
173 if ( style
& wxCHK_3STATE
)
175 msStyle
|= BS_3STATE
;
179 wxASSERT_MSG( !Is3rdStateAllowedForUser(),
180 wxT("Using wxCH_ALLOW_3RD_STATE_FOR_USER")
181 wxT(" style flag for a 2-state checkbox is useless") );
182 msStyle
|= BS_CHECKBOX
;
185 if ( style
& wxALIGN_RIGHT
)
187 msStyle
|= BS_LEFTTEXT
| BS_RIGHT
;
190 return MSWCreateControl(wxT("BUTTON"), msStyle
, pos
, size
, label
, 0);
193 // ----------------------------------------------------------------------------
194 // wxCheckBox geometry
195 // ----------------------------------------------------------------------------
197 wxSize
wxCheckBox::DoGetBestSize() const
199 static int s_checkSize
= 0;
204 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
206 s_checkSize
= dc
.GetCharHeight();
209 wxString str
= wxGetWindowText(GetHWND());
211 int wCheckbox
, hCheckbox
;
214 GetTextExtent(GetLabelText(str
), &wCheckbox
, &hCheckbox
);
215 wCheckbox
+= s_checkSize
+ GetCharWidth();
217 if ( hCheckbox
< s_checkSize
)
218 hCheckbox
= s_checkSize
;
222 wCheckbox
= s_checkSize
;
223 hCheckbox
= s_checkSize
;
229 wxSize
best(wCheckbox
, hCheckbox
);
234 // ----------------------------------------------------------------------------
235 // wxCheckBox operations
236 // ----------------------------------------------------------------------------
238 void wxCheckBox::SetValue(bool val
)
240 Set3StateValue(val
? wxCHK_CHECKED
: wxCHK_UNCHECKED
);
243 bool wxCheckBox::GetValue() const
245 return Get3StateValue() != wxCHK_UNCHECKED
;
248 void wxCheckBox::Command(wxCommandEvent
& event
)
250 int state
= event
.GetInt();
251 wxCHECK_RET( (state
== wxCHK_UNCHECKED
) || (state
== wxCHK_CHECKED
)
252 || (state
== wxCHK_UNDETERMINED
),
253 wxT("event.GetInt() returned an invalid checkbox state") );
255 Set3StateValue((wxCheckBoxState
) state
);
256 ProcessCommand(event
);
259 wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED
== BST_UNCHECKED
260 && wxCHK_CHECKED
== BST_CHECKED
261 && wxCHK_UNDETERMINED
== BST_INDETERMINATE
, EnumValuesIncorrect
);
263 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
266 if ( !IsOwnerDrawn() )
267 ::SendMessage(GetHwnd(), BM_SETCHECK
, (WPARAM
) state
, 0);
268 else // owner drawn buttons don't react to this message
272 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
277 bool wxCheckBox::MSWCommand(WXUINT cmd
, WXWORD
WXUNUSED(id
))
279 if ( cmd
!= BN_CLICKED
&& cmd
!= BN_DBLCLK
)
282 // first update the value so that user event handler gets the new checkbox
285 // ownerdrawn buttons don't manage their state themselves unlike usual
286 // auto checkboxes so do it ourselves in any case
287 wxCheckBoxState state
;
288 if ( Is3rdStateAllowedForUser() )
290 state
= (wxCheckBoxState
)((m_state
+ 1) % 3);
292 else // 2 state checkbox (at least from users point of view)
294 // note that wxCHK_UNDETERMINED also becomes unchecked when clicked
295 state
= m_state
== wxCHK_UNCHECKED
? wxCHK_CHECKED
: wxCHK_UNCHECKED
;
298 DoSet3StateValue(state
);
301 // generate the event
302 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, m_windowId
);
305 event
.SetEventObject(this);
306 ProcessCommand(event
);
311 // ----------------------------------------------------------------------------
312 // owner drawn checkboxes stuff
313 // ----------------------------------------------------------------------------
315 bool wxCheckBox::SetForegroundColour(const wxColour
& colour
)
317 if ( !wxCheckBoxBase::SetForegroundColour(colour
) )
320 // the only way to change the checkbox foreground colour under Windows XP
321 // is to owner draw it
322 if ( wxUxThemeEngine::GetIfActive() )
323 MakeOwnerDrawn(colour
.Ok());
328 bool wxCheckBox::IsOwnerDrawn() const
331 (::GetWindowLong(GetHwnd(), GWL_STYLE
) & BS_OWNERDRAW
) == BS_OWNERDRAW
;
334 void wxCheckBox::MakeOwnerDrawn(bool ownerDrawn
)
336 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
338 // note that BS_CHECKBOX & BS_OWNERDRAW != 0 so we can't operate on
339 // them as on independent style bits
342 style
&= ~(BS_CHECKBOX
| BS_3STATE
);
343 style
|= BS_OWNERDRAW
;
345 Connect(wxEVT_ENTER_WINDOW
,
346 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
347 Connect(wxEVT_LEAVE_WINDOW
,
348 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
349 Connect(wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
350 Connect(wxEVT_LEFT_UP
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
351 Connect(wxEVT_SET_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
352 Connect(wxEVT_KILL_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
354 else // reset to default colour
356 style
&= ~BS_OWNERDRAW
;
357 style
|= HasFlag(wxCHK_3STATE
) ? BS_3STATE
: BS_CHECKBOX
;
359 Disconnect(wxEVT_ENTER_WINDOW
,
360 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
361 Disconnect(wxEVT_LEAVE_WINDOW
,
362 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
363 Disconnect(wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
364 Disconnect(wxEVT_LEFT_UP
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
365 Disconnect(wxEVT_SET_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
366 Disconnect(wxEVT_KILL_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
369 ::SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
373 // ensure that controls state is consistent with internal state
374 DoSet3StateValue(m_state
);
378 void wxCheckBox::OnMouseEnterOrLeave(wxMouseEvent
& event
)
380 m_isHot
= event
.GetEventType() == wxEVT_ENTER_WINDOW
;
389 void wxCheckBox::OnMouseLeft(wxMouseEvent
& event
)
391 // TODO: we should capture the mouse here to be notified about left up
392 // event but this interferes with BN_CLICKED generation so if we
393 // want to do this we'd need to generate them ourselves
394 m_isPressed
= event
.GetEventType() == wxEVT_LEFT_DOWN
;
400 void wxCheckBox::OnFocus(wxFocusEvent
& event
)
407 bool wxCheckBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
409 DRAWITEMSTRUCT
*dis
= (DRAWITEMSTRUCT
*)item
;
411 if ( !IsOwnerDrawn() || dis
->CtlType
!= ODT_BUTTON
)
412 return wxCheckBoxBase::MSWOnDraw(item
);
414 // calculate the rectangles for the check mark itself and the label
416 RECT
& rect
= dis
->rcItem
;
420 rectLabel
.top
= rect
.top
;
422 rectLabel
.bottom
= rect
.bottom
;
423 const int checkSize
= GetBestSize().y
;
424 const int MARGIN
= 3;
426 const bool isRightAligned
= HasFlag(wxALIGN_RIGHT
);
427 if ( isRightAligned
)
429 rectCheck
.right
= rect
.right
;
430 rectCheck
.left
= rectCheck
.right
- checkSize
;
432 rectLabel
.right
= rectCheck
.left
- MARGIN
;
433 rectLabel
.left
= rect
.left
;
435 else // normal, left-aligned checkbox
437 rectCheck
.left
= rect
.left
;
438 rectCheck
.right
= rectCheck
.left
+ checkSize
;
440 rectLabel
.left
= rectCheck
.right
+ MARGIN
;
441 rectLabel
.right
= rect
.right
;
444 // show we draw a focus rect?
445 const bool isFocused
= m_isPressed
|| FindFocus() == this;
448 // draw the checkbox itself
453 flags
|= wxCONTROL_DISABLED
;
454 switch ( Get3StateValue() )
457 flags
|= wxCONTROL_CHECKED
;
460 case wxCHK_UNDETERMINED
:
461 flags
|= wxCONTROL_PRESSED
;
465 wxFAIL_MSG( _T("unexpected Get3StateValue() return value") );
468 case wxCHK_UNCHECKED
:
469 // no extra styles needed
473 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
474 flags
|= wxCONTROL_CURRENT
;
476 wxRendererNative::Get().
477 DrawCheckBox(this, dc
, wxRectFromRECT(rectCheck
), flags
);
480 const wxString
& label
= GetLabel();
482 // first we need to measure it
483 UINT fmt
= DT_NOCLIP
;
485 // drawing underlying doesn't look well with focus rect (and the native
486 // control doesn't do it)
488 fmt
|= DT_HIDEPREFIX
;
489 if ( isRightAligned
)
491 // TODO: also use DT_HIDEPREFIX if the system is configured so
493 // we need to get the label real size first if we have to draw a focus rect
497 if ( !::DrawText(hdc
, label
, label
.length(), &rectLabel
,
500 wxLogLastError(_T("DrawText(DT_CALCRECT)"));
506 ::SetTextColor(hdc
, ::GetSysColor(COLOR_GRAYTEXT
));
509 if ( !::DrawText(hdc
, label
, label
.length(), &rectLabel
, fmt
) )
511 wxLogLastError(_T("DrawText()"));
514 // finally draw the focus
519 if ( !::DrawFocusRect(hdc
, &rectLabel
) )
521 wxLogLastError(_T("DrawFocusRect()"));
528 #endif // wxUSE_CHECKBOX