[glade3/signal-tree-model: 61/358] signal-editor: Update model correctly when adding/changing/removing



commit e51ee00b7ecedf79ddddc47dfccaaf39827d3ebc
Author: Johannes Schmid <jhs gnome org>
Date:   Fri Jul 9 22:48:42 2010 +0200

    signal-editor: Update model correctly when adding/changing/removing
    
    Show widgets with associated signals in bold when there are handlers defined.

 gladeui/glade-signal-editor.c |   90 +++++++++++++++++++++++++++++++---------
 gladeui/glade-signal-model.c  |   74 ++++++++++++++++++++++++++++++++--
 gladeui/glade-signal-model.h  |    2 +-
 3 files changed, 140 insertions(+), 26 deletions(-)
---
diff --git a/gladeui/glade-signal-editor.c b/gladeui/glade-signal-editor.c
index 041cac2..49f308a 100644
--- a/gladeui/glade-signal-editor.c
+++ b/gladeui/glade-signal-editor.c
@@ -76,7 +76,6 @@ on_handler_edited (GtkCellRendererText* renderer,
 	GladeSignalEditor* self = GLADE_SIGNAL_EDITOR(user_data);
 	GtkTreePath* tree_path = gtk_tree_path_new_from_string (path);
 	GtkTreeIter iter;
-	gchar* name;
 	gchar* old_handler;
 	gboolean not_dummy;
 
@@ -95,29 +94,46 @@ on_handler_edited (GtkCellRendererText* renderer,
 	{
 		if (not_dummy)
 		{
-			/* change an existing signal handler */
-			GladeSignal* old_signal;
-			GladeSignal* new_signal;
-
-			gtk_tree_model_get (self->priv->model,
-			                    &iter,
-			                    GLADE_SIGNAL_COLUMN_SIGNAL,
-			                    &old_signal, -1);
-
-			new_signal = glade_signal_clone (old_signal);
-
-			/* Change the new signal handler */
-			g_free (new_signal->handler);
-			new_signal->handler = g_strdup(handler);
-
-			glade_command_change_signal (self->priv->widget, old_signal, new_signal);
-
-			glade_signal_free (new_signal);
+			if (strlen (handler))
+			{
+				/* change an existing signal handler */
+				GladeSignal* old_signal;
+				GladeSignal* new_signal;
+
+				gtk_tree_model_get (self->priv->model,
+				                    &iter,
+				                    GLADE_SIGNAL_COLUMN_SIGNAL,
+				                    &old_signal, -1);
+
+				new_signal = glade_signal_clone (old_signal);
+
+				/* Change the new signal handler */
+				g_free (new_signal->handler);
+				new_signal->handler = g_strdup(handler);
+
+				glade_command_change_signal (self->priv->widget, old_signal, new_signal);
+
+				glade_signal_free (new_signal);
+			}
+			else
+			{
+				GladeSignal* deleted_signal;
+				gtk_tree_model_get (self->priv->model,
+				                    &iter,
+				                    GLADE_SIGNAL_COLUMN_SIGNAL,
+				                    &deleted_signal, -1);
+				
+				
+				/* Delete signal */
+				glade_command_remove_signal (self->priv->widget,
+				                             deleted_signal);
+			}
 		}
 		else
 		{
 			GtkTreeIter parent;
 			GladeSignal* signal;
+			gchar* name;
 
 			/* Get the signal name */
 			gtk_tree_model_iter_parent (self->priv->model, &parent, &iter);
@@ -135,9 +151,9 @@ on_handler_edited (GtkCellRendererText* renderer,
 			                          self->priv->column_userdata,
 			                          TRUE);
 			glade_signal_free (signal);
+			g_free (name);
 		}
 	}
-	g_free (name);
 	g_free (old_handler);
 	gtk_tree_path_free (tree_path);
 }
@@ -322,6 +338,37 @@ glade_signal_editor_dispose (GObject *object)
 }
 
 static void
