+ ::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"));
+ }
+
+ delete [] buttons;
+
+ // Deal with the controls finally
+ // ------------------------------
+
+ // adjust the controls size to fit nicely in the toolbar
+ int y = 0;
+ size_t index = 0;
+ for ( node = m_tools.GetFirst(); node; node = node->GetNext(), index++ )
+ {
+ wxToolBarToolBase *tool = node->GetData();
+
+ // we calculate the running y coord for vertical toolbars so we need to
+ // get the items size for all items but for the horizontal ones we
+ // don't need to deal with the non controls
+ bool isControl = tool->IsControl();
+ if ( !isControl && !isVertical )
+ continue;
+
+ // 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(wxT("TB_GETITEMRECT"));
+ }
+
+ if ( !isControl )
+ {
+ // can only be control if isVertical
+ y += r.bottom - r.top;
+
+ continue;
+ }
+
+ wxControl *control = tool->GetControl();
+
+ wxSize size = control->GetSize();
+
+ // the position of the leftmost controls corner
+ int left = -1;
+
+ // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
+#ifdef TB_SETBUTTONINFO
+ // 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(wxT("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(wxT("TB_INSERTBUTTON"));
+ }
+
+ index++;
+ }
+
+ // remember the number of separators we used - we'd have to
+ // delete all of them later
+ ((wxToolBarTool *)tool)->SetSeparatorsCount(nSeparators);
+
+ // adjust the controls width to exactly cover the separators
+ control->SetSize((nSeparators + 1)*widthSep, -1);
+ }
+
+ // position the control itself correctly vertically
+ int height = r.bottom - r.top;
+ int diff = height - size.y;
+ if ( diff < 0 )
+ {
+ // the control is too high, resize to fit
+ control->SetSize(-1, height - 2);
+
+ diff = 2;
+ }
+
+ int top;
+ if ( isVertical )
+ {
+ left = 0;
+ top = y;
+
+ y += height + 2*GetMargins().y;
+ }
+ else // horizontal toolbar
+ {
+ if ( left == -1 )
+ left = r.left;
+
+ top = r.top;
+ }
+
+ control->Move(left, top + (diff + 1) / 2);
+ }
+
+ // the max index is the "real" number of buttons - i.e. counting even the
+ // separators which we added just for aligning the controls
+ m_nButtons = index;
+
+ if ( !isVertical )
+ {
+ if ( m_maxRows == 0 )
+ {
+ // if not set yet, only one row
+ SetRows(1);
+ }
+ }
+ else if ( m_nButtons > 0 ) // vertical non empty toolbar
+ {
+ if ( m_maxRows == 0 )
+ {
+ // if not set yet, have one column
+ SetRows(m_nButtons);
+ }
+ }
+
+ return TRUE;