[glade3/signal-tree-model] signal-editor: Add drag and drop animation



commit 03f6fb63040d8b98e70304c1c6a572b115404f47
Author: Johannes Schmid <jhs gnome org>
Date:   Sun Aug 1 16:21:10 2010 +0200

    signal-editor: Add drag and drop animation

 gladeui/glade-signal-editor.c |   97 ++++++++++++++++++++++++++++++++++++++---
 gladeui/glade-signal-model.c  |   18 ++++++--
 2 files changed, 104 insertions(+), 11 deletions(-)
---
diff --git a/gladeui/glade-signal-editor.c b/gladeui/glade-signal-editor.c
index 0ff49d8..9261da7 100644
--- a/gladeui/glade-signal-editor.c
+++ b/gladeui/glade-signal-editor.c
@@ -348,15 +348,16 @@ glade_signal_editor_enable_dnd (GladeSignalEditor *editor, gboolean enabled)
 			GTK_TARGET_OTHER_WIDGET,
 			1
 		};
-		gtk_drag_source_set (priv->signal_tree,
-		                     0,
-		                     &entry,
-		                     1,
-		                     GDK_ACTION_COPY);
+		/* DND */
+		gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW(priv->signal_tree),
+		                                        GDK_BUTTON1_MASK,
+		                                        &entry,
+		                                        1,
+		                                        GDK_ACTION_COPY);
 	}
 	else
 	{
-		gtk_drag_source_unset (priv->signal_tree);
+		gtk_tree_view_unset_rows_drag_source (GTK_TREE_VIEW (priv->signal_tree));
 	}
 }
 
@@ -399,7 +400,83 @@ name_cell_data_func (GtkTreeViewColumn* column,
 	
 	g_free (name);
 }
+
+static GdkPixmap*
+create_rich_drag_icon (GtkWidget* widget, const gchar* text)
+{
+	PangoLayout* layout = pango_layout_new (gtk_widget_get_pango_context (widget));
+	GdkPixmap* pixmap;
+	cairo_t cr;
+	gint width, height;
+	
+	pango_layout_set_text (layout, text, -1);
+	pango_layout_get_size (layout, &width, &height);
+	width = PANGO_PIXELS(width) + 10;
+	height = PANGO_PIXELS(height) + 10;
+	
+	pixmap = gdk_pixmap_new (gtk_widget_get_window (widget),
+	                         width,
+	                         height,
+	                         -1);
+	cr = gdk_cairo_create (pixmap);
+	gdk_cairo_set_source_pixmap (cr, pixmap, 0, 0);
+	
+	gdk_draw_rectangle (GDK_DRAWABLE (pixmap),
+	                    gtk_widget_get_style (widget)->base_gc [gtk_widget_get_state (widget)],
+	                    TRUE, 
+	                    0, 0,
+	                    width, height);
+	
+	gdk_draw_layout (GDK_DRAWABLE (pixmap),
+	                 gtk_widget_get_style (widget)->text_gc [gtk_widget_get_state (widget)],
+	                 5,
+	                 5,
+	                 layout);
+	gdk_draw_rectangle (GDK_DRAWABLE (pixmap),
+	                    gtk_widget_get_style (widget)->black_gc,
+	                    FALSE,
+	                    0, 0,
+	                    width - 1, height - 1);
+	g_object_unref (layout);
+
 	
+	return pixmap;
+}
+
+static void
+glade_signal_editor_drag_begin (GtkWidget* widget,
+                                GdkDragContext* context,
+                                gpointer user_data)
+{
+	GdkPixmap     *pixmap = NULL;
+	GtkTreeModel *model;
+	GtkTreeIter iter;
+	GtkTreeSelection* selection;
+
+	selection =  gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
+
+	if (gtk_tree_selection_get_selected (selection, &model, &iter))
+	{
+		gchar* handler;
+		gtk_tree_model_get (model, &iter,
+		                    GLADE_SIGNAL_COLUMN_HANDLER, &handler, -1);
+		pixmap = create_rich_drag_icon (widget, handler);
+	}
+	
+	if (pixmap)
+	{
+		gtk_drag_set_icon_pixmap (context,
+		                          gdk_drawable_get_colormap (GDK_DRAWABLE(gtk_widget_get_window (widget))),
+		                          pixmap,
+		                          NULL,
+		                          -2, -2);
+		g_object_unref (pixmap);
+	}
+	else
+	{
+		gtk_drag_set_icon_default (context);
+	}
+}
 
 static void
 glade_signal_editor_init (GladeSignalEditor *self)
@@ -484,9 +561,15 @@ glade_signal_editor_init (GladeSignalEditor *self)
 					     GTK_SHADOW_IN);
 	
 	gtk_container_add (GTK_CONTAINER (scroll), self->priv->signal_tree);
-
+	
 	gtk_box_pack_start (GTK_BOX (self), scroll, TRUE, TRUE, 0);
 
+	/* Dnd */
+	g_signal_connect_after (self->priv->signal_tree,
+	                        "drag-begin",
+	                        G_CALLBACK(glade_signal_editor_drag_begin),
+	                        self);
+	
 	gtk_widget_show_all (GTK_WIDGET(self));
 }
 
diff --git a/gladeui/glade-signal-model.c b/gladeui/glade-signal-model.c
index dbe1650..a027c12 100644
--- a/gladeui/glade-signal-model.c
+++ b/gladeui/glade-signal-model.c
@@ -945,10 +945,16 @@ glade_signal_model_row_draggable (GtkTreeDragSource* model,
                                   GtkTreePath* path)
 {
 	GtkTreeIter iter;
+	gboolean is_handler;
+	gboolean not_dummy;
 	gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path);
 
-	return glade_signal_model_not_dummy_handler (GLADE_SIGNAL_MODEL (model),
-	                                             &iter);
+	gtk_tree_model_get (GTK_TREE_MODEL (model), &iter,
+	                    GLADE_SIGNAL_COLUMN_IS_HANDLER, &is_handler,
+	                    GLADE_SIGNAL_COLUMN_NOT_DUMMY, &not_dummy,
+	                    -1);
+	
+	return (is_handler && not_dummy);
 }
 
 static gboolean
@@ -968,10 +974,14 @@ glade_signal_model_drag_data_get (GtkTreeDragSource* model,
 		                    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_message ("Sent: %s", dnd_text);
+		gtk_selection_data_set (data,
+		                        gdk_atom_intern_static_string ("application/x-glade-signal"),
+		                        8,
+		                        (guchar*) dnd_text,
+		                        strlen (dnd_text));
 
 		g_free (dnd_text);
-		
 		return TRUE;
 	}
 	else



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