]> www.fi.muni.cz Git - evince.git/blob - shell/ev-jobs.c
Clean up selection to be much smoother!
[evince.git] / shell / ev-jobs.c
1 #include "ev-jobs.h"
2 #include "ev-job-queue.h"
3 #include "ev-document-thumbnails.h"
4 #include "ev-document-links.h"
5 #include "ev-document-fonts.h"
6 #include "ev-selection.h"
7 #include "ev-async-renderer.h"
8
9 static void ev_job_init                 (EvJob               *job);
10 static void ev_job_class_init           (EvJobClass          *class);
11 static void ev_job_links_init           (EvJobLinks          *job);
12 static void ev_job_links_class_init     (EvJobLinksClass     *class);
13 static void ev_job_render_init          (EvJobRender         *job);
14 static void ev_job_render_class_init    (EvJobRenderClass    *class);
15 static void ev_job_thumbnail_init       (EvJobThumbnail      *job);
16 static void ev_job_thumbnail_class_init (EvJobThumbnailClass *class);
17 static void ev_job_load_init            (EvJobLoad           *job);
18 static void ev_job_load_class_init      (EvJobLoadClass      *class);
19
20 enum
21 {
22         FINISHED,
23         LAST_SIGNAL
24 };
25
26 static guint job_signals[LAST_SIGNAL] = { 0 };
27
28 G_DEFINE_TYPE (EvJob, ev_job, G_TYPE_OBJECT)
29 G_DEFINE_TYPE (EvJobLinks, ev_job_links, EV_TYPE_JOB)
30 G_DEFINE_TYPE (EvJobRender, ev_job_render, EV_TYPE_JOB)
31 G_DEFINE_TYPE (EvJobThumbnail, ev_job_thumbnail, EV_TYPE_JOB)
32 G_DEFINE_TYPE (EvJobLoad, ev_job_load, EV_TYPE_JOB)
33 G_DEFINE_TYPE (EvJobFonts, ev_job_fonts, EV_TYPE_JOB)
34
35 static void ev_job_init (EvJob *job) { /* Do Nothing */ }
36
37 static void
38 ev_job_dispose (GObject *object)
39 {
40         EvJob *job;
41
42         job = EV_JOB (object);
43
44         if (job->document) {
45                 g_object_unref (job->document);
46                 job->document = NULL;
47         }
48
49         (* G_OBJECT_CLASS (ev_job_parent_class)->dispose) (object);
50 }
51
52 static void
53 ev_job_class_init (EvJobClass *class)
54 {
55         GObjectClass *oclass;
56
57         oclass = G_OBJECT_CLASS (class);
58
59         oclass->dispose = ev_job_dispose;
60
61         job_signals [FINISHED] =
62                 g_signal_new ("finished",
63                               EV_TYPE_JOB,
64                               G_SIGNAL_RUN_LAST,
65                               G_STRUCT_OFFSET (EvJobClass, finished),
66                               NULL, NULL,
67                               g_cclosure_marshal_VOID__VOID,
68                               G_TYPE_NONE, 0);
69 }
70
71
72 static void ev_job_links_init (EvJobLinks *job) { /* Do Nothing */ }
73
74 static void
75 ev_job_links_dispose (GObject *object)
76 {
77         EvJobLinks *job;
78
79         job = EV_JOB_LINKS (object);
80
81         if (job->model) {
82                 g_object_unref (job->model);
83                 job->model = NULL;
84         }
85
86         (* G_OBJECT_CLASS (ev_job_links_parent_class)->dispose) (object);
87 }
88
89 static void
90 ev_job_links_class_init (EvJobLinksClass *class)
91 {
92         GObjectClass *oclass;
93
94         oclass = G_OBJECT_CLASS (class);
95
96         oclass->dispose = ev_job_links_dispose;
97 }
98
99
100 static void ev_job_render_init (EvJobRender *job) { /* Do Nothing */ }
101
102 static void
103 ev_job_render_dispose (GObject *object)
104 {
105         EvJobRender *job;
106
107         job = EV_JOB_RENDER (object);
108
109         if (job->pixbuf) {
110                 g_object_unref (job->pixbuf);
111                 job->pixbuf = NULL;
112         }
113
114         if (job->rc) {
115                 g_object_unref (job->rc);
116                 job->rc = NULL;
117         }
118
119         if (job->selection) {
120                 g_object_unref (job->selection);
121                 job->selection = NULL;
122         }
123
124         if (job->selection_region) {
125                 gdk_region_destroy (job->selection_region);
126                 job->selection_region = NULL;
127         }
128
129         (* G_OBJECT_CLASS (ev_job_render_parent_class)->dispose) (object);
130 }
131
132 static void
133 ev_job_render_class_init (EvJobRenderClass *class)
134 {
135         GObjectClass *oclass;
136
137         oclass = G_OBJECT_CLASS (class);
138
139         oclass->dispose = ev_job_render_dispose;
140 }
141
142 static void ev_job_thumbnail_init (EvJobThumbnail *job) { /* Do Nothing */ }
143
144 static void
145 ev_job_thumbnail_dispose (GObject *object)
146 {
147         EvJobThumbnail *job;
148
149         job = EV_JOB_THUMBNAIL (object);
150
151         if (job->thumbnail) {
152                 g_object_unref (job->thumbnail);
153                 job->thumbnail = NULL;
154         }
155
156         (* G_OBJECT_CLASS (ev_job_thumbnail_parent_class)->dispose) (object);
157 }
158
159 static void
160 ev_job_thumbnail_class_init (EvJobThumbnailClass *class)
161 {
162         GObjectClass *oclass;
163
164         oclass = G_OBJECT_CLASS (class);
165
166         oclass->dispose = ev_job_thumbnail_dispose;
167 }
168
169 static void ev_job_load_init (EvJobLoad *job) { /* Do Nothing */ }
170
171 static void
172 ev_job_load_dispose (GObject *object)
173 {
174         EvJobLoad *job;
175
176         job = EV_JOB_LOAD (object);
177
178         if (job->uri) {
179                 g_free (job->uri);
180                 job->uri = NULL;
181         }
182
183         if (job->error) {
184                 g_error_free (job->error);
185                 job->error = NULL;
186         }
187
188         (* G_OBJECT_CLASS (ev_job_load_parent_class)->dispose) (object);
189 }
190
191 static void
192 ev_job_load_class_init (EvJobLoadClass *class)
193 {
194         GObjectClass *oclass;
195
196         oclass = G_OBJECT_CLASS (class);
197
198         oclass->dispose = ev_job_load_dispose;
199 }
200
201 /* Public functions */
202 void
203 ev_job_finished (EvJob *job)
204 {
205         g_return_if_fail (EV_IS_JOB (job));
206
207         g_signal_emit (job, job_signals[FINISHED], 0);
208 }
209
210 EvJob *
211 ev_job_links_new (EvDocument *document)
212 {
213         EvJob *job;
214
215         job = g_object_new (EV_TYPE_JOB_LINKS, NULL);
216         job->document = g_object_ref (document);
217
218         return job;
219 }
220
221 void
222 ev_job_links_run (EvJobLinks *job)
223 {
224         g_return_if_fail (EV_IS_JOB_LINKS (job));
225
226         ev_document_doc_mutex_lock ();
227         job->model = ev_document_links_get_links_model (EV_DOCUMENT_LINKS (EV_JOB (job)->document));
228         EV_JOB (job)->finished = TRUE;
229         ev_document_doc_mutex_unlock ();
230 }
231
232
233 EvJob *
234 ev_job_render_new (EvDocument      *document,
235                    EvRenderContext *rc,
236                    gint             width,
237                    gint             height,
238                    EvRectangle     *selection_points,
239                    guint            text,
240                    guint            base,
241                    gboolean         include_links,
242                    gboolean         include_text,
243                    gboolean         include_selection)
244 {
245         EvJobRender *job;
246
247         g_return_val_if_fail (EV_IS_RENDER_CONTEXT (rc), NULL);
248         if (include_selection)
249                 g_return_val_if_fail (selection_points != NULL, NULL);
250
251         job = g_object_new (EV_TYPE_JOB_RENDER, NULL);
252
253         EV_JOB (job)->document = g_object_ref (document);
254         job->rc = g_object_ref (rc);
255         job->target_width = width;
256         job->target_height = height;
257         job->text = text;
258         job->base = base;
259         job->include_links = include_links;
260         job->include_text = include_text;
261         job->include_selection = include_selection;
262
263         if (include_selection)
264                 job->selection_points = *selection_points;
265
266         if (EV_IS_ASYNC_RENDERER (document)) {  
267                 EV_JOB (job)->async = TRUE;
268         }
269
270         return EV_JOB (job);
271 }
272
273 static void
274 render_finished_cb (EvDocument *document, GdkPixbuf *pixbuf, EvJobRender *job)
275 {
276         g_signal_handlers_disconnect_by_func (EV_JOB (job)->document,
277                                               render_finished_cb, job);
278
279         EV_JOB (job)->finished = TRUE;
280         job->pixbuf = g_object_ref (pixbuf);
281         ev_job_finished (EV_JOB (job));
282 }
283
284 void
285 ev_job_render_run (EvJobRender *job)
286 {
287         g_return_if_fail (EV_IS_JOB_RENDER (job));
288
289         ev_document_doc_mutex_lock ();
290
291         if (EV_JOB (job)->async) {
292                 EvAsyncRenderer *renderer = EV_ASYNC_RENDERER (EV_JOB (job)->document);
293                 ev_async_renderer_render_pixbuf (renderer, job->rc->page, job->rc->scale);
294                 g_signal_connect (EV_JOB (job)->document, "render_finished",
295                                   G_CALLBACK (render_finished_cb), job);
296         } else {
297                 job->pixbuf = ev_document_render_pixbuf (EV_JOB (job)->document, job->rc);
298                 if (job->include_links)
299                         job->link_mapping = ev_document_get_links (EV_JOB (job)->document, job->rc->page);
300                 if (job->include_text && EV_IS_SELECTION (EV_JOB (job)->document))
301                         job->text_mapping = ev_selection_get_selection_map (EV_SELECTION (EV_JOB (job)->document), job->rc);
302                 if (job->include_selection && EV_IS_SELECTION (EV_JOB (job)->document)) {
303                         ev_selection_render_selection (EV_SELECTION (EV_JOB (job)->document),
304                                                        job->rc,
305                                                        &(job->selection),
306                                                        &(job->selection_points),
307                                                        NULL,
308                                                        job->text, job->base);
309                         job->selection_region =
310                                 ev_selection_get_selection_region (EV_SELECTION (EV_JOB (job)->document),
311                                                                    job->rc,
312                                                                    &(job->selection_points));
313                 }
314
315                 EV_JOB (job)->finished = TRUE;
316         }
317
318         ev_document_doc_mutex_unlock ();
319 }
320
321 EvJob *
322 ev_job_thumbnail_new (EvDocument   *document,
323                       gint          page,
324                       int           rotation,
325                       gint          requested_width)
326 {
327         EvJobThumbnail *job;
328
329         job = g_object_new (EV_TYPE_JOB_THUMBNAIL, NULL);
330
331         EV_JOB (job)->document = g_object_ref (document);
332         job->page = page;
333         job->rotation = rotation;
334         job->requested_width = requested_width;
335
336         return EV_JOB (job);
337 }
338
339 void
340 ev_job_thumbnail_run (EvJobThumbnail *job)
341 {
342         g_return_if_fail (EV_IS_JOB_THUMBNAIL (job));
343
344         ev_document_doc_mutex_lock ();
345
346         job->thumbnail =
347                 ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (EV_JOB (job)->document),
348                                                       job->page,
349                                                       job->rotation,
350                                                       job->requested_width,
351                                                       TRUE);
352         EV_JOB (job)->finished = TRUE;
353
354         ev_document_doc_mutex_unlock ();
355 }
356
357 EvJob *
358 ev_job_load_new (EvDocument *document,
359                  const gchar *uri)
360 {
361         EvJobLoad *job;
362
363         job = g_object_new (EV_TYPE_JOB_LOAD, NULL);
364
365         EV_JOB (job)->document = g_object_ref (document);
366         job->uri = g_strdup (uri);
367
368         return EV_JOB (job);
369 }
370
371 void
372 ev_job_load_run (EvJobLoad *job)
373 {
374         g_return_if_fail (EV_IS_JOB_LOAD (job));
375
376         ev_document_doc_mutex_lock ();
377         
378         if (job->error) {
379                 g_error_free (job->error);
380                 job->error = NULL;
381         }
382             
383         ev_document_load (EV_JOB(job)->document, job->uri, &job->error);
384         
385         EV_JOB (job)->finished = TRUE;
386
387         ev_document_doc_mutex_unlock ();
388 }
389
390 static void ev_job_fonts_init (EvJobFonts *job) { /* Do Nothing */ }
391
392 static void ev_job_fonts_class_init (EvJobFontsClass *class) { /* Do Nothing */ }
393
394 EvJob *
395 ev_job_fonts_new (EvDocument *document)
396 {
397         EvJobFonts *job;
398
399         job = g_object_new (EV_TYPE_JOB_FONTS, NULL);
400
401         EV_JOB (job)->document = g_object_ref (document);
402
403         return EV_JOB (job);
404 }
405
406 void
407 ev_job_fonts_run (EvJobFonts *job)
408 {
409         EvDocumentFonts *fonts;
410
411         g_return_if_fail (EV_IS_JOB_FONTS (job));
412
413         ev_document_doc_mutex_lock ();
414         
415         fonts = EV_DOCUMENT_FONTS (EV_JOB (job)->document);
416         job->scan_completed = !ev_document_fonts_scan (fonts, 20);
417         
418         EV_JOB (job)->finished = TRUE;
419
420         ev_document_doc_mutex_unlock ();
421 }