// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
+#ifdef __GNUG__
+ #pragma implementation "menu.h"
+#endif
+
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
+ #include "wx/app.h"
#include "wx/frame.h"
#include "wx/menu.h"
#include "wx/utils.h"
#include "wx/intl.h"
+ #include "wx/log.h"
#endif
#if wxUSE_OWNER_DRAWN
#endif
#include "wx/os2/private.h"
-#include "wx/menu.h"
-#include "wx/menuitem.h"
-#include "wx/log.h"
// other standard headers
#include <string.h>
// global variables
// ----------------------------------------------------------------------------
-extern wxMenu *wxCurrentPopupMenu;
+extern wxMenu* wxCurrentPopupMenu;
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
-// the (popup) menu title has this special id
-static const int idMenuTitle = -2;
+//
+// The (popup) menu title has this special id
+//
+static const int idMenuTitle = -2;
+
+//
+// The unique ID for Menus
+//
+USHORT wxMenu::m_nextMenuId = 0;
// ----------------------------------------------------------------------------
// macros
// ----------------------------------------------------------------------------
-#if !USE_SHARED_LIBRARY
IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler)
-#endif
+
+// ----------------------------------------------------------------------------
+// static function for translating menu labels
+// ----------------------------------------------------------------------------
+
+static wxString TextToLabel(
+ const wxString& rsTitle
+)
+{
+ wxString sTitle = "";
+ const wxChar* zPc;
+
+ if (rsTitle.IsEmpty())
+ return sTitle;
+ for (zPc = rsTitle.c_str(); *zPc != wxT('\0'); zPc++ )
+ {
+ if (*zPc == wxT('&') )
+ {
+ if (*(zPc + 1) == wxT('&'))
+ {
+ zPc++;
+ sTitle << wxT('&');
+ }
+ else
+ sTitle << wxT('~');
+ }
+ else
+ {
+ if ( *zPc == wxT('~') )
+ {
+ //
+ // Tildes must be doubled to prevent them from being
+ // interpreted as accelerator character prefix by PM ???
+ //
+ sTitle << *zPc;
+ }
+ sTitle << *zPc;
+ }
+ }
+ return sTitle;
+} // end of TextToLabel
// ============================================================================
// implementation
// wxMenu construction, adding and removing menu items
// ---------------------------------------------------------------------------
+//
// Construct a menu with optional title (then use append)
-void wxMenu::Init(const wxString& title, const wxFunction func )
+//
+void wxMenu::Init()
{
- m_title = title;
- m_parent = NULL;
- m_eventHandler = this;
- m_pInvokingWindow = NULL;
- m_doBreak = FALSE ;
- m_noItems = 0;
- m_menuBar = NULL;
- m_hMenu = 0; //(WXHMENU) CreatePopupMenu();
- m_savehMenu = 0 ;
- m_topLevelMenu = this;
- m_clientData = (void*) NULL;
-
- if ( !!m_title )
+ m_bDoBreak = FALSE;
+ m_nStartRadioGroup = -1;
+
+ //
+ // Create the menu (to be used as a submenu or a popup)
+ //
+ if ((m_hMenu = ::WinCreateWindow( HWND_DESKTOP
+ ,WC_MENU
+ ,"Menu"
+ ,0L
+ ,0L
+ ,0L
+ ,0L
+ ,0L
+ ,NULLHANDLE
+ ,HWND_TOP
+ ,0L
+ ,NULL
+ ,NULL
+ )) == 0)
{
- Append(idMenuTitle, m_title) ;
- AppendSeparator() ;
+ wxLogLastError("WinLoadMenu");
}
+ m_vMenuData.iPosition = 0;
+ m_vMenuData.afStyle = MIS_SUBMENU | MIS_TEXT;
+ m_vMenuData.afAttribute = (USHORT)0;
+ m_vMenuData.id = m_nextMenuId++;
+ m_vMenuData.hwndSubMenu = m_hMenu;
+ m_vMenuData.hItem = NULLHANDLE;
+
+ //
+ // If we have a title, insert it in the beginning of the menu
+ //
+ if (!m_title.IsEmpty())
+ {
+ Append( idMenuTitle
+ ,m_title
+ ,wxEmptyString
+ ,wxITEM_NORMAL
+ );
+ AppendSeparator();
+ }
+} // end of wxMenu::Init
- Callback(func);
-}
-
+//
// The wxWindow destructor will take care of deleting the submenus.
+//
wxMenu::~wxMenu()
{
- // free Windows resources
- if ( m_hMenu )
+ //
+ // We should free PM resources only if PM doesn't do it for us
+ // which happens if we're attached to a menubar or a submenu of another
+ // menu
+ if (!IsAttached() && !GetParent())
{
-// ::DestroyMenu((HMENU)m_hMenu);
- m_hMenu = 0;
+ if (!::WinDestroyWindow((HWND)GetHmenu()) )
+ {
+ wxLogLastError("WinDestroyWindow");
+ }
}
- // delete submenus
- wxNode *node = m_menuItems.First();
- while ( node )
- {
- wxMenuItem *item = (wxMenuItem *)node->Data();
-
- // Delete child menus.
- // Beware: they must not be appended to children list!!!
- // (because order of delete is significant)
- if ( item->IsSubMenu() )
- item->DeleteSubMenu();
-
- wxNode *next = node->Next();
- delete item;
- delete node;
- node = next;
- }
-}
+#if wxUSE_ACCEL
+ //
+ // Delete accels
+ //
+ WX_CLEAR_ARRAY(m_vAccels);
+#endif // wxUSE_ACCEL
+} // end of wxMenu::~wxMenu
void wxMenu::Break()
{
- m_doBreak = TRUE;
-}
+ // this will take effect during the next call to Append()
+ m_bDoBreak = TRUE;
+} // end of wxMenu::Break
-// function appends a new item or submenu to the menu
-void wxMenu::Append(wxMenuItem *pItem)
+void wxMenu::Attach(
+ wxMenuBarBase* pMenubar
+)
{
- wxCHECK_RET( pItem != NULL, wxT("can't append NULL item to the menu") );
+ wxMenuBase::Attach(pMenubar);
+ EndRadioGroup();
+} // end of wxMenu::Break;
#if wxUSE_ACCEL
- // check for accelerators: they are given after '\t'
- wxString label = pItem->GetName();
- int posTab = label.Find(wxT('\t'));
- if ( posTab != wxNOT_FOUND ) {
- // parse the accelerator string
- int keyCode = 0;
- int accelFlags = wxACCEL_NORMAL;
- wxString current;
- for ( size_t n = (size_t)posTab + 1; n < label.Len(); n++ ) {
- if ( (label[n] == '+') || (label[n] == '-') ) {
- if ( current == _("ctrl") )
- accelFlags |= wxACCEL_CTRL;
- else if ( current == _("alt") )
- accelFlags |= wxACCEL_ALT;
- else if ( current == _("shift") )
- accelFlags |= wxACCEL_SHIFT;
- else {
- wxLogDebug(wxT("Unknown accel modifier: '%s'"),
- current.c_str());
- }
-
- current.Empty();
- }
- else {
- current += wxTolower(label[n]);
- }
+
+int wxMenu::FindAccel(
+ int nId
+) const
+{
+ size_t n;
+ size_t nCount = m_vAccels.GetCount();
+
+ for (n = 0; n < nCount; n++)
+ if (m_vAccels[n]->m_command == nId)
+ return n;
+ return wxNOT_FOUND;
+} // end of wxMenu::FindAccel
+
+void wxMenu::UpdateAccel(
+ wxMenuItem* pItem
+)
+{
+ if (pItem->IsSubMenu())
+ {
+ wxMenu* pSubmenu = pItem->GetSubMenu();
+ wxMenuItemList::Node* pNode = pSubmenu->GetMenuItems().GetFirst();
+
+ while (pNode)
+ {
+ UpdateAccel(pNode->GetData());
+ pNode = pNode->GetNext();
}
+ }
+ else if (!pItem->IsSeparator())
+ {
+ //
+ // Find the (new) accel for this item
+ //
+ wxAcceleratorEntry* pAccel = wxGetAccelFromString(pItem->GetText());
+
+ if (pAccel)
+ pAccel->m_command = pItem->GetId();
- if ( current.IsEmpty() ) {
- wxLogDebug(wxT("No accel key found, accel string ignored."));
+ //
+ // Find the old one
+ //
+ size_t n = FindAccel(pItem->GetId());
+
+ if (n == wxNOT_FOUND)
+ {
+ //
+ // No old, add new if any
+ //
+ if (pAccel)
+ m_vAccels.Add(pAccel);
+ else
+ return;
}
- else {
- if ( current.Len() == 1 ) {
- // it's a letter
- keyCode = wxToupper(current[0U]);
- }
- else {
- // it should be a function key
- if ( current[0U] == 'f' && isdigit(current[1U]) &&
- (current.Len() == 2 ||
- (current.Len() == 3 && isdigit(current[2U]))) ) {
- int n;
- wxSscanf(current.c_str() + 1, wxT("%d"), &n);
-
- keyCode = VK_F1 + n - 1;
- }
- else {
- wxLogDebug(wxT("Unrecognized accel key '%s', accel "
- "string ignored."), current.c_str());
- }
- }
+ else
+ {
+ //
+ // Replace old with new or just remove the old one if no new
+ //
+ delete m_vAccels[n];
+ if (pAccel)
+ m_vAccels[n] = pAccel;
+ else
+ m_vAccels.RemoveAt(n);
}
- if ( keyCode ) {
- // do add an entry
- m_accelKeyCodes.Add(keyCode);
- m_accelFlags.Add(accelFlags);
- m_accelIds.Add(pItem->GetId());
+ if (IsAttached())
+ {
+ m_menuBar->RebuildAccelTable();
}
}
+} // wxMenu::UpdateAccel
+
#endif // wxUSE_ACCEL
- UINT flags = 0;
+//
+// Append a new item or submenu to the menu
+//
+bool wxMenu::DoInsertOrAppend(
+ wxMenuItem* pItem
+, size_t nPos
+)
+{
+ wxMenu* pSubmenu = pItem->GetSubMenu();
+ MENUITEM& rItem = (pSubmenu != NULL)?pSubmenu->m_vMenuData:
+ pItem->m_vMenuData;
+
+ ERRORID vError;
+ wxString sError;
+ char zMsg[128];
+
+#if wxUSE_ACCEL
+ UpdateAccel(pItem);
+#endif // wxUSE_ACCEL
- // TODO:
-/*
- // if "Break" has just been called, insert a menu break before this item
+ //
+ // If "Break" has just been called, insert a menu break before this item
// (and don't forget to reset the flag)
- if ( m_doBreak ) {
- flags |= MF_MENUBREAK;
- m_doBreak = FALSE;
+ //
+ if (m_bDoBreak)
+ {
+ rItem.afStyle |= MIS_BREAK;
+ m_bDoBreak = FALSE;
}
- if ( pItem->IsSeparator() ) {
- flags |= MF_SEPARATOR;
+ if (pItem->IsSeparator())
+ {
+ rItem.afStyle |= MIS_SEPARATOR;
}
- // id is the numeric id for normal menu items and HMENU for submenus as
- // required by ::AppendMenu() API
- UINT id;
- wxMenu *submenu = pItem->GetSubMenu();
- if ( submenu != NULL ) {
- wxASSERT( submenu->GetHMenu() != (WXHMENU) NULL );
+ //
+ // Id is the numeric id for normal menu items and HMENU for submenus as
+ // required by ::MM_INSERTITEM message API
+ //
- id = (UINT)submenu->GetHMenu();
- submenu->m_topLevelMenu = m_topLevelMenu;
- submenu->m_parent = this;
- submenu->m_savehMenu = (WXHMENU)id;
- submenu->m_hMenu = 0;
+ if (pSubmenu != NULL)
+ {
+ wxASSERT_MSG(pSubmenu->GetHMenu(), wxT("invalid submenu"));
+ pSubmenu->SetParent(this);
- flags |= MF_POPUP;
+ rItem.iPosition = 0; // submenus have a 0 position
+ rItem.id = (USHORT)pSubmenu->GetHMenu();
+ rItem.afStyle |= MIS_SUBMENU | MIS_TEXT;
}
- else {
- id = pItem->GetId();
+ else
+ {
+ rItem.id = pItem->GetId();
}
- const char* pData;
+ BYTE* pData;
#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 = (const char*)pItem;
+ if (pItem->IsOwnerDrawn())
+ {
+ //
+ // Want to get {Measure|Draw}Item messages?
+ // item draws itself, passing pointer to data doesn't work in OS/2
+ // Will eventually need to set the image handle somewhere into vItem.hItem
+ //
+ rItem.afStyle |= MIS_OWNERDRAW;
+ pData = (BYTE*)NULL;
+ rItem.hItem = (HBITMAP)pItem->GetBitmap().GetHBITMAP();
+ pItem->m_vMenuData.afStyle = rItem.afStyle;
+ pItem->m_vMenuData.hItem = rItem.hItem;
}
else
#endif
{
- // menu is just a normal string (passed in data parameter)
- flags |= MF_STRING;
- pData = label;
+ //
+ // Menu is just a normal string (passed in data parameter)
+ //
+ rItem.afStyle |= MIS_TEXT;
+ pData = (char*)pItem->GetText().c_str();
}
- if ( !::AppendMenu(GetHmenu(), flags, id, pData) )
+
+ if (nPos == (size_t)-1)
{
- wxLogLastError("AppendMenu");
+ rItem.iPosition = MIT_END;
}
else
{
- if ( id == idMenuTitle )
- {
- // visually select the menu title
- MENUITEMINFO mii;
- mii.cbSize = sizeof(mii);
- mii.fMask = MIIM_STATE;
- mii.fState = MFS_DEFAULT;
+ rItem.iPosition = nPos;
+ }
- if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id, FALSE, &mii) )
- {
- wxLogLastError(wxT("SetMenuItemInfo"));
- }
+ APIRET rc;
+
+ rc = (APIRET)::WinSendMsg( GetHmenu()
+ ,MM_INSERTITEM
+ ,(MPARAM)&rItem
+ ,(MPARAM)pData
+ );
+#if wxUSE_OWNER_DRAWN
+ if (pItem->IsOwnerDrawn())
+ {
+ BOOL rc;
+ MENUITEM vMenuItem;
+
+ ::WinSendMsg( GetHmenu()
+ ,MM_QUERYITEM
+ ,MPFROM2SHORT( (USHORT)pItem->GetId()
+ ,(USHORT)(FALSE)
+ )
+ ,&vMenuItem
+ );
+ }
+#endif
+ if (rc == MIT_MEMERROR || rc == MIT_ERROR)
+ {
+ vError = ::WinGetLastError(vHabmain);
+ sError = wxPMErrorToStr(vError);
+ wxLogError("Error inserting or appending a menuitem. Error: %s\n", sError.c_str());
+ wxLogLastError("Insert or AppendMenu");
+ return FALSE;
+ }
+ else
+ {
+ //
+ // If we're already attached to the menubar, we must update it
+ //
+ if (IsAttached() && m_menuBar->IsAttached())
+ {
+ m_menuBar->Refresh();
}
- m_menuItems.Append(pItem);
- m_noItems++;
+ return TRUE;
}
-*/
-}
+ return FALSE;
+} // end of wxMenu::DoInsertOrAppend
-void wxMenu::AppendSeparator()
+void wxMenu::EndRadioGroup()
{
- Append(new wxMenuItem(this, ID_SEPARATOR));
-}
-
-// Pullright item
-void wxMenu::Append(int id,
- const wxString& label,
- wxMenu *SubMenu,
- const wxString& helpString)
+ //
+ // We're not inside a radio group any longer
+ //
+ m_nStartRadioGroup = -1;
+} // end of wxMenu::EndRadioGroup
+
+bool wxMenu::DoAppend(
+ wxMenuItem* pItem
+)
{
- Append(new wxMenuItem(this, id, label, helpString, FALSE, SubMenu));
-}
-
-// Ordinary menu item
-void wxMenu::Append(int id,
- const wxString& label,
- const wxString& helpString,
- bool checkable)
-{
- // 'checkable' parameter is useless for Windows.
- Append(new wxMenuItem(this, id, label, helpString, checkable));
-}
+ wxCHECK_MSG( pItem, FALSE, _T("NULL item in wxMenu::DoAppend") );
-// delete item by id
-void wxMenu::Delete(int id)
-{
- wxMenuItem *item = NULL;
- int pos;
- wxNode *node;
- for (pos = 0, node = m_menuItems.First(); node; node = node->Next(), pos++)
+ bool bCheck = FALSE;
+
+ if (pItem->GetKind() == wxITEM_RADIO)
{
- item = (wxMenuItem *)node->Data();
- if ( item->GetId() == id )
- break;
- }
+ int nCount = GetMenuItemCount();
- wxCHECK_RET( node, wxT("wxMenu::Delete(): item doesn't exist") );
+ if (m_nStartRadioGroup == -1)
+ {
+ //
+ // Start a new radio group
+ //
+ m_nStartRadioGroup = nCount;
+
+ //
+ // For now it has just one element
+ //
+ pItem->SetAsRadioGroupStart();
+ pItem->SetRadioGroupEnd(m_nStartRadioGroup);
+
+ //
+ // Ensure that we have a checked item in the radio group
+ //
+ bCheck = TRUE;
+ }
+ else // extend the current radio group
+ {
+ //
+ // We need to update its end item
+ //
+ pItem->SetRadioGroupStart(m_nStartRadioGroup);
- HMENU menu = GetHmenu();
+ wxMenuItemList::Node* pNode = GetMenuItems().Item(m_nStartRadioGroup);
- wxMenu *pSubMenu = item->GetSubMenu();
- if ( pSubMenu != NULL )
+ if (pNode)
+ {
+ pNode->GetData()->SetRadioGroupEnd(nCount);
+ }
+ else
+ {
+ wxFAIL_MSG( _T("where is the radio group start item?") );
+ }
+ }
+ }
+ else // not a radio item
{
-// RemoveMenu(menu, (UINT)pos, MF_BYPOSITION);
- pSubMenu->m_hMenu = pSubMenu->m_savehMenu;
- pSubMenu->m_savehMenu = 0;
- pSubMenu->m_parent = NULL;
- // RemoveChild(item->subMenu);
- pSubMenu->m_topLevelMenu = NULL;
- // TODO: Why isn't subMenu deleted here???
- // Will put this in for now. Assuming this is supposed
- // to delete the menu, not just remove it.
- item->DeleteSubMenu();
+ EndRadioGroup();
}
-// else
-// {
-// DeleteMenu(menu, (UINT)pos, MF_BYPOSITION);
-// }
-
- m_menuItems.DeleteNode(node);
- delete item;
-}
-
-#if wxUSE_ACCEL
-// ---------------------------------------------------------------------------
-// accelerator helpers
-// ---------------------------------------------------------------------------
-
-// create the wxAcceleratorEntries for our accels and put them into provided
-// array - return the number of accels we have
-size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
-{
- size_t count = GetAccelCount();
- for ( size_t n = 0; n < count; n++ )
+ if (!wxMenuBase::DoAppend(pItem) || !DoInsertOrAppend(pItem))
{
- (*accels++).Set(m_accelFlags[n], m_accelKeyCodes[n], m_accelIds[n]);
+ return FALSE;
}
+ if (bCheck)
+ {
+ //
+ // Check the item initially
+ //
+ pItem->Check(TRUE);
+ }
+ return TRUE;
+} // end of wxMenu::DoAppend
- return count;
-}
-
-#endif // wxUSE_ACCEL
-
-// ---------------------------------------------------------------------------
-// wxMenu functions implemented in wxMenuItem
-// ---------------------------------------------------------------------------
-
-void wxMenu::Enable(int id, bool Flag)
+bool wxMenu::DoInsert(
+ size_t nPos
+, wxMenuItem* pItem
+)
{
- wxMenuItem *item = FindItemForId(id);
- wxCHECK_RET( item != NULL, wxT("can't enable non-existing menu item") );
-
- item->Enable(Flag);
-}
-
-bool wxMenu::IsEnabled(int id) const
+ return ( wxMenuBase::DoInsert( nPos
+ ,pItem) &&
+ DoInsertOrAppend( pItem
+ ,nPos
+ )
+ );
+} // end of wxMenu::DoInsert
+
+wxMenuItem* wxMenu::DoRemove(
+ wxMenuItem* pItem
+)
{
- wxMenuItem *item = FindItemForId(id);
- wxCHECK_MSG( item != NULL, FALSE, wxT("invalid item id") );
+ //
+ // We need to find the items position in the child list
+ //
+ size_t nPos;
+ wxMenuItemList::Node* pNode = GetMenuItems().GetFirst();
- return item->IsEnabled();
-}
-
-void wxMenu::Check(int id, bool Flag)
-{
- wxMenuItem *item = FindItemForId(id);
- wxCHECK_RET( item != NULL, wxT("can't get status of non-existing menu item") );
+ for (nPos = 0; pNode; nPos++)
+ {
+ if (pNode->GetData() == pItem)
+ break;
+ pNode = pNode->GetNext();
+ }
- item->Check(Flag);
-}
+ //
+ // DoRemove() (unlike Remove) can only be called for existing item!
+ //
+ wxCHECK_MSG(pNode, NULL, wxT("bug in wxMenu::Remove logic"));
-bool wxMenu::IsChecked(int id) const
-{
- wxMenuItem *item = FindItemForId(id);
- wxCHECK_MSG( item != NULL, FALSE, wxT("invalid item id") );
+#if wxUSE_ACCEL
+ //
+ // Remove the corresponding accel from the accel table
+ //
+ int n = FindAccel(pItem->GetId());
- return item->IsChecked();
-}
+ if (n != wxNOT_FOUND)
+ {
+ delete m_vAccels[n];
+ m_vAccels.RemoveAt(n);
+ }
-void wxMenu::SetLabel(int id, const wxString& label)
-{
- wxMenuItem *item = FindItemForId(id) ;
- wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") );
+#endif // wxUSE_ACCEL
+ //
+ // Remove the item from the menu
+ //
+ ::WinSendMsg( GetHmenu()
+ ,MM_REMOVEITEM
+ ,MPFROM2SHORT(pItem->GetId(), TRUE)
+ ,(MPARAM)0
+ );
+ if (IsAttached() && m_menuBar->IsAttached())
+ {
+ //
+ // Otherwise, the chane won't be visible
+ //
+ m_menuBar->Refresh();
+ }
- item->SetName(label);
-}
+ //
+ // And from internal data structures
+ //
+ return wxMenuBase::DoRemove(pItem);
+} // end of wxMenu::DoRemove
-wxString wxMenu::GetLabel(int id) const
-{
- wxString label;
- wxMenuItem *pItem = FindItemForId(id) ;
- if (pItem)
- label = pItem->GetName() ;
- else
- wxFAIL_MSG(wxT("wxMenu::GetLabel: item doesn't exist"));
+// ---------------------------------------------------------------------------
+// accelerator helpers
+// ---------------------------------------------------------------------------
- return label;
-}
+#if wxUSE_ACCEL
-void wxMenu::SetHelpString(int itemId, const wxString& helpString)
+//
+// Create the wxAcceleratorEntries for our accels and put them into provided
+// array - return the number of accels we have
+//
+size_t wxMenu::CopyAccels(
+ wxAcceleratorEntry* pAccels
+) const
{
- wxMenuItem *item = FindItemForId (itemId);
- if (item)
- item->SetHelp(helpString);
- else
- wxFAIL_MSG(wxT("wxMenu::SetHelpString: item doesn't exist"));
-}
+ size_t nCount = GetAccelCount();
-wxString wxMenu::GetHelpString (int itemId) const
-{
- wxString help;
- wxMenuItem *item = FindItemForId (itemId);
- if (item)
- help = item->GetHelp();
- else
- wxFAIL_MSG(wxT("wxMenu::GetHelpString: item doesn't exist"));
+ for (size_t n = 0; n < nCount; n++)
+ {
+ *pAccels++ = *m_vAccels[n];
+ }
+ return nCount;
+} // end of wxMenu::CopyAccels
- return help;
-}
+#endif // wxUSE_ACCEL
// ---------------------------------------------------------------------------
-// wxMenu title
+// set wxMenu title
// ---------------------------------------------------------------------------
-void wxMenu::SetTitle(const wxString& label)
+void wxMenu::SetTitle(
+ const wxString& rLabel
+)
{
- bool hasNoTitle = m_title.IsEmpty();
- m_title = label;
+ bool bHasNoTitle = m_title.IsEmpty();
+ HWND hMenu = GetHmenu();
- HMENU hMenu = GetHmenu();
-// TODO
-/*
- if ( hasNoTitle )
+ m_title = rLabel;
+ if (bHasNoTitle)
{
- if ( !label.IsEmpty() )
+ if (!rLabel.IsEmpty())
{
- if ( !InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
- (unsigned)idMenuTitle, m_title) ||
- !InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
+ if (!::WinSetWindowText(hMenu, rLabel.c_str()))
{
- wxLogLastError(wxT("InsertMenu"));
+ wxLogLastError("SetMenuTitle");
}
}
}
else
{
- if ( label.IsEmpty() )
+ if (rLabel.IsEmpty() )
{
- // remove the title and the separator after it
- if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
- !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
- {
- wxLogLastError("RemoveMenu");
- }
+ ::WinSendMsg( GetHmenu()
+ ,MM_REMOVEITEM
+ ,MPFROM2SHORT(hMenu, TRUE)
+ ,(MPARAM)0
+ );
}
else
{
- // modify the title
- if ( !ModifyMenu(hMenu, 0u,
- MF_BYPOSITION | MF_STRING,
- (unsigned)idMenuTitle, m_title) )
+ //
+ // Modify the title
+ //
+ if (!::WinSetWindowText(hMenu, rLabel.c_str()))
{
- wxLogLastError("ModifyMenu");
+ wxLogLastError("SetMenuTitle");
}
}
}
-
- // put the title string in bold face
- if ( !m_title.IsEmpty() )
- {
- MENUITEMINFO mii;
- mii.cbSize = sizeof(mii);
- mii.fMask = MIIM_STATE;
- mii.fState = MFS_DEFAULT;
-
- if ( !SetMenuItemInfo(hMenu, (unsigned)idMenuTitle, FALSE, &mii) )
- {
- wxLogLastError("SetMenuItemInfo");
- }
- }
-*/
-}
-
-const wxString wxMenu::GetTitle() const
-{
- return m_title;
-}
+} // end of wxMenu::SetTitle
// ---------------------------------------------------------------------------
// event processing
// ---------------------------------------------------------------------------
-bool wxMenu::OS2Command(WXUINT WXUNUSED(param), WXWORD id)
+bool wxMenu::OS2Command(
+ WXUINT WXUNUSED(uParam)
+, WXWORD vId
+)
{
- // ignore commands from the menu title
+ //
+ // Ignore commands from the menu title
+ //
- if ( id != (WXWORD)idMenuTitle )
+ if (vId != (WXWORD)idMenuTitle)
{
- wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED);
- event.SetEventObject( this );
- event.SetId( id );
- event.SetInt( id );
- ProcessCommand(event);
+ SendEvent( vId
+ ,(int)::WinSendMsg( GetHmenu()
+ ,MM_QUERYITEMATTR
+ ,(MPARAM)vId
+ ,(MPARAM)MIA_CHECKED
+ )
+ );
}
-
return TRUE;
-}
-
-bool wxMenu::ProcessCommand(wxCommandEvent & event)
-{
- bool processed = FALSE;
-
- // Try a callback
- if (m_callback)
- {
- (void)(*(m_callback))(*this, event);
- processed = TRUE;
- }
-
- // Try the menu's event handler
- if ( !processed && GetEventHandler())
- {
- processed = GetEventHandler()->ProcessEvent(event);
- }
-
- // Try the window the menu was popped up from (and up through the
- // hierarchy)
- wxWindow *win = GetInvokingWindow();
- if ( !processed && win )
- processed = win->GetEventHandler()->ProcessEvent(event);
-
- return processed;
-}
+} // end of wxMenu::OS2Command
// ---------------------------------------------------------------------------
-// Item search
+// other
// ---------------------------------------------------------------------------
-// Finds the item id matching the given string, -1 if not found.
-int wxMenu::FindItem (const wxString& itemString) const
+wxWindow* wxMenu::GetWindow() const
{
- wxString itemLabel = wxStripMenuCodes(itemString);
- for ( wxNode *node = m_menuItems.First(); node; node = node->Next() )
- {
- wxMenuItem *item = (wxMenuItem *)node->Data();
- if ( item->IsSubMenu() )
- {
- int ans = item->GetSubMenu()->FindItem(itemString);
- if ( ans != wxNOT_FOUND )
- return ans;
- }
- else if ( !item->IsSeparator() )
- {
- wxString label = wxStripMenuCodes(item->GetName());
- if ( itemLabel == label )
- return item->GetId();
- }
- }
-
- return wxNOT_FOUND;
-}
+ if (m_invokingWindow != NULL)
+ return m_invokingWindow;
+ else if ( m_menuBar != NULL)
+ return m_menuBar->GetFrame();
-wxMenuItem *wxMenu::FindItemForId(int itemId, wxMenu ** itemMenu) const
+ return NULL;
+} // end of wxMenu::GetWindow
+
+// recursive search for item by id
+wxMenuItem* wxMenu::FindItem(
+ int nItemId
+, ULONG hItem
+, wxMenu** ppItemMenu
+) const
{
- if ( itemMenu )
- *itemMenu = NULL;
+ if ( ppItemMenu )
+ *ppItemMenu = NULL;
+
+ wxMenuItem* pItem = NULL;
- wxMenuItem *item = NULL;
- for ( wxNode *node = m_menuItems.First(); node && !item; node = node->Next() )
+ for ( wxMenuItemList::Node *node = m_items.GetFirst();
+ node && !pItem;
+ node = node->GetNext() )
{
- item = (wxMenuItem *)node->Data();
+ pItem = node->GetData();
- if ( item->GetId() == itemId )
+ if ( pItem->GetId() == nItemId && pItem->m_vMenuData.hItem == hItem)
{
- if (itemMenu)
- *itemMenu = (wxMenu *)this;
+ if ( ppItemMenu )
+ *ppItemMenu = (wxMenu *)this;
}
- else if ( item->IsSubMenu() )
+ else if ( pItem->IsSubMenu() )
{
- item = item->GetSubMenu()->FindItemForId(itemId, itemMenu);
+ pItem = pItem->GetSubMenu()->FindItem( nItemId
+ ,hItem
+ ,ppItemMenu
+ );
+ if (pItem)
+ break;
}
else
{
// don't exit the loop
- item = NULL;
+ pItem = NULL;
}
}
-
- return item;
-}
-
-// ---------------------------------------------------------------------------
-// other
-// ---------------------------------------------------------------------------
-
-void wxMenu::Attach(wxMenuBar *menubar)
-{
- // menu can be in at most one menubar because otherwise they would both
- // delete the menu pointer
- wxASSERT_MSG( !m_menuBar, wxT("menu belongs to 2 menubars, expect a crash") );
-
- m_menuBar = menubar;
- m_savehMenu = m_hMenu;
- m_hMenu = 0;
-}
-
-void wxMenu::Detach()
-{
- wxASSERT_MSG( m_menuBar, wxT("can't detach menu if it's not attached") );
-
- m_hMenu = m_savehMenu;
- m_savehMenu = 0;
-}
+ return pItem;
+} // end of wxMenu::FindItem
// ---------------------------------------------------------------------------
// Menu Bar
void wxMenuBar::Init()
{
m_eventHandler = this;
- m_menuCount = 0;
- m_menus = NULL;
- m_titles = NULL;
m_menuBarFrame = NULL;
m_hMenu = 0;
-}
+} // end of wxMenuBar::Init
wxMenuBar::wxMenuBar()
{
Init();
-}
+} // end of wxMenuBar::wxMenuBar
-wxMenuBar::wxMenuBar( long WXUNUSED(style) )
+wxMenuBar::wxMenuBar(
+ long WXUNUSED(lStyle)
+)
{
Init();
-}
+} // end of wxMenuBar::wxMenuBar
-wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[])
+wxMenuBar::wxMenuBar(
+ int nCount
+, wxMenu* vMenus[]
+, const wxString sTitles[]
+)
{
Init();
- m_menuCount = count;
- m_menus = menus;
- m_titles = new wxString[count];
-
- int i;
- for ( i = 0; i < count; i++ )
- m_titles[i] = titles[i];
-
- for ( i = 0; i < count; i++ )
- m_menus[i]->Attach(this);
-}
+ m_titles.Alloc(nCount);
+ for ( int i = 0; i < nCount; i++ )
+ {
+ m_menus.Append(vMenus[i]);
+ m_titles.Add(sTitles[i]);
+ vMenus[i]->Attach(this);
+ }
+} // end of wxMenuBar::wxMenuBar
wxMenuBar::~wxMenuBar()
{
- for ( int i = 0; i < m_menuCount; i++ )
+ //
+ // We should free PM's resources only if PM doesn't do it for us
+ // which happens if we're attached to a frame
+ //
+ if (m_hMenu && !IsAttached())
{
- delete m_menus[i];
+ ::WinDestroyWindow((HMENU)m_hMenu);
+ m_hMenu = (WXHMENU)NULL;
}
-
- delete[] m_menus;
- delete[] m_titles;
-}
+} // end of wxMenuBar::~wxMenuBar
// ---------------------------------------------------------------------------
// wxMenuBar helpers
void wxMenuBar::Refresh()
{
- wxCHECK_RET( m_menuBarFrame, wxT("can't refresh a menubar withotu a frame") );
+ wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
-// TODO DrawMenuBar((HWND)m_menuBarFrame->GetHWND()) ;
-}
+ WinSendMsg(GetWinHwnd(m_menuBarFrame), WM_UPDATEFRAME, (MPARAM)FCF_MENU, (MPARAM)0);
+} // end of wxMenuBar::Refresh
WXHMENU wxMenuBar::Create()
{
- if (m_hMenu != 0 )
- return m_hMenu;
+ MENUITEM vItem;
+ HWND hFrame;
- wxCHECK_MSG( !m_hMenu, TRUE, wxT("menubar already created") );
+ if (m_hMenu != 0 )
+ return m_hMenu;
- m_hMenu = 0; // TODO: (WXHMENU)::CreateMenu();
+ wxCHECK_MSG(!m_hMenu, TRUE, wxT("menubar already created"));
- if ( !m_hMenu )
+ //
+ // Menubars should be associated with a frame otherwise they are popups
+ //
+ if (m_menuBarFrame != NULL)
+ hFrame = GetWinHwnd(m_menuBarFrame);
+ else
+ hFrame = HWND_DESKTOP;
+ //
+ // Create an empty menu and then fill it with insertions
+ //
+ if ((m_hMenu = ::WinCreateWindow( hFrame
+ ,WC_MENU
+ ,(PSZ)NULL
+ ,MS_ACTIONBAR | WS_SYNCPAINT | WS_VISIBLE
+ ,0L
+ ,0L
+ ,0L
+ ,0L
+ ,hFrame
+ ,HWND_TOP
+ ,FID_MENU
+ ,NULL
+ ,NULL
+ )) == 0)
{
- wxLogLastError("CreateMenu");
+ wxLogLastError("WinLoadMenu");
}
else
{
- for ( int i = 0; i < m_menuCount; i++ )
- {
-// if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
-// (UINT)m_menus[i]->GetHMenu(),
-// m_titles[i]) )
-// {
-// wxLogLastError("AppendMenu");
-// }
- }
- }
-
- return m_hMenu;
-}
-
-// ---------------------------------------------------------------------------
-// wxMenuBar functions forwarded to wxMenuItem
-// ---------------------------------------------------------------------------
-
-// Must only be used AFTER menu has been attached to frame,
-// otherwise use individual menus to enable/disable items
-void wxMenuBar::Enable(int id, bool enable)
-{
- wxMenu *itemMenu = NULL;
- wxMenuItem *item = FindItemForId(id, &itemMenu) ;
-
- wxCHECK_RET( item, wxT("attempt to enable an item which doesn't exist") );
-
- item->Enable(enable);
-}
-
-void wxMenuBar::EnableTop(int pos, bool enable)
-{
- int flag = 0; // TODO enable ? MF_ENABLED : MF_GRAYED;;
-
-// EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag);
-}
-
-// Must only be used AFTER menu has been attached to frame,
-// otherwise use individual menus
-void wxMenuBar::Check(int id, bool check)
-{
- wxMenu *itemMenu = NULL;
- wxMenuItem *item = FindItemForId(id, &itemMenu) ;
-
- wxCHECK_RET( item, wxT("attempt to check an item which doesn't exist") );
- wxCHECK_RET( item->IsCheckable(), wxT("attempt to check an uncheckable item") );
-
- item->Check(check);
-}
-
-bool wxMenuBar::IsChecked(int id) const
-{
- wxMenu *itemMenu = NULL;
- wxMenuItem *item = FindItemForId(id, &itemMenu) ;
-
- wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsChecked(): no such item") );
-
-// int flag = ::GetMenuState(GetHmenuOf(itemMenu), id, MF_BYCOMMAND);
-
-// return (flag & MF_CHECKED) != 0;
- return FALSE;
-}
-
-bool wxMenuBar::IsEnabled(int id) const
-{
- wxMenu *itemMenu = NULL;
- wxMenuItem *item = FindItemForId(id, &itemMenu) ;
-
- wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsEnabled(): no such item") );
-
-// int flag = ::GetMenuState(GetHmenuOf(itemMenu), id, MF_BYCOMMAND) ;
-
- // don't "and" with MF_ENABLED because its value is 0
-// return (flag & MF_DISABLED) == 0;
- return FALSE;
-}
-
-void wxMenuBar::SetLabel(int id, const wxString& label)
-{
- wxMenu *itemMenu = NULL;
- wxMenuItem *item = FindItemForId(id, &itemMenu) ;
+ size_t nCount = GetMenuCount();
- wxCHECK_RET( item, wxT("wxMenuBar::SetLabel(): no such item") );
-
- item->SetName(label);
-}
-
-wxString wxMenuBar::GetLabel(int id) const
-{
- wxMenu *itemMenu = NULL;
- wxMenuItem *item = FindItemForId(id, &itemMenu) ;
-
- wxCHECK_MSG( item, wxT(""), wxT("wxMenuBar::GetLabel(): no such item") );
-
- return item->GetName();
-}
-
-void wxMenuBar::SetHelpString (int id, const wxString& helpString)
-{
- wxMenu *itemMenu = NULL;
- wxMenuItem *item = FindItemForId(id, &itemMenu) ;
-
- wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString(): no such item") );
-
- item->SetHelp(helpString);
-}
+ for (size_t i = 0; i < nCount; i++)
+ {
+ APIRET rc;
+ ERRORID vError;
+ wxString sError;
+ HWND hSubMenu;
+
+ //
+ // Set the parent and owner of the submenues to be the menubar, not the desktop
+ //
+ hSubMenu = m_menus[i]->m_vMenuData.hwndSubMenu;
+ if (!::WinSetParent(m_menus[i]->m_vMenuData.hwndSubMenu, m_hMenu, FALSE))
+ {
+ vError = ::WinGetLastError(vHabmain);
+ sError = wxPMErrorToStr(vError);
+ wxLogError("Error setting parent for submenu. Error: %s\n", sError.c_str());
+ return NULLHANDLE;
+ }
-wxString wxMenuBar::GetHelpString (int id) const
-{
- wxMenu *itemMenu = NULL;
- wxMenuItem *item = FindItemForId(id, &itemMenu) ;
+ if (!::WinSetOwner(m_menus[i]->m_vMenuData.hwndSubMenu, m_hMenu))
+ {
+ vError = ::WinGetLastError(vHabmain);
+ sError = wxPMErrorToStr(vError);
+ wxLogError("Error setting parent for submenu. Error: %s\n", sError.c_str());
+ return NULLHANDLE;
+ }
- wxCHECK_MSG( item, wxT(""), wxT("wxMenuBar::GetHelpString(): no such item") );
+ m_menus[i]->m_vMenuData.iPosition = i;
- return item->GetHelp();
-}
+ rc = (APIRET)::WinSendMsg(m_hMenu, MM_INSERTITEM, (MPARAM)&m_menus[i]->m_vMenuData, (MPARAM)m_titles[i].c_str());
+ if (rc == MIT_MEMERROR || rc == MIT_ERROR)
+ {
+ vError = ::WinGetLastError(vHabmain);
+ sError = wxPMErrorToStr(vError);
+ wxLogError("Error inserting or appending a menuitem. Error: %s\n", sError.c_str());
+ return NULLHANDLE;
+ }
+ }
+ }
+ return m_hMenu;
+} // end of wxMenuBar::Create
// ---------------------------------------------------------------------------
// wxMenuBar functions to work with the top level submenus
// ---------------------------------------------------------------------------
+//
// NB: we don't support owner drawn top level items for now, if we do these
// functions would have to be changed to use wxMenuItem as well
-
-void wxMenuBar::SetLabelTop(int pos, const wxString& label)
+//
+void wxMenuBar::EnableTop(
+ size_t nPos
+, bool bEnable
+)
{
- UINT id;
- UINT flagsOld = 0; // TODO: ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION);
- if ( flagsOld == 0xFFFFFFFF )
- {
- wxLogLastError(wxT("GetMenuState"));
+ wxCHECK_RET(IsAttached(), wxT("doesn't work with unattached menubars"));
+ USHORT uFlag = 0;
+ SHORT nId;
+
+ if(!bEnable)
+ uFlag = MIA_DISABLED;
+ nId = SHORT1FROMMR(::WinSendMsg((HWND)m_hMenu, MM_ITEMIDFROMPOSITION, MPFROMSHORT(nPos), (MPARAM)0));
+ if (nId == MIT_ERROR)
+ {
+ wxLogLastError("LogLastError");
return;
}
+ ::WinSendMsg((HWND)m_hMenu, MM_SETITEMATTR, MPFROM2SHORT(nId, TRUE), MPFROM2SHORT(MIA_DISABLED, uFlag));
+ Refresh();
+} // end of wxMenuBar::EnableTop
-// if ( flagsOld & MF_POPUP )
-// {
-// // HIBYTE contains the number of items in the submenu in this case
-// flagsOld &= 0xff ;
-// id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos) ;
-// }
-// else
-// {
-// id = pos;
-// }
-
-// if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld,
-// id, label) == 0xFFFFFFFF )
-// {
-// wxLogLastError("ModifyMenu");
-// }
-}
-
-wxString wxMenuBar::GetLabelTop(int pos) const
+void wxMenuBar::SetLabelTop(
+ size_t nPos
+, const wxString& rLabel
+)
{
- int len = 0; // TODO: ::GetMenuString((HMENU)m_hMenu, pos, NULL, 0, MF_BYCOMMAND);
-
- len++; // for the NUL character
- wxString label;
-// ::GetMenuString(GetHmenu(), pos, label.GetWriteBuf(len), len, MF_BYCOMMAND);
- label.UngetWriteBuf();
-
- return label;
-}
-
-// ---------------------------------------------------------------------------
-// wxMenuBar notifications
-// ---------------------------------------------------------------------------
+ SHORT nId;
+ MENUITEM vItem;
-bool wxMenuBar::OnDelete(wxMenu *a_menu, int pos)
-{
- if ( !m_menuBarFrame )
- return TRUE;
+ wxCHECK_RET(nPos < GetMenuCount(), wxT("invalid menu index"));
+ m_titles[nPos] = rLabel;
- // TODO
-/*
- if ( ::RemoveMenu((HMENU)m_hMenu, (UINT)pos, MF_BYPOSITION) )
+ if (!IsAttached())
{
- // VZ: I'm not sure about what's going on here, so I leave an assert
- wxASSERT_MSG( m_menus[pos] == a_menu, wxT("what is this parameter for??") );
-
- a_menu->Detach();
-
- if ( m_menuBarFrame )
- Refresh();
+ return;
+ }
- return TRUE;
+ nId = SHORT1FROMMR(::WinSendMsg((HWND)m_hMenu, MM_ITEMIDFROMPOSITION, MPFROMSHORT(nPos), (MPARAM)0));
+ if (nId == MIT_ERROR)
+ {
+ wxLogLastError("LogLastError");
+ return;
}
- else
+ if(!::WinSendMsg( (HWND)m_hMenu
+ ,MM_QUERYITEM
+ ,MPFROM2SHORT(nId, TRUE)
+ ,MPARAM(&vItem)
+ ))
{
- wxLogLastError("RemoveMenu");
+ wxLogLastError("QueryItem");
}
-*/
- return FALSE;
-}
-
-bool wxMenuBar::OnAppend(wxMenu *a_menu, const wxChar *title)
-{
- WXHMENU submenu = a_menu->GetHMenu();
- if ( !submenu )
- return FALSE;
-
- if ( !m_menuBarFrame )
- return TRUE;
-
- a_menu->Attach(this);
-
-// if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
-// (UINT)submenu, title) )
-// {
-// wxLogLastError(wxT("AppendMenu"));
-// }
+ nId = vItem.id;
+ if (::WinSendMsg(GetHmenu(), MM_SETITEMTEXT, MPFROMSHORT(nId), (MPARAM)rLabel.c_str()));
+ {
+ wxLogLastError("ModifyMenu");
+ }
Refresh();
+} // end of wxMenuBar::SetLabelTop
- return TRUE;
-}
+wxString wxMenuBar::GetLabelTop(
+ size_t nPos
+) const
+{
+ wxCHECK_MSG( nPos < GetMenuCount(), wxEmptyString,
+ wxT("invalid menu index in wxMenuBar::GetLabelTop") );
+ return m_titles[nPos];
+} // end of wxMenuBar::GetLabelTop
// ---------------------------------------------------------------------------
// wxMenuBar construction
// ---------------------------------------------------------------------------
-int wxMenuBar::FindMenu(const wxString& title)
-{
- wxString menuTitle = wxStripMenuCodes(title);
- for ( int i = 0; i < m_menuCount; i++ )
- {
- wxString title = wxStripMenuCodes(m_titles[i]);
- if ( menuTitle == title )
- return i;
- }
-
- return wxNOT_FOUND;
-
-}
-
-void wxMenuBar::ReplaceMenu(int pos, wxMenu * new_menu, const wxString& title)
+wxMenu* wxMenuBar::Replace(
+ size_t nPos
+, wxMenu* pMenu
+, const wxString& rTitle
+)
{
- if (m_menuBarFrame) return;
+ SHORT nId;
+ wxString sTitle = TextToLabel(rTitle);
+ wxMenu* pMenuOld = wxMenuBarBase::Replace( nPos
+ ,pMenu
+ ,sTitle
+ );
+
- if ( pos >= 0 && pos < m_menuCount )
+ nId = SHORT1FROMMR(::WinSendMsg((HWND)m_hMenu, MM_ITEMIDFROMPOSITION, MPFROMSHORT(nPos), (MPARAM)0));
+ if (nId == MIT_ERROR)
{
- wxMenu *old_menu = m_menus[pos];
- m_menus[pos] = new_menu;
- delete old_menu;
+ wxLogLastError("LogLastError");
+ return NULL;
}
+ if (!pMenuOld)
+ return NULL;
+ m_titles[nPos] = sTitle;
+ if (IsAttached())
+ {
+ ::WinSendMsg((HWND)m_hMenu, MM_REMOVEITEM, MPFROM2SHORT(nId, TRUE), (MPARAM)0);
+ ::WinSendMsg((HWND)m_hMenu, MM_INSERTITEM, (MPARAM)&pMenu->m_vMenuData, (MPARAM)sTitle.c_str());
-}
-
-
-void wxMenuBar::Insert(int pos, wxMenu * menu, const wxString& title)
+#if wxUSE_ACCEL
+ if (pMenuOld->HasAccels() || pMenu->HasAccels())
+ {
+ //
+ // Need to rebuild accell table
+ //
+ RebuildAccelTable();
+ }
+#endif // wxUSE_ACCEL
+ Refresh();
+ }
+ return pMenuOld;
+} // end of wxMenuBar::Replace
+
+bool wxMenuBar::Insert(
+ size_t nPos
+, wxMenu* pMenu
+, const wxString& rTitle
+)
{
- if (m_menuBarFrame) return;
- if ( pos < 0 && pos >= m_menuCount ) return;
-
- m_menuCount ++;
- wxMenu **new_menus = new wxMenu *[m_menuCount];
- wxString *new_titles = new wxString[m_menuCount];
- int i;
+ wxString sTitle = TextToLabel(rTitle);
- for (i = 0; i < pos; i++)
- {
- new_menus[i] = m_menus[i];
- m_menus[i] = NULL;
- new_titles[i] = m_titles[i];
- m_titles[i] = wxT("");
- }
+ if (!wxMenuBarBase::Insert( nPos
+ ,pMenu
+ ,sTitle
+ ))
+ return FALSE;
- new_menus[pos] = (wxMenu *)menu;
- new_titles[i] = title;
+ m_titles.Insert( sTitle
+ ,nPos
+ );
- for (i = pos+1; i < m_menuCount; i++)
- {
- new_menus[i] = m_menus[i-1];
- m_menus[i-1] = NULL;
- new_titles[i] = m_titles[i-1];
- m_titles[i-1] = wxT("");
- }
- if (m_menus)
+ if (IsAttached())
{
- delete[]m_menus;
- delete[]m_titles;
+ pMenu->m_vMenuData.iPosition = nPos;
+ ::WinSendMsg( (HWND)m_hMenu
+ ,MM_INSERTITEM
+ ,(MPARAM)&pMenu->m_vMenuData
+ ,(MPARAM)sTitle.c_str()
+ );
+#if wxUSE_ACCEL
+ if (pMenu->HasAccels())
+ {
+ // need to rebuild accell table
+ RebuildAccelTable();
+ }
+#endif // wxUSE_ACCEL
+ Refresh();
}
- m_menus = new_menus;
- m_titles = new_titles;
+ return TRUE;
+} // end of wxMenuBar::Insert
- menu->SetParent(this);
+bool wxMenuBar::Append(
+ wxMenu* pMenu
+, const wxString& rsTitle
+)
+{
+ WXHMENU hSubmenu = pMenu ? pMenu->GetHMenu() : 0;
-}
+ wxCHECK_MSG(hSubmenu, FALSE, wxT("can't append invalid menu to menubar"));
+ wxString sTitle = TextToLabel(rsTitle);
-void wxMenuBar::Append (wxMenu * menu, const wxString& title)
-{
- if (!OnAppend(menu, title))
- return;
+ if (!wxMenuBarBase::Append(pMenu, sTitle))
+ return FALSE;
- m_menuCount ++;
- wxMenu **new_menus = new wxMenu *[m_menuCount];
- wxString *new_titles = new wxString[m_menuCount];
- int i;
+ m_titles.Add(sTitle);
- for (i = 0; i < m_menuCount - 1; i++)
- {
- new_menus[i] = m_menus[i];
- m_menus[i] = NULL;
- new_titles[i] = m_titles[i];
- m_titles[i] = wxT("");
- }
- if (m_menus)
+ if ( IsAttached() )
{
- delete[]m_menus;
- delete[]m_titles;
+ pMenu->m_vMenuData.iPosition = MIT_END;
+ ::WinSendMsg((HWND)m_hMenu, MM_INSERTITEM, (MPARAM)&pMenu->m_vMenuData, (MPARAM)sTitle.c_str());
+#if wxUSE_ACCEL
+ if (pMenu->HasAccels())
+ {
+ //
+ // Need to rebuild accell table
+ //
+ RebuildAccelTable();
+ }
+#endif // wxUSE_ACCEL
+ Refresh();
}
- m_menus = new_menus;
- m_titles = new_titles;
-
- m_menus[m_menuCount - 1] = (wxMenu *)menu;
- m_titles[m_menuCount - 1] = title;
-
- menu->SetParent(this);
-}
+ return TRUE;
+} // end of wxMenuBar::Append
-void wxMenuBar::Delete(wxMenu * menu, int i)
+wxMenu* wxMenuBar::Remove(
+ size_t nPos
+)
{
- int j;
- int ii = (int) i;
+ wxMenu* pMenu = wxMenuBarBase::Remove(nPos);
+ SHORT nId;
+
+ if (!pMenu)
+ return NULL;
+
+ nId = SHORT1FROMMR(::WinSendMsg( (HWND)GetHmenu()
+ ,MM_ITEMIDFROMPOSITION
+ ,MPFROMSHORT(nPos)
+ ,(MPARAM)0)
+ );
+ if (nId == MIT_ERROR)
+ {
+ wxLogLastError("LogLastError");
+ return NULL;
+ }
+ if (IsAttached())
+ {
+ ::WinSendMsg( (HWND)GetHmenu()
+ ,MM_REMOVEITEM
+ ,MPFROM2SHORT(nId, TRUE)
+ ,(MPARAM)0
+ );
- if (menu != 0) {
- for (ii = 0; ii < m_menuCount; ii++) {
- if (m_menus[ii] == menu)
- break;
+#if wxUSE_ACCEL
+ if (pMenu->HasAccels())
+ {
+ //
+ // Need to rebuild accell table
+ //
+ RebuildAccelTable();
}
- if (ii >= m_menuCount)
- return;
- } else {
- if (ii < 0 || ii >= m_menuCount)
- return;
- menu = m_menus[ii];
+#endif // wxUSE_ACCEL
+ Refresh();
}
+ m_titles.Remove(nPos);
+ return pMenu;
+} // end of wxMenuBar::Remove
- if (!OnDelete(menu, ii))
- return;
-
- menu->SetParent(NULL);
-
- -- m_menuCount;
- for (j = ii; j < m_menuCount; j++) {
- m_menus[j] = m_menus[j + 1];
- m_titles[j] = m_titles[j + 1];
- }
-}
+#if wxUSE_ACCEL
-void wxMenuBar::Attach(wxFrame *frame)
+void wxMenuBar::RebuildAccelTable()
{
- wxASSERT_MSG( !m_menuBarFrame, wxT("menubar already attached!") );
-
- m_menuBarFrame = frame;
-
-#if wxUSE_ACCEL
- // create the accel table - we consider that the menubar construction is
- // finished
- size_t nAccelCount = 0;
- int i;
- for ( i = 0; i < m_menuCount; i++ )
+ //
+ // Merge the accelerators of all menus into one accel table
+ //
+ size_t nAccelCount = 0;
+ size_t i;
+ size_t nCount = GetMenuCount();
+
+ for (i = 0; i < nCount; i++)
{
nAccelCount += m_menus[i]->GetAccelCount();
}
- if ( nAccelCount )
+ if (nAccelCount)
{
- wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
+ wxAcceleratorEntry* pAccelEntries = new wxAcceleratorEntry[nAccelCount];
nAccelCount = 0;
- for ( i = 0; i < m_menuCount; i++ )
+ for (i = 0; i < nCount; i++)
{
- nAccelCount += m_menus[i]->CopyAccels(&accelEntries[nAccelCount]);
+ nAccelCount += m_menus[i]->CopyAccels(&pAccelEntries[nAccelCount]);
}
+ m_vAccelTable = wxAcceleratorTable( nAccelCount
+ ,pAccelEntries
+ );
+ delete [] pAccelEntries;
+ }
+} // end of wxMenuBar::RebuildAccelTable
- m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries);
+#endif // wxUSE_ACCEL
- delete [] accelEntries;
- }
+void wxMenuBar::Attach(
+ wxFrame* pFrame
+)
+{
+ wxMenuBarBase::Attach(pFrame);
+
+#if wxUSE_ACCEL
+ RebuildAccelTable();
+ //
+ // Ensure the accelerator table is set to the frame (not the client!)
+ //
+ if (!::WinSetAccelTable( vHabmain
+ ,m_vAccelTable.GetHACCEL()
+ ,(HWND)pFrame->GetFrame()
+ ))
+ wxLogLastError("WinSetAccelTable");
#endif // wxUSE_ACCEL
-}
+} // end of wxMenuBar::Attach
void wxMenuBar::Detach()
{
-// ::DestroyMenu((HMENU)m_hMenu);
- m_hMenu = NULL;
+ ::WinDestroyWindow((HWND)m_hMenu);
+ m_hMenu = (WXHMENU)NULL;
m_menuBarFrame = NULL;
-}
-
+} // end of wxMenuBar::Detach
// ---------------------------------------------------------------------------
// wxMenuBar searching for menu items
// ---------------------------------------------------------------------------
+//
// Find the itemString in menuString, and return the item id or wxNOT_FOUND
-int wxMenuBar::FindMenuItem(const wxString& menuString,
- const wxString& itemString) const
+//
+int wxMenuBar::FindMenuItem(
+ const wxString& rMenuString
+, const wxString& rItemString
+) const
{
- wxString menuLabel = wxStripMenuCodes(menuString);
- for ( int i = 0; i < m_menuCount; i++ )
+ wxString sMenuLabel = wxStripMenuCodes(rMenuString);
+ size_t nCount = GetMenuCount();
+
+ for (size_t i = 0; i < nCount; i++)
{
- wxString title = wxStripMenuCodes(m_titles[i]);
- if ( menuString == title )
- return m_menus[i]->FindItem(itemString);
- }
+ wxString sTitle = wxStripMenuCodes(m_titles[i]);
+ if (rMenuString == sTitle)
+ return m_menus[i]->FindItem(rItemString);
+ }
return wxNOT_FOUND;
-}
+} // end of wxMenuBar::FindMenuItem
-wxMenuItem *wxMenuBar::FindItemForId (int id, wxMenu **itemMenu) const
+wxMenuItem* wxMenuBar::FindItem(
+ int nId
+, wxMenu** ppItemMenu
+) const
{
- if ( itemMenu )
- *itemMenu = NULL;
+ if (ppItemMenu)
+ *ppItemMenu = NULL;
+
+ wxMenuItem* pItem = NULL;
+ size_t nCount = GetMenuCount();
- wxMenuItem *item = NULL;
- for ( int i = 0; !item && (i < m_menuCount); i++ )
+ for (size_t i = 0; !pItem && (i < nCount); i++)
{
- item = m_menus[i]->FindItemForId(id, itemMenu);
+ pItem = m_menus[i]->FindItem( nId
+ ,ppItemMenu
+ );
}
-
- return item;
-}
-
-
-// ----------------------------------------------------------------------------
-// helper functions
-// ----------------------------------------------------------------------------
-
-wxWindow *wxMenu::GetWindow() const
+ return pItem;
+} // end of wxMenuBar::FindItem
+
+wxMenuItem* wxMenuBar::FindItem(
+ int nId
+, ULONG hItem
+, wxMenu** ppItemMenu
+) const
{
- if ( m_pInvokingWindow != NULL )
- return m_pInvokingWindow;
- else if ( m_menuBar != NULL)
- return m_menuBar->GetFrame();
+ if (ppItemMenu)
+ *ppItemMenu = NULL;
- return NULL;
-}
+ wxMenuItem* pItem = NULL;
+ size_t nCount = GetMenuCount();
-WXHMENU wxMenu::GetHMenu() const
-{
- if ( m_hMenu != 0 )
- return m_hMenu;
- else if ( m_savehMenu != 0 )
- return m_savehMenu;
-
- wxFAIL_MSG(wxT("wxMenu without HMENU"));
-
- return 0;
-}
-
-// Update a menu and all submenus recursively. source is the object that has
-// the update event handlers defined for it. If NULL, the menu or associated
-// window will be used.
-void wxMenu::UpdateUI(wxEvtHandler* source)
-{
- if (!source && GetInvokingWindow())
- source = GetInvokingWindow()->GetEventHandler();
- if (!source)
- source = GetEventHandler();
- if (!source)
- source = this;
-
- wxNode* node = GetItems().First();
- while (node)
+ for (size_t i = 0; !pItem && (i < nCount); i++)
{
- wxMenuItem* item = (wxMenuItem*) node->Data();
- if ( !item->IsSeparator() )
- {
- wxWindowID id = item->GetId();
- wxUpdateUIEvent event(id);
- event.SetEventObject( source );
-
- if (source->ProcessEvent(event))
- {
- if (event.GetSetText())
- SetLabel(id, event.GetText());
- if (event.GetSetChecked())
- Check(id, event.GetChecked());
- if (event.GetSetEnabled())
- Enable(id, event.GetEnabled());
- }
-
- if (item->GetSubMenu())
- item->GetSubMenu()->UpdateUI(source);
- }
- node = node->Next();
+ pItem = m_menus[i]->FindItem( nId
+ ,hItem
+ ,ppItemMenu
+ );
}
-}
+ return pItem;
+} // end of wxMenuBar::FindItem