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 // ----------------------------------------------------------------------------
83 // wxCheckBox creation
84 // ----------------------------------------------------------------------------
86 void wxCheckBox::Init()
88 m_state
= wxCHK_UNCHECKED
;
93 bool wxCheckBox::Create(wxWindow
*parent
,
95 const wxString
& label
,
97 const wxSize
& size
, long style
,
98 const wxValidator
& validator
,
103 WXValidateStyle(&style
);
104 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
107 long msStyle
= WS_TABSTOP
;
109 if ( style
& wxCHK_3STATE
)
110 msStyle
|= BS_3STATE
;
112 msStyle
|= BS_CHECKBOX
;
114 if ( style
& wxALIGN_RIGHT
)
116 msStyle
|= BS_LEFTTEXT
| BS_RIGHT
;
119 msStyle
|= wxMSWButton::GetMultilineStyle(label
);
121 return MSWCreateControl(wxT("BUTTON"), msStyle
, pos
, size
, label
, 0);
124 // ----------------------------------------------------------------------------
125 // wxCheckBox geometry
126 // ----------------------------------------------------------------------------
128 wxSize
wxCheckBox::DoGetBestSize() const
130 static int s_checkSize
= 0;
135 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
137 s_checkSize
= dc
.GetCharHeight();
140 wxString str
= wxGetWindowText(GetHWND());
142 int wCheckbox
, hCheckbox
;
145 wxClientDC
dc(const_cast<wxCheckBox
*>(this));
146 dc
.SetFont(GetFont());
147 dc
.GetMultiLineTextExtent(GetLabelText(str
), &wCheckbox
, &hCheckbox
);
148 wCheckbox
+= s_checkSize
+ GetCharWidth();
150 if ( hCheckbox
< s_checkSize
)
151 hCheckbox
= s_checkSize
;
155 wCheckbox
= s_checkSize
;
156 hCheckbox
= s_checkSize
;
162 wxSize
best(wCheckbox
, hCheckbox
);
167 // ----------------------------------------------------------------------------
168 // wxCheckBox operations
169 // ----------------------------------------------------------------------------
171 void wxCheckBox::SetLabel(const wxString
& label
)
173 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label
);
175 wxCheckBoxBase::SetLabel(label
);
178 void wxCheckBox::SetValue(bool val
)
180 Set3StateValue(val
? wxCHK_CHECKED
: wxCHK_UNCHECKED
);
183 bool wxCheckBox::GetValue() const
185 return Get3StateValue() != wxCHK_UNCHECKED
;
188 void wxCheckBox::Command(wxCommandEvent
& event
)
190 int state
= event
.GetInt();
191 wxCHECK_RET( (state
== wxCHK_UNCHECKED
) || (state
== wxCHK_CHECKED
)
192 || (state
== wxCHK_UNDETERMINED
),
193 wxT("event.GetInt() returned an invalid checkbox state") );
195 Set3StateValue((wxCheckBoxState
) state
);
196 ProcessCommand(event
);
199 wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED
== BST_UNCHECKED
200 && wxCHK_CHECKED
== BST_CHECKED
201 && wxCHK_UNDETERMINED
== BST_INDETERMINATE
, EnumValuesIncorrect
);
203 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
206 if ( !IsOwnerDrawn() )
207 ::SendMessage(GetHwnd(), BM_SETCHECK
, (WPARAM
) state
, 0);
208 else // owner drawn buttons don't react to this message
212 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
217 bool wxCheckBox::MSWCommand(WXUINT cmd
, WXWORD
WXUNUSED(id
))
219 if ( cmd
!= BN_CLICKED
&& cmd
!= BN_DBLCLK
)
222 // first update the value so that user event handler gets the new checkbox
225 // ownerdrawn buttons don't manage their state themselves unlike usual
226 // auto checkboxes so do it ourselves in any case
227 wxCheckBoxState state
;
228 if ( Is3rdStateAllowedForUser() )
230 state
= (wxCheckBoxState
)((m_state
+ 1) % 3);
232 else // 2 state checkbox (at least from users point of view)
234 // note that wxCHK_UNDETERMINED also becomes unchecked when clicked
235 state
= m_state
== wxCHK_UNCHECKED
? wxCHK_CHECKED
: wxCHK_UNCHECKED
;
238 DoSet3StateValue(state
);
241 // generate the event
242 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, m_windowId
);
245 event
.SetEventObject(this);
246 ProcessCommand(event
);
251 // ----------------------------------------------------------------------------
252 // owner drawn checkboxes stuff
253 // ----------------------------------------------------------------------------
255 bool wxCheckBox::SetForegroundColour(const wxColour
& colour
)
257 if ( !wxCheckBoxBase::SetForegroundColour(colour
) )
260 // the only way to change the checkbox foreground colour under Windows XP
261 // is to owner draw it
262 if ( wxUxThemeEngine::GetIfActive() )
263 MakeOwnerDrawn(colour
.Ok());
268 bool wxCheckBox::IsOwnerDrawn() const
271 (::GetWindowLong(GetHwnd(), GWL_STYLE
) & BS_OWNERDRAW
) == BS_OWNERDRAW
;
274 void wxCheckBox::MakeOwnerDrawn(bool ownerDrawn
)
276 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
278 // note that BS_CHECKBOX & BS_OWNERDRAW != 0 so we can't operate on
279 // them as on independent style bits
282 style
&= ~(BS_CHECKBOX
| BS_3STATE
);
283 style
|= BS_OWNERDRAW
;
285 Connect(wxEVT_ENTER_WINDOW
,
286 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
287 Connect(wxEVT_LEAVE_WINDOW
,
288 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
289 Connect(wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
290 Connect(wxEVT_LEFT_UP
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
291 Connect(wxEVT_SET_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
292 Connect(wxEVT_KILL_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
294 else // reset to default colour
296 style
&= ~BS_OWNERDRAW
;
297 style
|= HasFlag(wxCHK_3STATE
) ? BS_3STATE
: BS_CHECKBOX
;
299 Disconnect(wxEVT_ENTER_WINDOW
,
300 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
301 Disconnect(wxEVT_LEAVE_WINDOW
,
302 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
303 Disconnect(wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
304 Disconnect(wxEVT_LEFT_UP
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
305 Disconnect(wxEVT_SET_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
306 Disconnect(wxEVT_KILL_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
309 ::SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
313 // ensure that controls state is consistent with internal state
314 DoSet3StateValue(m_state
);
318 void wxCheckBox::OnMouseEnterOrLeave(wxMouseEvent
& event
)
320 m_isHot
= event
.GetEventType() == wxEVT_ENTER_WINDOW
;
329 void wxCheckBox::OnMouseLeft(wxMouseEvent
& event
)
331 // TODO: we should capture the mouse here to be notified about left up
332 // event but this interferes with BN_CLICKED generation so if we
333 // want to do this we'd need to generate them ourselves
334 m_isPressed
= event
.GetEventType() == wxEVT_LEFT_DOWN
;
340 void wxCheckBox::OnFocus(wxFocusEvent
& event
)
347 bool wxCheckBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
349 DRAWITEMSTRUCT
*dis
= (DRAWITEMSTRUCT
*)item
;
351 if ( !IsOwnerDrawn() || dis
->CtlType
!= ODT_BUTTON
)
352 return wxCheckBoxBase::MSWOnDraw(item
);
354 // calculate the rectangles for the check mark itself and the label
356 RECT
& rect
= dis
->rcItem
;
360 rectLabel
.top
= rect
.top
;
362 rectLabel
.bottom
= rect
.bottom
;
363 const int checkSize
= GetBestSize().y
;
364 const int MARGIN
= 3;
366 const bool isRightAligned
= HasFlag(wxALIGN_RIGHT
);
367 if ( isRightAligned
)
369 rectCheck
.right
= rect
.right
;
370 rectCheck
.left
= rectCheck
.right
- checkSize
;
372 rectLabel
.right
= rectCheck
.left
- MARGIN
;
373 rectLabel
.left
= rect
.left
;
375 else // normal, left-aligned checkbox
377 rectCheck
.left
= rect
.left
;
378 rectCheck
.right
= rectCheck
.left
+ checkSize
;
380 rectLabel
.left
= rectCheck
.right
+ MARGIN
;
381 rectLabel
.right
= rect
.right
;
384 // show we draw a focus rect?
385 const bool isFocused
= m_isPressed
|| FindFocus() == this;
388 // draw the checkbox itself
393 flags
|= wxCONTROL_DISABLED
;
394 switch ( Get3StateValue() )
397 flags
|= wxCONTROL_CHECKED
;
400 case wxCHK_UNDETERMINED
:
401 flags
|= wxCONTROL_PRESSED
;
405 wxFAIL_MSG( wxT("unexpected Get3StateValue() return value") );
408 case wxCHK_UNCHECKED
:
409 // no extra styles needed
413 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
414 flags
|= wxCONTROL_CURRENT
;
416 wxRendererNative::Get().
417 DrawCheckBox(this, dc
, wxRectFromRECT(rectCheck
), flags
);
420 const wxString
& label
= GetLabel();
422 // first we need to measure it
423 UINT fmt
= DT_NOCLIP
;
425 // drawing underlying doesn't look well with focus rect (and the native
426 // control doesn't do it)
428 fmt
|= DT_HIDEPREFIX
;
429 if ( isRightAligned
)
431 // TODO: also use DT_HIDEPREFIX if the system is configured so
433 // we need to get the label real size first if we have to draw a focus rect
437 if ( !::DrawText(hdc
, label
.wx_str(), label
.length(), &rectLabel
,
440 wxLogLastError(wxT("DrawText(DT_CALCRECT)"));
446 ::SetTextColor(hdc
, ::GetSysColor(COLOR_GRAYTEXT
));
449 if ( !::DrawText(hdc
, label
.wx_str(), label
.length(), &rectLabel
, fmt
) )
451 wxLogLastError(wxT("DrawText()"));
454 // finally draw the focus
459 if ( !::DrawFocusRect(hdc
, &rectLabel
) )
461 wxLogLastError(wxT("DrawFocusRect()"));
468 #endif // wxUSE_CHECKBOX