use accessor functions
[geeqie.git] / src / dnd.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2012 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13
14 #include "main.h"
15 #include "dnd.h"
16
17 #include "collect.h"
18 #include "image.h"
19 #include "ui_fileops.h"
20
21
22 GtkTargetEntry dnd_file_drag_types[] = {
23         { "text/uri-list", 0, TARGET_URI_LIST },
24         { "text/plain", 0, TARGET_TEXT_PLAIN }
25 };
26 gint dnd_file_drag_types_count = 2;
27
28 GtkTargetEntry dnd_file_drop_types[] = {
29         { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
30         { "text/uri-list", 0, TARGET_URI_LIST },
31         { "text/plain", 0, TARGET_TEXT_PLAIN },
32 };
33 gint dnd_file_drop_types_count = 3;
34
35
36 #define DND_ICON_SIZE (options->dnd_icon_size)
37
38
39 static void pixbuf_draw_border(GdkPixbuf *pixbuf, gint w, gint h)
40 {
41         gboolean alpha;
42         gint rs;
43         guchar *pix;
44         guchar *p;
45         gint i;
46
47         alpha = gdk_pixbuf_get_has_alpha(pixbuf);
48         rs = gdk_pixbuf_get_rowstride(pixbuf);
49         pix = gdk_pixbuf_get_pixels(pixbuf);
50
51         p = pix;
52         for (i = 0; i < w; i++)
53                 {
54                 *p = 0; p++; *p = 0; p++; *p = 0; p++;
55                 if (alpha) { *p= 255; p++; }
56                 }
57         for (i = 1; i < h - 1; i++)
58                 {
59                 p = pix + rs * i;
60                 *p = 0; p++; *p = 0; p++; *p = 0; p++;
61                 if (alpha) *p= 255;
62
63                 p = pix + rs * i + (w - 1) * ((alpha == TRUE) ? 4 : 3);
64                 *p = 0; p++; *p = 0; p++; *p = 0; p++;
65                 if (alpha) *p= 255;
66                 }
67         p = pix + rs * (h - 1);
68         for (i = 0; i < w; i++)
69                 {
70                 *p = 0; p++; *p = 0; p++; *p = 0; p++;
71                 if (alpha) { *p= 255; p++; }
72                 }
73 }
74
75 static void pixbuf_draw_rect(GdkPixbuf *pixbuf, gint x, gint y, gint w, gint h, guint8 val)
76 {
77         gboolean alpha;
78         gint rs;
79         guchar *pix;
80         guchar *p;
81         gint i, j;
82
83         alpha = gdk_pixbuf_get_has_alpha(pixbuf);
84         rs = gdk_pixbuf_get_rowstride(pixbuf);
85         pix = gdk_pixbuf_get_pixels(pixbuf);
86
87         for (j = 0; j < h; j++)
88                 {
89                 p = pix + (rs * (y + j)) + (x * ((alpha) ? 4 : 3));
90                 for (i = 0; i < w; i++)
91                         {
92                         *p = (*p * (256-val)) >> 8; p++;
93                         *p = (*p * (256-val)) >> 8; p++;
94                         *p = (*p * (256-val)) >> 8; p++;
95                         if (alpha) { *p = 255; p++; }
96                         }
97                 }
98 }
99
100 void dnd_set_drag_icon(GtkWidget *widget, GdkDragContext *context, GdkPixbuf *pixbuf, gint items)
101 {
102         GdkPixbuf *dest;
103         gint w, h;
104         gint sw, sh;
105         PangoLayout *layout = NULL;
106         gint x, y;
107
108         x = y = 0;
109
110         sw = gdk_pixbuf_get_width(pixbuf);
111         sh = gdk_pixbuf_get_height(pixbuf);
112
113         if (sw <= DND_ICON_SIZE && sh <= DND_ICON_SIZE)
114                 {
115                 w = sw;
116                 h = sh;
117                 }
118         else if (sw < sh)
119                 {
120                 w = sw * DND_ICON_SIZE / sh;
121                 h = DND_ICON_SIZE;
122                 }
123         else
124                 {
125                 w = DND_ICON_SIZE;
126                 h = sh * DND_ICON_SIZE / sw;
127                 }
128
129         dest = gdk_pixbuf_scale_simple(pixbuf, w, h, GDK_INTERP_BILINEAR);
130         pixbuf_draw_border(dest, w, h);
131
132         if (items > 1)
133                 {
134                 gchar *buf;
135                 gint lw,lh;
136
137                 layout = gtk_widget_create_pango_layout(widget, NULL);
138                 buf = g_strdup_printf("<small> %d </small>", items);
139                 pango_layout_set_markup(layout, buf, -1);
140                 g_free(buf);
141
142                 pango_layout_get_pixel_size(layout, &lw, &lh);
143
144                 x = MAX(0, w - lw);
145                 y = MAX(0, h - lh);
146                 lw = CLAMP(lw, 0, w - x - 1);
147                 lh = CLAMP(lh, 0, h - y - 1);
148
149                 pixbuf_draw_rect(dest, x, y, lw, lh, 128);
150                 }
151
152         if (layout)
153                 {
154                 pixbuf_draw_layout(dest, layout, NULL, x+1, y+1, 0, 0, 0, 255);
155                 pixbuf_draw_layout(dest, layout, NULL, x, y, 255, 255, 255, 255);
156
157                 g_object_unref(G_OBJECT(layout));
158                 }
159
160         gtk_drag_set_icon_pixbuf(context, dest, -8, -6);
161
162         g_object_unref(dest);
163 }
164
165 static void dnd_set_drag_label_end_cb(GtkWidget *widget, GdkDragContext *context, gpointer data)
166 {
167         GtkWidget *window = data;
168         g_signal_handlers_disconnect_by_func(widget, dnd_set_drag_label_end_cb, data);
169         gtk_widget_destroy(window);
170 }
171
172 void dnd_set_drag_label(GtkWidget *widget, GdkDragContext *context, const gchar *text)
173 {
174         GtkWidget *window;
175         GtkWidget *label;
176
177         window = gtk_window_new(GTK_WINDOW_POPUP);
178         gtk_widget_realize (window);
179
180         label = gtk_label_new(text);
181         gtk_container_add(GTK_CONTAINER (window), label);
182         gtk_widget_show(label);
183         gtk_drag_set_icon_widget(context, window, -15, 10);
184         g_signal_connect(G_OBJECT(widget), "drag_end",
185                          G_CALLBACK(dnd_set_drag_label_end_cb), window);
186 }
187
188
189 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */