1 /* gEDA - GPL Electronic Design Automation
2 * gschem - gEDA Schematic Capture
3 * Copyright (C) 2013 Ales Hvezda
4 * Copyright (C) 2013-2020 gEDA Contributors (see ChangeLog for details)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * \file gschem_swatch_column_renderer.c
23 * \brief A cell renderer for color swatches.
40 /* The width of the border around the swatch, in pixels.
42 #define SWATCH_BORDER_WIDTH (1.0)
54 get_property (GObject
*object
,
60 set_property (GObject
*object
,
66 render (GtkCellRenderer
*cell
,
69 GdkRectangle
*background_area
,
70 GdkRectangle
*cell_area
,
71 GdkRectangle
*expose_area
,
72 GtkCellRendererState flags
);
77 * \brief Get a property.
79 * \brief [in] object The object with the property
80 * \brief [in] param_id The id of the property
81 * \brief [out] value The value of the property
82 * \brief [in] pspec The property param spec
85 get_property (GObject
*object
,
90 GschemSwatchColumnRenderer
*swatch
= GSCHEM_SWATCH_COLUMN_RENDERER (object
);
96 color
.red
= swatch
->color
.red
;
97 color
.green
= swatch
->color
.green
;
98 color
.blue
= swatch
->color
.blue
;
100 g_value_set_boxed (value
, &color
);
105 g_value_set_boolean (value
, swatch
->enabled
);
109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
116 /*! \brief Initialize swatch cell renderer class
118 * \param [in,out] klass The swatch cell renderer class
121 swatchcr_class_init (GschemSwatchColumnRendererClass
*klass
)
123 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
125 klass
->parent_class
.parent_class
.render
= render
;
127 object_class
->get_property
= get_property
;
128 object_class
->set_property
= set_property
;
130 g_object_class_install_property (object_class
,
132 g_param_spec_boxed ("color",
137 g_object_class_install_property (object_class
,
139 g_param_spec_boolean ("enabled",
148 /*! \brief Initialize Swatchcr instance
150 * \param [in,out] swatch The swatch cell renderer
153 swatchcr_init (GschemSwatchColumnRenderer
*swatch
)
155 swatch
->color
.red
= 0;
156 swatch
->color
.green
= 0;
157 swatch
->color
.blue
= 0;
159 swatch
->enabled
= TRUE
;
165 /*! \brief Render the swatch into the cell
170 * \param [in] background_area
171 * \param [in] cell_area
172 * \param [in] expose_area
176 render (GtkCellRenderer
*cell
,
179 GdkRectangle
*background_area
,
180 GdkRectangle
*cell_area
,
181 GdkRectangle
*expose_area
,
182 GtkCellRendererState flags
)
184 GschemSwatchColumnRenderer
*swatch
= GSCHEM_SWATCH_COLUMN_RENDERER (cell
);
186 if (swatch
->enabled
) {
187 cairo_t
*cr
= gdk_cairo_create (window
);
188 double offset
= SWATCH_BORDER_WIDTH
/ 2.0;
191 gdk_cairo_rectangle (cr
, expose_area
);
196 (double) cell_area
->x
+ offset
,
197 (double) cell_area
->y
+ offset
);
200 (double) cell_area
->x
+ (double) cell_area
->width
- offset
,
201 (double) cell_area
->y
+ offset
);
204 (double) cell_area
->x
+ (double) cell_area
->width
- offset
,
205 (double) cell_area
->y
+ (double) cell_area
->height
- offset
);
208 (double) cell_area
->x
+ offset
,
209 (double) cell_area
->y
+ (double) cell_area
->height
- offset
);
211 cairo_close_path (cr
);
213 cairo_set_line_width (cr
, SWATCH_BORDER_WIDTH
);
215 cairo_set_source_rgb (cr
,
216 swatch
->color
.red
/ 65535.0,
217 swatch
->color
.green
/ 65535.0,
218 swatch
->color
.blue
/ 65535.0);
220 cairo_fill_preserve (cr
);
222 cairo_set_source_rgb (cr
, 0.0, 0.0, 0.0);
233 * \brief Set the swatch color.
235 * \param [in,out] swatch The swatch cell renderer
236 * \param [in] color The color of the swatch
239 set_color (GschemSwatchColumnRenderer
*swatch
, const GdkColor
*color
)
242 swatch
->color
.red
= color
->red
;
243 swatch
->color
.green
= color
->green
;
244 swatch
->color
.blue
= color
->blue
;
251 * \brief Set a property.
253 * \brief [in,out] object The object with the property
254 * \brief [in] param_id The id of the property
255 * \brief [in] value The value of the property
256 * \brief [in] pspec The property param spec
259 set_property (GObject
*object
,
264 GschemSwatchColumnRenderer
*swatch
= GSCHEM_SWATCH_COLUMN_RENDERER (object
);
268 set_color (swatch
, g_value_get_boxed (value
));
272 swatch
->enabled
= g_value_get_boolean (value
);
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
283 /*! \brief Get/register Swatchcr type.
286 gschem_swatch_column_renderer_get_type()
288 static GType type
= 0;
291 static const GTypeInfo info
= {
292 sizeof(GschemSwatchColumnRendererClass
),
293 NULL
, /* base_init */
294 NULL
, /* base_finalize */
295 (GClassInitFunc
) swatchcr_class_init
,
296 NULL
, /* class_finalize */
297 NULL
, /* class_data */
298 sizeof(GschemSwatchColumnRenderer
),
300 (GInstanceInitFunc
) swatchcr_init
,
303 type
= g_type_register_static (GTK_TYPE_CELL_RENDERER_TEXT
, "GschemSwatchColumnRenderer", &info
, 0);
311 /*! \brief Create a swatch cell renderer
313 GschemSwatchColumnRenderer
*
314 gschem_swatch_column_renderer_new()
316 GschemSwatchColumnRenderer
*swatch
= GSCHEM_SWATCH_COLUMN_RENDERER (g_object_new (GSCHEM_TYPE_SWATCH_COLUMN_RENDERER
, NULL
));