X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/598ddd96f45745188c96f16757f7ceeaa0c8d70d..fc1e45d95b99c7939c61daa1853567c0bbe6aeca:/src/msw/menuitem.cpp?ds=sidebyside diff --git a/src/msw/menuitem.cpp b/src/msw/menuitem.cpp index 8b9127fe3f..cc55d41996 100644 --- a/src/msw/menuitem.cpp +++ b/src/msw/menuitem.cpp @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////////////////// -// Name: menuitem.cpp +// Name: src/msw/menuitem.cpp // Purpose: wxMenuItem implementation // Author: Vadim Zeitlin // Modified by: @@ -17,10 +17,6 @@ // headers // --------------------------------------------------------------------------- -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) - #pragma implementation "menuitem.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -30,20 +26,20 @@ #if wxUSE_MENUS +#include "wx/menuitem.h" +#include "wx/stockitem.h" + #ifndef WX_PRECOMP #include "wx/font.h" #include "wx/bitmap.h" #include "wx/settings.h" - #include "wx/font.h" #include "wx/window.h" #include "wx/accel.h" - #include "wx/menu.h" #include "wx/string.h" + #include "wx/log.h" + #include "wx/menu.h" #endif -#include "wx/menuitem.h" -#include "wx/log.h" - #if wxUSE_ACCEL #include "wx/accel.h" #endif // wxUSE_ACCEL @@ -82,7 +78,7 @@ UINT GetMenuState(HMENU hMenu, UINT id, UINT flags) ; bool wxMenuItemStreamingCallback( const wxObject *object, wxWriter * , wxPersister * , wxxVariantArray & ) { const wxMenuItem * mitem = dynamic_cast(object) ; - if ( mitem->GetMenu() && !mitem->GetMenu()->GetTitle().IsEmpty() ) + 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 ) @@ -141,6 +137,7 @@ wxMenuItem::wxMenuItem(wxMenu *pParentMenu, Init(); } +#if WXWIN_COMPATIBILITY_2_8 wxMenuItem::wxMenuItem(wxMenu *parentMenu, int id, const wxString& text, @@ -155,6 +152,7 @@ wxMenuItem::wxMenuItem(wxMenu *parentMenu, { Init(); } +#endif void wxMenuItem::Init() { @@ -162,18 +160,22 @@ void wxMenuItem::Init() m_isRadioGroupStart = false; #if wxUSE_OWNER_DRAWN - // set default menu colors - #define SYS_COLOR(c) (wxSystemSettings::GetColour(wxSYS_COLOUR_##c)) - SetTextColour(SYS_COLOR(MENUTEXT)); - SetBackgroundColour(SYS_COLOR(MENU)); + // 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 + // is restarted and our menus look bad + SetTextColour(wxNullColour); + SetBackgroundColour(wxNullColour); - #undef SYS_COLOR - - // we don't want normal items be owner-drawn + // setting default colors switched ownerdraw on: switch it off again ResetOwnerDrawn(); - // tell the owner drawing code to to show the accel string as well + // switch ownerdraw back on if using a non default margin + if ( !IsSeparator() ) + SetMarginWidth(GetMarginWidth()); + + // tell the owner drawing code to show the accel string as well SetAccelString(m_text.AfterFirst(_T('\t'))); #endif // wxUSE_OWNER_DRAWN } @@ -186,9 +188,14 @@ wxMenuItem::~wxMenuItem() // ---- // return the id for calling Win32 API functions -int wxMenuItem::GetRealId() const +WXWPARAM wxMenuItem::GetMSWId() const { - return m_subMenu ? (int)m_subMenu->GetHMenu() : GetId(); + // we must use ids in unsigned short range with Windows functions, if we + // pass ids > USHRT_MAX to them they get very confused (e.g. start + // generating WM_COMMAND messages with negative high word of wParam), so + // use the cast to ensure the id is in range + return m_subMenu ? wxPtrToUInt(m_subMenu->GetHMenu()) + : static_cast(GetId()); } // get item state @@ -198,20 +205,14 @@ bool wxMenuItem::IsChecked() const { // fix that RTTI is always getting the correct state (separators cannot be checked, but the call below // returns true - if ( GetId() == wxID_SEPARATOR ) + if ( IsSeparator() ) return false ; - int flag = ::GetMenuState(GetHMenuOf(m_parentMenu), GetId(), MF_BYCOMMAND); + int flag = ::GetMenuState(GetHMenuOf(m_parentMenu), GetMSWId(), MF_BYCOMMAND); return (flag & MF_CHECKED) != 0; } -/* static */ -wxString wxMenuItemBase::GetLabelFromText(const wxString& text) -{ - return wxStripMenuCodes(text); -} - // radio group stuff // ----------------- @@ -245,7 +246,7 @@ void wxMenuItem::Enable(bool enable) return; long rc = EnableMenuItem(GetHMenuOf(m_parentMenu), - GetRealId(), + GetMSWId(), MF_BYCOMMAND | (enable ? MF_ENABLED : MF_GRAYED)); @@ -327,27 +328,34 @@ void wxMenuItem::Check(bool check) else // check item { if ( ::CheckMenuItem(hmenu, - GetRealId(), + GetMSWId(), MF_BYCOMMAND | flags) == (DWORD)-1 ) { - wxLogLastError(wxT("CheckMenuItem")); + wxFAIL_MSG( _T("CheckMenuItem() failed, item not in the menu?") ); } } wxMenuItemBase::Check(check); } -void wxMenuItem::SetText(const wxString& text) +void wxMenuItem::SetItemLabel(const wxString& txt) { + wxString text = txt; + // don't do anything if label didn't change - if ( m_text == text ) + if ( m_text == txt ) return; - wxMenuItemBase::SetText(text); - OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text) ); + // wxMenuItemBase will do stock ID checks + wxMenuItemBase::SetItemLabel(text); + + // m_text could now be different from 'text' if we are a stock menu item, + // so use only m_text below + + OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(m_text) ); #if wxUSE_OWNER_DRAWN // tell the owner drawing code to to show the accel string as well - SetAccelString(text.AfterFirst(_T('\t'))); + SetAccelString(m_text.AfterFirst(_T('\t'))); #endif HMENU hMenu = GetHMenuOf(m_parentMenu); @@ -357,7 +365,7 @@ void wxMenuItem::SetText(const wxString& text) m_parentMenu->UpdateAccel(this); #endif // wxUSE_ACCEL - UINT id = GetRealId(); + UINT id = GetMSWId(); UINT flagsOld = ::GetMenuState(hMenu, id, MF_BYCOMMAND); if ( flagsOld == 0xFFFFFFFF ) { @@ -385,7 +393,7 @@ void wxMenuItem::SetText(const wxString& text) #endif //owner drawn { flagsOld |= MF_STRING; - data = (wxChar*) text.c_str(); + data = (wxChar*) m_text.wx_str(); } #ifdef __WXWINCE__ @@ -399,7 +407,7 @@ void wxMenuItem::SetText(const wxString& text) info.cbSize = sizeof(info); info.fMask = MIIM_TYPE; info.fType = MFT_STRING; - info.cch = text.Length(); + info.cch = m_text.length(); info.dwTypeData = (LPTSTR) data ; if ( !::SetMenuItemInfo(hMenu, id, FALSE, & info) ) {