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