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(130, screenFrame.size.height - 280, 200, 100);
  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: YES];
  62         colorPanel.restorable = NO;
  63         [colorPanel setFrameTopLeftPoint: NSMakePoint(130, screenFrame.size.height - 300)];
  64         // Java coordinates are 100, 400
  65         [colorPanel makeKeyAndOrderFront: nil];
  66     };
  67 
  68     if ([NSThread isMainThread]) {
  69         block();
  70     } else {
  71         [JNFRunLoop performOnMainThreadWaiting:YES withBlock:block];
  72     }
  73 
  74     JNF_COCOA_EXIT(env);
  75 }
  76 
  77 /*
  78  * Class:     TestMainKeyWindow
  79  * Method:    takedown
  80  * Signature: ()V
  81  */
  82 JNIEXPORT void JNICALL Java_TestMainKeyWindow_takedown(JNIEnv *env, jclass cl)
  83 {
  84     JNF_COCOA_ENTER(env);
  85 
  86     void (^block)() = ^(){
  87         if (testWindow != nil) {
  88             [testWindow close];
  89             testWindow = nil;
  90         }
  91         if (colorPanel != nil) {
  92             [colorPanel orderOut:nil];
  93             colorPanel = nil;
  94         }
  95     };
  96 
  97     if ([NSThread isMainThread]) {
  98         block();
  99     } else {
 100         [JNFRunLoop performOnMainThreadWaiting:YES withBlock:block];
 101     }
 102 
 103     JNF_COCOA_EXIT(env);
 104 }
 105 
 106 /*
 107  * Class:     TestMainKeyWindow
 108  * Method:    activateApplication
 109  * Signature: ()V
 110  */
 111 JNIEXPORT void JNICALL Java_TestMainKeyWindow_activateApplication
 112   (JNIEnv *env, jclass cl)
 113 {
 114     JNF_COCOA_ENTER(env);
 115 
 116     void (^block)() = ^(){
 117         [NSApp activateIgnoringOtherApps:YES];
 118     };
 119 
 120     [JNFRunLoop performOnMainThreadWaiting:YES withBlock:block];
 121 
 122   JNF_COCOA_EXIT(env);
 123 }