]> Pileus Git - ~andy/gtk/blobdiff - tests/testtreeview.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / testtreeview.c
index 4dc4b586f34ddcc8ff662462c6fd8eb77e4c5b32..9816540de424cf93134023149ab4b023f25b4c07 100644 (file)
@@ -1,8 +1,25 @@
-#undef GTK_DISABLE_DEPRECATED
+/* testtreeview.c
+ * Copyright (C) 2001 Red Hat, Inc
+ * Author: Jonathan Blandford
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #include <string.h>
 #include "prop-editor.h"
 #include <gtk/gtk.h>
-
+#include <stdlib.h>
 
 /* Don't copy this bad example; inline RGB data is always a better
  * idea than inline XPMs.
@@ -238,6 +255,66 @@ toggled_callback (GtkCellRendererToggle *celltoggle,
                g_type_name (G_TYPE_FROM_INSTANCE (model)));
 }
 
+static void
+edited_callback (GtkCellRendererText *renderer,
+                const gchar   *path_string,
+                const gchar   *new_text,
+                GtkTreeView  *tree_view)
+{
+  GtkTreeModel *model = NULL;
+  GtkTreeModelSort *sort_model = NULL;
+  GtkTreePath *path;
+  GtkTreeIter iter;
+  guint value = atoi (new_text);
+  
+  g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
+
+  model = gtk_tree_view_get_model (tree_view);
+  
+  if (GTK_IS_TREE_MODEL_SORT (model))
+    {
+      sort_model = GTK_TREE_MODEL_SORT (model);
+      model = gtk_tree_model_sort_get_model (sort_model);
+    }
+
+  if (model == NULL)
+    return;
+
+  if (sort_model)
+    {
+      g_warning ("FIXME implement conversion from TreeModelSort iter to child model iter");
+      return;
+    }
+      
+  path = gtk_tree_path_new_from_string (path_string);
+  if (!gtk_tree_model_get_iter (model,
+                                &iter, path))
+    {
+      g_warning ("%s: bad path?", G_STRLOC);
+      return;
+    }
+  gtk_tree_path_free (path);
+
+  if (GTK_IS_LIST_STORE (model))
+    {
+      gtk_list_store_set (GTK_LIST_STORE (model),
+                          &iter,
+                          4,
+                          value,
+                          -1);
+    }
+  else if (GTK_IS_TREE_STORE (model))
+    {
+      gtk_tree_store_set (GTK_TREE_STORE (model),
+                          &iter,
+                          4,
+                          value,
+                          -1);
+    }
+  else
+    g_warning ("don't know how to actually toggle value for model type %s",
+               g_type_name (G_TYPE_FROM_INSTANCE (model)));
+}
 
 static ColumnsType current_column_type = COLUMNS_LOTS;
 
@@ -248,7 +325,8 @@ set_columns_type (GtkTreeView *tree_view, ColumnsType type)
   GtkCellRenderer *rend;
   GdkPixbuf *pixbuf;
   GtkWidget *image;
-  
+  GtkAdjustment *adjustment;
+
   current_column_type = type;
   
   col = gtk_tree_view_get_column (tree_view, 0);
@@ -271,7 +349,7 @@ set_columns_type (GtkTreeView *tree_view, ColumnsType type)
       gtk_tree_view_set_rules_hint (tree_view, TRUE);
       
       rend = gtk_cell_renderer_text_new ();
-      
+
       col = gtk_tree_view_column_new_with_attributes ("Column 1",
                                                       rend,
                                                       "text", 1,
@@ -338,19 +416,25 @@ set_columns_type (GtkTreeView *tree_view, ColumnsType type)
       setup_column (col);
       
       gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), col);
-      
-#if 0
 
-      rend = gtk_cell_renderer_text_new ();
-      
+      rend = gtk_cell_renderer_spin_new ();
+
+      adjustment = gtk_adjustment_new (0, 0, 10000, 100, 100, 100);
+      g_object_set (rend, "editable", TRUE, NULL);
+      g_object_set (rend, "adjustment", adjustment, NULL);
+
+      g_signal_connect (rend, "edited",
+                       G_CALLBACK (edited_callback), tree_view);
+
       col = gtk_tree_view_column_new_with_attributes ("Column 5",
                                                       rend,
-                                                      "text", 3,
+                                                      "text", 4,
                                                       NULL);
 
       setup_column (col);
       
       gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), col);
+#if 0
       
       rend = gtk_cell_renderer_text_new ();
       
@@ -540,17 +624,14 @@ create_tree_model (void)
   GtkTreeStore *store;
   gint i;
   GType *t;
-  volatile GType dummy; /* G_GNUC_CONST makes the optimizer remove
-                         * get_type calls if you don't do something
-                         * like this
-                         */
   
   /* Make the tree more interesting */
