]> git.saurik.com Git - wxWidgets.git/blobdiff - src/osx/cocoa/menuitem.mm
reverting part of r58797, otherwise wxClientDC get no graphics context at all
[wxWidgets.git] / src / osx / cocoa / menuitem.mm
index b8f33fc0cc900c47825dee76e2a01a21a6664f01..8599af3a619fae2c6c55c2376e4a9ad5e9ec454a 100644 (file)
 
 #include "wx/osx/private.h"
 
+// a mapping from wx ids to standard osx actions in order to support the native menu item handling
+// if a new mapping is added, make sure the wxNonOwnedWindowController has a handler for this action as well
+
+struct Mapping
+{
+    int menuid;
+    SEL action;
+};
+
+Mapping sActionToWXMapping[] =
+{
+    { wxID_UNDO, @selector(undo:) },
+    { wxID_REDO, @selector(redo:) },
+    { wxID_CUT, @selector(cut:) },
+    { wxID_COPY, @selector(copy:) },
+    { wxID_PASTE, @selector(paste:) },
+    { wxID_CLEAR, @selector(delete:) },
+    { wxID_SELECTALL, @selector(selectAll:) },
+    { 0, NULL }
+};
+
+int wxOSXGetIdFromSelector(SEL action )
+{
+    int i = 0 ;
+    while ( sActionToWXMapping[i].action != nil )
+    {
+        if ( sActionToWXMapping[i].action == action )
+            return sActionToWXMapping[i].menuid;
+        ++i;
+    }
+    
+    return 0;
+}
+
+SEL wxOSXGetSelectorFromID(int menuId )
+{
+    int i = 0 ;
+    while ( sActionToWXMapping[i].action != nil )
+    {
+        if ( sActionToWXMapping[i].menuid == menuId )
+            return sActionToWXMapping[i].action;
+        ++i;
+    }
+    
+    return nil;
+}
+
+
 @implementation wxNSMenuItem
 
-- (id) init
+- (id) initWithTitle:(NSString *)aString action:(SEL)aSelector keyEquivalent:(NSString *)charCode
 {
-    [super init];
+    [super initWithTitle:aString action:aSelector keyEquivalent:charCode];
      return self;
 }
 
     wxUnusedVar(sender);
     if ( impl )
     {
-        impl->GetWXPeer()->GetMenu()->HandleCommandProcess(impl->GetWXPeer());
+        wxMenuItem* menuitem = impl->GetWXPeer();
+        if ( menuitem->GetMenu()->HandleCommandProcess(menuitem) == false )
+        {
+        }
      }
 }
 
+- (void) setEnabled:(BOOL) flag
+{
+    [super setEnabled:flag];
+}
+
 - (BOOL)validateMenuItem:(NSMenuItem *) menuItem
 {
     wxUnusedVar(menuItem);
     if( impl )
     {
-        impl->GetWXPeer()->GetMenu()->HandleCommandUpdateStatus(impl->GetWXPeer());
-        return impl->GetWXPeer()->IsEnabled();
+        if ( impl->GetWXPeer()->GetMenu()->HandleCommandUpdateStatus(impl->GetWXPeer()) )
+            return impl->GetWXPeer()->IsEnabled();
     }
     return YES ;
 }
