1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/menu.cpp
3 // Purpose: wxMenuItem, wxMenu and wxMenuBar implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
29 #include "wx/dynarray.h"
30 #include "wx/control.h" // for FindAccelIndex()
32 #include "wx/settings.h"
37 #include "wx/popupwin.h"
38 #include "wx/evtloop.h"
39 #include "wx/dcclient.h"
42 #include "wx/univ/renderer.h"
45 #include "wx/msw/private.h"
48 // ----------------------------------------------------------------------------
49 // wxMenuInfo contains all extra information about top level menus we need
50 // ----------------------------------------------------------------------------
52 class WXDLLEXPORT wxMenuInfo
56 wxMenuInfo(const wxString
& text
)
64 void SetLabel(const wxString
& text
)
66 // remember the accel char (may be -1 if none)
67 m_indexAccel
= wxControl::FindAccelIndex(text
, &m_label
);
69 // calculate the width later, after the menu bar is created
73 void SetEnabled(bool enabled
= true) { m_isEnabled
= enabled
; }
77 const wxString
& GetLabel() const { return m_label
; }
78 bool IsEnabled() const { return m_isEnabled
; }
79 wxCoord
GetWidth(wxMenuBar
*menubar
) const
83 wxConstCast(this, wxMenuInfo
)->CalcWidth(menubar
);
89 int GetAccelIndex() const { return m_indexAccel
; }
92 void CalcWidth(wxMenuBar
*menubar
)
95 wxClientDC
dc(menubar
);
96 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
97 dc
.GetTextExtent(m_label
, &size
.x
, &size
.y
);
99 // adjust for the renderer we use and store the width
100 m_width
= menubar
->GetRenderer()->GetMenuBarItemSize(size
).x
;
109 #include "wx/arrimpl.cpp"
111 WX_DEFINE_OBJARRAY(wxMenuInfoArray
);
113 // ----------------------------------------------------------------------------
114 // wxPopupMenuWindow: a popup window showing a menu
115 // ----------------------------------------------------------------------------
117 class wxPopupMenuWindow
: public wxPopupTransientWindow
120 wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
);
122 ~wxPopupMenuWindow();
124 // override the base class version to select the first item initially
125 virtual void Popup(wxWindow
*focus
= NULL
);
127 // override the base class version to dismiss any open submenus
128 virtual void Dismiss();
130 // called when a submenu is dismissed
131 void OnSubmenuDismiss(bool dismissParent
);
133 // the default wxMSW wxPopupTransientWindow::OnIdle disables the capture
134 // when the cursor is inside the popup, which dsables the menu tracking
135 // so override it to do nothing
137 void OnIdle(wxIdleEvent
& WXUNUSED(event
)) { }
140 // get the currently selected item (may be NULL)
141 wxMenuItem
*GetCurrentItem() const
143 return m_nodeCurrent
? m_nodeCurrent
->GetData() : NULL
;
146 // find the menu item at given position
147 wxMenuItemList::compatibility_iterator
GetMenuItemFromPoint(const wxPoint
& pt
) const;
149 // refresh the given item
150 void RefreshItem(wxMenuItem
*item
);
152 // preselect the first item
153 void SelectFirst() { SetCurrent(m_menu
->GetMenuItems().GetFirst()); }
155 // process the key event, return true if done
156 bool ProcessKeyDown(int key
);
158 // process mouse move event
159 void ProcessMouseMove(const wxPoint
& pt
);
161 // don't dismiss the popup window if the parent menu was clicked
162 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
164 virtual bool SetCurrent(bool doit
= true) { return wxPopupTransientWindow::SetCurrent(doit
); };
167 // how did we perform this operation?
174 // notify the menu when the window disappears from screen
175 virtual void OnDismiss();
177 // draw the menu inside this window
178 virtual void DoDraw(wxControlRenderer
*renderer
);
181 void OnLeftUp(wxMouseEvent
& event
);
182 void OnMouseMove(wxMouseEvent
& event
);
183 void OnMouseLeave(wxMouseEvent
& event
);
184 void OnKeyDown(wxKeyEvent
& event
);
186 // reset the current item and node
189 // set the current node and item withotu refreshing anything
190 void SetCurrent(wxMenuItemList::compatibility_iterator node
);
192 // change the current item refreshing the old and new items
193 void ChangeCurrent(wxMenuItemList::compatibility_iterator node
);
195 // activate item, i.e. call either ClickItem() or OpenSubmenu() depending
196 // on what it is, return true if something was done (i.e. it's not a
198 bool ActivateItem(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
200 // send the event about the item click
201 void ClickItem(wxMenuItem
*item
);
203 // show the submenu for this item
204 void OpenSubmenu(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
206 // can this tiem be opened?
207 bool CanOpen(wxMenuItem
*item
)
209 return item
&& item
->IsEnabled() && item
->IsSubMenu();
212 // dismiss the menu and all parent menus too
213 void DismissAndNotify();
215 // react to dimissing this menu and also dismiss the parent if
217 void HandleDismiss(bool dismissParent
);
219 // do we have an open submenu?
220 bool HasOpenSubmenu() const { return m_hasOpenSubMenu
; }
222 // get previous node after the current one
223 wxMenuItemList::compatibility_iterator
GetPrevNode() const;
225 // get previous node before the given one, wrapping if it's the first one
226 wxMenuItemList::compatibility_iterator
GetPrevNode(wxMenuItemList::compatibility_iterator node
) const;
228 // get next node after the current one
229 wxMenuItemList::compatibility_iterator
GetNextNode() const;
231 // get next node after the given one, wrapping if it's the last one
232 wxMenuItemList::compatibility_iterator
GetNextNode(wxMenuItemList::compatibility_iterator node
) const;
238 // the menu node corresponding to the current item
239 wxMenuItemList::compatibility_iterator m_nodeCurrent
;
241 // do we currently have an opened submenu?
242 bool m_hasOpenSubMenu
;
244 DECLARE_EVENT_TABLE()
247 // ----------------------------------------------------------------------------
248 // wxMenuKbdRedirector: an event handler which redirects kbd input to wxMenu
249 // ----------------------------------------------------------------------------
251 class wxMenuKbdRedirector
: public wxEvtHandler
254 wxMenuKbdRedirector(wxMenu
*menu
) { m_menu
= menu
; }
256 virtual bool ProcessEvent(wxEvent
& event
)
258 if ( event
.GetEventType() == wxEVT_KEY_DOWN
)
260 return m_menu
->ProcessKeyDown(((wxKeyEvent
&)event
).GetKeyCode());
266 return wxEvtHandler::ProcessEvent(event
);
274 // ----------------------------------------------------------------------------
276 // ----------------------------------------------------------------------------
278 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
279 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
280 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
282 BEGIN_EVENT_TABLE(wxPopupMenuWindow
, wxPopupTransientWindow
)
283 EVT_KEY_DOWN(wxPopupMenuWindow::OnKeyDown
)
285 EVT_LEFT_UP(wxPopupMenuWindow::OnLeftUp
)
286 EVT_MOTION(wxPopupMenuWindow::OnMouseMove
)
287 EVT_LEAVE_WINDOW(wxPopupMenuWindow::OnMouseLeave
)
289 EVT_IDLE(wxPopupMenuWindow::OnIdle
)
293 BEGIN_EVENT_TABLE(wxMenuBar
, wxMenuBarBase
)
294 EVT_KILL_FOCUS(wxMenuBar::OnKillFocus
)
296 EVT_KEY_DOWN(wxMenuBar::OnKeyDown
)
298 EVT_LEFT_DOWN(wxMenuBar::OnLeftDown
)
299 EVT_MOTION(wxMenuBar::OnMouseMove
)
302 // ============================================================================
304 // ============================================================================
306 // ----------------------------------------------------------------------------
308 // ----------------------------------------------------------------------------
310 wxPopupMenuWindow::wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
)
313 m_hasOpenSubMenu
= false;
317 (void)Create(parent
, wxBORDER_RAISED
);
319 SetCursor(wxCURSOR_ARROW
);
322 wxPopupMenuWindow::~wxPopupMenuWindow()
324 // When m_popupMenu in wxMenu is deleted because it
325 // is a child of an old menu bar being deleted (note: it does
326 // not get destroyed by the wxMenu destructor, but
327 // by DestroyChildren()), m_popupMenu should be reset to NULL.
329 m_menu
->m_popupMenu
= NULL
;
332 // ----------------------------------------------------------------------------
333 // wxPopupMenuWindow current item/node handling
334 // ----------------------------------------------------------------------------
336 void wxPopupMenuWindow::ResetCurrent()
339 SetCurrent(wxMenuItemList::compatibility_iterator());
341 SetCurrent((wxwxMenuItemListNode
*)NULL
);
345 void wxPopupMenuWindow::SetCurrent(wxMenuItemList::compatibility_iterator node
)
347 m_nodeCurrent
= node
;
350 void wxPopupMenuWindow::ChangeCurrent(wxMenuItemList::compatibility_iterator node
)
352 if ( node
!= m_nodeCurrent
)
354 wxMenuItemList::compatibility_iterator nodeOldCurrent
= m_nodeCurrent
;
356 m_nodeCurrent
= node
;
358 if ( nodeOldCurrent
)
360 wxMenuItem
*item
= nodeOldCurrent
->GetData();
361 wxCHECK_RET( item
, _T("no current item?") );
363 // if it was the currently opened menu, close it
364 if ( item
->IsSubMenu() && item
->GetSubMenu()->IsShown() )
366 item
->GetSubMenu()->Dismiss();
367 OnSubmenuDismiss( false );
374 RefreshItem(m_nodeCurrent
->GetData());
378 wxMenuItemList::compatibility_iterator
wxPopupMenuWindow::GetPrevNode() const
380 // return the last node if there had been no previously selected one
381 return m_nodeCurrent
? GetPrevNode(m_nodeCurrent
)
382 : m_menu
->GetMenuItems().GetLast();
385 wxMenuItemList::compatibility_iterator
386 wxPopupMenuWindow::GetPrevNode(wxMenuItemList::compatibility_iterator node
) const
390 node
= node
->GetPrevious();
393 node
= m_menu
->GetMenuItems().GetLast();
396 //else: the menu is empty
401 wxMenuItemList::compatibility_iterator
wxPopupMenuWindow::GetNextNode() const
403 // return the first node if there had been no previously selected one
404 return m_nodeCurrent
? GetNextNode(m_nodeCurrent
)
405 : m_menu
->GetMenuItems().GetFirst();
408 wxMenuItemList::compatibility_iterator
409 wxPopupMenuWindow::GetNextNode(wxMenuItemList::compatibility_iterator node
) const
413 node
= node
->GetNext();
416 node
= m_menu
->GetMenuItems().GetFirst();
419 //else: the menu is empty
424 // ----------------------------------------------------------------------------
425 // wxPopupMenuWindow popup/dismiss
426 // ----------------------------------------------------------------------------
428 void wxPopupMenuWindow::Popup(wxWindow
*focus
)
430 // check that the current item had been properly reset before
431 wxASSERT_MSG( !m_nodeCurrent
||
432 m_nodeCurrent
== m_menu
->GetMenuItems().GetFirst(),
433 _T("menu current item preselected incorrectly") );
435 wxPopupTransientWindow::Popup(focus
);
437 // the base class no-longer captures the mouse automatically when Popup
438 // is called, so do it here to allow the menu tracking to work
443 // ensure that this window is really on top of everything: without using
444 // SetWindowPos() it can be covered by its parent menu which is not
445 // really what we want
446 wxMenu
*menuParent
= m_menu
->GetParent();
449 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
451 // if we're shown, the parent menu must be also shown
452 wxCHECK_RET( win
, _T("parent menu is not shown?") );
454 if ( !::SetWindowPos(GetHwndOf(win
), GetHwnd(),
456 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOREDRAW
) )
458 wxLogLastError(_T("SetWindowPos(HWND_TOP)"));
466 void wxPopupMenuWindow::Dismiss()
468 if ( HasOpenSubmenu() )
470 wxMenuItem
*item
= GetCurrentItem();
471 wxCHECK_RET( item
&& item
->IsSubMenu(), _T("where is our open submenu?") );
473 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
474 wxCHECK_RET( win
, _T("opened submenu is not opened?") );
477 OnSubmenuDismiss( false );
480 wxPopupTransientWindow::Dismiss();
485 void wxPopupMenuWindow::OnDismiss()
487 // when we are dismissed because the user clicked elsewhere or we lost
488 // focus in any other way, hide the parent menu as well
492 void wxPopupMenuWindow::OnSubmenuDismiss(bool WXUNUSED(dismissParent
))
494 m_hasOpenSubMenu
= false;
497 void wxPopupMenuWindow::HandleDismiss(bool dismissParent
)
499 m_menu
->OnDismiss(dismissParent
);
502 void wxPopupMenuWindow::DismissAndNotify()
508 // ----------------------------------------------------------------------------
509 // wxPopupMenuWindow geometry
510 // ----------------------------------------------------------------------------
512 wxMenuItemList::compatibility_iterator
513 wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint
& pt
) const
515 // we only use the y coord normally, but still check x in case the point is
516 // outside the window completely
517 if ( wxWindow::HitTest(pt
) == wxHT_WINDOW_INSIDE
)
520 for ( wxMenuItemList::compatibility_iterator node
= m_menu
->GetMenuItems().GetFirst();
522 node
= node
->GetNext() )
524 wxMenuItem
*item
= node
->GetData();
525 y
+= item
->GetHeight();
535 return wxMenuItemList::compatibility_iterator();
541 // ----------------------------------------------------------------------------
542 // wxPopupMenuWindow drawing
543 // ----------------------------------------------------------------------------
545 void wxPopupMenuWindow::RefreshItem(wxMenuItem
*item
)
547 wxCHECK_RET( item
, _T("can't refresh NULL item") );
549 wxASSERT_MSG( IsShown(), _T("can't refresh menu which is not shown") );
551 // FIXME: -1 here because of SetLogicalOrigin(1, 1) in DoDraw()
552 RefreshRect(wxRect(0, item
->GetPosition() - 1,
553 m_menu
->GetGeometryInfo().GetSize().x
, item
->GetHeight()));
556 void wxPopupMenuWindow::DoDraw(wxControlRenderer
*renderer
)
558 // no clipping so far - do we need it? I don't think so as the menu is
559 // never partially covered as it is always on top of everything
561 wxDC
& dc
= renderer
->GetDC();
562 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
564 // FIXME: this should be done in the renderer, however when it is fixed
565 // wxPopupMenuWindow::RefreshItem() should be changed too!
566 dc
.SetLogicalOrigin(1, 1);
568 wxRenderer
*rend
= renderer
->GetRenderer();
571 const wxMenuGeometryInfo
& gi
= m_menu
->GetGeometryInfo();
572 for ( wxMenuItemList::compatibility_iterator node
= m_menu
->GetMenuItems().GetFirst();
574 node
= node
->GetNext() )
576 wxMenuItem
*item
= node
->GetData();
578 if ( item
->IsSeparator() )
580 rend
->DrawMenuSeparator(dc
, y
, gi
);
582 else // not a separator
585 if ( item
->IsCheckable() )
587 flags
|= wxCONTROL_CHECKABLE
;
589 if ( item
->IsChecked() )
591 flags
|= wxCONTROL_CHECKED
;
595 if ( !item
->IsEnabled() )
596 flags
|= wxCONTROL_DISABLED
;
598 if ( item
->IsSubMenu() )
599 flags
|= wxCONTROL_ISSUBMENU
;
601 if ( item
== GetCurrentItem() )
602 flags
|= wxCONTROL_SELECTED
;
606 if ( !item
->IsEnabled() )
608 bmp
= item
->GetDisabledBitmap();
613 // strangely enough, for unchecked item we use the
614 // "checked" bitmap because this is the default one - this
615 // explains this strange boolean expression
616 bmp
= item
->GetBitmap(!item
->IsCheckable() || item
->IsChecked());
625 item
->GetAccelString(),
628 item
->GetAccelIndex()
632 y
+= item
->GetHeight();
636 // ----------------------------------------------------------------------------
637 // wxPopupMenuWindow actions
638 // ----------------------------------------------------------------------------
640 void wxPopupMenuWindow::ClickItem(wxMenuItem
*item
)
642 wxCHECK_RET( item
, _T("can't click NULL item") );
644 wxASSERT_MSG( !item
->IsSeparator() && !item
->IsSubMenu(),
645 _T("can't click this item") );
647 wxMenu
* menu
= m_menu
;
652 menu
->ClickItem(item
);
655 void wxPopupMenuWindow::OpenSubmenu(wxMenuItem
*item
, InputMethod how
)
657 wxCHECK_RET( item
, _T("can't open NULL submenu") );
659 wxMenu
*submenu
= item
->GetSubMenu();
660 wxCHECK_RET( submenu
, _T("can only open submenus!") );
662 // FIXME: should take into account the border width
663 submenu
->Popup(ClientToScreen(wxPoint(0, item
->GetPosition())),
664 wxSize(m_menu
->GetGeometryInfo().GetSize().x
, 0),
665 how
== WithKeyboard
/* preselect first item then */);
667 m_hasOpenSubMenu
= true;
670 bool wxPopupMenuWindow::ActivateItem(wxMenuItem
*item
, InputMethod how
)
672 // don't activate disabled items
673 if ( !item
|| !item
->IsEnabled() )
678 // normal menu items generate commands, submenus can be opened and
679 // the separators don't do anything
680 if ( item
->IsSubMenu() )
682 OpenSubmenu(item
, how
);
684 else if ( !item
->IsSeparator() )
688 else // separator, can't activate
696 // ----------------------------------------------------------------------------
697 // wxPopupMenuWindow input handling
698 // ----------------------------------------------------------------------------
700 bool wxPopupMenuWindow::ProcessLeftDown(wxMouseEvent
& event
)
702 // wxPopupWindowHandler dismisses the window when the mouse is clicked
703 // outside it which is usually just fine, but there is one case when we
704 // don't want to do it: if the mouse was clicked on the parent submenu item
705 // which opens this menu, so check for it
707 wxPoint pos
= event
.GetPosition();
708 if ( HitTest(pos
.x
, pos
.y
) == wxHT_WINDOW_OUTSIDE
)
710 wxMenu
*menu
= m_menu
->GetParent();
713 wxPopupMenuWindow
*win
= menu
->m_popupMenu
;
715 wxCHECK_MSG( win
, false, _T("parent menu not shown?") );
717 pos
= ClientToScreen(pos
);
718 if ( win
->GetMenuItemFromPoint(win
->ScreenToClient(pos
)) )
723 //else: it is outside the parent menu as well, do dismiss this one
730 void wxPopupMenuWindow::OnLeftUp(wxMouseEvent
& event
)
732 wxMenuItemList::compatibility_iterator node
= GetMenuItemFromPoint(event
.GetPosition());
735 ActivateItem(node
->GetData(), WithMouse
);
739 void wxPopupMenuWindow::OnMouseMove(wxMouseEvent
& event
)
741 const wxPoint pt
= event
.GetPosition();
743 // we need to ignore extra mouse events: example when this happens is when
744 // the mouse is on the menu and we open a submenu from keyboard - Windows
745 // then sends us a dummy mouse move event, we (correctly) determine that it
746 // happens in the parent menu and so immediately close the just opened
749 static wxPoint s_ptLast
;
750 wxPoint ptCur
= ClientToScreen(pt
);
751 if ( ptCur
== s_ptLast
)
759 ProcessMouseMove(pt
);
764 void wxPopupMenuWindow::ProcessMouseMove(const wxPoint
& pt
)
766 wxMenuItemList::compatibility_iterator node
= GetMenuItemFromPoint(pt
);
768 // don't reset current to NULL here, we only do it when the mouse leaves
769 // the window (see below)
772 if ( node
!= m_nodeCurrent
)
776 wxMenuItem
*item
= GetCurrentItem();
779 OpenSubmenu(item
, WithMouse
);
782 //else: same item, nothing to do
784 else // not on an item
786 // the last open submenu forwards the mouse move messages to its
787 // parent, so if the mouse moves to another item of the parent menu,
788 // this menu is closed and this other item is selected - in the similar
789 // manner, the top menu forwards the mouse moves to the menubar which
790 // allows to select another top level menu by just moving the mouse
792 // we need to translate our client coords to the client coords of the
793 // window we forward this event to
794 wxPoint ptScreen
= ClientToScreen(pt
);
796 // if the mouse is outside this menu, let the parent one to
798 wxMenu
*menuParent
= m_menu
->GetParent();
801 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
803 // if we're shown, the parent menu must be also shown
804 wxCHECK_RET( win
, _T("parent menu is not shown?") );
806 win
->ProcessMouseMove(win
->ScreenToClient(ptScreen
));
808 else // no parent menu
810 wxMenuBar
*menubar
= m_menu
->GetMenuBar();
813 if ( menubar
->ProcessMouseEvent(
814 menubar
->ScreenToClient(ptScreen
)) )
816 // menubar has closed this menu and opened another one, probably
821 //else: top level popup menu, no other processing to do
825 void wxPopupMenuWindow::OnMouseLeave(wxMouseEvent
& event
)
827 // due to the artefact of mouse events generation under MSW, we actually
828 // may get the mouse leave event after the menu had been already dismissed
829 // and calling ChangeCurrent() would then assert, so don't do it
832 // we shouldn't change the current them if our submenu is opened and
833 // mouse moved there, in this case the submenu is responsable for
836 if ( HasOpenSubmenu() )
838 wxMenuItem
*item
= GetCurrentItem();
839 wxCHECK_RET( CanOpen(item
), _T("where is our open submenu?") );
841 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
842 wxCHECK_RET( win
, _T("submenu is opened but not shown?") );
844 // only handle this event if the mouse is not inside the submenu
845 wxPoint pt
= ClientToScreen(event
.GetPosition());
847 win
->HitTest(win
->ScreenToClient(pt
)) == wxHT_WINDOW_OUTSIDE
;
851 // this menu is the last opened
858 ChangeCurrent(wxMenuItemList::compatibility_iterator());
868 void wxPopupMenuWindow::OnKeyDown(wxKeyEvent
& event
)
870 wxMenuBar
*menubar
= m_menu
->GetMenuBar();
874 menubar
->ProcessEvent(event
);
876 else if ( !ProcessKeyDown(event
.GetKeyCode()) )
882 bool wxPopupMenuWindow::ProcessKeyDown(int key
)
884 wxMenuItem
*item
= GetCurrentItem();
886 // first let the opened submenu to have it (no test for IsEnabled() here,
887 // the keys navigate even in a disabled submenu if we had somehow managed
888 // to open it inspit of this)
889 if ( HasOpenSubmenu() )
891 wxCHECK_MSG( CanOpen(item
), false,
892 _T("has open submenu but another item selected?") );
894 if ( item
->GetSubMenu()->ProcessKeyDown(key
) )
898 bool processed
= true;
900 // handle the up/down arrows, home, end, esc and return here, pass the
901 // left/right arrows to the menu bar except when the right arrow can be
902 // used to open a submenu
906 // if we're not a top level menu, close us, else leave this to the
908 if ( !m_menu
->GetParent() )
917 // close just this menu
919 HandleDismiss(false);
923 processed
= ActivateItem(item
);
927 ChangeCurrent(m_menu
->GetMenuItems().GetFirst());
931 ChangeCurrent(m_menu
->GetMenuItems().GetLast());
937 bool up
= key
== WXK_UP
;
939 wxMenuItemList::compatibility_iterator nodeStart
= up
? GetPrevNode()
942 while ( node
&& node
->GetData()->IsSeparator() )
944 node
= up
? GetPrevNode(node
) : GetNextNode(node
);
946 if ( node
== nodeStart
)
948 // nothing but separators and disabled items in this
951 node
= wxMenuItemList::compatibility_iterator();
970 // don't try to reopen an already opened menu
971 if ( !HasOpenSubmenu() && CanOpen(item
) )
982 // look for the menu item starting with this letter
983 if ( wxIsalnum((wxChar
)key
) )
985 // we want to start from the item after this one because
986 // if we're already on the item with the given accel we want to
987 // go to the next one, not to stay in place
988 wxMenuItemList::compatibility_iterator nodeStart
= GetNextNode();
990 // do we have more than one item with this accel?
991 bool notUnique
= false;
993 // translate everything to lower case before comparing
994 wxChar chAccel
= (wxChar
)wxTolower(key
);
996 // loop through all items searching for the item with this
998 wxMenuItemList::compatibility_iterator node
= nodeStart
,
1000 nodeFound
= wxMenuItemList::compatibility_iterator();
1006 item
= node
->GetData();
1008 int idxAccel
= item
->GetAccelIndex();
1009 if ( idxAccel
!= -1 &&
1010 wxTolower(item
->GetLabel()[(size_t)idxAccel
])
1013 // ok, found an item with this accel
1016 // store it but continue searching as we need to
1017 // know if it's the only item with this accel or if
1021 else // we already had found such item
1025 // no need to continue further, we won't find
1026 // anything we don't already know
1031 // we want to iterate over all items wrapping around if
1033 node
= GetNextNode(node
);
1034 if ( node
== nodeStart
)
1036 // we've seen all nodes
1043 item
= nodeFound
->GetData();
1045 // go to this item anyhow
1046 ChangeCurrent(nodeFound
);
1048 if ( !notUnique
&& item
->IsEnabled() )
1050 // unique item with this accel - activate it
1051 processed
= ActivateItem(item
);
1053 //else: just select it but don't activate as the user might
1054 // have wanted to activate another item
1056 // skip "processed = false" below
1067 // ----------------------------------------------------------------------------
1069 // ----------------------------------------------------------------------------
1077 m_startRadioGroup
= -1;
1086 // ----------------------------------------------------------------------------
1087 // wxMenu and wxMenuGeometryInfo
1088 // ----------------------------------------------------------------------------
1090 wxMenuGeometryInfo::~wxMenuGeometryInfo()
1094 const wxMenuGeometryInfo
& wxMenu::GetGeometryInfo() const
1100 wxConstCast(this, wxMenu
)->m_geometry
=
1101 m_popupMenu
->GetRenderer()->GetMenuGeometry(m_popupMenu
, *this);
1105 wxFAIL_MSG( _T("can't get geometry without window") );
1112 void wxMenu::InvalidateGeometryInfo()
1121 // ----------------------------------------------------------------------------
1122 // wxMenu adding/removing items
1123 // ----------------------------------------------------------------------------
1125 void wxMenu::OnItemAdded(wxMenuItem
*item
)
1127 InvalidateGeometryInfo();
1131 #endif // wxUSE_ACCEL
1133 // the submenus of a popup menu should have the same invoking window as it
1135 if ( m_invokingWindow
&& item
->IsSubMenu() )
1137 item
->GetSubMenu()->SetInvokingWindow(m_invokingWindow
);
1141 void wxMenu::EndRadioGroup()
1143 // we're not inside a radio group any longer
1144 m_startRadioGroup
= -1;
1147 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
1149 if ( item
->GetKind() == wxITEM_RADIO
)
1151 int count
= GetMenuItemCount();
1153 if ( m_startRadioGroup
== -1 )
1155 // start a new radio group
1156 m_startRadioGroup
= count
;
1158 // for now it has just one element
1159 item
->SetAsRadioGroupStart();
1160 item
->SetRadioGroupEnd(m_startRadioGroup
);
1162 else // extend the current radio group
1164 // we need to update its end item
1165 item
->SetRadioGroupStart(m_startRadioGroup
);
1166 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
1170 node
->GetData()->SetRadioGroupEnd(count
);
1174 wxFAIL_MSG( _T("where is the radio group start item?") );
1178 else // not a radio item
1183 if ( !wxMenuBase::DoAppend(item
) )
1191 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1193 if ( !wxMenuBase::DoInsert(pos
, item
) )
1201 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1203 wxMenuItem
*itemOld
= wxMenuBase::DoRemove(item
);
1207 InvalidateGeometryInfo();
1210 RemoveAccelFor(item
);
1211 #endif // wxUSE_ACCEL
1217 // ----------------------------------------------------------------------------
1218 // wxMenu attaching/detaching
1219 // ----------------------------------------------------------------------------
1221 void wxMenu::Attach(wxMenuBarBase
*menubar
)
1223 wxMenuBase::Attach(menubar
);
1225 wxCHECK_RET( m_menuBar
, _T("menubar can't be NULL after attaching") );
1227 // unfortunately, we can't use m_menuBar->GetEventHandler() here because,
1228 // if the menubar is currently showing a menu, its event handler is a
1229 // temporary one installed by wxPopupWindow and so will disappear soon any
1230 // any attempts to use it from the newly attached menu would result in a
1233 // so we use the menubar itself, even if it's a pity as it means we can't
1234 // redirect all menu events by changing the menubar handler (FIXME)
1235 SetNextHandler(m_menuBar
);
1238 void wxMenu::Detach()
1240 wxMenuBase::Detach();
1243 // ----------------------------------------------------------------------------
1244 // wxMenu misc functions
1245 // ----------------------------------------------------------------------------
1247 wxWindow
*wxMenu::GetRootWindow() const
1251 // simple case - a normal menu attached to the menubar
1252 return GetMenuBar();
1255 // we're a popup menu but the trouble is that only the top level popup menu
1256 // has a pointer to the invoking window, so we must walk up the menu chain
1258 wxWindow
*win
= GetInvokingWindow();
1261 // we already have it
1265 wxMenu
*menu
= GetParent();
1268 // We are a submenu of a menu of a menubar
1269 if (menu
->GetMenuBar())
1270 return menu
->GetMenuBar();
1272 win
= menu
->GetInvokingWindow();
1276 menu
= menu
->GetParent();
1279 // we're probably going to crash in the caller anyhow, but try to detect
1280 // this error as soon as possible
1281 wxASSERT_MSG( win
, _T("menu without any associated window?") );
1283 // also remember it in this menu so that we don't have to search for it the
1285 wxConstCast(this, wxMenu
)->m_invokingWindow
= win
;
1290 wxRenderer
*wxMenu::GetRenderer() const
1292 // we're going to crash without renderer!
1293 wxCHECK_MSG( m_popupMenu
, NULL
, _T("neither popup nor menubar menu?") );
1295 return m_popupMenu
->GetRenderer();
1298 void wxMenu::RefreshItem(wxMenuItem
*item
)
1300 // the item geometry changed, so our might have changed as well
1301 InvalidateGeometryInfo();
1305 // this would be a bug in IsShown()
1306 wxCHECK_RET( m_popupMenu
, _T("must have popup window if shown!") );
1308 // recalc geometry to update the item height and such
1309 (void)GetGeometryInfo();
1311 m_popupMenu
->RefreshItem(item
);
1315 // ----------------------------------------------------------------------------
1316 // wxMenu showing and hiding
1317 // ----------------------------------------------------------------------------
1319 bool wxMenu::IsShown() const
1321 return m_popupMenu
&& m_popupMenu
->IsShown();
1324 void wxMenu::OnDismiss(bool dismissParent
)
1328 // always notify the parent about submenu disappearance
1329 wxPopupMenuWindow
*win
= m_menuParent
->m_popupMenu
;
1332 win
->OnSubmenuDismiss( true );
1336 wxFAIL_MSG( _T("parent menu not shown?") );
1339 // and if we dismiss everything, propagate to parent
1340 if ( dismissParent
)
1342 // dismissParent is recursive
1343 m_menuParent
->Dismiss();
1344 m_menuParent
->OnDismiss(true);
1347 else // no parent menu
1349 // notify the menu bar if we're a top level menu
1352 m_menuBar
->OnDismissMenu(dismissParent
);
1356 wxCHECK_RET( m_invokingWindow
, _T("what kind of menu is this?") );
1358 m_invokingWindow
->DismissPopupMenu();
1360 // Why reset it here? We need it for sending the event to...
1361 // SetInvokingWindow(NULL);
1366 void wxMenu::Popup(const wxPoint
& pos
, const wxSize
& size
, bool selectFirst
)
1368 // create the popup window if not done yet
1371 m_popupMenu
= new wxPopupMenuWindow(GetRootWindow(), this);
1374 // select the first item unless disabled
1377 m_popupMenu
->SelectFirst();
1380 // the geometry might have changed since the last time we were shown, so
1382 m_popupMenu
->SetClientSize(GetGeometryInfo().GetSize());
1384 // position it as specified
1385 m_popupMenu
->Position(pos
, size
);
1387 // the menu can't have the focus itself (it is a Windows limitation), so
1388 // always keep the focus at the originating window
1389 wxWindow
*focus
= GetRootWindow();
1391 wxASSERT_MSG( focus
, _T("no window to keep focus on?") );
1394 m_popupMenu
->Popup(focus
);
1397 void wxMenu::Dismiss()
1399 wxCHECK_RET( IsShown(), _T("can't dismiss hidden menu") );
1401 m_popupMenu
->Dismiss();
1404 // ----------------------------------------------------------------------------
1405 // wxMenu event processing
1406 // ----------------------------------------------------------------------------
1408 bool wxMenu::ProcessKeyDown(int key
)
1410 wxCHECK_MSG( m_popupMenu
, false,
1411 _T("can't process key events if not shown") );
1413 return m_popupMenu
->ProcessKeyDown(key
);
1416 bool wxMenu::ClickItem(wxMenuItem
*item
)
1419 if ( item
->IsCheckable() )
1421 // update the item state
1422 isChecked
= !item
->IsChecked();
1424 item
->Check(isChecked
!= 0);
1432 return SendEvent(item
->GetId(), isChecked
);
1435 // ----------------------------------------------------------------------------
1436 // wxMenu accel support
1437 // ----------------------------------------------------------------------------
1441 bool wxMenu::ProcessAccelEvent(const wxKeyEvent
& event
)
1443 // do we have an item for this accel?
1444 wxMenuItem
*item
= m_accelTable
.GetMenuItem(event
);
1445 if ( item
&& item
->IsEnabled() )
1447 return ClickItem(item
);
1451 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
1453 node
= node
->GetNext() )
1455 const wxMenuItem
*item
= node
->GetData();
1456 if ( item
->IsSubMenu() && item
->IsEnabled() )
1459 if ( item
->GetSubMenu()->ProcessAccelEvent(event
) )
1469 void wxMenu::AddAccelFor(wxMenuItem
*item
)
1471 wxAcceleratorEntry
*accel
= item
->GetAccel();
1474 accel
->SetMenuItem(item
);
1476 m_accelTable
.Add(*accel
);
1482 void wxMenu::RemoveAccelFor(wxMenuItem
*item
)
1484 wxAcceleratorEntry
*accel
= item
->GetAccel();
1487 m_accelTable
.Remove(*accel
);
1493 #endif // wxUSE_ACCEL
1495 // ----------------------------------------------------------------------------
1496 // wxMenuItem construction
1497 // ----------------------------------------------------------------------------
1499 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
1501 const wxString
& text
,
1502 const wxString
& help
,
1505 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
1508 m_height
= wxDefaultCoord
;
1510 m_radioGroup
.start
= -1;
1511 m_isRadioGroupStart
= false;
1513 m_bmpDisabled
= wxNullBitmap
;
1518 wxMenuItem::~wxMenuItem()
1522 // ----------------------------------------------------------------------------
1523 // wxMenuItemBase methods implemented here
1524 // ----------------------------------------------------------------------------
1527 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
1529 const wxString
& name
,
1530 const wxString
& help
,
1534 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
1538 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
1540 return wxStripMenuCodes(text
);
1543 // ----------------------------------------------------------------------------
1544 // wxMenuItem operations
1545 // ----------------------------------------------------------------------------
1547 void wxMenuItem::NotifyMenu()
1549 m_parentMenu
->RefreshItem(this);
1552 void wxMenuItem::UpdateAccelInfo()
1554 m_indexAccel
= wxControl::FindAccelIndex(m_text
);
1556 // will be empty if the text contains no TABs - ok
1557 m_strAccel
= m_text
.AfterFirst(_T('\t'));
1560 void wxMenuItem::SetText(const wxString
& text
)
1562 if ( text
!= m_text
)
1564 // first call the base class version to change m_text
1565 wxMenuItemBase::SetText(text
);
1573 void wxMenuItem::SetCheckable(bool checkable
)
1575 if ( checkable
!= IsCheckable() )
1577 wxMenuItemBase::SetCheckable(checkable
);
1583 void wxMenuItem::SetBitmaps(const wxBitmap
& bmpChecked
,
1584 const wxBitmap
& bmpUnchecked
)
1586 m_bmpChecked
= bmpChecked
;
1587 m_bmpUnchecked
= bmpUnchecked
;
1592 void wxMenuItem::Enable(bool enable
)
1594 if ( enable
!= m_isEnabled
)
1596 wxMenuItemBase::Enable(enable
);
1602 void wxMenuItem::Check(bool check
)
1604 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
1606 if ( m_isChecked
== check
)
1609 if ( GetKind() == wxITEM_RADIO
)
1611 // it doesn't make sense to uncheck a radio item - what would this do?
1615 // get the index of this item in the menu
1616 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
1617 int pos
= items
.IndexOf(this);
1618 wxCHECK_RET( pos
!= wxNOT_FOUND
,
1619 _T("menuitem not found in the menu items list?") );
1621 // get the radio group range
1625 if ( m_isRadioGroupStart
)
1627 // we already have all information we need
1629 end
= m_radioGroup
.end
;
1631 else // next radio group item
1633 // get the radio group end from the start item
1634 start
= m_radioGroup
.start
;
1635 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
1638 // also uncheck all the other items in this radio group
1639 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
1640 for ( int n
= start
; n
<= end
&& node
; n
++ )
1644 node
->GetData()->m_isChecked
= false;
1646 node
= node
->GetNext();
1650 wxMenuItemBase::Check(check
);
1655 // radio group stuff
1656 // -----------------
1658 void wxMenuItem::SetAsRadioGroupStart()
1660 m_isRadioGroupStart
= true;
1663 void wxMenuItem::SetRadioGroupStart(int start
)
1665 wxASSERT_MSG( !m_isRadioGroupStart
,
1666 _T("should only be called for the next radio items") );
1668 m_radioGroup
.start
= start
;
1671 void wxMenuItem::SetRadioGroupEnd(int end
)
1673 wxASSERT_MSG( m_isRadioGroupStart
,
1674 _T("should only be called for the first radio item") );
1676 m_radioGroup
.end
= end
;
1679 // ----------------------------------------------------------------------------
1680 // wxMenuBar creation
1681 // ----------------------------------------------------------------------------
1683 void wxMenuBar::Init()
1691 m_shouldShowMenu
= false;
1694 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
1698 for (size_t i
= 0; i
< n
; ++i
)
1699 Append(menus
[i
], titles
[i
]);
1702 void wxMenuBar::Attach(wxFrame
*frame
)
1704 // maybe you really wanted to call Detach()?
1705 wxCHECK_RET( frame
, _T("wxMenuBar::Attach(NULL) called") );
1707 wxMenuBarBase::Attach(frame
);
1711 // reparent if necessary
1712 if ( m_frameLast
!= frame
)
1717 // show it back - was hidden by Detach()
1720 else // not created yet, do it now
1722 // we have no way to return the error from here anyhow :-(
1723 (void)Create(frame
, wxID_ANY
);
1725 SetCursor(wxCURSOR_ARROW
);
1727 SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT
));
1729 // calculate and set our height (it won't be changed any more)
1730 SetSize(wxDefaultCoord
, GetBestSize().y
);
1733 // remember the last frame which had us to avoid unnecessarily reparenting
1735 m_frameLast
= frame
;
1738 void wxMenuBar::Detach()
1740 // don't delete the window because we may be reattached later, just hide it
1746 wxMenuBarBase::Detach();
1749 wxMenuBar::~wxMenuBar()
1753 // ----------------------------------------------------------------------------
1754 // wxMenuBar adding/removing items
1755 // ----------------------------------------------------------------------------
1757 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1759 return Insert(GetCount(), menu
, title
);
1762 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1764 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1767 wxMenuInfo
*info
= new wxMenuInfo(title
);
1768 m_menuInfos
.Insert(info
, pos
);
1770 RefreshAllItemsAfter(pos
);
1775 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1777 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1781 wxMenuInfo
& info
= m_menuInfos
[pos
];
1783 info
.SetLabel(title
);
1785 // even if the old menu was disabled, the new one is not any more
1788 // even if we change only this one, the new label has different width,
1789 // so we need to refresh everything beyond this item as well
1790 RefreshAllItemsAfter(pos
);
1796 wxMenu
*wxMenuBar::Remove(size_t pos
)
1798 wxMenu
*menuOld
= wxMenuBarBase::Remove(pos
);
1802 m_menuInfos
.RemoveAt(pos
);
1804 // this doesn't happen too often, so don't try to be too smart - just
1805 // refresh everything
1812 // ----------------------------------------------------------------------------
1813 // wxMenuBar top level menus access
1814 // ----------------------------------------------------------------------------
1816 wxCoord
wxMenuBar::GetItemWidth(size_t pos
) const
1818 return m_menuInfos
[pos
].GetWidth(wxConstCast(this, wxMenuBar
));
1821 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1823 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1825 if ( enable
!= m_menuInfos
[pos
].IsEnabled() )
1827 m_menuInfos
[pos
].SetEnabled(enable
);
1831 //else: nothing to do
1834 bool wxMenuBar::IsEnabledTop(size_t pos
) const
1836 wxCHECK_MSG( pos
< GetCount(), false, _T("invalid index in IsEnabledTop") );
1838 return m_menuInfos
[pos
].IsEnabled();
1841 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
1843 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1845 if ( label
!= m_menuInfos
[pos
].GetLabel() )
1847 m_menuInfos
[pos
].SetLabel(label
);
1851 //else: nothing to do
1854 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
1856 wxCHECK_MSG( pos
< GetCount(), wxEmptyString
, _T("invalid index in GetLabelTop") );
1858 return m_menuInfos
[pos
].GetLabel();
1861 // ----------------------------------------------------------------------------
1862 // wxMenuBar drawing
1863 // ----------------------------------------------------------------------------
1865 void wxMenuBar::RefreshAllItemsAfter(size_t pos
)
1869 // no need to refresh if nothing is shown yet
1873 wxRect rect
= GetItemRect(pos
);
1874 rect
.width
= GetClientSize().x
- rect
.x
;
1878 void wxMenuBar::RefreshItem(size_t pos
)
1880 wxCHECK_RET( pos
!= (size_t)-1,
1881 _T("invalid item in wxMenuBar::RefreshItem") );
1885 // no need to refresh if nothing is shown yet
1889 RefreshRect(GetItemRect(pos
));
1892 void wxMenuBar::DoDraw(wxControlRenderer
*renderer
)
1894 wxDC
& dc
= renderer
->GetDC();
1895 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1897 // redraw only the items which must be redrawn
1899 // we don't have to use GetUpdateClientRect() here because our client rect
1900 // is the same as total one
1901 wxRect rectUpdate
= GetUpdateRegion().GetBox();
1903 int flagsMenubar
= GetStateFlags();
1907 rect
.height
= GetClientSize().y
;
1910 size_t count
= GetCount();
1911 for ( size_t n
= 0; n
< count
; n
++ )
1913 if ( x
> rectUpdate
.GetRight() )
1915 // all remaining items are to the right of rectUpdate
1920 rect
.width
= GetItemWidth(n
);
1922 if ( x
< rectUpdate
.x
)
1924 // this item is still to the left of rectUpdate
1928 int flags
= flagsMenubar
;
1929 if ( m_current
!= -1 && n
== (size_t)m_current
)
1931 flags
|= wxCONTROL_SELECTED
;
1934 if ( !IsEnabledTop(n
) )
1936 flags
|= wxCONTROL_DISABLED
;
1939 GetRenderer()->DrawMenuBarItem
1943 m_menuInfos
[n
].GetLabel(),
1945 m_menuInfos
[n
].GetAccelIndex()
1950 // ----------------------------------------------------------------------------
1951 // wxMenuBar geometry
1952 // ----------------------------------------------------------------------------
1954 wxRect
wxMenuBar::GetItemRect(size_t pos
) const
1956 wxASSERT_MSG( pos
< GetCount(), _T("invalid menu bar item index") );
1957 wxASSERT_MSG( IsCreated(), _T("can't call this method yet") );
1962 rect
.height
= GetClientSize().y
;
1964 for ( size_t n
= 0; n
< pos
; n
++ )
1966 rect
.x
+= GetItemWidth(n
);
1969 rect
.width
= GetItemWidth(pos
);
1974 wxSize
wxMenuBar::DoGetBestClientSize() const
1977 if ( GetMenuCount() > 0 )
1979 wxClientDC
dc(wxConstCast(this, wxMenuBar
));
1980 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1981 dc
.GetTextExtent(GetLabelTop(0), &size
.x
, &size
.y
);
1983 // adjust for the renderer we use
1984 size
= GetRenderer()->GetMenuBarItemSize(size
);
1986 else // empty menubar
1992 // the width is arbitrary, of course, for horizontal menubar
1998 int wxMenuBar::GetMenuFromPoint(const wxPoint
& pos
) const
2000 if ( pos
.x
< 0 || pos
.y
< 0 || pos
.y
> GetClientSize().y
)
2005 size_t count
= GetCount();
2006 for ( size_t item
= 0; item
< count
; item
++ )
2008 x
+= GetItemWidth(item
);
2016 // to the right of the last menu item
2020 // ----------------------------------------------------------------------------
2021 // wxMenuBar menu operations
2022 // ----------------------------------------------------------------------------
2024 void wxMenuBar::SelectMenu(size_t pos
)
2027 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::SelectMenu"));
2033 void wxMenuBar::DoSelectMenu(size_t pos
)
2035 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in DoSelectMenu") );
2037 int posOld
= m_current
;
2043 // close the previous menu
2044 if ( IsShowingMenu() )
2046 // restore m_shouldShowMenu flag after DismissMenu() which resets
2048 bool old
= m_shouldShowMenu
;
2052 m_shouldShowMenu
= old
;
2055 RefreshItem((size_t)posOld
);
2061 void wxMenuBar::PopupMenu(size_t pos
)
2063 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in PopupCurrentMenu") );
2070 // ----------------------------------------------------------------------------
2071 // wxMenuBar input handing
2072 // ----------------------------------------------------------------------------
2075 Note that wxMenuBar doesn't use wxInputHandler but handles keyboard and
2076 mouse in the same way under all platforms. This is because it doesn't derive
2077 from wxControl (which works with input handlers) but directly from wxWindow.
2079 Also, menu bar input handling is rather simple, so maybe it's not really
2080 worth making it themeable - at least I've decided against doing it now as it
2081 would merging the changes back into trunk more difficult. But it still could
2082 be done later if really needed.
2085 void wxMenuBar::OnKillFocus(wxFocusEvent
& event
)
2087 if ( m_current
!= -1 )
2089 RefreshItem((size_t)m_current
);
2097 void wxMenuBar::OnLeftDown(wxMouseEvent
& event
)
2105 else // we didn't have mouse capture, capture it now
2107 m_current
= GetMenuFromPoint(event
.GetPosition());
2108 if ( m_current
== -1 )
2110 // unfortunately, we can't prevent wxMSW from giving us the focus,
2111 // so we can only give it back
2116 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::OnLeftDown"));
2119 // show it as selected
2120 RefreshItem((size_t)m_current
);
2123 PopupCurrentMenu(false /* don't select first item - as Windows does */);
2128 void wxMenuBar::OnMouseMove(wxMouseEvent
& event
)
2132 (void)ProcessMouseEvent(event
.GetPosition());
2140 bool wxMenuBar::ProcessMouseEvent(const wxPoint
& pt
)
2142 // a hack to ignore the extra mouse events MSW sends us: this is similar to
2143 // wxUSE_MOUSEEVENT_HACK in wxWin itself but it isn't enough for us here as
2144 // we get the messages from different windows (old and new popup menus for
2147 static wxPoint s_ptLast
;
2148 if ( pt
== s_ptLast
)
2156 int currentNew
= GetMenuFromPoint(pt
);
2157 if ( (currentNew
== -1) || (currentNew
== m_current
) )
2162 // select the new active item
2163 DoSelectMenu(currentNew
);
2165 // show the menu if we know that we should, even if we hadn't been showing
2166 // it before (this may happen if the previous menu was disabled)
2167 if ( m_shouldShowMenu
&& !m_menuShown
)
2169 // open the new menu if the old one we closed had been opened
2170 PopupCurrentMenu(false /* don't select first item - as Windows does */);
2176 void wxMenuBar::OnKeyDown(wxKeyEvent
& event
)
2178 // ensure that we have a current item - we might not have it if we're
2179 // given the focus with Alt or F10 press (and under GTK+ the menubar
2180 // somehow gets the keyboard events even when it doesn't have focus...)
2181 if ( m_current
== -1 )
2183 if ( !HasCapture() )
2187 else // we do have capture
2189 // we always maintain a valid current item while we're in modal
2190 // state (i.e. have the capture)
2191 wxFAIL_MSG( _T("how did we manage to lose current item?") );
2197 int key
= event
.GetKeyCode();
2199 // first let the menu have it
2200 if ( IsShowingMenu() && m_menuShown
->ProcessKeyDown(key
) )
2205 // cycle through the menu items when left/right arrows are pressed and open
2206 // the menu when up/down one is
2210 // Alt must be processed at wxWindow level too
2215 // remove the selection and give the focus away
2216 if ( m_current
!= -1 )
2218 if ( IsShowingMenu() )
2230 size_t count
= GetCount();
2233 // the item won't change anyhow
2236 //else: otherwise, it will
2238 // remember if we were showing a menu - if we did, we should
2239 // show the new menu after changing the item
2240 bool wasMenuOpened
= IsShowingMenu();
2241 if ( wasMenuOpened
)
2246 // cast is safe as we tested for -1 above
2247 size_t currentNew
= (size_t)m_current
;
2249 if ( key
== WXK_LEFT
)
2251 if ( currentNew
-- == 0 )
2252 currentNew
= count
- 1;
2256 if ( ++currentNew
== count
)
2260 DoSelectMenu(currentNew
);
2262 if ( wasMenuOpened
)
2277 // letters open the corresponding menu
2280 int idxFound
= FindNextItemForAccel(m_current
, key
, &unique
);
2282 if ( idxFound
!= -1 )
2284 if ( IsShowingMenu() )
2289 DoSelectMenu((size_t)idxFound
);
2291 // if the item is not unique, just select it but don't
2292 // activate as the user might have wanted to activate
2295 // also, don't try to open a disabled menu
2296 if ( unique
&& IsEnabledTop((size_t)idxFound
) )
2302 // skip the "event.Skip()" below
2311 // ----------------------------------------------------------------------------
2312 // wxMenuBar accel handling
2313 // ----------------------------------------------------------------------------
2315 int wxMenuBar::FindNextItemForAccel(int idxStart
, int key
, bool *unique
) const
2317 if ( !wxIsalnum((wxChar
)key
) )
2319 // we only support letters/digits as accels
2323 // do we have more than one item with this accel?
2327 // translate everything to lower case before comparing
2328 wxChar chAccel
= (wxChar
)wxTolower(key
);
2330 // the index of the item with this accel
2333 // loop through all items searching for the item with this
2334 // accel starting at the item after the current one
2335 int count
= GetCount();
2336 int n
= idxStart
== -1 ? 0 : idxStart
+ 1;
2347 const wxMenuInfo
& info
= m_menuInfos
[n
];
2349 int idxAccel
= info
.GetAccelIndex();
2350 if ( idxAccel
!= -1 &&
2351 wxTolower(info
.GetLabel()[(size_t)idxAccel
])
2354 // ok, found an item with this accel
2355 if ( idxFound
== -1 )
2357 // store it but continue searching as we need to
2358 // know if it's the only item with this accel or if
2362 else // we already had found such item
2367 // no need to continue further, we won't find
2368 // anything we don't already know
2373 // we want to iterate over all items wrapping around if
2381 if ( n
== idxStart
)
2383 // we've seen all items
2393 bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent
& event
)
2396 for ( wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
2398 node
= node
->GetNext(), n
++ )
2400 // accels of the items in the disabled menus shouldn't work
2401 if ( m_menuInfos
[n
].IsEnabled() )
2403 if ( node
->GetData()->ProcessAccelEvent(event
) )
2405 // menu processed it
2415 #endif // wxUSE_ACCEL
2417 // ----------------------------------------------------------------------------
2418 // wxMenuBar menus showing
2419 // ----------------------------------------------------------------------------
2421 void wxMenuBar::PopupCurrentMenu(bool selectFirst
)
2423 wxCHECK_RET( m_current
!= -1, _T("no menu to popup") );
2425 // forgot to call DismissMenu()?
2426 wxASSERT_MSG( !m_menuShown
, _T("shouldn't show two menus at once!") );
2428 // in any case, we should show it - even if we won't
2429 m_shouldShowMenu
= true;
2431 if ( IsEnabledTop(m_current
) )
2433 // remember the menu we show
2434 m_menuShown
= GetMenu(m_current
);
2436 // we don't show the menu at all if it has no items
2437 if ( !m_menuShown
->IsEmpty() )
2439 // position it correctly: note that we must use screen coords and
2440 // that we pass 0 as width to position the menu exactly below the
2441 // item, not to the right of it
2442 wxRect rectItem
= GetItemRect(m_current
);
2444 m_menuShown
->SetInvokingWindow(m_frameLast
);
2446 m_menuShown
->Popup(ClientToScreen(rectItem
.GetPosition()),
2447 wxSize(0, rectItem
.GetHeight()),
2452 // reset it back as no menu is shown
2456 //else: don't show disabled menu
2459 void wxMenuBar::DismissMenu()
2461 wxCHECK_RET( m_menuShown
, _T("can't dismiss menu if none is shown") );
2463 m_menuShown
->Dismiss();
2467 void wxMenuBar::OnDismissMenu(bool dismissMenuBar
)
2469 m_shouldShowMenu
= false;
2471 if ( dismissMenuBar
)
2477 void wxMenuBar::OnDismiss()
2479 if ( ReleaseMouseCapture() )
2480 wxLogTrace(_T("mousecapture"), _T("Releasing mouse from wxMenuBar::OnDismiss"));
2482 if ( m_current
!= -1 )
2484 size_t current
= m_current
;
2487 RefreshItem(current
);
2493 bool wxMenuBar::ReleaseMouseCapture()
2496 // With wxX11, when a menu is closed by clicking away from it, a control
2497 // under the click will still get an event, even though the menu has the
2498 // capture (bug?). So that control may already have taken the capture by
2499 // this point, preventing us from releasing the menu's capture. So to work
2500 // around this, we release both captures, then put back the control's
2502 wxWindow
*capture
= GetCapture();
2505 capture
->ReleaseMouse();
2507 if ( capture
== this )
2510 bool had
= HasCapture();
2515 capture
->CaptureMouse();
2529 void wxMenuBar::GiveAwayFocus()
2531 GetFrame()->SetFocus();
2534 // ----------------------------------------------------------------------------
2535 // popup menu support
2536 // ----------------------------------------------------------------------------
2538 wxEventLoop
*wxWindow::ms_evtLoopPopup
= NULL
;
2540 bool wxWindow::DoPopupMenu(wxMenu
*menu
, int x
, int y
)
2542 wxCHECK_MSG( !ms_evtLoopPopup
, false,
2543 _T("can't show more than one popup menu at a time") );
2546 // we need to change the cursor before showing the menu as, apparently, no
2547 // cursor changes took place while the mouse is captured
2548 wxCursor cursorOld
= GetCursor();
2549 SetCursor(wxCURSOR_ARROW
);
2553 // flash any delayed log messages before showing the menu, otherwise it
2554 // could be dismissed (because it would lose focus) immediately after being
2556 wxLog::FlushActive();
2558 // some controls update themselves from OnIdle() call - let them do it
2559 wxTheApp
->ProcessIdle();
2561 // if the window hadn't been refreshed yet, the menu can adversely affect
2562 // its next OnPaint() handler execution - i.e. scrolled window refresh
2563 // logic breaks then as it scrolls part of the menu which hadn't been there
2564 // when the update event was generated into view
2568 menu
->SetInvokingWindow(this);
2570 // wxLogDebug( "Name of invoking window %s", menu->GetInvokingWindow()->GetName().c_str() );
2572 menu
->Popup(ClientToScreen(wxPoint(x
, y
)), wxSize(0,0));
2574 // this is not very useful if the menu was popped up because of the mouse
2575 // click but I think it is nice to do when it appears because of a key
2576 // press (i.e. Windows menu key)
2578 // Windows itself doesn't do it, but IMHO this is nice
2581 // we have to redirect all keyboard input to the menu temporarily
2582 PushEventHandler(new wxMenuKbdRedirector(menu
));
2584 // enter the local modal loop
2585 ms_evtLoopPopup
= new wxEventLoop
;
2586 ms_evtLoopPopup
->Run();
2588 delete ms_evtLoopPopup
;
2589 ms_evtLoopPopup
= NULL
;
2591 // remove the handler
2592 PopEventHandler(true /* delete it */);
2594 menu
->SetInvokingWindow(NULL
);
2597 SetCursor(cursorOld
);
2603 void wxWindow::DismissPopupMenu()
2605 wxCHECK_RET( ms_evtLoopPopup
, _T("no popup menu shown") );
2607 ms_evtLoopPopup
->Exit();
2610 #endif // wxUSE_MENUS