Remove commented out code.
[geeqie.git] / src / dnd.c
index 0779fe4..5394d82 100644 (file)
--- a/src/dnd.c
+++ b/src/dnd.c
 /*
- * GQview image viewer
- * (C)2000 John Ellis
+ * Geeqie
+ * (C) 2004 John Ellis
+ * Copyright (C) 2008 - 2012 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!
  */
 
-#include "gqview.h"
+
+#include "main.h"
+#include "dnd.h"
+
+#include "collect.h"
 #include "image.h"
+#include "ui_fileops.h"
+#include "pixbuf_util.h"
 
-enum {
-       TARGET_URI_LIST,
-       TARGET_TEXT_PLAIN
-};
 
-static GtkTargetEntry file_drag_types[] = {
+GtkTargetEntry dnd_file_drag_types[] = {
        { "text/uri-list", 0, TARGET_URI_LIST },
        { "text/plain", 0, TARGET_TEXT_PLAIN }
 };
-static gint n_file_drag_types = 2;
+gint dnd_file_drag_types_count = 2;
 
-static GtkTargetEntry file_drop_types[] = {
-       { "text/uri-list", 0, TARGET_URI_LIST }
+GtkTargetEntry dnd_file_drop_types[] = {
+       { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
+       { "text/uri-list", 0, TARGET_URI_LIST },
+       { "text/plain", 0, TARGET_TEXT_PLAIN },
 };
-static gint n_file_drop_types = 1;
-
-static GList *get_uri_file_list(gchar *data);
-
-static void image_get_dnd_data(GtkWidget *widget, GdkDragContext *context,
-                              gint x, gint y,
-                              GtkSelectionData *selection_data, guint info,
-                              guint time, gpointer data);
-static void image_set_dnd_data(GtkWidget *widget, GdkDragContext *context,
-                             GtkSelectionData *selection_data, guint info,
-                             guint time, gpointer data);
-static void image_drag_end(GtkWidget *widget, GdkDragContext *context, gpointer data);
-
-static void file_list_set_dnd_data(GtkWidget *widget, GdkDragContext *context,
-                             GtkSelectionData *selection_data, guint info,
-                             guint time, gpointer data);
-static void file_list_drag_begin(GtkWidget *widget, GdkDragContext *context, gpointer data);
-static void file_list_drag_end(GtkWidget *widget, GdkDragContext *context, gpointer data);
-
-static void dir_list_set_dnd_data(GtkWidget *widget, GdkDragContext *context,
-                             GtkSelectionData *selection_data, guint info,
-                             guint time, gpointer data);
-static void dir_clist_set_highlight(gint set);
-static void dir_list_drag_begin(GtkWidget *widget, GdkDragContext *context, gpointer data);
-static void dir_list_drag_end(GtkWidget *widget, GdkDragContext *context, gpointer data);
-
-static GList *get_uri_file_list(gchar *data)
-{
-       GList *list = NULL;
-       gint b, e;
+gint dnd_file_drop_types_count = 3;
 
-       b = e = 0;
 
-       while (data[b] != '\0')
-               {
-               while (data[e] != '\r' && data[e] != '\n' && data[e] != '\0') e++;
-               if (!strncmp(data + b, "file:", 5))
-                       {
-                       b += 5;
-                       list = g_list_append(list, g_strndup(data + b, e - b));
-                       }
-               while (data[e] == '\r' || data[e] == '\n') e++;
-               b = e;
-               }
+#define DND_ICON_SIZE (options->dnd_icon_size)
 
-       return list;
-}
 
-/*
- *-----------------------------------------------------------------------------
- * image drag and drop routines
- *-----------------------------------------------------------------------------
- */ 
-
-static void image_get_dnd_data(GtkWidget *widget, GdkDragContext *context,
-                              gint x, gint y,
-                              GtkSelectionData *selection_data, guint info,
-                              guint time, gpointer data)
+static void pixbuf_draw_border(GdkPixbuf *pixbuf, gint w, gint h)
 {
-       ImageWindow *imd = data;
-
-       if (info == TARGET_URI_LIST)
+       gboolean alpha;
+       gint rs;
+       guchar *pix;
+       guchar *p;
+       gint i;
+
+       alpha = gdk_pixbuf_get_has_alpha(pixbuf);
+       rs = gdk_pixbuf_get_rowstride(pixbuf);
+       pix = gdk_pixbuf_get_pixels(pixbuf);
+
+       p = pix;
+       for (i = 0; i < w; i++)
                {
-               GList *list = get_uri_file_list(selection_data->data);
-               if (list)
-                       {
-                       gchar *path;
-
-                       path = list->data;
-
-                       if (imd == normal_image)
-                               {
-                               if (isfile(path))
-                                       {
-                                       gint row;
-                                       gchar *dir = remove_level_from_path(path);
-                                       if (strcmp(dir, current_path) != 0)
-                                               filelist_change_to(dir);
-                                       g_free(dir);
-       
-                                       row = find_file_in_list(path);
-                                       if (row == -1)
-                                               image_change_to(path);
-                                       else
-                                               file_image_change_to(row);
-                                       }
-                               else if (isdir(path))
-                                       {
-                                       filelist_change_to(path);
-                                       image_change_to(NULL);
-                                       }
-                               }
-                       else
-                               {
-                               if (isfile(path))
-                                       {
-                                       image_area_set_image(imd, path, get_default_zoom(imd));
-                                       }
-                               }
-
-                       if (debug)
-                               {
-                               GList *work = list;
-                               while (work)
-                                       {
-                                       printf("dropped: %s\n", (gchar *)(work->data));
-                                       work = work->next;
-                                       }
-                               }
-
-                       g_list_foreach(list, (GFunc)g_free, NULL);
-                       g_list_free(list);
-                       }
+               *p = 0; p++; *p = 0; p++; *p = 0; p++;
+               if (alpha) { *p= 255; p++; }
                }
-}
-
-static void image_set_dnd_data(GtkWidget *widget, GdkDragContext *context,
-                              GtkSelectionData *selection_data, guint info,
-                              guint time, gpointer data)
-{
-       ImageWindow *imd = data;
-       gchar *path = image_area_get_path(imd);
-
-       if (path)
+       for (i = 1; i < h - 1; i++)
                {
-               gchar *text = NULL;
-               switch (info)
-                       {
-                       case TARGET_URI_LIST:
-                               text = g_strconcat("file:", path, "\r\n", NULL);
-                               break;
-                       case TARGET_TEXT_PLAIN:
-                               text = g_strdup(path);
-                               break;
-                       }
-               if (text)
-                       {
-                       gtk_selection_data_set (selection_data, selection_data->target,
-                                               8, text, strlen(text));
-                       g_free(text);
-                       }
+               p = pix + rs * i;
+               *p = 0; p++; *p = 0; p++; *p = 0; p++;
+               if (alpha) *p= 255;
+
+               p = pix + rs * i + (w - 1) * ((alpha == TRUE) ? 4 : 3);
+               *p = 0; p++; *p = 0; p++; *p = 0; p++;
+               if (alpha) *p= 255;
                }
-       else
+       p = pix + rs * (h - 1);
+       for (i = 0; i < w; i++)
                {
-               gtk_selection_data_set (selection_data, selection_data->target,
-                                       8, NULL, 0);
+               *p = 0; p++; *p = 0; p++; *p = 0; p++;
+               if (alpha) { *p= 255; p++; }
                }
 }
 
-static void image_drag_end(GtkWidget *widget, GdkDragContext *context, gpointer data)
+/*
+static void pixbuf_draw_rect(GdkPixbuf *pixbuf, gint x, gint y, gint w, gint h, guint8 val)
 {
-       ImageWindow *imd = data;
-       if (context->action == GDK_ACTION_MOVE)
+       gboolean alpha;
+       gint rs;
+       guchar *pix;
+       guchar *p;
+       gint i, j;
+
+       alpha = gdk_pixbuf_get_has_alpha(pixbuf);
+       rs = gdk_pixbuf_get_rowstride(pixbuf);
+       pix = gdk_pixbuf_get_pixels(pixbuf);
+
+       for (j = 0; j < h; j++)
                {
-               gint row = find_file_in_list(image_area_get_path(imd));
-               if (row < 0) return;
-               if (image_get_path() && strcmp(image_get_path(), image_area_get_path(imd)) == 0)
+               p = pix + (rs * (y + j)) + (x * ((alpha) ? 4 : 3));
+               for (i = 0; i < w; i++)
                        {
-                       if (row < file_count() - 1)
-                               {
-                               file_next_image();
-                               }
-                       else
-                               {
-                               file_prev_image();
-                               }
+                       *p = (*p * (256-val)) >> 8; p++;
+                       *p = (*p * (256-val)) >> 8; p++;
+                       *p = (*p * (256-val)) >> 8; p++;
+                       if (alpha) { *p = 255; p++; }
                        }
-               filelist_refresh();
                }
 }
-
-void image_dnd_init(ImageWindow *imd)
+*/
+void dnd_set_drag_icon(GtkWidget *widget, GdkDragContext *context, GdkPixbuf *pixbuf, gint items)
 {
-       gtk_drag_source_set(imd->viewport, GDK_BUTTON2_MASK,
-                               file_drag_types, n_file_drag_types, GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
-       gtk_signal_connect(GTK_OBJECT(imd->viewport), "drag_data_get",
-                               image_set_dnd_data, imd);
-       gtk_signal_connect(GTK_OBJECT(imd->viewport), "drag_end",
-                               image_drag_end, imd);
-
-       gtk_drag_dest_set(imd->viewport,
-                         GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP,
-                         file_drop_types, n_file_drop_types,
-                          GDK_ACTION_COPY);
-       gtk_signal_connect(GTK_OBJECT(imd->viewport), "drag_data_received",
-                               image_get_dnd_data, imd);
-}
+       GdkPixbuf *dest;
+       gint w, h;
+       gint sw, sh;
+       PangoLayout *layout = NULL;
+       gint x, y;
 
-/*
- *-----------------------------------------------------------------------------
- * file list drag and drop routines (private)
- *-----------------------------------------------------------------------------
- */ 
-
-static void file_list_set_dnd_data(GtkWidget *widget, GdkDragContext *context,
-                             GtkSelectionData *selection_data, guint info,
-                             guint time, gpointer data)
-{
-       gchar *uri_text = NULL;
-       gchar *leader = "file:";
-       gchar *sep = "\r\n";
-       gint total;
-       gint leader_l;
-       gint sep_l = strlen(sep);
-       gchar *ptr;
-
-       switch (info)
+       x = y = 0;
+
+       sw = gdk_pixbuf_get_width(pixbuf);
+       sh = gdk_pixbuf_get_height(pixbuf);
+
+       if (sw <= DND_ICON_SIZE && sh <= DND_ICON_SIZE)
                {
-               case TARGET_URI_LIST:
-                       leader_l = strlen(leader);
-                       break;
-               case TARGET_TEXT_PLAIN:
-                       leader_l = 0;
-                       break;
-               default:
-                       return;
-                       break;
+               w = sw;
+               h = sh;
                }
-       
-       if (file_clicked_is_selected())
+       else if (sw < sh)
                {
-               GList *list;
-               GList *work;
-
-               list = file_get_selected_list();
-               if (!list) return;
-               work = list;
-               total = 0;
-
-               /* compute length */
-
-               while (work)
-                       {
-                       gchar *name = work->data;
-                       total += leader_l + strlen(name) + sep_l;
-                       work = work->next;
-                       }
-
-               /* create list */
-               uri_text = g_malloc(total + 1);
-               ptr = uri_text;
-
-               work = list;
-               while (work)
-                       {
-                       gchar *name = work->data;
-                       if (leader_l > 0)
-                               {
-                               strcpy(ptr, leader);
-                               ptr += leader_l;
-                               }
-                       strcpy(ptr, name);
-                       ptr += strlen(name);
-                       strcpy(ptr, sep);
-                       ptr += sep_l;
-                       work = work->next;
-                       }
-               ptr[0] = '\0';
-               free_selected_list(list);
+               w = sw * DND_ICON_SIZE / sh;
+               h = DND_ICON_SIZE;
                }
        else
                {
-               gchar *path = file_clicked_get_path();
-               if (!path) return;
-               switch (info)
-                       {
-                       case TARGET_URI_LIST:
-                               uri_text = g_strconcat("file:", path, "\r\n", NULL);
-                               break;
-                       case TARGET_TEXT_PLAIN:
-                               uri_text = g_strdup(path);
-                               break;
-                       }
-               total = strlen(uri_text);
-               g_free(path);
+               w = DND_ICON_SIZE;
+               h = sh * DND_ICON_SIZE / sw;
                }
 
-       if (debug) printf(uri_text);
+       dest = gdk_pixbuf_scale_simple(pixbuf, w, h, GDK_INTERP_BILINEAR);
+       pixbuf_draw_border(dest, w, h);
 
-       gtk_selection_data_set (selection_data, selection_data->target,
-                                       8, uri_text, total);
-       g_free(uri_text);
+       if (items > 1)
+               {
+               gchar *buf;
+               gint lw,lh;
 
-       file_clist_highlight_unset();
-}
+               layout = gtk_widget_create_pango_layout(widget, NULL);
+               buf = g_strdup_printf("<small> %d </small>", items);
+               pango_layout_set_markup(layout, buf, -1);
+               g_free(buf);
 
-static void file_list_drag_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
-{
-       file_clist_highlight_set();
-}
+               pango_layout_get_pixel_size(layout, &lw, &lh);
 
-static void file_list_drag_end(GtkWidget *widget, GdkDragContext *context, gpointer data)
-{
-       file_clist_highlight_unset();
+               x = MAX(0, w - lw);
+               y = MAX(0, h - lh);
+               lw = CLAMP(lw, 0, w - x - 1);
+               lh = CLAMP(lh, 0, h - y - 1);
 
-       if (context->action == GDK_ACTION_MOVE)
-               {
-               filelist_refresh();
+               pixbuf_draw_rect_fill(dest, x, y, lw, lh, 128, 128, 128, 255);
                }
-}
 
-/*
- *-----------------------------------------------------------------------------
- * directory list drag and drop routines (private)
- *-----------------------------------------------------------------------------
- */ 
-
-static void dir_list_set_dnd_data(GtkWidget *widget, GdkDragContext *context,
-                             GtkSelectionData *selection_data, guint info,
-                             guint time, gpointer data)
-{
-       gint row = GPOINTER_TO_INT(gtk_object_get_user_data(GTK_OBJECT(dir_clist)));
-
-       if (row >= 0)
+       if (layout)
                {
-               gchar *name = gtk_clist_get_row_data(GTK_CLIST(dir_clist), row);
-               gchar *new_path;
-               gchar *text = NULL;
-
-               if (strcmp(name, ".") == 0)
-                       new_path = g_strdup(current_path);
-               else if (strcmp(name, "..") == 0)
-                       new_path = remove_level_from_path(current_path);
-               else
-                       {
-                       if (strcmp(current_path, "/") == 0)
-                               new_path = g_strconcat(current_path, name, NULL);
-                       else
-                               new_path = g_strconcat(current_path, "/", name, NULL);
-                       }
+               pixbuf_draw_layout(dest, layout, NULL, x+1, y+1, 0, 0, 0, 255);
+               pixbuf_draw_layout(dest, layout, NULL, x, y, 255, 255, 255, 255);
 
-               
-               switch (info)
-                       {
-                       case TARGET_URI_LIST:
-                               text = g_strconcat("file:", new_path, "\r\n", NULL);
-                               break;
-                       case TARGET_TEXT_PLAIN:
-                               text = g_strdup(new_path);
-                               break;
-                       }
-               if (text)
-                       {
-                       gtk_selection_data_set (selection_data, selection_data->target,
-                                       8, text, strlen(text));
-                       g_free(text);
-                       }
-               g_free(new_path);
+               g_object_unref(G_OBJECT(layout));
                }
-}
 
-static void dir_clist_set_highlight(gint set)
-{
-       gint row = GPOINTER_TO_INT(gtk_object_get_user_data(GTK_OBJECT(dir_clist)));
-       if (set)
-               {
-               gtk_clist_set_background(GTK_CLIST(dir_clist), row,
-                       &GTK_WIDGET (dir_clist)->style->bg[GTK_STATE_SELECTED]);
-               gtk_clist_set_foreground(GTK_CLIST(dir_clist), row,
-                       &GTK_WIDGET (dir_clist)->style->fg[GTK_STATE_SELECTED]);
-               }
-       else
-               {
-               gtk_clist_set_background(GTK_CLIST(dir_clist), row, NULL);
-               gtk_clist_set_foreground(GTK_CLIST(dir_clist), row, NULL);
-               }
-}
+       gtk_drag_set_icon_pixbuf(context, dest, -8, -6);
 
-static void dir_list_drag_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
-{
-       dir_clist_set_highlight(TRUE);
+       g_object_unref(dest);
 }
 
-static void dir_list_drag_end(GtkWidget *widget, GdkDragContext *context, gpointer data)
+static void dnd_set_drag_label_end_cb(GtkWidget *widget, GdkDragContext *context, gpointer data)
 {
-       dir_clist_set_highlight(FALSE);
-
-       if (context->action == GDK_ACTION_MOVE)
-               {
-               filelist_refresh();
-               }
+       GtkWidget *window = data;
+       g_signal_handlers_disconnect_by_func(widget, dnd_set_drag_label_end_cb, data);
+       gtk_widget_destroy(window);
 }
 
-/*
- *-----------------------------------------------------------------------------
- * drag and drop initialization (public)
- *-----------------------------------------------------------------------------
- */ 
-
-void init_dnd()
+void dnd_set_drag_label(GtkWidget *widget, GdkDragContext *context, const gchar *text)
 {
-       /* dir clist */
-       gtk_drag_source_set(dir_clist, GDK_BUTTON2_MASK,
-                               file_drag_types, n_file_drag_types, GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
-       gtk_signal_connect(GTK_OBJECT(dir_clist), "drag_begin",
-                               dir_list_drag_begin, NULL);
-       gtk_signal_connect(GTK_OBJECT(dir_clist), "drag_data_get",
-                               dir_list_set_dnd_data, NULL);
-       gtk_signal_connect(GTK_OBJECT(dir_clist), "drag_end",
-                               dir_list_drag_end, NULL);
-
-       /* file clist */
-       gtk_drag_source_set(file_clist, GDK_BUTTON2_MASK,
-                               file_drag_types, n_file_drag_types, GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
-       gtk_signal_connect(GTK_OBJECT(file_clist), "drag_begin",
-                               file_list_drag_begin, NULL);
-       gtk_signal_connect(GTK_OBJECT(file_clist), "drag_data_get",
-                               file_list_set_dnd_data, NULL);
-       gtk_signal_connect(GTK_OBJECT(file_clist), "drag_end",
-                               file_list_drag_end, NULL);
-
-       /* image */
-       image_dnd_init(main_image);
+       GtkWidget *window;
+       GtkWidget *label;
+
+       window = gtk_window_new(GTK_WINDOW_POPUP);
+       gtk_widget_realize (window);
+
+       label = gtk_label_new(text);
+       gtk_container_add(GTK_CONTAINER (window), label);
+       gtk_widget_show(label);
+       gtk_drag_set_icon_widget(context, window, -15, 10);
+       g_signal_connect(G_OBJECT(widget), "drag_end",
+                        G_CALLBACK(dnd_set_drag_label_end_cb), window);
 }
 
 
+/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */