One more check on valid display which is known to be in the startup
[xcircuit.git] / libraries.c
bloba7dc41a6f589ed2956fdbaad1f443529988a1cc5
1 /*-------------------------------------------------------------------------*/
2 /* libraries.c --- xcircuit routines for the builtin and user libraries */
3 /* Copyright (c) 2002 Tim Edwards, Johns Hopkins University */
4 /*-------------------------------------------------------------------------*/
6 /*-------------------------------------------------------------------------*/
7 /* written by Tim Edwards, 8/13/93 */
8 /*-------------------------------------------------------------------------*/
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
14 #ifndef XC_WIN32
15 #include <X11/Intrinsic.h>
16 #include <X11/StringDefs.h>
17 #endif
18 #ifdef TCL_WRAPPER
19 #include <tk.h>
20 #else
21 #ifndef XC_WIN32
22 #include "Xw/Xw.h"
23 #endif
24 #endif
26 #include <math.h>
28 /*-------------------------------------------------------------------------*/
29 /* Local includes */
30 /*-------------------------------------------------------------------------*/
32 #include "colordefs.h"
33 #include "xcircuit.h"
35 /*----------------------------------------------------------------------*/
36 /* Function prototype declarations */
37 /*----------------------------------------------------------------------*/
38 #include "prototypes.h"
40 /*-------------------------------------------------------------------------*/
41 /* Global Variable definitions */
42 /*-------------------------------------------------------------------------*/
44 extern Display *dpy; /* Works well to make this globally accessible */
45 extern Cursor appcursors[NUM_CURSORS];
46 extern Globaldata xobjs;
47 extern XCWindowData *areawin;
48 extern char _STR[150];
49 extern short fontcount;
50 extern fontinfo *fonts;
51 extern Boolean was_preselected;
52 extern int number_colors;
53 extern colorindex *colorlist;
55 /*---------------------------------------------------------*/
56 /* Find the Helvetica font for use in labeling the objects */
57 /*---------------------------------------------------------*/
59 short findhelvetica()
61 short fval;
63 if (fontcount == 0) loadfontfile("Helvetica");
65 for (fval = 0; fval < fontcount; fval++)
66 if (!strcmp(fonts[fval].psname, "Helvetica"))
67 break;
69 /* If not there, use the first Helvetica font */
71 if (fval == fontcount) {
72 for (fval = 0; fval < fontcount; fval++)
73 if (!strcmp(fonts[fval].family, "Helvetica"))
74 break;
77 /* If still not there, use the first non-Symbol font */
78 /* If this doesn't work, then the libraries are probably misplaced. . .*/
80 if (fval == fontcount) {
81 for (fval = 0; fval < fontcount; fval++)
82 if (strcmp(fonts[fval].family, "Symbol"))
83 break;
86 return fval;
89 /*-------------------------------------------*/
90 /* Return to drawing window from the library */
91 /*-------------------------------------------*/
93 void catreturn()
95 /* Pop the object being edited from the push stack. */
97 popobject(NULL, (pointertype)1, NULL);
100 /*------------------------------------------------------*/
101 /* Find page number from cursor position */
102 /* Mode = 0: Look for exact corresponding page number */
103 /* and return -1 if out-of-bounds */
104 /* Mode = 1: Look for position between pages, return */
105 /* page number of page to the right. */
106 /*------------------------------------------------------*/
108 int pageposition(short libmode, int x, int y, int mode)
110 int xin, yin, bpage, pages;
111 int gxsize, gysize, xdel, ydel;
113 pages = (libmode == PAGELIB) ? xobjs.pages : xobjs.numlibs;
114 computespacing(libmode, &gxsize, &gysize, &xdel, &ydel);
115 window_to_user(x, y, &areawin->save);
117 if (mode == 0) { /* On-page */
118 if (areawin->save.x >= 0 && areawin->save.y <= 0) {
119 xin = areawin->save.x / xdel;
120 yin = areawin->save.y / ydel;
121 if (xin < gxsize && yin > -gysize) {
122 bpage = (xin % gxsize) - (yin * gxsize);
123 if (bpage < pages)
124 return bpage;
127 return -1;
129 else { /* Between-pages */
130 xin = (areawin->save.x + (xdel >> 1)) / xdel;
131 if (xin > gxsize) xin = gxsize;
132 if (xin < 0) xin = 0;
133 yin = areawin->save.y / ydel;
134 if (yin > 0) yin = 0;
135 if (yin < -gysize) yin = -gysize;
136 bpage = (xin % (gxsize + 1)) + 1 - (yin * gxsize);
137 if (bpage > pages + 1) bpage = pages + 1;
138 return bpage;
142 /*------------------------------------------------------*/
143 /* Find the number of other pages linked to the */
144 /* indicated page (having the same filename, and */
145 /* ignoring empty pages). result is the total number */
146 /* of pages in the output file. */
147 /*------------------------------------------------------*/
149 short pagelinks(int page)
151 int i;
152 short count = 0;
154 for (i = 0; i < xobjs.pages; i++)
155 if (xobjs.pagelist[i]->pageinst != NULL)
156 if (xobjs.pagelist[i]->pageinst->thisobject->parts > 0)
157 if ((i == page) || (xobjs.pagelist[i]->filename &&
158 xobjs.pagelist[page]->filename &&
159 (!filecmp(xobjs.pagelist[i]->filename,
160 xobjs.pagelist[page]->filename))))
161 count++;
163 return count;
166 /*------------------------------------------------------*/
167 /* This is an expanded version of pagelinks() (above), */
168 /* to deal with the separate issues of independent top- */
169 /* level schematics and subcircuits. For the indicated */
170 /* page, return a list of pages depending on the mode: */
171 /* */
172 /* mode = INDEPENDENT: independent top-level pages */
173 /* mode = DEPENDENT: dependent pages (subcircuits) */
174 /* mode = PAGE_DEPEND: subcircuits of the current page, */
175 /* mode = LINKED_PAGES: subcircuits of the current */
176 /* page, including parameter links */
177 /* mode = TOTAL_PAGES: independent pages + subcircuits */
178 /* mode = ALL_PAGES: all pages in xcircuit */
179 /* */
180 /* The list is the size of the number of pages, and */
181 /* entries corresponding to the requested mode are set */
182 /* nonzero (the actual number indicates the number of */
183 /* references to the page, which may or may not be */
184 /* useful to know). */
185 /* */
186 /* It is the responsibility of the calling routine to */
187 /* free the memory allocated for the returned list. */
188 /*------------------------------------------------------*/
190 short *pagetotals(int page, short mode)
192 int i;
193 short *counts, *icount;
195 if (xobjs.pagelist[page]->pageinst == NULL) return NULL;
197 counts = (short *)malloc(xobjs.pages * sizeof(short));
198 icount = (short *)malloc(xobjs.pages * sizeof(short));
199 for (i = 0; i < xobjs.pages; i++) {
200 *(counts + i) = 0;
201 *(icount + i) = 0;
204 /* Find all the subcircuits of this page */
206 if (mode != ALL_PAGES)
207 findsubschems(page, xobjs.pagelist[page]->pageinst->thisobject,
208 0, counts, (mode == LINKED_PAGES) ? TRUE : FALSE);
210 /* Check independent entries (top-level pages which are not */
211 /* subcircuits of another page, but have the same filename */
212 /* as the page we started from). Set the counts entry to -1 */
213 /* to mark each independent page. */
215 if (mode != PAGE_DEPEND)
216 for (i = 0; i < xobjs.pages; i++)
217 if (xobjs.pagelist[i]->pageinst != NULL)
218 if (xobjs.pagelist[i]->pageinst->thisobject->parts > 0)
220 if (mode == ALL_PAGES)
221 (*(counts + i)) = 1;
222 else
224 if ((i == page) || (xobjs.pagelist[i]->filename
225 && xobjs.pagelist[page]->filename
226 && (!filecmp(xobjs.pagelist[i]->filename,
227 xobjs.pagelist[page]->filename))))
228 if ((mode == INDEPENDENT) || (*(counts + i) == 0))
229 (*(icount + i))++;
233 /* Check other dependent entries (top-level pages which are */
234 /* subcircuits of any independent page). */
236 if ((mode == DEPENDENT) || (mode == TOTAL_PAGES) || (mode == LINKED_PAGES))
238 for (i = 0; i < xobjs.pages; i++)
239 if ((i != page) && (*(icount + i) > 0))
240 findsubschems(i, xobjs.pagelist[i]->pageinst->thisobject,
241 0, counts, (mode == LINKED_PAGES) ? TRUE : FALSE);
244 if (mode == INDEPENDENT)
246 free((char *)counts);
247 return icount;
249 else
251 if ((mode == TOTAL_PAGES) || (mode == LINKED_PAGES)) {
252 /* merge dependent and independent */
253 for (i = 0; i < xobjs.pages; i++)
254 if (*(icount + i) > 0)
255 (*(counts + i))++;
257 free((char *)icount);
258 return counts;
262 /*---------------------------------------------------------*/
263 /* Test whether a library instance is a "virtual" instance */
264 /*---------------------------------------------------------*/
266 Boolean is_virtual(objinstptr thisinst) {
267 int libno;
268 liblistptr ilist;
270 libno = libfindobject(thisinst->thisobject, NULL);
272 for (ilist = xobjs.userlibs[libno].instlist; ilist != NULL; ilist = ilist->next)
273 if ((ilist->thisinst == thisinst) && (ilist->virtual == TRUE))
274 return TRUE;
276 return FALSE;
279 /*------------------------------------------------------*/
280 /* Test whether an object is a page, and return the */
281 /* page number if it is. Otherwise, return -1. */
282 /*------------------------------------------------------*/
284 int is_page(objectptr thisobj)
286 int i;
288 for (i = 0; i < xobjs.pages; i++)
289 if (xobjs.pagelist[i]->pageinst != NULL)
290 if (xobjs.pagelist[i]->pageinst->thisobject == thisobj) return i;
292 return -1;
295 /*------------------------------------------------------*/
296 /* Test whether an object is a library, and return the */
297 /* library number if it is. Otherwise, return -1. */
298 /*------------------------------------------------------*/
300 int is_library(objectptr thisobj)
302 int i;
304 for (i = 0; i < xobjs.numlibs; i++)
305 if (xobjs.libtop[i + LIBRARY]->thisobject == thisobj) return i;
307 return -1;
310 /*------------------------------------------------------*/
311 /* Check for library name (string). Because XCircuit */
312 /* generates the text "Library: <filename>" for */
313 /* library names, we also check against <filename> */
314 /* only in these names (this library name syntax is */
315 /* deprecated, but the check is retained for backwards */
316 /* compatibility). */
317 /* */
318 /* If no library matches the given name, return -1. */
319 /*------------------------------------------------------*/
321 int NameToLibrary(char *libname)
323 char *slib;
324 int i;
326 for (i = 0; i < xobjs.numlibs; i++) {
327 slib = xobjs.libtop[i + LIBRARY]->thisobject->name;
328 if (!strcmp(libname, slib)) {
329 return i;
331 else if (!strncmp(slib, "Library: ", 9)) {
332 if (!strcmp(libname, slib + 9)) {
333 return i;
337 return -1;
340 /*------------------------------------------------------*/
341 /* Move an object and all of its virtual instances from */
342 /* one library to another. */
343 /*------------------------------------------------------*/
345 int libmoveobject(objectptr thisobject, int libtarget)
347 int j, libsource;
348 liblistptr spec, slast, srch;
350 libsource = libfindobject(thisobject, &j);
352 if (libsource == libtarget) return libsource; /* nothing to do */
353 if (libsource < 0) return libsource; /* object not in the library */
355 /* Move the object from library "libsource" to library "libtarget" */
357 xobjs.userlibs[libtarget].library = (objectptr *)
358 realloc(xobjs.userlibs[libtarget].library,
359 (xobjs.userlibs[libtarget].number + 1) * sizeof(objectptr));
361 *(xobjs.userlibs[libtarget].library + xobjs.userlibs[libtarget].number) = thisobject;
362 xobjs.userlibs[libtarget].number++;
364 for (; j < xobjs.userlibs[libsource].number; j++)
365 *(xobjs.userlibs[libsource].library + j) =
366 *(xobjs.userlibs[libsource].library + j + 1);
367 xobjs.userlibs[libsource].number--;
369 /* Move all instances from library "libsource" to library "libtarget" */
371 slast = NULL;
372 for (spec = xobjs.userlibs[libsource].instlist; spec != NULL;) {
373 if (spec->thisinst->thisobject == thisobject) {
375 /* Add to end of spec list in target */
376 srch = xobjs.userlibs[libtarget].instlist;
377 if (srch == NULL)
378 xobjs.userlibs[libtarget].instlist = spec;
379 else {
380 for (; srch->next != NULL; srch = srch->next);
381 spec->next = srch->next;
382 srch->next = spec;
385 if (slast != NULL) {
386 slast->next = spec->next;
387 spec = slast->next;
389 else {
390 xobjs.userlibs[libsource].instlist = spec->next;
391 spec = xobjs.userlibs[libsource].instlist;
394 else {
395 slast = spec;
396 spec = spec->next;
400 return libsource;
403 /*------------------------------------------------------*/
404 /* Determine which library contains the specified */
405 /* object. If found, return the library number, or */
406 /* else return -1 if the object was not found in any */
407 /* library. If "partidx" is non-null, fill with the */
408 /* integer offset of the object from the beginning of */
409 /* the library. */
410 /*------------------------------------------------------*/
412 int libfindobject(objectptr thisobject, int *partidx)
414 int i, j;
415 objectptr libobj;
417 for (i = 0; i < xobjs.numlibs; i++) {
418 for (j = 0; j < xobjs.userlibs[i].number; j++) {
419 libobj = *(xobjs.userlibs[i].library + j);
420 if (libobj == thisobject) {
421 if (partidx != NULL) *partidx = j;
422 return i;
426 return -1;
429 /*------------------------------------------------------*/
430 /* ButtonPress handler during page catalog viewing mode */
431 /*------------------------------------------------------*/
433 void pagecat_op(int op, int x, int y)
435 int bpage;
436 short mode;
438 for (mode = 0; mode < LIBRARY; mode++) {
439 if (areawin->topinstance == xobjs.libtop[mode]) break;
441 if (mode == LIBRARY) return; /* Something went wrong if this happens */
443 if (op != XCF_Cancel) {
444 if ((bpage = pageposition(mode, x, y, 0)) >= 0) {
446 if (eventmode == ASSOC_MODE) {
447 if (mode == PAGELIB) {
448 /* using changepage() allows use of new page for schematic */
449 changepage(bpage);
450 /* associate the new schematic */
451 schemassoc(topobject, areawin->stack->thisinst->thisobject);
452 /* pop back to calling (symbol) page */
453 catreturn();
454 eventmode = NORMAL_MODE;
456 else {
457 areawin->lastlibrary = bpage;
458 startcatalog(NULL, (pointertype)(LIBRARY + bpage), NULL);
460 return;
462 else if (op == XCF_Select) {
463 if (mode == PAGELIB) /* No such method for LIBLIB is defined. */
464 select_add_element(OBJINST);
466 else if ((op == XCF_Library_Pop) || (op == XCF_Finish)) {
468 /* like catreturn(), but don't actually go to the popped page */
469 unselect_all();
470 eventmode = NORMAL_MODE;
471 if (mode == PAGELIB) {
472 newpage(bpage);
474 else {
475 startcatalog(NULL, (pointertype)(LIBRARY + bpage), NULL);
477 return;
481 else {
482 eventmode = NORMAL_MODE;
483 catreturn();
487 /*------------------------------------------------------------------------------*/
488 /* Subroutine to find the correct scale and position of the object instance */
489 /* representing an entire page in the page directory. */
490 /*------------------------------------------------------------------------------*/
492 void pageinstpos(short mode, short tpage, objinstptr drawinst, int gxsize,
493 int gysize, int xdel, int ydel)
495 objectptr libobj = drawinst->thisobject;
496 float scalex, scaley;
498 drawinst->position.x = (tpage % gxsize) * xdel;
499 drawinst->position.y = -(tpage / gxsize + 1) * ydel;
501 /* center the object on its page bounding box */
503 if (drawinst->bbox.width == 0 || drawinst->bbox.height == 0) {
504 drawinst->scale = 0.45 * libobj->viewscale;
505 drawinst->position.x += 0.05 * xdel - libobj->pcorner.x * drawinst->scale;
506 drawinst->position.y += 0.05 * ydel - libobj->pcorner.y * drawinst->scale;
508 else {
509 scalex = (0.9 * xdel) / drawinst->bbox.width;
510 scaley = (0.9 * ydel) / drawinst->bbox.height;
511 if (scalex > scaley) {
512 drawinst->scale = scaley;
513 drawinst->position.x -= (drawinst->bbox.lowerleft.x * scaley);
514 drawinst->position.x += (xdel - (drawinst->bbox.width * scaley)) / 2;
515 drawinst->position.y += 0.05 * ydel - drawinst->bbox.lowerleft.y
516 * drawinst->scale;
518 else {
519 drawinst->scale = scalex;
520 drawinst->position.y -= (drawinst->bbox.lowerleft.y * scalex);
521 drawinst->position.y += (ydel - (drawinst->bbox.height * scalex)) / 2;
522 drawinst->position.x += 0.05 * xdel - drawinst->bbox.lowerleft.x
523 * drawinst->scale;
528 /*--------------------------------------------------------------*/
529 /* Make a new instance for inserting into the page directory */
530 /*--------------------------------------------------------------*/
532 objinstptr newpageinst(objectptr pageobj)
534 objinstptr newinst = (objinstptr) malloc(sizeof(objinst));
535 instancedefaults(newinst, pageobj, 0, 0);
536 newinst->type = OBJINST;
537 newinst->color = DEFAULTCOLOR;
538 newinst->style = NORMAL; /* Do not scale linewidth with page scale */
539 return newinst;
542 /*-----------------------------------------------------------*/
543 /* Find spacing of objects for pages in the page directories */
544 /*-----------------------------------------------------------*/
546 void computespacing(short mode, int *gxsize, int *gysize, int *xdel, int *ydel)
548 int pages = (mode == PAGELIB) ? xobjs.pages : xobjs.numlibs;
550 *gxsize = (int)sqrt((double)pages) + 1;
551 *gysize = 1 + pages / (*gxsize);
553 /* 0.5 is the default vscale; g#size is the number of pages per line */
555 *xdel = areawin->width / (0.5 * (*gxsize));
556 *ydel = areawin->height / (0.5 * (*gysize));
559 /*-------------------------------------------------------------------*/
560 /* Draw the catalog of page ordering or the library master directory */
561 /*-------------------------------------------------------------------*/
563 void composepagelib(short mode)
565 genericptr *pgen;
566 objinstptr drawinst;
567 objectptr libobj, directory = xobjs.libtop[mode]->thisobject;
568 short i;
569 polyptr *drawbox;
570 labelptr *pagelabel;
571 stringpart *strptr;
572 pointlist pointptr;
573 int margin, xdel, ydel, gxsize, gysize;
574 int pages = (mode == PAGELIB) ? xobjs.pages : xobjs.numlibs;
575 short fval = findhelvetica();
577 /* Like the normal libraries, instances come from a special list, so */
578 /* they should not be destroyed, but will be null'd out and retrieved */
579 /* from the list. */
581 for (pgen = directory->plist; pgen < directory->plist + directory->parts; pgen++)
582 if (IS_OBJINST(*pgen)) *pgen = NULL;
584 reset(directory, NORMAL);
586 /* generate the list of object instances */
588 directory->plist = (genericptr *)malloc(sizeof(genericptr));
589 directory->parts = 0;
591 computespacing(mode, &gxsize, &gysize, &xdel, &ydel);
592 margin = xdel / 40; /* margin between pages */
594 for (i = 0; i < pages; i++) {
595 drawinst = (mode == PAGELIB) ? xobjs.pagelist[i]->pageinst :
596 xobjs.libtop[i + LIBRARY];
597 if (drawinst != NULL) {
598 libobj = drawinst->thisobject;
600 /* This is a stop-gap measure. . . should be recalculating the bounds of */
601 /* the instance on every action, not just before arranging the library. */
602 drawinst->bbox.lowerleft.x = libobj->bbox.lowerleft.x;
603 drawinst->bbox.lowerleft.y = libobj->bbox.lowerleft.y;
604 drawinst->bbox.width = libobj->bbox.width;
605 drawinst->bbox.height = libobj->bbox.height;
606 /* End stop-gap measure */
608 PLIST_INCR(directory);
609 *(directory->plist + directory->parts) = (genericptr)drawinst;
610 directory->parts++;
611 pageinstpos(mode, i, drawinst, gxsize, gysize, xdel, ydel);
614 /* separate pages (including empty ones) with bounding boxes */
616 NEW_POLY(drawbox, directory);
617 (*drawbox)->color = LOCALPINCOLOR; /* default red */
618 (*drawbox)->style = NORMAL; /* CLOSED */
619 (*drawbox)->width = 1.0;
620 (*drawbox)->number = 4;
621 (*drawbox)->points = (pointlist) malloc(4 * sizeof(XPoint));
622 (*drawbox)->passed = NULL;
623 (*drawbox)->cycle = NULL;
624 pointptr = (*drawbox)->points;
625 pointptr->x = (i % gxsize) * xdel + margin;
626 pointptr->y = -(i / gxsize) * ydel - margin;
627 pointptr = (*drawbox)->points + 1;
628 pointptr->x = ((i % gxsize) + 1) * xdel - margin;
629 pointptr->y = -(i / gxsize) * ydel - margin;
630 pointptr = (*drawbox)->points + 2;
631 pointptr->x = ((i % gxsize) + 1) * xdel - margin;
632 pointptr->y = -((i / gxsize) + 1) * ydel + margin;
633 pointptr = (*drawbox)->points + 3;
634 pointptr->x = (i % gxsize) * xdel + margin;
635 pointptr->y = -((i / gxsize) + 1) * ydel + margin;
637 /* each page gets its name printed at the bottom */
639 if (drawinst != NULL) {
640 NEW_LABEL(pagelabel, directory);
641 labeldefaults(*pagelabel, False, (pointptr->x + (pointptr-1)->x) / 2,
642 pointptr->y - 5);
643 (*pagelabel)->color = DEFAULTCOLOR;
644 (*pagelabel)->scale = 0.75;
645 (*pagelabel)->string->data.font = fval;
646 (*pagelabel)->passed = NULL;
647 strptr = makesegment(&((*pagelabel)->string), NULL);
648 strptr->type = TEXT_STRING;
649 strptr->data.string = (char *) malloc(1 + strlen(libobj->name));
650 strcpy(strptr->data.string, libobj->name);
651 (*pagelabel)->anchor = TOP | NOTBOTTOM | NOTLEFT;
655 /* calculate a bounding box for this display */
656 /* and center it in its window */
658 calcbbox(xobjs.libtop[mode]);
659 centerview(xobjs.libtop[mode]);
662 /*------------------------------------------------------------*/
663 /* Update the page or library directory based on new bounding */
664 /* box information for the page or library passed in "tpage". */
665 /*------------------------------------------------------------*/
667 void updatepagelib(short mode, short tpage)
669 objectptr compobj, libinst = xobjs.libtop[mode]->thisobject;
670 objinstptr pinst;
671 genericptr *gelem;
672 int i, xdel, ydel, gxsize, gysize, lpage;
674 /* lpage is the number of the page as found on the directory page */
675 lpage = (mode == PAGELIB) ? tpage : tpage - LIBRARY;
676 compobj = (mode == PAGELIB) ? xobjs.pagelist[tpage]->pageinst->thisobject
677 : xobjs.libtop[tpage]->thisobject;
679 computespacing(mode, &gxsize, &gysize, &xdel, &ydel);
681 for (i = 0; i < libinst->parts; i++) {
682 gelem = libinst->plist + i;
683 if (IS_OBJINST(*gelem)) {
684 pinst = TOOBJINST(gelem);
685 if (pinst->thisobject == compobj) {
686 /* recalculate scale and position of the object instance */
687 pageinstpos(mode, lpage, pinst, gxsize, gysize, xdel, ydel);
688 break;
693 /* if there is no instance for this page, then recompose the whole library */
695 if (i == libinst->parts) composelib(mode);
698 /*----------------------*/
699 /* Rearrange pages */
700 /*----------------------*/
702 void pagecatmove(int x, int y)
704 int bpage;
705 objinstptr exchobj;
706 Pagedata *ipage, **testpage, **tpage2;
708 if (areawin->selects == 0) return;
709 else if (areawin->selects > 2) {
710 Wprintf("Select maximum of two objects.");
711 return;
714 /* Get the page corresponding to the first selected object */
716 exchobj = SELTOOBJINST(areawin->selectlist);
717 for (testpage = xobjs.pagelist; testpage < xobjs.pagelist + xobjs.pages; testpage++)
718 if (*testpage != NULL && (*testpage)->pageinst == exchobj)
719 break;
721 /* If two objects are selected, then exchange their order */
723 if (areawin->selects == 2) {
724 exchobj = SELTOOBJINST(areawin->selectlist + 1);
725 for (tpage2 = xobjs.pagelist; tpage2 < xobjs.pagelist + xobjs.pages; tpage2++)
726 if (*tpage2 != NULL && (*tpage2)->pageinst == exchobj)
727 break;
729 ipage = *testpage;
730 *testpage = *tpage2;
731 *tpage2 = ipage;
734 /* If one object selected; find place to put from cursor position */
736 else if ((bpage = pageposition(PAGELIB, x, y, 1)) >= 0) {
737 int k, epage;
738 Pagedata *eptr;
740 /* Find page number of the original page */
742 epage = (int)(testpage - xobjs.pagelist);
743 eptr = *(xobjs.pagelist + epage);
745 /* move page (epage) to position between current pages */
746 /* (bpage - 2) and (bpage - 1) by shifting pointers. */
748 if ((bpage - 1) < epage) {
749 for (k = epage - 1; k >= bpage - 1; k--) {
750 *(xobjs.pagelist + k + 1) = *(xobjs.pagelist + k);
751 renamepage(k + 1);
753 *(xobjs.pagelist + bpage - 1) = eptr;
754 renamepage(bpage - 1);
756 else if ((bpage - 2) > epage) {
757 for (k = epage + 1; k <= bpage - 2; k++) {
758 *(xobjs.pagelist + k - 1) = *(xobjs.pagelist + k);
759 renamepage(k - 1);
761 *(xobjs.pagelist + bpage - 2) = eptr;
762 renamepage(bpage - 2);
766 unselect_all();
767 composelib(PAGELIB);
768 drawarea(NULL, NULL, NULL);
771 /*-----------------------------------------*/
772 /* Draw the catalog of predefined elements */
773 /*-----------------------------------------*/
775 void composelib(short mode)
777 genericptr *pgen;
778 objinstptr drawinst;
779 labelptr *drawname;
780 objectptr libobj, libpage = xobjs.libtop[mode]->thisobject;
781 liblistptr spec;
782 int xpos = 0, ypos = areawin->height << 1;
783 int nypos = 220, nxpos;
784 short fval;
785 short llx, lly, urx, ury, width, height, xcenter;
787 int totalarea, targetwidth;
788 double scale, savescale;
789 short savemode;
790 XPoint savepos;
792 /* Also make composelib() a wrapper for composepagelib() */
793 if ((mode > FONTLIB) && (mode < LIBRARY)) {
794 composepagelib(mode);
795 return;
798 /* The instances on the library page come from the library's */
799 /* "instlist". So that we don't destroy the actual instance when we */
800 /* call reset(), we find the pointer to the instance and NULL it. */
802 for (pgen = libpage->plist; pgen < libpage->plist + libpage->parts; pgen++)
803 if (IS_OBJINST(*pgen)) *pgen = NULL;
805 /* Before resetting, save the position and scale. We will restore */
806 /* them at the end. */
808 savepos = libpage->pcorner;
809 savescale = libpage->viewscale;
810 reset(libpage, NORMAL);
812 /* Return if library defines no objects or virtual instances */
814 if (xobjs.userlibs[mode - LIBRARY].instlist == NULL) return;
816 /* Find the Helvetica font for use in labeling the objects */
818 fval = findhelvetica();
820 /* Attempt to produce a library with the same aspect ratio as the */
821 /* drawing window. This is only approximate, and does not take */
822 /* into account factors such as the length of the name string. */
824 totalarea = 0;
825 for (spec = xobjs.userlibs[mode - LIBRARY].instlist; spec != NULL;
826 spec = spec->next) {
827 libobj = spec->thisinst->thisobject;
829 /* "Hidden" objects are not drawn */
830 if (libobj->hidden == True) continue;
832 drawinst = spec->thisinst;
833 drawinst->position.x = 0;
834 drawinst->position.y = 0;
836 /* Get the bounding box of the instance in the page's coordinate system */
837 calcinstbbox((genericptr *)(&drawinst), &llx, &lly, &urx, &ury);
838 width = urx - llx;
839 height = ury - lly;
840 width += 30; /* space padding */
841 height += 30; /* height padding */
842 if (width < 200) width = 200; /* minimum box width */
843 if (height < 220) height = 220; /* minimum box height */
844 totalarea += (width * height);
847 scale = (double)totalarea / (double)(areawin->width * areawin->height);
848 targetwidth = (int)(sqrt(scale) * (double)areawin->width);
850 /* generate the list of object instances and their labels */
852 savemode = eventmode;
853 eventmode = CATALOG_MODE;
854 for (spec = xobjs.userlibs[mode - LIBRARY].instlist; spec != NULL;
855 spec = spec->next) {
856 libobj = spec->thisinst->thisobject;
858 /* "Hidden" objects are not drawn */
859 if (libobj->hidden == True) continue;
861 drawinst = spec->thisinst;
862 drawinst->position.x = 0;
863 drawinst->position.y = 0;
865 /* Generate the part; unlike the usual NEW_OBJINST, the */
866 /* instance record isn't allocated. */
868 PLIST_INCR(libpage);
869 *(libpage->plist + libpage->parts) = (genericptr)drawinst;
870 libpage->parts++;
872 /* Get the bounding box of the instance in the page's coordinate system */
873 calcinstbbox((genericptr *)(&drawinst), &llx, &lly, &urx, &ury);
874 xcenter = (llx + urx) >> 1;
875 width = urx - llx;
876 height = ury - lly;
878 /* Add an ad-hoc spacing rule of 30 for padding space between objects */
879 width += 30;
881 /* Prepare the object name and determine its width. Adjust width */
882 /* needed for object on the library page if necessary. */
884 if (fval < fontcount) {
885 stringpart *strptr;
886 TextExtents tmpext;
888 NEW_LABEL(drawname, libpage);
889 labeldefaults(*drawname, False, 0, 0);
890 (*drawname)->color = (spec->virtual) ?
891 OFFBUTTONCOLOR : DEFAULTCOLOR;
892 (*drawname)->scale = 0.75;
893 (*drawname)->string->data.font = fval;
894 strptr = makesegment(&((*drawname)->string), NULL);
895 strptr->type = TEXT_STRING;
897 strptr->data.string = strdup(libobj->name);
898 (*drawname)->anchor = TOP | NOTBOTTOM | NOTLEFT;
900 /* If the label is longer than the object width, then */
901 /* adjust positions accordingly. Note that as an ad- */
902 /* hoc spacing rule, a padding of 30 is put between */
903 /* objects; this padding is reduced to 5 */
905 tmpext = ULength(*drawname, drawinst, NULL);
907 /* Ad-hoc spacing rule is 5 for labels */
908 tmpext.width += 5;
910 if (tmpext.width > width)
911 width = tmpext.width;
914 /* Minimum allowed width is 200 */
915 if (width < 200) width = 200;
917 /* Determine the area needed on the page to draw the object */
919 nxpos = xpos + width;
920 if ((nxpos > targetwidth) && (xpos > 0)) {
921 nxpos -= xpos;
922 xpos = 0;
923 ypos -= nypos;
924 nypos = 200;
927 if (height > (nypos - 50)) nypos = height + 50;
929 drawinst->position.x = xpos + (width >> 1) - xcenter;
930 drawinst->position.y = ypos - (height + lly);
931 if (height <= 170) drawinst->position.y -= ((170 - height) >> 1);
932 drawinst->color = DEFAULTCOLOR;
934 if (fval < fontcount) {
935 (*drawname)->position.x = xpos + (width >> 1);
937 if (height > 170)
938 (*drawname)->position.y = drawinst->position.y + lly - 10;
939 else
940 (*drawname)->position.y = ypos - 180;
943 xpos = nxpos;
945 eventmode = savemode;
947 /* Compute the bounding box of the library page */
948 calcbbox(xobjs.libtop[mode]);
950 /* Update the library directory */
951 updatepagelib(LIBLIB, mode);
953 /* Restore original view position */
954 libpage->pcorner = savepos;
955 libpage->viewscale = savescale;
958 /*----------------------------------------------------------------*/
959 /* Find any dependencies on an object. */
960 /* Return values: 0 = no dependency, 1 = dependency on page, */
961 /* 2 = dependency in another library object. */
962 /* Object/Page with dependency (if any) returned in "compobjp". */
963 /*----------------------------------------------------------------*/
965 short finddepend(objinstptr libobj, objectptr **compobjp)
967 genericptr *testobj;
968 short page, i, j;
969 objectptr *compobj;
971 for (i = 0; i < xobjs.numlibs; i++) {
972 for (j = 0; j < xobjs.userlibs[i].number; j++) {
973 compobj = xobjs.userlibs[i].library + j;
974 *compobjp = compobj;
976 for (testobj = (*compobj)->plist; testobj < (*compobj)->plist
977 + (*compobj)->parts; testobj++) {
978 if (IS_OBJINST(*testobj)) {
979 if (TOOBJINST(testobj)->thisobject == libobj->thisobject) return 2;
985 /* also look in the xobjs.pagelist */
987 for (page = 0; page < xobjs.pages; page++) {
988 if (xobjs.pagelist[page]->pageinst == NULL) continue;
989 compobj = &(xobjs.pagelist[page]->pageinst->thisobject);
990 *compobjp = compobj;
991 for (testobj = (*compobj)->plist; testobj < (*compobj)->plist
992 + (*compobj)->parts; testobj++) {
993 if (IS_OBJINST(*testobj)) {
994 if (TOOBJINST(testobj)->thisobject == libobj->thisobject) return 1;
998 return 0;
1001 /*--------------------------------------------------------------*/
1002 /* Virtual copy: Make a separate copy of an object on the same */
1003 /* library page as the original, representing an instance of */
1004 /* the object with different parameters. The object must have */
1005 /* parameters for this to make sense, so check for parameters */
1006 /* before allowing the virtual copy. */
1007 /*--------------------------------------------------------------*/
1009 void catvirtualcopy()
1011 short i, *newselect;
1012 objinstptr libobj, libinst;
1014 if (areawin->selects == 0) return;
1015 else if ((i = is_library(topobject)) < 0) return;
1017 /* Check for existance of parameters in the object for each */
1018 /* selected instance */
1020 for (newselect = areawin->selectlist; newselect < areawin->selectlist
1021 + areawin->selects; newselect++) {
1022 libobj = SELTOOBJINST(newselect);
1023 libinst = addtoinstlist(i, libobj->thisobject, TRUE);
1024 instcopy(libinst, libobj);
1025 tech_mark_changed(GetObjectTechnology(libobj->thisobject));
1028 clearselects();
1029 composelib(LIBRARY + i);
1030 drawarea(NULL, NULL, NULL);
1033 /*----------------------------------------------------------------*/
1034 /* "Hide" an object (must have a dependency or else it disappears)*/
1035 /*----------------------------------------------------------------*/
1037 void cathide()
1039 int i;
1040 short *newselect;
1041 objectptr *compobj;
1042 objinstptr libobj;
1044 if (areawin->selects == 0) return;
1046 /* Can only hide objects which are instances in other objects; */
1047 /* Otherwise, object would be "lost". */
1049 for (newselect = areawin->selectlist; newselect < areawin->selectlist
1050 + areawin->selects; newselect++) {
1051 libobj = SELTOOBJINST(newselect);
1053 if (finddepend(libobj, &compobj) == 0) {
1054 Wprintf("Cannot hide: no dependencies");
1056 else { /* All's clear to hide. */
1057 libobj->thisobject->hidden = True;
1061 clearselects();
1063 if ((i = is_library(topobject)) >= 0) composelib(LIBRARY + i);
1065 drawarea(NULL, NULL, NULL);
1068 /*----------------------------------------------------------------*/
1069 /* Delete an object from the library if there are no dependencies */
1070 /*----------------------------------------------------------------*/
1072 void catdelete()
1074 short *newselect, *libpobjs;
1075 int i;
1076 /* genericptr *testobj, *tobj; (jdk) */
1077 objinstptr libobj;
1078 liblistptr ilist, llist;
1079 objectptr *libpage, *compobj, *tlib, *slib;
1081 if (areawin->selects == 0) return;
1083 if ((i = is_library(topobject)) >= 0) {
1084 libpage = xobjs.userlibs[i].library;
1085 libpobjs = &xobjs.userlibs[i].number;
1087 else
1088 return; /* To-do: Should have a mechanism here for deleting pages! */
1090 for (newselect = areawin->selectlist; newselect < areawin->selectlist
1091 + areawin->selects; newselect++) {
1092 libobj = SELTOOBJINST(newselect);
1094 /* If this is just a "virtual copy", simply remove it from the list */
1096 llist = NULL;
1097 for (ilist = xobjs.userlibs[i].instlist; ilist != NULL;
1098 llist = ilist, ilist = ilist->next) {
1099 if ((ilist->thisinst == libobj) && (ilist->virtual == TRUE)) {
1100 if (llist == NULL)
1101 xobjs.userlibs[i].instlist = ilist->next;
1102 else
1103 llist->next = ilist->next;
1104 break;
1107 if (ilist != NULL) {
1108 free(ilist);
1109 continue;
1112 /* Cannot delete an object if another object uses an instance of it, */
1113 /* or if the object is used on a page. */
1115 if (finddepend(libobj, &compobj)) {
1116 Wprintf("Cannot delete: dependency in \"%s\"", (*compobj)->name);
1118 else { /* All's clear to delete. */
1120 /* Clear the undo stack so that any references to this object */
1121 /* won't screw up the database (this could be kinder & gentler */
1122 /* by first checking if there are any references to the object */
1123 /* in the undo stack. . . */
1125 flush_undo_stack();
1127 /* Next, remove the object from the library page. */
1129 for (tlib = libpage; tlib < libpage + *libpobjs; tlib++)
1130 if ((*tlib) == libobj->thisobject) {
1131 for (slib = tlib; slib < libpage + *libpobjs - 1; slib++)
1132 (*slib) = (*(slib + 1));
1133 (*libpobjs)--;
1134 break;
1137 /* Next, remove all instances of the object on the library page. */
1139 llist = NULL;
1140 for (ilist = xobjs.userlibs[i].instlist; ilist != NULL;
1141 llist = ilist, ilist = ilist->next) {
1142 if (ilist->thisinst->thisobject == libobj->thisobject) {
1143 if (llist == NULL) {
1144 xobjs.userlibs[i].instlist = ilist->next;
1145 free(ilist);
1146 if (!(ilist = xobjs.userlibs[i].instlist)) break;
1148 else {
1149 llist->next = ilist->next;
1150 free(ilist);
1151 if (!(ilist = llist)) break;
1156 /* Finally, delete the object (permanent---no undoing this!) */
1157 tech_mark_changed(GetObjectTechnology(libobj->thisobject));
1158 reset(libobj->thisobject, DESTROY);
1162 clearselects();
1164 if ((i = is_library(topobject)) >= 0) {
1165 composelib(LIBRARY + i);
1168 drawarea(NULL, NULL, NULL);
1171 /*------------------------------------------------------*/
1172 /* Linked list rearrangement routines */
1173 /*------------------------------------------------------*/
1175 void linkedlistswap(liblistptr *spec, int o1, int o2)
1177 liblistptr s1, s1m, s2, s2m, stmp;
1178 int j;
1180 if (o1 == o2) return;
1182 s1m = NULL;
1183 s1 = *spec;
1184 for (j = 0; j < o1; j++) {
1185 s1m = s1;
1186 s1 = s1->next;
1189 s2m = NULL;
1190 s2 = *spec;
1191 for (j = 0; j < o2; j++) {
1192 s2m = s2;
1193 s2 = s2->next;
1196 if (s2m)
1197 s2m->next = s1;
1198 else
1199 *spec = s1;
1201 if (s1m)
1202 s1m->next = s2;
1203 else
1204 *spec = s2;
1206 stmp = s1->next;
1207 s1->next = s2->next;
1208 s2->next = stmp;
1211 /*------------------------------------------------------*/
1213 void linkedlistinsertafter(liblistptr *spec, int o1, int o2)
1215 liblistptr s1, s1m, s2; /* , s2m, stmp; (jdk) */
1216 int j;
1218 if ((o1 == o2) || (o1 == (o2 + 1))) return;
1220 s1m = NULL;
1221 s1 = *spec;
1222 for (j = 0; j < o1; j++) {
1223 s1m = s1;
1224 s1 = s1->next;
1227 s2 = *spec;
1228 for (j = 0; j < o2; j++)
1229 s2 = s2->next;
1231 if (s1m)
1232 s1m->next = s1->next;
1233 else
1234 *spec = s1->next;
1236 if (o2 == -1) { /* move s1 to front */
1237 s1->next = *spec;
1238 *spec = s1;
1240 else {
1241 s1->next = s2->next;
1242 s2->next = s1;
1246 /*------------------------------------------------------*/
1247 /* Set the "changed" flag in a library if any object */
1248 /* in that library has changed. */
1249 /* */
1250 /* If "technology" is NULL, check all objects, */
1251 /* otherwise only check objects with a matching */
1252 /* technology. */
1253 /*------------------------------------------------------*/
1255 void tech_set_changes(TechPtr refns)
1257 TechPtr ns;
1258 int i, j;
1259 objectptr thisobj;
1261 for (i = 0; i < xobjs.numlibs; i++) {
1262 for (j = 0; j < xobjs.userlibs[i].number; j++) {
1263 thisobj = *(xobjs.userlibs[i].library + j);
1264 if (getchanges(thisobj) > 0) {
1265 ns = GetObjectTechnology(thisobj);
1266 if ((refns == NULL) || (refns == ns))
1267 ns->flags |= TECH_CHANGED;
1273 /*------------------------------------------------------*/
1274 /* Mark a technology as having been modified. */
1275 /*------------------------------------------------------*/
1277 void tech_mark_changed(TechPtr ns)
1279 if (ns != NULL) ns->flags |= TECH_CHANGED;
1282 /*------------------------------------------------------*/
1283 /* Rearrange objects in the library */
1284 /*------------------------------------------------------*/
1286 void catmove(int x, int y)
1288 int i, j, k, s1, s2, ocentx, ocenty, rangey, l; /* rangex, (jdk) */
1289 liblistptr spec;
1290 objinstptr exchobj, lobj;
1292 /* make catmove() a wrapper for pagecatmove() */
1294 if ((i = is_library(topobject)) < 0) {
1295 pagecatmove(x, y);
1296 return;
1299 if (areawin->selects == 0) return;
1301 /* Add selected object or objects at the cursor position */
1303 window_to_user(x, y, &areawin->save);
1305 s2 = -1;
1306 for (j = 0, spec = xobjs.userlibs[i].instlist; spec != NULL;
1307 spec = spec->next, j++) {
1308 lobj = spec->thisinst;
1309 for (k = 0; k < areawin->selects; k++) {
1310 exchobj = SELTOOBJINST(areawin->selectlist + k);
1311 if (lobj == exchobj) break;
1313 if (k < areawin->selects) continue; /* ignore items being moved */
1315 ocentx = lobj->position.x + lobj->bbox.lowerleft.x
1316 + (lobj->bbox.width >> 1);
1317 ocenty = lobj->position.y + lobj->bbox.lowerleft.y
1318 + (lobj->bbox.height >> 1);
1319 rangey = (lobj->bbox.height > 200) ?
1320 (lobj->bbox.height >> 1) : 100;
1322 if ((areawin->save.y < ocenty + rangey) && (areawin->save.y
1323 > ocenty - rangey)) {
1324 s2 = j - 1;
1325 if (areawin->save.x < ocentx) break;
1326 else s2 = j;
1329 if ((s2 == -1) && (spec == NULL)) {
1330 if (areawin->save.y <
1331 xobjs.libtop[i + LIBRARY]->thisobject->bbox.lowerleft.y)
1332 s2 = j - 1;
1333 else if (areawin->save.y <=
1334 xobjs.libtop[i + LIBRARY]->thisobject->bbox.lowerleft.y +
1335 xobjs.libtop[i + LIBRARY]->thisobject->bbox.height) {
1336 unselect_all();
1337 Wprintf("Could not find appropriate place to insert object");
1338 return;
1342 /* Find object number s2 (because s2 value may change during insertion) */
1343 if (s2 > -1) {
1344 for (k = 0, spec = xobjs.userlibs[i].instlist; k < s2; spec = spec->next, k++);
1345 lobj = spec->thisinst;
1347 else lobj = NULL;
1349 /* Move items; insert them after item s2 in order selected */
1351 j = i;
1352 for (k = 0; k < areawin->selects; k++) {
1354 /* Find number of lobj (may have changed) */
1356 if (lobj == NULL)
1357 s2 = -1;
1358 else {
1359 for (s2 = 0, spec = xobjs.userlibs[i].instlist; spec != NULL;
1360 spec = spec->next, s2++)
1361 if (spec->thisinst == lobj)
1362 break;
1365 exchobj = SELTOOBJINST(areawin->selectlist + k);
1366 for (s1 = 0, spec = xobjs.userlibs[i].instlist; spec != NULL;
1367 spec = spec->next, s1++)
1368 if (spec->thisinst == exchobj)
1369 break;
1371 if (spec == NULL) {
1372 /* Object came from another library */
1373 if ((l = libmoveobject(exchobj->thisobject, i)) >= 0) j = l;
1375 else {
1376 linkedlistinsertafter(&(xobjs.userlibs[i].instlist), s1, s2);
1380 unselect_all();
1381 composelib(LIBRARY + i);
1382 if (j != i) {
1383 composelib(LIBRARY + j);
1384 centerview(xobjs.libtop[LIBRARY + j]);
1387 drawarea(NULL, NULL, NULL);
1390 /*------------------------------------------------------*/
1391 /* Make a duplicate of an object, put in the User */
1392 /* Library or the current library (if we're in one). */
1393 /* */
1394 /* Updated 2/8/2014 for use with technology namespaces: */
1395 /* it is most likely that the copied object is to be */
1396 /* modified but kept in the same namespace. So, when */
1397 /* renaming the object, prepend "_" to the object name */
1398 /* instead of the namespace prefix. */
1399 /*------------------------------------------------------*/
1401 void copycat()
1403 short *newselect;
1404 objectptr *newobj, *curlib, oldobj;
1405 objinstptr libobj;
1406 oparamptr ops, newops;
1407 int i, libnum;
1408 char *cptr;
1410 /* Return if nothing is selected */
1411 if (areawin->selects == 0) return;
1413 libnum = is_library(topobject);
1414 if (libnum < 0) libnum = USERLIB - LIBRARY; /* default */
1416 for (newselect = areawin->selectlist; newselect < areawin->selectlist
1417 + areawin->selects; newselect++) {
1419 libobj = SELTOOBJINST(newselect);
1420 oldobj = libobj->thisobject;
1422 /* generate new entry in user library */
1424 curlib = (objectptr *) realloc(xobjs.userlibs[libnum].library,
1425 (xobjs.userlibs[libnum].number + 1) * sizeof(objectptr));
1426 xobjs.userlibs[libnum].library = curlib;
1427 newobj = xobjs.userlibs[libnum].library + xobjs.userlibs[libnum].number;
1428 *newobj = (objectptr) malloc(sizeof(object));
1429 xobjs.userlibs[libnum].number++;
1431 /* give the new object a unique name */
1433 cptr = strstr(oldobj->name, "::");
1434 if (cptr == NULL)
1435 sprintf((*newobj)->name, "_%s", oldobj->name);
1436 else {
1437 strcpy((*newobj)->name, oldobj->name);
1438 sprintf((*newobj)->name + (cptr - oldobj->name) + 2, "_%s", cptr + 2);
1440 checkname(*newobj);
1442 /* copy other object properties */
1444 (*newobj)->bbox.width = oldobj->bbox.width;
1445 (*newobj)->bbox.height = oldobj->bbox.height;
1446 (*newobj)->bbox.lowerleft.x = oldobj->bbox.lowerleft.x;
1447 (*newobj)->bbox.lowerleft.y = oldobj->bbox.lowerleft.y;
1448 (*newobj)->pcorner.x = oldobj->pcorner.x;
1449 (*newobj)->pcorner.y = oldobj->pcorner.y;
1450 (*newobj)->viewscale = oldobj->viewscale;
1451 /* don't attach the same schematic. . . */
1452 (*newobj)->symschem = NULL;
1453 /* don't copy highlights */
1454 (*newobj)->highlight.netlist = NULL;
1455 (*newobj)->highlight.thisinst = NULL;
1457 /* Copy the parameter structure */
1458 (*newobj)->params = NULL;
1459 for (ops = oldobj->params; ops != NULL; ops = ops->next) {
1460 newops = (oparamptr)malloc(sizeof(oparam));
1461 newops->next = (*newobj)->params;
1462 newops->key = strdup(ops->key);
1463 (*newobj)->params = newops;
1464 newops->type = ops->type;
1465 newops->which = ops->which;
1466 switch (ops->type) {
1467 case XC_INT:
1468 newops->parameter.ivalue = ops->parameter.ivalue;
1469 break;
1470 case XC_FLOAT:
1471 newops->parameter.fvalue = ops->parameter.fvalue;
1472 break;
1473 case XC_STRING:
1474 newops->parameter.string = stringcopy(ops->parameter.string);
1475 break;
1476 case XC_EXPR:
1477 newops->parameter.expr = strdup(ops->parameter.expr);
1478 break;
1482 (*newobj)->schemtype = oldobj->schemtype;
1483 (*newobj)->netnames = NULL;
1484 (*newobj)->ports = NULL;
1485 (*newobj)->calls = NULL;
1486 (*newobj)->polygons = NULL;
1487 (*newobj)->labels = NULL;
1488 (*newobj)->valid = False;
1489 (*newobj)->traversed = False;
1490 (*newobj)->hidden = False;
1492 /* copy over all the elements of the original object */
1494 (*newobj)->parts = 0;
1495 (*newobj)->plist = (genericptr *)malloc(sizeof(genericptr));
1497 for (i = 0; i < oldobj->parts; i++) {
1498 switch(ELEMENTTYPE(*(oldobj->plist + i))) {
1499 case(PATH): {
1500 register pathptr *npath, cpath = TOPATH(oldobj->plist + i);
1502 NEW_PATH(npath, (*newobj));
1503 pathcopy(*npath, cpath);
1504 } break;
1506 case(ARC): {
1507 register arcptr *narc, carc = TOARC(oldobj->plist + i);
1509 NEW_ARC(narc, (*newobj));
1510 arccopy(*narc, carc);
1511 } break;
1513 case(POLYGON): {
1514 register polyptr *npoly, cpoly = TOPOLY(oldobj->plist + i);
1516 NEW_POLY(npoly, (*newobj));
1517 polycopy(*npoly, cpoly);
1518 } break;
1520 case(SPLINE): {
1521 register splineptr *nspl, cspl = TOSPLINE(oldobj->plist + i);
1523 NEW_SPLINE(nspl, (*newobj));
1524 splinecopy(*nspl, cspl);
1525 } break;
1527 case(LABEL): {
1528 register labelptr *nlabel, clabel = TOLABEL(oldobj->plist + i);
1530 NEW_LABEL(nlabel, (*newobj));
1531 labelcopy(*nlabel, clabel);
1532 } break;
1534 case(OBJINST): {
1535 register objinstptr *ninst, cinst = TOOBJINST(oldobj->plist + i);
1537 NEW_OBJINST(ninst, (*newobj));
1538 instcopy(*ninst, cinst);
1539 } break;
1544 /* make instance for library and measure its bounding box */
1545 addtoinstlist(USERLIB - LIBRARY, *newobj, FALSE);
1547 composelib(USERLIB);
1548 unselect_all();
1550 if (areawin->topinstance == xobjs.libtop[USERLIB])
1551 drawarea(NULL, NULL, NULL);
1552 else startcatalog(NULL, (pointertype)USERLIB, NULL);
1553 zoomview(NULL, NULL, NULL);
1556 /*--------------------------------------------------------*/
1557 /* ButtonPress handler during normal catalog viewing mode */
1558 /*--------------------------------------------------------*/
1560 void catalog_op(int op, int x, int y)
1562 short *newselect;
1563 objinstptr *newobject, *libobj;
1564 objectptr libpage = topobject;
1565 short ocentx, ocenty, rangex, rangey, xdiff, ydiff, flag = 0;
1566 XPoint oldpos;
1568 /* Make catalog_op() a wrapper for pagecat_op() */
1570 if (is_library(topobject) < 0) {
1571 pagecat_op(op, x, y);
1572 return;
1575 /* If XCF_Cancel was invoked, return without a selection. */
1577 if (op == XCF_Cancel) {
1578 eventmode = NORMAL_MODE;
1579 catreturn();
1581 else {
1583 window_to_user(x, y, &areawin->save);
1585 for (libobj = (objinstptr *)topobject->plist; libobj <
1586 (objinstptr *)topobject->plist + topobject->parts; libobj++) {
1587 if (IS_OBJINST(*libobj)) {
1589 ocentx = (*libobj)->position.x + (*libobj)->bbox.lowerleft.x
1590 + ((*libobj)->bbox.width >> 1);
1591 ocenty = (*libobj)->position.y + (*libobj)->bbox.lowerleft.y
1592 + ((*libobj)->bbox.height >> 1);
1594 rangex = ((*libobj)->bbox.width > 200) ?
1595 ((*libobj)->bbox.width >> 1) : 100;
1596 rangey = ((*libobj)->bbox.height > 200) ?
1597 ((*libobj)->bbox.height >> 1) : 100;
1599 if (areawin->save.x > ocentx - rangex && areawin->save.x <
1600 ocentx + rangex && areawin->save.y < ocenty + rangey
1601 && areawin->save.y > ocenty - rangey) {
1603 /* setup to move object around and draw the selected object */
1605 if (eventmode == ASSOC_MODE) {
1607 /* revert to old page */
1608 topobject->viewscale = areawin->vscale;
1609 topobject->pcorner = areawin->pcorner;
1610 areawin->topinstance = (areawin->stack == NULL) ?
1611 xobjs.pagelist[areawin->page]->pageinst
1612 : areawin->stack->thisinst;
1613 /* associate the new object */
1614 schemassoc(topobject, (*libobj)->thisobject);
1615 setpage(TRUE);
1616 catreturn();
1617 eventmode = NORMAL_MODE;
1619 else if ((op == XCF_Library_Pop) || (op == XCF_Library_Copy)) {
1620 int saveselects;
1622 /* revert to old page */
1624 topobject->viewscale = areawin->vscale;
1625 topobject->pcorner = areawin->pcorner;
1626 areawin->topinstance = (areawin->stack == NULL) ?
1627 xobjs.pagelist[areawin->page]->pageinst
1628 : areawin->stack->thisinst;
1630 /* retrieve drawing window state and set position of object to
1631 be correct in reference to that window */
1633 snap(x, y, &oldpos);
1635 saveselects = areawin->selects;
1636 areawin->selects = 0;
1637 setpage(FALSE);
1638 areawin->selects = saveselects;
1640 snap(x, y, &areawin->save);
1641 xdiff = areawin->save.x - oldpos.x;
1642 ydiff = areawin->save.y - oldpos.y;
1644 /* collect all of the selected items */
1646 for (newselect = areawin->selectlist; newselect <
1647 areawin->selectlist + areawin->selects; newselect++) {
1648 NEW_OBJINST(newobject, topobject);
1649 instcopy(*newobject, TOOBJINST(libpage->plist + *newselect));
1650 /* color should be recast as current color */
1651 (*newobject)->color = areawin->color;
1652 /* position modified by (xdiff, ydiff) */
1653 (*newobject)->position.x += xdiff;
1654 (*newobject)->position.y += ydiff;
1656 u2u_snap(&((*newobject)->position));
1657 *newselect = (short)(newobject - (objinstptr *)topobject->plist);
1658 if ((*newobject)->thisobject == (*libobj)->thisobject)
1659 flag = 1;
1662 /* add final object to the list of object instances */
1664 if (!flag) {
1665 NEW_OBJINST(newobject, topobject);
1666 instcopy(*newobject, *libobj);
1667 (*newobject)->color = areawin->color;
1668 (*newobject)->position.x = areawin->save.x;
1669 (*newobject)->position.y = areawin->save.y;
1671 /* add this object to the list of selected items */
1673 newselect = allocselect();
1674 *newselect = (short)(newobject - (objinstptr *)topobject->plist);
1677 if (op == XCF_Library_Copy) {
1679 /* Key "c" pressed for "copy" (default binding) */
1681 XDefineCursor(dpy, areawin->window, COPYCURSOR);
1682 eventmode = COPY_MODE;
1683 #ifndef TCL_WRAPPER
1684 xcAddEventHandler(areawin->area, PointerMotionMask |
1685 ButtonMotionMask, False,
1686 (xcEventHandler)xlib_drag, NULL);
1687 #endif
1689 else {
1690 eventmode = MOVE_MODE;
1691 was_preselected = FALSE;
1692 register_for_undo(XCF_Library_Pop, UNDO_MORE, areawin->topinstance,
1693 areawin->selectlist, areawin->selects);
1695 #ifdef TCL_WRAPPER
1696 /* fprintf(stderr, "Creating event handler for xctk_drag: "); */
1697 /* printeventmode(); */
1698 Tk_CreateEventHandler(areawin->area, PointerMotionMask |
1699 ButtonMotionMask, (Tk_EventProc *)xctk_drag, NULL);
1700 #endif
1701 catreturn();
1704 /* Select the closest element and stay in the catalog. */
1705 /* Could just do "select_element" here, but would not cover */
1706 /* the entire area in the directory surrounding the object. */
1708 else if (op == XCF_Select) {
1709 short newinst = (short)(libobj - (objinstptr *)topobject->plist);
1710 /* (ignore this object if it is already in the list of selects) */
1711 for (newselect = areawin->selectlist; newselect <
1712 areawin->selectlist + areawin->selects; newselect++)
1713 if (*newselect == newinst) break;
1714 if (newselect == areawin->selectlist + areawin->selects) {
1715 newselect = allocselect();
1716 *newselect = newinst;
1717 XcTopSetForeground(SELECTCOLOR);
1718 UDrawObject(*libobj, SINGLE, SELECTCOLOR,
1719 xobjs.pagelist[areawin->page]->wirewidth, NULL);
1722 break;
1729 /*------------------------------*/
1730 /* Switch to the next catalog */
1731 /*------------------------------*/
1733 void changecat()
1735 int i, j;
1737 if ((i = is_library(topobject)) < 0) {
1738 if (areawin->lastlibrary >= xobjs.numlibs) areawin->lastlibrary = 0;
1739 j = areawin->lastlibrary;
1740 eventmode = CATALOG_MODE;
1742 else {
1743 j = (i + 1) % xobjs.numlibs;
1744 if (j == i) {
1745 Wprintf("This is the only library.");
1746 return;
1748 areawin->lastlibrary = j;
1751 if (eventmode == CATMOVE_MODE)
1752 delete_for_xfer(NORMAL, areawin->selectlist, areawin->selects);
1754 startcatalog(NULL, (pointertype)(j + LIBRARY), NULL);
1757 /*--------------------------------------*/
1758 /* Begin catalog viewing mode */
1759 /*--------------------------------------*/
1761 void startcatalog(xcWidget w, pointertype libmod, caddr_t nulldata)
1763 if (xobjs.libtop == NULL) return; /* No libraries defined */
1765 if ((xobjs.libtop[libmod]->thisobject == NULL) ||
1766 (areawin->topinstance == xobjs.libtop[libmod])) return;
1768 if (libmod == FONTLIB) {
1769 XDefineCursor (dpy, areawin->window, DEFAULTCURSOR);
1770 if (eventmode == TEXT_MODE)
1771 eventmode = FONTCAT_MODE;
1772 else
1773 eventmode = EFONTCAT_MODE;
1775 else if (eventmode == ASSOC_MODE) {
1776 XDefineCursor (dpy, areawin->window, DEFAULTCURSOR);
1778 else if (libmod == PAGELIB || libmod == LIBLIB) {
1779 XDefineCursor (dpy, areawin->window, DEFAULTCURSOR);
1780 eventmode = CATALOG_MODE;
1782 else if (eventmode != CATMOVE_MODE) {
1783 /* Don't know why I put this here---causes xcircuit to redraw */
1784 /* the schematic view when switching between library pages. */
1785 // finish_op(XCF_Cancel, 0, 0);
1786 eventmode = CATALOG_MODE;
1789 /* Push the current page onto the push stack, unless we're going */
1790 /* to a library from the library directory or vice versa, or */
1791 /* library to library. */
1793 if (!(((is_library(topobject) >= 0)
1794 || (areawin->topinstance == xobjs.libtop[LIBLIB])
1795 || (areawin->topinstance == xobjs.libtop[PAGELIB]))
1796 && libmod >= PAGELIB)) {
1797 push_stack(&areawin->stack, areawin->topinstance, NULL);
1800 /* set library as new object */
1802 topobject->viewscale = areawin->vscale;
1803 topobject->pcorner = areawin->pcorner;
1804 areawin->topinstance = xobjs.libtop[libmod];
1806 if (libmod == FONTLIB)
1807 setpage(FALSE);
1808 else {
1809 setpage(TRUE);
1810 transferselects();
1813 /* draw the new screen */
1815 refresh(NULL, NULL, NULL);
1818 /*-------------------------------------------------------------------------*/