1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/menu.cpp
3 // Purpose: wxMenuItem, wxMenu and wxMenuBar implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
31 #include "wx/dynarray.h"
32 #include "wx/control.h" // for FindAccelIndex()
33 #include "wx/settings.h"
37 #include "wx/dcclient.h"
40 #include "wx/popupwin.h"
41 #include "wx/evtloop.h"
43 #include "wx/univ/renderer.h"
46 #include "wx/msw/private.h"
49 typedef wxMenuItemList::compatibility_iterator wxMenuItemIter
;
51 // ----------------------------------------------------------------------------
52 // wxMenuInfo contains all extra information about top level menus we need
53 // ----------------------------------------------------------------------------
55 class WXDLLEXPORT wxMenuInfo
59 wxMenuInfo(const wxString
& text
)
67 void SetLabel(const wxString
& text
)
69 // remember the accel char (may be -1 if none)
70 m_indexAccel
= wxControl::FindAccelIndex(text
, &m_label
);
72 // calculate the width later, after the menu bar is created
76 void SetEnabled(bool enabled
= true) { m_isEnabled
= enabled
; }
80 const wxString
& GetLabel() const { return m_label
; }
81 bool IsEnabled() const { return m_isEnabled
; }
82 wxCoord
GetWidth(wxMenuBar
*menubar
) const
86 wxConstCast(this, wxMenuInfo
)->CalcWidth(menubar
);
92 int GetAccelIndex() const { return m_indexAccel
; }
95 void CalcWidth(wxMenuBar
*menubar
)
98 wxClientDC
dc(menubar
);
99 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
100 dc
.GetTextExtent(m_label
, &size
.x
, &size
.y
);
102 // adjust for the renderer we use and store the width
103 m_width
= menubar
->GetRenderer()->GetMenuBarItemSize(size
).x
;
112 #include "wx/arrimpl.cpp"
114 WX_DEFINE_OBJARRAY(wxMenuInfoArray
);
116 // ----------------------------------------------------------------------------
117 // wxPopupMenuWindow: a popup window showing a menu
118 // ----------------------------------------------------------------------------
120 class wxPopupMenuWindow
: public wxPopupTransientWindow
123 wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
);
125 ~wxPopupMenuWindow();
127 // override the base class version to select the first item initially
128 virtual void Popup(wxWindow
*focus
= NULL
);
130 // override the base class version to dismiss any open submenus
131 virtual void Dismiss();
133 // called when a submenu is dismissed
134 void OnSubmenuDismiss(bool dismissParent
);
136 // the default wxMSW wxPopupTransientWindow::OnIdle disables the capture
137 // when the cursor is inside the popup, which dsables the menu tracking
138 // so override it to do nothing
140 void OnIdle(wxIdleEvent
& WXUNUSED(event
)) { }
143 // get the currently selected item (may be NULL)
144 wxMenuItem
*GetCurrentItem() const
146 return m_nodeCurrent
? m_nodeCurrent
->GetData() : NULL
;
149 // find the menu item at given position
150 wxMenuItemIter
GetMenuItemFromPoint(const wxPoint
& pt
) const;
152 // refresh the given item
153 void RefreshItem(wxMenuItem
*item
);
155 // preselect the first item
156 void SelectFirst() { SetCurrentItem(m_menu
->GetMenuItems().GetFirst()); }
158 // process the key event, return true if done
159 bool ProcessKeyDown(int key
);
161 // process mouse move event
162 void ProcessMouseMove(const wxPoint
& pt
);
164 // don't dismiss the popup window if the parent menu was clicked
165 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
168 // how did we perform this operation?
175 // notify the menu when the window disappears from screen
176 virtual void OnDismiss();
178 // draw the menu inside this window
179 virtual void DoDraw(wxControlRenderer
*renderer
);
182 void OnLeftUp(wxMouseEvent
& event
);
183 void OnMouseMove(wxMouseEvent
& event
);
184 void OnMouseLeave(wxMouseEvent
& event
);
185 void OnKeyDown(wxKeyEvent
& event
);
187 // reset the current item and node
190 // set the current node and item without refreshing anything
191 void SetCurrentItem(wxMenuItemIter node
);
193 // change the current item refreshing the old and new items
194 void ChangeCurrent(wxMenuItemIter node
);
196 // activate item, i.e. call either ClickItem() or OpenSubmenu() depending
197 // on what it is, return true if something was done (i.e. it's not a
199 bool ActivateItem(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
201 // send the event about the item click
202 void ClickItem(wxMenuItem
*item
);
204 // show the submenu for this item
205 void OpenSubmenu(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
207 // can this tiem be opened?
208 bool CanOpen(wxMenuItem
*item
)
210 return item
&& item
->IsEnabled() && item
->IsSubMenu();
213 // dismiss the menu and all parent menus too
214 void DismissAndNotify();
216 // react to dimissing this menu and also dismiss the parent if
218 void HandleDismiss(bool dismissParent
);
220 // do we have an open submenu?
221 bool HasOpenSubmenu() const { return m_hasOpenSubMenu
; }
223 // get previous node after the current one
224 wxMenuItemIter
GetPrevNode() const;
226 // get previous node before the given one, wrapping if it's the first one
227 wxMenuItemIter
GetPrevNode(wxMenuItemIter node
) const;
229 // get next node after the current one
230 wxMenuItemIter
GetNextNode() const;
232 // get next node after the given one, wrapping if it's the last one
233 wxMenuItemIter
GetNextNode(wxMenuItemIter node
) const;
239 // the menu node corresponding to the current item
240 wxMenuItemIter m_nodeCurrent
;
242 // do we currently have an opened submenu?
243 bool m_hasOpenSubMenu
;
245 DECLARE_EVENT_TABLE()
248 // ----------------------------------------------------------------------------
249 // wxMenuKbdRedirector: an event handler which redirects kbd input to wxMenu
250 // ----------------------------------------------------------------------------
252 class wxMenuKbdRedirector
: public wxEvtHandler
255 wxMenuKbdRedirector(wxMenu
*menu
) { m_menu
= menu
; }
257 virtual bool ProcessEvent(wxEvent
& event
)
259 if ( event
.GetEventType() == wxEVT_KEY_DOWN
)
261 return m_menu
->ProcessKeyDown(((wxKeyEvent
&)event
).GetKeyCode());
267 return wxEvtHandler::ProcessEvent(event
);
275 // ----------------------------------------------------------------------------
277 // ----------------------------------------------------------------------------
279 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
280 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
281 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
283 BEGIN_EVENT_TABLE(wxPopupMenuWindow
, wxPopupTransientWindow
)
284 EVT_KEY_DOWN(wxPopupMenuWindow::OnKeyDown
)
286 EVT_LEFT_UP(wxPopupMenuWindow::OnLeftUp
)
287 EVT_MOTION(wxPopupMenuWindow::OnMouseMove
)
288 EVT_LEAVE_WINDOW(wxPopupMenuWindow::OnMouseLeave
)
290 EVT_IDLE(wxPopupMenuWindow::OnIdle
)
294 BEGIN_EVENT_TABLE(wxMenuBar
, wxMenuBarBase
)
295 EVT_KILL_FOCUS(wxMenuBar::OnKillFocus
)
297 EVT_KEY_DOWN(wxMenuBar::OnKeyDown
)
299 EVT_LEFT_DOWN(wxMenuBar::OnLeftDown
)
300 EVT_MOTION(wxMenuBar::OnMouseMove
)
303 // ============================================================================
305 // ============================================================================
307 // ----------------------------------------------------------------------------
309 // ----------------------------------------------------------------------------
311 wxPopupMenuWindow::wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
)
314 m_hasOpenSubMenu
= false;
318 (void)Create(parent
, wxBORDER_RAISED
);
320 SetCursor(wxCURSOR_ARROW
);
323 wxPopupMenuWindow::~wxPopupMenuWindow()
325 // When m_popupMenu in wxMenu is deleted because it
326 // is a child of an old menu bar being deleted (note: it does
327 // not get destroyed by the wxMenu destructor, but
328 // by DestroyChildren()), m_popupMenu should be reset to NULL.
330 m_menu
->m_popupMenu
= NULL
;
333 // ----------------------------------------------------------------------------
334 // wxPopupMenuWindow current item/node handling
335 // ----------------------------------------------------------------------------
337 void wxPopupMenuWindow::ResetCurrent()
339 SetCurrentItem(wxMenuItemIter());
342 void wxPopupMenuWindow::SetCurrentItem(wxMenuItemIter node
)
344 m_nodeCurrent
= node
;
347 void wxPopupMenuWindow::ChangeCurrent(wxMenuItemIter node
)
349 if ( node
!= m_nodeCurrent
)
351 wxMenuItemIter nodeOldCurrent
= m_nodeCurrent
;
353 m_nodeCurrent
= node
;
355 if ( nodeOldCurrent
)
357 wxMenuItem
*item
= nodeOldCurrent
->GetData();
358 wxCHECK_RET( item
, _T("no current item?") );
360 // if it was the currently opened menu, close it
361 if ( item
->IsSubMenu() && item
->GetSubMenu()->IsShown() )
363 item
->GetSubMenu()->Dismiss();
364 OnSubmenuDismiss( false );
371 RefreshItem(m_nodeCurrent
->GetData());
375 wxMenuItemIter
wxPopupMenuWindow::GetPrevNode() const
377 // return the last node if there had been no previously selected one
378 return m_nodeCurrent
? GetPrevNode(m_nodeCurrent
)
379 : wxMenuItemIter(m_menu
->GetMenuItems().GetLast());
383 wxPopupMenuWindow::GetPrevNode(wxMenuItemIter node
) const
387 node
= node
->GetPrevious();
390 node
= m_menu
->GetMenuItems().GetLast();
393 //else: the menu is empty
398 wxMenuItemIter
wxPopupMenuWindow::GetNextNode() const
400 // return the first node if there had been no previously selected one
401 return m_nodeCurrent
? GetNextNode(m_nodeCurrent
)
402 : wxMenuItemIter(m_menu
->GetMenuItems().GetFirst());
406 wxPopupMenuWindow::GetNextNode(wxMenuItemIter node
) const
410 node
= node
->GetNext();
413 node
= m_menu
->GetMenuItems().GetFirst();
416 //else: the menu is empty
421 // ----------------------------------------------------------------------------
422 // wxPopupMenuWindow popup/dismiss
423 // ----------------------------------------------------------------------------
425 void wxPopupMenuWindow::Popup(wxWindow
*focus
)
427 // check that the current item had been properly reset before
428 wxASSERT_MSG( !m_nodeCurrent
||
429 m_nodeCurrent
== m_menu
->GetMenuItems().GetFirst(),
430 _T("menu current item preselected incorrectly") );
432 wxPopupTransientWindow::Popup(focus
);
434 // the base class no-longer captures the mouse automatically when Popup
435 // is called, so do it here to allow the menu tracking to work
440 // ensure that this window is really on top of everything: without using
441 // SetWindowPos() it can be covered by its parent menu which is not
442 // really what we want
443 wxMenu
*menuParent
= m_menu
->GetParent();
446 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
448 // if we're shown, the parent menu must be also shown
449 wxCHECK_RET( win
, _T("parent menu is not shown?") );
451 if ( !::SetWindowPos(GetHwndOf(win
), GetHwnd(),
453 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOREDRAW
) )
455 wxLogLastError(_T("SetWindowPos(HWND_TOP)"));
463 void wxPopupMenuWindow::Dismiss()
465 if ( HasOpenSubmenu() )
467 wxMenuItem
*item
= GetCurrentItem();
468 wxCHECK_RET( item
&& item
->IsSubMenu(), _T("where is our open submenu?") );
470 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
471 wxCHECK_RET( win
, _T("opened submenu is not opened?") );
474 OnSubmenuDismiss( false );
477 wxPopupTransientWindow::Dismiss();
482 void wxPopupMenuWindow::OnDismiss()
484 // when we are dismissed because the user clicked elsewhere or we lost
485 // focus in any other way, hide the parent menu as well
489 void wxPopupMenuWindow::OnSubmenuDismiss(bool WXUNUSED(dismissParent
))
491 m_hasOpenSubMenu
= false;
494 void wxPopupMenuWindow::HandleDismiss(bool dismissParent
)
496 m_menu
->OnDismiss(dismissParent
);
499 void wxPopupMenuWindow::DismissAndNotify()
505 // ----------------------------------------------------------------------------
506 // wxPopupMenuWindow geometry
507 // ----------------------------------------------------------------------------
510 wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint
& pt
) const
512 // we only use the y coord normally, but still check x in case the point is
513 // outside the window completely
514 if ( wxWindow::HitTest(pt
) == wxHT_WINDOW_INSIDE
)
517 for ( wxMenuItemIter node
= m_menu
->GetMenuItems().GetFirst();
519 node
= node
->GetNext() )
521 wxMenuItem
*item
= node
->GetData();
522 y
+= item
->GetHeight();
531 return wxMenuItemIter();
534 // ----------------------------------------------------------------------------
535 // wxPopupMenuWindow drawing
536 // ----------------------------------------------------------------------------
538 void wxPopupMenuWindow::RefreshItem(wxMenuItem
*item
)
540 wxCHECK_RET( item
, _T("can't refresh NULL item") );
542 wxASSERT_MSG( IsShown(), _T("can't refresh menu which is not shown") );
544 // FIXME: -1 here because of SetLogicalOrigin(1, 1) in DoDraw()
545 RefreshRect(wxRect(0, item
->GetPosition() - 1,
546 m_menu
->GetGeometryInfo().GetSize().x
, item
->GetHeight()));
549 void wxPopupMenuWindow::DoDraw(wxControlRenderer
*renderer
)
551 // no clipping so far - do we need it? I don't think so as the menu is
552 // never partially covered as it is always on top of everything
554 wxDC
& dc
= renderer
->GetDC();
555 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
557 // FIXME: this should be done in the renderer, however when it is fixed
558 // wxPopupMenuWindow::RefreshItem() should be changed too!
559 dc
.SetLogicalOrigin(1, 1);
561 wxRenderer
*rend
= renderer
->GetRenderer();
564 const wxMenuGeometryInfo
& gi
= m_menu
->GetGeometryInfo();
565 for ( wxMenuItemIter node
= m_menu
->GetMenuItems().GetFirst();
567 node
= node
->GetNext() )
569 wxMenuItem
*item
= node
->GetData();
571 if ( item
->IsSeparator() )
573 rend
->DrawMenuSeparator(dc
, y
, gi
);
575 else // not a separator
578 if ( item
->IsCheckable() )
580 flags
|= wxCONTROL_CHECKABLE
;
582 if ( item
->IsChecked() )
584 flags
|= wxCONTROL_CHECKED
;
588 if ( !item
->IsEnabled() )
589 flags
|= wxCONTROL_DISABLED
;
591 if ( item
->IsSubMenu() )
592 flags
|= wxCONTROL_ISSUBMENU
;
594 if ( item
== GetCurrentItem() )
595 flags
|= wxCONTROL_SELECTED
;
599 if ( !item
->IsEnabled() )
601 bmp
= item
->GetDisabledBitmap();
606 // strangely enough, for unchecked item we use the
607 // "checked" bitmap because this is the default one - this
608 // explains this strange boolean expression
609 bmp
= item
->GetBitmap(!item
->IsCheckable() || item
->IsChecked());
618 item
->GetAccelString(),
621 item
->GetAccelIndex()
625 y
+= item
->GetHeight();
629 // ----------------------------------------------------------------------------
630 // wxPopupMenuWindow actions
631 // ----------------------------------------------------------------------------
633 void wxPopupMenuWindow::ClickItem(wxMenuItem
*item
)
635 wxCHECK_RET( item
, _T("can't click NULL item") );
637 wxASSERT_MSG( !item
->IsSeparator() && !item
->IsSubMenu(),
638 _T("can't click this item") );
640 wxMenu
* menu
= m_menu
;
645 menu
->ClickItem(item
);
648 void wxPopupMenuWindow::OpenSubmenu(wxMenuItem
*item
, InputMethod how
)
650 wxCHECK_RET( item
, _T("can't open NULL submenu") );
652 wxMenu
*submenu
= item
->GetSubMenu();
653 wxCHECK_RET( submenu
, _T("can only open submenus!") );
655 // FIXME: should take into account the border width
656 submenu
->Popup(ClientToScreen(wxPoint(0, item
->GetPosition())),
657 wxSize(m_menu
->GetGeometryInfo().GetSize().x
, 0),
658 how
== WithKeyboard
/* preselect first item then */);
660 m_hasOpenSubMenu
= true;
663 bool wxPopupMenuWindow::ActivateItem(wxMenuItem
*item
, InputMethod how
)
665 // don't activate disabled items
666 if ( !item
|| !item
->IsEnabled() )
671 // normal menu items generate commands, submenus can be opened and
672 // the separators don't do anything
673 if ( item
->IsSubMenu() )
675 OpenSubmenu(item
, how
);
677 else if ( !item
->IsSeparator() )
681 else // separator, can't activate
689 // ----------------------------------------------------------------------------
690 // wxPopupMenuWindow input handling
691 // ----------------------------------------------------------------------------
693 bool wxPopupMenuWindow::ProcessLeftDown(wxMouseEvent
& event
)
695 // wxPopupWindowHandler dismisses the window when the mouse is clicked
696 // outside it which is usually just fine, but there is one case when we
697 // don't want to do it: if the mouse was clicked on the parent submenu item
698 // which opens this menu, so check for it
700 wxPoint pos
= event
.GetPosition();
701 if ( HitTest(pos
.x
, pos
.y
) == wxHT_WINDOW_OUTSIDE
)
703 wxMenu
*menu
= m_menu
->GetParent();
706 wxPopupMenuWindow
*win
= menu
->m_popupMenu
;
708 wxCHECK_MSG( win
, false, _T("parent menu not shown?") );
710 pos
= ClientToScreen(pos
);
711 if ( win
->GetMenuItemFromPoint(win
->ScreenToClient(pos
)) )
716 //else: it is outside the parent menu as well, do dismiss this one
723 void wxPopupMenuWindow::OnLeftUp(wxMouseEvent
& event
)
725 wxMenuItemIter node
= GetMenuItemFromPoint(event
.GetPosition());
728 ActivateItem(node
->GetData(), WithMouse
);
732 void wxPopupMenuWindow::OnMouseMove(wxMouseEvent
& event
)
734 const wxPoint pt
= event
.GetPosition();
736 // we need to ignore extra mouse events: example when this happens is when
737 // the mouse is on the menu and we open a submenu from keyboard - Windows
738 // then sends us a dummy mouse move event, we (correctly) determine that it
739 // happens in the parent menu and so immediately close the just opened
742 static wxPoint s_ptLast
;
743 wxPoint ptCur
= ClientToScreen(pt
);
744 if ( ptCur
== s_ptLast
)
752 ProcessMouseMove(pt
);
757 void wxPopupMenuWindow::ProcessMouseMove(const wxPoint
& pt
)
759 wxMenuItemIter node
= GetMenuItemFromPoint(pt
);
761 // don't reset current to NULL here, we only do it when the mouse leaves
762 // the window (see below)
765 if ( node
!= m_nodeCurrent
)
769 wxMenuItem
*item
= GetCurrentItem();
772 OpenSubmenu(item
, WithMouse
);
775 //else: same item, nothing to do
777 else // not on an item
779 // the last open submenu forwards the mouse move messages to its
780 // parent, so if the mouse moves to another item of the parent menu,
781 // this menu is closed and this other item is selected - in the similar
782 // manner, the top menu forwards the mouse moves to the menubar which
783 // allows to select another top level menu by just moving the mouse
785 // we need to translate our client coords to the client coords of the
786 // window we forward this event to
787 wxPoint ptScreen
= ClientToScreen(pt
);
789 // if the mouse is outside this menu, let the parent one to
791 wxMenu
*menuParent
= m_menu
->GetParent();
794 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
796 // if we're shown, the parent menu must be also shown
797 wxCHECK_RET( win
, _T("parent menu is not shown?") );
799 win
->ProcessMouseMove(win
->ScreenToClient(ptScreen
));
801 else // no parent menu
803 wxMenuBar
*menubar
= m_menu
->GetMenuBar();
806 if ( menubar
->ProcessMouseEvent(
807 menubar
->ScreenToClient(ptScreen
)) )
809 // menubar has closed this menu and opened another one, probably
814 //else: top level popup menu, no other processing to do
818 void wxPopupMenuWindow::OnMouseLeave(wxMouseEvent
& event
)
820 // due to the artefact of mouse events generation under MSW, we actually
821 // may get the mouse leave event after the menu had been already dismissed
822 // and calling ChangeCurrent() would then assert, so don't do it
825 // we shouldn't change the current them if our submenu is opened and
826 // mouse moved there, in this case the submenu is responsable for
829 if ( HasOpenSubmenu() )
831 wxMenuItem
*item
= GetCurrentItem();
832 wxCHECK_RET( CanOpen(item
), _T("where is our open submenu?") );
834 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
835 wxCHECK_RET( win
, _T("submenu is opened but not shown?") );
837 // only handle this event if the mouse is not inside the submenu
838 wxPoint pt
= ClientToScreen(event
.GetPosition());
840 win
->HitTest(win
->ScreenToClient(pt
)) == wxHT_WINDOW_OUTSIDE
;
844 // this menu is the last opened
850 ChangeCurrent(wxMenuItemIter());
857 void wxPopupMenuWindow::OnKeyDown(wxKeyEvent
& event
)
859 wxMenuBar
*menubar
= m_menu
->GetMenuBar();
863 menubar
->ProcessEvent(event
);
865 else if ( !ProcessKeyDown(event
.GetKeyCode()) )
871 bool wxPopupMenuWindow::ProcessKeyDown(int key
)
873 wxMenuItem
*item
= GetCurrentItem();
875 // first let the opened submenu to have it (no test for IsEnabled() here,
876 // the keys navigate even in a disabled submenu if we had somehow managed
877 // to open it inspit of this)
878 if ( HasOpenSubmenu() )
880 wxCHECK_MSG( CanOpen(item
), false,
881 _T("has open submenu but another item selected?") );
883 if ( item
->GetSubMenu()->ProcessKeyDown(key
) )
887 bool processed
= true;
889 // handle the up/down arrows, home, end, esc and return here, pass the
890 // left/right arrows to the menu bar except when the right arrow can be
891 // used to open a submenu
895 // if we're not a top level menu, close us, else leave this to the
897 if ( !m_menu
->GetParent() )
906 // close just this menu
908 HandleDismiss(false);
912 processed
= ActivateItem(item
);
916 ChangeCurrent(m_menu
->GetMenuItems().GetFirst());
920 ChangeCurrent(m_menu
->GetMenuItems().GetLast());
926 bool up
= key
== WXK_UP
;
928 wxMenuItemIter nodeStart
= up
? GetPrevNode() : GetNextNode(),
930 while ( node
&& node
->GetData()->IsSeparator() )
932 node
= up
? GetPrevNode(node
) : GetNextNode(node
);
934 if ( node
== nodeStart
)
936 // nothing but separators and disabled items in this
938 node
= wxMenuItemIter();
954 // don't try to reopen an already opened menu
955 if ( !HasOpenSubmenu() && CanOpen(item
) )
966 // look for the menu item starting with this letter
967 if ( wxIsalnum((wxChar
)key
) )
969 // we want to start from the item after this one because
970 // if we're already on the item with the given accel we want to
971 // go to the next one, not to stay in place
972 wxMenuItemIter nodeStart
= GetNextNode();
974 // do we have more than one item with this accel?
975 bool notUnique
= false;
977 // translate everything to lower case before comparing
978 wxChar chAccel
= (wxChar
)wxTolower(key
);
980 // loop through all items searching for the item with this
982 wxMenuItemIter nodeFound
,
986 item
= node
->GetData();
988 int idxAccel
= item
->GetAccelIndex();
989 if ( idxAccel
!= -1 &&
990 wxTolower(item
->GetLabel()[(size_t)idxAccel
])
993 // ok, found an item with this accel
996 // store it but continue searching as we need to
997 // know if it's the only item with this accel or if
1001 else // we already had found such item
1005 // no need to continue further, we won't find
1006 // anything we don't already know
1011 // we want to iterate over all items wrapping around if
1013 node
= GetNextNode(node
);
1014 if ( node
== nodeStart
)
1016 // we've seen all nodes
1023 item
= nodeFound
->GetData();
1025 // go to this item anyhow
1026 ChangeCurrent(nodeFound
);
1028 if ( !notUnique
&& item
->IsEnabled() )
1030 // unique item with this accel - activate it
1031 processed
= ActivateItem(item
);
1033 //else: just select it but don't activate as the user might
1034 // have wanted to activate another item
1036 // skip "processed = false" below
1047 // ----------------------------------------------------------------------------
1049 // ----------------------------------------------------------------------------
1057 m_startRadioGroup
= -1;
1066 // ----------------------------------------------------------------------------
1067 // wxMenu and wxMenuGeometryInfo
1068 // ----------------------------------------------------------------------------
1070 wxMenuGeometryInfo::~wxMenuGeometryInfo()
1074 const wxMenuGeometryInfo
& wxMenu::GetGeometryInfo() const
1080 wxConstCast(this, wxMenu
)->m_geometry
=
1081 m_popupMenu
->GetRenderer()->GetMenuGeometry(m_popupMenu
, *this);
1085 wxFAIL_MSG( _T("can't get geometry without window") );
1092 void wxMenu::InvalidateGeometryInfo()
1101 // ----------------------------------------------------------------------------
1102 // wxMenu adding/removing items
1103 // ----------------------------------------------------------------------------
1105 void wxMenu::OnItemAdded(wxMenuItem
*item
)
1107 InvalidateGeometryInfo();
1111 #endif // wxUSE_ACCEL
1113 // the submenus of a popup menu should have the same invoking window as it
1115 if ( m_invokingWindow
&& item
->IsSubMenu() )
1117 item
->GetSubMenu()->SetInvokingWindow(m_invokingWindow
);
1121 void wxMenu::EndRadioGroup()
1123 // we're not inside a radio group any longer
1124 m_startRadioGroup
= -1;
1127 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
1129 if ( item
->GetKind() == wxITEM_RADIO
)
1131 int count
= GetMenuItemCount();
1133 if ( m_startRadioGroup
== -1 )
1135 // start a new radio group
1136 m_startRadioGroup
= count
;
1138 // for now it has just one element
1139 item
->SetAsRadioGroupStart();
1140 item
->SetRadioGroupEnd(m_startRadioGroup
);
1142 else // extend the current radio group
1144 // we need to update its end item
1145 item
->SetRadioGroupStart(m_startRadioGroup
);
1146 wxMenuItemIter node
= GetMenuItems().Item(m_startRadioGroup
);
1150 node
->GetData()->SetRadioGroupEnd(count
);
1154 wxFAIL_MSG( _T("where is the radio group start item?") );
1158 else // not a radio item
1163 if ( !wxMenuBase::DoAppend(item
) )
1171 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1173 if ( !wxMenuBase::DoInsert(pos
, item
) )
1181 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1183 wxMenuItem
*itemOld
= wxMenuBase::DoRemove(item
);
1187 InvalidateGeometryInfo();
1190 RemoveAccelFor(item
);
1191 #endif // wxUSE_ACCEL
1197 // ----------------------------------------------------------------------------
1198 // wxMenu attaching/detaching
1199 // ----------------------------------------------------------------------------
1201 void wxMenu::Attach(wxMenuBarBase
*menubar
)
1203 wxMenuBase::Attach(menubar
);
1205 wxCHECK_RET( m_menuBar
, _T("menubar can't be NULL after attaching") );
1207 // unfortunately, we can't use m_menuBar->GetEventHandler() here because,
1208 // if the menubar is currently showing a menu, its event handler is a
1209 // temporary one installed by wxPopupWindow and so will disappear soon any
1210 // any attempts to use it from the newly attached menu would result in a
1213 // so we use the menubar itself, even if it's a pity as it means we can't
1214 // redirect all menu events by changing the menubar handler (FIXME)
1215 SetNextHandler(m_menuBar
);
1218 void wxMenu::Detach()
1220 wxMenuBase::Detach();
1223 // ----------------------------------------------------------------------------
1224 // wxMenu misc functions
1225 // ----------------------------------------------------------------------------
1227 wxWindow
*wxMenu::GetRootWindow() const
1231 // simple case - a normal menu attached to the menubar
1232 return GetMenuBar();
1235 // we're a popup menu but the trouble is that only the top level popup menu
1236 // has a pointer to the invoking window, so we must walk up the menu chain
1238 wxWindow
*win
= GetInvokingWindow();
1241 // we already have it
1245 wxMenu
*menu
= GetParent();
1248 // We are a submenu of a menu of a menubar
1249 if (menu
->GetMenuBar())
1250 return menu
->GetMenuBar();
1252 win
= menu
->GetInvokingWindow();
1256 menu
= menu
->GetParent();
1259 // we're probably going to crash in the caller anyhow, but try to detect
1260 // this error as soon as possible
1261 wxASSERT_MSG( win
, _T("menu without any associated window?") );
1263 // also remember it in this menu so that we don't have to search for it the
1265 wxConstCast(this, wxMenu
)->m_invokingWindow
= win
;
1270 wxRenderer
*wxMenu::GetRenderer() const
1272 // we're going to crash without renderer!
1273 wxCHECK_MSG( m_popupMenu
, NULL
, _T("neither popup nor menubar menu?") );
1275 return m_popupMenu
->GetRenderer();
1278 void wxMenu::RefreshItem(wxMenuItem
*item
)
1280 // the item geometry changed, so our might have changed as well
1281 InvalidateGeometryInfo();
1285 // this would be a bug in IsShown()
1286 wxCHECK_RET( m_popupMenu
, _T("must have popup window if shown!") );
1288 // recalc geometry to update the item height and such
1289 (void)GetGeometryInfo();
1291 m_popupMenu
->RefreshItem(item
);
1295 // ----------------------------------------------------------------------------
1296 // wxMenu showing and hiding
1297 // ----------------------------------------------------------------------------
1299 bool wxMenu::IsShown() const
1301 return m_popupMenu
&& m_popupMenu
->IsShown();
1304 void wxMenu::OnDismiss(bool dismissParent
)
1308 // always notify the parent about submenu disappearance
1309 wxPopupMenuWindow
*win
= m_menuParent
->m_popupMenu
;
1312 win
->OnSubmenuDismiss( true );
1316 wxFAIL_MSG( _T("parent menu not shown?") );
1319 // and if we dismiss everything, propagate to parent
1320 if ( dismissParent
)
1322 // dismissParent is recursive
1323 m_menuParent
->Dismiss();
1324 m_menuParent
->OnDismiss(true);
1327 else // no parent menu
1329 // notify the menu bar if we're a top level menu
1332 m_menuBar
->OnDismissMenu(dismissParent
);
1336 wxCHECK_RET( m_invokingWindow
, _T("what kind of menu is this?") );
1338 m_invokingWindow
->DismissPopupMenu();
1340 // Why reset it here? We need it for sending the event to...
1341 // SetInvokingWindow(NULL);
1346 void wxMenu::Popup(const wxPoint
& pos
, const wxSize
& size
, bool selectFirst
)
1348 // create the popup window if not done yet
1351 m_popupMenu
= new wxPopupMenuWindow(GetRootWindow(), this);
1354 // select the first item unless disabled
1357 m_popupMenu
->SelectFirst();
1360 // the geometry might have changed since the last time we were shown, so
1362 m_popupMenu
->SetClientSize(GetGeometryInfo().GetSize());
1364 // position it as specified
1365 m_popupMenu
->Position(pos
, size
);
1367 // the menu can't have the focus itself (it is a Windows limitation), so
1368 // always keep the focus at the originating window
1369 wxWindow
*focus
= GetRootWindow();
1371 wxASSERT_MSG( focus
, _T("no window to keep focus on?") );
1374 m_popupMenu
->Popup(focus
);
1377 void wxMenu::Dismiss()
1379 wxCHECK_RET( IsShown(), _T("can't dismiss hidden menu") );
1381 m_popupMenu
->Dismiss();
1384 // ----------------------------------------------------------------------------
1385 // wxMenu event processing
1386 // ----------------------------------------------------------------------------
1388 bool wxMenu::ProcessKeyDown(int key
)
1390 wxCHECK_MSG( m_popupMenu
, false,
1391 _T("can't process key events if not shown") );
1393 return m_popupMenu
->ProcessKeyDown(key
);
1396 bool wxMenu::ClickItem(wxMenuItem
*item
)
1399 if ( item
->IsCheckable() )
1401 // update the item state
1402 isChecked
= !item
->IsChecked();
1404 item
->Check(isChecked
!= 0);
1412 return SendEvent(item
->GetId(), isChecked
);
1415 // ----------------------------------------------------------------------------
1416 // wxMenu accel support
1417 // ----------------------------------------------------------------------------
1421 bool wxMenu::ProcessAccelEvent(const wxKeyEvent
& event
)
1423 // do we have an item for this accel?
1424 wxMenuItem
*item
= m_accelTable
.GetMenuItem(event
);
1425 if ( item
&& item
->IsEnabled() )
1427 return ClickItem(item
);
1431 for ( wxMenuItemIter node
= GetMenuItems().GetFirst();
1433 node
= node
->GetNext() )
1435 const wxMenuItem
*item
= node
->GetData();
1436 if ( item
->IsSubMenu() && item
->IsEnabled() )
1439 if ( item
->GetSubMenu()->ProcessAccelEvent(event
) )
1449 void wxMenu::AddAccelFor(wxMenuItem
*item
)
1451 wxAcceleratorEntry
*accel
= item
->GetAccel();
1454 accel
->SetMenuItem(item
);
1456 m_accelTable
.Add(*accel
);
1462 void wxMenu::RemoveAccelFor(wxMenuItem
*item
)
1464 wxAcceleratorEntry
*accel
= item
->GetAccel();
1467 m_accelTable
.Remove(*accel
);
1473 #endif // wxUSE_ACCEL
1475 // ----------------------------------------------------------------------------
1476 // wxMenuItem construction
1477 // ----------------------------------------------------------------------------
1479 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
1481 const wxString
& text
,
1482 const wxString
& help
,
1485 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
1488 m_height
= wxDefaultCoord
;
1490 m_radioGroup
.start
= -1;
1491 m_isRadioGroupStart
= false;
1493 m_bmpDisabled
= wxNullBitmap
;
1498 wxMenuItem::~wxMenuItem()
1502 // ----------------------------------------------------------------------------
1503 // wxMenuItemBase methods implemented here
1504 // ----------------------------------------------------------------------------
1507 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
1509 const wxString
& name
,
1510 const wxString
& help
,
1514 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
1518 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
1520 return wxStripMenuCodes(text
);
1523 // ----------------------------------------------------------------------------
1524 // wxMenuItem operations
1525 // ----------------------------------------------------------------------------
1527 void wxMenuItem::NotifyMenu()
1529 m_parentMenu
->RefreshItem(this);
1532 void wxMenuItem::UpdateAccelInfo()
1534 m_indexAccel
= wxControl::FindAccelIndex(m_text
);
1536 // will be empty if the text contains no TABs - ok
1537 m_strAccel
= m_text
.AfterFirst(_T('\t'));
1540 void wxMenuItem::SetText(const wxString
& text
)
1542 if ( text
!= m_text
)
1544 // first call the base class version to change m_text
1545 wxMenuItemBase::SetText(text
);
1553 void wxMenuItem::SetCheckable(bool checkable
)
1555 if ( checkable
!= IsCheckable() )
1557 wxMenuItemBase::SetCheckable(checkable
);
1563 void wxMenuItem::SetBitmaps(const wxBitmap
& bmpChecked
,
1564 const wxBitmap
& bmpUnchecked
)
1566 m_bmpChecked
= bmpChecked
;
1567 m_bmpUnchecked
= bmpUnchecked
;
1572 void wxMenuItem::Enable(bool enable
)
1574 if ( enable
!= m_isEnabled
)
1576 wxMenuItemBase::Enable(enable
);
1582 void wxMenuItem::Check(bool check
)
1584 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
1586 if ( m_isChecked
== check
)
1589 if ( GetKind() == wxITEM_RADIO
)
1591 // it doesn't make sense to uncheck a radio item - what would this do?
1595 // get the index of this item in the menu
1596 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
1597 int pos
= items
.IndexOf(this);
1598 wxCHECK_RET( pos
!= wxNOT_FOUND
,
1599 _T("menuitem not found in the menu items list?") );
1601 // get the radio group range
1605 if ( m_isRadioGroupStart
)
1607 // we already have all information we need
1609 end
= m_radioGroup
.end
;
1611 else // next radio group item
1613 // get the radio group end from the start item
1614 start
= m_radioGroup
.start
;
1615 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
1618 // also uncheck all the other items in this radio group
1619 wxMenuItemIter node
= items
.Item(start
);
1620 for ( int n
= start
; n
<= end
&& node
; n
++ )
1624 node
->GetData()->m_isChecked
= false;
1626 node
= node
->GetNext();
1630 wxMenuItemBase::Check(check
);
1635 // radio group stuff
1636 // -----------------
1638 void wxMenuItem::SetAsRadioGroupStart()
1640 m_isRadioGroupStart
= true;
1643 void wxMenuItem::SetRadioGroupStart(int start
)
1645 wxASSERT_MSG( !m_isRadioGroupStart
,
1646 _T("should only be called for the next radio items") );
1648 m_radioGroup
.start
= start
;
1651 void wxMenuItem::SetRadioGroupEnd(int end
)
1653 wxASSERT_MSG( m_isRadioGroupStart
,
1654 _T("should only be called for the first radio item") );
1656 m_radioGroup
.end
= end
;
1659 // ----------------------------------------------------------------------------
1660 // wxMenuBar creation
1661 // ----------------------------------------------------------------------------
1663 void wxMenuBar::Init()
1671 m_shouldShowMenu
= false;
1674 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
1678 for (size_t i
= 0; i
< n
; ++i
)
1679 Append(menus
[i
], titles
[i
]);
1682 void wxMenuBar::Attach(wxFrame
*frame
)
1684 // maybe you really wanted to call Detach()?
1685 wxCHECK_RET( frame
, _T("wxMenuBar::Attach(NULL) called") );
1687 wxMenuBarBase::Attach(frame
);
1691 // reparent if necessary
1692 if ( m_frameLast
!= frame
)
1697 // show it back - was hidden by Detach()
1700 else // not created yet, do it now
1702 // we have no way to return the error from here anyhow :-(
1703 (void)Create(frame
, wxID_ANY
);
1705 SetCursor(wxCURSOR_ARROW
);
1707 SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT
));
1709 // calculate and set our height (it won't be changed any more)
1710 SetSize(wxDefaultCoord
, GetBestSize().y
);
1713 // remember the last frame which had us to avoid unnecessarily reparenting
1715 m_frameLast
= frame
;
1718 void wxMenuBar::Detach()
1720 // don't delete the window because we may be reattached later, just hide it
1726 wxMenuBarBase::Detach();
1729 wxMenuBar::~wxMenuBar()
1733 // ----------------------------------------------------------------------------
1734 // wxMenuBar adding/removing items
1735 // ----------------------------------------------------------------------------
1737 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1739 return Insert(GetCount(), menu
, title
);
1742 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1744 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1747 wxMenuInfo
*info
= new wxMenuInfo(title
);
1748 m_menuInfos
.Insert(info
, pos
);
1750 RefreshAllItemsAfter(pos
);
1755 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1757 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1761 wxMenuInfo
& info
= m_menuInfos
[pos
];
1763 info
.SetLabel(title
);
1765 // even if the old menu was disabled, the new one is not any more
1768 // even if we change only this one, the new label has different width,
1769 // so we need to refresh everything beyond this item as well
1770 RefreshAllItemsAfter(pos
);
1776 wxMenu
*wxMenuBar::Remove(size_t pos
)
1778 wxMenu
*menuOld
= wxMenuBarBase::Remove(pos
);
1782 m_menuInfos
.RemoveAt(pos
);
1784 // this doesn't happen too often, so don't try to be too smart - just
1785 // refresh everything
1792 // ----------------------------------------------------------------------------
1793 // wxMenuBar top level menus access
1794 // ----------------------------------------------------------------------------
1796 wxCoord
wxMenuBar::GetItemWidth(size_t pos
) const
1798 return m_menuInfos
[pos
].GetWidth(wxConstCast(this, wxMenuBar
));
1801 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1803 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1805 if ( enable
!= m_menuInfos
[pos
].IsEnabled() )
1807 m_menuInfos
[pos
].SetEnabled(enable
);
1811 //else: nothing to do
1814 bool wxMenuBar::IsEnabledTop(size_t pos
) const
1816 wxCHECK_MSG( pos
< GetCount(), false, _T("invalid index in IsEnabledTop") );
1818 return m_menuInfos
[pos
].IsEnabled();
1821 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
1823 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1825 if ( label
!= m_menuInfos
[pos
].GetLabel() )
1827 m_menuInfos
[pos
].SetLabel(label
);
1831 //else: nothing to do
1834 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
1836 wxCHECK_MSG( pos
< GetCount(), wxEmptyString
, _T("invalid index in GetLabelTop") );
1838 return m_menuInfos
[pos
].GetLabel();
1841 // ----------------------------------------------------------------------------
1842 // wxMenuBar drawing
1843 // ----------------------------------------------------------------------------
1845 void wxMenuBar::RefreshAllItemsAfter(size_t pos
)
1849 // no need to refresh if nothing is shown yet
1853 wxRect rect
= GetItemRect(pos
);
1854 rect
.width
= GetClientSize().x
- rect
.x
;
1858 void wxMenuBar::RefreshItem(size_t pos
)
1860 wxCHECK_RET( pos
!= (size_t)-1,
1861 _T("invalid item in wxMenuBar::RefreshItem") );
1865 // no need to refresh if nothing is shown yet
1869 RefreshRect(GetItemRect(pos
));
1872 void wxMenuBar::DoDraw(wxControlRenderer
*renderer
)
1874 wxDC
& dc
= renderer
->GetDC();
1875 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1877 // redraw only the items which must be redrawn
1879 // we don't have to use GetUpdateClientRect() here because our client rect
1880 // is the same as total one
1881 wxRect rectUpdate
= GetUpdateRegion().GetBox();
1883 int flagsMenubar
= GetStateFlags();
1887 rect
.height
= GetClientSize().y
;
1890 size_t count
= GetCount();
1891 for ( size_t n
= 0; n
< count
; n
++ )
1893 if ( x
> rectUpdate
.GetRight() )
1895 // all remaining items are to the right of rectUpdate
1900 rect
.width
= GetItemWidth(n
);
1902 if ( x
< rectUpdate
.x
)
1904 // this item is still to the left of rectUpdate
1908 int flags
= flagsMenubar
;
1909 if ( m_current
!= -1 && n
== (size_t)m_current
)
1911 flags
|= wxCONTROL_SELECTED
;
1914 if ( !IsEnabledTop(n
) )
1916 flags
|= wxCONTROL_DISABLED
;
1919 GetRenderer()->DrawMenuBarItem
1923 m_menuInfos
[n
].GetLabel(),
1925 m_menuInfos
[n
].GetAccelIndex()
1930 // ----------------------------------------------------------------------------
1931 // wxMenuBar geometry
1932 // ----------------------------------------------------------------------------
1934 wxRect
wxMenuBar::GetItemRect(size_t pos
) const
1936 wxASSERT_MSG( pos
< GetCount(), _T("invalid menu bar item index") );
1937 wxASSERT_MSG( IsCreated(), _T("can't call this method yet") );
1942 rect
.height
= GetClientSize().y
;
1944 for ( size_t n
= 0; n
< pos
; n
++ )
1946 rect
.x
+= GetItemWidth(n
);
1949 rect
.width
= GetItemWidth(pos
);
1954 wxSize
wxMenuBar::DoGetBestClientSize() const
1957 if ( GetMenuCount() > 0 )
1959 wxClientDC
dc(wxConstCast(this, wxMenuBar
));
1960 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1961 dc
.GetTextExtent(GetLabelTop(0), &size
.x
, &size
.y
);
1963 // adjust for the renderer we use
1964 size
= GetRenderer()->GetMenuBarItemSize(size
);
1966 else // empty menubar
1972 // the width is arbitrary, of course, for horizontal menubar
1978 int wxMenuBar::GetMenuFromPoint(const wxPoint
& pos
) const
1980 if ( pos
.x
< 0 || pos
.y
< 0 || pos
.y
> GetClientSize().y
)
1985 size_t count
= GetCount();
1986 for ( size_t item
= 0; item
< count
; item
++ )
1988 x
+= GetItemWidth(item
);
1996 // to the right of the last menu item
2000 // ----------------------------------------------------------------------------
2001 // wxMenuBar menu operations
2002 // ----------------------------------------------------------------------------
2004 void wxMenuBar::SelectMenu(size_t pos
)
2007 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::SelectMenu"));
2013 void wxMenuBar::DoSelectMenu(size_t pos
)
2015 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in DoSelectMenu") );
2017 int posOld
= m_current
;
2023 // close the previous menu
2024 if ( IsShowingMenu() )
2026 // restore m_shouldShowMenu flag after DismissMenu() which resets
2028 bool old
= m_shouldShowMenu
;
2032 m_shouldShowMenu
= old
;
2035 RefreshItem((size_t)posOld
);
2041 void wxMenuBar::PopupMenu(size_t pos
)
2043 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in PopupCurrentMenu") );
2050 // ----------------------------------------------------------------------------
2051 // wxMenuBar input handing
2052 // ----------------------------------------------------------------------------
2055 Note that wxMenuBar doesn't use wxInputHandler but handles keyboard and
2056 mouse in the same way under all platforms. This is because it doesn't derive
2057 from wxControl (which works with input handlers) but directly from wxWindow.
2059 Also, menu bar input handling is rather simple, so maybe it's not really
2060 worth making it themeable - at least I've decided against doing it now as it
2061 would merging the changes back into trunk more difficult. But it still could
2062 be done later if really needed.
2065 void wxMenuBar::OnKillFocus(wxFocusEvent
& event
)
2067 if ( m_current
!= -1 )
2069 RefreshItem((size_t)m_current
);
2077 void wxMenuBar::OnLeftDown(wxMouseEvent
& event
)
2085 else // we didn't have mouse capture, capture it now
2087 m_current
= GetMenuFromPoint(event
.GetPosition());
2088 if ( m_current
== -1 )
2090 // unfortunately, we can't prevent wxMSW from giving us the focus,
2091 // so we can only give it back
2096 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::OnLeftDown"));
2099 // show it as selected
2100 RefreshItem((size_t)m_current
);
2103 PopupCurrentMenu(false /* don't select first item - as Windows does */);
2108 void wxMenuBar::OnMouseMove(wxMouseEvent
& event
)
2112 (void)ProcessMouseEvent(event
.GetPosition());
2120 bool wxMenuBar::ProcessMouseEvent(const wxPoint
& pt
)
2122 // a hack to ignore the extra mouse events MSW sends us: this is similar to
2123 // wxUSE_MOUSEEVENT_HACK in wxWin itself but it isn't enough for us here as
2124 // we get the messages from different windows (old and new popup menus for
2127 static wxPoint s_ptLast
;
2128 if ( pt
== s_ptLast
)
2136 int currentNew
= GetMenuFromPoint(pt
);
2137 if ( (currentNew
== -1) || (currentNew
== m_current
) )
2142 // select the new active item
2143 DoSelectMenu(currentNew
);
2145 // show the menu if we know that we should, even if we hadn't been showing
2146 // it before (this may happen if the previous menu was disabled)
2147 if ( m_shouldShowMenu
&& !m_menuShown
)
2149 // open the new menu if the old one we closed had been opened
2150 PopupCurrentMenu(false /* don't select first item - as Windows does */);
2156 void wxMenuBar::OnKeyDown(wxKeyEvent
& event
)
2158 // ensure that we have a current item - we might not have it if we're
2159 // given the focus with Alt or F10 press (and under GTK+ the menubar
2160 // somehow gets the keyboard events even when it doesn't have focus...)
2161 if ( m_current
== -1 )
2163 if ( !HasCapture() )
2167 else // we do have capture
2169 // we always maintain a valid current item while we're in modal
2170 // state (i.e. have the capture)
2171 wxFAIL_MSG( _T("how did we manage to lose current item?") );
2177 int key
= event
.GetKeyCode();
2179 // first let the menu have it
2180 if ( IsShowingMenu() && m_menuShown
->ProcessKeyDown(key
) )
2185 // cycle through the menu items when left/right arrows are pressed and open
2186 // the menu when up/down one is
2190 // Alt must be processed at wxWindow level too
2195 // remove the selection and give the focus away
2196 if ( m_current
!= -1 )
2198 if ( IsShowingMenu() )
2210 size_t count
= GetCount();
2213 // the item won't change anyhow
2216 //else: otherwise, it will
2218 // remember if we were showing a menu - if we did, we should
2219 // show the new menu after changing the item
2220 bool wasMenuOpened
= IsShowingMenu();
2221 if ( wasMenuOpened
)
2226 // cast is safe as we tested for -1 above
2227 size_t currentNew
= (size_t)m_current
;
2229 if ( key
== WXK_LEFT
)
2231 if ( currentNew
-- == 0 )
2232 currentNew
= count
- 1;
2236 if ( ++currentNew
== count
)
2240 DoSelectMenu(currentNew
);
2242 if ( wasMenuOpened
)
2257 // letters open the corresponding menu
2260 int idxFound
= FindNextItemForAccel(m_current
, key
, &unique
);
2262 if ( idxFound
!= -1 )
2264 if ( IsShowingMenu() )
2269 DoSelectMenu((size_t)idxFound
);
2271 // if the item is not unique, just select it but don't
2272 // activate as the user might have wanted to activate
2275 // also, don't try to open a disabled menu
2276 if ( unique
&& IsEnabledTop((size_t)idxFound
) )
2282 // skip the "event.Skip()" below
2291 // ----------------------------------------------------------------------------
2292 // wxMenuBar accel handling
2293 // ----------------------------------------------------------------------------
2295 int wxMenuBar::FindNextItemForAccel(int idxStart
, int key
, bool *unique
) const
2297 if ( !wxIsalnum((wxChar
)key
) )
2299 // we only support letters/digits as accels
2303 // do we have more than one item with this accel?
2307 // translate everything to lower case before comparing
2308 wxChar chAccel
= (wxChar
)wxTolower(key
);
2310 // the index of the item with this accel
2313 // loop through all items searching for the item with this
2314 // accel starting at the item after the current one
2315 int count
= GetCount();
2316 int n
= idxStart
== -1 ? 0 : idxStart
+ 1;
2327 const wxMenuInfo
& info
= m_menuInfos
[n
];
2329 int idxAccel
= info
.GetAccelIndex();
2330 if ( idxAccel
!= -1 &&
2331 wxTolower(info
.GetLabel()[(size_t)idxAccel
])
2334 // ok, found an item with this accel
2335 if ( idxFound
== -1 )
2337 // store it but continue searching as we need to
2338 // know if it's the only item with this accel or if
2342 else // we already had found such item
2347 // no need to continue further, we won't find
2348 // anything we don't already know
2353 // we want to iterate over all items wrapping around if
2361 if ( n
== idxStart
)
2363 // we've seen all items
2373 bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent
& event
)
2376 for ( wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
2378 node
= node
->GetNext(), n
++ )
2380 // accels of the items in the disabled menus shouldn't work
2381 if ( m_menuInfos
[n
].IsEnabled() )
2383 if ( node
->GetData()->ProcessAccelEvent(event
) )
2385 // menu processed it
2395 #endif // wxUSE_ACCEL
2397 // ----------------------------------------------------------------------------
2398 // wxMenuBar menus showing
2399 // ----------------------------------------------------------------------------
2401 void wxMenuBar::PopupCurrentMenu(bool selectFirst
)
2403 wxCHECK_RET( m_current
!= -1, _T("no menu to popup") );
2405 // forgot to call DismissMenu()?
2406 wxASSERT_MSG( !m_menuShown
, _T("shouldn't show two menus at once!") );
2408 // in any case, we should show it - even if we won't
2409 m_shouldShowMenu
= true;
2411 if ( IsEnabledTop(m_current
) )
2413 // remember the menu we show
2414 m_menuShown
= GetMenu(m_current
);
2416 // we don't show the menu at all if it has no items
2417 if ( !m_menuShown
->IsEmpty() )
2419 // position it correctly: note that we must use screen coords and
2420 // that we pass 0 as width to position the menu exactly below the
2421 // item, not to the right of it
2422 wxRect rectItem
= GetItemRect(m_current
);
2424 m_menuShown
->SetInvokingWindow(m_frameLast
);
2426 m_menuShown
->Popup(ClientToScreen(rectItem
.GetPosition()),
2427 wxSize(0, rectItem
.GetHeight()),
2432 // reset it back as no menu is shown
2436 //else: don't show disabled menu
2439 void wxMenuBar::DismissMenu()
2441 wxCHECK_RET( m_menuShown
, _T("can't dismiss menu if none is shown") );
2443 m_menuShown
->Dismiss();
2447 void wxMenuBar::OnDismissMenu(bool dismissMenuBar
)
2449 m_shouldShowMenu
= false;
2451 if ( dismissMenuBar
)
2457 void wxMenuBar::OnDismiss()
2459 if ( ReleaseMouseCapture() )
2460 wxLogTrace(_T("mousecapture"), _T("Releasing mouse from wxMenuBar::OnDismiss"));
2462 if ( m_current
!= -1 )
2464 size_t current
= m_current
;
2467 RefreshItem(current
);
2473 bool wxMenuBar::ReleaseMouseCapture()
2476 // With wxX11, when a menu is closed by clicking away from it, a control
2477 // under the click will still get an event, even though the menu has the
2478 // capture (bug?). So that control may already have taken the capture by
2479 // this point, preventing us from releasing the menu's capture. So to work
2480 // around this, we release both captures, then put back the control's
2482 wxWindow
*capture
= GetCapture();
2485 capture
->ReleaseMouse();
2487 if ( capture
== this )
2490 bool had
= HasCapture();
2495 capture
->CaptureMouse();
2509 void wxMenuBar::GiveAwayFocus()
2511 GetFrame()->SetFocus();
2514 // ----------------------------------------------------------------------------
2515 // popup menu support
2516 // ----------------------------------------------------------------------------
2518 wxEventLoop
*wxWindow::ms_evtLoopPopup
= NULL
;
2520 bool wxWindow::DoPopupMenu(wxMenu
*menu
, int x
, int y
)
2522 wxCHECK_MSG( !ms_evtLoopPopup
, false,
2523 _T("can't show more than one popup menu at a time") );
2526 // we need to change the cursor before showing the menu as, apparently, no
2527 // cursor changes took place while the mouse is captured
2528 wxCursor cursorOld
= GetCursor();
2529 SetCursor(wxCURSOR_ARROW
);
2533 // flash any delayed log messages before showing the menu, otherwise it
2534 // could be dismissed (because it would lose focus) immediately after being
2536 wxLog::FlushActive();
2538 // some controls update themselves from OnIdle() call - let them do it
2539 wxTheApp
->ProcessIdle();
2541 // if the window hadn't been refreshed yet, the menu can adversely affect
2542 // its next OnPaint() handler execution - i.e. scrolled window refresh
2543 // logic breaks then as it scrolls part of the menu which hadn't been there
2544 // when the update event was generated into view
2548 menu
->SetInvokingWindow(this);
2550 // wxLogDebug( "Name of invoking window %s", menu->GetInvokingWindow()->GetName().c_str() );
2552 menu
->Popup(ClientToScreen(wxPoint(x
, y
)), wxSize(0,0));
2554 // this is not very useful if the menu was popped up because of the mouse
2555 // click but I think it is nice to do when it appears because of a key
2556 // press (i.e. Windows menu key)
2558 // Windows itself doesn't do it, but IMHO this is nice
2561 // we have to redirect all keyboard input to the menu temporarily
2562 PushEventHandler(new wxMenuKbdRedirector(menu
));
2564 // enter the local modal loop
2565 ms_evtLoopPopup
= new wxEventLoop
;
2566 ms_evtLoopPopup
->Run();
2568 delete ms_evtLoopPopup
;
2569 ms_evtLoopPopup
= NULL
;
2571 // remove the handler
2572 PopEventHandler(true /* delete it */);
2574 menu
->SetInvokingWindow(NULL
);
2577 SetCursor(cursorOld
);
2583 void wxWindow::DismissPopupMenu()
2585 wxCHECK_RET( ms_evtLoopPopup
, _T("no popup menu shown") );
2587 ms_evtLoopPopup
->Exit();
2590 #endif // wxUSE_MENUS