]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/menu.mm
updated setup.h for OpenVMS
[wxWidgets.git] / src / osx / cocoa / menu.mm
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
7 // RCS-ID: $Id$
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
21 #ifndef WX_PRECOMP
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"
27 #endif
28
29 #include "wx/menu.h"
30
31 #include "wx/osx/private.h"
32
33 // other standard headers
34 // ----------------------
35 #include <string.h>
36
37 @implementation wxNSMenu
38
39 - (id) initWithTitle:(NSString*) title
40 {
41 self = [super initWithTitle:title];
42 impl = NULL;
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
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
62 @interface wxNSMenuController : NSObject wxOSX_10_6_AND_LATER(<NSMenuDelegate>)
63 {
64 }
65
66 #if wxOSX_USE_NEEDSUPDATE_HOOK
67 - (void)menuNeedsUpdate:(NSMenu*)smenu;
68 #else
69 - (void)menuWillOpen:(NSMenu *)menu;
70 - (void)menuDidClose:(NSMenu *)menu;
71 #endif
72 - (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item;
73
74 @end
75
76 @implementation wxNSMenuController
77
78 - (id) init
79 {
80 self = [super init];
81 return self;
82 }
83
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
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();
104 if ( wxpeer )
105 wxpeer->HandleMenuOpened();
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();
116 if ( wxpeer )
117 wxpeer->HandleMenuClosed();
118 }
119 }
120 #endif
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
142 @interface NSApplication(MissingAppleMenuCall)
143 - (void)setAppleMenu:(NSMenu *)menu;
144 @end
145
146 class wxMenuCocoaImpl : public wxMenuImpl
147 {
148 public :
149 wxMenuCocoaImpl( wxMenu* peer , wxNSMenu* menu) : wxMenuImpl(peer), m_osxMenu(menu)
150 {
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];
158 // gc aware
159 if ( m_osxMenu )
160 CFRetain(m_osxMenu);
161 [m_osxMenu release];
162 }
163
164 virtual ~wxMenuCocoaImpl();
165
166 virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos)
167 {
168 NSMenuItem* nsmenuitem = (NSMenuItem*) pItem->GetPeer()->GetHMenuItem();
169 // make sure a call of SetSubMenu is also reflected (occuring after Create)
170 // update the native menu item accordingly
171
172 if ( pItem->IsSubMenu() )
173 {
174 wxMenu* wxsubmenu = pItem->GetSubMenu();
175 WXHMENU nssubmenu = wxsubmenu->GetHMenu();
176 if ( [nsmenuitem submenu] != nssubmenu )
177 {
178 wxsubmenu->GetPeer()->SetTitle( pItem->GetItemLabelText() );
179 [nsmenuitem setSubmenu:nssubmenu];
180 }
181 }
182
183 if ( pos == (size_t) -1 )
184 [m_osxMenu addItem:nsmenuitem ];
185 else
186 [m_osxMenu insertItem:nsmenuitem atIndex:pos];
187 }
188
189 virtual void Remove( wxMenuItem *pItem )
190 {
191 [m_osxMenu removeItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem()];
192 }
193
194 virtual void MakeRoot()
195 {
196 [NSApp setMainMenu:m_osxMenu];
197 [NSApp setAppleMenu:[[m_osxMenu itemAtIndex:0] submenu]];
198 }
199
200 virtual void Enable( bool WXUNUSED(enable) )
201 {
202 }
203
204 virtual void SetTitle( const wxString& text )
205 {
206 wxCFStringRef cfText(text);
207 [m_osxMenu setTitle:cfText.AsNSString()];
208 }
209
210 virtual void PopUp( wxWindow *win, int x, int y )
211 {
212 win->ScreenToClient( &x , &y ) ;
213 NSView *view = win->GetPeer()->GetWXWidget();
214 NSRect frame = [view frame];
215 frame.origin.x = x;
216 frame.origin.y = y;
217 frame.size.width = 1;
218 frame.size.height = 1;
219 NSPopUpButtonCell *popUpButtonCell = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO];
220 [popUpButtonCell setAutoenablesItems:NO];
221 [popUpButtonCell setAltersStateOfSelectedItem:NO];
222 [popUpButtonCell setMenu:m_osxMenu];
223 [popUpButtonCell selectItem:nil];
224 [popUpButtonCell performClickWithFrame:frame inView:view];
225 [popUpButtonCell release];
226 }
227
228 WXHMENU GetHMenu() { return m_osxMenu; }
229
230 static wxMenuImpl* Create( wxMenu* peer, const wxString& title );
231 static wxMenuImpl* CreateRootMenu( wxMenu* peer );
232 protected :
233 wxNSMenu* m_osxMenu;
234 } ;
235
236 wxMenuCocoaImpl::~wxMenuCocoaImpl()
237 {
238 [m_osxMenu setDelegate:nil];
239 [m_osxMenu setImplementation:nil];
240 // gc aware
241 if ( m_osxMenu )
242 CFRelease(m_osxMenu);
243 }
244
245 wxMenuImpl* wxMenuImpl::Create( wxMenu* peer, const wxString& title )
246 {
247 wxCFStringRef cfText( title );
248 wxNSMenu* menu = [[wxNSMenu alloc] initWithTitle:cfText.AsNSString()];
249 wxMenuImpl* c = new wxMenuCocoaImpl( peer, menu );
250 return c;
251 }