]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/menu.mm
adding translation of NSDragOperationGeneric to wxDragCopy fixes #14059
[wxWidgets.git] / src / osx / cocoa / menu.mm
CommitLineData
dbeddfb9
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/cocoa/menu.mm
3// Purpose: wxMenu, wxMenuBar, wxMenuItem
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
a9a4f229 7// RCS-ID: $Id$
dbeddfb9
SC
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// headers & declarations
14// ============================================================================
15
16// wxWidgets headers
17// -----------------
18
19#include "wx/wxprec.h"
20
dbeddfb9 21#ifndef WX_PRECOMP
5f4bed8a
SC
22#include "wx/log.h"
23#include "wx/app.h"
24#include "wx/utils.h"
25#include "wx/frame.h"
26#include "wx/menuitem.h"
dbeddfb9
SC
27#endif
28
5f4bed8a
SC
29#include "wx/menu.h"
30
dbeddfb9
SC
31#include "wx/osx/private.h"
32
33// other standard headers
34// ----------------------
35#include <string.h>
36
dbeddfb9
SC
37@implementation wxNSMenu
38
fbbe829a 39- (id) initWithTitle:(NSString*) title
dbeddfb9 40{
a985b2c8 41 self = [super initWithTitle:title];
be136f07 42 impl = NULL;
dbeddfb9
SC
43 return self;
44}
45
46- (void)setImplementation: (wxMenuImpl *) theImplementation
47{
48 impl = theImplementation;
49}
50
51- (wxMenuImpl*) implementation
52{
53 return impl;
54}
55
56@end
57
5f4bed8a
SC
58// this is more compatible, as it is also called for command-key shortcuts
59// and under 10.4, we are not getting a 'close' event however...
60#define wxOSX_USE_NEEDSUPDATE_HOOK 1
61
030495ec 62@interface wxNSMenuController : NSObject wxOSX_10_6_AND_LATER(<NSMenuDelegate>)
dbeddfb9
SC
63{
64}
65
5f4bed8a
SC
66#if wxOSX_USE_NEEDSUPDATE_HOOK
67- (void)menuNeedsUpdate:(NSMenu*)smenu;
68#else
dbeddfb9
SC
69- (void)menuWillOpen:(NSMenu *)menu;
70- (void)menuDidClose:(NSMenu *)menu;
5f4bed8a 71#endif
dbeddfb9
SC
72- (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item;
73
74@end
75
76@implementation wxNSMenuController
77
78- (id) init
79{
a985b2c8 80 self = [super init];
dbeddfb9
SC
81 return self;
82}
83
5f4bed8a
SC
84#if wxOSX_USE_NEEDSUPDATE_HOOK
85- (void)menuNeedsUpdate:(NSMenu*)smenu
86{
87 wxNSMenu* menu = (wxNSMenu*) smenu;
88 wxMenuImpl* menuimpl = [menu implementation];
89 if ( menuimpl )
90 {
91 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
92 if ( wxpeer )
93 wxpeer->HandleMenuOpened();
94 }
95}
96#else
dbeddfb9
SC
97- (void)menuWillOpen:(NSMenu *)smenu
98{
99 wxNSMenu* menu = (wxNSMenu*) smenu;
100 wxMenuImpl* menuimpl = [menu implementation];
101 if ( menuimpl )
102 {
103 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
3fe48198
SC
104 if ( wxpeer )
105 wxpeer->HandleMenuOpened();
dbeddfb9
SC
106 }
107}
108
109- (void)menuDidClose:(NSMenu *)smenu
110{
111 wxNSMenu* menu = (wxNSMenu*) smenu;
112 wxMenuImpl* menuimpl = [menu implementation];
113 if ( menuimpl )
114 {
115 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
3fe48198
SC
116 if ( wxpeer )
117 wxpeer->HandleMenuClosed();
dbeddfb9
SC
118 }
119}
5f4bed8a 120#endif
dbeddfb9
SC
121
122- (void)menu:(NSMenu *)smenu willHighlightItem:(NSMenuItem *)item
123{
124 wxNSMenu* menu = (wxNSMenu*) smenu;
125 wxMenuImpl* menuimpl = [menu implementation];
126 if ( menuimpl )
127 {
128 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
129 if ( [ item isKindOfClass:[wxNSMenuItem class] ] )
130 {
131 wxMenuItemImpl* menuitemimpl = (wxMenuItemImpl*) [ (wxNSMenuItem*) item implementation ];
132 if ( wxpeer && menuitemimpl )
133 {
134 wxpeer->HandleMenuItemHighlighted( menuitemimpl->GetWXPeer() );
135 }
136 }
137 }
138}
139
140@end
141
03647350
VZ
142@interface NSApplication(MissingAppleMenuCall)
143- (void)setAppleMenu:(NSMenu *)menu;
144@end
ffad7b0d 145
03647350 146class wxMenuCocoaImpl : public wxMenuImpl
dbeddfb9
SC
147{
148public :
be136f07 149 wxMenuCocoaImpl( wxMenu* peer , wxNSMenu* menu) : wxMenuImpl(peer), m_osxMenu(menu)
dbeddfb9 150 {
be136f07
SC
151 static wxNSMenuController* controller = NULL;
152 if ( controller == NULL )
153 {
154 controller = [[wxNSMenuController alloc] init];
155 }
156 [menu setDelegate:controller];
157 [m_osxMenu setImplementation:this];
5f4bed8a 158 [menu setAutoenablesItems:NO];
9a038ddc
SC
159 // gc aware
160 if ( m_osxMenu )
161 CFRetain(m_osxMenu);
162 [m_osxMenu release];
dbeddfb9 163 }
03647350 164
dbeddfb9 165 virtual ~wxMenuCocoaImpl();
03647350
VZ
166
167 virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos)
dbeddfb9 168 {
b7a015cb
SC
169 NSMenuItem* nsmenuitem = (NSMenuItem*) pItem->GetPeer()->GetHMenuItem();
170 // make sure a call of SetSubMenu is also reflected (occuring after Create)
171 // update the native menu item accordingly
172
173 if ( pItem->IsSubMenu() )
174 {
175 wxMenu* wxsubmenu = pItem->GetSubMenu();
176 WXHMENU nssubmenu = wxsubmenu->GetHMenu();
177 if ( [nsmenuitem submenu] != nssubmenu )
178 {
179 wxsubmenu->GetPeer()->SetTitle( pItem->GetItemLabelText() );
180 [nsmenuitem setSubmenu:nssubmenu];
181 }
182 }
183
dbeddfb9 184 if ( pos == (size_t) -1 )
b7a015cb 185 [m_osxMenu addItem:nsmenuitem ];
dbeddfb9 186 else
b7a015cb 187 [m_osxMenu insertItem:nsmenuitem atIndex:pos];
dbeddfb9 188 }
03647350
VZ
189
190 virtual void Remove( wxMenuItem *pItem )
dbeddfb9
SC
191 {
192 [m_osxMenu removeItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem()];
193 }
03647350 194
dbeddfb9
SC
195 virtual void MakeRoot()
196 {
197 [NSApp setMainMenu:m_osxMenu];
198 [NSApp setAppleMenu:[[m_osxMenu itemAtIndex:0] submenu]];
199 }
200
d8207702 201 virtual void Enable( bool WXUNUSED(enable) )
dbeddfb9
SC
202 {
203 }
03647350 204
dbeddfb9
SC
205 virtual void SetTitle( const wxString& text )
206 {
207 wxCFStringRef cfText(text);
208 [m_osxMenu setTitle:cfText.AsNSString()];
209 }
210
2cb5d2d2
SC
211 virtual void PopUp( wxWindow *win, int x, int y )
212 {
213 win->ScreenToClient( &x , &y ) ;
214 NSView *view = win->GetPeer()->GetWXWidget();
215 NSRect frame = [view frame];
216 frame.origin.x = x;
217 frame.origin.y = y;
218 frame.size.width = 1;
219 frame.size.height = 1;
220 NSPopUpButtonCell *popUpButtonCell = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO];
221 [popUpButtonCell setAutoenablesItems:NO];
222 [popUpButtonCell setAltersStateOfSelectedItem:NO];
223 [popUpButtonCell setMenu:m_osxMenu];
224 [popUpButtonCell selectItem:nil];
225 [popUpButtonCell performClickWithFrame:frame inView:view];
226 [popUpButtonCell release];
227 }
228
dbeddfb9
SC
229 WXHMENU GetHMenu() { return m_osxMenu; }
230
231 static wxMenuImpl* Create( wxMenu* peer, const wxString& title );
232 static wxMenuImpl* CreateRootMenu( wxMenu* peer );
233protected :
be136f07 234 wxNSMenu* m_osxMenu;
dbeddfb9
SC
235} ;
236
237wxMenuCocoaImpl::~wxMenuCocoaImpl()
238{
42c2b729 239 [m_osxMenu setDelegate:nil];
be136f07 240 [m_osxMenu setImplementation:nil];
9a038ddc
SC
241 // gc aware
242 if ( m_osxMenu )
243 CFRelease(m_osxMenu);
dbeddfb9
SC
244}
245
246wxMenuImpl* wxMenuImpl::Create( wxMenu* peer, const wxString& title )
247{
dbeddfb9
SC
248 wxCFStringRef cfText( title );
249 wxNSMenu* menu = [[wxNSMenu alloc] initWithTitle:cfText.AsNSString()];
250 wxMenuImpl* c = new wxMenuCocoaImpl( peer, menu );
dbeddfb9
SC
251 return c;
252}