]> www.fi.muni.cz Git - evince.git/blob - shell/ev-sidebar-thumbnails.c
Ensure thumbnails selection, toolbar page control and current page are in
[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
32 #include "ev-sidebar-thumbnails.h"
33 #include "ev-document-thumbnails.h"
34 #include "ev-document-misc.h"
35 #include "ev-window.h"
36 #include "ev-utils.h"
37
38 #define THUMBNAIL_WIDTH 75
39 /* Amount of time we devote to each iteration of the idle, in microseconds */
40 #define IDLE_WORK_LENGTH 5000
41
42 struct _EvSidebarThumbnailsPrivate {
43         GtkWidget *tree_view;
44         GtkAdjustment *vadjustment;
45         GtkListStore *list_store;
46         EvDocument *document;
47
48         guint idle_id;
49         gint current_page, n_pages, pages_done;
50         GtkTreeIter current_page_iter;
51 };
52
53 enum {
54         COLUMN_PAGE_STRING,
55         COLUMN_PIXBUF,
56         COLUMN_THUMBNAIL_SET,
57         NUM_COLUMNS
58 };
59
60 static GtkVBoxClass *parent_class;
61
62 G_DEFINE_TYPE (EvSidebarThumbnails, ev_sidebar_thumbnails, GTK_TYPE_VBOX);
63
64 #define EV_SIDEBAR_THUMBNAILS_GET_PRIVATE(object) \
65         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_THUMBNAILS, EvSidebarThumbnailsPrivate));
66
67 static void
68 ev_sidebar_thumbnails_destroy (GtkObject *object)
69 {
70         EvSidebarThumbnails *ev_sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (object);
71         EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
72
73         if (priv->idle_id != 0) {
74                 g_source_remove (priv->idle_id);
75
76                 priv->idle_id = 0;
77         }
78
79         GTK_OBJECT_CLASS (parent_class)->destroy (object);
80 }
81
82 static void
83 ev_sidebar_thumbnails_class_init (EvSidebarThumbnailsClass *ev_sidebar_thumbnails_class)
84 {
85         GObjectClass *g_object_class;
86         GtkObjectClass *gtk_object_class;
87
88         g_object_class = G_OBJECT_CLASS (ev_sidebar_thumbnails_class);
89         gtk_object_class = GTK_OBJECT_CLASS (ev_sidebar_thumbnails_class);
90
91         parent_class = g_type_class_peek_parent (ev_sidebar_thumbnails_class);
92
93         gtk_object_class->destroy = ev_sidebar_thumbnails_destroy;
94
95         g_type_class_add_private (g_object_class, sizeof (EvSidebarThumbnailsPrivate));
96
97 }
98
99 static void
100 adjustment_changed_cb (GtkAdjustment       *adjustment,
101                        EvSidebarThumbnails *ev_sidebar_thumbnails)
102 {
103         EvSidebarThumbnailsPrivate *priv;
104         GtkTreePath *path;
105         GtkTreeIter iter;
106         int page;
107         gboolean thumbnail_set;
108
109         priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
110
111         gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
112                                        1, 1, &path,
113                                        NULL, NULL, NULL);
114         if (!path)
115                 return;
116
117         page = gtk_tree_path_get_indices (path)[0];
118         if (page == priv->current_page)
119                 return;
120         gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store),
121                                  &iter, path);
122         gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store), &iter,
123                             COLUMN_THUMBNAIL_SET, &thumbnail_set,
124                             -1);
125         if (! thumbnail_set) {
126                 priv->current_page = page;
127                 priv->current_page_iter = iter;
128                 
129         }
130 }
131
132 static void
133 ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
134                                    EvSidebarThumbnails *ev_sidebar_thumbnails)
135 {
136         EvSidebarThumbnailsPrivate *priv;
137         GtkWidget *window;
138         GtkTreePath *path;
139         GtkTreeIter iter;
140         int page;
141
142         priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
143   
144         if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
145                 return;
146         
147         path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
148                                         &iter);
149
150         page = gtk_tree_path_get_indices (path)[0] + 1;
151
152         gtk_tree_path_free (path);
153
154         window = gtk_widget_get_ancestor (GTK_WIDGET (ev_sidebar_thumbnails),
155                                           EV_TYPE_WINDOW);
156         if (window && ev_document_get_page (priv->document) != page) {
157                 ev_window_open_page (EV_WINDOW (window), page);
158         }
159 }
160
161 static void
162 ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
163 {
164         GtkWidget *swindow;
165         EvSidebarThumbnailsPrivate *priv;
166         GtkCellRenderer *renderer;
167         GtkTreeSelection *selection;
168
169         priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
170         
171         priv->list_store = gtk_list_store_new (NUM_COLUMNS, G_TYPE_STRING, GDK_TYPE_PIXBUF, G_TYPE_BOOLEAN);
172         priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
173         
174         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
175         g_signal_connect (selection, "changed",
176                           G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
177         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
178         renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
179                                  "xpad", 2,
180                                  "ypad", 2,
181                                  NULL);
182         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
183                                                      NULL, renderer,
184                                                      "pixbuf", 1,
185                                                      NULL);
186         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
187                                                      NULL, gtk_cell_renderer_text_new (),
188                                                      "markup", 0, NULL);
189
190         g_object_unref (priv->list_store);
191
192         swindow = gtk_scrolled_window_new (NULL, NULL);
193         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
194                                         GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
195         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
196                                              GTK_SHADOW_IN);
197         priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (swindow));
198         g_signal_connect (G_OBJECT (priv->vadjustment), "value-changed",
199                           G_CALLBACK (adjustment_changed_cb),
200                           ev_sidebar_thumbnails);
201         gtk_container_add (GTK_CONTAINER (swindow), priv->tree_view);
202         gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), swindow, TRUE, TRUE, 0);
203
204         gtk_widget_show_all (swindow);
205 }
206
207 GtkWidget *
208 ev_sidebar_thumbnails_new (void)
209 {
210         GtkWidget *ev_sidebar_thumbnails;
211
212         ev_sidebar_thumbnails = g_object_new (EV_TYPE_SIDEBAR_THUMBNAILS, NULL);
213
214         return ev_sidebar_thumbnails;
215 }
216
217 static gboolean
218 do_one_iteration (EvSidebarThumbnails *ev_sidebar_thumbnails)
219 {
220         EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
221         GdkPixbuf *pixbuf;
222         gboolean thumbnail_set;
223
224         gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store),
225                             &(priv->current_page_iter),
226                             COLUMN_THUMBNAIL_SET, &thumbnail_set,
227                             -1);
228         if (!thumbnail_set) {
229                 pixbuf = ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (priv->document),
230                                                                priv->current_page, THUMBNAIL_WIDTH);
231
232                 gtk_list_store_set (priv->list_store,
233                                     &(priv->current_page_iter),
234                                     COLUMN_PIXBUF, pixbuf,
235                                     -1);
236
237                 g_object_unref (pixbuf);
238                 priv->pages_done ++;
239         }
240
241         priv->current_page++;
242
243         if (priv->current_page == priv->n_pages) {
244                 priv->current_page = 0;
245                 gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->list_store),
246                                                &(priv->current_page_iter));
247         } else {
248                 gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store),
249                                           &(priv->current_page_iter));
250         }
251
252         if (priv->pages_done == priv->n_pages)
253                 return FALSE;
254         else
255                 return TRUE;
256 }
257
258 static gboolean
259 populate_thumbnails_idle (gpointer data)
260 {
261         GTimer *timer;
262         int i;
263         gdouble time_elapsed = 0;
264
265         EvSidebarThumbnails *ev_sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (data);
266         EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
267
268 #if PROFILE_THUMB == 1
269         static GTimer *total_timer;
270         static gboolean first_time = TRUE;
271
272         if (first_time) {
273                 total_timer = g_timer_new ();
274                 first_time = FALSE;
275                 g_timer_start (total_timer);
276         }
277 #endif
278
279         /* undo the thumbnailing idle and handler */
280         if (priv->pages_done == priv->n_pages) {
281                 priv->idle_id = 0;
282                 g_signal_handlers_disconnect_by_func (priv->vadjustment,
283                                                       adjustment_changed_cb,
284                                                       ev_sidebar_thumbnails);
285 #if PROFILE_THUMB == 1
286                 time_elapsed = g_timer_elapsed (total_timer, NULL);
287                 g_timer_destroy (total_timer);
288                 g_print ("%d rows done in %f seconds\n",
289                          gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->list_store), NULL),
290                          time_elapsed);
291 #endif
292                 return FALSE;
293         }
294
295         timer = g_timer_new ();
296         i = 0;
297         g_timer_start (timer);
298         while (do_one_iteration (ev_sidebar_thumbnails)) {
299                 i++;
300                 time_elapsed = g_timer_elapsed (timer, NULL);
301                 if (time_elapsed > IDLE_WORK_LENGTH/1000000)
302                         break;
303         }
304         g_timer_destroy (timer);
305 #if PROFILE_THUMB == 2
306         g_print ("%d rows done this idle in %f seconds\n", i, time_elapsed);
307 #endif
308
309         return TRUE;
310 }
311
312 void
313 ev_sidebar_thumbnails_select_page (EvSidebarThumbnails *sidebar,
314                                    int                  page)
315 {
316         GtkTreePath *path;
317         GtkTreeSelection *selection;
318
319         path = gtk_tree_path_new_from_indices (page - 1, -1);
320         selection = gtk_tree_view_get_selection
321                         (GTK_TREE_VIEW (sidebar->priv->tree_view));
322
323         if (path) {
324                 gtk_tree_selection_select_path (selection, path);
325                 gtk_tree_path_free (path);      
326         }
327 }
328
329
330 void
331 ev_sidebar_thumbnails_set_document (EvSidebarThumbnails *sidebar_thumbnails,
332                                     EvDocument          *document)
333 {
334         GdkPixbuf *loading_icon;
335         gint i, n_pages;
336         GtkTreeIter iter;
337         gchar *page;
338         gint width = THUMBNAIL_WIDTH;
339         gint height = THUMBNAIL_WIDTH;
340
341         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
342
343         g_return_if_fail (EV_IS_DOCUMENT_THUMBNAILS (document));
344
345         if (priv->idle_id != 0) {
346                 g_source_remove (priv->idle_id);
347         }
348         n_pages = ev_document_get_n_pages (document);
349
350         priv->document = document;
351         priv->idle_id = g_idle_add (populate_thumbnails_idle, sidebar_thumbnails);
352         priv->n_pages = n_pages;
353
354         /* We get the dimensions of the first doc so that we can make a blank
355          * icon.  */
356         ev_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (priv->document),
357                                                0, THUMBNAIL_WIDTH, &width, &height);
358         loading_icon = ev_document_misc_get_thumbnail_frame (width, height, NULL);
359
360         for (i = 0; i < n_pages; i++) {
361                 page = g_strdup_printf ("<i>%d</i>", i + 1);
362                 gtk_list_store_append (priv->list_store, &iter);
363                 gtk_list_store_set (priv->list_store, &iter,
364                                     COLUMN_PAGE_STRING, page,
365                                     COLUMN_PIXBUF, loading_icon,
366                                     COLUMN_THUMBNAIL_SET, FALSE,
367                                     -1);
368                 g_free (page);
369         }
370
371         g_object_unref (loading_icon);
372         gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->list_store),
373                                        &(priv->current_page_iter));
374         priv->current_page = 0;
375         priv->pages_done = 0;
376 }
377