X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a393f450dc992c9b37300b4a6255bf4e1c97ce69..5405bfb439dcb552bd79e6f35c347498811f85ef:/src/univ/menu.cpp diff --git a/src/univ/menu.cpp b/src/univ/menu.cpp index ba4e56b519..eaf34b8f9e 100644 --- a/src/univ/menu.cpp +++ b/src/univ/menu.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: univ/menu.cpp +// Name: src/univ/menu.cpp // Purpose: wxMenuItem, wxMenu and wxMenuBar implementation // Author: Vadim Zeitlin // Modified by: @@ -17,32 +17,29 @@ // headers // ---------------------------------------------------------------------------- -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) - #pragma implementation "univmenuitem.h" - #pragma implementation "univmenu.h" -#endif - #include "wx/wxprec.h" #ifdef __BORLANDC__ #pragma hdrstop #endif +#if wxUSE_MENUS + +#include "wx/menu.h" +#include "wx/stockitem.h" + #ifndef WX_PRECOMP #include "wx/dynarray.h" #include "wx/control.h" // for FindAccelIndex() - #include "wx/menu.h" #include "wx/settings.h" #include "wx/accel.h" #include "wx/log.h" + #include "wx/frame.h" + #include "wx/dcclient.h" #endif // WX_PRECOMP -#if wxUSE_MENUS - #include "wx/popupwin.h" #include "wx/evtloop.h" -#include "wx/dcclient.h" -#include "wx/frame.h" #include "wx/univ/renderer.h" @@ -50,6 +47,8 @@ #include "wx/msw/private.h" #endif // __WXMSW__ +typedef wxMenuItemList::compatibility_iterator wxMenuItemIter; + // ---------------------------------------------------------------------------- // wxMenuInfo contains all extra information about top level menus we need // ---------------------------------------------------------------------------- @@ -68,6 +67,8 @@ public: void SetLabel(const wxString& text) { + m_originalLabel = text; + // remember the accel char (may be -1 if none) m_indexAccel = wxControl::FindAccelIndex(text, &m_label); @@ -80,6 +81,7 @@ public: // accessors const wxString& GetLabel() const { return m_label; } + const wxString& GetOriginalLabel() const { return m_originalLabel; } bool IsEnabled() const { return m_isEnabled; } wxCoord GetWidth(wxMenuBar *menubar) const { @@ -106,6 +108,7 @@ private: } wxString m_label; + wxString m_originalLabel; wxCoord m_width; int m_indexAccel; bool m_isEnabled; @@ -124,7 +127,7 @@ class wxPopupMenuWindow : public wxPopupTransientWindow public: wxPopupMenuWindow(wxWindow *parent, wxMenu *menu); - ~wxPopupMenuWindow(); + virtual ~wxPopupMenuWindow(); // override the base class version to select the first item initially virtual void Popup(wxWindow *focus = NULL); @@ -132,12 +135,16 @@ public: // override the base class version to dismiss any open submenus virtual void Dismiss(); - // notify the menu when the window disappears from screen - virtual void OnDismiss(); - // called when a submenu is dismissed void OnSubmenuDismiss(bool dismissParent); + // the default wxMSW wxPopupTransientWindow::OnIdle disables the capture + // when the cursor is inside the popup, which dsables the menu tracking + // so override it to do nothing +#ifdef __WXMSW__ + void OnIdle(wxIdleEvent& WXUNUSED(event)) { } +#endif + // get the currently selected item (may be NULL) wxMenuItem *GetCurrentItem() const { @@ -145,13 +152,13 @@ public: } // find the menu item at given position - wxMenuItemList::compatibility_iterator GetMenuItemFromPoint(const wxPoint& pt) const; + wxMenuItemIter GetMenuItemFromPoint(const wxPoint& pt) const; // refresh the given item void RefreshItem(wxMenuItem *item); // preselect the first item - void SelectFirst() { SetCurrent(m_menu->GetMenuItems().GetFirst()); } + void SelectFirst() { SetCurrentItem(m_menu->GetMenuItems().GetFirst()); } // process the key event, return true if done bool ProcessKeyDown(int key); @@ -170,6 +177,9 @@ protected: WithMouse }; + // notify the menu when the window disappears from screen + virtual void OnDismiss(); + // draw the menu inside this window virtual void DoDraw(wxControlRenderer *renderer); @@ -182,12 +192,11 @@ protected: // reset the current item and node void ResetCurrent(); - // set the current node and item withotu refreshing anything - void SetCurrent(wxMenuItemList::compatibility_iterator node); - virtual bool SetCurrent(bool doit = true){return wxPopupTransientWindow::SetCurrent(doit);}; + // set the current node and item without refreshing anything + void SetCurrentItem(wxMenuItemIter node); // change the current item refreshing the old and new items - void ChangeCurrent(wxMenuItemList::compatibility_iterator node); + void ChangeCurrent(wxMenuItemIter node); // activate item, i.e. call either ClickItem() or OpenSubmenu() depending // on what it is, return true if something was done (i.e. it's not a @@ -217,23 +226,23 @@ protected: bool HasOpenSubmenu() const { return m_hasOpenSubMenu; } // get previous node after the current one - wxMenuItemList::compatibility_iterator GetPrevNode() const; + wxMenuItemIter GetPrevNode() const; // get previous node before the given one, wrapping if it's the first one - wxMenuItemList::compatibility_iterator GetPrevNode(wxMenuItemList::compatibility_iterator node) const; + wxMenuItemIter GetPrevNode(wxMenuItemIter node) const; // get next node after the current one - wxMenuItemList::compatibility_iterator GetNextNode() const; + wxMenuItemIter GetNextNode() const; // get next node after the given one, wrapping if it's the last one - wxMenuItemList::compatibility_iterator GetNextNode(wxMenuItemList::compatibility_iterator node) const; + wxMenuItemIter GetNextNode(wxMenuItemIter node) const; private: // the menu we show wxMenu *m_menu; // the menu node corresponding to the current item - wxMenuItemList::compatibility_iterator m_nodeCurrent; + wxMenuItemIter m_nodeCurrent; // do we currently have an opened submenu? bool m_hasOpenSubMenu; @@ -282,6 +291,9 @@ BEGIN_EVENT_TABLE(wxPopupMenuWindow, wxPopupTransientWindow) EVT_LEFT_UP(wxPopupMenuWindow::OnLeftUp) EVT_MOTION(wxPopupMenuWindow::OnMouseMove) EVT_LEAVE_WINDOW(wxPopupMenuWindow::OnMouseLeave) +#ifdef __WXMSW__ + EVT_IDLE(wxPopupMenuWindow::OnIdle) +#endif END_EVENT_TABLE() BEGIN_EVENT_TABLE(wxMenuBar, wxMenuBarBase) @@ -329,30 +341,26 @@ wxPopupMenuWindow::~wxPopupMenuWindow() void wxPopupMenuWindow::ResetCurrent() { -#if wxUSE_STL - SetCurrent(wxMenuItemList::compatibility_iterator()); -#else - SetCurrent((wxwxMenuItemListNode *)NULL); -#endif + SetCurrentItem(wxMenuItemIter()); } -void wxPopupMenuWindow::SetCurrent(wxMenuItemList::compatibility_iterator node) +void wxPopupMenuWindow::SetCurrentItem(wxMenuItemIter node) { m_nodeCurrent = node; } -void wxPopupMenuWindow::ChangeCurrent(wxMenuItemList::compatibility_iterator node) +void wxPopupMenuWindow::ChangeCurrent(wxMenuItemIter node) { - if ( node != m_nodeCurrent ) + if ( !m_nodeCurrent || !node || (node != m_nodeCurrent) ) { - wxMenuItemList::compatibility_iterator nodeOldCurrent = m_nodeCurrent; + wxMenuItemIter nodeOldCurrent = m_nodeCurrent; m_nodeCurrent = node; if ( nodeOldCurrent ) { wxMenuItem *item = nodeOldCurrent->GetData(); - wxCHECK_RET( item, _T("no current item?") ); + wxCHECK_RET( item, wxT("no current item?") ); // if it was the currently opened menu, close it if ( item->IsSubMenu() && item->GetSubMenu()->IsShown() ) @@ -369,15 +377,15 @@ void wxPopupMenuWindow::ChangeCurrent(wxMenuItemList::compatibility_iterator nod } } -wxMenuItemList::compatibility_iterator wxPopupMenuWindow::GetPrevNode() const +wxMenuItemIter wxPopupMenuWindow::GetPrevNode() const { // return the last node if there had been no previously selected one return m_nodeCurrent ? GetPrevNode(m_nodeCurrent) - : m_menu->GetMenuItems().GetLast(); + : wxMenuItemIter(m_menu->GetMenuItems().GetLast()); } -wxMenuItemList::compatibility_iterator -wxPopupMenuWindow::GetPrevNode(wxMenuItemList::compatibility_iterator node) const +wxMenuItemIter +wxPopupMenuWindow::GetPrevNode(wxMenuItemIter node) const { if ( node ) { @@ -392,15 +400,15 @@ wxPopupMenuWindow::GetPrevNode(wxMenuItemList::compatibility_iterator node) cons return node; } -wxMenuItemList::compatibility_iterator wxPopupMenuWindow::GetNextNode() const +wxMenuItemIter wxPopupMenuWindow::GetNextNode() const { // return the first node if there had been no previously selected one return m_nodeCurrent ? GetNextNode(m_nodeCurrent) - : m_menu->GetMenuItems().GetFirst(); + : wxMenuItemIter(m_menu->GetMenuItems().GetFirst()); } -wxMenuItemList::compatibility_iterator -wxPopupMenuWindow::GetNextNode(wxMenuItemList::compatibility_iterator node) const +wxMenuItemIter +wxPopupMenuWindow::GetNextNode(wxMenuItemIter node) const { if ( node ) { @@ -424,10 +432,15 @@ void wxPopupMenuWindow::Popup(wxWindow *focus) // check that the current item had been properly reset before wxASSERT_MSG( !m_nodeCurrent || m_nodeCurrent == m_menu->GetMenuItems().GetFirst(), - _T("menu current item preselected incorrectly") ); + wxT("menu current item preselected incorrectly") ); wxPopupTransientWindow::Popup(focus); + // the base class no-longer captures the mouse automatically when Popup + // is called, so do it here to allow the menu tracking to work + if ( !HasCapture() ) + CaptureMouse(); + #ifdef __WXMSW__ // ensure that this window is really on top of everything: without using // SetWindowPos() it can be covered by its parent menu which is not @@ -438,13 +451,13 @@ void wxPopupMenuWindow::Popup(wxWindow *focus) wxPopupMenuWindow *win = menuParent->m_popupMenu; // if we're shown, the parent menu must be also shown - wxCHECK_RET( win, _T("parent menu is not shown?") ); + wxCHECK_RET( win, wxT("parent menu is not shown?") ); if ( !::SetWindowPos(GetHwndOf(win), GetHwnd(), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOREDRAW) ) { - wxLogLastError(_T("SetWindowPos(HWND_TOP)")); + wxLogLastError(wxT("SetWindowPos(HWND_TOP)")); } Refresh(); @@ -457,16 +470,18 @@ void wxPopupMenuWindow::Dismiss() if ( HasOpenSubmenu() ) { wxMenuItem *item = GetCurrentItem(); - wxCHECK_RET( item && item->IsSubMenu(), _T("where is our open submenu?") ); + wxCHECK_RET( item && item->IsSubMenu(), wxT("where is our open submenu?") ); wxPopupMenuWindow *win = item->GetSubMenu()->m_popupMenu; - wxCHECK_RET( win, _T("opened submenu is not opened?") ); + wxCHECK_RET( win, wxT("opened submenu is not opened?") ); win->Dismiss(); OnSubmenuDismiss( false ); } wxPopupTransientWindow::Dismiss(); + + ResetCurrent(); } void wxPopupMenuWindow::OnDismiss() @@ -476,19 +491,13 @@ void wxPopupMenuWindow::OnDismiss() HandleDismiss(true); } -void wxPopupMenuWindow::OnSubmenuDismiss(bool dismissParent) +void wxPopupMenuWindow::OnSubmenuDismiss(bool WXUNUSED(dismissParent)) { m_hasOpenSubMenu = false; - - // we are closing whole menu so remove current highlight - if ( dismissParent ) - ResetCurrent(); } void wxPopupMenuWindow::HandleDismiss(bool dismissParent) { - ResetCurrent(); - m_menu->OnDismiss(dismissParent); } @@ -502,7 +511,7 @@ void wxPopupMenuWindow::DismissAndNotify() // wxPopupMenuWindow geometry // ---------------------------------------------------------------------------- -wxMenuItemList::compatibility_iterator +wxMenuItemIter wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint& pt) const { // we only use the y coord normally, but still check x in case the point is @@ -510,7 +519,7 @@ wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint& pt) const if ( wxWindow::HitTest(pt) == wxHT_WINDOW_INSIDE ) { wxCoord y = 0; - for ( wxMenuItemList::compatibility_iterator node = m_menu->GetMenuItems().GetFirst(); + for ( wxMenuItemIter node = m_menu->GetMenuItems().GetFirst(); node; node = node->GetNext() ) { @@ -524,11 +533,7 @@ wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint& pt) const } } -#if wxUSE_STL - return wxMenuItemList::compatibility_iterator(); -#else - return NULL; -#endif + return wxMenuItemIter(); } // ---------------------------------------------------------------------------- @@ -537,9 +542,9 @@ wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint& pt) const void wxPopupMenuWindow::RefreshItem(wxMenuItem *item) { - wxCHECK_RET( item, _T("can't refresh NULL item") ); + wxCHECK_RET( item, wxT("can't refresh NULL item") ); - wxASSERT_MSG( IsShown(), _T("can't refresh menu which is not shown") ); + wxASSERT_MSG( IsShown(), wxT("can't refresh menu which is not shown") ); // FIXME: -1 here because of SetLogicalOrigin(1, 1) in DoDraw() RefreshRect(wxRect(0, item->GetPosition() - 1, @@ -562,7 +567,7 @@ void wxPopupMenuWindow::DoDraw(wxControlRenderer *renderer) wxCoord y = 0; const wxMenuGeometryInfo& gi = m_menu->GetGeometryInfo(); - for ( wxMenuItemList::compatibility_iterator node = m_menu->GetMenuItems().GetFirst(); + for ( wxMenuItemIter node = m_menu->GetMenuItems().GetFirst(); node; node = node->GetNext() ) { @@ -614,7 +619,7 @@ void wxPopupMenuWindow::DoDraw(wxControlRenderer *renderer) dc, y, gi, - item->GetLabel(), + item->GetItemLabelText(), item->GetAccelString(), bmp, flags, @@ -632,10 +637,10 @@ void wxPopupMenuWindow::DoDraw(wxControlRenderer *renderer) void wxPopupMenuWindow::ClickItem(wxMenuItem *item) { - wxCHECK_RET( item, _T("can't click NULL item") ); + wxCHECK_RET( item, wxT("can't click NULL item") ); wxASSERT_MSG( !item->IsSeparator() && !item->IsSubMenu(), - _T("can't click this item") ); + wxT("can't click this item") ); wxMenu* menu = m_menu; @@ -647,10 +652,10 @@ void wxPopupMenuWindow::ClickItem(wxMenuItem *item) void wxPopupMenuWindow::OpenSubmenu(wxMenuItem *item, InputMethod how) { - wxCHECK_RET( item, _T("can't open NULL submenu") ); + wxCHECK_RET( item, wxT("can't open NULL submenu") ); wxMenu *submenu = item->GetSubMenu(); - wxCHECK_RET( submenu, _T("can only open submenus!") ); + wxCHECK_RET( submenu, wxT("can only open submenus!") ); // FIXME: should take into account the border width submenu->Popup(ClientToScreen(wxPoint(0, item->GetPosition())), @@ -705,7 +710,7 @@ bool wxPopupMenuWindow::ProcessLeftDown(wxMouseEvent& event) { wxPopupMenuWindow *win = menu->m_popupMenu; - wxCHECK_MSG( win, false, _T("parent menu not shown?") ); + wxCHECK_MSG( win, false, wxT("parent menu not shown?") ); pos = ClientToScreen(pos); if ( win->GetMenuItemFromPoint(win->ScreenToClient(pos)) ) @@ -722,7 +727,7 @@ bool wxPopupMenuWindow::ProcessLeftDown(wxMouseEvent& event) void wxPopupMenuWindow::OnLeftUp(wxMouseEvent& event) { - wxMenuItemList::compatibility_iterator node = GetMenuItemFromPoint(event.GetPosition()); + wxMenuItemIter node = GetMenuItemFromPoint(event.GetPosition()); if ( node ) { ActivateItem(node->GetData(), WithMouse); @@ -756,13 +761,13 @@ void wxPopupMenuWindow::OnMouseMove(wxMouseEvent& event) void wxPopupMenuWindow::ProcessMouseMove(const wxPoint& pt) { - wxMenuItemList::compatibility_iterator node = GetMenuItemFromPoint(pt); + wxMenuItemIter node = GetMenuItemFromPoint(pt); // don't reset current to NULL here, we only do it when the mouse leaves // the window (see below) if ( node ) { - if ( node != m_nodeCurrent ) + if ( !m_nodeCurrent || (node != m_nodeCurrent) ) { ChangeCurrent(node); @@ -794,7 +799,7 @@ void wxPopupMenuWindow::ProcessMouseMove(const wxPoint& pt) wxPopupMenuWindow *win = menuParent->m_popupMenu; // if we're shown, the parent menu must be also shown - wxCHECK_RET( win, _T("parent menu is not shown?") ); + wxCHECK_RET( win, wxT("parent menu is not shown?") ); win->ProcessMouseMove(win->ScreenToClient(ptScreen)); } @@ -829,10 +834,10 @@ void wxPopupMenuWindow::OnMouseLeave(wxMouseEvent& event) if ( HasOpenSubmenu() ) { wxMenuItem *item = GetCurrentItem(); - wxCHECK_RET( CanOpen(item), _T("where is our open submenu?") ); + wxCHECK_RET( CanOpen(item), wxT("where is our open submenu?") ); wxPopupMenuWindow *win = item->GetSubMenu()->m_popupMenu; - wxCHECK_RET( win, _T("submenu is opened but not shown?") ); + wxCHECK_RET( win, wxT("submenu is opened but not shown?") ); // only handle this event if the mouse is not inside the submenu wxPoint pt = ClientToScreen(event.GetPosition()); @@ -847,11 +852,7 @@ void wxPopupMenuWindow::OnMouseLeave(wxMouseEvent& event) if ( resetCurrent ) { -#if wxUSE_STL - ChangeCurrent(wxMenuItemList::compatibility_iterator()); -#else - ChangeCurrent(NULL); -#endif + ChangeCurrent(wxMenuItemIter()); } } @@ -860,7 +861,13 @@ void wxPopupMenuWindow::OnMouseLeave(wxMouseEvent& event) void wxPopupMenuWindow::OnKeyDown(wxKeyEvent& event) { - if ( !ProcessKeyDown(event.GetKeyCode()) ) + wxMenuBar *menubar = m_menu->GetMenuBar(); + + if ( menubar ) + { + menubar->ProcessEvent(event); + } + else if ( !ProcessKeyDown(event.GetKeyCode()) ) { event.Skip(); } @@ -876,7 +883,7 @@ bool wxPopupMenuWindow::ProcessKeyDown(int key) if ( HasOpenSubmenu() ) { wxCHECK_MSG( CanOpen(item), false, - _T("has open submenu but another item selected?") ); + wxT("has open submenu but another item selected?") ); if ( item->GetSubMenu()->ProcessKeyDown(key) ) return true; @@ -923,9 +930,8 @@ bool wxPopupMenuWindow::ProcessKeyDown(int key) { bool up = key == WXK_UP; - wxMenuItemList::compatibility_iterator nodeStart = up ? GetPrevNode() - : GetNextNode(), - node = nodeStart; + wxMenuItemIter nodeStart = up ? GetPrevNode() : GetNextNode(), + node = nodeStart; while ( node && node->GetData()->IsSeparator() ) { node = up ? GetPrevNode(node) : GetNextNode(node); @@ -934,11 +940,7 @@ bool wxPopupMenuWindow::ProcessKeyDown(int key) { // nothing but separators and disabled items in this // menu, break out -#if wxUSE_STL - node = wxMenuItemList::compatibility_iterator(); -#else - node = NULL; -#endif + node = wxMenuItemIter(); } } @@ -972,7 +974,7 @@ bool wxPopupMenuWindow::ProcessKeyDown(int key) // we want to start from the item after this one because // if we're already on the item with the given accel we want to // go to the next one, not to stay in place - wxMenuItemList::compatibility_iterator nodeStart = GetNextNode(); + wxMenuItemIter nodeStart = GetNextNode(); // do we have more than one item with this accel? bool notUnique = false; @@ -982,19 +984,15 @@ bool wxPopupMenuWindow::ProcessKeyDown(int key) // loop through all items searching for the item with this // accel - wxMenuItemList::compatibility_iterator node = nodeStart, -#if wxUSE_STL - nodeFound = wxMenuItemList::compatibility_iterator(); -#else - nodeFound = NULL; -#endif + wxMenuItemIter nodeFound, + node = nodeStart; for ( ;; ) { item = node->GetData(); int idxAccel = item->GetAccelIndex(); if ( idxAccel != -1 && - wxTolower(item->GetLabel()[(size_t)idxAccel]) + (wxChar)wxTolower(item->GetItemLabelText()[(size_t)idxAccel]) == chAccel ) { // ok, found an item with this accel @@ -1089,7 +1087,7 @@ const wxMenuGeometryInfo& wxMenu::GetGeometryInfo() const } else { - wxFAIL_MSG( _T("can't get geometry without window") ); + wxFAIL_MSG( wxT("can't get geometry without window") ); } } @@ -1150,7 +1148,7 @@ wxMenuItem* wxMenu::DoAppend(wxMenuItem *item) { // we need to update its end item item->SetRadioGroupStart(m_startRadioGroup); - wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup); + wxMenuItemIter node = GetMenuItems().Item(m_startRadioGroup); if ( node ) { @@ -1158,7 +1156,7 @@ wxMenuItem* wxMenu::DoAppend(wxMenuItem *item) } else { - wxFAIL_MSG( _T("where is the radio group start item?") ); + wxFAIL_MSG( wxT("where is the radio group start item?") ); } } } @@ -1209,7 +1207,7 @@ void wxMenu::Attach(wxMenuBarBase *menubar) { wxMenuBase::Attach(menubar); - wxCHECK_RET( m_menuBar, _T("menubar can't be NULL after attaching") ); + wxCHECK_RET( m_menuBar, wxT("menubar can't be NULL after attaching") ); // unfortunately, we can't use m_menuBar->GetEventHandler() here because, // if the menubar is currently showing a menu, its event handler is a @@ -1265,7 +1263,7 @@ wxWindow *wxMenu::GetRootWindow() const // we're probably going to crash in the caller anyhow, but try to detect // this error as soon as possible - wxASSERT_MSG( win, _T("menu without any associated window?") ); + wxASSERT_MSG( win, wxT("menu without any associated window?") ); // also remember it in this menu so that we don't have to search for it the // next time @@ -1277,7 +1275,7 @@ wxWindow *wxMenu::GetRootWindow() const wxRenderer *wxMenu::GetRenderer() const { // we're going to crash without renderer! - wxCHECK_MSG( m_popupMenu, NULL, _T("neither popup nor menubar menu?") ); + wxCHECK_MSG( m_popupMenu, NULL, wxT("neither popup nor menubar menu?") ); return m_popupMenu->GetRenderer(); } @@ -1290,7 +1288,7 @@ void wxMenu::RefreshItem(wxMenuItem *item) if ( IsShown() ) { // this would be a bug in IsShown() - wxCHECK_RET( m_popupMenu, _T("must have popup window if shown!") ); + wxCHECK_RET( m_popupMenu, wxT("must have popup window if shown!") ); // recalc geometry to update the item height and such (void)GetGeometryInfo(); @@ -1320,7 +1318,7 @@ void wxMenu::OnDismiss(bool dismissParent) } else { - wxFAIL_MSG( _T("parent menu not shown?") ); + wxFAIL_MSG( wxT("parent menu not shown?") ); } // and if we dismiss everything, propagate to parent @@ -1340,7 +1338,7 @@ void wxMenu::OnDismiss(bool dismissParent) } else // popup menu { - wxCHECK_RET( m_invokingWindow, _T("what kind of menu is this?") ); + wxCHECK_RET( m_invokingWindow, wxT("what kind of menu is this?") ); m_invokingWindow->DismissPopupMenu(); @@ -1375,7 +1373,7 @@ void wxMenu::Popup(const wxPoint& pos, const wxSize& size, bool selectFirst) // always keep the focus at the originating window wxWindow *focus = GetRootWindow(); - wxASSERT_MSG( focus, _T("no window to keep focus on?") ); + wxASSERT_MSG( focus, wxT("no window to keep focus on?") ); // and show it m_popupMenu->Popup(focus); @@ -1383,7 +1381,7 @@ void wxMenu::Popup(const wxPoint& pos, const wxSize& size, bool selectFirst) void wxMenu::Dismiss() { - wxCHECK_RET( IsShown(), _T("can't dismiss hidden menu") ); + wxCHECK_RET( IsShown(), wxT("can't dismiss hidden menu") ); m_popupMenu->Dismiss(); } @@ -1395,7 +1393,7 @@ void wxMenu::Dismiss() bool wxMenu::ProcessKeyDown(int key) { wxCHECK_MSG( m_popupMenu, false, - _T("can't process key events if not shown") ); + wxT("can't process key events if not shown") ); return m_popupMenu->ProcessKeyDown(key); } @@ -1435,7 +1433,7 @@ bool wxMenu::ProcessAccelEvent(const wxKeyEvent& event) } // try our submenus - for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst(); + for ( wxMenuItemIter node = GetMenuItems().GetFirst(); node; node = node->GetNext() ) { @@ -1521,12 +1519,6 @@ wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu, return new wxMenuItem(parentMenu, id, name, help, kind, subMenu); } -/* static */ -wxString wxMenuItemBase::GetLabelFromText(const wxString& text) -{ - return wxStripMenuCodes(text); -} - // ---------------------------------------------------------------------------- // wxMenuItem operations // ---------------------------------------------------------------------------- @@ -1541,15 +1533,16 @@ void wxMenuItem::UpdateAccelInfo() m_indexAccel = wxControl::FindAccelIndex(m_text); // will be empty if the text contains no TABs - ok - m_strAccel = m_text.AfterFirst(_T('\t')); + m_strAccel = m_text.AfterFirst(wxT('\t')); } -void wxMenuItem::SetText(const wxString& text) +void wxMenuItem::SetItemLabel(const wxString& text) { if ( text != m_text ) { // first call the base class version to change m_text - wxMenuItemBase::SetText(text); + // (and also check if we don't have a stock menu item) + wxMenuItemBase::SetItemLabel(text); UpdateAccelInfo(); @@ -1603,7 +1596,7 @@ void wxMenuItem::Check(bool check) const wxMenuItemList& items = m_parentMenu->GetMenuItems(); int pos = items.IndexOf(this); wxCHECK_RET( pos != wxNOT_FOUND, - _T("menuitem not found in the menu items list?") ); + wxT("menuitem not found in the menu items list?") ); // get the radio group range int start, @@ -1623,7 +1616,7 @@ void wxMenuItem::Check(bool check) } // also uncheck all the other items in this radio group - wxMenuItemList::compatibility_iterator node = items.Item(start); + wxMenuItemIter node = items.Item(start); for ( int n = start; n <= end && node; n++ ) { if ( n != pos ) @@ -1650,7 +1643,7 @@ void wxMenuItem::SetAsRadioGroupStart() void wxMenuItem::SetRadioGroupStart(int start) { wxASSERT_MSG( !m_isRadioGroupStart, - _T("should only be called for the next radio items") ); + wxT("should only be called for the next radio items") ); m_radioGroup.start = start; } @@ -1658,7 +1651,7 @@ void wxMenuItem::SetRadioGroupStart(int start) void wxMenuItem::SetRadioGroupEnd(int end) { wxASSERT_MSG( m_isRadioGroupStart, - _T("should only be called for the first radio item") ); + wxT("should only be called for the first radio item") ); m_radioGroup.end = end; } @@ -1689,7 +1682,7 @@ wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long WX void wxMenuBar::Attach(wxFrame *frame) { // maybe you really wanted to call Detach()? - wxCHECK_RET( frame, _T("wxMenuBar::Attach(NULL) called") ); + wxCHECK_RET( frame, wxT("wxMenuBar::Attach(NULL) called") ); wxMenuBarBase::Attach(frame); @@ -1807,7 +1800,7 @@ wxCoord wxMenuBar::GetItemWidth(size_t pos) const void wxMenuBar::EnableTop(size_t pos, bool enable) { - wxCHECK_RET( pos < GetCount(), _T("invalid index in EnableTop") ); + wxCHECK_RET( pos < GetCount(), wxT("invalid index in EnableTop") ); if ( enable != m_menuInfos[pos].IsEnabled() ) { @@ -1820,16 +1813,16 @@ void wxMenuBar::EnableTop(size_t pos, bool enable) bool wxMenuBar::IsEnabledTop(size_t pos) const { - wxCHECK_MSG( pos < GetCount(), false, _T("invalid index in IsEnabledTop") ); + wxCHECK_MSG( pos < GetCount(), false, wxT("invalid index in IsEnabledTop") ); return m_menuInfos[pos].IsEnabled(); } -void wxMenuBar::SetLabelTop(size_t pos, const wxString& label) +void wxMenuBar::SetMenuLabel(size_t pos, const wxString& label) { - wxCHECK_RET( pos < GetCount(), _T("invalid index in EnableTop") ); + wxCHECK_RET( pos < GetCount(), wxT("invalid index in SetMenuLabel") ); - if ( label != m_menuInfos[pos].GetLabel() ) + if ( label != m_menuInfos[pos].GetOriginalLabel() ) { m_menuInfos[pos].SetLabel(label); @@ -1838,11 +1831,11 @@ void wxMenuBar::SetLabelTop(size_t pos, const wxString& label) //else: nothing to do } -wxString wxMenuBar::GetLabelTop(size_t pos) const +wxString wxMenuBar::GetMenuLabel(size_t pos) const { - wxCHECK_MSG( pos < GetCount(), wxEmptyString, _T("invalid index in GetLabelTop") ); + wxCHECK_MSG( pos < GetCount(), wxEmptyString, wxT("invalid index in GetMenuLabel") ); - return m_menuInfos[pos].GetLabel(); + return m_menuInfos[pos].GetOriginalLabel(); } // ---------------------------------------------------------------------------- @@ -1865,7 +1858,7 @@ void wxMenuBar::RefreshAllItemsAfter(size_t pos) void wxMenuBar::RefreshItem(size_t pos) { wxCHECK_RET( pos != (size_t)-1, - _T("invalid item in wxMenuBar::RefreshItem") ); + wxT("invalid item in wxMenuBar::RefreshItem") ); if ( !IsCreated() ) { @@ -1940,8 +1933,8 @@ void wxMenuBar::DoDraw(wxControlRenderer *renderer) wxRect wxMenuBar::GetItemRect(size_t pos) const { - wxASSERT_MSG( pos < GetCount(), _T("invalid menu bar item index") ); - wxASSERT_MSG( IsCreated(), _T("can't call this method yet") ); + wxASSERT_MSG( pos < GetCount(), wxT("invalid menu bar item index") ); + wxASSERT_MSG( IsCreated(), wxT("can't call this method yet") ); wxRect rect; rect.x = @@ -1965,7 +1958,7 @@ wxSize wxMenuBar::DoGetBestClientSize() const { wxClientDC dc(wxConstCast(this, wxMenuBar)); dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); - dc.GetTextExtent(GetLabelTop(0), &size.x, &size.y); + dc.GetTextExtent(GetMenuLabel(0), &size.x, &size.y); // adjust for the renderer we use size = GetRenderer()->GetMenuBarItemSize(size); @@ -2011,7 +2004,7 @@ int wxMenuBar::GetMenuFromPoint(const wxPoint& pos) const void wxMenuBar::SelectMenu(size_t pos) { SetFocus(); - wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::SelectMenu")); + wxLogTrace(wxT("mousecapture"), wxT("Capturing mouse from wxMenuBar::SelectMenu")); CaptureMouse(); DoSelectMenu(pos); @@ -2019,7 +2012,7 @@ void wxMenuBar::SelectMenu(size_t pos) void wxMenuBar::DoSelectMenu(size_t pos) { - wxCHECK_RET( pos < GetCount(), _T("invalid menu index in DoSelectMenu") ); + wxCHECK_RET( pos < GetCount(), wxT("invalid menu index in DoSelectMenu") ); int posOld = m_current; @@ -2047,7 +2040,7 @@ void wxMenuBar::DoSelectMenu(size_t pos) void wxMenuBar::PopupMenu(size_t pos) { - wxCHECK_RET( pos < GetCount(), _T("invalid menu index in PopupCurrentMenu") ); + wxCHECK_RET( pos < GetCount(), wxT("invalid menu index in PopupCurrentMenu") ); SetFocus(); DoSelectMenu(pos); @@ -2100,7 +2093,7 @@ void wxMenuBar::OnLeftDown(wxMouseEvent& event) } else // on item { - wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::OnLeftDown")); + wxLogTrace(wxT("mousecapture"), wxT("Capturing mouse from wxMenuBar::OnLeftDown")); CaptureMouse(); // show it as selected @@ -2146,9 +2139,6 @@ bool wxMenuBar::ProcessMouseEvent(const wxPoint& pt) return false; } - // FIXME: temporary workaround for crash, to be fixed - // in a later version. -#if 0 // select the new active item DoSelectMenu(currentNew); @@ -2159,7 +2149,6 @@ bool wxMenuBar::ProcessMouseEvent(const wxPoint& pt) // open the new menu if the old one we closed had been opened PopupCurrentMenu(false /* don't select first item - as Windows does */); } -#endif return true; } @@ -2179,7 +2168,7 @@ void wxMenuBar::OnKeyDown(wxKeyEvent& event) { // we always maintain a valid current item while we're in modal // state (i.e. have the capture) - wxFAIL_MSG( _T("how did we manage to lose current item?") ); + wxFAIL_MSG( wxT("how did we manage to lose current item?") ); return; } @@ -2339,8 +2328,7 @@ int wxMenuBar::FindNextItemForAccel(int idxStart, int key, bool *unique) const int idxAccel = info.GetAccelIndex(); if ( idxAccel != -1 && - wxTolower(info.GetLabel()[(size_t)idxAccel]) - == chAccel ) + (wxChar)wxTolower(info.GetLabel()[(size_t)idxAccel]) == chAccel ) { // ok, found an item with this accel if ( idxFound == -1 ) @@ -2411,10 +2399,10 @@ bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent& event) void wxMenuBar::PopupCurrentMenu(bool selectFirst) { - wxCHECK_RET( m_current != -1, _T("no menu to popup") ); + wxCHECK_RET( m_current != -1, wxT("no menu to popup") ); // forgot to call DismissMenu()? - wxASSERT_MSG( !m_menuShown, _T("shouldn't show two menus at once!") ); + wxASSERT_MSG( !m_menuShown, wxT("shouldn't show two menus at once!") ); // in any case, we should show it - even if we won't m_shouldShowMenu = true; @@ -2449,7 +2437,7 @@ void wxMenuBar::PopupCurrentMenu(bool selectFirst) void wxMenuBar::DismissMenu() { - wxCHECK_RET( m_menuShown, _T("can't dismiss menu if none is shown") ); + wxCHECK_RET( m_menuShown, wxT("can't dismiss menu if none is shown") ); m_menuShown->Dismiss(); OnDismissMenu(); @@ -2467,10 +2455,9 @@ void wxMenuBar::OnDismissMenu(bool dismissMenuBar) void wxMenuBar::OnDismiss() { - if ( GetCapture() ) + if ( ReleaseMouseCapture() ) { - wxLogTrace(_T("mousecapture"), _T("Releasing mouse from wxMenuBar::OnDismiss")); - GetCapture()->ReleaseMouse(); + wxLogTrace(wxT("mousecapture"), wxT("Releasing mouse from wxMenuBar::OnDismiss")); } if ( m_current != -1 ) @@ -2484,6 +2471,42 @@ void wxMenuBar::OnDismiss() GiveAwayFocus(); } +bool wxMenuBar::ReleaseMouseCapture() +{ +#ifdef __WXX11__ + // With wxX11, when a menu is closed by clicking away from it, a control + // under the click will still get an event, even though the menu has the + // capture (bug?). So that control may already have taken the capture by + // this point, preventing us from releasing the menu's capture. So to work + // around this, we release both captures, then put back the control's + // capture. + wxWindow *capture = GetCapture(); + if ( capture ) + { + capture->ReleaseMouse(); + + if ( capture == this ) + return true; + + bool had = HasCapture(); + + if ( had ) + ReleaseMouse(); + + capture->CaptureMouse(); + + return had; + } +#else + if ( HasCapture() ) + { + ReleaseMouse(); + return true; + } +#endif + return false; +} + void wxMenuBar::GiveAwayFocus() { GetFrame()->SetFocus(); @@ -2498,7 +2521,7 @@ wxEventLoop *wxWindow::ms_evtLoopPopup = NULL; bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y) { wxCHECK_MSG( !ms_evtLoopPopup, false, - _T("can't show more than one popup menu at a time") ); + wxT("can't show more than one popup menu at a time") ); #ifdef __WXMSW__ // we need to change the cursor before showing the menu as, apparently, no @@ -2560,10 +2583,9 @@ bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y) void wxWindow::DismissPopupMenu() { - wxCHECK_RET( ms_evtLoopPopup, _T("no popup menu shown") ); + wxCHECK_RET( ms_evtLoopPopup, wxT("no popup menu shown") ); ms_evtLoopPopup->Exit(); } #endif // wxUSE_MENUS -