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_fill_swatch_cell_renderer.c
23 * \brief A cell renderer for fill type swatches.
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)
56 class_init (GschemFillSwatchCellRendererClass
*klass
);
59 get_property (GObject
*object
,
65 instance_init (GschemFillSwatchCellRenderer
*swatch
);
68 render (GtkCellRenderer
*cell
,
71 GdkRectangle
*background_area
,
72 GdkRectangle
*cell_area
,
73 GdkRectangle
*expose_area
,
74 GtkCellRendererState flags
);
77 set_property (GObject
*object
,
84 /*! \brief Get/register GschemFillSwatchCellRenderer type.
87 gschem_fill_swatch_cell_renderer_get_type ()
89 static GType type
= 0;
92 static const GTypeInfo info
= {
93 sizeof (GschemFillSwatchCellRendererClass
),
95 NULL
, /* base_finalize */
96 (GClassInitFunc
) class_init
,
97 NULL
, /* class_finalize */
98 NULL
, /* class_data */
99 sizeof (GschemFillSwatchCellRenderer
),
101 (GInstanceInitFunc
) instance_init
,
104 type
= g_type_register_static (GTK_TYPE_CELL_RENDERER_TEXT
,
105 "GschemFillSwatchCellRenderer",
115 /*! \brief Create a new GschemFillSwatchCellRenderer
117 * \return The new cell renderer
120 gschem_fill_swatch_cell_renderer_new ()
122 return g_object_new (GSCHEM_TYPE_FILL_SWATCH_CELL_RENDERER
, NULL
);
128 * \brief Initialize swatch cell renderer class
130 * \param [in,out] klass The swatch cell renderer class
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
,
144 g_param_spec_boolean ("enabled",
150 g_object_class_install_property (object_class
,
152 g_param_spec_int ("fill-type",
164 * \brief Initialize fill swatch cell renderer instance
166 * \param [in,out] renderer The fill swatch cell renderer
169 instance_init (GschemFillSwatchCellRenderer
*swatch
)
171 swatch
->enabled
= TRUE
;
172 swatch
->fill_type
= FILLING_HOLLOW
;
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
186 get_property (GObject
*object
,
191 GschemFillSwatchCellRenderer
*swatch
= GSCHEM_FILL_SWATCH_CELL_RENDERER (object
);
195 g_value_set_boolean (value
, swatch
->enabled
);
199 g_value_set_int (value
, swatch
->fill_type
);
203 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
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
222 render (GtkCellRenderer
*cell
,
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
) {
234 cairo_t
*cr
= gdk_cairo_create (window
);
235 double offset
= SWATCH_BORDER_WIDTH
/ 2.0;
239 gdk_cairo_rectangle (cr
, expose_area
);
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
),
251 cairo_set_source_rgb (cr
,
253 color
.green
/ 65535.0,
254 color
.blue
/ 65535.0);
258 (double) cell_area
->x
+ offset
,
259 (double) cell_area
->y
+ offset
);
262 (double) cell_area
->x
+ (double) cell_area
->width
- offset
,
263 (double) cell_area
->y
+ offset
);
266 (double) cell_area
->x
+ (double) cell_area
->width
- offset
,
267 (double) cell_area
->y
+ (double) cell_area
->height
- offset
);
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
)) {
278 GArray
*lines
= g_array_new (FALSE
, FALSE
, sizeof (LINE
));
279 cairo_path_t
*save_path
= cairo_copy_path (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
);
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
);
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
333 set_property (GObject
*object
,
338 GschemFillSwatchCellRenderer
*swatch
= GSCHEM_FILL_SWATCH_CELL_RENDERER (object
);
342 swatch
->enabled
= g_value_get_boolean (value
);
346 swatch
->fill_type
= g_value_get_int (value
);
350 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);