aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-cell-renderer-color.c
blob: dad3992c0b4d4014cfbac4cc40e6a72c2bbdf735 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-cell-renderer-color.c
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * 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.
 *
 * 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 General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-cell-renderer-color.h"

#include <string.h>
#include <glib/gi18n-lib.h>

#define E_CELL_RENDERER_COLOR_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_CELL_RENDERER_COLOR, ECellRendererColorPrivate))

enum {
    PROP_0,
    PROP_COLOR
};

struct _ECellRendererColorPrivate {
    GdkColor *color;
};

G_DEFINE_TYPE (
    ECellRendererColor,
    e_cell_renderer_color,
    GTK_TYPE_CELL_RENDERER)

static void
cell_renderer_color_get_size (GtkCellRenderer *cell,
                              GtkWidget *widget,
                              const GdkRectangle *cell_area,
                              gint *x_offset,
                              gint *y_offset,
                              gint *width,
                              gint *height)
{
    gint color_width = 16;
    gint color_height = 16;
    gint calc_width;
    gint calc_height;
    gfloat xalign;
    gfloat yalign;
    guint xpad;
    guint ypad;

    g_object_get (
        cell, "xalign", &xalign, "yalign", &yalign,
        "xpad", &xpad, "ypad", &ypad, NULL);

    calc_width = (gint) xpad * 2 + color_width;
    calc_height = (gint) ypad * 2 + color_height;

    if (cell_area && color_width > 0 && color_height > 0) {
        if (x_offset) {
            *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
                    (1.0 - xalign) : xalign) *
                    (cell_area->width - calc_width));
            *x_offset = MAX (*x_offset, 0);
        }

        if (y_offset) {
            *y_offset =(yalign *
                (cell_area->height - calc_height));
            *y_offset = MAX (*y_offset, 0);
        }
    } else {
        if (x_offset) *x_offset = 0;
        if (y_offset) *y_offset = 0;
    }

    if (width)
        *width = calc_width;

    if (height)
        *height = calc_height;
}

static void
cell_renderer_color_render (GtkCellRenderer *cell,
                            cairo_t *cr,
                            GtkWidget *widget,
                            const GdkRectangle *background_area,
                            const GdkRectangle *cell_area,
                            GtkCellRendererState flags)
{
    ECellRendererColorPrivate *priv;
    GdkRectangle pix_rect;
    GdkRectangle draw_rect;
    GdkRGBA rgba;
    guint xpad;
    guint ypad;

    priv = E_CELL_RENDERER_COLOR_GET_PRIVATE (cell);

    if (priv->color == NULL)
        return;

    cell_renderer_color_get_size (
        cell, widget, cell_area,
        &pix_rect.x, &pix_rect.y,
        &pix_rect.width, &pix_rect.height);

    g_object_get (cell, "xpad", &xpad, "ypad", &ypad, NULL);

    pix_rect.x += cell_area->x + xpad;
    pix_rect.y += cell_area->y + ypad;
    pix_rect.width  -= xpad * 2;
    pix_rect.height -= ypad * 2;

    if (!gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect))
        return;

    rgba.red = priv->color->red / 65535.0;
    rgba.green = priv->color->green / 65535.0;
    rgba.blue = priv->color->blue / 65535.0;
    rgba.alpha = 1.0;

    gdk_cairo_set_source_rgba (cr, &rgba);
    cairo_rectangle (cr, pix_rect.x, pix_rect.y, draw_rect.width, draw_rect.height);

    cairo_fill (cr);
}

static void
cell_renderer_color_set_property (GObject *object,
                                  guint property_id,
                                  const GValue *value,
                                  GParamSpec *pspec)
{
    ECellRendererColorPrivate *priv;

    priv = E_CELL_RENDERER_COLOR_GET_PRIVATE (object);

    switch (property_id) {
        case PROP_COLOR:
            if (priv->color != NULL)
                gdk_color_free (priv->color);
            priv->color = g_value_dup_boxed (value);
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
cell_renderer_color_get_property (GObject *object,
                                  guint property_id,
                                  GValue *value,
                                  GParamSpec *pspec)
{
    ECellRendererColorPrivate *priv;

    priv = E_CELL_RENDERER_COLOR_GET_PRIVATE (object);

    switch (property_id) {
        case PROP_COLOR:
            g_value_set_boxed (value, priv->color);
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
cell_renderer_color_finalize (GObject *object)
{
    ECellRendererColorPrivate *priv;

    priv = E_CELL_RENDERER_COLOR_GET_PRIVATE (object);

    if (priv->color != NULL)
        gdk_color_free (priv->color);

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

static void
e_cell_renderer_color_class_init (ECellRendererColorClass *class)
{
    GObjectClass *object_class;
    GtkCellRendererClass *cell_class;

    g_type_class_add_private (class, sizeof (ECellRendererColorPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = cell_renderer_color_set_property;
    object_class->get_property = cell_renderer_color_get_property;
    object_class->finalize = cell_renderer_color_finalize;

    cell_class = GTK_CELL_RENDERER_CLASS (class);
    cell_class->get_size = cell_renderer_color_get_size;
    cell_class->render = cell_renderer_color_render;

    g_object_class_install_property (
        object_class,
        PROP_COLOR,
        g_param_spec_boxed (
            "color",
            "Color Info",
            "The color to render",
            GDK_TYPE_COLOR,
            G_PARAM_READWRITE));
}

static void
e_cell_renderer_color_init (ECellRendererColor *cellcolor)
{
    cellcolor->priv = E_CELL_RENDERER_COLOR_GET_PRIVATE (cellcolor);

    g_object_set (cellcolor, "xpad", 4, NULL);
}

/**
 * e_cell_renderer_color_new:
 *
 * Since: 2.22
 **/
GtkCellRenderer *
e_cell_renderer_color_new (void)
{
    return g_object_new (E_TYPE_CELL_RENDERER_COLOR, NULL);
}