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