]> git.saurik.com Git - wxWidgets.git/commitdiff
support native edit menu handling (cocoa enables menu items in built-in modal dialogs...
authorStefan Csomor <csomor@advancedconcepts.ch>
Wed, 16 Feb 2011 18:32:31 +0000 (18:32 +0000)
committerStefan Csomor <csomor@advancedconcepts.ch>
Wed, 16 Feb 2011 18:32:31 +0000 (18:32 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66907 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/osx/cocoa/menu.mm
src/osx/cocoa/menuitem.mm
src/osx/cocoa/nonownedwnd.mm

index ea02cf9f2b3c8b2e8f0b3f93744ce9a917054f3f..053c1200520ef980c26e3d0905cc60b389446f32 100644 (file)
@@ -36,9 +36,9 @@
 
 @implementation wxNSMenu
 
-- (id) init
+- (id) initWithTitle:(NSString*) title
 {
-    [super init];
+    [super initWithTitle:title];
     impl = NULL;
     return self;
 }
index c253f75672c9d77b55d47cfe79e85b37bd46491f..2c9a16cdbefce90b07f65570ba0fc5f7149cabdc 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;
 }
 
      }
 }
 
+- (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 ;
 }
@@ -256,7 +309,7 @@ bool wxMenuItemCocoaImpl::DoDefault()
 }
 
 wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, wxMenu *pParentMenu,
-                       int WXUNUSED(id),
+                       int menuid,
                        const wxString& text,
                        wxAcceleratorEntry *entry,
                        const wxString& WXUNUSED(strHelp),
@@ -273,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;
index 1c97ba28fc92e3a96280ccd327ffd40b8f7b283c..6adfba6560b37398ddc4b1ea5411c73b92845a33 100644 (file)
@@ -263,6 +263,8 @@ bool shouldHandleSelector(SEL selector)
 
 @end
 
+extern int wxOSXGetIdFromSelector(SEL action );
+
 @implementation wxNonOwnedWindowController
 
 - (id) init
@@ -271,6 +273,72 @@ bool shouldHandleSelector(SEL selector)
     return self;
 }
 
+- (BOOL) triggerMenu:(SEL) action
+{
+    wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar();
+    if ( mbar )
+    {
+        wxMenu* menu = NULL;
+        wxMenuItem* menuitem = mbar->FindItem(wxOSXGetIdFromSelector(action), &menu);
+        if ( menu != NULL && menuitem != NULL)
+            return menu->HandleCommandProcess(menuitem);
+    }
+    return NO;
+}
+
+- (BOOL)validateMenuItem:(NSMenuItem *)menuItem 
+{    
+    SEL action = [menuItem action];
+
+    wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar();
+    if ( mbar )
+    {
+        wxMenu* menu = NULL;
+        wxMenuItem* menuitem = mbar->FindItem(wxOSXGetIdFromSelector(action), &menu);
+        if ( menu != NULL && menuitem != NULL)
+        {
+            if ( menu->HandleCommandUpdateStatus(menuitem) )
+                return menuitem->IsEnabled();
+        }
+    }
+    return YES;
+}
+
+- (void)undo:(id)sender 
+{
+    [self triggerMenu:_cmd];
+}
+
+- (void)redo:(id)sender 
+{
+    [self triggerMenu:_cmd];
+}
+
+- (void)cut:(id)sender 
+{
+    [self triggerMenu:_cmd];
+}
+
+- (void)copy:(id)sender
+{
+    [self triggerMenu:_cmd];
+}
+
+- (void)paste:(id)sender
+{
+    [self triggerMenu:_cmd];
+}
+
+- (void)delete:(id)sender 
+{
+    [self triggerMenu:_cmd];
+}
+
+- (void)selectAll:(id)sender 
+{
+    [self triggerMenu:_cmd];
+}
+
 - (BOOL)windowShouldClose:(id)nwindow
 {
     wxNonOwnedWindowCocoaImpl* windowimpl = [(NSWindow*) nwindow WX_implementation];