[glade/glade-3-8] * gladeui/glade-project.c: Fixed GladeProject to only ever report widgets that are actually in t



commit 808026f04cc7108935d6a38ebcc5f91cef9d5e8d
Author: Tristan Van Berkom <tristan van berkom gmail com>
Date:   Thu Feb 3 18:18:29 2011 +0900

    	* gladeui/glade-project.c: Fixed GladeProject to only ever report widgets that are
    	  actually in the project from the GtkTreeModel interface.
    
    Conflicts:
    
    	ChangeLog
    	gladeui/glade-project.c

 ChangeLog                |    3 +
 gladeui/glade-project.c  |  464 +++++++++++++++++++++++++---------------------
 gladeui/glade-property.c |   10 +-
 gladeui/glade-widget.c   |    3 +-
 4 files changed, 260 insertions(+), 220 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index e57e197..6a8e1bb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2011-02-03  Tristan Van Berkom <tristanvb openismus com>
 
+	* gladeui/glade-project.c: Fixed GladeProject to only ever report widgets that are
+	  actually in the project from the GtkTreeModel interface.
+
 	* gladeui/glade-widget.[ch], gladeui/glade-project.c, gladeui/glade-utils.c: Now update
 	  the GladeWidget's project pointer when going in and out of a project.
 
diff --git a/gladeui/glade-project.c b/gladeui/glade-project.c
index 5b7ba0f..d8744db 100644
--- a/gladeui/glade-project.c
+++ b/gladeui/glade-project.c
@@ -4664,6 +4664,90 @@ glade_project_display_dependencies (GladeProject *project)
 }
 
 /* GtkTreeModel implementation */
