Fix warnings in exiv2
[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         for (i = 1; i < h - 1; i++)
68                 {
69                 p = pix + rs * i;
70                 *p = 0; p++; *p = 0; p++; *p = 0; p++;
71                 if (alpha) *p= 255;
72
73                 p = pix + rs * i + (w - 1) * ((alpha == TRUE) ? 4 : 3);
74                 *p = 0; p++; *p = 0; p++; *p = 0; p++;
75                 if (alpha) *p= 255;
76                 }
77         p = pix + rs * (h - 1);
78         for (i = 0; i < w; i++)
79                 {
80                 *p = 0; p++; *p = 0; p++; *p = 0; p++;
81                 if (alpha) { *p= 255; p++; }
82                 }
83 }
84
85 #pragma GCC diagnostic push
86 #pragma GCC diagnostic ignored "-Wunused-function"
87 static void pixbuf_draw_rect_unused(GdkPixbuf *pixbuf, gint x, gint y, gint w, gint h, guint8 val)
88 {
89         gboolean alpha;
90         gint rs;
91         guchar *pix;
92         guchar *p;
93         gint i;
94         gint j;
95
96         alpha = gdk_pixbuf_get_has_alpha(pixbuf);
97         rs = gdk_pixbuf_get_rowstride(pixbuf);
98         pix = gdk_pixbuf_get_pixels(pixbuf);
99
100         for (j = 0; j < h; j++)
101                 {
102                 p = pix + (rs * (y + j)) + (x * ((alpha) ? 4 : 3));
103                 for (i = 0; i < w; i++)
104                         {
105                         *p = (*p * (256-val)) >> 8; p++;
106                         *p = (*p * (256-val)) >> 8; p++;
107                         *p = (*p * (256-val)) >> 8; p++;
108                         if (alpha) { *p = 255; p++; }
109                         }
110                 }
111 }
112 #pragma GCC diagnostic pop
113
114 void dnd_set_drag_icon(GtkWidget *widget, GdkDragContext *context, GdkPixbuf *pixbuf, gint items)
115 {
116         GdkPixbuf *dest;
117         gint w;
118         gint h;
119         gint sw;
120         gint sh;
121         PangoLayout *layout = nullptr;
122         gint x;
123         gint y;
124
125         x = y = 0;
126
127         sw = gdk_pixbuf_get_width(pixbuf);
128         sh = gdk_pixbuf_get_height(pixbuf);
129
130         if (sw <= DND_ICON_SIZE && sh <= DND_ICON_SIZE)
131                 {
132                 w = sw;
133                 h = sh;
134                 }
135         else if (sw < sh)
136                 {
137                 w = sw * DND_ICON_SIZE / sh;
138                 h = DND_ICON_SIZE;
139                 }
140         else
141                 {
142                 w = DND_ICON_SIZE;
143                 h = sh * DND_ICON_SIZE / sw;
144                 }
145
146         dest = gdk_pixbuf_scale_simple(pixbuf, MAX(1, w), MAX(1, h), GDK_INTERP_BILINEAR);
147         pixbuf_draw_border(dest, MAX(1, w), MAX(1, h));
148
149         if (items > 1)
150                 {
151                 gchar *buf;
152                 gint lw;
153                 gint lh;
154
155                 layout = gtk_widget_create_pango_layout(widget, nullptr);
156                 buf = g_strdup_printf("<small> %d </small>", items);
157                 pango_layout_set_markup(layout, buf, -1);
158                 g_free(buf);
159
160                 pango_layout_get_pixel_size(layout, &lw, &lh);
161
162                 x = MAX(0, w - lw);
163                 y = MAX(0, h - lh);
164                 lw = CLAMP(lw, 0, w - x - 1);
165                 lh = CLAMP(lh, 0, h - y - 1);
166
167                 pixbuf_draw_rect_fill(dest, x, y, lw, lh, 128, 128, 128, 255);
168                 }
169
170         if (layout)
171                 {
172                 pixbuf_draw_layout(dest, layout, nullptr, x+1, y+1, 0, 0, 0, 255);
173                 pixbuf_draw_layout(dest, layout, nullptr, x, y, 255, 255, 255, 255);
174
175                 g_object_unref(G_OBJECT(layout));
176                 }
177
178         gtk_drag_set_icon_pixbuf(context, dest, -8, -6);
179
180         g_object_unref(dest);
181 }
182
183 static void dnd_set_drag_label_end_cb(GtkWidget *widget, GdkDragContext *, gpointer data)
184 {
185         auto window = static_cast<GtkWidget *>(data);
186         g_signal_handlers_disconnect_by_func(widget, (gpointer)dnd_set_drag_label_end_cb, data);
187         gq_gtk_widget_destroy(window);
188 }
189
190 void dnd_set_drag_label(GtkWidget *widget, GdkDragContext *context, const gchar *text)
191 {
192         GtkWidget *window;
193         GtkWidget *label;
194
195         window = gtk_window_new(GTK_WINDOW_POPUP);
196         gtk_widget_realize (window);
197
198         label = gtk_label_new(text);
199         gq_gtk_container_add(GTK_WIDGET (window), label);
200         gtk_widget_show(label);
201         gtk_drag_set_icon_widget(context, window, -15, 10);
202         g_signal_connect(G_OBJECT(widget), "drag_end",
203                          G_CALLBACK(dnd_set_drag_label_end_cb), window);
204 }
205
206
207 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */