+static void
+ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
+ EvSidebarThumbnails *ev_sidebar_thumbnails)
+{
+ EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
+ GtkTreePath *path;
+ GtkTreeIter iter;
+ int page;
+
+ if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
+ return;
+
+ path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
+ &iter);
+ page = gtk_tree_path_get_indices (path)[0];
+ gtk_tree_path_free (path);
+
+ ev_document_model_set_page (priv->model, page);
+}
+
+static void
+ev_sidebar_icon_selection_changed (GtkIconView *icon_view,
+ EvSidebarThumbnails *ev_sidebar_thumbnails)
+{
+ EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
+ GtkTreePath *path;
+ GList *selected;
+ int page;
+
+ selected = gtk_icon_view_get_selected_items (icon_view);
+ if (selected == NULL)
+ return;
+
+ /* We don't handle or expect multiple selection. */
+ g_assert (selected->next == NULL);
+
+ path = selected->data;
+ page = gtk_tree_path_get_indices (path)[0];
+
+ gtk_tree_path_free (path);
+ g_list_free (selected);
+
+ ev_document_model_set_page (priv->model, page);
+}
+
+static void
+ev_sidebar_init_tree_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
+{
+ EvSidebarThumbnailsPrivate *priv;
+ GtkTreeSelection *selection;
+ GtkCellRenderer *renderer;
+
+ priv = ev_sidebar_thumbnails->priv;
+ priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
+
+ selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
+ g_signal_connect (selection, "changed",
+ G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
+ gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
+ renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
+ "xpad", 2,
+ "ypad", 2,
+ NULL);
+ gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
+ NULL, renderer,
+ "pixbuf", 1,
+ NULL);
+ gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
+ NULL, gtk_cell_renderer_text_new (),
+ "markup", 0, NULL);
+ gtk_container_add (GTK_CONTAINER (priv->swindow), priv->tree_view);
+ gtk_widget_show (priv->tree_view);
+}
+
+static void
+ev_sidebar_init_icon_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
+{
+ EvSidebarThumbnailsPrivate *priv;
+
+ priv = ev_sidebar_thumbnails->priv;
+
+ priv->icon_view = gtk_icon_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
+ gtk_icon_view_set_markup_column (GTK_ICON_VIEW (priv->icon_view), 0);
+ gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (priv->icon_view), 1);
+ g_signal_connect (priv->icon_view, "selection-changed",
+ G_CALLBACK (ev_sidebar_icon_selection_changed), ev_sidebar_thumbnails);
+
+ gtk_container_add (GTK_CONTAINER (priv->swindow), priv->icon_view);
+ gtk_widget_show (priv->icon_view);
+}
+
+static gboolean
+ev_sidebar_thumbnails_use_icon_view (EvSidebarThumbnails *sidebar_thumbnails)
+{
+ EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
+
+ return (ev_document_get_n_pages (priv->document) <= MAX_ICON_VIEW_PAGE_COUNT);
+}
+
+static void
+ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
+{
+ EvSidebarThumbnailsPrivate *priv;
+
+ priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
+
+ priv->list_store = gtk_list_store_new (NUM_COLUMNS,
+ G_TYPE_STRING,
+ GDK_TYPE_PIXBUF,
+ G_TYPE_BOOLEAN,
+ EV_TYPE_JOB_THUMBNAIL);
+
+ priv->swindow = gtk_scrolled_window_new (NULL, NULL);
+
+ /* We actually don't want GTK_POLICY_AUTOMATIC for horizontal scrollbar here
+ * it's just a workaround for bug #449462
+ */
+ gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->swindow),
+ GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
+ gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->swindow),
+ GTK_SHADOW_IN);
+ priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->swindow));
+ g_signal_connect_data (priv->vadjustment, "value-changed",
+ G_CALLBACK (adjustment_changed_cb),
+ ev_sidebar_thumbnails, NULL,
+ G_CONNECT_SWAPPED | G_CONNECT_AFTER);
+ g_signal_connect_swapped (priv->swindow, "size-allocate",
+ G_CALLBACK (adjustment_changed_cb),
+ ev_sidebar_thumbnails);
+ gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), priv->swindow, TRUE, TRUE, 0);
+
+ /* Put it all together */
+ gtk_widget_show_all (priv->swindow);
+}
+
+static void
+ev_sidebar_thumbnails_set_current_page (EvSidebarThumbnails *sidebar,
+ gint page)
+{
+ GtkTreeView *tree_view;
+ GtkTreePath *path;
+
+ path = gtk_tree_path_new_from_indices (page, -1);
+
+ if (sidebar->priv->tree_view) {
+ tree_view = GTK_TREE_VIEW (sidebar->priv->tree_view);
+ gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
+ gtk_tree_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.0, 0.0);
+ } else if (sidebar->priv->icon_view) {
+
+ g_signal_handlers_block_by_func
+ (sidebar->priv->icon_view,
+ G_CALLBACK (ev_sidebar_icon_selection_changed), sidebar);
+
+ gtk_icon_view_select_path (GTK_ICON_VIEW (sidebar->priv->icon_view), path);
+
+ g_signal_handlers_unblock_by_func
+ (sidebar->priv->icon_view,
+ G_CALLBACK (ev_sidebar_icon_selection_changed), sidebar);
+
+ gtk_icon_view_scroll_to_path (GTK_ICON_VIEW (sidebar->priv->icon_view), path, FALSE, 0.0, 0.0);
+ }
+
+ gtk_tree_path_free (path);
+}
+
+static void
+page_changed_cb (EvSidebarThumbnails *sidebar,
+ gint old_page,
+ gint new_page)
+{
+ ev_sidebar_thumbnails_set_current_page (sidebar, new_page);
+}
+
+static gboolean
+refresh (EvSidebarThumbnails *sidebar_thumbnails)
+{
+ adjustment_changed_cb (sidebar_thumbnails);
+ return FALSE;
+}
+
+static void
+ev_sidebar_thumbnails_reload (EvSidebarThumbnails *sidebar_thumbnails)
+{
+ EvDocumentModel *model;
+
+ if (sidebar_thumbnails->priv->loading_icons)
+ g_hash_table_remove_all (sidebar_thumbnails->priv->loading_icons);
+
+ if (sidebar_thumbnails->priv->document == NULL ||
+ sidebar_thumbnails->priv->n_pages <= 0)
+ return;
+
+ model = sidebar_thumbnails->priv->model;
+
+ ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
+ ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
+
+ /* Trigger a redraw */
+ sidebar_thumbnails->priv->start_page = -1;
+ sidebar_thumbnails->priv->end_page = -1;
+ ev_sidebar_thumbnails_set_current_page (sidebar_thumbnails,
+ ev_document_model_get_page (model));
+ g_idle_add ((GSourceFunc)refresh, sidebar_thumbnails);
+}
+
+static void
+ev_sidebar_thumbnails_rotation_changed_cb (EvDocumentModel *model,
+ GParamSpec *pspec,
+ EvSidebarThumbnails *sidebar_thumbnails)
+{
+ gint rotation = ev_document_model_get_rotation (model);
+
+ sidebar_thumbnails->priv->rotation = rotation;
+ ev_sidebar_thumbnails_reload (sidebar_thumbnails);
+}
+
+static void
+ev_sidebar_thumbnails_inverted_colors_changed_cb (EvDocumentModel *model,
+ GParamSpec *pspec,
+ EvSidebarThumbnails *sidebar_thumbnails)
+{
+ gboolean inverted_colors = ev_document_model_get_inverted_colors (model);
+
+ sidebar_thumbnails->priv->inverted_colors = inverted_colors;
+ ev_sidebar_thumbnails_reload (sidebar_thumbnails);
+}
+
+static void
+thumbnail_job_completed_callback (EvJobThumbnail *job,
+ EvSidebarThumbnails *sidebar_thumbnails)
+{
+ EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
+ GtkTreeIter *iter;
+
+ iter = (GtkTreeIter *) g_object_get_data (G_OBJECT (job), "tree_iter");
+ if (priv->inverted_colors)
+ ev_document_misc_invert_pixbuf (job->thumbnail);
+ gtk_list_store_set (priv->list_store,
+ iter,
+ COLUMN_PIXBUF, job->thumbnail,
+ COLUMN_THUMBNAIL_SET, TRUE,
+ COLUMN_JOB, NULL,
+ -1);
+}
+
+static void
+ev_sidebar_thumbnails_document_changed_cb (EvDocumentModel *model,
+ GParamSpec *pspec,
+ EvSidebarThumbnails *sidebar_thumbnails)
+{
+ EvDocument *document = ev_document_model_get_document (model);
+ EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
+
+ if (ev_document_get_n_pages (document) <= 0 ||
+ !ev_document_check_dimensions (document)) {
+ return;
+ }
+
+ priv->size_cache = ev_thumbnails_size_cache_get (document);