]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/NSMenu.mm
submenus have to work in these as well
[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 }
44
45 @end // wxPoserNSMenu
46
47 // ============================================================================
48 // @class wxNSMenuNotificationObserver
49 // ============================================================================
50 @interface wxNSMenuNotificationObserver : NSObject
51 {
52 }
53
54 struct objc_object *wxCocoaNSMenu::sm_cocoaObserver = [[wxNSMenuNotificationObserver alloc] init];
55
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
62
63 @implementation wxNSMenuNotificationObserver : NSObject
64
65 - (void)menuDidAddItem: (NSNotification *)notification
66 {
67 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
68 wxCHECK_RET(menu,wxT("menuDidAddItem received but no wxMenu exists"));
69 menu->CocoaNotification_menuDidAddItem(notification);
70 }
71
72 - (void)menuDidChangeItem: (NSNotification *)notification
73 {
74 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
75 wxCHECK_RET(menu,wxT("menuDidChangeItem received but no wxMenu exists"));
76 menu->CocoaNotification_menuDidChangeItem(notification);
77 }
78
79 - (void)menuDidRemoveItem: (NSNotification *)notification
80 {
81 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
82 wxCHECK_RET(menu,wxT("menuDidRemoveItem received but no wxMenu exists"));
83 menu->CocoaNotification_menuDidRemoveItem(notification);
84 }
85
86 - (void)menuDidSendAction: (NSNotification *)notification
87 {
88 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
89 wxCHECK_RET(menu,wxT("menuDidSendAction received but no wxMenu exists"));
90 menu->CocoaNotification_menuDidSendAction(notification);
91 }
92
93 - (void)menuWillSendAction: (NSNotification *)notification
94 {
95 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
96 wxCHECK_RET(menu,wxT("menuWillSendAction received but no wxMenu exists"));
97 menu->CocoaNotification_menuWillSendAction(notification);
98 }
99
100 @end // implementation wxNSMenuNotificationObserver
101
102 // ========================================================================
103 // wxCocoaNSMenu
104 // ========================================================================
105 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSMenu)
106
107 void wxCocoaNSMenu::AssociateNSMenu(WX_NSMenu cocoaNSMenu, unsigned int flags)
108 {
109 if(cocoaNSMenu)
110 {
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];
122 }
123 }
124
125 void wxCocoaNSMenu::DisassociateNSMenu(WX_NSMenu cocoaNSMenu)
126 {
127 if(cocoaNSMenu)
128 {
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];
135 }
136 }
137
138 #endif // wxUSE_MENUS