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