Factor out common owner drawn code from wx{Check,Radio}Box.

Create wxMSWOwnerDrawnButton class which contains all of this code.

Currently the methods of this class are (still) implemented in
src/msw/control.cpp.

See #10137.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76459 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2014-05-04 22:13:28 +00:00
parent f1be272d32
commit cc73050b73
7 changed files with 291 additions and 280 deletions

View File

@@ -84,8 +84,6 @@ enum
void wxCheckBox::Init()
{
m_state = wxCHK_UNCHECKED;
m_isPressed =
m_isHot = false;
}
bool wxCheckBox::Create(wxWindow *parent,
@@ -269,145 +267,43 @@ bool wxCheckBox::MSWCommand(WXUINT cmd, WXWORD WXUNUSED(id))
}
// ----------------------------------------------------------------------------
// owner drawn checkboxes stuff
// owner drawn checkboxes support
// ----------------------------------------------------------------------------
bool wxCheckBox::SetForegroundColour(const wxColour& colour)
int wxCheckBox::MSWGetButtonStyle() const
{
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() )
MSWMakeOwnerDrawn(colour.IsOk());
return true;
return HasFlag(wxCHK_3STATE) ? BS_3STATE : BS_CHECKBOX;
}
bool wxCheckBox::IsOwnerDrawn() const
void wxCheckBox::MSWOnButtonResetOwnerDrawn()
{
return
(::GetWindowLong(GetHwnd(), GWL_STYLE) & BS_OWNERDRAW) == BS_OWNERDRAW;
// ensure that controls state is consistent with internal state
DoSet3StateValue(m_state);
}
void wxCheckBox::MSWMakeOwnerDrawn(bool ownerDrawn)
int wxCheckBox::MSWGetButtonCheckedFlag() const
{
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);
// shall we draw a focus rect?
const bool isFocused = m_isPressed || FindFocus() == this;
int flags = 0;
if ( !IsEnabled() )
flags |= wxCONTROL_DISABLED;
switch ( Get3StateValue() )
{
case wxCHK_CHECKED:
flags |= wxCONTROL_CHECKED;
break;
return wxCONTROL_CHECKED;
case wxCHK_UNDETERMINED:
flags |= wxCONTROL_PRESSED;
break;
default:
wxFAIL_MSG( wxT("unexpected Get3StateValue() return value") );
// fall through
return wxCONTROL_PRESSED;
case wxCHK_UNCHECKED:
// no extra styles needed
break;
return 0;
}
if ( m_isPressed )
flags |= wxCONTROL_PRESSED;
wxFAIL_MSG( wxT("unexpected Get3StateValue() return value") );
if ( wxFindWindowAtPoint(wxGetMousePosition()) == this )
flags |= wxCONTROL_CURRENT;
return MSWOwnerDrawnButton(dis, flags, isFocused);
return 0;
}
void wxCheckBox::MSWDrawButtonBitmap(wxWindow *win, wxDC& dc, const wxRect& rect, int flags)
void wxCheckBox::MSWDrawButtonBitmap(wxDC& dc, const wxRect& rect, int flags)
{
wxRendererNative::Get().DrawCheckBox(win, dc, rect, flags);
wxRendererNative::Get().DrawCheckBox(this, dc, rect, flags);
}
#endif // wxUSE_CHECKBOX