2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
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.
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.
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.
24 #include <glib-object.h>
25 #include <pango/pango.h>
29 #include "pixbuf-util.h"
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 }
36 gint dnd_file_drag_types_count = 2;
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 },
43 gint dnd_file_drop_types_count = 3;
46 #define DND_ICON_SIZE (options->dnd_icon_size)
49 static void pixbuf_draw_border(GdkPixbuf *pixbuf, gint w, gint h)
57 alpha = gdk_pixbuf_get_has_alpha(pixbuf);
58 rs = gdk_pixbuf_get_rowstride(pixbuf);
59 pix = gdk_pixbuf_get_pixels(pixbuf);
62 for (i = 0; i < w; i++)
64 *p = 0; p++; *p = 0; p++; *p = 0; p++;
65 if (alpha) { *p= 255; p++; }
67 for (i = 1; i < h - 1; i++)
70 *p = 0; p++; *p = 0; p++; *p = 0; p++;
73 p = pix + rs * i + (w - 1) * ((alpha == TRUE) ? 4 : 3);
74 *p = 0; p++; *p = 0; p++; *p = 0; p++;
77 p = pix + rs * (h - 1);
78 for (i = 0; i < w; i++)
80 *p = 0; p++; *p = 0; p++; *p = 0; p++;
81 if (alpha) { *p= 255; p++; }
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)
96 alpha = gdk_pixbuf_get_has_alpha(pixbuf);
97 rs = gdk_pixbuf_get_rowstride(pixbuf);
98 pix = gdk_pixbuf_get_pixels(pixbuf);
100 for (j = 0; j < h; j++)
102 p = pix + (rs * (y + j)) + (x * ((alpha) ? 4 : 3));
103 for (i = 0; i < w; i++)
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++; }
112 #pragma GCC diagnostic pop
114 void dnd_set_drag_icon(GtkWidget *widget, GdkDragContext *context, GdkPixbuf *pixbuf, gint items)
121 PangoLayout *layout = nullptr;
127 sw = gdk_pixbuf_get_width(pixbuf);
128 sh = gdk_pixbuf_get_height(pixbuf);
130 if (sw <= DND_ICON_SIZE && sh <= DND_ICON_SIZE)
137 w = sw * DND_ICON_SIZE / sh;
143 h = sh * DND_ICON_SIZE / sw;
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));
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);
160 pango_layout_get_pixel_size(layout, &lw, &lh);
164 lw = CLAMP(lw, 0, w - x - 1);
165 lh = CLAMP(lh, 0, h - y - 1);
167 pixbuf_draw_rect_fill(dest, x, y, lw, lh, 128, 128, 128, 255);
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);
175 g_object_unref(G_OBJECT(layout));
178 gtk_drag_set_icon_pixbuf(context, dest, -8, -6);
180 g_object_unref(dest);
183 static void dnd_set_drag_label_end_cb(GtkWidget *widget, GdkDragContext *, gpointer data)
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);
190 void dnd_set_drag_label(GtkWidget *widget, GdkDragContext *context, const gchar *text)
195 window = gtk_window_new(GTK_WINDOW_POPUP);
196 gtk_widget_realize (window);
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);
207 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */