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!
15 #include "pixbuf-renderer.h"
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
25 #define GQVIEW_BUILD 1
29 #include "pixbuf_util.h"
33 /* size to use when breaking up image pane for rendering */
34 #define PR_TILE_SIZE 128
36 /* default size of tile cache (mb) */
37 #define PR_CACHE_SIZE_DEFAULT 8
39 /* default min and max zoom */
40 #define PR_ZOOM_MIN -32.0
41 #define PR_ZOOM_MAX 32.0
43 /* distance to drag mouse to disable image flip */
44 #define PR_DRAG_SCROLL_THRESHHOLD 4
46 /* increase pan rate when holding down shift */
47 #define PR_PAN_SHIFT_MULTIPLIER 6
50 #define PR_SCROLLER_UPDATES_PER_SEC 30
51 #define PR_SCROLLER_DEAD_ZONE 6
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
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)
61 #define PR_MIN_SCALE_SIZE 8
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;
70 typedef struct _ImageTile ImageTile;
71 typedef struct _QueueData QueueData;
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 '' */
84 /* render_todo: (explanation)
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*
91 ImageTileRenderType render_todo; /* what to do (see above) */
92 ImageTileRenderType render_done; /* highest that has been done before on tile */
97 guint size; /* est. memory used by pixmap and pixbuf */
110 typedef struct _SourceTile SourceTile;
119 typedef struct _OverlayData OverlayData;
128 gint relative; /* x,y coordinates are relative, negative values start bottom right */
131 gint always; /* hide temporarily when scrolling */
137 SIGNAL_SCROLL_NOTIFY,
138 SIGNAL_RENDER_COMPLETE,
154 PROP_CACHE_SIZE_DISPLAY,
155 PROP_CACHE_SIZE_TILES,
158 PROP_WINDOW_LIMIT_VALUE
163 static guint signals[SIGNAL_COUNT] = { 0 };
164 static GtkEventBoxClass *parent_class = NULL;
168 static void pixbuf_renderer_class_init(PixbufRendererClass *class);
169 static void pixbuf_renderer_init(PixbufRenderer *pr);
170 static void pixbuf_renderer_finalize(GObject *object);
171 static void pixbuf_renderer_set_property(GObject *object, guint prop_id,
172 const GValue *value, GParamSpec *pspec);
173 static void pixbuf_renderer_get_property(GObject *object, guint prop_id,
174 GValue *value, GParamSpec *pspec);
175 static gint pixbuf_renderer_expose(GtkWidget *widget, GdkEventExpose *event);
177 static void pr_render_complete_signal(PixbufRenderer *pr);
179 static void pr_overlay_list_clear(PixbufRenderer *pr);
180 static void pr_scroller_timer_set(PixbufRenderer *pr, gint start);
181 static void pr_border_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h);
184 static void pr_source_tile_free_all(PixbufRenderer *pr);
185 static void pr_tile_free_all(PixbufRenderer *pr);
186 static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h);
187 static gint pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it);
188 static void pr_queue_clear(PixbufRenderer *pr);
189 static void pr_queue_merge(QueueData *parent, QueueData *qd);
190 static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
191 gint clamp, ImageTileRenderType render, gint new_data);
193 static void pr_redraw(PixbufRenderer *pr, gint new_data);
195 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
196 gint force, gint blank, gint new,
197 gint center_point, gint px, gint py);
199 static void pr_signals_connect(PixbufRenderer *pr);
200 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data);
201 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area);
205 *-------------------------------------------------------------------
206 * Pixbuf Renderer object
207 *-------------------------------------------------------------------
210 GType pixbuf_renderer_get_type(void)
212 static GType pixbuf_renderer_type = 0;
214 if (!pixbuf_renderer_type)
216 static const GTypeInfo pixbuf_renderer_info =
218 sizeof(PixbufRendererClass),
219 NULL, /* base_init */
220 NULL, /* base_finalize */
221 (GClassInitFunc)pixbuf_renderer_class_init,
222 NULL, /* class_finalize */
223 NULL, /* class_data */
224 sizeof(PixbufRenderer),
226 (GInstanceInitFunc)pixbuf_renderer_init,
229 pixbuf_renderer_type = g_type_register_static(GTK_TYPE_EVENT_BOX, "PixbufRenderer",
230 &pixbuf_renderer_info, 0);
233 return pixbuf_renderer_type;
236 static void pixbuf_renderer_class_init(PixbufRendererClass *class)
238 GObjectClass *gobject_class = G_OBJECT_CLASS(class);
239 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);
241 parent_class = g_type_class_peek_parent(class);
243 gobject_class->set_property = pixbuf_renderer_set_property;
244 gobject_class->get_property = pixbuf_renderer_get_property;
246 gobject_class->finalize = pixbuf_renderer_finalize;
248 widget_class->expose_event = pixbuf_renderer_expose;
250 g_object_class_install_property(gobject_class,
252 g_param_spec_double("zoom_min",
258 G_PARAM_READABLE | G_PARAM_WRITABLE));
260 g_object_class_install_property(gobject_class,
262 g_param_spec_double("zoom_max",
268 G_PARAM_READABLE | G_PARAM_WRITABLE));
270 g_object_class_install_property(gobject_class,
272 g_param_spec_uint("zoom_quality",
278 G_PARAM_READABLE | G_PARAM_WRITABLE));
280 g_object_class_install_property(gobject_class,
282 g_param_spec_boolean("zoom_2pass",
286 G_PARAM_READABLE | G_PARAM_WRITABLE));
288 g_object_class_install_property(gobject_class,
290 g_param_spec_boolean("zoom_expand",
291 "Expand image in autozoom.",
294 G_PARAM_READABLE | G_PARAM_WRITABLE));
296 g_object_class_install_property(gobject_class,
298 g_param_spec_uint("dither_quality",
303 GDK_RGB_DITHER_NORMAL,
304 G_PARAM_READABLE | G_PARAM_WRITABLE));
306 g_object_class_install_property(gobject_class,
308 g_param_spec_uint("scroll_reset",
309 "New image scroll reset",
311 PR_SCROLL_RESET_TOPLEFT,
312 PR_SCROLL_RESET_NOCHANGE,
313 PR_SCROLL_RESET_TOPLEFT,
314 G_PARAM_READABLE | G_PARAM_WRITABLE));
316 g_object_class_install_property(gobject_class,
318 g_param_spec_boolean("delay_flip",
319 "Delay image update",
322 G_PARAM_READABLE | G_PARAM_WRITABLE));
324 g_object_class_install_property(gobject_class,
326 g_param_spec_boolean("loading",
327 "Image actively loading",
330 G_PARAM_READABLE | G_PARAM_WRITABLE));
332 g_object_class_install_property(gobject_class,
334 g_param_spec_boolean("complete",
335 "Image rendering complete",
338 G_PARAM_READABLE | G_PARAM_WRITABLE));
340 g_object_class_install_property(gobject_class,
341 PROP_CACHE_SIZE_DISPLAY,
342 g_param_spec_uint("cache_display",
343 "Display cache size MB",
347 PR_CACHE_SIZE_DEFAULT,
348 G_PARAM_READABLE | G_PARAM_WRITABLE));
350 g_object_class_install_property(gobject_class,
351 PROP_CACHE_SIZE_TILES,
352 g_param_spec_uint("cache_tiles",
354 "Number of tiles to retain in memory at any one time.",
357 PR_CACHE_SIZE_DEFAULT,
358 G_PARAM_READABLE | G_PARAM_WRITABLE));
360 g_object_class_install_property(gobject_class,
362 g_param_spec_boolean("window_fit",
363 "Fit window to image size",
366 G_PARAM_READABLE | G_PARAM_WRITABLE));
368 g_object_class_install_property(gobject_class,
370 g_param_spec_boolean("window_limit",
371 "Limit size of parent window",
374 G_PARAM_READABLE | G_PARAM_WRITABLE));
376 g_object_class_install_property(gobject_class,
377 PROP_WINDOW_LIMIT_VALUE,
378 g_param_spec_uint("window_limit_value",
379 "Size limit of parent window",
384 G_PARAM_READABLE | G_PARAM_WRITABLE));
386 signals[SIGNAL_ZOOM] =
388 G_OBJECT_CLASS_TYPE(gobject_class),
390 G_STRUCT_OFFSET(PixbufRendererClass, zoom),
392 g_cclosure_marshal_VOID__DOUBLE,
396 signals[SIGNAL_CLICKED] =
397 g_signal_new("clicked",
398 G_OBJECT_CLASS_TYPE(gobject_class),
400 G_STRUCT_OFFSET(PixbufRendererClass, clicked),
402 g_cclosure_marshal_VOID__BOXED,
406 signals[SIGNAL_SCROLL_NOTIFY] =
407 g_signal_new("scroll-notify",
408 G_OBJECT_CLASS_TYPE(gobject_class),
410 G_STRUCT_OFFSET(PixbufRendererClass, scroll_notify),
412 g_cclosure_marshal_VOID__VOID,
415 signals[SIGNAL_RENDER_COMPLETE] =
416 g_signal_new("render-complete",
417 G_OBJECT_CLASS_TYPE(gobject_class),
419 G_STRUCT_OFFSET(PixbufRendererClass, render_complete),
421 g_cclosure_marshal_VOID__VOID,
425 static void pixbuf_renderer_init(PixbufRenderer *pr)
429 box = GTK_WIDGET(pr);
431 pr->zoom_min = PR_ZOOM_MIN;
432 pr->zoom_max = PR_ZOOM_MAX;
433 pr->zoom_quality = GDK_INTERP_BILINEAR;
434 pr->zoom_2pass = FALSE;
439 pr->dither_quality = GDK_RGB_DITHER_NORMAL;
441 pr->scroll_reset = PR_SCROLL_RESET_TOPLEFT;
443 pr->draw_idle_id = -1;
445 pr->tile_width = PR_TILE_SIZE;
446 pr->tile_height = PR_TILE_SIZE;
449 pr->tile_cache_size = 0;
451 pr->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
453 pr->scroller_id = -1;
454 pr->scroller_overlay = -1;
456 pr->source_tiles_enabled = FALSE;
457 pr->source_tiles = NULL;
459 gtk_widget_set_double_buffered(box, FALSE);
460 g_signal_connect_after(G_OBJECT(box), "size_allocate",
461 G_CALLBACK(pr_size_cb), pr);
463 pr_signals_connect(pr);
466 static void pixbuf_renderer_finalize(GObject *object)
470 pr = PIXBUF_RENDERER(object);
473 pr_tile_free_all(pr);
475 if (pr->pixbuf) g_object_unref(pr->pixbuf);
477 pr_scroller_timer_set(pr, FALSE);
478 pr_overlay_list_clear(pr);
480 pr_source_tile_free_all(pr);
483 PixbufRenderer* pixbuf_renderer_new(void)
485 return g_object_new(TYPE_PIXBUF_RENDERER, NULL);
488 static void pixbuf_renderer_set_property(GObject *object, guint prop_id,
489 const GValue *value, GParamSpec *pspec)
493 pr = PIXBUF_RENDERER(object);
498 pr->zoom_min = g_value_get_double(value);
501 pr->zoom_max = g_value_get_double(value);
503 case PROP_ZOOM_QUALITY:
504 pr->zoom_quality = g_value_get_uint(value);
506 case PROP_ZOOM_2PASS:
507 pr->zoom_2pass = g_value_get_boolean(value);
509 case PROP_ZOOM_EXPAND:
510 pr->zoom_expand = g_value_get_boolean(value);
512 case PROP_DITHER_QUALITY:
513 pr->dither_quality = g_value_get_uint(value);
515 case PROP_SCROLL_RESET:
516 pr->scroll_reset = g_value_get_uint(value);
518 case PROP_DELAY_FLIP:
519 pr->delay_flip = g_value_get_boolean(value);
522 pr->loading = g_value_get_boolean(value);
525 pr->complete = g_value_get_boolean(value);
527 case PROP_CACHE_SIZE_DISPLAY:
528 pr->tile_cache_max = g_value_get_uint(value);
530 case PROP_CACHE_SIZE_TILES:
531 pr->source_tiles_cache_size = g_value_get_uint(value);
533 case PROP_WINDOW_FIT:
534 pr->window_fit = g_value_get_boolean(value);
536 case PROP_WINDOW_LIMIT:
537 pr->window_limit = g_value_get_boolean(value);
539 case PROP_WINDOW_LIMIT_VALUE:
540 pr->window_limit_size = g_value_get_uint(value);
543 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
548 static void pixbuf_renderer_get_property(GObject *object, guint prop_id,
549 GValue *value, GParamSpec *pspec)
553 pr = PIXBUF_RENDERER(object);
558 g_value_set_double(value, pr->zoom_min);
561 g_value_set_double(value, pr->zoom_max);
563 case PROP_ZOOM_QUALITY:
564 g_value_set_uint(value, pr->zoom_quality);
566 case PROP_ZOOM_2PASS:
567 g_value_set_boolean(value, pr->zoom_2pass);
569 case PROP_ZOOM_EXPAND:
570 g_value_set_boolean(value, pr->zoom_expand);
572 case PROP_DITHER_QUALITY:
573 g_value_set_uint(value, pr->dither_quality);
575 case PROP_SCROLL_RESET:
576 g_value_set_uint(value, pr->scroll_reset);
578 case PROP_DELAY_FLIP:
579 g_value_set_boolean(value, pr->delay_flip);
582 g_value_set_boolean(value, pr->loading);
585 g_value_set_boolean(value, pr->complete);
587 case PROP_CACHE_SIZE_DISPLAY:
588 g_value_set_uint(value, pr->tile_cache_max);
590 case PROP_CACHE_SIZE_TILES:
591 g_value_set_uint(value, pr->source_tiles_cache_size);
593 case PROP_WINDOW_FIT:
594 g_value_set_boolean(value, pr->window_fit);
596 case PROP_WINDOW_LIMIT:
597 g_value_set_boolean(value, pr->window_limit);
599 case PROP_WINDOW_LIMIT_VALUE:
600 g_value_set_uint(value, pr->window_limit_size);
603 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
608 static gint pixbuf_renderer_expose(GtkWidget *widget, GdkEventExpose *event)
610 if (GTK_WIDGET_DRAWABLE(widget))
612 if (!GTK_WIDGET_NO_WINDOW(widget))
614 pixbuf_renderer_paint(PIXBUF_RENDERER(widget), &event->area);
622 *-------------------------------------------------------------------
624 *-------------------------------------------------------------------
627 static void widget_set_cursor(GtkWidget *widget, gint icon)
631 if (!widget->window) return;
639 cursor = gdk_cursor_new (icon);
642 gdk_window_set_cursor(widget->window, cursor);
644 if (cursor) gdk_cursor_unref(cursor);
647 static gint pixmap_calc_size(GdkPixmap *pixmap)
651 d = gdk_drawable_get_depth(pixmap);
652 gdk_drawable_get_size(pixmap, &w, &h);
653 return w * h * (d / 8);
656 static gint util_clip_region(gint x, gint y, gint w, gint h,
657 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
658 gint *rx, gint *ry, gint *rw, gint *rh)
660 if (clip_x + clip_w <= x ||
662 clip_y + clip_h <= y ||
668 *rx = MAX(x, clip_x);
669 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
671 *ry = MAX(y, clip_y);
672 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
677 static gint pr_parent_window_sizable(PixbufRenderer *pr)
679 GdkWindowState state;
681 if (!pr->parent_window) return FALSE;
682 if (!pr->window_fit) return FALSE;
683 if (!GTK_WIDGET(pr)->window) return FALSE;
685 if (!pr->parent_window->window) return FALSE;
686 state = gdk_window_get_state(pr->parent_window->window);
687 if (state & GDK_WINDOW_STATE_MAXIMIZED) return FALSE;
692 static gint pr_parent_window_resize(PixbufRenderer *pr, gint w, gint h)
698 if (!pr_parent_window_sizable(pr)) return FALSE;
700 if (pr->window_limit)
702 gint sw = gdk_screen_width() * pr->window_limit_size / 100;
703 gint sh = gdk_screen_height() * pr->window_limit_size / 100;
709 widget = GTK_WIDGET(pr);
710 parent = GTK_WIDGET(pr->parent_window);
712 w += (parent->allocation.width - widget->allocation.width);
713 h += (parent->allocation.height - widget->allocation.height);
715 gdk_drawable_get_size(parent->window, &ww, &wh);
716 if (w == ww && h == wh) return FALSE;
718 gdk_window_resize(parent->window, w, h);
723 void pixbuf_renderer_set_parent(PixbufRenderer *pr, GtkWindow *window)
725 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
726 g_return_if_fail(window == NULL || GTK_IS_WINDOW(window));
728 pr->parent_window = GTK_WIDGET(window);
731 GtkWindow *pixbuf_renderer_get_parent(PixbufRenderer *pr)
733 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
735 return GTK_WINDOW(pr->parent_window);
740 *-------------------------------------------------------------------
742 *-------------------------------------------------------------------
745 static void pr_overlay_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
750 box = GTK_WIDGET(pr);
752 work = pr->overlay_list;
762 if (!od->visible) continue;
764 pw = gdk_pixbuf_get_width(od->pixbuf);
765 ph = gdk_pixbuf_get_height(od->pixbuf);
771 if (px < 0) px = pr->window_width - pw + px;
772 if (py < 0) py = pr->window_height - ph + py;
775 if (util_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
777 gdk_draw_pixbuf(box->window,
778 box->style->fg_gc[GTK_WIDGET_STATE(box)],
782 pr->dither_quality, rx, ry);
787 static void pr_overlay_queue_draw(PixbufRenderer *pr, OverlayData *od, gint hidden)
792 w = gdk_pixbuf_get_width(od->pixbuf);
793 h = gdk_pixbuf_get_height(od->pixbuf);
799 if (x < 0) x = pr->window_width - w + x;
800 if (y < 0) y = pr->window_height - h + y;
803 pr_queue(pr, pr->x_scroll - pr->x_offset + x,
804 pr->y_scroll - pr->y_offset + y,
806 FALSE, TILE_RENDER_ALL, FALSE);
808 old_vis = od->visible;
809 if (hidden) od->visible = FALSE;
810 pr_border_draw(pr, x, y, w, h);
811 od->visible = old_vis;
814 static void pr_overlay_queue_all(PixbufRenderer *pr)
818 work = pr->overlay_list;
821 OverlayData *od = work->data;
824 pr_overlay_queue_draw(pr, od, FALSE);
828 static OverlayData *pr_overlay_find(PixbufRenderer *pr, gint id)
832 work = pr->overlay_list;
835 OverlayData *od = work->data;
838 if (od->id == id) return od;
844 gint pixbuf_renderer_overlay_add(PixbufRenderer *pr, GdkPixbuf *pixbuf, gint x, gint y,
845 gint relative, gint always)
850 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
851 g_return_val_if_fail(pixbuf != NULL, -1);
854 while (pr_overlay_find(pr, id)) id++;
856 od = g_new0(OverlayData, 1);
859 g_object_ref(G_OBJECT(od->pixbuf));
862 od->relative = relative;
866 pr->overlay_list = g_list_append(pr->overlay_list, od);
868 pr_overlay_queue_draw(pr, od, FALSE);
873 static void pr_overlay_free(PixbufRenderer *pr, OverlayData *od)
875 pr->overlay_list = g_list_remove(pr->overlay_list, od);
877 if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
881 static void pr_overlay_list_clear(PixbufRenderer *pr)
883 while (pr->overlay_list)
887 od = pr->overlay_list->data;
888 pr_overlay_free(pr, od);
892 void pixbuf_renderer_overlay_set(PixbufRenderer *pr, gint id, GdkPixbuf *pixbuf, gint x, gint y)
896 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
898 od = pr_overlay_find(pr, id);
903 pr_overlay_queue_draw(pr, od, TRUE);
905 g_object_ref(G_OBJECT(pixbuf));
906 g_object_unref(G_OBJECT(od->pixbuf));
912 pr_overlay_queue_draw(pr, od, FALSE);
916 pr_overlay_queue_draw(pr, od, TRUE);
917 pr_overlay_free(pr, od);
921 gint pixbuf_renderer_overlay_get(PixbufRenderer *pr, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
925 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
927 od = pr_overlay_find(pr, id);
928 if (!od) return FALSE;
930 if (pixbuf) *pixbuf = od->pixbuf;
937 void pixbuf_renderer_overlay_remove(PixbufRenderer *pr, gint id)
939 pixbuf_renderer_overlay_set(pr, id, NULL, 0, 0);
943 *-------------------------------------------------------------------
945 *-------------------------------------------------------------------
949 static gboolean pr_scroller_update_cb(gpointer data)
951 PixbufRenderer *pr = data;
955 /* this was a simple scroll by difference between scroller and mouse position,
956 * but all this math results in a smoother result and accounts for a dead zone.
959 if (abs(pr->scroller_xpos - pr->scroller_x) < PR_SCROLLER_DEAD_ZONE)
965 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
966 x = (pr->scroller_xpos - pr->scroller_x) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
967 x += (x > 0) ? -shift : shift;
970 if (abs(pr->scroller_ypos - pr->scroller_y) < PR_SCROLLER_DEAD_ZONE)
976 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
977 y = (pr->scroller_ypos - pr->scroller_y) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
978 y += (y > 0) ? -shift : shift;
981 if (abs(x) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
987 xinc = pr->scroller_xinc;
991 if (xinc < 0) xinc = 0;
992 if (x < xinc) xinc = x;
993 if (x > xinc) xinc = MIN(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
997 if (xinc > 0) xinc = 0;
998 if (x > xinc) xinc = x;
999 if (x < xinc) xinc = MAX(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
1003 if (abs(y) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
1009 yinc = pr->scroller_yinc;
1013 if (yinc < 0) yinc = 0;
1014 if (y < yinc) yinc = y;
1015 if (y > yinc) yinc = MIN(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
1019 if (yinc > 0) yinc = 0;
1020 if (y > yinc) yinc = y;
1021 if (y < yinc) yinc = MAX(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
1025 pr->scroller_xinc = xinc;
1026 pr->scroller_yinc = yinc;
1028 xinc = xinc / PR_SCROLLER_UPDATES_PER_SEC;
1029 yinc = yinc / PR_SCROLLER_UPDATES_PER_SEC;
1031 pixbuf_renderer_scroll(pr, xinc, yinc);
1036 static void pr_scroller_timer_set(PixbufRenderer *pr, gint start)
1038 if (pr->scroller_id != -1)
1040 g_source_remove(pr->scroller_id);
1041 pr->scroller_id = -1;
1046 pr->scroller_id = g_timeout_add(1000 / PR_SCROLLER_UPDATES_PER_SEC,
1047 pr_scroller_update_cb, pr);
1051 static void pr_scroller_start(PixbufRenderer *pr, gint x, gint y)
1053 if (pr->scroller_overlay == -1)
1059 pixbuf = pixbuf_inline(PIXBUF_INLINE_SCROLLER);
1061 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 32, 32);
1062 gdk_pixbuf_fill(pixbuf, 0x000000ff);
1064 w = gdk_pixbuf_get_width(pixbuf);
1065 h = gdk_pixbuf_get_height(pixbuf);
1067 pr->scroller_overlay = pixbuf_renderer_overlay_add(pr, pixbuf, x - w / 2, y - h / 2, FALSE, TRUE);
1068 g_object_unref(pixbuf);
1073 pr->scroller_xpos = x;
1074 pr->scroller_ypos = y;
1076 pr_scroller_timer_set(pr, TRUE);
1079 static void pr_scroller_stop(PixbufRenderer *pr)
1081 if (pr->scroller_id == -1) return;
1083 pixbuf_renderer_overlay_remove(pr, pr->scroller_overlay);
1084 pr->scroller_overlay = -1;
1086 pr_scroller_timer_set(pr, FALSE);
1090 *-------------------------------------------------------------------
1092 *-------------------------------------------------------------------
1095 static void pr_border_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
1098 gint rx, ry, rw, rh;
1100 box = GTK_WIDGET(pr);
1102 if (!box->window) return;
1104 if (!pr->pixbuf && !pr->source_tiles_enabled)
1106 if (util_clip_region(x, y, w, h,
1108 pr->window_width, pr->window_height,
1109 &rx, &ry, &rw, &rh))
1111 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1112 pr_overlay_draw(pr, rx, ry, rw, rh);
1117 if (pr->vis_width < pr->window_width)
1119 if (pr->x_offset > 0 &&
1120 util_clip_region(x, y, w, h,
1122 pr->x_offset, pr->window_height,
1123 &rx, &ry, &rw, &rh))
1125 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1126 pr_overlay_draw(pr, rx, ry, rw, rh);
1128 if (pr->window_width - pr->vis_width - pr->x_offset > 0 &&
1129 util_clip_region(x, y, w, h,
1130 pr->x_offset + pr->vis_width, 0,
1131 pr->window_width - pr->vis_width - pr->x_offset, pr->window_height,
1132 &rx, &ry, &rw, &rh))
1134 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1135 pr_overlay_draw(pr, rx, ry, rw, rh);
1138 if (pr->vis_height < pr->window_height)
1140 if (pr->y_offset > 0 &&
1141 util_clip_region(x, y, w, h,
1143 pr->vis_width, pr->y_offset,
1144 &rx, &ry, &rw, &rh))
1146 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1147 pr_overlay_draw(pr, rx, ry, rw, rh);
1149 if (pr->window_height - pr->vis_height - pr->y_offset > 0 &&
1150 util_clip_region(x, y, w, h,
1151 pr->x_offset, pr->y_offset + pr->vis_height,
1152 pr->vis_width, pr->window_height - pr->vis_height - pr->y_offset,
1153 &rx, &ry, &rw, &rh))
1155 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1156 pr_overlay_draw(pr, rx, ry, rw, rh);
1161 static void pr_border_clear(PixbufRenderer *pr)
1163 pr_border_draw(pr, 0, 0, pr->window_width, pr->window_height);
1166 void pixbuf_renderer_set_color(PixbufRenderer *pr, GdkColor *color)
1171 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1173 widget = GTK_WIDGET(pr);
1175 style = gtk_style_copy(gtk_widget_get_style(widget));
1176 g_object_ref(G_OBJECT(style));
1182 slot = &style->bg[GTK_STATE_NORMAL];
1184 slot->red = color->red;
1185 slot->green = color->green;
1186 slot->blue = color->blue;
1189 gtk_widget_set_style(widget, style);
1190 g_object_unref(G_OBJECT(style));
1192 if (GTK_WIDGET_VISIBLE(widget)) pr_border_clear(pr);
1195 void pixbuf_renderer_set_black(PixbufRenderer *pr, gint black)
1197 GdkColor color = { 0, 0, 0, 0};
1199 pixbuf_renderer_set_color(pr, &color);
1204 *-------------------------------------------------------------------
1206 *-------------------------------------------------------------------
1209 static void pr_source_tile_free(SourceTile *st)
1213 if (st->pixbuf) g_object_unref(st->pixbuf);
1217 static void pr_source_tile_free_all(PixbufRenderer *pr)
1221 work = pr->source_tiles;
1229 pr_source_tile_free(st);
1232 g_list_free(pr->source_tiles);
1233 pr->source_tiles = NULL;
1236 static void pr_source_tile_unset(PixbufRenderer *pr)
1238 pr_source_tile_free_all(pr);
1239 pr->source_tiles_enabled = FALSE;
1242 static gint pr_source_tile_visible(PixbufRenderer *pr, SourceTile *st)
1244 gint x1, y1, x2, y2;
1246 if (!st) return FALSE;
1248 x1 = (pr->x_scroll / PR_TILE_SIZE) * PR_TILE_SIZE;
1249 y1 = (pr->y_scroll / PR_TILE_SIZE) * PR_TILE_SIZE;
1250 x2 = ((pr->x_scroll + pr->vis_width) / PR_TILE_SIZE) * PR_TILE_SIZE + PR_TILE_SIZE;
1251 y2 = ((pr->y_scroll + pr->vis_height) / PR_TILE_SIZE) * PR_TILE_SIZE + PR_TILE_SIZE;
1253 return !((double)st->x * pr->scale > (double)x2 ||
1254 (double)(st->x + pr->source_tile_width) * pr->scale < (double)x1 ||
1255 (double)st->y * pr->scale > (double)y2 ||
1256 (double)(st->y + pr->source_tile_height) * pr->scale < (double)y1);
1259 static SourceTile *pr_source_tile_new(PixbufRenderer *pr, gint x, gint y)
1261 SourceTile *st = NULL;
1264 g_return_val_if_fail(pr->source_tile_width >= 1 && pr->source_tile_height >= 1, NULL);
1266 if (pr->source_tiles_cache_size < 4) pr->source_tiles_cache_size = 4;
1268 count = g_list_length(pr->source_tiles);
1269 if (count >= pr->source_tiles_cache_size)
1273 work = g_list_last(pr->source_tiles);
1274 while (work && count >= pr->source_tiles_cache_size)
1278 needle = work->data;
1281 if (!pr_source_tile_visible(pr, needle))
1283 pr->source_tiles = g_list_remove(pr->source_tiles, needle);
1285 if (pr->func_tile_dispose)
1287 pr->func_tile_dispose(pr, needle->x, needle->y,
1288 pr->source_tile_width, pr->source_tile_height,
1289 needle->pixbuf, pr->func_tile_data);
1298 pr_source_tile_free(needle);
1308 st = g_new0(SourceTile, 1);
1309 st->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
1310 pr->source_tile_width, pr->source_tile_height);
1313 st->x = (x / pr->source_tile_width) * pr->source_tile_width;
1314 st->y = (y / pr->source_tile_height) * pr->source_tile_height;
1317 pr->source_tiles = g_list_prepend(pr->source_tiles, st);
1322 static SourceTile *pr_source_tile_request(PixbufRenderer *pr, gint x, gint y)
1326 st = pr_source_tile_new(pr, x, y);
1327 if (!st) return NULL;
1329 if (pr->func_tile_request &&
1330 pr->func_tile_request(pr, st->x, st->y,
1331 pr->source_tile_width, pr->source_tile_height, st->pixbuf, pr->func_tile_data))
1336 pr_tile_invalidate_region(pr, st->x * pr->scale, st->y * pr->scale,
1337 pr->source_tile_width * pr->scale, pr->source_tile_height * pr->scale);
1342 static SourceTile *pr_source_tile_find(PixbufRenderer *pr, gint x, gint y)
1346 work = pr->source_tiles;
1349 SourceTile *st = work->data;
1351 if (x >= st->x && x < st->x + pr->source_tile_width &&
1352 y >= st->y && y < st->y + pr->source_tile_height)
1354 if (work != pr->source_tiles)
1356 pr->source_tiles = g_list_remove_link(pr->source_tiles, work);
1357 pr->source_tiles = g_list_concat(work, pr->source_tiles);
1368 static GList *pr_source_tile_compute_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h, gint request)
1376 if (w > pr->image_width) w = pr->image_width;
1377 if (h > pr->image_height) h = pr->image_height;
1379 sx = (x / pr->source_tile_width) * pr->source_tile_width;
1380 sy = (y / pr->source_tile_height) * pr->source_tile_height;
1382 for (x1 = sx; x1 < x + w; x1+= pr->source_tile_width)
1384 for (y1 = sy; y1 < y + h; y1 += pr->source_tile_height)
1388 st = pr_source_tile_find(pr, x1, y1);
1389 if (!st && request) st = pr_source_tile_request(pr, x1, y1);
1391 if (st) list = g_list_prepend(list, st);
1395 return g_list_reverse(list);
1398 static void pr_source_tile_changed(PixbufRenderer *pr, gint x, gint y, gint width, gint height)
1402 work = pr->source_tiles;
1406 gint rx, ry, rw, rh;
1411 if (util_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1412 x, y, width, height,
1413 &rx, &ry, &rw, &rh))
1417 pixbuf = gdk_pixbuf_new_subpixbuf(st->pixbuf, rx - st->x, ry - st->y, rw, rh);
1418 if (pr->func_tile_request &&
1419 pr->func_tile_request(pr, rx, ry, rw, rh, pixbuf, pr->func_tile_data))
1421 pr_tile_invalidate_region(pr, rx * pr->scale, ry * pr->scale,
1422 rw * pr->scale, rh * pr->scale);
1424 g_object_unref(pixbuf);
1429 static gint pr_source_tile_render(PixbufRenderer *pr, ImageTile *it,
1430 gint x, gint y, gint w, gint h,
1431 gint new_data, gint fast)
1438 box = GTK_WIDGET(pr);
1440 if (pr->zoom == 1.0 || pr->scale == 1.0)
1442 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1447 gint rx, ry, rw, rh;
1452 if (util_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1453 it->x + x, it->y + y, w, h,
1454 &rx, &ry, &rw, &rh))
1458 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1459 rx - st->x, ry - st->y, rw, rh);
1461 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1463 gdk_draw_pixbuf(it->pixmap,
1464 box->style->fg_gc[GTK_WIDGET_STATE(box)],
1466 rx - st->x, ry - st->y,
1467 rx - it->x, ry - it->y,
1469 pr->dither_quality, rx, ry);
1476 double scale_x, scale_y;
1477 gint sx, sy, sw, sh;
1479 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1480 scale_x = (double)pr->width / pr->image_width;
1481 scale_y = (double)pr->height / pr->image_height;
1483 sx = (double)(it->x + x) / scale_x;
1484 sy = (double)(it->y + y) / scale_y;
1485 sw = (double)w / scale_x;
1486 sh = (double)h / scale_y;
1488 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1491 /* draws red over draw region, to check for leaks (regions not filled) */
1492 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1495 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1500 gint rx, ry, rw, rh;
1501 gint stx, sty, stw, sth;
1506 stx = floor((double)st->x * scale_x);
1507 sty = floor((double)st->y * scale_y);
1508 stw = ceil ((double)(st->x + pr->source_tile_width) * scale_x) - stx;
1509 sth = ceil ((double)(st->y + pr->source_tile_height) * scale_y) - sty;
1511 if (util_clip_region(stx, sty, stw, sth,
1512 it->x + x, it->y + y, w, h,
1513 &rx, &ry, &rw, &rh))
1517 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1518 rx - st->x, ry - st->y, rw, rh);
1525 /* may need to use unfloored stx,sty values here */
1526 offset_x = (double)(stx - it->x);
1527 offset_y = (double)(sty - it->y);
1529 gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1530 (double) 0.0 + offset_x,
1531 (double) 0.0 + offset_y,
1533 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1545 void pixbuf_renderer_set_tiles(PixbufRenderer *pr, gint width, gint height,
1546 gint tile_width, gint tile_height, gint cache_size,
1547 PixbufRendererTileRequestFunc func_request,
1548 PixbufRendererTileDisposeFunc func_dispose,
1552 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1553 g_return_if_fail(tile_width >= 32 && tile_width >= 32);
1554 g_return_if_fail(width >= 32 && height > 32);
1555 g_return_if_fail(func_request != NULL);
1557 if (pr->pixbuf) g_object_unref(pr->pixbuf);
1560 pr_source_tile_unset(pr);
1562 if (cache_size < 4) cache_size = 4;
1564 pr->source_tiles_enabled = TRUE;
1565 pr->source_tiles_cache_size = cache_size;
1566 pr->source_tile_width = tile_width;
1567 pr->source_tile_height = tile_height;
1569 pr->image_width = width;
1570 pr->image_height = height;
1572 pr->func_tile_request = func_request;
1573 pr->func_tile_dispose = func_dispose;
1574 pr->func_tile_data = user_data;
1576 pr_zoom_sync(pr, zoom, TRUE, FALSE, TRUE, FALSE, 0, 0);
1577 pr_redraw(pr, TRUE);
1580 gint pixbuf_renderer_get_tiles(PixbufRenderer *pr)
1582 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
1584 return pr->source_tiles_enabled;
1587 static void pr_zoom_adjust_real(PixbufRenderer *pr, gdouble increment,
1588 gint center_point, gint x, gint y)
1590 gdouble zoom = pr->zoom;
1592 if (increment == 0.0) return;
1596 if (pr->scale < 1.0)
1598 zoom = 0.0 - 1.0 / pr->scale;
1606 if (increment < 0.0)
1608 if (zoom >= 1.0 && zoom + increment < 1.0)
1610 zoom = zoom + increment - 2.0;
1614 zoom = zoom + increment;
1619 if (zoom <= -1.0 && zoom + increment > -1.0)
1621 zoom = zoom + increment + 2.0;
1625 zoom = zoom + increment;
1629 pr_zoom_sync(pr, zoom, FALSE, FALSE, FALSE, center_point, x, y);
1633 *-------------------------------------------------------------------
1635 *-------------------------------------------------------------------
1638 static ImageTile *pr_tile_new(gint x, gint y, gint width, gint height)
1642 it = g_new0(ImageTile, 1);
1649 it->render_done = TILE_RENDER_NONE;
1654 static void pr_tile_free(ImageTile *it)
1658 if (it->pixbuf) gdk_pixbuf_unref(it->pixbuf);
1659 if (it->pixmap) g_object_unref(it->pixmap);
1664 static void pr_tile_free_all(PixbufRenderer *pr)
1679 g_list_free(pr->tiles);
1681 pr->tile_cache_size = 0;
1684 static ImageTile *pr_tile_add(PixbufRenderer *pr, gint x, gint y)
1688 it = pr_tile_new(x, y, pr->tile_width, pr->tile_height);
1690 if (it->x + it->w > pr->width) it->w = pr->width - it->x;
1691 if (it->y + it->h > pr->height) it->h = pr->height - it->y;
1693 pr->tiles = g_list_prepend(pr->tiles, it);
1694 pr->tile_cache_size += it->size;
1699 static void pr_tile_remove(PixbufRenderer *pr, ImageTile *it)
1703 QueueData *qd = it->qd;
1706 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
1712 QueueData *qd = it->qd2;
1715 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
1719 pr->tiles = g_list_remove(pr->tiles, it);
1720 pr->tile_cache_size -= it->size;
1725 static void pr_tile_free_space(PixbufRenderer *pr, guint space, ImageTile *it)
1730 work = g_list_last(pr->tiles);
1732 if (pr->source_tiles_enabled && pr->scale < 1.0)
1736 tiles = (pr->vis_width / PR_TILE_SIZE + 1) * (pr->vis_width / PR_TILE_SIZE + 1);
1737 tile_max = MAX(tiles * PR_TILE_SIZE * PR_TILE_SIZE * 3,
1738 (gint)((double)pr->tile_cache_max * 1048576.0 * pr->scale));
1742 tile_max = pr->tile_cache_max * 1048576;
1745 while (work && pr->tile_cache_size + space > tile_max)
1749 needle = work->data;
1752 ((!needle->qd && !needle->qd2) || !pr_tile_is_visible(pr, needle))) pr_tile_remove(pr, needle);
1756 static void pr_tile_invalidate_all(PixbufRenderer *pr)
1768 it->render_done = TILE_RENDER_NONE;
1769 it->render_todo = TILE_RENDER_NONE;
1772 it->w = MIN(pr->tile_width, pr->width - it->x);
1773 it->h = MIN(pr->tile_height, pr->height - it->y);
1777 static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
1783 x1 = (gint)floor(x / pr->tile_width) * pr->tile_width;
1784 x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width;
1786 y1 = (gint)floor(y / pr->tile_height) * pr->tile_height;
1787 y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height;
1797 if (it->x < x2 && it->x + it->w > x1 &&
1798 it->y < y2 && it->y + it->h > y1)
1800 it->render_done = TILE_RENDER_NONE;
1801 it->render_todo = TILE_RENDER_NONE;
1806 static ImageTile *pr_tile_get(PixbufRenderer *pr, gint x, gint y)
1816 if (it->x == x && it->y == y)
1818 pr->tiles = g_list_delete_link(pr->tiles, work);
1819 pr->tiles = g_list_prepend(pr->tiles, it);
1826 return pr_tile_add(pr, x, y);
1829 static void pr_tile_prepare(PixbufRenderer *pr, ImageTile *it)
1836 pixmap = gdk_pixmap_new(((GtkWidget *)pr)->window, pr->tile_width, pr->tile_height, -1);
1838 size = pixmap_calc_size(pixmap);
1839 pr_tile_free_space(pr, size, it);
1841 it->pixmap = pixmap;
1843 pr->tile_cache_size += size;
1846 if ((pr->zoom != 1.0 || pr->source_tiles_enabled || (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf)) ) &&
1854 pixbuf = gdk_pixbuf_new(gdk_pixbuf_get_colorspace(pr->pixbuf),
1855 gdk_pixbuf_get_has_alpha(pr->pixbuf),
1856 gdk_pixbuf_get_bits_per_sample(pr->pixbuf),
1857 pr->tile_width, pr->tile_height);
1861 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, pr->tile_width, pr->tile_height);
1864 size = gdk_pixbuf_get_rowstride(pixbuf) * pr->tile_height;
1865 pr_tile_free_space(pr, size, it);
1867 it->pixbuf = pixbuf;
1869 pr->tile_cache_size += size;
1874 *-------------------------------------------------------------------
1876 *-------------------------------------------------------------------
1879 static void pr_tile_render(PixbufRenderer *pr, ImageTile *it,
1880 gint x, gint y, gint w, gint h,
1881 gint new_data, gint fast)
1887 if (it->render_todo == TILE_RENDER_NONE && it->pixmap && !new_data) return;
1889 if (it->render_done != TILE_RENDER_ALL)
1895 if (!fast) it->render_done = TILE_RENDER_ALL;
1897 else if (it->render_todo != TILE_RENDER_AREA)
1899 if (!fast) it->render_todo = TILE_RENDER_NONE;
1903 if (!fast) it->render_todo = TILE_RENDER_NONE;
1905 if (new_data) it->blank = FALSE;
1907 pr_tile_prepare(pr, it);
1908 has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1910 box = GTK_WIDGET(pr);
1912 /* FIXME checker colors for alpha should be configurable,
1913 * also should be drawn for blank = TRUE
1918 /* no data, do fast rect fill */
1919 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1920 0, 0, it->w, it->h);
1922 else if (pr->source_tiles_enabled)
1924 draw = pr_source_tile_render(pr, it, x, y, w, h, new_data, fast);
1926 else if (pr->zoom == 1.0 || pr->scale == 1.0)
1930 gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, x, y, w, h,
1931 (double) 0.0 - it->x,
1932 (double) 0.0 - it->y,
1933 1.0, 1.0, GDK_INTERP_NEAREST,
1934 255, it->x + x, it->y + y,
1935 PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
1940 /* faster, simple */
1941 gdk_draw_pixbuf(it->pixmap,
1942 box->style->fg_gc[GTK_WIDGET_STATE(box)],
1944 it->x + x, it->y + y,
1947 pr->dither_quality, it->x + x, it->y + y);
1952 double scale_x, scale_y;
1954 if (pr->image_width == 0 || pr->image_height == 0) return;
1955 scale_x = (double)pr->width / pr->image_width;
1956 scale_y = (double)pr->height / pr->image_height;
1958 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1959 * small sizes for anything but GDK_INTERP_NEAREST
1961 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1965 gdk_pixbuf_scale(pr->pixbuf, it->pixbuf, x, y, w, h,
1966 (double) 0.0 - it->x,
1967 (double) 0.0 - it->y,
1969 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1973 gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, x, y, w, h,
1974 (double) 0.0 - it->x,
1975 (double) 0.0 - it->y,
1977 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1978 255, it->x + x, it->y + y,
1979 PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
1984 if (draw && it->pixbuf && !it->blank)
1986 gdk_draw_pixbuf(it->pixmap,
1987 box->style->fg_gc[GTK_WIDGET_STATE(box)],
1992 pr->dither_quality, it->x + x, it->y + y);
1997 static void pr_tile_expose(PixbufRenderer *pr, ImageTile *it,
1998 gint x, gint y, gint w, gint h,
1999 gint new_data, gint fast)
2003 pr_tile_render(pr, it, x, y, w, h, new_data, fast);
2005 box = GTK_WIDGET(pr);
2007 gdk_draw_drawable(box->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
2009 pr->x_offset + (it->x - pr->x_scroll) + x, pr->y_offset + (it->y - pr->y_scroll) + y, w, h);
2011 if (pr->overlay_list)
2013 pr_overlay_draw(pr, pr->x_offset + (it->x - pr->x_scroll) + x,
2014 pr->y_offset + (it->y - pr->y_scroll) + y,
2020 static gint pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it)
2022 return (it->x + it->w >= pr->x_scroll && it->x <= pr->x_scroll + pr->window_width - pr->x_offset * 2 &&
2023 it->y + it->h >= pr->y_scroll && it->y <= pr->y_scroll + pr->window_height - pr->y_offset * 2);
2027 *-------------------------------------------------------------------
2029 *-------------------------------------------------------------------
2032 static gint pr_queue_draw_idle_cb(gpointer data)
2034 PixbufRenderer *pr = data;
2038 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
2039 (!pr->draw_queue && !pr->draw_queue_2pass) ||
2040 pr->draw_idle_id == -1)
2042 pr_render_complete_signal(pr);
2044 pr->draw_idle_id = -1;
2050 qd = pr->draw_queue->data;
2051 fast = (pr->zoom_2pass && pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0);
2057 /* still loading, wait till done (also drops the higher priority) */
2059 pr->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
2060 pr_queue_draw_idle_cb, pr, NULL);
2061 pr->draw_idle_high = FALSE;
2065 qd = pr->draw_queue_2pass->data;
2069 if (GTK_WIDGET_REALIZED(pr))
2071 if (pr_tile_is_visible(pr, qd->it))
2073 pr_tile_expose(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2075 else if (qd->new_data)
2077 /* if new pixel data, and we already have a pixmap, update the tile */
2078 qd->it->blank = FALSE;
2079 if (qd->it->pixmap && qd->it->render_done == TILE_RENDER_ALL)
2081 pr_tile_render(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2089 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
2094 pr_queue_merge(qd->it->qd2, qd);
2100 pr->draw_queue_2pass = g_list_append(pr->draw_queue_2pass, qd);
2111 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
2115 if (!pr->draw_queue && !pr->draw_queue_2pass)
2117 pr_render_complete_signal(pr);
2119 pr->draw_idle_id = -1;
2126 static void pr_queue_list_free(GList *list)
2146 static void pr_queue_clear(PixbufRenderer *pr)
2148 pr_queue_list_free(pr->draw_queue);
2149 pr->draw_queue = NULL;
2151 pr_queue_list_free(pr->draw_queue_2pass);
2152 pr->draw_queue_2pass = NULL;
2154 if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
2155 pr->draw_idle_id = -1;
2158 static void pr_queue_merge(QueueData *parent, QueueData *qd)
2160 if (parent->x + parent->w < qd->x + qd->w)
2162 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
2164 if (parent->x > qd->x)
2166 parent->w += parent->x - qd->x;
2170 if (parent->y + parent->h < qd->y + qd->h)
2172 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
2174 if (parent->y > qd->y)
2176 parent->h += parent->y - qd->y;
2180 parent->new_data |= qd->new_data;
2183 static gint pr_clamp_to_visible(PixbufRenderer *pr, gint *x, gint *y, gint *w, gint *h)
2191 vh = pr->vis_height;
2196 if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
2199 nx = CLAMP(*x, vx, vx + vw);
2200 nw = CLAMP(*w - (nx - *x), 1, vw);
2202 ny = CLAMP(*y, vy, vy + vh);
2203 nh = CLAMP(*h - (ny - *y), 1, vh);
2213 static gint pr_queue_to_tiles(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
2214 gint clamp, ImageTileRenderType render, gint new_data)
2220 if (clamp && !pr_clamp_to_visible(pr, &x, &y, &w, &h)) return FALSE;
2222 x1 = (gint)floor(x / pr->tile_width) * pr->tile_width;
2223 x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width;
2225 y1 = (gint)floor(y / pr->tile_height) * pr->tile_height;
2226 y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height;
2228 for (j = y1; j <= y2; j += pr->tile_height)
2230 for (i = x1; i <= x2; i += pr->tile_width)
2234 it = pr_tile_get(pr, i, j);
2239 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
2240 (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
2242 it->render_todo = render;
2245 qd = g_new(QueueData, 1);
2247 qd->new_data = new_data;
2257 qd->w = x + w - i - qd->x;
2258 if (qd->x + qd->w > pr->tile_width) qd->w = pr->tile_width - qd->x;
2268 qd->h = y + h - j - qd->y;
2269 if (qd->y + qd->h > pr->tile_height) qd->h = pr->tile_height - qd->y;
2271 if (qd->w < 1 || qd->h < 1)
2277 pr_queue_merge(it->qd, qd);
2283 pr->draw_queue = g_list_append(pr->draw_queue, qd);
2292 static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
2293 gint clamp, ImageTileRenderType render, gint new_data)
2297 nx = CLAMP(x, 0, pr->width - 1);
2298 ny = CLAMP(y, 0, pr->height - 1);
2301 w = CLAMP(w, 0, pr->width - nx);
2302 h = CLAMP(h, 0, pr->height - ny);
2303 if (w < 1 || h < 1) return;
2305 if (pr_queue_to_tiles(pr, nx, ny, w, h, clamp, render, new_data) &&
2306 ((!pr->draw_queue && !pr->draw_queue_2pass) || pr->draw_idle_id == -1 || !pr->draw_idle_high))
2308 if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
2309 pr->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW,
2310 pr_queue_draw_idle_cb, pr, NULL);
2311 pr->draw_idle_high = TRUE;
2315 static void pr_redraw(PixbufRenderer *pr, gint new_data)
2318 pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, new_data);
2322 *-------------------------------------------------------------------
2324 *-------------------------------------------------------------------
2327 static void pr_update_signal(PixbufRenderer *pr)
2330 printf("FIXME: send updated signal\n");
2334 static void pr_zoom_signal(PixbufRenderer *pr)
2336 g_signal_emit(pr, signals[SIGNAL_ZOOM], 0, pr->zoom);
2339 static void pr_clicked_signal(PixbufRenderer *pr, GdkEventButton *bevent)
2341 g_signal_emit(pr, signals[SIGNAL_CLICKED], 0, bevent);
2344 static void pr_scroll_notify_signal(PixbufRenderer *pr)
2346 g_signal_emit(pr, signals[SIGNAL_SCROLL_NOTIFY], 0);
2349 static void pr_render_complete_signal(PixbufRenderer *pr)
2353 g_signal_emit(pr, signals[SIGNAL_RENDER_COMPLETE], 0);
2354 g_object_set(G_OBJECT(pr), "complete", TRUE, NULL);
2359 *-------------------------------------------------------------------
2361 *-------------------------------------------------------------------
2364 static gint pr_scroll_clamp(PixbufRenderer *pr)
2369 if (pr->zoom == 0.0)
2374 pr_scroll_notify_signal(pr);
2378 old_xs = pr->x_scroll;
2379 old_ys = pr->y_scroll;
2381 if (pr->x_offset > 0)
2387 pr->x_scroll = CLAMP(pr->x_scroll, 0, pr->width - pr->vis_width);
2390 if (pr->y_offset > 0)
2396 pr->y_scroll = CLAMP(pr->y_scroll, 0, pr->height - pr->vis_height);
2399 pr_scroll_notify_signal(pr);
2401 return (old_xs != pr->x_scroll || old_ys != pr->y_scroll);
2404 static gint pr_size_clamp(PixbufRenderer *pr)
2406 gint old_vw, old_vh;
2408 old_vw = pr->vis_width;
2409 old_vh = pr->vis_height;
2411 if (pr->width < pr->window_width)
2413 pr->vis_width = pr->width;
2414 pr->x_offset = (pr->window_width - pr->width) / 2;
2418 pr->vis_width = pr->window_width;
2422 if (pr->height < pr->window_height)
2424 pr->vis_height = pr->height;
2425 pr->y_offset = (pr->window_height - pr->height) / 2;
2429 pr->vis_height = pr->window_height;
2433 return (old_vw != pr->vis_width || old_vh != pr->vis_height);
2436 static gint pr_zoom_clamp(PixbufRenderer *pr, gdouble zoom,
2437 gint force, gint new, gint invalidate,
2444 zoom = CLAMP(zoom, pr->zoom_min, pr->zoom_max);
2446 if (pr->zoom == zoom && !force) return FALSE;
2448 w = pr->image_width;
2449 h = pr->image_height;
2451 if (zoom == 0.0 && !pr->pixbuf)
2455 else if (zoom == 0.0)
2461 sizeable = (new && pr_parent_window_sizable(pr));
2465 max_w = gdk_screen_width();
2466 max_h = gdk_screen_height();
2468 if (pr->window_limit)
2470 max_w = max_w * pr->window_limit_size / 100;
2471 max_h = max_h * pr->window_limit_size / 100;
2476 max_w = pr->window_width;
2477 max_h = pr->window_height;
2480 if ((pr->zoom_expand && !sizeable) || w > max_w || h > max_h)
2482 if ((gdouble)max_w / w > (gdouble)max_h / h)
2484 scale = (gdouble)max_h / h;
2486 w = w * scale + 0.5;
2487 if (w > max_w) w = max_w;
2491 scale = (gdouble)max_w / w;
2493 h = h * scale + 0.5;
2494 if (h > max_h) h = max_h;
2504 else if (zoom > 0.0) /* zoom orig, in */
2512 scale = 1.0 / (0.0 - zoom);
2517 invalid = (pr->width != w || pr->height != h);
2524 if (invalidate || invalid)
2526 pr_tile_invalidate_all(pr);
2527 pr_redraw(pr, TRUE);
2529 if (redrawn) *redrawn = (invalidate || invalid);
2534 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
2535 gint force, gint blank, gint new,
2536 gint center_point, gint px, gint py)
2539 gint old_cx, old_cy;
2542 gint redrawn = FALSE;
2544 old_scale = pr->scale;
2547 px = CLAMP(px, 0, pr->width);
2548 py = CLAMP(py, 0, pr->height);
2549 old_cx = pr->x_scroll + (px - pr->x_offset);
2550 old_cy = pr->y_scroll + (py - pr->y_offset);
2555 old_cx = pr->x_scroll + pr->vis_width / 2;
2556 old_cy = pr->y_scroll + pr->vis_height / 2;
2559 if (!pr_zoom_clamp(pr, zoom, force, new, force, &redrawn)) return;
2561 clamped = pr_size_clamp(pr);
2562 sized = pr_parent_window_resize(pr, pr->width, pr->height);
2566 switch (pr->scroll_reset)
2568 case PR_SCROLL_RESET_NOCHANGE:
2569 /* maintain old scroll position, do nothing */
2571 case PR_SCROLL_RESET_CENTER:
2572 /* center new image */
2573 pr->x_scroll = ((double)pr->image_width / 2.0 * pr->scale) - pr->vis_width / 2;
2574 pr->y_scroll = ((double)pr->image_height / 2.0 * pr->scale) - pr->vis_height / 2;
2576 case PR_SCROLL_RESET_TOPLEFT:
2578 /* reset to upper left */
2586 /* user zoom does not force, so keep visible center point */
2589 pr->x_scroll = old_cx / old_scale * pr->scale - (px - pr->x_offset);
2590 pr->y_scroll = old_cy / old_scale * pr->scale - (py - pr->y_offset);
2594 pr->x_scroll = old_cx / old_scale * pr->scale - (pr->vis_width / 2);
2595 pr->y_scroll = old_cy / old_scale * pr->scale - (pr->vis_height / 2);
2599 pr_scroll_clamp(pr);
2602 pr_tile_sync(pr, blank);
2605 /* If the window was not sized, redraw the image - we know there will be no size/expose signal.
2606 * But even if a size is claimed, there is no guarantee that the window manager will allow it,
2607 * so redraw the window anyway :/
2609 if (sized || clamped) pr_border_clear(pr);
2610 pr_redraw(pr, redrawn);
2613 pr_update_signal(pr);
2616 static void pr_size_sync(PixbufRenderer *pr, gint new_width, gint new_height)
2618 if (pr->window_width == new_width && pr->window_height == new_height) return;
2620 pr->window_width = new_width;
2621 pr->window_height = new_height;
2623 if (pr->zoom == 0.0) pr_zoom_clamp(pr, 0.0, TRUE, FALSE, FALSE, NULL);
2626 pr_scroll_clamp(pr);
2628 /* ensure scroller remains visible */
2629 if (pr->scroller_overlay != -1)
2631 gint update = FALSE;
2633 if (pr->scroller_x > new_width)
2635 pr->scroller_x = new_width;
2636 pr->scroller_xpos = new_width;
2639 if (pr->scroller_y > new_height)
2641 pr->scroller_y = new_height;
2642 pr->scroller_ypos = new_height;
2650 if (pixbuf_renderer_overlay_get(pr, pr->scroller_overlay, &pixbuf, NULL, NULL))
2654 w = gdk_pixbuf_get_width(pixbuf);
2655 h = gdk_pixbuf_get_height(pixbuf);
2656 pixbuf_renderer_overlay_set(pr, pr->scroller_overlay, pixbuf,
2657 pr->scroller_x - w / 2, pr->scroller_y - h / 2);
2662 pr_border_clear(pr);
2665 pr_tile_sync(pr, pr->width, pr->height, FALSE);
2668 pr_update_signal(pr);
2671 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
2673 PixbufRenderer *pr = data;
2675 pr_size_sync(pr, allocation->width, allocation->height);
2678 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area)
2682 pr_border_draw(pr, area->x, area->y, area->width, area->height);
2684 x = MAX(0, (gint)area->x - pr->x_offset + pr->x_scroll);
2685 y = MAX(0, (gint)area->y - pr->y_offset + pr->y_scroll);
2688 MIN((gint)area->width, pr->width - x),
2689 MIN((gint)area->height, pr->height - y),
2690 FALSE, TILE_RENDER_ALL, FALSE);
2694 *-------------------------------------------------------------------
2696 *-------------------------------------------------------------------
2699 void pixbuf_renderer_scroll(PixbufRenderer *pr, gint x, gint y)
2705 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2707 if (!pr->pixbuf && !pr->source_tiles_enabled) return;
2709 old_x = pr->x_scroll;
2710 old_y = pr->y_scroll;
2715 pr_scroll_clamp(pr);
2716 if (pr->x_scroll == old_x && pr->y_scroll == old_y) return;
2718 if (pr->overlay_list)
2722 new_x = pr->x_scroll;
2723 new_y = pr->y_scroll;
2724 pr->x_scroll = old_x;
2725 pr->y_scroll = old_y;
2727 pr_overlay_queue_all(pr);
2729 pr->x_scroll = new_x;
2730 pr->y_scroll = new_y;
2733 x_off = pr->x_scroll - old_x;
2734 y_off = pr->y_scroll - old_y;
2736 w = pr->vis_width - abs(x_off);
2737 h = pr->vis_height - abs(y_off);
2741 /* scrolled completely to new material */
2742 pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE);
2774 box = GTK_WIDGET(pr);
2776 gc = gdk_gc_new(box->window);
2777 gdk_gc_set_exposures(gc, TRUE);
2778 gdk_draw_drawable(box->window, gc,
2780 x2 + pr->x_offset, y2 + pr->y_offset,
2781 x1 + pr->x_offset, y1 + pr->y_offset, w, h);
2784 if (pr->overlay_list)
2786 pr_overlay_queue_all(pr);
2789 w = pr->vis_width - w;
2790 h = pr->vis_height - h;
2795 x_off > 0 ? pr->x_scroll + (pr->vis_width - w) : pr->x_scroll, pr->y_scroll,
2796 w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE);
2800 /* FIXME, to optimize this, remove overlap */
2802 pr->x_scroll, y_off > 0 ? pr->y_scroll + (pr->vis_height - h) : pr->y_scroll,
2803 pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE);
2808 void pixbuf_renderer_scroll_to_point(PixbufRenderer *pr, gint x, gint y,
2809 gdouble x_align, gdouble y_align)
2814 x_align = CLAMP(x_align, 0.0, 1.0);
2815 y_align = CLAMP(y_align, 0.0, 1.0);
2817 ax = (gdouble)pr->vis_width * x_align;
2818 ay = (gdouble)pr->vis_height * y_align;
2820 px = (gdouble)x * pr->scale - (pr->x_scroll + ax);
2821 py = (gdouble)y * pr->scale - (pr->y_scroll + ay);
2823 pixbuf_renderer_scroll(pr, px, py);
2827 *-------------------------------------------------------------------
2829 *-------------------------------------------------------------------
2832 static gint pr_mouse_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
2837 pr = PIXBUF_RENDERER(widget);
2839 if (pr->scroller_id != -1)
2841 pr->scroller_xpos = bevent->x;
2842 pr->scroller_ypos = bevent->y;
2845 if (!pr->in_drag || !gdk_pointer_is_grabbed()) return FALSE;
2847 if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
2853 widget_set_cursor(widget, GDK_FLEUR);
2856 if (bevent->state & GDK_SHIFT_MASK)
2858 accel = PR_PAN_SHIFT_MULTIPLIER;
2866 pixbuf_renderer_scroll(pr, (pr->drag_last_x - bevent->x) * accel,
2867 (pr->drag_last_y - bevent->y) * accel);
2869 pr->drag_last_x = bevent->x;
2870 pr->drag_last_y = bevent->y;
2875 static gint pr_mouse_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
2880 pr = PIXBUF_RENDERER(widget);
2882 if (pr->scroller_id != -1) return TRUE;
2884 switch (bevent->button)
2888 pr->drag_last_x = bevent->x;
2889 pr->drag_last_y = bevent->y;
2891 gdk_pointer_grab(widget->window, FALSE,
2892 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
2893 NULL, NULL, bevent->time);
2894 gtk_grab_add(widget);
2900 pr_clicked_signal(pr, bevent);
2906 parent = gtk_widget_get_parent(widget);
2907 if (widget && GTK_WIDGET_CAN_FOCUS(parent))
2909 gtk_widget_grab_focus(parent);
2915 static gint pr_mouse_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
2919 pr = PIXBUF_RENDERER(widget);
2921 if (pr->scroller_id != -1)
2923 pr_scroller_stop(pr);
2927 if (gdk_pointer_is_grabbed() && GTK_WIDGET_HAS_GRAB(pr))
2929 gtk_grab_remove(widget);
2930 gdk_pointer_ungrab(bevent->time);
2931 widget_set_cursor(widget, -1);
2934 if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
2936 if (bevent->button == 1 && (bevent->state & GDK_SHIFT_MASK))
2938 pr_scroller_start(pr, bevent->x, bevent->y);
2940 else if (bevent->button == 1 || bevent->button == 2)
2942 pr_clicked_signal(pr, bevent);
2946 pr->in_drag = FALSE;
2951 static gint pr_mouse_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
2955 pr = PIXBUF_RENDERER(widget);
2957 if (pr->scroller_id != -1)
2959 pr->scroller_xpos = pr->scroller_x;
2960 pr->scroller_ypos = pr->scroller_y;
2961 pr->scroller_xinc = 0;
2962 pr->scroller_yinc = 0;
2968 static void pr_mouse_drag_cb(GtkWidget *widget, GdkDragContext *context, gpointer data)
2972 pr = PIXBUF_RENDERER(widget);
2974 pr->drag_moved = PR_DRAG_SCROLL_THRESHHOLD;
2977 static void pr_signals_connect(PixbufRenderer *pr)
2979 g_signal_connect(G_OBJECT(pr), "motion_notify_event",
2980 G_CALLBACK(pr_mouse_motion_cb), pr);
2981 g_signal_connect(G_OBJECT(pr), "button_press_event",
2982 G_CALLBACK(pr_mouse_press_cb), pr);
2983 g_signal_connect(G_OBJECT(pr), "button_release_event",
2984 G_CALLBACK(pr_mouse_release_cb), pr);
2985 g_signal_connect(G_OBJECT(pr), "leave_notify_event",
2986 G_CALLBACK(pr_mouse_leave_cb), pr);
2988 gtk_widget_set_events(GTK_WIDGET(pr), GDK_POINTER_MOTION_MASK |
2989 GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
2990 GDK_LEAVE_NOTIFY_MASK);
2992 g_signal_connect(G_OBJECT(pr), "drag_begin",
2993 G_CALLBACK(pr_mouse_drag_cb), pr);
2998 *-------------------------------------------------------------------
3000 *-------------------------------------------------------------------
3003 static void pr_pixbuf_sync(PixbufRenderer *pr, gdouble zoom, gint blank, gint new)
3009 /* no pixbuf so just clear the window */
3010 pr->image_width = 0;
3011 pr->image_height = 0;
3014 box = GTK_WIDGET(pr);
3016 if (GTK_WIDGET_REALIZED(box))
3018 gdk_window_clear(box->window);
3019 pr_overlay_draw(pr, 0, 0, pr->window_width, pr->window_height);
3022 pr_update_signal(pr);
3027 pr->image_width = gdk_pixbuf_get_width(pr->pixbuf);
3028 pr->image_height = gdk_pixbuf_get_height(pr->pixbuf);
3030 pr_zoom_sync(pr, zoom, TRUE, blank, new, FALSE, 0, 0);
3033 static void pr_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom, gint new)
3035 if (pixbuf) g_object_ref(pixbuf);
3036 if (pr->pixbuf) g_object_unref(pr->pixbuf);
3037 pr->pixbuf = pixbuf;
3039 pr_pixbuf_sync(pr, zoom, FALSE, new);
3042 void pixbuf_renderer_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom)
3044 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3046 pr_source_tile_unset(pr);
3048 pr_set_pixbuf(pr, pixbuf, zoom, TRUE);
3050 pr_update_signal(pr);
3053 GdkPixbuf *pixbuf_renderer_get_pixbuf(PixbufRenderer *pr)
3055 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
3060 void pixbuf_renderer_move(PixbufRenderer *pr, PixbufRenderer *source)
3063 PixbufRendererScrollResetType scroll_reset;
3065 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3066 g_return_if_fail(IS_PIXBUF_RENDERER(source));
3068 if (pr == source) return;
3070 object = G_OBJECT(pr);
3072 g_object_set(object, "zoom_min", source->zoom_min, NULL);
3073 g_object_set(object, "zoom_max", source->zoom_max, NULL);
3074 g_object_set(object, "loading", source->loading, NULL);
3076 pr->x_scroll = source->x_scroll;
3077 pr->y_scroll = source->y_scroll;
3079 scroll_reset = pr->scroll_reset;
3080 pr->scroll_reset = PR_SCROLL_RESET_NOCHANGE;
3082 if (source->source_tiles_enabled)
3084 pr_source_tile_unset(pr);
3086 pr->source_tiles_enabled = source->source_tiles_enabled;
3087 pr->source_tiles_cache_size = source->source_tiles_cache_size;
3088 pr->source_tile_width = source->source_tile_width;
3089 pr->source_tile_height = source->source_tile_height;
3090 pr->image_width = source->image_width;
3091 pr->image_height = source->image_height;
3093 pr->func_tile_request = source->func_tile_request;
3094 pr->func_tile_dispose = source->func_tile_dispose;
3095 pr->func_tile_data = source->func_tile_data;
3097 pr->source_tiles = source->source_tiles;
3098 source->source_tiles = NULL;
3100 pr_zoom_sync(pr, pr->zoom, TRUE, FALSE, TRUE, FALSE, 0, 0);
3101 pr_redraw(pr, TRUE);
3105 pixbuf_renderer_set_pixbuf(pr, source->pixbuf, source->zoom);
3108 pr->scroll_reset = scroll_reset;
3110 pixbuf_renderer_set_pixbuf(source, NULL, source->zoom);
3113 void pixbuf_renderer_area_changed(PixbufRenderer *pr, gint x, gint y, gint width, gint height)
3115 gint x1, y1, x2, y2;
3117 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3119 if (pr->source_tiles_enabled)
3121 pr_source_tile_changed(pr, x, y, width, height);
3124 if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
3126 /* increase region when using a zoom quality that may access surrounding pixels */
3131 x1 = (gint)floor((double)x * pr->scale);
3132 y1 = (gint)floor((double)y * pr->scale);
3133 x2 = (gint)ceil((double)(x + width) * pr->scale);
3134 y2 = (gint)ceil((double)(y + height) * pr->scale);
3136 pr_queue(pr, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE);
3139 void pixbuf_renderer_zoom_adjust(PixbufRenderer *pr, gdouble increment)
3141 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3143 pr_zoom_adjust_real(pr, increment, FALSE, 0, 0);
3146 void pixbuf_renderer_zoom_adjust_at_point(PixbufRenderer *pr, gdouble increment, gint x, gint y)
3148 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3150 pr_zoom_adjust_real(pr, increment, TRUE, x, y);
3153 void pixbuf_renderer_zoom_set(PixbufRenderer *pr, gdouble zoom)
3155 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3157 pr_zoom_sync(pr, zoom, FALSE, FALSE, FALSE, FALSE, 0, 0);
3160 gdouble pixbuf_renderer_zoom_get(PixbufRenderer *pr)
3162 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
3167 gdouble pixbuf_renderer_zoom_get_scale(PixbufRenderer *pr)
3169 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
3174 void pixbuf_renderer_zoom_set_limits(PixbufRenderer *pr, gdouble min, gdouble max)
3176 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3178 if (min > 1.0 || max < 1.0) return;
3179 if (min < 1.0 && min > -1.0) return;
3180 if (min < -200.0 || max > 200.0) return;
3182 if (pr->zoom_min != min)
3185 g_object_notify(G_OBJECT(pr), "zoom_min");
3187 if (pr->zoom_max != max)
3190 g_object_notify(G_OBJECT(pr), "zoom_max");
3194 gint pixbuf_renderer_get_image_size(PixbufRenderer *pr, gint *width, gint *height)
3196 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3197 g_return_val_if_fail(width != NULL && height != NULL, FALSE);
3199 if (!pr->pixbuf && !pr->source_tiles_enabled)
3206 *width = pr->image_width;
3207 *height = pr->image_height;
3211 gint pixbuf_renderer_get_scaled_size(PixbufRenderer *pr, gint *width, gint *height)
3213 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3214 g_return_val_if_fail(width != NULL && height != NULL, FALSE);
3216 if (!pr->pixbuf && !pr->source_tiles_enabled)
3224 *height = pr->height;
3228 gint pixbuf_renderer_get_visible_rect(PixbufRenderer *pr, GdkRectangle *rect)
3230 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3231 g_return_val_if_fail(rect != NULL, FALSE);
3233 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
3243 rect->x = (gint)((gdouble)pr->x_scroll / pr->scale);
3244 rect->y = (gint)((gdouble)pr->y_scroll / pr->scale);
3245 rect->width = (gint)((gdouble)pr->vis_width / pr->scale);
3246 rect->height = (gint)((gdouble)pr->vis_height / pr->scale);
3250 gint pixbuf_renderer_get_virtual_rect(PixbufRenderer *pr, GdkRectangle *rect)
3252 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3253 g_return_val_if_fail(rect != NULL, FALSE);
3255 if ((!pr->pixbuf && !pr->source_tiles_enabled))
3264 rect->x = pr->x_scroll;
3265 rect->y = pr->y_scroll;
3266 rect->width = pr->vis_width;
3267 rect->height = pr->vis_height;