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