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 /* Amount of time we devote to each iteration of the idle, in microseconds */
44 #define IDLE_WORK_LENGTH 5000
46 struct _EvSidebarThumbnailsPrivate {
48 GtkAdjustment *vadjustment;
49 GtkListStore *list_store;
50 GdkPixbuf *loading_icon;
53 gint n_pages, pages_done;
56 gint start_page, end_page;
67 static void ev_sidebar_thumbnails_clear_model (EvSidebarThumbnails *sidebar);
68 static gboolean ev_sidebar_thumbnails_support_document (EvSidebarPage *sidebar_page,
69 EvDocument *document);
70 static void ev_sidebar_thumbnails_page_iface_init (EvSidebarPageIface *iface);
71 static void ev_sidebar_thumbnails_set_document (EvSidebarPage *sidebar_page,
72 EvDocument *document);
73 static const gchar* ev_sidebar_thumbnails_get_label (EvSidebarPage *sidebar_page);
74 static void thumbnail_job_completed_callback (EvJobThumbnail *job,
75 EvSidebarThumbnails *sidebar_thumbnails);
77 G_DEFINE_TYPE_EXTENDED (EvSidebarThumbnails,
78 ev_sidebar_thumbnails,
81 G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
82 ev_sidebar_thumbnails_page_iface_init))
84 #define EV_SIDEBAR_THUMBNAILS_GET_PRIVATE(object) \
85 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_THUMBNAILS, EvSidebarThumbnailsPrivate));
89 ev_sidebar_thumbnails_dispose (GObject *object)
91 EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (object);
93 ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
94 g_object_unref (sidebar_thumbnails->priv->loading_icon);
96 G_OBJECT_CLASS (ev_sidebar_thumbnails_parent_class)->dispose (object);
100 ev_sidebar_thumbnails_class_init (EvSidebarThumbnailsClass *ev_sidebar_thumbnails_class)
102 GObjectClass *g_object_class;
103 GtkObjectClass *gtk_object_class;
105 g_object_class = G_OBJECT_CLASS (ev_sidebar_thumbnails_class);
106 gtk_object_class = GTK_OBJECT_CLASS (ev_sidebar_thumbnails_class);
108 g_object_class->dispose = ev_sidebar_thumbnails_dispose;
110 g_type_class_add_private (g_object_class, sizeof (EvSidebarThumbnailsPrivate));
114 ev_sidebar_thumbnails_new (void)
116 GtkWidget *ev_sidebar_thumbnails;
118 ev_sidebar_thumbnails = g_object_new (EV_TYPE_SIDEBAR_THUMBNAILS, NULL);
120 return ev_sidebar_thumbnails;
124 clear_range (EvSidebarThumbnails *sidebar_thumbnails,
128 EvSidebarThumbnailsPrivate *priv;
133 priv = sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (sidebar_thumbnails);
135 g_assert (start_page <= end_page);
137 path = gtk_tree_path_new_from_indices (start_page, -1);
138 for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
139 result && start_page <= end_page;
140 result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), start_page ++) {
143 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store),
149 g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, sidebar_thumbnails);
150 ev_job_queue_remove_job (EV_JOB (job));
151 g_object_unref (job);
154 gtk_list_store_set (priv->list_store, &iter,
156 COLUMN_THUMBNAIL_SET, FALSE,
157 COLUMN_PIXBUF, priv->loading_icon,
160 gtk_tree_path_free (path);
164 add_range (EvSidebarThumbnails *sidebar_thumbnails,
168 EvSidebarThumbnailsPrivate *priv;
172 gint page = start_page;
174 priv = sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (sidebar_thumbnails);
176 g_assert (start_page <= end_page);
178 path = gtk_tree_path_new_from_indices (start_page, -1);
179 for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
180 result && page <= end_page;
181 result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), page ++) {
183 gboolean thumbnail_set;
185 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store), &iter,
187 COLUMN_THUMBNAIL_SET, &thumbnail_set,
190 if (job == NULL && !thumbnail_set) {
191 job = (EvJobThumbnail *)ev_job_thumbnail_new (priv->document, page, THUMBNAIL_WIDTH);
192 ev_job_queue_add_job (EV_JOB (job), EV_JOB_PRIORITY_HIGH);
193 g_object_set_data_full (G_OBJECT (job), "tree_iter",
194 gtk_tree_iter_copy (&iter),
195 (GDestroyNotify) gtk_tree_iter_free);
196 g_signal_connect (job, "finished",
197 G_CALLBACK (thumbnail_job_completed_callback),
199 gtk_list_store_set (priv->list_store, &iter,
202 /* The queue and the list own a ref to the job now */
203 g_object_unref (job);
205 g_object_unref (job);
208 gtk_tree_path_free (path);
211 /* This modifies start */
213 update_visible_range (EvSidebarThumbnails *sidebar_thumbnails,
217 EvSidebarThumbnailsPrivate *priv;
218 int old_start_page, old_end_page;
220 priv = sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (sidebar_thumbnails);
222 old_start_page = priv->start_page;
223 old_end_page = priv->end_page;
225 if (start_page == old_start_page &&
226 end_page == old_end_page)
229 /* Clear the areas we no longer display */
230 if (old_start_page < start_page)
231 clear_range (sidebar_thumbnails, old_start_page, MIN (start_page - 1, old_end_page));
233 if (old_end_page > end_page)
234 clear_range (sidebar_thumbnails, MAX (end_page + 1, old_start_page), old_end_page);
236 add_range (sidebar_thumbnails, start_page, end_page);
238 priv->start_page = start_page;
239 priv->end_page = end_page;
243 adjustment_changed_cb (EvSidebarThumbnails *sidebar_thumbnails)
245 EvSidebarThumbnailsPrivate *priv;
251 priv = sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (sidebar_thumbnails);
253 if (! GTK_WIDGET_REALIZED (priv->tree_view))
256 gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
257 0, (int) priv->vadjustment->value,
259 gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
260 0, (int) (priv->vadjustment->value + priv->vadjustment->page_size),
262 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
265 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
269 path = gtk_tree_path_new_first ();
271 path2 = gtk_tree_path_new_from_indices (priv->n_pages,
273 update_visible_range (sidebar_thumbnails,
274 gtk_tree_path_get_indices (path)[0],
275 gtk_tree_path_get_indices (path2)[0]);
277 gtk_tree_path_free (path);
278 gtk_tree_path_free (path2);
282 ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
283 EvSidebarThumbnails *ev_sidebar_thumbnails)
285 EvSidebarThumbnailsPrivate *priv;
286 EvPageCache *page_cache;
291 priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
293 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
296 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
298 page = gtk_tree_path_get_indices (path)[0];
299 gtk_tree_path_free (path);
301 page_cache = ev_document_get_page_cache (priv->document);
302 ev_page_cache_set_current_page (page_cache, page);
306 ev_sidebar_thumbnails_get_treeview (EvSidebarThumbnails *sidebar)
308 return sidebar->priv->tree_view;
312 ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
315 EvSidebarThumbnailsPrivate *priv;
316 GtkCellRenderer *renderer;
317 GtkTreeSelection *selection;
319 priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
321 priv->list_store = gtk_list_store_new (NUM_COLUMNS, G_TYPE_STRING, GDK_TYPE_PIXBUF, G_TYPE_BOOLEAN, EV_TYPE_JOB_THUMBNAIL);
322 priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
324 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
325 g_signal_connect (selection, "changed",
326 G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
327 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
328 renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
332 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
336 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
337 NULL, gtk_cell_renderer_text_new (),
340 g_object_unref (priv->list_store);
342 swindow = gtk_scrolled_window_new (NULL, NULL);
343 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
344 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
345 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
347 priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (swindow));
348 g_signal_connect_data (G_OBJECT (priv->vadjustment), "value-changed",
349 G_CALLBACK (adjustment_changed_cb),
350 ev_sidebar_thumbnails, NULL,
351 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
352 g_signal_connect_swapped (G_OBJECT (swindow), "size-allocate",
353 G_CALLBACK (adjustment_changed_cb),
354 ev_sidebar_thumbnails);
355 gtk_container_add (GTK_CONTAINER (swindow), priv->tree_view);
356 gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), swindow, TRUE, TRUE, 0);
358 gtk_widget_show_all (swindow);
362 page_changed_cb (EvPageCache *page_cache,
364 EvSidebarThumbnails *sidebar)
366 GtkTreeView *tree_view = GTK_TREE_VIEW (sidebar->priv->tree_view);
369 path = gtk_tree_path_new_from_indices (page, -1);
370 gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
371 gtk_tree_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.0, 0.0);
372 gtk_tree_path_free (path);
376 thumbnail_job_completed_callback (EvJobThumbnail *job,
377 EvSidebarThumbnails *sidebar_thumbnails)
379 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
382 iter = (GtkTreeIter *) g_object_get_data (G_OBJECT (job), "tree_iter");
383 gtk_list_store_set (priv->list_store,
385 COLUMN_PIXBUF, job->thumbnail,
386 COLUMN_THUMBNAIL_SET, TRUE,
392 ev_sidebar_thumbnails_set_document (EvSidebarPage *sidebar_page,
393 EvDocument *document)
395 EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (sidebar_page);
398 gint width = THUMBNAIL_WIDTH;
399 gint height = THUMBNAIL_WIDTH;
400 EvPageCache *page_cache;
402 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
404 g_return_if_fail (EV_IS_DOCUMENT_THUMBNAILS (document));
406 page_cache = ev_document_get_page_cache (document);
407 n_pages = ev_page_cache_get_n_pages (page_cache);
409 priv->document = document;
410 priv->n_pages = n_pages;
412 /* We get the dimensions of the first doc so that we can make a blank
414 ev_document_doc_mutex_lock ();
415 ev_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (priv->document),
416 0, THUMBNAIL_WIDTH, &width, &height);
417 ev_document_doc_mutex_unlock ();
419 if (priv->loading_icon)
420 g_object_unref (priv->loading_icon);
421 priv->loading_icon = ev_document_misc_get_thumbnail_frame (width, height, NULL);
423 ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
424 for (i = 0; i < n_pages; i++) {
428 page_label = ev_page_cache_get_page_label (page_cache, i);
429 page_string = g_markup_printf_escaped ("<i>%s</i>", page_label);
431 gtk_list_store_append (priv->list_store, &iter);
432 gtk_list_store_set (priv->list_store, &iter,
433 COLUMN_PAGE_STRING, page_string,
434 COLUMN_PIXBUF, priv->loading_icon,
435 COLUMN_THUMBNAIL_SET, FALSE,
438 g_free (page_string);
441 /* Connect to the signal and trigger a fake callback */
442 g_signal_connect (page_cache, "page-changed", G_CALLBACK (page_changed_cb), sidebar_thumbnails);
443 adjustment_changed_cb (sidebar_thumbnails);
447 ev_sidebar_thumbnails_clear_job (GtkTreeModel *model,
454 gtk_tree_model_get (model, iter, COLUMN_JOB, &job, -1);
457 ev_job_queue_remove_job (job);
458 g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, data);
459 g_object_unref (job);
466 ev_sidebar_thumbnails_clear_model (EvSidebarThumbnails *sidebar_thumbnails)
468 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
470 gtk_tree_model_foreach (GTK_TREE_MODEL (priv->list_store), ev_sidebar_thumbnails_clear_job, sidebar_thumbnails);
471 gtk_list_store_clear (priv->list_store);
475 ev_sidebar_thumbnails_support_document (EvSidebarPage *sidebar_page,
476 EvDocument *document)
478 return (EV_IS_DOCUMENT_THUMBNAILS (document) &&
479 (ev_document_get_n_pages (document) > 1));
483 ev_sidebar_thumbnails_get_label (EvSidebarPage *sidebar_page)
485 return _("Thumbnails");
489 ev_sidebar_thumbnails_page_iface_init (EvSidebarPageIface *iface)
491 iface->support_document = ev_sidebar_thumbnails_support_document;
492 iface->set_document = ev_sidebar_thumbnails_set_document;
493 iface->get_label = ev_sidebar_thumbnails_get_label;