X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/cb9eed05d6ab23bfa5ab6babbc8ce91c948b4471..c259ffdff79ed1cb412cc829a19558064ce87474:/src/msw/menu.cpp diff --git a/src/msw/menu.cpp b/src/msw/menu.cpp index 6e7201f524..f89a45c790 100644 --- a/src/msw/menu.cpp +++ b/src/msw/menu.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: menu.cpp +// Name: src/msw/menu.cpp // Purpose: wxMenu, wxMenuBar, wxMenuItem // Author: Julian Smart // Modified by: Vadim Zeitlin @@ -17,10 +17,6 @@ // headers // --------------------------------------------------------------------------- -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) - #pragma implementation "menu.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -30,9 +26,10 @@ #if wxUSE_MENUS +#include "wx/menu.h" + #ifndef WX_PRECOMP #include "wx/frame.h" - #include "wx/menu.h" #include "wx/utils.h" #include "wx/intl.h" #include "wx/log.h" @@ -62,12 +59,21 @@ // other standard headers #include +#if wxUSE_OWNER_DRAWN && defined(MIIM_BITMAP) + #include "wx/dynlib.h" +#endif + +#ifndef MNS_CHECKORBMP + #define MNS_CHECKORBMP 0x04000000 +#endif +#ifndef MIM_STYLE + #define MIM_STYLE 0x00000010 +#endif + // ---------------------------------------------------------------------------- // global variables // ---------------------------------------------------------------------------- -extern wxMenu *wxCurrentPopupMenu; - // ---------------------------------------------------------------------------- // constants // ---------------------------------------------------------------------------- @@ -80,7 +86,8 @@ static const int idMenuTitle = -3; // ---------------------------------------------------------------------------- // make the given menu item default -static void SetDefaultMenuItem(HMENU hmenu, UINT id) +static void SetDefaultMenuItem(HMENU WXUNUSED_IN_WINCE(hmenu), + UINT WXUNUSED_IN_WINCE(id)) { #ifndef __WXWINCE__ MENUITEMINFO mii; @@ -93,9 +100,6 @@ static void SetDefaultMenuItem(HMENU hmenu, UINT id) { wxLogLastError(wxT("SetMenuItemInfo")); } -#else - wxUnusedVar(hmenu); - wxUnusedVar(id); #endif } @@ -117,9 +121,9 @@ UINT GetMenuState(HMENU hMenu, UINT id, UINT flags) // implementation // ============================================================================ -#include +#include "wx/listimpl.cpp" -WX_DEFINE_LIST( wxMenuInfoList ) ; +WX_DEFINE_LIST( wxMenuInfoList ) #if wxUSE_EXTENDED_RTTI @@ -229,7 +233,7 @@ void wxMenu::Init() } // if we have a title, insert it in the beginning of the menu - if ( !m_title.IsEmpty() ) + if ( !m_title.empty() ) { Append(idMenuTitle, m_title); AppendSeparator(); @@ -298,6 +302,16 @@ void wxMenu::UpdateAccel(wxMenuItem *item) } else if ( !item->IsSeparator() ) { + // recurse upwards: we should only modify m_accels of the top level + // menus, not of the submenus as wxMenuBar doesn't look at them + // (alternative and arguable cleaner solution would be to recurse + // downwards in GetAccelCount() and CopyAccels()) + if ( GetParent() ) + { + GetParent()->UpdateAccel(item); + return; + } + // find the (new) accel for this item wxAcceleratorEntry *accel = wxGetAccelFromString(item->GetText()); if ( accel ) @@ -376,10 +390,15 @@ bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos) LPCTSTR pData = NULL; if ( pos == (size_t)-1 ) { - // append at the end - pos = ::GetMenuItemCount(GetHmenu()); + // append at the end (note that the item is already appended to + // internal data structures) + pos = GetMenuItemCount() - 1; } + // adjust position to account for the title, if any + if ( !m_title.empty() ) + pos += 2; // for the title itself and its separator + BOOL ok = false; // check if we have something more than a simple text item @@ -390,7 +409,8 @@ bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos) if ( pItem->GetBitmap().Ok() && !pItem->GetTextColour().Ok() && !pItem->GetBackgroundColour().Ok() && - !pItem->GetFont().Ok() ) + !pItem->GetFont().Ok() && + !pItem->GetBitmap(true).Ok() ) { // try to use InsertMenuItem() as it's guaranteed to look correctly // while our owner-drawning code is not @@ -400,13 +420,23 @@ bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos) WinStruct mii; // now run-time one: MIIM_BITMAP only works under WinME/2000+ - if ( wxGetWinVersion() >= wxWinVersion_5 ) + if ( wxGetWinVersion() >= wxWinVersion_98 ) { - mii.fMask = MIIM_ID | MIIM_STRING | MIIM_DATA | MIIM_BITMAP; - mii.wID = id; + mii.fMask = MIIM_STRING | MIIM_DATA | MIIM_BITMAP; mii.cch = itemText.length(); mii.dwTypeData = wx_const_cast(wxChar *, itemText.c_str()); + if (flags & MF_POPUP) + { + mii.fMask |= MIIM_SUBMENU; + mii.hSubMenu = (HMENU)pItem->GetSubMenu()->GetHMenu(); + } + else + { + mii.fMask |= MIIM_ID; + mii.wID = id; + } + // we can't pass HBITMAP directly as hbmpItem for 2 reasons: // 1. we can't draw it with transparency then (this is not // very important now but would be with themed menu bg) @@ -430,10 +460,19 @@ bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos) // case in wx API WinStruct mi; - mi.fMask = MIM_STYLE; - mi.dwStyle = MNS_CHECKORBMP; - if ( !::SetMenuInfo(GetHmenu(), &mi) ) - wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)")); + // don't call SetMenuInfo() directly, this would prevent + // the app from starting up under Windows 95/NT 4 + typedef BOOL (WINAPI *SetMenuInfo_t)(HMENU, MENUINFO *); + + wxDynamicLibrary dllUser(_T("user32")); + wxDYNLIB_FUNCTION(SetMenuInfo_t, SetMenuInfo, dllUser); + if ( pfnSetMenuInfo ) + { + mi.fMask = MIM_STYLE; + mi.dwStyle = MNS_CHECKORBMP; + if ( !(*pfnSetMenuInfo)(GetHmenu(), &mi) ) + wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)")); + } // tell the item that it's not really owner-drawn but only // needs to draw its bitmap, the rest is done by Windows @@ -633,14 +672,14 @@ size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const void wxMenu::SetTitle(const wxString& label) { - bool hasNoTitle = m_title.IsEmpty(); + bool hasNoTitle = m_title.empty(); m_title = label; HMENU hMenu = GetHmenu(); if ( hasNoTitle ) { - if ( !label.IsEmpty() ) + if ( !label.empty() ) { if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING, (unsigned)idMenuTitle, m_title) || @@ -652,7 +691,7 @@ void wxMenu::SetTitle(const wxString& label) } else { - if ( label.IsEmpty() ) + if ( label.empty() ) { // remove the title and the separator after it if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) || @@ -670,7 +709,7 @@ void wxMenu::SetTitle(const wxString& label) info.cbSize = sizeof(info); info.fMask = MIIM_TYPE; info.fType = MFT_STRING; - info.cch = m_title.Length(); + info.cch = m_title.length(); info.dwTypeData = (LPTSTR) m_title.c_str(); if ( !SetMenuItemInfo(hMenu, 0, TRUE, & info) ) { @@ -689,7 +728,7 @@ void wxMenu::SetTitle(const wxString& label) #ifdef __WIN32__ // put the title string in bold face - if ( !m_title.IsEmpty() ) + if ( !m_title.empty() ) { SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle); } @@ -836,7 +875,9 @@ WXHMENU wxMenuBar::Create() HWND hCommandBar = (HWND) GetToolBar()->GetHWND(); HMENU hMenu = (HMENU)::SendMessage(hCommandBar, SHCMBM_GETMENU, (WPARAM)0, (LPARAM)0); - if (hMenu) + + // hMenu may be zero on Windows Mobile 5. So add the menus anyway. + if (1) // (hMenu) { TBBUTTON tbButton; memset(&tbButton, 0, sizeof(TBBUTTON)); @@ -978,7 +1019,7 @@ void wxMenuBar::SetLabelTop(size_t pos, const wxString& label) info.cbSize = sizeof(info); info.fMask = MIIM_TYPE; info.fType = MFT_STRING; - info.cch = label.Length(); + info.cch = label.length(); info.dwTypeData = (LPTSTR) label.c_str(); if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, & info) ) { @@ -1016,7 +1057,11 @@ wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) m_titles[pos] = title; - if ( IsAttached() ) +#if defined(WINCE_WITHOUT_COMMANDBAR) + if (IsAttached()) +#else + if (GetHmenu()) +#endif { int mswpos = MSWPositionForWxMenu(menuOld,pos); @@ -1041,7 +1086,8 @@ wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) } #endif // wxUSE_ACCEL - Refresh(); + if (IsAttached()) + Refresh(); } return menuOld; @@ -1052,7 +1098,14 @@ bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) // Find out which MSW item before which we'll be inserting before // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu. // If IsAttached() is false this won't be used anyway - int mswpos = (!IsAttached() || (pos == m_menus.GetCount())) + bool isAttached = +#if defined(WINCE_WITHOUT_COMMANDBAR) + IsAttached(); +#else + (GetHmenu() != 0); +#endif + + int mswpos = (!isAttached || (pos == m_menus.GetCount())) ? -1 // append the menu : MSWPositionForWxMenu(GetMenu(pos),pos); @@ -1061,9 +1114,9 @@ bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) m_titles.Insert(title, pos); - if ( IsAttached() ) + if ( isAttached ) { -#if defined(WINCE_WITHOUT_COMMANDAR) +#if defined(WINCE_WITHOUT_COMMANDBAR) if (!GetToolBar()) return false; TBBUTTON tbButton; @@ -1083,6 +1136,7 @@ bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) wxLogLastError(wxT("TB_INSERTBUTTON")); return false; } + wxUnusedVar(mswpos); #else if ( !::InsertMenu(GetHmenu(), mswpos, MF_BYPOSITION | MF_POPUP | MF_STRING, @@ -1099,7 +1153,8 @@ bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) } #endif // wxUSE_ACCEL - Refresh(); + if (IsAttached()) + Refresh(); } return true; @@ -1115,9 +1170,13 @@ bool wxMenuBar::Append(wxMenu *menu, const wxString& title) m_titles.Add(title); - if ( IsAttached() ) +#if defined(WINCE_WITHOUT_COMMANDBAR) + if (IsAttached()) +#else + if (GetHmenu()) +#endif { -#if defined(WINCE_WITHOUT_COMMANDAR) +#if defined(WINCE_WITHOUT_COMMANDBAR) if (!GetToolBar()) return false; TBBUTTON tbButton; @@ -1154,7 +1213,8 @@ bool wxMenuBar::Append(wxMenu *menu, const wxString& title) } #endif // wxUSE_ACCEL - Refresh(); + if (IsAttached()) + Refresh(); } return true; @@ -1166,9 +1226,13 @@ wxMenu *wxMenuBar::Remove(size_t pos) if ( !menu ) return NULL; - if ( IsAttached() ) +#if defined(WINCE_WITHOUT_COMMANDBAR) + if (IsAttached()) +#else + if (GetHmenu()) +#endif { -#if defined(WINCE_WITHOUT_COMMANDAR) +#if defined(WINCE_WITHOUT_COMMANDBAR) if (GetToolBar()) { if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_DELETEBUTTON, (UINT) pos, (LPARAM) 0)) @@ -1191,10 +1255,10 @@ wxMenu *wxMenuBar::Remove(size_t pos) } #endif // wxUSE_ACCEL - Refresh(); + if (IsAttached()) + Refresh(); } - m_titles.RemoveAt(pos); return menu;