Fix #926: Use system background color in window mode
[geeqie.git] / src / image.c
index 4c5edc7..1326ff1 100644 (file)
@@ -1,25 +1,35 @@
 /*
- * Geeqie
- * (C) 2006 John Ellis
- * Copyright (C) 2008 - 2009 The Geeqie Team
+ * Copyright (C) 2006 John Ellis
+ * Copyright (C) 2008 - 2016 The Geeqie Team
  *
  * 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!
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-
 #include "main.h"
 #include "image.h"
 
 
 #include "collect.h"
+#include "collect-table.h"
 #include "color-man.h"
 #include "exif.h"
 #include "metadata.h"
 #include "histogram.h"
+#include "history_list.h"
 #include "image-load.h"
 #include "image-overlay.h"
 #include "layout.h"
 
 static GList *image_list = NULL;
 
-static void image_update_title(ImageWindow *imd);
+void image_update_title(ImageWindow *imd);
 static void image_read_ahead_start(ImageWindow *imd);
 static void image_cache_set(ImageWindow *imd, FileData *fd);
 
+// For draw rectangle function
+static gint pixbuf_start_x;
+static gint pixbuf_start_y;
+static gint image_start_x;
+static gint image_start_y;
+static gint rect_x1, rect_x2, rect_y1, rect_y2;
+static gint rect_id = 0;
+
 /*
  *-------------------------------------------------------------------
  * 'signals'
@@ -48,6 +66,10 @@ static void image_cache_set(ImageWindow *imd, FileData *fd);
 static void image_click_cb(PixbufRenderer *pr, GdkEventButton *event, gpointer data)
 {
        ImageWindow *imd = data;
+       if (!options->image_lm_click_nav && event->button == MOUSE_BUTTON_MIDDLE)
+               {
+               imd->mouse_wheel_mode = !imd->mouse_wheel_mode;
+               }
 
        if (imd->func_button)
                {
@@ -55,10 +77,175 @@ static void image_click_cb(PixbufRenderer *pr, GdkEventButton *event, gpointer d
                }
 }
 
-static void image_drag_cb(PixbufRenderer *pr, GdkEventButton *event, gpointer data)
+static void switch_coords_orientation(ImageWindow *imd, gint x, gint y, gint width, gint height)
+{
+       switch (imd->orientation)
+               {
+               case EXIF_ORIENTATION_TOP_LEFT:
+                       /* normal -- nothing to do */
+                       rect_x1 = image_start_x;
+                       rect_y1 = image_start_y;
+                       rect_x2 = x;
+                       rect_y2 = y;
+                       break;
+               case EXIF_ORIENTATION_TOP_RIGHT:
+                       /* mirrored */
+                       rect_x1 = width - x;
+                       rect_y1 = image_start_y;
+                       rect_x2 = width - image_start_x;
+                       rect_y2 = y;
+                       break;
+               case EXIF_ORIENTATION_BOTTOM_RIGHT:
+                       /* upside down */
+                       rect_x1 = width - x;
+                       rect_y1 = height - y;
+                       rect_x2 = width - image_start_x;
+                       rect_y2 = height - image_start_y;
+                       break;
+               case EXIF_ORIENTATION_BOTTOM_LEFT:
+                       /* flipped */
+                       rect_x1 = image_start_x;
+                       rect_y1 = height - y;
+                       rect_x2 = x;
+                       rect_y2 = height - image_start_y;
+                       break;
+               case EXIF_ORIENTATION_LEFT_TOP:
+                       /* left mirrored */
+                       rect_x1 = image_start_y;
+                       rect_y1 = image_start_x;
+                       rect_x2 = y;
+                       rect_y2 = x;
+                       break;
+               case EXIF_ORIENTATION_RIGHT_TOP:
+                       /* rotated -90 (270) */
+                       rect_x1 = image_start_y;
+                       rect_y1 = width - x;
+                       rect_x2 = y;
+                       rect_y2 = width - image_start_x;
+                       break;
+               case EXIF_ORIENTATION_RIGHT_BOTTOM:
+                       /* right mirrored */
+                       rect_x1 = height - y;
+                       rect_y1 = width - x;
+                       rect_x2 = height - image_start_y;
+                       rect_y2 = width - image_start_x;
+                       break;
+               case EXIF_ORIENTATION_LEFT_BOTTOM:
+                       /* rotated 90 */
+                       rect_x1 = height - y;
+                       rect_y1 = image_start_x;
+                       rect_x2 = height - image_start_y;
+                       rect_y2 = x;
+                       break;
+               default:
+                       /* The other values are out of range */
+                       break;
+               }
+}
+
+static void image_press_cb(PixbufRenderer *pr, GdkEventButton *event, gpointer data)
+{
+       ImageWindow *imd = data;
+       LayoutWindow *lw;
+       gint x_pixel, y_pixel;
+
+       if(options->draw_rectangle)
+               {
+               pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
+
+               pixbuf_start_x = event->x;
+               pixbuf_start_y = event->y;
+
+               if (x_pixel == -1)
+                       {
+                       image_start_x = 0;
+                       }
+               else
+                       {
+                       image_start_x = x_pixel;
+                       }
+
+               if (y_pixel == -1)
+                       {
+                       image_start_y = 0;
+                       }
+               else
+                       {
+                       image_start_y = y_pixel;
+                       }
+               }
+
+       if (rect_id)
+               {
+               pixbuf_renderer_overlay_remove((PixbufRenderer *)imd->pr, rect_id);
+               }
+
+       lw = layout_find_by_image(imd);
+       if (lw && event->button == MOUSE_BUTTON_LEFT && event->type == GDK_2BUTTON_PRESS
+                                                                                               && !options->image_lm_click_nav)
+               {
+               if (lw->full_screen)
+                       layout_image_full_screen_stop(lw);
+               }
+}
+
+static void image_drag_cb(PixbufRenderer *pr, GdkEventMotion *event, gpointer data)
 {
        ImageWindow *imd = data;
        gint width, height;
+       gint rect_width;
+       gint rect_height;
+       GdkPixbuf *rect_pixbuf;
+       gint x_pixel, y_pixel;
+       gint image_x_pixel, image_y_pixel;
+
+       if (options->draw_rectangle)
+               {
+               pixbuf_renderer_get_image_size(pr, &width, &height);
+               pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
+
+               if (x_pixel == -1)
+                       {
+                       image_x_pixel = width;
+                       }
+               else
+                       {
+                       image_x_pixel = x_pixel;
+                       }
+
+               if (y_pixel == -1)
+                       {
+                       image_y_pixel = height;
+                       }
+               else
+                       {
+                       image_y_pixel = y_pixel;
+                       }
+
+               switch_coords_orientation(imd, image_x_pixel, image_y_pixel, width, height);
+               if (rect_id)
+                       {
+                       pixbuf_renderer_overlay_remove((PixbufRenderer *)imd->pr, rect_id);
+                       }
+
+               rect_width = pr->drag_last_x - pixbuf_start_x;
+               if (rect_width <= 0)
+                       {
+                       rect_width = 1;
+                       }
+               rect_height = pr->drag_last_y - pixbuf_start_y;
+               if (rect_height <= 0)
+                       {
+                       rect_height = 1;
+                       }
+
+               rect_pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, rect_width, rect_height);
+               pixbuf_set_rect_fill(rect_pixbuf, 0, 0, rect_width, rect_height, 255, 255, 255, 0);
+               pixbuf_set_rect(rect_pixbuf, 1, 1, rect_width-2, rect_height - 2, 0, 0, 0, 255, 1, 1, 1, 1);
+               pixbuf_set_rect(rect_pixbuf, 2, 2, rect_width-4, rect_height - 4, 255, 255, 255, 255, 1, 1, 1, 1);
+
+               rect_id = pixbuf_renderer_overlay_add((PixbufRenderer *)imd->pr, rect_pixbuf, pixbuf_start_x, pixbuf_start_y, OVL_NORMAL);
+               }
 
        pixbuf_renderer_get_scaled_size(pr, &width, &height);
 
