1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/menu.cpp
3 // Purpose: wxMenuItem, wxMenu and wxMenuBar implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
29 #include "wx/dynarray.h"
30 #include "wx/control.h" // for FindAccelIndex()
32 #include "wx/settings.h"
37 #include "wx/popupwin.h"
38 #include "wx/evtloop.h"
39 #include "wx/dcclient.h"
42 #include "wx/univ/renderer.h"
45 #include "wx/msw/private.h"
48 typedef wxMenuItemList::compatibility_iterator wxMenuItemIter
;
50 // ----------------------------------------------------------------------------
51 // wxMenuInfo contains all extra information about top level menus we need
52 // ----------------------------------------------------------------------------
54 class WXDLLEXPORT wxMenuInfo
58 wxMenuInfo(const wxString
& text
)
66 void SetLabel(const wxString
& text
)
68 // remember the accel char (may be -1 if none)
69 m_indexAccel
= wxControl::FindAccelIndex(text
, &m_label
);
71 // calculate the width later, after the menu bar is created
75 void SetEnabled(bool enabled
= true) { m_isEnabled
= enabled
; }
79 const wxString
& GetLabel() const { return m_label
; }
80 bool IsEnabled() const { return m_isEnabled
; }
81 wxCoord
GetWidth(wxMenuBar
*menubar
) const
85 wxConstCast(this, wxMenuInfo
)->CalcWidth(menubar
);
91 int GetAccelIndex() const { return m_indexAccel
; }
94 void CalcWidth(wxMenuBar
*menubar
)
97 wxClientDC
dc(menubar
);
98 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
99 dc
.GetTextExtent(m_label
, &size
.x
, &size
.y
);
101 // adjust for the renderer we use and store the width
102 m_width
= menubar
->GetRenderer()->GetMenuBarItemSize(size
).x
;
111 #include "wx/arrimpl.cpp"
113 WX_DEFINE_OBJARRAY(wxMenuInfoArray
);
115 // ----------------------------------------------------------------------------
116 // wxPopupMenuWindow: a popup window showing a menu
117 // ----------------------------------------------------------------------------
119 class wxPopupMenuWindow
: public wxPopupTransientWindow
122 wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
);
124 ~wxPopupMenuWindow();
126 // override the base class version to select the first item initially
127 virtual void Popup(wxWindow
*focus
= NULL
);
129 // override the base class version to dismiss any open submenus
130 virtual void Dismiss();
132 // called when a submenu is dismissed
133 void OnSubmenuDismiss(bool dismissParent
);
135 // the default wxMSW wxPopupTransientWindow::OnIdle disables the capture
136 // when the cursor is inside the popup, which dsables the menu tracking
137 // so override it to do nothing
139 void OnIdle(wxIdleEvent
& WXUNUSED(event
)) { }
142 // get the currently selected item (may be NULL)
143 wxMenuItem
*GetCurrentItem() const
145 return m_nodeCurrent
? m_nodeCurrent
->GetData() : NULL
;
148 // find the menu item at given position
149 wxMenuItemIter
GetMenuItemFromPoint(const wxPoint
& pt
) const;
151 // refresh the given item
152 void RefreshItem(wxMenuItem
*item
);
154 // preselect the first item
155 void SelectFirst() { SetCurrentItem(m_menu
->GetMenuItems().GetFirst()); }
157 // process the key event, return true if done
158 bool ProcessKeyDown(int key
);
160 // process mouse move event
161 void ProcessMouseMove(const wxPoint
& pt
);
163 // don't dismiss the popup window if the parent menu was clicked
164 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
167 // how did we perform this operation?
174 // notify the menu when the window disappears from screen
175 virtual void OnDismiss();
177 // draw the menu inside this window
178 virtual void DoDraw(wxControlRenderer
*renderer
);
181 void OnLeftUp(wxMouseEvent
& event
);
182 void OnMouseMove(wxMouseEvent
& event
);
183 void OnMouseLeave(wxMouseEvent
& event
);
184 void OnKeyDown(wxKeyEvent
& event
);
186 // reset the current item and node
189 // set the current node and item without refreshing anything
190 void SetCurrentItem(wxMenuItemIter node
);
192 // change the current item refreshing the old and new items
193 void ChangeCurrent(wxMenuItemIter node
);
195 // activate item, i.e. call either ClickItem() or OpenSubmenu() depending
196 // on what it is, return true if something was done (i.e. it's not a
198 bool ActivateItem(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
200 // send the event about the item click
201 void ClickItem(wxMenuItem
*item
);
203 // show the submenu for this item
204 void OpenSubmenu(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
206 // can this tiem be opened?
207 bool CanOpen(wxMenuItem
*item
)
209 return item
&& item
->IsEnabled() && item
->IsSubMenu();
212 // dismiss the menu and all parent menus too
213 void DismissAndNotify();
215 // react to dimissing this menu and also dismiss the parent if
217 void HandleDismiss(bool dismissParent
);
219 // do we have an open submenu?
220 bool HasOpenSubmenu() const { return m_hasOpenSubMenu
; }
222 // get previous node after the current one
223 wxMenuItemIter
GetPrevNode() const;
225 // get previous node before the given one, wrapping if it's the first one
226 wxMenuItemIter
GetPrevNode(wxMenuItemIter node
) const;
228 // get next node after the current one
229 wxMenuItemIter
GetNextNode() const;
231 // get next node after the given one, wrapping if it's the last one
232 wxMenuItemIter
GetNextNode(wxMenuItemIter node
) const;
238 // the menu node corresponding to the current item
239 wxMenuItemIter m_nodeCurrent
;
241 // do we currently have an opened submenu?
242 bool m_hasOpenSubMenu
;
244 DECLARE_EVENT_TABLE()
247 // ----------------------------------------------------------------------------
248 // wxMenuKbdRedirector: an event handler which redirects kbd input to wxMenu
249 // ----------------------------------------------------------------------------
251 class wxMenuKbdRedirector
: public wxEvtHandler
254 wxMenuKbdRedirector(wxMenu
*menu
) { m_menu
= menu
; }
256 virtual bool ProcessEvent(wxEvent
& event
)
258 if ( event
.GetEventType() == wxEVT_KEY_DOWN
)
260 return m_menu
->ProcessKeyDown(((wxKeyEvent
&)event
).GetKeyCode());
266 return wxEvtHandler::ProcessEvent(event
);
274 // ----------------------------------------------------------------------------
276 // ----------------------------------------------------------------------------
278 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
279 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
280 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
282 BEGIN_EVENT_TABLE(wxPopupMenuWindow
, wxPopupTransientWindow
)
283 EVT_KEY_DOWN(wxPopupMenuWindow::OnKeyDown
)
285 EVT_LEFT_UP(wxPopupMenuWindow::OnLeftUp
)
286 EVT_MOTION(wxPopupMenuWindow::OnMouseMove
)
287 EVT_LEAVE_WINDOW(wxPopupMenuWindow::OnMouseLeave
)
289 EVT_IDLE(wxPopupMenuWindow::OnIdle
)
293 BEGIN_EVENT_TABLE(wxMenuBar
, wxMenuBarBase
)
294 EVT_KILL_FOCUS(wxMenuBar::OnKillFocus
)
296 EVT_KEY_DOWN(wxMenuBar::OnKeyDown
)
298 EVT_LEFT_DOWN(wxMenuBar::OnLeftDown
)
299 EVT_MOTION(wxMenuBar::OnMouseMove
)
302 // ============================================================================
304 // ============================================================================
306 // ----------------------------------------------------------------------------
308 // ----------------------------------------------------------------------------
310 wxPopupMenuWindow::wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
)
313 m_hasOpenSubMenu
= false;
317 (void)Create(parent
, wxBORDER_RAISED
);
319 SetCursor(wxCURSOR_ARROW
);
322 wxPopupMenuWindow::~wxPopupMenuWindow()
324 // When m_popupMenu in wxMenu is deleted because it
325 // is a child of an old menu bar being deleted (note: it does
326 // not get destroyed by the wxMenu destructor, but
327 // by DestroyChildren()), m_popupMenu should be reset to NULL.
329 m_menu
->m_popupMenu
= NULL
;
332 // ----------------------------------------------------------------------------
333 // wxPopupMenuWindow current item/node handling
334 // ----------------------------------------------------------------------------
336 void wxPopupMenuWindow::ResetCurrent()
338 SetCurrentItem(wxMenuItemIter());
341 void wxPopupMenuWindow::SetCurrentItem(wxMenuItemIter node
)
343 m_nodeCurrent
= node
;
346 void wxPopupMenuWindow::ChangeCurrent(wxMenuItemIter node
)
348 if ( node
!= m_nodeCurrent
)
350 wxMenuItemIter nodeOldCurrent
= m_nodeCurrent
;
352 m_nodeCurrent
= node
;
354 if ( nodeOldCurrent
)
356 wxMenuItem
*item
= nodeOldCurrent
->GetData();
357 wxCHECK_RET( item
, _T("no current item?") );
359 // if it was the currently opened menu, close it
360 if ( item
->IsSubMenu() && item
->GetSubMenu()->IsShown() )
362 item
->GetSubMenu()->Dismiss();
363 OnSubmenuDismiss( false );
370 RefreshItem(m_nodeCurrent
->GetData());
374 wxMenuItemIter
wxPopupMenuWindow::GetPrevNode() const
376 // return the last node if there had been no previously selected one
377 return m_nodeCurrent
? GetPrevNode(m_nodeCurrent
)
378 : wxMenuItemIter(m_menu
->GetMenuItems().GetLast());
382 wxPopupMenuWindow::GetPrevNode(wxMenuItemIter node
) const
386 node
= node
->GetPrevious();
389 node
= m_menu
->GetMenuItems().GetLast();
392 //else: the menu is empty
397 wxMenuItemIter
wxPopupMenuWindow::GetNextNode() const
399 // return the first node if there had been no previously selected one
400 return m_nodeCurrent
? GetNextNode(m_nodeCurrent
)
401 : wxMenuItemIter(m_menu
->GetMenuItems().GetFirst());
405 wxPopupMenuWindow::GetNextNode(wxMenuItemIter node
) const
409 node
= node
->GetNext();
412 node
= m_menu
->GetMenuItems().GetFirst();
415 //else: the menu is empty
420 // ----------------------------------------------------------------------------
421 // wxPopupMenuWindow popup/dismiss
422 // ----------------------------------------------------------------------------
424 void wxPopupMenuWindow::Popup(wxWindow
*focus
)
426 // check that the current item had been properly reset before
427 wxASSERT_MSG( !m_nodeCurrent
||
428 m_nodeCurrent
== m_menu
->GetMenuItems().GetFirst(),
429 _T("menu current item preselected incorrectly") );
431 wxPopupTransientWindow::Popup(focus
);
433 // the base class no-longer captures the mouse automatically when Popup
434 // is called, so do it here to allow the menu tracking to work
439 // ensure that this window is really on top of everything: without using
440 // SetWindowPos() it can be covered by its parent menu which is not
441 // really what we want
442 wxMenu
*menuParent
= m_menu
->GetParent();
445 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
447 // if we're shown, the parent menu must be also shown
448 wxCHECK_RET( win
, _T("parent menu is not shown?") );
450 if ( !::SetWindowPos(GetHwndOf(win
), GetHwnd(),
452 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOREDRAW
) )
454 wxLogLastError(_T("SetWindowPos(HWND_TOP)"));
462 void wxPopupMenuWindow::Dismiss()
464 if ( HasOpenSubmenu() )
466 wxMenuItem
*item
= GetCurrentItem();
467 wxCHECK_RET( item
&& item
->IsSubMenu(), _T("where is our open submenu?") );
469 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
470 wxCHECK_RET( win
, _T("opened submenu is not opened?") );
473 OnSubmenuDismiss( false );
476 wxPopupTransientWindow::Dismiss();
481 void wxPopupMenuWindow::OnDismiss()
483 // when we are dismissed because the user clicked elsewhere or we lost
484 // focus in any other way, hide the parent menu as well
488 void wxPopupMenuWindow::OnSubmenuDismiss(bool WXUNUSED(dismissParent
))
490 m_hasOpenSubMenu
= false;
493 void wxPopupMenuWindow::HandleDismiss(bool dismissParent
)
495 m_menu
->OnDismiss(dismissParent
);
498 void wxPopupMenuWindow::DismissAndNotify()
504 // ----------------------------------------------------------------------------
505 // wxPopupMenuWindow geometry
506 // ----------------------------------------------------------------------------
509 wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint
& pt
) const
511 // we only use the y coord normally, but still check x in case the point is
512 // outside the window completely
513 if ( wxWindow::HitTest(pt
) == wxHT_WINDOW_INSIDE
)
516 for ( wxMenuItemIter node
= m_menu
->GetMenuItems().GetFirst();
518 node
= node
->GetNext() )
520 wxMenuItem
*item
= node
->GetData();
521 y
+= item
->GetHeight();
530 return wxMenuItemIter();
533 // ----------------------------------------------------------------------------
534 // wxPopupMenuWindow drawing
535 // ----------------------------------------------------------------------------
537 void wxPopupMenuWindow::RefreshItem(wxMenuItem
*item
)
539 wxCHECK_RET( item
, _T("can't refresh NULL item") );
541 wxASSERT_MSG( IsShown(), _T("can't refresh menu which is not shown") );
543 // FIXME: -1 here because of SetLogicalOrigin(1, 1) in DoDraw()
544 RefreshRect(wxRect(0, item
->GetPosition() - 1,
545 m_menu
->GetGeometryInfo().GetSize().x
, item
->GetHeight()));
548 void wxPopupMenuWindow::DoDraw(wxControlRenderer
*renderer
)
550 // no clipping so far - do we need it? I don't think so as the menu is
551 // never partially covered as it is always on top of everything
553 wxDC
& dc
= renderer
->GetDC();
554 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
556 // FIXME: this should be done in the renderer, however when it is fixed
557 // wxPopupMenuWindow::RefreshItem() should be changed too!
558 dc
.SetLogicalOrigin(1, 1);
560 wxRenderer
*rend
= renderer
->GetRenderer();
563 const wxMenuGeometryInfo
& gi
= m_menu
->GetGeometryInfo();
564 for ( wxMenuItemIter node
= m_menu
->GetMenuItems().GetFirst();
566 node
= node
->GetNext() )
568 wxMenuItem
*item
= node
->GetData();
570 if ( item
->IsSeparator() )
572 rend
->DrawMenuSeparator(dc
, y
, gi
);
574 else // not a separator
577 if ( item
->IsCheckable() )
579 flags
|= wxCONTROL_CHECKABLE
;
581 if ( item
->IsChecked() )
583 flags
|= wxCONTROL_CHECKED
;
587 if ( !item
->IsEnabled() )
588 flags
|= wxCONTROL_DISABLED
;
590 if ( item
->IsSubMenu() )
591 flags
|= wxCONTROL_ISSUBMENU
;
593 if ( item
== GetCurrentItem() )
594 flags
|= wxCONTROL_SELECTED
;
598 if ( !item
->IsEnabled() )
600 bmp
= item
->GetDisabledBitmap();
605 // strangely enough, for unchecked item we use the
606 // "checked" bitmap because this is the default one - this
607 // explains this strange boolean expression
608 bmp
= item
->GetBitmap(!item
->IsCheckable() || item
->IsChecked());
617 item
->GetAccelString(),
620 item
->GetAccelIndex()
624 y
+= item
->GetHeight();
628 // ----------------------------------------------------------------------------
629 // wxPopupMenuWindow actions
630 // ----------------------------------------------------------------------------
632 void wxPopupMenuWindow::ClickItem(wxMenuItem
*item
)
634 wxCHECK_RET( item
, _T("can't click NULL item") );
636 wxASSERT_MSG( !item
->IsSeparator() && !item
->IsSubMenu(),
637 _T("can't click this item") );
639 wxMenu
* menu
= m_menu
;
644 menu
->ClickItem(item
);
647 void wxPopupMenuWindow::OpenSubmenu(wxMenuItem
*item
, InputMethod how
)
649 wxCHECK_RET( item
, _T("can't open NULL submenu") );
651 wxMenu
*submenu
= item
->GetSubMenu();
652 wxCHECK_RET( submenu
, _T("can only open submenus!") );
654 // FIXME: should take into account the border width
655 submenu
->Popup(ClientToScreen(wxPoint(0, item
->GetPosition())),
656 wxSize(m_menu
->GetGeometryInfo().GetSize().x
, 0),
657 how
== WithKeyboard
/* preselect first item then */);
659 m_hasOpenSubMenu
= true;
662 bool wxPopupMenuWindow::ActivateItem(wxMenuItem
*item
, InputMethod how
)
664 // don't activate disabled items
665 if ( !item
|| !item
->IsEnabled() )
670 // normal menu items generate commands, submenus can be opened and
671 // the separators don't do anything
672 if ( item
->IsSubMenu() )
674 OpenSubmenu(item
, how
);
676 else if ( !item
->IsSeparator() )
680 else // separator, can't activate
688 // ----------------------------------------------------------------------------
689 // wxPopupMenuWindow input handling
690 // ----------------------------------------------------------------------------
692 bool wxPopupMenuWindow::ProcessLeftDown(wxMouseEvent
& event
)
694 // wxPopupWindowHandler dismisses the window when the mouse is clicked
695 // outside it which is usually just fine, but there is one case when we
696 // don't want to do it: if the mouse was clicked on the parent submenu item
697 // which opens this menu, so check for it
699 wxPoint pos
= event
.GetPosition();
700 if ( HitTest(pos
.x
, pos
.y
) == wxHT_WINDOW_OUTSIDE
)
702 wxMenu
*menu
= m_menu
->GetParent();
705 wxPopupMenuWindow
*win
= menu
->m_popupMenu
;
707 wxCHECK_MSG( win
, false, _T("parent menu not shown?") );
709 pos
= ClientToScreen(pos
);
710 if ( win
->GetMenuItemFromPoint(win
->ScreenToClient(pos
)) )
715 //else: it is outside the parent menu as well, do dismiss this one
722 void wxPopupMenuWindow::OnLeftUp(wxMouseEvent
& event
)
724 wxMenuItemIter node
= GetMenuItemFromPoint(event
.GetPosition());
727 ActivateItem(node
->GetData(), WithMouse
);
731 void wxPopupMenuWindow::OnMouseMove(wxMouseEvent
& event
)
733 const wxPoint pt
= event
.GetPosition();
735 // we need to ignore extra mouse events: example when this happens is when
736 // the mouse is on the menu and we open a submenu from keyboard - Windows
737 // then sends us a dummy mouse move event, we (correctly) determine that it
738 // happens in the parent menu and so immediately close the just opened
741 static wxPoint s_ptLast
;
742 wxPoint ptCur
= ClientToScreen(pt
);
743 if ( ptCur
== s_ptLast
)
751 ProcessMouseMove(pt
);
756 void wxPopupMenuWindow::ProcessMouseMove(const wxPoint
& pt
)
758 wxMenuItemIter node
= GetMenuItemFromPoint(pt
);
760 // don't reset current to NULL here, we only do it when the mouse leaves
761 // the window (see below)
764 if ( node
!= m_nodeCurrent
)
768 wxMenuItem
*item
= GetCurrentItem();
771 OpenSubmenu(item
, WithMouse
);
774 //else: same item, nothing to do
776 else // not on an item
778 // the last open submenu forwards the mouse move messages to its
779 // parent, so if the mouse moves to another item of the parent menu,
780 // this menu is closed and this other item is selected - in the similar
781 // manner, the top menu forwards the mouse moves to the menubar which
782 // allows to select another top level menu by just moving the mouse
784 // we need to translate our client coords to the client coords of the
785 // window we forward this event to
786 wxPoint ptScreen
= ClientToScreen(pt
);
788 // if the mouse is outside this menu, let the parent one to
790 wxMenu
*menuParent
= m_menu
->GetParent();
793 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
795 // if we're shown, the parent menu must be also shown
796 wxCHECK_RET( win
, _T("parent menu is not shown?") );
798 win
->ProcessMouseMove(win
->ScreenToClient(ptScreen
));
800 else // no parent menu
802 wxMenuBar
*menubar
= m_menu
->GetMenuBar();
805 if ( menubar
->ProcessMouseEvent(
806 menubar
->ScreenToClient(ptScreen
)) )
808 // menubar has closed this menu and opened another one, probably
813 //else: top level popup menu, no other processing to do
817 void wxPopupMenuWindow::OnMouseLeave(wxMouseEvent
& event
)
819 // due to the artefact of mouse events generation under MSW, we actually
820 // may get the mouse leave event after the menu had been already dismissed
821 // and calling ChangeCurrent() would then assert, so don't do it
824 // we shouldn't change the current them if our submenu is opened and
825 // mouse moved there, in this case the submenu is responsable for
828 if ( HasOpenSubmenu() )
830 wxMenuItem
*item
= GetCurrentItem();
831 wxCHECK_RET( CanOpen(item
), _T("where is our open submenu?") );
833 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
834 wxCHECK_RET( win
, _T("submenu is opened but not shown?") );
836 // only handle this event if the mouse is not inside the submenu
837 wxPoint pt
= ClientToScreen(event
.GetPosition());
839 win
->HitTest(win
->ScreenToClient(pt
)) == wxHT_WINDOW_OUTSIDE
;
843 // this menu is the last opened
849 ChangeCurrent(wxMenuItemIter());
856 void wxPopupMenuWindow::OnKeyDown(wxKeyEvent
& event
)
858 wxMenuBar
*menubar
= m_menu
->GetMenuBar();
862 menubar
->ProcessEvent(event
);
864 else if ( !ProcessKeyDown(event
.GetKeyCode()) )
870 bool wxPopupMenuWindow::ProcessKeyDown(int key
)
872 wxMenuItem
*item
= GetCurrentItem();
874 // first let the opened submenu to have it (no test for IsEnabled() here,
875 // the keys navigate even in a disabled submenu if we had somehow managed
876 // to open it inspit of this)
877 if ( HasOpenSubmenu() )
879 wxCHECK_MSG( CanOpen(item
), false,
880 _T("has open submenu but another item selected?") );
882 if ( item
->GetSubMenu()->ProcessKeyDown(key
) )
886 bool processed
= true;
888 // handle the up/down arrows, home, end, esc and return here, pass the
889 // left/right arrows to the menu bar except when the right arrow can be
890 // used to open a submenu
894 // if we're not a top level menu, close us, else leave this to the
896 if ( !m_menu
->GetParent() )
905 // close just this menu
907 HandleDismiss(false);
911 processed
= ActivateItem(item
);
915 ChangeCurrent(m_menu
->GetMenuItems().GetFirst());
919 ChangeCurrent(m_menu
->GetMenuItems().GetLast());
925 bool up
= key
== WXK_UP
;
927 wxMenuItemIter nodeStart
= up
? GetPrevNode() : GetNextNode(),
929 while ( node
&& node
->GetData()->IsSeparator() )
931 node
= up
? GetPrevNode(node
) : GetNextNode(node
);
933 if ( node
== nodeStart
)
935 // nothing but separators and disabled items in this
937 node
= wxMenuItemIter();
953 // don't try to reopen an already opened menu
954 if ( !HasOpenSubmenu() && CanOpen(item
) )
965 // look for the menu item starting with this letter
966 if ( wxIsalnum((wxChar
)key
) )
968 // we want to start from the item after this one because
969 // if we're already on the item with the given accel we want to
970 // go to the next one, not to stay in place
971 wxMenuItemIter nodeStart
= GetNextNode();
973 // do we have more than one item with this accel?
974 bool notUnique
= false;
976 // translate everything to lower case before comparing
977 wxChar chAccel
= (wxChar
)wxTolower(key
);
979 // loop through all items searching for the item with this
981 wxMenuItemIter nodeFound
,
985 item
= node
->GetData();
987 int idxAccel
= item
->GetAccelIndex();
988 if ( idxAccel
!= -1 &&
989 wxTolower(item
->GetLabel()[(size_t)idxAccel
])
992 // ok, found an item with this accel
995 // store it but continue searching as we need to
996 // know if it's the only item with this accel or if
1000 else // we already had found such item
1004 // no need to continue further, we won't find
1005 // anything we don't already know
1010 // we want to iterate over all items wrapping around if
1012 node
= GetNextNode(node
);
1013 if ( node
== nodeStart
)
1015 // we've seen all nodes
1022 item
= nodeFound
->GetData();
1024 // go to this item anyhow
1025 ChangeCurrent(nodeFound
);
1027 if ( !notUnique
&& item
->IsEnabled() )
1029 // unique item with this accel - activate it
1030 processed
= ActivateItem(item
);
1032 //else: just select it but don't activate as the user might
1033 // have wanted to activate another item
1035 // skip "processed = false" below
1046 // ----------------------------------------------------------------------------
1048 // ----------------------------------------------------------------------------
1056 m_startRadioGroup
= -1;
1065 // ----------------------------------------------------------------------------
1066 // wxMenu and wxMenuGeometryInfo
1067 // ----------------------------------------------------------------------------
1069 wxMenuGeometryInfo::~wxMenuGeometryInfo()
1073 const wxMenuGeometryInfo
& wxMenu::GetGeometryInfo() const
1079 wxConstCast(this, wxMenu
)->m_geometry
=
1080 m_popupMenu
->GetRenderer()->GetMenuGeometry(m_popupMenu
, *this);
1084 wxFAIL_MSG( _T("can't get geometry without window") );
1091 void wxMenu::InvalidateGeometryInfo()
1100 // ----------------------------------------------------------------------------
1101 // wxMenu adding/removing items
1102 // ----------------------------------------------------------------------------
1104 void wxMenu::OnItemAdded(wxMenuItem
*item
)
1106 InvalidateGeometryInfo();
1110 #endif // wxUSE_ACCEL
1112 // the submenus of a popup menu should have the same invoking window as it
1114 if ( m_invokingWindow
&& item
->IsSubMenu() )
1116 item
->GetSubMenu()->SetInvokingWindow(m_invokingWindow
);
1120 void wxMenu::EndRadioGroup()
1122 // we're not inside a radio group any longer
1123 m_startRadioGroup
= -1;
1126 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
1128 if ( item
->GetKind() == wxITEM_RADIO
)
1130 int count
= GetMenuItemCount();
1132 if ( m_startRadioGroup
== -1 )
1134 // start a new radio group
1135 m_startRadioGroup
= count
;
1137 // for now it has just one element
1138 item
->SetAsRadioGroupStart();
1139 item
->SetRadioGroupEnd(m_startRadioGroup
);
1141 else // extend the current radio group
1143 // we need to update its end item
1144 item
->SetRadioGroupStart(m_startRadioGroup
);
1145 wxMenuItemIter node
= GetMenuItems().Item(m_startRadioGroup
);
1149 node
->GetData()->SetRadioGroupEnd(count
);
1153 wxFAIL_MSG( _T("where is the radio group start item?") );
1157 else // not a radio item
1162 if ( !wxMenuBase::DoAppend(item
) )
1170 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1172 if ( !wxMenuBase::DoInsert(pos
, item
) )
1180 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1182 wxMenuItem
*itemOld
= wxMenuBase::DoRemove(item
);
1186 InvalidateGeometryInfo();
1189 RemoveAccelFor(item
);
1190 #endif // wxUSE_ACCEL
1196 // ----------------------------------------------------------------------------
1197 // wxMenu attaching/detaching
1198 // ----------------------------------------------------------------------------
1200 void wxMenu::Attach(wxMenuBarBase
*menubar
)
1202 wxMenuBase::Attach(menubar
);
1204 wxCHECK_RET( m_menuBar
, _T("menubar can't be NULL after attaching") );
1206 // unfortunately, we can't use m_menuBar->GetEventHandler() here because,
1207 // if the menubar is currently showing a menu, its event handler is a
1208 // temporary one installed by wxPopupWindow and so will disappear soon any
1209 // any attempts to use it from the newly attached menu would result in a
1212 // so we use the menubar itself, even if it's a pity as it means we can't
1213 // redirect all menu events by changing the menubar handler (FIXME)
1214 SetNextHandler(m_menuBar
);
1217 void wxMenu::Detach()
1219 wxMenuBase::Detach();
1222 // ----------------------------------------------------------------------------
1223 // wxMenu misc functions
1224 // ----------------------------------------------------------------------------
1226 wxWindow
*wxMenu::GetRootWindow() const
1230 // simple case - a normal menu attached to the menubar
1231 return GetMenuBar();
1234 // we're a popup menu but the trouble is that only the top level popup menu
1235 // has a pointer to the invoking window, so we must walk up the menu chain
1237 wxWindow
*win
= GetInvokingWindow();
1240 // we already have it
1244 wxMenu
*menu
= GetParent();
1247 // We are a submenu of a menu of a menubar
1248 if (menu
->GetMenuBar())
1249 return menu
->GetMenuBar();
1251 win
= menu
->GetInvokingWindow();
1255 menu
= menu
->GetParent();
1258 // we're probably going to crash in the caller anyhow, but try to detect
1259 // this error as soon as possible
1260 wxASSERT_MSG( win
, _T("menu without any associated window?") );
1262 // also remember it in this menu so that we don't have to search for it the
1264 wxConstCast(this, wxMenu
)->m_invokingWindow
= win
;
1269 wxRenderer
*wxMenu::GetRenderer() const
1271 // we're going to crash without renderer!
1272 wxCHECK_MSG( m_popupMenu
, NULL
, _T("neither popup nor menubar menu?") );
1274 return m_popupMenu
->GetRenderer();
1277 void wxMenu::RefreshItem(wxMenuItem
*item
)
1279 // the item geometry changed, so our might have changed as well
1280 InvalidateGeometryInfo();
1284 // this would be a bug in IsShown()
1285 wxCHECK_RET( m_popupMenu
, _T("must have popup window if shown!") );
1287 // recalc geometry to update the item height and such
1288 (void)GetGeometryInfo();
1290 m_popupMenu
->RefreshItem(item
);
1294 // ----------------------------------------------------------------------------
1295 // wxMenu showing and hiding
1296 // ----------------------------------------------------------------------------
1298 bool wxMenu::IsShown() const
1300 return m_popupMenu
&& m_popupMenu
->IsShown();
1303 void wxMenu::OnDismiss(bool dismissParent
)
1307 // always notify the parent about submenu disappearance
1308 wxPopupMenuWindow
*win
= m_menuParent
->m_popupMenu
;
1311 win
->OnSubmenuDismiss( true );
1315 wxFAIL_MSG( _T("parent menu not shown?") );
1318 // and if we dismiss everything, propagate to parent
1319 if ( dismissParent
)
1321 // dismissParent is recursive
1322 m_menuParent
->Dismiss();
1323 m_menuParent
->OnDismiss(true);
1326 else // no parent menu
1328 // notify the menu bar if we're a top level menu
1331 m_menuBar
->OnDismissMenu(dismissParent
);
1335 wxCHECK_RET( m_invokingWindow
, _T("what kind of menu is this?") );
1337 m_invokingWindow
->DismissPopupMenu();
1339 // Why reset it here? We need it for sending the event to...
1340 // SetInvokingWindow(NULL);
1345 void wxMenu::Popup(const wxPoint
& pos
, const wxSize
& size
, bool selectFirst
)
1347 // create the popup window if not done yet
1350 m_popupMenu
= new wxPopupMenuWindow(GetRootWindow(), this);
1353 // select the first item unless disabled
1356 m_popupMenu
->SelectFirst();
1359 // the geometry might have changed since the last time we were shown, so
1361 m_popupMenu
->SetClientSize(GetGeometryInfo().GetSize());
1363 // position it as specified
1364 m_popupMenu
->Position(pos
, size
);
1366 // the menu can't have the focus itself (it is a Windows limitation), so
1367 // always keep the focus at the originating window
1368 wxWindow
*focus
= GetRootWindow();
1370 wxASSERT_MSG( focus
, _T("no window to keep focus on?") );
1373 m_popupMenu
->Popup(focus
);
1376 void wxMenu::Dismiss()
1378 wxCHECK_RET( IsShown(), _T("can't dismiss hidden menu") );
1380 m_popupMenu
->Dismiss();
1383 // ----------------------------------------------------------------------------
1384 // wxMenu event processing
1385 // ----------------------------------------------------------------------------
1387 bool wxMenu::ProcessKeyDown(int key
)
1389 wxCHECK_MSG( m_popupMenu
, false,
1390 _T("can't process key events if not shown") );
1392 return m_popupMenu
->ProcessKeyDown(key
);
1395 bool wxMenu::ClickItem(wxMenuItem
*item
)
1398 if ( item
->IsCheckable() )
1400 // update the item state
1401 isChecked
= !item
->IsChecked();
1403 item
->Check(isChecked
!= 0);
1411 return SendEvent(item
->GetId(), isChecked
);
1414 // ----------------------------------------------------------------------------
1415 // wxMenu accel support
1416 // ----------------------------------------------------------------------------
1420 bool wxMenu::ProcessAccelEvent(const wxKeyEvent
& event
)
1422 // do we have an item for this accel?
1423 wxMenuItem
*item
= m_accelTable
.GetMenuItem(event
);
1424 if ( item
&& item
->IsEnabled() )
1426 return ClickItem(item
);
1430 for ( wxMenuItemIter node
= GetMenuItems().GetFirst();
1432 node
= node
->GetNext() )
1434 const wxMenuItem
*item
= node
->GetData();
1435 if ( item
->IsSubMenu() && item
->IsEnabled() )
1438 if ( item
->GetSubMenu()->ProcessAccelEvent(event
) )
1448 void wxMenu::AddAccelFor(wxMenuItem
*item
)
1450 wxAcceleratorEntry
*accel
= item
->GetAccel();
1453 accel
->SetMenuItem(item
);
1455 m_accelTable
.Add(*accel
);
1461 void wxMenu::RemoveAccelFor(wxMenuItem
*item
)
1463 wxAcceleratorEntry
*accel
= item
->GetAccel();
1466 m_accelTable
.Remove(*accel
);
1472 #endif // wxUSE_ACCEL
1474 // ----------------------------------------------------------------------------
1475 // wxMenuItem construction
1476 // ----------------------------------------------------------------------------
1478 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
1480 const wxString
& text
,
1481 const wxString
& help
,
1484 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
1487 m_height
= wxDefaultCoord
;
1489 m_radioGroup
.start
= -1;
1490 m_isRadioGroupStart
= false;
1492 m_bmpDisabled
= wxNullBitmap
;
1497 wxMenuItem::~wxMenuItem()
1501 // ----------------------------------------------------------------------------
1502 // wxMenuItemBase methods implemented here
1503 // ----------------------------------------------------------------------------
1506 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
1508 const wxString
& name
,
1509 const wxString
& help
,
1513 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
1517 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
1519 return wxStripMenuCodes(text
);
1522 // ----------------------------------------------------------------------------
1523 // wxMenuItem operations
1524 // ----------------------------------------------------------------------------
1526 void wxMenuItem::NotifyMenu()
1528 m_parentMenu
->RefreshItem(this);
1531 void wxMenuItem::UpdateAccelInfo()
1533 m_indexAccel
= wxControl::FindAccelIndex(m_text
);
1535 // will be empty if the text contains no TABs - ok
1536 m_strAccel
= m_text
.AfterFirst(_T('\t'));
1539 void wxMenuItem::SetText(const wxString
& text
)
1541 if ( text
!= m_text
)
1543 // first call the base class version to change m_text
1544 wxMenuItemBase::SetText(text
);
1552 void wxMenuItem::SetCheckable(bool checkable
)
1554 if ( checkable
!= IsCheckable() )
1556 wxMenuItemBase::SetCheckable(checkable
);
1562 void wxMenuItem::SetBitmaps(const wxBitmap
& bmpChecked
,
1563 const wxBitmap
& bmpUnchecked
)
1565 m_bmpChecked
= bmpChecked
;
1566 m_bmpUnchecked
= bmpUnchecked
;
1571 void wxMenuItem::Enable(bool enable
)
1573 if ( enable
!= m_isEnabled
)
1575 wxMenuItemBase::Enable(enable
);
1581 void wxMenuItem::Check(bool check
)
1583 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
1585 if ( m_isChecked
== check
)
1588 if ( GetKind() == wxITEM_RADIO
)
1590 // it doesn't make sense to uncheck a radio item - what would this do?
1594 // get the index of this item in the menu
1595 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
1596 int pos
= items
.IndexOf(this);
1597 wxCHECK_RET( pos
!= wxNOT_FOUND
,
1598 _T("menuitem not found in the menu items list?") );
1600 // get the radio group range
1604 if ( m_isRadioGroupStart
)
1606 // we already have all information we need
1608 end
= m_radioGroup
.end
;
1610 else // next radio group item
1612 // get the radio group end from the start item
1613 start
= m_radioGroup
.start
;
1614 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
1617 // also uncheck all the other items in this radio group
1618 wxMenuItemIter node
= items
.Item(start
);
1619 for ( int n
= start
; n
<= end
&& node
; n
++ )
1623 node
->GetData()->m_isChecked
= false;
1625 node
= node
->GetNext();
1629 wxMenuItemBase::Check(check
);
1634 // radio group stuff
1635 // -----------------
1637 void wxMenuItem::SetAsRadioGroupStart()
1639 m_isRadioGroupStart
= true;
1642 void wxMenuItem::SetRadioGroupStart(int start
)
1644 wxASSERT_MSG( !m_isRadioGroupStart
,
1645 _T("should only be called for the next radio items") );
1647 m_radioGroup
.start
= start
;
1650 void wxMenuItem::SetRadioGroupEnd(int end
)
1652 wxASSERT_MSG( m_isRadioGroupStart
,
1653 _T("should only be called for the first radio item") );
1655 m_radioGroup
.end
= end
;
1658 // ----------------------------------------------------------------------------
1659 // wxMenuBar creation
1660 // ----------------------------------------------------------------------------
1662 void wxMenuBar::Init()
1670 m_shouldShowMenu
= false;
1673 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
1677 for (size_t i
= 0; i
< n
; ++i
)
1678 Append(menus
[i
], titles
[i
]);
1681 void wxMenuBar::Attach(wxFrame
*frame
)
1683 // maybe you really wanted to call Detach()?
1684 wxCHECK_RET( frame
, _T("wxMenuBar::Attach(NULL) called") );
1686 wxMenuBarBase::Attach(frame
);
1690 // reparent if necessary
1691 if ( m_frameLast
!= frame
)
1696 // show it back - was hidden by Detach()
1699 else // not created yet, do it now
1701 // we have no way to return the error from here anyhow :-(
1702 (void)Create(frame
, wxID_ANY
);
1704 SetCursor(wxCURSOR_ARROW
);
1706 SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT
));
1708 // calculate and set our height (it won't be changed any more)
1709 SetSize(wxDefaultCoord
, GetBestSize().y
);
1712 // remember the last frame which had us to avoid unnecessarily reparenting
1714 m_frameLast
= frame
;
1717 void wxMenuBar::Detach()
1719 // don't delete the window because we may be reattached later, just hide it
1725 wxMenuBarBase::Detach();
1728 wxMenuBar::~wxMenuBar()
1732 // ----------------------------------------------------------------------------
1733 // wxMenuBar adding/removing items
1734 // ----------------------------------------------------------------------------
1736 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1738 return Insert(GetCount(), menu
, title
);
1741 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1743 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1746 wxMenuInfo
*info
= new wxMenuInfo(title
);
1747 m_menuInfos
.Insert(info
, pos
);
1749 RefreshAllItemsAfter(pos
);
1754 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1756 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1760 wxMenuInfo
& info
= m_menuInfos
[pos
];
1762 info
.SetLabel(title
);
1764 // even if the old menu was disabled, the new one is not any more
1767 // even if we change only this one, the new label has different width,
1768 // so we need to refresh everything beyond this item as well
1769 RefreshAllItemsAfter(pos
);
1775 wxMenu
*wxMenuBar::Remove(size_t pos
)
1777 wxMenu
*menuOld
= wxMenuBarBase::Remove(pos
);
1781 m_menuInfos
.RemoveAt(pos
);
1783 // this doesn't happen too often, so don't try to be too smart - just
1784 // refresh everything
1791 // ----------------------------------------------------------------------------
1792 // wxMenuBar top level menus access
1793 // ----------------------------------------------------------------------------
1795 wxCoord
wxMenuBar::GetItemWidth(size_t pos
) const
1797 return m_menuInfos
[pos
].GetWidth(wxConstCast(this, wxMenuBar
));
1800 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1802 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1804 if ( enable
!= m_menuInfos
[pos
].IsEnabled() )
1806 m_menuInfos
[pos
].SetEnabled(enable
);
1810 //else: nothing to do
1813 bool wxMenuBar::IsEnabledTop(size_t pos
) const
1815 wxCHECK_MSG( pos
< GetCount(), false, _T("invalid index in IsEnabledTop") );
1817 return m_menuInfos
[pos
].IsEnabled();
1820 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
1822 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1824 if ( label
!= m_menuInfos
[pos
].GetLabel() )
1826 m_menuInfos
[pos
].SetLabel(label
);
1830 //else: nothing to do
1833 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
1835 wxCHECK_MSG( pos
< GetCount(), wxEmptyString
, _T("invalid index in GetLabelTop") );
1837 return m_menuInfos
[pos
].GetLabel();
1840 // ----------------------------------------------------------------------------
1841 // wxMenuBar drawing
1842 // ----------------------------------------------------------------------------
1844 void wxMenuBar::RefreshAllItemsAfter(size_t pos
)
1848 // no need to refresh if nothing is shown yet
1852 wxRect rect
= GetItemRect(pos
);
1853 rect
.width
= GetClientSize().x
- rect
.x
;
1857 void wxMenuBar::RefreshItem(size_t pos
)
1859 wxCHECK_RET( pos
!= (size_t)-1,
1860 _T("invalid item in wxMenuBar::RefreshItem") );
1864 // no need to refresh if nothing is shown yet
1868 RefreshRect(GetItemRect(pos
));
1871 void wxMenuBar::DoDraw(wxControlRenderer
*renderer
)
1873 wxDC
& dc
= renderer
->GetDC();
1874 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1876 // redraw only the items which must be redrawn
1878 // we don't have to use GetUpdateClientRect() here because our client rect
1879 // is the same as total one
1880 wxRect rectUpdate
= GetUpdateRegion().GetBox();
1882 int flagsMenubar
= GetStateFlags();
1886 rect
.height
= GetClientSize().y
;
1889 size_t count
= GetCount();
1890 for ( size_t n
= 0; n
< count
; n
++ )
1892 if ( x
> rectUpdate
.GetRight() )
1894 // all remaining items are to the right of rectUpdate
1899 rect
.width
= GetItemWidth(n
);
1901 if ( x
< rectUpdate
.x
)
1903 // this item is still to the left of rectUpdate
1907 int flags
= flagsMenubar
;
1908 if ( m_current
!= -1 && n
== (size_t)m_current
)
1910 flags
|= wxCONTROL_SELECTED
;
1913 if ( !IsEnabledTop(n
) )
1915 flags
|= wxCONTROL_DISABLED
;
1918 GetRenderer()->DrawMenuBarItem
1922 m_menuInfos
[n
].GetLabel(),
1924 m_menuInfos
[n
].GetAccelIndex()
1929 // ----------------------------------------------------------------------------
1930 // wxMenuBar geometry
1931 // ----------------------------------------------------------------------------
1933 wxRect
wxMenuBar::GetItemRect(size_t pos
) const
1935 wxASSERT_MSG( pos
< GetCount(), _T("invalid menu bar item index") );
1936 wxASSERT_MSG( IsCreated(), _T("can't call this method yet") );
1941 rect
.height
= GetClientSize().y
;
1943 for ( size_t n
= 0; n
< pos
; n
++ )
1945 rect
.x
+= GetItemWidth(n
);
1948 rect
.width
= GetItemWidth(pos
);
1953 wxSize
wxMenuBar::DoGetBestClientSize() const
1956 if ( GetMenuCount() > 0 )
1958 wxClientDC
dc(wxConstCast(this, wxMenuBar
));
1959 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1960 dc
.GetTextExtent(GetLabelTop(0), &size
.x
, &size
.y
);
1962 // adjust for the renderer we use
1963 size
= GetRenderer()->GetMenuBarItemSize(size
);
1965 else // empty menubar
1971 // the width is arbitrary, of course, for horizontal menubar
1977 int wxMenuBar::GetMenuFromPoint(const wxPoint
& pos
) const
1979 if ( pos
.x
< 0 || pos
.y
< 0 || pos
.y
> GetClientSize().y
)
1984 size_t count
= GetCount();
1985 for ( size_t item
= 0; item
< count
; item
++ )
1987 x
+= GetItemWidth(item
);
1995 // to the right of the last menu item
1999 // ----------------------------------------------------------------------------
2000 // wxMenuBar menu operations
2001 // ----------------------------------------------------------------------------
2003 void wxMenuBar::SelectMenu(size_t pos
)
2006 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::SelectMenu"));
2012 void wxMenuBar::DoSelectMenu(size_t pos
)
2014 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in DoSelectMenu") );
2016 int posOld
= m_current
;
2022 // close the previous menu
2023 if ( IsShowingMenu() )
2025 // restore m_shouldShowMenu flag after DismissMenu() which resets
2027 bool old
= m_shouldShowMenu
;
2031 m_shouldShowMenu
= old
;
2034 RefreshItem((size_t)posOld
);
2040 void wxMenuBar::PopupMenu(size_t pos
)
2042 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in PopupCurrentMenu") );
2049 // ----------------------------------------------------------------------------
2050 // wxMenuBar input handing
2051 // ----------------------------------------------------------------------------
2054 Note that wxMenuBar doesn't use wxInputHandler but handles keyboard and
2055 mouse in the same way under all platforms. This is because it doesn't derive
2056 from wxControl (which works with input handlers) but directly from wxWindow.
2058 Also, menu bar input handling is rather simple, so maybe it's not really
2059 worth making it themeable - at least I've decided against doing it now as it
2060 would merging the changes back into trunk more difficult. But it still could
2061 be done later if really needed.
2064 void wxMenuBar::OnKillFocus(wxFocusEvent
& event
)
2066 if ( m_current
!= -1 )
2068 RefreshItem((size_t)m_current
);
2076 void wxMenuBar::OnLeftDown(wxMouseEvent
& event
)
2084 else // we didn't have mouse capture, capture it now
2086 m_current
= GetMenuFromPoint(event
.GetPosition());
2087 if ( m_current
== -1 )
2089 // unfortunately, we can't prevent wxMSW from giving us the focus,
2090 // so we can only give it back
2095 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::OnLeftDown"));
2098 // show it as selected
2099 RefreshItem((size_t)m_current
);
2102 PopupCurrentMenu(false /* don't select first item - as Windows does */);
2107 void wxMenuBar::OnMouseMove(wxMouseEvent
& event
)
2111 (void)ProcessMouseEvent(event
.GetPosition());
2119 bool wxMenuBar::ProcessMouseEvent(const wxPoint
& pt
)
2121 // a hack to ignore the extra mouse events MSW sends us: this is similar to
2122 // wxUSE_MOUSEEVENT_HACK in wxWin itself but it isn't enough for us here as
2123 // we get the messages from different windows (old and new popup menus for
2126 static wxPoint s_ptLast
;
2127 if ( pt
== s_ptLast
)
2135 int currentNew
= GetMenuFromPoint(pt
);
2136 if ( (currentNew
== -1) || (currentNew
== m_current
) )
2141 // select the new active item
2142 DoSelectMenu(currentNew
);
2144 // show the menu if we know that we should, even if we hadn't been showing
2145 // it before (this may happen if the previous menu was disabled)
2146 if ( m_shouldShowMenu
&& !m_menuShown
)
2148 // open the new menu if the old one we closed had been opened
2149 PopupCurrentMenu(false /* don't select first item - as Windows does */);
2155 void wxMenuBar::OnKeyDown(wxKeyEvent
& event
)
2157 // ensure that we have a current item - we might not have it if we're
2158 // given the focus with Alt or F10 press (and under GTK+ the menubar
2159 // somehow gets the keyboard events even when it doesn't have focus...)
2160 if ( m_current
== -1 )
2162 if ( !HasCapture() )
2166 else // we do have capture
2168 // we always maintain a valid current item while we're in modal
2169 // state (i.e. have the capture)
2170 wxFAIL_MSG( _T("how did we manage to lose current item?") );
2176 int key
= event
.GetKeyCode();
2178 // first let the menu have it
2179 if ( IsShowingMenu() && m_menuShown
->ProcessKeyDown(key
) )
2184 // cycle through the menu items when left/right arrows are pressed and open
2185 // the menu when up/down one is
2189 // Alt must be processed at wxWindow level too
2194 // remove the selection and give the focus away
2195 if ( m_current
!= -1 )
2197 if ( IsShowingMenu() )
2209 size_t count
= GetCount();
2212 // the item won't change anyhow
2215 //else: otherwise, it will
2217 // remember if we were showing a menu - if we did, we should
2218 // show the new menu after changing the item
2219 bool wasMenuOpened
= IsShowingMenu();
2220 if ( wasMenuOpened
)
2225 // cast is safe as we tested for -1 above
2226 size_t currentNew
= (size_t)m_current
;
2228 if ( key
== WXK_LEFT
)
2230 if ( currentNew
-- == 0 )
2231 currentNew
= count
- 1;
2235 if ( ++currentNew
== count
)
2239 DoSelectMenu(currentNew
);
2241 if ( wasMenuOpened
)
2256 // letters open the corresponding menu
2259 int idxFound
= FindNextItemForAccel(m_current
, key
, &unique
);
2261 if ( idxFound
!= -1 )
2263 if ( IsShowingMenu() )
2268 DoSelectMenu((size_t)idxFound
);
2270 // if the item is not unique, just select it but don't
2271 // activate as the user might have wanted to activate
2274 // also, don't try to open a disabled menu
2275 if ( unique
&& IsEnabledTop((size_t)idxFound
) )
2281 // skip the "event.Skip()" below
2290 // ----------------------------------------------------------------------------
2291 // wxMenuBar accel handling
2292 // ----------------------------------------------------------------------------
2294 int wxMenuBar::FindNextItemForAccel(int idxStart
, int key
, bool *unique
) const
2296 if ( !wxIsalnum((wxChar
)key
) )
2298 // we only support letters/digits as accels
2302 // do we have more than one item with this accel?
2306 // translate everything to lower case before comparing
2307 wxChar chAccel
= (wxChar
)wxTolower(key
);
2309 // the index of the item with this accel
2312 // loop through all items searching for the item with this
2313 // accel starting at the item after the current one
2314 int count
= GetCount();
2315 int n
= idxStart
== -1 ? 0 : idxStart
+ 1;
2326 const wxMenuInfo
& info
= m_menuInfos
[n
];
2328 int idxAccel
= info
.GetAccelIndex();
2329 if ( idxAccel
!= -1 &&
2330 wxTolower(info
.GetLabel()[(size_t)idxAccel
])
2333 // ok, found an item with this accel
2334 if ( idxFound
== -1 )
2336 // store it but continue searching as we need to
2337 // know if it's the only item with this accel or if
2341 else // we already had found such item
2346 // no need to continue further, we won't find
2347 // anything we don't already know
2352 // we want to iterate over all items wrapping around if
2360 if ( n
== idxStart
)
2362 // we've seen all items
2372 bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent
& event
)
2375 for ( wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
2377 node
= node
->GetNext(), n
++ )
2379 // accels of the items in the disabled menus shouldn't work
2380 if ( m_menuInfos
[n
].IsEnabled() )
2382 if ( node
->GetData()->ProcessAccelEvent(event
) )
2384 // menu processed it
2394 #endif // wxUSE_ACCEL
2396 // ----------------------------------------------------------------------------
2397 // wxMenuBar menus showing
2398 // ----------------------------------------------------------------------------
2400 void wxMenuBar::PopupCurrentMenu(bool selectFirst
)
2402 wxCHECK_RET( m_current
!= -1, _T("no menu to popup") );
2404 // forgot to call DismissMenu()?
2405 wxASSERT_MSG( !m_menuShown
, _T("shouldn't show two menus at once!") );
2407 // in any case, we should show it - even if we won't
2408 m_shouldShowMenu
= true;
2410 if ( IsEnabledTop(m_current
) )
2412 // remember the menu we show
2413 m_menuShown
= GetMenu(m_current
);
2415 // we don't show the menu at all if it has no items
2416 if ( !m_menuShown
->IsEmpty() )
2418 // position it correctly: note that we must use screen coords and
2419 // that we pass 0 as width to position the menu exactly below the
2420 // item, not to the right of it
2421 wxRect rectItem
= GetItemRect(m_current
);
2423 m_menuShown
->SetInvokingWindow(m_frameLast
);
2425 m_menuShown
->Popup(ClientToScreen(rectItem
.GetPosition()),
2426 wxSize(0, rectItem
.GetHeight()),
2431 // reset it back as no menu is shown
2435 //else: don't show disabled menu
2438 void wxMenuBar::DismissMenu()
2440 wxCHECK_RET( m_menuShown
, _T("can't dismiss menu if none is shown") );
2442 m_menuShown
->Dismiss();
2446 void wxMenuBar::OnDismissMenu(bool dismissMenuBar
)
2448 m_shouldShowMenu
= false;
2450 if ( dismissMenuBar
)
2456 void wxMenuBar::OnDismiss()
2458 if ( ReleaseMouseCapture() )
2459 wxLogTrace(_T("mousecapture"), _T("Releasing mouse from wxMenuBar::OnDismiss"));
2461 if ( m_current
!= -1 )
2463 size_t current
= m_current
;
2466 RefreshItem(current
);
2472 bool wxMenuBar::ReleaseMouseCapture()
2475 // With wxX11, when a menu is closed by clicking away from it, a control
2476 // under the click will still get an event, even though the menu has the
2477 // capture (bug?). So that control may already have taken the capture by
2478 // this point, preventing us from releasing the menu's capture. So to work
2479 // around this, we release both captures, then put back the control's
2481 wxWindow
*capture
= GetCapture();
2484 capture
->ReleaseMouse();
2486 if ( capture
== this )
2489 bool had
= HasCapture();
2494 capture
->CaptureMouse();
2508 void wxMenuBar::GiveAwayFocus()
2510 GetFrame()->SetFocus();
2513 // ----------------------------------------------------------------------------
2514 // popup menu support
2515 // ----------------------------------------------------------------------------
2517 wxEventLoop
*wxWindow::ms_evtLoopPopup
= NULL
;
2519 bool wxWindow::DoPopupMenu(wxMenu
*menu
, int x
, int y
)
2521 wxCHECK_MSG( !ms_evtLoopPopup
, false,
2522 _T("can't show more than one popup menu at a time") );
2525 // we need to change the cursor before showing the menu as, apparently, no
2526 // cursor changes took place while the mouse is captured
2527 wxCursor cursorOld
= GetCursor();
2528 SetCursor(wxCURSOR_ARROW
);
2532 // flash any delayed log messages before showing the menu, otherwise it
2533 // could be dismissed (because it would lose focus) immediately after being
2535 wxLog::FlushActive();
2537 // some controls update themselves from OnIdle() call - let them do it
2538 wxTheApp
->ProcessIdle();
2540 // if the window hadn't been refreshed yet, the menu can adversely affect
2541 // its next OnPaint() handler execution - i.e. scrolled window refresh
2542 // logic breaks then as it scrolls part of the menu which hadn't been there
2543 // when the update event was generated into view
2547 menu
->SetInvokingWindow(this);
2549 // wxLogDebug( "Name of invoking window %s", menu->GetInvokingWindow()->GetName().c_str() );
2551 menu
->Popup(ClientToScreen(wxPoint(x
, y
)), wxSize(0,0));
2553 // this is not very useful if the menu was popped up because of the mouse
2554 // click but I think it is nice to do when it appears because of a key
2555 // press (i.e. Windows menu key)
2557 // Windows itself doesn't do it, but IMHO this is nice
2560 // we have to redirect all keyboard input to the menu temporarily
2561 PushEventHandler(new wxMenuKbdRedirector(menu
));
2563 // enter the local modal loop
2564 ms_evtLoopPopup
= new wxEventLoop
;
2565 ms_evtLoopPopup
->Run();
2567 delete ms_evtLoopPopup
;
2568 ms_evtLoopPopup
= NULL
;
2570 // remove the handler
2571 PopEventHandler(true /* delete it */);
2573 menu
->SetInvokingWindow(NULL
);
2576 SetCursor(cursorOld
);
2582 void wxWindow::DismissPopupMenu()
2584 wxCHECK_RET( ms_evtLoopPopup
, _T("no popup menu shown") );
2586 ms_evtLoopPopup
->Exit();
2589 #endif // wxUSE_MENUS