Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/native_NOTIOS/com/apple/laf/ScreenMenu.m
38902 views
/*1* Copyright (c) 2011, 2012, 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 "ScreenMenu.h"2627#import "com_apple_laf_ScreenMenu.h"28#import "java_awt_Event.h"29#import "java_awt_event_KeyEvent.h"30#import "java_awt_event_InputEvent.h"31#import "java_awt_event_MouseEvent.h"3233#import <JavaNativeFoundation/JavaNativeFoundation.h>34#import <JavaRuntimeSupport/JavaRuntimeSupport.h>3536#import "ThreadUtilities.h"37#import "CMenuBar.h"3839/* Use THIS_FILE when it is available. */40#ifndef THIS_FILE41#define THIS_FILE __FILE__42#endif4344static JNF_CLASS_CACHE(sjc_ScreenMenu, "com/apple/laf/ScreenMenu");4546static jint ns2awtModifiers(NSUInteger keyMods) {47jint result = 0;48if (keyMods & NSShiftKeyMask) result |= java_awt_Event_SHIFT_MASK;49if (keyMods & NSControlKeyMask) result |= java_awt_Event_CTRL_MASK;50if (keyMods & NSAlternateKeyMask) result |= java_awt_Event_ALT_MASK;51if (keyMods & NSCommandKeyMask) result |= java_awt_Event_META_MASK;52return result;53}5455static jint ns2awtMouseButton(NSInteger mouseButton) {56switch (mouseButton) {57case 1: return java_awt_event_InputEvent_BUTTON1_MASK;58case 2: return java_awt_event_InputEvent_BUTTON2_MASK;59case 3: return java_awt_event_InputEvent_BUTTON3_MASK;60}61return 0;62}636465@interface NativeToJavaDelegate : NSObject <JRSMenuDelegate, NSMenuDelegate>66{67@public68NSMenu *nsmenu;69JNFJObjectWrapper *javaObjectWrapper;70}7172@property (nonatomic, retain) NSMenu *nsmenu;73@property (nonatomic, retain) JNFJObjectWrapper *javaObjectWrapper;7475- (id)initFromMenu:(NSMenu *)menu javaObj:(JNFJObjectWrapper *)obj;76- (NSMenu*)menu;77@end787980@implementation NativeToJavaDelegate8182@synthesize nsmenu;83@synthesize javaObjectWrapper;8485- (id)initFromMenu:(NSMenu *)aMenu javaObj:(JNFJObjectWrapper *)obj86{87self = [super init];88if (self) {89self.nsmenu = aMenu;90self.javaObjectWrapper = obj;91}92return self;93}9495- (NSMenu *)menu {96return self.nsmenu;97}9899- (void)menuWillOpen:(NSMenu *)menu100{101if (self.javaObjectWrapper == nil) {102#ifdef DEBUG103NSLog(@"_javaObject is NULL: (%s - %s : %d)", THIS_FILE, __FUNCTION__, __LINE__);104#endif105return;106}107108JNIEnv *env = [ThreadUtilities getJNIEnv];109JNF_COCOA_ENTER(env);110//NSLog(@"menuWillOpen %@", [menu title]);111static JNF_MEMBER_CACHE(jm_ScreenMenu_invokeOpenLater, sjc_ScreenMenu, "invokeOpenLater", "()V");112JNFCallVoidMethod(env, [self.javaObjectWrapper jObject], jm_ScreenMenu_invokeOpenLater); // AWT_THREADING Safe (AWTRunLoopMode)113JNF_COCOA_EXIT(env);114115}116117- (void)menuDidClose:(NSMenu *)menu118{119if (self.javaObjectWrapper == nil) {120#ifdef DEBUG121NSLog(@"_javaObject is NULL: (%s - %s : %d)", THIS_FILE, __FUNCTION__, __LINE__);122#endif123return;124}125126JNIEnv *env = [ThreadUtilities getJNIEnv];127JNF_COCOA_ENTER(env);128//NSLog(@"menuDidClose %@", [menu title]);129static JNF_MEMBER_CACHE(jm_ScreenMenu_invokeMenuClosing, sjc_ScreenMenu, "invokeMenuClosing", "()V");130JNFCallVoidMethod(env, [self.javaObjectWrapper jObject], jm_ScreenMenu_invokeMenuClosing); // AWT_THREADING Safe (AWTRunLoopMode)131JNF_COCOA_EXIT(env);132}133134135- (void)handleJavaMenuItemTargetedAtIndex:(NSUInteger)menuIndex rect:(NSRect)rect136{137if (self.javaObjectWrapper == nil) {138#ifdef DEBUG139NSLog(@"_javaObject is NULL: (%s - %s : %d)", THIS_FILE, __FUNCTION__, __LINE__);140#endif141return;142}143144JNIEnv *env = [ThreadUtilities getJNIEnv];145JNF_COCOA_ENTER(env);146// Send that to Java so we can test which item was hit.147static JNF_MEMBER_CACHE(jm_ScreenMenu_updateSelectedItem, sjc_ScreenMenu, "handleItemTargeted", "(IIIII)V");148JNFCallVoidMethod(env, [self.javaObjectWrapper jObject], jm_ScreenMenu_updateSelectedItem, menuIndex,149NSMinY(rect), NSMinX(rect), NSMaxY(rect), NSMaxX(rect)); // AWT_THREADING Safe (AWTRunLoopMode)150151JNF_COCOA_EXIT(env);152}153154155// Called from event handler callback156- (void)handleJavaMouseEvent:(NSEvent *)event157{158NSInteger kind = [event type];159jint javaKind = 0;160161switch (kind) {162case NSLeftMouseUp: case NSRightMouseUp: case NSOtherMouseUp:163javaKind = java_awt_event_MouseEvent_MOUSE_RELEASED;164break;165case NSLeftMouseDown: case NSRightMouseDown: case NSOtherMouseDown:166javaKind = java_awt_event_MouseEvent_MOUSE_PRESSED;167break;168case NSMouseMoved:169javaKind = java_awt_event_MouseEvent_MOUSE_MOVED;170break;171case NSLeftMouseDragged: case NSRightMouseDragged: case NSOtherMouseDragged:172javaKind = java_awt_event_MouseEvent_MOUSE_DRAGGED;173break;174}175176// Get the coordinates of the mouse in global coordinates (must be global, since our tracking rects are global.)177NSPoint globalPoint = [event locationInWindow];178jint javaX = globalPoint.x;179jint javaY = globalPoint.y;180181// Convert the event modifiers into Java modifiers182jint javaModifiers = ns2awtModifiers([event modifierFlags]) | ns2awtMouseButton([event buttonNumber]);183184// Get the event time185jlong javaWhen = JNFNSTimeIntervalToJavaMillis([event timestamp]);186187// Call the mouse event handler, which will generate Java mouse events.188JNIEnv *env = [ThreadUtilities getJNIEnv];189JNF_COCOA_ENTER(env);190static JNF_MEMBER_CACHE(jm_ScreenMenu_handleMouseEvent, sjc_ScreenMenu, "handleMouseEvent", "(IIIIJ)V");191JNFCallVoidMethod(env, [self.javaObjectWrapper jObject], jm_ScreenMenu_handleMouseEvent, javaKind, javaX, javaY, javaModifiers, javaWhen); // AWT_THREADING Safe (AWTRunLoopMode)192JNF_COCOA_EXIT(env);193}194195@end196197198/*199* Class: com_apple_laf_ScreenMenu200* Method: addMenuListeners201* Signature: (Lcom/apple/laf/ScreenMenu;J[J)V202*/203JNIEXPORT jlong JNICALL Java_com_apple_laf_ScreenMenu_addMenuListeners204(JNIEnv *env, jclass clz, jobject listener, jlong nativeMenu)205{206NativeToJavaDelegate *delegate = nil;207208JNF_COCOA_ENTER(env);209210JNFJObjectWrapper *wrapper = [JNFJObjectWrapper wrapperWithJObject:listener withEnv:env];211NSMenu *menu = jlong_to_ptr(nativeMenu);212213delegate = [[[NativeToJavaDelegate alloc] initFromMenu:menu javaObj:wrapper] autorelease];214CFRetain(delegate); // GC215216[JNFRunLoop performOnMainThreadWaiting:YES withBlock:^{217NSMenu *menu = delegate.nsmenu;218if ([menu isJavaMenu]) {219[menu setDelegate:delegate];220[menu setJavaMenuDelegate:delegate];221}222}];223224JNF_COCOA_EXIT(env);225226return ptr_to_jlong(delegate);227}228229/*230* Class: com_apple_laf_ScreenMenu231* Method: removeMenuListeners232* Signature: (JJ)V233*/234JNIEXPORT void JNICALL Java_com_apple_laf_ScreenMenu_removeMenuListeners235(JNIEnv *env, jclass clz, jlong fModelPtr)236{237if (fModelPtr == 0L) return;238239JNF_COCOA_ENTER(env);240241NativeToJavaDelegate *delegate = (NativeToJavaDelegate *)jlong_to_ptr(fModelPtr);242243[JNFRunLoop performOnMainThreadWaiting:YES withBlock:^{244NSMenu *menu = delegate.nsmenu;245[menu setJavaMenuDelegate:nil];246[menu setDelegate:nil];247delegate.nsmenu = nil;248delegate.javaObjectWrapper = nil;249}];250251CFRelease(delegate); // GC252253JNF_COCOA_EXIT(env);254}255256257