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 licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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::compatibility_iterator
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::compatibility_iterator node
);
187 virtual bool SetCurrent(bool doit
= true){return wxPopupTransientWindow::SetCurrent(doit
);};
189 // change the current item refreshing the old and new items
190 void ChangeCurrent(wxMenuItemList::compatibility_iterator node
);
192 // activate item, i.e. call either ClickItem() or OpenSubmenu() depending
193 // on what it is, return TRUE if something was done (i.e. it's not a
195 bool ActivateItem(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
197 // send the event about the item click
198 void ClickItem(wxMenuItem
*item
);
200 // show the submenu for this item
201 void OpenSubmenu(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
203 // can this tiem be opened?
204 bool CanOpen(wxMenuItem
*item
)
206 return item
&& item
->IsEnabled() && item
->IsSubMenu();
209 // dismiss the menu and all parent menus too
210 void DismissAndNotify();
212 // react to dimissing this menu and also dismiss the parent if
214 void HandleDismiss(bool dismissParent
);
216 // do we have an open submenu?
217 bool HasOpenSubmenu() const { return m_hasOpenSubMenu
; }
219 // get previous node after the current one
220 wxMenuItemList::compatibility_iterator
GetPrevNode() const;
222 // get previous node before the given one, wrapping if it's the first one
223 wxMenuItemList::compatibility_iterator
GetPrevNode(wxMenuItemList::compatibility_iterator node
) const;
225 // get next node after the current one
226 wxMenuItemList::compatibility_iterator
GetNextNode() const;
228 // get next node after the given one, wrapping if it's the last one
229 wxMenuItemList::compatibility_iterator
GetNextNode(wxMenuItemList::compatibility_iterator node
) const;
235 // the menu node corresponding to the current item
236 wxMenuItemList::compatibility_iterator m_nodeCurrent
;
238 // do we currently have an opened submenu?
239 bool m_hasOpenSubMenu
;
241 DECLARE_EVENT_TABLE()
244 // ----------------------------------------------------------------------------
245 // wxMenuKbdRedirector: an event handler which redirects kbd input to wxMenu
246 // ----------------------------------------------------------------------------
248 class wxMenuKbdRedirector
: public wxEvtHandler
251 wxMenuKbdRedirector(wxMenu
*menu
) { m_menu
= menu
; }
253 virtual bool ProcessEvent(wxEvent
& event
)
255 if ( event
.GetEventType() == wxEVT_KEY_DOWN
)
257 return m_menu
->ProcessKeyDown(((wxKeyEvent
&)event
).GetKeyCode());
263 return wxEvtHandler::ProcessEvent(event
);
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
275 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
276 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
277 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
279 BEGIN_EVENT_TABLE(wxPopupMenuWindow
, wxPopupTransientWindow
)
280 EVT_KEY_DOWN(wxPopupMenuWindow::OnKeyDown
)
282 EVT_LEFT_UP(wxPopupMenuWindow::OnLeftUp
)
283 EVT_MOTION(wxPopupMenuWindow::OnMouseMove
)
284 EVT_LEAVE_WINDOW(wxPopupMenuWindow::OnMouseLeave
)
287 BEGIN_EVENT_TABLE(wxMenuBar
, wxMenuBarBase
)
288 EVT_KILL_FOCUS(wxMenuBar::OnKillFocus
)
290 EVT_KEY_DOWN(wxMenuBar::OnKeyDown
)
292 EVT_LEFT_DOWN(wxMenuBar::OnLeftDown
)
293 EVT_MOTION(wxMenuBar::OnMouseMove
)
296 // ============================================================================
298 // ============================================================================
300 // ----------------------------------------------------------------------------
302 // ----------------------------------------------------------------------------
304 wxPopupMenuWindow::wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
)
307 m_hasOpenSubMenu
= FALSE
;
311 (void)Create(parent
, wxBORDER_RAISED
);
313 SetCursor(wxCURSOR_ARROW
);
316 wxPopupMenuWindow::~wxPopupMenuWindow()
318 // When m_popupMenu in wxMenu is deleted because it
319 // is a child of an old menu bar being deleted (note: it does
320 // not get destroyed by the wxMenu destructor, but
321 // by DestroyChildren()), m_popupMenu should be reset to NULL.
323 m_menu
->m_popupMenu
= NULL
;
326 // ----------------------------------------------------------------------------
327 // wxPopupMenuWindow current item/node handling
328 // ----------------------------------------------------------------------------
330 void wxPopupMenuWindow::ResetCurrent()
333 SetCurrent(wxMenuItemList::compatibility_iterator());
335 SetCurrent((wxwxMenuItemListNode
*)NULL
);
339 void wxPopupMenuWindow::SetCurrent(wxMenuItemList::compatibility_iterator node
)
341 m_nodeCurrent
= node
;
344 void wxPopupMenuWindow::ChangeCurrent(wxMenuItemList::compatibility_iterator node
)
346 if ( node
!= m_nodeCurrent
)
348 wxMenuItemList::compatibility_iterator nodeOldCurrent
= m_nodeCurrent
;
350 m_nodeCurrent
= node
;
352 if ( nodeOldCurrent
)
354 wxMenuItem
*item
= nodeOldCurrent
->GetData();
355 wxCHECK_RET( item
, _T("no current item?") );
357 // if it was the currently opened menu, close it
358 if ( item
->IsSubMenu() && item
->GetSubMenu()->IsShown() )
360 item
->GetSubMenu()->Dismiss();
368 RefreshItem(m_nodeCurrent
->GetData());
372 wxMenuItemList::compatibility_iterator
wxPopupMenuWindow::GetPrevNode() const
374 // return the last node if there had been no previously selected one
375 return m_nodeCurrent
? GetPrevNode(m_nodeCurrent
)
376 : m_menu
->GetMenuItems().GetLast();
379 wxMenuItemList::compatibility_iterator
380 wxPopupMenuWindow::GetPrevNode(wxMenuItemList::compatibility_iterator node
) const
384 node
= node
->GetPrevious();
387 node
= m_menu
->GetMenuItems().GetLast();
390 //else: the menu is empty
395 wxMenuItemList::compatibility_iterator
wxPopupMenuWindow::GetNextNode() const
397 // return the first node if there had been no previously selected one
398 return m_nodeCurrent
? GetNextNode(m_nodeCurrent
)
399 : m_menu
->GetMenuItems().GetFirst();
402 wxMenuItemList::compatibility_iterator
403 wxPopupMenuWindow::GetNextNode(wxMenuItemList::compatibility_iterator node
) const
407 node
= node
->GetNext();
410 node
= m_menu
->GetMenuItems().GetFirst();
413 //else: the menu is empty
418 // ----------------------------------------------------------------------------
419 // wxPopupMenuWindow popup/dismiss
420 // ----------------------------------------------------------------------------
422 void wxPopupMenuWindow::Popup(wxWindow
*focus
)
424 // check that the current item had been properly reset before
425 wxASSERT_MSG( !m_nodeCurrent
||
426 m_nodeCurrent
== m_menu
->GetMenuItems().GetFirst(),
427 _T("menu current item preselected incorrectly") );
429 wxPopupTransientWindow::Popup(focus
);
432 // ensure that this window is really on top of everything: without using
433 // SetWindowPos() it can be covered by its parent menu which is not
434 // really what we want
435 wxMenu
*menuParent
= m_menu
->GetParent();
438 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
440 // if we're shown, the parent menu must be also shown
441 wxCHECK_RET( win
, _T("parent menu is not shown?") );
443 if ( !::SetWindowPos(GetHwndOf(win
), GetHwnd(),
445 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOREDRAW
) )
447 wxLogLastError(_T("SetWindowPos(HWND_TOP)"));
455 void wxPopupMenuWindow::Dismiss()
457 if ( HasOpenSubmenu() )
459 wxMenuItem
*item
= GetCurrentItem();
460 wxCHECK_RET( item
&& item
->IsSubMenu(), _T("where is our open submenu?") );
462 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
463 wxCHECK_RET( win
, _T("opened submenu is not opened?") );
469 wxPopupTransientWindow::Dismiss();
472 void wxPopupMenuWindow::OnDismiss()
474 // when we are dismissed because the user clicked elsewhere or we lost
475 // focus in any other way, hide the parent menu as well
479 void wxPopupMenuWindow::HandleDismiss(bool dismissParent
)
483 m_menu
->OnDismiss(dismissParent
);
486 void wxPopupMenuWindow::DismissAndNotify()
492 // ----------------------------------------------------------------------------
493 // wxPopupMenuWindow geometry
494 // ----------------------------------------------------------------------------
496 wxMenuItemList::compatibility_iterator
497 wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint
& pt
) const
499 // we only use the y coord normally, but still check x in case the point is
500 // outside the window completely
501 if ( wxWindow::HitTest(pt
) == wxHT_WINDOW_INSIDE
)
504 for ( wxMenuItemList::compatibility_iterator node
= m_menu
->GetMenuItems().GetFirst();
506 node
= node
->GetNext() )
508 wxMenuItem
*item
= node
->GetData();
509 y
+= item
->GetHeight();
519 return wxMenuItemList::compatibility_iterator();
525 // ----------------------------------------------------------------------------
526 // wxPopupMenuWindow drawing
527 // ----------------------------------------------------------------------------
529 void wxPopupMenuWindow::RefreshItem(wxMenuItem
*item
)
531 wxCHECK_RET( item
, _T("can't refresh NULL item") );
533 wxASSERT_MSG( IsShown(), _T("can't refresh menu which is not shown") );
535 // FIXME: -1 here because of SetLogicalOrigin(1, 1) in DoDraw()
536 RefreshRect(wxRect(0, item
->GetPosition() - 1,
537 m_menu
->GetGeometryInfo().GetSize().x
, item
->GetHeight()));
540 void wxPopupMenuWindow::DoDraw(wxControlRenderer
*renderer
)
542 // no clipping so far - do we need it? I don't think so as the menu is
543 // never partially covered as it is always on top of everything
545 wxDC
& dc
= renderer
->GetDC();
546 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
548 // FIXME: this should be done in the renderer, however when it is fixed
549 // wxPopupMenuWindow::RefreshItem() should be changed too!
550 dc
.SetLogicalOrigin(1, 1);
552 wxRenderer
*rend
= renderer
->GetRenderer();
555 const wxMenuGeometryInfo
& gi
= m_menu
->GetGeometryInfo();
556 for ( wxMenuItemList::compatibility_iterator node
= m_menu
->GetMenuItems().GetFirst();
558 node
= node
->GetNext() )
560 wxMenuItem
*item
= node
->GetData();
562 if ( item
->IsSeparator() )
564 rend
->DrawMenuSeparator(dc
, y
, gi
);
566 else // not a separator
569 if ( item
->IsCheckable() )
571 flags
|= wxCONTROL_CHECKABLE
;
573 if ( item
->IsChecked() )
575 flags
|= wxCONTROL_CHECKED
;
579 if ( !item
->IsEnabled() )
580 flags
|= wxCONTROL_DISABLED
;
582 if ( item
->IsSubMenu() )
583 flags
|= wxCONTROL_ISSUBMENU
;
585 if ( item
== GetCurrentItem() )
586 flags
|= wxCONTROL_SELECTED
;
594 item
->GetAccelString(),
595 // strangely enough, for unchecked item we use the
596 // "checked" bitmap because this is the default one - this
597 // explains this strange boolean expression
598 item
->GetBitmap(!item
->IsCheckable() || item
->IsChecked()),
600 item
->GetAccelIndex()
604 y
+= item
->GetHeight();
608 // ----------------------------------------------------------------------------
609 // wxPopupMenuWindow actions
610 // ----------------------------------------------------------------------------
612 void wxPopupMenuWindow::ClickItem(wxMenuItem
*item
)
614 wxCHECK_RET( item
, _T("can't click NULL item") );
616 wxASSERT_MSG( !item
->IsSeparator() && !item
->IsSubMenu(),
617 _T("can't click this item") );
619 wxMenu
* menu
= m_menu
;
624 menu
->ClickItem(item
);
627 void wxPopupMenuWindow::OpenSubmenu(wxMenuItem
*item
, InputMethod how
)
629 wxCHECK_RET( item
, _T("can't open NULL submenu") );
631 wxMenu
*submenu
= item
->GetSubMenu();
632 wxCHECK_RET( submenu
, _T("can only open submenus!") );
634 // FIXME: should take into account the border width
635 submenu
->Popup(ClientToScreen(wxPoint(0, item
->GetPosition())),
636 wxSize(m_menu
->GetGeometryInfo().GetSize().x
, 0),
637 how
== WithKeyboard
/* preselect first item then */);
639 m_hasOpenSubMenu
= TRUE
;
642 bool wxPopupMenuWindow::ActivateItem(wxMenuItem
*item
, InputMethod how
)
644 // don't activate disabled items
645 if ( !item
|| !item
->IsEnabled() )
650 // normal menu items generate commands, submenus can be opened and
651 // the separators don't do anything
652 if ( item
->IsSubMenu() )
654 OpenSubmenu(item
, how
);
656 else if ( !item
->IsSeparator() )
660 else // separator, can't activate
668 // ----------------------------------------------------------------------------
669 // wxPopupMenuWindow input handling
670 // ----------------------------------------------------------------------------
672 bool wxPopupMenuWindow::ProcessLeftDown(wxMouseEvent
& event
)
674 // wxPopupWindowHandler dismisses the window when the mouse is clicked
675 // outside it which is usually just fine, but there is one case when we
676 // don't want to do it: if the mouse was clicked on the parent submenu item
677 // which opens this menu, so check for it
679 wxPoint pos
= event
.GetPosition();
680 if ( HitTest(pos
.x
, pos
.y
) == wxHT_WINDOW_OUTSIDE
)
682 wxMenu
*menu
= m_menu
->GetParent();
685 wxPopupMenuWindow
*win
= menu
->m_popupMenu
;
687 wxCHECK_MSG( win
, FALSE
, _T("parent menu not shown?") );
689 pos
= ClientToScreen(pos
);
690 if ( win
->GetMenuItemFromPoint(win
->ScreenToClient(pos
)) )
695 //else: it is outside the parent menu as well, do dismiss this one
702 void wxPopupMenuWindow::OnLeftUp(wxMouseEvent
& event
)
704 wxMenuItemList::compatibility_iterator node
= GetMenuItemFromPoint(event
.GetPosition());
707 ActivateItem(node
->GetData(), WithMouse
);
711 void wxPopupMenuWindow::OnMouseMove(wxMouseEvent
& event
)
713 const wxPoint pt
= event
.GetPosition();
715 // we need to ignore extra mouse events: example when this happens is when
716 // the mouse is on the menu and we open a submenu from keyboard - Windows
717 // then sends us a dummy mouse move event, we (correctly) determine that it
718 // happens in the parent menu and so immediately close the just opened
721 static wxPoint s_ptLast
;
722 wxPoint ptCur
= ClientToScreen(pt
);
723 if ( ptCur
== s_ptLast
)
731 ProcessMouseMove(pt
);
736 void wxPopupMenuWindow::ProcessMouseMove(const wxPoint
& pt
)
738 wxMenuItemList::compatibility_iterator node
= GetMenuItemFromPoint(pt
);
740 // don't reset current to NULL here, we only do it when the mouse leaves
741 // the window (see below)
744 if ( node
!= m_nodeCurrent
)
748 wxMenuItem
*item
= GetCurrentItem();
751 OpenSubmenu(item
, WithMouse
);
754 //else: same item, nothing to do
756 else // not on an item
758 // the last open submenu forwards the mouse move messages to its
759 // parent, so if the mouse moves to another item of the parent menu,
760 // this menu is closed and this other item is selected - in the similar
761 // manner, the top menu forwards the mouse moves to the menubar which
762 // allows to select another top level menu by just moving the mouse
764 // we need to translate our client coords to the client coords of the
765 // window we forward this event to
766 wxPoint ptScreen
= ClientToScreen(pt
);
768 // if the mouse is outside this menu, let the parent one to
770 wxMenu
*menuParent
= m_menu
->GetParent();
773 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
775 // if we're shown, the parent menu must be also shown
776 wxCHECK_RET( win
, _T("parent menu is not shown?") );
778 win
->ProcessMouseMove(win
->ScreenToClient(ptScreen
));
780 else // no parent menu
782 wxMenuBar
*menubar
= m_menu
->GetMenuBar();
785 if ( menubar
->ProcessMouseEvent(
786 menubar
->ScreenToClient(ptScreen
)) )
788 // menubar has closed this menu and opened another one, probably
793 //else: top level popup menu, no other processing to do
797 void wxPopupMenuWindow::OnMouseLeave(wxMouseEvent
& event
)
799 // due to the artefact of mouse events generation under MSW, we actually
800 // may get the mouse leave event after the menu had been already dismissed
801 // and calling ChangeCurrent() would then assert, so don't do it
804 // we shouldn't change the current them if our submenu is opened and
805 // mouse moved there, in this case the submenu is responsable for
808 if ( HasOpenSubmenu() )
810 wxMenuItem
*item
= GetCurrentItem();
811 wxCHECK_RET( CanOpen(item
), _T("where is our open submenu?") );
813 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
814 wxCHECK_RET( win
, _T("submenu is opened but not shown?") );
816 // only handle this event if the mouse is not inside the submenu
817 wxPoint pt
= ClientToScreen(event
.GetPosition());
819 win
->HitTest(win
->ScreenToClient(pt
)) == wxHT_WINDOW_OUTSIDE
;
823 // this menu is the last opened
830 ChangeCurrent(wxMenuItemList::compatibility_iterator());
840 void wxPopupMenuWindow::OnKeyDown(wxKeyEvent
& event
)
842 if ( !ProcessKeyDown(event
.GetKeyCode()) )
848 bool wxPopupMenuWindow::ProcessKeyDown(int key
)
850 wxMenuItem
*item
= GetCurrentItem();
852 // first let the opened submenu to have it (no test for IsEnabled() here,
853 // the keys navigate even in a disabled submenu if we had somehow managed
854 // to open it inspit of this)
855 if ( HasOpenSubmenu() )
857 wxCHECK_MSG( CanOpen(item
), FALSE
,
858 _T("has open submenu but another item selected?") );
860 if ( item
->GetSubMenu()->ProcessKeyDown(key
) )
864 bool processed
= TRUE
;
866 // handle the up/down arrows, home, end, esc and return here, pass the
867 // left/right arrows to the menu bar except when the right arrow can be
868 // used to open a submenu
872 // if we're not a top level menu, close us, else leave this to the
874 if ( !m_menu
->GetParent() )
883 // close just this menu
885 HandleDismiss(FALSE
);
889 processed
= ActivateItem(item
);
893 ChangeCurrent(m_menu
->GetMenuItems().GetFirst());
897 ChangeCurrent(m_menu
->GetMenuItems().GetLast());
903 bool up
= key
== WXK_UP
;
905 wxMenuItemList::compatibility_iterator nodeStart
= up
? GetPrevNode()
908 while ( node
&& node
->GetData()->IsSeparator() )
910 node
= up
? GetPrevNode(node
) : GetNextNode(node
);
912 if ( node
== nodeStart
)
914 // nothing but separators and disabled items in this
917 node
= wxMenuItemList::compatibility_iterator();
936 // don't try to reopen an already opened menu
937 if ( !HasOpenSubmenu() && CanOpen(item
) )
948 // look for the menu item starting with this letter
949 if ( wxIsalnum(key
) )
951 // we want to start from the item after this one because
952 // if we're already on the item with the given accel we want to
953 // go to the next one, not to stay in place
954 wxMenuItemList::compatibility_iterator nodeStart
= GetNextNode();
956 // do we have more than one item with this accel?
957 bool notUnique
= FALSE
;
959 // translate everything to lower case before comparing
960 wxChar chAccel
= wxTolower(key
);
962 // loop through all items searching for the item with this
964 wxMenuItemList::compatibility_iterator node
= nodeStart
,
966 nodeFound
= wxMenuItemList::compatibility_iterator();
972 item
= node
->GetData();
974 int idxAccel
= item
->GetAccelIndex();
975 if ( idxAccel
!= -1 &&
976 wxTolower(item
->GetLabel()[(size_t)idxAccel
])
979 // ok, found an item with this accel
982 // store it but continue searching as we need to
983 // know if it's the only item with this accel or if
987 else // we already had found such item
991 // no need to continue further, we won't find
992 // anything we don't already know
997 // we want to iterate over all items wrapping around if
999 node
= GetNextNode(node
);
1000 if ( node
== nodeStart
)
1002 // we've seen all nodes
1009 item
= nodeFound
->GetData();
1011 // go to this item anyhow
1012 ChangeCurrent(nodeFound
);
1014 if ( !notUnique
&& item
->IsEnabled() )
1016 // unique item with this accel - activate it
1017 processed
= ActivateItem(item
);
1019 //else: just select it but don't activate as the user might
1020 // have wanted to activate another item
1022 // skip "processed = FALSE" below
1033 // ----------------------------------------------------------------------------
1035 // ----------------------------------------------------------------------------
1043 m_startRadioGroup
= -1;
1052 // ----------------------------------------------------------------------------
1053 // wxMenu and wxMenuGeometryInfo
1054 // ----------------------------------------------------------------------------
1056 wxMenuGeometryInfo::~wxMenuGeometryInfo()
1060 const wxMenuGeometryInfo
& wxMenu::GetGeometryInfo() const
1066 wxConstCast(this, wxMenu
)->m_geometry
=
1067 m_popupMenu
->GetRenderer()->GetMenuGeometry(m_popupMenu
, *this);
1071 wxFAIL_MSG( _T("can't get geometry without window") );
1078 void wxMenu::InvalidateGeometryInfo()
1087 // ----------------------------------------------------------------------------
1088 // wxMenu adding/removing items
1089 // ----------------------------------------------------------------------------
1091 void wxMenu::OnItemAdded(wxMenuItem
*item
)
1093 InvalidateGeometryInfo();
1097 #endif // wxUSE_ACCEL
1099 // the submenus of a popup menu should have the same invoking window as it
1101 if ( m_invokingWindow
&& item
->IsSubMenu() )
1103 item
->GetSubMenu()->SetInvokingWindow(m_invokingWindow
);
1107 void wxMenu::EndRadioGroup()
1109 // we're not inside a radio group any longer
1110 m_startRadioGroup
= -1;
1113 bool wxMenu::DoAppend(wxMenuItem
*item
)
1120 if ( item
->GetKind() == wxITEM_RADIO
)
1122 int count
= GetMenuItemCount();
1124 if ( m_startRadioGroup
== -1 )
1126 // start a new radio group
1127 m_startRadioGroup
= count
;
1129 // for now it has just one element
1130 item
->SetAsRadioGroupStart();
1131 item
->SetRadioGroupEnd(m_startRadioGroup
);
1133 // ensure that we have a checked item in the radio group
1139 else // extend the current radio group
1141 // we need to update its end item
1142 item
->SetRadioGroupStart(m_startRadioGroup
);
1143 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
1147 node
->GetData()->SetRadioGroupEnd(count
);
1151 wxFAIL_MSG( _T("where is the radio group start item?") );
1155 else // not a radio item
1160 if ( !wxMenuBase::DoAppend(item
) )
1168 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1170 if ( !wxMenuBase::DoInsert(pos
, item
) )
1178 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1180 wxMenuItem
*itemOld
= wxMenuBase::DoRemove(item
);
1184 InvalidateGeometryInfo();
1187 RemoveAccelFor(item
);
1188 #endif // wxUSE_ACCEL
1194 // ----------------------------------------------------------------------------
1195 // wxMenu attaching/detaching
1196 // ----------------------------------------------------------------------------
1198 void wxMenu::Attach(wxMenuBarBase
*menubar
)
1200 wxMenuBase::Attach(menubar
);
1202 wxCHECK_RET( m_menuBar
, _T("menubar can't be NULL after attaching") );
1204 // unfortunately, we can't use m_menuBar->GetEventHandler() here because,
1205 // if the menubar is currently showing a menu, its event handler is a
1206 // temporary one installed by wxPopupWindow and so will disappear soon any
1207 // any attempts to use it from the newly attached menu would result in a
1210 // so we use the menubar itself, even if it's a pity as it means we can't
1211 // redirect all menu events by changing the menubar handler (FIXME)
1212 SetNextHandler(m_menuBar
);
1215 void wxMenu::Detach()
1217 wxMenuBase::Detach();
1220 // ----------------------------------------------------------------------------
1221 // wxMenu misc functions
1222 // ----------------------------------------------------------------------------
1224 wxWindow
*wxMenu::GetRootWindow() const
1228 // simple case - a normal menu attached to the menubar
1232 // we're a popup menu but the trouble is that only the top level popup menu
1233 // has a pointer to the invoking window, so we must walk up the menu chain
1235 wxWindow
*win
= GetInvokingWindow();
1238 // we already have it
1242 wxMenu
*menu
= GetParent();
1245 // We are a submenu of a menu of a menubar
1246 if (menu
->GetMenuBar())
1247 return menu
->GetMenuBar();
1249 win
= menu
->GetInvokingWindow();
1253 menu
= menu
->GetParent();
1256 // we're probably going to crash in the caller anyhow, but try to detect
1257 // this error as soon as possible
1258 wxASSERT_MSG( win
, _T("menu without any associated window?") );
1260 // also remember it in this menu so that we don't have to search for it the
1262 wxConstCast(this, wxMenu
)->m_invokingWindow
= win
;
1267 wxRenderer
*wxMenu::GetRenderer() const
1269 // we're going to crash without renderer!
1270 wxCHECK_MSG( m_popupMenu
, NULL
, _T("neither popup nor menubar menu?") );
1272 return m_popupMenu
->GetRenderer();
1275 void wxMenu::RefreshItem(wxMenuItem
*item
)
1277 // the item geometry changed, so our might have changed as well
1278 InvalidateGeometryInfo();
1282 // this would be a bug in IsShown()
1283 wxCHECK_RET( m_popupMenu
, _T("must have popup window if shown!") );
1285 // recalc geometry to update the item height and such
1286 (void)GetGeometryInfo();
1288 m_popupMenu
->RefreshItem(item
);
1292 // ----------------------------------------------------------------------------
1293 // wxMenu showing and hiding
1294 // ----------------------------------------------------------------------------
1296 bool wxMenu::IsShown() const
1298 return m_popupMenu
&& m_popupMenu
->IsShown();
1301 void wxMenu::OnDismiss(bool dismissParent
)
1305 // always notify the parent about submenu disappearance
1306 wxPopupMenuWindow
*win
= m_menuParent
->m_popupMenu
;
1309 win
->OnSubmenuDismiss();
1313 wxFAIL_MSG( _T("parent menu not shown?") );
1316 // and if we dismiss everything, propagate to parent
1317 if ( dismissParent
)
1319 // dismissParent is recursive
1320 m_menuParent
->Dismiss();
1321 m_menuParent
->OnDismiss(TRUE
);
1324 else // no parent menu
1326 // notify the menu bar if we're a top level menu
1329 m_menuBar
->OnDismissMenu(dismissParent
);
1333 wxCHECK_RET( m_invokingWindow
, _T("what kind of menu is this?") );
1335 m_invokingWindow
->DismissPopupMenu();
1337 // Why reset it here? We need it for sending the event to...
1338 // SetInvokingWindow(NULL);
1343 void wxMenu::Popup(const wxPoint
& pos
, const wxSize
& size
, bool selectFirst
)
1345 // create the popup window if not done yet
1348 m_popupMenu
= new wxPopupMenuWindow(GetRootWindow(), this);
1351 // select the first item unless disabled
1354 m_popupMenu
->SelectFirst();
1357 // the geometry might have changed since the last time we were shown, so
1359 m_popupMenu
->SetClientSize(GetGeometryInfo().GetSize());
1361 // position it as specified
1362 m_popupMenu
->Position(pos
, size
);
1364 // the menu can't have the focus itself (it is a Windows limitation), so
1365 // always keep the focus at the originating window
1366 wxWindow
*focus
= GetRootWindow();
1368 wxASSERT_MSG( focus
, _T("no window to keep focus on?") );
1371 m_popupMenu
->Popup(focus
);
1374 void wxMenu::Dismiss()
1376 wxCHECK_RET( IsShown(), _T("can't dismiss hidden menu") );
1378 m_popupMenu
->Dismiss();
1381 // ----------------------------------------------------------------------------
1382 // wxMenu event processing
1383 // ----------------------------------------------------------------------------
1385 bool wxMenu::ProcessKeyDown(int key
)
1387 wxCHECK_MSG( m_popupMenu
, FALSE
,
1388 _T("can't process key events if not shown") );
1390 return m_popupMenu
->ProcessKeyDown(key
);
1393 bool wxMenu::ClickItem(wxMenuItem
*item
)
1396 if ( item
->IsCheckable() )
1398 // update the item state
1399 isChecked
= !item
->IsChecked();
1401 item
->Check(isChecked
!= 0);
1409 return SendEvent(item
->GetId(), isChecked
);
1412 // ----------------------------------------------------------------------------
1413 // wxMenu accel support
1414 // ----------------------------------------------------------------------------
1418 bool wxMenu::ProcessAccelEvent(const wxKeyEvent
& event
)
1420 // do we have an item for this accel?
1421 wxMenuItem
*item
= m_accelTable
.GetMenuItem(event
);
1422 if ( item
&& item
->IsEnabled() )
1424 return ClickItem(item
);
1428 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
1430 node
= node
->GetNext() )
1432 const wxMenuItem
*item
= node
->GetData();
1433 if ( item
->IsSubMenu() && item
->IsEnabled() )
1436 if ( item
->GetSubMenu()->ProcessAccelEvent(event
) )
1446 void wxMenu::AddAccelFor(wxMenuItem
*item
)
1448 wxAcceleratorEntry
*accel
= item
->GetAccel();
1451 accel
->SetMenuItem(item
);
1453 m_accelTable
.Add(*accel
);
1459 void wxMenu::RemoveAccelFor(wxMenuItem
*item
)
1461 wxAcceleratorEntry
*accel
= item
->GetAccel();
1464 m_accelTable
.Remove(*accel
);
1470 #endif // wxUSE_ACCEL
1472 // ----------------------------------------------------------------------------
1473 // wxMenuItem construction
1474 // ----------------------------------------------------------------------------
1476 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
1478 const wxString
& text
,
1479 const wxString
& help
,
1482 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
1487 m_radioGroup
.start
= -1;
1488 m_isRadioGroupStart
= FALSE
;
1493 wxMenuItem::~wxMenuItem()
1497 // ----------------------------------------------------------------------------
1498 // wxMenuItemBase methods implemented here
1499 // ----------------------------------------------------------------------------
1502 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
1504 const wxString
& name
,
1505 const wxString
& help
,
1509 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
1513 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
1515 return wxStripMenuCodes(text
);
1518 // ----------------------------------------------------------------------------
1519 // wxMenuItem operations
1520 // ----------------------------------------------------------------------------
1522 void wxMenuItem::NotifyMenu()
1524 m_parentMenu
->RefreshItem(this);
1527 void wxMenuItem::UpdateAccelInfo()
1529 m_indexAccel
= wxControl::FindAccelIndex(m_text
);
1531 // will be empty if the text contains no TABs - ok
1532 m_strAccel
= m_text
.AfterFirst(_T('\t'));
1535 void wxMenuItem::SetText(const wxString
& text
)
1537 if ( text
!= m_text
)
1539 // first call the base class version to change m_text
1540 wxMenuItemBase::SetText(text
);
1548 void wxMenuItem::SetCheckable(bool checkable
)
1550 if ( checkable
!= IsCheckable() )
1552 wxMenuItemBase::SetCheckable(checkable
);
1558 void wxMenuItem::SetBitmaps(const wxBitmap
& bmpChecked
,
1559 const wxBitmap
& bmpUnchecked
)
1561 m_bmpChecked
= bmpChecked
;
1562 m_bmpUnchecked
= bmpUnchecked
;
1567 void wxMenuItem::Enable(bool enable
)
1569 if ( enable
!= m_isEnabled
)
1571 wxMenuItemBase::Enable(enable
);
1577 void wxMenuItem::Check(bool check
)
1579 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
1581 if ( m_isChecked
== check
)
1584 if ( GetKind() == wxITEM_RADIO
)
1586 // it doesn't make sense to uncheck a radio item - what would this do?
1590 // get the index of this item in the menu
1591 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
1592 int pos
= items
.IndexOf(this);
1593 wxCHECK_RET( pos
!= wxNOT_FOUND
,
1594 _T("menuitem not found in the menu items list?") );
1596 // get the radio group range
1600 if ( m_isRadioGroupStart
)
1602 // we already have all information we need
1604 end
= m_radioGroup
.end
;
1606 else // next radio group item
1608 // get the radio group end from the start item
1609 start
= m_radioGroup
.start
;
1610 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
1613 // also uncheck all the other items in this radio group
1614 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
1615 for ( int n
= start
; n
<= end
&& node
; n
++ )
1619 node
->GetData()->m_isChecked
= FALSE
;
1621 node
= node
->GetNext();
1625 wxMenuItemBase::Check(check
);
1630 // radio group stuff
1631 // -----------------
1633 void wxMenuItem::SetAsRadioGroupStart()
1635 m_isRadioGroupStart
= TRUE
;
1638 void wxMenuItem::SetRadioGroupStart(int start
)
1640 wxASSERT_MSG( !m_isRadioGroupStart
,
1641 _T("should only be called for the next radio items") );
1643 m_radioGroup
.start
= start
;
1646 void wxMenuItem::SetRadioGroupEnd(int end
)
1648 wxASSERT_MSG( m_isRadioGroupStart
,
1649 _T("should only be called for the first radio item") );
1651 m_radioGroup
.end
= end
;
1654 // ----------------------------------------------------------------------------
1655 // wxMenuBar creation
1656 // ----------------------------------------------------------------------------
1658 void wxMenuBar::Init()
1666 m_shouldShowMenu
= FALSE
;
1668 m_windowStyle
|= wxNO_FULL_REPAINT_ON_RESIZE
;
1671 void wxMenuBar::Attach(wxFrame
*frame
)
1673 // maybe you really wanted to call Detach()?
1674 wxCHECK_RET( frame
, _T("wxMenuBar::Attach(NULL) called") );
1676 wxMenuBarBase::Attach(frame
);
1680 // reparent if necessary
1681 if ( m_frameLast
!= frame
)
1686 // show it back - was hidden by Detach()
1689 else // not created yet, do it now
1691 // we have no way to return the error from here anyhow :-(
1692 (void)Create(frame
, -1);
1694 SetCursor(wxCURSOR_ARROW
);
1696 SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT
));
1698 // calculate and set our height (it won't be changed any more)
1699 SetSize(-1, GetBestSize().y
);
1702 // remember the last frame which had us to avoid unnecessarily reparenting
1704 m_frameLast
= frame
;
1707 void wxMenuBar::Detach()
1709 // don't delete the window because we may be reattached later, just hide it
1715 wxMenuBarBase::Detach();
1718 wxMenuBar::~wxMenuBar()
1722 // ----------------------------------------------------------------------------
1723 // wxMenuBar adding/removing items
1724 // ----------------------------------------------------------------------------
1726 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1728 return Insert(GetCount(), menu
, title
);
1731 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1733 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1736 wxMenuInfo
*info
= new wxMenuInfo(title
);
1737 m_menuInfos
.Insert(info
, pos
);
1739 RefreshAllItemsAfter(pos
);
1744 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1746 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1750 wxMenuInfo
& info
= m_menuInfos
[pos
];
1752 info
.SetLabel(title
);
1754 // even if the old menu was disabled, the new one is not any more
1757 // even if we change only this one, the new label has different width,
1758 // so we need to refresh everything beyond this item as well
1759 RefreshAllItemsAfter(pos
);
1765 wxMenu
*wxMenuBar::Remove(size_t pos
)
1767 wxMenu
*menuOld
= wxMenuBarBase::Remove(pos
);
1771 m_menuInfos
.RemoveAt(pos
);
1773 // this doesn't happen too often, so don't try to be too smart - just
1774 // refresh everything
1781 // ----------------------------------------------------------------------------
1782 // wxMenuBar top level menus access
1783 // ----------------------------------------------------------------------------
1785 wxCoord
wxMenuBar::GetItemWidth(size_t pos
) const
1787 return m_menuInfos
[pos
].GetWidth(wxConstCast(this, wxMenuBar
));
1790 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1792 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1794 if ( enable
!= m_menuInfos
[pos
].IsEnabled() )
1796 m_menuInfos
[pos
].SetEnabled(enable
);
1800 //else: nothing to do
1803 bool wxMenuBar::IsEnabledTop(size_t pos
) const
1805 wxCHECK_MSG( pos
< GetCount(), FALSE
, _T("invalid index in IsEnabledTop") );
1807 return m_menuInfos
[pos
].IsEnabled();
1810 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
1812 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1814 if ( label
!= m_menuInfos
[pos
].GetLabel() )
1816 m_menuInfos
[pos
].SetLabel(label
);
1820 //else: nothing to do
1823 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
1825 wxCHECK_MSG( pos
< GetCount(), _T(""), _T("invalid index in GetLabelTop") );
1827 return m_menuInfos
[pos
].GetLabel();
1830 // ----------------------------------------------------------------------------
1831 // wxMenuBar drawing
1832 // ----------------------------------------------------------------------------
1834 void wxMenuBar::RefreshAllItemsAfter(size_t pos
)
1838 // no need to refresh if nothing is shown yet
1842 wxRect rect
= GetItemRect(pos
);
1843 rect
.width
= GetClientSize().x
- rect
.x
;
1847 void wxMenuBar::RefreshItem(size_t pos
)
1849 wxCHECK_RET( pos
!= (size_t)-1,
1850 _T("invalid item in wxMenuBar::RefreshItem") );
1854 // no need to refresh if nothing is shown yet
1858 RefreshRect(GetItemRect(pos
));
1861 void wxMenuBar::DoDraw(wxControlRenderer
*renderer
)
1863 wxDC
& dc
= renderer
->GetDC();
1864 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1866 // redraw only the items which must be redrawn
1868 // we don't have to use GetUpdateClientRect() here because our client rect
1869 // is the same as total one
1870 wxRect rectUpdate
= GetUpdateRegion().GetBox();
1872 int flagsMenubar
= GetStateFlags();
1876 rect
.height
= GetClientSize().y
;
1879 size_t count
= GetCount();
1880 for ( size_t n
= 0; n
< count
; n
++ )
1882 if ( x
> rectUpdate
.GetRight() )
1884 // all remaining items are to the right of rectUpdate
1889 rect
.width
= GetItemWidth(n
);
1891 if ( x
< rectUpdate
.x
)
1893 // this item is still to the left of rectUpdate
1897 int flags
= flagsMenubar
;
1898 if ( m_current
!= -1 && n
== (size_t)m_current
)
1900 flags
|= wxCONTROL_SELECTED
;
1903 if ( !IsEnabledTop(n
) )
1905 flags
|= wxCONTROL_DISABLED
;
1908 GetRenderer()->DrawMenuBarItem
1912 m_menuInfos
[n
].GetLabel(),
1914 m_menuInfos
[n
].GetAccelIndex()
1919 // ----------------------------------------------------------------------------
1920 // wxMenuBar geometry
1921 // ----------------------------------------------------------------------------
1923 wxRect
wxMenuBar::GetItemRect(size_t pos
) const
1925 wxASSERT_MSG( pos
< GetCount(), _T("invalid menu bar item index") );
1926 wxASSERT_MSG( IsCreated(), _T("can't call this method yet") );
1931 rect
.height
= GetClientSize().y
;
1933 for ( size_t n
= 0; n
< pos
; n
++ )
1935 rect
.x
+= GetItemWidth(n
);
1938 rect
.width
= GetItemWidth(pos
);
1943 wxSize
wxMenuBar::DoGetBestClientSize() const
1946 if ( GetMenuCount() > 0 )
1948 wxClientDC
dc(wxConstCast(this, wxMenuBar
));
1949 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1950 dc
.GetTextExtent(GetLabelTop(0), &size
.x
, &size
.y
);
1952 // adjust for the renderer we use
1953 size
= GetRenderer()->GetMenuBarItemSize(size
);
1955 else // empty menubar
1961 // the width is arbitrary, of course, for horizontal menubar
1967 int wxMenuBar::GetMenuFromPoint(const wxPoint
& pos
) const
1969 if ( pos
.x
< 0 || pos
.y
< 0 || pos
.y
> GetClientSize().y
)
1974 size_t count
= GetCount();
1975 for ( size_t item
= 0; item
< count
; item
++ )
1977 x
+= GetItemWidth(item
);
1985 // to the right of the last menu item
1989 // ----------------------------------------------------------------------------
1990 // wxMenuBar menu operations
1991 // ----------------------------------------------------------------------------
1993 void wxMenuBar::SelectMenu(size_t pos
)
1996 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::SelectMenu"));
2002 void wxMenuBar::DoSelectMenu(size_t pos
)
2004 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in DoSelectMenu") );
2006 int posOld
= m_current
;
2012 // close the previous menu
2013 if ( IsShowingMenu() )
2015 // restore m_shouldShowMenu flag after DismissMenu() which resets
2017 bool old
= m_shouldShowMenu
;
2021 m_shouldShowMenu
= old
;
2024 RefreshItem((size_t)posOld
);
2030 void wxMenuBar::PopupMenu(size_t pos
)
2032 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in PopupCurrentMenu") );
2039 // ----------------------------------------------------------------------------
2040 // wxMenuBar input handing
2041 // ----------------------------------------------------------------------------
2044 Note that wxMenuBar doesn't use wxInputHandler but handles keyboard and
2045 mouse in the same way under all platforms. This is because it doesn't derive
2046 from wxControl (which works with input handlers) but directly from wxWindow.
2048 Also, menu bar input handling is rather simple, so maybe it's not really
2049 worth making it themeable - at least I've decided against doing it now as it
2050 would merging the changes back into trunk more difficult. But it still could
2051 be done later if really needed.
2054 void wxMenuBar::OnKillFocus(wxFocusEvent
& event
)
2056 if ( m_current
!= -1 )
2058 RefreshItem((size_t)m_current
);
2066 void wxMenuBar::OnLeftDown(wxMouseEvent
& event
)
2074 else // we didn't have mouse capture, capture it now
2076 m_current
= GetMenuFromPoint(event
.GetPosition());
2077 if ( m_current
== -1 )
2079 // unfortunately, we can't prevent wxMSW from giving us the focus,
2080 // so we can only give it back
2085 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::OnLeftDown"));
2088 // show it as selected
2089 RefreshItem((size_t)m_current
);
2092 PopupCurrentMenu(FALSE
/* don't select first item - as Windows does */);
2097 void wxMenuBar::OnMouseMove(wxMouseEvent
& event
)
2101 (void)ProcessMouseEvent(event
.GetPosition());
2109 bool wxMenuBar::ProcessMouseEvent(const wxPoint
& pt
)
2111 // a hack to ignore the extra mouse events MSW sends us: this is similar to
2112 // wxUSE_MOUSEEVENT_HACK in wxWin itself but it isn't enough for us here as
2113 // we get the messages from different windows (old and new popup menus for
2116 static wxPoint s_ptLast
;
2117 if ( pt
== s_ptLast
)
2125 int currentNew
= GetMenuFromPoint(pt
);
2126 if ( (currentNew
== -1) || (currentNew
== m_current
) )
2131 // select the new active item
2132 DoSelectMenu(currentNew
);
2134 // show the menu if we know that we should, even if we hadn't been showing
2135 // it before (this may happen if the previous menu was disabled)
2136 if ( m_shouldShowMenu
&& !m_menuShown
)
2138 // open the new menu if the old one we closed had been opened
2139 PopupCurrentMenu(FALSE
/* don't select first item - as Windows does */);
2145 void wxMenuBar::OnKeyDown(wxKeyEvent
& event
)
2147 // ensure that we have a current item - we might not have it if we're
2148 // given the focus with Alt or F10 press (and under GTK+ the menubar
2149 // somehow gets the keyboard events even when it doesn't have focus...)
2150 if ( m_current
== -1 )
2152 if ( !HasCapture() )
2156 else // we do have capture
2158 // we always maintain a valid current item while we're in modal
2159 // state (i.e. have the capture)
2160 wxFAIL_MSG( _T("how did we manage to lose current item?") );
2166 int key
= event
.GetKeyCode();
2168 // first let the menu have it
2169 if ( IsShowingMenu() && m_menuShown
->ProcessKeyDown(key
) )
2174 // cycle through the menu items when left/right arrows are pressed and open
2175 // the menu when up/down one is
2179 // Alt must be processed at wxWindow level too
2184 // remove the selection and give the focus away
2185 if ( m_current
!= -1 )
2187 if ( IsShowingMenu() )
2199 size_t count
= GetCount();
2202 // the item won't change anyhow
2205 //else: otherwise, it will
2207 // remember if we were showing a menu - if we did, we should
2208 // show the new menu after changing the item
2209 bool wasMenuOpened
= IsShowingMenu();
2210 if ( wasMenuOpened
)
2215 // cast is safe as we tested for -1 above
2216 size_t currentNew
= (size_t)m_current
;
2218 if ( key
== WXK_LEFT
)
2220 if ( currentNew
-- == 0 )
2221 currentNew
= count
- 1;
2225 if ( ++currentNew
== count
)
2229 DoSelectMenu(currentNew
);
2231 if ( wasMenuOpened
)
2246 // letters open the corresponding menu
2249 int idxFound
= FindNextItemForAccel(m_current
, key
, &unique
);
2251 if ( idxFound
!= -1 )
2253 if ( IsShowingMenu() )
2258 DoSelectMenu((size_t)idxFound
);
2260 // if the item is not unique, just select it but don't
2261 // activate as the user might have wanted to activate
2264 // also, don't try to open a disabled menu
2265 if ( unique
&& IsEnabledTop((size_t)idxFound
) )
2271 // skip the "event.Skip()" below
2280 // ----------------------------------------------------------------------------
2281 // wxMenuBar accel handling
2282 // ----------------------------------------------------------------------------
2284 int wxMenuBar::FindNextItemForAccel(int idxStart
, int key
, bool *unique
) const
2286 if ( !wxIsalnum(key
) )
2288 // we only support letters/digits as accels
2292 // do we have more than one item with this accel?
2296 // translate everything to lower case before comparing
2297 wxChar chAccel
= wxTolower(key
);
2299 // the index of the item with this accel
2302 // loop through all items searching for the item with this
2303 // accel starting at the item after the current one
2304 int count
= GetCount();
2305 int n
= idxStart
== -1 ? 0 : idxStart
+ 1;
2316 const wxMenuInfo
& info
= m_menuInfos
[n
];
2318 int idxAccel
= info
.GetAccelIndex();
2319 if ( idxAccel
!= -1 &&
2320 wxTolower(info
.GetLabel()[(size_t)idxAccel
])
2323 // ok, found an item with this accel
2324 if ( idxFound
== -1 )
2326 // store it but continue searching as we need to
2327 // know if it's the only item with this accel or if
2331 else // we already had found such item
2336 // no need to continue further, we won't find
2337 // anything we don't already know
2342 // we want to iterate over all items wrapping around if
2350 if ( n
== idxStart
)
2352 // we've seen all items
2362 bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent
& event
)
2365 for ( wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
2367 node
= node
->GetNext(), n
++ )
2369 // accels of the items in the disabled menus shouldn't work
2370 if ( m_menuInfos
[n
].IsEnabled() )
2372 if ( node
->GetData()->ProcessAccelEvent(event
) )
2374 // menu processed it
2384 #endif // wxUSE_ACCEL
2386 // ----------------------------------------------------------------------------
2387 // wxMenuBar menus showing
2388 // ----------------------------------------------------------------------------
2390 void wxMenuBar::PopupCurrentMenu(bool selectFirst
)
2392 wxCHECK_RET( m_current
!= -1, _T("no menu to popup") );
2394 // forgot to call DismissMenu()?
2395 wxASSERT_MSG( !m_menuShown
, _T("shouldn't show two menus at once!") );
2397 // in any case, we should show it - even if we won't
2398 m_shouldShowMenu
= TRUE
;
2400 if ( IsEnabledTop(m_current
) )
2402 // remember the menu we show
2403 m_menuShown
= GetMenu(m_current
);
2405 // we don't show the menu at all if it has no items
2406 if ( !m_menuShown
->IsEmpty() )
2408 // position it correctly: note that we must use screen coords and
2409 // that we pass 0 as width to position the menu exactly below the
2410 // item, not to the right of it
2411 wxRect rectItem
= GetItemRect(m_current
);
2413 m_menuShown
->Popup(ClientToScreen(rectItem
.GetPosition()),
2414 wxSize(0, rectItem
.GetHeight()),
2419 // reset it back as no menu is shown
2423 //else: don't show disabled menu
2426 void wxMenuBar::DismissMenu()
2428 wxCHECK_RET( m_menuShown
, _T("can't dismiss menu if none is shown") );
2430 m_menuShown
->Dismiss();
2434 void wxMenuBar::OnDismissMenu(bool dismissMenuBar
)
2436 m_shouldShowMenu
= FALSE
;
2438 if ( dismissMenuBar
)
2444 void wxMenuBar::OnDismiss()
2448 wxLogTrace(_T("mousecapture"), _T("Releasing mouse from wxMenuBar::OnDismiss"));
2449 GetCapture()->ReleaseMouse();
2452 if ( m_current
!= -1 )
2454 size_t current
= m_current
;
2457 RefreshItem(current
);
2463 void wxMenuBar::GiveAwayFocus()
2465 GetFrame()->SetFocus();
2468 // ----------------------------------------------------------------------------
2469 // popup menu support
2470 // ----------------------------------------------------------------------------
2472 wxEventLoop
*wxWindow::ms_evtLoopPopup
= NULL
;
2474 bool wxWindow::DoPopupMenu(wxMenu
*menu
, int x
, int y
)
2476 wxCHECK_MSG( !ms_evtLoopPopup
, FALSE
,
2477 _T("can't show more than one popup menu at a time") );
2480 // we need to change the cursor before showing the menu as, apparently, no
2481 // cursor changes took place while the mouse is captured
2482 wxCursor cursorOld
= GetCursor();
2483 SetCursor(wxCURSOR_ARROW
);
2487 // flash any delayed log messages before showing the menu, otherwise it
2488 // could be dismissed (because it would lose focus) immediately after being
2490 wxLog::FlushActive();
2492 // some controls update themselves from OnIdle() call - let them do it
2493 wxTheApp
->ProcessIdle();
2495 // if the window hadn't been refreshed yet, the menu can adversely affect
2496 // its next OnPaint() handler execution - i.e. scrolled window refresh
2497 // logic breaks then as it scrolls part of the menu which hadn't been there
2498 // when the update event was generated into view
2502 menu
->SetInvokingWindow(this);
2504 // wxLogDebug( "Name of invoking window %s", menu->GetInvokingWindow()->GetName().c_str() );
2506 menu
->Popup(ClientToScreen(wxPoint(x
, y
)), wxSize(0, 0));
2508 // this is not very useful if the menu was popped up because of the mouse
2509 // click but I think it is nice to do when it appears because of a key
2510 // press (i.e. Windows menu key)
2512 // Windows itself doesn't do it, but IMHO this is nice
2515 // we have to redirect all keyboard input to the menu temporarily
2516 PushEventHandler(new wxMenuKbdRedirector(menu
));
2518 // enter the local modal loop
2519 ms_evtLoopPopup
= new wxEventLoop
;
2520 ms_evtLoopPopup
->Run();
2522 delete ms_evtLoopPopup
;
2523 ms_evtLoopPopup
= NULL
;
2525 // remove the handler
2526 PopEventHandler(TRUE
/* delete it */);
2528 menu
->SetInvokingWindow(NULL
);
2531 SetCursor(cursorOld
);
2537 void wxWindow::DismissPopupMenu()
2539 wxCHECK_RET( ms_evtLoopPopup
, _T("no popup menu shown") );
2541 ms_evtLoopPopup
->Exit();
2544 #endif // wxUSE_MENUS