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