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 license 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  14 // ============================================================================ 
  16 // ---------------------------------------------------------------------------- 
  18 // ---------------------------------------------------------------------------- 
  21     #pragma implementation "univmenuitem.h" 
  22     #pragma implementation "univmenu.h" 
  25 #include "wx/wxprec.h" 
  32     #include "wx/dynarray.h" 
  33     #include "wx/control.h"      // for FindAccelIndex() 
  35     #include "wx/settings.h" 
  42 #include "wx/popupwin.h" 
  43 #include "wx/evtloop.h" 
  44 #include "wx/dcclient.h" 
  47 #include "wx/univ/renderer.h" 
  50     #include "wx/msw/private.h" 
  53 // ---------------------------------------------------------------------------- 
  54 // wxMenuInfo contains all extra information about top level menus we need 
  55 // ---------------------------------------------------------------------------- 
  57 class WXDLLEXPORT wxMenuInfo
 
  61     wxMenuInfo(const wxString
& text
) 
  69     void SetLabel(const wxString
& text
) 
  71         // remember the accel char (may be -1 if none) 
  72         m_indexAccel 
= wxControl::FindAccelIndex(text
, &m_label
); 
  74         // calculate the width later, after the menu bar is created 
  78     void SetEnabled(bool enabled 
= TRUE
) { m_isEnabled 
= enabled
; } 
  82     const wxString
& GetLabel() const { return m_label
; } 
  83     bool IsEnabled() const { return m_isEnabled
; } 
  84     wxCoord 
GetWidth(wxMenuBar 
*menubar
) const 
  88             wxConstCast(this, wxMenuInfo
)->CalcWidth(menubar
); 
  94     int GetAccelIndex() const { return m_indexAccel
; } 
  97     void CalcWidth(wxMenuBar 
*menubar
) 
 100         wxClientDC 
dc(menubar
); 
 101         dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
)); 
 102         dc
.GetTextExtent(m_label
, &size
.x
, &size
.y
); 
 104         // adjust for the renderer we use and store the width 
 105         m_width 
= menubar
->GetRenderer()->GetMenuBarItemSize(size
).x
; 
 114 #include "wx/arrimpl.cpp" 
 116 WX_DEFINE_OBJARRAY(wxMenuInfoArray
); 
 118 // ---------------------------------------------------------------------------- 
 119 // wxPopupMenuWindow: a popup window showing a menu 
 120 // ---------------------------------------------------------------------------- 
 122 class wxPopupMenuWindow 
: public wxPopupTransientWindow
 
 125     wxPopupMenuWindow(wxWindow 
*parent
, wxMenu 
*menu
); 
 127     ~wxPopupMenuWindow(); 
 129     // override the base class version to select the first item initially 
 130     virtual void Popup(wxWindow 
*focus 
= NULL
); 
 132     // override the base class version to dismiss any open submenus 
 133     virtual void Dismiss(); 
 135     // notify the menu when the window disappears from screen 
 136     virtual void OnDismiss(); 
 138     // called when a submenu is dismissed 
 139     void OnSubmenuDismiss() { m_hasOpenSubMenu 
= FALSE
; } 
 141     // get the currently selected item (may be NULL) 
 142     wxMenuItem 
*GetCurrentItem() const 
 144         return m_nodeCurrent 
? m_nodeCurrent
->GetData() : NULL
; 
 147     // find the menu item at given position 
 148     wxMenuItemList::Node 
*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::Node 
*node
); 
 188     // change the current item refreshing the old and new items 
 189     void ChangeCurrent(wxMenuItemList::Node 
*node
); 
 191     // activate item, i.e. call either ClickItem() or OpenSubmenu() depending 
 192     // on what it is, return TRUE if something was done (i.e. it's not a 
 194     bool ActivateItem(wxMenuItem 
*item
, InputMethod how 
= WithKeyboard
); 
 196     // send the event about the item click 
 197     void ClickItem(wxMenuItem 
*item
); 
 199     // show the submenu for this item 
 200     void OpenSubmenu(wxMenuItem 
*item
, InputMethod how 
= WithKeyboard
); 
 202     // can this tiem be opened? 
 203     bool CanOpen(wxMenuItem 
*item
) 
 205         return item 
&& item
->IsEnabled() && item
->IsSubMenu(); 
 208     // dismiss the menu and all parent menus too 
 209     void DismissAndNotify(); 
 211     // react to dimissing this menu and also dismiss the parent if 
 213     void HandleDismiss(bool dismissParent
); 
 215     // do we have an open submenu? 
 216     bool HasOpenSubmenu() const { return m_hasOpenSubMenu
; } 
 218     // get previous node after the current one 
 219     wxMenuItemList::Node 
*GetPrevNode() const; 
 221     // get previous node before the given one, wrapping if it's the first one 
 222     wxMenuItemList::Node 
*GetPrevNode(wxMenuItemList::Node 
*node
) const; 
 224     // get next node after the current one 
 225     wxMenuItemList::Node 
*GetNextNode() const; 
 227     // get next node after the given one, wrapping if it's the last one 
 228     wxMenuItemList::Node 
*GetNextNode(wxMenuItemList::Node 
*node
) const; 
 234     // the menu node corresponding to the current item 
 235     wxMenuItemList::Node 
*m_nodeCurrent
; 
 237     // do we currently have an opened submenu? 
 238     bool m_hasOpenSubMenu
; 
 240     DECLARE_EVENT_TABLE() 
 243 // ---------------------------------------------------------------------------- 
 244 // wxMenuKbdRedirector: an event handler which redirects kbd input to wxMenu 
 245 // ---------------------------------------------------------------------------- 
 247 class wxMenuKbdRedirector 
: public wxEvtHandler
 
 250     wxMenuKbdRedirector(wxMenu 
*menu
) { m_menu 
= menu
; } 
 252     virtual bool ProcessEvent(wxEvent
& event
) 
 254         if ( event
.GetEventType() == wxEVT_KEY_DOWN 
) 
 256             return m_menu
->ProcessKeyDown(((wxKeyEvent 
&)event
).GetKeyCode()); 
 262             return wxEvtHandler::ProcessEvent(event
); 
 270 // ---------------------------------------------------------------------------- 
 272 // ---------------------------------------------------------------------------- 
 274 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
) 
 275 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
) 
 276 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
) 
 278 BEGIN_EVENT_TABLE(wxPopupMenuWindow
, wxPopupTransientWindow
) 
 279     EVT_KEY_DOWN(wxPopupMenuWindow::OnKeyDown
) 
 281     EVT_LEFT_UP(wxPopupMenuWindow::OnLeftUp
) 
 282     EVT_MOTION(wxPopupMenuWindow::OnMouseMove
) 
 283     EVT_LEAVE_WINDOW(wxPopupMenuWindow::OnMouseLeave
) 
 286 BEGIN_EVENT_TABLE(wxMenuBar
, wxMenuBarBase
) 
 287     EVT_KILL_FOCUS(wxMenuBar::OnKillFocus
) 
 289     EVT_KEY_DOWN(wxMenuBar::OnKeyDown
) 
 291     EVT_LEFT_DOWN(wxMenuBar::OnLeftDown
) 
 292     EVT_MOTION(wxMenuBar::OnMouseMove
) 
 295 // ============================================================================ 
 297 // ============================================================================ 
 299 // ---------------------------------------------------------------------------- 
 301 // ---------------------------------------------------------------------------- 
 303 wxPopupMenuWindow::wxPopupMenuWindow(wxWindow 
*parent
, wxMenu 
*menu
) 
 306     m_hasOpenSubMenu 
