+ // 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;
+ }
+
+ // 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() )
+ {
+ wxToolBarToolBase *tool2 = node->GetData();
+ if ( tool2->IsControl() )
+ {
+ int x;
+ wxControl *control = tool2->GetControl();
+ control->GetPosition(&x, NULL);
+ control->Move(x - width, -1);
+ }
+ }
+
+ InvalidateBestSize();
+ return TRUE;
+}
+
+bool wxToolBar::Realize()
+{
+ const size_t nTools = GetToolsCount();
+ if ( nTools == 0 )
+ {
+ // nothing to do
+ return TRUE;
+ }
+
+ const bool isVertical = HasFlag(wxTB_VERTICAL);
+
+ // 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 * nTools,
+ totalBitmapHeight = m_defaultHeight;
+
+ // Create a bitmap and copy all the tool bitmaps to it
+#if USE_BITMAP_MASKS
+ wxMemoryDC dcAllButtons;
+ wxBitmap bitmap(totalBitmapWidth, totalBitmapHeight);
+ dcAllButtons.SelectObject(bitmap);
+ dcAllButtons.SetBackground(*wxLIGHT_GREY_BRUSH);
+ dcAllButtons.Clear();
+
+ m_hBitmap = bitmap.GetHBITMAP();
+ HBITMAP hBitmap = (HBITMAP)m_hBitmap;
+#else // !USE_BITMAP_MASKS
+ HBITMAP hBitmap = ::CreateCompatibleBitmap(ScreenHDC(),
+ totalBitmapWidth,
+ totalBitmapHeight);
+ if ( !hBitmap )
+ {
+ wxLogLastError(_T("CreateCompatibleBitmap"));
+
+ return FALSE;
+ }
+
+ m_hBitmap = (WXHBITMAP)hBitmap;
+
+ MemoryHDC memoryDC;
+ SelectInHDC hdcSelector(memoryDC, hBitmap);
+
+ MemoryHDC memoryDC2;
+#endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
+
+ if (wxSystemOptions::HasOption(wxT("msw.remap")) && wxSystemOptions::GetOptionInt(wxT("msw.remap")) == 0)
+ {
+#if USE_BITMAP_MASKS
+ dcAllButtons.SelectObject(wxNullBitmap);
+#endif
+
+ // Even if we're not remapping the bitmap
+ // content, we still have to remap the background.
+ hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap,
+ totalBitmapWidth, totalBitmapHeight);
+
+#if USE_BITMAP_MASKS
+ dcAllButtons.SelectObject(bitmap);
+#endif
+ }
+
+ // the button position
+ wxCoord x = 0;
+
+ // the number of buttons (not separators)
+ int nButtons = 0;
+
+ for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
+ {
+ wxToolBarToolBase *tool = node->GetData();
+ if ( tool->IsButton() )
+ {
+ const wxBitmap& bmp = tool->GetNormalBitmap();
+ if ( bmp.Ok() )
+ {
+ int xOffset = wxMax(0, (m_defaultWidth - bmp.GetWidth())/2);
+ int yOffset = wxMax(0, (m_defaultHeight - bmp.GetHeight())/2);
+#if USE_BITMAP_MASKS
+ // notice the last parameter: do use mask
+ dcAllButtons.DrawBitmap(bmp, x+xOffset, yOffset, TRUE);
+#else // !USE_BITMAP_MASKS
+ SelectInHDC hdcSelector2(memoryDC2, GetHbitmapOf(bmp));
+ if ( !BitBlt(memoryDC,
+ x+xOffset, yOffset, m_defaultWidth, m_defaultHeight,
+ memoryDC2,
+ 0, 0, SRCCOPY) )
+ {
+ wxLogLastError(wxT("BitBlt"));
+ }
+#endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
+ }
+ else
+ {
+ wxFAIL_MSG( _T("invalid tool button bitmap") );
+ }
+
+ // 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++;
+ }
+ }
+
+#if USE_BITMAP_MASKS
+ dcAllButtons.SelectObject(wxNullBitmap);
+
+ // don't delete this HBITMAP!
+ bitmap.SetHBITMAP(0);
+#endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
+
+ if (!wxSystemOptions::HasOption(wxT("msw.remap")) || wxSystemOptions::GetOptionInt(wxT("msw.remap")) == 1)
+ {
+ // Map to system colours
+ hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap,
+ totalBitmapWidth, totalBitmapHeight);
+ }
+
+ bool addBitmap = TRUE;
+
+ if ( oldToolBarBitmap )
+ {
+#ifdef TB_REPLACEBITMAP
+ if ( wxTheApp->GetComCtl32Version() >= 400 )
+ {
+ TBREPLACEBITMAP replaceBitmap;
+ replaceBitmap.hInstOld = NULL;
+ replaceBitmap.hInstNew = NULL;
+ replaceBitmap.nIDOld = (UINT) oldToolBarBitmap;
+ replaceBitmap.nIDNew = (UINT) 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) hBitmap;
+ if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP,
+ (WPARAM) nButtons, (LPARAM)&addBitmap) == -1 )
+ {
+ wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
+ }
+ }
+ }
+
+ // 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() &&
+ wxTheApp->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)label.c_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;
+
+ tool->Toggle(TRUE);
+ }
+
+ isRadio = TRUE;
+ break;
+
+ case wxITEM_CHECK:
+ button.fsStyle = TBSTYLE_CHECK;
+ break;
+
+ default:
+ wxFAIL_MSG( _T("unexpected toolbar button kind") );
+ // fall through
+
+ case wxITEM_NORMAL:
+ button.fsStyle = TBSTYLE_BUTTON;
+ }
+
+ bitmapId++;
+ break;
+ }
+
+ lastWasRadio = isRadio;
+
+ i++;
+ }
+
+ if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS, (WPARAM)i, (LPARAM)buttons) )
+ {
+ wxLogLastError(wxT("TB_ADDBUTTONS"));
+ }