// Created: 2002/12/09
// RCS-ID: $Id:
// Copyright: (c) 2002 David Elliott
-// Licence: wxWindows license
+// Licence: wxWidgets licence
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
{
wxAutoNSAutoreleasePool pool;
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];
}
wxMenuItem* wxMenu::DoAppend(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
// ============================================================================
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];
bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
{
wxAutoNSAutoreleasePool pool;
- wxLogDebug(wxT("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);
bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
{
wxAutoNSAutoreleasePool pool;
- wxLogDebug(wxT("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))