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.
25 #include "pixbuf-util.h"
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 }
32 gint dnd_file_drag_types_count = 2;
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 },
39 gint dnd_file_drop_types_count = 3;
42 #define DND_ICON_SIZE (options->dnd_icon_size)
45 static void pixbuf_draw_border(GdkPixbuf *pixbuf, gint w, gint h)
53 alpha = gdk_pixbuf_get_has_alpha(pixbuf);
54 rs = gdk_pixbuf_get_rowstride(pixbuf);
55 pix = gdk_pixbuf_get_pixels(pixbuf);
58 for (i = 0; i < w; i++)
60 *p = 0; p++; *p = 0; p++; *p = 0; p++;
61 if (alpha) { *p= 255; p++; }
63 for (i = 1; i < h - 1; i++)
66 *p = 0; p++; *p = 0; p++; *p = 0; p++;
69 p = pix + rs * i + (w - 1) * ((alpha == TRUE) ? 4 : 3);
70 *p = 0; p++; *p = 0; p++; *p = 0; p++;
73 p = pix + rs * (h - 1);
74 for (i = 0; i < w; i++)
76 *p = 0; p++; *p = 0; p++; *p = 0; p++;
77 if (alpha) { *p= 255; p++; }
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)
92 alpha = gdk_pixbuf_get_has_alpha(pixbuf);
93 rs = gdk_pixbuf_get_rowstride(pixbuf);
94 pix = gdk_pixbuf_get_pixels(pixbuf);
96 for (j = 0; j < h; j++)
98 p = pix + (rs * (y + j)) + (x * ((alpha) ? 4 : 3));
99 for (i = 0; i < w; i++)
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++; }
108 #pragma GCC diagnostic pop
110 void dnd_set_drag_icon(GtkWidget *widget, GdkDragContext *context, GdkPixbuf *pixbuf, gint items)
117 PangoLayout *layout = nullptr;
123 sw = gdk_pixbuf_get_width(pixbuf);
124 sh = gdk_pixbuf_get_height(pixbuf);
126 if (sw <= DND_ICON_SIZE && sh <= DND_ICON_SIZE)
133 w = sw * DND_ICON_SIZE / sh;
139 h = sh * DND_ICON_SIZE / sw;
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));
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);
156 pango_layout_get_pixel_size(layout, &lw, &lh);
160 lw = CLAMP(lw, 0, w - x - 1);
161 lh = CLAMP(lh, 0, h - y - 1);
163 pixbuf_draw_rect_fill(dest, x, y, lw, lh, 128, 128, 128, 255);
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);
171 g_object_unref(G_OBJECT(layout));
174 gtk_drag_set_icon_pixbuf(context, dest, -8, -6);
176 g_object_unref(dest);
179 static void dnd_set_drag_label_end_cb(GtkWidget *widget, GdkDragContext *, gpointer data)
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);
186 void dnd_set_drag_label(GtkWidget *widget, GdkDragContext *context, const gchar *text)
191 window = gtk_window_new(GTK_WINDOW_POPUP);
192 gtk_widget_realize (window);
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);
203 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */