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);
89 G_DEFINE_TYPE_EXTENDED (EvSidebarThumbnails,
90 ev_sidebar_thumbnails,
93 G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
94 ev_sidebar_thumbnails_page_iface_init))
96 #define EV_SIDEBAR_THUMBNAILS_GET_PRIVATE(object) \
97 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_THUMBNAILS, EvSidebarThumbnailsPrivate));
101 ev_sidebar_thumbnails_dispose (GObject *object)
103 EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (object);
105 if (sidebar_thumbnails->priv->loading_icon) {
106 g_object_unref (sidebar_thumbnails->priv->loading_icon);
107 sidebar_thumbnails->priv->loading_icon = NULL;
110 if (sidebar_thumbnails->priv->list_store) {
111 ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
112 g_object_unref (sidebar_thumbnails->priv->list_store);
113 sidebar_thumbnails->priv->list_store = NULL;
116 G_OBJECT_CLASS (ev_sidebar_thumbnails_parent_class)->dispose (object);
120 ev_sidebar_thumbnails_get_property (GObject *object,
125 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_class_init (EvSidebarThumbnailsClass *ev_sidebar_thumbnails_class)
144 GObjectClass *g_object_class;
145 GtkObjectClass *gtk_object_class;
147 g_object_class = G_OBJECT_CLASS (ev_sidebar_thumbnails_class);
148 gtk_object_class = GTK_OBJECT_CLASS (ev_sidebar_thumbnails_class);
150 g_object_class->dispose = ev_sidebar_thumbnails_dispose;
151 g_object_class->get_property = ev_sidebar_thumbnails_get_property;
153 g_object_class_override_property (g_object_class,
157 g_type_class_add_private (g_object_class, sizeof (EvSidebarThumbnailsPrivate));
161 ev_sidebar_thumbnails_new (void)
163 GtkWidget *ev_sidebar_thumbnails;
165 ev_sidebar_thumbnails = g_object_new (EV_TYPE_SIDEBAR_THUMBNAILS, NULL);
167 return ev_sidebar_thumbnails;
171 clear_range (EvSidebarThumbnails *sidebar_thumbnails,
175 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
180 g_assert (start_page <= end_page);
182 path = gtk_tree_path_new_from_indices (start_page, -1);
183 for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
184 result && start_page <= end_page;
185 result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), start_page ++) {
188 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store),
194 g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, sidebar_thumbnails);
195 ev_job_queue_remove_job (EV_JOB (job));
196 g_object_unref (job);
199 gtk_list_store_set (priv->list_store, &iter,
201 COLUMN_THUMBNAIL_SET, FALSE,
202 COLUMN_PIXBUF, priv->loading_icon,
205 gtk_tree_path_free (path);
209 add_range (EvSidebarThumbnails *sidebar_thumbnails,
213 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
217 gint page = start_page;
219 g_assert (start_page <= end_page);
221 path = gtk_tree_path_new_from_indices (start_page, -1);
222 for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
223 result && page <= end_page;
224 result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), page ++) {
226 gboolean thumbnail_set;
228 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store), &iter,
230 COLUMN_THUMBNAIL_SET, &thumbnail_set,
233 if (job == NULL && !thumbnail_set) {
234 /* FIXME: Need rotation */
235 job = (EvJobThumbnail *)ev_job_thumbnail_new (priv->document, page, priv->rotation, THUMBNAIL_WIDTH);
236 ev_job_queue_add_job (EV_JOB (job), EV_JOB_PRIORITY_HIGH);
237 g_object_set_data_full (G_OBJECT (job), "tree_iter",
238 gtk_tree_iter_copy (&iter),
239 (GDestroyNotify) gtk_tree_iter_free);
240 g_signal_connect (job, "finished",
241 G_CALLBACK (thumbnail_job_completed_callback),
243 gtk_list_store_set (priv->list_store, &iter,
246 /* The queue and the list own a ref to the job now */
247 g_object_unref (job);
249 g_object_unref (job);
252 gtk_tree_path_free (path);
255 /* This modifies start */
257 update_visible_range (EvSidebarThumbnails *sidebar_thumbnails,
261 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
262 int old_start_page, old_end_page;
264 old_start_page = priv->start_page;
265 old_end_page = priv->end_page;
267 if (start_page == old_start_page &&
268 end_page == old_end_page)
271 /* Clear the areas we no longer display */
272 if (old_start_page < start_page)
273 clear_range (sidebar_thumbnails, old_start_page, MIN (start_page - 1, old_end_page));
275 if (old_end_page > end_page)
276 clear_range (sidebar_thumbnails, MAX (end_page + 1, old_start_page), old_end_page);
278 add_range (sidebar_thumbnails, start_page, end_page);
280 priv->start_page = start_page;
281 priv->end_page = end_page;
285 adjustment_changed_cb (EvSidebarThumbnails *sidebar_thumbnails)
287 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
288 GtkTreePath *path = NULL;
289 GtkTreePath *path2 = NULL;
293 if (priv->tree_view) {
294 if (! GTK_WIDGET_REALIZED (priv->tree_view))
297 gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
298 0, (int) priv->vadjustment->value,
300 gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
301 0, (int) (priv->vadjustment->value + priv->vadjustment->page_size),
303 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
306 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
309 } else if (priv->icon_view) {
310 if (! GTK_WIDGET_REALIZED (priv->icon_view))
312 if (! gtk_icon_view_get_visible_range (GTK_ICON_VIEW (priv->icon_view), &path, &path2))
319 path = gtk_tree_path_new_first ();
321 path2 = gtk_tree_path_new_from_indices (priv->n_pages,
323 update_visible_range (sidebar_thumbnails,
324 gtk_tree_path_get_indices (path)[0],
325 gtk_tree_path_get_indices (path2)[0]);
327 gtk_tree_path_free (path);
328 gtk_tree_path_free (path2);
332 ev_sidebar_thumbnails_fill_model (EvSidebarThumbnails *sidebar_thumbnails)
334 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
338 for (i = 0; i < sidebar_thumbnails->priv->n_pages; i++) {
342 page_label = ev_page_cache_get_page_label (priv->page_cache, i);
343 page_string = g_markup_printf_escaped ("<i>%s</i>", page_label);
345 gtk_list_store_append (priv->list_store, &iter);
346 gtk_list_store_set (priv->list_store, &iter,
347 COLUMN_PAGE_STRING, page_string,
348 COLUMN_PIXBUF, priv->loading_icon,
349 COLUMN_THUMBNAIL_SET, FALSE,
352 g_free (page_string);
358 ev_sidebar_thumbnails_set_loading_icon (EvSidebarThumbnails *sidebar_thumbnails)
360 gint width = THUMBNAIL_WIDTH;
361 gint height = THUMBNAIL_WIDTH;
363 if (sidebar_thumbnails->priv->loading_icon)
364 g_object_unref (sidebar_thumbnails->priv->loading_icon);
366 if (sidebar_thumbnails->priv->document) {
367 /* We get the dimensions of the first doc so that we can make a blank
369 ev_document_doc_mutex_lock ();
370 ev_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (sidebar_thumbnails->priv->document),
371 0, THUMBNAIL_WIDTH, &width, &height);
372 ev_document_doc_mutex_unlock ();
373 sidebar_thumbnails->priv->loading_icon =
374 ev_document_misc_get_thumbnail_frame (width, height, sidebar_thumbnails->priv->rotation, NULL);
376 sidebar_thumbnails->priv->loading_icon = NULL;
381 ev_sidebar_thumbnails_refresh (EvSidebarThumbnails *sidebar_thumbnails,
384 sidebar_thumbnails->priv->rotation = rotation;
385 ev_sidebar_thumbnails_set_loading_icon (sidebar_thumbnails);
387 if (sidebar_thumbnails->priv->document == NULL)
390 ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
391 ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
393 /* Trigger a redraw */
394 sidebar_thumbnails->priv->start_page = 0;
395 sidebar_thumbnails->priv->end_page = 0;
396 adjustment_changed_cb (sidebar_thumbnails);
400 ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
401 EvSidebarThumbnails *ev_sidebar_thumbnails)
403 EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
408 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
411 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
413 page = gtk_tree_path_get_indices (path)[0];
414 gtk_tree_path_free (path);
416 ev_page_cache_set_current_page (priv->page_cache, page);
420 ev_sidebar_icon_selection_changed (GtkIconView *icon_view,
421 EvSidebarThumbnails *ev_sidebar_thumbnails)
423 EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
428 selected = gtk_icon_view_get_selected_items (icon_view);
429 if (selected == NULL)
432 /* We don't handle or expect multiple selection. */
433 g_assert (selected->next == NULL);
435 path = selected->data;
436 page = gtk_tree_path_get_indices (path)[0];
438 gtk_tree_path_free (path);
439 g_list_free (selected);
441 ev_page_cache_set_current_page (priv->page_cache, page);
445 ev_sidebar_init_tree_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
447 EvSidebarThumbnailsPrivate *priv;
448 GtkTreeSelection *selection;
449 GtkCellRenderer *renderer;
451 priv = ev_sidebar_thumbnails->priv;
452 priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
454 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
455 g_signal_connect (selection, "changed",
456 G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
457 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
458 renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
462 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
466 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
467 NULL, gtk_cell_renderer_text_new (),
469 gtk_container_add (GTK_CONTAINER (priv->swindow), priv->tree_view);
470 gtk_widget_show (priv->tree_view);
474 ev_sidebar_init_icon_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
476 EvSidebarThumbnailsPrivate *priv;
478 priv = ev_sidebar_thumbnails->priv;
480 priv->icon_view = gtk_icon_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
481 gtk_icon_view_set_markup_column (GTK_ICON_VIEW (priv->icon_view), 0);
482 gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (priv->icon_view), 1);
483 g_signal_connect (priv->icon_view, "selection-changed",
484 G_CALLBACK (ev_sidebar_icon_selection_changed), ev_sidebar_thumbnails);
486 gtk_container_add (GTK_CONTAINER (priv->swindow), priv->icon_view);
487 gtk_widget_show (priv->icon_view);
491 ev_sidebar_thumbnails_use_icon_view (EvSidebarThumbnails *sidebar_thumbnails)
493 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
494 if (ev_page_cache_get_n_pages (priv->page_cache) > MAX_ICON_VIEW_PAGE_COUNT)
500 ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
502 EvSidebarThumbnailsPrivate *priv;
504 priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
506 priv->list_store = gtk_list_store_new (NUM_COLUMNS,
510 EV_TYPE_JOB_THUMBNAIL);
512 priv->swindow = gtk_scrolled_window_new (NULL, NULL);
513 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->swindow),
514 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
515 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->swindow),
517 priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->swindow));
518 g_signal_connect_data (G_OBJECT (priv->vadjustment), "value-changed",
519 G_CALLBACK (adjustment_changed_cb),
520 ev_sidebar_thumbnails, NULL,
521 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
522 g_signal_connect_swapped (G_OBJECT (priv->swindow), "size-allocate",
523 G_CALLBACK (adjustment_changed_cb),
524 ev_sidebar_thumbnails);
525 gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), priv->swindow, TRUE, TRUE, 0);
527 /* Put it all together */
528 gtk_widget_show_all (priv->swindow);
532 page_changed_cb (EvPageCache *page_cache,
534 EvSidebarThumbnails *sidebar)
536 GtkTreeView *tree_view;
539 path = gtk_tree_path_new_from_indices (page, -1);
541 if (sidebar->priv->tree_view) {
542 tree_view = GTK_TREE_VIEW (sidebar->priv->tree_view);
543 gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
544 gtk_tree_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.0, 0.0);
545 } else if (sidebar->priv->icon_view) {
546 gtk_icon_view_select_path (GTK_ICON_VIEW (sidebar->priv->icon_view), path);
547 gtk_icon_view_set_cursor (GTK_ICON_VIEW (sidebar->priv->icon_view), path, NULL, FALSE);
550 gtk_tree_path_free (path);
554 thumbnail_job_completed_callback (EvJobThumbnail *job,
555 EvSidebarThumbnails *sidebar_thumbnails)
557 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
560 iter = (GtkTreeIter *) g_object_get_data (G_OBJECT (job), "tree_iter");
561 gtk_list_store_set (priv->list_store,
563 COLUMN_PIXBUF, job->thumbnail,
564 COLUMN_THUMBNAIL_SET, TRUE,
570 ev_sidebar_thumbnails_set_document (EvSidebarPage *sidebar_page,
571 EvDocument *document)
573 EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (sidebar_page);
575 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
577 g_return_if_fail (EV_IS_DOCUMENT_THUMBNAILS (document));
579 priv->page_cache = ev_page_cache_get (document);
580 priv->document = document;
581 priv->n_pages = ev_page_cache_get_n_pages (priv->page_cache);
583 ev_sidebar_thumbnails_set_loading_icon (sidebar_thumbnails);
585 ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
586 ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
588 /* Create the view widget, and remove the old one, if needed */
589 if (ev_sidebar_thumbnails_use_icon_view (sidebar_thumbnails)) {
590 if (priv->tree_view) {
591 gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->tree_view);
592 priv->tree_view = NULL;
595 if (! priv->icon_view) {
596 ev_sidebar_init_icon_view (sidebar_thumbnails);
597 g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
599 gtk_widget_queue_resize (priv->icon_view);
602 if (priv->icon_view) {
603 gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->icon_view);
604 priv->icon_view = NULL;
607 if (! priv->tree_view) {
608 ev_sidebar_init_tree_view (sidebar_thumbnails);
609 g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
613 /* Connect to the signal and trigger a fake callback */
614 g_signal_connect (priv->page_cache, "page-changed", G_CALLBACK (page_changed_cb), sidebar_thumbnails);
615 sidebar_thumbnails->priv->start_page = 0;
616 sidebar_thumbnails->priv->end_page = 0;
617 adjustment_changed_cb (sidebar_thumbnails);
621 ev_sidebar_thumbnails_clear_job (GtkTreeModel *model,
628 gtk_tree_model_get (model, iter, COLUMN_JOB, &job, -1);
631 ev_job_queue_remove_job (job);
632 g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, data);
633 g_object_unref (job);
640 ev_sidebar_thumbnails_clear_model (EvSidebarThumbnails *sidebar_thumbnails)
642 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
644 gtk_tree_model_foreach (GTK_TREE_MODEL (priv->list_store), ev_sidebar_thumbnails_clear_job, sidebar_thumbnails);
645 gtk_list_store_clear (priv->list_store);
649 ev_sidebar_thumbnails_support_document (EvSidebarPage *sidebar_page,
650 EvDocument *document)
652 return (EV_IS_DOCUMENT_THUMBNAILS (document) &&
653 (ev_document_get_n_pages (document) > 1));
657 ev_sidebar_thumbnails_get_label (EvSidebarPage *sidebar_page)
659 return _("Thumbnails");
663 ev_sidebar_thumbnails_page_iface_init (EvSidebarPageIface *iface)
665 iface->support_document = ev_sidebar_thumbnails_support_document;
666 iface->set_document = ev_sidebar_thumbnails_set_document;
667 iface->get_label = ev_sidebar_thumbnails_get_label;