]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/NSMenu.mm
default implementations for Replace and Remove
[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();
9a7247f0 43 [super dealloc];
3312496d
DE
44}
45
fb896a32 46@end // wxPoserNSMenu
fb896a32 47
bcf01487
DE
48// ============================================================================
49// @class wxNSMenuNotificationObserver
50// ============================================================================
51@interface wxNSMenuNotificationObserver : NSObject
52{
53}
54
55struct 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]);
2b030203 69 wxCHECK_RET(menu,wxT("menuDidAddItem received but no wxMenu exists"));
bcf01487
DE
70 menu->CocoaNotification_menuDidAddItem(notification);
71}
72
73- (void)menuDidChangeItem: (NSNotification *)notification
74{
75 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
2b030203 76 wxCHECK_RET(menu,wxT("menuDidChangeItem received but no wxMenu exists"));
bcf01487
DE
77 menu->CocoaNotification_menuDidChangeItem(notification);
78}
79
80- (void)menuDidRemoveItem: (NSNotification *)notification
81{
82 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
2b030203 83 wxCHECK_RET(menu,wxT("menuDidRemoveItem received but no wxMenu exists"));
bcf01487
DE
84 menu->CocoaNotification_menuDidRemoveItem(notification);
85}
86
87- (void)menuDidSendAction: (NSNotification *)notification
88{
89 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
2b030203 90 wxCHECK_RET(menu,wxT("menuDidSendAction received but no wxMenu exists"));
bcf01487
DE
91 menu->CocoaNotification_menuDidSendAction(notification);
92}
93
94- (void)menuWillSendAction: (NSNotification *)notification
95{
96 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
2b030203 97 wxCHECK_RET(menu,wxT("menuWillSendAction received but no wxMenu exists"));
bcf01487
DE
98 menu->CocoaNotification_menuWillSendAction(notification);
99}
100
101@end // implementation wxNSMenuNotificationObserver
102
103// ========================================================================
104// wxCocoaNSMenu
105// ========================================================================
bc8eaeb3
DE
106WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSMenu)
107
bcf01487
DE
108void 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
126void 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
fb896a32 139#endif // wxUSE_MENUS