1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  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 <Cocoa/Cocoa.h>
  27 #import <jni_util.h>
  28 
  29 static NSWindow *testWindow;
  30 static NSColorPanel *colorPanel;
  31 
  32 #define JNI_COCOA_ENTER(env) \
  33  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; \
  34  @try {
  35 
  36 #define JNI_COCOA_EXIT(env) \
  37  } \
  38  @catch (NSException *e) { \
  39      NSLog(@"%@", [e callStackSymbols]); \
  40  } \
  41  @finally { \
  42     [pool drain]; \
  43   };
  44 
  45 /*
  46  * Pass the block to a selector of a class that extends NSObject
  47  * There is no need to copy the block since this class always waits.
  48  */
  49 @interface BlockRunner : NSObject { }
  50 
  51 + (void)invokeBlock:(void (^)())block;
  52 @end
  53 
  54 @implementation BlockRunner
  55 
  56 + (void)invokeBlock:(void (^)())block{
  57   block();
  58 }
  59 
  60 + (void)performBlock:(void (^)())block {
  61   [self performSelectorOnMainThread:@selector(invokeBlock:) withObject:block waitUntilDone:YES];
  62 }
  63 
  64 @end
  65 
  66 /*
  67  * Class:     TestMainKeyWindow
  68  * Method:    setup
  69  * Signature: ()V
  70  */
  71 JNIEXPORT void JNICALL Java_TestMainKeyWindow_setup(JNIEnv *env, jclass cl)
  72 {
  73     JNI_COCOA_ENTER(env);
  74 
  75     void (^block)() = ^(){
  76         NSScreen *mainScreen = [NSScreen mainScreen];
  77         NSRect screenFrame = [mainScreen frame];
  78         NSRect frame = NSMakeRect(130, screenFrame.size.height - 280, 200, 100);
  79 
  80 #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101200
  81         NSWindowStyleMask style = NSWindowStyleMaskTitled;
  82 #else
  83         NSInteger style = NSTitledWindowMask;
  84 #endif
  85 
  86         NSRect rect = [NSWindow contentRectForFrameRect:frame styleMask:style];
  87         NSBackingStoreType bt = NSBackingStoreBuffered;
  88         testWindow = [[[NSWindow alloc] initWithContentRect:rect styleMask:style backing:bt defer:NO] retain];
  89         testWindow.title = @"NSWindow";
  90         [testWindow setBackgroundColor:[NSColor blueColor]];
  91         [testWindow makeKeyAndOrderFront:nil];
  92         // Java coordinates are 100, 200
  93 
  94         colorPanel = [[NSColorPanel sharedColorPanel] retain];
  95         [colorPanel setReleasedWhenClosed: YES];
  96         colorPanel.restorable = NO;
  97         [colorPanel setFrameTopLeftPoint: NSMakePoint(130, screenFrame.size.height - 300)];
  98         // Java coordinates are 100, 400
  99         [colorPanel makeKeyAndOrderFront: nil];
 100     };
 101 
 102     if ([NSThread isMainThread]) {
 103         block();
 104     } else {
 105         [BlockRunner performBlock:block];
 106     }
 107 
 108     JNI_COCOA_EXIT(env);
 109 }
 110 
 111 /*
 112  * Class:     TestMainKeyWindow
 113  * Method:    takedown
 114  * Signature: ()V
 115  */
 116 JNIEXPORT void JNICALL Java_TestMainKeyWindow_takedown(JNIEnv *env, jclass cl)
 117 {
 118     JNI_COCOA_ENTER(env);
 119 
 120     void (^block)() = ^(){
 121         if (testWindow != nil) {
 122             [testWindow close];
 123             testWindow = nil;
 124         }
 125         if (colorPanel != nil) {
 126             [colorPanel orderOut:nil];
 127             colorPanel = nil;
 128         }
 129     };
 130 
 131     if ([NSThread isMainThread]) {
 132         block();
 133     } else {
 134         [BlockRunner performBlock:block];
 135     }
 136 
 137     JNI_COCOA_EXIT(env);
 138 }
 139 
 140 /*
 141  * Class:     TestMainKeyWindow
 142  * Method:    activateApplication
 143  * Signature: ()V
 144  */
 145 JNIEXPORT void JNICALL Java_TestMainKeyWindow_activateApplication
 146   (JNIEnv *env, jclass cl)
 147 {
 148     JNI_COCOA_ENTER(env);
 149 
 150     void (^block)() = ^(){
 151         [NSApp activateIgnoringOtherApps:YES];
 152     };
 153 
 154     [BlockRunner performBlock:block];
 155 
 156   JNI_COCOA_EXIT(env);
 157 }