X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/abaa31e7eab9de0b7d8d279602e809ebae7c3cad..4e15d1caa03346c126015019c1fdf093033ef40b:/src/msw/menuitem.cpp diff --git a/src/msw/menuitem.cpp b/src/msw/menuitem.cpp index bc5bebea68..509f021654 100644 --- a/src/msw/menuitem.cpp +++ b/src/msw/menuitem.cpp @@ -65,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 // ============================================================================ @@ -135,49 +207,6 @@ const int TMT_SIZINGMARGINS = 3601; // dynamic classes implementation // ---------------------------------------------------------------------------- -#if wxUSE_EXTENDED_RTTI - -bool wxMenuItemStreamingCallback( const wxObject *object, wxWriter * , wxPersister * , wxxVariantArray & ) -{ - const wxMenuItem * mitem = dynamic_cast(object) ; - if ( mitem->GetMenu() && !mitem->GetMenu()->GetTitle().empty() ) - { - // 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 ; - } - return true ; -} - -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 - // ---------------------------------------------------------------------------- // wxMenuItem // ---------------------------------------------------------------------------- @@ -367,9 +396,9 @@ void MenuDrawData::Init() Offset = -14; - wxNativeFontInfo fontInfo; - theme->GetThemeSysFont(hTheme, TMT_MENUFONT, &fontInfo.lf); - Font = wxFont(fontInfo); + wxUxThemeFont themeFont; + theme->GetThemeSysFont(hTheme, TMT_MENUFONT, themeFont.GetPtr()); + Font = wxFont(themeFont.GetLOGFONT()); Theme = true; @@ -465,9 +494,6 @@ wxMenuItem::wxMenuItem(wxMenu *parentMenu, void wxMenuItem::Init() { - m_radioGroup.start = -1; - m_isRadioGroupStart = false; - #if wxUSE_OWNER_DRAWN // when the color is not valid, wxOwnerDraw takes the default ones. @@ -528,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 // ----------------- @@ -605,17 +607,10 @@ void wxMenuItem::Check(bool check) int start, end; - if ( m_isRadioGroupStart ) - { - // we already have all information we need - start = pos; - end = m_radioGroup.end; - } - else // next radio group item + if ( !m_parentMenu->MSWGetRadioGroupRange(pos, &start, &end) ) { - // 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__ @@ -720,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) ) { @@ -810,12 +805,12 @@ bool wxMenuItem::OnMeasureItem(size_t *width, size_t *height) *width += imgWidth + data->CheckBgMargin.GetTotalX(); } - if ( m_bmpChecked.IsOk() || m_bmpChecked.IsOk() ) + 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 widthtBmp = wxMax(m_bmpChecked.GetWidth(), m_bmpUnchecked.GetWidth()); + size_t widthBmp = wxMax(m_bmpChecked.GetWidth(), m_bmpUnchecked.GetWidth()); if ( IsOwnerDrawn() ) { @@ -824,7 +819,7 @@ bool wxMenuItem::OnMeasureItem(size_t *width, size_t *height) else { // we must allocate enough space for the bitmap - *width += widthtBmp; + *width += widthBmp; } // Is BMP height larger than text height? @@ -860,11 +855,8 @@ bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc, wxFont font; GetFontToUse(font); - wxColour colText1, colBack1; - GetColourToUse(stat, colText1, colBack1); - - DWORD colText = wxColourToPalRGB(colText1); - DWORD colBack = wxColourToPalRGB(colBack1); + wxColour colText, colBack; + GetColourToUse(stat, colText, colBack); // calculate metrics of item parts RECT rcSelection = rect; @@ -890,7 +882,11 @@ bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc, rcText.top--; #if wxUSE_UXTHEME - wxUxThemeEngine* theme = MenuDrawData::GetUxThemeEngine(); + // 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; @@ -942,7 +938,7 @@ bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc, return true; } - AutoHBRUSH hbr(colBack); + AutoHBRUSH hbr(colBack.GetPixel()); SelectInHDC selBrush(hdc, hbr); ::FillRect(hdc, &rcSelection, hbr); } @@ -951,21 +947,20 @@ bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc, // draw text label // using native API because it recognizes '&' - COLORREF colOldText = ::SetTextColor(hdc, colText); - COLORREF colOldBack = ::SetBkColor(hdc, colBack); - - int prevMode = SetBkMode(hdc, TRANSPARENT); + HDCTextColChanger changeTextCol(hdc, colText.GetPixel()); + HDCBgColChanger changeBgCol(hdc, colBack.GetPixel()); + HDCBgModeChanger changeBgMode(hdc, TRANSPARENT); SelectInHDC selFont(hdc, GetHfontOf(font)); - // item text name without menemonic for calculating size + // item text name without mnemonic for calculating size wxString text = GetName(); SIZE textSize; ::GetTextExtentPoint32(hdc, text.c_str(), text.length(), &textSize); - // item text name with menemonic + // item text name with mnemonic text = GetItemLabel().BeforeFirst('\t'); int flags = DST_PREFIXTEXT; @@ -980,7 +975,7 @@ bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc, int x = rcText.left; int y = rcText.top + (rcText.bottom - rcText.top - textSize.cy) / 2; - ::DrawState(hdc, NULL, NULL, (LPARAM)text.wx_str(), + ::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 @@ -1010,13 +1005,9 @@ bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc, int y = rcText.top + (rcText.bottom - rcText.top - accelSize.cy) / 2; - ::DrawState(hdc, NULL, NULL, (LPARAM)accel.wx_str(), + ::DrawState(hdc, NULL, NULL, wxMSW_CONV_LPARAM(accel), accel.length(), x, y, 0, 0, flags); } - - ::SetBkMode(hdc, prevMode); - ::SetBkColor(hdc, colOldBack); - ::SetTextColor(hdc, colOldText); } @@ -1038,7 +1029,7 @@ bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc, - data->CheckBgMargin.cyBottomHeight - data->CheckMargin.cyBottomHeight); - if ( IsCheckable() && !m_bmpChecked.Ok() ) + if ( IsCheckable() && !m_bmpChecked.IsOk() ) { if ( stat & wxODChecked ) { @@ -1054,25 +1045,25 @@ 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); @@ -1081,9 +1072,6 @@ bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc, int nBmpWidth = bmp.GetWidth(), nBmpHeight = bmp.GetHeight(); - // there should be enough space! - wxASSERT( nBmpWidth <= imgWidth && nBmpHeight <= (rcImg.bottom - rcImg.top) ); - 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); @@ -1103,9 +1091,9 @@ void DrawColorCheckMark(HDC hdc, int x, int y, int cx, int cy, HDC hdcCheckMask, const COLORREF colBlack = RGB(0, 0, 0); const COLORREF colWhite = RGB(255, 255, 255); - COLORREF colOldText = ::SetTextColor(hdc, colBlack); - COLORREF colOldBack = ::SetBkColor(hdc, colWhite); - int prevMode = SetBkMode(hdc, TRANSPARENT); + HDCTextColChanger changeTextCol(hdc, colBlack); + HDCBgColChanger changeBgCol(hdc, colWhite); + HDCBgModeChanger changeBgMode(hdc, TRANSPARENT); // memory DC for color bitmap MemoryHDC hdcMem(hdc); @@ -1132,10 +1120,6 @@ void DrawColorCheckMark(HDC hdc, int x, int y, int cx, int cy, HDC hdcCheckMask, ::BitBlt(hdc, x, y, cx, cy, hdcCheckMask, 0, 0, SRCAND); ::BitBlt(hdc, x, y, cx, cy, hdcMem, 0, 0, SRCPAINT); } - - ::SetBkMode(hdc, prevMode); - ::SetBkColor(hdc, colOldBack); - ::SetTextColor(hdc, colOldText); } } // anonymous namespace