[glade] Added a composite templates widget group in palette. Replace template widget with new type.



commit b399c2555ae4b41db6d87ea55211c97b442d5919
Author: Juan Pablo Ugarte <juanpablougarte gmail com>
Date:   Sat Oct 20 15:20:40 2012 -0700

    Added a composite templates widget group in palette.
    Replace template widget with new type.

 gladeui/glade-composite-template.c |   52 ++++++++++++++++++++++++++++-
 gladeui/glade-composite-template.h |    3 +-
 gladeui/glade-palette.c            |   65 +++++++++++++++++++++++++++++++++--
 gladeui/glade-widget-adaptor.c     |    2 +
 plugins/gtk+/glade-gtk.c           |    2 +-
 5 files changed, 117 insertions(+), 7 deletions(-)
---
diff --git a/gladeui/glade-composite-template.c b/gladeui/glade-composite-template.c
index 5791285..81ede79 100644
--- a/gladeui/glade-composite-template.c
+++ b/gladeui/glade-composite-template.c
@@ -20,6 +20,9 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
+#include <config.h>
+
+#include <glib/gi18n-lib.h>
 #include "glade-composite-template.h"
 #include "glade-app.h"
 #include "glade-utils.h"
@@ -90,9 +93,36 @@ composite_template_derived_init (GTypeInstance *instance, gpointer g_class)
 {
 }
 
+#if !HAVE_GTK_CONTAINER_CLASS_SET_TEMPLATE_FROM_STRING
+static GObject *
+composite_template_constructor (GType type,
+                                guint n_construct_properties,
+                                GObjectConstructParam *construct_properties)
+{
+  GladeWidgetAdaptor *adaptor;
+  GObjectClass *parent_class;
+  GObject *obj;
+
+  parent_class = g_type_class_peek (g_type_parent (type));
+  obj = parent_class->constructor (type,
+                                   n_construct_properties,
+                                   construct_properties); 
+/*
+  adaptor = glade_widget_adaptor_get_by_type (type);
+
+
+  glade_widget_adaptor_get_*/
+  return obj;
+}
+#endif
+
 static void
 composite_template_derived_class_init (gpointer g_class, gpointer class_data)
 {
+#if !HAVE_GTK_CONTAINER_CLASS_SET_TEMPLATE_FROM_STRING
+  GObjectClass *object_class = g_class;
+  object_class->constructor = composite_template_constructor;
+#endif
 }
 
 static inline GType
@@ -226,6 +256,7 @@ glade_composite_template_load_directory (const gchar *directory)
  * @gwidget: a #GladeWidget
  * @template_class: the name of the new composite template class
  * @filename: a file name to save the template
+ * @replace: True if you want to replace @gwidget with a new widget of type @template_class
  * 
  * Saves a copy of @gwidget as a composite template in @filename with @template_class
  * as the class name
@@ -233,7 +264,8 @@ glade_composite_template_load_directory (const gchar *directory)
 void
 glade_composite_template_save_from_widget (GladeWidget *gwidget,
                                            const gchar *template_class,
-                                           const gchar *filename)
+                                           const gchar *filename,
+                                           gboolean replace)
 {
   GladeProject *project;
   gchar *template_xml;
@@ -255,6 +287,24 @@ glade_composite_template_save_from_widget (GladeWidget *gwidget,
 
   g_file_set_contents (filename, template_xml, -1, NULL);
 
+  if (replace)
+    {
+      GladeProject *project = glade_widget_get_project (gwidget);
+      GladeWidget *parent = glade_widget_get_parent (gwidget);
+      GladeWidgetAdaptor *new_adaptor;
+      GList widgets = {0, };
+      
+      /* Create it at run time */
+      if ((new_adaptor = glade_composite_template_load_from_string (template_xml)))
+        g_object_set (new_adaptor, "template-path", filename, NULL);
+
+      glade_command_push_group (_("Create new composite type %s"), template_class);
+      widgets.data = gwidget;
+      glade_command_cut (&widgets);
+      glade_command_create (new_adaptor, parent, NULL, project);
+      glade_command_pop_group ();
+    }
+  
   g_free (template_xml);
   g_object_unref (project);
 }
diff --git a/gladeui/glade-composite-template.h b/gladeui/glade-composite-template.h
index 557cea9..46a2373 100644
--- a/gladeui/glade-composite-template.h
+++ b/gladeui/glade-composite-template.h
@@ -35,7 +35,8 @@ void glade_composite_template_load_directory (const gchar *directory);
 
 void glade_composite_template_save_from_widget (GladeWidget *gwidget,
                                                 const gchar *template_class,
-                                                const gchar *filename);
+                                                const gchar *filename,
+                                                gboolean     replace);
 
 G_END_DECLS
 
diff --git a/gladeui/glade-palette.c b/gladeui/glade-palette.c
index 410ddac..ec08bbb 100644
--- a/gladeui/glade-palette.c
+++ b/gladeui/glade-palette.c
@@ -69,6 +69,8 @@ struct _GladePalettePrivate
 
   GladeWidgetAdaptor *local_selection;
   GHashTable         *button_table;
