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 "collection.h"
46 #include "gui_support.h"
53 #include "usericons.h"
56 #define MAXURILEN 4096 /* Longest URI to allow */
58 gint drag_start_x
, drag_start_y
;
59 MotionType motion_state
= MOTION_NONE
;
61 static GList
*prompt_local_paths
= NULL
;
62 static gchar
*prompt_dest_path
= NULL
;
64 /* This keeps track of how many mouse buttons are currently down.
65 * We add a grab when it does 0->1 and release it on 1<-0.
67 * It may also be set to zero to disable the motion system (eg,
68 * when popping up a menu).
70 gint motion_buttons_pressed
= 0;
72 /* Static prototypes */
73 static void set_xds_prop(GdkDragContext
*context
, char *text
);
74 static gboolean
drag_motion(GtkWidget
*widget
,
75 GdkDragContext
*context
,
79 FilerWindow
*filer_window
);
80 static void drag_leave(GtkWidget
*widget
,
81 GdkDragContext
*context
,
83 FilerWindow
*filer_window
);
84 static void desktop_drag_data_received(GtkWidget
*widget
,
85 GdkDragContext
*context
,
88 GtkSelectionData
*selection_data
,
91 FilerWindow
*filer_window
);
92 static void got_data_xds_reply(GtkWidget
*widget
,
93 GdkDragContext
*context
,
94 GtkSelectionData
*selection_data
,
96 static void got_data_raw(GtkWidget
*widget
,
97 GdkDragContext
*context
,
98 GtkSelectionData
*selection_data
,
100 static void got_uri_list(GtkWidget
*widget
,
101 GdkDragContext
*context
,
102 GtkSelectionData
*selection_data
,
104 static void drag_end(GtkWidget
*widget
,
105 GdkDragContext
*context
,
106 FilerWindow
*filer_window
);
107 static gboolean
drag_drop(GtkWidget
*widget
,
108 GdkDragContext
*context
,
113 static void drag_data_received(GtkWidget
*widget
,
114 GdkDragContext
*context
,
117 GtkSelectionData
*selection_data
,
121 static gboolean
spring_now(gpointer data
);
122 static void spring_win_destroyed(GtkWidget
*widget
, gpointer data
);
123 static void menuitem_response(gpointer data
, guint action
, GtkWidget
*widget
);
124 static void prompt_action(GList
*paths
, gchar
*dest
);
134 static GtkItemFactoryEntry menu_def
[] = {
135 {N_("Copy"), NULL
, menuitem_response
, MENU_COPY
, NULL
},
136 {N_("Move"), NULL
, menuitem_response
, MENU_MOVE
, NULL
},
137 {N_("Link"), NULL
, menuitem_response
, MENU_LINK
, NULL
},
138 {"", NULL
, NULL
, 0, "<Separator>"},
139 {N_("Set Icon"), NULL
, menuitem_response
, MENU_SET_ICON
, NULL
},
141 static GtkWidget
*dnd_menu
= NULL
;
143 /* The handler of the signal handler for scroll events.
144 * This is used to cancel spring loading when autoscrolling is used.
146 static gulong scrolled_signal
= -1;
147 static GtkObject
*scrolled_adj
= NULL
; /* The object watched */
149 /* Possible values for drop_dest_type (can also be NULL).
150 * In either case, drop_dest_path is the app/file/dir to use.
152 char *drop_dest_prog
= "drop_dest_prog"; /* Run a program */
153 char *drop_dest_dir
= "drop_dest_dir"; /* Save to path */
155 GdkAtom XdndDirectSave0
;
156 GdkAtom xa_text_plain
;
157 GdkAtom text_uri_list
;
158 GdkAtom application_octet_stream
;
159 GdkAtom xa_string
; /* Not actually used for DnD, but the others are here! */
161 static Option o_dnd_drag_to_icons
;
162 Option o_dnd_spring_open
;
163 static Option o_dnd_spring_delay
;
164 static Option o_dnd_middle_menu
;
168 GtkItemFactory
*item_factory
;
170 XdndDirectSave0
= gdk_atom_intern("XdndDirectSave0", FALSE
);
171 xa_text_plain
= gdk_atom_intern("text/plain", FALSE
);
172 text_uri_list
= gdk_atom_intern("text/uri-list", FALSE
);
173 application_octet_stream
= gdk_atom_intern("application/octet-stream",
175 xa_string
= gdk_atom_intern("STRING", FALSE
);
177 option_add_int(&o_dnd_drag_to_icons
, "dnd_drag_to_icons", 1);
178 option_add_int(&o_dnd_spring_open
, "dnd_spring_open", 0);
179 option_add_int(&o_dnd_spring_delay
, "dnd_spring_delay", 400);
180 option_add_int(&o_dnd_middle_menu
, "dnd_middle_menu", TRUE
);
182 item_factory
= menu_create(menu_def
,
183 sizeof(menu_def
) / sizeof(*menu_def
),
185 dnd_menu
= gtk_item_factory_get_widget(item_factory
, "<dnd>");
188 /* SUPPORT FUNCTIONS */
190 /* Set the XdndDirectSave0 property on the source window for this context */
191 static void set_xds_prop(GdkDragContext
*context
, char *text
)
193 gdk_property_change(context
->source_window
,
196 GDK_PROP_MODE_REPLACE
,
201 static char *get_xds_prop(GdkDragContext
*context
)
206 if (gdk_property_get(context
->source_window
,
212 &length
, &prop_text
) && prop_text
)
214 /* Terminate the string */
215 prop_text
= g_realloc(prop_text
, length
+ 1);
216 prop_text
[length
] = '\0';
223 /* Is the sender willing to supply this target type? */
224 gboolean
provides(GdkDragContext
*context
, GdkAtom target
)
226 GList
*targets
= context
->targets
;
228 while (targets
&& ((GdkAtom
) targets
->data
!= target
))
229 targets
= targets
->next
;
231 return targets
!= NULL
;
234 /* Convert a list of URIs into a list of strings.
235 * Lines beginning with # are skipped.
236 * The text block passed in is zero terminated (after the final CRLF)
238 GList
*uri_list_to_glist(const char *uri_list
)
248 linebreak
= strchr(uri_list
, 13);
250 if (!linebreak
|| linebreak
[1] != 10)
252 delayed_error("uri_list_to_glist: %s",
253 _("Incorrect or missing line "
254 "break in text/uri-list data"));
258 length
= linebreak
- uri_list
;
260 if (length
&& uri_list
[0] != '#')
262 uri
= g_strndup(uri_list
, length
);
263 list
= g_list_append(list
, uri
);
266 uri_list
= linebreak
+ 2;
272 /* DRAGGING FROM US */
274 /* The user has held the mouse button down over a group of item and moved -
275 * start a drag. 'uri_list' is copied, so you can delete it straight away.
277 void drag_selection(GtkWidget
*widget
, GdkEventMotion
*event
, guchar
*uri_list
)
279 GdkDragContext
*context
;
280 GdkDragAction actions
;
281 GtkTargetList
*target_list
;
282 GtkTargetEntry target_table
[] = {
283 {"text/uri-list", 0, TARGET_URI_LIST
},
286 if (event
->state
& GDK_BUTTON1_MASK
)
287 actions
= GDK_ACTION_COPY
| GDK_ACTION_MOVE
288 | GDK_ACTION_LINK
| GDK_ACTION_ASK
;
291 if (o_dnd_middle_menu
.int_value
)
292 actions
= GDK_ACTION_ASK
;
294 actions
= GDK_ACTION_MOVE
;
297 target_list
= gtk_target_list_new(target_table
, 1);
299 context
= gtk_drag_begin(widget
,
302 (event
->state
& GDK_BUTTON1_MASK
) ? 1 :
303 (event
->state
& GDK_BUTTON2_MASK
) ? 2 : 3,
306 g_dataset_set_data_full(context
, "uri_list",
307 g_strdup(uri_list
), g_free
);
309 gtk_drag_set_icon_pixmap(context
,
310 gtk_widget_get_colormap(widget
),
316 /* Copy/Load this item into another directory/application */
317 void drag_one_item(GtkWidget
*widget
,
318 GdkEventMotion
*event
,
319 const guchar
*full_path
,
324 GdkDragContext
*context
;
325 GdkDragAction actions
;
326 GtkTargetList
*target_list
;
327 GtkTargetEntry target_table
[] = {
328 {"text/uri-list", 0, TARGET_URI_LIST
},
329 {"application/octet-stream", 0, TARGET_RAW
},
333 g_return_if_fail(full_path
!= NULL
);
334 g_return_if_fail(item
!= NULL
);
339 if (item
->base_type
== TYPE_FILE
)
341 MIME_type
*t
= item
->mime_type
;
343 target_table
[2].target
= g_strconcat(t
->media_type
, "/",
345 target_list
= gtk_target_list_new(target_table
, 3);
346 g_free(target_table
[2].target
);
349 target_list
= gtk_target_list_new(target_table
, 1);
351 if (event
->state
& GDK_BUTTON1_MASK
)
352 actions
= GDK_ACTION_COPY
| GDK_ACTION_ASK
353 | GDK_ACTION_MOVE
| GDK_ACTION_LINK
;
356 if (o_dnd_middle_menu
.int_value
)
357 actions
= GDK_ACTION_ASK
;
359 actions
= GDK_ACTION_MOVE
;
362 context
= gtk_drag_begin(widget
,
365 (event
->state
& GDK_BUTTON1_MASK
) ? 1 :
366 (event
->state
& GDK_BUTTON2_MASK
) ? 2 : 3,
369 g_dataset_set_data_full(context
, "full_path",
370 g_strdup(full_path
), g_free
);
371 uri
= g_strconcat("file://", our_host_name_for_dnd(),
372 full_path
, "\r\n", NULL
);
373 g_dataset_set_data_full(context
, "uri_list", uri
, g_free
);
375 g_return_if_fail(image
!= NULL
);
377 gtk_drag_set_icon_pixmap(context
,
378 gtk_widget_get_colormap(widget
),
379 image
->pixmap
, image
->mask
, 0, 0);
382 static void drag_end(GtkWidget
*widget
,
383 GdkDragContext
*context
,
384 FilerWindow
*filer_window
)
386 collection_set_autoscroll(filer_window
->collection
, FALSE
);
387 if (filer_window
->temp_item_selected
)
389 collection_clear_selection(filer_window
->collection
);
390 filer_window
->temp_item_selected
= FALSE
;
394 /* Called when a remote app wants us to send it some data.
395 * TODO: Maybe we should handle errors better (ie, let the remote app know
396 * the drag has failed)?
398 void drag_data_get(GtkWidget
*widget
,
399 GdkDragContext
*context
,
400 GtkSelectionData
*selection_data
,
405 char *to_send
= "E"; /* Default to sending an error */
406 long to_send_length
= 1;
407 gboolean delete_once_sent
= FALSE
;
411 type
= gdk_x11_xatom_to_atom(XA_STRING
);
416 path
= g_dataset_get_data(context
, "full_path");
417 if (path
&& load_file(path
, &to_send
, &to_send_length
))
419 delete_once_sent
= TRUE
;
420 type
= selection_data
->target
;
423 g_warning("drag_data_get: Can't find path!\n");
425 case TARGET_URI_LIST
:
426 to_send
= g_dataset_get_data(context
, "uri_list");
427 to_send_length
= strlen(to_send
);
428 type
= text_uri_list
; /* (needed for xine) */
429 delete_once_sent
= FALSE
;
432 delayed_error("drag_data_get: %s",
433 _("Internal error - bad info type"));
437 gtk_selection_data_set(selection_data
,
443 if (delete_once_sent
)
449 /* Set up this widget as a drop-target.
450 * Does not attach any motion handlers.
452 void make_drop_target(GtkWidget
*widget
, GtkDestDefaults defaults
)
454 GtkTargetEntry target_table
[] =
456 {"text/uri-list", 0, TARGET_URI_LIST
},
457 {"XdndDirectSave0", 0, TARGET_XDS
},
458 {"application/octet-stream", 0, TARGET_RAW
},
461 gtk_drag_dest_set(widget
,
464 sizeof(target_table
) / sizeof(*target_table
),
465 GDK_ACTION_COPY
| GDK_ACTION_ASK
| GDK_ACTION_MOVE
466 | GDK_ACTION_LINK
| GDK_ACTION_PRIVATE
);
468 g_signal_connect(widget
, "drag_drop", G_CALLBACK(drag_drop
), NULL
);
469 g_signal_connect(widget
, "drag_data_received",
470 G_CALLBACK(drag_data_received
), NULL
);
473 /* Set up this filer window as a drop target. Called once, when the
474 * filer window is first created.
476 void drag_set_dest(FilerWindow
*filer_window
)
478 GtkWidget
*widget
= GTK_WIDGET(filer_window
->collection
);
480 make_drop_target(widget
, 0);
482 g_signal_connect(widget
, "drag_motion",
483 G_CALLBACK(drag_motion
), filer_window
);
484 g_signal_connect(widget
, "drag_leave",
485 G_CALLBACK(drag_leave
), filer_window
);
486 g_signal_connect(widget
, "drag_end",
487 G_CALLBACK(drag_end
), filer_window
);
490 /* Like drag_set_dest, but for a pinboard-type widget.
491 * You must ensure that dnd events reach this widget (eg with
492 * setup_xdnd_proxy() for the root window).
494 void drag_set_pinboard_dest(GtkWidget
*widget
)
496 GtkTargetEntry target_table
[] = {
497 {"text/uri-list", 0, TARGET_URI_LIST
},
500 gtk_drag_dest_set(widget
,
501 GTK_DEST_DEFAULT_DROP
,
503 sizeof(target_table
) / sizeof(*target_table
),
505 g_signal_connect(widget
, "drag_data_received",
506 G_CALLBACK(desktop_drag_data_received
), NULL
);
509 /* Called during the drag when the mouse is in a widget registered
510 * as a drop target. Returns TRUE if we can accept the drop.
512 static gboolean
drag_motion(GtkWidget
*widget
,
513 GdkDragContext
*context
,
517 FilerWindow
*filer_window
)
521 GdkDragAction action
= context
->suggested_action
;
522 char *new_path
= NULL
;
524 gboolean retval
= FALSE
;
526 if (filer_window
->collection
->auto_scroll
== -1)
527 collection_set_autoscroll(filer_window
->collection
, TRUE
);
529 if (o_dnd_drag_to_icons
.int_value
)
530 item_number
= collection_get_item(filer_window
->collection
,
535 item
= item_number
>= 0
536 ? (DirItem
*) filer_window
->collection
->items
[item_number
].data
539 if (item
&& filer_window
->collection
->items
[item_number
].selected
)
542 type
= dnd_motion_item(context
, &item
);
547 /* Don't allow drops to non-writeable directories. BUT, still
548 * allow drops on non-writeable SUBdirectories so that we can
549 * do the spring-open thing.
551 if (item
&& type
== drop_dest_dir
&&
552 !(item
->flags
& ITEM_FLAG_APPDIR
))
555 /* XXX: Do we still need this under 2.0? */
556 GtkObject
*vadj
= GTK_OBJECT(filer_window
->collection
->vadj
);
558 /* Subdir: prepare for spring-open */
559 if (scrolled_adj
!= vadj
)
562 gtk_signal_disconnect(scrolled_adj
,
565 scrolled_signal
= gtk_signal_connect(
568 GTK_SIGNAL_FUNC(scrolled
),
569 filer_window
->collection
);
572 dnd_spring_load(context
, filer_window
);
579 collection_set_cursor_item(filer_window
->collection
,
584 collection_set_cursor_item(filer_window
->collection
, -1);
586 /* Disallow background drops within a single window */
587 if (type
&& gtk_drag_get_source_widget(context
) == widget
)
594 new_path
= make_path(filer_window
->path
,
595 item
->leafname
)->str
;
597 new_path
= filer_window
->path
;
600 g_dataset_set_data(context
, "drop_dest_type", type
);
603 gdk_drag_status(context
, action
, time
);
604 g_dataset_set_data_full(context
, "drop_dest_path",
605 g_strdup(new_path
), g_free
);
612 /* item is the item the file is held over, NULL for directory background.
613 * 'item' may be NULL on exit if the drop should be treated as onto the
614 * background. Disallow drags to a selected icon before calling this.
616 * Returns NULL to reject the drop, or drop_dest_prog/drop_dest_dir to
617 * accept. Build the path based on item.
619 guchar
*dnd_motion_item(GdkDragContext
*context
, DirItem
**item_p
)
621 DirItem
*item
= *item_p
;
625 /* If we didn't drop onto a directory, application or
626 * executable file then act as though the drop is to the
629 if (item
->base_type
!= TYPE_DIRECTORY
630 && !(item
->mime_type
== special_exec
))
639 /* Drop onto the window background */
641 return drop_dest_dir
;
644 /* Drop onto a program/directory of some sort */
646 if (item
->base_type
== TYPE_DIRECTORY
&&
647 !(item
->flags
& ITEM_FLAG_APPDIR
))
649 /* A normal directory */
650 if (provides(context
, text_uri_list
) ||
651 provides(context
, XdndDirectSave0
))
652 return drop_dest_dir
;
656 if (provides(context
, text_uri_list
) ||
657 provides(context
, application_octet_stream
))
658 return drop_dest_prog
;
664 /* Remove highlights */
665 static void drag_leave(GtkWidget
*widget
,
666 GdkDragContext
*context
,
668 FilerWindow
*filer_window
)
670 collection_set_autoscroll(filer_window
->collection
, FALSE
);
671 collection_set_cursor_item(filer_window
->collection
, -1);
675 g_signal_handler_disconnect(scrolled_adj
, scrolled_signal
);
680 /* User has tried to drop some data on us. Decide what format we would
683 static gboolean
drag_drop(GtkWidget
*widget
,
684 GdkDragContext
*context
,
690 const char *error
= NULL
;
691 char *leafname
= NULL
;
692 GdkAtom target
= GDK_NONE
;
694 char *dest_type
= NULL
;
696 dest_path
= g_dataset_get_data(context
, "drop_dest_path");
697 dest_type
= g_dataset_get_data(context
, "drop_dest_type");
699 g_return_val_if_fail(dest_path
!= NULL
, TRUE
);
701 if (dest_type
== drop_dest_dir
&& provides(context
, XdndDirectSave0
))
703 leafname
= get_xds_prop(context
);
706 if (strchr(leafname
, '/'))
708 error
= _("XDS protocol error: "
709 "leafname may not contain '/'\n");
718 uri
= g_string_new(NULL
);
719 g_string_sprintf(uri
, "file://%s%s",
720 our_host_name_for_dnd(),
723 set_xds_prop(context
, uri
->str
);
724 g_string_free(uri
, TRUE
);
726 target
= XdndDirectSave0
;
727 g_dataset_set_data_full(context
, "leafname",
733 "XdndDirectSave0 target provided, but the atom "
734 "XdndDirectSave0 (type text/plain) did not "
735 "contain a leafname\n");
737 else if (provides(context
, text_uri_list
))
738 target
= text_uri_list
;
739 else if (provides(context
, application_octet_stream
))
740 target
= application_octet_stream
;
743 if (dest_type
== drop_dest_dir
)
744 error
= _("Sorry - I require a target type of "
745 "text/uri-list or XdndDirectSave0.");
747 error
= _("Sorry - I require a target type of "
748 "text/uri-list or application/octet-stream.");
753 gtk_drag_finish(context
, FALSE
, FALSE
, time
); /* Failure */
755 delayed_error("%s", error
);
758 gtk_drag_get_data(widget
, context
, target
, time
);
763 /* Called when a text/uri-list arrives */
764 static void desktop_drag_data_received(GtkWidget
*widget
,
765 GdkDragContext
*context
,
768 GtkSelectionData
*selection_data
,
771 FilerWindow
*filer_window
)
776 if (!selection_data
->data
)
782 if (pinboard_drag_in_progress
)
784 pinboard_move_icons();
788 gdk_window_get_position(widget
->window
, &dx
, &dy
);
792 uris
= uri_list_to_glist(selection_data
->data
);
794 for (next
= uris
; next
; next
= next
->next
)
798 path
= get_local_path((gchar
*) next
->data
);
801 pinboard_pin(path
, NULL
, x
, y
);
812 /* Called when some data arrives from the remote app (which we asked for
815 static void drag_data_received(GtkWidget
*widget
,
816 GdkDragContext
*context
,
819 GtkSelectionData
*selection_data
,
824 if (!selection_data
->data
)
827 gtk_drag_finish(context
, FALSE
, FALSE
, time
); /* Failure */
834 got_data_xds_reply(widget
, context
,
835 selection_data
, time
);
838 got_data_raw(widget
, context
, selection_data
, time
);
840 case TARGET_URI_LIST
:
841 got_uri_list(widget
, context
, selection_data
, time
);
844 gtk_drag_finish(context
, FALSE
, FALSE
, time
);
845 delayed_error("drag_data_received: %s",
846 _("Unknown target"));
851 static void got_data_xds_reply(GtkWidget
*widget
,
852 GdkDragContext
*context
,
853 GtkSelectionData
*selection_data
,
856 gboolean mark_unsafe
= TRUE
;
857 char response
= *selection_data
->data
;
858 const char *error
= NULL
;
861 dest_path
= g_dataset_get_data(context
, "drop_dest_path");
863 if (selection_data
->length
!= 1)
868 /* Sender couldn't save there - ask for another
871 if (provides(context
, application_octet_stream
))
873 mark_unsafe
= FALSE
; /* Wait and see */
875 gtk_drag_get_data(widget
, context
,
876 application_octet_stream
, time
);
879 error
= _("Remote app can't or won't send me "
882 else if (response
== 'S')
884 /* Success - data is saved */
885 mark_unsafe
= FALSE
; /* It really is safe */
886 gtk_drag_finish(context
, TRUE
, FALSE
, time
);
888 refresh_dirs(dest_path
);
890 else if (response
!= 'E')
892 error
= _("XDS protocol error: "
893 "return code should be 'S', 'F' or 'E'\n");
895 /* else: error has been reported by the sender */
899 set_xds_prop(context
, "");
900 /* Unsave also implies that the drag failed */
901 gtk_drag_finish(context
, FALSE
, FALSE
, time
);
905 delayed_error("%s", error
);
908 static void got_data_raw(GtkWidget
*widget
,
909 GdkDragContext
*context
,
910 GtkSelectionData
*selection_data
,
913 const char *leafname
;
915 const char *error
= NULL
;
916 const char *dest_path
;
918 g_return_if_fail(selection_data
->data
!= NULL
);
920 dest_path
= g_dataset_get_data(context
, "drop_dest_path");
922 if (context
->action
== GDK_ACTION_ASK
)
924 gtk_drag_finish(context
, FALSE
, FALSE
, time
); /* Failure */
925 delayed_error(_("Sorry, can't display a menu of actions "
926 "for a remote file / raw data."));
930 if (g_dataset_get_data(context
, "drop_dest_type") == drop_dest_prog
)
932 /* The data needs to be sent to an application */
933 run_with_data(dest_path
,
934 selection_data
->data
, selection_data
->length
);
935 gtk_drag_finish(context
, TRUE
, FALSE
, time
); /* Success! */
939 leafname
= g_dataset_get_data(context
, "leafname");
941 leafname
= _("UntitledData");
943 fd
= open(make_path(dest_path
, leafname
)->str
,
944 O_WRONLY
| O_CREAT
| O_EXCL
| O_NOCTTY
,
945 S_IRUSR
| S_IRGRP
| S_IROTH
|
946 S_IWUSR
| S_IWGRP
| S_IWOTH
);
949 error
= g_strerror(errno
);
953 selection_data
->data
,
954 selection_data
->length
) == -1)
955 error
= g_strerror(errno
);
957 if (close(fd
) == -1 && !error
)
958 error
= g_strerror(errno
);
960 refresh_dirs(dest_path
);
965 if (provides(context
, XdndDirectSave0
))
966 set_xds_prop(context
, "");
967 gtk_drag_finish(context
, FALSE
, FALSE
, time
); /* Failure */
968 delayed_error(_("Error saving file: %s"), error
);
971 gtk_drag_finish(context
, TRUE
, FALSE
, time
); /* Success! */
974 /* We've got a list of URIs from somewhere (probably another filer window).
975 * If the files are on the local machine then try to copy them ourselves,
976 * otherwise, if there was only one file and application/octet-stream was
977 * provided, get the data via the X server.
979 static void got_uri_list(GtkWidget
*widget
,
980 GdkDragContext
*context
,
981 GtkSelectionData
*selection_data
,
985 const char *error
= NULL
;
987 gboolean send_reply
= TRUE
;
991 dest_path
= g_dataset_get_data(context
, "drop_dest_path");
992 type
= g_dataset_get_data(context
, "drop_dest_type");
994 g_return_if_fail(dest_path
!= NULL
);
996 uri_list
= uri_list_to_glist(selection_data
->data
);
999 error
= _("No URIs in the text/uri-list (nothing to do!)");
1000 else if (context
->action
!= GDK_ACTION_ASK
&& type
== drop_dest_prog
)
1001 run_with_files(dest_path
, uri_list
);
1002 else if ((!uri_list
->next
) && (!get_local_path(uri_list
->data
)))
1004 /* There is one URI in the list, and it's not on the local
1005 * machine. Get it via the X server if possible.
1008 if (provides(context
, application_octet_stream
))
1011 leaf
= strrchr(uri_list
->data
, '/');
1015 leaf
= uri_list
->data
;
1016 g_dataset_set_data_full(context
, "leafname",
1017 g_strdup(leaf
), g_free
);
1018 gtk_drag_get_data(widget
, context
,
1019 application_octet_stream
, time
);
1023 error
= _("Can't get data from remote machine "
1024 "(application/octet-stream not provided)");
1028 GList
*local_paths
= NULL
;
1031 /* Either one local URI, or a list. If everything in the list
1032 * isn't local then we are stuck.
1035 for (next_uri
= uri_list
; next_uri
; next_uri
= next_uri
->next
)
1039 path
= get_local_path((char *) next_uri
->data
);
1042 local_paths
= g_list_append(local_paths
,
1045 error
= _("Some of these files are on a "
1046 "different machine - they will be "
1052 error
= _("None of these files are on the local "
1053 "machine - I can't operate on multiple "
1054 "remote files - sorry.");
1056 else if (context
->action
== GDK_ACTION_ASK
)
1057 prompt_action(local_paths
, dest_path
);
1058 else if (context
->action
== GDK_ACTION_MOVE
)
1059 action_move(local_paths
, dest_path
, NULL
, -1);
1060 else if (context
->action
== GDK_ACTION_COPY
)
1061 action_copy(local_paths
, dest_path
, NULL
, -1);
1062 else if (context
->action
== GDK_ACTION_LINK
)
1063 action_link(local_paths
, dest_path
, NULL
);
1065 error
= _("Unknown action requested");
1067 for (next
= local_paths
; next
; next
= next
->next
)
1069 g_list_free(local_paths
);
1074 gtk_drag_finish(context
, FALSE
, FALSE
, time
); /* Failure */
1075 delayed_error(_("Error getting file list: %s"), error
);
1077 else if (send_reply
)
1078 gtk_drag_finish(context
, TRUE
, FALSE
, time
); /* Success! */
1080 next_uri
= uri_list
;
1083 g_free(next_uri
->data
);
1084 next_uri
= next_uri
->next
;
1086 g_list_free(uri_list
);
1089 /* Called when an item from the ACTION_ASK menu is chosen */
1090 static void menuitem_response(gpointer data
, guint action
, GtkWidget
*widget
)
1092 if (action
== MENU_MOVE
)
1093 action_move(prompt_local_paths
, prompt_dest_path
, NULL
, -1);
1094 else if (action
== MENU_COPY
)
1095 action_copy(prompt_local_paths
, prompt_dest_path
, NULL
, -1);
1096 else if (action
== MENU_LINK
)
1097 action_link(prompt_local_paths
, prompt_dest_path
, NULL
);
1098 else if (action
== MENU_SET_ICON
)
1100 if (g_list_length(prompt_local_paths
) == 1)
1101 set_icon_path(prompt_dest_path
,
1102 (char*) prompt_local_paths
->data
);
1105 _("You can't use multiple files with Set Icon!"));
1109 /* When some local files are dropped somewhere with ACTION_ASK, this
1110 * function is called to display the menu.
1112 static void prompt_action(GList
*paths
, gchar
*dest
)
1116 if (prompt_local_paths
)
1118 g_list_foreach(prompt_local_paths
, (GFunc
) g_free
, NULL
);
1119 g_list_free(prompt_local_paths
);
1120 g_free(prompt_dest_path
);
1122 prompt_dest_path
= NULL
;
1123 prompt_local_paths
= NULL
;
1126 /* Make a copy of the arguments */
1127 for (next
= paths
; next
; next
= next
->next
)
1128 prompt_local_paths
= g_list_append(prompt_local_paths
,
1129 g_strdup((gchar
*) next
->data
));
1130 prompt_dest_path
= g_strdup(dest
);
1132 /* Shade 'Set Icon' if there are multiple files */
1133 menu_set_items_shaded(dnd_menu
, g_list_length(paths
) != 1, 4, 1);
1135 show_popup_menu(dnd_menu
, gtk_get_current_event(), 1);
1139 /* SPRING-LOADING */
1141 /* This is the code that makes directories pop open if you hold a
1144 * First, call dnd_spring_load(context) to arm the system.
1145 * After a timeout (1/2 a second) the dest_path directory will be
1146 * opened in a new window, unless dnd_spring_abort is called first.
1149 static gint spring_timeout
= -1;
1150 static GdkDragContext
*spring_context
= NULL
;
1151 static FilerWindow
*spring_window
= NULL
;
1152 static FilerWindow
*spring_src_window
= NULL
;
1154 void dnd_spring_load(GdkDragContext
*context
, FilerWindow
*src_win
)
1156 g_return_if_fail(context
!= NULL
);
1158 if (!o_dnd_spring_open
.int_value
)
1164 spring_context
= context
;
1165 g_object_ref(spring_context
);
1166 spring_src_window
= src_win
;
1167 spring_timeout
= gtk_timeout_add(
1168 o_dnd_spring_delay
.int_value
, spring_now
, NULL
);
1171 void dnd_spring_abort(void)
1173 if (!spring_context
)
1176 g_object_unref(spring_context
);
1177 spring_context
= NULL
;
1178 gtk_timeout_remove(spring_timeout
);
1181 /* If all mod keys are released, no buttons are pressed, and the
1182 * mouse is outside the spring window, then close it.
1184 static gboolean
spring_check_idle(gpointer data
)
1191 if (!get_pointer_xy(&p_x
, &p_y
))
1194 GdkWindow *win = spring_window->window->window;
1198 gdk_window_get_position(win, &x, &y);
1199 gdk_window_get_size(win, &w, &h);
1201 if (p_x < x || p_x > x + w || p_y < y || p_y > y + h)
1205 gtk_widget_destroy(spring_window
->window
);
1206 return FALSE
; /* Got it! */
1209 return TRUE
; /* Try again later */
1212 static gboolean
spring_now(gpointer data
)
1214 gboolean old_unique
= o_unique_filer_windows
.int_value
;
1218 g_return_val_if_fail(spring_context
!= NULL
, FALSE
);
1220 dest_path
= g_dataset_get_data(spring_context
, "drop_dest_path");
1221 g_return_val_if_fail(dest_path
!= NULL
, FALSE
);
1224 * Note: Due to a bug in gtk, if a window disappears during
1225 * a drag and the pointer moves over where the window was,
1226 * the sender crashes! Therefore, do not close any windows
1227 * while dragging! (fixed in later versions)
1231 gtk_widget_destroy(spring_window->window);
1234 get_pointer_xy(&x
, &y
);
1236 o_unique_filer_windows
.int_value
= FALSE
; /* XXX: yuck! */
1239 collection_set_cursor_item(spring_window
->collection
, -1);
1240 filer_change_to(spring_window
, dest_path
, NULL
);
1241 /* DON'T move the window. Gtk+ sometimes doesn't
1247 spring_window
= filer_opendir(dest_path
, spring_src_window
);
1250 gtk_timeout_add(500, spring_check_idle
, NULL
);
1251 g_signal_connect(spring_window
->window
, "destroy",
1252 G_CALLBACK(spring_win_destroyed
), NULL
);
1253 centre_window(spring_window
->window
->window
, x
, y
);
1256 o_unique_filer_windows
.int_value
= old_unique
;
1263 static void spring_win_destroyed(GtkWidget
*widget
, gpointer data
)
1265 spring_window
= NULL
;
1268 /* HANDLING MOTION EVENTS */
1270 /* If not-NULL, then this widget has a grab */
1271 static GtkWidget
*motion_widget
= NULL
;
1273 /* If TRUE, we must gdk_pointer_ungrab() too when finishing */
1274 static gboolean motion_pointer_grab
= FALSE
;
1276 /* Call this on a button press event. It stores the mouse position
1277 * as the start of the new drag and returns TRUE if all is well.
1278 * Further motions events are disabled at this point - you must
1279 * then call dnd_motion_start() to set the type of motion expected.
1280 * Grabs the widget on the first press.
1282 * If the system is not ready to handle a motion event (because a
1283 * button is already held down?) it does nothing and returns FALSE.
1285 * If the event is not a single click then it simply returns TRUE.
1287 gboolean
dnd_motion_press(GtkWidget
*widget
, GdkEventButton
*event
)
1289 if (event
->type
!= GDK_BUTTON_PRESS
)
1290 return TRUE
; /* Not a click event! */
1292 motion_buttons_pressed
++;
1293 if (motion_buttons_pressed
== 1)
1295 /* g_print("[ grab! ]\n"); */
1296 gtk_grab_add(widget
);
1297 motion_widget
= widget
;
1300 if (motion_state
!= MOTION_NONE
)
1301 return FALSE
; /* Ignore clicks - we're busy! */
1303 motion_state
= MOTION_DISABLED
;
1304 drag_start_x
= event
->x_root
;
1305 drag_start_y
= event
->y_root
;
1310 /* After the button press event, decide what kind of motion is expected.
1311 * If you don't call this then the motion system is disabled - call
1312 * dnd_motion_release() to reset it.
1314 * Note: If you open a popup menu or start DND call dnd_motion_ungrab()
1317 void dnd_motion_start(MotionType motion
)
1319 g_return_if_fail(motion_state
== MOTION_DISABLED
);
1321 motion_state
= motion
;
1324 /* Call this on a button release event. If some buttons are still pressed,
1325 * returns TRUE and does nothing.
1327 * Otherwise, it resets the motion system to be ready again and returns TRUE.
1329 * If the motion system wasn't being used (MOTION_NONE) then it does nothing
1330 * and returns FALSE - process the release event yourself as it isn't part
1331 * of a motion. This also happens if a motion was primed but never happened.
1333 gboolean
dnd_motion_release(GdkEventButton
*event
)
1335 MotionType motion
= motion_state
;
1338 if (motion_buttons_pressed
== 0)
1339 return TRUE
; /* We were disabled */
1341 if (motion_buttons_pressed
== 1)
1342 dnd_motion_ungrab();
1345 motion_buttons_pressed
--;
1349 if (motion
== MOTION_REPOSITION
|| motion
== MOTION_DISABLED
)
1350 return TRUE
; /* Already done something - eat the event */
1352 /* Eat release events that happen too far from the click
1353 * source. Otherwise, allow the caller to treat this as a click
1354 * that never became a motion.
1356 dx
= event
->x_root
- drag_start_x
;
1357 dy
= event
->y_root
- drag_start_y
;
1359 return ABS(dx
) > 5 || ABS(dy
) > 5;
1362 /* Use this to disable the motion system. The system will be reset once
1363 * all mouse buttons are released.
1365 void dnd_motion_disable(void)
1367 g_return_if_fail(motion_state
!= MOTION_NONE
&&
1368 motion_state
!= MOTION_DISABLED
);
1370 motion_state
= MOTION_DISABLED
;
1373 /* Use this if something else is going to grab the pointer so that
1374 * we won't get any more motion or release events.
1376 void dnd_motion_ungrab(void)
1378 if (motion_buttons_pressed
> 0)
1380 if (motion_pointer_grab
)
1382 gdk_pointer_ungrab(GDK_CURRENT_TIME
);
1383 motion_pointer_grab
= FALSE
;
1384 /* g_print("[ ungrab_pointer ]\n"); */
1386 gtk_grab_remove(motion_widget
);
1387 motion_widget
= NULL
;
1388 motion_buttons_pressed
= 0;
1389 /* g_print("[ ungrab ]\n"); */
1392 motion_state
= MOTION_NONE
;
1395 /* Call this on motion events. If the mouse position is far enough
1396 * from the click position, returns TRUE and does dnd_motion_ungrab().
1397 * You should then start regular drag-and-drop.
1399 * Otherwise, returns FALSE.
1401 gboolean
dnd_motion_moved(GdkEventMotion
*event
)
1405 dx
= event
->x_root
- drag_start_x
;
1406 dy
= event
->y_root
- drag_start_y
;
1408 if (ABS(dx
) <= 5 && ABS(dy
) <= 5)
1409 return FALSE
; /* Not far enough */
1411 dnd_motion_ungrab();
1416 /* Normally, the X server will automatically grab the pointer on a
1417 * button press and ungrab on release. However, if the grab widget
1418 * is reparented then call this to re-aquire the grab.
1420 void dnd_motion_grab_pointer(void)
1422 g_return_if_fail(motion_widget
!= NULL
);
1424 gdk_pointer_grab(motion_widget
->window
, FALSE
,
1425 GDK_POINTER_MOTION_MASK
|
1426 GDK_BUTTON_RELEASE_MASK
,
1427 FALSE
, NULL
, GDK_CURRENT_TIME
);
1429 motion_pointer_grab
= TRUE
;