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 JNF_CLASS_CACHE(sjc_Point2D, "java/awt/geom/Point2D"); 30 static JNF_MEMBER_CACHE(jm_pt_getX, sjc_Point2D, "getX", "()D"); 31 static JNF_MEMBER_CACHE(jm_pt_getY, sjc_Point2D, "getY", "()D"); 32 33 static JNF_CLASS_CACHE(sjc_Dimension2D, "java/awt/geom/Dimension2D"); 34 static JNF_MEMBER_CACHE(jm_sz_getWidth, sjc_Dimension2D, "getWidth", "()D"); 35 static JNF_MEMBER_CACHE(jm_sz_getHeight, sjc_Dimension2D, "getHeight", "()D"); 36 37 static JNF_CLASS_CACHE(sjc_Rectangle2D, "java/awt/geom/Rectangle2D"); 38 static JNF_MEMBER_CACHE(jm_rect_getX, sjc_Rectangle2D, "getX", "()D"); 39 static JNF_MEMBER_CACHE(jm_rect_getY, sjc_Rectangle2D, "getY", "()D"); 40 static JNF_MEMBER_CACHE(jm_rect_getWidth, sjc_Rectangle2D, "getWidth", "()D"); 41 static JNF_MEMBER_CACHE(jm_rect_getHeight, sjc_Rectangle2D, "getHeight", "()D"); 42 43 44 static jobject NewJavaRect(JNIEnv *env, jdouble x, jdouble y, jdouble w, jdouble h) { 45 static JNF_CLASS_CACHE(sjc_Rectangle2DDouble, "java/awt/geom/Rectangle2D$Double"); 46 static JNF_CTOR_CACHE(ctor_Rectangle2DDouble, sjc_Rectangle2DDouble, "(DDDD)V"); 47 return JNFNewObject(env, ctor_Rectangle2DDouble, x, y, w, h); 48 } 49 50 jobject CGToJavaRect(JNIEnv *env, CGRect rect) { 51 return NewJavaRect(env, 52 rect.origin.x, 53 rect.origin.y, 54 rect.size.width, 55 rect.size.height); 56 } 57 58 jobject NSToJavaRect(JNIEnv *env, NSRect rect) { 59 return NewJavaRect(env, 60 rect.origin.x, 61 rect.origin.y, 62 rect.size.width, 63 rect.size.height); 64 } 65 66 CGRect JavaToCGRect(JNIEnv *env, jobject rect) { 67 return CGRectMake(JNFCallDoubleMethod(env, rect, jm_rect_getX), 68 JNFCallDoubleMethod(env, rect, jm_rect_getY), 69 JNFCallDoubleMethod(env, rect, jm_rect_getWidth), 70 JNFCallDoubleMethod(env, rect, jm_rect_getHeight)); 71 } 72 73 NSRect JavaToNSRect(JNIEnv *env, jobject rect) { 74 return NSMakeRect(JNFCallDoubleMethod(env, rect, jm_rect_getX), 75 JNFCallDoubleMethod(env, rect, jm_rect_getY), 76 JNFCallDoubleMethod(env, rect, jm_rect_getWidth), 77 JNFCallDoubleMethod(env, rect, jm_rect_getHeight)); 78 } 79 80 jobject NSToJavaPoint(JNIEnv *env, NSPoint point) { 81 static JNF_CLASS_CACHE(sjc_Point2DDouble, "java/awt/geom/Point2D$Double"); 82 static JNF_CTOR_CACHE(ctor_Point2DDouble, sjc_Point2DDouble, "(DD)V"); 83 return JNFNewObject(env, ctor_Point2DDouble, (jdouble)point.x, (jdouble)point.y); 84 } 85 86 NSPoint JavaToNSPoint(JNIEnv *env, jobject point) { 87 return NSMakePoint(JNFCallDoubleMethod(env, point, jm_pt_getX), 88 JNFCallDoubleMethod(env, point, jm_pt_getY)); 89 } 90 91 jobject NSToJavaSize(JNIEnv *env, NSSize size) { 92 static JNF_CLASS_CACHE(sjc_Dimension2DDouble, "java/awt/Dimension"); // No Dimension2D$Double :-( 93 static JNF_CTOR_CACHE(ctor_Dimension2DDouble, sjc_Dimension2DDouble, "(II)V"); 94 return JNFNewObject(env, ctor_Dimension2DDouble, (jint)size.width, (jint)size.height); 95 } 96 97 NSSize JavaToNSSize(JNIEnv *env, jobject dimension) { 98 return NSMakeSize(JNFCallDoubleMethod(env, dimension, jm_sz_getWidth), 99 JNFCallDoubleMethod(env, dimension, jm_sz_getHeight)); 100 } 101 102 static NSScreen *primaryScreen(JNIEnv *env) { 103 NSScreen *primaryScreen = [[NSScreen screens] objectAtIndex:0]; 104 if (primaryScreen != nil) return primaryScreen; 105 if (env != NULL) [JNFException raise:env as:kRuntimeException reason:"Failed to convert, no screen."]; 106 return nil; 107 } 108 109 NSPoint ConvertNSScreenPoint(JNIEnv *env, NSPoint point) { 110 point.y = [primaryScreen(env) frame].size.height - point.y; 111 return point; 112 } 113 114 NSRect ConvertNSScreenRect(JNIEnv *env, NSRect rect) { 115 rect.origin.y = [primaryScreen(env) frame].size.height - rect.origin.y - rect.size.height; 116 return rect; 117 }