src/macosx/native/sun/awt/CMenu.m

Print this page




  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  27 #import <JavaRuntimeSupport/JavaRuntimeSupport.h>
  28 
  29 
  30 #import "CMenu.h"
  31 #import "CMenuBar.h"
  32 #import "ThreadUtilities.h"
  33 
  34 #import "sun_lwawt_macosx_CMenu.h"
  35 
  36 @implementation CMenu
  37 
  38 - (id)initWithPeer:(jobject)peer {
  39 AWT_ASSERT_APPKIT_THREAD;
  40     // Create the new NSMenu
  41     self = [super initWithPeer:peer asSeparator:[NSNumber numberWithBool:NO]];
  42     if (self) {
  43         fMenu = [NSMenu javaMenuWithTitle:@""];
  44         [fMenu retain];
  45         [fMenu setAutoenablesItems:NO];
  46     }
  47     return self;
  48 }
  49 
  50 - (void)dealloc {
  51     [fMenu release];
  52     fMenu = nil;
  53     [super dealloc];
  54 }
  55 
  56 - (void)addJavaSubmenu:(CMenu *)submenu {
  57     [ThreadUtilities performOnMainThread:@selector(addNativeItem_OnAppKitThread:) on:self withObject:submenu waitUntilDone:YES];
  58 }
  59 
  60 - (void)addJavaMenuItem:(CMenuItem *)theMenuItem {
  61     [ThreadUtilities performOnMainThread:@selector(addNativeItem_OnAppKitThread:) on:self withObject:theMenuItem waitUntilDone:YES];
  62 }
  63 
  64 - (void)addNativeItem_OnAppKitThread:(CMenuItem *)itemModified {
  65 AWT_ASSERT_APPKIT_THREAD;
  66     [itemModified addNSMenuItemToMenu:[self menu]];
  67 }
  68 
  69 - (void)setJavaMenuTitle:(NSString *)title {
  70 
  71     if (title) {
  72         [ThreadUtilities performOnMainThread:@selector(setNativeMenuTitle_OnAppKitThread:) on:self withObject:title waitUntilDone:YES];
  73     }
  74 }
  75 
  76 - (void)setNativeMenuTitle_OnAppKitThread:(NSString *)title {
  77 AWT_ASSERT_APPKIT_THREAD;
  78 
  79     [fMenu setTitle:title];
  80     // If we are a submenu we need to set our name in the parent menu's menu item.
  81     NSMenu *parent = [fMenu supermenu];
  82     if (parent) {
  83         NSInteger index = [parent indexOfItemWithSubmenu:fMenu];
  84         NSMenuItem *menuItem = [parent itemAtIndex:index];
  85         [menuItem setTitle:title];
  86     }
  87 }


 116 
 117 - (void)setNativeEnabled_OnAppKitThread:(NSNumber *)boolNumber {
 118 AWT_ASSERT_APPKIT_THREAD;
 119 
 120     @synchronized(self) {
 121         fIsEnabled = [boolNumber boolValue];
 122 
 123         NSMenu* supermenu = [fMenu supermenu];
 124         [[supermenu itemAtIndex:[supermenu indexOfItemWithSubmenu:fMenu]] setEnabled:fIsEnabled];
 125     }
 126 }
 127 
 128 - (NSString *)description {
 129     return [NSString stringWithFormat:@"CMenu[ %@ ]", fMenu];
 130 }
 131 
 132 @end
 133 
 134 CMenu * createCMenu (jobject cPeerObjGlobal) {
 135 
 136     CMenu *aCMenu = nil;
 137 
 138     // We use an array here only to be able to get a return value
 139     NSMutableArray *args = [[NSMutableArray alloc] initWithObjects:[NSValue valueWithBytes:&cPeerObjGlobal objCType:@encode(jobject)], nil];
 140 
 141     [ThreadUtilities performOnMainThread:@selector(_create_OnAppKitThread:) on:[CMenu alloc] withObject:args waitUntilDone:YES];
 142 
 143     aCMenu = (CMenu *)[args objectAtIndex: 0];
 144 
 145     if (aCMenu == nil) {
 146         return 0L;
 147     }
 148 
 149     return aCMenu;
 150 
 151 }
 152 
 153 /*
 154  * Class:     sun_lwawt_macosx_CMenu
 155  * Method:    nativeCreateSubMenu
 156  * Signature: (J)J
 157  */
 158 JNIEXPORT jlong JNICALL
 159 Java_sun_lwawt_macosx_CMenu_nativeCreateSubMenu
 160 (JNIEnv *env, jobject peer, jlong parentMenu)
 161 {
 162     CMenu *aCMenu = nil;


 163 JNF_COCOA_ENTER(env);
 164 
 165     jobject cPeerObjGlobal = (*env)->NewGlobalRef(env, peer);
 166 
 167     aCMenu = createCMenu (cPeerObjGlobal);
 168 
 169     // Add it to the parent menu
 170     [((CMenu *)jlong_to_ptr(parentMenu)) addJavaSubmenu: aCMenu];
 171 
 172 JNF_COCOA_EXIT(env);
 173 
 174     return ptr_to_jlong(aCMenu);
 175 }
 176 
 177 
 178 
 179 /*
 180  * Class:     sun_lwawt_macosx_CMenu
 181  * Method:    nativeCreateMenu
 182  * Signature: (JZ)J
 183  */
 184 JNIEXPORT jlong JNICALL
 185 Java_sun_lwawt_macosx_CMenu_nativeCreateMenu
 186 (JNIEnv *env, jobject peer,
 187         jlong parentMenuBar, jboolean isHelpMenu, jint insertLocation)
 188 {
 189     CMenu *aCMenu = nil;
 190     CMenuBar *parent = (CMenuBar *)jlong_to_ptr(parentMenuBar);
 191 JNF_COCOA_ENTER(env);
 192 
 193     jobject cPeerObjGlobal = (*env)->NewGlobalRef(env, peer);
 194 
 195     aCMenu = createCMenu (cPeerObjGlobal);

 196 
 197     // Add it to the menu bar.
 198     [parent javaAddMenu:aCMenu atIndex:insertLocation];
 199 
 200     // If the menu is already the help menu (because we are creating an entire
 201     // menu bar) we need to note that now, because we can't rely on
 202     // setHelpMenu() being called again.
 203     if (isHelpMenu == JNI_TRUE) {
 204         [parent javaSetHelpMenu: aCMenu];
 205     }
 206 
 207 JNF_COCOA_EXIT(env);
 208     return ptr_to_jlong(aCMenu);
 209 }
 210 
 211 
 212 /*
 213  * Class:     sun_lwawt_macosx_CMenu
 214  * Method:    nativeSetMenuTitle
 215  * Signature: (JLjava/lang/String;)V




  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  27 #import <JavaRuntimeSupport/JavaRuntimeSupport.h>
  28 
  29 
  30 #import "CMenu.h"
  31 #import "CMenuBar.h"
  32 #import "ThreadUtilities.h"
  33 
  34 #import "sun_lwawt_macosx_CMenu.h"
  35 
  36 @implementation CMenu
  37 
  38 - (id)initWithPeer:(jobject)peer {
  39 AWT_ASSERT_APPKIT_THREAD;
  40     // Create the new NSMenu
  41     self = [super initWithPeer:peer asSeparator:NO];
  42     if (self) {
  43         fMenu = [NSMenu javaMenuWithTitle:@""];
  44         [fMenu retain];
  45         [fMenu setAutoenablesItems:NO];
  46     }
  47     return self;
  48 }
  49 
  50 - (void)dealloc {
  51     [fMenu release];
  52     fMenu = nil;
  53     [super dealloc];
  54 }
  55 












  56 
  57 - (void)setJavaMenuTitle:(NSString *)title {
  58 
  59     if (title) {
  60         [ThreadUtilities performOnMainThread:@selector(setNativeMenuTitle_OnAppKitThread:) on:self withObject:title waitUntilDone:YES];
  61     }
  62 }
  63 
  64 - (void)setNativeMenuTitle_OnAppKitThread:(NSString *)title {
  65 AWT_ASSERT_APPKIT_THREAD;
  66 
  67     [fMenu setTitle:title];
  68     // If we are a submenu we need to set our name in the parent menu's menu item.
  69     NSMenu *parent = [fMenu supermenu];
  70     if (parent) {
  71         NSInteger index = [parent indexOfItemWithSubmenu:fMenu];
  72         NSMenuItem *menuItem = [parent itemAtIndex:index];
  73         [menuItem setTitle:title];
  74     }
  75 }


 104 
 105 - (void)setNativeEnabled_OnAppKitThread:(NSNumber *)boolNumber {
 106 AWT_ASSERT_APPKIT_THREAD;
 107 
 108     @synchronized(self) {
 109         fIsEnabled = [boolNumber boolValue];
 110 
 111         NSMenu* supermenu = [fMenu supermenu];
 112         [[supermenu itemAtIndex:[supermenu indexOfItemWithSubmenu:fMenu]] setEnabled:fIsEnabled];
 113     }
 114 }
 115 
 116 - (NSString *)description {
 117     return [NSString stringWithFormat:@"CMenu[ %@ ]", fMenu];
 118 }
 119 
 120 @end
 121 
 122 CMenu * createCMenu (jobject cPeerObjGlobal) {
 123 
 124     __block CMenu *aCMenu = nil;
 125     [ThreadUtilities performOnMainThreadWaiting:YES block:^() {
 126         aCMenu = [[CMenu alloc] initWithPeer:cPeerObjGlobal];
 127     }];









 128     return aCMenu;
 129 
 130 }
 131 
 132 /*
 133  * Class:     sun_lwawt_macosx_CMenu
 134  * Method:    nativeCreateSubMenu
 135  * Signature: (J)J
 136  */
 137 JNIEXPORT jlong JNICALL
 138 Java_sun_lwawt_macosx_CMenu_nativeCreateSubMenu
 139 (JNIEnv *env, jobject peer, jlong jParentMenu)
 140 {
 141     __block CMenu *aCMenu = nil;
 142     CMenu* parentMenu = (CMenu *)jlong_to_ptr(jParentMenu);
 143 
 144 JNF_COCOA_ENTER(env);
 145 
 146     jobject cPeerObjGlobal = (*env)->NewGlobalRef(env, peer);
 147     [ThreadUtilities performOnMainThreadWaiting:YES block:^() {
 148         aCMenu = [[CMenu alloc] initWithPeer:cPeerObjGlobal];
 149         [aCMenu addNSMenuItemToMenu:[parentMenu menu]];
 150     }];

 151 
 152 JNF_COCOA_EXIT(env);
 153 
 154     return ptr_to_jlong(aCMenu);
 155 }
 156 
 157 
 158 
 159 /*
 160  * Class:     sun_lwawt_macosx_CMenu
 161  * Method:    nativeCreateMenu
 162  * Signature: (JZ)J
 163  */
 164 JNIEXPORT jlong JNICALL
 165 Java_sun_lwawt_macosx_CMenu_nativeCreateMenu
 166 (JNIEnv *env, jobject peer, jlong parentMenuBar, jboolean isHelpMenu, jint insertLocation)

 167 {
 168     __block CMenu *aCMenu = nil;
 169     CMenuBar *parent = (CMenuBar *)jlong_to_ptr(parentMenuBar);
 170 JNF_COCOA_ENTER(env);
 171 
 172     jobject cPeerObjGlobal = (*env)->NewGlobalRef(env, peer);
 173     [ThreadUtilities performOnMainThreadWaiting:YES block:^() {
 174         aCMenu = [[CMenu alloc] initWithPeer:cPeerObjGlobal];
 175     }];
 176 
 177     // Add it to the menu bar.
 178     [parent javaAddMenu:aCMenu atIndex:insertLocation];
 179 
 180     // If the menu is already the help menu (because we are creating an entire
 181     // menu bar) we need to note that now, because we can't rely on
 182     // setHelpMenu() being called again.
 183     if (isHelpMenu == JNI_TRUE) {
 184         [parent javaSetHelpMenu: aCMenu];
 185     }
 186 
 187 JNF_COCOA_EXIT(env);
 188     return ptr_to_jlong(aCMenu);
 189 }
 190 
 191 
 192 /*
 193  * Class:     sun_lwawt_macosx_CMenu
 194  * Method:    nativeSetMenuTitle
 195  * Signature: (JLjava/lang/String;)V