1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/menu.cpp
3 // Purpose: wxMenuItem, wxMenu and wxMenuBar implementation
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
28 #include "wx/stockitem.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 m_originalLabel
= 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 const wxString
& GetOriginalLabel() const { return m_originalLabel
; }
84 bool IsEnabled() const { return m_isEnabled
; }
85 wxCoord
GetWidth(wxMenuBar
*menubar
) const
89 wxConstCast(this, wxMenuInfo
)->CalcWidth(menubar
);
95 int GetAccelIndex() const { return m_indexAccel
; }
98 void CalcWidth(wxMenuBar
*menubar
)
101 wxClientDC
dc(menubar
);
102 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
103 dc
.GetTextExtent(m_label
, &size
.x
, &size
.y
);
105 // adjust for the renderer we use and store the width
106 m_width
= menubar
->GetRenderer()->GetMenuBarItemSize(size
).x
;
110 wxString m_originalLabel
;
116 #include "wx/arrimpl.cpp"
118 WX_DEFINE_OBJARRAY(wxMenuInfoArray
);
120 // ----------------------------------------------------------------------------
121 // wxPopupMenuWindow: a popup window showing a menu
122 // ----------------------------------------------------------------------------
124 class wxPopupMenuWindow
: public wxPopupTransientWindow
127 wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
);
129 virtual ~wxPopupMenuWindow();
131 // override the base class version to select the first item initially
132 virtual void Popup(wxWindow
*focus
= NULL
);
134 // override the base class version to dismiss any open submenus
135 virtual void Dismiss();
137 // called when a submenu is dismissed
138 void OnSubmenuDismiss(bool dismissParent
);
140 // the default wxMSW wxPopupTransientWindow::OnIdle disables the capture
141 // when the cursor is inside the popup, which dsables the menu tracking
142 // so override it to do nothing
144 void OnIdle(wxIdleEvent
& WXUNUSED(event
)) { }
147 // get the currently selected item (may be NULL)
148 wxMenuItem
*GetCurrentItem() const
150 return m_nodeCurrent
? m_nodeCurrent
->GetData() : NULL
;
153 // find the menu item at given position
154 wxMenuItemIter
GetMenuItemFromPoint(const wxPoint
& pt
) const;
156 // refresh the given item
157 void RefreshItem(wxMenuItem
*item
);
159 // preselect the first item
160 void SelectFirst() { SetCurrentItem(m_menu
->GetMenuItems().GetFirst()); }
162 // process the key event, return true if done
163 bool ProcessKeyDown(int key
);
165 // process mouse move event
166 void ProcessMouseMove(const wxPoint
& pt
);
168 // don't dismiss the popup window if the parent menu was clicked
169 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
172 // how did we perform this operation?
179 // notify the menu when the window disappears from screen
180 virtual void OnDismiss();
182 // draw the menu inside this window
183 virtual void DoDraw(wxControlRenderer
*renderer
);
186 void OnLeftUp(wxMouseEvent
& event
);
187 void OnMouseMove(wxMouseEvent
& event
);
188 void OnMouseLeave(wxMouseEvent
& event
);
189 void OnKeyDown(wxKeyEvent
& event
);
191 // reset the current item and node
194 // set the current node and item without refreshing anything
195 void SetCurrentItem(wxMenuItemIter node
);
197 // change the current item refreshing the old and new items
198 void ChangeCurrent(wxMenuItemIter node
);
200 // activate item, i.e. call either ClickItem() or OpenSubmenu() depending
201 // on what it is, return true if something was done (i.e. it's not a
203 bool ActivateItem(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
205 // send the event about the item click
206 void ClickItem(wxMenuItem
*item
);
208 // show the submenu for this item
209 void OpenSubmenu(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
211 // can this tiem be opened?
212 bool CanOpen(wxMenuItem
*item
)
214 return item
&& item
->IsEnabled() && item
->IsSubMenu();
217 // dismiss the menu and all parent menus too
218 void DismissAndNotify();
220 // react to dimissing this menu and also dismiss the parent if
222 void HandleDismiss(bool dismissParent
);
224 // do we have an open submenu?
225 bool HasOpenSubmenu() const { return m_hasOpenSubMenu
; }
227 // get previous node after the current one
228 wxMenuItemIter
GetPrevNode() const;
230 // get previous node before the given one, wrapping if it's the first one
231 wxMenuItemIter
GetPrevNode(wxMenuItemIter node
) const;
233 // get next node after the current one
234 wxMenuItemIter
GetNextNode() const;
236 // get next node after the given one, wrapping if it's the last one
237 wxMenuItemIter
GetNextNode(wxMenuItemIter node
) const;
243 // the menu node corresponding to the current item
244 wxMenuItemIter m_nodeCurrent
;
246 // do we currently have an opened submenu?
247 bool m_hasOpenSubMenu
;
249 DECLARE_EVENT_TABLE()
252 // ----------------------------------------------------------------------------
253 // wxMenuKbdRedirector: an event handler which redirects kbd input to wxMenu
254 // ----------------------------------------------------------------------------
256 class wxMenuKbdRedirector
: public wxEvtHandler
259 wxMenuKbdRedirector(wxMenu
*menu
) { m_menu
= menu
; }
261 virtual bool ProcessEvent(wxEvent
& event
)
263 if ( event
.GetEventType() == wxEVT_KEY_DOWN
)
265 return m_menu
->ProcessKeyDown(((wxKeyEvent
&)event
).GetKeyCode());
271 return wxEvtHandler::ProcessEvent(event
);
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
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 ( !m_nodeCurrent
|| !node
|| (node
!= m_nodeCurrent
) )
351 wxMenuItemIter nodeOldCurrent
= m_nodeCurrent
;
353 m_nodeCurrent
= node
;
355 if ( nodeOldCurrent
)
357 wxMenuItem
*item
= nodeOldCurrent
->GetData();
358 wxCHECK_RET( item
, wxT("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 wxT("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
, wxT("parent menu is not shown?") );
451 if ( !::SetWindowPos(GetHwndOf(win
), GetHwnd(),
453 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOREDRAW
) )
455 wxLogLastError(wxT("SetWindowPos(HWND_TOP)"));
463 void wxPopupMenuWindow::Dismiss()
465 if ( HasOpenSubmenu() )
467 wxMenuItem
*item
= GetCurrentItem();
468 wxCHECK_RET( item
&& item
->IsSubMenu(), wxT("where is our open submenu?") );
470 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
471 wxCHECK_RET( win
, wxT("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
, wxT("can't refresh NULL item") );
542 wxASSERT_MSG( IsShown(), wxT("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());
617 item
->GetItemLabelText(),
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
, wxT("can't click NULL item") );
637 wxASSERT_MSG( !item
->IsSeparator() && !item
->IsSubMenu(),
638 wxT("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
, wxT("can't open NULL submenu") );
652 wxMenu
*submenu
= item
->GetSubMenu();
653 wxCHECK_RET( submenu
, wxT("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, wxT("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 ( !m_nodeCurrent
|| (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
, wxT("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
), wxT("where is our open submenu?") );
834 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
835 wxCHECK_RET( win
, wxT("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 wxT("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 (wxChar
)wxTolower(item
->GetItemLabelText()[(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( wxT("can't get geometry without window") );
1092 void wxMenu::InvalidateGeometryInfo()
1094 wxDELETE(m_geometry
);
1097 // ----------------------------------------------------------------------------
1098 // wxMenu adding/removing items
1099 // ----------------------------------------------------------------------------
1101 void wxMenu::OnItemAdded(wxMenuItem
*item
)
1103 InvalidateGeometryInfo();
1107 #endif // wxUSE_ACCEL
1110 void wxMenu::EndRadioGroup()
1112 // we're not inside a radio group any longer
1113 m_startRadioGroup
= -1;
1116 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
1118 if ( item
->GetKind() == wxITEM_RADIO
)
1120 int count
= GetMenuItemCount();
1122 if ( m_startRadioGroup
== -1 )
1124 // start a new radio group
1125 m_startRadioGroup
= count
;
1127 // for now it has just one element
1128 item
->SetAsRadioGroupStart();
1129 item
->SetRadioGroupEnd(m_startRadioGroup
);
1131 else // extend the current radio group
1133 // we need to update its end item
1134 item
->SetRadioGroupStart(m_startRadioGroup
);
1135 wxMenuItemIter node
= GetMenuItems().Item(m_startRadioGroup
);
1139 node
->GetData()->SetRadioGroupEnd(count
);
1143 wxFAIL_MSG( wxT("where is the radio group start item?") );
1147 else // not a radio item
1152 if ( !wxMenuBase::DoAppend(item
) )
1160 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1162 if ( !wxMenuBase::DoInsert(pos
, item
) )
1170 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1172 wxMenuItem
*itemOld
= wxMenuBase::DoRemove(item
);
1176 InvalidateGeometryInfo();
1179 RemoveAccelFor(item
);
1180 #endif // wxUSE_ACCEL
1186 // ----------------------------------------------------------------------------
1187 // wxMenu attaching/detaching
1188 // ----------------------------------------------------------------------------
1190 void wxMenu::Attach(wxMenuBarBase
*menubar
)
1192 wxMenuBase::Attach(menubar
);
1194 wxCHECK_RET( m_menuBar
, wxT("menubar can't be NULL after attaching") );
1196 // unfortunately, we can't use m_menuBar->GetEventHandler() here because,
1197 // if the menubar is currently showing a menu, its event handler is a
1198 // temporary one installed by wxPopupWindow and so will disappear soon any
1199 // any attempts to use it from the newly attached menu would result in a
1202 // so we use the menubar itself, even if it's a pity as it means we can't
1203 // redirect all menu events by changing the menubar handler (FIXME)
1204 SetNextHandler(m_menuBar
);
1207 void wxMenu::Detach()
1209 // After the menu is detached from the menu bar, it shouldn't send its
1211 SetNextHandler(NULL
);
1213 wxMenuBase::Detach();
1216 // ----------------------------------------------------------------------------
1217 // wxMenu misc functions
1218 // ----------------------------------------------------------------------------
1220 wxWindow
*wxMenu::GetRootWindow() const
1222 return GetMenuBar() ? GetMenuBar() : GetInvokingWindow();
1225 wxRenderer
*wxMenu::GetRenderer() const
1227 // we're going to crash without renderer!
1228 wxCHECK_MSG( m_popupMenu
, NULL
, wxT("neither popup nor menubar menu?") );
1230 return m_popupMenu
->GetRenderer();
1233 void wxMenu::RefreshItem(wxMenuItem
*item
)
1235 // the item geometry changed, so our might have changed as well
1236 InvalidateGeometryInfo();
1240 // this would be a bug in IsShown()
1241 wxCHECK_RET( m_popupMenu
, wxT("must have popup window if shown!") );
1243 // recalc geometry to update the item height and such
1244 (void)GetGeometryInfo();
1246 m_popupMenu
->RefreshItem(item
);
1250 // ----------------------------------------------------------------------------
1251 // wxMenu showing and hiding
1252 // ----------------------------------------------------------------------------
1254 bool wxMenu::IsShown() const
1256 return m_popupMenu
&& m_popupMenu
->IsShown();
1259 void wxMenu::OnDismiss(bool dismissParent
)
1263 // always notify the parent about submenu disappearance
1264 wxPopupMenuWindow
*win
= m_menuParent
->m_popupMenu
;
1267 win
->OnSubmenuDismiss( true );
1271 wxFAIL_MSG( wxT("parent menu not shown?") );
1274 // and if we dismiss everything, propagate to parent
1275 if ( dismissParent
)
1277 // dismissParent is recursive
1278 m_menuParent
->Dismiss();
1279 m_menuParent
->OnDismiss(true);
1282 else // no parent menu
1284 // notify the menu bar if we're a top level menu
1287 m_menuBar
->OnDismissMenu(dismissParent
);
1291 wxWindow
* const win
= GetInvokingWindow();
1292 wxCHECK_RET( win
, wxT("what kind of menu is this?") );
1294 win
->DismissPopupMenu();
1299 void wxMenu::Popup(const wxPoint
& pos
, const wxSize
& size
, bool selectFirst
)
1301 // create the popup window if not done yet
1304 m_popupMenu
= new wxPopupMenuWindow(GetRootWindow(), this);
1307 // select the first item unless disabled
1310 m_popupMenu
->SelectFirst();
1313 // the geometry might have changed since the last time we were shown, so
1315 m_popupMenu
->SetClientSize(GetGeometryInfo().GetSize());
1317 // position it as specified
1318 m_popupMenu
->Position(pos
, size
);
1320 // the menu can't have the focus itself (it is a Windows limitation), so
1321 // always keep the focus at the originating window
1322 wxWindow
*focus
= GetRootWindow();
1324 wxASSERT_MSG( focus
, wxT("no window to keep focus on?") );
1327 m_popupMenu
->Popup(focus
);
1330 void wxMenu::Dismiss()
1332 wxCHECK_RET( IsShown(), wxT("can't dismiss hidden menu") );
1334 m_popupMenu
->Dismiss();
1337 // ----------------------------------------------------------------------------
1338 // wxMenu event processing
1339 // ----------------------------------------------------------------------------
1341 bool wxMenu::ProcessKeyDown(int key
)
1343 wxCHECK_MSG( m_popupMenu
, false,
1344 wxT("can't process key events if not shown") );
1346 return m_popupMenu
->ProcessKeyDown(key
);
1349 bool wxMenu::ClickItem(wxMenuItem
*item
)
1352 if ( item
->IsCheckable() )
1354 // update the item state
1355 isChecked
= !item
->IsChecked();
1357 item
->Check(isChecked
!= 0);
1365 return SendEvent(item
->GetId(), isChecked
);
1368 // ----------------------------------------------------------------------------
1369 // wxMenu accel support
1370 // ----------------------------------------------------------------------------
1374 bool wxMenu::ProcessAccelEvent(const wxKeyEvent
& event
)
1376 // do we have an item for this accel?
1377 wxMenuItem
*item
= m_accelTable
.GetMenuItem(event
);
1378 if ( item
&& item
->IsEnabled() )
1380 return ClickItem(item
);
1384 for ( wxMenuItemIter node
= GetMenuItems().GetFirst();
1386 node
= node
->GetNext() )
1388 const wxMenuItem
*item
= node
->GetData();
1389 if ( item
->IsSubMenu() && item
->IsEnabled() )
1392 if ( item
->GetSubMenu()->ProcessAccelEvent(event
) )
1402 void wxMenu::AddAccelFor(wxMenuItem
*item
)
1404 wxAcceleratorEntry
*accel
= item
->GetAccel();
1407 accel
->SetMenuItem(item
);
1409 m_accelTable
.Add(*accel
);
1415 void wxMenu::RemoveAccelFor(wxMenuItem
*item
)
1417 wxAcceleratorEntry
*accel
= item
->GetAccel();
1420 m_accelTable
.Remove(*accel
);
1426 #endif // wxUSE_ACCEL
1428 // ----------------------------------------------------------------------------
1429 // wxMenuItem construction
1430 // ----------------------------------------------------------------------------
1432 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
1434 const wxString
& text
,
1435 const wxString
& help
,
1438 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
1441 m_height
= wxDefaultCoord
;
1443 m_radioGroup
.start
= -1;
1444 m_isRadioGroupStart
= false;
1446 m_bmpDisabled
= wxNullBitmap
;
1451 wxMenuItem::~wxMenuItem()
1455 // ----------------------------------------------------------------------------
1456 // wxMenuItemBase methods implemented here
1457 // ----------------------------------------------------------------------------
1460 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
1462 const wxString
& name
,
1463 const wxString
& help
,
1467 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
1470 // ----------------------------------------------------------------------------
1471 // wxMenuItem operations
1472 // ----------------------------------------------------------------------------
1474 void wxMenuItem::NotifyMenu()
1476 m_parentMenu
->RefreshItem(this);
1479 void wxMenuItem::UpdateAccelInfo()
1481 m_indexAccel
= wxControl::FindAccelIndex(m_text
);
1483 // will be empty if the text contains no TABs - ok
1484 m_strAccel
= m_text
.AfterFirst(wxT('\t'));
1487 void wxMenuItem::SetItemLabel(const wxString
& text
)
1489 if ( text
!= m_text
)
1491 // first call the base class version to change m_text
1492 // (and also check if we don't have a stock menu item)
1493 wxMenuItemBase::SetItemLabel(text
);
1501 void wxMenuItem::SetCheckable(bool checkable
)
1503 if ( checkable
!= IsCheckable() )
1505 wxMenuItemBase::SetCheckable(checkable
);
1511 void wxMenuItem::SetBitmaps(const wxBitmap
& bmpChecked
,
1512 const wxBitmap
& bmpUnchecked
)
1514 m_bmpChecked
= bmpChecked
;
1515 m_bmpUnchecked
= bmpUnchecked
;
1520 void wxMenuItem::Enable(bool enable
)
1522 if ( enable
!= m_isEnabled
)
1524 wxMenuItemBase::Enable(enable
);
1530 void wxMenuItem::Check(bool check
)
1532 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
1534 if ( m_isChecked
== check
)
1537 if ( GetKind() == wxITEM_RADIO
)
1539 // it doesn't make sense to uncheck a radio item - what would this do?
1543 // get the index of this item in the menu
1544 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
1545 int pos
= items
.IndexOf(this);
1546 wxCHECK_RET( pos
!= wxNOT_FOUND
,
1547 wxT("menuitem not found in the menu items list?") );
1549 // get the radio group range
1553 if ( m_isRadioGroupStart
)
1555 // we already have all information we need
1557 end
= m_radioGroup
.end
;
1559 else // next radio group item
1561 // get the radio group end from the start item
1562 start
= m_radioGroup
.start
;
1563 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
1566 // also uncheck all the other items in this radio group
1567 wxMenuItemIter node
= items
.Item(start
);
1568 for ( int n
= start
; n
<= end
&& node
; n
++ )
1572 node
->GetData()->m_isChecked
= false;
1574 node
= node
->GetNext();
1578 wxMenuItemBase::Check(check
);
1583 // radio group stuff
1584 // -----------------
1586 void wxMenuItem::SetAsRadioGroupStart()
1588 m_isRadioGroupStart
= true;
1591 void wxMenuItem::SetRadioGroupStart(int start
)
1593 wxASSERT_MSG( !m_isRadioGroupStart
,
1594 wxT("should only be called for the next radio items") );
1596 m_radioGroup
.start
= start
;
1599 void wxMenuItem::SetRadioGroupEnd(int end
)
1601 wxASSERT_MSG( m_isRadioGroupStart
,
1602 wxT("should only be called for the first radio item") );
1604 m_radioGroup
.end
= end
;
1607 // ----------------------------------------------------------------------------
1608 // wxMenuBar creation
1609 // ----------------------------------------------------------------------------
1611 void wxMenuBar::Init()
1619 m_shouldShowMenu
= false;
1622 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
1626 for (size_t i
= 0; i
< n
; ++i
)
1627 Append(menus
[i
], titles
[i
]);
1630 void wxMenuBar::Attach(wxFrame
*frame
)
1632 // maybe you really wanted to call Detach()?
1633 wxCHECK_RET( frame
, wxT("wxMenuBar::Attach(NULL) called") );
1635 wxMenuBarBase::Attach(frame
);
1639 // reparent if necessary
1640 if ( m_frameLast
!= frame
)
1645 // show it back - was hidden by Detach()
1648 else // not created yet, do it now
1650 // we have no way to return the error from here anyhow :-(
1651 (void)Create(frame
, wxID_ANY
);
1653 SetCursor(wxCURSOR_ARROW
);
1655 SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT
));
1657 // calculate and set our height (it won't be changed any more)
1658 SetSize(wxDefaultCoord
, GetBestSize().y
);
1661 // remember the last frame which had us to avoid unnecessarily reparenting
1663 m_frameLast
= frame
;
1666 void wxMenuBar::Detach()
1668 // don't delete the window because we may be reattached later, just hide it
1674 wxMenuBarBase::Detach();
1677 wxMenuBar::~wxMenuBar()
1681 // ----------------------------------------------------------------------------
1682 // wxMenuBar adding/removing items
1683 // ----------------------------------------------------------------------------
1685 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1687 return Insert(GetCount(), menu
, title
);
1690 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1692 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1695 wxMenuInfo
*info
= new wxMenuInfo(title
);
1696 m_menuInfos
.Insert(info
, pos
);
1698 RefreshAllItemsAfter(pos
);
1703 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1705 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1709 wxMenuInfo
& info
= m_menuInfos
[pos
];
1711 info
.SetLabel(title
);
1713 // even if the old menu was disabled, the new one is not any more
1716 // even if we change only this one, the new label has different width,
1717 // so we need to refresh everything beyond this item as well
1718 RefreshAllItemsAfter(pos
);
1724 wxMenu
*wxMenuBar::Remove(size_t pos
)
1726 wxMenu
*menuOld
= wxMenuBarBase::Remove(pos
);
1730 m_menuInfos
.RemoveAt(pos
);
1732 // this doesn't happen too often, so don't try to be too smart - just
1733 // refresh everything
1740 // ----------------------------------------------------------------------------
1741 // wxMenuBar top level menus access
1742 // ----------------------------------------------------------------------------
1744 wxCoord
wxMenuBar::GetItemWidth(size_t pos
) const
1746 return m_menuInfos
[pos
].GetWidth(wxConstCast(this, wxMenuBar
));
1749 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1751 wxCHECK_RET( pos
< GetCount(), wxT("invalid index in EnableTop") );
1753 if ( enable
!= m_menuInfos
[pos
].IsEnabled() )
1755 m_menuInfos
[pos
].SetEnabled(enable
);
1759 //else: nothing to do
1762 bool wxMenuBar::IsEnabledTop(size_t pos
) const
1764 wxCHECK_MSG( pos
< GetCount(), false, wxT("invalid index in IsEnabledTop") );
1766 return m_menuInfos
[pos
].IsEnabled();
1769 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1771 wxCHECK_RET( pos
< GetCount(), wxT("invalid index in SetMenuLabel") );
1773 if ( label
!= m_menuInfos
[pos
].GetOriginalLabel() )
1775 m_menuInfos
[pos
].SetLabel(label
);
1779 //else: nothing to do
1782 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1784 wxCHECK_MSG( pos
< GetCount(), wxEmptyString
, wxT("invalid index in GetMenuLabel") );
1786 return m_menuInfos
[pos
].GetOriginalLabel();
1789 // ----------------------------------------------------------------------------
1790 // wxMenuBar drawing
1791 // ----------------------------------------------------------------------------
1793 void wxMenuBar::RefreshAllItemsAfter(size_t pos
)
1797 // no need to refresh if nothing is shown yet
1801 wxRect rect
= GetItemRect(pos
);
1802 rect
.width
= GetClientSize().x
- rect
.x
;
1806 void wxMenuBar::RefreshItem(size_t pos
)
1808 wxCHECK_RET( pos
!= (size_t)-1,
1809 wxT("invalid item in wxMenuBar::RefreshItem") );
1813 // no need to refresh if nothing is shown yet
1817 RefreshRect(GetItemRect(pos
));
1820 void wxMenuBar::DoDraw(wxControlRenderer
*renderer
)
1822 wxDC
& dc
= renderer
->GetDC();
1823 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1825 // redraw only the items which must be redrawn
1827 // we don't have to use GetUpdateClientRect() here because our client rect
1828 // is the same as total one
1829 wxRect rectUpdate
= GetUpdateRegion().GetBox();
1831 int flagsMenubar
= GetStateFlags();
1835 rect
.height
= GetClientSize().y
;
1838 size_t count
= GetCount();
1839 for ( size_t n
= 0; n
< count
; n
++ )
1841 if ( x
> rectUpdate
.GetRight() )
1843 // all remaining items are to the right of rectUpdate
1848 rect
.width
= GetItemWidth(n
);
1850 if ( x
< rectUpdate
.x
)
1852 // this item is still to the left of rectUpdate
1856 int flags
= flagsMenubar
;
1857 if ( m_current
!= -1 && n
== (size_t)m_current
)
1859 flags
|= wxCONTROL_SELECTED
;
1862 if ( !IsEnabledTop(n
) )
1864 flags
|= wxCONTROL_DISABLED
;
1867 GetRenderer()->DrawMenuBarItem
1871 m_menuInfos
[n
].GetLabel(),
1873 m_menuInfos
[n
].GetAccelIndex()
1878 // ----------------------------------------------------------------------------
1879 // wxMenuBar geometry
1880 // ----------------------------------------------------------------------------
1882 wxRect
wxMenuBar::GetItemRect(size_t pos
) const
1884 wxASSERT_MSG( pos
< GetCount(), wxT("invalid menu bar item index") );
1885 wxASSERT_MSG( IsCreated(), wxT("can't call this method yet") );
1890 rect
.height
= GetClientSize().y
;
1892 for ( size_t n
= 0; n
< pos
; n
++ )
1894 rect
.x
+= GetItemWidth(n
);
1897 rect
.width
= GetItemWidth(pos
);
1902 wxSize
wxMenuBar::DoGetBestClientSize() const
1905 if ( GetMenuCount() > 0 )
1907 wxClientDC
dc(wxConstCast(this, wxMenuBar
));
1908 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1909 dc
.GetTextExtent(GetMenuLabel(0), &size
.x
, &size
.y
);
1911 // adjust for the renderer we use
1912 size
= GetRenderer()->GetMenuBarItemSize(size
);
1914 else // empty menubar
1920 // the width is arbitrary, of course, for horizontal menubar
1926 int wxMenuBar::GetMenuFromPoint(const wxPoint
& pos
) const
1928 if ( pos
.x
< 0 || pos
.y
< 0 || pos
.y
> GetClientSize().y
)
1933 size_t count
= GetCount();
1934 for ( size_t item
= 0; item
< count
; item
++ )
1936 x
+= GetItemWidth(item
);
1944 // to the right of the last menu item
1948 // ----------------------------------------------------------------------------
1949 // wxMenuBar menu operations
1950 // ----------------------------------------------------------------------------
1952 void wxMenuBar::SelectMenu(size_t pos
)
1955 wxLogTrace(wxT("mousecapture"), wxT("Capturing mouse from wxMenuBar::SelectMenu"));
1961 void wxMenuBar::DoSelectMenu(size_t pos
)
1963 wxCHECK_RET( pos
< GetCount(), wxT("invalid menu index in DoSelectMenu") );
1965 int posOld
= m_current
;
1971 // close the previous menu
1972 if ( IsShowingMenu() )
1974 // restore m_shouldShowMenu flag after DismissMenu() which resets
1976 bool old
= m_shouldShowMenu
;
1980 m_shouldShowMenu
= old
;
1983 RefreshItem((size_t)posOld
);
1989 void wxMenuBar::PopupMenu(size_t pos
)
1991 wxCHECK_RET( pos
< GetCount(), wxT("invalid menu index in PopupCurrentMenu") );
1998 // ----------------------------------------------------------------------------
1999 // wxMenuBar input handing
2000 // ----------------------------------------------------------------------------
2003 Note that wxMenuBar doesn't use wxInputHandler but handles keyboard and
2004 mouse in the same way under all platforms. This is because it doesn't derive
2005 from wxControl (which works with input handlers) but directly from wxWindow.
2007 Also, menu bar input handling is rather simple, so maybe it's not really
2008 worth making it themeable - at least I've decided against doing it now as it
2009 would merging the changes back into trunk more difficult. But it still could
2010 be done later if really needed.
2013 void wxMenuBar::OnKillFocus(wxFocusEvent
& event
)
2015 if ( m_current
!= -1 )
2017 RefreshItem((size_t)m_current
);
2025 void wxMenuBar::OnLeftDown(wxMouseEvent
& event
)
2033 else // we didn't have mouse capture, capture it now
2035 m_current
= GetMenuFromPoint(event
.GetPosition());
2036 if ( m_current
== -1 )
2038 // unfortunately, we can't prevent wxMSW from giving us the focus,
2039 // so we can only give it back
2044 wxLogTrace(wxT("mousecapture"), wxT("Capturing mouse from wxMenuBar::OnLeftDown"));
2047 // show it as selected
2048 RefreshItem((size_t)m_current
);
2051 PopupCurrentMenu(false /* don't select first item - as Windows does */);
2056 void wxMenuBar::OnMouseMove(wxMouseEvent
& event
)
2060 (void)ProcessMouseEvent(event
.GetPosition());
2068 bool wxMenuBar::ProcessMouseEvent(const wxPoint
& pt
)
2070 // a hack to ignore the extra mouse events MSW sends us: this is similar to
2071 // wxUSE_MOUSEEVENT_HACK in wxWin itself but it isn't enough for us here as
2072 // we get the messages from different windows (old and new popup menus for
2075 static wxPoint s_ptLast
;
2076 if ( pt
== s_ptLast
)
2084 int currentNew
= GetMenuFromPoint(pt
);
2085 if ( (currentNew
== -1) || (currentNew
== m_current
) )
2090 // select the new active item
2091 DoSelectMenu(currentNew
);
2093 // show the menu if we know that we should, even if we hadn't been showing
2094 // it before (this may happen if the previous menu was disabled)
2095 if ( m_shouldShowMenu
&& !m_menuShown
)
2097 // open the new menu if the old one we closed had been opened
2098 PopupCurrentMenu(false /* don't select first item - as Windows does */);
2104 void wxMenuBar::OnKeyDown(wxKeyEvent
& event
)
2106 // ensure that we have a current item - we might not have it if we're
2107 // given the focus with Alt or F10 press (and under GTK+ the menubar
2108 // somehow gets the keyboard events even when it doesn't have focus...)
2109 if ( m_current
== -1 )
2111 if ( !HasCapture() )
2115 else // we do have capture
2117 // we always maintain a valid current item while we're in modal
2118 // state (i.e. have the capture)
2119 wxFAIL_MSG( wxT("how did we manage to lose current item?") );
2125 int key
= event
.GetKeyCode();
2127 // first let the menu have it
2128 if ( IsShowingMenu() && m_menuShown
->ProcessKeyDown(key
) )
2133 // cycle through the menu items when left/right arrows are pressed and open
2134 // the menu when up/down one is
2138 // Alt must be processed at wxWindow level too
2143 // remove the selection and give the focus away
2144 if ( m_current
!= -1 )
2146 if ( IsShowingMenu() )
2158 size_t count
= GetCount();
2161 // the item won't change anyhow
2164 //else: otherwise, it will
2166 // remember if we were showing a menu - if we did, we should
2167 // show the new menu after changing the item
2168 bool wasMenuOpened
= IsShowingMenu();
2169 if ( wasMenuOpened
)
2174 // cast is safe as we tested for -1 above
2175 size_t currentNew
= (size_t)m_current
;
2177 if ( key
== WXK_LEFT
)
2179 if ( currentNew
-- == 0 )
2180 currentNew
= count
- 1;
2184 if ( ++currentNew
== count
)
2188 DoSelectMenu(currentNew
);
2190 if ( wasMenuOpened
)
2205 // letters open the corresponding menu
2208 int idxFound
= FindNextItemForAccel(m_current
, key
, &unique
);
2210 if ( idxFound
!= -1 )
2212 if ( IsShowingMenu() )
2217 DoSelectMenu((size_t)idxFound
);
2219 // if the item is not unique, just select it but don't
2220 // activate as the user might have wanted to activate
2223 // also, don't try to open a disabled menu
2224 if ( unique
&& IsEnabledTop((size_t)idxFound
) )
2230 // skip the "event.Skip()" below
2239 // ----------------------------------------------------------------------------
2240 // wxMenuBar accel handling
2241 // ----------------------------------------------------------------------------
2243 int wxMenuBar::FindNextItemForAccel(int idxStart
, int key
, bool *unique
) const
2245 if ( !wxIsalnum((wxChar
)key
) )
2247 // we only support letters/digits as accels
2251 // do we have more than one item with this accel?
2255 // translate everything to lower case before comparing
2256 wxChar chAccel
= (wxChar
)wxTolower(key
);
2258 // the index of the item with this accel
2261 // loop through all items searching for the item with this
2262 // accel starting at the item after the current one
2263 int count
= GetCount();
2264 int n
= idxStart
== -1 ? 0 : idxStart
+ 1;
2275 const wxMenuInfo
& info
= m_menuInfos
[n
];
2277 int idxAccel
= info
.GetAccelIndex();
2278 if ( idxAccel
!= -1 &&
2279 (wxChar
)wxTolower(info
.GetLabel()[(size_t)idxAccel
]) == chAccel
)
2281 // ok, found an item with this accel
2282 if ( idxFound
== -1 )
2284 // store it but continue searching as we need to
2285 // know if it's the only item with this accel or if
2289 else // we already had found such item
2294 // no need to continue further, we won't find
2295 // anything we don't already know
2300 // we want to iterate over all items wrapping around if
2308 if ( n
== idxStart
)
2310 // we've seen all items
2320 bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent
& event
)
2323 for ( wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
2325 node
= node
->GetNext(), n
++ )
2327 // accels of the items in the disabled menus shouldn't work
2328 if ( m_menuInfos
[n
].IsEnabled() )
2330 if ( node
->GetData()->ProcessAccelEvent(event
) )
2332 // menu processed it
2342 #endif // wxUSE_ACCEL
2344 // ----------------------------------------------------------------------------
2345 // wxMenuBar menus showing
2346 // ----------------------------------------------------------------------------
2348 void wxMenuBar::PopupCurrentMenu(bool selectFirst
)
2350 wxCHECK_RET( m_current
!= -1, wxT("no menu to popup") );
2352 // forgot to call DismissMenu()?
2353 wxASSERT_MSG( !m_menuShown
, wxT("shouldn't show two menus at once!") );
2355 // in any case, we should show it - even if we won't
2356 m_shouldShowMenu
= true;
2358 if ( IsEnabledTop(m_current
) )
2360 // remember the menu we show
2361 m_menuShown
= GetMenu(m_current
);
2363 // we don't show the menu at all if it has no items
2364 if ( !m_menuShown
->IsEmpty() )
2366 // position it correctly: note that we must use screen coords and
2367 // that we pass 0 as width to position the menu exactly below the
2368 // item, not to the right of it
2369 wxRect rectItem
= GetItemRect(m_current
);
2371 m_menuShown
->Popup(ClientToScreen(rectItem
.GetPosition()),
2372 wxSize(0, rectItem
.GetHeight()),
2377 // reset it back as no menu is shown
2381 //else: don't show disabled menu
2384 void wxMenuBar::DismissMenu()
2386 wxCHECK_RET( m_menuShown
, wxT("can't dismiss menu if none is shown") );
2388 m_menuShown
->Dismiss();
2392 void wxMenuBar::OnDismissMenu(bool dismissMenuBar
)
2394 m_shouldShowMenu
= false;
2396 if ( dismissMenuBar
)
2402 void wxMenuBar::OnDismiss()
2404 if ( ReleaseMouseCapture() )
2406 wxLogTrace(wxT("mousecapture"), wxT("Releasing mouse from wxMenuBar::OnDismiss"));
2409 if ( m_current
!= -1 )
2411 size_t current
= m_current
;
2414 RefreshItem(current
);
2420 bool wxMenuBar::ReleaseMouseCapture()
2423 // With wxX11, when a menu is closed by clicking away from it, a control
2424 // under the click will still get an event, even though the menu has the
2425 // capture (bug?). So that control may already have taken the capture by
2426 // this point, preventing us from releasing the menu's capture. So to work
2427 // around this, we release both captures, then put back the control's
2429 wxWindow
*capture
= GetCapture();
2432 capture
->ReleaseMouse();
2434 if ( capture
== this )
2437 bool had
= HasCapture();
2442 capture
->CaptureMouse();
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 wxT("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
->Popup(ClientToScreen(wxPoint(x
, y
)), wxSize(0,0));
2497 // this is not very useful if the menu was popped up because of the mouse
2498 // click but I think it is nice to do when it appears because of a key
2499 // press (i.e. Windows menu key)
2501 // Windows itself doesn't do it, but IMHO this is nice
2504 // we have to redirect all keyboard input to the menu temporarily
2505 PushEventHandler(new wxMenuKbdRedirector(menu
));
2507 // enter the local modal loop
2508 ms_evtLoopPopup
= new wxEventLoop
;
2509 ms_evtLoopPopup
->Run();
2511 wxDELETE(ms_evtLoopPopup
);
2513 // remove the handler
2514 PopEventHandler(true /* delete it */);
2517 SetCursor(cursorOld
);
2523 void wxWindow::DismissPopupMenu()
2525 wxCHECK_RET( ms_evtLoopPopup
, wxT("no popup menu shown") );
2527 ms_evtLoopPopup
->Exit();
2530 #endif // wxUSE_MENUS