Allow the wxMenu to be owned by the NSMenu so that it can be returned

from methods which Cocoa calls when a menu is to be popped up.


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@29867 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
David Elliott
2004-10-15 03:06:50 +00:00
parent 3312496dcc
commit 950432e407
2 changed files with 37 additions and 2 deletions

View File

@@ -27,7 +27,9 @@ class WXDLLEXPORT wxMenu : public wxMenuBase, public wxCocoaNSMenu
public:
// ctors and dtor
wxMenu(const wxString& title, long style = 0)
: wxMenuBase(title, style) { Create(title,style); }
: wxMenuBase(title, style)
, m_cocoaDeletes(false)
{ Create(title,style); }
bool Create(const wxString& title, long style = 0);
wxMenu(long style = 0) : wxMenuBase(style) { Create(wxEmptyString, style); }
@@ -39,8 +41,11 @@ public:
// ------------------------------------------------------------------------
public:
inline WX_NSMenu GetNSMenu() { return m_cocoaNSMenu; }
void SetCocoaDeletes(bool cocoaDeletes);
virtual void Cocoa_dealloc();
protected:
WX_NSMenu m_cocoaNSMenu;
bool m_cocoaDeletes;
// ------------------------------------------------------------------------
// Implementation
// ------------------------------------------------------------------------

View File

@@ -45,11 +45,14 @@ bool wxMenu::Create(const wxString& title, long style)
{
wxAutoNSAutoreleasePool pool;
m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: wxNSStringWithWxString(title)];
AssociateNSMenu(m_cocoaNSMenu);
return true;
}
wxMenu::~wxMenu()
{
DisassociateNSMenu(m_cocoaNSMenu);
if(!m_cocoaDeletes)
[m_cocoaNSMenu release];
}
@@ -80,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
// ============================================================================