/////////////////////////////////////////////////////////////////////////////
-// 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/msw/wrapcctl.h" // include <commctrl.h> "properly"
#include "wx/frame.h"
- #include "wx/menu.h"
#include "wx/utils.h"
#include "wx/intl.h"
#include "wx/log.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
// ----------------------------------------------------------------------------
// 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
}
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());
+ wxAcceleratorEntry *accel = wxAcceleratorEntry::Create(item->GetText());
if ( accel )
accel->m_command = item->GetId();
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
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
+ // 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
// now run-time one: MIIM_BITMAP only works under WinME/2000+
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)
- // 2. worse, Windows inverses the bitmap for the selected
+ // 2. worse, Windows inverts the bitmap for the selected
// item and this looks downright ugly
//
// so instead draw it ourselves in MSWOnDrawItem()
// case in wx API
WinStruct<MENUINFO> 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
else
#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__
pData = (wxChar*)itemText.c_str();
}
- // item might have been already inserted by InsertMenuItem() above
+ // item might have already been inserted by InsertMenuItem() above
if ( !ok )
{
if ( !::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData) )
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++ )
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
if ( IsAttached() && GetMenuBar()->IsAttached() )
{
- // otherwise, the chane won't be visible
+ // otherwise, the change won't be visible
GetMenuBar()->Refresh();
}
#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
{
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) )
{
void wxMenuBar::Refresh()
{
+ if ( IsFrozen() )
+ return;
+
wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
#if defined(WINCE_WITHOUT_COMMANDBAR)
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.
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;