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++; }
68 const gint p_step = alpha ? 4 : 3;
69 for (i = 1; i < h - 1; i++)
72 *p = 0; p++; *p = 0; p++; *p = 0; p++;
75 p = pix + rs * i + (w - 1) * p_step;
76 *p = 0; p++; *p = 0; p++; *p = 0; p++;
79 p = pix + rs * (h - 1);
80 for (i = 0; i < w; i++)
82 *p = 0; p++; *p = 0; p++; *p = 0; p++;
83 if (alpha) { *p= 255; p++; }
87 #pragma GCC diagnostic push
88 #pragma GCC diagnostic ignored "-Wunused-function"
89 static void pixbuf_draw_rect_unused(GdkPixbuf *pixbuf, gint x, gint y, gint w, gint h, guint8 val)
98 alpha = gdk_pixbuf_get_has_alpha(pixbuf);
99 rs = gdk_pixbuf_get_rowstride(pixbuf);
100 pix = gdk_pixbuf_get_pixels(pixbuf);
102 const gint p_step = alpha ? 4 : 3;
104 for (j = 0; j < h; j++)
106 p = pix + (rs * (y + j)) + (x * p_step);
107 for (i = 0; i < w; i++)
109 *p = (*p * (256-val)) >> 8; p++;
110 *p = (*p * (256-val)) >> 8; p++;
111 *p = (*p * (256-val)) >> 8; p++;
112 if (alpha) { *p = 255; p++; }
116 #pragma GCC diagnostic pop
118 void dnd_set_drag_icon(GtkWidget *widget, GdkDragContext *context, GdkPixbuf *pixbuf, gint items)
125 PangoLayout *layout = nullptr;
131 sw = gdk_pixbuf_get_width(pixbuf);
132 sh = gdk_pixbuf_get_height(pixbuf);
134 if (sw <= DND_ICON_SIZE && sh <= DND_ICON_SIZE)
141 w = sw * DND_ICON_SIZE / sh;
147 h = sh * DND_ICON_SIZE / sw;
150 dest = gdk_pixbuf_scale_simple(pixbuf, MAX(1, w), MAX(1, h), GDK_INTERP_BILINEAR);
151 pixbuf_draw_border(dest, MAX(1, w), MAX(1, h));
159 layout = gtk_widget_create_pango_layout(widget, nullptr);
160 buf = g_strdup_printf("<small> %d </small>", items);
161 pango_layout_set_markup(layout, buf, -1);
164 pango_layout_get_pixel_size(layout, &lw, &lh);
168 lw = CLAMP(lw, 0, w - x - 1);
169 lh = CLAMP(lh, 0, h - y - 1);
171 pixbuf_draw_rect_fill(dest, x, y, lw, lh, 128, 128, 128, 255);
176 pixbuf_draw_layout(dest, layout, nullptr, x+1, y+1, 0, 0, 0, 255);
177 pixbuf_draw_layout(dest, layout, nullptr, x, y, 255, 255, 255, 255);
179 g_object_unref(G_OBJECT(layout));
182 gtk_drag_set_icon_pixbuf(context, dest, -8, -6);
184 g_object_unref(dest);
187 static void dnd_set_drag_label_end_cb(GtkWidget *widget, GdkDragContext *, gpointer data)
189 auto window = static_cast<GtkWidget *>(data);
190 g_signal_handlers_disconnect_by_func(widget, (gpointer)dnd_set_drag_label_end_cb, data);
191 gq_gtk_widget_destroy(window);
194 void dnd_set_drag_label(GtkWidget *widget, GdkDragContext *context, const gchar *text)
199 window = gtk_window_new(GTK_WINDOW_POPUP);
200 gtk_widget_realize (window);
202 label = gtk_label_new(text);
203 gq_gtk_container_add(GTK_WIDGET (window), label);
204 gtk_widget_show(label);
205 gtk_drag_set_icon_widget(context, window, -15, 10);
206 g_signal_connect(G_OBJECT(widget), "drag_end",
207 G_CALLBACK(dnd_set_drag_label_end_cb), window);
211 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */