+ bool isVertical = (GetWindowStyle() & wxTB_VERTICAL) != 0;
+
+ // First, add the bitmap: we use one bitmap for all toolbar buttons
+ // ----------------------------------------------------------------
+
+ // if we already have a bitmap, we'll replace the existing one - otherwise
+ // we'll install a new one
+ HBITMAP oldToolBarBitmap = (HBITMAP)m_hBitmap;
+
+ int totalBitmapWidth = (int)(m_defaultWidth * nTools);
+ int totalBitmapHeight = (int)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;
+
+ HDC memoryDC = ::CreateCompatibleDC(NULL);
+ HBITMAP oldBitmap = (HBITMAP) ::SelectObject(memoryDC, hBitmap);
+
+ HDC memoryDC2 = ::CreateCompatibleDC(NULL);
+#endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
+
+ // the button position
+ wxCoord x = 0;
+
+ // the number of buttons (not separators)
+ int nButtons = 0;
+
+ wxToolBarToolsList::Node *node = m_tools.GetFirst();
+ while ( node )
+ {
+ wxToolBarToolBase *tool = node->GetData();
+ if ( tool->IsButton() )
+ {
+ const wxBitmap& bmp = tool->GetBitmap1();
+ if ( bmp.Ok() )
+ {
+#if USE_BITMAP_MASKS
+ // notice the last parameter: do use mask
+ dcAllButtons.DrawBitmap(tool->GetBitmap1(), x, 0, TRUE);
+#else // !USE_BITMAP_MASKS
+ HBITMAP hbmp = GetHbitmapOf(bmp);
+ HBITMAP oldBitmap2 = (HBITMAP)::SelectObject(memoryDC2, hbmp);
+ if ( !BitBlt(memoryDC, x, 0, m_defaultWidth, m_defaultHeight,
+ memoryDC2, 0, 0, SRCCOPY) )
+ {
+ wxLogLastError(wxT("BitBlt"));
+ }
+
+ ::SelectObject(memoryDC2, oldBitmap2);
+#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++;
+ }
+
+ node = node->GetNext();
+ }
+
+#if USE_BITMAP_MASKS
+ dcAllButtons.SelectObject(wxNullBitmap);
+
+ // don't delete this HBITMAP!
+ bitmap.SetHBITMAP(0);
+#else // !USE_BITMAP_MASKS
+ ::SelectObject(memoryDC, oldBitmap);
+ ::DeleteDC(memoryDC);
+ ::DeleteDC(memoryDC2);
+#endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
+
+ // Map to system colours
+ wxMapBitmap(hBitmap, totalBitmapWidth, totalBitmapHeight);
+
+ int bitmapId = 0;
+
+ 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;
+ }
+
+ // Now delete all the buttons
+ for ( size_t pos = 0; pos < m_nButtons; pos++ )
+ {
+ if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, 0, 0) )
+ {
+ wxLogLastError(wxT("TB_DELETEBUTTON"));
+ }
+ }
+
+ }
+
+ 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"));
+ }
+ }
+
+ // 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;
+
+ int i = 0;
+ for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
+ {
+ wxToolBarToolBase *tool = node->GetData();
+
+ // don't add separators to the vertical toolbar - looks ugly
+ if ( isVertical && tool->IsSeparator() )
+ continue;
+
+ TBBUTTON& button = buttons[i];
+
+ wxZeroMemory(button);
+
+ 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:
+ button.iBitmap = bitmapId;
+ button.idCommand = tool->GetId();
+
+ if ( tool->IsEnabled() )
+ button.fsState |= TBSTATE_ENABLED;
+ if ( tool->IsToggled() )
+ button.fsState |= TBSTATE_CHECKED;