]> www.fi.muni.cz Git - evince.git/blob - shell/ev-sidebar-thumbnails.c
Cleanup many #include statements: reorder so that a generic-to-specific
[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 #include <glib/gi18n.h>
31 #include <gtk/gtk.h>
32
33 #include "ev-sidebar-page.h"
34 #include "ev-sidebar-thumbnails.h"
35 #include "ev-document-thumbnails.h"
36 #include "ev-document-misc.h"
37 #include "ev-job-scheduler.h"
38 #include "ev-window.h"
39 #include "ev-utils.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
48 struct _EvSidebarThumbnailsPrivate {
49         GtkWidget *swindow;
50         GtkWidget *icon_view;
51         GtkWidget *tree_view;
52         GtkAdjustment *vadjustment;
53         GtkListStore *list_store;
54         GdkPixbuf *loading_icon;
55         EvDocument *document;
56         EvPageCache *page_cache;
57
58         gint n_pages, pages_done;
59
60         int rotation;
61
62         /* Visible pages */
63         gint start_page, end_page;
64 };
65
66 enum {
67         COLUMN_PAGE_STRING,
68         COLUMN_PIXBUF,
69         COLUMN_THUMBNAIL_SET,
70         COLUMN_JOB,
71         NUM_COLUMNS
72 };
73
74 enum {
75         PROP_0,
76         PROP_WIDGET,
77 };
78
79 static void         ev_sidebar_thumbnails_clear_model      (EvSidebarThumbnails *sidebar);
80 static gboolean     ev_sidebar_thumbnails_support_document (EvSidebarPage       *sidebar_page,
81                                                             EvDocument          *document);
82 static void         ev_sidebar_thumbnails_page_iface_init  (EvSidebarPageIface  *iface);
83 static void         ev_sidebar_thumbnails_set_document     (EvSidebarPage       *sidebar_page,
84                                                             EvDocument          *document);
85 static const gchar* ev_sidebar_thumbnails_get_label        (EvSidebarPage       *sidebar_page);
86 static void         thumbnail_job_completed_callback       (EvJobThumbnail      *job,
87                                                             EvSidebarThumbnails *sidebar_thumbnails);
88 static void         adjustment_changed_cb                  (EvSidebarThumbnails *sidebar_thumbnails);
89
90 G_DEFINE_TYPE_EXTENDED (EvSidebarThumbnails, 
91                         ev_sidebar_thumbnails, 
92                         GTK_TYPE_VBOX,
93                         0, 
94                         G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE, 
95                                                ev_sidebar_thumbnails_page_iface_init))
96
97 #define EV_SIDEBAR_THUMBNAILS_GET_PRIVATE(object) \
98         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_THUMBNAILS, EvSidebarThumbnailsPrivate));
99
100
101 static void
102 ev_sidebar_thumbnails_dispose (GObject *object)
103 {
104         EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (object);
105         
106         if (sidebar_thumbnails->priv->loading_icon) {
107                 g_object_unref (sidebar_thumbnails->priv->loading_icon);
108                 sidebar_thumbnails->priv->loading_icon = NULL;
109         }
110         
111         if (sidebar_thumbnails->priv->list_store) {
112                 ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
113                 g_object_unref (sidebar_thumbnails->priv->list_store);
114                 sidebar_thumbnails->priv->list_store = NULL;
115         }
116         
117         G_OBJECT_CLASS (ev_sidebar_thumbnails_parent_class)->dispose (object);
118 }
119
120 static void
121 ev_sidebar_thumbnails_get_property (GObject    *object,
122                                     guint       prop_id,
123                                     GValue     *value,
124                                     GParamSpec *pspec)
125 {
126         EvSidebarThumbnails *sidebar = EV_SIDEBAR_THUMBNAILS (object);
127
128         switch (prop_id) {
129         case PROP_WIDGET:
130                 if (sidebar->priv->tree_view)
131                         g_value_set_object (value, sidebar->priv->tree_view);
132                 else
133                         g_value_set_object (value, sidebar->priv->icon_view);
134                 break;
135         default:
136                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
137                 break;
138         }
139 }
140
141 static void
142 ev_sidebar_thumbnails_map (GtkWidget *widget)
143 {
144         EvSidebarThumbnails *sidebar;
145
146         sidebar = EV_SIDEBAR_THUMBNAILS (widget);
147
148         GTK_WIDGET_CLASS (ev_sidebar_thumbnails_parent_class)->map (widget);
149         
150         adjustment_changed_cb (sidebar);
151 }
152
153 static void
154 ev_sidebar_thumbnails_class_init (EvSidebarThumbnailsClass *ev_sidebar_thumbnails_class)
155 {
156         GObjectClass *g_object_class;
157         GtkObjectClass *gtk_object_class;
158         GtkWidgetClass *widget_class;
159
160         g_object_class = G_OBJECT_CLASS (ev_sidebar_thumbnails_class);
161         gtk_object_class = GTK_OBJECT_CLASS (ev_sidebar_thumbnails_class);
162         widget_class = GTK_WIDGET_CLASS (ev_sidebar_thumbnails_class);
163
164         g_object_class->dispose = ev_sidebar_thumbnails_dispose;
165         g_object_class->get_property = ev_sidebar_thumbnails_get_property;
166         widget_class->map = ev_sidebar_thumbnails_map;
167
168         g_object_class_override_property (g_object_class,
169                                           PROP_WIDGET,
170                                           "main-widget");
171
172         g_type_class_add_private (g_object_class, sizeof (EvSidebarThumbnailsPrivate));
173 }
174
175 GtkWidget *
176 ev_sidebar_thumbnails_new (void)
177 {
178         GtkWidget *ev_sidebar_thumbnails;
179
180         ev_sidebar_thumbnails = g_object_new (EV_TYPE_SIDEBAR_THUMBNAILS, NULL);
181
182         return ev_sidebar_thumbnails;
183 }
184
185 static void
186 clear_range (EvSidebarThumbnails *sidebar_thumbnails,
187              gint                 start_page,
188              gint                 end_page)
189 {
190         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
191         GtkTreePath *path;
192         GtkTreeIter iter;
193         gboolean result;
194
195         g_assert (start_page <= end_page);
196
197         path = gtk_tree_path_new_from_indices (start_page, -1);
198         for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
199              result && start_page <= end_page;
200              result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), start_page ++) {
201                 EvJobThumbnail *job;
202
203                 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store),
204                                     &iter,
205                                     COLUMN_JOB, &job,
206                                     -1);
207
208                 if (job) {
209                         g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, sidebar_thumbnails);
210                         ev_job_cancel (EV_JOB (job));
211                         g_object_unref (job);
212                 }
213
214                 gtk_list_store_set (priv->list_store, &iter,
215                                     COLUMN_JOB, NULL,
216                                     COLUMN_THUMBNAIL_SET, FALSE,
217                                     COLUMN_PIXBUF, priv->loading_icon,
218                                     -1);
219         }
220         gtk_tree_path_free (path);
221 }
222
223 static gdouble
224 get_scale_for_page (EvSidebarThumbnails *sidebar_thumbnails,
225                     gint                 page)
226 {
227         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
228         gint width, height;
229
230         ev_page_cache_get_size (priv->page_cache,
231                                 page, 0,
232                                 1.0, &width, &height);
233         
234         return (gdouble)THUMBNAIL_WIDTH / (gdouble)width;
235 }
236
237 static void
238 add_range (EvSidebarThumbnails *sidebar_thumbnails,
239            gint                 start_page,
240            gint                 end_page)
241 {
242         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
243         GtkTreePath *path;
244         GtkTreeIter iter;
245         gboolean result;
246         gint page = start_page;
247
248         g_assert (start_page <= end_page);
249
250         path = gtk_tree_path_new_from_indices (start_page, -1);
251         for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
252              result && page <= end_page;
253              result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), page ++) {
254                 EvJob *job;
255                 gboolean thumbnail_set;
256
257                 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store), &iter,
258                                     COLUMN_JOB, &job,
259                                     COLUMN_THUMBNAIL_SET, &thumbnail_set,
260                                     -1);
261
262                 if (job == NULL && !thumbnail_set) {
263                         job = ev_job_thumbnail_new (priv->document,
264                                                     page, priv->rotation,
265                                                     get_scale_for_page (sidebar_thumbnails, page));
266                         ev_job_scheduler_push_job (EV_JOB (job), EV_JOB_PRIORITY_HIGH);
267                         
268                         g_object_set_data_full (G_OBJECT (job), "tree_iter",
269                                                 gtk_tree_iter_copy (&iter),
270                                                 (GDestroyNotify) gtk_tree_iter_free);
271                         g_signal_connect (job, "finished",
272                                           G_CALLBACK (thumbnail_job_completed_callback),
273                                           sidebar_thumbnails);
274                         gtk_list_store_set (priv->list_store, &iter,
275                                             COLUMN_JOB, job,
276                                             -1);
277                         
278                         /* The queue and the list own a ref to the job now */
279                         g_object_unref (job);
280                 } else if (job) {
281                         g_object_unref (job);
282                 }
283         }
284         gtk_tree_path_free (path);
285 }
286
287 /* This modifies start */
288 static void
289 update_visible_range (EvSidebarThumbnails *sidebar_thumbnails,
290                       gint                 start_page,
291                       gint                 end_page)
292 {
293         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
294         int old_start_page, old_end_page;
295
296         old_start_page = priv->start_page;
297         old_end_page = priv->end_page;
298
299         if (start_page == old_start_page &&
300             end_page == old_end_page)
301                 return;
302
303         /* Clear the areas we no longer display */
304         if (old_start_page >= 0 && old_start_page < start_page)
305                 clear_range (sidebar_thumbnails, old_start_page, MIN (start_page - 1, old_end_page));
306         
307         if (old_end_page > 0 && old_end_page > end_page)
308                 clear_range (sidebar_thumbnails, MAX (end_page + 1, old_start_page), old_end_page);
309
310         add_range (sidebar_thumbnails, start_page, end_page);
311         
312         priv->start_page = start_page;
313         priv->end_page = end_page;
314 }
315
316 static void
317 adjustment_changed_cb (EvSidebarThumbnails *sidebar_thumbnails)
318 {
319         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
320         GtkTreePath *path = NULL;
321         GtkTreePath *path2 = NULL;
322         gint wy1;
323         gint wy2;
324
325         /* Widget is not currently visible */
326         if (!GTK_WIDGET_MAPPED (sidebar_thumbnails))
327                 return;
328         
329         if (priv->tree_view) {
330                 if (! GTK_WIDGET_REALIZED (priv->tree_view))
331                         return;
332
333 #if GTK_CHECK_VERSION (2, 11, 3)
334                 gtk_tree_view_convert_widget_to_bin_window_coords (GTK_TREE_VIEW (priv->tree_view),
335                                                                    0, (int) priv->vadjustment->value,
336                                                                    NULL, &wy1);
337                 gtk_tree_view_convert_widget_to_bin_window_coords (GTK_TREE_VIEW (priv->tree_view),
338                                                                    0, (int) (priv->vadjustment->value + priv->vadjustment->page_size),
339                                                                    NULL, &wy2);
340 #else
341                 gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
342                                                      0, (int) priv->vadjustment->value,
343                                                      NULL, &wy1);
344                 gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
345                                                      0, (int) (priv->vadjustment->value + priv->vadjustment->page_size),
346                                                      NULL, &wy2);
347 #endif /* GTK_CHECK_VERSION (2, 11, 3) */
348                 
349                 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
350                                                1, wy1 + 1, &path,
351                                                NULL, NULL, NULL);
352                 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
353                                                1, wy2 -1, &path2,
354                                                NULL, NULL, NULL);
355         } else if (priv->icon_view) {
356                 if (! GTK_WIDGET_REALIZED (priv->icon_view))
357                         return;
358                 if (! gtk_icon_view_get_visible_range (GTK_ICON_VIEW (priv->icon_view), &path, &path2))
359                         return;
360         } else {
361                 return;
362         }
363
364         if (path && path2) {
365                 update_visible_range (sidebar_thumbnails,
366                                       gtk_tree_path_get_indices (path)[0],
367                                       gtk_tree_path_get_indices (path2)[0]);
368         }
369
370         gtk_tree_path_free (path);
371         gtk_tree_path_free (path2);
372 }
373
374 static void
375 ev_sidebar_thumbnails_fill_model (EvSidebarThumbnails *sidebar_thumbnails)
376 {
377         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
378         GtkTreeIter iter;
379         int i;
380
381         for (i = 0; i < sidebar_thumbnails->priv->n_pages; i++) {
382                 gchar *page_label;
383                 gchar *page_string;
384
385                 page_label = ev_page_cache_get_page_label (priv->page_cache, i);
386                 page_string = g_markup_printf_escaped ("<i>%s</i>", page_label);
387
388                 gtk_list_store_append (priv->list_store, &iter);
389                 gtk_list_store_set (priv->list_store, &iter,
390                                     COLUMN_PAGE_STRING, page_string,
391                                     COLUMN_PIXBUF, priv->loading_icon,
392                                     COLUMN_THUMBNAIL_SET, FALSE,
393                                     -1);
394                 g_free (page_label);
395                 g_free (page_string);
396         }
397 }
398
399
400 static void
401 ev_sidebar_thumbnails_set_loading_icon (EvSidebarThumbnails *sidebar_thumbnails)
402 {
403         if (sidebar_thumbnails->priv->loading_icon)
404                 g_object_unref (sidebar_thumbnails->priv->loading_icon);
405
406         if (sidebar_thumbnails->priv->document &&
407             sidebar_thumbnails->priv->n_pages > 0) {
408                 gint width, height;
409
410                 /* We get the dimensions of the first page so that we can make a blank
411                  * icon.  */
412                 ev_page_cache_get_thumbnail_size (sidebar_thumbnails->priv->page_cache, 0,
413                                                   sidebar_thumbnails->priv->rotation,
414                                                   &width, &height);
415
416                 sidebar_thumbnails->priv->loading_icon =
417                         ev_document_misc_get_thumbnail_frame (width, height, NULL);
418         } else {
419                 sidebar_thumbnails->priv->loading_icon = NULL;
420         }
421
422 }
423
424 static gboolean
425 refresh (EvSidebarThumbnails *sidebar_thumbnails)
426 {
427         adjustment_changed_cb (sidebar_thumbnails);
428         return FALSE;
429 }
430
431 void
432 ev_sidebar_thumbnails_refresh (EvSidebarThumbnails *sidebar_thumbnails,
433                                int                  rotation)
434 {
435         sidebar_thumbnails->priv->rotation = rotation;
436         ev_sidebar_thumbnails_set_loading_icon (sidebar_thumbnails);
437
438         if (sidebar_thumbnails->priv->document == NULL ||
439             sidebar_thumbnails->priv->n_pages <= 0)
440                 return;
441
442         ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
443         ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
444
445         /* Trigger a redraw */
446         sidebar_thumbnails->priv->start_page = -1;
447         sidebar_thumbnails->priv->end_page = -1;
448         g_idle_add ((GSourceFunc)refresh, sidebar_thumbnails);
449 }
450
451 static void
452 ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
453                                    EvSidebarThumbnails *ev_sidebar_thumbnails)
454 {
455         EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
456         GtkTreePath *path;
457         GtkTreeIter iter;
458         int page;
459
460         if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
461                 return;
462
463         path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
464                                         &iter);
465         page = gtk_tree_path_get_indices (path)[0];
466         gtk_tree_path_free (path);
467
468         ev_page_cache_set_current_page_history (priv->page_cache, page);
469 }
470
471 static void
472 ev_sidebar_icon_selection_changed (GtkIconView         *icon_view,
473                                    EvSidebarThumbnails *ev_sidebar_thumbnails)
474 {
475         EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
476         GtkTreePath *path;
477         GList *selected;
478         int page;
479
480         selected = gtk_icon_view_get_selected_items (icon_view);
481         if (selected == NULL)
482                 return;
483
484         /* We don't handle or expect multiple selection. */
485         g_assert (selected->next == NULL);
486
487         path = selected->data;
488         page = gtk_tree_path_get_indices (path)[0];
489
490         gtk_tree_path_free (path);
491         g_list_free (selected);
492
493         ev_page_cache_set_current_page_history (priv->page_cache, page);
494 }
495
496 static void
497 ev_sidebar_init_tree_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
498 {
499         EvSidebarThumbnailsPrivate *priv;
500         GtkTreeSelection *selection;
501         GtkCellRenderer *renderer;
502
503         priv = ev_sidebar_thumbnails->priv;
504         priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
505
506         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
507         g_signal_connect (selection, "changed",
508                           G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
509         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
510         renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
511                                  "xpad", 2,
512                                  "ypad", 2,
513                                  NULL);
514         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
515                                                      NULL, renderer,
516                                                      "pixbuf", 1,
517                                                      NULL);
518         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
519                                                      NULL, gtk_cell_renderer_text_new (),
520                                                      "markup", 0, NULL);
521         gtk_container_add (GTK_CONTAINER (priv->swindow), priv->tree_view);
522         gtk_widget_show (priv->tree_view);
523 }
524
525 static void
526 ev_sidebar_init_icon_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
527 {
528         EvSidebarThumbnailsPrivate *priv;
529
530         priv = ev_sidebar_thumbnails->priv;
531
532         priv->icon_view = gtk_icon_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
533         gtk_icon_view_set_markup_column (GTK_ICON_VIEW (priv->icon_view), 0);
534         gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (priv->icon_view), 1);
535         g_signal_connect (priv->icon_view, "selection-changed",
536                           G_CALLBACK (ev_sidebar_icon_selection_changed), ev_sidebar_thumbnails);
537
538         gtk_container_add (GTK_CONTAINER (priv->swindow), priv->icon_view);
539         gtk_widget_show (priv->icon_view);
540 }
541
542 static gboolean
543 ev_sidebar_thumbnails_use_icon_view (EvSidebarThumbnails *sidebar_thumbnails)
544 {
545         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
546         if (ev_page_cache_get_n_pages (priv->page_cache) > MAX_ICON_VIEW_PAGE_COUNT)
547                 return FALSE;
548         return TRUE;
549 }
550
551 static void
552 ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
553 {
554         EvSidebarThumbnailsPrivate *priv;
555
556         priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
557
558         priv->list_store = gtk_list_store_new (NUM_COLUMNS,
559                                                G_TYPE_STRING,
560                                                GDK_TYPE_PIXBUF,
561                                                G_TYPE_BOOLEAN,
562                                                EV_TYPE_JOB_THUMBNAIL);
563
564         priv->swindow = gtk_scrolled_window_new (NULL, NULL);
565         
566         /* We actually don't want GTK_POLICY_AUTOMATIC for horizontal scrollbar here
567          * it's just a workaround for bug #449462
568          */
569         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->swindow),
570                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
571         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->swindow),
572                                              GTK_SHADOW_IN);
573         priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->swindow));
574         g_signal_connect_data (G_OBJECT (priv->vadjustment), "value-changed",
575                                G_CALLBACK (adjustment_changed_cb),
576                                ev_sidebar_thumbnails, NULL,
577                                G_CONNECT_SWAPPED | G_CONNECT_AFTER);
578         g_signal_connect_swapped (G_OBJECT (priv->swindow), "size-allocate",
579                                   G_CALLBACK (adjustment_changed_cb),
580                                   ev_sidebar_thumbnails);
581         gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), priv->swindow, TRUE, TRUE, 0);
582
583         /* Put it all together */
584         gtk_widget_show_all (priv->swindow);
585 }
586
587 static void
588 page_changed_cb (EvPageCache         *page_cache,
589                  int                  page,
590                  EvSidebarThumbnails *sidebar)
591 {
592         GtkTreeView *tree_view;
593         GtkTreePath *path;
594
595         path = gtk_tree_path_new_from_indices (page, -1);
596
597         if (sidebar->priv->tree_view) {
598                 tree_view = GTK_TREE_VIEW (sidebar->priv->tree_view);
599                 gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
600                 gtk_tree_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.0, 0.0);
601         } else if (sidebar->priv->icon_view) {
602
603                 g_signal_handlers_block_by_func
604                         (sidebar->priv->icon_view,
605                          G_CALLBACK (ev_sidebar_icon_selection_changed), sidebar);
606
607                 gtk_icon_view_select_path (GTK_ICON_VIEW (sidebar->priv->icon_view), path);
608
609                 g_signal_handlers_unblock_by_func
610                         (sidebar->priv->icon_view,
611                          G_CALLBACK (ev_sidebar_icon_selection_changed), sidebar);
612
613                 gtk_icon_view_set_cursor (GTK_ICON_VIEW (sidebar->priv->icon_view), path, NULL, FALSE);
614         }
615
616         gtk_tree_path_free (path);
617 }
618
619 static void
620 thumbnail_job_completed_callback (EvJobThumbnail      *job,
621                                   EvSidebarThumbnails *sidebar_thumbnails)
622 {
623         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
624         GtkTreeIter *iter;
625
626         iter = (GtkTreeIter *) g_object_get_data (G_OBJECT (job), "tree_iter");
627         gtk_list_store_set (priv->list_store,
628                             iter,
629                             COLUMN_PIXBUF, job->thumbnail,
630                             COLUMN_THUMBNAIL_SET, TRUE,
631                             COLUMN_JOB, NULL,
632                             -1);
633 }
634
635 static void
636 ev_sidebar_thumbnails_set_document (EvSidebarPage       *sidebar_page,
637                                     EvDocument          *document)
638 {
639         EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (sidebar_page);
640
641         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
642
643         g_return_if_fail (EV_IS_DOCUMENT_THUMBNAILS (document));
644
645         priv->page_cache = ev_page_cache_get (document);
646         priv->document = document;
647         priv->n_pages = ev_page_cache_get_n_pages (priv->page_cache);
648
649         ev_sidebar_thumbnails_set_loading_icon (sidebar_thumbnails);
650
651         ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
652         ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
653
654         /* Create the view widget, and remove the old one, if needed */
655         if (ev_sidebar_thumbnails_use_icon_view (sidebar_thumbnails)) {
656                 if (priv->tree_view) {
657                         gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->tree_view);
658                         priv->tree_view = NULL;
659                 }
660
661                 if (! priv->icon_view) {
662                         ev_sidebar_init_icon_view (sidebar_thumbnails);
663                         g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
664                 } else {
665                         gtk_widget_queue_resize (priv->icon_view);
666                 }
667         } else {
668                 if (priv->icon_view) {
669                         gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->icon_view);
670                         priv->icon_view = NULL;
671                 }
672
673                 if (! priv->tree_view) {
674                         ev_sidebar_init_tree_view (sidebar_thumbnails);
675                         g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
676                 }
677         }
678
679         /* Connect to the signal and trigger a fake callback */
680         g_signal_connect (priv->page_cache, "page-changed", G_CALLBACK (page_changed_cb), sidebar_thumbnails);
681         sidebar_thumbnails->priv->start_page = -1;
682         sidebar_thumbnails->priv->end_page = -1;
683         page_changed_cb (priv->page_cache,
684                          ev_page_cache_get_current_page (priv->page_cache),
685                          sidebar_thumbnails);
686         adjustment_changed_cb (sidebar_thumbnails);
687 }
688
689 static gboolean
690 ev_sidebar_thumbnails_clear_job (GtkTreeModel *model,                                             
691                                  GtkTreePath *path,
692                                  GtkTreeIter *iter,
693                                  gpointer data)
694 {
695         EvJob *job;
696         
697         gtk_tree_model_get (model, iter, COLUMN_JOB, &job, -1);
698         
699         if (job != NULL) {
700                 ev_job_cancel (job);
701                 g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, data);
702                 g_object_unref (job);
703         }
704         
705         return FALSE;    
706 }
707
708 static void 
709 ev_sidebar_thumbnails_clear_model (EvSidebarThumbnails *sidebar_thumbnails)
710 {
711         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
712         
713         gtk_tree_model_foreach (GTK_TREE_MODEL (priv->list_store), ev_sidebar_thumbnails_clear_job, sidebar_thumbnails);
714         gtk_list_store_clear (priv->list_store);
715 }
716
717 static gboolean
718 ev_sidebar_thumbnails_support_document (EvSidebarPage   *sidebar_page,
719                                         EvDocument *document)
720 {
721         return (EV_IS_DOCUMENT_THUMBNAILS (document));
722 }
723
724 static const gchar*
725 ev_sidebar_thumbnails_get_label (EvSidebarPage *sidebar_page)
726 {
727         return _("Thumbnails");
728 }
729
730 static void
731 ev_sidebar_thumbnails_page_iface_init (EvSidebarPageIface *iface)
732 {
733         iface->support_document = ev_sidebar_thumbnails_support_document;
734         iface->set_document = ev_sidebar_thumbnails_set_document;
735         iface->get_label = ev_sidebar_thumbnails_get_label;
736 }
737