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