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