@@ -91,15 +278,6 @@ static void image_update_util(ImageWindow *imd)
        if (imd->func_update) imd->func_update(imd, imd->data_update);
 }
 
-static void image_zoom_cb(PixbufRenderer *pr, gdouble zoom, gpointer data)
-{
-       ImageWindow *imd = data;
-
-       if (imd->title_show_zoom) image_update_title(imd);
-       if (imd->overlay_show_zoom) image_osd_update(imd);
-
-       image_update_util(imd);
-}
 
 static void image_complete_util(ImageWindow *imd, gboolean preload)
 {
@@ -140,17 +318,28 @@ static void image_state_unset(ImageWindow *imd, ImageState state)
        if (imd->func_state) imd->func_state(imd, state, imd->data_state);
 }
 
+static void image_zoom_cb(PixbufRenderer *pr, gdouble zoom, gpointer data)
+{
+       ImageWindow *imd = data;
+
+       if (imd->title_show_zoom) image_update_title(imd);
+       image_state_set(imd, IMAGE_STATE_IMAGE);
+       image_update_util(imd);
+}
+
 /*
  *-------------------------------------------------------------------
  * misc
  *-------------------------------------------------------------------
  */
 
-static void image_update_title(ImageWindow *imd)
+void image_update_title(ImageWindow *imd)
 {
        gchar *title = NULL;
        gchar *zoom = NULL;
        gchar *collection = NULL;
+       LayoutWindow *lw;
+       gchar *lw_ident = NULL;
 
        if (!imd->top_window) return;
 
@@ -168,13 +357,25 @@ static void image_update_title(ImageWindow *imd)
                g_free(buf);
                }
 
-       title = g_strdup_printf("%s%s%s%s%s%s",
+       lw = layout_find_by_image(imd);
+       if (lw)
+               {
+               lw_ident = g_strconcat(" (", lw->options.id, ")", NULL);
+               }
+
+       title = g_strdup_printf("%s%s%s%s%s%s%s",
                imd->title ? imd->title : "",
                imd->image_fd ? imd->image_fd->name : "",
                zoom ? zoom : "",
                collection ? collection : "",
                imd->image_fd ? " - " : "",
-               imd->title_right ? imd->title_right : "");
+               imd->title_right ? imd->title_right : "",
+               options->show_window_ids ? (lw_ident ? lw_ident : "") : ""
+               );
+       if (lw_ident)
+               {
+               g_free(lw_ident);
+               }
 
        gtk_window_set_title(GTK_WINDOW(imd->top_window), title);
 
