]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: menu.cpp | |
3 | // Purpose: wxMenu, wxMenuBar, wxMenuItem | |
a31a5f85 | 4 | // Author: Stefan Csomor |
e9576ca5 | 5 | // Modified by: |
a31a5f85 | 6 | // Created: 1998-01-01 |
e9576ca5 | 7 | // RCS-ID: $Id$ |
a31a5f85 | 8 | // Copyright: (c) Stefan Csomor |
65571936 | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
eb22f2a6 GD |
12 | #ifdef __GNUG__ |
13 | #pragma implementation "menu.h" | |
14 | #pragma implementation "menuitem.h" | |
15 | #endif | |
16 | ||
e9576ca5 SC |
17 | // ============================================================================ |
18 | // headers & declarations | |
19 | // ============================================================================ | |
20 | ||
77ffb593 | 21 | // wxWidgets headers |
e9576ca5 SC |
22 | // ----------------- |
23 | ||
03e11df5 | 24 | #include "wx/app.h" |
e9576ca5 SC |
25 | #include "wx/menu.h" |
26 | #include "wx/menuitem.h" | |
03e11df5 | 27 | #include "wx/window.h" |
e9576ca5 SC |
28 | #include "wx/log.h" |
29 | #include "wx/utils.h" | |
2b5f62a0 | 30 | #include "wx/frame.h" |
e9576ca5 | 31 | |
519cb848 SC |
32 | #include "wx/mac/uma.h" |
33 | ||
e9576ca5 SC |
34 | // other standard headers |
35 | // ---------------------- | |
36 | #include <string.h> | |
37 | ||
2f1ae414 | 38 | #if !USE_SHARED_LIBRARY |
e9576ca5 SC |
39 | IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) |
40 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler) | |
2f1ae414 | 41 | #endif |
e9576ca5 | 42 | |
519cb848 | 43 | // the (popup) menu title has this special id |
de5914a1 | 44 | static const int idMenuTitle = -3; |
b03e4fcd | 45 | static MenuItemIndex firstUserHelpMenuItem = 0 ; |
519cb848 SC |
46 | |
47 | const short kwxMacMenuBarResource = 1 ; | |
48 | const short kwxMacAppleMenuId = 1 ; | |
49 | ||
de5914a1 SC |
50 | |
51 | // Find an item given the Macintosh Menu Reference | |
52 | ||
53 | wxList wxWinMacMenuList(wxKEY_INTEGER); | |
54 | wxMenu *wxFindMenuFromMacMenu(MenuRef inMenuRef) | |
55 | { | |
56 | wxNode *node = wxWinMacMenuList.Find((long)inMenuRef); | |
57 | if (!node) | |
58 | return NULL; | |
59 | return (wxMenu *)node->GetData(); | |
60 | } | |
61 | ||
62 | void wxAssociateMenuWithMacMenu(MenuRef inMenuRef, wxMenu *menu) ; | |
63 | void wxAssociateMenuWithMacMenu(MenuRef inMenuRef, wxMenu *menu) | |
64 | { | |
65 | // adding NULL MenuRef is (first) surely a result of an error and | |
66 | // (secondly) breaks menu command processing | |
67 | wxCHECK_RET( inMenuRef != (MenuRef) NULL, wxT("attempt to add a NULL MenuRef to menu list") ); | |
68 | ||
69 | if ( !wxWinMacMenuList.Find((long)inMenuRef) ) | |
70 | wxWinMacMenuList.Append((long)inMenuRef, menu); | |
71 | } | |
72 | ||
73 | void wxRemoveMacMenuAssociation(wxMenu *menu) ; | |
74 | void wxRemoveMacMenuAssociation(wxMenu *menu) | |
75 | { | |
76 | wxWinMacMenuList.DeleteObject(menu); | |
77 | } | |
78 | ||
79 | ||
e9576ca5 SC |
80 | // ============================================================================ |
81 | // implementation | |
82 | // ============================================================================ | |
7c15086c SC |
83 | static void wxMenubarUnsetInvokingWindow( wxMenu *menu ) ; |
84 | static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win ); | |
519cb848 | 85 | |
e9576ca5 SC |
86 | // Menus |
87 | ||
88 | // Construct a menu with optional title (then use append) | |
519cb848 | 89 | |
f5c6eb5c | 90 | #ifdef __DARWIN__ |
82ca6dbc GD |
91 | short wxMenu::s_macNextMenuId = 3 ; |
92 | #else | |
519cb848 | 93 | short wxMenu::s_macNextMenuId = 2 ; |
82ca6dbc | 94 | #endif |
519cb848 | 95 | |
e7549107 | 96 | void wxMenu::Init() |
e9576ca5 | 97 | { |
e7549107 | 98 | m_doBreak = FALSE; |
7c15086c | 99 | m_startRadioGroup = -1; |
e7549107 SC |
100 | |
101 | // create the menu | |
58751a86 | 102 | m_macMenuId = s_macNextMenuId++; |
a9412f8f | 103 | m_hMenu = UMANewMenu(m_macMenuId, m_title, wxFont::GetDefaultEncoding() ); |
e7549107 SC |
104 | |
105 | if ( !m_hMenu ) | |
106 | { | |
22026088 | 107 | wxLogLastError(wxT("UMANewMenu failed")); |
e7549107 SC |
108 | } |
109 | ||
de5914a1 SC |
110 | wxAssociateMenuWithMacMenu( (MenuRef)m_hMenu , this ) ; |
111 | ||
e7549107 SC |
112 | // if we have a title, insert it in the beginning of the menu |
113 | if ( !!m_title ) | |
e9576ca5 | 114 | { |
519cb848 | 115 | Append(idMenuTitle, m_title) ; |
e9576ca5 SC |
116 | AppendSeparator() ; |
117 | } | |
e7549107 | 118 | } |
e9576ca5 | 119 | |
e7549107 SC |
120 | wxMenu::~wxMenu() |
121 | { | |
de5914a1 | 122 | wxRemoveMacMenuAssociation( this ) ; |
e40298d5 JS |
123 | if (MAC_WXHMENU(m_hMenu)) |
124 | ::DisposeMenu(MAC_WXHMENU(m_hMenu)); | |
e9576ca5 SC |
125 | } |
126 | ||
e7549107 | 127 | void wxMenu::Break() |
e9576ca5 | 128 | { |
e40298d5 | 129 | // not available on the mac platform |
e7549107 | 130 | } |
e9576ca5 | 131 | |
7c15086c | 132 | void wxMenu::Attach(wxMenuBarBase *menubar) |
e7549107 | 133 | { |
7c15086c | 134 | wxMenuBase::Attach(menubar); |
e7549107 | 135 | |
7c15086c | 136 | EndRadioGroup(); |
e9576ca5 SC |
137 | } |
138 | ||
e9576ca5 | 139 | // function appends a new item or submenu to the menu |
e7549107 SC |
140 | // append a new item or submenu to the menu |
141 | bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos) | |
e9576ca5 | 142 | { |
427ff662 | 143 | wxASSERT_MSG( pItem != NULL, wxT("can't append NULL item to the menu") ); |
e9576ca5 | 144 | |
e40298d5 JS |
145 | if ( pItem->IsSeparator() ) |
146 | { | |
147 | if ( pos == (size_t)-1 ) | |
148 | MacAppendMenu(MAC_WXHMENU(m_hMenu), "\p-"); | |
149 | else | |
150 | MacInsertMenuItem(MAC_WXHMENU(m_hMenu), "\p-" , pos); | |
151 | } | |
58751a86 | 152 | else |
e40298d5 JS |
153 | { |
154 | wxMenu *pSubMenu = pItem->GetSubMenu() ; | |
155 | if ( pSubMenu != NULL ) | |
156 | { | |
427ff662 | 157 | wxASSERT_MSG( pSubMenu->m_hMenu != NULL , wxT("invalid submenu added")); |
e40298d5 | 158 | pSubMenu->m_menuParent = this ; |
58751a86 | 159 | |
4224f059 | 160 | if (wxMenuBar::MacGetInstalledMenuBar() == GetMenuBar()) |
d46342af SC |
161 | { |
162 | pSubMenu->MacBeforeDisplay( true ) ; | |
163 | } | |
58751a86 | 164 | |
e40298d5 | 165 | if ( pos == (size_t)-1 ) |
a9412f8f | 166 | UMAAppendSubMenuItem(MAC_WXHMENU(m_hMenu), pItem->GetText(), wxFont::GetDefaultEncoding() , pSubMenu->m_macMenuId); |
e40298d5 | 167 | else |
a9412f8f | 168 | UMAInsertSubMenuItem(MAC_WXHMENU(m_hMenu), pItem->GetText(), wxFont::GetDefaultEncoding() , pos, pSubMenu->m_macMenuId); |
e40298d5 JS |
169 | pItem->UpdateItemBitmap() ; |
170 | pItem->UpdateItemStatus() ; | |
171 | } | |
172 | else | |
173 | { | |
174 | if ( pos == (size_t)-1 ) | |
175 | { | |
a9412f8f | 176 | UMAAppendMenuItem(MAC_WXHMENU(m_hMenu), wxT("a") , wxFont::GetDefaultEncoding() ); |
e40298d5 JS |
177 | pos = CountMenuItems(MAC_WXHMENU(m_hMenu)) ; |
178 | } | |
179 | else | |
180 | { | |
4ab107d7 SC |
181 | // MacOS counts menu items from 1 and inserts after, therefore having the |
182 | // same effect as wx 0 based and inserting before, we must correct pos | |
183 | // after however for updates to be correct | |
a9412f8f | 184 | UMAInsertMenuItem(MAC_WXHMENU(m_hMenu), wxT("a"), wxFont::GetDefaultEncoding(), pos); |
4ab107d7 | 185 | pos += 1 ; |
e40298d5 JS |
186 | } |
187 | ||
4ab107d7 | 188 | SetMenuItemCommandID( MAC_WXHMENU(m_hMenu) , pos , pItem->GetId() ) ; |
de5914a1 | 189 | SetMenuItemRefCon( MAC_WXHMENU(m_hMenu) , pos , (UInt32) pItem ) ; |
e40298d5 JS |
190 | pItem->UpdateItemText() ; |
191 | pItem->UpdateItemBitmap() ; | |
192 | pItem->UpdateItemStatus() ; | |
193 | ||
58751a86 | 194 | if ( pItem->GetId() == idMenuTitle ) |
e40298d5 JS |
195 | { |
196 | UMAEnableMenuItem(MAC_WXHMENU(m_hMenu) , pos , false ) ; | |
197 | } | |
198 | } | |
199 | } | |
e7549107 SC |
200 | // if we're already attached to the menubar, we must update it |
201 | if ( IsAttached() ) | |
202 | { | |
4224f059 | 203 | GetMenuBar()->Refresh(); |
e7549107 | 204 | } |
e40298d5 | 205 | return TRUE ; |
e9576ca5 SC |
206 | } |
207 | ||
7c15086c SC |
208 | void wxMenu::EndRadioGroup() |
209 | { | |
210 | // we're not inside a radio group any longer | |
211 | m_startRadioGroup = -1; | |
212 | } | |
213 | ||
9add9367 | 214 | wxMenuItem* wxMenu::DoAppend(wxMenuItem *item) |
e9576ca5 | 215 | { |
9add9367 | 216 | wxCHECK_MSG( item, NULL, _T("NULL item in wxMenu::DoAppend") ); |
7c15086c SC |
217 | |
218 | bool check = FALSE; | |
219 | ||
220 | if ( item->GetKind() == wxITEM_RADIO ) | |
221 | { | |
222 | int count = GetMenuItemCount(); | |
223 | ||
224 | if ( m_startRadioGroup == -1 ) | |
225 | { | |
226 | // start a new radio group | |
227 | m_startRadioGroup = count; | |
228 | ||
229 | // for now it has just one element | |
230 | item->SetAsRadioGroupStart(); | |
231 | item->SetRadioGroupEnd(m_startRadioGroup); | |
232 | ||
233 | // ensure that we have a checked item in the radio group | |
234 | check = TRUE; | |
235 | } | |
236 | else // extend the current radio group | |
237 | { | |
238 | // we need to update its end item | |
239 | item->SetRadioGroupStart(m_startRadioGroup); | |
240 | wxMenuItemList::Node *node = GetMenuItems().Item(m_startRadioGroup); | |
241 | ||
242 | if ( node ) | |
243 | { | |
244 | node->GetData()->SetRadioGroupEnd(count); | |
245 | } | |
246 | else | |
247 | { | |
248 | wxFAIL_MSG( _T("where is the radio group start item?") ); | |
249 | } | |
250 | } | |
251 | } | |
252 | else // not a radio item | |
253 | { | |
254 | EndRadioGroup(); | |
255 | } | |
256 | ||
257 | if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) ) | |
258 | { | |
9add9367 | 259 | return NULL; |
7c15086c SC |
260 | } |
261 | ||
262 | if ( check ) | |
263 | { | |
264 | // check the item initially | |
265 | item->Check(TRUE); | |
266 | } | |
267 | ||
9add9367 | 268 | return item; |
e9576ca5 SC |
269 | } |
270 | ||
9add9367 | 271 | wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item) |
e9576ca5 | 272 | { |
9add9367 RD |
273 | if (wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos)) |
274 | return item; | |
275 | else | |
276 | return NULL; | |
e9576ca5 SC |
277 | } |
278 | ||
e7549107 | 279 | wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) |
e9576ca5 | 280 | { |
e7549107 SC |
281 | // we need to find the items position in the child list |
282 | size_t pos; | |
283 | wxMenuItemList::Node *node = GetMenuItems().GetFirst(); | |
284 | for ( pos = 0; node; pos++ ) | |
285 | { | |
286 | if ( node->GetData() == item ) | |
287 | break; | |
e9576ca5 | 288 | |
e7549107 | 289 | node = node->GetNext(); |
e9576ca5 SC |
290 | } |
291 | ||
e7549107 SC |
292 | // DoRemove() (unlike Remove) can only be called for existing item! |
293 | wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") ); | |
519cb848 | 294 | |
e40298d5 | 295 | ::DeleteMenuItem(MAC_WXHMENU(m_hMenu) , pos + 1); |
e9576ca5 | 296 | |
e7549107 SC |
297 | if ( IsAttached() ) |
298 | { | |
7c15086c | 299 | // otherwise, the change won't be visible |
4224f059 | 300 | GetMenuBar()->Refresh(); |
e7549107 | 301 | } |
e9576ca5 | 302 | |
e7549107 SC |
303 | // and from internal data structures |
304 | return wxMenuBase::DoRemove(item); | |
e9576ca5 SC |
305 | } |
306 | ||
e9576ca5 SC |
307 | void wxMenu::SetTitle(const wxString& label) |
308 | { | |
e40298d5 | 309 | m_title = label ; |
a9412f8f | 310 | UMASetMenuTitle(MAC_WXHMENU(m_hMenu) , label , wxFont::GetDefaultEncoding() ) ; |
e9576ca5 | 311 | } |
e7549107 | 312 | bool wxMenu::ProcessCommand(wxCommandEvent & event) |
e9576ca5 SC |
313 | { |
314 | bool processed = FALSE; | |
315 | ||
e9576ca5 SC |
316 | // Try the menu's event handler |
317 | if ( !processed && GetEventHandler()) | |
318 | { | |
e7549107 | 319 | processed = GetEventHandler()->ProcessEvent(event); |
e9576ca5 | 320 | } |
519cb848 | 321 | |
e7549107 SC |
322 | // Try the window the menu was popped up from (and up through the |
323 | // hierarchy) | |
324 | wxWindow *win = GetInvokingWindow(); | |
325 | if ( !processed && win ) | |
326 | processed = win->GetEventHandler()->ProcessEvent(event); | |
327 | ||
328 | return processed; | |
329 | } | |
330 | ||
331 | ||
332 | // --------------------------------------------------------------------------- | |
333 | // other | |
334 | // --------------------------------------------------------------------------- | |
335 | ||
e7549107 SC |
336 | wxWindow *wxMenu::GetWindow() const |
337 | { | |
338 | if ( m_invokingWindow != NULL ) | |
339 | return m_invokingWindow; | |
4224f059 DE |
340 | else if ( GetMenuBar() != NULL) |
341 | return (wxWindow *) GetMenuBar()->GetFrame(); | |
e7549107 SC |
342 | |
343 | return NULL; | |
344 | } | |
519cb848 | 345 | |
58751a86 | 346 | // helper functions returning the mac menu position for a certain item, note that this is |
519cb848 SC |
347 | // mac-wise 1 - based, i.e. the first item has index 1 whereas on MSWin it has pos 0 |
348 | ||
58751a86 | 349 | int wxMenu::MacGetIndexFromId( int id ) |
519cb848 | 350 | { |
e7549107 SC |
351 | size_t pos; |
352 | wxMenuItemList::Node *node = GetMenuItems().GetFirst(); | |
353 | for ( pos = 0; node; pos++ ) | |
519cb848 | 354 | { |
e7549107 SC |
355 | if ( node->GetData()->GetId() == id ) |
356 | break; | |
519cb848 | 357 | |
e7549107 SC |
358 | node = node->GetNext(); |
359 | } | |
58751a86 | 360 | |
519cb848 | 361 | if (!node) |
e40298d5 | 362 | return 0; |
58751a86 | 363 | |
e40298d5 | 364 | return pos + 1 ; |
519cb848 SC |
365 | } |
366 | ||
58751a86 | 367 | int wxMenu::MacGetIndexFromItem( wxMenuItem *pItem ) |
519cb848 | 368 | { |
e7549107 SC |
369 | size_t pos; |
370 | wxMenuItemList::Node *node = GetMenuItems().GetFirst(); | |
371 | for ( pos = 0; node; pos++ ) | |
519cb848 | 372 | { |
e7549107 SC |
373 | if ( node->GetData() == pItem ) |
374 | break; | |
375 | ||
376 | node = node->GetNext(); | |
519cb848 SC |
377 | } |
378 | ||
379 | if (!node) | |
e40298d5 | 380 | return 0; |
58751a86 | 381 | |
e40298d5 | 382 | return pos + 1 ; |
519cb848 SC |
383 | } |
384 | ||
58751a86 | 385 | void wxMenu::MacEnableMenu( bool bDoEnable ) |
519cb848 | 386 | { |
e40298d5 | 387 | UMAEnableMenuItem(MAC_WXHMENU(m_hMenu) , 0 , bDoEnable ) ; |
58751a86 | 388 | |
e40298d5 | 389 | ::DrawMenuBar() ; |
519cb848 SC |
390 | } |
391 | ||
d46342af SC |
392 | // MacOS needs to know about submenus somewhere within this menu |
393 | // before it can be displayed , also hide special menu items like preferences | |
394 | // that are handled by the OS | |
58751a86 | 395 | void wxMenu::MacBeforeDisplay( bool isSubMenu ) |
d46342af SC |
396 | { |
397 | wxMenuItem* previousItem = NULL ; | |
5be55d56 | 398 | size_t pos ; |
d46342af SC |
399 | wxMenuItemList::Node *node; |
400 | wxMenuItem *item; | |
58751a86 | 401 | for (pos = 0, node = GetMenuItems().GetFirst(); node; node = node->GetNext(), pos++) |
d46342af SC |
402 | { |
403 | item = (wxMenuItem *)node->GetData(); | |
404 | wxMenu* subMenu = item->GetSubMenu() ; | |
58751a86 | 405 | if (subMenu) |
d46342af SC |
406 | { |
407 | subMenu->MacBeforeDisplay( true ) ; | |
408 | } | |
409 | else | |
410 | { | |
411 | #if TARGET_CARBON | |
412 | if ( UMAGetSystemVersion() >= 0x1000 ) | |
413 | { | |
414 | if ( item->GetId() == wxApp::s_macPreferencesMenuItemId || item->GetId() == wxApp::s_macExitMenuItemId) | |
415 | { | |
416 | ChangeMenuItemAttributes( MAC_WXHMENU( GetHMenu() ) , pos + 1, kMenuItemAttrHidden, 0 ); | |
5be55d56 VZ |
417 | if ( GetMenuItems().GetCount() == pos + 1 && |
418 | previousItem != NULL && | |
419 | previousItem->IsSeparator() ) | |
d46342af SC |
420 | { |
421 | ChangeMenuItemAttributes( MAC_WXHMENU( GetHMenu() ) , pos , kMenuItemAttrHidden, 0 ); | |
422 | } | |
423 | } | |
424 | } | |
58751a86 | 425 | #endif |
d46342af SC |
426 | } |
427 | previousItem = item ; | |
428 | } | |
429 | ||
430 | if ( isSubMenu ) | |
431 | ::InsertMenu(MAC_WXHMENU( GetHMenu()), -1); | |
432 | ||
433 | } | |
434 | // undo all changes from the MacBeforeDisplay call | |
58751a86 | 435 | void wxMenu::MacAfterDisplay( bool isSubMenu ) |
d46342af SC |
436 | { |
437 | if ( isSubMenu ) | |
438 | ::DeleteMenu(MacGetMenuId()); | |
439 | ||
440 | wxMenuItem* previousItem = NULL ; | |
441 | int pos ; | |
442 | wxMenuItemList::Node *node; | |
443 | wxMenuItem *item; | |
58751a86 | 444 | for (pos = 0, node = GetMenuItems().GetFirst(); node; node = node->GetNext(), pos++) |
d46342af SC |
445 | { |
446 | item = (wxMenuItem *)node->GetData(); | |
447 | wxMenu* subMenu = item->GetSubMenu() ; | |
58751a86 | 448 | if (subMenu) |
d46342af SC |
449 | { |
450 | subMenu->MacAfterDisplay( true ) ; | |
451 | } | |
452 | else | |
453 | { | |
454 | // no need to undo hidings | |
455 | } | |
456 | previousItem = item ; | |
457 | } | |
458 | } | |
459 | ||
e9576ca5 | 460 | // Menu Bar |
519cb848 | 461 | |
58751a86 | 462 | /* |
519cb848 SC |
463 | |
464 | Mac Implementation note : | |
465 | ||
466 | The Mac has only one global menubar, so we attempt to install the currently | |
467 | active menubar from a frame, we currently don't take into account mdi-frames | |
468 | which would ask for menu-merging | |
469 | ||
58751a86 | 470 | Secondly there is no mac api for changing a menubar that is not the current |
519cb848 SC |
471 | menubar, so we have to wait for preparing the actual menubar until the |
472 | wxMenubar is to be used | |
473 | ||
58751a86 | 474 | We can in subsequent versions use MacInstallMenuBar to provide some sort of |
519cb848 SC |
475 | auto-merge for MDI in case this will be necessary |
476 | ||
477 | */ | |
478 | ||
479 | wxMenuBar* wxMenuBar::s_macInstalledMenuBar = NULL ; | |
1b1d2207 | 480 | wxMenuBar* wxMenuBar::s_macCommonMenuBar = NULL ; |
519cb848 | 481 | |
e7549107 | 482 | void wxMenuBar::Init() |
e9576ca5 SC |
483 | { |
484 | m_eventHandler = this; | |
e9576ca5 | 485 | m_menuBarFrame = NULL; |
7c15086c | 486 | m_invokingWindow = (wxWindow*) NULL; |
e9576ca5 SC |
487 | } |
488 | ||
51abe921 SC |
489 | wxMenuBar::wxMenuBar() |
490 | { | |
491 | Init(); | |
492 | } | |
493 | ||
494 | wxMenuBar::wxMenuBar( long WXUNUSED(style) ) | |
495 | { | |
496 | Init(); | |
497 | } | |
498 | ||
e7549107 SC |
499 | |
500 | wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[]) | |
e9576ca5 | 501 | { |
e7549107 SC |
502 | Init(); |
503 | ||
504 | m_titles.Alloc(count); | |
505 | ||
506 | for ( int i = 0; i < count; i++ ) | |
507 | { | |
508 | m_menus.Append(menus[i]); | |
509 | m_titles.Add(titles[i]); | |
510 | ||
511 | menus[i]->Attach(this); | |
512 | } | |
e9576ca5 SC |
513 | } |
514 | ||
515 | wxMenuBar::~wxMenuBar() | |
516 | { | |
1b1d2207 DE |
517 | if (s_macCommonMenuBar == this) |
518 | s_macCommonMenuBar = NULL; | |
e40298d5 JS |
519 | if (s_macInstalledMenuBar == this) |
520 | { | |
521 | ::ClearMenuBar(); | |
522 | s_macInstalledMenuBar = NULL; | |
523 | } | |
519cb848 | 524 | |
e7549107 SC |
525 | } |
526 | ||
6d6da89c | 527 | void wxMenuBar::Refresh(bool WXUNUSED(eraseBackground), const wxRect *WXUNUSED(rect)) |
e7549107 SC |
528 | { |
529 | wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") ); | |
e9576ca5 | 530 | |
e7549107 | 531 | DrawMenuBar(); |
e9576ca5 SC |
532 | } |
533 | ||
58751a86 | 534 | void wxMenuBar::MacInstallMenuBar() |
519cb848 | 535 | { |
2b8a6962 GD |
536 | if ( s_macInstalledMenuBar == this ) |
537 | return ; | |
58751a86 | 538 | |
53d92341 SC |
539 | MenuBarHandle menubar = NULL ; |
540 | #if TARGET_API_MAC_OSX | |
541 | menubar = NewHandleClear( 6 /* sizeof( MenuBarHeader ) */ ) ; | |
542 | #else | |
543 | menubar = NewHandleClear( 12 ) ; | |
544 | (*menubar)[3] = 0x0a ; | |
545 | #endif | |
2b8a6962 | 546 | ::SetMenuBar( menubar ) ; |
dd05f811 | 547 | DisposeMenuBar( menubar ) ; |
cda73c27 SC |
548 | MenuHandle appleMenu = NULL ; |
549 | char appleMenuTitle[3] = { 01 , kMenuAppleLogoFilledGlyph , 0 } ; | |
550 | ||
551 | verify_noerr( CreateNewMenu( kwxMacAppleMenuId , 0 , &appleMenu ) ) ; | |
552 | verify_noerr( SetMenuTitle( appleMenu , (ConstStr255Param) appleMenuTitle ) ); | |
23c6ddc8 RN |
553 | |
554 | //under 10.3 it is a standard to include a seperator between About and Preferences | |
555 | #if ( MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 ) | |
e957d41c | 556 | MacInsertMenuItem( appleMenu , "\p-" , 0 ) ; |
23c6ddc8 RN |
557 | #endif |
558 | ||
cda73c27 SC |
559 | MacInsertMenuItem( appleMenu , "\pAbout..." , 0 ) ; |
560 | MacInsertMenu( appleMenu , 0 ) ; | |
561 | ||
7c15086c | 562 | // clean-up the help menu before adding new items |
e40298d5 JS |
563 | MenuHandle mh = NULL ; |
564 | if ( UMAGetHelpMenu( &mh , &firstUserHelpMenuItem) == noErr ) | |
565 | { | |
566 | for ( int i = CountMenuItems( mh ) ; i >= firstUserHelpMenuItem ; --i ) | |
567 | { | |
568 | DeleteMenuItem( mh , i ) ; | |
569 | } | |
570 | } | |
571 | else | |
572 | { | |
573 | mh = NULL ; | |
574 | } | |
7c15086c | 575 | #if TARGET_CARBON |
e40298d5 JS |
576 | if ( UMAGetSystemVersion() >= 0x1000 && wxApp::s_macPreferencesMenuItemId) |
577 | { | |
578 | wxMenuItem *item = FindItem( wxApp::s_macPreferencesMenuItemId , NULL ) ; | |
579 | if ( item == NULL || !(item->IsEnabled()) ) | |
580 | DisableMenuCommand( NULL , kHICommandPreferences ) ; | |
581 | else | |
582 | EnableMenuCommand( NULL , kHICommandPreferences ) ; | |
583 | } | |
7c15086c | 584 | #endif |
e40298d5 JS |
585 | for (size_t i = 0; i < m_menus.GetCount(); i++) |
586 | { | |
f823cacb | 587 | wxMenuItemList::Node *node; |
2b5f62a0 VZ |
588 | wxMenuItem *item; |
589 | int pos ; | |
e40298d5 JS |
590 | wxMenu* menu = m_menus[i] , *subMenu = NULL ; |
591 | ||
427ff662 | 592 | if( m_titles[i] == wxT("?") || m_titles[i] == wxT("&?") || m_titles[i] == wxApp::s_macHelpMenuTitleName ) |
e40298d5 JS |
593 | { |
594 | if ( mh == NULL ) | |
595 | { | |
596 | continue ; | |
597 | } | |
58751a86 RD |
598 | |
599 | for (pos = 0 , node = menu->GetMenuItems().GetFirst(); node; node = node->GetNext(), pos++) | |
e40298d5 JS |
600 | { |
601 | item = (wxMenuItem *)node->GetData(); | |
602 | subMenu = item->GetSubMenu() ; | |
58751a86 | 603 | if (subMenu) |
e40298d5 JS |
604 | { |
605 | // we don't support hierarchical menus in the help menu yet | |
606 | } | |
58751a86 | 607 | else |
e40298d5 JS |
608 | { |
609 | if ( item->IsSeparator() ) | |
610 | { | |
611 | if ( mh ) | |
612 | MacAppendMenu(mh, "\p-" ); | |
613 | } | |
614 | else | |
615 | { | |
616 | wxAcceleratorEntry* entry = wxGetAccelFromString( item->GetText() ) ; | |
617 | ||
618 | if ( item->GetId() == wxApp::s_macAboutMenuItemId ) | |
58751a86 | 619 | { |
a9412f8f | 620 | UMASetMenuItemText( GetMenuHandle( kwxMacAppleMenuId ) , 1 , item->GetText() , wxFont::GetDefaultEncoding() ); |
e40298d5 JS |
621 | UMAEnableMenuItem( GetMenuHandle( kwxMacAppleMenuId ) , 1 , true ); |
622 | SetMenuItemCommandID( GetMenuHandle( kwxMacAppleMenuId ) , 1 , item->GetId() ) ; | |
de5914a1 | 623 | SetMenuItemRefCon(GetMenuHandle( kwxMacAppleMenuId ) , 1 , (UInt32)item ) ; |
e40298d5 JS |
624 | UMASetMenuItemShortcut( GetMenuHandle( kwxMacAppleMenuId ) , 1 , entry ) ; |
625 | } | |
626 | else | |
627 | { | |
628 | if ( mh ) | |
629 | { | |
a9412f8f | 630 | UMAAppendMenuItem(mh, item->GetText() , wxFont::GetDefaultEncoding(), entry); |
e40298d5 | 631 | SetMenuItemCommandID( mh , CountMenuItems(mh) , item->GetId() ) ; |
de5914a1 | 632 | SetMenuItemRefCon( mh , CountMenuItems(mh) , (UInt32)item ) ; |
e40298d5 JS |
633 | } |
634 | } | |
58751a86 | 635 | |
e40298d5 JS |
636 | delete entry ; |
637 | } | |
638 | } | |
639 | } | |
640 | } | |
641 | else | |
642 | { | |
a9412f8f | 643 | UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , m_titles[i], m_font.GetEncoding() ) ; |
d46342af | 644 | m_menus[i]->MacBeforeDisplay(false) ; |
e40298d5 | 645 | ::InsertMenu(MAC_WXHMENU(m_menus[i]->GetHMenu()), 0); |
e40298d5 JS |
646 | } |
647 | } | |
648 | ::DrawMenuBar() ; | |
649 | s_macInstalledMenuBar = this; | |
519cb848 SC |
650 | } |
651 | ||
e7549107 | 652 | void wxMenuBar::EnableTop(size_t pos, bool enable) |
e9576ca5 | 653 | { |
e7549107 | 654 | wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") ); |
e40298d5 | 655 | m_menus[pos]->MacEnableMenu( enable ) ; |
e7549107 | 656 | Refresh(); |
e9576ca5 SC |
657 | } |
658 | ||
c393c740 JS |
659 | bool wxMenuBar::Enable( bool enable) |
660 | { | |
5059f192 | 661 | wxCHECK_MSG( IsAttached(), false, wxT("doesn't work with unattached menubars") ); |
c393c740 JS |
662 | size_t i; |
663 | for (i = 0; i < GetMenuCount(); i++) | |
664 | { | |
665 | EnableTop(i, enable); | |
666 | } | |
667 | return true; | |
668 | } | |
669 | ||
e7549107 | 670 | void wxMenuBar::SetLabelTop(size_t pos, const wxString& label) |
e9576ca5 | 671 | { |
e7549107 | 672 | wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") ); |
e9576ca5 | 673 | |
e7549107 | 674 | m_titles[pos] = label; |
e9576ca5 | 675 | |
e7549107 SC |
676 | if ( !IsAttached() ) |
677 | { | |
e9576ca5 | 678 | return; |
e7549107 | 679 | } |
e9576ca5 | 680 | |
519cb848 | 681 | m_menus[pos]->SetTitle( label ) ; |
e40298d5 JS |
682 | if (wxMenuBar::s_macInstalledMenuBar == this) // are we currently installed ? |
683 | { | |
684 | ::SetMenuBar( GetMenuBar() ) ; | |
685 | ::InvalMenuBar() ; | |
686 | } | |
e9576ca5 SC |
687 | } |
688 | ||
e7549107 | 689 | wxString wxMenuBar::GetLabelTop(size_t pos) const |
e9576ca5 | 690 | { |
e7549107 SC |
691 | wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString, |
692 | wxT("invalid menu index in wxMenuBar::GetLabelTop") ); | |
519cb848 | 693 | |
e7549107 | 694 | return m_titles[pos]; |
e9576ca5 SC |
695 | } |
696 | ||
e7549107 | 697 | int wxMenuBar::FindMenu(const wxString& title) |
e9576ca5 | 698 | { |
e7549107 | 699 | wxString menuTitle = wxStripMenuCodes(title); |
e9576ca5 | 700 | |
e7549107 SC |
701 | size_t count = GetMenuCount(); |
702 | for ( size_t i = 0; i < count; i++ ) | |
e9576ca5 | 703 | { |
e7549107 SC |
704 | wxString title = wxStripMenuCodes(m_titles[i]); |
705 | if ( menuTitle == title ) | |
58751a86 | 706 | return i; |
e9576ca5 | 707 | } |
e9576ca5 | 708 | |
e7549107 | 709 | return wxNOT_FOUND; |
e9576ca5 | 710 | |
e9576ca5 SC |
711 | } |
712 | ||
e7549107 SC |
713 | |
714 | // --------------------------------------------------------------------------- | |
715 | // wxMenuBar construction | |
716 | // --------------------------------------------------------------------------- | |
717 | ||
718 | // --------------------------------------------------------------------------- | |
719 | // wxMenuBar construction | |
720 | // --------------------------------------------------------------------------- | |
721 | ||
722 | wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) | |
e9576ca5 | 723 | { |
e7549107 SC |
724 | wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title); |
725 | if ( !menuOld ) | |
726 | return FALSE; | |
727 | m_titles[pos] = title; | |
e9576ca5 | 728 | |
e7549107 | 729 | if ( IsAttached() ) |
e9576ca5 | 730 | { |
e40298d5 JS |
731 | if (s_macInstalledMenuBar == this) |
732 | { | |
d46342af | 733 | menuOld->MacAfterDisplay( false ) ; |
e40298d5 JS |
734 | ::DeleteMenu( menuOld->MacGetMenuId() /* m_menus[pos]->MacGetMenuId() */ ) ; |
735 | { | |
d46342af | 736 | menu->MacBeforeDisplay( false ) ; |
a9412f8f | 737 | UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , title , m_font.GetEncoding() ) ; |
e40298d5 JS |
738 | if ( pos == m_menus.GetCount() - 1) |
739 | { | |
740 | ::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , 0 ) ; | |
741 | } | |
742 | else | |
743 | { | |
744 | ::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , m_menus[pos+1]->MacGetMenuId() ) ; | |
745 | } | |
746 | } | |
747 | } | |
e9576ca5 | 748 | |
e7549107 | 749 | Refresh(); |
e9576ca5 | 750 | } |
e9576ca5 | 751 | |
e7549107 | 752 | return menuOld; |
e9576ca5 SC |
753 | } |
754 | ||
e7549107 | 755 | bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) |
e9576ca5 | 756 | { |
e7549107 SC |
757 | if ( !wxMenuBarBase::Insert(pos, menu, title) ) |
758 | return FALSE; | |
e9576ca5 | 759 | |
e7549107 | 760 | m_titles.Insert(title, pos); |
e9576ca5 | 761 | |
a9412f8f | 762 | UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , title , m_font.GetEncoding() ) ; |
e7549107 | 763 | |
d46342af | 764 | if ( IsAttached() && s_macInstalledMenuBar == this ) |
e9576ca5 | 765 | { |
d46342af | 766 | if (s_macInstalledMenuBar == this) |
e40298d5 | 767 | { |
d46342af SC |
768 | menu->MacBeforeDisplay( false ) ; |
769 | if ( pos == (size_t) -1 || pos + 1 == m_menus.GetCount() ) | |
770 | { | |
771 | ::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , 0 ) ; | |
772 | } | |
773 | else | |
774 | { | |
775 | ::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , m_menus[pos+1]->MacGetMenuId() ) ; | |
776 | } | |
e40298d5 | 777 | } |
e7549107 | 778 | Refresh(); |
e9576ca5 | 779 | } |
e7549107 SC |
780 | |
781 | return TRUE; | |
e9576ca5 SC |
782 | } |
783 | ||
51abe921 SC |
784 | wxMenu *wxMenuBar::Remove(size_t pos) |
785 | { | |
786 | wxMenu *menu = wxMenuBarBase::Remove(pos); | |
787 | if ( !menu ) | |
788 | return NULL; | |
789 | ||
790 | if ( IsAttached() ) | |
791 | { | |
e40298d5 JS |
792 | if (s_macInstalledMenuBar == this) |
793 | { | |
794 | ::DeleteMenu( menu->MacGetMenuId() /* m_menus[pos]->MacGetMenuId() */ ) ; | |
795 | } | |
51abe921 | 796 | |
51abe921 SC |
797 | Refresh(); |
798 | } | |
799 | ||
5fe38474 | 800 | m_titles.RemoveAt(pos); |
51abe921 SC |
801 | |
802 | return menu; | |
803 | } | |
804 | ||
805 | bool wxMenuBar::Append(wxMenu *menu, const wxString& title) | |
806 | { | |
807 | WXHMENU submenu = menu ? menu->GetHMenu() : 0; | |
808 | wxCHECK_MSG( submenu, FALSE, wxT("can't append invalid menu to menubar") ); | |
809 | ||
810 | if ( !wxMenuBarBase::Append(menu, title) ) | |
811 | return FALSE; | |
812 | ||
51abe921 | 813 | m_titles.Add(title); |
58751a86 | 814 | |
a9412f8f | 815 | UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , title , m_font.GetEncoding() ) ; |
51abe921 SC |
816 | |
817 | if ( IsAttached() ) | |
818 | { | |
e40298d5 JS |
819 | if (s_macInstalledMenuBar == this) |
820 | { | |
821 | ::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , 0 ) ; | |
822 | } | |
51abe921 SC |
823 | |
824 | Refresh(); | |
825 | } | |
826 | ||
7c15086c SC |
827 | // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables |
828 | // adding menu later on. | |
829 | if (m_invokingWindow) | |
830 | wxMenubarSetInvokingWindow( menu, m_invokingWindow ); | |
831 | ||
51abe921 SC |
832 | return TRUE; |
833 | } | |
834 | ||
2b5f62a0 VZ |
835 | static void wxMenubarUnsetInvokingWindow( wxMenu *menu ) |
836 | { | |
837 | menu->SetInvokingWindow( (wxWindow*) NULL ); | |
838 | ||
839 | wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst(); | |
840 | while (node) | |
841 | { | |
842 | wxMenuItem *menuitem = node->GetData(); | |
843 | if (menuitem->IsSubMenu()) | |
844 | wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu() ); | |
845 | node = node->GetNext(); | |
846 | } | |
847 | } | |
848 | ||
849 | static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win ) | |
850 | { | |
851 | menu->SetInvokingWindow( win ); | |
852 | ||
853 | wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst(); | |
854 | while (node) | |
855 | { | |
856 | wxMenuItem *menuitem = node->GetData(); | |
857 | if (menuitem->IsSubMenu()) | |
858 | wxMenubarSetInvokingWindow( menuitem->GetSubMenu() , win ); | |
859 | node = node->GetNext(); | |
860 | } | |
861 | } | |
862 | ||
863 | void wxMenuBar::UnsetInvokingWindow() | |
864 | { | |
7c15086c | 865 | m_invokingWindow = (wxWindow*) NULL; |
2b5f62a0 VZ |
866 | wxMenuList::Node *node = m_menus.GetFirst(); |
867 | while (node) | |
868 | { | |
869 | wxMenu *menu = node->GetData(); | |
870 | wxMenubarUnsetInvokingWindow( menu ); | |
871 | node = node->GetNext(); | |
872 | } | |
873 | } | |
874 | ||
875 | void wxMenuBar::SetInvokingWindow(wxFrame *frame) | |
876 | { | |
7c15086c | 877 | m_invokingWindow = frame; |
2b5f62a0 VZ |
878 | wxMenuList::Node *node = m_menus.GetFirst(); |
879 | while (node) | |
880 | { | |
881 | wxMenu *menu = node->GetData(); | |
882 | wxMenubarSetInvokingWindow( menu, frame ); | |
883 | node = node->GetNext(); | |
884 | } | |
885 | } | |
886 | ||
90b959ae | 887 | void wxMenuBar::Detach() |
2f1ae414 | 888 | { |
90b959ae SC |
889 | wxMenuBarBase::Detach() ; |
890 | } | |
2f1ae414 | 891 | |
90b959ae SC |
892 | void wxMenuBar::Attach(wxFrame *frame) |
893 | { | |
894 | wxMenuBarBase::Attach( frame ) ; | |
2f1ae414 | 895 | } |
51abe921 SC |
896 | // --------------------------------------------------------------------------- |
897 | // wxMenuBar searching for menu items | |
898 | // --------------------------------------------------------------------------- | |
899 | ||
900 | // Find the itemString in menuString, and return the item id or wxNOT_FOUND | |
901 | int wxMenuBar::FindMenuItem(const wxString& menuString, | |
902 | const wxString& itemString) const | |
903 | { | |
904 | wxString menuLabel = wxStripMenuCodes(menuString); | |
905 | size_t count = GetMenuCount(); | |
906 | for ( size_t i = 0; i < count; i++ ) | |
907 | { | |
908 | wxString title = wxStripMenuCodes(m_titles[i]); | |
ef089805 | 909 | if ( menuLabel == title ) |
51abe921 SC |
910 | return m_menus[i]->FindItem(itemString); |
911 | } | |
912 | ||
913 | return wxNOT_FOUND; | |
914 | } | |
915 | ||
916 | wxMenuItem *wxMenuBar::FindItem(int id, wxMenu **itemMenu) const | |
917 | { | |
918 | if ( itemMenu ) | |
919 | *itemMenu = NULL; | |
920 | ||
921 | wxMenuItem *item = NULL; | |
922 | size_t count = GetMenuCount(); | |
923 | for ( size_t i = 0; !item && (i < count); i++ ) | |
924 | { | |
925 | item = m_menus[i]->FindItem(id, itemMenu); | |
926 | } | |
927 | ||
928 | return item; | |
929 | } | |
930 | ||
931 |