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