@@ -220,7 +421,7 @@ static gboolean image_post_process_color(ImageWindow *imd, gint start_row, gbool
            imd->color_profile_input <  COLOR_PROFILE_FILE + COLOR_PROFILE_INPUTS)
                {
                const gchar *file = options->color_profile.input_file[imd->color_profile_input - COLOR_PROFILE_FILE];
-       
+
                if (!is_readable_file(file)) return FALSE;
 
                input_type = COLOR_PROFILE_FILE;
@@ -259,7 +460,7 @@ static gboolean image_post_process_color(ImageWindow *imd, gint start_row, gbool
        imd->color_profile_from_image = COLOR_PROFILE_NONE;
 
        exif = exif_read_fd(imd->image_fd);
-               
+
        if (exif)
                {
                profile = exif_get_color_profile(exif, &profile_len);
@@ -320,7 +521,7 @@ static gboolean image_post_process_color(ImageWindow *imd, gint start_row, gbool
 
                exif_free_fd(imd->image_fd, exif);
                }
-       
+
 
        if (profile)
                {
@@ -345,12 +546,16 @@ static gboolean image_post_process_color(ImageWindow *imd, gint start_row, gbool
                        }
 
                imd->cm = (gpointer)cm;
-#if 0
-               if (run_in_bg) color_man_start_bg(imd->cm, image_post_process_color_cb, imd);
-#endif
                }
 
        image_update_util(imd);
+
+       if (screen_profile)
+               {
+               g_free(screen_profile);
+               screen_profile = NULL;
+               }
+
        return !!cm;
 }
 
@@ -360,10 +565,10 @@ static void image_post_process_tile_color_cb(PixbufRenderer *pr, GdkPixbuf **pix
        ImageWindow *imd = (ImageWindow *)data;
        if (imd->cm) color_man_correct_region(imd->cm, *pixbuf, x, y, w, h);
        if (imd->desaturate) pixbuf_desaturate_rect(*pixbuf, x, y, w, h);
-
+       if (imd->overunderexposed) pixbuf_highlight_overunderexposed(*pixbuf, x, y, w, h);
 }
 
-void image_alter_orientation(ImageWindow *imd, AlterType type)
+void image_alter_orientation(ImageWindow *imd, FileData *fd_n, AlterType type)
 {
        static const gint rotate_90[]    = {1,   6, 7, 8, 5, 2, 3, 4, 1};
        static const gint rotate_90_cc[] = {1,   8, 5, 6, 7, 4, 1, 2, 3};
@@ -371,74 +576,108 @@ void image_alter_orientation(ImageWindow *imd, AlterType type)
        static const gint mirror[]       = {1,   2, 1, 4, 3, 6, 5, 8, 7};
        static const gint flip[]         = {1,   4, 3, 2, 1, 8, 7, 6, 5};
 
+       gint orientation;
 
-       if (!imd || !imd->pr || !imd->image_fd) return;
+       if (!imd || !imd->pr || !imd->image_fd || !fd_n) return;
 
-       if (imd->orientation < 1 || imd->orientation > 8) imd->orientation = 1;
+       orientation = EXIF_ORIENTATION_TOP_LEFT;
+       {
+       if (fd_n->user_orientation)
+               {
+               orientation = fd_n->user_orientation;
+               }
+       else
+               if (options->metadata.write_orientation)
+                       {
+                       if (g_strcmp0(imd->image_fd->format_name, "heif") == 0)
+                               {
+                               orientation = EXIF_ORIENTATION_TOP_LEFT;
+                               }
+                       else
+                               {
+                               orientation = metadata_read_int(fd_n, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
+                               }
+                       }
+       }
 
        switch (type)
                {
                case ALTER_ROTATE_90:
-                       imd->orientation = rotate_90[imd->orientation];
+                       orientation = rotate_90[orientation];
                        break;
                case ALTER_ROTATE_90_CC:
-                       imd->orientation = rotate_90_cc[imd->orientation];
+                       orientation = rotate_90_cc[orientation];
                        break;
                case ALTER_ROTATE_180:
-                       imd->orientation = rotate_180[imd->orientation];
+                       orientation = rotate_180[orientation];
                        break;
                case ALTER_MIRROR:
-                       imd->orientation = mirror[imd->orientation];
+                       orientation = mirror[orientation];
                        break;
                case ALTER_FLIP:
-                       imd->orientation = flip[imd->orientation];
+                       orientation = flip[orientation];
                        break;
                case ALTER_NONE:
-                       imd->orientation = imd->image_fd->exif_orientation ? imd->image_fd->exif_orientation : 1;
+                       orientation = fd_n->exif_orientation ? fd_n->exif_orientation : 1;
                        break;
                default:
                        return;
                        break;
                }
 
-       if (imd->orientation != imd->image_fd->exif_orientation ? imd->image_fd->exif_orientation : 1)
+       if (orientation != (fd_n->exif_orientation ? fd_n->exif_orientation : 1))
                {
-               if (!options->metadata.write_orientation)
+               if (g_strcmp0(fd_n->format_name, "heif") != 0)
                        {
-                       /* user_orientation does not work together with options->metadata.write_orientation,
-                          use either one or the other.
-                          we must however handle switching metadata.write_orientation on and off, therefore
-                          we just disable referencing new fd's, not unreferencing the old ones
-                       */
-                       if (imd->image_fd->user_orientation == 0) file_data_ref(imd->image_fd);
-                       imd->image_fd->user_orientation = imd->orientation;
+                       if (!options->metadata.write_orientation)
+                               {
+                               /* user_orientation does not work together with options->metadata.write_orientation,
+                                  use either one or the other.
+                                  we must however handle switching metadata.write_orientation on and off, therefore
+                                  we just disable referencing new fd's, not unreferencing the old ones
+                               */
+                               if (fd_n->user_orientation == 0) file_data_ref(fd_n);
+                               fd_n->user_orientation = orientation;
+                               }
+                       }
+               else
+                       {
+                       if (fd_n->user_orientation == 0) file_data_ref(fd_n);
+                       fd_n->user_orientation = orientation;
                        }
                }
        else
                {
-               if (imd->image_fd->user_orientation != 0) file_data_unref(imd->image_fd);
-               imd->image_fd->user_orientation = 0;
+               if (fd_n->user_orientation != 0) file_data_unref(fd_n);
+               fd_n->user_orientation = 0;
                }
 
-       if (options->metadata.write_orientation)
+       if (g_strcmp0(fd_n->format_name, "heif") != 0)
                {
-               if (type == ALTER_NONE)
-                       {
-                       metadata_write_revert(imd->image_fd, ORIENTATION_KEY);
-                       }
-               else
+               if (options->metadata.write_orientation)
                        {
-                       metadata_write_int(imd->image_fd, ORIENTATION_KEY, imd->orientation);
+                       if (type == ALTER_NONE)
+                               {
+                               metadata_write_revert(fd_n, ORIENTATION_KEY);
+                               }
+                       else
+                               {
+                               metadata_write_int(fd_n, ORIENTATION_KEY, orientation);
+                               }
                        }
                }
 
-       pixbuf_renderer_set_orientation((PixbufRenderer *)imd->pr, imd->orientation);
+       if (imd->image_fd == fd_n && !(options->metadata.write_orientation && !options->image.exif_rotate_enable))
+               {
+               imd->orientation = orientation;
+               pixbuf_renderer_set_orientation((PixbufRenderer *)imd->pr, orientation);
+               }
 }
 
 void image_set_desaturate(ImageWindow *imd, gboolean desaturate)
 {
        imd->desaturate = desaturate;
-       if (imd->cm || imd->desaturate)
+       if (imd->cm || imd->desaturate || imd->overunderexposed)
                pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, image_post_process_tile_color_cb, (gpointer) imd, (imd->cm != NULL) );
        else
                pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, NULL, NULL, TRUE);
@@ -450,6 +689,26 @@ gboolean image_get_desaturate(ImageWindow *imd)
        return imd->desaturate;
 }
 
+void image_set_overunderexposed(ImageWindow *imd, gboolean overunderexposed)
+{
+       imd->overunderexposed = overunderexposed;
+       if (imd->cm || imd->desaturate || imd->overunderexposed)
+               pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, image_post_process_tile_color_cb, (gpointer) imd, (imd->cm != NULL) );
+       else
+               pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, NULL, NULL, TRUE);
+       pixbuf_renderer_set_orientation((PixbufRenderer *)imd->pr, imd->orientation);
+}
+
+gboolean image_get_overunderexposed(ImageWindow *imd)
+{
+       return imd->overunderexposed;
+}
+
+void image_set_ignore_alpha(ImageWindow *imd, gboolean ignore_alpha)
+{
+   pixbuf_renderer_set_ignore_alpha((PixbufRenderer *)imd->pr, ignore_alpha);
+}
+
 /*
  *-------------------------------------------------------------------
  * read ahead (prebuffer)
@@ -471,6 +730,8 @@ static void image_read_ahead_done_cb(ImageLoader *il, gpointer data)
 {
        ImageWindow *imd = data;
 
+       if (!imd->read_ahead_fd || !imd->read_ahead_il) return;
+
        DEBUG_1("%s read ahead done for :%s", get_exec_time(), imd->read_ahead_fd->path);
 
        if (!imd->read_ahead_fd->pixbuf)
@@ -505,7 +766,7 @@ static void image_read_ahead_start(ImageWindow *imd)
        DEBUG_1("%s read ahead started for :%s", get_exec_time(), imd->read_ahead_fd->path);
 
        imd->read_ahead_il = image_loader_new(imd->read_ahead_fd);
-       
+
        image_loader_delay_area_ready(imd->read_ahead_il, TRUE); /* we will need the area_ready signals later */
 
        g_signal_connect(G_OBJECT(imd->read_ahead_il), "error", (GCallback)image_read_ahead_error_cb, imd);
