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,
23 #include <libanjuta/anjuta-utils.h>
27 #define TARGET_URI_LIST 100
29 void (* dnd_data_dropped
) (GFile
*uri
, gpointer data
) = NULL
;
32 drag_get_uri_target (GtkWidget
*widget
,
33 GdkDragContext
*context
)
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
);
48 dnd_drag_drop (GtkWidget
*widget
,
49 GdkDragContext
*context
,
54 gboolean result
= FALSE
;
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
);
70 * Callback for the drag_data_received signal, emitted whenever something is
71 * dragged onto the widget.
74 drag_data_received_cb (GtkWidget
*widget
, GdkDragContext
*context
,
75 gint x
, gint y
, GtkSelectionData
*data
,
76 guint info
, guint time
, gpointer user_data
)
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
);
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
);
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.
105 dnd_drop_init (GtkWidget
*widget
,
106 void (* data_dropped
) (GFile
* file
, gpointer user_data
),
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
);
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
;