This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Patch: FYI: Implement getMousePosition
- From: Thomas Fitzsimmons <fitzsim at redhat dot com>
- To: java-patches at gcc dot gnu dot org
- Date: Wed, 05 Dec 2007 13:08:36 -0500
- Subject: Patch: FYI: Implement getMousePosition
Hi,
I back-ported this patch from GNU Classpath to implement
Component.getMousePosition() and Container.getMousePosition(boolean).
Tested with "make check-target-libjava" on x86 and x86_64.
Tom
2007-12-05 Thomas Fitzsimmons <fitzsim@redhat.com>
* gnu/java/awt/peer/gtk/GdkGraphicsEnvironment.java,
native/jni/gtk-peer/gnu_java_awt_peer_gtk_GdkGraphicsEnvironment.c
(isWindowUnderMouse): New method.
* include/gnu_java_awt_peer_gtk_GdkGraphicsEnvironment.h:
Regenerate.
* gnu/java/awt/peer/gtk/GtkComponentPeer.java
(getLocationOnScreen): Move WindowPeer section to...
* gnu/java/awt/peer/gtk/GtkWindowPeer.java (getLocationOnScreen):
New method.
* gnu/java/awt/peer/gtk/GtkMouseInfoPeer.java
(isWindowUnderMouse): Implement.
* java/awt/Component.java (getMousePosition): New method.
(getMousePositionHelper): Likewise.
(mouseOverComponent): Likewise.
* java/awt/Container.java (getMousePosition): New method.
(mouseOverComponent): Likewise.
* classpath/lib/java/awt/Component.class,
classpath/lib/java/awt/Component$BltBufferStrategy.class,
classpath/lib/java/awt/Container$GfxPaintAllVisitor.class,
classpath/lib/java/awt/Component$AccessibleAWTComponent
$AccessibleAWTFocusHandler.class,
classpath/lib/java/awt/Component$FlipBufferStrategy.class,
classpath/lib/java/awt/Container$GfxVisitor.class,
classpath/lib/java/awt/Component$AccessibleAWTComponent
$AccessibleAWTComponentHandler.class,
classpath/lib/java/awt/Container$AccessibleAWTContainer
$AccessibleContainerHandler.class,
classpath/lib/java/awt/Container.class,
classpath/lib/java/awt/Container$AccessibleAWTContainer.class,
classpath/lib/java/awt/Container$GfxPrintAllVisitor.class,
classpath/lib/java/awt/Component$AccessibleAWTComponent.class,
classpath/lib/java/awt/Container$GfxPaintVisitor.class,
classpath/lib/java/awt/Container$GfxPrintVisitor.class,
classpath/lib/java/awt/Component$HeavyweightInLightweightListener.class,
classpath/lib/gnu/java/awt/peer/gtk/GtkComponentPeer.class,
classpath/lib/gnu/java/awt/peer/gtk/GdkGraphicsEnvironment.class,
classpath/lib/gnu/java/awt/peer/gtk/GtkMouseInfoPeer.class,
classpath/lib/gnu/java/awt/peer/gtk/GtkWindowPeer.class,
classpath/lib/gnu/java/awt/peer/gtk/GtkComponentPeer
$RepaintTimerTask.class:
Regenerate.
2007-12-05 Thomas Fitzsimmons <fitzsim@redhat.com>
* gnu/java/awt/peer/gtk/GdkGraphicsEnvironment.h: Regenerate.
* gnu/java/awt/peer/gtk/GtkWindowPeer.h: Likewise.
* java/awt/Component.h: Likewise.
* java/awt/Container.h: Likewise.
Index: classpath/native/jni/gtk-peer/gnu_java_awt_peer_gtk_GdkGraphicsEnvironment.c
===================================================================
--- classpath/native/jni/gtk-peer/gnu_java_awt_peer_gtk_GdkGraphicsEnvironment.c (revision 130589)
+++ classpath/native/jni/gtk-peer/gnu_java_awt_peer_gtk_GdkGraphicsEnvironment.c (working copy)
@@ -263,3 +263,37 @@
return retArray;
}
+
+JNIEXPORT jboolean JNICALL
+Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_isWindowUnderMouse
+(JNIEnv *env, jobject obj, jobject windowPeer)
+{
+ GdkDisplay *display = NULL;
+ gint x = 0;
+ gint y = 0;
+ GtkWidget *windowToTest = NULL;
+ GdkWindow *windowAtPointer = NULL;
+ jboolean retVal = JNI_FALSE;
+
+ display = (GdkDisplay *) gtkpeer_get_display (env, obj);
+ g_assert (display != NULL);
+
+ windowToTest = (GtkWidget *) gtkpeer_get_widget (env, windowPeer);
+
+ gdk_threads_enter ();
+
+ windowAtPointer = gdk_display_get_window_at_pointer (display, &x, &y);
+
+ while (windowAtPointer
+ && windowAtPointer != windowToTest->window)
+ windowAtPointer = gdk_window_get_parent (windowAtPointer);
+
+ gdk_threads_leave ();
+
+ if (windowAtPointer)
+ retVal = JNI_TRUE;
+ else
+ retVal = JNI_FALSE;
+
+ return retVal;
+}
Index: classpath/gnu/java/awt/peer/gtk/GdkGraphicsEnvironment.java
===================================================================
--- classpath/gnu/java/awt/peer/gtk/GdkGraphicsEnvironment.java (revision 130589)
+++ classpath/gnu/java/awt/peer/gtk/GdkGraphicsEnvironment.java (working copy)
@@ -155,6 +155,7 @@
* Used by GtkMouseInfoPeer.
*/
native int[] getMouseCoordinates();
+ native boolean isWindowUnderMouse(GtkWindowPeer windowPeer);
public WritableRaster createRaster(ColorModel cm, SampleModel sm)
{
Index: classpath/gnu/java/awt/peer/gtk/GtkComponentPeer.java
===================================================================
--- classpath/gnu/java/awt/peer/gtk/GtkComponentPeer.java (revision 130589)
+++ classpath/gnu/java/awt/peer/gtk/GtkComponentPeer.java (working copy)
@@ -76,7 +76,6 @@
import java.awt.peer.ComponentPeer;
import java.awt.peer.ContainerPeer;
import java.awt.peer.LightweightPeer;
-import java.awt.peer.WindowPeer;
import java.util.Timer;
import java.util.TimerTask;
@@ -251,10 +250,7 @@
public Point getLocationOnScreen ()
{
int point[] = new int[2];
- if( this instanceof WindowPeer )
- gtkWindowGetLocationOnScreen (point);
- else
- gtkWidgetGetLocationOnScreen (point);
+ gtkWidgetGetLocationOnScreen (point);
return new Point (point[0], point[1]);
}
Index: classpath/gnu/java/awt/peer/gtk/GtkWindowPeer.java
===================================================================
--- classpath/gnu/java/awt/peer/gtk/GtkWindowPeer.java (revision 130589)
+++ classpath/gnu/java/awt/peer/gtk/GtkWindowPeer.java (working copy)
@@ -45,6 +45,7 @@
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.KeyboardFocusManager;
+import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.ComponentEvent;
@@ -392,6 +393,13 @@
clickCount, popupTrigger);
}
+ public Point getLocationOnScreen()
+ {
+ int point[] = new int[2];
+ gtkWindowGetLocationOnScreen(point);
+ return new Point(point[0], point[1]);
+ }
+
// We override this to keep it in sync with our internal
// representation.
public Rectangle getBounds()
Index: classpath/gnu/java/awt/peer/gtk/GtkMouseInfoPeer.java
===================================================================
--- classpath/gnu/java/awt/peer/gtk/GtkMouseInfoPeer.java (revision 130589)
+++ classpath/gnu/java/awt/peer/gtk/GtkMouseInfoPeer.java (working copy)
@@ -60,17 +60,7 @@
public boolean isWindowUnderMouse(Window w)
{
- int[] coords = gde.getMouseCoordinates();
- GraphicsDevice[] gds = gde.getScreenDevices();
-
- // Check if the screen of the Window and the cursor match
- if( gds[ coords[0] ] != w.getGraphicsConfiguration().getDevice() )
- return false;
-
- // Return the bounds-check.
- Point p = w.getLocationOnScreen();
- return (coords[1] >= p.x && coords[1] < p.x + w.getWidth() &&
- coords[2] >= p.y && coords[2] < p.y + w.getHeight() );
- }
+ return gde.isWindowUnderMouse((GtkWindowPeer) w.getPeer());
+ }
}
Index: classpath/java/awt/Container.java
===================================================================
--- classpath/java/awt/Container.java (revision 130589)
+++ classpath/java/awt/Container.java (working copy)
@@ -1098,6 +1098,33 @@
}
/**
+ * Returns the mouse pointer position relative to this Container's
+ * top-left corner. If allowChildren is false, the mouse pointer
+ * must be directly over this container. If allowChildren is true,
+ * the mouse pointer may be over this container or any of its
+ * descendents.
+ *
+ * @param allowChildren true to allow descendents, false if pointer
+ * must be directly over Container.
+ *
+ * @return relative mouse pointer position
+ *
+ * @throws HeadlessException if in a headless environment
+ */
+ public Point getMousePosition(boolean allowChildren) throws HeadlessException
+ {
+ return super.getMousePositionHelper(allowChildren);
+ }
+
+ boolean mouseOverComponent(Component component, boolean allowChildren)
+ {
+ if (allowChildren)
+ return isAncestorOf(component);
+ else
+ return component == this;
+ }
+
+ /**
* Returns the component located at the specified point. This is done
* by checking whether or not a child component claims to contain this
* point. The first child component that does is returned. If no
Index: classpath/java/awt/Component.java
===================================================================
--- classpath/java/awt/Component.java (revision 130589)
+++ classpath/java/awt/Component.java (working copy)
@@ -5834,6 +5834,62 @@
}
/**
+ * Returns the mouse pointer position relative to this Component's
+ * top-left corner.
+ *
+ * @return relative mouse pointer position
+ *
+ * @throws HeadlessException if in a headless environment
+ */
+ public Point getMousePosition() throws HeadlessException
+ {
+ return getMousePositionHelper(true);
+ }
+
+ Point getMousePositionHelper(boolean allowChildren) throws HeadlessException
+ {
+ if (GraphicsEnvironment.isHeadless())
+ throw new HeadlessException("can't get mouse position"
+ + " in headless environment");
+ if (!isShowing())
+ return null;
+
+ Component parent = this;
+ int windowRelativeXOffset = 0;
+ int windowRelativeYOffset = 0;
+ while (parent != null && !(parent instanceof Window))
+ {
+ windowRelativeXOffset += parent.getX();
+ windowRelativeYOffset += parent.getY();
+ parent = parent.getParent();
+ }
+ if (parent == null)
+ return null;
+
+ Window window = (Window) parent;
+ if (!Toolkit.getDefaultToolkit()
+ .getMouseInfoPeer().isWindowUnderMouse(window))
+ return null;
+
+ PointerInfo info = MouseInfo.getPointerInfo();
+ Point mouseLocation = info.getLocation();
+ Point windowLocation = window.getLocationOnScreen();
+
+ int x = mouseLocation.x - windowLocation.x;
+ int y = mouseLocation.y - windowLocation.y;
+
+ if (!mouseOverComponent(window.getComponentAt(x, y), allowChildren))
+ return null;
+
+ return new Point(x - windowRelativeXOffset, y - windowRelativeYOffset);
+ }
+
+ boolean mouseOverComponent(Component component, boolean allowChildren)
+ {
+ return component == this;
+ }
+
+ /**
* This method is used to implement transferFocus(). CHILD is the child
* making the request. This is overridden by Container; when called for an
* ordinary component there is no child and so we always return null.
Index: classpath/include/gnu_java_awt_peer_gtk_GdkGraphicsEnvironment.h
===================================================================
--- classpath/include/gnu_java_awt_peer_gtk_GdkGraphicsEnvironment.h (revision 130589)
+++ classpath/include/gnu_java_awt_peer_gtk_GdkGraphicsEnvironment.h (working copy)
@@ -1,10 +1,10 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
+#include <jni.h>
+
#ifndef __gnu_java_awt_peer_gtk_GdkGraphicsEnvironment__
#define __gnu_java_awt_peer_gtk_GdkGraphicsEnvironment__
-#include <jni.h>
-
#ifdef __cplusplus
extern "C"
{
@@ -17,6 +17,7 @@
JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_nativeGetNumFontFamilies (JNIEnv *env, jobject);
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_nativeGetFontFamilies (JNIEnv *env, jobject, jobjectArray);
JNIEXPORT jintArray JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_getMouseCoordinates (JNIEnv *env, jobject);
+JNIEXPORT jboolean JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_isWindowUnderMouse (JNIEnv *env, jobject, jobject);
#ifdef __cplusplus
}