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
);
188 // change the current item refreshing the old and new items
189 void ChangeCurrent(wxMenuItemList::compatibility_iterator node
);
191 // activate item, i.e. call either ClickItem() or OpenSubmenu() depending
192 // on what it is, return TRUE if something was done (i.e. it's not a
194 bool ActivateItem(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
196 // send the event about the item click
197 void ClickItem(wxMenuItem
*item
);
199 // show the submenu for this item
200 void OpenSubmenu(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
202 // can this tiem be opened?
203 bool CanOpen(wxMenuItem
*item
)
205 return item
&& item
->IsEnabled() && item
->IsSubMenu();
208 // dismiss the menu and all parent menus too
209 void DismissAndNotify();
211 // react to dimissing this menu and also dismiss the parent if
213 void HandleDismiss(bool dismissParent
);
215 // do we have an open submenu?
216 bool HasOpenSubmenu() const { return m_hasOpenSubMenu
; }
218 // get previous node after the current one
219 wxMenuItemList::compatibility_iterator
GetPrevNode() const;
221 // get previous node before the given one, wrapping if it's the first one
222 wxMenuItemList::compatibility_iterator
GetPrevNode(wxMenuItemList::compatibility_iterator node
) const;
224 // get next node after the current one
225 wxMenuItemList::compatibility_iterator
GetNextNode() const;
227 // get next node after the given one, wrapping if it's the last one
228 wxMenuItemList::compatibility_iterator
GetNextNode(wxMenuItemList::compatibility_iterator node
) const;
234 // the menu node corresponding to the current item
235 wxMenuItemList::compatibility_iterator m_nodeCurrent
;
237 // do we currently have an opened submenu?
238 bool m_hasOpenSubMenu
;
240 DECLARE_EVENT_TABLE()
243 // ----------------------------------------------------------------------------
244 // wxMenuKbdRedirector: an event handler which redirects kbd input to wxMenu
245 // ----------------------------------------------------------------------------
247 class wxMenuKbdRedirector
: public wxEvtHandler
250 wxMenuKbdRedirector(wxMenu
*menu
) { m_menu
= menu
; }
252 virtual bool ProcessEvent(wxEvent
& event
)
254 if ( event
.GetEventType() == wxEVT_KEY_DOWN
)
256 return m_menu
->ProcessKeyDown(((wxKeyEvent
&)event
).GetKeyCode());
262 return wxEvtHandler::ProcessEvent(event
);
270 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
274 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
275 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
276 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
278 BEGIN_EVENT_TABLE(wxPopupMenuWindow
, wxPopupTransientWindow
)
279 EVT_KEY_DOWN(wxPopupMenuWindow::OnKeyDown
)
281 EVT_LEFT_UP(wxPopupMenuWindow::OnLeftUp
)
282 EVT_MOTION(wxPopupMenuWindow::OnMouseMove
)
283 EVT_LEAVE_WINDOW(wxPopupMenuWindow::OnMouseLeave
)
286 BEGIN_EVENT_TABLE(wxMenuBar
, wxMenuBarBase
)
287 EVT_KILL_FOCUS(wxMenuBar::OnKillFocus
)
289 EVT_KEY_DOWN(wxMenuBar::OnKeyDown
)
291 EVT_LEFT_DOWN(wxMenuBar::OnLeftDown
)
292 EVT_MOTION(wxMenuBar::OnMouseMove
)
295 // ============================================================================
297 // ============================================================================
299 // ----------------------------------------------------------------------------
301 // ----------------------------------------------------------------------------
303 wxPopupMenuWindow::wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
)
306 m_hasOpenSubMenu
= FALSE
;
310 (void)Create(parent
, wxBORDER_RAISED
);
312 SetCursor(wxCURSOR_ARROW
);
315 wxPopupMenuWindow::~wxPopupMenuWindow()
317 // When m_popupMenu in wxMenu is deleted because it
318 // is a child of an old menu bar being deleted (note: it does
319 // not get destroyed by the wxMenu destructor, but
320 // by DestroyChildren()), m_popupMenu should be reset to NULL.
322 m_menu
->m_popupMenu
= NULL
;
325 // ----------------------------------------------------------------------------
326 // wxPopupMenuWindow current item/node handling
327 // ----------------------------------------------------------------------------
329 void wxPopupMenuWindow::ResetCurrent()
332 SetCurrent(wxMenuItemList::compatibility_iterator());
338 void wxPopupMenuWindow::SetCurrent(wxMenuItemList::compatibility_iterator node
)
340 m_nodeCurrent
= node
;
343 void wxPopupMenuWindow::ChangeCurrent(wxMenuItemList::compatibility_iterator node
)
345 if ( node
!= m_nodeCurrent
)
347 wxMenuItemList::compatibility_iterator nodeOldCurrent
= m_nodeCurrent
;
349 m_nodeCurrent
= node
;
351 if ( nodeOldCurrent
)
353 wxMenuItem
*item
= nodeOldCurrent
->GetData();
354 wxCHECK_RET( item
, _T("no current item?") );
356 // if it was the currently opened menu, close it
357 if ( item
->IsSubMenu() && item
->GetSubMenu()->IsShown() )
359 item
->GetSubMenu()->Dismiss();
367 RefreshItem(m_nodeCurrent
->GetData());
371 wxMenuItemList::compatibility_iterator
wxPopupMenuWindow::GetPrevNode() const
373 // return the last node if there had been no previously selected one
374 return m_nodeCurrent
? GetPrevNode(m_nodeCurrent
)
375 : m_menu
->GetMenuItems().GetLast();
378 wxMenuItemList::compatibility_iterator
379 wxPopupMenuWindow::GetPrevNode(wxMenuItemList::compatibility_iterator node
) const
383 node
= node
->GetPrevious();
386 node
= m_menu
->GetMenuItems().GetLast();
389 //else: the menu is empty
394 wxMenuItemList::compatibility_iterator
wxPopupMenuWindow::GetNextNode() const
396 // return the first node if there had been no previously selected one
397 return m_nodeCurrent
? GetNextNode(m_nodeCurrent
)
398 : m_menu
->GetMenuItems().GetFirst();
401 wxMenuItemList::compatibility_iterator
402 wxPopupMenuWindow::GetNextNode(wxMenuItemList::compatibility_iterator node
) const
406 node
= node
->GetNext();
409 node
= m_menu
->GetMenuItems().GetFirst();
412 //else: the menu is empty
417 // ----------------------------------------------------------------------------
418 // wxPopupMenuWindow popup/dismiss
419 // ----------------------------------------------------------------------------
421 void wxPopupMenuWindow::Popup(wxWindow
*focus
)
423 // check that the current item had been properly reset before
424 wxASSERT_MSG( !m_nodeCurrent
||
425 m_nodeCurrent
== m_menu
->GetMenuItems().GetFirst(),
426 _T("menu current item preselected incorrectly") );
428 wxPopupTransientWindow::Popup(focus
);
431 // ensure that this window is really on top of everything: without using
432 // SetWindowPos() it can be covered by its parent menu which is not
433 // really what we want
434 wxMenu
*menuParent
= m_menu
->GetParent();
437 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
439 // if we're shown, the parent menu must be also shown
440 wxCHECK_RET( win
, _T("parent menu is not shown?") );
442 if ( !::SetWindowPos(GetHwndOf(win
), GetHwnd(),
444 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOREDRAW
) )
446 wxLogLastError(_T("SetWindowPos(HWND_TOP)"));
454 void wxPopupMenuWindow::Dismiss()
456 if ( HasOpenSubmenu() )
458 wxMenuItem
*item
= GetCurrentItem();
459 wxCHECK_RET( item
&& item
->IsSubMenu(), _T("where is our open submenu?") );
461 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
462 wxCHECK_RET( win
, _T("opened submenu is not opened?") );
468 wxPopupTransientWindow::Dismiss();
471 void wxPopupMenuWindow::OnDismiss()
473 // when we are dismissed because the user clicked elsewhere or we lost
474 // focus in any other way, hide the parent menu as well
478 void wxPopupMenuWindow::HandleDismiss(bool dismissParent
)
482 m_menu
->OnDismiss(dismissParent
);
485 void wxPopupMenuWindow::DismissAndNotify()
491 // ----------------------------------------------------------------------------
492 // wxPopupMenuWindow geometry
493 // ----------------------------------------------------------------------------
495 wxMenuItemList::compatibility_iterator
496 wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint
& pt
) const
498 // we only use the y coord normally, but still check x in case the point is
499 // outside the window completely
500 if ( wxWindow::HitTest(pt
) == wxHT_WINDOW_INSIDE
)
503 for ( wxMenuItemList::compatibility_iterator node
= m_menu
->GetMenuItems().GetFirst();
505 node
= node
->GetNext() )
507 wxMenuItem
*item
= node
->GetData();
508 y
+= item
->GetHeight();
518 return wxMenuItemList::compatibility_iterator();
524 // ----------------------------------------------------------------------------
525 // wxPopupMenuWindow drawing
526 // ----------------------------------------------------------------------------
528 void wxPopupMenuWindow::RefreshItem(wxMenuItem
*item
)
530 wxCHECK_RET( item
, _T("can't refresh NULL item") );
532 wxASSERT_MSG( IsShown(), _T("can't refresh menu which is not shown") );
534 // FIXME: -1 here because of SetLogicalOrigin(1, 1) in DoDraw()
535 RefreshRect(wxRect(0, item
->GetPosition() - 1,
536 m_menu
->GetGeometryInfo().GetSize().x
, item
->GetHeight()));
539 void wxPopupMenuWindow::DoDraw(wxControlRenderer
*renderer
)
541 // no clipping so far - do we need it? I don't think so as the menu is
542 // never partially covered as it is always on top of everything
544 wxDC
& dc
= renderer
->GetDC();
545 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
547 // FIXME: this should be done in the renderer, however when it is fixed
548 // wxPopupMenuWindow::RefreshItem() should be changed too!
549 dc
.SetLogicalOrigin(1, 1);
551 wxRenderer
*rend
= renderer
->GetRenderer();
554 const wxMenuGeometryInfo
& gi
= m_menu
->GetGeometryInfo();
555 for ( wxMenuItemList::compatibility_iterator node
= m_menu
->GetMenuItems().GetFirst();
557 node
= node
->GetNext() )
559 wxMenuItem
*item
= node
->GetData();
561 if ( item
->IsSeparator() )
563 rend
->DrawMenuSeparator(dc
, y
, gi
);
565 else // not a separator
568 if ( item
->IsCheckable() )
570 flags
|= wxCONTROL_CHECKABLE
;
572 if ( item
->IsChecked() )
574 flags
|= wxCONTROL_CHECKED
;
578 if ( !item
->IsEnabled() )
579 flags
|= wxCONTROL_DISABLED
;
581 if ( item
->IsSubMenu() )
582 flags
|= wxCONTROL_ISSUBMENU
;
584 if ( item
== GetCurrentItem() )
585 flags
|= wxCONTROL_SELECTED
;
593 item
->GetAccelString(),
594 // strangely enough, for unchecked item we use the
595 // "checked" bitmap because this is the default one - this
596 // explains this strange boolean expression
597 item
->GetBitmap(!item
->IsCheckable() || item
->IsChecked()),
599 item
->GetAccelIndex()
603 y
+= item
->GetHeight();
607 // ----------------------------------------------------------------------------
608 // wxPopupMenuWindow actions
609 // ----------------------------------------------------------------------------
611 void wxPopupMenuWindow::ClickItem(wxMenuItem
*item
)
613 wxCHECK_RET( item
, _T("can't click NULL item") );
615 wxASSERT_MSG( !item
->IsSeparator() && !item
->IsSubMenu(),
616 _T("can't click this item") );
618 wxMenu
* menu
= m_menu
;
623 menu
->ClickItem(item
);
626 void wxPopupMenuWindow::OpenSubmenu(wxMenuItem
*item
, InputMethod how
)
628 wxCHECK_RET( item
, _T("can't open NULL submenu") );
630 wxMenu
*submenu
= item
->GetSubMenu();
631 wxCHECK_RET( submenu
, _T("can only open submenus!") );
633 // FIXME: should take into account the border width
634 submenu
->Popup(ClientToScreen(wxPoint(0, item
->GetPosition())),
635 wxSize(m_menu
->GetGeometryInfo().GetSize().x
, 0),
636 how
== WithKeyboard
/* preselect first item then */);
638 m_hasOpenSubMenu
= TRUE
;
641 bool wxPopupMenuWindow::ActivateItem(wxMenuItem
*item
, InputMethod how
)
643 // don't activate disabled items
644 if ( !item
|| !item
->IsEnabled() )
649 // normal menu items generate commands, submenus can be opened and
650 // the separators don't do anything
651 if ( item
->IsSubMenu() )
653 OpenSubmenu(item
, how
);
655 else if ( !item
->IsSeparator() )
659 else // separator, can't activate
667 // ----------------------------------------------------------------------------
668 // wxPopupMenuWindow input handling
669 // ----------------------------------------------------------------------------
671 bool wxPopupMenuWindow::ProcessLeftDown(wxMouseEvent
& event
)
673 // wxPopupWindowHandler dismisses the window when the mouse is clicked
674 // outside it which is usually just fine, but there is one case when we
675 // don't want to do it: if the mouse was clicked on the parent submenu item
676 // which opens this menu, so check for it
678 wxPoint pos
= event
.GetPosition();
679 if ( HitTest(pos
.x
, pos
.y
) == wxHT_WINDOW_OUTSIDE
)
681 wxMenu
*menu
= m_menu
->GetParent();
684 wxPopupMenuWindow
*win
= menu
->m_popupMenu
;
686 wxCHECK_MSG( win
, FALSE
, _T("parent menu not shown?") );
688 pos
= ClientToScreen(pos
);
689 if ( win
->GetMenuItemFromPoint(win
->ScreenToClient(pos
)) )
694 //else: it is outside the parent menu as well, do dismiss this one
701 void wxPopupMenuWindow::OnLeftUp(wxMouseEvent
& event
)
703 wxMenuItemList::compatibility_iterator node
= GetMenuItemFromPoint(event
.GetPosition());
706 ActivateItem(node
->GetData(), WithMouse
);
710 void wxPopupMenuWindow::OnMouseMove(wxMouseEvent
& event
)
712 const wxPoint pt
= event
.GetPosition();
714 // we need to ignore extra mouse events: example when this happens is when
715 // the mouse is on the menu and we open a submenu from keyboard - Windows
716 // then sends us a dummy mouse move event, we (correctly) determine that it
717 // happens in the parent menu and so immediately close the just opened
720 static wxPoint s_ptLast
;
721 wxPoint ptCur
= ClientToScreen(pt
);
722 if ( ptCur
== s_ptLast
)
730 ProcessMouseMove(pt
);
735 void wxPopupMenuWindow::ProcessMouseMove(const wxPoint
& pt
)
737 wxMenuItemList::compatibility_iterator node
= GetMenuItemFromPoint(pt
);
739 // don't reset current to NULL here, we only do it when the mouse leaves
740 // the window (see below)
743 if ( node
!= m_nodeCurrent
)
747 wxMenuItem
*item
= GetCurrentItem();
750 OpenSubmenu(item
, WithMouse
);
753 //else: same item, nothing to do
755 else // not on an item
757 // the last open submenu forwards the mouse move messages to its
758 // parent, so if the mouse moves to another item of the parent menu,
759 // this menu is closed and this other item is selected - in the similar
760 // manner, the top menu forwards the mouse moves to the menubar which
761 // allows to select another top level menu by just moving the mouse
763 // we need to translate our client coords to the client coords of the
764 // window we forward this event to
765 wxPoint ptScreen
= ClientToScreen(pt
);
767 // if the mouse is outside this menu, let the parent one to
769 wxMenu
*menuParent
= m_menu
->GetParent();
772 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
774 // if we're shown, the parent menu must be also shown
775 wxCHECK_RET( win
, _T("parent menu is not shown?") );
777 win
->ProcessMouseMove(win
->ScreenToClient(ptScreen
));
779 else // no parent menu
781 wxMenuBar
*menubar
= m_menu
->GetMenuBar();
784 if ( menubar
->ProcessMouseEvent(
785 menubar
->ScreenToClient(ptScreen
)) )
787 // menubar has closed this menu and opened another one, probably
792 //else: top level popup menu, no other processing to do
796 void wxPopupMenuWindow::OnMouseLeave(wxMouseEvent
& event
)
798 // due to the artefact of mouse events generation under MSW, we actually
799 // may get the mouse leave event after the menu had been already dismissed
800 // and calling ChangeCurrent() would then assert, so don't do it
803 // we shouldn't change the current them if our submenu is opened and
804 // mouse moved there, in this case the submenu is responsable for
807 if ( HasOpenSubmenu() )
809 wxMenuItem
*item
= GetCurrentItem();
810 wxCHECK_RET( CanOpen(item
), _T("where is our open submenu?") );
812 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
813 wxCHECK_RET( win
, _T("submenu is opened but not shown?") );
815 // only handle this event if the mouse is not inside the submenu
816 wxPoint pt
= ClientToScreen(event
.GetPosition());
818 win
->HitTest(win
->ScreenToClient(pt
)) == wxHT_WINDOW_OUTSIDE
;
822 // this menu is the last opened
829 ChangeCurrent(wxMenuItemList::compatibility_iterator());
839 void wxPopupMenuWindow::OnKeyDown(wxKeyEvent
& event
)
841 if ( !ProcessKeyDown(event
.GetKeyCode()) )
847 bool wxPopupMenuWindow::ProcessKeyDown(int key
)
849 wxMenuItem
*item
= GetCurrentItem();
851 // first let the opened submenu to have it (no test for IsEnabled() here,
852 // the keys navigate even in a disabled submenu if we had somehow managed
853 // to open it inspit of this)
854 if ( HasOpenSubmenu() )
856 wxCHECK_MSG( CanOpen(item
), FALSE
,
857 _T("has open submenu but another item selected?") );
859 if ( item
->GetSubMenu()->ProcessKeyDown(key
) )
863 bool processed
= TRUE
;
865 // handle the up/down arrows, home, end, esc and return here, pass the
866 // left/right arrows to the menu bar except when the right arrow can be
867 // used to open a submenu
871 // if we're not a top level menu, close us, else leave this to the
873 if ( !m_menu
->GetParent() )
882 // close just this menu
884 HandleDismiss(FALSE
);
888 processed
= ActivateItem(item
);
892 ChangeCurrent(m_menu
->GetMenuItems().GetFirst());
896 ChangeCurrent(m_menu
->GetMenuItems().GetLast());
902 bool up
= key
== WXK_UP
;
904 wxMenuItemList::compatibility_iterator nodeStart
= up
? GetPrevNode()
907 while ( node
&& node
->GetData()->IsSeparator() )
909 node
= up
? GetPrevNode(node
) : GetNextNode(node
);
911 if ( node
== nodeStart
)
913 // nothing but separators and disabled items in this
916 node
= wxMenuItemList::compatibility_iterator();
935 // don't try to reopen an already opened menu
936 if ( !HasOpenSubmenu() && CanOpen(item
) )
947 // look for the menu item starting with this letter
948 if ( wxIsalnum(key
) )
950 // we want to start from the item after this one because
951 // if we're already on the item with the given accel we want to
952 // go to the next one, not to stay in place
953 wxMenuItemList::compatibility_iterator nodeStart
= GetNextNode();
955 // do we have more than one item with this accel?
956 bool notUnique
= FALSE
;
958 // translate everything to lower case before comparing
959 wxChar chAccel
= wxTolower(key
);
961 // loop through all items searching for the item with this
963 wxMenuItemList::compatibility_iterator node
= nodeStart
,
965 nodeFound
= wxMenuItemList::compatibility_iterator();
971 item
= node
->GetData();
973 int idxAccel
= item
->GetAccelIndex();
974 if ( idxAccel
!= -1 &&
975 wxTolower(item
->GetLabel()[(size_t)idxAccel
])
978 // ok, found an item with this accel
981 // store it but continue searching as we need to
982 // know if it's the only item with this accel or if
986 else // we already had found such item
990 // no need to continue further, we won't find
991 // anything we don't already know
996 // we want to iterate over all items wrapping around if
998 node
= GetNextNode(node
);
999 if ( node
== nodeStart
)
1001 // we've seen all nodes
1008 item
= nodeFound
->GetData();
1010 // go to this item anyhow
1011 ChangeCurrent(nodeFound
);
1013 if ( !notUnique
&& item
->IsEnabled() )
1015 // unique item with this accel - activate it
1016 processed
= ActivateItem(item
);
1018 //else: just select it but don't activate as the user might
1019 // have wanted to activate another item
1021 // skip "processed = FALSE" below
1032 // ----------------------------------------------------------------------------
1034 // ----------------------------------------------------------------------------
1042 m_startRadioGroup
= -1;
1051 // ----------------------------------------------------------------------------
1052 // wxMenu and wxMenuGeometryInfo
1053 // ----------------------------------------------------------------------------
1055 wxMenuGeometryInfo::~wxMenuGeometryInfo()
1059 const wxMenuGeometryInfo
& wxMenu::GetGeometryInfo() const
1065 wxConstCast(this, wxMenu
)->m_geometry
=
1066 m_popupMenu
->GetRenderer()->GetMenuGeometry(m_popupMenu
, *this);
1070 wxFAIL_MSG( _T("can't get geometry without window") );
1077 void wxMenu::InvalidateGeometryInfo()
1086 // ----------------------------------------------------------------------------
1087 // wxMenu adding/removing items
1088 // ----------------------------------------------------------------------------
1090 void wxMenu::OnItemAdded(wxMenuItem
*item
)
1092 InvalidateGeometryInfo();
1096 #endif // wxUSE_ACCEL
1098 // the submenus of a popup menu should have the same invoking window as it
1100 if ( m_invokingWindow
&& item
->IsSubMenu() )
1102 item
->GetSubMenu()->SetInvokingWindow(m_invokingWindow
);
1106 void wxMenu::EndRadioGroup()
1108 // we're not inside a radio group any longer
1109 m_startRadioGroup
= -1;
1112 bool wxMenu::DoAppend(wxMenuItem
*item
)
1116 if ( item
->GetKind() == wxITEM_RADIO
)
1118 int count
= GetMenuItemCount();
1120 if ( m_startRadioGroup
== -1 )
1122 // start a new radio group
1123 m_startRadioGroup
= count
;
1125 // for now it has just one element
1126 item
->SetAsRadioGroupStart();
1127 item
->SetRadioGroupEnd(m_startRadioGroup
);
1129 // ensure that we have a checked item in the radio group
1132 else // extend the current radio group
1134 // we need to update its end item
1135 item
->SetRadioGroupStart(m_startRadioGroup
);
1136 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
1140 node
->GetData()->SetRadioGroupEnd(count
);
1144 wxFAIL_MSG( _T("where is the radio group start item?") );
1148 else // not a radio item
1153 if ( !wxMenuBase::DoAppend(item
) )
1161 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1163 if ( !wxMenuBase::DoInsert(pos
, item
) )
1171 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1173 wxMenuItem
*itemOld
= wxMenuBase::DoRemove(item
);
1177 InvalidateGeometryInfo();
1180 RemoveAccelFor(item
);
1181 #endif // wxUSE_ACCEL
1187 // ----------------------------------------------------------------------------
1188 // wxMenu attaching/detaching
1189 // ----------------------------------------------------------------------------
1191 void wxMenu::Attach(wxMenuBarBase
*menubar
)
1193 wxMenuBase::Attach(menubar
);
1195 wxCHECK_RET( m_menuBar
, _T("menubar can't be NULL after attaching") );
1197 // unfortunately, we can't use m_menuBar->GetEventHandler() here because,
1198 // if the menubar is currently showing a menu, its event handler is a
1199 // temporary one installed by wxPopupWindow and so will disappear soon any
1200 // any attempts to use it from the newly attached menu would result in a
1203 // so we use the menubar itself, even if it's a pity as it means we can't
1204 // redirect all menu events by changing the menubar handler (FIXME)
1205 SetNextHandler(m_menuBar
);
1208 void wxMenu::Detach()
1210 wxMenuBase::Detach();
1213 // ----------------------------------------------------------------------------
1214 // wxMenu misc functions
1215 // ----------------------------------------------------------------------------
1217 wxWindow
*wxMenu::GetRootWindow() const
1221 // simple case - a normal menu attached to the menubar
1225 // we're a popup menu but the trouble is that only the top level popup menu
1226 // has a pointer to the invoking window, so we must walk up the menu chain
1228 wxWindow
*win
= GetInvokingWindow();
1231 // we already have it
1235 wxMenu
*menu
= GetParent();
1238 // We are a submenu of a menu of a menubar
1239 if (menu
->GetMenuBar())
1240 return menu
->GetMenuBar();
1242 win
= menu
->GetInvokingWindow();
1246 menu
= menu
->GetParent();
1249 // we're probably going to crash in the caller anyhow, but try to detect
1250 // this error as soon as possible
1251 wxASSERT_MSG( win
, _T("menu without any associated window?") );
1253 // also remember it in this menu so that we don't have to search for it the
1255 wxConstCast(this, wxMenu
)->m_invokingWindow
= win
;
1260 wxRenderer
*wxMenu::GetRenderer() const
1262 // we're going to crash without renderer!
1263 wxCHECK_MSG( m_popupMenu
, NULL
, _T("neither popup nor menubar menu?") );
1265 return m_popupMenu
->GetRenderer();
1268 void wxMenu::RefreshItem(wxMenuItem
*item
)
1270 // the item geometry changed, so our might have changed as well
1271 InvalidateGeometryInfo();
1275 // this would be a bug in IsShown()
1276 wxCHECK_RET( m_popupMenu
, _T("must have popup window if shown!") );
1278 // recalc geometry to update the item height and such
1279 (void)GetGeometryInfo();
1281 m_popupMenu
->RefreshItem(item
);
1285 // ----------------------------------------------------------------------------
1286 // wxMenu showing and hiding
1287 // ----------------------------------------------------------------------------
1289 bool wxMenu::IsShown() const
1291 return m_popupMenu
&& m_popupMenu
->IsShown();
1294 void wxMenu::OnDismiss(bool dismissParent
)
1298 // always notify the parent about submenu disappearance
1299 wxPopupMenuWindow
*win
= m_menuParent
->m_popupMenu
;
1302 win
->OnSubmenuDismiss();
1306 wxFAIL_MSG( _T("parent menu not shown?") );
1309 // and if we dismiss everything, propagate to parent
1310 if ( dismissParent
)
1312 // dismissParent is recursive
1313 m_menuParent
->Dismiss();
1314 m_menuParent
->OnDismiss(TRUE
);
1317 else // no parent menu
1319 // notify the menu bar if we're a top level menu
1322 m_menuBar
->OnDismissMenu(dismissParent
);
1326 wxCHECK_RET( m_invokingWindow
, _T("what kind of menu is this?") );
1328 m_invokingWindow
->DismissPopupMenu();
1330 // Why reset it here? We need it for sending the event to...
1331 // SetInvokingWindow(NULL);
1336 void wxMenu::Popup(const wxPoint
& pos
, const wxSize
& size
, bool selectFirst
)
1338 // create the popup window if not done yet
1341 m_popupMenu
= new wxPopupMenuWindow(GetRootWindow(), this);
1344 // select the first item unless disabled
1347 m_popupMenu
->SelectFirst();
1350 // the geometry might have changed since the last time we were shown, so
1352 m_popupMenu
->SetClientSize(GetGeometryInfo().GetSize());
1354 // position it as specified
1355 m_popupMenu
->Position(pos
, size
);
1357 // the menu can't have the focus itself (it is a Windows limitation), so
1358 // always keep the focus at the originating window
1359 wxWindow
*focus
= GetRootWindow();
1361 wxASSERT_MSG( focus
, _T("no window to keep focus on?") );
1364 m_popupMenu
->Popup(focus
);
1367 void wxMenu::Dismiss()
1369 wxCHECK_RET( IsShown(), _T("can't dismiss hidden menu") );
1371 m_popupMenu
->Dismiss();
1374 // ----------------------------------------------------------------------------
1375 // wxMenu event processing
1376 // ----------------------------------------------------------------------------
1378 bool wxMenu::ProcessKeyDown(int key
)
1380 wxCHECK_MSG( m_popupMenu
, FALSE
,
1381 _T("can't process key events if not shown") );
1383 return m_popupMenu
->ProcessKeyDown(key
);
1386 bool wxMenu::ClickItem(wxMenuItem
*item
)
1389 if ( item
->IsCheckable() )
1391 // update the item state
1392 isChecked
= !item
->IsChecked();
1394 item
->Check(isChecked
!= 0);
1402 return SendEvent(item
->GetId(), isChecked
);
1405 // ----------------------------------------------------------------------------
1406 // wxMenu accel support
1407 // ----------------------------------------------------------------------------
1411 bool wxMenu::ProcessAccelEvent(const wxKeyEvent
& event
)
1413 // do we have an item for this accel?
1414 wxMenuItem
*item
= m_accelTable
.GetMenuItem(event
);
1415 if ( item
&& item
->IsEnabled() )
1417 return ClickItem(item
);
1421 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
1423 node
= node
->GetNext() )
1425 const wxMenuItem
*item
= node
->GetData();
1426 if ( item
->IsSubMenu() && item
->IsEnabled() )
1429 if ( item
->GetSubMenu()->ProcessAccelEvent(event
) )
1439 void wxMenu::AddAccelFor(wxMenuItem
*item
)
1441 wxAcceleratorEntry
*accel
= item
->GetAccel();
1444 accel
->SetMenuItem(item
);
1446 m_accelTable
.Add(*accel
);
1452 void wxMenu::RemoveAccelFor(wxMenuItem
*item
)
1454 wxAcceleratorEntry
*accel
= item
->GetAccel();
1457 m_accelTable
.Remove(*accel
);
1463 #endif // wxUSE_ACCEL
1465 // ----------------------------------------------------------------------------
1466 // wxMenuItem construction
1467 // ----------------------------------------------------------------------------
1469 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
1471 const wxString
& text
,
1472 const wxString
& help
,
1475 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
1480 m_radioGroup
.start
= -1;
1481 m_isRadioGroupStart
= FALSE
;
1486 wxMenuItem::~wxMenuItem()
1490 // ----------------------------------------------------------------------------
1491 // wxMenuItemBase methods implemented here
1492 // ----------------------------------------------------------------------------
1495 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
1497 const wxString
& name
,
1498 const wxString
& help
,
1502 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
1506 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
1508 return wxStripMenuCodes(text
);
1511 // ----------------------------------------------------------------------------
1512 // wxMenuItem operations
1513 // ----------------------------------------------------------------------------
1515 void wxMenuItem::NotifyMenu()
1517 m_parentMenu
->RefreshItem(this);
1520 void wxMenuItem::UpdateAccelInfo()
1522 m_indexAccel
= wxControl::FindAccelIndex(m_text
);
1524 // will be empty if the text contains no TABs - ok
1525 m_strAccel
= m_text
.AfterFirst(_T('\t'));
1528 void wxMenuItem::SetText(const wxString
& text
)
1530 if ( text
!= m_text
)
1532 // first call the base class version to change m_text
1533 wxMenuItemBase::SetText(text
);
1541 void wxMenuItem::SetCheckable(bool checkable
)
1543 if ( checkable
!= IsCheckable() )
1545 wxMenuItemBase::SetCheckable(checkable
);
1551 void wxMenuItem::SetBitmaps(const wxBitmap
& bmpChecked
,
1552 const wxBitmap
& bmpUnchecked
)
1554 m_bmpChecked
= bmpChecked
;
1555 m_bmpUnchecked
= bmpUnchecked
;
1560 void wxMenuItem::Enable(bool enable
)
1562 if ( enable
!= m_isEnabled
)
1564 wxMenuItemBase::Enable(enable
);
1570 void wxMenuItem::Check(bool check
)
1572 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
1574 if ( m_isChecked
== check
)
1577 if ( GetKind() == wxITEM_RADIO
)
1579 // it doesn't make sense to uncheck a radio item - what would this do?
1583 // get the index of this item in the menu
1584 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
1585 int pos
= items
.IndexOf(this);
1586 wxCHECK_RET( pos
!= wxNOT_FOUND
,
1587 _T("menuitem not found in the menu items list?") );
1589 // get the radio group range
1593 if ( m_isRadioGroupStart
)
1595 // we already have all information we need
1597 end
= m_radioGroup
.end
;
1599 else // next radio group item
1601 // get the radio group end from the start item
1602 start
= m_radioGroup
.start
;
1603 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
1606 // also uncheck all the other items in this radio group
1607 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
1608 for ( int n
= start
; n
<= end
&& node
; n
++ )
1612 node
->GetData()->m_isChecked
= FALSE
;
1614 node
= node
->GetNext();
1618 wxMenuItemBase::Check(check
);
1623 // radio group stuff
1624 // -----------------
1626 void wxMenuItem::SetAsRadioGroupStart()
1628 m_isRadioGroupStart
= TRUE
;
1631 void wxMenuItem::SetRadioGroupStart(int start
)
1633 wxASSERT_MSG( !m_isRadioGroupStart
,
1634 _T("should only be called for the next radio items") );
1636 m_radioGroup
.start
= start
;
1639 void wxMenuItem::SetRadioGroupEnd(int end
)
1641 wxASSERT_MSG( m_isRadioGroupStart
,
1642 _T("should only be called for the first radio item") );
1644 m_radioGroup
.end
= end
;
1647 // ----------------------------------------------------------------------------
1648 // wxMenuBar creation
1649 // ----------------------------------------------------------------------------
1651 void wxMenuBar::Init()
1659 m_shouldShowMenu
= FALSE
;
1661 m_windowStyle
|= wxNO_FULL_REPAINT_ON_RESIZE
;
1664 void wxMenuBar::Attach(wxFrame
*frame
)
1666 // maybe you really wanted to call Detach()?
1667 wxCHECK_RET( frame
, _T("wxMenuBar::Attach(NULL) called") );
1669 wxMenuBarBase::Attach(frame
);
1673 // reparent if necessary
1674 if ( m_frameLast
!= frame
)
1679 // show it back - was hidden by Detach()
1682 else // not created yet, do it now
1684 // we have no way to return the error from here anyhow :-(
1685 (void)Create(frame
, -1);
1687 SetCursor(wxCURSOR_ARROW
);
1689 SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT
));
1691 // calculate and set our height (it won't be changed any more)
1692 SetSize(-1, GetBestSize().y
);
1695 // remember the last frame which had us to avoid unnecessarily reparenting
1697 m_frameLast
= frame
;
1700 void wxMenuBar::Detach()
1702 // don't delete the window because we may be reattached later, just hide it
1708 wxMenuBarBase::Detach();
1711 wxMenuBar::~wxMenuBar()
1715 // ----------------------------------------------------------------------------
1716 // wxMenuBar adding/removing items
1717 // ----------------------------------------------------------------------------
1719 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1721 return Insert(GetCount(), menu
, title
);
1724 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1726 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1729 wxMenuInfo
*info
= new wxMenuInfo(title
);
1730 m_menuInfos
.Insert(info
, pos
);
1732 RefreshAllItemsAfter(pos
);
1737 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1739 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1743 wxMenuInfo
& info
= m_menuInfos
[pos
];
1745 info
.SetLabel(title
);
1747 // even if the old menu was disabled, the new one is not any more
1750 // even if we change only this one, the new label has different width,
1751 // so we need to refresh everything beyond this item as well
1752 RefreshAllItemsAfter(pos
);
1758 wxMenu
*wxMenuBar::Remove(size_t pos
)
1760 wxMenu
*menuOld
= wxMenuBarBase::Remove(pos
);
1764 m_menuInfos
.RemoveAt(pos
);
1766 // this doesn't happen too often, so don't try to be too smart - just
1767 // refresh everything
1774 // ----------------------------------------------------------------------------
1775 // wxMenuBar top level menus access
1776 // ----------------------------------------------------------------------------
1778 wxCoord
wxMenuBar::GetItemWidth(size_t pos
) const
1780 return m_menuInfos
[pos
].GetWidth(wxConstCast(this, wxMenuBar
));
1783 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1785 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1787 if ( enable
!= m_menuInfos
[pos
].IsEnabled() )
1789 m_menuInfos
[pos
].SetEnabled(enable
);
1793 //else: nothing to do
1796 bool wxMenuBar::IsEnabledTop(size_t pos
) const
1798 wxCHECK_MSG( pos
< GetCount(), FALSE
, _T("invalid index in IsEnabledTop") );
1800 return m_menuInfos
[pos
].IsEnabled();
1803 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
1805 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1807 if ( label
!= m_menuInfos
[pos
].GetLabel() )
1809 m_menuInfos
[pos
].SetLabel(label
);
1813 //else: nothing to do
1816 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
1818 wxCHECK_MSG( pos
< GetCount(), _T(""), _T("invalid index in GetLabelTop") );
1820 return m_menuInfos
[pos
].GetLabel();
1823 // ----------------------------------------------------------------------------
1824 // wxMenuBar drawing
1825 // ----------------------------------------------------------------------------
1827 void wxMenuBar::RefreshAllItemsAfter(size_t pos
)
1831 // no need to refresh if nothing is shown yet
1835 wxRect rect
= GetItemRect(pos
);
1836 rect
.width
= GetClientSize().x
- rect
.x
;
1840 void wxMenuBar::RefreshItem(size_t pos
)
1842 wxCHECK_RET( pos
!= (size_t)-1,
1843 _T("invalid item in wxMenuBar::RefreshItem") );
1847 // no need to refresh if nothing is shown yet
1851 RefreshRect(GetItemRect(pos
));
1854 void wxMenuBar::DoDraw(wxControlRenderer
*renderer
)
1856 wxDC
& dc
= renderer
->GetDC();
1857 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1859 // redraw only the items which must be redrawn
1861 // we don't have to use GetUpdateClientRect() here because our client rect
1862 // is the same as total one
1863 wxRect rectUpdate
= GetUpdateRegion().GetBox();
1865 int flagsMenubar
= GetStateFlags();
1869 rect
.height
= GetClientSize().y
;
1872 size_t count
= GetCount();
1873 for ( size_t n
= 0; n
< count
; n
++ )
1875 if ( x
> rectUpdate
.GetRight() )
1877 // all remaining items are to the right of rectUpdate
1882 rect
.width
= GetItemWidth(n
);
1884 if ( x
< rectUpdate
.x
)
1886 // this item is still to the left of rectUpdate
1890 int flags
= flagsMenubar
;
1891 if ( m_current
!= -1 && n
== (size_t)m_current
)
1893 flags
|= wxCONTROL_SELECTED
;
1896 if ( !IsEnabledTop(n
) )
1898 flags
|= wxCONTROL_DISABLED
;
1901 GetRenderer()->DrawMenuBarItem
1905 m_menuInfos
[n
].GetLabel(),
1907 m_menuInfos
[n
].GetAccelIndex()
1912 // ----------------------------------------------------------------------------
1913 // wxMenuBar geometry
1914 // ----------------------------------------------------------------------------
1916 wxRect
wxMenuBar::GetItemRect(size_t pos
) const
1918 wxASSERT_MSG( pos
< GetCount(), _T("invalid menu bar item index") );
1919 wxASSERT_MSG( IsCreated(), _T("can't call this method yet") );
1924 rect
.height
= GetClientSize().y
;
1926 for ( size_t n
= 0; n
< pos
; n
++ )
1928 rect
.x
+= GetItemWidth(n
);
1931 rect
.width
= GetItemWidth(pos
);
1936 wxSize
wxMenuBar::DoGetBestClientSize() const
1939 if ( GetMenuCount() > 0 )
1941 wxClientDC
dc(wxConstCast(this, wxMenuBar
));
1942 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1943 dc
.GetTextExtent(GetLabelTop(0), &size
.x
, &size
.y
);
1945 // adjust for the renderer we use
1946 size
= GetRenderer()->GetMenuBarItemSize(size
);
1948 else // empty menubar
1954 // the width is arbitrary, of course, for horizontal menubar
1960 int wxMenuBar::GetMenuFromPoint(const wxPoint
& pos
) const
1962 if ( pos
.x
< 0 || pos
.y
< 0 || pos
.y
> GetClientSize().y
)
1967 size_t count
= GetCount();
1968 for ( size_t item
= 0; item
< count
; item
++ )
1970 x
+= GetItemWidth(item
);
1978 // to the right of the last menu item
1982 // ----------------------------------------------------------------------------
1983 // wxMenuBar menu operations
1984 // ----------------------------------------------------------------------------
1986 void wxMenuBar::SelectMenu(size_t pos
)
1989 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::SelectMenu"));
1995 void wxMenuBar::DoSelectMenu(size_t pos
)
1997 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in DoSelectMenu") );
1999 int posOld
= m_current
;
2005 // close the previous menu
2006 if ( IsShowingMenu() )
2008 // restore m_shouldShowMenu flag after DismissMenu() which resets
2010 bool old
= m_shouldShowMenu
;
2014 m_shouldShowMenu
= old
;
2017 RefreshItem((size_t)posOld
);
2023 void wxMenuBar::PopupMenu(size_t pos
)
2025 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in PopupCurrentMenu") );
2032 // ----------------------------------------------------------------------------
2033 // wxMenuBar input handing
2034 // ----------------------------------------------------------------------------
2037 Note that wxMenuBar doesn't use wxInputHandler but handles keyboard and
2038 mouse in the same way under all platforms. This is because it doesn't derive
2039 from wxControl (which works with input handlers) but directly from wxWindow.
2041 Also, menu bar input handling is rather simple, so maybe it's not really
2042 worth making it themeable - at least I've decided against doing it now as it
2043 would merging the changes back into trunk more difficult. But it still could
2044 be done later if really needed.
2047 void wxMenuBar::OnKillFocus(wxFocusEvent
& event
)
2049 if ( m_current
!= -1 )
2051 RefreshItem((size_t)m_current
);
2059 void wxMenuBar::OnLeftDown(wxMouseEvent
& event
)
2067 else // we didn't have mouse capture, capture it now
2069 m_current
= GetMenuFromPoint(event
.GetPosition());
2070 if ( m_current
== -1 )
2072 // unfortunately, we can't prevent wxMSW from giving us the focus,
2073 // so we can only give it back
2078 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::OnLeftDown"));
2081 // show it as selected
2082 RefreshItem((size_t)m_current
);
2085 PopupCurrentMenu(FALSE
/* don't select first item - as Windows does */);
2090 void wxMenuBar::OnMouseMove(wxMouseEvent
& event
)
2094 (void)ProcessMouseEvent(event
.GetPosition());
2102 bool wxMenuBar::ProcessMouseEvent(const wxPoint
& pt
)
2104 // a hack to ignore the extra mouse events MSW sends us: this is similar to
2105 // wxUSE_MOUSEEVENT_HACK in wxWin itself but it isn't enough for us here as
2106 // we get the messages from different windows (old and new popup menus for
2109 static wxPoint s_ptLast
;
2110 if ( pt
== s_ptLast
)
2118 int currentNew
= GetMenuFromPoint(pt
);
2119 if ( (currentNew
== -1) || (currentNew
== m_current
) )
2124 // select the new active item
2125 DoSelectMenu(currentNew
);
2127 // show the menu if we know that we should, even if we hadn't been showing
2128 // it before (this may happen if the previous menu was disabled)
2129 if ( m_shouldShowMenu
&& !m_menuShown
)
2131 // open the new menu if the old one we closed had been opened
2132 PopupCurrentMenu(FALSE
/* don't select first item - as Windows does */);
2138 void wxMenuBar::OnKeyDown(wxKeyEvent
& event
)
2140 // ensure that we have a current item - we might not have it if we're
2141 // given the focus with Alt or F10 press (and under GTK+ the menubar
2142 // somehow gets the keyboard events even when it doesn't have focus...)
2143 if ( m_current
== -1 )
2145 if ( !HasCapture() )
2149 else // we do have capture
2151 // we always maintain a valid current item while we're in modal
2152 // state (i.e. have the capture)
2153 wxFAIL_MSG( _T("how did we manage to lose current item?") );
2159 int key
= event
.GetKeyCode();
2161 // first let the menu have it
2162 if ( IsShowingMenu() && m_menuShown
->ProcessKeyDown(key
) )
2167 // cycle through the menu items when left/right arrows are pressed and open
2168 // the menu when up/down one is
2172 // Alt must be processed at wxWindow level too
2177 // remove the selection and give the focus away
2178 if ( m_current
!= -1 )
2180 if ( IsShowingMenu() )
2192 size_t count
= GetCount();
2195 // the item won't change anyhow
2198 //else: otherwise, it will
2200 // remember if we were showing a menu - if we did, we should
2201 // show the new menu after changing the item
2202 bool wasMenuOpened
= IsShowingMenu();
2203 if ( wasMenuOpened
)
2208 // cast is safe as we tested for -1 above
2209 size_t currentNew
= (size_t)m_current
;
2211 if ( key
== WXK_LEFT
)
2213 if ( currentNew
-- == 0 )
2214 currentNew
= count
- 1;
2218 if ( ++currentNew
== count
)
2222 DoSelectMenu(currentNew
);
2224 if ( wasMenuOpened
)
2239 // letters open the corresponding menu
2242 int idxFound
= FindNextItemForAccel(m_current
, key
, &unique
);
2244 if ( idxFound
!= -1 )
2246 if ( IsShowingMenu() )
2251 DoSelectMenu((size_t)idxFound
);
2253 // if the item is not unique, just select it but don't
2254 // activate as the user might have wanted to activate
2257 // also, don't try to open a disabled menu
2258 if ( unique
&& IsEnabledTop((size_t)idxFound
) )
2264 // skip the "event.Skip()" below
2273 // ----------------------------------------------------------------------------
2274 // wxMenuBar accel handling
2275 // ----------------------------------------------------------------------------
2277 int wxMenuBar::FindNextItemForAccel(int idxStart
, int key
, bool *unique
) const
2279 if ( !wxIsalnum(key
) )
2281 // we only support letters/digits as accels
2285 // do we have more than one item with this accel?
2289 // translate everything to lower case before comparing
2290 wxChar chAccel
= wxTolower(key
);
2292 // the index of the item with this accel
2295 // loop through all items searching for the item with this
2296 // accel starting at the item after the current one
2297 int count
= GetCount();
2298 int n
= idxStart
== -1 ? 0 : idxStart
+ 1;
2309 const wxMenuInfo
& info
= m_menuInfos
[n
];
2311 int idxAccel
= info
.GetAccelIndex();
2312 if ( idxAccel
!= -1 &&
2313 wxTolower(info
.GetLabel()[(size_t)idxAccel
])
2316 // ok, found an item with this accel
2317 if ( idxFound
== -1 )
2319 // store it but continue searching as we need to
2320 // know if it's the only item with this accel or if
2324 else // we already had found such item
2329 // no need to continue further, we won't find
2330 // anything we don't already know
2335 // we want to iterate over all items wrapping around if
2343 if ( n
== idxStart
)
2345 // we've seen all items
2355 bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent
& event
)
2358 for ( wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
2360 node
= node
->GetNext(), n
++ )
2362 // accels of the items in the disabled menus shouldn't work
2363 if ( m_menuInfos
[n
].IsEnabled() )
2365 if ( node
->GetData()->ProcessAccelEvent(event
) )
2367 // menu processed it
2377 #endif // wxUSE_ACCEL
2379 // ----------------------------------------------------------------------------
2380 // wxMenuBar menus showing
2381 // ----------------------------------------------------------------------------
2383 void wxMenuBar::PopupCurrentMenu(bool selectFirst
)
2385 wxCHECK_RET( m_current
!= -1, _T("no menu to popup") );
2387 // forgot to call DismissMenu()?
2388 wxASSERT_MSG( !m_menuShown
, _T("shouldn't show two menus at once!") );
2390 // in any case, we should show it - even if we won't
2391 m_shouldShowMenu
= TRUE
;
2393 if ( IsEnabledTop(m_current
) )
2395 // remember the menu we show
2396 m_menuShown
= GetMenu(m_current
);
2398 // we don't show the menu at all if it has no items
2399 if ( !m_menuShown
->IsEmpty() )
2401 // position it correctly: note that we must use screen coords and
2402 // that we pass 0 as width to position the menu exactly below the
2403 // item, not to the right of it
2404 wxRect rectItem
= GetItemRect(m_current
);
2406 m_menuShown
->Popup(ClientToScreen(rectItem
.GetPosition()),
2407 wxSize(0, rectItem
.GetHeight()),
2412 // reset it back as no menu is shown
2416 //else: don't show disabled menu
2419 void wxMenuBar::DismissMenu()
2421 wxCHECK_RET( m_menuShown
, _T("can't dismiss menu if none is shown") );
2423 m_menuShown
->Dismiss();
2427 void wxMenuBar::OnDismissMenu(bool dismissMenuBar
)
2429 m_shouldShowMenu
= FALSE
;
2431 if ( dismissMenuBar
)
2437 void wxMenuBar::OnDismiss()
2441 wxLogTrace(_T("mousecapture"), _T("Releasing mouse from wxMenuBar::OnDismiss"));
2442 GetCapture()->ReleaseMouse();
2445 if ( m_current
!= -1 )
2447 size_t current
= m_current
;
2450 RefreshItem(current
);
2456 void wxMenuBar::GiveAwayFocus()
2458 GetFrame()->SetFocus();
2461 // ----------------------------------------------------------------------------
2462 // popup menu support
2463 // ----------------------------------------------------------------------------
2465 wxEventLoop
*wxWindow::ms_evtLoopPopup
= NULL
;
2467 bool wxWindow::DoPopupMenu(wxMenu
*menu
, int x
, int y
)
2469 wxCHECK_MSG( !ms_evtLoopPopup
, FALSE
,
2470 _T("can't show more than one popup menu at a time") );
2473 // we need to change the cursor before showing the menu as, apparently, no
2474 // cursor changes took place while the mouse is captured
2475 wxCursor cursorOld
= GetCursor();
2476 SetCursor(wxCURSOR_ARROW
);
2480 // flash any delayed log messages before showing the menu, otherwise it
2481 // could be dismissed (because it would lose focus) immediately after being
2483 wxLog::FlushActive();
2485 // some controls update themselves from OnIdle() call - let them do it
2486 wxTheApp
->ProcessIdle();
2488 // if the window hadn't been refreshed yet, the menu can adversely affect
2489 // its next OnPaint() handler execution - i.e. scrolled window refresh
2490 // logic breaks then as it scrolls part of the menu which hadn't been there
2491 // when the update event was generated into view
2495 menu
->SetInvokingWindow(this);
2497 // wxLogDebug( "Name of invoking window %s", menu->GetInvokingWindow()->GetName().c_str() );
2499 menu
->Popup(ClientToScreen(wxPoint(x
, y
)), wxSize(0, 0));
2501 // this is not very useful if the menu was popped up because of the mouse
2502 // click but I think it is nice to do when it appears because of a key
2503 // press (i.e. Windows menu key)
2505 // Windows itself doesn't do it, but IMHO this is nice
2508 // we have to redirect all keyboard input to the menu temporarily
2509 PushEventHandler(new wxMenuKbdRedirector(menu
));
2511 // enter the local modal loop
2512 ms_evtLoopPopup
= new wxEventLoop
;
2513 ms_evtLoopPopup
->Run();
2515 delete ms_evtLoopPopup
;
2516 ms_evtLoopPopup
= NULL
;
2518 // remove the handler
2519 PopEventHandler(TRUE
/* delete it */);
2521 menu
->SetInvokingWindow(NULL
);
2524 SetCursor(cursorOld
);
2530 void wxWindow::DismissPopupMenu()
2532 wxCHECK_RET( ms_evtLoopPopup
, _T("no popup menu shown") );
2534 ms_evtLoopPopup
->Exit();
2537 #endif // wxUSE_MENUS