aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/misc/e-cursors.c
blob: 224c4677335752ac8ea6f1d08d4686661a21d969 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * e-cursors.c - cursor handling for gnumeric
 * Copyright 2000, 2001, Ximian, Inc.
 *
 * Authors:
 *   Miguel de Icaza (miguel@gnu.org)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License, version 2, as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <config.h>

#include <stdio.h>

#include "e-colors.h"
#include "e-cursors.h"

#include "pixmaps/cursor_cross.xpm"
#include "pixmaps/cursor_zoom_in.xpm"
#include "pixmaps/cursor_zoom_out.xpm"
#include "pixmaps/cursor_hand_open.xpm"
#include "pixmaps/cursor_hand_closed.xpm"

#define GDK_INTERNAL_CURSOR -1

typedef struct {
    GdkCursor *cursor;
    int       hot_x, hot_y;
    char      **xpm;
} CursorDef;

static CursorDef cursors [] = {
    { NULL, 17, 17, cursor_cross_xpm },
    { NULL, GDK_INTERNAL_CURSOR,   GDK_CROSSHAIR,         NULL },
    { NULL, GDK_INTERNAL_CURSOR,   GDK_ARROW,             NULL },
    { NULL, GDK_INTERNAL_CURSOR,   GDK_FLEUR,             NULL },
    { NULL, 24, 24, cursor_zoom_in_xpm },
    { NULL, 24, 24, cursor_zoom_out_xpm },
    { NULL, GDK_INTERNAL_CURSOR,   GDK_SB_H_DOUBLE_ARROW, NULL },
    { NULL, GDK_INTERNAL_CURSOR,   GDK_SB_V_DOUBLE_ARROW, NULL },
    { NULL, GDK_INTERNAL_CURSOR,   GDK_SIZING,            NULL },
    { NULL, GDK_INTERNAL_CURSOR,   GDK_SIZING,            NULL },
    { NULL, GDK_INTERNAL_CURSOR,   GDK_HAND2,             NULL },
    { NULL, 10, 10, cursor_hand_open_xpm },
    { NULL, 10, 10, cursor_hand_closed_xpm },
    { NULL, GDK_INTERNAL_CURSOR,   GDK_XTERM,             NULL },
    { NULL, 0,    0,  NULL }
};


static void
create_bitmap_and_mask_from_xpm (GdkBitmap **bitmap, GdkBitmap **mask, gchar **xpm)
{
    int height, width, colors;
    char pixmap_buffer [(32 * 32)/8];
    char mask_buffer [(32 * 32)/8];
    int x, y, pix, yofs;
    int transparent_color, black_color;

    sscanf (xpm [0], "%d %d %d %d", &height, &width, &colors, &pix);

    g_return_if_fail (height == 32);
    g_return_if_fail (width  == 32);
    g_return_if_fail (colors <= 3);

    transparent_color = ' ';
    black_color = '.';

    yofs = colors + 1;
    for (y = 0; y < 32; y++){
        for (x = 0; x < 32;){
            char value = 0, maskv = 0;

            for (pix = 0; pix < 8; pix++, x++){
                if (xpm [y + yofs][x] != transparent_color){
                    maskv |= 1 << pix;

                    /*
                     * Invert the colours here because it seems
                     * to workaround a bug the Matrox G100 Xserver?
                     * We reverse the foreground & background in the next
                     * routine to compensate.
                     */
                    if (xpm [y + yofs][x] == black_color){
                        value |= 1 << pix;
                    }
                }
            }
            pixmap_buffer [(y * 4 + x/8)-1] = value;
            mask_buffer [(y * 4 + x/8)-1] = maskv;
        }
    }
    *bitmap = gdk_bitmap_create_from_data (NULL, pixmap_buffer, 32, 32);
    *mask   = gdk_bitmap_create_from_data (NULL, mask_buffer, 32, 32);
}

void
e_cursors_init (void)
{
    int i;

    e_color_init ();

    for (i = 0; cursors [i].hot_x; i++){
        if (cursors [i].hot_x < 0)
            cursors [i].cursor = gdk_cursor_new (cursors [i].hot_y);
        else {
            GdkBitmap *bitmap = NULL, *mask = NULL;

            create_bitmap_and_mask_from_xpm (&bitmap, &mask, cursors [i].xpm);

            /* The foreground and background colours are reversed.
             * See comment above for explanation.
             */
            cursors [i].cursor =
                gdk_cursor_new_from_pixmap (
                    bitmap, mask,
                    &e_black, &e_white,
                    cursors [i].hot_x,
                    cursors [i].hot_y);

            g_object_unref (bitmap);
            g_object_unref (mask);
        }
    }

    g_return_if_fail (i == E_CURSOR_NUM_CURSORS);
}

void
e_cursors_shutdown (void)
{
    int i;

    for (i = 0; cursors [i].hot_x; i++)
        gdk_cursor_unref (cursors [i].cursor);
}


/* Returns a cursor given its type */
GdkCursor *
e_cursor_get (ECursorType type)
{
    g_return_val_if_fail (type >= 0 && type < E_CURSOR_NUM_CURSORS, NULL);

    return cursors [type].cursor;
}