From cfe9625a0d23b854929dfa0ca996e3477d0fbf2d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 5 Dec 2014 22:19:25 +0000 Subject: [PATCH] Fix checked disabled wxToolBar tools with custom images in wxMSW Windows doesn't use the correct image for checked disabled tools, at least up to and including Windows 7, so don't put such tools in the "checked" state at all: this doesn't matter as they are disabled anyhow, but shows the correct image for them. See #12989. (cherry picked from commit 29cd13cc8f06eab726d3b774e057b64c94570923) --- docs/changes.txt | 1 + src/msw/toolbar.cpp | 33 +++++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 9ff3638986..cdc0528b7d 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -715,6 +715,7 @@ wxMSW: - Fix using Esc as accelerator in the menus. - Fix wrong initial status bar height in some cases (Artur Wieczorek). - Correct wxGetOsDescription() for Windows 10 (Tobias Taschner). +- Fix appearance of checked disabled wxToolBar tools with custom images. 3.0.1: (released 2014-06-15) diff --git a/src/msw/toolbar.cpp b/src/msw/toolbar.cpp index 080012c56b..8d95b39260 100644 --- a/src/msw/toolbar.cpp +++ b/src/msw/toolbar.cpp @@ -296,6 +296,23 @@ static RECT wxGetTBItemRect(HWND hwnd, int index, int id = wxID_NONE) return r; } +inline bool MSWShouldBeChecked(const wxToolBarToolBase *tool) +{ + // Apparently, "checked" state image overrides the "disabled" image + // so we need to enforce our custom "disabled" image (if there is any) + // to be drawn for checked and disabled button tool. + // Note: We believe this erroneous overriding is fixed in MSW 8. + if ( wxGetWinVersion() <= wxWinVersion_7 && + tool->GetKind() == wxITEM_CHECK && + tool->GetDisabledBitmap().IsOk() && + !tool->IsEnabled() ) + { + return false; + } + + return tool->IsToggled(); +} + // ============================================================================ // implementation // ============================================================================ @@ -1011,7 +1028,7 @@ bool wxToolBar::Realize() if ( tool->IsEnabled() ) button.fsState |= TBSTATE_ENABLED; - if ( tool->IsToggled() ) + if ( MSWShouldBeChecked(tool) ) button.fsState |= TBSTATE_CHECKED; switch ( tool->GetKind() ) @@ -1415,7 +1432,7 @@ bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id_) state |= TBSTATE_ENABLED; else state &= ~TBSTATE_ENABLED; - if (tool->IsToggled()) + if ( MSWShouldBeChecked(tool) ) state |= TBSTATE_CHECKED; else state &= ~TBSTATE_CHECKED; @@ -1428,7 +1445,8 @@ bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id_) // revert back tool->Toggle(!toggled); - ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(!toggled, 0)); + ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, + MAKELONG(MSWShouldBeChecked(tool), 0)); } return true; @@ -1672,12 +1690,19 @@ void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable) { ::SendMessage(GetHwnd(), TB_ENABLEBUTTON, (WPARAM)tool->GetId(), (LPARAM)MAKELONG(enable, 0)); + + // Adjust displayed checked state -- it could have changed if the tool is + // disabled and has a custom "disabled state" bitmap. + DoToggleTool(tool, tool->IsToggled()); } void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle) { + wxASSERT_MSG( tool->IsToggled() == toggle, wxT("Inconsistent tool state") ); + ::SendMessage(GetHwnd(), TB_CHECKBUTTON, - (WPARAM)tool->GetId(), (LPARAM)MAKELONG(toggle, 0)); + (WPARAM)tool->GetId(), + (LPARAM)MAKELONG(MSWShouldBeChecked(tool), 0)); } void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))