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 <JavaNativeFoundation/JavaNativeFoundation.h>
  28 
  29 static NSWindow *testWindow;
  30 static NSColorPanel *colorPanel;
  31 
  32 /*
  33  * Class:     TestMainKeyWindow
  34  * Method:    setup
  35  * Signature: ()V
  36  */
  37 JNIEXPORT void JNICALL Java_TestMainKeyWindow_setup(JNIEnv *env, jclass cl)
  38 {
  39     JNF_COCOA_ENTER(env);
  40 
  41     void (^block)() = ^(){
  42     NSScreen *mainScreen = [NSScreen mainScreen];
  43     NSRect screenFrame = [mainScreen frame];
  44     NSRect frame = NSMakeRect(100, screenFrame.size.height - 350, 150, 150);
  45 
  46 #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101200
  47     NSWindowStyleMask style = NSWindowStyleMaskTitled;
  48 #else
  49     NSInteger style = NSTitledWindowMask;
  50 #endif
  51 
  52     NSRect rect = [NSWindow contentRectForFrameRect:frame styleMask:style];
  53     NSBackingStoreType bt = NSBackingStoreBuffered;
  54     testWindow = [[[NSWindow alloc] initWithContentRect:rect styleMask:style backing:bt defer:NO] retain];
  55     testWindow.title = @"NSWindow";
  56     [testWindow setBackgroundColor:[NSColor blueColor]];
  57     [testWindow makeKeyAndOrderFront:nil];
  58     // Java coordinates are 100, 200
  59 
  60     colorPanel = [[NSColorPanel sharedColorPanel] retain];
  61     [colorPanel setReleasedWhenClosed: NO];
  62     [colorPanel makeKeyAndOrderFront: nil];
  63     [colorPanel setFrameTopLeftPoint: NSMakePoint(100, screenFrame.size.height - 400)];
  64     // Java coordinates are 100, 400
  65     };
  66 
  67   if ([NSThread isMainThread]) {
  68       block();
  69   } else {
  70       [JNFRunLoop performOnMainThreadWaiting:YES withBlock:block];
  71   }
  72 
  73   JNF_COCOA_EXIT(env);
  74 }
  75 
  76 /*
  77  * Class:     TestMainKeyWindow
  78  * Method:    takedown
  79  * Signature: ()V
  80  */
  81 JNIEXPORT void JNICALL Java_TestMainKeyWindow_takedown(JNIEnv *env, jclass cl)
  82 {
  83     JNF_COCOA_ENTER(env);
  84 
  85     void (^block)() = ^(){
  86         if (testWindow != nil) {
  87             [testWindow close];
  88             testWindow = nil;
  89         }
  90         if (colorPanel != nil) {
  91             [colorPanel close];
  92             colorPanel = nil;
  93         }
  94     };
  95 
  96     if ([NSThread isMainThread]) {
  97         block();
  98     } else {
  99         [JNFRunLoop performOnMainThreadWaiting:YES withBlock:block];
 100     }
 101 
 102     JNF_COCOA_EXIT(env);
 103 }
 104 
 105 /*
 106  * Class:     TestMainKeyWindow
 107  * Method:    activateApplication
 108  * Signature: ()V
 109  */
 110 JNIEXPORT void JNICALL Java_TestMainKeyWindow_activateApplication
 111   (JNIEnv *env, jclass cl)
 112 {
 113     JNF_COCOA_ENTER(env);
 114 
 115     void (^block)() = ^(){
 116         [NSApp activateIgnoringOtherApps:YES];
 117     };
 118 
 119     [JNFRunLoop performOnMainThreadWaiting:YES withBlock:block];
 120 
 121   JNF_COCOA_EXIT(env);
 122 }