]> www.fi.muni.cz Git - evince.git/blob - shell/ev-sidebar-thumbnails.c
Replace GTK_WIDGET_REALIZED() with gtk_widget_get_realized()
[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         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         GdkPixbuf *icon;
336         gchar     *key;
337
338         key = g_strdup_printf ("%dx%d", width, height);
339         icon = g_hash_table_lookup (sidebar_thumbnails->priv->loading_icons, key);
340         if (!icon) {
341                 icon = ev_document_misc_get_thumbnail_frame (width, height, NULL);
342                 g_hash_table_insert (sidebar_thumbnails->priv->loading_icons,
343                                      key, icon);
344         } else {
345                 g_free (key);
346         }
347         
348         return icon;
349 }
350
351 static void
352 clear_range (EvSidebarThumbnails *sidebar_thumbnails,
353              gint                 start_page,
354              gint                 end_page)
355 {
356         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
357         GtkTreePath *path;
358         GtkTreeIter iter;
359         gboolean result;
360         gint prev_width = -1;
361         gint prev_height = -1;
362
363         g_assert (start_page <= end_page);
364
365         path = gtk_tree_path_new_from_indices (start_page, -1);
366         for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
367              result && start_page <= end_page;
368              result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), start_page ++) {
369                 EvJobThumbnail *job;
370                 GdkPixbuf *loading_icon = NULL;
371                 gint width, height;
372
373                 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store),
374                                     &iter,
375                                     COLUMN_JOB, &job,
376                                     -1);
377
378                 if (job) {
379                         g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, sidebar_thumbnails);
380                         ev_job_cancel (EV_JOB (job));
381                         g_object_unref (job);
382                 }
383
384                 ev_thumbnails_size_cache_get_size (priv->size_cache, start_page,
385                                                   priv->rotation,
386                                                   &width, &height);
387                 if (!loading_icon || (width != prev_width && height != prev_height)) {
388                         loading_icon =
389                                 ev_sidebar_thumbnails_get_loading_icon (sidebar_thumbnails,
390                                                                         width, height);
391                 }
392
393                 prev_width = width;
394                 prev_height = height;
395
396                 gtk_list_store_set (priv->list_store, &iter,
397                                     COLUMN_JOB, NULL,
398                                     COLUMN_THUMBNAIL_SET, FALSE,
399                                     COLUMN_PIXBUF, loading_icon,
400                                     -1);
401         }
402         gtk_tree_path_free (path);
403 }
404
405 static gdouble
406 get_scale_for_page (EvSidebarThumbnails *sidebar_thumbnails,
407                     gint                 page)
408 {
409         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
410         gdouble width;
411
412         ev_document_get_page_size (priv->document, page, &width, NULL);
413
414         return (gdouble)THUMBNAIL_WIDTH / width;
415 }
416
417 static void
418 add_range (EvSidebarThumbnails *sidebar_thumbnails,
419            gint                 start_page,
420            gint                 end_page)
421 {
422         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
423         GtkTreePath *path;
424         GtkTreeIter iter;
425         gboolean result;
426         gint page = start_page;
427
428         g_assert (start_page <= end_page);
429
430         path = gtk_tree_path_new_from_indices (start_page, -1);
431         for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
432              result && page <= end_page;
433              result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), page ++) {
434                 EvJob *job;
435                 gboolean thumbnail_set;
436
437                 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store), &iter,
438                                     COLUMN_JOB, &job,
439                                     COLUMN_THUMBNAIL_SET, &thumbnail_set,
440                                     -1);
441
442                 if (job == NULL && !thumbnail_set) {
443                         job = ev_job_thumbnail_new (priv->document,
444                                                     page, priv->rotation,
445                                                     get_scale_for_page (sidebar_thumbnails, page));
446                         ev_job_scheduler_push_job (EV_JOB (job), EV_JOB_PRIORITY_HIGH);
447                         
448                         g_object_set_data_full (G_OBJECT (job), "tree_iter",
449                                                 gtk_tree_iter_copy (&iter),
450                                                 (GDestroyNotify) gtk_tree_iter_free);
451                         g_signal_connect (job, "finished",
452                                           G_CALLBACK (thumbnail_job_completed_callback),
453                                           sidebar_thumbnails);
454                         gtk_list_store_set (priv->list_store, &iter,
455                                             COLUMN_JOB, job,
456                                             -1);
457                         
458                         /* The queue and the list own a ref to the job now */
459                         g_object_unref (job);
460                 } else if (job) {
461                         g_object_unref (job);
462                 }
463         }
464         gtk_tree_path_free (path);
465 }
466
467 /* This modifies start */
468 static void
469 update_visible_range (EvSidebarThumbnails *sidebar_thumbnails,
470                       gint                 start_page,
471                       gint                 end_page)
472 {
473         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
474         int old_start_page, old_end_page;
475
476         old_start_page = priv->start_page;
477         old_end_page = priv->end_page;
478
479         if (start_page == old_start_page &&
480             end_page == old_end_page)
481                 return;
482
483         /* Clear the areas we no longer display */
484         if (old_start_page >= 0 && old_start_page < start_page)
485                 clear_range (sidebar_thumbnails, old_start_page, MIN (start_page - 1, old_end_page));
486         
487         if (old_end_page > 0 && old_end_page > end_page)
488                 clear_range (sidebar_thumbnails, MAX (end_page + 1, old_start_page), old_end_page);
489
490         add_range (sidebar_thumbnails, start_page, end_page);
491         
492         priv->start_page = start_page;
493         priv->end_page = end_page;
494 }
495
496 static void
497 adjustment_changed_cb (EvSidebarThumbnails *sidebar_thumbnails)
498 {
499         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
500         GtkTreePath *path = NULL;
501         GtkTreePath *path2 = NULL;
502         gint wy1;
503         gint wy2;
504
505         /* Widget is not currently visible */
506         if (!GTK_WIDGET_MAPPED (sidebar_thumbnails))
507                 return;
508
509         if (priv->vadjustment->page_size == 0)
510                 return;
511         
512         if (priv->tree_view) {
513                 if (! gtk_widget_get_realized (priv->tree_view))
514                         return;
515
516                 gtk_tree_view_convert_tree_to_bin_window_coords (GTK_TREE_VIEW (priv->tree_view),
517                                                                  0, (int) priv->vadjustment->value,
518                                                                  NULL, &wy1);
519                 gtk_tree_view_convert_tree_to_bin_window_coords (GTK_TREE_VIEW (priv->tree_view),
520                                                                  0, (int) (priv->vadjustment->value + priv->vadjustment->page_size),
521                                                                  NULL, &wy2);
522                 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
523                                                1, wy1 + 1, &path,
524                                                NULL, NULL, NULL);
525                 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
526                                                1, wy2 -1, &path2,
527                                                NULL, NULL, NULL);
528         } else if (priv->icon_view) {
529                 if (! gtk_widget_get_realized (priv->icon_view))
530                         return;
531                 if (! gtk_icon_view_get_visible_range (GTK_ICON_VIEW (priv->icon_view), &path, &path2))
532                         return;
533         } else {
534                 return;
535         }
536
537         if (path && path2) {
538                 update_visible_range (sidebar_thumbnails,
539                                       gtk_tree_path_get_indices (path)[0],
540                                       gtk_tree_path_get_indices (path2)[0]);
541         }
542
543         gtk_tree_path_free (path);
544         gtk_tree_path_free (path2);
545 }
546
547 static void
548 ev_sidebar_thumbnails_fill_model (EvSidebarThumbnails *sidebar_thumbnails)
549 {
550         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
551         GtkTreeIter iter;
552         int i;
553         gint prev_width = -1;
554         gint prev_height = -1;
555
556         for (i = 0; i < sidebar_thumbnails->priv->n_pages; i++) {
557                 gchar     *page_label;
558                 gchar     *page_string;
559                 GdkPixbuf *loading_icon = NULL;
560                 gint       width, height;
561
562                 page_label = ev_document_get_page_label (priv->document, i);
563                 page_string = g_markup_printf_escaped ("<i>%s</i>", page_label);
564                 ev_thumbnails_size_cache_get_size (sidebar_thumbnails->priv->size_cache, i,
565                                                   sidebar_thumbnails->priv->rotation,
566                                                   &width, &height);
567                 if (!loading_icon || (width != prev_width && height != prev_height)) {
568                         loading_icon =
569                                 ev_sidebar_thumbnails_get_loading_icon (sidebar_thumbnails,
570                                                                         width, height);
571                 }
572
573                 prev_width = width;
574                 prev_height = height;
575                 
576                 gtk_list_store_append (priv->list_store, &iter);
577                 gtk_list_store_set (priv->list_store, &iter,
578                                     COLUMN_PAGE_STRING, page_string,
579                                     COLUMN_PIXBUF, loading_icon,
580                                     COLUMN_THUMBNAIL_SET, FALSE,
581                                     -1);
582                 g_free (page_label);
583                 g_free (page_string);
584         }
585 }
586
587 static void
588 ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
589                                    EvSidebarThumbnails *ev_sidebar_thumbnails)
590 {
591         EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
592         GtkTreePath *path;
593         GtkTreeIter iter;
594         int page;
595
596         if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
597                 return;
598
599         path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
600                                         &iter);
601         page = gtk_tree_path_get_indices (path)[0];
602         gtk_tree_path_free (path);
603
604         ev_document_model_set_page (priv->model, page);
605 }
606
607 static void
608 ev_sidebar_icon_selection_changed (GtkIconView         *icon_view,
609                                    EvSidebarThumbnails *ev_sidebar_thumbnails)
610 {
611         EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
612         GtkTreePath *path;
613         GList *selected;
614         int page;
615
616         selected = gtk_icon_view_get_selected_items (icon_view);
617         if (selected == NULL)
618                 return;
619
620         /* We don't handle or expect multiple selection. */
621         g_assert (selected->next == NULL);
622
623         path = selected->data;
624         page = gtk_tree_path_get_indices (path)[0];
625
626         gtk_tree_path_free (path);
627         g_list_free (selected);
628
629         ev_document_model_set_page (priv->model, page);
630 }
631
632 static void
633 ev_sidebar_init_tree_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
634 {
635         EvSidebarThumbnailsPrivate *priv;
636         GtkTreeSelection *selection;
637         GtkCellRenderer *renderer;
638
639         priv = ev_sidebar_thumbnails->priv;
640         priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
641
642         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
643         g_signal_connect (selection, "changed",
644                           G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
645         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
646         renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
647                                  "xpad", 2,
648                                  "ypad", 2,
649                                  NULL);
650         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
651                                                      NULL, renderer,
652                                                      "pixbuf", 1,
653                                                      NULL);
654         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
655                                                      NULL, gtk_cell_renderer_text_new (),
656                                                      "markup", 0, NULL);
657         gtk_container_add (GTK_CONTAINER (priv->swindow), priv->tree_view);
658         gtk_widget_show (priv->tree_view);
659 }
660
661 static void
662 ev_sidebar_init_icon_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
663 {
664         EvSidebarThumbnailsPrivate *priv;
665
666         priv = ev_sidebar_thumbnails->priv;
667
668         priv->icon_view = gtk_icon_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
669         gtk_icon_view_set_markup_column (GTK_ICON_VIEW (priv->icon_view), 0);
670         gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (priv->icon_view), 1);
671         g_signal_connect (priv->icon_view, "selection-changed",
672                           G_CALLBACK (ev_sidebar_icon_selection_changed), ev_sidebar_thumbnails);
673
674         gtk_container_add (GTK_CONTAINER (priv->swindow), priv->icon_view);
675         gtk_widget_show (priv->icon_view);
676 }
677
678 static gboolean
679 ev_sidebar_thumbnails_use_icon_view (EvSidebarThumbnails *sidebar_thumbnails)
680 {
681         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
682
683         return (ev_document_get_n_pages (priv->document) <= MAX_ICON_VIEW_PAGE_COUNT);
684 }
685
686 static void
687 ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
688 {
689         EvSidebarThumbnailsPrivate *priv;
690
691         priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
692
693         priv->list_store = gtk_list_store_new (NUM_COLUMNS,
694                                                G_TYPE_STRING,
695                                                GDK_TYPE_PIXBUF,
696                                                G_TYPE_BOOLEAN,
697                                                EV_TYPE_JOB_THUMBNAIL);
698
699         priv->swindow = gtk_scrolled_window_new (NULL, NULL);
700         
701         /* We actually don't want GTK_POLICY_AUTOMATIC for horizontal scrollbar here
702          * it's just a workaround for bug #449462
703          */
704         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->swindow),
705                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
706         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->swindow),
707                                              GTK_SHADOW_IN);
708         priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->swindow));
709         g_signal_connect_data (priv->vadjustment, "value-changed",
710                                G_CALLBACK (adjustment_changed_cb),
711                                ev_sidebar_thumbnails, NULL,
712                                G_CONNECT_SWAPPED | G_CONNECT_AFTER);
713         g_signal_connect_swapped (priv->swindow, "size-allocate",
714                                   G_CALLBACK (adjustment_changed_cb),
715                                   ev_sidebar_thumbnails);
716         gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), priv->swindow, TRUE, TRUE, 0);
717
718         /* Put it all together */
719         gtk_widget_show_all (priv->swindow);
720 }
721
722 static void
723 ev_sidebar_thumbnails_set_current_page (EvSidebarThumbnails *sidebar,
724                                         gint                 page)
725 {
726         GtkTreeView *tree_view;
727         GtkTreePath *path;
728
729         path = gtk_tree_path_new_from_indices (page, -1);
730
731         if (sidebar->priv->tree_view) {
732                 tree_view = GTK_TREE_VIEW (sidebar->priv->tree_view);
733                 gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
734                 gtk_tree_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.0, 0.0);
735         } else if (sidebar->priv->icon_view) {
736
737                 g_signal_handlers_block_by_func
738                         (sidebar->priv->icon_view,
739                          G_CALLBACK (ev_sidebar_icon_selection_changed), sidebar);
740
741                 gtk_icon_view_select_path (GTK_ICON_VIEW (sidebar->priv->icon_view), path);
742
743                 g_signal_handlers_unblock_by_func
744                         (sidebar->priv->icon_view,
745                          G_CALLBACK (ev_sidebar_icon_selection_changed), sidebar);
746
747                 gtk_icon_view_set_cursor (GTK_ICON_VIEW (sidebar->priv->icon_view), path, NULL, FALSE);
748         }
749
750         gtk_tree_path_free (path);
751 }
752
753 static void
754 page_changed_cb (EvSidebarThumbnails *sidebar,
755                  gint                 old_page,
756                  gint                 new_page)
757 {
758         ev_sidebar_thumbnails_set_current_page (sidebar, new_page);
759 }
760
761 static gboolean
762 refresh (EvSidebarThumbnails *sidebar_thumbnails)
763 {
764         adjustment_changed_cb (sidebar_thumbnails);
765         return FALSE;
766 }
767
768 static void
769 ev_sidebar_thumbnails_reload (EvSidebarThumbnails *sidebar_thumbnails)
770 {
771         EvDocumentModel *model;
772
773         if (sidebar_thumbnails->priv->loading_icons)
774                 g_hash_table_remove_all (sidebar_thumbnails->priv->loading_icons);
775
776         if (sidebar_thumbnails->priv->document == NULL ||
777             sidebar_thumbnails->priv->n_pages <= 0)
778                 return;
779
780         model = sidebar_thumbnails->priv->model;
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         ev_sidebar_thumbnails_set_current_page (sidebar_thumbnails,
789                                                 ev_document_model_get_page (model));
790         g_idle_add ((GSourceFunc)refresh, sidebar_thumbnails);
791 }
792
793 static void
794 ev_sidebar_thumbnails_rotation_changed_cb (EvDocumentModel     *model,
795                                            GParamSpec          *pspec,
796                                            EvSidebarThumbnails *sidebar_thumbnails)
797 {
798         gint rotation = ev_document_model_get_rotation (model);
799
800         sidebar_thumbnails->priv->rotation = rotation;
801         ev_sidebar_thumbnails_reload (sidebar_thumbnails);
802 }
803
804 static void
805 ev_sidebar_thumbnails_inverted_colors_changed_cb (EvDocumentModel     *model,
806                                                   GParamSpec          *pspec,
807                                                   EvSidebarThumbnails *sidebar_thumbnails)
808 {
809         gboolean inverted_colors = ev_document_model_get_inverted_colors (model);
810
811         sidebar_thumbnails->priv->inverted_colors = inverted_colors;
812         ev_sidebar_thumbnails_reload (sidebar_thumbnails);
813 }
814
815 static void
816 thumbnail_job_completed_callback (EvJobThumbnail      *job,
817                                   EvSidebarThumbnails *sidebar_thumbnails)
818 {
819         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
820         GtkTreeIter *iter;
821
822         iter = (GtkTreeIter *) g_object_get_data (G_OBJECT (job), "tree_iter");
823         if (priv->inverted_colors)
824                 ev_document_misc_invert_pixbuf (job->thumbnail);
825         gtk_list_store_set (priv->list_store,
826                             iter,
827                             COLUMN_PIXBUF, job->thumbnail,
828                             COLUMN_THUMBNAIL_SET, TRUE,
829                             COLUMN_JOB, NULL,
830                             -1);
831 }
832
833 static void
834 ev_sidebar_thumbnails_document_changed_cb (EvDocumentModel     *model,
835                                            GParamSpec          *pspec,
836                                            EvSidebarThumbnails *sidebar_thumbnails)
837 {
838         EvDocument *document = ev_document_model_get_document (model);
839         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
840
841         if (!EV_IS_DOCUMENT_THUMBNAILS (document) ||
842             ev_document_get_n_pages (document) <= 0 ||
843             !ev_document_check_dimensions (document)) {
844                 return;
845         }
846
847         priv->size_cache = ev_thumbnails_size_cache_get (document);
848         priv->document = document;
849         priv->n_pages = ev_document_get_n_pages (document);
850         priv->rotation = ev_document_model_get_rotation (model);
851         priv->inverted_colors = ev_document_model_get_inverted_colors (model);
852         priv->loading_icons = g_hash_table_new_full (g_str_hash,
853                                                      g_str_equal,
854                                                      (GDestroyNotify)g_free,
855                                                      (GDestroyNotify)g_object_unref);
856
857         ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
858         ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
859
860         /* Create the view widget, and remove the old one, if needed */
861         if (ev_sidebar_thumbnails_use_icon_view (sidebar_thumbnails)) {
862                 if (priv->tree_view) {
863                         gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->tree_view);
864                         priv->tree_view = NULL;
865                 }
866
867                 if (! priv->icon_view) {
868                         ev_sidebar_init_icon_view (sidebar_thumbnails);
869                         g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
870                 } else {
871                         gtk_widget_queue_resize (priv->icon_view);
872                 }
873         } else {
874                 if (priv->icon_view) {
875                         gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->icon_view);
876                         priv->icon_view = NULL;
877                 }
878
879                 if (! priv->tree_view) {
880                         ev_sidebar_init_tree_view (sidebar_thumbnails);
881                         g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
882                 }
883         }
884
885         /* Connect to the signal and trigger a fake callback */
886         g_signal_connect_swapped (priv->model, "page-changed",
887                                   G_CALLBACK (page_changed_cb),
888                                   sidebar_thumbnails);
889         g_signal_connect (priv->model, "notify::rotation",
890                           G_CALLBACK (ev_sidebar_thumbnails_rotation_changed_cb),
891                           sidebar_thumbnails);
892         g_signal_connect (priv->model, "notify::inverted-colors",
893                           G_CALLBACK (ev_sidebar_thumbnails_inverted_colors_changed_cb),
894                           sidebar_thumbnails);
895         sidebar_thumbnails->priv->start_page = -1;
896         sidebar_thumbnails->priv->end_page = -1;
897         ev_sidebar_thumbnails_set_current_page (sidebar_thumbnails,
898                                                 ev_document_model_get_page (model));
899         adjustment_changed_cb (sidebar_thumbnails);
900 }
901
902 static void
903 ev_sidebar_thumbnails_set_model (EvSidebarPage   *sidebar_page,
904                                  EvDocumentModel *model)
905 {
906         EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (sidebar_page);
907         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
908
909         if (priv->model == model)
910                 return;
911
912         priv->model = model;
913         g_signal_connect (model, "notify::document",
914                           G_CALLBACK (ev_sidebar_thumbnails_document_changed_cb),
915                           sidebar_page);
916 }
917
918 static gboolean
919 ev_sidebar_thumbnails_clear_job (GtkTreeModel *model,                                             
920                                  GtkTreePath *path,
921                                  GtkTreeIter *iter,
922                                  gpointer data)
923 {
924         EvJob *job;
925         
926         gtk_tree_model_get (model, iter, COLUMN_JOB, &job, -1);
927         
928         if (job != NULL) {
929                 ev_job_cancel (job);
930                 g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, data);
931                 g_object_unref (job);
932         }
933         
934         return FALSE;    
935 }
936
937 static void 
938 ev_sidebar_thumbnails_clear_model (EvSidebarThumbnails *sidebar_thumbnails)
939 {
940         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
941         
942         gtk_tree_model_foreach (GTK_TREE_MODEL (priv->list_store), ev_sidebar_thumbnails_clear_job, sidebar_thumbnails);
943         gtk_list_store_clear (priv->list_store);
944 }
945
946 static gboolean
947 ev_sidebar_thumbnails_support_document (EvSidebarPage   *sidebar_page,
948                                         EvDocument *document)
949 {
950         return (EV_IS_DOCUMENT_THUMBNAILS (document));
951 }
952
953 static const gchar*
954 ev_sidebar_thumbnails_get_label (EvSidebarPage *sidebar_page)
955 {
956         return _("Thumbnails");
957 }
958
959 static void
960 ev_sidebar_thumbnails_page_iface_init (EvSidebarPageIface *iface)
961 {
962         iface->support_document = ev_sidebar_thumbnails_support_document;
963         iface->set_model = ev_sidebar_thumbnails_set_model;
964         iface->get_label = ev_sidebar_thumbnails_get_label;
965 }