Remove extra debug message
[geda-gaf.git] / gschem / src / gschem_fill_swatch_cell_renderer.c
blobba722697e1858dee194397050bd25ff0fe548f9e
1 /* gEDA - GPL Electronic Design Automation
2 * gschem - gEDA Schematic Capture
3 * Copyright (C) 2013 Ales Hvezda
4 * Copyright (C) 2013-2019 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_fill_swatch_cell_renderer.c
23 * \brief A cell renderer for fill type swatches.
26 #include <config.h>
27 #include "gschem.h"
31 /* Specifies the width of the border around the swatch, in pixels (double).
33 #define SWATCH_BORDER_WIDTH (1.0)
36 /* Specifies the pitch of hatch and mesh lines, in pixels (integer).
38 #define SWATCH_LINE_PITCH (7)
41 /* Specifies the width of hatch and mesh lines, in pixels (double).
43 #define SWATCH_LINE_WIDTH (2.0)
46 enum
48 PROP_0,
49 PROP_ENABLED,
50 PROP_FILL_TYPE
55 static void
56 class_init (GschemFillSwatchCellRendererClass *klass);
58 static void
59 get_property (GObject *object,
60 guint param_id,
61 GValue *value,
62 GParamSpec *pspec);
64 static void
65 instance_init (GschemFillSwatchCellRenderer *swatch);
67 static void
68 render (GtkCellRenderer *cell,
69 GdkWindow *window,
70 GtkWidget *widget,
71 GdkRectangle *background_area,
72 GdkRectangle *cell_area,
73 GdkRectangle *expose_area,
74 GtkCellRendererState flags);
76 static void
77 set_property (GObject *object,
78 guint param_id,
79 const GValue *value,
80 GParamSpec *pspec);
84 /*! \brief Get/register GschemFillSwatchCellRenderer type.
86 GType
87 gschem_fill_swatch_cell_renderer_get_type ()
89 static GType type = 0;
91 if (type == 0) {
92 static const GTypeInfo info = {
93 sizeof (GschemFillSwatchCellRendererClass),
94 NULL, /* base_init */
95 NULL, /* base_finalize */
96 (GClassInitFunc) class_init,
97 NULL, /* class_finalize */
98 NULL, /* class_data */
99 sizeof (GschemFillSwatchCellRenderer),
100 0, /* n_preallocs */
101 (GInstanceInitFunc) instance_init,
104 type = g_type_register_static (GTK_TYPE_CELL_RENDERER_TEXT,
105 "GschemFillSwatchCellRenderer",
106 &info,
110 return type;
115 /*! \brief Create a new GschemFillSwatchCellRenderer
117 * \return The new cell renderer
119 GtkCellRenderer*
120 gschem_fill_swatch_cell_renderer_new ()
122 return g_object_new (GSCHEM_TYPE_FILL_SWATCH_CELL_RENDERER, NULL);
127 /*! \private
128 * \brief Initialize swatch cell renderer class
130 * \param [in,out] klass The swatch cell renderer class
132 static void
133 class_init (GschemFillSwatchCellRendererClass *klass)
135 GObjectClass *object_class = G_OBJECT_CLASS (klass);
137 klass->parent_class.parent_class.render = render;
139 object_class->get_property = get_property;
140 object_class->set_property = set_property;
142 g_object_class_install_property (object_class,
143 PROP_ENABLED,
144 g_param_spec_boolean ("enabled",
145 "Swatch Enabled",
146 "Swatch Enabled",
147 TRUE,
148 G_PARAM_READWRITE));
150 g_object_class_install_property (object_class,
151 PROP_FILL_TYPE,
152 g_param_spec_int ("fill-type",
153 "Fill type",
154 "Fill Type",
157 FILLING_HOLLOW,
158 G_PARAM_READWRITE));
163 /*! \private
164 * \brief Initialize fill swatch cell renderer instance
166 * \param [in,out] renderer The fill swatch cell renderer
168 static void
169 instance_init (GschemFillSwatchCellRenderer *swatch)
171 swatch->enabled = TRUE;
172 swatch->fill_type = FILLING_HOLLOW;
177 /*! \private
178 * \brief Get a property.
180 * \param [in] object The object with the property
181 * \param [in] param_id The id of the property
182 * \param [out] value The value of the property
183 * \param [in] pspec The property param spec
185 static void
186 get_property (GObject *object,
187 guint param_id,
188 GValue *value,
189 GParamSpec *pspec)
191 GschemFillSwatchCellRenderer *swatch = GSCHEM_FILL_SWATCH_CELL_RENDERER (object);
193 switch (param_id) {
194 case PROP_ENABLED:
195 g_value_set_boolean (value, swatch->enabled);
196 break;
198 case PROP_FILL_TYPE:
199 g_value_set_int (value, swatch->fill_type);
200 break;
202 default:
203 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
204 break;
210 /*! \private
211 * \brief Render the swatch into the cell
213 * \param [in] cell this cell renderer
214 * \param [in] window the window to renter to
215 * \param [in] widget the widget owning the window
216 * \param [in] background_area entire cell area
217 * \param [in] cell_area area rendered normally
218 * \param [in] expose_area the area requiring update
219 * \param [in] flags
221 static void
222 render (GtkCellRenderer *cell,
223 GdkWindow *window,
224 GtkWidget *widget,
225 GdkRectangle *background_area,
226 GdkRectangle *cell_area,
227 GdkRectangle *expose_area,
228 GtkCellRendererState flags)
230 GschemFillSwatchCellRenderer *swatch = GSCHEM_FILL_SWATCH_CELL_RENDERER (cell);
232 if (swatch->enabled) {
233 GdkColor color;
234 cairo_t *cr = gdk_cairo_create (window);
235 double offset = SWATCH_BORDER_WIDTH / 2.0;
236 gboolean success;
238 if (expose_area) {
239 gdk_cairo_rectangle (cr, expose_area);
240 cairo_clip (cr);
243 /* Paint the swatch using the text color to match the user's desktop theme.
246 success = gtk_style_lookup_color (gtk_widget_get_style (widget),
247 "text_color",
248 &color);
250 if (success) {
251 cairo_set_source_rgb (cr,
252 color.red / 65535.0,
253 color.green / 65535.0,
254 color.blue / 65535.0);
257 cairo_move_to (cr,
258 (double) cell_area->x + offset,
259 (double) cell_area->y + offset);
261 cairo_line_to (cr,
262 (double) cell_area->x + (double) cell_area->width - offset,
263 (double) cell_area->y + offset);
265 cairo_line_to (cr,
266 (double) cell_area->x + (double) cell_area->width - offset,
267 (double) cell_area->y + (double) cell_area->height - offset);
269 cairo_line_to (cr,
270 (double) cell_area->x + offset,
271 (double) cell_area->y + (double) cell_area->height - offset);
273 cairo_close_path (cr);
275 if ((swatch->fill_type == FILLING_HATCH) || (swatch->fill_type == FILLING_MESH)) {
276 BOX box;
277 int index;
278 GArray *lines = g_array_new (FALSE, FALSE, sizeof (LINE));
279 cairo_path_t *save_path = cairo_copy_path (cr);
281 cairo_save (cr);
282 cairo_clip (cr);
284 box.lower_x = cell_area->x;
285 box.lower_y = cell_area->y;
286 box.upper_x = cell_area->x + cell_area->width;
287 box.upper_y = cell_area->y + cell_area->height;
289 m_hatch_box (&box, 135, SWATCH_LINE_PITCH, lines);
291 if (swatch->fill_type == FILLING_MESH) {
292 m_hatch_box (&box, 45, SWATCH_LINE_PITCH, lines);
295 for (index=0; index<lines->len; index++) {
296 LINE *line = &g_array_index (lines, LINE, index);
298 cairo_move_to (cr, line->x[0], line->y[0]);
299 cairo_line_to (cr, line->x[1], line->y[1]);
302 g_array_free (lines, TRUE);
304 cairo_set_line_width (cr, SWATCH_LINE_WIDTH);
305 cairo_stroke (cr);
306 cairo_restore (cr);
308 cairo_append_path (cr, save_path);
309 cairo_path_destroy (save_path);
312 if (swatch->fill_type == FILLING_FILL) {
313 cairo_fill_preserve (cr);
316 cairo_set_line_width (cr, SWATCH_BORDER_WIDTH);
317 cairo_stroke (cr);
318 cairo_destroy (cr);
324 /*! \private
325 * \brief Set a property.
327 * \param [in,out] object The object with the property
328 * \param [in] param_id The id of the property
329 * \param [in] value The value of the property
330 * \param [in] pspec The property param spec
332 static void
333 set_property (GObject *object,
334 guint param_id,
335 const GValue *value,
336 GParamSpec *pspec)
338 GschemFillSwatchCellRenderer *swatch = GSCHEM_FILL_SWATCH_CELL_RENDERER (object);
340 switch (param_id) {
341 case PROP_ENABLED:
342 swatch->enabled = g_value_get_boolean (value);
343 break;
345 case PROP_FILL_TYPE:
346 swatch->fill_type = g_value_get_int (value);
347 break;
349 default:
350 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
351 break;