4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* dnd.c - code for handling drag and drop */
31 #include <sys/param.h>
34 #include <X11/Xatom.h>
40 #include "view_iface.h"
41 #include "collection.h"
47 #include "gui_support.h"
54 #include "usericons.h"
57 #define MAXURILEN 4096 /* Longest URI to allow */
59 gint drag_start_x
, drag_start_y
;
60 MotionType motion_state
= MOTION_NONE
;
62 static GList
*prompt_local_paths
= NULL
;
63 static gchar
*prompt_dest_path
= NULL
;
65 /* This keeps track of how many mouse buttons are currently down.
66 * We add a grab when it does 0->1 and release it on 1<-0.
68 * It may also be set to zero to disable the motion system (eg,
69 * when popping up a menu).
71 gint motion_buttons_pressed
= 0;
73 /* Static prototypes */
74 static void set_xds_prop(GdkDragContext
*context
, char *text
);
75 static gboolean
drag_motion(GtkWidget
*widget
,
76 GdkDragContext
*context
,
80 FilerWindow
*filer_window
);
81 static void drag_leave(GtkWidget
*widget
,
82 GdkDragContext
*context
,
84 FilerWindow
*filer_window
);
85 static void desktop_drag_data_received(GtkWidget
*widget
,
86 GdkDragContext
*context
,
89 GtkSelectionData
*selection_data
,
92 FilerWindow
*filer_window
);
93 static void got_data_xds_reply(GtkWidget
*widget
,
94 GdkDragContext
*context
,
95 GtkSelectionData
*selection_data
,
97 static void got_data_raw(GtkWidget
*widget
,
98 GdkDragContext
*context
,
99 GtkSelectionData
*selection_data
,
101 static void got_uri_list(GtkWidget
*widget
,
102 GdkDragContext
*context
,
103 GtkSelectionData
*selection_data
,
105 static void drag_end(GtkWidget
*widget
,
106 GdkDragContext
*context
,
107 FilerWindow
*filer_window
);
108 static gboolean
drag_drop(GtkWidget
*widget
,
109 GdkDragContext
*context
,
114 static void drag_data_received(GtkWidget
*widget
,
115 GdkDragContext
*context
,
118 GtkSelectionData
*selection_data
,
122 static gboolean
spring_now(gpointer data
);
123 static void spring_win_destroyed(GtkWidget
*widget
, gpointer data
);
124 static void menuitem_response(gpointer data
, guint action
, GtkWidget
*widget
);
125 static void prompt_action(GList
*paths
, gchar
*dest
);
135 static GtkItemFactoryEntry menu_def
[] = {
136 {N_("Copy"), NULL
, menuitem_response
, MENU_COPY
, NULL
},
137 {N_("Move"), NULL
, menuitem_response
, MENU_MOVE
, NULL
},
138 {N_("Link"), NULL
, menuitem_response
, MENU_LINK
, NULL
},
139 {"", NULL
, NULL
, 0, "<Separator>"},
140 {N_("Set Icon"), NULL
, menuitem_response
, MENU_SET_ICON
, NULL
},
142 static GtkWidget
*dnd_menu
= NULL
;
144 /* The handler of the signal handler for scroll events.
145 * This is used to cancel spring loading when autoscrolling is used.
147 static gulong scrolled_signal
= -1;
148 static GtkObject
*scrolled_adj
= NULL
; /* The object watched */
150 /* Possible values for drop_dest_type (can also be NULL).
151 * In either case, drop_dest_path is the app/file/dir to use.
153 char *drop_dest_prog
= "drop_dest_prog"; /* Run a program */
154 char *drop_dest_dir
= "drop_dest_dir"; /* Save to path */
156 GdkAtom XdndDirectSave0
;
157 GdkAtom xa_text_plain
;
158 GdkAtom text_uri_list
;
159 GdkAtom application_octet_stream
;
160 GdkAtom xa_string
; /* Not actually used for DnD, but the others are here! */
162 static Option o_dnd_drag_to_icons
;
163 Option o_dnd_spring_open
;
164 static Option o_dnd_spring_delay
;
165 static Option o_dnd_middle_menu
;
169 XdndDirectSave0
= gdk_atom_intern("XdndDirectSave0", FALSE
);
170 xa_text_plain
= gdk_atom_intern("text/plain", FALSE
);
171 text_uri_list
= gdk_atom_intern("text/uri-list", FALSE
);
172 application_octet_stream
= gdk_atom_intern("application/octet-stream",
174 xa_string
= gdk_atom_intern("STRING", FALSE
);
176 option_add_int(&o_dnd_drag_to_icons
, "dnd_drag_to_icons", 1);
177 option_add_int(&o_dnd_spring_open
, "dnd_spring_open", 0);
178 option_add_int(&o_dnd_spring_delay
, "dnd_spring_delay", 400);
179 option_add_int(&o_dnd_middle_menu
, "dnd_middle_menu", TRUE
);
182 /* SUPPORT FUNCTIONS */
184 /* Set the XdndDirectSave0 property on the source window for this context */
185 static void set_xds_prop(GdkDragContext
*context
, char *text
)
187 gdk_property_change(context
->source_window
,
190 GDK_PROP_MODE_REPLACE
,
195 static char *get_xds_prop(GdkDragContext
*context
)
200 if (gdk_property_get(context
->source_window
,
206 &length
, &prop_text
) && prop_text
)
208 /* Terminate the string */
209 prop_text
= g_realloc(prop_text
, length
+ 1);
210 prop_text
[length
] = '\0';
217 /* Is the sender willing to supply this target type? */
218 gboolean
provides(GdkDragContext
*context
, GdkAtom target
)
220 GList
*targets
= context
->targets
;
222 while (targets
&& ((GdkAtom
) targets
->data
!= target
))
223 targets
= targets
->next
;
225 return targets
!= NULL
;
228 /* Convert a list of URIs into a list of strings.
229 * Lines beginning with # are skipped.
230 * The text block passed in is zero terminated (after the final CRLF)
232 GList
*uri_list_to_glist(const char *uri_list
)
242 linebreak
= strchr(uri_list
, 13);
244 if (!linebreak
|| linebreak
[1] != 10)
246 delayed_error("uri_list_to_glist: %s",
247 _("Incorrect or missing line "
248 "break in text/uri-list data"));
252 length
= linebreak
- uri_list
;
254 if (length
&& uri_list
[0] != '#')
256 uri
= g_strndup(uri_list
, length
);
257 list
= g_list_append(list
, uri
);
260 uri_list
= linebreak
+ 2;
266 /* DRAGGING FROM US */
268 /* The user has held the mouse button down over a group of item and moved -
269 * start a drag. 'uri_list' is copied, so you can delete it straight away.
271 void drag_selection(GtkWidget
*widget
, GdkEventMotion
*event
, guchar
*uri_list
)
273 GdkDragContext
*context
;
274 GdkDragAction actions
;
275 GtkTargetList
*target_list
;
276 GtkTargetEntry target_table
[] = {
277 {"text/uri-list", 0, TARGET_URI_LIST
},
280 if (event
->state
& GDK_BUTTON1_MASK
)
281 actions
= GDK_ACTION_COPY
| GDK_ACTION_MOVE
282 | GDK_ACTION_LINK
| GDK_ACTION_ASK
;
285 if (o_dnd_middle_menu
.int_value
)
286 actions
= GDK_ACTION_ASK
;
288 actions
= GDK_ACTION_MOVE
;
291 target_list
= gtk_target_list_new(target_table
, 1);
293 context
= gtk_drag_begin(widget
,
296 (event
->state
& GDK_BUTTON1_MASK
) ? 1 :
297 (event
->state
& GDK_BUTTON2_MASK
) ? 2 : 3,
300 g_dataset_set_data_full(context
, "uri_list",
301 g_strdup(uri_list
), g_free
);
303 gtk_drag_set_icon_pixbuf(context
, im_multiple
->pixbuf
, 0, 0);
306 /* Copy/Load this item into another directory/application */
307 void drag_one_item(GtkWidget
*widget
,
308 GdkEventMotion
*event
,
309 const guchar
*full_path
,
314 GdkDragContext
*context
;
315 GdkDragAction actions
;
316 GtkTargetList
*target_list
;
317 GtkTargetEntry target_table
[] = {
318 {"text/uri-list", 0, TARGET_URI_LIST
},
319 {"application/octet-stream", 0, TARGET_RAW
},
323 g_return_if_fail(full_path
!= NULL
);
324 g_return_if_fail(item
!= NULL
);
329 if (item
->base_type
== TYPE_FILE
)
331 MIME_type
*t
= item
->mime_type
;
333 target_table
[2].target
= g_strconcat(t
->media_type
, "/",
335 target_list
= gtk_target_list_new(target_table
, 3);
336 g_free(target_table
[2].target
);
339 target_list
= gtk_target_list_new(target_table
, 1);
341 if (event
->state
& GDK_BUTTON1_MASK
)
342 actions
= GDK_ACTION_COPY
| GDK_ACTION_ASK
343 | GDK_ACTION_MOVE
| GDK_ACTION_LINK
;
346 if (o_dnd_middle_menu
.int_value
)
347 actions
= GDK_ACTION_ASK
;
349 actions
= GDK_ACTION_MOVE
;
352 context
= gtk_drag_begin(widget
,
355 (event
->state
& GDK_BUTTON1_MASK
) ? 1 :
356 (event
->state
& GDK_BUTTON2_MASK
) ? 2 : 3,
359 g_dataset_set_data_full(context
, "full_path",
360 g_strdup(full_path
), g_free
);
361 uri
= g_strconcat("file://", our_host_name_for_dnd(),
362 full_path
, "\r\n", NULL
);
363 g_dataset_set_data_full(context
, "uri_list", uri
, g_free
);
365 g_return_if_fail(image
!= NULL
);
367 gtk_drag_set_icon_pixbuf(context
, image
->pixbuf
, 0, 0);
370 static void drag_end(GtkWidget
*widget
,
371 GdkDragContext
*context
,
372 FilerWindow
*filer_window
)
374 if (filer_window
->temp_item_selected
)
376 view_clear_selection(VIEW(filer_window
->view
));
377 filer_window
->temp_item_selected
= FALSE
;
381 /* Called when a remote app wants us to send it some data.
382 * TODO: Maybe we should handle errors better (ie, let the remote app know
383 * the drag has failed)?
385 void drag_data_get(GtkWidget
*widget
,
386 GdkDragContext
*context
,
387 GtkSelectionData
*selection_data
,
392 char *to_send
= "E"; /* Default to sending an error */
393 long to_send_length
= 1;
394 gboolean delete_once_sent
= FALSE
;
398 type
= gdk_x11_xatom_to_atom(XA_STRING
);
403 path
= g_dataset_get_data(context
, "full_path");
404 if (path
&& load_file(path
, &to_send
, &to_send_length
))
406 delete_once_sent
= TRUE
;
407 type
= selection_data
->target
;
410 g_warning("drag_data_get: Can't find path!\n");
412 case TARGET_URI_LIST
:
413 to_send
= g_dataset_get_data(context
, "uri_list");
414 to_send_length
= strlen(to_send
);
415 type
= text_uri_list
; /* (needed for xine) */
416 delete_once_sent
= FALSE
;
419 delayed_error("drag_data_get: %s",
420 _("Internal error - bad info type"));
424 gtk_selection_data_set(selection_data
,
430 if (delete_once_sent
)
436 /* Set up this widget as a drop-target.
437 * Does not attach any motion handlers.
439 void make_drop_target(GtkWidget
*widget
, GtkDestDefaults defaults
)
441 GtkTargetEntry target_table
[] =
443 {"text/uri-list", 0, TARGET_URI_LIST
},
444 {"XdndDirectSave0", 0, TARGET_XDS
},
445 {"application/octet-stream", 0, TARGET_RAW
},
448 gtk_drag_dest_set(widget
,
451 sizeof(target_table
) / sizeof(*target_table
),
452 GDK_ACTION_COPY
| GDK_ACTION_ASK
| GDK_ACTION_MOVE
453 | GDK_ACTION_LINK
| GDK_ACTION_PRIVATE
);
455 g_signal_connect(widget
, "drag_drop", G_CALLBACK(drag_drop
), NULL
);
456 g_signal_connect(widget
, "drag_data_received",
457 G_CALLBACK(drag_data_received
), NULL
);
460 /* Set up this filer window as a drop target. Called once, when the
461 * filer window is first created.
463 void drag_set_dest(FilerWindow
*filer_window
)
465 GtkWidget
*widget
= GTK_WIDGET(filer_window
->collection
);
467 make_drop_target(widget
, 0);
469 g_signal_connect(widget
, "drag_motion",
470 G_CALLBACK(drag_motion
), filer_window
);
471 g_signal_connect(widget
, "drag_leave",
472 G_CALLBACK(drag_leave
), filer_window
);
473 g_signal_connect(widget
, "drag_end",
474 G_CALLBACK(drag_end
), filer_window
);
477 /* Like drag_set_dest, but for a pinboard-type widget */
478 void drag_set_pinboard_dest(GtkWidget
*widget
)
480 GtkTargetEntry target_table
[] = {
481 {"text/uri-list", 0, TARGET_URI_LIST
},
484 gtk_drag_dest_set(widget
,
485 GTK_DEST_DEFAULT_DROP
,
487 sizeof(target_table
) / sizeof(*target_table
),
489 g_signal_connect(widget
, "drag_data_received",
490 G_CALLBACK(desktop_drag_data_received
), NULL
);
493 /* Called during the drag when the mouse is in a widget registered
494 * as a drop target. Returns TRUE if we can accept the drop.
496 static gboolean
drag_motion(GtkWidget
*widget
,
497 GdkDragContext
*context
,
501 FilerWindow
*filer_window
)
505 GdkDragAction action
= context
->suggested_action
;
506 char *new_path
= NULL
;
508 gboolean retval
= FALSE
;
510 if (o_dnd_drag_to_icons
.int_value
)
511 item_number
= collection_get_item(filer_window
->collection
,
516 item
= item_number
>= 0
517 ? (DirItem
*) filer_window
->collection
->items
[item_number
].data
520 if (item
&& filer_window
->collection
->items
[item_number
].selected
)
523 type
= dnd_motion_item(context
, &item
);
528 /* Don't allow drops to non-writeable directories. BUT, still
529 * allow drops on non-writeable SUBdirectories so that we can
530 * do the spring-open thing.
532 if (item
&& type
== drop_dest_dir
&&
533 !(item
->flags
& ITEM_FLAG_APPDIR
))
536 /* XXX: This is needed so that directories don't
537 * spring open while we scroll. Should go in
538 * view_collection.c, I think.
540 GtkObject
*vadj
= GTK_OBJECT(filer_window
->collection
->vadj
);
542 /* Subdir: prepare for spring-open */
543 if (scrolled_adj
!= vadj
)
546 gtk_signal_disconnect(scrolled_adj
,
549 scrolled_signal
= gtk_signal_connect(
552 GTK_SIGNAL_FUNC(scrolled
),
553 filer_window
->collection
);
556 dnd_spring_load(context
, filer_window
);
563 collection_set_cursor_item(filer_window
->collection
,
568 collection_set_cursor_item(filer_window
->collection
, -1);
570 /* Disallow background drops within a single window */
571 if (type
&& gtk_drag_get_source_widget(context
) == widget
)
578 new_path
= make_path(filer_window
->sym_path
,
579 item
->leafname
)->str
;
581 new_path
= filer_window
->sym_path
;
584 g_dataset_set_data(context
, "drop_dest_type", type
);
587 gdk_drag_status(context
, action
, time
);
588 g_dataset_set_data_full(context
, "drop_dest_path",
589 g_strdup(new_path
), g_free
);
596 /* item is the item the file is held over, NULL for directory background.
597 * 'item' may be NULL on exit if the drop should be treated as onto the
598 * background. Disallow drags to a selected icon before calling this.
600 * Returns NULL to reject the drop, or drop_dest_prog/drop_dest_dir to
601 * accept. Build the path based on item.
603 guchar
*dnd_motion_item(GdkDragContext
*context
, DirItem
**item_p
)
605 DirItem
*item
= *item_p
;
609 /* If we didn't drop onto a directory, application or
610 * executable file then act as though the drop is to the
613 if (item
->base_type
!= TYPE_DIRECTORY
614 && !(item
->mime_type
== application_executable
))
623 /* Drop onto the window background */
625 return drop_dest_dir
;
628 /* Drop onto a program/directory of some sort */
630 if (item
->base_type
== TYPE_DIRECTORY
&&
631 !(item
->flags
& ITEM_FLAG_APPDIR
))
633 /* A normal directory */
634 if (provides(context
, text_uri_list
) ||
635 provides(context
, XdndDirectSave0
))
636 return drop_dest_dir
;
640 if (provides(context
, text_uri_list
) ||
641 provides(context
, application_octet_stream
))
642 return drop_dest_prog
;
648 /* Remove highlights */
649 static void drag_leave(GtkWidget
*widget
,
650 GdkDragContext
*context
,
652 FilerWindow
*filer_window
)
657 g_signal_handler_disconnect(scrolled_adj
, scrolled_signal
);
662 /* User has tried to drop some data on us. Decide what format we would
665 static gboolean
drag_drop(GtkWidget
*widget
,
666 GdkDragContext
*context
,
672 const char *error
= NULL
;
673 char *leafname
= NULL
;
674 GdkAtom target
= GDK_NONE
;
676 char *dest_type
= NULL
;
678 dest_path
= g_dataset_get_data(context
, "drop_dest_path");
679 dest_type
= g_dataset_get_data(context
, "drop_dest_type");
681 g_return_val_if_fail(dest_path
!= NULL
, TRUE
);
683 if (dest_type
== drop_dest_dir
&& provides(context
, XdndDirectSave0
))
685 leafname
= get_xds_prop(context
);
688 if (strchr(leafname
, '/'))
690 error
= _("XDS protocol error: "
691 "leafname may not contain '/'\n");
700 uri
= g_string_new(NULL
);
701 g_string_sprintf(uri
, "file://%s%s",
702 our_host_name_for_dnd(),
705 set_xds_prop(context
, uri
->str
);
706 g_string_free(uri
, TRUE
);
708 target
= XdndDirectSave0
;
709 g_dataset_set_data_full(context
, "leafname",
715 "XdndDirectSave0 target provided, but the atom "
716 "XdndDirectSave0 (type text/plain) did not "
717 "contain a leafname\n");
719 else if (provides(context
, text_uri_list
))
720 target
= text_uri_list
;
721 else if (provides(context
, application_octet_stream
))
722 target
= application_octet_stream
;
725 if (dest_type
== drop_dest_dir
)
726 error
= _("Sorry - I require a target type of "
727 "text/uri-list or XdndDirectSave0.");
729 error
= _("Sorry - I require a target type of "
730 "text/uri-list or application/octet-stream.");
735 gtk_drag_finish(context
, FALSE
, FALSE
, time
); /* Failure */
737 delayed_error("%s", error
);
740 gtk_drag_get_data(widget
, context
, target
, time
);
745 /* Called when a text/uri-list arrives */
746 static void desktop_drag_data_received(GtkWidget
*widget
,
747 GdkDragContext
*context
,
750 GtkSelectionData
*selection_data
,
753 FilerWindow
*filer_window
)
758 if (!selection_data
->data
)
764 if (pinboard_drag_in_progress
)
766 pinboard_move_icons();
770 gdk_window_get_position(widget
->window
, &dx
, &dy
);
774 uris
= uri_list_to_glist(selection_data
->data
);
776 for (next
= uris
; next
; next
= next
->next
)
780 path
= get_local_path((gchar
*) next
->data
);
783 pinboard_pin(path
, NULL
, x
, y
);
794 /* Called when some data arrives from the remote app (which we asked for
797 static void drag_data_received(GtkWidget
*widget
,
798 GdkDragContext
*context
,
801 GtkSelectionData
*selection_data
,
806 if (!selection_data
->data
)
809 gtk_drag_finish(context
, FALSE
, FALSE
, time
); /* Failure */
816 got_data_xds_reply(widget
, context
,
817 selection_data
, time
);
820 got_data_raw(widget
, context
, selection_data
, time
);
822 case TARGET_URI_LIST
:
823 got_uri_list(widget
, context
, selection_data
, time
);
826 gtk_drag_finish(context
, FALSE
, FALSE
, time
);
827 delayed_error("drag_data_received: %s",
828 _("Unknown target"));
833 static void got_data_xds_reply(GtkWidget
*widget
,
834 GdkDragContext
*context
,
835 GtkSelectionData
*selection_data
,
838 gboolean mark_unsafe
= TRUE
;
839 char response
= *selection_data
->data
;
840 const char *error
= NULL
;
843 dest_path
= g_dataset_get_data(context
, "drop_dest_path");
845 if (selection_data
->length
!= 1)
850 /* Sender couldn't save there - ask for another
853 if (provides(context
, application_octet_stream
))
855 mark_unsafe
= FALSE
; /* Wait and see */
857 gtk_drag_get_data(widget
, context
,
858 application_octet_stream
, time
);
861 error
= _("Remote app can't or won't send me "
864 else if (response
== 'S')
866 /* Success - data is saved */
867 mark_unsafe
= FALSE
; /* It really is safe */
868 gtk_drag_finish(context
, TRUE
, FALSE
, time
);
870 refresh_dirs(dest_path
);
872 else if (response
!= 'E')
874 error
= _("XDS protocol error: "
875 "return code should be 'S', 'F' or 'E'\n");
877 /* else: error has been reported by the sender */
881 set_xds_prop(context
, "");
882 /* Unsave also implies that the drag failed */
883 gtk_drag_finish(context
, FALSE
, FALSE
, time
);
887 delayed_error("%s", error
);
890 static void got_data_raw(GtkWidget
*widget
,
891 GdkDragContext
*context
,
892 GtkSelectionData
*selection_data
,
895 const char *leafname
;
897 const char *error
= NULL
;
898 const char *dest_path
;
900 g_return_if_fail(selection_data
->data
!= NULL
);
902 dest_path
= g_dataset_get_data(context
, "drop_dest_path");
904 if (context
->action
== GDK_ACTION_ASK
)
906 gtk_drag_finish(context
, FALSE
, FALSE
, time
); /* Failure */
907 delayed_error(_("Sorry, can't display a menu of actions "
908 "for a remote file / raw data."));
912 if (g_dataset_get_data(context
, "drop_dest_type") == drop_dest_prog
)
914 /* The data needs to be sent to an application */
915 run_with_data(dest_path
,
916 selection_data
->data
, selection_data
->length
);
917 gtk_drag_finish(context
, TRUE
, FALSE
, time
); /* Success! */
921 leafname
= g_dataset_get_data(context
, "leafname");
923 leafname
= _("UntitledData");
925 fd
= open(make_path(dest_path
, leafname
)->str
,
926 O_WRONLY
| O_CREAT
| O_EXCL
| O_NOCTTY
,
927 S_IRUSR
| S_IRGRP
| S_IROTH
|
928 S_IWUSR
| S_IWGRP
| S_IWOTH
);
931 error
= g_strerror(errno
);
935 selection_data
->data
,
936 selection_data
->length
) == -1)
937 error
= g_strerror(errno
);
939 if (close(fd
) == -1 && !error
)
940 error
= g_strerror(errno
);
942 refresh_dirs(dest_path
);
947 if (provides(context
, XdndDirectSave0
))
948 set_xds_prop(context
, "");
949 gtk_drag_finish(context
, FALSE
, FALSE
, time
); /* Failure */
950 delayed_error(_("Error saving file: %s"), error
);
953 gtk_drag_finish(context
, TRUE
, FALSE
, time
); /* Success! */
956 /* We've got a list of URIs from somewhere (probably another filer window).
957 * If the files are on the local machine then try to copy them ourselves,
958 * otherwise, if there was only one file and application/octet-stream was
959 * provided, get the data via the X server.
961 static void got_uri_list(GtkWidget
*widget
,
962 GdkDragContext
*context
,
963 GtkSelectionData
*selection_data
,
967 const char *error
= NULL
;
969 gboolean send_reply
= TRUE
;
973 dest_path
= g_dataset_get_data(context
, "drop_dest_path");
974 type
= g_dataset_get_data(context
, "drop_dest_type");
976 g_return_if_fail(dest_path
!= NULL
);
978 uri_list
= uri_list_to_glist(selection_data
->data
);
981 error
= _("No URIs in the text/uri-list (nothing to do!)");
982 else if (context
->action
!= GDK_ACTION_ASK
&& type
== drop_dest_prog
)
983 run_with_files(dest_path
, uri_list
);
984 else if ((!uri_list
->next
) && (!get_local_path(uri_list
->data
)))
986 /* There is one URI in the list, and it's not on the local
987 * machine. Get it via the X server if possible.
990 if (provides(context
, application_octet_stream
))
993 leaf
= strrchr(uri_list
->data
, '/');
997 leaf
= uri_list
->data
;
998 g_dataset_set_data_full(context
, "leafname",
999 g_strdup(leaf
), g_free
);
1000 gtk_drag_get_data(widget
, context
,
1001 application_octet_stream
, time
);
1005 error
= _("Can't get data from remote machine "
1006 "(application/octet-stream not provided)");
1010 GList
*local_paths
= NULL
;
1013 /* Either one local URI, or a list. If everything in the list
1014 * isn't local then we are stuck.
1017 for (next_uri
= uri_list
; next_uri
; next_uri
= next_uri
->next
)
1021 path
= get_local_path((char *) next_uri
->data
);
1024 local_paths
= g_list_append(local_paths
,
1027 error
= _("Some of these files are on a "
1028 "different machine - they will be "
1034 error
= _("None of these files are on the local "
1035 "machine - I can't operate on multiple "
1036 "remote files - sorry.");
1038 else if (context
->action
== GDK_ACTION_ASK
)
1039 prompt_action(local_paths
, dest_path
);
1040 else if (context
->action
== GDK_ACTION_MOVE
)
1041 action_move(local_paths
, dest_path
, NULL
, -1);
1042 else if (context
->action
== GDK_ACTION_COPY
)
1043 action_copy(local_paths
, dest_path
, NULL
, -1);
1044 else if (context
->action
== GDK_ACTION_LINK
)
1045 action_link(local_paths
, dest_path
, NULL
);
1047 error
= _("Unknown action requested");
1049 for (next
= local_paths
; next
; next
= next
->next
)
1051 g_list_free(local_paths
);
1056 gtk_drag_finish(context
, FALSE
, FALSE
, time
); /* Failure */
1057 delayed_error(_("Error getting file list: %s"), error
);
1059 else if (send_reply
)
1060 gtk_drag_finish(context
, TRUE
, FALSE
, time
); /* Success! */
1062 next_uri
= uri_list
;
1065 g_free(next_uri
->data
);
1066 next_uri
= next_uri
->next
;
1068 g_list_free(uri_list
);
1071 /* Called when an item from the ACTION_ASK menu is chosen */
1072 static void menuitem_response(gpointer data
, guint action
, GtkWidget
*widget
)
1074 if (action
== MENU_MOVE
)
1075 action_move(prompt_local_paths
, prompt_dest_path
, NULL
, -1);
1076 else if (action
== MENU_COPY
)
1077 action_copy(prompt_local_paths
, prompt_dest_path
, NULL
, -1);
1078 else if (action
== MENU_LINK
)
1079 action_link(prompt_local_paths
, prompt_dest_path
, NULL
);
1080 else if (action
== MENU_SET_ICON
)
1082 if (g_list_length(prompt_local_paths
) == 1)
1083 set_icon_path(prompt_dest_path
,
1084 (char*) prompt_local_paths
->data
);
1087 _("You can't use multiple files with Set Icon!"));
1091 /* When some local files are dropped somewhere with ACTION_ASK, this
1092 * function is called to display the menu.
1094 static void prompt_action(GList
*paths
, gchar
*dest
)
1098 if (prompt_local_paths
)
1100 g_list_foreach(prompt_local_paths
, (GFunc
) g_free
, NULL
);
1101 g_list_free(prompt_local_paths
);
1102 g_free(prompt_dest_path
);
1104 prompt_dest_path
= NULL
;
1105 prompt_local_paths
= NULL
;
1108 /* Make a copy of the arguments */
1109 for (next
= paths
; next
; next
= next
->next
)
1110 prompt_local_paths
= g_list_append(prompt_local_paths
,
1111 g_strdup((gchar
*) next
->data
));
1112 prompt_dest_path
= g_strdup(dest
);
1116 GtkItemFactory
*item_factory
;
1118 item_factory
= menu_create(menu_def
,
1119 sizeof(menu_def
) / sizeof(*menu_def
),
1121 dnd_menu
= gtk_item_factory_get_widget(item_factory
, "<dnd>");
1124 /* Shade 'Set Icon' if there are multiple files */
1125 menu_set_items_shaded(dnd_menu
, g_list_length(paths
) != 1, 4, 1);
1127 show_popup_menu(dnd_menu
, gtk_get_current_event(), 1);
1131 /* SPRING-LOADING */
1133 /* This is the code that makes directories pop open if you hold a
1136 * First, call dnd_spring_load(context) to arm the system.
1137 * After a timeout (1/2 a second) the dest_path directory will be
1138 * opened in a new window, unless dnd_spring_abort is called first.
1141 static gint spring_timeout
= -1;
1142 static GdkDragContext
*spring_context
= NULL
;
1143 static FilerWindow
*spring_window
= NULL
;
1144 static FilerWindow
*spring_src_window
= NULL
;
1146 void dnd_spring_load(GdkDragContext
*context
, FilerWindow
*src_win
)
1148 g_return_if_fail(context
!= NULL
);
1150 if (!o_dnd_spring_open
.int_value
)
1156 spring_context
= context
;
1157 g_object_ref(spring_context
);
1158 spring_src_window
= src_win
;
1159 spring_timeout
= gtk_timeout_add(
1160 o_dnd_spring_delay
.int_value
, spring_now
, NULL
);
1163 void dnd_spring_abort(void)
1165 if (!spring_context
)
1168 g_object_unref(spring_context
);
1169 spring_context
= NULL
;
1170 gtk_timeout_remove(spring_timeout
);
1173 /* If all mod keys are released, no buttons are pressed, and the
1174 * mouse is outside the spring window, then close it.
1176 static gboolean
spring_check_idle(gpointer data
)
1183 if (!get_pointer_xy(&p_x
, &p_y
))
1186 GdkWindow *win = spring_window->window->window;
1190 gdk_window_get_position(win, &x, &y);
1191 gdk_window_get_size(win, &w, &h);
1193 if (p_x < x || p_x > x + w || p_y < y || p_y > y + h)
1197 gtk_widget_destroy(spring_window
->window
);
1198 return FALSE
; /* Got it! */
1201 return TRUE
; /* Try again later */
1204 static gboolean
spring_now(gpointer data
)
1206 gboolean old_unique
= o_unique_filer_windows
.int_value
;
1210 g_return_val_if_fail(spring_context
!= NULL
, FALSE
);
1212 dest_path
= g_dataset_get_data(spring_context
, "drop_dest_path");
1213 g_return_val_if_fail(dest_path
!= NULL
, FALSE
);
1216 * Note: Due to a bug in gtk, if a window disappears during
1217 * a drag and the pointer moves over where the window was,
1218 * the sender crashes! Therefore, do not close any windows
1219 * while dragging! (fixed in later versions)
1223 gtk_widget_destroy(spring_window->window);
1226 get_pointer_xy(&x
, &y
);
1228 o_unique_filer_windows
.int_value
= FALSE
; /* XXX: yuck! */
1231 collection_set_cursor_item(spring_window
->collection
, -1);
1232 filer_change_to(spring_window
, dest_path
, NULL
);
1233 /* DON'T move the window. Gtk+ sometimes doesn't
1239 spring_window
= filer_opendir(dest_path
, spring_src_window
);
1242 gtk_timeout_add(500, spring_check_idle
, NULL
);
1243 g_signal_connect(spring_window
->window
, "destroy",
1244 G_CALLBACK(spring_win_destroyed
), NULL
);
1245 centre_window(spring_window
->window
->window
, x
, y
);
1248 o_unique_filer_windows
.int_value
= old_unique
;
1255 static void spring_win_destroyed(GtkWidget
*widget
, gpointer data
)
1257 spring_window
= NULL
;
1260 /* HANDLING MOTION EVENTS */
1262 /* If not-NULL, then this widget has a grab */
1263 static GtkWidget
*motion_widget
= NULL
;
1265 /* If TRUE, we must gdk_pointer_ungrab() too when finishing */
1266 static gboolean motion_pointer_grab
= FALSE
;
1268 /* Call this on a button press event. It stores the mouse position
1269 * as the start of the new drag and returns TRUE if all is well.
1270 * Further motions events are disabled at this point - you must
1271 * then call dnd_motion_start() to set the type of motion expected.
1272 * Grabs the widget on the first press.
1274 * If the system is not ready to handle a motion event (because a
1275 * button is already held down?) it does nothing and returns FALSE.
1277 * If the event is not a single click then it simply returns TRUE.
1279 gboolean
dnd_motion_press(GtkWidget
*widget
, GdkEventButton
*event
)
1281 if (event
->type
!= GDK_BUTTON_PRESS
)
1282 return TRUE
; /* Not a click event! */
1284 motion_buttons_pressed
++;
1285 if (motion_buttons_pressed
== 1)
1287 /* g_print("[ grab! ]\n"); */
1288 gtk_grab_add(widget
);
1289 motion_widget
= widget
;
1292 if (motion_state
!= MOTION_NONE
)
1293 return FALSE
; /* Ignore clicks - we're busy! */
1295 motion_state
= MOTION_DISABLED
;
1296 drag_start_x
= event
->x_root
;
1297 drag_start_y
= event
->y_root
;
1302 /* After the button press event, decide what kind of motion is expected.
1303 * If you don't call this then the motion system is disabled - call
1304 * dnd_motion_release() to reset it.
1306 * Note: If you open a popup menu or start DND call dnd_motion_ungrab()
1309 void dnd_motion_start(MotionType motion
)
1311 g_return_if_fail(motion_state
== MOTION_DISABLED
);
1313 motion_state
= motion
;
1316 /* Call this on a button release event. If some buttons are still pressed,
1317 * returns TRUE and does nothing.
1319 * Otherwise, it resets the motion system to be ready again and returns TRUE.
1321 * If the motion system wasn't being used (MOTION_NONE) then it does nothing
1322 * and returns FALSE - process the release event yourself as it isn't part
1323 * of a motion. This also happens if a motion was primed but never happened.
1325 gboolean
dnd_motion_release(GdkEventButton
*event
)
1327 MotionType motion
= motion_state
;
1330 if (motion_buttons_pressed
== 0)
1331 return TRUE
; /* We were disabled */
1333 if (motion_buttons_pressed
== 1)
1334 dnd_motion_ungrab();
1337 motion_buttons_pressed
--;
1341 if (motion
== MOTION_REPOSITION
|| motion
== MOTION_DISABLED
)
1342 return TRUE
; /* Already done something - eat the event */
1344 /* Eat release events that happen too far from the click
1345 * source. Otherwise, allow the caller to treat this as a click
1346 * that never became a motion.
1348 dx
= event
->x_root
- drag_start_x
;
1349 dy
= event
->y_root
- drag_start_y
;
1351 return ABS(dx
) > 5 || ABS(dy
) > 5;
1354 /* Use this to disable the motion system. The system will be reset once
1355 * all mouse buttons are released.
1357 void dnd_motion_disable(void)
1359 g_return_if_fail(motion_state
!= MOTION_NONE
&&
1360 motion_state
!= MOTION_DISABLED
);
1362 motion_state
= MOTION_DISABLED
;
1365 /* Use this if something else is going to grab the pointer so that
1366 * we won't get any more motion or release events.
1368 void dnd_motion_ungrab(void)
1370 if (motion_buttons_pressed
> 0)
1372 if (motion_pointer_grab
)
1374 gdk_pointer_ungrab(GDK_CURRENT_TIME
);
1375 motion_pointer_grab
= FALSE
;
1376 /* g_print("[ ungrab_pointer ]\n"); */
1378 gtk_grab_remove(motion_widget
);
1379 motion_widget
= NULL
;
1380 motion_buttons_pressed
= 0;
1381 /* g_print("[ ungrab ]\n"); */
1384 motion_state
= MOTION_NONE
;
1387 /* Call this on motion events. If the mouse position is far enough
1388 * from the click position, returns TRUE and does dnd_motion_ungrab().
1389 * You should then start regular drag-and-drop.
1391 * Otherwise, returns FALSE.
1393 gboolean
dnd_motion_moved(GdkEventMotion
*event
)
1397 dx
= event
->x_root
- drag_start_x
;
1398 dy
= event
->y_root
- drag_start_y
;
1400 if (ABS(dx
) <= 5 && ABS(dy
) <= 5)
1401 return FALSE
; /* Not far enough */
1403 dnd_motion_ungrab();
1408 /* Normally, the X server will automatically grab the pointer on a
1409 * button press and ungrab on release. However, if the grab widget
1410 * is reparented then call this to re-aquire the grab.
1412 void dnd_motion_grab_pointer(void)
1414 g_return_if_fail(motion_widget
!= NULL
);
1416 gdk_pointer_grab(motion_widget
->window
, FALSE
,
1417 GDK_POINTER_MOTION_MASK
|
1418 GDK_BUTTON_RELEASE_MASK
,
1419 FALSE
, NULL
, GDK_CURRENT_TIME
);
1421 motion_pointer_grab
= TRUE
;