r1476: Options to forward button-3 clicks on the pinboard to the window manager
[rox-filer.git] / ROX-Filer / src / gtksavebox.c
blob1bf48dbb716aff90f9f702b2ae577468482e4f6b
1 /*
2 * $Id$
4 * SaveBox widget for the ROX desktop project
5 * Copyright (C) 2002 Thomas Leonard.
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)
10 * any later version.
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
15 * more details.
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 /* gtksavebox.c - ROX-style savebox widget */
25 * Note: This file is formatted like the Gtk+ sources, as it is/was hoped
26 * to include it in Gtk+ at some point.
29 #include "config.h"
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
35 #include "gdk/gdkkeysyms.h"
37 #include "gtksavebox.h"
38 #include "gtk/gtkwidget.h"
39 #include "gtk/gtkalignment.h"
40 #include "gtk/gtkdnd.h"
41 #include "gtk/gtkbutton.h"
42 #include "gtk/gtksignal.h"
43 #include "gtk/gtkhbox.h"
44 #include "gtk/gtkeventbox.h"
45 #include "gtk/gtkentry.h"
46 #include "gtk/gtkmessagedialog.h"
47 #include "gtk/gtkhseparator.h"
48 #include "gtk/gtkvbox.h"
49 #include "gtk/gtkdialog.h"
50 #include "gtk/gtklabel.h"
51 #include "gtk/gtkstock.h"
53 #include "global.h"
54 #include "support.h"
55 #include "gui_support.h"
57 /*
58 * Behaviour:
60 * - Clicking Save or pressing Return:
61 * - Emits 'save_to_file',
62 * - Emits 'saved_to_uri' (with the same pathname),
63 * - Destroys the widget.
65 * - Clicking Cancel or pressing Escape:
66 * - Destroys the widget.
68 * - Dragging the data somewhere:
69 * - Will either emit 'save_to_file' or get the selection,
70 * - Emits 'saved_to_uri' (possibly with a NULL URI),
71 * - Destroys the widget.
73 * - Clicking Discard:
74 * - Emits 'saved_to_uri' with a NULL URI,
75 * - Destroys the widget.
77 * To clarify: 'saved_to_uri' indicates that the save was successful. A
78 * NULL URI just means that the data was saved to another application rather
79 * than a fixed address. Data should only be marked unmodified when
80 * saved_to_uri is called with a non-NULL URI.
82 * Discard is a bit like a successful save to a null device. The data should
83 * be discarded when saved_to_uri is called, whatever URI is set to.
86 * Signals:
88 * gint save_to_file (GtkSavebox *savebox, const gchar *pathname)
89 * Save the data to disk using this pathname. Return GTK_XDS_SAVED
90 * on success, or GTK_XDS_SAVE_ERROR on failure (and report the error
91 * to the user somehow). DO NOT mark the data unmodified or change
92 * the pathname for the file - this might be a scrap file transfer.
94 * void saved_to_uri (GtkSavebox *savebox, const gchar *uri)
95 * The data is saved. If 'uri' is non-NULL, mark the file as unmodified
96 * and update the pathname/uri for the file to the one given.
99 enum {
100 PROP_0,
101 PROP_HAS_DISCARD
104 enum
106 SAVE_TO_FILE,
107 SAVED_TO_URI,
109 LAST_SIGNAL
112 static gpointer parent_class;
113 static guint savebox_signals[LAST_SIGNAL];
115 /* Longest possible XdndDirectSave0 property value */
116 #define XDS_MAXURILEN 4096
118 static GdkAtom XdndDirectSave;
119 static GdkAtom text_plain;
120 static GdkAtom xa_string;
122 static void gtk_savebox_class_init (GtkSaveboxClass *klass);
123 static void gtk_savebox_init (GtkSavebox *savebox);
124 static void button_press_over_icon (GtkWidget *drag_box,
125 GdkEventButton *event,
126 GtkSavebox *savebox);
127 static void drag_data_get (GtkWidget *widget,
128 GdkDragContext *context,
129 GtkSelectionData *selection_data,
130 guint info,
131 guint32 time);
132 static guchar *read_xds_property (GdkDragContext *context,
133 gboolean delete);
134 static void write_xds_property (GdkDragContext *context,
135 const guchar *value);
136 static void drag_end (GtkWidget *widget,
137 GdkDragContext *context);
138 static void gtk_savebox_response (GtkDialog *savebox,
139 gint response);
140 static void discard_clicked (GtkWidget *button,
141 GtkWidget *savebox);
142 static void do_save (GtkSavebox *savebox);
143 static void gtk_savebox_set_property (GObject *object,
144 guint prop_id,
145 const GValue *value,
146 GParamSpec *pspec);
147 static void gtk_savebox_get_property (GObject *object,
148 guint prop_id,
149 GValue *value,
150 GParamSpec *pspec);
152 void
153 marshal_INT__STRING (GClosure *closure,
154 GValue *return_value,
155 guint n_param_values,
156 const GValue *param_values,
157 gpointer invocation_hint,
158 gpointer marshal_data);
160 GType
161 gtk_savebox_get_type (void)
163 static GType my_type = 0;
165 if (!my_type)
167 static const GTypeInfo info =
169 sizeof (GtkSaveboxClass),
170 NULL, /* base_init */
171 NULL, /* base_finalise */
172 (GClassInitFunc) gtk_savebox_class_init,
173 NULL, /* class_finalise */
174 NULL, /* class_data */
175 sizeof(GtkSavebox),
176 0, /* n_preallocs */
177 (GInstanceInitFunc) gtk_savebox_init
180 my_type = g_type_register_static(GTK_TYPE_DIALOG, "GtkSavebox", &info, 0);
183 return my_type;
186 static void
187 gtk_savebox_class_init (GtkSaveboxClass *class)
189 GObjectClass *object_class;
190 GtkDialogClass *dialog = (GtkDialogClass *) class;
192 XdndDirectSave = gdk_atom_intern ("XdndDirectSave0", FALSE);
193 text_plain = gdk_atom_intern ("text/plain", FALSE);
194 xa_string = gdk_atom_intern ("STRING", FALSE);
196 parent_class = g_type_class_peek_parent (class);
198 class->saved_to_uri = NULL;
199 class->save_to_file = NULL;
200 dialog->response = gtk_savebox_response;
202 object_class = G_OBJECT_CLASS(class);
204 savebox_signals[SAVE_TO_FILE] = g_signal_new(
205 "save_to_file",
206 G_TYPE_FROM_CLASS(object_class),
207 G_SIGNAL_RUN_LAST,
208 G_STRUCT_OFFSET(GtkSaveboxClass,
209 save_to_file),
210 NULL, NULL,
211 marshal_INT__STRING,
212 G_TYPE_INT, 1,
213 G_TYPE_STRING);
215 savebox_signals[SAVED_TO_URI] = g_signal_new(
216 "saved_to_uri",
217 G_TYPE_FROM_CLASS(object_class),
218 G_SIGNAL_RUN_LAST,
219 G_STRUCT_OFFSET(GtkSaveboxClass,
220 saved_to_uri),
221 NULL, NULL,
222 g_cclosure_marshal_VOID__STRING,
223 G_TYPE_NONE, 1,
224 G_TYPE_STRING);
226 object_class->set_property = gtk_savebox_set_property;
227 object_class->get_property = gtk_savebox_get_property;
229 g_object_class_install_property(object_class, PROP_HAS_DISCARD,
230 g_param_spec_boolean("has_discard",
231 _("Has Discard"),
232 _("The dialog has a Discard button"),
233 TRUE,
234 G_PARAM_READWRITE));
237 static void
238 gtk_savebox_init (GtkSavebox *savebox)
240 GtkWidget *alignment, *button;
241 GtkDialog *dialog = (GtkDialog *) savebox;
242 GtkTargetEntry targets[] = { {"XdndDirectSave0", 0, GTK_TARGET_XDS} };
244 gtk_dialog_set_has_separator (dialog, FALSE);
246 savebox->targets = gtk_target_list_new (targets,
247 sizeof (targets) / sizeof (*targets));
248 savebox->icon = NULL;
250 gtk_window_set_title (GTK_WINDOW (savebox), _("Save As:"));
251 gtk_window_set_position (GTK_WINDOW (savebox), GTK_WIN_POS_MOUSE);
252 gtk_window_set_wmclass (GTK_WINDOW (savebox), "savebox", "Savebox");
254 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
255 gtk_box_pack_start (GTK_BOX (dialog->vbox), alignment, TRUE, TRUE, 0);
257 savebox->drag_box = gtk_event_box_new ();
258 gtk_container_set_border_width (GTK_CONTAINER (savebox->drag_box), 4);
259 gtk_widget_add_events (savebox->drag_box, GDK_BUTTON_PRESS_MASK);
260 g_signal_connect (savebox->drag_box, "button_press_event",
261 G_CALLBACK (button_press_over_icon), savebox);
262 g_signal_connect (savebox, "drag_end",
263 G_CALLBACK (drag_end), savebox);
264 g_signal_connect (savebox, "drag_data_get",
265 G_CALLBACK (drag_data_get), savebox);
266 gtk_container_add (GTK_CONTAINER (alignment), savebox->drag_box);
268 savebox->entry = gtk_entry_new ();
269 g_signal_connect_swapped (savebox->entry, "activate",
270 G_CALLBACK (do_save), savebox);
271 gtk_box_pack_start (GTK_BOX (dialog->vbox), savebox->entry, FALSE, TRUE, 4);
273 gtk_widget_show_all (dialog->vbox);
274 gtk_widget_grab_focus (savebox->entry);
276 savebox->discard_area = gtk_hbutton_box_new();
278 button = button_new_mixed (GTK_STOCK_DELETE, "_Discard");
279 gtk_box_pack_start (GTK_BOX (savebox->discard_area), button, FALSE, TRUE, 2);
280 g_signal_connect (button, "clicked", G_CALLBACK (discard_clicked), savebox);
281 GTK_WIDGET_UNSET_FLAGS (button, GTK_CAN_FOCUS);
282 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
284 gtk_box_pack_end (GTK_BOX (dialog->vbox), savebox->discard_area,
285 FALSE, TRUE, 0);
286 gtk_box_reorder_child (GTK_BOX (dialog->vbox), savebox->discard_area, 0);
289 GtkWidget*
290 gtk_savebox_new (const gchar *action)
292 GtkWidget *button;
293 GtkDialog *dialog;
294 GList *list, *next;
296 dialog = GTK_DIALOG (gtk_widget_new (gtk_savebox_get_type(), NULL));
298 gtk_dialog_add_button (dialog, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
300 button = button_new_mixed (GTK_STOCK_SAVE, action);
301 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
302 gtk_widget_show (button);
303 gtk_dialog_add_action_widget (dialog, button, GTK_RESPONSE_OK);
305 gtk_dialog_set_default_response (dialog, GTK_RESPONSE_OK);
307 list = gtk_container_get_children (GTK_CONTAINER (dialog->action_area));
308 for (next = list; next; next = next->next)
309 GTK_WIDGET_UNSET_FLAGS (GTK_WIDGET(next->data), GTK_CAN_FOCUS);
310 g_list_free(list);
312 return GTK_WIDGET(dialog);
315 void
316 gtk_savebox_set_icon (GtkSavebox *savebox, GdkPixbuf *pixbuf)
318 g_return_if_fail (savebox != NULL);
319 g_return_if_fail (GTK_IS_SAVEBOX (savebox));
320 g_return_if_fail (pixbuf != NULL);
322 if (savebox->icon)
323 gtk_image_set_from_pixbuf (GTK_IMAGE (savebox->icon), pixbuf);
324 else
326 savebox->icon = gtk_image_new_from_pixbuf (pixbuf);
327 gtk_container_add (GTK_CONTAINER (savebox->drag_box), savebox->icon);
328 gtk_widget_show(savebox->icon);
332 void
333 gtk_savebox_set_pathname (GtkSavebox *savebox, const gchar *pathname)
335 const gchar *slash, *dot;
336 gint leaf;
338 g_return_if_fail (savebox != NULL);
339 g_return_if_fail (GTK_IS_SAVEBOX (savebox));
340 g_return_if_fail (pathname != NULL);
342 gtk_entry_set_text (GTK_ENTRY (savebox->entry), pathname);
344 slash = strrchr (pathname, '/');
346 leaf = slash ? g_utf8_pointer_to_offset(pathname, slash) + 1 : 0;
347 dot = strchr(pathname + leaf, '.');
349 gtk_editable_select_region (GTK_EDITABLE (savebox->entry), leaf,
350 dot ? g_utf8_pointer_to_offset (pathname, dot)
351 : -1);
354 void
355 gtk_savebox_set_has_discard (GtkSavebox *savebox, gboolean setting)
357 if (setting)
358 gtk_widget_show_all (savebox->discard_area);
359 else
360 gtk_widget_hide (savebox->discard_area);
363 static void
364 button_press_over_icon (GtkWidget *drag_box, GdkEventButton *event,
365 GtkSavebox *savebox)
367 GdkDragContext *context;
368 const gchar *uri = NULL, *leafname;
370 g_return_if_fail (savebox != NULL);
371 g_return_if_fail (GTK_IS_SAVEBOX (savebox));
372 g_return_if_fail (event != NULL);
373 g_return_if_fail (savebox->icon != NULL);
375 savebox->using_xds = FALSE;
376 savebox->data_sent = FALSE;
377 context = gtk_drag_begin (GTK_WIDGET (savebox),
378 savebox->targets, GDK_ACTION_COPY,
379 event->button, (GdkEvent *) event);
381 uri = gtk_entry_get_text (GTK_ENTRY (savebox->entry));
382 if (uri && *uri)
383 leafname = g_basename (uri);
384 else
385 leafname = _("Unnamed");
387 write_xds_property (context, leafname);
389 gtk_drag_set_icon_pixbuf (context,
390 gtk_image_get_pixbuf (GTK_IMAGE (savebox->icon)),
391 event->x, event->y);
395 static void
396 drag_data_get (GtkWidget *widget,
397 GdkDragContext *context,
398 GtkSelectionData *selection_data,
399 guint info,
400 guint32 time)
402 GtkSavebox *savebox;
403 guchar to_send = 'E';
404 gchar *uri;
405 const gchar *pathname;
407 g_return_if_fail (widget != NULL);
408 g_return_if_fail (GTK_IS_SAVEBOX (widget));
409 g_return_if_fail (context != NULL);
410 g_return_if_fail (selection_data != NULL);
412 savebox = GTK_SAVEBOX (widget);
414 /* We're only concerned with the XDS protocol. Responding to other requests
415 * (including application/octet-stream) is the job of the application.
417 if (info != GTK_TARGET_XDS)
419 /* Assume that the data will be/has been sent */
420 savebox->data_sent = TRUE;
421 return;
424 uri = read_xds_property (context, FALSE);
426 if (uri)
428 gint result = GTK_XDS_NO_HANDLER;
430 pathname = get_local_path (uri);
431 if (!pathname)
432 to_send = 'F'; /* Not on the local machine */
433 else
435 g_signal_emit (widget, savebox_signals[SAVE_TO_FILE], 0,
436 pathname, &result);
438 if (result == GTK_XDS_SAVED)
440 savebox->data_sent = TRUE;
441 to_send = 'S';
443 else if (result != GTK_XDS_SAVE_ERROR)
444 g_warning ("No handler for saving to a file.\n");
446 g_free (uri);
449 else
451 g_warning (_("Remote application wants to use Direct Save, but I can't "
452 "read the XdndDirectSave0 (type text/plain) property.\n"));
455 if (to_send != 'E')
456 savebox->using_xds = TRUE;
457 gtk_selection_data_set (selection_data, xa_string, 8, &to_send, 1);
460 static guchar *
461 read_xds_property (GdkDragContext *context, gboolean delete)
463 guchar *prop_text;
464 guint length;
465 guchar *retval = NULL;
467 g_return_val_if_fail (context != NULL, NULL);
469 if (gdk_property_get (context->source_window, XdndDirectSave, text_plain,
470 0, XDS_MAXURILEN, delete,
471 NULL, NULL, &length, &prop_text)
472 && prop_text)
474 /* Terminate the string */
475 retval = g_realloc (prop_text, length + 1);
476 retval[length] = '\0';
479 return retval;
482 static void
483 write_xds_property (GdkDragContext *context, const guchar *value)
485 if (value)
487 gdk_property_change (context->source_window, XdndDirectSave,
488 text_plain, 8, GDK_PROP_MODE_REPLACE,
489 value, strlen (value));
491 else
492 gdk_property_delete (context->source_window, XdndDirectSave);
495 static void drag_end (GtkWidget *widget, GdkDragContext *context)
497 g_return_if_fail (widget != NULL);
498 g_return_if_fail (GTK_IS_SAVEBOX (widget));
499 g_return_if_fail (context != NULL);
501 if (GTK_SAVEBOX (widget)->using_xds)
503 guchar *uri;
504 uri = read_xds_property (context, TRUE);
506 if (uri)
508 const gchar *path;
510 path = get_local_path (uri);
512 g_signal_emit (widget, savebox_signals[SAVED_TO_URI], 0,
513 path ? path : (const gchar *) uri);
514 g_free(uri);
516 gtk_widget_destroy (widget);
518 return;
521 else
522 write_xds_property (context, NULL);
524 if (GTK_SAVEBOX (widget)->data_sent)
526 g_signal_emit (widget, savebox_signals[SAVED_TO_URI], 0, NULL);
527 gtk_widget_destroy (widget);
531 static void discard_clicked (GtkWidget *button, GtkWidget *savebox)
533 g_signal_emit (savebox, savebox_signals[SAVED_TO_URI], 0, NULL);
534 gtk_widget_destroy (savebox);
537 /* User has clicked Save or pressed Return... */
538 static void do_save (GtkSavebox *savebox)
540 gint result = GTK_XDS_NO_HANDLER;
541 const gchar *pathname, *uri;
543 g_return_if_fail (savebox != NULL);
544 g_return_if_fail (GTK_IS_SAVEBOX (savebox));
546 uri = gtk_entry_get_text (GTK_ENTRY (savebox->entry));
547 pathname = get_local_path (uri);
549 if (!pathname)
551 GtkWidget *dialog;
553 dialog = gtk_message_dialog_new (GTK_WINDOW (savebox),
554 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
555 GTK_MESSAGE_INFO, GTK_BUTTONS_OK,
556 _("Drag the icon to a directory viewer\n"
557 "(or enter a full pathname)"));
559 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
561 gtk_dialog_run (GTK_DIALOG (dialog));
562 gtk_widget_destroy (dialog);
564 return;
567 g_signal_emit (savebox, savebox_signals[SAVE_TO_FILE], 0, pathname, &result);
569 if (result == GTK_XDS_SAVED)
571 g_signal_emit (savebox, savebox_signals[SAVED_TO_URI], 0, pathname);
573 gtk_widget_destroy (GTK_WIDGET (savebox));
575 else if (result == GTK_XDS_NO_HANDLER)
576 g_warning ("No handler for saving to a file.\n");
579 static void
580 gtk_savebox_response (GtkDialog *savebox, gint response)
582 if (response == GTK_RESPONSE_OK)
584 do_save(GTK_SAVEBOX(savebox));
585 return;
587 else if (response == GTK_RESPONSE_CANCEL)
588 gtk_widget_destroy (GTK_WIDGET (savebox));
591 static void
592 gtk_savebox_set_property (GObject *object,
593 guint prop_id,
594 const GValue *value,
595 GParamSpec *pspec)
597 GtkSavebox *savebox;
599 savebox = GTK_SAVEBOX (object);
601 switch (prop_id)
603 case PROP_HAS_DISCARD:
604 gtk_savebox_set_has_discard (GTK_SAVEBOX(object),
605 g_value_get_boolean (value));
606 break;
608 default:
609 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
610 break;
614 static void
615 gtk_savebox_get_property (GObject *object,
616 guint prop_id,
617 GValue *value,
618 GParamSpec *pspec)
620 GtkSavebox *savebox;
622 savebox = GTK_SAVEBOX (object);
624 switch (prop_id)
626 case PROP_HAS_DISCARD:
627 g_value_set_boolean (value, GTK_WIDGET_VISIBLE(savebox->discard_area));
628 break;
630 default:
631 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
632 break;
636 void
637 marshal_INT__STRING (GClosure *closure,
638 GValue *return_value,
639 guint n_param_values,
640 const GValue *param_values,
641 gpointer invocation_hint,
642 gpointer marshal_data)
644 typedef gint (*GMarshalFunc_INT__STRING) (gpointer data1,
645 gpointer arg_1,
646 gpointer data2);
647 register GMarshalFunc_INT__STRING callback;
648 register GCClosure *cc = (GCClosure*) closure;
649 register gpointer data1, data2;
650 gint v_return;
652 g_return_if_fail (return_value != NULL);
653 g_return_if_fail (n_param_values == 2);
655 if (G_CCLOSURE_SWAP_DATA (closure))
657 data1 = closure->data;
658 data2 = g_value_peek_pointer (param_values + 0);
660 else
662 data1 = g_value_peek_pointer (param_values + 0);
663 data2 = closure->data;
665 callback = (GMarshalFunc_INT__STRING)
666 (marshal_data ? marshal_data : cc->callback);
668 v_return = callback (data1, param_values[1].data[0].v_pointer, data2);
670 g_value_set_int (return_value, v_return);