]>
Commit | Line | Data |
---|---|---|
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 | |
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 | ||
dbeddfb9 SC |
37 | @implementation wxNSMenu |
38 | ||
39 | - (id) init | |
40 | { | |
41 | [super init]; | |
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 | ||
7c17cb8c SC |
58 | @interface wxNSMenuController : NSObject |
59 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6 | |
60 | <NSMenuDelegate> | |
61 | #endif | |
dbeddfb9 SC |
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(); | |
3fe48198 SC |
86 | if ( wxpeer ) |
87 | wxpeer->HandleMenuOpened(); | |
dbeddfb9 SC |
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(); | |
3fe48198 SC |
98 | if ( wxpeer ) |
99 | wxpeer->HandleMenuClosed(); | |
dbeddfb9 SC |
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 | ||
03647350 VZ |
123 | @interface NSApplication(MissingAppleMenuCall) |
124 | - (void)setAppleMenu:(NSMenu *)menu; | |
125 | @end | |
ffad7b0d | 126 | |
03647350 | 127 | class wxMenuCocoaImpl : public wxMenuImpl |
dbeddfb9 SC |
128 | { |
129 | public : | |
be136f07 | 130 | wxMenuCocoaImpl( wxMenu* peer , wxNSMenu* menu) : wxMenuImpl(peer), m_osxMenu(menu) |
dbeddfb9 | 131 | { |
be136f07 SC |
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]; | |
9a038ddc SC |
139 | // gc aware |
140 | if ( m_osxMenu ) | |
141 | CFRetain(m_osxMenu); | |
142 | [m_osxMenu release]; | |
dbeddfb9 | 143 | } |
03647350 | 144 | |
dbeddfb9 | 145 | virtual ~wxMenuCocoaImpl(); |
03647350 VZ |
146 | |
147 | virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos) | |
dbeddfb9 SC |
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 | } | |
03647350 VZ |
154 | |
155 | virtual void Remove( wxMenuItem *pItem ) | |
dbeddfb9 SC |
156 | { |
157 | [m_osxMenu removeItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem()]; | |
158 | } | |
03647350 | 159 | |
dbeddfb9 SC |
160 | virtual void MakeRoot() |
161 | { | |
162 | [NSApp setMainMenu:m_osxMenu]; | |
163 | [NSApp setAppleMenu:[[m_osxMenu itemAtIndex:0] submenu]]; | |
164 | } | |
165 | ||
d8207702 | 166 | virtual void Enable( bool WXUNUSED(enable) ) |
dbeddfb9 SC |
167 | { |
168 | } | |
03647350 | 169 | |
dbeddfb9 SC |
170 | virtual void SetTitle( const wxString& text ) |
171 | { | |
172 | wxCFStringRef cfText(text); | |
173 | [m_osxMenu setTitle:cfText.AsNSString()]; | |
174 | } | |
175 | ||
2cb5d2d2 SC |
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 | ||
dbeddfb9 SC |
194 | WXHMENU GetHMenu() { return m_osxMenu; } |
195 | ||
196 | static wxMenuImpl* Create( wxMenu* peer, const wxString& title ); | |
197 | static wxMenuImpl* CreateRootMenu( wxMenu* peer ); | |
198 | protected : | |
be136f07 | 199 | wxNSMenu* m_osxMenu; |
dbeddfb9 SC |
200 | } ; |
201 | ||
202 | wxMenuCocoaImpl::~wxMenuCocoaImpl() | |
203 | { | |
42c2b729 | 204 | [m_osxMenu setDelegate:nil]; |
be136f07 | 205 | [m_osxMenu setImplementation:nil]; |
9a038ddc SC |
206 | // gc aware |
207 | if ( m_osxMenu ) | |
208 | CFRelease(m_osxMenu); | |
dbeddfb9 SC |
209 | } |
210 | ||
211 | wxMenuImpl* wxMenuImpl::Create( wxMenu* peer, const wxString& title ) | |
212 | { | |
dbeddfb9 SC |
213 | wxCFStringRef cfText( title ); |
214 | wxNSMenu* menu = [[wxNSMenu alloc] initWithTitle:cfText.AsNSString()]; | |
215 | wxMenuImpl* c = new wxMenuCocoaImpl( peer, menu ); | |
dbeddfb9 SC |
216 | return c; |
217 | } |