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