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