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