missing NULL terminator in set_config_x
[geda-gaf.git] / gschem / src / gschem_swatch_column_renderer.c
blob1082522197fc251741e118f54017192baea3e2f8
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
20 /*!
21 * \file gschem_swatch_column_renderer.c
23 * \brief A cell renderer for color swatches.
26 #include <config.h>
28 #include <stdio.h>
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
32 #ifdef HAVE_STRING_H
33 #include <string.h>
34 #endif
36 #include "gschem.h"
40 /* The width of the border around the swatch, in pixels.
42 #define SWATCH_BORDER_WIDTH (1.0)
45 enum
47 PROP_COLOR = 1,
48 PROP_ENABLED
53 static void
54 get_property (GObject *object,
55 guint param_id,
56 GValue *value,
57 GParamSpec *pspec);
59 static void
60 set_property (GObject *object,
61 guint param_id,
62 const GValue *value,
63 GParamSpec *pspec);
65 static void
66 render (GtkCellRenderer *cell,
67 GdkWindow *window,
68 GtkWidget *widget,
69 GdkRectangle *background_area,
70 GdkRectangle *cell_area,
71 GdkRectangle *expose_area,
72 GtkCellRendererState flags);
76 /*! \private
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
84 static void
85 get_property (GObject *object,
86 guint param_id,
87 GValue *value,
88 GParamSpec *pspec)
90 GschemSwatchColumnRenderer *swatch = GSCHEM_SWATCH_COLUMN_RENDERER (object);
92 switch (param_id) {
93 case PROP_COLOR: {
94 GdkColor color;
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);
102 break;
104 case PROP_ENABLED:
105 g_value_set_boolean (value, swatch->enabled);
106 break;
108 default:
109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
110 break;
116 /*! \brief Initialize swatch cell renderer class
118 * \param [in,out] klass The swatch cell renderer class
120 static void
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,
131 PROP_COLOR,
132 g_param_spec_boxed ("color",
133 "Swatch Color",
134 "Swatch Color",
135 GDK_TYPE_COLOR,
136 G_PARAM_READWRITE));
137 g_object_class_install_property (object_class,
138 PROP_ENABLED,
139 g_param_spec_boolean ("enabled",
140 "Swatch Enabled",
141 "Swatch Enabled",
142 TRUE,
143 G_PARAM_READWRITE));
148 /*! \brief Initialize Swatchcr instance
150 * \param [in,out] swatch The swatch cell renderer
152 static void
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
167 * \param [in] cell
168 * \param [in] window
169 * \param [in] widget
170 * \param [in] background_area
171 * \param [in] cell_area
172 * \param [in] expose_area
173 * \param [in] flags
175 static void
176 render (GtkCellRenderer *cell,
177 GdkWindow *window,
178 GtkWidget *widget,
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;
190 if (expose_area) {
191 gdk_cairo_rectangle (cr, expose_area);
192 cairo_clip (cr);
195 cairo_move_to (cr,
196 (double) cell_area->x + offset,
197 (double) cell_area->y + offset);
199 cairo_line_to (cr,
200 (double) cell_area->x + (double) cell_area->width - offset,
201 (double) cell_area->y + offset);
203 cairo_line_to (cr,
204 (double) cell_area->x + (double) cell_area->width - offset,
205 (double) cell_area->y + (double) cell_area->height - offset);
207 cairo_line_to (cr,
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);
224 cairo_stroke (cr);
226 cairo_destroy (cr);
232 /*! \private
233 * \brief Set the swatch color.
235 * \param [in,out] swatch The swatch cell renderer
236 * \param [in] color The color of the swatch
238 static void
239 set_color (GschemSwatchColumnRenderer *swatch, const GdkColor *color)
241 if (color) {
242 swatch->color.red = color->red;
243 swatch->color.green = color->green;
244 swatch->color.blue = color->blue;
250 /*! \private
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
258 static void
259 set_property (GObject *object,
260 guint param_id,
261 const GValue *value,
262 GParamSpec *pspec)
264 GschemSwatchColumnRenderer *swatch = GSCHEM_SWATCH_COLUMN_RENDERER (object);
266 switch (param_id) {
267 case PROP_COLOR:
268 set_color (swatch, g_value_get_boxed (value));
269 break;
271 case PROP_ENABLED:
272 swatch->enabled = g_value_get_boolean (value);
273 break;
275 default:
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
277 break;
283 /*! \brief Get/register Swatchcr type.
285 GType
286 gschem_swatch_column_renderer_get_type()
288 static GType type = 0;
290 if (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),
299 0, /* n_preallocs */
300 (GInstanceInitFunc) swatchcr_init,
303 type = g_type_register_static (GTK_TYPE_CELL_RENDERER_TEXT, "GschemSwatchColumnRenderer", &info, 0);
306 return type;
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));
318 return swatch;