]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/NSMenu.mm
Fix calculation of the margins for owner-drawn menu items.
[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
1a393573 18#include "wx/cocoa/ObjcRef.h"
fb896a32 19#include "wx/cocoa/NSMenu.h"
fb896a32 20
bcf01487 21#import <Foundation/NSNotification.h>
ad3628fa 22#include "wx/cocoa/objc/NSMenu.h"
bcf01487 23
fb896a32 24// ============================================================================
829a2e95 25// @class WXNSMenu
fb896a32 26// ============================================================================
fb896a32 27
829a2e95 28@implementation WXNSMenu : NSMenu
fb896a32 29
3312496d
DE
30- (void)dealloc
31{
32 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa(self);
33 if(menu)
34 menu->Cocoa_dealloc();
9a7247f0 35 [super dealloc];
3312496d
DE
36}
37
829a2e95 38@end // WXNSMenu
e7e1ad7d 39WX_IMPLEMENT_GET_OBJC_CLASS(WXNSMenu,NSMenu)
fb896a32 40
bcf01487
DE
41// ============================================================================
42// @class wxNSMenuNotificationObserver
43// ============================================================================
44@interface wxNSMenuNotificationObserver : NSObject
45{
46}
47
bcf01487
DE
48- (void)menuDidAddItem: (NSNotification *)notification;
49- (void)menuDidChangeItem: (NSNotification *)notification;
50- (void)menuDidRemoveItem: (NSNotification *)notification;
51- (void)menuDidSendAction: (NSNotification *)notification;
52- (void)menuWillSendAction: (NSNotification *)notification;
53@end // interface wxNSMenuNotificationObserver
e7e1ad7d 54WX_DECLARE_GET_OBJC_CLASS(wxNSMenuNotificationObserver,NSObject)
bcf01487
DE
55
56@implementation wxNSMenuNotificationObserver : NSObject
57
58- (void)menuDidAddItem: (NSNotification *)notification
59{
60 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
2b030203 61 wxCHECK_RET(menu,wxT("menuDidAddItem received but no wxMenu exists"));
bcf01487
DE
62 menu->CocoaNotification_menuDidAddItem(notification);
63}
64
65- (void)menuDidChangeItem: (NSNotification *)notification
66{
67 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
2b030203 68 wxCHECK_RET(menu,wxT("menuDidChangeItem received but no wxMenu exists"));
bcf01487
DE
69 menu->CocoaNotification_menuDidChangeItem(notification);
70}
71
72- (void)menuDidRemoveItem: (NSNotification *)notification
73{
74 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
2b030203 75 wxCHECK_RET(menu,wxT("menuDidRemoveItem received but no wxMenu exists"));
bcf01487
DE
76 menu->CocoaNotification_menuDidRemoveItem(notification);
77}
78
79- (void)menuDidSendAction: (NSNotification *)notification
80{
81 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
2b030203 82 wxCHECK_RET(menu,wxT("menuDidSendAction received but no wxMenu exists"));
bcf01487
DE
83 menu->CocoaNotification_menuDidSendAction(notification);
84}
85
86- (void)menuWillSendAction: (NSNotification *)notification
87{
88 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
2b030203 89 wxCHECK_RET(menu,wxT("menuWillSendAction received but no wxMenu exists"));
bcf01487
DE
90 menu->CocoaNotification_menuWillSendAction(notification);
91}
92
93@end // implementation wxNSMenuNotificationObserver
e7e1ad7d 94WX_IMPLEMENT_GET_OBJC_CLASS(wxNSMenuNotificationObserver,NSObject)
bcf01487
DE
95
96// ========================================================================
97// wxCocoaNSMenu
98// ========================================================================
bc8eaeb3
DE
99WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSMenu)
100
1a393573
DE
101// New CF-retained observer (this should have been using wxObjcAutoRefFromAlloc to begin with)
102static wxObjcAutoRefFromAlloc<wxNSMenuNotificationObserver*> s_cocoaNSMenuObserver([[WX_GET_OBJC_CLASS(wxNSMenuNotificationObserver) alloc] init]);
103// For compatibility with old code
104struct objc_object *wxCocoaNSMenu::sm_cocoaObserver = s_cocoaNSMenuObserver;
e7e1ad7d 105
bcf01487
DE
106void wxCocoaNSMenu::AssociateNSMenu(WX_NSMenu cocoaNSMenu, unsigned int flags)
107{
108 if(cocoaNSMenu)
109 {
110 sm_cocoaHash.insert(wxCocoaNSMenuHash::value_type(cocoaNSMenu,this));
111 if(flags&OBSERVE_DidAddItem)
112 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidAddItem:) name:NSMenuDidAddItemNotification object:cocoaNSMenu];
113 if(flags&OBSERVE_DidChangeItem)
114 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidChangeItem:) name:NSMenuDidChangeItemNotification object:cocoaNSMenu];
115 if(flags&OBSERVE_DidRemoveItem)
116 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidRemoveItem:) name:NSMenuDidRemoveItemNotification object:cocoaNSMenu];
117 if(flags&OBSERVE_DidSendAction)
118 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidSendAction:) name:NSMenuDidSendActionNotification object:cocoaNSMenu];
119 if(flags&OBSERVE_WillSendAction)
120 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuWillSendAction:) name:NSMenuWillSendActionNotification object:cocoaNSMenu];
121 }
122}
123
124void wxCocoaNSMenu::DisassociateNSMenu(WX_NSMenu cocoaNSMenu)
125{
126 if(cocoaNSMenu)
127 {
128 sm_cocoaHash.erase(cocoaNSMenu);
129 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidAddItemNotification object:cocoaNSMenu];
130 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidChangeItemNotification object:cocoaNSMenu];
131 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidRemoveItemNotification object:cocoaNSMenu];
132 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidSendActionNotification object:cocoaNSMenu];
133 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuWillSendActionNotification object:cocoaNSMenu];
134 }
135}
136
fb896a32 137#endif // wxUSE_MENUS