From fe69cac65ad41213b6d3e1a9237dcf164db66444 Mon Sep 17 00:00:00 2001 From: John Ellis Date: Sat, 19 Mar 2005 01:15:17 +0000 Subject: [PATCH] ##### 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 * 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 * image.c, pixbuf-renderer.[ch]: More progress, and 2 pass zoom now works again (mostly). Tue Mar 15 23:17:41 2005 John Ellis * 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 * pan-view.c: Convert to using new PixbufRenderer. * pixbuf-renderer.[ch]: Clean up and fixes. Sun Mar 13 10:21:35 2005 John Ellis * 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 * pixbuf-renderer.[ch]: Set up object properties and additional. Mar 10 19:22:18 2005 John Ellis * 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 * 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. --- ChangeLog | 50 + TODO | 27 +- src/Makefile.am | 2 + src/fullscreen.c | 4 +- src/image-overlay.c | 20 +- src/image.c | 3674 +++++++---------------------------------- src/image.h | 2 + src/img-view.c | 21 +- src/info.c | 22 +- src/layout.c | 6 +- src/layout_image.c | 14 +- src/pan-view.c | 318 ++-- src/pixbuf-renderer.c | 3180 +++++++++++++++++++++++++++++++++++ src/pixbuf-renderer.h | 214 +++ src/print.c | 4 +- src/typedefs.h | 64 +- 16 files changed, 4262 insertions(+), 3360 deletions(-) create mode 100644 src/pixbuf-renderer.c create mode 100644 src/pixbuf-renderer.h diff --git a/ChangeLog b/ChangeLog index 110b7bda..55563b16 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,53 @@ +Fri Mar 18 19:52:38 2005 John Ellis + + * 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 + + * image.c, pixbuf-renderer.[ch]: More progress, and 2 pass zoom now + works again (mostly). + +Tue Mar 15 23:17:41 2005 John Ellis + + * 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 + + * pan-view.c: Convert to using new PixbufRenderer. + * pixbuf-renderer.[ch]: Clean up and fixes. + +Sun Mar 13 10:21:35 2005 John Ellis + + * 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 + + * pixbuf-renderer.[ch]: Set up object properties and additional. + +Mar 10 19:22:18 2005 John Ellis + + * 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 + + * 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. + Sat Mar 5 16:23:32 2005 John Ellis * README: Updates. diff --git a/TODO b/TODO index c3e0cf2a..07942805 100644 --- a/TODO +++ b/TODO @@ -4,9 +4,19 @@ TODO key: d = done, w = work in progress, ? = possibly fixed Major: ---------------------------------------------- + > pixbuf-renderer.c: + > fix two pass render from corrupting it->qd pointer (need one pointer for each queue?). + > fix image_change_from_image (to do this need a pixbuf_renderer_move_image). + > fix broken zoom out drawing when using source tiles. + + > image.c: + > need to keep a list of ImageWindows and provide function to sync options to each object. + > test and probably fix delay_flip. + > make this a g_object with signals for completed, changed, etc. + + > work on pan view: > Pick a better keyboard shortcut than Control + J :) - d> Add warning dialog that it will be slow if the standard thumbnail cache is not enabled. > Fix occasional redraw bugs when zoomed out. > Fix occasional odd requests for non-visible tiles when zoomed out (related to above?). > Fix slowness in image.c with huge grid size by changing use of pre-allocated tile array @@ -15,19 +25,19 @@ Major: w> Fix search scrolling to try to center image and info popup. > Fix info popup location to opposing horizontal side when near edge of grid. > Find something to do with middle mouse clicks. - d> Set drag and drop data to clicked image. - d> Should the copy/move/rename/delete operations be available here? - > ^ and if so, should delete key actually work? - d> searching for same item more than once should step through all matches + > should delete key actually work? > search should highlight all matching images (with toggle?) > should non-thumbnail images have a drop shadow? + > uptimize pixbuf_draw_line (line endpoints should clip to draw region before draw loop) > time line view: - d> searching by date should scroll to proper alignment with year/month/day boundaries. + > allow use of file date or EXIF (embedded) date. + > allow horizontal _or_ vertical orientation. > calendar view: w> fix search. (UPDATE: still can not find day containing name/path/partial) > supports thumbnail size images only, should grey out actual size items in size menu or something... + > allow use of file date or EXIF (embedded) date. > grid view: > allow sorting by name, date, size, dimensions, etc. @@ -35,11 +45,12 @@ Major: > flower view: > fix it :) + > under consideration: + > split view + > the info dialog is not set as a transient of the calling window, this causes it to be behind a full screen window when 'stay above other windows' is enabled. -d> use doubles instead of floats wherever possible - ------------- > update translations: ( ) ( ) ( ) diff --git a/src/Makefile.am b/src/Makefile.am index d76963f2..647d9cbd 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -111,6 +111,8 @@ gqview_SOURCES = \ menu.h \ pan-view.c \ pan-view.h \ + pixbuf-renderer.c \ + pixbuf-renderer.h \ pixbuf_util.c \ pixbuf_util.h \ preferences.c \ diff --git a/src/fullscreen.c b/src/fullscreen.c index 494a2429..1507a6c0 100644 --- a/src/fullscreen.c +++ b/src/fullscreen.c @@ -159,7 +159,7 @@ static void fullscreen_image_update_cb(ImageWindow *imd, gpointer data) FullScreenData *fs = data; if (fs->imd->il && - fs->imd->pixbuf != fs->imd->il->pixbuf) + fs->imd->il->pixbuf != image_get_pixbuf(fs->imd)) { fullscreen_mouse_set_busy_idle(fs); } @@ -314,7 +314,7 @@ FullScreenData *fullscreen_start(GtkWidget *window, ImageWindow *imd, gtk_widget_show(fs->window); /* for hiding the mouse */ - g_signal_connect(G_OBJECT(fs->imd->image), "motion_notify_event", + g_signal_connect(G_OBJECT(fs->imd->pr), "motion_notify_event", G_CALLBACK(fullscreen_mouse_moved), fs); clear_mouse_cursor(fs->window, fs->cursor_state); diff --git a/src/image-overlay.c b/src/image-overlay.c index bc689328..530dbe69 100644 --- a/src/image-overlay.c +++ b/src/image-overlay.c @@ -17,6 +17,7 @@ #include "image.h" #include "img-view.h" #include "layout.h" +#include "pixbuf-renderer.h" #include "pixbuf_util.h" @@ -126,15 +127,14 @@ static GdkPixbuf *image_overlay_info_render(ImageWindow *imd) if (imd->delay_flip && imd->il && imd->il->pixbuf && - imd->pixbuf != imd->il->pixbuf) + image_get_pixbuf(imd) != imd->il->pixbuf) { w = gdk_pixbuf_get_width(imd->il->pixbuf); h = gdk_pixbuf_get_height(imd->il->pixbuf); } else { - w = imd->image_width; - h = imd->image_height; + pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), &w, &h); } text = g_strdup_printf("%s(%d/%d) %s\n%d x %d - %s - %s", ct, @@ -146,7 +146,7 @@ static GdkPixbuf *image_overlay_info_render(ImageWindow *imd) g_free(ct); g_free(name_escaped); - layout = gtk_widget_create_pango_layout(imd->image, NULL); + layout = gtk_widget_create_pango_layout(imd->pr, NULL); pango_layout_set_markup(layout, text, -1); g_free(text); @@ -165,7 +165,7 @@ static GdkPixbuf *image_overlay_info_render(ImageWindow *imd) pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0); pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0); - pixbuf_draw_layout(pixbuf, layout, imd->image, 5, 5, 0, 0, 0, 255); + pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255); g_object_unref(G_OBJECT(layout)); @@ -189,8 +189,8 @@ static gint image_overlay_update_cb(gpointer data) image_overlay_set(ou->imd, ou->id, pixbuf, IMAGE_OVERLAY_X, IMAGE_OVERLAY_Y); g_object_unref(pixbuf); - g_object_set_data(G_OBJECT(ou->imd->image), IMAGE_OVERLAY_UPDATE_KEY, NULL); - g_signal_handler_disconnect(ou->imd->image, ou->destroy_id); + g_object_set_data(G_OBJECT(ou->imd->pr), IMAGE_OVERLAY_UPDATE_KEY, NULL); + g_signal_handler_disconnect(ou->imd->pr, ou->destroy_id); g_free(ou); return FALSE; @@ -200,16 +200,16 @@ static void image_overlay_update_schedule(ImageWindow *imd, gint id) { OverlayUpdate *ou; - ou = g_object_get_data(G_OBJECT(imd->image), IMAGE_OVERLAY_UPDATE_KEY); + ou = g_object_get_data(G_OBJECT(imd->pr), IMAGE_OVERLAY_UPDATE_KEY); if (ou) return; ou = g_new0(OverlayUpdate, 1); ou->imd = imd; ou->id = id; ou->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_overlay_update_cb, ou, NULL); - ou->destroy_id = g_signal_connect(G_OBJECT(imd->image), "destroy", + ou->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy", G_CALLBACK(image_overlay_update_destroy_cb), ou); - g_object_set_data(G_OBJECT(imd->image), IMAGE_OVERLAY_UPDATE_KEY, ou); + g_object_set_data(G_OBJECT(imd->pr), IMAGE_OVERLAY_UPDATE_KEY, ou); } void image_overlay_update(ImageWindow *imd, gint id) diff --git a/src/image.c b/src/image.c index 3b27e48a..99e3ebb6 100644 --- a/src/image.c +++ b/src/image.c @@ -1,6 +1,6 @@ /* * GQview - * (C) 2004 John Ellis + * (C) 2005 John Ellis * * Author: John Ellis * @@ -17,19 +17,13 @@ #include "image-load.h" #include "collect.h" #include "exif.h" +#include "pixbuf-renderer.h" #include "pixbuf_util.h" #include "ui_fileops.h" #include -/* size to use when breaking up image pane for rendering */ -#define IMAGE_TILE_SIZE 512 - -/* default min and max zoom */ -#define IMAGE_ZOOM_MIN -32.0 -#define IMAGE_ZOOM_MAX 32.0 - /* size of the image loader buffer (512 bytes x defined number) */ #define IMAGE_LOAD_BUFFER_COUNT 8 @@ -42,3089 +36,758 @@ /* the file size at which throttling take place */ #define IMAGE_THROTTLE_THRESHOLD 1048576 -/* distance to drag mouse to disable image flip */ -#define IMAGE_DRAG_SCROLL_THRESHHOLD 4 - -/* increase pan rate when holding down shift */ -#define IMAGE_PAN_SHIFT_MULTIPLIER 6 - -/* alpha channel checkerboard background (same as gimp) */ -#define IMAGE_ALPHA_CHECK1 0x00999999 -#define IMAGE_ALPHA_CHECK2 0x00666666 -#define IMAGE_ALPHA_CHECK_SIZE 16 - #define IMAGE_AUTO_REFRESH_TIME 3000 -/* when scaling image to below this size, use nearest pixel for scaling - * (below about 4, the other scale types become slow generating their conversion tables) - */ -#define IMAGE_MIN_SCALE_SIZE 8 - -typedef enum { - TILE_RENDER_NONE = 0, /* do nothing */ - TILE_RENDER_AREA, /* render an area of the tile */ - TILE_RENDER_ALL /* render the whole tile */ -} TileRenderType; +/* + *------------------------------------------------------------------- + * 'signals' + *------------------------------------------------------------------- + */ -typedef struct _ImageTile ImageTile; -struct _ImageTile +static void image_click_cb(PixbufRenderer *pr, GdkEventButton *event, gpointer data) { - GdkPixmap *pixmap; /* off screen buffer */ - GdkPixbuf *pixbuf; /* pixbuf area for zooming */ - gint x; /* x offset into image */ - gint y; /* y offset into image */ - gint w; /* width that is visible (may be less if at edge of image) */ - gint h; /* height '' */ - - gint blank; - -/* render_todo: (explanation) - NONE do nothing - AREA render area of tile, usually only used when loading an image - note: will jump to an ALL if render_done is not ALL. - ALL render entire tile, if never done before w/ ALL, for expose events *only* -*/ + ImageWindow *imd = data; - TileRenderType render_todo; /* what to do (see above) */ - TileRenderType render_done; /* highest that has been done before on tile */ -}; + if (imd->func_button) + { + imd->func_button(imd, event->button, event->time, + event->x, event->y, event->state, imd->data_button); + } +} -typedef struct _QueueData QueueData; -struct _QueueData +static void image_scroll_notify_cb(PixbufRenderer *pr, gpointer data) { - ImageTile *it; - gint x; - gint y; - gint w; - gint h; - gint new_data; -}; + ImageWindow *imd = data; -typedef struct _CacheData CacheData; -struct _CacheData -{ - GdkPixmap *pixmap; - GdkPixbuf *pixbuf; - ImageTile *it; - guint size; -}; + if (imd->func_scroll_notify && pr->scale) + { + imd->func_scroll_notify(imd, + (gint)((gdouble)pr->x_scroll / pr->scale), + (gint)((gdouble)pr->y_scroll / pr->scale), + (gint)((gdouble)pr->image_width - pr->vis_width / pr->scale), + (gint)((gdouble)pr->image_height - pr->vis_height / pr->scale), + imd->data_scroll_notify); + } +} -typedef struct _OverlayData OverlayData; -struct _OverlayData +static void image_update_util(ImageWindow *imd) { - gint id; - - GdkPixbuf *pixbuf; + if (imd->func_update) imd->func_update(imd, imd->data_update); +} - gint x; - gint y; - gint relative; /* x,y coordinates are relative, negative values start bottom right */ +static void image_complete_util(ImageWindow *imd, gint preload) +{ + if (imd->il && image_get_pixbuf(imd) != image_loader_get_pixbuf(imd->il)) return; - gint visible; - gint always; /* hide temporarily when scrolling */ -}; + if (debug) printf("image load completed \"%s\" (%s)\n", + (preload) ? imd->read_ahead_path : imd->image_path, + (preload) ? "preload" : "current"); -/* needed to be declared before the source_tile stuff */ -static void image_pixbuf_sync(ImageWindow *imd, gdouble zoom, gint blank, gint new); -static void image_zoom_sync(ImageWindow *imd, gdouble zoom, - gint force, gint blank, gint new, - gint center_point, gint px, gint py); -static void image_queue(ImageWindow *imd, gint x, gint y, gint w, gint h, - gint clamp, TileRenderType render, gint new_data); + if (!preload) imd->completed = TRUE; + if (imd->func_complete) imd->func_complete(imd, preload, imd->data_complete); +} -static gint util_clip_region(gint x, gint y, gint w, gint h, - gint clip_x, gint clip_y, gint clip_w, gint clip_h, - gint *rx, gint *ry, gint *rw, gint *rh); +static void image_new_util(ImageWindow *imd) +{ + if (imd->func_new) imd->func_new(imd, imd->data_new); +} /* *------------------------------------------------------------------- - * source tiles + * misc *------------------------------------------------------------------- */ -typedef struct _SourceTile SourceTile; -struct _SourceTile -{ - gint x; - gint y; - GdkPixbuf *pixbuf; - gint blank; -}; - -static void source_tile_free(SourceTile *st) +static void image_update_title(ImageWindow *imd) { - if (!st) return; - - if (st->pixbuf) gdk_pixbuf_unref(st->pixbuf); - g_free(st); -} + gchar *title = NULL; + gchar *zoom = NULL; + gchar *collection = NULL; -static void source_tile_free_all(ImageWindow *imd) -{ - GList *work; + if (!imd->top_window) return; - work = imd->source_tiles; - while (work) + if (imd->collection && collection_to_number(imd->collection) >= 0) { - SourceTile *st = work->data; - work = work->next; - - source_tile_free(st); + const gchar *name; + name = imd->collection->name; + if (!name) name = _("Untitled"); + collection = g_strdup_printf(" (Collection %s)", name); } - g_list_free(imd->source_tiles); - imd->source_tiles = NULL; -} - -static gint source_tile_visible(ImageWindow *imd, SourceTile *st) -{ - gint x, y, w, h; + if (imd->title_show_zoom) + { + gchar *buf = image_zoom_get_as_text(imd); + zoom = g_strconcat(" [", buf, "]", NULL); + g_free(buf); + } - if (!st) return FALSE; + title = g_strdup_printf("%s%s%s%s%s%s", + imd->title ? imd->title : "", + imd->image_name ? imd->image_name : "", + zoom ? zoom : "", + collection ? collection : "", + imd->image_name ? " - " : "", + imd->title_right ? imd->title_right : ""); - x = (imd->x_scroll / IMAGE_TILE_SIZE) * IMAGE_TILE_SIZE; - y = (imd->y_scroll / IMAGE_TILE_SIZE) * IMAGE_TILE_SIZE; - w = ((imd->x_scroll + imd->vis_width) / IMAGE_TILE_SIZE) * IMAGE_TILE_SIZE + IMAGE_TILE_SIZE; - h = ((imd->y_scroll + imd->vis_height) / IMAGE_TILE_SIZE) * IMAGE_TILE_SIZE + IMAGE_TILE_SIZE; + gtk_window_set_title(GTK_WINDOW(imd->top_window), title); - return !((double)st->x * imd->scale < (double)x || - (double)(st->x + imd->source_tile_width) * imd->scale > (double)w || - (double)st->y * imd->scale < (double)y || - (double)(st->y + imd->source_tile_height) * imd->scale > (double)h); + g_free(title); + g_free(zoom); + g_free(collection); } -static SourceTile *source_tile_new(ImageWindow *imd, gint x, gint y) -{ - SourceTile *st = NULL; - gint count; - - if (imd->source_tiles_cache_size < 4) imd->source_tiles_cache_size = 4; - - if (imd->source_tile_width < 1 || imd->source_tile_height < 1) - { - printf("warning: source tile size too small %d x %d\n", imd->source_tile_width, imd->source_tile_height); - return NULL; - } +/* + *------------------------------------------------------------------- + * rotation, flip, etc. + *------------------------------------------------------------------- + */ - count = g_list_length(imd->source_tiles); - if (count >= imd->source_tiles_cache_size) - { - GList *work; +static void image_alter_real(ImageWindow *imd, AlterType type, gint clamp) +{ + PixbufRenderer *pr; + GdkPixbuf *new = NULL; + gint x, y; + gint t; - work = g_list_last(imd->source_tiles); - while (work && count >= imd->source_tiles_cache_size) - { - SourceTile *needle; + pr = (PixbufRenderer *)imd->pr; - needle = work->data; - work = work->prev; + imd->delay_alter_type = ALTER_NONE; - if (!source_tile_visible(imd, needle)) - { - imd->source_tiles = g_list_remove(imd->source_tiles, needle); - - if (imd->func_tile_dispose) - { - if (debug) printf("tile dispose: %d x %d @ %d x %d\n", - needle->x, needle->y, imd->x_scroll, imd->y_scroll); - imd->func_tile_dispose(imd, needle->x, needle->y, - imd->source_tile_width, imd->source_tile_height, - needle->pixbuf, imd->data_tile); - } - - if (!st) - { - st = needle; - } - else - { - source_tile_free(needle); - } - - count--; - } - else if (debug) - { - printf("we still think %d x %d is visble\n", needle->x, needle->y); - } - } + if (!pr->pixbuf) return; - if (debug) - { - printf("cache count %d, max is %d\n", count, imd->source_tiles_cache_size); - } - } + x = pr->x_scroll + (pr->vis_width / 2); + y = pr->y_scroll + (pr->vis_height / 2); - if (!st) + switch (type) { - st = g_new0(SourceTile, 1); - st->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, - imd->source_tile_width, imd->source_tile_height); + case ALTER_ROTATE_90: + new = pixbuf_copy_rotate_90(pr->pixbuf, FALSE); + t = x; + x = pr->height - y; + y = t; + break; + case ALTER_ROTATE_90_CC: + new = pixbuf_copy_rotate_90(pr->pixbuf, TRUE); + t = x; + x = y; + y = pr->width - t; + break; + case ALTER_ROTATE_180: + new = pixbuf_copy_mirror(pr->pixbuf, TRUE, TRUE); + x = pr->width - x; + y = pr->height - y; + break; + case ALTER_MIRROR: + new = pixbuf_copy_mirror(pr->pixbuf, TRUE, FALSE); + x = pr->width - x; + break; + case ALTER_FLIP: + new = pixbuf_copy_mirror(pr->pixbuf, FALSE, TRUE); + y = pr->height - y; + break; + case ALTER_NONE: + default: + return; + break; } - st->x = (x / imd->source_tile_width) * imd->source_tile_width; - st->y = (y / imd->source_tile_height) * imd->source_tile_height; - st->blank = TRUE; + if (!new) return; - imd->source_tiles = g_list_prepend(imd->source_tiles, st); + pixbuf_renderer_set_pixbuf(pr, new, pr->zoom); + g_object_unref(new); - if (debug) + if (clamp && pr->zoom != 0.0) { - printf("tile request: %d x %d\n", st->x, st->y); - if (!source_tile_visible(imd, st)) printf("tile request for invisible tile!\n"); + pixbuf_renderer_scroll(pr, x - (pr->vis_width / 2), y - (pr->vis_height / 2)); } - - return st; } -static void image_tile_invalidate(ImageWindow *imd, gint x, gint y, gint w, gint h) +static void image_post_process(ImageWindow *imd, gint clamp) { - gint i, j; - gint x1, x2; - gint y1, y2; - GList *work; - - x1 = (gint)floor(x / imd->tile_width) * imd->tile_width; - x2 = (gint)ceil((x + w) / imd->tile_width) * imd->tile_width; - - y1 = (gint)floor(y / imd->tile_height) * imd->tile_height; - y2 = (gint)ceil((y + h) / imd->tile_height) * imd->tile_height; - - work = g_list_nth(imd->tiles, y1 / imd->tile_height * imd->tile_cols + (x1 / imd->tile_width)); - for (j = y1; j <= y2; j += imd->tile_height) + if (exif_rotate_enable && image_get_pixbuf(imd)) { - GList *tmp; - tmp = work; - for (i = x1; i <= x2; i += imd->tile_width) + ExifData *ed; + gint orientation; + + ed = exif_read(imd->image_path); + if (ed && exif_get_integer(ed, "Orientation", &orientation)) { - if (tmp) - { - ImageTile *it = tmp->data; + /* see http://jpegclub.org/exif_orientation.html + 1 2 3 4 5 6 7 8 - it->render_done = TILE_RENDER_NONE; - it->render_todo = TILE_RENDER_ALL; + 888888 888888 88 88 8888888888 88 88 8888888888 + 88 88 88 88 88 88 88 88 88 88 88 88 + 8888 8888 8888 8888 88 8888888888 8888888888 88 + 88 88 88 88 + 88 88 888888 888888 + */ - tmp = tmp->next; + switch (orientation) + { + case EXIF_ORIENTATION_TOP_LEFT: + /* normal -- nothing to do */ + break; + case EXIF_ORIENTATION_TOP_RIGHT: + /* mirrored */ + imd->delay_alter_type = ALTER_MIRROR; + break; + case EXIF_ORIENTATION_BOTTOM_RIGHT: + /* upside down */ + imd->delay_alter_type = ALTER_ROTATE_180; + break; + case EXIF_ORIENTATION_BOTTOM_LEFT: + /* flipped */ + imd->delay_alter_type = ALTER_FLIP; + break; + case EXIF_ORIENTATION_LEFT_TOP: + /* not implemented -- too wacky to fix in one step */ + break; + case EXIF_ORIENTATION_RIGHT_TOP: + /* rotated -90 (270) */ + imd->delay_alter_type = ALTER_ROTATE_90; + break; + case EXIF_ORIENTATION_RIGHT_BOTTOM: + /* not implemented -- too wacky to fix in one step */ + break; + case EXIF_ORIENTATION_LEFT_BOTTOM: + /* rotated 90 */ + imd->delay_alter_type = ALTER_ROTATE_90_CC; + break; + default: + /* The other values are out of range */ + break; } } - work = g_list_nth(work, imd->tile_cols); /* step 1 row */ + exif_free(ed); } -} - -static SourceTile *source_tile_request(ImageWindow *imd, gint x, gint y) -{ - SourceTile *st; - - st = source_tile_new(imd, x, y); - if (imd->func_tile_request && - imd->func_tile_request(imd, st->x, st->y, - imd->source_tile_width, imd->source_tile_height, st->pixbuf, imd->data_tile)) + if (imd->delay_alter_type != ALTER_NONE) { - st->blank = FALSE; + image_alter_real(imd, imd->delay_alter_type, clamp); } -#if 0 - /* fixme: somehow invalidate the new st region */ - image_queue(imd, st->x, st->y, imd->source_tile_width, imd->source_tile_height, FALSE, TILE_RENDER_AREA, TRUE); -#endif - image_tile_invalidate(imd, st->x * imd->scale, st->y * imd->scale, - imd->source_tile_width * imd->scale, imd->source_tile_height * imd->scale); - - return st; } -static SourceTile *source_tile_find(ImageWindow *imd, gint x, gint y) -{ - GList *work; +/* + *------------------------------------------------------------------- + * read ahead (prebuffer) + *------------------------------------------------------------------- + */ - work = imd->source_tiles; - while (work) - { - SourceTile *st = work->data; +static void image_read_ahead_cancel(ImageWindow *imd) +{ + if (debug) printf("read ahead cancelled for :%s\n", imd->read_ahead_path); - if (x >= st->x && x < st->x + imd->source_tile_width && - y >= st->y && y < st->y + imd->source_tile_height) - { - if (work != imd->source_tiles) - { - imd->source_tiles = g_list_remove_link(imd->source_tiles, work); - imd->source_tiles = g_list_concat(work, imd->source_tiles); - } - return st; - } + image_loader_free(imd->read_ahead_il); + imd->read_ahead_il = NULL; - work = work->next; - } + if (imd->read_ahead_pixbuf) g_object_unref(imd->read_ahead_pixbuf); + imd->read_ahead_pixbuf = NULL; - return NULL; + g_free(imd->read_ahead_path); + imd->read_ahead_path = NULL; } -static GList *source_tile_compute_region(ImageWindow *imd, gint x, gint y, gint w, gint h, gint request) +static void image_read_ahead_done_cb(ImageLoader *il, gpointer data) { - gint x1, y1; - GList *list = NULL; - gint sx, sy; - - if (x < 0) x = 0; - if (y < 0) y = 0; - if (w > imd->image_width) w = imd->image_width; - if (h > imd->image_height) h = imd->image_height; + ImageWindow *imd = data; - sx = (x / imd->source_tile_width) * imd->source_tile_width; - sy = (y / imd->source_tile_height) * imd->source_tile_height; + if (debug) printf("read ahead done for :%s\n", imd->read_ahead_path); - for (x1 = sx; x1 < x + w; x1+= imd->source_tile_width) + imd->read_ahead_pixbuf = image_loader_get_pixbuf(imd->read_ahead_il); + if (imd->read_ahead_pixbuf) { - for (y1 = sy; y1 < y + h; y1 += imd->source_tile_height) - { - SourceTile *st; - - st = source_tile_find(imd, x1, y1); - if (!st && request) st = source_tile_request(imd, x1, y1); - - if (st) list = g_list_prepend(list, st); - } + g_object_ref(imd->read_ahead_pixbuf); + } + else + { + imd->read_ahead_pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN); } + image_loader_free(imd->read_ahead_il); + imd->read_ahead_il = NULL; - return g_list_reverse(list); + image_complete_util(imd, TRUE); } -static void source_tile_changed(ImageWindow *imd, gint x, gint y, gint width, gint height) +static void image_read_ahead_error_cb(ImageLoader *il, gpointer data) { - GList *work; - - work = imd->source_tiles; - while (work) - { - SourceTile *st; - gint rx, ry, rw, rh; - - st = work->data; - work = work->next; - - if (util_clip_region(st->x, st->y, imd->source_tile_width, imd->source_tile_height, - x, y, width, height, - &rx, &ry, &rw, &rh)) - { - GdkPixbuf *pixbuf; - - pixbuf = gdk_pixbuf_new_subpixbuf(st->pixbuf, rx - st->x, ry - st->y, rw, rh); - if (imd->func_tile_request && - imd->func_tile_request(imd, rx, ry, rw, rh, pixbuf, imd->data_tile)) - { - image_tile_invalidate(imd, rx * imd->scale, ry * imd->scale, rw * imd->scale, rh * imd->scale); - } - g_object_unref(pixbuf); - } - } + /* we even treat errors as success, maybe at least some of the file was ok */ + image_read_ahead_done_cb(il, data); } - -static gint source_tile_render(ImageWindow *imd, ImageTile *it, - gint x, gint y, gint w, gint h, - gint new_data, gint fast) +static void image_read_ahead_start(ImageWindow *imd) { - GList *list; - GList *work; - gint draw = FALSE; + /* already started ? */ + if (!imd->read_ahead_path || imd->read_ahead_il || imd->read_ahead_pixbuf) return; - if (imd->zoom == 1.0 || imd->scale == 1.0) - { - list = source_tile_compute_region(imd, it->x + x, it->y + y, w, h, TRUE); - work = list; - while (work) - { - SourceTile *st; - gint rx, ry, rw, rh; + /* still loading ?, do later */ + if (imd->il) return; - st = work->data; - work = work->next; + if (debug) printf("read ahead started for :%s\n", imd->read_ahead_path); - if (util_clip_region(st->x, st->y, imd->source_tile_width, imd->source_tile_height, - it->x + x, it->y + y, w, h, - &rx, &ry, &rw, &rh)) - { - if (st->blank) - { - gdk_draw_rectangle(it->pixmap, imd->image->style->black_gc, TRUE, - rx - st->x, ry - st->y, rw, rh); - } - else /* (imd->zoom == 1.0 || imd->scale == 1.0) */ - { - gdk_draw_pixbuf(it->pixmap, - imd->image->style->fg_gc[GTK_WIDGET_STATE(imd->image)], - st->pixbuf, - rx - st->x, ry - st->y, - rx - it->x, ry - it->y, - rw, rh, - (GdkRgbDither)dither_quality, rx, ry); - } - } - } - } - else - { - double scale_x, scale_y; - gint sx, sy, sw, sh; - - if (imd->image_width == 0 || imd->image_height == 0) return FALSE; - scale_x = (double)imd->width / imd->image_width; - scale_y = (double)imd->height / imd->image_height; - - sx = (double)(it->x + x) / scale_x; - sy = (double)(it->y + y) / scale_y; - sw = (double)w / scale_x; - sh = (double)h / scale_y; - - if (imd->width < IMAGE_MIN_SCALE_SIZE || imd->height < IMAGE_MIN_SCALE_SIZE) fast = TRUE; - -#if 0 - /* draws red over draw region, to check for leaks (regions not filled) */ - pixbuf_draw_rect(it->pixbuf, x, y, w, h, 255, 0, 0, 255, FALSE); -#endif - - list = source_tile_compute_region(imd, sx, sy, sw, sh, TRUE); - work = list; - while (work) - { - SourceTile *st; - gint rx, ry, rw, rh; - gint stx, sty, stw, sth; - - st = work->data; - work = work->next; - - stx = floor((double)st->x * scale_x); - sty = floor((double)st->y * scale_y); - stw = ceil ((double)(st->x + imd->source_tile_width) * scale_x) - stx; - sth = ceil ((double)(st->y + imd->source_tile_height) * scale_y) - sty; - - if (util_clip_region(stx, sty, stw, sth, - it->x + x, it->y + y, w, h, - &rx, &ry, &rw, &rh)) - { - if (st->blank) - { - gdk_draw_rectangle(it->pixmap, imd->image->style->black_gc, TRUE, - rx - st->x, ry - st->y, rw, rh); - } - else - { - double offset_x; - double offset_y; - - /* may need to use unfloored stx,sty values here */ - offset_x = ((double)stx < (double)it->x) ? - (double)stx - (double)it->x : 0.0; - offset_y = ((double)sty < (double)it->y) ? - (double)sty - (double)it->y : 0.0; - - gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh, - (double) 0.0 + rx - it->x + offset_x, - (double) 0.0 + ry - it->y + offset_y, - scale_x, scale_y, - (fast) ? GDK_INTERP_NEAREST : (GdkInterpType)zoom_quality); - draw = TRUE; - } - } - } - } - - g_list_free(list); - - return draw; -} - -static void image_source_tile_unset(ImageWindow *imd) -{ - source_tile_free_all(imd); - - imd->source_tiles_enabled = FALSE; -} - -void image_set_image_as_tiles(ImageWindow *imd, gint width, gint height, - gint tile_width, gint tile_height, gint cache_size, - ImageTileRequestFunc func_tile_request, - ImageTileDisposeFunc func_tile_dispose, - gpointer data, - gdouble zoom) -{ - /* FIXME: unset any current image */ - image_source_tile_unset(imd); - - if (tile_width < 32 || tile_height < 32) - { - printf("warning: tile size too small %d x %d (min 32x32)\n", tile_width, tile_height); - return; - } - if (width < 32 || height < 32) - { - printf("warning: tile canvas too small %d x %d (min 32x32)\n", width, height); - return; - } - if (!func_tile_request) - { - printf("warning: tile request function is null\n"); - } - - printf("Setting source tiles to size %d x %d, grid is %d x %d\n", tile_width, tile_height, width, height); - - if (cache_size < 4) cache_size = 4; - - imd->source_tiles_enabled = TRUE; - imd->source_tiles_cache_size = cache_size; - imd->source_tile_width = tile_width; - imd->source_tile_height = tile_height; - - imd->image_width = width; - imd->image_height = height; - - imd->func_tile_request = func_tile_request; - imd->func_tile_dispose = func_tile_dispose; - imd->data_tile = data; - - image_zoom_sync(imd, zoom, TRUE, FALSE, TRUE, FALSE, 0, 0); -} - - -static void image_queue_clear(ImageWindow *imd); - -static void image_update_title(ImageWindow *imd); -static void image_update_util(ImageWindow *imd); -static void image_complete_util(ImageWindow *imd, gint preload); - -static void image_button_do(ImageWindow *imd, GdkEventButton *bevent); - -static void image_overlay_draw(ImageWindow *imd, gint x, gint y, gint w, gint h); -static void image_overlay_queue_all(ImageWindow *imd); - -static void image_scroller_timer_set(ImageWindow *imd, gint start); - - -static gint util_clip_region(gint x, gint y, gint w, gint h, - gint clip_x, gint clip_y, gint clip_w, gint clip_h, - gint *rx, gint *ry, gint *rw, gint *rh) -{ - if (clip_x + clip_w <= x || - clip_x >= x + w || - clip_y + clip_h <= y || - clip_y >= y + h) - { - return FALSE; - } - - *rx = MAX(x, clip_x); - *rw = MIN((x + w), (clip_x + clip_w)) - *rx; - - *ry = MAX(y, clip_y); - *rh = MIN((y + h), (clip_y + clip_h)) - *ry; - - return TRUE; -} - - -/* - *------------------------------------------------------------------- - * tile cache - *------------------------------------------------------------------- - */ - -static gint pixmap_calc_size(GdkPixmap *pixmap) -{ - gint w, h, d; - - d = gdk_drawable_get_depth(pixmap); - gdk_drawable_get_size(pixmap, &w, &h); - return w * h * (d / 8); -} - -static void image_tile_cache_remove(ImageWindow *imd, ImageTile *it) -{ - GList *work; - - work = imd->tile_cache; - while(work) - { - CacheData *cd = work->data; - work = work->next; - - if (cd->it == it) - { - imd->tile_cache = g_list_remove(imd->tile_cache, cd); - imd->tile_cache_size -= cd->size; - g_free(cd); - } - } -} - -static void image_tile_cache_free(ImageWindow *imd, CacheData *cd) -{ - imd->tile_cache = g_list_remove(imd->tile_cache, cd); - if (cd->pixmap) - { - g_object_unref(cd->it->pixmap); - cd->it->pixmap = NULL; - cd->it->render_done = TILE_RENDER_NONE; - } - if (cd->pixbuf) - { - gdk_pixbuf_unref(cd->it->pixbuf); - cd->it->pixbuf = NULL; - } - imd->tile_cache_size -= cd->size; - g_free(cd); -} - -static void image_tile_cache_free_space(ImageWindow *imd, gint space, ImageTile *it) -{ - GList *work; - gint tile_max; - - work = g_list_last(imd->tile_cache); - - if (imd->source_tiles_enabled && imd->scale < 1.0) - { - gint tiles; - - tiles = (imd->vis_width / IMAGE_TILE_SIZE + 1) * (imd->vis_width / IMAGE_TILE_SIZE + 1); - tile_max = MAX(tiles * IMAGE_TILE_SIZE * IMAGE_TILE_SIZE * 3, - (gint)((double)tile_cache_max * 1048576.0 * imd->scale)); - } - else - { - tile_max = tile_cache_max * 1048576; - } - - while (work && imd->tile_cache_size > 0 && imd->tile_cache_size + space > tile_max) - { - CacheData *cd = work->data; - work = work->prev; - if (cd->it != it) image_tile_cache_free(imd, cd); - } -} - -static void image_tile_cache_add(ImageWindow *imd, ImageTile *it, - GdkPixmap *pixmap, GdkPixbuf *pixbuf, guint size) -{ - CacheData *cd; - - cd = g_new(CacheData, 1); - cd->pixmap = pixmap; - cd->pixbuf = pixbuf; - cd->it = it; - cd->size = size; - - imd->tile_cache = g_list_prepend(imd->tile_cache, cd); - - imd->tile_cache_size += cd->size; -} - -static void image_tile_prepare(ImageWindow *imd, ImageTile *it) -{ - if (!it->pixmap) - { - GdkPixmap *pixmap; - guint size; - - pixmap = gdk_pixmap_new(imd->image->window, imd->tile_width, imd->tile_height, -1); - - size = pixmap_calc_size(pixmap); - image_tile_cache_free_space(imd, size, it); - - it->pixmap = pixmap; - image_tile_cache_add(imd, it, pixmap, NULL, size); - } - - if ((imd->zoom != 1.0 || imd->source_tiles_enabled || (imd->pixbuf && gdk_pixbuf_get_has_alpha(imd->pixbuf)) ) && - !it->pixbuf) - { - GdkPixbuf *pixbuf; - guint size; - - if (imd->pixbuf) - { - pixbuf = gdk_pixbuf_new(gdk_pixbuf_get_colorspace(imd->pixbuf), - gdk_pixbuf_get_has_alpha(imd->pixbuf), - gdk_pixbuf_get_bits_per_sample(imd->pixbuf), - imd->tile_width, imd->tile_height); - } - else - { - pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, imd->tile_width, imd->tile_height); - } - - size = gdk_pixbuf_get_rowstride(pixbuf) * imd->tile_height; - image_tile_cache_free_space(imd, size, it); - - it->pixbuf = pixbuf; - image_tile_cache_add(imd, it, NULL, pixbuf, size); - } -} - -/* - *------------------------------------------------------------------- - * tiles - *------------------------------------------------------------------- - */ - -static ImageTile *image_tile_new(gint w, gint h) -{ - ImageTile *it; - - it = g_new0(ImageTile, 1); - it->w = w; - it->h = h; - it->pixmap = NULL; - it->pixbuf = NULL; - it->blank = TRUE; - it->render_todo = TILE_RENDER_NONE; - it->render_done = TILE_RENDER_NONE; - - return it; -} - -static void image_tile_free(ImageTile *it) -{ - if (!it) return; - - if (it->pixbuf) gdk_pixbuf_unref(it->pixbuf); - if (it->pixmap) g_object_unref(it->pixmap); - - g_free(it); -} - -static void image_tile_sync_count(ImageWindow *imd, gint n) -{ - gint l; - - l = g_list_length(imd->tiles); - - if (l == n) return; - - if (l < n) - { - while (l < n) - { - imd->tiles = g_list_prepend(imd->tiles, image_tile_new(imd->tile_width, imd->tile_height)); - l++; - } - } - else - { - /* This should remove from the tail of the GList, but with large images there are many tiles, - * making this significantly faster for those cases. - */ - while (l > n && imd->tiles) - { - ImageTile *it = imd->tiles->data; - imd->tiles = g_list_remove(imd->tiles, it); - image_tile_cache_remove(imd, it); - image_tile_free(it); - l--; - } - } -} - -static void image_tile_sync(ImageWindow *imd, gint width, gint height, gint blank) -{ - gint rows; - gint x, y; - GList *work; - - imd->tile_cols = (width + imd->tile_width - 1) / imd->tile_width; - - rows = (height + imd->tile_height - 1) / imd->tile_height; - - image_tile_sync_count(imd, imd->tile_cols * rows); - - x = y = 0; - work = imd->tiles; - while(work) - { - ImageTile *it = work->data; - work = work->next; - - it->x = x; - it->y = y; - if (x + imd->tile_width > width) - { - it->w = width - x; - } - else - { - it->w = imd->tile_width; - } - if (y + imd->tile_height > height) - { - it->h = height - y; - } - else - { - it->h = imd->tile_height; - } - - it->blank = blank; - it->render_todo = TILE_RENDER_NONE; - it->render_done = TILE_RENDER_NONE; - - x += imd->tile_width; - if (x >= width) - { - x = 0; - y += imd->tile_height; - } - } - - /* all it's are now useless in queue */ - image_queue_clear(imd); -} - -static void image_tile_render(ImageWindow *imd, ImageTile *it, - gint x, gint y, gint w, gint h, - gint new_data, gint fast) -{ - gint has_alpha; - gint draw = FALSE; - - if (it->render_todo == TILE_RENDER_NONE && it->pixmap && !new_data) return; - - if (it->render_done != TILE_RENDER_ALL) - { - x = 0; - y = 0; - w = it->w; - h = it->h; - if (!fast) it->render_done = TILE_RENDER_ALL; - } - else if (it->render_todo != TILE_RENDER_AREA) - { - if (!fast) it->render_todo = TILE_RENDER_NONE; - return; - } - - if (!fast) it->render_todo = TILE_RENDER_NONE; - - if (new_data) it->blank = FALSE; - - image_tile_prepare(imd, it); - has_alpha = (imd->pixbuf && gdk_pixbuf_get_has_alpha(imd->pixbuf)); - - /* FIXME checker colors for alpha should be configurable, - * also should be drawn for blank = TRUE - */ - - if (it->blank) - { - /* no data, do fast rect fill */ - gdk_draw_rectangle(it->pixmap, imd->image->style->black_gc, TRUE, - 0, 0, it->w, it->h); - } - else if (imd->source_tiles_enabled) - { - draw = source_tile_render(imd, it, x, y, w, h, new_data, fast); - } - else if (imd->zoom == 1.0 || imd->scale == 1.0) - { - if (has_alpha) - { - gdk_pixbuf_composite_color(imd->pixbuf, it->pixbuf, x, y, w, h, - (double) 0.0 - it->x, - (double) 0.0 - it->y, - 1.0, 1.0, GDK_INTERP_NEAREST, - 255, it->x + x, it->y + y, - IMAGE_ALPHA_CHECK_SIZE, IMAGE_ALPHA_CHECK1, IMAGE_ALPHA_CHECK2); - draw = TRUE; - } - else - { - /* faster, simple */ - gdk_draw_pixbuf(it->pixmap, - imd->image->style->fg_gc[GTK_WIDGET_STATE(imd->image)], - imd->pixbuf, - it->x + x, it->y + y, - x, y, - w, h, - (GdkRgbDither)dither_quality, it->x + x, it->y + y); - } - } - else - { - double scale_x, scale_y; - - if (imd->image_width == 0 || imd->image_height == 0) return; - scale_x = (double)imd->width / imd->image_width; - scale_y = (double)imd->height / imd->image_height; - - /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely - * small sizes for anything but GDK_INTERP_NEAREST - */ - if (imd->width < IMAGE_MIN_SCALE_SIZE || imd->height < IMAGE_MIN_SCALE_SIZE) fast = TRUE; - - if (!has_alpha) - { - gdk_pixbuf_scale(imd->pixbuf, it->pixbuf, x, y, w, h, - (double) 0.0 - it->x, - (double) 0.0 - it->y, - scale_x, scale_y, - (fast) ? GDK_INTERP_NEAREST : (GdkInterpType)zoom_quality); - } - else - { - gdk_pixbuf_composite_color(imd->pixbuf, it->pixbuf, x, y, w, h, - (double) 0.0 - it->x, - (double) 0.0 - it->y, - scale_x, scale_y, - (fast) ? GDK_INTERP_NEAREST : (GdkInterpType)zoom_quality, - 255, it->x + x, it->y + y, - IMAGE_ALPHA_CHECK_SIZE, IMAGE_ALPHA_CHECK1, IMAGE_ALPHA_CHECK2); - } - draw = TRUE; - } - - if (draw && it->pixbuf && !it->blank) - { - gdk_draw_pixbuf(it->pixmap, - imd->image->style->fg_gc[GTK_WIDGET_STATE(imd->image)], - it->pixbuf, - x, y, - x, y, - w, h, - (GdkRgbDither)dither_quality, it->x + x, it->y + y); - } -} - -static void image_tile_expose(ImageWindow *imd, ImageTile *it, - gint x, gint y, gint w, gint h, - gint new_data, gint fast) -{ - image_tile_render(imd, it, x, y, w, h, new_data, fast); - - gdk_draw_drawable(imd->image->window, imd->image->style->fg_gc[GTK_WIDGET_STATE(imd->image)], - it->pixmap, x, y, - imd->x_offset + (it->x - imd->x_scroll) + x, imd->y_offset + (it->y - imd->y_scroll) + y, w, h); - - if (imd->overlay_list) - { - image_overlay_draw(imd, imd->x_offset + (it->x - imd->x_scroll) + x, - imd->y_offset + (it->y - imd->y_scroll) + y, - w, h); - } -} - -static gint image_tile_is_visible(ImageWindow *imd, ImageTile *it) -{ - return (it->x + it->w >= imd->x_scroll && it->x <= imd->x_scroll + imd->window_width - imd->x_offset * 2 && - it->y + it->h >= imd->y_scroll && it->y <= imd->y_scroll + imd->window_height - imd->y_offset * 2); -} - -/* - *------------------------------------------------------------------- - * render queue - *------------------------------------------------------------------- - */ - - -static gint image_queue_draw_idle_cb(gpointer data) -{ - ImageWindow *imd = data; - QueueData *qd; - gint fast; - - if ((!imd->pixbuf && !imd->source_tiles_enabled) || (!imd->draw_queue && !imd->draw_queue_2pass) || imd->draw_idle_id == -1) - { - if (!imd->completed) image_complete_util(imd, FALSE); - - imd->draw_idle_id = -1; - return FALSE; - } - - if (imd->draw_queue) - { - qd = imd->draw_queue->data; - fast = (two_pass_zoom && (GdkInterpType)zoom_quality != GDK_INTERP_NEAREST && imd->scale != 1.0); - } - else - { - if (imd->il) - { - /* still loading, wait till done (also drops the higher priority) */ - - imd->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - image_queue_draw_idle_cb, imd, NULL); - imd->draw_idle_high = FALSE; - return FALSE; - } - qd = imd->draw_queue_2pass->data; - fast = FALSE; - } - - if (GTK_WIDGET_REALIZED(imd->image)) - { - if (image_tile_is_visible(imd, qd->it)) - { - image_tile_expose(imd, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast); - } - else if (qd->new_data) - { - /* if new pixel data, and we already have a pixmap, update the tile */ - qd->it->blank = FALSE; - if (qd->it->pixmap && qd->it->render_done == TILE_RENDER_ALL) - { - image_tile_render(imd, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast); - } - } - } - - if (imd->draw_queue) - { - imd->draw_queue = g_list_remove(imd->draw_queue, qd); - if (fast) - { - imd->draw_queue_2pass = g_list_append(imd->draw_queue_2pass, qd); - } - else - { - g_free(qd); - } - } - else - { - imd->draw_queue_2pass = g_list_remove(imd->draw_queue_2pass, qd); - g_free(qd); - } - - if (!imd->draw_queue && !imd->draw_queue_2pass) - { - if (!imd->completed) image_complete_util(imd, FALSE); - - imd->draw_idle_id = -1; - return FALSE; - } - - return TRUE; -} - -static QueueData *image_queue_combine(ImageWindow *imd, QueueData *qd) -{ - QueueData *found = NULL; - GList *work; - - work = imd->draw_queue; - while (work && !found) - { - found = work->data; - work = work->next; - - if (found->it != qd->it) found = NULL; - } - - if (found) - { - if (found->x + found->w < qd->x + qd->w) found->w += (qd->x + qd->w) - (found->x + found->w); - if (found->x > qd->x) - { - found->w += found->x - qd->x; - found->x = qd->x; - } - - if (found->y + found->h < qd->y + qd->h) found->h += (qd->y + qd->h) - (found->y + found->h); - if (found->y > qd->y) - { - found->h += found->y - qd->y; - found->y = qd->y; - } - found->new_data |= qd->new_data; - } - - return found; -} - -static gint image_clamp_to_visible(ImageWindow *imd, gint *x, gint *y, gint *w, gint *h) -{ - gint nx, ny; - gint nw, nh; - gint vx, vy; - gint vw, vh; - - vw = imd->vis_width; - vh = imd->vis_height; - - vx = imd->x_scroll; - vy = imd->y_scroll; - - if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE; - - /* now clamp it */ - nx = CLAMP(*x, vx, vx + vw); - nw = CLAMP(*w - (nx - *x), 1, vw); - - ny = CLAMP(*y, vy, vy + vh); - nh = CLAMP(*h - (ny - *y), 1, vh); - - *x = nx; - *y = ny; - *w = nw; - *h = nh; - - return TRUE; -} - -static gint image_queue_to_tiles(ImageWindow *imd, gint x, gint y, gint w, gint h, - gint clamp, TileRenderType render, gint new_data) -{ - gint i, j; - gint x1, x2; - gint y1, y2; - GList *work; - - if (clamp && !image_clamp_to_visible(imd, &x, &y, &w, &h)) return FALSE; - - x1 = (gint)floor(x / imd->tile_width) * imd->tile_width; - x2 = (gint)ceil((x + w) / imd->tile_width) * imd->tile_width; - - y1 = (gint)floor(y / imd->tile_height) * imd->tile_height; - y2 = (gint)ceil((y + h) / imd->tile_height) * imd->tile_height; - - work = g_list_nth(imd->tiles, y1 / imd->tile_height * imd->tile_cols + (x1 / imd->tile_width)); - for (j = y1; j <= y2; j += imd->tile_height) - { - GList *tmp; - tmp = work; - for (i = x1; i <= x2; i += imd->tile_width) - { - if (tmp) - { - ImageTile *it = tmp->data; - QueueData *qd; - - if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) || - (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL)) - { - it->render_todo = render; - } - - qd = g_new(QueueData, 1); - qd->it = it; - qd->new_data = new_data; - - if (i < x) - { - qd->x = x - i; - } - else - { - qd->x = 0; - } - qd->w = x + w - i - qd->x; - if (qd->x + qd->w > imd->tile_width) qd->w = imd->tile_width - qd->x; - - - if (j < y) - { - qd->y = y - j; - } - else - { - qd->y = 0; - } - qd->h = y + h - j - qd->y; - if (qd->y + qd->h > imd->tile_height) qd->h = imd->tile_height - qd->y; - - if (qd->w < 1 || qd->h < 1 || /* <--- sanity checks, rare cases cause this */ - image_queue_combine(imd, qd)) - { - g_free(qd); - } - else - { - imd->draw_queue = g_list_append(imd->draw_queue, qd); - } - - tmp = tmp->next; - } - } - work = g_list_nth(work, imd->tile_cols); /* step 1 row */ - } - - return TRUE; -} - -static void image_queue(ImageWindow *imd, gint x, gint y, gint w, gint h, - gint clamp, TileRenderType render, gint new_data) -{ - gint nx, ny; - - nx = CLAMP(x, 0, imd->width - 1); - ny = CLAMP(y, 0, imd->height - 1); - w -= (nx - x); - h -= (ny - y); - w = CLAMP(w, 0, imd->width - nx); - h = CLAMP(h, 0, imd->height - ny); - if (w < 1 || h < 1) return; - - if (image_queue_to_tiles(imd, nx, ny, w, h, clamp, render, new_data) && - ((!imd->draw_queue && !imd->draw_queue_2pass) || imd->draw_idle_id == -1 || !imd->draw_idle_high)) - { - if (imd->draw_idle_id != -1) g_source_remove(imd->draw_idle_id); - imd->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, - image_queue_draw_idle_cb, imd, NULL); - imd->draw_idle_high = TRUE; - } -} - -static void image_queue_list_free(GList *list) -{ - GList *work; - - work = list; - while (work) - { - QueueData *qd; - - qd = work->data; - work = work->next; - g_free(qd); - } - - g_list_free(list); -} - -static void image_queue_clear(ImageWindow *imd) -{ - image_queue_list_free(imd->draw_queue); - imd->draw_queue = NULL; - - image_queue_list_free(imd->draw_queue_2pass); - imd->draw_queue_2pass = NULL; - - if (imd->draw_idle_id != -1) g_source_remove(imd->draw_idle_id); - imd->draw_idle_id = -1; -} - -/* - *------------------------------------------------------------------- - * core calculations - *------------------------------------------------------------------- - */ - -static gint image_top_window_sizable(ImageWindow *imd) -{ - if (!imd->top_window) return FALSE; - if (!fit_window) return FALSE; - if (!imd->top_window_sync) return FALSE; - if (!imd->image->window) return FALSE; - if (window_maximized(imd->top_window)) return FALSE; - - return TRUE; -} - -static gint image_size_top_window(ImageWindow *imd, gint w, gint h) -{ - gint ww, wh; - - if (!image_top_window_sizable(imd)) return FALSE; - - if (limit_window_size) - { - gint sw = gdk_screen_width() * max_window_size / 100; - gint sh = gdk_screen_height() * max_window_size / 100; - - if (w > sw) w = sw; - if (h > sh) h = sh; - } - - w += (imd->top_window->allocation.width - imd->image->allocation.width); - h += (imd->top_window->allocation.height - imd->image->allocation.height); - - gdk_drawable_get_size(imd->top_window->window, &ww, &wh); - if (w == ww && h == wh) return FALSE; - - gdk_window_resize(imd->top_window->window, w, h); - - return TRUE; -} - -static void image_redraw(ImageWindow *imd, gint new_data) -{ - image_queue_clear(imd); - image_queue(imd, 0, 0, imd->width, imd->height, TRUE, TILE_RENDER_ALL, new_data); -} - -static void image_border_draw(ImageWindow *imd, gint x, gint y, gint w, gint h) -{ - gint rx, ry, rw, rh; - - if (!imd->image->window) return; - - if (!imd->pixbuf && !imd->source_tiles_enabled) - { - if (util_clip_region(x, y, w, h, - 0, 0, - imd->window_width, imd->window_height, - &rx, &ry, &rw, &rh)) - { - gdk_window_clear_area(imd->image->window, rx, ry, rw, rh); - image_overlay_draw(imd, rx, ry, rw, rh); - } - return; - } - - if (imd->vis_width < imd->window_width) - { - if (imd->x_offset > 0 && - util_clip_region(x, y, w, h, - 0, 0, - imd->x_offset, imd->window_height, - &rx, &ry, &rw, &rh)) - { - gdk_window_clear_area(imd->image->window, rx, ry, rw, rh); - image_overlay_draw(imd, rx, ry, rw, rh); - } - if (imd->window_width - imd->vis_width - imd->x_offset > 0 && - util_clip_region(x, y, w, h, - imd->x_offset + imd->vis_width, 0, - imd->window_width - imd->vis_width - imd->x_offset, imd->window_height, - &rx, &ry, &rw, &rh)) - { - gdk_window_clear_area(imd->image->window, rx, ry, rw, rh); - image_overlay_draw(imd, rx, ry, rw, rh); - } - } - if (imd->vis_height < imd->window_height) - { - if (imd->y_offset > 0 && - util_clip_region(x, y, w, h, - imd->x_offset, 0, - imd->vis_width, imd->y_offset, - &rx, &ry, &rw, &rh)) - { - gdk_window_clear_area(imd->image->window, rx, ry, rw, rh); - image_overlay_draw(imd, rx, ry, rw, rh); - } - if (imd->window_height - imd->vis_height - imd->y_offset > 0 && - util_clip_region(x, y, w, h, - imd->x_offset, imd->y_offset + imd->vis_height, - imd->vis_width, imd->window_height - imd->vis_height - imd->y_offset, - &rx, &ry, &rw, &rh)) - { - gdk_window_clear_area(imd->image->window, rx, ry, rw, rh); - image_overlay_draw(imd, rx, ry, rw, rh); - } - } -} - -static void image_border_clear(ImageWindow *imd) -{ - image_border_draw(imd, 0, 0, imd->window_width, imd->window_height); -} - -static void image_scroll_notify(ImageWindow *imd) -{ - if (imd->func_scroll_notify && imd->scale) - { - imd->func_scroll_notify(imd, - (gint)((gdouble)imd->x_scroll / imd->scale), - (gint)((gdouble)imd->y_scroll / imd->scale), - (gint)((gdouble)imd->image_width - imd->vis_width / imd->scale), - (gint)((gdouble)imd->image_height - imd->vis_height / imd->scale), - imd->data_scroll_notify); - } -} - -static gint image_scroll_clamp(ImageWindow *imd) -{ - gint old_xs; - gint old_ys; - - if (imd->zoom == 0.0) - { - imd->x_scroll = 0; - imd->y_scroll = 0; - - image_scroll_notify(imd); - return FALSE; - } - - old_xs = imd->x_scroll; - old_ys = imd->y_scroll; - - if (imd->x_offset > 0) - { - imd->x_scroll = 0; - } - else - { - imd->x_scroll = CLAMP(imd->x_scroll, 0, imd->width - imd->vis_width); - } - - if (imd->y_offset > 0) - { - imd->y_scroll = 0; - } - else - { - imd->y_scroll = CLAMP(imd->y_scroll, 0, imd->height - imd->vis_height); - } - - image_scroll_notify(imd); - - return (old_xs != imd->x_scroll || old_ys != imd->y_scroll); -} - -static gint image_zoom_clamp(ImageWindow *imd, gdouble zoom, gint force, gint new) -{ - gint w, h; - gdouble scale; - - zoom = CLAMP(zoom, imd->zoom_min, imd->zoom_max); - - if (imd->zoom == zoom && !force) return FALSE; - - w = imd->image_width; - h = imd->image_height; - - if (zoom == 0.0 && !imd->pixbuf) - { - scale = 1.0; - } - else if (zoom == 0.0) - { - gint max_w; - gint max_h; - gint sizeable; - - sizeable = (new && image_top_window_sizable(imd)); - - if (sizeable) - { - max_w = gdk_screen_width(); - max_h = gdk_screen_height(); - - if (limit_window_size) - { - max_w = max_w * max_window_size / 100; - max_h = max_h * max_window_size / 100; - } - } - else - { - max_w = imd->window_width; - max_h = imd->window_height; - } - - if ((zoom_to_fit_expands && !sizeable) || w > max_w || h > max_h) - { - if ((gdouble)max_w / w > (gdouble)max_h / h) - { - scale = (gdouble)max_h / h; - h = max_h; - w = w * scale + 0.5; - if (w > max_w) w = max_w; - } - else - { - scale = (gdouble)max_w / w; - w = max_w; - h = h * scale + 0.5; - if (h > max_h) h = max_h; - } - if (w < 1) w = 1; - if (h < 1) h = 1; - } - else - { - scale = 1.0; - } - } - else if (zoom > 0.0) /* zoom orig, in */ - { - scale = zoom; - w = w * scale; - h = h * scale; - } - else /* zoom out */ - { - scale = 1.0 / (0.0 - zoom); - w = w * scale; - h = h * scale; - } - - imd->zoom = zoom; - imd->width = w; - imd->height = h; - imd->scale = scale; - - return TRUE; -} - -static gint image_size_clamp(ImageWindow *imd) -{ - gint old_vw, old_vh; - - old_vw = imd->vis_width; - old_vh = imd->vis_height; - - if (imd->width < imd->window_width) - { - imd->vis_width = imd->width; - imd->x_offset = (imd->window_width - imd->width) / 2; - } - else - { - imd->vis_width = imd->window_width; - imd->x_offset = 0; - } - - if (imd->height < imd->window_height) - { - imd->vis_height = imd->height; - imd->y_offset = (imd->window_height - imd->height) / 2; - } - else - { - imd->vis_height = imd->window_height; - imd->y_offset = 0; - } - - return (old_vw != imd->vis_width || old_vh != imd->vis_height); -} - -static void image_size_sync(ImageWindow *imd, gint new_width, gint new_height) -{ - if (imd->window_width == new_width && imd->window_height == new_height) return; - - imd->window_width = new_width; - imd->window_height = new_height; - - if (imd->zoom == 0.0) image_zoom_clamp(imd, 0.0, TRUE, FALSE); - - image_size_clamp(imd); - image_scroll_clamp(imd); - -#if 0 - gtk_widget_set_size_request(imd->image, imd->window_width, imd->window_height); -#endif - - /* ensure scroller remains visible */ - if (imd->scroller_overlay != -1) - { - gint update = FALSE; - - if (imd->scroller_x > new_width) - { - imd->scroller_x = new_width; - imd->scroller_xpos = new_width; - update = TRUE; - } - if (imd->scroller_y > new_height) - { - imd->scroller_y = new_height; - imd->scroller_ypos = new_height; - update = TRUE; - } - - if (update) - { - GdkPixbuf *pixbuf; - - if (image_overlay_get(imd, imd->scroller_overlay, &pixbuf, NULL, NULL)) - { - gint w, h; - - w = gdk_pixbuf_get_width(pixbuf); - h = gdk_pixbuf_get_height(pixbuf); - image_overlay_set(imd, imd->scroller_overlay, pixbuf, - imd->scroller_x - w / 2, imd->scroller_y - h / 2); - } - } - } - - /* clear any borders */ - image_border_clear(imd); - - image_tile_sync(imd, imd->width, imd->height, FALSE); -#if 0 - /* no longer needed? (expose event should be doing this for us) */ - image_redraw(imd, FALSE); -#endif - - if (imd->title_show_zoom) image_update_title(imd); - image_update_util(imd); -} - -/* - *------------------------------------------------------------------- - * misc - *------------------------------------------------------------------- - */ - -static void image_update_title(ImageWindow *imd) -{ - gchar *title = NULL; - gchar *zoom = NULL; - gchar *collection = NULL; - - if (!imd->top_window) return; - - if (imd->collection && collection_to_number(imd->collection) >= 0) - { - const gchar *name; - name = imd->collection->name; - if (!name) name = _("Untitled"); - collection = g_strdup_printf(" (Collection %s)", name); - } - - if (imd->title_show_zoom) - { - gchar *buf = image_zoom_get_as_text(imd); - zoom = g_strconcat(" [", buf, "]", NULL); - g_free(buf); - } - - title = g_strdup_printf("%s%s%s%s%s%s", - imd->title ? imd->title : "", - imd->image_name ? imd->image_name : "", - zoom ? zoom : "", - collection ? collection : "", - imd->image_name ? " - " : "", - imd->title_right ? imd->title_right : ""); - - gtk_window_set_title(GTK_WINDOW(imd->top_window), title); - - g_free(title); - g_free(zoom); - g_free(collection); -} - -static void image_update_util(ImageWindow *imd) -{ - if (imd->func_update) imd->func_update(imd, imd->data_update); -} - -static void image_complete_util(ImageWindow *imd, gint preload) -{ - if (imd->il && imd->pixbuf != image_loader_get_pixbuf(imd->il)) return; - - if (debug) printf("image load completed \"%s\" (%s)\n", - (preload) ? imd->read_ahead_path : imd->image_path, - (preload) ? "preload" : "current"); - - if (!preload) imd->completed = TRUE; - if (imd->func_complete) imd->func_complete(imd, preload, imd->data_complete); -} - -static void image_new_util(ImageWindow *imd) -{ - if (imd->func_new) imd->func_new(imd, imd->data_new); -} - -static void image_scroll_real(ImageWindow *imd, gint x, gint y) -{ - gint old_x, old_y; - gint x_off, y_off; - gint w, h; - - if (!imd->pixbuf && !imd->source_tiles_enabled) return; - - old_x = imd->x_scroll; - old_y = imd->y_scroll; - - imd->x_scroll += x; - imd->y_scroll += y; - - image_scroll_clamp(imd); - if (imd->x_scroll == old_x && imd->y_scroll == old_y) return; - - if (imd->overlay_list) - { - gint new_x, new_y; - - new_x = imd->x_scroll; - new_y = imd->y_scroll; - imd->x_scroll = old_x; - imd->y_scroll = old_y; - image_overlay_queue_all(imd); - imd->x_scroll = new_x; - imd->y_scroll = new_y; - } - - x_off = imd->x_scroll - old_x; - y_off = imd->y_scroll - old_y; - - w = imd->vis_width - abs(x_off); - h = imd->vis_height - abs(y_off); - - if (w < 1 || h < 1) - { - /* scrolled completely to new material */ - image_queue(imd, 0, 0, imd->width, imd->height, TRUE, TILE_RENDER_ALL, FALSE); - return; - } - else - { - gint x1, y1; - gint x2, y2; - GdkGC *gc; - - if (x_off < 0) - { - x1 = abs(x_off); - x2 = 0; - } - else - { - x1 = 0; - x2 = abs(x_off); - } - - if (y_off < 0) - { - y1 = abs(y_off); - y2 = 0; - } - else - { - y1 = 0; - y2 = abs(y_off); - } - - gc = gdk_gc_new(imd->image->window); - gdk_gc_set_exposures(gc, TRUE); - gdk_draw_drawable(imd->image->window, gc, - imd->image->window, - x2 + imd->x_offset, y2 + imd->y_offset, - x1 + imd->x_offset, y1 + imd->y_offset, w, h); - g_object_unref(gc); - - if (imd->overlay_list) - { - image_overlay_queue_all(imd); - } - - w = imd->vis_width - w; - h = imd->vis_height - h; - - if (w > 0) - { - image_queue(imd, - x_off > 0 ? imd->x_scroll + (imd->vis_width - w) : imd->x_scroll, imd->y_scroll, - w, imd->vis_height, TRUE, TILE_RENDER_ALL, FALSE); - } - if (h > 0) - { - /* FIXME, to optimize this, remove overlap */ - image_queue(imd, - imd->x_scroll, y_off > 0 ? imd->y_scroll + (imd->vis_height - h) : imd->y_scroll, - imd->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE); - } - } -} - -static void widget_set_cursor(GtkWidget *widget, gint icon) -{ - GdkCursor *cursor; - - if (!widget->window) return; - - if (icon == -1) - { - cursor = NULL; - } - else - { - cursor = gdk_cursor_new (icon); - } - - gdk_window_set_cursor(widget->window, cursor); - - if (cursor) gdk_cursor_unref(cursor); -} - -/* - *------------------------------------------------------------------- - * image pixbuf handling - *------------------------------------------------------------------- - */ - -static void image_zoom_sync(ImageWindow *imd, gdouble zoom, - gint force, gint blank, gint new, - gint center_point, gint px, gint py) -{ - gdouble old_scale; - gint old_cx, old_cy; - gint clamped; - gint sized; - - old_scale = imd->scale; - if (center_point) - { - px = CLAMP(px, 0, imd->width); - py = CLAMP(py, 0, imd->height); - old_cx = imd->x_scroll + (px - imd->x_offset); - old_cy = imd->y_scroll + (py - imd->y_offset); - } - else - { - px = py = 0; - old_cx = imd->x_scroll + imd->vis_width / 2; - old_cy = imd->y_scroll + imd->vis_height / 2; - } - - if (!image_zoom_clamp(imd, zoom, force, new)) return; - - clamped = image_size_clamp(imd); - sized = image_size_top_window(imd, imd->width, imd->height); - - if (force) - { - /* force means new image, so update scroll offset per options */ - switch (scroll_reset_method) - { - case SCROLL_RESET_NOCHANGE: - /* maintain old scroll position, do nothing */ - break; - case SCROLL_RESET_CENTER: - /* center new image */ - imd->x_scroll = ((double)imd->image_width / 2.0 * imd->scale) - imd->vis_width / 2; - imd->y_scroll = ((double)imd->image_height / 2.0 * imd->scale) - imd->vis_height / 2; - break; - case SCROLL_RESET_TOPLEFT: - default: - /* reset to upper left */ - imd->x_scroll = 0; - imd->y_scroll = 0; - break; - } - } - else - { - /* user zoom does not force, so keep visible center point */ - if (center_point) - { - imd->x_scroll = old_cx / old_scale * imd->scale - (px - imd->x_offset); - imd->y_scroll = old_cy / old_scale * imd->scale - (py - imd->y_offset); - } - else - { - imd->x_scroll = old_cx / old_scale * imd->scale - (imd->vis_width / 2); - imd->y_scroll = old_cy / old_scale * imd->scale - (imd->vis_height / 2); - } - } - image_scroll_clamp(imd); - - image_tile_sync(imd, imd->width, imd->height, blank); - - /* If the window was not sized, redraw the image - we know there will be no size/expose signal. - * But even if a size is claimed, there is no guarantee that the window manager will allow it, - * so redraw the window anyway :/ - */ - if (sized || clamped) image_border_clear(imd); - image_redraw(imd, FALSE); - - if (imd->title_show_zoom) image_update_title(imd); - image_update_util(imd); -} - -static void image_pixbuf_sync(ImageWindow *imd, gdouble zoom, gint blank, gint new) -{ - if (!imd->pixbuf) - { - /* no pixbuf so just clear the window */ - imd->image_width = 0; - imd->image_height = 0; - imd->scale = 1.0; - - if (imd->image->window) - { - gdk_window_clear(imd->image->window); - image_overlay_draw(imd, 0, 0, imd->window_width, imd->window_height); - } - - image_update_util(imd); - - return; - } - - imd->image_width = gdk_pixbuf_get_width(imd->pixbuf); - imd->image_height = gdk_pixbuf_get_height(imd->pixbuf); - -#if 0 - /* reset scrolling */ - imd->x_scroll = 0; - imd->y_scroll = 0; -#endif - - image_zoom_sync(imd, zoom, TRUE, blank, new, FALSE, 0, 0); -} - -static void image_set_pixbuf(ImageWindow *imd, GdkPixbuf *pixbuf, gdouble zoom, gint new) -{ - if (pixbuf) g_object_ref(pixbuf); - if (imd->pixbuf) g_object_unref(imd->pixbuf); - imd->pixbuf = pixbuf; - - image_pixbuf_sync(imd, zoom, FALSE, new); -} - -static void image_alter_real(ImageWindow *imd, AlterType type, gint clamp) -{ - GdkPixbuf *new = NULL; - gint x, y; - gint t; - - imd->delay_alter_type = ALTER_NONE; - - if (!imd->pixbuf) return; - - x = imd->x_scroll + (imd->vis_width / 2); - y = imd->y_scroll + (imd->vis_height / 2); - - switch (type) - { - case ALTER_ROTATE_90: - new = pixbuf_copy_rotate_90(imd->pixbuf, FALSE); - t = x; - x = imd->height - y; - y = t; - break; - case ALTER_ROTATE_90_CC: - new = pixbuf_copy_rotate_90(imd->pixbuf, TRUE); - t = x; - x = y; - y = imd->width - t; - break; - case ALTER_ROTATE_180: - new = pixbuf_copy_mirror(imd->pixbuf, TRUE, TRUE); - x = imd->width - x; - y = imd->height - y; - break; - case ALTER_MIRROR: - new = pixbuf_copy_mirror(imd->pixbuf, TRUE, FALSE); - x = imd->width - x; - break; - case ALTER_FLIP: - new = pixbuf_copy_mirror(imd->pixbuf, FALSE, TRUE); - y = imd->height - y; - break; - case ALTER_NONE: - default: - return; - break; - } - - if (!new) return; - - if (clamp) - { - image_set_pixbuf(imd, new, imd->zoom, TRUE); - g_object_unref(new); - - if (imd->zoom != 0.0) - { - image_scroll(imd, x - (imd->vis_width / 2), y - (imd->vis_height / 2)); - } - } - else - { - g_object_unref(imd->pixbuf); - imd->pixbuf = new; - } -} - -static void image_post_process(ImageWindow *imd, gint clamp) -{ - if (exif_rotate_enable && imd->pixbuf) - { - ExifData *ed; - gint orientation; - - ed = exif_read(imd->image_path); - if (ed && exif_get_integer(ed, "Orientation", &orientation)) - { - /* see http://jpegclub.org/exif_orientation.html - 1 2 3 4 5 6 7 8 - - 888888 888888 88 88 8888888888 88 88 8888888888 - 88 88 88 88 88 88 88 88 88 88 88 88 - 8888 8888 8888 8888 88 8888888888 8888888888 88 - 88 88 88 88 - 88 88 888888 888888 - */ - - switch (orientation) - { - case EXIF_ORIENTATION_TOP_LEFT: - /* normal -- nothing to do */ - break; - case EXIF_ORIENTATION_TOP_RIGHT: - /* mirrored */ - imd->delay_alter_type = ALTER_MIRROR; - break; - case EXIF_ORIENTATION_BOTTOM_RIGHT: - /* upside down */ - imd->delay_alter_type = ALTER_ROTATE_180; - break; - case EXIF_ORIENTATION_BOTTOM_LEFT: - /* flipped */ - imd->delay_alter_type = ALTER_FLIP; - break; - case EXIF_ORIENTATION_LEFT_TOP: - /* not implemented -- too wacky to fix in one step */ - break; - case EXIF_ORIENTATION_RIGHT_TOP: - /* rotated -90 (270) */ - imd->delay_alter_type = ALTER_ROTATE_90; - break; - case EXIF_ORIENTATION_RIGHT_BOTTOM: - /* not implemented -- too wacky to fix in one step */ - break; - case EXIF_ORIENTATION_LEFT_BOTTOM: - /* rotated 90 */ - imd->delay_alter_type = ALTER_ROTATE_90_CC; - break; - default: - /* The other values are out of range */ - break; - } - } - exif_free(ed); - } - - if (imd->delay_alter_type != ALTER_NONE) - { - image_alter_real(imd, imd->delay_alter_type, clamp); - } -} - -/* - *------------------------------------------------------------------- - * read ahead (prebuffer) - *------------------------------------------------------------------- - */ - -static void image_read_ahead_cancel(ImageWindow *imd) -{ - if (debug) printf("read ahead cancelled for :%s\n", imd->read_ahead_path); - - image_loader_free(imd->read_ahead_il); - imd->read_ahead_il = NULL; - - if (imd->read_ahead_pixbuf) g_object_unref(imd->read_ahead_pixbuf); - imd->read_ahead_pixbuf = NULL; - - g_free(imd->read_ahead_path); - imd->read_ahead_path = NULL; -} - -static void image_read_ahead_done_cb(ImageLoader *il, gpointer data) -{ - ImageWindow *imd = data; - - if (debug) printf("read ahead done for :%s\n", imd->read_ahead_path); - - imd->read_ahead_pixbuf = image_loader_get_pixbuf(imd->read_ahead_il); - if (imd->read_ahead_pixbuf) - { - g_object_ref(imd->read_ahead_pixbuf); - } - else - { - imd->read_ahead_pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN); - } - image_loader_free(imd->read_ahead_il); - imd->read_ahead_il = NULL; - - image_complete_util(imd, TRUE); -} - -static void image_read_ahead_error_cb(ImageLoader *il, gpointer data) -{ - /* we even treat errors as success, maybe at least some of the file was ok */ - image_read_ahead_done_cb(il, data); -} - -static void image_read_ahead_start(ImageWindow *imd) -{ - /* already started ? */ - if (!imd->read_ahead_path || imd->read_ahead_il || imd->read_ahead_pixbuf) return; - - /* still loading ?, do later */ - if (imd->il) return; - - if (debug) printf("read ahead started for :%s\n", imd->read_ahead_path); - - imd->read_ahead_il = image_loader_new(imd->read_ahead_path); - - image_loader_set_error_func(imd->read_ahead_il, image_read_ahead_error_cb, imd); - if (!image_loader_start(imd->read_ahead_il, image_read_ahead_done_cb, imd)) - { - image_read_ahead_cancel(imd); - image_complete_util(imd, TRUE); - } -} - -static void image_read_ahead_set(ImageWindow *imd, const gchar *path) -{ - if (imd->read_ahead_path && path && strcmp(imd->read_ahead_path, path) == 0) return; - - image_read_ahead_cancel(imd); - - imd->read_ahead_path = g_strdup(path); - - if (debug) printf("read ahead set to :%s\n", imd->read_ahead_path); - - image_read_ahead_start(imd); -} - -/* - *------------------------------------------------------------------- - * post buffering - *------------------------------------------------------------------- - */ - -static void image_post_buffer_set(ImageWindow *imd, const gchar *path, GdkPixbuf *pixbuf) -{ - g_free(imd->prev_path); - if (imd->prev_pixbuf) g_object_unref(imd->prev_pixbuf); - - if (path && pixbuf) - { - imd->prev_path = g_strdup(path); - - g_object_ref(pixbuf); - imd->prev_pixbuf = pixbuf; - } - else - { - imd->prev_path = NULL; - imd->prev_pixbuf = NULL; - } - - if (debug) printf("post buffer set: %s\n", path); -} - -static gint image_post_buffer_get(ImageWindow *imd) -{ - gint success; - - if (imd->prev_pixbuf && - imd->image_path && imd->prev_path && strcmp(imd->image_path, imd->prev_path) == 0) - { - if (imd->pixbuf) g_object_unref(imd->pixbuf); - imd->pixbuf = imd->prev_pixbuf; - success = TRUE; - } - else - { - if (imd->prev_pixbuf) g_object_unref(imd->prev_pixbuf); - success = FALSE; - } - - imd->prev_pixbuf = NULL; - - g_free(imd->prev_path); - imd->prev_path = NULL; - - return success; -} - -/* - *------------------------------------------------------------------- - * loading - *------------------------------------------------------------------- - */ - -static void image_load_pixbuf_ready(ImageWindow *imd) -{ - if (imd->pixbuf || !imd->il) return; - - imd->pixbuf = image_loader_get_pixbuf(imd->il); - - if (imd->pixbuf) g_object_ref(imd->pixbuf); - - image_pixbuf_sync(imd, imd->zoom, TRUE, TRUE); -} - -static void image_load_area_cb(ImageLoader *il, guint x, guint y, guint w, guint h, gpointer data) -{ - ImageWindow *imd = data; - - if (imd->delay_flip && - imd->pixbuf != image_loader_get_pixbuf(il)) - { - return; - } - - if (!imd->pixbuf) image_load_pixbuf_ready(imd); - - if (imd->scale != 1.0) - { - x = (guint) floor((double)x * imd->scale); - y = (guint) floor((double)y * imd->scale); - w = (guint) ceil((double)w * imd->scale); - h = (guint) ceil((double)h * imd->scale); - - if (w == 0) w = 1; - if (h == 0) h = 1; - - if ((GdkInterpType)zoom_quality != GDK_INTERP_NEAREST) - { - /* some scaling types use surrounding pixels to smooth the image, - * this will expand the new area to cover up for faint black - * lines caused by previous renders with non-complete image - */ - y -= 1; - h += 2; - } - - } - - image_queue(imd, (gint) x, (gint) y, (gint) w, (gint) h, FALSE, TILE_RENDER_AREA, TRUE); -} - -static void image_load_done_cb(ImageLoader *il, gpointer data) -{ - ImageWindow *imd = data; - - if (debug) printf ("image done\n"); - - if (imd->delay_flip && - imd->pixbuf != image_loader_get_pixbuf(imd->il)) - { - if (imd->pixbuf) g_object_unref(imd->pixbuf); - imd->pixbuf = image_loader_get_pixbuf(imd->il); - if (imd->pixbuf) g_object_ref(imd->pixbuf); - image_pixbuf_sync(imd, imd->zoom, FALSE, TRUE); - } - - image_loader_free(imd->il); - imd->il = NULL; - - image_post_process(imd, TRUE); - - image_read_ahead_start(imd); -} - -static void image_load_error_cb(ImageLoader *il, gpointer data) -{ - if (debug) printf ("image error\n"); - - /* even on error handle it like it was done, - * since we have a pixbuf with _something_ */ - - image_load_done_cb(il, data); -} - -#ifdef IMAGE_THROTTLE_LARGER_IMAGES -static void image_load_buffer_throttle(ImageLoader *il) -{ - if (!il || il->bytes_total < IMAGE_THROTTLE_THRESHOLD) return; - - /* Larger image files usually have larger chunks of data per pixel... - * So increase the buffer read size so that the rendering chunks called - * are also larger. - */ - - image_loader_set_buffer_size(il, IMAGE_LOAD_BUFFER_COUNT * IMAGE_THROTTLE_FACTOR); -} -#endif - -/* this read ahead is located here merely for the callbacks, above */ - -static gint image_read_ahead_check(ImageWindow *imd) -{ - if (!imd->read_ahead_path) return FALSE; - if (imd->il) return FALSE; - - if (!imd->image_path || strcmp(imd->read_ahead_path, imd->image_path) != 0) - { - image_read_ahead_cancel(imd); - return FALSE; - } - - if (imd->read_ahead_il) - { - imd->il = imd->read_ahead_il; - imd->read_ahead_il = NULL; - - /* override the old signals */ - image_loader_set_area_ready_func(imd->il, image_load_area_cb, imd); - image_loader_set_error_func(imd->il, image_load_error_cb, imd); - image_loader_set_buffer_size(imd->il, IMAGE_LOAD_BUFFER_COUNT); - -#ifdef IMAGE_THROTTLE_LARGER_IMAGES - image_load_buffer_throttle(imd->il); -#endif - - /* do this one directly (probably should add a set func) */ - imd->il->func_done = image_load_done_cb; - - if (!imd->delay_flip) - { - if (imd->pixbuf) g_object_unref(imd->pixbuf); - imd->pixbuf = image_loader_get_pixbuf(imd->il); - if (imd->pixbuf) g_object_ref(imd->pixbuf); - } + imd->read_ahead_il = image_loader_new(imd->read_ahead_path); - image_read_ahead_cancel(imd); - return TRUE; - } - else if (imd->read_ahead_pixbuf) + image_loader_set_error_func(imd->read_ahead_il, image_read_ahead_error_cb, imd); + if (!image_loader_start(imd->read_ahead_il, image_read_ahead_done_cb, imd)) { - if (imd->pixbuf) g_object_unref(imd->pixbuf); - imd->pixbuf = imd->read_ahead_pixbuf; - imd->read_ahead_pixbuf = NULL; - image_read_ahead_cancel(imd); + image_complete_util(imd, TRUE); + } +} - image_post_process(imd, FALSE); - return TRUE; - } - - image_read_ahead_cancel(imd); - return FALSE; -} - -static gint image_load_begin(ImageWindow *imd, const gchar *path) -{ - if (debug) printf ("image begin \n"); - - if (imd->il) return FALSE; - - imd->completed = FALSE; - - if (image_post_buffer_get(imd)) - { - if (debug) printf("from post buffer: %s\n", imd->image_path); - - image_pixbuf_sync(imd, imd->zoom, FALSE, TRUE); - return TRUE; - } - - if (image_read_ahead_check(imd)) - { - if (debug) printf("from read ahead buffer: %s\n", imd->image_path); - - if (!imd->delay_flip || !imd->il) image_pixbuf_sync(imd, imd->zoom, FALSE, TRUE); - return TRUE; - } - - if (!imd->delay_flip && imd->pixbuf) - { - g_object_unref(imd->pixbuf); - imd->pixbuf = NULL; - } - - imd->il = image_loader_new(path); - - image_loader_set_area_ready_func(imd->il, image_load_area_cb, imd); - image_loader_set_error_func(imd->il, image_load_error_cb, imd); - image_loader_set_buffer_size(imd->il, IMAGE_LOAD_BUFFER_COUNT); - - if (!image_loader_start(imd->il, image_load_done_cb, imd)) - { - if (debug) printf("image start error\n"); - - image_loader_free(imd->il); - imd->il = NULL; - - image_complete_util(imd, FALSE); - - return FALSE; - } - -#ifdef IMAGE_THROTTLE_LARGER_IMAGES - image_load_buffer_throttle(imd->il); -#endif - - if (!imd->delay_flip && !imd->pixbuf) image_load_pixbuf_ready(imd); - - return TRUE; -} - -static void image_reset(ImageWindow *imd) -{ - /* stops anything currently being done */ - - if (debug) printf("image reset\n"); - - image_loader_free(imd->il); - imd->il = NULL; - - image_queue_clear(imd); - imd->delay_alter_type = ALTER_NONE; -} - -/* - *------------------------------------------------------------------- - * image changer - *------------------------------------------------------------------- - */ - -static void image_change_complete(ImageWindow *imd, gdouble zoom, gint new) -{ - gint sync = TRUE; - - image_source_tile_unset(imd); - - imd->zoom = zoom; /* store the zoom, needed by the loader */ - - image_reset(imd); - - if (imd->image_path && isfile(imd->image_path)) - { - if (image_load_begin(imd, imd->image_path)) - { - imd->unknown = FALSE; - sync = FALSE; - } - else - { - if (imd->pixbuf) g_object_unref(imd->pixbuf); - imd->pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN); - imd->unknown = TRUE; - } - imd->size = filesize(imd->image_path); - imd->mtime = filetime(imd->image_path); - } - else - { - if (imd->pixbuf) g_object_unref(imd->pixbuf); - imd->pixbuf = NULL; - - if (imd->image_path) - { - imd->pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN); - imd->mtime = filetime(imd->image_path); - } - else - { - imd->pixbuf = NULL; - imd->mtime = 0; - } - imd->unknown = TRUE; - imd->size = 0; - } - - if (sync) - { - image_pixbuf_sync(imd, zoom, FALSE, new); - } - else - { - image_update_util(imd); - } -} - -static void image_change_real(ImageWindow *imd, const gchar *path, - CollectionData *cd, CollectInfo *info, gdouble zoom) -{ - GdkPixbuf *prev_pixbuf = NULL; - gchar *prev_path = NULL; - gint prev_clear = FALSE; - - imd->collection = cd; - imd->collection_info = info; - - if (enable_read_ahead && imd->image_path && imd->pixbuf) - { - if (imd->il) - { - /* current image is not finished */ - prev_clear = TRUE; - } - else - { - prev_path = g_strdup(imd->image_path); - prev_pixbuf = imd->pixbuf; - g_object_ref(prev_pixbuf); - } - } - - g_free(imd->image_path); - imd->image_path = g_strdup(path); - imd->image_name = filename_from_path(imd->image_path); - - image_change_complete(imd, zoom, TRUE); - - if (prev_pixbuf) - { - image_post_buffer_set(imd, prev_path, prev_pixbuf); - g_free(prev_path); - g_object_unref(prev_pixbuf); - } - else if (prev_clear) - { - image_post_buffer_set(imd, NULL, NULL); - } - - image_update_title(imd); - image_new_util(imd); -} - -/* - *------------------------------------------------------------------- - * callbacks - *------------------------------------------------------------------- - */ - -static gint image_expose_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data) -{ - gint x, y; - - ImageWindow *imd = data; - - image_border_draw(imd, event->area.x, event->area.y, - event->area.width, event->area.height); - - /* image */ - x = MAX(0, (gint)event->area.x - imd->x_offset + imd->x_scroll); - y = MAX(0, (gint)event->area.y - imd->y_offset + imd->y_scroll); +static void image_read_ahead_set(ImageWindow *imd, const gchar *path) +{ + if (imd->read_ahead_path && path && strcmp(imd->read_ahead_path, path) == 0) return; - image_queue(imd, x, y, - MIN((gint)event->area.width, imd->width - x), - MIN((gint)event->area.height, imd->height - y), - FALSE, TILE_RENDER_ALL, FALSE); + image_read_ahead_cancel(imd); - return TRUE; -} + imd->read_ahead_path = g_strdup(path); -static void image_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data) -{ - ImageWindow *imd = data; + if (debug) printf("read ahead set to :%s\n", imd->read_ahead_path); - image_size_sync(imd, allocation->width, allocation->height); + image_read_ahead_start(imd); } /* *------------------------------------------------------------------- - * focus stuff + * post buffering *------------------------------------------------------------------- */ -static void image_focus_paint(ImageWindow *imd, gint has_focus, GdkRectangle *area) +static void image_post_buffer_set(ImageWindow *imd, const gchar *path, GdkPixbuf *pixbuf) { - GtkWidget *widget; - - widget = imd->widget; - if (!widget->window) return; + g_free(imd->prev_path); + if (imd->prev_pixbuf) g_object_unref(imd->prev_pixbuf); - if (has_focus) + if (path && pixbuf) { - gtk_paint_focus (widget->style, widget->window, GTK_STATE_ACTIVE, - area, widget, "image_window", - widget->allocation.x, widget->allocation.y, - widget->allocation.width - 1, widget->allocation.height - 1); + imd->prev_path = g_strdup(path); + + g_object_ref(pixbuf); + imd->prev_pixbuf = pixbuf; } else { - gtk_paint_shadow (widget->style, widget->window, GTK_STATE_NORMAL, GTK_SHADOW_IN, - area, widget, "image_window", - widget->allocation.x, widget->allocation.y, - widget->allocation.width - 1, widget->allocation.height - 1); + imd->prev_path = NULL; + imd->prev_pixbuf = NULL; } -} - -static gint image_focus_expose(GtkWidget *widget, GdkEventExpose *event, gpointer data) -{ - ImageWindow *imd = data; - image_focus_paint(imd, GTK_WIDGET_HAS_FOCUS(widget), &event->area); - return TRUE; + if (debug) printf("post buffer set: %s\n", path); } -static gint image_focus_in_cb(GtkWidget *widget, GdkEventFocus *event, gpointer data) +static gint image_post_buffer_get(ImageWindow *imd) { - ImageWindow *imd = data; - - GTK_WIDGET_SET_FLAGS(imd->widget, GTK_HAS_FOCUS); - image_focus_paint(imd, TRUE, NULL); + gint success; - return TRUE; -} + if (imd->prev_pixbuf && + imd->image_path && imd->prev_path && strcmp(imd->image_path, imd->prev_path) == 0) + { + image_change_pixbuf(imd, imd->prev_pixbuf, image_zoom_get(imd)); + success = TRUE; + } + else + { + success = FALSE; + } -static gint image_focus_out_cb(GtkWidget *widget, GdkEventFocus *event, gpointer data) -{ - ImageWindow *imd = data; + if (imd->prev_pixbuf) g_object_unref(imd->prev_pixbuf); + imd->prev_pixbuf = NULL; - GTK_WIDGET_UNSET_FLAGS(imd->widget, GTK_HAS_FOCUS); - image_focus_paint(imd, FALSE, NULL); + g_free(imd->prev_path); + imd->prev_path = NULL; - return TRUE; + return success; } - /* *------------------------------------------------------------------- - * overlays + * loading *------------------------------------------------------------------- */ -static void image_overlay_draw(ImageWindow *imd, gint x, gint y, gint w, gint h) +static void image_load_pixbuf_ready(ImageWindow *imd) { - GList *work; + if (image_get_pixbuf(imd) || !imd->il) return; - work = imd->overlay_list; - while (work) - { - OverlayData *od; - gint px, py, pw, ph; - gint rx, ry, rw, rh; + image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd)); +} - od = work->data; - work = work->next; +static void image_load_area_cb(ImageLoader *il, guint x, guint y, guint w, guint h, gpointer data) +{ + ImageWindow *imd = data; + PixbufRenderer *pr; - if (!od->visible) continue; + pr = (PixbufRenderer *)imd->pr; - pw = gdk_pixbuf_get_width(od->pixbuf); - ph = gdk_pixbuf_get_height(od->pixbuf); - px = od->x; - py = od->y; + if (imd->delay_flip && + pr->pixbuf != image_loader_get_pixbuf(il)) + { + return; + } - if (od->relative) - { - if (px < 0) px = imd->window_width - pw + px; - if (py < 0) py = imd->window_height - ph + py; - } + if (!pr->pixbuf) image_load_pixbuf_ready(imd); - if (util_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh)) - { - gdk_draw_pixbuf(imd->image->window, - imd->image->style->fg_gc[GTK_WIDGET_STATE(imd->image)], - od->pixbuf, - rx - px, ry - py, - rx, ry, rw, rh, - (GdkRgbDither)dither_quality, rx, ry); - } - } + pixbuf_renderer_area_changed(pr, x, y, w, h); } -static void image_overlay_queue_draw(ImageWindow *imd, OverlayData *od, gint hidden) +static void image_load_done_cb(ImageLoader *il, gpointer data) { - gint x, y, w, h; - gint old_vis; + ImageWindow *imd = data; - w = gdk_pixbuf_get_width(od->pixbuf); - h = gdk_pixbuf_get_height(od->pixbuf); - x = od->x; - y = od->y; + if (debug) printf ("image done\n"); + + g_object_set(G_OBJECT(imd->pr), "loading", FALSE, NULL); - if (od->relative) + if (imd->delay_flip && + image_get_pixbuf(imd) != image_loader_get_pixbuf(imd->il)) { - if (x < 0) x = imd->window_width - w + x; - if (y < 0) y = imd->window_height - h + y; + image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd)); } - image_queue(imd, imd->x_scroll - imd->x_offset + x, - imd->y_scroll - imd->y_offset + y, - w, h, - FALSE, TILE_RENDER_ALL, FALSE); + image_loader_free(imd->il); + imd->il = NULL; + + image_post_process(imd, TRUE); - old_vis = od->visible; - if (hidden) od->visible = FALSE; - image_border_draw(imd, x, y, w, h); - od->visible = old_vis; + image_read_ahead_start(imd); } -static void image_overlay_queue_all(ImageWindow *imd) +static void image_load_error_cb(ImageLoader *il, gpointer data) { - GList *work; + if (debug) printf ("image error\n"); - work = imd->overlay_list; - while (work) - { - OverlayData *od = work->data; - work = work->next; + /* even on error handle it like it was done, + * since we have a pixbuf with _something_ */ - image_overlay_queue_draw(imd, od, FALSE); - } + image_load_done_cb(il, data); } -static OverlayData *image_overlay_find(ImageWindow *imd, gint id) +#ifdef IMAGE_THROTTLE_LARGER_IMAGES +static void image_load_buffer_throttle(ImageLoader *il) { - GList *work; - - work = imd->overlay_list; - while (work) - { - OverlayData *od = work->data; - work = work->next; + if (!il || il->bytes_total < IMAGE_THROTTLE_THRESHOLD) return; - if (od->id == id) return od; - } + /* Larger image files usually have larger chunks of data per pixel... + * So increase the buffer read size so that the rendering chunks called + * are also larger. + */ - return NULL; + image_loader_set_buffer_size(il, IMAGE_LOAD_BUFFER_COUNT * IMAGE_THROTTLE_FACTOR); } +#endif -gint image_overlay_add(ImageWindow *imd, GdkPixbuf *pixbuf, gint x, gint y, - gint relative, gint always) -{ - OverlayData *od; - gint id; +/* this read ahead is located here merely for the callbacks, above */ - if (!imd || !pixbuf) return -1; +static gint image_read_ahead_check(ImageWindow *imd) +{ + if (!imd->read_ahead_path) return FALSE; + if (imd->il) return FALSE; - id = 1; - while (image_overlay_find(imd, id)) id++; + if (!imd->image_path || strcmp(imd->read_ahead_path, imd->image_path) != 0) + { + image_read_ahead_cancel(imd); + return FALSE; + } - od = g_new0(OverlayData, 1); - od->id = id; - od->pixbuf = pixbuf; - g_object_ref(G_OBJECT(od->pixbuf)); - od->x = x; - od->y = y; - od->relative = relative; - od->visible = TRUE; - od->always = always; + if (imd->read_ahead_il) + { + imd->il = imd->read_ahead_il; + imd->read_ahead_il = NULL; - imd->overlay_list = g_list_append(imd->overlay_list, od); + /* override the old signals */ + image_loader_set_area_ready_func(imd->il, image_load_area_cb, imd); + image_loader_set_error_func(imd->il, image_load_error_cb, imd); + image_loader_set_buffer_size(imd->il, IMAGE_LOAD_BUFFER_COUNT); - image_overlay_queue_draw(imd, od, FALSE); +#ifdef IMAGE_THROTTLE_LARGER_IMAGES + image_load_buffer_throttle(imd->il); +#endif - return od->id; -} + /* do this one directly (probably should add a set func) */ + imd->il->func_done = image_load_done_cb; -static void image_overlay_free(ImageWindow *imd, OverlayData *od) -{ - imd->overlay_list = g_list_remove(imd->overlay_list, od); + g_object_set(G_OBJECT(imd->pr), "loading", TRUE, NULL); - if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf)); - g_free(od); -} + if (!imd->delay_flip) + { + image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd)); + } -static void image_overlay_list_clear(ImageWindow *imd) -{ - while (imd->overlay_list) + image_read_ahead_cancel(imd); + return TRUE; + } + else if (imd->read_ahead_pixbuf) { - OverlayData *od; + image_change_pixbuf(imd, imd->read_ahead_pixbuf, image_zoom_get(imd)); + g_object_unref(imd->read_ahead_pixbuf); + imd->read_ahead_pixbuf = NULL; + + image_read_ahead_cancel(imd); - od = imd->overlay_list->data; - image_overlay_free(imd, od); + image_post_process(imd, FALSE); + return TRUE; } + + image_read_ahead_cancel(imd); + return FALSE; } -void image_overlay_set(ImageWindow *imd, gint id, GdkPixbuf *pixbuf, gint x, gint y) +static gint image_load_begin(ImageWindow *imd, const gchar *path) { - OverlayData *od; + if (debug) printf ("image begin \n"); - if (!imd) return; + if (imd->il) return FALSE; - od = image_overlay_find(imd, id); - if (!od) return; + imd->completed = FALSE; - if (pixbuf) + if (image_post_buffer_get(imd)) { - image_overlay_queue_draw(imd, od, TRUE); + if (debug) printf("from post buffer: %s\n", imd->image_path); + return TRUE; + } - g_object_ref(G_OBJECT(pixbuf)); - g_object_unref(G_OBJECT(od->pixbuf)); - od->pixbuf = pixbuf; + if (image_read_ahead_check(imd)) + { + if (debug) printf("from read ahead buffer: %s\n", imd->image_path); + return TRUE; + } - od->x = x; - od->y = y; + if (!imd->delay_flip && image_get_pixbuf(imd)) + { + PixbufRenderer *pr; - image_overlay_queue_draw(imd, od, FALSE); + pr = PIXBUF_RENDERER(imd->pr); + if (pr->pixbuf) g_object_unref(pr->pixbuf); + pr->pixbuf = NULL; } - else + + g_object_set(G_OBJECT(imd->pr), "loading", TRUE, NULL); + + imd->il = image_loader_new(path); + + image_loader_set_area_ready_func(imd->il, image_load_area_cb, imd); + image_loader_set_error_func(imd->il, image_load_error_cb, imd); + image_loader_set_buffer_size(imd->il, IMAGE_LOAD_BUFFER_COUNT); + + if (!image_loader_start(imd->il, image_load_done_cb, imd)) { - image_overlay_queue_draw(imd, od, TRUE); - image_overlay_free(imd, od); - } -} + if (debug) printf("image start error\n"); -gint image_overlay_get(ImageWindow *imd, gint id, GdkPixbuf **pixbuf, gint *x, gint *y) -{ - OverlayData *od; + g_object_set(G_OBJECT(imd->pr), "loading", FALSE, NULL); - if (!imd) return FALSE; + image_loader_free(imd->il); + imd->il = NULL; + + image_complete_util(imd, FALSE); - od = image_overlay_find(imd, id); - if (!od) return FALSE; + return FALSE; + } + +#ifdef IMAGE_THROTTLE_LARGER_IMAGES + image_load_buffer_throttle(imd->il); +#endif - if (pixbuf) *pixbuf = od->pixbuf; - if (x) *x = od->x; - if (y) *y = od->y; + if (!imd->delay_flip && !image_get_pixbuf(imd)) image_load_pixbuf_ready(imd); return TRUE; } -void image_overlay_remove(ImageWindow *imd, gint id) +static void image_reset(ImageWindow *imd) { - image_overlay_set(imd, id, NULL, 0, 0); + /* stops anything currently being done */ + + if (debug) printf("image reset\n"); + + g_object_set(G_OBJECT(imd->pr), "loading", FALSE, NULL); + + image_loader_free(imd->il); + imd->il = NULL; + + imd->delay_alter_type = ALTER_NONE; } /* *------------------------------------------------------------------- - * scroller + * image changer *------------------------------------------------------------------- */ -#define SCROLLER_UPDATES_PER_SEC 30 -#define SCROLLER_DEAD_ZONE 6 - - -static gboolean image_scroller_update_cb(gpointer data) +static void image_change_complete(ImageWindow *imd, gdouble zoom, gint new) { - ImageWindow *imd = data; - gint x, y; - gint xinc, yinc; - - /* this was a simple scroll by difference between scroller and mouse position, - * but all this math results in a smoother result and accounts for a dead zone. - */ - - if (abs(imd->scroller_xpos - imd->scroller_x) < SCROLLER_DEAD_ZONE) - { - x = 0; - } - else - { - gint shift = SCROLLER_DEAD_ZONE / 2 * SCROLLER_UPDATES_PER_SEC; - x = (imd->scroller_xpos - imd->scroller_x) / 2 * SCROLLER_UPDATES_PER_SEC; - x += (x > 0) ? -shift : shift; - } + image_reset(imd); - if (abs(imd->scroller_ypos - imd->scroller_y) < SCROLLER_DEAD_ZONE) - { - y = 0; - } - else + if (imd->image_path && isfile(imd->image_path)) { - gint shift = SCROLLER_DEAD_ZONE / 2 * SCROLLER_UPDATES_PER_SEC; - y = (imd->scroller_ypos - imd->scroller_y) / 2 * SCROLLER_UPDATES_PER_SEC; - y += (y > 0) ? -shift : shift; - } + PixbufRenderer *pr; - if (abs(x) < SCROLLER_DEAD_ZONE * SCROLLER_UPDATES_PER_SEC) - { - xinc = x; - } - else - { - xinc = imd->scroller_xinc; + pr = PIXBUF_RENDERER(imd->pr); + pr->zoom = zoom; /* store the zoom, needed by the loader */ - if (x >= 0) + if (image_load_begin(imd, imd->image_path)) { - if (xinc < 0) xinc = 0; - if (x < xinc) xinc = x; - if (x > xinc) xinc = MIN(xinc + x / SCROLLER_UPDATES_PER_SEC, x); + imd->unknown = FALSE; } else { - if (xinc > 0) xinc = 0; - if (x > xinc) xinc = x; - if (x < xinc) xinc = MAX(xinc + x / SCROLLER_UPDATES_PER_SEC, x); - } - } + GdkPixbuf *pixbuf; - if (abs(y) < SCROLLER_DEAD_ZONE * SCROLLER_UPDATES_PER_SEC) - { - yinc = y; + pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN); + image_change_pixbuf(imd, pixbuf, zoom); + g_object_unref(pixbuf); + + imd->unknown = TRUE; + } + imd->size = filesize(imd->image_path); + imd->mtime = filetime(imd->image_path); } else { - yinc = imd->scroller_yinc; - - if (y >= 0) + if (imd->image_path) { - if (yinc < 0) yinc = 0; - if (y < yinc) yinc = y; - if (y > yinc) yinc = MIN(yinc + y / SCROLLER_UPDATES_PER_SEC, y); + GdkPixbuf *pixbuf; + + pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN); + image_change_pixbuf(imd, pixbuf, zoom); + g_object_unref(pixbuf); + imd->mtime = filetime(imd->image_path); } else { - if (yinc > 0) yinc = 0; - if (y > yinc) yinc = y; - if (y < yinc) yinc = MAX(yinc + y / SCROLLER_UPDATES_PER_SEC, y); + image_change_pixbuf(imd, NULL, zoom); + imd->mtime = 0; } + imd->unknown = TRUE; + imd->size = 0; } - imd->scroller_xinc = xinc; - imd->scroller_yinc = yinc; + image_update_util(imd); +} - xinc = xinc / SCROLLER_UPDATES_PER_SEC; - yinc = yinc / SCROLLER_UPDATES_PER_SEC; +static void image_change_real(ImageWindow *imd, const gchar *path, + CollectionData *cd, CollectInfo *info, gdouble zoom) +{ + GdkPixbuf *pixbuf; + GdkPixbuf *prev_pixbuf = NULL; + gchar *prev_path = NULL; + gint prev_clear = FALSE; - image_scroll(imd, xinc, yinc); + imd->collection = cd; + imd->collection_info = info; - return TRUE; -} + pixbuf = image_get_pixbuf(imd); -static void image_scroller_timer_set(ImageWindow *imd, gint start) -{ - if (imd->scroller_id != -1) + if (enable_read_ahead && imd->image_path && pixbuf) { - g_source_remove(imd->scroller_id); - imd->scroller_id = -1; + if (imd->il) + { + /* current image is not finished */ + prev_clear = TRUE; + } + else + { + prev_path = g_strdup(imd->image_path); + prev_pixbuf = pixbuf; + g_object_ref(prev_pixbuf); + } } - if (start) + g_free(imd->image_path); + imd->image_path = g_strdup(path); + imd->image_name = filename_from_path(imd->image_path); + + image_change_complete(imd, zoom, TRUE); + + if (prev_pixbuf) { - imd->scroller_id = g_timeout_add(1000 / SCROLLER_UPDATES_PER_SEC, - image_scroller_update_cb, imd); + image_post_buffer_set(imd, prev_path, prev_pixbuf); + g_free(prev_path); + g_object_unref(prev_pixbuf); } -} - -static void image_scroller_start(ImageWindow *imd, gint x, gint y) -{ - if (imd->scroller_overlay == -1) + else if (prev_clear) { - GdkPixbuf *pixbuf; - gint w, h; - - pixbuf = pixbuf_inline(PIXBUF_INLINE_SCROLLER); - w = gdk_pixbuf_get_width(pixbuf); - h = gdk_pixbuf_get_height(pixbuf); - - imd->scroller_overlay = image_overlay_add(imd, pixbuf, x - w / 2, y - h / 2, FALSE, TRUE); + image_post_buffer_set(imd, NULL, NULL); } - imd->scroller_x = x; - imd->scroller_y = y; - imd->scroller_xpos = x; - imd->scroller_ypos = y; - - image_scroller_timer_set(imd, TRUE); -} - -static void image_scroller_stop(ImageWindow *imd) -{ - if (imd->scroller_id == -1) return; - - image_overlay_remove(imd, imd->scroller_overlay); - imd->scroller_overlay = -1; - - image_scroller_timer_set(imd, FALSE); + image_update_title(imd); + image_new_util(imd); } /* *------------------------------------------------------------------- - * mouse stuff + * focus stuff *------------------------------------------------------------------- */ -static gint image_mouse_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data) +static void image_focus_paint(ImageWindow *imd, gint has_focus, GdkRectangle *area) { - ImageWindow *imd = data; - gint accel; - - if (imd->scroller_id != -1) - { - imd->scroller_xpos = bevent->x; - imd->scroller_ypos = bevent->y; - } - - if (!imd->in_drag || !gdk_pointer_is_grabbed()) return FALSE; + GtkWidget *widget; - if (imd->drag_moved < IMAGE_DRAG_SCROLL_THRESHHOLD) - { - imd->drag_moved++; - } - else - { - widget_set_cursor (imd->image, GDK_FLEUR); - } + widget = imd->widget; + if (!widget->window) return; - if (bevent->state & GDK_SHIFT_MASK) + if (has_focus) { - accel = IMAGE_PAN_SHIFT_MULTIPLIER; + gtk_paint_focus (widget->style, widget->window, GTK_STATE_ACTIVE, + area, widget, "image_window", + widget->allocation.x, widget->allocation.y, + widget->allocation.width - 1, widget->allocation.height - 1); } else { - accel = 1; + gtk_paint_shadow (widget->style, widget->window, GTK_STATE_NORMAL, GTK_SHADOW_IN, + area, widget, "image_window", + widget->allocation.x, widget->allocation.y, + widget->allocation.width - 1, widget->allocation.height - 1); } - - /* do the scroll */ - image_scroll_real(imd, (imd->drag_last_x - bevent->x) * accel, - (imd->drag_last_y - bevent->y) * accel); - - imd->drag_last_x = bevent->x; - imd->drag_last_y = bevent->y; - - return FALSE; } -static gint image_mouse_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data) +static gint image_focus_expose(GtkWidget *widget, GdkEventExpose *event, gpointer data) { ImageWindow *imd = data; - if (imd->scroller_id != -1) return TRUE; + image_focus_paint(imd, GTK_WIDGET_HAS_FOCUS(widget), &event->area); + return TRUE; +} - switch (bevent->button) - { - case 1: - imd->in_drag = TRUE; - imd->drag_last_x = bevent->x; - imd->drag_last_y = bevent->y; - imd->drag_moved = 0; - gdk_pointer_grab(imd->image->window, FALSE, - GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK, - NULL, NULL, bevent->time); - gtk_grab_add(imd->image); - break; - case 2: - imd->drag_moved = 0; - break; - case 3: - image_button_do(imd, bevent); - break; - default: - break; - } +static gint image_focus_in_cb(GtkWidget *widget, GdkEventFocus *event, gpointer data) +{ + ImageWindow *imd = data; - gtk_widget_grab_focus(imd->widget); + GTK_WIDGET_SET_FLAGS(imd->widget, GTK_HAS_FOCUS); + image_focus_paint(imd, TRUE, NULL); - return FALSE; + return TRUE; } -static gint image_mouse_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data) +static gint image_focus_out_cb(GtkWidget *widget, GdkEventFocus *event, gpointer data) { ImageWindow *imd = data; - if (imd->scroller_id != -1) - { - image_scroller_stop(imd); - return TRUE; - } - - if (gdk_pointer_is_grabbed() && GTK_WIDGET_HAS_GRAB(imd->image)) - { - gtk_grab_remove(imd->image); - gdk_pointer_ungrab(bevent->time); - widget_set_cursor(imd->image, -1); - } - - if (imd->drag_moved < IMAGE_DRAG_SCROLL_THRESHHOLD) - { - if (bevent->button == 1 && (bevent->state & GDK_SHIFT_MASK)) - { - image_scroller_start(imd, bevent->x, bevent->y); - } - else if (bevent->button == 1 || bevent->button == 2) - { - image_button_do(imd, bevent); - } - } + GTK_WIDGET_UNSET_FLAGS(imd->widget, GTK_HAS_FOCUS); + image_focus_paint(imd, FALSE, NULL); - imd->in_drag = FALSE; + return TRUE; +} - return FALSE; +gint image_overlay_add(ImageWindow *imd, GdkPixbuf *pixbuf, gint x, gint y, + gint relative, gint always) +{ + return pixbuf_renderer_overlay_add((PixbufRenderer *)imd->pr, pixbuf, x, y, relative, always); } -static gint image_mouse_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data) +void image_overlay_set(ImageWindow *imd, gint id, GdkPixbuf *pixbuf, gint x, gint y) { - ImageWindow *imd = data; + pixbuf_renderer_overlay_set((PixbufRenderer *)imd->pr, id, pixbuf, x, y); +} - if (imd->scroller_id != -1) - { - imd->scroller_xpos = imd->scroller_x; - imd->scroller_ypos = imd->scroller_y; - imd->scroller_xinc = 0; - imd->scroller_yinc = 0; - } +gint image_overlay_get(ImageWindow *imd, gint id, GdkPixbuf **pixbuf, gint *x, gint *y) +{ + return pixbuf_renderer_overlay_get((PixbufRenderer *)imd->pr, id, pixbuf, x, y); +} - return FALSE; +void image_overlay_remove(ImageWindow *imd, gint id) +{ + pixbuf_renderer_overlay_remove((PixbufRenderer *)imd->pr, id); } static gint image_scroll_cb(GtkWidget *widget, GdkEventScroll *event, gpointer data) @@ -3142,19 +805,6 @@ static gint image_scroll_cb(GtkWidget *widget, GdkEventScroll *event, gpointer d return FALSE; } -static void image_mouse_drag_cb(GtkWidget *widget, GdkDragContext *context, gpointer data) -{ - ImageWindow *imd = data; - - imd->drag_moved = IMAGE_DRAG_SCROLL_THRESHHOLD; -} - -/* - *------------------------------------------------------------------- - * drag and drop - *------------------------------------------------------------------- - */ - /* *------------------------------------------------------------------- * public interface @@ -3171,6 +821,10 @@ void image_attach_window(ImageWindow *imd, GtkWidget *window, imd->title_right = g_strdup(title_right); imd->title_show_zoom = show_zoom; + if (!fit_window) window = NULL; + + pixbuf_renderer_set_parent((PixbufRenderer *)imd->pr, (GtkWindow *)window); + image_update_title(imd); } @@ -3199,17 +853,6 @@ void image_set_new_func(ImageWindow *imd, } -static void image_button_do(ImageWindow *imd, GdkEventButton *bevent) -{ - if (imd->func_button && - bevent && - (bevent->type == GDK_BUTTON_PRESS || bevent->type == GDK_BUTTON_RELEASE)) - { - imd->func_button(imd, bevent->button, bevent->time, - bevent->x, bevent->y, bevent->state, imd->data_button); - } -} - void image_set_button_func(ImageWindow *imd, void (*func)(ImageWindow *, gint button, guint32 time, gdouble x, gdouble y, guint state, gpointer), gpointer data) @@ -3264,16 +907,17 @@ void image_change_path(ImageWindow *imd, const gchar *path, gdouble zoom) if (imd->image_path == path || (path && imd->image_path && !strcmp(path, imd->image_path)) ) return; - image_source_tile_unset(imd); - image_change_real(imd, path, NULL, NULL, zoom); } -void image_change_pixbuf(ImageWindow *imd, GdkPixbuf *pixbuf, gdouble zoom) +GdkPixbuf *image_get_pixbuf(ImageWindow *imd) { - image_source_tile_unset(imd); + return pixbuf_renderer_get_pixbuf((PixbufRenderer *)imd->pr); +} - image_set_pixbuf(imd, pixbuf, zoom, TRUE); +void image_change_pixbuf(ImageWindow *imd, GdkPixbuf *pixbuf, gdouble zoom) +{ + pixbuf_renderer_set_pixbuf((PixbufRenderer *)imd->pr, pixbuf, zoom); image_new_util(imd); } @@ -3281,8 +925,6 @@ void image_change_from_collection(ImageWindow *imd, CollectionData *cd, CollectI { if (!cd || !info || !g_list_find(cd->list, info)) return; - image_source_tile_unset(imd); - image_change_real(imd, info->path, cd, info, zoom); } @@ -3305,6 +947,7 @@ CollectionData *image_get_collection(ImageWindow *imd, CollectInfo **info) return NULL; } +#if 0 static void image_loader_sync_data(ImageLoader *il, gpointer data) { /* change data for the callbacks directly */ @@ -3314,6 +957,7 @@ static void image_loader_sync_data(ImageLoader *il, gpointer data) il->data_done = data; il->data_percent = data; } +#endif /* this is more like a move function * it moves most data from source to imd, source does keep a ref on the pixbuf @@ -3322,6 +966,8 @@ void image_change_from_image(ImageWindow *imd, ImageWindow *source) { if (imd == source) return; + printf("FIXME: enable set from image\n"); +#if 0 imd->zoom_min = source->zoom_min; imd->zoom_max = source->zoom_max; @@ -3414,60 +1060,37 @@ void image_change_from_image(ImageWindow *imd, ImageWindow *source) } image_scroll_clamp(imd); +#endif } /* manipulation */ void image_area_changed(ImageWindow *imd, gint x, gint y, gint width, gint height) { - gint sx, sy, sw, sh; - - sx = (gint)floor((double)x * imd->scale); - sy = (gint)floor((double)y * imd->scale); - sw = (gint)ceil((double)width * imd->scale); - sh = (gint)ceil((double)height * imd->scale); - - if (imd->source_tiles_enabled) - { - source_tile_changed(imd, x, y, width, height); - } - - image_queue(imd, sx, sy, sw, sh, FALSE, TILE_RENDER_AREA, TRUE); + pixbuf_renderer_area_changed((PixbufRenderer *)imd->pr, x, y, width, height); } void image_reload(ImageWindow *imd) { - if (imd->source_tiles_enabled) return; + if (pixbuf_renderer_get_tiles((PixbufRenderer *)imd->pr)) return; - image_change_complete(imd, imd->zoom, FALSE); + image_change_complete(imd, image_zoom_get(imd), FALSE); } void image_scroll(ImageWindow *imd, gint x, gint y) { - image_scroll_real(imd, x, y); + pixbuf_renderer_scroll((PixbufRenderer *)imd->pr, x, y); } void image_scroll_to_point(ImageWindow *imd, gint x, gint y, gdouble x_align, gdouble y_align) { - gint px, py; - gint ax, ay; - - x_align = CLAMP(x_align, 0.0, 1.0); - y_align = CLAMP(y_align, 0.0, 1.0); - - ax = (gdouble)imd->vis_width * x_align; - ay = (gdouble)imd->vis_height * y_align; - - px = (gdouble)x * imd->scale - (imd->x_scroll + ax); - py = (gdouble)y * imd->scale - (imd->y_scroll + ay); - - image_scroll(imd, px, py); + pixbuf_renderer_scroll_to_point((PixbufRenderer *)imd->pr, x, y, x_align, y_align); } void image_alter(ImageWindow *imd, AlterType type) { - if (imd->source_tiles_enabled) return; + if (pixbuf_renderer_get_tiles((PixbufRenderer *)imd->pr)) return; if (imd->il) { @@ -3479,91 +1102,44 @@ void image_alter(ImageWindow *imd, AlterType type) image_alter_real(imd, type, TRUE); } -/* zoom */ - -static void image_zoom_adjust_real(ImageWindow *imd, gdouble increment, - gint center_point, gint x, gint y) -{ - gdouble zoom = imd->zoom; - - if (increment == 0.0) return; /* avoid possible div by zero, a no-op anyway... */ - - if (zoom == 0.0) - { - if (imd->scale < 1.0) - { - zoom = 0.0 - 1.0 / imd->scale; - } - else - { - zoom = imd->scale; - } - } - - if (increment < 0.0) - { - if (zoom >= 1.0 && zoom + increment < 1.0) - { - zoom = zoom + increment - 2.0; - } - else - { - zoom = zoom + increment; - } - } - else - { - if (zoom <= -1.0 && zoom + increment > -1.0) - { - zoom = zoom + increment + 2.0; - } - else - { - zoom = zoom + increment; - } - } - - image_zoom_sync(imd, zoom, FALSE, FALSE, FALSE, center_point, x, y); -} - void image_zoom_adjust(ImageWindow *imd, gdouble increment) { - image_zoom_adjust_real(imd, increment, FALSE, 0, 0); + pixbuf_renderer_zoom_adjust((PixbufRenderer *)imd->pr, increment); } void image_zoom_adjust_at_point(ImageWindow *imd, gdouble increment, gint x, gint y) { - image_zoom_adjust_real(imd, increment, TRUE, x, y); + pixbuf_renderer_zoom_adjust_at_point((PixbufRenderer *)imd->pr, increment, x, y); } void image_zoom_set_limits(ImageWindow *imd, gdouble min, gdouble max) { - if (min > 1.0 || max < 1.0) return; - if (min < 1.0 && min > -1.0) return; - if (min < -200.0 || max > 200.0) return; - - imd->zoom_min = min; - imd->zoom_max = max; + pixbuf_renderer_zoom_set_limits((PixbufRenderer *)imd->pr, min, max); } void image_zoom_set(ImageWindow *imd, gdouble zoom) { - image_zoom_sync(imd, zoom, FALSE, FALSE, FALSE, FALSE, 0, 0); + pixbuf_renderer_zoom_set((PixbufRenderer *)imd->pr, zoom); } void image_zoom_set_fill_geometry(ImageWindow *imd, gint vertical) { + PixbufRenderer *pr; gdouble zoom; + gint width, height; - if (!imd->pixbuf || imd->image_width < 1 || imd->image_height < 1) return; + pr = (PixbufRenderer *)imd->pr; + + if (!pixbuf_renderer_get_pixbuf(pr) || + !pixbuf_renderer_get_image_size(pr, &width, &height)) return; if (vertical) { - zoom = (gdouble)imd->window_height / imd->image_height; + zoom = (gdouble)pr->window_height / height; } else { - zoom = (gdouble)imd->window_width / imd->image_width; + zoom = (gdouble)pr->window_width / width; } if (zoom < 1.0) @@ -3571,44 +1147,49 @@ void image_zoom_set_fill_geometry(ImageWindow *imd, gint vertical) zoom = 0.0 - 1.0 / zoom; } - image_zoom_set(imd, zoom); + pixbuf_renderer_zoom_set(pr, zoom); } gdouble image_zoom_get(ImageWindow *imd) { - return imd->zoom; + return pixbuf_renderer_zoom_get((PixbufRenderer *)imd->pr); } gdouble image_zoom_get_real(ImageWindow *imd) { - return imd->scale; + return pixbuf_renderer_zoom_get_scale((PixbufRenderer *)imd->pr); } gchar *image_zoom_get_as_text(ImageWindow *imd) { + gdouble zoom; + gdouble scale; gdouble l = 1.0; gdouble r = 1.0; gint pl = 0; gint pr = 0; gchar *approx = " "; - if (imd->zoom > 0.0) + zoom = image_zoom_get(imd); + scale = image_zoom_get_real(imd); + + if (zoom > 0.0) { - l = imd->zoom; + l = zoom; } - else if (imd->zoom < 0.0) + else if (zoom < 0.0) { - r = 0.0 - imd->zoom; + r = 0.0 - zoom; } - else if (imd->zoom == 0.0 && imd->scale != 0.0) + else if (zoom == 0.0 && scale != 0.0) { - if (imd->scale >= 1.0) + if (scale >= 1.0) { - l = imd->scale; + l = scale; } else { - r = 1.0 / imd->scale; + r = 1.0 / scale; } approx = " ~"; } @@ -3650,7 +1231,7 @@ gdouble image_zoom_get_default(ImageWindow *imd, gint mode) void image_prebuffer_set(ImageWindow *imd, const gchar *path) { - if (imd->source_tiles_enabled) return; + if (pixbuf_renderer_get_tiles((PixbufRenderer *)imd->pr)) return; if (path) { @@ -3667,7 +1248,7 @@ static gint image_auto_refresh_cb(gpointer data) ImageWindow *imd = data; time_t newtime; - if (!imd || !imd->pixbuf || + if (!imd || !image_get_pixbuf(imd) || imd->il || !imd->image_path || !update_on_time_change) return TRUE; @@ -3686,7 +1267,7 @@ static gint image_auto_refresh_cb(gpointer data) void image_auto_refresh(ImageWindow *imd, gint interval) { if (!imd) return; - if (imd->source_tiles_enabled) return; + if (pixbuf_renderer_get_tiles((PixbufRenderer *)imd->pr)) return; if (imd->auto_refresh_id > -1) { @@ -3703,59 +1284,21 @@ void image_auto_refresh(ImageWindow *imd, gint interval) imd->auto_refresh_interval = interval; } -/* allow top window to be resized ? */ - void image_top_window_set_sync(ImageWindow *imd, gint allow_sync) { imd->top_window_sync = allow_sync; -} -/* background colors */ + g_object_set(G_OBJECT(imd->pr), "window_fit", allow_sync, NULL); +} void image_background_set_black(ImageWindow *imd, gint black) { - GtkStyle *style; - - if (!imd) return; - - style = gtk_style_copy(gtk_widget_get_style(imd->widget)); - g_object_ref(G_OBJECT(style)); - - if (black) - { - style->bg[GTK_STATE_NORMAL] = style->black; - } - - gtk_widget_set_style(imd->image, style); - g_object_unref(G_OBJECT(style)); - - if (GTK_WIDGET_VISIBLE(imd->widget)) image_border_clear(imd); + pixbuf_renderer_set_black((PixbufRenderer *)imd->pr, black); } void image_background_set_color(ImageWindow *imd, GdkColor *color) { - GtkStyle *style; - - if (!imd) return; - - style = gtk_style_copy(gtk_widget_get_style(imd->widget)); - g_object_ref(G_OBJECT(style)); - - if (color) - { - GdkColor *slot; - - slot = &style->bg[GTK_STATE_NORMAL]; - - slot->red = color->red; - slot->green = color->green; - slot->blue = color->blue; - } - - gtk_widget_set_style(imd->image, style); - g_object_unref(G_OBJECT(style)); - - if (GTK_WIDGET_VISIBLE(imd->widget)) image_border_clear(imd); + pixbuf_renderer_set_color((PixbufRenderer *)imd->pr, color); } void image_set_delay_flip(ImageWindow *imd, gint delay) @@ -3764,41 +1307,51 @@ void image_set_delay_flip(ImageWindow *imd, gint delay) imd->delay_flip == delay) return; imd->delay_flip = delay; + + g_object_set(G_OBJECT(imd->pr), "delay_flip", delay, NULL); + if (!imd->delay_flip && imd->il) { - if (imd->pixbuf) g_object_unref(imd->pixbuf); - imd->pixbuf = NULL; - image_load_pixbuf_ready(imd); + PixbufRenderer *pr; - image_queue_clear(imd); - image_queue(imd, 0, 0, imd->width, imd->height, FALSE, TILE_RENDER_AREA, TRUE); + pr = PIXBUF_RENDERER(imd->pr); + if (pr->pixbuf) g_object_unref(pr->pixbuf); + pr->pixbuf = NULL; + + image_load_pixbuf_ready(imd); } } -/* wallpaper util */ - void image_to_root_window(ImageWindow *imd, gint scaled) { GdkScreen *screen; GdkWindow *rootwindow; GdkPixmap *pixmap; + GdkPixbuf *pixbuf; GdkPixbuf *pb; + gint width, height; + + if (!imd) return; - if (!imd || !imd->pixbuf) return; + pixbuf = image_get_pixbuf(imd); + if (!pixbuf) return; - screen = gtk_widget_get_screen(imd->image); + screen = gtk_widget_get_screen(imd->widget); rootwindow = gdk_screen_get_root_window(screen); if (gdk_drawable_get_visual(rootwindow) != gdk_visual_get_system()) return; if (scaled) { - pb = gdk_pixbuf_scale_simple(imd->pixbuf, gdk_screen_width(), gdk_screen_height(), (GdkInterpType)zoom_quality); + width = gdk_screen_width(); + height = gdk_screen_height(); } else { - pb = gdk_pixbuf_scale_simple(imd->pixbuf, imd->width, imd->height, (GdkInterpType)zoom_quality); + pixbuf_renderer_get_scaled_size((PixbufRenderer *)imd->pr, &width, &height); } + pb = gdk_pixbuf_scale_simple(pixbuf, width, height, (GdkInterpType)zoom_quality); + gdk_pixbuf_render_pixmap_and_mask (pb, &pixmap, NULL, 128); gdk_window_set_back_pixmap(rootwindow, pixmap, FALSE); gdk_window_clear(rootwindow); @@ -3808,7 +1361,6 @@ void image_to_root_window(ImageWindow *imd, gint scaled) gdk_flush(); } - /* *------------------------------------------------------------------- * init / destroy @@ -3817,6 +1369,8 @@ void image_to_root_window(ImageWindow *imd, gint scaled) static void image_free(ImageWindow *imd) { + image_reset(imd); + image_read_ahead_cancel(imd); image_post_buffer_set(imd, NULL, NULL); image_auto_refresh(imd, -1); @@ -3825,16 +1379,6 @@ static void image_free(ImageWindow *imd) g_free(imd->title); g_free(imd->title_right); - image_reset(imd); - image_tile_sync_count(imd, 0); - if (imd->pixbuf) g_object_unref(imd->pixbuf); - - image_scroller_timer_set(imd, FALSE); - - image_overlay_list_clear(imd); - - source_tile_free_all(imd); - g_free(imd); } @@ -3850,16 +1394,6 @@ ImageWindow *image_new(gint frame) imd = g_new0(ImageWindow, 1); - imd->zoom_min = IMAGE_ZOOM_MIN; - imd->zoom_max = IMAGE_ZOOM_MAX; - imd->zoom = 1.0; - imd->scale = 1.0; - - imd->draw_idle_id = -1; - - imd->tile_width = IMAGE_TILE_SIZE; - imd->tile_height = IMAGE_TILE_SIZE; - imd->top_window = NULL; imd->title = NULL; imd->title_right = NULL; @@ -3867,14 +1401,9 @@ ImageWindow *image_new(gint frame) imd->unknown = TRUE; - imd->pixbuf = NULL; - imd->has_frame = frame; imd->top_window_sync = FALSE; - imd->tile_cache = NULL; - imd->tile_cache_size = 0; - imd->delay_alter_type = ALTER_NONE; imd->read_ahead_il = NULL; @@ -3896,21 +1425,16 @@ ImageWindow *image_new(gint frame) imd->func_button = NULL; imd->func_scroll = NULL; - imd->scroller_id = -1; - imd->scroller_overlay = -1; + imd->pr = GTK_WIDGET(pixbuf_renderer_new()); - imd->source_tiles_enabled = FALSE; - imd->source_tiles = NULL; - - imd->image = gtk_drawing_area_new(); - gtk_widget_set_double_buffered(imd->image, FALSE); + g_object_set(G_OBJECT(imd->pr), "zoom_2pass", TRUE, NULL); if (imd->has_frame) { imd->widget = gtk_frame_new(NULL); gtk_frame_set_shadow_type(GTK_FRAME(imd->widget), GTK_SHADOW_IN); - gtk_container_add(GTK_CONTAINER(imd->widget), imd->image); - gtk_widget_show(imd->image); + gtk_container_add(GTK_CONTAINER(imd->widget), imd->pr); + gtk_widget_show(imd->pr); GTK_WIDGET_SET_FLAGS(imd->widget, GTK_CAN_FOCUS); g_signal_connect(G_OBJECT(imd->widget), "focus_in_event", @@ -3923,32 +1447,18 @@ ImageWindow *image_new(gint frame) } else { - imd->widget = imd->image; + imd->widget = imd->pr; } - g_signal_connect(G_OBJECT(imd->image), "motion_notify_event", - G_CALLBACK(image_mouse_motion_cb), imd); - g_signal_connect(G_OBJECT(imd->image), "button_press_event", - G_CALLBACK(image_mouse_press_cb), imd); - g_signal_connect(G_OBJECT(imd->image), "button_release_event", - G_CALLBACK(image_mouse_release_cb), imd); - g_signal_connect(G_OBJECT(imd->image), "leave_notify_event", - G_CALLBACK(image_mouse_leave_cb), imd); - gtk_widget_set_events(imd->image, GDK_POINTER_MOTION_MASK | - GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK | - GDK_LEAVE_NOTIFY_MASK); - - g_signal_connect(G_OBJECT(imd->image), "expose_event", - G_CALLBACK(image_expose_cb), imd); - g_signal_connect_after(G_OBJECT(imd->image), "size_allocate", - G_CALLBACK(image_size_cb), imd); - - g_signal_connect(G_OBJECT(imd->image), "drag_begin", - G_CALLBACK(image_mouse_drag_cb), imd); - g_signal_connect(G_OBJECT(imd->image), "scroll_event", + g_signal_connect(G_OBJECT(imd->pr), "clicked", + G_CALLBACK(image_click_cb), imd); + g_signal_connect(G_OBJECT(imd->pr), "scroll_notify", + G_CALLBACK(image_scroll_notify_cb), imd); + + g_signal_connect(G_OBJECT(imd->pr), "scroll_event", G_CALLBACK(image_scroll_cb), imd); - g_signal_connect(G_OBJECT(imd->widget), "destroy", + g_signal_connect(G_OBJECT(imd->pr), "destroy", G_CALLBACK(image_destroy_cb), imd); return imd; diff --git a/src/image.h b/src/image.h index 0d811a3b..d5a1a04e 100644 --- a/src/image.h +++ b/src/image.h @@ -52,6 +52,8 @@ void image_change_from_collection(ImageWindow *imd, CollectionData *cd, CollectI CollectionData *image_get_collection(ImageWindow *imd, CollectInfo **info); void image_change_from_image(ImageWindow *imd, ImageWindow *source); +GdkPixbuf *image_get_pixbuf(ImageWindow *imd); + /* manipulation */ void image_area_changed(ImageWindow *imd, gint x, gint y, gint width, gint height); void image_reload(ImageWindow *imd); diff --git a/src/img-view.c b/src/img-view.c index 7681ae71..b0f305a8 100644 --- a/src/img-view.c +++ b/src/img-view.c @@ -23,6 +23,7 @@ #include "image-overlay.h" #include "info.h" #include "menu.h" +#include "pixbuf-renderer.h" #include "slideshow.h" #include "utilops.h" #include "ui_bookmark.h" @@ -317,7 +318,7 @@ static void view_window_menu_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *p ImageWindow *imd; imd = view_window_active_image(vw); - gdk_window_get_origin(imd->image->window, x, y); + gdk_window_get_origin(imd->pr->window, x, y); popup_menu_position_clamp(menu, x, y, 0); } @@ -852,13 +853,11 @@ static ViewWindow *real_view_window_new(const gchar *path, GList *list, Collecti if (image_zoom_get(vw->imd) == 0.0) { - w = vw->imd->image_width; - h = vw->imd->image_height; + pixbuf_renderer_get_image_size(PIXBUF_RENDERER(vw->imd->pr), &w, &h); } else { - w = vw->imd->width; - h = vw->imd->height; + pixbuf_renderer_get_scaled_size(PIXBUF_RENDERER(vw->imd->pr), &w, &h); } if (limit_window_size) { @@ -875,7 +874,7 @@ static ViewWindow *real_view_window_new(const gchar *path, GList *list, Collecti req_size.height = h; gtk_widget_size_allocate(GTK_WIDGET(vw->window), &req_size); - gtk_widget_set_size_request(vw->imd->image, w, h); + gtk_widget_set_size_request(vw->imd->pr, w, h); gtk_widget_show(vw->window); @@ -1369,7 +1368,7 @@ static void view_window_get_dnd_data(GtkWidget *widget, GdkDragContext *context, ViewWindow *vw = data; ImageWindow *imd; - if (gtk_drag_get_source_widget(context) == vw->imd->image) return; + if (gtk_drag_get_source_widget(context) == vw->imd->pr) return; imd = vw->imd; @@ -1489,17 +1488,17 @@ static void view_window_dnd_init(ViewWindow *vw) imd = vw->imd; - gtk_drag_source_set(imd->image, GDK_BUTTON2_MASK, + gtk_drag_source_set(imd->pr, GDK_BUTTON2_MASK, dnd_file_drag_types, dnd_file_drag_types_count, GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK); - g_signal_connect(G_OBJECT(imd->image), "drag_data_get", + g_signal_connect(G_OBJECT(imd->pr), "drag_data_get", G_CALLBACK(view_window_set_dnd_data), vw); - gtk_drag_dest_set(imd->image, + gtk_drag_dest_set(imd->pr, GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP, dnd_file_drop_types, dnd_file_drop_types_count, GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK); - g_signal_connect(G_OBJECT(imd->image), "drag_data_received", + g_signal_connect(G_OBJECT(imd->pr), "drag_data_received", G_CALLBACK(view_window_get_dnd_data), vw); } diff --git a/src/info.c b/src/info.c index d01ef890..b3653d76 100644 --- a/src/info.c +++ b/src/info.c @@ -19,6 +19,7 @@ #include "filelist.h" #include "image.h" #include "image-load.h" +#include "pixbuf-renderer.h" #include "ui_bookmark.h" #include "ui_fileops.h" #include "ui_misc.h" @@ -208,16 +209,21 @@ static void info_tab_general_image(InfoData *id, gpointer data) gchar *buf; guint mem_size; gint has_alpha; + GdkPixbuf *pixbuf; + gint width, height; if (id->image->unknown) return; - buf = g_strdup_printf("%d x %d", id->image->image_width, id->image->image_height); + pixbuf_renderer_get_image_size(PIXBUF_RENDERER(id->image->pr), &width, &height); + + buf = g_strdup_printf("%d x %d", width, height); gtk_label_set_text(GTK_LABEL(tab->label_dimensions), buf); g_free(buf); - if (id->image->pixbuf) + pixbuf = image_get_pixbuf(id->image); + if (pixbuf) { - has_alpha = gdk_pixbuf_get_has_alpha(id->image->pixbuf); + has_alpha = gdk_pixbuf_get_has_alpha(pixbuf); } else { @@ -225,7 +231,7 @@ static void info_tab_general_image(InfoData *id, gpointer data) } gtk_label_set_text(GTK_LABEL(tab->label_transparent), has_alpha ? _("yes") : _("no")); - mem_size = id->image->image_width * id->image->image_height * ((has_alpha) ? 4 : 3); + mem_size = width * height * ((has_alpha) ? 4 : 3); buf = text_from_size_abrev(mem_size); gtk_label_set_text(GTK_LABEL(tab->label_image_size), buf); g_free(buf); @@ -512,18 +518,18 @@ static void info_window_dnd_init(InfoData *id) imd = id->image; - gtk_drag_source_set(imd->image, GDK_BUTTON2_MASK, + gtk_drag_source_set(imd->pr, GDK_BUTTON2_MASK, dnd_file_drag_types, dnd_file_drag_types_count, GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK); - g_signal_connect(G_OBJECT(imd->image), "drag_data_get", + g_signal_connect(G_OBJECT(imd->pr), "drag_data_get", G_CALLBACK(info_window_dnd_data_set), id); #if 0 - gtk_drag_dest_set(imd->image, + gtk_drag_dest_set(imd->pr, GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP, dnd_file_drop_types, dnd_file_drop_types_count, GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK); - g_signal_connect(G_OBJECT(imd->image), "drag_data_received", + g_signal_connect(G_OBJECT(imd->pr), "drag_data_received", G_CALLBACK(info_window_dnd_data_get), id); #endif } diff --git a/src/layout.c b/src/layout.c index 5027cb84..be76cd61 100644 --- a/src/layout.c +++ b/src/layout.c @@ -17,6 +17,7 @@ #include "layout_image.h" #include "layout_util.h" #include "menu.h" +#include "pixbuf-renderer.h" #include "pixbuf_util.h" #include "view_dir_list.h" #include "view_dir_tree.h" @@ -405,8 +406,11 @@ void layout_status_update_image(LayoutWindow *lw) } else { + gint width, height; + + pixbuf_renderer_get_image_size(PIXBUF_RENDERER(lw->image->pr), &width, &height); text = g_strdup_printf(_("( %d x %d ) %s bytes"), - lw->image->image_width, lw->image->image_height, b); + width, height, b); } gtk_label_set_text(GTK_LABEL(lw->info_details), text); diff --git a/src/layout_image.c b/src/layout_image.c index d3a0540a..385d7163 100644 --- a/src/layout_image.c +++ b/src/layout_image.c @@ -84,7 +84,7 @@ static void layout_image_fullscreen_menu_pos_cb(GtkMenu *menu, gint *x, gint *y, if (!lw->full_screen) return; - gdk_window_get_origin(lw->full_screen->imd->image->window, x, y); + gdk_window_get_origin(lw->full_screen->imd->pr->window, x, y); popup_menu_position_clamp(menu, x, y, 0); } @@ -778,7 +778,7 @@ static void layout_image_menu_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean * { LayoutWindow *lw = data; - gdk_window_get_origin(lw->image->image->window, x, y); + gdk_window_get_origin(lw->image->pr->window, x, y); popup_menu_position_clamp(menu, x, y, 0); } @@ -936,19 +936,19 @@ static void layout_image_dnd_end(GtkWidget *widget, GdkDragContext *context, gpo static void layout_image_dnd_init(LayoutWindow *lw) { - gtk_drag_source_set(lw->image->image, GDK_BUTTON2_MASK, + gtk_drag_source_set(lw->image->pr, GDK_BUTTON2_MASK, dnd_file_drag_types, dnd_file_drag_types_count, GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK); - g_signal_connect(G_OBJECT(lw->image->image), "drag_data_get", + g_signal_connect(G_OBJECT(lw->image->pr), "drag_data_get", G_CALLBACK(layout_image_dnd_get), lw); - g_signal_connect(G_OBJECT(lw->image->image), "drag_end", + g_signal_connect(G_OBJECT(lw->image->pr), "drag_end", G_CALLBACK(layout_image_dnd_end), lw); - gtk_drag_dest_set(lw->image->image, + gtk_drag_dest_set(lw->image->pr, GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP, dnd_file_drop_types, dnd_file_drop_types_count, GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK); - g_signal_connect(G_OBJECT(lw->image->image), "drag_data_received", + g_signal_connect(G_OBJECT(lw->image->pr), "drag_data_received", G_CALLBACK(layout_image_dnd_receive), lw); } diff --git a/src/pan-view.c b/src/pan-view.c index e836dff1..ee5ece8a 100644 --- a/src/pan-view.c +++ b/src/pan-view.c @@ -18,11 +18,11 @@ #include "editors.h" #include "filelist.h" #include "fullscreen.h" -#include "image.h" #include "image-load.h" #include "img-view.h" #include "info.h" #include "menu.h" +#include "pixbuf-renderer.h" #include "pixbuf_util.h" #include "thumb.h" #include "utilops.h" @@ -180,8 +180,8 @@ typedef struct _PanWindow PanWindow; struct _PanWindow { GtkWidget *window; - ImageWindow *imd; - ImageWindow *imd_normal; + PixbufRenderer *pr; + PixbufRenderer *pr_normal; FullScreenData *fs; GtkWidget *path_entry; @@ -242,7 +242,6 @@ static GList *pan_layout_intersect(PanWindow *pw, gint x, gint y, gint width, gi static GtkWidget *pan_popup_menu(PanWindow *pw); static void pan_fullscreen_toggle(PanWindow *pw, gint force_off); -static void pan_overlay_toggle(PanWindow *pw); static void pan_window_close(PanWindow *pw); @@ -1079,7 +1078,7 @@ static PanItem *pan_item_new_text(PanWindow *pw, gint x, gint y, const gchar *te pi->color_b = b; pi->color_a = a; - pan_item_text_compute_size(pi, pw->imd->widget); + pan_item_text_compute_size(pi, (GtkWidget *)pw->pr); pw->list = g_list_prepend(pw->list, pi); @@ -1100,7 +1099,7 @@ static void pan_item_set_key(PanItem *pi, const gchar *key) static void pan_item_added(PanWindow *pw, PanItem *pi) { if (!pi) return; - image_area_changed(pw->imd, pi->x, pi->y, pi->width, pi->height); + pixbuf_renderer_area_changed(pw->pr, pi->x, pi->y, pi->width, pi->height); } static void pan_item_remove(PanWindow *pw, PanItem *pi) @@ -1113,7 +1112,7 @@ static void pan_item_remove(PanWindow *pw, PanItem *pi) pw->queue = g_list_remove(pw->queue, pi); pw->list = g_list_remove(pw->list, pi); - image_area_changed(pw->imd, pi->x, pi->y, pi->width, pi->height); + pixbuf_renderer_area_changed(pw->pr, pi->x, pi->y, pi->width, pi->height); pan_item_free(pi); } @@ -1273,9 +1272,6 @@ static PanItem *pan_item_find_by_coord(PanWindow *pw, ItemType type, gint x, gin { GList *work; - if (x < 0 || x >= pw->imd->image_width || - y < 0 || y >= pw->imd->image_height) return NULL; - work = pw->list; while (work) { @@ -1893,9 +1889,9 @@ static void pan_calendar_update(PanWindow *pw, PanItem *pi_day) y = pi_day->y; #if 0 - if (y + grid * (PAN_THUMB_SIZE + PAN_THUMB_GAP) + PAN_FOLDER_BOX_BORDER * 4 > pw->imd->image_height) + if (y + grid * (PAN_THUMB_SIZE + PAN_THUMB_GAP) + PAN_FOLDER_BOX_BORDER * 4 > pw->pr->image_height) { - y = pw->imd->image_height - (grid * (PAN_THUMB_SIZE + PAN_THUMB_GAP) + PAN_FOLDER_BOX_BORDER * 4); + y = pw->pr->image_height - (grid * (PAN_THUMB_SIZE + PAN_THUMB_GAP) + PAN_FOLDER_BOX_BORDER * 4); } #endif @@ -2486,7 +2482,7 @@ static void pan_layout_queue_thumb_done_cb(ThumbLoader *tl, gpointer data) pi->pixbuf = thumb_loader_get_pixbuf(tl, TRUE); rc = pi->refcount; - image_area_changed(pw->imd, pi->x, pi->y, pi->width, pi->height); + pixbuf_renderer_area_changed(pw->pr, pi->x, pi->y, pi->width, pi->height); pi->refcount = rc; } @@ -2527,7 +2523,7 @@ static void pan_layout_queue_image_done_cb(ImageLoader *il, gpointer data) } rc = pi->refcount; - image_area_changed(pw->imd, pi->x, pi->y, pi->width, pi->height); + pixbuf_renderer_area_changed(pw->pr, pi->x, pi->y, pi->width, pi->height); pi->refcount = rc; } @@ -2557,7 +2553,7 @@ static void pan_layout_queue_image_area_cb(ImageLoader *il, guint x, guint y, } rc = pi->refcount; - image_area_changed(pw->imd, pi->x + x, pi->y + y, width, height); + pixbuf_renderer_area_changed(pw->pr, pi->x + x, pi->y + y, width, height); pi->refcount = rc; } } @@ -2644,8 +2640,8 @@ static void pan_layout_queue(PanWindow *pw, PanItem *pi) if (!pw->tl && !pw->il) while(pan_layout_queue_step(pw)); } -static gint pan_window_request_tile_cb(ImageWindow *imd, gint x, gint y, gint width, gint height, - GdkPixbuf *pixbuf, gpointer data) +static gint pan_window_request_tile_cb(PixbufRenderer *pr, gint x, gint y, + gint width, gint height, GdkPixbuf *pixbuf, gpointer data) { PanWindow *pw = data; GList *list; @@ -2959,8 +2955,8 @@ static gint pan_window_request_tile_cb(ImageWindow *imd, gint x, gint y, gint wi { PangoLayout *layout; - layout = pan_item_text_layout(pi, imd->image); - pixbuf_draw_layout(pixbuf, layout, imd->image, + layout = pan_item_text_layout(pi, (GtkWidget *)pr); + pixbuf_draw_layout(pixbuf, layout, (GtkWidget *)pr, pi->x - x + PAN_TEXT_BORDER_SIZE, pi->y - y + PAN_TEXT_BORDER_SIZE, pi->color_r, pi->color_g, pi->color_b, pi->color_a); g_object_unref(G_OBJECT(layout)); @@ -2972,42 +2968,28 @@ static gint pan_window_request_tile_cb(ImageWindow *imd, gint x, gint y, gint wi { static gint count = 0; PangoLayout *layout; - gint lx, ly; - gint lw, lh; - GdkPixmap *pixmap; gchar *buf; - layout = gtk_widget_create_pango_layout(imd->image, NULL); + layout = gtk_widget_create_pango_layout((GtkWidget *)pr, NULL); buf = g_strdup_printf("%d,%d\n(#%d)", x, y, - (x / imd->source_tile_width) + - (y / imd->source_tile_height * (imd->image_width/imd->source_tile_width + 1))); + (x / pr->source_tile_width) + + (y / pr->source_tile_height * (pr->image_width/pr->source_tile_width + 1))); pango_layout_set_text(layout, buf, -1); g_free(buf); - pango_layout_get_pixel_size(layout, &lw, &lh); + pixbuf_draw_layout(pixbuf, layout, (GtkWidget *)pr, 0, 0, 0, 0, 0, 255); - pixmap = gdk_pixmap_new(imd->widget->window, lw, lh, -1); - gdk_draw_rectangle(pixmap, imd->widget->style->black_gc, TRUE, 0, 0, lw, lh); - gdk_draw_layout(pixmap, imd->widget->style->white_gc, 0, 0, layout); g_object_unref(G_OBJECT(layout)); - lx = MAX(0, width / 2 - lw / 2); - ly = MAX(0, height / 2 - lh / 2); - lw = MIN(lw, width - lx); - lh = MIN(lh, height - ly); - gdk_pixbuf_get_from_drawable(pixbuf, pixmap, gdk_drawable_get_colormap(imd->image->window), - 0, 0, lx, ly, lw, lh); - g_object_unref(pixmap); - count++; } return TRUE; } -static void pan_window_dispose_tile_cb(ImageWindow *imd, gint x, gint y, gint width, gint height, - GdkPixbuf *pixbuf, gpointer data) +static void pan_window_dispose_tile_cb(PixbufRenderer *pr, gint x, gint y, + gint width, gint height, GdkPixbuf *pixbuf, gpointer data) { PanWindow *pw = data; GList *list; @@ -3112,11 +3094,13 @@ static void pan_window_message(PanWindow *pw, const gchar *text) g_free(buf); } -static ImageWindow *pan_window_active_image(PanWindow *pw) +static PixbufRenderer *pan_window_active_image(PanWindow *pw) { +#if 0 if (pw->fs) return pw->fs->imd; +#endif - return pw->imd; + return pw->pr; } static void pan_window_zoom_limit(PanWindow *pw) @@ -3149,7 +3133,7 @@ static void pan_window_zoom_limit(PanWindow *pw) break; } - image_zoom_set_limits(pw->imd, min, 32.0); + pixbuf_renderer_zoom_set_limits(pw->pr, min, 32.0); } static gint pan_window_layout_update_idle_cb(gpointer data) @@ -3203,10 +3187,10 @@ static gint pan_window_layout_update_idle_cb(gpointer data) { gdouble align; - image_set_image_as_tiles(pw->imd, width, height, - PAN_TILE_SIZE, PAN_TILE_SIZE, 8, - pan_window_request_tile_cb, - pan_window_dispose_tile_cb, pw, 1.0); + pixbuf_renderer_set_tiles(pw->pr, width, height, + PAN_TILE_SIZE, PAN_TILE_SIZE, 10, + pan_window_request_tile_cb, + pan_window_dispose_tile_cb, pw, 1.0); if (scroll_x == 0 && scroll_y == 0) { @@ -3216,7 +3200,7 @@ static gint pan_window_layout_update_idle_cb(gpointer data) { align = 0.5; } - image_scroll_to_point(pw->imd, scroll_x, scroll_y, align, align); + pixbuf_renderer_scroll_to_point(pw->pr, scroll_x, scroll_y, align, align); } pan_window_message(pw, NULL); @@ -3250,17 +3234,17 @@ static const gchar *pan_menu_click_path(PanWindow *pw) static void pan_window_menu_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data) { PanWindow *pw = data; - ImageWindow *imd; + PixbufRenderer *pr; - imd = pan_window_active_image(pw); - gdk_window_get_origin(imd->image->window, x, y); + pr = pan_window_active_image(pw); + gdk_window_get_origin(GTK_WIDGET(pr)->window, x, y); popup_menu_position_clamp(menu, x, y, 0); } static gint pan_window_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer data) { PanWindow *pw = data; - ImageWindow *imd; + PixbufRenderer *pr; const gchar *path; gint stop_signal = FALSE; GtkWidget *menu; @@ -3268,11 +3252,11 @@ static gint pan_window_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpoin gint y = 0; gint focused; - focused = (pw->fs || GTK_WIDGET_HAS_FOCUS(pw->imd->widget)); - - imd = pan_window_active_image(pw); + pr = pan_window_active_image(pw); path = pan_menu_click_path(pw); + focused = (pw->fs || GTK_WIDGET_HAS_FOCUS(GTK_WIDGET(pr))); + if (focused) { switch (event->keyval) @@ -3294,16 +3278,16 @@ static gint pan_window_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpoin stop_signal = TRUE; break; case GDK_Page_Up: case GDK_KP_Page_Up: - image_scroll(imd, 0, 0-imd->vis_height / 2); + pixbuf_renderer_scroll(pr, 0, 0 - pr->vis_height / 2); break; case GDK_Page_Down: case GDK_KP_Page_Down: - image_scroll(imd, 0, imd->vis_height / 2); + pixbuf_renderer_scroll(pr, 0, pr->vis_height / 2); break; case GDK_Home: case GDK_KP_Home: - image_scroll(imd, 0-imd->vis_width / 2, 0); + pixbuf_renderer_scroll(pr, 0 - pr->vis_width / 2, 0); break; case GDK_End: case GDK_KP_End: - image_scroll(imd, imd->vis_width / 2, 0); + pixbuf_renderer_scroll(pr, pr->vis_width / 2, 0); break; } } @@ -3312,31 +3296,31 @@ static gint pan_window_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpoin switch (event->keyval) { case '+': case '=': case GDK_KP_Add: - image_zoom_adjust(imd, ZOOM_INCREMENT); + pixbuf_renderer_zoom_adjust(pr, ZOOM_INCREMENT); break; case '-': case GDK_KP_Subtract: - image_zoom_adjust(imd, -ZOOM_INCREMENT); + pixbuf_renderer_zoom_adjust(pr, -ZOOM_INCREMENT); break; case 'Z': case 'z': case GDK_KP_Divide: case '1': - image_zoom_set(imd, 1.0); + pixbuf_renderer_zoom_set(pr, 1.0); break; case '2': - image_zoom_set(imd, 2.0); + pixbuf_renderer_zoom_set(pr, 2.0); break; case '3': - image_zoom_set(imd, 3.0); + pixbuf_renderer_zoom_set(pr, 3.0); break; case '4': - image_zoom_set(imd, 4.0); + pixbuf_renderer_zoom_set(pr, 4.0); break; case '7': - image_zoom_set(imd, -4.0); + pixbuf_renderer_zoom_set(pr, -4.0); break; case '8': - image_zoom_set(imd, -3.0); + pixbuf_renderer_zoom_set(pr, -3.0); break; case '9': - image_zoom_set(imd, -2.0); + pixbuf_renderer_zoom_set(pr, -2.0); break; case 'F': case 'f': case 'V': case 'v': @@ -3344,7 +3328,9 @@ static gint pan_window_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpoin stop_signal = TRUE; break; case 'I': case 'i': +#if 0 pan_overlay_toggle(pw); +#endif break; case GDK_Delete: case GDK_KP_Delete: break; @@ -3418,16 +3404,16 @@ static gint pan_window_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpoin n = 9; break; case 'C': case 'c': - if (path) file_util_copy(path, NULL, NULL, imd->widget); + if (path) file_util_copy(path, NULL, NULL, GTK_WIDGET(pr)); break; case 'M': case 'm': - if (path) file_util_move(path, NULL, NULL, imd->widget); + if (path) file_util_move(path, NULL, NULL, GTK_WIDGET(pr)); break; case 'R': case 'r': - if (path) file_util_rename(path, NULL, imd->widget); + if (path) file_util_rename(path, NULL, GTK_WIDGET(pr)); break; case 'D': case 'd': - if (path) file_util_delete(path, NULL, imd->widget); + if (path) file_util_delete(path, NULL, GTK_WIDGET(pr)); break; case 'P': case 'p': if (path) info_window_new(path, NULL); @@ -3460,7 +3446,7 @@ static gint pan_window_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpoin } else if (GTK_WIDGET_HAS_FOCUS(pw->search_entry)) { - gtk_widget_grab_focus(pw->imd->widget); + gtk_widget_grab_focus(GTK_WIDGET(pw->pr)); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(pw->search_button), FALSE); stop_signal = TRUE; } @@ -3473,7 +3459,7 @@ static gint pan_window_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpoin if (x != 0 || y!= 0) { keyboard_scroll_calc(&x, &y, event); - image_scroll(imd, x, y); + pixbuf_renderer_scroll(pr, x, y); } return stop_signal; @@ -3611,7 +3597,7 @@ static gint pan_search_by_path(PanWindow *pw, const gchar *path) } pan_info_update(pw, pi); - image_scroll_to_point(pw->imd, pi->x + pi->width / 2, pi->y + pi->height / 2, 0.5, 0.5); + pixbuf_renderer_scroll_to_point(pw->pr, pi->x + pi->width / 2, pi->y + pi->height / 2, 0.5, 0.5); buf = g_strdup_printf("%s ( %d / %d )", (path[0] == '/') ? _("path found") : _("filename found"), @@ -3659,7 +3645,7 @@ static gint pan_search_by_partial(PanWindow *pw, const gchar *text) } pan_info_update(pw, pi); - image_scroll_to_point(pw->imd, pi->x + pi->width / 2, pi->y + pi->height / 2, 0.5, 0.5); + pixbuf_renderer_scroll_to_point(pw->pr, pi->x + pi->width / 2, pi->y + pi->height / 2, 0.5, 0.5); buf = g_strdup_printf("%s ( %d / %d )", _("partial match"), @@ -3837,16 +3823,16 @@ static gint pan_search_by_date(PanWindow *pw, const gchar *text) { pan_info_update(pw, NULL); pan_calendar_update(pw, pi); - image_scroll_to_point(pw->imd, - pi->x + pi->width / 2, - pi->y + pi->height / 2, 0.5, 0.5); + pixbuf_renderer_scroll_to_point(pw->pr, + pi->x + pi->width / 2, + pi->y + pi->height / 2, 0.5, 0.5); } else if (pi) { pan_info_update(pw, pi); - image_scroll_to_point(pw->imd, - pi->x - PAN_FOLDER_BOX_BORDER * 5 / 2, - pi->y, 0.0, 0.5); + pixbuf_renderer_scroll_to_point(pw->pr, + pi->x - PAN_FOLDER_BOX_BORDER * 5 / 2, + pi->y, 0.0, 0.5); } if (month > 0) @@ -3937,8 +3923,7 @@ static void pan_search_toggle_cb(GtkWidget *button, gpointer data) *----------------------------------------------------------------------------- */ -static void button_cb(ImageWindow *imd, gint button, guint32 time, - gdouble x, gdouble y, guint state, gpointer data) +static void button_cb(PixbufRenderer *pr, GdkEventButton *event, gpointer data) { PanWindow *pw = data; PanItem *pi = NULL; @@ -3946,16 +3931,16 @@ static void button_cb(ImageWindow *imd, gint button, guint32 time, gint rx, ry; rx = ry = 0; - if (pw->imd->scale) + if (pr->scale) { - rx = (double)(pw->imd->x_scroll + x - pw->imd->x_offset) / pw->imd->scale; - ry = (double)(pw->imd->y_scroll + y - pw->imd->y_offset) / pw->imd->scale; + rx = (double)(pr->x_scroll + event->x - pr->x_offset) / pr->scale; + ry = (double)(pr->y_scroll + event->y - pr->y_offset) / pr->scale; } pi = pan_item_find_by_coord(pw, (pw->size > LAYOUT_SIZE_THUMB_LARGE) ? ITEM_IMAGE : ITEM_THUMB, rx, ry, NULL); - switch (button) + switch (event->button) { case 1: pan_info_update(pw, pi); @@ -3971,39 +3956,40 @@ static void button_cb(ImageWindow *imd, gint button, guint32 time, case 3: pan_info_update(pw, pi); menu = pan_popup_menu(pw); - gtk_menu_popup (GTK_MENU(menu), NULL, NULL, NULL, NULL, 3, time); + gtk_menu_popup (GTK_MENU(menu), NULL, NULL, NULL, NULL, 3, event->time); break; default: break; } } -static void scroll_cb(ImageWindow *imd, GdkScrollDirection direction, guint32 time, - gdouble x, gdouble y, guint state, gpointer data) +static void scroll_cb(PixbufRenderer *pr, GdkEventScroll *event, gpointer data) { #if 0 PanWindow *pw = data; #endif gint w, h; - w = imd->vis_width; - h = imd->vis_height; + w = pr->vis_width; + h = pr->vis_height; - if (!(state & GDK_SHIFT_MASK)) + if (!(event->state & GDK_SHIFT_MASK)) { w /= 3; h /= 3; } - if (state & GDK_CONTROL_MASK) + if (event->state & GDK_CONTROL_MASK) { - switch (direction) + switch (event->direction) { case GDK_SCROLL_UP: - image_zoom_adjust_at_point(imd, ZOOM_INCREMENT, x, y); + pixbuf_renderer_zoom_adjust_at_point(pr, ZOOM_INCREMENT, + (gint)event->x, (gint)event->y); break; case GDK_SCROLL_DOWN: - image_zoom_adjust_at_point(imd, -ZOOM_INCREMENT, x, y); + pixbuf_renderer_zoom_adjust_at_point(pr, -ZOOM_INCREMENT, + (gint)event->x, (gint)event->y); break; default: break; @@ -4011,19 +3997,19 @@ static void scroll_cb(ImageWindow *imd, GdkScrollDirection direction, guint32 ti } else { - switch (direction) + switch (event->direction) { case GDK_SCROLL_UP: - image_scroll(imd, 0, -h); + pixbuf_renderer_scroll(pr, 0, -h); break; case GDK_SCROLL_DOWN: - image_scroll(imd, 0, h); + pixbuf_renderer_scroll(pr, 0, h); break; case GDK_SCROLL_LEFT: - image_scroll(imd, -w, 0); + pixbuf_renderer_scroll(pr, -w, 0); break; case GDK_SCROLL_RIGHT: - image_scroll(imd, w, 0); + pixbuf_renderer_scroll(pr, w, 0); break; default: break; @@ -4031,18 +4017,22 @@ static void scroll_cb(ImageWindow *imd, GdkScrollDirection direction, guint32 ti } } -static void pan_image_set_buttons(PanWindow *pw, ImageWindow *imd) +static void pan_image_set_buttons(PanWindow *pw, PixbufRenderer *pr) { - image_set_button_func(imd, button_cb, pw); - image_set_scroll_func(imd, scroll_cb, pw); + g_signal_connect(G_OBJECT(pr), "clicked", + G_CALLBACK(button_cb), pw); + g_signal_connect(G_OBJECT(pr), "scroll_event", + G_CALLBACK(scroll_cb), pw); } +#if 0 static void pan_fullscreen_stop_func(FullScreenData *fs, gpointer data) { PanWindow *pw = data; pw->fs = NULL; } +#endif static void pan_fullscreen_toggle(PanWindow *pw, gint force_off) { @@ -4051,10 +4041,12 @@ static void pan_fullscreen_toggle(PanWindow *pw, gint force_off) if (pw->fs) { fullscreen_stop(pw->fs); - pw->imd = pw->imd_normal; + pw->pr = pw->pr_normal; } else { + printf("FIXME: fullscreen\n"); +#if 0 pw->fs = fullscreen_start(pw->window, pw->imd, pan_fullscreen_stop_func, pw); pan_image_set_buttons(pw, pw->fs->imd); @@ -4062,62 +4054,52 @@ static void pan_fullscreen_toggle(PanWindow *pw, gint force_off) G_CALLBACK(pan_window_key_press_cb), pw); pw->imd = pw->fs->imd; - } -} - -static void pan_overlay_toggle(PanWindow *pw) -{ - ImageWindow *imd; - - imd = pan_window_active_image(pw); -#if 0 - if (pw->overlay_id == -1) - { - pw->overlay_id = image_overlay_info_enable(imd); - } - else - { - image_overlay_info_disable(imd, pw->overlay_id); - pw->overlay_id = -1; - } #endif + } } -static void pan_window_image_update_cb(ImageWindow *imd, gpointer data) +static void pan_window_image_zoom_cb(PixbufRenderer *pr, gdouble zoom, gpointer data) { PanWindow *pw = data; gchar *text; +#if 0 text = image_zoom_get_as_text(imd); +#endif + text = g_strdup_printf("%.2f", zoom); gtk_label_set_text(GTK_LABEL(pw->label_zoom), text); g_free(text); } -static void pan_window_image_scroll_notify_cb(ImageWindow *imd, gint x, gint y, - gint width, gint height, gpointer data) +static void pan_window_image_scroll_notify_cb(PixbufRenderer *pr, gpointer data) { PanWindow *pw = data; GtkAdjustment *adj; + GdkRectangle rect; + gint width, height; + + pixbuf_renderer_get_visible_rect(pr, &rect); + pixbuf_renderer_get_image_size(pr, &width, &height); adj = gtk_range_get_adjustment(GTK_RANGE(pw->scrollbar_h)); - adj->page_size = (gdouble)imd->vis_width / imd->scale; + adj->page_size = (gdouble)rect.width; adj->page_increment = adj->page_size / 2.0; - adj->step_increment = 48.0 / imd->scale; + adj->step_increment = 48.0 / pr->scale; adj->lower = 0.0; - adj->upper = MAX((gdouble)width + adj->page_size, 1.0); - adj->value = (gdouble)x; + adj->upper = MAX((gdouble)width, 1.0); + adj->value = (gdouble)rect.x; pref_signal_block_data(pw->scrollbar_h, pw); gtk_adjustment_changed(adj); pref_signal_unblock_data(pw->scrollbar_h, pw); adj = gtk_range_get_adjustment(GTK_RANGE(pw->scrollbar_v)); - adj->page_size = (gdouble)imd->vis_height / imd->scale; + adj->page_size = (gdouble)rect.height; adj->page_increment = adj->page_size / 2.0; - adj->step_increment = 48.0 / imd->scale; + adj->step_increment = 48.0 / pr->scale; adj->lower = 0.0; - adj->upper = MAX((gdouble)height + adj->page_size, 1.0); - adj->value = (gdouble)y; + adj->upper = MAX((gdouble)height, 1.0); + adj->value = (gdouble)rect.y; pref_signal_block_data(pw->scrollbar_v, pw); gtk_adjustment_changed(adj); @@ -4131,11 +4113,11 @@ static void pan_window_scrollbar_h_value_cb(GtkRange *range, gpointer data) PanWindow *pw = data; gint x; - if (!pw->imd->scale) return; + if (!pw->pr->scale) return; x = (gint)gtk_range_get_value(range); - image_scroll_to_point(pw->imd, x, (gint)((gdouble)pw->imd->y_scroll / pw->imd->scale), 0.0, 0.0); + pixbuf_renderer_scroll_to_point(pw->pr, x, (gint)((gdouble)pw->pr->y_scroll / pw->pr->scale), 0.0, 0.0); } static void pan_window_scrollbar_v_value_cb(GtkRange *range, gpointer data) @@ -4143,11 +4125,11 @@ static void pan_window_scrollbar_v_value_cb(GtkRange *range, gpointer data) PanWindow *pw = data; gint y; - if (!pw->imd->scale) return; + if (!pw->pr->scale) return; y = (gint)gtk_range_get_value(range); - image_scroll_to_point(pw->imd, (gint)((gdouble)pw->imd->x_scroll / pw->imd->scale), y, 0.0, 0.0); + pixbuf_renderer_scroll_to_point(pw->pr, (gint)((gdouble)pw->pr->x_scroll / pw->pr->scale), y, 0.0, 0.0); } static void pan_window_layout_change_cb(GtkWidget *combo, gpointer data) @@ -4316,24 +4298,25 @@ static void pan_window_new_real(const gchar *path) gtk_table_set_row_spacings(GTK_TABLE(table), 2); gtk_table_set_col_spacings(GTK_TABLE(table), 2); - pw->imd = image_new(TRUE); - pw->imd_normal = pw->imd; + pw->pr = pixbuf_renderer_new(); + pw->pr_normal = pw->pr; - if (black_window_background) image_background_set_black(pw->imd, TRUE); - image_set_update_func(pw->imd, pan_window_image_update_cb, pw); + if (black_window_background) pixbuf_renderer_set_black(pw->pr, TRUE); - image_set_scroll_notify_func(pw->imd, pan_window_image_scroll_notify_cb, pw); + g_object_set(G_OBJECT(pw->pr), "zoom_2pass", TRUE, NULL); -#if 0 - gtk_box_pack_start(GTK_BOX(vbox), pw->imd->widget, TRUE, TRUE, 0); -#endif - gtk_table_attach(GTK_TABLE(table), pw->imd->widget, 0, 1, 0, 1, + g_signal_connect(G_OBJECT(pw->pr), "zoom", + G_CALLBACK(pan_window_image_zoom_cb), pw); + g_signal_connect(G_OBJECT(pw->pr), "scroll_notify", + G_CALLBACK(pan_window_image_scroll_notify_cb), pw); + + gtk_table_attach(GTK_TABLE(table), GTK_WIDGET(pw->pr), 0, 1, 0, 1, GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 0, 0); - gtk_widget_show(pw->imd->widget); + gtk_widget_show(GTK_WIDGET(pw->pr)); pan_window_dnd_init(pw); - pan_image_set_buttons(pw, pw->imd); + pan_image_set_buttons(pw, pw->pr); pw->scrollbar_h = gtk_hscrollbar_new(NULL); g_signal_connect(G_OBJECT(pw->scrollbar_h), "value_changed", @@ -4422,7 +4405,9 @@ static void pan_window_new_real(const gchar *path) pan_window_layout_update_idle(pw); - gtk_widget_grab_focus(pw->imd->widget); +#if 0 + gtk_widget_grab_focus(GTK_WIDGET(pw->pr)); +#endif gtk_widget_show(pw->window); pan_window_list = g_list_append(pan_window_list, pw); @@ -4563,21 +4548,21 @@ static void pan_zoom_in_cb(GtkWidget *widget, gpointer data) { PanWindow *pw = data; - image_zoom_adjust(pan_window_active_image(pw), ZOOM_INCREMENT); + pixbuf_renderer_zoom_adjust(pan_window_active_image(pw), ZOOM_INCREMENT); } static void pan_zoom_out_cb(GtkWidget *widget, gpointer data) { PanWindow *pw = data; - image_zoom_adjust(pan_window_active_image(pw), -ZOOM_INCREMENT); + pixbuf_renderer_zoom_adjust(pan_window_active_image(pw), -ZOOM_INCREMENT); } static void pan_zoom_1_1_cb(GtkWidget *widget, gpointer data) { PanWindow *pw = data; - image_zoom_set(pan_window_active_image(pw), 1.0); + pixbuf_renderer_zoom_set(pan_window_active_image(pw), 1.0); } static void pan_copy_cb(GtkWidget *widget, gpointer data) @@ -4586,7 +4571,7 @@ static void pan_copy_cb(GtkWidget *widget, gpointer data) const gchar *path; path = pan_menu_click_path(pw); - if (path) file_util_copy(path, NULL, NULL, pw->imd->widget); + if (path) file_util_copy(path, NULL, NULL, GTK_WIDGET(pw->pr)); } static void pan_move_cb(GtkWidget *widget, gpointer data) @@ -4595,7 +4580,7 @@ static void pan_move_cb(GtkWidget *widget, gpointer data) const gchar *path; path = pan_menu_click_path(pw); - if (path) file_util_move(path, NULL, NULL, pw->imd->widget); + if (path) file_util_move(path, NULL, NULL, GTK_WIDGET(pw->pr)); } static void pan_rename_cb(GtkWidget *widget, gpointer data) @@ -4604,7 +4589,7 @@ static void pan_rename_cb(GtkWidget *widget, gpointer data) const gchar *path; path = pan_menu_click_path(pw); - if (path) file_util_rename(path, NULL, pw->imd->widget); + if (path) file_util_rename(path, NULL, GTK_WIDGET(pw->pr)); } static void pan_delete_cb(GtkWidget *widget, gpointer data) @@ -4613,7 +4598,7 @@ static void pan_delete_cb(GtkWidget *widget, gpointer data) const gchar *path; path = pan_menu_click_path(pw); - if (path) file_util_delete(path, NULL, pw->imd->widget); + if (path) file_util_delete(path, NULL, GTK_WIDGET(pw->pr)); } static void pan_fullscreen_cb(GtkWidget *widget, gpointer data) @@ -4696,11 +4681,8 @@ static void pan_window_get_dnd_data(GtkWidget *widget, GdkDragContext *context, guint time, gpointer data) { PanWindow *pw = data; - ImageWindow *imd; - - if (gtk_drag_get_source_widget(context) == pw->imd->image) return; - imd = pw->imd; + if (gtk_drag_get_source_widget(context) == GTK_WIDGET(pw->pr)) return; if (info == TARGET_URI_LIST) { @@ -4765,21 +4747,21 @@ static void pan_window_set_dnd_data(GtkWidget *widget, GdkDragContext *context, static void pan_window_dnd_init(PanWindow *pw) { - ImageWindow *imd; + GtkWidget *widget; - imd = pw->imd; + widget = GTK_WIDGET(pw->pr); - gtk_drag_source_set(imd->image, GDK_BUTTON2_MASK, + gtk_drag_source_set(widget, GDK_BUTTON2_MASK, dnd_file_drag_types, dnd_file_drag_types_count, GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK); - g_signal_connect(G_OBJECT(imd->image), "drag_data_get", + g_signal_connect(G_OBJECT(widget), "drag_data_get", G_CALLBACK(pan_window_set_dnd_data), pw); - gtk_drag_dest_set(imd->image, + gtk_drag_dest_set(widget, GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP, dnd_file_drop_types, dnd_file_drop_types_count, GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK); - g_signal_connect(G_OBJECT(imd->image), "drag_data_received", + g_signal_connect(G_OBJECT(widget), "drag_data_received", G_CALLBACK(pan_window_get_dnd_data), pw); } diff --git a/src/pixbuf-renderer.c b/src/pixbuf-renderer.c new file mode 100644 index 00000000..93347e0f --- /dev/null +++ b/src/pixbuf-renderer.c @@ -0,0 +1,3180 @@ +/* + * GQview + * (C) 2005 John Ellis + * + * Author: John Ellis + * + * This software is released under the GNU General Public License (GNU GPL). + * Please read the included file COPYING for more information. + * This software comes with no warranty of any kind, use at your own risk! + */ + +#include +#include +#include +#include "pixbuf-renderer.h" +#include "intl.h" + +#include + + +/* comment this out if not using this from within GQview + * defining GQVIEW_BUILD does these things: + * - Sets the shift-click scroller pixbuf to a nice icon instead of a black box + */ +#define GQVIEW_BUILD 1 + + +#ifdef GQVIEW_BUILD + #include "pixbuf_util.h" +#endif + + +/* size to use when breaking up image pane for rendering */ +#define PR_TILE_SIZE 128 + +/* default size of tile cache (mb) */ +#define PR_CACHE_SIZE_DEFAULT 8 + +/* default min and max zoom */ +#define PR_ZOOM_MIN -32.0 +#define PR_ZOOM_MAX 32.0 + +/* distance to drag mouse to disable image flip */ +#define PR_DRAG_SCROLL_THRESHHOLD 4 + +/* increase pan rate when holding down shift */ +#define PR_PAN_SHIFT_MULTIPLIER 6 + +/* scroller config */ +#define PR_SCROLLER_UPDATES_PER_SEC 30 +#define PR_SCROLLER_DEAD_ZONE 6 + +/* alpha channel checkerboard background (same as gimp) */ +#define PR_ALPHA_CHECK1 0x00999999 +#define PR_ALPHA_CHECK2 0x00666666 +#define PR_ALPHA_CHECK_SIZE 16 + +/* when scaling image to below this size, use nearest pixel for scaling + * (below about 4, the other scale types become slow generating their conversion tables) + */ +#define PR_MIN_SCALE_SIZE 8 + + +typedef enum { + TILE_RENDER_NONE = 0, /* do nothing */ + TILE_RENDER_AREA, /* render an area of the tile */ + TILE_RENDER_ALL /* render the whole tile */ +} ImageTileRenderType; + +typedef struct _ImageTile ImageTile; +typedef struct _QueueData QueueData; + +struct _ImageTile +{ + GdkPixmap *pixmap; /* off screen buffer */ + GdkPixbuf *pixbuf; /* pixbuf area for zooming */ + gint x; /* x offset into image */ + gint y; /* y offset into image */ + gint w; /* width that is visible (may be less if at edge of image) */ + gint h; /* height '' */ + + gboolean blank; + +/* render_todo: (explanation) + NONE do nothing + AREA render area of tile, usually only used when loading an image + note: will jump to an ALL if render_done is not ALL. + ALL render entire tile, if never done before w/ ALL, for expose events *only* +*/ + + ImageTileRenderType render_todo; /* what to do (see above) */ + ImageTileRenderType render_done; /* highest that has been done before on tile */ + + QueueData *qd; + QueueData *qd2; + + guint size; /* est. memory used by pixmap and pixbuf */ +}; + +struct _QueueData +{ + ImageTile *it; + gint x; + gint y; + gint w; + gint h; + gboolean new_data; +}; + +typedef struct _SourceTile SourceTile; +struct _SourceTile +{ + gint x; + gint y; + GdkPixbuf *pixbuf; + gboolean blank; +}; + +typedef struct _OverlayData OverlayData; +struct _OverlayData +{ + gint id; + + GdkPixbuf *pixbuf; + + gint x; + gint y; + gint relative; /* x,y coordinates are relative, negative values start bottom right */ + + gint visible; + gint always; /* hide temporarily when scrolling */ +}; + +enum { + SIGNAL_ZOOM = 0, + SIGNAL_CLICKED, + SIGNAL_SCROLL_NOTIFY, + SIGNAL_COUNT +}; + +enum { + PROP_0, + PROP_ZOOM_MIN, + PROP_ZOOM_MAX, + PROP_ZOOM_QUALITY, + PROP_ZOOM_2PASS, + PROP_ZOOM_EXPAND, + PROP_DITHER_QUALITY, + PROP_SCROLL_RESET, + PROP_DELAY_FLIP, + PROP_LOADING, + PROP_CACHE_SIZE_DISPLAY, + PROP_CACHE_SIZE_TILES, + PROP_WINDOW_FIT, + PROP_WINDOW_LIMIT, + PROP_WINDOW_LIMIT_VALUE +}; + + + +static guint signals[SIGNAL_COUNT] = { 0 }; +static GtkEventBoxClass *parent_class = NULL; + + + +static void pixbuf_renderer_class_init(PixbufRendererClass *class); +static void pixbuf_renderer_init(PixbufRenderer *pr); +static void pixbuf_renderer_finalize(GObject *object); +static void pixbuf_renderer_set_property(GObject *object, guint prop_id, + const GValue *value, GParamSpec *pspec); +static void pixbuf_renderer_get_property(GObject *object, guint prop_id, + GValue *value, GParamSpec *pspec); +static gint pixbuf_renderer_expose(GtkWidget *widget, GdkEventExpose *event); + + +static void pr_overlay_list_clear(PixbufRenderer *pr); +static void pr_scroller_timer_set(PixbufRenderer *pr, gint start); +static void pr_border_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h); + + +static void pr_source_tile_free_all(PixbufRenderer *pr); +static void pr_tile_free_all(PixbufRenderer *pr); +static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h); +static gint pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it); +static void pr_queue_clear(PixbufRenderer *pr); +static void pr_queue_merge(QueueData *parent, QueueData *qd); +static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h, + gint clamp, ImageTileRenderType render, gint new_data); + +static void pr_redraw(PixbufRenderer *pr, gint new_data); + +static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom, + gint force, gint blank, gint new, + gint center_point, gint px, gint py); + +static void pr_signals_connect(PixbufRenderer *pr); +static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data); +static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area); + + +/* + *------------------------------------------------------------------- + * Pixbuf Renderer object + *------------------------------------------------------------------- + */ + +GType pixbuf_renderer_get_type(void) +{ + static GType pixbuf_renderer_type = 0; + + if (!pixbuf_renderer_type) + { + static const GTypeInfo pixbuf_renderer_info = + { + sizeof(PixbufRendererClass), + NULL, /* base_init */ + NULL, /* base_finalize */ + (GClassInitFunc)pixbuf_renderer_class_init, + NULL, /* class_finalize */ + NULL, /* class_data */ + sizeof(PixbufRenderer), + 0, /* n_preallocs */ + (GInstanceInitFunc)pixbuf_renderer_init, + }; + + pixbuf_renderer_type = g_type_register_static(GTK_TYPE_EVENT_BOX, "PixbufRenderer", + &pixbuf_renderer_info, 0); + } + + return pixbuf_renderer_type; +} + +static void pixbuf_renderer_class_init(PixbufRendererClass *class) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS(class); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class); + + parent_class = g_type_class_peek_parent(class); + + gobject_class->set_property = pixbuf_renderer_set_property; + gobject_class->get_property = pixbuf_renderer_get_property; + + gobject_class->finalize = pixbuf_renderer_finalize; + + widget_class->expose_event = pixbuf_renderer_expose; + + g_object_class_install_property(gobject_class, + PROP_ZOOM_MIN, + g_param_spec_double("zoom_min", + "Zoom minimum", + NULL, + -1000.0, + 1000.0, + PR_ZOOM_MIN, + G_PARAM_READABLE | G_PARAM_WRITABLE)); + + g_object_class_install_property(gobject_class, + PROP_ZOOM_MAX, + g_param_spec_double("zoom_max", + "Zoom maximum", + NULL, + -1000.0, + 1000.0, + PR_ZOOM_MIN, + G_PARAM_READABLE | G_PARAM_WRITABLE)); + + g_object_class_install_property(gobject_class, + PROP_ZOOM_QUALITY, + g_param_spec_uint("zoom_quality", + "Zoom quality", + NULL, + GDK_INTERP_NEAREST, + GDK_INTERP_HYPER, + GDK_INTERP_BILINEAR, + G_PARAM_READABLE | G_PARAM_WRITABLE)); + + g_object_class_install_property(gobject_class, + PROP_ZOOM_2PASS, + g_param_spec_boolean("zoom_2pass", + "2 pass zoom", + NULL, + TRUE, + G_PARAM_READABLE | G_PARAM_WRITABLE)); + + g_object_class_install_property(gobject_class, + PROP_ZOOM_EXPAND, + g_param_spec_boolean("zoom_expand", + "Expand image in autozoom.", + NULL, + FALSE, + G_PARAM_READABLE | G_PARAM_WRITABLE)); + + g_object_class_install_property(gobject_class, + PROP_DITHER_QUALITY, + g_param_spec_uint("dither_quality", + "Dither quality", + NULL, + GDK_RGB_DITHER_NONE, + GDK_RGB_DITHER_MAX, + GDK_RGB_DITHER_NORMAL, + G_PARAM_READABLE | G_PARAM_WRITABLE)); + + g_object_class_install_property(gobject_class, + PROP_SCROLL_RESET, + g_param_spec_uint("scroll_reset", + "New image scroll reset", + NULL, + PR_SCROLL_RESET_TOPLEFT, + PR_SCROLL_RESET_NOCHANGE, + PR_SCROLL_RESET_TOPLEFT, + G_PARAM_READABLE | G_PARAM_WRITABLE)); + + g_object_class_install_property(gobject_class, + PROP_DELAY_FLIP, + g_param_spec_boolean("delay_flip", + "Delay image update", + NULL, + FALSE, + G_PARAM_READABLE | G_PARAM_WRITABLE)); + + g_object_class_install_property(gobject_class, + PROP_LOADING, + g_param_spec_boolean("loading", + "Image actively loading", + NULL, + FALSE, + G_PARAM_READABLE | G_PARAM_WRITABLE)); + + g_object_class_install_property(gobject_class, + PROP_CACHE_SIZE_DISPLAY, + g_param_spec_uint("cache_display", + "Display cache size MB", + NULL, + 0, + 128, + PR_CACHE_SIZE_DEFAULT, + G_PARAM_READABLE | G_PARAM_WRITABLE)); + + g_object_class_install_property(gobject_class, + PROP_CACHE_SIZE_TILES, + g_param_spec_uint("cache_tiles", + "Tile cache count", + "Number of tiles to retain in memory at any one time.", + 0, + 256, + PR_CACHE_SIZE_DEFAULT, + G_PARAM_READABLE | G_PARAM_WRITABLE)); + + g_object_class_install_property(gobject_class, + PROP_WINDOW_FIT, + g_param_spec_boolean("window_fit", + "Fit window to image size", + NULL, + FALSE, + G_PARAM_READABLE | G_PARAM_WRITABLE)); + + g_object_class_install_property(gobject_class, + PROP_WINDOW_LIMIT, + g_param_spec_boolean("window_limit", + "Limit size of parent window", + NULL, + FALSE, + G_PARAM_READABLE | G_PARAM_WRITABLE)); + + g_object_class_install_property(gobject_class, + PROP_WINDOW_LIMIT_VALUE, + g_param_spec_uint("window_limit_value", + "Size limit of parent window", + NULL, + 10, + 150, + 100, + G_PARAM_READABLE | G_PARAM_WRITABLE)); + + signals[SIGNAL_ZOOM] = + g_signal_new("zoom", + G_OBJECT_CLASS_TYPE(gobject_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET(PixbufRendererClass, zoom), + NULL, NULL, + g_cclosure_marshal_VOID__DOUBLE, + G_TYPE_NONE, 1, + G_TYPE_DOUBLE); + + signals[SIGNAL_CLICKED] = + g_signal_new("clicked", + G_OBJECT_CLASS_TYPE(gobject_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET(PixbufRendererClass, clicked), + NULL, NULL, + g_cclosure_marshal_VOID__BOXED, + G_TYPE_NONE, 1, + GDK_TYPE_EVENT); + + signals[SIGNAL_SCROLL_NOTIFY] = + g_signal_new("scroll-notify", + G_OBJECT_CLASS_TYPE(gobject_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET(PixbufRendererClass, scroll_notify), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); +} + +static void pixbuf_renderer_init(PixbufRenderer *pr) +{ + GtkWidget *box; + + box = GTK_WIDGET(pr); + + pr->zoom_min = PR_ZOOM_MIN; + pr->zoom_max = PR_ZOOM_MAX; + pr->zoom_quality = GDK_INTERP_BILINEAR; + pr->zoom_2pass = FALSE; + + pr->zoom = 1.0; + pr->scale = 1.0; + + pr->dither_quality = GDK_RGB_DITHER_NORMAL; + + pr->scroll_reset = PR_SCROLL_RESET_TOPLEFT; + + pr->draw_idle_id = -1; + + pr->tile_width = PR_TILE_SIZE; + pr->tile_height = PR_TILE_SIZE; + + pr->tiles = NULL; + pr->tile_cache_size = 0; + + pr->tile_cache_max = PR_CACHE_SIZE_DEFAULT; + + pr->scroller_id = -1; + pr->scroller_overlay = -1; + + pr->source_tiles_enabled = FALSE; + pr->source_tiles = NULL; + + gtk_widget_set_double_buffered(box, FALSE); + g_signal_connect_after(G_OBJECT(box), "size_allocate", + G_CALLBACK(pr_size_cb), pr); + + pr_signals_connect(pr); +} + +static void pixbuf_renderer_finalize(GObject *object) +{ + PixbufRenderer *pr; + + pr = PIXBUF_RENDERER(object); + + pr_queue_clear(pr); + pr_tile_free_all(pr); + + if (pr->pixbuf) g_object_unref(pr->pixbuf); + + pr_scroller_timer_set(pr, FALSE); + pr_overlay_list_clear(pr); + + pr_source_tile_free_all(pr); +} + +PixbufRenderer* pixbuf_renderer_new(void) +{ + return g_object_new(TYPE_PIXBUF_RENDERER, NULL); +} + +static void pixbuf_renderer_set_property(GObject *object, guint prop_id, + const GValue *value, GParamSpec *pspec) +{ + PixbufRenderer *pr; + + pr = PIXBUF_RENDERER(object); + + switch (prop_id) + { + case PROP_ZOOM_MIN: + pr->zoom_min = g_value_get_double(value); + break; + case PROP_ZOOM_MAX: + pr->zoom_max = g_value_get_double(value); + break; + case PROP_ZOOM_QUALITY: + pr->zoom_quality = g_value_get_uint(value); + break; + case PROP_ZOOM_2PASS: + pr->zoom_2pass = g_value_get_boolean(value); + break; + case PROP_ZOOM_EXPAND: + pr->zoom_expand = g_value_get_boolean(value); + break; + case PROP_DITHER_QUALITY: + pr->dither_quality = g_value_get_uint(value); + break; + case PROP_SCROLL_RESET: + pr->scroll_reset = g_value_get_uint(value); + break; + case PROP_DELAY_FLIP: + pr->delay_flip = g_value_get_boolean(value); + break; + case PROP_LOADING: + pr->loading = g_value_get_boolean(value); + break; + case PROP_CACHE_SIZE_DISPLAY: + pr->tile_cache_max = g_value_get_uint(value); + break; + case PROP_CACHE_SIZE_TILES: + pr->source_tiles_cache_size = g_value_get_uint(value); + break; + case PROP_WINDOW_FIT: + pr->window_fit = g_value_get_boolean(value); + break; + case PROP_WINDOW_LIMIT: + pr->window_limit = g_value_get_boolean(value); + break; + case PROP_WINDOW_LIMIT_VALUE: + pr->window_limit_size = g_value_get_uint(value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +static void pixbuf_renderer_get_property(GObject *object, guint prop_id, + GValue *value, GParamSpec *pspec) +{ + PixbufRenderer *pr; + + pr = PIXBUF_RENDERER(object); + + switch (prop_id) + { + case PROP_ZOOM_MIN: + g_value_set_double(value, pr->zoom_min); + break; + case PROP_ZOOM_MAX: + g_value_set_double(value, pr->zoom_max); + break; + case PROP_ZOOM_QUALITY: + g_value_set_uint(value, pr->zoom_quality); + break; + case PROP_ZOOM_2PASS: + g_value_set_boolean(value, pr->zoom_2pass); + break; + case PROP_ZOOM_EXPAND: + g_value_set_boolean(value, pr->zoom_expand); + break; + case PROP_DITHER_QUALITY: + g_value_set_uint(value, pr->dither_quality); + break; + case PROP_SCROLL_RESET: + g_value_set_uint(value, pr->scroll_reset); + break; + case PROP_DELAY_FLIP: + g_value_set_boolean(value, pr->delay_flip); + break; + case PROP_LOADING: + g_value_set_boolean(value, pr->loading); + break; + case PROP_CACHE_SIZE_DISPLAY: + g_value_set_uint(value, pr->tile_cache_max); + break; + case PROP_CACHE_SIZE_TILES: + g_value_set_uint(value, pr->source_tiles_cache_size); + break; + case PROP_WINDOW_FIT: + g_value_set_boolean(value, pr->window_fit); + break; + case PROP_WINDOW_LIMIT: + g_value_set_boolean(value, pr->window_limit); + break; + case PROP_WINDOW_LIMIT_VALUE: + g_value_set_uint(value, pr->window_limit_size); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +static gint pixbuf_renderer_expose(GtkWidget *widget, GdkEventExpose *event) +{ + if (GTK_WIDGET_DRAWABLE(widget)) + { + if (!GTK_WIDGET_NO_WINDOW(widget)) + { + pixbuf_renderer_paint(PIXBUF_RENDERER(widget), &event->area); + } + } + + return FALSE; +} + +/* + *------------------------------------------------------------------- + * misc utilities + *------------------------------------------------------------------- + */ + +static void widget_set_cursor(GtkWidget *widget, gint icon) +{ + GdkCursor *cursor; + + if (!widget->window) return; + + if (icon == -1) + { + cursor = NULL; + } + else + { + cursor = gdk_cursor_new (icon); + } + + gdk_window_set_cursor(widget->window, cursor); + + if (cursor) gdk_cursor_unref(cursor); +} + +static gint pixmap_calc_size(GdkPixmap *pixmap) +{ + gint w, h, d; + + d = gdk_drawable_get_depth(pixmap); + gdk_drawable_get_size(pixmap, &w, &h); + return w * h * (d / 8); +} + +static gint util_clip_region(gint x, gint y, gint w, gint h, + gint clip_x, gint clip_y, gint clip_w, gint clip_h, + gint *rx, gint *ry, gint *rw, gint *rh) +{ + if (clip_x + clip_w <= x || + clip_x >= x + w || + clip_y + clip_h <= y || + clip_y >= y + h) + { + return FALSE; + } + + *rx = MAX(x, clip_x); + *rw = MIN((x + w), (clip_x + clip_w)) - *rx; + + *ry = MAX(y, clip_y); + *rh = MIN((y + h), (clip_y + clip_h)) - *ry; + + return TRUE; +} + +static gint pr_parent_window_sizable(PixbufRenderer *pr) +{ + if (!pr->parent_window) return FALSE; + if (!pr->window_fit) return FALSE; + if (!GTK_WIDGET(pr)->window) return FALSE; +#if 0 + if (window_maximized(pr->top_window)) return FALSE; +#endif + + return TRUE; +} + +static gint pr_parent_window_resize(PixbufRenderer *pr, gint w, gint h) +{ + GtkWidget *widget; + GtkWidget *parent; + gint ww, wh; + + if (!pr_parent_window_sizable(pr)) return FALSE; + + if (pr->window_limit) + { + gint sw = gdk_screen_width() * pr->window_limit_size / 100; + gint sh = gdk_screen_height() * pr->window_limit_size / 100; + + if (w > sw) w = sw; + if (h > sh) h = sh; + } + + widget = GTK_WIDGET(pr); + parent = GTK_WIDGET(pr->parent_window); + + w += (parent->allocation.width - widget->allocation.width); + h += (parent->allocation.height - widget->allocation.height); + + gdk_drawable_get_size(parent->window, &ww, &wh); + if (w == ww && h == wh) return FALSE; + + gdk_window_resize(parent->window, w, h); + + return TRUE; +} + +void pixbuf_renderer_set_parent(PixbufRenderer *pr, GtkWindow *window) +{ + g_return_if_fail(IS_PIXBUF_RENDERER(pr)); + g_return_if_fail(window == NULL || GTK_IS_WINDOW(window)); + + pr->parent_window = GTK_WIDGET(window); +} + +GtkWindow *pixbuf_renderer_get_parent(PixbufRenderer *pr) +{ + g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL); + + return GTK_WINDOW(pr->parent_window); +} + + +/* + *------------------------------------------------------------------- + * overlays + *------------------------------------------------------------------- + */ + +static void pr_overlay_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h) +{ + GtkWidget *box; + GList *work; + + box = GTK_WIDGET(pr); + + work = pr->overlay_list; + while (work) + { + OverlayData *od; + gint px, py, pw, ph; + gint rx, ry, rw, rh; + + od = work->data; + work = work->next; + + if (!od->visible) continue; + + pw = gdk_pixbuf_get_width(od->pixbuf); + ph = gdk_pixbuf_get_height(od->pixbuf); + px = od->x; + py = od->y; + + if (od->relative) + { + if (px < 0) px = pr->window_width - pw + px; + if (py < 0) py = pr->window_height - ph + py; + } + + if (util_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh)) + { + gdk_draw_pixbuf(box->window, + box->style->fg_gc[GTK_WIDGET_STATE(box)], + od->pixbuf, + rx - px, ry - py, + rx, ry, rw, rh, + pr->dither_quality, rx, ry); + } + } +} + +static void pr_overlay_queue_draw(PixbufRenderer *pr, OverlayData *od, gint hidden) +{ + gint x, y, w, h; + gint old_vis; + + w = gdk_pixbuf_get_width(od->pixbuf); + h = gdk_pixbuf_get_height(od->pixbuf); + x = od->x; + y = od->y; + + if (od->relative) + { + if (x < 0) x = pr->window_width - w + x; + if (y < 0) y = pr->window_height - h + y; + } + + pr_queue(pr, pr->x_scroll - pr->x_offset + x, + pr->y_scroll - pr->y_offset + y, + w, h, + FALSE, TILE_RENDER_ALL, FALSE); + + old_vis = od->visible; + if (hidden) od->visible = FALSE; + pr_border_draw(pr, x, y, w, h); + od->visible = old_vis; +} + +static void pr_overlay_queue_all(PixbufRenderer *pr) +{ + GList *work; + + work = pr->overlay_list; + while (work) + { + OverlayData *od = work->data; + work = work->next; + + pr_overlay_queue_draw(pr, od, FALSE); + } +} + +static OverlayData *pr_overlay_find(PixbufRenderer *pr, gint id) +{ + GList *work; + + work = pr->overlay_list; + while (work) + { + OverlayData *od = work->data; + work = work->next; + + if (od->id == id) return od; + } + + return NULL; +} + +gint pixbuf_renderer_overlay_add(PixbufRenderer *pr, GdkPixbuf *pixbuf, gint x, gint y, + gint relative, gint always) +{ + OverlayData *od; + gint id; + + g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1); + g_return_val_if_fail(pixbuf != NULL, -1); + + id = 1; + while (pr_overlay_find(pr, id)) id++; + + od = g_new0(OverlayData, 1); + od->id = id; + od->pixbuf = pixbuf; + g_object_ref(G_OBJECT(od->pixbuf)); + od->x = x; + od->y = y; + od->relative = relative; + od->visible = TRUE; + od->always = always; + + pr->overlay_list = g_list_append(pr->overlay_list, od); + + pr_overlay_queue_draw(pr, od, FALSE); + + return od->id; +} + +static void pr_overlay_free(PixbufRenderer *pr, OverlayData *od) +{ + pr->overlay_list = g_list_remove(pr->overlay_list, od); + + if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf)); + g_free(od); +} + +static void pr_overlay_list_clear(PixbufRenderer *pr) +{ + while (pr->overlay_list) + { + OverlayData *od; + + od = pr->overlay_list->data; + pr_overlay_free(pr, od); + } +} + +void pixbuf_renderer_overlay_set(PixbufRenderer *pr, gint id, GdkPixbuf *pixbuf, gint x, gint y) +{ + OverlayData *od; + + g_return_if_fail(IS_PIXBUF_RENDERER(pr)); + + od = pr_overlay_find(pr, id); + if (!od) return; + + if (pixbuf) + { + pr_overlay_queue_draw(pr, od, TRUE); + + g_object_ref(G_OBJECT(pixbuf)); + g_object_unref(G_OBJECT(od->pixbuf)); + od->pixbuf = pixbuf; + + od->x = x; + od->y = y; + + pr_overlay_queue_draw(pr, od, FALSE); + } + else + { + pr_overlay_queue_draw(pr, od, TRUE); + pr_overlay_free(pr, od); + } +} + +gint pixbuf_renderer_overlay_get(PixbufRenderer *pr, gint id, GdkPixbuf **pixbuf, gint *x, gint *y) +{ + OverlayData *od; + + g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE); + + od = pr_overlay_find(pr, id); + if (!od) return FALSE; + + if (pixbuf) *pixbuf = od->pixbuf; + if (x) *x = od->x; + if (y) *y = od->y; + + return TRUE; +} + +void pixbuf_renderer_overlay_remove(PixbufRenderer *pr, gint id) +{ + pixbuf_renderer_overlay_set(pr, id, NULL, 0, 0); +} + +/* + *------------------------------------------------------------------- + * scroller overlay + *------------------------------------------------------------------- + */ + + +static gboolean pr_scroller_update_cb(gpointer data) +{ + PixbufRenderer *pr = data; + gint x, y; + gint xinc, yinc; + + /* this was a simple scroll by difference between scroller and mouse position, + * but all this math results in a smoother result and accounts for a dead zone. + */ + + if (abs(pr->scroller_xpos - pr->scroller_x) < PR_SCROLLER_DEAD_ZONE) + { + x = 0; + } + else + { + gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC; + x = (pr->scroller_xpos - pr->scroller_x) / 2 * PR_SCROLLER_UPDATES_PER_SEC; + x += (x > 0) ? -shift : shift; + } + + if (abs(pr->scroller_ypos - pr->scroller_y) < PR_SCROLLER_DEAD_ZONE) + { + y = 0; + } + else + { + gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC; + y = (pr->scroller_ypos - pr->scroller_y) / 2 * PR_SCROLLER_UPDATES_PER_SEC; + y += (y > 0) ? -shift : shift; + } + + if (abs(x) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC) + { + xinc = x; + } + else + { + xinc = pr->scroller_xinc; + + if (x >= 0) + { + if (xinc < 0) xinc = 0; + if (x < xinc) xinc = x; + if (x > xinc) xinc = MIN(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x); + } + else + { + if (xinc > 0) xinc = 0; + if (x > xinc) xinc = x; + if (x < xinc) xinc = MAX(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x); + } + } + + if (abs(y) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC) + { + yinc = y; + } + else + { + yinc = pr->scroller_yinc; + + if (y >= 0) + { + if (yinc < 0) yinc = 0; + if (y < yinc) yinc = y; + if (y > yinc) yinc = MIN(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y); + } + else + { + if (yinc > 0) yinc = 0; + if (y > yinc) yinc = y; + if (y < yinc) yinc = MAX(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y); + } + } + + pr->scroller_xinc = xinc; + pr->scroller_yinc = yinc; + + xinc = xinc / PR_SCROLLER_UPDATES_PER_SEC; + yinc = yinc / PR_SCROLLER_UPDATES_PER_SEC; + + pixbuf_renderer_scroll(pr, xinc, yinc); + + return TRUE; +} + +static void pr_scroller_timer_set(PixbufRenderer *pr, gint start) +{ + if (pr->scroller_id != -1) + { + g_source_remove(pr->scroller_id); + pr->scroller_id = -1; + } + + if (start) + { + pr->scroller_id = g_timeout_add(1000 / PR_SCROLLER_UPDATES_PER_SEC, + pr_scroller_update_cb, pr); + } +} + +static void pr_scroller_start(PixbufRenderer *pr, gint x, gint y) +{ + if (pr->scroller_overlay == -1) + { + GdkPixbuf *pixbuf; + gint w, h; + +#ifdef GQVIEW_BUILD + pixbuf = pixbuf_inline(PIXBUF_INLINE_SCROLLER); +#else + pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 32, 32); + gdk_pixbuf_fill(pixbuf, 0x000000ff); +#endif + w = gdk_pixbuf_get_width(pixbuf); + h = gdk_pixbuf_get_height(pixbuf); + + pr->scroller_overlay = pixbuf_renderer_overlay_add(pr, pixbuf, x - w / 2, y - h / 2, FALSE, TRUE); + g_object_unref(pixbuf); + } + + pr->scroller_x = x; + pr->scroller_y = y; + pr->scroller_xpos = x; + pr->scroller_ypos = y; + + pr_scroller_timer_set(pr, TRUE); +} + +static void pr_scroller_stop(PixbufRenderer *pr) +{ + if (pr->scroller_id == -1) return; + + pixbuf_renderer_overlay_remove(pr, pr->scroller_overlay); + pr->scroller_overlay = -1; + + pr_scroller_timer_set(pr, FALSE); +} + +/* + *------------------------------------------------------------------- + * borders + *------------------------------------------------------------------- + */ + +static void pr_border_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h) +{ + GtkWidget *box; + gint rx, ry, rw, rh; + + box = GTK_WIDGET(pr); + + if (!box->window) return; + + if (!pr->pixbuf && !pr->source_tiles_enabled) + { + if (util_clip_region(x, y, w, h, + 0, 0, + pr->window_width, pr->window_height, + &rx, &ry, &rw, &rh)) + { + gdk_window_clear_area(box->window, rx, ry, rw, rh); + pr_overlay_draw(pr, rx, ry, rw, rh); + } + return; + } + + if (pr->vis_width < pr->window_width) + { + if (pr->x_offset > 0 && + util_clip_region(x, y, w, h, + 0, 0, + pr->x_offset, pr->window_height, + &rx, &ry, &rw, &rh)) + { + gdk_window_clear_area(box->window, rx, ry, rw, rh); + pr_overlay_draw(pr, rx, ry, rw, rh); + } + if (pr->window_width - pr->vis_width - pr->x_offset > 0 && + util_clip_region(x, y, w, h, + pr->x_offset + pr->vis_width, 0, + pr->window_width - pr->vis_width - pr->x_offset, pr->window_height, + &rx, &ry, &rw, &rh)) + { + gdk_window_clear_area(box->window, rx, ry, rw, rh); + pr_overlay_draw(pr, rx, ry, rw, rh); + } + } + if (pr->vis_height < pr->window_height) + { + if (pr->y_offset > 0 && + util_clip_region(x, y, w, h, + pr->x_offset, 0, + pr->vis_width, pr->y_offset, + &rx, &ry, &rw, &rh)) + { + gdk_window_clear_area(box->window, rx, ry, rw, rh); + pr_overlay_draw(pr, rx, ry, rw, rh); + } + if (pr->window_height - pr->vis_height - pr->y_offset > 0 && + util_clip_region(x, y, w, h, + pr->x_offset, pr->y_offset + pr->vis_height, + pr->vis_width, pr->window_height - pr->vis_height - pr->y_offset, + &rx, &ry, &rw, &rh)) + { + gdk_window_clear_area(box->window, rx, ry, rw, rh); + pr_overlay_draw(pr, rx, ry, rw, rh); + } + } +} + +static void pr_border_clear(PixbufRenderer *pr) +{ + pr_border_draw(pr, 0, 0, pr->window_width, pr->window_height); +} + +void pixbuf_renderer_set_color(PixbufRenderer *pr, GdkColor *color) +{ + GtkStyle *style; + GtkWidget *widget; + + g_return_if_fail(IS_PIXBUF_RENDERER(pr)); + + widget = GTK_WIDGET(pr); + + style = gtk_style_copy(gtk_widget_get_style(widget)); + g_object_ref(G_OBJECT(style)); + + if (color) + { + GdkColor *slot; + + slot = &style->bg[GTK_STATE_NORMAL]; + + slot->red = color->red; + slot->green = color->green; + slot->blue = color->blue; + } + + gtk_widget_set_style(widget, style); + g_object_unref(G_OBJECT(style)); + + if (GTK_WIDGET_VISIBLE(widget)) pr_border_clear(pr); +} + +void pixbuf_renderer_set_black(PixbufRenderer *pr, gint black) +{ + GdkColor color = { 0, 0, 0, 0}; + + pixbuf_renderer_set_color(pr, &color); +} + + +/* + *------------------------------------------------------------------- + * source tiles + *------------------------------------------------------------------- + */ + +static void pr_source_tile_free(SourceTile *st) +{ + if (!st) return; + + if (st->pixbuf) g_object_unref(st->pixbuf); + g_free(st); +} + +static void pr_source_tile_free_all(PixbufRenderer *pr) +{ + GList *work; + + work = pr->source_tiles; + while (work) + { + SourceTile *st; + + st = work->data; + work = work->next; + + pr_source_tile_free(st); + } + + g_list_free(pr->source_tiles); + pr->source_tiles = NULL; +} + +static void pr_source_tile_unset(PixbufRenderer *pr) +{ + pr_source_tile_free_all(pr); + pr->source_tiles_enabled = FALSE; +} + +static gint pr_source_tile_visible(PixbufRenderer *pr, SourceTile *st) +{ + gint x1, y1, x2, y2; + + if (!st) return FALSE; + + x1 = (pr->x_scroll / PR_TILE_SIZE) * PR_TILE_SIZE; + y1 = (pr->y_scroll / PR_TILE_SIZE) * PR_TILE_SIZE; + x2 = ((pr->x_scroll + pr->vis_width) / PR_TILE_SIZE) * PR_TILE_SIZE + PR_TILE_SIZE; + y2 = ((pr->y_scroll + pr->vis_height) / PR_TILE_SIZE) * PR_TILE_SIZE + PR_TILE_SIZE; + + return !((double)st->x * pr->scale > (double)x2 || + (double)(st->x + pr->source_tile_width) * pr->scale < (double)x1 || + (double)st->y * pr->scale > (double)x2 || + (double)(st->y + pr->source_tile_height) * pr->scale < (double)y1); +} + +static SourceTile *pr_source_tile_new(PixbufRenderer *pr, gint x, gint y) +{ + SourceTile *st = NULL; + gint count; + + g_return_val_if_fail(pr->source_tile_width >= 1 && pr->source_tile_height >= 1, NULL); + + if (pr->source_tiles_cache_size < 4) pr->source_tiles_cache_size = 4; + + count = g_list_length(pr->source_tiles); + if (count >= pr->source_tiles_cache_size) + { + GList *work; + + work = g_list_last(pr->source_tiles); + while (work && count >= pr->source_tiles_cache_size) + { + SourceTile *needle; + + needle = work->data; + work = work->prev; + + if (!pr_source_tile_visible(pr, needle)) + { + pr->source_tiles = g_list_remove(pr->source_tiles, needle); + + if (pr->func_tile_dispose) + { + pr->func_tile_dispose(pr, needle->x, needle->y, + pr->source_tile_width, pr->source_tile_height, + needle->pixbuf, pr->func_tile_data); + } + + if (!st) + { + st = needle; + } + else + { + pr_source_tile_free(needle); + } + + count--; + } + } + } + + if (!st) + { + st = g_new0(SourceTile, 1); + st->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, + pr->source_tile_width, pr->source_tile_height); + } + + st->x = (x / pr->source_tile_width) * pr->source_tile_width; + st->y = (y / pr->source_tile_height) * pr->source_tile_height; + st->blank = TRUE; + + pr->source_tiles = g_list_prepend(pr->source_tiles, st); + + return st; +} + +static SourceTile *pr_source_tile_request(PixbufRenderer *pr, gint x, gint y) +{ + SourceTile *st; + + st = pr_source_tile_new(pr, x, y); + if (!st) return NULL; + + if (pr->func_tile_request && + pr->func_tile_request(pr, st->x, st->y, + pr->source_tile_width, pr->source_tile_height, st->pixbuf, pr->func_tile_data)) + { + st->blank = FALSE; + } + + pr_tile_invalidate_region(pr, st->x * pr->scale, st->y * pr->scale, + pr->source_tile_width * pr->scale, pr->source_tile_height * pr->scale); + + return st; +} + +static SourceTile *pr_source_tile_find(PixbufRenderer *pr, gint x, gint y) +{ + GList *work; + + work = pr->source_tiles; + while (work) + { + SourceTile *st = work->data; + + if (x >= st->x && x < st->x + pr->source_tile_width && + y >= st->y && y < st->y + pr->source_tile_height) + { + if (work != pr->source_tiles) + { + pr->source_tiles = g_list_remove_link(pr->source_tiles, work); + pr->source_tiles = g_list_concat(work, pr->source_tiles); + } + return st; + } + + work = work->next; + } + + return NULL; +} + +static GList *pr_source_tile_compute_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h, gint request) +{ + gint x1, y1; + GList *list = NULL; + gint sx, sy; + + if (x < 0) x = 0; + if (y < 0) y = 0; + if (w > pr->image_width) w = pr->image_width; + if (h > pr->image_height) h = pr->image_height; + + sx = (x / pr->source_tile_width) * pr->source_tile_width; + sy = (y / pr->source_tile_height) * pr->source_tile_height; + + for (x1 = sx; x1 < x + w; x1+= pr->source_tile_width) + { + for (y1 = sy; y1 < y + h; y1 += pr->source_tile_height) + { + SourceTile *st; + + st = pr_source_tile_find(pr, x1, y1); + if (!st && request) st = pr_source_tile_request(pr, x1, y1); + + if (st) list = g_list_prepend(list, st); + } + } + + return g_list_reverse(list); +} + +static void pr_source_tile_changed(PixbufRenderer *pr, gint x, gint y, gint width, gint height) +{ + GList *work; + + work = pr->source_tiles; + while (work) + { + SourceTile *st; + gint rx, ry, rw, rh; + + st = work->data; + work = work->next; + + if (util_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height, + x, y, width, height, + &rx, &ry, &rw, &rh)) + { + GdkPixbuf *pixbuf; + + pixbuf = gdk_pixbuf_new_subpixbuf(st->pixbuf, rx - st->x, ry - st->y, rw, rh); + if (pr->func_tile_request && + pr->func_tile_request(pr, rx, ry, rw, rh, pixbuf, pr->func_tile_data)) + { + pr_tile_invalidate_region(pr, rx * pr->scale, ry * pr->scale, + rw * pr->scale, rh * pr->scale); + } + g_object_unref(pixbuf); + } + } +} + +static gint pr_source_tile_render(PixbufRenderer *pr, ImageTile *it, + gint x, gint y, gint w, gint h, + gint new_data, gint fast) +{ + GtkWidget *box; + GList *list; + GList *work; + gint draw = FALSE; + + box = GTK_WIDGET(pr); + + if (pr->zoom == 1.0 || pr->scale == 1.0) + { + list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE); + work = list; + while (work) + { + SourceTile *st; + gint rx, ry, rw, rh; + + st = work->data; + work = work->next; + + if (util_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height, + it->x + x, it->y + y, w, h, + &rx, &ry, &rw, &rh)) + { + if (st->blank) + { + gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE, + rx - st->x, ry - st->y, rw, rh); + } + else /* (pr->zoom == 1.0 || pr->scale == 1.0) */ + { + gdk_draw_pixbuf(it->pixmap, + box->style->fg_gc[GTK_WIDGET_STATE(box)], + st->pixbuf, + rx - st->x, ry - st->y, + rx - it->x, ry - it->y, + rw, rh, + pr->dither_quality, rx, ry); + } + } + } + } + else + { + double scale_x, scale_y; + gint sx, sy, sw, sh; + + if (pr->image_width == 0 || pr->image_height == 0) return FALSE; + scale_x = (double)pr->width / pr->image_width; + scale_y = (double)pr->height / pr->image_height; + + sx = (double)(it->x + x) / scale_x; + sy = (double)(it->y + y) / scale_y; + sw = (double)w / scale_x; + sh = (double)h / scale_y; + + if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE; + +#if 0 + /* draws red over draw region, to check for leaks (regions not filled) */ + pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255); +#endif + + list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE); + work = list; + while (work) + { + SourceTile *st; + gint rx, ry, rw, rh; + gint stx, sty, stw, sth; + + st = work->data; + work = work->next; + + stx = floor((double)st->x * scale_x); + sty = floor((double)st->y * scale_y); + stw = ceil ((double)(st->x + pr->source_tile_width) * scale_x) - stx; + sth = ceil ((double)(st->y + pr->source_tile_height) * scale_y) - sty; + + if (util_clip_region(stx, sty, stw, sth, + it->x + x, it->y + y, w, h, + &rx, &ry, &rw, &rh)) + { + if (st->blank) + { + gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE, + rx - st->x, ry - st->y, rw, rh); + } + else + { + double offset_x; + double offset_y; + + /* may need to use unfloored stx,sty values here */ + offset_x = (double)(stx - it->x); + offset_y = (double)(sty - it->y); + + gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh, + (double) 0.0 + offset_x, + (double) 0.0 + offset_y, + scale_x, scale_y, + (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality); + draw = TRUE; + } + } + } + } + + g_list_free(list); + + return draw; +} + +void pixbuf_renderer_set_tiles(PixbufRenderer *pr, gint width, gint height, + gint tile_width, gint tile_height, gint cache_size, + PixbufRendererTileRequestFunc func_request, + PixbufRendererTileDisposeFunc func_dispose, + gpointer user_data, + gdouble zoom) +{ + g_return_if_fail(IS_PIXBUF_RENDERER(pr)); + g_return_if_fail(tile_width >= 32 && tile_width >= 32); + g_return_if_fail(width >= 32 && height > 32); + g_return_if_fail(func_request != NULL); + + if (pr->pixbuf) g_object_unref(pr->pixbuf); + pr->pixbuf = NULL; + + pr_source_tile_unset(pr); + + if (cache_size < 4) cache_size = 4; + + pr->source_tiles_enabled = TRUE; + pr->source_tiles_cache_size = cache_size; + pr->source_tile_width = tile_width; + pr->source_tile_height = tile_height; + + pr->image_width = width; + pr->image_height = height; + + pr->func_tile_request = func_request; + pr->func_tile_dispose = func_dispose; + pr->func_tile_data = user_data; + + pr_zoom_sync(pr, zoom, TRUE, FALSE, TRUE, FALSE, 0, 0); + pr_redraw(pr, TRUE); +} + +gint pixbuf_renderer_get_tiles(PixbufRenderer *pr) +{ + g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE); + + return pr->source_tiles_enabled; +} + +static void pr_zoom_adjust_real(PixbufRenderer *pr, gdouble increment, + gint center_point, gint x, gint y) +{ + gdouble zoom = pr->zoom; + + if (increment == 0.0) return; + + if (zoom == 0.0) + { + if (pr->scale < 1.0) + { + zoom = 0.0 - 1.0 / pr->scale; + } + else + { + zoom = pr->scale; + } + } + + if (increment < 0.0) + { + if (zoom >= 1.0 && zoom + increment < 1.0) + { + zoom = zoom + increment - 2.0; + } + else + { + zoom = zoom + increment; + } + } + else + { + if (zoom <= -1.0 && zoom + increment > -1.0) + { + zoom = zoom + increment + 2.0; + } + else + { + zoom = zoom + increment; + } + } + + pr_zoom_sync(pr, zoom, FALSE, FALSE, FALSE, center_point, x, y); +} + +/* + *------------------------------------------------------------------- + * display tiles + *------------------------------------------------------------------- + */ + +static ImageTile *pr_tile_new(gint x, gint y, gint width, gint height) +{ + ImageTile *it; + + it = g_new0(ImageTile, 1); + + it->x = x; + it->y = y; + it->w = width; + it->h = height; + + it->render_done = TILE_RENDER_NONE; + + return it; +} + +static void pr_tile_free(ImageTile *it) +{ + if (!it) return; + + if (it->pixbuf) gdk_pixbuf_unref(it->pixbuf); + if (it->pixmap) g_object_unref(it->pixmap); + + g_free(it); +} + +static void pr_tile_free_all(PixbufRenderer *pr) +{ + GList *work; + + work = pr->tiles; + while (work) + { + ImageTile *it; + + it = work->data; + work = work->next; + + pr_tile_free(it); + } + + g_list_free(pr->tiles); + pr->tiles = NULL; + pr->tile_cache_size = 0; +} + +static ImageTile *pr_tile_add(PixbufRenderer *pr, gint x, gint y) +{ + ImageTile *it; + + it = pr_tile_new(x, y, pr->tile_width, pr->tile_height); + + if (it->x + it->w > pr->width) it->w = pr->width - it->x; + if (it->y + it->h > pr->height) it->h = pr->height - it->y; + + pr->tiles = g_list_prepend(pr->tiles, it); + pr->tile_cache_size += it->size; + + return it; +} + +static void pr_tile_remove(PixbufRenderer *pr, ImageTile *it) +{ + if (it->qd) + { + QueueData *qd = it->qd; + + it->qd = NULL; + pr->draw_queue = g_list_remove(pr->draw_queue, qd); + g_free(qd); + } + + if (it->qd2) + { + QueueData *qd = it->qd2; + + it->qd2 = NULL; + pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd); + g_free(qd); + } + + pr->tiles = g_list_remove(pr->tiles, it); + pr->tile_cache_size -= it->size; + + pr_tile_free(it); +} + +static void pr_tile_free_space(PixbufRenderer *pr, guint space, ImageTile *it) +{ + GList *work; + gint tile_max; + + work = g_list_last(pr->tiles); + + if (pr->source_tiles_enabled && pr->scale < 1.0) + { + gint tiles; + + tiles = (pr->vis_width / PR_TILE_SIZE + 1) * (pr->vis_width / PR_TILE_SIZE + 1); + tile_max = MAX(tiles * PR_TILE_SIZE * PR_TILE_SIZE * 3, + (gint)((double)pr->tile_cache_max * 1048576.0 * pr->scale)); + } + else + { + tile_max = pr->tile_cache_max * 1048576; + } + + while (work && pr->tile_cache_size + space > tile_max) + { + ImageTile *needle; + + needle = work->data; + work = work->prev; + if (needle != it && + ((!needle->qd && !needle->qd2) || !pr_tile_is_visible(pr, needle))) pr_tile_remove(pr, needle); + } +} + +static void pr_tile_invalidate_all(PixbufRenderer *pr) +{ + GList *work; + + work = pr->tiles; + while (work) + { + ImageTile *it; + + it = work->data; + work = work->next; + + it->render_done = TILE_RENDER_NONE; + it->render_todo = TILE_RENDER_NONE; + it->blank = FALSE; + + it->w = MIN(pr->tile_width, pr->width - it->x); + it->h = MIN(pr->tile_height, pr->height - it->y); + } +} + +static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h) +{ + gint x1, x2; + gint y1, y2; + GList *work; + + x1 = (gint)floor(x / pr->tile_width) * pr->tile_width; + x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width; + + y1 = (gint)floor(y / pr->tile_height) * pr->tile_height; + y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height; + + work = pr->tiles; + while (work) + { + ImageTile *it; + + it = work->data; + work = work->next; + + if (it->x < x2 && it->x + it->w > x1 && + it->y < y2 && it->y + it->h > y1) + { + it->render_done = TILE_RENDER_NONE; + it->render_todo = TILE_RENDER_NONE; + } + } +} + +static ImageTile *pr_tile_get(PixbufRenderer *pr, gint x, gint y) +{ + GList *work; + + work = pr->tiles; + while (work) + { + ImageTile *it; + + it = work->data; + if (it->x == x && it->y == y) + { + pr->tiles = g_list_delete_link(pr->tiles, work); + pr->tiles = g_list_prepend(pr->tiles, it); + return it; + } + + work = work->next; + } + + return pr_tile_add(pr, x, y); +} + +static void pr_tile_prepare(PixbufRenderer *pr, ImageTile *it) +{ + if (!it->pixmap) + { + GdkPixmap *pixmap; + guint size; + + pixmap = gdk_pixmap_new(((GtkWidget *)pr)->window, pr->tile_width, pr->tile_height, -1); + + size = pixmap_calc_size(pixmap); + pr_tile_free_space(pr, size, it); + + it->pixmap = pixmap; + it->size += size; + pr->tile_cache_size += size; + } + + if ((pr->zoom != 1.0 || pr->source_tiles_enabled || (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf)) ) && + !it->pixbuf) + { + GdkPixbuf *pixbuf; + guint size; + + if (pr->pixbuf) + { + pixbuf = gdk_pixbuf_new(gdk_pixbuf_get_colorspace(pr->pixbuf), + gdk_pixbuf_get_has_alpha(pr->pixbuf), + gdk_pixbuf_get_bits_per_sample(pr->pixbuf), + pr->tile_width, pr->tile_height); + } + else + { + pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, pr->tile_width, pr->tile_height); + } + + size = gdk_pixbuf_get_rowstride(pixbuf) * pr->tile_height; + pr_tile_free_space(pr, size, it); + + it->pixbuf = pixbuf; + it->size += size; + pr->tile_cache_size += size; + } +} + +/* + *------------------------------------------------------------------- + * drawing + *------------------------------------------------------------------- + */ + +static void pr_tile_render(PixbufRenderer *pr, ImageTile *it, + gint x, gint y, gint w, gint h, + gint new_data, gint fast) +{ + GtkWidget *box; + gint has_alpha; + gint draw = FALSE; + + if (it->render_todo == TILE_RENDER_NONE && it->pixmap && !new_data) return; + + if (it->render_done != TILE_RENDER_ALL) + { + x = 0; + y = 0; + w = it->w; + h = it->h; + if (!fast) it->render_done = TILE_RENDER_ALL; + } + else if (it->render_todo != TILE_RENDER_AREA) + { + if (!fast) it->render_todo = TILE_RENDER_NONE; + return; + } + + if (!fast) it->render_todo = TILE_RENDER_NONE; + + if (new_data) it->blank = FALSE; + + pr_tile_prepare(pr, it); + has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf)); + + box = GTK_WIDGET(pr); + + /* FIXME checker colors for alpha should be configurable, + * also should be drawn for blank = TRUE + */ + + if (it->blank) + { + /* no data, do fast rect fill */ + gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE, + 0, 0, it->w, it->h); + } + else if (pr->source_tiles_enabled) + { + draw = pr_source_tile_render(pr, it, x, y, w, h, new_data, fast); + } + else if (pr->zoom == 1.0 || pr->scale == 1.0) + { + if (has_alpha) + { + gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, x, y, w, h, + (double) 0.0 - it->x, + (double) 0.0 - it->y, + 1.0, 1.0, GDK_INTERP_NEAREST, + 255, it->x + x, it->y + y, + PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2); + draw = TRUE; + } + else + { + /* faster, simple */ + gdk_draw_pixbuf(it->pixmap, + box->style->fg_gc[GTK_WIDGET_STATE(box)], + pr->pixbuf, + it->x + x, it->y + y, + x, y, + w, h, + pr->dither_quality, it->x + x, it->y + y); + } + } + else + { + double scale_x, scale_y; + + if (pr->image_width == 0 || pr->image_height == 0) return; + scale_x = (double)pr->width / pr->image_width; + scale_y = (double)pr->height / pr->image_height; + + /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely + * small sizes for anything but GDK_INTERP_NEAREST + */ + if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE; + + if (!has_alpha) + { + gdk_pixbuf_scale(pr->pixbuf, it->pixbuf, x, y, w, h, + (double) 0.0 - it->x, + (double) 0.0 - it->y, + scale_x, scale_y, + (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality); + } + else + { + gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, x, y, w, h, + (double) 0.0 - it->x, + (double) 0.0 - it->y, + scale_x, scale_y, + (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality, + 255, it->x + x, it->y + y, + PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2); + } + draw = TRUE; + } + + if (draw && it->pixbuf && !it->blank) + { + gdk_draw_pixbuf(it->pixmap, + box->style->fg_gc[GTK_WIDGET_STATE(box)], + it->pixbuf, + x, y, + x, y, + w, h, + pr->dither_quality, it->x + x, it->y + y); + } +} + + +static void pr_tile_expose(PixbufRenderer *pr, ImageTile *it, + gint x, gint y, gint w, gint h, + gint new_data, gint fast) +{ + GtkWidget *box; + + pr_tile_render(pr, it, x, y, w, h, new_data, fast); + + box = GTK_WIDGET(pr); + + gdk_draw_drawable(box->window, box->style->fg_gc[GTK_WIDGET_STATE(box)], + it->pixmap, x, y, + pr->x_offset + (it->x - pr->x_scroll) + x, pr->y_offset + (it->y - pr->y_scroll) + y, w, h); + + if (pr->overlay_list) + { + pr_overlay_draw(pr, pr->x_offset + (it->x - pr->x_scroll) + x, + pr->y_offset + (it->y - pr->y_scroll) + y, + w, h); + } +} + + +static gint pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it) +{ + return (it->x + it->w >= pr->x_scroll && it->x <= pr->x_scroll + pr->window_width - pr->x_offset * 2 && + it->y + it->h >= pr->y_scroll && it->y <= pr->y_scroll + pr->window_height - pr->y_offset * 2); +} + +/* + *------------------------------------------------------------------- + * draw queue + *------------------------------------------------------------------- + */ + +static gint pr_queue_draw_idle_cb(gpointer data) +{ + PixbufRenderer *pr = data; + QueueData *qd; + gint fast; + + if ((!pr->pixbuf && !pr->source_tiles_enabled) || + (!pr->draw_queue && !pr->draw_queue_2pass) || + pr->draw_idle_id == -1) + { +#if 0 + if (!pr->completed) image_complete_util(pr, FALSE); +#endif + + pr->draw_idle_id = -1; + return FALSE; + } + + if (pr->draw_queue) + { + qd = pr->draw_queue->data; + fast = (pr->zoom_2pass && pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0); + } + else + { + if (pr->loading) + { + /* still loading, wait till done (also drops the higher priority) */ + + pr->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + pr_queue_draw_idle_cb, pr, NULL); + pr->draw_idle_high = FALSE; + return FALSE; + } + + qd = pr->draw_queue_2pass->data; + fast = FALSE; + } + + if (GTK_WIDGET_REALIZED(pr)) + { + if (pr_tile_is_visible(pr, qd->it)) + { + pr_tile_expose(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast); + } + else if (qd->new_data) + { + /* if new pixel data, and we already have a pixmap, update the tile */ + qd->it->blank = FALSE; + if (qd->it->pixmap && qd->it->render_done == TILE_RENDER_ALL) + { + pr_tile_render(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast); + } + } + } + + if (pr->draw_queue) + { + qd->it->qd = NULL; + pr->draw_queue = g_list_remove(pr->draw_queue, qd); + if (fast) + { + if (qd->it->qd2) + { + pr_queue_merge(qd->it->qd2, qd); + g_free(qd); + } + else + { + qd->it->qd2 = qd; + pr->draw_queue_2pass = g_list_append(pr->draw_queue_2pass, qd); + } + } + else + { + g_free(qd); + } + } + else + { + qd->it->qd2 = NULL; + pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd); + g_free(qd); + } + + if (!pr->draw_queue && !pr->draw_queue_2pass) + { +#if 0 + if (!pr->completed) image_complete_util(pr, FALSE); +#endif + + pr->draw_idle_id = -1; + return FALSE; + } + + return TRUE; +} + +static void pr_queue_list_free(GList *list) +{ + GList *work; + + work = list; + while (work) + { + QueueData *qd; + + qd = work->data; + work = work->next; + + qd->it->qd = NULL; + qd->it->qd2 = NULL; + g_free(qd); + } + + g_list_free(list); +} + +static void pr_queue_clear(PixbufRenderer *pr) +{ + pr_queue_list_free(pr->draw_queue); + pr->draw_queue = NULL; + + pr_queue_list_free(pr->draw_queue_2pass); + pr->draw_queue_2pass = NULL; + + if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id); + pr->draw_idle_id = -1; +} + +static void pr_queue_merge(QueueData *parent, QueueData *qd) +{ + if (parent->x + parent->w < qd->x + qd->w) + { + parent->w += (qd->x + qd->w) - (parent->x + parent->w); + } + if (parent->x > qd->x) + { + parent->w += parent->x - qd->x; + parent->x = qd->x; + } + + if (parent->y + parent->h < qd->y + qd->h) + { + parent->h += (qd->y + qd->h) - (parent->y + parent->h); + } + if (parent->y > qd->y) + { + parent->h += parent->y - qd->y; + parent->y = qd->y; + } + + parent->new_data |= qd->new_data; +} + +static gint pr_clamp_to_visible(PixbufRenderer *pr, gint *x, gint *y, gint *w, gint *h) +{ + gint nx, ny; + gint nw, nh; + gint vx, vy; + gint vw, vh; + + vw = pr->vis_width; + vh = pr->vis_height; + + vx = pr->x_scroll; + vy = pr->y_scroll; + + if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE; + + /* now clamp it */ + nx = CLAMP(*x, vx, vx + vw); + nw = CLAMP(*w - (nx - *x), 1, vw); + + ny = CLAMP(*y, vy, vy + vh); + nh = CLAMP(*h - (ny - *y), 1, vh); + + *x = nx; + *y = ny; + *w = nw; + *h = nh; + + return TRUE; +} + +static gint pr_queue_to_tiles(PixbufRenderer *pr, gint x, gint y, gint w, gint h, + gint clamp, ImageTileRenderType render, gint new_data) +{ + gint i, j; + gint x1, x2; + gint y1, y2; + + if (clamp && !pr_clamp_to_visible(pr, &x, &y, &w, &h)) return FALSE; + + x1 = (gint)floor(x / pr->tile_width) * pr->tile_width; + x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width; + + y1 = (gint)floor(y / pr->tile_height) * pr->tile_height; + y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height; + + for (j = y1; j <= y2; j += pr->tile_height) + { + for (i = x1; i <= x2; i += pr->tile_width) + { + ImageTile *it; + + it = pr_tile_get(pr, i, j); + if (it) + { + QueueData *qd; + + if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) || + (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL)) + { + it->render_todo = render; + } + + qd = g_new(QueueData, 1); + qd->it = it; + qd->new_data = new_data; + + if (i < x) + { + qd->x = x - i; + } + else + { + qd->x = 0; + } + qd->w = x + w - i - qd->x; + if (qd->x + qd->w > pr->tile_width) qd->w = pr->tile_width - qd->x; + + if (j < y) + { + qd->y = y - j; + } + else + { + qd->y = 0; + } + qd->h = y + h - j - qd->y; + if (qd->y + qd->h > pr->tile_height) qd->h = pr->tile_height - qd->y; + + if (qd->w < 1 || qd->h < 1) + { + g_free(qd); + } + else if (it->qd) + { + pr_queue_merge(it->qd, qd); + g_free(qd); + } + else + { + it->qd = qd; + pr->draw_queue = g_list_append(pr->draw_queue, qd); + } + } + } + } + + return TRUE; +} + +static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h, + gint clamp, ImageTileRenderType render, gint new_data) +{ + gint nx, ny; + + nx = CLAMP(x, 0, pr->width - 1); + ny = CLAMP(y, 0, pr->height - 1); + w -= (nx - x); + h -= (ny - y); + w = CLAMP(w, 0, pr->width - nx); + h = CLAMP(h, 0, pr->height - ny); + if (w < 1 || h < 1) return; + + if (pr_queue_to_tiles(pr, nx, ny, w, h, clamp, render, new_data) && + ((!pr->draw_queue && !pr->draw_queue_2pass) || pr->draw_idle_id == -1 || !pr->draw_idle_high)) + { + if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id); + pr->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, + pr_queue_draw_idle_cb, pr, NULL); + pr->draw_idle_high = TRUE; + } +} + +static void pr_redraw(PixbufRenderer *pr, gint new_data) +{ + pr_queue_clear(pr); + pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, new_data); +} + +/* + *------------------------------------------------------------------- + * signal emission + *------------------------------------------------------------------- + */ + +static void pr_update_signal(PixbufRenderer *pr) +{ +#if 0 + printf("FIXME: send updated signal\n"); +#endif +} + +static void pr_zoom_signal(PixbufRenderer *pr) +{ + g_signal_emit(pr, signals[SIGNAL_ZOOM], 0, pr->zoom); +} + +static void pr_scroll_notify_signal(PixbufRenderer *pr) +{ + g_signal_emit(pr, signals[SIGNAL_SCROLL_NOTIFY], 0); +} + +static void pr_button_signal(PixbufRenderer *pr, GdkEventButton *bevent) +{ + g_signal_emit(pr, signals[SIGNAL_CLICKED], 0, bevent); +} + +/* + *------------------------------------------------------------------- + * sync and clamp + *------------------------------------------------------------------- + */ + +static gint pr_scroll_clamp(PixbufRenderer *pr) +{ + gint old_xs; + gint old_ys; + + if (pr->zoom == 0.0) + { + pr->x_scroll = 0; + pr->y_scroll = 0; + + pr_scroll_notify_signal(pr); + return FALSE; + } + + old_xs = pr->x_scroll; + old_ys = pr->y_scroll; + + if (pr->x_offset > 0) + { + pr->x_scroll = 0; + } + else + { + pr->x_scroll = CLAMP(pr->x_scroll, 0, pr->width - pr->vis_width); + } + + if (pr->y_offset > 0) + { + pr->y_scroll = 0; + } + else + { + pr->y_scroll = CLAMP(pr->y_scroll, 0, pr->height - pr->vis_height); + } + + pr_scroll_notify_signal(pr); + + return (old_xs != pr->x_scroll || old_ys != pr->y_scroll); +} + +static gint pr_size_clamp(PixbufRenderer *pr) +{ + gint old_vw, old_vh; + + old_vw = pr->vis_width; + old_vh = pr->vis_height; + + if (pr->width < pr->window_width) + { + pr->vis_width = pr->width; + pr->x_offset = (pr->window_width - pr->width) / 2; + } + else + { + pr->vis_width = pr->window_width; + pr->x_offset = 0; + } + + if (pr->height < pr->window_height) + { + pr->vis_height = pr->height; + pr->y_offset = (pr->window_height - pr->height) / 2; + } + else + { + pr->vis_height = pr->window_height; + pr->y_offset = 0; + } + + return (old_vw != pr->vis_width || old_vh != pr->vis_height); +} + +static gint pr_zoom_clamp(PixbufRenderer *pr, gdouble zoom, gint force, gint new, gint invalidate) +{ + gint w, h; + gdouble scale; + gint invalid; + + zoom = CLAMP(zoom, pr->zoom_min, pr->zoom_max); + + if (pr->zoom == zoom && !force) return FALSE; + + w = pr->image_width; + h = pr->image_height; + + if (zoom == 0.0 && !pr->pixbuf) + { + scale = 1.0; + } + else if (zoom == 0.0) + { + gint max_w; + gint max_h; + gint sizeable; + + sizeable = (new && pr_parent_window_sizable(pr)); + + if (sizeable) + { + max_w = gdk_screen_width(); + max_h = gdk_screen_height(); + + if (pr->window_limit) + { + max_w = max_w * pr->window_limit_size / 100; + max_h = max_h * pr->window_limit_size / 100; + } + } + else + { + max_w = pr->window_width; + max_h = pr->window_height; + } + + if ((pr->zoom_expand && !sizeable) || w > max_w || h > max_h) + { + if ((gdouble)max_w / w > (gdouble)max_h / h) + { + scale = (gdouble)max_h / h; + h = max_h; + w = w * scale + 0.5; + if (w > max_w) w = max_w; + } + else + { + scale = (gdouble)max_w / w; + w = max_w; + h = h * scale + 0.5; + if (h > max_h) h = max_h; + } + if (w < 1) w = 1; + if (h < 1) h = 1; + } + else + { + scale = 1.0; + } + } + else if (zoom > 0.0) /* zoom orig, in */ + { + scale = zoom; + w = w * scale; + h = h * scale; + } + else /* zoom out */ + { + scale = 1.0 / (0.0 - zoom); + w = w * scale; + h = h * scale; + } + + invalid = (pr->width != w || pr->height != h); + + pr->zoom = zoom; + pr->width = w; + pr->height = h; + pr->scale = scale; + + if (invalidate || invalid) + { + pr_tile_invalidate_all(pr); + pr_redraw(pr, TRUE); + } + + return TRUE; +} + +static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom, + gint force, gint blank, gint new, + gint center_point, gint px, gint py) +{ + gdouble old_scale; + gint old_cx, old_cy; + gint clamped; + gint sized; + + old_scale = pr->scale; + if (center_point) + { + px = CLAMP(px, 0, pr->width); + py = CLAMP(py, 0, pr->height); + old_cx = pr->x_scroll + (px - pr->x_offset); + old_cy = pr->y_scroll + (py - pr->y_offset); + } + else + { + px = py = 0; + old_cx = pr->x_scroll + pr->vis_width / 2; + old_cy = pr->y_scroll + pr->vis_height / 2; + } + + if (!pr_zoom_clamp(pr, zoom, force, new, force)) return; + + clamped = pr_size_clamp(pr); + sized = pr_parent_window_resize(pr, pr->width, pr->height); + + if (force) + { + switch (pr->scroll_reset) + { + case PR_SCROLL_RESET_NOCHANGE: + /* maintain old scroll position, do nothing */ + break; + case PR_SCROLL_RESET_CENTER: + /* center new image */ + pr->x_scroll = ((double)pr->image_width / 2.0 * pr->scale) - pr->vis_width / 2; + pr->y_scroll = ((double)pr->image_height / 2.0 * pr->scale) - pr->vis_height / 2; + break; + case PR_SCROLL_RESET_TOPLEFT: + default: + /* reset to upper left */ + pr->x_scroll = 0; + pr->y_scroll = 0; + break; + } + } + else + { + /* user zoom does not force, so keep visible center point */ + if (center_point) + { + pr->x_scroll = old_cx / old_scale * pr->scale - (px - pr->x_offset); + pr->y_scroll = old_cy / old_scale * pr->scale - (py - pr->y_offset); + } + else + { + pr->x_scroll = old_cx / old_scale * pr->scale - (pr->vis_width / 2); + pr->y_scroll = old_cy / old_scale * pr->scale - (pr->vis_height / 2); + } + } + + pr_scroll_clamp(pr); + +#if 0 + pr_tile_sync(pr, blank); +#endif + + /* If the window was not sized, redraw the image - we know there will be no size/expose signal. + * But even if a size is claimed, there is no guarantee that the window manager will allow it, + * so redraw the window anyway :/ + */ + if (sized || clamped) pr_border_clear(pr); + pr_redraw(pr, FALSE); + + pr_zoom_signal(pr); + pr_update_signal(pr); +} + +static void pr_size_sync(PixbufRenderer *pr, gint new_width, gint new_height) +{ + if (pr->window_width == new_width && pr->window_height == new_height) return; + + pr->window_width = new_width; + pr->window_height = new_height; + + if (pr->zoom == 0.0) pr_zoom_clamp(pr, 0.0, TRUE, FALSE, FALSE); + + pr_size_clamp(pr); + pr_scroll_clamp(pr); + + /* ensure scroller remains visible */ + if (pr->scroller_overlay != -1) + { + gint update = FALSE; + + if (pr->scroller_x > new_width) + { + pr->scroller_x = new_width; + pr->scroller_xpos = new_width; + update = TRUE; + } + if (pr->scroller_y > new_height) + { + pr->scroller_y = new_height; + pr->scroller_ypos = new_height; + update = TRUE; + } + + if (update) + { + GdkPixbuf *pixbuf; + + if (pixbuf_renderer_overlay_get(pr, pr->scroller_overlay, &pixbuf, NULL, NULL)) + { + gint w, h; + + w = gdk_pixbuf_get_width(pixbuf); + h = gdk_pixbuf_get_height(pixbuf); + pixbuf_renderer_overlay_set(pr, pr->scroller_overlay, pixbuf, + pr->scroller_x - w / 2, pr->scroller_y - h / 2); + } + } + } + + pr_border_clear(pr); + +#if 0 + pr_tile_sync(pr, pr->width, pr->height, FALSE); +#endif + + pr_update_signal(pr); +} + +static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data) +{ + PixbufRenderer *pr = data; + + pr_size_sync(pr, allocation->width, allocation->height); +} + +static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area) +{ + gint x, y; + + pr_border_draw(pr, area->x, area->y, area->width, area->height); + + x = MAX(0, (gint)area->x - pr->x_offset + pr->x_scroll); + y = MAX(0, (gint)area->y - pr->y_offset + pr->y_scroll); + + pr_queue(pr, x, y, + MIN((gint)area->width, pr->width - x), + MIN((gint)area->height, pr->height - y), + FALSE, TILE_RENDER_ALL, FALSE); +} + +/* + *------------------------------------------------------------------- + * scrolling + *------------------------------------------------------------------- + */ + +void pixbuf_renderer_scroll(PixbufRenderer *pr, gint x, gint y) +{ + gint old_x, old_y; + gint x_off, y_off; + gint w, h; + + g_return_if_fail(IS_PIXBUF_RENDERER(pr)); + + if (!pr->pixbuf && !pr->source_tiles_enabled) return; + + old_x = pr->x_scroll; + old_y = pr->y_scroll; + + pr->x_scroll += x; + pr->y_scroll += y; + + pr_scroll_clamp(pr); + if (pr->x_scroll == old_x && pr->y_scroll == old_y) return; + + if (pr->overlay_list) + { + gint new_x, new_y; + + new_x = pr->x_scroll; + new_y = pr->y_scroll; + pr->x_scroll = old_x; + pr->y_scroll = old_y; + + pr_overlay_queue_all(pr); + + pr->x_scroll = new_x; + pr->y_scroll = new_y; + } + + x_off = pr->x_scroll - old_x; + y_off = pr->y_scroll - old_y; + + w = pr->vis_width - abs(x_off); + h = pr->vis_height - abs(y_off); + + if (w < 1 || h < 1) + { + /* scrolled completely to new material */ + pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE); + return; + } + else + { + gint x1, y1; + gint x2, y2; + GtkWidget *box; + GdkGC *gc; + + if (x_off < 0) + { + x1 = abs(x_off); + x2 = 0; + } + else + { + x1 = 0; + x2 = abs(x_off); + } + + if (y_off < 0) + { + y1 = abs(y_off); + y2 = 0; + } + else + { + y1 = 0; + y2 = abs(y_off); + } + + box = GTK_WIDGET(pr); + + gc = gdk_gc_new(box->window); + gdk_gc_set_exposures(gc, TRUE); + gdk_draw_drawable(box->window, gc, + box->window, + x2 + pr->x_offset, y2 + pr->y_offset, + x1 + pr->x_offset, y1 + pr->y_offset, w, h); + g_object_unref(gc); + + if (pr->overlay_list) + { + pr_overlay_queue_all(pr); + } + + w = pr->vis_width - w; + h = pr->vis_height - h; + + if (w > 0) + { + pr_queue(pr, + x_off > 0 ? pr->x_scroll + (pr->vis_width - w) : pr->x_scroll, pr->y_scroll, + w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE); + } + if (h > 0) + { + /* FIXME, to optimize this, remove overlap */ + pr_queue(pr, + pr->x_scroll, y_off > 0 ? pr->y_scroll + (pr->vis_height - h) : pr->y_scroll, + pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE); + } + } +} + +void pixbuf_renderer_scroll_to_point(PixbufRenderer *pr, gint x, gint y, + gdouble x_align, gdouble y_align) +{ + gint px, py; + gint ax, ay; + + x_align = CLAMP(x_align, 0.0, 1.0); + y_align = CLAMP(y_align, 0.0, 1.0); + + ax = (gdouble)pr->vis_width * x_align; + ay = (gdouble)pr->vis_height * y_align; + + px = (gdouble)x * pr->scale - (pr->x_scroll + ax); + py = (gdouble)y * pr->scale - (pr->y_scroll + ay); + + pixbuf_renderer_scroll(pr, px, py); +} + +/* + *------------------------------------------------------------------- + * mouse + *------------------------------------------------------------------- + */ + +static gint pr_mouse_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data) +{ + PixbufRenderer *pr; + gint accel; + + pr = PIXBUF_RENDERER(widget); + + if (pr->scroller_id != -1) + { + pr->scroller_xpos = bevent->x; + pr->scroller_ypos = bevent->y; + } + + if (!pr->in_drag || !gdk_pointer_is_grabbed()) return FALSE; + + if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD) + { + pr->drag_moved++; + } + else + { + widget_set_cursor(widget, GDK_FLEUR); + } + + if (bevent->state & GDK_SHIFT_MASK) + { + accel = PR_PAN_SHIFT_MULTIPLIER; + } + else + { + accel = 1; + } + + /* do the scroll */ + pixbuf_renderer_scroll(pr, (pr->drag_last_x - bevent->x) * accel, + (pr->drag_last_y - bevent->y) * accel); + + pr->drag_last_x = bevent->x; + pr->drag_last_y = bevent->y; + + return FALSE; +} + +static gint pr_mouse_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data) +{ + PixbufRenderer *pr; + GtkWidget *parent; + + pr = PIXBUF_RENDERER(widget); + + if (pr->scroller_id != -1) return TRUE; + + switch (bevent->button) + { + case 1: + pr->in_drag = TRUE; + pr->drag_last_x = bevent->x; + pr->drag_last_y = bevent->y; + pr->drag_moved = 0; + gdk_pointer_grab(widget->window, FALSE, + GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK, + NULL, NULL, bevent->time); + gtk_grab_add(widget); + break; + case 2: + pr->drag_moved = 0; + break; + case 3: + pr_button_signal(pr, bevent); + break; + default: + break; + } + + parent = gtk_widget_get_parent(widget); + if (widget && GTK_WIDGET_CAN_FOCUS(parent)) + { + gtk_widget_grab_focus(parent); + } + + return FALSE; +} + +static gint pr_mouse_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data) +{ + PixbufRenderer *pr; + + pr = PIXBUF_RENDERER(widget); + + if (pr->scroller_id != -1) + { + pr_scroller_stop(pr); + return TRUE; + } + + if (gdk_pointer_is_grabbed() && GTK_WIDGET_HAS_GRAB(pr)) + { + gtk_grab_remove(widget); + gdk_pointer_ungrab(bevent->time); + widget_set_cursor(widget, -1); + } + + if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD) + { + if (bevent->button == 1 && (bevent->state & GDK_SHIFT_MASK)) + { + pr_scroller_start(pr, bevent->x, bevent->y); + } + else if (bevent->button == 1 || bevent->button == 2) + { + pr_button_signal(pr, bevent); + } + } + + pr->in_drag = FALSE; + + return FALSE; +} + +static gint pr_mouse_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data) +{ + PixbufRenderer *pr; + + pr = PIXBUF_RENDERER(widget); + + if (pr->scroller_id != -1) + { + pr->scroller_xpos = pr->scroller_x; + pr->scroller_ypos = pr->scroller_y; + pr->scroller_xinc = 0; + pr->scroller_yinc = 0; + } + + return FALSE; +} + +static void pr_mouse_drag_cb(GtkWidget *widget, GdkDragContext *context, gpointer data) +{ + PixbufRenderer *pr; + + pr = PIXBUF_RENDERER(widget); + + pr->drag_moved = PR_DRAG_SCROLL_THRESHHOLD; +} + +static void pr_signals_connect(PixbufRenderer *pr) +{ + g_signal_connect(G_OBJECT(pr), "motion_notify_event", + G_CALLBACK(pr_mouse_motion_cb), pr); + g_signal_connect(G_OBJECT(pr), "button_press_event", + G_CALLBACK(pr_mouse_press_cb), pr); + g_signal_connect(G_OBJECT(pr), "button_release_event", + G_CALLBACK(pr_mouse_release_cb), pr); + g_signal_connect(G_OBJECT(pr), "leave_notify_event", + G_CALLBACK(pr_mouse_leave_cb), pr); + + gtk_widget_set_events(GTK_WIDGET(pr), GDK_POINTER_MOTION_MASK | + GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK | + GDK_LEAVE_NOTIFY_MASK); + + g_signal_connect(G_OBJECT(pr), "drag_begin", + G_CALLBACK(pr_mouse_drag_cb), pr); + +} + +/* + *------------------------------------------------------------------- + * public + *------------------------------------------------------------------- + */ + +static void pr_pixbuf_sync(PixbufRenderer *pr, gdouble zoom, gint blank, gint new) +{ + if (!pr->pixbuf) + { + GtkWidget *box; + + /* no pixbuf so just clear the window */ + pr->image_width = 0; + pr->image_height = 0; + pr->scale = 1.0; + + box = GTK_WIDGET(pr); + + if (GTK_WIDGET_REALIZED(box)) + { + gdk_window_clear(box->window); + pr_overlay_draw(pr, 0, 0, pr->window_width, pr->window_height); + } + + pr_update_signal(pr); + + return; + } + + pr->image_width = gdk_pixbuf_get_width(pr->pixbuf); + pr->image_height = gdk_pixbuf_get_height(pr->pixbuf); + + pr_zoom_sync(pr, zoom, TRUE, blank, new, FALSE, 0, 0); +} + +static void pr_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom, gint new) +{ + if (pixbuf) g_object_ref(pixbuf); + if (pr->pixbuf) g_object_unref(pr->pixbuf); + pr->pixbuf = pixbuf; + + pr_pixbuf_sync(pr, zoom, FALSE, new); +} + +void pixbuf_renderer_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom) +{ + g_return_if_fail(IS_PIXBUF_RENDERER(pr)); + + pr_source_tile_unset(pr); + + pr_set_pixbuf(pr, pixbuf, zoom, TRUE); + + pr_update_signal(pr); +} + +GdkPixbuf *pixbuf_renderer_get_pixbuf(PixbufRenderer *pr) +{ + g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL); + + return pr->pixbuf; +} + +void pixbuf_renderer_area_changed(PixbufRenderer *pr, gint x, gint y, gint width, gint height) +{ + gint sx, sy, sw, sh; + + g_return_if_fail(IS_PIXBUF_RENDERER(pr)); + + if (pr->source_tiles_enabled) + { + pr_source_tile_changed(pr, x, y, width, height); + } + + if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST) + { + /* increase region when using a zoom quality that may access surrounding pixels */ + y -= 1; + height += 2; + } + + sx = (gint)floor((double)x * pr->scale); + sy = (gint)floor((double)y * pr->scale); + sw = (gint)ceil((double)width * pr->scale); + sh = (gint)ceil((double)height * pr->scale); + + pr_queue(pr, sx, sy, sw, sh, FALSE, TILE_RENDER_AREA, TRUE); +} + +void pixbuf_renderer_zoom_adjust(PixbufRenderer *pr, gdouble increment) +{ + g_return_if_fail(IS_PIXBUF_RENDERER(pr)); + + pr_zoom_adjust_real(pr, increment, FALSE, 0, 0); +} + +void pixbuf_renderer_zoom_adjust_at_point(PixbufRenderer *pr, gdouble increment, gint x, gint y) +{ + g_return_if_fail(IS_PIXBUF_RENDERER(pr)); + + pr_zoom_adjust_real(pr, increment, TRUE, x, y); +} + +void pixbuf_renderer_zoom_set(PixbufRenderer *pr, gdouble zoom) +{ + g_return_if_fail(IS_PIXBUF_RENDERER(pr)); + + pr_zoom_sync(pr, zoom, FALSE, FALSE, FALSE, FALSE, 0, 0); +} + +gdouble pixbuf_renderer_zoom_get(PixbufRenderer *pr) +{ + g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0); + + return pr->zoom; +} + +gdouble pixbuf_renderer_zoom_get_scale(PixbufRenderer *pr) +{ + g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0); + + return pr->scale; +} + +void pixbuf_renderer_zoom_set_limits(PixbufRenderer *pr, gdouble min, gdouble max) +{ + g_return_if_fail(IS_PIXBUF_RENDERER(pr)); + + if (min > 1.0 || max < 1.0) return; + if (min < 1.0 && min > -1.0) return; + if (min < -200.0 || max > 200.0) return; + + if (pr->zoom_min != min) + { + pr->zoom_min = min; + g_object_notify(G_OBJECT(pr), "zoom_min"); + } + if (pr->zoom_max != max) + { + pr->zoom_max = max; + g_object_notify(G_OBJECT(pr), "zoom_max"); + } +} + +gint pixbuf_renderer_get_image_size(PixbufRenderer *pr, gint *width, gint *height) +{ + g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE); + g_return_val_if_fail(width != NULL && height != NULL, FALSE); + + if (!pr->pixbuf && !pr->source_tiles_enabled) + { + *width = 0; + *height = 0; + return FALSE; + } + + *width = pr->image_width; + *height = pr->image_height; + return TRUE; +} + +gint pixbuf_renderer_get_scaled_size(PixbufRenderer *pr, gint *width, gint *height) +{ + g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE); + g_return_val_if_fail(width != NULL && height != NULL, FALSE); + + if (!pr->pixbuf && !pr->source_tiles_enabled) + { + *width = 0; + *height = 0; + return FALSE; + } + + *width = pr->width; + *height = pr->height; + return TRUE; +} + +gint pixbuf_renderer_get_visible_rect(PixbufRenderer *pr, GdkRectangle *rect) +{ + g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE); + g_return_val_if_fail(rect != NULL, FALSE); + + if ((!pr->pixbuf && !pr->source_tiles_enabled) || + !pr->scale) + { + rect->x = 0; + rect->y = 0; + rect->width = 0; + rect->height = 0; + return FALSE; + } + + rect->x = (gint)((gdouble)pr->x_scroll / pr->scale); + rect->y = (gint)((gdouble)pr->y_scroll / pr->scale); + rect->width = (gint)((gdouble)pr->vis_width / pr->scale); + rect->height = (gint)((gdouble)pr->vis_height / pr->scale); + return TRUE; +} + +gint pixbuf_renderer_get_virtual_rect(PixbufRenderer *pr, GdkRectangle *rect) +{ + g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE); + g_return_val_if_fail(rect != NULL, FALSE); + + if ((!pr->pixbuf && !pr->source_tiles_enabled)) + { + rect->x = 0; + rect->y = 0; + rect->width = 0; + rect->height = 0; + return FALSE; + } + + rect->x = pr->x_scroll; + rect->y = pr->y_scroll; + rect->width = pr->vis_width; + rect->height = pr->vis_height; + return TRUE; +} + + diff --git a/src/pixbuf-renderer.h b/src/pixbuf-renderer.h new file mode 100644 index 00000000..7d2d2e48 --- /dev/null +++ b/src/pixbuf-renderer.h @@ -0,0 +1,214 @@ +/* + * GQview + * (C) 2005 John Ellis + * + * Author: John Ellis + * + * This software is released under the GNU General Public License (GNU GPL). + * Please read the included file COPYING for more information. + * This software comes with no warranty of any kind, use at your own risk! + */ + +#ifndef PIXBUF_RENDERER_H +#define PIXBUF_RENDERER_H + +#include +#include + + +#define TYPE_PIXBUF_RENDERER (pixbuf_renderer_get_type()) +#define PIXBUF_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), TYPE_PIXBUF_RENDERER, PixbufRenderer)) +#define PIXBUF_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), TYPE_PIXBUF_RENDERER, PixbufRendererClass)) +#define IS_PIXBUF_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), TYPE_PIXBUF_RENDERER)) +#define IS_PIXBUF_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), TYPE_PIXBUF_RENDERER)) +#define PIXBUF_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), TYPE_PIXBUF_RENDERER, PixbufRendererClass)) + + +typedef struct _PixbufRenderer PixbufRenderer; +typedef struct _PixbufRendererClass PixbufRendererClass; + + +typedef gint (* PixbufRendererTileRequestFunc)(PixbufRenderer *pr, gint x, gint y, + gint width, gint height, GdkPixbuf *pixbuf, gpointer user_data); +typedef void (* PixbufRendererTileDisposeFunc)(PixbufRenderer *pr, gint x, gint y, + gint width, gint height, GdkPixbuf *pixbuf, gpointer user_data); +typedef enum { + PR_SCROLL_RESET_TOPLEFT = 0, + PR_SCROLL_RESET_CENTER, + PR_SCROLL_RESET_NOCHANGE +} PixbufRendererScrollResetType; + +struct _PixbufRenderer +{ + GtkEventBox eventbox; + + gint image_width; /* image actual dimensions (pixels) */ + gint image_height; + + GdkPixbuf *pixbuf; + + gint window_width; /* allocated size of window (drawing area) */ + gint window_height; + + gint x_offset; /* offset of image start (non-zero when image < window) */ + gint y_offset; + + gint vis_width; /* dimensions of visible part of image */ + gint vis_height; + + gint width; /* size of scaled image (result) */ + gint height; + + gint x_scroll; /* scroll offset of image (into width, height to start drawing) */ + gint y_scroll; + + gdouble zoom_min; + gdouble zoom_max; + gdouble zoom; /* zoom we want (0 is auto) */ + gdouble scale; /* zoom we got (should never be 0) */ + + GdkInterpType zoom_quality; + gboolean zoom_2pass; + gboolean zoom_expand; + + GdkRgbDither dither_quality; + + PixbufRendererScrollResetType scroll_reset; + + gboolean has_frame; + + GtkWidget *parent_window; /* resize parent_window when image dimensions change */ + + gboolean window_fit; + gboolean window_limit; + gint window_limit_size; + + gint tile_cache_max; /* max mb to use for offscreen buffer */ + + /*< private >*/ + + gint tile_width; + gint tile_height; + gint tile_cols; /* count of tile columns */ + GList *tiles; /* list of buffer tiles */ + gint tile_cache_size; /* allocated size of pixmaps/pixbufs */ + GList *draw_queue; /* list of areas to redraw */ + GList *draw_queue_2pass;/* list when 2 pass is enabled */ + + gint draw_idle_id; + gint draw_idle_high; /* current idle_id has high priority */ + + gboolean in_drag; + gint drag_last_x; + gint drag_last_y; + gint drag_moved; + + gboolean source_tiles_enabled; + gint source_tiles_cache_size; + + GList *source_tiles; /* list of active source tiles */ + gint source_tile_width; + gint source_tile_height; + + PixbufRendererTileRequestFunc func_tile_request; + PixbufRendererTileDisposeFunc func_tile_dispose; + + gpointer func_tile_data; + + gboolean delay_flip; + gboolean loading; + + gint scroller_id; + gint scroller_overlay; + gint scroller_x; + gint scroller_y; + gint scroller_xpos; + gint scroller_ypos; + gint scroller_xinc; + gint scroller_yinc; + + GList *overlay_list; +}; + +struct _PixbufRendererClass +{ + GtkEventBoxClass parent_class; + + void (* zoom) (PixbufRenderer *pr, gdouble zoom); + void (* clicked) (PixbufRenderer *pr, GdkEventButton *event); + void (* scroll_notify) (PixbufRenderer *pr); +}; + + +GType pixbuf_renderer_get_type (void); + +PixbufRenderer *pixbuf_renderer_new(void); + +void pixbuf_renderer_set_parent(PixbufRenderer *pr, GtkWindow *window); +GtkWindow *pixbuf_renderer_get_parent(PixbufRenderer *pr); + +/* display a pixbuf */ + +void pixbuf_renderer_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom); +GdkPixbuf *pixbuf_renderer_get_pixbuf(PixbufRenderer *pr); + +/* display an on-request array of pixbuf tiles */ + +void pixbuf_renderer_set_tiles(PixbufRenderer *pr, gint width, gint height, + gint tile_width, gint tile_height, gint cache_size, + PixbufRendererTileRequestFunc func_request, + PixbufRendererTileDisposeFunc func_dispose, + gpointer user_data, + gdouble zoom); +gint pixbuf_renderer_get_tiles(PixbufRenderer *pr); + +/* update region of existing image */ + +void pixbuf_renderer_area_changed(PixbufRenderer *pr, gint x, gint y, gint width, gint height); + +/* scrolling */ + +void pixbuf_renderer_scroll(PixbufRenderer *pr, gint x, gint y); +void pixbuf_renderer_scroll_to_point(PixbufRenderer *pr, gint x, gint y, + gdouble x_align, gdouble y_align); + +/* zoom */ + +void pixbuf_renderer_zoom_adjust(PixbufRenderer *pr, gdouble increment); +void pixbuf_renderer_zoom_adjust_at_point(PixbufRenderer *pr, gdouble increment, gint x, gint y); + +void pixbuf_renderer_zoom_set(PixbufRenderer *pr, gdouble zoom); +gdouble pixbuf_renderer_zoom_get(PixbufRenderer *pr); +gdouble pixbuf_renderer_zoom_get_scale(PixbufRenderer *pr); + +void pixbuf_renderer_zoom_set_limits(PixbufRenderer *pr, gdouble min, gdouble max); + +/* sizes */ + +gint pixbuf_renderer_get_image_size(PixbufRenderer *pr, gint *width, gint *height); +gint pixbuf_renderer_get_scaled_size(PixbufRenderer *pr, gint *width, gint *height); + +/* region of image in pixel coordinates */ +gint pixbuf_renderer_get_visible_rect(PixbufRenderer *pr, GdkRectangle *rect); + +/* actual size of the PixbufRenderer window minus borders, + * x and y are the scroll offset and include zoom factor. + */ +gint pixbuf_renderer_get_virtual_rect(PixbufRenderer *pr, GdkRectangle *rect); + +/* background color */ + +void pixbuf_renderer_set_color(PixbufRenderer *pr, GdkColor *color); +void pixbuf_renderer_set_black(PixbufRenderer *pr, gint black); + +/* overlay */ + +gint pixbuf_renderer_overlay_add(PixbufRenderer *pr, GdkPixbuf *pixbuf, gint x, gint y, + gint relative, gint always); +void pixbuf_renderer_overlay_set(PixbufRenderer *pr, gint id, GdkPixbuf *pixbuf, gint x, gint y); +gint pixbuf_renderer_overlay_get(PixbufRenderer *pr, gint id, GdkPixbuf **pixbuf, gint *x, gint *y); +void pixbuf_renderer_overlay_remove(PixbufRenderer *pr, gint id); + + +#endif + diff --git a/src/print.c b/src/print.c index e216c4a1..b12fa1f3 100644 --- a/src/print.c +++ b/src/print.c @@ -614,7 +614,7 @@ static void print_window_layout_size(PrintWindow *pw) sw = print_preview_unit(width); sh = print_preview_unit(height); - pixbuf = pw->layout_image->pixbuf; + pixbuf = image_get_pixbuf(pw->layout_image); if (!pixbuf || gdk_pixbuf_get_width(pixbuf) != sw || gdk_pixbuf_get_height(pixbuf) != sh) @@ -1836,7 +1836,7 @@ static gint print_job_preview_page_text(PrintWindow *pw, const gchar *text, gdou static gint print_job_preview_init(PrintWindow *pw) { if (pw->job_pixbuf) g_object_unref(pw->job_pixbuf); - pw->job_pixbuf = pw->layout_image->pixbuf; + pw->job_pixbuf = image_get_pixbuf(pw->layout_image); g_object_ref(pw->job_pixbuf); return print_job_preview_page_new(pw, pw->job_page); diff --git a/src/typedefs.h b/src/typedefs.h index 5e05533a..902ef493 100644 --- a/src/typedefs.h +++ b/src/typedefs.h @@ -219,69 +219,22 @@ typedef void (* ImageTileDisposeFunc)(ImageWindow *imd, gint x, gint y, struct _ImageWindow { GtkWidget *widget; /* use this to add it and show it */ - GtkWidget *image; + GtkWidget *pr; + GtkWidget *frame; gchar *image_path; const gchar *image_name; - gint image_width; /* image actual dimensions (pixels) */ - gint image_height; - GdkPixbuf *pixbuf; gint64 size; /* file size (bytes) */ time_t mtime; /* file modified time stamp */ gint unknown; /* failed to load image */ - gint window_width; /* allocated size of window (drawing area) */ - gint window_height; - - gint x_offset; /* offset of image start (non-zero when image < window) */ - gint y_offset; - - gint vis_width; /* dimensions of visible part of image */ - gint vis_height; - - gint width; /* size of scaled image (result) */ - gint height; - - gint x_scroll; /* scroll offset of image (into width, height to start drawing) */ - gint y_scroll; - - gdouble zoom_min; - gdouble zoom_max; - gdouble zoom; /* zoom we want (0 is auto) */ - gdouble scale; /* zoom we got (should never be 0) */ - - gint tile_width; - gint tile_height; - gint tile_cols; /* count of tile columns */ - GList *tiles; /* list of buffer tiles */ - GList *tile_cache; /* list of pixmap/pixbuf tile allocations */ - gint tile_cache_size; /* allocated size of pixmaps/pixbufs */ - GList *draw_queue; /* list of areas to redraw */ - - gint source_tiles_enabled; - gint source_tiles_cache_size; - - GList *source_tiles; /* list of active source tiles */ - gint source_tile_width; - gint source_tile_height; - - GList *draw_queue_2pass;/* list when 2 pass is enabled */ - ImageLoader *il; - gint draw_idle_id; - gint draw_idle_high; /* current idle_id has high priority */ - - gint in_drag; - gint drag_last_x; - gint drag_last_y; - gint drag_moved; - gint has_frame; - gint top_window_sync; /* resize top_window when image dimensions change */ /* top level (not necessarily parent) window */ + gint top_window_sync; /* resize top_window when image dimensions change */ GtkWidget *top_window; /* window that gets title, and window to resize when 'fitting' */ gchar *title; /* window title to display left of file name */ gchar *title_right; /* window title to display right of file name */ @@ -331,17 +284,6 @@ struct _ImageWindow gint auto_refresh_interval; gint delay_flip; - - gint scroller_id; - gint scroller_overlay; - gint scroller_x; - gint scroller_y; - gint scroller_xpos; - gint scroller_ypos; - gint scroller_xinc; - gint scroller_yinc; - - GList *overlay_list; }; struct _FileData { -- 2.20.1