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