]> git.saurik.com Git - wxWidgets.git/commitdiff
Allow the wxMenu to be owned by the NSMenu so that it can be returned
authorDavid Elliott <dfe@tgwbd.org>
Fri, 15 Oct 2004 03:06:50 +0000 (03:06 +0000)
committerDavid Elliott <dfe@tgwbd.org>
Fri, 15 Oct 2004 03:06:50 +0000 (03:06 +0000)
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

include/wx/cocoa/menu.h
src/cocoa/menu.mm

index 819a36ddf93b00c1214f13896141e98c88c4a4bc..e05cd49d7041b023f3929d04fbac28da850ef773 100644 (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
 // ------------------------------------------------------------------------
index dee38021275ed768f822d47a973b0494a392ab63..529f422350d674e6dccfd96c166c16bad1ba2644 100644 (file)
@@ -45,12 +45,15 @@ bool wxMenu::Create(const wxString& title, long style)
 {
     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)
@@ -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
 // ============================================================================