Code

specialize MaybeStorage for Rect, and start using reference maybes to
[inkscape.git] / src / sp-cursor.cpp
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 sp_cursor_bitmap_and_mask_from_xpm (GdkBitmap **bitmap, GdkBitmap **mask, gchar **xpm)
21 {
22     int height;
23     int width;
24     int colors;
25     int pix;
26     sscanf(xpm[0], "%d %d %d %d", &height, &width, &colors, &pix);
28     g_return_if_fail (height == 32);
29     g_return_if_fail (width == 32);
30     g_return_if_fail (colors >= 3);
32     int transparent_color = ' ';
33     int black_color = '.';
34         
35     char pixmap_buffer[(32 * 32)/8];
36     char mask_buffer[(32 * 32)/8];
38     for (int i = 0; i < colors; i++) {
40         char const *p = xpm[1 + i];
41         char const ccode = *p;
43         p++;
44         while (isspace(*p)) {
45             p++;
46         }
47         p++;
48         while (isspace(*p)) {
49             p++;
50         }
52         if (strcmp(p, "None") == 0) {
53             transparent_color = ccode;
54         }
56         if (strcmp(p, "#000000") == 0) {
57             black_color = ccode;
58         }
59     }
61     for (int y = 0; y < 32; y++) {
62         for (int x = 0; x < 32; ) {
64             char value = 0;
65             char maskv = 0;
66                         
67             for (int pix = 0; pix < 8; pix++, x++){
68                 if (xpm [4+y][x] != transparent_color) {
69                     maskv |= 1 << pix;
71                     if (xpm [4+y][x] == black_color) {
72                         value |= 1 << pix;
73                     }
74                 }
75             }
77             pixmap_buffer[(y * 4 + x/8)-1] = value;
78             mask_buffer[(y * 4 + x/8)-1] = maskv;
79         }
80     }
82     *bitmap = gdk_bitmap_create_from_data(NULL, pixmap_buffer, 32, 32);
83     *mask   = gdk_bitmap_create_from_data(NULL, mask_buffer, 32, 32);
84 }
86 GdkCursor *sp_cursor_new_from_xpm (gchar **xpm, gint hot_x, gint hot_y)
87 {
88     GdkColor const fg = { 0, 0, 0, 0 };
89     GdkColor const bg = { 0, 65535, 65535, 65535 };
91     GdkBitmap *bitmap = NULL;
92     GdkBitmap *mask = NULL;
94     sp_cursor_bitmap_and_mask_from_xpm (&bitmap, &mask, xpm);
95     if ( bitmap != NULL && mask != NULL ) {
96         GdkCursor *new_cursor = gdk_cursor_new_from_pixmap (bitmap, mask,
97                                            &fg, &bg,
98                                            hot_x, hot_y);
99         g_object_unref (bitmap);
100         g_object_unref (mask);
101         return new_cursor;
102     }
104     return NULL;
107 /*
108   Local Variables:
109   mode:c++
110   c-file-style:"stroustrup"
111   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
112   indent-tabs-mode:nil
113   fill-column:99
114   End:
115 */
116 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :