1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Eclipse Public License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.eclipse.org/org/documents/epl-v10.php
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* History:
* Original code by the <a href="http://www.simidude.com/blog/2008/macify-a-swt-application-in-a-cross-platform-way/">CarbonUIEnhancer from Agynami</a>
* with the implementation being modified from the <a href="http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.ui.cocoa/src/org/eclipse/ui/internal/cocoa/CocoaUIEnhancer.java">org.eclipse.ui.internal.cocoa.CocoaUIEnhancer</a>,
* then modified by http://www.transparentech.com/opensource/cocoauienhancer to use reflection
* rather than 'link' to SWT cocoa, and finally modified to be usable by the SwtMenuBar project.
*/
package com.android.menubar.internal;
import com.android.menubar.IMenuBarCallback;
import com.android.menubar.IMenuBarEnhancer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.internal.C;
import org.eclipse.swt.internal.Callback;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MenuBarEnhancerCocoa implements IMenuBarEnhancer {
private static final long kAboutMenuItem = 0;
private static final long kPreferencesMenuItem = 2;
// private static final long kServicesMenuItem = 4;
// private static final long kHideApplicationMenuItem = 6;
private static final long kQuitMenuItem = 10;
static long mSelPreferencesMenuItemSelected;
static long mSelAboutMenuItemSelected;
static Callback mProc3Args;
private String mAppName;
/**
* Class invoked via the Callback object to run the about and preferences
* actions.
* <p>
* If you don't use JFace in your application (SWT only), change the
* {@link org.eclipse.jface.action.IAction}s to
* {@link org.eclipse.swt.widgets.Listener}s.
* </p>
*/
private static class ActionProctarget {
private final IMenuBarCallback mCallbacks;
public ActionProctarget(IMenuBarCallback callbacks) {
mCallbacks = callbacks;
}
/**
* Will be called on 32bit SWT.
*/
@SuppressWarnings("unused")
public int actionProc(int id, int sel, int arg0) {
return (int) actionProc((long) id, (long) sel, (long) arg0);
}
/**
* Will be called on 64bit SWT.
*/
public long actionProc(long id, long sel, long arg0) {
if (sel == mSelAboutMenuItemSelected) {
mCallbacks.onAboutMenuSelected();
} else if (sel == mSelPreferencesMenuItemSelected) {
mCallbacks.onPreferencesMenuSelected();
} else {
// Unknown selection!
}
// Return value is not used.
return 0;
}
}
/**
* Construct a new CocoaUIEnhancer.
*
* @param mAppName The name of the application. It will be used to customize
* the About and Quit menu items. If you do not wish to customize
* the About and Quit menu items, just pass <tt>null</tt> here.
*/
public MenuBarEnhancerCocoa() {
}
public MenuBarMode getMenuBarMode() {
return MenuBarMode.MAC_OS;
}
/**
* Setup the About and Preferences native menut items with the
* given application name and links them to the callback.
*
* @param appName The application name.
* @param swtMenu The tools menu. Not used here.
* @param callbacks The callbacks invoked by the menus.
*/
public void setupMenu(
String appName,
Menu swtMenu,
IMenuBarCallback callbacks) {
mAppName = appName;
final Display display = swtMenu.getDisplay();
// This is our callback object whose 'actionProc' method will be called
// when the About or Preferences menuItem is invoked.
ActionProctarget target = new ActionProctarget(callbacks);
try {
// Initialize the menuItems.
initialize(target);
} catch (Exception e) {
throw new IllegalStateException(e);
}
// Schedule disposal of callback object
display.disposeExec(new Runnable() {
public void run() {
invoke(mProc3Args, "dispose");
}
});
}
private void initialize(Object callbackObject)
throws Exception {
Class<?> osCls = classForName("org.eclipse.swt.internal.cocoa.OS");
// Register names in objective-c.
if (mSelAboutMenuItemSelected == 0) {
mSelPreferencesMenuItemSelected = registerName(osCls, "preferencesMenuItemSelected:"); //$NON-NLS-1$
mSelAboutMenuItemSelected = registerName(osCls, "aboutMenuItemSelected:"); //$NON-NLS-1$
}
// Create an SWT Callback object that will invoke the actionProc method
// of our internal callback Object.
mProc3Args = new Callback(callbackObject, "actionProc", 3); //$NON-NLS-1$
Method getAddress = Callback.class.getMethod("getAddress", new Class[0]);
Object object = getAddress.invoke(mProc3Args, (Object[]) null);
long proc3 = convertToLong(object);
if (proc3 == 0) {
SWT.error(SWT.ERROR_NO_MORE_CALLBACKS);
}
Class<?> nsMenuCls = classForName("org.eclipse.swt.internal.cocoa.NSMenu");
Class<?> nsMenuitemCls = classForName("org.eclipse.swt.internal.cocoa.NSMenuItem");
Class<?> nsStringCls = classForName("org.eclipse.swt.internal.cocoa.NSString");
Class<?> nsApplicationCls = classForName("org.eclipse.swt.internal.cocoa.NSApplication");
// Instead of creating a new delegate class in objective-c,
// just use the current SWTApplicationDelegate. An instance of this
// is a field of the Cocoa Display object and is already the target
// for the menuItems. So just get this class and add the new methods
// to it.
object = invoke(osCls, "objc_lookUpClass", new Object[] {
"SWTApplicationDelegate"
});
long cls = convertToLong(object);
// Add the action callbacks for Preferences and About menu items.
invoke(osCls, "class_addMethod",
new Object[] {
wrapPointer(cls),
wrapPointer(mSelPreferencesMenuItemSelected),
wrapPointer(proc3), "@:@"}); //$NON-NLS-1$
invoke(osCls, "class_addMethod",
new Object[] {
wrapPointer(cls),
wrapPointer(mSelAboutMenuItemSelected),
wrapPointer(proc3), "@:@"}); //$NON-NLS-1$
// Get the Mac OS X Application menu.
Object sharedApplication = invoke(nsApplicationCls, "sharedApplication");
Object mainMenu = invoke(sharedApplication, "mainMenu");
Object mainMenuItem = invoke(nsMenuCls, mainMenu, "itemAtIndex", new Object[] {
wrapPointer(0)
});
Object appMenu = invoke(mainMenuItem, "submenu");
// Create the About <application-name> menu command
Object aboutMenuItem =
invoke(nsMenuCls, appMenu, "itemAtIndex", new Object[] {
wrapPointer(kAboutMenuItem)
});
if (mAppName != null) {
Object nsStr = invoke(nsStringCls, "stringWith", new Object[] {
"About " + mAppName
});
invoke(nsMenuitemCls, aboutMenuItem, "setTitle", new Object[] {
nsStr
});
}
// Rename the quit action.
if (mAppName != null) {
Object quitMenuItem =
invoke(nsMenuCls, appMenu, "itemAtIndex", new Object[] {
wrapPointer(kQuitMenuItem)
});
Object nsStr = invoke(nsStringCls, "stringWith", new Object[] {
"Quit " + mAppName
});
invoke(nsMenuitemCls, quitMenuItem, "setTitle", new Object[] {
nsStr
});
}
// Enable the Preferences menuItem.
Object prefMenuItem =
invoke(nsMenuCls, appMenu, "itemAtIndex", new Object[] {
wrapPointer(kPreferencesMenuItem)
});
invoke(nsMenuitemCls, prefMenuItem, "setEnabled", new Object[] {
true
});
// Set the action to execute when the About or Preferences menuItem is
// invoked.
//
// We don't need to set the target here as the current target is the
// SWTApplicationDelegate and we have registerd the new selectors on
// it. So just set the new action to invoke the selector.
invoke(nsMenuitemCls, prefMenuItem, "setAction",
new Object[] {
wrapPointer(mSelPreferencesMenuItemSelected)
});
invoke(nsMenuitemCls, aboutMenuItem, "setAction",
new Object[] {
wrapPointer(mSelAboutMenuItemSelected)
});
}
private long registerName(Class<?> osCls, String name)
throws IllegalArgumentException, SecurityException, IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
Object object = invoke(osCls, "sel_registerName", new Object[] {
name
});
return convertToLong(object);
}
private long convertToLong(Object object) {
if (object instanceof Integer) {
Integer i = (Integer) object;
return i.longValue();
}
if (object instanceof Long) {
Long l = (Long) object;
return l.longValue();
}
return 0;
}
private static Object wrapPointer(long value) {
Class<?> PTR_CLASS = C.PTR_SIZEOF == 8 ? long.class : int.class;
if (PTR_CLASS == long.class) {
return new Long(value);
} else {
return new Integer((int) value);
}
}
private static Object invoke(Class<?> clazz, String methodName, Object[] args) {
return invoke(clazz, null, methodName, args);
}
private static Object invoke(Class<?> clazz, Object target, String methodName, Object[] args) {
try {
Class<?>[] signature = new Class<?>[args.length];
for (int i = 0; i < args.length; i++) {
Class<?> thisClass = args[i].getClass();
if (thisClass == Integer.class)
signature[i] = int.class;
else if (thisClass == Long.class)
signature[i] = long.class;
else if (thisClass == Byte.class)
signature[i] = byte.class;
else if (thisClass == Boolean.class)
signature[i] = boolean.class;
else
signature[i] = thisClass;
}
Method method = clazz.getMethod(methodName, signature);
return method.invoke(target, args);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
private Class<?> classForName(String classname) {
try {
Class<?> cls = Class.forName(classname);
return cls;
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
}
private Object invoke(Class<?> cls, String methodName) {
return invoke(cls, methodName, (Class<?>[]) null, (Object[]) null);
}
private Object invoke(Class<?> cls, String methodName, Class<?>[] paramTypes,
Object... arguments) {
try {
Method m = cls.getDeclaredMethod(methodName, paramTypes);
return m.invoke(null, arguments);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
private Object invoke(Object obj, String methodName) {
return invoke(obj, methodName, (Class<?>[]) null, (Object[]) null);
}
private Object invoke(Object obj, String methodName, Class<?>[] paramTypes, Object... arguments) {
try {
Method m = obj.getClass().getDeclaredMethod(methodName, paramTypes);
return m.invoke(obj, arguments);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
|