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