X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/98fbab9e7bf70ee8af44a78ea2a1d2cbb406880a..4e15d1caa03346c126015019c1fdf093033ef40b:/src/msw/menuitem.cpp diff --git a/src/msw/menuitem.cpp b/src/msw/menuitem.cpp index c2bfd4d26d..509f021654 100644 --- a/src/msw/menuitem.cpp +++ b/src/msw/menuitem.cpp @@ -30,6 +30,8 @@ #include "wx/stockitem.h" #ifndef WX_PRECOMP + #include "wx/app.h" + #include "wx/dcmemory.h" #include "wx/font.h" #include "wx/bitmap.h" #include "wx/settings.h" @@ -52,6 +54,10 @@ UINT GetMenuState(HMENU hMenu, UINT id, UINT flags) ; #endif +#if wxUSE_UXTHEME + #include "wx/msw/uxtheme.h" +#endif + // --------------------------------------------------------------------------- // macro // --------------------------------------------------------------------------- @@ -59,6 +65,78 @@ UINT GetMenuState(HMENU hMenu, UINT id, UINT flags) ; // hide the ugly cast #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu()) +// ---------------------------------------------------------------------------- +// helper classes for temporarily changing HDC parameters +// ---------------------------------------------------------------------------- + +namespace +{ + +// This class just stores an HDC. +class HDCHandler +{ +protected: + HDCHandler(HDC hdc) : m_hdc(hdc) { } + + const HDC m_hdc; +}; + +class HDCTextColChanger : HDCHandler +{ +public: + HDCTextColChanger(HDC hdc, COLORREF col) + : HDCHandler(hdc), + m_colOld(::SetTextColor(hdc, col)) + { + } + + ~HDCTextColChanger() + { + ::SetTextColor(m_hdc, m_colOld); + } + +private: + COLORREF m_colOld; +}; + +class HDCBgColChanger : HDCHandler +{ +public: + HDCBgColChanger(HDC hdc, COLORREF col) + : HDCHandler(hdc), + m_colOld(::SetBkColor(hdc, col)) + { + } + + ~HDCBgColChanger() + { + ::SetBkColor(m_hdc, m_colOld); + } + +private: + COLORREF m_colOld; +}; + +class HDCBgModeChanger : HDCHandler +{ +public: + HDCBgModeChanger(HDC hdc, int mode) + : HDCHandler(hdc), + m_modeOld(::SetBkMode(hdc, mode)) + { + } + + ~HDCBgModeChanger() + { + ::SetBkMode(m_hdc, m_modeOld); + } + +private: + int m_modeOld; +}; + +} // anonymous namespace + // ============================================================================ // implementation // ============================================================================ @@ -76,66 +154,312 @@ UINT GetMenuState(HMENU hMenu, UINT id, UINT flags) ; #define DSS_HIDEPREFIX 0x0200 #endif +#if wxUSE_UXTHEME + +enum MENUPARTS +{ + MENU_MENUITEM_TMSCHEMA = 1, + MENU_SEPARATOR_TMSCHEMA = 6, + MENU_POPUPBACKGROUND = 9, + MENU_POPUPBORDERS = 10, + MENU_POPUPCHECK = 11, + MENU_POPUPCHECKBACKGROUND = 12, + MENU_POPUPGUTTER = 13, + MENU_POPUPITEM = 14, + MENU_POPUPSEPARATOR = 15, + MENU_POPUPSUBMENU = 16, +}; + + +enum POPUPITEMSTATES +{ + MPI_NORMAL = 1, + MPI_HOT = 2, + MPI_DISABLED = 3, + MPI_DISABLEDHOT = 4, +}; + +enum POPUPCHECKBACKGROUNDSTATES +{ + MCB_DISABLED = 1, + MCB_NORMAL = 2, + MCB_BITMAP = 3, +}; + +enum POPUPCHECKSTATES +{ + MC_CHECKMARKNORMAL = 1, + MC_CHECKMARKDISABLED = 2, + MC_BULLETNORMAL = 3, + MC_BULLETDISABLED = 4, +}; + +const int TMT_MENUFONT = 803; +const int TMT_BORDERSIZE = 2403; +const int TMT_CONTENTMARGINS = 3602; +const int TMT_SIZINGMARGINS = 3601; + +#endif // wxUSE_UXTHEME + #endif // wxUSE_OWNER_DRAWN // ---------------------------------------------------------------------------- // dynamic classes implementation // ---------------------------------------------------------------------------- -#if wxUSE_EXTENDED_RTTI +// ---------------------------------------------------------------------------- +// wxMenuItem +// ---------------------------------------------------------------------------- + +#if wxUSE_OWNER_DRAWN + +namespace +{ + +// helper class to keep information about metrics and other stuff +// needed for measuring and drawing menu item +class MenuDrawData +{ +public: + // Wrapper around standard MARGINS structure providing some helper + // functions and automatically initializing the margin fields to 0. + struct Margins : MARGINS + { + Margins() + { + cxLeftWidth = + cxRightWidth = + cyTopHeight = + cyBottomHeight = 0; + } + + int GetTotalX() const { return cxLeftWidth + cxRightWidth; } + int GetTotalY() const { return cyTopHeight + cyBottomHeight; } + + void ApplyTo(RECT& rect) const + { + rect.top += cyTopHeight; + rect.left += cxLeftWidth; + rect.right -= cyTopHeight; + rect.bottom -= cyBottomHeight; + } + + void UnapplyFrom(RECT& rect) const + { + rect.top -= cyTopHeight; + rect.left -= cxLeftWidth; + rect.right += cyTopHeight; + rect.bottom += cyBottomHeight; + } + }; + + Margins ItemMargin; // popup item margins + + Margins CheckMargin; // popup check margins + Margins CheckBgMargin; // popup check background margins + + Margins ArrowMargin; // popup submenu arrow margins + + Margins SeparatorMargin; // popup separator margins + + SIZE CheckSize; // popup check size metric + SIZE ArrowSize; // popup submenu arrow size metric + SIZE SeparatorSize; // popup separator size metric + + int TextBorder; // popup border space between + // item text and gutter + + int AccelBorder; // popup border space between + // item text and accelerator + + int ArrowBorder; // popup border space between + // item accelerator and submenu arrow + + int Offset; // system added space at the end of the menu, + // add this offset for remove the extra space + + wxFont Font; // default menu font + + bool AlwaysShowCues; // must keyboard cues always be shown? -bool wxMenuItemStreamingCallback( const wxObject *object, wxWriter * , wxPersister * , wxxVariantArray & ) + bool Theme; // is data initialized for FullTheme? + + static const MenuDrawData* Get() + { + // notice that s_menuData can't be created as a global variable because + // it needs a window to initialize and no windows exist at the time of + // globals initialization yet + if ( !ms_instance ) + { + static MenuDrawData s_menuData; + ms_instance = &s_menuData; + } + + #if wxUSE_UXTHEME + bool theme = MenuLayout() == FullTheme; + if ( ms_instance->Theme != theme ) + ms_instance->Init(); + #endif // wxUSE_UXTHEME + return ms_instance; + } + + MenuDrawData() + { + Init(); + } + + + // get the theme engine or NULL if themes + // are not available or not supported on menu + static wxUxThemeEngine *GetUxThemeEngine() + { + #if wxUSE_UXTHEME + if ( MenuLayout() == FullTheme ) + return wxUxThemeEngine::GetIfActive(); + #endif // wxUSE_UXTHEME + return NULL; + } + + + enum MenuLayoutType + { + FullTheme, // full menu themes (Vista or new) + PseudoTheme, // pseudo menu themes (on XP) + Classic + }; + + static MenuLayoutType MenuLayout() + { + MenuLayoutType menu = Classic; + #if wxUSE_UXTHEME + if ( wxUxThemeEngine::GetIfActive() != NULL ) + { + static wxWinVersion ver = wxGetWinVersion(); + if ( ver >= wxWinVersion_Vista ) + menu = FullTheme; + else if ( ver == wxWinVersion_XP ) + menu = PseudoTheme; + } + #endif // wxUSE_UXTHEME + return menu; + } + +private: + void Init(); + + static MenuDrawData* ms_instance; +}; + +MenuDrawData* MenuDrawData::ms_instance = NULL; + +void MenuDrawData::Init() { - const wxMenuItem * mitem = dynamic_cast(object) ; - if ( mitem->GetMenu() && !mitem->GetMenu()->GetTitle().empty() ) +#if wxUSE_UXTHEME + wxUxThemeEngine* theme = GetUxThemeEngine(); + if ( theme ) { - // we don't stream out the first two items for menus with a title, they will be reconstructed - if ( mitem->GetMenu()->FindItemByPosition(0) == mitem || mitem->GetMenu()->FindItemByPosition(1) == mitem ) - return false ; + wxWindow* window = static_cast(wxApp::GetInstance())->GetTopWindow(); + wxUxThemeHandle hTheme(window, L"MENU"); + + theme->GetThemeMargins(hTheme, NULL, MENU_POPUPITEM, 0, + TMT_CONTENTMARGINS, NULL, + &ItemMargin); + + theme->GetThemeMargins(hTheme, NULL, MENU_POPUPCHECK, 0, + TMT_CONTENTMARGINS, NULL, + &CheckMargin); + theme->GetThemeMargins(hTheme, NULL, MENU_POPUPCHECKBACKGROUND, 0, + TMT_CONTENTMARGINS, NULL, + &CheckBgMargin); + + theme->GetThemeMargins(hTheme, NULL, MENU_POPUPSUBMENU, 0, + TMT_CONTENTMARGINS, NULL, + &ArrowMargin); + + theme->GetThemeMargins(hTheme, NULL, MENU_POPUPSEPARATOR, 0, + TMT_SIZINGMARGINS, NULL, + &SeparatorMargin); + + theme->GetThemePartSize(hTheme, NULL, MENU_POPUPCHECK, 0, + NULL, TS_TRUE, &CheckSize); + + theme->GetThemePartSize(hTheme, NULL, MENU_POPUPSUBMENU, 0, + NULL, TS_TRUE, &ArrowSize); + + theme->GetThemePartSize(hTheme, NULL, MENU_POPUPSEPARATOR, 0, + NULL, TS_TRUE, &SeparatorSize); + + theme->GetThemeInt(hTheme, MENU_POPUPBACKGROUND, 0, TMT_BORDERSIZE, &TextBorder); + + AccelBorder = 34; + ArrowBorder = 0; + + Offset = -14; + + wxUxThemeFont themeFont; + theme->GetThemeSysFont(hTheme, TMT_MENUFONT, themeFont.GetPtr()); + Font = wxFont(themeFont.GetLOGFONT()); + + Theme = true; + + // native menu doesn't uses the vertical margins + ItemMargin.cyTopHeight = + ItemMargin.cyBottomHeight = 0; + + // native menu uses small top margin for separator + if ( SeparatorMargin.cyTopHeight >= 2 ) + SeparatorMargin.cyTopHeight -= 2; } - return true ; -} + else +#endif // wxUSE_UXTHEME + { + const NONCLIENTMETRICS& metrics = wxMSWImpl::GetNonClientMetrics(); -wxBEGIN_ENUM( wxItemKind ) - wxENUM_MEMBER( wxITEM_SEPARATOR ) - wxENUM_MEMBER( wxITEM_NORMAL ) - wxENUM_MEMBER( wxITEM_CHECK ) - wxENUM_MEMBER( wxITEM_RADIO ) -wxEND_ENUM( wxItemKind ) - -IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuItem, wxObject,"wx/menuitem.h",wxMenuItemStreamingCallback) - -wxBEGIN_PROPERTIES_TABLE(wxMenuItem) - wxPROPERTY( Parent,wxMenu*, SetMenu, GetMenu, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) - wxPROPERTY( Id,int, SetId, GetId, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) - wxPROPERTY( Text, wxString , SetText, GetText, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) - wxPROPERTY( Help, wxString , SetHelp, GetHelp, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) - wxREADONLY_PROPERTY( Kind, wxItemKind , GetKind , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) - wxPROPERTY( SubMenu,wxMenu*, SetSubMenu, GetSubMenu, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) - wxPROPERTY( Enabled , bool , Enable , IsEnabled , wxxVariant((bool)true) , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) - wxPROPERTY( Checked , bool , Check , IsChecked , wxxVariant((bool)false) , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) - wxPROPERTY( Checkable , bool , SetCheckable , IsCheckable , wxxVariant((bool)false) , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) -wxEND_PROPERTIES_TABLE() - -wxBEGIN_HANDLERS_TABLE(wxMenuItem) -wxEND_HANDLERS_TABLE() - -wxDIRECT_CONSTRUCTOR_6( wxMenuItem , wxMenu* , Parent , int , Id , wxString , Text , wxString , Help , wxItemKind , Kind , wxMenu* , SubMenu ) -#else -IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject) -#endif + CheckMargin.cxLeftWidth = + CheckMargin.cxRightWidth = ::GetSystemMetrics(SM_CXEDGE); + CheckMargin.cyTopHeight = + CheckMargin.cyBottomHeight = ::GetSystemMetrics(SM_CYEDGE); -// ---------------------------------------------------------------------------- -// wxMenuItem -// ---------------------------------------------------------------------------- + CheckSize.cx = ::GetSystemMetrics(SM_CXMENUCHECK); + CheckSize.cy = ::GetSystemMetrics(SM_CYMENUCHECK); -#if wxUSE_OWNER_DRAWN + ArrowSize = CheckSize; -// these static variables are from the wxMenuItem object for cache -// system settings returned by the Win32 API's SystemParametersInfo() call -wxFont wxMenuItem::ms_systemMenuFont; -size_t wxMenuItem::ms_systemMenuHeight = 0; -bool wxMenuItem::ms_alwaysShowCues = false; + // separator height with margins + int sepFullSize = metrics.iMenuHeight / 2; + + SeparatorMargin.cxLeftWidth = + SeparatorMargin.cxRightWidth = 1; + SeparatorMargin.cyTopHeight = + SeparatorMargin.cyBottomHeight = sepFullSize / 2 - 1; + + SeparatorSize.cx = 1; + SeparatorSize.cy = sepFullSize - SeparatorMargin.GetTotalY(); + + TextBorder = 0; + AccelBorder = 8; + ArrowBorder = 6; + + Offset = -12; + + Font = wxFont(wxNativeFontInfo(metrics.lfMenuFont)); + + Theme = false; + } + + int value; + if ( ::SystemParametersInfo(SPI_GETKEYBOARDCUES, 0, &value, 0) == 0 ) + { + // if it's not supported, we must be on an old Windows version + // which always shows them + value = 1; + } + + AlwaysShowCues = value == 1; + +} + +} // anonymous namespace #endif // wxUSE_OWNER_DRAWN @@ -170,29 +494,8 @@ wxMenuItem::wxMenuItem(wxMenu *parentMenu, void wxMenuItem::Init() { - m_radioGroup.start = -1; - m_isRadioGroupStart = false; - #if wxUSE_OWNER_DRAWN - // init static varaibles - if ( !ms_systemMenuHeight ) - { - const NONCLIENTMETRICS& metrics = wxMSWImpl::GetNonClientMetrics(); - - ms_systemMenuFont = wxFont(wxNativeFontInfo(metrics.lfMenuFont)); - ms_systemMenuHeight = metrics.iMenuHeight; - - if ( ::SystemParametersInfo(SPI_GETKEYBOARDCUES, 0, - &ms_alwaysShowCues, 0) == 0 ) - { - // if it's not supported, we must be on an old Windows version - // which always shows them - ms_alwaysShowCues = true; - } - - } - // when the color is not valid, wxOwnerDraw takes the default ones. // If we set the colors here and they are changed by the user during // the execution, then the colors are not updated until the application @@ -251,30 +554,6 @@ bool wxMenuItem::IsChecked() const return (flag & MF_CHECKED) != 0; } -// radio group stuff -// ----------------- - -void wxMenuItem::SetAsRadioGroupStart() -{ - m_isRadioGroupStart = true; -} - -void wxMenuItem::SetRadioGroupStart(int start) -{ - wxASSERT_MSG( !m_isRadioGroupStart, - wxT("should only be called for the next radio items") ); - - m_radioGroup.start = start; -} - -void wxMenuItem::SetRadioGroupEnd(int end) -{ - wxASSERT_MSG( m_isRadioGroupStart, - wxT("should only be called for the first radio item") ); - - m_radioGroup.end = end; -} - // change item state // ----------------- @@ -328,17 +607,10 @@ void wxMenuItem::Check(bool check) int start, end; - if ( m_isRadioGroupStart ) + if ( !m_parentMenu->MSWGetRadioGroupRange(pos, &start, &end) ) { - // 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; + wxFAIL_MSG( wxT("Menu radio item not part of radio group?") ); + return; } #ifdef __WIN32__ @@ -443,7 +715,7 @@ void wxMenuItem::SetItemLabel(const wxString& txt) if ( isLaterThanWin95 ) info.fMask |= MIIM_STRING; //else: MIIM_TYPE already specified - info.dwTypeData = (LPTSTR)m_text.wx_str(); + info.dwTypeData = wxMSW_CONV_LPTSTR(m_text); info.cch = m_text.length(); if ( !::SetMenuItemInfo(hMenu, id, FALSE, &info) ) { @@ -453,6 +725,21 @@ void wxMenuItem::SetItemLabel(const wxString& txt) #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(); @@ -460,21 +747,24 @@ wxString wxMenuItem::GetName() const bool wxMenuItem::OnMeasureItem(size_t *width, size_t *height) { + const MenuDrawData* data = MenuDrawData::Get(); + if ( IsOwnerDrawn() ) { + *width = data->ItemMargin.GetTotalX(); + *height = data->ItemMargin.GetTotalY(); - wxString str = GetName(); - - // if we have a valid accel string, then pad out - // the menu string so that the menu and accel string are not - // placed on top of each other. - wxString accel = GetItemLabel().AfterFirst(wxT('\t')); - if ( !accel.empty() ) + if ( IsSeparator() ) { - str.Pad(str.length()%8); - str += accel; + *width += data->SeparatorSize.cx + + data->SeparatorMargin.GetTotalX(); + *height += data->SeparatorSize.cy + + data->SeparatorMargin.GetTotalY(); + return true; } + wxString str = GetName(); + wxMemoryDC dc; wxFont font; GetFontToUse(font); @@ -482,8 +772,16 @@ bool wxMenuItem::OnMeasureItem(size_t *width, size_t *height) wxCoord w, h; dc.GetTextExtent(str, &w, &h); - *width = w; + + *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.GetTotalX() + data->ArrowSize.cx; } else // don't draw the text, just the bitmap (if any) { @@ -491,43 +789,49 @@ bool wxMenuItem::OnMeasureItem(size_t *width, size_t *height) *height = 0; } - // increase size to accommodate bigger bitmaps if necessary - if (m_bmpChecked.Ok()) + // bitmap + + if ( IsOwnerDrawn() ) { - // Is BMP height larger than text height? - size_t adjustedHeight = m_bmpChecked.GetHeight(); - if ( *height < adjustedHeight ) - *height = adjustedHeight; + // 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.GetTotalX(); + + *width += imgWidth + data->CheckBgMargin.GetTotalX(); + } + + if ( m_bmpChecked.IsOk() || m_bmpUnchecked.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 widthBmp = wxMax(m_bmpChecked.GetWidth(), m_bmpUnchecked.GetWidth()); - const int widthBmp = m_bmpChecked.GetWidth(); if ( IsOwnerDrawn() ) { - // widen the margin to fit the bitmap if necessary - if ( GetMarginWidth() < widthBmp ) - SetMarginWidth(widthBmp); - + heightBmp += data->CheckMargin.GetTotalY(); } - else // we must allocate enough space for the bitmap + else { + // we must allocate enough space for the bitmap *width += widthBmp; } - } - - // add a 4-pixel separator, otherwise menus look cluttered - *width += 4; - // notice that this adjustment must be done after (possibly) changing the - // margin width above - if ( IsOwnerDrawn() ) - { - // add space at the end of the menu for the submenu expansion arrow - // this will also allow offsetting the accel string from the right edge - *width += GetMarginWidth() + 16; + // 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 - if ( *height < ms_systemMenuHeight ) - *height = ms_systemMenuHeight; + const size_t menuHeight = data->CheckMargin.GetTotalY() + + data->CheckSize.cy; + if (*height < menuHeight) + *height = menuHeight; return true; } @@ -535,106 +839,144 @@ bool wxMenuItem::OnMeasureItem(size_t *width, size_t *height) bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction WXUNUSED(act), wxODStatus stat) { + const MenuDrawData* data = MenuDrawData::Get(); - // this flag determines whether or not an edge will - // be drawn around the bitmap. In most "windows classic" - // applications, a 1-pixel highlight edge is drawn around - // the bitmap of an item when it is selected. However, - // with the new "luna" theme, no edge is drawn around - // the bitmap because the background is white (this applies - // only to "non-XP style" menus w/ bitmaps -- - // see IE 6 menus for an example) - - bool draw_bitmap_edge = true; + wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl(); + HDC hdc = GetHdcOf(*impl); - // set the colors - // -------------- - wxColour colText1, colBack1; - GetColourToUse(stat, colText1, colBack1); + RECT rect; + wxCopyRectToRECT(rc, rect); - DWORD colText = wxColourToPalRGB(colText1); - DWORD colBack = wxColourToPalRGB(colBack1); + int imgWidth = wxMax(GetMarginWidth(), data->CheckSize.cx); if ( IsOwnerDrawn() ) { - // don't draw an edge around the bitmap, if background is white ... - DWORD menu_bg_color = GetSysColor(COLOR_MENU); - if ( GetRValue( menu_bg_color ) >= 0xf0 && - GetGValue( menu_bg_color ) >= 0xf0 && - GetBValue( menu_bg_color ) >= 0xf0 ) - { - draw_bitmap_edge = false; - } - } - else // edge doesn't look well with default Windows drawing - { - draw_bitmap_edge = false; - } + // font and colors to use + wxFont font; + GetFontToUse(font); + wxColour colText, colBack; + GetColourToUse(stat, colText, colBack); + + // calculate metrics of item parts + RECT rcSelection = rect; + data->ItemMargin.ApplyTo(rcSelection); + + RECT rcSeparator = rcSelection; + data->SeparatorMargin.ApplyTo(rcSeparator); + + RECT rcGutter = rcSelection; + rcGutter.right = data->ItemMargin.cxLeftWidth + + data->CheckBgMargin.cxLeftWidth + + data->CheckMargin.cxLeftWidth + + imgWidth + + data->CheckMargin.cxRightWidth + + data->CheckBgMargin.cxRightWidth; + + RECT 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 + // If a custom background colour is explicitly specified, we should use + // it instead of the default theme background. + wxUxThemeEngine* const theme = GetBackgroundColour().IsOk() + ? NULL + : 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; + } - wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl(); - HDC hdc = GetHdcOf(*impl); - COLORREF colOldText = ::SetTextColor(hdc, colText); - COLORREF colOldBack = ::SetBkColor(hdc, colBack); + wxUxThemeHandle hTheme(GetMenu()->GetWindow(), L"MENU"); - // *2, as in wxSYS_EDGE_Y - int margin = GetMarginWidth() + 2 * wxSystemSettings::GetMetric(wxSYS_EDGE_X); + if ( theme->IsThemeBackgroundPartiallyTransparent(hTheme, + MENU_POPUPITEM, state) ) + { + theme->DrawThemeBackground(hTheme, hdc, + MENU_POPUPBACKGROUND, + 0, &rect, NULL); + } - // select the font and draw the text - // --------------------------------- + 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; + } - // determine where to draw and leave space for a check-mark. - // + 1 pixel to separate the edge from the highlight rectangle - int xText = rc.x + margin + 1; + theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPITEM, + state, &rcSelection, NULL); + } + else +#endif // wxUSE_UXTHEME + { + if ( IsSeparator() ) + { + DrawEdge(hdc, &rcSeparator, EDGE_ETCHED, BF_TOP); + return true; + } - // using native API because it recognizes '&' - if ( IsOwnerDrawn() ) - { - int prevMode = SetBkMode(hdc, TRANSPARENT); - AutoHBRUSH hbr(colBack); - SelectInHDC selBrush(hdc, hbr); + AutoHBRUSH hbr(colBack.GetPixel()); + SelectInHDC selBrush(hdc, hbr); + ::FillRect(hdc, &rcSelection, hbr); + } - RECT rectFill; - wxCopyRectToRECT(rc, rectFill); - if ( (stat & wxODSelected) && m_bmpChecked.Ok() && draw_bitmap_edge ) - { - // only draw the highlight under the text, not under - // the bitmap or checkmark - rectFill.left = xText; - } + // draw text label + // using native API because it recognizes '&' - ::FillRect(hdc, &rectFill, hbr); + HDCTextColChanger changeTextCol(hdc, colText.GetPixel()); + HDCBgColChanger changeBgCol(hdc, colBack.GetPixel()); + HDCBgModeChanger changeBgMode(hdc, TRANSPARENT); - // use default font if no font set - wxFont font; - GetFontToUse(font); SelectInHDC selFont(hdc, GetHfontOf(font)); - // item text name with menemonic - wxString text = GetItemLabel().BeforeFirst('\t'); - xText += 3; // separate text from the highlight rectangle + // item text name without mnemonic for calculating size + wxString text = GetName(); - SIZE textRect; - ::GetTextExtentPoint32(hdc, text.c_str(), text.length(), &textRect); + SIZE textSize; + ::GetTextExtentPoint32(hdc, text.c_str(), text.length(), &textSize); + + // item text name with mnemonic + text = GetItemLabel().BeforeFirst('\t'); int flags = DST_PREFIXTEXT; - if ( (stat & wxODDisabled) && !(stat & wxODSelected) ) + // themes menu is using specified color for disabled labels + if ( data->MenuLayout() == MenuDrawData::Classic && + (stat & wxODDisabled) && !(stat & wxODSelected) ) flags |= DSS_DISABLED; - if ( (stat & wxODHidePrefix) && !ms_alwaysShowCues ) + if ( (stat & wxODHidePrefix) && !data->AlwaysShowCues ) flags |= DSS_HIDEPREFIX; - int x = xText; - int y = rc.y + (rc.GetHeight() - textRect.cy) / 2; - int cx = rc.GetWidth() - GetMarginWidth(); - int cy = textRect.cy; + 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, cx, cy, flags); + ::DrawState(hdc, NULL, NULL, wxMSW_CONV_LPARAM(text), + 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 @@ -642,52 +984,56 @@ bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc, wxString accel = GetItemLabel().AfterFirst(wxT('\t')); if ( !accel.empty() ) { - SIZE accelRect; - ::GetTextExtentPoint32(hdc, accel.c_str(), accel.length(), &accelRect); + SIZE accelSize; + ::GetTextExtentPoint32(hdc, accel.c_str(), accel.length(), &accelSize); int flags = DST_TEXT; - if ( (stat & wxODDisabled) && !(stat & wxODSelected) ) + // themes menu is using specified color for disabled labels + if ( data->MenuLayout() == MenuDrawData::Classic && + (stat & wxODDisabled) && !(stat & wxODSelected) ) flags |= DSS_DISABLED; - // right align accel string with right edge of menu - // (offset by the margin width) + int x = rcText.right - data->ArrowMargin.GetTotalX() + - data->ArrowSize.cx + - data->ArrowBorder; + + // right align accel on FullTheme menu, left otherwise + if ( data->MenuLayout() == MenuDrawData::FullTheme) + x -= accelSize.cx; + else + x -= m_parentMenu->GetMaxAccelWidth(); - int x = rc.GetWidth() - 16 - accelRect.cx; - int y = rc.y + (rc.GetHeight() - accelRect.cy) / 2; - ::DrawState(hdc, NULL, NULL, (LPARAM)accel.wx_str(), + int y = rcText.top + (rcText.bottom - rcText.top - accelSize.cy) / 2; + + ::DrawState(hdc, NULL, NULL, wxMSW_CONV_LPARAM(accel), accel.length(), x, y, 0, 0, flags); } - - ::SetBkMode(hdc, prevMode); } // draw the bitmap - // --------------- - if ( IsCheckable() && !m_bmpChecked.Ok() ) + + RECT rcImg; + SetRect(&rcImg, + rect.left + data->ItemMargin.cxLeftWidth + + data->CheckBgMargin.cxLeftWidth + + data->CheckMargin.cxLeftWidth, + rect.top + data->ItemMargin.cyTopHeight + + data->CheckBgMargin.cyTopHeight + + data->CheckMargin.cyTopHeight, + rect.left + data->ItemMargin.cxLeftWidth + + data->CheckBgMargin.cxLeftWidth + + data->CheckMargin.cxLeftWidth + + imgWidth, + rect.bottom - data->ItemMargin.cyBottomHeight + - data->CheckBgMargin.cyBottomHeight + - data->CheckMargin.cyBottomHeight); + + if ( IsCheckable() && !m_bmpChecked.IsOk() ) { if ( stat & wxODChecked ) { - // what goes on: DrawFrameControl creates a b/w mask, - // then we copy it to screen to have right colors - - // first create a monochrome bitmap in a memory DC - HDC hdcMem = CreateCompatibleDC(hdc); - HBITMAP hbmpCheck = CreateBitmap(margin, rc.GetHeight(), 1, 1, 0); - SelectObject(hdcMem, hbmpCheck); - - // then draw a check mark into it - RECT rect = { 0, 0, margin, rc.GetHeight() }; - if ( rc.GetHeight() > 0 ) - { - ::DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK); - } - - // finally copy it to screen DC and clean up - BitBlt(hdc, rc.x, rc.y, margin, rc.GetHeight(), hdcMem, 0, 0, SRCCOPY); - - DeleteDC(hdcMem); - DeleteObject(hbmpCheck); + DrawStdCheckMark((WXHDC)hdc, &rcImg, stat); } } else @@ -699,68 +1045,201 @@ bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc, bmp = GetDisabledBitmap(); } - if ( !bmp.Ok() ) + if ( !bmp.IsOk() ) { // for not checkable bitmaps we should always use unchecked one // because their checked bitmap is not set bmp = GetBitmap(!IsCheckable() || (stat & wxODChecked)); #if wxUSE_IMAGE - if ( bmp.Ok() && stat & wxODDisabled ) + if ( bmp.IsOk() && stat & wxODDisabled ) { // we need to grey out the bitmap as we don't have any specific // disabled bitmap wxImage imgGrey = bmp.ConvertToImage().ConvertToGreyscale(); - if ( imgGrey.Ok() ) + if ( imgGrey.IsOk() ) bmp = wxBitmap(imgGrey); } #endif // wxUSE_IMAGE } - if ( bmp.Ok() ) + if ( bmp.IsOk() ) { wxMemoryDC dcMem(&dc); dcMem.SelectObjectAsSource(bmp); // center bitmap - int nBmpWidth = bmp.GetWidth(), + int nBmpWidth = bmp.GetWidth(), nBmpHeight = bmp.GetHeight(); - // there should be enough space! - wxASSERT((nBmpWidth <= rc.GetWidth()) && (nBmpHeight <= rc.GetHeight())); + int x = rcImg.left + (imgWidth - nBmpWidth) / 2; + int y = rcImg.top + (rcImg.bottom - rcImg.top - nBmpHeight) / 2; + dc.Blit(x, y, nBmpWidth, nBmpHeight, &dcMem, 0, 0, wxCOPY, true); + } + } - int heightDiff = rc.GetHeight() - nBmpHeight; - dc.Blit(rc.x + (margin - nBmpWidth) / 2, - rc.y + heightDiff / 2, - nBmpWidth, nBmpHeight, - &dcMem, 0, 0, wxCOPY, true /* use mask */); + return true; - if ( ( stat & wxODSelected ) && !( stat & wxODDisabled ) && draw_bitmap_edge ) - { - RECT rectBmp = { rc.GetLeft(), rc.GetTop(), - rc.GetLeft() + margin, - rc.GetTop() + rc.GetHeight() }; - SetBkColor(hdc, colBack); +} - DrawEdge(hdc, &rectBmp, BDR_RAISEDINNER, BF_RECT); - } +namespace +{ + +// helper function for draw coloured check mark +void DrawColorCheckMark(HDC hdc, int x, int y, int cx, int cy, HDC hdcCheckMask, int idxColor) +{ + const COLORREF colBlack = RGB(0, 0, 0); + const COLORREF colWhite = RGB(255, 255, 255); + + HDCTextColChanger changeTextCol(hdc, colBlack); + HDCBgColChanger changeBgCol(hdc, colWhite); + HDCBgModeChanger changeBgMode(hdc, TRANSPARENT); + + // memory DC for color bitmap + MemoryHDC hdcMem(hdc); + CompatibleBitmap hbmpMem(hdc, cx, cy); + SelectInHDC selMem(hdcMem, hbmpMem); + + RECT rect = { 0, 0, cx, cy }; + ::FillRect(hdcMem, &rect, ::GetSysColorBrush(idxColor)); + + const COLORREF colCheck = ::GetSysColor(idxColor); + if ( colCheck == colWhite ) + { + ::BitBlt(hdc, x, y, cx, cy, hdcCheckMask, 0, 0, MERGEPAINT); + ::BitBlt(hdc, x, y, cx, cy, hdcMem, 0, 0, SRCAND); + } + else + { + if ( colCheck != colBlack ) + { + const DWORD ROP_DSna = 0x00220326; // dest = (NOT src) AND dest + ::BitBlt(hdcMem, 0, 0, cx, cy, hdcCheckMask, 0, 0, ROP_DSna); } + + ::BitBlt(hdc, x, y, cx, cy, hdcCheckMask, 0, 0, SRCAND); + ::BitBlt(hdc, x, y, cx, cy, hdcMem, 0, 0, SRCPAINT); } +} - ::SetTextColor(hdc, colOldText); - ::SetBkColor(hdc, colOldBack); +} // anonymous namespace - return true; +void wxMenuItem::DrawStdCheckMark(WXHDC hdc_, const RECT* rc, wxODStatus stat) +{ + HDC hdc = (HDC)hdc_; + +#if wxUSE_UXTHEME + wxUxThemeEngine* theme = MenuDrawData::GetUxThemeEngine(); + if ( theme ) + { + wxUxThemeHandle hTheme(GetMenu()->GetWindow(), L"MENU"); + + const MenuDrawData* data = MenuDrawData::Get(); + + // rect for background must be without check margins + RECT rcBg = *rc; + data->CheckMargin.UnapplyFrom(rcBg); + POPUPCHECKBACKGROUNDSTATES stateCheckBg = (stat & wxODDisabled) + ? MCB_DISABLED + : MCB_NORMAL; + + theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPCHECKBACKGROUND, + stateCheckBg, &rcBg, NULL); + + POPUPCHECKSTATES stateCheck; + if ( GetKind() == wxITEM_CHECK ) + { + stateCheck = (stat & wxODDisabled) ? MC_CHECKMARKDISABLED + : MC_CHECKMARKNORMAL; + } + else + { + stateCheck = (stat & wxODDisabled) ? MC_BULLETDISABLED + : MC_BULLETNORMAL; + } + + theme->DrawThemeBackground(hTheme, hdc, MENU_POPUPCHECK, + stateCheck, rc, NULL); + } + else +#endif // wxUSE_UXTHEME + { + int cx = rc->right - rc->left; + int cy = rc->bottom - rc->top; + + // first create mask of check mark + MemoryHDC hdcMask(hdc); + MonoBitmap hbmpMask(cx, cy); + SelectInHDC selMask(hdcMask,hbmpMask); + + // then draw a check mark into it + UINT stateCheck = (GetKind() == wxITEM_CHECK) ? DFCS_MENUCHECK + : DFCS_MENUBULLET; + RECT rect = { 0, 0, cx, cy }; + ::DrawFrameControl(hdcMask, &rect, DFC_MENU, stateCheck); + + // first draw shadow if disabled + if ( (stat & wxODDisabled) && !(stat & wxODSelected) ) + { + DrawColorCheckMark(hdc, rc->left + 1, rc->top + 1, + cx, cy, hdcMask, COLOR_3DHILIGHT); + } + + // then draw a check mark + int color = COLOR_MENUTEXT; + if ( stat & wxODDisabled ) + color = COLOR_BTNSHADOW; + else if ( stat & wxODSelected ) + color = COLOR_HIGHLIGHTTEXT; + + DrawColorCheckMark(hdc, rc->left, rc->top, cx, cy, hdcMask, color); + } } void wxMenuItem::GetFontToUse(wxFont& font) const { font = GetFont(); if ( !font.IsOk() ) - font = ms_systemMenuFont; + font = MenuDrawData::Get()->Font; } +void wxMenuItem::GetColourToUse(wxODStatus stat, wxColour& colText, wxColour& colBack) const +{ +#if wxUSE_UXTHEME + wxUxThemeEngine* theme = MenuDrawData::GetUxThemeEngine(); + if ( theme ) + { + wxUxThemeHandle hTheme(GetMenu()->GetWindow(), L"MENU"); + + if ( stat & wxODDisabled) + { + wxRGBToColour(colText, theme->GetThemeSysColor(hTheme, COLOR_GRAYTEXT)); + } + else + { + colText = GetTextColour(); + if ( !colText.IsOk() ) + wxRGBToColour(colText, theme->GetThemeSysColor(hTheme, COLOR_MENUTEXT)); + } + + if ( stat & wxODSelected ) + { + wxRGBToColour(colBack, theme->GetThemeSysColor(hTheme, COLOR_HIGHLIGHT)); + } + else + { + colBack = GetBackgroundColour(); + if ( !colBack.IsOk() ) + wxRGBToColour(colBack, theme->GetThemeSysColor(hTheme, COLOR_MENU)); + } + } + else +#endif // wxUSE_UXTHEME + { + wxOwnerDrawn::GetColourToUse(stat, colText, colBack); + } +} #endif // wxUSE_OWNER_DRAWN // ----------------------------------------------------------------------------