]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/NSMenu.mm
wx/wxprec.h already includes wx/defs.h (with other minor cleaning).
[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 #import <AppKit/NSMenu.h>
23
24 // ============================================================================
25 // @class wxPoserNSMenu
26 // ============================================================================
27 @interface wxPoserNSMenu : NSMenu
28 {
29 }
30
31 - (void)dealloc;
32
33 @end // wxPoserNSMenu
34
35 WX_IMPLEMENT_POSER(wxPoserNSMenu);
36 @implementation wxPoserNSMenu : NSMenu
37
38 - (void)dealloc
39 {
40 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa(self);
41 if(menu)
42 menu->Cocoa_dealloc();
43 [super dealloc];
44 }
45
46 @end // wxPoserNSMenu
47
48 // ============================================================================
49 // @class wxNSMenuNotificationObserver
50 // ============================================================================
51 @interface wxNSMenuNotificationObserver : NSObject
52 {
53 }
54
55 struct objc_object *wxCocoaNSMenu::sm_cocoaObserver = [[wxNSMenuNotificationObserver alloc] init];
56
57 - (void)menuDidAddItem: (NSNotification *)notification;
58 - (void)menuDidChangeItem: (NSNotification *)notification;
59 - (void)menuDidRemoveItem: (NSNotification *)notification;
60 - (void)menuDidSendAction: (NSNotification *)notification;
61 - (void)menuWillSendAction: (NSNotification *)notification;
62 @end // interface wxNSMenuNotificationObserver
63
64 @implementation wxNSMenuNotificationObserver : NSObject
65
66 - (void)menuDidAddItem: (NSNotification *)notification
67 {
68 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
69 wxCHECK_RET(menu,wxT("menuDidAddItem received but no wxMenu exists"));
70 menu->CocoaNotification_menuDidAddItem(notification);
71 }
72
73 - (void)menuDidChangeItem: (NSNotification *)notification
74 {
75 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
76 wxCHECK_RET(menu,wxT("menuDidChangeItem received but no wxMenu exists"));
77 menu->CocoaNotification_menuDidChangeItem(notification);
78 }
79
80 - (void)menuDidRemoveItem: (NSNotification *)notification
81 {
82 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
83 wxCHECK_RET(menu,wxT("menuDidRemoveItem received but no wxMenu exists"));
84 menu->CocoaNotification_menuDidRemoveItem(notification);
85 }
86
87 - (void)menuDidSendAction: (NSNotification *)notification
88 {
89 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
90 wxCHECK_RET(menu,wxT("menuDidSendAction received but no wxMenu exists"));
91 menu->CocoaNotification_menuDidSendAction(notification);
92 }
93
94 - (void)menuWillSendAction: (NSNotification *)notification
95 {
96 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
97 wxCHECK_RET(menu,wxT("menuWillSendAction received but no wxMenu exists"));
98 menu->CocoaNotification_menuWillSendAction(notification);
99 }
100
101 @end // implementation wxNSMenuNotificationObserver
102
103 // ========================================================================
104 // wxCocoaNSMenu
105 // ========================================================================
106 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSMenu)
107
108 void wxCocoaNSMenu::AssociateNSMenu(WX_NSMenu cocoaNSMenu, unsigned int flags)
109 {
110 if(cocoaNSMenu)
111 {
112 sm_cocoaHash.insert(wxCocoaNSMenuHash::value_type(cocoaNSMenu,this));
113 if(flags&OBSERVE_DidAddItem)
114 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidAddItem:) name:NSMenuDidAddItemNotification object:cocoaNSMenu];
115 if(flags&OBSERVE_DidChangeItem)
116 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidChangeItem:) name:NSMenuDidChangeItemNotification object:cocoaNSMenu];
117 if(flags&OBSERVE_DidRemoveItem)
118 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidRemoveItem:) name:NSMenuDidRemoveItemNotification object:cocoaNSMenu];
119 if(flags&OBSERVE_DidSendAction)
120 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidSendAction:) name:NSMenuDidSendActionNotification object:cocoaNSMenu];
121 if(flags&OBSERVE_WillSendAction)
122 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuWillSendAction:) name:NSMenuWillSendActionNotification object:cocoaNSMenu];
123 }
124 }
125
126 void wxCocoaNSMenu::DisassociateNSMenu(WX_NSMenu cocoaNSMenu)
127 {
128 if(cocoaNSMenu)
129 {
130 sm_cocoaHash.erase(cocoaNSMenu);
131 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidAddItemNotification object:cocoaNSMenu];
132 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidChangeItemNotification object:cocoaNSMenu];
133 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidRemoveItemNotification object:cocoaNSMenu];
134 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidSendActionNotification object:cocoaNSMenu];
135 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuWillSendActionNotification object:cocoaNSMenu];
136 }
137 }
138
139 #endif // wxUSE_MENUS