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