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