Initial German translation of the build tutorial
[anjuta.git] / plugins / file-loader / dnd.c
blob3a0d37fedb980e7a9d0870a75e42d657a5b7bf01
1 /*
2 * dnd.c (c) Johannes Schmid, 2009
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version 2
6 * of the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
16 * USA.
19 #include <gtk/gtk.h>
20 #include <glib.h>
21 #include <gdk/gdk.h>
23 #include <libanjuta/anjuta-utils.h>
25 #include "dnd.h"
27 #define TARGET_URI_LIST 100
29 void (* dnd_data_dropped) (GFile *uri, gpointer data) = NULL;
31 static GdkAtom
32 drag_get_uri_target (GtkWidget *widget,
33 GdkDragContext *context)
35 GdkAtom target;
36 GtkTargetList *tl;
38 tl = gtk_target_list_new (NULL, 0);
39 gtk_target_list_add_uri_targets (tl, 0);
41 target = gtk_drag_dest_find_target (widget, context, tl);
42 gtk_target_list_unref (tl);
44 return target;
47 static gboolean
48 dnd_drag_drop (GtkWidget *widget,
49 GdkDragContext *context,
50 gint x,
51 gint y,
52 guint timestamp)
54 gboolean result = FALSE;
55 GdkAtom target;
57 /* If this is a URL, just get the drag data */
58 target = drag_get_uri_target (widget, context);
60 if (target != GDK_NONE)
62 gtk_drag_get_data (widget, context, target, timestamp);
63 result = TRUE;
66 return result;
70 * Callback for the drag_data_received signal, emitted whenever something is
71 * dragged onto the widget.
73 static void
74 drag_data_received_cb (GtkWidget *widget, GdkDragContext *context,
75 gint x, gint y, GtkSelectionData *data,
76 guint info, guint time, gpointer user_data)
78 GSList* files;
79 /* If this is an URL emit DROP_URIS, otherwise chain up the signal */
80 if (info == TARGET_URI_LIST)
82 files = anjuta_utils_drop_get_files (data);
84 if (files != NULL)
86 GSList* node;
87 for (node = files; node != NULL; node = g_slist_next(node))
89 GFile* file = node->data;
90 dnd_data_dropped (file, user_data);
91 g_object_unref (file);
93 g_slist_free (files);
94 gtk_drag_finish (context, TRUE, FALSE, time);
96 gtk_drag_finish (context, FALSE, FALSE, time);
101 * Initialize widget to start accepting "droppings" for the (NULL terminated)
102 * list of mime types.
104 void
105 dnd_drop_init (GtkWidget *widget,
106 void (* data_dropped) (GFile* file, gpointer user_data),
107 gpointer user_data)
109 GtkTargetList* tl;
110 GtkTargetEntry accepted[1];
112 accepted[0].target = "application-x/anjuta";
113 accepted[0].info = 0;
114 accepted[0].flags = 0;
116 /* Drag and drop support */
117 gtk_drag_dest_set (widget, GTK_DEST_DEFAULT_ALL, accepted, 1, GDK_ACTION_COPY);
118 tl = gtk_drag_dest_get_target_list (widget);
119 gtk_target_list_add_uri_targets (tl, TARGET_URI_LIST);
120 dnd_data_dropped = *data_dropped;
121 g_signal_connect (G_OBJECT (widget), "drag_data_received",
122 G_CALLBACK (drag_data_received_cb),
123 (gpointer) user_data);
124 g_signal_connect (G_OBJECT (widget), "drag-drop",
125 G_CALLBACK (dnd_drag_drop),
126 (gpointer) user_data);
127 return;
130 void
131 dnd_drop_finalize (GtkWidget *widget, gpointer user_data)
133 g_signal_handlers_disconnect_by_func (G_OBJECT (widget),
134 G_CALLBACK (drag_data_received_cb), user_data);
135 g_signal_handlers_disconnect_by_func (G_OBJECT (widget),
136 G_CALLBACK (dnd_drag_drop), user_data);
137 dnd_data_dropped = NULL;