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