= FALSE
; 
 310     (void)Create(parent
, wxBORDER_RAISED
); 
 312     SetCursor(wxCURSOR_ARROW
); 
 315 wxPopupMenuWindow::~wxPopupMenuWindow() 
 317     // When m_popupMenu in wxMenu is deleted because it 
 318     // is a child of an old menu bar being deleted (note: it does 
 319     // not get destroyed by the wxMenu destructor, but 
 320     // by DestroyChildren()), m_popupMenu should be reset to NULL. 
 322     m_menu
->m_popupMenu 
= NULL
; 
 325 // ---------------------------------------------------------------------------- 
 326 // wxPopupMenuWindow current item/node handling 
 327 // ---------------------------------------------------------------------------- 
 329 void wxPopupMenuWindow::ResetCurrent() 
 334 void wxPopupMenuWindow::SetCurrent(wxMenuItemList::Node 
*node
) 
 336     m_nodeCurrent 
= node
; 
 339 void wxPopupMenuWindow::ChangeCurrent(wxMenuItemList::Node 
*node
) 
 341     if ( node 
!= m_nodeCurrent 
) 
 343         wxMenuItemList::Node 
*nodeOldCurrent 
= m_nodeCurrent
; 
 345         m_nodeCurrent 
= node
; 
 347         if ( nodeOldCurrent 
) 
 349             wxMenuItem 
*item 
= nodeOldCurrent
->GetData(); 
 350             wxCHECK_RET( item
, _T("no current item?") ); 
 352             // if it was the currently opened menu, close it 
 353             if ( item
->IsSubMenu() && item
->GetSubMenu()->IsShown() ) 
 355                 item
->GetSubMenu()->Dismiss(); 
 363             RefreshItem(m_nodeCurrent
->GetData()); 
 367 wxMenuItemList::Node 
*wxPopupMenuWindow::GetPrevNode() const 
 369     // return the last node if there had been no previously selected one 
 370     return m_nodeCurrent 
? GetPrevNode(m_nodeCurrent
) 
 371                          : m_menu
->GetMenuItems().GetLast(); 
 374 wxMenuItemList::Node 
* 
 375 wxPopupMenuWindow::GetPrevNode(wxMenuItemList::Node 
*node
) const 
 379         node 
= node
->GetPrevious(); 
 382             node 
= m_menu
->GetMenuItems().GetLast(); 
 385     //else: the menu is empty 
 390 wxMenuItemList::Node 
*wxPopupMenuWindow::GetNextNode() const 
 392     // return the first node if there had been no previously selected one 
 393     return m_nodeCurrent 
? GetNextNode(m_nodeCurrent
) 
 394                          : m_menu
->GetMenuItems().GetFirst(); 
 397 wxMenuItemList::Node 
* 
 398 wxPopupMenuWindow::GetNextNode(wxMenuItemList::Node 
*node
) const 
 402         node 
= node
->GetNext(); 
 405             node 
= m_menu
->GetMenuItems().GetFirst(); 
 408     //else: the menu is empty 
 413 // ---------------------------------------------------------------------------- 
 414 // wxPopupMenuWindow popup/dismiss 
 415 // ---------------------------------------------------------------------------- 
 417 void wxPopupMenuWindow::Popup(wxWindow 
*focus
) 
 419     // check that the current item had been properly reset before 
 420     wxASSERT_MSG( !m_nodeCurrent 
|| 
 421                   m_nodeCurrent 
== m_menu
->GetMenuItems().GetFirst(), 
 422                   _T("menu current item preselected incorrectly") ); 
 424     wxPopupTransientWindow::Popup(focus
); 
 427     // ensure that this window is really on top of everything: without using 
 428     // SetWindowPos() it can be covered by its parent menu which is not 
 429     // really what we want 
 430     wxMenu 
*menuParent 
= m_menu
->GetParent(); 
 433         wxPopupMenuWindow 
*win 
= menuParent
->m_popupMenu
; 
 435         // if we're shown, the parent menu must be also shown 
 436         wxCHECK_RET( win
, _T("parent menu is not shown?") ); 
 438         if ( !::SetWindowPos(GetHwndOf(win
), GetHwnd(), 
 440                              SWP_NOMOVE 
| SWP_NOSIZE 
| SWP_NOREDRAW
) ) 
 442             wxLogLastError(_T("SetWindowPos(HWND_TOP)")); 
 450 void wxPopupMenuWindow::Dismiss() 
 452     if ( HasOpenSubmenu() ) 
 454         wxMenuItem 
*item 
= GetCurrentItem(); 
 455         wxCHECK_RET( item 
&& item
->IsSubMenu(), _T("where is our open submenu?") ); 
 457         wxPopupMenuWindow 
*win 
= item
->GetSubMenu()->m_popupMenu
; 
 458         wxCHECK_RET( win
, _T("opened submenu is not opened?") ); 
 464     wxPopupTransientWindow::Dismiss(); 
 467 void wxPopupMenuWindow::OnDismiss() 
 469     // when we are dismissed because the user clicked elsewhere or we lost 
 470     // focus in any other way, hide the parent menu as well 
 474 void wxPopupMenuWindow::HandleDismiss(bool dismissParent
) 
 478     m_menu
->OnDismiss(dismissParent
); 
 481 void wxPopupMenuWindow::DismissAndNotify() 
 487 // ---------------------------------------------------------------------------- 
 488 // wxPopupMenuWindow geometry 
 489 // ---------------------------------------------------------------------------- 
 491 wxMenuItemList::Node 
* 
 492 wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint
& pt
) const 
 494     // we only use the y coord normally, but still check x in case the point is 
 495     // outside the window completely 
 496     if ( wxWindow::HitTest(pt
) == wxHT_WINDOW_INSIDE 
) 
 499         for ( wxMenuItemList::Node 
*node 
= m_menu
->GetMenuItems().GetFirst(); 
 501               node 
= node
->GetNext() ) 
 503             wxMenuItem 
*item 
= node
->GetData(); 
 504             y 
+= item
->GetHeight(); 
 516 // ---------------------------------------------------------------------------- 
 517 // wxPopupMenuWindow drawing 
 518 // ---------------------------------------------------------------------------- 
 520 void wxPopupMenuWindow::RefreshItem(wxMenuItem 
*item
) 
 522     wxCHECK_RET( item
, _T("can't refresh NULL item") ); 
 524     wxASSERT_MSG( IsShown(), _T("can't refresh menu which is not shown") ); 
 526     // FIXME: -1 here because of SetLogicalOrigin(1, 1) in DoDraw() 
 527     RefreshRect(wxRect(0, item
->GetPosition() - 1, 
 528                 m_menu
->GetGeometryInfo().GetSize().x
, item
->GetHeight())); 
 531 void wxPopupMenuWindow::DoDraw(wxControlRenderer 
*renderer
) 
 533     // no clipping so far - do we need it? I don't think so as the menu is 
 534     // never partially covered as it is always on top of everything 
 536     wxDC
& dc 
= renderer
->GetDC(); 
 537     dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
)); 
 539     // FIXME: this should be done in the renderer, however when it is fixed 
 540     //        wxPopupMenuWindow::RefreshItem() should be changed too! 
 541     dc
.SetLogicalOrigin(1, 1); 
 543     wxRenderer 
*rend 
= renderer
->GetRenderer(); 
 546     const wxMenuGeometryInfo
& gi 
= m_menu
->GetGeometryInfo(); 
 547     for ( wxMenuItemList::Node 
*node 
= m_menu
->GetMenuItems().GetFirst(); 
 549           node 
= node
->GetNext() ) 
 551         wxMenuItem 
