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