@@ -569,7 +830,7 @@ static gint image_cache_get(ImageWindow *imd)
                g_assert(imd->image_fd->pixbuf);
                image_change_pixbuf(imd, imd->image_fd->pixbuf, image_zoom_get(imd), FALSE);
                }
-       
+
 //     file_cache_dump(image_get_cache());
        return success;
 }
@@ -624,7 +885,30 @@ static void image_load_done_cb(ImageLoader *il, gpointer data)
                {
                GdkPixbuf *pixbuf;
 
-               pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
+               switch (imd->image_fd->format_class)
+                       {
+                       case FORMAT_CLASS_UNKNOWN:
+                               pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
+                               break;
+                       case FORMAT_CLASS_META:
+                               pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
+                               break;
+                       case FORMAT_CLASS_VIDEO:
+                               pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
+                               break;
+                       case FORMAT_CLASS_COLLECTION:
+                               pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
+                               break;
+                       case FORMAT_CLASS_DOCUMENT:
+                               pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
+                               break;
+                       case FORMAT_CLASS_ARCHIVE:
+                               pixbuf = pixbuf_inline(PIXBUF_INLINE_ARCHIVE);
+                               break;
+                       default:
+                               pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
+                       }
+
                image_change_pixbuf(imd, pixbuf, image_zoom_get(imd), FALSE);
                g_object_unref(pixbuf);
 
@@ -645,6 +929,14 @@ static void image_load_done_cb(ImageLoader *il, gpointer data)
        image_read_ahead_start(imd);
 }
 
+static void image_load_size_cb(ImageLoader *il, guint width, guint height, gpointer data)
+{
+       ImageWindow *imd = data;
+
+       DEBUG_1("image_load_size_cb: %dx%d", width, height);
+       pixbuf_renderer_set_size_early((PixbufRenderer *)imd->pr, width, height);
+}
+
 static void image_load_error_cb(ImageLoader *il, gpointer data)
 {
        DEBUG_1("%s image error", get_exec_time());
@@ -667,6 +959,7 @@ static void image_load_set_signals(ImageWindow *imd, gboolean override_old_signa
        g_signal_connect(G_OBJECT(imd->il), "area_ready", (GCallback)image_load_area_cb, imd);
        g_signal_connect(G_OBJECT(imd->il), "error", (GCallback)image_load_error_cb, imd);
        g_signal_connect(G_OBJECT(imd->il), "done", (GCallback)image_load_done_cb, imd);
+       g_signal_connect(G_OBJECT(imd->il), "size_prepared", (GCallback)image_load_size_cb, imd);
 }
 
 /* this read ahead is located here merely for the callbacks, above */
@@ -775,7 +1068,7 @@ static gboolean image_load_begin(ImageWindow *imd, FileData *fd)
                {
                image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), TRUE);
                }
-*/     
+*/
        return TRUE;
 }
 
