This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[gui][PATCH] GTK list peer update


Hi,

This patch updates GtkListPeer to use the new GtkTreeView widget.  One
of the advantages of using the new widget is that it allows List
components to be sized correctly.

I committed this patch to java-gui-branch.

Tom

2004-05-10  Thomas Fitzsimmons  <fitzsim@redhat.com>

	* gnu/java/awt/peer/gtk/GtkListPeer.java,
	jni/gtk-peer/gnu_java_awt_peer_gtk_GtkListPeer.c: Update
	implementation of list peer to use GtkTreeView instead of
	deprecated GtkCList.


Index: gnu/java/awt/peer/gtk/GtkListPeer.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/awt/peer/gtk/GtkListPeer.java,v
retrieving revision 1.7
diff -u -r1.7 GtkListPeer.java
--- gnu/java/awt/peer/gtk/GtkListPeer.java	3 Feb 2004 17:10:49 -0000	1.7
+++ gnu/java/awt/peer/gtk/GtkListPeer.java	10 May 2004 17:25:34 -0000
@@ -48,12 +48,21 @@
 public class GtkListPeer extends GtkComponentPeer
   implements ListPeer
 {
-//    native void create (ComponentPeer parent, String [] items, boolean mode);
+  void create ()
+  {
+    List list = (List) awtComponent;
+
+    create (list.getRows ());
+
+    setMultipleMode (list.isMultipleMode ());
+  }
 
-  native void create ();
+  native void create (int rows);
   native void connectJObject ();
   native void connectSignals ();
 
+  native protected void gtkWidgetRequestFocus ();
+
   native void getSize (int rows, int dims[]);
 
   public GtkListPeer (List list)
@@ -129,19 +138,19 @@
   {
     if (e.getID () == MouseEvent.MOUSE_CLICKED && isEnabled ())
       {
-        /* Only generate the ActionEvent on the second click of
-	   a multiple click */
+        // Only generate the ActionEvent on the second click of a
+        // multiple click.
 	MouseEvent me = (MouseEvent) e;
 	if (!me.isConsumed ()
 	    && (me.getModifiers () & MouseEvent.BUTTON1_MASK) != 0
 	    && me.getClickCount() == 2)
 	  {
-            String selectedItem = ((List)awtComponent).getSelectedItem ();
+            String selectedItem = ((List) awtComponent).getSelectedItem ();
 
-            /* Double-click only generates an Action event
-	       if something is selected */
+            // Double-click only generates an Action event if
+            // something is selected.
             if (selectedItem != null)
-	      postActionEvent (((List)awtComponent).getSelectedItem (), 
+	      postActionEvent (((List) awtComponent).getSelectedItem (), 
 			       me.getModifiers ());
 	  }
       }
@@ -151,9 +160,10 @@
 	KeyEvent ke = (KeyEvent) e;
 	if (!ke.isConsumed () && ke.getKeyCode () == KeyEvent.VK_ENTER)
 	  {
-            String selectedItem = ((List)awtComponent).getSelectedItem ();
+            String selectedItem = ((List) awtComponent).getSelectedItem ();
 
-            /* Enter only generates an Action event if something is selected */
+            // Enter only generates an Action event if something is
+            // selected.
             if (selectedItem != null)
 	      postActionEvent (selectedItem, ke.getModifiers ());
 	  }
Index: jni/gtk-peer/gnu_java_awt_peer_gtk_GtkListPeer.c
===================================================================
RCS file: /cvs/gcc/gcc/libjava/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkListPeer.c,v
retrieving revision 1.7
diff -u -r1.7 gnu_java_awt_peer_gtk_GtkListPeer.c
--- jni/gtk-peer/gnu_java_awt_peer_gtk_GtkListPeer.c	13 Dec 2003 01:15:47 -0000	1.7
+++ jni/gtk-peer/gnu_java_awt_peer_gtk_GtkListPeer.c	10 May 2004 17:25:35 -0000
@@ -1,76 +1,109 @@
-/* gtklistpeer.c -- Native implementation of GtkListPeer
-   Copyright (C) 1998, 1999 Free Software Foundation, Inc.
+/* GtkListPeer.c -- implements GtkListPeer's native methods
+   Copyright (C) 1998, 1999, 2003, 2004 Free Software Foundation, Inc.
 
-This file is part of GNU Classpath.
-
-GNU Classpath is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
-any later version.
-
-GNU Classpath is distributed in the hope that it will be useful, but
-WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with GNU Classpath; see the file COPYING.  If not, write to the
-Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-02111-1307 USA.
-
-Linking this library statically or dynamically with other modules is
-making a combined work based on this library.  Thus, the terms and
-conditions of the GNU General Public License cover the whole
-combination.
-
-As a special exception, the copyright holders of this library give you
-permission to link this library with independent modules to produce an
-executable, regardless of the license terms of these independent
-modules, and to copy and distribute the resulting executable under
-terms of your choice, provided that you also meet, for each linked
-independent module, the terms and conditions of the license of that
-module.  An independent module is a module which is not derived from
-or based on this library.  If you modify this library, you may extend
-this exception to your version of the library, but you are not
-obligated to do so.  If you do not wish to do so, delete this
-exception statement from your version. */
+   This file is part of GNU Classpath.
 
+   GNU Classpath is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   GNU Classpath is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GNU Classpath; see the file COPYING.  If not, write to the
+   Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.
+
+   Linking this library statically or dynamically with other modules is
+   making a combined work based on this library.  Thus, the terms and
+   conditions of the GNU General Public License cover the whole
+   combination.
+
+   As a special exception, the copyright holders of this library give you
+   permission to link this library with independent modules to produce an
+   executable, regardless of the license terms of these independent
+   modules, and to copy and distribute the resulting executable under
+   terms of your choice, provided that you also meet, for each linked
+   independent module, the terms and conditions of the license of that
+   module.  An independent module is a module which is not derived from
+   or based on this library.  If you modify this library, you may extend
+   this exception to your version of the library, but you are not
+   obligated to do so.  If you do not wish to do so, delete this
+   exception statement from your version. */
 
 #include "gtkpeer.h"
-#include "gnu_java_awt_peer_gtk_GtkComponentPeer.h"
 #include "gnu_java_awt_peer_gtk_GtkListPeer.h"
 
-static void item_select (GtkCList *list __attribute__((unused)),
-	                 int row, int col __attribute__((unused)),
-	                 GdkEventButton *event __attribute__((unused)), 
-	                 jobject peer_obj);
-static void item_unselect (GtkCList *list __attribute__((unused)),
-	                   int row,
-	                   int col __attribute__((unused)),
-	                   GdkEventButton *event __attribute__((unused)),
-	                   jobject peer_obj);
+enum
+  {
+    COLUMN_STRING,
+    N_COLUMNS
+  };
 
-#define CLIST_FROM_SW(obj) (GTK_CLIST(GTK_SCROLLED_WINDOW (obj)->container.child))
+#define TREE_VIEW_FROM_SW(obj) \
+           (GTK_TREE_VIEW (GTK_SCROLLED_WINDOW (obj)->container.child))
 
-JNIEXPORT void JNICALL 
+JNIEXPORT void JNICALL
 Java_gnu_java_awt_peer_gtk_GtkListPeer_create
-  (JNIEnv *env, jobject obj)
+  (JNIEnv *env, jobject obj, jint rows)
 {
-  GtkWidget *list, *sw;
+  GtkWidget *sw;
+  GtkWidget *list;
+  GtkCellRenderer *renderer;
+  GtkTreeViewColumn *column;
+  GtkListStore *list_store;
+  GtkTreeIter iter;
+  GtkRequisition req;
+  gint i;
 
   /* Create global reference and save it for future use */
   NSA_SET_GLOBAL_REF (env, obj);
 
   gdk_threads_enter ();
-  
-  list = gtk_clist_new (1);
-  gtk_widget_show (list);
+
+  list_store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING);
+  /* Add the number of rows so that we can calculate the tree view's
+     size request. */
+  for (i = 0; i < rows; i++)
+    {
+      gtk_list_store_append (list_store, &iter);
+      gtk_list_store_set (list_store, &iter,
+                          COLUMN_STRING, "",
+                          -1);
+    }
+  list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store));
+  renderer = gtk_cell_renderer_text_new ();
+  column = gtk_tree_view_column_new_with_attributes (NULL,
+				     renderer,
+				     "text",
+				     COLUMN_STRING,
+				     NULL);
+
   sw = gtk_scrolled_window_new (NULL, NULL);
-  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), 
+  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
 				  GTK_POLICY_AUTOMATIC,
 				  GTK_POLICY_AUTOMATIC);
