Adding a vim modeline to all files - patch by Klaus Ethgen
[geeqie.git] / src / dnd.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 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 };
32 gint dnd_file_drop_types_count = 2;
33
34
35 #define DND_ICON_SIZE (options->dnd_icon_size)
36
37
38 static void pixbuf_draw_border(GdkPixbuf *pixbuf, gint w, gint h)
39 {
40         gint alpha;
41         gint rs;
42         guchar *pix;
43         guchar *p;
44         gint i;
45
46         alpha = gdk_pixbuf_get_has_alpha(pixbuf);
47         rs = gdk_pixbuf_get_rowstride(pixbuf);
48         pix = gdk_pixbuf_get_pixels(pixbuf);
49
50         p = pix;
51         for (i = 0; i < w; i++)
52                 {
53                 *p = 0; p++; *p = 0; p++; *p = 0; p++;
54                 if (alpha) { *p= 255; p++; }
55                 }
56         for (i = 1; i < h - 1; i++)
57                 {
58                 p = pix + rs * i;
59                 *p = 0; p++; *p = 0; p++; *p = 0; p++;
60                 if (alpha) *p= 255;
61
62                 p = pix + rs * i + (w - 1) * ((alpha == TRUE) ? 4 : 3);
63                 *p = 0; p++; *p = 0; p++; *p = 0; p++;
64                 if (alpha) *p= 255;
65                 }
66         p = pix + rs * (h - 1);
67         for (i = 0; i < w; i++)
68                 {
69                 *p = 0; p++; *p = 0; p++; *p = 0; p++;
70                 if (alpha) { *p= 255; p++; }
71                 }
72 }
73
74 static void pixbuf_draw_rect(GdkPixbuf *pixbuf, gint x, gint y, gint w, gint h, guint8 val)
75 {
76         gint alpha;
77         gint rs;
78         guchar *pix;
79         guchar *p;
80         gint i, j;
81
82         alpha = gdk_pixbuf_get_has_alpha(pixbuf);
83         rs = gdk_pixbuf_get_rowstride(pixbuf);
84         pix = gdk_pixbuf_get_pixels(pixbuf);
85
86         for (j = 0; j < h; j++)
87                 {
88                 p = pix + (rs * (y + j)) + (x * ((alpha) ? 4 : 3));
89                 for (i = 0; i < w; i++)
90                         {
91                         *p = (*p * (256-val)) >> 8; p++;
92                         *p = (*p * (256-val)) >> 8; p++;
93                         *p = (*p * (256-val)) >> 8; p++;
94                         if (alpha) { *p = 255; p++; }
95                         }
96                 }
97 }
98
99 void dnd_set_drag_icon(GtkWidget *widget, GdkDragContext *context, GdkPixbuf *pixbuf, gint items)
100 {
101         GdkPixmap *pixmap;
102         GdkBitmap *mask;
103         GdkPixbuf *dest;
104         gint w, h;
105         gint sw, sh;
106         PangoLayout *layout = NULL;
107         gint x, y;
108
109         x = y = 0;
110
111         sw = gdk_pixbuf_get_width(pixbuf);
112         sh = gdk_pixbuf_get_height(pixbuf);
113
114         if (sw <= DND_ICON_SIZE && sh <= DND_ICON_SIZE)
115                 {
116                 w = sw;
117                 h = sh;
118                 }
119         else if (sw < sh)
120                 {
121                 w = sw * DND_ICON_SIZE / sh;
122                 h = DND_ICON_SIZE;
123                 }
124         else
125                 {
126                 w = DND_ICON_SIZE;
127                 h = sh * DND_ICON_SIZE / sw;
128                 }
129
130         dest = gdk_pixbuf_scale_simple(pixbuf, w, h, GDK_INTERP_BILINEAR);
131         pixbuf_draw_border(dest, w, h);
132
133         if (items > 1)
134                 {
135                 gchar *buf;
136                 gint lw,lh;
137
138                 layout = gtk_widget_create_pango_layout(widget, NULL);
139                 buf = g_strdup_printf("<small> %d </small>", items);
140                 pango_layout_set_markup(layout, buf, -1);
141                 g_free(buf);
142
143                 pango_layout_get_pixel_size(layout, &lw, &lh);
144
145                 x = MAX(0, w - lw);
146                 y = MAX(0, h - lh);
147                 lw = CLAMP(lw, 0, w - x - 1);
148                 lh = CLAMP(lh, 0, h - y - 1);
149
150                 pixbuf_draw_rect(dest, x, y, lw, lh, 128);
151                 }
152
153         gdk_pixbuf_render_pixmap_and_mask(dest, &pixmap, &mask, 128);
154         g_object_unref(dest);
155
156         if (layout)
157                 {
158                 gdk_draw_layout(pixmap, widget->style->black_gc, x+1, y+1, layout);
159                 gdk_draw_layout(pixmap, widget->style->white_gc, x, y, layout);
160
161                 g_object_unref(G_OBJECT(layout));
162                 }
163
164         gtk_drag_set_icon_pixmap(context, gtk_widget_get_colormap(widget), pixmap, mask, -8, -6);
165
166         g_object_unref(pixmap);
167         if (mask) g_object_unref(mask);
168 }
169 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */