[glade/composite-templates-new: 1/3] GladeWidget: Added glade_widget_depends()



commit 2771403d891374ad190240b84d5d9dad11d58fb7
Author: Tristan Van Berkom <tristan van berkom gmail com>
Date:   Sat Mar 30 23:28:28 2013 +0900

    GladeWidget: Added glade_widget_depends()
    
    A highly recursive function that runs glade_widget_adaptor_depends().
    
    Updated the GladeProject to use glade_widget_depends() at save
    time only on the toplevels.

 gladeui/glade-project.c |   16 +++++++-----
 gladeui/glade-widget.c  |   61 +++++++++++++++++++++++++++++++++++++++++++++++
 gladeui/glade-widget.h  |    2 +
 3 files changed, 72 insertions(+), 7 deletions(-)
---
diff --git a/gladeui/glade-project.c b/gladeui/glade-project.c
index 642adcb..b1c71e8 100644
--- a/gladeui/glade-project.c
+++ b/gladeui/glade-project.c
@@ -1899,9 +1899,9 @@ sort_project_dependancies (GObject *a, GObject *b)
   ga = glade_widget_get_from_gobject (a);
   gb = glade_widget_get_from_gobject (b);
 
-  if (glade_widget_adaptor_depends (glade_widget_get_adaptor (ga), ga, gb))
+  if (glade_widget_depends (ga, gb))
     return 1;
-  else if (glade_widget_adaptor_depends (glade_widget_get_adaptor (gb), gb, ga))
+  else if (glade_widget_depends (gb, ga))
     return -1;
   else
     return strcmp (glade_widget_get_name (ga), glade_widget_get_name (gb));
@@ -1915,6 +1915,7 @@ glade_project_write (GladeProject *project)
   GladeXmlDoc *doc;
   GladeXmlNode *root;           /* *comment_node; */
   GList *list;
+  GList *toplevels;
 
   doc = glade_xml_doc_new ();
   context = glade_xml_context_new (doc, NULL);
@@ -1934,12 +1935,11 @@ glade_project_write (GladeProject *project)
 
   glade_project_write_resource_path (project, context, root);
 
-  /* Sort the whole thing */
-  project->priv->objects =
-      g_list_sort (project->priv->objects,
-                   (GCompareFunc) sort_project_dependancies);
+  /* Sort the toplevels */
+  toplevels = g_list_copy (project->priv->tree);
+  toplevels = g_list_sort (toplevels, (GCompareFunc)sort_project_dependancies);
 
-  for (list = project->priv->objects; list; list = list->next)
+  for (list = toplevels; list; list = list->next)
     {
       GladeWidget *widget;
 
@@ -1953,6 +1953,8 @@ glade_project_write (GladeProject *project)
         glade_widget_write (widget, context, root);
     }
 
+  g_list_free (toplevels);
+
   return context;
 }
 
diff --git a/gladeui/glade-widget.c b/gladeui/glade-widget.c
index 6bb76be..956d4f8 100644
--- a/gladeui/glade-widget.c
+++ b/gladeui/glade-widget.c
@@ -4023,6 +4023,67 @@ glade_widget_is_ancestor (GladeWidget * widget, GladeWidget * ancestor)
 }
 
 /**
+ * glade_widget_depends:
+ * @widget: a #GladeWidget
+ * @other: another #GladeWidget
+ *
+ * Determines whether @widget is somehow dependent on @other, in
+ * which case it should be serialized after @other.
+ *
+ * A widget is dependent on another widget if @widget or any
+ * of @widget's children depends on @other or any of @other's children.
+ *
+ * <note><para>This is a very recursive operation, it is used once when
+ * saving the project to ensure proper order at save time.</para></note>
+ *
+ * Return value: %TRUE if @widget depends on @other.
+ **/
+gboolean
+glade_widget_depends (GladeWidget      *widget,
+                     GladeWidget      *other)
+{
+  g_return_val_if_fail (GLADE_IS_WIDGET (widget), FALSE);
+  g_return_val_if_fail (GLADE_IS_WIDGET (other), FALSE);
+
+  if (!glade_widget_adaptor_depends (widget->priv->adaptor, widget, other))
+    {
+      gboolean depends;
+      GList *children, *l;
+
+      /* Recurse into 'other' */
+      children = glade_widget_get_children (other);
+      for (depends = FALSE, l = children;
+          depends == FALSE && l != NULL;
+          l = l->next)
+       {
+         GladeWidget *child = glade_widget_get_from_gobject (l->data);
+
+         depends = glade_widget_depends (widget, child);
+       }
+
+      g_list_free (children);
+      if (depends)
+       return TRUE;
+
+      /* Recurse into 'widget' */
+      children = glade_widget_get_children (widget);
+      for (depends = FALSE, l = children;
+          depends == FALSE && l != NULL;
+          l = l->next)
+       {
+         GladeWidget *child = glade_widget_get_from_gobject (l->data);
+
+         depends = glade_widget_depends (child, other);
+       }
+      g_list_free (children);
+
+      return depends;
+    }
+
+  return TRUE;
+}
+
+/**
  * glade_widget_get_device_from_event:
  * @event: a GdkEvent
  * 
diff --git a/gladeui/glade-widget.h b/gladeui/glade-widget.h
index 7762ced..0e19c2f 100644
--- a/gladeui/glade-widget.h
+++ b/gladeui/glade-widget.h
@@ -215,6 +215,8 @@ gchar                  *glade_widget_generate_path_name     (GladeWidget      *w
 
 gboolean                glade_widget_is_ancestor            (GladeWidget      *widget,
                                                             GladeWidget      *ancestor);
+gboolean                glade_widget_depends                (GladeWidget      *widget,
+                                                            GladeWidget      *other);
 
 GdkDevice              *glade_widget_get_device_from_event  (GdkEvent *event);
 


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