Path: blob/master/src/java.desktop/macosx/native/libawt_lwawt/awt/CMenu.m
41152 views
/*1* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#import <JavaRuntimeSupport/JavaRuntimeSupport.h>262728#import "CMenu.h"29#import "CMenuBar.h"30#import "ThreadUtilities.h"31#import "JNIUtilities.h"3233#import "sun_lwawt_macosx_CMenu.h"3435@implementation CMenu3637- (id)initWithPeer:(jobject)peer {38AWT_ASSERT_APPKIT_THREAD;39// Create the new NSMenu40self = [super initWithPeer:peer asSeparator:NO];41if (self) {42fMenu = [NSMenu javaMenuWithTitle:@""];43[fMenu retain];44[fMenu setAutoenablesItems:NO];45}46return self;47}4849- (void)dealloc {50[fMenu release];51fMenu = nil;52[super dealloc];53}5455- (void)addJavaSubmenu:(CMenu *)submenu {56[ThreadUtilities performOnMainThread:@selector(addNativeItem_OnAppKitThread:) on:self withObject:submenu waitUntilDone:YES];57}5859- (void)addJavaMenuItem:(CMenuItem *)theMenuItem {60[ThreadUtilities performOnMainThread:@selector(addNativeItem_OnAppKitThread:) on:self withObject:theMenuItem waitUntilDone:YES];61}6263- (void)addNativeItem_OnAppKitThread:(CMenuItem *)itemModified {64AWT_ASSERT_APPKIT_THREAD;65[itemModified addNSMenuItemToMenu:[self menu]];66}6768- (void)setJavaMenuTitle:(NSString *)title {6970if (title) {71[ThreadUtilities performOnMainThread:@selector(setNativeMenuTitle_OnAppKitThread:) on:self withObject:title waitUntilDone:YES];72}73}7475- (void)setNativeMenuTitle_OnAppKitThread:(NSString *)title {76AWT_ASSERT_APPKIT_THREAD;7778[fMenu setTitle:title];79// If we are a submenu we need to set our name in the parent menu's menu item.80NSMenu *parent = [fMenu supermenu];81if (parent) {82NSInteger index = [parent indexOfItemWithSubmenu:fMenu];83NSMenuItem *menuItem = [parent itemAtIndex:index];84[menuItem setTitle:title];85}86}8788- (void)deleteJavaItem:(jint)index {8990[ThreadUtilities performOnMainThread:@selector(deleteNativeJavaItem_OnAppKitThread:) on:self withObject:[NSNumber numberWithInt:index] waitUntilDone:YES];91}9293- (void)deleteNativeJavaItem_OnAppKitThread:(NSNumber *)number {94AWT_ASSERT_APPKIT_THREAD;9596int n = [number intValue];97if (n < [[self menu] numberOfItems]) {98[[self menu] removeItemAtIndex:n];99}100}101102- (void)addNSMenuItemToMenu:(NSMenu *)inMenu {103if (fMenuItem == nil) return;104[fMenuItem setSubmenu:fMenu];105[inMenu addItem:fMenuItem];106}107108- (NSMenu *)menu {109return [[fMenu retain] autorelease];110}111112- (void)setNativeEnabled_OnAppKitThread:(NSNumber *)boolNumber {113AWT_ASSERT_APPKIT_THREAD;114115@synchronized(self) {116fIsEnabled = [boolNumber boolValue];117118NSMenu* supermenu = [fMenu supermenu];119[[supermenu itemAtIndex:[supermenu indexOfItemWithSubmenu:fMenu]] setEnabled:fIsEnabled];120}121}122123- (NSString *)description {124return [NSString stringWithFormat:@"CMenu[ %@ ]", fMenu];125}126127@end128129CMenu * createCMenu (jobject cPeerObjGlobal) {130131__block CMenu *aCMenu = nil;132133[ThreadUtilities performOnMainThreadWaiting:YES block:^(){134135aCMenu = [[CMenu alloc] initWithPeer:cPeerObjGlobal];136// the aCMenu is released in CMenuComponent.dispose()137}];138139if (aCMenu == nil) {140return 0L;141}142143return aCMenu;144145}146147/*148* Class: sun_lwawt_macosx_CMenu149* Method: nativeCreateSubMenu150* Signature: (J)J151*/152JNIEXPORT jlong JNICALL153Java_sun_lwawt_macosx_CMenu_nativeCreateSubMenu154(JNIEnv *env, jobject peer, jlong parentMenu)155{156CMenu *aCMenu = nil;157JNI_COCOA_ENTER(env);158159jobject cPeerObjGlobal = (*env)->NewGlobalRef(env, peer);160161aCMenu = createCMenu (cPeerObjGlobal);162163// Add it to the parent menu164[((CMenu *)jlong_to_ptr(parentMenu)) addJavaSubmenu: aCMenu];165166JNI_COCOA_EXIT(env);167168return ptr_to_jlong(aCMenu);169}170171172173/*174* Class: sun_lwawt_macosx_CMenu175* Method: nativeCreateMenu176* Signature: (JZ)J177*/178JNIEXPORT jlong JNICALL179Java_sun_lwawt_macosx_CMenu_nativeCreateMenu180(JNIEnv *env, jobject peer,181jlong parentMenuBar, jboolean isHelpMenu, jint insertLocation)182{183CMenu *aCMenu = nil;184CMenuBar *parent = (CMenuBar *)jlong_to_ptr(parentMenuBar);185JNI_COCOA_ENTER(env);186187jobject cPeerObjGlobal = (*env)->NewGlobalRef(env, peer);188189aCMenu = createCMenu (cPeerObjGlobal);190191// Add it to the menu bar.192[parent javaAddMenu:aCMenu atIndex:insertLocation];193194// If the menu is already the help menu (because we are creating an entire195// menu bar) we need to note that now, because we can't rely on196// setHelpMenu() being called again.197if (isHelpMenu == JNI_TRUE) {198[parent javaSetHelpMenu: aCMenu];199}200201JNI_COCOA_EXIT(env);202return ptr_to_jlong(aCMenu);203}204205206/*207* Class: sun_lwawt_macosx_CMenu208* Method: nativeSetMenuTitle209* Signature: (JLjava/lang/String;)V210*/211JNIEXPORT void JNICALL212Java_sun_lwawt_macosx_CMenu_nativeSetMenuTitle213(JNIEnv *env, jobject peer, jlong menuObject, jstring label)214{215JNI_COCOA_ENTER(env);216// Set the menu's title.217[((CMenu *)jlong_to_ptr(menuObject)) setJavaMenuTitle:JavaStringToNSString(env, label)];218JNI_COCOA_EXIT(env);219}220221/*222* Class: sun_lwawt_macosx_CMenu223* Method: nativeDeleteItem224* Signature: (JI)V225*/226JNIEXPORT void JNICALL227Java_sun_lwawt_macosx_CMenu_nativeDeleteItem228(JNIEnv *env, jobject peer, jlong menuObject, jint index)229{230JNI_COCOA_ENTER(env);231// Remove the specified item.232[((CMenu *)jlong_to_ptr(menuObject)) deleteJavaItem: index];233JNI_COCOA_EXIT(env);234}235236/*237* Class: sun_lwawt_macosx_CMenu238* Method: nativeGetNSMenu239* Signature: (J)J240*/241JNIEXPORT jlong JNICALL242Java_sun_lwawt_macosx_CMenu_nativeGetNSMenu243(JNIEnv *env, jobject peer, jlong menuObject)244{245NSMenu* nsMenu = NULL;246247JNI_COCOA_ENTER(env);248// Strong retain this menu; it'll get released in Java_apple_laf_ScreenMenu_addMenuListeners249nsMenu = [[((CMenu *)jlong_to_ptr(menuObject)) menu] retain];250JNI_COCOA_EXIT(env);251252return ptr_to_jlong(nsMenu);253}254255256