< prev index next >

src/java.desktop/macosx/native/libawt_lwawt/awt/GeomUtilities.m

Print this page
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


   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 }


   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 }
< prev index next >