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
)
1127 if ( item
->GetKind() == wxITEM_RADIO
)
1129 int count
= GetMenuItemCount();
1131 if ( m_startRadioGroup
== -1 )
1133 // start a new radio group
1134 m_startRadioGroup
= count
;
1136 // for now it has just one element
1137 item
->SetAsRadioGroupStart();
1138 item
->SetRadioGroupEnd(m_startRadioGroup
);
1140 else // extend the current radio group
1142 // we need to update its end item
1143 item
->SetRadioGroupStart(m_startRadioGroup
);
1144 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
1148 node
->GetData()->SetRadioGroupEnd(count
);
1152 wxFAIL_MSG( _T("where is the radio group start item?") );
1156 else // not a radio item
1161 if ( !wxMenuBase::DoAppend(item
) )
1169 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1171 if ( !wxMenuBase::DoInsert(pos
, item
) )
1179 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1181 wxMenuItem
*itemOld
= wxMenuBase::DoRemove(item
);
1185 InvalidateGeometryInfo();
1188 RemoveAccelFor(item
);
1189 #endif // wxUSE_ACCEL
1195 // ----------------------------------------------------------------------------
1196 // wxMenu attaching/detaching
1197 // ----------------------------------------------------------------------------
1199 void wxMenu::Attach(wxMenuBarBase
*menubar
)
1201 wxMenuBase::Attach(menubar
);
1203 wxCHECK_RET( m_menuBar
, _T("menubar can't be NULL after attaching") );
1205 // unfortunately, we can't use m_menuBar->GetEventHandler() here because,
1206 // if the menubar is currently showing a menu, its event handler is a
1207 // temporary one installed by wxPopupWindow and so will disappear soon any
1208 // any attempts to use it from the newly attached menu would result in a
1211 // so we use the menubar itself, even if it's a pity as it means we can't
1212 // redirect all menu events by changing the menubar handler (FIXME)
1213 SetNextHandler(m_menuBar
);
1216 void wxMenu::Detach()
1218 wxMenuBase::Detach();
1221 // ----------------------------------------------------------------------------
1222 // wxMenu misc functions
1223 // ----------------------------------------------------------------------------
1225 wxWindow
*wxMenu::GetRootWindow() const
1229 // simple case - a normal menu attached to the menubar
1230 return GetMenuBar();
1233 // we're a popup menu but the trouble is that only the top level popup menu
1234 // has a pointer to the invoking window, so we must walk up the menu chain
1236 wxWindow
*win
= GetInvokingWindow();
1239 // we already have it
1243 wxMenu
*menu
= GetParent();
1246 // We are a submenu of a menu of a menubar
1247 if (menu
->GetMenuBar())
1248 return menu
->GetMenuBar();
1250 win
= menu
->GetInvokingWindow();
1254 menu
= menu
->GetParent();
1257 // we're probably going to crash in the caller anyhow, but try to detect
1258 // this error as soon as possible
1259 wxASSERT_MSG( win
, _T("menu without any associated window?") );
1261 // also remember it in this menu so that we don't have to search for it the
1263 wxConstCast(this, wxMenu
)->m_invokingWindow
= win
;
1268 wxRenderer
*wxMenu::GetRenderer() const
1270 // we're going to crash without renderer!
1271 wxCHECK_MSG( m_popupMenu
, NULL
, _T("neither popup nor menubar menu?") );
1273 return m_popupMenu
->GetRenderer();
1276 void wxMenu::RefreshItem(wxMenuItem
*item
)
1278 // the item geometry changed, so our might have changed as well
1279 InvalidateGeometryInfo();
1283 // this would be a bug in IsShown()
1284 wxCHECK_RET( m_popupMenu
, _T("must have popup window if shown!") );
1286 // recalc geometry to update the item height and such
1287 (void)GetGeometryInfo();
1289 m_popupMenu
->RefreshItem(item
);
1293 // ----------------------------------------------------------------------------
1294 // wxMenu showing and hiding
1295 // ----------------------------------------------------------------------------
1297 bool wxMenu::IsShown() const
1299 return m_popupMenu
&& m_popupMenu
->IsShown();
1302 void wxMenu::OnDismiss(bool dismissParent
)
1306 // always notify the parent about submenu disappearance
1307 wxPopupMenuWindow
*win
= m_menuParent
->m_popupMenu
;
1310 win
->OnSubmenuDismiss();
1314 wxFAIL_MSG( _T("parent menu not shown?") );
1317 // and if we dismiss everything, propagate to parent
1318 if ( dismissParent
)
1320 // dismissParent is recursive
1321 m_menuParent
->Dismiss();
1322 m_menuParent
->OnDismiss(true);
1325 else // no parent menu
1327 // notify the menu bar if we're a top level menu
1330 m_menuBar
->OnDismissMenu(dismissParent
);
1334 wxCHECK_RET( m_invokingWindow
, _T("what kind of menu is this?") );
1336 m_invokingWindow
->DismissPopupMenu();
1338 // Why reset it here? We need it for sending the event to...
1339 // SetInvokingWindow(NULL);
1344 void wxMenu::Popup(const wxPoint
& pos
, const wxSize
& size
, bool selectFirst
)
1346 // create the popup window if not done yet
1349 m_popupMenu
= new wxPopupMenuWindow(GetRootWindow(), this);
1352 // select the first item unless disabled
1355 m_popupMenu
->SelectFirst();
1358 // the geometry might have changed since the last time we were shown, so
1360 m_popupMenu
->SetClientSize(GetGeometryInfo().GetSize());
1362 // position it as specified
1363 m_popupMenu
->Position(pos
, size
);
1365 // the menu can't have the focus itself (it is a Windows limitation), so
1366 // always keep the focus at the originating window
1367 wxWindow
*focus
= GetRootWindow();
1369 wxASSERT_MSG( focus
, _T("no window to keep focus on?") );
1372 m_popupMenu
->Popup(focus
);
1375 void wxMenu::Dismiss()
1377 wxCHECK_RET( IsShown(), _T("can't dismiss hidden menu") );
1379 m_popupMenu
->Dismiss();
1382 // ----------------------------------------------------------------------------
1383 // wxMenu event processing
1384 // ----------------------------------------------------------------------------
1386 bool wxMenu::ProcessKeyDown(int key
)
1388 wxCHECK_MSG( m_popupMenu
, false,
1389 _T("can't process key events if not shown") );
1391 return m_popupMenu
->ProcessKeyDown(key
);
1394 bool wxMenu::ClickItem(wxMenuItem
*item
)
1397 if ( item
->IsCheckable() )
1399 // update the item state
1400 isChecked
= !item
->IsChecked();
1402 item
->Check(isChecked
!= 0);
1410 return SendEvent(item
->GetId(), isChecked
);
1413 // ----------------------------------------------------------------------------
1414 // wxMenu accel support
1415 // ----------------------------------------------------------------------------
1419 bool wxMenu::ProcessAccelEvent(const wxKeyEvent
& event
)
1421 // do we have an item for this accel?
1422 wxMenuItem
*item
= m_accelTable
.GetMenuItem(event
);
1423 if ( item
&& item
->IsEnabled() )
1425 return ClickItem(item
);
1429 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
1431 node
= node
->GetNext() )
1433 const wxMenuItem
*item
= node
->GetData();
1434 if ( item
->IsSubMenu() && item
->IsEnabled() )
1437 if ( item
->GetSubMenu()->ProcessAccelEvent(event
) )
1447 void wxMenu::AddAccelFor(wxMenuItem
*item
)
1449 wxAcceleratorEntry
*accel
= item
->GetAccel();
1452 accel
->SetMenuItem(item
);
1454 m_accelTable
.Add(*accel
);
1460 void wxMenu::RemoveAccelFor(wxMenuItem
*item
)
1462 wxAcceleratorEntry
*accel
= item
->GetAccel();
1465 m_accelTable
.Remove(*accel
);
1471 #endif // wxUSE_ACCEL
1473 // ----------------------------------------------------------------------------
1474 // wxMenuItem construction
1475 // ----------------------------------------------------------------------------
1477 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
1479 const wxString
& text
,
1480 const wxString
& help
,
1483 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
1486 m_height
= wxDefaultCoord
;
1488 m_radioGroup
.start
= -1;
1489 m_isRadioGroupStart
= false;
1491 m_bmpDisabled
= wxNullBitmap
;
1496 wxMenuItem::~wxMenuItem()
1500 // ----------------------------------------------------------------------------
1501 // wxMenuItemBase methods implemented here
1502 // ----------------------------------------------------------------------------
1505 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
1507 const wxString
& name
,
1508 const wxString
& help
,
1512 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
1516 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
1518 return wxStripMenuCodes(text
);
1521 // ----------------------------------------------------------------------------
1522 // wxMenuItem operations
1523 // ----------------------------------------------------------------------------
1525 void wxMenuItem::NotifyMenu()
1527 m_parentMenu
->RefreshItem(this);
1530 void wxMenuItem::UpdateAccelInfo()
1532 m_indexAccel
= wxControl::FindAccelIndex(m_text
);
1534 // will be empty if the text contains no TABs - ok
1535 m_strAccel
= m_text
.AfterFirst(_T('\t'));
1538 void wxMenuItem::SetText(const wxString
& text
)
1540 if ( text
!= m_text
)
1542 // first call the base class version to change m_text
1543 wxMenuItemBase::SetText(text
);
1551 void wxMenuItem::SetCheckable(bool checkable
)
1553 if ( checkable
!= IsCheckable() )
1555 wxMenuItemBase::SetCheckable(checkable
);
1561 void wxMenuItem::SetBitmaps(const wxBitmap
& bmpChecked
,
1562 const wxBitmap
& bmpUnchecked
)
1564 m_bmpChecked
= bmpChecked
;
1565 m_bmpUnchecked
= bmpUnchecked
;
1570 void wxMenuItem::Enable(bool enable
)
1572 if ( enable
!= m_isEnabled
)
1574 wxMenuItemBase::Enable(enable
);
1580 void wxMenuItem::Check(bool check
)
1582 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
1584 if ( m_isChecked
== check
)
1587 if ( GetKind() == wxITEM_RADIO
)
1589 // it doesn't make sense to uncheck a radio item - what would this do?
1593 // get the index of this item in the menu
1594 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
1595 int pos
= items
.IndexOf(this);
1596 wxCHECK_RET( pos
!= wxNOT_FOUND
,
1597 _T("menuitem not found in the menu items list?") );
1599 // get the radio group range
1603 if ( m_isRadioGroupStart
)
1605 // we already have all information we need
1607 end
= m_radioGroup
.end
;
1609 else // next radio group item
1611 // get the radio group end from the start item
1612 start
= m_radioGroup
.start
;
1613 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
1616 // also uncheck all the other items in this radio group
1617 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
1618 for ( int n
= start
; n
<= end
&& node
; n
++ )
1622 node
->GetData()->m_isChecked
= false;
1624 node
= node
->GetNext();
1628 wxMenuItemBase::Check(check
);
1633 // radio group stuff
1634 // -----------------
1636 void wxMenuItem::SetAsRadioGroupStart()
1638 m_isRadioGroupStart
= true;
1641 void wxMenuItem::SetRadioGroupStart(int start
)
1643 wxASSERT_MSG( !m_isRadioGroupStart
,
1644 _T("should only be called for the next radio items") );
1646 m_radioGroup
.start
= start
;
1649 void wxMenuItem::SetRadioGroupEnd(int end
)
1651 wxASSERT_MSG( m_isRadioGroupStart
,
1652 _T("should only be called for the first radio item") );
1654 m_radioGroup
.end
= end
;
1657 // ----------------------------------------------------------------------------
1658 // wxMenuBar creation
1659 // ----------------------------------------------------------------------------
1661 void wxMenuBar::Init()
1669 m_shouldShowMenu
= false;
1672 void wxMenuBar::Attach(wxFrame
*frame
)
1674 // maybe you really wanted to call Detach()?
1675 wxCHECK_RET( frame
, _T("wxMenuBar::Attach(NULL) called") );
1677 wxMenuBarBase::Attach(frame
);
1681 // reparent if necessary
1682 if ( m_frameLast
!= frame
)
1687 // show it back - was hidden by Detach()
1690 else // not created yet, do it now
1692 // we have no way to return the error from here anyhow :-(
1693 (void)Create(frame
, wxID_ANY
);
1695 SetCursor(wxCURSOR_ARROW
);
1697 SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT
));
1699 // calculate and set our height (it won't be changed any more)
1700 SetSize(wxDefaultCoord
, GetBestSize().y
);
1703 // remember the last frame which had us to avoid unnecessarily reparenting
1705 m_frameLast
= frame
;
1708 void wxMenuBar::Detach()
1710 // don't delete the window because we may be reattached later, just hide it
1716 wxMenuBarBase::Detach();
1719 wxMenuBar::~wxMenuBar()
1723 // ----------------------------------------------------------------------------
1724 // wxMenuBar adding/removing items
1725 // ----------------------------------------------------------------------------
1727 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1729 return Insert(GetCount(), menu
, title
);
1732 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1734 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1737 wxMenuInfo
*info
= new wxMenuInfo(title
);
1738 m_menuInfos
.Insert(info
, pos
);
1740 RefreshAllItemsAfter(pos
);
1745 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1747 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1751 wxMenuInfo
& info
= m_menuInfos
[pos
];
1753 info
.SetLabel(title
);
1755 // even if the old menu was disabled, the new one is not any more
1758 // even if we change only this one, the new label has different width,
1759 // so we need to refresh everything beyond this item as well
1760 RefreshAllItemsAfter(pos
);
1766 wxMenu
*wxMenuBar::Remove(size_t pos
)
1768 wxMenu
*menuOld
= wxMenuBarBase::Remove(pos
);
1772 m_menuInfos
.RemoveAt(pos
);
1774 // this doesn't happen too often, so don't try to be too smart - just
1775 // refresh everything
1782 // ----------------------------------------------------------------------------
1783 // wxMenuBar top level menus access
1784 // ----------------------------------------------------------------------------
1786 wxCoord
wxMenuBar::GetItemWidth(size_t pos
) const
1788 return m_menuInfos
[pos
].GetWidth(wxConstCast(this, wxMenuBar
));
1791 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1793 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1795 if ( enable
!= m_menuInfos
[pos
].IsEnabled() )
1797 m_menuInfos
[pos
].SetEnabled(enable
);
1801 //else: nothing to do
1804 bool wxMenuBar::IsEnabledTop(size_t pos
) const
1806 wxCHECK_MSG( pos
< GetCount(), false, _T("invalid index in IsEnabledTop") );
1808 return m_menuInfos
[pos
].IsEnabled();
1811 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
1813 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1815 if ( label
!= m_menuInfos
[pos
].GetLabel() )
1817 m_menuInfos
[pos
].SetLabel(label
);
1821 //else: nothing to do
1824 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
1826 wxCHECK_MSG( pos
< GetCount(), _T(""), _T("invalid index in GetLabelTop") );
1828 return m_menuInfos
[pos
].GetLabel();
1831 // ----------------------------------------------------------------------------
1832 // wxMenuBar drawing
1833 // ----------------------------------------------------------------------------
1835 void wxMenuBar::RefreshAllItemsAfter(size_t pos
)
1839 // no need to refresh if nothing is shown yet
1843 wxRect rect
= GetItemRect(pos
);
1844 rect
.width
= GetClientSize().x
- rect
.x
;
1848 void wxMenuBar::RefreshItem(size_t pos
)
1850 wxCHECK_RET( pos
!= (size_t)-1,
1851 _T("invalid item in wxMenuBar::RefreshItem") );
1855 // no need to refresh if nothing is shown yet
1859 RefreshRect(GetItemRect(pos
));
1862 void wxMenuBar::DoDraw(wxControlRenderer
*renderer
)
1864 wxDC
& dc
= renderer
->GetDC();
1865 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1867 // redraw only the items which must be redrawn
1869 // we don't have to use GetUpdateClientRect() here because our client rect
1870 // is the same as total one
1871 wxRect rectUpdate
= GetUpdateRegion().GetBox();
1873 int flagsMenubar
= GetStateFlags();
1877 rect
.height
= GetClientSize().y
;
1880 size_t count
= GetCount();
1881 for ( size_t n
= 0; n
< count
; n
++ )
1883 if ( x
> rectUpdate
.GetRight() )
1885 // all remaining items are to the right of rectUpdate
1890 rect
.width
= GetItemWidth(n
);
1892 if ( x
< rectUpdate
.x
)
1894 // this item is still to the left of rectUpdate
1898 int flags
= flagsMenubar
;
1899 if ( m_current
!= -1 && n
== (size_t)m_current
)
1901 flags
|= wxCONTROL_SELECTED
;
1904 if ( !IsEnabledTop(n
) )
1906 flags
|= wxCONTROL_DISABLED
;
1909 GetRenderer()->DrawMenuBarItem
1913 m_menuInfos
[n
].GetLabel(),
1915 m_menuInfos
[n
].GetAccelIndex()
1920 // ----------------------------------------------------------------------------
1921 // wxMenuBar geometry
1922 // ----------------------------------------------------------------------------
1924 wxRect
wxMenuBar::GetItemRect(size_t pos
) const
1926 wxASSERT_MSG( pos
< GetCount(), _T("invalid menu bar item index") );
1927 wxASSERT_MSG( IsCreated(), _T("can't call this method yet") );
1932 rect
.height
= GetClientSize().y
;
1934 for ( size_t n
= 0; n
< pos
; n
++ )
1936 rect
.x
+= GetItemWidth(n
);
1939 rect
.width
= GetItemWidth(pos
);
1944 wxSize
wxMenuBar::DoGetBestClientSize() const
1947 if ( GetMenuCount() > 0 )
1949 wxClientDC
dc(wxConstCast(this, wxMenuBar
));
1950 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1951 dc
.GetTextExtent(GetLabelTop(0), &size
.x
, &size
.y
);
1953 // adjust for the renderer we use
1954 size
= GetRenderer()->GetMenuBarItemSize(size
);
1956 else // empty menubar
1962 // the width is arbitrary, of course, for horizontal menubar
1968 int wxMenuBar::GetMenuFromPoint(const wxPoint
& pos
) const
1970 if ( pos
.x
< 0 || pos
.y
< 0 || pos
.y
> GetClientSize().y
)
1975 size_t count
= GetCount();
1976 for ( size_t item
= 0; item
< count
; item
++ )
1978 x
+= GetItemWidth(item
);
1986 // to the right of the last menu item
1990 // ----------------------------------------------------------------------------
1991 // wxMenuBar menu operations
1992 // ----------------------------------------------------------------------------
1994 void wxMenuBar::SelectMenu(size_t pos
)
1997 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::SelectMenu"));
2003 void wxMenuBar::DoSelectMenu(size_t pos
)
2005 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in DoSelectMenu") );
2007 int posOld
= m_current
;
2013 // close the previous menu
2014 if ( IsShowingMenu() )
2016 // restore m_shouldShowMenu flag after DismissMenu() which resets
2018 bool old
= m_shouldShowMenu
;
2022 m_shouldShowMenu
= old
;
2025 RefreshItem((size_t)posOld
);
2031 void wxMenuBar::PopupMenu(size_t pos
)
2033 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in PopupCurrentMenu") );
2040 // ----------------------------------------------------------------------------
2041 // wxMenuBar input handing
2042 // ----------------------------------------------------------------------------
2045 Note that wxMenuBar doesn't use wxInputHandler but handles keyboard and
2046 mouse in the same way under all platforms. This is because it doesn't derive
2047 from wxControl (which works with input handlers) but directly from wxWindow.
2049 Also, menu bar input handling is rather simple, so maybe it's not really
2050 worth making it themeable - at least I've decided against doing it now as it
2051 would merging the changes back into trunk more difficult. But it still could
2052 be done later if really needed.
2055 void wxMenuBar::OnKillFocus(wxFocusEvent
& event
)
2057 if ( m_current
!= -1 )
2059 RefreshItem((size_t)m_current
);
2067 void wxMenuBar::OnLeftDown(wxMouseEvent
& event
)
2075 else // we didn't have mouse capture, capture it now
2077 m_current
= GetMenuFromPoint(event
.GetPosition());
2078 if ( m_current
== -1 )
2080 // unfortunately, we can't prevent wxMSW from giving us the focus,
2081 // so we can only give it back
2086 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::OnLeftDown"));
2089 // show it as selected
2090 RefreshItem((size_t)m_current
);
2093 PopupCurrentMenu(false /* don't select first item - as Windows does */);
2098 void wxMenuBar::OnMouseMove(wxMouseEvent
& event
)
2102 (void)ProcessMouseEvent(event
.GetPosition());
2110 bool wxMenuBar::ProcessMouseEvent(const wxPoint
& pt
)
2112 // a hack to ignore the extra mouse events MSW sends us: this is similar to
2113 // wxUSE_MOUSEEVENT_HACK in wxWin itself but it isn't enough for us here as
2114 // we get the messages from different windows (old and new popup menus for
2117 static wxPoint s_ptLast
;
2118 if ( pt
== s_ptLast
)
2126 int currentNew
= GetMenuFromPoint(pt
);
2127 if ( (currentNew
== -1) || (currentNew
== m_current
) )
2132 // select the new active item
2133 DoSelectMenu(currentNew
);
2135 // show the menu if we know that we should, even if we hadn't been showing
2136 // it before (this may happen if the previous menu was disabled)
2137 if ( m_shouldShowMenu
&& !m_menuShown
)
2139 // open the new menu if the old one we closed had been opened
2140 PopupCurrentMenu(false /* don't select first item - as Windows does */);
2146 void wxMenuBar::OnKeyDown(wxKeyEvent
& event
)
2148 // ensure that we have a current item - we might not have it if we're
2149 // given the focus with Alt or F10 press (and under GTK+ the menubar
2150 // somehow gets the keyboard events even when it doesn't have focus...)
2151 if ( m_current
== -1 )
2153 if ( !HasCapture() )
2157 else // we do have capture
2159 // we always maintain a valid current item while we're in modal
2160 // state (i.e. have the capture)
2161 wxFAIL_MSG( _T("how did we manage to lose current item?") );
2167 int key
= event
.GetKeyCode();
2169 // first let the menu have it
2170 if ( IsShowingMenu() && m_menuShown
->ProcessKeyDown(key
) )
2175 // cycle through the menu items when left/right arrows are pressed and open
2176 // the menu when up/down one is
2180 // Alt must be processed at wxWindow level too
2185 // remove the selection and give the focus away
2186 if ( m_current
!= -1 )
2188 if ( IsShowingMenu() )
2200 size_t count
= GetCount();
2203 // the item won't change anyhow
2206 //else: otherwise, it will
2208 // remember if we were showing a menu - if we did, we should
2209 // show the new menu after changing the item
2210 bool wasMenuOpened
= IsShowingMenu();
2211 if ( wasMenuOpened
)
2216 // cast is safe as we tested for -1 above
2217 size_t currentNew
= (size_t)m_current
;
2219 if ( key
== WXK_LEFT
)
2221 if ( currentNew
-- == 0 )
2222 currentNew
= count
- 1;
2226 if ( ++currentNew
== count
)
2230 DoSelectMenu(currentNew
);
2232 if ( wasMenuOpened
)
2247 // letters open the corresponding menu
2250 int idxFound
= FindNextItemForAccel(m_current
, key
, &unique
);
2252 if ( idxFound
!= -1 )
2254 if ( IsShowingMenu() )
2259 DoSelectMenu((size_t)idxFound
);
2261 // if the item is not unique, just select it but don't
2262 // activate as the user might have wanted to activate
2265 // also, don't try to open a disabled menu
2266 if ( unique
&& IsEnabledTop((size_t)idxFound
) )
2272 // skip the "event.Skip()" below
2281 // ----------------------------------------------------------------------------
2282 // wxMenuBar accel handling
2283 // ----------------------------------------------------------------------------
2285 int wxMenuBar::FindNextItemForAccel(int idxStart
, int key
, bool *unique
) const
2287 if ( !wxIsalnum(key
) )
2289 // we only support letters/digits as accels
2293 // do we have more than one item with this accel?
2297 // translate everything to lower case before comparing
2298 wxChar chAccel
= wxTolower(key
);
2300 // the index of the item with this accel
2303 // loop through all items searching for the item with this
2304 // accel starting at the item after the current one
2305 int count
= GetCount();
2306 int n
= idxStart
== -1 ? 0 : idxStart
+ 1;
2317 const wxMenuInfo
& info
= m_menuInfos
[n
];
2319 int idxAccel
= info
.GetAccelIndex();
2320 if ( idxAccel
!= -1 &&
2321 wxTolower(info
.GetLabel()[(size_t)idxAccel
])
2324 // ok, found an item with this accel
2325 if ( idxFound
== -1 )
2327 // store it but continue searching as we need to
2328 // know if it's the only item with this accel or if
2332 else // we already had found such item
2337 // no need to continue further, we won't find
2338 // anything we don't already know
2343 // we want to iterate over all items wrapping around if
2351 if ( n
== idxStart
)
2353 // we've seen all items
2363 bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent
& event
)
2366 for ( wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
2368 node
= node
->GetNext(), n
++ )
2370 // accels of the items in the disabled menus shouldn't work
2371 if ( m_menuInfos
[n
].IsEnabled() )
2373 if ( node
->GetData()->ProcessAccelEvent(event
) )
2375 // menu processed it
2385 #endif // wxUSE_ACCEL
2387 // ----------------------------------------------------------------------------
2388 // wxMenuBar menus showing
2389 // ----------------------------------------------------------------------------
2391 void wxMenuBar::PopupCurrentMenu(bool selectFirst
)
2393 wxCHECK_RET( m_current
!= -1, _T("no menu to popup") );
2395 // forgot to call DismissMenu()?
2396 wxASSERT_MSG( !m_menuShown
, _T("shouldn't show two menus at once!") );
2398 // in any case, we should show it - even if we won't
2399 m_shouldShowMenu
= true;
2401 if ( IsEnabledTop(m_current
) )
2403 // remember the menu we show
2404 m_menuShown
= GetMenu(m_current
);
2406 // we don't show the menu at all if it has no items
2407 if ( !m_menuShown
->IsEmpty() )
2409 // position it correctly: note that we must use screen coords and
2410 // that we pass 0 as width to position the menu exactly below the
2411 // item, not to the right of it
2412 wxRect rectItem
= GetItemRect(m_current
);
2414 m_menuShown
->SetInvokingWindow(m_frameLast
);
2416 m_menuShown
->Popup(ClientToScreen(rectItem
.GetPosition()),
2417 wxSize(0, rectItem
.GetHeight()),
2422 // reset it back as no menu is shown
2426 //else: don't show disabled menu
2429 void wxMenuBar::DismissMenu()
2431 wxCHECK_RET( m_menuShown
, _T("can't dismiss menu if none is shown") );
2433 m_menuShown
->Dismiss();
2437 void wxMenuBar::OnDismissMenu(bool dismissMenuBar
)
2439 m_shouldShowMenu
= false;
2441 if ( dismissMenuBar
)
2447 void wxMenuBar::OnDismiss()
2451 wxLogTrace(_T("mousecapture"), _T("Releasing mouse from wxMenuBar::OnDismiss"));
2452 GetCapture()->ReleaseMouse();
2455 if ( m_current
!= -1 )
2457 size_t current
= m_current
;
2460 RefreshItem(current
);
2466 void wxMenuBar::GiveAwayFocus()
2468 GetFrame()->SetFocus();
2471 // ----------------------------------------------------------------------------
2472 // popup menu support
2473 // ----------------------------------------------------------------------------
2475 wxEventLoop
*wxWindow::ms_evtLoopPopup
= NULL
;
2477 bool wxWindow::DoPopupMenu(wxMenu
*menu
, int x
, int y
)
2479 wxCHECK_MSG( !ms_evtLoopPopup
, false,
2480 _T("can't show more than one popup menu at a time") );
2483 // we need to change the cursor before showing the menu as, apparently, no
2484 // cursor changes took place while the mouse is captured
2485 wxCursor cursorOld
= GetCursor();
2486 SetCursor(wxCURSOR_ARROW
);
2490 // flash any delayed log messages before showing the menu, otherwise it
2491 // could be dismissed (because it would lose focus) immediately after being
2493 wxLog::FlushActive();
2495 // some controls update themselves from OnIdle() call - let them do it
2496 wxTheApp
->ProcessIdle();
2498 // if the window hadn't been refreshed yet, the menu can adversely affect
2499 // its next OnPaint() handler execution - i.e. scrolled window refresh
2500 // logic breaks then as it scrolls part of the menu which hadn't been there
2501 // when the update event was generated into view
2505 menu
->SetInvokingWindow(this);
2507 // wxLogDebug( "Name of invoking window %s", menu->GetInvokingWindow()->GetName().c_str() );
2509 menu
->Popup(ClientToScreen(wxPoint(x
, y
)), wxSize(0, 0));
2511 // this is not very useful if the menu was popped up because of the mouse
2512 // click but I think it is nice to do when it appears because of a key
2513 // press (i.e. Windows menu key)
2515 // Windows itself doesn't do it, but IMHO this is nice
2518 // we have to redirect all keyboard input to the menu temporarily
2519 PushEventHandler(new wxMenuKbdRedirector(menu
));
2521 // enter the local modal loop
2522 ms_evtLoopPopup
= new wxEventLoop
;
2523 ms_evtLoopPopup
->Run();
2525 delete ms_evtLoopPopup
;
2526 ms_evtLoopPopup
= NULL
;
2528 // remove the handler
2529 PopEventHandler(true /* delete it */);
2531 menu
->SetInvokingWindow(NULL
);
2534 SetCursor(cursorOld
);
2540 void wxWindow::DismissPopupMenu()
2542 wxCHECK_RET( ms_evtLoopPopup
, _T("no popup menu shown") );
2544 ms_evtLoopPopup
->Exit();
2547 #endif // wxUSE_MENUS