*item 
= node
->GetData(); 
 553         if ( item
->IsSeparator() ) 
 555             rend
->DrawMenuSeparator(dc
, y
, gi
); 
 557         else // not a separator 
 560             if ( item
->IsCheckable() ) 
 562                 flags 
|= wxCONTROL_CHECKABLE
; 
 564                 if ( item
->IsChecked() ) 
 566                     flags 
|= wxCONTROL_CHECKED
; 
 570             if ( !item
->IsEnabled() ) 
 571                 flags 
|= wxCONTROL_DISABLED
; 
 573             if ( item
->IsSubMenu() ) 
 574                 flags 
|= wxCONTROL_ISSUBMENU
; 
 576             if ( item 
== GetCurrentItem() ) 
 577                 flags 
|= wxCONTROL_SELECTED
; 
 585                      item
->GetAccelString(), 
 586                      // strangely enough, for unchecked item we use the 
 587                      // "checked" bitmap because this is the default one - this 
 588                      // explains this strange boolean expression 
 589                      item
->GetBitmap(!item
->IsCheckable() || item
->IsChecked()), 
 591                      item
->GetAccelIndex() 
 595         y 
+= item
->GetHeight(); 
 599 // ---------------------------------------------------------------------------- 
 600 // wxPopupMenuWindow actions 
 601 // ---------------------------------------------------------------------------- 
 603 void wxPopupMenuWindow::ClickItem(wxMenuItem 
*item
) 
 605     wxCHECK_RET( item
, _T("can't click NULL item") ); 
 607     wxASSERT_MSG( !item
->IsSeparator() && !item
->IsSubMenu(), 
 608                   _T("can't click this item") ); 
 610     wxMenu
* menu 
= m_menu
; 
 615     menu
->ClickItem(item
); 
 618 void wxPopupMenuWindow::OpenSubmenu(wxMenuItem 
*item
, InputMethod how
) 
 620     wxCHECK_RET( item
, _T("can't open NULL submenu") ); 
 622     wxMenu 
*submenu 
= item
->GetSubMenu(); 
 623     wxCHECK_RET( submenu
, _T("can only open submenus!") ); 
 625     // FIXME: should take into account the border width 
 626     submenu
->Popup(ClientToScreen(wxPoint(0, item
->GetPosition())), 
 627                    wxSize(m_menu
->GetGeometryInfo().GetSize().x
, 0), 
 628                    how 
== WithKeyboard 
/* preselect first item then */); 
 630     m_hasOpenSubMenu 
= TRUE
; 
 633 bool wxPopupMenuWindow::ActivateItem(wxMenuItem 
*item
, InputMethod how
) 
 635     // don't activate disabled items 
 636     if ( !item 
|| !item
->IsEnabled() ) 
 641     // normal menu items generate commands, submenus can be opened and 
 642     // the separators don't do anything 
 643     if ( item
->IsSubMenu() ) 
 645         OpenSubmenu(item
, how
); 
 647     else if ( !item
->IsSeparator() ) 
 651     else // separator, can't activate 
 659 // ---------------------------------------------------------------------------- 
 660 // wxPopupMenuWindow input handling 
 661 // ---------------------------------------------------------------------------- 
 663 bool wxPopupMenuWindow::ProcessLeftDown(wxMouseEvent
& event
) 
 665     // wxPopupWindowHandler dismisses the window when the mouse is clicked 
 666     // outside it which is usually just fine, but there is one case when we 
 667     // don't want to do it: if the mouse was clicked on the parent submenu item 
 668     // which opens this menu, so check for it 
 670     wxPoint pos 
= event
.GetPosition(); 
 671     if ( HitTest(pos
.x
, pos
.y
) == wxHT_WINDOW_OUTSIDE 
) 
 673         wxMenu 
*menu 
= m_menu
->GetParent(); 
 676             wxPopupMenuWindow 
*win 
= menu
->m_popupMenu
; 
 678             wxCHECK_MSG( win
, FALSE
, _T("parent menu not shown?") ); 
 680             pos 
= ClientToScreen(pos
); 
 681             if ( win
->GetMenuItemFromPoint(win
->ScreenToClient(pos
)) ) 
 686             //else: it is outside the parent menu as well, do dismiss this one 
 693 void wxPopupMenuWindow::OnLeftUp(wxMouseEvent
& event
) 
 695     wxMenuItemList::Node 
*node 
= GetMenuItemFromPoint(event
.GetPosition()); 
 698         ActivateItem(node
->GetData(), WithMouse
); 
 702 void wxPopupMenuWindow::OnMouseMove(wxMouseEvent
& event
) 
 704     const wxPoint pt 
= event
.GetPosition(); 
 706     // we need to ignore extra mouse events: example when this happens is when 
 707     // the mouse is on the menu and we open a submenu from keyboard - Windows 
 708     // then sends us a dummy mouse move event, we (correctly) determine that it 
 709     // happens in the parent menu and so immediately close the just opened 
 712     static wxPoint s_ptLast
; 
 713     wxPoint ptCur 
= ClientToScreen(pt
); 
 714     if ( ptCur 
== s_ptLast 
) 
 722     ProcessMouseMove(pt
); 
 727 void wxPopupMenuWindow::ProcessMouseMove(const wxPoint
& pt
) 
 729     wxMenuItemList::Node 
*node 
= GetMenuItemFromPoint(pt
); 
 731     // don't reset current to NULL here, we only do it when the mouse leaves 
 732     // the window (see below) 
 735         if ( node 
!= m_nodeCurrent 
) 
 739             wxMenuItem 
*item 
= GetCurrentItem(); 
 742                 OpenSubmenu(item
, WithMouse
); 
 745         //else: same item, nothing to do 
 747     else // not on an item 
 749         // the last open submenu forwards the mouse move messages to its 
 750         // parent, so if the mouse moves to another item of the parent menu, 
 751         // this menu is closed and this other item is selected - in the similar 
 752         // manner, the top menu forwards the mouse moves to the menubar which 
 753         // allows to select another top level menu by just moving the mouse 
 755         // we need to translate our client coords to the client coords of the 
 756         // window we forward this event to 
 757         wxPoint ptScreen 
= ClientToScreen(pt
); 
 759         // if the mouse is outside this menu, let the parent one to 
 761         wxMenu 
*menuParent 
= m_menu
->GetParent(); 
 764             wxPopupMenuWindow 
*win 
= menuParent
->m_popupMenu
; 
 766             // if we're shown, the parent menu must be also shown 
 767             wxCHECK_RET( win
, _T("parent menu is not shown?") ); 
 769             win
->ProcessMouseMove(win
->ScreenToClient(ptScreen
)); 
 771         else // no parent menu 
 773             wxMenuBar 
*menubar 
= m_menu
->GetMenuBar(); 
 776                 if ( menubar
->ProcessMouseEvent( 
 777                             menubar
->ScreenToClient(ptScreen
)) ) 
 779                     // menubar has closed this menu and opened another one, probably 
 784         //else: top level popup menu, no other processing to do 
 788 void wxPopupMenuWindow::OnMouseLeave(wxMouseEvent
& event
) 
 790     // due to the artefact of mouse events generation under MSW, we actually 
 791     // may get the mouse leave event after the menu had been already dismissed 
 792     // and calling ChangeCurrent() would then assert, so don't do it 
 795         // we shouldn't change the current them if our submenu is opened and 
 796         // mouse moved there, in this case the submenu is responsable for 
 799         if ( HasOpenSubmenu() ) 
 801             wxMenuItem 
