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;
52 gint n_pages, pages_done;
63 static void ev_sidebar_thumbnails_clear_model (EvSidebarThumbnails *sidebar);
64 static gboolean ev_sidebar_thumbnails_support_document (EvSidebarPage *sidebar_page,
65 EvDocument *document);
66 static void ev_sidebar_thumbnails_page_iface_init (EvSidebarPageIface *iface);
67 static void ev_sidebar_thumbnails_set_document (EvSidebarPage *sidebar_page,
68 EvDocument *document);
69 static const gchar* ev_sidebar_thumbnails_get_label (EvSidebarPage *sidebar_page);
71 G_DEFINE_TYPE_EXTENDED (EvSidebarThumbnails,
72 ev_sidebar_thumbnails,
75 G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
76 ev_sidebar_thumbnails_page_iface_init))
78 #define EV_SIDEBAR_THUMBNAILS_GET_PRIVATE(object) \
79 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_THUMBNAILS, EvSidebarThumbnailsPrivate));
83 ev_sidebar_thumbnails_dispose (GObject *object)
85 EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (object);
87 ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
89 G_OBJECT_CLASS (ev_sidebar_thumbnails_parent_class)->dispose (object);
93 ev_sidebar_thumbnails_class_init (EvSidebarThumbnailsClass *ev_sidebar_thumbnails_class)
95 GObjectClass *g_object_class;
96 GtkObjectClass *gtk_object_class;
98 g_object_class = G_OBJECT_CLASS (ev_sidebar_thumbnails_class);
99 gtk_object_class = GTK_OBJECT_CLASS (ev_sidebar_thumbnails_class);
101 g_object_class->dispose = ev_sidebar_thumbnails_dispose;
103 g_type_class_add_private (g_object_class, sizeof (EvSidebarThumbnailsPrivate));
107 ev_sidebar_thumbnails_new (void)
109 GtkWidget *ev_sidebar_thumbnails;
111 ev_sidebar_thumbnails = g_object_new (EV_TYPE_SIDEBAR_THUMBNAILS, NULL);
113 return ev_sidebar_thumbnails;
117 adjustment_changed_cb (GtkAdjustment *adjustment,
118 EvSidebarThumbnails *ev_sidebar_thumbnails)
120 EvSidebarThumbnailsPrivate *priv;
124 gboolean thumbnail_set;
126 priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
128 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
134 page = gtk_tree_path_get_indices (path)[0];
135 gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store),
137 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store), &iter,
138 COLUMN_THUMBNAIL_SET, &thumbnail_set,
143 ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
144 EvSidebarThumbnails *ev_sidebar_thumbnails)
146 EvSidebarThumbnailsPrivate *priv;
147 EvPageCache *page_cache;
152 priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
154 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
157 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
159 page = gtk_tree_path_get_indices (path)[0];
160 gtk_tree_path_free (path);
162 page_cache = ev_document_get_page_cache (priv->document);
163 ev_page_cache_set_current_page (page_cache, page);
167 ev_sidebar_thumbnails_get_treeview (EvSidebarThumbnails *sidebar)
169 return sidebar->priv->tree_view;
173 ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
176 EvSidebarThumbnailsPrivate *priv;
177 GtkCellRenderer *renderer;
178 GtkTreeSelection *selection;
180 priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
182 priv->list_store = gtk_list_store_new (NUM_COLUMNS, G_TYPE_STRING, GDK_TYPE_PIXBUF, G_TYPE_BOOLEAN, EV_TYPE_JOB_THUMBNAIL);
183 priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
185 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
186 g_signal_connect (selection, "changed",
187 G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
188 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
189 renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
193 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
197 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
198 NULL, gtk_cell_renderer_text_new (),
201 g_object_unref (priv->list_store);
203 swindow = gtk_scrolled_window_new (NULL, NULL);
204 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
205 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
206 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
208 priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (swindow));
209 g_signal_connect (G_OBJECT (priv->vadjustment), "value-changed",
210 G_CALLBACK (adjustment_changed_cb),
211 ev_sidebar_thumbnails);
212 gtk_container_add (GTK_CONTAINER (swindow), priv->tree_view);
213 gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), swindow, TRUE, TRUE, 0);
215 gtk_widget_show_all (swindow);
219 page_changed_cb (EvPageCache *page_cache,
221 EvSidebarThumbnails *sidebar)
223 GtkTreeView *tree_view = GTK_TREE_VIEW (sidebar->priv->tree_view);
226 path = gtk_tree_path_new_from_indices (page, -1);
227 gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
228 gtk_tree_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.0, 0.0);
229 gtk_tree_path_free (path);
233 thumbnail_job_completed_callback (EvJobThumbnail *job,
234 EvSidebarThumbnails *sidebar_thumbnails)
236 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
239 iter = (GtkTreeIter *) g_object_get_data (G_OBJECT (job), "tree_iter");
240 gtk_list_store_set (priv->list_store,
242 COLUMN_PIXBUF, job->thumbnail,
243 COLUMN_THUMBNAIL_SET, TRUE,
249 ev_sidebar_thumbnails_set_document (EvSidebarPage *sidebar_page,
250 EvDocument *document)
252 EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (sidebar_page);
253 GdkPixbuf *loading_icon;
256 gint width = THUMBNAIL_WIDTH;
257 gint height = THUMBNAIL_WIDTH;
258 EvPageCache *page_cache;
260 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
262 g_return_if_fail (EV_IS_DOCUMENT_THUMBNAILS (document));
264 page_cache = ev_document_get_page_cache (document);
265 n_pages = ev_page_cache_get_n_pages (page_cache);
267 priv->document = document;
268 priv->n_pages = n_pages;
270 /* We get the dimensions of the first doc so that we can make a blank
272 ev_document_doc_mutex_lock ();
273 ev_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (priv->document),
274 0, THUMBNAIL_WIDTH, &width, &height);
275 ev_document_doc_mutex_unlock ();
277 loading_icon = ev_document_misc_get_thumbnail_frame (width, height, NULL);
279 ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
280 for (i = 0; i < n_pages; i++) {
285 job = ev_job_thumbnail_new (priv->document, i, THUMBNAIL_WIDTH);
286 page_label = ev_page_cache_get_page_label (page_cache, i);
287 page_string = g_markup_printf_escaped ("<i>%s</i>", page_label);
289 gtk_list_store_append (priv->list_store, &iter);
290 gtk_list_store_set (priv->list_store, &iter,
291 COLUMN_PAGE_STRING, page_string,
292 COLUMN_PIXBUF, loading_icon,
293 COLUMN_THUMBNAIL_SET, FALSE,
297 g_free (page_string);
298 ev_job_queue_add_job (job, EV_JOB_PRIORITY_LOW);
299 g_object_set_data_full (G_OBJECT (job), "tree_iter",
300 gtk_tree_iter_copy (&iter),
301 (GDestroyNotify) gtk_tree_iter_free);
302 g_signal_connect (job, "finished",
303 G_CALLBACK (thumbnail_job_completed_callback),
307 g_object_unref (loading_icon);
309 /* Connect to the signal and trigger a fake callback */
310 g_signal_connect (page_cache, "page-changed", G_CALLBACK (page_changed_cb), sidebar_thumbnails);
311 page_changed_cb (page_cache, ev_page_cache_get_current_page (page_cache), sidebar_thumbnails);
316 ev_sidebar_thumbnails_clear_job (GtkTreeModel *model,
323 gtk_tree_model_get (model, iter, COLUMN_JOB, &job, -1);
326 ev_job_queue_remove_job (job);
327 g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, data);
328 g_object_unref (job);
335 ev_sidebar_thumbnails_clear_model (EvSidebarThumbnails *sidebar_thumbnails)
337 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
339 gtk_tree_model_foreach (GTK_TREE_MODEL (priv->list_store), ev_sidebar_thumbnails_clear_job, sidebar_thumbnails);
340 gtk_list_store_clear (priv->list_store);
344 ev_sidebar_thumbnails_support_document (EvSidebarPage *sidebar_page,
345 EvDocument *document)
347 return (EV_IS_DOCUMENT_THUMBNAILS (document) &&
348 (ev_document_get_n_pages (document) > 1));
352 ev_sidebar_thumbnails_get_label (EvSidebarPage *sidebar_page)
354 return _("Thumbnails");
358 ev_sidebar_thumbnails_page_iface_init (EvSidebarPageIface *iface)
360 iface->support_document = ev_sidebar_thumbnails_support_document;
361 iface->set_document = ev_sidebar_thumbnails_set_document;
362 iface->get_label = ev_sidebar_thumbnails_get_label;