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 #include "wx/cocoa/objc/NSMenu.h"
24 // ============================================================================
25 // @class wxPoserNSMenu
26 // ============================================================================
28 WX_IMPLEMENT_POSER(wxPoserNSMenu);
29 @implementation wxPoserNSMenu : NSMenu
33 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa(self);
35 menu->Cocoa_dealloc();
41 // ============================================================================
42 // @class wxNSMenuNotificationObserver
43 // ============================================================================
44 @interface wxNSMenuNotificationObserver : NSObject
48 struct objc_object *wxCocoaNSMenu::sm_cocoaObserver = [[wxNSMenuNotificationObserver alloc] init];
50 - (void)menuDidAddItem: (NSNotification *)notification;
51 - (void)menuDidChangeItem: (NSNotification *)notification;
52 - (void)menuDidRemoveItem: (NSNotification *)notification;
53 - (void)menuDidSendAction: (NSNotification *)notification;
54 - (void)menuWillSendAction: (NSNotification *)notification;
55 @end // interface wxNSMenuNotificationObserver
57 @implementation wxNSMenuNotificationObserver : NSObject
59 - (void)menuDidAddItem: (NSNotification *)notification
61 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
62 wxCHECK_RET(menu,wxT("menuDidAddItem received but no wxMenu exists"));
63 menu->CocoaNotification_menuDidAddItem(notification);
66 - (void)menuDidChangeItem: (NSNotification *)notification
68 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
69 wxCHECK_RET(menu,wxT("menuDidChangeItem received but no wxMenu exists"));
70 menu->CocoaNotification_menuDidChangeItem(notification);
73 - (void)menuDidRemoveItem: (NSNotification *)notification
75 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
76 wxCHECK_RET(menu,wxT("menuDidRemoveItem received but no wxMenu exists"));
77 menu->CocoaNotification_menuDidRemoveItem(notification);
80 - (void)menuDidSendAction: (NSNotification *)notification
82 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
83 wxCHECK_RET(menu,wxT("menuDidSendAction received but no wxMenu exists"));
84 menu->CocoaNotification_menuDidSendAction(notification);
87 - (void)menuWillSendAction: (NSNotification *)notification
89 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
90 wxCHECK_RET(menu,wxT("menuWillSendAction received but no wxMenu exists"));
91 menu->CocoaNotification_menuWillSendAction(notification);
94 @end // implementation wxNSMenuNotificationObserver
96 // ========================================================================
98 // ========================================================================
99 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSMenu)
101 void wxCocoaNSMenu::AssociateNSMenu(WX_NSMenu cocoaNSMenu, unsigned int flags)
105 sm_cocoaHash.insert(wxCocoaNSMenuHash::value_type(cocoaNSMenu,this));
106 if(flags&OBSERVE_DidAddItem)
107 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidAddItem:) name:NSMenuDidAddItemNotification object:cocoaNSMenu];
108 if(flags&OBSERVE_DidChangeItem)
109 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidChangeItem:) name:NSMenuDidChangeItemNotification object:cocoaNSMenu];
110 if(flags&OBSERVE_DidRemoveItem)
111 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidRemoveItem:) name:NSMenuDidRemoveItemNotification object:cocoaNSMenu];
112 if(flags&OBSERVE_DidSendAction)
113 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidSendAction:) name:NSMenuDidSendActionNotification object:cocoaNSMenu];
114 if(flags&OBSERVE_WillSendAction)
115 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuWillSendAction:) name:NSMenuWillSendActionNotification object:cocoaNSMenu];
119 void wxCocoaNSMenu::DisassociateNSMenu(WX_NSMenu cocoaNSMenu)
123 sm_cocoaHash.erase(cocoaNSMenu);
124 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidAddItemNotification object:cocoaNSMenu];
125 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidChangeItemNotification object:cocoaNSMenu];
126 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidRemoveItemNotification object:cocoaNSMenu];
127 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidSendActionNotification object:cocoaNSMenu];
128 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuWillSendActionNotification object:cocoaNSMenu];
132 #endif // wxUSE_MENUS