]> git.saurik.com Git - wxWidgets.git/blame - src/univ/menu.cpp
This commit was generated by cvs2svn to compensate for changes in r30285,
[wxWidgets.git] / src / univ / menu.cpp
CommitLineData
1e6feb95
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: univ/menu.cpp
3// Purpose: wxMenuItem, wxMenu and wxMenuBar implementation
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 25.08.00
7// RCS-ID: $Id$
442b35b5 8// Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
65571936 9// Licence: wxWindows licence
1e6feb95
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
a3870b2f
VZ
21 #pragma implementation "univmenuitem.h"
22 #pragma implementation "univmenu.h"
1e6feb95
VZ
23#endif
24
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/dynarray.h"
33 #include "wx/control.h" // for FindAccelIndex()
34 #include "wx/menu.h"
35 #include "wx/settings.h"
36 #include "wx/accel.h"
37 #include "wx/log.h"
38#endif // WX_PRECOMP
39
40#if wxUSE_MENUS
41
42#include "wx/popupwin.h"
43#include "wx/evtloop.h"
8cb172b4
JS
44#include "wx/dcclient.h"
45#include "wx/frame.h"
1e6feb95
VZ
46
47#include "wx/univ/renderer.h"
48
49#ifdef __WXMSW__
50 #include "wx/msw/private.h"
51#endif // __WXMSW__
52
53// ----------------------------------------------------------------------------
54// wxMenuInfo contains all extra information about top level menus we need
55// ----------------------------------------------------------------------------
56
57class WXDLLEXPORT wxMenuInfo
58{
59public:
60 // ctor
61 wxMenuInfo(const wxString& text)
62 {
63 SetLabel(text);
64 SetEnabled();
65 }
66
67 // modifiers
68
69 void SetLabel(const wxString& text)
70 {
71 // remember the accel char (may be -1 if none)
72 m_indexAccel = wxControl::FindAccelIndex(text, &m_label);
73
74 // calculate the width later, after the menu bar is created
75 m_width = 0;
76 }
77
a290fa5a 78 void SetEnabled(bool enabled = true) { m_isEnabled = enabled; }
1e6feb95
VZ
79
80 // accessors
81
82 const wxString& GetLabel() const { return m_label; }
83 bool IsEnabled() const { return m_isEnabled; }
84 wxCoord GetWidth(wxMenuBar *menubar) const
85 {
86 if ( !m_width )
87 {
88 wxConstCast(this, wxMenuInfo)->CalcWidth(menubar);
89 }
90
91 return m_width;
92 }
93
94 int GetAccelIndex() const { return m_indexAccel; }
95
96private:
97 void CalcWidth(wxMenuBar *menubar)
98 {
99 wxSize size;
100 wxClientDC dc(menubar);
a756f210 101 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
1e6feb95
VZ
102 dc.GetTextExtent(m_label, &size.x, &size.y);
103
104 // adjust for the renderer we use and store the width
105 m_width = menubar->GetRenderer()->GetMenuBarItemSize(size).x;
106 }
107
108 wxString m_label;
109 wxCoord m_width;
110 int m_indexAccel;
111 bool m_isEnabled;
112};
113
114#include "wx/arrimpl.cpp"
115
116WX_DEFINE_OBJARRAY(wxMenuInfoArray);
117
118// ----------------------------------------------------------------------------
119// wxPopupMenuWindow: a popup window showing a menu
120// ----------------------------------------------------------------------------
121
122class wxPopupMenuWindow : public wxPopupTransientWindow
123{
124public:
125 wxPopupMenuWindow(wxWindow *parent, wxMenu *menu);
e441e1f4 126
ea1ad04b 127 ~wxPopupMenuWindow();
1e6feb95
VZ
128
129 // override the base class version to select the first item initially
130 virtual void Popup(wxWindow *focus = NULL);
131
132 // override the base class version to dismiss any open submenus
133 virtual void Dismiss();
134
135 // notify the menu when the window disappears from screen
136 virtual void OnDismiss();
137
138 // called when a submenu is dismissed
a290fa5a 139 void OnSubmenuDismiss() { m_hasOpenSubMenu = false; }
1e6feb95
VZ
140
141 // get the currently selected item (may be NULL)
142 wxMenuItem *GetCurrentItem() const
143 {
144 return m_nodeCurrent ? m_nodeCurrent->GetData() : NULL;
145 }
146
147 // find the menu item at given position
ac32ba44 148 wxMenuItemList::compatibility_iterator GetMenuItemFromPoint(const wxPoint& pt) const;
1e6feb95
VZ
149
150 // refresh the given item
151 void RefreshItem(wxMenuItem *item);
152
153 // preselect the first item
154 void SelectFirst() { SetCurrent(m_menu->GetMenuItems().GetFirst()); }
155
a290fa5a 156 // process the key event, return true if done
1e6feb95
VZ
157 bool ProcessKeyDown(int key);
158
159 // process mouse move event
160 void ProcessMouseMove(const wxPoint& pt);
161
162 // don't dismiss the popup window if the parent menu was clicked
163 virtual bool ProcessLeftDown(wxMouseEvent& event);
164
165protected:
166 // how did we perform this operation?
167 enum InputMethod
168 {
169 WithKeyboard,
170 WithMouse
171 };
172
173 // draw the menu inside this window
174 virtual void DoDraw(wxControlRenderer *renderer);
175
176 // event handlers
177 void OnLeftUp(wxMouseEvent& event);
178 void OnMouseMove(wxMouseEvent& event);
179 void OnMouseLeave(wxMouseEvent& event);
180 void OnKeyDown(wxKeyEvent& event);
181
182 // reset the current item and node
183 void ResetCurrent();
184
185 // set the current node and item withotu refreshing anything
ac32ba44 186 void SetCurrent(wxMenuItemList::compatibility_iterator node);
8a39593e 187 virtual bool SetCurrent(bool doit = true){return wxPopupTransientWindow::SetCurrent(doit);};
1e6feb95
VZ
188
189 // change the current item refreshing the old and new items
ac32ba44 190 void ChangeCurrent(wxMenuItemList::compatibility_iterator node);
1e6feb95
VZ
191
192 // activate item, i.e. call either ClickItem() or OpenSubmenu() depending
a290fa5a 193 // on what it is, return true if something was done (i.e. it's not a
1e6feb95
VZ
194 // separator...)
195 bool ActivateItem(wxMenuItem *item, InputMethod how = WithKeyboard);
196
197 // send the event about the item click
198 void ClickItem(wxMenuItem *item);
199
200 // show the submenu for this item
201 void OpenSubmenu(wxMenuItem *item, InputMethod how = WithKeyboard);
202
203 // can this tiem be opened?
204 bool CanOpen(wxMenuItem *item)
205 {
206 return item && item->IsEnabled() && item->IsSubMenu();
207 }
208
209 // dismiss the menu and all parent menus too
210 void DismissAndNotify();
211
212 // react to dimissing this menu and also dismiss the parent if
213 // dismissParent
214 void HandleDismiss(bool dismissParent);
215
216 // do we have an open submenu?
217 bool HasOpenSubmenu() const { return m_hasOpenSubMenu; }
218
219 // get previous node after the current one
ac32ba44 220 wxMenuItemList::compatibility_iterator GetPrevNode() const;
1e6feb95
VZ
221
222 // get previous node before the given one, wrapping if it's the first one
ac32ba44 223 wxMenuItemList::compatibility_iterator GetPrevNode(wxMenuItemList::compatibility_iterator node) const;
1e6feb95
VZ
224
225 // get next node after the current one
ac32ba44 226 wxMenuItemList::compatibility_iterator GetNextNode() const;
1e6feb95
VZ
227
228 // get next node after the given one, wrapping if it's the last one
ac32ba44 229 wxMenuItemList::compatibility_iterator GetNextNode(wxMenuItemList::compatibility_iterator node) const;
1e6feb95
VZ
230
231private:
232 // the menu we show
233 wxMenu *m_menu;
234
235 // the menu node corresponding to the current item
ac32ba44 236 wxMenuItemList::compatibility_iterator m_nodeCurrent;
1e6feb95
VZ
237
238 // do we currently have an opened submenu?
239 bool m_hasOpenSubMenu;
240
241 DECLARE_EVENT_TABLE()
242};
243
244// ----------------------------------------------------------------------------
245// wxMenuKbdRedirector: an event handler which redirects kbd input to wxMenu
246// ----------------------------------------------------------------------------
247
248class wxMenuKbdRedirector : public wxEvtHandler
249{
250public:
251 wxMenuKbdRedirector(wxMenu *menu) { m_menu = menu; }
252
253 virtual bool ProcessEvent(wxEvent& event)
254 {
255 if ( event.GetEventType() == wxEVT_KEY_DOWN )
256 {
257 return m_menu->ProcessKeyDown(((wxKeyEvent &)event).GetKeyCode());
258 }
259 else
260 {
a290fa5a 261 // return false;
e441e1f4 262
1e6feb95
VZ
263 return wxEvtHandler::ProcessEvent(event);
264 }
265 }
266
267private:
268 wxMenu *m_menu;
269};
270
271// ----------------------------------------------------------------------------
272// wxWin macros
273// ----------------------------------------------------------------------------
274
275IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
276IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow)
277IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
278
279BEGIN_EVENT_TABLE(wxPopupMenuWindow, wxPopupTransientWindow)
280 EVT_KEY_DOWN(wxPopupMenuWindow::OnKeyDown)
281
282 EVT_LEFT_UP(wxPopupMenuWindow::OnLeftUp)
283 EVT_MOTION(wxPopupMenuWindow::OnMouseMove)
284 EVT_LEAVE_WINDOW(wxPopupMenuWindow::OnMouseLeave)
285END_EVENT_TABLE()
286
287BEGIN_EVENT_TABLE(wxMenuBar, wxMenuBarBase)
288 EVT_KILL_FOCUS(wxMenuBar::OnKillFocus)
289
290 EVT_KEY_DOWN(wxMenuBar::OnKeyDown)
291
292 EVT_LEFT_DOWN(wxMenuBar::OnLeftDown)
293 EVT_MOTION(wxMenuBar::OnMouseMove)
294END_EVENT_TABLE()
295
296// ============================================================================
297// implementation
298// ============================================================================
299
300// ----------------------------------------------------------------------------
301// wxPopupMenuWindow
302// ----------------------------------------------------------------------------
303
304wxPopupMenuWindow::wxPopupMenuWindow(wxWindow *parent, wxMenu *menu)
305{
306 m_menu = menu;
a290fa5a 307 m_hasOpenSubMenu = false;
1e6feb95
VZ
308
309 ResetCurrent();
310
311 (void)Create(parent, wxBORDER_RAISED);
312
313 SetCursor(wxCURSOR_ARROW);
314}
315
ea1ad04b
RR
316wxPopupMenuWindow::~wxPopupMenuWindow()
317{
90b18154
JS
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.
322
323 m_menu->m_popupMenu = NULL;
ea1ad04b
RR
324}
325
1e6feb95
VZ
326// ----------------------------------------------------------------------------
327// wxPopupMenuWindow current item/node handling
328// ----------------------------------------------------------------------------
329
330void wxPopupMenuWindow::ResetCurrent()
331{
ac32ba44
MB
332#if wxUSE_STL
333 SetCurrent(wxMenuItemList::compatibility_iterator());
334#else
8a39593e 335 SetCurrent((wxwxMenuItemListNode *)NULL);
ac32ba44 336#endif
1e6feb95
VZ
337}
338
ac32ba44 339void wxPopupMenuWindow::SetCurrent(wxMenuItemList::compatibility_iterator node)
1e6feb95
VZ
340{
341 m_nodeCurrent = node;
342}
343
ac32ba44 344void wxPopupMenuWindow::ChangeCurrent(wxMenuItemList::compatibility_iterator node)
1e6feb95
VZ
345{
346 if ( node != m_nodeCurrent )
347 {
ac32ba44 348 wxMenuItemList::compatibility_iterator nodeOldCurrent = m_nodeCurrent;
23d8bb2c
VZ
349
350 m_nodeCurrent = node;
351
352 if ( nodeOldCurrent )
1e6feb95 353 {
23d8bb2c 354 wxMenuItem *item = nodeOldCurrent->GetData();
1e6feb95
VZ
355 wxCHECK_RET( item, _T("no current item?") );
356
357 // if it was the currently opened menu, close it
358 if ( item->IsSubMenu() && item->GetSubMenu()->IsShown() )
359 {
360 item->GetSubMenu()->Dismiss();
361 OnSubmenuDismiss();
362 }
363
364 RefreshItem(item);
365 }
366
1e6feb95
VZ
367 if ( m_nodeCurrent )
368 RefreshItem(m_nodeCurrent->GetData());
369 }
370}
371
ac32ba44 372wxMenuItemList::compatibility_iterator wxPopupMenuWindow::GetPrevNode() const
1e6feb95 373{
86171954
VZ
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();
1e6feb95
VZ
377}
378
e441e1f4 379wxMenuItemList::compatibility_iterator
ac32ba44 380wxPopupMenuWindow::GetPrevNode(wxMenuItemList::compatibility_iterator node) const
1e6feb95
VZ
381{
382 if ( node )
383 {
384 node = node->GetPrevious();
385 if ( !node )
386 {
387 node = m_menu->GetMenuItems().GetLast();
388 }
389 }
390 //else: the menu is empty
391
392 return node;
393}
394
ac32ba44 395wxMenuItemList::compatibility_iterator wxPopupMenuWindow::GetNextNode() const
1e6feb95 396{
86171954
VZ
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();
1e6feb95
VZ
400}
401
e441e1f4 402wxMenuItemList::compatibility_iterator
ac32ba44 403wxPopupMenuWindow::GetNextNode(wxMenuItemList::compatibility_iterator node) const
1e6feb95
VZ
404{
405 if ( node )
406 {
407 node = node->GetNext();
408 if ( !node )
409 {
410 node = m_menu->GetMenuItems().GetFirst();
411 }
412 }
413 //else: the menu is empty
414
415 return node;
416}
417
418// ----------------------------------------------------------------------------
419// wxPopupMenuWindow popup/dismiss
420// ----------------------------------------------------------------------------
421
422void wxPopupMenuWindow::Popup(wxWindow *focus)
423{
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") );
428
429 wxPopupTransientWindow::Popup(focus);
430
431#ifdef __WXMSW__
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();
436 if ( menuParent )
437 {
438 wxPopupMenuWindow *win = menuParent->m_popupMenu;
439
440 // if we're shown, the parent menu must be also shown
441 wxCHECK_RET( win, _T("parent menu is not shown?") );
442
443 if ( !::SetWindowPos(GetHwndOf(win), GetHwnd(),
444 0, 0, 0, 0,
445 SWP_NOMOVE | SWP_NOSIZE | SWP_NOREDRAW) )
446 {
447 wxLogLastError(_T("SetWindowPos(HWND_TOP)"));
448 }
449
450 Refresh();
451 }
452#endif // __WXMSW__
453}
454
455void wxPopupMenuWindow::Dismiss()
456{
457 if ( HasOpenSubmenu() )
458 {
459 wxMenuItem *item = GetCurrentItem();
460 wxCHECK_RET( item && item->IsSubMenu(), _T("where is our open submenu?") );
461
462 wxPopupMenuWindow *win = item->GetSubMenu()->m_popupMenu;
463 wxCHECK_RET( win, _T("opened submenu is not opened?") );
464
465 win->Dismiss();
466 OnSubmenuDismiss();
467 }
468
469 wxPopupTransientWindow::Dismiss();
470}
471
472void wxPopupMenuWindow::OnDismiss()
473{
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
a290fa5a 476 HandleDismiss(true);
1e6feb95
VZ
477}
478
479void wxPopupMenuWindow::HandleDismiss(bool dismissParent)
480{
481 ResetCurrent();
482
483 m_menu->OnDismiss(dismissParent);
484}
485
486void wxPopupMenuWindow::DismissAndNotify()
487{
488 Dismiss();
a290fa5a 489 HandleDismiss(true);
1e6feb95
VZ
490}
491
492// ----------------------------------------------------------------------------
493// wxPopupMenuWindow geometry
494// ----------------------------------------------------------------------------
495
ac32ba44 496wxMenuItemList::compatibility_iterator
1e6feb95
VZ
497wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint& pt) const
498{
499 // we only use the y coord normally, but still check x in case the point is
500 // outside the window completely
501 if ( wxWindow::HitTest(pt) == wxHT_WINDOW_INSIDE )
502 {
503 wxCoord y = 0;
ac32ba44 504 for ( wxMenuItemList::compatibility_iterator node = m_menu->GetMenuItems().GetFirst();
1e6feb95
VZ
505 node;
506 node = node->GetNext() )
507 {
508 wxMenuItem *item = node->GetData();
509 y += item->GetHeight();
510 if ( y > pt.y )
511 {
512 // found
513 return node;
514 }
515 }
516 }
517
ac32ba44
MB
518#if wxUSE_STL
519 return wxMenuItemList::compatibility_iterator();
520#else
1e6feb95 521 return NULL;
ac32ba44 522#endif
1e6feb95
VZ
523}
524
525// ----------------------------------------------------------------------------
526// wxPopupMenuWindow drawing
527// ----------------------------------------------------------------------------
528
529void wxPopupMenuWindow::RefreshItem(wxMenuItem *item)
530{
531 wxCHECK_RET( item, _T("can't refresh NULL item") );
532
533 wxASSERT_MSG( IsShown(), _T("can't refresh menu which is not shown") );
534
535 // FIXME: -1 here because of SetLogicalOrigin(1, 1) in DoDraw()
536 RefreshRect(wxRect(0, item->GetPosition() - 1,
537 m_menu->GetGeometryInfo().GetSize().x, item->GetHeight()));
538}
539
540void wxPopupMenuWindow::DoDraw(wxControlRenderer *renderer)
541{
542 // no clipping so far - do we need it? I don't think so as the menu is
543 // never partially covered as it is always on top of everything
544
545 wxDC& dc = renderer->GetDC();
a756f210 546 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
1e6feb95
VZ
547
548 // FIXME: this should be done in the renderer, however when it is fixed
549 // wxPopupMenuWindow::RefreshItem() should be changed too!
550 dc.SetLogicalOrigin(1, 1);
551
552 wxRenderer *rend = renderer->GetRenderer();
553
554 wxCoord y = 0;
555 const wxMenuGeometryInfo& gi = m_menu->GetGeometryInfo();
ac32ba44 556 for ( wxMenuItemList::compatibility_iterator node = m_menu->GetMenuItems().GetFirst();
1e6feb95
VZ
557 node;
558 node = node->GetNext() )
559 {
560 wxMenuItem *item = node->GetData();
561
562 if ( item->IsSeparator() )
563 {
564 rend->DrawMenuSeparator(dc, y, gi);
565 }
566 else // not a separator
567 {
568 int flags = 0;
569 if ( item->IsCheckable() )
570 {
571 flags |= wxCONTROL_CHECKABLE;
572
573 if ( item->IsChecked() )
574 {
575 flags |= wxCONTROL_CHECKED;
576 }
577 }
578
579 if ( !item->IsEnabled() )
580 flags |= wxCONTROL_DISABLED;
581
582 if ( item->IsSubMenu() )
583 flags |= wxCONTROL_ISSUBMENU;
584
585 if ( item == GetCurrentItem() )
586 flags |= wxCONTROL_SELECTED;
587
dba6b4f8
JS
588 wxBitmap bmp;
589
590 if ( !item->IsEnabled() )
591 {
592 bmp = item->GetDisabledBitmap();
593 }
594
595 if ( !bmp.Ok() )
596 {
597 // strangely enough, for unchecked item we use the
598 // "checked" bitmap because this is the default one - this
599 // explains this strange boolean expression
600 bmp = item->GetBitmap(!item->IsCheckable() || item->IsChecked());
601 }
602
1e6feb95
VZ
603 rend->DrawMenuItem
604 (
605 dc,
606 y,
607 gi,
608 item->GetLabel(),
609 item->GetAccelString(),
dba6b4f8 610 bmp,
1e6feb95
VZ
611 flags,
612 item->GetAccelIndex()
613 );
614 }
615
616 y += item->GetHeight();
617 }
618}
619
620// ----------------------------------------------------------------------------
621// wxPopupMenuWindow actions
622// ----------------------------------------------------------------------------
623
624void wxPopupMenuWindow::ClickItem(wxMenuItem *item)
625{
626 wxCHECK_RET( item, _T("can't click NULL item") );
627
628 wxASSERT_MSG( !item->IsSeparator() && !item->IsSubMenu(),
629 _T("can't click this item") );
630
1f752a22 631 wxMenu* menu = m_menu;
1e6feb95
VZ
632
633 // close all menus
634 DismissAndNotify();
e441e1f4 635
1f752a22 636 menu->ClickItem(item);
1e6feb95
VZ
637}
638
639void wxPopupMenuWindow::OpenSubmenu(wxMenuItem *item, InputMethod how)
640{
641 wxCHECK_RET( item, _T("can't open NULL submenu") );
642
643 wxMenu *submenu = item->GetSubMenu();
644 wxCHECK_RET( submenu, _T("can only open submenus!") );
645
646 // FIXME: should take into account the border width
647 submenu->Popup(ClientToScreen(wxPoint(0, item->GetPosition())),
648 wxSize(m_menu->GetGeometryInfo().GetSize().x, 0),
649 how == WithKeyboard /* preselect first item then */);
650
a290fa5a 651 m_hasOpenSubMenu = true;
1e6feb95
VZ
652}
653
654bool wxPopupMenuWindow::ActivateItem(wxMenuItem *item, InputMethod how)
655{
656 // don't activate disabled items
657 if ( !item || !item->IsEnabled() )
658 {
a290fa5a 659 return false;
1e6feb95
VZ
660 }
661
662 // normal menu items generate commands, submenus can be opened and
663 // the separators don't do anything
664 if ( item->IsSubMenu() )
665 {
666 OpenSubmenu(item, how);
667 }
668 else if ( !item->IsSeparator() )
669 {
670 ClickItem(item);
671 }
672 else // separator, can't activate
673 {
a290fa5a 674 return false;
1e6feb95
VZ
675 }
676
a290fa5a 677 return true;
1e6feb95
VZ
678}
679
680// ----------------------------------------------------------------------------
681// wxPopupMenuWindow input handling
682// ----------------------------------------------------------------------------
683
684bool wxPopupMenuWindow::ProcessLeftDown(wxMouseEvent& event)
685{
686 // wxPopupWindowHandler dismisses the window when the mouse is clicked
687 // outside it which is usually just fine, but there is one case when we
688 // don't want to do it: if the mouse was clicked on the parent submenu item
689 // which opens this menu, so check for it
690
691 wxPoint pos = event.GetPosition();
692 if ( HitTest(pos.x, pos.y) == wxHT_WINDOW_OUTSIDE )
693 {
694 wxMenu *menu = m_menu->GetParent();
695 if ( menu )
696 {
697 wxPopupMenuWindow *win = menu->m_popupMenu;
698
a290fa5a 699 wxCHECK_MSG( win, false, _T("parent menu not shown?") );
e441e1f4 700
1e6feb95
VZ
701 pos = ClientToScreen(pos);
702 if ( win->GetMenuItemFromPoint(win->ScreenToClient(pos)) )
703 {
704 // eat the event
a290fa5a 705 return true;
1e6feb95
VZ
706 }
707 //else: it is outside the parent menu as well, do dismiss this one
708 }
709 }
710
a290fa5a 711 return false;
1e6feb95
VZ
712}
713
714void wxPopupMenuWindow::OnLeftUp(wxMouseEvent& event)
715{
ac32ba44 716 wxMenuItemList::compatibility_iterator node = GetMenuItemFromPoint(event.GetPosition());
1e6feb95
VZ
717 if ( node )
718 {
719 ActivateItem(node->GetData(), WithMouse);
720 }
721}
722
723void wxPopupMenuWindow::OnMouseMove(wxMouseEvent& event)
724{
725 const wxPoint pt = event.GetPosition();
726
727 // we need to ignore extra mouse events: example when this happens is when
728 // the mouse is on the menu and we open a submenu from keyboard - Windows
729 // then sends us a dummy mouse move event, we (correctly) determine that it
730 // happens in the parent menu and so immediately close the just opened
731 // submenu!
732#ifdef __WXMSW__
733 static wxPoint s_ptLast;
734 wxPoint ptCur = ClientToScreen(pt);
735 if ( ptCur == s_ptLast )
736 {
737 return;
738 }
739
740 s_ptLast = ptCur;
741#endif // __WXMSW__
742
743 ProcessMouseMove(pt);
744
745 event.Skip();
746}
747
748void wxPopupMenuWindow::ProcessMouseMove(const wxPoint& pt)
749{
ac32ba44 750 wxMenuItemList::compatibility_iterator node = GetMenuItemFromPoint(pt);
1e6feb95
VZ
751
752 // don't reset current to NULL here, we only do it when the mouse leaves
753 // the window (see below)
754 if ( node )
755 {
756 if ( node != m_nodeCurrent )
757 {
758 ChangeCurrent(node);
759
760 wxMenuItem *item = GetCurrentItem();
761 if ( CanOpen(item) )
762 {
763 OpenSubmenu(item, WithMouse);
764 }
765 }
766 //else: same item, nothing to do
767 }
768 else // not on an item
769 {
770 // the last open submenu forwards the mouse move messages to its
771 // parent, so if the mouse moves to another item of the parent menu,
772 // this menu is closed and this other item is selected - in the similar
773 // manner, the top menu forwards the mouse moves to the menubar which
774 // allows to select another top level menu by just moving the mouse
775
776 // we need to translate our client coords to the client coords of the
777 // window we forward this event to
778 wxPoint ptScreen = ClientToScreen(pt);
779
780 // if the mouse is outside this menu, let the parent one to
781 // process it
782 wxMenu *menuParent = m_menu->GetParent();
783 if ( menuParent )
784 {
785 wxPopupMenuWindow *win = menuParent->m_popupMenu;
786
787 // if we're shown, the parent menu must be also shown
788 wxCHECK_RET( win, _T("parent menu is not shown?") );
789
790 win->ProcessMouseMove(win->ScreenToClient(ptScreen));
791 }
792 else // no parent menu
793 {
794 wxMenuBar *menubar = m_menu->GetMenuBar();
795 if ( menubar )
796 {
797 if ( menubar->ProcessMouseEvent(
798 menubar->ScreenToClient(ptScreen)) )
799 {
800 // menubar has closed this menu and opened another one, probably
801 return;
802 }
803 }
804 }
805 //else: top level popup menu, no other processing to do
806 }
807}
808
809void wxPopupMenuWindow::OnMouseLeave(wxMouseEvent& event)
810{
811 // due to the artefact of mouse events generation under MSW, we actually
812 // may get the mouse leave event after the menu had been already dismissed
813 // and calling ChangeCurrent() would then assert, so don't do it
814 if ( IsShown() )
815 {
816 // we shouldn't change the current them if our submenu is opened and
817 // mouse moved there, in this case the submenu is responsable for
818 // handling it
819 bool resetCurrent;
820 if ( HasOpenSubmenu() )
821 {
822 wxMenuItem *item = GetCurrentItem();
823 wxCHECK_RET( CanOpen(item), _T("where is our open submenu?") );
824
825 wxPopupMenuWindow *win = item->GetSubMenu()->m_popupMenu;
826 wxCHECK_RET( win, _T("submenu is opened but not shown?") );
827
828 // only handle this event if the mouse is not inside the submenu
829 wxPoint pt = ClientToScreen(event.GetPosition());
830 resetCurrent =
831 win->HitTest(win->ScreenToClient(pt)) == wxHT_WINDOW_OUTSIDE;
832 }
833 else
834 {
835 // this menu is the last opened
a290fa5a 836 resetCurrent = true;
1e6feb95
VZ
837 }
838
839 if ( resetCurrent )
840 {
ac32ba44
MB
841#if wxUSE_STL
842 ChangeCurrent(wxMenuItemList::compatibility_iterator());
843#else
1e6feb95 844 ChangeCurrent(NULL);
ac32ba44 845#endif
1e6feb95
VZ
846 }
847 }
848
849 event.Skip();
850}
851
852void wxPopupMenuWindow::OnKeyDown(wxKeyEvent& event)
853{
854 if ( !ProcessKeyDown(event.GetKeyCode()) )
855 {
856 event.Skip();
857 }
858}
859
860bool wxPopupMenuWindow::ProcessKeyDown(int key)
861{
862 wxMenuItem *item = GetCurrentItem();
863
864 // first let the opened submenu to have it (no test for IsEnabled() here,
865 // the keys navigate even in a disabled submenu if we had somehow managed
866 // to open it inspit of this)
867 if ( HasOpenSubmenu() )
868 {
a290fa5a 869 wxCHECK_MSG( CanOpen(item), false,
1e6feb95
VZ
870 _T("has open submenu but another item selected?") );
871
872 if ( item->GetSubMenu()->ProcessKeyDown(key) )
a290fa5a 873 return true;
1e6feb95
VZ
874 }
875
a290fa5a 876 bool processed = true;
1e6feb95
VZ
877
878 // handle the up/down arrows, home, end, esc and return here, pass the
879 // left/right arrows to the menu bar except when the right arrow can be
880 // used to open a submenu
881 switch ( key )
882 {
883 case WXK_LEFT:
884 // if we're not a top level menu, close us, else leave this to the
885 // menubar
886 if ( !m_menu->GetParent() )
887 {
a290fa5a 888 processed = false;
1e6feb95
VZ
889 break;
890 }
891
892 // fall through
893
894 case WXK_ESCAPE:
895 // close just this menu
896 Dismiss();
a290fa5a 897 HandleDismiss(false);
1e6feb95
VZ
898 break;
899
900 case WXK_RETURN:
901 processed = ActivateItem(item);
902 break;
903
904 case WXK_HOME:
905 ChangeCurrent(m_menu->GetMenuItems().GetFirst());
906 break;
907
908 case WXK_END:
909 ChangeCurrent(m_menu->GetMenuItems().GetLast());
910 break;
911
912 case WXK_UP:
913 case WXK_DOWN:
914 {
915 bool up = key == WXK_UP;
916
ac32ba44 917 wxMenuItemList::compatibility_iterator nodeStart = up ? GetPrevNode()
1e6feb95 918 : GetNextNode(),
ac32ba44 919 node = nodeStart;
1e6feb95
VZ
920 while ( node && node->GetData()->IsSeparator() )
921 {
922 node = up ? GetPrevNode(node) : GetNextNode(node);
923
924 if ( node == nodeStart )
925 {
926 // nothing but separators and disabled items in this
927 // menu, break out
ac32ba44
MB
928#if wxUSE_STL
929 node = wxMenuItemList::compatibility_iterator();
930#else
1e6feb95 931 node = NULL;
ac32ba44 932#endif
1e6feb95
VZ
933 }
934 }
935
936 if ( node )
937 {
938 ChangeCurrent(node);
939 }
940 else
941 {
a290fa5a 942 processed = false;
1e6feb95
VZ
943 }
944 }
945 break;
946
947 case WXK_RIGHT:
948 // don't try to reopen an already opened menu
949 if ( !HasOpenSubmenu() && CanOpen(item) )
950 {
951 OpenSubmenu(item);
952 }
953 else
954 {
a290fa5a 955 processed = false;
1e6feb95
VZ
956 }
957 break;
958
959 default:
960 // look for the menu item starting with this letter
961 if ( wxIsalnum(key) )
962 {
963 // we want to start from the item after this one because
964 // if we're already on the item with the given accel we want to
965 // go to the next one, not to stay in place
ac32ba44 966 wxMenuItemList::compatibility_iterator nodeStart = GetNextNode();
1e6feb95
VZ
967
968 // do we have more than one item with this accel?
a290fa5a 969 bool notUnique = false;
1e6feb95
VZ
970
971 // translate everything to lower case before comparing
972 wxChar chAccel = wxTolower(key);
973
974 // loop through all items searching for the item with this
975 // accel
ac32ba44
MB
976 wxMenuItemList::compatibility_iterator node = nodeStart,
977#if wxUSE_STL
978 nodeFound = wxMenuItemList::compatibility_iterator();
979#else
980 nodeFound = NULL;
981#endif
1e6feb95
VZ
982 for ( ;; )
983 {
984 item = node->GetData();
985
986 int idxAccel = item->GetAccelIndex();
987 if ( idxAccel != -1 &&
988 wxTolower(item->GetLabel()[(size_t)idxAccel])
989 == chAccel )
990 {
991 // ok, found an item with this accel
992 if ( !nodeFound )
993 {
994 // store it but continue searching as we need to
995 // know if it's the only item with this accel or if
996 // there are more
997 nodeFound = node;
998 }
999 else // we already had found such item
1000 {
a290fa5a 1001 notUnique = true;
1e6feb95
VZ
1002
1003 // no need to continue further, we won't find
1004 // anything we don't already know
1005 break;
1006 }
1007 }
1008
1009 // we want to iterate over all items wrapping around if
1010 // necessary
1011 node = GetNextNode(node);
1012 if ( node == nodeStart )
1013 {
1014 // we've seen all nodes
1015 break;
1016 }
1017 }
1018
1019 if ( nodeFound )
1020 {
1021 item = nodeFound->GetData();
1022
1023 // go to this item anyhow
1024 ChangeCurrent(nodeFound);
1025
1026 if ( !notUnique && item->IsEnabled() )
1027 {
1028 // unique item with this accel - activate it
1029 processed = ActivateItem(item);
1030 }
1031 //else: just select it but don't activate as the user might
1032 // have wanted to activate another item
1033
a290fa5a 1034 // skip "processed = false" below
1e6feb95
VZ
1035 break;
1036 }
1037 }
1038
a290fa5a 1039 processed = false;
1e6feb95
VZ
1040 }
1041
1042 return processed;
1043}
1044
1045// ----------------------------------------------------------------------------
1046// wxMenu
1047// ----------------------------------------------------------------------------
1048
1049void wxMenu::Init()
1050{
1051 m_geometry = NULL;
1052
1053 m_popupMenu = NULL;
6f7c5199
JS
1054
1055 m_startRadioGroup = -1;
1e6feb95
VZ
1056}
1057
1058wxMenu::~wxMenu()
1059{
1060 delete m_geometry;
1061 delete m_popupMenu;
1062}
1063
1064// ----------------------------------------------------------------------------
1065// wxMenu and wxMenuGeometryInfo
1066// ----------------------------------------------------------------------------
1067
1068wxMenuGeometryInfo::~wxMenuGeometryInfo()
1069{
1070}
1071
1072const wxMenuGeometryInfo& wxMenu::GetGeometryInfo() const
1073{
1074 if ( !m_geometry )
1075 {
1076 if ( m_popupMenu )
1077 {
1078 wxConstCast(this, wxMenu)->m_geometry =
1079 m_popupMenu->GetRenderer()->GetMenuGeometry(m_popupMenu, *this);
1080 }
1081 else
1082 {
1083 wxFAIL_MSG( _T("can't get geometry without window") );
1084 }
1085 }
1086
1087 return *m_geometry;
1088}
1089
1090void wxMenu::InvalidateGeometryInfo()
1091{
1092 if ( m_geometry )
1093 {
1094 delete m_geometry;
1095 m_geometry = NULL;
1096 }
1097}
1098
1099// ----------------------------------------------------------------------------
1100// wxMenu adding/removing items
1101// ----------------------------------------------------------------------------
1102
1103void wxMenu::OnItemAdded(wxMenuItem *item)
1104{
1105 InvalidateGeometryInfo();
1106
1107#if wxUSE_ACCEL
1108 AddAccelFor(item);
1109#endif // wxUSE_ACCEL
1110
1111 // the submenus of a popup menu should have the same invoking window as it
1112 // has
1113 if ( m_invokingWindow && item->IsSubMenu() )
1114 {
1115 item->GetSubMenu()->SetInvokingWindow(m_invokingWindow);
1116 }
1117}
1118
6f7c5199
JS
1119void wxMenu::EndRadioGroup()
1120{
1121 // we're not inside a radio group any longer
1122 m_startRadioGroup = -1;
1123}
1124
9add9367 1125wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
1e6feb95 1126{
6f7c5199
JS
1127 if ( item->GetKind() == wxITEM_RADIO )
1128 {
1129 int count = GetMenuItemCount();
1130
1131 if ( m_startRadioGroup == -1 )
1132 {
1133 // start a new radio group
1134 m_startRadioGroup = count;
1135
1136 // for now it has just one element
1137 item->SetAsRadioGroupStart();
1138 item->SetRadioGroupEnd(m_startRadioGroup);
6f7c5199
JS
1139 }
1140 else // extend the current radio group
1141 {
1142 // we need to update its end item
1143 item->SetRadioGroupStart(m_startRadioGroup);
ac32ba44 1144 wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup);
6f7c5199
JS
1145
1146 if ( node )
1147 {
1148 node->GetData()->SetRadioGroupEnd(count);
1149 }
1150 else
1151 {
1152 wxFAIL_MSG( _T("where is the radio group start item?") );
1153 }
1154 }
1155 }
1156 else // not a radio item
1157 {
1158 EndRadioGroup();
1159 }
1160
1e6feb95 1161 if ( !wxMenuBase::DoAppend(item) )
9add9367 1162 return NULL;
1e6feb95
VZ
1163
1164 OnItemAdded(item);
1165
9add9367 1166 return item;
1e6feb95
VZ
1167}
1168
9add9367 1169wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
1e6feb95
VZ
1170{
1171 if ( !wxMenuBase::DoInsert(pos, item) )
9add9367 1172 return NULL;
1e6feb95
VZ
1173
1174 OnItemAdded(item);
1175
9add9367 1176 return item;
1e6feb95
VZ
1177}
1178
1179wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
1180{
1181 wxMenuItem *itemOld = wxMenuBase::DoRemove(item);
1182
1183 if ( itemOld )
1184 {
1185 InvalidateGeometryInfo();
1186
1187#if wxUSE_ACCEL
1188 RemoveAccelFor(item);
1189#endif // wxUSE_ACCEL
1190 }
1191
1192 return itemOld;
1193}
1194
1195// ----------------------------------------------------------------------------
1196// wxMenu attaching/detaching
1197// ----------------------------------------------------------------------------
1198
1199void wxMenu::Attach(wxMenuBarBase *menubar)
1200{
1201 wxMenuBase::Attach(menubar);
1202
1203 wxCHECK_RET( m_menuBar, _T("menubar can't be NULL after attaching") );
1204
1205 // unfortunately, we can't use m_menuBar->GetEventHandler() here because,
1206 // if the menubar is currently showing a menu, its event handler is a
1207 // temporary one installed by wxPopupWindow and so will disappear soon any
1208 // any attempts to use it from the newly attached menu would result in a
1209 // crash
1210 //
1211 // so we use the menubar itself, even if it's a pity as it means we can't
1212 // redirect all menu events by changing the menubar handler (FIXME)
1213 SetNextHandler(m_menuBar);
1214}
1215
1216void wxMenu::Detach()
1217{
1218 wxMenuBase::Detach();
1219}
1220
1221// ----------------------------------------------------------------------------
1222// wxMenu misc functions
1223// ----------------------------------------------------------------------------
1224
1225wxWindow *wxMenu::GetRootWindow() const
1226{
4224f059 1227 if ( GetMenuBar() )
1e6feb95
VZ
1228 {
1229 // simple case - a normal menu attached to the menubar
4224f059 1230 return GetMenuBar();
1e6feb95
VZ
1231 }
1232
1233 // we're a popup menu but the trouble is that only the top level popup menu
1234 // has a pointer to the invoking window, so we must walk up the menu chain
1235 // if needed
1236 wxWindow *win = GetInvokingWindow();
1237 if ( win )
1238 {
1239 // we already have it
1240 return win;
1241 }
1242
1243 wxMenu *menu = GetParent();
1244 while ( menu )
1245 {
ea1ad04b
RR
1246 // We are a submenu of a menu of a menubar
1247 if (menu->GetMenuBar())
1248 return menu->GetMenuBar();
e441e1f4 1249
1e6feb95
VZ
1250 win = menu->GetInvokingWindow();
1251 if ( win )
1252 break;
1253
1254 menu = menu->GetParent();
1255 }
1256
1257 // we're probably going to crash in the caller anyhow, but try to detect
1258 // this error as soon as possible
1259 wxASSERT_MSG( win, _T("menu without any associated window?") );
e441e1f4 1260
1e6feb95
VZ
1261 // also remember it in this menu so that we don't have to search for it the
1262 // next time
1263 wxConstCast(this, wxMenu)->m_invokingWindow = win;
1264
1265 return win;
1266}
1267
1268wxRenderer *wxMenu::GetRenderer() const
1269{
1270 // we're going to crash without renderer!
1271 wxCHECK_MSG( m_popupMenu, NULL, _T("neither popup nor menubar menu?") );
1272
1273 return m_popupMenu->GetRenderer();
1274}
1275
1276void wxMenu::RefreshItem(wxMenuItem *item)
1277{
1278 // the item geometry changed, so our might have changed as well
1279 InvalidateGeometryInfo();
1280
1281 if ( IsShown() )
1282 {
1283 // this would be a bug in IsShown()
1284 wxCHECK_RET( m_popupMenu, _T("must have popup window if shown!") );
1285
1286 // recalc geometry to update the item height and such
1287 (void)GetGeometryInfo();
1288
1289 m_popupMenu->RefreshItem(item);
1290 }
1291}
1292
1293// ----------------------------------------------------------------------------
1294// wxMenu showing and hiding
1295// ----------------------------------------------------------------------------
1296
1297bool wxMenu::IsShown() const
1298{
1299 return m_popupMenu && m_popupMenu->IsShown();
1300}
1301
1302void wxMenu::OnDismiss(bool dismissParent)
1303{
1304 if ( m_menuParent )
1305 {
1306 // always notify the parent about submenu disappearance
1307 wxPopupMenuWindow *win = m_menuParent->m_popupMenu;
1308 if ( win )
1309 {
1310 win->OnSubmenuDismiss();
1311 }
1312 else
1313 {
1314 wxFAIL_MSG( _T("parent menu not shown?") );
1315 }
1316
1317 // and if we dismiss everything, propagate to parent
1318 if ( dismissParent )
1319 {
1320 // dismissParent is recursive
1321 m_menuParent->Dismiss();
a290fa5a 1322 m_menuParent->OnDismiss(true);
1e6feb95
VZ
1323 }
1324 }
1325 else // no parent menu
1326 {
1327 // notify the menu bar if we're a top level menu
1328 if ( m_menuBar )
1329 {
1330 m_menuBar->OnDismissMenu(dismissParent);
1331 }
1332 else // popup menu
1333 {
1334 wxCHECK_RET( m_invokingWindow, _T("what kind of menu is this?") );
1335
1336 m_invokingWindow->DismissPopupMenu();
e441e1f4 1337
ee351013
RR
1338 // Why reset it here? We need it for sending the event to...
1339 // SetInvokingWindow(NULL);
1e6feb95
VZ
1340 }
1341 }
1342}
1343
1344void wxMenu::Popup(const wxPoint& pos, const wxSize& size, bool selectFirst)
1345{
1346 // create the popup window if not done yet
1347 if ( !m_popupMenu )
1348 {
1349 m_popupMenu = new wxPopupMenuWindow(GetRootWindow(), this);
1350 }
1351
1352 // select the first item unless disabled
1353 if ( selectFirst )
1354 {
1355 m_popupMenu->SelectFirst();
1356 }
e441e1f4 1357
1e6feb95
VZ
1358 // the geometry might have changed since the last time we were shown, so
1359 // always resize
1360 m_popupMenu->SetClientSize(GetGeometryInfo().GetSize());
1361
1362 // position it as specified
1363 m_popupMenu->Position(pos, size);
1364
1365 // the menu can't have the focus itself (it is a Windows limitation), so
1366 // always keep the focus at the originating window
1367 wxWindow *focus = GetRootWindow();
1368
1369 wxASSERT_MSG( focus, _T("no window to keep focus on?") );
1370
1371 // and show it
1372 m_popupMenu->Popup(focus);
1373}
1374
1375void wxMenu::Dismiss()
1376{
1377 wxCHECK_RET( IsShown(), _T("can't dismiss hidden menu") );
1378
1379 m_popupMenu->Dismiss();
1380}
1381
1382// ----------------------------------------------------------------------------
1383// wxMenu event processing
1384// ----------------------------------------------------------------------------
1385
1386bool wxMenu::ProcessKeyDown(int key)
1387{
a290fa5a 1388 wxCHECK_MSG( m_popupMenu, false,
1e6feb95
VZ
1389 _T("can't process key events if not shown") );
1390
1391 return m_popupMenu->ProcessKeyDown(key);
1392}
1393
1394bool wxMenu::ClickItem(wxMenuItem *item)
1395{
1396 int isChecked;
1397 if ( item->IsCheckable() )
1398 {
1399 // update the item state
1400 isChecked = !item->IsChecked();
1401
1402 item->Check(isChecked != 0);
1403 }
1404 else
1405 {
1406 // not applicabled
1407 isChecked = -1;
1408 }
e441e1f4 1409
1e6feb95
VZ
1410 return SendEvent(item->GetId(), isChecked);
1411}
1412
1413// ----------------------------------------------------------------------------
1414// wxMenu accel support
1415// ----------------------------------------------------------------------------
1416
1417#if wxUSE_ACCEL
1418
1419bool wxMenu::ProcessAccelEvent(const wxKeyEvent& event)
1420{
1421 // do we have an item for this accel?
1422 wxMenuItem *item = m_accelTable.GetMenuItem(event);
1423 if ( item && item->IsEnabled() )
1424 {
1425 return ClickItem(item);
1426 }
1427
1428 // try our submenus
ac32ba44 1429 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
1e6feb95
VZ
1430 node;
1431 node = node->GetNext() )
1432 {
1433 const wxMenuItem *item = node->GetData();
1434 if ( item->IsSubMenu() && item->IsEnabled() )
1435 {
1436 // try its elements
1437 if ( item->GetSubMenu()->ProcessAccelEvent(event) )
1438 {
a290fa5a 1439 return true;
1e6feb95
VZ
1440 }
1441 }
1442 }
1443
a290fa5a 1444 return false;
1e6feb95
VZ
1445}
1446
1447void wxMenu::AddAccelFor(wxMenuItem *item)
1448{
1449 wxAcceleratorEntry *accel = item->GetAccel();
1450 if ( accel )
1451 {
1452 accel->SetMenuItem(item);
1453
1454 m_accelTable.Add(*accel);
1455
1456 delete accel;
1457 }
1458}
1459
1460void wxMenu::RemoveAccelFor(wxMenuItem *item)
1461{
1462 wxAcceleratorEntry *accel = item->GetAccel();
1463 if ( accel )
1464 {
1465 m_accelTable.Remove(*accel);
1466
1467 delete accel;
1468 }
1469}
1470
1471#endif // wxUSE_ACCEL
1472
1473// ----------------------------------------------------------------------------
1474// wxMenuItem construction
1475// ----------------------------------------------------------------------------
1476
1477wxMenuItem::wxMenuItem(wxMenu *parentMenu,
1478 int id,
1479 const wxString& text,
1480 const wxString& help,
d65c269b 1481 wxItemKind kind,
1e6feb95 1482 wxMenu *subMenu)
d65c269b 1483 : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu)
1e6feb95 1484{
1e6feb95 1485 m_posY =
a290fa5a 1486 m_height = wxDefaultCoord;
1e6feb95 1487
6f7c5199 1488 m_radioGroup.start = -1;
a290fa5a 1489 m_isRadioGroupStart = false;
6f7c5199 1490
dba6b4f8
JS
1491 m_bmpDisabled = wxNullBitmap;
1492
1e6feb95
VZ
1493 UpdateAccelInfo();
1494}
1495
1496wxMenuItem::~wxMenuItem()
1497{
1498}
1499
1500// ----------------------------------------------------------------------------
1501// wxMenuItemBase methods implemented here
1502// ----------------------------------------------------------------------------
1503
1504/* static */
1505wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
1506 int id,
1507 const wxString& name,
1508 const wxString& help,
e4052bd3 1509 wxItemKind kind,
1e6feb95
VZ
1510 wxMenu *subMenu)
1511{
e4052bd3 1512 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
1e6feb95
VZ
1513}
1514
1515/* static */
1516wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
1517{
1518 return wxStripMenuCodes(text);
1519}
1520
1521// ----------------------------------------------------------------------------
1522// wxMenuItem operations
1523// ----------------------------------------------------------------------------
1524
1525void wxMenuItem::NotifyMenu()
1526{
1527 m_parentMenu->RefreshItem(this);
1528}
1529
1530void wxMenuItem::UpdateAccelInfo()
1531{
1532 m_indexAccel = wxControl::FindAccelIndex(m_text);
1533
1534 // will be empty if the text contains no TABs - ok
1535 m_strAccel = m_text.AfterFirst(_T('\t'));
1536}
1537
1538void wxMenuItem::SetText(const wxString& text)
1539{
1540 if ( text != m_text )
1541 {
1542 // first call the base class version to change m_text
1543 wxMenuItemBase::SetText(text);
1544
1545 UpdateAccelInfo();
1546
1547 NotifyMenu();
1548 }
1549}
1550
1551void wxMenuItem::SetCheckable(bool checkable)
1552{
d65c269b 1553 if ( checkable != IsCheckable() )
1e6feb95
VZ
1554 {
1555 wxMenuItemBase::SetCheckable(checkable);
1556
1557 NotifyMenu();
1558 }
1559}
1560
1561void wxMenuItem::SetBitmaps(const wxBitmap& bmpChecked,
1562 const wxBitmap& bmpUnchecked)
1563{
1564 m_bmpChecked = bmpChecked;
1565 m_bmpUnchecked = bmpUnchecked;
1566
1567 NotifyMenu();
1568}
1569
1570void wxMenuItem::Enable(bool enable)
1571{
1572 if ( enable != m_isEnabled )
1573 {
1574 wxMenuItemBase::Enable(enable);
1575
1576 NotifyMenu();
1577 }
1578}
1579
1580void wxMenuItem::Check(bool check)
1581{
6f7c5199
JS
1582 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
1583
1584 if ( m_isChecked == check )
1585 return;
1586
1587 if ( GetKind() == wxITEM_RADIO )
1e6feb95 1588 {
6f7c5199
JS
1589 // it doesn't make sense to uncheck a radio item - what would this do?
1590 if ( !check )
1591 return;
1e6feb95 1592
6f7c5199
JS
1593 // get the index of this item in the menu
1594 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
1595 int pos = items.IndexOf(this);
1596 wxCHECK_RET( pos != wxNOT_FOUND,
1597 _T("menuitem not found in the menu items list?") );
1598
1599 // get the radio group range
1600 int start,
1601 end;
1602
1603 if ( m_isRadioGroupStart )
1604 {
1605 // we already have all information we need
1606 start = pos;
1607 end = m_radioGroup.end;
1608 }
1609 else // next radio group item
1610 {
1611 // get the radio group end from the start item
1612 start = m_radioGroup.start;
1613 end = items.Item(start)->GetData()->m_radioGroup.end;
1614 }
1615
1616 // also uncheck all the other items in this radio group
ac32ba44 1617 wxMenuItemList::compatibility_iterator node = items.Item(start);
6f7c5199
JS
1618 for ( int n = start; n <= end && node; n++ )
1619 {
1620 if ( n != pos )
1621 {
a290fa5a 1622 node->GetData()->m_isChecked = false;
6f7c5199
JS
1623 }
1624 node = node->GetNext();
1625 }
1e6feb95 1626 }
6f7c5199
JS
1627
1628 wxMenuItemBase::Check(check);
1629
1630 NotifyMenu();
1631}
1632
1633// radio group stuff
1634// -----------------
1635
1636void wxMenuItem::SetAsRadioGroupStart()
1637{
a290fa5a 1638 m_isRadioGroupStart = true;
6f7c5199
JS
1639}
1640
1641void wxMenuItem::SetRadioGroupStart(int start)
1642{
1643 wxASSERT_MSG( !m_isRadioGroupStart,
1644 _T("should only be called for the next radio items") );
1645
1646 m_radioGroup.start = start;
1647}
1648
1649void wxMenuItem::SetRadioGroupEnd(int end)
1650{
1651 wxASSERT_MSG( m_isRadioGroupStart,
1652 _T("should only be called for the first radio item") );
1653
1654 m_radioGroup.end = end;
1e6feb95
VZ
1655}
1656
1657// ----------------------------------------------------------------------------
1658// wxMenuBar creation
1659// ----------------------------------------------------------------------------
1660
1661void wxMenuBar::Init()
1662{
1663 m_frameLast = NULL;
1664
1665 m_current = -1;
1666
1667 m_menuShown = NULL;
1668
a290fa5a 1669 m_shouldShowMenu = false;
1e6feb95
VZ
1670}
1671
1672void wxMenuBar::Attach(wxFrame *frame)
1673{
1674 // maybe you really wanted to call Detach()?
1675 wxCHECK_RET( frame, _T("wxMenuBar::Attach(NULL) called") );
1676
1677 wxMenuBarBase::Attach(frame);
1678
1679 if ( IsCreated() )
1680 {
1681 // reparent if necessary
1682 if ( m_frameLast != frame )
1683 {
1684 Reparent(frame);
1685 }
1686
1687 // show it back - was hidden by Detach()
1688 Show();
1689 }
1690 else // not created yet, do it now
1691 {
1692 // we have no way to return the error from here anyhow :-(
a290fa5a 1693 (void)Create(frame, wxID_ANY);
1e6feb95
VZ
1694
1695 SetCursor(wxCURSOR_ARROW);
1696
a756f210 1697 SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT));
02cbc27a
VZ
1698
1699 // calculate and set our height (it won't be changed any more)
a290fa5a 1700 SetSize(wxDefaultCoord, GetBestSize().y);
1e6feb95
VZ
1701 }
1702
1703 // remember the last frame which had us to avoid unnecessarily reparenting
1704 // above
1705 m_frameLast = frame;
1706}
1707
1708void wxMenuBar::Detach()
1709{
1710 // don't delete the window because we may be reattached later, just hide it
1711 if ( m_frameLast )
1712 {
1713 Hide();
1714 }
1715
1716 wxMenuBarBase::Detach();
1717}
1718
1719wxMenuBar::~wxMenuBar()
1720{
1721}
1722
1723// ----------------------------------------------------------------------------
1724// wxMenuBar adding/removing items
1725// ----------------------------------------------------------------------------
1726
1727bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
1728{
1729 return Insert(GetCount(), menu, title);
1730}
1731
1732bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
1733{
1734 if ( !wxMenuBarBase::Insert(pos, menu, title) )
a290fa5a 1735 return false;
1e6feb95
VZ
1736
1737 wxMenuInfo *info = new wxMenuInfo(title);
1738 m_menuInfos.Insert(info, pos);
1739
1740 RefreshAllItemsAfter(pos);
1741
a290fa5a 1742 return true;
1e6feb95
VZ
1743}
1744
1745wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
1746{
1747 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
1748
1749 if ( menuOld )
1750 {
1751 wxMenuInfo& info = m_menuInfos[pos];
1752
1753 info.SetLabel(title);
1754
1755 // even if the old menu was disabled, the new one is not any more
1756 info.SetEnabled();
1757
1758 // even if we change only this one, the new label has different width,
1759 // so we need to refresh everything beyond this item as well
1760 RefreshAllItemsAfter(pos);
1761 }
1762
1763 return menuOld;
1764}
1765
1766wxMenu *wxMenuBar::Remove(size_t pos)
1767{
1768 wxMenu *menuOld = wxMenuBarBase::Remove(pos);
1769
1770 if ( menuOld )
1771 {
1772 m_menuInfos.RemoveAt(pos);
1773
1774 // this doesn't happen too often, so don't try to be too smart - just
1775 // refresh everything
1776 Refresh();
1777 }
1778
1779 return menuOld;
1780}
1781
1782// ----------------------------------------------------------------------------
1783// wxMenuBar top level menus access
1784// ----------------------------------------------------------------------------
1785
1786wxCoord wxMenuBar::GetItemWidth(size_t pos) const
1787{
1788 return m_menuInfos[pos].GetWidth(wxConstCast(this, wxMenuBar));
1789}
1790
1791void wxMenuBar::EnableTop(size_t pos, bool enable)
1792{
1793 wxCHECK_RET( pos < GetCount(), _T("invalid index in EnableTop") );
1794
1795 if ( enable != m_menuInfos[pos].IsEnabled() )
1796 {
1797 m_menuInfos[pos].SetEnabled(enable);
1798
1799 RefreshItem(pos);
1800 }
1801 //else: nothing to do
1802}
1803
1804bool wxMenuBar::IsEnabledTop(size_t pos) const
1805{
a290fa5a 1806 wxCHECK_MSG( pos < GetCount(), false, _T("invalid index in IsEnabledTop") );
1e6feb95
VZ
1807
1808 return m_menuInfos[pos].IsEnabled();
1809}
1810
1811void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
1812{
1813 wxCHECK_RET( pos < GetCount(), _T("invalid index in EnableTop") );
1814
1815 if ( label != m_menuInfos[pos].GetLabel() )
1816 {
1817 m_menuInfos[pos].SetLabel(label);
1818
1819 RefreshItem(pos);
1820 }
1821 //else: nothing to do
1822}
1823
1824wxString wxMenuBar::GetLabelTop(size_t pos) const
1825{
1826 wxCHECK_MSG( pos < GetCount(), _T(""), _T("invalid index in GetLabelTop") );
1827
1828 return m_menuInfos[pos].GetLabel();
1829}
1830
1831// ----------------------------------------------------------------------------
1832// wxMenuBar drawing
1833// ----------------------------------------------------------------------------
1834
1835void wxMenuBar::RefreshAllItemsAfter(size_t pos)
1836{
4087064a
VZ
1837 if ( !IsCreated() )
1838 {
1839 // no need to refresh if nothing is shown yet
1840 return;
1841 }
1842
1e6feb95
VZ
1843 wxRect rect = GetItemRect(pos);
1844 rect.width = GetClientSize().x - rect.x;
1845 RefreshRect(rect);
1846}
1847
1848void wxMenuBar::RefreshItem(size_t pos)
1849{
1850 wxCHECK_RET( pos != (size_t)-1,
1851 _T("invalid item in wxMenuBar::RefreshItem") );
1852
4087064a
VZ
1853 if ( !IsCreated() )
1854 {
1855 // no need to refresh if nothing is shown yet
1856 return;
1857 }
1858
1e6feb95
VZ
1859 RefreshRect(GetItemRect(pos));
1860}
1861
1862void wxMenuBar::DoDraw(wxControlRenderer *renderer)
1863{
1864 wxDC& dc = renderer->GetDC();
a756f210 1865 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
1e6feb95
VZ
1866
1867 // redraw only the items which must be redrawn
1868
1869 // we don't have to use GetUpdateClientRect() here because our client rect
1870 // is the same as total one
1871 wxRect rectUpdate = GetUpdateRegion().GetBox();
1872
1873 int flagsMenubar = GetStateFlags();
1874
1875 wxRect rect;
1876 rect.y = 0;
1877 rect.height = GetClientSize().y;
1878
1879 wxCoord x = 0;
1880 size_t count = GetCount();
1881 for ( size_t n = 0; n < count; n++ )
1882 {
1883 if ( x > rectUpdate.GetRight() )
1884 {
1885 // all remaining items are to the right of rectUpdate
1886 break;
1887 }
1888
1889 rect.x = x;
1890 rect.width = GetItemWidth(n);
1891 x += rect.width;
1892 if ( x < rectUpdate.x )
1893 {
1894 // this item is still to the left of rectUpdate
1895 continue;
1896 }
1897
1898 int flags = flagsMenubar;
1899 if ( m_current != -1 && n == (size_t)m_current )
1900 {
1901 flags |= wxCONTROL_SELECTED;
1902 }
1903
1904 if ( !IsEnabledTop(n) )
1905 {
1906 flags |= wxCONTROL_DISABLED;
1907 }
1908
1909 GetRenderer()->DrawMenuBarItem
1910 (
1911 dc,
1912 rect,
1913 m_menuInfos[n].GetLabel(),
1914 flags,
1915 m_menuInfos[n].GetAccelIndex()
1916 );
1917 }
1918}
1919
1920// ----------------------------------------------------------------------------
1921// wxMenuBar geometry
1922// ----------------------------------------------------------------------------
1923
1924wxRect wxMenuBar::GetItemRect(size_t pos) const
1925{
1926 wxASSERT_MSG( pos < GetCount(), _T("invalid menu bar item index") );
4087064a 1927 wxASSERT_MSG( IsCreated(), _T("can't call this method yet") );
1e6feb95
VZ
1928
1929 wxRect rect;
1930 rect.x =
1931 rect.y = 0;
1932 rect.height = GetClientSize().y;
1933
1934 for ( size_t n = 0; n < pos; n++ )
1935 {
1936 rect.x += GetItemWidth(n);
1937 }
1938
1939 rect.width = GetItemWidth(pos);
1940
1941 return rect;
1942}
1943
1944wxSize wxMenuBar::DoGetBestClientSize() const
1945{
1946 wxSize size;
1947 if ( GetMenuCount() > 0 )
1948 {
1949 wxClientDC dc(wxConstCast(this, wxMenuBar));
a756f210 1950 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
1e6feb95
VZ
1951 dc.GetTextExtent(GetLabelTop(0), &size.x, &size.y);
1952
1953 // adjust for the renderer we use
1954 size = GetRenderer()->GetMenuBarItemSize(size);
1955 }
1956 else // empty menubar
1957 {
1958 size.x =
1959 size.y = 0;
1960 }
1961
1962 // the width is arbitrary, of course, for horizontal menubar
1963 size.x = 100;
1964
1965 return size;
1966}
1967
1968int wxMenuBar::GetMenuFromPoint(const wxPoint& pos) const
1969{
1970 if ( pos.x < 0 || pos.y < 0 || pos.y > GetClientSize().y )
1971 return -1;
1972
1973 // do find it
1974 wxCoord x = 0;
1975 size_t count = GetCount();
1976 for ( size_t item = 0; item < count; item++ )
1977 {
1978 x += GetItemWidth(item);
1979
1980 if ( x > pos.x )
1981 {
1982 return item;
1983 }
1984 }
1985
1986 // to the right of the last menu item
1987 return -1;
1988}
1989
1990// ----------------------------------------------------------------------------
1991// wxMenuBar menu operations
1992// ----------------------------------------------------------------------------
1993
1994void wxMenuBar::SelectMenu(size_t pos)
1995{
1996 SetFocus();
5a750e5a 1997 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::SelectMenu"));
1e6feb95
VZ
1998 CaptureMouse();
1999
2000 DoSelectMenu(pos);
2001}
2002
2003void wxMenuBar::DoSelectMenu(size_t pos)
2004{
2005 wxCHECK_RET( pos < GetCount(), _T("invalid menu index in DoSelectMenu") );
2006
23d8bb2c
VZ
2007 int posOld = m_current;
2008
2009 m_current = pos;
2010
2011 if ( posOld != -1 )
1e6feb95
VZ
2012 {
2013 // close the previous menu
2014 if ( IsShowingMenu() )
2015 {
2016 // restore m_shouldShowMenu flag after DismissMenu() which resets
a290fa5a 2017 // it to false
1e6feb95
VZ
2018 bool old = m_shouldShowMenu;
2019
2020 DismissMenu();
2021
2022 m_shouldShowMenu = old;
2023 }
2024
23d8bb2c 2025 RefreshItem((size_t)posOld);
1e6feb95
VZ
2026 }
2027
1e6feb95
VZ
2028 RefreshItem(pos);
2029}
2030
2031void wxMenuBar::PopupMenu(size_t pos)
2032{
2033 wxCHECK_RET( pos < GetCount(), _T("invalid menu index in PopupCurrentMenu") );
2034
2035 SetFocus();
2036 DoSelectMenu(pos);
2037 PopupCurrentMenu();
2038}
2039
2040// ----------------------------------------------------------------------------
2041// wxMenuBar input handing
2042// ----------------------------------------------------------------------------
2043
2044/*
2045 Note that wxMenuBar doesn't use wxInputHandler but handles keyboard and
2046 mouse in the same way under all platforms. This is because it doesn't derive
2047 from wxControl (which works with input handlers) but directly from wxWindow.
2048
2049 Also, menu bar input handling is rather simple, so maybe it's not really
2050 worth making it themeable - at least I've decided against doing it now as it
2051 would merging the changes back into trunk more difficult. But it still could
2052 be done later if really needed.
2053 */
2054
2055void wxMenuBar::OnKillFocus(wxFocusEvent& event)
2056{
2057 if ( m_current != -1 )
2058 {
2059 RefreshItem((size_t)m_current);
2060
2061 m_current = -1;
2062 }
2063
2064 event.Skip();
2065}
2066
2067void wxMenuBar::OnLeftDown(wxMouseEvent& event)
2068{
2069 if ( HasCapture() )
2070 {
2071 OnDismiss();
2072
2073 event.Skip();
2074 }
2075 else // we didn't have mouse capture, capture it now
2076 {
2077 m_current = GetMenuFromPoint(event.GetPosition());
2078 if ( m_current == -1 )
2079 {
2080 // unfortunately, we can't prevent wxMSW from giving us the focus,
2081 // so we can only give it back
2082 GiveAwayFocus();
2083 }
2084 else // on item
2085 {
a290fa5a 2086 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::OnLeftDown"));
1e6feb95
VZ
2087 CaptureMouse();
2088
2089 // show it as selected
2090 RefreshItem((size_t)m_current);
2091
2092 // show the menu
a290fa5a 2093 PopupCurrentMenu(false /* don't select first item - as Windows does */);
1e6feb95
VZ
2094 }
2095 }
2096}
2097
2098void wxMenuBar::OnMouseMove(wxMouseEvent& event)
2099{
2100 if ( HasCapture() )
2101 {
2102 (void)ProcessMouseEvent(event.GetPosition());
2103 }
2104 else
2105 {
2106 event.Skip();
2107 }
2108}
2109
2110bool wxMenuBar::ProcessMouseEvent(const wxPoint& pt)
2111{
2112 // a hack to ignore the extra mouse events MSW sends us: this is similar to
2113 // wxUSE_MOUSEEVENT_HACK in wxWin itself but it isn't enough for us here as
2114 // we get the messages from different windows (old and new popup menus for
2115 // example)
2116#ifdef __WXMSW__
2117 static wxPoint s_ptLast;
2118 if ( pt == s_ptLast )
2119 {
a290fa5a 2120 return false;
1e6feb95
VZ
2121 }
2122
2123 s_ptLast = pt;
2124#endif // __WXMSW__
2125
2126 int currentNew = GetMenuFromPoint(pt);
2127 if ( (currentNew == -1) || (currentNew == m_current) )
2128 {
a290fa5a 2129 return false;
1e6feb95
VZ
2130 }
2131
2132 // select the new active item
2133 DoSelectMenu(currentNew);
2134
2135 // show the menu if we know that we should, even if we hadn't been showing
2136 // it before (this may happen if the previous menu was disabled)
54800df8 2137 if ( m_shouldShowMenu && !m_menuShown)
1e6feb95
VZ
2138 {
2139 // open the new menu if the old one we closed had been opened
a290fa5a 2140 PopupCurrentMenu(false /* don't select first item - as Windows does */);
1e6feb95
VZ
2141 }
2142
a290fa5a 2143 return true;
1e6feb95
VZ
2144}
2145
2146void wxMenuBar::OnKeyDown(wxKeyEvent& event)
2147{
23d8bb2c
VZ
2148 // ensure that we have a current item - we might not have it if we're
2149 // given the focus with Alt or F10 press (and under GTK+ the menubar
2150 // somehow gets the keyboard events even when it doesn't have focus...)
2151 if ( m_current == -1 )
2152 {
2153 if ( !HasCapture() )
2154 {
2155 SelectMenu(0);
2156 }
2157 else // we do have capture
2158 {
2159 // we always maintain a valid current item while we're in modal
2160 // state (i.e. have the capture)
2161 wxFAIL_MSG( _T("how did we manage to lose current item?") );
2162
2163 return;
2164 }
2165 }
1e6feb95
VZ
2166
2167 int key = event.GetKeyCode();
2168
2169 // first let the menu have it
2170 if ( IsShowingMenu() && m_menuShown->ProcessKeyDown(key) )
2171 {
2172 return;
2173 }
2174
2175 // cycle through the menu items when left/right arrows are pressed and open
2176 // the menu when up/down one is
2177 switch ( key )
2178 {
2179 case WXK_MENU:
2180 // Alt must be processed at wxWindow level too
2181 event.Skip();
2182 // fall through
2183
2184 case WXK_ESCAPE:
2185 // remove the selection and give the focus away
2186 if ( m_current != -1 )
2187 {
2188 if ( IsShowingMenu() )
2189 {
2190 DismissMenu();
2191 }
2192
2193 OnDismiss();
2194 }
2195 break;
2196
2197 case WXK_LEFT:
2198 case WXK_RIGHT:
2199 {
2200 size_t count = GetCount();
2201 if ( count == 1 )
2202 {
2203 // the item won't change anyhow
2204 break;
2205 }
2206 //else: otherwise, it will
2207
2208 // remember if we were showing a menu - if we did, we should
2209 // show the new menu after changing the item
2210 bool wasMenuOpened = IsShowingMenu();
2211 if ( wasMenuOpened )
2212 {
2213 DismissMenu();
2214 }
2215
2216 // cast is safe as we tested for -1 above
2217 size_t currentNew = (size_t)m_current;
2218
2219 if ( key == WXK_LEFT )
2220 {
2221 if ( currentNew-- == 0 )
2222 currentNew = count - 1;
2223 }
2224 else // right
2225 {
6522713c 2226 if ( ++currentNew == count )
1e6feb95
VZ
2227 currentNew = 0;
2228 }
2229
2230 DoSelectMenu(currentNew);
2231
2232 if ( wasMenuOpened )
2233 {
2234 PopupCurrentMenu();
2235 }
2236 }
2237 break;
2238
2239 case WXK_DOWN:
2240 case WXK_UP:
2241 case WXK_RETURN:
2242 // open the menu
2243 PopupCurrentMenu();
2244 break;
2245
2246 default:
2247 // letters open the corresponding menu
2248 {
2249 bool unique;
2250 int idxFound = FindNextItemForAccel(m_current, key, &unique);
2251
2252 if ( idxFound != -1 )
2253 {
2254 if ( IsShowingMenu() )
2255 {
2256 DismissMenu();
2257 }
2258
2259 DoSelectMenu((size_t)idxFound);
2260
2261 // if the item is not unique, just select it but don't
2262 // activate as the user might have wanted to activate
2263 // another item
2264 //
2265 // also, don't try to open a disabled menu
2266 if ( unique && IsEnabledTop((size_t)idxFound) )
2267 {
2268 // open the menu
2269 PopupCurrentMenu();
2270 }
2271
2272 // skip the "event.Skip()" below
2273 break;
2274 }
2275 }
2276
2277 event.Skip();
2278 }
2279}
2280
2281// ----------------------------------------------------------------------------
2282// wxMenuBar accel handling
2283// ----------------------------------------------------------------------------
2284
2285int wxMenuBar::FindNextItemForAccel(int idxStart, int key, bool *unique) const
2286{
2287 if ( !wxIsalnum(key) )
2288 {
2289 // we only support letters/digits as accels
2290 return -1;
2291 }
2292
2293 // do we have more than one item with this accel?
2294 if ( unique )
a290fa5a 2295 *unique = true;
1e6feb95
VZ
2296
2297 // translate everything to lower case before comparing
2298 wxChar chAccel = wxTolower(key);
2299
2300 // the index of the item with this accel
2301 int idxFound = -1;
2302
2303 // loop through all items searching for the item with this
2304 // accel starting at the item after the current one
2305 int count = GetCount();
2306 int n = idxStart == -1 ? 0 : idxStart + 1;
2307
2308 if ( n == count )
2309 {
2310 // wrap
2311 n = 0;
2312 }
2313
2314 idxStart = n;
2315 for ( ;; )
2316 {
2317 const wxMenuInfo& info = m_menuInfos[n];
2318
2319 int idxAccel = info.GetAccelIndex();
2320 if ( idxAccel != -1 &&
2321 wxTolower(info.GetLabel()[(size_t)idxAccel])
2322 == chAccel )
2323 {
2324 // ok, found an item with this accel
2325 if ( idxFound == -1 )
2326 {
2327 // store it but continue searching as we need to
2328 // know if it's the only item with this accel or if
2329 // there are more
2330 idxFound = n;
2331 }
2332 else // we already had found such item
2333 {
2334 if ( unique )
a290fa5a 2335 *unique = false;
1e6feb95
VZ
2336
2337 // no need to continue further, we won't find
2338 // anything we don't already know
2339 break;
2340 }
2341 }
2342
2343 // we want to iterate over all items wrapping around if
2344 // necessary
2345 if ( ++n == count )
2346 {
2347 // wrap
2348 n = 0;
2349 }
2350
2351 if ( n == idxStart )
2352 {
2353 // we've seen all items
2354 break;
2355 }
2356 }
2357
2358 return idxFound;
2359}
2360
2361#if wxUSE_ACCEL
2362
2363bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent& event)
2364{
2365 size_t n = 0;
ac32ba44 2366 for ( wxMenuList::compatibility_iterator node = m_menus.GetFirst();
1e6feb95
VZ
2367 node;
2368 node = node->GetNext(), n++ )
2369 {
2370 // accels of the items in the disabled menus shouldn't work
2371 if ( m_menuInfos[n].IsEnabled() )
2372 {
2373 if ( node->GetData()->ProcessAccelEvent(event) )
2374 {
2375 // menu processed it
a290fa5a 2376 return true;
1e6feb95
VZ
2377 }
2378 }
2379 }
2380
2381 // not found
a290fa5a 2382 return false;
1e6feb95
VZ
2383}
2384
2385#endif // wxUSE_ACCEL
2386
2387// ----------------------------------------------------------------------------
2388// wxMenuBar menus showing
2389// ----------------------------------------------------------------------------
2390
2391void wxMenuBar::PopupCurrentMenu(bool selectFirst)
2392{
2393 wxCHECK_RET( m_current != -1, _T("no menu to popup") );
2394
2395 // forgot to call DismissMenu()?
8601b2e1 2396 wxASSERT_MSG( !m_menuShown, _T("shouldn't show two menus at once!") );
1e6feb95
VZ
2397
2398 // in any case, we should show it - even if we won't
a290fa5a 2399 m_shouldShowMenu = true;
1e6feb95
VZ
2400
2401 if ( IsEnabledTop(m_current) )
2402 {
2403 // remember the menu we show
2404 m_menuShown = GetMenu(m_current);
2405
2406 // we don't show the menu at all if it has no items
2407 if ( !m_menuShown->IsEmpty() )
2408 {
2409 // position it correctly: note that we must use screen coords and
2410 // that we pass 0 as width to position the menu exactly below the
2411 // item, not to the right of it
2412 wxRect rectItem = GetItemRect(m_current);
1f752a22 2413
c5536589
WS
2414 m_menuShown->SetInvokingWindow(m_frameLast);
2415
1e6feb95
VZ
2416 m_menuShown->Popup(ClientToScreen(rectItem.GetPosition()),
2417 wxSize(0, rectItem.GetHeight()),
2418 selectFirst);
2419 }
2420 else
2421 {
2422 // reset it back as no menu is shown
2423 m_menuShown = NULL;
2424 }
2425 }
2426 //else: don't show disabled menu
2427}
2428
2429void wxMenuBar::DismissMenu()
2430{
2431 wxCHECK_RET( m_menuShown, _T("can't dismiss menu if none is shown") );
2432
2433 m_menuShown->Dismiss();
2434 OnDismissMenu();
2435}
2436
2437void wxMenuBar::OnDismissMenu(bool dismissMenuBar)
2438{
a290fa5a 2439 m_shouldShowMenu = false;
1e6feb95
VZ
2440 m_menuShown = NULL;
2441 if ( dismissMenuBar )
2442 {
2443 OnDismiss();
2444 }
2445}
2446
2447void wxMenuBar::OnDismiss()
2448{
a9a05059 2449 if ( GetCapture() )
7edcafa4 2450 {
5a750e5a 2451 wxLogTrace(_T("mousecapture"), _T("Releasing mouse from wxMenuBar::OnDismiss"));
a9a05059 2452 GetCapture()->ReleaseMouse();
7edcafa4 2453 }
1e6feb95
VZ
2454
2455 if ( m_current != -1 )
2456 {
23d8bb2c 2457 size_t current = m_current;
1e6feb95 2458 m_current = -1;
23d8bb2c
VZ
2459
2460 RefreshItem(current);
1e6feb95
VZ
2461 }
2462
2463 GiveAwayFocus();
2464}
2465
2466void wxMenuBar::GiveAwayFocus()
2467{
2468 GetFrame()->SetFocus();
2469}
2470
2471// ----------------------------------------------------------------------------
2472// popup menu support
2473// ----------------------------------------------------------------------------
2474
2475wxEventLoop *wxWindow::ms_evtLoopPopup = NULL;
2476
2477bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
2478{
a290fa5a 2479 wxCHECK_MSG( !ms_evtLoopPopup, false,
1e6feb95
VZ
2480 _T("can't show more than one popup menu at a time") );
2481
2482#ifdef __WXMSW__
2483 // we need to change the cursor before showing the menu as, apparently, no
2484 // cursor changes took place while the mouse is captured
2485 wxCursor cursorOld = GetCursor();
2486 SetCursor(wxCURSOR_ARROW);
2487#endif // __WXMSW__
2488
2489#if 0
2490 // flash any delayed log messages before showing the menu, otherwise it
2491 // could be dismissed (because it would lose focus) immediately after being
2492 // shown
2493 wxLog::FlushActive();
2494
2495 // some controls update themselves from OnIdle() call - let them do it
e39af974 2496 wxTheApp->ProcessIdle();
1e6feb95
VZ
2497
2498 // if the window hadn't been refreshed yet, the menu can adversely affect
2499 // its next OnPaint() handler execution - i.e. scrolled window refresh
2500 // logic breaks then as it scrolls part of the menu which hadn't been there
2501 // when the update event was generated into view
2502 Update();
2503#endif // 0
2504
2505 menu->SetInvokingWindow(this);
e441e1f4 2506
ee351013 2507 // wxLogDebug( "Name of invoking window %s", menu->GetInvokingWindow()->GetName().c_str() );
e441e1f4 2508
1e6feb95
VZ
2509 menu->Popup(ClientToScreen(wxPoint(x, y)), wxSize(0, 0));
2510
2511 // this is not very useful if the menu was popped up because of the mouse
2512 // click but I think it is nice to do when it appears because of a key
2513 // press (i.e. Windows menu key)
2514 //
2515 // Windows itself doesn't do it, but IMHO this is nice
2516 WarpPointer(x, y);
2517
2518 // we have to redirect all keyboard input to the menu temporarily
2519 PushEventHandler(new wxMenuKbdRedirector(menu));
2520
2521 // enter the local modal loop
2522 ms_evtLoopPopup = new wxEventLoop;
2523 ms_evtLoopPopup->Run();
2524
2525 delete ms_evtLoopPopup;
2526 ms_evtLoopPopup = NULL;
2527
2528 // remove the handler
a290fa5a 2529 PopEventHandler(true /* delete it */);
1e6feb95
VZ
2530
2531 menu->SetInvokingWindow(NULL);
2532
2533#ifdef __WXMSW__
2534 SetCursor(cursorOld);
2535#endif // __WXMSW__
2536
a290fa5a 2537 return true;
1e6feb95
VZ
2538}
2539
2540void wxWindow::DismissPopupMenu()
2541{
2542 wxCHECK_RET( ms_evtLoopPopup, _T("no popup menu shown") );
e441e1f4 2543
1e6feb95
VZ
2544 ms_evtLoopPopup->Exit();
2545}
2546
2547#endif // wxUSE_MENUS
2548