This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
[gui][PATCH] allow Panels to become focused
- From: Thomas Fitzsimmons <fitzsim at redhat dot com>
- To: java-patches at gcc dot gnu dot org
- Date: Sat, 10 Jul 2004 01:15:39 -0400
- Subject: [gui][PATCH] allow Panels to become focused
Hi,
I'm committing this patch to java-gui-branch. It allows Panels to
become focused when they are clicked. The patch also contains
miscellaneous paramString and focus handling fixes.
Tom
2004-07-10 Thomas Fitzsimmons <fitzsim@redhat.com>
* gnu/java/awt/peer/gtk/GtkPanelPeer.java (handleEvent): Handle
MOUSE_PRESSED event.
* java/awt/Component.java (requestFocus()): Handle Panel
specially. Post FOCUS_LOST event on opposite component.
(requestFocus(boolean)): Likewise.
(requestFocusInWindow(boolean)): Likewise.
(paramString): Reorder dimension fields.
* java/awt/Container.java (paramString): Fix string format.
* java/awt/DefaultKeyboardFocusManager.java (dispatchEvent):
Handle FOCUS_LOST events. Don't handle Windows specially. Only
process key events if the focus owner is non-null.
(dispatchKeyEvent): Likewise.
* java/awt/Frame.java (paramString): Fix formatting.
(generateName): New method.
(getUniqueLong): Likewise.
* java/awt/KeyboardFocusManager.java (getFocusOwner): Check if
the temporary focus owner is null. If so, return the permanent
focus owner.
* java/awt/Panel.java (generateName): New method.
(getUniqueLong): Likewise.
* jni/gtk-peer/gnu_java_awt_peer_gtk_GtkWindowPeer.c: Rework
signal handling to make callbacks more specific.
? jawt_md.h
Index: gnu/java/awt/peer/gtk/GtkPanelPeer.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/awt/peer/gtk/GtkPanelPeer.java,v
retrieving revision 1.3.8.1
diff -u -r1.3.8.1 GtkPanelPeer.java
--- gnu/java/awt/peer/gtk/GtkPanelPeer.java 9 Apr 2004 22:29:01 -0000 1.3.8.1
+++ gnu/java/awt/peer/gtk/GtkPanelPeer.java 10 Jul 2004 04:53:09 -0000
@@ -38,7 +38,9 @@
package gnu.java.awt.peer.gtk;
+import java.awt.AWTEvent;
import java.awt.Panel;
+import java.awt.event.MouseEvent;
import java.awt.peer.PanelPeer;
public class GtkPanelPeer extends GtkContainerPeer
@@ -51,4 +53,17 @@
{
super (p);
}
+
+ public void handleEvent (AWTEvent event)
+ {
+ int id = event.getID();
+
+ switch (id)
+ {
+ case MouseEvent.MOUSE_PRESSED:
+ awtComponent.requestFocusInWindow ();
+ break;
+ }
+ super.handleEvent (event);
+ }
}
Index: java/awt/Component.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/awt/Component.java,v
retrieving revision 1.37.2.13
diff -u -r1.37.2.13 Component.java
--- java/awt/Component.java 25 Jun 2004 17:13:32 -0000 1.37.2.13
+++ java/awt/Component.java 10 Jul 2004 04:53:25 -0000
@@ -3643,7 +3643,8 @@
Window toplevel = (Window) parent;
if (toplevel.isFocusableWindow ())
{
- if (peer != null)
+ if (peer != null
+ && !(this instanceof Panel))
// This call will cause a FOCUS_GAINED event to be
// posted to the system event queue if the native
// windowing system grants the focus request.
@@ -3654,7 +3655,20 @@
// lightweight component. In either case we want to
// post a FOCUS_GAINED event.
EventQueue eq = Toolkit.getDefaultToolkit ().getSystemEventQueue ();
- eq.postEvent (new FocusEvent(this, FocusEvent.FOCUS_GAINED));
+ synchronized (eq)
+ {
+ KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
+ Component currentFocusOwner = manager.getGlobalPermanentFocusOwner ();
+ if (currentFocusOwner != null)
+ {
+ eq.postEvent (new FocusEvent(currentFocusOwner, FocusEvent.FOCUS_LOST,
+ false, this));
+ eq.postEvent (new FocusEvent(this, FocusEvent.FOCUS_GAINED, false,
+ currentFocusOwner));
+ }
+ else
+ eq.postEvent (new FocusEvent(this, FocusEvent.FOCUS_GAINED, false));
+ }
}
}
else
@@ -3716,7 +3730,8 @@
Window toplevel = (Window) parent;
if (toplevel.isFocusableWindow ())
{
- if (peer != null)
+ if (peer != null
+ && !(this instanceof Panel))
// This call will cause a FOCUS_GAINED event to be
// posted to the system event queue if the native
// windowing system grants the focus request.
@@ -3727,7 +3742,23 @@
// lightweight component. In either case we want to
// post a FOCUS_GAINED event.
EventQueue eq = Toolkit.getDefaultToolkit ().getSystemEventQueue ();
- eq.postEvent (new FocusEvent(this, FocusEvent.FOCUS_GAINED, temporary));
+ synchronized (eq)
+ {
+ KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
+ Component currentFocusOwner = manager.getGlobalPermanentFocusOwner ();
+ if (currentFocusOwner != null)
+ {
+ eq.postEvent (new FocusEvent(currentFocusOwner,
+ FocusEvent.FOCUS_LOST,
+ temporary, this));
+ eq.postEvent (new FocusEvent(this,
+ FocusEvent.FOCUS_GAINED,
+ temporary,
+ currentFocusOwner));
+ }
+ else
+ eq.postEvent (new FocusEvent(this, FocusEvent.FOCUS_GAINED, temporary));
+ }
}
}
else
@@ -3820,7 +3851,9 @@
// Check if top-level ancestor is currently focused window.
if (focusedWindow == toplevel)
{
- if (peer != null)
+ if (peer != null
+ && !(this instanceof Panel)
+ && !(this instanceof Window))
// This call will cause a FOCUS_GAINED event to be
// posted to the system event queue if the native
// windowing system grants the focus request.
@@ -3831,7 +3864,19 @@
// lightweight component. In either case we want to
// post a FOCUS_GAINED event.
EventQueue eq = Toolkit.getDefaultToolkit ().getSystemEventQueue ();
- eq.postEvent (new FocusEvent(this, FocusEvent.FOCUS_GAINED, temporary));
+ synchronized (eq)
+ {
+ Component currentFocusOwner = manager.getGlobalPermanentFocusOwner ();
+ if (currentFocusOwner != null)
+ {
+ eq.postEvent (new FocusEvent(currentFocusOwner, FocusEvent.FOCUS_LOST,
+ temporary, this));
+ eq.postEvent (new FocusEvent(this, FocusEvent.FOCUS_GAINED, temporary,
+ currentFocusOwner));
+ }
+ else
+ eq.postEvent (new FocusEvent(this, FocusEvent.FOCUS_GAINED, temporary));
+ }
}
}
else
@@ -4009,8 +4054,8 @@
String name = getName();
if (name != null)
param.append(name).append(",");
- param.append(width).append("x").append(height).append("+").append(x)
- .append("+").append(y);
+ param.append(x).append(",").append(y).append(",").append(width)
+ .append("x").append(height);
if (! isValid())
param.append(",invalid");
if (! isVisible())
Index: java/awt/Container.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/awt/Container.java,v
retrieving revision 1.34.2.12
diff -u -r1.34.2.12 Container.java
--- java/awt/Container.java 9 Jun 2004 20:55:09 -0000 1.34.2.12
+++ java/awt/Container.java 10 Jul 2004 04:53:25 -0000
@@ -1027,7 +1027,7 @@
{
String param = super.paramString();
if (layoutMgr != null)
- param = param + "," + layoutMgr.getClass().getName();
+ param = param + ",layout=" + layoutMgr.getClass().getName();
return param;
}
Index: java/awt/DefaultKeyboardFocusManager.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/awt/DefaultKeyboardFocusManager.java,v
retrieving revision 1.1.54.4
diff -u -r1.1.54.4 DefaultKeyboardFocusManager.java
--- java/awt/DefaultKeyboardFocusManager.java 24 Jun 2004 05:30:15 -0000 1.1.54.4
+++ java/awt/DefaultKeyboardFocusManager.java 10 Jul 2004 04:53:30 -0000
@@ -162,17 +162,22 @@
{
Component target = (Component) e.getSource ();
- if (e.id == FocusEvent.FOCUS_GAINED
- && !(target instanceof Window))
+ if (e.id == FocusEvent.FOCUS_GAINED)
{
if (((FocusEvent) e).isTemporary ())
setGlobalFocusOwner (target);
else
setGlobalPermanentFocusOwner (target);
}
+ else if (e.id == FocusEvent.FOCUS_LOST)
+ {
+ if (((FocusEvent) e).isTemporary ())
+ setGlobalFocusOwner (null);
+ else
+ setGlobalPermanentFocusOwner (null);
+ }
- if (!(target instanceof Window))
- target.dispatchEvent (e);
+ target.dispatchEvent (e);
return true;
}
@@ -192,7 +197,9 @@
// processKeyEvent checks if this event represents a focus
// traversal key stroke.
Component focusOwner = getGlobalPermanentFocusOwner ();
- processKeyEvent (focusOwner, (KeyEvent) e);
+
+ if (focusOwner != null)
+ processKeyEvent (focusOwner, (KeyEvent) e);
if (e.isConsumed ())
return true;
@@ -230,7 +237,8 @@
{
Component focusOwner = getGlobalPermanentFocusOwner ();
- focusOwner.dispatchEvent (e);
+ if (focusOwner != null)
+ focusOwner.dispatchEvent (e);
// Loop through all registered KeyEventPostProcessors, giving
// each a chance to process this event.
Index: java/awt/Frame.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/awt/Frame.java,v
retrieving revision 1.20
diff -u -r1.20 Frame.java
--- java/awt/Frame.java 27 Jan 2004 19:29:56 -0000 1.20
+++ java/awt/Frame.java 10 Jul 2004 04:53:30 -0000
@@ -50,11 +50,6 @@
*/
public class Frame extends Window implements MenuContainer
{
-
-/*
- * Static Variables
- */
-
/**
* Constant for the default cursor.
* @deprecated Replaced by <code>Cursor.DEFAULT_CURSOR</code> instead.
@@ -148,12 +143,6 @@
// Serialization version constant
private static final long serialVersionUID = 2673458971256075116L;
-/*************************************************************************/
-
-/*
- * Instance Variables
- */
-
/**
* @serial The version of the class data being serialized
* // FIXME: what is this value?
@@ -208,11 +197,10 @@
*/
private boolean undecorated = false;
-/*************************************************************************/
-
-/*
- * Constructors
- */
+ /*
+ * The number used to generate the name returned by getName.
+ */
+ private static transient long next_frame_number = 0;
/**
* Initializes a new instance of <code>Frame</code> that is not visible
@@ -224,8 +212,6 @@
this("");
}
-/*************************************************************************/
-
/**
* Initializes a new instance of <code>Frame</code> that is not visible
* and has the specified title.
@@ -256,12 +242,6 @@
visible = false;
}
-/*************************************************************************/
-
-/*
- * Instance Methods
- */
-
/**
* Returns this frame's title string.
*
@@ -273,8 +253,6 @@
return(title);
}
-/*************************************************************************/
-
/*
* Sets this frame's title to the specified value.
*
@@ -288,8 +266,6 @@
((FramePeer) peer).setTitle(title);
}
-/*************************************************************************/
-
/**
* Returns this frame's icon.
*
@@ -302,8 +278,6 @@
return(icon);
}
-/*************************************************************************/
-
/**
* Sets this frame's icon to the specified value.
*
@@ -317,8 +291,6 @@
((FramePeer) peer).setIconImage(icon);
}
-/*************************************************************************/
-
/**
* Returns this frame's menu bar.
*
@@ -331,8 +303,6 @@
return(menuBar);
}
-/*************************************************************************/
-
/**
* Sets this frame's menu bar.
*
@@ -352,8 +322,6 @@
this.menuBar = menuBar;
}
-/*************************************************************************/
-
/**
* Tests whether or not this frame is resizable. This will be
* <code>true</code> by default.
@@ -367,8 +335,6 @@
return(resizable);
}
-/*************************************************************************/
-
/**
* Sets the resizability of this frame to the specified value.
*
@@ -383,8 +349,6 @@
((FramePeer) peer).setResizable(resizable);
}
-/*************************************************************************/
-
/**
* Returns the cursor type of the cursor for this window. This will
* be one of the constants in this class.
@@ -399,8 +363,6 @@
return(getCursor().getType());
}
-/*************************************************************************/
-
/**
* Sets the cursor for this window to the specified type. The specified
* type should be one of the constants in this class.
@@ -415,8 +377,6 @@
setCursor(new Cursor(type));
}
-/*************************************************************************/
-
/**
* Removes the specified component from this frame's menu.
*
@@ -428,8 +388,6 @@
menuBar.remove(menu);
}
-/*************************************************************************/
-
/**
* Notifies this frame that it should create its native peer.
*/
@@ -450,18 +408,41 @@
super.removeNotify();
}
-/*************************************************************************/
+ /**
+ * Returns a debugging string describing this window.
+ *
+ * @return A debugging string describing this window.
+ */
+ protected String paramString ()
+ {
+ String title = getTitle ();
-/**
- * Returns a debugging string describing this window.
- *
- * @return A debugging string describing this window.
- */
-protected String
-paramString()
-{
- return(getClass().getName());
-}
+ String resizable = "";
+ if (isResizable ())
+ resizable = ",resizable";
+
+ String state = "";
+ switch (getState ())
+ {
+ case NORMAL:
+ state = ",normal";
+ break;
+ case ICONIFIED:
+ state = ",iconified";
+ break;
+ case MAXIMIZED_BOTH:
+ state = ",maximized-both";
+ break;
+ case MAXIMIZED_HORIZ:
+ state = ",maximized-horiz";
+ break;
+ case MAXIMIZED_VERT:
+ state = ",maximized-vert";
+ break;
+ }
+
+ return super.paramString () + ",title=" + title + resizable + state;
+ }
public static Frame[]
getFrames()
@@ -553,5 +534,19 @@
this.undecorated = undecorated;
}
-} // class Frame
+ /**
+ * Generate a unique name for this frame.
+ *
+ * @return A unique name for this frame.
+ */
+ String generateName ()
+ {
+ return "frame" + getUniqueLong ();
+ }
+
+ private static synchronized long getUniqueLong ()
+ {
+ return next_frame_number++;
+ }
+}
Index: java/awt/KeyboardFocusManager.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/awt/KeyboardFocusManager.java,v
retrieving revision 1.2.24.2
diff -u -r1.2.24.2 KeyboardFocusManager.java
--- java/awt/KeyboardFocusManager.java 22 Apr 2004 20:26:32 -0000 1.2.24.2
+++ java/awt/KeyboardFocusManager.java 10 Jul 2004 04:53:30 -0000
@@ -246,7 +246,10 @@
*/
public Component getFocusOwner ()
{
- return (Component) getObject (currentFocusOwners);
+ Component owner = (Component) getObject (currentFocusOwners);
+ if (owner == null)
+ owner = (Component) getObject (currentPermanentFocusOwners);
+ return owner;
}
/**
Index: java/awt/Panel.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/awt/Panel.java,v
retrieving revision 1.8.20.1
diff -u -r1.8.20.1 Panel.java
--- java/awt/Panel.java 2 Jul 2004 06:04:46 -0000 1.8.20.1
+++ java/awt/Panel.java 10 Jul 2004 04:53:30 -0000
@@ -71,6 +71,11 @@
consumed. */
private transient boolean initialUpdateConsumed;
+ /*
+ * The number used to generate the name returned by getName.
+ */
+ private static transient long next_panel_number = 0;
+
/**
* Initializes a new instance of <code>Panel</code> that has a default
* layout manager of <code>FlowLayout</code>.
@@ -180,5 +185,20 @@
{
return AccessibleRole.PANEL;
}
- } // class AccessibleAWTPanel
-} // class Panel
+ }
+
+ /**
+ * Generate a unique name for this panel.
+ *
+ * @return A unique name for this panel.
+ */
+ String generateName ()
+ {
+ return "panel" + getUniqueLong ();
+ }
+
+ private static synchronized long getUniqueLong ()
+ {
+ return next_panel_number++;
+ }
+}
Index: java/awt/Window.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/awt/Window.java,v
retrieving revision 1.32.12.7
diff -u -r1.32.12.7 Window.java
--- java/awt/Window.java 2 Jul 2004 19:08:49 -0000 1.32.12.7
+++ java/awt/Window.java 10 Jul 2004 04:53:30 -0000
@@ -39,6 +39,7 @@
package java.awt;
import java.awt.event.ComponentEvent;
+import java.awt.event.FocusEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
@@ -84,6 +85,7 @@
private transient AccessibleContext accessibleContext;
private transient boolean shown;
+ private transient Component windowFocusOwner = null;
/**
* This (package access) constructor is used by subclasses that want
@@ -658,8 +660,8 @@
// The currently-focused Component belongs to the active Window.
if (activeWindow == this)
return manager.getFocusOwner ();
-
- return null;
+ else
+ return null;
}
/**
Index: jni/gtk-peer/gnu_java_awt_peer_gtk_GtkWindowPeer.c
===================================================================
RCS file: /cvs/gcc/gcc/libjava/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkWindowPeer.c,v
retrieving revision 1.24.2.9
diff -u -r1.24.2.9 gnu_java_awt_peer_gtk_GtkWindowPeer.c
--- jni/gtk-peer/gnu_java_awt_peer_gtk_GtkWindowPeer.c 1 Jul 2004 18:31:31 -0000 1.24.2.9
+++ jni/gtk-peer/gnu_java_awt_peer_gtk_GtkWindowPeer.c 10 Jul 2004 04:53:33 -0000
@@ -65,9 +65,18 @@
static void window_destroy_cb (GtkWidget *widget, GdkEvent *event,
jobject peer);
static void window_show_cb (GtkWidget *widget, jobject peer);
-static void window_focus_or_active_state_change_cb (GtkWidget *widget,
- GParamSpec *pspec,
- jobject peer);
+static void window_active_state_change_cb (GtkWidget *widget,
+ GParamSpec *pspec,
+ jobject peer);
+static void window_focus_state_change_cb (GtkWidget *widget,
+ GParamSpec *pspec,
+ jobject peer);
+static gboolean window_focus_in_cb (GtkWidget * widget,
+ GdkEventFocus *event,
+ jobject peer);
+static gboolean window_focus_out_cb (GtkWidget * widget,
+ GdkEventFocus *event,
+ jobject peer);
static gboolean window_window_state_cb (GtkWidget *widget,
GdkEvent *event,
jobject peer);
@@ -229,8 +238,17 @@
g_signal_connect (G_OBJECT (ptr), "show",
G_CALLBACK (window_show_cb), *gref);
- g_signal_connect (G_OBJECT (ptr), "notify",
- G_CALLBACK (window_focus_or_active_state_change_cb), *gref);
+ g_signal_connect (G_OBJECT (ptr), "notify::is-active",
+ G_CALLBACK (window_active_state_change_cb), *gref);
+
+ g_signal_connect (G_OBJECT (ptr), "notify::has-toplevel-focus",
+ G_CALLBACK (window_focus_state_change_cb), *gref);
+
+ g_signal_connect (G_OBJECT (ptr), "focus-in-event",
+ G_CALLBACK (window_focus_in_cb), *gref);
+
+ g_signal_connect (G_OBJECT (ptr), "focus-out-event",
+ G_CALLBACK (window_focus_out_cb), *gref);
g_signal_connect (G_OBJECT (ptr), "window-state-event",
G_CALLBACK (window_window_state_cb), *gref);
@@ -607,36 +625,64 @@
}
static void
-window_focus_or_active_state_change_cb (GtkWidget *widget,
- GParamSpec *pspec,
- jobject peer)
+window_active_state_change_cb (GtkWidget *widget,
+ GParamSpec *pspec,
+ jobject peer)
+{
+ /* FIXME: not sure if this is needed or not. */
+#if 0
+ if (GTK_WINDOW (widget)->is_active)
+ (*gdk_env)->CallVoidMethod (gdk_env, peer,
+ postWindowEventID,
+ (jint) AWT_WINDOW_GAINED_FOCUS,
+ (jobject) NULL, (jint) 0);
+ else
+ (*gdk_env)->CallVoidMethod (gdk_env, peer,
+ postWindowEventID,
+ (jint) AWT_WINDOW_DEACTIVATED,
+ (jobject) NULL, (jint) 0);
+#endif
+}
+
+static void
+window_focus_state_change_cb (GtkWidget *widget,
+ GParamSpec *pspec,
+ jobject peer)
{
- if (!strcmp (g_param_spec_get_name (pspec), "is-active"))
- {
- if (GTK_WINDOW (widget)->is_active)
- (*gdk_env)->CallVoidMethod (gdk_env, peer,
- postWindowEventID,
- (jint) AWT_WINDOW_GAINED_FOCUS,
- (jobject) NULL, (jint) 0);
- else
- (*gdk_env)->CallVoidMethod (gdk_env, peer,
- postWindowEventID,
- (jint) AWT_WINDOW_DEACTIVATED,
- (jobject) NULL, (jint) 0);
- }
- else if (!strcmp (g_param_spec_get_name (pspec), "has-toplevel-focus"))
- {
- if (GTK_WINDOW (widget)->has_toplevel_focus)
- (*gdk_env)->CallVoidMethod (gdk_env, peer,
- postWindowEventID,
- (jint) AWT_WINDOW_ACTIVATED,
- (jobject) NULL, (jint) 0);
- else
- (*gdk_env)->CallVoidMethod (gdk_env, peer,
- postWindowEventID,
- (jint) AWT_WINDOW_LOST_FOCUS,
- (jobject) NULL, (jint) 0);
- }
+ if (GTK_WINDOW (widget)->has_toplevel_focus)
+ (*gdk_env)->CallVoidMethod (gdk_env, peer,
+ postWindowEventID,
+ (jint) AWT_WINDOW_ACTIVATED,
+ (jobject) NULL, (jint) 0);
+ else
+ (*gdk_env)->CallVoidMethod (gdk_env, peer,
+ postWindowEventID,
+ (jint) AWT_WINDOW_DEACTIVATED,
+ (jobject) NULL, (jint) 0);
+}
+
+static gboolean
+window_focus_in_cb (GtkWidget * widget,
+ GdkEventFocus *event,
+ jobject peer)
+{
+ (*gdk_env)->CallVoidMethod (gdk_env, peer,
+ postWindowEventID,
+ (jint) AWT_WINDOW_GAINED_FOCUS,
+ (jobject) NULL, (jint) 0);
+ return FALSE;
+}
+
+static gboolean
+window_focus_out_cb (GtkWidget * widget,
+ GdkEventFocus *event,
+ jobject peer)
+{
+ (*gdk_env)->CallVoidMethod (gdk_env, peer,
+ postWindowEventID,
+ (jint) AWT_WINDOW_LOST_FOCUS,
+ (jobject) NULL, (jint) 0);
+ return FALSE;
}
static gboolean