+ return m_state;
+}
+
+bool wxCheckBox::MSWCommand(WXUINT cmd, WXWORD WXUNUSED(id))
+{
+ if ( cmd != BN_CLICKED && cmd != BN_DBLCLK )
+ return false;
+
+ // first update the value so that user event handler gets the new checkbox
+ // value
+
+ // ownerdrawn buttons don't manage their state themselves unlike usual
+ // auto checkboxes so do it ourselves in any case
+ wxCheckBoxState state;
+ if ( Is3rdStateAllowedForUser() )
+ {
+ state = (wxCheckBoxState)((m_state + 1) % 3);
+ }
+ else // 2 state checkbox (at least from users point of view)
+ {
+ // note that wxCHK_UNDETERMINED also becomes unchecked when clicked
+ state = m_state == wxCHK_UNCHECKED ? wxCHK_CHECKED : wxCHK_UNCHECKED;
+ }
+
+ DoSet3StateValue(state);
+
+
+ // generate the event
+ wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, m_windowId);
+
+ event.SetInt(state);
+ event.SetEventObject(this);
+ ProcessCommand(event);
+
+ return true;
+}
+
+// ----------------------------------------------------------------------------
+// owner drawn checkboxes stuff
+// ----------------------------------------------------------------------------
+
+bool wxCheckBox::SetForegroundColour(const wxColour& colour)
+{
+ if ( !wxCheckBoxBase::SetForegroundColour(colour) )
+ return false;
+
+ // the only way to change the checkbox foreground colour under Windows XP
+ // is to owner draw it
+ if ( wxUxThemeEngine::GetIfActive() )
+ MakeOwnerDrawn(colour.Ok());
+
+ return true;
+}
+
+bool wxCheckBox::IsOwnerDrawn() const
+{
+ return
+ (::GetWindowLong(GetHwnd(), GWL_STYLE) & BS_OWNERDRAW) == BS_OWNERDRAW;
+}
+
+void wxCheckBox::MakeOwnerDrawn(bool ownerDrawn)
+{
+ long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
+
+ // note that BS_CHECKBOX & BS_OWNERDRAW != 0 so we can't operate on
+ // them as on independent style bits
+ if ( ownerDrawn )
+ {
+ style &= ~(BS_CHECKBOX | BS_3STATE);
+ style |= BS_OWNERDRAW;
+
+ Connect(wxEVT_ENTER_WINDOW,
+ wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
+ Connect(wxEVT_LEAVE_WINDOW,
+ wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
+ Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
+ Connect(wxEVT_LEFT_UP, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
+ Connect(wxEVT_SET_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
+ Connect(wxEVT_KILL_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
+ }
+ else // reset to default colour
+ {
+ style &= ~BS_OWNERDRAW;
+ style |= HasFlag(wxCHK_3STATE) ? BS_3STATE : BS_CHECKBOX;
+
+ Disconnect(wxEVT_ENTER_WINDOW,
+ wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
+ Disconnect(wxEVT_LEAVE_WINDOW,
+ wxMouseEventHandler(wxCheckBox::OnMouseEnterOrLeave));
+ Disconnect(wxEVT_LEFT_DOWN, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
+ Disconnect(wxEVT_LEFT_UP, wxMouseEventHandler(wxCheckBox::OnMouseLeft));
+ Disconnect(wxEVT_SET_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
+ Disconnect(wxEVT_KILL_FOCUS, wxFocusEventHandler(wxCheckBox::OnFocus));
+ }
+
+ ::SetWindowLong(GetHwnd(), GWL_STYLE, style);
+
+ if ( !ownerDrawn )
+ {
+ // ensure that controls state is consistent with internal state
+ DoSet3StateValue(m_state);
+ }
+}
+
+void wxCheckBox::OnMouseEnterOrLeave(wxMouseEvent& event)
+{
+ m_isHot = event.GetEventType() == wxEVT_ENTER_WINDOW;
+ if ( !m_isHot )
+ m_isPressed = false;
+
+ Refresh();
+
+ event.Skip();
+}
+
+void wxCheckBox::OnMouseLeft(wxMouseEvent& event)
+{
+ // TODO: we should capture the mouse here to be notified about left up
+ // event but this interferes with BN_CLICKED generation so if we
+ // want to do this we'd need to generate them ourselves
+ m_isPressed = event.GetEventType() == wxEVT_LEFT_DOWN;
+ Refresh();
+
+ event.Skip();
+}
+
+void wxCheckBox::OnFocus(wxFocusEvent& event)
+{
+ Refresh();
+
+ event.Skip();
+}
+
+bool wxCheckBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
+{
+ DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)item;
+
+ if ( !IsOwnerDrawn() || dis->CtlType != ODT_BUTTON )
+ return wxCheckBoxBase::MSWOnDraw(item);
+
+ // calculate the rectangles for the check mark itself and the label
+ HDC hdc = dis->hDC;
+ RECT& rect = dis->rcItem;
+ RECT rectCheck,
+ rectLabel;
+ rectCheck.top =
+ rectLabel.top = rect.top;
+ rectCheck.bottom =
+ rectLabel.bottom = rect.bottom;
+ const int checkSize = GetBestSize().y;
+ const int MARGIN = 3;
+
+ const bool isRightAligned = HasFlag(wxALIGN_RIGHT);
+ if ( isRightAligned )
+ {
+ rectCheck.right = rect.right;
+ rectCheck.left = rectCheck.right - checkSize;