]> www.fi.muni.cz Git - evince.git/blob - shell/ev-sidebar-thumbnails.c
3e3f4bece27d8d7071028dc9fe0928e0d4281b28
[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 typedef struct _EvThumbsSize
49 {
50         gint width;
51         gint height;
52 } EvThumbsSize;
53
54 typedef struct _EvThumbsSizeCache {
55         gboolean uniform;
56         gint uniform_width;
57         gint uniform_height;
58         EvThumbsSize *sizes;
59 } EvThumbsSizeCache;
60
61 struct _EvSidebarThumbnailsPrivate {
62         GtkWidget *swindow;
63         GtkWidget *icon_view;
64         GtkWidget *tree_view;
65         GtkAdjustment *vadjustment;
66         GtkListStore *list_store;
67         GHashTable *loading_icons;
68         EvDocument *document;
69         EvDocumentModel *model;
70         EvThumbsSizeCache *size_cache;
71
72         gint n_pages, pages_done;
73
74         int rotation;
75
76         /* Visible pages */
77         gint start_page, end_page;
78 };
79
80 enum {
81         COLUMN_PAGE_STRING,
82         COLUMN_PIXBUF,
83         COLUMN_THUMBNAIL_SET,
84         COLUMN_JOB,
85         NUM_COLUMNS
86 };
87
88 enum {
89         PROP_0,
90         PROP_WIDGET,
91 };
92
93 static void         ev_sidebar_thumbnails_clear_model      (EvSidebarThumbnails *sidebar);
94 static gboolean     ev_sidebar_thumbnails_support_document (EvSidebarPage       *sidebar_page,
95                                                             EvDocument          *document);
96 static void         ev_sidebar_thumbnails_page_iface_init  (EvSidebarPageIface  *iface);
97 static const gchar* ev_sidebar_thumbnails_get_label        (EvSidebarPage       *sidebar_page);
98 static void         thumbnail_job_completed_callback       (EvJobThumbnail      *job,
99                                                             EvSidebarThumbnails *sidebar_thumbnails);
100 static void         adjustment_changed_cb                  (EvSidebarThumbnails *sidebar_thumbnails);
101
102 G_DEFINE_TYPE_EXTENDED (EvSidebarThumbnails, 
103                         ev_sidebar_thumbnails, 
104                         GTK_TYPE_VBOX,
105                         0, 
106                         G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE, 
107                                                ev_sidebar_thumbnails_page_iface_init))
108
109 #define EV_SIDEBAR_THUMBNAILS_GET_PRIVATE(object) \
110         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_THUMBNAILS, EvSidebarThumbnailsPrivate));
111
112 /* Thumbnails dimensions cache */
113 #define EV_THUMBNAILS_SIZE_CACHE_KEY "ev-thumbnails-size-cache"
114
115 static EvThumbsSizeCache *
116 ev_thumbnails_size_cache_new (EvDocument *document)
117 {
118         EvThumbsSizeCache *cache;
119         EvRenderContext *rc;
120         gint i, n_pages;
121         EvThumbsSize *thumb_size;
122
123         cache = g_new0 (EvThumbsSizeCache, 1);
124
125         n_pages = ev_document_get_n_pages (document);
126
127         /* Assume all pages are the same size until proven otherwise */
128         cache->uniform = TRUE;
129
130         for (i = 0; i < n_pages; i++) {
131                 EvPage *page;
132                 gdouble page_width, page_height;
133                 gint    thumb_width = 0;
134                 gint    thumb_height = 0;
135
136                 page = ev_document_get_page (document, i);
137
138                 ev_document_get_page_size (document, i, &page_width, &page_height);
139
140                 if (!rc) {
141                         rc = ev_render_context_new (page, 0, (gdouble)THUMBNAIL_WIDTH / page_width);
142                 } else {
143                         ev_render_context_set_page (rc, page);
144                         ev_render_context_set_scale (rc, (gdouble)THUMBNAIL_WIDTH / page_width);
145                 }
146
147                 ev_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (document),
148                                                        rc, &thumb_width, &thumb_height);
149
150                 if (i == 0) {
151                         cache->uniform_width = thumb_width;
152                         cache->uniform_height = thumb_height;
153                 } else if (cache->uniform &&
154                            (cache->uniform_width != thumb_width ||
155                             cache->uniform_height != thumb_height)) {
156                         /* It's a different thumbnail size.  Backfill the array. */
157                         int j;
158
159                         cache->sizes = g_new0 (EvThumbsSize, n_pages);
160
161                         for (j = 0; j < i; j++) {
162                                 thumb_size = &(cache->sizes[j]);
163                                 thumb_size->width = cache->uniform_width;
164                                 thumb_size->height = cache->uniform_height;
165                         }
166                         cache->uniform = FALSE;
167                 }
168
169                 if (! cache->uniform) {
170                         thumb_size = &(cache->sizes[i]);
171
172                         thumb_size->width = thumb_width;
173                         thumb_size->height = thumb_height;
174                 }
175
176                 g_object_unref (page);
177         }
178
179         if (rc) {
180                 g_object_unref (rc);
181         }
182
183         return cache;
184 }
185
186 static void
187 ev_thumbnails_size_cache_get_size (EvThumbsSizeCache *cache,
188                                    gint               page,
189                                    gint               rotation,
190                                    gint              *width,
191                                    gint              *height)
192 {
193         gint w, h;
194
195         if (cache->uniform) {
196                 w = cache->uniform_width;
197                 h = cache->uniform_height;
198         } else {
199                 EvThumbsSize *thumb_size;
200
201                 thumb_size = &(cache->sizes[page]);
202
203                 w = thumb_size->width;
204                 h = thumb_size->height;
205         }
206
207         if (rotation == 0 || rotation == 180) {
208                 if (width) *width = w;
209                 if (height) *height = h;
210         } else {
211                 if (width) *width = h;
212                 if (height) *height = w;
213         }
214 }
215
216 static void
217 ev_thumbnails_size_cache_free (EvThumbsSizeCache *cache)
218 {
219         if (cache->sizes) {
220                 g_free (cache->sizes);
221                 cache->sizes = NULL;
222         }
223
224         g_free (cache);
225 }
226
227 static EvThumbsSizeCache *
228 ev_thumbnails_size_cache_get (EvDocument *document)
229 {
230         EvThumbsSizeCache *cache;
231
232         cache = g_object_get_data (G_OBJECT (document), EV_THUMBNAILS_SIZE_CACHE_KEY);
233         if (!cache) {
234                 cache = ev_thumbnails_size_cache_new (document);
235                 g_object_set_data_full (G_OBJECT (document),
236                                         EV_THUMBNAILS_SIZE_CACHE_KEY,
237                                         cache,
238                                         (GDestroyNotify)ev_thumbnails_size_cache_free);
239         }
240
241         return cache;
242 }
243
244
245 static void
246 ev_sidebar_thumbnails_dispose (GObject *object)
247 {
248         EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (object);
249         
250         if (sidebar_thumbnails->priv->loading_icons) {
251                 g_hash_table_destroy (sidebar_thumbnails->priv->loading_icons);
252                 sidebar_thumbnails->priv->loading_icons = NULL;
253         }
254         
255         if (sidebar_thumbnails->priv->list_store) {
256                 ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
257                 g_object_unref (sidebar_thumbnails->priv->list_store);
258                 sidebar_thumbnails->priv->list_store = NULL;
259         }
260
261         G_OBJECT_CLASS (ev_sidebar_thumbnails_parent_class)->dispose (object);
262 }
263
264 static void
265 ev_sidebar_thumbnails_get_property (GObject    *object,
266                                     guint       prop_id,
267                                     GValue     *value,
268                                     GParamSpec *pspec)
269 {
270         EvSidebarThumbnails *sidebar = EV_SIDEBAR_THUMBNAILS (object);
271
272         switch (prop_id) {
273         case PROP_WIDGET:
274                 if (sidebar->priv->tree_view)
275                         g_value_set_object (value, sidebar->priv->tree_view);
276                 else
277                         g_value_set_object (value, sidebar->priv->icon_view);
278                 break;
279         default:
280                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
281                 break;
282         }
283 }
284
285 static void
286 ev_sidebar_thumbnails_map (GtkWidget *widget)
287 {
288         EvSidebarThumbnails *sidebar;
289
290         sidebar = EV_SIDEBAR_THUMBNAILS (widget);
291
292         GTK_WIDGET_CLASS (ev_sidebar_thumbnails_parent_class)->map (widget);
293         
294         adjustment_changed_cb (sidebar);
295 }
296
297 static void
298 ev_sidebar_thumbnails_class_init (EvSidebarThumbnailsClass *ev_sidebar_thumbnails_class)
299 {
300         GObjectClass *g_object_class;
301         GtkObjectClass *gtk_object_class;
302         GtkWidgetClass *widget_class;
303
304         g_object_class = G_OBJECT_CLASS (ev_sidebar_thumbnails_class);
305         gtk_object_class = GTK_OBJECT_CLASS (ev_sidebar_thumbnails_class);
306         widget_class = GTK_WIDGET_CLASS (ev_sidebar_thumbnails_class);
307
308         g_object_class->dispose = ev_sidebar_thumbnails_dispose;
309         g_object_class->get_property = ev_sidebar_thumbnails_get_property;
310         widget_class->map = ev_sidebar_thumbnails_map;
311
312         g_object_class_override_property (g_object_class,
313                                           PROP_WIDGET,
314                                           "main-widget");
315
316         g_type_class_add_private (g_object_class, sizeof (EvSidebarThumbnailsPrivate));
317 }
318
319 GtkWidget *
320 ev_sidebar_thumbnails_new (void)
321 {
322         GtkWidget *ev_sidebar_thumbnails;
323
324         ev_sidebar_thumbnails = g_object_new (EV_TYPE_SIDEBAR_THUMBNAILS, NULL);
325
326         return ev_sidebar_thumbnails;
327 }
328
329 static GdkPixbuf *
330 ev_sidebar_thumbnails_get_loading_icon (EvSidebarThumbnails *sidebar_thumbnails,
331                                         gint                 width,
332                                         gint                 height)
333 {
334         GdkPixbuf *icon;
335         gchar     *key;
336
337         key = g_strdup_printf ("%dx%d", width, height);
338         icon = g_hash_table_lookup (sidebar_thumbnails->priv->loading_icons, key);
339         if (!icon) {
340                 icon = ev_document_misc_get_thumbnail_frame (width, height, NULL);
341                 g_hash_table_insert (sidebar_thumbnails->priv->loading_icons,
342                                      key, icon);
343         } else {
344                 g_free (key);
345         }
346         
347         return icon;
348 }
349
350 static void
351 clear_range (EvSidebarThumbnails *sidebar_thumbnails,
352              gint                 start_page,
353              gint                 end_page)
354 {
355         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
356         GtkTreePath *path;
357         GtkTreeIter iter;
358         gboolean result;
359         gint prev_width = -1;
360         gint prev_height = -1;
361
362         g_assert (start_page <= end_page);
363
364         path = gtk_tree_path_new_from_indices (start_page, -1);
365         for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
366              result && start_page <= end_page;
367              result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), start_page ++) {
368                 EvJobThumbnail *job;
369                 GdkPixbuf *loading_icon = NULL;
370                 gint width, height;
371
372                 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store),
373                                     &iter,
374                                     COLUMN_JOB, &job,
375                                     -1);
376
377                 if (job) {
378                         g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, sidebar_thumbnails);
379                         ev_job_cancel (EV_JOB (job));
380                         g_object_unref (job);
381                 }
382
383                 ev_thumbnails_size_cache_get_size (priv->size_cache, start_page,
384                                                   priv->rotation,
385                                                   &width, &height);
386                 if (!loading_icon || (width != prev_width && height != prev_height)) {
387                         loading_icon =
388                                 ev_sidebar_thumbnails_get_loading_icon (sidebar_thumbnails,
389                                                                         width, height);
390                 }
391
392                 prev_width = width;
393                 prev_height = height;
394
395                 gtk_list_store_set (priv->list_store, &iter,
396                                     COLUMN_JOB, NULL,
397                                     COLUMN_THUMBNAIL_SET, FALSE,
398                                     COLUMN_PIXBUF, loading_icon,
399                                     -1);
400         }
401         gtk_tree_path_free (path);
402 }
403
404 static gdouble
405 get_scale_for_page (EvSidebarThumbnails *sidebar_thumbnails,
406                     gint                 page)
407 {
408         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
409         gdouble width;
410
411         ev_document_get_page_size (priv->document, page, &width, NULL);
412
413         return (gdouble)THUMBNAIL_WIDTH / width;
414 }
415
416 static void
417 add_range (EvSidebarThumbnails *sidebar_thumbnails,
418            gint                 start_page,
419            gint                 end_page)
420 {
421         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
422         GtkTreePath *path;
423         GtkTreeIter iter;
424         gboolean result;
425         gint page = start_page;
426
427         g_assert (start_page <= end_page);
428
429         path = gtk_tree_path_new_from_indices (start_page, -1);
430         for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
431              result && page <= end_page;
432              result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), page ++) {
433                 EvJob *job;
434                 gboolean thumbnail_set;
435
436                 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store), &iter,
437                                     COLUMN_JOB, &job,
438                                     COLUMN_THUMBNAIL_SET, &thumbnail_set,
439                                     -1);
440
441                 if (job == NULL && !thumbnail_set) {
442                         job = ev_job_thumbnail_new (priv->document,
443                                                     page, priv->rotation,
444                                                     get_scale_for_page (sidebar_thumbnails, page));
445                         ev_job_scheduler_push_job (EV_JOB (job), EV_JOB_PRIORITY_HIGH);
446                         
447                         g_object_set_data_full (G_OBJECT (job), "tree_iter",
448                                                 gtk_tree_iter_copy (&iter),
449                                                 (GDestroyNotify) gtk_tree_iter_free);
450                         g_signal_connect (job, "finished",
451                                           G_CALLBACK (thumbnail_job_completed_callback),
452                                           sidebar_thumbnails);
453                         gtk_list_store_set (priv->list_store, &iter,
454                                             COLUMN_JOB, job,
455                                             -1);
456                         
457                         /* The queue and the list own a ref to the job now */
458                         g_object_unref (job);
459                 } else if (job) {
460                         g_object_unref (job);
461                 }
462         }
463         gtk_tree_path_free (path);
464 }
465
466 /* This modifies start */
467 static void
468 update_visible_range (EvSidebarThumbnails *sidebar_thumbnails,
469                       gint                 start_page,
470                       gint                 end_page)
471 {
472         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
473         int old_start_page, old_end_page;
474
475         old_start_page = priv->start_page;
476         old_end_page = priv->end_page;
477
478         if (start_page == old_start_page &&
479             end_page == old_end_page)
480                 return;
481
482         /* Clear the areas we no longer display */
483         if (old_start_page >= 0 && old_start_page < start_page)
484                 clear_range (sidebar_thumbnails, old_start_page, MIN (start_page - 1, old_end_page));
485         
486         if (old_end_page > 0 && old_end_page > end_page)
487                 clear_range (sidebar_thumbnails, MAX (end_page + 1, old_start_page), old_end_page);
488
489         add_range (sidebar_thumbnails, start_page, end_page);
490         
491         priv->start_page = start_page;
492         priv->end_page = end_page;
493 }
494
495 static void
496 adjustment_changed_cb (EvSidebarThumbnails *sidebar_thumbnails)
497 {
498         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
499         GtkTreePath *path = NULL;
500         GtkTreePath *path2 = NULL;
501         gint wy1;
502         gint wy2;
503
504         /* Widget is not currently visible */
505         if (!GTK_WIDGET_MAPPED (sidebar_thumbnails))
506                 return;
507
508         if (priv->vadjustment->page_size == 0)
509                 return;
510         
511         if (priv->tree_view) {
512                 if (! GTK_WIDGET_REALIZED (priv->tree_view))
513                         return;
514
515                 gtk_tree_view_convert_tree_to_bin_window_coords (GTK_TREE_VIEW (priv->tree_view),
516                                                                  0, (int) priv->vadjustment->value,
517                                                                  NULL, &wy1);
518                 gtk_tree_view_convert_tree_to_bin_window_coords (GTK_TREE_VIEW (priv->tree_view),
519                                                                  0, (int) (priv->vadjustment->value + priv->vadjustment->page_size),
520                                                                  NULL, &wy2);
521                 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
522                                                1, wy1 + 1, &path,
523                                                NULL, NULL, NULL);
524                 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
525                                                1, wy2 -1, &path2,
526                                                NULL, NULL, NULL);
527         } else if (priv->icon_view) {
528                 if (! GTK_WIDGET_REALIZED (priv->icon_view))
529                         return;
530                 if (! gtk_icon_view_get_visible_range (GTK_ICON_VIEW (priv->icon_view), &path, &path2))
531                         return;
532         } else {
533                 return;
534         }
535
536         if (path && path2) {
537                 update_visible_range (sidebar_thumbnails,
538                                       gtk_tree_path_get_indices (path)[0],
539                                       gtk_tree_path_get_indices (path2)[0]);
540         }
541
542         gtk_tree_path_free (path);
543         gtk_tree_path_free (path2);
544 }
545
546 static void
547 ev_sidebar_thumbnails_fill_model (EvSidebarThumbnails *sidebar_thumbnails)
548 {
549         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
550         GtkTreeIter iter;
551         int i;
552         gint prev_width = -1;
553         gint prev_height = -1;
554
555         for (i = 0; i < sidebar_thumbnails->priv->n_pages; i++) {
556                 gchar     *page_label;
557                 gchar     *page_string;
558                 GdkPixbuf *loading_icon = NULL;
559                 gint       width, height;
560
561                 page_label = ev_document_get_page_label (priv->document, i);
562                 page_string = g_markup_printf_escaped ("<i>%s</i>", page_label);
563                 ev_thumbnails_size_cache_get_size (sidebar_thumbnails->priv->size_cache, i,
564                                                   sidebar_thumbnails->priv->rotation,
565                                                   &width, &height);
566                 if (!loading_icon || (width != prev_width && height != prev_height)) {
567                         loading_icon =
568                                 ev_sidebar_thumbnails_get_loading_icon (sidebar_thumbnails,
569                                                                         width, height);
570                 }
571
572                 prev_width = width;
573                 prev_height = height;
574                 
575                 gtk_list_store_append (priv->list_store, &iter);
576                 gtk_list_store_set (priv->list_store, &iter,
577                                     COLUMN_PAGE_STRING, page_string,
578                                     COLUMN_PIXBUF, loading_icon,
579                                     COLUMN_THUMBNAIL_SET, FALSE,
580                                     -1);
581                 g_free (page_label);
582                 g_free (page_string);
583         }
584 }
585
586 static void
587 ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
588                                    EvSidebarThumbnails *ev_sidebar_thumbnails)
589 {
590         EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
591         GtkTreePath *path;
592         GtkTreeIter iter;
593         int page;
594
595         if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
596                 return;
597
598         path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
599                                         &iter);
600         page = gtk_tree_path_get_indices (path)[0];
601         gtk_tree_path_free (path);
602
603         ev_document_model_set_page (priv->model, page);
604 }
605
606 static void
607 ev_sidebar_icon_selection_changed (GtkIconView         *icon_view,
608                                    EvSidebarThumbnails *ev_sidebar_thumbnails)
609 {
610         EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
611         GtkTreePath *path;
612         GList *selected;
613         int page;
614
615         selected = gtk_icon_view_get_selected_items (icon_view);
616         if (selected == NULL)
617                 return;
618
619         /* We don't handle or expect multiple selection. */
620         g_assert (selected->next == NULL);
621
622         path = selected->data;
623         page = gtk_tree_path_get_indices (path)[0];
624
625         gtk_tree_path_free (path);
626         g_list_free (selected);
627
628         ev_document_model_set_page (priv->model, page);
629 }
630
631 static void
632 ev_sidebar_init_tree_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
633 {
634         EvSidebarThumbnailsPrivate *priv;
635         GtkTreeSelection *selection;
636         GtkCellRenderer *renderer;
637
638         priv = ev_sidebar_thumbnails->priv;
639         priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
640
641         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
642         g_signal_connect (selection, "changed",
643                           G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
644         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
645         renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
646                                  "xpad", 2,
647                                  "ypad", 2,
648                                  NULL);
649         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
650                                                      NULL, renderer,
651                                                      "pixbuf", 1,
652                                                      NULL);
653         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
654                                                      NULL, gtk_cell_renderer_text_new (),
655                                                      "markup", 0, NULL);
656         gtk_container_add (GTK_CONTAINER (priv->swindow), priv->tree_view);
657         gtk_widget_show (priv->tree_view);
658 }
659
660 static void
661 ev_sidebar_init_icon_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
662 {
663         EvSidebarThumbnailsPrivate *priv;
664
665         priv = ev_sidebar_thumbnails->priv;
666
667         priv->icon_view = gtk_icon_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
668         gtk_icon_view_set_markup_column (GTK_ICON_VIEW (priv->icon_view), 0);
669         gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (priv->icon_view), 1);
670         g_signal_connect (priv->icon_view, "selection-changed",
671                           G_CALLBACK (ev_sidebar_icon_selection_changed), ev_sidebar_thumbnails);
672
673         gtk_container_add (GTK_CONTAINER (priv->swindow), priv->icon_view);
674         gtk_widget_show (priv->icon_view);
675 }
676
677 static gboolean
678 ev_sidebar_thumbnails_use_icon_view (EvSidebarThumbnails *sidebar_thumbnails)
679 {
680         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
681
682         return (ev_document_get_n_pages (priv->document) <= MAX_ICON_VIEW_PAGE_COUNT);
683 }
684
685 static void
686 ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
687 {
688         EvSidebarThumbnailsPrivate *priv;
689
690         priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
691
692         priv->list_store = gtk_list_store_new (NUM_COLUMNS,
693                                                G_TYPE_STRING,
694                                                GDK_TYPE_PIXBUF,
695                                                G_TYPE_BOOLEAN,
696                                                EV_TYPE_JOB_THUMBNAIL);
697
698         priv->swindow = gtk_scrolled_window_new (NULL, NULL);
699         
700         /* We actually don't want GTK_POLICY_AUTOMATIC for horizontal scrollbar here
701          * it's just a workaround for bug #449462
702          */
703         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->swindow),
704                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
705         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->swindow),
706                                              GTK_SHADOW_IN);
707         priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->swindow));
708         g_signal_connect_data (priv->vadjustment, "value-changed",
709                                G_CALLBACK (adjustment_changed_cb),
710                                ev_sidebar_thumbnails, NULL,
711                                G_CONNECT_SWAPPED | G_CONNECT_AFTER);
712         g_signal_connect_swapped (priv->swindow, "size-allocate",
713                                   G_CALLBACK (adjustment_changed_cb),
714                                   ev_sidebar_thumbnails);
715         gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), priv->swindow, TRUE, TRUE, 0);
716
717         /* Put it all together */
718         gtk_widget_show_all (priv->swindow);
719 }
720
721 static void
722 ev_sidebar_thumbnails_set_current_page (EvSidebarThumbnails *sidebar,
723                                         gint                 page)
724 {
725         GtkTreeView *tree_view;
726         GtkTreePath *path;
727
728         path = gtk_tree_path_new_from_indices (page, -1);
729
730         if (sidebar->priv->tree_view) {
731                 tree_view = GTK_TREE_VIEW (sidebar->priv->tree_view);
732                 gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
733                 gtk_tree_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.0, 0.0);
734         } else if (sidebar->priv->icon_view) {
735
736                 g_signal_handlers_block_by_func
737                         (sidebar->priv->icon_view,
738                          G_CALLBACK (ev_sidebar_icon_selection_changed), sidebar);
739
740                 gtk_icon_view_select_path (GTK_ICON_VIEW (sidebar->priv->icon_view), path);
741
742                 g_signal_handlers_unblock_by_func
743                         (sidebar->priv->icon_view,
744                          G_CALLBACK (ev_sidebar_icon_selection_changed), sidebar);
745
746                 gtk_icon_view_set_cursor (GTK_ICON_VIEW (sidebar->priv->icon_view), path, NULL, FALSE);
747         }
748
749         gtk_tree_path_free (path);
750 }
751
752 static void
753 page_changed_cb (EvSidebarThumbnails *sidebar,
754                  gint                 old_page,
755                  gint                 new_page)
756 {
757         ev_sidebar_thumbnails_set_current_page (sidebar, new_page);
758 }
759
760 static gboolean
761 refresh (EvSidebarThumbnails *sidebar_thumbnails)
762 {
763         adjustment_changed_cb (sidebar_thumbnails);
764         return FALSE;
765 }
766
767 static void
768 ev_sidebar_thumbnails_rotation_changed_cb (EvDocumentModel     *model,
769                                            GParamSpec          *pspec,
770                                            EvSidebarThumbnails *sidebar_thumbnails)
771 {
772         gint rotation = ev_document_model_get_rotation (model);
773
774         sidebar_thumbnails->priv->rotation = rotation;
775         if (sidebar_thumbnails->priv->loading_icons)
776                 g_hash_table_remove_all (sidebar_thumbnails->priv->loading_icons);
777
778         if (sidebar_thumbnails->priv->document == NULL ||
779             sidebar_thumbnails->priv->n_pages <= 0)
780                 return;
781
782         ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
783         ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
784
785         /* Trigger a redraw */
786         sidebar_thumbnails->priv->start_page = -1;
787         sidebar_thumbnails->priv->end_page = -1;
788         g_idle_add ((GSourceFunc)refresh, sidebar_thumbnails);
789 }
790
791 static void
792 thumbnail_job_completed_callback (EvJobThumbnail      *job,
793                                   EvSidebarThumbnails *sidebar_thumbnails)
794 {
795         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
796         GtkTreeIter *iter;
797
798         iter = (GtkTreeIter *) g_object_get_data (G_OBJECT (job), "tree_iter");
799         gtk_list_store_set (priv->list_store,
800                             iter,
801                             COLUMN_PIXBUF, job->thumbnail,
802                             COLUMN_THUMBNAIL_SET, TRUE,
803                             COLUMN_JOB, NULL,
804                             -1);
805 }
806
807 static void
808 ev_sidebar_thumbnails_document_changed_cb (EvDocumentModel     *model,
809                                            GParamSpec          *pspec,
810                                            EvSidebarThumbnails *sidebar_thumbnails)
811 {
812         EvDocument *document = ev_document_model_get_document (model);
813         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
814
815         if (!EV_IS_DOCUMENT_THUMBNAILS (document) ||
816             ev_document_get_n_pages (document) <= 0 ||
817             !ev_document_check_dimensions (document)) {
818                 return;
819         }
820
821         priv->size_cache = ev_thumbnails_size_cache_get (document);
822         priv->document = document;
823         priv->n_pages = ev_document_get_n_pages (document);
824         priv->rotation = ev_document_model_get_rotation (model);
825         priv->loading_icons = g_hash_table_new_full (g_str_hash,
826                                                      g_str_equal,
827                                                      (GDestroyNotify)g_free,
828                                                      (GDestroyNotify)g_object_unref);
829
830         ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
831         ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
832
833         /* Create the view widget, and remove the old one, if needed */
834         if (ev_sidebar_thumbnails_use_icon_view (sidebar_thumbnails)) {
835                 if (priv->tree_view) {
836                         gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->tree_view);
837                         priv->tree_view = NULL;
838                 }
839
840                 if (! priv->icon_view) {
841                         ev_sidebar_init_icon_view (sidebar_thumbnails);
842                         g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
843                 } else {
844                         gtk_widget_queue_resize (priv->icon_view);
845                 }
846         } else {
847                 if (priv->icon_view) {
848                         gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->icon_view);
849                         priv->icon_view = NULL;
850                 }
851
852                 if (! priv->tree_view) {
853                         ev_sidebar_init_tree_view (sidebar_thumbnails);
854                         g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
855                 }
856         }
857
858         /* Connect to the signal and trigger a fake callback */
859         g_signal_connect_swapped (priv->model, "page-changed",
860                                   G_CALLBACK (page_changed_cb),
861                                   sidebar_thumbnails);
862         g_signal_connect (priv->model, "notify::rotation",
863                           G_CALLBACK (ev_sidebar_thumbnails_rotation_changed_cb),
864                           sidebar_thumbnails);
865         sidebar_thumbnails->priv->start_page = -1;
866         sidebar_thumbnails->priv->end_page = -1;
867         ev_sidebar_thumbnails_set_current_page (sidebar_thumbnails,
868                                                 ev_document_model_get_page (model));
869         adjustment_changed_cb (sidebar_thumbnails);
870 }
871
872 static void
873 ev_sidebar_thumbnails_set_model (EvSidebarPage   *sidebar_page,
874                                  EvDocumentModel *model)
875 {
876         EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (sidebar_page);
877         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
878
879         if (priv->model == model)
880                 return;
881
882         priv->model = model;
883         g_signal_connect (model, "notify::document",
884                           G_CALLBACK (ev_sidebar_thumbnails_document_changed_cb),
885                           sidebar_page);
886 }
887
888 static gboolean
889 ev_sidebar_thumbnails_clear_job (GtkTreeModel *model,                                             
890                                  GtkTreePath *path,
891                                  GtkTreeIter *iter,
892                                  gpointer data)
893 {
894         EvJob *job;
895         
896         gtk_tree_model_get (model, iter, COLUMN_JOB, &job, -1);
897         
898         if (job != NULL) {
899                 ev_job_cancel (job);
900                 g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, data);
901                 g_object_unref (job);
902         }
903         
904         return FALSE;    
905 }
906
907 static void 
908 ev_sidebar_thumbnails_clear_model (EvSidebarThumbnails *sidebar_thumbnails)
909 {
910         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
911         
912         gtk_tree_model_foreach (GTK_TREE_MODEL (priv->list_store), ev_sidebar_thumbnails_clear_job, sidebar_thumbnails);
913         gtk_list_store_clear (priv->list_store);
914 }
915
916 static gboolean
917 ev_sidebar_thumbnails_support_document (EvSidebarPage   *sidebar_page,
918                                         EvDocument *document)
919 {
920         return (EV_IS_DOCUMENT_THUMBNAILS (document));
921 }
922
923 static const gchar*
924 ev_sidebar_thumbnails_get_label (EvSidebarPage *sidebar_page)
925 {
926         return _("Thumbnails");
927 }
928
929 static void
930 ev_sidebar_thumbnails_page_iface_init (EvSidebarPageIface *iface)
931 {
932         iface->support_document = ev_sidebar_thumbnails_support_document;
933         iface->set_model = ev_sidebar_thumbnails_set_model;
934         iface->get_label = ev_sidebar_thumbnails_get_label;
935 }
936