X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/605c7e7ed81c740e80de66c751f50fc54bd77e4d..b09a59f521e4a365f1b073e26e53f17a5e03e1fa:/src/cocoa/menu.mm diff --git a/src/cocoa/menu.mm b/src/cocoa/menu.mm index 3c69ea7481..fc5e4b038c 100644 --- a/src/cocoa/menu.mm +++ b/src/cocoa/menu.mm @@ -6,7 +6,7 @@ // Created: 2002/12/09 // RCS-ID: $Id: // Copyright: (c) 2002 David Elliott -// Licence: wxWindows license +// Licence: wxWidgets licence ///////////////////////////////////////////////////////////////////////////// // ============================================================================ @@ -24,6 +24,7 @@ #endif // WX_PRECOMP #include "wx/cocoa/autorelease.h" +#include "wx/cocoa/string.h" #import #import @@ -43,31 +44,34 @@ IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler) bool wxMenu::Create(const wxString& title, long style) { wxAutoNSAutoreleasePool pool; - m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: [NSString stringWithCString: title.c_str()]]; + m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: wxNSStringWithWxString(title)]; + AssociateNSMenu(m_cocoaNSMenu); return true; } wxMenu::~wxMenu() { - [m_cocoaNSMenu release]; + DisassociateNSMenu(m_cocoaNSMenu); + if(!m_cocoaDeletes) + [m_cocoaNSMenu release]; } -bool wxMenu::DoAppend(wxMenuItem *item) +wxMenuItem* wxMenu::DoAppend(wxMenuItem *item) { wxAutoNSAutoreleasePool pool; if(!wxMenuBase::DoAppend(item)) - return false; + return NULL; [m_cocoaNSMenu addItem: item->GetNSMenuItem()]; - return true; + return item; } -bool wxMenu::DoInsert(unsigned long pos, wxMenuItem *item) +wxMenuItem* wxMenu::DoInsert(unsigned long pos, wxMenuItem *item) { wxAutoNSAutoreleasePool pool; if(!wxMenuBase::DoInsert(pos,item)) - return false; + return NULL; [m_cocoaNSMenu insertItem:item->GetNSMenuItem() atIndex:pos]; - return true; + return item; } wxMenuItem* wxMenu::DoRemove(wxMenuItem *item) @@ -79,6 +83,33 @@ wxMenuItem* wxMenu::DoRemove(wxMenuItem *item) return retitem; } +// This autoreleases the menu on the assumption that something is +// going to retain it shortly (for instance, it is going to be returned from +// an overloaded [NSStatusItem menu] or from the applicationDockMenu: +// NSApplication delegate method. +// +// It then sets a bool flag m_cocoaDeletes. When the NSMenu is dealloc'd +// (dealloc is the Cocoa destructor) we delete ourselves. In this manner we +// can be available for Cocoa calls until Cocoa is finished with us. +// +// I can see very few reasons to undo this. Nevertheless, it is implemented. +void wxMenu::SetCocoaDeletes(bool cocoaDeletes) +{ + if(m_cocoaDeletes==cocoaDeletes) + return; + m_cocoaDeletes = cocoaDeletes; + if(m_cocoaDeletes) + [m_cocoaNSMenu autorelease]; + else + [m_cocoaNSMenu retain]; +} + +void wxMenu::Cocoa_dealloc() +{ + if(m_cocoaDeletes) + delete this; +} + // ============================================================================ // wxMenuBar implementation // ============================================================================ @@ -97,6 +128,17 @@ bool wxMenuBar::Create(long style) return true; } +wxMenuBar::wxMenuBar(size_t n, + wxMenu *menus[], + const wxString titles[], + long style) +{ + Create(style); + + for ( size_t i = 0; i < n; ++i ) + Append(menus[i], titles[i]); +} + wxMenuBar::~wxMenuBar() { [m_cocoaNSMenu release]; @@ -105,12 +147,12 @@ wxMenuBar::~wxMenuBar() bool wxMenuBar::Append( wxMenu *menu, const wxString &title ) { wxAutoNSAutoreleasePool pool; - wxLogDebug("append menu=%p, title=%s",menu,title.c_str()); + wxLogTrace(wxTRACE_COCOA,wxT("append menu=%p, title=%s"),menu,title.c_str()); if(!wxMenuBarBase::Append(menu,title)) return false; wxASSERT(menu); wxASSERT(menu->GetNSMenu()); - NSString *menuTitle = [[NSString alloc] initWithCString: wxStripMenuCodes(title).c_str()]; + NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], wxStripMenuCodes(title)); NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""]; [menu->GetNSMenu() setTitle:menuTitle]; [newItem setSubmenu:menu->GetNSMenu()]; @@ -125,14 +167,14 @@ bool wxMenuBar::Append( wxMenu *menu, const wxString &title ) bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) { wxAutoNSAutoreleasePool pool; - wxLogDebug("insert pos=%lu, menu=%p, title=%s",pos,menu,title.c_str()); + wxLogTrace(wxTRACE_COCOA,wxT("insert pos=%lu, menu=%p, title=%s"),pos,menu,title.c_str()); // Get the current menu at this position wxMenu *nextmenu = GetMenu(pos); if(!wxMenuBarBase::Insert(pos,menu,title)) return false; wxASSERT(menu); wxASSERT(menu->GetNSMenu()); - NSString *menuTitle = [[NSString alloc] initWithCString: title.c_str()]; + NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], title); NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""]; [menu->GetNSMenu() setTitle:menuTitle]; [newItem setSubmenu:menu->GetNSMenu()]; @@ -180,7 +222,7 @@ wxString wxMenuBar::GetLabelTop(size_t pos) const wxMenu *menu = GetMenu(pos); int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:menu->GetNSMenu()]; wxASSERT(itemindex>=0); - return wxString([[[m_cocoaNSMenu itemAtIndex:itemindex] title] lossyCString]); + return wxStringWithNSString([[m_cocoaNSMenu itemAtIndex:itemindex] title]); } void wxMenuBar::Attach(wxFrame *frame)