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