]> git.saurik.com Git - wxWidgets.git/blame - src/univ/menu.cpp
make it possible to use wxToolBarSimple as wxToolBar with wxUniv
[wxWidgets.git] / src / univ / menu.cpp
CommitLineData
1e6feb95
VZ
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$
442b35b5 8// Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
1e6feb95
VZ
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
a3870b2f
VZ
21 #pragma implementation "univmenuitem.h"
22 #pragma implementation "univmenu.h"
1e6feb95
VZ
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"
8cb172b4
JS
44#include "wx/dcclient.h"
45#include "wx/frame.h"
1e6feb95
VZ
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
57class WXDLLEXPORT wxMenuInfo
58{
59public:
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
96private:
97 void CalcWidth(wxMenuBar *menubar)
98 {
99 wxSize size;
100 wxClientDC dc(menubar);
101 dc.SetFont(wxSystemSettings::GetSystemFont(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
116WX_DEFINE_OBJARRAY(wxMenuInfoArray);
117
118// ----------------------------------------------------------------------------
119// wxPopupMenuWindow: a popup window showing a menu
120// ----------------------------------------------------------------------------
121
122class wxPopupMenuWindow : public wxPopupTransientWindow
123{
124public:
125 wxPopupMenuWindow(wxWindow *parent, wxMenu *menu);
126
127 // override the base class version to select the first item initially
128 virtual void Popup(wxWindow *focus = NULL);
129
130 // override the base class version to dismiss any open submenus
131 virtual void Dismiss();
132
133 // notify the menu when the window disappears from screen
134 virtual void OnDismiss();
135
136 // called when a submenu is dismissed
137 void OnSubmenuDismiss() { m_hasOpenSubMenu = FALSE; }
138
139 // get the currently selected item (may be NULL)
140 wxMenuItem *GetCurrentItem() const
141 {
142 return m_nodeCurrent ? m_nodeCurrent->GetData() : NULL;
143 }
144
145 // find the menu item at given position
146 wxMenuItemList::Node *GetMenuItemFromPoint(const wxPoint& pt) const;
147
148 // refresh the given item
149 void RefreshItem(wxMenuItem *item);
150
151 // preselect the first item
152 void SelectFirst() { SetCurrent(m_menu->GetMenuItems().GetFirst()); }
153
154 // process the key event, return TRUE if done
155 bool ProcessKeyDown(int key);
156
157 // process mouse move event
158 void ProcessMouseMove(const wxPoint& pt);
159
160 // don't dismiss the popup window if the parent menu was clicked
161 virtual bool ProcessLeftDown(wxMouseEvent& event);
162
163protected:
164 // how did we perform this operation?
165 enum InputMethod
166 {
167 WithKeyboard,
168 WithMouse
169 };
170
171 // draw the menu inside this window
172 virtual void DoDraw(wxControlRenderer *renderer);
173
174 // event handlers
175 void OnLeftUp(wxMouseEvent& event);
176 void OnMouseMove(wxMouseEvent& event);
177 void OnMouseLeave(wxMouseEvent& event);
178 void OnKeyDown(wxKeyEvent& event);
179
180 // reset the current item and node
181 void ResetCurrent();
182
183 // set the current node and item withotu refreshing anything
184 void SetCurrent(wxMenuItemList::Node *node);
185
186 // change the current item refreshing the old and new items
187 void ChangeCurrent(wxMenuItemList::Node *node);
188
189 // activate item, i.e. call either ClickItem() or OpenSubmenu() depending
190 // on what it is, return TRUE if something was done (i.e. it's not a
191 // separator...)
192 bool ActivateItem(wxMenuItem *item, InputMethod how = WithKeyboard);
193
194 // send the event about the item click
195 void ClickItem(wxMenuItem *item);
196
197 // show the submenu for this item
198 void OpenSubmenu(wxMenuItem *item, InputMethod how = WithKeyboard);
199
200 // can this tiem be opened?
201 bool CanOpen(wxMenuItem *item)
202 {
203 return item && item->IsEnabled() && item->IsSubMenu();
204 }
205
206 // dismiss the menu and all parent menus too
207 void DismissAndNotify();
208
209 // react to dimissing this menu and also dismiss the parent if
210 // dismissParent
211 void HandleDismiss(bool dismissParent);
212
213 // do we have an open submenu?
214 bool HasOpenSubmenu() const { return m_hasOpenSubMenu; }
215
216 // get previous node after the current one
217 wxMenuItemList::Node *GetPrevNode() const;
218
219 // get previous node before the given one, wrapping if it's the first one
220 wxMenuItemList::Node *GetPrevNode(wxMenuItemList::Node *node) const;
221
222 // get next node after the current one
223 wxMenuItemList::Node *GetNextNode() const;
224
225 // get next node after the given one, wrapping if it's the last one
226 wxMenuItemList::Node *GetNextNode(wxMenuItemList::Node *node) const;
227
228private:
229 // the menu we show
230 wxMenu *m_menu;
231
232 // the menu node corresponding to the current item
233 wxMenuItemList::Node *m_nodeCurrent;
234
235 // do we currently have an opened submenu?
236 bool m_hasOpenSubMenu;
237
238 DECLARE_EVENT_TABLE()
239};
240
241// ----------------------------------------------------------------------------
242// wxMenuKbdRedirector: an event handler which redirects kbd input to wxMenu
243// ----------------------------------------------------------------------------
244
245class wxMenuKbdRedirector : public wxEvtHandler
246{
247public:
248 wxMenuKbdRedirector(wxMenu *menu) { m_menu = menu; }
249
250 virtual bool ProcessEvent(wxEvent& event)
251 {
252 if ( event.GetEventType() == wxEVT_KEY_DOWN )
253 {
254 return m_menu->ProcessKeyDown(((wxKeyEvent &)event).GetKeyCode());
255 }
256 else
257 {
258 return wxEvtHandler::ProcessEvent(event);
259 }
260 }
261
262private:
263 wxMenu *m_menu;
264};
265
266// ----------------------------------------------------------------------------
267// wxWin macros
268// ----------------------------------------------------------------------------
269
270IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
271IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow)
272IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
273
274BEGIN_EVENT_TABLE(wxPopupMenuWindow, wxPopupTransientWindow)
275 EVT_KEY_DOWN(wxPopupMenuWindow::OnKeyDown)
276
277 EVT_LEFT_UP(wxPopupMenuWindow::OnLeftUp)
278 EVT_MOTION(wxPopupMenuWindow::OnMouseMove)
279 EVT_LEAVE_WINDOW(wxPopupMenuWindow::OnMouseLeave)
280END_EVENT_TABLE()
281
282BEGIN_EVENT_TABLE(wxMenuBar, wxMenuBarBase)
283 EVT_KILL_FOCUS(wxMenuBar::OnKillFocus)
284
285 EVT_KEY_DOWN(wxMenuBar::OnKeyDown)
286
287 EVT_LEFT_DOWN(wxMenuBar::OnLeftDown)
288 EVT_MOTION(wxMenuBar::OnMouseMove)
289END_EVENT_TABLE()
290
291// ============================================================================
292// implementation
293// ============================================================================
294
295// ----------------------------------------------------------------------------
296// wxPopupMenuWindow
297// ----------------------------------------------------------------------------
298
299wxPopupMenuWindow::wxPopupMenuWindow(wxWindow *parent, wxMenu *menu)
300{
301 m_menu = menu;
302 m_hasOpenSubMenu = FALSE;
303
304 ResetCurrent();
305
306 (void)Create(parent, wxBORDER_RAISED);
307
308 SetCursor(wxCURSOR_ARROW);
309}
310
311// ----------------------------------------------------------------------------
312// wxPopupMenuWindow current item/node handling
313// ----------------------------------------------------------------------------
314
315void wxPopupMenuWindow::ResetCurrent()
316{
317 SetCurrent(NULL);
318}
319
320void wxPopupMenuWindow::SetCurrent(wxMenuItemList::Node *node)
321{
322 m_nodeCurrent = node;
323}
324
325void wxPopupMenuWindow::ChangeCurrent(wxMenuItemList::Node *node)
326{
327 if ( node != m_nodeCurrent )
328 {
329 if ( m_nodeCurrent )
330 {
331 wxMenuItem *item = m_nodeCurrent->GetData();
332 wxCHECK_RET( item, _T("no current item?") );
333
334 // if it was the currently opened menu, close it
335 if ( item->IsSubMenu() && item->GetSubMenu()->IsShown() )
336 {
337 item->GetSubMenu()->Dismiss();
338 OnSubmenuDismiss();
339 }
340
341 RefreshItem(item);
342 }
343
344 m_nodeCurrent = node;
345
346 if ( m_nodeCurrent )
347 RefreshItem(m_nodeCurrent->GetData());
348 }
349}
350
351wxMenuItemList::Node *wxPopupMenuWindow::GetPrevNode() const
352{
353 wxMenuItemList::Node *node = m_nodeCurrent;
354 if ( !node )
355 {
356 // start from the end if no current item
357 node = m_menu->GetMenuItems().GetLast();
358 }
359
360 return GetPrevNode(node);
361}
362
363wxMenuItemList::Node *
364wxPopupMenuWindow::GetPrevNode(wxMenuItemList::Node *node) const
365{
366 if ( node )
367 {
368 node = node->GetPrevious();
369 if ( !node )
370 {
371 node = m_menu->GetMenuItems().GetLast();
372 }
373 }
374 //else: the menu is empty
375
376 return node;
377}
378
379wxMenuItemList::Node *wxPopupMenuWindow::GetNextNode() const
380{
381 wxMenuItemList::Node *node = m_nodeCurrent;
382 if ( !node )
383 {
384 // start from the beginning if no current item
385 node = m_menu->GetMenuItems().GetFirst();
386 }
387
388 return GetNextNode(node);
389}
390
391wxMenuItemList::Node *
392wxPopupMenuWindow::GetNextNode(wxMenuItemList::Node *node) const
393{
394 if ( node )
395 {
396 node = node->GetNext();
397 if ( !node )
398 {
399 node = m_menu->GetMenuItems().GetFirst();
400 }
401 }
402 //else: the menu is empty
403
404 return node;
405}
406
407// ----------------------------------------------------------------------------
408// wxPopupMenuWindow popup/dismiss
409// ----------------------------------------------------------------------------
410
411void wxPopupMenuWindow::Popup(wxWindow *focus)
412{
413 // check that the current item had been properly reset before
414 wxASSERT_MSG( !m_nodeCurrent ||
415 m_nodeCurrent == m_menu->GetMenuItems().GetFirst(),
416 _T("menu current item preselected incorrectly") );
417
418 wxPopupTransientWindow::Popup(focus);
419
420#ifdef __WXMSW__
421 // ensure that this window is really on top of everything: without using
422 // SetWindowPos() it can be covered by its parent menu which is not
423 // really what we want
424 wxMenu *menuParent = m_menu->GetParent();
425 if ( menuParent )
426 {
427 wxPopupMenuWindow *win = menuParent->m_popupMenu;
428
429 // if we're shown, the parent menu must be also shown
430 wxCHECK_RET( win, _T("parent menu is not shown?") );
431
432 if ( !::SetWindowPos(GetHwndOf(win), GetHwnd(),
433 0, 0, 0, 0,
434 SWP_NOMOVE | SWP_NOSIZE | SWP_NOREDRAW) )
435 {
436 wxLogLastError(_T("SetWindowPos(HWND_TOP)"));
437 }
438
439 Refresh();
440 }
441#endif // __WXMSW__
442}
443
444void wxPopupMenuWindow::Dismiss()
445{
446 if ( HasOpenSubmenu() )
447 {
448 wxMenuItem *item = GetCurrentItem();
449 wxCHECK_RET( item && item->IsSubMenu(), _T("where is our open submenu?") );
450
451 wxPopupMenuWindow *win = item->GetSubMenu()->m_popupMenu;
452 wxCHECK_RET( win, _T("opened submenu is not opened?") );
453
454 win->Dismiss();
455 OnSubmenuDismiss();
456 }
457
458 wxPopupTransientWindow::Dismiss();
459}
460
461void wxPopupMenuWindow::OnDismiss()
462{
463 // when we are dismissed because the user clicked elsewhere or we lost
464 // focus in any other way, hide the parent menu as well
465 HandleDismiss(TRUE);
466}
467
468void wxPopupMenuWindow::HandleDismiss(bool dismissParent)
469{
470 ResetCurrent();
471
472 m_menu->OnDismiss(dismissParent);
473}
474
475void wxPopupMenuWindow::DismissAndNotify()
476{
477 Dismiss();
478 HandleDismiss(TRUE);
479}
480
481// ----------------------------------------------------------------------------
482// wxPopupMenuWindow geometry
483// ----------------------------------------------------------------------------
484
485wxMenuItemList::Node *
486wxPopupMenuWindow::GetMenuItemFromPoint(const wxPoint& pt) const
487{
488 // we only use the y coord normally, but still check x in case the point is
489 // outside the window completely
490 if ( wxWindow::HitTest(pt) == wxHT_WINDOW_INSIDE )
491 {
492 wxCoord y = 0;
493 for ( wxMenuItemList::Node *node = m_menu->GetMenuItems().GetFirst();
494 node;
495 node = node->GetNext() )
496 {
497 wxMenuItem *item = node->GetData();
498 y += item->GetHeight();
499 if ( y > pt.y )
500 {
501 // found
502 return node;
503 }
504 }
505 }
506
507 return NULL;
508}
509
510// ----------------------------------------------------------------------------
511// wxPopupMenuWindow drawing
512// ----------------------------------------------------------------------------
513
514void wxPopupMenuWindow::RefreshItem(wxMenuItem *item)
515{
516 wxCHECK_RET( item, _T("can't refresh NULL item") );
517
518 wxASSERT_MSG( IsShown(), _T("can't refresh menu which is not shown") );
519
520 // FIXME: -1 here because of SetLogicalOrigin(1, 1) in DoDraw()
521 RefreshRect(wxRect(0, item->GetPosition() - 1,
522 m_menu->GetGeometryInfo().GetSize().x, item->GetHeight()));
523}
524
525void wxPopupMenuWindow::DoDraw(wxControlRenderer *renderer)
526{
527 // no clipping so far - do we need it? I don't think so as the menu is
528 // never partially covered as it is always on top of everything
529
530 wxDC& dc = renderer->GetDC();
531 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
532
533 // FIXME: this should be done in the renderer, however when it is fixed
534 // wxPopupMenuWindow::RefreshItem() should be changed too!
535 dc.SetLogicalOrigin(1, 1);
536
537 wxRenderer *rend = renderer->GetRenderer();
538
539 wxCoord y = 0;
540 const wxMenuGeometryInfo& gi = m_menu->GetGeometryInfo();
541 for ( wxMenuItemList::Node *node = m_menu->GetMenuItems().GetFirst();
542 node;
543 node = node->GetNext() )
544 {
545 wxMenuItem *item = node->GetData();
546
547 if ( item->IsSeparator() )
548 {
549 rend->DrawMenuSeparator(dc, y, gi);
550 }
551 else // not a separator
552 {
553 int flags = 0;
554 if ( item->IsCheckable() )
555 {
556 flags |= wxCONTROL_CHECKABLE;
557
558 if ( item->IsChecked() )
559 {
560 flags |= wxCONTROL_CHECKED;
561 }
562 }
563
564 if ( !item->IsEnabled() )
565 flags |= wxCONTROL_DISABLED;
566
567 if ( item->IsSubMenu() )
568 flags |= wxCONTROL_ISSUBMENU;
569
570 if ( item == GetCurrentItem() )
571 flags |= wxCONTROL_SELECTED;
572
573 rend->DrawMenuItem
574 (
575 dc,
576 y,
577 gi,
578 item->GetLabel(),
579 item->GetAccelString(),
580 // strangely enough, for unchecked item we use the
581 // "checked" bitmap because this is the default one - this
582 // explains this strange boolean expression
583 item->GetBitmap(!item->IsCheckable() || item->IsChecked()),
584 flags,
585 item->GetAccelIndex()
586 );
587 }
588
589 y += item->GetHeight();
590 }
591}
592
593// ----------------------------------------------------------------------------
594// wxPopupMenuWindow actions
595// ----------------------------------------------------------------------------
596
597void wxPopupMenuWindow::ClickItem(wxMenuItem *item)
598{
599 wxCHECK_RET( item, _T("can't click NULL item") );
600
601 wxASSERT_MSG( !item->IsSeparator() && !item->IsSubMenu(),
602 _T("can't click this item") );
603
604 m_menu->ClickItem(item);
605
606 // close all menus
607 DismissAndNotify();
608}
609
610void 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
625bool 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
655bool 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
685void wxPopupMenuWindow::OnLeftUp(wxMouseEvent& event)
686{
687 wxMenuItemList::Node *node = GetMenuItemFromPoint(event.GetPosition());
688 if ( node )
689 {
690 ActivateItem(node->GetData(), WithMouse);
691 }
692}
693
694void 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
719void 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
780void 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
819void wxPopupMenuWindow::OnKeyDown(wxKeyEvent& event)
820{
821 if ( !ProcessKeyDown(event.GetKeyCode()) )
822 {
823 event.Skip();
824 }
825}
826
827bool 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
1008void wxMenu::Init()
1009{
1010 m_geometry = NULL;
1011
1012 m_popupMenu = NULL;
1013}
1014
1015wxMenu::~wxMenu()
1016{
1017 delete m_geometry;
1018 delete m_popupMenu;
1019}
1020
1021// ----------------------------------------------------------------------------
1022// wxMenu and wxMenuGeometryInfo
1023// ----------------------------------------------------------------------------
1024
1025wxMenuGeometryInfo::~wxMenuGeometryInfo()
1026{
1027}
1028
1029const 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
1047void 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
1060void 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
1076bool wxMenu::DoAppend(wxMenuItem *item)
1077{
1078 if ( !wxMenuBase::DoAppend(item) )
1079 return FALSE;
1080
1081 OnItemAdded(item);
1082
1083 return TRUE;
1084}
1085
1086bool 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
1096wxMenuItem *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
1116void 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
1133void wxMenu::Detach()
1134{
1135 wxMenuBase::Detach();
1136}
1137
1138// ----------------------------------------------------------------------------
1139// wxMenu misc functions
1140// ----------------------------------------------------------------------------
1141
1142wxWindow *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 win = menu->GetInvokingWindow();
1164 if ( win )
1165 break;
1166
1167 menu = menu->GetParent();
1168 }
1169
1170 // we're probably going to crash in the caller anyhow, but try to detect
1171 // this error as soon as possible
1172 wxASSERT_MSG( win, _T("menu without any associated window?") );
1173
1174 // also remember it in this menu so that we don't have to search for it the
1175 // next time
1176 wxConstCast(this, wxMenu)->m_invokingWindow = win;
1177
1178 return win;
1179}
1180
1181wxRenderer *wxMenu::GetRenderer() const
1182{
1183 // we're going to crash without renderer!
1184 wxCHECK_MSG( m_popupMenu, NULL, _T("neither popup nor menubar menu?") );
1185
1186 return m_popupMenu->GetRenderer();
1187}
1188
1189void wxMenu::RefreshItem(wxMenuItem *item)
1190{
1191 // the item geometry changed, so our might have changed as well
1192 InvalidateGeometryInfo();
1193
1194 if ( IsShown() )
1195 {
1196 // this would be a bug in IsShown()
1197 wxCHECK_RET( m_popupMenu, _T("must have popup window if shown!") );
1198
1199 // recalc geometry to update the item height and such
1200 (void)GetGeometryInfo();
1201
1202 m_popupMenu->RefreshItem(item);
1203 }
1204}
1205
1206// ----------------------------------------------------------------------------
1207// wxMenu showing and hiding
1208// ----------------------------------------------------------------------------
1209
1210bool wxMenu::IsShown() const
1211{
1212 return m_popupMenu && m_popupMenu->IsShown();
1213}
1214
1215void wxMenu::OnDismiss(bool dismissParent)
1216{
1217 if ( m_menuParent )
1218 {
1219 // always notify the parent about submenu disappearance
1220 wxPopupMenuWindow *win = m_menuParent->m_popupMenu;
1221 if ( win )
1222 {
1223 win->OnSubmenuDismiss();
1224 }
1225 else
1226 {
1227 wxFAIL_MSG( _T("parent menu not shown?") );
1228 }
1229
1230 // and if we dismiss everything, propagate to parent
1231 if ( dismissParent )
1232 {
1233 // dismissParent is recursive
1234 m_menuParent->Dismiss();
1235 m_menuParent->OnDismiss(TRUE);
1236 }
1237 }
1238 else // no parent menu
1239 {
1240 // notify the menu bar if we're a top level menu
1241 if ( m_menuBar )
1242 {
1243 m_menuBar->OnDismissMenu(dismissParent);
1244 }
1245 else // popup menu
1246 {
1247 wxCHECK_RET( m_invokingWindow, _T("what kind of menu is this?") );
1248
1249 m_invokingWindow->DismissPopupMenu();
1250 SetInvokingWindow(NULL);
1251 }
1252 }
1253}
1254
1255void wxMenu::Popup(const wxPoint& pos, const wxSize& size, bool selectFirst)
1256{
1257 // create the popup window if not done yet
1258 if ( !m_popupMenu )
1259 {
1260 m_popupMenu = new wxPopupMenuWindow(GetRootWindow(), this);
1261 }
1262
1263 // select the first item unless disabled
1264 if ( selectFirst )
1265 {
1266 m_popupMenu->SelectFirst();
1267 }
1268
1269 // the geometry might have changed since the last time we were shown, so
1270 // always resize
1271 m_popupMenu->SetClientSize(GetGeometryInfo().GetSize());
1272
1273 // position it as specified
1274 m_popupMenu->Position(pos, size);
1275
1276 // the menu can't have the focus itself (it is a Windows limitation), so
1277 // always keep the focus at the originating window
1278 wxWindow *focus = GetRootWindow();
1279
1280 wxASSERT_MSG( focus, _T("no window to keep focus on?") );
1281
1282 // and show it
1283 m_popupMenu->Popup(focus);
1284}
1285
1286void wxMenu::Dismiss()
1287{
1288 wxCHECK_RET( IsShown(), _T("can't dismiss hidden menu") );
1289
1290 m_popupMenu->Dismiss();
1291}
1292
1293// ----------------------------------------------------------------------------
1294// wxMenu event processing
1295// ----------------------------------------------------------------------------
1296
1297bool wxMenu::ProcessKeyDown(int key)
1298{
1299 wxCHECK_MSG( m_popupMenu, FALSE,
1300 _T("can't process key events if not shown") );
1301
1302 return m_popupMenu->ProcessKeyDown(key);
1303}
1304
1305bool wxMenu::ClickItem(wxMenuItem *item)
1306{
1307 int isChecked;
1308 if ( item->IsCheckable() )
1309 {
1310 // update the item state
1311 isChecked = !item->IsChecked();
1312
1313 item->Check(isChecked != 0);
1314 }
1315 else
1316 {
1317 // not applicabled
1318 isChecked = -1;
1319 }
1320
1321 return SendEvent(item->GetId(), isChecked);
1322}
1323
1324// ----------------------------------------------------------------------------
1325// wxMenu accel support
1326// ----------------------------------------------------------------------------
1327
1328#if wxUSE_ACCEL
1329
1330bool wxMenu::ProcessAccelEvent(const wxKeyEvent& event)
1331{
1332 // do we have an item for this accel?
1333 wxMenuItem *item = m_accelTable.GetMenuItem(event);
1334 if ( item && item->IsEnabled() )
1335 {
1336 return ClickItem(item);
1337 }
1338
1339 // try our submenus
1340 for ( wxMenuItemList::Node *node = GetMenuItems().GetFirst();
1341 node;
1342 node = node->GetNext() )
1343 {
1344 const wxMenuItem *item = node->GetData();
1345 if ( item->IsSubMenu() && item->IsEnabled() )
1346 {
1347 // try its elements
1348 if ( item->GetSubMenu()->ProcessAccelEvent(event) )
1349 {
1350 return TRUE;
1351 }
1352 }
1353 }
1354
1355 return FALSE;
1356}
1357
1358void wxMenu::AddAccelFor(wxMenuItem *item)
1359{
1360 wxAcceleratorEntry *accel = item->GetAccel();
1361 if ( accel )
1362 {
1363 accel->SetMenuItem(item);
1364
1365 m_accelTable.Add(*accel);
1366
1367 delete accel;
1368 }
1369}
1370
1371void wxMenu::RemoveAccelFor(wxMenuItem *item)
1372{
1373 wxAcceleratorEntry *accel = item->GetAccel();
1374 if ( accel )
1375 {
1376 m_accelTable.Remove(*accel);
1377
1378 delete accel;
1379 }
1380}
1381
1382#endif // wxUSE_ACCEL
1383
1384// ----------------------------------------------------------------------------
1385// wxMenuItem construction
1386// ----------------------------------------------------------------------------
1387
1388wxMenuItem::wxMenuItem(wxMenu *parentMenu,
1389 int id,
1390 const wxString& text,
1391 const wxString& help,
1392 bool isCheckable,
1393 wxMenu *subMenu)
1394{
1395 m_id = id;
1396 m_parentMenu = parentMenu;
1397 m_subMenu = subMenu;
1398
1399 m_text = text;
1400 m_help = help;
1401
1402 m_isCheckable = isCheckable;
1403 m_isEnabled = TRUE;
1404 m_isChecked = FALSE;
1405
1406 m_posY =
1407 m_height = -1;
1408
1409 UpdateAccelInfo();
1410}
1411
1412wxMenuItem::~wxMenuItem()
1413{
1414}
1415
1416// ----------------------------------------------------------------------------
1417// wxMenuItemBase methods implemented here
1418// ----------------------------------------------------------------------------
1419
1420/* static */
1421wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
1422 int id,
1423 const wxString& name,
1424 const wxString& help,
1425 bool isCheckable,
1426 wxMenu *subMenu)
1427{
1428 return new wxMenuItem(parentMenu, id, name, help, isCheckable, subMenu);
1429}
1430
1431/* static */
1432wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
1433{
1434 return wxStripMenuCodes(text);
1435}
1436
1437// ----------------------------------------------------------------------------
1438// wxMenuItem operations
1439// ----------------------------------------------------------------------------
1440
1441void wxMenuItem::NotifyMenu()
1442{
1443 m_parentMenu->RefreshItem(this);
1444}
1445
1446void wxMenuItem::UpdateAccelInfo()
1447{
1448 m_indexAccel = wxControl::FindAccelIndex(m_text);
1449
1450 // will be empty if the text contains no TABs - ok
1451 m_strAccel = m_text.AfterFirst(_T('\t'));
1452}
1453
1454void wxMenuItem::SetText(const wxString& text)
1455{
1456 if ( text != m_text )
1457 {
1458 // first call the base class version to change m_text
1459 wxMenuItemBase::SetText(text);
1460
1461 UpdateAccelInfo();
1462
1463 NotifyMenu();
1464 }
1465}
1466
1467void wxMenuItem::SetCheckable(bool checkable)
1468{
1469 if ( checkable != m_isCheckable )
1470 {
1471 wxMenuItemBase::SetCheckable(checkable);
1472
1473 NotifyMenu();
1474 }
1475}
1476
1477void wxMenuItem::SetBitmaps(const wxBitmap& bmpChecked,
1478 const wxBitmap& bmpUnchecked)
1479{
1480 m_bmpChecked = bmpChecked;
1481 m_bmpUnchecked = bmpUnchecked;
1482
1483 NotifyMenu();
1484}
1485
1486void wxMenuItem::Enable(bool enable)
1487{
1488 if ( enable != m_isEnabled )
1489 {
1490 wxMenuItemBase::Enable(enable);
1491
1492 NotifyMenu();
1493 }
1494}
1495
1496void wxMenuItem::Check(bool check)
1497{
1498 if ( check != m_isChecked )
1499 {
1500 wxMenuItemBase::Check(check);
1501
1502 NotifyMenu();
1503 }
1504}
1505
1506// ----------------------------------------------------------------------------
1507// wxMenuBar creation
1508// ----------------------------------------------------------------------------
1509
1510void wxMenuBar::Init()
1511{
1512 m_frameLast = NULL;
1513
1514 m_current = -1;
1515
1516 m_menuShown = NULL;
1517
1518 m_shouldShowMenu = FALSE;
1519}
1520
1521void wxMenuBar::Attach(wxFrame *frame)
1522{
1523 // maybe you really wanted to call Detach()?
1524 wxCHECK_RET( frame, _T("wxMenuBar::Attach(NULL) called") );
1525
1526 wxMenuBarBase::Attach(frame);
1527
1528 if ( IsCreated() )
1529 {
1530 // reparent if necessary
1531 if ( m_frameLast != frame )
1532 {
1533 Reparent(frame);
1534 }
1535
1536 // show it back - was hidden by Detach()
1537 Show();
1538 }
1539 else // not created yet, do it now
1540 {
1541 // we have no way to return the error from here anyhow :-(
1542 (void)Create(frame, -1);
1543
1544 SetCursor(wxCURSOR_ARROW);
1545
1546 SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT));
1547 }
1548
1549 // remember the last frame which had us to avoid unnecessarily reparenting
1550 // above
1551 m_frameLast = frame;
1552}
1553
1554void wxMenuBar::Detach()
1555{
1556 // don't delete the window because we may be reattached later, just hide it
1557 if ( m_frameLast )
1558 {
1559 Hide();
1560 }
1561
1562 wxMenuBarBase::Detach();
1563}
1564
1565wxMenuBar::~wxMenuBar()
1566{
1567}
1568
1569// ----------------------------------------------------------------------------
1570// wxMenuBar adding/removing items
1571// ----------------------------------------------------------------------------
1572
1573bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
1574{
1575 return Insert(GetCount(), menu, title);
1576}
1577
1578bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
1579{
1580 if ( !wxMenuBarBase::Insert(pos, menu, title) )
1581 return FALSE;
1582
1583 wxMenuInfo *info = new wxMenuInfo(title);
1584 m_menuInfos.Insert(info, pos);
1585
1586 RefreshAllItemsAfter(pos);
1587
1588 return TRUE;
1589}
1590
1591wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
1592{
1593 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
1594
1595 if ( menuOld )
1596 {
1597 wxMenuInfo& info = m_menuInfos[pos];
1598
1599 info.SetLabel(title);
1600
1601 // even if the old menu was disabled, the new one is not any more
1602 info.SetEnabled();
1603
1604 // even if we change only this one, the new label has different width,
1605 // so we need to refresh everything beyond this item as well
1606 RefreshAllItemsAfter(pos);
1607 }
1608
1609 return menuOld;
1610}
1611
1612wxMenu *wxMenuBar::Remove(size_t pos)
1613{
1614 wxMenu *menuOld = wxMenuBarBase::Remove(pos);
1615
1616 if ( menuOld )
1617 {
1618 m_menuInfos.RemoveAt(pos);
1619
1620 // this doesn't happen too often, so don't try to be too smart - just
1621 // refresh everything
1622 Refresh();
1623 }
1624
1625 return menuOld;
1626}
1627
1628// ----------------------------------------------------------------------------
1629// wxMenuBar top level menus access
1630// ----------------------------------------------------------------------------
1631
1632wxCoord wxMenuBar::GetItemWidth(size_t pos) const
1633{
1634 return m_menuInfos[pos].GetWidth(wxConstCast(this, wxMenuBar));
1635}
1636
1637void wxMenuBar::EnableTop(size_t pos, bool enable)
1638{
1639 wxCHECK_RET( pos < GetCount(), _T("invalid index in EnableTop") );
1640
1641 if ( enable != m_menuInfos[pos].IsEnabled() )
1642 {
1643 m_menuInfos[pos].SetEnabled(enable);
1644
1645 RefreshItem(pos);
1646 }
1647 //else: nothing to do
1648}
1649
1650bool wxMenuBar::IsEnabledTop(size_t pos) const
1651{
1652 wxCHECK_MSG( pos < GetCount(), FALSE, _T("invalid index in IsEnabledTop") );
1653
1654 return m_menuInfos[pos].IsEnabled();
1655}
1656
1657void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
1658{
1659 wxCHECK_RET( pos < GetCount(), _T("invalid index in EnableTop") );
1660
1661 if ( label != m_menuInfos[pos].GetLabel() )
1662 {
1663 m_menuInfos[pos].SetLabel(label);
1664
1665 RefreshItem(pos);
1666 }
1667 //else: nothing to do
1668}
1669
1670wxString wxMenuBar::GetLabelTop(size_t pos) const
1671{
1672 wxCHECK_MSG( pos < GetCount(), _T(""), _T("invalid index in GetLabelTop") );
1673
1674 return m_menuInfos[pos].GetLabel();
1675}
1676
1677// ----------------------------------------------------------------------------
1678// wxMenuBar drawing
1679// ----------------------------------------------------------------------------
1680
1681void wxMenuBar::RefreshAllItemsAfter(size_t pos)
1682{
1683 wxRect rect = GetItemRect(pos);
1684 rect.width = GetClientSize().x - rect.x;
1685 RefreshRect(rect);
1686}
1687
1688void wxMenuBar::RefreshItem(size_t pos)
1689{
1690 wxCHECK_RET( pos != (size_t)-1,
1691 _T("invalid item in wxMenuBar::RefreshItem") );
1692
1693 RefreshRect(GetItemRect(pos));
1694}
1695
1696void wxMenuBar::DoDraw(wxControlRenderer *renderer)
1697{
1698 wxDC& dc = renderer->GetDC();
1699 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
1700
1701 // redraw only the items which must be redrawn
1702
1703 // we don't have to use GetUpdateClientRect() here because our client rect
1704 // is the same as total one
1705 wxRect rectUpdate = GetUpdateRegion().GetBox();
1706
1707 int flagsMenubar = GetStateFlags();
1708
1709 wxRect rect;
1710 rect.y = 0;
1711 rect.height = GetClientSize().y;
1712
1713 wxCoord x = 0;
1714 size_t count = GetCount();
1715 for ( size_t n = 0; n < count; n++ )
1716 {
1717 if ( x > rectUpdate.GetRight() )
1718 {
1719 // all remaining items are to the right of rectUpdate
1720 break;
1721 }
1722
1723 rect.x = x;
1724 rect.width = GetItemWidth(n);
1725 x += rect.width;
1726 if ( x < rectUpdate.x )
1727 {
1728 // this item is still to the left of rectUpdate
1729 continue;
1730 }
1731
1732 int flags = flagsMenubar;
1733 if ( m_current != -1 && n == (size_t)m_current )
1734 {
1735 flags |= wxCONTROL_SELECTED;
1736 }
1737
1738 if ( !IsEnabledTop(n) )
1739 {
1740 flags |= wxCONTROL_DISABLED;
1741 }
1742
1743 GetRenderer()->DrawMenuBarItem
1744 (
1745 dc,
1746 rect,
1747 m_menuInfos[n].GetLabel(),
1748 flags,
1749 m_menuInfos[n].GetAccelIndex()
1750 );
1751 }
1752}
1753
1754// ----------------------------------------------------------------------------
1755// wxMenuBar geometry
1756// ----------------------------------------------------------------------------
1757
1758wxRect wxMenuBar::GetItemRect(size_t pos) const
1759{
1760 wxASSERT_MSG( pos < GetCount(), _T("invalid menu bar item index") );
1761
1762 wxRect rect;
1763 rect.x =
1764 rect.y = 0;
1765 rect.height = GetClientSize().y;
1766
1767 for ( size_t n = 0; n < pos; n++ )
1768 {
1769 rect.x += GetItemWidth(n);
1770 }
1771
1772 rect.width = GetItemWidth(pos);
1773
1774 return rect;
1775}
1776
1777wxSize wxMenuBar::DoGetBestClientSize() const
1778{
1779 wxSize size;
1780 if ( GetMenuCount() > 0 )
1781 {
1782 wxClientDC dc(wxConstCast(this, wxMenuBar));
1783 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
1784 dc.GetTextExtent(GetLabelTop(0), &size.x, &size.y);
1785
1786 // adjust for the renderer we use
1787 size = GetRenderer()->GetMenuBarItemSize(size);
1788 }
1789 else // empty menubar
1790 {
1791 size.x =
1792 size.y = 0;
1793 }
1794
1795 // the width is arbitrary, of course, for horizontal menubar
1796 size.x = 100;
1797
1798 return size;
1799}
1800
1801int wxMenuBar::GetMenuFromPoint(const wxPoint& pos) const
1802{
1803 if ( pos.x < 0 || pos.y < 0 || pos.y > GetClientSize().y )
1804 return -1;
1805
1806 // do find it
1807 wxCoord x = 0;
1808 size_t count = GetCount();
1809 for ( size_t item = 0; item < count; item++ )
1810 {
1811 x += GetItemWidth(item);
1812
1813 if ( x > pos.x )
1814 {
1815 return item;
1816 }
1817 }
1818
1819 // to the right of the last menu item
1820 return -1;
1821}
1822
1823// ----------------------------------------------------------------------------
1824// wxMenuBar menu operations
1825// ----------------------------------------------------------------------------
1826
1827void wxMenuBar::SelectMenu(size_t pos)
1828{
1829 SetFocus();
1830 CaptureMouse();
1831
1832 DoSelectMenu(pos);
1833}
1834
1835void wxMenuBar::DoSelectMenu(size_t pos)
1836{
1837 wxCHECK_RET( pos < GetCount(), _T("invalid menu index in DoSelectMenu") );
1838
1839 if ( m_current != -1 )
1840 {
1841 // close the previous menu
1842 if ( IsShowingMenu() )
1843 {
1844 // restore m_shouldShowMenu flag after DismissMenu() which resets
1845 // it to FALSE
1846 bool old = m_shouldShowMenu;
1847
1848 DismissMenu();
1849
1850 m_shouldShowMenu = old;
1851 }
1852
1853 RefreshItem((size_t)m_current);
1854 }
1855
1856 m_current = pos;
1857
1858 RefreshItem(pos);
1859}
1860
1861void wxMenuBar::PopupMenu(size_t pos)
1862{
1863 wxCHECK_RET( pos < GetCount(), _T("invalid menu index in PopupCurrentMenu") );
1864
1865 SetFocus();
1866 DoSelectMenu(pos);
1867 PopupCurrentMenu();
1868}
1869
1870// ----------------------------------------------------------------------------
1871// wxMenuBar input handing
1872// ----------------------------------------------------------------------------
1873
1874/*
1875 Note that wxMenuBar doesn't use wxInputHandler but handles keyboard and
1876 mouse in the same way under all platforms. This is because it doesn't derive
1877 from wxControl (which works with input handlers) but directly from wxWindow.
1878
1879 Also, menu bar input handling is rather simple, so maybe it's not really
1880 worth making it themeable - at least I've decided against doing it now as it
1881 would merging the changes back into trunk more difficult. But it still could
1882 be done later if really needed.
1883 */
1884
1885void wxMenuBar::OnKillFocus(wxFocusEvent& event)
1886{
1887 if ( m_current != -1 )
1888 {
1889 RefreshItem((size_t)m_current);
1890
1891 m_current = -1;
1892 }
1893
1894 event.Skip();
1895}
1896
1897void wxMenuBar::OnLeftDown(wxMouseEvent& event)
1898{
1899 if ( HasCapture() )
1900 {
1901 OnDismiss();
1902
1903 event.Skip();
1904 }
1905 else // we didn't have mouse capture, capture it now
1906 {
1907 m_current = GetMenuFromPoint(event.GetPosition());
1908 if ( m_current == -1 )
1909 {
1910 // unfortunately, we can't prevent wxMSW from giving us the focus,
1911 // so we can only give it back
1912 GiveAwayFocus();
1913 }
1914 else // on item
1915 {
1916 CaptureMouse();
1917
1918 // show it as selected
1919 RefreshItem((size_t)m_current);
1920
1921 // show the menu
1922 PopupCurrentMenu(FALSE /* don't select first item - as Windows does */);
1923 }
1924 }
1925}
1926
1927void wxMenuBar::OnMouseMove(wxMouseEvent& event)
1928{
1929 if ( HasCapture() )
1930 {
1931 (void)ProcessMouseEvent(event.GetPosition());
1932 }
1933 else
1934 {
1935 event.Skip();
1936 }
1937}
1938
1939bool wxMenuBar::ProcessMouseEvent(const wxPoint& pt)
1940{
1941 // a hack to ignore the extra mouse events MSW sends us: this is similar to
1942 // wxUSE_MOUSEEVENT_HACK in wxWin itself but it isn't enough for us here as
1943 // we get the messages from different windows (old and new popup menus for
1944 // example)
1945#ifdef __WXMSW__
1946 static wxPoint s_ptLast;
1947 if ( pt == s_ptLast )
1948 {
1949 return FALSE;
1950 }
1951
1952 s_ptLast = pt;
1953#endif // __WXMSW__
1954
1955 int currentNew = GetMenuFromPoint(pt);
1956 if ( (currentNew == -1) || (currentNew == m_current) )
1957 {
1958 return FALSE;
1959 }
1960
1961 // select the new active item
1962 DoSelectMenu(currentNew);
1963
1964 // show the menu if we know that we should, even if we hadn't been showing
1965 // it before (this may happen if the previous menu was disabled)
1966 if ( m_shouldShowMenu )
1967 {
1968 // open the new menu if the old one we closed had been opened
1969 PopupCurrentMenu(FALSE /* don't select first item - as Windows does */);
1970 }
1971
1972 return TRUE;
1973}
1974
1975void wxMenuBar::OnKeyDown(wxKeyEvent& event)
1976{
1977 // the current item must have been set before
1978 wxCHECK_RET( m_current != -1, _T("where is current item?") );
1979
1980 int key = event.GetKeyCode();
1981
1982 // first let the menu have it
1983 if ( IsShowingMenu() && m_menuShown->ProcessKeyDown(key) )
1984 {
1985 return;
1986 }
1987
1988 // cycle through the menu items when left/right arrows are pressed and open
1989 // the menu when up/down one is
1990 switch ( key )
1991 {
1992 case WXK_MENU:
1993 // Alt must be processed at wxWindow level too
1994 event.Skip();
1995 // fall through
1996
1997 case WXK_ESCAPE:
1998 // remove the selection and give the focus away
1999 if ( m_current != -1 )
2000 {
2001 if ( IsShowingMenu() )
2002 {
2003 DismissMenu();
2004 }
2005
2006 OnDismiss();
2007 }
2008 break;
2009
2010 case WXK_LEFT:
2011 case WXK_RIGHT:
2012 {
2013 size_t count = GetCount();
2014 if ( count == 1 )
2015 {
2016 // the item won't change anyhow
2017 break;
2018 }
2019 //else: otherwise, it will
2020
2021 // remember if we were showing a menu - if we did, we should
2022 // show the new menu after changing the item
2023 bool wasMenuOpened = IsShowingMenu();
2024 if ( wasMenuOpened )
2025 {
2026 DismissMenu();
2027 }
2028
2029 // cast is safe as we tested for -1 above
2030 size_t currentNew = (size_t)m_current;
2031
2032 if ( key == WXK_LEFT )
2033 {
2034 if ( currentNew-- == 0 )
2035 currentNew = count - 1;
2036 }
2037 else // right
2038 {
2039 if ( ++currentNew == (int)count )
2040 currentNew = 0;
2041 }
2042
2043 DoSelectMenu(currentNew);
2044
2045 if ( wasMenuOpened )
2046 {
2047 PopupCurrentMenu();
2048 }
2049 }
2050 break;
2051
2052 case WXK_DOWN:
2053 case WXK_UP:
2054 case WXK_RETURN:
2055 // open the menu
2056 PopupCurrentMenu();
2057 break;
2058
2059 default:
2060 // letters open the corresponding menu
2061 {
2062 bool unique;
2063 int idxFound = FindNextItemForAccel(m_current, key, &unique);
2064
2065 if ( idxFound != -1 )
2066 {
2067 if ( IsShowingMenu() )
2068 {
2069 DismissMenu();
2070 }
2071
2072 DoSelectMenu((size_t)idxFound);
2073
2074 // if the item is not unique, just select it but don't
2075 // activate as the user might have wanted to activate
2076 // another item
2077 //
2078 // also, don't try to open a disabled menu
2079 if ( unique && IsEnabledTop((size_t)idxFound) )
2080 {
2081 // open the menu
2082 PopupCurrentMenu();
2083 }
2084
2085 // skip the "event.Skip()" below
2086 break;
2087 }
2088 }
2089
2090 event.Skip();
2091 }
2092}
2093
2094// ----------------------------------------------------------------------------
2095// wxMenuBar accel handling
2096// ----------------------------------------------------------------------------
2097
2098int wxMenuBar::FindNextItemForAccel(int idxStart, int key, bool *unique) const
2099{
2100 if ( !wxIsalnum(key) )
2101 {
2102 // we only support letters/digits as accels
2103 return -1;
2104 }
2105
2106 // do we have more than one item with this accel?
2107 if ( unique )
2108 *unique = TRUE;
2109
2110 // translate everything to lower case before comparing
2111 wxChar chAccel = wxTolower(key);
2112
2113 // the index of the item with this accel
2114 int idxFound = -1;
2115
2116 // loop through all items searching for the item with this
2117 // accel starting at the item after the current one
2118 int count = GetCount();
2119 int n = idxStart == -1 ? 0 : idxStart + 1;
2120
2121 if ( n == count )
2122 {
2123 // wrap
2124 n = 0;
2125 }
2126
2127 idxStart = n;
2128 for ( ;; )
2129 {
2130 const wxMenuInfo& info = m_menuInfos[n];
2131
2132 int idxAccel = info.GetAccelIndex();
2133 if ( idxAccel != -1 &&
2134 wxTolower(info.GetLabel()[(size_t)idxAccel])
2135 == chAccel )
2136 {
2137 // ok, found an item with this accel
2138 if ( idxFound == -1 )
2139 {
2140 // store it but continue searching as we need to
2141 // know if it's the only item with this accel or if
2142 // there are more
2143 idxFound = n;
2144 }
2145 else // we already had found such item
2146 {
2147 if ( unique )
2148 *unique = FALSE;
2149
2150 // no need to continue further, we won't find
2151 // anything we don't already know
2152 break;
2153 }
2154 }
2155
2156 // we want to iterate over all items wrapping around if
2157 // necessary
2158 if ( ++n == count )
2159 {
2160 // wrap
2161 n = 0;
2162 }
2163
2164 if ( n == idxStart )
2165 {
2166 // we've seen all items
2167 break;
2168 }
2169 }
2170
2171 return idxFound;
2172}
2173
2174#if wxUSE_ACCEL
2175
2176bool wxMenuBar::ProcessAccelEvent(const wxKeyEvent& event)
2177{
2178 size_t n = 0;
2179 for ( wxMenuList::Node *node = m_menus.GetFirst();
2180 node;
2181 node = node->GetNext(), n++ )
2182 {
2183 // accels of the items in the disabled menus shouldn't work
2184 if ( m_menuInfos[n].IsEnabled() )
2185 {
2186 if ( node->GetData()->ProcessAccelEvent(event) )
2187 {
2188 // menu processed it
2189 return TRUE;
2190 }
2191 }
2192 }
2193
2194 // not found
2195 return FALSE;
2196}
2197
2198#endif // wxUSE_ACCEL
2199
2200// ----------------------------------------------------------------------------
2201// wxMenuBar menus showing
2202// ----------------------------------------------------------------------------
2203
2204void wxMenuBar::PopupCurrentMenu(bool selectFirst)
2205{
2206 wxCHECK_RET( m_current != -1, _T("no menu to popup") );
2207
2208 // forgot to call DismissMenu()?
2209 wxASSERT_MSG( !m_menuShown, _T("shouldn't show two menu at once!") );
2210
2211 // in any case, we should show it - even if we won't
2212 m_shouldShowMenu = TRUE;
2213
2214 if ( IsEnabledTop(m_current) )
2215 {
2216 // remember the menu we show
2217 m_menuShown = GetMenu(m_current);
2218
2219 // we don't show the menu at all if it has no items
2220 if ( !m_menuShown->IsEmpty() )
2221 {
2222 // position it correctly: note that we must use screen coords and
2223 // that we pass 0 as width to position the menu exactly below the
2224 // item, not to the right of it
2225 wxRect rectItem = GetItemRect(m_current);
2226 m_menuShown->Popup(ClientToScreen(rectItem.GetPosition()),
2227 wxSize(0, rectItem.GetHeight()),
2228 selectFirst);
2229 }
2230 else
2231 {
2232 // reset it back as no menu is shown
2233 m_menuShown = NULL;
2234 }
2235 }
2236 //else: don't show disabled menu
2237}
2238
2239void wxMenuBar::DismissMenu()
2240{
2241 wxCHECK_RET( m_menuShown, _T("can't dismiss menu if none is shown") );
2242
2243 m_menuShown->Dismiss();
2244 OnDismissMenu();
2245}
2246
2247void wxMenuBar::OnDismissMenu(bool dismissMenuBar)
2248{
2249 m_shouldShowMenu = FALSE;
2250 m_menuShown = NULL;
2251 if ( dismissMenuBar )
2252 {
2253 OnDismiss();
2254 }
2255}
2256
2257void wxMenuBar::OnDismiss()
2258{
2259 ReleaseCapture();
2260
2261 if ( m_current != -1 )
2262 {
2263 RefreshItem((size_t)m_current);
2264
2265 m_current = -1;
2266 }
2267
2268 GiveAwayFocus();
2269}
2270
2271void wxMenuBar::GiveAwayFocus()
2272{
2273 GetFrame()->SetFocus();
2274}
2275
2276// ----------------------------------------------------------------------------
2277// popup menu support
2278// ----------------------------------------------------------------------------
2279
2280wxEventLoop *wxWindow::ms_evtLoopPopup = NULL;
2281
2282bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
2283{
2284 wxCHECK_MSG( !ms_evtLoopPopup, FALSE,
2285 _T("can't show more than one popup menu at a time") );
2286
2287#ifdef __WXMSW__
2288 // we need to change the cursor before showing the menu as, apparently, no
2289 // cursor changes took place while the mouse is captured
2290 wxCursor cursorOld = GetCursor();
2291 SetCursor(wxCURSOR_ARROW);
2292#endif // __WXMSW__
2293
2294#if 0
2295 // flash any delayed log messages before showing the menu, otherwise it
2296 // could be dismissed (because it would lose focus) immediately after being
2297 // shown
2298 wxLog::FlushActive();
2299
2300 // some controls update themselves from OnIdle() call - let them do it
2301 wxIdleEvent event;
2302 wxTheApp->ProcessEvent(event);
2303
2304 // if the window hadn't been refreshed yet, the menu can adversely affect
2305 // its next OnPaint() handler execution - i.e. scrolled window refresh
2306 // logic breaks then as it scrolls part of the menu which hadn't been there
2307 // when the update event was generated into view
2308 Update();
2309#endif // 0
2310
2311 menu->SetInvokingWindow(this);
2312 menu->Popup(ClientToScreen(wxPoint(x, y)), wxSize(0, 0));
2313
2314 // this is not very useful if the menu was popped up because of the mouse
2315 // click but I think it is nice to do when it appears because of a key
2316 // press (i.e. Windows menu key)
2317 //
2318 // Windows itself doesn't do it, but IMHO this is nice
2319 WarpPointer(x, y);
2320
2321 // we have to redirect all keyboard input to the menu temporarily
2322 PushEventHandler(new wxMenuKbdRedirector(menu));
2323
2324 // enter the local modal loop
2325 ms_evtLoopPopup = new wxEventLoop;
2326 ms_evtLoopPopup->Run();
2327
2328 delete ms_evtLoopPopup;
2329 ms_evtLoopPopup = NULL;
2330
2331 // remove the handler
2332 PopEventHandler(TRUE /* delete it */);
2333
2334 menu->SetInvokingWindow(NULL);
2335
2336#ifdef __WXMSW__
2337 SetCursor(cursorOld);
2338#endif // __WXMSW__
2339
2340 return TRUE;
2341}
2342
2343void wxWindow::DismissPopupMenu()
2344{
2345 wxCHECK_RET( ms_evtLoopPopup, _T("no popup menu shown") );
2346
2347 ms_evtLoopPopup->Exit();
2348}
2349
2350#endif // wxUSE_MENUS
2351