#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 ;
}
}
wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer, wxMenu *pParentMenu,
- int WXUNUSED(id),
+ int menuid,
const wxString& text,
wxAcceleratorEntry *entry,
const wxString& WXUNUSED(strHelp),
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;
@end
+extern int wxOSXGetIdFromSelector(SEL action );
+
@implementation wxNonOwnedWindowController
- (id) init
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];