*item 
= GetCurrentItem(); 
 802             wxCHECK_RET( CanOpen(item
), _T("where is our open submenu?") ); 
 804             wxPopupMenuWindow 
*win 
= item
->GetSubMenu()->m_popupMenu
; 
 805             wxCHECK_RET( win
, _T("submenu is opened but not shown?") ); 
 807             // only handle this event if the mouse is not inside the submenu 
 808             wxPoint pt 
= ClientToScreen(event
.GetPosition()); 
 810                 win
->HitTest(win
->ScreenToClient(pt
)) == wxHT_WINDOW_OUTSIDE
; 
 814             // this menu is the last opened 
 827 void wxPopupMenuWindow::OnKeyDown(wxKeyEvent
& event
) 
 829     if ( !ProcessKeyDown(event
.GetKeyCode()) ) 
 835 bool wxPopupMenuWindow::ProcessKeyDown(int key
) 
 837     wxMenuItem 
*item 
= GetCurrentItem(); 
 839     // first let the opened submenu to have it (no test for IsEnabled() here, 
 840     // the keys navigate even in a disabled submenu if we had somehow managed 
 841     // to open it inspit of this) 
 842     if ( HasOpenSubmenu() ) 
 844         wxCHECK_MSG( CanOpen(item
), FALSE
, 
 845                      _T("has open submenu but another item selected?") ); 
 847         if ( item
->GetSubMenu()->ProcessKeyDown(key
) ) 
 851     bool processed 
= TRUE
; 
 853     // handle the up/down arrows, home, end, esc and return here, pass the 
 854     // left/right arrows to the menu bar except when the right arrow can be 
 855     // used to open a submenu 
 859             // if we're not a top level menu, close us, else leave this to the 
 861             if ( !m_menu
->GetParent() ) 
 870             // close just this menu 
 872             HandleDismiss(FALSE
); 
 876             processed 
= ActivateItem(item
); 
 880             ChangeCurrent(m_menu
->GetMenuItems().GetFirst()); 
 884             ChangeCurrent(m_menu
->GetMenuItems().GetLast()); 
 890                 bool up 
= key 
== WXK_UP
; 
 892                 wxMenuItemList::Node 
*nodeStart 
= up 
? GetPrevNode() 
 895                 while ( node 
&& node
->GetData()->IsSeparator() ) 
 897                     node 
= up 
? GetPrevNode(node
) : GetNextNode(node
); 
 899                     if ( node 
== nodeStart 
) 
 901                         // nothing but separators and disabled items in this 
 919             // don't try to reopen an already opened menu 
 920             if ( !HasOpenSubmenu() && CanOpen(item
) ) 
 931             // look for the menu item starting with this letter 
 932             if ( wxIsalnum(key
) ) 
 934                 // we want to start from the item after this one because 
 935                 // if we're already on the item with the given accel we want to 
 936                 // go to the next one, not to stay in place 
 937                 wxMenuItemList::Node 
*nodeStart 
= GetNextNode(); 
 939                 // do we have more than one item with this accel? 
 940                 bool notUnique 
= FALSE
; 
 942                 // translate everything to lower case before comparing 
 943                 wxChar chAccel 
= wxTolower(key
); 
 945                 // loop through all items searching for the item with this 
 947                 wxMenuItemList::Node 
*node 
= nodeStart
, 
 951                     item 
= node
->GetData(); 
 953                     int idxAccel 
