/////////////////////////////////////////////////////////////////////////////
// Name: menu.cpp
// Purpose: wxMenu, wxMenuBar, wxMenuItem
-// Author: AUTHOR
+// Author: David Webster
// Modified by:
-// Created: ??/??/98
+// Created: 10/10/99
// RCS-ID: $Id$
-// Copyright: (c) AUTHOR
-// Licence: wxWindows licence
+// Copyright: (c) David Webster
+// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
+#ifdef __GNUG__
+ #pragma implementation "menu.h"
+#endif
-// ============================================================================
-// headers & declarations
-// ============================================================================
+// For compilers that support precompilation, includes "wx.h".
+#include "wx/wxprec.h"
-// wxWindows headers
-// -----------------
+#ifndef WX_PRECOMP
+ #include "wx/frame.h"
+ #include "wx/menu.h"
+ #include "wx/utils.h"
+ #include "wx/intl.h"
+ #include "wx/log.h"
+#endif
-#ifdef __GNUG__
-#pragma implementation "menu.h"
-#pragma implementation "menuitem.h"
+#if wxUSE_OWNER_DRAWN
+ #include "wx/ownerdrw.h"
#endif
-#include "wx/menu.h"
-#include "wx/menuitem.h"
-#include "wx/log.h"
-#include "wx/utils.h"
+#include "wx/os2/private.h"
// other standard headers
-// ----------------------
#include <string.h>
-#if !USE_SHARED_LIBRARY
-IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
-IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler)
-#endif
+// ----------------------------------------------------------------------------
+// global variables
+// ----------------------------------------------------------------------------
+
+extern wxMenu *wxCurrentPopupMenu;
+
+// ----------------------------------------------------------------------------
+// constants
+// ----------------------------------------------------------------------------
+
+// the (popup) menu title has this special id
+static const int idMenuTitle = -2;
+
+// ----------------------------------------------------------------------------
+// macros
+// ----------------------------------------------------------------------------
+
+ IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
+ IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler)
// ============================================================================
// implementation
// ============================================================================
-// Menus
+// ---------------------------------------------------------------------------
+// wxMenu construction, adding and removing menu items
+// ---------------------------------------------------------------------------
// Construct a menu with optional title (then use append)
-wxMenu::wxMenu(const wxString& title, const wxFunction func)
+void wxMenu::Init()
{
- m_title = title;
- m_parent = NULL;
- m_eventHandler = this;
- m_noItems = 0;
- m_menuBar = NULL;
- m_clientData = (void*) NULL;
- if (m_title != "")
+ m_doBreak = FALSE;
+
+ // create the menu
+ m_hMenu = (WXHMENU)0; // CreatePopupMenu();
+ if ( !m_hMenu )
{
- Append(-2, m_title) ;
- AppendSeparator() ;
+ wxLogLastError("CreatePopupMenu");
}
- Callback(func);
-
- // TODO create menu
+ // if we have a title, insert it in the beginning of the menu
+ if ( !!m_title )
+ {
+ Append(idMenuTitle, m_title);
+ AppendSeparator();
+ }
}
// The wxWindow destructor will take care of deleting the submenus.
wxMenu::~wxMenu()
{
- // TODO destroy menu and children
-
- wxNode *node = m_menuItems.First();
- while (node)
+ // we should free Windows resources only if Windows doesn't do it for us
+ // which happens if we're attached to a menubar or a submenu of another
+ // menu
+ if ( !IsAttached() && !GetParent() )
{
- 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->GetSubMenu())
- item->DeleteSubMenu();
-
- wxNode *next = node->Next();
- delete item;
- delete node;
- node = next;
+/*
+ if ( !::DestroyMenu(GetHmenu()) )
+ {
+ wxLogLastError("DestroyMenu");
+ }
+*/
}
+
+#if wxUSE_ACCEL
+ // delete accels
+ WX_CLEAR_ARRAY(m_accels);
+#endif // wxUSE_ACCEL
}
void wxMenu::Break()
{
- // TODO
+ // this will take effect during the next call to Append()
+ m_doBreak = TRUE;
}
-// function appends a new item or submenu to the menu
-void wxMenu::Append(wxMenuItem *pItem)
-{
- // TODO
+#if wxUSE_ACCEL
- wxCHECK_RET( pItem != NULL, "can't append NULL item to the menu" );
-
- m_menuItems.Append(pItem);
+int wxMenu::FindAccel(int id) const
+{
+ size_t n, count = m_accels.GetCount();
+ for ( n = 0; n < count; n++ )
+ {
+ if ( m_accels[n]->m_command == id )
+ return n;
+ }
- m_noItems++;
+ return wxNOT_FOUND;
}
-void wxMenu::AppendSeparator()
+void wxMenu::UpdateAccel(wxMenuItem *item)
{
- // TODO
- Append(new wxMenuItem(this, ID_SEPARATOR));
-}
+ // find the (new) accel for this item
+ wxAcceleratorEntry *accel = wxGetAccelFromString(item->GetText());
+ if ( accel )
+ accel->m_command = item->GetId();
-// Pullright item
-void wxMenu::Append(int Id, const wxString& label, wxMenu *SubMenu,
- const wxString& helpString)
-{
- Append(new wxMenuItem(this, Id, label, helpString, FALSE, SubMenu));
-}
+ // find the old one
+ int n = FindAccel(item->GetId());
+ if ( n == wxNOT_FOUND )
+ {
+ // no old, add new if any
+ if ( accel )
+ m_accels.Add(accel);
+ else
+ return; // skipping RebuildAccelTable() below
+ }
+ else
+ {
+ // replace old with new or just remove the old one if no new
+ delete m_accels[n];
+ if ( accel )
+ m_accels[n] = accel;
+ else
+ m_accels.Remove(n);
+ }
-// 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));
+ if ( IsAttached() )
+ {
+ m_menuBar->RebuildAccelTable();
+ }
}
-void wxMenu::Delete(int id)
+#endif // wxUSE_ACCEL
+
+// append a new item or submenu to the menu
+bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos)
{
- wxNode *node;
- wxMenuItem *item;
- int pos;
+#if wxUSE_ACCEL
+ UpdateAccel(pItem);
+#endif // wxUSE_ACCEL
- for (pos = 0, node = m_menuItems.First(); node; node = node->Next(), pos++) {
- item = (wxMenuItem *)node->Data();
- if (item->GetId() == id)
- break;
+ UINT flags = 0;
+// TODO:
+/*
+ // 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 (!node)
- return;
+ if ( pItem->IsSeparator() ) {
+ flags |= MF_SEPARATOR;
+ }
- m_menuItems.DeleteNode(node);
- delete item;
+ // 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_MSG( submenu->GetHMenu(), wxT("invalid submenu") );
- // TODO
-}
+ submenu->SetParent(this);
-void wxMenu::Enable(int Id, bool Flag)
-{
- wxMenuItem *item = FindItemForId(Id);
- wxCHECK_RET( item != NULL, "can't enable non-existing menu item" );
+ id = (UINT)submenu->GetHMenu();
- item->Enable(Flag);
-}
+ flags |= MF_POPUP;
+ }
+ else {
+ id = pItem->GetId();
+ }
-bool wxMenu::Enabled(int Id) const
-{
- wxMenuItem *item = FindItemForId(Id);
- wxCHECK( item != NULL, FALSE );
+ LPCTSTR pData;
- return item->IsEnabled();
-}
+#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;
+ }
+ else
+#endif
+ {
+ // menu is just a normal string (passed in data parameter)
+ flags |= MF_STRING;
-void wxMenu::Check(int Id, bool Flag)
-{
- wxMenuItem *item = FindItemForId(Id);
- wxCHECK_RET( item != NULL, "can't get status of non-existing menu item" );
+ pData = (char*)pItem->GetText().c_str();
+ }
- item->Check(Flag);
-}
+ BOOL ok;
+ if ( pos == (size_t)-1 )
+ {
+ ok = ::AppendMenu(GetHmenu(), flags, id, pData);
+ }
+ else
+ {
+ ok = ::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData);
+ }
-bool wxMenu::Checked(int Id) const
-{
- wxMenuItem *item = FindItemForId(Id);
- wxCHECK( item != NULL, FALSE );
+ if ( !ok )
+ {
+ wxLogLastError("Insert or AppendMenu");
+
+ return FALSE;
+ }
+ else
+ {
+ // if we just appended the title, highlight it
+#ifdef __WIN32__
+ if ( (int)id == idMenuTitle )
+ {
+ // visually select the menu title
+ MENUITEMINFO mii;
+ mii.cbSize = sizeof(mii);
+ mii.fMask = MIIM_STATE;
+ mii.fState = MFS_DEFAULT;
+
+ if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id, FALSE, &mii) )
+ {
+ wxLogLastError(wxT("SetMenuItemInfo"));
+ }
+ }
+#endif // __WIN32__
- return item->IsChecked();
+ // if we're already attached to the menubar, we must update it
+ if ( IsAttached() )
+ {
+ m_menuBar->Refresh();
+ }
+
+ return TRUE;
+ }
+*/
+ return FALSE;
}
-void wxMenu::SetTitle(const wxString& label)
+bool wxMenu::DoAppend(wxMenuItem *item)
{
- m_title = label ;
- // TODO
+ return wxMenuBase::DoAppend(item) && DoInsertOrAppend(item);
}
-const wxString wxMenu::GetTitle() const
+bool wxMenu::DoInsert(size_t pos, wxMenuItem *item)
{
- return m_title;
+ return wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos);
}
-void wxMenu::SetLabel(int id, const wxString& label)
+wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
{
- wxMenuItem *item = FindItemForId(id) ;
- if (item==NULL)
- return;
+ // we need to find the items position in the child list
+ size_t pos;
+ wxMenuItemList::Node *node = GetMenuItems().GetFirst();
+ for ( pos = 0; node; pos++ )
+ {
+ if ( node->GetData() == item )
+ break;
+
+ node = node->GetNext();
+ }
+
+ // DoRemove() (unlike Remove) can only be called for existing item!
+ wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
+
+#if wxUSE_ACCEL
+ // remove the corresponding accel from the accel table
+ int n = FindAccel(item->GetId());
+ if ( n != wxNOT_FOUND )
+ {
+ delete m_accels[n];
- if (item->GetSubMenu()==NULL)
+ m_accels.Remove(n);
+ }
+ //else: this item doesn't have an accel, nothing to do
+#endif // wxUSE_ACCEL
+/*
+ // remove the item from the menu
+ if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
{
- // TODO
+ wxLogLastError("RemoveMenu");
}
- else
+*/
+ if ( IsAttached() )
{
- // TODO
+ // otherwise, the chane won't be visible
+ m_menuBar->Refresh();
}
- item->SetName(label);
-}
-wxString wxMenu::GetLabel(int Id) const
-{
- // TODO
- return wxString("") ;
+ // and from internal data structures
+ return wxMenuBase::DoRemove(item);
}
-// Finds the item id matching the given string, -1 if not found.
-int wxMenu::FindItem (const wxString& itemString) const
-{
- char buf1[200];
- char buf2[200];
- wxStripMenuCodes ((char *)(const char *)itemString, buf1);
+// ---------------------------------------------------------------------------
+// accelerator helpers
+// ---------------------------------------------------------------------------
- for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
+#if wxUSE_ACCEL
+
+// 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++ )
{
- wxMenuItem *item = (wxMenuItem *) node->Data ();
- if (item->GetSubMenu())
- {
- int ans = item->GetSubMenu()->FindItem(itemString);
- if (ans > -1)
- return ans;
- }
- if ( !item->IsSeparator() )
- {
- wxStripMenuCodes((char *)item->GetName().c_str(), buf2);
- if (strcmp(buf1, buf2) == 0)
- return item->GetId();
- }
+ *accels++ = *m_accels[n];
}
- return -1;
+ return count;
}
-wxMenuItem *wxMenu::FindItemForId(int itemId, wxMenu ** itemMenu) const
+#endif // wxUSE_ACCEL
+
+// ---------------------------------------------------------------------------
+// set wxMenu title
+// ---------------------------------------------------------------------------
+
+void wxMenu::SetTitle(const wxString& label)
{
- if (itemMenu)
- *itemMenu = NULL;
- for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
- {
- wxMenuItem *item = (wxMenuItem *) node->Data ();
+ bool hasNoTitle = m_title.IsEmpty();
+ m_title = label;
+
+ HMENU hMenu = GetHmenu();
- if (item->GetId() == itemId)
+ if ( hasNoTitle )
+ {
+ if ( !label.IsEmpty() )
+ {
+/*
+ if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
+ (unsigned)idMenuTitle, m_title) ||
+ !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
+ {
+ wxLogLastError("InsertMenu");
+ }
+*/
+ }
+ }
+ else
+ {
+ if ( label.IsEmpty() )
{
- if (itemMenu)
- *itemMenu = (wxMenu *) this;
- return item;
+/*
+ // remove the title and the separator after it
+ if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
+ !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
+ {
+ wxLogLastError("RemoveMenu");
+ }
+*/
}
-
- if (item->GetSubMenu())
+ else
{
- wxMenuItem *ans = item->GetSubMenu()->FindItemForId (itemId, itemMenu);
- if (ans)
- return ans;
+/*
+ // modify the title
+ if ( !ModifyMenu(hMenu, 0u,
+ MF_BYPOSITION | MF_STRING,
+ (unsigned)idMenuTitle, m_title) )
+ {
+ wxLogLastError("ModifyMenu");
+ }
+*/
}
}
+/*
+#ifdef __WIN32__
+ // 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 (itemMenu)
- *itemMenu = NULL;
- return NULL;
+ if ( !SetMenuItemInfo(hMenu, (unsigned)idMenuTitle, FALSE, &mii) )
+ {
+ wxLogLastError("SetMenuItemInfo");
+ }
+ }
+#endif // Win32
+*/
}
-void wxMenu::SetHelpString(int itemId, const wxString& helpString)
-{
- wxMenuItem *item = FindItemForId (itemId);
- if (item)
- item->SetHelp(helpString);
-}
+// ---------------------------------------------------------------------------
+// event processing
+// ---------------------------------------------------------------------------
-wxString wxMenu::GetHelpString (int itemId) const
+bool wxMenu::OS2Command(WXUINT WXUNUSED(param), WXWORD id)
{
- wxMenuItem *item = FindItemForId (itemId);
- wxString str("");
- return (item == NULL) ? str : item->GetHelp();
+ // ignore commands from the menu title
+
+ // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
+ if ( id != (WXWORD)idMenuTitle )
+ {
+ wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED);
+ event.SetEventObject( this );
+ event.SetId( id );
+ event.SetInt( id );
+ ProcessCommand(event);
+ }
+
+ return TRUE;
}
-void wxMenu::ProcessCommand(wxCommandEvent & event)
+bool wxMenu::ProcessCommand(wxCommandEvent & event)
{
bool processed = FALSE;
+#if WXWIN_COMPATIBILITY
// Try a callback
if (m_callback)
{
- (void) (*(m_callback)) (*this, event);
- processed = TRUE;
+ (void)(*(m_callback))(*this, event);
+ processed = TRUE;
}
+#endif // WXWIN_COMPATIBILITY
// Try the menu's event handler
if ( !processed && GetEventHandler())
{
- processed = GetEventHandler()->ProcessEvent(event);
+ processed = GetEventHandler()->ProcessEvent(event);
}
-/* TODO
- // Try the window the menu was popped up from (and up
- // through the hierarchy)
- if ( !processed && GetInvokingWindow())
- processed = GetInvokingWindow()->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;
}
-// 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)
+// ---------------------------------------------------------------------------
+// other
+// ---------------------------------------------------------------------------
+
+void wxMenu::Attach(wxMenuBar *menubar)
{
- if (!source && GetInvokingWindow())
- source = GetInvokingWindow()->GetEventHandler();
- if (!source)
- source = GetEventHandler();
- if (!source)
- source = this;
+ // 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") );
- wxNode* node = GetItems().First();
- while (node)
- {
- wxMenuItem* item = (wxMenuItem*) node->Data();
- if ( !item->IsSeparator() )
- {
- wxWindowID id = item->GetId();
- wxUpdateUIEvent event(id);
- event.SetEventObject( source );
+ m_menuBar = menubar;
+}
- 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());
- }
+void wxMenu::Detach()
+{
+ wxASSERT_MSG( m_menuBar, wxT("can't detach menu if it's not attached") );
- if (item->GetSubMenu())
- item->GetSubMenu()->UpdateUI(source);
- }
- node = node->Next();
- }
+ m_menuBar = NULL;
}
-bool wxWindow::PopupMenu(wxMenu *menu, int x, int y)
+wxWindow *wxMenu::GetWindow() const
{
- menu->SetInvokingWindow(this);
- menu->UpdateUI();
+ if ( m_invokingWindow != NULL )
+ return m_invokingWindow;
+ else if ( m_menuBar != NULL)
+ return m_menuBar->GetFrame();
- // TODO
- return FALSE;
+ return NULL;
}
+// ---------------------------------------------------------------------------
// Menu Bar
-wxMenuBar::wxMenuBar()
+// ---------------------------------------------------------------------------
+
+void wxMenuBar::Init()
{
m_eventHandler = this;
- m_menuCount = 0;
- m_menus = NULL;
- m_titles = NULL;
m_menuBarFrame = NULL;
-
- // TODO
+ m_hMenu = 0;
}
-wxMenuBar::wxMenuBar(int n, wxMenu *menus[], const wxString titles[])
+wxMenuBar::wxMenuBar()
{
- m_eventHandler = this;
- m_menuCount = n;
- m_menus = menus;
- m_titles = new wxString[n];
- int i;
- for ( i = 0; i < n; i++ )
- m_titles[i] = titles[i];
- m_menuBarFrame = NULL;
+ Init();
+}
- // TODO
+wxMenuBar::wxMenuBar( long WXUNUSED(style) )
+{
+ Init();
}
-wxMenuBar::~wxMenuBar()
+wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[])
{
- int i;
- for (i = 0; i < m_menuCount; i++)
+ Init();
+
+ m_titles.Alloc(count);
+
+ for ( int i = 0; i < count; i++ )
{
- delete m_menus[i];
- }
- delete[] m_menus;
- delete[] m_titles;
+ m_menus.Append(menus[i]);
+ m_titles.Add(titles[i]);
- // TODO
+ menus[i]->Attach(this);
+ }
}
-// 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 flag)
+wxMenuBar::~wxMenuBar()
{
- wxMenu *itemMenu = NULL;
- wxMenuItem *item = FindItemForId(id, &itemMenu) ;
- if (!item)
- return;
-
- // TODO
}
-void wxMenuBar::EnableTop(int pos, bool flag)
+// ---------------------------------------------------------------------------
+// wxMenuBar helpers
+// ---------------------------------------------------------------------------
+
+void wxMenuBar::Refresh(
+ bool WXUNUSED(bEraseBackground)
+, const wxRect* WXUNUSED(pRect)
+)
{
- // TODO
+ wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
+
+// DrawMenuBar(GetHwndOf(m_menuBarFrame));
}
-// Must only be used AFTER menu has been attached to frame,
-// otherwise use individual menus
-void wxMenuBar::Check(int id, bool flag)
+WXHMENU wxMenuBar::Create()
{
- wxMenu *itemMenu = NULL;
- wxMenuItem *item = FindItemForId(id, &itemMenu) ;
- if (!item)
- return;
+ if (m_hMenu != 0 )
+ return m_hMenu;
- if (!item->IsCheckable())
- return ;
+ wxCHECK_MSG( !m_hMenu, TRUE, wxT("menubar already created") );
- // TODO
-}
+// TODO:
+/*
-bool wxMenuBar::Checked(int id) const
-{
- wxMenu *itemMenu = NULL;
- wxMenuItem *item = FindItemForId(id, &itemMenu) ;
- if (!item)
- return FALSE;
+ m_hMenu = (WXHMENU)::CreateMenu();
- // TODO
- return FALSE;
+ if ( !m_hMenu )
+ {
+ wxLogLastError("CreateMenu");
+ }
+ else
+ {
+ size_t count = GetMenuCount();
+ for ( size_t i = 0; i < count; i++ )
+ {
+ if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
+ (UINT)m_menus[i]->GetHMenu(),
+ m_titles[i]) )
+ {
+ wxLogLastError("AppendMenu");
+ }
+ }
+ }
+
+ return m_hMenu;
+*/
+ return (WXHMENU)0;
}
-bool wxMenuBar::Enabled(int id) const
+// ---------------------------------------------------------------------------
+// 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::EnableTop(size_t pos, bool enable)
{
- wxMenu *itemMenu = NULL;
- wxMenuItem *item = FindItemForId(id, &itemMenu) ;
- if (!item)
- return FALSE;
+ wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
- // TODO
- return FALSE ;
-}
+// int flag = enable ? MF_ENABLED : MF_GRAYED;
+// EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag);
-void wxMenuBar::SetLabel(int id, const wxString& label)
+ Refresh();
+}
+
+void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
{
- wxMenu *itemMenu = NULL;
- wxMenuItem *item = FindItemForId(id, &itemMenu) ;
+ wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
+
+ m_titles[pos] = label;
- if (!item)
+ if ( !IsAttached() )
+ {
return;
+ }
+ //else: have to modify the existing menu
- // TODO
-}
+// TODO:
+/*
+ UINT id;
+ UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION);
+ if ( flagsOld == 0xFFFFFFFF )
+ {
+ wxLogLastError(wxT("GetMenuState"));
-wxString wxMenuBar::GetLabel(int id) const
-{
- wxMenu *itemMenu = NULL;
- wxMenuItem *item = FindItemForId(id, &itemMenu) ;
+ return;
+ }
- if (!item)
- return wxString("");
+ 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;
+ }
- // TODO
- return wxString("") ;
+ if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld,
+ id, label) == (int)0xFFFFFFFF )
+ {
+ wxLogLastError("ModifyMenu");
+ }
+*/
+ Refresh();
}
-void wxMenuBar::SetLabelTop(int pos, const wxString& label)
+wxString wxMenuBar::GetLabelTop(size_t pos) const
{
- // TODO
-}
+ wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
+ wxT("invalid menu index in wxMenuBar::GetLabelTop") );
-wxString wxMenuBar::GetLabelTop(int pos) const
-{
- // TODO
- return wxString("");
+ return m_titles[pos];
}
-bool wxMenuBar::OnDelete(wxMenu *a_menu, int pos)
+int wxMenuBar::FindMenu(const wxString& title)
{
- // TODO
- return FALSE;
+ wxString menuTitle = wxStripMenuCodes(title);
+
+ size_t count = GetMenuCount();
+ for ( size_t i = 0; i < count; i++ )
+ {
+ wxString title = wxStripMenuCodes(m_titles[i]);
+ if ( menuTitle == title )
+ return i;
+ }
+
+ return wxNOT_FOUND;
+
}
-bool wxMenuBar::OnAppend(wxMenu *a_menu, const char *title)
+// ---------------------------------------------------------------------------
+// wxMenuBar construction
+// ---------------------------------------------------------------------------
+
+wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
{
- // TODO
- return FALSE;
+ wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
+ if ( !menuOld )
+ return FALSE;
+ m_titles[pos] = title;
+// TODO:
+/*
+ if ( IsAttached() )
+ {
+ // can't use ModifyMenu() because it deletes the submenu it replaces
+ if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
+ {
+ wxLogLastError("RemoveMenu");
+ }
+
+ if ( !::InsertMenu(GetHmenu(), (UINT)pos,
+ MF_BYPOSITION | MF_POPUP | MF_STRING,
+ (UINT)GetHmenuOf(menu), title) )
+ {
+ wxLogLastError("InsertMenu");
+ }
+
+#if wxUSE_ACCEL
+ if ( menuOld->HasAccels() || menu->HasAccels() )
+ {
+ // need to rebuild accell table
+ RebuildAccelTable();
+ }
+#endif // wxUSE_ACCEL
+
+ Refresh();
+ }
+*/
+ return menuOld;
}
-void wxMenuBar::Append (wxMenu * menu, const wxString& title)
+bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
{
- if (!OnAppend(menu, title))
- return;
+ if ( !wxMenuBarBase::Insert(pos, menu, title) )
+ return FALSE;
- m_menuCount ++;
- wxMenu **new_menus = new wxMenu *[m_menuCount];
- wxString *new_titles = new wxString[m_menuCount];
- int i;
+ m_titles.Insert(title, pos);
- 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] = "";
- }
- if (m_menus)
+ menu->Attach(this);
+// TODO:
+/*
+ if ( IsAttached() )
{
- delete[]m_menus;
- delete[]m_titles;
- }
- m_menus = new_menus;
- m_titles = new_titles;
+ if ( !::InsertMenu(GetHmenu(), pos,
+ MF_BYPOSITION | MF_POPUP | MF_STRING,
+ (UINT)GetHmenuOf(menu), title) )
+ {
+ wxLogLastError("InsertMenu");
+ }
- m_menus[m_menuCount - 1] = (wxMenu *)menu;
- m_titles[m_menuCount - 1] = title;
+#if wxUSE_ACCEL
+ if ( menu->HasAccels() )
+ {
+ // need to rebuild accell table
+ RebuildAccelTable();
+ }
+#endif // wxUSE_ACCEL
- // TODO
+ Refresh();
+ }
+*/
+ return TRUE;
}
-void wxMenuBar::Delete(wxMenu * menu, int i)
+bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
{
- int j;
- int ii = (int) i;
+ WXHMENU submenu = menu ? menu->GetHMenu() : 0;
+ wxCHECK_MSG( submenu, FALSE, wxT("can't append invalid menu to menubar") );
- if (menu != 0)
+ if ( !wxMenuBarBase::Append(menu, title) )
+ return FALSE;
+
+ menu->Attach(this);
+
+ m_titles.Add(title);
+// TODO:
+/*
+ if ( IsAttached() )
{
- for (ii = 0; ii < m_menuCount; ii++)
+ if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
+ (UINT)submenu, title) )
{
- if (m_menus[ii] == menu)
- break;
- }
- if (ii >= m_menuCount)
- return;
- } else
- {
- if (ii < 0 || ii >= m_menuCount)
- return;
- menu = m_menus[ii];
- }
+ wxLogLastError(wxT("AppendMenu"));
+ }
- if (!OnDelete(menu, ii))
- return;
+#if wxUSE_ACCEL
+ if ( menu->HasAccels() )
+ {
+ // need to rebuild accell table
+ RebuildAccelTable();
+ }
+#endif // wxUSE_ACCEL
- menu->SetParent(NULL);
+ Refresh();
+ }
+*/
+ return TRUE;
+}
- -- m_menuCount;
- for (j = ii; j < m_menuCount; j++)
+wxMenu *wxMenuBar::Remove(size_t pos)
+{
+ wxMenu *menu = wxMenuBarBase::Remove(pos);
+ if ( !menu )
+ return NULL;
+// TODO:
+/*
+ if ( IsAttached() )
{
- m_menus[j] = m_menus[j + 1];
- m_titles[j] = m_titles[j + 1];
+ if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
+ {
+ wxLogLastError("RemoveMenu");
+ }
+
+ menu->Detach();
+
+#if wxUSE_ACCEL
+ if ( menu->HasAccels() )
+ {
+ // need to rebuild accell table
+ RebuildAccelTable();
+ }
+#endif // wxUSE_ACCEL
+
+ Refresh();
}
+
+ m_titles.Remove(pos);
+*/
+ return menu;
}
-// Find the menu menuString, item itemString, and return the item id.
-// Returns -1 if none found.
-int wxMenuBar::FindMenuItem (const wxString& menuString, const wxString& itemString) const
+#if wxUSE_ACCEL
+
+void wxMenuBar::RebuildAccelTable()
{
- char buf1[200];
- char buf2[200];
- wxStripMenuCodes ((char *)(const char *)menuString, buf1);
- 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, count = GetMenuCount();
+ for ( i = 0; i < count; i++ )
{
- wxStripMenuCodes ((char *)(const char *)m_titles[i], buf2);
- if (strcmp (buf1, buf2) == 0)
- return m_menus[i]->FindItem (itemString);
+ nAccelCount += m_menus[i]->GetAccelCount();
+ }
+
+ if ( nAccelCount )
+ {
+ wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
+
+ nAccelCount = 0;
+ for ( i = 0; i < count; i++ )
+ {
+ nAccelCount += m_menus[i]->CopyAccels(&accelEntries[nAccelCount]);
+ }
+
+ m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries);
+
+ delete [] accelEntries;
}
- return -1;
}
-wxMenuItem *wxMenuBar::FindItemForId (int Id, wxMenu ** itemMenu) const
+#endif // wxUSE_ACCEL
+
+void wxMenuBar::Attach(wxFrame *frame)
{
- if (itemMenu)
- *itemMenu = NULL;
+ wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
- wxMenuItem *item = NULL;
- int i;
- for (i = 0; i < m_menuCount; i++)
- if ((item = m_menus[i]->FindItemForId (Id, itemMenu)))
- return item;
- return NULL;
+ m_menuBarFrame = frame;
+
+#if wxUSE_ACCEL
+ RebuildAccelTable();
+#endif // wxUSE_ACCEL
}
-void wxMenuBar::SetHelpString (int Id, const wxString& helpString)
+void wxMenuBar::Detach()
+{
+// ::DestroyMenu((HMENU)m_hMenu);
+ m_hMenu = (WXHMENU)NULL;
+ m_menuBarFrame = NULL;
+}
+
+
+// ---------------------------------------------------------------------------
+// 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 i;
- for (i = 0; i < m_menuCount; i++)
+ wxString menuLabel = wxStripMenuCodes(menuString);
+ size_t count = GetMenuCount();
+ for ( size_t i = 0; i < count; i++ )
{
- if (m_menus[i]->FindItemForId (Id))
- {
- m_menus[i]->SetHelpString (Id, helpString);
- return;
- }
+ wxString title = wxStripMenuCodes(m_titles[i]);
+ if ( menuString == title )
+ return m_menus[i]->FindItem(itemString);
}
+
+ return wxNOT_FOUND;
}
-wxString wxMenuBar::GetHelpString (int Id) const
+wxMenuItem *wxMenuBar::FindItem(int id, wxMenu **itemMenu) const
{
- int i;
- for (i = 0; i < m_menuCount; i++)
+ if ( itemMenu )
+ *itemMenu = NULL;
+
+ wxMenuItem *item = NULL;
+ size_t count = GetMenuCount();
+ for ( size_t i = 0; !item && (i < count); i++ )
{
- if (m_menus[i]->FindItemForId (Id))
- return wxString(m_menus[i]->GetHelpString (Id));
+ item = m_menus[i]->FindItem(id, itemMenu);
}
- return wxString("");
-}
+ return item;
+}