]> www.fi.muni.cz Git - evince.git/blob - shell/ev-sidebar-thumbnails.c
Do not crash when adjustment page size is 0. Fixes bug #547440.
[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         GdkPixbuf *loading_icon;
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_icon) {
108                 g_object_unref (sidebar_thumbnails->priv->loading_icon);
109                 sidebar_thumbnails->priv->loading_icon = 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 void
187 clear_range (EvSidebarThumbnails *sidebar_thumbnails,
188              gint                 start_page,
189              gint                 end_page)
190 {
191         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
192         GtkTreePath *path;
193         GtkTreeIter iter;
194         gboolean result;
195
196         g_assert (start_page <= end_page);
197
198         path = gtk_tree_path_new_from_indices (start_page, -1);
199         for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
200              result && start_page <= end_page;
201              result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), start_page ++) {
202                 EvJobThumbnail *job;
203
204                 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store),
205                                     &iter,
206                                     COLUMN_JOB, &job,
207                                     -1);
208
209                 if (job) {
210                         g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, sidebar_thumbnails);
211                         ev_job_cancel (EV_JOB (job));
212                         g_object_unref (job);
213                 }
214
215                 gtk_list_store_set (priv->list_store, &iter,
216                                     COLUMN_JOB, NULL,
217                                     COLUMN_THUMBNAIL_SET, FALSE,
218                                     COLUMN_PIXBUF, priv->loading_icon,
219                                     -1);
220         }
221         gtk_tree_path_free (path);
222 }
223
224 static gdouble
225 get_scale_for_page (EvSidebarThumbnails *sidebar_thumbnails,
226                     gint                 page)
227 {
228         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
229         gint width, height;
230
231         ev_page_cache_get_size (priv->page_cache,
232                                 page, 0,
233                                 1.0, &width, &height);
234         
235         return (gdouble)THUMBNAIL_WIDTH / (gdouble)width;
236 }
237
238 static void
239 add_range (EvSidebarThumbnails *sidebar_thumbnails,
240            gint                 start_page,
241            gint                 end_page)
242 {
243         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
244         GtkTreePath *path;
245         GtkTreeIter iter;
246         gboolean result;
247         gint page = start_page;
248
249         g_assert (start_page <= end_page);
250
251         path = gtk_tree_path_new_from_indices (start_page, -1);
252         for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
253              result && page <= end_page;
254              result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), page ++) {
255                 EvJob *job;
256                 gboolean thumbnail_set;
257
258                 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store), &iter,
259                                     COLUMN_JOB, &job,
260                                     COLUMN_THUMBNAIL_SET, &thumbnail_set,
261                                     -1);
262
263                 if (job == NULL && !thumbnail_set) {
264                         job = ev_job_thumbnail_new (priv->document,
265                                                     page, priv->rotation,
266                                                     get_scale_for_page (sidebar_thumbnails, page));
267                         ev_job_scheduler_push_job (EV_JOB (job), EV_JOB_PRIORITY_HIGH);
268                         
269                         g_object_set_data_full (G_OBJECT (job), "tree_iter",
270                                                 gtk_tree_iter_copy (&iter),
271                                                 (GDestroyNotify) gtk_tree_iter_free);
272                         g_signal_connect (job, "finished",
273                                           G_CALLBACK (thumbnail_job_completed_callback),
274                                           sidebar_thumbnails);
275                         gtk_list_store_set (priv->list_store, &iter,
276                                             COLUMN_JOB, job,
277                                             -1);
278                         
279                         /* The queue and the list own a ref to the job now */
280                         g_object_unref (job);
281                 } else if (job) {
282                         g_object_unref (job);
283                 }
284         }
285         gtk_tree_path_free (path);
286 }
287
288 /* This modifies start */
289 static void
290 update_visible_range (EvSidebarThumbnails *sidebar_thumbnails,
291                       gint                 start_page,
292                       gint                 end_page)
293 {
294         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
295         int old_start_page, old_end_page;
296
297         old_start_page = priv->start_page;
298         old_end_page = priv->end_page;
299
300         if (start_page == old_start_page &&
301             end_page == old_end_page)
302                 return;
303
304         /* Clear the areas we no longer display */
305         if (old_start_page >= 0 && old_start_page < start_page)
306                 clear_range (sidebar_thumbnails, old_start_page, MIN (start_page - 1, old_end_page));
307         
308         if (old_end_page > 0 && old_end_page > end_page)
309                 clear_range (sidebar_thumbnails, MAX (end_page + 1, old_start_page), old_end_page);
310
311         add_range (sidebar_thumbnails, start_page, end_page);
312         
313         priv->start_page = start_page;
314         priv->end_page = end_page;
315 }
316
317 static void
318 adjustment_changed_cb (EvSidebarThumbnails *sidebar_thumbnails)
319 {
320         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
321         GtkTreePath *path = NULL;
322         GtkTreePath *path2 = NULL;
323         gint wy1;
324         gint wy2;
325
326         /* Widget is not currently visible */
327         if (!GTK_WIDGET_MAPPED (sidebar_thumbnails))
328                 return;
329
330         if (priv->vadjustment->page_size == 0)
331                 return;
332         
333         if (priv->tree_view) {
334                 if (! GTK_WIDGET_REALIZED (priv->tree_view))
335                         return;
336
337 #if GTK_CHECK_VERSION (2, 11, 3)
338                 gtk_tree_view_convert_tree_to_bin_window_coords (GTK_TREE_VIEW (priv->tree_view),
339                                                                  0, (int) priv->vadjustment->value,
340                                                                  NULL, &wy1);
341                 gtk_tree_view_convert_tree_to_bin_window_coords (GTK_TREE_VIEW (priv->tree_view),
342                                                                  0, (int) (priv->vadjustment->value + priv->vadjustment->page_size),
343                                                                  NULL, &wy2);
344 #else
345                 gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
346                                                      0, (int) priv->vadjustment->value,
347                                                      NULL, &wy1);
348                 gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
349                                                      0, (int) (priv->vadjustment->value + priv->vadjustment->page_size),
350                                                      NULL, &wy2);
351 #endif /* GTK_CHECK_VERSION (2, 11, 3) */
352
353                 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
354                                                1, wy1 + 1, &path,
355                                                NULL, NULL, NULL);
356                 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
357                                                1, wy2 -1, &path2,
358                                                NULL, NULL, NULL);
359         } else if (priv->icon_view) {
360                 if (! GTK_WIDGET_REALIZED (priv->icon_view))
361                         return;
362                 if (! gtk_icon_view_get_visible_range (GTK_ICON_VIEW (priv->icon_view), &path, &path2))
363                         return;
364         } else {
365                 return;
366         }
367
368         if (path && path2) {
369                 update_visible_range (sidebar_thumbnails,
370                                       gtk_tree_path_get_indices (path)[0],
371                                       gtk_tree_path_get_indices (path2)[0]);
372         }
373
374         gtk_tree_path_free (path);
375         gtk_tree_path_free (path2);
376 }
377
378 static void
379 ev_sidebar_thumbnails_fill_model (EvSidebarThumbnails *sidebar_thumbnails)
380 {
381         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
382         GtkTreeIter iter;
383         int i;
384
385         for (i = 0; i < sidebar_thumbnails->priv->n_pages; i++) {
386                 gchar *page_label;
387                 gchar *page_string;
388
389                 page_label = ev_page_cache_get_page_label (priv->page_cache, i);
390                 page_string = g_markup_printf_escaped ("<i>%s</i>", page_label);
391
392                 gtk_list_store_append (priv->list_store, &iter);
393                 gtk_list_store_set (priv->list_store, &iter,
394                                     COLUMN_PAGE_STRING, page_string,
395                                     COLUMN_PIXBUF, priv->loading_icon,
396                                     COLUMN_THUMBNAIL_SET, FALSE,
397                                     -1);
398                 g_free (page_label);
399                 g_free (page_string);
400         }
401 }
402
403
404 static void
405 ev_sidebar_thumbnails_set_loading_icon (EvSidebarThumbnails *sidebar_thumbnails)
406 {
407         if (sidebar_thumbnails->priv->loading_icon)
408                 g_object_unref (sidebar_thumbnails->priv->loading_icon);
409
410         if (sidebar_thumbnails->priv->document &&
411             sidebar_thumbnails->priv->n_pages > 0) {
412                 gint width, height;
413
414                 /* We get the dimensions of the first page so that we can make a blank
415                  * icon.  */
416                 ev_page_cache_get_thumbnail_size (sidebar_thumbnails->priv->page_cache, 0,
417                                                   sidebar_thumbnails->priv->rotation,
418                                                   &width, &height);
419
420                 sidebar_thumbnails->priv->loading_icon =
421                         ev_document_misc_get_thumbnail_frame (width, height, NULL);
422         } else {
423                 sidebar_thumbnails->priv->loading_icon = NULL;
424         }
425
426 }
427
428 static gboolean
429 refresh (EvSidebarThumbnails *sidebar_thumbnails)
430 {
431         adjustment_changed_cb (sidebar_thumbnails);
432         return FALSE;
433 }
434
435 void
436 ev_sidebar_thumbnails_refresh (EvSidebarThumbnails *sidebar_thumbnails,
437                                int                  rotation)
438 {
439         sidebar_thumbnails->priv->rotation = rotation;
440         ev_sidebar_thumbnails_set_loading_icon (sidebar_thumbnails);
441
442         if (sidebar_thumbnails->priv->document == NULL ||
443             sidebar_thumbnails->priv->n_pages <= 0)
444                 return;
445
446         ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
447         ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
448
449         /* Trigger a redraw */
450         sidebar_thumbnails->priv->start_page = -1;
451         sidebar_thumbnails->priv->end_page = -1;
452         g_idle_add ((GSourceFunc)refresh, sidebar_thumbnails);
453 }
454
455 static void
456 ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
457                                    EvSidebarThumbnails *ev_sidebar_thumbnails)
458 {
459         EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
460         GtkTreePath *path;
461         GtkTreeIter iter;
462         int page;
463
464         if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
465                 return;
466
467         path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
468                                         &iter);
469         page = gtk_tree_path_get_indices (path)[0];
470         gtk_tree_path_free (path);
471
472         ev_page_cache_set_current_page_history (priv->page_cache, page);
473 }
474
475 static void
476 ev_sidebar_icon_selection_changed (GtkIconView         *icon_view,
477                                    EvSidebarThumbnails *ev_sidebar_thumbnails)
478 {
479         EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
480         GtkTreePath *path;
481         GList *selected;
482         int page;
483
484         selected = gtk_icon_view_get_selected_items (icon_view);
485         if (selected == NULL)
486                 return;
487
488         /* We don't handle or expect multiple selection. */
489         g_assert (selected->next == NULL);
490
491         path = selected->data;
492         page = gtk_tree_path_get_indices (path)[0];
493
494         gtk_tree_path_free (path);
495         g_list_free (selected);
496
497         ev_page_cache_set_current_page_history (priv->page_cache, page);
498 }
499
500 static void
501 ev_sidebar_init_tree_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
502 {
503         EvSidebarThumbnailsPrivate *priv;
504         GtkTreeSelection *selection;
505         GtkCellRenderer *renderer;
506
507         priv = ev_sidebar_thumbnails->priv;
508         priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
509
510         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
511         g_signal_connect (selection, "changed",
512                           G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
513         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
514         renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
515                                  "xpad", 2,
516                                  "ypad", 2,
517                                  NULL);
518         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
519                                                      NULL, renderer,
520                                                      "pixbuf", 1,
521                                                      NULL);
522         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
523                                                      NULL, gtk_cell_renderer_text_new (),
524                                                      "markup", 0, NULL);
525         gtk_container_add (GTK_CONTAINER (priv->swindow), priv->tree_view);
526         gtk_widget_show (priv->tree_view);
527 }
528
529 static void
530 ev_sidebar_init_icon_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
531 {
532         EvSidebarThumbnailsPrivate *priv;
533
534         priv = ev_sidebar_thumbnails->priv;
535
536         priv->icon_view = gtk_icon_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
537         gtk_icon_view_set_markup_column (GTK_ICON_VIEW (priv->icon_view), 0);
538         gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (priv->icon_view), 1);
539         g_signal_connect (priv->icon_view, "selection-changed",
540                           G_CALLBACK (ev_sidebar_icon_selection_changed), ev_sidebar_thumbnails);
541
542         gtk_container_add (GTK_CONTAINER (priv->swindow), priv->icon_view);
543         gtk_widget_show (priv->icon_view);
544 }
545
546 static gboolean
547 ev_sidebar_thumbnails_use_icon_view (EvSidebarThumbnails *sidebar_thumbnails)
548 {
549         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
550         if (ev_page_cache_get_n_pages (priv->page_cache) > MAX_ICON_VIEW_PAGE_COUNT)
551                 return FALSE;
552         return TRUE;
553 }
554
555 static void
556 ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
557 {
558         EvSidebarThumbnailsPrivate *priv;
559
560         priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
561
562         priv->list_store = gtk_list_store_new (NUM_COLUMNS,
563                                                G_TYPE_STRING,
564                                                GDK_TYPE_PIXBUF,
565                                                G_TYPE_BOOLEAN,
566                                                EV_TYPE_JOB_THUMBNAIL);
567
568         priv->swindow = gtk_scrolled_window_new (NULL, NULL);
569         
570         /* We actually don't want GTK_POLICY_AUTOMATIC for horizontal scrollbar here
571          * it's just a workaround for bug #449462
572          */
573         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->swindow),
574                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
575         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->swindow),
576                                              GTK_SHADOW_IN);
577         priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->swindow));
578         g_signal_connect_data (G_OBJECT (priv->vadjustment), "value-changed",
579                                G_CALLBACK (adjustment_changed_cb),
580                                ev_sidebar_thumbnails, NULL,
581                                G_CONNECT_SWAPPED | G_CONNECT_AFTER);
582         g_signal_connect_swapped (G_OBJECT (priv->swindow), "size-allocate",
583                                   G_CALLBACK (adjustment_changed_cb),
584                                   ev_sidebar_thumbnails);
585         gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), priv->swindow, TRUE, TRUE, 0);
586
587         /* Put it all together */
588         gtk_widget_show_all (priv->swindow);
589 }
590
591 static void
592 page_changed_cb (EvPageCache         *page_cache,
593                  int                  page,
594                  EvSidebarThumbnails *sidebar)
595 {
596         GtkTreeView *tree_view;
597         GtkTreePath *path;
598
599         path = gtk_tree_path_new_from_indices (page, -1);
600
601         if (sidebar->priv->tree_view) {
602                 tree_view = GTK_TREE_VIEW (sidebar->priv->tree_view);
603                 gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
604                 gtk_tree_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.0, 0.0);
605         } else if (sidebar->priv->icon_view) {
606
607                 g_signal_handlers_block_by_func
608                         (sidebar->priv->icon_view,
609                          G_CALLBACK (ev_sidebar_icon_selection_changed), sidebar);
610
611                 gtk_icon_view_select_path (GTK_ICON_VIEW (sidebar->priv->icon_view), path);
612
613                 g_signal_handlers_unblock_by_func
614                         (sidebar->priv->icon_view,
615                          G_CALLBACK (ev_sidebar_icon_selection_changed), sidebar);
616
617                 gtk_icon_view_set_cursor (GTK_ICON_VIEW (sidebar->priv->icon_view), path, NULL, FALSE);
618         }
619
620         gtk_tree_path_free (path);
621 }
622
623 static void
624 thumbnail_job_completed_callback (EvJobThumbnail      *job,
625                                   EvSidebarThumbnails *sidebar_thumbnails)
626 {
627         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
628         GtkTreeIter *iter;
629
630         iter = (GtkTreeIter *) g_object_get_data (G_OBJECT (job), "tree_iter");
631         gtk_list_store_set (priv->list_store,
632                             iter,
633                             COLUMN_PIXBUF, job->thumbnail,
634                             COLUMN_THUMBNAIL_SET, TRUE,
635                             COLUMN_JOB, NULL,
636                             -1);
637 }
638
639 static void
640 ev_sidebar_thumbnails_set_document (EvSidebarPage       *sidebar_page,
641                                     EvDocument          *document)
642 {
643         EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (sidebar_page);
644
645         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
646
647         g_return_if_fail (EV_IS_DOCUMENT_THUMBNAILS (document));
648
649         priv->page_cache = ev_page_cache_get (document);
650         priv->document = document;
651         priv->n_pages = ev_page_cache_get_n_pages (priv->page_cache);
652
653         ev_sidebar_thumbnails_set_loading_icon (sidebar_thumbnails);
654
655         ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
656         ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
657
658         /* Create the view widget, and remove the old one, if needed */
659         if (ev_sidebar_thumbnails_use_icon_view (sidebar_thumbnails)) {
660                 if (priv->tree_view) {
661                         gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->tree_view);
662                         priv->tree_view = NULL;
663                 }
664
665                 if (! priv->icon_view) {
666                         ev_sidebar_init_icon_view (sidebar_thumbnails);
667                         g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
668                 } else {
669                         gtk_widget_queue_resize (priv->icon_view);
670                 }
671         } else {
672                 if (priv->icon_view) {
673                         gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->icon_view);
674                         priv->icon_view = NULL;
675                 }
676
677                 if (! priv->tree_view) {
678                         ev_sidebar_init_tree_view (sidebar_thumbnails);
679                         g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
680                 }
681         }
682
683         /* Connect to the signal and trigger a fake callback */
684         g_signal_connect (priv->page_cache, "page-changed", G_CALLBACK (page_changed_cb), sidebar_thumbnails);
685         sidebar_thumbnails->priv->start_page = -1;
686         sidebar_thumbnails->priv->end_page = -1;
687         page_changed_cb (priv->page_cache,
688                          ev_page_cache_get_current_page (priv->page_cache),
689                          sidebar_thumbnails);
690         adjustment_changed_cb (sidebar_thumbnails);
691 }
692
693 static gboolean
694 ev_sidebar_thumbnails_clear_job (GtkTreeModel *model,                                             
695                                  GtkTreePath *path,
696                                  GtkTreeIter *iter,
697                                  gpointer data)
698 {
699         EvJob *job;
700         
701         gtk_tree_model_get (model, iter, COLUMN_JOB, &job, -1);
702         
703         if (job != NULL) {
704                 ev_job_cancel (job);
705                 g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, data);
706                 g_object_unref (job);
707         }
708         
709         return FALSE;    
710 }
711
712 static void 
713 ev_sidebar_thumbnails_clear_model (EvSidebarThumbnails *sidebar_thumbnails)
714 {
715         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
716         
717         gtk_tree_model_foreach (GTK_TREE_MODEL (priv->list_store), ev_sidebar_thumbnails_clear_job, sidebar_thumbnails);
718         gtk_list_store_clear (priv->list_store);
719 }
720
721 static gboolean
722 ev_sidebar_thumbnails_support_document (EvSidebarPage   *sidebar_page,
723                                         EvDocument *document)
724 {
725         return (EV_IS_DOCUMENT_THUMBNAILS (document));
726 }
727
728 static const gchar*
729 ev_sidebar_thumbnails_get_label (EvSidebarPage *sidebar_page)
730 {
731         return _("Thumbnails");
732 }
733
734 static void
735 ev_sidebar_thumbnails_page_iface_init (EvSidebarPageIface *iface)
736 {
737         iface->support_document = ev_sidebar_thumbnails_support_document;
738         iface->set_document = ev_sidebar_thumbnails_set_document;
739         iface->get_label = ev_sidebar_thumbnails_get_label;
740 }
741