= item
->GetAccelIndex(); 
 954                     if ( idxAccel 
!= -1 && 
 955                          wxTolower(item
->GetLabel()[(size_t)idxAccel
]) 
 958                         // ok, found an item with this accel 
 961                             // store it but continue searching as we need to 
 962                             // know if it's the only item with this accel or if 
 966                         else // we already had found such item 
 970                             // no need to continue further, we won't find 
 971                             // anything we don't already know 
 976                     // we want to iterate over all items wrapping around if 
 978                     node 
= GetNextNode(node
); 
 979                     if ( node 
== nodeStart 
) 
 981                         // we've seen all nodes 
 988                     item 
= nodeFound
->GetData(); 
 990                     // go to this item anyhow 
 991                     ChangeCurrent(nodeFound
); 
 993                     if ( !notUnique 
&& item
->IsEnabled() ) 
 995                         // unique item with this accel - activate it 
 996                         processed 
= ActivateItem(item
); 
 998                     //else: just select it but don't activate as the user might 
 999                     //      have wanted to activate another item 
1001                     // skip "processed = FALSE" below 
1012 // ---------------------------------------------------------------------------- 
1014 // ---------------------------------------------------------------------------- 
1029 // ---------------------------------------------------------------------------- 
1030 // wxMenu and wxMenuGeometryInfo 
1031 // ---------------------------------------------------------------------------- 
1033 wxMenuGeometryInfo::~wxMenuGeometryInfo() 
1037 const wxMenuGeometryInfo
& wxMenu::GetGeometryInfo() const 
1043             wxConstCast(this, wxMenu
)->m_geometry 
= 
1044                 m_popupMenu
->GetRenderer()->GetMenuGeometry(m_popupMenu
, *this); 
1048             wxFAIL_MSG( _T("can't get geometry without window") ); 
1055 void wxMenu::InvalidateGeometryInfo() 
1064 // ---------------------------------------------------------------------------- 
1065 // wxMenu adding/removing items 
1066 // ---------------------------------------------------------------------------- 
1068 void wxMenu::OnItemAdded(wxMenuItem 
*item
) 
1070     InvalidateGeometryInfo(); 
1074 #endif // wxUSE_ACCEL 
1076     // the submenus of a popup menu should have the same invoking window as it 
1078     if ( m_invokingWindow 
&& item
->IsSubMenu() ) 
1080         item
->GetSubMenu()->SetInvokingWindow(m_invokingWindow
); 
1084 bool wxMenu::DoAppend(wxMenuItem 
*item
) 
1086     if ( !wxMenuBase::DoAppend(item
) ) 
1094 bool wxMenu::DoInsert(size_t pos
, wxMenuItem 
*item
) 
1096     if ( !wxMenuBase::DoInsert(pos
, item
) ) 
1104 wxMenuItem 
*wxMenu::DoRemove(wxMenuItem 
*item
) 
1106     wxMenuItem 
*itemOld 
= wxMenuBase::DoRemove(item
); 
1110         InvalidateGeometryInfo(); 
1113         RemoveAccelFor(item
); 
1114 #endif // wxUSE_ACCEL 
1120 // ---------------------------------------------------------------------------- 
1121 // wxMenu attaching/detaching 
1122 // ---------------------------------------------------------------------------- 
1124 void wxMenu::Attach(wxMenuBarBase 
*menubar
) 
1126     wxMenuBase::Attach(menubar
); 
1128     wxCHECK_RET( m_menuBar
, _T("menubar can't be NULL after attaching") ); 
1130     // unfortunately, we can't use m_menuBar->GetEventHandler() here because, 
1131     // if the menubar is currently showing a menu, its event handler is a 
1132     // temporary one installed by wxPopupWindow and so will disappear soon any 
1133     // any attempts to use it from the newly attached menu would result in a 
1136     // so we use the menubar itself, even if it's a pity as it means we can't 
1137     // redirect all menu events by changing the menubar handler (FIXME) 
1138     SetNextHandler(m_menuBar
); 
1141 void wxMenu::Detach() 
1143     wxMenuBase::Detach(); 
1146 // ---------------------------------------------------------------------------- 
1147 // wxMenu misc functions 
1148 // ---------------------------------------------------------------------------- 
1150 wxWindow 
*wxMenu::GetRootWindow() const 
1154         // simple case - a normal menu attached to the menubar 
1158     // we're a popup menu but the trouble is that only the top level popup menu 
1159     // has a pointer to the invoking window, so we must walk up the menu chain 
1161     wxWindow 
*win 
= GetInvokingWindow(); 
1164         // we already have it 
1168     wxMenu 
*menu 
= GetParent(); 
1171         // We are a submenu of a menu of a menubar 
1172         if (menu
->GetMenuBar()) 
1173            return menu
->GetMenuBar(); 
1175         win 
= menu
->GetInvokingWindow(); 
1179         menu 
= menu
->GetParent(); 
1182     // we're probably going to crash in the caller anyhow, but try to detect 
1183     // this error as soon as possible 
1184     wxASSERT_MSG( win
, _T("menu without any associated window?") ); 
1186     // also remember it in this menu so that we don't have to search for it the 
1188     wxConstCast(this, wxMenu
)->m_invokingWindow 
= win
; 
1193 wxRenderer 
*wxMenu::GetRenderer() const 
1195     // we're going to crash without renderer! 
1196     wxCHECK_MSG( m_popupMenu
, NULL
, _T("neither popup nor menubar menu?") ); 
1198     return m_popupMenu
->GetRenderer(); 
1201 void wxMenu::RefreshItem(wxMenuItem 
*item
) 
1203     // the item geometry changed, so our might have changed as well 
1204     InvalidateGeometryInfo(); 
1208         // this would be a bug in IsShown() 
1209         wxCHECK_RET( m_popupMenu
, _T("must have popup window if shown!") ); 
1211         // recalc geometry to update the item height and such 
1212         (void)GetGeometryInfo(); 
1214         m_popupMenu
->RefreshItem(item
); 
1218 // ---------------------------------------------------------------------------- 
1219 // wxMenu showing and hiding 
1220 // ---------------------------------------------------------------------------- 
1222 bool wxMenu::IsShown() const 
1224     return m_popupMenu 
&& m_popupMenu
->IsShown(); 
1227 void wxMenu::OnDismiss(bool dismissParent
) 
1231         // always notify the parent about submenu disappearance 
1232         wxPopupMenuWindow 
*win 
= m_menuParent
->m_popupMenu
; 
1235             win
->OnSubmenuDismiss(); 
1239             wxFAIL_MSG( _T("parent menu not shown?") ); 
1242         // and if we dismiss everything, propagate to parent 
1243         if ( dismissParent 
) 
1245             // dismissParent is recursive 
1246             m_menuParent
->Dismiss(); 
1247             m_menuParent
->OnDismiss(TRUE
); 
1250     else // no parent menu 
1252         // notify the menu bar if we're a top level menu 
1255             m_menuBar
->OnDismissMenu(dismissParent
); 
1259             wxCHECK_RET( m_invokingWindow
, _T("what kind of menu is this?") ); 
1261             m_invokingWindow
->DismissPopupMenu(); 
1263             // Why reset it here? We need it for sending the event to... 
1264             // SetInvokingWindow(NULL); 
1269 void wxMenu::Popup(const wxPoint
& pos
, const wxSize
& size
, bool selectFirst
) 
1271     // create the popup window if not done yet 
1274         m_popupMenu 
= new wxPopupMenuWindow(GetRootWindow(), this); 
1277     // select the first item unless disabled 
1280         m_popupMenu
->SelectFirst(); 
1283     // the geometry might have changed since the last time we were shown, so 
1285     m_popupMenu
->SetClientSize(GetGeometryInfo().GetSize()); 
1287     // position it as specified 
1288     m_popupMenu
->Position(pos
, size
); 
1290     // the menu can't have the focus itself (it is a Windows limitation), so 
1291     // always keep the focus at the originating window 
1292     wxWindow 
*focus 
= GetRootWindow(); 
1294     wxASSERT_MSG( focus
, _T("no window to keep focus on?") ); 
1297     m_popupMenu
->Popup(focus
); 
1300 void wxMenu::Dismiss() 
1302     wxCHECK_RET( IsShown(), _T("can't dismiss hidden menu") ); 
1304     m_popupMenu
->Dismiss(); 
1307 // ---------------------------------------------------------------------------- 
1308 // wxMenu event processing 
1309 // ---------------------------------------------------------------------------- 
1311 bool wxMenu::ProcessKeyDown(int key
) 
1313     wxCHECK_MSG( m_popupMenu
, FALSE
, 
1314                  _T("can't process key events if not shown") ); 
1316     return m_popupMenu
->ProcessKeyDown(key
); 
1319 bool wxMenu::ClickItem(wxMenuItem 
*item
) 
1322     if ( item
->IsCheckable() ) 
1324         // update the item state 
1325         isChecked 
= !item
->IsChecked(); 
1327         item
->Check(isChecked 
!= 0); 
1335     return SendEvent(item
->GetId(), isChecked
); 
1338 // ---------------------------------------------------------------------------- 
1339 // wxMenu accel support 
1340 // ---------------------------------------------------------------------------- 
1344 bool wxMenu::ProcessAccelEvent(const wxKeyEvent
& event
) 
1346     // do we have an item for this accel? 
1347     wxMenuItem 
*item 
= m_accelTable
.GetMenuItem(event
); 
1348     if ( item 
&& item
->IsEnabled() ) 
1350         return ClickItem(item
); 
1354     for ( wxMenuItemList::Node 
*node 
= GetMenuItems().GetFirst(); 
1356           node 
= node
->GetNext() ) 
1358         const wxMenuItem 
*item 
= node
->GetData(); 
1359         if ( item
->IsSubMenu() && item
->IsEnabled() ) 
1362             if ( item
->GetSubMenu()->ProcessAccelEvent(event
) ) 
1372 void wxMenu::AddAccelFor(wxMenuItem 
*item
) 
1374     wxAcceleratorEntry 
*accel 
= item
->GetAccel(); 
1377         accel
->SetMenuItem(item
); 
1379         m_accelTable
.Add(*accel
); 
1385 void wxMenu::RemoveAccelFor(wxMenuItem 
*item
) 
1387     wxAcceleratorEntry 
*accel 
= item
->GetAccel(); 
1390         m_accelTable
.Remove(*accel
); 
1396 #endif // wxUSE_ACCEL 
1398 // ---------------------------------------------------------------------------- 
1399 // wxMenuItem construction 
1400 // ---------------------------------------------------------------------------- 
1402 wxMenuItem::wxMenuItem(wxMenu 
*parentMenu
, 
1404                        const wxString
& text
, 
1405                        const wxString
& help
, 
1408           : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
) 
1416 wxMenuItem::~wxMenuItem() 
1420 // ---------------------------------------------------------------------------- 
1421 // wxMenuItemBase methods implemented here 
1422 // ---------------------------------------------------------------------------- 
1425 wxMenuItem 
*wxMenuItemBase::New(wxMenu 
*parentMenu
, 
1427                                 const wxString
& name
, 
1428                                 const wxString
& help
, 
1432     return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
); 
1436 wxString 
wxMenuItemBase::GetLabelFromText(const wxString
& text
) 
1438     return wxStripMenuCodes(text
); 
1441 // ---------------------------------------------------------------------------- 
1442 // wxMenuItem operations 
1443 // ---------------------------------------------------------------------------- 
1445 void wxMenuItem::NotifyMenu() 
1447     m_parentMenu
->RefreshItem(this); 
1450 void wxMenuItem::UpdateAccelInfo() 
1452     m_indexAccel 
= wxControl::FindAccelIndex(m_text
); 
1454     // will be empty if the text contains no TABs - ok 
1455     m_strAccel 
= m_text
.AfterFirst(_T('\t')); 
1458 void wxMenuItem::SetText(const wxString
& text
) 
1460     if ( text 
!= m_text 
) 
1462         // first call the base class version to change m_text 
1463         wxMenuItemBase::SetText(text
); 
1471 void wxMenuItem::SetCheckable(bool checkable
) 
1473     if ( checkable 
!= IsCheckable() ) 
1475         wxMenuItemBase::SetCheckable(checkable
); 
1481 void wxMenuItem::SetBitmaps(const wxBitmap
& bmpChecked
, 
1482                             const wxBitmap
& bmpUnchecked
) 
1484     m_bmpChecked 
= bmpChecked
; 
1485     m_bmpUnchecked 
= bmpUnchecked
; 
1490 void wxMenuItem::Enable(bool enable
) 
1492     if ( enable 
!= m_isEnabled 
) 
1494         wxMenuItemBase::Enable(enable
); 
1500 void wxMenuItem::Check(bool check
) 
1502     if ( check 
!= m_isChecked 
) 
1504         wxMenuItemBase::Check(check
); 
1510 // ---------------------------------------------------------------------------- 
1511 // wxMenuBar creation 
1512 // ---------------------------------------------------------------------------- 
1514 void wxMenuBar::Init() 
1522     m_shouldShowMenu 
= FALSE
; 
1524     m_windowStyle 
|= wxNO_FULL_REPAINT_ON_RESIZE
; 
1527 void wxMenuBar::Attach(wxFrame 
*frame
) 
1529     // maybe you really wanted to call Detach()? 
1530     wxCHECK_RET( frame
, _T("wxMenuBar::Attach(NULL) called") ); 
1532     wxMenuBarBase::Attach(frame
); 
1536         // reparent if necessary 
1537         if ( m_frameLast 
!= frame 
) 
1542         // show it back - was hidden by Detach() 
1545     else // not created yet, do it now 
1547         // we have no way to return the error from here anyhow :-( 
1548         (void)Create(frame
, -1); 
1550         SetCursor(wxCURSOR_ARROW
); 
1552         SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT
)); 
1554         // calculate and set our height (it won't be changed any more) 
1555         SetSize(-1, GetBestSize().y
); 
1558     // remember the last frame which had us to avoid unnecessarily reparenting 
1560     m_frameLast 
= frame
; 
1563 void wxMenuBar::Detach() 
1565     // don't delete the window because we may be reattached later, just hide it 
1571     wxMenuBarBase::Detach(); 
1574 wxMenuBar::~wxMenuBar() 
1578 // ---------------------------------------------------------------------------- 
1579 // wxMenuBar adding/removing items 
1580 // ---------------------------------------------------------------------------- 
1582 bool wxMenuBar::Append(wxMenu 
*menu
, const wxString
& title
) 
1584     return Insert(GetCount(), menu
, title
); 
1587 bool wxMenuBar::Insert(size_t pos
, wxMenu 
*menu
, const wxString
& title
) 
1589     if ( !wxMenuBarBase::Insert(pos
, menu
, title
) ) 
1592     wxMenuInfo 
*info 
= new wxMenuInfo(title
); 
1593     m_menuInfos
.Insert(info
, pos
); 
1595     RefreshAllItemsAfter(pos
); 
1600 wxMenu 
*wxMenuBar::Replace(size_t pos
, wxMenu 
*menu
, const wxString
& title
) 
1602     wxMenu 
*menuOld 
= wxMenuBarBase::Replace(pos
, menu
, title
); 
1606         wxMenuInfo
& info 
= m_menuInfos
[pos
]; 
1608         info
.SetLabel(title
); 
1610         // even if the old menu was disabled, the new one is not any more 
1613         // even if we change only this one, the new label has different width, 
1614         // so we need to refresh everything beyond this item as well 
1615         RefreshAllItemsAfter(pos
); 
1621 wxMenu 
*wxMenuBar::Remove(size_t pos
) 
1623     wxMenu 
*menuOld 
= wxMenuBarBase::Remove(pos
); 
1627         m_menuInfos
.RemoveAt(pos
); 
1629         // this doesn't happen too often, so don't try to be too smart - just 
1630         // refresh everything 
1637 // ---------------------------------------------------------------------------- 
1638 // wxMenuBar top level menus access 
1639 // ---------------------------------------------------------------------------- 
1641 wxCoord 
wxMenuBar::GetItemWidth(size_t pos
) const 
1643     return m_menuInfos
[pos
].GetWidth(wxConstCast(this, wxMenuBar
)); 
1646 void wxMenuBar::EnableTop(size_t pos
, bool enable
) 
1648     wxCHECK_RET( pos 
< GetCount(), _T("invalid index in EnableTop") ); 
1650     if ( enable 
!= m_menuInfos
[pos
].IsEnabled() ) 
1652         m_menuInfos
[pos
].SetEnabled(enable
); 
1656     //else: nothing to do 
1659 bool wxMenuBar::IsEnabledTop(size_t pos
) const 
1661     wxCHECK_MSG( pos 
< GetCount(), FALSE
, _T("invalid index in IsEnabledTop") ); 
1663     return m_menuInfos
[pos
].IsEnabled(); 
1666 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
) 
1668     wxCHECK_RET( pos 
< GetCount(), _T("invalid index in EnableTop") ); 
1670     if ( label 
!= m_menuInfos
[pos
].GetLabel() ) 
1672         m_menuInfos
[pos
].SetLabel(label
); 
1676     //else: nothing to do 
1679 wxString 
wxMenuBar::GetLabelTop(size_t pos
) const 
1681     wxCHECK_MSG( pos 
< GetCount(), _T(""), _T("invalid index in GetLabelTop") ); 
1683     return m_menuInfos
[pos
].GetLabel(); 
1686 // ---------------------------------------------------------------------------- 
1687 // wxMenuBar drawing 
1688 // ---------------------------------------------------------------------------- 
1690 void wxMenuBar::RefreshAllItemsAfter(size_t pos
) 
1694         // no need to refresh if nothing is shown yet 
1698     wxRect rect 
= GetItemRect(pos
); 
1699     rect
.width 
= GetClientSize().x 
- rect
.x
; 
1703 void wxMenuBar::RefreshItem(size_t pos
) 
1705     wxCHECK_RET( pos 
!= (size_t)-1, 
1706                  _T("invalid item in wxMenuBar::RefreshItem") ); 
1710         // no need to refresh if nothing is shown yet 
1714     RefreshRect(GetItemRect(pos
)); 
1717 void wxMenuBar::DoDraw(wxControlRenderer 
*renderer
) 
1719     wxDC
& dc 
= renderer
->GetDC(); 
1720     dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
)); 
1722     // redraw only the items which must be redrawn 
1724     // we don't have to use GetUpdateClientRect() here because our client rect 
1725     // is the same as total one 
1726     wxRect rectUpdate 
= GetUpdateRegion().GetBox(); 
1728     int flagsMenubar 
= GetStateFlags(); 
1732     rect
.height 
= GetClientSize().y
; 
1735     size_t count 
= GetCount(); 
1736     for ( size_t n 
= 0; n 
< count
; n
++ ) 
1738         if ( x 
> rectUpdate
.GetRight() ) 
1740             // all remaining items are to the right of rectUpdate 
1745         rect
.width 
= GetItemWidth(n
); 
1747         if ( x 
< rectUpdate
.x 
) 
1749             // this item is still to the left of rectUpdate 
1753         int flags 
= flagsMenubar
; 
1754         if ( m_current 
!= -1 && n 
== (size_t)m_current 
) 
1756             flags 
|= wxCONTROL_SELECTED
; 
1759         if ( !IsEnabledTop(n
) ) 
1761             flags 
|= wxCONTROL_DISABLED
; 
1764         GetRenderer()->DrawMenuBarItem
 
