[glade3/signal-tree-model: 73/358] signal-editor: Implement basic drag and drop support



commit 5299bcfdc68d27f66581e3fee2d8494dd28f504f
Author: Johannes Schmid <jhs gnome org>
Date:   Sun Jul 25 13:02:02 2010 +0200

    signal-editor: Implement basic drag and drop support

 gladeui/glade-signal-editor.c |   33 ++++++++++++++++++++++
 gladeui/glade-signal-editor.h |    2 +
 gladeui/glade-signal-model.c  |   61 ++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 95 insertions(+), 1 deletions(-)
---
diff --git a/gladeui/glade-signal-editor.c b/gladeui/glade-signal-editor.c
index 49f308a..0ff49d8 100644
--- a/gladeui/glade-signal-editor.c
+++ b/gladeui/glade-signal-editor.c
@@ -327,6 +327,39 @@ glade_signal_editor_load_widget (GladeSignalEditor *editor,
 	g_object_set (priv->renderer_userdata, "model", glade_app_get_project (), NULL);
 }
 
+/**
+ * glade_signal_editor_enable_dnd:
+ * @editor: a #GladeSignalEditor
+ * @enabled: whether the drag and drop support should be enabled
+ *
+ * If drag and drop support is enabled, the user will be able to drag signal handler
+ * from the tree to some editor. The type of the dnd data will be "application/x-glade-signal"
+ * and it will be in the form of "widget:signal:handler" so for example 
+ * "GtkToggleButton:toggled:on_toggle_button_toggled".
+ */ 
+void 
+glade_signal_editor_enable_dnd (GladeSignalEditor *editor, gboolean enabled)
+{
+	GladeSignalEditorPrivate *priv = editor->priv;
+	if (enabled)
+	{
+		const GtkTargetEntry entry = {
+			"application/x-glade-signal",
+			GTK_TARGET_OTHER_WIDGET,
+			1
+		};
+		gtk_drag_source_set (priv->signal_tree,
+		                     0,
+		                     &entry,
+		                     1,
+		                     GDK_ACTION_COPY);
+	}
+	else
+	{
+		gtk_drag_source_unset (priv->signal_tree);
+	}
+}
+
 static void
 glade_signal_editor_dispose (GObject *object)
 {
diff --git a/gladeui/glade-signal-editor.h b/gladeui/glade-signal-editor.h
index 6efd69b..8cde757 100644
--- a/gladeui/glade-signal-editor.h
+++ b/gladeui/glade-signal-editor.h
@@ -39,6 +39,8 @@ GladeSignalEditor *glade_signal_editor_new (void);
 
 void glade_signal_editor_load_widget (GladeSignalEditor *editor, GladeWidget *widget);
 
+void glade_signal_editor_enable_dnd (GladeSignalEditor *editor, gboolean enabled);
+
 G_END_DECLS
 
 #endif /* __GLADE_SIGNAL_EDITOR_H__ */
diff --git a/gladeui/glade-signal-model.c b/gladeui/glade-signal-model.c
index 37aa3f3..dbe1650 100644
--- a/gladeui/glade-signal-model.c
+++ b/gladeui/glade-signal-model.c
@@ -44,6 +44,7 @@ enum
 };
 
 static void gtk_tree_model_iface_init (GtkTreeModelIface* iface);
+static void gtk_tree_drag_source_iface_init (GtkTreeDragSourceIface* iface);
 
 static void
 on_glade_signal_model_added (GladeWidget* widget, const GladeSignal* signal,
@@ -57,7 +58,9 @@ on_glade_signal_model_changed (GladeWidget* widget, const GladeSignal* old_signa
 
 G_DEFINE_TYPE_WITH_CODE (GladeSignalModel, glade_signal_model, G_TYPE_OBJECT,
                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
-                                      gtk_tree_model_iface_init))
+                                      gtk_tree_model_iface_init);
+                         G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
+                                      gtk_tree_drag_source_iface_init))
 
 static void
 glade_signal_model_init (GladeSignalModel *object)
@@ -935,4 +938,60 @@ gtk_tree_model_iface_init (GtkTreeModelIface *iface)
 	iface->iter_n_children = glade_signal_model_iter_n_children;
 	iface->iter_nth_child = glade_signal_model_iter_nth_child;
 	iface->iter_parent = glade_signal_model_iter_parent;
+}
+
+static gboolean
+glade_signal_model_row_draggable (GtkTreeDragSource* model, 
+                                  GtkTreePath* path)
+{
+	GtkTreeIter iter;
+	gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path);
+
+	return glade_signal_model_not_dummy_handler (GLADE_SIGNAL_MODEL (model),
+	                                             &iter);
+}
+
+static gboolean
+glade_signal_model_drag_data_get (GtkTreeDragSource* model, 
+                                  GtkTreePath* path,
+                                  GtkSelectionData* data)
+{
+	GtkTreeIter iter;
+
+	if (gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
+	{
+		GladeSignal* signal;
+		const gchar* widget = iter.user_data;
+		gchar* dnd_text;
+
+		gtk_tree_model_get (GTK_TREE_MODEL (model), &iter,
+		                    GLADE_SIGNAL_COLUMN_SIGNAL, &signal, -1);
+
+		dnd_text = g_strdup_printf ("%s:%s:%s", widget, signal->name, signal->handler);
+		gtk_selection_data_set_text (data, dnd_text, strlen (dnd_text));
+
+		g_free (dnd_text);
+		
+		return TRUE;
+	}
+	else
+	{
+		return FALSE;
+	}
+}
+
+static gboolean
+glade_signal_model_drag_data_delete (GtkTreeDragSource* model, 
+                                     GtkTreePath* path)
+{
+	/* We don't move rows... */
+	return FALSE;
+}
+
+static void
+gtk_tree_drag_source_iface_init (GtkTreeDragSourceIface* iface)
+{
+	iface->row_draggable = glade_signal_model_row_draggable;
+	iface->drag_data_get = glade_signal_model_drag_data_get;
+	iface->drag_data_delete = glade_signal_model_drag_data_delete;
 }
\ No newline at end of file



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