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