aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/misc/e-canvas-background.c
blob: 431e0fde6e64ce90af56ce76a6a3dcff47067958 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * e-canvas-background.c - background color for canvas.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Chris Lahey <clahey@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#include <config.h>

#include <math.h>
#include <stdio.h>
#include <string.h>

#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>

#include <glib/gi18n.h>
#include "e-util/e-util.h"
#include "misc/e-canvas.h"
#include "misc/e-canvas-utils.h"
#include "misc/e-hsv-utils.h"

#include "e-canvas-background.h"

/* workaround for avoiding API broken */
#define ecb_get_type e_canvas_background_get_type
G_DEFINE_TYPE (
    ECanvasBackground,
    ecb,
    GNOME_TYPE_CANVAS_ITEM)

#define d(x)

struct _ECanvasBackgroundPrivate {
    guint rgba;     /* Fill color, RGBA */
};

enum {
    STYLE_SET,
    LAST_SIGNAL
};

static guint ecb_signals[LAST_SIGNAL] = { 0, };

enum {
    PROP_0,
    PROP_FILL_COLOR,
    PROP_FILL_COLOR_GDK,
    PROP_FILL_COLOR_RGBA,
};

static void
ecb_bounds (GnomeCanvasItem *item, gdouble *x1, gdouble *y1, gdouble *x2, gdouble *y2)
{
    *x1 = -G_MAXDOUBLE;
    *y1 = -G_MAXDOUBLE;
    *x2 = G_MAXDOUBLE;
    *y2 = G_MAXDOUBLE;
}

static void
ecb_update (GnomeCanvasItem *item, const cairo_matrix_t *i2c, gint flags)
{
    gdouble x1, y1, x2, y2;

    x1 = item->x1;
    y1 = item->y1;
    x2 = item->x2;
    y2 = item->y2;

    /* The bounds are all constants so we should only have to
     * redraw once. */
    ecb_bounds (item, &item->x1, &item->y1, &item->x2, &item->y2);

    if (item->x1 != x1 || item->y1 != y1 ||
        item->x2 != x2 || item->y2 != y2)
        gnome_canvas_request_redraw (
            item->canvas, item->x1, item->y1, item->x2, item->y2);
}

static void
ecb_dispose (GObject *object)
{
    ECanvasBackground *ecb = E_CANVAS_BACKGROUND (object);

    if (ecb->priv) {
        g_free (ecb->priv);
        ecb->priv = NULL;
    }

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (ecb_parent_class)->dispose (object);
}

static void
ecb_set_property (GObject *object,
          guint prop_id,
          const GValue *value,
          GParamSpec *pspec)
{
    ECanvasBackground *ecb;

    GdkColor color = { 0, 0, 0, 0, };
    GdkColor *pcolor;

    ecb = E_CANVAS_BACKGROUND (object);

    switch (prop_id) {
    case PROP_FILL_COLOR:
        if (g_value_get_string (value))
            gdk_color_parse (g_value_get_string (value), &color);

        ecb->priv->rgba = ((color.red & 0xff00) << 16 |
                   (color.green & 0xff00) << 8 |
                   (color.blue & 0xff00) |
                   0xff);
        break;

    case PROP_FILL_COLOR_GDK:
        pcolor = g_value_get_boxed (value);
        if (pcolor) {
            color = *pcolor;
        }

        ecb->priv->rgba = ((color.red & 0xff00) << 16 |
                   (color.green & 0xff00) << 8 |
                   (color.blue & 0xff00) |
                   0xff);
        break;

    case PROP_FILL_COLOR_RGBA:
        ecb->priv->rgba = g_value_get_uint (value);
        break;

    }

    gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (ecb));
}

static void
ecb_get_property (GObject *object,
          guint prop_id,
          GValue *value,
          GParamSpec *pspec)
{
    ECanvasBackground *ecb;

    ecb = E_CANVAS_BACKGROUND (object);

    switch (prop_id) {
    case PROP_FILL_COLOR_RGBA:
        g_value_set_uint (value, ecb->priv->rgba);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
ecb_init (ECanvasBackground *ecb)
{
    ecb->priv               = g_new (ECanvasBackgroundPrivate, 1);
}

static void
ecb_draw (GnomeCanvasItem *item,
          cairo_t *cr,
          gint x,
          gint y,
          gint width,
          gint height)
{
    ECanvasBackground *ecb = E_CANVAS_BACKGROUND (item);

    cairo_save (cr);
    cairo_set_source_rgba (cr,
                   ((ecb->priv->rgba >> 24) & 0xff) / 255.0,
                   ((ecb->priv->rgba >> 16) & 0xff) / 255.0,
                   ((ecb->priv->rgba >>  8) & 0xff) / 255.0,
                   ( ecb->priv->rgba        & 0xff) / 255.0);
    cairo_paint (cr);
    cairo_restore (cr);
}

static GnomeCanvasItem *
ecb_point (GnomeCanvasItem *item, gdouble x, gdouble y, gint cx, gint cy)
{
    return item;
}

static void
ecb_style_set (ECanvasBackground *ecb,
               GtkStyle *previous_style)
{
    GnomeCanvasItem *item = GNOME_CANVAS_ITEM (ecb);

    if (gtk_widget_get_realized (GTK_WIDGET (item->canvas)))
        gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (ecb));
}

static void
ecb_class_init (ECanvasBackgroundClass *ecb_class)
{
    GnomeCanvasItemClass *item_class = GNOME_CANVAS_ITEM_CLASS (ecb_class);
    GObjectClass *object_class = G_OBJECT_CLASS (ecb_class);

    object_class->dispose       = ecb_dispose;
    object_class->set_property  = ecb_set_property;
    object_class->get_property  = ecb_get_property;

    item_class->update          = ecb_update;
    item_class->draw            = ecb_draw;
    item_class->point           = ecb_point;
    item_class->bounds          = ecb_bounds;

    ecb_class->style_set        = ecb_style_set;

    g_object_class_install_property (object_class, PROP_FILL_COLOR,
                     g_param_spec_string ("fill_color",
                                  "Fill color",
                                  "Fill color",
                                  NULL,
                                  G_PARAM_WRITABLE));

    g_object_class_install_property (object_class, PROP_FILL_COLOR_GDK,
                     g_param_spec_boxed ("fill_color_gdk",
                                 "GDK fill color",
                                 "GDK fill color",
                                 GDK_TYPE_COLOR,
                                 G_PARAM_WRITABLE));

    g_object_class_install_property (object_class, PROP_FILL_COLOR_RGBA,
                     g_param_spec_uint ("fill_color_rgba",
                                "GDK fill color",
                                "GDK fill color",
                                0, G_MAXUINT, 0,
                                G_PARAM_READWRITE));

    ecb_signals[STYLE_SET] =
        g_signal_new ("style_set",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (ECanvasBackgroundClass, style_set),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__OBJECT,
                  G_TYPE_NONE, 1, GTK_TYPE_STYLE);
}