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-window.h"
38 #define THUMBNAIL_WIDTH 75
39 /* Amount of time we devote to each iteration of the idle, in microseconds */
40 #define IDLE_WORK_LENGTH 5000
42 struct _EvSidebarThumbnailsPrivate {
44 GtkAdjustment *vadjustment;
45 GtkListStore *list_store;
49 gint current_page, n_pages, pages_done;
50 GtkTreeIter current_page_iter;
60 static GtkVBoxClass *parent_class;
62 G_DEFINE_TYPE (EvSidebarThumbnails, ev_sidebar_thumbnails, GTK_TYPE_VBOX);
64 #define EV_SIDEBAR_THUMBNAILS_GET_PRIVATE(object) \
65 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_THUMBNAILS, EvSidebarThumbnailsPrivate));
68 ev_sidebar_thumbnails_destroy (GtkObject *object)
70 EvSidebarThumbnails *ev_sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (object);
71 EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
73 if (priv->idle_id != 0) {
74 g_source_remove (priv->idle_id);
79 GTK_OBJECT_CLASS (parent_class)->destroy (object);
83 ev_sidebar_thumbnails_class_init (EvSidebarThumbnailsClass *ev_sidebar_thumbnails_class)
85 GObjectClass *g_object_class;
86 GtkObjectClass *gtk_object_class;
88 g_object_class = G_OBJECT_CLASS (ev_sidebar_thumbnails_class);
89 gtk_object_class = GTK_OBJECT_CLASS (ev_sidebar_thumbnails_class);
91 parent_class = g_type_class_peek_parent (ev_sidebar_thumbnails_class);
93 gtk_object_class->destroy = ev_sidebar_thumbnails_destroy;
95 g_type_class_add_private (g_object_class, sizeof (EvSidebarThumbnailsPrivate));
100 adjustment_changed_cb (GtkAdjustment *adjustment,
101 EvSidebarThumbnails *ev_sidebar_thumbnails)
103 EvSidebarThumbnailsPrivate *priv;
107 gboolean thumbnail_set;
109 priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
111 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
117 page = gtk_tree_path_get_indices (path)[0];
118 if (page == priv->current_page)
120 gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store),
122 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store), &iter,
123 COLUMN_THUMBNAIL_SET, &thumbnail_set,
125 if (! thumbnail_set) {
126 priv->current_page = page;
127 priv->current_page_iter = iter;
133 ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
134 EvSidebarThumbnails *ev_sidebar_thumbnails)
136 EvSidebarThumbnailsPrivate *priv;
142 priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
144 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
147 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
150 page = gtk_tree_path_get_indices (path)[0] + 1;
152 gtk_tree_path_free (path);
154 window = gtk_widget_get_ancestor (GTK_WIDGET (ev_sidebar_thumbnails),
156 if (window && ev_document_get_page (priv->document) != page) {
157 ev_window_open_page (EV_WINDOW (window), page);
162 ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
165 EvSidebarThumbnailsPrivate *priv;
166 GtkCellRenderer *renderer;
167 GtkTreeSelection *selection;
169 priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
171 priv->list_store = gtk_list_store_new (NUM_COLUMNS, G_TYPE_STRING, GDK_TYPE_PIXBUF, G_TYPE_BOOLEAN);
172 priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
174 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
175 g_signal_connect (selection, "changed",
176 G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
177 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
178 renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
182 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
186 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
187 NULL, gtk_cell_renderer_text_new (),
190 g_object_unref (priv->list_store);
192 swindow = gtk_scrolled_window_new (NULL, NULL);
193 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
194 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
195 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
197 priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (swindow));
198 g_signal_connect (G_OBJECT (priv->vadjustment), "value-changed",
199 G_CALLBACK (adjustment_changed_cb),
200 ev_sidebar_thumbnails);
201 gtk_container_add (GTK_CONTAINER (swindow), priv->tree_view);
202 gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), swindow, TRUE, TRUE, 0);
204 gtk_widget_show_all (swindow);
208 ev_sidebar_thumbnails_new (void)
210 GtkWidget *ev_sidebar_thumbnails;
212 ev_sidebar_thumbnails = g_object_new (EV_TYPE_SIDEBAR_THUMBNAILS, NULL);
214 return ev_sidebar_thumbnails;
218 do_one_iteration (EvSidebarThumbnails *ev_sidebar_thumbnails)
220 EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
222 gboolean thumbnail_set;
224 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store),
225 &(priv->current_page_iter),
226 COLUMN_THUMBNAIL_SET, &thumbnail_set,
228 if (!thumbnail_set) {
229 pixbuf = ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (priv->document),
230 priv->current_page, THUMBNAIL_WIDTH);
232 gtk_list_store_set (priv->list_store,
233 &(priv->current_page_iter),
234 COLUMN_PIXBUF, pixbuf,
237 g_object_unref (pixbuf);
241 priv->current_page++;
243 if (priv->current_page == priv->n_pages) {
244 priv->current_page = 0;
245 gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->list_store),
246 &(priv->current_page_iter));
248 gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store),
249 &(priv->current_page_iter));
252 if (priv->pages_done == priv->n_pages)
259 populate_thumbnails_idle (gpointer data)
263 gdouble time_elapsed = 0;
265 EvSidebarThumbnails *ev_sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (data);
266 EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
268 #if PROFILE_THUMB == 1
269 static GTimer *total_timer;
270 static gboolean first_time = TRUE;
273 total_timer = g_timer_new ();
275 g_timer_start (total_timer);
279 /* undo the thumbnailing idle and handler */
280 if (priv->pages_done == priv->n_pages) {
282 g_signal_handlers_disconnect_by_func (priv->vadjustment,
283 adjustment_changed_cb,
284 ev_sidebar_thumbnails);
285 #if PROFILE_THUMB == 1
286 time_elapsed = g_timer_elapsed (total_timer, NULL);
287 g_timer_destroy (total_timer);
288 g_print ("%d rows done in %f seconds\n",
289 gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->list_store), NULL),
295 timer = g_timer_new ();
297 g_timer_start (timer);
298 while (do_one_iteration (ev_sidebar_thumbnails)) {
300 time_elapsed = g_timer_elapsed (timer, NULL);
301 if (time_elapsed > IDLE_WORK_LENGTH/1000000)
304 g_timer_destroy (timer);
305 #if PROFILE_THUMB == 2
306 g_print ("%d rows done this idle in %f seconds\n", i, time_elapsed);
313 ev_sidebar_thumbnails_select_page (EvSidebarThumbnails *sidebar,
317 GtkTreeSelection *selection;
319 path = gtk_tree_path_new_from_indices (page - 1, -1);
320 selection = gtk_tree_view_get_selection
321 (GTK_TREE_VIEW (sidebar->priv->tree_view));
324 gtk_tree_selection_select_path (selection, path);
325 gtk_tree_path_free (path);
331 ev_sidebar_thumbnails_set_document (EvSidebarThumbnails *sidebar_thumbnails,
332 EvDocument *document)
334 GdkPixbuf *loading_icon;
338 gint width = THUMBNAIL_WIDTH;
339 gint height = THUMBNAIL_WIDTH;
341 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
343 g_return_if_fail (EV_IS_DOCUMENT_THUMBNAILS (document));
345 if (priv->idle_id != 0) {
346 g_source_remove (priv->idle_id);
348 n_pages = ev_document_get_n_pages (document);
350 priv->document = document;
351 priv->idle_id = g_idle_add (populate_thumbnails_idle, sidebar_thumbnails);
352 priv->n_pages = n_pages;
354 /* We get the dimensions of the first doc so that we can make a blank
356 ev_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (priv->document),
357 0, THUMBNAIL_WIDTH, &width, &height);
358 loading_icon = ev_document_misc_get_thumbnail_frame (width, height, NULL);
360 for (i = 0; i < n_pages; i++) {
361 page = g_strdup_printf ("<i>%d</i>", i + 1);
362 gtk_list_store_append (priv->list_store, &iter);
363 gtk_list_store_set (priv->list_store, &iter,
364 COLUMN_PAGE_STRING, page,
365 COLUMN_PIXBUF, loading_icon,
366 COLUMN_THUMBNAIL_SET, FALSE,
371 g_object_unref (loading_icon);
372 gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->list_store),
373 &(priv->current_page_iter));
374 priv->current_page = 0;
375 priv->pages_done = 0;