]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/NSMenu.mm
renamed wxMotif::wxTLW::DoCreate() to XmDoCreateTLW() to avoid conflicts with
[wxWidgets.git] / src / cocoa / NSMenu.mm
CommitLineData
fb896a32
DE
1/////////////////////////////////////////////////////////////////////////////
2// Name: cocoa/NSMenu.mm
3// Purpose: wxCocoaNSMenu implementation
4// Author: David Elliott
5// Modified by:
6// Created: 2002/12/09
605c7e7e 7// RCS-ID: $Id$
fb896a32 8// Copyright: (c) 2002 David Elliott
065e208e 9// Licence: wxWidgets licence
fb896a32
DE
10/////////////////////////////////////////////////////////////////////////////
11
fb896a32 12#include "wx/wxprec.h"
605c7e7e 13#if wxUSE_MENUS
fb896a32
DE
14#ifndef WX_PRECOMP
15 #include "wx/log.h"
16#endif // WX_PRECOMP
17
18#include "wx/cocoa/NSMenu.h"
3312496d 19#include "wx/cocoa/ObjcPose.h"
fb896a32 20
bcf01487
DE
21#import <Foundation/NSNotification.h>
22#import <AppKit/NSMenu.h>
23
fb896a32
DE
24// ============================================================================
25// @class wxPoserNSMenu
26// ============================================================================
27@interface wxPoserNSMenu : NSMenu
28{
29}
30
3312496d
DE
31- (void)dealloc;
32
fb896a32
DE
33@end // wxPoserNSMenu
34
35WX_IMPLEMENT_POSER(wxPoserNSMenu);
36@implementation wxPoserNSMenu : NSMenu
37
3312496d
DE
38- (void)dealloc
39{
40 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa(self);
41 if(menu)
42 menu->Cocoa_dealloc();
43}
44
fb896a32 45@end // wxPoserNSMenu
fb896a32 46
bcf01487
DE
47// ============================================================================
48// @class wxNSMenuNotificationObserver
49// ============================================================================
50@interface wxNSMenuNotificationObserver : NSObject
51{
52}
53
54struct 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]);
2b030203 68 wxCHECK_RET(menu,wxT("menuDidAddItem received but no wxMenu exists"));
bcf01487
DE
69 menu->CocoaNotification_menuDidAddItem(notification);
70}
71
72- (void)menuDidChangeItem: (NSNotification *)notification
73{
74 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
2b030203 75 wxCHECK_RET(menu,wxT("menuDidChangeItem received but no wxMenu exists"));
bcf01487
DE
76 menu->CocoaNotification_menuDidChangeItem(notification);
77}
78
79- (void)menuDidRemoveItem: (NSNotification *)notification
80{
81 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
2b030203 82 wxCHECK_RET(menu,wxT("menuDidRemoveItem received but no wxMenu exists"));
bcf01487
DE
83 menu->CocoaNotification_menuDidRemoveItem(notification);
84}
85
86- (void)menuDidSendAction: (NSNotification *)notification
87{
88 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
2b030203 89 wxCHECK_RET(menu,wxT("menuDidSendAction received but no wxMenu exists"));
bcf01487
DE
90 menu->CocoaNotification_menuDidSendAction(notification);
91}
92
93- (void)menuWillSendAction: (NSNotification *)notification
94{
95 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
2b030203 96 wxCHECK_RET(menu,wxT("menuWillSendAction received but no wxMenu exists"));
bcf01487
DE
97 menu->CocoaNotification_menuWillSendAction(notification);
98}
99
100@end // implementation wxNSMenuNotificationObserver
101
102// ========================================================================
103// wxCocoaNSMenu
104// ========================================================================
bc8eaeb3
DE
105WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSMenu)
106
bcf01487
DE
107void 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
125void 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
fb896a32 138#endif // wxUSE_MENUS