2 * this file is part of evince, a gnome document viewer
4 * Copyright (C) 2006 Carlos Garcia Campos
7 * Carlos Garcia Campos <carlosgc@gnome.org>
9 * Evince is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * Evince is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
28 #include <glib/gi18n.h>
29 #include <glib/gstdio.h>
32 #include <libgnomeui/gnome-icon-lookup.h>
34 #include "ev-attachment-bar.h"
51 static const GtkTargetEntry drag_targets[] = {
52 { "text/uri-list", 0, 0 }
55 static guint signals[N_SIGNALS];
57 struct _EvAttachmentBarPrivate {
63 GtkIconTheme *icon_theme;
64 GHashTable *icon_cache;
67 G_DEFINE_TYPE (EvAttachmentBar, ev_attachment_bar, GTK_TYPE_EXPANDER)
69 #define EV_ATTACHMENT_BAR_GET_PRIVATE(object) \
70 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_ATTACHMENT_BAR, EvAttachmentBarPrivate))
74 ev_attachment_bar_icon_cache_add (EvAttachmentBar *ev_attachbar,
75 const gchar *mime_type,
76 const GdkPixbuf *pixbuf)
78 g_assert (mime_type != NULL);
79 g_assert (GDK_IS_PIXBUF (pixbuf));
81 g_hash_table_insert (ev_attachbar->priv->icon_cache,
82 (gpointer)g_strdup (mime_type),
88 icon_theme_get_pixbuf_from_mime_type (GtkIconTheme *icon_theme,
89 const gchar *mime_type)
91 GdkPixbuf *pixbuf = NULL;
94 icon = gnome_icon_lookup (icon_theme,
98 GNOME_ICON_LOOKUP_FLAGS_NONE,
101 pixbuf = gtk_icon_theme_load_icon (icon_theme,
109 ev_attachment_bar_icon_cache_get (EvAttachmentBar *ev_attachbar,
110 const gchar *mime_type)
112 GdkPixbuf *pixbuf = NULL;
114 g_assert (mime_type != NULL);
116 pixbuf = g_hash_table_lookup (ev_attachbar->priv->icon_cache,
119 if (GDK_IS_PIXBUF (pixbuf))
122 pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
125 if (GDK_IS_PIXBUF (pixbuf))
126 ev_attachment_bar_icon_cache_add (ev_attachbar,
134 icon_cache_update_icon (gchar *key,
136 EvAttachmentBar *ev_attachbar)
138 GdkPixbuf *pixbuf = NULL;
140 pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
143 ev_attachment_bar_icon_cache_add (ev_attachbar,
151 ev_attachment_bar_icon_cache_refresh (EvAttachmentBar *ev_attachbar)
153 g_hash_table_foreach_remove (ev_attachbar->priv->icon_cache,
154 (GHRFunc) icon_cache_update_icon,
159 ev_attachment_bar_toggled (GObject *object,
160 GParamSpec *param_spec,
163 EvAttachmentBar *attachbar = EV_ATTACHMENT_BAR (object);
164 GtkExpander *expander = GTK_EXPANDER (object);
166 if (!attachbar->priv->label)
169 if (gtk_expander_get_expanded (expander)) {
170 gtk_label_set_text (GTK_LABEL (attachbar->priv->label),
171 _("Hide attachments bar"));
173 gtk_label_set_text (GTK_LABEL (attachbar->priv->label),
174 _("Show attachments bar"));
178 static EvAttachment *
179 ev_attachment_bar_get_attachment_at_pos (EvAttachmentBar *ev_attachbar,
183 GtkTreePath *path = NULL;
185 EvAttachment *attachment = NULL;
187 path = gtk_icon_view_get_path_at_pos (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
193 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
195 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
196 COLUMN_ATTACHMENT, &attachment,
199 gtk_icon_view_select_path (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
202 gtk_tree_path_free (path);
208 ev_attachment_bar_popup_menu_show (EvAttachmentBar *ev_attachbar,
212 GtkIconView *icon_view;
214 GList *selected = NULL, *l;
215 GList *attach_list = NULL;
217 icon_view = GTK_ICON_VIEW (ev_attachbar->priv->icon_view);
219 path = gtk_icon_view_get_path_at_pos (icon_view, x, y);
223 if (!gtk_icon_view_path_is_selected (icon_view, path)) {
224 gtk_icon_view_unselect_all (icon_view);
225 gtk_icon_view_select_path (icon_view, path);
228 gtk_tree_path_free (path);
230 selected = gtk_icon_view_get_selected_items (icon_view);
234 for (l = selected; l && l->data; l = g_list_next (l)) {
236 EvAttachment *attachment = NULL;
238 path = (GtkTreePath *) l->data;
240 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
242 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
243 COLUMN_ATTACHMENT, &attachment,
247 attach_list = g_list_prepend (attach_list, attachment);
249 gtk_tree_path_free (path);
252 g_list_free (selected);
257 g_signal_emit (ev_attachbar, signals[SIGNAL_POPUP_MENU], 0, attach_list);
263 ev_attachment_bar_popup_menu (GtkWidget *widget)
265 EvAttachmentBar *ev_attachbar = EV_ATTACHMENT_BAR (widget);
268 gtk_widget_get_pointer (widget, &x, &y);
270 return ev_attachment_bar_popup_menu_show (ev_attachbar, x, y);
274 ev_attachment_bar_button_press (EvAttachmentBar *ev_attachbar,
275 GdkEventButton *event,
276 GtkWidget *icon_view)
278 if (!GTK_WIDGET_HAS_FOCUS (icon_view)) {
279 gtk_widget_grab_focus (icon_view);
282 if (event->button == 2)
285 switch (event->button) {
287 if (event->type == GDK_2BUTTON_PRESS) {
288 GError *error = NULL;
289 EvAttachment *attachment;
291 attachment = ev_attachment_bar_get_attachment_at_pos (ev_attachbar,
297 ev_attachment_open (attachment, &error);
300 g_warning (error->message);
301 g_error_free (error);
304 g_object_unref (attachment);
310 return ev_attachment_bar_popup_menu_show (ev_attachbar, event->x, event->y);
317 ev_attachment_bar_focus_in (GtkWidget *widget,
318 GdkEventFocus *event)
320 EvAttachmentBar *ev_attachbar = EV_ATTACHMENT_BAR (widget);
322 if (gtk_expander_get_expanded (GTK_EXPANDER (ev_attachbar)))
323 gtk_widget_grab_focus (ev_attachbar->priv->icon_view);
329 ev_attachment_bar_update_icons (EvAttachmentBar *ev_attachbar,
335 ev_attachment_bar_icon_cache_refresh (ev_attachbar);
337 valid = gtk_tree_model_get_iter_first (
338 GTK_TREE_MODEL (ev_attachbar->priv->model),
342 EvAttachment *attachment = NULL;
343 GdkPixbuf *pixbuf = NULL;
344 const gchar *mime_type;
346 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
347 COLUMN_ATTACHMENT, &attachment,
350 mime_type = ev_attachment_get_mime_type (attachment);
353 g_object_unref (attachment);
355 pixbuf = ev_attachment_bar_icon_cache_get (ev_attachbar,
358 gtk_list_store_set (ev_attachbar->priv->model, &iter,
362 valid = gtk_tree_model_iter_next (
363 GTK_TREE_MODEL (ev_attachbar->priv->model),
369 ev_attachment_bar_drag_data_get (GtkWidget *widget,
370 GdkDragContext *drag_context,
371 GtkSelectionData *data,
376 EvAttachmentBar *ev_attachbar = EV_ATTACHMENT_BAR (user_data);
379 GList *selected = NULL, *l;
381 selected = gtk_icon_view_get_selected_items (GTK_ICON_VIEW (ev_attachbar->priv->icon_view));
385 uri_list = g_string_new (NULL);
387 for (l = selected; l && l->data; l = g_list_next (l)) {
388 EvAttachment *attachment;
391 gchar *uri, *filename;
392 GError *error = NULL;
394 path = (GtkTreePath *) l->data;
396 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
398 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
399 COLUMN_ATTACHMENT, &attachment,
402 filename = g_build_filename (g_get_tmp_dir (),
403 ev_attachment_get_name (attachment),
406 uri = g_filename_to_uri (filename, NULL, NULL);
408 if (ev_attachment_save (attachment, filename, &error)) {
409 g_string_append (uri_list, uri);
410 g_string_append_c (uri_list, '\n');
414 g_warning (error->message);
415 g_error_free (error);
419 gtk_tree_path_free (path);
420 g_object_unref (attachment);
423 uris = g_string_free (uri_list, FALSE);
426 gtk_selection_data_set (data,
433 g_list_free (selected);
437 ev_attachment_bar_destroy (GtkObject *object)
439 EvAttachmentBar *ev_attachbar = EV_ATTACHMENT_BAR (object);
441 if (ev_attachbar->priv->model) {
442 g_object_unref (ev_attachbar->priv->model);
443 ev_attachbar->priv->model = NULL;
446 if (ev_attachbar->priv->icon_cache) {
447 g_hash_table_destroy (ev_attachbar->priv->icon_cache);
448 ev_attachbar->priv->icon_cache = NULL;
451 (* GTK_OBJECT_CLASS (ev_attachment_bar_parent_class)->destroy) (object);
455 ev_attachment_bar_class_init (EvAttachmentBarClass *ev_attachbar_class)
457 GObjectClass *g_object_class;
458 GtkObjectClass *gtk_object_class;
459 GtkWidgetClass *gtk_widget_class;
461 g_object_class = G_OBJECT_CLASS (ev_attachbar_class);
462 gtk_object_class = GTK_OBJECT_CLASS (ev_attachbar_class);
463 gtk_widget_class = GTK_WIDGET_CLASS (ev_attachbar_class);
465 gtk_object_class->destroy = ev_attachment_bar_destroy;
466 gtk_widget_class->popup_menu = ev_attachment_bar_popup_menu;
467 gtk_widget_class->focus_in_event = ev_attachment_bar_focus_in;
469 g_type_class_add_private (g_object_class, sizeof (EvAttachmentBarPrivate));
472 signals[SIGNAL_POPUP_MENU] =
473 g_signal_new ("popup",
474 G_TYPE_FROM_CLASS (g_object_class),
475 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
476 G_STRUCT_OFFSET (EvAttachmentBarClass, popup_menu),
478 g_cclosure_marshal_VOID__POINTER,
484 ev_attachment_bar_init (EvAttachmentBar *ev_attachbar)
488 ev_attachbar->priv = EV_ATTACHMENT_BAR_GET_PRIVATE (ev_attachbar);
490 swindow = gtk_scrolled_window_new (NULL, NULL);
491 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
493 GTK_POLICY_AUTOMATIC);
494 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
496 gtk_widget_set_size_request (swindow, -1, MIN_HEIGHT);
499 ev_attachbar->priv->model = gtk_list_store_new (N_COLS,
506 ev_attachbar->priv->icon_view =
507 gtk_icon_view_new_with_model (GTK_TREE_MODEL (ev_attachbar->priv->model));
508 gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
509 GTK_SELECTION_MULTIPLE);
510 gtk_icon_view_set_columns (GTK_ICON_VIEW (ev_attachbar->priv->icon_view), -1);
511 g_object_set (G_OBJECT (ev_attachbar->priv->icon_view),
512 "text-column", COLUMN_NAME,
513 "pixbuf-column", COLUMN_ICON,
515 g_signal_connect_swapped (G_OBJECT (ev_attachbar->priv->icon_view),
516 "button-press-event",
517 G_CALLBACK (ev_attachment_bar_button_press),
518 (gpointer) ev_attachbar);
520 gtk_container_add (GTK_CONTAINER (swindow),
521 ev_attachbar->priv->icon_view);
522 gtk_widget_show (ev_attachbar->priv->icon_view);
524 gtk_container_add (GTK_CONTAINER (ev_attachbar),
526 gtk_widget_show (swindow);
529 ev_attachbar->priv->icon_theme = gtk_icon_theme_get_default ();
530 g_signal_connect_swapped (G_OBJECT (ev_attachbar->priv->icon_theme),
532 G_CALLBACK (ev_attachment_bar_update_icons),
533 (gpointer) ev_attachbar);
536 ev_attachbar->priv->icon_cache = g_hash_table_new_full (g_str_hash,
542 #ifdef HAVE_GTK_ICON_VIEW_ENABLE_MODEL_DRAG_SOURCE
543 gtk_icon_view_enable_model_drag_source (
544 GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
547 G_N_ELEMENTS (drag_targets),
550 g_signal_connect (G_OBJECT (ev_attachbar->priv->icon_view),
552 G_CALLBACK (ev_attachment_bar_drag_data_get),
553 (gpointer) ev_attachbar);
555 g_signal_connect (G_OBJECT (ev_attachbar),
557 G_CALLBACK (ev_attachment_bar_toggled),
563 ev_attachment_bar_setup (EvAttachmentBar *ev_attachbar)
568 hbox = gtk_hbox_new (FALSE, 6);
569 image = gtk_image_new_from_stock ("gnome-stock-attach",
571 gtk_box_pack_start (GTK_BOX (hbox),
574 gtk_widget_show (image);
576 ev_attachbar->priv->label = gtk_label_new (_("Show attachments bar"));
577 gtk_box_pack_start (GTK_BOX (hbox),
578 ev_attachbar->priv->label,
580 gtk_widget_show (ev_attachbar->priv->label);
582 gtk_expander_set_label_widget (GTK_EXPANDER (ev_attachbar), hbox);
583 gtk_widget_show (hbox);
587 ev_attachment_bar_new (void)
589 GtkWidget *ev_attachbar;
591 ev_attachbar = g_object_new (EV_TYPE_ATTACHMENT_BAR, NULL);
593 ev_attachment_bar_setup (EV_ATTACHMENT_BAR (ev_attachbar));
599 ev_attachment_bar_set_document (EvAttachmentBar *ev_attachbar,
600 EvDocument *document)
602 GList *attachments = NULL;
605 if (!ev_document_has_attachments (document))
608 attachments = ev_document_get_attachments (document);
610 gtk_list_store_clear (ev_attachbar->priv->model);
612 for (l = attachments; l && l->data; l = g_list_next (l)) {
613 EvAttachment *attachment;
615 GdkPixbuf *pixbuf = NULL;
616 const gchar *mime_type;
618 attachment = EV_ATTACHMENT (l->data);
620 mime_type = ev_attachment_get_mime_type (attachment);
621 pixbuf = ev_attachment_bar_icon_cache_get (ev_attachbar,
624 gtk_list_store_append (ev_attachbar->priv->model, &iter);
625 gtk_list_store_set (ev_attachbar->priv->model, &iter,
626 COLUMN_NAME, ev_attachment_get_name (attachment),
628 COLUMN_ATTACHMENT, attachment,
631 g_object_unref (attachment);
634 g_list_free (attachments);