1768                             m_menuInfos
[n
].GetLabel(), 
1770                             m_menuInfos
[n
].GetAccelIndex() 
1775 // ---------------------------------------------------------------------------- 
1776 // wxMenuBar geometry 
1777 // ---------------------------------------------------------------------------- 
1779 wxRect 
wxMenuBar::GetItemRect(size_t pos
) const 
1781     wxASSERT_MSG( pos 
< GetCount(), _T("invalid menu bar item index") ); 
1782     wxASSERT_MSG( IsCreated(), _T("can't call this method yet") ); 
1787     rect
.height 
= GetClientSize().y
; 
1789     for ( size_t n 
= 0; n 
< pos
; n
++ ) 
1791         rect
.x 
+= GetItemWidth(n
); 
1794     rect
.width 
= GetItemWidth(pos
); 
1799 wxSize 
wxMenuBar::DoGetBestClientSize() const 
1802     if ( GetMenuCount() > 0 ) 
1804         wxClientDC 
dc(wxConstCast(this, wxMenuBar
)); 
1805         dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
)); 
1806         dc
.GetTextExtent(GetLabelTop(0), &size
.x
, &size
.y
); 
1808         // adjust for the renderer we use 
1809         size 
= GetRenderer()->GetMenuBarItemSize(size
); 
1811     else // empty menubar 
1817     // the width is arbitrary, of course, for horizontal menubar 
1823 int wxMenuBar::GetMenuFromPoint(const wxPoint
& pos
) const 
1825     if ( pos
.x 
< 0 || pos
.y 
< 0 || pos
.y 
> GetClientSize().y 
) 
1830     size_t count 
= GetCount(); 
1831     for ( size_t item 
= 0; item 
< count
; item
++ ) 
1833         x 
+= GetItemWidth(item
); 
1841     // to the right of the last menu item 
1845 // ---------------------------------------------------------------------------- 
1846 // wxMenuBar menu operations 
1847 // ---------------------------------------------------------------------------- 
1849 void wxMenuBar::SelectMenu(size_t pos
) 
1852     wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::SelectMenu")); 
1858 void wxMenuBar::DoSelectMenu(size_t pos
) 
1860     wxCHECK_RET( pos 
< GetCount(), _T("invalid menu index in DoSelectMenu") ); 
1862     int posOld 
= m_current
; 
1868         // close the previous menu 
1869         if ( IsShowingMenu() ) 
1871             // restore m_shouldShowMenu flag after DismissMenu() which resets 
1873             bool old 
= m_shouldShowMenu
; 
1877             m_shouldShowMenu 
= old
; 
1880         RefreshItem((size_t)posOld
); 
1886 void wxMenuBar::PopupMenu(size_t pos
) 
1888     wxCHECK_RET( pos 
< GetCount(), _T("invalid menu index in PopupCurrentMenu") ); 
1895 // ---------------------------------------------------------------------------- 
1896 // wxMenuBar input handing 
1897 // ---------------------------------------------------------------------------- 
1900    Note that wxMenuBar doesn't use wxInputHandler but handles keyboard and 
1901    mouse in the same way under all platforms. This is because it doesn't derive 
1902    from wxControl (which works with input handlers) but directly from wxWindow. 
1904    Also, menu bar input handling is rather simple, so maybe it's not really 
1905    worth making it themeable - at least I've decided against doing it now as it 
1906    would merging the changes back into trunk more difficult. But it still could 
1907    be done later if really needed. 
1910 void wxMenuBar::OnKillFocus(wxFocusEvent
& event
) 
1912     if ( m_current 
!= -1 ) 
1914         RefreshItem((size_t)m_current
); 
1922 void wxMenuBar::OnLeftDown(wxMouseEvent
& event
) 
1930     else // we didn't have mouse capture, capture it now 
1932         m_current 
= GetMenuFromPoint(event
.GetPosition()); 
1933         if ( m_current 
== -1 ) 
1935             // unfortunately, we can't prevent wxMSW from giving us the focus, 
1936             // so we can only give it back 
1941                 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::OnLeftDown")); 
1944             // show it as selected 
1945             RefreshItem((size_t)m_current
); 
1948             PopupCurrentMenu(FALSE 
/* don't select first item - as Windows does */); 
1953 void wxMenuBar::OnMouseMove(wxMouseEvent
& event
) 
1957         (void)ProcessMouseEvent(event
.GetPosition()); 
1965 bool wxMenuBar::ProcessMouseEvent(const wxPoint
& pt
) 
1967     // a hack to ignore the extra mouse events MSW sends us: this is similar to 
1968     // wxUSE_MOUSEEVENT_HACK in wxWin itself but it isn't enough for us here as 
1969     // we get the messages from different windows (old and new popup menus for 
1972     static wxPoint s_ptLast
; 
1973     if ( pt 
== s_ptLast 
) 
1981     int currentNew 
= GetMenuFromPoint(pt
); 
1982     if ( (currentNew 
== -1) || (currentNew 
== m_current
) ) 
1987     // select the new active item 
1988     DoSelectMenu(currentNew
); 
1990     // show the menu if we know that we should, even if we hadn't been showing 
1991     // it before (this may happen if the previous menu was disabled) 
1992     if ( m_shouldShowMenu 
&& !m_menuShown
) 
1994         // open the new menu if the old one we closed had been opened 
1995         PopupCurrentMenu(FALSE 
/* don't select first item - as Windows does */); 
2001 void wxMenuBar::OnKeyDown(wxKeyEvent
& event
) 
2003     // ensure that we have a current item - we might not have it if we're 
2004     // given the focus with Alt or F10 press (and under GTK+ the menubar 
2005     // somehow gets the keyboard events even when it doesn't have focus...) 
2006     if ( m_current 
== -1 ) 
2008         if ( !HasCapture() ) 
2012         else // we do have capture 
2014             // we always maintain a valid current item while we're in modal 
2015             // state (i.e. have the capture) 
2016             wxFAIL_MSG( _T("how did we manage to lose current item?") ); 
2022     int key 
= event
.GetKeyCode(); 
2024     // first let the menu have it 
2025     if ( IsShowingMenu() && m_menuShown
->ProcessKeyDown(key
) ) 
2030     // cycle through the menu items when left/right arrows are pressed and open 
2031     // the menu when up/down one is 
2035             // Alt must be processed at wxWindow level too 
2040             // remove the selection and give the focus away 
2041             if ( m_current 
!= -1 ) 
2043                 if ( IsShowingMenu() ) 
2055                 size_t count 
= GetCount(); 
2058                     // the item won't change anyhow 
2061                 //else: otherwise, it will 
2063                 // remember if we were showing a menu - if we did, we should 
2064                 // show the new menu after changing the item 
2065                 bool wasMenuOpened 
= IsShowingMenu(); 
2066                 if ( wasMenuOpened 
) 
2071                 // cast is safe as we tested for -1 above 
2072                 size_t currentNew 
= (size_t)m_current
; 
2074                 if ( key 
== WXK_LEFT 
) 
2076                     if ( currentNew
-- == 0 ) 
2077                         currentNew 
= count 
- 1; 
2081                     if ( ++currentNew 
== count 
) 
2085                 DoSelectMenu(currentNew
); 
2087                 if ( wasMenuOpened 
) 
2102             // letters open the corresponding menu 
2105                 int idxFound 
= FindNextItemForAccel(m_current
, key
, &unique
); 
2107                 if ( idxFound 
!= -1 ) 
2109                     if ( IsShowingMenu() ) 
2114                     DoSelectMenu((size_t)idxFound
); 
2116                     // if the item is not unique, just select it but don't 
2117                     // activate as the user might have wanted to activate 
2120                     // also, don't try to open a disabled menu 
2121                     if ( unique 
&& IsEnabledTop((size_t)idxFound
) ) 
2127                     // skip the "event.Skip()" below 
2136 // ---------------------------------------------------------------------------- 
2137 // wxMenuBar accel handling 
2138 // ---------------------------------------------------------------------------- 
2140 int wxMenuBar::FindNextItemForAccel(int idxStart
, int key
, bool *unique
) const 
2142     if ( !wxIsalnum(key
) ) 
2144         // we only support letters/digits as accels 
2148     // do we have more than one item with this accel? 
2152     // translate everything to lower case before comparing 
2153     wxChar chAccel 
= wxTolower(key
); 
2155     // the index of the item with this accel 
2158     // loop through all items searching for the item with this 
2159     // accel starting at the item after the current one 
2160     int count 
= GetCount(); 
2161     int n 
= idxStart 
== -1 ? 0 : idxStart 
+ 1; 
2172         const wxMenuInfo
& info 
= m_menuInfos
[n
]; 
2174         int idxAccel 
= info
.GetAccelIndex(); 
2175         if ( idxAccel 
!= -1 && 
2176              wxTolower(info
.GetLabel()[(size_t)idxAccel
]) 
2179             // ok, found an item with this accel 
2180             if ( idxFound 
== -1 ) 
2182                 // store it but continue searching as we need to 
2183                 // know if it's the only item with this accel or if 
2187             else // we already had found such item 
2192                 // no need to continue further, we won't find 
2193                 // anything we don't already know 
2198         // we want to iterate over all items wrapping around if 
2206         if ( n 
== idxStart 
) 
2208             // we've seen all items 
2218 bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent
& event
) 
2221     for ( wxMenuList::Node 
*node 
= m_menus
.GetFirst(); 
2223           node 
= node
->GetNext(), n
++ ) 
2225         // accels of the items in the disabled menus shouldn't work 
2226         if ( m_menuInfos
[n
].IsEnabled() ) 
2228             if ( node
->GetData()->ProcessAccelEvent(event
) ) 
2230                 // menu processed it 
2240 #endif // wxUSE_ACCEL 
2242 // ---------------------------------------------------------------------------- 
2243 // wxMenuBar menus showing 
2244 // ---------------------------------------------------------------------------- 
2246 void wxMenuBar::PopupCurrentMenu(bool selectFirst
) 
2248     wxCHECK_RET( m_current 
!= -1, _T("no menu to popup") ); 
2250     // forgot to call DismissMenu()? 
2251     wxASSERT_MSG( !m_menuShown
, _T("shouldn't show two menus at once!") ); 
2253     // in any case, we should show it - even if we won't 
2254     m_shouldShowMenu 
= TRUE
; 
2256     if ( IsEnabledTop(m_current
) ) 
2258         // remember the menu we show 
2259         m_menuShown 
= GetMenu(m_current
); 
2261         // we don't show the menu at all if it has no items 
2262         if ( !m_menuShown
->IsEmpty() ) 
2264             // position it correctly: note that we must use screen coords and 
2265             // that we pass 0 as width to position the menu exactly below the 
2266             // item, not to the right of it 
2267             wxRect rectItem 
= GetItemRect(m_current
); 
2269             m_menuShown
->Popup(ClientToScreen(rectItem
.GetPosition()), 
2270                                wxSize(0, rectItem
.GetHeight()), 
2275             // reset it back as no menu is shown 
2279     //else: don't show disabled menu 
2282 void wxMenuBar::DismissMenu() 
2284     wxCHECK_RET( m_menuShown
, _T("can't dismiss menu if none is shown") ); 
2286     m_menuShown
->Dismiss(); 
2290 void wxMenuBar::OnDismissMenu(bool dismissMenuBar
) 
2292     m_shouldShowMenu 
= FALSE
; 
2294     if ( dismissMenuBar 
) 
2300 void wxMenuBar::OnDismiss() 
2304         wxLogTrace(_T("mousecapture"), _T("Releasing mouse from wxMenuBar::OnDismiss")); 
2305         GetCapture()->ReleaseMouse(); 
2308     if ( m_current 
!= -1 ) 
2310         size_t current 
= m_current
; 
2313         RefreshItem(current
); 
2319 void wxMenuBar::GiveAwayFocus() 
2321     GetFrame()->SetFocus(); 
2324 // ---------------------------------------------------------------------------- 
2325 // popup menu support 
2326 // ---------------------------------------------------------------------------- 
2328 wxEventLoop 
*wxWindow::ms_evtLoopPopup 
= NULL
; 
2330 bool wxWindow::DoPopupMenu(wxMenu 
*menu
, int x
, int y
) 
2332     wxCHECK_MSG( !ms_evtLoopPopup
, FALSE
, 
2333                  _T("can't show more than one popup menu at a time") ); 
2336     // we need to change the cursor before showing the menu as, apparently, no 
2337     // cursor changes took place while the mouse is captured 
2338     wxCursor cursorOld 
= GetCursor(); 
2339     SetCursor(wxCURSOR_ARROW
); 
2343     // flash any delayed log messages before showing the menu, otherwise it 
2344     // could be dismissed (because it would lose focus) immediately after being 
2346     wxLog::FlushActive(); 
2348     // some controls update themselves from OnIdle() call - let them do it 
2350     wxTheApp
->ProcessEvent(event
); 
2352     // if the window hadn't been refreshed yet, the menu can adversely affect 
2353     // its next OnPaint() handler execution - i.e. scrolled window refresh 
2354     // logic breaks then as it scrolls part of the menu which hadn't been there 
2355     // when the update event was generated into view 
2359     menu
->SetInvokingWindow(this); 
2361     // wxLogDebug( "Name of invoking window %s", menu->GetInvokingWindow()->GetName().c_str() ); 
2363     menu
->Popup(ClientToScreen(wxPoint(x
, y
)), wxSize(0, 0)); 
2365     // this is not very useful if the menu was popped up because of the mouse 
2366     // click but I think it is nice to do when it appears because of a key 
2367     // press (i.e. Windows menu key) 
2369     // Windows itself doesn't do it, but IMHO this is nice 
2372     // we have to redirect all keyboard input to the menu temporarily 
2373     PushEventHandler(new wxMenuKbdRedirector(menu
)); 
2375     // enter the local modal loop 
2376     ms_evtLoopPopup 
= new wxEventLoop
; 
2377     ms_evtLoopPopup
->Run(); 
2379     delete ms_evtLoopPopup
; 
2380     ms_evtLoopPopup 
= NULL
; 
2382     // remove the handler 
2383     PopEventHandler(TRUE 
/* delete it */); 
2385     menu
->SetInvokingWindow(NULL
); 
2388     SetCursor(cursorOld
); 
2394 void wxWindow::DismissPopupMenu() 
2396     wxCHECK_RET( ms_evtLoopPopup
, _T("no popup menu shown") ); 
2398     ms_evtLoopPopup
->Exit(); 
2401 #endif // wxUSE_MENUS