+static GObject *
+glade_project_nth_child (GladeProject *project, 
+			 GladeWidget  *parent, 
+			 gint          nth)
+{
+  GList   *children, *list;
+  GObject *child = NULL;
+  gint     i;
+
+  children = glade_widget_get_children (parent);
+
+  for (list = children, i = 0; list; list = list->next)
+    {
+      child = list->data;
+
+      if (!glade_project_has_object (project, child))
+	continue;
+
+      if (i == nth)
+	break;
+
+      child = NULL;
+      i++;
+    }
+
+  g_list_free (children);
+
+  return child;
+}
+
+static gint
+glade_project_child_position (GladeProject *project, 
+			      GladeWidget  *parent, 
+			      GObject      *child)
+{
+  GList   *children, *list;
+  GObject *iter;
+  gint     i, position = -1;
+
+  children = glade_widget_get_children (parent);
+
+  for (list = children, i = 0; list; list = list->next)
+    {
+      iter = list->data;
+
+      if (!glade_project_has_object (project, iter))
+	continue;
+
+      if (iter == child)
+	{
+	  position = i;
+	  break;
+	}
+      i++;
+    }
+
+  g_list_free (children);
+
+  return position;
+}
+
+static gint
+glade_project_count_children (GladeProject *project, 
+			      GladeWidget  *parent)
+{
+  GList   *children, *list;
+  GObject *iter;
+  gint     i;
+
+  children = glade_widget_get_children (parent);
+
+  for (list = children, i = 0; list; list = list->next)
+    {
+      iter = list->data;
+
+      if (!glade_project_has_object (project, iter))
+	continue;
+      i++;
+    }
+
+  g_list_free (children);
+
+  return i;
+}
 
 static void
 glade_project_model_get_iter_for_object (GladeProject* project,
@@ -4711,124 +4795,100 @@ glade_project_model_get_column_type (GtkTreeModel* model,
 }
 
 static gboolean
-glade_project_model_get_iter (GtkTreeModel* model,
-                              GtkTreeIter* iter,
-                              GtkTreePath* path)
-{
-	GladeProject* project = GLADE_PROJECT (model);
-	gint* indices = gtk_tree_path_get_indices (path);
-	gint depth = gtk_tree_path_get_depth (path);
-	GladeWidget* widget;
-	GObject* object;
-
-	gint i;
-	GList* parent;
-	
-	if ((parent = g_list_nth (project->priv->tree, indices[0])) != NULL)
-	{
-		object = parent->data;
-		widget = glade_widget_get_from_gobject (object);
-	}
-	else
-	{
-		iter->stamp = 0;
-		iter->user_data = NULL;
-		return FALSE;
-	}
-
-	for (i = 1; i < depth; i++)
-	{		
-		GList* children = glade_widget_get_children (widget);
-		GList* node;
-		if (!children)
-		{
-			iter->stamp = 0;
-			iter->user_data = NULL;
-			return FALSE;
-		}
+glade_project_model_get_iter (GtkTreeModel * model,
+                              GtkTreeIter * iter, GtkTreePath * path)
+{
+  GladeProject *project = GLADE_PROJECT (model);
+  gint         *indices = gtk_tree_path_get_indices (path);
+  gint          depth = gtk_tree_path_get_depth (path);
+  GladeWidget  *widget;
+  GObject      *object;
+  gint          i;
+  GList        *parent;
+
+  if ((parent = g_list_nth (project->priv->tree, indices[0])) != NULL)
+    {
+      object = parent->data;
+      widget = glade_widget_get_from_gobject (object);
+    }
+  else
+    {
+      iter->stamp = 0;
+      iter->user_data = NULL;
+      return FALSE;
+    }
+
+  for (i = 1; i < depth; i++)
+    {
+      object = glade_project_nth_child (project, widget, indices[i]);
+
+      if (!object)
+        {
+          iter->stamp = 0;
+          iter->user_data = NULL;
+          return FALSE;
+        }
 
-		node = g_list_nth (children, indices[i]);
-		
-		if (node)
-		{
-			object = G_OBJECT (node->data);
-		}
-		g_list_free (children);
+      widget = glade_widget_get_from_gobject (object);
+    }
 
-		if (!node)
-		{
-			iter->stamp = 0;
-			iter->user_data = NULL;
-			return FALSE;
-		}
-		
-		widget = 
-			glade_widget_get_from_gobject (object);
-	}
-
-	if (object)
-	{
-		glade_project_model_get_iter_for_object (project,
-							 object,
-							 iter);
-		return TRUE;
-	}
-	else
-	{
-		iter->stamp = 0;
-		iter->user_data = NULL;
-		return FALSE;
-	}
+  if (object)
+    {
+      glade_project_model_get_iter_for_object (project, object, iter);
+      return TRUE;
+    }
+  else
+    {
+      iter->stamp = 0;
+      iter->user_data = NULL;
+      return FALSE;
+    }
 }
 
-static GtkTreePath*
-glade_project_model_get_path (GtkTreeModel* model,
-                              GtkTreeIter* iter)
+static GtkTreePath *
+glade_project_model_get_path (GtkTreeModel * model, GtkTreeIter * iter)
 {
-	GladeProject* project = GLADE_PROJECT (model);
-	GtkTreePath* path;
-	GObject* object;
-	GladeWidget* widget;
-	GladeWidget* toplevel;
-	GladeWidget* parent;
-	GList* top;
+  GladeProject *project = GLADE_PROJECT (model);
+  GtkTreePath  *path;
+  GObject      *object;
+  GladeWidget  *widget;
+  GladeWidget  *toplevel;
+  GladeWidget  *parent;
+  GList        *top;
 
-	g_return_val_if_fail (VALID_ITER (project, iter), NULL);
+  g_return_val_if_fail (VALID_ITER (project, iter), NULL);
 
-	object   = iter->user_data;
-	widget   = glade_widget_get_from_gobject (object);
-	toplevel = glade_widget_get_toplevel (widget);
-	parent   = widget;
+  object   = iter->user_data;
+  widget   = glade_widget_get_from_gobject (object);
+  toplevel = glade_widget_get_toplevel (widget);
+  parent   = widget;
 
-	path = gtk_tree_path_new ();
+  path = gtk_tree_path_new ();
 
-	while ((parent = glade_widget_get_parent (widget)) != NULL)
-	{
-		GList* children = glade_widget_get_children (parent);
-		GList* child = g_list_find (children, glade_widget_get_object (widget));
+  while ((parent = glade_widget_get_parent (widget)) != NULL)
+    {
+      gint position;
 
-		if (!child)
-			gtk_tree_path_prepend_index (path,
-			                             0);
-		else
-			gtk_tree_path_prepend_index (path,
-			                             g_list_position (children, child));
-		
-		g_list_free (children);
-		widget = parent;
-	}
+      if ((position = 
+	   glade_project_child_position (project, parent, 
+					 glade_widget_get_object (widget))) < 0)
+        gtk_tree_path_prepend_index (path, 0);
+      else
+        gtk_tree_path_prepend_index (path, position);
 
-	/* Get the index for the top-level list */
-	top = g_list_find (project->priv->tree, 
-	                   glade_widget_get_object (toplevel));
+      widget = parent;
+    }
 
-	/* While the project is disposing widgets are unparented and sometimes no longer in the tree */
-	if (top)
-		gtk_tree_path_prepend_index (path, g_list_position (project->priv->tree, top));
-	else
-		gtk_tree_path_prepend_index (path, 0);
-	
-	return path;
+  /* Get the index for the top-level list */
+  top = g_list_find (project->priv->tree, glade_widget_get_object (toplevel));
+
+  /* While the project is disposing widgets are unparented and sometimes no longer in the tree */
+  if (top)
+    gtk_tree_path_prepend_index (path, g_list_position (project->priv->tree, top));
+  else
+    gtk_tree_path_prepend_index (path, 0);
+  
+  return path;
 }
 
 static void
@@ -4883,26 +4943,25 @@ glade_project_model_get_value (GtkTreeModel* model,
 }
 
 static gboolean
-glade_project_model_iter_next (GtkTreeModel* model,
-                               GtkTreeIter* iter)
+glade_project_model_iter_next (GtkTreeModel * model, GtkTreeIter * iter)
 {
-	GladeProject* project = GLADE_PROJECT (model);
-	GObject* object = iter->user_data;
-	GladeWidget* widget;
-	GladeWidget* parent;
-	GList* children;
-	GList* child;
-	GList* next;
+	GladeProject *project = GLADE_PROJECT (model);
+	GObject *object = iter->user_data;
+	GladeWidget *widget;
+	GladeWidget *parent;
+	GList *children;
+	GList *child;
+	GList *next;
 	gboolean retval = FALSE;
 
 	g_return_val_if_fail (VALID_ITER (project, iter), FALSE);
 
 	widget = glade_widget_get_from_gobject (object);
 	parent = glade_widget_get_parent (widget);
-	
+
 	if (parent)
 	{
-		children = glade_widget_get_children(parent);
+		children = glade_widget_get_children (parent);
 	}
 	else
 	{
@@ -4912,12 +4971,18 @@ glade_project_model_iter_next (GtkTreeModel* model,
 	child = g_list_find (children, object);
 	if (child)
 	{
-		next = g_list_next (child);
+	    /* Get the next child that is actually in the project */
+		for (next = child->next; next; next = next->next)
+		{
+			GObject *object = next->data;
+
+			if (glade_project_has_object (project, object))
+				break;
+		}
+
 		if (next)
 		{
-			glade_project_model_get_iter_for_object (project,
-			                                         next->data,
-			                                         iter);
+			glade_project_model_get_iter_for_object (project, next->data, iter);
 			retval = TRUE;
 		}
 	}
@@ -4931,19 +4996,16 @@ static gboolean
 glade_project_model_iter_has_child (GtkTreeModel* model,
                                     GtkTreeIter* iter)
 {
-	GladeWidget* widget;
-	GList* children;
+	GladeProject *project = GLADE_PROJECT (model);
+	GladeWidget  *widget;
 
 	g_return_val_if_fail (VALID_ITER (model, iter), FALSE);
 
 	widget = glade_widget_get_from_gobject (iter->user_data);
-	children = glade_widget_get_children(widget);
 
-	if (children != NULL)
-	{
-		g_list_free (children);
+	if (glade_project_count_children (project, widget) > 0)
 		return TRUE;
-	}
+
 	return FALSE;
 }
 
@@ -4957,108 +5019,79 @@ glade_project_model_iter_n_children (GtkTreeModel* model,
 
 	if (iter)
 	{
-		GladeWidget* widget = glade_widget_get_from_gobject (iter->user_data);
-		GList* children = glade_widget_get_children(widget);
-		if (children)
-		{
-			gint n = g_list_length (children);
-			g_list_free (children);
-			return n;
-		}
+		GladeWidget *widget = glade_widget_get_from_gobject (iter->user_data);
+
+		return glade_project_count_children (project, widget);
 	}
-	else
-		return g_list_length (project->priv->tree);
-	return 0;
+
+	return g_list_length (project->priv->tree);
 }
 
 static gboolean
-glade_project_model_iter_nth_child (GtkTreeModel* model,
-                                    GtkTreeIter* iter,
-                                    GtkTreeIter* parent,
-                                    gint n)
+glade_project_model_iter_nth_child (GtkTreeModel * model,
+                                    GtkTreeIter * iter,
+                                    GtkTreeIter * parent, gint nth)
 {
-	GladeProject* project = GLADE_PROJECT (model);
-	GList* children;
+  GladeProject *project = GLADE_PROJECT (model);
+  GObject      *object = NULL;
 
-	g_return_val_if_fail (parent == NULL || VALID_ITER (project, parent), FALSE);
+  g_return_val_if_fail (parent == NULL || VALID_ITER (project, parent), FALSE);
 
-	if (parent != NULL)
-	{
-		GObject* object = parent->user_data;
-		GladeWidget* widget = glade_widget_get_from_gobject (object);
-		children = glade_widget_get_children(widget);
-	}		
-	else
-	{
-		children = project->priv->tree;
-	}
-	
-	if (children)
-	{
-		gboolean retval = FALSE;
-		GList* child = g_list_nth (children, n);
-		if (child)
-		{
-			glade_project_model_get_iter_for_object (project,
-			                                         child->data,
-			                                         iter);
-			retval = TRUE;
-		}
-		else
-		{
-			iter->stamp = 0;
-			iter->user_data = NULL;
-		}
+  if (parent != NULL)
+    {
+      GObject     *obj    = parent->user_data;
+      GladeWidget *widget = glade_widget_get_from_gobject (obj);
 
-		if (children != project->priv->tree)
-			g_list_free (children);
+      object = glade_project_nth_child (project, widget, nth);
+    }
+  else if (project->priv->tree)
+    {
+      GList *child = 
+	g_list_nth (project->priv->tree, nth);
 
-		return retval;
-	}
+      if (child)
+	object = child->data;
+    }
 
-	iter->stamp = 0;
-	iter->user_data = NULL;
+  if (object)
+    {
+      glade_project_model_get_iter_for_object (project, object, iter);
+      return TRUE;
+    }
 
-	return FALSE;
+  iter->stamp     = 0;
+  iter->user_data = NULL;
+
+  return FALSE;
 }
 
 static gboolean
-glade_project_model_iter_children (GtkTreeModel* model,
-                                   GtkTreeIter* iter,
-                                   GtkTreeIter* parent)
+glade_project_model_iter_children (GtkTreeModel * model,
+                                   GtkTreeIter * iter, GtkTreeIter * parent)
 {
-	GladeProject* project = GLADE_PROJECT (model);
+  GladeProject *project = GLADE_PROJECT (model);
+  GObject      *object  = NULL;
 
-	g_return_val_if_fail (parent == NULL || VALID_ITER (project, parent), FALSE);
+  g_return_val_if_fail (parent == NULL || VALID_ITER (project, parent), FALSE);
 
-	if (parent)
-	{
-		GladeWidget* widget = glade_widget_get_from_gobject (parent->user_data);
-		GList* children = glade_widget_get_children (widget);
-		
-		if (children)
-		{
-			glade_project_model_get_iter_for_object (project,
-			                                         G_OBJECT(children->data),
-			                                         iter);
-			g_list_free (children);
-			return TRUE;
-		}
-	}
-	else
-	{
-		if (project->priv->tree)
-		{
-			glade_project_model_get_iter_for_object (project,
-			                                         G_OBJECT(project->priv->tree->data),
-			                                         iter);
-			return TRUE;
-		}
-	}
+  if (parent)
+    {
+      GladeWidget *widget = glade_widget_get_from_gobject (parent->user_data);
 
-	iter->stamp = 0;
-	iter->user_data = NULL;
-	return FALSE;
+      object = glade_project_nth_child (project, widget, 0);
+    }
+  else if (project->priv->tree)
+      object = project->priv->tree->data;
+
+  if (object)
+    {
+      glade_project_model_get_iter_for_object (project, object, iter);
+      return TRUE;
+    }
+
+  iter->stamp = 0;
+  iter->user_data = NULL;
+  return FALSE;
 }
 
 static gboolean
@@ -5075,11 +5108,12 @@ glade_project_model_iter_parent (GtkTreeModel* model,
 	widget = glade_widget_get_from_gobject (child->user_data);
 	parent = glade_widget_get_parent (widget);
 
-	if (parent)
+	if (parent && 
+	    glade_project_has_object (project, glade_widget_get_object (parent)))
 	{
 		glade_project_model_get_iter_for_object (project,
-			                                 glade_widget_get_object (parent),
-	        		                         iter);
+							 glade_widget_get_object (parent),
+							 iter);
 		return TRUE;
 	}
 
diff --git a/gladeui/glade-property.c b/gladeui/glade-property.c
index ad654f4..e70a7bd 100644
--- a/gladeui/glade-property.c
+++ b/gladeui/glade-property.c
@@ -130,7 +130,8 @@ glade_property_equals_value_impl (GladeProperty *property,
 	if (property->widget)
 	{
 		project = glade_widget_get_project (property->widget);
-		fmt     = glade_project_get_format (project);
+		if (project)
+			fmt = glade_project_get_format (project);
 	}
 
 	return !glade_property_class_compare (property->klass, property->value, value, fmt);
@@ -290,11 +291,12 @@ glade_property_set_value_impl (GladeProperty *property, const GValue *value)
 	/* Add/Remove references from widget ref stacks here
 	 * (before assigning the value)
 	 */
-	if (property->widget && changed && glade_property_class_is_object 
-	    (property->klass, glade_project_get_format (project)))
+	if (property->widget && changed && 
+	    glade_property_class_is_object (property->klass, 
+					    project ? glade_project_get_format (project) : 
+					    GLADE_PROJECT_FORMAT_GTKBUILDER))
 		glade_property_update_prop_refs (property, property->value, value);
 
-
 	/* Make a copy of the old value */
 	g_value_init (&old_value, G_VALUE_TYPE (property->value));
 	g_value_copy (property->value, &old_value);
diff --git a/gladeui/glade-widget.c b/gladeui/glade-widget.c
index a0bf81e..bbe63f6 100644
--- a/gladeui/glade-widget.c
+++ b/gladeui/glade-widget.c
@@ -842,7 +842,8 @@ reset_object_property (GladeProperty *property,
 		       GladeProject  *project)
 {
 	if (glade_property_class_is_object (property->klass, 
-					    glade_project_get_format (project)))
+					    project ? glade_project_get_format (project) :
+					    GLADE_PROJECT_FORMAT_LIBGLADE))
 		glade_property_reset (property);
 }
 



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