[gtk/prop-list: 40/96] inspector: Use a dropdown for attribute mapping



commit f55528fd101fc5b9e40612a2839f8639e107fdf6
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Dec 13 22:12:15 2019 -0500

    inspector: Use a dropdown for attribute mapping
    
    Use a GtkDropDown for the attribute mapping editor.

 gtk/inspector/prop-editor.c | 120 +++++++++++++++++++++++++++++++++++---------
 1 file changed, 95 insertions(+), 25 deletions(-)
---
diff --git a/gtk/inspector/prop-editor.c b/gtk/inspector/prop-editor.c
index 77d1020b9b..5146cedf79 100644
--- a/gtk/inspector/prop-editor.c
+++ b/gtk/inspector/prop-editor.c
@@ -1156,7 +1156,8 @@ model_properties (GtkButton              *button,
 }
 
 static void
-attribute_mapping_changed (GtkComboBox            *combo,
+attribute_mapping_changed (GtkDropDown            *dropdown,
+                           GParamSpec             *pspec,
                            GtkInspectorPropEditor *self)
 {
   gint col;
@@ -1164,7 +1165,7 @@ attribute_mapping_changed (GtkComboBox            *combo,
   GtkCellRenderer *cell;
   GtkCellArea *area;
 
-  col = gtk_combo_box_get_active (combo) - 1;
+  col = gtk_drop_down_get_selected (dropdown) - 1;
   layout = g_object_get_data (self->object, "gtk-inspector-cell-layout");
   if (GTK_IS_CELL_LAYOUT (layout))
     {
@@ -1179,6 +1180,72 @@ attribute_mapping_changed (GtkComboBox            *combo,
     }
 }
 
+#define ATTRIBUTE_TYPE_HOLDER (attribute_holder_get_type ())
+G_DECLARE_FINAL_TYPE (AttributeHolder, attribute_holder, ATTRIBUTE, HOLDER, GObject)
+
+struct _AttributeHolder {
+  GObject parent_instance;
+  int column;
+  gboolean sensitive;
+};
+
+G_DEFINE_TYPE (AttributeHolder, attribute_holder, G_TYPE_OBJECT);
+
+static void
+attribute_holder_init (AttributeHolder *holder)
+{
+}
+
+static void
+attribute_holder_class_init (AttributeHolderClass *class)
+{
+}
+
+static AttributeHolder *
+attribute_holder_new (int      column,
+                      gboolean sensitive)
+{
+  AttributeHolder *holder = g_object_new (ATTRIBUTE_TYPE_HOLDER, NULL);
+  holder->column = column;
+  holder->sensitive = sensitive;
+  return holder;
+}
+
+static void
+attribute_setup_item (GtkSignalListItemFactory *factory,
+                      GtkListItem              *item)
+{
+  GtkWidget *label;
+
+  label = gtk_label_new ("");
+  gtk_label_set_xalign (GTK_LABEL (label), 0.0);
+
+  gtk_list_item_set_child (item, label);
+}
+
+static void
+attribute_bind_item (GtkSignalListItemFactory *factory,
+                     GtkListItem              *item)
+{
+  GtkWidget *label;
+  AttributeHolder *holder;
+
+  holder = gtk_list_item_get_item (item);
+  label = gtk_list_item_get_child (item);
+
+  if (holder->column >= 0)
+    {
+      char *text = g_strdup_printf ("%d", holder->column);
+      gtk_label_set_label (GTK_LABEL (label), text);
+      g_free (text);
+    }
+  else
+    gtk_label_set_label (GTK_LABEL (label), _("None"));
+
+  gtk_list_item_set_selectable (item, holder->sensitive);
+  gtk_widget_set_sensitive (label, holder->sensitive);
+}
+
 static GtkWidget *
 attribute_editor (GObject                *object,
                   GParamSpec             *spec,
@@ -1191,13 +1258,12 @@ attribute_editor (GObject                *object,
   GtkWidget *label;
   GtkWidget *button;
   GtkWidget *box;
-  GtkWidget *combo;
-  gchar *text;
+  GtkWidget *dropdown;
+  GListStore *store;
+  GtkListItemFactory *factory;
   gint i;
+  AttributeHolder *holder;
   gboolean sensitive;
-  GtkCellRenderer *renderer;
-  GtkListStore *store;
-  GtkTreeIter iter;
 
   layout = g_object_get_data (object, "gtk-inspector-cell-layout");
   if (GTK_IS_CELL_LAYOUT (layout))
@@ -1220,30 +1286,34 @@ attribute_editor (GObject                *object,
   gtk_container_add (GTK_CONTAINER (box), button);
 
   gtk_container_add (GTK_CONTAINER (box), gtk_label_new (_("Column:")));
-  store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
-  combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
-  renderer = gtk_cell_renderer_text_new ();
-  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, FALSE);
-  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
-                                  "text", 0,
-                                  "sensitive", 1,
-                                  NULL);
-  gtk_list_store_append (store, &iter);
-  gtk_list_store_set (store, &iter, 0, C_("property name", "None"), 1, TRUE, -1);
+  dropdown = gtk_drop_down_new ();
+
+  store = g_list_store_new (ATTRIBUTE_TYPE_HOLDER);
+  holder = attribute_holder_new (-1, TRUE);
+  g_list_store_append (store, holder);
+  g_object_unref (holder);
   for (i = 0; i < gtk_tree_model_get_n_columns (model); i++)
     {
-      text = g_strdup_printf ("%d", i);
       sensitive = g_value_type_transformable (gtk_tree_model_get_column_type (model, i),
                                               spec->value_type);
-      gtk_list_store_append (store, &iter);
-      gtk_list_store_set (store, &iter, 0, text, 1, sensitive, -1);
-      g_free (text);
+      holder = attribute_holder_new (i, sensitive);
+      g_list_store_append (store, holder);
+      g_object_unref (holder);
     }
-  gtk_combo_box_set_active (GTK_COMBO_BOX (combo), col + 1);
-  attribute_mapping_changed (GTK_COMBO_BOX (combo), self);
-  g_signal_connect (combo, "changed",
+  gtk_drop_down_set_model (GTK_DROP_DOWN (dropdown), G_LIST_MODEL (store));
+  g_object_unref (store);
+
+  factory = gtk_signal_list_item_factory_new ();
+  g_signal_connect (factory, "setup", G_CALLBACK (attribute_setup_item), NULL);
+  g_signal_connect (factory, "bind", G_CALLBACK (attribute_bind_item), NULL);
+  gtk_drop_down_set_factory (GTK_DROP_DOWN (dropdown), factory);
+  g_object_unref (factory);
+
+  gtk_drop_down_set_selected (GTK_DROP_DOWN (dropdown), col + 1);
+  attribute_mapping_changed (GTK_DROP_DOWN (dropdown), NULL, self);
+  g_signal_connect (dropdown, "notify::selected",
                     G_CALLBACK (attribute_mapping_changed), self);
-  gtk_container_add (GTK_CONTAINER (box), combo);
+  gtk_container_add (GTK_CONTAINER (box), dropdown);
 
   return box;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]