1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/NSMenu.mm
3 // Purpose: wxCocoaNSMenu implementation
4 // Author: David Elliott
8 // Copyright: (c) 2002 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #include "wx/cocoa/NSMenu.h"
19 //#include "wx/cocoa/ObjcPose.h"
21 #import <Foundation/NSNotification.h>
22 #import <AppKit/NSMenu.h>
24 #if 0 // There is no reason to pose for NSMenu at this time.
25 // ============================================================================
26 // @class wxPoserNSMenu
27 // ============================================================================
28 @interface wxPoserNSMenu : NSMenu
34 WX_IMPLEMENT_POSER(wxPoserNSMenu);
35 @implementation wxPoserNSMenu : NSMenu
40 // ============================================================================
41 // @class wxNSMenuNotificationObserver
42 // ============================================================================
43 @interface wxNSMenuNotificationObserver : NSObject
47 struct objc_object *wxCocoaNSMenu::sm_cocoaObserver = [[wxNSMenuNotificationObserver alloc] init];
49 - (void)menuDidAddItem: (NSNotification *)notification;
50 - (void)menuDidChangeItem: (NSNotification *)notification;
51 - (void)menuDidRemoveItem: (NSNotification *)notification;
52 - (void)menuDidSendAction: (NSNotification *)notification;
53 - (void)menuWillSendAction: (NSNotification *)notification;
54 @end // interface wxNSMenuNotificationObserver
56 @implementation wxNSMenuNotificationObserver : NSObject
58 - (void)menuDidAddItem: (NSNotification *)notification
60 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
61 wxCHECK_RET(menu,wxT("menuDidAddItem received but no wxMenu exists"));
62 menu->CocoaNotification_menuDidAddItem(notification);
65 - (void)menuDidChangeItem: (NSNotification *)notification
67 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
68 wxCHECK_RET(menu,wxT("menuDidChangeItem received but no wxMenu exists"));
69 menu->CocoaNotification_menuDidChangeItem(notification);
72 - (void)menuDidRemoveItem: (NSNotification *)notification
74 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
75 wxCHECK_RET(menu,wxT("menuDidRemoveItem received but no wxMenu exists"));
76 menu->CocoaNotification_menuDidRemoveItem(notification);
79 - (void)menuDidSendAction: (NSNotification *)notification
81 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
82 wxCHECK_RET(menu,wxT("menuDidSendAction received but no wxMenu exists"));
83 menu->CocoaNotification_menuDidSendAction(notification);
86 - (void)menuWillSendAction: (NSNotification *)notification
88 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
89 wxCHECK_RET(menu,wxT("menuWillSendAction received but no wxMenu exists"));
90 menu->CocoaNotification_menuWillSendAction(notification);
93 @end // implementation wxNSMenuNotificationObserver
95 // ========================================================================
97 // ========================================================================
98 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSMenu)
100 void wxCocoaNSMenu::AssociateNSMenu(WX_NSMenu cocoaNSMenu, unsigned int flags)
104 sm_cocoaHash.insert(wxCocoaNSMenuHash::value_type(cocoaNSMenu,this));
105 if(flags&OBSERVE_DidAddItem)
106 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidAddItem:) name:NSMenuDidAddItemNotification object:cocoaNSMenu];
107 if(flags&OBSERVE_DidChangeItem)
108 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidChangeItem:) name:NSMenuDidChangeItemNotification object:cocoaNSMenu];
109 if(flags&OBSERVE_DidRemoveItem)
110 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidRemoveItem:) name:NSMenuDidRemoveItemNotification object:cocoaNSMenu];
111 if(flags&OBSERVE_DidSendAction)
112 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidSendAction:) name:NSMenuDidSendActionNotification object:cocoaNSMenu];
113 if(flags&OBSERVE_WillSendAction)
114 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuWillSendAction:) name:NSMenuWillSendActionNotification object:cocoaNSMenu];
118 void wxCocoaNSMenu::DisassociateNSMenu(WX_NSMenu cocoaNSMenu)
122 sm_cocoaHash.erase(cocoaNSMenu);
123 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidAddItemNotification object:cocoaNSMenu];
124 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidChangeItemNotification object:cocoaNSMenu];
125 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidRemoveItemNotification object:cocoaNSMenu];
126 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidSendActionNotification object:cocoaNSMenu];
127 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuWillSendActionNotification object:cocoaNSMenu];
131 #endif // wxUSE_MENUS