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