[glade/tintou/listbox-placeholder: 2/2] Allow to specify placeholder to GtkListBox



commit 04498978e30ab64c9f0f49c6fcae4b074e969e8c
Author: Corentin Noël <corentin noel collabora com>
Date:   Tue Jun 25 10:10:27 2019 +0200

    Allow to specify placeholder to GtkListBox

 plugins/gtk+/glade-gtk-list-box.c | 180 +++++++++++++++++++++++++++++++-------
 plugins/gtk+/gtk+.xml             |  13 ++-
 2 files changed, 160 insertions(+), 33 deletions(-)
---
diff --git a/plugins/gtk+/glade-gtk-list-box.c b/plugins/gtk+/glade-gtk-list-box.c
index 9e054ad6..79feb7bb 100644
--- a/plugins/gtk+/glade-gtk-list-box.c
+++ b/plugins/gtk+/glade-gtk-list-box.c
@@ -93,11 +93,16 @@ glade_gtk_listbox_get_child_property (GladeWidgetAdaptor *adaptor,
                                       GValue             *value)
 {
   g_return_if_fail (GTK_IS_LIST_BOX (container));
-  g_return_if_fail (GTK_IS_LIST_BOX_ROW (child));
+  g_return_if_fail (GTK_IS_WIDGET (child));
 
   if (strcmp (property_name, "position") == 0)
     {
-      gint position = gtk_list_box_row_get_index (GTK_LIST_BOX_ROW (child));
+      gint position = 0;
+
+      if (GTK_IS_LIST_BOX_ROW (child)) {
+        position = gtk_list_box_row_get_index (GTK_LIST_BOX_ROW (child));
+      }
+
       g_value_set_int (value, position);
     }
   else
@@ -119,18 +124,19 @@ glade_gtk_listbox_set_child_property (GladeWidgetAdaptor *adaptor,
                                       GValue             *value)
 {
   g_return_if_fail (GTK_IS_LIST_BOX (container));
-  g_return_if_fail (GTK_IS_LIST_BOX_ROW (child));
+  g_return_if_fail (GTK_IS_WIDGET (child));
 
   g_return_if_fail (property_name != NULL || value != NULL);
 
   if (strcmp (property_name, "position") == 0)
     {
-      gint position;
+      gint position = g_value_get_int (value);
 
-      position = g_value_get_int (value);
-      glade_gtk_listbox_reorder (GTK_LIST_BOX (container),
-                                 GTK_LIST_BOX_ROW (child),
-                                 position);
+      if (GTK_IS_LIST_BOX_ROW (child)) {
+        glade_gtk_listbox_reorder (GTK_LIST_BOX (container),
+                                   GTK_LIST_BOX_ROW (child),
+                                   position);
+      }
     }
   else
     {
@@ -143,30 +149,88 @@ glade_gtk_listbox_set_child_property (GladeWidgetAdaptor *adaptor,
     }
 }
 
-gboolean
-glade_gtk_listbox_add_verify (GladeWidgetAdaptor *adaptor,
-                              GtkWidget          *container,
-                              GtkWidget          *child,
-                              gboolean            user_feedback)
+static void
+glade_listbox_search_placeholder_forall (GtkWidget *widget,
+                                         gpointer data)
 {
-  if (!GTK_IS_LIST_BOX_ROW (child))
+  GtkWidget **placeholder = (GtkWidget **)data;
+  /* A simple child should be a GtkListBoxRow, otherwise it's a placeholder */
+  if (!GTK_IS_LIST_BOX_ROW (widget) && GTK_IS_WIDGET (widget)) {
+    *placeholder = GTK_WIDGET (widget);
+  }
+}
+
+static GtkWidget*
+glade_listbox_get_placeholder (GtkListBox *list_box) {
+  GtkWidget *placeholder = NULL;
+
+  gtk_container_forall (GTK_CONTAINER (list_box), glade_listbox_search_placeholder_forall, &placeholder);
+
+  return placeholder;
+}
+
+static void
+glade_gtk_listbox_parse_finished (GladeProject *project, GladeWidget *gbox)
+{
+  GObject *box = glade_widget_get_object (gbox);
+  glade_widget_property_set (gbox, "use-placeholder", glade_listbox_get_placeholder (GTK_LIST_BOX (box)) != 
NULL);
+}
+
+void
+glade_gtk_listbox_post_create (GladeWidgetAdaptor *adaptor,
+                                GObject            *container,
+                                GladeCreateReason   reason)
+{
+  GladeWidget *gwidget = glade_widget_get_from_gobject (container);
+  GladeProject *project = glade_widget_get_project (gwidget);
+
+  if (reason == GLADE_CREATE_LOAD)
     {
-      if (user_feedback)
-        {
-          GladeWidgetAdaptor *tool_item_adaptor =
-            glade_widget_adaptor_get_by_type (GTK_TYPE_LIST_BOX_ROW);
-
-          glade_util_ui_message (glade_app_get_window (),
-                                 GLADE_UI_INFO, NULL,
-                                 ONLY_THIS_GOES_IN_THAT_MSG,
-                                 glade_widget_adaptor_get_title (tool_item_adaptor),
-                                 glade_widget_adaptor_get_title (adaptor));
-        }
+      g_signal_connect_object (project, "parse-finished",
+                               G_CALLBACK (glade_gtk_listbox_parse_finished),
+                               gwidget, 0);
+    }
+}
 
-      return FALSE;
+void
+glade_gtk_listbox_get_property (GladeWidgetAdaptor *adaptor,
+                                GObject            *object,
+                                const gchar        *id,
+                                GValue             *value)
+{
+  if (!strcmp (id, "use-placeholder"))
+    {
+      g_value_set_boolean (value, glade_listbox_get_placeholder (GTK_LIST_BOX (object)) != NULL);
     }
+  else
+    GWA_GET_CLASS (GTK_TYPE_CONTAINER)->get_property (adaptor, object, id,
+                                                      value);
+}
+
+void
+glade_gtk_listbox_set_property (GladeWidgetAdaptor *adaptor,
+                                GObject            *object,
+                                const gchar        *id,
+                                const GValue       *value)
+{
+  if (!strcmp (id, "use-placeholder"))
+    {
+      GtkWidget *child;
 
-  return TRUE;
+      if (g_value_get_boolean (value))
+        {
+          child = glade_listbox_get_placeholder (GTK_LIST_BOX (object));
+          if (!child)
+            child = glade_placeholder_new ();
+          g_object_set_data (G_OBJECT (child), "special-child-type", "placeholder");
+        }
+      else
+        child = NULL;
+      gtk_list_box_set_placeholder (GTK_LIST_BOX (object), child);
+    }
+  else
+    GWA_GET_CLASS (GTK_TYPE_CONTAINER)->set_property (adaptor, object, id,
+                                                      value);
 }
 
 void
@@ -174,7 +238,18 @@ glade_gtk_listbox_add_child (GladeWidgetAdaptor *adaptor,
                              GObject            *object,
                              GObject            *child)
 {
+  gchar *special_child_type;
+
   g_return_if_fail (GTK_IS_LIST_BOX (object));
+  g_return_if_fail (GTK_IS_WIDGET (child));
+
+  special_child_type = g_object_get_data (child, "special-child-type");
+  if (!g_strcmp0 (special_child_type, "placeholder"))
+    {
+      gtk_list_box_set_placeholder (GTK_LIST_BOX (object), GTK_WIDGET (child));
+       return;
+    }
+
   g_return_if_fail (GTK_IS_LIST_BOX_ROW (child));
 
   /* Insert to the end of the list */
@@ -183,11 +258,48 @@ glade_gtk_listbox_add_child (GladeWidgetAdaptor *adaptor,
                             -1);
 }
 
+void
+glade_gtk_listbox_replace_child (GladeWidgetAdaptor *adaptor,
+                                 GObject            *container,
+                                 GObject            *current,
+                                 GObject            *new_widget)
+{
+  gchar *special_child_type =
+    g_object_get_data (G_OBJECT (current), "special-child-type");
+
+  if (!g_strcmp0 (special_child_type, "placeholder"))
+    {
+      g_object_set_data (G_OBJECT (new_widget), "special-child-type", "placeholder");
+      gtk_list_box_set_placeholder (GTK_LIST_BOX (container), GTK_WIDGET (new_widget));
+      return;
+    }
+
+  GWA_GET_CLASS (GTK_TYPE_CONTAINER)->replace_child (adaptor,
+                                                     container,
+                                                     current, new_widget);
+}
+
 void
 glade_gtk_listbox_remove_child (GladeWidgetAdaptor *adaptor,
                                 GObject            *object,
                                 GObject            *child)
 {
+  gchar *special_child_type;
+
+  g_return_if_fail (GTK_IS_LIST_BOX (object));
+  g_return_if_fail (GTK_IS_WIDGET (child));
+
+  special_child_type = g_object_get_data (child, "special-child-type");
+  if (!g_strcmp0 (special_child_type, "placeholder"))
+    {
+      GtkWidget *w;
+
+      w = glade_placeholder_new ();
+      g_object_set_data (G_OBJECT (w), "special-child-type", "placeholder");
+      gtk_list_box_set_placeholder (GTK_LIST_BOX (object), w);
+      return;
+    }
+
   gtk_container_remove (GTK_CONTAINER (object), GTK_WIDGET (child));
 
   sync_row_positions (GTK_LIST_BOX (object));
@@ -201,14 +313,20 @@ glade_gtk_listbox_child_insert_action (GladeWidgetAdaptor *adaptor,
 {
   GladeWidget *parent;
   GladeWidget *gchild;
-  gint position;
+  gint position = 0;
 
   parent = glade_widget_get_from_gobject (container);
   glade_command_push_group (_("Insert Row on %s"), glade_widget_get_name (parent));
 
-  position = gtk_list_box_row_get_index (GTK_LIST_BOX_ROW (object));
-  if (after)
-    position++;
+  /* We can right click on the placeholder too */
+  if (GTK_IS_LIST_BOX_ROW (object)) {
+    position = gtk_list_box_row_get_index (GTK_LIST_BOX_ROW (object));
+    if (after)
+      position++;
+  } else {
+    if (after)
+      position = -1;
+  }
 
   gchild = glade_command_create (glade_widget_adaptor_get_by_type (GTK_TYPE_LIST_BOX_ROW),
                                  parent,
diff --git a/plugins/gtk+/gtk+.xml b/plugins/gtk+/gtk+.xml
index 8add4b5b..e5f8603e 100644
--- a/plugins/gtk+/gtk+.xml
+++ b/plugins/gtk+/gtk+.xml
@@ -2707,13 +2707,16 @@
         <action-activate-function>glade_gtk_listbox_action_activate</action-activate-function>
         
<child-action-activate-function>glade_gtk_listbox_child_action_activate</child-action-activate-function>
         <create-widget-function>glade_gtk_create_fixed_widget</create-widget-function>
+        <set-property-function>glade_gtk_listbox_set_property</set-property-function>
+        <get-property-function>glade_gtk_listbox_get_property</get-property-function>
         <!-- We do not want glade_gtk_container_post_create be executed -->
-        <post-create-function>empty</post-create-function>
-        <add-child-verify-function>glade_gtk_listbox_add_verify</add-child-verify-function>
+        <post-create-function>glade_gtk_listbox_post_create</post-create-function>
         <add-child-function>glade_gtk_listbox_add_child</add-child-function>
+        <replace-child-function>glade_gtk_listbox_replace_child</replace-child-function>
         <remove-child-function>glade_gtk_listbox_remove_child</remove-child-function>
         <child-set-property-function>glade_gtk_listbox_set_child_property</child-set-property-function>
         <child-get-property-function>glade_gtk_listbox_get_child_property</child-get-property-function>
+        <special-child-type>type</special-child-type>
         
         <actions>
           <action id="add_row" name="Add Row" stock="list-add" important="True"/>
@@ -2738,6 +2741,12 @@
               <value id="GTK_SELECTION_MULTIPLE" name="Multiple"/>
             </displayable-values>
           </property>
+          <property save="False" id="use-placeholder" since="3.10" default="False" name="Placeholder">
+            <parameter-spec>
+              <type>GParamBoolean</type>
+            </parameter-spec>
+            <tooltip>Whether this listbox should have a placeholder widget that is shown in the list when it 
doesn't display any visible children</tooltip>
+          </property>
         </properties>
         
         <packing-properties>


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