1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/NSMenu.mm
3 // Purpose: wxCocoaNSMenu implementation
4 // Author: David Elliott
8 // Copyright: (c) 2002 David Elliott
9 // Licence: wxWidgets 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 // ============================================================================
25 // @class wxPoserNSMenu
26 // ============================================================================
27 @interface wxPoserNSMenu : NSMenu
35 WX_IMPLEMENT_POSER(wxPoserNSMenu);
36 @implementation wxPoserNSMenu : NSMenu
40 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa(self);
42 menu->Cocoa_dealloc();
47 // ============================================================================
48 // @class wxNSMenuNotificationObserver
49 // ============================================================================
50 @interface wxNSMenuNotificationObserver : NSObject
54 struct objc_object *wxCocoaNSMenu::sm_cocoaObserver = [[wxNSMenuNotificationObserver alloc] init];
56 - (void)menuDidAddItem: (NSNotification *)notification;
57 - (void)menuDidChangeItem: (NSNotification *)notification;
58 - (void)menuDidRemoveItem: (NSNotification *)notification;
59 - (void)menuDidSendAction: (NSNotification *)notification;
60 - (void)menuWillSendAction: (NSNotification *)notification;
61 @end // interface wxNSMenuNotificationObserver
63 @implementation wxNSMenuNotificationObserver : NSObject
65 - (void)menuDidAddItem: (NSNotification *)notification
67 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
68 wxCHECK_RET(menu,wxT("menuDidAddItem received but no wxMenu exists"));
69 menu->CocoaNotification_menuDidAddItem(notification);
72 - (void)menuDidChangeItem: (NSNotification *)notification
74 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
75 wxCHECK_RET(menu,wxT("menuDidChangeItem received but no wxMenu exists"));
76 menu->CocoaNotification_menuDidChangeItem(notification);
79 - (void)menuDidRemoveItem: (NSNotification *)notification
81 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
82 wxCHECK_RET(menu,wxT("menuDidRemoveItem received but no wxMenu exists"));
83 menu->CocoaNotification_menuDidRemoveItem(notification);
86 - (void)menuDidSendAction: (NSNotification *)notification
88 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
89 wxCHECK_RET(menu,wxT("menuDidSendAction received but no wxMenu exists"));
90 menu->CocoaNotification_menuDidSendAction(notification);
93 - (void)menuWillSendAction: (NSNotification *)notification
95 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
96 wxCHECK_RET(menu,wxT("menuWillSendAction received but no wxMenu exists"));
97 menu->CocoaNotification_menuWillSendAction(notification);
100 @end // implementation wxNSMenuNotificationObserver
102 // ========================================================================
104 // ========================================================================
105 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSMenu)
107 void wxCocoaNSMenu::AssociateNSMenu(WX_NSMenu cocoaNSMenu, unsigned int flags)
111 sm_cocoaHash.insert(wxCocoaNSMenuHash::value_type(cocoaNSMenu,this));
112 if(flags&OBSERVE_DidAddItem)
113 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidAddItem:) name:NSMenuDidAddItemNotification object:cocoaNSMenu];
114 if(flags&OBSERVE_DidChangeItem)
115 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidChangeItem:) name:NSMenuDidChangeItemNotification object:cocoaNSMenu];
116 if(flags&OBSERVE_DidRemoveItem)
117 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidRemoveItem:) name:NSMenuDidRemoveItemNotification object:cocoaNSMenu];
118 if(flags&OBSERVE_DidSendAction)
119 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidSendAction:) name:NSMenuDidSendActionNotification object:cocoaNSMenu];
120 if(flags&OBSERVE_WillSendAction)
121 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuWillSendAction:) name:NSMenuWillSendActionNotification object:cocoaNSMenu];
125 void wxCocoaNSMenu::DisassociateNSMenu(WX_NSMenu cocoaNSMenu)
129 sm_cocoaHash.erase(cocoaNSMenu);
130 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidAddItemNotification object:cocoaNSMenu];
131 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidChangeItemNotification object:cocoaNSMenu];
132 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidRemoveItemNotification object:cocoaNSMenu];
133 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidSendActionNotification object:cocoaNSMenu];
134 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuWillSendActionNotification object:cocoaNSMenu];
138 #endif // wxUSE_MENUS