1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenuItem, wxMenu and wxMenuBar implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "univmenuitem.h"
22 #pragma implementation "univmenu.h"
25 #include "wx/wxprec.h"
32 #include "wx/dynarray.h"
33 #include "wx/control.h" // for FindAccelIndex()
35 #include "wx/settings.h"
42 #include "wx/popupwin.h"
43 #include "wx/evtloop.h"
44 #include "wx/dcclient.h"
47 #include "wx/univ/renderer.h"
50 #include "wx/msw/private.h"
53 // ----------------------------------------------------------------------------
54 // wxMenuInfo contains all extra information about top level menus we need
55 // ----------------------------------------------------------------------------
57 class WXDLLEXPORT wxMenuInfo
61 wxMenuInfo(const wxString
& text
)
69 void SetLabel(const wxString
& text
)
71 // remember the accel char (may be -1 if none)
72 m_indexAccel
= wxControl::FindAccelIndex(text
, &m_label
);
74 // calculate the width later, after the menu bar is created
78 void SetEnabled(bool enabled
= true) { m_isEnabled
= enabled
; }
82 const wxString
& GetLabel() const { return m_label
; }
83 bool IsEnabled() const { return m_isEnabled
; }
84 wxCoord
GetWidth(wxMenuBar
*menubar
) const
88 wxConstCast(this, wxMenuInfo
)->CalcWidth(menubar
);
94 int GetAccelIndex() const { return m_indexAccel
; }
97 void CalcWidth(wxMenuBar
*menubar
)
100 wxClientDC
dc(menubar
);
101 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
102 dc
.GetTextExtent(m_label
, &size
.x
, &size
.y
);
104 // adjust for the renderer we use and store the width
105 m_width
= menubar
->GetRenderer()->GetMenuBarItemSize(size
).x
;
114 #include "wx/arrimpl.cpp"
116 WX_DEFINE_OBJARRAY(wxMenuInfoArray
);
118 // ----------------------------------------------------------------------------
119 // wxPopupMenuWindow: a popup window showing a menu
120 // ----------------------------------------------------------------------------
122 class wxPopupMenuWindow
: public wxPopupTransientWindow
125 wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
);
127 ~wxPopupMenuWindow();
129 // override the base class version to select the first item initially
130 virtual void Popup(wxWindow
*focus
= NULL
);
132 // override the base class version to dismiss any open submenus
133 virtual void Dismiss();
135 // notify the menu when the window disappears from screen
136 virtual void OnDismiss();
138 // called when a submenu is dismissed
139 void OnSubmenuDismiss(bool dismissParent
);
141 // get the currently selected item (may be NULL)
142 wxMenuItem
*GetCurrentItem() const
144 return m_nodeCurrent
? m_nodeCurrent
->GetData() : NULL
;
147 // find the menu item at given position
148 wxMenuItemList::compatibility_iterator
GetMenuItemFromPoint(const wxPoint
& pt
) const;
150 // refresh the given item
151 void RefreshItem(wxMenuItem
*item
);
153 // preselect the first item
154 void SelectFirst() { SetCurrent(m_menu
->GetMenuItems().GetFirst()); }
156 // process the key event, return true if done
157 bool ProcessKeyDown(int key
);
159 // process mouse move event
160 void ProcessMouseMove(const wxPoint
& pt
);
162 // don't dismiss the popup window if the parent menu was clicked
163 virtual bool ProcessLeftDown(wxMouseEvent
& event
);
166 // how did we perform this operation?
173 // draw the menu inside this window
174 virtual void DoDraw(wxControlRenderer
*renderer
);
177 void OnLeftUp(wxMouseEvent
& event
);
178 void OnMouseMove(wxMouseEvent
& event
);
179 void OnMouseLeave(wxMouseEvent
& event
);
180 void OnKeyDown(wxKeyEvent
& event
);
182 // reset the current item and node
185 // set the current node and item withotu refreshing anything
186 void SetCurrent(wxMenuItemList::compatibility_iterator node
);
187 virtual bool SetCurrent(bool doit
= true){return wxPopupTransientWindow::SetCurrent(doit
);};
189 // change the current item refreshing the old and new items
190 void ChangeCurrent(wxMenuItemList::compatibility_iterator node
);
192 // activate item, i.e. call either ClickItem() or OpenSubmenu() depending
193 // on what it is, return true if something was done (i.e. it's not a
195 bool ActivateItem(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
197 // send the event about the item click
198 void ClickItem(wxMenuItem
*item
);
200 // show the submenu for this item
201 void OpenSubmenu(wxMenuItem
*item
, InputMethod how
= WithKeyboard
);
203 // can this tiem be opened?
204 bool CanOpen(wxMenuItem
*item
)
206 return item
&& item
->IsEnabled() && item
->IsSubMenu();
209 // dismiss the menu and all parent menus too
210 void DismissAndNotify();
212 // react to dimissing this menu and also dismiss the parent if
214 void HandleDismiss(bool dismissParent
);
216 // do we have an open submenu?
217 bool HasOpenSubmenu() const { return m_hasOpenSubMenu
; }
219 // get previous node after the current one
220 wxMenuItemList::compatibility_iterator
GetPrevNode() const;
222 // get previous node before the given one, wrapping if it's the first one
223 wxMenuItemList::compatibility_iterator
GetPrevNode(wxMenuItemList::compatibility_iterator node
) const;
225 // get next node after the current one
226 wxMenuItemList::compatibility_iterator
GetNextNode() const;
228 // get next node after the given one, wrapping if it's the last one
229 wxMenuItemList::compatibility_iterator
GetNextNode(wxMenuItemList::compatibility_iterator node
) const;
235 // the menu node corresponding to the current item
236 wxMenuItemList::compatibility_iterator m_nodeCurrent
;
238 // do we currently have an opened submenu?
239 bool m_hasOpenSubMenu
;
241 DECLARE_EVENT_TABLE()
244 // ----------------------------------------------------------------------------
245 // wxMenuKbdRedirector: an event handler which redirects kbd input to wxMenu
246 // ----------------------------------------------------------------------------
248 class wxMenuKbdRedirector
: public wxEvtHandler
251 wxMenuKbdRedirector(wxMenu
*menu
) { m_menu
= menu
; }
253 virtual bool ProcessEvent(wxEvent
& event
)
255 if ( event
.GetEventType() == wxEVT_KEY_DOWN
)
257 return m_menu
->ProcessKeyDown(((wxKeyEvent
&)event
).GetKeyCode());
263 return wxEvtHandler::ProcessEvent(event
);
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
275 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
276 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
277 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
279 BEGIN_EVENT_TABLE(wxPopupMenuWindow
, wxPopupTransientWindow
)
280 EVT_KEY_DOWN(wxPopupMenuWindow::OnKeyDown
)
282 EVT_LEFT_UP(wxPopupMenuWindow::OnLeftUp
)
283 EVT_MOTION(wxPopupMenuWindow::OnMouseMove
)
284 EVT_LEAVE_WINDOW(wxPopupMenuWindow::OnMouseLeave
)
287 BEGIN_EVENT_TABLE(wxMenuBar
, wxMenuBarBase
)
288 EVT_KILL_FOCUS(wxMenuBar::OnKillFocus
)
290 EVT_KEY_DOWN(wxMenuBar::OnKeyDown
)
292 EVT_LEFT_DOWN(wxMenuBar::OnLeftDown
)
293 EVT_MOTION(wxMenuBar::OnMouseMove
)
296 // ============================================================================
298 // ============================================================================
300 // ----------------------------------------------------------------------------
302 // ----------------------------------------------------------------------------
304 wxPopupMenuWindow::wxPopupMenuWindow(wxWindow
*parent
, wxMenu
*menu
)
307 m_hasOpenSubMenu
= false;
311 (void)Create(parent
, wxBORDER_RAISED
);
313 SetCursor(wxCURSOR_ARROW
);
316 wxPopupMenuWindow::~wxPopupMenuWindow()
318 // When m_popupMenu in wxMenu is deleted because it
319 // is a child of an old menu bar being deleted (note: it does
320 // not get destroyed by the wxMenu destructor, but
321 // by DestroyChildren()), m_popupMenu should be reset to NULL.
323 m_menu
->m_popupMenu
= NULL
;
326 // ----------------------------------------------------------------------------
327 // wxPopupMenuWindow current item/node handling
328 // ----------------------------------------------------------------------------
330 void wxPopupMenuWindow::ResetCurrent()
333 SetCurrent(wxMenuItemList::compatibility_iterator());
335 SetCurrent((wxwxMenuItemListNode
*)NULL
);
339 void wxPopupMenuWindow::SetCurrent(wxMenuItemList::compatibility_iterator node
)
341 m_nodeCurrent
= node
;
344 void wxPopupMenuWindow::ChangeCurrent(wxMenuItemList::compatibility_iterator node
)
346 if ( node
!= m_nodeCurrent
)
348 wxMenuItemList::compatibility_iterator nodeOldCurrent
= m_nodeCurrent
;
350 m_nodeCurrent
= node
;
352 if ( nodeOldCurrent
)
354 wxMenuItem
*item
= nodeOldCurrent
->GetData();
355 wxCHECK_RET( item
, _T("no current item?") );
357 // if it was the currently opened menu, close it
358 if ( item
->IsSubMenu() && item
->GetSubMenu()->IsShown() )
360 item
->GetSubMenu()->Dismiss();
361 OnSubmenuDismiss( false );
368 RefreshItem(m_nodeCurrent
->GetData());
372 wxMenuItemList::compatibility_iterator
wxPopupMenuWindow::GetPrevNode() const
374 // return the last node if there had been no previously selected one
375 return m_nodeCurrent
? GetPrevNode(m_nodeCurrent
)
376 : m_menu
->GetMenuItems().GetLast();
379 wxMenuItemList::compatibility_iterator
380 wxPopupMenuWindow::GetPrevNode(wxMenuItemList::compatibility_iterator node
) const
384 node
= node
->GetPrevious();
387 node
= m_menu
->GetMenuItems().GetLast();
390 //else: the menu is empty
395 wxMenuItemList::compatibility_iterator
wxPopupMenuWindow::GetNextNode() const
397 // return the first node if there had been no previously selected one
398 return m_nodeCurrent
? GetNextNode(m_nodeCurrent
)
399 : m_menu
->GetMenuItems().GetFirst();
402 wxMenuItemList::compatibility_iterator
403 wxPopupMenuWindow::GetNextNode(wxMenuItemList::compatibility_iterator node
) const
407 node
= node
->GetNext();
410 node
= m_menu
->GetMenuItems().GetFirst();
413 //else: the menu is empty
418 // ----------------------------------------------------------------------------
419 // wxPopupMenuWindow popup/dismiss
420 // ----------------------------------------------------------------------------
422 void wxPopupMenuWindow::Popup(wxWindow
*focus
)
424 // check that the current item had been properly reset before
425 wxASSERT_MSG( !m_nodeCurrent
||
426 m_nodeCurrent
== m_menu
->GetMenuItems().GetFirst(),
427 _T("menu current item preselected incorrectly") );
429 wxPopupTransientWindow::Popup(focus
);
432 // ensure that this window is really on top of everything: without using
433 // SetWindowPos() it can be covered by its parent menu which is not
434 // really what we want
435 wxMenu
*menuParent
= m_menu
->GetParent();
438 wxPopupMenuWindow
*win
= menuParent
->m_popupMenu
;
440 // if we're shown, the parent menu must be also shown
441 wxCHECK_RET( win
, _T("parent menu is not shown?") );
443 if ( !::SetWindowPos(GetHwndOf(win
), GetHwnd(),
445 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOREDRAW
) )
447 wxLogLastError(_T("SetWindowPos(HWND_TOP)"));
455 void wxPopupMenuWindow::Dismiss()
457 if ( HasOpenSubmenu() )
459 wxMenuItem
*item
= GetCurrentItem();
460 wxCHECK_RET( item
&& item
->IsSubMenu(), _T("where is our open submenu?") );
462 wxPopupMenuWindow
*win
= item
->GetSubMenu()->m_popupMenu
;
463 wxCHECK_RET( win
, _T("opened submenu is not opened?") );
466 OnSubmenuDismiss( false );
469 wxPopupTransientWindow::Dismiss();
472 void wxPopupMenuWindow::OnDismiss()
474 // when we are dismissed because the user clicked elsewhere or we lost
475 // focus in any other way, hide the parent menu as well
479 void wxPopupMenuWindow::OnSubmenuDismiss(bool dismissParent
)
481 m_hasOpenSubMenu
= false;
483 // we are closing whole menu so remove current highlight
488 void wxPopupMenuWindow::HandleDismiss(bool dismissParent
)
492 m_menu
->OnDismiss(dismissParent
);
495 void wxPopupMenuWindow::DismissAndNotify()
501 // ----------------------------------------------------------------------------
502 // wxPopupMenuWindow geometry
503 // ----------------------------------------------------------------------------
505 wxMenuItemList::compatibility_iterator
506 wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint
& pt
) const
508 // we only use the y coord normally, but still check x in case the point is
509 // outside the window completely
510 if ( wxWindow::HitTest(pt
) == wxHT_WINDOW_INSIDE
)
513 for ( wxMenuItemList::compatibility_iterator node
= m_menu
->GetMenuItems().GetFirst();
515 node
= node
->GetNext() )
517 wxMenuItem
*item
= node
->GetData();
518 y
+= item
->GetHeight();
528 return wxMenuItemList::compatibility_iterator();
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 ( wxMenuItemList::compatibility_iterator 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 wxMenuItemList::compatibility_iterator 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 wxMenuItemList::compatibility_iterator 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
851 ChangeCurrent(wxMenuItemList::compatibility_iterator());
861 void wxPopupMenuWindow::OnKeyDown(wxKeyEvent
& event
)
863 if ( !ProcessKeyDown(event
.GetKeyCode()) )
869 bool wxPopupMenuWindow::ProcessKeyDown(int key
)
871 wxMenuItem
*item
= GetCurrentItem();
873 // first let the opened submenu to have it (no test for IsEnabled() here,
874 // the keys navigate even in a disabled submenu if we had somehow managed
875 // to open it inspit of this)
876 if ( HasOpenSubmenu() )
878 wxCHECK_MSG( CanOpen(item
), false,
879 _T("has open submenu but another item selected?") );
881 if ( item
->GetSubMenu()->ProcessKeyDown(key
) )
885 bool processed
= true;
887 // handle the up/down arrows, home, end, esc and return here, pass the
888 // left/right arrows to the menu bar except when the right arrow can be
889 // used to open a submenu
893 // if we're not a top level menu, close us, else leave this to the
895 if ( !m_menu
->GetParent() )
904 // close just this menu
906 HandleDismiss(false);
910 processed
= ActivateItem(item
);
914 ChangeCurrent(m_menu
->GetMenuItems().GetFirst());
918 ChangeCurrent(m_menu
->GetMenuItems().GetLast());
924 bool up
= key
== WXK_UP
;
926 wxMenuItemList::compatibility_iterator nodeStart
= up
? GetPrevNode()
929 while ( node
&& node
->GetData()->IsSeparator() )
931 node
= up
? GetPrevNode(node
) : GetNextNode(node
);
933 if ( node
== nodeStart
)
935 // nothing but separators and disabled items in this
938 node
= wxMenuItemList::compatibility_iterator();
957 // don't try to reopen an already opened menu
958 if ( !HasOpenSubmenu() && CanOpen(item
) )
969 // look for the menu item starting with this letter
970 if ( wxIsalnum((wxChar
)key
) )
972 // we want to start from the item after this one because
973 // if we're already on the item with the given accel we want to
974 // go to the next one, not to stay in place
975 wxMenuItemList::compatibility_iterator nodeStart
= GetNextNode();
977 // do we have more than one item with this accel?
978 bool notUnique
= false;
980 // translate everything to lower case before comparing
981 wxChar chAccel
= (wxChar
)wxTolower(key
);
983 // loop through all items searching for the item with this
985 wxMenuItemList::compatibility_iterator node
= nodeStart
,
987 nodeFound
= wxMenuItemList::compatibility_iterator();
993 item
= node
->GetData();
995 int idxAccel
= item
->GetAccelIndex();
996 if ( idxAccel
!= -1 &&
997 wxTolower(item
->GetLabel()[(size_t)idxAccel
])
1000 // ok, found an item with this accel
1003 // store it but continue searching as we need to
1004 // know if it's the only item with this accel or if
1008 else // we already had found such item
1012 // no need to continue further, we won't find
1013 // anything we don't already know
1018 // we want to iterate over all items wrapping around if
1020 node
= GetNextNode(node
);
1021 if ( node
== nodeStart
)
1023 // we've seen all nodes
1030 item
= nodeFound
->GetData();
1032 // go to this item anyhow
1033 ChangeCurrent(nodeFound
);
1035 if ( !notUnique
&& item
->IsEnabled() )
1037 // unique item with this accel - activate it
1038 processed
= ActivateItem(item
);
1040 //else: just select it but don't activate as the user might
1041 // have wanted to activate another item
1043 // skip "processed = false" below
1054 // ----------------------------------------------------------------------------
1056 // ----------------------------------------------------------------------------
1064 m_startRadioGroup
= -1;
1073 // ----------------------------------------------------------------------------
1074 // wxMenu and wxMenuGeometryInfo
1075 // ----------------------------------------------------------------------------
1077 wxMenuGeometryInfo::~wxMenuGeometryInfo()
1081 const wxMenuGeometryInfo
& wxMenu::GetGeometryInfo() const
1087 wxConstCast(this, wxMenu
)->m_geometry
=
1088 m_popupMenu
->GetRenderer()->GetMenuGeometry(m_popupMenu
, *this);
1092 wxFAIL_MSG( _T("can't get geometry without window") );
1099 void wxMenu::InvalidateGeometryInfo()
1108 // ----------------------------------------------------------------------------
1109 // wxMenu adding/removing items
1110 // ----------------------------------------------------------------------------
1112 void wxMenu::OnItemAdded(wxMenuItem
*item
)
1114 InvalidateGeometryInfo();
1118 #endif // wxUSE_ACCEL
1120 // the submenus of a popup menu should have the same invoking window as it
1122 if ( m_invokingWindow
&& item
->IsSubMenu() )
1124 item
->GetSubMenu()->SetInvokingWindow(m_invokingWindow
);
1128 void wxMenu::EndRadioGroup()
1130 // we're not inside a radio group any longer
1131 m_startRadioGroup
= -1;
1134 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
1136 if ( item
->GetKind() == wxITEM_RADIO
)
1138 int count
= GetMenuItemCount();
1140 if ( m_startRadioGroup
== -1 )
1142 // start a new radio group
1143 m_startRadioGroup
= count
;
1145 // for now it has just one element
1146 item
->SetAsRadioGroupStart();
1147 item
->SetRadioGroupEnd(m_startRadioGroup
);
1149 else // extend the current radio group
1151 // we need to update its end item
1152 item
->SetRadioGroupStart(m_startRadioGroup
);
1153 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
1157 node
->GetData()->SetRadioGroupEnd(count
);
1161 wxFAIL_MSG( _T("where is the radio group start item?") );
1165 else // not a radio item
1170 if ( !wxMenuBase::DoAppend(item
) )
1178 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1180 if ( !wxMenuBase::DoInsert(pos
, item
) )
1188 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1190 wxMenuItem
*itemOld
= wxMenuBase::DoRemove(item
);
1194 InvalidateGeometryInfo();
1197 RemoveAccelFor(item
);
1198 #endif // wxUSE_ACCEL
1204 // ----------------------------------------------------------------------------
1205 // wxMenu attaching/detaching
1206 // ----------------------------------------------------------------------------
1208 void wxMenu::Attach(wxMenuBarBase
*menubar
)
1210 wxMenuBase::Attach(menubar
);
1212 wxCHECK_RET( m_menuBar
, _T("menubar can't be NULL after attaching") );
1214 // unfortunately, we can't use m_menuBar->GetEventHandler() here because,
1215 // if the menubar is currently showing a menu, its event handler is a
1216 // temporary one installed by wxPopupWindow and so will disappear soon any
1217 // any attempts to use it from the newly attached menu would result in a
1220 // so we use the menubar itself, even if it's a pity as it means we can't
1221 // redirect all menu events by changing the menubar handler (FIXME)
1222 SetNextHandler(m_menuBar
);
1225 void wxMenu::Detach()
1227 wxMenuBase::Detach();
1230 // ----------------------------------------------------------------------------
1231 // wxMenu misc functions
1232 // ----------------------------------------------------------------------------
1234 wxWindow
*wxMenu::GetRootWindow() const
1238 // simple case - a normal menu attached to the menubar
1239 return GetMenuBar();
1242 // we're a popup menu but the trouble is that only the top level popup menu
1243 // has a pointer to the invoking window, so we must walk up the menu chain
1245 wxWindow
*win
= GetInvokingWindow();
1248 // we already have it
1252 wxMenu
*menu
= GetParent();
1255 // We are a submenu of a menu of a menubar
1256 if (menu
->GetMenuBar())
1257 return menu
->GetMenuBar();
1259 win
= menu
->GetInvokingWindow();
1263 menu
= menu
->GetParent();
1266 // we're probably going to crash in the caller anyhow, but try to detect
1267 // this error as soon as possible
1268 wxASSERT_MSG( win
, _T("menu without any associated window?") );
1270 // also remember it in this menu so that we don't have to search for it the
1272 wxConstCast(this, wxMenu
)->m_invokingWindow
= win
;
1277 wxRenderer
*wxMenu::GetRenderer() const
1279 // we're going to crash without renderer!
1280 wxCHECK_MSG( m_popupMenu
, NULL
, _T("neither popup nor menubar menu?") );
1282 return m_popupMenu
->GetRenderer();
1285 void wxMenu::RefreshItem(wxMenuItem
*item
)
1287 // the item geometry changed, so our might have changed as well
1288 InvalidateGeometryInfo();
1292 // this would be a bug in IsShown()
1293 wxCHECK_RET( m_popupMenu
, _T("must have popup window if shown!") );
1295 // recalc geometry to update the item height and such
1296 (void)GetGeometryInfo();
1298 m_popupMenu
->RefreshItem(item
);
1302 // ----------------------------------------------------------------------------
1303 // wxMenu showing and hiding
1304 // ----------------------------------------------------------------------------
1306 bool wxMenu::IsShown() const
1308 return m_popupMenu
&& m_popupMenu
->IsShown();
1311 void wxMenu::OnDismiss(bool dismissParent
)
1315 // always notify the parent about submenu disappearance
1316 wxPopupMenuWindow
*win
= m_menuParent
->m_popupMenu
;
1319 win
->OnSubmenuDismiss( true );
1323 wxFAIL_MSG( _T("parent menu not shown?") );
1326 // and if we dismiss everything, propagate to parent
1327 if ( dismissParent
)
1329 // dismissParent is recursive
1330 m_menuParent
->Dismiss();
1331 m_menuParent
->OnDismiss(true);
1334 else // no parent menu
1336 // notify the menu bar if we're a top level menu
1339 m_menuBar
->OnDismissMenu(dismissParent
);
1343 wxCHECK_RET( m_invokingWindow
, _T("what kind of menu is this?") );
1345 m_invokingWindow
->DismissPopupMenu();
1347 // Why reset it here? We need it for sending the event to...
1348 // SetInvokingWindow(NULL);
1353 void wxMenu::Popup(const wxPoint
& pos
, const wxSize
& size
, bool selectFirst
)
1355 // create the popup window if not done yet
1358 m_popupMenu
= new wxPopupMenuWindow(GetRootWindow(), this);
1361 // select the first item unless disabled
1364 m_popupMenu
->SelectFirst();
1367 // the geometry might have changed since the last time we were shown, so
1369 m_popupMenu
->SetClientSize(GetGeometryInfo().GetSize());
1371 // position it as specified
1372 m_popupMenu
->Position(pos
, size
);
1374 // the menu can't have the focus itself (it is a Windows limitation), so
1375 // always keep the focus at the originating window
1376 wxWindow
*focus
= GetRootWindow();
1378 wxASSERT_MSG( focus
, _T("no window to keep focus on?") );
1381 m_popupMenu
->Popup(focus
);
1384 void wxMenu::Dismiss()
1386 wxCHECK_RET( IsShown(), _T("can't dismiss hidden menu") );
1388 m_popupMenu
->Dismiss();
1391 // ----------------------------------------------------------------------------
1392 // wxMenu event processing
1393 // ----------------------------------------------------------------------------
1395 bool wxMenu::ProcessKeyDown(int key
)
1397 wxCHECK_MSG( m_popupMenu
, false,
1398 _T("can't process key events if not shown") );
1400 return m_popupMenu
->ProcessKeyDown(key
);
1403 bool wxMenu::ClickItem(wxMenuItem
*item
)
1406 if ( item
->IsCheckable() )
1408 // update the item state
1409 isChecked
= !item
->IsChecked();
1411 item
->Check(isChecked
!= 0);
1419 return SendEvent(item
->GetId(), isChecked
);
1422 // ----------------------------------------------------------------------------
1423 // wxMenu accel support
1424 // ----------------------------------------------------------------------------
1428 bool wxMenu::ProcessAccelEvent(const wxKeyEvent
& event
)
1430 // do we have an item for this accel?
1431 wxMenuItem
*item
= m_accelTable
.GetMenuItem(event
);
1432 if ( item
&& item
->IsEnabled() )
1434 return ClickItem(item
);
1438 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
1440 node
= node
->GetNext() )
1442 const wxMenuItem
*item
= node
->GetData();
1443 if ( item
->IsSubMenu() && item
->IsEnabled() )
1446 if ( item
->GetSubMenu()->ProcessAccelEvent(event
) )
1456 void wxMenu::AddAccelFor(wxMenuItem
*item
)
1458 wxAcceleratorEntry
*accel
= item
->GetAccel();
1461 accel
->SetMenuItem(item
);
1463 m_accelTable
.Add(*accel
);
1469 void wxMenu::RemoveAccelFor(wxMenuItem
*item
)
1471 wxAcceleratorEntry
*accel
= item
->GetAccel();
1474 m_accelTable
.Remove(*accel
);
1480 #endif // wxUSE_ACCEL
1482 // ----------------------------------------------------------------------------
1483 // wxMenuItem construction
1484 // ----------------------------------------------------------------------------
1486 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
1488 const wxString
& text
,
1489 const wxString
& help
,
1492 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
1495 m_height
= wxDefaultCoord
;
1497 m_radioGroup
.start
= -1;
1498 m_isRadioGroupStart
= false;
1500 m_bmpDisabled
= wxNullBitmap
;
1505 wxMenuItem::~wxMenuItem()
1509 // ----------------------------------------------------------------------------
1510 // wxMenuItemBase methods implemented here
1511 // ----------------------------------------------------------------------------
1514 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
1516 const wxString
& name
,
1517 const wxString
& help
,
1521 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
1525 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
1527 return wxStripMenuCodes(text
);
1530 // ----------------------------------------------------------------------------
1531 // wxMenuItem operations
1532 // ----------------------------------------------------------------------------
1534 void wxMenuItem::NotifyMenu()
1536 m_parentMenu
->RefreshItem(this);
1539 void wxMenuItem::UpdateAccelInfo()
1541 m_indexAccel
= wxControl::FindAccelIndex(m_text
);
1543 // will be empty if the text contains no TABs - ok
1544 m_strAccel
= m_text
.AfterFirst(_T('\t'));
1547 void wxMenuItem::SetText(const wxString
& text
)
1549 if ( text
!= m_text
)
1551 // first call the base class version to change m_text
1552 wxMenuItemBase::SetText(text
);
1560 void wxMenuItem::SetCheckable(bool checkable
)
1562 if ( checkable
!= IsCheckable() )
1564 wxMenuItemBase::SetCheckable(checkable
);
1570 void wxMenuItem::SetBitmaps(const wxBitmap
& bmpChecked
,
1571 const wxBitmap
& bmpUnchecked
)
1573 m_bmpChecked
= bmpChecked
;
1574 m_bmpUnchecked
= bmpUnchecked
;
1579 void wxMenuItem::Enable(bool enable
)
1581 if ( enable
!= m_isEnabled
)
1583 wxMenuItemBase::Enable(enable
);
1589 void wxMenuItem::Check(bool check
)
1591 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
1593 if ( m_isChecked
== check
)
1596 if ( GetKind() == wxITEM_RADIO
)
1598 // it doesn't make sense to uncheck a radio item - what would this do?
1602 // get the index of this item in the menu
1603 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
1604 int pos
= items
.IndexOf(this);
1605 wxCHECK_RET( pos
!= wxNOT_FOUND
,
1606 _T("menuitem not found in the menu items list?") );
1608 // get the radio group range
1612 if ( m_isRadioGroupStart
)
1614 // we already have all information we need
1616 end
= m_radioGroup
.end
;
1618 else // next radio group item
1620 // get the radio group end from the start item
1621 start
= m_radioGroup
.start
;
1622 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
1625 // also uncheck all the other items in this radio group
1626 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
1627 for ( int n
= start
; n
<= end
&& node
; n
++ )
1631 node
->GetData()->m_isChecked
= false;
1633 node
= node
->GetNext();
1637 wxMenuItemBase::Check(check
);
1642 // radio group stuff
1643 // -----------------
1645 void wxMenuItem::SetAsRadioGroupStart()
1647 m_isRadioGroupStart
= true;
1650 void wxMenuItem::SetRadioGroupStart(int start
)
1652 wxASSERT_MSG( !m_isRadioGroupStart
,
1653 _T("should only be called for the next radio items") );
1655 m_radioGroup
.start
= start
;
1658 void wxMenuItem::SetRadioGroupEnd(int end
)
1660 wxASSERT_MSG( m_isRadioGroupStart
,
1661 _T("should only be called for the first radio item") );
1663 m_radioGroup
.end
= end
;
1666 // ----------------------------------------------------------------------------
1667 // wxMenuBar creation
1668 // ----------------------------------------------------------------------------
1670 void wxMenuBar::Init()
1678 m_shouldShowMenu
= false;
1681 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
1685 for (size_t i
= 0; i
< n
; ++i
)
1686 Append(menus
[i
], titles
[i
]);
1689 void wxMenuBar::Attach(wxFrame
*frame
)
1691 // maybe you really wanted to call Detach()?
1692 wxCHECK_RET( frame
, _T("wxMenuBar::Attach(NULL) called") );
1694 wxMenuBarBase::Attach(frame
);
1698 // reparent if necessary
1699 if ( m_frameLast
!= frame
)
1704 // show it back - was hidden by Detach()
1707 else // not created yet, do it now
1709 // we have no way to return the error from here anyhow :-(
1710 (void)Create(frame
, wxID_ANY
);
1712 SetCursor(wxCURSOR_ARROW
);
1714 SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT
));
1716 // calculate and set our height (it won't be changed any more)
1717 SetSize(wxDefaultCoord
, GetBestSize().y
);
1720 // remember the last frame which had us to avoid unnecessarily reparenting
1722 m_frameLast
= frame
;
1725 void wxMenuBar::Detach()
1727 // don't delete the window because we may be reattached later, just hide it
1733 wxMenuBarBase::Detach();
1736 wxMenuBar::~wxMenuBar()
1740 // ----------------------------------------------------------------------------
1741 // wxMenuBar adding/removing items
1742 // ----------------------------------------------------------------------------
1744 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1746 return Insert(GetCount(), menu
, title
);
1749 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1751 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1754 wxMenuInfo
*info
= new wxMenuInfo(title
);
1755 m_menuInfos
.Insert(info
, pos
);
1757 RefreshAllItemsAfter(pos
);
1762 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1764 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1768 wxMenuInfo
& info
= m_menuInfos
[pos
];
1770 info
.SetLabel(title
);
1772 // even if the old menu was disabled, the new one is not any more
1775 // even if we change only this one, the new label has different width,
1776 // so we need to refresh everything beyond this item as well
1777 RefreshAllItemsAfter(pos
);
1783 wxMenu
*wxMenuBar::Remove(size_t pos
)
1785 wxMenu
*menuOld
= wxMenuBarBase::Remove(pos
);
1789 m_menuInfos
.RemoveAt(pos
);
1791 // this doesn't happen too often, so don't try to be too smart - just
1792 // refresh everything
1799 // ----------------------------------------------------------------------------
1800 // wxMenuBar top level menus access
1801 // ----------------------------------------------------------------------------
1803 wxCoord
wxMenuBar::GetItemWidth(size_t pos
) const
1805 return m_menuInfos
[pos
].GetWidth(wxConstCast(this, wxMenuBar
));
1808 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1810 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1812 if ( enable
!= m_menuInfos
[pos
].IsEnabled() )
1814 m_menuInfos
[pos
].SetEnabled(enable
);
1818 //else: nothing to do
1821 bool wxMenuBar::IsEnabledTop(size_t pos
) const
1823 wxCHECK_MSG( pos
< GetCount(), false, _T("invalid index in IsEnabledTop") );
1825 return m_menuInfos
[pos
].IsEnabled();
1828 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
1830 wxCHECK_RET( pos
< GetCount(), _T("invalid index in EnableTop") );
1832 if ( label
!= m_menuInfos
[pos
].GetLabel() )
1834 m_menuInfos
[pos
].SetLabel(label
);
1838 //else: nothing to do
1841 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
1843 wxCHECK_MSG( pos
< GetCount(), wxEmptyString
, _T("invalid index in GetLabelTop") );
1845 return m_menuInfos
[pos
].GetLabel();
1848 // ----------------------------------------------------------------------------
1849 // wxMenuBar drawing
1850 // ----------------------------------------------------------------------------
1852 void wxMenuBar::RefreshAllItemsAfter(size_t pos
)
1856 // no need to refresh if nothing is shown yet
1860 wxRect rect
= GetItemRect(pos
);
1861 rect
.width
= GetClientSize().x
- rect
.x
;
1865 void wxMenuBar::RefreshItem(size_t pos
)
1867 wxCHECK_RET( pos
!= (size_t)-1,
1868 _T("invalid item in wxMenuBar::RefreshItem") );
1872 // no need to refresh if nothing is shown yet
1876 RefreshRect(GetItemRect(pos
));
1879 void wxMenuBar::DoDraw(wxControlRenderer
*renderer
)
1881 wxDC
& dc
= renderer
->GetDC();
1882 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1884 // redraw only the items which must be redrawn
1886 // we don't have to use GetUpdateClientRect() here because our client rect
1887 // is the same as total one
1888 wxRect rectUpdate
= GetUpdateRegion().GetBox();
1890 int flagsMenubar
= GetStateFlags();
1894 rect
.height
= GetClientSize().y
;
1897 size_t count
= GetCount();
1898 for ( size_t n
= 0; n
< count
; n
++ )
1900 if ( x
> rectUpdate
.GetRight() )
1902 // all remaining items are to the right of rectUpdate
1907 rect
.width
= GetItemWidth(n
);
1909 if ( x
< rectUpdate
.x
)
1911 // this item is still to the left of rectUpdate
1915 int flags
= flagsMenubar
;
1916 if ( m_current
!= -1 && n
== (size_t)m_current
)
1918 flags
|= wxCONTROL_SELECTED
;
1921 if ( !IsEnabledTop(n
) )
1923 flags
|= wxCONTROL_DISABLED
;
1926 GetRenderer()->DrawMenuBarItem
1930 m_menuInfos
[n
].GetLabel(),
1932 m_menuInfos
[n
].GetAccelIndex()
1937 // ----------------------------------------------------------------------------
1938 // wxMenuBar geometry
1939 // ----------------------------------------------------------------------------
1941 wxRect
wxMenuBar::GetItemRect(size_t pos
) const
1943 wxASSERT_MSG( pos
< GetCount(), _T("invalid menu bar item index") );
1944 wxASSERT_MSG( IsCreated(), _T("can't call this method yet") );
1949 rect
.height
= GetClientSize().y
;
1951 for ( size_t n
= 0; n
< pos
; n
++ )
1953 rect
.x
+= GetItemWidth(n
);
1956 rect
.width
= GetItemWidth(pos
);
1961 wxSize
wxMenuBar::DoGetBestClientSize() const
1964 if ( GetMenuCount() > 0 )
1966 wxClientDC
dc(wxConstCast(this, wxMenuBar
));
1967 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1968 dc
.GetTextExtent(GetLabelTop(0), &size
.x
, &size
.y
);
1970 // adjust for the renderer we use
1971 size
= GetRenderer()->GetMenuBarItemSize(size
);
1973 else // empty menubar
1979 // the width is arbitrary, of course, for horizontal menubar
1985 int wxMenuBar::GetMenuFromPoint(const wxPoint
& pos
) const
1987 if ( pos
.x
< 0 || pos
.y
< 0 || pos
.y
> GetClientSize().y
)
1992 size_t count
= GetCount();
1993 for ( size_t item
= 0; item
< count
; item
++ )
1995 x
+= GetItemWidth(item
);
2003 // to the right of the last menu item
2007 // ----------------------------------------------------------------------------
2008 // wxMenuBar menu operations
2009 // ----------------------------------------------------------------------------
2011 void wxMenuBar::SelectMenu(size_t pos
)
2014 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::SelectMenu"));
2020 void wxMenuBar::DoSelectMenu(size_t pos
)
2022 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in DoSelectMenu") );
2024 int posOld
= m_current
;
2030 // close the previous menu
2031 if ( IsShowingMenu() )
2033 // restore m_shouldShowMenu flag after DismissMenu() which resets
2035 bool old
= m_shouldShowMenu
;
2039 m_shouldShowMenu
= old
;
2042 RefreshItem((size_t)posOld
);
2048 void wxMenuBar::PopupMenu(size_t pos
)
2050 wxCHECK_RET( pos
< GetCount(), _T("invalid menu index in PopupCurrentMenu") );
2057 // ----------------------------------------------------------------------------
2058 // wxMenuBar input handing
2059 // ----------------------------------------------------------------------------
2062 Note that wxMenuBar doesn't use wxInputHandler but handles keyboard and
2063 mouse in the same way under all platforms. This is because it doesn't derive
2064 from wxControl (which works with input handlers) but directly from wxWindow.
2066 Also, menu bar input handling is rather simple, so maybe it's not really
2067 worth making it themeable - at least I've decided against doing it now as it
2068 would merging the changes back into trunk more difficult. But it still could
2069 be done later if really needed.
2072 void wxMenuBar::OnKillFocus(wxFocusEvent
& event
)
2074 if ( m_current
!= -1 )
2076 RefreshItem((size_t)m_current
);
2084 void wxMenuBar::OnLeftDown(wxMouseEvent
& event
)
2092 else // we didn't have mouse capture, capture it now
2094 m_current
= GetMenuFromPoint(event
.GetPosition());
2095 if ( m_current
== -1 )
2097 // unfortunately, we can't prevent wxMSW from giving us the focus,
2098 // so we can only give it back
2103 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::OnLeftDown"));
2106 // show it as selected
2107 RefreshItem((size_t)m_current
);
2110 PopupCurrentMenu(false /* don't select first item - as Windows does */);
2115 void wxMenuBar::OnMouseMove(wxMouseEvent
& event
)
2119 (void)ProcessMouseEvent(event
.GetPosition());
2127 bool wxMenuBar::ProcessMouseEvent(const wxPoint
& pt
)
2129 // a hack to ignore the extra mouse events MSW sends us: this is similar to
2130 // wxUSE_MOUSEEVENT_HACK in wxWin itself but it isn't enough for us here as
2131 // we get the messages from different windows (old and new popup menus for
2134 static wxPoint s_ptLast
;
2135 if ( pt
== s_ptLast
)
2143 int currentNew
= GetMenuFromPoint(pt
);
2144 if ( (currentNew
== -1) || (currentNew
== m_current
) )
2149 // FIXME: temporary workaround for crash, to be fixed
2150 // in a later version.
2152 // select the new active item
2153 DoSelectMenu(currentNew
);
2155 // show the menu if we know that we should, even if we hadn't been showing
2156 // it before (this may happen if the previous menu was disabled)
2157 if ( m_shouldShowMenu
&& !m_menuShown
)
2159 // open the new menu if the old one we closed had been opened
2160 PopupCurrentMenu(false /* don't select first item - as Windows does */);
2167 void wxMenuBar::OnKeyDown(wxKeyEvent
& event
)
2169 // ensure that we have a current item - we might not have it if we're
2170 // given the focus with Alt or F10 press (and under GTK+ the menubar
2171 // somehow gets the keyboard events even when it doesn't have focus...)
2172 if ( m_current
== -1 )
2174 if ( !HasCapture() )
2178 else // we do have capture
2180 // we always maintain a valid current item while we're in modal
2181 // state (i.e. have the capture)
2182 wxFAIL_MSG( _T("how did we manage to lose current item?") );
2188 int key
= event
.GetKeyCode();
2190 // first let the menu have it
2191 if ( IsShowingMenu() && m_menuShown
->ProcessKeyDown(key
) )
2196 // cycle through the menu items when left/right arrows are pressed and open
2197 // the menu when up/down one is
2201 // Alt must be processed at wxWindow level too
2206 // remove the selection and give the focus away
2207 if ( m_current
!= -1 )
2209 if ( IsShowingMenu() )
2221 size_t count
= GetCount();
2224 // the item won't change anyhow
2227 //else: otherwise, it will
2229 // remember if we were showing a menu - if we did, we should
2230 // show the new menu after changing the item
2231 bool wasMenuOpened
= IsShowingMenu();
2232 if ( wasMenuOpened
)
2237 // cast is safe as we tested for -1 above
2238 size_t currentNew
= (size_t)m_current
;
2240 if ( key
== WXK_LEFT
)
2242 if ( currentNew
-- == 0 )
2243 currentNew
= count
- 1;
2247 if ( ++currentNew
== count
)
2251 DoSelectMenu(currentNew
);
2253 if ( wasMenuOpened
)
2268 // letters open the corresponding menu
2271 int idxFound
= FindNextItemForAccel(m_current
, key
, &unique
);
2273 if ( idxFound
!= -1 )
2275 if ( IsShowingMenu() )
2280 DoSelectMenu((size_t)idxFound
);
2282 // if the item is not unique, just select it but don't
2283 // activate as the user might have wanted to activate
2286 // also, don't try to open a disabled menu
2287 if ( unique
&& IsEnabledTop((size_t)idxFound
) )
2293 // skip the "event.Skip()" below
2302 // ----------------------------------------------------------------------------
2303 // wxMenuBar accel handling
2304 // ----------------------------------------------------------------------------
2306 int wxMenuBar::FindNextItemForAccel(int idxStart
, int key
, bool *unique
) const
2308 if ( !wxIsalnum((wxChar
)key
) )
2310 // we only support letters/digits as accels
2314 // do we have more than one item with this accel?
2318 // translate everything to lower case before comparing
2319 wxChar chAccel
= (wxChar
)wxTolower(key
);
2321 // the index of the item with this accel
2324 // loop through all items searching for the item with this
2325 // accel starting at the item after the current one
2326 int count
= GetCount();
2327 int n
= idxStart
== -1 ? 0 : idxStart
+ 1;
2338 const wxMenuInfo
& info
= m_menuInfos
[n
];
2340 int idxAccel
= info
.GetAccelIndex();
2341 if ( idxAccel
!= -1 &&
2342 wxTolower(info
.GetLabel()[(size_t)idxAccel
])
2345 // ok, found an item with this accel
2346 if ( idxFound
== -1 )
2348 // store it but continue searching as we need to
2349 // know if it's the only item with this accel or if
2353 else // we already had found such item
2358 // no need to continue further, we won't find
2359 // anything we don't already know
2364 // we want to iterate over all items wrapping around if
2372 if ( n
== idxStart
)
2374 // we've seen all items
2384 bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent
& event
)
2387 for ( wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
2389 node
= node
->GetNext(), n
++ )
2391 // accels of the items in the disabled menus shouldn't work
2392 if ( m_menuInfos
[n
].IsEnabled() )
2394 if ( node
->GetData()->ProcessAccelEvent(event
) )
2396 // menu processed it
2406 #endif // wxUSE_ACCEL
2408 // ----------------------------------------------------------------------------
2409 // wxMenuBar menus showing
2410 // ----------------------------------------------------------------------------
2412 void wxMenuBar::PopupCurrentMenu(bool selectFirst
)
2414 wxCHECK_RET( m_current
!= -1, _T("no menu to popup") );
2416 // forgot to call DismissMenu()?
2417 wxASSERT_MSG( !m_menuShown
, _T("shouldn't show two menus at once!") );
2419 // in any case, we should show it - even if we won't
2420 m_shouldShowMenu
= true;
2422 if ( IsEnabledTop(m_current
) )
2424 // remember the menu we show
2425 m_menuShown
= GetMenu(m_current
);
2427 // we don't show the menu at all if it has no items
2428 if ( !m_menuShown
->IsEmpty() )
2430 // position it correctly: note that we must use screen coords and
2431 // that we pass 0 as width to position the menu exactly below the
2432 // item, not to the right of it
2433 wxRect rectItem
= GetItemRect(m_current
);
2435 m_menuShown
->SetInvokingWindow(m_frameLast
);
2437 m_menuShown
->Popup(ClientToScreen(rectItem
.GetPosition()),
2438 wxSize(0, rectItem
.GetHeight()),
2443 // reset it back as no menu is shown
2447 //else: don't show disabled menu
2450 void wxMenuBar::DismissMenu()
2452 wxCHECK_RET( m_menuShown
, _T("can't dismiss menu if none is shown") );
2454 m_menuShown
->Dismiss();
2458 void wxMenuBar::OnDismissMenu(bool dismissMenuBar
)
2460 m_shouldShowMenu
= false;
2462 if ( dismissMenuBar
)
2468 void wxMenuBar::OnDismiss()
2472 wxLogTrace(_T("mousecapture"), _T("Releasing mouse from wxMenuBar::OnDismiss"));
2473 GetCapture()->ReleaseMouse();
2476 if ( m_current
!= -1 )
2478 size_t current
= m_current
;
2481 RefreshItem(current
);
2487 void wxMenuBar::GiveAwayFocus()
2489 GetFrame()->SetFocus();
2492 // ----------------------------------------------------------------------------
2493 // popup menu support
2494 // ----------------------------------------------------------------------------
2496 wxEventLoop
*wxWindow::ms_evtLoopPopup
= NULL
;
2498 bool wxWindow::DoPopupMenu(wxMenu
*menu
, int x
, int y
)
2500 wxCHECK_MSG( !ms_evtLoopPopup
, false,
2501 _T("can't show more than one popup menu at a time") );
2504 // we need to change the cursor before showing the menu as, apparently, no
2505 // cursor changes took place while the mouse is captured
2506 wxCursor cursorOld
= GetCursor();
2507 SetCursor(wxCURSOR_ARROW
);
2511 // flash any delayed log messages before showing the menu, otherwise it
2512 // could be dismissed (because it would lose focus) immediately after being
2514 wxLog::FlushActive();
2516 // some controls update themselves from OnIdle() call - let them do it
2517 wxTheApp
->ProcessIdle();
2519 // if the window hadn't been refreshed yet, the menu can adversely affect
2520 // its next OnPaint() handler execution - i.e. scrolled window refresh
2521 // logic breaks then as it scrolls part of the menu which hadn't been there
2522 // when the update event was generated into view
2526 menu
->SetInvokingWindow(this);
2528 // wxLogDebug( "Name of invoking window %s", menu->GetInvokingWindow()->GetName().c_str() );
2530 menu
->Popup(ClientToScreen(wxPoint(x
, y
)), wxSize(0,0));
2532 // this is not very useful if the menu was popped up because of the mouse
2533 // click but I think it is nice to do when it appears because of a key
2534 // press (i.e. Windows menu key)
2536 // Windows itself doesn't do it, but IMHO this is nice
2539 // we have to redirect all keyboard input to the menu temporarily
2540 PushEventHandler(new wxMenuKbdRedirector(menu
));
2542 // enter the local modal loop
2543 ms_evtLoopPopup
= new wxEventLoop
;
2544 ms_evtLoopPopup
->Run();
2546 delete ms_evtLoopPopup
;
2547 ms_evtLoopPopup
= NULL
;
2549 // remove the handler
2550 PopEventHandler(true /* delete it */);
2552 menu
->SetInvokingWindow(NULL
);
2555 SetCursor(cursorOld
);
2561 void wxWindow::DismissPopupMenu()
2563 wxCHECK_RET( ms_evtLoopPopup
, _T("no popup menu shown") );
2565 ms_evtLoopPopup
->Exit();
2568 #endif // wxUSE_MENUS