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