@@ -819,10 +1112,19 @@ static void image_change_complete(ImageWindow *imd, gdouble zoom)
                if (is_readable_file(imd->image_fd->path))
                        {
                        PixbufRenderer *pr;
-       
+
                        pr = PIXBUF_RENDERER(imd->pr);
                        pr->zoom = zoom;        /* store the zoom, needed by the loader */
 
+                       /* Disable 2-pass for GIFs. Animated GIFs can flicker when enabled
+                        * Reduce quality to worst but fastest to avoid dropped frames */
+                       if (g_ascii_strcasecmp(imd->image_fd->extension, ".GIF") == 0)
+                               {
+                               g_object_set(G_OBJECT(imd->pr), "zoom_2pass", FALSE, NULL);
+                               g_object_set(G_OBJECT(imd->pr), "zoom_quality", GDK_INTERP_NEAREST, NULL);
+                               }
+
+
                        if (image_load_begin(imd, imd->image_fd))
                                {
                                imd->unknown = FALSE;
@@ -832,7 +1134,7 @@ static void image_change_complete(ImageWindow *imd, gdouble zoom)
                if (imd->unknown == TRUE)
                        {
                        GdkPixbuf *pixbuf;
-       
+
                        pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
                        image_change_pixbuf(imd, pixbuf, zoom, FALSE);
                        g_object_unref(pixbuf);
@@ -871,44 +1173,10 @@ static void image_change_real(ImageWindow *imd, FileData *fd,
  *-------------------------------------------------------------------
  */
 
-static void image_focus_paint(ImageWindow *imd, gboolean has_focus, GdkRectangle *area)
-{
-       GtkWidget *widget;
-
-       widget = imd->widget;
-       if (!widget->window) return;
-
-       if (has_focus)
-               {
-               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
-               {
-               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);
-               }
-}
-
-static gboolean 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;
-}
-
 static gboolean image_focus_in_cb(GtkWidget *widget, GdkEventFocus *event, gpointer data)
 {
        ImageWindow *imd = data;
 
-       GTK_WIDGET_SET_FLAGS(imd->widget, GTK_HAS_FOCUS);
-       image_focus_paint(imd, TRUE, NULL);
-
        if (imd->func_focus_in)
                {
                imd->func_focus_in(imd, imd->data_focus_in);
@@ -917,25 +1185,50 @@ static gboolean image_focus_in_cb(GtkWidget *widget, GdkEventFocus *event, gpoin
        return TRUE;
 }
 
-static gboolean image_focus_out_cb(GtkWidget *widget, GdkEventFocus *event, gpointer data)
-{
-       ImageWindow *imd = data;
-
-       GTK_WIDGET_UNSET_FLAGS(imd->widget, GTK_HAS_FOCUS);
-       image_focus_paint(imd, FALSE, NULL);
-
-       return TRUE;
-}
-
 static gboolean image_scroll_cb(GtkWidget *widget, GdkEventScroll *event, gpointer data)
 {
        ImageWindow *imd = data;
+       gboolean in_lw = FALSE;
+       gint i = 0;
+       LayoutWindow *lw = NULL;
 
-       if (imd->func_scroll &&
-           event && event->type == GDK_SCROLL)
+       if (imd->func_scroll && event && event->type == GDK_SCROLL)
                {
-               imd->func_scroll(imd, event, imd->data_scroll);
-               return TRUE;
+               layout_valid(&lw);
+               /* check if the image is in a layout window */
+               for (i = 0; i < MAX_SPLIT_IMAGES; i++)
+                       {
+                       if (imd == lw->split_images[i])
+                               {
+                               in_lw = TRUE;
+                               break;
+                               }
+                       }
+
+               if (in_lw)
+                       {
+                       if (lw->options.split_pane_sync)
+                               {
+                               for (i = 0; i < MAX_SPLIT_IMAGES; i++)
+                                       {
+                                       if (lw->split_images[i])
+                                               {
+                                               layout_image_activate(lw, i, FALSE);
+                                               imd->func_scroll(lw->split_images[i], event, lw->split_images[i]->data_scroll);
+                                               }
+                                       }
+                               }
+                       else
+                               {
+                               imd->func_scroll(imd, event, imd->data_scroll);
+                               }
+                       return TRUE;
+                       }
+               else
+                       {
+                       imd->func_scroll(imd, event, imd->data_scroll);
+                       return TRUE;
+                       }
                }
 
        return FALSE;
@@ -950,6 +1243,8 @@ static gboolean image_scroll_cb(GtkWidget *widget, GdkEventScroll *event, gpoint
 void image_attach_window(ImageWindow *imd, GtkWidget *window,
                         const gchar *title, const gchar *title_right, gboolean show_zoom)
 {
+       LayoutWindow *lw;
+
        imd->top_window = window;
        g_free(imd->title);
        imd->title = g_strdup(title);
@@ -957,7 +1252,9 @@ void image_attach_window(ImageWindow *imd, GtkWidget *window,
        imd->title_right = g_strdup(title_right);
        imd->title_show_zoom = show_zoom;
 
-       if (!options->image.fit_window_to_image) window = NULL;
+       lw = layout_find_by_image(imd);
+
+       if (!(options->image.fit_window_to_image && lw && (lw->options.tools_float || lw->options.tools_hidden))) window = NULL;
 
        pixbuf_renderer_set_parent((PixbufRenderer *)imd->pr, (GtkWindow *)window);
 
@@ -998,7 +1295,7 @@ void image_set_button_func(ImageWindow *imd,
 }
 
 void image_set_drag_func(ImageWindow *imd,
-                          void (*func)(ImageWindow *, GdkEventButton *event, gdouble dx, gdouble dy, gpointer),
+                          void (*func)(ImageWindow *, GdkEventMotion *event, gdouble dx, gdouble dy, gpointer),
                           gpointer data)
 {
        imd->func_drag = func;
@@ -1085,12 +1382,14 @@ GdkPixbuf *image_get_pixbuf(ImageWindow *imd)
 
 void image_change_pixbuf(ImageWindow *imd, GdkPixbuf *pixbuf, gdouble zoom, gboolean lazy)
 {
+       LayoutWindow *lw;
+       StereoPixbufData stereo_data = STEREO_PIXBUF_DEFAULT;
+       /* read_exif and similar functions can actually notice that the file has changed and trigger
+          a notification that removes the pixbuf from cache and unrefs it. Therefore we must ref it
+          here before it is taken over by the renderer. */
+       if (pixbuf) g_object_ref(pixbuf);
 
-
-       /* read_exif and similar functions can actually notice that the file has changed and triger a notification
-       that removes the pixbuf from cache and unref it. Therefore we must ref it here before it is taken over by the renderer. */
-       if (pixbuf) g_object_ref(pixbuf); 
-       
+       imd->orientation = EXIF_ORIENTATION_TOP_LEFT;
        if (imd->image_fd)
                {
                if (imd->image_fd->user_orientation)
@@ -1099,8 +1398,25 @@ void image_change_pixbuf(ImageWindow *imd, GdkPixbuf *pixbuf, gdouble zoom, gboo
                        }
                else if (options->image.exif_rotate_enable)
                        {
-                       imd->orientation = metadata_read_int(imd->image_fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
-                       imd->image_fd->exif_orientation = imd->orientation;
+                       if (g_strcmp0(imd->image_fd->format_name, "heif") == 0)
+                               {
+                               imd->orientation = EXIF_ORIENTATION_TOP_LEFT;
+                               imd->image_fd->exif_orientation = imd->orientation;
+                               }
+                       else
+                               {
+                               imd->orientation = metadata_read_int(imd->image_fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
+                               imd->image_fd->exif_orientation = imd->orientation;
+                               }
+                       }
+               }
+
+       if (pixbuf)
+               {
+               stereo_data = imd->user_stereo;
+               if (stereo_data == STEREO_PIXBUF_DEFAULT)
+                       {
+                       stereo_data = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(pixbuf), "stereo_data"));
                        }
                }
 
@@ -1113,22 +1429,25 @@ void image_change_pixbuf(ImageWindow *imd, GdkPixbuf *pixbuf, gdouble zoom, gboo
 
        if (lazy)
                {
-               pixbuf_renderer_set_pixbuf_lazy((PixbufRenderer *)imd->pr, pixbuf, zoom, imd->orientation);
+               pixbuf_renderer_set_pixbuf_lazy((PixbufRenderer *)imd->pr, pixbuf, zoom, imd->orientation, stereo_data);
                }
        else
                {
                pixbuf_renderer_set_pixbuf((PixbufRenderer *)imd->pr, pixbuf, zoom);
                pixbuf_renderer_set_orientation((PixbufRenderer *)imd->pr, imd->orientation);
+               pixbuf_renderer_set_stereo_data((PixbufRenderer *)imd->pr, stereo_data);
                }
 
        if (pixbuf) g_object_unref(pixbuf);
 
-       if (imd->color_profile_enable)
+       /* Color correction takes too much time for an animated gif */
+       lw = layout_find_by_image(imd);
+       if (imd->color_profile_enable && lw && !lw->animation)
                {
-               image_post_process_color(imd, 0, FALSE); /* TODO: error handling */
+               image_post_process_color(imd, 0, FALSE); /** @todo error handling */
                }
 
-       if (imd->cm || imd->desaturate)
+       if (imd->cm || imd->desaturate || imd->overunderexposed)
                pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, image_post_process_tile_color_cb, (gpointer) imd, (imd->cm != NULL) );
 
        image_state_set(imd, IMAGE_STATE_IMAGE);
@@ -1136,9 +1455,23 @@ void image_change_pixbuf(ImageWindow *imd, GdkPixbuf *pixbuf, gdouble zoom, gboo
 
 void image_change_from_collection(ImageWindow *imd, CollectionData *cd, CollectInfo *info, gdouble zoom)
 {
+       CollectWindow *cw;
+
        if (!cd || !info || !g_list_find(cd->list, info)) return;
 
        image_change_real(imd, info->fd, cd, info, zoom);
+       cw = collection_window_find(cd);
+       if (cw)
+               {
+               collection_table_set_focus(cw->table, info);
+               collection_table_unselect_all(cw->table);
+               collection_table_select(cw->table,info);
+               }
+
+       if (info->fd)
+               {
+               image_chain_append_end(info->fd->path);
+               }
 }
 
 CollectionData *image_get_collection(ImageWindow *imd, CollectInfo **info)
@@ -1170,7 +1503,7 @@ static void image_loader_sync_read_ahead_data(ImageLoader *il, gpointer old_data
 }
 
 static void image_loader_sync_data(ImageLoader *il, gpointer old_data, gpointer data)
-{              
+{
        if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (GCallback)image_load_area_cb, old_data))
                g_signal_connect(G_OBJECT(il), "area_ready", (GCallback)image_load_area_cb, data);
 
@@ -1184,7 +1517,7 @@ static void image_loader_sync_data(ImageLoader *il, gpointer old_data, gpointer
 /* this is more like a move function
  * it moves most data from source to imd
  */
-void image_change_from_image(ImageWindow *imd, ImageWindow *source)
+void image_move_from_image(ImageWindow *imd, ImageWindow *source)
 {
        if (imd == source) return;
 
@@ -1210,6 +1543,58 @@ void image_change_from_image(ImageWindow *imd, ImageWindow *source)
                source->delay_alter_type = ALTER_NONE;
                }
 
+       imd->color_profile_enable = source->color_profile_enable;
+       imd->color_profile_input = source->color_profile_input;
+       imd->color_profile_use_image = source->color_profile_use_image;
+       color_man_free((ColorMan *)imd->cm);
+       imd->cm = NULL;
+       if (source->cm)
+               {
+               ColorMan *cm;
+
+               imd->cm = source->cm;
+               source->cm = NULL;
+
+               cm = (ColorMan *)imd->cm;
+               cm->imd = imd;
+               cm->func_done_data = imd;
+               }
+
+       file_data_unref(imd->read_ahead_fd);
+       source->read_ahead_fd = NULL;
+
+       imd->orientation = source->orientation;
+       imd->desaturate = source->desaturate;
+
+       imd->user_stereo = source->user_stereo;
+
+       pixbuf_renderer_move(PIXBUF_RENDERER(imd->pr), PIXBUF_RENDERER(source->pr));
+
+       if (imd->cm || imd->desaturate || imd->overunderexposed)
+               pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, image_post_process_tile_color_cb, (gpointer) imd, (imd->cm != NULL) );
+       else
+               pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, NULL, NULL, TRUE);
+
+}
+
+/* this is  a copy function
+ * source stays unchanged
+ */
+void image_copy_from_image(ImageWindow *imd, ImageWindow *source)
+{
+       if (imd == source) return;
+
+       imd->unknown = source->unknown;
+
+       imd->collection = source->collection;
+       imd->collection_info = source->collection_info;
+
+       image_loader_free(imd->il);
+       imd->il = NULL;
+
+       image_set_fd(imd, image_get_fd(source));
+
+
        imd->color_profile_enable = source->color_profile_enable;
        imd->color_profile_input = source->color_profile_input;
        imd->color_profile_use_image = source->color_profile_use_image;
@@ -1243,15 +1628,18 @@ void image_change_from_image(ImageWindow *imd, ImageWindow *source)
        imd->orientation = source->orientation;
        imd->desaturate = source->desaturate;
 
-       pixbuf_renderer_move(PIXBUF_RENDERER(imd->pr), PIXBUF_RENDERER(source->pr));
+       imd->user_stereo = source->user_stereo;
+
+       pixbuf_renderer_copy(PIXBUF_RENDERER(imd->pr), PIXBUF_RENDERER(source->pr));
 
-       if (imd->cm || imd->desaturate)
+       if (imd->cm || imd->desaturate || imd->overunderexposed)
                pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, image_post_process_tile_color_cb, (gpointer) imd, (imd->cm != NULL) );
        else
                pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, NULL, NULL, TRUE);
 
 }
 
+
 /* manipulation */
 
 void image_area_changed(ImageWindow *imd, gint x, gint y, gint width, gint height)
@@ -1320,11 +1708,11 @@ void image_zoom_set_fill_geometry(ImageWindow *imd, gboolean vertical)
 
        if (vertical)
                {
-               zoom = (gdouble)pr->window_height / height;
+               zoom = (gdouble)pr->viewport_height / height;
                }
        else
                {
-               zoom = (gdouble)pr->window_width / width;
+               zoom = (gdouble)pr->viewport_width / width;
                }
 
        if (zoom < 1.0)
@@ -1379,8 +1767,8 @@ gchar *image_zoom_get_as_text(ImageWindow *imd)
                approx = "~";
                }
 
-       if (rint(l) != l) pl = 1;
-       if (rint(r) != r) pr = 1;
+       if (rint(l) != l) pl = 2;
+       if (rint(r) != r) pr = 2;
 
        return g_strdup_printf("%.*f :%s%.*f", pl, l, approx, pr, r);
 }
@@ -1404,6 +1792,36 @@ gdouble image_zoom_get_default(ImageWindow *imd)
        return zoom;
 }
 
+/* stereo */
+gint image_stereo_get(ImageWindow *imd)
+{
+       return pixbuf_renderer_stereo_get((PixbufRenderer *)imd->pr);
+}
+
+void image_stereo_set(ImageWindow *imd, gint stereo_mode)
+{
+       DEBUG_1("Setting stereo mode %04x for imd %p", stereo_mode, imd);
+       pixbuf_renderer_stereo_set((PixbufRenderer *)imd->pr, stereo_mode);
+}
+
+void image_stereo_swap(ImageWindow *imd)
+{
+       gint stereo_mode = pixbuf_renderer_stereo_get((PixbufRenderer *)imd->pr);
+       stereo_mode ^= PR_STEREO_SWAP;
+       pixbuf_renderer_stereo_set((PixbufRenderer *)imd->pr, stereo_mode);
+}
+
+StereoPixbufData image_stereo_pixbuf_get(ImageWindow *imd)
+{
+       return imd->user_stereo;
+}
+
+void image_stereo_pixbuf_set(ImageWindow *imd, StereoPixbufData stereo_mode)
+{
+       imd->user_stereo = stereo_mode;
+       image_reload(imd);
+}
+
 /* read ahead */
 
 void image_prebuffer_set(ImageWindow *imd, FileData *fd)
@@ -1433,8 +1851,12 @@ static void image_notify_cb(FileData *fd, NotifyType type, gpointer data)
            imd->state == IMAGE_STATE_NONE /* loading not started, no need to reload */
            ) return;
 
-       if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE)) && fd == imd->image_fd)
+       if ((type & NOTIFY_REREAD) && fd == imd->image_fd)
                {
+               /* there is no need to reload on NOTIFY_CHANGE,
+                  modified files should be detacted anyway and NOTIFY_REREAD should be received
+                  or they are removed from the filelist completely on "move" and "delete"
+               */
                DEBUG_1("Notify image: %s %04x", fd->path, type);
                image_reload(imd);
                }
@@ -1466,13 +1888,35 @@ void image_background_set_color(ImageWindow *imd, GdkColor *color)
 void image_background_set_color_from_options(ImageWindow *imd, gboolean fullscreen)
 {
        GdkColor *color = NULL;
+#if GTK_CHECK_VERSION(3,0,0)
+       GdkColor theme_color;
+       GdkRGBA bg_color;
+       GtkStyleContext *style_context;
+       LayoutWindow *lw = NULL;
+#endif
 
-       if (options->image.use_custom_border_color
-           && (!options->image.custom_border_fullscreen_only || fullscreen))
+       if ((options->image.use_custom_border_color && !fullscreen) ||
+           (options->image.use_custom_border_color_in_fullscreen && fullscreen))
                {
                color = &options->image.border_color;
                }
 
+#if GTK_CHECK_VERSION(3,0,0)
+       else
+               {
+               if (!layout_valid(&lw)) return;
+
+               style_context = gtk_widget_get_style_context(lw->window);
+               gtk_style_context_get_background_color(style_context, GTK_STATE_FLAG_NORMAL, &bg_color);
+
+               theme_color.red = bg_color.red * 65535;
+               theme_color.green = bg_color.green * 65535;
+               theme_color.blue = bg_color.blue * 65535;
+
+               color = &theme_color;
+               }
+#endif
+
        image_background_set_color(imd, color);
 }
 
@@ -1523,7 +1967,7 @@ gboolean image_color_profile_get_status(ImageWindow *imd, gchar **image_profile,
 {
        ColorMan *cm;
        if (!imd) return FALSE;
-       
+
        cm = imd->cm;
        if (!cm) return FALSE;
        return color_man_get_status(cm, image_profile, screen_profile);
@@ -1553,6 +1997,7 @@ void image_set_delay_flip(ImageWindow *imd, gboolean delay)
 
 void image_to_root_window(ImageWindow *imd, gboolean scaled)
 {
+#if !GTK_CHECK_VERSION(3,0,0)
        GdkScreen *screen;
        GdkWindow *rootwindow;
        GdkPixmap *pixmap;
@@ -1567,7 +2012,7 @@ void image_to_root_window(ImageWindow *imd, gboolean scaled)
 
        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 (gdk_window_get_visual(rootwindow) != gdk_visual_get_system()) return;
 
        if (scaled)
                {
@@ -1588,12 +2033,13 @@ void image_to_root_window(ImageWindow *imd, gboolean scaled)
        g_object_unref(pixmap);
 
        gdk_flush();
+#endif
 }
 
 void image_select(ImageWindow *imd, gboolean select)
 {
        if (!imd->has_frame) return;
-       
+
        if (select)
                {
                gtk_widget_set_state(imd->widget, GTK_STATE_SELECTED);
@@ -1606,7 +2052,7 @@ void image_select(ImageWindow *imd, gboolean select)
 void image_set_selectable(ImageWindow *imd, gboolean selectable)
 {
        if (!imd->has_frame) return;
-       
+
        gtk_frame_set_shadow_type(GTK_FRAME(imd->frame), GTK_SHADOW_NONE);
        gtk_container_set_border_width(GTK_CONTAINER(imd->frame), selectable ? 4 : 0);
 }
@@ -1635,7 +2081,6 @@ static void image_options_set(ImageWindow *imd)
        g_object_set(G_OBJECT(imd->pr), "zoom_quality", options->image.zoom_quality,
                                        "zoom_2pass", options->image.zoom_2pass,
                                        "zoom_expand", options->image.zoom_to_fit_allow_expand,
-                                       "dither_quality", options->image.dither_quality,
                                        "scroll_reset", options->image.scroll_reset_method,
                                        "cache_display", options->image.tile_cache_max,
                                        "window_fit", (imd->top_window_sync && options->image.fit_window_to_image),
@@ -1643,10 +2088,17 @@ static void image_options_set(ImageWindow *imd)
                                        "window_limit_value", options->image.max_window_size,
                                        "autofit_limit", options->image.limit_autofit_size,
                                        "autofit_limit_value", options->image.max_autofit_size,
+                                       "enlargement_limit_value", options->image.max_enlargement_size,
 
                                        NULL);
 
        pixbuf_renderer_set_parent((PixbufRenderer *)imd->pr, (GtkWindow *)imd->top_window);
+
+       image_stereo_set(imd, options->stereo.mode);
+       pixbuf_renderer_stereo_fixed_set((PixbufRenderer *)imd->pr,
+                                       options->stereo.fixed_w, options->stereo.fixed_h,
+                                       options->stereo.fixed_x1, options->stereo.fixed_y1,
+                                       options->stereo.fixed_x2, options->stereo.fixed_y2);
 }
 
 void image_options_sync(void)
@@ -1690,27 +2142,74 @@ static void image_free(ImageWindow *imd)
        g_free(imd);
 }
 
-static void image_destroy_cb(GtkObject *widget, gpointer data)
+static void image_destroy_cb(GtkWidget *widget, gpointer data)
 {
        ImageWindow *imd = data;
        image_free(imd);
 }
+#if GTK_CHECK_VERSION(3,0,0)
+gboolean selectable_frame_draw_cb(GtkWidget *widget, cairo_t *cr, gpointer data)
+{
+       GtkAllocation allocation;
+       gtk_widget_get_allocation(widget, &allocation);
+       gtk_paint_flat_box(gtk_widget_get_style(widget),
+                          cr,
+                          gtk_widget_get_state(widget),
+                          gtk_frame_get_shadow_type(GTK_FRAME(widget)),
+                          widget,
+                          NULL,
+                          allocation.x + 3, allocation.y + 3,
+                          allocation.width - 6, allocation.height - 6);
 
+       if (gtk_widget_has_focus(widget))
+               {
+               gtk_paint_focus(gtk_widget_get_style(widget), cr, GTK_STATE_ACTIVE,
+                               widget, "image_window",
+                               allocation.x, allocation.y,
+                               allocation.width - 1, allocation.height - 1);
+               }
+       else
+               {
+               gtk_paint_shadow(gtk_widget_get_style(widget), cr, GTK_STATE_NORMAL, GTK_SHADOW_IN,
+                                widget, "image_window",
+                                allocation.x, allocation.y,
+                                allocation.width - 1, allocation.height - 1);
+               }
+       return FALSE;
+}
+
+#else
 gboolean selectable_frame_expose_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
 {
-       gtk_paint_flat_box(widget->style,
-                          widget->window,
-                          widget->state,
-                          (GTK_FRAME(widget))->shadow_type,
+       GtkAllocation allocation;
+       gtk_widget_get_allocation(widget, &allocation);
+       gtk_paint_flat_box(gtk_widget_get_style(widget),
+                          gtk_widget_get_window(widget),
+                          gtk_widget_get_state(widget),
+                          gtk_frame_get_shadow_type(GTK_FRAME(widget)),
                           NULL,
                           widget,
                           NULL,
-                          widget->allocation.x + 3, widget->allocation.y + 3,
-                          widget->allocation.width - 6, widget->allocation.height - 6);
-
+                          allocation.x + 3, allocation.y + 3,
+                          allocation.width - 6, allocation.height - 6);
 
+       if (gtk_widget_has_focus(widget))
+               {
+               gtk_paint_focus(gtk_widget_get_style(widget), gtk_widget_get_window(widget), GTK_STATE_ACTIVE,
+                               &event->area, widget, "image_window",
+                               allocation.x, allocation.y,
+                               allocation.width - 1, allocation.height - 1);
+               }
+       else
+               {
+               gtk_paint_shadow(gtk_widget_get_style(widget), gtk_widget_get_window(widget), GTK_STATE_NORMAL, GTK_SHADOW_IN,
+                                &event->area, widget, "image_window",
+                                allocation.x, allocation.y,
+                                allocation.width - 1, allocation.height - 1);
+               }
        return FALSE;
 }
+#endif
 
 
 void image_set_frame(ImageWindow *imd, gboolean frame)
@@ -1724,62 +2223,40 @@ void image_set_frame(ImageWindow *imd, gboolean frame)
        if (frame)
                {
                imd->frame = gtk_frame_new(NULL);
-#if GTK_CHECK_VERSION(2,12,0)
+               DEBUG_NAME(imd->frame);
                g_object_ref(imd->pr);
-#else
-               gtk_widget_ref(imd->pr);
-#endif
                if (imd->has_frame != -1) gtk_container_remove(GTK_CONTAINER(imd->widget), imd->pr);
                gtk_container_add(GTK_CONTAINER(imd->frame), imd->pr);
 
-#if GTK_CHECK_VERSION(2,12,0)
                g_object_unref(imd->pr);
+               gtk_widget_set_can_focus(imd->frame, TRUE);
+               gtk_widget_set_app_paintable(imd->frame, TRUE);
+
+#if GTK_CHECK_VERSION(3,0,0)
+               g_signal_connect(G_OBJECT(imd->frame), "draw",
+                                G_CALLBACK(selectable_frame_draw_cb), NULL);
 #else
-               gtk_widget_unref(imd->pr);
-#endif
                g_signal_connect(G_OBJECT(imd->frame), "expose_event",
-                                G_CALLBACK(selectable_frame_expose_cb), NULL);
-
-               GTK_WIDGET_SET_FLAGS(imd->frame, GTK_CAN_FOCUS);
+                                G_CALLBACK(selectable_frame_expose_cb), NULL);
+#endif
                g_signal_connect(G_OBJECT(imd->frame), "focus_in_event",
                                 G_CALLBACK(image_focus_in_cb), imd);
-               g_signal_connect(G_OBJECT(imd->frame), "focus_out_event",
-                                G_CALLBACK(image_focus_out_cb), imd);
-
-               g_signal_connect_after(G_OBJECT(imd->frame), "expose_event",
-                                      G_CALLBACK(image_focus_expose), imd);
 
-#if GTK_CHECK_VERSION(2,14,0)
                gtk_box_pack_start(GTK_BOX(imd->widget), imd->frame, TRUE, TRUE, 0);
-#else
-               gtk_box_pack_start_defaults(GTK_BOX(imd->widget), imd->frame);
-#endif
                gtk_widget_show(imd->frame);
                }
        else
                {
-#if GTK_CHECK_VERSION(2,12,0)
                g_object_ref(imd->pr);
-#else
-               gtk_widget_ref(imd->pr);
-#endif
                if (imd->frame)
                        {
                        gtk_container_remove(GTK_CONTAINER(imd->frame), imd->pr);
                        gtk_widget_destroy(imd->frame);
                        imd->frame = NULL;
                        }
-#if GTK_CHECK_VERSION(2,14,0)
                gtk_box_pack_start(GTK_BOX(imd->widget), imd->pr, TRUE, TRUE, 0);
-#else
-               gtk_box_pack_start_defaults(GTK_BOX(imd->widget), imd->pr);
-#endif
 
-#if GTK_CHECK_VERSION(2,12,0)
                g_object_unref(imd->pr);
-#else
-               gtk_widget_unref(imd->pr);
-#endif
                }
 
        gtk_widget_show(imd->pr);
@@ -1801,10 +2278,12 @@ ImageWindow *image_new(gboolean frame)
        imd->orientation = 1;
 
        imd->pr = GTK_WIDGET(pixbuf_renderer_new());
+       DEBUG_NAME(imd->pr);
 
        image_options_set(imd);
 
        imd->widget = gtk_vbox_new(0, 0);
+       DEBUG_NAME(imd->widget);
 
        image_set_frame(imd, frame);
 
@@ -1812,6 +2291,8 @@ ImageWindow *image_new(gboolean frame)
 
        g_signal_connect(G_OBJECT(imd->pr), "clicked",
                         G_CALLBACK(image_click_cb), imd);
+       g_signal_connect(G_OBJECT(imd->pr), "button_press_event",
+                        G_CALLBACK(image_press_cb), imd);
        g_signal_connect(G_OBJECT(imd->pr), "scroll_notify",
                         G_CALLBACK(image_scroll_notify_cb), imd);
 
@@ -1834,4 +2315,13 @@ ImageWindow *image_new(gboolean frame)
 
        return imd;
 }
+
+void image_get_rectangle(gint *x1, gint *y1, gint *x2, gint *y2)
+{
+       *x1 = rect_x1;
+       *y1 = rect_y1;
+       *x2 = rect_x2;
+       *y2 = rect_y2;
+}
+
 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */