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