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 // remember the accel char (may be -1 if none)
71 m_indexAccel
= wxControl::FindAccelIndex(text
, &m_label
);
73 // calculate the width later, after the menu bar is created
77 void SetEnabled(bool enabled
= true) { m_isEnabled
= enabled
; }
81 const wxString
& GetLabel() const { return m_label
; }
82 bool IsEnabled() const { return m_isEnabled
; }
83 wxCoord
GetWidth(wxMenuBar
*menubar
) const
87 wxConstCast(this, wxMenuInfo
)->CalcWidth(menubar
);
93 int GetAccelIndex() const { return m_indexAccel
; }
96 void CalcWidth(wxMenuBar
*menubar
)
99 wxClientDC
dc(menubar
);
100 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
101 dc
.GetTextExtent(m_label
, &size
.x
, &size
.y
);
103 // adjust for the renderer we use and store the width
104 m_width
= menubar
->GetRenderer()->GetMenuBarItemSize(size
).x
;
113 #include "wx/arrimpl.cpp"
115 WX_DEFINE_OBJARRAY(wxMenuInfoArray
);
117 // ----------------------------------------------------------------------------
118 // wxPopupMenuWindow: a popup window showing a menu
119 // ----------------------------------------------------------------------------
121 class wxPopupMenuWindow
: public wxPopupTransientWindow
124 wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
);
126 ~wxPopupMenuWindow();
128 // override the base class version to select the first item initially
129 virtual void Popup(wxWindow
*focus
= NULL
);
131 // override the base class version to dismiss any open submenus
132 virtual void Dismiss();
134 // called when a submenu is dismissed
135 void OnSubmenuDismiss(bool dismissParent
);
137 // the default wxMSW wxPopupTransientWindow::OnIdle disables the capture
138 // when the cursor is inside the popup, which dsables the menu tracking
139 // so override it to do nothing
141 void OnIdle(wxIdleEvent
& WXUNUSED(event
)) { }
144 // get the currently selected item (may be NULL)
145 wxMenuItem
*GetCurrentItem() const
147 return m_nodeCurrent
? m_nodeCurrent
->GetData() : NULL
;
150 // find the menu item at given position
151 wxMenuItemIter
GetMenuItemFromPoint(const wxPoint
& pt
) const;
153 // refresh the given item
154 void RefreshItem(wxMenuItem
*item
);
156 // preselect the first item
157 void SelectFirst() { SetCurrentItem(m_menu
->GetMenuItems().GetFirst()); }
159 // process the key event, return true if done
160 bool ProcessKeyDown(int key
);
162 // process mouse move event
163 void ProcessMouseMove(const wxPoint
& pt
);
165 // don't dismiss the popup window if the parent menu was clicked
166 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
169 // how did we perform this operation?
176 // notify the menu when the window disappears from screen
177 virtual void OnDismiss();
179 // draw the menu inside this window
180 virtual void DoDraw(wxControlRenderer
*renderer
);
183 void OnLeftUp(wxMouseEvent
& event
);
184 void OnMouseMove(wxMouseEvent
& event
);
185 void OnMouseLeave(wxMouseEvent
& event
);
186 void OnKeyDown(wxKeyEvent
& event
);
188 // reset the current item and node
191 // set the current node and item without refreshing anything
192 void SetCurrentItem(wxMenuItemIter node
);
194 // change the current item refreshing the old and new items
195 void ChangeCurrent(wxMenuItemIter node
);
197 // activate item, i.e. call either ClickItem() or OpenSubmenu() depending
198 // on what it is, return true if something was done (i.e. it's not a
200 bool ActivateItem(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
202 // send the event about the item click
203 void ClickItem(wxMenuItem
*item
);
205 // show the submenu for this item
206 void OpenSubmenu(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
208 // can this tiem be opened?
209 bool CanOpen(wxMenuItem
*item
)
211 return item
&& item
->IsEnabled() && item
->IsSubMenu();
214 // dismiss the menu and all parent menus too
215 void DismissAndNotify();
217 // react to dimissing this menu and also dismiss the parent if
219 void HandleDismiss(bool dismissParent
);
221 // do we have an open submenu?
222 bool HasOpenSubmenu() const { return m_hasOpenSubMenu
; }
224 // get previous node after the current one
225 wxMenuItemIter
GetPrevNode() const;
227 // get previous node before the given one, wrapping if it's the first one
228 wxMenuItemIter
GetPrevNode(wxMenuItemIter node
) const;
230 // get next node after the current one
231 wxMenuItemIter
GetNextNode() const;
233 // get next node after the given one, wrapping if it's the last one
234 wxMenuItemIter
GetNextNode(wxMenuItemIter node
) const;
240 // the menu node corresponding to the current item
241 wxMenuItemIter m_nodeCurrent
;
243 // do we currently have an opened submenu?
244 bool m_hasOpenSubMenu
;
246 DECLARE_EVENT_TABLE()
249 // ----------------------------------------------------------------------------
250 // wxMenuKbdRedirector: an event handler which redirects kbd input to wxMenu
251 // ----------------------------------------------------------------------------
253 class wxMenuKbdRedirector
: public wxEvtHandler
256 wxMenuKbdRedirector(wxMenu
*menu
) { m_menu
= menu
; }
258 virtual bool ProcessEvent(wxEvent
& event
)
260 if ( event
.GetEventType() == wxEVT_KEY_DOWN
)
262 return m_menu
->ProcessKeyDown(((wxKeyEvent
&)event
).GetKeyCode());
268 return wxEvtHandler::ProcessEvent(event
);
276 // ----------------------------------------------------------------------------
278 // ----------------------------------------------------------------------------
280 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
281 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
282 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
284 BEGIN_EVENT_TABLE(wxPopupMenuWindow
, wxPopupTransientWindow
)
285 EVT_KEY_DOWN(wxPopupMenuWindow::OnKeyDown
)
287 EVT_LEFT_UP(wxPopupMenuWindow::OnLeftUp
)
288 EVT_MOTION(wxPopupMenuWindow::OnMouseMove
)
289 EVT_LEAVE_WINDOW(wxPopupMenuWindow::OnMouseLeave
)
291 EVT_IDLE(wxPopupMenuWindow::OnIdle
)
295 BEGIN_EVENT_TABLE(wxMenuBar
, wxMenuBarBase
)
296 EVT_KILL_FOCUS(wxMenuBar::OnKillFocus
)
298 EVT_KEY_DOWN(wxMenuBar::OnKeyDown
)
300 EVT_LEFT_DOWN(wxMenuBar::OnLeftDown
)
301 EVT_MOTION(wxMenuBar::OnMouseMove
)
304 // ============================================================================
306 // ============================================================================
308 // ----------------------------------------------------------------------------
310 // ----------------------------------------------------------------------------
312 wxPopupMenuWindow::wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
)
315 m_hasOpenSubMenu
= false;
319 (void)Create(parent
, wxBORDER_RAISED
);
321 SetCursor(wxCURSOR_ARROW
);
324 wxPopupMenuWindow::~wxPopupMenuWindow()
326 // When m_popupMenu in wxMenu is deleted because it
327 // is a child of an old menu bar being deleted (note: it does
328 // not get destroyed by the wxMenu destructor, but
329 // by DestroyChildren()), m_popupMenu should be reset to NULL.
331 m_menu
->m_popupMenu
= NULL
;
334 // ----------------------------------------------------------------------------
335 // wxPopupMenuWindow current item/node handling
336 // ----------------------------------------------------------------------------
338 void wxPopupMenuWindow::ResetCurrent()
340 SetCurrentItem(wxMenuItemIter());
343 void wxPopupMenuWindow::SetCurrentItem(wxMenuItemIter node
)
345 m_nodeCurrent
= node
;
348 void wxPopupMenuWindow::ChangeCurrent(wxMenuItemIter node
)
350 if ( !m_nodeCurrent
|| (node
!= m_nodeCurrent
) )
352 wxMenuItemIter nodeOldCurrent
= m_nodeCurrent
;
354 m_nodeCurrent
= node
;
356 if ( nodeOldCurrent
)
358 wxMenuItem
*item
= nodeOldCurrent
->GetData();
359 wxCHECK_RET( item
, _T("no current item?") );
361 // if it was the currently opened menu, close it
362 if ( item
->IsSubMenu() && item
->GetSubMenu()->IsShown() )
364 item
->GetSubMenu()->Dismiss();
365 OnSubmenuDismiss( false );
372 RefreshItem(m_nodeCurrent
->GetData());
376 wxMenuItemIter
wxPopupMenuWindow::GetPrevNode() const
378 // return the last node if there had been no previously selected one
379 return m_nodeCurrent
? GetPrevNode(m_nodeCurrent
)
380 : wxMenuItemIter(m_menu
->GetMenuItems().GetLast());
384 wxPopupMenuWindow::GetPrevNode(wxMenuItemIter node
) const
388 node
= node
->GetPrevious();
391 node
= m_menu
->GetMenuItems().GetLast();
394 //else: the menu is empty
399 wxMenuItemIter
wxPopupMenuWindow::GetNextNode() const
401 // return the first node if there had been no previously selected one
402 return m_nodeCurrent
? GetNextNode(m_nodeCurrent
)
403 : wxMenuItemIter(m_menu
->GetMenuItems().GetFirst());
407 wxPopupMenuWindow::GetNextNode(wxMenuItemIter node
) const
411 node
= node
->GetNext();
414 node
= m_menu
->GetMenuItems().GetFirst();
417 //else: the menu is empty
422 // ----------------------------------------------------------------------------
423 // wxPopupMenuWindow popup/dismiss
424 // ----------------------------------------------------------------------------
426 void wxPopupMenuWindow::Popup(wxWindow
*focus
)
428 // check that the current item had been properly reset before
429 wxASSERT_MSG( !m_nodeCurrent
||
430 m_nodeCurrent
== m_menu
->GetMenuItems().GetFirst(),
431 _T("menu current item preselected incorrectly") );
433 wxPopupTransientWindow::Popup(focus
);
435 // the base class no-longer captures the mouse automatically when Popup
436 // is called, so do it here to allow the menu tracking to work
441 // ensure that this window is really on top of everything: without using
442 // SetWindowPos() it can be covered by its parent menu which is not
443 // really what we want
444 wxMenu
*menuParent
= m_menu
->GetParent();
447 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
449 // if we're shown, the parent menu must be also shown
450 wxCHECK_RET( win
, _T("parent menu is not shown?") );
452 if ( !::SetWindowPos(GetHwndOf(win
), GetHwnd(),
454 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOREDRAW
) )
456 wxLogLastError(_T("SetWindowPos(HWND_TOP)"));
464 void wxPopupMenuWindow::Dismiss()
466 if ( HasOpenSubmenu() )
468 wxMenuItem
*item
= GetCurrentItem();
469 wxCHECK_RET( item
&& item
->IsSubMenu(), _T("where is our open submenu?") );
471 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
472 wxCHECK_RET( win
, _T("opened submenu is not opened?") );
475 OnSubmenuDismiss( false );
478 wxPopupTransientWindow::Dismiss();
483 void wxPopupMenuWindow::OnDismiss()
485 // when we are dismissed because the user clicked elsewhere or we lost
486 // focus in any other way, hide the parent menu as well
490 void wxPopupMenuWindow::OnSubmenuDismiss(bool WXUNUSED(dismissParent
))
492 m_hasOpenSubMenu
= false;
495 void wxPopupMenuWindow::HandleDismiss(bool dismissParent
)
497 m_menu
->OnDismiss(dismissParent
);
500 void wxPopupMenuWindow::DismissAndNotify()
506 // ----------------------------------------------------------------------------
507 // wxPopupMenuWindow geometry
508 // ----------------------------------------------------------------------------
511 wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint
& pt
) const
513 // we only use the y coord normally, but still check x in case the point is
514 // outside the window completely
515 if ( wxWindow::HitTest(pt
) == wxHT_WINDOW_INSIDE
)
518 for ( wxMenuItemIter node
= m_menu
->GetMenuItems().GetFirst();
520 node
= node
->GetNext() )
522 wxMenuItem
*item
= node
->GetData();
523 y
+= item
->GetHeight();
532 return wxMenuItemIter();
535 // ----------------------------------------------------------------------------
536 // wxPopupMenuWindow drawing
537 // ----------------------------------------------------------------------------
539 void wxPopupMenuWindow::RefreshItem(wxMenuItem
*item
)
541 wxCHECK_RET( item
, _T("can't refresh NULL item") );
543 wxASSERT_MSG( IsShown(), _T("can't refresh menu which is not shown") );
545 // FIXME: -1 here because of SetLogicalOrigin(1, 1) in DoDraw()
546 RefreshRect(wxRect(0, item
->GetPosition() - 1,
547 m_menu
->GetGeometryInfo().GetSize().x
, item
->GetHeight()));
550 void wxPopupMenuWindow::DoDraw(wxControlRenderer
*renderer
)
552 // no clipping so far - do we need it? I don't think so as the menu is
553 // never partially covered as it is always on top of everything
555 wxDC
& dc
= renderer
->GetDC();
556 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
558 // FIXME: this should be done in the renderer, however when it is fixed
559 // wxPopupMenuWindow::RefreshItem() should be changed too!
560 dc
.SetLogicalOrigin(1, 1);
562 wxRenderer
*rend
= renderer
->GetRenderer();
565 const wxMenuGeometryInfo
& gi
= m_menu
->GetGeometryInfo();
566 for ( wxMenuItemIter node
= m_menu
->GetMenuItems().GetFirst();
568 node
= node
->GetNext() )
570 wxMenuItem
*item
= node
->GetData();
572 if ( item
->IsSeparator() )
574 rend
->DrawMenuSeparator(dc
, y
, gi
);
576 else // not a separator
579 if ( item
->IsCheckable() )
581 flags
|= wxCONTROL_CHECKABLE
;
583 if ( item
->IsChecked() )
585 flags
|= wxCONTROL_CHECKED
;
589 if ( !item
->IsEnabled() )
590 flags
|= wxCONTROL_DISABLED
;
592 if ( item
->IsSubMenu() )
593 flags
|= wxCONTROL_ISSUBMENU
;
595 if ( item
== GetCurrentItem() )
596 flags
|= wxCONTROL_SELECTED
;
600 if ( !item
->IsEnabled() )
602 bmp
= item
->GetDisabledBitmap();
607 // strangely enough, for unchecked item we use the
608 // "checked" bitmap because this is the default one - this
609 // explains this strange boolean expression
610 bmp
= item
->GetBitmap(!item
->IsCheckable() || item
->IsChecked());
619 item
->GetAccelString(),
622 item
->GetAccelIndex()
626 y
+= item
->GetHeight();
630 // ----------------------------------------------------------------------------
631 // wxPopupMenuWindow actions
632 // ----------------------------------------------------------------------------
634 void wxPopupMenuWindow::ClickItem(wxMenuItem
*item
)
636 wxCHECK_RET( item
, _T("can't click NULL item") );
638 wxASSERT_MSG( !item
->IsSeparator() && !item
->IsSubMenu(),
639 _T("can't click this item") );
641 wxMenu
* menu
= m_menu
;
646 menu
->ClickItem(item
);
649 void wxPopupMenuWindow::OpenSubmenu(wxMenuItem
*item
, InputMethod how
)
651 wxCHECK_RET( item
, _T("can't open NULL submenu") );
653 wxMenu
*submenu
= item
->GetSubMenu();
654 wxCHECK_RET( submenu
, _T("can only open submenus!") );
656 // FIXME: should take into account the border width
657 submenu
->Popup(ClientToScreen(wxPoint(0, item
->GetPosition())),
658 wxSize(m_menu
->GetGeometryInfo().GetSize().x
, 0),
659 how
== WithKeyboard
/* preselect first item then */);
661 m_hasOpenSubMenu
= true;
664 bool wxPopupMenuWindow::ActivateItem(wxMenuItem
*item
, InputMethod how
)
666 // don't activate disabled items
667 if ( !item
|| !item
->IsEnabled() )
672 // normal menu items generate commands, submenus can be opened and
673 // the separators don't do anything
674 if ( item
->IsSubMenu() )
676 OpenSubmenu(item
, how
);
678 else if ( !item
->IsSeparator() )
682 else // separator, can't activate
690 // ----------------------------------------------------------------------------
691 // wxPopupMenuWindow input handling
692 // ----------------------------------------------------------------------------
694 bool wxPopupMenuWindow::ProcessLeftDown(wxMouseEvent
& event
)
696 // wxPopupWindowHandler dismisses the window when the mouse is clicked
697 // outside it which is usually just fine, but there is one case when we
698 // don't want to do it: if the mouse was clicked on the parent submenu item
699 // which opens this menu, so check for it
701 wxPoint pos
= event
.GetPosition();
702 if ( HitTest(pos
.x
, pos
.y
) == wxHT_WINDOW_OUTSIDE
)
704 wxMenu
*menu
= m_menu
->GetParent();
707 wxPopupMenuWindow
*win
= menu
->m_popupMenu
;
709 wxCHECK_MSG( win
, false, _T("parent menu not shown?") );
711 pos
= ClientToScreen(pos
);
712 if ( win
->GetMenuItemFromPoint(win
->ScreenToClient(pos
)) )
717 //else: it is outside the parent menu as well, do dismiss this one
724 void wxPopupMenuWindow::OnLeftUp(wxMouseEvent
& event
)
726 wxMenuItemIter node
= GetMenuItemFromPoint(event
.GetPosition());
729 ActivateItem(node
->GetData(), WithMouse
);
733 void wxPopupMenuWindow::OnMouseMove(wxMouseEvent
& event
)
735 const wxPoint pt
= event
.GetPosition();
737 // we need to ignore extra mouse events: example when this happens is when
738 // the mouse is on the menu and we open a submenu from keyboard - Windows
739 // then sends us a dummy mouse move event, we (correctly) determine that it
740 // happens in the parent menu and so immediately close the just opened
743 static wxPoint s_ptLast
;
744 wxPoint ptCur
= ClientToScreen(pt
);
745 if ( ptCur
== s_ptLast
)
753 ProcessMouseMove(pt
);
758 void wxPopupMenuWindow::ProcessMouseMove(const wxPoint
& pt
)
760 wxMenuItemIter node
= GetMenuItemFromPoint(pt
);
762 // don't reset current to NULL here, we only do it when the mouse leaves
763 // the window (see below)
766 if ( !m_nodeCurrent
|| (node
!= m_nodeCurrent
) )
770 wxMenuItem
*item
= GetCurrentItem();
773 OpenSubmenu(item
, WithMouse
);
776 //else: same item, nothing to do
778 else // not on an item
780 // the last open submenu forwards the mouse move messages to its
781 // parent, so if the mouse moves to another item of the parent menu,
782 // this menu is closed and this other item is selected - in the similar
783 // manner, the top menu forwards the mouse moves to the menubar which
784 // allows to select another top level menu by just moving the mouse
786 // we need to translate our client coords to the client coords of the
787 // window we forward this event to
788 wxPoint ptScreen
= ClientToScreen(pt
);
790 // if the mouse is outside this menu, let the parent one to
792 wxMenu
*menuParent
= m_menu
->GetParent();
795 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
797 // if we're shown, the parent menu must be also shown
798 wxCHECK_RET( win
, _T("parent menu is not shown?") );
800 win
->ProcessMouseMove(win
->ScreenToClient(ptScreen
));
802 else // no parent menu
804 wxMenuBar
*menubar
= m_menu
->GetMenuBar();
807 if ( menubar
->ProcessMouseEvent(
808 menubar
->ScreenToClient(ptScreen
)) )
810 // menubar has closed this menu and opened another one, probably
815 //else: top level popup menu, no other processing to do
819 void wxPopupMenuWindow::OnMouseLeave(wxMouseEvent
& event
)
821 // due to the artefact of mouse events generation under MSW, we actually
822 // may get the mouse leave event after the menu had been already dismissed
823 // and calling ChangeCurrent() would then assert, so don't do it
826 // we shouldn't change the current them if our submenu is opened and
827 // mouse moved there, in this case the submenu is responsable for
830 if ( HasOpenSubmenu() )
832 wxMenuItem
*item
= GetCurrentItem();
833 wxCHECK_RET( CanOpen(item
), _T("where is our open submenu?") );
835 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
836 wxCHECK_RET( win
, _T("submenu is opened but not shown?") );
838 // only handle this event if the mouse is not inside the submenu
839 wxPoint pt
= ClientToScreen(event
.GetPosition());
841 win
->HitTest(win
->ScreenToClient(pt
)) == wxHT_WINDOW_OUTSIDE
;
845 // this menu is the last opened
851 ChangeCurrent(wxMenuItemIter());
858 void wxPopupMenuWindow::OnKeyDown(wxKeyEvent
& event
)
860 wxMenuBar
*menubar
= m_menu
->GetMenuBar();
864 menubar
->ProcessEvent(event
);
866 else if ( !ProcessKeyDown(event
.GetKeyCode()) )
872 bool wxPopupMenuWindow::ProcessKeyDown(int key
)
874 wxMenuItem
*item
= GetCurrentItem();
876 // first let the opened submenu to have it (no test for IsEnabled() here,
877 // the keys navigate even in a disabled submenu if we had somehow managed
878 // to open it inspit of this)
879 if ( HasOpenSubmenu() )
881 wxCHECK_MSG( CanOpen(item
), false,
882 _T("has open submenu but another item selected?") );
884 if ( item
->GetSubMenu()->ProcessKeyDown(key
) )
888 bool processed
= true;
890 // handle the up/down arrows, home, end, esc and return here, pass the
891 // left/right arrows to the menu bar except when the right arrow can be
892 // used to open a submenu
896 // if we're not a top level menu, close us, else leave this to the
898 if ( !m_menu
->GetParent() )
907 // close just this menu
909 HandleDismiss(false);
913 processed
= ActivateItem(item
);
917 ChangeCurrent(m_menu
->GetMenuItems().GetFirst());
921 ChangeCurrent(m_menu
->GetMenuItems().GetLast());
927 bool up
= key
== WXK_UP
;
929 wxMenuItemIter nodeStart
= up
? GetPrevNode() : GetNextNode(),
931 while ( node
&& node
->GetData()->IsSeparator() )
933 node
= up
? GetPrevNode(node
) : GetNextNode(node
);
935 if ( node
== nodeStart
)
937 // nothing but separators and disabled items in this
939 node
= wxMenuItemIter();
955 // don't try to reopen an already opened menu
956 if ( !HasOpenSubmenu() && CanOpen(item
) )
967 // look for the menu item starting with this letter
968 if ( wxIsalnum((wxChar
)key
) )
970 // we want to start from the item after this one because
971 // if we're already on the item with the given accel we want to
972 // go to the next one, not to stay in place
973 wxMenuItemIter nodeStart
= GetNextNode();
975 // do we have more than one item with this accel?
976 bool notUnique
= false;
978 // translate everything to lower case before comparing
979 wxChar chAccel
= (wxChar
)wxTolower(key
);
981 // loop through all items searching for the item with this
983 wxMenuItemIter nodeFound
,
987 item
= node
->GetData();
989 int idxAccel
= item
->GetAccelIndex();
990 if ( idxAccel
!= -1 &&
991 wxTolower(item
->GetLabel()[(size_t)idxAccel
])
994 // ok, found an item with this accel
997 // store it but continue searching as we need to
998 // know if it's the only item with this accel or if
1002 else // we already had found such item
1006 // no need to continue further, we won't find
1007 // anything we don't already know
1012 // we want to iterate over all items wrapping around if
1014 node
= GetNextNode(node
);
1015 if ( node
== nodeStart
)
1017 // we've seen all nodes
1024 item
= nodeFound
->GetData();
1026 // go to this item anyhow
1027 ChangeCurrent(nodeFound
);
1029 if ( !notUnique
&& item
->IsEnabled() )
1031 // unique item with this accel - activate it
1032 processed
= ActivateItem(item
);
1034 //else: just select it but don't activate as the user might
1035 // have wanted to activate another item
1037 // skip "processed = false" below
1048 // ----------------------------------------------------------------------------
1050 // ----------------------------------------------------------------------------
1058 m_startRadioGroup
= -1;
1067 // ----------------------------------------------------------------------------
1068 // wxMenu and wxMenuGeometryInfo
1069 // ----------------------------------------------------------------------------
1071 wxMenuGeometryInfo::~wxMenuGeometryInfo()
1075 const wxMenuGeometryInfo
& wxMenu::GetGeometryInfo() const
1081 wxConstCast(this, wxMenu
)->m_geometry
=
1082 m_popupMenu
->GetRenderer()->GetMenuGeometry(m_popupMenu
, *this);
1086 wxFAIL_MSG( _T("can't get geometry without window") );
1093 void wxMenu::InvalidateGeometryInfo()
1102 // ----------------------------------------------------------------------------
1103 // wxMenu adding/removing items
1104 // ----------------------------------------------------------------------------
1106 void wxMenu::OnItemAdded(wxMenuItem
*item
)
1108 InvalidateGeometryInfo();
1112 #endif // wxUSE_ACCEL
1114 // the submenus of a popup menu should have the same invoking window as it
1116 if ( m_invokingWindow
&& item
->IsSubMenu() )
1118 item
->GetSubMenu()->SetInvokingWindow(m_invokingWindow
);
1122 void wxMenu::EndRadioGroup()
1124 // we're not inside a radio group any longer
1125 m_startRadioGroup
= -1;
1128 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
1130 if ( item
->GetKind() == wxITEM_RADIO
)
1132 int count
= GetMenuItemCount();
1134 if ( m_startRadioGroup
== -1 )
1136 // start a new radio group
1137 m_startRadioGroup
= count
;
1139 // for now it has just one element
1140 item
->SetAsRadioGroupStart();
1141 item
->SetRadioGroupEnd(m_startRadioGroup
);
1143 else // extend the current radio group
1145 // we need to update its end item
1146 item
->SetRadioGroupStart(m_startRadioGroup
);
1147 wxMenuItemIter node
= GetMenuItems().Item(m_startRadioGroup
);
1151 node
->GetData()->SetRadioGroupEnd(count
);
1155 wxFAIL_MSG( _T("where is the radio group start item?") );
1159 else // not a radio item
1164 if ( !wxMenuBase::DoAppend(item
) )
1172 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1174 if ( !wxMenuBase::DoInsert(pos
, item
) )
1182 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1184 wxMenuItem
*itemOld
= wxMenuBase::DoRemove(item
);
1188 InvalidateGeometryInfo();
1191 RemoveAccelFor(item
);
1192 #endif // wxUSE_ACCEL
1198 // ----------------------------------------------------------------------------
1199 // wxMenu attaching/detaching
1200 // ----------------------------------------------------------------------------
1202 void wxMenu::Attach(wxMenuBarBase
*menubar
)
1204 wxMenuBase::Attach(menubar
);
1206 wxCHECK_RET( m_menuBar
, _T("menubar can't be NULL after attaching") );
1208 // unfortunately, we can't use m_menuBar->GetEventHandler() here because,
1209 // if the menubar is currently showing a menu, its event handler is a
1210 // temporary one installed by wxPopupWindow and so will disappear soon any
1211 // any attempts to use it from the newly attached menu would result in a
1214 // so we use the menubar itself, even if it's a pity as it means we can't
1215 // redirect all menu events by changing the menubar handler (FIXME)
1216 SetNextHandler(m_menuBar
);
1219 void wxMenu::Detach()
1221 wxMenuBase::Detach();
1224 // ----------------------------------------------------------------------------
1225 // wxMenu misc functions
1226 // ----------------------------------------------------------------------------
1228 wxWindow
*wxMenu::GetRootWindow() const
1232 // simple case - a normal menu attached to the menubar
1233 return GetMenuBar();
1236 // we're a popup menu but the trouble is that only the top level popup menu
1237 // has a pointer to the invoking window, so we must walk up the menu chain
1239 wxWindow
*win
= GetInvokingWindow();
1242 // we already have it
1246 wxMenu
*menu
= GetParent();
1249 // We are a submenu of a menu of a menubar
1250 if (menu
->GetMenuBar())
1251 return menu
->GetMenuBar();
1253 win
= menu
->GetInvokingWindow();
1257 menu
= menu
->GetParent();
1260 // we're probably going to crash in the caller anyhow, but try to detect
1261 // this error as soon as possible
1262 wxASSERT_MSG( win
, _T("menu without any associated window?") );
1264 // also remember it in this menu so that we don't have to search for it the
1266 wxConstCast(this, wxMenu
)->m_invokingWindow
= win
;
1271 wxRenderer
*wxMenu::GetRenderer() const
1273 // we're going to crash without renderer!
1274 wxCHECK_MSG( m_popupMenu
, NULL
, _T("neither popup nor menubar menu?") );
1276 return m_popupMenu
->GetRenderer();
1279 void wxMenu::RefreshItem(wxMenuItem
*item
)
1281 // the item geometry changed, so our might have changed as well
1282 InvalidateGeometryInfo();
1286 // this would be a bug in IsShown()
1287 wxCHECK_RET( m_popupMenu
, _T("must have popup window if shown!") );
1289 // recalc geometry to update the item height and such
1290 (void)GetGeometryInfo();
1292 m_popupMenu
->RefreshItem(item
);
1296 // ----------------------------------------------------------------------------
1297 // wxMenu showing and hiding
1298 // ----------------------------------------------------------------------------
1300 bool wxMenu::IsShown() const
1302 return m_popupMenu
&& m_popupMenu
->IsShown();
1305 void wxMenu::OnDismiss(bool dismissParent
)
1309 // always notify the parent about submenu disappearance
1310 wxPopupMenuWindow
*win
= m_menuParent
->m_popupMenu
;
1313 win
->OnSubmenuDismiss( true );
1317 wxFAIL_MSG( _T("parent menu not shown?") );
1320 // and if we dismiss everything, propagate to parent
1321 if ( dismissParent
)
1323 // dismissParent is recursive
1324 m_menuParent
->Dismiss();
1325 m_menuParent
->OnDismiss(true);
1328 else // no parent menu
1330 // notify the menu bar if we're a top level menu
1333 m_menuBar
->OnDismissMenu(dismissParent
);
1337 wxCHECK_RET( m_invokingWindow
, _T("what kind of menu is this?") );
1339 m_invokingWindow
->DismissPopupMenu();
1341 // Why reset it here? We need it for sending the event to...
1342 // SetInvokingWindow(NULL);
1347 void wxMenu::Popup(const wxPoint
& pos
, const wxSize
& size
, bool selectFirst
)
1349 // create the popup window if not done yet
1352 m_popupMenu
= new wxPopupMenuWindow(GetRootWindow(), this);
1355 // select the first item unless disabled
1358 m_popupMenu
->SelectFirst();
1361 // the geometry might have changed since the last time we were shown, so
1363 m_popupMenu
->SetClientSize(GetGeometryInfo().GetSize());
1365 // position it as specified
1366 m_popupMenu
->Position(pos
, size
);
1368 // the menu can't have the focus itself (it is a Windows limitation), so
1369 // always keep the focus at the originating window
1370 wxWindow
*focus
= GetRootWindow();
1372 wxASSERT_MSG( focus
, _T("no window to keep focus on?") );
1375 m_popupMenu
->Popup(focus
);
1378 void wxMenu::Dismiss()
1380 wxCHECK_RET( IsShown(), _T("can't dismiss hidden menu") );
1382 m_popupMenu
->Dismiss();
1385 // ----------------------------------------------------------------------------
1386 // wxMenu event processing
1387 // ----------------------------------------------------------------------------
1389 bool wxMenu::ProcessKeyDown(int key
)
1391 wxCHECK_MSG( m_popupMenu
, false,
1392 _T("can't process key events if not shown") );
1394 return m_popupMenu
->ProcessKeyDown(key
);
1397 bool wxMenu::ClickItem(wxMenuItem
*item
)
1400 if ( item
->IsCheckable() )
1402 // update the item state
1403 isChecked
= !item
->IsChecked();
1405 item
->Check(isChecked
!= 0);
1413 return SendEvent(item
->GetId(), isChecked
);
1416 // ----------------------------------------------------------------------------
1417 // wxMenu accel support
1418 // ----------------------------------------------------------------------------
1422 bool wxMenu::ProcessAccelEvent(const wxKeyEvent
& event
)
1424 // do we have an item for this accel?
1425 wxMenuItem
*item
= m_accelTable
.GetMenuItem(event
);
1426 if ( item
&& item
->IsEnabled() )
1428 return ClickItem(item
);
1432 for ( wxMenuItemIter node
= GetMenuItems().GetFirst();
1434 node
= node
->GetNext() )
1436 const wxMenuItem
*item
= node
->GetData();
1437 if ( item
->IsSubMenu() && item
->IsEnabled() )
1440 if ( item
->GetSubMenu()->ProcessAccelEvent(event
) )
1450 void wxMenu::AddAccelFor(wxMenuItem
*item
)
1452 wxAcceleratorEntry
*accel
= item
->GetAccel();
1455 accel
->SetMenuItem(item
);
1457 m_accelTable
.Add(*accel
);
1463 void wxMenu::RemoveAccelFor(wxMenuItem
*item
)
1465 wxAcceleratorEntry
*accel
= item
->GetAccel();
1468 m_accelTable
.Remove(*accel
);
1474 #endif // wxUSE_ACCEL
1476 // ----------------------------------------------------------------------------
1477 // wxMenuItem construction
1478 // ----------------------------------------------------------------------------
1480 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
1482 const wxString
& text
,
1483 const wxString
& help
,
1486 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
1489 m_height
= wxDefaultCoord
;
1491 m_radioGroup
.start
= -1;
1492 m_isRadioGroupStart
= false;
1494 m_bmpDisabled
= wxNullBitmap
;
1499 wxMenuItem::~wxMenuItem()
1503 // ----------------------------------------------------------------------------
1504 // wxMenuItemBase methods implemented here
1505 // ----------------------------------------------------------------------------
1508 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
1510 const wxString
& name
,
1511 const wxString
& help
,
1515 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
1519 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
1521 return wxStripMenuCodes(text
);
1524 // ----------------------------------------------------------------------------
1525 // wxMenuItem operations
1526 // ----------------------------------------------------------------------------
1528 void wxMenuItem::NotifyMenu()
1530 m_parentMenu
->RefreshItem(this);
1533 void wxMenuItem::UpdateAccelInfo()
1535 m_indexAccel
= wxControl::FindAccelIndex(m_text
);
1537 // will be empty if the text contains no TABs - ok
1538 m_strAccel
= m_text
.AfterFirst(_T('\t'));
1541 void wxMenuItem::SetText(const wxString
& txt
)
1543 if ( txt
!= m_text
)
1545 wxString text
= txt
;
1548 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
1549 text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|wxSTOCK_WITH_MNEMONIC
);
1552 // first call the base class version to change m_text
1553 wxMenuItemBase::SetText(text
);
1561 void wxMenuItem::SetCheckable(bool checkable
)
1563 if ( checkable
!= IsCheckable() )
1565 wxMenuItemBase::SetCheckable(checkable
);
1571 void wxMenuItem::SetBitmaps(const wxBitmap
& bmpChecked
,
1572 const wxBitmap
& bmpUnchecked
)
1574 m_bmpChecked
= bmpChecked
;
1575 m_bmpUnchecked
= bmpUnchecked
;
1580 void wxMenuItem::Enable(bool enable
)
1582 if ( enable
!= m_isEnabled
)
1584 wxMenuItemBase::Enable(enable
);
1590 void wxMenuItem::Check(bool check
)
1592 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
1594 if ( m_isChecked
== check
)
1597 if ( GetKind() == wxITEM_RADIO
)
1599 // it doesn't make sense to uncheck a radio item - what would this do?
1603 // get the index of this item in the menu
1604 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
1605 int pos
= items
.IndexOf(this);
1606 wxCHECK_RET( pos
!= wxNOT_FOUND
,
1607 _T("menuitem not found in the menu items list?") );
1609 // get the radio group range
1613 if ( m_isRadioGroupStart
)
1615 // we already have all information we need
1617 end
= m_radioGroup
.end
;
1619 else // next radio group item
1621 // get the radio group end from the start item
1622 start
= m_radioGroup
.start
;
1623 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
1626 // also uncheck all the other items in this radio group
1627 wxMenuItemIter node
= items
.Item(start
);
1628 for ( int n
= start
; n
<= end
&& node
; n
++ )
1632 node
->GetData()->m_isChecked
= false;
1634 node
= node
->GetNext();
1638 wxMenuItemBase::Check(check
);
1643 // radio group stuff
1644 // -----------------
1646 void wxMenuItem::SetAsRadioGroupStart()
1648 m_isRadioGroupStart
= true;
1651 void wxMenuItem::SetRadioGroupStart(int start
)
1653 wxASSERT_MSG( !m_isRadioGroupStart
,
1654 _T("should only be called for the next radio items") );
1656 m_radioGroup
.start
= start
;
1659 void wxMenuItem::SetRadioGroupEnd(int end
)
1661 wxASSERT_MSG( m_isRadioGroupStart
,
1662 _T("should only be called for the first radio item") );
1664 m_radioGroup
.end
= end
;
1667 // ----------------------------------------------------------------------------
1668 // wxMenuBar creation
1669 // ----------------------------------------------------------------------------
1671 void wxMenuBar::Init()
1679 m_shouldShowMenu
= false;
1682 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
1686 for (size_t i
= 0; i
< n
; ++i
)
1687 Append(menus
[i
], titles
[i
]);
1690 void wxMenuBar::Attach(wxFrame
*frame
)
1692 // maybe you really wanted to call Detach()?
1693 wxCHECK_RET( frame
, _T("wxMenuBar::Attach(NULL) called") );
1695 wxMenuBarBase::Attach(frame
);
1699 // reparent if necessary
1700 if ( m_frameLast
!= frame
)
1705 // show it back - was hidden by Detach()
1708 else // not created yet, do it now
1710 // we have no way to return the error from here anyhow :-(
1711 (void)Create(frame
, wxID_ANY
);
1713 SetCursor(wxCURSOR_ARROW
);
1715 SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT
));
1717 // calculate and set our height (it won't be changed any more)
1718 SetSize(wxDefaultCoord
, GetBestSize().y
);
1721 // remember the last frame which had us to avoid unnecessarily reparenting
1723 m_frameLast
= frame
;
1726 void wxMenuBar::Detach()
1728 // don't delete the window because we may be reattached later, just hide it
1734 wxMenuBarBase::Detach();
1737 wxMenuBar::~wxMenuBar()
1741 // ----------------------------------------------------------------------------
1742 // wxMenuBar adding/removing items
1743 // ----------------------------------------------------------------------------
1745 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1747 return Insert(GetCount(), menu
, title
);
1750 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1752 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1755 wxMenuInfo
*info
= new wxMenuInfo(title
);
1756 m_menuInfos
.Insert(info
, pos
);
1758 RefreshAllItemsAfter(pos
);
1763 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1765 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1769 wxMenuInfo
& info
= m_menuInfos
[pos
];
1771 info
.SetLabel(title
);
1773 // even if the old menu was disabled, the new one is not any more
1776 // even if we change only this one, the new label has different width,
1777 // so we need to refresh everything beyond this item as well
1778 RefreshAllItemsAfter(pos
);
1784 wxMenu
*wxMenuBar::Remove(size_t pos
)
1786 wxMenu
*menuOld
= wxMenuBarBase::Remove(pos
);
1790 m_menuInfos
.RemoveAt(pos
);
1792 // this doesn't happen too often, so don't try to be too smart - just
1793 // refresh everything
1800 // ----------------------------------------------------------------------------
1801 // wxMenuBar top level menus access
1802 // ----------------------------------------------------------------------------
1804 wxCoord
wxMenuBar::GetItemWidth(size_t pos
) const
1806 return m_menuInfos
[pos
].GetWidth(wxConstCast(this, wxMenuBar
));
1809 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1811 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1813 if ( enable
!= m_menuInfos
[pos
].IsEnabled() )
1815 m_menuInfos
[pos
].SetEnabled(enable
);
1819 //else: nothing to do
1822 bool wxMenuBar::IsEnabledTop(size_t pos
) const
1824 wxCHECK_MSG( pos
< GetCount(), false, _T("invalid index in IsEnabledTop") );
1826 return m_menuInfos
[pos
].IsEnabled();
1829 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
1831 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1833 if ( label
!= m_menuInfos
[pos
].GetLabel() )
1835 m_menuInfos
[pos
].SetLabel(label
);
1839 //else: nothing to do
1842 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
1844 wxCHECK_MSG( pos
< GetCount(), wxEmptyString
, _T("invalid index in GetLabelTop") );
1846 return m_menuInfos
[pos
].GetLabel();
1849 // ----------------------------------------------------------------------------
1850 // wxMenuBar drawing
1851 // ----------------------------------------------------------------------------
1853 void wxMenuBar::RefreshAllItemsAfter(size_t pos
)
1857 // no need to refresh if nothing is shown yet
1861 wxRect rect
= GetItemRect(pos
);
1862 rect
.width
= GetClientSize().x
- rect
.x
;
1866 void wxMenuBar::RefreshItem(size_t pos
)
1868 wxCHECK_RET( pos
!= (size_t)-1,
1869 _T("invalid item in wxMenuBar::RefreshItem") );
1873 // no need to refresh if nothing is shown yet
1877 RefreshRect(GetItemRect(pos
));
1880 void wxMenuBar::DoDraw(wxControlRenderer
*renderer
)
1882 wxDC
& dc
= renderer
->GetDC();
1883 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1885 // redraw only the items which must be redrawn
1887 // we don't have to use GetUpdateClientRect() here because our client rect
1888 // is the same as total one
1889 wxRect rectUpdate
= GetUpdateRegion().GetBox();
1891 int flagsMenubar
= GetStateFlags();
1895 rect
.height
= GetClientSize().y
;
1898 size_t count
= GetCount();
1899 for ( size_t n
= 0; n
< count
; n
++ )
1901 if ( x
> rectUpdate
.GetRight() )
1903 // all remaining items are to the right of rectUpdate
1908 rect
.width
= GetItemWidth(n
);
1910 if ( x
< rectUpdate
.x
)
1912 // this item is still to the left of rectUpdate
1916 int flags
= flagsMenubar
;
1917 if ( m_current
!= -1 && n
== (size_t)m_current
)
1919 flags
|= wxCONTROL_SELECTED
;
1922 if ( !IsEnabledTop(n
) )
1924 flags
|= wxCONTROL_DISABLED
;
1927 GetRenderer()->DrawMenuBarItem
1931 m_menuInfos
[n
].GetLabel(),
1933 m_menuInfos
[n
].GetAccelIndex()
1938 // ----------------------------------------------------------------------------
1939 // wxMenuBar geometry
1940 // ----------------------------------------------------------------------------
1942 wxRect
wxMenuBar::GetItemRect(size_t pos
) const
1944 wxASSERT_MSG( pos
< GetCount(), _T("invalid menu bar item index") );
1945 wxASSERT_MSG( IsCreated(), _T("can't call this method yet") );
1950 rect
.height
= GetClientSize().y
;
1952 for ( size_t n
= 0; n
< pos
; n
++ )
1954 rect
.x
+= GetItemWidth(n
);
1957 rect
.width
= GetItemWidth(pos
);
1962 wxSize
wxMenuBar::DoGetBestClientSize() const
1965 if ( GetMenuCount() > 0 )
1967 wxClientDC
dc(wxConstCast(this, wxMenuBar
));
1968 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1969 dc
.GetTextExtent(GetLabelTop(0), &size
.x
, &size
.y
);
1971 // adjust for the renderer we use
1972 size
= GetRenderer()->GetMenuBarItemSize(size
);
1974 else // empty menubar
1980 // the width is arbitrary, of course, for horizontal menubar
1986 int wxMenuBar::GetMenuFromPoint(const wxPoint
& pos
) const
1988 if ( pos
.x
< 0 || pos
.y
< 0 || pos
.y
> GetClientSize().y
)
1993 size_t count
= GetCount();
1994 for ( size_t item
= 0; item
< count
; item
++ )
1996 x
+= GetItemWidth(item
);
2004 // to the right of the last menu item
2008 // ----------------------------------------------------------------------------
2009 // wxMenuBar menu operations
2010 // ----------------------------------------------------------------------------
2012 void wxMenuBar::SelectMenu(size_t pos
)
2015 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::SelectMenu"));
2021 void wxMenuBar::DoSelectMenu(size_t pos
)
2023 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in DoSelectMenu") );
2025 int posOld
= m_current
;
2031 // close the previous menu
2032 if ( IsShowingMenu() )
2034 // restore m_shouldShowMenu flag after DismissMenu() which resets
2036 bool old
= m_shouldShowMenu
;
2040 m_shouldShowMenu
= old
;
2043 RefreshItem((size_t)posOld
);
2049 void wxMenuBar::PopupMenu(size_t pos
)
2051 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in PopupCurrentMenu") );
2058 // ----------------------------------------------------------------------------
2059 // wxMenuBar input handing
2060 // ----------------------------------------------------------------------------
2063 Note that wxMenuBar doesn't use wxInputHandler but handles keyboard and
2064 mouse in the same way under all platforms. This is because it doesn't derive
2065 from wxControl (which works with input handlers) but directly from wxWindow.
2067 Also, menu bar input handling is rather simple, so maybe it's not really
2068 worth making it themeable - at least I've decided against doing it now as it
2069 would merging the changes back into trunk more difficult. But it still could
2070 be done later if really needed.
2073 void wxMenuBar::OnKillFocus(wxFocusEvent
& event
)
2075 if ( m_current
!= -1 )
2077 RefreshItem((size_t)m_current
);
2085 void wxMenuBar::OnLeftDown(wxMouseEvent
& event
)
2093 else // we didn't have mouse capture, capture it now
2095 m_current
= GetMenuFromPoint(event
.GetPosition());
2096 if ( m_current
== -1 )
2098 // unfortunately, we can't prevent wxMSW from giving us the focus,
2099 // so we can only give it back
2104 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::OnLeftDown"));
2107 // show it as selected
2108 RefreshItem((size_t)m_current
);
2111 PopupCurrentMenu(false /* don't select first item - as Windows does */);
2116 void wxMenuBar::OnMouseMove(wxMouseEvent
& event
)
2120 (void)ProcessMouseEvent(event
.GetPosition());
2128 bool wxMenuBar::ProcessMouseEvent(const wxPoint
& pt
)
2130 // a hack to ignore the extra mouse events MSW sends us: this is similar to
2131 // wxUSE_MOUSEEVENT_HACK in wxWin itself but it isn't enough for us here as
2132 // we get the messages from different windows (old and new popup menus for
2135 static wxPoint s_ptLast
;
2136 if ( pt
== s_ptLast
)
2144 int currentNew
= GetMenuFromPoint(pt
);
2145 if ( (currentNew
== -1) || (currentNew
== m_current
) )
2150 // select the new active item
2151 DoSelectMenu(currentNew
);
2153 // show the menu if we know that we should, even if we hadn't been showing
2154 // it before (this may happen if the previous menu was disabled)
2155 if ( m_shouldShowMenu
&& !m_menuShown
)
2157 // open the new menu if the old one we closed had been opened
2158 PopupCurrentMenu(false /* don't select first item - as Windows does */);
2164 void wxMenuBar::OnKeyDown(wxKeyEvent
& event
)
2166 // ensure that we have a current item - we might not have it if we're
2167 // given the focus with Alt or F10 press (and under GTK+ the menubar
2168 // somehow gets the keyboard events even when it doesn't have focus...)
2169 if ( m_current
== -1 )
2171 if ( !HasCapture() )
2175 else // we do have capture
2177 // we always maintain a valid current item while we're in modal
2178 // state (i.e. have the capture)
2179 wxFAIL_MSG( _T("how did we manage to lose current item?") );
2185 int key
= event
.GetKeyCode();
2187 // first let the menu have it
2188 if ( IsShowingMenu() && m_menuShown
->ProcessKeyDown(key
) )
2193 // cycle through the menu items when left/right arrows are pressed and open
2194 // the menu when up/down one is
2198 // Alt must be processed at wxWindow level too
2203 // remove the selection and give the focus away
2204 if ( m_current
!= -1 )
2206 if ( IsShowingMenu() )
2218 size_t count
= GetCount();
2221 // the item won't change anyhow
2224 //else: otherwise, it will
2226 // remember if we were showing a menu - if we did, we should
2227 // show the new menu after changing the item
2228 bool wasMenuOpened
= IsShowingMenu();
2229 if ( wasMenuOpened
)
2234 // cast is safe as we tested for -1 above
2235 size_t currentNew
= (size_t)m_current
;
2237 if ( key
== WXK_LEFT
)
2239 if ( currentNew
-- == 0 )
2240 currentNew
= count
- 1;
2244 if ( ++currentNew
== count
)
2248 DoSelectMenu(currentNew
);
2250 if ( wasMenuOpened
)
2265 // letters open the corresponding menu
2268 int idxFound
= FindNextItemForAccel(m_current
, key
, &unique
);
2270 if ( idxFound
!= -1 )
2272 if ( IsShowingMenu() )
2277 DoSelectMenu((size_t)idxFound
);
2279 // if the item is not unique, just select it but don't
2280 // activate as the user might have wanted to activate
2283 // also, don't try to open a disabled menu
2284 if ( unique
&& IsEnabledTop((size_t)idxFound
) )
2290 // skip the "event.Skip()" below
2299 // ----------------------------------------------------------------------------
2300 // wxMenuBar accel handling
2301 // ----------------------------------------------------------------------------
2303 int wxMenuBar::FindNextItemForAccel(int idxStart
, int key
, bool *unique
) const
2305 if ( !wxIsalnum((wxChar
)key
) )
2307 // we only support letters/digits as accels
2311 // do we have more than one item with this accel?
2315 // translate everything to lower case before comparing
2316 wxChar chAccel
= (wxChar
)wxTolower(key
);
2318 // the index of the item with this accel
2321 // loop through all items searching for the item with this
2322 // accel starting at the item after the current one
2323 int count
= GetCount();
2324 int n
= idxStart
== -1 ? 0 : idxStart
+ 1;
2335 const wxMenuInfo
& info
= m_menuInfos
[n
];
2337 int idxAccel
= info
.GetAccelIndex();
2338 if ( idxAccel
!= -1 &&
2339 wxTolower(info
.GetLabel()[(size_t)idxAccel
])
2342 // ok, found an item with this accel
2343 if ( idxFound
== -1 )
2345 // store it but continue searching as we need to
2346 // know if it's the only item with this accel or if
2350 else // we already had found such item
2355 // no need to continue further, we won't find
2356 // anything we don't already know
2361 // we want to iterate over all items wrapping around if
2369 if ( n
== idxStart
)
2371 // we've seen all items
2381 bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent
& event
)
2384 for ( wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
2386 node
= node
->GetNext(), n
++ )
2388 // accels of the items in the disabled menus shouldn't work
2389 if ( m_menuInfos
[n
].IsEnabled() )
2391 if ( node
->GetData()->ProcessAccelEvent(event
) )
2393 // menu processed it
2403 #endif // wxUSE_ACCEL
2405 // ----------------------------------------------------------------------------
2406 // wxMenuBar menus showing
2407 // ----------------------------------------------------------------------------
2409 void wxMenuBar::PopupCurrentMenu(bool selectFirst
)
2411 wxCHECK_RET( m_current
!= -1, _T("no menu to popup") );
2413 // forgot to call DismissMenu()?
2414 wxASSERT_MSG( !m_menuShown
, _T("shouldn't show two menus at once!") );
2416 // in any case, we should show it - even if we won't
2417 m_shouldShowMenu
= true;
2419 if ( IsEnabledTop(m_current
) )
2421 // remember the menu we show
2422 m_menuShown
= GetMenu(m_current
);
2424 // we don't show the menu at all if it has no items
2425 if ( !m_menuShown
->IsEmpty() )
2427 // position it correctly: note that we must use screen coords and
2428 // that we pass 0 as width to position the menu exactly below the
2429 // item, not to the right of it
2430 wxRect rectItem
= GetItemRect(m_current
);
2432 m_menuShown
->SetInvokingWindow(m_frameLast
);
2434 m_menuShown
->Popup(ClientToScreen(rectItem
.GetPosition()),
2435 wxSize(0, rectItem
.GetHeight()),
2440 // reset it back as no menu is shown
2444 //else: don't show disabled menu
2447 void wxMenuBar::DismissMenu()
2449 wxCHECK_RET( m_menuShown
, _T("can't dismiss menu if none is shown") );
2451 m_menuShown
->Dismiss();
2455 void wxMenuBar::OnDismissMenu(bool dismissMenuBar
)
2457 m_shouldShowMenu
= false;
2459 if ( dismissMenuBar
)
2465 void wxMenuBar::OnDismiss()
2467 if ( ReleaseMouseCapture() )
2468 wxLogTrace(_T("mousecapture"), _T("Releasing mouse from wxMenuBar::OnDismiss"));
2470 if ( m_current
!= -1 )
2472 size_t current
= m_current
;
2475 RefreshItem(current
);
2481 bool wxMenuBar::ReleaseMouseCapture()
2484 // With wxX11, when a menu is closed by clicking away from it, a control
2485 // under the click will still get an event, even though the menu has the
2486 // capture (bug?). So that control may already have taken the capture by
2487 // this point, preventing us from releasing the menu's capture. So to work
2488 // around this, we release both captures, then put back the control's
2490 wxWindow
*capture
= GetCapture();
2493 capture
->ReleaseMouse();
2495 if ( capture
== this )
2498 bool had
= HasCapture();
2503 capture
->CaptureMouse();
2517 void wxMenuBar::GiveAwayFocus()
2519 GetFrame()->SetFocus();
2522 // ----------------------------------------------------------------------------
2523 // popup menu support
2524 // ----------------------------------------------------------------------------
2526 wxEventLoop
*wxWindow::ms_evtLoopPopup
= NULL
;
2528 bool wxWindow::DoPopupMenu(wxMenu
*menu
, int x
, int y
)
2530 wxCHECK_MSG( !ms_evtLoopPopup
, false,
2531 _T("can't show more than one popup menu at a time") );
2534 // we need to change the cursor before showing the menu as, apparently, no
2535 // cursor changes took place while the mouse is captured
2536 wxCursor cursorOld
= GetCursor();
2537 SetCursor(wxCURSOR_ARROW
);
2541 // flash any delayed log messages before showing the menu, otherwise it
2542 // could be dismissed (because it would lose focus) immediately after being
2544 wxLog::FlushActive();
2546 // some controls update themselves from OnIdle() call - let them do it
2547 wxTheApp
->ProcessIdle();
2549 // if the window hadn't been refreshed yet, the menu can adversely affect
2550 // its next OnPaint() handler execution - i.e. scrolled window refresh
2551 // logic breaks then as it scrolls part of the menu which hadn't been there
2552 // when the update event was generated into view
2556 menu
->SetInvokingWindow(this);
2558 // wxLogDebug( "Name of invoking window %s", menu->GetInvokingWindow()->GetName().c_str() );
2560 menu
->Popup(ClientToScreen(wxPoint(x
, y
)), wxSize(0,0));
2562 // this is not very useful if the menu was popped up because of the mouse
2563 // click but I think it is nice to do when it appears because of a key
2564 // press (i.e. Windows menu key)
2566 // Windows itself doesn't do it, but IMHO this is nice
2569 // we have to redirect all keyboard input to the menu temporarily
2570 PushEventHandler(new wxMenuKbdRedirector(menu
));
2572 // enter the local modal loop
2573 ms_evtLoopPopup
= new wxEventLoop
;
2574 ms_evtLoopPopup
->Run();
2576 delete ms_evtLoopPopup
;
2577 ms_evtLoopPopup
= NULL
;
2579 // remove the handler
2580 PopEventHandler(true /* delete it */);
2582 menu
->SetInvokingWindow(NULL
);
2585 SetCursor(cursorOld
);
2591 void wxWindow::DismissPopupMenu()
2593 wxCHECK_RET( ms_evtLoopPopup
, _T("no popup menu shown") );
2595 ms_evtLoopPopup
->Exit();
2598 #endif // wxUSE_MENUS