+ m_hBitmap = (WXHBITMAP)hBitmap;
+
+ // Now blit all the tools onto this bitmap
+ HDC memoryDC = ::CreateCompatibleDC(NULL);
+ HBITMAP oldBitmap = (HBITMAP) ::SelectObject(memoryDC, hBitmap);
+
+ HDC memoryDC2 = ::CreateCompatibleDC(NULL);
+
+ // 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() )
+ {
+ HBITMAP hbmp = GetHbitmapOf(tool->GetBitmap1());
+ if ( hbmp )
+ {
+ HBITMAP oldBitmap2 = (HBITMAP)::SelectObject(memoryDC2, hbmp);
+ if ( !BitBlt(memoryDC, x, 0, m_defaultWidth, m_defaultHeight,
+ memoryDC2, 0, 0, SRCCOPY) )
+ {
+ wxLogLastError("BitBlt");
+ }
+
+ ::SelectObject(memoryDC2, oldBitmap2);
+ }
+ 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();
+ }
+
+ ::SelectObject(memoryDC, oldBitmap);
+ ::DeleteDC(memoryDC);
+ ::DeleteDC(memoryDC2);
+
+ // 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("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;
+
+ button.fsStyle = tool->CanBeToggled() ? TBSTYLE_CHECK
+ : TBSTYLE_BUTTON;
+
+ bitmapId++;
+ break;
+ }
+
+ i++;
+ }
+
+ if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS,
+ (WPARAM)i, (LPARAM)buttons) )
+ {
+ wxLogLastError("TB_ADDBUTTONS");
+ }
+
+ delete [] buttons;
+
+ // Deal with the controls finally
+ // ------------------------------
+
+ // adjust the controls size to fit nicely in the toolbar
+ size_t index = 0;
+ for ( node = m_tools.GetFirst(); node; node = node->GetNext(), index++ )
+ {
+ wxToolBarToolBase *tool = node->GetData();
+ if ( !tool->IsControl() )
+ continue;
+
+ wxControl *control = tool->GetControl();
+
+ wxSize size = control->GetSize();
+
+ // the position of the leftmost controls corner
+ int left = -1;
+
+ // 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("TB_GETITEMRECT");
+ }
+
+ // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
+ #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
+ // available in headers, now check whether it is available now
+ // (during run-time)
+ if ( wxTheApp->GetComCtl32Version() >= 471 )
+ {
+ // set the (underlying) separators width to be that of the
+ // control
+ TBBUTTONINFO tbbi;
+ tbbi.cbSize = sizeof(tbbi);
+ tbbi.dwMask = TBIF_SIZE;
+ tbbi.cx = size.x;
+ if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO,
+ tool->GetId(), (LPARAM)&tbbi) )
+ {
+ // the id is probably invalid?
+ wxLogLastError("TB_SETBUTTONINFO");
+ }
+
+ }
+ else
+ #endif // comctl32.dll 4.71
+ // TB_SETBUTTONINFO unavailable
+ {
+ // try adding several separators to fit the controls width
+ int widthSep = r.right - r.left;
+ left = r.left;
+
+ TBBUTTON tbb;
+ wxZeroMemory(tbb);
+ tbb.idCommand = 0;
+ tbb.fsState = TBSTATE_ENABLED;
+ tbb.fsStyle = TBSTYLE_SEP;
+
+ size_t nSeparators = size.x / widthSep;
+ for ( size_t nSep = 0; nSep < nSeparators; nSep++ )
+ {
+ if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON,
+ index, (LPARAM)&tbb) )
+ {
+ wxLogLastError("TB_INSERTBUTTON");
+ }