Ref #202: "Set as wallpaper" doesn't work with GNOME Shell
[geeqie.git] / src / layout_image.c
index 091a75d..2e40174 100644 (file)
@@ -1,13 +1,22 @@
 /*
- * Geeqie
- * (C) 2006 John Ellis
- * Copyright (C) 2008 - 2012 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"
@@ -17,6 +26,7 @@
 #include "color-man.h"
 #include "dnd.h"
 #include "editors.h"
+#include "exif.h"
 #include "filedata.h"
 #include "fullscreen.h"
 #include "image.h"
@@ -25,6 +35,7 @@
 #include "layout.h"
 #include "layout_util.h"
 #include "menu.h"
+#include "metadata.h"
 #include "misc.h"
 #include "pixbuf_util.h"
 #include "pixbuf-renderer.h"
 
 #include <gdk/gdkkeysyms.h> /* for keyboard values */
 
+#define FILE_COLUMN_POINTER 0
 
 static GtkWidget *layout_image_pop_menu(LayoutWindow *lw);
 static void layout_image_set_buttons(LayoutWindow *lw);
+static void layout_image_animate_stop(LayoutWindow *lw);
+static gboolean layout_image_animate_new_file(LayoutWindow *lw);
+static void layout_image_animate_update_image(LayoutWindow *lw);
 
 /*
  *----------------------------------------------------------------------------
@@ -91,6 +106,7 @@ void layout_image_full_screen_start(LayoutWindow *lw)
        layout_actions_add_window(lw, lw->full_screen->window);
 
        image_osd_copy_status(lw->full_screen->normal_imd, lw->image);
+       layout_image_animate_update_image(lw);
 }
 
 void layout_image_full_screen_stop(LayoutWindow *lw)
@@ -102,6 +118,8 @@ void layout_image_full_screen_stop(LayoutWindow *lw)
                image_osd_copy_status(lw->image, lw->full_screen->normal_imd);
 
        fullscreen_stop(lw->full_screen);
+
+       layout_image_animate_update_image(lw);
 }
 
 void layout_image_full_screen_toggle(LayoutWindow *lw)
@@ -254,6 +272,157 @@ static gboolean layout_image_slideshow_continue_check(LayoutWindow *lw)
        return TRUE;
 }
 
+/*
+ *----------------------------------------------------------------------------
+ * Animation
+ *----------------------------------------------------------------------------
+ */
+
+static void image_animation_data_free(AnimationData *fd)
+{
+       if(!fd) return;
+       if(fd->iter) g_object_unref(fd->iter);
+       if(fd->gpa) g_object_unref(fd->gpa);
+       g_free(fd);
+}
+
+static gboolean animation_should_continue(AnimationData *fd)
+{
+       if (!fd->valid)
+               return FALSE;
+
+       return TRUE;
+}
+
+static gboolean show_next_frame(gpointer data)
+{
+       AnimationData *fd = (AnimationData*)data;
+       int delay;
+       PixbufRenderer *pr;
+
+       if(animation_should_continue(fd)==FALSE)
+               {
+               image_animation_data_free(fd);
+               return FALSE;
+               }
+
+       pr = (PixbufRenderer*)fd->iw->pr;
+
+       if (gdk_pixbuf_animation_iter_advance(fd->iter,NULL)==FALSE)
+               {
+               /* This indicates the animation is complete.
+                  Return FALSE here to disable looping. */
+               }
+
+       fd->gpb = gdk_pixbuf_animation_iter_get_pixbuf(fd->iter);
+       image_change_pixbuf(fd->iw,fd->gpb,pr->zoom,FALSE);
+
+       if (fd->iw->func_update)
+               fd->iw->func_update(fd->iw, fd->iw->data_update);
+
+       delay = gdk_pixbuf_animation_iter_get_delay_time(fd->iter);
+       if (delay!=fd->delay)
+               {
+               if (delay>0) /* Current frame not static. */
+                       {
+                       fd->delay=delay;
+                       g_timeout_add(delay,show_next_frame,fd);
+                       }
+               else
+                       {
+                       image_animation_data_free(fd);
+                       }
+               return FALSE;
+               }
+
+       return TRUE;
+}
+
+static gboolean layout_image_animate_check(LayoutWindow *lw)
+{
+       if (!layout_valid(&lw)) return FALSE;
+
+       if(!lw->options.animate || lw->image->image_fd == NULL)
+               {
+               if(lw->animation)
+                       {
+                       lw->animation->valid = FALSE;
+                       lw->animation = NULL;
+                       }
+               return FALSE;
+               }
+
+       return TRUE;
+}
+
+static void layout_image_animate_stop(LayoutWindow *lw)
+{
+       if (!layout_valid(&lw)) return;
+
+       if(lw->options.animate && lw->animation)
+               {
+               lw->animation->valid = FALSE;
+               lw->animation = NULL;
+               }
+}
+
+static void layout_image_animate_update_image(LayoutWindow *lw)
+{
+       if (!layout_valid(&lw)) return;
+
+       if(lw->options.animate && lw->animation)
+               {
+               if (lw->full_screen && lw->image != lw->full_screen->imd)
+                       lw->animation->iw = lw->full_screen->imd;
+               else
+                       lw->animation->iw = lw->image;
+               }
+}
+
+static gboolean layout_image_animate_new_file(LayoutWindow *lw)
+{
+       GError *err=NULL;
+
+       if(!layout_image_animate_check(lw)) return FALSE;
+
+       if(lw->animation) lw->animation->valid = FALSE;
+
+       lw->animation = g_malloc0(sizeof(AnimationData));
+
+       if(!(lw->animation->gpa = gdk_pixbuf_animation_new_from_file(lw->image->image_fd->path,&err)) || err ||
+               gdk_pixbuf_animation_is_static_image(lw->animation->gpa) ||
+               !(lw->animation->iter = gdk_pixbuf_animation_get_iter(lw->animation->gpa,NULL)))
+               {
+               image_animation_data_free(lw->animation);
+               lw->animation = NULL;
+               return FALSE;
+               }
+
+       lw->animation->data_adr = lw->image->image_fd;
+       lw->animation->delay = gdk_pixbuf_animation_iter_get_delay_time(lw->animation->iter);
+       lw->animation->valid = TRUE;
+
+       layout_image_animate_update_image(lw);
+
+       g_timeout_add(lw->animation->delay, show_next_frame, lw->animation);
+
+       return TRUE;
+}
+
+void layout_image_animate_toggle(LayoutWindow *lw)
+{
+       GtkAction *action;
+
+       if (!lw) return;
+
+       lw->options.animate = !lw->options.animate;
+
+       action = gtk_action_group_get_action(lw->action_group, "Animate");
+       gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->options.animate);
+
+       layout_image_animate_new_file(lw);
+}
+
 /*
  *----------------------------------------------------------------------------
  * pop-up menus
@@ -316,7 +485,7 @@ static void li_pop_menu_alter_cb(GtkWidget *widget, gpointer data)
        lw = submenu_item_get_data(widget);
        type = (AlterType)GPOINTER_TO_INT(data);
 
-       image_alter_orientation(lw->image, type);
+       image_alter_orientation(lw->image, lw->image->image_fd, type);
 }
 
 static void li_pop_menu_new_cb(GtkWidget *widget, gpointer data)
@@ -411,6 +580,13 @@ static void li_pop_menu_full_screen_cb(GtkWidget *widget, gpointer data)
        layout_image_full_screen_toggle(lw);
 }
 
+static void li_pop_menu_animate_cb(GtkWidget *widget, gpointer data)
+{
+       LayoutWindow *lw = data;
+
+       layout_image_animate_toggle(lw);
+}
+
 static void li_pop_menu_hide_cb(GtkWidget *widget, gpointer data)
 {
        LayoutWindow *lw = data;
@@ -462,7 +638,7 @@ static GList *layout_image_get_fd_list(LayoutWindow *lw)
                else
                        list = g_list_append(NULL, file_data_ref(fd));
                }
-       
+
        return list;
 }
 
@@ -492,8 +668,9 @@ static GtkWidget *layout_image_pop_menu(LayoutWindow *lw)
        submenu = submenu_add_edit(menu, &item, G_CALLBACK(li_pop_menu_edit_cb), lw, editmenu_fd_list);
        if (!path) gtk_widget_set_sensitive(item, FALSE);
        menu_item_add_divider(submenu);
+#if !GTK_CHECK_VERSION(3,0,0)
        menu_item_add(submenu, _("Set as _wallpaper"), G_CALLBACK(li_pop_menu_wallpaper_cb), lw);
-
+#endif
        item = submenu_add_alter(menu, G_CALLBACK(li_pop_menu_alter_cb), lw);
 
        item = menu_item_add_stock(menu, _("View in _new window"), GTK_STOCK_NEW, G_CALLBACK(li_pop_menu_new_cb), lw);
@@ -512,7 +689,7 @@ static GtkWidget *layout_image_pop_menu(LayoutWindow *lw)
        if (!path) gtk_widget_set_sensitive(item, FALSE);
        item = menu_item_add_stock(menu, _("_Delete..."), GTK_STOCK_DELETE, G_CALLBACK(li_pop_menu_delete_cb), lw);
        if (!path) gtk_widget_set_sensitive(item, FALSE);
-       
+
        item = menu_item_add(menu, _("_Copy path"), G_CALLBACK(li_pop_menu_copy_path_cb), lw);
        if (!path) gtk_widget_set_sensitive(item, FALSE);
 
@@ -548,6 +725,8 @@ static GtkWidget *layout_image_pop_menu(LayoutWindow *lw)
                menu_item_add(menu, _("Exit _full screen"), G_CALLBACK(li_pop_menu_full_screen_cb), lw);
                }
 
+       menu_item_add_check(menu, _("_Animate"), lw->options.animate, G_CALLBACK(li_pop_menu_animate_cb), lw);
+
        menu_item_add_divider(menu);
 
        item = menu_item_add_check(menu, _("Hide file _list"), lw->options.tools_hidden,
@@ -774,7 +953,7 @@ void layout_image_scroll(LayoutWindow *lw, gint x, gint y, gboolean connect_scro
 
        if (lw->full_screen && lw->image != lw->full_screen->imd)
                {
-               image_scroll(lw->full_screen->imd, x, y); 
+               image_scroll(lw->full_screen->imd, x, y);
                }
 
        if (!connect_scroll) return;
@@ -782,7 +961,7 @@ void layout_image_scroll(LayoutWindow *lw, gint x, gint y, gboolean connect_scro
        image_get_image_size(lw->image, &width, &height);
        dx = (gdouble) x / width;
        dy = (gdouble) y / height;
-       
+
        for (i = 0; i < MAX_SPLIT_IMAGES; i++)
                {
                if (lw->split_images[i] && lw->split_images[i] != lw->image)
@@ -806,7 +985,7 @@ void layout_image_zoom_adjust(LayoutWindow *lw, gdouble increment, gboolean conn
 
        if (lw->full_screen && lw->image != lw->full_screen->imd)
                {
-               image_zoom_adjust(lw->full_screen->imd, increment); 
+               image_zoom_adjust(lw->full_screen->imd, increment);
                }
 
        if (!connect_zoom) return;
@@ -827,14 +1006,14 @@ void layout_image_zoom_adjust_at_point(LayoutWindow *lw, gdouble increment, gint
 
        if (lw->full_screen && lw->image != lw->full_screen->imd)
                {
-               image_zoom_adjust_at_point(lw->full_screen->imd, increment, x, y); 
+               image_zoom_adjust_at_point(lw->full_screen->imd, increment, x, y);
                }
-
-       if (!connect_zoom) return;
+       if (!connect_zoom && !lw->split_mode) return;
 
        for (i = 0; i < MAX_SPLIT_IMAGES; i++)
                {
-               if (lw->split_images[i] && lw->split_images[i] != lw->image)
+               if (lw->split_images[i] && lw->split_images[i] != lw->image &&
+                                               lw->split_images[i]->mouse_wheel_mode)
                        image_zoom_adjust_at_point(lw->split_images[i], increment, x, y);
                }
 }
@@ -848,7 +1027,7 @@ void layout_image_zoom_set(LayoutWindow *lw, gdouble zoom, gboolean connect_zoom
 
        if (lw->full_screen && lw->image != lw->full_screen->imd)
                {
-               image_zoom_set(lw->full_screen->imd, zoom); 
+               image_zoom_set(lw->full_screen->imd, zoom);
                }
 
        if (!connect_zoom) return;
@@ -869,7 +1048,7 @@ void layout_image_zoom_set_fill_geometry(LayoutWindow *lw, gboolean vertical, gb
 
        if (lw->full_screen && lw->image != lw->full_screen->imd)
                {
-               image_zoom_set_fill_geometry(lw->full_screen->imd, vertical); 
+               image_zoom_set_fill_geometry(lw->full_screen->imd, vertical);
                }
 
        if (!connect_zoom) return;
@@ -885,7 +1064,71 @@ void layout_image_alter_orientation(LayoutWindow *lw, AlterType type)
 {
        if (!layout_valid(&lw)) return;
 
-       image_alter_orientation(lw->image, type);
+       GtkTreeModel *store;
+       GList *work;
+       GtkTreeSelection *selection;
+       GtkTreePath *tpath;
+       FileData *fd_n;
+       GtkTreeIter iter;
+       IconData *id;
+
+       if (!lw || !lw->vf) return;
+
+       if (lw->vf->type == FILEVIEW_ICON)
+               {
+               if (!VFICON(lw->vf)->selection) return;
+               work = VFICON(lw->vf)->selection;
+               }
+       else
+               {
+               selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(lw->vf->listview));
+               work = gtk_tree_selection_get_selected_rows(selection, &store);
+               }
+
+       while (work)
+               {
+               if (lw->vf->type == FILEVIEW_ICON)
+                       {
+                       id = work->data;
+                       fd_n = id->fd;
+                       work = work->next;
+                       }
+               else
+                       {
+                       tpath = work->data;
+                       gtk_tree_model_get_iter(store, &iter, tpath);
+                       gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd_n, -1);
+                       work = work->next;
+                       }
+
+               image_alter_orientation(lw->image, fd_n, type);
+               }
+}
+
+void layout_image_reset_orientation(LayoutWindow *lw)
+{
+       ImageWindow *imd= lw->image;
+
+       if (!layout_valid(&lw)) return;
+       if (!imd || !imd->pr || !imd->image_fd) return;
+
+       if (imd->orientation < 1 || imd->orientation > 8) imd->orientation = 1;
+
+       if (options->image.exif_rotate_enable)
+               {
+               imd->orientation = metadata_read_int(imd->image_fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
+               }
+       else
+               {
+               imd->orientation = 1;
+               }
+
+       if (imd->image_fd->user_orientation != 0)
+               {
+                imd->orientation = imd->image_fd->user_orientation;
+               }
+
+       pixbuf_renderer_set_orientation((PixbufRenderer *)imd->pr, imd->orientation);
 }
 
 void layout_image_set_desaturate(LayoutWindow *lw, gboolean desaturate)
@@ -993,6 +1236,7 @@ void layout_image_set_fd(LayoutWindow *lw, FileData *fd)
        layout_list_sync_fd(lw, fd);
        layout_image_slideshow_continue_check(lw);
        layout_bars_new_image(lw);
+       layout_image_animate_new_file(lw);
 }
 
 void layout_image_set_with_ahead(LayoutWindow *lw, FileData *fd, FileData *read_ahead_fd)
@@ -1396,11 +1640,11 @@ static void layout_image_button_cb(ImageWindow *imd, GdkEventButton *event, gpoi
        switch (event->button)
                {
                case MOUSE_BUTTON_LEFT:
-                       if (lw->split_mode == SPLIT_NONE)
+                       if (options->image_lm_click_nav && lw->split_mode == SPLIT_NONE)
                                layout_image_next(lw);
                        break;
                case MOUSE_BUTTON_MIDDLE:
-                       if (lw->split_mode == SPLIT_NONE)
+                       if (options->image_lm_click_nav && lw->split_mode == SPLIT_NONE)
                                layout_image_prev(lw);
                        break;
                case MOUSE_BUTTON_RIGHT:
@@ -1429,7 +1673,8 @@ static void layout_image_scroll_cb(ImageWindow *imd, GdkEventScroll *event, gpoi
                }
 
 
-       if (event->state & GDK_CONTROL_MASK)
+       if ((event->state & GDK_CONTROL_MASK) ||
+                               (imd->mouse_wheel_mode && !options->image_lm_click_nav))
                {
                switch (event->direction)
                        {
@@ -1479,13 +1724,13 @@ static void layout_image_scroll_cb(ImageWindow *imd, GdkEventScroll *event, gpoi
                }
 }
 
-static void layout_image_drag_cb(ImageWindow *imd, GdkEventButton *event, gdouble dx, gdouble dy, gpointer data)
+static void layout_image_drag_cb(ImageWindow *imd, GdkEventMotion *event, gdouble dx, gdouble dy, gpointer data)
 {
        gint i;
        LayoutWindow *lw = data;
        gdouble sx, sy;
 
-       if (lw->full_screen && lw->image != lw->full_screen->imd && 
+       if (lw->full_screen && lw->image != lw->full_screen->imd &&
            imd != lw->full_screen->imd)
                {
                if (event->state & GDK_CONTROL_MASK)
@@ -1550,7 +1795,7 @@ static void layout_image_button_inactive_cb(ImageWindow *imd, GdkEventButton *ev
 
 }
 
-static void layout_image_drag_inactive_cb(ImageWindow *imd, GdkEventButton *event, gdouble dx, gdouble dy, gpointer data)
+static void layout_image_drag_inactive_cb(ImageWindow *imd, GdkEventMotion *event, gdouble dx, gdouble dy, gpointer data)
 {
        LayoutWindow *lw = data;
        gint i = image_idx(lw, imd);
@@ -1605,19 +1850,19 @@ void layout_status_update_pixel_cb(PixbufRenderer *pr, gpointer data)
        if (width < 1 || height < 1) return;
 
        pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
-       
+
        if(x_pixel >= 0 && y_pixel >= 0)
                {
                gint r_mouse, g_mouse, b_mouse;
-                       
+
                pixbuf_renderer_get_pixel_colors(pr, x_pixel, y_pixel,
-                                                &r_mouse, &g_mouse, &b_mouse);                 
-               
+                                                &r_mouse, &g_mouse, &b_mouse);
+
                text = g_strdup_printf(_("[%*d,%*d]: RGB(%3d,%3d,%3d)"),
                                         num_length(width - 1), x_pixel,
                                         num_length(height - 1), y_pixel,
                                         r_mouse, g_mouse, b_mouse);
-               
+
                }
        else
                {
@@ -1745,7 +1990,7 @@ static void layout_image_setup_split_common(LayoutWindow *lw, gint n)
                        layout_image_new(lw, i);
                        image_set_frame(lw->split_images[i], frame);
                        image_set_selectable(lw->split_images[i], (n > 1));
-                       
+
                        if (lw->image)
                                {
                                image_osd_copy_status(lw->image, lw->split_images[i]);
@@ -1755,17 +2000,20 @@ static void layout_image_setup_split_common(LayoutWindow *lw, gint n)
                                {
                                GList *work = g_list_last(layout_selection_list(lw));
                                gint j = 0;
-                               
-                               if (work) work = work->prev;
 
                                while (work && j < i)
                                        {
                                        FileData *fd = work->data;
                                        work = work->prev;
-                                       
-                                       j++;
-                                       if (!fd || !*fd->path) continue;
+
+                                       if (!fd || !*fd->path || fd->parent ||
+                                                                               fd == lw->split_images[0]->image_fd)
+                                               {
+                                               continue;
+                                               }
                                        img_fd = fd;
+
+                                       j++;
                                        }
                                }
 
@@ -1798,7 +2046,7 @@ static void layout_image_setup_split_common(LayoutWindow *lw, gint n)
                        lw->split_images[i] = NULL;
                        }
                }
-       
+
        if (!lw->image || lw->active_split_image < 0 || lw->active_split_image >= n)
                {
                layout_image_activate(lw, 0, TRUE);
@@ -1814,7 +2062,7 @@ static void layout_image_setup_split_common(LayoutWindow *lw, gint n)
 GtkWidget *layout_image_setup_split_none(LayoutWindow *lw)
 {
        lw->split_mode = SPLIT_NONE;
-       
+
        layout_image_setup_split_common(lw, 1);
 
        lw->split_image_widget = lw->split_images[0]->widget;
@@ -1826,7 +2074,7 @@ GtkWidget *layout_image_setup_split_none(LayoutWindow *lw)
 GtkWidget *layout_image_setup_split_hv(LayoutWindow *lw, gboolean horizontal)
 {
        GtkWidget *paned;
-       
+
        lw->split_mode = horizontal ? SPLIT_HOR : SPLIT_VERT;
 
        layout_image_setup_split_common(lw, 2);
@@ -1938,8 +2186,8 @@ static void layout_image_maint_removed(LayoutWindow *lw, FileData *fd)
                                }
                        layout_image_set_fd(lw, NULL);
                        }
-                       
-               /* the image will be set to the next image from the list soon,  
+
+               /* the image will be set to the next image from the list soon,
                   setting it to NULL here is not necessary*/
                }
 }
@@ -1952,7 +2200,7 @@ void layout_image_notify_cb(FileData *fd, NotifyType type, gpointer data)
        if (!(type & NOTIFY_CHANGE) || !fd->change) return;
 
        DEBUG_1("Notify layout_image: %s %04x", fd->path, type);
-       
+
        switch (fd->change->type)
                {
                case FILEDATA_CHANGE_MOVE: