X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/936f635341e9b92b37db93cc7781d564babbe25f..69d31e313035d5e22d9400ec946f6007f710910c:/src/msw/toolbar.cpp?ds=sidebyside diff --git a/src/msw/toolbar.cpp b/src/msw/toolbar.cpp index 3bfec13c64..ba4638e445 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 // ---------------------------------------------------------------------------- @@ -202,7 +200,7 @@ public: wxStaticText* GetStaticText() { wxASSERT_MSG( IsControl(), - _T("only makes sense for embedded control tools") ); + wxT("only makes sense for embedded control tools") ); return m_staticText; } @@ -216,9 +214,36 @@ private: size_t m_nSepCount; wxStaticText *m_staticText; - DECLARE_NO_COPY_CLASS(wxToolBarTool) + wxDECLARE_NO_COPY_CLASS(wxToolBarTool); }; +// ---------------------------------------------------------------------------- +// helper functions +// ---------------------------------------------------------------------------- + +// return the rectangle of the item at the given index +// +// returns an empty (0, 0, 0, 0) rectangle if fails so the caller may compare +// r.right or r.bottom with 0 to check for this +static RECT wxGetTBItemRect(HWND hwnd, int index) +{ + RECT r; + + // note that we use TB_GETITEMRECT and not TB_GETRECT because the latter + // only appeared in v4.70 of comctl32.dll + if ( !::SendMessage(hwnd, TB_GETITEMRECT, index, (LPARAM)&r) ) + { + wxLogLastError(wxT("TB_GETITEMRECT")); + + r.top = + r.left = + r.right = + r.bottom = 0; + } + + return r; +} + // ============================================================================ // implementation // ============================================================================ @@ -257,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 + // wxToolBarBase::AdjustToolBitmapSize() + m_defaultWidth = 16; + m_defaultHeight = 15; m_pInTool = NULL; } @@ -331,7 +361,7 @@ void wxToolBar::Recreate() if ( !MSWCreateToolbar(pos, size) ) { // what can we do? - wxFAIL_MSG( _T("recreating the toolbar failed") ); + wxFAIL_MSG( wxT("recreating the toolbar failed") ); return; } @@ -370,9 +400,7 @@ wxToolBar::~wxToolBar() { // we must refresh the frame size when the toolbar is deleted but the frame // is not - otherwise toolbar leaves a hole in the place it used to occupy - wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); - if ( frame && !frame->IsBeingDeleted() ) - frame->SendSizeEvent(); + SendSizeEventToParent(); if ( m_hBitmap ) ::DeleteObject((HBITMAP) m_hBitmap); @@ -401,13 +429,27 @@ wxSize wxToolBar::DoGetBestSize() const sizeBest.y = t; } } - else + else // TB_GETMAXSIZE succeeded { + // but it could still return an incorrect result due to what appears to + // be a bug in old comctl32.dll versions which don't handle controls in + // the toolbar correctly, so work around it (see SF patch 1902358) + if ( !IsVertical() && wxApp::GetComCtl32Version() < 600 ) + { + // calculate the toolbar width in alternative way + const RECT rcFirst = wxGetTBItemRect(GetHwnd(), 0); + const RECT rcLast = wxGetTBItemRect(GetHwnd(), GetToolsCount() - 1); + + const int widthAlt = rcLast.right - rcFirst.left; + if ( widthAlt > size.cx ) + size.cx = widthAlt; + } + sizeBest.x = size.cx; sizeBest.y = size.cy; } - if (!IsVertical()) + if ( !IsVertical() ) { // Without the extra height, DoGetBestSize can report a size that's // smaller than the actual window, causing windows to overlap slightly @@ -473,12 +515,11 @@ WXDWORD wxToolBar::MSWGetStyle(long style, WXDWORD *exstyle) const // adding/removing tools // ---------------------------------------------------------------------------- -bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool) +bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), + wxToolBarToolBase * WXUNUSED(tool)) { // nothing special to do here - we really create the toolbar buttons in // Realize() later - tool->Attach(this); - InvalidateBestSize(); return true; } @@ -512,11 +553,7 @@ bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool) size_t nButtonsToDelete = 1; // get the size of the button we're going to delete - RECT r; - if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT, pos, (LPARAM)&r) ) - { - wxLogLastError(_T("TB_GETITEMRECT")); - } + const RECT r = wxGetTBItemRect(GetHwnd(), pos); int width = r.right - r.left; @@ -524,7 +561,6 @@ bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool) { nButtonsToDelete = ((wxToolBarTool *)tool)->GetSeparatorsCount(); width *= nButtonsToDelete; - tool->GetControl()->Destroy(); } // do delete all buttons @@ -539,8 +575,6 @@ bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool) } } - tool->Detach(); - // and finally reposition all the controls after this button (the toolbar // takes care of all normal items) for ( /* node -> first after deleted */ ; node; node = node->GetNext() ) @@ -548,13 +582,15 @@ bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool) wxToolBarTool *tool2 = (wxToolBarTool*)node->GetData(); if ( tool2->IsControl() ) { + wxControl * const control = tool2->GetControl(); + int x; - wxControl *control = tool2->GetControl(); control->GetPosition(&x, NULL); control->Move(x - width, wxDefaultCoord); - wxStaticText* staticText = tool2->GetStaticText(); - staticText->Move(x - width, wxDefaultCoord); + wxStaticText * const staticText = tool2->GetStaticText(); + if ( staticText ) + staticText->Move(x - width, wxDefaultCoord); } } @@ -600,10 +636,10 @@ void wxToolBar::CreateDisabledImageList() bool wxToolBar::Realize() { + if ( !wxToolBarBase::Realize() ) + return false; + const size_t nTools = GetToolsCount(); - if ( nTools == 0 ) - // nothing to do - return true; #ifdef wxREMAP_BUTTON_COLOURS // don't change the values of these constants, they can be set from the @@ -642,22 +678,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; @@ -726,7 +752,7 @@ bool wxToolBar::Realize() } else { - wxFAIL_MSG( _T("invalid tool button bitmap") ); + wxFAIL_MSG( wxT("invalid tool button bitmap") ); } // also deal with disabled bitmap if we want to use them @@ -861,18 +887,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; @@ -978,7 +997,7 @@ bool wxToolBar::Realize() break; default: - wxFAIL_MSG( _T("unexpected toolbar button kind") ); + wxFAIL_MSG( wxT("unexpected toolbar button kind") ); button.fsStyle = TBSTYLE_BUTTON; break; } @@ -992,12 +1011,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 // ------------------------------ @@ -1016,15 +1034,7 @@ bool wxToolBar::Realize() if ( !isControl && !IsVertical() ) continue; - // note that we use TB_GETITEMRECT and not TB_GETRECT because the - // latter only appeared in v4.70 of comctl32.dll - RECT r; - if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT, - index, (LPARAM)(LPRECT)&r) ) - { - wxLogLastError(wxT("TB_GETITEMRECT")); - } - + const RECT r = wxGetTBItemRect(GetHwnd(), index); if ( !isControl ) { // can only be control if isVertical @@ -1193,9 +1203,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 @@ -1207,9 +1218,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); @@ -1237,15 +1277,15 @@ bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl), } const wxToolBarToolBase * const tool = FindById(tbhdr->iItem); - wxCHECK_MSG( tool, false, _T("drop down message for unknown tool") ); + wxCHECK_MSG( tool, false, wxT("drop down message for unknown tool") ); wxMenu * const menu = tool->GetDropdownMenu(); if ( !menu ) return false; // Display popup menu below button - RECT r; - if (::SendMessage(GetHwnd(), TB_GETITEMRECT, GetToolPos(tbhdr->iItem), (LPARAM)&r)) + const RECT r = wxGetTBItemRect(GetHwnd(), GetToolPos(tbhdr->iItem)); + if ( r.right ) PopupMenu(menu, r.left, r.bottom); return true; @@ -1367,7 +1407,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 @@ -1395,11 +1435,7 @@ void wxToolBar::UpdateSize() // toolbar to full width again, but only if the parent is a frame and the // toolbar is managed by the frame. Otherwise assume that some other // layout mechanism is controlling the toolbar size and leave it alone. - wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); - if ( frame && frame->GetToolBar() == this ) - { - frame->SendSizeEvent(); - } + SendSizeEventToParent(); } // ---------------------------------------------------------------------------- @@ -1454,12 +1490,12 @@ void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(tog { // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or // without, so we really need to delete the button and recreate it here - wxFAIL_MSG( _T("not implemented") ); + wxFAIL_MSG( wxT("not implemented") ); } 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.")); @@ -1471,7 +1507,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.")); @@ -1538,7 +1574,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(); @@ -1564,7 +1600,9 @@ void wxToolBar::OnEraseBackground(wxEraseEvent& event) // it can also return S_FALSE which seems to simply say that it // didn't draw anything but no error really occurred if ( FAILED(hr) ) - wxLogApiError(_T("DrawThemeParentBackground(toolbar)"), hr); + { + wxLogApiError(wxT("DrawThemeParentBackground(toolbar)"), hr); + } } } @@ -1587,7 +1625,9 @@ void wxToolBar::OnEraseBackground(wxEraseEvent& event) // it can also return S_FALSE which seems to simply say that it // didn't draw anything but no error really occurred if ( FAILED(hr) ) - wxLogApiError(_T("DrawThemeParentBackground(toolbar)"), hr); + { + wxLogApiError(wxT("DrawThemeParentBackground(toolbar)"), hr); + } } } @@ -1617,50 +1657,53 @@ void wxToolBar::OnEraseBackground(wxEraseEvent& event) bool wxToolBar::HandleSize(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam) { + // wait until we have some tools + if ( !GetToolsCount() ) + return false; + // calculate our minor dimension ourselves - we're confusing the standard // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks - RECT r; - if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT, 0, (LPARAM)&r) ) - { - int w, h; + const RECT r = wxGetTBItemRect(GetHwnd(), 0); + if ( !r.right ) + return false; - if ( IsVertical() ) + int w, h; + + if ( IsVertical() ) + { + w = r.right - r.left; + if ( m_maxRows ) { - w = r.right - r.left; - if ( m_maxRows ) - { - w *= (m_nButtons + m_maxRows - 1)/m_maxRows; - } - h = HIWORD(lParam); + w *= (m_nButtons + m_maxRows - 1)/m_maxRows; } + h = HIWORD(lParam); + } + else + { + w = LOWORD(lParam); + if (HasFlag( wxTB_FLAT )) + h = r.bottom - r.top - 3; else + h = r.bottom - r.top; + if ( m_maxRows ) { - w = LOWORD(lParam); - if (HasFlag( wxTB_FLAT )) - h = r.bottom - r.top - 3; - else - h = r.bottom - r.top; - if ( m_maxRows ) - { - // FIXME: hardcoded separator line height... - h += HasFlag(wxTB_NODIVIDER) ? 4 : 6; - h *= m_maxRows; - } - } - - if ( MAKELPARAM(w, h) != lParam ) - { - // size really changed - SetSize(w, h); + // FIXME: hardcoded separator line height... + h += HasFlag(wxTB_NODIVIDER) ? 4 : 6; + h *= m_maxRows; } + } - // message processed - return true; + if ( MAKELPARAM(w, h) != lParam ) + { + // size really changed + SetSize(w, h); } - return false; + // message processed + return true; } +#ifndef __WXWINCE__ bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam) { // erase any dummy separators which were used @@ -1730,7 +1773,7 @@ bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam) if ( !::SendMessage(GetHwnd(), TB_GETBUTTON, n, (LPARAM)&tbb) ) { - wxLogDebug(_T("TB_GETBUTTON failed?")); + wxLogDebug(wxT("TB_GETBUTTON failed?")); continue; } @@ -1739,14 +1782,9 @@ bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam) continue; // get the bounding rect of the separator - RECT r; - if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT, - n, (LPARAM)&r) ) - { - wxLogDebug(_T("TB_GETITEMRECT failed?")); - + RECT r = wxGetTBItemRect(GetHwnd(), n); + if ( !r.right ) continue; - } // does it intersect the control? wxRect rectItem; @@ -1777,14 +1815,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); @@ -1809,6 +1847,7 @@ bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam) return true; } +#endif // __WXWINCE__ void wxToolBar::HandleMouseMove(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam) { @@ -1841,12 +1880,14 @@ WXLRESULT wxToolBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam #ifndef __WXWINCE__ case WM_PAINT: - if ( HandlePaint(wParam, lParam) ) + // refreshing the controls in the toolbar inside a composite window + // results in an endless stream of WM_PAINT messages -- and seems + // to be unnecessary anyhow as everything works just fine without + // any special workarounds in this case + if ( !IsDoubleBuffered() && HandlePaint(wParam, lParam) ) return 0; -#endif - - default: break; +#endif // __WXWINCE__ } return wxControl::MSWWindowProc(nMsg, wParam, lParam); @@ -1864,7 +1905,7 @@ WXHBITMAP wxToolBar::MapBitmap(WXHBITMAP bitmap, int width, int height) if ( !hdcMem ) { - wxLogLastError(_T("CreateCompatibleDC")); + wxLogLastError(wxT("CreateCompatibleDC")); return bitmap; } @@ -1873,7 +1914,7 @@ WXHBITMAP wxToolBar::MapBitmap(WXHBITMAP bitmap, int width, int height) if ( !bmpInHDC ) { - wxLogLastError(_T("SelectObject")); + wxLogLastError(wxT("SelectObject")); return bitmap; }