-
Notifications
You must be signed in to change notification settings - Fork 24
/
libraries.c
1818 lines (1485 loc) · 54.6 KB
/
libraries.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------*/
/* libraries.c --- xcircuit routines for the builtin and user libraries */
/* Copyright (c) 2002 Tim Edwards, Johns Hopkins University */
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/* written by Tim Edwards, 8/13/93 */
/*-------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifndef XC_WIN32
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#endif
#ifdef TCL_WRAPPER
#include <tk.h>
#else
#ifndef XC_WIN32
#include "Xw/Xw.h"
#endif
#endif
#include <math.h>
/*-------------------------------------------------------------------------*/
/* Local includes */
/*-------------------------------------------------------------------------*/
#include "colordefs.h"
#include "xcircuit.h"
/*----------------------------------------------------------------------*/
/* Function prototype declarations */
/*----------------------------------------------------------------------*/
#include "prototypes.h"
/*-------------------------------------------------------------------------*/
/* Global Variable definitions */
/*-------------------------------------------------------------------------*/
extern Display *dpy; /* Works well to make this globally accessible */
extern Cursor appcursors[NUM_CURSORS];
extern Globaldata xobjs;
extern XCWindowData *areawin;
extern char _STR[150];
extern short fontcount;
extern fontinfo *fonts;
extern Boolean was_preselected;
extern int number_colors;
extern colorindex *colorlist;
/*---------------------------------------------------------*/
/* Find the Helvetica font for use in labeling the objects */
/*---------------------------------------------------------*/
short findhelvetica()
{
short fval;
if (fontcount == 0) loadfontfile("Helvetica");
for (fval = 0; fval < fontcount; fval++)
if (!strcmp(fonts[fval].psname, "Helvetica"))
break;
/* If not there, use the first Helvetica font */
if (fval == fontcount) {
for (fval = 0; fval < fontcount; fval++)
if (!strcmp(fonts[fval].family, "Helvetica"))
break;
}
/* If still not there, use the first non-Symbol font */
/* If this doesn't work, then the libraries are probably misplaced. . .*/
if (fval == fontcount) {
for (fval = 0; fval < fontcount; fval++)
if (strcmp(fonts[fval].family, "Symbol"))
break;
}
return fval;
}
/*-------------------------------------------*/
/* Return to drawing window from the library */
/*-------------------------------------------*/
void catreturn()
{
/* Pop the object being edited from the push stack. */
popobject(NULL, (pointertype)1, NULL);
}
/*------------------------------------------------------*/
/* Find page number from cursor position */
/* Mode = 0: Look for exact corresponding page number */
/* and return -1 if out-of-bounds */
/* Mode = 1: Look for position between pages, return */
/* page number of page to the right. */
/*------------------------------------------------------*/
int pageposition(short libmode, int x, int y, int mode)
{
int xin, yin, bpage, pages;
int gxsize, gysize, xdel, ydel;
pages = (libmode == PAGELIB) ? xobjs.pages : xobjs.numlibs;
computespacing(libmode, &gxsize, &gysize, &xdel, &ydel);
window_to_user(x, y, &areawin->save);
if (mode == 0) { /* On-page */
if (areawin->save.x >= 0 && areawin->save.y <= 0) {
xin = areawin->save.x / xdel;
yin = areawin->save.y / ydel;
if (xin < gxsize && yin > -gysize) {
bpage = (xin % gxsize) - (yin * gxsize);
if (bpage < pages)
return bpage;
}
}
return -1;
}
else { /* Between-pages */
xin = (areawin->save.x + (xdel >> 1)) / xdel;
if (xin > gxsize) xin = gxsize;
if (xin < 0) xin = 0;
yin = areawin->save.y / ydel;
if (yin > 0) yin = 0;
if (yin < -gysize) yin = -gysize;
bpage = (xin % (gxsize + 1)) + 1 - (yin * gxsize);
if (bpage > pages + 1) bpage = pages + 1;
return bpage;
}
}
/*------------------------------------------------------*/
/* Find the number of other pages linked to the */
/* indicated page (having the same filename, and */
/* ignoring empty pages). result is the total number */
/* of pages in the output file. */
/*------------------------------------------------------*/
short pagelinks(int page)
{
int i;
short count = 0;
for (i = 0; i < xobjs.pages; i++)
if (xobjs.pagelist[i]->pageinst != NULL)
if (xobjs.pagelist[i]->pageinst->thisobject->parts > 0)
if ((i == page) || (xobjs.pagelist[i]->filename &&
xobjs.pagelist[page]->filename &&
(!filecmp(xobjs.pagelist[i]->filename,
xobjs.pagelist[page]->filename))))
count++;
return count;
}
/*------------------------------------------------------*/
/* This is an expanded version of pagelinks() (above), */
/* to deal with the separate issues of independent top- */
/* level schematics and subcircuits. For the indicated */
/* page, return a list of pages depending on the mode: */
/* */
/* mode = INDEPENDENT: independent top-level pages */
/* mode = DEPENDENT: dependent pages (subcircuits) */
/* mode = PAGE_DEPEND: subcircuits of the current page, */
/* mode = LINKED_PAGES: subcircuits of the current */
/* page, including parameter links */
/* mode = TOTAL_PAGES: independent pages + subcircuits */
/* mode = ALL_PAGES: all pages in xcircuit */
/* */
/* The list is the size of the number of pages, and */
/* entries corresponding to the requested mode are set */
/* nonzero (the actual number indicates the number of */
/* references to the page, which may or may not be */
/* useful to know). */
/* */
/* It is the responsibility of the calling routine to */
/* free the memory allocated for the returned list. */
/*------------------------------------------------------*/
short *pagetotals(int page, short mode)
{
int i;
short *counts, *icount;
if (xobjs.pagelist[page]->pageinst == NULL) return NULL;
counts = (short *)malloc(xobjs.pages * sizeof(short));
icount = (short *)malloc(xobjs.pages * sizeof(short));
for (i = 0; i < xobjs.pages; i++) {
*(counts + i) = 0;
*(icount + i) = 0;
}
/* Find all the subcircuits of this page */
if (mode != ALL_PAGES)
findsubschems(page, xobjs.pagelist[page]->pageinst->thisobject,
0, counts, (mode == LINKED_PAGES) ? TRUE : FALSE);
/* Check independent entries (top-level pages which are not */
/* subcircuits of another page, but have the same filename */
/* as the page we started from). Set the counts entry to -1 */
/* to mark each independent page. */
if (mode != PAGE_DEPEND)
for (i = 0; i < xobjs.pages; i++)
if (xobjs.pagelist[i]->pageinst != NULL)
if (xobjs.pagelist[i]->pageinst->thisobject->parts > 0)
{
if (mode == ALL_PAGES)
(*(counts + i)) = 1;
else
{
if ((i == page) || (xobjs.pagelist[i]->filename
&& xobjs.pagelist[page]->filename
&& (!filecmp(xobjs.pagelist[i]->filename,
xobjs.pagelist[page]->filename))))
if ((mode == INDEPENDENT) || (*(counts + i) == 0))
(*(icount + i))++;
}
}
/* Check other dependent entries (top-level pages which are */
/* subcircuits of any independent page). */
if ((mode == DEPENDENT) || (mode == TOTAL_PAGES) || (mode == LINKED_PAGES))
{
for (i = 0; i < xobjs.pages; i++)
if ((i != page) && (*(icount + i) > 0))
findsubschems(i, xobjs.pagelist[i]->pageinst->thisobject,
0, counts, (mode == LINKED_PAGES) ? TRUE : FALSE);
}
if (mode == INDEPENDENT)
{
free((char *)counts);
return icount;
}
else
{
if ((mode == TOTAL_PAGES) || (mode == LINKED_PAGES)) {
/* merge dependent and independent */
for (i = 0; i < xobjs.pages; i++)
if (*(icount + i) > 0)
(*(counts + i))++;
}
free((char *)icount);
return counts;
}
}
/*---------------------------------------------------------*/
/* Test whether a library instance is a "virtual" instance */
/*---------------------------------------------------------*/
Boolean is_virtual(objinstptr thisinst) {
int libno;
liblistptr ilist;
libno = libfindobject(thisinst->thisobject, NULL);
for (ilist = xobjs.userlibs[libno].instlist; ilist != NULL; ilist = ilist->next)
if ((ilist->thisinst == thisinst) && (ilist->virtual == TRUE))
return TRUE;
return FALSE;
}
/*------------------------------------------------------*/
/* Test whether an object is a page, and return the */
/* page number if it is. Otherwise, return -1. */
/*------------------------------------------------------*/
int is_page(objectptr thisobj)
{
int i;
for (i = 0; i < xobjs.pages; i++)
if (xobjs.pagelist[i]->pageinst != NULL)
if (xobjs.pagelist[i]->pageinst->thisobject == thisobj) return i;
return -1;
}
/*------------------------------------------------------*/
/* Test whether an object is a library, and return the */
/* library number if it is. Otherwise, return -1. */
/*------------------------------------------------------*/
int is_library(objectptr thisobj)
{
int i;
for (i = 0; i < xobjs.numlibs; i++)
if (xobjs.libtop[i + LIBRARY]->thisobject == thisobj) return i;
return -1;
}
/*------------------------------------------------------*/
/* Check for library name (string). Because XCircuit */
/* generates the text "Library: <filename>" for */
/* library names, we also check against <filename> */
/* only in these names (this library name syntax is */
/* deprecated, but the check is retained for backwards */
/* compatibility). */
/* */
/* If no library matches the given name, return -1. */
/*------------------------------------------------------*/
int NameToLibrary(char *libname)
{
char *slib;
int i;
for (i = 0; i < xobjs.numlibs; i++) {
slib = xobjs.libtop[i + LIBRARY]->thisobject->name;
if (!strcmp(libname, slib)) {
return i;
}
else if (!strncmp(slib, "Library: ", 9)) {
if (!strcmp(libname, slib + 9)) {
return i;
}
}
}
return -1;
}
/*------------------------------------------------------*/
/* Move an object and all of its virtual instances from */
/* one library to another. */
/*------------------------------------------------------*/
int libmoveobject(objectptr thisobject, int libtarget)
{
int j, libsource;
liblistptr spec, slast, srch;
libsource = libfindobject(thisobject, &j);
if (libsource == libtarget) return libsource; /* nothing to do */
if (libsource < 0) return libsource; /* object not in the library */
/* Move the object from library "libsource" to library "libtarget" */
xobjs.userlibs[libtarget].library = (objectptr *)
realloc(xobjs.userlibs[libtarget].library,
(xobjs.userlibs[libtarget].number + 1) * sizeof(objectptr));
*(xobjs.userlibs[libtarget].library + xobjs.userlibs[libtarget].number) = thisobject;
xobjs.userlibs[libtarget].number++;
for (; j < xobjs.userlibs[libsource].number; j++)
*(xobjs.userlibs[libsource].library + j) =
*(xobjs.userlibs[libsource].library + j + 1);
xobjs.userlibs[libsource].number--;
/* Move all instances from library "libsource" to library "libtarget" */
slast = NULL;
for (spec = xobjs.userlibs[libsource].instlist; spec != NULL;) {
if (spec->thisinst->thisobject == thisobject) {
/* Add to end of spec list in target */
srch = xobjs.userlibs[libtarget].instlist;
if (srch == NULL)
xobjs.userlibs[libtarget].instlist = spec;
else {
for (; srch->next != NULL; srch = srch->next);
spec->next = srch->next;
srch->next = spec;
}
if (slast != NULL) {
slast->next = spec->next;
spec = slast->next;
}
else {
xobjs.userlibs[libsource].instlist = spec->next;
spec = xobjs.userlibs[libsource].instlist;
}
}
else {
slast = spec;
spec = spec->next;
}
}
return libsource;
}
/*------------------------------------------------------*/
/* Determine which library contains the specified */
/* object. If found, return the library number, or */
/* else return -1 if the object was not found in any */
/* library. If "partidx" is non-null, fill with the */
/* integer offset of the object from the beginning of */
/* the library. */
/*------------------------------------------------------*/
int libfindobject(objectptr thisobject, int *partidx)
{
int i, j;
objectptr libobj;
for (i = 0; i < xobjs.numlibs; i++) {
for (j = 0; j < xobjs.userlibs[i].number; j++) {
libobj = *(xobjs.userlibs[i].library + j);
if (libobj == thisobject) {
if (partidx != NULL) *partidx = j;
return i;
}
}
}
return -1;
}
/*------------------------------------------------------*/
/* ButtonPress handler during page catalog viewing mode */
/*------------------------------------------------------*/
void pagecat_op(int op, int x, int y)
{
int bpage;
short mode;
for (mode = 0; mode < LIBRARY; mode++) {
if (areawin->topinstance == xobjs.libtop[mode]) break;
}
if (mode == LIBRARY) return; /* Something went wrong if this happens */
if (op != XCF_Cancel) {
if ((bpage = pageposition(mode, x, y, 0)) >= 0) {
if (eventmode == ASSOC_MODE) {
if (mode == PAGELIB) {
/* using changepage() allows use of new page for schematic */
changepage(bpage);
/* associate the new schematic */
schemassoc(topobject, areawin->stack->thisinst->thisobject);
/* pop back to calling (symbol) page */
catreturn();
eventmode = NORMAL_MODE;
}
else {
areawin->lastlibrary = bpage;
startcatalog(NULL, (pointertype)(LIBRARY + bpage), NULL);
}
return;
}
else if (op == XCF_Select) {
if (mode == PAGELIB) /* No such method for LIBLIB is defined. */
select_add_element(OBJINST);
}
else if ((op == XCF_Library_Pop) || (op == XCF_Finish)) {
/* like catreturn(), but don't actually go to the popped page */
unselect_all();
eventmode = NORMAL_MODE;
if (mode == PAGELIB) {
newpage(bpage);
}
else {
startcatalog(NULL, (pointertype)(LIBRARY + bpage), NULL);
}
return;
}
}
}
else {
eventmode = NORMAL_MODE;
catreturn();
}
}
/*------------------------------------------------------------------------------*/
/* Subroutine to find the correct scale and position of the object instance */
/* representing an entire page in the page directory. */
/*------------------------------------------------------------------------------*/
void pageinstpos(short mode, short tpage, objinstptr drawinst, int gxsize,
int gysize, int xdel, int ydel)
{
objectptr libobj = drawinst->thisobject;
float scalex, scaley;
drawinst->position.x = (tpage % gxsize) * xdel;
drawinst->position.y = -(tpage / gxsize + 1) * ydel;
/* center the object on its page bounding box */
if (drawinst->bbox.width == 0 || drawinst->bbox.height == 0) {
drawinst->scale = 0.45 * libobj->viewscale;
drawinst->position.x += 0.05 * xdel - libobj->pcorner.x * drawinst->scale;
drawinst->position.y += 0.05 * ydel - libobj->pcorner.y * drawinst->scale;
}
else {
scalex = (0.9 * xdel) / drawinst->bbox.width;
scaley = (0.9 * ydel) / drawinst->bbox.height;
if (scalex > scaley) {
drawinst->scale = scaley;
drawinst->position.x -= (drawinst->bbox.lowerleft.x * scaley);
drawinst->position.x += (xdel - (drawinst->bbox.width * scaley)) / 2;
drawinst->position.y += 0.05 * ydel - drawinst->bbox.lowerleft.y
* drawinst->scale;
}
else {
drawinst->scale = scalex;
drawinst->position.y -= (drawinst->bbox.lowerleft.y * scalex);
drawinst->position.y += (ydel - (drawinst->bbox.height * scalex)) / 2;
drawinst->position.x += 0.05 * xdel - drawinst->bbox.lowerleft.x
* drawinst->scale;
}
}
}
/*--------------------------------------------------------------*/
/* Make a new instance for inserting into the page directory */
/*--------------------------------------------------------------*/
objinstptr newpageinst(objectptr pageobj)
{
objinstptr newinst = (objinstptr) malloc(sizeof(objinst));
instancedefaults(newinst, pageobj, 0, 0);
newinst->type = OBJINST;
newinst->color = DEFAULTCOLOR;
newinst->style = NORMAL; /* Do not scale linewidth with page scale */
return newinst;
}
/*-----------------------------------------------------------*/
/* Find spacing of objects for pages in the page directories */
/*-----------------------------------------------------------*/
void computespacing(short mode, int *gxsize, int *gysize, int *xdel, int *ydel)
{
int pages = (mode == PAGELIB) ? xobjs.pages : xobjs.numlibs;
*gxsize = (int)sqrt((double)pages) + 1;
*gysize = 1 + pages / (*gxsize);
/* 0.5 is the default vscale; g#size is the number of pages per line */
*xdel = areawin->width / (0.5 * (*gxsize));
*ydel = areawin->height / (0.5 * (*gysize));
}
/*-------------------------------------------------------------------*/
/* Draw the catalog of page ordering or the library master directory */
/*-------------------------------------------------------------------*/
void composepagelib(short mode)
{
genericptr *pgen;
objinstptr drawinst;
objectptr libobj, directory = xobjs.libtop[mode]->thisobject;
short i;
polyptr *drawbox;
labelptr *pagelabel;
stringpart *strptr;
pointlist pointptr;
int margin, xdel, ydel, gxsize, gysize;
int pages = (mode == PAGELIB) ? xobjs.pages : xobjs.numlibs;
short fval = findhelvetica();
/* Like the normal libraries, instances come from a special list, so */
/* they should not be destroyed, but will be null'd out and retrieved */
/* from the list. */
for (pgen = directory->plist; pgen < directory->plist + directory->parts; pgen++)
if (IS_OBJINST(*pgen)) *pgen = NULL;
reset(directory, NORMAL);
/* generate the list of object instances */
directory->plist = (genericptr *)malloc(sizeof(genericptr));
directory->parts = 0;
computespacing(mode, &gxsize, &gysize, &xdel, &ydel);
margin = xdel / 40; /* margin between pages */
for (i = 0; i < pages; i++) {
drawinst = (mode == PAGELIB) ? xobjs.pagelist[i]->pageinst :
xobjs.libtop[i + LIBRARY];
if (drawinst != NULL) {
libobj = drawinst->thisobject;
/* This is a stop-gap measure. . . should be recalculating the bounds of */
/* the instance on every action, not just before arranging the library. */
drawinst->bbox.lowerleft.x = libobj->bbox.lowerleft.x;
drawinst->bbox.lowerleft.y = libobj->bbox.lowerleft.y;
drawinst->bbox.width = libobj->bbox.width;
drawinst->bbox.height = libobj->bbox.height;
/* End stop-gap measure */
PLIST_INCR(directory);
*(directory->plist + directory->parts) = (genericptr)drawinst;
directory->parts++;
pageinstpos(mode, i, drawinst, gxsize, gysize, xdel, ydel);
}
/* separate pages (including empty ones) with bounding boxes */
NEW_POLY(drawbox, directory);
(*drawbox)->color = LOCALPINCOLOR; /* default red */
(*drawbox)->style = NORMAL; /* CLOSED */
(*drawbox)->width = 1.0;
(*drawbox)->number = 4;
(*drawbox)->points = (pointlist) malloc(4 * sizeof(XPoint));
(*drawbox)->passed = NULL;
(*drawbox)->cycle = NULL;
pointptr = (*drawbox)->points;
pointptr->x = (i % gxsize) * xdel + margin;
pointptr->y = -(i / gxsize) * ydel - margin;
pointptr = (*drawbox)->points + 1;
pointptr->x = ((i % gxsize) + 1) * xdel - margin;
pointptr->y = -(i / gxsize) * ydel - margin;
pointptr = (*drawbox)->points + 2;
pointptr->x = ((i % gxsize) + 1) * xdel - margin;
pointptr->y = -((i / gxsize) + 1) * ydel + margin;
pointptr = (*drawbox)->points + 3;
pointptr->x = (i % gxsize) * xdel + margin;
pointptr->y = -((i / gxsize) + 1) * ydel + margin;
/* each page gets its name printed at the bottom */
if (drawinst != NULL) {
NEW_LABEL(pagelabel, directory);
labeldefaults(*pagelabel, False, (pointptr->x + (pointptr-1)->x) / 2,
pointptr->y - 5);
(*pagelabel)->color = DEFAULTCOLOR;
(*pagelabel)->scale = 0.75;
(*pagelabel)->string->data.font = fval;
(*pagelabel)->passed = NULL;
strptr = makesegment(&((*pagelabel)->string), NULL);
strptr->type = TEXT_STRING;
strptr->data.string = (char *) malloc(1 + strlen(libobj->name));
strcpy(strptr->data.string, libobj->name);
(*pagelabel)->anchor = TOP | NOTBOTTOM | NOTLEFT;
}
}
/* calculate a bounding box for this display */
/* and center it in its window */
calcbbox(xobjs.libtop[mode]);
centerview(xobjs.libtop[mode]);
}
/*------------------------------------------------------------*/
/* Update the page or library directory based on new bounding */
/* box information for the page or library passed in "tpage". */
/*------------------------------------------------------------*/
void updatepagelib(short mode, short tpage)
{
objectptr compobj, libinst = xobjs.libtop[mode]->thisobject;
objinstptr pinst;
genericptr *gelem;
int i, xdel, ydel, gxsize, gysize, lpage;
/* lpage is the number of the page as found on the directory page */
lpage = (mode == PAGELIB) ? tpage : tpage - LIBRARY;
compobj = (mode == PAGELIB) ? xobjs.pagelist[tpage]->pageinst->thisobject
: xobjs.libtop[tpage]->thisobject;
computespacing(mode, &gxsize, &gysize, &xdel, &ydel);
for (i = 0; i < libinst->parts; i++) {
gelem = libinst->plist + i;
if (IS_OBJINST(*gelem)) {
pinst = TOOBJINST(gelem);
if (pinst->thisobject == compobj) {
/* recalculate scale and position of the object instance */
pageinstpos(mode, lpage, pinst, gxsize, gysize, xdel, ydel);
break;
}
}
}
/* if there is no instance for this page, then recompose the whole library */
if (i == libinst->parts) composelib(mode);
}
/*----------------------*/
/* Rearrange pages */
/*----------------------*/
void pagecatmove(int x, int y)
{
int bpage;
objinstptr exchobj;
Pagedata *ipage, **testpage, **tpage2;
if (areawin->selects == 0) return;
else if (areawin->selects > 2) {
Wprintf("Select maximum of two objects.");
return;
}
/* Get the page corresponding to the first selected object */
exchobj = SELTOOBJINST(areawin->selectlist);
for (testpage = xobjs.pagelist; testpage < xobjs.pagelist + xobjs.pages; testpage++)
if (*testpage != NULL && (*testpage)->pageinst == exchobj)
break;
/* If two objects are selected, then exchange their order */
if (areawin->selects == 2) {
exchobj = SELTOOBJINST(areawin->selectlist + 1);
for (tpage2 = xobjs.pagelist; tpage2 < xobjs.pagelist + xobjs.pages; tpage2++)
if (*tpage2 != NULL && (*tpage2)->pageinst == exchobj)
break;
ipage = *testpage;
*testpage = *tpage2;
*tpage2 = ipage;
}
/* If one object selected; find place to put from cursor position */
else if ((bpage = pageposition(PAGELIB, x, y, 1)) >= 0) {
int k, epage;
Pagedata *eptr;
/* Find page number of the original page */
epage = (int)(testpage - xobjs.pagelist);
eptr = *(xobjs.pagelist + epage);
/* move page (epage) to position between current pages */
/* (bpage - 2) and (bpage - 1) by shifting pointers. */
if ((bpage - 1) < epage) {
for (k = epage - 1; k >= bpage - 1; k--) {
*(xobjs.pagelist + k + 1) = *(xobjs.pagelist + k);
renamepage(k + 1);
}
*(xobjs.pagelist + bpage - 1) = eptr;
renamepage(bpage - 1);
}
else if ((bpage - 2) > epage) {
for (k = epage + 1; k <= bpage - 2; k++) {
*(xobjs.pagelist + k - 1) = *(xobjs.pagelist + k);
renamepage(k - 1);
}
*(xobjs.pagelist + bpage - 2) = eptr;
renamepage(bpage - 2);
}
}
unselect_all();
composelib(PAGELIB);
drawarea(NULL, NULL, NULL);
}
/*-----------------------------------------*/
/* Draw the catalog of predefined elements */
/*-----------------------------------------*/
void composelib(short mode)
{
genericptr *pgen;
objinstptr drawinst;
labelptr *drawname;
objectptr libobj, libpage = xobjs.libtop[mode]->thisobject;
liblistptr spec;
int xpos = 0, ypos = areawin->height << 1;
int nypos = 220, nxpos;
short fval;
short llx, lly, urx, ury, width, height, xcenter;
int totalarea, targetwidth;
double scale, savescale;
short savemode;
XPoint savepos;
/* Also make composelib() a wrapper for composepagelib() */
if ((mode > FONTLIB) && (mode < LIBRARY)) {
composepagelib(mode);
return;
}
/* The instances on the library page come from the library's */
/* "instlist". So that we don't destroy the actual instance when we */
/* call reset(), we find the pointer to the instance and NULL it. */
for (pgen = libpage->plist; pgen < libpage->plist + libpage->parts; pgen++)
if (IS_OBJINST(*pgen)) *pgen = NULL;
/* Before resetting, save the position and scale. We will restore */
/* them at the end. */
savepos = libpage->pcorner;
savescale = libpage->viewscale;
reset(libpage, NORMAL);
/* Return if library defines no objects or virtual instances */
if (xobjs.userlibs[mode - LIBRARY].instlist == NULL) return;
/* Find the Helvetica font for use in labeling the objects */
fval = findhelvetica();
/* Attempt to produce a library with the same aspect ratio as the */
/* drawing window. This is only approximate, and does not take */
/* into account factors such as the length of the name string. */
totalarea = 0;
for (spec = xobjs.userlibs[mode - LIBRARY].instlist; spec != NULL;
spec = spec->next) {
libobj = spec->thisinst->thisobject;
/* "Hidden" objects are not drawn */
if (libobj->hidden == True) continue;
drawinst = spec->thisinst;
drawinst->position.x = 0;
drawinst->position.y = 0;
/* Get the bounding box of the instance in the page's coordinate system */
calcinstbbox((genericptr *)(&drawinst), &llx, &lly, &urx, &ury);
width = urx - llx;
height = ury - lly;
width += 30; /* space padding */
height += 30; /* height padding */
if (width < 200) width = 200; /* minimum box width */
if (height < 220) height = 220; /* minimum box height */
totalarea += (width * height);
}
scale = (double)totalarea / (double)(areawin->width * areawin->height);
targetwidth = (int)(sqrt(scale) * (double)areawin->width);
/* generate the list of object instances and their labels */
savemode = eventmode;
eventmode = CATALOG_MODE;
for (spec = xobjs.userlibs[mode - LIBRARY].instlist; spec != NULL;
spec = spec->next) {
libobj = spec->thisinst->thisobject;
/* "Hidden" objects are not drawn */
if (libobj->hidden == True) continue;
drawinst = spec->thisinst;
drawinst->position.x = 0;
drawinst->position.y = 0;
/* Generate the part; unlike the usual NEW_OBJINST, the */
/* instance record isn't allocated. */
PLIST_INCR(libpage);
*(libpage->plist + libpage->parts) = (genericptr)drawinst;
libpage->parts++;
/* Get the bounding box of the instance in the page's coordinate system */
calcinstbbox((genericptr *)(&drawinst), &llx, &lly, &urx, &ury);
xcenter = (llx + urx) >> 1;
width = urx - llx;
height = ury - lly;
/* Add an ad-hoc spacing rule of 30 for padding space between objects */
width += 30;
/* Prepare the object name and determine its width. Adjust width */
/* needed for object on the library page if necessary. */
if (fval < fontcount) {
stringpart *strptr;
TextExtents tmpext;
NEW_LABEL(drawname, libpage);
labeldefaults(*drawname, False, 0, 0);
(*drawname)->color = (spec->virtual) ?
OFFBUTTONCOLOR : DEFAULTCOLOR;
(*drawname)->scale = 0.75;
(*drawname)->string->data.font = fval;
strptr = makesegment(&((*drawname)->string), NULL);
strptr->type = TEXT_STRING;
strptr->data.string = strdup(libobj->name);
(*drawname)->anchor = TOP | NOTBOTTOM | NOTLEFT;
/* If the label is longer than the object width, then */
/* adjust positions accordingly. Note that as an ad- */
/* hoc spacing rule, a padding of 30 is put between */
/* objects; this padding is reduced to 5 */
tmpext = ULength(*drawname, drawinst, NULL);
/* Ad-hoc spacing rule is 5 for labels */
tmpext.width += 5;
if (tmpext.width > width)
width = tmpext.width;
}
/* Minimum allowed width is 200 */
if (width < 200) width = 200;
/* Determine the area needed on the page to draw the object */
nxpos = xpos + width;
if ((nxpos > targetwidth) && (xpos > 0)) {
nxpos -= xpos;
xpos = 0;
ypos -= nypos;
nypos = 200;
}
if (height > (nypos - 50)) nypos = height + 50;
drawinst->position.x = xpos + (width >> 1) - xcenter;
drawinst->position.y = ypos - (height + lly);
if (height <= 170) drawinst->position.y -= ((170 - height) >> 1);
drawinst->color = DEFAULTCOLOR;
if (fval < fontcount) {
(*drawname)->position.x = xpos + (width >> 1);
if (height > 170)
(*drawname)->position.y = drawinst->position.y + lly - 10;
else
(*drawname)->position.y = ypos - 180;
}
xpos = nxpos;
}
eventmode = savemode;
/* Compute the bounding box of the library page */
calcbbox(xobjs.libtop[mode]);
/* Update the library directory */
updatepagelib(LIBLIB, mode);
/* Restore original view position */
libpage->pcorner = savepos;
libpage->viewscale = savescale;
}
/*----------------------------------------------------------------*/
/* Find any dependencies on an object. */
/* Return values: 0 = no dependency, 1 = dependency on page, */
/* 2 = dependency in another library object. */
/* Object/Page with dependency (if any) returned in "compobjp". */
/*----------------------------------------------------------------*/
short finddepend(objinstptr libobj, objectptr **compobjp)
{
genericptr *testobj;
short page, i, j;
objectptr *compobj;
for (i = 0; i < xobjs.numlibs; i++) {
for (j = 0; j < xobjs.userlibs[i].number; j++) {
compobj = xobjs.userlibs[i].library + j;
*compobjp = compobj;
for (testobj = (*compobj)->plist; testobj < (*compobj)->plist
+ (*compobj)->parts; testobj++) {
if (IS_OBJINST(*testobj)) {
if (TOOBJINST(testobj)->thisobject == libobj->thisobject) return 2;
}
}
}
}
/* also look in the xobjs.pagelist */
for (page = 0; page < xobjs.pages; page++) {
if (xobjs.pagelist[page]->pageinst == NULL) continue;
compobj = &(xobjs.pagelist[page]->pageinst->thisobject);
*compobjp = compobj;
for (testobj = (*compobj)->plist; testobj < (*compobj)->plist
+ (*compobj)->parts; testobj++) {
if (IS_OBJINST(*testobj)) {
if (TOOBJINST(testobj)->thisobject == libobj->thisobject) return 1;
}
}
}
return 0;
}