X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3519d946766951cb946803f1eb763da62b0c5f8f..475edb11e0aa7f103478da219253d99db480d55b:/src/msw/menu.cpp diff --git a/src/msw/menu.cpp b/src/msw/menu.cpp index 37d22056e1..156e4e4035 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" @@ -44,13 +41,15 @@ #include "wx/msw/private.h" +// include "properly" +#include "wx/msw/wrapcctl.h" + #ifdef __WXWINCE__ #include #include #include #include #include -#include #if (_WIN32_WCE < 400) && !defined(__HANDHELDPC__) #include #endif @@ -62,12 +61,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 +88,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 +102,6 @@ static void SetDefaultMenuItem(HMENU hmenu, UINT id) { wxLogLastError(wxT("SetMenuItemInfo")); } -#else - wxUnusedVar(hmenu); - wxUnusedVar(id); #endif } @@ -117,9 +123,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 +235,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 +304,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 ) @@ -370,57 +386,142 @@ bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos) id = pItem->GetId(); } -#ifdef __WXWINCE__ - wxString strippedString; -#endif - LPCTSTR pData; + // prepare to insert the item in the menu + wxString itemText = pItem->GetText(); + LPCTSTR pData = NULL; + if ( pos == (size_t)-1 ) + { + // 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 #if wxUSE_OWNER_DRAWN - if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages? - // item draws itself, pass pointer to it in data parameter - flags |= MF_OWNERDRAW; - pData = (LPCTSTR)pItem; + if ( pItem->IsOwnerDrawn() ) + { + // is the item owner-drawn just because of the bitmap? + if ( pItem->GetBitmap().Ok() && + !pItem->GetTextColour().Ok() && + !pItem->GetBackgroundColour().Ok() && + !pItem->GetFont().Ok() && + !pItem->GetBitmap(true).Ok() ) + { + // try to use InsertMenuItem() as it's guaranteed to look correct + // while our owner-drawn code is not + + // first compile-time check +#ifdef MIIM_BITMAP + WinStruct mii; + + // now run-time one: MIIM_BITMAP only works under WinME/2000+ + if ( wxGetWinVersion() >= wxWinVersion_98 ) + { + 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) + // 2. worse, Windows inverts the bitmap for the selected + // item and this looks downright ugly + // + // so instead draw it ourselves in MSWOnDrawItem() + mii.dwItemData = wx_reinterpret_cast(ULONG_PTR, pItem); + mii.hbmpItem = HBMMENU_CALLBACK; + + ok = ::InsertMenuItem(GetHmenu(), pos, TRUE /* by pos */, &mii); + if ( !ok ) + { + wxLogLastError(wxT("InsertMenuItem()")); + } + else // InsertMenuItem() ok + { + // we need to remove the extra indent which is reserved for + // the checkboxes by default as it looks ugly unless check + // boxes are used together with bitmaps and this is not the + // case in wx API + WinStruct mi; + + // 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 + pItem->ResetOwnerDrawn(); + } + } +#endif // MIIM_BITMAP + } + + if ( !ok ) + { + // item draws itself, pass pointer to it in data parameter + flags |= MF_OWNERDRAW; + pData = (LPCTSTR)pItem; + } } else -#endif +#endif // wxUSE_OWNER_DRAWN { - // menu is just a normal string (passed in data parameter) + // item is just a normal string (passed in data parameter) flags |= MF_STRING; #ifdef __WXWINCE__ - strippedString = wxStripMenuCodes(pItem->GetText()); - pData = (wxChar*)strippedString.c_str(); -#else - pData = (wxChar*)pItem->GetText().c_str(); + itemText = wxMenuItem::GetLabelFromText(itemText); #endif - } - BOOL ok; - if ( pos == (size_t)-1 ) - { - ok = ::AppendMenu(GetHmenu(), flags, id, pData); - } - else - { - ok = ::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData); + pData = (wxChar*)itemText.c_str(); } + // item might have already been inserted by InsertMenuItem() above if ( !ok ) { - wxLogLastError(wxT("Insert or AppendMenu")); + if ( !::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData) ) + { + wxLogLastError(wxT("InsertMenu[Item]()")); - return false; + return false; + } } + // if we just appended the title, highlight it -#ifdef __WIN32__ if ( (int)id == idMenuTitle ) { // visually select the menu title SetDefaultMenuItem(GetHmenu(), id); } -#endif // __WIN32__ // if we're already attached to the menubar, we must update it if ( IsAttached() && GetMenuBar()->IsAttached() ) @@ -504,7 +605,7 @@ wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item) wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) { - // we need to find the items position in the child list + // we need to find the item's position in the child list size_t pos; wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst(); for ( pos = 0; node; pos++ ) @@ -515,7 +616,7 @@ wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) node = node->GetNext(); } - // DoRemove() (unlike Remove) can only be called for existing item! + // DoRemove() (unlike Remove) can only be called for an existing item! wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") ); #if wxUSE_ACCEL @@ -538,7 +639,7 @@ wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) if ( IsAttached() && GetMenuBar()->IsAttached() ) { - // otherwise, the chane won't be visible + // otherwise, the change won't be visible GetMenuBar()->Refresh(); } @@ -552,7 +653,7 @@ wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) #if wxUSE_ACCEL -// create the wxAcceleratorEntries for our accels and put them into provided +// create the wxAcceleratorEntries for our accels and put them into the provided // array - return the number of accels we have size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const { @@ -573,14 +674,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) || @@ -592,7 +693,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) || @@ -610,7 +711,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) ) { @@ -629,7 +730,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); } @@ -646,7 +747,7 @@ bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id) if ( id != (WXWORD)idMenuTitle ) { // get the checked status of the command: notice that menuState is the - // old state of the menu, so the test for MF_CHECKED must be inversed + // old state of the menu, so the test for MF_CHECKED must be inverted UINT menuState = ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND); SendEvent(id, !(menuState & MF_CHECKED)); } @@ -746,6 +847,9 @@ wxMenuBar::~wxMenuBar() void wxMenuBar::Refresh() { + if ( IsFrozen() ) + return; + wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") ); #if defined(WINCE_WITHOUT_COMMANDBAR) @@ -763,7 +867,7 @@ void wxMenuBar::Refresh() WXHMENU wxMenuBar::Create() { - // Note: this totally doesn't work on Smartphone, + // Note: this doesn't work at all on Smartphone, // since you have to use resources. // We'll have to find another way to add a menu // by changing/adding menu items to an existing menu. @@ -776,7 +880,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)); @@ -918,7 +1024,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) ) { @@ -956,7 +1062,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); @@ -981,7 +1091,8 @@ wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) } #endif // wxUSE_ACCEL - Refresh(); + if (IsAttached()) + Refresh(); } return menuOld; @@ -992,7 +1103,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); @@ -1001,9 +1119,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; @@ -1023,6 +1141,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, @@ -1039,7 +1158,8 @@ bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) } #endif // wxUSE_ACCEL - Refresh(); + if (IsAttached()) + Refresh(); } return true; @@ -1055,9 +1175,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; @@ -1094,7 +1218,8 @@ bool wxMenuBar::Append(wxMenu *menu, const wxString& title) } #endif // wxUSE_ACCEL - Refresh(); + if (IsAttached()) + Refresh(); } return true; @@ -1106,9 +1231,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)) @@ -1131,10 +1260,10 @@ wxMenu *wxMenuBar::Remove(size_t pos) } #endif // wxUSE_ACCEL - Refresh(); + if (IsAttached()) + Refresh(); } - m_titles.RemoveAt(pos); return menu;