+
+  gtk_tree_view_append_column (GTK_TREE_VIEW (list), column);
+
+  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (list), FALSE);
+
+  gtk_widget_size_request (GTK_WIDGET (list), &req);
+
+  gtk_widget_set_size_request (GTK_WIDGET (list), req.width, req.height);
+
   gtk_container_add (GTK_CONTAINER (sw), list);
 
+  /* Remove the blank rows. */
+  gtk_list_store_clear (list_store);
+
+  gtk_widget_show (list);
+  gtk_widget_show (sw);
+
   gdk_threads_leave ();
 
   NSA_SET_PTR (env, obj, sw);
@@ -87,7 +120,6 @@
   gdk_threads_enter ();
 
   gtk_widget_realize (GTK_WIDGET (ptr));
-
   connect_awt_hook (env, obj, 1, GTK_WIDGET (ptr)->window);
 
   gdk_threads_leave ();
@@ -97,50 +129,63 @@
 Java_gnu_java_awt_peer_gtk_GtkListPeer_connectSignals
   (JNIEnv *env, jobject obj)
 {
-  GtkCList *list;
-  void *ptr = NSA_GET_PTR (env, obj);
-  jobject *gref = NSA_GET_GLOBAL_REF (env, obj);
+  void *ptr;
+  GtkTreeView *list;
+  jobject *gref;
+
+  gref = NSA_GET_GLOBAL_REF (env, obj);
   g_assert (gref);
 
+  ptr = NSA_GET_PTR (env, obj);
+
   gdk_threads_enter ();
 
   gtk_widget_realize (GTK_WIDGET (ptr));
+  list = TREE_VIEW_FROM_SW (ptr);
+  g_signal_connect (G_OBJECT (list), "event",
+                    G_CALLBACK (pre_event_handler), *gref);
 
-  /* connect selectable hook */
-  
-  list = CLIST_FROM_SW (ptr);
+  gdk_threads_leave ();
+}
 
