]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/NSMenu.mm
40226cd5a2b7c2736dd6f678c8197d5554cd47c3
[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/NSMenu.h"
19 #include "wx/cocoa/ObjcPose.h"
20
21 #import <Foundation/NSNotification.h>
22 #include "wx/cocoa/objc/NSMenu.h"
23
24 // ============================================================================
25 // @class wxPoserNSMenu
26 // ============================================================================
27
28 WX_IMPLEMENT_POSER(wxPoserNSMenu);
29 @implementation wxPoserNSMenu : NSMenu
30
31 - (void)dealloc
32 {
33 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa(self);
34 if(menu)
35 menu->Cocoa_dealloc();
36 [super dealloc];
37 }
38
39 @end // wxPoserNSMenu
40
41 // ============================================================================
42 // @class wxNSMenuNotificationObserver
43 // ============================================================================
44 @interface wxNSMenuNotificationObserver : NSObject
45 {
46 }
47
48 struct objc_object *wxCocoaNSMenu::sm_cocoaObserver = [[wxNSMenuNotificationObserver alloc] init];
49
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
56
57 @implementation wxNSMenuNotificationObserver : NSObject
58
59 - (void)menuDidAddItem: (NSNotification *)notification
60 {
61 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
62 wxCHECK_RET(menu,wxT("menuDidAddItem received but no wxMenu exists"));
63 menu->CocoaNotification_menuDidAddItem(notification);
64 }
65
66 - (void)menuDidChangeItem: (NSNotification *)notification
67 {
68 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
69 wxCHECK_RET(menu,wxT("menuDidChangeItem received but no wxMenu exists"));
70 menu->CocoaNotification_menuDidChangeItem(notification);
71 }
72
73 - (void)menuDidRemoveItem: (NSNotification *)notification
74 {
75 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
76 wxCHECK_RET(menu,wxT("menuDidRemoveItem received but no wxMenu exists"));
77 menu->CocoaNotification_menuDidRemoveItem(notification);
78 }
79
80 - (void)menuDidSendAction: (NSNotification *)notification
81 {
82 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
83 wxCHECK_RET(menu,wxT("menuDidSendAction received but no wxMenu exists"));
84 menu->CocoaNotification_menuDidSendAction(notification);
85 }
86
87 - (void)menuWillSendAction: (NSNotification *)notification
88 {
89 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
90 wxCHECK_RET(menu,wxT("menuWillSendAction received but no wxMenu exists"));
91 menu->CocoaNotification_menuWillSendAction(notification);
92 }
93
94 @end // implementation wxNSMenuNotificationObserver
95
96 // ========================================================================
97 // wxCocoaNSMenu
98 // ========================================================================
99 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSMenu)
100
101 void wxCocoaNSMenu::AssociateNSMenu(WX_NSMenu cocoaNSMenu, unsigned int flags)
102 {
103 if(cocoaNSMenu)
104 {
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];
116 }
117 }
118
119 void wxCocoaNSMenu::DisassociateNSMenu(WX_NSMenu cocoaNSMenu)
120 {
121 if(cocoaNSMenu)
122 {
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];
129 }
130 }
131
132 #endif // wxUSE_MENUS