X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e441e1f4e8671915ee9bb32049edb4f1d700126e..b74077ace2413000c5556cbe0c41ab210ac12c54:/src/univ/menu.cpp diff --git a/src/univ/menu.cpp b/src/univ/menu.cpp index f760357497..ac4d815131 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); @@ -75,11 +76,12 @@ public: m_width = 0; } - void SetEnabled(bool enabled = TRUE) { m_isEnabled = enabled; } + void SetEnabled(bool enabled = true) { m_isEnabled = enabled; } // 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,11 +135,15 @@ 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() { m_hasOpenSubMenu = FALSE; } + 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,15 +152,15 @@ 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 + // process the key event, return true if done bool ProcessKeyDown(int key); // process mouse move event @@ -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,15 +192,14 @@ 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 + // on what it is, return true if something was done (i.e. it's not a // separator...) bool ActivateItem(wxMenuItem *item, InputMethod how = WithKeyboard); @@ -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; @@ -258,7 +267,7 @@ public: } else { - // return FALSE; + // return false; return wxEvtHandler::ProcessEvent(event); } @@ -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) @@ -304,7 +316,7 @@ END_EVENT_TABLE() wxPopupMenuWindow::wxPopupMenuWindow(wxWindow *parent, wxMenu *menu) { m_menu = menu; - m_hasOpenSubMenu = FALSE; + m_hasOpenSubMenu = false; ResetCurrent(); @@ -329,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; @@ -358,7 +366,7 @@ void wxPopupMenuWindow::ChangeCurrent(wxMenuItemList::compatibility_iterator nod if ( item->IsSubMenu() && item->GetSubMenu()->IsShown() ) { item->GetSubMenu()->Dismiss(); - OnSubmenuDismiss(); + OnSubmenuDismiss( false ); } RefreshItem(item); @@ -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 ) { @@ -428,6 +436,11 @@ void wxPopupMenuWindow::Popup(wxWindow *focus) 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 @@ -463,37 +476,42 @@ void wxPopupMenuWindow::Dismiss() wxCHECK_RET( win, _T("opened submenu is not opened?") ); win->Dismiss(); - OnSubmenuDismiss(); + OnSubmenuDismiss( false ); } wxPopupTransientWindow::Dismiss(); + + ResetCurrent(); } void wxPopupMenuWindow::OnDismiss() { // when we are dismissed because the user clicked elsewhere or we lost // focus in any other way, hide the parent menu as well - HandleDismiss(TRUE); + HandleDismiss(true); } -void wxPopupMenuWindow::HandleDismiss(bool dismissParent) +void wxPopupMenuWindow::OnSubmenuDismiss(bool WXUNUSED(dismissParent)) { - ResetCurrent(); + m_hasOpenSubMenu = false; +} +void wxPopupMenuWindow::HandleDismiss(bool dismissParent) +{ m_menu->OnDismiss(dismissParent); } void wxPopupMenuWindow::DismissAndNotify() { Dismiss(); - HandleDismiss(TRUE); + HandleDismiss(true); } // ---------------------------------------------------------------------------- // 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 @@ -501,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() ) { @@ -515,11 +533,7 @@ wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint& pt) const } } -#if wxUSE_STL - return wxMenuItemList::compatibility_iterator(); -#else - return NULL; -#endif + return wxMenuItemIter(); } // ---------------------------------------------------------------------------- @@ -553,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() ) { @@ -585,17 +599,29 @@ void wxPopupMenuWindow::DoDraw(wxControlRenderer *renderer) if ( item == GetCurrentItem() ) flags |= wxCONTROL_SELECTED; + wxBitmap bmp; + + if ( !item->IsEnabled() ) + { + bmp = item->GetDisabledBitmap(); + } + + if ( !bmp.Ok() ) + { + // strangely enough, for unchecked item we use the + // "checked" bitmap because this is the default one - this + // explains this strange boolean expression + bmp = item->GetBitmap(!item->IsCheckable() || item->IsChecked()); + } + rend->DrawMenuItem ( dc, y, gi, - item->GetLabel(), + item->GetItemLabelText(), item->GetAccelString(), - // strangely enough, for unchecked item we use the - // "checked" bitmap because this is the default one - this - // explains this strange boolean expression - item->GetBitmap(!item->IsCheckable() || item->IsChecked()), + bmp, flags, item->GetAccelIndex() ); @@ -636,7 +662,7 @@ void wxPopupMenuWindow::OpenSubmenu(wxMenuItem *item, InputMethod how) wxSize(m_menu->GetGeometryInfo().GetSize().x, 0), how == WithKeyboard /* preselect first item then */); - m_hasOpenSubMenu = TRUE; + m_hasOpenSubMenu = true; } bool wxPopupMenuWindow::ActivateItem(wxMenuItem *item, InputMethod how) @@ -644,7 +670,7 @@ bool wxPopupMenuWindow::ActivateItem(wxMenuItem *item, InputMethod how) // don't activate disabled items if ( !item || !item->IsEnabled() ) { - return FALSE; + return false; } // normal menu items generate commands, submenus can be opened and @@ -659,10 +685,10 @@ bool wxPopupMenuWindow::ActivateItem(wxMenuItem *item, InputMethod how) } else // separator, can't activate { - return FALSE; + return false; } - return TRUE; + return true; } // ---------------------------------------------------------------------------- @@ -684,24 +710,24 @@ bool wxPopupMenuWindow::ProcessLeftDown(wxMouseEvent& event) { wxPopupMenuWindow *win = menu->m_popupMenu; - wxCHECK_MSG( win, FALSE, _T("parent menu not shown?") ); + wxCHECK_MSG( win, false, _T("parent menu not shown?") ); pos = ClientToScreen(pos); if ( win->GetMenuItemFromPoint(win->ScreenToClient(pos)) ) { // eat the event - return TRUE; + return true; } //else: it is outside the parent menu as well, do dismiss this one } } - return FALSE; + return false; } void wxPopupMenuWindow::OnLeftUp(wxMouseEvent& event) { - wxMenuItemList::compatibility_iterator node = GetMenuItemFromPoint(event.GetPosition()); + wxMenuItemIter node = GetMenuItemFromPoint(event.GetPosition()); if ( node ) { ActivateItem(node->GetData(), WithMouse); @@ -735,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); @@ -821,16 +847,12 @@ void wxPopupMenuWindow::OnMouseLeave(wxMouseEvent& event) else { // this menu is the last opened - resetCurrent = TRUE; + resetCurrent = true; } if ( resetCurrent ) { -#if wxUSE_STL - ChangeCurrent(wxMenuItemList::compatibility_iterator()); -#else - ChangeCurrent(NULL); -#endif + ChangeCurrent(wxMenuItemIter()); } } @@ -839,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(); } @@ -854,14 +882,14 @@ bool wxPopupMenuWindow::ProcessKeyDown(int key) // to open it inspit of this) if ( HasOpenSubmenu() ) { - wxCHECK_MSG( CanOpen(item), FALSE, + wxCHECK_MSG( CanOpen(item), false, _T("has open submenu but another item selected?") ); if ( item->GetSubMenu()->ProcessKeyDown(key) ) - return TRUE; + return true; } - bool processed = TRUE; + bool processed = true; // handle the up/down arrows, home, end, esc and return here, pass the // left/right arrows to the menu bar except when the right arrow can be @@ -873,7 +901,7 @@ bool wxPopupMenuWindow::ProcessKeyDown(int key) // menubar if ( !m_menu->GetParent() ) { - processed = FALSE; + processed = false; break; } @@ -882,7 +910,7 @@ bool wxPopupMenuWindow::ProcessKeyDown(int key) case WXK_ESCAPE: // close just this menu Dismiss(); - HandleDismiss(FALSE); + HandleDismiss(false); break; case WXK_RETURN: @@ -902,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); @@ -913,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(); } } @@ -927,7 +950,7 @@ bool wxPopupMenuWindow::ProcessKeyDown(int key) } else { - processed = FALSE; + processed = false; } } break; @@ -940,40 +963,36 @@ bool wxPopupMenuWindow::ProcessKeyDown(int key) } else { - processed = FALSE; + processed = false; } break; default: // look for the menu item starting with this letter - if ( wxIsalnum(key) ) + if ( wxIsalnum((wxChar)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; + bool notUnique = false; // translate everything to lower case before comparing - wxChar chAccel = wxTolower(key); + wxChar chAccel = (wxChar)wxTolower(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 @@ -986,7 +1005,7 @@ bool wxPopupMenuWindow::ProcessKeyDown(int key) } else // we already had found such item { - notUnique = TRUE; + notUnique = true; // no need to continue further, we won't find // anything we don't already know @@ -1019,12 +1038,12 @@ bool wxPopupMenuWindow::ProcessKeyDown(int key) //else: just select it but don't activate as the user might // have wanted to activate another item - // skip "processed = FALSE" below + // skip "processed = false" below break; } } - processed = FALSE; + processed = false; } return processed; @@ -1110,13 +1129,8 @@ void wxMenu::EndRadioGroup() m_startRadioGroup = -1; } -bool wxMenu::DoAppend(wxMenuItem *item) +wxMenuItem* wxMenu::DoAppend(wxMenuItem *item) { - #if 0 - // not used at all - bool check = FALSE; - #endif - if ( item->GetKind() == wxITEM_RADIO ) { int count = GetMenuItemCount(); @@ -1129,18 +1143,12 @@ bool wxMenu::DoAppend(wxMenuItem *item) // for now it has just one element item->SetAsRadioGroupStart(); item->SetRadioGroupEnd(m_startRadioGroup); - - // ensure that we have a checked item in the radio group - #if 0 - // not used at all - check = TRUE; - #endif } else // extend the current radio group { // 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,21 +1166,21 @@ bool wxMenu::DoAppend(wxMenuItem *item) } if ( !wxMenuBase::DoAppend(item) ) - return FALSE; + return NULL; OnItemAdded(item); - return TRUE; + return item; } -bool wxMenu::DoInsert(size_t pos, wxMenuItem *item) +wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item) { if ( !wxMenuBase::DoInsert(pos, item) ) - return FALSE; + return NULL; OnItemAdded(item); - return TRUE; + return item; } wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) @@ -1223,10 +1231,10 @@ void wxMenu::Detach() wxWindow *wxMenu::GetRootWindow() const { - if ( m_menuBar ) + if ( GetMenuBar() ) { // simple case - a normal menu attached to the menubar - return m_menuBar; + return GetMenuBar(); } // we're a popup menu but the trouble is that only the top level popup menu @@ -1306,7 +1314,7 @@ void wxMenu::OnDismiss(bool dismissParent) wxPopupMenuWindow *win = m_menuParent->m_popupMenu; if ( win ) { - win->OnSubmenuDismiss(); + win->OnSubmenuDismiss( true ); } else { @@ -1318,7 +1326,7 @@ void wxMenu::OnDismiss(bool dismissParent) { // dismissParent is recursive m_menuParent->Dismiss(); - m_menuParent->OnDismiss(TRUE); + m_menuParent->OnDismiss(true); } } else // no parent menu @@ -1384,7 +1392,7 @@ void wxMenu::Dismiss() bool wxMenu::ProcessKeyDown(int key) { - wxCHECK_MSG( m_popupMenu, FALSE, + wxCHECK_MSG( m_popupMenu, false, _T("can't process key events if not shown") ); return m_popupMenu->ProcessKeyDown(key); @@ -1425,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() ) { @@ -1435,12 +1443,12 @@ bool wxMenu::ProcessAccelEvent(const wxKeyEvent& event) // try its elements if ( item->GetSubMenu()->ProcessAccelEvent(event) ) { - return TRUE; + return true; } } } - return FALSE; + return false; } void wxMenu::AddAccelFor(wxMenuItem *item) @@ -1482,10 +1490,12 @@ wxMenuItem::wxMenuItem(wxMenu *parentMenu, : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu) { m_posY = - m_height = -1; + m_height = wxDefaultCoord; m_radioGroup.start = -1; - m_isRadioGroupStart = FALSE; + m_isRadioGroupStart = false; + + m_bmpDisabled = wxNullBitmap; UpdateAccelInfo(); } @@ -1510,7 +1520,7 @@ wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu, } /* static */ -wxString wxMenuItemBase::GetLabelFromText(const wxString& text) +wxString wxMenuItemBase::GetLabelText(const wxString& text) { return wxStripMenuCodes(text); } @@ -1532,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(); @@ -1611,12 +1622,12 @@ 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 ) { - node->GetData()->m_isChecked = FALSE; + node->GetData()->m_isChecked = false; } node = node->GetNext(); } @@ -1632,7 +1643,7 @@ void wxMenuItem::Check(bool check) void wxMenuItem::SetAsRadioGroupStart() { - m_isRadioGroupStart = TRUE; + m_isRadioGroupStart = true; } void wxMenuItem::SetRadioGroupStart(int start) @@ -1663,7 +1674,15 @@ void wxMenuBar::Init() m_menuShown = NULL; - m_shouldShowMenu = FALSE; + m_shouldShowMenu = false; +} + +wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long WXUNUSED(style)) +{ + Init(); + + for (size_t i = 0; i < n; ++i ) + Append(menus[i], titles[i]); } void wxMenuBar::Attach(wxFrame *frame) @@ -1687,14 +1706,14 @@ void wxMenuBar::Attach(wxFrame *frame) else // not created yet, do it now { // we have no way to return the error from here anyhow :-( - (void)Create(frame, -1); + (void)Create(frame, wxID_ANY); SetCursor(wxCURSOR_ARROW); SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT)); // calculate and set our height (it won't be changed any more) - SetSize(-1, GetBestSize().y); + SetSize(wxDefaultCoord, GetBestSize().y); } // remember the last frame which had us to avoid unnecessarily reparenting @@ -1729,14 +1748,14 @@ bool wxMenuBar::Append(wxMenu *menu, const wxString& title) bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) { if ( !wxMenuBarBase::Insert(pos, menu, title) ) - return FALSE; + return false; wxMenuInfo *info = new wxMenuInfo(title); m_menuInfos.Insert(info, pos); RefreshAllItemsAfter(pos); - return TRUE; + return true; } wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) @@ -1800,16 +1819,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, _T("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(), _T("invalid index in SetMenuLabel") ); - if ( label != m_menuInfos[pos].GetLabel() ) + if ( label != m_menuInfos[pos].GetOriginalLabel() ) { m_menuInfos[pos].SetLabel(label); @@ -1818,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(), _T(""), _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(); } // ---------------------------------------------------------------------------- @@ -1945,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); @@ -2011,7 +2030,7 @@ void wxMenuBar::DoSelectMenu(size_t pos) if ( IsShowingMenu() ) { // restore m_shouldShowMenu flag after DismissMenu() which resets - // it to FALSE + // it to false bool old = m_shouldShowMenu; DismissMenu(); @@ -2080,14 +2099,14 @@ void wxMenuBar::OnLeftDown(wxMouseEvent& event) } else // on item { - wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::OnLeftDown")); + wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::OnLeftDown")); CaptureMouse(); // show it as selected RefreshItem((size_t)m_current); // show the menu - PopupCurrentMenu(FALSE /* don't select first item - as Windows does */); + PopupCurrentMenu(false /* don't select first item - as Windows does */); } } } @@ -2114,7 +2133,7 @@ bool wxMenuBar::ProcessMouseEvent(const wxPoint& pt) static wxPoint s_ptLast; if ( pt == s_ptLast ) { - return FALSE; + return false; } s_ptLast = pt; @@ -2123,7 +2142,7 @@ bool wxMenuBar::ProcessMouseEvent(const wxPoint& pt) int currentNew = GetMenuFromPoint(pt); if ( (currentNew == -1) || (currentNew == m_current) ) { - return FALSE; + return false; } // select the new active item @@ -2134,10 +2153,10 @@ bool wxMenuBar::ProcessMouseEvent(const wxPoint& pt) if ( m_shouldShowMenu && !m_menuShown) { // open the new menu if the old one we closed had been opened - PopupCurrentMenu(FALSE /* don't select first item - as Windows does */); + PopupCurrentMenu(false /* don't select first item - as Windows does */); } - return TRUE; + return true; } void wxMenuBar::OnKeyDown(wxKeyEvent& event) @@ -2173,7 +2192,7 @@ void wxMenuBar::OnKeyDown(wxKeyEvent& event) // the menu when up/down one is switch ( key ) { - case WXK_MENU: + case WXK_ALT: // Alt must be processed at wxWindow level too event.Skip(); // fall through @@ -2281,7 +2300,7 @@ void wxMenuBar::OnKeyDown(wxKeyEvent& event) int wxMenuBar::FindNextItemForAccel(int idxStart, int key, bool *unique) const { - if ( !wxIsalnum(key) ) + if ( !wxIsalnum((wxChar)key) ) { // we only support letters/digits as accels return -1; @@ -2289,10 +2308,10 @@ int wxMenuBar::FindNextItemForAccel(int idxStart, int key, bool *unique) const // do we have more than one item with this accel? if ( unique ) - *unique = TRUE; + *unique = true; // translate everything to lower case before comparing - wxChar chAccel = wxTolower(key); + wxChar chAccel = (wxChar)wxTolower(key); // the index of the item with this accel int idxFound = -1; @@ -2315,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 ) @@ -2329,7 +2347,7 @@ int wxMenuBar::FindNextItemForAccel(int idxStart, int key, bool *unique) const else // we already had found such item { if ( unique ) - *unique = FALSE; + *unique = false; // no need to continue further, we won't find // anything we don't already know @@ -2370,13 +2388,13 @@ bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent& event) if ( node->GetData()->ProcessAccelEvent(event) ) { // menu processed it - return TRUE; + return true; } } } // not found - return FALSE; + return false; } #endif // wxUSE_ACCEL @@ -2393,7 +2411,7 @@ void wxMenuBar::PopupCurrentMenu(bool selectFirst) wxASSERT_MSG( !m_menuShown, _T("shouldn't show two menus at once!") ); // in any case, we should show it - even if we won't - m_shouldShowMenu = TRUE; + m_shouldShowMenu = true; if ( IsEnabledTop(m_current) ) { @@ -2408,6 +2426,8 @@ void wxMenuBar::PopupCurrentMenu(bool selectFirst) // item, not to the right of it wxRect rectItem = GetItemRect(m_current); + m_menuShown->SetInvokingWindow(m_frameLast); + m_menuShown->Popup(ClientToScreen(rectItem.GetPosition()), wxSize(0, rectItem.GetHeight()), selectFirst); @@ -2431,7 +2451,7 @@ void wxMenuBar::DismissMenu() void wxMenuBar::OnDismissMenu(bool dismissMenuBar) { - m_shouldShowMenu = FALSE; + m_shouldShowMenu = false; m_menuShown = NULL; if ( dismissMenuBar ) { @@ -2441,11 +2461,8 @@ void wxMenuBar::OnDismissMenu(bool dismissMenuBar) void wxMenuBar::OnDismiss() { - if ( GetCapture() ) - { + if ( ReleaseMouseCapture() ) wxLogTrace(_T("mousecapture"), _T("Releasing mouse from wxMenuBar::OnDismiss")); - GetCapture()->ReleaseMouse(); - } if ( m_current != -1 ) { @@ -2458,6 +2475,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(); @@ -2471,7 +2524,7 @@ wxEventLoop *wxWindow::ms_evtLoopPopup = NULL; bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y) { - wxCHECK_MSG( !ms_evtLoopPopup, FALSE, + wxCHECK_MSG( !ms_evtLoopPopup, false, _T("can't show more than one popup menu at a time") ); #ifdef __WXMSW__ @@ -2501,7 +2554,7 @@ bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y) // wxLogDebug( "Name of invoking window %s", menu->GetInvokingWindow()->GetName().c_str() ); - menu->Popup(ClientToScreen(wxPoint(x, y)), wxSize(0, 0)); + menu->Popup(ClientToScreen(wxPoint(x, y)), wxSize(0,0)); // this is not very useful if the menu was popped up because of the mouse // click but I think it is nice to do when it appears because of a key @@ -2521,7 +2574,7 @@ bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y) ms_evtLoopPopup = NULL; // remove the handler - PopEventHandler(TRUE /* delete it */); + PopEventHandler(true /* delete it */); menu->SetInvokingWindow(NULL); @@ -2529,7 +2582,7 @@ bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y) SetCursor(cursorOld); #endif // __WXMSW__ - return TRUE; + return true; } void wxWindow::DismissPopupMenu() @@ -2540,4 +2593,3 @@ void wxWindow::DismissPopupMenu() } #endif // wxUSE_MENUS -