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