-  dummy = gtk_scrolled_window_get_type ();
-  dummy = gtk_label_get_type ();
-  dummy = gtk_hscrollbar_get_type ();
-  dummy = gtk_vscrollbar_get_type ();
-  dummy = pango_layout_get_type ();
+  /* - we need this magic here so we are sure the type ends up being
+   * registered and gcc doesn't optimize away the code */
+  g_type_class_unref (g_type_class_ref (gtk_scrolled_window_get_type ()));
+  g_type_class_unref (g_type_class_ref (gtk_label_get_type ()));
+  g_type_class_unref (g_type_class_ref (gtk_scrollbar_get_type ()));
+  g_type_class_unref (g_type_class_ref (pango_layout_get_type ()));
 
   t = get_model_types ();
   
@@ -571,12 +652,12 @@ create_tree_model (void)
 }
 
 static void
-model_selected (GtkOptionMenu *om, gpointer data)
+model_selected (GtkComboBox *combo_box, gpointer data)
 {
   GtkTreeView *tree_view = GTK_TREE_VIEW (data);
   gint hist;
 
-  hist = gtk_option_menu_get_history (om);
+  hist = gtk_combo_box_get_active (combo_box);
 
   if (models[hist] != gtk_tree_view_get_model (tree_view))
     {
@@ -585,12 +666,12 @@ model_selected (GtkOptionMenu *om, gpointer data)
 }
 
 static void
-columns_selected (GtkOptionMenu *om, gpointer data)
+columns_selected (GtkComboBox *combo_box, gpointer data)
 {
   GtkTreeView *tree_view = GTK_TREE_VIEW (data);
   gint hist;
 
-  hist = gtk_option_menu_get_history (om);
+  hist = gtk_combo_box_get_active (combo_box);
 
   if (hist != get_columns_type ())
     {
@@ -598,6 +679,14 @@ columns_selected (GtkOptionMenu *om, gpointer data)
     }
 }
 
+void
+on_row_activated (GtkTreeView       *tree_view,
+                  GtkTreePath       *path,
+                  GtkTreeViewColumn *column,
+                  gpointer           user_data)
+{
+  g_print ("Row activated\n");
+}
 
 enum
 {
@@ -616,14 +705,16 @@ main (int    argc,
   GtkWidget *window;
   GtkWidget *sw;
   GtkWidget *tv;
-  GtkWidget *table;
-  GtkWidget *om;
-  GtkWidget *menu;
+  GtkWidget *box;
+  GtkWidget *combo_box;
   GtkTreeModel *model;
   gint i;
   
   gtk_init (&argc, &argv);
 
+  if (g_getenv ("RTL"))
+    gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
+
   our_pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) book_closed_xpm);  
   
 #if 0
@@ -649,14 +740,15 @@ main (int    argc,
   
   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
-  gtk_window_set_default_size (GTK_WINDOW (window), 400, 400);
+  gtk_window_set_default_size (GTK_WINDOW (window), 430, 400);
 
-  table = gtk_table_new (3, 1, FALSE);
+  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
 
-  gtk_container_add (GTK_CONTAINER (window), table);
+  gtk_container_add (GTK_CONTAINER (window), box);
 
   tv = gtk_tree_view_new_with_model (models[0]);
-  
+  g_signal_connect (tv, "row-activated", G_CALLBACK (on_row_activated), NULL);
+
   gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (tv),
                                          GDK_BUTTON1_MASK,
                                          row_targets,
@@ -669,94 +761,47 @@ main (int    argc,
                                        GDK_ACTION_MOVE | GDK_ACTION_COPY);
   
   /* Model menu */
+  combo_box = gtk_combo_box_text_new ();
+  gtk_widget_set_halign (combo_box, GTK_ALIGN_CENTER);
+  for (i = 0; i < MODEL_LAST; i++)
+      gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), model_names[i]);
 
-  menu = gtk_menu_new ();
-  
-  i = 0;
-  while (i < MODEL_LAST)
-    {
-      GtkWidget *mi;
-      const char *name;
-
-      name = model_names[i];
-      
-      mi = gtk_menu_item_new_with_label (name);
-
-      gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
-
-#if 0
-      window = create_prop_editor (G_OBJECT (models[i]));
-
-      gtk_window_set_title (GTK_WINDOW (window),                            
-                            name);
-#endif
-
-      ++i;
-    }
-  gtk_widget_show_all (menu);
-  
-  om = gtk_option_menu_new ();
-  gtk_option_menu_set_menu (GTK_OPTION_MENU (om), menu);
-  
-  gtk_table_attach (GTK_TABLE (table), om,
-                    0, 1, 0, 1,
-                    0, 0, 
-                    0, 0);
-
-  g_signal_connect (om,
+  gtk_container_add (GTK_CONTAINER (box), combo_box);
+  g_signal_connect (combo_box,
                     "changed",
                     G_CALLBACK (model_selected),
                    tv);
   
   /* Columns menu */
+  combo_box = gtk_combo_box_text_new ();
+  gtk_widget_set_halign (combo_box, GTK_ALIGN_CENTER);
+  for (i = 0; i < COLUMNS_LAST; i++)
+      gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), column_type_names[i]);
 
