Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/native_NOTIOS/sun/awt/CMenu.m
38829 views
/*1* Copyright (c) 2011, 2016, 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 <JavaNativeFoundation/JavaNativeFoundation.h>26#import <JavaRuntimeSupport/JavaRuntimeSupport.h>272829#import "CMenu.h"30#import "CMenuBar.h"31#import "ThreadUtilities.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)addSeparator {89// Nothing calls this, which is good because we need a CMenuItem here.90}9192- (void)deleteJavaItem:(jint)index {9394[ThreadUtilities performOnMainThread:@selector(deleteNativeJavaItem_OnAppKitThread:) on:self withObject:[NSNumber numberWithInt:index] waitUntilDone:YES];95}9697- (void)deleteNativeJavaItem_OnAppKitThread:(NSNumber *)number {98AWT_ASSERT_APPKIT_THREAD;99100int n = [number intValue];101if (n < [[self menu] numberOfItems]) {102[[self menu] removeItemAtIndex:n];103}104}105106- (void)addNSMenuItemToMenu:(NSMenu *)inMenu {107if (fMenuItem == nil) return;108[fMenuItem setSubmenu:fMenu];109[inMenu addItem:fMenuItem];110}111112- (NSMenu *)menu {113return [[fMenu retain] autorelease];114}115116- (void)setNativeEnabled_OnAppKitThread:(NSNumber *)boolNumber {117AWT_ASSERT_APPKIT_THREAD;118119@synchronized(self) {120fIsEnabled = [boolNumber boolValue];121122NSMenu* supermenu = [fMenu supermenu];123[[supermenu itemAtIndex:[supermenu indexOfItemWithSubmenu:fMenu]] setEnabled:fIsEnabled];124}125}126127- (NSString *)description {128return [NSString stringWithFormat:@"CMenu[ %@ ]", fMenu];129}130131@end132133CMenu * createCMenu (jobject cPeerObjGlobal) {134135__block CMenu *aCMenu = nil;136137[ThreadUtilities performOnMainThreadWaiting:YES block:^(){138139aCMenu = [[CMenu alloc] initWithPeer:cPeerObjGlobal];140// the aCMenu is released in CMenuComponent.dispose()141}];142143if (aCMenu == nil) {144return 0L;145}146147return aCMenu;148149}150151/*152* Class: sun_lwawt_macosx_CMenu153* Method: nativeCreateSubMenu154* Signature: (J)J155*/156JNIEXPORT jlong JNICALL157Java_sun_lwawt_macosx_CMenu_nativeCreateSubMenu158(JNIEnv *env, jobject peer, jlong parentMenu)159{160CMenu *aCMenu = nil;161JNF_COCOA_ENTER(env);162163jobject cPeerObjGlobal = (*env)->NewGlobalRef(env, peer);164165aCMenu = createCMenu (cPeerObjGlobal);166167// Add it to the parent menu168[((CMenu *)jlong_to_ptr(parentMenu)) addJavaSubmenu: aCMenu];169170JNF_COCOA_EXIT(env);171172return ptr_to_jlong(aCMenu);173}174175176177/*178* Class: sun_lwawt_macosx_CMenu179* Method: nativeCreateMenu180* Signature: (JZ)J181*/182JNIEXPORT jlong JNICALL183Java_sun_lwawt_macosx_CMenu_nativeCreateMenu184(JNIEnv *env, jobject peer,185jlong parentMenuBar, jboolean isHelpMenu, jint insertLocation)186{187CMenu *aCMenu = nil;188CMenuBar *parent = (CMenuBar *)jlong_to_ptr(parentMenuBar);189JNF_COCOA_ENTER(env);190191jobject cPeerObjGlobal = (*env)->NewGlobalRef(env, peer);192193aCMenu = createCMenu (cPeerObjGlobal);194195// Add it to the menu bar.196[parent javaAddMenu:aCMenu atIndex:insertLocation];197198// If the menu is already the help menu (because we are creating an entire199// menu bar) we need to note that now, because we can't rely on200// setHelpMenu() being called again.201if (isHelpMenu == JNI_TRUE) {202[parent javaSetHelpMenu: aCMenu];203}204205JNF_COCOA_EXIT(env);206return ptr_to_jlong(aCMenu);207}208209210/*211* Class: sun_lwawt_macosx_CMenu212* Method: nativeSetMenuTitle213* Signature: (JLjava/lang/String;)V214*/215JNIEXPORT void JNICALL216Java_sun_lwawt_macosx_CMenu_nativeSetMenuTitle217(JNIEnv *env, jobject peer, jlong menuObject, jstring label)218{219JNF_COCOA_ENTER(env);220// Set the menu's title.221[((CMenu *)jlong_to_ptr(menuObject)) setJavaMenuTitle:JNFJavaToNSString(env, label)];222JNF_COCOA_EXIT(env);223}224225/*226* Class: sun_lwawt_macosx_CMenu227* Method: nativeAddSeparator228* Signature: (J)V229*/230JNIEXPORT void JNICALL231Java_sun_lwawt_macosx_CMenu_nativeAddSeparator232(JNIEnv *env, jobject peer, jlong menuObject)233{234JNF_COCOA_ENTER(env);235// Add a separator item.236[((CMenu *)jlong_to_ptr(menuObject))addSeparator];237JNF_COCOA_EXIT(env);238}239240/*241* Class: sun_lwawt_macosx_CMenu242* Method: nativeDeleteItem243* Signature: (JI)V244*/245JNIEXPORT void JNICALL246Java_sun_lwawt_macosx_CMenu_nativeDeleteItem247(JNIEnv *env, jobject peer, jlong menuObject, jint index)248{249JNF_COCOA_ENTER(env);250// Remove the specified item.251[((CMenu *)jlong_to_ptr(menuObject)) deleteJavaItem: index];252JNF_COCOA_EXIT(env);253}254255/*256* Class: sun_lwawt_macosx_CMenu257* Method: nativeGetNSMenu258* Signature: (J)J259*/260JNIEXPORT jlong JNICALL261Java_sun_lwawt_macosx_CMenu_nativeGetNSMenu262(JNIEnv *env, jobject peer, jlong menuObject)263{264NSMenu* nsMenu = NULL;265266JNF_COCOA_ENTER(env);267// Strong retain this menu; it'll get released in Java_apple_laf_ScreenMenu_addMenuListeners268nsMenu = [[((CMenu *)jlong_to_ptr(menuObject)) menu] retain];269JNF_COCOA_EXIT(env);270271return ptr_to_jlong(nsMenu);272}273274275