@@ -68,7 +124,7 @@ void wxMacCocoaMenuItemSetAccelerator( NSMenuItem* menuItem, wxAcceleratorEntry*
     int key = entry->GetKeyCode() ;
     if ( key )
     {
-        if (entry->GetFlags() & wxACCEL_CTRL);
+        if (entry->GetFlags() & wxACCEL_CTRL)
             modifiers |= NSCommandKeyMask;
 
         if (entry->GetFlags() & wxACCEL_ALT)
@@ -168,7 +224,7 @@ void wxMacCocoaMenuItemSetAccelerator( NSMenuItem* menuItem, wxAcceleratorEntry*
     }
 }
 
-class wxMenuItemCocoaImpl : public wxMenuItemImpl 
+class wxMenuItemCocoaImpl : public wxMenuItemImpl
 {
 public :
     wxMenuItemCocoaImpl( wxMenuItem* peer, NSMenuItem* item ) : wxMenuItemImpl(peer), m_osxMenuItem(item)
@@ -176,24 +232,24 @@ public :
         if ( ![m_osxMenuItem isSeparatorItem] )
             [(wxNSMenuItem*)m_osxMenuItem setImplementation:this];
     }
-    
+
     ~wxMenuItemCocoaImpl();
-        
-    void SetBitmap( const wxBitmap& bitmap ) 
+
+    void SetBitmap( const wxBitmap& bitmap )
     {
         [m_osxMenuItem setImage:bitmap.GetNSImage()];
     }
-    
-    void Enable( bool enable ) 
+
+    void Enable( bool enable )
     {
         [m_osxMenuItem setEnabled:enable];
     }
-    
-    void Check( bool check ) 
+
+    void Check( bool check )
     {
         [m_osxMenuItem setState:( check ?  NSOnState :  NSOffState) ];
     }
-    
+
     void Hide( bool hide )
     {
         // NB: setHidden is new as of 10.5 so we should not call it below there
@@ -202,17 +258,19 @@ public :
         else
             wxLogDebug("wxMenuItemCocoaImpl::Hide not yet supported under OS X < 10.5");
     }
-    
-    void SetLabel( const wxString& text, wxAcceleratorEntry *entry ) 
+
+    void SetLabel( const wxString& text, wxAcceleratorEntry *entry )
     {
         wxCFStringRef cfText(text);
         [m_osxMenuItem setTitle:cfText.AsNSString()];
-        
+
         if ( entry )
             wxMacCocoaMenuItemSetAccelerator( m_osxMenuItem, entry );
 
     }
     
+    bool DoDefault();
+
     void * GetHMenuItem() { return m_osxMenuItem; }
 
 protected :
@@ -223,11 +281,35 @@ wxMenuItemCocoaImpl::~wxMenuItemCocoaImpl()
 {
     if ( ![m_osxMenuItem isSeparatorItem] )
         [(wxNSMenuItem*)m_osxMenuItem setImplementation:nil];
+    [m_osxMenuItem release];
 }
 
+bool wxMenuItemCocoaImpl::DoDefault()
+{
+    bool handled=false;
+    int menuid = m_peer->GetId();
+    
+    NSApplication *theNSApplication = [NSApplication sharedApplication];
+    if (menuid == wxID_OSX_HIDE)
+    {
+        [theNSApplication hide:nil];
+        handled=true;
+    }
+    else if (menuid == wxID_OSX_HIDEOTHERS)
+    {
+        [theNSApplication hideOtherApplications:nil];
+        handled=true;
+    }
+    else if (menuid == wxID_OSX_SHOWALL)
+    {
+        [theNSApplication unhideAllApplications:nil];
+        handled=true;
+    }
+    return handled;
+}
 
 wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, wxMenu *pParentMenu,
-                       int WXUNUSED(id),
+                       int menuid,
                        const wxString& text,
                        wxAcceleratorEntry *entry,
                        const wxString& WXUNUSED(strHelp),
@@ -236,7 +318,7 @@ wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, wxMenu *pParentMenu,
 {
     wxMenuItemImpl* c = NULL;
     NSMenuItem* item = nil;
-    
+
     if ( kind == wxITEM_SEPARATOR )
     {
         item = [[NSMenuItem separatorItem] retain];
@@ -244,24 +326,34 @@ wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, wxMenu *pParentMenu,
     else
     {
         wxCFStringRef cfText(text);
-        wxNSMenuItem* temp = [ [ wxNSMenuItem alloc ] init ];
-        if ( ! pParentMenu->GetNoEventsMode() )
+        SEL selector = nil;
+        bool targetSelf = false;
+        if ( ! pParentMenu->GetNoEventsMode() && pSubMenu == NULL )
         {
-            [temp setTarget: temp];
-            [temp setAction: @selector(clickedAction:)];
+            selector = wxOSXGetSelectorFromID(menuid);
+            
+            if ( selector == nil )
+            {
+                selector = @selector(clickedAction:);
+                targetSelf = true;
+            }
         }
-        [temp setTitle:cfText.AsNSString()];
+        
+        wxNSMenuItem* menuitem = [ [ wxNSMenuItem alloc ] initWithTitle:cfText.AsNSString() action:selector keyEquivalent:@""];
+        if ( targetSelf )
+            [menuitem setTarget:menuitem];
+        
         if ( pSubMenu )
         {
             pSubMenu->GetPeer()->SetTitle( text );
-            [temp setSubmenu:pSubMenu->GetHMenu()];
+            [menuitem setSubmenu:pSubMenu->GetHMenu()];
         }
         else
         {
             if ( entry )
-                wxMacCocoaMenuItemSetAccelerator( temp, entry );
+                wxMacCocoaMenuItemSetAccelerator( menuitem, entry );
         }
-        item = temp;
+        item = menuitem;
     }
     c = new wxMenuItemCocoaImpl( peer, item );
     return c;