-  menu = gtk_menu_new ();
-  
-  i = 0;
-  while (i < COLUMNS_LAST)
-    {
-      GtkWidget *mi;
-      const char *name;
-
-      name = column_type_names[i];
-      
-      mi = gtk_menu_item_new_with_label (name);
-
-      gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
-
-      ++i;
-    }
-  gtk_widget_show_all (menu);
-  
-  om = gtk_option_menu_new ();
-  gtk_option_menu_set_menu (GTK_OPTION_MENU (om), menu);
-  
-  gtk_table_attach (GTK_TABLE (table), om,
-                    0, 1, 1, 2,
-                    0, 0, 
-                    0, 0);
+  gtk_container_add (GTK_CONTAINER (box), combo_box);
 
   set_columns_type (GTK_TREE_VIEW (tv), COLUMNS_LOTS);
-  gtk_option_menu_set_history (GTK_OPTION_MENU (om), COLUMNS_LOTS);
-  
-  g_signal_connect (om,
+  gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), COLUMNS_LOTS);
+
+  g_signal_connect (combo_box,
                     "changed",
                     G_CALLBACK (columns_selected),
                     tv);
   
   sw = gtk_scrolled_window_new (NULL, NULL);
+  gtk_widget_set_hexpand (sw, TRUE);
+  gtk_widget_set_vexpand (sw, TRUE);
   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
                                   GTK_POLICY_AUTOMATIC,
                                   GTK_POLICY_AUTOMATIC);
   
-  gtk_table_attach (GTK_TABLE (table), sw,
-                    0, 1, 2, 3,
-                    GTK_EXPAND | GTK_FILL,
-                    GTK_EXPAND | GTK_FILL,
-                    0, 0);
+  gtk_container_add (GTK_CONTAINER (box), sw);
   
   gtk_container_add (GTK_CONTAINER (sw), tv);
   
+  create_prop_editor (G_OBJECT (tv), GTK_TYPE_TREE_VIEW);
+  create_prop_editor (G_OBJECT (gtk_tree_view_get_selection (GTK_TREE_VIEW (tv))), GTK_TYPE_TREE_SELECTION);
+
   gtk_widget_show_all (window);
   
   gtk_main ();
@@ -804,7 +849,7 @@ gtk_tree_model_types_get_type (void)
 
   if (!model_types_type)
     {
-      static const GTypeInfo model_types_info =
+      const GTypeInfo model_types_info =
       {
         sizeof (GtkTreeModelTypesClass),
        NULL,           /* base_init */
@@ -817,7 +862,7 @@ gtk_tree_model_types_get_type (void)
         (GInstanceInitFunc) gtk_tree_model_types_init
       };
 
-      static const GInterfaceInfo tree_model_info =
+      const GInterfaceInfo tree_model_info =
       {
        (GInterfaceInitFunc) gtk_tree_model_types_tree_model_init,
        NULL,
@@ -840,7 +885,7 @@ gtk_tree_model_types_new (void)
 {
   GtkTreeModelTypes *retval;
 
-  retval = GTK_TREE_MODEL_TYPES (g_object_new (GTK_TYPE_MODEL_TYPES, NULL));
+  retval = g_object_new (GTK_TYPE_MODEL_TYPES, NULL);
 
   return retval;
 }
@@ -963,7 +1008,7 @@ gtk_real_model_types_get_value (GtkTreeModel *tree_model,
         
         g_value_init (value, G_TYPE_STRING);
 
-        str = g_strdup_printf ("%ld", type);
+        str = g_strdup_printf ("%ld", (long int) type);
         g_value_set_string (value, str);
         g_free (str);
       }
@@ -1162,7 +1207,9 @@ gtk_real_model_types_iter_parent (GtkTreeModel *tree_model,
   if (parent == G_TYPE_INVALID)
     {
       if (type > G_TYPE_FUNDAMENTAL_MAX)
-        g_warning ("no parent for %ld %s\n", type, g_type_name (type));
+        g_warning ("no parent for %ld %s\n",
+                   (long int) type,
+                   g_type_name (type));
       return FALSE;
     }
   else
@@ -1177,6 +1224,8 @@ gtk_real_model_types_iter_parent (GtkTreeModel *tree_model,
  * Automated testing
  */
 
+#if 0
+
 static void
 treestore_torture_recurse (GtkTreeStore *store,
                            GtkTreeIter  *root,
@@ -1266,6 +1315,8 @@ treestore_torture_recurse (GtkTreeStore *store,
     }
 }
 
+#endif
+
 static void
 run_automated_tests (void)
 {