diff options
author | Dmitri Trembovetski <[email protected]> | 2009-05-26 21:24:24 +0000 |
---|---|---|
committer | Dmitri Trembovetski <[email protected]> | 2009-05-26 21:24:24 +0000 |
commit | 90c95605aa73985289600ad1f70d5ae29e69dcfc (patch) | |
tree | bf50ee60a5a6792eca4ba86b7124a8430e4a86e2 /src/newt | |
parent | 6aaddcca9dcefd240eb616e58ecffc1371d8f19b (diff) |
Newt: implemented simple Window focus management (focusGained/focusLost/requestFocus for Window). Currently only implemented on Windows.
git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/svn-server-sync/jogl/branches/JOGL_2_SANDBOX@1916 232f8b59-042b-4e1e-8c03-345bb8c30851
Diffstat (limited to 'src/newt')
-rwxr-xr-x | src/newt/classes/com/sun/javafx/newt/Window.java | 15 | ||||
-rw-r--r-- | src/newt/classes/com/sun/javafx/newt/WindowEvent.java | 6 | ||||
-rw-r--r-- | src/newt/classes/com/sun/javafx/newt/WindowListener.java | 2 | ||||
-rw-r--r-- | src/newt/classes/com/sun/javafx/newt/opengl/GLWindow.java | 6 | ||||
-rwxr-xr-x | src/newt/classes/com/sun/javafx/newt/windows/WindowsWindow.java | 23 | ||||
-rw-r--r-- | src/newt/native/KeyEvent.h | 2 | ||||
-rwxr-xr-x | src/newt/native/WindowsWindow.c | 106 |
7 files changed, 119 insertions, 41 deletions
diff --git a/src/newt/classes/com/sun/javafx/newt/Window.java b/src/newt/classes/com/sun/javafx/newt/Window.java index 29d4b6195..d649b53a4 100755 --- a/src/newt/classes/com/sun/javafx/newt/Window.java +++ b/src/newt/classes/com/sun/javafx/newt/Window.java @@ -221,7 +221,8 @@ public abstract class Window implements NativeWindow return undecorated; } - + public void requestFocus() { + } // // NativeWindow impl @@ -551,7 +552,7 @@ public abstract class Window implements NativeWindow private ArrayList keyListeners = new ArrayList(); protected void sendKeyEvent(int eventType, int modifiers, int keyCode, char keyChar) { - KeyEvent e = new KeyEvent(true, eventType, this, System.currentTimeMillis(), + KeyEvent e = new KeyEvent(true, eventType, this, System.currentTimeMillis(), modifiers, keyCode, keyChar); if(DEBUG_KEY_EVENT) { System.out.println("sendKeyEvent: "+e); @@ -627,8 +628,16 @@ public abstract class Window implements NativeWindow case WindowEvent.EVENT_WINDOW_DESTROY_NOTIFY: l.windowDestroyNotify(e); break; + case WindowEvent.EVENT_WINDOW_GAINED_FOCUS: + l.windowGainedFocus(e); + break; + case WindowEvent.EVENT_WINDOW_LOST_FOCUS: + l.windowLostFocus(e); + break; default: - throw new NativeWindowException("Unexpected window event type " + e.getEventType()); + throw + new NativeWindowException("Unexpected window event type " + + e.getEventType()); } } } diff --git a/src/newt/classes/com/sun/javafx/newt/WindowEvent.java b/src/newt/classes/com/sun/javafx/newt/WindowEvent.java index a502e912c..634672f32 100644 --- a/src/newt/classes/com/sun/javafx/newt/WindowEvent.java +++ b/src/newt/classes/com/sun/javafx/newt/WindowEvent.java @@ -36,7 +36,9 @@ package com.sun.javafx.newt; public class WindowEvent extends Event { public static final int EVENT_WINDOW_RESIZED = 100; public static final int EVENT_WINDOW_MOVED = 101; - public static final int EVENT_WINDOW_DESTROY_NOTIFY = 102; + public static final int EVENT_WINDOW_DESTROY_NOTIFY = 102; + public static final int EVENT_WINDOW_GAINED_FOCUS = 103; + public static final int EVENT_WINDOW_LOST_FOCUS = 104; public WindowEvent(int eventType, Window source, long when) { this(false, eventType, source, when); @@ -51,6 +53,8 @@ public class WindowEvent extends Event { case EVENT_WINDOW_RESIZED: return "WINDOW_RESIZED"; case EVENT_WINDOW_MOVED: return "WINDOW_MOVED"; case EVENT_WINDOW_DESTROY_NOTIFY: return "EVENT_WINDOW_DESTROY_NOTIFY"; + case EVENT_WINDOW_GAINED_FOCUS: return "EVENT_WINDOW_GAINED_FOCUS"; + case EVENT_WINDOW_LOST_FOCUS: return "EVENT_WINDOW_LOST_FOCUS"; default: return "unknown (" + type + ")"; } } diff --git a/src/newt/classes/com/sun/javafx/newt/WindowListener.java b/src/newt/classes/com/sun/javafx/newt/WindowListener.java index e8423cbcc..81624fa75 100644 --- a/src/newt/classes/com/sun/javafx/newt/WindowListener.java +++ b/src/newt/classes/com/sun/javafx/newt/WindowListener.java @@ -37,4 +37,6 @@ public interface WindowListener extends EventListener { public void windowResized(WindowEvent e); public void windowMoved(WindowEvent e); public void windowDestroyNotify(WindowEvent e); + public void windowGainedFocus(WindowEvent e); + public void windowLostFocus(WindowEvent e); } diff --git a/src/newt/classes/com/sun/javafx/newt/opengl/GLWindow.java b/src/newt/classes/com/sun/javafx/newt/opengl/GLWindow.java index 196396857..8f7c309f8 100644 --- a/src/newt/classes/com/sun/javafx/newt/opengl/GLWindow.java +++ b/src/newt/classes/com/sun/javafx/newt/opengl/GLWindow.java @@ -83,6 +83,12 @@ public class GLWindow extends Window implements GLAutoDrawable { public void windowMoved(WindowEvent e) { } + public void windowGainedFocus(WindowEvent e) { + } + + public void windowLostFocus(WindowEvent e) { + } + public void windowDestroyNotify(WindowEvent e) { sendDispose = true; } diff --git a/src/newt/classes/com/sun/javafx/newt/windows/WindowsWindow.java b/src/newt/classes/com/sun/javafx/newt/windows/WindowsWindow.java index 2073c5d1b..6c7d8ab56 100755 --- a/src/newt/classes/com/sun/javafx/newt/windows/WindowsWindow.java +++ b/src/newt/classes/com/sun/javafx/newt/windows/WindowsWindow.java @@ -134,6 +134,14 @@ public class WindowsWindow extends Window { } // @Override + public void requestFocus() { + super.requestFocus(); + if (windowHandle != 0L) { + requestFocus(windowHandle); + } + } + + // @Override public void setTitle(String title) { if (title == null) { title = ""; @@ -188,6 +196,7 @@ public class WindowsWindow extends Window { private native boolean setFullScreen0(long windowHandle, boolean fullscreen); private static native void setPosition(long windowHandle, int x, int y); private static native void setTitle(long windowHandle, String title); + private static native void requestFocus(long windowHandle); private void sizeChanged(int newWidth, int newHeight) { width = newWidth; @@ -200,4 +209,18 @@ public class WindowsWindow extends Window { y = newY; sendWindowEvent(WindowEvent.EVENT_WINDOW_MOVED); } + + /** + * + * @param focusOwner if focusGained is true, focusOwner is the previous + * focus owner, if focusGained is false, focusOwner is the new focus owner + * @param focusGained + */ + private void focusChanged(long focusOwner, boolean focusGained) { + if (focusGained) { + sendWindowEvent(WindowEvent.EVENT_WINDOW_GAINED_FOCUS); + } else { + sendWindowEvent(WindowEvent.EVENT_WINDOW_LOST_FOCUS); + } + } } diff --git a/src/newt/native/KeyEvent.h b/src/newt/native/KeyEvent.h index eb5547b21..e1881366f 100644 --- a/src/newt/native/KeyEvent.h +++ b/src/newt/native/KeyEvent.h @@ -4,7 +4,7 @@ #define EVENT_KEY_PRESSED 300 #define EVENT_KEY_RELEASED 301 -// Send by Java: EVENT_KEY_TYPED 302 +#define EVENT_KEY_TYPED 302 #define VK_ENTER '\n' #define VK_BACK_SPACE '\b' diff --git a/src/newt/native/WindowsWindow.c b/src/newt/native/WindowsWindow.c index d4de158dd..3f8ba5c87 100755 --- a/src/newt/native/WindowsWindow.c +++ b/src/newt/native/WindowsWindow.c @@ -75,6 +75,7 @@ typedef int intptr_t; static jmethodID sizeChangedID = NULL; static jmethodID positionChangedID = NULL; +static jmethodID focusChangedID = NULL; static jmethodID windowDestroyNotifyID = NULL; static jmethodID windowDestroyedID = NULL; static jmethodID sendMouseEventID = NULL; @@ -94,20 +95,14 @@ static jchar* GetNullTerminatedStringChars(JNIEnv* env, jstring str) return strChars; } -static jint ConvertModifiers(WPARAM wParam) { +static jint GetModifiers() { jint modifiers = 0; - if ((wParam & MK_CONTROL) != 0) { + if (HIBYTE(GetKeyState(VK_CONTROL)) != 0) { modifiers |= EVENT_CTRL_MASK; } - if ((wParam & MK_SHIFT) != 0) { + if (HIBYTE(GetKeyState(VK_SHIFT)) != 0) { modifiers |= EVENT_SHIFT_MASK; } - if ((wParam & MK_CONTROL) != 0) { - modifiers |= EVENT_CTRL_MASK; - } - if ((wParam & MK_CONTROL) != 0) { - modifiers |= EVENT_CTRL_MASK; - } if (HIBYTE(GetKeyState(VK_MENU)) != 0) { modifiers |= EVENT_ALT_MASK; } @@ -124,6 +119,25 @@ static jint ConvertModifiers(WPARAM wParam) { return modifiers; } +static int WmChar(JNIEnv *env, jobject window, UINT character, UINT repCnt, + UINT flags, BOOL system) +{ + // The Alt modifier is reported in the 29th bit of the lParam, + // i.e., it is the 13th bit of `flags' (which is HIWORD(lParam)). + BOOL alt_is_down = (flags & (1<<13)) != 0; + if (system && alt_is_down) { + if (character == VK_SPACE) { + return 1; + } + } + (*env)->CallVoidMethod(env, window, sendKeyEventID, + (jint) EVENT_KEY_TYPED, + GetModifiers(), + (jint) -1, + (jchar) character); + return 1; +} + static LRESULT CALLBACK wndProc(HWND wnd, UINT message, WPARAM wParam, LPARAM lParam) { @@ -152,34 +166,25 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, (*env)->CallVoidMethod(env, window, windowDestroyedID); break; - // Windows does the translation between key codes and key chars by - // producing synthetic WM_CHAR messages in response to WM_KEYDOWN - // messages. The Java level contains the state machine to assemble - // these multiple events into a single event. - case WM_CHAR: - // NOTE that the (1 << 31) bit is meaningless regardless of - // what the docs for WM_CHAR say; we never receive a WM_CHAR - // message in response to translating a WM_KEYUP message - (*env)->CallVoidMethod(env, window, sendKeyEventID, - (jint) EVENT_KEY_PRESSED, - // FIXME: need to interpret the key events in order to compute the modifiers - (jint) 0, - (jint) -1, - (jchar) wParam); - useDefWindowProc = 1; + case WM_SYSCHAR: + useDefWindowProc = WmChar(env, window, wParam, + LOWORD(lParam), HIWORD(lParam), FALSE); break; + case WM_CHAR: + useDefWindowProc = WmChar(env, window, wParam, + LOWORD(lParam), HIWORD(lParam), TRUE); + break; + case WM_KEYDOWN: - // FIXME: need to interpret the key events in order to compute the modifiers (*env)->CallVoidMethod(env, window, sendKeyEventID, (jint) EVENT_KEY_PRESSED, - (jint) 0, (jint) wParam, (jchar) -1); + GetModifiers(), (jint) wParam, (jchar) -1); useDefWindowProc = 1; break; case WM_KEYUP: - // FIXME: need to interpret the key events in order to compute the modifiers (*env)->CallVoidMethod(env, window, sendKeyEventID, (jint) EVENT_KEY_RELEASED, - (jint) 0, (jint) wParam, (jchar) -1); + GetModifiers(), (jint) wParam, (jchar) -1); useDefWindowProc = 1; break; @@ -191,7 +196,7 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, case WM_LBUTTONDOWN: (*env)->CallVoidMethod(env, window, sendMouseEventID, (jint) EVENT_MOUSE_PRESSED, - ConvertModifiers(wParam), + GetModifiers(), (jint) LOWORD(lParam), (jint) HIWORD(lParam), (jint) 1, (jint) 0); useDefWindowProc = 1; @@ -200,7 +205,7 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, case WM_LBUTTONUP: (*env)->CallVoidMethod(env, window, sendMouseEventID, (jint) EVENT_MOUSE_RELEASED, - ConvertModifiers(wParam), + GetModifiers(), (jint) LOWORD(lParam), (jint) HIWORD(lParam), (jint) 1, (jint) 0); useDefWindowProc = 1; @@ -209,7 +214,7 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, case WM_MBUTTONDOWN: (*env)->CallVoidMethod(env, window, sendMouseEventID, (jint) EVENT_MOUSE_PRESSED, - ConvertModifiers(wParam), + GetModifiers(), (jint) LOWORD(lParam), (jint) HIWORD(lParam), (jint) 2, (jint) 0); useDefWindowProc = 1; @@ -218,7 +223,7 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, case WM_MBUTTONUP: (*env)->CallVoidMethod(env, window, sendMouseEventID, (jint) EVENT_MOUSE_RELEASED, - ConvertModifiers(wParam), + GetModifiers(), (jint) LOWORD(lParam), (jint) HIWORD(lParam), (jint) 2, (jint) 0); useDefWindowProc = 1; @@ -227,7 +232,7 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, case WM_RBUTTONDOWN: (*env)->CallVoidMethod(env, window, sendMouseEventID, (jint) EVENT_MOUSE_PRESSED, - ConvertModifiers(wParam), + GetModifiers(), (jint) LOWORD(lParam), (jint) HIWORD(lParam), (jint) 3, (jint) 0); useDefWindowProc = 1; @@ -236,7 +241,7 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, case WM_RBUTTONUP: (*env)->CallVoidMethod(env, window, sendMouseEventID, (jint) EVENT_MOUSE_RELEASED, - ConvertModifiers(wParam), + GetModifiers(), (jint) LOWORD(lParam), (jint) HIWORD(lParam), (jint) 3, (jint) 0); useDefWindowProc = 1; @@ -245,7 +250,7 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, case WM_MOUSEMOVE: (*env)->CallVoidMethod(env, window, sendMouseEventID, (jint) EVENT_MOUSE_MOVED, - ConvertModifiers(wParam), + GetModifiers(), (jint) LOWORD(lParam), (jint) HIWORD(lParam), (jint) 0, (jint) 0); useDefWindowProc = 1; @@ -261,13 +266,25 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, ScreenToClient(wnd, &eventPt); (*env)->CallVoidMethod(env, window, sendMouseEventID, (jint) EVENT_MOUSE_WHEEL_MOVED, - ConvertModifiers(wParam), + GetModifiers(), (jint) eventPt.x, (jint) eventPt.y, (jint) 0, (jint) GET_WHEEL_DELTA_WPARAM(wParam)); useDefWindowProc = 1; break; } + case WM_SETFOCUS: + (*env)->CallVoidMethod(env, window, focusChangedID, + (jlong)wParam, JNI_TRUE); + useDefWindowProc = 1; + break; + + case WM_KILLFOCUS: + (*env)->CallVoidMethod(env, window, focusChangedID, + (jlong)wParam, JNI_FALSE); + useDefWindowProc = 1; + break; + case WM_MOVE: (*env)->CallVoidMethod(env, window, positionChangedID, (jint)LOWORD(lParam), (jint)HIWORD(lParam)); @@ -300,12 +317,14 @@ JNIEXPORT jboolean JNICALL Java_com_sun_javafx_newt_windows_WindowsWindow_initID { sizeChangedID = (*env)->GetMethodID(env, clazz, "sizeChanged", "(II)V"); positionChangedID = (*env)->GetMethodID(env, clazz, "positionChanged", "(II)V"); + focusChangedID = (*env)->GetMethodID(env, clazz, "focusChanged", "(JZ)V"); windowDestroyNotifyID = (*env)->GetMethodID(env, clazz, "windowDestroyNotify", "()V"); windowDestroyedID = (*env)->GetMethodID(env, clazz, "windowDestroyed", "()V"); sendMouseEventID = (*env)->GetMethodID(env, clazz, "sendMouseEvent", "(IIIIII)V"); sendKeyEventID = (*env)->GetMethodID(env, clazz, "sendKeyEvent", "(IIIC)V"); if (sizeChangedID == NULL || positionChangedID == NULL || + focusChangedID == NULL || windowDestroyNotifyID == NULL || windowDestroyedID == NULL || sendMouseEventID == NULL || @@ -649,3 +668,18 @@ JNIEXPORT void JNICALL Java_com_sun_javafx_newt_windows_WindowsWindow_setTitle } } } + +/* + * Class: com_sun_javafx_newt_windows_WindowsWindow + * Method: requestFocus + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_com_sun_javafx_newt_windows_WindowsWindow_requestFocus + (JNIEnv *env, jclass clazz, jlong window) +{ + HWND hwnd = (HWND)window; + + if (IsWindow(hwnd)) { + SetFocus(hwnd); + } +} |