]> git.saurik.com Git - wxWidgets.git/blob - src/univ/menu.cpp
1. derive wxGTK wxRadioBox from wxRadioBoxBase now, as in all other ports
[wxWidgets.git] / src / univ / menu.cpp
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$
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/dynarray.h"
28 #include "wx/control.h" // for FindAccelIndex()
29 #include "wx/menu.h"
30 #include "wx/settings.h"
31 #include "wx/accel.h"
32 #include "wx/log.h"
33 #endif // WX_PRECOMP
34
35 #if wxUSE_MENUS
36
37 #include "wx/popupwin.h"
38 #include "wx/evtloop.h"
39 #include "wx/dcclient.h"
40 #include "wx/frame.h"
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
52 class WXDLLEXPORT wxMenuInfo
53 {
54 public:
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
73 void SetEnabled(bool enabled = true) { m_isEnabled = enabled; }
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
91 private:
92 void CalcWidth(wxMenuBar *menubar)
93 {
94 wxSize size;
95 wxClientDC dc(menubar);
96 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
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
111 WX_DEFINE_OBJARRAY(wxMenuInfoArray);
112
113 // ----------------------------------------------------------------------------
114 // wxPopupMenuWindow: a popup window showing a menu
115 // ----------------------------------------------------------------------------
116
117 class wxPopupMenuWindow : public wxPopupTransientWindow
118 {
119 public:
120 wxPopupMenuWindow(wxWindow *parent, wxMenu *menu);
121
122 ~wxPopupMenuWindow();
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
134 void OnSubmenuDismiss(bool dismissParent);
135
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__
140 void OnIdle(wxIdleEvent& WXUNUSED(event)) { }
141 #endif
142
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
150 wxMenuItemList::compatibility_iterator GetMenuItemFromPoint(const wxPoint& pt) const;
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
158 // process the key event, return true if done
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
167 protected:
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
188 void SetCurrent(wxMenuItemList::compatibility_iterator node);
189 virtual bool SetCurrent(bool doit = true){return wxPopupTransientWindow::SetCurrent(doit);};
190
191 // change the current item refreshing the old and new items
192 void ChangeCurrent(wxMenuItemList::compatibility_iterator node);
193
194 // activate item, i.e. call either ClickItem() or OpenSubmenu() depending
195 // on what it is, return true if something was done (i.e. it's not a
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
222 wxMenuItemList::compatibility_iterator GetPrevNode() const;
223
224 // get previous node before the given one, wrapping if it's the first one
225 wxMenuItemList::compatibility_iterator GetPrevNode(wxMenuItemList::compatibility_iterator node) const;
226
227 // get next node after the current one
228 wxMenuItemList::compatibility_iterator GetNextNode() const;
229
230 // get next node after the given one, wrapping if it's the last one
231 wxMenuItemList::compatibility_iterator GetNextNode(wxMenuItemList::compatibility_iterator node) const;
232
233 private:
234 // the menu we show
235 wxMenu *m_menu;
236
237 // the menu node corresponding to the current item
238 wxMenuItemList::compatibility_iterator m_nodeCurrent;
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
250 class wxMenuKbdRedirector : public wxEvtHandler
251 {
252 public:
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 {
263 // return false;
264
265 return wxEvtHandler::ProcessEvent(event);
266 }
267 }
268
269 private:
270 wxMenu *m_menu;
271 };
272
273 // ----------------------------------------------------------------------------
274 // wxWin macros
275 // ----------------------------------------------------------------------------
276
277 IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
278 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow)
279 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
280
281 BEGIN_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)
287 #ifdef __WXMSW__
288 EVT_IDLE(wxPopupMenuWindow::OnIdle)
289 #endif
290 END_EVENT_TABLE()
291
292 BEGIN_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)
299 END_EVENT_TABLE()
300
301 // ============================================================================
302 // implementation
303 // ============================================================================
304
305 // ----------------------------------------------------------------------------
306 // wxPopupMenuWindow
307 // ----------------------------------------------------------------------------
308
309 wxPopupMenuWindow::wxPopupMenuWindow(wxWindow *parent, wxMenu *menu)
310 {
311 m_menu = menu;
312 m_hasOpenSubMenu = false;
313
314 ResetCurrent();
315
316 (void)Create(parent, wxBORDER_RAISED);
317
318 SetCursor(wxCURSOR_ARROW);
319 }
320
321 wxPopupMenuWindow::~wxPopupMenuWindow()
322 {
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;
329 }
330
331 // ----------------------------------------------------------------------------
332 // wxPopupMenuWindow current item/node handling
333 // ----------------------------------------------------------------------------
334
335 void wxPopupMenuWindow::ResetCurrent()
336 {
337 #if wxUSE_STL
338 SetCurrent(wxMenuItemList::compatibility_iterator());
339 #else
340 SetCurrent((wxwxMenuItemListNode *)NULL);
341 #endif
342 }
343
344 void wxPopupMenuWindow::SetCurrent(wxMenuItemList::compatibility_iterator node)
345 {
346 m_nodeCurrent = node;
347 }
348
349 void wxPopupMenuWindow::ChangeCurrent(wxMenuItemList::compatibility_iterator node)
350 {
351 if ( node != m_nodeCurrent )
352 {
353 wxMenuItemList::compatibility_iterator nodeOldCurrent = m_nodeCurrent;
354
355 m_nodeCurrent = node;
356
357 if ( nodeOldCurrent )
358 {
359 wxMenuItem *item = nodeOldCurrent->GetData();
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();
366 OnSubmenuDismiss( false );
367 }
368
369 RefreshItem(item);
370 }
371
372 if ( m_nodeCurrent )
373 RefreshItem(m_nodeCurrent->GetData());
374 }
375 }
376
377 wxMenuItemList::compatibility_iterator wxPopupMenuWindow::GetPrevNode() const
378 {
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();
382 }
383
384 wxMenuItemList::compatibility_iterator
385 wxPopupMenuWindow::GetPrevNode(wxMenuItemList::compatibility_iterator node) const
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
400 wxMenuItemList::compatibility_iterator wxPopupMenuWindow::GetNextNode() const
401 {
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();
405 }
406
407 wxMenuItemList::compatibility_iterator
408 wxPopupMenuWindow::GetNextNode(wxMenuItemList::compatibility_iterator node) const
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
427 void 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
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
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
465 void 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();
476 OnSubmenuDismiss( false );
477 }
478
479 wxPopupTransientWindow::Dismiss();
480
481 ResetCurrent();
482 }
483
484 void 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
488 HandleDismiss(true);
489 }
490
491 void wxPopupMenuWindow::OnSubmenuDismiss(bool WXUNUSED(dismissParent))
492 {
493 m_hasOpenSubMenu = false;
494 }
495
496 void wxPopupMenuWindow::HandleDismiss(bool dismissParent)
497 {
498 m_menu->OnDismiss(dismissParent);
499 }
500
501 void wxPopupMenuWindow::DismissAndNotify()
502 {
503 Dismiss();
504 HandleDismiss(true);
505 }
506
507 // ----------------------------------------------------------------------------
508 // wxPopupMenuWindow geometry
509 // ----------------------------------------------------------------------------
510
511 wxMenuItemList::compatibility_iterator
512 wxPopupMenuWindow::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;
519 for ( wxMenuItemList::compatibility_iterator node = m_menu->GetMenuItems().GetFirst();
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
533 #if wxUSE_STL
534 return wxMenuItemList::compatibility_iterator();
535 #else
536 return NULL;
537 #endif
538 }
539
540 // ----------------------------------------------------------------------------
541 // wxPopupMenuWindow drawing
542 // ----------------------------------------------------------------------------
543
544 void 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
555 void 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();
561 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
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();
571 for ( wxMenuItemList::compatibility_iterator node = m_menu->GetMenuItems().GetFirst();
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
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
618 rend->DrawMenuItem
619 (
620 dc,
621 y,
622 gi,
623 item->GetLabel(),
624 item->GetAccelString(),
625 bmp,
626 flags,
627 item->GetAccelIndex()
628 );
629 }
630
631 y += item->GetHeight();
632 }
633 }
634
635 // ----------------------------------------------------------------------------
636 // wxPopupMenuWindow actions
637 // ----------------------------------------------------------------------------
638
639 void 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
646 wxMenu* menu = m_menu;
647
648 // close all menus
649 DismissAndNotify();
650
651 menu->ClickItem(item);
652 }
653
654 void 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
666 m_hasOpenSubMenu = true;
667 }
668
669 bool wxPopupMenuWindow::ActivateItem(wxMenuItem *item, InputMethod how)
670 {
671 // don't activate disabled items
672 if ( !item || !item->IsEnabled() )
673 {
674 return false;
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 {
689 return false;
690 }
691
692 return true;
693 }
694
695 // ----------------------------------------------------------------------------
696 // wxPopupMenuWindow input handling
697 // ----------------------------------------------------------------------------
698
699 bool 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
714 wxCHECK_MSG( win, false, _T("parent menu not shown?") );
715
716 pos = ClientToScreen(pos);
717 if ( win->GetMenuItemFromPoint(win->ScreenToClient(pos)) )
718 {
719 // eat the event
720 return true;
721 }
722 //else: it is outside the parent menu as well, do dismiss this one
723 }
724 }
725
726 return false;
727 }
728
729 void wxPopupMenuWindow::OnLeftUp(wxMouseEvent& event)
730 {
731 wxMenuItemList::compatibility_iterator node = GetMenuItemFromPoint(event.GetPosition());
732 if ( node )
733 {
734 ActivateItem(node->GetData(), WithMouse);
735 }
736 }
737
738 void 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
763 void wxPopupMenuWindow::ProcessMouseMove(const wxPoint& pt)
764 {
765 wxMenuItemList::compatibility_iterator node = GetMenuItemFromPoint(pt);
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
824 void 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
851 resetCurrent = true;
852 }
853
854 if ( resetCurrent )
855 {
856 #if wxUSE_STL
857 ChangeCurrent(wxMenuItemList::compatibility_iterator());
858 #else
859 ChangeCurrent(NULL);
860 #endif
861 }
862 }
863
864 event.Skip();
865 }
866
867 void wxPopupMenuWindow::OnKeyDown(wxKeyEvent& event)
868 {
869 wxMenuBar *menubar = m_menu->GetMenuBar();
870
871 if ( menubar )
872 {
873 menubar->ProcessEvent(event);
874 }
875 else if ( !ProcessKeyDown(event.GetKeyCode()) )
876 {
877 event.Skip();
878 }
879 }
880
881 bool 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 {
890 wxCHECK_MSG( CanOpen(item), false,
891 _T("has open submenu but another item selected?") );
892
893 if ( item->GetSubMenu()->ProcessKeyDown(key) )
894 return true;
895 }
896
897 bool processed = true;
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 {
909 processed = false;
910 break;
911 }
912
913 // fall through
914
915 case WXK_ESCAPE:
916 // close just this menu
917 Dismiss();
918 HandleDismiss(false);
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
938 wxMenuItemList::compatibility_iterator nodeStart = up ? GetPrevNode()
939 : GetNextNode(),
940 node = nodeStart;
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
949 #if wxUSE_STL
950 node = wxMenuItemList::compatibility_iterator();
951 #else
952 node = NULL;
953 #endif
954 }
955 }
956
957 if ( node )
958 {
959 ChangeCurrent(node);
960 }
961 else
962 {
963 processed = false;
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 {
976 processed = false;
977 }
978 break;
979
980 default:
981 // look for the menu item starting with this letter
982 if ( wxIsalnum((wxChar)key) )
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
987 wxMenuItemList::compatibility_iterator nodeStart = GetNextNode();
988
989 // do we have more than one item with this accel?
990 bool notUnique = false;
991
992 // translate everything to lower case before comparing
993 wxChar chAccel = (wxChar)wxTolower(key);
994
995 // loop through all items searching for the item with this
996 // accel
997 wxMenuItemList::compatibility_iterator node = nodeStart,
998 #if wxUSE_STL
999 nodeFound = wxMenuItemList::compatibility_iterator();
1000 #else
1001 nodeFound = NULL;
1002 #endif
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 {
1022 notUnique = true;
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
1055 // skip "processed = false" below
1056 break;
1057 }
1058 }
1059
1060 processed = false;
1061 }
1062
1063 return processed;
1064 }
1065
1066 // ----------------------------------------------------------------------------
1067 // wxMenu
1068 // ----------------------------------------------------------------------------
1069
1070 void wxMenu::Init()
1071 {
1072 m_geometry = NULL;
1073
1074 m_popupMenu = NULL;
1075
1076 m_startRadioGroup = -1;
1077 }
1078
1079 wxMenu::~wxMenu()
1080 {
1081 delete m_geometry;
1082 delete m_popupMenu;
1083 }
1084
1085 // ----------------------------------------------------------------------------
1086 // wxMenu and wxMenuGeometryInfo
1087 // ----------------------------------------------------------------------------
1088
1089 wxMenuGeometryInfo::~wxMenuGeometryInfo()
1090 {
1091 }
1092
1093 const 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
1111 void 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
1124 void 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
1140 void wxMenu::EndRadioGroup()
1141 {
1142 // we're not inside a radio group any longer
1143 m_startRadioGroup = -1;
1144 }
1145
1146 wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
1147 {
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);
1160 }
1161 else // extend the current radio group
1162 {
1163 // we need to update its end item
1164 item->SetRadioGroupStart(m_startRadioGroup);
1165 wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup);
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
1182 if ( !wxMenuBase::DoAppend(item) )
1183 return NULL;
1184
1185 OnItemAdded(item);
1186
1187 return item;
1188 }
1189
1190 wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
1191 {
1192 if ( !wxMenuBase::DoInsert(pos, item) )
1193 return NULL;
1194
1195 OnItemAdded(item);
1196
1197 return item;
1198 }
1199
1200 wxMenuItem *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
1220 void 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
1237 void wxMenu::Detach()
1238 {
1239 wxMenuBase::Detach();
1240 }
1241
1242 // ----------------------------------------------------------------------------
1243 // wxMenu misc functions
1244 // ----------------------------------------------------------------------------
1245
1246 wxWindow *wxMenu::GetRootWindow() const
1247 {
1248 if ( GetMenuBar() )
1249 {
1250 // simple case - a normal menu attached to the menubar
1251 return GetMenuBar();
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 {
1267 // We are a submenu of a menu of a menubar
1268 if (menu->GetMenuBar())
1269 return menu->GetMenuBar();
1270
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?") );
1281
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
1289 wxRenderer *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
1297 void 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
1318 bool wxMenu::IsShown() const
1319 {
1320 return m_popupMenu && m_popupMenu->IsShown();
1321 }
1322
1323 void 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 {
1331 win->OnSubmenuDismiss( true );
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();
1343 m_menuParent->OnDismiss(true);
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();
1358
1359 // Why reset it here? We need it for sending the event to...
1360 // SetInvokingWindow(NULL);
1361 }
1362 }
1363 }
1364
1365 void 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 }
1378
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
1396 void 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
1407 bool wxMenu::ProcessKeyDown(int key)
1408 {
1409 wxCHECK_MSG( m_popupMenu, false,
1410 _T("can't process key events if not shown") );
1411
1412 return m_popupMenu->ProcessKeyDown(key);
1413 }
1414
1415 bool 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 }
1430
1431 return SendEvent(item->GetId(), isChecked);
1432 }
1433
1434 // ----------------------------------------------------------------------------
1435 // wxMenu accel support
1436 // ----------------------------------------------------------------------------
1437
1438 #if wxUSE_ACCEL
1439
1440 bool 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
1450 for ( wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
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 {
1460 return true;
1461 }
1462 }
1463 }
1464
1465 return false;
1466 }
1467
1468 void 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
1481 void 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
1498 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
1499 int id,
1500 const wxString& text,
1501 const wxString& help,
1502 wxItemKind kind,
1503 wxMenu *subMenu)
1504 : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu)
1505 {
1506 m_posY =
1507 m_height = wxDefaultCoord;
1508
1509 m_radioGroup.start = -1;
1510 m_isRadioGroupStart = false;
1511
1512 m_bmpDisabled = wxNullBitmap;
1513
1514 UpdateAccelInfo();
1515 }
1516
1517 wxMenuItem::~wxMenuItem()
1518 {
1519 }
1520
1521 // ----------------------------------------------------------------------------
1522 // wxMenuItemBase methods implemented here
1523 // ----------------------------------------------------------------------------
1524
1525 /* static */
1526 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
1527 int id,
1528 const wxString& name,
1529 const wxString& help,
1530 wxItemKind kind,
1531 wxMenu *subMenu)
1532 {
1533 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
1534 }
1535
1536 /* static */
1537 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
1538 {
1539 return wxStripMenuCodes(text);
1540 }
1541
1542 // ----------------------------------------------------------------------------
1543 // wxMenuItem operations
1544 // ----------------------------------------------------------------------------
1545
1546 void wxMenuItem::NotifyMenu()
1547 {
1548 m_parentMenu->RefreshItem(this);
1549 }
1550
1551 void 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
1559 void 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
1572 void wxMenuItem::SetCheckable(bool checkable)
1573 {
1574 if ( checkable != IsCheckable() )
1575 {
1576 wxMenuItemBase::SetCheckable(checkable);
1577
1578 NotifyMenu();
1579 }
1580 }
1581
1582 void wxMenuItem::SetBitmaps(const wxBitmap& bmpChecked,
1583 const wxBitmap& bmpUnchecked)
1584 {
1585 m_bmpChecked = bmpChecked;
1586 m_bmpUnchecked = bmpUnchecked;
1587
1588 NotifyMenu();
1589 }
1590
1591 void wxMenuItem::Enable(bool enable)
1592 {
1593 if ( enable != m_isEnabled )
1594 {
1595 wxMenuItemBase::Enable(enable);
1596
1597 NotifyMenu();
1598 }
1599 }
1600
1601 void wxMenuItem::Check(bool check)
1602 {
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 )
1609 {
1610 // it doesn't make sense to uncheck a radio item - what would this do?
1611 if ( !check )
1612 return;
1613
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
1638 wxMenuItemList::compatibility_iterator node = items.Item(start);
1639 for ( int n = start; n <= end && node; n++ )
1640 {
1641 if ( n != pos )
1642 {
1643 node->GetData()->m_isChecked = false;
1644 }
1645 node = node->GetNext();
1646 }
1647 }
1648
1649 wxMenuItemBase::Check(check);
1650
1651 NotifyMenu();
1652 }
1653
1654 // radio group stuff
1655 // -----------------
1656
1657 void wxMenuItem::SetAsRadioGroupStart()
1658 {
1659 m_isRadioGroupStart = true;
1660 }
1661
1662 void 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
1670 void 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;
1676 }
1677
1678 // ----------------------------------------------------------------------------
1679 // wxMenuBar creation
1680 // ----------------------------------------------------------------------------
1681
1682 void wxMenuBar::Init()
1683 {
1684 m_frameLast = NULL;
1685
1686 m_current = -1;
1687
1688 m_menuShown = NULL;
1689
1690 m_shouldShowMenu = false;
1691 }
1692
1693 wxMenuBar::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
1701 void 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 :-(
1722 (void)Create(frame, wxID_ANY);
1723
1724 SetCursor(wxCURSOR_ARROW);
1725
1726 SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT));
1727
1728 // calculate and set our height (it won't be changed any more)
1729 SetSize(wxDefaultCoord, GetBestSize().y);
1730 }
1731
1732 // remember the last frame which had us to avoid unnecessarily reparenting
1733 // above
1734 m_frameLast = frame;
1735 }
1736
1737 void 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
1748 wxMenuBar::~wxMenuBar()
1749 {
1750 }
1751
1752 // ----------------------------------------------------------------------------
1753 // wxMenuBar adding/removing items
1754 // ----------------------------------------------------------------------------
1755
1756 bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
1757 {
1758 return Insert(GetCount(), menu, title);
1759 }
1760
1761 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
1762 {
1763 if ( !wxMenuBarBase::Insert(pos, menu, title) )
1764 return false;
1765
1766 wxMenuInfo *info = new wxMenuInfo(title);
1767 m_menuInfos.Insert(info, pos);
1768
1769 RefreshAllItemsAfter(pos);
1770
1771 return true;
1772 }
1773
1774 wxMenu *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
1795 wxMenu *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
1815 wxCoord wxMenuBar::GetItemWidth(size_t pos) const
1816 {
1817 return m_menuInfos[pos].GetWidth(wxConstCast(this, wxMenuBar));
1818 }
1819
1820 void 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
1833 bool wxMenuBar::IsEnabledTop(size_t pos) const
1834 {
1835 wxCHECK_MSG( pos < GetCount(), false, _T("invalid index in IsEnabledTop") );
1836
1837 return m_menuInfos[pos].IsEnabled();
1838 }
1839
1840 void 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
1853 wxString wxMenuBar::GetLabelTop(size_t pos) const
1854 {
1855 wxCHECK_MSG( pos < GetCount(), wxEmptyString, _T("invalid index in GetLabelTop") );
1856
1857 return m_menuInfos[pos].GetLabel();
1858 }
1859
1860 // ----------------------------------------------------------------------------
1861 // wxMenuBar drawing
1862 // ----------------------------------------------------------------------------
1863
1864 void wxMenuBar::RefreshAllItemsAfter(size_t pos)
1865 {
1866 if ( !IsCreated() )
1867 {
1868 // no need to refresh if nothing is shown yet
1869 return;
1870 }
1871
1872 wxRect rect = GetItemRect(pos);
1873 rect.width = GetClientSize().x - rect.x;
1874 RefreshRect(rect);
1875 }
1876
1877 void wxMenuBar::RefreshItem(size_t pos)
1878 {
1879 wxCHECK_RET( pos != (size_t)-1,
1880 _T("invalid item in wxMenuBar::RefreshItem") );
1881
1882 if ( !IsCreated() )
1883 {
1884 // no need to refresh if nothing is shown yet
1885 return;
1886 }
1887
1888 RefreshRect(GetItemRect(pos));
1889 }
1890
1891 void wxMenuBar::DoDraw(wxControlRenderer *renderer)
1892 {
1893 wxDC& dc = renderer->GetDC();
1894 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
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
1953 wxRect wxMenuBar::GetItemRect(size_t pos) const
1954 {
1955 wxASSERT_MSG( pos < GetCount(), _T("invalid menu bar item index") );
1956 wxASSERT_MSG( IsCreated(), _T("can't call this method yet") );
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
1973 wxSize wxMenuBar::DoGetBestClientSize() const
1974 {
1975 wxSize size;
1976 if ( GetMenuCount() > 0 )
1977 {
1978 wxClientDC dc(wxConstCast(this, wxMenuBar));
1979 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
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
1997 int 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
2023 void wxMenuBar::SelectMenu(size_t pos)
2024 {
2025 SetFocus();
2026 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::SelectMenu"));
2027 CaptureMouse();
2028
2029 DoSelectMenu(pos);
2030 }
2031
2032 void wxMenuBar::DoSelectMenu(size_t pos)
2033 {
2034 wxCHECK_RET( pos < GetCount(), _T("invalid menu index in DoSelectMenu") );
2035
2036 int posOld = m_current;
2037
2038 m_current = pos;
2039
2040 if ( posOld != -1 )
2041 {
2042 // close the previous menu
2043 if ( IsShowingMenu() )
2044 {
2045 // restore m_shouldShowMenu flag after DismissMenu() which resets
2046 // it to false
2047 bool old = m_shouldShowMenu;
2048
2049 DismissMenu();
2050
2051 m_shouldShowMenu = old;
2052 }
2053
2054 RefreshItem((size_t)posOld);
2055 }
2056
2057 RefreshItem(pos);
2058 }
2059
2060 void 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
2084 void 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
2096 void 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 {
2115 wxLogTrace(_T("mousecapture"), _T("Capturing mouse from wxMenuBar::OnLeftDown"));
2116 CaptureMouse();
2117
2118 // show it as selected
2119 RefreshItem((size_t)m_current);
2120
2121 // show the menu
2122 PopupCurrentMenu(false /* don't select first item - as Windows does */);
2123 }
2124 }
2125 }
2126
2127 void wxMenuBar::OnMouseMove(wxMouseEvent& event)
2128 {
2129 if ( HasCapture() )
2130 {
2131 (void)ProcessMouseEvent(event.GetPosition());
2132 }
2133 else
2134 {
2135 event.Skip();
2136 }
2137 }
2138
2139 bool 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 {
2149 return false;
2150 }
2151
2152 s_ptLast = pt;
2153 #endif // __WXMSW__
2154
2155 int currentNew = GetMenuFromPoint(pt);
2156 if ( (currentNew == -1) || (currentNew == m_current) )
2157 {
2158 return false;
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)
2166 if ( m_shouldShowMenu && !m_menuShown)
2167 {
2168 // open the new menu if the old one we closed had been opened
2169 PopupCurrentMenu(false /* don't select first item - as Windows does */);
2170 }
2171
2172 return true;
2173 }
2174
2175 void wxMenuBar::OnKeyDown(wxKeyEvent& event)
2176 {
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 }
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 {
2208 case WXK_ALT:
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 {
2255 if ( ++currentNew == count )
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
2314 int wxMenuBar::FindNextItemForAccel(int idxStart, int key, bool *unique) const
2315 {
2316 if ( !wxIsalnum((wxChar)key) )
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 )
2324 *unique = true;
2325
2326 // translate everything to lower case before comparing
2327 wxChar chAccel = (wxChar)wxTolower(key);
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 )
2364 *unique = false;
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
2392 bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent& event)
2393 {
2394 size_t n = 0;
2395 for ( wxMenuList::compatibility_iterator node = m_menus.GetFirst();
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
2405 return true;
2406 }
2407 }
2408 }
2409
2410 // not found
2411 return false;
2412 }
2413
2414 #endif // wxUSE_ACCEL
2415
2416 // ----------------------------------------------------------------------------
2417 // wxMenuBar menus showing
2418 // ----------------------------------------------------------------------------
2419
2420 void wxMenuBar::PopupCurrentMenu(bool selectFirst)
2421 {
2422 wxCHECK_RET( m_current != -1, _T("no menu to popup") );
2423
2424 // forgot to call DismissMenu()?
2425 wxASSERT_MSG( !m_menuShown, _T("shouldn't show two menus at once!") );
2426
2427 // in any case, we should show it - even if we won't
2428 m_shouldShowMenu = true;
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);
2442
2443 m_menuShown->SetInvokingWindow(m_frameLast);
2444
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
2458 void 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
2466 void wxMenuBar::OnDismissMenu(bool dismissMenuBar)
2467 {
2468 m_shouldShowMenu = false;
2469 m_menuShown = NULL;
2470 if ( dismissMenuBar )
2471 {
2472 OnDismiss();
2473 }
2474 }
2475
2476 void wxMenuBar::OnDismiss()
2477 {
2478 if ( ReleaseMouseCapture() )
2479 wxLogTrace(_T("mousecapture"), _T("Releasing mouse from wxMenuBar::OnDismiss"));
2480
2481 if ( m_current != -1 )
2482 {
2483 size_t current = m_current;
2484 m_current = -1;
2485
2486 RefreshItem(current);
2487 }
2488
2489 GiveAwayFocus();
2490 }
2491
2492 bool wxMenuBar::ReleaseMouseCapture()
2493 {
2494 #if __WXX11__
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();
2513
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
2528 void wxMenuBar::GiveAwayFocus()
2529 {
2530 GetFrame()->SetFocus();
2531 }
2532
2533 // ----------------------------------------------------------------------------
2534 // popup menu support
2535 // ----------------------------------------------------------------------------
2536
2537 wxEventLoop *wxWindow::ms_evtLoopPopup = NULL;
2538
2539 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
2540 {
2541 wxCHECK_MSG( !ms_evtLoopPopup, false,
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
2558 wxTheApp->ProcessIdle();
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);
2568
2569 // wxLogDebug( "Name of invoking window %s", menu->GetInvokingWindow()->GetName().c_str() );
2570
2571 menu->Popup(ClientToScreen(wxPoint(x, y)), wxSize(0,0));
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
2591 PopEventHandler(true /* delete it */);
2592
2593 menu->SetInvokingWindow(NULL);
2594
2595 #ifdef __WXMSW__
2596 SetCursor(cursorOld);
2597 #endif // __WXMSW__
2598
2599 return true;
2600 }
2601
2602 void wxWindow::DismissPopupMenu()
2603 {
2604 wxCHECK_RET( ms_evtLoopPopup, _T("no popup menu shown") );
2605
2606 ms_evtLoopPopup->Exit();
2607 }
2608
2609 #endif // wxUSE_MENUS
2610