X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3c30bbb9faaa517f4fa3a4e896a945f3db82b1b0..8f182e0a3cec694ade9d9feeebbdebbf910bfcf6:/src/msw/toolbar.cpp diff --git a/src/msw/toolbar.cpp b/src/msw/toolbar.cpp index 5c4b2c4d5b..c02310686d 100644 --- a/src/msw/toolbar.cpp +++ b/src/msw/toolbar.cpp @@ -43,6 +43,7 @@ #include "wx/stattext.h" #endif +#include "wx/artprov.h" #include "wx/sysopt.h" #include "wx/dcclient.h" @@ -96,10 +97,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 // ---------------------------------------------------------------------------- @@ -284,8 +281,9 @@ void wxToolBar::Init() m_nButtons = 0; - m_defaultWidth = DEFAULTBITMAPX; - m_defaultHeight = DEFAULTBITMAPY; + const wxSize size = wxArtProvider::GetNativeSizeHint(wxART_TOOLBAR); + m_defaultWidth = size.x; + m_defaultHeight = size.y; m_pInTool = NULL; } @@ -397,9 +395,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); @@ -581,13 +577,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); } } @@ -631,6 +629,23 @@ void wxToolBar::CreateDisabledImageList() } } +void wxToolBar::AdjustToolBitmapSize() +{ + wxSize s(m_defaultWidth, m_defaultHeight); + const wxSize orig_s(s); + + for ( wxToolBarToolsList::const_iterator i = m_tools.begin(); + i != m_tools.end(); + ++i ) + { + const wxBitmap& bmp = (*i)->GetNormalBitmap(); + s.IncTo(bmp.GetSize()); + } + + if ( s != orig_s ) + SetToolBitmapSize(s); +} + bool wxToolBar::Realize() { const size_t nTools = GetToolsCount(); @@ -638,6 +653,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 @@ -1218,9 +1237,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 +1252,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 +1441,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 @@ -1420,11 +1469,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(); } // ---------------------------------------------------------------------------- @@ -1484,7 +1529,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.")); @@ -1496,7 +1541,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.")); @@ -1642,6 +1687,10 @@ 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 const RECT r = wxGetTBItemRect(GetHwnd(), 0); @@ -1859,12 +1908,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);