-  g_signal_connect (G_OBJECT (list), "select_row", 
-		      GTK_SIGNAL_FUNC (item_select), *gref);
+JNIEXPORT void JNICALL
+Java_gnu_java_awt_peer_gtk_GtkListPeer_gtkWidgetRequestFocus
+  (JNIEnv *env, jobject obj)
+{
+  void *ptr;
+  GtkWidget *list;
 
-  g_signal_connect (G_OBJECT (list), "unselect_row", 
-		      GTK_SIGNAL_FUNC (item_unselect), *gref);
+  ptr = NSA_GET_PTR (env, obj);
+  
+  gdk_threads_enter ();
 
-  /* Connect the superclass signals.  */
-  /* FIXME: Cannot do that here or it will get the sw and not the list.
-     We must a generic way of doing this. */
-  /* Java_gnu_java_awt_peer_gtk_GtkComponentPeer_connectSignals (env, peer_obj); */
-  g_signal_connect (GTK_OBJECT (list), "event", 
-                    G_CALLBACK (pre_event_handler), *gref);
+  list = GTK_WIDGET (TREE_VIEW_FROM_SW (ptr));
+  gtk_widget_grab_focus (list);
 
   gdk_threads_leave ();
 }
 
-JNIEXPORT void JNICALL 
-Java_gnu_java_awt_peer_gtk_GtkListPeer_append 
+JNIEXPORT void JNICALL
+Java_gnu_java_awt_peer_gtk_GtkListPeer_append
   (JNIEnv *env, jobject obj, jobjectArray items)
 {
   void *ptr;
-  GtkCList *list;
-  jint count, i;
+  GtkTreeView *list;
+  GtkTreeIter iter;
+  GtkTreeModel *list_store;
+  jint count;
+  jint i;
 
   ptr = NSA_GET_PTR (env, obj);
 
   count = (*env)->GetArrayLength (env, items);
 
   gdk_threads_enter ();
-  list = CLIST_FROM_SW (ptr);
-  for (i = 0; i < count; i++) 
+
+  list = TREE_VIEW_FROM_SW (ptr);
+  list_store = gtk_tree_view_get_model (list);
+
+  for (i = 0; i < count; i++)
     {
       const char *text;
       jobject item;
@@ -148,11 +193,13 @@
       item = (*env)->GetObjectArrayElement (env, items, i);
 
       text = (*env)->GetStringUTFChars (env, item, NULL);
-      gtk_clist_append (list, (char **)&text);
+      gtk_list_store_append (GTK_LIST_STORE (list_store), &iter);
+      gtk_list_store_set (GTK_LIST_STORE (list_store), &iter,
+                          COLUMN_STRING, text,
+                          -1);
       (*env)->ReleaseStringUTFChars (env, item, text);
     }
 
-  gtk_clist_columns_autosize (list);
   gdk_threads_leave ();
 }
 
