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