+ int flags = check ? MF_CHECKED : MF_UNCHECKED;
+ HMENU hmenu = GetHMenuOf(m_parentMenu);
+
+ if ( GetKind() == wxITEM_RADIO )
+ {
+ // it doesn't make sense to uncheck a radio item -- what would this
+ // do?
+ if ( !check )
+ return;
+
+ // get the index of this item in the menu
+ const wxMenuItemList& items = m_parentMenu->GetMenuItems();
+ int pos = items.IndexOf(this);
+ wxCHECK_RET( pos != wxNOT_FOUND,
+ wxT("menuitem not found in the menu items list?") );
+
+ // get the radio group range
+ int start,
+ end;
+
+ if ( m_isRadioGroupStart )
+ {
+ // we already have all information we need
+ start = pos;
+ end = m_radioGroup.end;
+ }
+ else // next radio group item
+ {
+ // get the radio group end from the start item
+ start = m_radioGroup.start;
+ end = items.Item(start)->GetData()->m_radioGroup.end;
+ }
+
+#ifdef __WIN32__
+ // calling CheckMenuRadioItem() with such parameters hangs my system
+ // (NT4 SP6) and I suspect this could happen to the others as well,
+ // so don't do it!
+ wxCHECK_RET( start != -1 && end != -1,
+ wxT("invalid ::CheckMenuRadioItem() parameter(s)") );
+
+ if ( !::CheckMenuRadioItem(hmenu,
+ start, // the first radio group item
+ end, // the last one
+ pos, // the one to check
+ MF_BYPOSITION) )
+ {
+ wxLogLastError(wxT("CheckMenuRadioItem"));
+ }
+#endif // __WIN32__
+
+ // also uncheck all the other items in this radio group
+ wxMenuItemList::compatibility_iterator node = items.Item(start);
+ for ( int n = start; n <= end && node; n++ )
+ {
+ if ( n != pos )
+ {
+ node->GetData()->m_isChecked = false;
+ }
+
+ node = node->GetNext();
+ }
+ }
+ else // check item
+ {
+ if ( ::CheckMenuItem(hmenu,
+ GetMSWId(),
+ MF_BYCOMMAND | flags) == (DWORD)-1 )
+ {
+ wxFAIL_MSG(wxT("CheckMenuItem() failed, item not in the menu?"));
+ }
+ }
+ }
+
+ wxMenuItemBase::Check(check);
+}
+
+void wxMenuItem::SetItemLabel(const wxString& txt)
+{
+ wxString text = txt;
+
+ // don't do anything if label didn't change
+ if ( m_text == txt )
+ return;
+
+ // wxMenuItemBase will do stock ID checks
+ wxMenuItemBase::SetItemLabel(text);
+
+ // the item can be not attached to any menu yet and SetItemLabel() is still
+ // valid to call in this case and should do nothing else
+ if ( !m_parentMenu )
+ return;
+
+#if wxUSE_ACCEL
+ m_parentMenu->UpdateAccel(this);
+#endif // wxUSE_ACCEL
+
+ const UINT id = GetMSWId();
+ HMENU hMenu = GetHMenuOf(m_parentMenu);
+ if ( !hMenu || ::GetMenuState(hMenu, id, MF_BYCOMMAND) == (UINT)-1 )
+ return;
+
+#if wxUSE_OWNER_DRAWN
+ if ( IsOwnerDrawn() )
+ {
+ // we don't need to do anything for owner drawn items, they will redraw
+ // themselves using the new text the next time they're displayed
+ return;
+ }
+#endif // owner drawn
+
+ // update the text of the native menu item
+ WinStruct<MENUITEMINFO> info;
+
+ // surprisingly, calling SetMenuItemInfo() with just MIIM_STRING doesn't
+ // work as it resets the menu bitmap, so we need to first get the old item
+ // state and then modify it
+ const bool isLaterThanWin95 = wxGetWinVersion() > wxWinVersion_95;
+ info.fMask = MIIM_STATE |
+ MIIM_ID |
+ MIIM_SUBMENU |
+ MIIM_CHECKMARKS |
+ MIIM_DATA;
+ if ( isLaterThanWin95 )
+ info.fMask |= MIIM_BITMAP | MIIM_FTYPE;
+ else
+ info.fMask |= MIIM_TYPE;
+ if ( !::GetMenuItemInfo(hMenu, id, FALSE, &info) )
+ {
+ wxLogLastError(wxT("GetMenuItemInfo"));
+ return;
+ }
+
+ if ( isLaterThanWin95 )
+ info.fMask |= MIIM_STRING;
+ //else: MIIM_TYPE already specified
+ info.dwTypeData = (LPTSTR)m_text.wx_str();
+ info.cch = m_text.length();
+ if ( !::SetMenuItemInfo(hMenu, id, FALSE, &info) )
+ {
+ wxLogLastError(wxT("SetMenuItemInfo"));
+ }
+}
+
+#if wxUSE_OWNER_DRAWN
+
+int wxMenuItem::MeasureAccelWidth() const
+{
+ wxString accel = GetItemLabel().AfterFirst(wxT('\t'));
+
+ wxMemoryDC dc;
+ wxFont font;
+ GetFontToUse(font);
+ dc.SetFont(font);
+
+ wxCoord w;
+ dc.GetTextExtent(accel, &w, NULL);
+
+ return w;
+}
+
+wxString wxMenuItem::GetName() const
+{
+ return GetItemLabelText();
+}
+
+bool wxMenuItem::OnMeasureItem(size_t *width, size_t *height)
+{
+ const MenuDrawData* data = MenuDrawData::Get();
+
+ if ( IsOwnerDrawn() )
+ {
+ *width = data->ItemMargin.left + data->ItemMargin.right;
+ *height = data->ItemMargin.top + data->ItemMargin.bottom;
+
+ if ( IsSeparator() )
+ {
+ *width += data->SeparatorSize.cx
+ + data->SeparatorMargin.left + data->SeparatorMargin.right;
+ *height += data->SeparatorSize.cy
+ + data->SeparatorMargin.top + data->SeparatorMargin.bottom;
+ return true;
+ }
+
+ wxString str = GetName();
+
+ wxMemoryDC dc;
+ wxFont font;
+ GetFontToUse(font);
+ dc.SetFont(font);
+
+ wxCoord w, h;
+ dc.GetTextExtent(str, &w, &h);
+
+ *width = data->TextBorder + w + data->AccelBorder;
+ *height = h;
+
+ w = m_parentMenu->GetMaxAccelWidth();
+ if ( w > 0 )
+ *width += w + data->ArrowBorder;
+
+ *width += data->Offset;
+ *width += data->ArrowMargin.left + data->ArrowSize.cx + data->ArrowMargin.right;
+ }
+ else // don't draw the text, just the bitmap (if any)
+ {
+ *width = 0;
+ *height = 0;
+ }
+
+ // bitmap
+
+ if ( IsOwnerDrawn() )
+ {
+ // width of menu icon with margins in ownerdrawn menu
+ // if any bitmap is not set, the width of space reserved for icon
+ // image is equal to the width of std check mark,
+ // if bitmap is set, then the width is set to the width of the widest
+ // bitmap in menu (GetMarginWidth()) unless std check mark is wider,
+ // then it's is set to std mark's width
+ int imgWidth = wxMax(GetMarginWidth(), data->CheckSize.cx)
+ + data->CheckMargin.left + data->CheckMargin.right;
+
+ *width += imgWidth + data->CheckBgMargin.left + data->CheckBgMargin.right;
+ }
+
+ if ( m_bmpChecked.IsOk() || m_bmpChecked.IsOk() )
+ {
+ // get size of bitmap always return valid value (0 for invalid bitmap),
+ // so we don't needed check if bitmap is valid ;)
+ size_t heightBmp = wxMax(m_bmpChecked.GetHeight(), m_bmpUnchecked.GetHeight());
+ size_t widthtBmp = wxMax(m_bmpChecked.GetWidth(), m_bmpUnchecked.GetWidth());
+
+ if ( IsOwnerDrawn() )
+ {
+ heightBmp += data->CheckMargin.top + data->CheckMargin.bottom;
+ }
+ else
+ {
+ // we must allocate enough space for the bitmap
+ *width += widthtBmp;
+ }
+
+ // Is BMP height larger than text height?
+ if ( *height < heightBmp )
+ *height = heightBmp;
+ }
+
+ // make sure that this item is at least as tall as the system menu height
+ const size_t menuHeight = data->CheckMargin.top + data->CheckMargin.bottom
+ + data->CheckSize.cy;
+ if (*height < menuHeight)
+ *height = menuHeight;
+
+ return true;
+}
+
+bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc,
+ wxODAction WXUNUSED(act), wxODStatus stat)
+{
+ const MenuDrawData* data = MenuDrawData::Get();
+
+ wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
+ HDC hdc = GetHdcOf(*impl);
+
+ RECT rect;
+ wxCopyRectToRECT(rc, rect);
+
+ int imgWidth = wxMax(GetMarginWidth(), data->CheckSize.cx);
+
+ if ( IsOwnerDrawn() )
+ {
+ // font and colors to use
+ wxFont font;
+ GetFontToUse(font);
+
+ wxColour colText1, colBack1;
+ GetColourToUse(stat, colText1, colBack1);
+
+ DWORD colText = wxColourToPalRGB(colText1);
+ DWORD colBack = wxColourToPalRGB(colBack1);
+
+ // calculate metrics of item parts
+ RECT rcSelection;
+ RECT rcSeparator;
+ RECT rcGutter;
+ RECT rcText;
+
+ SetRect(&rcSelection,
+ rect.left + data->ItemMargin.left,
+ rect.top + data->ItemMargin.top,
+ rect.right - data->ItemMargin.right,
+ rect.bottom - data->ItemMargin.bottom);
+
+ SetRect(&rcSeparator,
+ rcSelection.left + data->SeparatorMargin.left,
+ rcSelection.top + data->SeparatorMargin.top,
+ rcSelection.right - data->SeparatorMargin.right,
+ rcSelection.bottom - data->SeparatorMargin.bottom);
+
+ CopyRect(&rcGutter, &rcSelection);
+ rcGutter.right = data->ItemMargin.left
+ + data->CheckBgMargin.left
+ + data->CheckMargin.left
+ + imgWidth
+ + data->CheckMargin.right
+ + data->CheckBgMargin.right;
+
+ CopyRect(&rcText, &rcSelection);
+ rcText.left = rcGutter.right + data->TextBorder;
+
+ // we draw the text label vertically centered, but this results in it
+ // being 1px too low compared to native menus for some reason, fix it
+ if ( data->MenuLayout() != MenuDrawData::FullTheme )
+ rcText.top--;
+
+#if wxUSE_UXTHEME
+ wxUxThemeEngine* theme = MenuDrawData::GetUxThemeEngine();
+ if ( theme )
+ {
+ POPUPITEMSTATES state;
+ if ( stat & wxODDisabled )
+ {
+ state = (stat & wxODSelected) ? MPI_DISABLEDHOT
+ : MPI_DISABLED;
+ }
+ else if ( stat & wxODSelected )
+ {
+ state = MPI_HOT;
+ }
+ else
+ {
+ state = MPI_NORMAL;
+ }
+
+ wxUxThemeHandle hTheme(GetMenu()->GetWindow(), L"MENU");
+
+ if ( theme->IsThemeBackgroundPartiallyTransparent(hTheme,
+ MENU_POPUPITEM, state) )
+ {
+ theme->DrawThemeBackground(hTheme, hdc,
+ MENU_POPUPBACKGROUND,
+ 0, &rect, NULL);
+ }
+
+ theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPGUTTER,
+ 0, &rcGutter, NULL);
+
+ if ( IsSeparator() )
+ {
+ rcSeparator.left = rcGutter.right;
+ theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPSEPARATOR,
+ 0, &rcSeparator, NULL);
+ return true;
+ }
+
+ theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPITEM,
+ state, &rcSelection, NULL);
+
+ }
+ else
+#endif // wxUSE_UXTHEME
+ {
+ if ( IsSeparator() )
+ {
+ DrawEdge(hdc, &rcSeparator, EDGE_ETCHED, BF_TOP);
+ return true;
+ }
+
+ AutoHBRUSH hbr(colBack);
+ SelectInHDC selBrush(hdc, hbr);
+ ::FillRect(hdc, &rcSelection, hbr);
+ }
+
+
+ // draw text label
+ // using native API because it recognizes '&'
+
+ COLORREF colOldText = ::SetTextColor(hdc, colText);
+ COLORREF colOldBack = ::SetBkColor(hdc, colBack);
+
+ int prevMode = SetBkMode(hdc, TRANSPARENT);
+
+ SelectInHDC selFont(hdc, GetHfontOf(font));
+
+
+ // item text name without menemonic for calculating size
+ wxString text = GetName();
+
+ SIZE textSize;
+ ::GetTextExtentPoint32(hdc, text.c_str(), text.length(), &textSize);
+
+ // item text name with menemonic
+ text = GetItemLabel().BeforeFirst('\t');
+
+ int flags = DST_PREFIXTEXT;
+ // themes menu is using specified color for disabled labels
+ if ( data->MenuLayout() == MenuDrawData::Classic &&
+ (stat & wxODDisabled) && !(stat & wxODSelected) )
+ flags |= DSS_DISABLED;
+
+ if ( (stat & wxODHidePrefix) && !data->AlwaysShowCues )
+ flags |= DSS_HIDEPREFIX;
+
+ int x = rcText.left;
+ int y = rcText.top + (rcText.bottom - rcText.top - textSize.cy) / 2;
+
+ ::DrawState(hdc, NULL, NULL, (LPARAM)text.wx_str(),
+ text.length(), x, y, 0, 0, flags);
+
+ // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
+ // as the last parameter in DrawState() (at least with Windows98). So we have
+ // to take care of right alignment ourselves.
+ wxString accel = GetItemLabel().AfterFirst(wxT('\t'));
+ if ( !accel.empty() )
+ {
+ SIZE accelSize;
+ ::GetTextExtentPoint32(hdc, accel.c_str(), accel.length(), &accelSize);
+
+ int flags = DST_TEXT;
+ // themes menu is using specified color for disabled labels
+ if ( data->MenuLayout() == MenuDrawData::Classic &&
+ (stat & wxODDisabled) && !(stat & wxODSelected) )
+ flags |= DSS_DISABLED;
+
+ int x = rcText.right - data->ArrowMargin.left
+ - data->ArrowSize.cx
+ - data->ArrowMargin.right
+ - data->ArrowBorder;
+
+ // right align accel on FullTheme menu, left otherwise
+ if ( data->MenuLayout() == MenuDrawData::FullTheme)
+ x -= accelSize.cx;
+ else
+ x -= m_parentMenu->GetMaxAccelWidth();
+
+ int y = rcText.top + (rcText.bottom - rcText.top - accelSize.cy) / 2;
+
+ ::DrawState(hdc, NULL, NULL, (LPARAM)accel.wx_str(),
+ accel.length(), x, y, 0, 0, flags);
+ }
+
+ ::SetBkMode(hdc, prevMode);
+ ::SetBkColor(hdc, colOldBack);
+ ::SetTextColor(hdc, colOldText);