@@ -162,12 +209,26 @@
 {
   void *ptr;
   const char *str;
-    
+  GtkTreeView *list;
+  GtkTreeIter iter;
+  GtkTreeModel *list_store;
+
   ptr = NSA_GET_PTR (env, obj);
   str = (*env)->GetStringUTFChars (env, text, NULL);
 
   gdk_threads_enter ();
-  gtk_clist_insert (CLIST_FROM_SW (ptr), index, (char **)&str);
+
+  list = TREE_VIEW_FROM_SW (ptr);
+  list_store = gtk_tree_view_get_model (list);
+
+  if (index == -1)
+    gtk_list_store_append (GTK_LIST_STORE (list_store), &iter);
+  else
+    gtk_list_store_insert (GTK_LIST_STORE (list_store), &iter, index);
+
+  gtk_list_store_set (GTK_LIST_STORE (list_store), &iter,
+                      COLUMN_STRING, str, -1);
+
   gdk_threads_leave ();
 
   (*env)->ReleaseStringUTFChars (env, text, str);
@@ -179,22 +240,32 @@
   (JNIEnv *env, jobject obj, jint start, jint end)
 {
   void *ptr;
-  GtkCList *list;
+  GtkTreeView *list;
+  GtkTreeIter iter;
+  GtkTreeModel *list_store;
   jint i;
+  jint num_items;
     
   ptr = NSA_GET_PTR (env, obj);
 
   gdk_threads_enter ();
-  list = CLIST_FROM_SW (ptr);
 
-  if (end == -1)		/* special case for removing all rows */
-    gtk_clist_clear (list);
+  list = TREE_VIEW_FROM_SW (ptr);
+  list_store = gtk_tree_view_get_model (list);
+
+  /* Special case: remove all rows. */
+  if (end == -1)
+    gtk_list_store_clear (GTK_LIST_STORE (list_store));
   else
     {
-      gtk_clist_freeze (list);
-      for (i = end; i >= start; i--)
-        gtk_clist_remove (list, i);
-      gtk_clist_thaw (list);
+      i = 0;
+      num_items = end - start + 1;
+      gtk_tree_model_iter_nth_child (list_store, &iter, NULL, start);
+      while (i < num_items)
+	{
+	  gtk_list_store_remove (GTK_LIST_STORE (list_store), &iter);
+	  i++;
+	}
     }
 
   gdk_threads_leave ();
@@ -205,11 +276,17 @@
   (JNIEnv *env, jobject obj, jint index)
 {
   void *ptr;
+  GtkTreeView *list;
+  GtkTreePath *path;
     
   ptr = NSA_GET_PTR (env, obj);
 
   gdk_threads_enter ();
-  gtk_clist_select_row (CLIST_FROM_SW (ptr), index, 0);
+
+  list = TREE_VIEW_FROM_SW (ptr);
+  path = gtk_tree_path_new_from_indices (index, -1);
+  gtk_tree_view_set_cursor (list, path, NULL, FALSE);
+
   gdk_threads_leave ();
 }
 
@@ -218,11 +295,19 @@
   (JNIEnv *env, jobject obj, jint index)
 {
   void *ptr;
+  GtkTreeView *list;
+  GtkTreeSelection *selection;
+  GtkTreePath *path;
 
   ptr = NSA_GET_PTR (env, obj);
 
   gdk_threads_enter ();
-  gtk_clist_unselect_row (CLIST_FROM_SW (ptr), index, 0);
+
+  list = TREE_VIEW_FROM_SW (ptr);
+  selection = gtk_tree_view_get_selection (list);
+  path = gtk_tree_path_new_from_indices (index, -1);
+  gtk_tree_selection_unselect_path (selection, path);
+
   gdk_threads_leave ();
 }
 
@@ -232,9 +317,10 @@
 {
   void *ptr;
   jint *dims;
-  GtkWidget *list;
+  GtkTreeView *list;
+  GtkTreeModel *list_store;
   GtkScrolledWindow *sw;
-  GtkRequisition myreq;
+  GtkRequisition req;
 
   dims = (*env)->GetIntArrayElements (env, jdims, NULL);
   dims[0] = dims[1] = 0;
@@ -243,15 +329,16 @@
     rows = 3;
 
   ptr = NSA_GET_PTR (env, obj);
+
   gdk_threads_enter ();
 
-  list = GTK_WIDGET (CLIST_FROM_SW (ptr));
   sw = GTK_SCROLLED_WINDOW (ptr);
+  list = TREE_VIEW_FROM_SW (ptr);
+  list_store = gtk_tree_view_get_model (list);
+  gtk_widget_size_request (GTK_WIDGET (sw), &req);
+  dims[1] = req.height;
+  dims[0] = req.width;
 
-  gtk_widget_size_request(GTK_WIDGET(sw), &myreq);
-  dims[1]=myreq.height;
-  dims[0]=myreq.width;
-  
   gdk_threads_leave ();
 
   (*env)->ReleaseIntArrayElements (env, jdims, dims, 0);
@@ -263,31 +350,53 @@
   (JNIEnv *env, jobject obj)
 {
   void *ptr;
-  GtkCList *list;
-  jintArray selection;
-  jint *sel;
-  GList *child;
-  jint count, i;
+  GtkTreeView *list;
+  GtkTreeSelection *selection;
+  jintArray result_array;
+  jint *result_array_iter;
+  GList *current_row;
+  GList *rows;
+  gint *indices;
+  jint count;
+  jint i;
 
   ptr = NSA_GET_PTR (env, obj);
+
   gdk_threads_enter ();
 
-  list = CLIST_FROM_SW (ptr);
-  count = g_list_length (list->selection);
+  list = TREE_VIEW_FROM_SW (ptr);
+  selection = gtk_tree_view_get_selection (list);
+  count = gtk_tree_selection_count_selected_rows (selection);
+  if (count > 0)
+    {
+      current_row = rows = gtk_tree_selection_get_selected_rows (selection, NULL);
 
-  selection = (*env)->NewIntArray (env, count);
-  sel = (*env)->GetIntArrayElements (env, selection, NULL);  
+      result_array = (*env)->NewIntArray (env, count);
+      result_array_iter = (*env)->GetIntArrayElements (env, result_array, NULL);
 
-  for (i = 0, child = list->selection; i < count; i++)
-    {
-      sel[i] = GPOINTER_TO_INT (child->data);
-      child = g_list_next (child);
-    }
-  gdk_threads_leave ();
+      for (i = 0; i < count; i++)
+        {
+          indices = gtk_tree_path_get_indices (current_row->data);
+          result_array_iter[i] = indices ? indices[0] : -1;
+          current_row = g_list_next (current_row);
+        }
 
-  (*env)->ReleaseIntArrayElements (env, selection, sel, 0);
+      if (rows)
+        {
+          g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
+          g_list_free (rows);
+        }
+
+      gdk_threads_leave ();
+
+      (*env)->ReleaseIntArrayElements (env, result_array, result_array_iter, 0);
+
+      return result_array;
+    }
+  else
+    gdk_threads_leave ();
 
-  return selection;
+  return (jintArray) NULL;
 }
 
 JNIEXPORT void JNICALL
@@ -295,11 +404,17 @@
   (JNIEnv *env, jobject obj, jint index)
 {
   void *ptr;
+  GtkTreeView *list;
+  GtkTreePath *path;
 
   ptr = NSA_GET_PTR (env, obj);
 
   gdk_threads_enter ();
-  gtk_clist_moveto (CLIST_FROM_SW (ptr), index, 0, 0.5, 0.5);
+
+  list = TREE_VIEW_FROM_SW (ptr);
+  path = gtk_tree_path_new_from_indices (index, -1);
+  gtk_tree_view_scroll_to_cell (list, path, NULL, FALSE, 0.0, 0.0);
+
   gdk_threads_leave ();
 }
 
@@ -308,40 +423,18 @@
   (JNIEnv *env, jobject obj, jboolean mode)
 {
   void *ptr;
-    
+  GtkTreeView *list;
+  GtkTreeSelection *selection;
+
   ptr = NSA_GET_PTR (env, obj);
 
   gdk_threads_enter ();
-  gtk_clist_set_selection_mode (CLIST_FROM_SW (ptr),
-				mode ? GTK_SELECTION_MULTIPLE : 
-				       GTK_SELECTION_SINGLE);
-  gdk_threads_leave ();
-}
 
-static void
-item_select (GtkCList *list __attribute__((unused)),
-	     int row, int col __attribute__((unused)),
-	     GdkEventButton *event __attribute__((unused)), 
-	     jobject peer_obj)
-{
-  //g_print ("select_row\n");
-  (*gdk_env)->CallVoidMethod (gdk_env, peer_obj,
-			      postListItemEventID,
-			      row,
-			      (jint) AWT_ITEM_SELECTED);
-}
-
-static void
-item_unselect (GtkCList *list __attribute__((unused)),
-	       int row,
-	       int col __attribute__((unused)),
-	       GdkEventButton *event __attribute__((unused)),
-	       jobject peer_obj)
-{
-  //g_print ("unselect_row\n");
-  (*gdk_env)->CallVoidMethod (gdk_env, peer_obj,
-			      postListItemEventID,
-			      row,
-	   		      (jint) AWT_ITEM_DESELECTED);
-}
+  list = TREE_VIEW_FROM_SW (ptr);
+  selection = gtk_tree_view_get_selection (list);
+  gtk_tree_selection_set_mode (selection,
+                               mode ? GTK_SELECTION_MULTIPLE
+                               : GTK_SELECTION_SINGLE);
 
+  gdk_threads_leave ();
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]