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.
32 #include "ev-sidebar-thumbnails.h"
33 #include "ev-document-thumbnails.h"
34 #include "ev-document-misc.h"
35 #include "ev-job-queue.h"
36 #include "ev-window.h"
39 #define THUMBNAIL_WIDTH 100
41 /* Amount of time we devote to each iteration of the idle, in microseconds */
42 #define IDLE_WORK_LENGTH 5000
44 struct _EvSidebarThumbnailsPrivate {
46 GtkAdjustment *vadjustment;
47 GtkListStore *list_store;
50 gint n_pages, pages_done;
61 G_DEFINE_TYPE (EvSidebarThumbnails, ev_sidebar_thumbnails, GTK_TYPE_VBOX);
63 #define EV_SIDEBAR_THUMBNAILS_GET_PRIVATE(object) \
64 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_THUMBNAILS, EvSidebarThumbnailsPrivate));
67 ev_sidebar_thumbnails_class_init (EvSidebarThumbnailsClass *ev_sidebar_thumbnails_class)
69 GObjectClass *g_object_class;
70 GtkObjectClass *gtk_object_class;
72 g_object_class = G_OBJECT_CLASS (ev_sidebar_thumbnails_class);
73 gtk_object_class = GTK_OBJECT_CLASS (ev_sidebar_thumbnails_class);
75 g_type_class_add_private (g_object_class, sizeof (EvSidebarThumbnailsPrivate));
79 adjustment_changed_cb (GtkAdjustment *adjustment,
80 EvSidebarThumbnails *ev_sidebar_thumbnails)
82 EvSidebarThumbnailsPrivate *priv;
86 gboolean thumbnail_set;
88 priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
90 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
96 page = gtk_tree_path_get_indices (path)[0];
97 gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store),
99 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store), &iter,
100 COLUMN_THUMBNAIL_SET, &thumbnail_set,
105 ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
106 EvSidebarThumbnails *ev_sidebar_thumbnails)
108 EvSidebarThumbnailsPrivate *priv;
109 EvPageCache *page_cache;
114 priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
116 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
119 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
121 page = gtk_tree_path_get_indices (path)[0];
122 gtk_tree_path_free (path);
124 page_cache = ev_document_get_page_cache (priv->document);
125 ev_page_cache_set_current_page (page_cache, page);
129 ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
132 EvSidebarThumbnailsPrivate *priv;
133 GtkCellRenderer *renderer;
134 GtkTreeSelection *selection;
136 priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
138 priv->list_store = gtk_list_store_new (NUM_COLUMNS, G_TYPE_STRING, GDK_TYPE_PIXBUF, G_TYPE_BOOLEAN, EV_TYPE_JOB_THUMBNAIL);
139 priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
141 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
142 g_signal_connect (selection, "changed",
143 G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
144 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
145 renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
149 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
153 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
154 NULL, gtk_cell_renderer_text_new (),
157 g_object_unref (priv->list_store);
159 swindow = gtk_scrolled_window_new (NULL, NULL);
160 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
161 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
162 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
164 priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (swindow));
165 g_signal_connect (G_OBJECT (priv->vadjustment), "value-changed",
166 G_CALLBACK (adjustment_changed_cb),
167 ev_sidebar_thumbnails);
168 gtk_container_add (GTK_CONTAINER (swindow), priv->tree_view);
169 gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), swindow, TRUE, TRUE, 0);
171 gtk_widget_show_all (swindow);
175 ev_sidebar_thumbnails_new (void)
177 GtkWidget *ev_sidebar_thumbnails;
179 ev_sidebar_thumbnails = g_object_new (EV_TYPE_SIDEBAR_THUMBNAILS, NULL);
181 return ev_sidebar_thumbnails;
185 page_changed_cb (EvPageCache *page_cache,
187 EvSidebarThumbnails *sidebar)
190 GtkTreeSelection *selection;
192 path = gtk_tree_path_new_from_indices (page, -1);
193 selection = gtk_tree_view_get_selection
194 (GTK_TREE_VIEW (sidebar->priv->tree_view));
196 gtk_tree_selection_select_path (selection, path);
197 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (sidebar->priv->tree_view),
198 path, NULL, FALSE, 0.0, 0.0);
199 gtk_tree_path_free (path);
203 thumbnail_job_completed_callback (EvJobThumbnail *job,
204 EvSidebarThumbnails *sidebar_thumbnails)
206 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
209 iter = (GtkTreeIter *) g_object_get_data (G_OBJECT (job), "tree_iter");
210 gtk_list_store_set (priv->list_store,
212 COLUMN_PIXBUF, job->thumbnail,
213 COLUMN_THUMBNAIL_SET, TRUE,
219 ev_sidebar_thumbnails_set_document (EvSidebarThumbnails *sidebar_thumbnails,
220 EvDocument *document)
222 GdkPixbuf *loading_icon;
226 gint width = THUMBNAIL_WIDTH;
227 gint height = THUMBNAIL_WIDTH;
228 EvPageCache *page_cache;
230 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
232 g_return_if_fail (EV_IS_DOCUMENT_THUMBNAILS (document));
234 page_cache = ev_document_get_page_cache (document);
235 n_pages = ev_page_cache_get_n_pages (page_cache);
237 priv->document = document;
238 priv->n_pages = n_pages;
240 /* We get the dimensions of the first doc so that we can make a blank
242 g_mutex_lock (EV_DOC_MUTEX);
243 ev_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (priv->document),
244 1, THUMBNAIL_WIDTH, &width, &height);
245 g_mutex_unlock (EV_DOC_MUTEX);
247 loading_icon = ev_document_misc_get_thumbnail_frame (width, height, NULL);
249 gtk_list_store_clear (priv->list_store);
250 for (i = 0; i < n_pages; i++) {
253 /* FIXME: Bah. This is still -1 for some reason. Need to track it down.. */
254 job = ev_job_thumbnail_new (priv->document, i, THUMBNAIL_WIDTH);
255 page = g_strdup_printf ("<i>%d</i>", i + 1); /* FIXME: replace with string. */
256 gtk_list_store_append (priv->list_store, &iter);
257 gtk_list_store_set (priv->list_store, &iter,
258 COLUMN_PAGE_STRING, page,
259 COLUMN_PIXBUF, loading_icon,
260 COLUMN_THUMBNAIL_SET, FALSE,
264 ev_job_queue_add_job (job, EV_JOB_PRIORITY_LOW);
265 g_object_set_data_full (G_OBJECT (job), "tree_iter",
266 gtk_tree_iter_copy (&iter),
267 (GDestroyNotify) gtk_tree_iter_free);
268 g_signal_connect (job, "finished",
269 G_CALLBACK (thumbnail_job_completed_callback),
273 g_object_unref (loading_icon);
275 /* Connect to the signal and trigger a fake callback */
276 g_signal_connect (page_cache, "page-changed", G_CALLBACK (page_changed_cb), sidebar_thumbnails);
277 page_changed_cb (page_cache, ev_page_cache_get_current_page (page_cache), sidebar_thumbnails);