+ // the main difficulty we have here is with the controls in the toolbars:
+ // as we (sometimes) use several separators to cover up the space used by
+ // them, the indices are not the same for us and the toolbar
+
+ // first determine the position of the first button to delete: it may be
+ // different from pos if we use several separators to cover the space used
+ // by a control
+ wxToolBarToolsList::compatibility_iterator node;
+ for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
+ {
+ wxToolBarToolBase *tool2 = node->GetData();
+ if ( tool2 == tool )
+ {
+ // let node point to the next node in the list
+ node = node->GetNext();
+
+ break;
+ }
+
+ if ( tool2->IsControl() )
+ pos += ((wxToolBarTool *)tool2)->GetSeparatorsCount() - 1;
+ }
+
+ // now determine the number of buttons to delete and the area taken by them
+ 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"));
+ }
+
+ int width = r.right - r.left;
+
+ if ( tool->IsControl() )
+ {
+ nButtonsToDelete = ((wxToolBarTool *)tool)->GetSeparatorsCount();
+ width *= nButtonsToDelete;
+ tool->GetControl()->Destroy();
+ }
+
+ // do delete all buttons
+ m_nButtons -= nButtonsToDelete;
+ while ( nButtonsToDelete-- > 0 )
+ {
+ if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, pos, 0) )
+ {
+ wxLogLastError(wxT("TB_DELETEBUTTON"));
+
+ return false;
+ }
+ }
+
+ 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() )
+ {
+ wxToolBarTool *tool2 = (wxToolBarTool*)node->GetData();
+ if ( tool2->IsControl() )
+ {
+ int x;
+ wxControl *control = tool2->GetControl();
+ control->GetPosition(&x, NULL);
+ control->Move(x - width, wxDefaultCoord);
+
+ wxStaticText* staticText = tool2->GetStaticText();
+ staticText->Move(x - width, wxDefaultCoord);
+ }
+ }
+
+ InvalidateBestSize();
+
+ return true;
+}
+
+void wxToolBar::CreateDisabledImageList()
+{
+ if (m_disabledImgList != NULL)
+ {
+ delete m_disabledImgList;
+ m_disabledImgList = NULL;
+ }
+
+ // as we can't use disabled image list with older versions of comctl32.dll,
+ // don't even bother creating it
+ if ( wxApp::GetComCtl32Version() >= 470 )
+ {
+ // search for the first disabled button img in the toolbar, if any
+ for ( wxToolBarToolsList::compatibility_iterator
+ node = m_tools.GetFirst(); node; node = node->GetNext() )
+ {
+ wxToolBarToolBase *tool = node->GetData();
+ wxBitmap bmpDisabled = tool->GetDisabledBitmap();
+ if ( bmpDisabled.Ok() )
+ {
+ m_disabledImgList = new wxImageList
+ (
+ m_defaultWidth,
+ m_defaultHeight,
+ bmpDisabled.GetMask() != NULL,
+ GetToolsCount()
+ );
+ break;
+ }
+ }
+
+ // we don't have any disabled bitmaps
+ }
+}
+
+bool wxToolBar::Realize()
+{
+ 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
+ // user code via wxSystemOptions
+ enum
+ {
+ Remap_None = -1,
+ Remap_Bg,
+ Remap_Buttons,
+ Remap_TransparentBg
+ };
+
+ // the user-specified option overrides anything, but if it wasn't set, only
+ // remap the buttons on 8bpp displays as otherwise the bitmaps usually look
+ // much worse after remapping
+ static const wxChar *remapOption = wxT("msw.remap");
+ const int remapValue = wxSystemOptions::HasOption(remapOption)
+ ? wxSystemOptions::GetOptionInt(remapOption)
+ : wxDisplayDepth() <= 8 ? Remap_Buttons
+ : Remap_None;
+
+#endif // wxREMAP_BUTTON_COLOURS
+
+ // delete all old buttons, if any
+ for ( size_t pos = 0; pos < m_nButtons; pos++ )
+ {
+ if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, 0, 0) )
+ {
+ wxLogDebug(wxT("TB_DELETEBUTTON failed"));
+ }
+ }
+
+ // First, add the bitmap: we use one bitmap for all toolbar buttons
+ // ----------------------------------------------------------------
+
+ 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 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;
+
+ // Create a bitmap and copy all the tool bitmaps into it
+ wxMemoryDC dcAllButtons;
+ wxBitmap bitmap(totalBitmapWidth, totalBitmapHeight);
+ dcAllButtons.SelectObject(bitmap);
+
+#ifdef wxREMAP_BUTTON_COLOURS
+ if ( remapValue != Remap_TransparentBg )
+#endif // wxREMAP_BUTTON_COLOURS
+ {
+ // VZ: why do we hardcode grey colour for CE?
+ dcAllButtons.SetBackground(wxBrush(
+#ifdef __WXWINCE__
+ wxColour(0xc0, 0xc0, 0xc0)
+#else // !__WXWINCE__
+ GetBackgroundColour()
+#endif // __WXWINCE__/!__WXWINCE__
+ ));
+ dcAllButtons.Clear();
+ }
+
+ m_hBitmap = bitmap.GetHBITMAP();
+ HBITMAP hBitmap = (HBITMAP)m_hBitmap;
+
+#ifdef wxREMAP_BUTTON_COLOURS
+ if ( remapValue == Remap_Bg )
+ {
+ dcAllButtons.SelectObject(wxNullBitmap);
+
+ // Even if we're not remapping the bitmap
+ // content, we still have to remap the background.
+ hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap,
+ totalBitmapWidth, totalBitmapHeight);
+
+ dcAllButtons.SelectObject(bitmap);
+ }
+#endif // wxREMAP_BUTTON_COLOURS
+
+ // the button position
+ wxCoord x = 0;
+
+ // the number of buttons (not separators)
+ int nButtons = 0;
+
+ CreateDisabledImageList();
+ for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
+ {
+ wxToolBarToolBase *tool = node->GetData();
+ if ( tool->IsButton() )
+ {
+ const wxBitmap& bmp = tool->GetNormalBitmap();
+
+ const int w = bmp.GetWidth();
+ const int h = bmp.GetHeight();
+
+ if ( bmp.Ok() )
+ {
+ int xOffset = wxMax(0, (m_defaultWidth - w)/2);
+ int yOffset = wxMax(0, (m_defaultHeight - h)/2);
+
+ // notice the last parameter: do use mask
+ dcAllButtons.DrawBitmap(bmp, x + xOffset, yOffset, true);
+ }
+ else
+ {
+ wxFAIL_MSG( _T("invalid tool button bitmap") );
+ }
+
+ // also deal with disabled bitmap if we want to use them
+ if ( m_disabledImgList )
+ {
+ wxBitmap bmpDisabled = tool->GetDisabledBitmap();
+#if wxUSE_IMAGE && wxUSE_WXDIB
+ if ( !bmpDisabled.Ok() )
+ {
+ // no disabled bitmap specified but we still need to
+ // fill the space in the image list with something, so
+ // we grey out the normal bitmap
+ wxImage
+ imgGreyed = bmp.ConvertToImage().ConvertToGreyscale();
+
+#ifdef wxREMAP_BUTTON_COLOURS
+ if ( remapValue == Remap_Buttons )
+ {
+ // we need to have light grey background colour for
+ // MapBitmap() to work correctly
+ for ( int y = 0; y < h; y++ )
+ {
+ for ( int x = 0; x < w; x++ )
+ {
+ if ( imgGreyed.IsTransparent(x, y) )
+ imgGreyed.SetRGB(x, y,
+ wxLIGHT_GREY->Red(),
+ wxLIGHT_GREY->Green(),
+ wxLIGHT_GREY->Blue());
+ }
+ }
+ }
+#endif // wxREMAP_BUTTON_COLOURS
+
+ bmpDisabled = wxBitmap(imgGreyed);
+ }
+#endif // wxUSE_IMAGE
+
+#ifdef wxREMAP_BUTTON_COLOURS
+ if ( remapValue == Remap_Buttons )
+ MapBitmap(bmpDisabled.GetHBITMAP(), w, h);
+#endif // wxREMAP_BUTTON_COLOURS
+
+ m_disabledImgList->Add(bmpDisabled);
+ }
+
+ // still inc width and number of buttons because otherwise the
+ // subsequent buttons will all be shifted which is rather confusing
+ // (and like this you'd see immediately which bitmap was bad)
+ x += m_defaultWidth;
+ nButtons++;
+ }
+ }
+
+ dcAllButtons.SelectObject(wxNullBitmap);
+
+ // don't delete this HBITMAP!
+ bitmap.SetHBITMAP(0);
+
+#ifdef wxREMAP_BUTTON_COLOURS
+ if ( remapValue == Remap_Buttons )
+ {
+ // Map to system colours
+ hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap,
+ totalBitmapWidth, totalBitmapHeight);
+ }
+#endif // wxREMAP_BUTTON_COLOURS
+
+ bool addBitmap = true;
+
+ if ( oldToolBarBitmap )
+ {
+#ifdef TB_REPLACEBITMAP
+ if ( wxApp::GetComCtl32Version() >= 400 )
+ {
+ TBREPLACEBITMAP replaceBitmap;
+ replaceBitmap.hInstOld = NULL;
+ replaceBitmap.hInstNew = NULL;
+ replaceBitmap.nIDOld = (UINT_PTR)oldToolBarBitmap;
+ replaceBitmap.nIDNew = (UINT_PTR)hBitmap;
+ replaceBitmap.nButtons = nButtons;
+ if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP,
+ 0, (LPARAM) &replaceBitmap) )
+ {
+ wxFAIL_MSG(wxT("Could not replace the old bitmap"));
+ }
+
+ ::DeleteObject(oldToolBarBitmap);
+
+ // already done
+ addBitmap = false;
+ }
+ else
+#endif // TB_REPLACEBITMAP
+ {
+ // we can't replace the old bitmap, so we will add another one
+ // (awfully inefficient, but what else to do?) and shift the bitmap
+ // indices accordingly
+ addBitmap = true;
+
+ bitmapId = m_nButtons;
+ }
+ }
+
+ if ( addBitmap ) // no old bitmap or we can't replace it
+ {
+ TBADDBITMAP addBitmap;
+ addBitmap.hInst = 0;
+ addBitmap.nID = (UINT_PTR)hBitmap;
+ if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP,
+ (WPARAM) nButtons, (LPARAM)&addBitmap) == -1 )
+ {
+ wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
+ }
+ }
+
+ // disable image lists are only supported in comctl32.dll 4.70+
+ if ( wxApp::GetComCtl32Version() >= 470 )
+ {
+ HIMAGELIST hil = m_disabledImgList
+ ? GetHimagelistOf(m_disabledImgList)
+ : 0;
+
+ // notice that we set the image list even if don't have one right
+ // now as we could have it before and need to reset it in this case
+ HIMAGELIST oldImageList = (HIMAGELIST)
+ ::SendMessage(GetHwnd(), TB_SETDISABLEDIMAGELIST, 0, (LPARAM)hil);
+
+ // delete previous image list if any
+ if ( oldImageList )
+ ::DeleteObject(oldImageList);
+ }
+ }
+
+ // 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];
+
+ // this array will hold the indices of all controls in the toolbar
+ wxArrayInt controlIds;
+
+ bool lastWasRadio = false;
+ int i = 0;
+ for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
+ {
+ wxToolBarToolBase *tool = node->GetData();
+
+ // don't add separators to the vertical toolbar with old comctl32.dll
+ // versions as they didn't handle this properly
+ if ( IsVertical() && tool->IsSeparator() &&
+ wxApp::GetComCtl32Version() <= 472 )
+ {
+ continue;
+ }
+
+ TBBUTTON& button = buttons[i];
+
+ wxZeroMemory(button);
+
+ bool isRadio = false;
+ switch ( tool->GetStyle() )
+ {
+ case wxTOOL_STYLE_CONTROL:
+ button.idCommand = tool->GetId();
+ // fall through: create just a separator too
+
+ case wxTOOL_STYLE_SEPARATOR:
+ button.fsState = TBSTATE_ENABLED;
+ button.fsStyle = TBSTYLE_SEP;
+ break;
+
+ case wxTOOL_STYLE_BUTTON:
+ if ( !HasFlag(wxTB_NOICONS) )
+ button.iBitmap = bitmapId;
+
+ if ( HasFlag(wxTB_TEXT) )
+ {
+ const wxString& label = tool->GetLabel();
+ if ( !label.empty() )
+ button.iString = (INT_PTR)label.wx_str();
+ }
+
+ button.idCommand = tool->GetId();
+
+ if ( tool->IsEnabled() )
+ button.fsState |= TBSTATE_ENABLED;
+ if ( tool->IsToggled() )
+ button.fsState |= TBSTATE_CHECKED;
+
+ switch ( tool->GetKind() )
+ {
+ case wxITEM_RADIO:
+ button.fsStyle = TBSTYLE_CHECKGROUP;
+
+ if ( !lastWasRadio )
+ {
+ // the first item in the radio group is checked by
+ // default to be consistent with wxGTK and the menu
+ // radio items
+ button.fsState |= TBSTATE_CHECKED;
+
+ if (tool->Toggle(true))
+ {
+ DoToggleTool(tool, true);
+ }
+ }
+ else if ( tool->IsToggled() )
+ {
+ wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
+ int prevIndex = i - 1;
+ while ( nodePrev )
+ {
+ TBBUTTON& prevButton = buttons[prevIndex];
+ wxToolBarToolBase *tool = nodePrev->GetData();
+ if ( !tool->IsButton() || tool->GetKind() != wxITEM_RADIO )
+ break;
+
+ if ( tool->Toggle(false) )
+ DoToggleTool(tool, false);
+
+ prevButton.fsState &= ~TBSTATE_CHECKED;
+ nodePrev = nodePrev->GetPrevious();
+ prevIndex--;
+ }
+ }
+
+ isRadio = true;
+ break;
+
+ case wxITEM_CHECK:
+ button.fsStyle = TBSTYLE_CHECK;
+ break;
+
+ case wxITEM_NORMAL:
+ button.fsStyle = TBSTYLE_BUTTON;
+ break;
+
+ case wxITEM_DROPDOWN:
+ button.fsStyle = TBSTYLE_DROPDOWN;
+ break;
+
+ default:
+ wxFAIL_MSG( _T("unexpected toolbar button kind") );
+ button.fsStyle = TBSTYLE_BUTTON;
+ break;
+ }
+
+ bitmapId++;
+ break;
+ }