X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/78f933650057d0e09ee06b59f7d23cb83b48e1b3..06d7ed27a19281549a8293d26cb44dd2d45055ea:/src/univ/menu.cpp diff --git a/src/univ/menu.cpp b/src/univ/menu.cpp index 22949bed1d..ac4d815131 100644 --- a/src/univ/menu.cpp +++ b/src/univ/menu.cpp @@ -25,19 +25,21 @@ #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 #include "wx/popupwin.h" #include "wx/evtloop.h" -#include "wx/dcclient.h" -#include "wx/frame.h" #include "wx/univ/renderer.h" @@ -45,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 // ---------------------------------------------------------------------------- @@ -63,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); @@ -75,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 { @@ -101,6 +108,7 @@ private: } wxString m_label; + wxString m_originalLabel; wxCoord m_width; int m_indexAccel; bool m_isEnabled; @@ -119,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); @@ -127,9 +135,6 @@ 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); @@ -147,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); @@ -172,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); @@ -184,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 @@ -219,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; @@ -334,23 +341,19 @@ 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; @@ -374,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 ) { @@ -397,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 ) { @@ -508,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 @@ -516,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() ) { @@ -530,11 +533,7 @@ wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint& pt) const } } -#if wxUSE_STL - return wxMenuItemList::compatibility_iterator(); -#else - return NULL; -#endif + return wxMenuItemIter(); } // ---------------------------------------------------------------------------- @@ -568,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() ) { @@ -620,7 +619,7 @@ void wxPopupMenuWindow::DoDraw(wxControlRenderer *renderer) dc, y, gi, - item->GetLabel(), + item->GetItemLabelText(), item->GetAccelString(), bmp, flags, @@ -728,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); @@ -762,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); @@ -853,11 +852,7 @@ void wxPopupMenuWindow::OnMouseLeave(wxMouseEvent& event) if ( resetCurrent ) { -#if wxUSE_STL - ChangeCurrent(wxMenuItemList::compatibility_iterator()); -#else - ChangeCurrent(NULL); -#endif + ChangeCurrent(wxMenuItemIter()); } } @@ -935,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); @@ -946,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(); } } @@ -984,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; @@ -994,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 @@ -1162,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 ) { @@ -1447,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() ) { @@ -1534,7 +1520,7 @@ wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu, } /* static */ -wxString wxMenuItemBase::GetLabelFromText(const wxString& text) +wxString wxMenuItemBase::GetLabelText(const wxString& text) { return wxStripMenuCodes(text); } @@ -1556,12 +1542,13 @@ void wxMenuItem::UpdateAccelInfo() m_strAccel = m_text.AfterFirst(_T('\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(); @@ -1635,7 +1622,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 ) @@ -1837,11 +1824,11 @@ bool wxMenuBar::IsEnabledTop(size_t pos) const 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(), _T("invalid index in SetMenuLabel") ); - if ( label != m_menuInfos[pos].GetLabel() ) + if ( label != m_menuInfos[pos].GetOriginalLabel() ) { m_menuInfos[pos].SetLabel(label); @@ -1850,11 +1837,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, _T("invalid index in GetMenuLabel") ); - return m_menuInfos[pos].GetLabel(); + return m_menuInfos[pos].GetOriginalLabel(); } // ---------------------------------------------------------------------------- @@ -1977,7 +1964,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); @@ -2347,8 +2334,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 )