1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2004 Red Hat, Inc.
4 * Copyright (C) 2004, 2005 Anders Carlsson <andersca@gnome.org>
7 * Jonathan Blandford <jrb@alum.mit.edu>
8 * Anders Carlsson <andersca@gnome.org>
10 * Evince is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * Evince is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
31 #include <glib/gi18n.h>
33 #include "ev-sidebar-page.h"
34 #include "ev-sidebar-thumbnails.h"
35 #include "ev-document-thumbnails.h"
36 #include "ev-document-misc.h"
37 #include "ev-job-queue.h"
38 #include "ev-window.h"
41 #define THUMBNAIL_WIDTH 100
43 /* The IconView doesn't scale nearly as well as the TreeView, so we arbitrarily
45 #define MAX_ICON_VIEW_PAGE_COUNT 1500
48 struct _EvSidebarThumbnailsPrivate {
52 GtkAdjustment *vadjustment;
53 GtkListStore *list_store;
54 GdkPixbuf *loading_icon;
56 EvPageCache *page_cache;
58 gint n_pages, pages_done;
63 gint start_page, end_page;
79 static void ev_sidebar_thumbnails_clear_model (EvSidebarThumbnails *sidebar);
80 static gboolean ev_sidebar_thumbnails_support_document (EvSidebarPage *sidebar_page,
81 EvDocument *document);
82 static void ev_sidebar_thumbnails_page_iface_init (EvSidebarPageIface *iface);
83 static void ev_sidebar_thumbnails_set_document (EvSidebarPage *sidebar_page,
84 EvDocument *document);
85 static const gchar* ev_sidebar_thumbnails_get_label (EvSidebarPage *sidebar_page);
86 static void thumbnail_job_completed_callback (EvJobThumbnail *job,
87 EvSidebarThumbnails *sidebar_thumbnails);
88 static void adjustment_changed_cb (EvSidebarThumbnails *sidebar_thumbnails);
90 G_DEFINE_TYPE_EXTENDED (EvSidebarThumbnails,
91 ev_sidebar_thumbnails,
94 G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
95 ev_sidebar_thumbnails_page_iface_init))
97 #define EV_SIDEBAR_THUMBNAILS_GET_PRIVATE(object) \
98 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_THUMBNAILS, EvSidebarThumbnailsPrivate));
102 ev_sidebar_thumbnails_dispose (GObject *object)
104 EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (object);
106 if (sidebar_thumbnails->priv->loading_icon) {
107 g_object_unref (sidebar_thumbnails->priv->loading_icon);
108 sidebar_thumbnails->priv->loading_icon = NULL;
111 if (sidebar_thumbnails->priv->list_store) {
112 ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
113 g_object_unref (sidebar_thumbnails->priv->list_store);
114 sidebar_thumbnails->priv->list_store = NULL;
117 G_OBJECT_CLASS (ev_sidebar_thumbnails_parent_class)->dispose (object);
121 ev_sidebar_thumbnails_get_property (GObject *object,
126 EvSidebarThumbnails *sidebar = EV_SIDEBAR_THUMBNAILS (object);
130 if (sidebar->priv->tree_view)
131 g_value_set_object (value, sidebar->priv->tree_view);
133 g_value_set_object (value, sidebar->priv->icon_view);
136 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142 ev_sidebar_thumbnails_map (GtkWidget *widget)
144 EvSidebarThumbnails *sidebar;
146 sidebar = EV_SIDEBAR_THUMBNAILS (widget);
148 GTK_WIDGET_CLASS (ev_sidebar_thumbnails_parent_class)->map (widget);
150 adjustment_changed_cb (sidebar);
154 ev_sidebar_thumbnails_class_init (EvSidebarThumbnailsClass *ev_sidebar_thumbnails_class)
156 GObjectClass *g_object_class;
157 GtkObjectClass *gtk_object_class;
158 GtkWidgetClass *widget_class;
160 g_object_class = G_OBJECT_CLASS (ev_sidebar_thumbnails_class);
161 gtk_object_class = GTK_OBJECT_CLASS (ev_sidebar_thumbnails_class);
162 widget_class = GTK_WIDGET_CLASS (ev_sidebar_thumbnails_class);
164 g_object_class->dispose = ev_sidebar_thumbnails_dispose;
165 g_object_class->get_property = ev_sidebar_thumbnails_get_property;
166 widget_class->map = ev_sidebar_thumbnails_map;
168 g_object_class_override_property (g_object_class,
172 g_type_class_add_private (g_object_class, sizeof (EvSidebarThumbnailsPrivate));
176 ev_sidebar_thumbnails_new (void)
178 GtkWidget *ev_sidebar_thumbnails;
180 ev_sidebar_thumbnails = g_object_new (EV_TYPE_SIDEBAR_THUMBNAILS, NULL);
182 return ev_sidebar_thumbnails;
186 clear_range (EvSidebarThumbnails *sidebar_thumbnails,
190 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
195 g_assert (start_page <= end_page);
197 path = gtk_tree_path_new_from_indices (start_page, -1);
198 for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
199 result && start_page <= end_page;
200 result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), start_page ++) {
203 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store),
209 g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, sidebar_thumbnails);
210 ev_job_queue_remove_job (EV_JOB (job));
211 g_object_unref (job);
214 gtk_list_store_set (priv->list_store, &iter,
216 COLUMN_THUMBNAIL_SET, FALSE,
217 COLUMN_PIXBUF, priv->loading_icon,
220 gtk_tree_path_free (path);
224 get_scale_for_page (EvSidebarThumbnails *sidebar_thumbnails,
227 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
230 ev_page_cache_get_size (priv->page_cache,
232 1.0, &width, &height);
234 return (gdouble)THUMBNAIL_WIDTH / (gdouble)width;
238 add_range (EvSidebarThumbnails *sidebar_thumbnails,
242 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
246 gint page = start_page;
248 g_assert (start_page <= end_page);
250 path = gtk_tree_path_new_from_indices (start_page, -1);
251 for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
252 result && page <= end_page;
253 result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), page ++) {
255 gboolean thumbnail_set;
257 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store), &iter,
259 COLUMN_THUMBNAIL_SET, &thumbnail_set,
262 if (job == NULL && !thumbnail_set) {
263 job = ev_job_thumbnail_new (priv->document,
264 page, priv->rotation,
265 get_scale_for_page (sidebar_thumbnails, page));
266 ev_job_queue_add_job (EV_JOB (job), EV_JOB_PRIORITY_HIGH);
268 g_object_set_data_full (G_OBJECT (job), "tree_iter",
269 gtk_tree_iter_copy (&iter),
270 (GDestroyNotify) gtk_tree_iter_free);
271 g_signal_connect (job, "finished",
272 G_CALLBACK (thumbnail_job_completed_callback),
274 gtk_list_store_set (priv->list_store, &iter,
278 /* The queue and the list own a ref to the job now */
279 g_object_unref (job);
281 g_object_unref (job);
284 gtk_tree_path_free (path);
287 /* This modifies start */
289 update_visible_range (EvSidebarThumbnails *sidebar_thumbnails,
293 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
294 int old_start_page, old_end_page;
296 old_start_page = priv->start_page;
297 old_end_page = priv->end_page;
299 if (start_page == old_start_page &&
300 end_page == old_end_page)
303 /* Clear the areas we no longer display */
304 if (old_start_page >= 0 && old_start_page < start_page)
305 clear_range (sidebar_thumbnails, old_start_page, MIN (start_page - 1, old_end_page));
307 if (old_end_page > 0 && old_end_page > end_page)
308 clear_range (sidebar_thumbnails, MAX (end_page + 1, old_start_page), old_end_page);
310 add_range (sidebar_thumbnails, start_page, end_page);
312 priv->start_page = start_page;
313 priv->end_page = end_page;
317 adjustment_changed_cb (EvSidebarThumbnails *sidebar_thumbnails)
319 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
320 GtkTreePath *path = NULL;
321 GtkTreePath *path2 = NULL;
325 /* Widget is not currently visible */
326 if (!GTK_WIDGET_MAPPED (sidebar_thumbnails))
329 if (priv->tree_view) {
330 if (! GTK_WIDGET_REALIZED (priv->tree_view))
333 #if GTK_CHECK_VERSION (2, 11, 3)
334 gtk_tree_view_convert_widget_to_bin_window_coords (GTK_TREE_VIEW (priv->tree_view),
335 0, (int) priv->vadjustment->value,
337 gtk_tree_view_convert_widget_to_bin_window_coords (GTK_TREE_VIEW (priv->tree_view),
338 0, (int) (priv->vadjustment->value + priv->vadjustment->page_size),
341 gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
342 0, (int) priv->vadjustment->value,
344 gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
345 0, (int) (priv->vadjustment->value + priv->vadjustment->page_size),
347 #endif /* GTK_CHECK_VERSION (2, 11, 3) */
349 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
352 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
355 } else if (priv->icon_view) {
356 if (! GTK_WIDGET_REALIZED (priv->icon_view))
358 if (! gtk_icon_view_get_visible_range (GTK_ICON_VIEW (priv->icon_view), &path, &path2))
365 update_visible_range (sidebar_thumbnails,
366 gtk_tree_path_get_indices (path)[0],
367 gtk_tree_path_get_indices (path2)[0]);
370 gtk_tree_path_free (path);
371 gtk_tree_path_free (path2);
375 ev_sidebar_thumbnails_fill_model (EvSidebarThumbnails *sidebar_thumbnails)
377 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
381 for (i = 0; i < sidebar_thumbnails->priv->n_pages; i++) {
385 page_label = ev_page_cache_get_page_label (priv->page_cache, i);
386 page_string = g_markup_printf_escaped ("<i>%s</i>", page_label);
388 gtk_list_store_append (priv->list_store, &iter);
389 gtk_list_store_set (priv->list_store, &iter,
390 COLUMN_PAGE_STRING, page_string,
391 COLUMN_PIXBUF, priv->loading_icon,
392 COLUMN_THUMBNAIL_SET, FALSE,
395 g_free (page_string);
401 ev_sidebar_thumbnails_set_loading_icon (EvSidebarThumbnails *sidebar_thumbnails)
403 if (sidebar_thumbnails->priv->loading_icon)
404 g_object_unref (sidebar_thumbnails->priv->loading_icon);
406 if (sidebar_thumbnails->priv->document) {
409 /* We get the dimensions of the first page so that we can make a blank
411 ev_page_cache_get_thumbnail_size (sidebar_thumbnails->priv->page_cache, 0,
412 sidebar_thumbnails->priv->rotation,
415 sidebar_thumbnails->priv->loading_icon =
416 ev_document_misc_get_thumbnail_frame (width, height, NULL);
418 sidebar_thumbnails->priv->loading_icon = NULL;
424 refresh (EvSidebarThumbnails *sidebar_thumbnails)
426 adjustment_changed_cb (sidebar_thumbnails);
431 ev_sidebar_thumbnails_refresh (EvSidebarThumbnails *sidebar_thumbnails,
434 sidebar_thumbnails->priv->rotation = rotation;
435 ev_sidebar_thumbnails_set_loading_icon (sidebar_thumbnails);
437 if (sidebar_thumbnails->priv->document == NULL)
440 ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
441 ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
443 /* Trigger a redraw */
444 sidebar_thumbnails->priv->start_page = -1;
445 sidebar_thumbnails->priv->end_page = -1;
446 g_idle_add ((GSourceFunc)refresh, sidebar_thumbnails);
450 ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
451 EvSidebarThumbnails *ev_sidebar_thumbnails)
453 EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
458 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
461 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
463 page = gtk_tree_path_get_indices (path)[0];
464 gtk_tree_path_free (path);
466 ev_page_cache_set_current_page_history (priv->page_cache, page);
470 ev_sidebar_icon_selection_changed (GtkIconView *icon_view,
471 EvSidebarThumbnails *ev_sidebar_thumbnails)
473 EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
478 selected = gtk_icon_view_get_selected_items (icon_view);
479 if (selected == NULL)
482 /* We don't handle or expect multiple selection. */
483 g_assert (selected->next == NULL);
485 path = selected->data;
486 page = gtk_tree_path_get_indices (path)[0];
488 gtk_tree_path_free (path);
489 g_list_free (selected);
491 ev_page_cache_set_current_page_history (priv->page_cache, page);
495 ev_sidebar_init_tree_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
497 EvSidebarThumbnailsPrivate *priv;
498 GtkTreeSelection *selection;
499 GtkCellRenderer *renderer;
501 priv = ev_sidebar_thumbnails->priv;
502 priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
504 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
505 g_signal_connect (selection, "changed",
506 G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
507 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
508 renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
512 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
516 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
517 NULL, gtk_cell_renderer_text_new (),
519 gtk_container_add (GTK_CONTAINER (priv->swindow), priv->tree_view);
520 gtk_widget_show (priv->tree_view);
524 ev_sidebar_init_icon_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
526 EvSidebarThumbnailsPrivate *priv;
528 priv = ev_sidebar_thumbnails->priv;
530 priv->icon_view = gtk_icon_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
531 gtk_icon_view_set_markup_column (GTK_ICON_VIEW (priv->icon_view), 0);
532 gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (priv->icon_view), 1);
533 g_signal_connect (priv->icon_view, "selection-changed",
534 G_CALLBACK (ev_sidebar_icon_selection_changed), ev_sidebar_thumbnails);
536 gtk_container_add (GTK_CONTAINER (priv->swindow), priv->icon_view);
537 gtk_widget_show (priv->icon_view);
541 ev_sidebar_thumbnails_use_icon_view (EvSidebarThumbnails *sidebar_thumbnails)
543 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
544 if (ev_page_cache_get_n_pages (priv->page_cache) > MAX_ICON_VIEW_PAGE_COUNT)
550 ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
552 EvSidebarThumbnailsPrivate *priv;
554 priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
556 priv->list_store = gtk_list_store_new (NUM_COLUMNS,
560 EV_TYPE_JOB_THUMBNAIL);
562 priv->swindow = gtk_scrolled_window_new (NULL, NULL);
564 /* We actually don't want GTK_POLICY_AUTOMATIC for horizontal scrollbar here
565 * it's just a workaround for bug #449462
567 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->swindow),
568 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
569 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->swindow),
571 priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->swindow));
572 g_signal_connect_data (G_OBJECT (priv->vadjustment), "value-changed",
573 G_CALLBACK (adjustment_changed_cb),
574 ev_sidebar_thumbnails, NULL,
575 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
576 g_signal_connect_swapped (G_OBJECT (priv->swindow), "size-allocate",
577 G_CALLBACK (adjustment_changed_cb),
578 ev_sidebar_thumbnails);
579 gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), priv->swindow, TRUE, TRUE, 0);
581 /* Put it all together */
582 gtk_widget_show_all (priv->swindow);
586 page_changed_cb (EvPageCache *page_cache,
588 EvSidebarThumbnails *sidebar)
590 GtkTreeView *tree_view;
593 path = gtk_tree_path_new_from_indices (page, -1);
595 if (sidebar->priv->tree_view) {
596 tree_view = GTK_TREE_VIEW (sidebar->priv->tree_view);
597 gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
598 gtk_tree_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.0, 0.0);
599 } else if (sidebar->priv->icon_view) {
601 g_signal_handlers_block_by_func
602 (sidebar->priv->icon_view,
603 G_CALLBACK (ev_sidebar_icon_selection_changed), sidebar);
605 gtk_icon_view_select_path (GTK_ICON_VIEW (sidebar->priv->icon_view), path);
607 g_signal_handlers_unblock_by_func
608 (sidebar->priv->icon_view,
609 G_CALLBACK (ev_sidebar_icon_selection_changed), sidebar);
611 gtk_icon_view_set_cursor (GTK_ICON_VIEW (sidebar->priv->icon_view), path, NULL, FALSE);
614 gtk_tree_path_free (path);
618 thumbnail_job_completed_callback (EvJobThumbnail *job,
619 EvSidebarThumbnails *sidebar_thumbnails)
621 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
624 iter = (GtkTreeIter *) g_object_get_data (G_OBJECT (job), "tree_iter");
625 gtk_list_store_set (priv->list_store,
627 COLUMN_PIXBUF, job->thumbnail,
628 COLUMN_THUMBNAIL_SET, TRUE,
634 ev_sidebar_thumbnails_set_document (EvSidebarPage *sidebar_page,
635 EvDocument *document)
637 EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (sidebar_page);
639 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
641 g_return_if_fail (EV_IS_DOCUMENT_THUMBNAILS (document));
643 priv->page_cache = ev_page_cache_get (document);
644 priv->document = document;
645 priv->n_pages = ev_page_cache_get_n_pages (priv->page_cache);
647 ev_sidebar_thumbnails_set_loading_icon (sidebar_thumbnails);
649 ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
650 ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
652 /* Create the view widget, and remove the old one, if needed */
653 if (ev_sidebar_thumbnails_use_icon_view (sidebar_thumbnails)) {
654 if (priv->tree_view) {
655 gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->tree_view);
656 priv->tree_view = NULL;
659 if (! priv->icon_view) {
660 ev_sidebar_init_icon_view (sidebar_thumbnails);
661 g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
663 gtk_widget_queue_resize (priv->icon_view);
666 if (priv->icon_view) {
667 gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->icon_view);
668 priv->icon_view = NULL;
671 if (! priv->tree_view) {
672 ev_sidebar_init_tree_view (sidebar_thumbnails);
673 g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
677 /* Connect to the signal and trigger a fake callback */
678 g_signal_connect (priv->page_cache, "page-changed", G_CALLBACK (page_changed_cb), sidebar_thumbnails);
679 sidebar_thumbnails->priv->start_page = -1;
680 sidebar_thumbnails->priv->end_page = -1;
681 page_changed_cb (priv->page_cache,
682 ev_page_cache_get_current_page (priv->page_cache),
684 adjustment_changed_cb (sidebar_thumbnails);
688 ev_sidebar_thumbnails_clear_job (GtkTreeModel *model,
695 gtk_tree_model_get (model, iter, COLUMN_JOB, &job, -1);
698 ev_job_queue_remove_job (job);
699 g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, data);
700 g_object_unref (job);
707 ev_sidebar_thumbnails_clear_model (EvSidebarThumbnails *sidebar_thumbnails)
709 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
711 gtk_tree_model_foreach (GTK_TREE_MODEL (priv->list_store), ev_sidebar_thumbnails_clear_job, sidebar_thumbnails);
712 gtk_list_store_clear (priv->list_store);
716 ev_sidebar_thumbnails_support_document (EvSidebarPage *sidebar_page,
717 EvDocument *document)
719 return (EV_IS_DOCUMENT_THUMBNAILS (document));
723 ev_sidebar_thumbnails_get_label (EvSidebarPage *sidebar_page)
725 return _("Thumbnails");
729 ev_sidebar_thumbnails_page_iface_init (EvSidebarPageIface *iface)
731 iface->support_document = ev_sidebar_thumbnails_support_document;
732 iface->set_document = ev_sidebar_thumbnails_set_document;
733 iface->get_label = ev_sidebar_thumbnails_get_label;