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)
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);
290 gtk_savebox_new (const gchar
*action
)
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
);
312 return GTK_WIDGET(dialog
);
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
);
323 gtk_image_set_from_pixbuf (GTK_IMAGE (savebox
->icon
), pixbuf
);
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
);
333 gtk_savebox_set_pathname (GtkSavebox
*savebox
, const gchar
*pathname
)
335 const gchar
*slash
, *dot
;
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
)
355 gtk_savebox_set_has_discard (GtkSavebox
*savebox
, gboolean setting
)
358 gtk_widget_show_all (savebox
->discard_area
);
360 gtk_widget_hide (savebox
->discard_area
);
364 button_press_over_icon (GtkWidget
*drag_box
, GdkEventButton
*event
,
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
));
383 leafname
= g_basename (uri
);
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
)),
396 drag_data_get (GtkWidget
*widget
,
397 GdkDragContext
*context
,
398 GtkSelectionData
*selection_data
,
403 guchar to_send
= 'E';
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
;
424 uri
= read_xds_property (context
, FALSE
);
428 gint result
= GTK_XDS_NO_HANDLER
;
430 pathname
= get_local_path (uri
);
432 to_send
= 'F'; /* Not on the local machine */
435 g_signal_emit (widget
, savebox_signals
[SAVE_TO_FILE
], 0,
438 if (result
== GTK_XDS_SAVED
)
440 savebox
->data_sent
= TRUE
;
443 else if (result
!= GTK_XDS_SAVE_ERROR
)
444 g_warning ("No handler for saving to a file.\n");
451 g_warning (_("Remote application wants to use Direct Save, but I can't "
452 "read the XdndDirectSave0 (type text/plain) property.\n"));
456 savebox
->using_xds
= TRUE
;
457 gtk_selection_data_set (selection_data
, xa_string
, 8, &to_send
, 1);
461 read_xds_property (GdkDragContext
*context
, gboolean
delete)
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
)
474 /* Terminate the string */
475 retval
= g_realloc (prop_text
, length
+ 1);
476 retval
[length
] = '\0';
483 write_xds_property (GdkDragContext
*context
, const guchar
*value
)
487 gdk_property_change (context
->source_window
, XdndDirectSave
,
488 text_plain
, 8, GDK_PROP_MODE_REPLACE
,
489 value
, strlen (value
));
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
)
504 uri
= read_xds_property (context
, TRUE
);
510 path
= get_local_path (uri
);
512 g_signal_emit (widget
, savebox_signals
[SAVED_TO_URI
], 0,
513 path
? path
: (const gchar
*) uri
);
516 gtk_widget_destroy (widget
);
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
);
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
);
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");
580 gtk_savebox_response (GtkDialog
*savebox
, gint response
)
582 if (response
== GTK_RESPONSE_OK
)
584 do_save(GTK_SAVEBOX(savebox
));
587 else if (response
== GTK_RESPONSE_CANCEL
)
588 gtk_widget_destroy (GTK_WIDGET (savebox
));
592 gtk_savebox_set_property (GObject
*object
,
599 savebox
= GTK_SAVEBOX (object
);
603 case PROP_HAS_DISCARD
:
604 gtk_savebox_set_has_discard (GTK_SAVEBOX(object
),
605 g_value_get_boolean (value
));
609 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
615 gtk_savebox_get_property (GObject
*object
,
622 savebox
= GTK_SAVEBOX (object
);
626 case PROP_HAS_DISCARD
:
627 g_value_set_boolean (value
, GTK_WIDGET_VISIBLE(savebox
->discard_area
));
631 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
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
,
647 register GMarshalFunc_INT__STRING callback
;
648 register GCClosure
*cc
= (GCClosure
*) closure
;
649 register gpointer data1
, data2
;
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);
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
);