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