]> www.fi.muni.cz Git - evince.git/blob - shell/ev-sidebar-thumbnails.c
Small rework of sidebar pages handling. See bug #164811 for details
[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 /* Amount of time we devote to each iteration of the idle, in microseconds */
44 #define IDLE_WORK_LENGTH 5000
45
46 struct _EvSidebarThumbnailsPrivate {
47         GtkWidget *tree_view;
48         GtkAdjustment *vadjustment;
49         GtkListStore *list_store;
50         EvDocument *document;
51
52         gint n_pages, pages_done;
53 };
54
55 enum {
56         COLUMN_PAGE_STRING,
57         COLUMN_PIXBUF,
58         COLUMN_THUMBNAIL_SET,
59         COLUMN_JOB,
60         NUM_COLUMNS
61 };
62
63 static void     ev_sidebar_thumbnails_clear_model               (EvSidebarThumbnails *sidebar);
64 static gboolean ev_sidebar_thumbnails_support_document          (EvSidebarPage   *sidebar_page,
65                                                                  EvDocument *document);
66 static void     ev_sidebar_thumbnails_page_iface_init           (EvSidebarPageIface *iface);
67 static void     ev_sidebar_thumbnails_set_document              (EvSidebarPage  *sidebar_page,
68                                                                  EvDocument          *document);
69 static const gchar* ev_sidebar_thumbnails_get_label             (EvSidebarPage *sidebar_page);
70
71 G_DEFINE_TYPE_EXTENDED (EvSidebarThumbnails, 
72                         ev_sidebar_thumbnails, 
73                         GTK_TYPE_VBOX,
74                         0, 
75                         G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE, 
76                                                ev_sidebar_thumbnails_page_iface_init))
77
78 #define EV_SIDEBAR_THUMBNAILS_GET_PRIVATE(object) \
79         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_THUMBNAILS, EvSidebarThumbnailsPrivate));
80
81
82 static void
83 ev_sidebar_thumbnails_dispose (GObject *object)
84 {
85         EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (object);
86         
87         ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
88
89         G_OBJECT_CLASS (ev_sidebar_thumbnails_parent_class)->dispose (object);
90 }
91
92 static void
93 ev_sidebar_thumbnails_class_init (EvSidebarThumbnailsClass *ev_sidebar_thumbnails_class)
94 {
95         GObjectClass *g_object_class;
96         GtkObjectClass *gtk_object_class;
97
98         g_object_class = G_OBJECT_CLASS (ev_sidebar_thumbnails_class);
99         gtk_object_class = GTK_OBJECT_CLASS (ev_sidebar_thumbnails_class);
100
101         g_object_class->dispose = ev_sidebar_thumbnails_dispose;
102
103         g_type_class_add_private (g_object_class, sizeof (EvSidebarThumbnailsPrivate));
104 }
105
106 GtkWidget *
107 ev_sidebar_thumbnails_new (void)
108 {
109         GtkWidget *ev_sidebar_thumbnails;
110
111         ev_sidebar_thumbnails = g_object_new (EV_TYPE_SIDEBAR_THUMBNAILS, NULL);
112
113         return ev_sidebar_thumbnails;
114 }
115
116 static void
117 adjustment_changed_cb (GtkAdjustment       *adjustment,
118                        EvSidebarThumbnails *ev_sidebar_thumbnails)
119 {
120         EvSidebarThumbnailsPrivate *priv;
121         GtkTreePath *path;
122         GtkTreeIter iter;
123         int page;
124         gboolean thumbnail_set;
125
126         priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
127
128         gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
129                                        1, 1, &path,
130                                        NULL, NULL, NULL);
131         if (!path)
132                 return;
133
134         page = gtk_tree_path_get_indices (path)[0];
135         gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store),
136                                  &iter, path);
137         gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store), &iter,
138                             COLUMN_THUMBNAIL_SET, &thumbnail_set,
139                             -1);
140 }
141
142 static void
143 ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
144                                    EvSidebarThumbnails *ev_sidebar_thumbnails)
145 {
146         EvSidebarThumbnailsPrivate *priv;
147         EvPageCache *page_cache;
148         GtkTreePath *path;
149         GtkTreeIter iter;
150         int page;
151
152         priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
153
154         if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
155                 return;
156
157         path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
158                                         &iter);
159         page = gtk_tree_path_get_indices (path)[0];
160         gtk_tree_path_free (path);
161
162         page_cache = ev_document_get_page_cache (priv->document);
163         ev_page_cache_set_current_page (page_cache, page);
164 }
165
166 static void
167 ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
168 {
169         GtkWidget *swindow;
170         EvSidebarThumbnailsPrivate *priv;
171         GtkCellRenderer *renderer;
172         GtkTreeSelection *selection;
173
174         priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
175
176         priv->list_store = gtk_list_store_new (NUM_COLUMNS, G_TYPE_STRING, GDK_TYPE_PIXBUF, G_TYPE_BOOLEAN, EV_TYPE_JOB_THUMBNAIL);
177         priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
178
179         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
180         g_signal_connect (selection, "changed",
181                           G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
182         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
183         renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
184                                  "xpad", 2,
185                                  "ypad", 2,
186                                  NULL);
187         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
188                                                      NULL, renderer,
189                                                      "pixbuf", 1,
190                                                      NULL);
191         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
192                                                      NULL, gtk_cell_renderer_text_new (),
193                                                      "markup", 0, NULL);
194
195         g_object_unref (priv->list_store);
196
197         swindow = gtk_scrolled_window_new (NULL, NULL);
198         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
199                                         GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
200         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
201                                              GTK_SHADOW_IN);
202         priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (swindow));
203         g_signal_connect (G_OBJECT (priv->vadjustment), "value-changed",
204                           G_CALLBACK (adjustment_changed_cb),
205                           ev_sidebar_thumbnails);
206         gtk_container_add (GTK_CONTAINER (swindow), priv->tree_view);
207         gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), swindow, TRUE, TRUE, 0);
208
209         gtk_widget_show_all (swindow);
210 }
211
212 static void
213 page_changed_cb (EvPageCache         *page_cache,
214                  int                  page,
215                  EvSidebarThumbnails *sidebar)
216 {
217         GtkTreePath *path;
218         GtkTreeSelection *selection;
219
220         path = gtk_tree_path_new_from_indices (page, -1);
221         selection = gtk_tree_view_get_selection
222                         (GTK_TREE_VIEW (sidebar->priv->tree_view));
223
224         gtk_tree_selection_select_path (selection, path);
225         gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (sidebar->priv->tree_view),
226                                       path, NULL, FALSE, 0.0, 0.0);
227         gtk_tree_path_free (path);
228 }
229
230 static void
231 thumbnail_job_completed_callback (EvJobThumbnail      *job,
232                                   EvSidebarThumbnails *sidebar_thumbnails)
233 {
234         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
235         GtkTreeIter *iter;
236
237         iter = (GtkTreeIter *) g_object_get_data (G_OBJECT (job), "tree_iter");
238         gtk_list_store_set (priv->list_store,
239                             iter,
240                             COLUMN_PIXBUF, job->thumbnail,
241                             COLUMN_THUMBNAIL_SET, TRUE,
242                             COLUMN_JOB, NULL,
243                             -1);
244 }
245
246 static void
247 ev_sidebar_thumbnails_set_document (EvSidebarPage       *sidebar_page,
248                                     EvDocument          *document)
249 {
250         EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (sidebar_page);
251         GdkPixbuf *loading_icon;
252         gint i, n_pages;
253         GtkTreeIter iter;
254         gint width = THUMBNAIL_WIDTH;
255         gint height = THUMBNAIL_WIDTH;
256         EvPageCache *page_cache;
257
258         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
259
260         g_return_if_fail (EV_IS_DOCUMENT_THUMBNAILS (document));
261
262         page_cache = ev_document_get_page_cache (document);
263         n_pages = ev_page_cache_get_n_pages (page_cache);
264
265         priv->document = document;
266         priv->n_pages = n_pages;
267
268         /* We get the dimensions of the first doc so that we can make a blank
269          * icon.  */
270         g_mutex_lock (EV_DOC_MUTEX);
271         ev_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (priv->document),
272                                                0, THUMBNAIL_WIDTH, &width, &height);
273         g_mutex_unlock (EV_DOC_MUTEX);
274
275         loading_icon = ev_document_misc_get_thumbnail_frame (width, height, NULL);
276
277         ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
278         for (i = 0; i < n_pages; i++) {
279                 EvJob *job;
280                 gchar *page_label;
281                 gchar *page_string;
282
283                 job = ev_job_thumbnail_new (priv->document, i, THUMBNAIL_WIDTH);
284                 page_label = ev_page_cache_get_page_label (page_cache, i);
285                 page_string = g_markup_printf_escaped ("<i>%s</i>", page_label);
286
287                 gtk_list_store_append (priv->list_store, &iter);
288                 gtk_list_store_set (priv->list_store, &iter,
289                                     COLUMN_PAGE_STRING, page_string,
290                                     COLUMN_PIXBUF, loading_icon,
291                                     COLUMN_THUMBNAIL_SET, FALSE,
292                                     COLUMN_JOB, job,
293                                     -1);
294                 g_free (page_label);
295                 g_free (page_string);
296                 ev_job_queue_add_job (job, EV_JOB_PRIORITY_LOW);
297                 g_object_set_data_full (G_OBJECT (job), "tree_iter",
298                                         gtk_tree_iter_copy (&iter),
299                                         (GDestroyNotify) gtk_tree_iter_free);
300                 g_signal_connect (job, "finished",
301                                   G_CALLBACK (thumbnail_job_completed_callback),
302                                   sidebar_thumbnails);
303         }
304
305         g_object_unref (loading_icon);
306
307         /* Connect to the signal and trigger a fake callback */
308         g_signal_connect (page_cache, "page-changed", G_CALLBACK (page_changed_cb), sidebar_thumbnails);
309         page_changed_cb (page_cache, ev_page_cache_get_current_page (page_cache), sidebar_thumbnails);
310
311 }
312
313 static gboolean
314 ev_sidebar_thumbnails_clear_job (GtkTreeModel *model,                                             
315                                  GtkTreePath *path,                                                                                      
316                                  GtkTreeIter *iter,                                                                                                                                   
317                                  gpointer data)
318 {
319     EvJob *job;
320     
321     gtk_tree_model_get (model, iter, COLUMN_JOB, &job, -1);
322     
323     if (job != NULL) {
324         ev_job_queue_remove_job (job);
325         g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, data);
326         g_object_unref (job);
327     }
328
329     return FALSE;    
330 }
331
332 static void 
333 ev_sidebar_thumbnails_clear_model (EvSidebarThumbnails *sidebar_thumbnails)
334 {
335     EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
336     
337     gtk_tree_model_foreach (GTK_TREE_MODEL (priv->list_store), ev_sidebar_thumbnails_clear_job, sidebar_thumbnails);
338     gtk_list_store_clear (priv->list_store);
339 }
340
341 static gboolean
342 ev_sidebar_thumbnails_support_document (EvSidebarPage   *sidebar_page,
343                                         EvDocument *document)
344 {
345         return (EV_IS_DOCUMENT_THUMBNAILS (document) &&
346                     (ev_document_get_n_pages (document) > 1));
347 }
348
349 static const gchar*
350 ev_sidebar_thumbnails_get_label (EvSidebarPage *sidebar_page)
351 {
352     return _("Thumbnails");
353 }
354
355 static void
356 ev_sidebar_thumbnails_page_iface_init (EvSidebarPageIface *iface)
357 {
358         iface->support_document = ev_sidebar_thumbnails_support_document;
359         iface->set_document = ev_sidebar_thumbnails_set_document;
360         iface->get_label = ev_sidebar_thumbnails_get_label;
361 }
362