Convert GET_{LEFT,RIGHT}_PIXBUF_OFFSET macro to function
[geeqie.git] / src / dnd.cc
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 "dnd.h"
23
24 #include <glib-object.h>
25 #include <pango/pango.h>
26
27 #include "compat.h"
28 #include "options.h"
29 #include "pixbuf-util.h"
30
31
32 GtkTargetEntry dnd_file_drag_types[] = {
33         { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST },
34         { const_cast<gchar *>("text/plain"), 0, TARGET_TEXT_PLAIN }
35 };
36 gint dnd_file_drag_types_count = 2;
37
38 GtkTargetEntry dnd_file_drop_types[] = {
39         { const_cast<gchar *>(TARGET_APP_COLLECTION_MEMBER_STRING), 0, TARGET_APP_COLLECTION_MEMBER },
40         { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST },
41         { const_cast<gchar *>("text/plain"), 0, TARGET_TEXT_PLAIN },
42 };
43 gint dnd_file_drop_types_count = 3;
44
45
46 #define DND_ICON_SIZE (options->dnd_icon_size)
47
48
49 static void pixbuf_draw_border(GdkPixbuf *pixbuf, gint w, gint h)
50 {
51         gboolean alpha;
52         gint rs;
53         guchar *pix;
54         guchar *p;
55         gint i;
56
57         alpha = gdk_pixbuf_get_has_alpha(pixbuf);
58         rs = gdk_pixbuf_get_rowstride(pixbuf);
59         pix = gdk_pixbuf_get_pixels(pixbuf);
60
61         p = pix;
62         for (i = 0; i < w; i++)
63                 {
64                 *p = 0; p++; *p = 0; p++; *p = 0; p++;
65                 if (alpha) { *p= 255; p++; }
66                 }
67
68         const gint p_step = alpha ? 4 : 3;
69         for (i = 1; i < h - 1; i++)
70                 {
71                 p = pix + rs * i;
72                 *p = 0; p++; *p = 0; p++; *p = 0; p++;
73                 if (alpha) *p= 255;
74
75                 p = pix + rs * i + (w - 1) * p_step;
76                 *p = 0; p++; *p = 0; p++; *p = 0; p++;
77                 if (alpha) *p= 255;
78                 }
79         p = pix + rs * (h - 1);
80         for (i = 0; i < w; i++)
81                 {
82                 *p = 0; p++; *p = 0; p++; *p = 0; p++;
83                 if (alpha) { *p= 255; p++; }
84                 }
85 }
86
87 #pragma GCC diagnostic push
88 #pragma GCC diagnostic ignored "-Wunused-function"
89 static void pixbuf_draw_rect_unused(GdkPixbuf *pixbuf, gint x, gint y, gint w, gint h, guint8 val)
90 {
91         gboolean alpha;
92         gint rs;
93         guchar *pix;
94         guchar *p;
95         gint i;
96         gint j;
97
98         alpha = gdk_pixbuf_get_has_alpha(pixbuf);
99         rs = gdk_pixbuf_get_rowstride(pixbuf);
100         pix = gdk_pixbuf_get_pixels(pixbuf);
101
102         const gint p_step = alpha ? 4 : 3;
103
104         for (j = 0; j < h; j++)
105                 {
106                 p = pix + (rs * (y + j)) + (x * p_step);
107                 for (i = 0; i < w; i++)
108                         {
109                         *p = (*p * (256-val)) >> 8; p++;
110                         *p = (*p * (256-val)) >> 8; p++;
111                         *p = (*p * (256-val)) >> 8; p++;
112                         if (alpha) { *p = 255; p++; }
113                         }
114                 }
115 }
116 #pragma GCC diagnostic pop
117
118 void dnd_set_drag_icon(GtkWidget *widget, GdkDragContext *context, GdkPixbuf *pixbuf, gint items)
119 {
120         GdkPixbuf *dest;
121         gint w;
122         gint h;
123         gint sw;
124         gint sh;
125         PangoLayout *layout = nullptr;
126         gint x;
127         gint y;
128
129         x = y = 0;
130
131         sw = gdk_pixbuf_get_width(pixbuf);
132         sh = gdk_pixbuf_get_height(pixbuf);
133
134         if (sw <= DND_ICON_SIZE && sh <= DND_ICON_SIZE)
135                 {
136                 w = sw;
137                 h = sh;
138                 }
139         else if (sw < sh)
140                 {
141                 w = sw * DND_ICON_SIZE / sh;
142                 h = DND_ICON_SIZE;
143                 }
144         else
145                 {
146                 w = DND_ICON_SIZE;
147                 h = sh * DND_ICON_SIZE / sw;
148                 }
149
150         dest = gdk_pixbuf_scale_simple(pixbuf, MAX(1, w), MAX(1, h), GDK_INTERP_BILINEAR);
151         pixbuf_draw_border(dest, MAX(1, w), MAX(1, h));
152
153         if (items > 1)
154                 {
155                 gchar *buf;
156                 gint lw;
157                 gint lh;
158
159                 layout = gtk_widget_create_pango_layout(widget, nullptr);
160                 buf = g_strdup_printf("<small> %d </small>", items);
161                 pango_layout_set_markup(layout, buf, -1);
162                 g_free(buf);
163
164                 pango_layout_get_pixel_size(layout, &lw, &lh);
165
166                 x = MAX(0, w - lw);
167                 y = MAX(0, h - lh);
168                 lw = CLAMP(lw, 0, w - x - 1);
169                 lh = CLAMP(lh, 0, h - y - 1);
170
171                 pixbuf_draw_rect_fill(dest, x, y, lw, lh, 128, 128, 128, 255);
172                 }
173
174         if (layout)
175                 {
176                 pixbuf_draw_layout(dest, layout, nullptr, x+1, y+1, 0, 0, 0, 255);
177                 pixbuf_draw_layout(dest, layout, nullptr, x, y, 255, 255, 255, 255);
178
179                 g_object_unref(G_OBJECT(layout));
180                 }
181
182         gtk_drag_set_icon_pixbuf(context, dest, -8, -6);
183
184         g_object_unref(dest);
185 }
186
187 static void dnd_set_drag_label_end_cb(GtkWidget *widget, GdkDragContext *, gpointer data)
188 {
189         auto window = static_cast<GtkWidget *>(data);
190         g_signal_handlers_disconnect_by_func(widget, (gpointer)dnd_set_drag_label_end_cb, data);
191         gq_gtk_widget_destroy(window);
192 }
193
194 void dnd_set_drag_label(GtkWidget *widget, GdkDragContext *context, const gchar *text)
195 {
196         GtkWidget *window;
197         GtkWidget *label;
198
199         window = gtk_window_new(GTK_WINDOW_POPUP);
200         gtk_widget_realize (window);
201
202         label = gtk_label_new(text);
203         gq_gtk_container_add(GTK_WIDGET (window), label);
204         gtk_widget_show(label);
205         gtk_drag_set_icon_widget(context, window, -15, 10);
206         g_signal_connect(G_OBJECT(widget), "drag_end",
207                          G_CALLBACK(dnd_set_drag_label_end_cb), window);
208 }
209
210
211 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */