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