+name_cell_data_func (GtkTreeViewColumn* column,
+                     GtkCellRenderer* renderer,
+                     GtkTreeModel* model,
+                     GtkTreeIter* iter,
+                     gpointer data)
+{
+	gchar* name;
+	gboolean bold;
+	gboolean handler;
+	PangoWeight weight = PANGO_WEIGHT_NORMAL;
+
+	gtk_tree_model_get (model, iter,
+	                    GLADE_SIGNAL_COLUMN_NAME, &name,
+	                    GLADE_SIGNAL_COLUMN_HAS_HANDLERS, &bold,
+	                    GLADE_SIGNAL_COLUMN_IS_HANDLER, &handler,
+	                    -1);
+	
+	if (bold)
+		weight = PANGO_WEIGHT_BOLD;
+
+	g_object_set (renderer, 
+	              "text", name,
+	              "weight", weight,
+	              "visible", !handler,
+	              NULL);
+	
+	g_free (name);
+}
+	
+
+static void
 glade_signal_editor_init (GladeSignalEditor *self)
 {
 	GtkWidget *scroll;
@@ -339,8 +386,9 @@ glade_signal_editor_init (GladeSignalEditor *self)
 	renderer = gtk_cell_renderer_text_new ();
 	priv->column_name = gtk_tree_view_column_new_with_attributes (_("Signal"),
 	                                                              renderer,
-	                                                              "text", GLADE_SIGNAL_COLUMN_NAME,
 	                                                              NULL);
+	gtk_tree_view_column_set_cell_data_func (priv->column_name, renderer,
+	                                         name_cell_data_func, self, NULL);
 	gtk_tree_view_append_column (GTK_TREE_VIEW (self->priv->signal_tree), priv->column_name);
 
 	/* Signal handler */
diff --git a/gladeui/glade-signal-model.c b/gladeui/glade-signal-model.c
index f8a7cdb..37aa3f3 100644
--- a/gladeui/glade-signal-model.c
+++ b/gladeui/glade-signal-model.c
@@ -209,6 +209,8 @@ glade_signal_model_get_column_type (GtkTreeModel* model,
 			return G_TYPE_BOOLEAN;
 		case GLADE_SIGNAL_COLUMN_NOT_DUMMY:
 			return G_TYPE_BOOLEAN;
+		case GLADE_SIGNAL_COLUMN_HAS_HANDLERS:
+			return G_TYPE_BOOLEAN;
 		case GLADE_SIGNAL_COLUMN_SIGNAL:
 			return G_TYPE_POINTER;
 		default:
@@ -308,6 +310,43 @@ static GList* glade_signal_model_create_signal_list (GladeSignalModel* sig_model
 }
 
 static gboolean
+glade_signal_model_has_handlers (GladeSignalModel* model, GtkTreeIter* iter)
+{
+	const gchar* widget = iter->user_data;
+	GladeSignalClass* sig_class = iter->user_data2;
+	GladeSignal* signal = iter->user_data3;
+
+	if (signal)
+		return FALSE;
+
+	if (sig_class)
+	{
+		GPtrArray* handlers = g_hash_table_lookup (model->priv->widget->signals,
+		                                           sig_class->name);
+		if (handlers && handlers->len > 0)
+			return TRUE;
+	}
+	else if (widget)
+	{
+		GList* signals = glade_signal_model_create_signal_list (model, widget);
+		GList* class_iter;
+		for (class_iter = signals; class_iter != NULL; class_iter = g_list_next (class_iter))
+		{
+			sig_class = class_iter->data;
+			GPtrArray* handlers = g_hash_table_lookup (model->priv->widget->signals,
+			                                           sig_class->name);
+			if (handlers && handlers->len > 0)
+			{
+				g_list_free (signals);
+				return TRUE;
+			}
+		}
+		g_list_free (signals);
+	}
+	return FALSE;		 
+}
+
+static gboolean
 glade_signal_model_iter_for_signal (GladeSignalModel* model, const GladeSignal* signal, GtkTreeIter* iter)
 {
 	GList* list;
@@ -329,6 +368,30 @@ glade_signal_model_iter_for_signal (GladeSignalModel* model, const GladeSignal*
 	return FALSE;
 }
 
+/* Be sure to update the parent columns when signals are added/removed
+ * as that might affect the appearance */
+static void
+glade_signal_model_update_parents (GladeSignalModel* model,
+                                   GtkTreeIter* iter)
+{
+	GtkTreeIter sig_class_iter;
+	GtkTreeIter widget_iter;
+	GtkTreePath* sig_class_path;
+	GtkTreePath* widget_path;
+
+	gtk_tree_model_iter_parent (GTK_TREE_MODEL (model), &sig_class_iter, iter);
+	gtk_tree_model_iter_parent (GTK_TREE_MODEL (model), &widget_iter, &sig_class_iter);
+
+	sig_class_path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &sig_class_iter);
+	widget_path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &widget_iter);
+
+	gtk_tree_model_row_changed (GTK_TREE_MODEL (model), sig_class_path, &sig_class_iter);
+	gtk_tree_model_row_changed (GTK_TREE_MODEL (model), widget_path, &widget_iter);
+
+	gtk_tree_path_free (sig_class_path);
+	gtk_tree_path_free (widget_path);
+}
+
 static void
 on_glade_signal_model_added (GladeWidget* widget, const GladeSignal* signal,
                              GladeSignalModel* model)
@@ -341,6 +404,7 @@ on_glade_signal_model_added (GladeWidget* widget, const GladeSignal* signal,
 		gtk_tree_model_row_inserted (GTK_TREE_MODEL (model),
 		                             path,
 		                             &iter);
+		glade_signal_model_update_parents (model, &iter);
 		gtk_tree_path_free (path);
 		model->priv->stamp++;
 	}
@@ -358,6 +422,7 @@ on_glade_signal_model_removed (GladeWidget* widget, const GladeSignal* signal,
 		gtk_tree_model_row_deleted (GTK_TREE_MODEL (model),
 		                            gtk_tree_model_get_path (GTK_TREE_MODEL (model),
 		                                                     &iter));
+		glade_signal_model_update_parents (model, &iter);
 		gtk_tree_path_free (path);
 		model->priv->stamp++;
 	}
@@ -617,15 +682,16 @@ glade_signal_model_get_value (GtkTreeModel* model,
 			g_value_set_boolean (value,
 			                     widget && sig_class && handler);
 			break;
-		case GLADE_SIGNAL_COLUMN_IS_LABEL:
-			g_value_set_boolean (value,
-			                     !(widget && sig_class && handler));
-			break;
 		case GLADE_SIGNAL_COLUMN_NOT_DUMMY:
 			g_value_set_boolean (value,
 				                 glade_signal_model_not_dummy_handler (sig_model,
 				                                                      iter));
 			break;
+		case GLADE_SIGNAL_COLUMN_HAS_HANDLERS:
+			g_value_set_boolean (value,
+			                     glade_signal_model_has_handlers (sig_model,
+			                                                      iter));
+			break;
 		case GLADE_SIGNAL_COLUMN_SIGNAL:
 			g_value_set_pointer (value, handler);
 			break;
diff --git a/gladeui/glade-signal-model.h b/gladeui/glade-signal-model.h
index 3aa9c34..504157d 100644
--- a/gladeui/glade-signal-model.h
+++ b/gladeui/glade-signal-model.h
@@ -44,9 +44,9 @@ typedef enum
 	GLADE_SIGNAL_COLUMN_SWAP,
 	GLADE_SIGNAL_COLUMN_AFTER,
 	GLADE_SIGNAL_COLUMN_IS_HANDLER,
-	GLADE_SIGNAL_COLUMN_IS_LABEL,
 	GLADE_SIGNAL_COLUMN_NOT_DUMMY,
 	GLADE_SIGNAL_COLUMN_VERSION_WARNING,
+	GLADE_SIGNAL_COLUMN_HAS_HANDLERS,
 	GLADE_SIGNAL_COLUMN_SIGNAL,
 	GLADE_SIGNAL_N_COLUMNS
 } GladeSignalModelColumns;



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