From b6fa901bc961f54ec9b24c77538f159ad6736d20 Mon Sep 17 00:00:00 2001 From: Thomas Fitzsimmons <fitzsim@redhat.com> Date: Mon, 7 Jul 2003 19:56:04 +0000 Subject: [PATCH] * gnu/java/awt/peer/gtk/GtkTextComponentPeer.java (setCaretPosition, setEditable): Rely entirely on native implementation. (getArgs): Remove. (postTextEvent): New method. (handleEvent): New method. * gnu/java/awt/peer/gtk/GtkTextFieldPeer.java (handleEvent): New method. * java/awt/event/ActionEvent.java (paramString): Fix formatting. * jni/gtk-peer/gnu_java_awt_peer_gtk_GtkEvents.c (keysym_to_awt_keycode): Fix range checks. (generates_key_typed_event): New function. (awt_event_handler): Post AWT_KEY_RELEASED events to event queue. * jni/gtk-peer/gnu_java_awt_peer_gtk_GtkMainThread.c (gtkInit): Store TextComponent's postTextEvent method ID. * jni/gtk-peer/gnu_java_awt_peer_gtk_GtkTextComponentPeer.c (setText): Post TEXT_VALUE_CHANGED event to event queue. From-SVN: r69054 --- libjava/ChangeLog | 21 ++++++ .../awt/peer/gtk/GtkTextComponentPeer.java | 42 +++++------ .../java/awt/peer/gtk/GtkTextFieldPeer.java | 15 ++++ libjava/java/awt/event/ActionEvent.java | 2 +- .../gnu_java_awt_peer_gtk_GtkEvents.c | 74 +++++++++++++++++-- .../gnu_java_awt_peer_gtk_GtkMainThread.c | 8 +- ...u_java_awt_peer_gtk_GtkTextComponentPeer.c | 13 ++++ libjava/jni/gtk-peer/gtkpeer.h | 1 + 8 files changed, 146 insertions(+), 30 deletions(-) diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 585da3becf25..02c9d5b2f339 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,24 @@ +2003-07-07 Thomas Fitzsimmons <fitzsim@redhat.com> + + * gnu/java/awt/peer/gtk/GtkTextComponentPeer.java + (setCaretPosition, setEditable): Rely entirely on native + implementation. + (getArgs): Remove. + (postTextEvent): New method. + (handleEvent): New method. + * gnu/java/awt/peer/gtk/GtkTextFieldPeer.java (handleEvent): New + method. + * java/awt/event/ActionEvent.java (paramString): Fix formatting. + * jni/gtk-peer/gnu_java_awt_peer_gtk_GtkEvents.c + (keysym_to_awt_keycode): Fix range checks. + (generates_key_typed_event): New function. + (awt_event_handler): Post AWT_KEY_RELEASED events to event + queue. + * jni/gtk-peer/gnu_java_awt_peer_gtk_GtkMainThread.c + (gtkInit): Store TextComponent's postTextEvent method ID. + * jni/gtk-peer/gnu_java_awt_peer_gtk_GtkTextComponentPeer.c + (setText): Post TEXT_VALUE_CHANGED event to event queue. + 2003-07-07 Rainer Orth <ro@TechFak.Uni-Bielefeld.DE> * configure.in: Check for usleep declaration. diff --git a/libjava/gnu/java/awt/peer/gtk/GtkTextComponentPeer.java b/libjava/gnu/java/awt/peer/gtk/GtkTextComponentPeer.java index d9ebd40aa72a..102e43973a1b 100644 --- a/libjava/gnu/java/awt/peer/gtk/GtkTextComponentPeer.java +++ b/libjava/gnu/java/awt/peer/gtk/GtkTextComponentPeer.java @@ -39,6 +39,7 @@ exception statement from your version. */ package gnu.java.awt.peer.gtk; import java.awt.peer.*; import java.awt.*; +import java.awt.event.*; public class GtkTextComponentPeer extends GtkComponentPeer implements TextComponentPeer @@ -51,32 +52,14 @@ public class GtkTextComponentPeer extends GtkComponentPeer } public native int getCaretPosition (); - public void setCaretPosition (int pos) - { - set ("text_position", pos); - } + public native void setCaretPosition (int pos); public native int getSelectionStart (); public native int getSelectionEnd (); public native String getText (); public native void select (int start, int end); - - public void setEditable (boolean state) - { - set ("editable", state); - } - + public native void setEditable (boolean state); public native void setText (String text); - - public void getArgs (Component component, GtkArgList args) - { - super.getArgs (component, args); - - TextComponent tc = (TextComponent) component; - - args.add ("text_position", tc.getCaretPosition ()); - args.add ("editable", tc.isEditable ()); - } - + public int getIndexAtPoint(int x, int y) { return 0; // FIXME @@ -91,4 +74,21 @@ public class GtkTextComponentPeer extends GtkComponentPeer { return filter; // FIXME } + + protected void postTextEvent () + { + q.postEvent (new TextEvent (awtComponent, TextEvent.TEXT_VALUE_CHANGED)); + } + + public void handleEvent (AWTEvent e) + { + if (e.getID () == KeyEvent.KEY_TYPED + && ((TextComponent)e.getSource()).isEditable()) + { + KeyEvent ke = (KeyEvent)e; + + if (!ke.isConsumed()) + postTextEvent (); + } + } } diff --git a/libjava/gnu/java/awt/peer/gtk/GtkTextFieldPeer.java b/libjava/gnu/java/awt/peer/gtk/GtkTextFieldPeer.java index 628cdd627aec..05112d16b8b9 100644 --- a/libjava/gnu/java/awt/peer/gtk/GtkTextFieldPeer.java +++ b/libjava/gnu/java/awt/peer/gtk/GtkTextFieldPeer.java @@ -37,6 +37,7 @@ exception statement from your version. */ package gnu.java.awt.peer.gtk; +import java.awt.event.KeyEvent; import java.awt.peer.*; import java.awt.*; @@ -102,4 +103,18 @@ public class GtkTextFieldPeer extends GtkTextComponentPeer { gtkSetFont(((GtkFontPeer)f.getPeer()).getXLFD(), f.getSize()); } + + public void handleEvent (AWTEvent e) + { + if (e.getID () == KeyEvent.KEY_PRESSED) + { + KeyEvent ke = (KeyEvent)e; + + if (!ke.isConsumed() + && ke.getKeyCode() == KeyEvent.VK_ENTER) + postActionEvent (getText(), ke.getModifiers ()); + } + + super.handleEvent (e); + } } diff --git a/libjava/java/awt/event/ActionEvent.java b/libjava/java/awt/event/ActionEvent.java index 4f77fe06eea5..97f980704164 100644 --- a/libjava/java/awt/event/ActionEvent.java +++ b/libjava/java/awt/event/ActionEvent.java @@ -205,7 +205,7 @@ public class ActionEvent extends AWTEvent StringBuffer s = new StringBuffer(id == ACTION_PERFORMED ? "ACTION_PERFORMED,cmd=" : "unknown type,cmd="); - s.append(actionCommand).append(",when=").append(when).append("modifiers"); + s.append(actionCommand).append(",when=").append(when).append(",modifiers"); int len = s.length(); s.setLength(len + 1); if ((modifiers & META_MASK) != 0) diff --git a/libjava/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkEvents.c b/libjava/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkEvents.c index 2573a1823c33..3c400114e4b9 100644 --- a/libjava/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkEvents.c +++ b/libjava/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkEvents.c @@ -90,10 +90,10 @@ keysym_to_awt_keycode (guint keyval) vk = gdk_keyval_to_upper (keyval); - if (vk <= 0x41 && vk <= 0x5A) /* VK_A through VK_Z */ + if (vk >= 0x41 && vk <= 0x5A) /* VK_A through VK_Z */ return vk; - if (vk <= 0x30 && vk <= 39) /* VK_0 through VK_9 */ + if (vk >= 0x30 && vk <= 0x39) /* VK_0 through VK_9 */ return vk; switch (vk) @@ -232,6 +232,24 @@ keysym_to_awt_keycode (guint keyval) } } +static int +generates_key_typed_event (guint keyval) +{ + guint vk; + + vk = gdk_keyval_to_upper (keyval); + + if ((vk >= 0x20 && vk <= 0x7e) /* Most printable keysyms on a + standard US keyboard. */ + || (vk >= 0xFF9F && vk <= 0xFFB9) /* Numeric Keypad keysyms. */ + || vk == GDK_BackSpace + || vk == GDK_Delete + || vk == GDK_Return) + return 1; + else + return 0; +} + void awt_event_handler (GdkEvent *event) { @@ -275,6 +293,7 @@ awt_event_handler (GdkEvent *event) || event->type == GDK_CONFIGURE || event->type == GDK_EXPOSE || event->type == GDK_KEY_PRESS + || event->type == GDK_KEY_RELEASE || event->type == GDK_FOCUS_CHANGE || event->type == GDK_MOTION_NOTIFY) && gdk_property_get (event->any.window, @@ -468,29 +487,70 @@ awt_event_handler (GdkEvent *event) NULL, NULL, (guchar **)&obj_ptr); - + /* if (grab && GTK_WIDGET_HAS_DEFAULT (widget) ) */ /* { */ (*gdk_env)->CallVoidMethod (gdk_env, *obj_ptr, postKeyEventID, (jint) AWT_KEY_PRESSED, (jlong) event->key.time, - state_to_awt_mods (event->key.state), - keysym_to_awt_keycode (event->key.keyval), + state_to_awt_mods (event->key.state), + keysym_to_awt_keycode (event->key.keyval), (jchar) (event->key.length) ? event->key.string[0] : AWT_KEY_CHAR_UNDEFINED); - if (event->key.length) + + if (event->key.length + && generates_key_typed_event(event->key.keyval)) (*gdk_env)->CallVoidMethod (gdk_env, *obj_ptr, postKeyEventID, (jint) AWT_KEY_TYPED, (jlong) event->key.time, - state_to_awt_mods (event->key.state), + state_to_awt_mods (event->key.state), VK_UNDEFINED, (jchar) event->key.string[0]); } } break; + case GDK_KEY_RELEASE: + { + GtkWidget *widget; + GtkWindow *window; + + gdk_window_get_user_data (event->any.window, (void **) &widget); + + window = GTK_WINDOW (gtk_widget_get_ancestor (widget, + GTK_TYPE_WINDOW)); + if (window + && GTK_WIDGET_IS_SENSITIVE (window) + && window->focus_widget + && GTK_WIDGET_IS_SENSITIVE (window->focus_widget) + && window->focus_widget->window) + { + gtk_widget_activate (window->focus_widget); + gdk_property_get (window->focus_widget->window, + gdk_atom_intern ("_GNU_GTKAWT_ADDR", FALSE), + gdk_atom_intern ("CARDINAL", FALSE), + 0, + sizeof (jobject), + FALSE, + NULL, + NULL, + NULL, + (guchar **)&obj_ptr); + + (*gdk_env)->CallVoidMethod (gdk_env, *obj_ptr, + postKeyEventID, + (jint) AWT_KEY_RELEASED, + (jlong) event->key.time, + state_to_awt_mods (event->key.state), + keysym_to_awt_keycode (event->key.keyval), + (jchar) (event->key.length) ? + event->key.string[0] : + AWT_KEY_CHAR_UNDEFINED); + } + } + break; case GDK_FOCUS_CHANGE: (*gdk_env)->CallVoidMethod (gdk_env, *obj_ptr, postFocusEventID, diff --git a/libjava/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkMainThread.c b/libjava/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkMainThread.c index cd4d7e2fe7b5..83fab1cb8c08 100644 --- a/libjava/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkMainThread.c +++ b/libjava/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkMainThread.c @@ -54,6 +54,7 @@ jmethodID postFocusEventID; jmethodID postAdjustmentEventID; jmethodID postItemEventID; jmethodID postListItemEventID; +jmethodID postTextEventID; JNIEnv *gdk_env; #ifdef PORTABLE_NATIVE_SYNC @@ -72,7 +73,8 @@ Java_gnu_java_awt_peer_gtk_GtkMainThread_gtkInit (JNIEnv *env, jclass clazz) char **argv; char *homedir, *rcpath = NULL; /* jclass gtkgenericpeer; */ - jclass gtkcomponentpeer, gtkwindowpeer, gtkscrollbarpeer, gtklistpeer, gtkmenuitempeer; + jclass gtkcomponentpeer, gtkwindowpeer, gtkscrollbarpeer, gtklistpeer, + gtkmenuitempeer, gtktextcomponentpeer; NSA_INIT (env, clazz); @@ -133,6 +135,7 @@ Java_gnu_java_awt_peer_gtk_GtkMainThread_gtkInit (JNIEnv *env, jclass clazz) "gnu/java/awt/peer/gtk/GtkScrollbarPeer"); gtklistpeer = (*env)->FindClass (env, "gnu/java/awt/peer/gtk/GtkListPeer"); gtkmenuitempeer = (*env)->FindClass (env, "gnu/java/awt/peer/gtk/GtkMenuItemPeer"); + gtktextcomponentpeer = (*env)->FindClass (env, "gnu/java/awt/peer/gtk/GtkTextComponentPeer"); /* gdkColor = (*env)->FindClass (env, */ /* "gnu/java/awt/peer/gtk/GdkColor"); */ /* gdkColorID = (*env)->GetMethodID (env, gdkColor, "<init>", "(III)V"); */ @@ -162,6 +165,9 @@ Java_gnu_java_awt_peer_gtk_GtkMainThread_gtkInit (JNIEnv *env, jclass clazz) postListItemEventID = (*env)->GetMethodID (env, gtklistpeer, "postItemEvent", "(II)V"); + postTextEventID = (*env)->GetMethodID (env, gtktextcomponentpeer, + "postTextEvent", + "()V"); } /* diff --git a/libjava/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkTextComponentPeer.c b/libjava/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkTextComponentPeer.c index 8cdff34f11ba..de590352dd15 100644 --- a/libjava/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkTextComponentPeer.c +++ b/libjava/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkTextComponentPeer.c @@ -361,6 +361,7 @@ Java_gnu_java_awt_peer_gtk_GtkTextComponentPeer_setText const char *str; GtkWidget *text = NULL; // type of GtkTextView (TextArea) GtkTextBuffer *buf; + jobject *obj_ptr; ptr = NSA_GET_PTR (env, obj); str = (*env)->GetStringUTFChars (env, contents, NULL); @@ -370,6 +371,18 @@ Java_gnu_java_awt_peer_gtk_GtkTextComponentPeer_setText if (GTK_IS_EDITABLE (ptr)) { gtk_entry_set_text (GTK_ENTRY (ptr), str); + + if (gdk_property_get (GTK_WIDGET(ptr)->window, + gdk_atom_intern ("_GNU_GTKAWT_ADDR", FALSE), + gdk_atom_intern ("CARDINAL", FALSE), + 0, + sizeof (jobject), + FALSE, + NULL, + NULL, + NULL, + (guchar **)&obj_ptr)) + (*gdk_env)->CallVoidMethod (gdk_env, *obj_ptr, postTextEventID); } else { diff --git a/libjava/jni/gtk-peer/gtkpeer.h b/libjava/jni/gtk-peer/gtkpeer.h index fa1f19b5a1df..1cfa6c472cde 100644 --- a/libjava/jni/gtk-peer/gtkpeer.h +++ b/libjava/jni/gtk-peer/gtkpeer.h @@ -266,6 +266,7 @@ extern jmethodID postFocusEventID; extern jmethodID postAdjustmentEventID; extern jmethodID postItemEventID; extern jmethodID postListItemEventID; +extern jmethodID postTextEventID; extern jmethodID syncAttrsID; extern jclass gdkColor; extern jmethodID gdkColorID; -- GitLab