rev 54094 : 8257853: Remove dependencies on JNF's JNI utility functions in AWT and 2D code
rev 54095 : 8259343: [macOS] Update JNI error handling in Cocoa code.
rev 54098 : 8260616: Removing remaining JNF dependencies in the java.desktop module
8259729: Missed JNFInstanceOf -> IsInstanceOf conversion

   1 /*
   2  * Copyright (c) 2011, 2015, 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 "GeomUtilities.h"
  27 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  28 















  29 static jobject NewJavaRect(JNIEnv *env, jdouble x, jdouble y, jdouble w, jdouble h) {
  30     DECLARE_CLASS_RETURN(sjc_Rectangle2DDouble, "java/awt/geom/Rectangle2D$Double", NULL);
  31     DECLARE_METHOD_RETURN(ctor_Rectangle2DDouble, sjc_Rectangle2DDouble, "<init>", "(DDDD)V", NULL);
  32     jobject o = (*env)->NewObject(env, sjc_Rectangle2DDouble, ctor_Rectangle2DDouble, x, y, w, h);
  33     CHECK_EXCEPTION();
  34     return o;
  35 }
  36 
  37 jobject CGToJavaRect(JNIEnv *env, CGRect rect) {
  38    return NewJavaRect(env,
  39                       rect.origin.x,
  40                       rect.origin.y,
  41                       rect.size.width,
  42                       rect.size.height);
  43 }
  44 
  45 jobject NSToJavaRect(JNIEnv *env, NSRect rect) {
  46     return NewJavaRect(env,
  47                        rect.origin.x,
  48                        rect.origin.y,
  49                        rect.size.width,
  50                        rect.size.height);
  51 }
  52 







  53 NSRect JavaToNSRect(JNIEnv *env, jobject rect) {
  54     DECLARE_CLASS_RETURN(sjc_Rectangle2D, "java/awt/geom/Rectangle2D", NSZeroRect);
  55     DECLARE_METHOD_RETURN(jm_rect_getX, sjc_Rectangle2D, "getX", "()D", NSZeroRect);
  56     DECLARE_METHOD_RETURN(jm_rect_getY, sjc_Rectangle2D, "getY", "()D", NSZeroRect);
  57     DECLARE_METHOD_RETURN(jm_rect_getWidth, sjc_Rectangle2D, "getWidth", "()D", NSZeroRect);
  58     DECLARE_METHOD_RETURN(jm_rect_getHeight, sjc_Rectangle2D, "getHeight", "()D", NSZeroRect);
  59     jdouble x = (*env)->CallDoubleMethod(env, rect, jm_rect_getX); CHECK_EXCEPTION();
  60     jdouble y = (*env)->CallDoubleMethod(env, rect, jm_rect_getY); CHECK_EXCEPTION();
  61     jdouble w = (*env)->CallDoubleMethod(env, rect, jm_rect_getWidth); CHECK_EXCEPTION();
  62     jdouble h = (*env)->CallDoubleMethod(env, rect, jm_rect_getHeight); CHECK_EXCEPTION();
  63     return NSMakeRect(x, y, w, h);
  64 }
  65 
  66 jobject NSToJavaPoint(JNIEnv *env, NSPoint point) {
  67     DECLARE_CLASS_RETURN(sjc_Point2DDouble, "java/awt/geom/Point2D$Double", NULL);
  68     DECLARE_METHOD_RETURN(ctor_Point2DDouble, sjc_Point2DDouble, "<init>", "(DD)V", NULL);
  69     jobject o =  (*env)->NewObject(env, sjc_Point2DDouble, ctor_Point2DDouble, (jdouble)point.x, (jdouble)point.y);
  70     CHECK_EXCEPTION();
  71     return o;
  72 }
  73 
  74 NSPoint JavaToNSPoint(JNIEnv *env, jobject point) {
  75     DECLARE_CLASS_RETURN(sjc_Point2D, "java/awt/geom/Point2D", NSZeroPoint);
  76     DECLARE_METHOD_RETURN(jm_pt_getX, sjc_Point2D, "getX", "()D", NSZeroPoint);
  77     DECLARE_METHOD_RETURN(jm_pt_getY, sjc_Point2D, "getY", "()D", NSZeroPoint);
  78     jdouble x = (*env)->CallDoubleMethod(env, point, jm_pt_getX); CHECK_EXCEPTION();
  79     jdouble y = (*env)->CallDoubleMethod(env, point, jm_pt_getY); CHECK_EXCEPTION();
  80     return NSMakePoint(x, y);
  81 }
  82 
  83 jobject NSToJavaSize(JNIEnv *env, NSSize size) {
  84     DECLARE_CLASS_RETURN(sjc_Dimension2DDouble, "java/awt/Dimension", NULL); // No Dimension2D$Double :-(
  85     DECLARE_METHOD_RETURN(ctor_Dimension2DDouble, sjc_Dimension2DDouble, "<init>", "(II)V", NULL);
  86     jobject o = (*env)->NewObject(env, sjc_Dimension2DDouble, ctor_Dimension2DDouble, (jint)size.width, (jint)size.height);
  87     CHECK_EXCEPTION();
  88     return o;
  89 }
  90 
  91 NSSize JavaToNSSize(JNIEnv *env, jobject dimension) {
  92     DECLARE_CLASS_RETURN(sjc_Dimension2D, "java/awt/geom/Dimension2D", NSZeroSize);
  93     DECLARE_METHOD_RETURN(jm_sz_getWidth, sjc_Dimension2D, "getWidth", "()D", NSZeroSize);
  94     DECLARE_METHOD_RETURN(jm_sz_getHeight, sjc_Dimension2D, "getHeight", "()D", NSZeroSize);
  95     jdouble w = (*env)->CallDoubleMethod(env, dimension, jm_sz_getWidth); CHECK_EXCEPTION();
  96     jdouble h = (*env)->CallDoubleMethod(env, dimension, jm_sz_getHeight); CHECK_EXCEPTION();
  97     return NSMakeSize(w, h);
  98 }
  99 
 100 static NSScreen *primaryScreen(JNIEnv *env) {
 101     NSScreen *primaryScreen = [[NSScreen screens] objectAtIndex:0];
 102     if (primaryScreen != nil) return primaryScreen;
 103     if ((env != NULL) && ([NSThread isMainThread] == NO)) {
 104         JNU_ThrowByName(env, "java/lang/RuntimeException", "Failed to convert, no screen.");
 105     }
 106     [NSException raise:NSGenericException format:@"Failed to convert, no screen."];
 107     return nil;
 108 }
 109 
 110 NSPoint ConvertNSScreenPoint(JNIEnv *env, NSPoint point) {
 111     point.y = [primaryScreen(env) frame].size.height - point.y;
 112     return point;
 113 }
 114 
 115 NSRect ConvertNSScreenRect(JNIEnv *env, NSRect rect) {
 116     rect.origin.y = [primaryScreen(env) frame].size.height - rect.origin.y - rect.size.height;
 117     return rect;
 118 }
--- EOF ---