]> www.fi.muni.cz Git - evince.git/blob - shell/ev-sidebar-thumbnails.c
aa83dee4a4dc370d123a246ff95133df37ae1ce5
[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 <gtk/gtk.h>
31 #include <glib/gi18n.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-queue.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_queue_remove_job (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 void
224 add_range (EvSidebarThumbnails *sidebar_thumbnails,
225            gint                 start_page,
226            gint                 end_page)
227 {
228         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
229         GtkTreePath *path;
230         GtkTreeIter iter;
231         gboolean result;
232         gint page = start_page;
233
234         g_assert (start_page <= end_page);
235
236         path = gtk_tree_path_new_from_indices (start_page, -1);
237         for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
238              result && page <= end_page;
239              result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), page ++) {
240                 EvJobThumbnail *job;
241                 gboolean thumbnail_set;
242
243                 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store), &iter,
244                                     COLUMN_JOB, &job,
245                                     COLUMN_THUMBNAIL_SET, &thumbnail_set,
246                                     -1);
247
248                 if (job == NULL && !thumbnail_set) {
249                         /* FIXME: Need rotation */
250                         job = (EvJobThumbnail *)ev_job_thumbnail_new (priv->document, page, priv->rotation, THUMBNAIL_WIDTH);
251                         ev_job_queue_add_job (EV_JOB (job), EV_JOB_PRIORITY_HIGH);
252                         g_object_set_data_full (G_OBJECT (job), "tree_iter",
253                                                 gtk_tree_iter_copy (&iter),
254                                                 (GDestroyNotify) gtk_tree_iter_free);
255                         g_signal_connect (job, "finished",
256                                           G_CALLBACK (thumbnail_job_completed_callback),
257                                           sidebar_thumbnails);
258                         gtk_list_store_set (priv->list_store, &iter,
259                                             COLUMN_JOB, job,
260                                             -1);
261                         /* The queue and the list own a ref to the job now */
262                         g_object_unref (job);
263                 } else if (job) {
264                         g_object_unref (job);
265                 }
266         }
267         gtk_tree_path_free (path);
268 }
269
270 /* This modifies start */
271 static void
272 update_visible_range (EvSidebarThumbnails *sidebar_thumbnails,
273                       gint                 start_page,
274                       gint                 end_page)
275 {
276         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
277         int old_start_page, old_end_page;
278
279         old_start_page = priv->start_page;
280         old_end_page = priv->end_page;
281
282         if (start_page == old_start_page &&
283             end_page == old_end_page)
284                 return;
285
286         /* Clear the areas we no longer display */
287         if (old_start_page < start_page)
288                 clear_range (sidebar_thumbnails, old_start_page, MIN (start_page - 1, old_end_page));
289
290         if (old_end_page > end_page)
291                 clear_range (sidebar_thumbnails, MAX (end_page + 1, old_start_page), old_end_page);
292
293         add_range (sidebar_thumbnails, start_page, end_page);
294         
295         priv->start_page = start_page;
296         priv->end_page = end_page;
297 }
298
299 static void
300 adjustment_changed_cb (EvSidebarThumbnails *sidebar_thumbnails)
301 {
302         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
303         GtkTreePath *path = NULL;
304         GtkTreePath *path2 = NULL;
305         gint wy1;
306         gint wy2;
307
308         /* Widget is not currently visible */
309         if (!GTK_WIDGET_MAPPED (sidebar_thumbnails))
310                 return;
311         
312         if (priv->tree_view) {
313                 if (! GTK_WIDGET_REALIZED (priv->tree_view))
314                         return;
315
316                 gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
317                                                      0, (int) priv->vadjustment->value,
318                                                      NULL, &wy1);
319                 gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
320                                                      0, (int) (priv->vadjustment->value + priv->vadjustment->page_size),
321                                                      NULL, &wy2);
322                 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
323                                                1, wy1 + 1, &path,
324                                                NULL, NULL, NULL);
325                 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
326                                                1, wy2 -1, &path2,
327                                                NULL, NULL, NULL);
328         } else if (priv->icon_view) {
329                 if (! GTK_WIDGET_REALIZED (priv->icon_view))
330                         return;
331                 if (! gtk_icon_view_get_visible_range (GTK_ICON_VIEW (priv->icon_view), &path, &path2))
332                         return;
333         } else {
334                 return;
335         }
336
337         if (path == NULL)
338                 path = gtk_tree_path_new_first ();
339         if (path2 == NULL)
340                 path2 = gtk_tree_path_new_from_indices (priv->n_pages,
341                                                         -1);
342         update_visible_range (sidebar_thumbnails,
343                               gtk_tree_path_get_indices (path)[0],
344                               gtk_tree_path_get_indices (path2)[0]);
345
346         gtk_tree_path_free (path);
347         gtk_tree_path_free (path2);
348 }
349
350 static void
351 ev_sidebar_thumbnails_fill_model (EvSidebarThumbnails *sidebar_thumbnails)
352 {
353         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
354         GtkTreeIter iter;
355         int i;
356
357         for (i = 0; i < sidebar_thumbnails->priv->n_pages; i++) {
358                 gchar *page_label;
359                 gchar *page_string;
360
361                 page_label = ev_page_cache_get_page_label (priv->page_cache, i);
362                 page_string = g_markup_printf_escaped ("<i>%s</i>", page_label);
363
364                 gtk_list_store_append (priv->list_store, &iter);
365                 gtk_list_store_set (priv->list_store, &iter,
366                                     COLUMN_PAGE_STRING, page_string,
367                                     COLUMN_PIXBUF, priv->loading_icon,
368                                     COLUMN_THUMBNAIL_SET, FALSE,
369                                     -1);
370                 g_free (page_label);
371                 g_free (page_string);
372         }
373 }
374
375
376 static void
377 ev_sidebar_thumbnails_set_loading_icon (EvSidebarThumbnails *sidebar_thumbnails)
378 {
379         gint width = THUMBNAIL_WIDTH;
380         gint height = THUMBNAIL_WIDTH;
381
382         if (sidebar_thumbnails->priv->loading_icon)
383                 g_object_unref (sidebar_thumbnails->priv->loading_icon);
384
385         if (sidebar_thumbnails->priv->document) {
386                 /* We get the dimensions of the first doc so that we can make a blank
387                  * icon.  */
388                 ev_document_doc_mutex_lock ();
389                 ev_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (sidebar_thumbnails->priv->document),
390                                                        0, THUMBNAIL_WIDTH, &width, &height);
391                 ev_document_doc_mutex_unlock ();
392                 sidebar_thumbnails->priv->loading_icon =
393                         ev_document_misc_get_thumbnail_frame (width, height, sidebar_thumbnails->priv->rotation, NULL);
394         } else {
395                 sidebar_thumbnails->priv->loading_icon = NULL;
396         }
397
398 }
399
400 void
401 ev_sidebar_thumbnails_refresh (EvSidebarThumbnails *sidebar_thumbnails,
402                                int                  rotation)
403 {
404         sidebar_thumbnails->priv->rotation = rotation;
405         ev_sidebar_thumbnails_set_loading_icon (sidebar_thumbnails);
406
407         if (sidebar_thumbnails->priv->document == NULL)
408                 return;
409
410         ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
411         ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
412
413         /* Trigger a redraw */
414         sidebar_thumbnails->priv->start_page = 0;
415         sidebar_thumbnails->priv->end_page = 0;
416         adjustment_changed_cb (sidebar_thumbnails);
417 }
418
419 static void
420 ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
421                                    EvSidebarThumbnails *ev_sidebar_thumbnails)
422 {
423         EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
424         GtkTreePath *path;
425         GtkTreeIter iter;
426         int page;
427
428         if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
429                 return;
430
431         path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
432                                         &iter);
433         page = gtk_tree_path_get_indices (path)[0];
434         gtk_tree_path_free (path);
435
436         ev_page_cache_set_current_page_history (priv->page_cache, page);
437 }
438
439 static void
440 ev_sidebar_icon_selection_changed (GtkIconView         *icon_view,
441                                    EvSidebarThumbnails *ev_sidebar_thumbnails)
442 {
443         EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
444         GtkTreePath *path;
445         GList *selected;
446         int page;
447
448         selected = gtk_icon_view_get_selected_items (icon_view);
449         if (selected == NULL)
450                 return;
451
452         /* We don't handle or expect multiple selection. */
453         g_assert (selected->next == NULL);
454
455         path = selected->data;
456         page = gtk_tree_path_get_indices (path)[0];
457
458         gtk_tree_path_free (path);
459         g_list_free (selected);
460
461         ev_page_cache_set_current_page_history (priv->page_cache, page);
462 }
463
464 static void
465 ev_sidebar_init_tree_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
466 {
467         EvSidebarThumbnailsPrivate *priv;
468         GtkTreeSelection *selection;
469         GtkCellRenderer *renderer;
470
471         priv = ev_sidebar_thumbnails->priv;
472         priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
473
474         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
475         g_signal_connect (selection, "changed",
476                           G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
477         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
478         renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
479                                  "xpad", 2,
480                                  "ypad", 2,
481                                  NULL);
482         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
483                                                      NULL, renderer,
484                                                      "pixbuf", 1,
485                                                      NULL);
486         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
487                                                      NULL, gtk_cell_renderer_text_new (),
488                                                      "markup", 0, NULL);
489         gtk_container_add (GTK_CONTAINER (priv->swindow), priv->tree_view);
490         gtk_widget_show (priv->tree_view);
491 }
492
493 static void
494 ev_sidebar_init_icon_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
495 {
496         EvSidebarThumbnailsPrivate *priv;
497
498         priv = ev_sidebar_thumbnails->priv;
499
500         priv->icon_view = gtk_icon_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
501         gtk_icon_view_set_markup_column (GTK_ICON_VIEW (priv->icon_view), 0);
502         gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (priv->icon_view), 1);
503         g_signal_connect (priv->icon_view, "selection-changed",
504                           G_CALLBACK (ev_sidebar_icon_selection_changed), ev_sidebar_thumbnails);
505
506         gtk_container_add (GTK_CONTAINER (priv->swindow), priv->icon_view);
507         gtk_widget_show (priv->icon_view);
508 }
509
510 static gboolean
511 ev_sidebar_thumbnails_use_icon_view (EvSidebarThumbnails *sidebar_thumbnails)
512 {
513         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
514         if (ev_page_cache_get_n_pages (priv->page_cache) > MAX_ICON_VIEW_PAGE_COUNT)
515                 return FALSE;
516         return TRUE;
517 }
518
519 static void
520 ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
521 {
522         EvSidebarThumbnailsPrivate *priv;
523
524         priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
525
526         priv->list_store = gtk_list_store_new (NUM_COLUMNS,
527                                                G_TYPE_STRING,
528                                                GDK_TYPE_PIXBUF,
529                                                G_TYPE_BOOLEAN,
530                                                EV_TYPE_JOB_THUMBNAIL);
531
532         priv->swindow = gtk_scrolled_window_new (NULL, NULL);
533         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->swindow),
534                                         GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
535         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->swindow),
536                                              GTK_SHADOW_IN);
537         priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->swindow));
538         g_signal_connect_data (G_OBJECT (priv->vadjustment), "value-changed",
539                                G_CALLBACK (adjustment_changed_cb),
540                                ev_sidebar_thumbnails, NULL,
541                                G_CONNECT_SWAPPED | G_CONNECT_AFTER);
542         g_signal_connect_swapped (G_OBJECT (priv->swindow), "size-allocate",
543                                   G_CALLBACK (adjustment_changed_cb),
544                                   ev_sidebar_thumbnails);
545         gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), priv->swindow, TRUE, TRUE, 0);
546
547         /* Put it all together */
548         gtk_widget_show_all (priv->swindow);
549 }
550
551 static void
552 page_changed_cb (EvPageCache         *page_cache,
553                  int                  page,
554                  EvSidebarThumbnails *sidebar)
555 {
556         GtkTreeView *tree_view;
557         GtkTreePath *path;
558
559         path = gtk_tree_path_new_from_indices (page, -1);
560
561         if (sidebar->priv->tree_view) {
562                 tree_view = GTK_TREE_VIEW (sidebar->priv->tree_view);
563                 gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
564                 gtk_tree_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.0, 0.0);
565         } else if (sidebar->priv->icon_view) {
566
567                 g_signal_handlers_block_by_func
568                         (sidebar->priv->icon_view,
569                          G_CALLBACK (ev_sidebar_icon_selection_changed), sidebar);
570
571                 gtk_icon_view_select_path (GTK_ICON_VIEW (sidebar->priv->icon_view), path);
572
573                 g_signal_handlers_unblock_by_func
574                         (sidebar->priv->icon_view,
575                          G_CALLBACK (ev_sidebar_icon_selection_changed), sidebar);
576
577                 gtk_icon_view_set_cursor (GTK_ICON_VIEW (sidebar->priv->icon_view), path, NULL, FALSE);
578         }
579
580         gtk_tree_path_free (path);
581 }
582
583 static void
584 thumbnail_job_completed_callback (EvJobThumbnail      *job,
585                                   EvSidebarThumbnails *sidebar_thumbnails)
586 {
587         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
588         GtkTreeIter *iter;
589
590         iter = (GtkTreeIter *) g_object_get_data (G_OBJECT (job), "tree_iter");
591         gtk_list_store_set (priv->list_store,
592                             iter,
593                             COLUMN_PIXBUF, job->thumbnail,
594                             COLUMN_THUMBNAIL_SET, TRUE,
595                             COLUMN_JOB, NULL,
596                             -1);
597 }
598
599 static void
600 ev_sidebar_thumbnails_set_document (EvSidebarPage       *sidebar_page,
601                                     EvDocument          *document)
602 {
603         EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (sidebar_page);
604
605         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
606
607         g_return_if_fail (EV_IS_DOCUMENT_THUMBNAILS (document));
608
609         priv->page_cache = ev_page_cache_get (document);
610         priv->document = document;
611         priv->n_pages = ev_page_cache_get_n_pages (priv->page_cache);
612
613         ev_sidebar_thumbnails_set_loading_icon (sidebar_thumbnails);
614
615         ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
616         ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
617
618         /* Create the view widget, and remove the old one, if needed */
619         if (ev_sidebar_thumbnails_use_icon_view (sidebar_thumbnails)) {
620                 if (priv->tree_view) {
621                         gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->tree_view);
622                         priv->tree_view = NULL;
623                 }
624
625                 if (! priv->icon_view) {
626                         ev_sidebar_init_icon_view (sidebar_thumbnails);
627                         g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
628                 } else {
629                         gtk_widget_queue_resize (priv->icon_view);
630                 }
631         } else {
632                 if (priv->icon_view) {
633                         gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->icon_view);
634                         priv->icon_view = NULL;
635                 }
636
637                 if (! priv->tree_view) {
638                         ev_sidebar_init_tree_view (sidebar_thumbnails);
639                         g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
640                 }
641         }
642
643         /* Connect to the signal and trigger a fake callback */
644         g_signal_connect (priv->page_cache, "page-changed", G_CALLBACK (page_changed_cb), sidebar_thumbnails);
645         sidebar_thumbnails->priv->start_page = 0;
646         sidebar_thumbnails->priv->end_page = 0;
647         adjustment_changed_cb (sidebar_thumbnails);
648 }
649
650 static gboolean
651 ev_sidebar_thumbnails_clear_job (GtkTreeModel *model,                                             
652                                  GtkTreePath *path,                                                                                      
653                                  GtkTreeIter *iter,                                                                                                                                   
654                                  gpointer data)
655 {
656     EvJob *job;
657     
658     gtk_tree_model_get (model, iter, COLUMN_JOB, &job, -1);
659     
660     if (job != NULL) {
661         ev_job_queue_remove_job (job);
662         g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, data);
663         g_object_unref (job);
664     }
665
666     return FALSE;    
667 }
668
669 static void 
670 ev_sidebar_thumbnails_clear_model (EvSidebarThumbnails *sidebar_thumbnails)
671 {
672     EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
673     
674     gtk_tree_model_foreach (GTK_TREE_MODEL (priv->list_store), ev_sidebar_thumbnails_clear_job, sidebar_thumbnails);
675     gtk_list_store_clear (priv->list_store);
676 }
677
678 static gboolean
679 ev_sidebar_thumbnails_support_document (EvSidebarPage   *sidebar_page,
680                                         EvDocument *document)
681 {
682         return (EV_IS_DOCUMENT_THUMBNAILS (document) &&
683                     (ev_document_get_n_pages (document) > 1));
684 }
685
686 static const gchar*
687 ev_sidebar_thumbnails_get_label (EvSidebarPage *sidebar_page)
688 {
689     return _("Thumbnails");
690 }
691
692 static void
693 ev_sidebar_thumbnails_page_iface_init (EvSidebarPageIface *iface)
694 {
695         iface->support_document = ev_sidebar_thumbnails_support_document;
696         iface->set_document = ev_sidebar_thumbnails_set_document;
697         iface->get_label = ev_sidebar_thumbnails_get_label;
698 }
699