]>
git.saurik.com Git - wxWidgets.git/blob - src/univ/menu.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenuItem, wxMenu and wxMenuBar implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "univmenuitem.h"
22 #pragma implementation "univmenu.h"
25 #include "wx/wxprec.h"
32 #include "wx/dynarray.h"
33 #include "wx/control.h" // for FindAccelIndex()
35 #include "wx/settings.h"
42 #include "wx/popupwin.h"
43 #include "wx/evtloop.h"
44 #include "wx/dcclient.h"
47 #include "wx/univ/renderer.h"
50 #include "wx/msw/private.h"
53 // ----------------------------------------------------------------------------
54 // wxMenuInfo contains all extra information about top level menus we need
55 // ----------------------------------------------------------------------------
57 class WXDLLEXPORT wxMenuInfo
61 wxMenuInfo(const wxString
& text
)
69 void SetLabel(const wxString
& text
)
71 // remember the accel char (may be -1 if none)
72 m_indexAccel
= wxControl::FindAccelIndex(text
, &m_label
);
74 // calculate the width later, after the menu bar is created
78 void SetEnabled(bool enabled
= TRUE
) { m_isEnabled
= enabled
; }
82 const wxString
& GetLabel() const { return m_label
; }
83 bool IsEnabled() const { return m_isEnabled
; }
84 wxCoord
GetWidth(wxMenuBar
*menubar
) const
88 wxConstCast(this, wxMenuInfo
)->CalcWidth(menubar
);
94 int GetAccelIndex() const { return m_indexAccel
; }
97 void CalcWidth(wxMenuBar
*menubar
)
100 wxClientDC
dc(menubar
);
101 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
102 dc
.GetTextExtent(m_label
, &size
.x
, &size
.y
);
104 // adjust for the renderer we use and store the width
105 m_width
= menubar
->GetRenderer()->GetMenuBarItemSize(size
).x
;
114 #include "wx/arrimpl.cpp"
116 WX_DEFINE_OBJARRAY(wxMenuInfoArray
);
118 // ----------------------------------------------------------------------------
119 // wxPopupMenuWindow: a popup window showing a menu
120 // ----------------------------------------------------------------------------
122 class wxPopupMenuWindow
: public wxPopupTransientWindow
125 wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
);
127 // override the base class version to select the first item initially
128 virtual void Popup(wxWindow
*focus
= NULL
);
130 // override the base class version to dismiss any open submenus
131 virtual void Dismiss();
133 // notify the menu when the window disappears from screen
134 virtual void OnDismiss();
136 // called when a submenu is dismissed
137 void OnSubmenuDismiss() { m_hasOpenSubMenu
= FALSE
; }
139 // get the currently selected item (may be NULL)
140 wxMenuItem
*GetCurrentItem() const
142 return m_nodeCurrent
? m_nodeCurrent
->GetData() : NULL
;
145 // find the menu item at given position
146 wxMenuItemList::Node
*GetMenuItemFromPoint(const wxPoint
& pt
) const;
148 // refresh the given item
149 void RefreshItem(wxMenuItem
*item
);
151 // preselect the first item
152 void SelectFirst() { SetCurrent(m_menu
->GetMenuItems().GetFirst()); }
154 // process the key event, return TRUE if done
155 bool ProcessKeyDown(int key
);
157 // process mouse move event
158 void ProcessMouseMove(const wxPoint
& pt
);
160 // don't dismiss the popup window if the parent menu was clicked
161 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
164 // how did we perform this operation?
171 // draw the menu inside this window
172 virtual void DoDraw(wxControlRenderer
*renderer
);
175 void OnLeftUp(wxMouseEvent
& event
);
176 void OnMouseMove(wxMouseEvent
& event
);
177 void OnMouseLeave(wxMouseEvent
& event
);
178 void OnKeyDown(wxKeyEvent
& event
);
180 // reset the current item and node
183 // set the current node and item withotu refreshing anything
184 void SetCurrent(wxMenuItemList::Node
*node
);
186 // change the current item refreshing the old and new items
187 void ChangeCurrent(wxMenuItemList::Node
*node
);
189 // activate item, i.e. call either ClickItem() or OpenSubmenu() depending
190 // on what it is, return TRUE if something was done (i.e. it's not a
192 bool ActivateItem(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
194 // send the event about the item click
195 void ClickItem(wxMenuItem
*item
);
197 // show the submenu for this item
198 void OpenSubmenu(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
200 // can this tiem be opened?
201 bool CanOpen(wxMenuItem
*item
)
203 return item
&& item
->IsEnabled() && item
->IsSubMenu();
206 // dismiss the menu and all parent menus too
207 void DismissAndNotify();
209 // react to dimissing this menu and also dismiss the parent if
211 void HandleDismiss(bool dismissParent
);
213 // do we have an open submenu?
214 bool HasOpenSubmenu() const { return m_hasOpenSubMenu
; }
216 // get previous node after the current one
217 wxMenuItemList::Node
*GetPrevNode() const;
219 // get previous node before the given one, wrapping if it's the first one
220 wxMenuItemList::Node
*GetPrevNode(wxMenuItemList::Node
*node
) const;
222 // get next node after the current one
223 wxMenuItemList::Node
*GetNextNode() const;
225 // get next node after the given one, wrapping if it's the last one
226 wxMenuItemList::Node
*GetNextNode(wxMenuItemList::Node
*node
) const;
232 // the menu node corresponding to the current item
233 wxMenuItemList::Node
*m_nodeCurrent
;
235 // do we currently have an opened submenu?
236 bool m_hasOpenSubMenu
;
238 DECLARE_EVENT_TABLE()
241 // ----------------------------------------------------------------------------
242 // wxMenuKbdRedirector: an event handler which redirects kbd input to wxMenu
243 // ----------------------------------------------------------------------------
245 class wxMenuKbdRedirector
: public wxEvtHandler
248 wxMenuKbdRedirector(wxMenu
*menu
) { m_menu
= menu
; }
250 virtual bool ProcessEvent(wxEvent
& event
)
252 if ( event
.GetEventType() == wxEVT_KEY_DOWN
)
254 return m_menu
->ProcessKeyDown(((wxKeyEvent
&)event
).GetKeyCode());
258 return wxEvtHandler::ProcessEvent(event
);
266 // ----------------------------------------------------------------------------
268 // ----------------------------------------------------------------------------
270 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
271 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
272 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
274 BEGIN_EVENT_TABLE(wxPopupMenuWindow
, wxPopupTransientWindow
)
275 EVT_KEY_DOWN(wxPopupMenuWindow::OnKeyDown
)
277 EVT_LEFT_UP(wxPopupMenuWindow::OnLeftUp
)
278 EVT_MOTION(wxPopupMenuWindow::OnMouseMove
)
279 EVT_LEAVE_WINDOW(wxPopupMenuWindow::OnMouseLeave
)
282 BEGIN_EVENT_TABLE(wxMenuBar
, wxMenuBarBase
)
283 EVT_KILL_FOCUS(wxMenuBar::OnKillFocus
)
285 EVT_KEY_DOWN(wxMenuBar::OnKeyDown
)
287 EVT_LEFT_DOWN(wxMenuBar::OnLeftDown
)
288 EVT_MOTION(wxMenuBar::OnMouseMove
)
291 // ============================================================================
293 // ============================================================================
295 // ----------------------------------------------------------------------------
297 // ----------------------------------------------------------------------------
299 wxPopupMenuWindow::wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
)
302 m_hasOpenSubMenu
= FALSE
;
306 (void)Create(parent
, wxBORDER_RAISED
);
308 SetCursor(wxCURSOR_ARROW
);
311 // ----------------------------------------------------------------------------
312 // wxPopupMenuWindow current item/node handling
313 // ----------------------------------------------------------------------------
315 void wxPopupMenuWindow::ResetCurrent()
320 void wxPopupMenuWindow::SetCurrent(wxMenuItemList::Node
*node
)
322 m_nodeCurrent
= node
;
325 void wxPopupMenuWindow::ChangeCurrent(wxMenuItemList::Node
*node
)
327 if ( node
!= m_nodeCurrent
)
329 wxMenuItemList::Node
*nodeOldCurrent
= m_nodeCurrent
;
331 m_nodeCurrent
= node
;
333 if ( nodeOldCurrent
)
335 wxMenuItem
*item
= nodeOldCurrent
->GetData();
336 wxCHECK_RET( item
, _T("no current item?") );
338 // if it was the currently opened menu, close it
339 if ( item
->IsSubMenu() && item
->GetSubMenu()->IsShown() )
341 item
->GetSubMenu()->Dismiss();
349 RefreshItem(m_nodeCurrent
->GetData());
353 wxMenuItemList::Node
*wxPopupMenuWindow::GetPrevNode() const
355 wxMenuItemList::Node
*node
= m_nodeCurrent
;
358 // start from the end if no current item
359 node
= m_menu
->GetMenuItems().GetLast();
362 return GetPrevNode(node
);
365 wxMenuItemList::Node
*
366 wxPopupMenuWindow::GetPrevNode(wxMenuItemList::Node
*node
) const
370 node
= node
->GetPrevious();
373 node
= m_menu
->GetMenuItems().GetLast();
376 //else: the menu is empty
381 wxMenuItemList::Node
*wxPopupMenuWindow::GetNextNode() const
383 wxMenuItemList::Node
*node
= m_nodeCurrent
;
386 // start from the beginning if no current item
387 node
= m_menu
->GetMenuItems().GetFirst();
390 return GetNextNode(node
);
393 wxMenuItemList::Node
*
394 wxPopupMenuWindow::GetNextNode(wxMenuItemList::Node
*node
) const
398 node
= node
->GetNext();
401 node
= m_menu
->GetMenuItems().GetFirst();
404 //else: the menu is empty
409 // ----------------------------------------------------------------------------
410 // wxPopupMenuWindow popup/dismiss
411 // ----------------------------------------------------------------------------
413 void wxPopupMenuWindow::Popup(wxWindow
*focus
)
415 // check that the current item had been properly reset before
416 wxASSERT_MSG( !m_nodeCurrent
||
417 m_nodeCurrent
== m_menu
->GetMenuItems().GetFirst(),
418 _T("menu current item preselected incorrectly") );
420 wxPopupTransientWindow::Popup(focus
);
423 // ensure that this window is really on top of everything: without using
424 // SetWindowPos() it can be covered by its parent menu which is not
425 // really what we want
426 wxMenu
*menuParent
= m_menu
->GetParent();
429 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
431 // if we're shown, the parent menu must be also shown
432 wxCHECK_RET( win
, _T("parent menu is not shown?") );
434 if ( !::SetWindowPos(GetHwndOf(win
), GetHwnd(),
436 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOREDRAW
) )
438 wxLogLastError(_T("SetWindowPos(HWND_TOP)"));
446 void wxPopupMenuWindow::Dismiss()
448 if ( HasOpenSubmenu() )
450 wxMenuItem
*item
= GetCurrentItem();
451 wxCHECK_RET( item
&& item
->IsSubMenu(), _T("where is our open submenu?") );
453 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
454 wxCHECK_RET( win
, _T("opened submenu is not opened?") );
460 wxPopupTransientWindow::Dismiss();
463 void wxPopupMenuWindow::OnDismiss()
465 // when we are dismissed because the user clicked elsewhere or we lost
466 // focus in any other way, hide the parent menu as well
470 void wxPopupMenuWindow::HandleDismiss(bool dismissParent
)
474 m_menu
->OnDismiss(dismissParent
);
477 void wxPopupMenuWindow::DismissAndNotify()
483 // ----------------------------------------------------------------------------
484 // wxPopupMenuWindow geometry
485 // ----------------------------------------------------------------------------
487 wxMenuItemList::Node
*
488 wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint
& pt
) const
490 // we only use the y coord normally, but still check x in case the point is
491 // outside the window completely
492 if ( wxWindow::HitTest(pt
) == wxHT_WINDOW_INSIDE
)
495 for ( wxMenuItemList::Node
*node
= m_menu
->GetMenuItems().GetFirst();
497 node
= node
->GetNext() )
499 wxMenuItem
*item
= node
->GetData();
500 y
+= item
->GetHeight();
512 // ----------------------------------------------------------------------------
513 // wxPopupMenuWindow drawing
514 // ----------------------------------------------------------------------------
516 void wxPopupMenuWindow::RefreshItem(wxMenuItem
*item
)
518 wxCHECK_RET( item
, _T("can't refresh NULL item") );
520 wxASSERT_MSG( IsShown(), _T("can't refresh menu which is not shown") );
522 // FIXME: -1 here because of SetLogicalOrigin(1, 1) in DoDraw()
523 RefreshRect(wxRect(0, item
->GetPosition() - 1,
524 m_menu
->GetGeometryInfo().GetSize().x
, item
->GetHeight()));
527 void wxPopupMenuWindow::DoDraw(wxControlRenderer
*renderer
)
529 // no clipping so far - do we need it? I don't think so as the menu is
530 // never partially covered as it is always on top of everything
532 wxDC
& dc
= renderer
->GetDC();
533 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
535 // FIXME: this should be done in the renderer, however when it is fixed
536 // wxPopupMenuWindow::RefreshItem() should be changed too!
537 dc
.SetLogicalOrigin(1, 1);
539 wxRenderer
*rend
= renderer
->GetRenderer();
542 const wxMenuGeometryInfo
& gi
= m_menu
->GetGeometryInfo();
543 for ( wxMenuItemList::Node
*node
= m_menu
->GetMenuItems().GetFirst();
545 node
= node
->GetNext() )
547 wxMenuItem
*item
= node
->GetData();
549 if ( item
->IsSeparator() )
551 rend
->DrawMenuSeparator(dc
, y
, gi
);
553 else // not a separator
556 if ( item
->IsCheckable() )
558 flags
|= wxCONTROL_CHECKABLE
;
560 if ( item
->IsChecked() )
562 flags
|= wxCONTROL_CHECKED
;
566 if ( !item
->IsEnabled() )
567 flags
|= wxCONTROL_DISABLED
;
569 if ( item
->IsSubMenu() )
570 flags
|= wxCONTROL_ISSUBMENU
;
572 if ( item
== GetCurrentItem() )
573 flags
|= wxCONTROL_SELECTED
;
581 item
->GetAccelString(),
582 // strangely enough, for unchecked item we use the
583 // "checked" bitmap because this is the default one - this
584 // explains this strange boolean expression
585 item
->GetBitmap(!item
->IsCheckable() || item
->IsChecked()),
587 item
->GetAccelIndex()
591 y
+= item
->GetHeight();
595 // ----------------------------------------------------------------------------
596 // wxPopupMenuWindow actions
597 // ----------------------------------------------------------------------------
599 void wxPopupMenuWindow::ClickItem(wxMenuItem
*item
)
601 wxCHECK_RET( item
, _T("can't click NULL item") );
603 wxASSERT_MSG( !item
->IsSeparator() && !item
->IsSubMenu(),
604 _T("can't click this item") );
606 wxMenu
* menu
= m_menu
;
611 menu
->ClickItem(item
);
614 void wxPopupMenuWindow::OpenSubmenu(wxMenuItem
*item
, InputMethod how
)
616 wxCHECK_RET( item
, _T("can't open NULL submenu") );
618 wxMenu
*submenu
= item
->GetSubMenu();
619 wxCHECK_RET( submenu
, _T("can only open submenus!") );
621 // FIXME: should take into account the border width
622 submenu
->Popup(ClientToScreen(wxPoint(0, item
->GetPosition())),
623 wxSize(m_menu
->GetGeometryInfo().GetSize().x
, 0),
624 how
== WithKeyboard
/* preselect first item then */);
626 m_hasOpenSubMenu
= TRUE
;
629 bool wxPopupMenuWindow::ActivateItem(wxMenuItem
*item
, InputMethod how
)
631 // don't activate disabled items
632 if ( !item
|| !item
->IsEnabled() )
637 // normal menu items generate commands, submenus can be opened and
638 // the separators don't do anything
639 if ( item
->IsSubMenu() )
641 OpenSubmenu(item
, how
);
643 else if ( !item
->IsSeparator() )
647 else // separator, can't activate
655 // ----------------------------------------------------------------------------
656 // wxPopupMenuWindow input handling
657 // ----------------------------------------------------------------------------
659 bool wxPopupMenuWindow::ProcessLeftDown(wxMouseEvent
& event
)
661 // wxPopupWindowHandler dismisses the window when the mouse is clicked
662 // outside it which is usually just fine, but there is one case when we
663 // don't want to do it: if the mouse was clicked on the parent submenu item
664 // which opens this menu, so check for it
666 wxPoint pos
= event
.GetPosition();
667 if ( HitTest(pos
.x
, pos
.y
) == wxHT_WINDOW_OUTSIDE
)
669 wxMenu
*menu
= m_menu
->GetParent();
672 wxPopupMenuWindow
*win
= menu
->m_popupMenu
;
674 wxCHECK_MSG( win
, FALSE
, _T("parent menu not shown?") );
676 pos
= ClientToScreen(pos
);
677 if ( win
->GetMenuItemFromPoint(win
->ScreenToClient(pos
)) )
682 //else: it is outside the parent menu as well, do dismiss this one
689 void wxPopupMenuWindow::OnLeftUp(wxMouseEvent
& event
)
691 wxMenuItemList::Node
*node
= GetMenuItemFromPoint(event
.GetPosition());
694 ActivateItem(node
->GetData(), WithMouse
);
698 void wxPopupMenuWindow::OnMouseMove(wxMouseEvent
& event
)
700 const wxPoint pt
= event
.GetPosition();
702 // we need to ignore extra mouse events: example when this happens is when
703 // the mouse is on the menu and we open a submenu from keyboard - Windows
704 // then sends us a dummy mouse move event, we (correctly) determine that it
705 // happens in the parent menu and so immediately close the just opened
708 static wxPoint s_ptLast
;
709 wxPoint ptCur
= ClientToScreen(pt
);
710 if ( ptCur
== s_ptLast
)
718 ProcessMouseMove(pt
);
723 void wxPopupMenuWindow::ProcessMouseMove(const wxPoint
& pt
)
725 wxMenuItemList::Node
*node
= GetMenuItemFromPoint(pt
);
727 // don't reset current to NULL here, we only do it when the mouse leaves
728 // the window (see below)
731 if ( node
!= m_nodeCurrent
)
735 wxMenuItem
*item
= GetCurrentItem();
738 OpenSubmenu(item
, WithMouse
);
741 //else: same item, nothing to do
743 else // not on an item
745 // the last open submenu forwards the mouse move messages to its
746 // parent, so if the mouse moves to another item of the parent menu,
747 // this menu is closed and this other item is selected - in the similar
748 // manner, the top menu forwards the mouse moves to the menubar which
749 // allows to select another top level menu by just moving the mouse
751 // we need to translate our client coords to the client coords of the
752 // window we forward this event to
753 wxPoint ptScreen
= ClientToScreen(pt
);
755 // if the mouse is outside this menu, let the parent one to
757 wxMenu
*menuParent
= m_menu
->GetParent();
760 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
762 // if we're shown, the parent menu must be also shown
763 wxCHECK_RET( win
, _T("parent menu is not shown?") );
765 win
->ProcessMouseMove(win
->ScreenToClient(ptScreen
));
767 else // no parent menu
769 wxMenuBar
*menubar
= m_menu
->GetMenuBar();
772 if ( menubar
->ProcessMouseEvent(
773 menubar
->ScreenToClient(ptScreen
)) )
775 // menubar has closed this menu and opened another one, probably
780 //else: top level popup menu, no other processing to do
784 void wxPopupMenuWindow::OnMouseLeave(wxMouseEvent
& event
)
786 // due to the artefact of mouse events generation under MSW, we actually
787 // may get the mouse leave event after the menu had been already dismissed
788 // and calling ChangeCurrent() would then assert, so don't do it
791 // we shouldn't change the current them if our submenu is opened and
792 // mouse moved there, in this case the submenu is responsable for
795 if ( HasOpenSubmenu() )
797 wxMenuItem
*item
= GetCurrentItem();
798 wxCHECK_RET( CanOpen(item
), _T("where is our open submenu?") );
800 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
801 wxCHECK_RET( win
, _T("submenu is opened but not shown?") );
803 // only handle this event if the mouse is not inside the submenu
804 wxPoint pt
= ClientToScreen(event
.GetPosition());
806 win
->HitTest(win
->ScreenToClient(pt
)) == wxHT_WINDOW_OUTSIDE
;
810 // this menu is the last opened
823 void wxPopupMenuWindow::OnKeyDown(wxKeyEvent
& event
)
825 if ( !ProcessKeyDown(event
.GetKeyCode()) )
831 bool wxPopupMenuWindow::ProcessKeyDown(int key
)
833 wxMenuItem
*item
= GetCurrentItem();
835 // first let the opened submenu to have it (no test for IsEnabled() here,
836 // the keys navigate even in a disabled submenu if we had somehow managed
837 // to open it inspit of this)
838 if ( HasOpenSubmenu() )
840 wxCHECK_MSG( CanOpen(item
), FALSE
,
841 _T("has open submenu but another item selected?") );
843 if ( item
->GetSubMenu()->ProcessKeyDown(key
) )
847 bool processed
= TRUE
;
849 // handle the up/down arrows, home, end, esc and return here, pass the
850 // left/right arrows to the menu bar except when the right arrow can be
851 // used to open a submenu
855 // if we're not a top level menu, close us, else leave this to the
857 if ( !m_menu
->GetParent() )
866 // close just this menu
868 HandleDismiss(FALSE
);
872 processed
= ActivateItem(item
);
876 ChangeCurrent(m_menu
->GetMenuItems().GetFirst());
880 ChangeCurrent(m_menu
->GetMenuItems().GetLast());
886 bool up
= key
== WXK_UP
;
888 wxMenuItemList::Node
*nodeStart
= up
? GetPrevNode()
891 while ( node
&& node
->GetData()->IsSeparator() )
893 node
= up
? GetPrevNode(node
) : GetNextNode(node
);
895 if ( node
== nodeStart
)
897 // nothing but separators and disabled items in this
915 // don't try to reopen an already opened menu
916 if ( !HasOpenSubmenu() && CanOpen(item
) )
927 // look for the menu item starting with this letter
928 if ( wxIsalnum(key
) )
930 // we want to start from the item after this one because
931 // if we're already on the item with the given accel we want to
932 // go to the next one, not to stay in place
933 wxMenuItemList::Node
*nodeStart
= GetNextNode();
935 // do we have more than one item with this accel?
936 bool notUnique
= FALSE
;
938 // translate everything to lower case before comparing
939 wxChar chAccel
= wxTolower(key
);
941 // loop through all items searching for the item with this
943 wxMenuItemList::Node
*node
= nodeStart
,
947 item
= node
->GetData();
949 int idxAccel
= item
->GetAccelIndex();
950 if ( idxAccel
!= -1 &&
951 wxTolower(item
->GetLabel()[(size_t)idxAccel
])
954 // ok, found an item with this accel
957 // store it but continue searching as we need to
958 // know if it's the only item with this accel or if
962 else // we already had found such item
966 // no need to continue further, we won't find
967 // anything we don't already know
972 // we want to iterate over all items wrapping around if
974 node
= GetNextNode(node
);
975 if ( node
== nodeStart
)
977 // we've seen all nodes
984 item
= nodeFound
->GetData();
986 // go to this item anyhow
987 ChangeCurrent(nodeFound
);
989 if ( !notUnique
&& item
->IsEnabled() )
991 // unique item with this accel - activate it
992 processed
= ActivateItem(item
);
994 //else: just select it but don't activate as the user might
995 // have wanted to activate another item
997 // skip "processed = FALSE" below
1008 // ----------------------------------------------------------------------------
1010 // ----------------------------------------------------------------------------
1025 // ----------------------------------------------------------------------------
1026 // wxMenu and wxMenuGeometryInfo
1027 // ----------------------------------------------------------------------------
1029 wxMenuGeometryInfo::~wxMenuGeometryInfo()
1033 const wxMenuGeometryInfo
& wxMenu::GetGeometryInfo() const
1039 wxConstCast(this, wxMenu
)->m_geometry
=
1040 m_popupMenu
->GetRenderer()->GetMenuGeometry(m_popupMenu
, *this);
1044 wxFAIL_MSG( _T("can't get geometry without window") );
1051 void wxMenu::InvalidateGeometryInfo()
1060 // ----------------------------------------------------------------------------
1061 // wxMenu adding/removing items
1062 // ----------------------------------------------------------------------------
1064 void wxMenu::OnItemAdded(wxMenuItem
*item
)
1066 InvalidateGeometryInfo();
1070 #endif // wxUSE_ACCEL
1072 // the submenus of a popup menu should have the same invoking window as it
1074 if ( m_invokingWindow
&& item
->IsSubMenu() )
1076 item
->GetSubMenu()->SetInvokingWindow(m_invokingWindow
);
1080 bool wxMenu::DoAppend(wxMenuItem
*item
)
1082 if ( !wxMenuBase::DoAppend(item
) )
1090 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1092 if ( !wxMenuBase::DoInsert(pos
, item
) )
1100 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1102 wxMenuItem
*itemOld
= wxMenuBase::DoRemove(item
);
1106 InvalidateGeometryInfo();
1109 RemoveAccelFor(item
);
1110 #endif // wxUSE_ACCEL
1116 // ----------------------------------------------------------------------------
1117 // wxMenu attaching/detaching
1118 // ----------------------------------------------------------------------------
1120 void wxMenu::Attach(wxMenuBarBase
*menubar
)
1122 wxMenuBase::Attach(menubar
);
1124 wxCHECK_RET( m_menuBar
, _T("menubar can't be NULL after attaching") );
1126 // unfortunately, we can't use m_menuBar->GetEventHandler() here because,
1127 // if the menubar is currently showing a menu, its event handler is a
1128 // temporary one installed by wxPopupWindow and so will disappear soon any
1129 // any attempts to use it from the newly attached menu would result in a
1132 // so we use the menubar itself, even if it's a pity as it means we can't
1133 // redirect all menu events by changing the menubar handler (FIXME)
1134 SetNextHandler(m_menuBar
);
1137 void wxMenu::Detach()
1139 wxMenuBase::Detach();
1142 // ----------------------------------------------------------------------------
1143 // wxMenu misc functions
1144 // ----------------------------------------------------------------------------
1146 wxWindow
*wxMenu::GetRootWindow() const
1150 // simple case - a normal menu attached to the menubar
1154 // we're a popup menu but the trouble is that only the top level popup menu
1155 // has a pointer to the invoking window, so we must walk up the menu chain
1157 wxWindow
*win
= GetInvokingWindow();
1160 // we already have it
1164 wxMenu
*menu
= GetParent();
1167 win
= menu
->GetInvokingWindow();
1171 menu
= menu
->GetParent();
1174 // we're probably going to crash in the caller anyhow, but try to detect
1175 // this error as soon as possible
1176 wxASSERT_MSG( win
, _T("menu without any associated window?") );
1178 // also remember it in this menu so that we don't have to search for it the
1180 wxConstCast(this, wxMenu
)->m_invokingWindow
= win
;
1185 wxRenderer
*wxMenu::GetRenderer() const
1187 // we're going to crash without renderer!
1188 wxCHECK_MSG( m_popupMenu
, NULL
, _T("neither popup nor menubar menu?") );
1190 return m_popupMenu
->GetRenderer();
1193 void wxMenu::RefreshItem(wxMenuItem
*item
)
1195 // the item geometry changed, so our might have changed as well
1196 InvalidateGeometryInfo();
1200 // this would be a bug in IsShown()
1201 wxCHECK_RET( m_popupMenu
, _T("must have popup window if shown!") );
1203 // recalc geometry to update the item height and such
1204 (void)GetGeometryInfo();
1206 m_popupMenu
->RefreshItem(item
);
1210 // ----------------------------------------------------------------------------
1211 // wxMenu showing and hiding
1212 // ----------------------------------------------------------------------------
1214 bool wxMenu::IsShown() const
1216 return m_popupMenu
&& m_popupMenu
->IsShown();
1219 void wxMenu::OnDismiss(bool dismissParent
)
1223 // always notify the parent about submenu disappearance
1224 wxPopupMenuWindow
*win
= m_menuParent
->m_popupMenu
;
1227 win
->OnSubmenuDismiss();
1231 wxFAIL_MSG( _T("parent menu not shown?") );
1234 // and if we dismiss everything, propagate to parent
1235 if ( dismissParent
)
1237 // dismissParent is recursive
1238 m_menuParent
->Dismiss();
1239 m_menuParent
->OnDismiss(TRUE
);
1242 else // no parent menu
1244 // notify the menu bar if we're a top level menu
1247 m_menuBar
->OnDismissMenu(dismissParent
);
1251 wxCHECK_RET( m_invokingWindow
, _T("what kind of menu is this?") );
1253 m_invokingWindow
->DismissPopupMenu();
1254 SetInvokingWindow(NULL
);
1259 void wxMenu::Popup(const wxPoint
& pos
, const wxSize
& size
, bool selectFirst
)
1261 // create the popup window if not done yet
1264 m_popupMenu
= new wxPopupMenuWindow(GetRootWindow(), this);
1267 // select the first item unless disabled
1270 m_popupMenu
->SelectFirst();
1273 // the geometry might have changed since the last time we were shown, so
1275 m_popupMenu
->SetClientSize(GetGeometryInfo().GetSize());
1277 // position it as specified
1278 m_popupMenu
->Position(pos
, size
);
1280 // the menu can't have the focus itself (it is a Windows limitation), so
1281 // always keep the focus at the originating window
1282 wxWindow
*focus
= GetRootWindow();
1284 wxASSERT_MSG( focus
, _T("no window to keep focus on?") );
1287 m_popupMenu
->Popup(focus
);
1290 void wxMenu::Dismiss()
1292 wxCHECK_RET( IsShown(), _T("can't dismiss hidden menu") );
1294 m_popupMenu
->Dismiss();
1297 // ----------------------------------------------------------------------------
1298 // wxMenu event processing
1299 // ----------------------------------------------------------------------------
1301 bool wxMenu::ProcessKeyDown(int key
)
1303 wxCHECK_MSG( m_popupMenu
, FALSE
,
1304 _T("can't process key events if not shown") );
1306 return m_popupMenu
->ProcessKeyDown(key
);
1309 bool wxMenu::ClickItem(wxMenuItem
*item
)
1312 if ( item
->IsCheckable() )
1314 // update the item state
1315 isChecked
= !item
->IsChecked();
1317 item
->Check(isChecked
!= 0);
1325 return SendEvent(item
->GetId(), isChecked
);
1328 // ----------------------------------------------------------------------------
1329 // wxMenu accel support
1330 // ----------------------------------------------------------------------------
1334 bool wxMenu::ProcessAccelEvent(const wxKeyEvent
& event
)
1336 // do we have an item for this accel?
1337 wxMenuItem
*item
= m_accelTable
.GetMenuItem(event
);
1338 if ( item
&& item
->IsEnabled() )
1340 return ClickItem(item
);
1344 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
1346 node
= node
->GetNext() )
1348 const wxMenuItem
*item
= node
->GetData();
1349 if ( item
->IsSubMenu() && item
->IsEnabled() )
1352 if ( item
->GetSubMenu()->ProcessAccelEvent(event
) )
1362 void wxMenu::AddAccelFor(wxMenuItem
*item
)
1364 wxAcceleratorEntry
*accel
= item
->GetAccel();
1367 accel
->SetMenuItem(item
);
1369 m_accelTable
.Add(*accel
);
1375 void wxMenu::RemoveAccelFor(wxMenuItem
*item
)
1377 wxAcceleratorEntry
*accel
= item
->GetAccel();
1380 m_accelTable
.Remove(*accel
);
1386 #endif // wxUSE_ACCEL
1388 // ----------------------------------------------------------------------------
1389 // wxMenuItem construction
1390 // ----------------------------------------------------------------------------
1392 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
1394 const wxString
& text
,
1395 const wxString
& help
,
1400 m_parentMenu
= parentMenu
;
1401 m_subMenu
= subMenu
;
1406 m_isCheckable
= isCheckable
;
1408 m_isChecked
= FALSE
;
1416 wxMenuItem::~wxMenuItem()
1420 // ----------------------------------------------------------------------------
1421 // wxMenuItemBase methods implemented here
1422 // ----------------------------------------------------------------------------
1425 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
1427 const wxString
& name
,
1428 const wxString
& help
,
1432 return new wxMenuItem(parentMenu
, id
, name
, help
, isCheckable
, subMenu
);
1436 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
1438 return wxStripMenuCodes(text
);
1441 // ----------------------------------------------------------------------------
1442 // wxMenuItem operations
1443 // ----------------------------------------------------------------------------
1445 void wxMenuItem::NotifyMenu()
1447 m_parentMenu
->RefreshItem(this);
1450 void wxMenuItem::UpdateAccelInfo()
1452 m_indexAccel
= wxControl::FindAccelIndex(m_text
);
1454 // will be empty if the text contains no TABs - ok
1455 m_strAccel
= m_text
.AfterFirst(_T('\t'));
1458 void wxMenuItem::SetText(const wxString
& text
)
1460 if ( text
!= m_text
)
1462 // first call the base class version to change m_text
1463 wxMenuItemBase::SetText(text
);
1471 void wxMenuItem::SetCheckable(bool checkable
)
1473 if ( checkable
!= m_isCheckable
)
1475 wxMenuItemBase::SetCheckable(checkable
);
1481 void wxMenuItem::SetBitmaps(const wxBitmap
& bmpChecked
,
1482 const wxBitmap
& bmpUnchecked
)
1484 m_bmpChecked
= bmpChecked
;
1485 m_bmpUnchecked
= bmpUnchecked
;
1490 void wxMenuItem::Enable(bool enable
)
1492 if ( enable
!= m_isEnabled
)
1494 wxMenuItemBase::Enable(enable
);
1500 void wxMenuItem::Check(bool check
)
1502 if ( check
!= m_isChecked
)
1504 wxMenuItemBase::Check(check
);
1510 // ----------------------------------------------------------------------------
1511 // wxMenuBar creation
1512 // ----------------------------------------------------------------------------
1514 void wxMenuBar::Init()
1522 m_shouldShowMenu
= FALSE
;
1525 void wxMenuBar::Attach(wxFrame
*frame
)
1527 // maybe you really wanted to call Detach()?
1528 wxCHECK_RET( frame
, _T("wxMenuBar::Attach(NULL) called") );
1530 wxMenuBarBase::Attach(frame
);
1534 // reparent if necessary
1535 if ( m_frameLast
!= frame
)
1540 // show it back - was hidden by Detach()
1543 else // not created yet, do it now
1545 // we have no way to return the error from here anyhow :-(
1546 (void)Create(frame
, -1);
1548 SetCursor(wxCURSOR_ARROW
);
1550 SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT
));
1552 // calculate and set our height (it won't be changed any more)
1553 SetSize(-1, GetBestSize().y
);
1556 // remember the last frame which had us to avoid unnecessarily reparenting
1558 m_frameLast
= frame
;
1561 void wxMenuBar::Detach()
1563 // don't delete the window because we may be reattached later, just hide it
1569 wxMenuBarBase::Detach();
1572 wxMenuBar::~wxMenuBar()
1576 // ----------------------------------------------------------------------------
1577 // wxMenuBar adding/removing items
1578 // ----------------------------------------------------------------------------
1580 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1582 return Insert(GetCount(), menu
, title
);
1585 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1587 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1590 wxMenuInfo
*info
= new wxMenuInfo(title
);
1591 m_menuInfos
.Insert(info
, pos
);
1593 RefreshAllItemsAfter(pos
);
1598 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1600 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1604 wxMenuInfo
& info
= m_menuInfos
[pos
];
1606 info
.SetLabel(title
);
1608 // even if the old menu was disabled, the new one is not any more
1611 // even if we change only this one, the new label has different width,
1612 // so we need to refresh everything beyond this item as well
1613 RefreshAllItemsAfter(pos
);
1619 wxMenu
*wxMenuBar::Remove(size_t pos
)
1621 wxMenu
*menuOld
= wxMenuBarBase::Remove(pos
);
1625 m_menuInfos
.RemoveAt(pos
);
1627 // this doesn't happen too often, so don't try to be too smart - just
1628 // refresh everything
1635 // ----------------------------------------------------------------------------
1636 // wxMenuBar top level menus access
1637 // ----------------------------------------------------------------------------
1639 wxCoord
wxMenuBar::GetItemWidth(size_t pos
) const
1641 return m_menuInfos
[pos
].GetWidth(wxConstCast(this, wxMenuBar
));
1644 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1646 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1648 if ( enable
!= m_menuInfos
[pos
].IsEnabled() )
1650 m_menuInfos
[pos
].SetEnabled(enable
);
1654 //else: nothing to do
1657 bool wxMenuBar::IsEnabledTop(size_t pos
) const
1659 wxCHECK_MSG( pos
< GetCount(), FALSE
, _T("invalid index in IsEnabledTop") );
1661 return m_menuInfos
[pos
].IsEnabled();
1664 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
1666 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1668 if ( label
!= m_menuInfos
[pos
].GetLabel() )
1670 m_menuInfos
[pos
].SetLabel(label
);
1674 //else: nothing to do
1677 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
1679 wxCHECK_MSG( pos
< GetCount(), _T(""), _T("invalid index in GetLabelTop") );
1681 return m_menuInfos
[pos
].GetLabel();
1684 // ----------------------------------------------------------------------------
1685 // wxMenuBar drawing
1686 // ----------------------------------------------------------------------------
1688 void wxMenuBar::RefreshAllItemsAfter(size_t pos
)
1692 // no need to refresh if nothing is shown yet
1696 wxRect rect
= GetItemRect(pos
);
1697 rect
.width
= GetClientSize().x
- rect
.x
;
1701 void wxMenuBar::RefreshItem(size_t pos
)
1703 wxCHECK_RET( pos
!= (size_t)-1,
1704 _T("invalid item in wxMenuBar::RefreshItem") );
1708 // no need to refresh if nothing is shown yet
1712 RefreshRect(GetItemRect(pos
));
1715 void wxMenuBar::DoDraw(wxControlRenderer
*renderer
)
1717 wxDC
& dc
= renderer
->GetDC();
1718 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
1720 // redraw only the items which must be redrawn
1722 // we don't have to use GetUpdateClientRect() here because our client rect
1723 // is the same as total one
1724 wxRect rectUpdate
= GetUpdateRegion().GetBox();
1726 int flagsMenubar
= GetStateFlags();
1730 rect
.height
= GetClientSize().y
;
1733 size_t count
= GetCount();
1734 for ( size_t n
= 0; n
< count
; n
++ )
1736 if ( x
> rectUpdate
.GetRight() )
1738 // all remaining items are to the right of rectUpdate
1743 rect
.width
= GetItemWidth(n
);
1745 if ( x
< rectUpdate
.x
)
1747 // this item is still to the left of rectUpdate
1751 int flags
= flagsMenubar
;
1752 if ( m_current
!= -1 && n
== (size_t)m_current
)
1754 flags
|= wxCONTROL_SELECTED
;
1757 if ( !IsEnabledTop(n
) )
1759 flags
|= wxCONTROL_DISABLED
;
1762 GetRenderer()->DrawMenuBarItem
1766 m_menuInfos
[n
].GetLabel(),
1768 m_menuInfos
[n
].GetAccelIndex()
1773 // ----------------------------------------------------------------------------
1774 // wxMenuBar geometry
1775 // ----------------------------------------------------------------------------
1777 wxRect
wxMenuBar::GetItemRect(size_t pos
) const
1779 wxASSERT_MSG( pos
< GetCount(), _T("invalid menu bar item index") );
1780 wxASSERT_MSG( IsCreated(), _T("can't call this method yet") );
1785 rect
.height
= GetClientSize().y
;
1787 for ( size_t n
= 0; n
< pos
; n
++ )
1789 rect
.x
+= GetItemWidth(n
);
1792 rect
.width
= GetItemWidth(pos
);
1797 wxSize
wxMenuBar::DoGetBestClientSize() const
1800 if ( GetMenuCount() > 0 )
1802 wxClientDC
dc(wxConstCast(this, wxMenuBar
));
1803 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
1804 dc
.GetTextExtent(GetLabelTop(0), &size
.x
, &size
.y
);
1806 // adjust for the renderer we use
1807 size
= GetRenderer()->GetMenuBarItemSize(size
);
1809 else // empty menubar
1815 // the width is arbitrary, of course, for horizontal menubar
1821 int wxMenuBar::GetMenuFromPoint(const wxPoint
& pos
) const
1823 if ( pos
.x
< 0 || pos
.y
< 0 || pos
.y
> GetClientSize().y
)
1828 size_t count
= GetCount();
1829 for ( size_t item
= 0; item
< count
; item
++ )
1831 x
+= GetItemWidth(item
);
1839 // to the right of the last menu item
1843 // ----------------------------------------------------------------------------
1844 // wxMenuBar menu operations
1845 // ----------------------------------------------------------------------------
1847 void wxMenuBar::SelectMenu(size_t pos
)
1855 void wxMenuBar::DoSelectMenu(size_t pos
)
1857 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in DoSelectMenu") );
1859 int posOld
= m_current
;
1865 // close the previous menu
1866 if ( IsShowingMenu() )
1868 // restore m_shouldShowMenu flag after DismissMenu() which resets
1870 bool old
= m_shouldShowMenu
;
1874 m_shouldShowMenu
= old
;
1877 RefreshItem((size_t)posOld
);
1883 void wxMenuBar::PopupMenu(size_t pos
)
1885 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in PopupCurrentMenu") );
1892 // ----------------------------------------------------------------------------
1893 // wxMenuBar input handing
1894 // ----------------------------------------------------------------------------
1897 Note that wxMenuBar doesn't use wxInputHandler but handles keyboard and
1898 mouse in the same way under all platforms. This is because it doesn't derive
1899 from wxControl (which works with input handlers) but directly from wxWindow.
1901 Also, menu bar input handling is rather simple, so maybe it's not really
1902 worth making it themeable - at least I've decided against doing it now as it
1903 would merging the changes back into trunk more difficult. But it still could
1904 be done later if really needed.
1907 void wxMenuBar::OnKillFocus(wxFocusEvent
& event
)
1909 if ( m_current
!= -1 )
1911 RefreshItem((size_t)m_current
);
1919 void wxMenuBar::OnLeftDown(wxMouseEvent
& event
)
1927 else // we didn't have mouse capture, capture it now
1929 m_current
= GetMenuFromPoint(event
.GetPosition());
1930 if ( m_current
== -1 )
1932 // unfortunately, we can't prevent wxMSW from giving us the focus,
1933 // so we can only give it back
1940 // show it as selected
1941 RefreshItem((size_t)m_current
);
1944 PopupCurrentMenu(FALSE
/* don't select first item - as Windows does */);
1949 void wxMenuBar::OnMouseMove(wxMouseEvent
& event
)
1953 (void)ProcessMouseEvent(event
.GetPosition());
1961 bool wxMenuBar::ProcessMouseEvent(const wxPoint
& pt
)
1963 // a hack to ignore the extra mouse events MSW sends us: this is similar to
1964 // wxUSE_MOUSEEVENT_HACK in wxWin itself but it isn't enough for us here as
1965 // we get the messages from different windows (old and new popup menus for
1968 static wxPoint s_ptLast
;
1969 if ( pt
== s_ptLast
)
1977 int currentNew
= GetMenuFromPoint(pt
);
1978 if ( (currentNew
== -1) || (currentNew
== m_current
) )
1983 // select the new active item
1984 DoSelectMenu(currentNew
);
1986 // show the menu if we know that we should, even if we hadn't been showing
1987 // it before (this may happen if the previous menu was disabled)
1988 if ( m_shouldShowMenu
&& !m_menuShown
)
1990 // open the new menu if the old one we closed had been opened
1991 PopupCurrentMenu(FALSE
/* don't select first item - as Windows does */);
1997 void wxMenuBar::OnKeyDown(wxKeyEvent
& event
)
1999 // ensure that we have a current item - we might not have it if we're
2000 // given the focus with Alt or F10 press (and under GTK+ the menubar
2001 // somehow gets the keyboard events even when it doesn't have focus...)
2002 if ( m_current
== -1 )
2004 if ( !HasCapture() )
2008 else // we do have capture
2010 // we always maintain a valid current item while we're in modal
2011 // state (i.e. have the capture)
2012 wxFAIL_MSG( _T("how did we manage to lose current item?") );
2018 int key
= event
.GetKeyCode();
2020 // first let the menu have it
2021 if ( IsShowingMenu() && m_menuShown
->ProcessKeyDown(key
) )
2026 // cycle through the menu items when left/right arrows are pressed and open
2027 // the menu when up/down one is
2031 // Alt must be processed at wxWindow level too
2036 // remove the selection and give the focus away
2037 if ( m_current
!= -1 )
2039 if ( IsShowingMenu() )
2051 size_t count
= GetCount();
2054 // the item won't change anyhow
2057 //else: otherwise, it will
2059 // remember if we were showing a menu - if we did, we should
2060 // show the new menu after changing the item
2061 bool wasMenuOpened
= IsShowingMenu();
2062 if ( wasMenuOpened
)
2067 // cast is safe as we tested for -1 above
2068 size_t currentNew
= (size_t)m_current
;
2070 if ( key
== WXK_LEFT
)
2072 if ( currentNew
-- == 0 )
2073 currentNew
= count
- 1;
2077 if ( ++currentNew
== count
)
2081 DoSelectMenu(currentNew
);
2083 if ( wasMenuOpened
)
2098 // letters open the corresponding menu
2101 int idxFound
= FindNextItemForAccel(m_current
, key
, &unique
);
2103 if ( idxFound
!= -1 )
2105 if ( IsShowingMenu() )
2110 DoSelectMenu((size_t)idxFound
);
2112 // if the item is not unique, just select it but don't
2113 // activate as the user might have wanted to activate
2116 // also, don't try to open a disabled menu
2117 if ( unique
&& IsEnabledTop((size_t)idxFound
) )
2123 // skip the "event.Skip()" below
2132 // ----------------------------------------------------------------------------
2133 // wxMenuBar accel handling
2134 // ----------------------------------------------------------------------------
2136 int wxMenuBar::FindNextItemForAccel(int idxStart
, int key
, bool *unique
) const
2138 if ( !wxIsalnum(key
) )
2140 // we only support letters/digits as accels
2144 // do we have more than one item with this accel?
2148 // translate everything to lower case before comparing
2149 wxChar chAccel
= wxTolower(key
);
2151 // the index of the item with this accel
2154 // loop through all items searching for the item with this
2155 // accel starting at the item after the current one
2156 int count
= GetCount();
2157 int n
= idxStart
== -1 ? 0 : idxStart
+ 1;
2168 const wxMenuInfo
& info
= m_menuInfos
[n
];
2170 int idxAccel
= info
.GetAccelIndex();
2171 if ( idxAccel
!= -1 &&
2172 wxTolower(info
.GetLabel()[(size_t)idxAccel
])
2175 // ok, found an item with this accel
2176 if ( idxFound
== -1 )
2178 // store it but continue searching as we need to
2179 // know if it's the only item with this accel or if
2183 else // we already had found such item
2188 // no need to continue further, we won't find
2189 // anything we don't already know
2194 // we want to iterate over all items wrapping around if
2202 if ( n
== idxStart
)
2204 // we've seen all items
2214 bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent
& event
)
2217 for ( wxMenuList::Node
*node
= m_menus
.GetFirst();
2219 node
= node
->GetNext(), n
++ )
2221 // accels of the items in the disabled menus shouldn't work
2222 if ( m_menuInfos
[n
].IsEnabled() )
2224 if ( node
->GetData()->ProcessAccelEvent(event
) )
2226 // menu processed it
2236 #endif // wxUSE_ACCEL
2238 // ----------------------------------------------------------------------------
2239 // wxMenuBar menus showing
2240 // ----------------------------------------------------------------------------
2242 void wxMenuBar::PopupCurrentMenu(bool selectFirst
)
2244 wxCHECK_RET( m_current
!= -1, _T("no menu to popup") );
2246 // forgot to call DismissMenu()?
2247 wxASSERT_MSG( !m_menuShown
, _T("shouldn't show two menu at once!") );
2249 // in any case, we should show it - even if we won't
2250 m_shouldShowMenu
= TRUE
;
2252 if ( IsEnabledTop(m_current
) )
2254 // remember the menu we show
2255 m_menuShown
= GetMenu(m_current
);
2257 // we don't show the menu at all if it has no items
2258 if ( !m_menuShown
->IsEmpty() )
2260 // position it correctly: note that we must use screen coords and
2261 // that we pass 0 as width to position the menu exactly below the
2262 // item, not to the right of it
2263 wxRect rectItem
= GetItemRect(m_current
);
2265 m_menuShown
->Popup(ClientToScreen(rectItem
.GetPosition()),
2266 wxSize(0, rectItem
.GetHeight()),
2271 // reset it back as no menu is shown
2275 //else: don't show disabled menu
2278 void wxMenuBar::DismissMenu()
2280 wxCHECK_RET( m_menuShown
, _T("can't dismiss menu if none is shown") );
2282 m_menuShown
->Dismiss();
2286 void wxMenuBar::OnDismissMenu(bool dismissMenuBar
)
2288 m_shouldShowMenu
= FALSE
;
2290 if ( dismissMenuBar
)
2296 void wxMenuBar::OnDismiss()
2300 if ( m_current
!= -1 )
2302 size_t current
= m_current
;
2305 RefreshItem(current
);
2311 void wxMenuBar::GiveAwayFocus()
2313 GetFrame()->SetFocus();
2316 // ----------------------------------------------------------------------------
2317 // popup menu support
2318 // ----------------------------------------------------------------------------
2320 wxEventLoop
*wxWindow::ms_evtLoopPopup
= NULL
;
2322 bool wxWindow::DoPopupMenu(wxMenu
*menu
, int x
, int y
)
2324 wxCHECK_MSG( !ms_evtLoopPopup
, FALSE
,
2325 _T("can't show more than one popup menu at a time") );
2328 // we need to change the cursor before showing the menu as, apparently, no
2329 // cursor changes took place while the mouse is captured
2330 wxCursor cursorOld
= GetCursor();
2331 SetCursor(wxCURSOR_ARROW
);
2335 // flash any delayed log messages before showing the menu, otherwise it
2336 // could be dismissed (because it would lose focus) immediately after being
2338 wxLog::FlushActive();
2340 // some controls update themselves from OnIdle() call - let them do it
2342 wxTheApp
->ProcessEvent(event
);
2344 // if the window hadn't been refreshed yet, the menu can adversely affect
2345 // its next OnPaint() handler execution - i.e. scrolled window refresh
2346 // logic breaks then as it scrolls part of the menu which hadn't been there
2347 // when the update event was generated into view
2351 menu
->SetInvokingWindow(this);
2352 menu
->Popup(ClientToScreen(wxPoint(x
, y
)), wxSize(0, 0));
2354 // this is not very useful if the menu was popped up because of the mouse
2355 // click but I think it is nice to do when it appears because of a key
2356 // press (i.e. Windows menu key)
2358 // Windows itself doesn't do it, but IMHO this is nice
2361 // we have to redirect all keyboard input to the menu temporarily
2362 PushEventHandler(new wxMenuKbdRedirector(menu
));
2364 // enter the local modal loop
2365 ms_evtLoopPopup
= new wxEventLoop
;
2366 ms_evtLoopPopup
->Run();
2368 delete ms_evtLoopPopup
;
2369 ms_evtLoopPopup
= NULL
;
2371 // remove the handler
2372 PopEventHandler(TRUE
/* delete it */);
2374 menu
->SetInvokingWindow(NULL
);
2377 SetCursor(cursorOld
);
2383 void wxWindow::DismissPopupMenu()
2385 wxCHECK_RET( ms_evtLoopPopup
, _T("no popup menu shown") );
2387 ms_evtLoopPopup
->Exit();
2390 #endif // wxUSE_MENUS