refdes_renum: warn of possible number clash with non-conforming values
[geda-gaf/whiteaudio.git] / gschem / src / x_stroke.c
bloba38f28658b4e56ab65f6909b25e717c495826737
1 /* gEDA - GPL Electronic Design Automation
2 * gschem - gEDA Schematic Capture
3 * Copyright (C) 1998-2010 Ales Hvezda
4 * Copyright (C) 1998-2010 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 #include <config.h>
22 #include "gschem.h"
24 #ifdef HAVE_LIBSTROKE
25 #include <stroke.h>
27 #ifdef HAVE_LIBDMALLOC
28 #include <dmalloc.h>
29 #endif
32 * <B>stroke_points</B> is an array of points for the stroke
33 * footprints. The points of the stroke are displayed over the display
34 * area of the main window. They have to be erased when the stroke is
35 * translated and the sequence evaluated.
37 * Its size will never exceed <B>STROKE_MAX_POINTS</B> (the limit in
38 * number of points of a stroke provided by libstroke).
40 typedef struct {
41 gint x, y;
42 } StrokePoint;
44 static GArray *stroke_points = NULL;
47 /*! \brief Initializes the stroke interface.
48 * \par Function Description
49 * This is the initialization function for the stroke interface. It
50 * initializes the libstroke library and prepare an array of points
51 * for the mouse footprints.
53 * This function has to be called only once at application
54 * initialization before any use of the stroke interface.
56 void
57 x_stroke_init (void)
59 g_return_if_fail (stroke_points == NULL);
61 stroke_init ();
63 stroke_points = g_array_new (FALSE,
64 FALSE,
65 sizeof (StrokePoint));
68 /*! \brief Frees memory of the stroke interface.
69 * \par Function Description
70 * This function frees the memory used for the mouse footprint
71 * points. It terminates the use of the stroke interface.
73 void
74 x_stroke_free (void)
76 g_return_if_fail (stroke_points != NULL);
78 g_array_free (stroke_points, TRUE);
79 stroke_points = NULL;
82 /*! \brief Records a new point for the stroke.
83 * \par Function Description
84 * This function adds the point (<B>x</B>,<B>y</B>) as a new point in
85 * the stroke.
87 * The footprint is updated and the new point is drawn on the drawing area.
89 * \param [in] w_current The GSCHEM_TOPLEVEL object.
90 * \param [in] x The X coord of the new point.
91 * \param [in] Y The X coord of the new point.
93 void
94 x_stroke_record (GSCHEM_TOPLEVEL *w_current, gint x, gint y)
96 g_assert (stroke_points != NULL);
98 stroke_record (x, y);
100 if (stroke_points->len < STROKE_MAX_POINTS) {
101 StrokePoint point = { x, y };
103 g_array_append_val (stroke_points, point);
105 gdk_gc_set_foreground (w_current->gc, x_get_color (STROKE_COLOR));
106 gdk_draw_point (w_current->window, w_current->gc, x, y);
111 /*! \brief Evaluates the stroke.
112 * \par Function Description
113 * This function transforms the stroke input so far in an action.
115 * It makes use of the guile procedure <B>eval-stroke</B> to evaluate
116 * the stroke sequence into a possible action. The mouse footprint is
117 * erased in this function.
119 * It returns 1 if the stroke has been successfully evaluated as an
120 * action. It returns 0 if libstroke failed to transform the stroke
121 * or there is no action attached to the stroke.
123 * \param [in] w_current The GSCHEM_TOPLEVEL object.
124 * \returns 1 on success, 0 otherwise.
126 gint
127 x_stroke_translate_and_execute (GSCHEM_TOPLEVEL *w_current)
129 gchar sequence[STROKE_MAX_SEQUENCE];
130 StrokePoint *point;
131 int min_x, min_y, max_x, max_y;
132 gint i;
134 g_assert (stroke_points != NULL);
136 if (stroke_points->len == 0)
137 return 0;
139 point = &g_array_index (stroke_points, StrokePoint, 0);
140 min_x = max_x = point->x;
141 min_y = max_y = point->y;
143 for (i = 1; i < stroke_points->len; i++) {
144 point = &g_array_index (stroke_points, StrokePoint, i);
145 min_x = min (min_x, point->x);
146 min_y = min (min_y, point->y);
147 max_x = max (max_x, point->x);
148 max_y = max (max_y, point->y);
151 o_invalidate_rect (w_current, min_x, min_y, max_x + 1, max_y + 1);
153 /* resets length of array */
154 stroke_points->len = 0;
156 /* try evaluating stroke */
157 if (stroke_trans ((char*)&sequence)) {
158 gchar *guile_string =
159 g_strdup_printf("(eval-stroke \"%s\")", sequence);
160 SCM ret;
162 scm_dynwind_begin (0);
163 scm_dynwind_unwind_handler (g_free, guile_string, SCM_F_WIND_EXPLICITLY);
164 ret = g_scm_c_eval_string_protected (guile_string);
165 scm_dynwind_end ();
167 return (SCM_NFALSEP (ret));
170 return 0;
173 #endif /* HAVE_LIBSTROKE */