4 * SaveBox widget for the ROX desktop project
5 * Copyright (C) 2003 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)
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 /* 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.
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"
55 #include "gui_support.h"
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.
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.
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.
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
,
132 static guchar
*read_xds_property (GdkDragContext
*context
,
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
,
140 static void discard_clicked (GtkWidget
*button
,
142 static void do_save (GtkSavebox
*savebox
);
143 static void gtk_savebox_set_property (GObject
*object
,
147 static void gtk_savebox_get_property (GObject
*object
,
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
);
161 gtk_savebox_get_type (void)
163 static GType my_type
= 0;
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 */
177 (GInstanceInitFunc
) gtk_savebox_init
180 my_type
= g_type_register_static(GTK_TYPE_DIALOG
, "GtkSavebox", &info
, 0);
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(
206 G_TYPE_FROM_CLASS(object_class
),
208 G_STRUCT_OFFSET(GtkSaveboxClass
,
215 savebox_signals
[SAVED_TO_URI
] = g_signal_new(
217 G_TYPE_FROM_CLASS(object_class
),
219 G_STRUCT_OFFSET(GtkSaveboxClass
,
222 g_cclosure_marshal_VOID__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",
232 "The dialog has a Discard button",
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
,
286 gtk_box_reorder_child (GTK_BOX (dialog
->vbox
), savebox
->discard_area
, 0);
288 savebox
->dnd_action
= GDK_ACTION_COPY
;
292 gtk_savebox_set_action (GtkSavebox
*savebox
, GdkDragAction action
)
294 g_return_if_fail (savebox
!= NULL
);
295 g_return_if_fail (GTK_IS_SAVEBOX (savebox
));
297 savebox
->dnd_action
= action
;
301 gtk_savebox_new (const gchar
*action
)
307 dialog
= GTK_DIALOG (gtk_widget_new (gtk_savebox_get_type(), NULL
));
309 gtk_dialog_add_button (dialog
, GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
);
311 button
= button_new_mixed (GTK_STOCK_SAVE
, action
);
312 GTK_WIDGET_SET_FLAGS (button
, GTK_CAN_DEFAULT
);
313 gtk_widget_show (button
);
314 gtk_dialog_add_action_widget (dialog
, button
, GTK_RESPONSE_OK
);
316 gtk_dialog_set_default_response (dialog
, GTK_RESPONSE_OK
);
318 list
= gtk_container_get_children (GTK_CONTAINER (dialog
->action_area
));
319 for (next
= list
; next
; next
= next
->next
)
320 GTK_WIDGET_UNSET_FLAGS (GTK_WIDGET(next
->data
), GTK_CAN_FOCUS
);
323 return GTK_WIDGET(dialog
);
327 gtk_savebox_set_icon (GtkSavebox
*savebox
, GdkPixbuf
*pixbuf
)
329 g_return_if_fail (savebox
!= NULL
);
330 g_return_if_fail (GTK_IS_SAVEBOX (savebox
));
331 g_return_if_fail (pixbuf
!= NULL
);
334 gtk_image_set_from_pixbuf (GTK_IMAGE (savebox
->icon
), pixbuf
);
337 savebox
->icon
= gtk_image_new_from_pixbuf (pixbuf
);
338 gtk_container_add (GTK_CONTAINER (savebox
->drag_box
), savebox
->icon
);
339 gtk_widget_show(savebox
->icon
);
344 gtk_savebox_set_pathname (GtkSavebox
*savebox
, const gchar
*pathname
)
346 const gchar
*slash
, *dot
;
349 g_return_if_fail (savebox
!= NULL
);
350 g_return_if_fail (GTK_IS_SAVEBOX (savebox
));
351 g_return_if_fail (pathname
!= NULL
);
353 gtk_entry_set_text (GTK_ENTRY (savebox
->entry
), pathname
);
355 slash
= strrchr (pathname
, '/');
357 leaf
= slash
? g_utf8_pointer_to_offset(pathname
, slash
) + 1 : 0;
358 dot
= strchr(pathname
+ leaf
, '.');
360 gtk_editable_select_region (GTK_EDITABLE (savebox
->entry
), leaf
,
361 dot
? g_utf8_pointer_to_offset (pathname
, dot
)
366 gtk_savebox_set_has_discard (GtkSavebox
*savebox
, gboolean setting
)
369 gtk_widget_show_all (savebox
->discard_area
);
371 gtk_widget_hide (savebox
->discard_area
);
375 button_press_over_icon (GtkWidget
*drag_box
, GdkEventButton
*event
,
378 GdkDragContext
*context
;
379 const gchar
*uri
= NULL
, *leafname
;
381 g_return_if_fail (savebox
!= NULL
);
382 g_return_if_fail (GTK_IS_SAVEBOX (savebox
));
383 g_return_if_fail (event
!= NULL
);
384 g_return_if_fail (savebox
->icon
!= NULL
);
386 savebox
->using_xds
= FALSE
;
387 savebox
->data_sent
= FALSE
;
388 context
= gtk_drag_begin (GTK_WIDGET (savebox
),
389 savebox
->targets
, savebox
->dnd_action
,
390 event
->button
, (GdkEvent
*) event
);
392 uri
= gtk_entry_get_text (GTK_ENTRY (savebox
->entry
));
394 leafname
= g_basename (uri
);
396 leafname
= _("Unnamed");
398 write_xds_property (context
, leafname
);
400 gtk_drag_set_icon_pixbuf (context
,
401 gtk_image_get_pixbuf (GTK_IMAGE (savebox
->icon
)),
407 drag_data_get (GtkWidget
*widget
,
408 GdkDragContext
*context
,
409 GtkSelectionData
*selection_data
,
414 guchar to_send
= 'E';
418 g_return_if_fail (widget
!= NULL
);
419 g_return_if_fail (GTK_IS_SAVEBOX (widget
));
420 g_return_if_fail (context
!= NULL
);
421 g_return_if_fail (selection_data
!= NULL
);
423 savebox
= GTK_SAVEBOX (widget
);
425 /* We're only concerned with the XDS protocol. Responding to other requests
426 * (including application/octet-stream) is the job of the application.
428 if (info
!= GTK_TARGET_XDS
)
430 /* Assume that the data will be/has been sent */
431 savebox
->data_sent
= TRUE
;
435 uri
= read_xds_property (context
, FALSE
);
439 gint result
= GTK_XDS_NO_HANDLER
;
441 pathname
= get_local_path (uri
);
443 to_send
= 'F'; /* Not on the local machine */
446 g_signal_emit (widget
, savebox_signals
[SAVE_TO_FILE
], 0,
450 if (result
== GTK_XDS_SAVED
)
452 savebox
->data_sent
= TRUE
;
455 else if (result
!= GTK_XDS_SAVE_ERROR
)
456 g_warning ("No handler for saving to a file.\n");
463 g_warning (_("Remote application wants to use Direct Save, but I can't "
464 "read the XdndDirectSave0 (type text/plain) property.\n"));
468 savebox
->using_xds
= TRUE
;
469 gtk_selection_data_set (selection_data
, xa_string
, 8, &to_send
, 1);
473 read_xds_property (GdkDragContext
*context
, gboolean
delete)
477 guchar
*retval
= NULL
;
479 g_return_val_if_fail (context
!= NULL
, NULL
);
481 if (gdk_property_get (context
->source_window
, XdndDirectSave
, text_plain
,
482 0, XDS_MAXURILEN
, delete,
483 NULL
, NULL
, &length
, &prop_text
)
486 /* Terminate the string */
487 retval
= g_realloc (prop_text
, length
+ 1);
488 retval
[length
] = '\0';
495 write_xds_property (GdkDragContext
*context
, const guchar
*value
)
499 gdk_property_change (context
->source_window
, XdndDirectSave
,
500 text_plain
, 8, GDK_PROP_MODE_REPLACE
,
501 value
, strlen (value
));
504 gdk_property_delete (context
->source_window
, XdndDirectSave
);
507 static void drag_end (GtkWidget
*widget
, GdkDragContext
*context
)
509 g_return_if_fail (widget
!= NULL
);
510 g_return_if_fail (GTK_IS_SAVEBOX (widget
));
511 g_return_if_fail (context
!= NULL
);
513 if (GTK_SAVEBOX (widget
)->using_xds
)
516 uri
= read_xds_property (context
, TRUE
);
522 path
= get_local_path (uri
);
524 g_signal_emit (widget
, savebox_signals
[SAVED_TO_URI
], 0,
525 path
? path
: (const gchar
*) uri
);
530 gtk_widget_destroy (widget
);
536 write_xds_property (context
, NULL
);
538 if (GTK_SAVEBOX (widget
)->data_sent
)
540 g_signal_emit (widget
, savebox_signals
[SAVED_TO_URI
], 0, NULL
);
541 gtk_widget_destroy (widget
);
545 static void discard_clicked (GtkWidget
*button
, GtkWidget
*savebox
)
547 g_signal_emit (savebox
, savebox_signals
[SAVED_TO_URI
], 0, NULL
);
548 gtk_widget_destroy (savebox
);
551 /* User has clicked Save or pressed Return... */
552 static void do_save (GtkSavebox
*savebox
)
554 gint result
= GTK_XDS_NO_HANDLER
;
558 g_return_if_fail (savebox
!= NULL
);
559 g_return_if_fail (GTK_IS_SAVEBOX (savebox
));
561 uri
= gtk_entry_get_text (GTK_ENTRY (savebox
->entry
));
562 pathname
= get_local_path (uri
);
568 dialog
= gtk_message_dialog_new (GTK_WINDOW (savebox
),
569 GTK_DIALOG_MODAL
| GTK_DIALOG_DESTROY_WITH_PARENT
,
570 GTK_MESSAGE_INFO
, GTK_BUTTONS_OK
,
571 _("Drag the icon to a directory viewer\n"
572 "(or enter a full pathname)"));
574 gtk_window_set_position (GTK_WINDOW (dialog
), GTK_WIN_POS_CENTER
);
576 gtk_dialog_run (GTK_DIALOG (dialog
));
577 gtk_widget_destroy (dialog
);
582 g_signal_emit (savebox
, savebox_signals
[SAVE_TO_FILE
], 0, pathname
, &result
);
584 if (result
== GTK_XDS_SAVED
)
586 g_signal_emit (savebox
, savebox_signals
[SAVED_TO_URI
], 0, pathname
);
588 gtk_widget_destroy (GTK_WIDGET (savebox
));
590 else if (result
== GTK_XDS_NO_HANDLER
)
591 g_warning ("No handler for saving to a file.\n");
596 gtk_savebox_response (GtkDialog
*savebox
, gint response
)
598 if (response
== GTK_RESPONSE_OK
)
600 do_save(GTK_SAVEBOX(savebox
));
603 else if (response
== GTK_RESPONSE_CANCEL
)
604 gtk_widget_destroy (GTK_WIDGET (savebox
));
608 gtk_savebox_set_property (GObject
*object
,
615 savebox
= GTK_SAVEBOX (object
);
619 case PROP_HAS_DISCARD
:
620 gtk_savebox_set_has_discard (GTK_SAVEBOX(object
),
621 g_value_get_boolean (value
));
625 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
631 gtk_savebox_get_property (GObject
*object
,
638 savebox
= GTK_SAVEBOX (object
);
642 case PROP_HAS_DISCARD
:
643 g_value_set_boolean (value
, GTK_WIDGET_VISIBLE(savebox
->discard_area
));
647 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
653 marshal_INT__STRING (GClosure
*closure
,
654 GValue
*return_value
,
655 guint n_param_values
,
656 const GValue
*param_values
,
657 gpointer invocation_hint
,
658 gpointer marshal_data
)
660 typedef gint (*GMarshalFunc_INT__STRING
) (gpointer data1
,
663 register GMarshalFunc_INT__STRING callback
;
664 register GCClosure
*cc
= (GCClosure
*) closure
;
665 register gpointer data1
, data2
;
668 g_return_if_fail (return_value
!= NULL
);
669 g_return_if_fail (n_param_values
== 2);
671 if (G_CCLOSURE_SWAP_DATA (closure
))
673 data1
= closure
->data
;
674 data2
= g_value_peek_pointer (param_values
+ 0);
678 data1
= g_value_peek_pointer (param_values
+ 0);
679 data2
= closure
->data
;
681 callback
= (GMarshalFunc_INT__STRING
)
682 (marshal_data
? marshal_data
: cc
->callback
);
684 v_return
= callback (data1
, param_values
[1].data
[0].v_pointer
, data2
);
686 g_value_set_int (return_value
, v_return
);