Prefer o_text_change over random poking.
[geda-gaf/berndj.git] / gschem / src / o_attrib.c
blobce2e8a408e566293d57191b1683028f9cd6428a9
1 /* gEDA - GPL Electronic Design Automation
2 * gschem - gEDA Schematic Capture
3 * Copyright (C) 1998-2007 Ales Hvezda
4 * Copyright (C) 1998-2007 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., 59 Temple Place, Suite 330, Boston, MA 02111 USA
20 #include <config.h>
22 #include <stdio.h>
23 #ifdef HAVE_STRING_H
24 #include <string.h>
25 #endif
26 #include <math.h>
28 #include "gschem.h"
30 #ifdef HAVE_LIBDMALLOC
31 #include <dmalloc.h>
32 #endif
34 /* No special type for attributes */
35 /* You can only edit text attributes */
37 /* be sure in o_copy o_move o_delete you maintain the attributes */
38 /* delete is a bare, because you will have to unattach the other end */
39 /* and in o_save o_read as well */
40 /* and in o_select when selecting objects, select the attributes */
42 /* there needs to be a modifier (in struct.h, such as a flag) which
43 * signifies that this is an attribute */
45 /*! \todo Finish function documentation!!!
46 * \brief
47 * \par Function Description
48 * Copy all attributes select to the selection list.
50 * \todo get a better name
52 void o_attrib_add_selected(GSCHEM_TOPLEVEL *w_current, SELECTION *selection,
53 OBJECT *selected)
55 OBJECT *a_current;
56 GList *a_iter;
58 g_assert( selection != NULL );
60 /* deal with attributes here? */
61 if (selected->attribs != NULL) {
62 /* first node is head */
63 a_iter = selected->attribs;
65 while (a_iter != NULL) {
66 a_current = a_iter->data;
68 /* make sure object isn't selected already */
69 if (a_current->saved_color == -1) {
70 o_selection_add(selection, a_current);
71 o_redraw_single(w_current, a_current);
74 a_iter = g_list_next (a_iter);
78 return;
81 /*! \brief Change visibility status of attribute object.
82 * \par Function Description
83 * This function toggles the visibility status of the attribute \a
84 * object and updates it. The object is erased or redrawn if
85 * necessary.
87 * \param [in] w_current The GSCHEM_TOPLEVEL object.
88 * \param [in] object The attribute object.
90 void o_attrib_toggle_visibility(GSCHEM_TOPLEVEL *w_current, OBJECT *object)
92 TOPLEVEL *toplevel = w_current->toplevel;
94 g_return_if_fail (object != NULL && object->type == OBJ_TEXT);
96 if (object->visibility == VISIBLE) {
97 /* only erase if we are not showing hidden text */
98 if (!object->show_hidden) {
99 o_erase_single(w_current, object);
102 o_text_change(object, NULL, INVISIBLE, LEAVE_NAME_VALUE_ALONE);
104 if (object->show_hidden) {
105 /* draw text so that little I is drawn */
106 o_text_draw(w_current, object);
108 } else {
109 /* if we are in the special show hidden mode, then erase text first */
110 /* to get rid of the little I */
111 if (object->show_hidden) {
112 o_erase_single(w_current, object);
115 o_text_change(object, NULL, VISIBLE, LEAVE_NAME_VALUE_ALONE);
116 o_text_recreate(object);
117 o_text_draw(w_current, object);
120 toplevel->page_current->CHANGED = 1;
123 /*! \brief Set what part of an attribute is shown.
124 * \par Function Description
125 * This function changes what part (name, value or both) of an
126 * attribute is shown by its attribute object. The attribute object
127 * is erased, updated and finally redrawn.
129 * \param [in] w_current The GSCHEM_TOPLEVEL object.
130 * \param [in] object The attribute object.
131 * \param [in] show_name_value The new display flag for attribute.
133 void o_attrib_toggle_show_name_value(GSCHEM_TOPLEVEL *w_current,
134 OBJECT *object, int show_name_value)
136 TOPLEVEL *toplevel = w_current->toplevel;
138 g_return_if_fail (object != NULL && object->type == OBJ_TEXT);
140 o_erase_single(w_current, object);
141 o_text_change(object, NULL, LEAVE_VISIBILITY_ALONE, show_name_value);
142 o_text_recreate(object);
143 o_text_draw(w_current, object);
145 toplevel->page_current->CHANGED = 1;
149 /*! \todo Finish function documentation!!!
150 * \brief
151 * \par Function Description
154 /* This function no longer returns NULL, but will always return the new */
155 /* text item */
156 OBJECT *o_attrib_add_attrib(GSCHEM_TOPLEVEL *w_current,
157 const char *text_string, int visibility,
158 int show_name_value, OBJECT *object)
160 TOPLEVEL *toplevel = w_current->toplevel;
161 OBJECT *new_obj;
162 int world_x = - 1, world_y = -1;
163 int color;
164 int left, right, top, bottom;
165 OBJECT *o_current;
167 color = toplevel->detachedattr_color;
169 o_current = object;
171 /* creating a toplevel or unattached attribute */
172 if (o_current) {
173 /* get coordinates of where to place the text object */
174 switch(o_current->type) {
175 int w_x1, w_y1, w_x2, w_y2;
177 case(OBJ_COMPLEX):
178 case(OBJ_PLACEHOLDER):
179 world_x = o_current->complex->x;
180 world_y = o_current->complex->y;
181 color = toplevel->attribute_color;
182 break;
184 case(OBJ_ARC):
185 s_basic_get_grip(o_current, GRIP_ARC_CENTER, &world_x, &world_y);
186 color = toplevel->attribute_color;
187 break;
189 case(OBJ_CIRCLE):
190 s_basic_get_grip(o_current, GRIP_CIRCLE_CENTER, &world_x, &world_y);
191 color = toplevel->attribute_color;
192 break;
194 case(OBJ_BOX):
195 s_basic_get_grip(o_current, GRIP_UPPER_LEFT, &w_x1, &w_y1);
196 s_basic_get_grip(o_current, GRIP_LOWER_RIGHT, &w_x2, &w_y2);
197 world_x = min(w_x1, w_x2);
198 world_y = max(w_y1, w_y2);
199 color = toplevel->attribute_color;
200 break;
202 case(OBJ_LINE):
203 case(OBJ_NET):
204 case(OBJ_PIN):
205 case(OBJ_BUS):
206 s_basic_get_grip(o_current, GRIP_1, &world_x, &world_y);
207 color = toplevel->attribute_color;
208 break;
210 case(OBJ_TEXT):
211 o_text_get_xy(o_current, &world_x, &world_y);
213 color = toplevel->detachedattr_color;
215 o_current = NULL;
216 break;
218 } else {
219 world_get_object_list_bounds(toplevel->page_current->object_head, LIST_KIND_HEAD,
220 &left, &top, &right, &bottom);
222 /* this really is the lower left hand corner */
223 world_x = left;
224 world_y = top;
226 /* printf("%d %d\n", world_x, world_y); */
227 color = toplevel->detachedattr_color;
230 /* first create text item */
231 new_obj = o_text_new(toplevel, OBJ_TEXT, color, world_x, world_y,
232 LOWER_LEFT, 0, /* zero is angle */
233 text_string, w_current->text_size, /* current text size */
234 visibility, show_name_value);
235 s_page_append(toplevel->page_current, new_obj);
237 /* now toplevel->page_current->object_tail contains new text item */
239 /* now attach the attribute to the object (if o_current is not NULL) */
240 /* remember that o_current contains the object to get the attribute */
241 if (o_current) {
242 o_attrib_attach (toplevel, toplevel->page_current->object_tail, o_current);
245 o_selection_add( toplevel->page_current->selection_list,
246 toplevel->page_current->object_tail );
248 o_erase_single(w_current, toplevel->page_current->object_tail);
249 o_text_draw(w_current, toplevel->page_current->object_tail);
251 /* handle slot= attribute, it's a special case */
252 if (g_ascii_strncasecmp (text_string, "slot=", 5) == 0) {
253 o_slot_end (w_current, text_string, strlen (text_string));
256 /* Run the add attribute hook */
257 if (scm_hook_empty_p(add_attribute_hook) == SCM_BOOL_F &&
258 o_current != NULL) {
259 scm_run_hook(add_attribute_hook,
260 scm_cons(g_make_object_smob(toplevel,
261 o_current),
262 SCM_EOL));
265 toplevel->page_current->CHANGED = 1;
267 return(toplevel->page_current->object_tail);