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/stockitem.h"
32 #include "wx/dynarray.h"
33 #include "wx/control.h" // for FindAccelIndex()
34 #include "wx/settings.h"
38 #include "wx/dcclient.h"
41 #include "wx/popupwin.h"
42 #include "wx/evtloop.h"
44 #include "wx/univ/renderer.h"
47 #include "wx/msw/private.h"
50 typedef wxMenuItemList::compatibility_iterator wxMenuItemIter
;
52 // ----------------------------------------------------------------------------
53 // wxMenuInfo contains all extra information about top level menus we need
54 // ----------------------------------------------------------------------------
56 class WXDLLEXPORT wxMenuInfo
60 wxMenuInfo(const wxString
& text
)
68 void SetLabel(const wxString
& text
)
70 m_originalLabel
= text
;
72 // remember the accel char (may be -1 if none)
73 m_indexAccel
= wxControl::FindAccelIndex(text
, &m_label
);
75 // calculate the width later, after the menu bar is created
79 void SetEnabled(bool enabled
= true) { m_isEnabled
= enabled
; }
83 const wxString
& GetLabel() const { return m_label
; }
84 const wxString
& GetOriginalLabel() const { return m_originalLabel
; }
85 bool IsEnabled() const { return m_isEnabled
; }
86 wxCoord
GetWidth(wxMenuBar
*menubar
) const
90 wxConstCast(this, wxMenuInfo
)->CalcWidth(menubar
);
96 int GetAccelIndex() const { return m_indexAccel
; }
99 void CalcWidth(wxMenuBar
*menubar
)
102 wxClientDC
dc(menubar
);
103 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
104 dc
.GetTextExtent(m_label
, &size
.x
, &size
.y
);
106 // adjust for the renderer we use and store the width
107 m_width
= menubar
->GetRenderer()->GetMenuBarItemSize(size
).x
;
111 wxString m_originalLabel
;
117 #include "wx/arrimpl.cpp"
119 WX_DEFINE_OBJARRAY(wxMenuInfoArray
);
121 // ----------------------------------------------------------------------------
122 // wxPopupMenuWindow: a popup window showing a menu
123 // ----------------------------------------------------------------------------
125 class wxPopupMenuWindow
: public wxPopupTransientWindow
128 wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
);
130 virtual ~wxPopupMenuWindow();
132 // override the base class version to select the first item initially
133 virtual void Popup(wxWindow
*focus
= NULL
);
135 // override the base class version to dismiss any open submenus
136 virtual void Dismiss();
138 // called when a submenu is dismissed
139 void OnSubmenuDismiss(bool dismissParent
);
141 // the default wxMSW wxPopupTransientWindow::OnIdle disables the capture
142 // when the cursor is inside the popup, which dsables the menu tracking
143 // so override it to do nothing
145 void OnIdle(wxIdleEvent
& WXUNUSED(event
)) { }
148 // get the currently selected item (may be NULL)
149 wxMenuItem
*GetCurrentItem() const
151 return m_nodeCurrent
? m_nodeCurrent
->GetData() : NULL
;
154 // find the menu item at given position
155 wxMenuItemIter
GetMenuItemFromPoint(const wxPoint
& pt
) const;
157 // refresh the given item
158 void RefreshItem(wxMenuItem
*item
);
160 // preselect the first item
161 void SelectFirst() { SetCurrentItem(m_menu
->GetMenuItems().GetFirst()); }
163 // process the key event, return true if done
164 bool ProcessKeyDown(int key
);
166 // process mouse move event
167 void ProcessMouseMove(const wxPoint
& pt
);
169 // don't dismiss the popup window if the parent menu was clicked
170 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
173 // how did we perform this operation?
180 // notify the menu when the window disappears from screen
181 virtual void OnDismiss();
183 // draw the menu inside this window
184 virtual void DoDraw(wxControlRenderer
*renderer
);
187 void OnLeftUp(wxMouseEvent
& event
);
188 void OnMouseMove(wxMouseEvent
& event
);
189 void OnMouseLeave(wxMouseEvent
& event
);
190 void OnKeyDown(wxKeyEvent
& event
);
192 // reset the current item and node
195 // set the current node and item without refreshing anything
196 void SetCurrentItem(wxMenuItemIter node
);
198 // change the current item refreshing the old and new items
199 void ChangeCurrent(wxMenuItemIter node
);
201 // activate item, i.e. call either ClickItem() or OpenSubmenu() depending
202 // on what it is, return true if something was done (i.e. it's not a
204 bool ActivateItem(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
206 // send the event about the item click
207 void ClickItem(wxMenuItem
*item
);
209 // show the submenu for this item
210 void OpenSubmenu(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
212 // can this tiem be opened?
213 bool CanOpen(wxMenuItem
*item
)
215 return item
&& item
->IsEnabled() && item
->IsSubMenu();
218 // dismiss the menu and all parent menus too
219 void DismissAndNotify();
221 // react to dimissing this menu and also dismiss the parent if
223 void HandleDismiss(bool dismissParent
);
225 // do we have an open submenu?
226 bool HasOpenSubmenu() const { return m_hasOpenSubMenu
; }
228 // get previous node after the current one
229 wxMenuItemIter
GetPrevNode() const;
231 // get previous node before the given one, wrapping if it's the first one
232 wxMenuItemIter
GetPrevNode(wxMenuItemIter node
) const;
234 // get next node after the current one
235 wxMenuItemIter
GetNextNode() const;
237 // get next node after the given one, wrapping if it's the last one
238 wxMenuItemIter
GetNextNode(wxMenuItemIter node
) const;
244 // the menu node corresponding to the current item
245 wxMenuItemIter m_nodeCurrent
;
247 // do we currently have an opened submenu?
248 bool m_hasOpenSubMenu
;
250 DECLARE_EVENT_TABLE()
253 // ----------------------------------------------------------------------------
254 // wxMenuKbdRedirector: an event handler which redirects kbd input to wxMenu
255 // ----------------------------------------------------------------------------
257 class wxMenuKbdRedirector
: public wxEvtHandler
260 wxMenuKbdRedirector(wxMenu
*menu
) { m_menu
= menu
; }
262 virtual bool ProcessEvent(wxEvent
& event
)
264 if ( event
.GetEventType() == wxEVT_KEY_DOWN
)
266 return m_menu
->ProcessKeyDown(((wxKeyEvent
&)event
).GetKeyCode());
272 return wxEvtHandler::ProcessEvent(event
);
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
284 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
285 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
286 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
288 BEGIN_EVENT_TABLE(wxPopupMenuWindow
, wxPopupTransientWindow
)
289 EVT_KEY_DOWN(wxPopupMenuWindow::OnKeyDown
)
291 EVT_LEFT_UP(wxPopupMenuWindow::OnLeftUp
)
292 EVT_MOTION(wxPopupMenuWindow::OnMouseMove
)
293 EVT_LEAVE_WINDOW(wxPopupMenuWindow::OnMouseLeave
)
295 EVT_IDLE(wxPopupMenuWindow::OnIdle
)
299 BEGIN_EVENT_TABLE(wxMenuBar
, wxMenuBarBase
)
300 EVT_KILL_FOCUS(wxMenuBar::OnKillFocus
)
302 EVT_KEY_DOWN(wxMenuBar::OnKeyDown
)
304 EVT_LEFT_DOWN(wxMenuBar::OnLeftDown
)
305 EVT_MOTION(wxMenuBar::OnMouseMove
)
308 // ============================================================================
310 // ============================================================================
312 // ----------------------------------------------------------------------------
314 // ----------------------------------------------------------------------------
316 wxPopupMenuWindow::wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
)
319 m_hasOpenSubMenu
= false;
323 (void)Create(parent
, wxBORDER_RAISED
);
325 SetCursor(wxCURSOR_ARROW
);
328 wxPopupMenuWindow::~wxPopupMenuWindow()
330 // When m_popupMenu in wxMenu is deleted because it
331 // is a child of an old menu bar being deleted (note: it does
332 // not get destroyed by the wxMenu destructor, but
333 // by DestroyChildren()), m_popupMenu should be reset to NULL.
335 m_menu
->m_popupMenu
= NULL
;
338 // ----------------------------------------------------------------------------
339 // wxPopupMenuWindow current item/node handling
340 // ----------------------------------------------------------------------------
342 void wxPopupMenuWindow::ResetCurrent()
344 SetCurrentItem(wxMenuItemIter());
347 void wxPopupMenuWindow::SetCurrentItem(wxMenuItemIter node
)
349 m_nodeCurrent
= node
;
352 void wxPopupMenuWindow::ChangeCurrent(wxMenuItemIter node
)
354 if ( !m_nodeCurrent
|| !node
|| (node
!= m_nodeCurrent
) )
356 wxMenuItemIter nodeOldCurrent
= m_nodeCurrent
;
358 m_nodeCurrent
= node
;
360 if ( nodeOldCurrent
)
362 wxMenuItem
*item
= nodeOldCurrent
->GetData();
363 wxCHECK_RET( item
, _T("no current item?") );
365 // if it was the currently opened menu, close it
366 if ( item
->IsSubMenu() && item
->GetSubMenu()->IsShown() )
368 item
->GetSubMenu()->Dismiss();
369 OnSubmenuDismiss( false );
376 RefreshItem(m_nodeCurrent
->GetData());
380 wxMenuItemIter
wxPopupMenuWindow::GetPrevNode() const
382 // return the last node if there had been no previously selected one
383 return m_nodeCurrent
? GetPrevNode(m_nodeCurrent
)
384 : wxMenuItemIter(m_menu
->GetMenuItems().GetLast());
388 wxPopupMenuWindow::GetPrevNode(wxMenuItemIter node
) const
392 node
= node
->GetPrevious();
395 node
= m_menu
->GetMenuItems().GetLast();
398 //else: the menu is empty
403 wxMenuItemIter
wxPopupMenuWindow::GetNextNode() const
405 // return the first node if there had been no previously selected one
406 return m_nodeCurrent
? GetNextNode(m_nodeCurrent
)
407 : wxMenuItemIter(m_menu
->GetMenuItems().GetFirst());
411 wxPopupMenuWindow::GetNextNode(wxMenuItemIter node
) const
415 node
= node
->GetNext();
418 node
= m_menu
->GetMenuItems().GetFirst();
421 //else: the menu is empty
426 // ----------------------------------------------------------------------------
427 // wxPopupMenuWindow popup/dismiss
428 // ----------------------------------------------------------------------------
430 void wxPopupMenuWindow::Popup(wxWindow
*focus
)
432 // check that the current item had been properly reset before
433 wxASSERT_MSG( !m_nodeCurrent
||
434 m_nodeCurrent
== m_menu
->GetMenuItems().GetFirst(),
435 _T("menu current item preselected incorrectly") );
437 wxPopupTransientWindow::Popup(focus
);
439 // the base class no-longer captures the mouse automatically when Popup
440 // is called, so do it here to allow the menu tracking to work
445 // ensure that this window is really on top of everything: without using
446 // SetWindowPos() it can be covered by its parent menu which is not
447 // really what we want
448 wxMenu
*menuParent
= m_menu
->GetParent();
451 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
453 // if we're shown, the parent menu must be also shown
454 wxCHECK_RET( win
, _T("parent menu is not shown?") );
456 if ( !::SetWindowPos(GetHwndOf(win
), GetHwnd(),
458 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOREDRAW
) )
460 wxLogLastError(_T("SetWindowPos(HWND_TOP)"));
468 void wxPopupMenuWindow::Dismiss()
470 if ( HasOpenSubmenu() )
472 wxMenuItem
*item
= GetCurrentItem();
473 wxCHECK_RET( item
&& item
->IsSubMenu(), _T("where is our open submenu?") );
475 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
476 wxCHECK_RET( win
, _T("opened submenu is not opened?") );
479 OnSubmenuDismiss( false );
482 wxPopupTransientWindow::Dismiss();
487 void wxPopupMenuWindow::OnDismiss()
489 // when we are dismissed because the user clicked elsewhere or we lost
490 // focus in any other way, hide the parent menu as well
494 void wxPopupMenuWindow::OnSubmenuDismiss(bool WXUNUSED(dismissParent
))
496 m_hasOpenSubMenu
= false;
499 void wxPopupMenuWindow::HandleDismiss(bool dismissParent
)
501 m_menu
->OnDismiss(dismissParent
);
504 void wxPopupMenuWindow::DismissAndNotify()
510 // ----------------------------------------------------------------------------
511 // wxPopupMenuWindow geometry
512 // ----------------------------------------------------------------------------
515 wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint
& pt
) const
517 // we only use the y coord normally, but still check x in case the point is
518 // outside the window completely
519 if ( wxWindow::HitTest(pt
) == wxHT_WINDOW_INSIDE
)
522 for ( wxMenuItemIter node
= m_menu
->GetMenuItems().GetFirst();
524 node
= node
->GetNext() )
526 wxMenuItem
*item
= node
->GetData();
527 y
+= item
->GetHeight();
536 return wxMenuItemIter();
539 // ----------------------------------------------------------------------------
540 // wxPopupMenuWindow drawing
541 // ----------------------------------------------------------------------------
543 void wxPopupMenuWindow::RefreshItem(wxMenuItem
*item
)
545 wxCHECK_RET( item
, _T("can't refresh NULL item") );
547 wxASSERT_MSG( IsShown(), _T("can't refresh menu which is not shown") );
549 // FIXME: -1 here because of SetLogicalOrigin(1, 1) in DoDraw()
550 RefreshRect(wxRect(0, item
->GetPosition() - 1,
551 m_menu
->GetGeometryInfo().GetSize().x
, item
->GetHeight()));
554 void wxPopupMenuWindow::DoDraw(wxControlRenderer
*renderer
)
556 // no clipping so far - do we need it? I don't think so as the menu is
557 // never partially covered as it is always on top of everything
559 wxDC
& dc
= renderer
->GetDC();
560 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
562 // FIXME: this should be done in the renderer, however when it is fixed
563 // wxPopupMenuWindow::RefreshItem() should be changed too!
564 dc
.SetLogicalOrigin(1, 1);
566 wxRenderer
*rend
= renderer
->GetRenderer();
569 const wxMenuGeometryInfo
& gi
= m_menu
->GetGeometryInfo();
570 for ( wxMenuItemIter node
= m_menu
->GetMenuItems().GetFirst();
572 node
= node
->GetNext() )
574 wxMenuItem
*item
= node
->GetData();
576 if ( item
->IsSeparator() )
578 rend
->DrawMenuSeparator(dc
, y
, gi
);
580 else // not a separator
583 if ( item
->IsCheckable() )
585 flags
|= wxCONTROL_CHECKABLE
;
587 if ( item
->IsChecked() )
589 flags
|= wxCONTROL_CHECKED
;
593 if ( !item
->IsEnabled() )
594 flags
|= wxCONTROL_DISABLED
;
596 if ( item
->IsSubMenu() )
597 flags
|= wxCONTROL_ISSUBMENU
;
599 if ( item
== GetCurrentItem() )
600 flags
|= wxCONTROL_SELECTED
;
604 if ( !item
->IsEnabled() )
606 bmp
= item
->GetDisabledBitmap();
611 // strangely enough, for unchecked item we use the
612 // "checked" bitmap because this is the default one - this
613 // explains this strange boolean expression
614 bmp
= item
->GetBitmap(!item
->IsCheckable() || item
->IsChecked());
622 item
->GetItemLabelText(),
623 item
->GetAccelString(),
626 item
->GetAccelIndex()
630 y
+= item
->GetHeight();
634 // ----------------------------------------------------------------------------
635 // wxPopupMenuWindow actions
636 // ----------------------------------------------------------------------------
638 void wxPopupMenuWindow::ClickItem(wxMenuItem
*item
)
640 wxCHECK_RET( item
, _T("can't click NULL item") );
642 wxASSERT_MSG( !item
->IsSeparator() && !item
->IsSubMenu(),
643 _T("can't click this item") );
645 wxMenu
* menu
= m_menu
;
650 menu
->ClickItem(item
);
653 void wxPopupMenuWindow::OpenSubmenu(wxMenuItem
*item
, InputMethod how
)
655 wxCHECK_RET( item
, _T("can't open NULL submenu") );
657 wxMenu
*submenu
= item
->GetSubMenu();
658 wxCHECK_RET( submenu
, _T("can only open submenus!") );
660 // FIXME: should take into account the border width
661 submenu
->Popup(ClientToScreen(wxPoint(0, item
->GetPosition())),
662 wxSize(m_menu
->GetGeometryInfo().GetSize().x
, 0),
663 how
== WithKeyboard
/* preselect first item then */);
665 m_hasOpenSubMenu
= true;
668 bool wxPopupMenuWindow::ActivateItem(wxMenuItem
*item
, InputMethod how
)
670 // don't activate disabled items
671 if ( !item
|| !item
->IsEnabled() )
676 // normal menu items generate commands, submenus can be opened and
677 // the separators don't do anything
678 if ( item
->IsSubMenu() )
680 OpenSubmenu(item
, how
);
682 else if ( !item
->IsSeparator() )
686 else // separator, can't activate
694 // ----------------------------------------------------------------------------
695 // wxPopupMenuWindow input handling
696 // ----------------------------------------------------------------------------
698 bool wxPopupMenuWindow::ProcessLeftDown(wxMouseEvent
& event
)
700 // wxPopupWindowHandler dismisses the window when the mouse is clicked
701 // outside it which is usually just fine, but there is one case when we
702 // don't want to do it: if the mouse was clicked on the parent submenu item
703 // which opens this menu, so check for it
705 wxPoint pos
= event
.GetPosition();
706 if ( HitTest(pos
.x
, pos
.y
) == wxHT_WINDOW_OUTSIDE
)
708 wxMenu
*menu
= m_menu
->GetParent();
711 wxPopupMenuWindow
*win
= menu
->m_popupMenu
;
713 wxCHECK_MSG( win
, false, _T("parent menu not shown?") );
715 pos
= ClientToScreen(pos
);
716 if ( win
->GetMenuItemFromPoint(win
->ScreenToClient(pos
)) )
721 //else: it is outside the parent menu as well, do dismiss this one
728 void wxPopupMenuWindow::OnLeftUp(wxMouseEvent
& event
)
730 wxMenuItemIter node
= GetMenuItemFromPoint(event
.GetPosition());
733 ActivateItem(node
->GetData(), WithMouse
);
737 void wxPopupMenuWindow::OnMouseMove(wxMouseEvent
& event
)
739 const wxPoint pt
= event
.GetPosition();
741 // we need to ignore extra mouse events: example when this happens is when
742 // the mouse is on the menu and we open a submenu from keyboard - Windows
743 // then sends us a dummy mouse move event, we (correctly) determine that it
744 // happens in the parent menu and so immediately close the just opened
747 static wxPoint s_ptLast
;
748 wxPoint ptCur
= ClientToScreen(pt
);
749 if ( ptCur
== s_ptLast
)
757 ProcessMouseMove(pt
);
762 void wxPopupMenuWindow::ProcessMouseMove(const wxPoint
& pt
)
764 wxMenuItemIter node
= GetMenuItemFromPoint(pt
);
766 // don't reset current to NULL here, we only do it when the mouse leaves
767 // the window (see below)
770 if ( !m_nodeCurrent
|| (node
!= m_nodeCurrent
) )
774 wxMenuItem
*item
= GetCurrentItem();
777 OpenSubmenu(item
, WithMouse
);
780 //else: same item, nothing to do
782 else // not on an item
784 // the last open submenu forwards the mouse move messages to its
785 // parent, so if the mouse moves to another item of the parent menu,
786 // this menu is closed and this other item is selected - in the similar
787 // manner, the top menu forwards the mouse moves to the menubar which
788 // allows to select another top level menu by just moving the mouse
790 // we need to translate our client coords to the client coords of the
791 // window we forward this event to
792 wxPoint ptScreen
= ClientToScreen(pt
);
794 // if the mouse is outside this menu, let the parent one to
796 wxMenu
*menuParent
= m_menu
->GetParent();
799 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
801 // if we're shown, the parent menu must be also shown
802 wxCHECK_RET( win
, _T("parent menu is not shown?") );
804 win
->ProcessMouseMove(win
->ScreenToClient(ptScreen
));
806 else // no parent menu
808 wxMenuBar
*menubar
= m_menu
->GetMenuBar();
811 if ( menubar
->ProcessMouseEvent(
812 menubar
->ScreenToClient(ptScreen
)) )
814 // menubar has closed this menu and opened another one, probably
819 //else: top level popup menu, no other processing to do
823 void wxPopupMenuWindow::OnMouseLeave(wxMouseEvent
& event
)
825 // due to the artefact of mouse events generation under MSW, we actually
826 // may get the mouse leave event after the menu had been already dismissed
827 // and calling ChangeCurrent() would then assert, so don't do it
830 // we shouldn't change the current them if our submenu is opened and
831 // mouse moved there, in this case the submenu is responsable for
834 if ( HasOpenSubmenu() )
836 wxMenuItem
*item
= GetCurrentItem();
837 wxCHECK_RET( CanOpen(item
), _T("where is our open submenu?") );
839 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
840 wxCHECK_RET( win
, _T("submenu is opened but not shown?") );
842 // only handle this event if the mouse is not inside the submenu
843 wxPoint pt
= ClientToScreen(event
.GetPosition());
845 win
->HitTest(win
->ScreenToClient(pt
)) == wxHT_WINDOW_OUTSIDE
;
849 // this menu is the last opened
855 ChangeCurrent(wxMenuItemIter());
862 void wxPopupMenuWindow::OnKeyDown(wxKeyEvent
& event
)
864 wxMenuBar
*menubar
= m_menu
->GetMenuBar();
868 menubar
->ProcessEvent(event
);
870 else if ( !ProcessKeyDown(event
.GetKeyCode()) )
876 bool wxPopupMenuWindow::ProcessKeyDown(int key
)
878 wxMenuItem
*item
= GetCurrentItem();
880 // first let the opened submenu to have it (no test for IsEnabled() here,
881 // the keys navigate even in a disabled submenu if we had somehow managed
882 // to open it inspit of this)
883 if ( HasOpenSubmenu() )
885 wxCHECK_MSG( CanOpen(item
), false,
886 _T("has open submenu but another item selected?") );
888 if ( item
->GetSubMenu()->ProcessKeyDown(key
) )
892 bool processed
= true;
894 // handle the up/down arrows, home, end, esc and return here, pass the
895 // left/right arrows to the menu bar except when the right arrow can be
896 // used to open a submenu
900 // if we're not a top level menu, close us, else leave this to the
902 if ( !m_menu
->GetParent() )
911 // close just this menu
913 HandleDismiss(false);
917 processed
= ActivateItem(item
);
921 ChangeCurrent(m_menu
->GetMenuItems().GetFirst());
925 ChangeCurrent(m_menu
->GetMenuItems().GetLast());
931 bool up
= key
== WXK_UP
;
933 wxMenuItemIter nodeStart
= up
? GetPrevNode() : GetNextNode(),
935 while ( node
&& node
->GetData()->IsSeparator() )
937 node
= up
? GetPrevNode(node
) : GetNextNode(node
);
939 if ( node
== nodeStart
)
941 // nothing but separators and disabled items in this
943 node
= wxMenuItemIter();
959 // don't try to reopen an already opened menu
960 if ( !HasOpenSubmenu() && CanOpen(item
) )
971 // look for the menu item starting with this letter
972 if ( wxIsalnum((wxChar
)key
) )
974 // we want to start from the item after this one because
975 // if we're already on the item with the given accel we want to
976 // go to the next one, not to stay in place
977 wxMenuItemIter nodeStart
= GetNextNode();
979 // do we have more than one item with this accel?
980 bool notUnique
= false;
982 // translate everything to lower case before comparing
983 wxChar chAccel
= (wxChar
)wxTolower(key
);
985 // loop through all items searching for the item with this
987 wxMenuItemIter nodeFound
,
991 item
= node
->GetData();
993 int idxAccel
= item
->GetAccelIndex();
994 if ( idxAccel
!= -1 &&
995 (wxChar
)wxTolower(item
->GetItemLabelText()[(size_t)idxAccel
])
998 // ok, found an item with this accel
1001 // store it but continue searching as we need to
1002 // know if it's the only item with this accel or if
1006 else // we already had found such item
1010 // no need to continue further, we won't find
1011 // anything we don't already know
1016 // we want to iterate over all items wrapping around if
1018 node
= GetNextNode(node
);
1019 if ( node
== nodeStart
)
1021 // we've seen all nodes
1028 item
= nodeFound
->GetData();
1030 // go to this item anyhow
1031 ChangeCurrent(nodeFound
);
1033 if ( !notUnique
&& item
->IsEnabled() )
1035 // unique item with this accel - activate it
1036 processed
= ActivateItem(item
);
1038 //else: just select it but don't activate as the user might
1039 // have wanted to activate another item
1041 // skip "processed = false" below
1052 // ----------------------------------------------------------------------------
1054 // ----------------------------------------------------------------------------
1062 m_startRadioGroup
= -1;
1071 // ----------------------------------------------------------------------------
1072 // wxMenu and wxMenuGeometryInfo
1073 // ----------------------------------------------------------------------------
1075 wxMenuGeometryInfo::~wxMenuGeometryInfo()
1079 const wxMenuGeometryInfo
& wxMenu::GetGeometryInfo() const
1085 wxConstCast(this, wxMenu
)->m_geometry
=
1086 m_popupMenu
->GetRenderer()->GetMenuGeometry(m_popupMenu
, *this);
1090 wxFAIL_MSG( _T("can't get geometry without window") );
1097 void wxMenu::InvalidateGeometryInfo()
1106 // ----------------------------------------------------------------------------
1107 // wxMenu adding/removing items
1108 // ----------------------------------------------------------------------------
1110 void wxMenu::OnItemAdded(wxMenuItem
*item
)
1112 InvalidateGeometryInfo();
1116 #endif // wxUSE_ACCEL
1118 // the submenus of a popup menu should have the same invoking window as it
1120 if ( m_invokingWindow
&& item
->IsSubMenu() )
1122 item
->GetSubMenu()->SetInvokingWindow(m_invokingWindow
);
1126 void wxMenu::EndRadioGroup()
1128 // we're not inside a radio group any longer
1129 m_startRadioGroup
= -1;
1132 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
1134 if ( item
->GetKind() == wxITEM_RADIO
)
1136 int count
= GetMenuItemCount();
1138 if ( m_startRadioGroup
== -1 )
1140 // start a new radio group
1141 m_startRadioGroup
= count
;
1143 // for now it has just one element
1144 item
->SetAsRadioGroupStart();
1145 item
->SetRadioGroupEnd(m_startRadioGroup
);
1147 else // extend the current radio group
1149 // we need to update its end item
1150 item
->SetRadioGroupStart(m_startRadioGroup
);
1151 wxMenuItemIter node
= GetMenuItems().Item(m_startRadioGroup
);
1155 node
->GetData()->SetRadioGroupEnd(count
);
1159 wxFAIL_MSG( _T("where is the radio group start item?") );
1163 else // not a radio item
1168 if ( !wxMenuBase::DoAppend(item
) )
1176 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1178 if ( !wxMenuBase::DoInsert(pos
, item
) )
1186 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1188 wxMenuItem
*itemOld
= wxMenuBase::DoRemove(item
);
1192 InvalidateGeometryInfo();
1195 RemoveAccelFor(item
);
1196 #endif // wxUSE_ACCEL
1202 // ----------------------------------------------------------------------------
1203 // wxMenu attaching/detaching
1204 // ----------------------------------------------------------------------------
1206 void wxMenu::Attach(wxMenuBarBase
*menubar
)
1208 wxMenuBase::Attach(menubar
);
1210 wxCHECK_RET( m_menuBar
, _T("menubar can't be NULL after attaching") );
1212 // unfortunately, we can't use m_menuBar->GetEventHandler() here because,
1213 // if the menubar is currently showing a menu, its event handler is a
1214 // temporary one installed by wxPopupWindow and so will disappear soon any
1215 // any attempts to use it from the newly attached menu would result in a
1218 // so we use the menubar itself, even if it's a pity as it means we can't
1219 // redirect all menu events by changing the menubar handler (FIXME)
1220 SetNextHandler(m_menuBar
);
1223 void wxMenu::Detach()
1225 wxMenuBase::Detach();
1228 // ----------------------------------------------------------------------------
1229 // wxMenu misc functions
1230 // ----------------------------------------------------------------------------
1232 wxWindow
*wxMenu::GetRootWindow() const
1236 // simple case - a normal menu attached to the menubar
1237 return GetMenuBar();
1240 // we're a popup menu but the trouble is that only the top level popup menu
1241 // has a pointer to the invoking window, so we must walk up the menu chain
1243 wxWindow
*win
= GetInvokingWindow();
1246 // we already have it
1250 wxMenu
*menu
= GetParent();
1253 // We are a submenu of a menu of a menubar
1254 if (menu
->GetMenuBar())
1255 return menu
->GetMenuBar();
1257 win
= menu
->GetInvokingWindow();
1261 menu
= menu
->GetParent();
1264 // we're probably going to crash in the caller anyhow, but try to detect
1265 // this error as soon as possible
1266 wxASSERT_MSG( win
, _T("menu without any associated window?") );
1268 // also remember it in this menu so that we don't have to search for it the
1270 wxConstCast(this, wxMenu
)->m_invokingWindow
= win
;
1275 wxRenderer
*wxMenu::GetRenderer() const
1277 // we're going to crash without renderer!
1278 wxCHECK_MSG( m_popupMenu
, NULL
, _T("neither popup nor menubar menu?") );
1280 return m_popupMenu
->GetRenderer();
1283 void wxMenu::RefreshItem(wxMenuItem
*item
)
1285 // the item geometry changed, so our might have changed as well
1286 InvalidateGeometryInfo();
1290 // this would be a bug in IsShown()
1291 wxCHECK_RET( m_popupMenu
, _T("must have popup window if shown!") );
1293 // recalc geometry to update the item height and such
1294 (void)GetGeometryInfo();
1296 m_popupMenu
->RefreshItem(item
);
1300 // ----------------------------------------------------------------------------
1301 // wxMenu showing and hiding
1302 // ----------------------------------------------------------------------------
1304 bool wxMenu::IsShown() const
1306 return m_popupMenu
&& m_popupMenu
->IsShown();
1309 void wxMenu::OnDismiss(bool dismissParent
)
1313 // always notify the parent about submenu disappearance
1314 wxPopupMenuWindow
*win
= m_menuParent
->m_popupMenu
;
1317 win
->OnSubmenuDismiss( true );
1321 wxFAIL_MSG( _T("parent menu not shown?") );
1324 // and if we dismiss everything, propagate to parent
1325 if ( dismissParent
)
1327 // dismissParent is recursive
1328 m_menuParent
->Dismiss();
1329 m_menuParent
->OnDismiss(true);
1332 else // no parent menu
1334 // notify the menu bar if we're a top level menu
1337 m_menuBar
->OnDismissMenu(dismissParent
);
1341 wxCHECK_RET( m_invokingWindow
, _T("what kind of menu is this?") );
1343 m_invokingWindow
->DismissPopupMenu();
1345 // Why reset it here? We need it for sending the event to...
1346 // SetInvokingWindow(NULL);
1351 void wxMenu::Popup(const wxPoint
& pos
, const wxSize
& size
, bool selectFirst
)
1353 // create the popup window if not done yet
1356 m_popupMenu
= new wxPopupMenuWindow(GetRootWindow(), this);
1359 // select the first item unless disabled
1362 m_popupMenu
->SelectFirst();
1365 // the geometry might have changed since the last time we were shown, so
1367 m_popupMenu
->SetClientSize(GetGeometryInfo().GetSize());
1369 // position it as specified
1370 m_popupMenu
->Position(pos
, size
);
1372 // the menu can't have the focus itself (it is a Windows limitation), so
1373 // always keep the focus at the originating window
1374 wxWindow
*focus
= GetRootWindow();
1376 wxASSERT_MSG( focus
, _T("no window to keep focus on?") );
1379 m_popupMenu
->Popup(focus
);
1382 void wxMenu::Dismiss()
1384 wxCHECK_RET( IsShown(), _T("can't dismiss hidden menu") );
1386 m_popupMenu
->Dismiss();
1389 // ----------------------------------------------------------------------------
1390 // wxMenu event processing
1391 // ----------------------------------------------------------------------------
1393 bool wxMenu::ProcessKeyDown(int key
)
1395 wxCHECK_MSG( m_popupMenu
, false,
1396 _T("can't process key events if not shown") );
1398 return m_popupMenu
->ProcessKeyDown(key
);
1401 bool wxMenu::ClickItem(wxMenuItem
*item
)
1404 if ( item
->IsCheckable() )
1406 // update the item state
1407 isChecked
= !item
->IsChecked();
1409 item
->Check(isChecked
!= 0);
1417 return SendEvent(item
->GetId(), isChecked
);
1420 // ----------------------------------------------------------------------------
1421 // wxMenu accel support
1422 // ----------------------------------------------------------------------------
1426 bool wxMenu::ProcessAccelEvent(const wxKeyEvent
& event
)
1428 // do we have an item for this accel?
1429 wxMenuItem
*item
= m_accelTable
.GetMenuItem(event
);
1430 if ( item
&& item
->IsEnabled() )
1432 return ClickItem(item
);
1436 for ( wxMenuItemIter node
= GetMenuItems().GetFirst();
1438 node
= node
->GetNext() )
1440 const wxMenuItem
*item
= node
->GetData();
1441 if ( item
->IsSubMenu() && item
->IsEnabled() )
1444 if ( item
->GetSubMenu()->ProcessAccelEvent(event
) )
1454 void wxMenu::AddAccelFor(wxMenuItem
*item
)
1456 wxAcceleratorEntry
*accel
= item
->GetAccel();
1459 accel
->SetMenuItem(item
);
1461 m_accelTable
.Add(*accel
);
1467 void wxMenu::RemoveAccelFor(wxMenuItem
*item
)
1469 wxAcceleratorEntry
*accel
= item
->GetAccel();
1472 m_accelTable
.Remove(*accel
);
1478 #endif // wxUSE_ACCEL
1480 // ----------------------------------------------------------------------------
1481 // wxMenuItem construction
1482 // ----------------------------------------------------------------------------
1484 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
1486 const wxString
& text
,
1487 const wxString
& help
,
1490 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
1493 m_height
= wxDefaultCoord
;
1495 m_radioGroup
.start
= -1;
1496 m_isRadioGroupStart
= false;
1498 m_bmpDisabled
= wxNullBitmap
;
1503 wxMenuItem::~wxMenuItem()
1507 // ----------------------------------------------------------------------------
1508 // wxMenuItemBase methods implemented here
1509 // ----------------------------------------------------------------------------
1512 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
1514 const wxString
& name
,
1515 const wxString
& help
,
1519 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
1523 wxString
wxMenuItemBase::GetLabelText(const wxString
& text
)
1525 return wxStripMenuCodes(text
);
1528 // ----------------------------------------------------------------------------
1529 // wxMenuItem operations
1530 // ----------------------------------------------------------------------------
1532 void wxMenuItem::NotifyMenu()
1534 m_parentMenu
->RefreshItem(this);
1537 void wxMenuItem::UpdateAccelInfo()
1539 m_indexAccel
= wxControl::FindAccelIndex(m_text
);
1541 // will be empty if the text contains no TABs - ok
1542 m_strAccel
= m_text
.AfterFirst(_T('\t'));
1545 void wxMenuItem::SetItemLabel(const wxString
& text
)
1547 if ( text
!= m_text
)
1549 // first call the base class version to change m_text
1550 // (and also check if we don't have a stock menu item)
1551 wxMenuItemBase::SetItemLabel(text
);
1559 void wxMenuItem::SetCheckable(bool checkable
)
1561 if ( checkable
!= IsCheckable() )
1563 wxMenuItemBase::SetCheckable(checkable
);
1569 void wxMenuItem::SetBitmaps(const wxBitmap
& bmpChecked
,
1570 const wxBitmap
& bmpUnchecked
)
1572 m_bmpChecked
= bmpChecked
;
1573 m_bmpUnchecked
= bmpUnchecked
;
1578 void wxMenuItem::Enable(bool enable
)
1580 if ( enable
!= m_isEnabled
)
1582 wxMenuItemBase::Enable(enable
);
1588 void wxMenuItem::Check(bool check
)
1590 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
1592 if ( m_isChecked
== check
)
1595 if ( GetKind() == wxITEM_RADIO
)
1597 // it doesn't make sense to uncheck a radio item - what would this do?
1601 // get the index of this item in the menu
1602 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
1603 int pos
= items
.IndexOf(this);
1604 wxCHECK_RET( pos
!= wxNOT_FOUND
,
1605 _T("menuitem not found in the menu items list?") );
1607 // get the radio group range
1611 if ( m_isRadioGroupStart
)
1613 // we already have all information we need
1615 end
= m_radioGroup
.end
;
1617 else // next radio group item
1619 // get the radio group end from the start item
1620 start
= m_radioGroup
.start
;
1621 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
1624 // also uncheck all the other items in this radio group
1625 wxMenuItemIter node
= items
.Item(start
);
1626 for ( int n
= start
; n
<= end
&& node
; n
++ )
1630 node
->GetData()->m_isChecked
= false;
1632 node
= node
->GetNext();
1636 wxMenuItemBase::Check(check
);
1641 // radio group stuff
1642 // -----------------
1644 void wxMenuItem::SetAsRadioGroupStart()
1646 m_isRadioGroupStart
= true;
1649 void wxMenuItem::SetRadioGroupStart(int start
)
1651 wxASSERT_MSG( !m_isRadioGroupStart
,
1652 _T("should only be called for the next radio items") );
1654 m_radioGroup
.start
= start
;
1657 void wxMenuItem::SetRadioGroupEnd(int end
)
1659 wxASSERT_MSG( m_isRadioGroupStart
,
1660 _T("should only be called for the first radio item") );
1662 m_radioGroup
.end
= end
;
1665 // ----------------------------------------------------------------------------
1666 // wxMenuBar creation
1667 // ----------------------------------------------------------------------------
1669 void wxMenuBar::Init()
1677 m_shouldShowMenu
= false;
1680 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
1684 for (size_t i
= 0; i
< n
; ++i
)
1685 Append(menus
[i
], titles
[i
]);
1688 void wxMenuBar::Attach(wxFrame
*frame
)
1690 // maybe you really wanted to call Detach()?
1691 wxCHECK_RET( frame
, _T("wxMenuBar::Attach(NULL) called") );
1693 wxMenuBarBase::Attach(frame
);
1697 // reparent if necessary
1698 if ( m_frameLast
!= frame
)
1703 // show it back - was hidden by Detach()
1706 else // not created yet, do it now
1708 // we have no way to return the error from here anyhow :-(
1709 (void)Create(frame
, wxID_ANY
);
1711 SetCursor(wxCURSOR_ARROW
);
1713 SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT
));
1715 // calculate and set our height (it won't be changed any more)
1716 SetSize(wxDefaultCoord
, GetBestSize().y
);
1719 // remember the last frame which had us to avoid unnecessarily reparenting
1721 m_frameLast
= frame
;
1724 void wxMenuBar::Detach()
1726 // don't delete the window because we may be reattached later, just hide it
1732 wxMenuBarBase::Detach();
1735 wxMenuBar::~wxMenuBar()
1739 // ----------------------------------------------------------------------------
1740 // wxMenuBar adding/removing items
1741 // ----------------------------------------------------------------------------
1743 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1745 return Insert(GetCount(), menu
, title
);
1748 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1750 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1753 wxMenuInfo
*info
= new wxMenuInfo(title
);
1754 m_menuInfos
.Insert(info
, pos
);
1756 RefreshAllItemsAfter(pos
);
1761 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1763 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1767 wxMenuInfo
& info
= m_menuInfos
[pos
];
1769 info
.SetLabel(title
);
1771 // even if the old menu was disabled, the new one is not any more
1774 // even if we change only this one, the new label has different width,
1775 // so we need to refresh everything beyond this item as well
1776 RefreshAllItemsAfter(pos
);
1782 wxMenu
*wxMenuBar::Remove(size_t pos
)
1784 wxMenu
*menuOld
= wxMenuBarBase::Remove(pos
);
1788 m_menuInfos
.RemoveAt(pos
);
1790 // this doesn't happen too often, so don't try to be too smart - just
1791 // refresh everything
1798 // ----------------------------------------------------------------------------
1799 // wxMenuBar top level menus access
1800 // ----------------------------------------------------------------------------
1802 wxCoord
wxMenuBar::GetItemWidth(size_t pos
) const
1804 return m_menuInfos
[pos
].GetWidth(wxConstCast(this, wxMenuBar
));
1807 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1809 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1811 if ( enable
!= m_menuInfos
[pos
].IsEnabled() )
1813 m_menuInfos
[pos
].SetEnabled(enable
);
1817 //else: nothing to do
1820 bool wxMenuBar::IsEnabledTop(size_t pos
) const
1822 wxCHECK_MSG( pos
< GetCount(), false, _T("invalid index in IsEnabledTop") );
1824 return m_menuInfos
[pos
].IsEnabled();
1827 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1829 wxCHECK_RET( pos
< GetCount(), _T("invalid index in SetMenuLabel") );
1831 if ( label
!= m_menuInfos
[pos
].GetOriginalLabel() )
1833 m_menuInfos
[pos
].SetLabel(label
);
1837 //else: nothing to do
1840 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1842 wxCHECK_MSG( pos
< GetCount(), wxEmptyString
, _T("invalid index in GetMenuLabel") );
1844 return m_menuInfos
[pos
].GetOriginalLabel();
1847 // ----------------------------------------------------------------------------
1848 // wxMenuBar drawing
1849 // ----------------------------------------------------------------------------
1851 void wxMenuBar::RefreshAllItemsAfter(size_t pos
)
1855 // no need to refresh if nothing is shown yet
1859 wxRect rect
= GetItemRect(pos
);
1860 rect
.width
= GetClientSize().x
- rect
.x
;
1864 void wxMenuBar::RefreshItem(size_t pos
)
1866 wxCHECK_RET( pos
!= (size_t)-1,
1867 _T("invalid item in wxMenuBar::RefreshItem") );
1871 // no need to refresh if nothing is shown yet
1875 RefreshRect(GetItemRect(pos
));
1878 void wxMenuBar::DoDraw(wxControlRenderer
*renderer
)
1880 wxDC
& dc
= renderer
->GetDC();
1881 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1883 // redraw only the items which must be redrawn
1885 // we don't have to use GetUpdateClientRect() here because our client rect
1886 // is the same as total one
1887 wxRect rectUpdate
= GetUpdateRegion().GetBox();
1889 int flagsMenubar
= GetStateFlags();
1893 rect
.height
= GetClientSize().y
;
1896 size_t count
= GetCount();
1897 for ( size_t n
= 0; n
< count
; n
++ )
1899 if ( x
> rectUpdate
.GetRight() )
1901 // all remaining items are to the right of rectUpdate
1906 rect
.width
= GetItemWidth(n
);
1908 if ( x
< rectUpdate
.x
)
1910 // this item is still to the left of rectUpdate
1914 int flags
= flagsMenubar
;
1915 if ( m_current
!= -1 && n
== (size_t)m_current
)
1917 flags
|= wxCONTROL_SELECTED
;
1920 if ( !IsEnabledTop(n
) )
1922 flags
|= wxCONTROL_DISABLED
;
1925 GetRenderer()->DrawMenuBarItem
1929 m_menuInfos
[n
].GetLabel(),
1931 m_menuInfos
[n
].GetAccelIndex()
1936 // ----------------------------------------------------------------------------
1937 // wxMenuBar geometry
1938 // ----------------------------------------------------------------------------
1940 wxRect
wxMenuBar::GetItemRect(size_t pos
) const
1942 wxASSERT_MSG( pos
< GetCount(), _T("invalid menu bar item index") );
1943 wxASSERT_MSG( IsCreated(), _T("can't call this method yet") );
1948 rect
.height
= GetClientSize().y
;
1950 for ( size_t n
= 0; n
< pos
; n
++ )
1952 rect
.x
+= GetItemWidth(n
);
1955 rect
.width
= GetItemWidth(pos
);
1960 wxSize
wxMenuBar::DoGetBestClientSize() const
1963 if ( GetMenuCount() > 0 )
1965 wxClientDC
dc(wxConstCast(this, wxMenuBar
));
1966 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1967 dc
.GetTextExtent(GetMenuLabel(0), &size
.x
, &size
.y
);
1969 // adjust for the renderer we use
1970 size
= GetRenderer()->GetMenuBarItemSize(size
);
1972 else // empty menubar
1978 // the width is arbitrary, of course, for horizontal menubar
1984 int wxMenuBar::GetMenuFromPoint(const wxPoint
& pos
) const
1986 if ( pos
.x
< 0 || pos
.y
< 0 || pos
.y
> GetClientSize().y
)
1991 size_t count
= GetCount();
1992 for ( size_t item
= 0; item
< count
; item
++ )
1994 x
+= GetItemWidth(item
);
2002 // to the right of the last menu item
2006 // ----------------------------------------------------------------------------
2007 // wxMenuBar menu operations
2008 // ----------------------------------------------------------------------------
2010 void wxMenuBar::SelectMenu(size_t pos
)
2013 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::SelectMenu"));
2019 void wxMenuBar::DoSelectMenu(size_t pos
)
2021 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in DoSelectMenu") );
2023 int posOld
= m_current
;
2029 // close the previous menu
2030 if ( IsShowingMenu() )
2032 // restore m_shouldShowMenu flag after DismissMenu() which resets
2034 bool old
= m_shouldShowMenu
;
2038 m_shouldShowMenu
= old
;
2041 RefreshItem((size_t)posOld
);
2047 void wxMenuBar::PopupMenu(size_t pos
)
2049 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in PopupCurrentMenu") );
2056 // ----------------------------------------------------------------------------
2057 // wxMenuBar input handing
2058 // ----------------------------------------------------------------------------
2061 Note that wxMenuBar doesn't use wxInputHandler but handles keyboard and
2062 mouse in the same way under all platforms. This is because it doesn't derive
2063 from wxControl (which works with input handlers) but directly from wxWindow.
2065 Also, menu bar input handling is rather simple, so maybe it's not really
2066 worth making it themeable - at least I've decided against doing it now as it
2067 would merging the changes back into trunk more difficult. But it still could
2068 be done later if really needed.
2071 void wxMenuBar::OnKillFocus(wxFocusEvent
& event
)
2073 if ( m_current
!= -1 )
2075 RefreshItem((size_t)m_current
);
2083 void wxMenuBar::OnLeftDown(wxMouseEvent
& event
)
2091 else // we didn't have mouse capture, capture it now
2093 m_current
= GetMenuFromPoint(event
.GetPosition());
2094 if ( m_current
== -1 )
2096 // unfortunately, we can't prevent wxMSW from giving us the focus,
2097 // so we can only give it back
2102 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::OnLeftDown"));
2105 // show it as selected
2106 RefreshItem((size_t)m_current
);
2109 PopupCurrentMenu(false /* don't select first item - as Windows does */);
2114 void wxMenuBar::OnMouseMove(wxMouseEvent
& event
)
2118 (void)ProcessMouseEvent(event
.GetPosition());
2126 bool wxMenuBar::ProcessMouseEvent(const wxPoint
& pt
)
2128 // a hack to ignore the extra mouse events MSW sends us: this is similar to
2129 // wxUSE_MOUSEEVENT_HACK in wxWin itself but it isn't enough for us here as
2130 // we get the messages from different windows (old and new popup menus for
2133 static wxPoint s_ptLast
;
2134 if ( pt
== s_ptLast
)
2142 int currentNew
= GetMenuFromPoint(pt
);
2143 if ( (currentNew
== -1) || (currentNew
== m_current
) )
2148 // select the new active item
2149 DoSelectMenu(currentNew
);
2151 // show the menu if we know that we should, even if we hadn't been showing
2152 // it before (this may happen if the previous menu was disabled)
2153 if ( m_shouldShowMenu
&& !m_menuShown
)
2155 // open the new menu if the old one we closed had been opened
2156 PopupCurrentMenu(false /* don't select first item - as Windows does */);
2162 void wxMenuBar::OnKeyDown(wxKeyEvent
& event
)
2164 // ensure that we have a current item - we might not have it if we're
2165 // given the focus with Alt or F10 press (and under GTK+ the menubar
2166 // somehow gets the keyboard events even when it doesn't have focus...)
2167 if ( m_current
== -1 )
2169 if ( !HasCapture() )
2173 else // we do have capture
2175 // we always maintain a valid current item while we're in modal
2176 // state (i.e. have the capture)
2177 wxFAIL_MSG( _T("how did we manage to lose current item?") );
2183 int key
= event
.GetKeyCode();
2185 // first let the menu have it
2186 if ( IsShowingMenu() && m_menuShown
->ProcessKeyDown(key
) )
2191 // cycle through the menu items when left/right arrows are pressed and open
2192 // the menu when up/down one is
2196 // Alt must be processed at wxWindow level too
2201 // remove the selection and give the focus away
2202 if ( m_current
!= -1 )
2204 if ( IsShowingMenu() )
2216 size_t count
= GetCount();
2219 // the item won't change anyhow
2222 //else: otherwise, it will
2224 // remember if we were showing a menu - if we did, we should
2225 // show the new menu after changing the item
2226 bool wasMenuOpened
= IsShowingMenu();
2227 if ( wasMenuOpened
)
2232 // cast is safe as we tested for -1 above
2233 size_t currentNew
= (size_t)m_current
;
2235 if ( key
== WXK_LEFT
)
2237 if ( currentNew
-- == 0 )
2238 currentNew
= count
- 1;
2242 if ( ++currentNew
== count
)
2246 DoSelectMenu(currentNew
);
2248 if ( wasMenuOpened
)
2263 // letters open the corresponding menu
2266 int idxFound
= FindNextItemForAccel(m_current
, key
, &unique
);
2268 if ( idxFound
!= -1 )
2270 if ( IsShowingMenu() )
2275 DoSelectMenu((size_t)idxFound
);
2277 // if the item is not unique, just select it but don't
2278 // activate as the user might have wanted to activate
2281 // also, don't try to open a disabled menu
2282 if ( unique
&& IsEnabledTop((size_t)idxFound
) )
2288 // skip the "event.Skip()" below
2297 // ----------------------------------------------------------------------------
2298 // wxMenuBar accel handling
2299 // ----------------------------------------------------------------------------
2301 int wxMenuBar::FindNextItemForAccel(int idxStart
, int key
, bool *unique
) const
2303 if ( !wxIsalnum((wxChar
)key
) )
2305 // we only support letters/digits as accels
2309 // do we have more than one item with this accel?
2313 // translate everything to lower case before comparing
2314 wxChar chAccel
= (wxChar
)wxTolower(key
);
2316 // the index of the item with this accel
2319 // loop through all items searching for the item with this
2320 // accel starting at the item after the current one
2321 int count
= GetCount();
2322 int n
= idxStart
== -1 ? 0 : idxStart
+ 1;
2333 const wxMenuInfo
& info
= m_menuInfos
[n
];
2335 int idxAccel
= info
.GetAccelIndex();
2336 if ( idxAccel
!= -1 &&
2337 (wxChar
)wxTolower(info
.GetLabel()[(size_t)idxAccel
]) == chAccel
)
2339 // ok, found an item with this accel
2340 if ( idxFound
== -1 )
2342 // store it but continue searching as we need to
2343 // know if it's the only item with this accel or if
2347 else // we already had found such item
2352 // no need to continue further, we won't find
2353 // anything we don't already know
2358 // we want to iterate over all items wrapping around if
2366 if ( n
== idxStart
)
2368 // we've seen all items
2378 bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent
& event
)
2381 for ( wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
2383 node
= node
->GetNext(), n
++ )
2385 // accels of the items in the disabled menus shouldn't work
2386 if ( m_menuInfos
[n
].IsEnabled() )
2388 if ( node
->GetData()->ProcessAccelEvent(event
) )
2390 // menu processed it
2400 #endif // wxUSE_ACCEL
2402 // ----------------------------------------------------------------------------
2403 // wxMenuBar menus showing
2404 // ----------------------------------------------------------------------------
2406 void wxMenuBar::PopupCurrentMenu(bool selectFirst
)
2408 wxCHECK_RET( m_current
!= -1, _T("no menu to popup") );
2410 // forgot to call DismissMenu()?
2411 wxASSERT_MSG( !m_menuShown
, _T("shouldn't show two menus at once!") );
2413 // in any case, we should show it - even if we won't
2414 m_shouldShowMenu
= true;
2416 if ( IsEnabledTop(m_current
) )
2418 // remember the menu we show
2419 m_menuShown
= GetMenu(m_current
);
2421 // we don't show the menu at all if it has no items
2422 if ( !m_menuShown
->IsEmpty() )
2424 // position it correctly: note that we must use screen coords and
2425 // that we pass 0 as width to position the menu exactly below the
2426 // item, not to the right of it
2427 wxRect rectItem
= GetItemRect(m_current
);
2429 m_menuShown
->SetInvokingWindow(m_frameLast
);
2431 m_menuShown
->Popup(ClientToScreen(rectItem
.GetPosition()),
2432 wxSize(0, rectItem
.GetHeight()),
2437 // reset it back as no menu is shown
2441 //else: don't show disabled menu
2444 void wxMenuBar::DismissMenu()
2446 wxCHECK_RET( m_menuShown
, _T("can't dismiss menu if none is shown") );
2448 m_menuShown
->Dismiss();
2452 void wxMenuBar::OnDismissMenu(bool dismissMenuBar
)
2454 m_shouldShowMenu
= false;
2456 if ( dismissMenuBar
)
2462 void wxMenuBar::OnDismiss()
2464 if ( ReleaseMouseCapture() )
2465 wxLogTrace(_T("mousecapture"), _T("Releasing mouse from wxMenuBar::OnDismiss"));
2467 if ( m_current
!= -1 )
2469 size_t current
= m_current
;
2472 RefreshItem(current
);
2478 bool wxMenuBar::ReleaseMouseCapture()
2481 // With wxX11, when a menu is closed by clicking away from it, a control
2482 // under the click will still get an event, even though the menu has the
2483 // capture (bug?). So that control may already have taken the capture by
2484 // this point, preventing us from releasing the menu's capture. So to work
2485 // around this, we release both captures, then put back the control's
2487 wxWindow
*capture
= GetCapture();
2490 capture
->ReleaseMouse();
2492 if ( capture
== this )
2495 bool had
= HasCapture();
2500 capture
->CaptureMouse();
2514 void wxMenuBar::GiveAwayFocus()
2516 GetFrame()->SetFocus();
2519 // ----------------------------------------------------------------------------
2520 // popup menu support
2521 // ----------------------------------------------------------------------------
2523 wxEventLoop
*wxWindow::ms_evtLoopPopup
= NULL
;
2525 bool wxWindow::DoPopupMenu(wxMenu
*menu
, int x
, int y
)
2527 wxCHECK_MSG( !ms_evtLoopPopup
, false,
2528 _T("can't show more than one popup menu at a time") );
2531 // we need to change the cursor before showing the menu as, apparently, no
2532 // cursor changes took place while the mouse is captured
2533 wxCursor cursorOld
= GetCursor();
2534 SetCursor(wxCURSOR_ARROW
);
2538 // flash any delayed log messages before showing the menu, otherwise it
2539 // could be dismissed (because it would lose focus) immediately after being
2541 wxLog::FlushActive();
2543 // some controls update themselves from OnIdle() call - let them do it
2544 wxTheApp
->ProcessIdle();
2546 // if the window hadn't been refreshed yet, the menu can adversely affect
2547 // its next OnPaint() handler execution - i.e. scrolled window refresh
2548 // logic breaks then as it scrolls part of the menu which hadn't been there
2549 // when the update event was generated into view
2553 menu
->SetInvokingWindow(this);
2555 // wxLogDebug( "Name of invoking window %s", menu->GetInvokingWindow()->GetName().c_str() );
2557 menu
->Popup(ClientToScreen(wxPoint(x
, y
)), wxSize(0,0));
2559 // this is not very useful if the menu was popped up because of the mouse
2560 // click but I think it is nice to do when it appears because of a key
2561 // press (i.e. Windows menu key)
2563 // Windows itself doesn't do it, but IMHO this is nice
2566 // we have to redirect all keyboard input to the menu temporarily
2567 PushEventHandler(new wxMenuKbdRedirector(menu
));
2569 // enter the local modal loop
2570 ms_evtLoopPopup
= new wxEventLoop
;
2571 ms_evtLoopPopup
->Run();
2573 delete ms_evtLoopPopup
;
2574 ms_evtLoopPopup
= NULL
;
2576 // remove the handler
2577 PopEventHandler(true /* delete it */);
2579 menu
->SetInvokingWindow(NULL
);
2582 SetCursor(cursorOld
);
2588 void wxWindow::DismissPopupMenu()
2590 wxCHECK_RET( ms_evtLoopPopup
, _T("no popup menu shown") );
2592 ms_evtLoopPopup
->Exit();
2595 #endif // wxUSE_MENUS