// Author: Stefan Csomor
// Modified by:
// Created: 1998-01-01
-// RCS-ID: $Id: menu.cpp 54129 2008-06-11 19:30:52Z SC $
+// RCS-ID: $Id$
// Copyright: (c) Stefan Csomor
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
@implementation wxNSMenu
-- (id) init
+- (id) initWithTitle:(NSString*) title
{
- [super init];
+ [super initWithTitle:title];
+ impl = NULL;
return self;
}
@end
-@interface wxNSMenuController : NSObject
+@interface wxNSMenuController : NSObject wxOSX_10_6_AND_LATER(<NSMenuDelegate>)
{
}
@end
-@interface NSApplication(MissingAppleMenuCall)
-- (void)setAppleMenu:(NSMenu *)menu;
-@end
+@interface NSApplication(MissingAppleMenuCall)
+- (void)setAppleMenu:(NSMenu *)menu;
+@end
-class wxMenuCocoaImpl : public wxMenuImpl
+class wxMenuCocoaImpl : public wxMenuImpl
{
public :
- wxMenuCocoaImpl( wxMenu* peer , NSMenu* menu) : wxMenuImpl(peer), m_osxMenu(menu)
+ wxMenuCocoaImpl( wxMenu* peer , wxNSMenu* menu) : wxMenuImpl(peer), m_osxMenu(menu)
{
+ static wxNSMenuController* controller = NULL;
+ if ( controller == NULL )
+ {
+ controller = [[wxNSMenuController alloc] init];
+ }
+ [menu setDelegate:controller];
+ [m_osxMenu setImplementation:this];
+ // gc aware
+ if ( m_osxMenu )
+ CFRetain(m_osxMenu);
+ [m_osxMenu release];
}
-
+
virtual ~wxMenuCocoaImpl();
-
- virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos)
+
+ virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos)
{
+ NSMenuItem* nsmenuitem = (NSMenuItem*) pItem->GetPeer()->GetHMenuItem();
+ // make sure a call of SetSubMenu is also reflected (occuring after Create)
+ // update the native menu item accordingly
+
+ if ( pItem->IsSubMenu() )
+ {
+ wxMenu* wxsubmenu = pItem->GetSubMenu();
+ WXHMENU nssubmenu = wxsubmenu->GetHMenu();
+ if ( [nsmenuitem submenu] != nssubmenu )
+ {
+ wxsubmenu->GetPeer()->SetTitle( pItem->GetItemLabelText() );
+ [nsmenuitem setSubmenu:nssubmenu];
+ }
+ }
+
if ( pos == (size_t) -1 )
- [m_osxMenu addItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() ];
+ [m_osxMenu addItem:nsmenuitem ];
else
- [m_osxMenu insertItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() atIndex:pos];
+ [m_osxMenu insertItem:nsmenuitem atIndex:pos];
}
-
- virtual void Remove( wxMenuItem *pItem )
+
+ virtual void Remove( wxMenuItem *pItem )
{
[m_osxMenu removeItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem()];
}
-
+
virtual void MakeRoot()
{
[NSApp setMainMenu:m_osxMenu];
[NSApp setAppleMenu:[[m_osxMenu itemAtIndex:0] submenu]];
}
- virtual void Enable( bool enable )
+ virtual void Enable( bool WXUNUSED(enable) )
{
}
-
+
virtual void SetTitle( const wxString& text )
{
wxCFStringRef cfText(text);
static wxMenuImpl* Create( wxMenu* peer, const wxString& title );
static wxMenuImpl* CreateRootMenu( wxMenu* peer );
protected :
- NSMenu* m_osxMenu;
+ wxNSMenu* m_osxMenu;
} ;
wxMenuCocoaImpl::~wxMenuCocoaImpl()
{
- [m_osxMenu release];
+ [m_osxMenu setDelegate:nil];
+ [m_osxMenu setImplementation:nil];
+ // gc aware
+ if ( m_osxMenu )
+ CFRelease(m_osxMenu);
}
wxMenuImpl* wxMenuImpl::Create( wxMenu* peer, const wxString& title )
{
- static wxNSMenuController* controller = NULL;
- if ( controller == NULL )
- {
- controller = [[wxNSMenuController alloc] init];
- }
wxCFStringRef cfText( title );
wxNSMenu* menu = [[wxNSMenu alloc] initWithTitle:cfText.AsNSString()];
wxMenuImpl* c = new wxMenuCocoaImpl( peer, menu );
- [menu setDelegate:controller];
- [menu setImplementation:c];
return c;
}