Path: blob/master/src/java.desktop/macosx/native/libawt_lwawt/awt/ApplicationDelegate.m
41152 views
/*1* Copyright (c) 2011, 2018, 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 "ApplicationDelegate.h"2627#import "com_apple_eawt_Application.h"28#import "com_apple_eawt__AppDockIconHandler.h"29#import "com_apple_eawt__AppEventHandler.h"30#import "com_apple_eawt__AppMenuBarHandler.h"31#import "com_apple_eawt__AppMenuBarHandler.h"32#import "com_apple_eawt__AppMiscHandlers.h"3334#import "CPopupMenu.h"35#import "CMenuBar.h"36#import "ThreadUtilities.h"37#import "NSApplicationAWT.h"38#import "JNIUtilities.h"3940#pragma mark App Menu helpers4142// The following is a AWT convention?43#define PREFERENCES_TAG 424445static void addMenuItem(NSMenuItem* menuItem, NSInteger index) {46AWT_ASSERT_APPKIT_THREAD;4748NSMenu *menuBar = [[NSApplication sharedApplication] mainMenu];49NSMenu *appMenu = [[menuBar itemAtIndex:0] submenu];5051[appMenu insertItem:menuItem atIndex:index];52[appMenu insertItem:[NSMenuItem separatorItem] atIndex:index + 1]; // Add the following separator53}5455static void removeMenuItem(NSMenuItem* menuItem) {56AWT_ASSERT_APPKIT_THREAD;5758NSMenu *menuBar = [[NSApplication sharedApplication] mainMenu];59NSMenu *appMenu = [[menuBar itemAtIndex:0] submenu];6061NSInteger index = [appMenu indexOfItem:menuItem];62if (index < 0) return; // something went wrong6364[appMenu removeItemAtIndex:index + 1]; // Get the following separator65[appMenu removeItem:menuItem];66}6768@interface NSBundle (EAWTOverrides)69- (BOOL)_hasEAWTOverride:(NSString *)key;70@end717273@implementation NSBundle (EAWTOverrides)7475- (BOOL)_hasEAWTOverride:(NSString *)key {76return [[[[self objectForInfoDictionaryKey:@"Java"] objectForKey:@"EAWTOverride"] objectForKey:key] boolValue];77}7879@end808182// used by JavaRuntimeSupport.framework's [JRSAppKitAWT awtAppDelegate]83// to expose our app delegate to the SWT or other apps that have knoledge84// of Java's AWT and want to install their own app delegate that will delegate85// to the AWT for some operations8687@interface JavaAWTAppDelegateLoader : NSObject { }88@end8990@implementation JavaAWTAppDelegateLoader91+ (ApplicationDelegate *) awtAppDelegate {92return [ApplicationDelegate sharedDelegate];93}94@end959697@implementation ApplicationDelegate9899@synthesize fPreferencesMenu;100@synthesize fAboutMenu;101@synthesize fProgressIndicator;102103@synthesize fDockMenu;104@synthesize fDefaultMenuBar;105106107+ (ApplicationDelegate *)sharedDelegate {108static ApplicationDelegate *sApplicationDelegate = nil;109static BOOL checked = NO;110111if (sApplicationDelegate != nil) return sApplicationDelegate;112if (checked) return nil;113114AWT_ASSERT_APPKIT_THREAD;115116// don't install the EAWT delegate if another kind of NSApplication is installed, like say, Safari117BOOL shouldInstall = NO;118if (NSApp != nil) {119if ([NSApp isMemberOfClass:[NSApplication class]]) shouldInstall = YES;120if ([NSApp isKindOfClass:[NSApplicationAWT class]]) shouldInstall = YES;121}122checked = YES;123if (!shouldInstall) return nil;124125sApplicationDelegate = [[ApplicationDelegate alloc] init];126return sApplicationDelegate;127}128129- (void)_updatePreferencesMenu:(BOOL)prefsAvailable enabled:(BOOL)prefsEnabled {130AWT_ASSERT_APPKIT_THREAD;131132if (prefsAvailable) {133// Make sure Prefs is around134if ([self.fPreferencesMenu menu] == nil) {135// Position of Prefs depends upon About availability.136NSInteger index = ([self.fAboutMenu menu] != nil) ? 2 : 0;137138addMenuItem(self.fPreferencesMenu, index);139}140141if (prefsEnabled) {142[self.fPreferencesMenu setEnabled:YES];143[self.fPreferencesMenu setTarget:self];144[self.fPreferencesMenu setAction:@selector(_preferencesMenuHandler)];145} else {146[self.fPreferencesMenu setEnabled:NO];147[self.fPreferencesMenu setTarget:nil];148[self.fPreferencesMenu setAction:nil];149}150} else {151if ([self.fPreferencesMenu menu] == nil) return;152153// Remove the preferences item154removeMenuItem(self.fPreferencesMenu);155}156}157158- (void)_updateAboutMenu:(BOOL)aboutAvailable enabled:(BOOL)aboutEnabled {159AWT_ASSERT_APPKIT_THREAD;160161if (aboutAvailable) {162// Make sure About is around163if ([self.fAboutMenu menu] == nil) {164addMenuItem(self.fAboutMenu, 0);165}166167if (aboutEnabled) {168[self.fAboutMenu setEnabled:YES];169[self.fAboutMenu setTarget:self];170[self.fAboutMenu setAction:@selector(_aboutMenuHandler)];171} else {172[self.fAboutMenu setEnabled:NO];173[self.fAboutMenu setTarget:nil];174[self.fAboutMenu setAction:nil];175}176} else {177if ([self.fAboutMenu menu] == nil) return;178179// Remove About item.180removeMenuItem(self.fAboutMenu);181}182}183184- (id) init {185AWT_ASSERT_APPKIT_THREAD;186187self = [super init];188if (!self) return self;189190// Prep for about and preferences menu191BOOL usingDefaultNib = YES;192if ([NSApp isKindOfClass:[NSApplicationAWT class]]) {193usingDefaultNib = [NSApp usingDefaultNib];194}195if (!usingDefaultNib) return self;196197NSMenu *menuBar = [[NSApplication sharedApplication] mainMenu];198NSMenu *appMenu = [[menuBar itemAtIndex:0] submenu];199200self.fPreferencesMenu = (NSMenuItem*)[appMenu itemWithTag:PREFERENCES_TAG];201self.fAboutMenu = (NSMenuItem*)[appMenu itemAtIndex:0];202203NSDockTile *dockTile = [NSApp dockTile];204self.fProgressIndicator = [[NSProgressIndicator alloc]205initWithFrame:NSMakeRect(3.f, 0.f, dockTile.size.width - 6.f, 20.f)];206207[fProgressIndicator setStyle:NSProgressIndicatorBarStyle];208[fProgressIndicator setIndeterminate:NO];209[[dockTile contentView] addSubview:fProgressIndicator];210[fProgressIndicator setMinValue:0];211[fProgressIndicator setMaxValue:100];212[fProgressIndicator setHidden:YES];213[fProgressIndicator release];214215// If the java application has a bundle with an Info.plist file with216// a CFBundleDocumentTypes entry, then it is set up to handle Open Doc217// and Print Doc commands for these files. Therefore java AWT will218// cache Open Doc and Print Doc events that are sent prior to a219// listener being installed by the client java application.220NSBundle *bundle = [NSBundle mainBundle];221fHandlesDocumentTypes = [bundle objectForInfoDictionaryKey:@"CFBundleDocumentTypes"] != nil || [bundle _hasEAWTOverride:@"DocumentHandler"];222fHandlesURLTypes = [bundle objectForInfoDictionaryKey:@"CFBundleURLTypes"] != nil || [bundle _hasEAWTOverride:@"URLHandler"];223if (fHandlesURLTypes) {224[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self225andSelector:@selector(_handleOpenURLEvent:withReplyEvent:)226forEventClass:kInternetEventClass227andEventID:kAEGetURL];228}229230// By HIG, Preferences are not available unless there is a handler. By default in Mac OS X,231// there is not a handler, but it is in the nib file for convenience.232removeMenuItem(self.fPreferencesMenu);233234[self _updateAboutMenu:YES enabled:YES];235236// Now that the AppKit objects are known and set up, initialize the model data237BOOL aboutAvailable = ([self.fAboutMenu menu] != nil);238BOOL aboutEnabled = (aboutAvailable && [self.fAboutMenu isEnabled] && ([self.fAboutMenu target] != nil));239240BOOL prefsAvailable = ([self.fPreferencesMenu menu] != nil);241BOOL prefsEnabled = (prefsAvailable && [self.fPreferencesMenu isEnabled] && ([self.fPreferencesMenu target] != nil));242243JNIEnv *env = [ThreadUtilities getJNIEnv];244DECLARE_CLASS_RETURN(sjc_AppMenuBarHandler, "com/apple/eawt/_AppMenuBarHandler", NULL);245DECLARE_STATIC_METHOD_RETURN(sjm_initMenuStates, sjc_AppMenuBarHandler, "initMenuStates", "(ZZZZ)V", NULL);246(*env)->CallStaticVoidMethod(env, sjc_AppMenuBarHandler, sjm_initMenuStates,247aboutAvailable, aboutEnabled, prefsAvailable, prefsEnabled);248CHECK_EXCEPTION();249250// register for the finish launching and system power off notifications by default251NSNotificationCenter *ctr = [NSNotificationCenter defaultCenter];252Class clz = [ApplicationDelegate class];253[ctr addObserver:clz selector:@selector(_willFinishLaunching) name:NSApplicationWillFinishLaunchingNotification object:nil];254[ctr addObserver:clz selector:@selector(_systemWillPowerOff) name:NSWorkspaceWillPowerOffNotification object:nil];255[ctr addObserver:clz selector:@selector(_appDidActivate) name:NSApplicationDidBecomeActiveNotification object:nil];256[ctr addObserver:clz selector:@selector(_appDidDeactivate) name:NSApplicationDidResignActiveNotification object:nil];257[ctr addObserver:clz selector:@selector(_appDidHide) name:NSApplicationDidHideNotification object:nil];258[ctr addObserver:clz selector:@selector(_appDidUnhide) name:NSApplicationDidUnhideNotification object:nil];259260return self;261}262263- (void)dealloc {264self.fPreferencesMenu = nil;265self.fAboutMenu = nil;266self.fDockMenu = nil;267self.fDefaultMenuBar = nil;268self.fProgressIndicator = nil;269270[super dealloc];271}272273#pragma mark Callbacks from AppKit274275static jclass sjc_AppEventHandler = NULL;276#define GET_APPEVENTHANDLER_CLASS() \277GET_CLASS(sjc_AppEventHandler, "com/apple/eawt/_AppEventHandler");278279#define GET_APPEVENTHANDLER_CLASS_RETURN(ret) \280GET_CLASS_RETURN(sjc_AppEventHandler, "com/apple/eawt/_AppEventHandler", ret);281282- (void)_handleOpenURLEvent:(NSAppleEventDescriptor *)openURLEvent withReplyEvent:(NSAppleEventDescriptor *)replyEvent {283AWT_ASSERT_APPKIT_THREAD;284if (!fHandlesURLTypes) return;285286NSString *url = [[openURLEvent paramDescriptorForKeyword:keyDirectObject] stringValue];287288//fprintf(stderr,"jm_handleOpenURL\n");289JNIEnv *env = [ThreadUtilities getJNIEnv];290jstring jURL = NSStringToJavaString(env, url);291GET_APPEVENTHANDLER_CLASS();292DECLARE_STATIC_METHOD(jm_handleOpenURI, sjc_AppEventHandler, "handleOpenURI", "(Ljava/lang/String;)V");293(*env)->CallStaticVoidMethod(env, sjc_AppEventHandler, jm_handleOpenURI, jURL);294CHECK_EXCEPTION();295(*env)->DeleteLocalRef(env, jURL);296297[replyEvent insertDescriptor:[NSAppleEventDescriptor nullDescriptor] atIndex:0];298}299300// Helper for both open file and print file methods301// Creates a Java list of files from a native list of files302- (jobject)_createFilePathArrayFrom:(NSArray *)filenames withEnv:(JNIEnv *)env {303static jclass sjc_ArrayList = NULL;304if (sjc_ArrayList == NULL) {305sjc_ArrayList = (*env)->FindClass(env, "java/util/ArrayList");306if (sjc_ArrayList != NULL) sjc_ArrayList = (*env)->NewGlobalRef(env, sjc_ArrayList); \307}308CHECK_NULL_RETURN(sjc_ArrayList, NULL);309DECLARE_METHOD_RETURN(jm_ArrayList_ctor, sjc_ArrayList, "<init>", "(I)V", NULL);310DECLARE_METHOD_RETURN(jm_ArrayList_add, sjc_ArrayList, "add", "(Ljava/lang/Object;)Z", NULL);311312jobject jFileNamesArray = (*env)->NewObject(env, sjc_ArrayList, jm_ArrayList_ctor, (jint)[filenames count]);313CHECK_EXCEPTION_NULL_RETURN(jFileNamesArray, NULL);314315for (NSString *filename in filenames) {316jstring jFileName = NormalizedPathJavaStringFromNSString(env, filename);317(*env)->CallVoidMethod(env, jFileNamesArray, jm_ArrayList_add, jFileName);318CHECK_EXCEPTION();319}320321return jFileNamesArray;322}323324// Open file handler325- (void)application:(NSApplication *)theApplication openFiles:(NSArray *)fileNames {326AWT_ASSERT_APPKIT_THREAD;327if (!fHandlesDocumentTypes) {328[theApplication replyToOpenOrPrint:NSApplicationDelegateReplyCancel];329return;330}331332//fprintf(stderr,"jm_handleOpenFile\n");333JNIEnv *env = [ThreadUtilities getJNIEnv];334335// if these files were opened from a Spotlight query, try to get the search text from the current AppleEvent336NSAppleEventDescriptor *currentEvent = [[NSAppleEventManager sharedAppleEventManager] currentAppleEvent];337NSString *searchString = [[currentEvent paramDescriptorForKeyword:keyAESearchText] stringValue];338jstring jSearchString = NSStringToJavaString(env, searchString);339340// convert the file names array341jobject jFileNamesArray = [self _createFilePathArrayFrom:fileNames withEnv:env];342343GET_APPEVENTHANDLER_CLASS();344DECLARE_STATIC_METHOD(jm_handleOpenFiles, sjc_AppEventHandler,345"handleOpenFiles", "(Ljava/util/List;Ljava/lang/String;)V");346(*env)->CallStaticVoidMethod(env, sjc_AppEventHandler, jm_handleOpenFiles, jFileNamesArray, jSearchString);347CHECK_EXCEPTION();348(*env)->DeleteLocalRef(env, jFileNamesArray);349(*env)->DeleteLocalRef(env, jSearchString);350351[theApplication replyToOpenOrPrint:NSApplicationDelegateReplySuccess];352}353354// Print handler355- (NSApplicationPrintReply)application:(NSApplication *)application printFiles:(NSArray *)fileNames withSettings:(NSDictionary *)printSettings showPrintPanels:(BOOL)showPrintPanels {356AWT_ASSERT_APPKIT_THREAD;357if (!fHandlesDocumentTypes) return NSPrintingCancelled;358359//fprintf(stderr,"jm_handlePrintFile\n");360JNIEnv *env = [ThreadUtilities getJNIEnv];361jobject jFileNamesArray = [self _createFilePathArrayFrom:fileNames withEnv:env];362GET_APPEVENTHANDLER_CLASS_RETURN(NSPrintingCancelled);363DECLARE_STATIC_METHOD_RETURN(jm_handlePrintFile, sjc_AppEventHandler,364"handlePrintFiles", "(Ljava/util/List;)V", NSPrintingCancelled);365(*env)->CallStaticVoidMethod(env, sjc_AppEventHandler, jm_handlePrintFile, jFileNamesArray);366CHECK_EXCEPTION();367(*env)->DeleteLocalRef(env, jFileNamesArray);368369return NSPrintingSuccess;370}371372// Open app handler, registered in -init373+ (void)_notifyJava:(jint)notificationType {374AWT_ASSERT_APPKIT_THREAD;375376//fprintf(stderr,"jm_handleOpenApplication\n");377JNIEnv *env = [ThreadUtilities getJNIEnv];378GET_APPEVENTHANDLER_CLASS();379DECLARE_STATIC_METHOD(jm_handleNativeNotification, sjc_AppEventHandler, "handleNativeNotification", "(I)V");380(*env)->CallStaticVoidMethod(env, sjc_AppEventHandler, jm_handleNativeNotification, notificationType);381CHECK_EXCEPTION();382}383384// About menu handler385- (void)_aboutMenuHandler {386[ApplicationDelegate _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_ABOUT];387}388389// Preferences handler390- (void)_preferencesMenuHandler {391[ApplicationDelegate _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_PREFS];392}393394// Open app handler, registered in -init395+ (void)_willFinishLaunching {396[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_OPEN_APP];397}398399// ReOpen app handler400- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag {401[ApplicationDelegate _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_REOPEN_APP];402return YES;403}404405// Quit handler406- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)app {407[ApplicationDelegate _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_QUIT];408return NSTerminateLater;409}410411+ (void)_systemWillPowerOff {412[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_SHUTDOWN];413}414415+ (void)_appDidActivate {416[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_ACTIVE_APP_GAINED];417}418419+ (void)_appDidDeactivate {420[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_ACTIVE_APP_LOST];421}422423+ (void)_appDidHide {424[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_APP_HIDDEN];425}426427+ (void)_appDidUnhide {428[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_APP_SHOWN];429}430431+ (void)_sessionDidActivate {432[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_USER_SESSION_ACTIVE];433}434435+ (void)_sessionDidDeactivate {436[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_USER_SESSION_INACTIVE];437}438439+ (void)_screenDidSleep {440[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_SCREEN_SLEEP];441}442443+ (void)_screenDidWake {444[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_SCREEN_WAKE];445}446447+ (void)_systemDidSleep {448[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_SYSTEM_SLEEP];449}450451+ (void)_systemDidWake {452[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_SYSTEM_WAKE];453}454455+ (void)_registerForNotification:(NSNumber *)notificationTypeNum {456NSNotificationCenter *ctr = [[NSWorkspace sharedWorkspace] notificationCenter];457Class clz = [ApplicationDelegate class];458459jint notificationType = [notificationTypeNum intValue];460switch (notificationType) {461case com_apple_eawt__AppEventHandler_REGISTER_USER_SESSION:462[ctr addObserver:clz selector:@selector(_sessionDidActivate) name:NSWorkspaceSessionDidBecomeActiveNotification object:nil];463[ctr addObserver:clz selector:@selector(_sessionDidDeactivate) name:NSWorkspaceSessionDidResignActiveNotification object:nil];464break;465case com_apple_eawt__AppEventHandler_REGISTER_SCREEN_SLEEP:466[ctr addObserver:clz selector:@selector(_screenDidSleep) name:NSWorkspaceScreensDidSleepNotification object:nil];467[ctr addObserver:clz selector:@selector(_screenDidWake) name:NSWorkspaceScreensDidWakeNotification object:nil];468break;469case com_apple_eawt__AppEventHandler_REGISTER_SYSTEM_SLEEP:470[ctr addObserver:clz selector:@selector(_systemDidSleep) name:NSWorkspaceWillSleepNotification object:nil];471[ctr addObserver:clz selector:@selector(_systemDidWake) name:NSWorkspaceDidWakeNotification object:nil];472break;473default:474NSLog(@"EAWT attempting to register for unknown notification: %d", (int)notificationType);475break;476}477}478479// Retrieves the menu to be attached to the Dock icon (AppKit callback)480- (NSMenu *)applicationDockMenu:(NSApplication *)sender {481AWT_ASSERT_APPKIT_THREAD;482return self.fDockMenu;483}484485- (CMenuBar *)defaultMenuBar {486return [[self.fDefaultMenuBar retain] autorelease];487}488489490#pragma mark Helpers called on the main thread from Java491492// Sets a new NSImageView on the Dock tile493+ (void)_setDockIconImage:(NSImage *)image {494AWT_ASSERT_APPKIT_THREAD;495496NSDockTile *dockTile = [NSApp dockTile];497if (image == nil) {498[dockTile setContentView:nil];499return;500}501502// setup an image view for the dock tile503NSRect frame = NSMakeRect(0, 0, dockTile.size.width, dockTile.size.height);504NSImageView *dockImageView = [[NSImageView alloc] initWithFrame: frame];505[dockImageView setImageScaling:NSImageScaleProportionallyUpOrDown];506[dockImageView setImage:image];507508[[ApplicationDelegate sharedDelegate].fProgressIndicator removeFromSuperview];509[dockImageView addSubview:[ApplicationDelegate sharedDelegate].fProgressIndicator];510511// add it to the NSDockTile512[dockTile setContentView: dockImageView];513[dockTile display];514515[dockImageView release];516}517518+ (void)_setDockIconProgress:(NSNumber *)value {519AWT_ASSERT_APPKIT_THREAD;520521ApplicationDelegate *delegate = [ApplicationDelegate sharedDelegate];522if ([value doubleValue] >= 0 && [value doubleValue] <=100) {523[delegate.fProgressIndicator setDoubleValue:[value doubleValue]];524[delegate.fProgressIndicator setHidden:NO];525} else {526[delegate.fProgressIndicator setHidden:YES];527}528529[[NSApp dockTile] display];530}531532// Obtains the image of the Dock icon, either manually set, a drawn copy, or the default NSApplicationIcon533+ (NSImage *)_dockIconImage {534AWT_ASSERT_APPKIT_THREAD;535536NSDockTile *dockTile = [NSApp dockTile];537NSView *view = [dockTile contentView];538539if ([view isKindOfClass:[NSImageView class]]) {540NSImage *img = [((NSImageView *)view) image];541if (img) return img;542}543544if (view == nil) {545return [NSImage imageNamed:@"NSApplicationIcon"];546}547548NSRect frame = [view frame];549NSImage *image = [[NSImage alloc] initWithSize:frame.size];550[image lockFocus];551[view drawRect:frame];552[image unlockFocus];553[image autorelease];554return image;555}556557@end558559560#pragma mark Native JNI calls561562/*563* Class: com_apple_eawt_Application564* Method: nativeInitializeApplicationDelegate565* Signature: ()V566*/567JNIEXPORT void JNICALL Java_com_apple_eawt_Application_nativeInitializeApplicationDelegate568(JNIEnv *env, jclass clz)569{570JNI_COCOA_ENTER(env);571// Force initialization to happen on AppKit thread!572[ThreadUtilities performOnMainThreadWaiting:NO block:^(){573[ApplicationDelegate sharedDelegate];574}];575JNI_COCOA_EXIT(env);576}577578/*579* Class: com_apple_eawt__AppEventHandler580* Method: nativeOpenCocoaAboutWindow581* Signature: ()V582*/583JNIEXPORT void JNICALL Java_com_apple_eawt__1AppEventHandler_nativeOpenCocoaAboutWindow584(JNIEnv *env, jclass clz)585{586JNI_COCOA_ENTER(env);587588[ThreadUtilities performOnMainThreadWaiting:NO block:^(){589[NSApp orderFrontStandardAboutPanel:nil];590}];591592JNI_COCOA_EXIT(env);593}594595/*596* Class: com_apple_eawt__AppEventHandler597* Method: nativeReplyToAppShouldTerminate598* Signature: (Z)V599*/600JNIEXPORT void JNICALL Java_com_apple_eawt__1AppEventHandler_nativeReplyToAppShouldTerminate601(JNIEnv *env, jclass clz, jboolean doTerminate)602{603JNI_COCOA_ENTER(env);604605[ThreadUtilities performOnMainThreadWaiting:NO block:^(){606[NSApp replyToApplicationShouldTerminate:doTerminate];607}];608609JNI_COCOA_EXIT(env);610}611612/*613* Class: com_apple_eawt__AppEventHandler614* Method: nativeRegisterForNotification615* Signature: (I)V616*/617JNIEXPORT void JNICALL Java_com_apple_eawt__1AppEventHandler_nativeRegisterForNotification618(JNIEnv *env, jclass clz, jint notificationType)619{620JNI_COCOA_ENTER(env);621[ThreadUtilities performOnMainThread:@selector(_registerForNotification:)622on:[ApplicationDelegate class]623withObject:[NSNumber numberWithInt:notificationType]624waitUntilDone:NO];625JNI_COCOA_EXIT(env);626}627628/*629* Class: com_apple_eawt__AppDockIconHandler630* Method: nativeSetDockMenu631* Signature: (J)V632*/633JNIEXPORT void JNICALL Java_com_apple_eawt__1AppDockIconHandler_nativeSetDockMenu634(JNIEnv *env, jclass clz, jlong nsMenuPtr)635{636JNI_COCOA_ENTER(env);637638NSMenu *menu = (NSMenu *)jlong_to_ptr(nsMenuPtr);639[ThreadUtilities performOnMainThreadWaiting:YES block:^(){640[ApplicationDelegate sharedDelegate].fDockMenu = menu;641}];642643JNI_COCOA_EXIT(env);644}645646/*647* Class: com_apple_eawt__AppDockIconHandler648* Method: nativeSetDockIconImage649* Signature: (J)V650*/651JNIEXPORT void JNICALL Java_com_apple_eawt__1AppDockIconHandler_nativeSetDockIconImage652(JNIEnv *env, jclass clz, jlong nsImagePtr)653{654JNI_COCOA_ENTER(env);655656NSImage *_image = (NSImage *)jlong_to_ptr(nsImagePtr);657[ThreadUtilities performOnMainThread:@selector(_setDockIconImage:)658on:[ApplicationDelegate class]659withObject:_image660waitUntilDone:NO];661662JNI_COCOA_EXIT(env);663}664665/*666* Class: com_apple_eawt__AppDockIconHandler667* Method: nativeSetDockIconProgress668* Signature: (I)V669*/670JNIEXPORT void JNICALL Java_com_apple_eawt__1AppDockIconHandler_nativeSetDockIconProgress671(JNIEnv *env, jclass clz, jint value)672{673JNI_COCOA_ENTER(env);674675[ThreadUtilities performOnMainThread:@selector(_setDockIconProgress:)676on:[ApplicationDelegate class]677withObject:[NSNumber numberWithInt:value]678waitUntilDone:NO];679680JNI_COCOA_EXIT(env);681}682683/*684* Class: com_apple_eawt__AppDockIconHandler685* Method: nativeGetDockIconImage686* Signature: ()J687*/688JNIEXPORT jlong JNICALL Java_com_apple_eawt__1AppDockIconHandler_nativeGetDockIconImage689(JNIEnv *env, jclass clz)690{691__block NSImage *image = nil;692693JNI_COCOA_ENTER(env);694695[ThreadUtilities performOnMainThreadWaiting:YES block:^(){696image = [[ApplicationDelegate _dockIconImage] retain];697}];698699JNI_COCOA_EXIT(env);700701return ptr_to_jlong(image);702}703704/*705* Class: com_apple_eawt__AppDockIconHandler706* Method: nativeSetDockIconBadge707* Signature: (Ljava/lang/String;)V708*/709JNIEXPORT void JNICALL Java_com_apple_eawt__1AppDockIconHandler_nativeSetDockIconBadge710(JNIEnv *env, jclass clz, jstring badge)711{712JNI_COCOA_ENTER(env);713714NSString *badgeString = JavaStringToNSString(env, badge);715[ThreadUtilities performOnMainThreadWaiting:NO block:^(){716NSDockTile *dockTile = [NSApp dockTile];717[dockTile setBadgeLabel:badgeString];718[dockTile display];719}];720721JNI_COCOA_EXIT(env);722}723724/*725* Class: com_apple_eawt__AppMiscHandlers726* Method: nativeRequestActivation727* Signature: (Z)V728*/729JNIEXPORT void JNICALL Java_com_apple_eawt__1AppMiscHandlers_nativeRequestActivation730(JNIEnv *env, jclass clz, jboolean allWindows)731{732JNI_COCOA_ENTER(env);733734[ThreadUtilities performOnMainThreadWaiting:NO block:^(){735NSApplicationActivationOptions options = allWindows ? NSApplicationActivateAllWindows : 0;736options |= NSApplicationActivateIgnoringOtherApps; // without this, nothing happens!737[[NSRunningApplication currentApplication] activateWithOptions:options];738}];739740JNI_COCOA_EXIT(env);741}742743/*744* Class: com_apple_eawt__AppMiscHandlers745* Method: nativeRequestUserAttention746* Signature: (Z)V747*/748JNIEXPORT void JNICALL Java_com_apple_eawt__1AppMiscHandlers_nativeRequestUserAttention749(JNIEnv *env, jclass clz, jboolean critical)750{751JNI_COCOA_ENTER(env);752753[ThreadUtilities performOnMainThreadWaiting:NO block:^(){754[NSApp requestUserAttention:critical ? NSCriticalRequest : NSInformationalRequest];755}];756757JNI_COCOA_EXIT(env);758}759760/*761* Class: com_apple_eawt__AppMiscHandlers762* Method: nativeOpenHelpViewer763* Signature: ()V764*/765JNIEXPORT void JNICALL Java_com_apple_eawt__1AppMiscHandlers_nativeOpenHelpViewer766(JNIEnv *env, jclass clz)767{768JNI_COCOA_ENTER(env);769770[ThreadUtilities performOnMainThread:@selector(showHelp:)771on:NSApp772withObject:nil773waitUntilDone:NO];774775JNI_COCOA_EXIT(env);776}777778/*779* Class: com_apple_eawt__AppMiscHandlers780* Method: nativeEnableSuddenTermination781* Signature: ()V782*/783JNIEXPORT void JNICALL Java_com_apple_eawt__1AppMiscHandlers_nativeEnableSuddenTermination784(JNIEnv *env, jclass clz)785{786JNI_COCOA_ENTER(env);787788[[NSProcessInfo processInfo] enableSuddenTermination]; // Foundation thread-safe789790JNI_COCOA_EXIT(env);791}792793/*794* Class: com_apple_eawt__AppMiscHandlers795* Method: nativeDisableSuddenTermination796* Signature: ()V797*/798JNIEXPORT void JNICALL Java_com_apple_eawt__1AppMiscHandlers_nativeDisableSuddenTermination799(JNIEnv *env, jclass clz)800{801JNI_COCOA_ENTER(env);802803[[NSProcessInfo processInfo] disableSuddenTermination]; // Foundation thread-safe804805JNI_COCOA_EXIT(env);806}807808/*809* Class: com_apple_eawt__AppMenuBarHandler810* Method: nativeSetMenuState811* Signature: (IZZ)V812*/813JNIEXPORT void JNICALL Java_com_apple_eawt__1AppMenuBarHandler_nativeSetMenuState814(JNIEnv *env, jclass clz, jint menuID, jboolean visible, jboolean enabled)815{816JNI_COCOA_ENTER(env);817818[ThreadUtilities performOnMainThreadWaiting:NO block:^(){819ApplicationDelegate *delegate = [ApplicationDelegate sharedDelegate];820switch (menuID) {821case com_apple_eawt__AppMenuBarHandler_MENU_ABOUT:822[delegate _updateAboutMenu:visible enabled:enabled];823break;824case com_apple_eawt__AppMenuBarHandler_MENU_PREFS:825[delegate _updatePreferencesMenu:visible enabled:enabled];826break;827}828}];829830JNI_COCOA_EXIT(env);831}832833/*834* Class: com_apple_eawt__AppMenuBarHandler835* Method: nativeSetDefaultMenuBar836* Signature: (J)V837*/838JNIEXPORT void JNICALL Java_com_apple_eawt__1AppMenuBarHandler_nativeSetDefaultMenuBar839(JNIEnv *env, jclass clz, jlong cMenuBarPtr)840{841JNI_COCOA_ENTER(env);842843CMenuBar *menu = (CMenuBar *)jlong_to_ptr(cMenuBarPtr);844[ThreadUtilities performOnMainThreadWaiting:NO block:^(){845[ApplicationDelegate sharedDelegate].fDefaultMenuBar = menu;846}];847848JNI_COCOA_EXIT(env);849}850851/*852* Class: com_apple_eawt__AppMenuBarHandler853* Method: nativeActivateDefaultMenuBar854* Signature: (J)V855*/856JNIEXPORT void JNICALL Java_com_apple_eawt__1AppMenuBarHandler_nativeActivateDefaultMenuBar857(JNIEnv *env, jclass clz, jlong cMenuBarPtr)858{859JNI_COCOA_ENTER(env);860861CMenuBar *menu = (CMenuBar *)jlong_to_ptr(cMenuBarPtr);862[ThreadUtilities performOnMainThreadWaiting:NO block:^(){863if (menu) {864[CMenuBar activate:menu modallyDisabled:NO];865}866}];867868JNI_COCOA_EXIT(env);869}870871872