1 /* this file is part of atril, a mate 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  * Atril 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  * Atril 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  */
24 
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 
29 #include <string.h>
30 
31 #include <glib/gi18n.h>
32 #include <gtk/gtk.h>
33 
34 #include "ev-document-misc.h"
35 #include "ev-document-thumbnails.h"
36 #include "ev-job-scheduler.h"
37 #include "ev-sidebar-page.h"
38 #include "ev-sidebar-thumbnails.h"
39 #include "ev-utils.h"
40 #include "ev-window.h"
41 
42 #define THUMBNAIL_WIDTH 100
43 
44 /* The IconView doesn't scale nearly as well as the TreeView, so we arbitrarily
45  * limit its use */
46 #define MAX_ICON_VIEW_PAGE_COUNT 1500
47 
48 typedef struct _EvThumbsSize
49 {
50 	gint width;
51 	gint height;
52 } EvThumbsSize;
53 
54 typedef struct _EvThumbsSizeCache {
55 	gboolean uniform;
56 	gint uniform_width;
57 	gint uniform_height;
58 	EvThumbsSize *sizes;
59 } EvThumbsSizeCache;
60 
61 struct _EvSidebarThumbnailsPrivate {
62 	GtkWidget *swindow;
63 	GtkWidget *icon_view;
64 	GtkWidget *tree_view;
65 	GtkAdjustment *vadjustment;
66 	GtkListStore *list_store;
67 	GHashTable *loading_icons;
68 	EvDocument *document;
69 	EvDocumentModel *model;
70 	EvThumbsSizeCache *size_cache;
71 	gint n_pages, pages_done;
72 
73 	int rotation;
74 	gboolean inverted_colors;
75 
76 	/* Visible pages */
77 	gint start_page, end_page;
78 };
79 
80 enum {
81 	COLUMN_PAGE_STRING,
82 	COLUMN_PIXBUF,
83 	COLUMN_THUMBNAIL_SET,
84 	COLUMN_JOB,
85 	NUM_COLUMNS
86 };
87 
88 enum {
89 	PROP_0,
90 	PROP_WIDGET,
91 };
92 
93 static void         ev_sidebar_thumbnails_clear_model      (EvSidebarThumbnails     *sidebar);
94 static gboolean     ev_sidebar_thumbnails_support_document (EvSidebarPage           *sidebar_page,
95 							    EvDocument              *document);
96 static void         ev_sidebar_thumbnails_page_iface_init  (EvSidebarPageInterface  *iface);
97 static const gchar* ev_sidebar_thumbnails_get_label        (EvSidebarPage           *sidebar_page);
98 static void         thumbnail_job_completed_callback       (EvJobThumbnail          *job,
99 							    EvSidebarThumbnails     *sidebar_thumbnails);
100 static void         adjustment_changed_cb                  (EvSidebarThumbnails     *sidebar_thumbnails);
101 
102 G_DEFINE_TYPE_EXTENDED (EvSidebarThumbnails,
103                         ev_sidebar_thumbnails,
104                         GTK_TYPE_BOX,
105                         0,
106                         G_ADD_PRIVATE (EvSidebarThumbnails)
107                         G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
108 					       ev_sidebar_thumbnails_page_iface_init))
109 
110 /* Thumbnails dimensions cache */
111 #define EV_THUMBNAILS_SIZE_CACHE_KEY "ev-thumbnails-size-cache"
112 
113 static EvThumbsSizeCache *
ev_thumbnails_size_cache_new(EvDocument * document)114 ev_thumbnails_size_cache_new (EvDocument *document)
115 {
116 	EvThumbsSizeCache *cache;
117 	EvRenderContext *rc = NULL;
118 	gint i, n_pages;
119 	EvThumbsSize *thumb_size;
120 
121 	cache = g_new0 (EvThumbsSizeCache, 1);
122 
123 	n_pages = ev_document_get_n_pages (document);
124 
125 	/* Assume all pages are the same size until proven otherwise */
126 	cache->uniform = TRUE;
127 
128 	for (i = 0; i < n_pages; i++) {
129 		EvPage *page;
130 		gdouble page_width, page_height;
131 		gint    thumb_width = 0;
132 		gint    thumb_height = 0;
133 
134 		page = ev_document_get_page (document, i);
135 
136 		if (document->iswebdocument == FALSE ) {
137 			ev_document_get_page_size (document, i, &page_width, &page_height);
138 		}
139 		else {
140 			/* Hardcoding these values to a large enough dimesnsion so as to achieve max content without loss in visibility*/
141 			page_width = 800;
142 			page_height = 1080;
143 		}
144 		if (!rc) {
145 			rc = ev_render_context_new (page, 0, (gdouble)THUMBNAIL_WIDTH / page_width);
146 		} else {
147 			ev_render_context_set_page (rc, page);
148 			ev_render_context_set_scale (rc, (gdouble)THUMBNAIL_WIDTH / page_width);
149 		}
150 
151 		ev_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (document),
152 						       rc, &thumb_width, &thumb_height);
153 
154 		if (i == 0) {
155 			cache->uniform_width = thumb_width;
156 			cache->uniform_height = thumb_height;
157 		} else if (cache->uniform &&
158 			   (cache->uniform_width != thumb_width ||
159 			    cache->uniform_height != thumb_height)) {
160 			/* It's a different thumbnail size.  Backfill the array. */
161 			int j;
162 
163 			cache->sizes = g_new0 (EvThumbsSize, n_pages);
164 
165 			for (j = 0; j < i; j++) {
166 				thumb_size = &(cache->sizes[j]);
167 				thumb_size->width = cache->uniform_width;
168 				thumb_size->height = cache->uniform_height;
169 			}
170 			cache->uniform = FALSE;
171 		}
172 
173 		if (! cache->uniform) {
174 			thumb_size = &(cache->sizes[i]);
175 
176 			thumb_size->width = thumb_width;
177 			thumb_size->height = thumb_height;
178 		}
179 
180 		g_object_unref (page);
181 	}
182 
183 	if (rc) {
184 		g_object_unref (rc);
185 	}
186 
187 	return cache;
188 }
189 
190 static void
ev_thumbnails_size_cache_get_size(EvThumbsSizeCache * cache,gint page,gint rotation,gint * width,gint * height)191 ev_thumbnails_size_cache_get_size (EvThumbsSizeCache *cache,
192 				   gint               page,
193 				   gint               rotation,
194 				   gint              *width,
195 				   gint              *height)
196 {
197 	gint w, h;
198 
199 	if (cache->uniform) {
200 		w = cache->uniform_width;
201 		h = cache->uniform_height;
202 	} else {
203 		EvThumbsSize *thumb_size;
204 
205 		thumb_size = &(cache->sizes[page]);
206 
207 		w = thumb_size->width;
208 		h = thumb_size->height;
209 	}
210 
211 	if (rotation == 0 || rotation == 180) {
212 		if (width) *width = w;
213 		if (height) *height = h;
214 	} else {
215 		if (width) *width = h;
216 		if (height) *height = w;
217 	}
218 }
219 
220 static void
ev_thumbnails_size_cache_free(EvThumbsSizeCache * cache)221 ev_thumbnails_size_cache_free (EvThumbsSizeCache *cache)
222 {
223 	if (cache->sizes) {
224 		g_free (cache->sizes);
225 		cache->sizes = NULL;
226 	}
227 
228 	g_free (cache);
229 }
230 
231 static EvThumbsSizeCache *
ev_thumbnails_size_cache_get(EvDocument * document)232 ev_thumbnails_size_cache_get (EvDocument *document)
233 {
234 	EvThumbsSizeCache *cache;
235 
236 	cache = g_object_get_data (G_OBJECT (document), EV_THUMBNAILS_SIZE_CACHE_KEY);
237 	if (!cache) {
238 		cache = ev_thumbnails_size_cache_new (document);
239 		g_object_set_data_full (G_OBJECT (document),
240 					EV_THUMBNAILS_SIZE_CACHE_KEY,
241 					cache,
242 					(GDestroyNotify)ev_thumbnails_size_cache_free);
243 	}
244 
245 	return cache;
246 }
247 
248 
249 static void
ev_sidebar_thumbnails_dispose(GObject * object)250 ev_sidebar_thumbnails_dispose (GObject *object)
251 {
252 	EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (object);
253 
254 	if (sidebar_thumbnails->priv->loading_icons) {
255 		g_hash_table_destroy (sidebar_thumbnails->priv->loading_icons);
256 		sidebar_thumbnails->priv->loading_icons = NULL;
257 	}
258 
259 	if (sidebar_thumbnails->priv->list_store) {
260 		ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
261 		g_object_unref (sidebar_thumbnails->priv->list_store);
262 		sidebar_thumbnails->priv->list_store = NULL;
263 	}
264 
265 	G_OBJECT_CLASS (ev_sidebar_thumbnails_parent_class)->dispose (object);
266 }
267 
268 static void
ev_sidebar_thumbnails_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)269 ev_sidebar_thumbnails_get_property (GObject    *object,
270 				    guint       prop_id,
271 				    GValue     *value,
272 				    GParamSpec *pspec)
273 {
274 	EvSidebarThumbnails *sidebar = EV_SIDEBAR_THUMBNAILS (object);
275 
276 	switch (prop_id) {
277 	case PROP_WIDGET:
278 		if (sidebar->priv->tree_view)
279 			g_value_set_object (value, sidebar->priv->tree_view);
280 		else
281 			g_value_set_object (value, sidebar->priv->icon_view);
282 		break;
283 	default:
284 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
285 		break;
286 	}
287 }
288 
289 static void
ev_sidebar_thumbnails_map(GtkWidget * widget)290 ev_sidebar_thumbnails_map (GtkWidget *widget)
291 {
292 	EvSidebarThumbnails *sidebar;
293 
294 	sidebar = EV_SIDEBAR_THUMBNAILS (widget);
295 
296 	GTK_WIDGET_CLASS (ev_sidebar_thumbnails_parent_class)->map (widget);
297 
298 	adjustment_changed_cb (sidebar);
299 }
300 
301 static void
ev_sidebar_fullscreen_cb(EvSidebarThumbnails * sidebar)302 ev_sidebar_fullscreen_cb (EvSidebarThumbnails *sidebar)
303 {
304 	/* After activating or deactivating fullscreen mode, the sidebar
305 	 * window is automatically moved to its start, while scroll bar
306 	 * stays in its original position.
307 	 *
308 	 * The sidebar window move is unwanted and unsolicited, and it's
309 	 * most probably caused by GtkIconView or GtkScrolledWindow bug.
310 	 *
311 	 * Workaround this by having the sidebar sync its window with the
312 	 * current scroll position after a fullscreen operation, do that by
313 	 * just emitting a "value-changed" on the current scroll adjustment.
314 	 * Fixes https://bugzilla.gnome.org/show_bug.cgi?id=783404 */
315 	g_signal_emit_by_name (sidebar->priv->vadjustment, "value-changed");
316 }
317 
318 static void
ev_sidebar_thumbnails_class_init(EvSidebarThumbnailsClass * ev_sidebar_thumbnails_class)319 ev_sidebar_thumbnails_class_init (EvSidebarThumbnailsClass *ev_sidebar_thumbnails_class)
320 {
321 	GObjectClass *g_object_class;
322 	GtkWidgetClass *widget_class;
323 
324 	g_object_class = G_OBJECT_CLASS (ev_sidebar_thumbnails_class);
325 	widget_class = GTK_WIDGET_CLASS (ev_sidebar_thumbnails_class);
326 
327 	g_object_class->dispose = ev_sidebar_thumbnails_dispose;
328 	g_object_class->get_property = ev_sidebar_thumbnails_get_property;
329 	widget_class->map = ev_sidebar_thumbnails_map;
330 
331 	g_object_class_override_property (g_object_class,
332 					  PROP_WIDGET,
333 					  "main-widget");
334 }
335 
336 GtkWidget *
ev_sidebar_thumbnails_new(void)337 ev_sidebar_thumbnails_new (void)
338 {
339 	GtkWidget *ev_sidebar_thumbnails;
340 
341 	ev_sidebar_thumbnails = g_object_new (EV_TYPE_SIDEBAR_THUMBNAILS, NULL);
342 
343 	return ev_sidebar_thumbnails;
344 }
345 
346 static GdkPixbuf *
ev_sidebar_thumbnails_get_loading_icon(EvSidebarThumbnails * sidebar_thumbnails,gint width,gint height)347 ev_sidebar_thumbnails_get_loading_icon (EvSidebarThumbnails *sidebar_thumbnails,
348 					gint                 width,
349 					gint                 height)
350 {
351 	EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
352 	GdkPixbuf *icon;
353 	gchar     *key;
354 
355 	key = g_strdup_printf ("%dx%d", width, height);
356 	icon = g_hash_table_lookup (priv->loading_icons, key);
357 	if (!icon) {
358 		gboolean inverted_colors;
359 
360 		inverted_colors = ev_document_model_get_inverted_colors (priv->model);
361 		icon = ev_document_misc_get_loading_thumbnail (width, height, inverted_colors);
362 		g_hash_table_insert (priv->loading_icons, key, icon);
363 	} else {
364 		g_free (key);
365 	}
366 
367 	return icon;
368 }
369 
370 static void
cancel_running_jobs(EvSidebarThumbnails * sidebar_thumbnails,gint start_page,gint end_page)371 cancel_running_jobs (EvSidebarThumbnails *sidebar_thumbnails,
372                      gint                 start_page,
373                      gint                 end_page)
374 {
375 	EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
376 	GtkTreePath *path;
377 	GtkTreeIter iter;
378 	gboolean result;
379 
380 	g_assert (start_page <= end_page);
381 
382 	path = gtk_tree_path_new_from_indices (start_page, -1);
383 	for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
384 	     result && start_page <= end_page;
385 	     result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), start_page ++) {
386 		EvJobThumbnail *job;
387 		gboolean thumbnail_set;
388 
389 		gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store),
390 				    &iter,
391 				    COLUMN_JOB, &job,
392 				    COLUMN_THUMBNAIL_SET, &thumbnail_set,
393 				    -1);
394 
395 		if (thumbnail_set) {
396 			g_assert (job == NULL);
397 			continue;
398 		}
399 
400 		if (job) {
401 			g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, sidebar_thumbnails);
402 			ev_job_cancel (EV_JOB (job));
403 			g_object_unref (job);
404 		}
405 
406 		gtk_list_store_set (priv->list_store, &iter,
407 				    COLUMN_JOB, NULL,
408 				    COLUMN_THUMBNAIL_SET, FALSE,
409 				    -1);
410 	}
411 	gtk_tree_path_free (path);
412 }
413 
414 static gdouble
get_scale_for_page(EvSidebarThumbnails * sidebar_thumbnails,gint page)415 get_scale_for_page (EvSidebarThumbnails *sidebar_thumbnails,
416 		    gint                 page)
417 {
418 	EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
419 	gdouble width;
420 	if (priv->document->iswebdocument == TRUE ) {
421 		/* Hardcoded for epub documents*/
422 		width = 800;
423 	} else {
424 		ev_document_get_page_size (priv->document, page, &width, NULL);
425 	}
426 	return (gdouble)THUMBNAIL_WIDTH / width;
427 }
428 
429 static void
add_range(EvSidebarThumbnails * sidebar_thumbnails,gint start_page,gint end_page)430 add_range (EvSidebarThumbnails *sidebar_thumbnails,
431 	   gint                 start_page,
432 	   gint                 end_page)
433 {
434 	EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
435 	GtkTreePath *path;
436 	GtkTreeIter iter;
437 	gboolean result;
438 	gint page = start_page;
439 
440 	g_assert (start_page <= end_page);
441 
442 	path = gtk_tree_path_new_from_indices (start_page, -1);
443 	for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
444 	     result && page <= end_page;
445 	     result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), page ++) {
446 		EvJob *job;
447 		gboolean thumbnail_set;
448 
449 		gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store), &iter,
450 				    COLUMN_JOB, &job,
451 				    COLUMN_THUMBNAIL_SET, &thumbnail_set,
452 				    -1);
453 
454 		if (job == NULL && !thumbnail_set) {
455 			job = ev_job_thumbnail_new (priv->document,
456 						    page, priv->rotation,
457 						    get_scale_for_page (sidebar_thumbnails, page));
458 
459 			if (priv->document->iswebdocument) {
460 				ev_job_set_run_mode(job, EV_JOB_RUN_MAIN_LOOP);
461 			}
462 
463 			g_object_set_data_full (G_OBJECT (job), "tree_iter",
464 						gtk_tree_iter_copy (&iter),
465 						(GDestroyNotify) gtk_tree_iter_free);
466 			g_signal_connect (job, "finished",
467 					  G_CALLBACK (thumbnail_job_completed_callback),
468 					  sidebar_thumbnails);
469 			gtk_list_store_set (priv->list_store, &iter,
470 					    COLUMN_JOB, job,
471 					    -1);
472 
473 			ev_job_scheduler_push_job (EV_JOB (job), EV_JOB_PRIORITY_HIGH);
474 
475 			/* The queue and the list own a ref to the job now */
476 			g_object_unref (job);
477 		} else if (job) {
478 			g_object_unref (job);
479 		}
480 	}
481 	gtk_tree_path_free (path);
482 }
483 
484 /* This modifies start */
485 static void
update_visible_range(EvSidebarThumbnails * sidebar_thumbnails,gint start_page,gint end_page)486 update_visible_range (EvSidebarThumbnails *sidebar_thumbnails,
487 		      gint                 start_page,
488 		      gint                 end_page)
489 {
490 	EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
491 	int old_start_page, old_end_page;
492 	int n_pages_in_visible_range;
493 
494 	/* Preload before and after current visible scrolling range, the same amount of
495 	 * thumbs in it, to help prevent thumbnail creation happening in the user's sight.
496 	 * https://bugzilla.gnome.org/show_bug.cgi?id=342110#c15 */
497 	n_pages_in_visible_range = (end_page - start_page) + 1;
498 	start_page = MAX (0, start_page - n_pages_in_visible_range);
499 	end_page = MIN (priv->n_pages - 1, end_page + n_pages_in_visible_range);
500 
501 	old_start_page = priv->start_page;
502 	old_end_page = priv->end_page;
503 
504 	if (start_page == old_start_page &&
505 	    end_page == old_end_page)
506 		return;
507 
508 	/* Clear the areas we no longer display */
509 	if (old_start_page >= 0 && old_start_page < start_page)
510 		cancel_running_jobs (sidebar_thumbnails, old_start_page, MIN (start_page - 1, old_end_page));
511 
512 	if (old_end_page > 0 && old_end_page > end_page)
513 		cancel_running_jobs (sidebar_thumbnails, MAX (end_page + 1, old_start_page), old_end_page);
514 
515 	add_range (sidebar_thumbnails, start_page, end_page);
516 
517 	priv->start_page = start_page;
518 	priv->end_page = end_page;
519 }
520 
521 static void
adjustment_changed_cb(EvSidebarThumbnails * sidebar_thumbnails)522 adjustment_changed_cb (EvSidebarThumbnails *sidebar_thumbnails)
523 {
524 	EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
525 	GtkTreePath *path = NULL;
526 	GtkTreePath *path2 = NULL;
527 	gdouble page_size;
528 	gdouble value;
529 	gint wy1;
530 	gint wy2;
531 
532 	/* Widget is not currently visible */
533 	if (!gtk_widget_get_mapped (GTK_WIDGET (sidebar_thumbnails)))
534 		return;
535 
536 	page_size = gtk_adjustment_get_page_size (priv->vadjustment);
537 
538 	if (page_size == 0)
539 		return;
540 
541 	value = gtk_adjustment_get_value (priv->vadjustment);
542 
543 	if (priv->tree_view) {
544 		if (! gtk_widget_get_realized (priv->tree_view))
545 			return;
546 
547 		gtk_tree_view_convert_tree_to_bin_window_coords (GTK_TREE_VIEW (priv->tree_view),
548 								 0, (int) value,
549 								 NULL, &wy1);
550 		gtk_tree_view_convert_tree_to_bin_window_coords (GTK_TREE_VIEW (priv->tree_view),
551 								 0, (int) (value + page_size),
552 								 NULL, &wy2);
553 		gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
554 					       1, wy1 + 1, &path,
555 					       NULL, NULL, NULL);
556 		gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
557 					       1, wy2 -1, &path2,
558 					       NULL, NULL, NULL);
559 	} else if (priv->icon_view) {
560 		if (! gtk_widget_get_realized (priv->icon_view))
561 			return;
562 		if (! gtk_icon_view_get_visible_range (GTK_ICON_VIEW (priv->icon_view), &path, &path2))
563 			return;
564 	} else {
565 		return;
566 	}
567 
568 	if (path && path2) {
569 		update_visible_range (sidebar_thumbnails,
570 				      gtk_tree_path_get_indices (path)[0],
571 				      gtk_tree_path_get_indices (path2)[0]);
572 	}
573 
574 	gtk_tree_path_free (path);
575 	gtk_tree_path_free (path2);
576 }
577 
578 static void
ev_sidebar_thumbnails_fill_model(EvSidebarThumbnails * sidebar_thumbnails)579 ev_sidebar_thumbnails_fill_model (EvSidebarThumbnails *sidebar_thumbnails)
580 {
581 	EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
582 	GtkTreeIter iter;
583 	int i;
584 
585 	for (i = 0; i < sidebar_thumbnails->priv->n_pages; i++) {
586 		gchar     *page_label;
587 		gchar     *page_string;
588 		GdkPixbuf *loading_icon = NULL;
589 		gint       width, height;
590 
591 		page_label = ev_document_get_page_label (priv->document, i);
592 		page_string = g_markup_printf_escaped ("<i>%s</i>", page_label);
593 		ev_thumbnails_size_cache_get_size (sidebar_thumbnails->priv->size_cache, i,
594 						   sidebar_thumbnails->priv->rotation,
595 						   &width, &height);
596 
597 		loading_icon = ev_sidebar_thumbnails_get_loading_icon (sidebar_thumbnails,
598 								       width, height);
599 
600 		gtk_list_store_append (priv->list_store, &iter);
601 		gtk_list_store_set (priv->list_store, &iter,
602 				    COLUMN_PAGE_STRING, page_string,
603 				    COLUMN_PIXBUF, loading_icon,
604 				    COLUMN_THUMBNAIL_SET, FALSE,
605 				    -1);
606 		g_free (page_label);
607 		g_free (page_string);
608 	}
609 }
610 
611 static void
ev_sidebar_tree_selection_changed(GtkTreeSelection * selection,EvSidebarThumbnails * ev_sidebar_thumbnails)612 ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
613 				   EvSidebarThumbnails *ev_sidebar_thumbnails)
614 {
615 	EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
616 	GtkTreePath *path;
617 	GtkTreeIter iter;
618 	int page;
619 
620 	if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
621 		return;
622 
623 	path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
624 					&iter);
625 	page = gtk_tree_path_get_indices (path)[0];
626 	gtk_tree_path_free (path);
627 
628 	ev_document_model_set_page (priv->model, page);
629 }
630 
631 static void
ev_sidebar_icon_selection_changed(GtkIconView * icon_view,EvSidebarThumbnails * ev_sidebar_thumbnails)632 ev_sidebar_icon_selection_changed (GtkIconView         *icon_view,
633 				   EvSidebarThumbnails *ev_sidebar_thumbnails)
634 {
635 	EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
636 	GtkTreePath *path;
637 	GList *selected;
638 	int page;
639 
640 	selected = gtk_icon_view_get_selected_items (icon_view);
641 	if (selected == NULL)
642 		return;
643 
644 	/* We don't handle or expect multiple selection. */
645 	g_assert (selected->next == NULL);
646 
647 	path = selected->data;
648 	page = gtk_tree_path_get_indices (path)[0];
649 
650 	gtk_tree_path_free (path);
651 	g_list_free (selected);
652 
653 	ev_document_model_set_page (priv->model, page);
654 }
655 
656 static void
ev_sidebar_init_tree_view(EvSidebarThumbnails * ev_sidebar_thumbnails)657 ev_sidebar_init_tree_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
658 {
659 	EvSidebarThumbnailsPrivate *priv;
660 	GtkTreeSelection *selection;
661 	GtkCellRenderer *renderer;
662 
663 	gtk_orientable_set_orientation (GTK_ORIENTABLE (ev_sidebar_thumbnails), GTK_ORIENTATION_VERTICAL);
664 
665 	priv = ev_sidebar_thumbnails->priv;
666 	priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
667 
668 	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
669 	g_signal_connect (selection, "changed",
670 			  G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
671 	gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
672 	renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
673 				 "xpad", 2,
674 				 "ypad", 2,
675 				 NULL);
676 	gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
677 						     NULL, renderer,
678 						     "pixbuf", 1,
679 						     NULL);
680 	gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
681 						     NULL, gtk_cell_renderer_text_new (),
682 						     "markup", 0, NULL);
683 	gtk_container_add (GTK_CONTAINER (priv->swindow), priv->tree_view);
684 	gtk_widget_show (priv->tree_view);
685 }
686 
687 static void
ev_sidebar_init_icon_view(EvSidebarThumbnails * ev_sidebar_thumbnails)688 ev_sidebar_init_icon_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
689 {
690 	EvSidebarThumbnailsPrivate *priv;
691 	GtkCellRenderer *renderer;
692 
693 	priv = ev_sidebar_thumbnails->priv;
694 	priv->icon_view = gtk_icon_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
695 
696 	renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
697 				 "xalign", 0.5,
698 				 "yalign", 1.0,
699 				 NULL);
700 	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->icon_view), renderer, FALSE);
701 	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (priv->icon_view),
702 					renderer, "pixbuf", 1, NULL);
703 
704 	renderer = g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
705 				 "alignment", PANGO_ALIGN_CENTER,
706 				 "wrap-mode", PANGO_WRAP_WORD_CHAR,
707 				 "xalign", 0.5,
708 				 "yalign", 0.0,
709 				 "width", THUMBNAIL_WIDTH,
710 				 "wrap-width", THUMBNAIL_WIDTH,
711 				 NULL);
712 	gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (priv->icon_view), renderer, FALSE);
713 	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (priv->icon_view),
714 					renderer, "markup", 0, NULL);
715 
716 	g_signal_connect (priv->icon_view, "selection-changed",
717 			  G_CALLBACK (ev_sidebar_icon_selection_changed), ev_sidebar_thumbnails);
718 
719 	gtk_container_add (GTK_CONTAINER (priv->swindow), priv->icon_view);
720 	gtk_widget_show (priv->icon_view);
721 }
722 
723 static gboolean
ev_sidebar_thumbnails_use_icon_view(EvSidebarThumbnails * sidebar_thumbnails)724 ev_sidebar_thumbnails_use_icon_view (EvSidebarThumbnails *sidebar_thumbnails)
725 {
726 	EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
727 
728 	return (ev_document_get_n_pages (priv->document) <= MAX_ICON_VIEW_PAGE_COUNT);
729 }
730 
731 static void
ev_sidebar_thumbnails_row_changed(GtkTreeModel * model,GtkTreePath * path,GtkTreeIter * iter,gpointer data)732 ev_sidebar_thumbnails_row_changed (GtkTreeModel *model,
733                                    GtkTreePath  *path,
734                                    GtkTreeIter  *iter,
735                                    gpointer      data)
736 {
737 	guint signal_id;
738 
739 	signal_id = GPOINTER_TO_UINT (data);
740 
741 	/* PREVENT GtkIconView "row-changed" handler to be reached, as it will
742 	 * perform a full invalidate and relayout of all items, See bug:
743 	 * https://bugzilla.gnome.org/show_bug.cgi?id=691448#c9 */
744 	g_signal_stop_emission (model, signal_id, 0);
745 }
746 
747 static void
ev_sidebar_thumbnails_init(EvSidebarThumbnails * ev_sidebar_thumbnails)748 ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
749 {
750 	EvSidebarThumbnailsPrivate *priv;
751 	guint signal_id;
752 
753 	priv = ev_sidebar_thumbnails->priv = ev_sidebar_thumbnails_get_instance_private (ev_sidebar_thumbnails);
754 
755 	priv->list_store = gtk_list_store_new (NUM_COLUMNS,
756 					       G_TYPE_STRING,
757 					       GDK_TYPE_PIXBUF,
758 					       G_TYPE_BOOLEAN,
759 					       EV_TYPE_JOB_THUMBNAIL);
760 
761 	signal_id = g_signal_lookup ("row-changed", GTK_TYPE_TREE_MODEL);
762 	g_signal_connect (GTK_TREE_MODEL (priv->list_store), "row-changed",
763 			  G_CALLBACK (ev_sidebar_thumbnails_row_changed),
764 			  GUINT_TO_POINTER (signal_id));
765 
766 	priv->swindow = gtk_scrolled_window_new (NULL, NULL);
767 
768 	/* We actually don't want GTK_POLICY_AUTOMATIC for horizontal scrollbar here
769 	 * it's just a workaround for bug #449462 (GTK2 only)
770 	 */
771 	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->swindow),
772 					GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
773 	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->swindow),
774 					     GTK_SHADOW_IN);
775 	priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->swindow));
776 	g_signal_connect_data (priv->vadjustment, "value-changed",
777 			       G_CALLBACK (adjustment_changed_cb),
778 			       ev_sidebar_thumbnails, NULL,
779 			       G_CONNECT_SWAPPED | G_CONNECT_AFTER);
780 	g_signal_connect_swapped (priv->swindow, "size-allocate",
781 				  G_CALLBACK (adjustment_changed_cb),
782 				  ev_sidebar_thumbnails);
783 	gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), priv->swindow, TRUE, TRUE, 0);
784 
785 	/* Put it all together */
786 	gtk_widget_show_all (priv->swindow);
787 }
788 
789 static void
ev_sidebar_thumbnails_set_current_page(EvSidebarThumbnails * sidebar,gint page)790 ev_sidebar_thumbnails_set_current_page (EvSidebarThumbnails *sidebar,
791 					gint                 page)
792 {
793 	GtkTreeView *tree_view;
794 	GtkTreePath *path;
795 
796 	path = gtk_tree_path_new_from_indices (page, -1);
797 
798 	if (sidebar->priv->tree_view) {
799 		tree_view = GTK_TREE_VIEW (sidebar->priv->tree_view);
800 		gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
801 		gtk_tree_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.0, 0.0);
802 	} else if (sidebar->priv->icon_view) {
803 
804 		g_signal_handlers_block_by_func
805 			(sidebar->priv->icon_view,
806 			 G_CALLBACK (ev_sidebar_icon_selection_changed), sidebar);
807 
808 		gtk_icon_view_select_path (GTK_ICON_VIEW (sidebar->priv->icon_view), path);
809 
810 		g_signal_handlers_unblock_by_func
811 			(sidebar->priv->icon_view,
812 			 G_CALLBACK (ev_sidebar_icon_selection_changed), sidebar);
813 
814 		gtk_icon_view_scroll_to_path (GTK_ICON_VIEW (sidebar->priv->icon_view), path, FALSE, 0.0, 0.0);
815 	}
816 
817 	gtk_tree_path_free (path);
818 }
819 
820 static void
page_changed_cb(EvSidebarThumbnails * sidebar,gint old_page,gint new_page)821 page_changed_cb (EvSidebarThumbnails *sidebar,
822 		 gint                 old_page,
823 		 gint                 new_page)
824 {
825 	ev_sidebar_thumbnails_set_current_page (sidebar, new_page);
826 }
827 
828 static gboolean
refresh(EvSidebarThumbnails * sidebar_thumbnails)829 refresh (EvSidebarThumbnails *sidebar_thumbnails)
830 {
831 	adjustment_changed_cb (sidebar_thumbnails);
832 	return FALSE;
833 }
834 
835 static void
ev_sidebar_thumbnails_reload(EvSidebarThumbnails * sidebar_thumbnails)836 ev_sidebar_thumbnails_reload (EvSidebarThumbnails *sidebar_thumbnails)
837 {
838 	EvDocumentModel *model;
839 
840 	if (sidebar_thumbnails->priv->loading_icons)
841 		g_hash_table_remove_all (sidebar_thumbnails->priv->loading_icons);
842 
843 	if (sidebar_thumbnails->priv->document == NULL ||
844 	    sidebar_thumbnails->priv->n_pages <= 0)
845 		return;
846 
847 	model = sidebar_thumbnails->priv->model;
848 
849 	ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
850 	ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
851 
852 	/* Trigger a redraw */
853 	sidebar_thumbnails->priv->start_page = -1;
854 	sidebar_thumbnails->priv->end_page = -1;
855 	ev_sidebar_thumbnails_set_current_page (sidebar_thumbnails,
856 						ev_document_model_get_page (model));
857 	g_idle_add ((GSourceFunc)refresh, sidebar_thumbnails);
858 }
859 
860 static void
ev_sidebar_thumbnails_rotation_changed_cb(EvDocumentModel * model,GParamSpec * pspec,EvSidebarThumbnails * sidebar_thumbnails)861 ev_sidebar_thumbnails_rotation_changed_cb (EvDocumentModel     *model,
862 					   GParamSpec          *pspec,
863 					   EvSidebarThumbnails *sidebar_thumbnails)
864 {
865 	gint rotation = ev_document_model_get_rotation (model);
866 
867 	sidebar_thumbnails->priv->rotation = rotation;
868 	ev_sidebar_thumbnails_reload (sidebar_thumbnails);
869 }
870 
871 static void
ev_sidebar_thumbnails_inverted_colors_changed_cb(EvDocumentModel * model,GParamSpec * pspec,EvSidebarThumbnails * sidebar_thumbnails)872 ev_sidebar_thumbnails_inverted_colors_changed_cb (EvDocumentModel     *model,
873 						  GParamSpec          *pspec,
874 						  EvSidebarThumbnails *sidebar_thumbnails)
875 {
876 	gboolean inverted_colors = ev_document_model_get_inverted_colors (model);
877 
878 	sidebar_thumbnails->priv->inverted_colors = inverted_colors;
879 	ev_sidebar_thumbnails_reload (sidebar_thumbnails);
880 }
881 
882 static void
thumbnail_job_completed_callback(EvJobThumbnail * job,EvSidebarThumbnails * sidebar_thumbnails)883 thumbnail_job_completed_callback (EvJobThumbnail      *job,
884 				  EvSidebarThumbnails *sidebar_thumbnails)
885 {
886 	EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
887 	GtkTreeIter *iter;
888 
889 	iter = (GtkTreeIter *) g_object_get_data (G_OBJECT (job), "tree_iter");
890 	if (priv->inverted_colors && priv->document->iswebdocument == FALSE)
891 		ev_document_misc_invert_pixbuf (job->thumbnail);
892 	gtk_list_store_set (priv->list_store,
893 			    iter,
894 			    COLUMN_PIXBUF, job->thumbnail,
895 			    COLUMN_THUMBNAIL_SET, TRUE,
896 			    COLUMN_JOB, NULL,
897 			    -1);
898 
899 	gtk_widget_queue_draw (priv->icon_view);
900 }
901 
902 static void
ev_sidebar_thumbnails_document_changed_cb(EvDocumentModel * model,GParamSpec * pspec,EvSidebarThumbnails * sidebar_thumbnails)903 ev_sidebar_thumbnails_document_changed_cb (EvDocumentModel     *model,
904 					   GParamSpec          *pspec,
905 					   EvSidebarThumbnails *sidebar_thumbnails)
906 {
907 	EvDocument *document = ev_document_model_get_document (model);
908 	EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
909 
910 	if (!EV_IS_DOCUMENT_THUMBNAILS (document) ||
911 	    ev_document_get_n_pages (document) <= 0 ||
912 	    !ev_document_check_dimensions (document)) {
913 		return;
914 	}
915 
916 	priv->size_cache = ev_thumbnails_size_cache_get (document);
917 	priv->document = document;
918 	priv->n_pages = ev_document_get_n_pages (document);
919 	priv->rotation = ev_document_model_get_rotation (model);
920 	priv->inverted_colors = ev_document_model_get_inverted_colors (model);
921 	priv->loading_icons = g_hash_table_new_full (g_str_hash,
922 						     g_str_equal,
923 						     (GDestroyNotify)g_free,
924 						     (GDestroyNotify)g_object_unref);
925 
926 	ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
927 	ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
928 
929 	/* Create the view widget, and remove the old one, if needed */
930 	if (ev_sidebar_thumbnails_use_icon_view (sidebar_thumbnails)) {
931 		if (priv->tree_view) {
932 			gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->tree_view);
933 			priv->tree_view = NULL;
934 		}
935 
936 		if (! priv->icon_view) {
937 			ev_sidebar_init_icon_view (sidebar_thumbnails);
938 			g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
939 		} else {
940 			gtk_widget_queue_resize (priv->icon_view);
941 		}
942 	} else {
943 		if (priv->icon_view) {
944 			gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->icon_view);
945 			priv->icon_view = NULL;
946 		}
947 
948 		if (! priv->tree_view) {
949 			ev_sidebar_init_tree_view (sidebar_thumbnails);
950 			g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
951 		}
952 	}
953 
954 	/* Connect to the signal and trigger a fake callback */
955 	g_signal_connect_swapped (priv->model, "page-changed",
956 				  G_CALLBACK (page_changed_cb),
957 				  sidebar_thumbnails);
958 	g_signal_connect (priv->model, "notify::rotation",
959 			  G_CALLBACK (ev_sidebar_thumbnails_rotation_changed_cb),
960 			  sidebar_thumbnails);
961 	g_signal_connect (priv->model, "notify::inverted-colors",
962 			  G_CALLBACK (ev_sidebar_thumbnails_inverted_colors_changed_cb),
963 			  sidebar_thumbnails);
964 	g_signal_connect_swapped (priv->model, "notify::fullscreen",
965 			          G_CALLBACK (ev_sidebar_fullscreen_cb),
966 			          sidebar_thumbnails);
967 	sidebar_thumbnails->priv->start_page = -1;
968 	sidebar_thumbnails->priv->end_page = -1;
969 	ev_sidebar_thumbnails_set_current_page (sidebar_thumbnails,
970 						ev_document_model_get_page (model));
971 	adjustment_changed_cb (sidebar_thumbnails);
972 }
973 
974 static void
ev_sidebar_thumbnails_set_model(EvSidebarPage * sidebar_page,EvDocumentModel * model)975 ev_sidebar_thumbnails_set_model (EvSidebarPage   *sidebar_page,
976 				 EvDocumentModel *model)
977 {
978 	EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (sidebar_page);
979 	EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
980 
981 	if (priv->model == model)
982 		return;
983 
984 	priv->model = model;
985 	g_signal_connect (model, "notify::document",
986 			  G_CALLBACK (ev_sidebar_thumbnails_document_changed_cb),
987 			  sidebar_page);
988 }
989 
990 static gboolean
ev_sidebar_thumbnails_clear_job(GtkTreeModel * model,GtkTreePath * path,GtkTreeIter * iter,gpointer data)991 ev_sidebar_thumbnails_clear_job (GtkTreeModel *model,
992 			         GtkTreePath *path,
993 			         GtkTreeIter *iter,
994 				 gpointer data)
995 {
996 	EvJob *job;
997 
998 	gtk_tree_model_get (model, iter, COLUMN_JOB, &job, -1);
999 
1000 	if (job != NULL) {
1001 		ev_job_cancel (job);
1002 		g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, data);
1003 		g_object_unref (job);
1004 	}
1005 
1006 	return FALSE;
1007 }
1008 
1009 static void
ev_sidebar_thumbnails_clear_model(EvSidebarThumbnails * sidebar_thumbnails)1010 ev_sidebar_thumbnails_clear_model (EvSidebarThumbnails *sidebar_thumbnails)
1011 {
1012 	EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
1013 
1014 	gtk_tree_model_foreach (GTK_TREE_MODEL (priv->list_store), ev_sidebar_thumbnails_clear_job, sidebar_thumbnails);
1015 	gtk_list_store_clear (priv->list_store);
1016 }
1017 
1018 static gboolean
ev_sidebar_thumbnails_support_document(EvSidebarPage * sidebar_page,EvDocument * document)1019 ev_sidebar_thumbnails_support_document (EvSidebarPage   *sidebar_page,
1020 				        EvDocument *document)
1021 {
1022 	return (EV_IS_DOCUMENT_THUMBNAILS (document));
1023 }
1024 
1025 static const gchar*
ev_sidebar_thumbnails_get_label(EvSidebarPage * sidebar_page)1026 ev_sidebar_thumbnails_get_label (EvSidebarPage *sidebar_page)
1027 {
1028 	return _("Thumbnails");
1029 }
1030 
1031 static void
ev_sidebar_thumbnails_page_iface_init(EvSidebarPageInterface * iface)1032 ev_sidebar_thumbnails_page_iface_init (EvSidebarPageInterface *iface)
1033 {
1034 	iface->support_document = ev_sidebar_thumbnails_support_document;
1035 	iface->set_model = ev_sidebar_thumbnails_set_model;
1036 	iface->get_label = ev_sidebar_thumbnails_get_label;
1037 }
1038