]>
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::GetFont(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 ~wxPopupMenuWindow();
129 // override the base class version to select the first item initially
130 virtual void Popup(wxWindow
*focus
= NULL
);
132 // override the base class version to dismiss any open submenus
133 virtual void Dismiss();
135 // notify the menu when the window disappears from screen
136 virtual void OnDismiss();
138 // called when a submenu is dismissed
139 void OnSubmenuDismiss() { m_hasOpenSubMenu
= FALSE
; }
141 // get the currently selected item (may be NULL)
142 wxMenuItem
*GetCurrentItem() const
144 return m_nodeCurrent
? m_nodeCurrent
->GetData() : NULL
;
147 // find the menu item at given position
148 wxMenuItemList::Node
*GetMenuItemFromPoint(const wxPoint
& pt
) const;
150 // refresh the given item
151 void RefreshItem(wxMenuItem
*item
);
153 // preselect the first item
154 void SelectFirst() { SetCurrent(m_menu
->GetMenuItems().GetFirst()); }
156 // process the key event, return TRUE if done
157 bool ProcessKeyDown(int key
);
159 // process mouse move event
160 void ProcessMouseMove(const wxPoint
& pt
);
162 // don't dismiss the popup window if the parent menu was clicked
163 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
166 // how did we perform this operation?
173 // draw the menu inside this window
174 virtual void DoDraw(wxControlRenderer
*renderer
);
177 void OnLeftUp(wxMouseEvent
& event
);
178 void OnMouseMove(wxMouseEvent
& event
);
179 void OnMouseLeave(wxMouseEvent
& event
);
180 void OnKeyDown(wxKeyEvent
& event
);
182 // reset the current item and node
185 // set the current node and item withotu refreshing anything
186 void SetCurrent(wxMenuItemList::Node
*node
);
188 // change the current item refreshing the old and new items
189 void ChangeCurrent(wxMenuItemList::Node
*node
);
191 // activate item, i.e. call either ClickItem() or OpenSubmenu() depending
192 // on what it is, return TRUE if something was done (i.e. it's not a
194 bool ActivateItem(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
196 // send the event about the item click
197 void ClickItem(wxMenuItem
*item
);
199 // show the submenu for this item
200 void OpenSubmenu(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
202 // can this tiem be opened?
203 bool CanOpen(wxMenuItem
*item
)
205 return item
&& item
->IsEnabled() && item
->IsSubMenu();
208 // dismiss the menu and all parent menus too
209 void DismissAndNotify();
211 // react to dimissing this menu and also dismiss the parent if
213 void HandleDismiss(bool dismissParent
);
215 // do we have an open submenu?
216 bool HasOpenSubmenu() const { return m_hasOpenSubMenu
; }
218 // get previous node after the current one
219 wxMenuItemList::Node
*GetPrevNode() const;
221 // get previous node before the given one, wrapping if it's the first one
222 wxMenuItemList::Node
*GetPrevNode(wxMenuItemList::Node
*node
) const;
224 // get next node after the current one
225 wxMenuItemList::Node
*GetNextNode() const;
227 // get next node after the given one, wrapping if it's the last one
228 wxMenuItemList::Node
*GetNextNode(wxMenuItemList::Node
*node
) const;
234 // the menu node corresponding to the current item
235 wxMenuItemList::Node
*m_nodeCurrent
;
237 // do we currently have an opened submenu?
238 bool m_hasOpenSubMenu
;
240 DECLARE_EVENT_TABLE()
243 // ----------------------------------------------------------------------------
244 // wxMenuKbdRedirector: an event handler which redirects kbd input to wxMenu
245 // ----------------------------------------------------------------------------
247 class wxMenuKbdRedirector
: public wxEvtHandler
250 wxMenuKbdRedirector(wxMenu
*menu
) { m_menu
= menu
; }
252 virtual bool ProcessEvent(wxEvent
& event
)
254 if ( event
.GetEventType() == wxEVT_KEY_DOWN
)
256 return m_menu
->ProcessKeyDown(((wxKeyEvent
&)event
).GetKeyCode());
262 return wxEvtHandler::ProcessEvent(event
);
270 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
274 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
275 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
276 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
278 BEGIN_EVENT_TABLE(wxPopupMenuWindow
, wxPopupTransientWindow
)
279 EVT_KEY_DOWN(wxPopupMenuWindow::OnKeyDown
)
281 EVT_LEFT_UP(wxPopupMenuWindow::OnLeftUp
)
282 EVT_MOTION(wxPopupMenuWindow::OnMouseMove
)
283 EVT_LEAVE_WINDOW(wxPopupMenuWindow::OnMouseLeave
)
286 BEGIN_EVENT_TABLE(wxMenuBar
, wxMenuBarBase
)
287 EVT_KILL_FOCUS(wxMenuBar::OnKillFocus
)
289 EVT_KEY_DOWN(wxMenuBar::OnKeyDown
)
291 EVT_LEFT_DOWN(wxMenuBar::OnLeftDown
)
292 EVT_MOTION(wxMenuBar::OnMouseMove
)
295 // ============================================================================
297 // ============================================================================
299 // ----------------------------------------------------------------------------
301 // ----------------------------------------------------------------------------
303 wxPopupMenuWindow::wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
)
306 m_hasOpenSubMenu
= FALSE
;
310 (void)Create(parent
, wxBORDER_RAISED
);
312 SetCursor(wxCURSOR_ARROW
);
315 wxPopupMenuWindow::~wxPopupMenuWindow()
319 // ----------------------------------------------------------------------------
320 // wxPopupMenuWindow current item/node handling
321 // ----------------------------------------------------------------------------
323 void wxPopupMenuWindow::ResetCurrent()
328 void wxPopupMenuWindow::SetCurrent(wxMenuItemList::Node
*node
)
330 m_nodeCurrent
= node
;
333 void wxPopupMenuWindow::ChangeCurrent(wxMenuItemList::Node
*node
)
335 if ( node
!= m_nodeCurrent
)
337 wxMenuItemList::Node
*nodeOldCurrent
= m_nodeCurrent
;
339 m_nodeCurrent
= node
;
341 if ( nodeOldCurrent
)
343 wxMenuItem
*item
= nodeOldCurrent
->GetData();
344 wxCHECK_RET( item
, _T("no current item?") );
346 // if it was the currently opened menu, close it
347 if ( item
->IsSubMenu() && item
->GetSubMenu()->IsShown() )
349 item
->GetSubMenu()->Dismiss();
357 RefreshItem(m_nodeCurrent
->GetData());
361 wxMenuItemList::Node
*wxPopupMenuWindow::GetPrevNode() const
363 // return the last node if there had been no previously selected one
364 return m_nodeCurrent
? GetPrevNode(m_nodeCurrent
)
365 : m_menu
->GetMenuItems().GetLast();
368 wxMenuItemList::Node
*
369 wxPopupMenuWindow::GetPrevNode(wxMenuItemList::Node
*node
) const
373 node
= node
->GetPrevious();
376 node
= m_menu
->GetMenuItems().GetLast();
379 //else: the menu is empty
384 wxMenuItemList::Node
*wxPopupMenuWindow::GetNextNode() const
386 // return the first node if there had been no previously selected one
387 return m_nodeCurrent
? GetNextNode(m_nodeCurrent
)
388 : m_menu
->GetMenuItems().GetFirst();
391 wxMenuItemList::Node
*
392 wxPopupMenuWindow::GetNextNode(wxMenuItemList::Node
*node
) const
396 node
= node
->GetNext();
399 node
= m_menu
->GetMenuItems().GetFirst();
402 //else: the menu is empty
407 // ----------------------------------------------------------------------------
408 // wxPopupMenuWindow popup/dismiss
409 // ----------------------------------------------------------------------------
411 void wxPopupMenuWindow::Popup(wxWindow
*focus
)
413 // check that the current item had been properly reset before
414 wxASSERT_MSG( !m_nodeCurrent
||
415 m_nodeCurrent
== m_menu
->GetMenuItems().GetFirst(),
416 _T("menu current item preselected incorrectly") );
418 wxPopupTransientWindow::Popup(focus
);
421 // ensure that this window is really on top of everything: without using
422 // SetWindowPos() it can be covered by its parent menu which is not
423 // really what we want
424 wxMenu
*menuParent
= m_menu
->GetParent();
427 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
429 // if we're shown, the parent menu must be also shown
430 wxCHECK_RET( win
, _T("parent menu is not shown?") );
432 if ( !::SetWindowPos(GetHwndOf(win
), GetHwnd(),
434 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOREDRAW
) )
436 wxLogLastError(_T("SetWindowPos(HWND_TOP)"));
444 void wxPopupMenuWindow::Dismiss()
446 if ( HasOpenSubmenu() )
448 wxMenuItem
*item
= GetCurrentItem();
449 wxCHECK_RET( item
&& item
->IsSubMenu(), _T("where is our open submenu?") );
451 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
452 wxCHECK_RET( win
, _T("opened submenu is not opened?") );
458 wxPopupTransientWindow::Dismiss();
461 void wxPopupMenuWindow::OnDismiss()
463 // when we are dismissed because the user clicked elsewhere or we lost
464 // focus in any other way, hide the parent menu as well
468 void wxPopupMenuWindow::HandleDismiss(bool dismissParent
)
472 m_menu
->OnDismiss(dismissParent
);
475 void wxPopupMenuWindow::DismissAndNotify()
481 // ----------------------------------------------------------------------------
482 // wxPopupMenuWindow geometry
483 // ----------------------------------------------------------------------------
485 wxMenuItemList::Node
*
486 wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint
& pt
) const
488 // we only use the y coord normally, but still check x in case the point is
489 // outside the window completely
490 if ( wxWindow::HitTest(pt
) == wxHT_WINDOW_INSIDE
)
493 for ( wxMenuItemList::Node
*node
= m_menu
->GetMenuItems().GetFirst();
495 node
= node
->GetNext() )
497 wxMenuItem
*item
= node
->GetData();
498 y
+= item
->GetHeight();
510 // ----------------------------------------------------------------------------
511 // wxPopupMenuWindow drawing
512 // ----------------------------------------------------------------------------
514 void wxPopupMenuWindow::RefreshItem(wxMenuItem
*item
)
516 wxCHECK_RET( item
, _T("can't refresh NULL item") );
518 wxASSERT_MSG( IsShown(), _T("can't refresh menu which is not shown") );
520 // FIXME: -1 here because of SetLogicalOrigin(1, 1) in DoDraw()
521 RefreshRect(wxRect(0, item
->GetPosition() - 1,
522 m_menu
->GetGeometryInfo().GetSize().x
, item
->GetHeight()));
525 void wxPopupMenuWindow::DoDraw(wxControlRenderer
*renderer
)
527 // no clipping so far - do we need it? I don't think so as the menu is
528 // never partially covered as it is always on top of everything
530 wxDC
& dc
= renderer
->GetDC();
531 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
533 // FIXME: this should be done in the renderer, however when it is fixed
534 // wxPopupMenuWindow::RefreshItem() should be changed too!
535 dc
.SetLogicalOrigin(1, 1);
537 wxRenderer
*rend
= renderer
->GetRenderer();
540 const wxMenuGeometryInfo
& gi
= m_menu
->GetGeometryInfo();
541 for ( wxMenuItemList::Node
*node
= m_menu
->GetMenuItems().GetFirst();
543 node
= node
->GetNext() )
545 wxMenuItem
*item
= node
->GetData();
547 if ( item
->IsSeparator() )
549 rend
->DrawMenuSeparator(dc
, y
, gi
);
551 else // not a separator
554 if ( item
->IsCheckable() )
556 flags
|= wxCONTROL_CHECKABLE
;
558 if ( item
->IsChecked() )
560 flags
|= wxCONTROL_CHECKED
;
564 if ( !item
->IsEnabled() )
565 flags
|= wxCONTROL_DISABLED
;
567 if ( item
->IsSubMenu() )
568 flags
|= wxCONTROL_ISSUBMENU
;
570 if ( item
== GetCurrentItem() )
571 flags
|= wxCONTROL_SELECTED
;
579 item
->GetAccelString(),
580 // strangely enough, for unchecked item we use the
581 // "checked" bitmap because this is the default one - this
582 // explains this strange boolean expression
583 item
->GetBitmap(!item
->IsCheckable() || item
->IsChecked()),
585 item
->GetAccelIndex()
589 y
+= item
->GetHeight();
593 // ----------------------------------------------------------------------------
594 // wxPopupMenuWindow actions
595 // ----------------------------------------------------------------------------
597 void wxPopupMenuWindow::ClickItem(wxMenuItem
*item
)
599 wxCHECK_RET( item
, _T("can't click NULL item") );
601 wxASSERT_MSG( !item
->IsSeparator() && !item
->IsSubMenu(),
602 _T("can't click this item") );
604 wxMenu
* menu
= m_menu
;
609 menu
->ClickItem(item
);
612 void wxPopupMenuWindow::OpenSubmenu(wxMenuItem
*item
, InputMethod how
)
614 wxCHECK_RET( item
, _T("can't open NULL submenu") );
616 wxMenu
*submenu
= item
->GetSubMenu();
617 wxCHECK_RET( submenu
, _T("can only open submenus!") );
619 // FIXME: should take into account the border width
620 submenu
->Popup(ClientToScreen(wxPoint(0, item
->GetPosition())),
621 wxSize(m_menu
->GetGeometryInfo().GetSize().x
, 0),
622 how
== WithKeyboard
/* preselect first item then */);
624 m_hasOpenSubMenu
= TRUE
;
627 bool wxPopupMenuWindow::ActivateItem(wxMenuItem
*item
, InputMethod how
)
629 // don't activate disabled items
630 if ( !item
|| !item
->IsEnabled() )
635 // normal menu items generate commands, submenus can be opened and
636 // the separators don't do anything
637 if ( item
->IsSubMenu() )
639 OpenSubmenu(item
, how
);
641 else if ( !item
->IsSeparator() )
645 else // separator, can't activate
653 // ----------------------------------------------------------------------------
654 // wxPopupMenuWindow input handling
655 // ----------------------------------------------------------------------------
657 bool wxPopupMenuWindow::ProcessLeftDown(wxMouseEvent
& event
)
659 // wxPopupWindowHandler dismisses the window when the mouse is clicked
660 // outside it which is usually just fine, but there is one case when we
661 // don't want to do it: if the mouse was clicked on the parent submenu item
662 // which opens this menu, so check for it
664 wxPoint pos
= event
.GetPosition();
665 if ( HitTest(pos
.x
, pos
.y
) == wxHT_WINDOW_OUTSIDE
)
667 wxMenu
*menu
= m_menu
->GetParent();
670 wxPopupMenuWindow
*win
= menu
->m_popupMenu
;
672 wxCHECK_MSG( win
, FALSE
, _T("parent menu not shown?") );
674 pos
= ClientToScreen(pos
);
675 if ( win
->GetMenuItemFromPoint(win
->ScreenToClient(pos
)) )
680 //else: it is outside the parent menu as well, do dismiss this one
687 void wxPopupMenuWindow::OnLeftUp(wxMouseEvent
& event
)
689 wxMenuItemList::Node
*node
= GetMenuItemFromPoint(event
.GetPosition());
692 ActivateItem(node
->GetData(), WithMouse
);
696 void wxPopupMenuWindow::OnMouseMove(wxMouseEvent
& event
)
698 const wxPoint pt
= event
.GetPosition();
700 // we need to ignore extra mouse events: example when this happens is when
701 // the mouse is on the menu and we open a submenu from keyboard - Windows
702 // then sends us a dummy mouse move event, we (correctly) determine that it
703 // happens in the parent menu and so immediately close the just opened
706 static wxPoint s_ptLast
;
707 wxPoint ptCur
= ClientToScreen(pt
);
708 if ( ptCur
== s_ptLast
)
716 ProcessMouseMove(pt
);
721 void wxPopupMenuWindow::ProcessMouseMove(const wxPoint
& pt
)
723 wxMenuItemList::Node
*node
= GetMenuItemFromPoint(pt
);
725 // don't reset current to NULL here, we only do it when the mouse leaves
726 // the window (see below)
729 if ( node
!= m_nodeCurrent
)
733 wxMenuItem
*item
= GetCurrentItem();
736 OpenSubmenu(item
, WithMouse
);
739 //else: same item, nothing to do
741 else // not on an item
743 // the last open submenu forwards the mouse move messages to its
744 // parent, so if the mouse moves to another item of the parent menu,
745 // this menu is closed and this other item is selected - in the similar
746 // manner, the top menu forwards the mouse moves to the menubar which
747 // allows to select another top level menu by just moving the mouse
749 // we need to translate our client coords to the client coords of the
750 // window we forward this event to
751 wxPoint ptScreen
= ClientToScreen(pt
);
753 // if the mouse is outside this menu, let the parent one to
755 wxMenu
*menuParent
= m_menu
->GetParent();
758 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
760 // if we're shown, the parent menu must be also shown
761 wxCHECK_RET( win
, _T("parent menu is not shown?") );
763 win
->ProcessMouseMove(win
->ScreenToClient(ptScreen
));
765 else // no parent menu
767 wxMenuBar
*menubar
= m_menu
->GetMenuBar();
770 if ( menubar
->ProcessMouseEvent(
771 menubar
->ScreenToClient(ptScreen
)) )
773 // menubar has closed this menu and opened another one, probably
778 //else: top level popup menu, no other processing to do
782 void wxPopupMenuWindow::OnMouseLeave(wxMouseEvent
& event
)
784 // due to the artefact of mouse events generation under MSW, we actually
785 // may get the mouse leave event after the menu had been already dismissed
786 // and calling ChangeCurrent() would then assert, so don't do it
789 // we shouldn't change the current them if our submenu is opened and
790 // mouse moved there, in this case the submenu is responsable for
793 if ( HasOpenSubmenu() )
795 wxMenuItem
*item
= GetCurrentItem();
796 wxCHECK_RET( CanOpen(item
), _T("where is our open submenu?") );
798 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
799 wxCHECK_RET( win
, _T("submenu is opened but not shown?") );
801 // only handle this event if the mouse is not inside the submenu
802 wxPoint pt
= ClientToScreen(event
.GetPosition());
804 win
->HitTest(win
->ScreenToClient(pt
)) == wxHT_WINDOW_OUTSIDE
;
808 // this menu is the last opened
821 void wxPopupMenuWindow::OnKeyDown(wxKeyEvent
& event
)
823 if ( !ProcessKeyDown(event
.GetKeyCode()) )
829 bool wxPopupMenuWindow::ProcessKeyDown(int key
)
831 wxMenuItem
*item
= GetCurrentItem();
833 // first let the opened submenu to have it (no test for IsEnabled() here,
834 // the keys navigate even in a disabled submenu if we had somehow managed
835 // to open it inspit of this)
836 if ( HasOpenSubmenu() )
838 wxCHECK_MSG( CanOpen(item
), FALSE
,
839 _T("has open submenu but another item selected?") );
841 if ( item
->GetSubMenu()->ProcessKeyDown(key
) )
845 bool processed
= TRUE
;
847 // handle the up/down arrows, home, end, esc and return here, pass the
848 // left/right arrows to the menu bar except when the right arrow can be
849 // used to open a submenu
853 // if we're not a top level menu, close us, else leave this to the
855 if ( !m_menu
->GetParent() )
864 // close just this menu
866 HandleDismiss(FALSE
);
870 processed
= ActivateItem(item
);
874 ChangeCurrent(m_menu
->GetMenuItems().GetFirst());
878 ChangeCurrent(m_menu
->GetMenuItems().GetLast());
884 bool up
= key
== WXK_UP
;
886 wxMenuItemList::Node
*nodeStart
= up
? GetPrevNode()
889 while ( node
&& node
->GetData()->IsSeparator() )
891 node
= up
? GetPrevNode(node
) : GetNextNode(node
);
893 if ( node
== nodeStart
)
895 // nothing but separators and disabled items in this
913 // don't try to reopen an already opened menu
914 if ( !HasOpenSubmenu() && CanOpen(item
) )
925 // look for the menu item starting with this letter
926 if ( wxIsalnum(key
) )
928 // we want to start from the item after this one because
929 // if we're already on the item with the given accel we want to
930 // go to the next one, not to stay in place
931 wxMenuItemList::Node
*nodeStart
= GetNextNode();
933 // do we have more than one item with this accel?
934 bool notUnique
= FALSE
;
936 // translate everything to lower case before comparing
937 wxChar chAccel
= wxTolower(key
);
939 // loop through all items searching for the item with this
941 wxMenuItemList::Node
*node
= nodeStart
,
945 item
= node
->GetData();
947 int idxAccel
= item
->GetAccelIndex();
948 if ( idxAccel
!= -1 &&
949 wxTolower(item
->GetLabel()[(size_t)idxAccel
])
952 // ok, found an item with this accel
955 // store it but continue searching as we need to
956 // know if it's the only item with this accel or if
960 else // we already had found such item
964 // no need to continue further, we won't find
965 // anything we don't already know
970 // we want to iterate over all items wrapping around if
972 node
= GetNextNode(node
);
973 if ( node
== nodeStart
)
975 // we've seen all nodes
982 item
= nodeFound
->GetData();
984 // go to this item anyhow
985 ChangeCurrent(nodeFound
);
987 if ( !notUnique
&& item
->IsEnabled() )
989 // unique item with this accel - activate it
990 processed
= ActivateItem(item
);
992 //else: just select it but don't activate as the user might
993 // have wanted to activate another item
995 // skip "processed = FALSE" below
1006 // ----------------------------------------------------------------------------
1008 // ----------------------------------------------------------------------------
1023 // ----------------------------------------------------------------------------
1024 // wxMenu and wxMenuGeometryInfo
1025 // ----------------------------------------------------------------------------
1027 wxMenuGeometryInfo::~wxMenuGeometryInfo()
1031 const wxMenuGeometryInfo
& wxMenu::GetGeometryInfo() const
1037 wxConstCast(this, wxMenu
)->m_geometry
=
1038 m_popupMenu
->GetRenderer()->GetMenuGeometry(m_popupMenu
, *this);
1042 wxFAIL_MSG( _T("can't get geometry without window") );
1049 void wxMenu::InvalidateGeometryInfo()
1058 // ----------------------------------------------------------------------------
1059 // wxMenu adding/removing items
1060 // ----------------------------------------------------------------------------
1062 void wxMenu::OnItemAdded(wxMenuItem
*item
)
1064 InvalidateGeometryInfo();
1068 #endif // wxUSE_ACCEL
1070 // the submenus of a popup menu should have the same invoking window as it
1072 if ( m_invokingWindow
&& item
->IsSubMenu() )
1074 item
->GetSubMenu()->SetInvokingWindow(m_invokingWindow
);
1078 bool wxMenu::DoAppend(wxMenuItem
*item
)
1080 if ( !wxMenuBase::DoAppend(item
) )
1088 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1090 if ( !wxMenuBase::DoInsert(pos
, item
) )
1098 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1100 wxMenuItem
*itemOld
= wxMenuBase::DoRemove(item
);
1104 InvalidateGeometryInfo();
1107 RemoveAccelFor(item
);
1108 #endif // wxUSE_ACCEL
1114 // ----------------------------------------------------------------------------
1115 // wxMenu attaching/detaching
1116 // ----------------------------------------------------------------------------
1118 void wxMenu::Attach(wxMenuBarBase
*menubar
)
1120 wxMenuBase::Attach(menubar
);
1122 wxCHECK_RET( m_menuBar
, _T("menubar can't be NULL after attaching") );
1124 // unfortunately, we can't use m_menuBar->GetEventHandler() here because,
1125 // if the menubar is currently showing a menu, its event handler is a
1126 // temporary one installed by wxPopupWindow and so will disappear soon any
1127 // any attempts to use it from the newly attached menu would result in a
1130 // so we use the menubar itself, even if it's a pity as it means we can't
1131 // redirect all menu events by changing the menubar handler (FIXME)
1132 SetNextHandler(m_menuBar
);
1135 void wxMenu::Detach()
1137 wxMenuBase::Detach();
1140 // ----------------------------------------------------------------------------
1141 // wxMenu misc functions
1142 // ----------------------------------------------------------------------------
1144 wxWindow
*wxMenu::GetRootWindow() const
1148 // simple case - a normal menu attached to the menubar
1152 // we're a popup menu but the trouble is that only the top level popup menu
1153 // has a pointer to the invoking window, so we must walk up the menu chain
1155 wxWindow
*win
= GetInvokingWindow();
1158 // we already have it
1162 wxMenu
*menu
= GetParent();
1165 // We are a submenu of a menu of a menubar
1166 if (menu
->GetMenuBar())
1167 return menu
->GetMenuBar();
1169 win
= menu
->GetInvokingWindow();
1173 menu
= menu
->GetParent();
1176 // we're probably going to crash in the caller anyhow, but try to detect
1177 // this error as soon as possible
1178 wxASSERT_MSG( win
, _T("menu without any associated window?") );
1180 // also remember it in this menu so that we don't have to search for it the
1182 wxConstCast(this, wxMenu
)->m_invokingWindow
= win
;
1187 wxRenderer
*wxMenu::GetRenderer() const
1189 // we're going to crash without renderer!
1190 wxCHECK_MSG( m_popupMenu
, NULL
, _T("neither popup nor menubar menu?") );
1192 return m_popupMenu
->GetRenderer();
1195 void wxMenu::RefreshItem(wxMenuItem
*item
)
1197 // the item geometry changed, so our might have changed as well
1198 InvalidateGeometryInfo();
1202 // this would be a bug in IsShown()
1203 wxCHECK_RET( m_popupMenu
, _T("must have popup window if shown!") );
1205 // recalc geometry to update the item height and such
1206 (void)GetGeometryInfo();
1208 m_popupMenu
->RefreshItem(item
);
1212 // ----------------------------------------------------------------------------
1213 // wxMenu showing and hiding
1214 // ----------------------------------------------------------------------------
1216 bool wxMenu::IsShown() const
1218 return m_popupMenu
&& m_popupMenu
->IsShown();
1221 void wxMenu::OnDismiss(bool dismissParent
)
1225 // always notify the parent about submenu disappearance
1226 wxPopupMenuWindow
*win
= m_menuParent
->m_popupMenu
;
1229 win
->OnSubmenuDismiss();
1233 wxFAIL_MSG( _T("parent menu not shown?") );
1236 // and if we dismiss everything, propagate to parent
1237 if ( dismissParent
)
1239 // dismissParent is recursive
1240 m_menuParent
->Dismiss();
1241 m_menuParent
->OnDismiss(TRUE
);
1244 else // no parent menu
1246 // notify the menu bar if we're a top level menu
1249 m_menuBar
->OnDismissMenu(dismissParent
);
1253 wxCHECK_RET( m_invokingWindow
, _T("what kind of menu is this?") );
1255 m_invokingWindow
->DismissPopupMenu();
1257 // Why reset it here? We need it for sending the event to...
1258 // SetInvokingWindow(NULL);
1263 void wxMenu::Popup(const wxPoint
& pos
, const wxSize
& size
, bool selectFirst
)
1265 // create the popup window if not done yet
1268 m_popupMenu
= new wxPopupMenuWindow(GetRootWindow(), this);
1271 // select the first item unless disabled
1274 m_popupMenu
->SelectFirst();
1277 // the geometry might have changed since the last time we were shown, so
1279 m_popupMenu
->SetClientSize(GetGeometryInfo().GetSize());
1281 // position it as specified
1282 m_popupMenu
->Position(pos
, size
);
1284 // the menu can't have the focus itself (it is a Windows limitation), so
1285 // always keep the focus at the originating window
1286 wxWindow
*focus
= GetRootWindow();
1288 wxASSERT_MSG( focus
, _T("no window to keep focus on?") );
1291 m_popupMenu
->Popup(focus
);
1294 void wxMenu::Dismiss()
1296 wxCHECK_RET( IsShown(), _T("can't dismiss hidden menu") );
1298 m_popupMenu
->Dismiss();
1301 // ----------------------------------------------------------------------------
1302 // wxMenu event processing
1303 // ----------------------------------------------------------------------------
1305 bool wxMenu::ProcessKeyDown(int key
)
1307 wxCHECK_MSG( m_popupMenu
, FALSE
,
1308 _T("can't process key events if not shown") );
1310 return m_popupMenu
->ProcessKeyDown(key
);
1313 bool wxMenu::ClickItem(wxMenuItem
*item
)
1316 if ( item
->IsCheckable() )
1318 // update the item state
1319 isChecked
= !item
->IsChecked();
1321 item
->Check(isChecked
!= 0);
1329 return SendEvent(item
->GetId(), isChecked
);
1332 // ----------------------------------------------------------------------------
1333 // wxMenu accel support
1334 // ----------------------------------------------------------------------------
1338 bool wxMenu::ProcessAccelEvent(const wxKeyEvent
& event
)
1340 // do we have an item for this accel?
1341 wxMenuItem
*item
= m_accelTable
.GetMenuItem(event
);
1342 if ( item
&& item
->IsEnabled() )
1344 return ClickItem(item
);
1348 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
1350 node
= node
->GetNext() )
1352 const wxMenuItem
*item
= node
->GetData();
1353 if ( item
->IsSubMenu() && item
->IsEnabled() )
1356 if ( item
->GetSubMenu()->ProcessAccelEvent(event
) )
1366 void wxMenu::AddAccelFor(wxMenuItem
*item
)
1368 wxAcceleratorEntry
*accel
= item
->GetAccel();
1371 accel
->SetMenuItem(item
);
1373 m_accelTable
.Add(*accel
);
1379 void wxMenu::RemoveAccelFor(wxMenuItem
*item
)
1381 wxAcceleratorEntry
*accel
= item
->GetAccel();
1384 m_accelTable
.Remove(*accel
);
1390 #endif // wxUSE_ACCEL
1392 // ----------------------------------------------------------------------------
1393 // wxMenuItem construction
1394 // ----------------------------------------------------------------------------
1396 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
1398 const wxString
& text
,
1399 const wxString
& help
,
1404 m_parentMenu
= parentMenu
;
1405 m_subMenu
= subMenu
;
1410 m_isCheckable
= isCheckable
;
1412 m_isChecked
= FALSE
;
1420 wxMenuItem::~wxMenuItem()
1424 // ----------------------------------------------------------------------------
1425 // wxMenuItemBase methods implemented here
1426 // ----------------------------------------------------------------------------
1429 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
1431 const wxString
& name
,
1432 const wxString
& help
,
1436 return new wxMenuItem(parentMenu
, id
, name
, help
, isCheckable
, subMenu
);
1440 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
1442 return wxStripMenuCodes(text
);
1445 // ----------------------------------------------------------------------------
1446 // wxMenuItem operations
1447 // ----------------------------------------------------------------------------
1449 void wxMenuItem::NotifyMenu()
1451 m_parentMenu
->RefreshItem(this);
1454 void wxMenuItem::UpdateAccelInfo()
1456 m_indexAccel
= wxControl::FindAccelIndex(m_text
);
1458 // will be empty if the text contains no TABs - ok
1459 m_strAccel
= m_text
.AfterFirst(_T('\t'));
1462 void wxMenuItem::SetText(const wxString
& text
)
1464 if ( text
!= m_text
)
1466 // first call the base class version to change m_text
1467 wxMenuItemBase::SetText(text
);
1475 void wxMenuItem::SetCheckable(bool checkable
)
1477 if ( checkable
!= m_isCheckable
)
1479 wxMenuItemBase::SetCheckable(checkable
);
1485 void wxMenuItem::SetBitmaps(const wxBitmap
& bmpChecked
,
1486 const wxBitmap
& bmpUnchecked
)
1488 m_bmpChecked
= bmpChecked
;
1489 m_bmpUnchecked
= bmpUnchecked
;
1494 void wxMenuItem::Enable(bool enable
)
1496 if ( enable
!= m_isEnabled
)
1498 wxMenuItemBase::Enable(enable
);
1504 void wxMenuItem::Check(bool check
)
1506 if ( check
!= m_isChecked
)
1508 wxMenuItemBase::Check(check
);
1514 // ----------------------------------------------------------------------------
1515 // wxMenuBar creation
1516 // ----------------------------------------------------------------------------
1518 void wxMenuBar::Init()
1526 m_shouldShowMenu
= FALSE
;
1529 void wxMenuBar::Attach(wxFrame
*frame
)
1531 // maybe you really wanted to call Detach()?
1532 wxCHECK_RET( frame
, _T("wxMenuBar::Attach(NULL) called") );
1534 wxMenuBarBase::Attach(frame
);
1538 // reparent if necessary
1539 if ( m_frameLast
!= frame
)
1544 // show it back - was hidden by Detach()
1547 else // not created yet, do it now
1549 // we have no way to return the error from here anyhow :-(
1550 (void)Create(frame
, -1);
1552 SetCursor(wxCURSOR_ARROW
);
1554 SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT
));
1556 // calculate and set our height (it won't be changed any more)
1557 SetSize(-1, GetBestSize().y
);
1560 // remember the last frame which had us to avoid unnecessarily reparenting
1562 m_frameLast
= frame
;
1565 void wxMenuBar::Detach()
1567 // don't delete the window because we may be reattached later, just hide it
1573 wxMenuBarBase::Detach();
1576 wxMenuBar::~wxMenuBar()
1580 // ----------------------------------------------------------------------------
1581 // wxMenuBar adding/removing items
1582 // ----------------------------------------------------------------------------
1584 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1586 return Insert(GetCount(), menu
, title
);
1589 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1591 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1594 wxMenuInfo
*info
= new wxMenuInfo(title
);
1595 m_menuInfos
.Insert(info
, pos
);
1597 RefreshAllItemsAfter(pos
);
1602 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1604 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1608 wxMenuInfo
& info
= m_menuInfos
[pos
];
1610 info
.SetLabel(title
);
1612 // even if the old menu was disabled, the new one is not any more
1615 // even if we change only this one, the new label has different width,
1616 // so we need to refresh everything beyond this item as well
1617 RefreshAllItemsAfter(pos
);
1623 wxMenu
*wxMenuBar::Remove(size_t pos
)
1625 wxMenu
*menuOld
= wxMenuBarBase::Remove(pos
);
1629 m_menuInfos
.RemoveAt(pos
);
1631 // this doesn't happen too often, so don't try to be too smart - just
1632 // refresh everything
1639 // ----------------------------------------------------------------------------
1640 // wxMenuBar top level menus access
1641 // ----------------------------------------------------------------------------
1643 wxCoord
wxMenuBar::GetItemWidth(size_t pos
) const
1645 return m_menuInfos
[pos
].GetWidth(wxConstCast(this, wxMenuBar
));
1648 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1650 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1652 if ( enable
!= m_menuInfos
[pos
].IsEnabled() )
1654 m_menuInfos
[pos
].SetEnabled(enable
);
1658 //else: nothing to do
1661 bool wxMenuBar::IsEnabledTop(size_t pos
) const
1663 wxCHECK_MSG( pos
< GetCount(), FALSE
, _T("invalid index in IsEnabledTop") );
1665 return m_menuInfos
[pos
].IsEnabled();
1668 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
1670 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1672 if ( label
!= m_menuInfos
[pos
].GetLabel() )
1674 m_menuInfos
[pos
].SetLabel(label
);
1678 //else: nothing to do
1681 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
1683 wxCHECK_MSG( pos
< GetCount(), _T(""), _T("invalid index in GetLabelTop") );
1685 return m_menuInfos
[pos
].GetLabel();
1688 // ----------------------------------------------------------------------------
1689 // wxMenuBar drawing
1690 // ----------------------------------------------------------------------------
1692 void wxMenuBar::RefreshAllItemsAfter(size_t pos
)
1696 // no need to refresh if nothing is shown yet
1700 wxRect rect
= GetItemRect(pos
);
1701 rect
.width
= GetClientSize().x
- rect
.x
;
1705 void wxMenuBar::RefreshItem(size_t pos
)
1707 wxCHECK_RET( pos
!= (size_t)-1,
1708 _T("invalid item in wxMenuBar::RefreshItem") );
1712 // no need to refresh if nothing is shown yet
1716 RefreshRect(GetItemRect(pos
));
1719 void wxMenuBar::DoDraw(wxControlRenderer
*renderer
)
1721 wxDC
& dc
= renderer
->GetDC();
1722 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1724 // redraw only the items which must be redrawn
1726 // we don't have to use GetUpdateClientRect() here because our client rect
1727 // is the same as total one
1728 wxRect rectUpdate
= GetUpdateRegion().GetBox();
1730 int flagsMenubar
= GetStateFlags();
1734 rect
.height
= GetClientSize().y
;
1737 size_t count
= GetCount();
1738 for ( size_t n
= 0; n
< count
; n
++ )
1740 if ( x
> rectUpdate
.GetRight() )
1742 // all remaining items are to the right of rectUpdate
1747 rect
.width
= GetItemWidth(n
);
1749 if ( x
< rectUpdate
.x
)
1751 // this item is still to the left of rectUpdate
1755 int flags
= flagsMenubar
;
1756 if ( m_current
!= -1 && n
== (size_t)m_current
)
1758 flags
|= wxCONTROL_SELECTED
;
1761 if ( !IsEnabledTop(n
) )
1763 flags
|= wxCONTROL_DISABLED
;
1766 GetRenderer()->DrawMenuBarItem
1770 m_menuInfos
[n
].GetLabel(),
1772 m_menuInfos
[n
].GetAccelIndex()
1777 // ----------------------------------------------------------------------------
1778 // wxMenuBar geometry
1779 // ----------------------------------------------------------------------------
1781 wxRect
wxMenuBar::GetItemRect(size_t pos
) const
1783 wxASSERT_MSG( pos
< GetCount(), _T("invalid menu bar item index") );
1784 wxASSERT_MSG( IsCreated(), _T("can't call this method yet") );
1789 rect
.height
= GetClientSize().y
;
1791 for ( size_t n
= 0; n
< pos
; n
++ )
1793 rect
.x
+= GetItemWidth(n
);
1796 rect
.width
= GetItemWidth(pos
);
1801 wxSize
wxMenuBar::DoGetBestClientSize() const
1804 if ( GetMenuCount() > 0 )
1806 wxClientDC
dc(wxConstCast(this, wxMenuBar
));
1807 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1808 dc
.GetTextExtent(GetLabelTop(0), &size
.x
, &size
.y
);
1810 // adjust for the renderer we use
1811 size
= GetRenderer()->GetMenuBarItemSize(size
);
1813 else // empty menubar
1819 // the width is arbitrary, of course, for horizontal menubar
1825 int wxMenuBar::GetMenuFromPoint(const wxPoint
& pos
) const
1827 if ( pos
.x
< 0 || pos
.y
< 0 || pos
.y
> GetClientSize().y
)
1832 size_t count
= GetCount();
1833 for ( size_t item
= 0; item
< count
; item
++ )
1835 x
+= GetItemWidth(item
);
1843 // to the right of the last menu item
1847 // ----------------------------------------------------------------------------
1848 // wxMenuBar menu operations
1849 // ----------------------------------------------------------------------------
1851 void wxMenuBar::SelectMenu(size_t pos
)
1854 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::SelectMenu"));
1860 void wxMenuBar::DoSelectMenu(size_t pos
)
1862 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in DoSelectMenu") );
1864 int posOld
= m_current
;
1870 // close the previous menu
1871 if ( IsShowingMenu() )
1873 // restore m_shouldShowMenu flag after DismissMenu() which resets
1875 bool old
= m_shouldShowMenu
;
1879 m_shouldShowMenu
= old
;
1882 RefreshItem((size_t)posOld
);
1888 void wxMenuBar::PopupMenu(size_t pos
)
1890 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in PopupCurrentMenu") );
1897 // ----------------------------------------------------------------------------
1898 // wxMenuBar input handing
1899 // ----------------------------------------------------------------------------
1902 Note that wxMenuBar doesn't use wxInputHandler but handles keyboard and
1903 mouse in the same way under all platforms. This is because it doesn't derive
1904 from wxControl (which works with input handlers) but directly from wxWindow.
1906 Also, menu bar input handling is rather simple, so maybe it's not really
1907 worth making it themeable - at least I've decided against doing it now as it
1908 would merging the changes back into trunk more difficult. But it still could
1909 be done later if really needed.
1912 void wxMenuBar::OnKillFocus(wxFocusEvent
& event
)
1914 if ( m_current
!= -1 )
1916 RefreshItem((size_t)m_current
);
1924 void wxMenuBar::OnLeftDown(wxMouseEvent
& event
)
1932 else // we didn't have mouse capture, capture it now
1934 m_current
= GetMenuFromPoint(event
.GetPosition());
1935 if ( m_current
== -1 )
1937 // unfortunately, we can't prevent wxMSW from giving us the focus,
1938 // so we can only give it back
1943 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::OnLeftDown"));
1946 // show it as selected
1947 RefreshItem((size_t)m_current
);
1950 PopupCurrentMenu(FALSE
/* don't select first item - as Windows does */);
1955 void wxMenuBar::OnMouseMove(wxMouseEvent
& event
)
1959 (void)ProcessMouseEvent(event
.GetPosition());
1967 bool wxMenuBar::ProcessMouseEvent(const wxPoint
& pt
)
1969 // a hack to ignore the extra mouse events MSW sends us: this is similar to
1970 // wxUSE_MOUSEEVENT_HACK in wxWin itself but it isn't enough for us here as
1971 // we get the messages from different windows (old and new popup menus for
1974 static wxPoint s_ptLast
;
1975 if ( pt
== s_ptLast
)
1983 int currentNew
= GetMenuFromPoint(pt
);
1984 if ( (currentNew
== -1) || (currentNew
== m_current
) )
1989 // select the new active item
1990 DoSelectMenu(currentNew
);
1992 // show the menu if we know that we should, even if we hadn't been showing
1993 // it before (this may happen if the previous menu was disabled)
1994 if ( m_shouldShowMenu
&& !m_menuShown
)
1996 // open the new menu if the old one we closed had been opened
1997 PopupCurrentMenu(FALSE
/* don't select first item - as Windows does */);
2003 void wxMenuBar::OnKeyDown(wxKeyEvent
& event
)
2005 // ensure that we have a current item - we might not have it if we're
2006 // given the focus with Alt or F10 press (and under GTK+ the menubar
2007 // somehow gets the keyboard events even when it doesn't have focus...)
2008 if ( m_current
== -1 )
2010 if ( !HasCapture() )
2014 else // we do have capture
2016 // we always maintain a valid current item while we're in modal
2017 // state (i.e. have the capture)
2018 wxFAIL_MSG( _T("how did we manage to lose current item?") );
2024 int key
= event
.GetKeyCode();
2026 // first let the menu have it
2027 if ( IsShowingMenu() && m_menuShown
->ProcessKeyDown(key
) )
2032 // cycle through the menu items when left/right arrows are pressed and open
2033 // the menu when up/down one is
2037 // Alt must be processed at wxWindow level too
2042 // remove the selection and give the focus away
2043 if ( m_current
!= -1 )
2045 if ( IsShowingMenu() )
2057 size_t count
= GetCount();
2060 // the item won't change anyhow
2063 //else: otherwise, it will
2065 // remember if we were showing a menu - if we did, we should
2066 // show the new menu after changing the item
2067 bool wasMenuOpened
= IsShowingMenu();
2068 if ( wasMenuOpened
)
2073 // cast is safe as we tested for -1 above
2074 size_t currentNew
= (size_t)m_current
;
2076 if ( key
== WXK_LEFT
)
2078 if ( currentNew
-- == 0 )
2079 currentNew
= count
- 1;
2083 if ( ++currentNew
== count
)
2087 DoSelectMenu(currentNew
);
2089 if ( wasMenuOpened
)
2104 // letters open the corresponding menu
2107 int idxFound
= FindNextItemForAccel(m_current
, key
, &unique
);
2109 if ( idxFound
!= -1 )
2111 if ( IsShowingMenu() )
2116 DoSelectMenu((size_t)idxFound
);
2118 // if the item is not unique, just select it but don't
2119 // activate as the user might have wanted to activate
2122 // also, don't try to open a disabled menu
2123 if ( unique
&& IsEnabledTop((size_t)idxFound
) )
2129 // skip the "event.Skip()" below
2138 // ----------------------------------------------------------------------------
2139 // wxMenuBar accel handling
2140 // ----------------------------------------------------------------------------
2142 int wxMenuBar::FindNextItemForAccel(int idxStart
, int key
, bool *unique
) const
2144 if ( !wxIsalnum(key
) )
2146 // we only support letters/digits as accels
2150 // do we have more than one item with this accel?
2154 // translate everything to lower case before comparing
2155 wxChar chAccel
= wxTolower(key
);
2157 // the index of the item with this accel
2160 // loop through all items searching for the item with this
2161 // accel starting at the item after the current one
2162 int count
= GetCount();
2163 int n
= idxStart
== -1 ? 0 : idxStart
+ 1;
2174 const wxMenuInfo
& info
= m_menuInfos
[n
];
2176 int idxAccel
= info
.GetAccelIndex();
2177 if ( idxAccel
!= -1 &&
2178 wxTolower(info
.GetLabel()[(size_t)idxAccel
])
2181 // ok, found an item with this accel
2182 if ( idxFound
== -1 )
2184 // store it but continue searching as we need to
2185 // know if it's the only item with this accel or if
2189 else // we already had found such item
2194 // no need to continue further, we won't find
2195 // anything we don't already know
2200 // we want to iterate over all items wrapping around if
2208 if ( n
== idxStart
)
2210 // we've seen all items
2220 bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent
& event
)
2223 for ( wxMenuList::Node
*node
= m_menus
.GetFirst();
2225 node
= node
->GetNext(), n
++ )
2227 // accels of the items in the disabled menus shouldn't work
2228 if ( m_menuInfos
[n
].IsEnabled() )
2230 if ( node
->GetData()->ProcessAccelEvent(event
) )
2232 // menu processed it
2242 #endif // wxUSE_ACCEL
2244 // ----------------------------------------------------------------------------
2245 // wxMenuBar menus showing
2246 // ----------------------------------------------------------------------------
2248 void wxMenuBar::PopupCurrentMenu(bool selectFirst
)
2250 wxCHECK_RET( m_current
!= -1, _T("no menu to popup") );
2252 // forgot to call DismissMenu()?
2253 wxASSERT_MSG( !m_menuShown
, _T("shouldn't show two menu at once!") );
2255 // in any case, we should show it - even if we won't
2256 m_shouldShowMenu
= TRUE
;
2258 if ( IsEnabledTop(m_current
) )
2260 // remember the menu we show
2261 m_menuShown
= GetMenu(m_current
);
2263 // we don't show the menu at all if it has no items
2264 if ( !m_menuShown
->IsEmpty() )
2266 // position it correctly: note that we must use screen coords and
2267 // that we pass 0 as width to position the menu exactly below the
2268 // item, not to the right of it
2269 wxRect rectItem
= GetItemRect(m_current
);
2271 m_menuShown
->Popup(ClientToScreen(rectItem
.GetPosition()),
2272 wxSize(0, rectItem
.GetHeight()),
2277 // reset it back as no menu is shown
2281 //else: don't show disabled menu
2284 void wxMenuBar::DismissMenu()
2286 wxCHECK_RET( m_menuShown
, _T("can't dismiss menu if none is shown") );
2288 m_menuShown
->Dismiss();
2292 void wxMenuBar::OnDismissMenu(bool dismissMenuBar
)
2294 m_shouldShowMenu
= FALSE
;
2296 if ( dismissMenuBar
)
2302 void wxMenuBar::OnDismiss()
2306 wxLogTrace(_T("mousecapture"), _T("Releasing mouse from wxMenuBar::OnDismiss"));
2307 GetCapture()->ReleaseMouse();
2310 if ( m_current
!= -1 )
2312 size_t current
= m_current
;
2315 RefreshItem(current
);
2321 void wxMenuBar::GiveAwayFocus()
2323 GetFrame()->SetFocus();
2326 // ----------------------------------------------------------------------------
2327 // popup menu support
2328 // ----------------------------------------------------------------------------
2330 wxEventLoop
*wxWindow::ms_evtLoopPopup
= NULL
;
2332 bool wxWindow::DoPopupMenu(wxMenu
*menu
, int x
, int y
)
2334 wxCHECK_MSG( !ms_evtLoopPopup
, FALSE
,
2335 _T("can't show more than one popup menu at a time") );
2338 // we need to change the cursor before showing the menu as, apparently, no
2339 // cursor changes took place while the mouse is captured
2340 wxCursor cursorOld
= GetCursor();
2341 SetCursor(wxCURSOR_ARROW
);
2345 // flash any delayed log messages before showing the menu, otherwise it
2346 // could be dismissed (because it would lose focus) immediately after being
2348 wxLog::FlushActive();
2350 // some controls update themselves from OnIdle() call - let them do it
2352 wxTheApp
->ProcessEvent(event
);
2354 // if the window hadn't been refreshed yet, the menu can adversely affect
2355 // its next OnPaint() handler execution - i.e. scrolled window refresh
2356 // logic breaks then as it scrolls part of the menu which hadn't been there
2357 // when the update event was generated into view
2361 menu
->SetInvokingWindow(this);
2363 // wxLogDebug( "Name of invoking window %s", menu->GetInvokingWindow()->GetName().c_str() );
2365 menu
->Popup(ClientToScreen(wxPoint(x
, y
)), wxSize(0, 0));
2367 // this is not very useful if the menu was popped up because of the mouse
2368 // click but I think it is nice to do when it appears because of a key
2369 // press (i.e. Windows menu key)
2371 // Windows itself doesn't do it, but IMHO this is nice
2374 // we have to redirect all keyboard input to the menu temporarily
2375 PushEventHandler(new wxMenuKbdRedirector(menu
));
2377 // enter the local modal loop
2378 ms_evtLoopPopup
= new wxEventLoop
;
2379 ms_evtLoopPopup
->Run();
2381 delete ms_evtLoopPopup
;
2382 ms_evtLoopPopup
= NULL
;
2384 // remove the handler
2385 PopEventHandler(TRUE
/* delete it */);
2387 menu
->SetInvokingWindow(NULL
);
2390 SetCursor(cursorOld
);
2396 void wxWindow::DismissPopupMenu()
2398 wxCHECK_RET( ms_evtLoopPopup
, _T("no popup menu shown") );
2400 ms_evtLoopPopup
->Exit();
2403 #endif // wxUSE_MENUS