]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/menu.mm
Big wxDataViewCtrl renderer classes refactoring.
[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: menu.cpp 54129 2008-06-11 19:30:52Z 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
21 #include "wx/menu.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/log.h"
25 #include "wx/app.h"
26 #include "wx/utils.h"
27 #include "wx/frame.h"
28 #include "wx/menuitem.h"
29 #endif
30
31 #include "wx/osx/private.h"
32
33 // other standard headers
34 // ----------------------
35 #include <string.h>
36
37 @implementation wxNSMenu
38
39 - (id) init
40 {
41 [super init];
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 @interface wxNSMenuController : NSObject
59 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
60 <NSMenuDelegate>
61 #endif
62 {
63 }
64
65 - (void)menuWillOpen:(NSMenu *)menu;
66 - (void)menuDidClose:(NSMenu *)menu;
67 - (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item;
68
69 @end
70
71 @implementation wxNSMenuController
72
73 - (id) init
74 {
75 [super init];
76 return self;
77 }
78
79 - (void)menuWillOpen:(NSMenu *)smenu
80 {
81 wxNSMenu* menu = (wxNSMenu*) smenu;
82 wxMenuImpl* menuimpl = [menu implementation];
83 if ( menuimpl )
84 {
85 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
86 if ( wxpeer )
87 wxpeer->HandleMenuOpened();
88 }
89 }
90
91 - (void)menuDidClose:(NSMenu *)smenu
92 {
93 wxNSMenu* menu = (wxNSMenu*) smenu;
94 wxMenuImpl* menuimpl = [menu implementation];
95 if ( menuimpl )
96 {
97 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
98 if ( wxpeer )
99 wxpeer->HandleMenuClosed();
100 }
101 }
102
103 - (void)menu:(NSMenu *)smenu willHighlightItem:(NSMenuItem *)item
104 {
105 wxNSMenu* menu = (wxNSMenu*) smenu;
106 wxMenuImpl* menuimpl = [menu implementation];
107 if ( menuimpl )
108 {
109 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
110 if ( [ item isKindOfClass:[wxNSMenuItem class] ] )
111 {
112 wxMenuItemImpl* menuitemimpl = (wxMenuItemImpl*) [ (wxNSMenuItem*) item implementation ];
113 if ( wxpeer && menuitemimpl )
114 {
115 wxpeer->HandleMenuItemHighlighted( menuitemimpl->GetWXPeer() );
116 }
117 }
118 }
119 }
120
121 @end
122
123 @interface NSApplication(MissingAppleMenuCall)
124 - (void)setAppleMenu:(NSMenu *)menu;
125 @end
126
127 class wxMenuCocoaImpl : public wxMenuImpl
128 {
129 public :
130 wxMenuCocoaImpl( wxMenu* peer , wxNSMenu* menu) : wxMenuImpl(peer), m_osxMenu(menu)
131 {
132 static wxNSMenuController* controller = NULL;
133 if ( controller == NULL )
134 {
135 controller = [[wxNSMenuController alloc] init];
136 }
137 [menu setDelegate:controller];
138 [m_osxMenu setImplementation:this];
139 // gc aware
140 if ( m_osxMenu )
141 CFRetain(m_osxMenu);
142 [m_osxMenu release];
143 }
144
145 virtual ~wxMenuCocoaImpl();
146
147 virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos)
148 {
149 if ( pos == (size_t) -1 )
150 [m_osxMenu addItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() ];
151 else
152 [m_osxMenu insertItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() atIndex:pos];
153 }
154
155 virtual void Remove( wxMenuItem *pItem )
156 {
157 [m_osxMenu removeItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem()];
158 }
159
160 virtual void MakeRoot()
161 {
162 [NSApp setMainMenu:m_osxMenu];
163 [NSApp setAppleMenu:[[m_osxMenu itemAtIndex:0] submenu]];
164 }
165
166 virtual void Enable( bool WXUNUSED(enable) )
167 {
168 }
169
170 virtual void SetTitle( const wxString& text )
171 {
172 wxCFStringRef cfText(text);
173 [m_osxMenu setTitle:cfText.AsNSString()];
174 }
175
176 virtual void PopUp( wxWindow *win, int x, int y )
177 {
178 win->ScreenToClient( &x , &y ) ;
179 NSView *view = win->GetPeer()->GetWXWidget();
180 NSRect frame = [view frame];
181 frame.origin.x = x;
182 frame.origin.y = y;
183 frame.size.width = 1;
184 frame.size.height = 1;
185 NSPopUpButtonCell *popUpButtonCell = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO];
186 [popUpButtonCell setAutoenablesItems:NO];
187 [popUpButtonCell setAltersStateOfSelectedItem:NO];
188 [popUpButtonCell setMenu:m_osxMenu];
189 [popUpButtonCell selectItem:nil];
190 [popUpButtonCell performClickWithFrame:frame inView:view];
191 [popUpButtonCell release];
192 }
193
194 WXHMENU GetHMenu() { return m_osxMenu; }
195
196 static wxMenuImpl* Create( wxMenu* peer, const wxString& title );
197 static wxMenuImpl* CreateRootMenu( wxMenu* peer );
198 protected :
199 wxNSMenu* m_osxMenu;
200 } ;
201
202 wxMenuCocoaImpl::~wxMenuCocoaImpl()
203 {
204 [m_osxMenu setDelegate:nil];
205 [m_osxMenu setImplementation:nil];
206 // gc aware
207 if ( m_osxMenu )
208 CFRelease(m_osxMenu);
209 }
210
211 wxMenuImpl* wxMenuImpl::Create( wxMenu* peer, const wxString& title )
212 {
213 wxCFStringRef cfText( title );
214 wxNSMenu* menu = [[wxNSMenu alloc] initWithTitle:cfText.AsNSString()];
215 wxMenuImpl* c = new wxMenuCocoaImpl( peer, menu );
216 return c;
217 }