1 /*
   2  * Copyright (c) 2003, 2014, 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 #include <windowsx.h>
  27 #include <jni.h>
  28 #include <jni_util.h>
  29 #include "awt.h"
  30 #include "awt_Object.h"
  31 #include "awt_Component.h"
  32 
  33 extern "C" {
  34 
  35 /*
  36  * Class:     sun_awt_DefaultMouseInfoPeer
  37  * Method:    isWindowUnderMouse
  38  * Signature: (Ljava/awt/Window)Z
  39  */
  40 JNIEXPORT jboolean JNICALL
  41 Java_sun_awt_DefaultMouseInfoPeer_isWindowUnderMouse(JNIEnv *env, jclass cls,
  42                                                         jobject window)
  43 {
  44     POINT pt;
  45 
  46     if (env->EnsureLocalCapacity(1) < 0) {
  47         return JNI_FALSE;
  48     }
  49 
  50     jobject winPeer = AwtObject::GetPeerForTarget(env, window);
  51     PDATA pData;
  52     pData = JNI_GET_PDATA(winPeer);
  53     env->DeleteLocalRef(winPeer);
  54     if (pData == NULL) {
  55         return JNI_FALSE;
  56     }
  57 
  58     AwtComponent * ourWindow = (AwtComponent *)pData;
  59     HWND hwnd = ourWindow->GetHWnd();
  60     VERIFY(::GetCursorPos(&pt));
  61 
  62     AwtComponent * componentFromPoint = AwtComponent::GetComponent(::WindowFromPoint(pt));
  63 
  64     while (componentFromPoint != NULL
  65         && componentFromPoint->GetHWnd() != hwnd
  66         && !AwtComponent::IsTopLevelHWnd(componentFromPoint->GetHWnd()))
  67     {
  68         componentFromPoint = componentFromPoint->GetParent();
  69     }
  70 
  71     return ((componentFromPoint != NULL) && (componentFromPoint->GetHWnd() == hwnd)) ? JNI_TRUE : JNI_FALSE;
  72 
  73 }
  74 
  75 /*
  76  * Class:     sun_awt_DefaultMouseInfoPeer
  77  * Method:    fillPointWithCoords
  78  * Signature: (Ljava/awt/Point)I
  79  */
  80 JNIEXPORT jint JNICALL
  81 Java_sun_awt_DefaultMouseInfoPeer_fillPointWithCoords(JNIEnv *env, jclass cls, jobject point)
  82 {
  83     static jclass pointClass = NULL;
  84     static jfieldID xID, yID;
  85     POINT pt;
  86 
  87     VERIFY(::GetCursorPos(&pt));
  88     if (pointClass == NULL) {
  89         jclass pointClassLocal = env->FindClass("java/awt/Point");
  90         DASSERT(pointClassLocal != NULL);
  91         if (pointClassLocal == NULL) {
  92             return (jint)0;
  93         }
  94         pointClass = (jclass)env->NewGlobalRef(pointClassLocal);
  95         env->DeleteLocalRef(pointClassLocal);
  96     }
  97 
  98     int screen = AwtWin32GraphicsDevice::GetDefaultDeviceIndex();
  99     Devices::InstanceAccess devices;
 100     AwtWin32GraphicsDevice *device = devices->GetDevice(screen);
 101 
 102     xID = env->GetFieldID(pointClass, "x", "I");
 103     CHECK_NULL_RETURN(xID, (jint)0);
 104     yID = env->GetFieldID(pointClass, "y", "I");
 105     CHECK_NULL_RETURN(yID, (jint)0);
 106 
 107     int x = (device == NULL) ? pt.x : device->ScaleDownX(pt.x);  
 108     int y = (device == NULL) ? pt.y : device->ScaleDownY(pt.y);  
 109 
 110     env->SetIntField(point, xID, x);
 111     env->SetIntField(point, yID, y);
 112 
 113     // Always return 0 on Windows: we assume there's always a
 114     // virtual screen device used.
 115     return (jint)0;
 116 }
 117 
 118 } // extern "C"