Simplify vflist_get_formatted()
[geeqie.git] / src / dnd.c
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "main.h"
23 #include "dnd.h"
24
25 #include "collect.h"
26 #include "image.h"
27 #include "ui_fileops.h"
28 #include "pixbuf_util.h"
29
30
31 GtkTargetEntry dnd_file_drag_types[] = {
32         { "text/uri-list", 0, TARGET_URI_LIST },
33         { "text/plain", 0, TARGET_TEXT_PLAIN }
34 };
35 gint dnd_file_drag_types_count = 2;
36
37 GtkTargetEntry dnd_file_drop_types[] = {
38         { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
39         { "text/uri-list", 0, TARGET_URI_LIST },
40         { "text/plain", 0, TARGET_TEXT_PLAIN },
41 };
42 gint dnd_file_drop_types_count = 3;
43
44
45 #define DND_ICON_SIZE (options->dnd_icon_size)
46
47
48 static void pixbuf_draw_border(GdkPixbuf *pixbuf, gint w, gint h)
49 {
50         gboolean alpha;
51         gint rs;
52         guchar *pix;
53         guchar *p;
54         gint i;
55
56         alpha = gdk_pixbuf_get_has_alpha(pixbuf);
57         rs = gdk_pixbuf_get_rowstride(pixbuf);
58         pix = gdk_pixbuf_get_pixels(pixbuf);
59
60         p = pix;
61         for (i = 0; i < w; i++)
62                 {
63                 *p = 0; p++; *p = 0; p++; *p = 0; p++;
64                 if (alpha) { *p= 255; p++; }
65                 }
66         for (i = 1; i < h - 1; i++)
67                 {
68                 p = pix + rs * i;
69                 *p = 0; p++; *p = 0; p++; *p = 0; p++;
70                 if (alpha) *p= 255;
71
72                 p = pix + rs * i + (w - 1) * ((alpha == TRUE) ? 4 : 3);
73                 *p = 0; p++; *p = 0; p++; *p = 0; p++;
74                 if (alpha) *p= 255;
75                 }
76         p = pix + rs * (h - 1);
77         for (i = 0; i < w; i++)
78                 {
79                 *p = 0; p++; *p = 0; p++; *p = 0; p++;
80                 if (alpha) { *p= 255; p++; }
81                 }
82 }
83
84 /*
85 static void pixbuf_draw_rect(GdkPixbuf *pixbuf, gint x, gint y, gint w, gint h, guint8 val)
86 {
87         gboolean alpha;
88         gint rs;
89         guchar *pix;
90         guchar *p;
91         gint i, j;
92
93         alpha = gdk_pixbuf_get_has_alpha(pixbuf);
94         rs = gdk_pixbuf_get_rowstride(pixbuf);
95         pix = gdk_pixbuf_get_pixels(pixbuf);
96
97         for (j = 0; j < h; j++)
98                 {
99                 p = pix + (rs * (y + j)) + (x * ((alpha) ? 4 : 3));
100                 for (i = 0; i < w; i++)
101                         {
102                         *p = (*p * (256-val)) >> 8; p++;
103                         *p = (*p * (256-val)) >> 8; p++;
104                         *p = (*p * (256-val)) >> 8; p++;
105                         if (alpha) { *p = 255; p++; }
106                         }
107                 }
108 }
109 */
110 void dnd_set_drag_icon(GtkWidget *widget, GdkDragContext *context, GdkPixbuf *pixbuf, gint items)
111 {
112         GdkPixbuf *dest;
113         gint w, h;
114         gint sw, sh;
115         PangoLayout *layout = NULL;
116         gint x, y;
117
118         x = y = 0;
119
120         sw = gdk_pixbuf_get_width(pixbuf);
121         sh = gdk_pixbuf_get_height(pixbuf);
122
123         if (sw <= DND_ICON_SIZE && sh <= DND_ICON_SIZE)
124                 {
125                 w = sw;
126                 h = sh;
127                 }
128         else if (sw < sh)
129                 {
130                 w = sw * DND_ICON_SIZE / sh;
131                 h = DND_ICON_SIZE;
132                 }
133         else
134                 {
135                 w = DND_ICON_SIZE;
136                 h = sh * DND_ICON_SIZE / sw;
137                 }
138
139         dest = gdk_pixbuf_scale_simple(pixbuf, w, h, GDK_INTERP_BILINEAR);
140         pixbuf_draw_border(dest, w, h);
141
142         if (items > 1)
143                 {
144                 gchar *buf;
145                 gint lw,lh;
146
147                 layout = gtk_widget_create_pango_layout(widget, NULL);
148                 buf = g_strdup_printf("<small> %d </small>", items);
149                 pango_layout_set_markup(layout, buf, -1);
150                 g_free(buf);
151
152                 pango_layout_get_pixel_size(layout, &lw, &lh);
153
154                 x = MAX(0, w - lw);
155                 y = MAX(0, h - lh);
156                 lw = CLAMP(lw, 0, w - x - 1);
157                 lh = CLAMP(lh, 0, h - y - 1);
158
159                 pixbuf_draw_rect_fill(dest, x, y, lw, lh, 128, 128, 128, 255);
160                 }
161
162         if (layout)
163                 {
164                 pixbuf_draw_layout(dest, layout, NULL, x+1, y+1, 0, 0, 0, 255);
165                 pixbuf_draw_layout(dest, layout, NULL, x, y, 255, 255, 255, 255);
166
167                 g_object_unref(G_OBJECT(layout));
168                 }
169
170         gtk_drag_set_icon_pixbuf(context, dest, -8, -6);
171
172         g_object_unref(dest);
173 }
174
175 static void dnd_set_drag_label_end_cb(GtkWidget *widget, GdkDragContext *context, gpointer data)
176 {
177         GtkWidget *window = data;
178         g_signal_handlers_disconnect_by_func(widget, dnd_set_drag_label_end_cb, data);
179         gtk_widget_destroy(window);
180 }
181
182 void dnd_set_drag_label(GtkWidget *widget, GdkDragContext *context, const gchar *text)
183 {
184         GtkWidget *window;
185         GtkWidget *label;
186
187         window = gtk_window_new(GTK_WINDOW_POPUP);
188         gtk_widget_realize (window);
189
190         label = gtk_label_new(text);
191         gtk_container_add(GTK_CONTAINER (window), label);
192         gtk_widget_show(label);
193         gtk_drag_set_icon_widget(context, window, -15, 10);
194         g_signal_connect(G_OBJECT(widget), "drag_end",
195                          G_CALLBACK(dnd_set_drag_label_end_cb), window);
196 }
197
198
199 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */