]> git.saurik.com Git - wxWidgets.git/blame - src/univ/menu.cpp
safe destruction, even if redraws might be triggered
[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)
6aa89a22 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
78 void SetEnabled(bool enabled = TRUE) { m_isEnabled = enabled; }
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
139 void OnSubmenuDismiss() { m_hasOpenSubMenu = FALSE; }
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
156 // process the key event, return TRUE if done
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
193 // on what it is, return TRUE if something was done (i.e. it's not a
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 {
ee351013 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;
307 m_hasOpenSubMenu = FALSE;
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
476 HandleDismiss(TRUE);
477}
478
479void wxPopupMenuWindow::HandleDismiss(bool dismissParent)
480{
481 ResetCurrent();
482
483 m_menu->OnDismiss(dismissParent);
484}
485
486void wxPopupMenuWindow::DismissAndNotify()
487{
488 Dismiss();
489 HandleDismiss(TRUE);
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
651 m_hasOpenSubMenu = TRUE;
652}
653
654bool wxPopupMenuWindow::ActivateItem(wxMenuItem *item, InputMethod how)
655{
656 // don't activate disabled items
657 if ( !item || !item->IsEnabled() )
658 {
659 return FALSE;
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 {
674 return FALSE;
675 }
676
677 return TRUE;
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
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
705 return TRUE;
706 }
707 //else: it is outside the parent menu as well, do dismiss this one
708 }
709 }
710
711 return FALSE;
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
836 resetCurrent = TRUE;
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 {
869 wxCHECK_MSG( CanOpen(item), FALSE,
870 _T("has open submenu but another item selected?") );
871
872 if ( item->GetSubMenu()->ProcessKeyDown(key) )
873 return TRUE;
874 }
875
876 bool processed = TRUE;
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 {
888 processed = FALSE;
889 break;
890 }
891
892 // fall through
893
894 case WXK_ESCAPE:
895 // close just this menu
896 Dismiss();
897 HandleDismiss(FALSE);
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 {
942 processed = FALSE;
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 {
955 processed = FALSE;
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?
969 bool notUnique = FALSE;
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 {
1001 notUnique = TRUE;
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
1034 // skip "processed = FALSE" below
1035 break;
1036 }
1037 }
1038
1039 processed = FALSE;
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{
8a39593e
JS
1127 #if 0
1128 // not used at all
6f7c5199 1129 bool check = FALSE;
8a39593e 1130 #endif
6f7c5199
JS
1131
1132 if ( item->GetKind() == wxITEM_RADIO )
1133 {
1134 int count = GetMenuItemCount();
1135
1136 if ( m_startRadioGroup == -1 )
1137 {
1138 // start a new radio group
1139 m_startRadioGroup = count;
1140
1141 // for now it has just one element
1142 item->SetAsRadioGroupStart();
1143 item->SetRadioGroupEnd(m_startRadioGroup);
1144
1145 // ensure that we have a checked item in the radio group
8a39593e
JS
1146 #if 0
1147 // not used at all
6f7c5199 1148 check = TRUE;
8a39593e 1149 #endif
6f7c5199
JS
1150 }
1151 else // extend the current radio group
1152 {
1153 // we need to update its end item
1154 item->SetRadioGroupStart(m_startRadioGroup);
ac32ba44 1155 wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup);
6f7c5199
JS
1156
1157 if ( node )
1158 {
1159 node->GetData()->SetRadioGroupEnd(count);
1160 }
1161 else
1162 {
1163 wxFAIL_MSG( _T("where is the radio group start item?") );
1164 }
1165 }
1166 }
1167 else // not a radio item
1168 {
1169 EndRadioGroup();
1170 }
1171
1e6feb95 1172 if ( !wxMenuBase::DoAppend(item) )
9add9367 1173 return NULL;
1e6feb95
VZ
1174
1175 OnItemAdded(item);
1176
9add9367 1177 return item;
1e6feb95
VZ
1178}
1179
9add9367 1180wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
1e6feb95
VZ
1181{
1182 if ( !wxMenuBase::DoInsert(pos, item) )
9add9367 1183 return NULL;
1e6feb95
VZ
1184
1185 OnItemAdded(item);
1186
9add9367 1187 return item;
1e6feb95
VZ
1188}
1189
1190wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
1191{
1192 wxMenuItem *itemOld = wxMenuBase::DoRemove(item);
1193
1194 if ( itemOld )
1195 {
1196 InvalidateGeometryInfo();
1197
1198#if wxUSE_ACCEL
1199 RemoveAccelFor(item);
1200#endif // wxUSE_ACCEL
1201 }
1202
1203 return itemOld;
1204}
1205
1206// ----------------------------------------------------------------------------
1207// wxMenu attaching/detaching
1208// ----------------------------------------------------------------------------
1209
1210void wxMenu::Attach(wxMenuBarBase *menubar)
1211{
1212 wxMenuBase::Attach(menubar);
1213
1214 wxCHECK_RET( m_menuBar, _T("menubar can't be NULL after attaching") );
1215
1216 // unfortunately, we can't use m_menuBar->GetEventHandler() here because,
1217 // if the menubar is currently showing a menu, its event handler is a
1218 // temporary one installed by wxPopupWindow and so will disappear soon any
1219 // any attempts to use it from the newly attached menu would result in a
1220 // crash
1221 //
1222 // so we use the menubar itself, even if it's a pity as it means we can't
1223 // redirect all menu events by changing the menubar handler (FIXME)
1224 SetNextHandler(m_menuBar);
1225}
1226
1227void wxMenu::Detach()
1228{
1229 wxMenuBase::Detach();
1230}
1231
1232// ----------------------------------------------------------------------------
1233// wxMenu misc functions
1234// ----------------------------------------------------------------------------
1235
1236wxWindow *wxMenu::GetRootWindow() const
1237{
1238 if ( m_menuBar )
1239 {
1240 // simple case - a normal menu attached to the menubar
1241 return m_menuBar;
1242 }
1243
1244 // we're a popup menu but the trouble is that only the top level popup menu
1245 // has a pointer to the invoking window, so we must walk up the menu chain
1246 // if needed
1247 wxWindow *win = GetInvokingWindow();
1248 if ( win )
1249 {
1250 // we already have it
1251 return win;
1252 }
1253
1254 wxMenu *menu = GetParent();
1255 while ( menu )
1256 {
ea1ad04b
RR
1257 // We are a submenu of a menu of a menubar
1258 if (menu->GetMenuBar())
1259 return menu->GetMenuBar();
e441e1f4 1260
1e6feb95
VZ
1261 win = menu->GetInvokingWindow();
1262 if ( win )
1263 break;
1264
1265 menu = menu->GetParent();
1266 }
1267
1268 // we're probably going to crash in the caller anyhow, but try to detect
1269 // this error as soon as possible
1270 wxASSERT_MSG( win, _T("menu without any associated window?") );
e441e1f4 1271
1e6feb95
VZ
1272 // also remember it in this menu so that we don't have to search for it the
1273 // next time
1274 wxConstCast(this, wxMenu)->m_invokingWindow = win;
1275
1276 return win;
1277}
1278
1279wxRenderer *wxMenu::GetRenderer() const
1280{
1281 // we're going to crash without renderer!
1282 wxCHECK_MSG( m_popupMenu, NULL, _T("neither popup nor menubar menu?") );
1283
1284 return m_popupMenu->GetRenderer();
1285}
1286
1287void wxMenu::RefreshItem(wxMenuItem *item)
1288{
1289 // the item geometry changed, so our might have changed as well
1290 InvalidateGeometryInfo();
1291
1292 if ( IsShown() )
1293 {
1294 // this would be a bug in IsShown()
1295 wxCHECK_RET( m_popupMenu, _T("must have popup window if shown!") );
1296
1297 // recalc geometry to update the item height and such
1298 (void)GetGeometryInfo();
1299
1300 m_popupMenu->RefreshItem(item);
1301 }
1302}
1303
1304// ----------------------------------------------------------------------------
1305// wxMenu showing and hiding
1306// ----------------------------------------------------------------------------
1307
1308bool wxMenu::IsShown() const
1309{
1310 return m_popupMenu && m_popupMenu->IsShown();
1311}
1312
1313void wxMenu::OnDismiss(bool dismissParent)
1314{
1315 if ( m_menuParent )
1316 {
1317 // always notify the parent about submenu disappearance
1318 wxPopupMenuWindow *win = m_menuParent->m_popupMenu;
1319 if ( win )
1320 {
1321 win->OnSubmenuDismiss();
1322 }
1323 else
1324 {
1325 wxFAIL_MSG( _T("parent menu not shown?") );
1326 }
1327
1328 // and if we dismiss everything, propagate to parent
1329 if ( dismissParent )
1330 {
1331 // dismissParent is recursive
1332 m_menuParent->Dismiss();
1333 m_menuParent->OnDismiss(TRUE);
1334 }
1335 }
1336 else // no parent menu
1337 {
1338 // notify the menu bar if we're a top level menu
1339 if ( m_menuBar )
1340 {
1341 m_menuBar->OnDismissMenu(dismissParent);
1342 }
1343 else // popup menu
1344 {
1345 wxCHECK_RET( m_invokingWindow, _T("what kind of menu is this?") );
1346
1347 m_invokingWindow->DismissPopupMenu();
e441e1f4 1348
ee351013
RR
1349 // Why reset it here? We need it for sending the event to...
1350 // SetInvokingWindow(NULL);
1e6feb95
VZ
1351 }
1352 }
1353}
1354
1355void wxMenu::Popup(const wxPoint& pos, const wxSize& size, bool selectFirst)
1356{
1357 // create the popup window if not done yet
1358 if ( !m_popupMenu )
1359 {
1360 m_popupMenu = new wxPopupMenuWindow(GetRootWindow(), this);
1361 }
1362
1363 // select the first item unless disabled
1364 if ( selectFirst )
1365 {
1366 m_popupMenu->SelectFirst();
1367 }
e441e1f4 1368
1e6feb95
VZ
1369 // the geometry might have changed since the last time we were shown, so
1370 // always resize
1371 m_popupMenu->SetClientSize(GetGeometryInfo().GetSize());
1372
1373 // position it as specified
1374 m_popupMenu->Position(pos, size);
1375
1376 // the menu can't have the focus itself (it is a Windows limitation), so
1377 // always keep the focus at the originating window
1378 wxWindow *focus = GetRootWindow();
1379
1380 wxASSERT_MSG( focus, _T("no window to keep focus on?") );
1381
1382 // and show it
1383 m_popupMenu->Popup(focus);
1384}
1385
1386void wxMenu::Dismiss()
1387{
1388 wxCHECK_RET( IsShown(), _T("can't dismiss hidden menu") );
1389
1390 m_popupMenu->Dismiss();
1391}
1392
1393// ----------------------------------------------------------------------------
1394// wxMenu event processing
1395// ----------------------------------------------------------------------------
1396
1397bool wxMenu::ProcessKeyDown(int key)
1398{
1399 wxCHECK_MSG( m_popupMenu, FALSE,
1400 _T("can't process key events if not shown") );
1401
1402 return m_popupMenu->ProcessKeyDown(key);
1403}
1404
1405bool wxMenu::ClickItem(wxMenuItem *item)
1406{
1407 int isChecked;
1408 if ( item->IsCheckable() )
1409 {
1410 // update the item state
1411 isChecked = !item->IsChecked();
1412
1413 item->Check(isChecked != 0);
1414 }
1415 else
1416 {
1417 // not applicabled
1418 isChecked = -1;
1419 }
e441e1f4 1420
1e6feb95
VZ
1421 return SendEvent(item->GetId(), isChecked);
1422}
1423
1424// ----------------------------------------------------------------------------
1425// wxMenu accel support
1426// ----------------------------------------------------------------------------
1427
1428#if wxUSE_ACCEL
1429
1430bool wxMenu::ProcessAccelEvent(const wxKeyEvent& event)
1431{
1432 // do we have an item for this accel?
1433 wxMenuItem *item = m_accelTable.GetMenuItem(event);
1434 if ( item && item->IsEnabled() )
1435 {
1436 return ClickItem(item);
1437 }
1438
1439 // try our submenus
ac32ba44 1440 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
1e6feb95
VZ
1441 node;
1442 node = node->GetNext() )
1443 {
1444 const wxMenuItem *item = node->GetData();
1445 if ( item->IsSubMenu() && item->IsEnabled() )
1446 {
1447 // try its elements
1448 if ( item->GetSubMenu()->ProcessAccelEvent(event) )
1449 {
1450 return TRUE;
1451 }
1452 }
1453 }
1454
1455 return FALSE;
1456}
1457
1458void wxMenu::AddAccelFor(wxMenuItem *item)
1459{
1460 wxAcceleratorEntry *accel = item->GetAccel();
1461 if ( accel )
1462 {
1463 accel->SetMenuItem(item);
1464
1465 m_accelTable.Add(*accel);
1466
1467 delete accel;
1468 }
1469}
1470
1471void wxMenu::RemoveAccelFor(wxMenuItem *item)
1472{
1473 wxAcceleratorEntry *accel = item->GetAccel();
1474 if ( accel )
1475 {
1476 m_accelTable.Remove(*accel);
1477
1478 delete accel;
1479 }
1480}
1481
1482#endif // wxUSE_ACCEL
1483
1484// ----------------------------------------------------------------------------
1485// wxMenuItem construction
1486// ----------------------------------------------------------------------------
1487
1488wxMenuItem::wxMenuItem(wxMenu *parentMenu,
1489 int id,
1490 const wxString& text,
1491 const wxString& help,
d65c269b 1492 wxItemKind kind,
1e6feb95 1493 wxMenu *subMenu)
d65c269b 1494 : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu)
1e6feb95 1495{
1e6feb95
VZ
1496 m_posY =
1497 m_height = -1;
1498
6f7c5199
JS
1499 m_radioGroup.start = -1;
1500 m_isRadioGroupStart = FALSE;
1501
dba6b4f8
JS
1502 m_bmpDisabled = wxNullBitmap;
1503
1e6feb95
VZ
1504 UpdateAccelInfo();
1505}
1506
1507wxMenuItem::~wxMenuItem()
1508{
1509}
1510
1511// ----------------------------------------------------------------------------
1512// wxMenuItemBase methods implemented here
1513// ----------------------------------------------------------------------------
1514
1515/* static */
1516wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
1517 int id,
1518 const wxString& name,
1519 const wxString& help,
e4052bd3 1520 wxItemKind kind,
1e6feb95
VZ
1521 wxMenu *subMenu)
1522{
e4052bd3 1523 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
1e6feb95
VZ
1524}
1525
1526/* static */
1527wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
1528{
1529 return wxStripMenuCodes(text);
1530}
1531
1532// ----------------------------------------------------------------------------
1533// wxMenuItem operations
1534// ----------------------------------------------------------------------------
1535
1536void wxMenuItem::NotifyMenu()
1537{
1538 m_parentMenu->RefreshItem(this);
1539}
1540
1541void wxMenuItem::UpdateAccelInfo()
1542{
1543 m_indexAccel = wxControl::FindAccelIndex(m_text);
1544
1545 // will be empty if the text contains no TABs - ok
1546 m_strAccel = m_text.AfterFirst(_T('\t'));
1547}
1548
1549void wxMenuItem::SetText(const wxString& text)
1550{
1551 if ( text != m_text )
1552 {
1553 // first call the base class version to change m_text
1554 wxMenuItemBase::SetText(text);
1555
1556 UpdateAccelInfo();
1557
1558 NotifyMenu();
1559 }
1560}
1561
1562void wxMenuItem::SetCheckable(bool checkable)
1563{
d65c269b 1564 if ( checkable != IsCheckable() )
1e6feb95
VZ
1565 {
1566 wxMenuItemBase::SetCheckable(checkable);
1567
1568 NotifyMenu();
1569 }
1570}
1571
1572void wxMenuItem::SetBitmaps(const wxBitmap& bmpChecked,
1573 const wxBitmap& bmpUnchecked)
1574{
1575 m_bmpChecked = bmpChecked;
1576 m_bmpUnchecked = bmpUnchecked;
1577
1578 NotifyMenu();
1579}
1580
1581void wxMenuItem::Enable(bool enable)
1582{
1583 if ( enable != m_isEnabled )
1584 {
1585 wxMenuItemBase::Enable(enable);
1586
1587 NotifyMenu();
1588 }
1589}
1590
1591void wxMenuItem::Check(bool check)
1592{
6f7c5199
JS
1593 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
1594
1595 if ( m_isChecked == check )
1596 return;
1597
1598 if ( GetKind() == wxITEM_RADIO )
1e6feb95 1599 {
6f7c5199
JS
1600 // it doesn't make sense to uncheck a radio item - what would this do?
1601 if ( !check )
1602 return;
1e6feb95 1603
6f7c5199
JS
1604 // get the index of this item in the menu
1605 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
1606 int pos = items.IndexOf(this);
1607 wxCHECK_RET( pos != wxNOT_FOUND,
1608 _T("menuitem not found in the menu items list?") );
1609
1610 // get the radio group range
1611 int start,
1612 end;
1613
1614 if ( m_isRadioGroupStart )
1615 {
1616 // we already have all information we need
1617 start = pos;
1618 end = m_radioGroup.end;
1619 }
1620 else // next radio group item
1621 {
1622 // get the radio group end from the start item
1623 start = m_radioGroup.start;
1624 end = items.Item(start)->GetData()->m_radioGroup.end;
1625 }
1626
1627 // also uncheck all the other items in this radio group
ac32ba44 1628 wxMenuItemList::compatibility_iterator node = items.Item(start);
6f7c5199
JS
1629 for ( int n = start; n <= end && node; n++ )
1630 {
1631 if ( n != pos )
1632 {
1633 node->GetData()->m_isChecked = FALSE;
1634 }
1635 node = node->GetNext();
1636 }
1e6feb95 1637 }
6f7c5199
JS
1638
1639 wxMenuItemBase::Check(check);
1640
1641 NotifyMenu();
1642}
1643
1644// radio group stuff
1645// -----------------
1646
1647void wxMenuItem::SetAsRadioGroupStart()
1648{
1649 m_isRadioGroupStart = TRUE;
1650}
1651
1652void wxMenuItem::SetRadioGroupStart(int start)
1653{
1654 wxASSERT_MSG( !m_isRadioGroupStart,
1655 _T("should only be called for the next radio items") );
1656
1657 m_radioGroup.start = start;
1658}
1659
1660void wxMenuItem::SetRadioGroupEnd(int end)
1661{
1662 wxASSERT_MSG( m_isRadioGroupStart,
1663 _T("should only be called for the first radio item") );
1664
1665 m_radioGroup.end = end;
1e6feb95
VZ
1666}
1667
1668// ----------------------------------------------------------------------------
1669// wxMenuBar creation
1670// ----------------------------------------------------------------------------
1671
1672void wxMenuBar::Init()
1673{
1674 m_frameLast = NULL;
1675
1676 m_current = -1;
1677
1678 m_menuShown = NULL;
1679
1680 m_shouldShowMenu = FALSE;
1681}
1682
1683void wxMenuBar::Attach(wxFrame *frame)
1684{
1685 // maybe you really wanted to call Detach()?
1686 wxCHECK_RET( frame, _T("wxMenuBar::Attach(NULL) called") );
1687
1688 wxMenuBarBase::Attach(frame);
1689
1690 if ( IsCreated() )
1691 {
1692 // reparent if necessary
1693 if ( m_frameLast != frame )
1694 {
1695 Reparent(frame);
1696 }
1697
1698 // show it back - was hidden by Detach()
1699 Show();
1700 }
1701 else // not created yet, do it now
1702 {
1703 // we have no way to return the error from here anyhow :-(
1704 (void)Create(frame, -1);
1705
1706 SetCursor(wxCURSOR_ARROW);
1707
a756f210 1708 SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT));
02cbc27a
VZ
1709
1710 // calculate and set our height (it won't be changed any more)
1711 SetSize(-1, GetBestSize().y);
1e6feb95
VZ
1712 }
1713
1714 // remember the last frame which had us to avoid unnecessarily reparenting
1715 // above
1716 m_frameLast = frame;
1717}
1718
1719void wxMenuBar::Detach()
1720{
1721 // don't delete the window because we may be reattached later, just hide it
1722 if ( m_frameLast )
1723 {
1724 Hide();
1725 }
1726
1727 wxMenuBarBase::Detach();
1728}
1729
1730wxMenuBar::~wxMenuBar()
1731{
1732}
1733
1734// ----------------------------------------------------------------------------
1735// wxMenuBar adding/removing items
1736// ----------------------------------------------------------------------------
1737
1738bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
1739{
1740 return Insert(GetCount(), menu, title);
1741}
1742
1743bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
1744{
1745 if ( !wxMenuBarBase::Insert(pos, menu, title) )
1746 return FALSE;
1747
1748 wxMenuInfo *info = new wxMenuInfo(title);
1749 m_menuInfos.Insert(info, pos);
1750
1751 RefreshAllItemsAfter(pos);
1752
1753 return TRUE;
1754}
1755
1756wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
1757{
1758 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
1759
1760 if ( menuOld )
1761 {
1762 wxMenuInfo& info = m_menuInfos[pos];
1763
1764 info.SetLabel(title);
1765
1766 // even if the old menu was disabled, the new one is not any more
1767 info.SetEnabled();
1768
1769 // even if we change only this one, the new label has different width,
1770 // so we need to refresh everything beyond this item as well
1771 RefreshAllItemsAfter(pos);
1772 }
1773
1774 return menuOld;
1775}
1776
1777wxMenu *wxMenuBar::Remove(size_t pos)
1778{
1779 wxMenu *menuOld = wxMenuBarBase::Remove(pos);
1780
1781 if ( menuOld )
1782 {
1783 m_menuInfos.RemoveAt(pos);
1784
1785 // this doesn't happen too often, so don't try to be too smart - just
1786 // refresh everything
1787 Refresh();
1788 }
1789
1790 return menuOld;
1791}
1792
1793// ----------------------------------------------------------------------------
1794// wxMenuBar top level menus access
1795// ----------------------------------------------------------------------------
1796
1797wxCoord wxMenuBar::GetItemWidth(size_t pos) const
1798{
1799 return m_menuInfos[pos].GetWidth(wxConstCast(this, wxMenuBar));
1800}
1801
1802void wxMenuBar::EnableTop(size_t pos, bool enable)
1803{
1804 wxCHECK_RET( pos < GetCount(), _T("invalid index in EnableTop") );
1805
1806 if ( enable != m_menuInfos[pos].IsEnabled() )
1807 {
1808 m_menuInfos[pos].SetEnabled(enable);
1809
1810 RefreshItem(pos);
1811 }
1812 //else: nothing to do
1813}
1814
1815bool wxMenuBar::IsEnabledTop(size_t pos) const
1816{
1817 wxCHECK_MSG( pos < GetCount(), FALSE, _T("invalid index in IsEnabledTop") );
1818
1819 return m_menuInfos[pos].IsEnabled();
1820}
1821
1822void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
1823{
1824 wxCHECK_RET( pos < GetCount(), _T("invalid index in EnableTop") );
1825
1826 if ( label != m_menuInfos[pos].GetLabel() )
1827 {
1828 m_menuInfos[pos].SetLabel(label);
1829
1830 RefreshItem(pos);
1831 }
1832 //else: nothing to do
1833}
1834
1835wxString wxMenuBar::GetLabelTop(size_t pos) const
1836{
1837 wxCHECK_MSG( pos < GetCount(), _T(""), _T("invalid index in GetLabelTop") );
1838
1839 return m_menuInfos[pos].GetLabel();
1840}
1841
1842// ----------------------------------------------------------------------------
1843// wxMenuBar drawing
1844// ----------------------------------------------------------------------------
1845
1846void wxMenuBar::RefreshAllItemsAfter(size_t pos)
1847{
4087064a
VZ
1848 if ( !IsCreated() )
1849 {
1850 // no need to refresh if nothing is shown yet
1851 return;
1852 }
1853
1e6feb95
VZ
1854 wxRect rect = GetItemRect(pos);
1855 rect.width = GetClientSize().x - rect.x;
1856 RefreshRect(rect);
1857}
1858
1859void wxMenuBar::RefreshItem(size_t pos)
1860{
1861 wxCHECK_RET( pos != (size_t)-1,
1862 _T("invalid item in wxMenuBar::RefreshItem") );
1863
4087064a
VZ
1864 if ( !IsCreated() )
1865 {
1866 // no need to refresh if nothing is shown yet
1867 return;
1868 }
1869
1e6feb95
VZ
1870 RefreshRect(GetItemRect(pos));
1871}
1872
1873void wxMenuBar::DoDraw(wxControlRenderer *renderer)
1874{
1875 wxDC& dc = renderer->GetDC();
a756f210 1876 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
1e6feb95
VZ
1877
1878 // redraw only the items which must be redrawn
1879
1880 // we don't have to use GetUpdateClientRect() here because our client rect
1881 // is the same as total one
1882 wxRect rectUpdate = GetUpdateRegion().GetBox();
1883
1884 int flagsMenubar = GetStateFlags();
1885
1886 wxRect rect;
1887 rect.y = 0;
1888 rect.height = GetClientSize().y;
1889
1890 wxCoord x = 0;
1891 size_t count = GetCount();
1892 for ( size_t n = 0; n < count; n++ )
1893 {
1894 if ( x > rectUpdate.GetRight() )
1895 {
1896 // all remaining items are to the right of rectUpdate
1897 break;
1898 }
1899
1900 rect.x = x;
1901 rect.width = GetItemWidth(n);
1902 x += rect.width;
1903 if ( x < rectUpdate.x )
1904 {
1905 // this item is still to the left of rectUpdate
1906 continue;
1907 }
1908
1909 int flags = flagsMenubar;
1910 if ( m_current != -1 && n == (size_t)m_current )
1911 {
1912 flags |= wxCONTROL_SELECTED;
1913 }
1914
1915 if ( !IsEnabledTop(n) )
1916 {
1917 flags |= wxCONTROL_DISABLED;
1918 }
1919
1920 GetRenderer()->DrawMenuBarItem
1921 (
1922 dc,
1923 rect,
1924 m_menuInfos[n].GetLabel(),
1925 flags,
1926 m_menuInfos[n].GetAccelIndex()
1927 );
1928 }
1929}
1930
1931// ----------------------------------------------------------------------------
1932// wxMenuBar geometry
1933// ----------------------------------------------------------------------------
1934
1935wxRect wxMenuBar::GetItemRect(size_t pos) const
1936{
1937 wxASSERT_MSG( pos < GetCount(), _T("invalid menu bar item index") );
4087064a 1938 wxASSERT_MSG( IsCreated(), _T("can't call this method yet") );
1e6feb95
VZ
1939
1940 wxRect rect;
1941 rect.x =
1942 rect.y = 0;
1943 rect.height = GetClientSize().y;
1944
1945 for ( size_t n = 0; n < pos; n++ )
1946 {
1947 rect.x += GetItemWidth(n);
1948 }
1949
1950 rect.width = GetItemWidth(pos);
1951
1952 return rect;
1953}
1954
1955wxSize wxMenuBar::DoGetBestClientSize() const
1956{
1957 wxSize size;
1958 if ( GetMenuCount() > 0 )
1959 {
1960 wxClientDC dc(wxConstCast(this, wxMenuBar));
a756f210 1961 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
1e6feb95
VZ
1962 dc.GetTextExtent(GetLabelTop(0), &size.x, &size.y);
1963
1964 // adjust for the renderer we use
1965 size = GetRenderer()->GetMenuBarItemSize(size);
1966 }
1967 else // empty menubar
1968 {
1969 size.x =
1970 size.y = 0;
1971 }
1972
1973 // the width is arbitrary, of course, for horizontal menubar
1974 size.x = 100;
1975
1976 return size;
1977}
1978
1979int wxMenuBar::GetMenuFromPoint(const wxPoint& pos) const
1980{
1981 if ( pos.x < 0 || pos.y < 0 || pos.y > GetClientSize().y )
1982 return -1;
1983
1984 // do find it
1985 wxCoord x = 0;
1986 size_t count = GetCount();
1987 for ( size_t item = 0; item < count; item++ )
1988 {
1989 x += GetItemWidth(item);
1990
1991 if ( x > pos.x )
1992 {
1993 return item;
1994 }
1995 }
1996
1997 // to the right of the last menu item
1998 return -1;
1999}
2000
2001// ----------------------------------------------------------------------------
2002// wxMenuBar menu operations
2003// ----------------------------------------------------------------------------
2004
2005void wxMenuBar::SelectMenu(size_t pos)
2006{
2007 SetFocus();
5a750e5a 2008 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::SelectMenu"));
1e6feb95
VZ
2009 CaptureMouse();
2010
2011 DoSelectMenu(pos);
2012}
2013
2014void wxMenuBar::DoSelectMenu(size_t pos)
2015{
2016 wxCHECK_RET( pos < GetCount(), _T("invalid menu index in DoSelectMenu") );
2017
23d8bb2c
VZ
2018 int posOld = m_current;
2019
2020 m_current = pos;
2021
2022 if ( posOld != -1 )
1e6feb95
VZ
2023 {
2024 // close the previous menu
2025 if ( IsShowingMenu() )
2026 {
2027 // restore m_shouldShowMenu flag after DismissMenu() which resets
2028 // it to FALSE
2029 bool old = m_shouldShowMenu;
2030
2031 DismissMenu();
2032
2033 m_shouldShowMenu = old;
2034 }
2035
23d8bb2c 2036 RefreshItem((size_t)posOld);
1e6feb95
VZ
2037 }
2038
1e6feb95
VZ
2039 RefreshItem(pos);
2040}
2041
2042void wxMenuBar::PopupMenu(size_t pos)
2043{
2044 wxCHECK_RET( pos < GetCount(), _T("invalid menu index in PopupCurrentMenu") );
2045
2046 SetFocus();
2047 DoSelectMenu(pos);
2048 PopupCurrentMenu();
2049}
2050
2051// ----------------------------------------------------------------------------
2052// wxMenuBar input handing
2053// ----------------------------------------------------------------------------
2054
2055/*
2056 Note that wxMenuBar doesn't use wxInputHandler but handles keyboard and
2057 mouse in the same way under all platforms. This is because it doesn't derive
2058 from wxControl (which works with input handlers) but directly from wxWindow.
2059
2060 Also, menu bar input handling is rather simple, so maybe it's not really
2061 worth making it themeable - at least I've decided against doing it now as it
2062 would merging the changes back into trunk more difficult. But it still could
2063 be done later if really needed.
2064 */
2065
2066void wxMenuBar::OnKillFocus(wxFocusEvent& event)
2067{
2068 if ( m_current != -1 )
2069 {
2070 RefreshItem((size_t)m_current);
2071
2072 m_current = -1;
2073 }
2074
2075 event.Skip();
2076}
2077
2078void wxMenuBar::OnLeftDown(wxMouseEvent& event)
2079{
2080 if ( HasCapture() )
2081 {
2082 OnDismiss();
2083
2084 event.Skip();
2085 }
2086 else // we didn't have mouse capture, capture it now
2087 {
2088 m_current = GetMenuFromPoint(event.GetPosition());
2089 if ( m_current == -1 )
2090 {
2091 // unfortunately, we can't prevent wxMSW from giving us the focus,
2092 // so we can only give it back
2093 GiveAwayFocus();
2094 }
2095 else // on item
2096 {
5a750e5a 2097 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::OnLeftDown"));
1e6feb95
VZ
2098 CaptureMouse();
2099
2100 // show it as selected
2101 RefreshItem((size_t)m_current);
2102
2103 // show the menu
2104 PopupCurrentMenu(FALSE /* don't select first item - as Windows does */);
2105 }
2106 }
2107}
2108
2109void wxMenuBar::OnMouseMove(wxMouseEvent& event)
2110{
2111 if ( HasCapture() )
2112 {
2113 (void)ProcessMouseEvent(event.GetPosition());
2114 }
2115 else
2116 {
2117 event.Skip();
2118 }
2119}
2120
2121bool wxMenuBar::ProcessMouseEvent(const wxPoint& pt)
2122{
2123 // a hack to ignore the extra mouse events MSW sends us: this is similar to
2124 // wxUSE_MOUSEEVENT_HACK in wxWin itself but it isn't enough for us here as
2125 // we get the messages from different windows (old and new popup menus for
2126 // example)
2127#ifdef __WXMSW__
2128 static wxPoint s_ptLast;
2129 if ( pt == s_ptLast )
2130 {
2131 return FALSE;
2132 }
2133
2134 s_ptLast = pt;
2135#endif // __WXMSW__
2136
2137 int currentNew = GetMenuFromPoint(pt);
2138 if ( (currentNew == -1) || (currentNew == m_current) )
2139 {
2140 return FALSE;
2141 }
2142
2143 // select the new active item
2144 DoSelectMenu(currentNew);
2145
2146 // show the menu if we know that we should, even if we hadn't been showing
2147 // it before (this may happen if the previous menu was disabled)
54800df8 2148 if ( m_shouldShowMenu && !m_menuShown)
1e6feb95
VZ
2149 {
2150 // open the new menu if the old one we closed had been opened
2151 PopupCurrentMenu(FALSE /* don't select first item - as Windows does */);
2152 }
2153
2154 return TRUE;
2155}
2156
2157void wxMenuBar::OnKeyDown(wxKeyEvent& event)
2158{
23d8bb2c
VZ
2159 // ensure that we have a current item - we might not have it if we're
2160 // given the focus with Alt or F10 press (and under GTK+ the menubar
2161 // somehow gets the keyboard events even when it doesn't have focus...)
2162 if ( m_current == -1 )
2163 {
2164 if ( !HasCapture() )
2165 {
2166 SelectMenu(0);
2167 }
2168 else // we do have capture
2169 {
2170 // we always maintain a valid current item while we're in modal
2171 // state (i.e. have the capture)
2172 wxFAIL_MSG( _T("how did we manage to lose current item?") );
2173
2174 return;
2175 }
2176 }
1e6feb95
VZ
2177
2178 int key = event.GetKeyCode();
2179
2180 // first let the menu have it
2181 if ( IsShowingMenu() && m_menuShown->ProcessKeyDown(key) )
2182 {
2183 return;
2184 }
2185
2186 // cycle through the menu items when left/right arrows are pressed and open
2187 // the menu when up/down one is
2188 switch ( key )
2189 {
2190 case WXK_MENU:
2191 // Alt must be processed at wxWindow level too
2192 event.Skip();
2193 // fall through
2194
2195 case WXK_ESCAPE:
2196 // remove the selection and give the focus away
2197 if ( m_current != -1 )
2198 {
2199 if ( IsShowingMenu() )
2200 {
2201 DismissMenu();
2202 }
2203
2204 OnDismiss();
2205 }
2206 break;
2207
2208 case WXK_LEFT:
2209 case WXK_RIGHT:
2210 {
2211 size_t count = GetCount();
2212 if ( count == 1 )
2213 {
2214 // the item won't change anyhow
2215 break;
2216 }
2217 //else: otherwise, it will
2218
2219 // remember if we were showing a menu - if we did, we should
2220 // show the new menu after changing the item
2221 bool wasMenuOpened = IsShowingMenu();
2222 if ( wasMenuOpened )
2223 {
2224 DismissMenu();
2225 }
2226
2227 // cast is safe as we tested for -1 above
2228 size_t currentNew = (size_t)m_current;
2229
2230 if ( key == WXK_LEFT )
2231 {
2232 if ( currentNew-- == 0 )
2233 currentNew = count - 1;
2234 }
2235 else // right
2236 {
6522713c 2237 if ( ++currentNew == count )
1e6feb95
VZ
2238 currentNew = 0;
2239 }
2240
2241 DoSelectMenu(currentNew);
2242
2243 if ( wasMenuOpened )
2244 {
2245 PopupCurrentMenu();
2246 }
2247 }
2248 break;
2249
2250 case WXK_DOWN:
2251 case WXK_UP:
2252 case WXK_RETURN:
2253 // open the menu
2254 PopupCurrentMenu();
2255 break;
2256
2257 default:
2258 // letters open the corresponding menu
2259 {
2260 bool unique;
2261 int idxFound = FindNextItemForAccel(m_current, key, &unique);
2262
2263 if ( idxFound != -1 )
2264 {
2265 if ( IsShowingMenu() )
2266 {
2267 DismissMenu();
2268 }
2269
2270 DoSelectMenu((size_t)idxFound);
2271
2272 // if the item is not unique, just select it but don't
2273 // activate as the user might have wanted to activate
2274 // another item
2275 //
2276 // also, don't try to open a disabled menu
2277 if ( unique && IsEnabledTop((size_t)idxFound) )
2278 {
2279 // open the menu
2280 PopupCurrentMenu();
2281 }
2282
2283 // skip the "event.Skip()" below
2284 break;
2285 }
2286 }
2287
2288 event.Skip();
2289 }
2290}
2291
2292// ----------------------------------------------------------------------------
2293// wxMenuBar accel handling
2294// ----------------------------------------------------------------------------
2295
2296int wxMenuBar::FindNextItemForAccel(int idxStart, int key, bool *unique) const
2297{
2298 if ( !wxIsalnum(key) )
2299 {
2300 // we only support letters/digits as accels
2301 return -1;
2302 }
2303
2304 // do we have more than one item with this accel?
2305 if ( unique )
2306 *unique = TRUE;
2307
2308 // translate everything to lower case before comparing
2309 wxChar chAccel = wxTolower(key);
2310
2311 // the index of the item with this accel
2312 int idxFound = -1;
2313
2314 // loop through all items searching for the item with this
2315 // accel starting at the item after the current one
2316 int count = GetCount();
2317 int n = idxStart == -1 ? 0 : idxStart + 1;
2318
2319 if ( n == count )
2320 {
2321 // wrap
2322 n = 0;
2323 }
2324
2325 idxStart = n;
2326 for ( ;; )
2327 {
2328 const wxMenuInfo& info = m_menuInfos[n];
2329
2330 int idxAccel = info.GetAccelIndex();
2331 if ( idxAccel != -1 &&
2332 wxTolower(info.GetLabel()[(size_t)idxAccel])
2333 == chAccel )
2334 {
2335 // ok, found an item with this accel
2336 if ( idxFound == -1 )
2337 {
2338 // store it but continue searching as we need to
2339 // know if it's the only item with this accel or if
2340 // there are more
2341 idxFound = n;
2342 }
2343 else // we already had found such item
2344 {
2345 if ( unique )
2346 *unique = FALSE;
2347
2348 // no need to continue further, we won't find
2349 // anything we don't already know
2350 break;
2351 }
2352 }
2353
2354 // we want to iterate over all items wrapping around if
2355 // necessary
2356 if ( ++n == count )
2357 {
2358 // wrap
2359 n = 0;
2360 }
2361
2362 if ( n == idxStart )
2363 {
2364 // we've seen all items
2365 break;
2366 }
2367 }
2368
2369 return idxFound;
2370}
2371
2372#if wxUSE_ACCEL
2373
2374bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent& event)
2375{
2376 size_t n = 0;
ac32ba44 2377 for ( wxMenuList::compatibility_iterator node = m_menus.GetFirst();
1e6feb95
VZ
2378 node;
2379 node = node->GetNext(), n++ )
2380 {
2381 // accels of the items in the disabled menus shouldn't work
2382 if ( m_menuInfos[n].IsEnabled() )
2383 {
2384 if ( node->GetData()->ProcessAccelEvent(event) )
2385 {
2386 // menu processed it
2387 return TRUE;
2388 }
2389 }
2390 }
2391
2392 // not found
2393 return FALSE;
2394}
2395
2396#endif // wxUSE_ACCEL
2397
2398// ----------------------------------------------------------------------------
2399// wxMenuBar menus showing
2400// ----------------------------------------------------------------------------
2401
2402void wxMenuBar::PopupCurrentMenu(bool selectFirst)
2403{
2404 wxCHECK_RET( m_current != -1, _T("no menu to popup") );
2405
2406 // forgot to call DismissMenu()?
8601b2e1 2407 wxASSERT_MSG( !m_menuShown, _T("shouldn't show two menus at once!") );
1e6feb95
VZ
2408
2409 // in any case, we should show it - even if we won't
2410 m_shouldShowMenu = TRUE;
2411
2412 if ( IsEnabledTop(m_current) )
2413 {
2414 // remember the menu we show
2415 m_menuShown = GetMenu(m_current);
2416
2417 // we don't show the menu at all if it has no items
2418 if ( !m_menuShown->IsEmpty() )
2419 {
2420 // position it correctly: note that we must use screen coords and
2421 // that we pass 0 as width to position the menu exactly below the
2422 // item, not to the right of it
2423 wxRect rectItem = GetItemRect(m_current);
1f752a22 2424
1e6feb95
VZ
2425 m_menuShown->Popup(ClientToScreen(rectItem.GetPosition()),
2426 wxSize(0, rectItem.GetHeight()),
2427 selectFirst);
2428 }
2429 else
2430 {
2431 // reset it back as no menu is shown
2432 m_menuShown = NULL;
2433 }
2434 }
2435 //else: don't show disabled menu
2436}
2437
2438void wxMenuBar::DismissMenu()
2439{
2440 wxCHECK_RET( m_menuShown, _T("can't dismiss menu if none is shown") );
2441
2442 m_menuShown->Dismiss();
2443 OnDismissMenu();
2444}
2445
2446void wxMenuBar::OnDismissMenu(bool dismissMenuBar)
2447{
2448 m_shouldShowMenu = FALSE;
2449 m_menuShown = NULL;
2450 if ( dismissMenuBar )
2451 {
2452 OnDismiss();
2453 }
2454}
2455
2456void wxMenuBar::OnDismiss()
2457{
a9a05059 2458 if ( GetCapture() )
7edcafa4 2459 {
5a750e5a 2460 wxLogTrace(_T("mousecapture"), _T("Releasing mouse from wxMenuBar::OnDismiss"));
a9a05059 2461 GetCapture()->ReleaseMouse();
7edcafa4 2462 }
1e6feb95
VZ
2463
2464 if ( m_current != -1 )
2465 {
23d8bb2c 2466 size_t current = m_current;
1e6feb95 2467 m_current = -1;
23d8bb2c
VZ
2468
2469 RefreshItem(current);
1e6feb95
VZ
2470 }
2471
2472 GiveAwayFocus();
2473}
2474
2475void wxMenuBar::GiveAwayFocus()
2476{
2477 GetFrame()->SetFocus();
2478}
2479
2480// ----------------------------------------------------------------------------
2481// popup menu support
2482// ----------------------------------------------------------------------------
2483
2484wxEventLoop *wxWindow::ms_evtLoopPopup = NULL;
2485
2486bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
2487{
2488 wxCHECK_MSG( !ms_evtLoopPopup, FALSE,
2489 _T("can't show more than one popup menu at a time") );
2490
2491#ifdef __WXMSW__
2492 // we need to change the cursor before showing the menu as, apparently, no
2493 // cursor changes took place while the mouse is captured
2494 wxCursor cursorOld = GetCursor();
2495 SetCursor(wxCURSOR_ARROW);
2496#endif // __WXMSW__
2497
2498#if 0
2499 // flash any delayed log messages before showing the menu, otherwise it
2500 // could be dismissed (because it would lose focus) immediately after being
2501 // shown
2502 wxLog::FlushActive();
2503
2504 // some controls update themselves from OnIdle() call - let them do it
e39af974 2505 wxTheApp->ProcessIdle();
1e6feb95
VZ
2506
2507 // if the window hadn't been refreshed yet, the menu can adversely affect
2508 // its next OnPaint() handler execution - i.e. scrolled window refresh
2509 // logic breaks then as it scrolls part of the menu which hadn't been there
2510 // when the update event was generated into view
2511 Update();
2512#endif // 0
2513
2514 menu->SetInvokingWindow(this);
e441e1f4 2515
ee351013 2516 // wxLogDebug( "Name of invoking window %s", menu->GetInvokingWindow()->GetName().c_str() );
e441e1f4 2517
1e6feb95
VZ
2518 menu->Popup(ClientToScreen(wxPoint(x, y)), wxSize(0, 0));
2519
2520 // this is not very useful if the menu was popped up because of the mouse
2521 // click but I think it is nice to do when it appears because of a key
2522 // press (i.e. Windows menu key)
2523 //
2524 // Windows itself doesn't do it, but IMHO this is nice
2525 WarpPointer(x, y);
2526
2527 // we have to redirect all keyboard input to the menu temporarily
2528 PushEventHandler(new wxMenuKbdRedirector(menu));
2529
2530 // enter the local modal loop
2531 ms_evtLoopPopup = new wxEventLoop;
2532 ms_evtLoopPopup->Run();
2533
2534 delete ms_evtLoopPopup;
2535 ms_evtLoopPopup = NULL;
2536
2537 // remove the handler
2538 PopEventHandler(TRUE /* delete it */);
2539
2540 menu->SetInvokingWindow(NULL);
2541
2542#ifdef __WXMSW__
2543 SetCursor(cursorOld);
2544#endif // __WXMSW__
2545
2546 return TRUE;
2547}
2548
2549void wxWindow::DismissPopupMenu()
2550{
2551 wxCHECK_RET( ms_evtLoopPopup, _T("no popup menu shown") );
e441e1f4 2552
1e6feb95
VZ
2553 ms_evtLoopPopup->Exit();
2554}
2555
2556#endif // wxUSE_MENUS
2557