]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/checkbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/checkbox.cpp
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/checkbox.h"
32 #include "wx/dcclient.h"
33 #include "wx/dcscreen.h"
34 #include "wx/settings.h"
37 #include "wx/msw/dc.h" // for wxDCTemp
38 #include "wx/renderer.h"
39 #include "wx/msw/uxtheme.h"
40 #include "wx/msw/private/button.h"
41 #include "wx/msw/missing.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
51 // these values are defined in tmschema.h (except the first one)
58 CBS_UNCHECKEDDISABLED
,
73 CBS_PRESSED_OFFSET
= 2,
74 CBS_DISABLED_OFFSET
= 3
77 // ============================================================================
79 // ============================================================================
81 // ----------------------------------------------------------------------------
82 // wxCheckBox creation
83 // ----------------------------------------------------------------------------
85 void wxCheckBox::Init()
87 m_state
= wxCHK_UNCHECKED
;
92 bool wxCheckBox::Create(wxWindow
*parent
,
94 const wxString
& label
,
96 const wxSize
& size
, long style
,
97 const wxValidator
& validator
,
102 WXValidateStyle(&style
);
103 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
107 WXDWORD msStyle
= MSWGetStyle(style
, &exstyle
);
109 msStyle
|= wxMSWButton::GetMultilineStyle(label
);
111 return MSWCreateControl(wxT("BUTTON"), msStyle
, pos
, size
, label
, exstyle
);
114 WXDWORD
wxCheckBox::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
116 // buttons never have an external border, they draw their own one
117 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
119 if ( style
& wxCHK_3STATE
)
120 msStyle
|= BS_3STATE
;
122 msStyle
|= BS_CHECKBOX
;
124 if ( style
& wxALIGN_RIGHT
)
126 msStyle
|= BS_LEFTTEXT
| BS_RIGHT
;
132 // ----------------------------------------------------------------------------
133 // wxCheckBox geometry
134 // ----------------------------------------------------------------------------
136 wxSize
wxCheckBox::DoGetBestClientSize() const
138 static int s_checkSize
= 0;
143 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
145 s_checkSize
= dc
.GetCharHeight();
148 wxString str
= wxGetWindowText(GetHWND());
150 int wCheckbox
, hCheckbox
;
153 wxClientDC
dc(const_cast<wxCheckBox
*>(this));
154 dc
.SetFont(GetFont());
155 dc
.GetMultiLineTextExtent(GetLabelText(str
), &wCheckbox
, &hCheckbox
);
156 wCheckbox
+= s_checkSize
+ GetCharWidth();
158 if ( ::GetWindowLong(GetHwnd(), GWL_STYLE
) & BS_MULTILINE
)
160 // We need to make the checkbox even wider in this case because
161 // otherwise it wraps lines automatically and not only on "\n"s as
162 // we need and this makes the size computed here wrong resulting in
163 // checkbox contents being truncated when it's actually displayed.
164 // Without this hack simple checkbox with "Some thing\n and more"
165 // label appears on 3 lines, not 2, under Windows 2003 using
166 // classic look and feel (although it works fine under Windows 7,
167 // with or without themes).
168 wCheckbox
+= s_checkSize
;
171 if ( hCheckbox
< s_checkSize
)
172 hCheckbox
= s_checkSize
;
176 wCheckbox
= s_checkSize
;
177 hCheckbox
= s_checkSize
;
183 wxSize
best(wCheckbox
, hCheckbox
);
188 // ----------------------------------------------------------------------------
189 // wxCheckBox operations
190 // ----------------------------------------------------------------------------
192 void wxCheckBox::SetLabel(const wxString
& label
)
194 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label
);
196 wxCheckBoxBase::SetLabel(label
);
199 void wxCheckBox::SetValue(bool val
)
201 Set3StateValue(val
? wxCHK_CHECKED
: wxCHK_UNCHECKED
);
204 bool wxCheckBox::GetValue() const
206 return Get3StateValue() != wxCHK_UNCHECKED
;
209 void wxCheckBox::Command(wxCommandEvent
& event
)
211 int state
= event
.GetInt();
212 wxCHECK_RET( (state
== wxCHK_UNCHECKED
) || (state
== wxCHK_CHECKED
)
213 || (state
== wxCHK_UNDETERMINED
),
214 wxT("event.GetInt() returned an invalid checkbox state") );
216 Set3StateValue((wxCheckBoxState
) state
);
217 ProcessCommand(event
);
220 wxCOMPILE_TIME_ASSERT(wxCHK_UNCHECKED
== BST_UNCHECKED
221 && wxCHK_CHECKED
== BST_CHECKED
222 && wxCHK_UNDETERMINED
== BST_INDETERMINATE
, EnumValuesIncorrect
);
224 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
227 if ( !IsOwnerDrawn() )
228 ::SendMessage(GetHwnd(), BM_SETCHECK
, (WPARAM
) state
, 0);
229 else // owner drawn buttons don't react to this message
233 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
238 bool wxCheckBox::MSWCommand(WXUINT cmd
, WXWORD
WXUNUSED(id
))
240 if ( cmd
!= BN_CLICKED
&& cmd
!= BN_DBLCLK
)
243 // first update the value so that user event handler gets the new checkbox
246 // ownerdrawn buttons don't manage their state themselves unlike usual
247 // auto checkboxes so do it ourselves in any case
248 wxCheckBoxState state
;
249 if ( Is3rdStateAllowedForUser() )
251 state
= (wxCheckBoxState
)((m_state
+ 1) % 3);
253 else // 2 state checkbox (at least from users point of view)
255 // note that wxCHK_UNDETERMINED also becomes unchecked when clicked
256 state
= m_state
== wxCHK_UNCHECKED
? wxCHK_CHECKED
: wxCHK_UNCHECKED
;
259 DoSet3StateValue(state
);
262 // generate the event
263 wxCommandEvent
event(wxEVT_CHECKBOX
, m_windowId
);
266 event
.SetEventObject(this);
267 ProcessCommand(event
);
272 // ----------------------------------------------------------------------------
273 // owner drawn checkboxes stuff
274 // ----------------------------------------------------------------------------
276 bool wxCheckBox::SetForegroundColour(const wxColour
& colour
)
278 if ( !wxCheckBoxBase::SetForegroundColour(colour
) )
281 // the only way to change the checkbox foreground colour under Windows XP
282 // is to owner draw it
283 if ( wxUxThemeEngine::GetIfActive() )
284 MSWMakeOwnerDrawn(colour
.IsOk());
289 bool wxCheckBox::IsOwnerDrawn() const
292 (::GetWindowLong(GetHwnd(), GWL_STYLE
) & BS_OWNERDRAW
) == BS_OWNERDRAW
;
295 void wxCheckBox::MSWMakeOwnerDrawn(bool ownerDrawn
)
297 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
299 // note that BS_CHECKBOX & BS_OWNERDRAW != 0 so we can't operate on
300 // them as on independent style bits
303 style
&= ~(BS_CHECKBOX
| BS_3STATE
);
304 style
|= BS_OWNERDRAW
;
306 Connect(wxEVT_ENTER_WINDOW
,
307 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
308 Connect(wxEVT_LEAVE_WINDOW
,
309 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
310 Connect(wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
311 Connect(wxEVT_LEFT_UP
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
312 Connect(wxEVT_SET_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
313 Connect(wxEVT_KILL_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
315 else // reset to default colour
317 style
&= ~BS_OWNERDRAW
;
318 style
|= HasFlag(wxCHK_3STATE
) ? BS_3STATE
: BS_CHECKBOX
;
320 Disconnect(wxEVT_ENTER_WINDOW
,
321 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
322 Disconnect(wxEVT_LEAVE_WINDOW
,
323 wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave
));
324 Disconnect(wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
325 Disconnect(wxEVT_LEFT_UP
, wxMouseEventHandler(wxCheckBox::OnMouseLeft
));
326 Disconnect(wxEVT_SET_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
327 Disconnect(wxEVT_KILL_FOCUS
, wxFocusEventHandler(wxCheckBox::OnFocus
));
330 ::SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
334 // ensure that controls state is consistent with internal state
335 DoSet3StateValue(m_state
);
339 void wxCheckBox::OnMouseEnterOrLeave(wxMouseEvent
& event
)
341 m_isHot
= event
.GetEventType() == wxEVT_ENTER_WINDOW
;
350 void wxCheckBox::OnMouseLeft(wxMouseEvent
& event
)
352 // TODO: we should capture the mouse here to be notified about left up
353 // event but this interferes with BN_CLICKED generation so if we
354 // want to do this we'd need to generate them ourselves
355 m_isPressed
= event
.GetEventType() == wxEVT_LEFT_DOWN
;
361 void wxCheckBox::OnFocus(wxFocusEvent
& event
)
368 bool wxCheckBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
370 DRAWITEMSTRUCT
*dis
= (DRAWITEMSTRUCT
*)item
;
372 if ( !IsOwnerDrawn() || dis
->CtlType
!= ODT_BUTTON
)
373 return wxCheckBoxBase::MSWOnDraw(item
);
375 // calculate the rectangles for the check mark itself and the label
377 RECT
& rect
= dis
->rcItem
;
380 rectLabel
.top
= rect
.top
+ (rect
.bottom
- rect
.top
- GetBestSize().y
) / 2;
381 rectLabel
.bottom
= rectLabel
.top
+ GetBestSize().y
;
382 const int MARGIN
= 3;
383 const int CXMENUCHECK
= ::GetSystemMetrics(SM_CXMENUCHECK
);
384 // the space between the checkbox and the label is included in the
386 const int checkSize
= wxMin(CXMENUCHECK
- MARGIN
, GetSize().y
);
387 rectCheck
.top
= rect
.top
+ (rect
.bottom
- rect
.top
- checkSize
) / 2;
388 rectCheck
.bottom
= rectCheck
.top
+ checkSize
;
390 const bool isRightAligned
= HasFlag(wxALIGN_RIGHT
);
391 if ( isRightAligned
)
393 rectLabel
.right
= rect
.right
- CXMENUCHECK
;
394 rectLabel
.left
= rect
.left
;
396 rectCheck
.left
= rectLabel
.right
+ ( CXMENUCHECK
+ MARGIN
- checkSize
) / 2;
397 rectCheck
.right
= rectCheck
.left
+ checkSize
;
399 else // normal, left-aligned checkbox
401 rectCheck
.left
= rect
.left
+ ( CXMENUCHECK
- MARGIN
- checkSize
) / 2;
402 rectCheck
.right
= rectCheck
.left
+ checkSize
;
404 rectLabel
.left
= rect
.left
+ CXMENUCHECK
;
405 rectLabel
.right
= rect
.right
;
408 // shall we draw a focus rect?
409 const bool isFocused
= m_isPressed
|| FindFocus() == this;
412 // draw the checkbox itself
417 flags
|= wxCONTROL_DISABLED
;
418 switch ( Get3StateValue() )
421 flags
|= wxCONTROL_CHECKED
;
424 case wxCHK_UNDETERMINED
:
425 flags
|= wxCONTROL_PRESSED
;
429 wxFAIL_MSG( wxT("unexpected Get3StateValue() return value") );
432 case wxCHK_UNCHECKED
:
433 // no extra styles needed
437 if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
438 flags
|= wxCONTROL_CURRENT
;
440 wxRendererNative::Get().
441 DrawCheckBox(this, dc
, wxRectFromRECT(rectCheck
), flags
);
444 const wxString
& label
= GetLabel();
446 // first we need to measure it
447 UINT fmt
= DT_NOCLIP
;
449 // drawing underlying doesn't look well with focus rect (and the native
450 // control doesn't do it)
452 fmt
|= DT_HIDEPREFIX
;
453 if ( isRightAligned
)
455 // TODO: also use DT_HIDEPREFIX if the system is configured so
457 // we need to get the label real size first if we have to draw a focus rect
461 RECT oldLabelRect
= rectLabel
; // needed if right aligned
463 if ( !::DrawText(hdc
, label
.t_str(), label
.length(), &rectLabel
,
466 wxLogLastError(wxT("DrawText(DT_CALCRECT)"));
469 if ( isRightAligned
)
471 // move the label rect to the right
472 const int labelWidth
= rectLabel
.right
- rectLabel
.left
;
473 rectLabel
.right
= oldLabelRect
.right
;
474 rectLabel
.left
= rectLabel
.right
- labelWidth
;
480 ::SetTextColor(hdc
, ::GetSysColor(COLOR_GRAYTEXT
));
483 if ( !::DrawText(hdc
, label
.t_str(), label
.length(), &rectLabel
, fmt
) )
485 wxLogLastError(wxT("DrawText()"));
488 // finally draw the focus
493 if ( !::DrawFocusRect(hdc
, &rectLabel
) )
495 wxLogLastError(wxT("DrawFocusRect()"));
502 #endif // wxUSE_CHECKBOX