comparison src/pixbuf-renderer.c @ 23:17acca639a86

##### Note: GQview CVS on sourceforge is not always up to date, please use ##### ##### an offical release when making enhancements and translation updates. ##### Fri Mar 18 19:52:38 2005 John Ellis <johne@verizon.net> * pixbuf-renderer.c: Fix broken 2pass queueing so it merges a single queue item for each tile, and now that a tile holds a queue pointer there is no need to walk the tile list to find a possible merge victim. Thu Mar 17 19:23:56 2005 John Ellis <johne@verizon.net> * image.c, pixbuf-renderer.[ch]: More progress, and 2 pass zoom now works again (mostly). Tue Mar 15 23:17:41 2005 John Ellis <johne@verizon.net> * image.[ch]: Start conversion to use of PixbufRenderer. * pixbuf-renderer.[ch]: Additions and fixes for use by ImageWindow. * fullscreen.c, image-overlay.c, img-view.c, info.c, layout.c, layout_image.c, print.c: Changes for above ImageWindow conversion. * typedefs.h: Remove unused ImageWindow variables. Mon Mar 14 20:45:50 2005 John Ellis <johne@verizon.net> * pan-view.c: Convert to using new PixbufRenderer. * pixbuf-renderer.[ch]: Clean up and fixes. Sun Mar 13 10:21:35 2005 John Ellis <johne@verizon.net> * image.c (image_scroller_start): Unref scroller pixbuf after adding it as an overlay to avoid leaking it. * pixbuf-renderer.[ch]: All functions defined in header file are now implemented. Fri Mar 11 17:05:36 2005 John Ellis <johne@verizon.net> * pixbuf-renderer.[ch]: Set up object properties and additional. Mar 10 19:22:18 2005 John Ellis <johne@verizon.net> * pixbuf-renderer.[ch]: FIx scrolling to use old method of gdk_draw and not gdk_scroll, as scroll often invalidates the entire window (!). Also merged in source tiles code from image.c. * pan-view.c: Change picked up when testing PixbufRender zoom. Tue Mar 8 23:26:00 2005 John Ellis <johne@verizon.net> * pixbuf-renderer.[ch]: Start of turning image drawing portion of ImageWindow into an actual GtkWidget. * src/Makefile.am: Add pixbuf-renderer.[ch]. * pan-view.c: Added temporary hack button and callback for testing PixbufRenderer widget.
author gqview
date Sat, 19 Mar 2005 01:15:17 +0000
parents
children 104e34f9ab1f
comparison
equal deleted inserted replaced
22:e9625635e162 23:17acca639a86
1 /*
2 * GQview
3 * (C) 2005 John Ellis
4 *
5 * Author: John Ellis
6 *
7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <math.h>
15 #include "pixbuf-renderer.h"
16 #include "intl.h"
17
18 #include <gtk/gtk.h>
19
20
21 /* comment this out if not using this from within GQview
22 * defining GQVIEW_BUILD does these things:
23 * - Sets the shift-click scroller pixbuf to a nice icon instead of a black box
24 */
25 #define GQVIEW_BUILD 1
26
27
28 #ifdef GQVIEW_BUILD
29 #include "pixbuf_util.h"
30 #endif
31
32
33 /* size to use when breaking up image pane for rendering */
34 #define PR_TILE_SIZE 128
35
36 /* default size of tile cache (mb) */
37 #define PR_CACHE_SIZE_DEFAULT 8
38
39 /* default min and max zoom */
40 #define PR_ZOOM_MIN -32.0
41 #define PR_ZOOM_MAX 32.0
42
43 /* distance to drag mouse to disable image flip */
44 #define PR_DRAG_SCROLL_THRESHHOLD 4
45
46 /* increase pan rate when holding down shift */
47 #define PR_PAN_SHIFT_MULTIPLIER 6
48
49 /* scroller config */
50 #define PR_SCROLLER_UPDATES_PER_SEC 30
51 #define PR_SCROLLER_DEAD_ZONE 6
52
53 /* alpha channel checkerboard background (same as gimp) */
54 #define PR_ALPHA_CHECK1 0x00999999
55 #define PR_ALPHA_CHECK2 0x00666666
56 #define PR_ALPHA_CHECK_SIZE 16
57
58 /* when scaling image to below this size, use nearest pixel for scaling
59 * (below about 4, the other scale types become slow generating their conversion tables)
60 */
61 #define PR_MIN_SCALE_SIZE 8
62
63
64 typedef enum {
65 TILE_RENDER_NONE = 0, /* do nothing */
66 TILE_RENDER_AREA, /* render an area of the tile */
67 TILE_RENDER_ALL /* render the whole tile */
68 } ImageTileRenderType;
69
70 typedef struct _ImageTile ImageTile;
71 typedef struct _QueueData QueueData;
72
73 struct _ImageTile
74 {
75 GdkPixmap *pixmap; /* off screen buffer */
76 GdkPixbuf *pixbuf; /* pixbuf area for zooming */
77 gint x; /* x offset into image */
78 gint y; /* y offset into image */
79 gint w; /* width that is visible (may be less if at edge of image) */
80 gint h; /* height '' */
81
82 gboolean blank;
83
84 /* render_todo: (explanation)
85 NONE do nothing
86 AREA render area of tile, usually only used when loading an image
87 note: will jump to an ALL if render_done is not ALL.
88 ALL render entire tile, if never done before w/ ALL, for expose events *only*
89 */
90
91 ImageTileRenderType render_todo; /* what to do (see above) */
92 ImageTileRenderType render_done; /* highest that has been done before on tile */
93
94 QueueData *qd;
95 QueueData *qd2;
96
97 guint size; /* est. memory used by pixmap and pixbuf */
98 };
99
100 struct _QueueData
101 {
102 ImageTile *it;
103 gint x;
104 gint y;
105 gint w;
106 gint h;
107 gboolean new_data;
108 };
109
110 typedef struct _SourceTile SourceTile;
111 struct _SourceTile
112 {
113 gint x;
114 gint y;
115 GdkPixbuf *pixbuf;
116 gboolean blank;
117 };
118
119 typedef struct _OverlayData OverlayData;
120 struct _OverlayData
121 {
122 gint id;
123
124 GdkPixbuf *pixbuf;
125
126 gint x;
127 gint y;
128 gint relative; /* x,y coordinates are relative, negative values start bottom right */
129
130 gint visible;
131 gint always; /* hide temporarily when scrolling */
132 };
133
134 enum {
135 SIGNAL_ZOOM = 0,
136 SIGNAL_CLICKED,
137 SIGNAL_SCROLL_NOTIFY,
138 SIGNAL_COUNT
139 };
140
141 enum {
142 PROP_0,
143 PROP_ZOOM_MIN,
144 PROP_ZOOM_MAX,
145 PROP_ZOOM_QUALITY,
146 PROP_ZOOM_2PASS,
147 PROP_ZOOM_EXPAND,
148 PROP_DITHER_QUALITY,
149 PROP_SCROLL_RESET,
150 PROP_DELAY_FLIP,
151 PROP_LOADING,
152 PROP_CACHE_SIZE_DISPLAY,
153 PROP_CACHE_SIZE_TILES,
154 PROP_WINDOW_FIT,
155 PROP_WINDOW_LIMIT,
156 PROP_WINDOW_LIMIT_VALUE
157 };
158
159
160
161 static guint signals[SIGNAL_COUNT] = { 0 };
162 static GtkEventBoxClass *parent_class = NULL;
163
164
165
166 static void pixbuf_renderer_class_init(PixbufRendererClass *class);
167 static void pixbuf_renderer_init(PixbufRenderer *pr);
168 static void pixbuf_renderer_finalize(GObject *object);
169 static void pixbuf_renderer_set_property(GObject *object, guint prop_id,
170 const GValue *value, GParamSpec *pspec);
171 static void pixbuf_renderer_get_property(GObject *object, guint prop_id,
172 GValue *value, GParamSpec *pspec);
173 static gint pixbuf_renderer_expose(GtkWidget *widget, GdkEventExpose *event);
174
175
176 static void pr_overlay_list_clear(PixbufRenderer *pr);
177 static void pr_scroller_timer_set(PixbufRenderer *pr, gint start);
178 static void pr_border_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h);
179
180
181 static void pr_source_tile_free_all(PixbufRenderer *pr);
182 static void pr_tile_free_all(PixbufRenderer *pr);
183 static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h);
184 static gint pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it);
185 static void pr_queue_clear(PixbufRenderer *pr);
186 static void pr_queue_merge(QueueData *parent, QueueData *qd);
187 static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
188 gint clamp, ImageTileRenderType render, gint new_data);
189
190 static void pr_redraw(PixbufRenderer *pr, gint new_data);
191
192 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
193 gint force, gint blank, gint new,
194 gint center_point, gint px, gint py);
195
196 static void pr_signals_connect(PixbufRenderer *pr);
197 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data);
198 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area);
199
200
201 /*
202 *-------------------------------------------------------------------
203 * Pixbuf Renderer object
204 *-------------------------------------------------------------------
205 */
206
207 GType pixbuf_renderer_get_type(void)
208 {
209 static GType pixbuf_renderer_type = 0;
210
211 if (!pixbuf_renderer_type)
212 {
213 static const GTypeInfo pixbuf_renderer_info =
214 {
215 sizeof(PixbufRendererClass),
216 NULL, /* base_init */
217 NULL, /* base_finalize */
218 (GClassInitFunc)pixbuf_renderer_class_init,
219 NULL, /* class_finalize */
220 NULL, /* class_data */
221 sizeof(PixbufRenderer),
222 0, /* n_preallocs */
223 (GInstanceInitFunc)pixbuf_renderer_init,
224 };
225
226 pixbuf_renderer_type = g_type_register_static(GTK_TYPE_EVENT_BOX, "PixbufRenderer",
227 &pixbuf_renderer_info, 0);
228 }
229
230 return pixbuf_renderer_type;
231 }
232
233 static void pixbuf_renderer_class_init(PixbufRendererClass *class)
234 {
235 GObjectClass *gobject_class = G_OBJECT_CLASS(class);
236 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);
237
238 parent_class = g_type_class_peek_parent(class);
239
240 gobject_class->set_property = pixbuf_renderer_set_property;
241 gobject_class->get_property = pixbuf_renderer_get_property;
242
243 gobject_class->finalize = pixbuf_renderer_finalize;
244
245 widget_class->expose_event = pixbuf_renderer_expose;
246
247 g_object_class_install_property(gobject_class,
248 PROP_ZOOM_MIN,
249 g_param_spec_double("zoom_min",
250 "Zoom minimum",
251 NULL,
252 -1000.0,
253 1000.0,
254 PR_ZOOM_MIN,
255 G_PARAM_READABLE | G_PARAM_WRITABLE));
256
257 g_object_class_install_property(gobject_class,
258 PROP_ZOOM_MAX,
259 g_param_spec_double("zoom_max",
260 "Zoom maximum",
261 NULL,
262 -1000.0,
263 1000.0,
264 PR_ZOOM_MIN,
265 G_PARAM_READABLE | G_PARAM_WRITABLE));
266
267 g_object_class_install_property(gobject_class,
268 PROP_ZOOM_QUALITY,
269 g_param_spec_uint("zoom_quality",
270 "Zoom quality",
271 NULL,
272 GDK_INTERP_NEAREST,
273 GDK_INTERP_HYPER,
274 GDK_INTERP_BILINEAR,
275 G_PARAM_READABLE | G_PARAM_WRITABLE));
276
277 g_object_class_install_property(gobject_class,
278 PROP_ZOOM_2PASS,
279 g_param_spec_boolean("zoom_2pass",
280 "2 pass zoom",
281 NULL,
282 TRUE,
283 G_PARAM_READABLE | G_PARAM_WRITABLE));
284
285 g_object_class_install_property(gobject_class,
286 PROP_ZOOM_EXPAND,
287 g_param_spec_boolean("zoom_expand",
288 "Expand image in autozoom.",
289 NULL,
290 FALSE,
291 G_PARAM_READABLE | G_PARAM_WRITABLE));
292
293 g_object_class_install_property(gobject_class,
294 PROP_DITHER_QUALITY,
295 g_param_spec_uint("dither_quality",
296 "Dither quality",
297 NULL,
298 GDK_RGB_DITHER_NONE,
299 GDK_RGB_DITHER_MAX,
300 GDK_RGB_DITHER_NORMAL,
301 G_PARAM_READABLE | G_PARAM_WRITABLE));
302
303 g_object_class_install_property(gobject_class,
304 PROP_SCROLL_RESET,
305 g_param_spec_uint("scroll_reset",
306 "New image scroll reset",
307 NULL,
308 PR_SCROLL_RESET_TOPLEFT,
309 PR_SCROLL_RESET_NOCHANGE,
310 PR_SCROLL_RESET_TOPLEFT,
311 G_PARAM_READABLE | G_PARAM_WRITABLE));
312
313 g_object_class_install_property(gobject_class,
314 PROP_DELAY_FLIP,
315 g_param_spec_boolean("delay_flip",
316 "Delay image update",
317 NULL,
318 FALSE,
319 G_PARAM_READABLE | G_PARAM_WRITABLE));
320
321 g_object_class_install_property(gobject_class,
322 PROP_LOADING,
323 g_param_spec_boolean("loading",
324 "Image actively loading",
325 NULL,
326 FALSE,
327 G_PARAM_READABLE | G_PARAM_WRITABLE));
328
329 g_object_class_install_property(gobject_class,
330 PROP_CACHE_SIZE_DISPLAY,
331 g_param_spec_uint("cache_display",
332 "Display cache size MB",
333 NULL,
334 0,
335 128,
336 PR_CACHE_SIZE_DEFAULT,
337 G_PARAM_READABLE | G_PARAM_WRITABLE));
338
339 g_object_class_install_property(gobject_class,
340 PROP_CACHE_SIZE_TILES,
341 g_param_spec_uint("cache_tiles",
342 "Tile cache count",
343 "Number of tiles to retain in memory at any one time.",
344 0,
345 256,
346 PR_CACHE_SIZE_DEFAULT,
347 G_PARAM_READABLE | G_PARAM_WRITABLE));
348
349 g_object_class_install_property(gobject_class,
350 PROP_WINDOW_FIT,
351 g_param_spec_boolean("window_fit",
352 "Fit window to image size",
353 NULL,
354 FALSE,
355 G_PARAM_READABLE | G_PARAM_WRITABLE));
356
357 g_object_class_install_property(gobject_class,
358 PROP_WINDOW_LIMIT,
359 g_param_spec_boolean("window_limit",
360 "Limit size of parent window",
361 NULL,
362 FALSE,
363 G_PARAM_READABLE | G_PARAM_WRITABLE));
364
365 g_object_class_install_property(gobject_class,
366 PROP_WINDOW_LIMIT_VALUE,
367 g_param_spec_uint("window_limit_value",
368 "Size limit of parent window",
369 NULL,
370 10,
371 150,
372 100,
373 G_PARAM_READABLE | G_PARAM_WRITABLE));
374
375 signals[SIGNAL_ZOOM] =
376 g_signal_new("zoom",
377 G_OBJECT_CLASS_TYPE(gobject_class),
378 G_SIGNAL_RUN_LAST,
379 G_STRUCT_OFFSET(PixbufRendererClass, zoom),
380 NULL, NULL,
381 g_cclosure_marshal_VOID__DOUBLE,
382 G_TYPE_NONE, 1,
383 G_TYPE_DOUBLE);
384
385 signals[SIGNAL_CLICKED] =
386 g_signal_new("clicked",
387 G_OBJECT_CLASS_TYPE(gobject_class),
388 G_SIGNAL_RUN_LAST,
389 G_STRUCT_OFFSET(PixbufRendererClass, clicked),
390 NULL, NULL,
391 g_cclosure_marshal_VOID__BOXED,
392 G_TYPE_NONE, 1,
393 GDK_TYPE_EVENT);
394
395 signals[SIGNAL_SCROLL_NOTIFY] =
396 g_signal_new("scroll-notify",
397 G_OBJECT_CLASS_TYPE(gobject_class),
398 G_SIGNAL_RUN_LAST,
399 G_STRUCT_OFFSET(PixbufRendererClass, scroll_notify),
400 NULL, NULL,
401 g_cclosure_marshal_VOID__VOID,
402 G_TYPE_NONE, 0);
403 }
404
405 static void pixbuf_renderer_init(PixbufRenderer *pr)
406 {
407 GtkWidget *box;
408
409 box = GTK_WIDGET(pr);
410
411 pr->zoom_min = PR_ZOOM_MIN;
412 pr->zoom_max = PR_ZOOM_MAX;
413 pr->zoom_quality = GDK_INTERP_BILINEAR;
414 pr->zoom_2pass = FALSE;
415
416 pr->zoom = 1.0;
417 pr->scale = 1.0;
418
419 pr->dither_quality = GDK_RGB_DITHER_NORMAL;
420
421 pr->scroll_reset = PR_SCROLL_RESET_TOPLEFT;
422
423 pr->draw_idle_id = -1;
424
425 pr->tile_width = PR_TILE_SIZE;
426 pr->tile_height = PR_TILE_SIZE;
427
428 pr->tiles = NULL;
429 pr->tile_cache_size = 0;
430
431 pr->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
432
433 pr->scroller_id = -1;
434 pr->scroller_overlay = -1;
435
436 pr->source_tiles_enabled = FALSE;
437 pr->source_tiles = NULL;
438
439 gtk_widget_set_double_buffered(box, FALSE);
440 g_signal_connect_after(G_OBJECT(box), "size_allocate",
441 G_CALLBACK(pr_size_cb), pr);
442
443 pr_signals_connect(pr);
444 }
445
446 static void pixbuf_renderer_finalize(GObject *object)
447 {
448 PixbufRenderer *pr;
449
450 pr = PIXBUF_RENDERER(object);
451
452 pr_queue_clear(pr);
453 pr_tile_free_all(pr);
454
455 if (pr->pixbuf) g_object_unref(pr->pixbuf);
456
457 pr_scroller_timer_set(pr, FALSE);
458 pr_overlay_list_clear(pr);
459
460 pr_source_tile_free_all(pr);
461 }
462
463 PixbufRenderer* pixbuf_renderer_new(void)
464 {
465 return g_object_new(TYPE_PIXBUF_RENDERER, NULL);
466 }
467
468 static void pixbuf_renderer_set_property(GObject *object, guint prop_id,
469 const GValue *value, GParamSpec *pspec)
470 {
471 PixbufRenderer *pr;
472
473 pr = PIXBUF_RENDERER(object);
474
475 switch (prop_id)
476 {
477 case PROP_ZOOM_MIN:
478 pr->zoom_min = g_value_get_double(value);
479 break;
480 case PROP_ZOOM_MAX:
481 pr->zoom_max = g_value_get_double(value);
482 break;
483 case PROP_ZOOM_QUALITY:
484 pr->zoom_quality = g_value_get_uint(value);
485 break;
486 case PROP_ZOOM_2PASS:
487 pr->zoom_2pass = g_value_get_boolean(value);
488 break;
489 case PROP_ZOOM_EXPAND:
490 pr->zoom_expand = g_value_get_boolean(value);
491 break;
492 case PROP_DITHER_QUALITY:
493 pr->dither_quality = g_value_get_uint(value);
494 break;
495 case PROP_SCROLL_RESET:
496 pr->scroll_reset = g_value_get_uint(value);
497 break;
498 case PROP_DELAY_FLIP:
499 pr->delay_flip = g_value_get_boolean(value);
500 break;
501 case PROP_LOADING:
502 pr->loading = g_value_get_boolean(value);
503 break;
504 case PROP_CACHE_SIZE_DISPLAY:
505 pr->tile_cache_max = g_value_get_uint(value);
506 break;
507 case PROP_CACHE_SIZE_TILES:
508 pr->source_tiles_cache_size = g_value_get_uint(value);
509 break;
510 case PROP_WINDOW_FIT:
511 pr->window_fit = g_value_get_boolean(value);
512 break;
513 case PROP_WINDOW_LIMIT:
514 pr->window_limit = g_value_get_boolean(value);
515 break;
516 case PROP_WINDOW_LIMIT_VALUE:
517 pr->window_limit_size = g_value_get_uint(value);
518 break;
519 default:
520 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
521 break;
522 }
523 }
524
525 static void pixbuf_renderer_get_property(GObject *object, guint prop_id,
526 GValue *value, GParamSpec *pspec)
527 {
528 PixbufRenderer *pr;
529
530 pr = PIXBUF_RENDERER(object);
531
532 switch (prop_id)
533 {
534 case PROP_ZOOM_MIN:
535 g_value_set_double(value, pr->zoom_min);
536 break;
537 case PROP_ZOOM_MAX:
538 g_value_set_double(value, pr->zoom_max);
539 break;
540 case PROP_ZOOM_QUALITY:
541 g_value_set_uint(value, pr->zoom_quality);
542 break;
543 case PROP_ZOOM_2PASS:
544 g_value_set_boolean(value, pr->zoom_2pass);
545 break;
546 case PROP_ZOOM_EXPAND:
547 g_value_set_boolean(value, pr->zoom_expand);
548 break;
549 case PROP_DITHER_QUALITY:
550 g_value_set_uint(value, pr->dither_quality);
551 break;
552 case PROP_SCROLL_RESET:
553 g_value_set_uint(value, pr->scroll_reset);
554 break;
555 case PROP_DELAY_FLIP:
556 g_value_set_boolean(value, pr->delay_flip);
557 break;
558 case PROP_LOADING:
559 g_value_set_boolean(value, pr->loading);
560 break;
561 case PROP_CACHE_SIZE_DISPLAY:
562 g_value_set_uint(value, pr->tile_cache_max);
563 break;
564 case PROP_CACHE_SIZE_TILES:
565 g_value_set_uint(value, pr->source_tiles_cache_size);
566 break;
567 case PROP_WINDOW_FIT:
568 g_value_set_boolean(value, pr->window_fit);
569 break;
570 case PROP_WINDOW_LIMIT:
571 g_value_set_boolean(value, pr->window_limit);
572 break;
573 case PROP_WINDOW_LIMIT_VALUE:
574 g_value_set_uint(value, pr->window_limit_size);
575 break;
576 default:
577 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
578 break;
579 }
580 }
581
582 static gint pixbuf_renderer_expose(GtkWidget *widget, GdkEventExpose *event)
583 {
584 if (GTK_WIDGET_DRAWABLE(widget))
585 {
586 if (!GTK_WIDGET_NO_WINDOW(widget))
587 {
588 pixbuf_renderer_paint(PIXBUF_RENDERER(widget), &event->area);
589 }
590 }
591
592 return FALSE;
593 }
594
595 /*
596 *-------------------------------------------------------------------
597 * misc utilities
598 *-------------------------------------------------------------------
599 */
600
601 static void widget_set_cursor(GtkWidget *widget, gint icon)
602 {
603 GdkCursor *cursor;
604
605 if (!widget->window) return;
606
607 if (icon == -1)
608 {
609 cursor = NULL;
610 }
611 else
612 {
613 cursor = gdk_cursor_new (icon);
614 }
615
616 gdk_window_set_cursor(widget->window, cursor);
617
618 if (cursor) gdk_cursor_unref(cursor);
619 }
620
621 static gint pixmap_calc_size(GdkPixmap *pixmap)
622 {
623 gint w, h, d;
624
625 d = gdk_drawable_get_depth(pixmap);
626 gdk_drawable_get_size(pixmap, &w, &h);
627 return w * h * (d / 8);
628 }
629
630 static gint util_clip_region(gint x, gint y, gint w, gint h,
631 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
632 gint *rx, gint *ry, gint *rw, gint *rh)
633 {
634 if (clip_x + clip_w <= x ||
635 clip_x >= x + w ||
636 clip_y + clip_h <= y ||
637 clip_y >= y + h)
638 {
639 return FALSE;
640 }
641
642 *rx = MAX(x, clip_x);
643 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
644
645 *ry = MAX(y, clip_y);
646 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
647
648 return TRUE;
649 }
650
651 static gint pr_parent_window_sizable(PixbufRenderer *pr)
652 {
653 if (!pr->parent_window) return FALSE;
654 if (!pr->window_fit) return FALSE;
655 if (!GTK_WIDGET(pr)->window) return FALSE;
656 #if 0
657 if (window_maximized(pr->top_window)) return FALSE;
658 #endif
659
660 return TRUE;
661 }
662
663 static gint pr_parent_window_resize(PixbufRenderer *pr, gint w, gint h)
664 {
665 GtkWidget *widget;
666 GtkWidget *parent;
667 gint ww, wh;
668
669 if (!pr_parent_window_sizable(pr)) return FALSE;
670
671 if (pr->window_limit)
672 {
673 gint sw = gdk_screen_width() * pr->window_limit_size / 100;
674 gint sh = gdk_screen_height() * pr->window_limit_size / 100;
675
676 if (w > sw) w = sw;
677 if (h > sh) h = sh;
678 }
679
680 widget = GTK_WIDGET(pr);
681 parent = GTK_WIDGET(pr->parent_window);
682
683 w += (parent->allocation.width - widget->allocation.width);
684 h += (parent->allocation.height - widget->allocation.height);
685
686 gdk_drawable_get_size(parent->window, &ww, &wh);
687 if (w == ww && h == wh) return FALSE;
688
689 gdk_window_resize(parent->window, w, h);
690
691 return TRUE;
692 }
693
694 void pixbuf_renderer_set_parent(PixbufRenderer *pr, GtkWindow *window)
695 {
696 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
697 g_return_if_fail(window == NULL || GTK_IS_WINDOW(window));
698
699 pr->parent_window = GTK_WIDGET(window);
700 }
701
702 GtkWindow *pixbuf_renderer_get_parent(PixbufRenderer *pr)
703 {
704 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
705
706 return GTK_WINDOW(pr->parent_window);
707 }
708
709
710 /*
711 *-------------------------------------------------------------------
712 * overlays
713 *-------------------------------------------------------------------
714 */
715
716 static void pr_overlay_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
717 {
718 GtkWidget *box;
719 GList *work;
720
721 box = GTK_WIDGET(pr);
722
723 work = pr->overlay_list;
724 while (work)
725 {
726 OverlayData *od;
727 gint px, py, pw, ph;
728 gint rx, ry, rw, rh;
729
730 od = work->data;
731 work = work->next;
732
733 if (!od->visible) continue;
734
735 pw = gdk_pixbuf_get_width(od->pixbuf);
736 ph = gdk_pixbuf_get_height(od->pixbuf);
737 px = od->x;
738 py = od->y;
739
740 if (od->relative)
741 {
742 if (px < 0) px = pr->window_width - pw + px;
743 if (py < 0) py = pr->window_height - ph + py;
744 }
745
746 if (util_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
747 {
748 gdk_draw_pixbuf(box->window,
749 box->style->fg_gc[GTK_WIDGET_STATE(box)],
750 od->pixbuf,
751 rx - px, ry - py,
752 rx, ry, rw, rh,
753 pr->dither_quality, rx, ry);
754 }
755 }
756 }
757
758 static void pr_overlay_queue_draw(PixbufRenderer *pr, OverlayData *od, gint hidden)
759 {
760 gint x, y, w, h;
761 gint old_vis;
762
763 w = gdk_pixbuf_get_width(od->pixbuf);
764 h = gdk_pixbuf_get_height(od->pixbuf);
765 x = od->x;
766 y = od->y;
767
768 if (od->relative)
769 {
770 if (x < 0) x = pr->window_width - w + x;
771 if (y < 0) y = pr->window_height - h + y;
772 }
773
774 pr_queue(pr, pr->x_scroll - pr->x_offset + x,
775 pr->y_scroll - pr->y_offset + y,
776 w, h,
777 FALSE, TILE_RENDER_ALL, FALSE);
778
779 old_vis = od->visible;
780 if (hidden) od->visible = FALSE;
781 pr_border_draw(pr, x, y, w, h);
782 od->visible = old_vis;
783 }
784
785 static void pr_overlay_queue_all(PixbufRenderer *pr)
786 {
787 GList *work;
788
789 work = pr->overlay_list;
790 while (work)
791 {
792 OverlayData *od = work->data;
793 work = work->next;
794
795 pr_overlay_queue_draw(pr, od, FALSE);
796 }
797 }
798
799 static OverlayData *pr_overlay_find(PixbufRenderer *pr, gint id)
800 {
801 GList *work;
802
803 work = pr->overlay_list;
804 while (work)
805 {
806 OverlayData *od = work->data;
807 work = work->next;
808
809 if (od->id == id) return od;
810 }
811
812 return NULL;
813 }
814
815 gint pixbuf_renderer_overlay_add(PixbufRenderer *pr, GdkPixbuf *pixbuf, gint x, gint y,
816 gint relative, gint always)
817 {
818 OverlayData *od;
819 gint id;
820
821 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
822 g_return_val_if_fail(pixbuf != NULL, -1);
823
824 id = 1;
825 while (pr_overlay_find(pr, id)) id++;
826
827 od = g_new0(OverlayData, 1);
828 od->id = id;
829 od->pixbuf = pixbuf;
830 g_object_ref(G_OBJECT(od->pixbuf));
831 od->x = x;
832 od->y = y;
833 od->relative = relative;
834 od->visible = TRUE;
835 od->always = always;
836
837 pr->overlay_list = g_list_append(pr->overlay_list, od);
838
839 pr_overlay_queue_draw(pr, od, FALSE);
840
841 return od->id;
842 }
843
844 static void pr_overlay_free(PixbufRenderer *pr, OverlayData *od)
845 {
846 pr->overlay_list = g_list_remove(pr->overlay_list, od);
847
848 if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
849 g_free(od);
850 }
851
852 static void pr_overlay_list_clear(PixbufRenderer *pr)
853 {
854 while (pr->overlay_list)
855 {
856 OverlayData *od;
857
858 od = pr->overlay_list->data;
859 pr_overlay_free(pr, od);
860 }
861 }
862
863 void pixbuf_renderer_overlay_set(PixbufRenderer *pr, gint id, GdkPixbuf *pixbuf, gint x, gint y)
864 {
865 OverlayData *od;
866
867 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
868
869 od = pr_overlay_find(pr, id);
870 if (!od) return;
871
872 if (pixbuf)
873 {
874 pr_overlay_queue_draw(pr, od, TRUE);
875
876 g_object_ref(G_OBJECT(pixbuf));
877 g_object_unref(G_OBJECT(od->pixbuf));
878 od->pixbuf = pixbuf;
879
880 od->x = x;
881 od->y = y;
882
883 pr_overlay_queue_draw(pr, od, FALSE);
884 }
885 else
886 {
887 pr_overlay_queue_draw(pr, od, TRUE);
888 pr_overlay_free(pr, od);
889 }
890 }
891
892 gint pixbuf_renderer_overlay_get(PixbufRenderer *pr, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
893 {
894 OverlayData *od;
895
896 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
897
898 od = pr_overlay_find(pr, id);
899 if (!od) return FALSE;
900
901 if (pixbuf) *pixbuf = od->pixbuf;
902 if (x) *x = od->x;
903 if (y) *y = od->y;
904
905 return TRUE;
906 }
907
908 void pixbuf_renderer_overlay_remove(PixbufRenderer *pr, gint id)
909 {
910 pixbuf_renderer_overlay_set(pr, id, NULL, 0, 0);
911 }
912
913 /*
914 *-------------------------------------------------------------------
915 * scroller overlay
916 *-------------------------------------------------------------------
917 */
918
919
920 static gboolean pr_scroller_update_cb(gpointer data)
921 {
922 PixbufRenderer *pr = data;
923 gint x, y;
924 gint xinc, yinc;
925
926 /* this was a simple scroll by difference between scroller and mouse position,
927 * but all this math results in a smoother result and accounts for a dead zone.
928 */
929
930 if (abs(pr->scroller_xpos - pr->scroller_x) < PR_SCROLLER_DEAD_ZONE)
931 {
932 x = 0;
933 }
934 else
935 {
936 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
937 x = (pr->scroller_xpos - pr->scroller_x) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
938 x += (x > 0) ? -shift : shift;
939 }
940
941 if (abs(pr->scroller_ypos - pr->scroller_y) < PR_SCROLLER_DEAD_ZONE)
942 {
943 y = 0;
944 }
945 else
946 {
947 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
948 y = (pr->scroller_ypos - pr->scroller_y) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
949 y += (y > 0) ? -shift : shift;
950 }
951
952 if (abs(x) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
953 {
954 xinc = x;
955 }
956 else
957 {
958 xinc = pr->scroller_xinc;
959
960 if (x >= 0)
961 {
962 if (xinc < 0) xinc = 0;
963 if (x < xinc) xinc = x;
964 if (x > xinc) xinc = MIN(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
965 }
966 else
967 {
968 if (xinc > 0) xinc = 0;
969 if (x > xinc) xinc = x;
970 if (x < xinc) xinc = MAX(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
971 }
972 }
973
974 if (abs(y) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
975 {
976 yinc = y;
977 }
978 else
979 {
980 yinc = pr->scroller_yinc;
981
982 if (y >= 0)
983 {
984 if (yinc < 0) yinc = 0;
985 if (y < yinc) yinc = y;
986 if (y > yinc) yinc = MIN(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
987 }
988 else
989 {
990 if (yinc > 0) yinc = 0;
991 if (y > yinc) yinc = y;
992 if (y < yinc) yinc = MAX(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
993 }
994 }
995
996 pr->scroller_xinc = xinc;
997 pr->scroller_yinc = yinc;
998
999 xinc = xinc / PR_SCROLLER_UPDATES_PER_SEC;
1000 yinc = yinc / PR_SCROLLER_UPDATES_PER_SEC;
1001
1002 pixbuf_renderer_scroll(pr, xinc, yinc);
1003
1004 return TRUE;
1005 }
1006
1007 static void pr_scroller_timer_set(PixbufRenderer *pr, gint start)
1008 {
1009 if (pr->scroller_id != -1)
1010 {
1011 g_source_remove(pr->scroller_id);
1012 pr->scroller_id = -1;
1013 }
1014
1015 if (start)
1016 {
1017 pr->scroller_id = g_timeout_add(1000 / PR_SCROLLER_UPDATES_PER_SEC,
1018 pr_scroller_update_cb, pr);
1019 }
1020 }
1021
1022 static void pr_scroller_start(PixbufRenderer *pr, gint x, gint y)
1023 {
1024 if (pr->scroller_overlay == -1)
1025 {
1026 GdkPixbuf *pixbuf;
1027 gint w, h;
1028
1029 #ifdef GQVIEW_BUILD
1030 pixbuf = pixbuf_inline(PIXBUF_INLINE_SCROLLER);
1031 #else
1032 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 32, 32);
1033 gdk_pixbuf_fill(pixbuf, 0x000000ff);
1034 #endif
1035 w = gdk_pixbuf_get_width(pixbuf);
1036 h = gdk_pixbuf_get_height(pixbuf);
1037
1038 pr->scroller_overlay = pixbuf_renderer_overlay_add(pr, pixbuf, x - w / 2, y - h / 2, FALSE, TRUE);
1039 g_object_unref(pixbuf);
1040 }
1041
1042 pr->scroller_x = x;
1043 pr->scroller_y = y;
1044 pr->scroller_xpos = x;
1045 pr->scroller_ypos = y;
1046
1047 pr_scroller_timer_set(pr, TRUE);
1048 }
1049
1050 static void pr_scroller_stop(PixbufRenderer *pr)
1051 {
1052 if (pr->scroller_id == -1) return;
1053
1054 pixbuf_renderer_overlay_remove(pr, pr->scroller_overlay);
1055 pr->scroller_overlay = -1;
1056
1057 pr_scroller_timer_set(pr, FALSE);
1058 }
1059
1060 /*
1061 *-------------------------------------------------------------------
1062 * borders
1063 *-------------------------------------------------------------------
1064 */
1065
1066 static void pr_border_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
1067 {
1068 GtkWidget *box;
1069 gint rx, ry, rw, rh;
1070
1071 box = GTK_WIDGET(pr);
1072
1073 if (!box->window) return;
1074
1075 if (!pr->pixbuf && !pr->source_tiles_enabled)
1076 {
1077 if (util_clip_region(x, y, w, h,
1078 0, 0,
1079 pr->window_width, pr->window_height,
1080 &rx, &ry, &rw, &rh))
1081 {
1082 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1083 pr_overlay_draw(pr, rx, ry, rw, rh);
1084 }
1085 return;
1086 }
1087
1088 if (pr->vis_width < pr->window_width)
1089 {
1090 if (pr->x_offset > 0 &&
1091 util_clip_region(x, y, w, h,
1092 0, 0,
1093 pr->x_offset, pr->window_height,
1094 &rx, &ry, &rw, &rh))
1095 {
1096 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1097 pr_overlay_draw(pr, rx, ry, rw, rh);
1098 }
1099 if (pr->window_width - pr->vis_width - pr->x_offset > 0 &&
1100 util_clip_region(x, y, w, h,
1101 pr->x_offset + pr->vis_width, 0,
1102 pr->window_width - pr->vis_width - pr->x_offset, pr->window_height,
1103 &rx, &ry, &rw, &rh))
1104 {
1105 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1106 pr_overlay_draw(pr, rx, ry, rw, rh);
1107 }
1108 }
1109 if (pr->vis_height < pr->window_height)
1110 {
1111 if (pr->y_offset > 0 &&
1112 util_clip_region(x, y, w, h,
1113 pr->x_offset, 0,
1114 pr->vis_width, pr->y_offset,
1115 &rx, &ry, &rw, &rh))
1116 {
1117 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1118 pr_overlay_draw(pr, rx, ry, rw, rh);
1119 }
1120 if (pr->window_height - pr->vis_height - pr->y_offset > 0 &&
1121 util_clip_region(x, y, w, h,
1122 pr->x_offset, pr->y_offset + pr->vis_height,
1123 pr->vis_width, pr->window_height - pr->vis_height - pr->y_offset,
1124 &rx, &ry, &rw, &rh))
1125 {
1126 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1127 pr_overlay_draw(pr, rx, ry, rw, rh);
1128 }
1129 }
1130 }
1131
1132 static void pr_border_clear(PixbufRenderer *pr)
1133 {
1134 pr_border_draw(pr, 0, 0, pr->window_width, pr->window_height);
1135 }
1136
1137 void pixbuf_renderer_set_color(PixbufRenderer *pr, GdkColor *color)
1138 {
1139 GtkStyle *style;
1140 GtkWidget *widget;
1141
1142 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1143
1144 widget = GTK_WIDGET(pr);
1145
1146 style = gtk_style_copy(gtk_widget_get_style(widget));
1147 g_object_ref(G_OBJECT(style));
1148
1149 if (color)
1150 {
1151 GdkColor *slot;
1152
1153 slot = &style->bg[GTK_STATE_NORMAL];
1154
1155 slot->red = color->red;
1156 slot->green = color->green;
1157 slot->blue = color->blue;
1158 }
1159
1160 gtk_widget_set_style(widget, style);
1161 g_object_unref(G_OBJECT(style));
1162
1163 if (GTK_WIDGET_VISIBLE(widget)) pr_border_clear(pr);
1164 }
1165
1166 void pixbuf_renderer_set_black(PixbufRenderer *pr, gint black)
1167 {
1168 GdkColor color = { 0, 0, 0, 0};
1169
1170 pixbuf_renderer_set_color(pr, &color);
1171 }
1172
1173
1174 /*
1175 *-------------------------------------------------------------------
1176 * source tiles
1177 *-------------------------------------------------------------------
1178 */
1179
1180 static void pr_source_tile_free(SourceTile *st)
1181 {
1182 if (!st) return;
1183
1184 if (st->pixbuf) g_object_unref(st->pixbuf);
1185 g_free(st);
1186 }
1187
1188 static void pr_source_tile_free_all(PixbufRenderer *pr)
1189 {
1190 GList *work;
1191
1192 work = pr->source_tiles;
1193 while (work)
1194 {
1195 SourceTile *st;
1196
1197 st = work->data;
1198 work = work->next;
1199
1200 pr_source_tile_free(st);
1201 }
1202
1203 g_list_free(pr->source_tiles);
1204 pr->source_tiles = NULL;
1205 }
1206
1207 static void pr_source_tile_unset(PixbufRenderer *pr)
1208 {
1209 pr_source_tile_free_all(pr);
1210 pr->source_tiles_enabled = FALSE;
1211 }
1212
1213 static gint pr_source_tile_visible(PixbufRenderer *pr, SourceTile *st)
1214 {
1215 gint x1, y1, x2, y2;
1216
1217 if (!st) return FALSE;
1218
1219 x1 = (pr->x_scroll / PR_TILE_SIZE) * PR_TILE_SIZE;
1220 y1 = (pr->y_scroll / PR_TILE_SIZE) * PR_TILE_SIZE;
1221 x2 = ((pr->x_scroll + pr->vis_width) / PR_TILE_SIZE) * PR_TILE_SIZE + PR_TILE_SIZE;
1222 y2 = ((pr->y_scroll + pr->vis_height) / PR_TILE_SIZE) * PR_TILE_SIZE + PR_TILE_SIZE;
1223
1224 return !((double)st->x * pr->scale > (double)x2 ||
1225 (double)(st->x + pr->source_tile_width) * pr->scale < (double)x1 ||
1226 (double)st->y * pr->scale > (double)x2 ||
1227 (double)(st->y + pr->source_tile_height) * pr->scale < (double)y1);
1228 }
1229
1230 static SourceTile *pr_source_tile_new(PixbufRenderer *pr, gint x, gint y)
1231 {
1232 SourceTile *st = NULL;
1233 gint count;
1234
1235 g_return_val_if_fail(pr->source_tile_width >= 1 && pr->source_tile_height >= 1, NULL);
1236
1237 if (pr->source_tiles_cache_size < 4) pr->source_tiles_cache_size = 4;
1238
1239 count = g_list_length(pr->source_tiles);
1240 if (count >= pr->source_tiles_cache_size)
1241 {
1242 GList *work;
1243
1244 work = g_list_last(pr->source_tiles);
1245 while (work && count >= pr->source_tiles_cache_size)
1246 {
1247 SourceTile *needle;
1248
1249 needle = work->data;
1250 work = work->prev;
1251
1252 if (!pr_source_tile_visible(pr, needle))
1253 {
1254 pr->source_tiles = g_list_remove(pr->source_tiles, needle);
1255
1256 if (pr->func_tile_dispose)
1257 {
1258 pr->func_tile_dispose(pr, needle->x, needle->y,
1259 pr->source_tile_width, pr->source_tile_height,
1260 needle->pixbuf, pr->func_tile_data);
1261 }
1262
1263 if (!st)
1264 {
1265 st = needle;
1266 }
1267 else
1268 {
1269 pr_source_tile_free(needle);
1270 }
1271
1272 count--;
1273 }
1274 }
1275 }
1276
1277 if (!st)
1278 {
1279 st = g_new0(SourceTile, 1);
1280 st->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
1281 pr->source_tile_width, pr->source_tile_height);
1282 }
1283
1284 st->x = (x / pr->source_tile_width) * pr->source_tile_width;
1285 st->y = (y / pr->source_tile_height) * pr->source_tile_height;
1286 st->blank = TRUE;
1287
1288 pr->source_tiles = g_list_prepend(pr->source_tiles, st);
1289
1290 return st;
1291 }
1292
1293 static SourceTile *pr_source_tile_request(PixbufRenderer *pr, gint x, gint y)
1294 {
1295 SourceTile *st;
1296
1297 st = pr_source_tile_new(pr, x, y);
1298 if (!st) return NULL;
1299
1300 if (pr->func_tile_request &&
1301 pr->func_tile_request(pr, st->x, st->y,
1302 pr->source_tile_width, pr->source_tile_height, st->pixbuf, pr->func_tile_data))
1303 {
1304 st->blank = FALSE;
1305 }
1306
1307 pr_tile_invalidate_region(pr, st->x * pr->scale, st->y * pr->scale,
1308 pr->source_tile_width * pr->scale, pr->source_tile_height * pr->scale);
1309
1310 return st;
1311 }
1312
1313 static SourceTile *pr_source_tile_find(PixbufRenderer *pr, gint x, gint y)
1314 {
1315 GList *work;
1316
1317 work = pr->source_tiles;
1318 while (work)
1319 {
1320 SourceTile *st = work->data;
1321
1322 if (x >= st->x && x < st->x + pr->source_tile_width &&
1323 y >= st->y && y < st->y + pr->source_tile_height)
1324 {
1325 if (work != pr->source_tiles)
1326 {
1327 pr->source_tiles = g_list_remove_link(pr->source_tiles, work);
1328 pr->source_tiles = g_list_concat(work, pr->source_tiles);
1329 }
1330 return st;
1331 }
1332
1333 work = work->next;
1334 }
1335
1336 return NULL;
1337 }
1338
1339 static GList *pr_source_tile_compute_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h, gint request)
1340 {
1341 gint x1, y1;
1342 GList *list = NULL;
1343 gint sx, sy;
1344
1345 if (x < 0) x = 0;
1346 if (y < 0) y = 0;
1347 if (w > pr->image_width) w = pr->image_width;
1348 if (h > pr->image_height) h = pr->image_height;
1349
1350 sx = (x / pr->source_tile_width) * pr->source_tile_width;
1351 sy = (y / pr->source_tile_height) * pr->source_tile_height;
1352
1353 for (x1 = sx; x1 < x + w; x1+= pr->source_tile_width)
1354 {
1355 for (y1 = sy; y1 < y + h; y1 += pr->source_tile_height)
1356 {
1357 SourceTile *st;
1358
1359 st = pr_source_tile_find(pr, x1, y1);
1360 if (!st && request) st = pr_source_tile_request(pr, x1, y1);
1361
1362 if (st) list = g_list_prepend(list, st);
1363 }
1364 }
1365
1366 return g_list_reverse(list);
1367 }
1368
1369 static void pr_source_tile_changed(PixbufRenderer *pr, gint x, gint y, gint width, gint height)
1370 {
1371 GList *work;
1372
1373 work = pr->source_tiles;
1374 while (work)
1375 {
1376 SourceTile *st;
1377 gint rx, ry, rw, rh;
1378
1379 st = work->data;
1380 work = work->next;
1381
1382 if (util_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1383 x, y, width, height,
1384 &rx, &ry, &rw, &rh))
1385 {
1386 GdkPixbuf *pixbuf;
1387
1388 pixbuf = gdk_pixbuf_new_subpixbuf(st->pixbuf, rx - st->x, ry - st->y, rw, rh);
1389 if (pr->func_tile_request &&
1390 pr->func_tile_request(pr, rx, ry, rw, rh, pixbuf, pr->func_tile_data))
1391 {
1392 pr_tile_invalidate_region(pr, rx * pr->scale, ry * pr->scale,
1393 rw * pr->scale, rh * pr->scale);
1394 }
1395 g_object_unref(pixbuf);
1396 }
1397 }
1398 }
1399
1400 static gint pr_source_tile_render(PixbufRenderer *pr, ImageTile *it,
1401 gint x, gint y, gint w, gint h,
1402 gint new_data, gint fast)
1403 {
1404 GtkWidget *box;
1405 GList *list;
1406 GList *work;
1407 gint draw = FALSE;
1408
1409 box = GTK_WIDGET(pr);
1410
1411 if (pr->zoom == 1.0 || pr->scale == 1.0)
1412 {
1413 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1414 work = list;
1415 while (work)
1416 {
1417 SourceTile *st;
1418 gint rx, ry, rw, rh;
1419
1420 st = work->data;
1421 work = work->next;
1422
1423 if (util_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1424 it->x + x, it->y + y, w, h,
1425 &rx, &ry, &rw, &rh))
1426 {
1427 if (st->blank)
1428 {
1429 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1430 rx - st->x, ry - st->y, rw, rh);
1431 }
1432 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1433 {
1434 gdk_draw_pixbuf(it->pixmap,
1435 box->style->fg_gc[GTK_WIDGET_STATE(box)],
1436 st->pixbuf,
1437 rx - st->x, ry - st->y,
1438 rx - it->x, ry - it->y,
1439 rw, rh,
1440 pr->dither_quality, rx, ry);
1441 }
1442 }
1443 }
1444 }
1445 else
1446 {
1447 double scale_x, scale_y;
1448 gint sx, sy, sw, sh;
1449
1450 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1451 scale_x = (double)pr->width / pr->image_width;
1452 scale_y = (double)pr->height / pr->image_height;
1453
1454 sx = (double)(it->x + x) / scale_x;
1455 sy = (double)(it->y + y) / scale_y;
1456 sw = (double)w / scale_x;
1457 sh = (double)h / scale_y;
1458
1459 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1460
1461 #if 0
1462 /* draws red over draw region, to check for leaks (regions not filled) */
1463 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1464 #endif
1465
1466 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1467 work = list;
1468 while (work)
1469 {
1470 SourceTile *st;
1471 gint rx, ry, rw, rh;
1472 gint stx, sty, stw, sth;
1473
1474 st = work->data;
1475 work = work->next;
1476
1477 stx = floor((double)st->x * scale_x);
1478 sty = floor((double)st->y * scale_y);
1479 stw = ceil ((double)(st->x + pr->source_tile_width) * scale_x) - stx;
1480 sth = ceil ((double)(st->y + pr->source_tile_height) * scale_y) - sty;
1481
1482 if (util_clip_region(stx, sty, stw, sth,
1483 it->x + x, it->y + y, w, h,
1484 &rx, &ry, &rw, &rh))
1485 {
1486 if (st->blank)
1487 {
1488 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1489 rx - st->x, ry - st->y, rw, rh);
1490 }
1491 else
1492 {
1493 double offset_x;
1494 double offset_y;
1495
1496 /* may need to use unfloored stx,sty values here */
1497 offset_x = (double)(stx - it->x);
1498 offset_y = (double)(sty - it->y);
1499
1500 gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1501 (double) 0.0 + offset_x,
1502 (double) 0.0 + offset_y,
1503 scale_x, scale_y,
1504 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1505 draw = TRUE;
1506 }
1507 }
1508 }
1509 }
1510
1511 g_list_free(list);
1512
1513 return draw;
1514 }
1515
1516 void pixbuf_renderer_set_tiles(PixbufRenderer *pr, gint width, gint height,
1517 gint tile_width, gint tile_height, gint cache_size,
1518 PixbufRendererTileRequestFunc func_request,
1519 PixbufRendererTileDisposeFunc func_dispose,
1520 gpointer user_data,
1521 gdouble zoom)
1522 {
1523 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1524 g_return_if_fail(tile_width >= 32 && tile_width >= 32);
1525 g_return_if_fail(width >= 32 && height > 32);
1526 g_return_if_fail(func_request != NULL);
1527
1528 if (pr->pixbuf) g_object_unref(pr->pixbuf);
1529 pr->pixbuf = NULL;
1530
1531 pr_source_tile_unset(pr);
1532
1533 if (cache_size < 4) cache_size = 4;
1534
1535 pr->source_tiles_enabled = TRUE;
1536 pr->source_tiles_cache_size = cache_size;
1537 pr->source_tile_width = tile_width;
1538 pr->source_tile_height = tile_height;
1539
1540 pr->image_width = width;
1541 pr->image_height = height;
1542
1543 pr->func_tile_request = func_request;
1544 pr->func_tile_dispose = func_dispose;
1545 pr->func_tile_data = user_data;
1546
1547 pr_zoom_sync(pr, zoom, TRUE, FALSE, TRUE, FALSE, 0, 0);
1548 pr_redraw(pr, TRUE);
1549 }
1550
1551 gint pixbuf_renderer_get_tiles(PixbufRenderer *pr)
1552 {
1553 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
1554
1555 return pr->source_tiles_enabled;
1556 }
1557
1558 static void pr_zoom_adjust_real(PixbufRenderer *pr, gdouble increment,
1559 gint center_point, gint x, gint y)
1560 {
1561 gdouble zoom = pr->zoom;
1562
1563 if (increment == 0.0) return;
1564
1565 if (zoom == 0.0)
1566 {
1567 if (pr->scale < 1.0)
1568 {
1569 zoom = 0.0 - 1.0 / pr->scale;
1570 }
1571 else
1572 {
1573 zoom = pr->scale;
1574 }
1575 }
1576
1577 if (increment < 0.0)
1578 {
1579 if (zoom >= 1.0 && zoom + increment < 1.0)
1580 {
1581 zoom = zoom + increment - 2.0;
1582 }
1583 else
1584 {
1585 zoom = zoom + increment;
1586 }
1587 }
1588 else
1589 {
1590 if (zoom <= -1.0 && zoom + increment > -1.0)
1591 {
1592 zoom = zoom + increment + 2.0;
1593 }
1594 else
1595 {
1596 zoom = zoom + increment;
1597 }
1598 }
1599
1600 pr_zoom_sync(pr, zoom, FALSE, FALSE, FALSE, center_point, x, y);
1601 }
1602
1603 /*
1604 *-------------------------------------------------------------------
1605 * display tiles
1606 *-------------------------------------------------------------------
1607 */
1608
1609 static ImageTile *pr_tile_new(gint x, gint y, gint width, gint height)
1610 {
1611 ImageTile *it;
1612
1613 it = g_new0(ImageTile, 1);
1614
1615 it->x = x;
1616 it->y = y;
1617 it->w = width;
1618 it->h = height;
1619
1620 it->render_done = TILE_RENDER_NONE;
1621
1622 return it;
1623 }
1624
1625 static void pr_tile_free(ImageTile *it)
1626 {
1627 if (!it) return;
1628
1629 if (it->pixbuf) gdk_pixbuf_unref(it->pixbuf);
1630 if (it->pixmap) g_object_unref(it->pixmap);
1631
1632 g_free(it);
1633 }
1634
1635 static void pr_tile_free_all(PixbufRenderer *pr)
1636 {
1637 GList *work;
1638
1639 work = pr->tiles;
1640 while (work)
1641 {
1642 ImageTile *it;
1643
1644 it = work->data;
1645 work = work->next;
1646
1647 pr_tile_free(it);
1648 }
1649
1650 g_list_free(pr->tiles);
1651 pr->tiles = NULL;
1652 pr->tile_cache_size = 0;
1653 }
1654
1655 static ImageTile *pr_tile_add(PixbufRenderer *pr, gint x, gint y)
1656 {
1657 ImageTile *it;
1658
1659 it = pr_tile_new(x, y, pr->tile_width, pr->tile_height);
1660
1661 if (it->x + it->w > pr->width) it->w = pr->width - it->x;
1662 if (it->y + it->h > pr->height) it->h = pr->height - it->y;
1663
1664 pr->tiles = g_list_prepend(pr->tiles, it);
1665 pr->tile_cache_size += it->size;
1666
1667 return it;
1668 }
1669
1670 static void pr_tile_remove(PixbufRenderer *pr, ImageTile *it)
1671 {
1672 if (it->qd)
1673 {
1674 QueueData *qd = it->qd;
1675
1676 it->qd = NULL;
1677 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
1678 g_free(qd);
1679 }
1680
1681 if (it->qd2)
1682 {
1683 QueueData *qd = it->qd2;
1684
1685 it->qd2 = NULL;
1686 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
1687 g_free(qd);
1688 }
1689
1690 pr->tiles = g_list_remove(pr->tiles, it);
1691 pr->tile_cache_size -= it->size;
1692
1693 pr_tile_free(it);
1694 }
1695
1696 static void pr_tile_free_space(PixbufRenderer *pr, guint space, ImageTile *it)
1697 {
1698 GList *work;
1699 gint tile_max;
1700
1701 work = g_list_last(pr->tiles);
1702
1703 if (pr->source_tiles_enabled && pr->scale < 1.0)
1704 {
1705 gint tiles;
1706
1707 tiles = (pr->vis_width / PR_TILE_SIZE + 1) * (pr->vis_width / PR_TILE_SIZE + 1);
1708 tile_max = MAX(tiles * PR_TILE_SIZE * PR_TILE_SIZE * 3,
1709 (gint)((double)pr->tile_cache_max * 1048576.0 * pr->scale));
1710 }
1711 else
1712 {
1713 tile_max = pr->tile_cache_max * 1048576;
1714 }
1715
1716 while (work && pr->tile_cache_size + space > tile_max)
1717 {
1718 ImageTile *needle;
1719
1720 needle = work->data;
1721 work = work->prev;
1722 if (needle != it &&
1723 ((!needle->qd && !needle->qd2) || !pr_tile_is_visible(pr, needle))) pr_tile_remove(pr, needle);
1724 }
1725 }
1726
1727 static void pr_tile_invalidate_all(PixbufRenderer *pr)
1728 {
1729 GList *work;
1730
1731 work = pr->tiles;
1732 while (work)
1733 {
1734 ImageTile *it;
1735
1736 it = work->data;
1737 work = work->next;
1738
1739 it->render_done = TILE_RENDER_NONE;
1740 it->render_todo = TILE_RENDER_NONE;
1741 it->blank = FALSE;
1742
1743 it->w = MIN(pr->tile_width, pr->width - it->x);
1744 it->h = MIN(pr->tile_height, pr->height - it->y);
1745 }
1746 }
1747
1748 static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
1749 {
1750 gint x1, x2;
1751 gint y1, y2;
1752 GList *work;
1753
1754 x1 = (gint)floor(x / pr->tile_width) * pr->tile_width;
1755 x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width;
1756
1757 y1 = (gint)floor(y / pr->tile_height) * pr->tile_height;
1758 y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height;
1759
1760 work = pr->tiles;
1761 while (work)
1762 {
1763 ImageTile *it;
1764
1765 it = work->data;
1766 work = work->next;
1767
1768 if (it->x < x2 && it->x + it->w > x1 &&
1769 it->y < y2 && it->y + it->h > y1)
1770 {
1771 it->render_done = TILE_RENDER_NONE;
1772 it->render_todo = TILE_RENDER_NONE;
1773 }
1774 }
1775 }
1776
1777 static ImageTile *pr_tile_get(PixbufRenderer *pr, gint x, gint y)
1778 {
1779 GList *work;
1780
1781 work = pr->tiles;
1782 while (work)
1783 {
1784 ImageTile *it;
1785
1786 it = work->data;
1787 if (it->x == x && it->y == y)
1788 {
1789 pr->tiles = g_list_delete_link(pr->tiles, work);
1790 pr->tiles = g_list_prepend(pr->tiles, it);
1791 return it;
1792 }
1793
1794 work = work->next;
1795 }
1796
1797 return pr_tile_add(pr, x, y);
1798 }
1799
1800 static void pr_tile_prepare(PixbufRenderer *pr, ImageTile *it)
1801 {
1802 if (!it->pixmap)
1803 {
1804 GdkPixmap *pixmap;
1805 guint size;
1806
1807 pixmap = gdk_pixmap_new(((GtkWidget *)pr)->window, pr->tile_width, pr->tile_height, -1);
1808
1809 size = pixmap_calc_size(pixmap);
1810 pr_tile_free_space(pr, size, it);
1811
1812 it->pixmap = pixmap;
1813 it->size += size;
1814 pr->tile_cache_size += size;
1815 }
1816
1817 if ((pr->zoom != 1.0 || pr->source_tiles_enabled || (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf)) ) &&
1818 !it->pixbuf)
1819 {
1820 GdkPixbuf *pixbuf;
1821 guint size;
1822
1823 if (pr->pixbuf)
1824 {
1825 pixbuf = gdk_pixbuf_new(gdk_pixbuf_get_colorspace(pr->pixbuf),
1826 gdk_pixbuf_get_has_alpha(pr->pixbuf),
1827 gdk_pixbuf_get_bits_per_sample(pr->pixbuf),
1828 pr->tile_width, pr->tile_height);
1829 }
1830 else
1831 {
1832 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, pr->tile_width, pr->tile_height);
1833 }
1834
1835 size = gdk_pixbuf_get_rowstride(pixbuf) * pr->tile_height;
1836 pr_tile_free_space(pr, size, it);
1837
1838 it->pixbuf = pixbuf;
1839 it->size += size;
1840 pr->tile_cache_size += size;
1841 }
1842 }
1843
1844 /*
1845 *-------------------------------------------------------------------
1846 * drawing
1847 *-------------------------------------------------------------------
1848 */
1849
1850 static void pr_tile_render(PixbufRenderer *pr, ImageTile *it,
1851 gint x, gint y, gint w, gint h,
1852 gint new_data, gint fast)
1853 {
1854 GtkWidget *box;
1855 gint has_alpha;
1856 gint draw = FALSE;
1857
1858 if (it->render_todo == TILE_RENDER_NONE && it->pixmap && !new_data) return;
1859
1860 if (it->render_done != TILE_RENDER_ALL)
1861 {
1862 x = 0;
1863 y = 0;
1864 w = it->w;
1865 h = it->h;
1866 if (!fast) it->render_done = TILE_RENDER_ALL;
1867 }
1868 else if (it->render_todo != TILE_RENDER_AREA)
1869 {
1870 if (!fast) it->render_todo = TILE_RENDER_NONE;
1871 return;
1872 }
1873
1874 if (!fast) it->render_todo = TILE_RENDER_NONE;
1875
1876 if (new_data) it->blank = FALSE;
1877
1878 pr_tile_prepare(pr, it);
1879 has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1880
1881 box = GTK_WIDGET(pr);
1882
1883 /* FIXME checker colors for alpha should be configurable,
1884 * also should be drawn for blank = TRUE
1885 */
1886
1887 if (it->blank)
1888 {
1889 /* no data, do fast rect fill */
1890 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1891 0, 0, it->w, it->h);
1892 }
1893 else if (pr->source_tiles_enabled)
1894 {
1895 draw = pr_source_tile_render(pr, it, x, y, w, h, new_data, fast);
1896 }
1897 else if (pr->zoom == 1.0 || pr->scale == 1.0)
1898 {
1899 if (has_alpha)
1900 {
1901 gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, x, y, w, h,
1902 (double) 0.0 - it->x,
1903 (double) 0.0 - it->y,
1904 1.0, 1.0, GDK_INTERP_NEAREST,
1905 255, it->x + x, it->y + y,
1906 PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
1907 draw = TRUE;
1908 }
1909 else
1910 {
1911 /* faster, simple */
1912 gdk_draw_pixbuf(it->pixmap,
1913 box->style->fg_gc[GTK_WIDGET_STATE(box)],
1914 pr->pixbuf,
1915 it->x + x, it->y + y,
1916 x, y,
1917 w, h,
1918 pr->dither_quality, it->x + x, it->y + y);
1919 }
1920 }
1921 else
1922 {
1923 double scale_x, scale_y;
1924
1925 if (pr->image_width == 0 || pr->image_height == 0) return;
1926 scale_x = (double)pr->width / pr->image_width;
1927 scale_y = (double)pr->height / pr->image_height;
1928
1929 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1930 * small sizes for anything but GDK_INTERP_NEAREST
1931 */
1932 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1933
1934 if (!has_alpha)
1935 {
1936 gdk_pixbuf_scale(pr->pixbuf, it->pixbuf, x, y, w, h,
1937 (double) 0.0 - it->x,
1938 (double) 0.0 - it->y,
1939 scale_x, scale_y,
1940 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1941 }
1942 else
1943 {
1944 gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, x, y, w, h,
1945 (double) 0.0 - it->x,
1946 (double) 0.0 - it->y,
1947 scale_x, scale_y,
1948 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1949 255, it->x + x, it->y + y,
1950 PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
1951 }
1952 draw = TRUE;
1953 }
1954
1955 if (draw && it->pixbuf && !it->blank)
1956 {
1957 gdk_draw_pixbuf(it->pixmap,
1958 box->style->fg_gc[GTK_WIDGET_STATE(box)],
1959 it->pixbuf,
1960 x, y,
1961 x, y,
1962 w, h,
1963 pr->dither_quality, it->x + x, it->y + y);
1964 }
1965 }
1966
1967
1968 static void pr_tile_expose(PixbufRenderer *pr, ImageTile *it,
1969 gint x, gint y, gint w, gint h,
1970 gint new_data, gint fast)
1971 {
1972 GtkWidget *box;
1973
1974 pr_tile_render(pr, it, x, y, w, h, new_data, fast);
1975
1976 box = GTK_WIDGET(pr);
1977
1978 gdk_draw_drawable(box->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
1979 it->pixmap, x, y,
1980 pr->x_offset + (it->x - pr->x_scroll) + x, pr->y_offset + (it->y - pr->y_scroll) + y, w, h);
1981
1982 if (pr->overlay_list)
1983 {
1984 pr_overlay_draw(pr, pr->x_offset + (it->x - pr->x_scroll) + x,
1985 pr->y_offset + (it->y - pr->y_scroll) + y,
1986 w, h);
1987 }
1988 }
1989
1990
1991 static gint pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it)
1992 {
1993 return (it->x + it->w >= pr->x_scroll && it->x <= pr->x_scroll + pr->window_width - pr->x_offset * 2 &&
1994 it->y + it->h >= pr->y_scroll && it->y <= pr->y_scroll + pr->window_height - pr->y_offset * 2);
1995 }
1996
1997 /*
1998 *-------------------------------------------------------------------
1999 * draw queue
2000 *-------------------------------------------------------------------
2001 */
2002
2003 static gint pr_queue_draw_idle_cb(gpointer data)
2004 {
2005 PixbufRenderer *pr = data;
2006 QueueData *qd;
2007 gint fast;
2008
2009 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
2010 (!pr->draw_queue && !pr->draw_queue_2pass) ||
2011 pr->draw_idle_id == -1)
2012 {
2013 #if 0
2014 if (!pr->completed) image_complete_util(pr, FALSE);
2015 #endif
2016
2017 pr->draw_idle_id = -1;
2018 return FALSE;
2019 }
2020
2021 if (pr->draw_queue)
2022 {
2023 qd = pr->draw_queue->data;
2024 fast = (pr->zoom_2pass && pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0);
2025 }
2026 else
2027 {
2028 if (pr->loading)
2029 {
2030 /* still loading, wait till done (also drops the higher priority) */
2031
2032 pr->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
2033 pr_queue_draw_idle_cb, pr, NULL);
2034 pr->draw_idle_high = FALSE;
2035 return FALSE;
2036 }
2037
2038 qd = pr->draw_queue_2pass->data;
2039 fast = FALSE;
2040 }
2041
2042 if (GTK_WIDGET_REALIZED(pr))
2043 {
2044 if (pr_tile_is_visible(pr, qd->it))
2045 {
2046 pr_tile_expose(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2047 }
2048 else if (qd->new_data)
2049 {
2050 /* if new pixel data, and we already have a pixmap, update the tile */
2051 qd->it->blank = FALSE;
2052 if (qd->it->pixmap && qd->it->render_done == TILE_RENDER_ALL)
2053 {
2054 pr_tile_render(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2055 }
2056 }
2057 }
2058
2059 if (pr->draw_queue)
2060 {
2061 qd->it->qd = NULL;
2062 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
2063 if (fast)
2064 {
2065 if (qd->it->qd2)
2066 {
2067 pr_queue_merge(qd->it->qd2, qd);
2068 g_free(qd);
2069 }
2070 else
2071 {
2072 qd->it->qd2 = qd;
2073 pr->draw_queue_2pass = g_list_append(pr->draw_queue_2pass, qd);
2074 }
2075 }
2076 else
2077 {
2078 g_free(qd);
2079 }
2080 }
2081 else
2082 {
2083 qd->it->qd2 = NULL;
2084 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
2085 g_free(qd);
2086 }
2087
2088 if (!pr->draw_queue && !pr->draw_queue_2pass)
2089 {
2090 #if 0
2091 if (!pr->completed) image_complete_util(pr, FALSE);
2092 #endif
2093
2094 pr->draw_idle_id = -1;
2095 return FALSE;
2096 }
2097
2098 return TRUE;
2099 }
2100
2101 static void pr_queue_list_free(GList *list)
2102 {
2103 GList *work;
2104
2105 work = list;
2106 while (work)
2107 {
2108 QueueData *qd;
2109
2110 qd = work->data;
2111 work = work->next;
2112
2113 qd->it->qd = NULL;
2114 qd->it->qd2 = NULL;
2115 g_free(qd);
2116 }
2117
2118 g_list_free(list);
2119 }
2120
2121 static void pr_queue_clear(PixbufRenderer *pr)
2122 {
2123 pr_queue_list_free(pr->draw_queue);
2124 pr->draw_queue = NULL;
2125
2126 pr_queue_list_free(pr->draw_queue_2pass);
2127 pr->draw_queue_2pass = NULL;
2128
2129 if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
2130 pr->draw_idle_id = -1;
2131 }
2132
2133 static void pr_queue_merge(QueueData *parent, QueueData *qd)
2134 {
2135 if (parent->x + parent->w < qd->x + qd->w)
2136 {
2137 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
2138 }
2139 if (parent->x > qd->x)
2140 {
2141 parent->w += parent->x - qd->x;
2142 parent->x = qd->x;
2143 }
2144
2145 if (parent->y + parent->h < qd->y + qd->h)
2146 {
2147 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
2148 }
2149 if (parent->y > qd->y)
2150 {
2151 parent->h += parent->y - qd->y;
2152 parent->y = qd->y;
2153 }
2154
2155 parent->new_data |= qd->new_data;
2156 }
2157
2158 static gint pr_clamp_to_visible(PixbufRenderer *pr, gint *x, gint *y, gint *w, gint *h)
2159 {
2160 gint nx, ny;
2161 gint nw, nh;
2162 gint vx, vy;
2163 gint vw, vh;
2164
2165 vw = pr->vis_width;
2166 vh = pr->vis_height;
2167
2168 vx = pr->x_scroll;
2169 vy = pr->y_scroll;
2170
2171 if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
2172
2173 /* now clamp it */
2174 nx = CLAMP(*x, vx, vx + vw);
2175 nw = CLAMP(*w - (nx - *x), 1, vw);
2176
2177 ny = CLAMP(*y, vy, vy + vh);
2178 nh = CLAMP(*h - (ny - *y), 1, vh);
2179
2180 *x = nx;
2181 *y = ny;
2182 *w = nw;
2183 *h = nh;
2184
2185 return TRUE;
2186 }
2187
2188 static gint pr_queue_to_tiles(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
2189 gint clamp, ImageTileRenderType render, gint new_data)
2190 {
2191 gint i, j;
2192 gint x1, x2;
2193 gint y1, y2;
2194
2195 if (clamp && !pr_clamp_to_visible(pr, &x, &y, &w, &h)) return FALSE;
2196
2197 x1 = (gint)floor(x / pr->tile_width) * pr->tile_width;
2198 x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width;
2199
2200 y1 = (gint)floor(y / pr->tile_height) * pr->tile_height;
2201 y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height;
2202
2203 for (j = y1; j <= y2; j += pr->tile_height)
2204 {
2205 for (i = x1; i <= x2; i += pr->tile_width)
2206 {
2207 ImageTile *it;
2208
2209 it = pr_tile_get(pr, i, j);
2210 if (it)
2211 {
2212 QueueData *qd;
2213
2214 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
2215 (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
2216 {
2217 it->render_todo = render;
2218 }
2219
2220 qd = g_new(QueueData, 1);
2221 qd->it = it;
2222 qd->new_data = new_data;
2223
2224 if (i < x)
2225 {
2226 qd->x = x - i;
2227 }
2228 else
2229 {
2230 qd->x = 0;
2231 }
2232 qd->w = x + w - i - qd->x;
2233 if (qd->x + qd->w > pr->tile_width) qd->w = pr->tile_width - qd->x;
2234
2235 if (j < y)
2236 {
2237 qd->y = y - j;
2238 }
2239 else
2240 {
2241 qd->y = 0;
2242 }
2243 qd->h = y + h - j - qd->y;
2244 if (qd->y + qd->h > pr->tile_height) qd->h = pr->tile_height - qd->y;
2245
2246 if (qd->w < 1 || qd->h < 1)
2247 {
2248 g_free(qd);
2249 }
2250 else if (it->qd)
2251 {
2252 pr_queue_merge(it->qd, qd);
2253 g_free(qd);
2254 }
2255 else
2256 {
2257 it->qd = qd;
2258 pr->draw_queue = g_list_append(pr->draw_queue, qd);
2259 }
2260 }
2261 }
2262 }
2263
2264 return TRUE;
2265 }
2266
2267 static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
2268 gint clamp, ImageTileRenderType render, gint new_data)
2269 {
2270 gint nx, ny;
2271
2272 nx = CLAMP(x, 0, pr->width - 1);
2273 ny = CLAMP(y, 0, pr->height - 1);
2274 w -= (nx - x);
2275 h -= (ny - y);
2276 w = CLAMP(w, 0, pr->width - nx);
2277 h = CLAMP(h, 0, pr->height - ny);
2278 if (w < 1 || h < 1) return;
2279
2280 if (pr_queue_to_tiles(pr, nx, ny, w, h, clamp, render, new_data) &&
2281 ((!pr->draw_queue && !pr->draw_queue_2pass) || pr->draw_idle_id == -1 || !pr->draw_idle_high))
2282 {
2283 if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
2284 pr->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW,
2285 pr_queue_draw_idle_cb, pr, NULL);
2286 pr->draw_idle_high = TRUE;
2287 }
2288 }
2289
2290 static void pr_redraw(PixbufRenderer *pr, gint new_data)
2291 {
2292 pr_queue_clear(pr);
2293 pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, new_data);
2294 }
2295
2296 /*
2297 *-------------------------------------------------------------------
2298 * signal emission
2299 *-------------------------------------------------------------------
2300 */
2301
2302 static void pr_update_signal(PixbufRenderer *pr)
2303 {
2304 #if 0
2305 printf("FIXME: send updated signal\n");
2306 #endif
2307 }
2308
2309 static void pr_zoom_signal(PixbufRenderer *pr)
2310 {
2311 g_signal_emit(pr, signals[SIGNAL_ZOOM], 0, pr->zoom);
2312 }
2313
2314 static void pr_scroll_notify_signal(PixbufRenderer *pr)
2315 {
2316 g_signal_emit(pr, signals[SIGNAL_SCROLL_NOTIFY], 0);
2317 }
2318
2319 static void pr_button_signal(PixbufRenderer *pr, GdkEventButton *bevent)
2320 {
2321 g_signal_emit(pr, signals[SIGNAL_CLICKED], 0, bevent);
2322 }
2323
2324 /*
2325 *-------------------------------------------------------------------
2326 * sync and clamp
2327 *-------------------------------------------------------------------
2328 */
2329
2330 static gint pr_scroll_clamp(PixbufRenderer *pr)
2331 {
2332 gint old_xs;
2333 gint old_ys;
2334
2335 if (pr->zoom == 0.0)
2336 {
2337 pr->x_scroll = 0;
2338 pr->y_scroll = 0;
2339
2340 pr_scroll_notify_signal(pr);
2341 return FALSE;
2342 }
2343
2344 old_xs = pr->x_scroll;
2345 old_ys = pr->y_scroll;
2346
2347 if (pr->x_offset > 0)
2348 {
2349 pr->x_scroll = 0;
2350 }
2351 else
2352 {
2353 pr->x_scroll = CLAMP(pr->x_scroll, 0, pr->width - pr->vis_width);
2354 }
2355
2356 if (pr->y_offset > 0)
2357 {
2358 pr->y_scroll = 0;
2359 }
2360 else
2361 {
2362 pr->y_scroll = CLAMP(pr->y_scroll, 0, pr->height - pr->vis_height);
2363 }
2364
2365 pr_scroll_notify_signal(pr);
2366
2367 return (old_xs != pr->x_scroll || old_ys != pr->y_scroll);
2368 }
2369
2370 static gint pr_size_clamp(PixbufRenderer *pr)
2371 {
2372 gint old_vw, old_vh;
2373
2374 old_vw = pr->vis_width;
2375 old_vh = pr->vis_height;
2376
2377 if (pr->width < pr->window_width)
2378 {
2379 pr->vis_width = pr->width;
2380 pr->x_offset = (pr->window_width - pr->width) / 2;
2381 }
2382 else
2383 {
2384 pr->vis_width = pr->window_width;
2385 pr->x_offset = 0;
2386 }
2387
2388 if (pr->height < pr->window_height)
2389 {
2390 pr->vis_height = pr->height;
2391 pr->y_offset = (pr->window_height - pr->height) / 2;
2392 }
2393 else
2394 {
2395 pr->vis_height = pr->window_height;
2396 pr->y_offset = 0;
2397 }
2398
2399 return (old_vw != pr->vis_width || old_vh != pr->vis_height);
2400 }
2401
2402 static gint pr_zoom_clamp(PixbufRenderer *pr, gdouble zoom, gint force, gint new, gint invalidate)
2403 {
2404 gint w, h;
2405 gdouble scale;
2406 gint invalid;
2407
2408 zoom = CLAMP(zoom, pr->zoom_min, pr->zoom_max);
2409
2410 if (pr->zoom == zoom && !force) return FALSE;
2411
2412 w = pr->image_width;
2413 h = pr->image_height;
2414
2415 if (zoom == 0.0 && !pr->pixbuf)
2416 {
2417 scale = 1.0;
2418 }
2419 else if (zoom == 0.0)
2420 {
2421 gint max_w;
2422 gint max_h;
2423 gint sizeable;
2424
2425 sizeable = (new && pr_parent_window_sizable(pr));
2426
2427 if (sizeable)
2428 {
2429 max_w = gdk_screen_width();
2430 max_h = gdk_screen_height();
2431
2432 if (pr->window_limit)
2433 {
2434 max_w = max_w * pr->window_limit_size / 100;
2435 max_h = max_h * pr->window_limit_size / 100;
2436 }
2437 }
2438 else
2439 {
2440 max_w = pr->window_width;
2441 max_h = pr->window_height;
2442 }
2443
2444 if ((pr->zoom_expand && !sizeable) || w > max_w || h > max_h)
2445 {
2446 if ((gdouble)max_w / w > (gdouble)max_h / h)
2447 {
2448 scale = (gdouble)max_h / h;
2449 h = max_h;
2450 w = w * scale + 0.5;
2451 if (w > max_w) w = max_w;
2452 }
2453 else
2454 {
2455 scale = (gdouble)max_w / w;
2456 w = max_w;
2457 h = h * scale + 0.5;
2458 if (h > max_h) h = max_h;
2459 }
2460 if (w < 1) w = 1;
2461 if (h < 1) h = 1;
2462 }
2463 else
2464 {
2465 scale = 1.0;
2466 }
2467 }
2468 else if (zoom > 0.0) /* zoom orig, in */
2469 {
2470 scale = zoom;
2471 w = w * scale;
2472 h = h * scale;
2473 }
2474 else /* zoom out */
2475 {
2476 scale = 1.0 / (0.0 - zoom);
2477 w = w * scale;
2478 h = h * scale;
2479 }
2480
2481 invalid = (pr->width != w || pr->height != h);
2482
2483 pr->zoom = zoom;
2484 pr->width = w;
2485 pr->height = h;
2486 pr->scale = scale;
2487
2488 if (invalidate || invalid)
2489 {
2490 pr_tile_invalidate_all(pr);
2491 pr_redraw(pr, TRUE);
2492 }
2493
2494 return TRUE;
2495 }
2496
2497 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
2498 gint force, gint blank, gint new,
2499 gint center_point, gint px, gint py)
2500 {
2501 gdouble old_scale;
2502 gint old_cx, old_cy;
2503 gint clamped;
2504 gint sized;
2505
2506 old_scale = pr->scale;
2507 if (center_point)
2508 {
2509 px = CLAMP(px, 0, pr->width);
2510 py = CLAMP(py, 0, pr->height);
2511 old_cx = pr->x_scroll + (px - pr->x_offset);
2512 old_cy = pr->y_scroll + (py - pr->y_offset);
2513 }
2514 else
2515 {
2516 px = py = 0;
2517 old_cx = pr->x_scroll + pr->vis_width / 2;
2518 old_cy = pr->y_scroll + pr->vis_height / 2;
2519 }
2520
2521 if (!pr_zoom_clamp(pr, zoom, force, new, force)) return;
2522
2523 clamped = pr_size_clamp(pr);
2524 sized = pr_parent_window_resize(pr, pr->width, pr->height);
2525
2526 if (force)
2527 {
2528 switch (pr->scroll_reset)
2529 {
2530 case PR_SCROLL_RESET_NOCHANGE:
2531 /* maintain old scroll position, do nothing */
2532 break;
2533 case PR_SCROLL_RESET_CENTER:
2534 /* center new image */
2535 pr->x_scroll = ((double)pr->image_width / 2.0 * pr->scale) - pr->vis_width / 2;
2536 pr->y_scroll = ((double)pr->image_height / 2.0 * pr->scale) - pr->vis_height / 2;
2537 break;
2538 case PR_SCROLL_RESET_TOPLEFT:
2539 default:
2540 /* reset to upper left */
2541 pr->x_scroll = 0;
2542 pr->y_scroll = 0;
2543 break;
2544 }
2545 }
2546 else
2547 {
2548 /* user zoom does not force, so keep visible center point */
2549 if (center_point)
2550 {
2551 pr->x_scroll = old_cx / old_scale * pr->scale - (px - pr->x_offset);
2552 pr->y_scroll = old_cy / old_scale * pr->scale - (py - pr->y_offset);
2553 }
2554 else
2555 {
2556 pr->x_scroll = old_cx / old_scale * pr->scale - (pr->vis_width / 2);
2557 pr->y_scroll = old_cy / old_scale * pr->scale - (pr->vis_height / 2);
2558 }
2559 }
2560
2561 pr_scroll_clamp(pr);
2562
2563 #if 0
2564 pr_tile_sync(pr, blank);
2565 #endif
2566
2567 /* If the window was not sized, redraw the image - we know there will be no size/expose signal.
2568 * But even if a size is claimed, there is no guarantee that the window manager will allow it,
2569 * so redraw the window anyway :/
2570 */
2571 if (sized || clamped) pr_border_clear(pr);
2572 pr_redraw(pr, FALSE);
2573
2574 pr_zoom_signal(pr);
2575 pr_update_signal(pr);
2576 }
2577
2578 static void pr_size_sync(PixbufRenderer *pr, gint new_width, gint new_height)
2579 {
2580 if (pr->window_width == new_width && pr->window_height == new_height) return;
2581
2582 pr->window_width = new_width;
2583 pr->window_height = new_height;
2584
2585 if (pr->zoom == 0.0) pr_zoom_clamp(pr, 0.0, TRUE, FALSE, FALSE);
2586
2587 pr_size_clamp(pr);
2588 pr_scroll_clamp(pr);
2589
2590 /* ensure scroller remains visible */
2591 if (pr->scroller_overlay != -1)
2592 {
2593 gint update = FALSE;
2594
2595 if (pr->scroller_x > new_width)
2596 {
2597 pr->scroller_x = new_width;
2598 pr->scroller_xpos = new_width;
2599 update = TRUE;
2600 }
2601 if (pr->scroller_y > new_height)
2602 {
2603 pr->scroller_y = new_height;
2604 pr->scroller_ypos = new_height;
2605 update = TRUE;
2606 }
2607
2608 if (update)
2609 {
2610 GdkPixbuf *pixbuf;
2611
2612 if (pixbuf_renderer_overlay_get(pr, pr->scroller_overlay, &pixbuf, NULL, NULL))
2613 {
2614 gint w, h;
2615
2616 w = gdk_pixbuf_get_width(pixbuf);
2617 h = gdk_pixbuf_get_height(pixbuf);
2618 pixbuf_renderer_overlay_set(pr, pr->scroller_overlay, pixbuf,
2619 pr->scroller_x - w / 2, pr->scroller_y - h / 2);
2620 }
2621 }
2622 }
2623
2624 pr_border_clear(pr);
2625
2626 #if 0
2627 pr_tile_sync(pr, pr->width, pr->height, FALSE);
2628 #endif
2629
2630 pr_update_signal(pr);
2631 }
2632
2633 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
2634 {
2635 PixbufRenderer *pr = data;
2636
2637 pr_size_sync(pr, allocation->width, allocation->height);
2638 }
2639
2640 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area)
2641 {
2642 gint x, y;
2643
2644 pr_border_draw(pr, area->x, area->y, area->width, area->height);
2645
2646 x = MAX(0, (gint)area->x - pr->x_offset + pr->x_scroll);
2647 y = MAX(0, (gint)area->y - pr->y_offset + pr->y_scroll);
2648
2649 pr_queue(pr, x, y,
2650 MIN((gint)area->width, pr->width - x),
2651 MIN((gint)area->height, pr->height - y),
2652 FALSE, TILE_RENDER_ALL, FALSE);
2653 }
2654
2655 /*
2656 *-------------------------------------------------------------------
2657 * scrolling
2658 *-------------------------------------------------------------------
2659 */
2660
2661 void pixbuf_renderer_scroll(PixbufRenderer *pr, gint x, gint y)
2662 {
2663 gint old_x, old_y;
2664 gint x_off, y_off;
2665 gint w, h;
2666
2667 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2668
2669 if (!pr->pixbuf && !pr->source_tiles_enabled) return;
2670
2671 old_x = pr->x_scroll;
2672 old_y = pr->y_scroll;
2673
2674 pr->x_scroll += x;
2675 pr->y_scroll += y;
2676
2677 pr_scroll_clamp(pr);
2678 if (pr->x_scroll == old_x && pr->y_scroll == old_y) return;
2679
2680 if (pr->overlay_list)
2681 {
2682 gint new_x, new_y;
2683
2684 new_x = pr->x_scroll;
2685 new_y = pr->y_scroll;
2686 pr->x_scroll = old_x;
2687 pr->y_scroll = old_y;
2688
2689 pr_overlay_queue_all(pr);
2690
2691 pr->x_scroll = new_x;
2692 pr->y_scroll = new_y;
2693 }
2694
2695 x_off = pr->x_scroll - old_x;
2696 y_off = pr->y_scroll - old_y;
2697
2698 w = pr->vis_width - abs(x_off);
2699 h = pr->vis_height - abs(y_off);
2700
2701 if (w < 1 || h < 1)
2702 {
2703 /* scrolled completely to new material */
2704 pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE);
2705 return;
2706 }
2707 else
2708 {
2709 gint x1, y1;
2710 gint x2, y2;
2711 GtkWidget *box;
2712 GdkGC *gc;
2713
2714 if (x_off < 0)
2715 {
2716 x1 = abs(x_off);
2717 x2 = 0;
2718 }
2719 else
2720 {
2721 x1 = 0;
2722 x2 = abs(x_off);
2723 }
2724
2725 if (y_off < 0)
2726 {
2727 y1 = abs(y_off);
2728 y2 = 0;
2729 }
2730 else
2731 {
2732 y1 = 0;
2733 y2 = abs(y_off);
2734 }
2735
2736 box = GTK_WIDGET(pr);
2737
2738 gc = gdk_gc_new(box->window);
2739 gdk_gc_set_exposures(gc, TRUE);
2740 gdk_draw_drawable(box->window, gc,
2741 box->window,
2742 x2 + pr->x_offset, y2 + pr->y_offset,
2743 x1 + pr->x_offset, y1 + pr->y_offset, w, h);
2744 g_object_unref(gc);
2745
2746 if (pr->overlay_list)
2747 {
2748 pr_overlay_queue_all(pr);
2749 }
2750
2751 w = pr->vis_width - w;
2752 h = pr->vis_height - h;
2753
2754 if (w > 0)
2755 {
2756 pr_queue(pr,
2757 x_off > 0 ? pr->x_scroll + (pr->vis_width - w) : pr->x_scroll, pr->y_scroll,
2758 w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE);
2759 }
2760 if (h > 0)
2761 {
2762 /* FIXME, to optimize this, remove overlap */
2763 pr_queue(pr,
2764 pr->x_scroll, y_off > 0 ? pr->y_scroll + (pr->vis_height - h) : pr->y_scroll,
2765 pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE);
2766 }
2767 }
2768 }
2769
2770 void pixbuf_renderer_scroll_to_point(PixbufRenderer *pr, gint x, gint y,
2771 gdouble x_align, gdouble y_align)
2772 {
2773 gint px, py;
2774 gint ax, ay;
2775
2776 x_align = CLAMP(x_align, 0.0, 1.0);
2777 y_align = CLAMP(y_align, 0.0, 1.0);
2778
2779 ax = (gdouble)pr->vis_width * x_align;
2780 ay = (gdouble)pr->vis_height * y_align;
2781
2782 px = (gdouble)x * pr->scale - (pr->x_scroll + ax);
2783 py = (gdouble)y * pr->scale - (pr->y_scroll + ay);
2784
2785 pixbuf_renderer_scroll(pr, px, py);
2786 }
2787
2788 /*
2789 *-------------------------------------------------------------------
2790 * mouse
2791 *-------------------------------------------------------------------
2792 */
2793
2794 static gint pr_mouse_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
2795 {
2796 PixbufRenderer *pr;
2797 gint accel;
2798
2799 pr = PIXBUF_RENDERER(widget);
2800
2801 if (pr->scroller_id != -1)
2802 {
2803 pr->scroller_xpos = bevent->x;
2804 pr->scroller_ypos = bevent->y;
2805 }
2806
2807 if (!pr->in_drag || !gdk_pointer_is_grabbed()) return FALSE;
2808
2809 if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
2810 {
2811 pr->drag_moved++;
2812 }
2813 else
2814 {
2815 widget_set_cursor(widget, GDK_FLEUR);
2816 }
2817
2818 if (bevent->state & GDK_SHIFT_MASK)
2819 {
2820 accel = PR_PAN_SHIFT_MULTIPLIER;
2821 }
2822 else
2823 {
2824 accel = 1;
2825 }
2826
2827 /* do the scroll */
2828 pixbuf_renderer_scroll(pr, (pr->drag_last_x - bevent->x) * accel,
2829 (pr->drag_last_y - bevent->y) * accel);
2830
2831 pr->drag_last_x = bevent->x;
2832 pr->drag_last_y = bevent->y;
2833
2834 return FALSE;
2835 }
2836
2837 static gint pr_mouse_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
2838 {
2839 PixbufRenderer *pr;
2840 GtkWidget *parent;
2841
2842 pr = PIXBUF_RENDERER(widget);
2843
2844 if (pr->scroller_id != -1) return TRUE;
2845
2846 switch (bevent->button)
2847 {
2848 case 1:
2849 pr->in_drag = TRUE;
2850 pr->drag_last_x = bevent->x;
2851 pr->drag_last_y = bevent->y;
2852 pr->drag_moved = 0;
2853 gdk_pointer_grab(widget->window, FALSE,
2854 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
2855 NULL, NULL, bevent->time);
2856 gtk_grab_add(widget);
2857 break;
2858 case 2:
2859 pr->drag_moved = 0;
2860 break;
2861 case 3:
2862 pr_button_signal(pr, bevent);
2863 break;
2864 default:
2865 break;
2866 }
2867
2868 parent = gtk_widget_get_parent(widget);
2869 if (widget && GTK_WIDGET_CAN_FOCUS(parent))
2870 {
2871 gtk_widget_grab_focus(parent);
2872 }
2873
2874 return FALSE;
2875 }
2876
2877 static gint pr_mouse_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
2878 {
2879 PixbufRenderer *pr;
2880
2881 pr = PIXBUF_RENDERER(widget);
2882
2883 if (pr->scroller_id != -1)
2884 {
2885 pr_scroller_stop(pr);
2886 return TRUE;
2887 }
2888
2889 if (gdk_pointer_is_grabbed() && GTK_WIDGET_HAS_GRAB(pr))
2890 {
2891 gtk_grab_remove(widget);
2892 gdk_pointer_ungrab(bevent->time);
2893 widget_set_cursor(widget, -1);
2894 }
2895
2896 if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
2897 {
2898 if (bevent->button == 1 && (bevent->state & GDK_SHIFT_MASK))
2899 {
2900 pr_scroller_start(pr, bevent->x, bevent->y);
2901 }
2902 else if (bevent->button == 1 || bevent->button == 2)
2903 {
2904 pr_button_signal(pr, bevent);
2905 }
2906 }
2907
2908 pr->in_drag = FALSE;
2909
2910 return FALSE;
2911 }
2912
2913 static gint pr_mouse_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
2914 {
2915 PixbufRenderer *pr;
2916
2917 pr = PIXBUF_RENDERER(widget);
2918
2919 if (pr->scroller_id != -1)
2920 {
2921 pr->scroller_xpos = pr->scroller_x;
2922 pr->scroller_ypos = pr->scroller_y;
2923 pr->scroller_xinc = 0;
2924 pr->scroller_yinc = 0;
2925 }
2926
2927 return FALSE;
2928 }
2929
2930 static void pr_mouse_drag_cb(GtkWidget *widget, GdkDragContext *context, gpointer data)
2931 {
2932 PixbufRenderer *pr;
2933
2934 pr = PIXBUF_RENDERER(widget);
2935
2936 pr->drag_moved = PR_DRAG_SCROLL_THRESHHOLD;
2937 }
2938
2939 static void pr_signals_connect(PixbufRenderer *pr)
2940 {
2941 g_signal_connect(G_OBJECT(pr), "motion_notify_event",
2942 G_CALLBACK(pr_mouse_motion_cb), pr);
2943 g_signal_connect(G_OBJECT(pr), "button_press_event",
2944 G_CALLBACK(pr_mouse_press_cb), pr);
2945 g_signal_connect(G_OBJECT(pr), "button_release_event",
2946 G_CALLBACK(pr_mouse_release_cb), pr);
2947 g_signal_connect(G_OBJECT(pr), "leave_notify_event",
2948 G_CALLBACK(pr_mouse_leave_cb), pr);
2949
2950 gtk_widget_set_events(GTK_WIDGET(pr), GDK_POINTER_MOTION_MASK |
2951 GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
2952 GDK_LEAVE_NOTIFY_MASK);
2953
2954 g_signal_connect(G_OBJECT(pr), "drag_begin",
2955 G_CALLBACK(pr_mouse_drag_cb), pr);
2956
2957 }
2958
2959 /*
2960 *-------------------------------------------------------------------
2961 * public
2962 *-------------------------------------------------------------------
2963 */
2964
2965 static void pr_pixbuf_sync(PixbufRenderer *pr, gdouble zoom, gint blank, gint new)
2966 {
2967 if (!pr->pixbuf)
2968 {
2969 GtkWidget *box;
2970
2971 /* no pixbuf so just clear the window */
2972 pr->image_width = 0;
2973 pr->image_height = 0;
2974 pr->scale = 1.0;
2975
2976 box = GTK_WIDGET(pr);
2977
2978 if (GTK_WIDGET_REALIZED(box))
2979 {
2980 gdk_window_clear(box->window);
2981 pr_overlay_draw(pr, 0, 0, pr->window_width, pr->window_height);
2982 }
2983
2984 pr_update_signal(pr);
2985
2986 return;
2987 }
2988
2989 pr->image_width = gdk_pixbuf_get_width(pr->pixbuf);
2990 pr->image_height = gdk_pixbuf_get_height(pr->pixbuf);
2991
2992 pr_zoom_sync(pr, zoom, TRUE, blank, new, FALSE, 0, 0);
2993 }
2994
2995 static void pr_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom, gint new)
2996 {
2997 if (pixbuf) g_object_ref(pixbuf);
2998 if (pr->pixbuf) g_object_unref(pr->pixbuf);
2999 pr->pixbuf = pixbuf;
3000
3001 pr_pixbuf_sync(pr, zoom, FALSE, new);
3002 }
3003
3004 void pixbuf_renderer_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom)
3005 {
3006 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3007
3008 pr_source_tile_unset(pr);
3009
3010 pr_set_pixbuf(pr, pixbuf, zoom, TRUE);
3011
3012 pr_update_signal(pr);
3013 }
3014
3015 GdkPixbuf *pixbuf_renderer_get_pixbuf(PixbufRenderer *pr)
3016 {
3017 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
3018
3019 return pr->pixbuf;
3020 }
3021
3022 void pixbuf_renderer_area_changed(PixbufRenderer *pr, gint x, gint y, gint width, gint height)
3023 {
3024 gint sx, sy, sw, sh;
3025
3026 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3027
3028 if (pr->source_tiles_enabled)
3029 {
3030 pr_source_tile_changed(pr, x, y, width, height);
3031 }
3032
3033 if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
3034 {
3035 /* increase region when using a zoom quality that may access surrounding pixels */
3036 y -= 1;
3037 height += 2;
3038 }
3039
3040 sx = (gint)floor((double)x * pr->scale);
3041 sy = (gint)floor((double)y * pr->scale);
3042 sw = (gint)ceil((double)width * pr->scale);
3043 sh = (gint)ceil((double)height * pr->scale);
3044
3045 pr_queue(pr, sx, sy, sw, sh, FALSE, TILE_RENDER_AREA, TRUE);
3046 }
3047
3048 void pixbuf_renderer_zoom_adjust(PixbufRenderer *pr, gdouble increment)
3049 {
3050 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3051
3052 pr_zoom_adjust_real(pr, increment, FALSE, 0, 0);
3053 }
3054
3055 void pixbuf_renderer_zoom_adjust_at_point(PixbufRenderer *pr, gdouble increment, gint x, gint y)
3056 {
3057 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3058
3059 pr_zoom_adjust_real(pr, increment, TRUE, x, y);
3060 }
3061
3062 void pixbuf_renderer_zoom_set(PixbufRenderer *pr, gdouble zoom)
3063 {
3064 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3065
3066 pr_zoom_sync(pr, zoom, FALSE, FALSE, FALSE, FALSE, 0, 0);
3067 }
3068
3069 gdouble pixbuf_renderer_zoom_get(PixbufRenderer *pr)
3070 {
3071 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
3072
3073 return pr->zoom;
3074 }
3075
3076 gdouble pixbuf_renderer_zoom_get_scale(PixbufRenderer *pr)
3077 {
3078 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
3079
3080 return pr->scale;
3081 }
3082
3083 void pixbuf_renderer_zoom_set_limits(PixbufRenderer *pr, gdouble min, gdouble max)
3084 {
3085 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3086
3087 if (min > 1.0 || max < 1.0) return;
3088 if (min < 1.0 && min > -1.0) return;
3089 if (min < -200.0 || max > 200.0) return;
3090
3091 if (pr->zoom_min != min)
3092 {
3093 pr->zoom_min = min;
3094 g_object_notify(G_OBJECT(pr), "zoom_min");
3095 }
3096 if (pr->zoom_max != max)
3097 {
3098 pr->zoom_max = max;
3099 g_object_notify(G_OBJECT(pr), "zoom_max");
3100 }
3101 }
3102
3103 gint pixbuf_renderer_get_image_size(PixbufRenderer *pr, gint *width, gint *height)
3104 {
3105 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3106 g_return_val_if_fail(width != NULL && height != NULL, FALSE);
3107
3108 if (!pr->pixbuf && !pr->source_tiles_enabled)
3109 {
3110 *width = 0;
3111 *height = 0;
3112 return FALSE;
3113 }
3114
3115 *width = pr->image_width;
3116 *height = pr->image_height;
3117 return TRUE;
3118 }
3119
3120 gint pixbuf_renderer_get_scaled_size(PixbufRenderer *pr, gint *width, gint *height)
3121 {
3122 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3123 g_return_val_if_fail(width != NULL && height != NULL, FALSE);
3124
3125 if (!pr->pixbuf && !pr->source_tiles_enabled)
3126 {
3127 *width = 0;
3128 *height = 0;
3129 return FALSE;
3130 }
3131
3132 *width = pr->width;
3133 *height = pr->height;
3134 return TRUE;
3135 }
3136
3137 gint pixbuf_renderer_get_visible_rect(PixbufRenderer *pr, GdkRectangle *rect)
3138 {
3139 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3140 g_return_val_if_fail(rect != NULL, FALSE);
3141
3142 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
3143 !pr->scale)
3144 {
3145 rect->x = 0;
3146 rect->y = 0;
3147 rect->width = 0;
3148 rect->height = 0;
3149 return FALSE;
3150 }
3151
3152 rect->x = (gint)((gdouble)pr->x_scroll / pr->scale);
3153 rect->y = (gint)((gdouble)pr->y_scroll / pr->scale);
3154 rect->width = (gint)((gdouble)pr->vis_width / pr->scale);
3155 rect->height = (gint)((gdouble)pr->vis_height / pr->scale);
3156 return TRUE;
3157 }
3158
3159 gint pixbuf_renderer_get_virtual_rect(PixbufRenderer *pr, GdkRectangle *rect)
3160 {
3161 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3162 g_return_val_if_fail(rect != NULL, FALSE);
3163
3164 if ((!pr->pixbuf && !pr->source_tiles_enabled))
3165 {
3166 rect->x = 0;
3167 rect->y = 0;
3168 rect->width = 0;
3169 rect->height = 0;
3170 return FALSE;
3171 }
3172
3173 rect->x = pr->x_scroll;
3174 rect->y = pr->y_scroll;
3175 rect->width = pr->vis_width;
3176 rect->height = pr->vis_height;
3177 return TRUE;
3178 }
3179
3180