From: David Elliott Date: Thu, 28 Aug 2003 17:49:51 +0000 (+0000) Subject: Send all menu item actions to a dedicated target. This is to ensure X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/2fc2d511d0f095ab5de3713cff6042832f886d15 Send all menu item actions to a dedicated target. This is to ensure the actions always make it to the proper wxFrame. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@23277 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/cocoa/NSWindow.h b/include/wx/cocoa/NSWindow.h index 29748141c9..f67430f868 100644 --- a/include/wx/cocoa/NSWindow.h +++ b/include/wx/cocoa/NSWindow.h @@ -26,7 +26,6 @@ public: void DisassociateNSWindow(WX_NSWindow cocoaNSWindow); virtual void Cocoa_close(void) = 0; virtual bool Cocoa_windowShouldClose(void) = 0; - virtual void Cocoa_wxMenuItemAction(wxMenuItem& item) = 0; virtual void CocoaNotification_DidBecomeKey(void) { } virtual void CocoaNotification_DidResignKey(void) { } protected: diff --git a/include/wx/cocoa/frame.h b/include/wx/cocoa/frame.h index 5190e59c41..041e7b399e 100644 --- a/include/wx/cocoa/frame.h +++ b/include/wx/cocoa/frame.h @@ -13,7 +13,6 @@ #define _WX_COCOA_FRAME_H_ class WXDLLEXPORT wxMenuBar; -class WXDLLEXPORT wxMenuItem; class WXDLLEXPORT wxStatusBar; class WXDLLEXPORT wxFrame: public wxFrameBase @@ -52,7 +51,6 @@ protected: // Cocoa specifics // ------------------------------------------------------------------------ protected: - virtual void Cocoa_wxMenuItemAction(wxMenuItem& item); virtual void CocoaSetWxWindowSize(int width, int height); // Helper function to position status/tool bars diff --git a/include/wx/cocoa/menuitem.h b/include/wx/cocoa/menuitem.h index 303c73b6e7..75e66b1d4c 100644 --- a/include/wx/cocoa/menuitem.h +++ b/include/wx/cocoa/menuitem.h @@ -52,6 +52,7 @@ public: protected: WX_NSMenuItem m_cocoaNSMenuItem; static wxMenuItemCocoaHash sm_cocoaHash; + static struct objc_object *sm_cocoaTarget; // ------------------------------------------------------------------------ // Implementation // ------------------------------------------------------------------------ diff --git a/include/wx/cocoa/toplevel.h b/include/wx/cocoa/toplevel.h index 6283dabdfb..eb8996a4cd 100644 --- a/include/wx/cocoa/toplevel.h +++ b/include/wx/cocoa/toplevel.h @@ -61,7 +61,6 @@ public: inline WX_NSWindow GetNSWindow() { return m_cocoaNSWindow; } virtual void Cocoa_close(void); virtual bool Cocoa_windowShouldClose(void); - virtual void Cocoa_wxMenuItemAction(wxMenuItem& item); virtual void CocoaNotification_DidBecomeKey(void); virtual void CocoaNotification_DidResignKey(void); protected: diff --git a/src/cocoa/NSWindow.mm b/src/cocoa/NSWindow.mm index 15b3eb583e..27d506a30b 100644 --- a/src/cocoa/NSWindow.mm +++ b/src/cocoa/NSWindow.mm @@ -98,7 +98,6 @@ void wxCocoaNSWindow::DisassociateNSWindow(WX_NSWindow cocoaNSWindow) - (void)close; - (BOOL)windowShouldClose: (id)sender; -- (BOOL)wxMenuItemAction: (id)sender; @end // wxPoserNSwindow WX_IMPLEMENT_POSER(wxPoserNSWindow); @@ -125,17 +124,5 @@ WX_IMPLEMENT_POSER(wxPoserNSWindow); return YES; } -- (BOOL)wxMenuItemAction: (id)sender -{ - wxLogDebug("wxMenuItemAction"); - wxMenuItem *item = wxMenuItem::GetFromCocoa(sender); - if(!item) - return NO; - - wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self); - wxASSERT(tlw); - tlw->Cocoa_wxMenuItemAction(*item); - return YES; -} @end // implementation wxPoserNSWindow diff --git a/src/cocoa/frame.mm b/src/cocoa/frame.mm index d4b5fbfa83..3c40bc0f66 100644 --- a/src/cocoa/frame.mm +++ b/src/cocoa/frame.mm @@ -19,8 +19,6 @@ #include "wx/statusbr.h" #endif // WX_PRECOMP -#include "wx/menuitem.h" - #include "wx/cocoa/autorelease.h" #import @@ -57,11 +55,6 @@ wxFrame::~wxFrame() [m_frameNSView release]; } -void wxFrame::Cocoa_wxMenuItemAction(wxMenuItem& item) -{ - Command(item.GetId()); -} - void wxFrame::AttachMenuBar(wxMenuBar *mbar) { wxFrameBase::AttachMenuBar(mbar); diff --git a/src/cocoa/menu.mm b/src/cocoa/menu.mm index f0fc49998a..57db00ae15 100644 --- a/src/cocoa/menu.mm +++ b/src/cocoa/menu.mm @@ -167,10 +167,12 @@ wxString wxMenuBar::GetLabelTop(size_t pos) const void wxMenuBar::Attach(wxFrame *frame) { + wxMenuBarBase::Attach(frame); } void wxMenuBar::Detach() { + wxMenuBarBase::Detach(); } wxSize wxMenuBar::DoGetBestClientSize() const diff --git a/src/cocoa/menuitem.mm b/src/cocoa/menuitem.mm index 5b78821918..ba92f411a8 100644 --- a/src/cocoa/menuitem.mm +++ b/src/cocoa/menuitem.mm @@ -23,6 +23,7 @@ #include "wx/menuitem.h" #include "wx/utils.h" #include "wx/frame.h" + #include "wx/log.h" #endif #include "wx/cocoa/ObjcPose.h" @@ -34,15 +35,41 @@ #if wxUSE_MENUS -// ---------------------------------------------------------------------------- -// globals -// ---------------------------------------------------------------------------- -wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash; - // ---------------------------------------------------------------------------- // functions prototypes // ---------------------------------------------------------------------------- +// ============================================================================ +// @class wxNSMenuItemTarget +// ============================================================================ +@interface wxNSMenuItemTarget : NSObject +{ +} + +- (void)wxMenuItemAction: (id)sender; +@end //interface wxNSMenuItemTarget + +@implementation wxNSMenuItemTarget : NSObject + +- (void)wxMenuItemAction: (id)sender +{ + wxLogDebug("wxMenuItemAction"); + wxMenuItem *item = wxMenuItem::GetFromCocoa(sender); + wxCHECK_RET(item,"wxMenuItemAction received but no wxMenuItem exists!"); + + wxMenu *menu = item->GetMenu(); + wxCHECK_RET(menu,"wxMenuItemAction received but wxMenuItem is not in a wxMenu"); + wxMenuBar *menubar = menu->GetMenuBar(); + if(menubar) + { + wxFrame *frame = menubar->GetFrame(); + wxCHECK_RET(frame, "wxMenuBar MUST be attached to a wxFrame!"); + frame->Command(item->GetId()); + } +} + +@end //implementation wxNSMenuItemTarget + // ============================================================================ // @class wxPoserNSMenuItem // ============================================================================ @@ -61,6 +88,9 @@ WX_IMPLEMENT_POSER(wxPoserNSMenuItem); // wxMenuItemCocoa implementation // ============================================================================ IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject) +wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash; + +struct objc_object *wxMenuItemCocoa::sm_cocoaTarget = [[wxNSMenuItemTarget alloc] init]; // ---------------------------------------------------------------------------- // wxMenuItemBase @@ -97,6 +127,7 @@ wxMenuItemCocoa::wxMenuItemCocoa(wxMenu *pParentMenu, NSString *menuTitle = [[NSString alloc] initWithCString: wxStripMenuCodes(strName).c_str()]; m_cocoaNSMenuItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:@selector(wxMenuItemAction:) keyEquivalent:@""]; sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this)); + [m_cocoaNSMenuItem setTarget:sm_cocoaTarget]; if(pSubMenu) { wxASSERT(pSubMenu->GetNSMenu()); diff --git a/src/cocoa/toplevel.mm b/src/cocoa/toplevel.mm index a3b1398a35..1b5703aa12 100644 --- a/src/cocoa/toplevel.mm +++ b/src/cocoa/toplevel.mm @@ -128,10 +128,6 @@ void wxTopLevelWindowCocoa::CocoaReplaceView(WX_NSView oldView, WX_NSView newVie [m_cocoaNSWindow setContentView:newView]; } -void wxTopLevelWindowCocoa::Cocoa_wxMenuItemAction(wxMenuItem& item) -{ -} - void wxTopLevelWindowCocoa::CocoaNotification_DidBecomeKey(void) { wxLogDebug("wxTopLevelWindowCocoa=%p::CocoaNotification_DidBecomeKey",this);