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 ( ::GetWindowLong(GetHwnd(), GWL_STYLE
) & BS_MULTILINE
)
152 // We need to make the checkbox even wider in this case because
153 // otherwise it wraps lines automatically and not only on "\n"s as
154 // we need and this makes the size computed here wrong resulting in
155 // checkbox contents being truncated when it's actually displayed.
156 // Without this hack simple checkbox with "Some thing\n and more"
157 // label appears on 3 lines, not 2, under Windows 2003 using
158 // classic look and feel (although it works fine under Windows 7,
159 // with or without themes).
160 wCheckbox
+= s_checkSize
;
163 if ( hCheckbox
< s_checkSize
)
164 hCheckbox
= s_checkSize
;
168 wCheckbox
= s_checkSize
;
169 hCheckbox
= s_checkSize
;
175 wxSize
best(wCheckbox
, hCheckbox
);
180 // ----------------------------------------------------------------------------
181 // wxCheckBox operations
182 // ----------------------------------------------------------------------------
184 void wxCheckBox::SetLabel(const wxString
& label
)
186 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label
);
188 wxCheckBoxBase::SetLabel(label
);
191 void wxCheckBox::SetValue(bool val
)
193 Set3StateValue(val
? wxCHK_CHECKED
: wxCHK_UNCHECKED
);
196 bool wxCheckBox::GetValue() const
198 return Get3StateValue() != wxCHK_UNCHECKED
;
201 void wxCheckBox::Command(wxCommandEvent
& event
)
203 int state
= event
.GetInt();
204 wxCHECK_RET( (state
== wxCHK_UNCHECKED
) || (state
== wxCHK_CHECKED
)
205 || (state
== wxCHK_UNDETERMINED
),
206 wxT("event.GetInt() returned an invalid checkbox state") );
208 Set3StateValue((wxCheckBoxState
) state
);
209 ProcessCommand(event
);
212 wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED
== BST_UNCHECKED
213 && wxCHK_CHECKED
== BST_CHECKED
214 && wxCHK_UNDETERMINED
== BST_INDETERMINATE
, EnumValuesIncorrect
);
216 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
219 if ( !IsOwnerDrawn() )
220 ::SendMessage(GetHwnd(), BM_SETCHECK
, (WPARAM
) state
, 0);
221 else // owner drawn buttons don't react to this message
225 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
230 bool wxCheckBox::MSWCommand(WXUINT cmd
, WXWORD
WXUNUSED(id
))
232 if ( cmd
!= BN_CLICKED
&& cmd
!= BN_DBLCLK
)
235 // first update the value so that user event handler gets the new checkbox
238 // ownerdrawn buttons don't manage their state themselves unlike usual
239 // auto checkboxes so do it ourselves in any case
240 wxCheckBoxState state
;
241 if ( Is3rdStateAllowedForUser() )
243 state
= (wxCheckBoxState
)((m_state
+ 1) % 3);
245 else // 2 state checkbox (at least from users point of view)
247 // note that wxCHK_UNDETERMINED also becomes unchecked when clicked
248 state
= m_state
== wxCHK_UNCHECKED
? wxCHK_CHECKED
: wxCHK_UNCHECKED
;
251 DoSet3StateValue(state
);
254 // generate the event
255 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, m_windowId
);
258 event
.SetEventObject(this);
259 ProcessCommand(event
);
264 // ----------------------------------------------------------------------------
265 // owner drawn checkboxes stuff
266 // ----------------------------------------------------------------------------
268 bool wxCheckBox::SetForegroundColour(const wxColour
& colour
)
270 if ( !wxCheckBoxBase::SetForegroundColour(colour
) )
273 // the only way to change the checkbox foreground colour under Windows XP
274 // is to owner draw it
275 if ( wxUxThemeEngine::GetIfActive() )
276 MakeOwnerDrawn(colour
.IsOk());
281 bool wxCheckBox::IsOwnerDrawn() const
284 (::GetWindowLong(GetHwnd(), GWL_STYLE
) & BS_OWNERDRAW
) == BS_OWNERDRAW
;
287 void wxCheckBox::MakeOwnerDrawn(bool ownerDrawn
)
289 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
291 // note that BS_CHECKBOX & BS_OWNERDRAW != 0 so we can't operate on
292 // them as on independent style bits
295 style
&= ~(BS_CHECKBOX
| BS_3STATE
);
296 style
|= BS_OWNERDRAW
;
298 Connect(wxEVT_ENTER_WINDOW
,
299 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
300 Connect(wxEVT_LEAVE_WINDOW
,
301 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
302 Connect(wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
303 Connect(wxEVT_LEFT_UP
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
304 Connect(wxEVT_SET_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
305 Connect(wxEVT_KILL_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
307 else // reset to default colour
309 style
&= ~BS_OWNERDRAW
;
310 style
|= HasFlag(wxCHK_3STATE
) ? BS_3STATE
: BS_CHECKBOX
;
312 Disconnect(wxEVT_ENTER_WINDOW
,
313 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
314 Disconnect(wxEVT_LEAVE_WINDOW
,
315 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
316 Disconnect(wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
317 Disconnect(wxEVT_LEFT_UP
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
318 Disconnect(wxEVT_SET_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
319 Disconnect(wxEVT_KILL_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
322 ::SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
326 // ensure that controls state is consistent with internal state
327 DoSet3StateValue(m_state
);
331 void wxCheckBox::OnMouseEnterOrLeave(wxMouseEvent
& event
)
333 m_isHot
= event
.GetEventType() == wxEVT_ENTER_WINDOW
;
342 void wxCheckBox::OnMouseLeft(wxMouseEvent
& event
)
344 // TODO: we should capture the mouse here to be notified about left up
345 // event but this interferes with BN_CLICKED generation so if we
346 // want to do this we'd need to generate them ourselves
347 m_isPressed
= event
.GetEventType() == wxEVT_LEFT_DOWN
;
353 void wxCheckBox::OnFocus(wxFocusEvent
& event
)
360 bool wxCheckBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
362 DRAWITEMSTRUCT
*dis
= (DRAWITEMSTRUCT
*)item
;
364 if ( !IsOwnerDrawn() || dis
->CtlType
!= ODT_BUTTON
)
365 return wxCheckBoxBase::MSWOnDraw(item
);
367 // calculate the rectangles for the check mark itself and the label
369 RECT
& rect
= dis
->rcItem
;
373 rectLabel
.top
= rect
.top
;
375 rectLabel
.bottom
= rect
.bottom
;
376 const int checkSize
= GetBestSize().y
;
377 const int MARGIN
= 3;
379 const bool isRightAligned
= HasFlag(wxALIGN_RIGHT
);
380 if ( isRightAligned
)
382 rectCheck
.right
= rect
.right
;
383 rectCheck
.left
= rectCheck
.right
- checkSize
;
385 rectLabel
.right
= rectCheck
.left
- MARGIN
;
386 rectLabel
.left
= rect
.left
;
388 else // normal, left-aligned checkbox
390 rectCheck
.left
= rect
.left
;
391 rectCheck
.right
= rectCheck
.left
+ checkSize
;
393 rectLabel
.left
= rectCheck
.right
+ MARGIN
;
394 rectLabel
.right
= rect
.right
;
397 // show we draw a focus rect?
398 const bool isFocused
= m_isPressed
|| FindFocus() == this;
401 // draw the checkbox itself
406 flags
|= wxCONTROL_DISABLED
;
407 switch ( Get3StateValue() )
410 flags
|= wxCONTROL_CHECKED
;
413 case wxCHK_UNDETERMINED
:
414 flags
|= wxCONTROL_PRESSED
;
418 wxFAIL_MSG( wxT("unexpected Get3StateValue() return value") );
421 case wxCHK_UNCHECKED
:
422 // no extra styles needed
426 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
427 flags
|= wxCONTROL_CURRENT
;
429 wxRendererNative::Get().
430 DrawCheckBox(this, dc
, wxRectFromRECT(rectCheck
), flags
);
433 const wxString
& label
= GetLabel();
435 // first we need to measure it
436 UINT fmt
= DT_NOCLIP
;
438 // drawing underlying doesn't look well with focus rect (and the native
439 // control doesn't do it)
441 fmt
|= DT_HIDEPREFIX
;
442 if ( isRightAligned
)
444 // TODO: also use DT_HIDEPREFIX if the system is configured so
446 // we need to get the label real size first if we have to draw a focus rect
450 if ( !::DrawText(hdc
, label
.wx_str(), label
.length(), &rectLabel
,
453 wxLogLastError(wxT("DrawText(DT_CALCRECT)"));
459 ::SetTextColor(hdc
, ::GetSysColor(COLOR_GRAYTEXT
));
462 if ( !::DrawText(hdc
, label
.wx_str(), label
.length(), &rectLabel
, fmt
) )
464 wxLogLastError(wxT("DrawText()"));
467 // finally draw the focus
472 if ( !::DrawFocusRect(hdc
, &rectLabel
) )
474 wxLogLastError(wxT("DrawFocusRect()"));
481 #endif // wxUSE_CHECKBOX