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