1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenuItem, wxMenu and wxMenuBar implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "univmenuitem.h"
22 #pragma implementation "univmenu.h"
25 #include "wx/wxprec.h"
32 #include "wx/dynarray.h"
33 #include "wx/control.h" // for FindAccelIndex()
35 #include "wx/settings.h"
42 #include "wx/popupwin.h"
43 #include "wx/evtloop.h"
44 #include "wx/dcclient.h"
47 #include "wx/univ/renderer.h"
50 #include "wx/msw/private.h"
53 // ----------------------------------------------------------------------------
54 // wxMenuInfo contains all extra information about top level menus we need
55 // ----------------------------------------------------------------------------
57 class WXDLLEXPORT wxMenuInfo
61 wxMenuInfo(const wxString
& text
)
69 void SetLabel(const wxString
& text
)
71 // remember the accel char (may be -1 if none)
72 m_indexAccel
= wxControl::FindAccelIndex(text
, &m_label
);
74 // calculate the width later, after the menu bar is created
78 void SetEnabled(bool enabled
= TRUE
) { m_isEnabled
= enabled
; }
82 const wxString
& GetLabel() const { return m_label
; }
83 bool IsEnabled() const { return m_isEnabled
; }
84 wxCoord
GetWidth(wxMenuBar
*menubar
) const
88 wxConstCast(this, wxMenuInfo
)->CalcWidth(menubar
);
94 int GetAccelIndex() const { return m_indexAccel
; }
97 void CalcWidth(wxMenuBar
*menubar
)
100 wxClientDC
dc(menubar
);
101 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
102 dc
.GetTextExtent(m_label
, &size
.x
, &size
.y
);
104 // adjust for the renderer we use and store the width
105 m_width
= menubar
->GetRenderer()->GetMenuBarItemSize(size
).x
;
114 #include "wx/arrimpl.cpp"
116 WX_DEFINE_OBJARRAY(wxMenuInfoArray
);
118 // ----------------------------------------------------------------------------
119 // wxPopupMenuWindow: a popup window showing a menu
120 // ----------------------------------------------------------------------------
122 class wxPopupMenuWindow
: public wxPopupTransientWindow
125 wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
);
127 ~wxPopupMenuWindow();
129 // override the base class version to select the first item initially
130 virtual void Popup(wxWindow
*focus
= NULL
);
132 // override the base class version to dismiss any open submenus
133 virtual void Dismiss();
135 // notify the menu when the window disappears from screen
136 virtual void OnDismiss();
138 // called when a submenu is dismissed
139 void OnSubmenuDismiss() { m_hasOpenSubMenu
= FALSE
; }
141 // get the currently selected item (may be NULL)
142 wxMenuItem
*GetCurrentItem() const
144 return m_nodeCurrent
? m_nodeCurrent
->GetData() : NULL
;
147 // find the menu item at given position
148 wxMenuItemList::compatibility_iterator
GetMenuItemFromPoint(const wxPoint
& pt
) const;
150 // refresh the given item
151 void RefreshItem(wxMenuItem
*item
);
153 // preselect the first item
154 void SelectFirst() { SetCurrent(m_menu
->GetMenuItems().GetFirst()); }
156 // process the key event, return TRUE if done
157 bool ProcessKeyDown(int key
);
159 // process mouse move event
160 void ProcessMouseMove(const wxPoint
& pt
);
162 // don't dismiss the popup window if the parent menu was clicked
163 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
166 // how did we perform this operation?
173 // draw the menu inside this window
174 virtual void DoDraw(wxControlRenderer
*renderer
);
177 void OnLeftUp(wxMouseEvent
& event
);
178 void OnMouseMove(wxMouseEvent
& event
);
179 void OnMouseLeave(wxMouseEvent
& event
);
180 void OnKeyDown(wxKeyEvent
& event
);
182 // reset the current item and node
185 // set the current node and item withotu refreshing anything
186 void SetCurrent(wxMenuItemList::compatibility_iterator node
);
187 virtual bool SetCurrent(bool doit
= true){return wxPopupTransientWindow::SetCurrent(doit
);};
189 // change the current item refreshing the old and new items
190 void ChangeCurrent(wxMenuItemList::compatibility_iterator node
);
192 // activate item, i.e. call either ClickItem() or OpenSubmenu() depending
193 // on what it is, return TRUE if something was done (i.e. it's not a
195 bool ActivateItem(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
197 // send the event about the item click
198 void ClickItem(wxMenuItem
*item
);
200 // show the submenu for this item
201 void OpenSubmenu(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
203 // can this tiem be opened?
204 bool CanOpen(wxMenuItem
*item
)
206 return item
&& item
->IsEnabled() && item
->IsSubMenu();
209 // dismiss the menu and all parent menus too
210 void DismissAndNotify();
212 // react to dimissing this menu and also dismiss the parent if
214 void HandleDismiss(bool dismissParent
);
216 // do we have an open submenu?
217 bool HasOpenSubmenu() const { return m_hasOpenSubMenu
; }
219 // get previous node after the current one
220 wxMenuItemList::compatibility_iterator
GetPrevNode() const;
222 // get previous node before the given one, wrapping if it's the first one
223 wxMenuItemList::compatibility_iterator
GetPrevNode(wxMenuItemList::compatibility_iterator node
) const;
225 // get next node after the current one
226 wxMenuItemList::compatibility_iterator
GetNextNode() const;
228 // get next node after the given one, wrapping if it's the last one
229 wxMenuItemList::compatibility_iterator
GetNextNode(wxMenuItemList::compatibility_iterator node
) const;
235 // the menu node corresponding to the current item
236 wxMenuItemList::compatibility_iterator m_nodeCurrent
;
238 // do we currently have an opened submenu?
239 bool m_hasOpenSubMenu
;
241 DECLARE_EVENT_TABLE()
244 // ----------------------------------------------------------------------------
245 // wxMenuKbdRedirector: an event handler which redirects kbd input to wxMenu
246 // ----------------------------------------------------------------------------
248 class wxMenuKbdRedirector
: public wxEvtHandler
251 wxMenuKbdRedirector(wxMenu
*menu
) { m_menu
= menu
; }
253 virtual bool ProcessEvent(wxEvent
& event
)
255 if ( event
.GetEventType() == wxEVT_KEY_DOWN
)
257 return m_menu
->ProcessKeyDown(((wxKeyEvent
&)event
).GetKeyCode());
263 return wxEvtHandler::ProcessEvent(event
);
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
275 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
276 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
277 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
279 BEGIN_EVENT_TABLE(wxPopupMenuWindow
, wxPopupTransientWindow
)
280 EVT_KEY_DOWN(wxPopupMenuWindow::OnKeyDown
)
282 EVT_LEFT_UP(wxPopupMenuWindow::OnLeftUp
)
283 EVT_MOTION(wxPopupMenuWindow::OnMouseMove
)
284 EVT_LEAVE_WINDOW(wxPopupMenuWindow::OnMouseLeave
)
287 BEGIN_EVENT_TABLE(wxMenuBar
, wxMenuBarBase
)
288 EVT_KILL_FOCUS(wxMenuBar::OnKillFocus
)
290 EVT_KEY_DOWN(wxMenuBar::OnKeyDown
)
292 EVT_LEFT_DOWN(wxMenuBar::OnLeftDown
)
293 EVT_MOTION(wxMenuBar::OnMouseMove
)
296 // ============================================================================
298 // ============================================================================
300 // ----------------------------------------------------------------------------
302 // ----------------------------------------------------------------------------
304 wxPopupMenuWindow::wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
)
307 m_hasOpenSubMenu
= FALSE
;
311 (void)Create(parent
, wxBORDER_RAISED
);
313 SetCursor(wxCURSOR_ARROW
);
316 wxPopupMenuWindow::~wxPopupMenuWindow()
318 // When m_popupMenu in wxMenu is deleted because it
319 // is a child of an old menu bar being deleted (note: it does
320 // not get destroyed by the wxMenu destructor, but
321 // by DestroyChildren()), m_popupMenu should be reset to NULL.
323 m_menu
->m_popupMenu
= NULL
;
326 // ----------------------------------------------------------------------------
327 // wxPopupMenuWindow current item/node handling
328 // ----------------------------------------------------------------------------
330 void wxPopupMenuWindow::ResetCurrent()
333 SetCurrent(wxMenuItemList::compatibility_iterator());
335 SetCurrent((wxwxMenuItemListNode
*)NULL
);
339 void wxPopupMenuWindow::SetCurrent(wxMenuItemList::compatibility_iterator node
)
341 m_nodeCurrent
= node
;
344 void wxPopupMenuWindow::ChangeCurrent(wxMenuItemList::compatibility_iterator node
)
346 if ( node
!= m_nodeCurrent
)
348 wxMenuItemList::compatibility_iterator nodeOldCurrent
= m_nodeCurrent
;
350 m_nodeCurrent
= node
;
352 if ( nodeOldCurrent
)
354 wxMenuItem
*item
= nodeOldCurrent
->GetData();
355 wxCHECK_RET( item
, _T("no current item?") );
357 // if it was the currently opened menu, close it
358 if ( item
->IsSubMenu() && item
->GetSubMenu()->IsShown() )
360 item
->GetSubMenu()->Dismiss();
368 RefreshItem(m_nodeCurrent
->GetData());
372 wxMenuItemList::compatibility_iterator
wxPopupMenuWindow::GetPrevNode() const
374 // return the last node if there had been no previously selected one
375 return m_nodeCurrent
? GetPrevNode(m_nodeCurrent
)
376 : m_menu
->GetMenuItems().GetLast();
379 wxMenuItemList::compatibility_iterator
380 wxPopupMenuWindow::GetPrevNode(wxMenuItemList::compatibility_iterator node
) const
384 node
= node
->GetPrevious();
387 node
= m_menu
->GetMenuItems().GetLast();
390 //else: the menu is empty
395 wxMenuItemList::compatibility_iterator
wxPopupMenuWindow::GetNextNode() const
397 // return the first node if there had been no previously selected one
398 return m_nodeCurrent
? GetNextNode(m_nodeCurrent
)
399 : m_menu
->GetMenuItems().GetFirst();
402 wxMenuItemList::compatibility_iterator
403 wxPopupMenuWindow::GetNextNode(wxMenuItemList::compatibility_iterator node
) const
407 node
= node
->GetNext();
410 node
= m_menu
->GetMenuItems().GetFirst();
413 //else: the menu is empty
418 // ----------------------------------------------------------------------------
419 // wxPopupMenuWindow popup/dismiss
420 // ----------------------------------------------------------------------------
422 void wxPopupMenuWindow::Popup(wxWindow
*focus
)
424 // check that the current item had been properly reset before
425 wxASSERT_MSG( !m_nodeCurrent
||
426 m_nodeCurrent
== m_menu
->GetMenuItems().GetFirst(),
427 _T("menu current item preselected incorrectly") );
429 wxPopupTransientWindow::Popup(focus
);
432 // ensure that this window is really on top of everything: without using
433 // SetWindowPos() it can be covered by its parent menu which is not
434 // really what we want
435 wxMenu
*menuParent
= m_menu
->GetParent();
438 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
440 // if we're shown, the parent menu must be also shown
441 wxCHECK_RET( win
, _T("parent menu is not shown?") );
443 if ( !::SetWindowPos(GetHwndOf(win
), GetHwnd(),
445 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOREDRAW
) )
447 wxLogLastError(_T("SetWindowPos(HWND_TOP)"));
455 void wxPopupMenuWindow::Dismiss()
457 if ( HasOpenSubmenu() )
459 wxMenuItem
*item
= GetCurrentItem();
460 wxCHECK_RET( item
&& item
->IsSubMenu(), _T("where is our open submenu?") );
462 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
463 wxCHECK_RET( win
, _T("opened submenu is not opened?") );
469 wxPopupTransientWindow::Dismiss();
472 void wxPopupMenuWindow::OnDismiss()
474 // when we are dismissed because the user clicked elsewhere or we lost
475 // focus in any other way, hide the parent menu as well
479 void wxPopupMenuWindow::HandleDismiss(bool dismissParent
)
483 m_menu
->OnDismiss(dismissParent
);
486 void wxPopupMenuWindow::DismissAndNotify()
492 // ----------------------------------------------------------------------------
493 // wxPopupMenuWindow geometry
494 // ----------------------------------------------------------------------------
496 wxMenuItemList::compatibility_iterator
497 wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint
& pt
) const
499 // we only use the y coord normally, but still check x in case the point is
500 // outside the window completely
501 if ( wxWindow::HitTest(pt
) == wxHT_WINDOW_INSIDE
)
504 for ( wxMenuItemList::compatibility_iterator node
= m_menu
->GetMenuItems().GetFirst();
506 node
= node
->GetNext() )
508 wxMenuItem
*item
= node
->GetData();
509 y
+= item
->GetHeight();
519 return wxMenuItemList::compatibility_iterator();
525 // ----------------------------------------------------------------------------
526 // wxPopupMenuWindow drawing
527 // ----------------------------------------------------------------------------
529 void wxPopupMenuWindow::RefreshItem(wxMenuItem
*item
)
531 wxCHECK_RET( item
, _T("can't refresh NULL item") );
533 wxASSERT_MSG( IsShown(), _T("can't refresh menu which is not shown") );
535 // FIXME: -1 here because of SetLogicalOrigin(1, 1) in DoDraw()
536 RefreshRect(wxRect(0, item
->GetPosition() - 1,
537 m_menu
->GetGeometryInfo().GetSize().x
, item
->GetHeight()));
540 void wxPopupMenuWindow::DoDraw(wxControlRenderer
*renderer
)
542 // no clipping so far - do we need it? I don't think so as the menu is
543 // never partially covered as it is always on top of everything
545 wxDC
& dc
= renderer
->GetDC();
546 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
548 // FIXME: this should be done in the renderer, however when it is fixed
549 // wxPopupMenuWindow::RefreshItem() should be changed too!
550 dc
.SetLogicalOrigin(1, 1);
552 wxRenderer
*rend
= renderer
->GetRenderer();
555 const wxMenuGeometryInfo
& gi
= m_menu
->GetGeometryInfo();
556 for ( wxMenuItemList::compatibility_iterator node
= m_menu
->GetMenuItems().GetFirst();
558 node
= node
->GetNext() )
560 wxMenuItem
*item
= node
->GetData();
562 if ( item
->IsSeparator() )
564 rend
->DrawMenuSeparator(dc
, y
, gi
);
566 else // not a separator
569 if ( item
->IsCheckable() )
571 flags
|= wxCONTROL_CHECKABLE
;
573 if ( item
->IsChecked() )
575 flags
|= wxCONTROL_CHECKED
;
579 if ( !item
->IsEnabled() )
580 flags
|= wxCONTROL_DISABLED
;
582 if ( item
->IsSubMenu() )
583 flags
|= wxCONTROL_ISSUBMENU
;
585 if ( item
== GetCurrentItem() )
586 flags
|= wxCONTROL_SELECTED
;
590 if ( !item
->IsEnabled() )
592 bmp
= item
->GetDisabledBitmap();
597 // strangely enough, for unchecked item we use the
598 // "checked" bitmap because this is the default one - this
599 // explains this strange boolean expression
600 bmp
= item
->GetBitmap(!item
->IsCheckable() || item
->IsChecked());
609 item
->GetAccelString(),
612 item
->GetAccelIndex()
616 y
+= item
->GetHeight();
620 // ----------------------------------------------------------------------------
621 // wxPopupMenuWindow actions
622 // ----------------------------------------------------------------------------
624 void wxPopupMenuWindow::ClickItem(wxMenuItem
*item
)
626 wxCHECK_RET( item
, _T("can't click NULL item") );
628 wxASSERT_MSG( !item
->IsSeparator() && !item
->IsSubMenu(),
629 _T("can't click this item") );
631 wxMenu
* menu
= m_menu
;
636 menu
->ClickItem(item
);
639 void wxPopupMenuWindow::OpenSubmenu(wxMenuItem
*item
, InputMethod how
)
641 wxCHECK_RET( item
, _T("can't open NULL submenu") );
643 wxMenu
*submenu
= item
->GetSubMenu();
644 wxCHECK_RET( submenu
, _T("can only open submenus!") );
646 // FIXME: should take into account the border width
647 submenu
->Popup(ClientToScreen(wxPoint(0, item
->GetPosition())),
648 wxSize(m_menu
->GetGeometryInfo().GetSize().x
, 0),
649 how
== WithKeyboard
/* preselect first item then */);
651 m_hasOpenSubMenu
= TRUE
;
654 bool wxPopupMenuWindow::ActivateItem(wxMenuItem
*item
, InputMethod how
)
656 // don't activate disabled items
657 if ( !item
|| !item
->IsEnabled() )
662 // normal menu items generate commands, submenus can be opened and
663 // the separators don't do anything
664 if ( item
->IsSubMenu() )
666 OpenSubmenu(item
, how
);
668 else if ( !item
->IsSeparator() )
672 else // separator, can't activate
680 // ----------------------------------------------------------------------------
681 // wxPopupMenuWindow input handling
682 // ----------------------------------------------------------------------------
684 bool wxPopupMenuWindow::ProcessLeftDown(wxMouseEvent
& event
)
686 // wxPopupWindowHandler dismisses the window when the mouse is clicked
687 // outside it which is usually just fine, but there is one case when we
688 // don't want to do it: if the mouse was clicked on the parent submenu item
689 // which opens this menu, so check for it
691 wxPoint pos
= event
.GetPosition();
692 if ( HitTest(pos
.x
, pos
.y
) == wxHT_WINDOW_OUTSIDE
)
694 wxMenu
*menu
= m_menu
->GetParent();
697 wxPopupMenuWindow
*win
= menu
->m_popupMenu
;
699 wxCHECK_MSG( win
, FALSE
, _T("parent menu not shown?") );
701 pos
= ClientToScreen(pos
);
702 if ( win
->GetMenuItemFromPoint(win
->ScreenToClient(pos
)) )
707 //else: it is outside the parent menu as well, do dismiss this one
714 void wxPopupMenuWindow::OnLeftUp(wxMouseEvent
& event
)
716 wxMenuItemList::compatibility_iterator node
= GetMenuItemFromPoint(event
.GetPosition());
719 ActivateItem(node
->GetData(), WithMouse
);
723 void wxPopupMenuWindow::OnMouseMove(wxMouseEvent
& event
)
725 const wxPoint pt
= event
.GetPosition();
727 // we need to ignore extra mouse events: example when this happens is when
728 // the mouse is on the menu and we open a submenu from keyboard - Windows
729 // then sends us a dummy mouse move event, we (correctly) determine that it
730 // happens in the parent menu and so immediately close the just opened
733 static wxPoint s_ptLast
;
734 wxPoint ptCur
= ClientToScreen(pt
);
735 if ( ptCur
== s_ptLast
)
743 ProcessMouseMove(pt
);
748 void wxPopupMenuWindow::ProcessMouseMove(const wxPoint
& pt
)
750 wxMenuItemList::compatibility_iterator node
= GetMenuItemFromPoint(pt
);
752 // don't reset current to NULL here, we only do it when the mouse leaves
753 // the window (see below)
756 if ( node
!= m_nodeCurrent
)
760 wxMenuItem
*item
= GetCurrentItem();
763 OpenSubmenu(item
, WithMouse
);
766 //else: same item, nothing to do
768 else // not on an item
770 // the last open submenu forwards the mouse move messages to its
771 // parent, so if the mouse moves to another item of the parent menu,
772 // this menu is closed and this other item is selected - in the similar
773 // manner, the top menu forwards the mouse moves to the menubar which
774 // allows to select another top level menu by just moving the mouse
776 // we need to translate our client coords to the client coords of the
777 // window we forward this event to
778 wxPoint ptScreen
= ClientToScreen(pt
);
780 // if the mouse is outside this menu, let the parent one to
782 wxMenu
*menuParent
= m_menu
->GetParent();
785 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
787 // if we're shown, the parent menu must be also shown
788 wxCHECK_RET( win
, _T("parent menu is not shown?") );
790 win
->ProcessMouseMove(win
->ScreenToClient(ptScreen
));
792 else // no parent menu
794 wxMenuBar
*menubar
= m_menu
->GetMenuBar();
797 if ( menubar
->ProcessMouseEvent(
798 menubar
->ScreenToClient(ptScreen
)) )
800 // menubar has closed this menu and opened another one, probably
805 //else: top level popup menu, no other processing to do
809 void wxPopupMenuWindow::OnMouseLeave(wxMouseEvent
& event
)
811 // due to the artefact of mouse events generation under MSW, we actually
812 // may get the mouse leave event after the menu had been already dismissed
813 // and calling ChangeCurrent() would then assert, so don't do it
816 // we shouldn't change the current them if our submenu is opened and
817 // mouse moved there, in this case the submenu is responsable for
820 if ( HasOpenSubmenu() )
822 wxMenuItem
*item
= GetCurrentItem();
823 wxCHECK_RET( CanOpen(item
), _T("where is our open submenu?") );
825 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
826 wxCHECK_RET( win
, _T("submenu is opened but not shown?") );
828 // only handle this event if the mouse is not inside the submenu
829 wxPoint pt
= ClientToScreen(event
.GetPosition());
831 win
->HitTest(win
->ScreenToClient(pt
)) == wxHT_WINDOW_OUTSIDE
;
835 // this menu is the last opened
842 ChangeCurrent(wxMenuItemList::compatibility_iterator());
852 void wxPopupMenuWindow::OnKeyDown(wxKeyEvent
& event
)
854 if ( !ProcessKeyDown(event
.GetKeyCode()) )
860 bool wxPopupMenuWindow::ProcessKeyDown(int key
)
862 wxMenuItem
*item
= GetCurrentItem();
864 // first let the opened submenu to have it (no test for IsEnabled() here,
865 // the keys navigate even in a disabled submenu if we had somehow managed
866 // to open it inspit of this)
867 if ( HasOpenSubmenu() )
869 wxCHECK_MSG( CanOpen(item
), FALSE
,
870 _T("has open submenu but another item selected?") );
872 if ( item
->GetSubMenu()->ProcessKeyDown(key
) )
876 bool processed
= TRUE
;
878 // handle the up/down arrows, home, end, esc and return here, pass the
879 // left/right arrows to the menu bar except when the right arrow can be
880 // used to open a submenu
884 // if we're not a top level menu, close us, else leave this to the
886 if ( !m_menu
->GetParent() )
895 // close just this menu
897 HandleDismiss(FALSE
);
901 processed
= ActivateItem(item
);
905 ChangeCurrent(m_menu
->GetMenuItems().GetFirst());
909 ChangeCurrent(m_menu
->GetMenuItems().GetLast());
915 bool up
= key
== WXK_UP
;
917 wxMenuItemList::compatibility_iterator nodeStart
= up
? GetPrevNode()
920 while ( node
&& node
->GetData()->IsSeparator() )
922 node
= up
? GetPrevNode(node
) : GetNextNode(node
);
924 if ( node
== nodeStart
)
926 // nothing but separators and disabled items in this
929 node
= wxMenuItemList::compatibility_iterator();
948 // don't try to reopen an already opened menu
949 if ( !HasOpenSubmenu() && CanOpen(item
) )
960 // look for the menu item starting with this letter
961 if ( wxIsalnum(key
) )
963 // we want to start from the item after this one because
964 // if we're already on the item with the given accel we want to
965 // go to the next one, not to stay in place
966 wxMenuItemList::compatibility_iterator nodeStart
= GetNextNode();
968 // do we have more than one item with this accel?
969 bool notUnique
= FALSE
;
971 // translate everything to lower case before comparing
972 wxChar chAccel
= wxTolower(key
);
974 // loop through all items searching for the item with this
976 wxMenuItemList::compatibility_iterator node
= nodeStart
,
978 nodeFound
= wxMenuItemList::compatibility_iterator();
984 item
= node
->GetData();
986 int idxAccel
= item
->GetAccelIndex();
987 if ( idxAccel
!= -1 &&
988 wxTolower(item
->GetLabel()[(size_t)idxAccel
])
991 // ok, found an item with this accel
994 // store it but continue searching as we need to
995 // know if it's the only item with this accel or if
999 else // we already had found such item
1003 // no need to continue further, we won't find
1004 // anything we don't already know
1009 // we want to iterate over all items wrapping around if
1011 node
= GetNextNode(node
);
1012 if ( node
== nodeStart
)
1014 // we've seen all nodes
1021 item
= nodeFound
->GetData();
1023 // go to this item anyhow
1024 ChangeCurrent(nodeFound
);
1026 if ( !notUnique
&& item
->IsEnabled() )
1028 // unique item with this accel - activate it
1029 processed
= ActivateItem(item
);
1031 //else: just select it but don't activate as the user might
1032 // have wanted to activate another item
1034 // skip "processed = FALSE" below
1045 // ----------------------------------------------------------------------------
1047 // ----------------------------------------------------------------------------
1055 m_startRadioGroup
= -1;
1064 // ----------------------------------------------------------------------------
1065 // wxMenu and wxMenuGeometryInfo
1066 // ----------------------------------------------------------------------------
1068 wxMenuGeometryInfo::~wxMenuGeometryInfo()
1072 const wxMenuGeometryInfo
& wxMenu::GetGeometryInfo() const
1078 wxConstCast(this, wxMenu
)->m_geometry
=
1079 m_popupMenu
->GetRenderer()->GetMenuGeometry(m_popupMenu
, *this);
1083 wxFAIL_MSG( _T("can't get geometry without window") );
1090 void wxMenu::InvalidateGeometryInfo()
1099 // ----------------------------------------------------------------------------
1100 // wxMenu adding/removing items
1101 // ----------------------------------------------------------------------------
1103 void wxMenu::OnItemAdded(wxMenuItem
*item
)
1105 InvalidateGeometryInfo();
1109 #endif // wxUSE_ACCEL
1111 // the submenus of a popup menu should have the same invoking window as it
1113 if ( m_invokingWindow
&& item
->IsSubMenu() )
1115 item
->GetSubMenu()->SetInvokingWindow(m_invokingWindow
);
1119 void wxMenu::EndRadioGroup()
1121 // we're not inside a radio group any longer
1122 m_startRadioGroup
= -1;
1125 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
1132 if ( item
->GetKind() == wxITEM_RADIO
)
1134 int count
= GetMenuItemCount();
1136 if ( m_startRadioGroup
== -1 )
1138 // start a new radio group
1139 m_startRadioGroup
= count
;
1141 // for now it has just one element
1142 item
->SetAsRadioGroupStart();
1143 item
->SetRadioGroupEnd(m_startRadioGroup
);
1145 // ensure that we have a checked item in the radio group
1151 else // extend the current radio group
1153 // we need to update its end item
1154 item
->SetRadioGroupStart(m_startRadioGroup
);
1155 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
1159 node
->GetData()->SetRadioGroupEnd(count
);
1163 wxFAIL_MSG( _T("where is the radio group start item?") );
1167 else // not a radio item
1172 if ( !wxMenuBase::DoAppend(item
) )
1180 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1182 if ( !wxMenuBase::DoInsert(pos
, item
) )
1190 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1192 wxMenuItem
*itemOld
= wxMenuBase::DoRemove(item
);
1196 InvalidateGeometryInfo();
1199 RemoveAccelFor(item
);
1200 #endif // wxUSE_ACCEL
1206 // ----------------------------------------------------------------------------
1207 // wxMenu attaching/detaching
1208 // ----------------------------------------------------------------------------
1210 void wxMenu::Attach(wxMenuBarBase
*menubar
)
1212 wxMenuBase::Attach(menubar
);
1214 wxCHECK_RET( m_menuBar
, _T("menubar can't be NULL after attaching") );
1216 // unfortunately, we can't use m_menuBar->GetEventHandler() here because,
1217 // if the menubar is currently showing a menu, its event handler is a
1218 // temporary one installed by wxPopupWindow and so will disappear soon any
1219 // any attempts to use it from the newly attached menu would result in a
1222 // so we use the menubar itself, even if it's a pity as it means we can't
1223 // redirect all menu events by changing the menubar handler (FIXME)
1224 SetNextHandler(m_menuBar
);
1227 void wxMenu::Detach()
1229 wxMenuBase::Detach();
1232 // ----------------------------------------------------------------------------
1233 // wxMenu misc functions
1234 // ----------------------------------------------------------------------------
1236 wxWindow
*wxMenu::GetRootWindow() const
1240 // simple case - a normal menu attached to the menubar
1244 // we're a popup menu but the trouble is that only the top level popup menu
1245 // has a pointer to the invoking window, so we must walk up the menu chain
1247 wxWindow
*win
= GetInvokingWindow();
1250 // we already have it
1254 wxMenu
*menu
= GetParent();
1257 // We are a submenu of a menu of a menubar
1258 if (menu
->GetMenuBar())
1259 return menu
->GetMenuBar();
1261 win
= menu
->GetInvokingWindow();
1265 menu
= menu
->GetParent();
1268 // we're probably going to crash in the caller anyhow, but try to detect
1269 // this error as soon as possible
1270 wxASSERT_MSG( win
, _T("menu without any associated window?") );
1272 // also remember it in this menu so that we don't have to search for it the
1274 wxConstCast(this, wxMenu
)->m_invokingWindow
= win
;
1279 wxRenderer
*wxMenu::GetRenderer() const
1281 // we're going to crash without renderer!
1282 wxCHECK_MSG( m_popupMenu
, NULL
, _T("neither popup nor menubar menu?") );
1284 return m_popupMenu
->GetRenderer();
1287 void wxMenu::RefreshItem(wxMenuItem
*item
)
1289 // the item geometry changed, so our might have changed as well
1290 InvalidateGeometryInfo();
1294 // this would be a bug in IsShown()
1295 wxCHECK_RET( m_popupMenu
, _T("must have popup window if shown!") );
1297 // recalc geometry to update the item height and such
1298 (void)GetGeometryInfo();
1300 m_popupMenu
->RefreshItem(item
);
1304 // ----------------------------------------------------------------------------
1305 // wxMenu showing and hiding
1306 // ----------------------------------------------------------------------------
1308 bool wxMenu::IsShown() const
1310 return m_popupMenu
&& m_popupMenu
->IsShown();
1313 void wxMenu::OnDismiss(bool dismissParent
)
1317 // always notify the parent about submenu disappearance
1318 wxPopupMenuWindow
*win
= m_menuParent
->m_popupMenu
;
1321 win
->OnSubmenuDismiss();
1325 wxFAIL_MSG( _T("parent menu not shown?") );
1328 // and if we dismiss everything, propagate to parent
1329 if ( dismissParent
)
1331 // dismissParent is recursive
1332 m_menuParent
->Dismiss();
1333 m_menuParent
->OnDismiss(TRUE
);
1336 else // no parent menu
1338 // notify the menu bar if we're a top level menu
1341 m_menuBar
->OnDismissMenu(dismissParent
);
1345 wxCHECK_RET( m_invokingWindow
, _T("what kind of menu is this?") );
1347 m_invokingWindow
->DismissPopupMenu();
1349 // Why reset it here? We need it for sending the event to...
1350 // SetInvokingWindow(NULL);
1355 void wxMenu::Popup(const wxPoint
& pos
, const wxSize
& size
, bool selectFirst
)
1357 // create the popup window if not done yet
1360 m_popupMenu
= new wxPopupMenuWindow(GetRootWindow(), this);
1363 // select the first item unless disabled
1366 m_popupMenu
->SelectFirst();
1369 // the geometry might have changed since the last time we were shown, so
1371 m_popupMenu
->SetClientSize(GetGeometryInfo().GetSize());
1373 // position it as specified
1374 m_popupMenu
->Position(pos
, size
);
1376 // the menu can't have the focus itself (it is a Windows limitation), so
1377 // always keep the focus at the originating window
1378 wxWindow
*focus
= GetRootWindow();
1380 wxASSERT_MSG( focus
, _T("no window to keep focus on?") );
1383 m_popupMenu
->Popup(focus
);
1386 void wxMenu::Dismiss()
1388 wxCHECK_RET( IsShown(), _T("can't dismiss hidden menu") );
1390 m_popupMenu
->Dismiss();
1393 // ----------------------------------------------------------------------------
1394 // wxMenu event processing
1395 // ----------------------------------------------------------------------------
1397 bool wxMenu::ProcessKeyDown(int key
)
1399 wxCHECK_MSG( m_popupMenu
, FALSE
,
1400 _T("can't process key events if not shown") );
1402 return m_popupMenu
->ProcessKeyDown(key
);
1405 bool wxMenu::ClickItem(wxMenuItem
*item
)
1408 if ( item
->IsCheckable() )
1410 // update the item state
1411 isChecked
= !item
->IsChecked();
1413 item
->Check(isChecked
!= 0);
1421 return SendEvent(item
->GetId(), isChecked
);
1424 // ----------------------------------------------------------------------------
1425 // wxMenu accel support
1426 // ----------------------------------------------------------------------------
1430 bool wxMenu::ProcessAccelEvent(const wxKeyEvent
& event
)
1432 // do we have an item for this accel?
1433 wxMenuItem
*item
= m_accelTable
.GetMenuItem(event
);
1434 if ( item
&& item
->IsEnabled() )
1436 return ClickItem(item
);
1440 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
1442 node
= node
->GetNext() )
1444 const wxMenuItem
*item
= node
->GetData();
1445 if ( item
->IsSubMenu() && item
->IsEnabled() )
1448 if ( item
->GetSubMenu()->ProcessAccelEvent(event
) )
1458 void wxMenu::AddAccelFor(wxMenuItem
*item
)
1460 wxAcceleratorEntry
*accel
= item
->GetAccel();
1463 accel
->SetMenuItem(item
);
1465 m_accelTable
.Add(*accel
);
1471 void wxMenu::RemoveAccelFor(wxMenuItem
*item
)
1473 wxAcceleratorEntry
*accel
= item
->GetAccel();
1476 m_accelTable
.Remove(*accel
);
1482 #endif // wxUSE_ACCEL
1484 // ----------------------------------------------------------------------------
1485 // wxMenuItem construction
1486 // ----------------------------------------------------------------------------
1488 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
1490 const wxString
& text
,
1491 const wxString
& help
,
1494 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
1499 m_radioGroup
.start
= -1;
1500 m_isRadioGroupStart
= FALSE
;
1502 m_bmpDisabled
= wxNullBitmap
;
1507 wxMenuItem::~wxMenuItem()
1511 // ----------------------------------------------------------------------------
1512 // wxMenuItemBase methods implemented here
1513 // ----------------------------------------------------------------------------
1516 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
1518 const wxString
& name
,
1519 const wxString
& help
,
1523 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
1527 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
1529 return wxStripMenuCodes(text
);
1532 // ----------------------------------------------------------------------------
1533 // wxMenuItem operations
1534 // ----------------------------------------------------------------------------
1536 void wxMenuItem::NotifyMenu()
1538 m_parentMenu
->RefreshItem(this);
1541 void wxMenuItem::UpdateAccelInfo()
1543 m_indexAccel
= wxControl::FindAccelIndex(m_text
);
1545 // will be empty if the text contains no TABs - ok
1546 m_strAccel
= m_text
.AfterFirst(_T('\t'));
1549 void wxMenuItem::SetText(const wxString
& text
)
1551 if ( text
!= m_text
)
1553 // first call the base class version to change m_text
1554 wxMenuItemBase::SetText(text
);
1562 void wxMenuItem::SetCheckable(bool checkable
)
1564 if ( checkable
!= IsCheckable() )
1566 wxMenuItemBase::SetCheckable(checkable
);
1572 void wxMenuItem::SetBitmaps(const wxBitmap
& bmpChecked
,
1573 const wxBitmap
& bmpUnchecked
)
1575 m_bmpChecked
= bmpChecked
;
1576 m_bmpUnchecked
= bmpUnchecked
;
1581 void wxMenuItem::Enable(bool enable
)
1583 if ( enable
!= m_isEnabled
)
1585 wxMenuItemBase::Enable(enable
);
1591 void wxMenuItem::Check(bool check
)
1593 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
1595 if ( m_isChecked
== check
)
1598 if ( GetKind() == wxITEM_RADIO
)
1600 // it doesn't make sense to uncheck a radio item - what would this do?
1604 // get the index of this item in the menu
1605 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
1606 int pos
= items
.IndexOf(this);
1607 wxCHECK_RET( pos
!= wxNOT_FOUND
,
1608 _T("menuitem not found in the menu items list?") );
1610 // get the radio group range
1614 if ( m_isRadioGroupStart
)
1616 // we already have all information we need
1618 end
= m_radioGroup
.end
;
1620 else // next radio group item
1622 // get the radio group end from the start item
1623 start
= m_radioGroup
.start
;
1624 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
1627 // also uncheck all the other items in this radio group
1628 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
1629 for ( int n
= start
; n
<= end
&& node
; n
++ )
1633 node
->GetData()->m_isChecked
= FALSE
;
1635 node
= node
->GetNext();
1639 wxMenuItemBase::Check(check
);
1644 // radio group stuff
1645 // -----------------
1647 void wxMenuItem::SetAsRadioGroupStart()
1649 m_isRadioGroupStart
= TRUE
;
1652 void wxMenuItem::SetRadioGroupStart(int start
)
1654 wxASSERT_MSG( !m_isRadioGroupStart
,
1655 _T("should only be called for the next radio items") );
1657 m_radioGroup
.start
= start
;
1660 void wxMenuItem::SetRadioGroupEnd(int end
)
1662 wxASSERT_MSG( m_isRadioGroupStart
,
1663 _T("should only be called for the first radio item") );
1665 m_radioGroup
.end
= end
;
1668 // ----------------------------------------------------------------------------
1669 // wxMenuBar creation
1670 // ----------------------------------------------------------------------------
1672 void wxMenuBar::Init()
1680 m_shouldShowMenu
= FALSE
;
1683 void wxMenuBar::Attach(wxFrame
*frame
)
1685 // maybe you really wanted to call Detach()?
1686 wxCHECK_RET( frame
, _T("wxMenuBar::Attach(NULL) called") );
1688 wxMenuBarBase::Attach(frame
);
1692 // reparent if necessary
1693 if ( m_frameLast
!= frame
)
1698 // show it back - was hidden by Detach()
1701 else // not created yet, do it now
1703 // we have no way to return the error from here anyhow :-(
1704 (void)Create(frame
, -1);
1706 SetCursor(wxCURSOR_ARROW
);
1708 SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT
));
1710 // calculate and set our height (it won't be changed any more)
1711 SetSize(-1, GetBestSize().y
);
1714 // remember the last frame which had us to avoid unnecessarily reparenting
1716 m_frameLast
= frame
;
1719 void wxMenuBar::Detach()
1721 // don't delete the window because we may be reattached later, just hide it
1727 wxMenuBarBase::Detach();
1730 wxMenuBar::~wxMenuBar()
1734 // ----------------------------------------------------------------------------
1735 // wxMenuBar adding/removing items
1736 // ----------------------------------------------------------------------------
1738 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1740 return Insert(GetCount(), menu
, title
);
1743 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1745 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1748 wxMenuInfo
*info
= new wxMenuInfo(title
);
1749 m_menuInfos
.Insert(info
, pos
);
1751 RefreshAllItemsAfter(pos
);
1756 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1758 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1762 wxMenuInfo
& info
= m_menuInfos
[pos
];
1764 info
.SetLabel(title
);
1766 // even if the old menu was disabled, the new one is not any more
1769 // even if we change only this one, the new label has different width,
1770 // so we need to refresh everything beyond this item as well
1771 RefreshAllItemsAfter(pos
);
1777 wxMenu
*wxMenuBar::Remove(size_t pos
)
1779 wxMenu
*menuOld
= wxMenuBarBase::Remove(pos
);
1783 m_menuInfos
.RemoveAt(pos
);
1785 // this doesn't happen too often, so don't try to be too smart - just
1786 // refresh everything
1793 // ----------------------------------------------------------------------------
1794 // wxMenuBar top level menus access
1795 // ----------------------------------------------------------------------------
1797 wxCoord
wxMenuBar::GetItemWidth(size_t pos
) const
1799 return m_menuInfos
[pos
].GetWidth(wxConstCast(this, wxMenuBar
));
1802 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1804 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1806 if ( enable
!= m_menuInfos
[pos
].IsEnabled() )
1808 m_menuInfos
[pos
].SetEnabled(enable
);
1812 //else: nothing to do
1815 bool wxMenuBar::IsEnabledTop(size_t pos
) const
1817 wxCHECK_MSG( pos
< GetCount(), FALSE
, _T("invalid index in IsEnabledTop") );
1819 return m_menuInfos
[pos
].IsEnabled();
1822 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
1824 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1826 if ( label
!= m_menuInfos
[pos
].GetLabel() )
1828 m_menuInfos
[pos
].SetLabel(label
);
1832 //else: nothing to do
1835 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
1837 wxCHECK_MSG( pos
< GetCount(), _T(""), _T("invalid index in GetLabelTop") );
1839 return m_menuInfos
[pos
].GetLabel();
1842 // ----------------------------------------------------------------------------
1843 // wxMenuBar drawing
1844 // ----------------------------------------------------------------------------
1846 void wxMenuBar::RefreshAllItemsAfter(size_t pos
)
1850 // no need to refresh if nothing is shown yet
1854 wxRect rect
= GetItemRect(pos
);
1855 rect
.width
= GetClientSize().x
- rect
.x
;
1859 void wxMenuBar::RefreshItem(size_t pos
)
1861 wxCHECK_RET( pos
!= (size_t)-1,
1862 _T("invalid item in wxMenuBar::RefreshItem") );
1866 // no need to refresh if nothing is shown yet
1870 RefreshRect(GetItemRect(pos
));
1873 void wxMenuBar::DoDraw(wxControlRenderer
*renderer
)
1875 wxDC
& dc
= renderer
->GetDC();
1876 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1878 // redraw only the items which must be redrawn
1880 // we don't have to use GetUpdateClientRect() here because our client rect
1881 // is the same as total one
1882 wxRect rectUpdate
= GetUpdateRegion().GetBox();
1884 int flagsMenubar
= GetStateFlags();
1888 rect
.height
= GetClientSize().y
;
1891 size_t count
= GetCount();
1892 for ( size_t n
= 0; n
< count
; n
++ )
1894 if ( x
> rectUpdate
.GetRight() )
1896 // all remaining items are to the right of rectUpdate
1901 rect
.width
= GetItemWidth(n
);
1903 if ( x
< rectUpdate
.x
)
1905 // this item is still to the left of rectUpdate
1909 int flags
= flagsMenubar
;
1910 if ( m_current
!= -1 && n
== (size_t)m_current
)
1912 flags
|= wxCONTROL_SELECTED
;
1915 if ( !IsEnabledTop(n
) )
1917 flags
|= wxCONTROL_DISABLED
;
1920 GetRenderer()->DrawMenuBarItem
1924 m_menuInfos
[n
].GetLabel(),
1926 m_menuInfos
[n
].GetAccelIndex()
1931 // ----------------------------------------------------------------------------
1932 // wxMenuBar geometry
1933 // ----------------------------------------------------------------------------
1935 wxRect
wxMenuBar::GetItemRect(size_t pos
) const
1937 wxASSERT_MSG( pos
< GetCount(), _T("invalid menu bar item index") );
1938 wxASSERT_MSG( IsCreated(), _T("can't call this method yet") );
1943 rect
.height
= GetClientSize().y
;
1945 for ( size_t n
= 0; n
< pos
; n
++ )
1947 rect
.x
+= GetItemWidth(n
);
1950 rect
.width
= GetItemWidth(pos
);
1955 wxSize
wxMenuBar::DoGetBestClientSize() const
1958 if ( GetMenuCount() > 0 )
1960 wxClientDC
dc(wxConstCast(this, wxMenuBar
));
1961 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1962 dc
.GetTextExtent(GetLabelTop(0), &size
.x
, &size
.y
);
1964 // adjust for the renderer we use
1965 size
= GetRenderer()->GetMenuBarItemSize(size
);
1967 else // empty menubar
1973 // the width is arbitrary, of course, for horizontal menubar
1979 int wxMenuBar::GetMenuFromPoint(const wxPoint
& pos
) const
1981 if ( pos
.x
< 0 || pos
.y
< 0 || pos
.y
> GetClientSize().y
)
1986 size_t count
= GetCount();
1987 for ( size_t item
= 0; item
< count
; item
++ )
1989 x
+= GetItemWidth(item
);
1997 // to the right of the last menu item
2001 // ----------------------------------------------------------------------------
2002 // wxMenuBar menu operations
2003 // ----------------------------------------------------------------------------
2005 void wxMenuBar::SelectMenu(size_t pos
)
2008 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::SelectMenu"));
2014 void wxMenuBar::DoSelectMenu(size_t pos
)
2016 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in DoSelectMenu") );
2018 int posOld
= m_current
;
2024 // close the previous menu
2025 if ( IsShowingMenu() )
2027 // restore m_shouldShowMenu flag after DismissMenu() which resets
2029 bool old
= m_shouldShowMenu
;
2033 m_shouldShowMenu
= old
;
2036 RefreshItem((size_t)posOld
);
2042 void wxMenuBar::PopupMenu(size_t pos
)
2044 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in PopupCurrentMenu") );
2051 // ----------------------------------------------------------------------------
2052 // wxMenuBar input handing
2053 // ----------------------------------------------------------------------------
2056 Note that wxMenuBar doesn't use wxInputHandler but handles keyboard and
2057 mouse in the same way under all platforms. This is because it doesn't derive
2058 from wxControl (which works with input handlers) but directly from wxWindow.
2060 Also, menu bar input handling is rather simple, so maybe it's not really
2061 worth making it themeable - at least I've decided against doing it now as it
2062 would merging the changes back into trunk more difficult. But it still could
2063 be done later if really needed.
2066 void wxMenuBar::OnKillFocus(wxFocusEvent
& event
)
2068 if ( m_current
!= -1 )
2070 RefreshItem((size_t)m_current
);
2078 void wxMenuBar::OnLeftDown(wxMouseEvent
& event
)
2086 else // we didn't have mouse capture, capture it now
2088 m_current
= GetMenuFromPoint(event
.GetPosition());
2089 if ( m_current
== -1 )
2091 // unfortunately, we can't prevent wxMSW from giving us the focus,
2092 // so we can only give it back
2097 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::OnLeftDown"));
2100 // show it as selected
2101 RefreshItem((size_t)m_current
);
2104 PopupCurrentMenu(FALSE
/* don't select first item - as Windows does */);
2109 void wxMenuBar::OnMouseMove(wxMouseEvent
& event
)
2113 (void)ProcessMouseEvent(event
.GetPosition());
2121 bool wxMenuBar::ProcessMouseEvent(const wxPoint
& pt
)
2123 // a hack to ignore the extra mouse events MSW sends us: this is similar to
2124 // wxUSE_MOUSEEVENT_HACK in wxWin itself but it isn't enough for us here as
2125 // we get the messages from different windows (old and new popup menus for
2128 static wxPoint s_ptLast
;
2129 if ( pt
== s_ptLast
)
2137 int currentNew
= GetMenuFromPoint(pt
);
2138 if ( (currentNew
== -1) || (currentNew
== m_current
) )
2143 // select the new active item
2144 DoSelectMenu(currentNew
);
2146 // show the menu if we know that we should, even if we hadn't been showing
2147 // it before (this may happen if the previous menu was disabled)
2148 if ( m_shouldShowMenu
&& !m_menuShown
)
2150 // open the new menu if the old one we closed had been opened
2151 PopupCurrentMenu(FALSE
/* don't select first item - as Windows does */);
2157 void wxMenuBar::OnKeyDown(wxKeyEvent
& event
)
2159 // ensure that we have a current item - we might not have it if we're
2160 // given the focus with Alt or F10 press (and under GTK+ the menubar
2161 // somehow gets the keyboard events even when it doesn't have focus...)
2162 if ( m_current
== -1 )
2164 if ( !HasCapture() )
2168 else // we do have capture
2170 // we always maintain a valid current item while we're in modal
2171 // state (i.e. have the capture)
2172 wxFAIL_MSG( _T("how did we manage to lose current item?") );
2178 int key
= event
.GetKeyCode();
2180 // first let the menu have it
2181 if ( IsShowingMenu() && m_menuShown
->ProcessKeyDown(key
) )
2186 // cycle through the menu items when left/right arrows are pressed and open
2187 // the menu when up/down one is
2191 // Alt must be processed at wxWindow level too
2196 // remove the selection and give the focus away
2197 if ( m_current
!= -1 )
2199 if ( IsShowingMenu() )
2211 size_t count
= GetCount();
2214 // the item won't change anyhow
2217 //else: otherwise, it will
2219 // remember if we were showing a menu - if we did, we should
2220 // show the new menu after changing the item
2221 bool wasMenuOpened
= IsShowingMenu();
2222 if ( wasMenuOpened
)
2227 // cast is safe as we tested for -1 above
2228 size_t currentNew
= (size_t)m_current
;
2230 if ( key
== WXK_LEFT
)
2232 if ( currentNew
-- == 0 )
2233 currentNew
= count
- 1;
2237 if ( ++currentNew
== count
)
2241 DoSelectMenu(currentNew
);
2243 if ( wasMenuOpened
)
2258 // letters open the corresponding menu
2261 int idxFound
= FindNextItemForAccel(m_current
, key
, &unique
);
2263 if ( idxFound
!= -1 )
2265 if ( IsShowingMenu() )
2270 DoSelectMenu((size_t)idxFound
);
2272 // if the item is not unique, just select it but don't
2273 // activate as the user might have wanted to activate
2276 // also, don't try to open a disabled menu
2277 if ( unique
&& IsEnabledTop((size_t)idxFound
) )
2283 // skip the "event.Skip()" below
2292 // ----------------------------------------------------------------------------
2293 // wxMenuBar accel handling
2294 // ----------------------------------------------------------------------------
2296 int wxMenuBar::FindNextItemForAccel(int idxStart
, int key
, bool *unique
) const
2298 if ( !wxIsalnum(key
) )
2300 // we only support letters/digits as accels
2304 // do we have more than one item with this accel?
2308 // translate everything to lower case before comparing
2309 wxChar chAccel
= wxTolower(key
);
2311 // the index of the item with this accel
2314 // loop through all items searching for the item with this
2315 // accel starting at the item after the current one
2316 int count
= GetCount();
2317 int n
= idxStart
== -1 ? 0 : idxStart
+ 1;
2328 const wxMenuInfo
& info
= m_menuInfos
[n
];
2330 int idxAccel
= info
.GetAccelIndex();
2331 if ( idxAccel
!= -1 &&
2332 wxTolower(info
.GetLabel()[(size_t)idxAccel
])
2335 // ok, found an item with this accel
2336 if ( idxFound
== -1 )
2338 // store it but continue searching as we need to
2339 // know if it's the only item with this accel or if
2343 else // we already had found such item
2348 // no need to continue further, we won't find
2349 // anything we don't already know
2354 // we want to iterate over all items wrapping around if
2362 if ( n
== idxStart
)
2364 // we've seen all items
2374 bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent
& event
)
2377 for ( wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
2379 node
= node
->GetNext(), n
++ )
2381 // accels of the items in the disabled menus shouldn't work
2382 if ( m_menuInfos
[n
].IsEnabled() )
2384 if ( node
->GetData()->ProcessAccelEvent(event
) )
2386 // menu processed it
2396 #endif // wxUSE_ACCEL
2398 // ----------------------------------------------------------------------------
2399 // wxMenuBar menus showing
2400 // ----------------------------------------------------------------------------
2402 void wxMenuBar::PopupCurrentMenu(bool selectFirst
)
2404 wxCHECK_RET( m_current
!= -1, _T("no menu to popup") );
2406 // forgot to call DismissMenu()?
2407 wxASSERT_MSG( !m_menuShown
, _T("shouldn't show two menus at once!") );
2409 // in any case, we should show it - even if we won't
2410 m_shouldShowMenu
= TRUE
;
2412 if ( IsEnabledTop(m_current
) )
2414 // remember the menu we show
2415 m_menuShown
= GetMenu(m_current
);
2417 // we don't show the menu at all if it has no items
2418 if ( !m_menuShown
->IsEmpty() )
2420 // position it correctly: note that we must use screen coords and
2421 // that we pass 0 as width to position the menu exactly below the
2422 // item, not to the right of it
2423 wxRect rectItem
= GetItemRect(m_current
);
2425 m_menuShown
->Popup(ClientToScreen(rectItem
.GetPosition()),
2426 wxSize(0, rectItem
.GetHeight()),
2431 // reset it back as no menu is shown
2435 //else: don't show disabled menu
2438 void wxMenuBar::DismissMenu()
2440 wxCHECK_RET( m_menuShown
, _T("can't dismiss menu if none is shown") );
2442 m_menuShown
->Dismiss();
2446 void wxMenuBar::OnDismissMenu(bool dismissMenuBar
)
2448 m_shouldShowMenu
= FALSE
;
2450 if ( dismissMenuBar
)
2456 void wxMenuBar::OnDismiss()
2460 wxLogTrace(_T("mousecapture"), _T("Releasing mouse from wxMenuBar::OnDismiss"));
2461 GetCapture()->ReleaseMouse();
2464 if ( m_current
!= -1 )
2466 size_t current
= m_current
;
2469 RefreshItem(current
);
2475 void wxMenuBar::GiveAwayFocus()
2477 GetFrame()->SetFocus();
2480 // ----------------------------------------------------------------------------
2481 // popup menu support
2482 // ----------------------------------------------------------------------------
2484 wxEventLoop
*wxWindow::ms_evtLoopPopup
= NULL
;
2486 bool wxWindow::DoPopupMenu(wxMenu
*menu
, int x
, int y
)
2488 wxCHECK_MSG( !ms_evtLoopPopup
, FALSE
,
2489 _T("can't show more than one popup menu at a time") );
2492 // we need to change the cursor before showing the menu as, apparently, no
2493 // cursor changes took place while the mouse is captured
2494 wxCursor cursorOld
= GetCursor();
2495 SetCursor(wxCURSOR_ARROW
);
2499 // flash any delayed log messages before showing the menu, otherwise it
2500 // could be dismissed (because it would lose focus) immediately after being
2502 wxLog::FlushActive();
2504 // some controls update themselves from OnIdle() call - let them do it
2505 wxTheApp
->ProcessIdle();
2507 // if the window hadn't been refreshed yet, the menu can adversely affect
2508 // its next OnPaint() handler execution - i.e. scrolled window refresh
2509 // logic breaks then as it scrolls part of the menu which hadn't been there
2510 // when the update event was generated into view
2514 menu
->SetInvokingWindow(this);
2516 // wxLogDebug( "Name of invoking window %s", menu->GetInvokingWindow()->GetName().c_str() );
2518 menu
->Popup(ClientToScreen(wxPoint(x
, y
)), wxSize(0, 0));
2520 // this is not very useful if the menu was popped up because of the mouse
2521 // click but I think it is nice to do when it appears because of a key
2522 // press (i.e. Windows menu key)
2524 // Windows itself doesn't do it, but IMHO this is nice
2527 // we have to redirect all keyboard input to the menu temporarily
2528 PushEventHandler(new wxMenuKbdRedirector(menu
));
2530 // enter the local modal loop
2531 ms_evtLoopPopup
= new wxEventLoop
;
2532 ms_evtLoopPopup
->Run();
2534 delete ms_evtLoopPopup
;
2535 ms_evtLoopPopup
= NULL
;
2537 // remove the handler
2538 PopEventHandler(TRUE
/* delete it */);
2540 menu
->SetInvokingWindow(NULL
);
2543 SetCursor(cursorOld
);
2549 void wxWindow::DismissPopupMenu()
2551 wxCHECK_RET( ms_evtLoopPopup
, _T("no popup menu shown") );
2553 ms_evtLoopPopup
->Exit();
2556 #endif // wxUSE_MENUS