]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/NSMenu.mm
adapting to new sound file organization for osx
[wxWidgets.git] / src / cocoa / NSMenu.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/NSMenu.mm
3 // Purpose: wxCocoaNSMenu implementation
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2002/12/09
7 // RCS-ID: $Id$
8 // Copyright: (c) 2002 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13 #if wxUSE_MENUS
14 #ifndef WX_PRECOMP
15 #include "wx/log.h"
16 #endif // WX_PRECOMP
17
18 #include "wx/cocoa/ObjcRef.h"
19 #include "wx/cocoa/NSMenu.h"
20
21 #import <Foundation/NSNotification.h>
22 #include "wx/cocoa/objc/NSMenu.h"
23
24 // ============================================================================
25 // @class WXNSMenu
26 // ============================================================================
27
28 @implementation WXNSMenu : NSMenu
29
30 - (void)dealloc
31 {
32 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa(self);
33 if(menu)
34 menu->Cocoa_dealloc();
35 [super dealloc];
36 }
37
38 @end // WXNSMenu
39 WX_IMPLEMENT_GET_OBJC_CLASS(WXNSMenu,NSMenu)
40
41 // ============================================================================
42 // @class wxNSMenuNotificationObserver
43 // ============================================================================
44 @interface wxNSMenuNotificationObserver : NSObject
45 {
46 }
47
48 - (void)menuDidAddItem: (NSNotification *)notification;
49 - (void)menuDidChangeItem: (NSNotification *)notification;
50 - (void)menuDidRemoveItem: (NSNotification *)notification;
51 - (void)menuDidSendAction: (NSNotification *)notification;
52 - (void)menuWillSendAction: (NSNotification *)notification;
53 @end // interface wxNSMenuNotificationObserver
54 WX_DECLARE_GET_OBJC_CLASS(wxNSMenuNotificationObserver,NSObject)
55
56 @implementation wxNSMenuNotificationObserver : NSObject
57
58 - (void)menuDidAddItem: (NSNotification *)notification
59 {
60 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
61 wxCHECK_RET(menu,wxT("menuDidAddItem received but no wxMenu exists"));
62 menu->CocoaNotification_menuDidAddItem(notification);
63 }
64
65 - (void)menuDidChangeItem: (NSNotification *)notification
66 {
67 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
68 wxCHECK_RET(menu,wxT("menuDidChangeItem received but no wxMenu exists"));
69 menu->CocoaNotification_menuDidChangeItem(notification);
70 }
71
72 - (void)menuDidRemoveItem: (NSNotification *)notification
73 {
74 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
75 wxCHECK_RET(menu,wxT("menuDidRemoveItem received but no wxMenu exists"));
76 menu->CocoaNotification_menuDidRemoveItem(notification);
77 }
78
79 - (void)menuDidSendAction: (NSNotification *)notification
80 {
81 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
82 wxCHECK_RET(menu,wxT("menuDidSendAction received but no wxMenu exists"));
83 menu->CocoaNotification_menuDidSendAction(notification);
84 }
85
86 - (void)menuWillSendAction: (NSNotification *)notification
87 {
88 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
89 wxCHECK_RET(menu,wxT("menuWillSendAction received but no wxMenu exists"));
90 menu->CocoaNotification_menuWillSendAction(notification);
91 }
92
93 @end // implementation wxNSMenuNotificationObserver
94 WX_IMPLEMENT_GET_OBJC_CLASS(wxNSMenuNotificationObserver,NSObject)
95
96 // ========================================================================
97 // wxCocoaNSMenu
98 // ========================================================================
99 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSMenu)
100
101 // New CF-retained observer (this should have been using wxObjcAutoRefFromAlloc to begin with)
102 static wxObjcAutoRefFromAlloc<wxNSMenuNotificationObserver*> s_cocoaNSMenuObserver([[WX_GET_OBJC_CLASS(wxNSMenuNotificationObserver) alloc] init]);
103 // For compatibility with old code
104 struct objc_object *wxCocoaNSMenu::sm_cocoaObserver = s_cocoaNSMenuObserver;
105
106 void wxCocoaNSMenu::AssociateNSMenu(WX_NSMenu cocoaNSMenu, unsigned int flags)
107 {
108 if(cocoaNSMenu)
109 {
110 sm_cocoaHash.insert(wxCocoaNSMenuHash::value_type(cocoaNSMenu,this));
111 if(flags&OBSERVE_DidAddItem)
112 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidAddItem:) name:NSMenuDidAddItemNotification object:cocoaNSMenu];
113 if(flags&OBSERVE_DidChangeItem)
114 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidChangeItem:) name:NSMenuDidChangeItemNotification object:cocoaNSMenu];
115 if(flags&OBSERVE_DidRemoveItem)
116 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidRemoveItem:) name:NSMenuDidRemoveItemNotification object:cocoaNSMenu];
117 if(flags&OBSERVE_DidSendAction)
118 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidSendAction:) name:NSMenuDidSendActionNotification object:cocoaNSMenu];
119 if(flags&OBSERVE_WillSendAction)
120 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuWillSendAction:) name:NSMenuWillSendActionNotification object:cocoaNSMenu];
121 }
122 }
123
124 void wxCocoaNSMenu::DisassociateNSMenu(WX_NSMenu cocoaNSMenu)
125 {
126 if(cocoaNSMenu)
127 {
128 sm_cocoaHash.erase(cocoaNSMenu);
129 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidAddItemNotification object:cocoaNSMenu];
130 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidChangeItemNotification object:cocoaNSMenu];
131 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidRemoveItemNotification object:cocoaNSMenu];
132 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidSendActionNotification object:cocoaNSMenu];
133 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuWillSendActionNotification object:cocoaNSMenu];
134 }
135 }
136
137 #endif // wxUSE_MENUS