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