]> www.fi.muni.cz Git - evince.git/blob - shell/ev-sidebar-thumbnails.c
9c0e21728d7ec3abc5390c92059dac50cba23691
[evince.git] / shell / ev-sidebar-thumbnails.c
1 /* this file is part of evince, a gnome document viewer
2  *
3  *  Copyright (C) 2004 Red Hat, Inc.
4  *  Copyright (C) 2004, 2005 Anders Carlsson <andersca@gnome.org>
5  *
6  *  Authors:
7  *    Jonathan Blandford <jrb@alum.mit.edu>
8  *    Anders Carlsson <andersca@gnome.org>
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <string.h>
30
31 #include <glib/gi18n.h>
32 #include <gtk/gtk.h>
33
34 #include "ev-document-misc.h"
35 #include "ev-document-thumbnails.h"
36 #include "ev-job-scheduler.h"
37 #include "ev-sidebar-page.h"
38 #include "ev-sidebar-thumbnails.h"
39 #include "ev-utils.h"
40 #include "ev-window.h"
41
42 #define THUMBNAIL_WIDTH 100
43
44 /* The IconView doesn't scale nearly as well as the TreeView, so we arbitrarily
45  * limit its use */
46 #define MAX_ICON_VIEW_PAGE_COUNT 1500
47
48
49 struct _EvSidebarThumbnailsPrivate {
50         GtkWidget *swindow;
51         GtkWidget *icon_view;
52         GtkWidget *tree_view;
53         GtkAdjustment *vadjustment;
54         GtkListStore *list_store;
55         GHashTable *loading_icons;
56         EvDocument *document;
57         EvPageCache *page_cache;
58
59         gint n_pages, pages_done;
60
61         int rotation;
62
63         /* Visible pages */
64         gint start_page, end_page;
65 };
66
67 enum {
68         COLUMN_PAGE_STRING,
69         COLUMN_PIXBUF,
70         COLUMN_THUMBNAIL_SET,
71         COLUMN_JOB,
72         NUM_COLUMNS
73 };
74
75 enum {
76         PROP_0,
77         PROP_WIDGET,
78 };
79
80 static void         ev_sidebar_thumbnails_clear_model      (EvSidebarThumbnails *sidebar);
81 static gboolean     ev_sidebar_thumbnails_support_document (EvSidebarPage       *sidebar_page,
82                                                             EvDocument          *document);
83 static void         ev_sidebar_thumbnails_page_iface_init  (EvSidebarPageIface  *iface);
84 static void         ev_sidebar_thumbnails_set_document     (EvSidebarPage       *sidebar_page,
85                                                             EvDocument          *document);
86 static const gchar* ev_sidebar_thumbnails_get_label        (EvSidebarPage       *sidebar_page);
87 static void         thumbnail_job_completed_callback       (EvJobThumbnail      *job,
88                                                             EvSidebarThumbnails *sidebar_thumbnails);
89 static void         adjustment_changed_cb                  (EvSidebarThumbnails *sidebar_thumbnails);
90
91 G_DEFINE_TYPE_EXTENDED (EvSidebarThumbnails, 
92                         ev_sidebar_thumbnails, 
93                         GTK_TYPE_VBOX,
94                         0, 
95                         G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE, 
96                                                ev_sidebar_thumbnails_page_iface_init))
97
98 #define EV_SIDEBAR_THUMBNAILS_GET_PRIVATE(object) \
99         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_THUMBNAILS, EvSidebarThumbnailsPrivate));
100
101
102 static void
103 ev_sidebar_thumbnails_dispose (GObject *object)
104 {
105         EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (object);
106         
107         if (sidebar_thumbnails->priv->loading_icons) {
108                 g_hash_table_destroy (sidebar_thumbnails->priv->loading_icons);
109                 sidebar_thumbnails->priv->loading_icons = NULL;
110         }
111         
112         if (sidebar_thumbnails->priv->list_store) {
113                 ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
114                 g_object_unref (sidebar_thumbnails->priv->list_store);
115                 sidebar_thumbnails->priv->list_store = NULL;
116         }
117         
118         G_OBJECT_CLASS (ev_sidebar_thumbnails_parent_class)->dispose (object);
119 }
120
121 static void
122 ev_sidebar_thumbnails_get_property (GObject    *object,
123                                     guint       prop_id,
124                                     GValue     *value,
125                                     GParamSpec *pspec)
126 {
127         EvSidebarThumbnails *sidebar = EV_SIDEBAR_THUMBNAILS (object);
128
129         switch (prop_id) {
130         case PROP_WIDGET:
131                 if (sidebar->priv->tree_view)
132                         g_value_set_object (value, sidebar->priv->tree_view);
133                 else
134                         g_value_set_object (value, sidebar->priv->icon_view);
135                 break;
136         default:
137                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
138                 break;
139         }
140 }
141
142 static void
143 ev_sidebar_thumbnails_map (GtkWidget *widget)
144 {
145         EvSidebarThumbnails *sidebar;
146
147         sidebar = EV_SIDEBAR_THUMBNAILS (widget);
148
149         GTK_WIDGET_CLASS (ev_sidebar_thumbnails_parent_class)->map (widget);
150         
151         adjustment_changed_cb (sidebar);
152 }
153
154 static void
155 ev_sidebar_thumbnails_class_init (EvSidebarThumbnailsClass *ev_sidebar_thumbnails_class)
156 {
157         GObjectClass *g_object_class;
158         GtkObjectClass *gtk_object_class;
159         GtkWidgetClass *widget_class;
160
161         g_object_class = G_OBJECT_CLASS (ev_sidebar_thumbnails_class);
162         gtk_object_class = GTK_OBJECT_CLASS (ev_sidebar_thumbnails_class);
163         widget_class = GTK_WIDGET_CLASS (ev_sidebar_thumbnails_class);
164
165         g_object_class->dispose = ev_sidebar_thumbnails_dispose;
166         g_object_class->get_property = ev_sidebar_thumbnails_get_property;
167         widget_class->map = ev_sidebar_thumbnails_map;
168
169         g_object_class_override_property (g_object_class,
170                                           PROP_WIDGET,
171                                           "main-widget");
172
173         g_type_class_add_private (g_object_class, sizeof (EvSidebarThumbnailsPrivate));
174 }
175
176 GtkWidget *
177 ev_sidebar_thumbnails_new (void)
178 {
179         GtkWidget *ev_sidebar_thumbnails;
180
181         ev_sidebar_thumbnails = g_object_new (EV_TYPE_SIDEBAR_THUMBNAILS, NULL);
182
183         return ev_sidebar_thumbnails;
184 }
185
186 static GdkPixbuf *
187 ev_sidebar_thumbnails_get_loading_icon (EvSidebarThumbnails *sidebar_thumbnails,
188                                         gint                 width,
189                                         gint                 height)
190 {
191         GdkPixbuf *icon;
192         gchar     *key;
193
194         key = g_strdup_printf ("%dx%d", width, height);
195         icon = g_hash_table_lookup (sidebar_thumbnails->priv->loading_icons, key);
196         if (!icon) {
197                 icon = ev_document_misc_get_thumbnail_frame (width, height, NULL);
198                 g_hash_table_insert (sidebar_thumbnails->priv->loading_icons,
199                                      key, icon);
200         } else {
201                 g_free (key);
202         }
203         
204         return icon;
205 }
206
207 static void
208 clear_range (EvSidebarThumbnails *sidebar_thumbnails,
209              gint                 start_page,
210              gint                 end_page)
211 {
212         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
213         GtkTreePath *path;
214         GtkTreeIter iter;
215         gboolean result;
216         gint prev_width = -1;
217         gint prev_height = -1;
218
219         g_assert (start_page <= end_page);
220
221         path = gtk_tree_path_new_from_indices (start_page, -1);
222         for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
223              result && start_page <= end_page;
224              result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), start_page ++) {
225                 EvJobThumbnail *job;
226                 GdkPixbuf *loading_icon = NULL;
227                 gint width, height;
228
229                 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store),
230                                     &iter,
231                                     COLUMN_JOB, &job,
232                                     -1);
233
234                 if (job) {
235                         g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, sidebar_thumbnails);
236                         ev_job_cancel (EV_JOB (job));
237                         g_object_unref (job);
238                 }
239
240                 ev_page_cache_get_thumbnail_size (priv->page_cache, start_page,
241                                                   priv->rotation,
242                                                   &width, &height);
243                 if (!loading_icon || (width != prev_width && height != prev_height)) {
244                         loading_icon =
245                                 ev_sidebar_thumbnails_get_loading_icon (sidebar_thumbnails,
246                                                                         width, height);
247                 }
248
249                 prev_width = width;
250                 prev_height = height;
251
252                 gtk_list_store_set (priv->list_store, &iter,
253                                     COLUMN_JOB, NULL,
254                                     COLUMN_THUMBNAIL_SET, FALSE,
255                                     COLUMN_PIXBUF, loading_icon,
256                                     -1);
257         }
258         gtk_tree_path_free (path);
259 }
260
261 static gdouble
262 get_scale_for_page (EvSidebarThumbnails *sidebar_thumbnails,
263                     gint                 page)
264 {
265         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
266         gint width, height;
267
268         ev_page_cache_get_size (priv->page_cache,
269                                 page, 0,
270                                 1.0, &width, &height);
271         
272         return (gdouble)THUMBNAIL_WIDTH / (gdouble)width;
273 }
274
275 static void
276 add_range (EvSidebarThumbnails *sidebar_thumbnails,
277            gint                 start_page,
278            gint                 end_page)
279 {
280         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
281         GtkTreePath *path;
282         GtkTreeIter iter;
283         gboolean result;
284         gint page = start_page;
285
286         g_assert (start_page <= end_page);
287
288         path = gtk_tree_path_new_from_indices (start_page, -1);
289         for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
290              result && page <= end_page;
291              result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), page ++) {
292                 EvJob *job;
293                 gboolean thumbnail_set;
294
295                 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store), &iter,
296                                     COLUMN_JOB, &job,
297                                     COLUMN_THUMBNAIL_SET, &thumbnail_set,
298                                     -1);
299
300                 if (job == NULL && !thumbnail_set) {
301                         job = ev_job_thumbnail_new (priv->document,
302                                                     page, priv->rotation,
303                                                     get_scale_for_page (sidebar_thumbnails, page));
304                         ev_job_scheduler_push_job (EV_JOB (job), EV_JOB_PRIORITY_HIGH);
305                         
306                         g_object_set_data_full (G_OBJECT (job), "tree_iter",
307                                                 gtk_tree_iter_copy (&iter),
308                                                 (GDestroyNotify) gtk_tree_iter_free);
309                         g_signal_connect (job, "finished",
310                                           G_CALLBACK (thumbnail_job_completed_callback),
311                                           sidebar_thumbnails);
312                         gtk_list_store_set (priv->list_store, &iter,
313                                             COLUMN_JOB, job,
314                                             -1);
315                         
316                         /* The queue and the list own a ref to the job now */
317                         g_object_unref (job);
318                 } else if (job) {
319                         g_object_unref (job);
320                 }
321         }
322         gtk_tree_path_free (path);
323 }
324
325 /* This modifies start */
326 static void
327 update_visible_range (EvSidebarThumbnails *sidebar_thumbnails,
328                       gint                 start_page,
329                       gint                 end_page)
330 {
331         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
332         int old_start_page, old_end_page;
333
334         old_start_page = priv->start_page;
335         old_end_page = priv->end_page;
336
337         if (start_page == old_start_page &&
338             end_page == old_end_page)
339                 return;
340
341         /* Clear the areas we no longer display */
342         if (old_start_page >= 0 && old_start_page < start_page)
343                 clear_range (sidebar_thumbnails, old_start_page, MIN (start_page - 1, old_end_page));
344         
345         if (old_end_page > 0 && old_end_page > end_page)
346                 clear_range (sidebar_thumbnails, MAX (end_page + 1, old_start_page), old_end_page);
347
348         add_range (sidebar_thumbnails, start_page, end_page);
349         
350         priv->start_page = start_page;
351         priv->end_page = end_page;
352 }
353
354 static void
355 adjustment_changed_cb (EvSidebarThumbnails *sidebar_thumbnails)
356 {
357         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
358         GtkTreePath *path = NULL;
359         GtkTreePath *path2 = NULL;
360         gint wy1;
361         gint wy2;
362
363         /* Widget is not currently visible */
364         if (!GTK_WIDGET_MAPPED (sidebar_thumbnails))
365                 return;
366
367         if (priv->vadjustment->page_size == 0)
368                 return;
369         
370         if (priv->tree_view) {
371                 if (! GTK_WIDGET_REALIZED (priv->tree_view))
372                         return;
373
374 #if GTK_CHECK_VERSION (2, 11, 3)
375                 gtk_tree_view_convert_tree_to_bin_window_coords (GTK_TREE_VIEW (priv->tree_view),
376                                                                  0, (int) priv->vadjustment->value,
377                                                                  NULL, &wy1);
378                 gtk_tree_view_convert_tree_to_bin_window_coords (GTK_TREE_VIEW (priv->tree_view),
379                                                                  0, (int) (priv->vadjustment->value + priv->vadjustment->page_size),
380                                                                  NULL, &wy2);
381 #else
382                 gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
383                                                      0, (int) priv->vadjustment->value,
384                                                      NULL, &wy1);
385                 gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
386                                                      0, (int) (priv->vadjustment->value + priv->vadjustment->page_size),
387                                                      NULL, &wy2);
388 #endif /* GTK_CHECK_VERSION (2, 11, 3) */
389
390                 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
391                                                1, wy1 + 1, &path,
392                                                NULL, NULL, NULL);
393                 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
394                                                1, wy2 -1, &path2,
395                                                NULL, NULL, NULL);
396         } else if (priv->icon_view) {
397                 if (! GTK_WIDGET_REALIZED (priv->icon_view))
398                         return;
399                 if (! gtk_icon_view_get_visible_range (GTK_ICON_VIEW (priv->icon_view), &path, &path2))
400                         return;
401         } else {
402                 return;
403         }
404
405         if (path && path2) {
406                 update_visible_range (sidebar_thumbnails,
407                                       gtk_tree_path_get_indices (path)[0],
408                                       gtk_tree_path_get_indices (path2)[0]);
409         }
410
411         gtk_tree_path_free (path);
412         gtk_tree_path_free (path2);
413 }
414
415 static void
416 ev_sidebar_thumbnails_fill_model (EvSidebarThumbnails *sidebar_thumbnails)
417 {
418         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
419         GtkTreeIter iter;
420         int i;
421         gint prev_width = -1;
422         gint prev_height = -1;
423
424         for (i = 0; i < sidebar_thumbnails->priv->n_pages; i++) {
425                 gchar     *page_label;
426                 gchar     *page_string;
427                 GdkPixbuf *loading_icon = NULL;
428                 gint       width, height;
429
430                 page_label = ev_page_cache_get_page_label (priv->page_cache, i);
431                 page_string = g_markup_printf_escaped ("<i>%s</i>", page_label);
432                 ev_page_cache_get_thumbnail_size (sidebar_thumbnails->priv->page_cache, i,
433                                                   sidebar_thumbnails->priv->rotation,
434                                                   &width, &height);
435                 if (!loading_icon || (width != prev_width && height != prev_height)) {
436                         loading_icon =
437                                 ev_sidebar_thumbnails_get_loading_icon (sidebar_thumbnails,
438                                                                         width, height);
439                 }
440
441                 prev_width = width;
442                 prev_height = height;
443                 
444                 gtk_list_store_append (priv->list_store, &iter);
445                 gtk_list_store_set (priv->list_store, &iter,
446                                     COLUMN_PAGE_STRING, page_string,
447                                     COLUMN_PIXBUF, loading_icon,
448                                     COLUMN_THUMBNAIL_SET, FALSE,
449                                     -1);
450                 g_free (page_label);
451                 g_free (page_string);
452         }
453 }
454
455 static gboolean
456 refresh (EvSidebarThumbnails *sidebar_thumbnails)
457 {
458         adjustment_changed_cb (sidebar_thumbnails);
459         return FALSE;
460 }
461
462 void
463 ev_sidebar_thumbnails_refresh (EvSidebarThumbnails *sidebar_thumbnails,
464                                int                  rotation)
465 {
466         sidebar_thumbnails->priv->rotation = rotation;
467         if (sidebar_thumbnails->priv->loading_icons)
468                 g_hash_table_remove_all (sidebar_thumbnails->priv->loading_icons);
469
470         if (sidebar_thumbnails->priv->document == NULL ||
471             sidebar_thumbnails->priv->n_pages <= 0)
472                 return;
473
474         ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
475         ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
476
477         /* Trigger a redraw */
478         sidebar_thumbnails->priv->start_page = -1;
479         sidebar_thumbnails->priv->end_page = -1;
480         g_idle_add ((GSourceFunc)refresh, sidebar_thumbnails);
481 }
482
483 static void
484 ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
485                                    EvSidebarThumbnails *ev_sidebar_thumbnails)
486 {
487         EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
488         GtkTreePath *path;
489         GtkTreeIter iter;
490         int page;
491
492         if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
493                 return;
494
495         path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
496                                         &iter);
497         page = gtk_tree_path_get_indices (path)[0];
498         gtk_tree_path_free (path);
499
500         ev_page_cache_set_current_page_history (priv->page_cache, page);
501 }
502
503 static void
504 ev_sidebar_icon_selection_changed (GtkIconView         *icon_view,
505                                    EvSidebarThumbnails *ev_sidebar_thumbnails)
506 {
507         EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
508         GtkTreePath *path;
509         GList *selected;
510         int page;
511
512         selected = gtk_icon_view_get_selected_items (icon_view);
513         if (selected == NULL)
514                 return;
515
516         /* We don't handle or expect multiple selection. */
517         g_assert (selected->next == NULL);
518
519         path = selected->data;
520         page = gtk_tree_path_get_indices (path)[0];
521
522         gtk_tree_path_free (path);
523         g_list_free (selected);
524
525         ev_page_cache_set_current_page_history (priv->page_cache, page);
526 }
527
528 static void
529 ev_sidebar_init_tree_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
530 {
531         EvSidebarThumbnailsPrivate *priv;
532         GtkTreeSelection *selection;
533         GtkCellRenderer *renderer;
534
535         priv = ev_sidebar_thumbnails->priv;
536         priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
537
538         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
539         g_signal_connect (selection, "changed",
540                           G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
541         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
542         renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
543                                  "xpad", 2,
544                                  "ypad", 2,
545                                  NULL);
546         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
547                                                      NULL, renderer,
548                                                      "pixbuf", 1,
549                                                      NULL);
550         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
551                                                      NULL, gtk_cell_renderer_text_new (),
552                                                      "markup", 0, NULL);
553         gtk_container_add (GTK_CONTAINER (priv->swindow), priv->tree_view);
554         gtk_widget_show (priv->tree_view);
555 }
556
557 static void
558 ev_sidebar_init_icon_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
559 {
560         EvSidebarThumbnailsPrivate *priv;
561
562         priv = ev_sidebar_thumbnails->priv;
563
564         priv->icon_view = gtk_icon_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
565         gtk_icon_view_set_markup_column (GTK_ICON_VIEW (priv->icon_view), 0);
566         gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (priv->icon_view), 1);
567         g_signal_connect (priv->icon_view, "selection-changed",
568                           G_CALLBACK (ev_sidebar_icon_selection_changed), ev_sidebar_thumbnails);
569
570         gtk_container_add (GTK_CONTAINER (priv->swindow), priv->icon_view);
571         gtk_widget_show (priv->icon_view);
572 }
573
574 static gboolean
575 ev_sidebar_thumbnails_use_icon_view (EvSidebarThumbnails *sidebar_thumbnails)
576 {
577         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
578         if (ev_page_cache_get_n_pages (priv->page_cache) > MAX_ICON_VIEW_PAGE_COUNT)
579                 return FALSE;
580         return TRUE;
581 }
582
583 static void
584 ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
585 {
586         EvSidebarThumbnailsPrivate *priv;
587
588         priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
589
590         priv->list_store = gtk_list_store_new (NUM_COLUMNS,
591                                                G_TYPE_STRING,
592                                                GDK_TYPE_PIXBUF,
593                                                G_TYPE_BOOLEAN,
594                                                EV_TYPE_JOB_THUMBNAIL);
595
596         priv->swindow = gtk_scrolled_window_new (NULL, NULL);
597         
598         /* We actually don't want GTK_POLICY_AUTOMATIC for horizontal scrollbar here
599          * it's just a workaround for bug #449462
600          */
601         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->swindow),
602                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
603         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->swindow),
604                                              GTK_SHADOW_IN);
605         priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->swindow));
606         g_signal_connect_data (G_OBJECT (priv->vadjustment), "value-changed",
607                                G_CALLBACK (adjustment_changed_cb),
608                                ev_sidebar_thumbnails, NULL,
609                                G_CONNECT_SWAPPED | G_CONNECT_AFTER);
610         g_signal_connect_swapped (G_OBJECT (priv->swindow), "size-allocate",
611                                   G_CALLBACK (adjustment_changed_cb),
612                                   ev_sidebar_thumbnails);
613         gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), priv->swindow, TRUE, TRUE, 0);
614
615         /* Put it all together */
616         gtk_widget_show_all (priv->swindow);
617 }
618
619 static void
620 page_changed_cb (EvPageCache         *page_cache,
621                  int                  page,
622                  EvSidebarThumbnails *sidebar)
623 {
624         GtkTreeView *tree_view;
625         GtkTreePath *path;
626
627         path = gtk_tree_path_new_from_indices (page, -1);
628
629         if (sidebar->priv->tree_view) {
630                 tree_view = GTK_TREE_VIEW (sidebar->priv->tree_view);
631                 gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
632                 gtk_tree_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.0, 0.0);
633         } else if (sidebar->priv->icon_view) {
634
635                 g_signal_handlers_block_by_func
636                         (sidebar->priv->icon_view,
637                          G_CALLBACK (ev_sidebar_icon_selection_changed), sidebar);
638
639                 gtk_icon_view_select_path (GTK_ICON_VIEW (sidebar->priv->icon_view), path);
640
641                 g_signal_handlers_unblock_by_func
642                         (sidebar->priv->icon_view,
643                          G_CALLBACK (ev_sidebar_icon_selection_changed), sidebar);
644
645                 gtk_icon_view_set_cursor (GTK_ICON_VIEW (sidebar->priv->icon_view), path, NULL, FALSE);
646         }
647
648         gtk_tree_path_free (path);
649 }
650
651 static void
652 thumbnail_job_completed_callback (EvJobThumbnail      *job,
653                                   EvSidebarThumbnails *sidebar_thumbnails)
654 {
655         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
656         GtkTreeIter *iter;
657
658         iter = (GtkTreeIter *) g_object_get_data (G_OBJECT (job), "tree_iter");
659         gtk_list_store_set (priv->list_store,
660                             iter,
661                             COLUMN_PIXBUF, job->thumbnail,
662                             COLUMN_THUMBNAIL_SET, TRUE,
663                             COLUMN_JOB, NULL,
664                             -1);
665 }
666
667 static void
668 ev_sidebar_thumbnails_set_document (EvSidebarPage       *sidebar_page,
669                                     EvDocument          *document)
670 {
671         EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (sidebar_page);
672
673         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
674
675         g_return_if_fail (EV_IS_DOCUMENT_THUMBNAILS (document));
676
677         priv->page_cache = ev_page_cache_get (document);
678         priv->document = document;
679         priv->n_pages = ev_page_cache_get_n_pages (priv->page_cache);
680         priv->loading_icons = g_hash_table_new_full (g_str_hash,
681                                                      g_str_equal,
682                                                      (GDestroyNotify)g_free,
683                                                      (GDestroyNotify)g_object_unref);
684
685         ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
686         ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
687
688         /* Create the view widget, and remove the old one, if needed */
689         if (ev_sidebar_thumbnails_use_icon_view (sidebar_thumbnails)) {
690                 if (priv->tree_view) {
691                         gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->tree_view);
692                         priv->tree_view = NULL;
693                 }
694
695                 if (! priv->icon_view) {
696                         ev_sidebar_init_icon_view (sidebar_thumbnails);
697                         g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
698                 } else {
699                         gtk_widget_queue_resize (priv->icon_view);
700                 }
701         } else {
702                 if (priv->icon_view) {
703                         gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->icon_view);
704                         priv->icon_view = NULL;
705                 }
706
707                 if (! priv->tree_view) {
708                         ev_sidebar_init_tree_view (sidebar_thumbnails);
709                         g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
710                 }
711         }
712
713         /* Connect to the signal and trigger a fake callback */
714         g_signal_connect (priv->page_cache, "page-changed", G_CALLBACK (page_changed_cb), sidebar_thumbnails);
715         sidebar_thumbnails->priv->start_page = -1;
716         sidebar_thumbnails->priv->end_page = -1;
717         page_changed_cb (priv->page_cache,
718                          ev_page_cache_get_current_page (priv->page_cache),
719                          sidebar_thumbnails);
720         adjustment_changed_cb (sidebar_thumbnails);
721 }
722
723 static gboolean
724 ev_sidebar_thumbnails_clear_job (GtkTreeModel *model,                                             
725                                  GtkTreePath *path,
726                                  GtkTreeIter *iter,
727                                  gpointer data)
728 {
729         EvJob *job;
730         
731         gtk_tree_model_get (model, iter, COLUMN_JOB, &job, -1);
732         
733         if (job != NULL) {
734                 ev_job_cancel (job);
735                 g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, data);
736                 g_object_unref (job);
737         }
738         
739         return FALSE;    
740 }
741
742 static void 
743 ev_sidebar_thumbnails_clear_model (EvSidebarThumbnails *sidebar_thumbnails)
744 {
745         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
746         
747         gtk_tree_model_foreach (GTK_TREE_MODEL (priv->list_store), ev_sidebar_thumbnails_clear_job, sidebar_thumbnails);
748         gtk_list_store_clear (priv->list_store);
749 }
750
751 static gboolean
752 ev_sidebar_thumbnails_support_document (EvSidebarPage   *sidebar_page,
753                                         EvDocument *document)
754 {
755         return (EV_IS_DOCUMENT_THUMBNAILS (document));
756 }
757
758 static const gchar*
759 ev_sidebar_thumbnails_get_label (EvSidebarPage *sidebar_page)
760 {
761         return _("Thumbnails");
762 }
763
764 static void
765 ev_sidebar_thumbnails_page_iface_init (EvSidebarPageIface *iface)
766 {
767         iface->support_document = ev_sidebar_thumbnails_support_document;
768         iface->set_document = ev_sidebar_thumbnails_set_document;
769         iface->get_label = ev_sidebar_thumbnails_get_label;
770 }
771