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;
61 gint start_page, end_page;
77 static void ev_sidebar_thumbnails_clear_model (EvSidebarThumbnails *sidebar);
78 static gboolean ev_sidebar_thumbnails_support_document (EvSidebarPage *sidebar_page,
79 EvDocument *document);
80 static void ev_sidebar_thumbnails_page_iface_init (EvSidebarPageIface *iface);
81 static void ev_sidebar_thumbnails_set_document (EvSidebarPage *sidebar_page,
82 EvDocument *document);
83 static const gchar* ev_sidebar_thumbnails_get_label (EvSidebarPage *sidebar_page);
84 static void thumbnail_job_completed_callback (EvJobThumbnail *job,
85 EvSidebarThumbnails *sidebar_thumbnails);
87 G_DEFINE_TYPE_EXTENDED (EvSidebarThumbnails,
88 ev_sidebar_thumbnails,
91 G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
92 ev_sidebar_thumbnails_page_iface_init))
94 #define EV_SIDEBAR_THUMBNAILS_GET_PRIVATE(object) \
95 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_THUMBNAILS, EvSidebarThumbnailsPrivate));
99 ev_sidebar_thumbnails_dispose (GObject *object)
101 EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (object);
103 ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
104 if (sidebar_thumbnails->priv->loading_icon) {
105 g_object_unref (sidebar_thumbnails->priv->loading_icon);
106 sidebar_thumbnails->priv->loading_icon = NULL;
109 if (sidebar_thumbnails->priv->list_store) {
110 g_object_unref (sidebar_thumbnails->priv->list_store);
111 sidebar_thumbnails->priv->list_store = NULL;
114 G_OBJECT_CLASS (ev_sidebar_thumbnails_parent_class)->dispose (object);
118 ev_sidebar_thumbnails_get_property (GObject *object,
123 EvSidebarThumbnails *sidebar = EV_SIDEBAR_THUMBNAILS (object);
128 if (sidebar->priv->tree_view)
129 g_value_set_object (value, sidebar->priv->tree_view);
131 g_value_set_object (value, sidebar->priv->icon_view);
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
140 ev_sidebar_thumbnails_class_init (EvSidebarThumbnailsClass *ev_sidebar_thumbnails_class)
142 GObjectClass *g_object_class;
143 GtkObjectClass *gtk_object_class;
145 g_object_class = G_OBJECT_CLASS (ev_sidebar_thumbnails_class);
146 gtk_object_class = GTK_OBJECT_CLASS (ev_sidebar_thumbnails_class);
148 g_object_class->dispose = ev_sidebar_thumbnails_dispose;
149 g_object_class->get_property = ev_sidebar_thumbnails_get_property;
151 g_object_class_override_property (g_object_class,
155 g_type_class_add_private (g_object_class, sizeof (EvSidebarThumbnailsPrivate));
159 ev_sidebar_thumbnails_new (void)
161 GtkWidget *ev_sidebar_thumbnails;
163 ev_sidebar_thumbnails = g_object_new (EV_TYPE_SIDEBAR_THUMBNAILS, NULL);
165 return ev_sidebar_thumbnails;
169 clear_range (EvSidebarThumbnails *sidebar_thumbnails,
173 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
178 g_assert (start_page <= end_page);
180 path = gtk_tree_path_new_from_indices (start_page, -1);
181 for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
182 result && start_page <= end_page;
183 result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), start_page ++) {
186 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store),
192 g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, sidebar_thumbnails);
193 ev_job_queue_remove_job (EV_JOB (job));
194 g_object_unref (job);
197 gtk_list_store_set (priv->list_store, &iter,
199 COLUMN_THUMBNAIL_SET, FALSE,
200 COLUMN_PIXBUF, priv->loading_icon,
203 gtk_tree_path_free (path);
207 add_range (EvSidebarThumbnails *sidebar_thumbnails,
211 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
215 gint page = start_page;
217 g_assert (start_page <= end_page);
219 path = gtk_tree_path_new_from_indices (start_page, -1);
220 for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
221 result && page <= end_page;
222 result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), page ++) {
224 gboolean thumbnail_set;
226 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store), &iter,
228 COLUMN_THUMBNAIL_SET, &thumbnail_set,
231 if (job == NULL && !thumbnail_set) {
232 job = (EvJobThumbnail *)ev_job_thumbnail_new (priv->document, page, THUMBNAIL_WIDTH,
233 EV_ORIENTATION_PORTRAIT);
234 ev_job_queue_add_job (EV_JOB (job), EV_JOB_PRIORITY_HIGH);
235 g_object_set_data_full (G_OBJECT (job), "tree_iter",
236 gtk_tree_iter_copy (&iter),
237 (GDestroyNotify) gtk_tree_iter_free);
238 g_signal_connect (job, "finished",
239 G_CALLBACK (thumbnail_job_completed_callback),
241 gtk_list_store_set (priv->list_store, &iter,
244 /* The queue and the list own a ref to the job now */
245 g_object_unref (job);
247 g_object_unref (job);
250 gtk_tree_path_free (path);
253 /* This modifies start */
255 update_visible_range (EvSidebarThumbnails *sidebar_thumbnails,
259 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
260 int old_start_page, old_end_page;
262 old_start_page = priv->start_page;
263 old_end_page = priv->end_page;
265 if (start_page == old_start_page &&
266 end_page == old_end_page)
269 /* Clear the areas we no longer display */
270 if (old_start_page < start_page)
271 clear_range (sidebar_thumbnails, old_start_page, MIN (start_page - 1, old_end_page));
273 if (old_end_page > end_page)
274 clear_range (sidebar_thumbnails, MAX (end_page + 1, old_start_page), old_end_page);
276 add_range (sidebar_thumbnails, start_page, end_page);
278 priv->start_page = start_page;
279 priv->end_page = end_page;
283 adjustment_changed_cb (EvSidebarThumbnails *sidebar_thumbnails)
285 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
286 GtkTreePath *path = NULL;
287 GtkTreePath *path2 = NULL;
291 if (priv->tree_view) {
292 if (! GTK_WIDGET_REALIZED (priv->tree_view))
295 gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
296 0, (int) priv->vadjustment->value,
298 gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
299 0, (int) (priv->vadjustment->value + priv->vadjustment->page_size),
301 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
304 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
307 } else if (priv->icon_view) {
308 #ifdef HAVE_GTK_ICON_VIEW_GET_VISIBLE_RANGE
309 if (! GTK_WIDGET_REALIZED (priv->icon_view))
311 gtk_icon_view_get_visible_range (GTK_ICON_VIEW (priv->icon_view), &path, &path2);
313 g_assert_not_reached ();
320 path = gtk_tree_path_new_first ();
322 path2 = gtk_tree_path_new_from_indices (priv->n_pages,
324 update_visible_range (sidebar_thumbnails,
325 gtk_tree_path_get_indices (path)[0],
326 gtk_tree_path_get_indices (path2)[0]);
328 gtk_tree_path_free (path);
329 gtk_tree_path_free (path2);
333 ev_sidebar_thumbnails_fill_model (EvSidebarThumbnails *sidebar_thumbnails)
335 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
339 for (i = 0; i < sidebar_thumbnails->priv->n_pages; i++) {
343 page_label = ev_page_cache_get_page_label (priv->page_cache, i);
344 page_string = g_markup_printf_escaped ("<i>%s</i>", page_label);
346 gtk_list_store_append (priv->list_store, &iter);
347 gtk_list_store_set (priv->list_store, &iter,
348 COLUMN_PAGE_STRING, page_string,
349 COLUMN_PIXBUF, priv->loading_icon,
350 COLUMN_THUMBNAIL_SET, FALSE,
353 g_free (page_string);
358 ev_sidebar_thumbnails_refresh (EvSidebarThumbnails *sidebar_thumbnails)
360 ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
361 ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
362 adjustment_changed_cb (sidebar_thumbnails);
366 ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
367 EvSidebarThumbnails *ev_sidebar_thumbnails)
369 EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
374 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
377 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
379 page = gtk_tree_path_get_indices (path)[0];
380 gtk_tree_path_free (path);
382 ev_page_cache_set_current_page (priv->page_cache, page);
386 ev_sidebar_icon_selection_changed (GtkIconView *icon_view,
387 EvSidebarThumbnails *ev_sidebar_thumbnails)
389 EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
394 selected = gtk_icon_view_get_selected_items (icon_view);
395 if (selected == NULL)
398 /* We don't handle or expect multiple selection. */
399 g_assert (selected->next == NULL);
401 path = selected->data;
402 page = gtk_tree_path_get_indices (path)[0];
404 gtk_tree_path_free (path);
405 g_list_free (selected);
407 ev_page_cache_set_current_page (priv->page_cache, page);
411 ev_sidebar_init_tree_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
413 EvSidebarThumbnailsPrivate *priv;
414 GtkTreeSelection *selection;
415 GtkCellRenderer *renderer;
417 priv = ev_sidebar_thumbnails->priv;
418 priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
420 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
421 g_signal_connect (selection, "changed",
422 G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
423 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
424 renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
428 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
432 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
433 NULL, gtk_cell_renderer_text_new (),
435 gtk_container_add (GTK_CONTAINER (priv->swindow), priv->tree_view);
436 gtk_widget_show (priv->tree_view);
440 ev_sidebar_init_icon_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
442 EvSidebarThumbnailsPrivate *priv;
444 priv = ev_sidebar_thumbnails->priv;
446 priv->icon_view = gtk_icon_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
447 gtk_icon_view_set_markup_column (GTK_ICON_VIEW (priv->icon_view), 0);
448 gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (priv->icon_view), 1);
449 g_signal_connect (priv->icon_view, "selection-changed",
450 G_CALLBACK (ev_sidebar_icon_selection_changed), ev_sidebar_thumbnails);
452 gtk_container_add (GTK_CONTAINER (priv->swindow), priv->icon_view);
453 gtk_widget_show (priv->icon_view);
457 ev_sidebar_thumbnails_use_icon_view (EvSidebarThumbnails *sidebar_thumbnails)
459 #ifdef HAVE_GTK_ICON_VIEW_GET_VISIBLE_RANGE
460 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
461 if (ev_page_cache_get_n_pages (priv->page_cache) > MAX_ICON_VIEW_PAGE_COUNT)
470 ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
472 EvSidebarThumbnailsPrivate *priv;
474 priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
476 priv->list_store = gtk_list_store_new (NUM_COLUMNS,
480 EV_TYPE_JOB_THUMBNAIL);
482 priv->swindow = gtk_scrolled_window_new (NULL, NULL);
483 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->swindow),
484 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
485 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->swindow),
487 priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->swindow));
488 g_signal_connect_data (G_OBJECT (priv->vadjustment), "value-changed",
489 G_CALLBACK (adjustment_changed_cb),
490 ev_sidebar_thumbnails, NULL,
491 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
492 g_signal_connect_swapped (G_OBJECT (priv->swindow), "size-allocate",
493 G_CALLBACK (adjustment_changed_cb),
494 ev_sidebar_thumbnails);
495 gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), priv->swindow, TRUE, TRUE, 0);
497 /* Put it all together */
498 gtk_widget_show_all (priv->swindow);
502 page_changed_cb (EvPageCache *page_cache,
504 EvSidebarThumbnails *sidebar)
506 GtkTreeView *tree_view;
509 path = gtk_tree_path_new_from_indices (page, -1);
511 if (sidebar->priv->tree_view) {
512 tree_view = GTK_TREE_VIEW (sidebar->priv->tree_view);
513 gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
514 gtk_tree_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.0, 0.0);
515 } else if (sidebar->priv->icon_view) {
516 /* Guard against gtk-2.6 */
517 #ifdef HAVE_GTK_ICON_VIEW_GET_VISIBLE_RANGE
518 gtk_icon_view_select_path (GTK_ICON_VIEW (sidebar->priv->icon_view), path);
519 gtk_icon_view_set_cursor (GTK_ICON_VIEW (sidebar->priv->icon_view), path, NULL, FALSE);
523 gtk_tree_path_free (path);
527 thumbnail_job_completed_callback (EvJobThumbnail *job,
528 EvSidebarThumbnails *sidebar_thumbnails)
530 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
533 iter = (GtkTreeIter *) g_object_get_data (G_OBJECT (job), "tree_iter");
534 gtk_list_store_set (priv->list_store,
536 COLUMN_PIXBUF, job->thumbnail,
537 COLUMN_THUMBNAIL_SET, TRUE,
543 ev_sidebar_thumbnails_set_document (EvSidebarPage *sidebar_page,
544 EvDocument *document)
546 EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (sidebar_page);
547 gint width = THUMBNAIL_WIDTH;
548 gint height = THUMBNAIL_WIDTH;
550 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
552 g_return_if_fail (EV_IS_DOCUMENT_THUMBNAILS (document));
554 priv->page_cache = ev_page_cache_get (document);
555 priv->document = document;
556 priv->n_pages = ev_page_cache_get_n_pages (priv->page_cache);
558 /* We get the dimensions of the first doc so that we can make a blank
560 ev_document_doc_mutex_lock ();
561 ev_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (priv->document),
562 0, THUMBNAIL_WIDTH, &width, &height);
563 ev_document_doc_mutex_unlock ();
565 if (priv->loading_icon)
566 g_object_unref (priv->loading_icon);
567 priv->loading_icon = ev_document_misc_get_thumbnail_frame (width, height, NULL);
569 ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
570 ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
572 /* Create the view widget, and remove the old one, if needed */
573 if (ev_sidebar_thumbnails_use_icon_view (sidebar_thumbnails)) {
574 if (priv->tree_view) {
575 gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->tree_view);
576 priv->tree_view = NULL;
579 if (! priv->icon_view) {
580 ev_sidebar_init_icon_view (sidebar_thumbnails);
581 g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
584 if (priv->icon_view) {
585 gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->icon_view);
586 priv->icon_view = NULL;
589 if (! priv->tree_view) {
590 ev_sidebar_init_tree_view (sidebar_thumbnails);
591 g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
595 /* Connect to the signal and trigger a fake callback */
596 g_signal_connect (priv->page_cache, "page-changed", G_CALLBACK (page_changed_cb), sidebar_thumbnails);
597 adjustment_changed_cb (sidebar_thumbnails);
601 ev_sidebar_thumbnails_clear_job (GtkTreeModel *model,
608 gtk_tree_model_get (model, iter, COLUMN_JOB, &job, -1);
611 ev_job_queue_remove_job (job);
612 g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, data);
613 g_object_unref (job);
620 ev_sidebar_thumbnails_clear_model (EvSidebarThumbnails *sidebar_thumbnails)
622 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
624 gtk_tree_model_foreach (GTK_TREE_MODEL (priv->list_store), ev_sidebar_thumbnails_clear_job, sidebar_thumbnails);
625 gtk_list_store_clear (priv->list_store);
629 ev_sidebar_thumbnails_support_document (EvSidebarPage *sidebar_page,
630 EvDocument *document)
632 return (EV_IS_DOCUMENT_THUMBNAILS (document) &&
633 (ev_document_get_n_pages (document) > 1));
637 ev_sidebar_thumbnails_get_label (EvSidebarPage *sidebar_page)
639 return _("Thumbnails");
643 ev_sidebar_thumbnails_page_iface_init (EvSidebarPageIface *iface)
645 iface->support_document = ev_sidebar_thumbnails_support_document;
646 iface->set_document = ev_sidebar_thumbnails_set_document;
647 iface->get_label = ev_sidebar_thumbnails_get_label;