Cleaned up and consolidated code which creates new pages.
[geda-gaf.git] / gschem / src / o_buffer.c
blob01198ae4dd3ae74dbf0b1ab71720ccd2d7f92bb7
1 /* gEDA - GPL Electronic Design Automation
2 * gschem - gEDA Schematic Capture
3 * Copyright (C) 1998-2000 Ales V. Hvezda
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
19 #include <config.h>
20 #include <stdio.h>
22 #include <libgeda/libgeda.h>
24 #include "../include/globals.h"
25 #include "../include/prototype.h"
27 #ifdef HAVE_LIBDMALLOC
28 #include <dmalloc.h>
29 #endif
31 /*! \todo Finish function documentation!!!
32 * \brief
33 * \par Function Description
36 void o_buffer_copy(TOPLEVEL *w_current, int buf_num)
38 GList *s_current = NULL;
40 if (buf_num < 0 || buf_num > MAX_BUFFERS) {
41 fprintf(stderr, _("Got an invalid buffer_number [o_buffer_copy]\n"));
42 return;
45 s_current = w_current->page_current->selection_list;
47 if (object_buffer[buf_num] == NULL) {
48 object_buffer[buf_num] = s_basic_init_object("buffer0_head");
49 object_buffer[buf_num]->type = OBJ_HEAD;
50 } else {
51 o_list_delete_rest(w_current, object_buffer[buf_num]);
52 object_buffer[buf_num]->next = NULL;
55 w_current->ADDING_SEL = 1;
56 o_list_copy_all_selection2(w_current, s_current,
57 object_buffer[buf_num], SELECTION_FLAG);
58 w_current->ADDING_SEL = 0;
60 #if DEBUG
61 o_current = object_buffer[buf_num];
62 while(o_current != NULL) {
63 printf("- %s\n", o_current->name);
64 o_current = o_current->next;
66 #endif
69 /*! \todo Finish function documentation!!!
70 * \brief
71 * \par Function Description
74 void o_buffer_cut(TOPLEVEL *w_current, int buf_num)
76 GList *s_current = NULL;
78 if (buf_num < 0 || buf_num > MAX_BUFFERS) {
79 fprintf(stderr, _("Got an invalid buffer_number [o_buffer_cut]\n"));
80 return;
83 s_current = w_current->page_current->selection_list;
85 if (object_buffer[buf_num] == NULL) {
86 object_buffer[buf_num] = s_basic_init_object("buffer0_head");
87 object_buffer[buf_num]->type = OBJ_HEAD;
88 } else {
89 o_list_delete_rest(w_current, object_buffer[buf_num]);
90 object_buffer[buf_num]->next = NULL;
93 w_current->ADDING_SEL = 1;
94 o_list_copy_all_selection2(w_current, s_current,
95 object_buffer[buf_num], SELECTION_FLAG);
96 w_current->ADDING_SEL = 0;
97 o_delete(w_current);
99 #if DEBUG
100 o_current = object_buffer[buf_num];
101 while(o_current != NULL) {
102 printf("- %s\n", o_current->name);
103 o_current = o_current->next;
105 #endif
108 /*! \todo Finish function documentation!!!
109 * \brief
110 * \par Function Description
113 void o_buffer_paste_start(TOPLEVEL *w_current, int screen_x, int screen_y,
114 int buf_num)
116 int rleft, rtop, rbottom, rright;
117 int x, y;
119 if (buf_num < 0 || buf_num > MAX_BUFFERS) {
120 fprintf(stderr, _("Got an invalid buffer_number [o_buffer_paste_start]\n"));
121 return;
124 if (!world_get_object_list_bounds(w_current, object_buffer[buf_num],
125 &rleft, &rtop,
126 &rright, &rbottom)) {
127 /* If the paste buffer doesn't have any objects
128 * to define its any bounds, we drop out here */
129 return;
132 /* snap x and y to the grid, pointed out by Martin Benes */
133 x = snap_grid(w_current, rleft);
134 y = snap_grid(w_current, rtop);
136 w_current->ADDING_SEL = 1;
137 o_complex_world_translate(w_current, -x, -y, object_buffer[buf_num]);
138 w_current->ADDING_SEL = 0;
140 /* now translate selection to current position */
141 SCREENtoWORLD(w_current, screen_x, screen_y, &x, &y);
142 x = snap_grid(w_current, x);
143 y = snap_grid(w_current, y);
145 w_current->ADDING_SEL = 1;
146 o_complex_world_translate(w_current, x, y, object_buffer[buf_num]);
147 w_current->ADDING_SEL = 0;
149 w_current->last_x = w_current->start_x = fix_x(w_current, screen_x);
150 w_current->last_y = w_current->start_y = fix_y(w_current, screen_y);
151 w_current->event_state = ENDPASTE;
153 /* store the buffer number for future use */
154 w_current->buffer_number = buf_num;
156 o_drawbounding(w_current,
157 object_buffer[buf_num]->next,
158 NULL,
159 x_get_darkcolor(w_current->bb_color), TRUE);
162 /*! \todo Finish function documentation!!!
163 * \brief
164 * \par Function Description
167 void o_buffer_paste_end(TOPLEVEL *w_current, int screen_x, int screen_y,
168 int buf_num)
170 int w_x, w_y;
171 int w_start_x, w_start_y;
172 int w_diff_x, w_diff_y;
173 OBJECT *o_current;
174 OBJECT *o_saved;
175 GList *temp_list;
176 PAGE *p_current;
177 GList *connected_objects = NULL;
179 if (buf_num < 0 || buf_num > MAX_BUFFERS) {
180 fprintf(stderr, _("Got an invalid buffer_number [o_buffer_paste_end]\n"));
181 return;
184 /* erase old image */
185 o_drawbounding(w_current,
186 object_buffer[buf_num]->next,
187 NULL,
188 x_get_darkcolor(w_current->bb_color), FALSE);
190 /* get the location where we ended */
191 SCREENtoWORLD(w_current, screen_x, screen_y, &w_x, &w_y);
192 SCREENtoWORLD(w_current, w_current->start_x, w_current->start_y,
193 &w_start_x, &w_start_y);
194 w_x = snap_grid(w_current, w_x);
195 w_y = snap_grid(w_current, w_y);
196 w_start_x = snap_grid(w_current, w_start_x);
197 w_start_y = snap_grid(w_current, w_start_y);
199 #if DEBUG
200 printf("%d %d\n", w_x - w_start_x, w_y - w_start_y);
201 #endif
202 /* calc and translate objects to their final position */
203 w_diff_x = w_x - w_start_x;
204 w_diff_y = w_y - w_start_y;
205 w_current->ADDING_SEL = 1;
206 o_complex_world_translate(w_current, w_diff_x, w_diff_y,
207 object_buffer[buf_num]);
208 w_current->ADDING_SEL = 0;
210 o_current = object_buffer[buf_num]->next;
211 p_current = w_current->page_current;
213 o_saved = p_current->object_tail;
214 o_list_copy_all(w_current, o_current, p_current->object_tail,
215 NORMAL_FLAG);
217 p_current->object_tail = return_tail(p_current->object_head);
218 o_current = o_saved->next;
219 temp_list = NULL;
221 /* now add new objects to the selection list */
222 while (o_current != NULL) {
223 o_selection_add(&temp_list, o_current);
224 s_conn_update_object(w_current, o_current);
225 if (o_current->type == OBJ_COMPLEX || o_current->type == OBJ_PLACEHOLDER) {
226 connected_objects = s_conn_return_complex_others(
227 connected_objects,
228 o_current);
229 } else {
230 connected_objects = s_conn_return_others(connected_objects,
231 o_current);
233 o_current = o_current->next;
236 o_cue_redraw_all(w_current, o_saved->next, TRUE);
237 o_cue_undraw_list(w_current, connected_objects);
238 o_cue_draw_list(w_current, connected_objects);
239 g_list_free(connected_objects);
240 connected_objects = NULL;
242 o_select_run_hooks(w_current, NULL, 2);
244 o_selection_unselect_list(w_current,
245 &(w_current->page_current->selection_list));
246 w_current->page_current->selection_list = temp_list;
248 w_current->page_current->CHANGED = 1;
249 o_redraw(w_current, o_saved->next, TRUE); /* only redraw new objects */
250 o_undo_savestate(w_current, UNDO_ALL);
251 i_update_menus(w_current);
254 /*! \todo Finish function documentation!!!
255 * \brief
256 * \par Function Description
259 void o_buffer_paste_rubberpaste(TOPLEVEL *w_current, int buf_num)
261 o_drawbounding(w_current,
262 object_buffer[buf_num]->next,
263 NULL,
264 x_get_darkcolor(w_current->bb_color), FALSE);
267 /*! \todo Finish function documentation!!!
268 * \brief
269 * \par Function Description
272 void o_buffer_init(void)
274 int i;
276 for (i = 0 ; i < MAX_BUFFERS; i++) {
277 object_buffer[i] = NULL;
281 /*! \todo Finish function documentation!!!
282 * \brief
283 * \par Function Description
286 void o_buffer_free(TOPLEVEL *w_current)
288 int i;
290 for (i = 0 ; i < MAX_BUFFERS; i++) {
291 if (object_buffer[i]) {
292 s_delete_list_fromstart(w_current,
293 object_buffer[i]);
294 object_buffer[i] = NULL;