+
+  GtkWidget *composite_templates;
 };
 
 enum
@@ -335,13 +337,12 @@ glade_palette_new_item (GladePalette * palette, GladeWidgetAdaptor * adaptor)
 }
 
 static GtkWidget *
-glade_palette_new_item_group (GladePalette * palette, GladeWidgetGroup * group)
+tool_item_group_new (const gchar *name)
 {
-  GtkWidget *item_group, *item, *label;
-  GList *l;
+  GtkWidget *item_group, *label;
 
   /* Give the item group a left aligned label */
-  label = gtk_label_new (glade_widget_group_get_title (group));
+  label = gtk_label_new (name);
   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
   gtk_widget_show (label);
 
@@ -353,6 +354,17 @@ glade_palette_new_item_group (GladePalette * palette, GladeWidgetGroup * group)
   gtk_tool_item_group_set_ellipsize (GTK_TOOL_ITEM_GROUP (item_group),
                                      PANGO_ELLIPSIZE_END);
 
+  return item_group;
+}
+
+static GtkWidget *
+glade_palette_new_item_group (GladePalette * palette, GladeWidgetGroup * group)
+{
+  GtkWidget *item_group, *item;
+  GList *l;
+
+  item_group = tool_item_group_new (glade_widget_group_get_title (group));
+
   gtk_widget_set_tooltip_text (item_group,
                                glade_widget_group_get_title (group));
 
@@ -388,12 +400,53 @@ glade_palette_append_item_group (GladePalette * palette,
 }
 
 static void
+glade_palette_update_templates (GladePalette *palette)
+{
+  GList *l, *adaptors = glade_widget_adaptor_list_adaptors ();
+  GladePalettePrivate *priv = palette->priv;
+
+  if (priv->composite_templates == NULL)
+    {
+      priv->composite_templates = tool_item_group_new (_("Composite Templates"));
+      gtk_container_add (GTK_CONTAINER (priv->toolpalette), priv->composite_templates);
+      gtk_widget_show (priv->composite_templates);
+    }
+  else
+    {
+      GtkContainer *container = GTK_CONTAINER (priv->composite_templates);
+      GList *l, *children = gtk_container_get_children (container);
+      
+      for (l = children; l; l = g_list_next (l))
+        gtk_container_remove (container, l->data);
+
+      g_list_free (children);
+    }
+  
+  for (l = adaptors; l; l = g_list_next (l))
+    {
+      GladeWidgetAdaptor *adaptor = l->data;
+
+      /* Create/append new item */
+      if (glade_widget_adaptor_get_template (adaptor))
+        {
+          GtkWidget *item = glade_palette_new_item (palette, adaptor);
+          gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (priv->composite_templates),
+                                      GTK_TOOL_ITEM (item), -1);
+        }
+    }
+
+  g_list_free (adaptors);
+}
+
+static void
 glade_palette_populate (GladePalette * palette)
 {
   GList *l;
 
   g_return_if_fail (GLADE_IS_PALETTE (palette));
 
+  glade_palette_update_templates (palette);
+    
   for (l = (GList *) glade_app_get_catalogs (); l; l = l->next)
     {
       GList *groups = glade_catalog_get_widget_groups (GLADE_CATALOG (l->data));
@@ -665,6 +718,10 @@ glade_palette_init (GladePalette * palette)
   gtk_widget_set_no_show_all (GTK_WIDGET (palette), TRUE);
 
   glade_palette_populate (palette);
+
+  g_signal_connect_swapped (glade_app_get (), "widget-adaptor-registered",
+                            G_CALLBACK (glade_palette_update_templates),
+                            palette);
 }
 
 
diff --git a/gladeui/glade-widget-adaptor.c b/gladeui/glade-widget-adaptor.c
index c37798e..56affc7 100644
--- a/gladeui/glade-widget-adaptor.c
+++ b/gladeui/glade-widget-adaptor.c
@@ -1956,6 +1956,8 @@ glade_widget_adaptor_register (GladeWidgetAdaptor *adaptor)
 
   g_hash_table_insert (adaptor_hash,
                        g_memdup (&adaptor->priv->type, sizeof (GType)), adaptor);
+
+  g_signal_emit_by_name (glade_app_get (), "widget-adaptor-registered", adaptor);
 }
 
 static GladePackingDefault *
diff --git a/plugins/gtk+/glade-gtk.c b/plugins/gtk+/glade-gtk.c
index 121bf2c..e441125 100644
--- a/plugins/gtk+/glade-gtk.c
+++ b/plugins/gtk+/glade-gtk.c
@@ -1422,7 +1422,7 @@ glade_gtk_container_action_activate (GladeWidgetAdaptor *adaptor,
 	      filename = tmp;
 	    }
 	  
-	  glade_composite_template_save_from_widget (gwidget, basename, filename);
+	  glade_composite_template_save_from_widget (gwidget, basename, filename, TRUE);
 
 	  g_free (basename);
 	  g_free (filename);



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