X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1c2b921a1ce6638dddeef827ea167f39dc119367..3e603cb7139e8b936f5704bccfc48fd25581d7cb:/src/msw/toolbar.cpp diff --git a/src/msw/toolbar.cpp b/src/msw/toolbar.cpp index 58490dcdc7..75630f870d 100644 --- a/src/msw/toolbar.cpp +++ b/src/msw/toolbar.cpp @@ -43,8 +43,10 @@ #include "wx/stattext.h" #endif +#include "wx/artprov.h" #include "wx/sysopt.h" #include "wx/dcclient.h" +#include "wx/scopedarray.h" #include "wx/msw/private.h" #include "wx/msw/dc.h" @@ -96,10 +98,6 @@ #define TB_GETMAXSIZE (WM_USER + 83) #endif -// these values correspond to those used by comctl32.dll -#define DEFAULTBITMAPX 16 -#define DEFAULTBITMAPY 15 - // ---------------------------------------------------------------------------- // wxWin macros // ---------------------------------------------------------------------------- @@ -216,7 +214,7 @@ private: size_t m_nSepCount; wxStaticText *m_staticText; - DECLARE_NO_COPY_CLASS(wxToolBarTool) + wxDECLARE_NO_COPY_CLASS(wxToolBarTool); }; // ---------------------------------------------------------------------------- @@ -284,8 +282,13 @@ void wxToolBar::Init() m_nButtons = 0; - m_defaultWidth = DEFAULTBITMAPX; - m_defaultHeight = DEFAULTBITMAPY; + // even though modern Windows applications typically use 24*24 (or even + // 32*32) size for their bitmaps, the native control itself still uses the + // old 16*15 default size (see TB_SETBITMAPSIZE documentation in MSDN), so + // default to it so that we don't call SetToolBitmapSize() unnecessarily in + // AdjustToolBitmapSize() + m_defaultWidth = 16; + m_defaultHeight = 15; m_pInTool = NULL; } @@ -631,6 +634,24 @@ void wxToolBar::CreateDisabledImageList() } } +void wxToolBar::AdjustToolBitmapSize() +{ + const wxSize sizeOrig(m_defaultWidth, m_defaultHeight); + + wxSize sizeActual(sizeOrig); + + for ( wxToolBarToolsList::const_iterator i = m_tools.begin(); + i != m_tools.end(); + ++i ) + { + const wxBitmap& bmp = (*i)->GetNormalBitmap(); + sizeActual.IncTo(bmp.GetSize()); + } + + if ( sizeActual != sizeOrig ) + SetToolBitmapSize(sizeActual); +} + bool wxToolBar::Realize() { const size_t nTools = GetToolsCount(); @@ -638,6 +659,10 @@ bool wxToolBar::Realize() // nothing to do return true; + // make sure tool size is larger enough for all all bitmaps to fit in + // (this is consistent with what other ports do): + AdjustToolBitmapSize(); + #ifdef wxREMAP_BUTTON_COLOURS // don't change the values of these constants, they can be set from the // user code via wxSystemOptions @@ -675,22 +700,12 @@ bool wxToolBar::Realize() wxToolBarToolsList::compatibility_iterator node; int bitmapId = 0; - wxSize sizeBmp; - if ( HasFlag(wxTB_NOICONS) ) - { - // no icons, don't leave space for them - sizeBmp.x = - sizeBmp.y = 0; - } - else // do show icons + if ( !HasFlag(wxTB_NOICONS) ) { // if we already have a bitmap, we'll replace the existing one -- // otherwise we'll install a new one HBITMAP oldToolBarBitmap = (HBITMAP)m_hBitmap; - sizeBmp.x = m_defaultWidth; - sizeBmp.y = m_defaultHeight; - const wxCoord totalBitmapWidth = m_defaultWidth * wx_truncate_cast(wxCoord, nTools), totalBitmapHeight = m_defaultHeight; @@ -894,18 +909,11 @@ bool wxToolBar::Realize() } } - // don't call SetToolBitmapSize() as we don't want to change the values of - // m_defaultWidth/Height - if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, - MAKELONG(sizeBmp.x, sizeBmp.y)) ) - { - wxLogLastError(_T("TB_SETBITMAPSIZE")); - } // Next add the buttons and separators // ----------------------------------- - TBBUTTON *buttons = new TBBUTTON[nTools]; + wxScopedArray buttons(new TBBUTTON[nTools]); // this array will hold the indices of all controls in the toolbar wxArrayInt controlIds; @@ -1025,12 +1033,11 @@ bool wxToolBar::Realize() i++; } - if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS, (WPARAM)i, (LPARAM)buttons) ) + if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS, i, (LPARAM)buttons.get()) ) { wxLogLastError(wxT("TB_ADDBUTTONS")); } - delete [] buttons; // Deal with the controls finally // ------------------------------ @@ -1218,9 +1225,10 @@ bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id_) bool toggled = false; // just to suppress warnings + LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0); + if ( tool->CanBeToggled() ) { - LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0); toggled = (state & TBSTATE_CHECKED) != 0; // ignore the event when a radio button is released, as this doesn't @@ -1232,9 +1240,38 @@ bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id_) UnToggleRadioGroup(tool); } + // Without the two lines of code below, if the toolbar was repainted during + // OnLeftClick(), then it could end up without the tool bitmap temporarily + // (see http://lists.nongnu.org/archive/html/lmi/2008-10/msg00014.html). + // The Update() call bellow ensures that this won't happen, by repainting + // invalidated areas of the toolbar immediately. + // + // To complicate matters, the tool would be drawn in depressed state (this + // code is called when mouse button is released, not pressed). That's not + // ideal, having the tool pressed for the duration of OnLeftClick() + // provides the user with useful visual clue that the app is busy reacting + // to the event. So we manually put the tool into pressed state, handle the + // event and then finally restore tool's original state. + ::SendMessage(GetHwnd(), TB_SETSTATE, id, MAKELONG(state | TBSTATE_PRESSED, 0)); + Update(); + + bool allowLeftClick = OnLeftClick(id, toggled); + + // Restore the unpressed state. Enabled/toggled state might have been + // changed since so take care of it. + if (tool->IsEnabled()) + state |= TBSTATE_ENABLED; + else + state &= ~TBSTATE_ENABLED; + if (tool->IsToggled()) + state |= TBSTATE_CHECKED; + else + state &= ~TBSTATE_CHECKED; + ::SendMessage(GetHwnd(), TB_SETSTATE, id, MAKELONG(state, 0)); + // OnLeftClick() can veto the button state change - for buttons which // may be toggled only, of couse - if ( !OnLeftClick(id, toggled) && tool->CanBeToggled() ) + if ( !allowLeftClick && tool->CanBeToggled() ) { // revert back tool->Toggle(!toggled); @@ -1392,7 +1429,7 @@ wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const // TB_HITTEST returns m_nButtons ( not -1 ) if ( index < 0 || (size_t)index >= m_nButtons ) // it's a separator or there is no tool at all there - return (wxToolBarToolBase *)NULL; + return NULL; // when TB_SETBUTTONINFO is available (both during compile- and run-time), // we don't use the dummy separators hack @@ -1480,7 +1517,7 @@ void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(tog void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap ) { - wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id)); + wxToolBarTool* tool = static_cast(FindById(id)); if ( tool ) { wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools.")); @@ -1492,7 +1529,7 @@ void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap ) void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap ) { - wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id)); + wxToolBarTool* tool = static_cast(FindById(id)); if ( tool ) { wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools.")); @@ -1559,7 +1596,7 @@ void wxToolBar::OnMouseEvent(wxMouseEvent& event) void wxToolBar::OnEraseBackground(wxEraseEvent& event) { RECT rect = wxGetClientRect(GetHwnd()); - + wxDC *dc = event.GetDC(); if (!dc) return; wxMSWDCImpl *impl = (wxMSWDCImpl*) dc->GetImpl(); @@ -1684,6 +1721,7 @@ bool wxToolBar::HandleSize(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam) return true; } +#ifndef __WXWINCE__ bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam) { // erase any dummy separators which were used @@ -1795,14 +1833,14 @@ bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam) r.right = clientSize.x; r.top = 0; r.bottom = clientSize.y; - + wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl(); HRESULT hr = theme->DrawThemeBackground(hTheme, GetHdcOf(*impl), 0, 0, & r, & clipRect); if ( hr == S_OK ) haveRefreshed = true; } } -#endif +#endif // wxUSE_UXTHEME if (!haveRefreshed) dc.DrawRectangle(rectItem); @@ -1827,6 +1865,7 @@ bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam) return true; } +#endif // __WXWINCE__ void wxToolBar::HandleMouseMove(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam) {