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 <string.h>
17 #include <ctype.h>
18 #include "sp-cursor.h"
20 void
21 sp_cursor_bitmap_and_mask_from_xpm(GdkBitmap **bitmap, GdkBitmap **mask, gchar const *const *xpm)
22 {
23 int height;
24 int width;
25 int colors;
26 int pix;
27 sscanf(xpm[0], "%d %d %d %d", &height, &width, &colors, &pix);
29 g_return_if_fail (height == 32);
30 g_return_if_fail (width == 32);
31 g_return_if_fail (colors >= 3);
33 int transparent_color = ' ';
34 int black_color = '.';
36 char pixmap_buffer[(32 * 32)/8];
37 char mask_buffer[(32 * 32)/8];
39 for (int i = 0; i < colors; i++) {
41 char const *p = xpm[1 + i];
42 char const ccode = *p;
44 p++;
45 while (isspace(*p)) {
46 p++;
47 }
48 p++;
49 while (isspace(*p)) {
50 p++;
51 }
53 if (strcmp(p, "None") == 0) {
54 transparent_color = ccode;
55 }
57 if (strcmp(p, "#000000") == 0) {
58 black_color = ccode;
59 }
60 }
62 for (int y = 0; y < 32; y++) {
63 for (int x = 0; x < 32; ) {
65 char value = 0;
66 char maskv = 0;
68 for (int pix = 0; pix < 8; pix++, x++){
69 if (xpm[4+y][x] != transparent_color) {
70 maskv |= 1 << pix;
72 if (xpm[4+y][x] == black_color) {
73 value |= 1 << pix;
74 }
75 }
76 }
78 pixmap_buffer[(y * 4 + x/8)-1] = value;
79 mask_buffer[(y * 4 + x/8)-1] = maskv;
80 }
81 }
83 *bitmap = gdk_bitmap_create_from_data(NULL, pixmap_buffer, 32, 32);
84 *mask = gdk_bitmap_create_from_data(NULL, mask_buffer, 32, 32);
85 }
87 GdkCursor *
88 sp_cursor_new_from_xpm(gchar const *const *xpm, gint hot_x, gint hot_y)
89 {
90 GdkColor const fg = { 0, 0, 0, 0 };
91 GdkColor const bg = { 0, 65535, 65535, 65535 };
93 GdkBitmap *bitmap = NULL;
94 GdkBitmap *mask = NULL;
96 sp_cursor_bitmap_and_mask_from_xpm (&bitmap, &mask, xpm);
97 if ( bitmap != NULL && mask != NULL ) {
98 GdkCursor *new_cursor = gdk_cursor_new_from_pixmap (bitmap, mask,
99 &fg, &bg,
100 hot_x, hot_y);
101 g_object_unref (bitmap);
102 g_object_unref (mask);
103 return new_cursor;
104 }
106 return NULL;
107 }
109 /*
110 Local Variables:
111 mode:c++
112 c-file-style:"stroustrup"
113 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
114 indent-tabs-mode:nil
115 fill-column:99
116 End:
117 */
118 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :