]> www.fi.muni.cz Git - evince.git/blob - shell/ev-sidebar-thumbnails.c
Slightly modified patch by Nickolay V. Shmyrev <nshmyrev@yandex.ru>. Fix
[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         GdkPixbuf *loading_icon;
51         EvDocument *document;
52
53         gint n_pages, pages_done;
54
55         /* Visible pages */
56         gint start_page, end_page;
57 };
58
59 enum {
60         COLUMN_PAGE_STRING,
61         COLUMN_PIXBUF,
62         COLUMN_THUMBNAIL_SET,
63         COLUMN_JOB,
64         NUM_COLUMNS
65 };
66
67 static void         ev_sidebar_thumbnails_clear_model      (EvSidebarThumbnails *sidebar);
68 static gboolean     ev_sidebar_thumbnails_support_document (EvSidebarPage       *sidebar_page,
69                                                             EvDocument          *document);
70 static void         ev_sidebar_thumbnails_page_iface_init  (EvSidebarPageIface  *iface);
71 static void         ev_sidebar_thumbnails_set_document     (EvSidebarPage       *sidebar_page,
72                                                             EvDocument          *document);
73 static const gchar* ev_sidebar_thumbnails_get_label        (EvSidebarPage       *sidebar_page);
74 static void         thumbnail_job_completed_callback       (EvJobThumbnail      *job,
75                                                             EvSidebarThumbnails *sidebar_thumbnails);
76
77 G_DEFINE_TYPE_EXTENDED (EvSidebarThumbnails, 
78                         ev_sidebar_thumbnails, 
79                         GTK_TYPE_VBOX,
80                         0, 
81                         G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE, 
82                                                ev_sidebar_thumbnails_page_iface_init))
83
84 #define EV_SIDEBAR_THUMBNAILS_GET_PRIVATE(object) \
85         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_THUMBNAILS, EvSidebarThumbnailsPrivate));
86
87
88 static void
89 ev_sidebar_thumbnails_dispose (GObject *object)
90 {
91         EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (object);
92         
93         ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
94         g_object_unref (sidebar_thumbnails->priv->loading_icon);
95
96         G_OBJECT_CLASS (ev_sidebar_thumbnails_parent_class)->dispose (object);
97 }
98
99 static void
100 ev_sidebar_thumbnails_class_init (EvSidebarThumbnailsClass *ev_sidebar_thumbnails_class)
101 {
102         GObjectClass *g_object_class;
103         GtkObjectClass *gtk_object_class;
104
105         g_object_class = G_OBJECT_CLASS (ev_sidebar_thumbnails_class);
106         gtk_object_class = GTK_OBJECT_CLASS (ev_sidebar_thumbnails_class);
107
108         g_object_class->dispose = ev_sidebar_thumbnails_dispose;
109
110         g_type_class_add_private (g_object_class, sizeof (EvSidebarThumbnailsPrivate));
111 }
112
113 GtkWidget *
114 ev_sidebar_thumbnails_new (void)
115 {
116         GtkWidget *ev_sidebar_thumbnails;
117
118         ev_sidebar_thumbnails = g_object_new (EV_TYPE_SIDEBAR_THUMBNAILS, NULL);
119
120         return ev_sidebar_thumbnails;
121 }
122
123 static void
124 clear_range (EvSidebarThumbnails *sidebar_thumbnails,
125              gint                 start_page,
126              gint                 end_page)
127 {
128         EvSidebarThumbnailsPrivate *priv;
129         GtkTreePath *path;
130         GtkTreeIter iter;
131         gboolean result;
132
133         priv = sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (sidebar_thumbnails);
134
135         g_assert (start_page <= end_page);
136
137         path = gtk_tree_path_new_from_indices (start_page, -1);
138         for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
139              result && start_page <= end_page;
140              result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), start_page ++) {
141                 EvJobThumbnail *job;
142
143                 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store),
144                                     &iter,
145                                     COLUMN_JOB, &job,
146                                     -1);
147
148                 if (job) {
149                         g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, sidebar_thumbnails);
150                         ev_job_queue_remove_job (EV_JOB (job));
151                         g_object_unref (job);
152                 }
153
154                 gtk_list_store_set (priv->list_store, &iter,
155                                     COLUMN_JOB, NULL,
156                                     COLUMN_THUMBNAIL_SET, FALSE,
157                                     COLUMN_PIXBUF, priv->loading_icon,
158                                     -1);
159         }
160         gtk_tree_path_free (path);
161 }
162
163 static void
164 add_range (EvSidebarThumbnails *sidebar_thumbnails,
165            gint                 start_page,
166            gint                 end_page)
167 {
168         EvSidebarThumbnailsPrivate *priv;
169         GtkTreePath *path;
170         GtkTreeIter iter;
171         gboolean result;
172         gint page = start_page;
173
174         priv = sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (sidebar_thumbnails);
175
176         g_assert (start_page <= end_page);
177
178         path = gtk_tree_path_new_from_indices (start_page, -1);
179         for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
180              result && page <= end_page;
181              result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), page ++) {
182                 EvJobThumbnail *job;
183                 gboolean thumbnail_set;
184
185                 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store), &iter,
186                                     COLUMN_JOB, &job,
187                                     COLUMN_THUMBNAIL_SET, &thumbnail_set,
188                                     -1);
189
190                 if (job == NULL && !thumbnail_set) {
191                         job = (EvJobThumbnail *)ev_job_thumbnail_new (priv->document, page, THUMBNAIL_WIDTH);
192                         ev_job_queue_add_job (EV_JOB (job), EV_JOB_PRIORITY_HIGH);
193                         g_object_set_data_full (G_OBJECT (job), "tree_iter",
194                                                 gtk_tree_iter_copy (&iter),
195                                                 (GDestroyNotify) gtk_tree_iter_free);
196                         g_signal_connect (job, "finished",
197                                           G_CALLBACK (thumbnail_job_completed_callback),
198                                           sidebar_thumbnails);
199                         gtk_list_store_set (priv->list_store, &iter,
200                                             COLUMN_JOB, job,
201                                             -1);
202                         /* The queue and the list own a ref to the job now */
203                         g_object_unref (job);
204                 } else if (job) {
205                         g_object_unref (job);
206                 }
207         }
208         gtk_tree_path_free (path);
209 }
210
211 /* This modifies start */
212 static void
213 update_visible_range (EvSidebarThumbnails *sidebar_thumbnails,
214                       gint                 start_page,
215                       gint                 end_page)
216 {
217         EvSidebarThumbnailsPrivate *priv;
218         int old_start_page, old_end_page;
219
220         priv = sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (sidebar_thumbnails);
221
222         old_start_page = priv->start_page;
223         old_end_page = priv->end_page;
224
225         if (start_page == old_start_page &&
226             end_page == old_end_page)
227                 return;
228
229         /* Clear the areas we no longer display */
230         if (old_start_page < start_page)
231                 clear_range (sidebar_thumbnails, old_start_page, MIN (start_page - 1, old_end_page));
232
233         if (old_end_page > end_page)
234                 clear_range (sidebar_thumbnails, MAX (end_page + 1, old_start_page), old_end_page);
235
236         add_range (sidebar_thumbnails, start_page, end_page);
237         
238         priv->start_page = start_page;
239         priv->end_page = end_page;
240 }
241
242 static void
243 adjustment_changed_cb (EvSidebarThumbnails *sidebar_thumbnails)
244 {
245         EvSidebarThumbnailsPrivate *priv;
246         GtkTreePath *path;
247         GtkTreePath *path2;
248         gint wy1;
249         gint wy2;
250
251         priv = sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (sidebar_thumbnails);
252
253         if (! GTK_WIDGET_REALIZED (priv->tree_view))
254                 return;
255
256         gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
257                                              0, (int) priv->vadjustment->value,
258                                              NULL, &wy1);
259         gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
260                                              0, (int) (priv->vadjustment->value + priv->vadjustment->page_size),
261                                              NULL, &wy2);
262         gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
263                                        1, wy1 + 1, &path,
264                                        NULL, NULL, NULL);
265         gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
266                                        1, wy2 -1, &path2,
267                                        NULL, NULL, NULL);
268         if (path == NULL)
269                 path = gtk_tree_path_new_first ();
270         if (path2 == NULL)
271                 path2 = gtk_tree_path_new_from_indices (priv->n_pages,
272                                                         -1);
273         update_visible_range (sidebar_thumbnails,
274                               gtk_tree_path_get_indices (path)[0],
275                               gtk_tree_path_get_indices (path2)[0]);
276
277         gtk_tree_path_free (path);
278         gtk_tree_path_free (path2);
279 }
280
281 static void
282 ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
283                                    EvSidebarThumbnails *ev_sidebar_thumbnails)
284 {
285         EvSidebarThumbnailsPrivate *priv;
286         EvPageCache *page_cache;
287         GtkTreePath *path;
288         GtkTreeIter iter;
289         int page;
290
291         priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
292
293         if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
294                 return;
295
296         path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
297                                         &iter);
298         page = gtk_tree_path_get_indices (path)[0];
299         gtk_tree_path_free (path);
300
301         page_cache = ev_document_get_page_cache (priv->document);
302         ev_page_cache_set_current_page (page_cache, page);
303 }
304
305 GtkWidget *
306 ev_sidebar_thumbnails_get_treeview (EvSidebarThumbnails *sidebar)
307 {
308         return sidebar->priv->tree_view;
309 }
310
311 static void
312 ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
313 {
314         GtkWidget *swindow;
315         EvSidebarThumbnailsPrivate *priv;
316         GtkCellRenderer *renderer;
317         GtkTreeSelection *selection;
318
319         priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
320
321         priv->list_store = gtk_list_store_new (NUM_COLUMNS, G_TYPE_STRING, GDK_TYPE_PIXBUF, G_TYPE_BOOLEAN, EV_TYPE_JOB_THUMBNAIL);
322         priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
323
324         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
325         g_signal_connect (selection, "changed",
326                           G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
327         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
328         renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
329                                  "xpad", 2,
330                                  "ypad", 2,
331                                  NULL);
332         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
333                                                      NULL, renderer,
334                                                      "pixbuf", 1,
335                                                      NULL);
336         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
337                                                      NULL, gtk_cell_renderer_text_new (),
338                                                      "markup", 0, NULL);
339
340         g_object_unref (priv->list_store);
341
342         swindow = gtk_scrolled_window_new (NULL, NULL);
343         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
344                                         GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
345         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
346                                              GTK_SHADOW_IN);
347         priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (swindow));
348         g_signal_connect_data (G_OBJECT (priv->vadjustment), "value-changed",
349                                G_CALLBACK (adjustment_changed_cb),
350                                ev_sidebar_thumbnails, NULL,
351                                G_CONNECT_SWAPPED | G_CONNECT_AFTER);
352         g_signal_connect_swapped (G_OBJECT (swindow), "size-allocate",
353                                   G_CALLBACK (adjustment_changed_cb),
354                                   ev_sidebar_thumbnails);
355         gtk_container_add (GTK_CONTAINER (swindow), priv->tree_view);
356         gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), swindow, TRUE, TRUE, 0);
357
358         gtk_widget_show_all (swindow);
359 }
360
361 static void
362 page_changed_cb (EvPageCache         *page_cache,
363                  int                  page,
364                  EvSidebarThumbnails *sidebar)
365 {
366         GtkTreeView *tree_view = GTK_TREE_VIEW (sidebar->priv->tree_view);
367         GtkTreePath *path;
368
369         path = gtk_tree_path_new_from_indices (page, -1);
370         gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
371         gtk_tree_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.0, 0.0);
372         gtk_tree_path_free (path);
373 }
374
375 static void
376 thumbnail_job_completed_callback (EvJobThumbnail      *job,
377                                   EvSidebarThumbnails *sidebar_thumbnails)
378 {
379         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
380         GtkTreeIter *iter;
381
382         iter = (GtkTreeIter *) g_object_get_data (G_OBJECT (job), "tree_iter");
383         gtk_list_store_set (priv->list_store,
384                             iter,
385                             COLUMN_PIXBUF, job->thumbnail,
386                             COLUMN_THUMBNAIL_SET, TRUE,
387                             COLUMN_JOB, NULL,
388                             -1);
389 }
390
391 static void
392 ev_sidebar_thumbnails_set_document (EvSidebarPage       *sidebar_page,
393                                     EvDocument          *document)
394 {
395         EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (sidebar_page);
396         gint i, n_pages;
397         GtkTreeIter iter;
398         gint width = THUMBNAIL_WIDTH;
399         gint height = THUMBNAIL_WIDTH;
400         EvPageCache *page_cache;
401
402         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
403
404         g_return_if_fail (EV_IS_DOCUMENT_THUMBNAILS (document));
405
406         page_cache = ev_document_get_page_cache (document);
407         n_pages = ev_page_cache_get_n_pages (page_cache);
408
409         priv->document = document;
410         priv->n_pages = n_pages;
411
412         /* We get the dimensions of the first doc so that we can make a blank
413          * icon.  */
414         ev_document_doc_mutex_lock ();
415         ev_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (priv->document),
416                                                0, THUMBNAIL_WIDTH, &width, &height);
417         ev_document_doc_mutex_unlock ();
418
419         if (priv->loading_icon)
420                 g_object_unref (priv->loading_icon);
421         priv->loading_icon = ev_document_misc_get_thumbnail_frame (width, height, NULL);
422
423         ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
424         for (i = 0; i < n_pages; i++) {
425                 gchar *page_label;
426                 gchar *page_string;
427
428                 page_label = ev_page_cache_get_page_label (page_cache, i);
429                 page_string = g_markup_printf_escaped ("<i>%s</i>", page_label);
430
431                 gtk_list_store_append (priv->list_store, &iter);
432                 gtk_list_store_set (priv->list_store, &iter,
433                                     COLUMN_PAGE_STRING, page_string,
434                                     COLUMN_PIXBUF, priv->loading_icon,
435                                     COLUMN_THUMBNAIL_SET, FALSE,
436                                     -1);
437                 g_free (page_label);
438                 g_free (page_string);
439         }
440
441         /* Connect to the signal and trigger a fake callback */
442         g_signal_connect (page_cache, "page-changed", G_CALLBACK (page_changed_cb), sidebar_thumbnails);
443         adjustment_changed_cb (sidebar_thumbnails);
444 }
445
446 static gboolean
447 ev_sidebar_thumbnails_clear_job (GtkTreeModel *model,                                             
448                                  GtkTreePath *path,                                                                                      
449                                  GtkTreeIter *iter,                                                                                                                                   
450                                  gpointer data)
451 {
452     EvJob *job;
453     
454     gtk_tree_model_get (model, iter, COLUMN_JOB, &job, -1);
455     
456     if (job != NULL) {
457         ev_job_queue_remove_job (job);
458         g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, data);
459         g_object_unref (job);
460     }
461
462     return FALSE;    
463 }
464
465 static void 
466 ev_sidebar_thumbnails_clear_model (EvSidebarThumbnails *sidebar_thumbnails)
467 {
468     EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
469     
470     gtk_tree_model_foreach (GTK_TREE_MODEL (priv->list_store), ev_sidebar_thumbnails_clear_job, sidebar_thumbnails);
471     gtk_list_store_clear (priv->list_store);
472 }
473
474 static gboolean
475 ev_sidebar_thumbnails_support_document (EvSidebarPage   *sidebar_page,
476                                         EvDocument *document)
477 {
478         return (EV_IS_DOCUMENT_THUMBNAILS (document) &&
479                     (ev_document_get_n_pages (document) > 1));
480 }
481
482 static const gchar*
483 ev_sidebar_thumbnails_get_label (EvSidebarPage *sidebar_page)
484 {
485     return _("Thumbnails");
486 }
487
488 static void
489 ev_sidebar_thumbnails_page_iface_init (EvSidebarPageIface *iface)
490 {
491         iface->support_document = ev_sidebar_thumbnails_support_document;
492         iface->set_document = ev_sidebar_thumbnails_set_document;
493         iface->get_label = ev_sidebar_thumbnails_get_label;
494 }
495