1 #define __SP_CURSOR_C__
3 /*
4 * Some convenience stuff
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * Copyright (C) 1999-2002 authors
10 * Copyright (C) 2001-2002 Ximian, Inc.
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #include <cstdio>
16 #include <cstring>
17 #include <string>
18 #include <ctype.h>
19 #include "sp-cursor.h"
21 void
22 sp_cursor_bitmap_and_mask_from_xpm(GdkBitmap **bitmap, GdkBitmap **mask, gchar const *const *xpm)
23 {
24 int height;
25 int width;
26 int colors;
27 int pix;
28 sscanf(xpm[0], "%d %d %d %d", &height, &width, &colors, &pix);
30 g_return_if_fail (height == 32);
31 g_return_if_fail (width == 32);
32 g_return_if_fail (colors >= 3);
34 int transparent_color = ' ';
35 int black_color = '.';
37 char pixmap_buffer[(32 * 32)/8];
38 char mask_buffer[(32 * 32)/8];
40 for (int i = 0; i < colors; i++) {
42 char const *p = xpm[1 + i];
43 char const ccode = *p;
45 p++;
46 while (isspace(*p)) {
47 p++;
48 }
49 p++;
50 while (isspace(*p)) {
51 p++;
52 }
54 if (strcmp(p, "None") == 0) {
55 transparent_color = ccode;
56 }
58 if (strcmp(p, "#000000") == 0) {
59 black_color = ccode;
60 }
61 }
63 for (int y = 0; y < 32; y++) {
64 for (int x = 0; x < 32; ) {
66 char value = 0;
67 char maskv = 0;
69 for (int pix = 0; pix < 8; pix++, x++){
70 if (xpm[4+y][x] != transparent_color) {
71 maskv |= 1 << pix;
73 if (xpm[4+y][x] == black_color) {
74 value |= 1 << pix;
75 }
76 }
77 }
79 pixmap_buffer[(y * 4 + x/8)-1] = value;
80 mask_buffer[(y * 4 + x/8)-1] = maskv;
81 }
82 }
84 *bitmap = gdk_bitmap_create_from_data(NULL, pixmap_buffer, 32, 32);
85 *mask = gdk_bitmap_create_from_data(NULL, mask_buffer, 32, 32);
86 }
88 GdkCursor *
89 sp_cursor_new_from_xpm(gchar const *const *xpm, gint hot_x, gint hot_y)
90 {
91 GdkColor const fg = { 0, 0, 0, 0 };
92 GdkColor const bg = { 0, 65535, 65535, 65535 };
94 GdkBitmap *bitmap = NULL;
95 GdkBitmap *mask = NULL;
97 sp_cursor_bitmap_and_mask_from_xpm (&bitmap, &mask, xpm);
98 if ( bitmap != NULL && mask != NULL ) {
99 GdkCursor *new_cursor = gdk_cursor_new_from_pixmap (bitmap, mask,
100 &fg, &bg,
101 hot_x, hot_y);
102 g_object_unref (bitmap);
103 g_object_unref (mask);
104 return new_cursor;
105 }
107 return NULL;
108 }
110 /*
111 Local Variables:
112 mode:c++
113 c-file-style:"stroustrup"
114 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
115 indent-tabs-mode:nil
116 fill-column:99
117 End:
118 */
119 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :