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 G_DEFINE_TYPE (EvSidebarThumbnails, ev_sidebar_thumbnails, GTK_TYPE_VBOX);
62 #define EV_SIDEBAR_THUMBNAILS_GET_PRIVATE(object) \
63 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_THUMBNAILS, EvSidebarThumbnailsPrivate));
66 ev_sidebar_thumbnails_destroy (GtkObject *object)
68 EvSidebarThumbnails *ev_sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (object);
69 EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
71 if (priv->idle_id != 0) {
72 g_source_remove (priv->idle_id);
77 GTK_OBJECT_CLASS (ev_sidebar_thumbnails_parent_class)->destroy (object);
81 ev_sidebar_thumbnails_class_init (EvSidebarThumbnailsClass *ev_sidebar_thumbnails_class)
83 GObjectClass *g_object_class;
84 GtkObjectClass *gtk_object_class;
86 g_object_class = G_OBJECT_CLASS (ev_sidebar_thumbnails_class);
87 gtk_object_class = GTK_OBJECT_CLASS (ev_sidebar_thumbnails_class);
89 gtk_object_class->destroy = ev_sidebar_thumbnails_destroy;
91 g_type_class_add_private (g_object_class, sizeof (EvSidebarThumbnailsPrivate));
96 adjustment_changed_cb (GtkAdjustment *adjustment,
97 EvSidebarThumbnails *ev_sidebar_thumbnails)
99 EvSidebarThumbnailsPrivate *priv;
103 gboolean thumbnail_set;
105 priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
107 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
113 page = gtk_tree_path_get_indices (path)[0];
114 if (page == priv->current_page)
116 gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store),
118 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store), &iter,
119 COLUMN_THUMBNAIL_SET, &thumbnail_set,
121 if (! thumbnail_set) {
122 priv->current_page = page;
123 priv->current_page_iter = iter;
129 ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
130 EvSidebarThumbnails *ev_sidebar_thumbnails)
132 EvSidebarThumbnailsPrivate *priv;
138 priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
140 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
143 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
146 page = gtk_tree_path_get_indices (path)[0] + 1;
148 gtk_tree_path_free (path);
150 window = gtk_widget_get_ancestor (GTK_WIDGET (ev_sidebar_thumbnails),
152 if (window && ev_document_get_page (priv->document) != page) {
153 ev_window_open_page (EV_WINDOW (window), page);
158 ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
161 EvSidebarThumbnailsPrivate *priv;
162 GtkCellRenderer *renderer;
163 GtkTreeSelection *selection;
165 priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
167 priv->list_store = gtk_list_store_new (NUM_COLUMNS, G_TYPE_STRING, GDK_TYPE_PIXBUF, G_TYPE_BOOLEAN);
168 priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
170 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
171 g_signal_connect (selection, "changed",
172 G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
173 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
174 renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
178 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
182 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
183 NULL, gtk_cell_renderer_text_new (),
186 g_object_unref (priv->list_store);
188 swindow = gtk_scrolled_window_new (NULL, NULL);
189 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
190 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
191 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
193 priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (swindow));
194 g_signal_connect (G_OBJECT (priv->vadjustment), "value-changed",
195 G_CALLBACK (adjustment_changed_cb),
196 ev_sidebar_thumbnails);
197 gtk_container_add (GTK_CONTAINER (swindow), priv->tree_view);
198 gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), swindow, TRUE, TRUE, 0);
200 gtk_widget_show_all (swindow);
204 ev_sidebar_thumbnails_new (void)
206 GtkWidget *ev_sidebar_thumbnails;
208 ev_sidebar_thumbnails = g_object_new (EV_TYPE_SIDEBAR_THUMBNAILS, NULL);
210 return ev_sidebar_thumbnails;
214 do_one_iteration (EvSidebarThumbnails *ev_sidebar_thumbnails)
216 EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
218 gboolean thumbnail_set;
220 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store),
221 &(priv->current_page_iter),
222 COLUMN_THUMBNAIL_SET, &thumbnail_set,
224 if (!thumbnail_set) {
225 pixbuf = ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (priv->document),
226 priv->current_page, THUMBNAIL_WIDTH);
228 gtk_list_store_set (priv->list_store,
229 &(priv->current_page_iter),
230 COLUMN_PIXBUF, pixbuf,
233 g_object_unref (pixbuf);
237 priv->current_page++;
239 if (priv->current_page == priv->n_pages) {
240 priv->current_page = 0;
241 gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->list_store),
242 &(priv->current_page_iter));
244 gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store),
245 &(priv->current_page_iter));
248 if (priv->pages_done == priv->n_pages)
255 populate_thumbnails_idle (gpointer data)
259 gdouble time_elapsed = 0;
261 EvSidebarThumbnails *ev_sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (data);
262 EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
264 #if PROFILE_THUMB == 1
265 static GTimer *total_timer;
266 static gboolean first_time = TRUE;
269 total_timer = g_timer_new ();
271 g_timer_start (total_timer);
275 /* undo the thumbnailing idle and handler */
276 if (priv->pages_done == priv->n_pages) {
278 g_signal_handlers_disconnect_by_func (priv->vadjustment,
279 adjustment_changed_cb,
280 ev_sidebar_thumbnails);
281 #if PROFILE_THUMB == 1
282 time_elapsed = g_timer_elapsed (total_timer, NULL);
283 g_timer_destroy (total_timer);
284 g_print ("%d rows done in %f seconds\n",
285 gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->list_store), NULL),
291 timer = g_timer_new ();
293 g_timer_start (timer);
294 while (do_one_iteration (ev_sidebar_thumbnails)) {
296 time_elapsed = g_timer_elapsed (timer, NULL);
297 if (time_elapsed > IDLE_WORK_LENGTH/1000000)
300 g_timer_destroy (timer);
301 #if PROFILE_THUMB == 2
302 g_print ("%d rows done this idle in %f seconds\n", i, time_elapsed);
309 ev_sidebar_thumbnails_select_page (EvSidebarThumbnails *sidebar,
313 GtkTreeSelection *selection;
315 /* if the EvSidebar's document can't provide thumbnails */
316 if (sidebar->priv->document == NULL)
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_view_scroll_to_cell (GTK_TREE_VIEW (sidebar->priv->tree_view),
326 path, NULL, FALSE, 0.0, 0.0);
327 gtk_tree_path_free (path);
333 ev_sidebar_thumbnails_set_document (EvSidebarThumbnails *sidebar_thumbnails,
334 EvDocument *document)
336 GdkPixbuf *loading_icon;
340 gint width = THUMBNAIL_WIDTH;
341 gint height = THUMBNAIL_WIDTH;
343 EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
345 g_return_if_fail (EV_IS_DOCUMENT_THUMBNAILS (document));
347 if (priv->idle_id != 0) {
348 g_source_remove (priv->idle_id);
350 n_pages = ev_document_get_n_pages (document);
352 priv->document = document;
353 priv->idle_id = g_idle_add (populate_thumbnails_idle, sidebar_thumbnails);
354 priv->n_pages = n_pages;
356 /* We get the dimensions of the first doc so that we can make a blank
358 ev_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (priv->document),
359 1, THUMBNAIL_WIDTH, &width, &height);
360 loading_icon = ev_document_misc_get_thumbnail_frame (width, height, NULL);
362 for (i = 1; i <= n_pages; i++) {
363 page = g_strdup_printf ("<i>%d</i>", i);
364 gtk_list_store_append (priv->list_store, &iter);
365 gtk_list_store_set (priv->list_store, &iter,
366 COLUMN_PAGE_STRING, page,
367 COLUMN_PIXBUF, loading_icon,
368 COLUMN_THUMBNAIL_SET, FALSE,
373 g_object_unref (loading_icon);
374 gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->list_store),
375 &(priv->current_page_iter));
376 priv->current_page = 0;
377 priv->pages_done = 0;