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
;
1669 void wxMenuBar::Attach(wxFrame
*frame
)
1671 // maybe you really wanted to call Detach()?
1672 wxCHECK_RET( frame
, _T("wxMenuBar::Attach(NULL) called") );
1674 wxMenuBarBase::Attach(frame
);
1678 // reparent if necessary
1679 if ( m_frameLast
!= frame
)
1684 // show it back - was hidden by Detach()
1687 else // not created yet, do it now
1689 // we have no way to return the error from here anyhow :-(
1690 (void)Create(frame
, -1);
1692 SetCursor(wxCURSOR_ARROW
);
1694 SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT
));
1696 // calculate and set our height (it won't be changed any more)
1697 SetSize(-1, GetBestSize().y
);
1700 // remember the last frame which had us to avoid unnecessarily reparenting
1702 m_frameLast
= frame
;
1705 void wxMenuBar::Detach()
1707 // don't delete the window because we may be reattached later, just hide it
1713 wxMenuBarBase::Detach();
1716 wxMenuBar::~wxMenuBar()
1720 // ----------------------------------------------------------------------------
1721 // wxMenuBar adding/removing items
1722 // ----------------------------------------------------------------------------
1724 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1726 return Insert(GetCount(), menu
, title
);
1729 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1731 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1734 wxMenuInfo
*info
= new wxMenuInfo(title
);
1735 m_menuInfos
.Insert(info
, pos
);
1737 RefreshAllItemsAfter(pos
);
1742 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1744 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1748 wxMenuInfo
& info
= m_menuInfos
[pos
];
1750 info
.SetLabel(title
);
1752 // even if the old menu was disabled, the new one is not any more
1755 // even if we change only this one, the new label has different width,
1756 // so we need to refresh everything beyond this item as well
1757 RefreshAllItemsAfter(pos
);
1763 wxMenu
*wxMenuBar::Remove(size_t pos
)
1765 wxMenu
*menuOld
= wxMenuBarBase::Remove(pos
);
1769 m_menuInfos
.RemoveAt(pos
);
1771 // this doesn't happen too often, so don't try to be too smart - just
1772 // refresh everything
1779 // ----------------------------------------------------------------------------
1780 // wxMenuBar top level menus access
1781 // ----------------------------------------------------------------------------
1783 wxCoord
wxMenuBar::GetItemWidth(size_t pos
) const
1785 return m_menuInfos
[pos
].GetWidth(wxConstCast(this, wxMenuBar
));
1788 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1790 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1792 if ( enable
!= m_menuInfos
[pos
].IsEnabled() )
1794 m_menuInfos
[pos
].SetEnabled(enable
);
1798 //else: nothing to do
1801 bool wxMenuBar::IsEnabledTop(size_t pos
) const
1803 wxCHECK_MSG( pos
< GetCount(), FALSE
, _T("invalid index in IsEnabledTop") );
1805 return m_menuInfos
[pos
].IsEnabled();
1808 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
1810 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1812 if ( label
!= m_menuInfos
[pos
].GetLabel() )
1814 m_menuInfos
[pos
].SetLabel(label
);
1818 //else: nothing to do
1821 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
1823 wxCHECK_MSG( pos
< GetCount(), _T(""), _T("invalid index in GetLabelTop") );
1825 return m_menuInfos
[pos
].GetLabel();
1828 // ----------------------------------------------------------------------------
1829 // wxMenuBar drawing
1830 // ----------------------------------------------------------------------------
1832 void wxMenuBar::RefreshAllItemsAfter(size_t pos
)
1836 // no need to refresh if nothing is shown yet
1840 wxRect rect
= GetItemRect(pos
);
1841 rect
.width
= GetClientSize().x
- rect
.x
;
1845 void wxMenuBar::RefreshItem(size_t pos
)
1847 wxCHECK_RET( pos
!= (size_t)-1,
1848 _T("invalid item in wxMenuBar::RefreshItem") );
1852 // no need to refresh if nothing is shown yet
1856 RefreshRect(GetItemRect(pos
));
1859 void wxMenuBar::DoDraw(wxControlRenderer
*renderer
)
1861 wxDC
& dc
= renderer
->GetDC();
1862 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1864 // redraw only the items which must be redrawn
1866 // we don't have to use GetUpdateClientRect() here because our client rect
1867 // is the same as total one
1868 wxRect rectUpdate
= GetUpdateRegion().GetBox();
1870 int flagsMenubar
= GetStateFlags();
1874 rect
.height
= GetClientSize().y
;
1877 size_t count
= GetCount();
1878 for ( size_t n
= 0; n
< count
; n
++ )
1880 if ( x
> rectUpdate
.GetRight() )
1882 // all remaining items are to the right of rectUpdate
1887 rect
.width
= GetItemWidth(n
);
1889 if ( x
< rectUpdate
.x
)
1891 // this item is still to the left of rectUpdate
1895 int flags
= flagsMenubar
;
1896 if ( m_current
!= -1 && n
== (size_t)m_current
)
1898 flags
|= wxCONTROL_SELECTED
;
1901 if ( !IsEnabledTop(n
) )
1903 flags
|= wxCONTROL_DISABLED
;
1906 GetRenderer()->DrawMenuBarItem
1910 m_menuInfos
[n
].GetLabel(),
1912 m_menuInfos
[n
].GetAccelIndex()
1917 // ----------------------------------------------------------------------------
1918 // wxMenuBar geometry
1919 // ----------------------------------------------------------------------------
1921 wxRect
wxMenuBar::GetItemRect(size_t pos
) const
1923 wxASSERT_MSG( pos
< GetCount(), _T("invalid menu bar item index") );
1924 wxASSERT_MSG( IsCreated(), _T("can't call this method yet") );
1929 rect
.height
= GetClientSize().y
;
1931 for ( size_t n
= 0; n
< pos
; n
++ )
1933 rect
.x
+= GetItemWidth(n
);
1936 rect
.width
= GetItemWidth(pos
);
1941 wxSize
wxMenuBar::DoGetBestClientSize() const
1944 if ( GetMenuCount() > 0 )
1946 wxClientDC
dc(wxConstCast(this, wxMenuBar
));
1947 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1948 dc
.GetTextExtent(GetLabelTop(0), &size
.x
, &size
.y
);
1950 // adjust for the renderer we use
1951 size
= GetRenderer()->GetMenuBarItemSize(size
);
1953 else // empty menubar
1959 // the width is arbitrary, of course, for horizontal menubar
1965 int wxMenuBar::GetMenuFromPoint(const wxPoint
& pos
) const
1967 if ( pos
.x
< 0 || pos
.y
< 0 || pos
.y
> GetClientSize().y
)
1972 size_t count
= GetCount();
1973 for ( size_t item
= 0; item
< count
; item
++ )
1975 x
+= GetItemWidth(item
);
1983 // to the right of the last menu item
1987 // ----------------------------------------------------------------------------
1988 // wxMenuBar menu operations
1989 // ----------------------------------------------------------------------------
1991 void wxMenuBar::SelectMenu(size_t pos
)
1994 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::SelectMenu"));
2000 void wxMenuBar::DoSelectMenu(size_t pos
)
2002 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in DoSelectMenu") );
2004 int posOld
= m_current
;
2010 // close the previous menu
2011 if ( IsShowingMenu() )
2013 // restore m_shouldShowMenu flag after DismissMenu() which resets
2015 bool old
= m_shouldShowMenu
;
2019 m_shouldShowMenu
= old
;
2022 RefreshItem((size_t)posOld
);
2028 void wxMenuBar::PopupMenu(size_t pos
)
2030 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in PopupCurrentMenu") );
2037 // ----------------------------------------------------------------------------
2038 // wxMenuBar input handing
2039 // ----------------------------------------------------------------------------
2042 Note that wxMenuBar doesn't use wxInputHandler but handles keyboard and
2043 mouse in the same way under all platforms. This is because it doesn't derive
2044 from wxControl (which works with input handlers) but directly from wxWindow.
2046 Also, menu bar input handling is rather simple, so maybe it's not really
2047 worth making it themeable - at least I've decided against doing it now as it
2048 would merging the changes back into trunk more difficult. But it still could
2049 be done later if really needed.
2052 void wxMenuBar::OnKillFocus(wxFocusEvent
& event
)
2054 if ( m_current
!= -1 )
2056 RefreshItem((size_t)m_current
);
2064 void wxMenuBar::OnLeftDown(wxMouseEvent
& event
)
2072 else // we didn't have mouse capture, capture it now
2074 m_current
= GetMenuFromPoint(event
.GetPosition());
2075 if ( m_current
== -1 )
2077 // unfortunately, we can't prevent wxMSW from giving us the focus,
2078 // so we can only give it back
2083 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::OnLeftDown"));
2086 // show it as selected
2087 RefreshItem((size_t)m_current
);
2090 PopupCurrentMenu(FALSE
/* don't select first item - as Windows does */);
2095 void wxMenuBar::OnMouseMove(wxMouseEvent
& event
)
2099 (void)ProcessMouseEvent(event
.GetPosition());
2107 bool wxMenuBar::ProcessMouseEvent(const wxPoint
& pt
)
2109 // a hack to ignore the extra mouse events MSW sends us: this is similar to
2110 // wxUSE_MOUSEEVENT_HACK in wxWin itself but it isn't enough for us here as
2111 // we get the messages from different windows (old and new popup menus for
2114 static wxPoint s_ptLast
;
2115 if ( pt
== s_ptLast
)
2123 int currentNew
= GetMenuFromPoint(pt
);
2124 if ( (currentNew
== -1) || (currentNew
== m_current
) )
2129 // select the new active item
2130 DoSelectMenu(currentNew
);
2132 // show the menu if we know that we should, even if we hadn't been showing
2133 // it before (this may happen if the previous menu was disabled)
2134 if ( m_shouldShowMenu
&& !m_menuShown
)
2136 // open the new menu if the old one we closed had been opened
2137 PopupCurrentMenu(FALSE
/* don't select first item - as Windows does */);
2143 void wxMenuBar::OnKeyDown(wxKeyEvent
& event
)
2145 // ensure that we have a current item - we might not have it if we're
2146 // given the focus with Alt or F10 press (and under GTK+ the menubar
2147 // somehow gets the keyboard events even when it doesn't have focus...)
2148 if ( m_current
== -1 )
2150 if ( !HasCapture() )
2154 else // we do have capture
2156 // we always maintain a valid current item while we're in modal
2157 // state (i.e. have the capture)
2158 wxFAIL_MSG( _T("how did we manage to lose current item?") );
2164 int key
= event
.GetKeyCode();
2166 // first let the menu have it
2167 if ( IsShowingMenu() && m_menuShown
->ProcessKeyDown(key
) )
2172 // cycle through the menu items when left/right arrows are pressed and open
2173 // the menu when up/down one is
2177 // Alt must be processed at wxWindow level too
2182 // remove the selection and give the focus away
2183 if ( m_current
!= -1 )
2185 if ( IsShowingMenu() )
2197 size_t count
= GetCount();
2200 // the item won't change anyhow
2203 //else: otherwise, it will
2205 // remember if we were showing a menu - if we did, we should
2206 // show the new menu after changing the item
2207 bool wasMenuOpened
= IsShowingMenu();
2208 if ( wasMenuOpened
)
2213 // cast is safe as we tested for -1 above
2214 size_t currentNew
= (size_t)m_current
;
2216 if ( key
== WXK_LEFT
)
2218 if ( currentNew
-- == 0 )
2219 currentNew
= count
- 1;
2223 if ( ++currentNew
== count
)
2227 DoSelectMenu(currentNew
);
2229 if ( wasMenuOpened
)
2244 // letters open the corresponding menu
2247 int idxFound
= FindNextItemForAccel(m_current
, key
, &unique
);
2249 if ( idxFound
!= -1 )
2251 if ( IsShowingMenu() )
2256 DoSelectMenu((size_t)idxFound
);
2258 // if the item is not unique, just select it but don't
2259 // activate as the user might have wanted to activate
2262 // also, don't try to open a disabled menu
2263 if ( unique
&& IsEnabledTop((size_t)idxFound
) )
2269 // skip the "event.Skip()" below
2278 // ----------------------------------------------------------------------------
2279 // wxMenuBar accel handling
2280 // ----------------------------------------------------------------------------
2282 int wxMenuBar::FindNextItemForAccel(int idxStart
, int key
, bool *unique
) const
2284 if ( !wxIsalnum(key
) )
2286 // we only support letters/digits as accels
2290 // do we have more than one item with this accel?
2294 // translate everything to lower case before comparing
2295 wxChar chAccel
= wxTolower(key
);
2297 // the index of the item with this accel
2300 // loop through all items searching for the item with this
2301 // accel starting at the item after the current one
2302 int count
= GetCount();
2303 int n
= idxStart
== -1 ? 0 : idxStart
+ 1;
2314 const wxMenuInfo
& info
= m_menuInfos
[n
];
2316 int idxAccel
= info
.GetAccelIndex();
2317 if ( idxAccel
!= -1 &&
2318 wxTolower(info
.GetLabel()[(size_t)idxAccel
])
2321 // ok, found an item with this accel
2322 if ( idxFound
== -1 )
2324 // store it but continue searching as we need to
2325 // know if it's the only item with this accel or if
2329 else // we already had found such item
2334 // no need to continue further, we won't find
2335 // anything we don't already know
2340 // we want to iterate over all items wrapping around if
2348 if ( n
== idxStart
)
2350 // we've seen all items
2360 bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent
& event
)
2363 for ( wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
2365 node
= node
->GetNext(), n
++ )
2367 // accels of the items in the disabled menus shouldn't work
2368 if ( m_menuInfos
[n
].IsEnabled() )
2370 if ( node
->GetData()->ProcessAccelEvent(event
) )
2372 // menu processed it
2382 #endif // wxUSE_ACCEL
2384 // ----------------------------------------------------------------------------
2385 // wxMenuBar menus showing
2386 // ----------------------------------------------------------------------------
2388 void wxMenuBar::PopupCurrentMenu(bool selectFirst
)
2390 wxCHECK_RET( m_current
!= -1, _T("no menu to popup") );
2392 // forgot to call DismissMenu()?
2393 wxASSERT_MSG( !m_menuShown
, _T("shouldn't show two menus at once!") );
2395 // in any case, we should show it - even if we won't
2396 m_shouldShowMenu
= TRUE
;
2398 if ( IsEnabledTop(m_current
) )
2400 // remember the menu we show
2401 m_menuShown
= GetMenu(m_current
);
2403 // we don't show the menu at all if it has no items
2404 if ( !m_menuShown
->IsEmpty() )
2406 // position it correctly: note that we must use screen coords and
2407 // that we pass 0 as width to position the menu exactly below the
2408 // item, not to the right of it
2409 wxRect rectItem
= GetItemRect(m_current
);
2411 m_menuShown
->Popup(ClientToScreen(rectItem
.GetPosition()),
2412 wxSize(0, rectItem
.GetHeight()),
2417 // reset it back as no menu is shown
2421 //else: don't show disabled menu
2424 void wxMenuBar::DismissMenu()
2426 wxCHECK_RET( m_menuShown
, _T("can't dismiss menu if none is shown") );
2428 m_menuShown
->Dismiss();
2432 void wxMenuBar::OnDismissMenu(bool dismissMenuBar
)
2434 m_shouldShowMenu
= FALSE
;
2436 if ( dismissMenuBar
)
2442 void wxMenuBar::OnDismiss()
2446 wxLogTrace(_T("mousecapture"), _T("Releasing mouse from wxMenuBar::OnDismiss"));
2447 GetCapture()->ReleaseMouse();
2450 if ( m_current
!= -1 )
2452 size_t current
= m_current
;
2455 RefreshItem(current
);
2461 void wxMenuBar::GiveAwayFocus()
2463 GetFrame()->SetFocus();
2466 // ----------------------------------------------------------------------------
2467 // popup menu support
2468 // ----------------------------------------------------------------------------
2470 wxEventLoop
*wxWindow::ms_evtLoopPopup
= NULL
;
2472 bool wxWindow::DoPopupMenu(wxMenu
*menu
, int x
, int y
)
2474 wxCHECK_MSG( !ms_evtLoopPopup
, FALSE
,
2475 _T("can't show more than one popup menu at a time") );
2478 // we need to change the cursor before showing the menu as, apparently, no
2479 // cursor changes took place while the mouse is captured
2480 wxCursor cursorOld
= GetCursor();
2481 SetCursor(wxCURSOR_ARROW
);
2485 // flash any delayed log messages before showing the menu, otherwise it
2486 // could be dismissed (because it would lose focus) immediately after being
2488 wxLog::FlushActive();
2490 // some controls update themselves from OnIdle() call - let them do it
2491 wxTheApp
->ProcessIdle();
2493 // if the window hadn't been refreshed yet, the menu can adversely affect
2494 // its next OnPaint() handler execution - i.e. scrolled window refresh
2495 // logic breaks then as it scrolls part of the menu which hadn't been there
2496 // when the update event was generated into view
2500 menu
->SetInvokingWindow(this);
2502 // wxLogDebug( "Name of invoking window %s", menu->GetInvokingWindow()->GetName().c_str() );
2504 menu
->Popup(ClientToScreen(wxPoint(x
, y
)), wxSize(0, 0));
2506 // this is not very useful if the menu was popped up because of the mouse
2507 // click but I think it is nice to do when it appears because of a key
2508 // press (i.e. Windows menu key)
2510 // Windows itself doesn't do it, but IMHO this is nice
2513 // we have to redirect all keyboard input to the menu temporarily
2514 PushEventHandler(new wxMenuKbdRedirector(menu
));
2516 // enter the local modal loop
2517 ms_evtLoopPopup
= new wxEventLoop
;
2518 ms_evtLoopPopup
->Run();
2520 delete ms_evtLoopPopup
;
2521 ms_evtLoopPopup
= NULL
;
2523 // remove the handler
2524 PopEventHandler(TRUE
/* delete it */);
2526 menu
->SetInvokingWindow(NULL
);
2529 SetCursor(cursorOld
);
2535 void wxWindow::DismissPopupMenu()
2537 wxCHECK_RET( ms_evtLoopPopup
, _T("no popup menu shown") );
2539 ms_evtLoopPopup
->Exit();
2542 #endif // wxUSE_MENUS