/////////////////////////////////////////////////////////////////////////////
-// Name: menu.cpp
+// Name: src/msw/menu.cpp
// Purpose: wxMenu, wxMenuBar, wxMenuItem
// Author: Julian Smart
// Modified by: Vadim Zeitlin
// 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"
#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"
#include "wx/msw/private.h"
+// include <commctrl.h> "properly"
+#include "wx/msw/wrapcctl.h"
+
#ifdef __WXWINCE__
#include <windows.h>
#include <windowsx.h>
#include <tchar.h>
#include <ole2.h>
#include <shellapi.h>
-#include <commctrl.h>
#if (_WIN32_WCE < 400) && !defined(__HANDHELDPC__)
#include <aygshell.h>
#endif
// other standard headers
#include <string.h>
+#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
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// 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;
{
wxLogLastError(wxT("SetMenuItemInfo"));
}
-#else
- wxUnusedVar(hmenu);
- wxUnusedVar(id);
#endif
}
// implementation
// ============================================================================
-#include <wx/listimpl.cpp>
+#include "wx/listimpl.cpp"
-WX_DEFINE_LIST( wxMenuInfoList ) ;
+WX_DEFINE_LIST( wxMenuInfoList )
#if wxUSE_EXTENDED_RTTI
}
// 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();
}
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 )
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 correctly
+ // while our owner-drawning code is not
+
+ // first compile-time check
+#ifdef MIIM_BITMAP
+ WinStruct<MENUITEMINFO> 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 inverses 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<MENUINFO> 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)
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 been already 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() )
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) ||
}
else
{
- if ( label.IsEmpty() )
+ if ( label.empty() )
{
// remove the title and the separator after it
if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
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) )
{
#ifdef __WIN32__
// put the title string in bold face
- if ( !m_title.IsEmpty() )
+ if ( !m_title.empty() )
{
SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle);
}
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));
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) )
{
m_titles[pos] = title;
- if ( IsAttached() )
+#if defined(WINCE_WITHOUT_COMMANDBAR)
+ if (IsAttached())
+#else
+ if (GetHmenu())
+#endif
{
int mswpos = MSWPositionForWxMenu(menuOld,pos);
}
#endif // wxUSE_ACCEL
- Refresh();
+ if (IsAttached())
+ Refresh();
}
return menuOld;
// 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);
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;
wxLogLastError(wxT("TB_INSERTBUTTON"));
return false;
}
+ wxUnusedVar(mswpos);
#else
if ( !::InsertMenu(GetHmenu(), mswpos,
MF_BYPOSITION | MF_POPUP | MF_STRING,
}
#endif // wxUSE_ACCEL
- Refresh();
+ if (IsAttached())
+ Refresh();
}
return true;
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;
}
#endif // wxUSE_ACCEL
- Refresh();
+ if (IsAttached())
+ Refresh();
}
return true;
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))
}
#endif // wxUSE_ACCEL
- Refresh();
+ if (IsAttached())
+ Refresh();
}
-
m_titles.RemoveAt(pos);
return menu;