-
Notifications
You must be signed in to change notification settings - Fork 24
/
elements.c
2753 lines (2352 loc) · 84.8 KB
/
elements.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
/*----------------------------------------------------------------------*/
/* elements.c --- xcircuit routines for creating basic elements */
/* Copyright (c) 2002 Tim Edwards, Johns Hopkins University */
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* written by Tim Edwards, 8/13/93 */
/*----------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.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
/*----------------------------------------------------------------------*/
/* Local includes */
/*----------------------------------------------------------------------*/
#include "xcircuit.h"
#include "colordefs.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 XCWindowData *areawin;
extern Globaldata xobjs;
extern xcWidget top;
extern fontinfo *fonts;
extern short fontcount;
extern char _STR[150], _STR2[250];
extern int number_colors;
extern colorindex *colorlist;
#if !defined(HAVE_CAIRO)
extern Pixmap dbuf;
#endif
extern double atan2();
/*------------------------------------------------------------------------*/
/* Declarations of global variables */
/*------------------------------------------------------------------------*/
char extchar[20];
double saveratio;
u_char texttype;
/*--------------------------------------*/
/* Element constructor functions */
/*--------------------------------------*/
/*--------------------------------------------------------------*/
/* Label constructor: Create a new label element in the object */
/* whose instance is "destinst" and return a pointer to it. */
/* */
/* "destinst" is the destination instance. If NULL, the */
/* top-level instance (areawin->topinstance) is used. */
/* "strptr" is a pointer to a stringpart string, and may */
/* be NULL. If non-NULL, should NOT be free'd by the */
/* calling routine. */
/* "pintype" is NORMAL, LOCAL, GLOBAL, or INFO */
/* "x" and "y" are the label coordinates. */
/* if "dochange" is FALSE, then this label is being drawn */
/* as part of a font or library and should not cause */
/* changes count to increment. */
/* */
/* Other properties must be set individually by the calling */
/* routine. */
/* */
/* Return value is a pointer to the newly created label. */
/*--------------------------------------------------------------*/
labelptr new_label(objinstptr destinst, stringpart *strptr, int pintype,
int x, int y, u_char dochange)
{
labelptr *newlab;
objectptr destobject;
objinstptr locdestinst;
locdestinst = (destinst == NULL) ? areawin->topinstance : destinst;
destobject = locdestinst->thisobject;
NEW_LABEL(newlab, destobject);
labeldefaults(*newlab, pintype, x, y);
if (strptr->type == FONT_NAME) {
free ((*newlab)->string);
(*newlab)->string = strptr;
}
else
(*newlab)->string->nextpart = strptr;
calcbboxvalues(locdestinst, (genericptr *)newlab);
updatepagebounds(destobject);
if (dochange != (u_char)0) incr_changes(destobject);
return *newlab;
}
/*--------------------------------------------------------------*/
/* Variant of the above; creates a label from a (char *) string */
/* instead of a stringpart pointer. Like the stringpart */
/* pointer above, "cstr" should NOT be free'd by the calling */
/* routine. */
/*--------------------------------------------------------------*/
labelptr new_simple_label(objinstptr destinst, char *cstr,
int pintype, int x, int y)
{
stringpart *strptr;
strptr = (stringpart *)malloc(sizeof(stringpart));
strptr->type = TEXT_STRING;
strptr->nextpart = NULL;
strptr->data.string = cstr;
return new_label(destinst, strptr, pintype, x, y, (u_char)0);
}
/*--------------------------------------------------------------*/
/* Another variant of the above; creates a "temporary" label */
/* from a (char *) string. As above, "cstr" should NOT be */
/* free'd by the calling routine. The "temporary" label has no */
/* font information, and cannot be displayed nor saved/loaded */
/* from the PostScript output file. Used to name networks or */
/* to mark port positions. Pin type is always LOCAL. Does not */
/* require updating bounding box info since it cannot be */
/* displayed. Consequently, only requires passing the object */
/* to get the new label, not its instance. */
/*--------------------------------------------------------------*/
labelptr new_temporary_label(objectptr destobject, char *cstr,
int x, int y)
{
labelptr *newlab;
NEW_LABEL(newlab, destobject);
labeldefaults(*newlab, LOCAL, x, y);
(*newlab)->string->type = TEXT_STRING; /* overwrites FONT record */
(*newlab)->string->data.string = cstr;
return *newlab;
}
/*--------------------------------------------------------------*/
/* Polygon constructor: Create a new polygon element in the */
/* object whose instance is "destinst" and return a pointer to */
/* it. */
/* */
/* "destinst" is the destination instance. If NULL, the */
/* top-level instance (areawin->topinstance) is used. */
/* "points" is a list of XPoint pointers, should not be */
/* NULL. It is transferred to the polygon verbatim, */
/* and should NOT be free'd by the calling routine. */
/* "number" is the number of points in the list, or zero */
/* if "points" is NULL. */
/* */
/* Other properties must be set individually by the calling */
/* routine. */
/* */
/* Return value is a pointer to the newly created polygon. */
/*--------------------------------------------------------------*/
polyptr new_polygon(objinstptr destinst, pointlist *points, int number)
{
polyptr *newpoly;
objectptr destobject;
objinstptr locdestinst;
locdestinst = (destinst == NULL) ? areawin->topinstance : destinst;
destobject = locdestinst->thisobject;
NEW_POLY(newpoly, destobject);
polydefaults(*newpoly, 0, 0, 0);
(*newpoly)->number = number;
(*newpoly)->points = *points;
calcbboxvalues(locdestinst, (genericptr *)newpoly);
updatepagebounds(destobject);
incr_changes(destobject);
return *newpoly;
}
/*--------------------------------------------------------------*/
/* Spline constructor: Create a new spline element in the */
/* object whose instance is "destinst" and return a pointer to */
/* it. */
/* */
/* "destinst" is the destination instance. If NULL, the */
/* top-level instance (areawin->topinstance) is used. */
/* "points" is a array of 4 XPoints; should not be NULL. */
/* */
/* Other properties must be set individually by the calling */
/* routine. */
/* */
/* Return value is a pointer to the newly created spline. */
/*--------------------------------------------------------------*/
splineptr new_spline(objinstptr destinst, pointlist points)
{
splineptr *newspline;
objectptr destobject;
objinstptr locdestinst;
int i;
locdestinst = (destinst == NULL) ? areawin->topinstance : destinst;
destobject = locdestinst->thisobject;
NEW_SPLINE(newspline, destobject);
splinedefaults(*newspline, 0, 0);
for (i = 0; i < 4; i++)
(*newspline)->ctrl[i] = points[i];
calcspline(*newspline);
calcbboxvalues(locdestinst, (genericptr *)newspline);
updatepagebounds(destobject);
incr_changes(destobject);
return *newspline;
}
/*--------------------------------------------------------------*/
/* Arc constructor: Create a new arc element in the object */
/* whose instance is "destinst" and return a pointer to it. */
/* */
/* "destinst" is the destination instance. If NULL, the */
/* top-level instance (areawin->topinstance) is used. */
/* "radius" is the radius of the (circular) arc. */
/* "x" and "y" represents the arc center position. */
/* */
/* Other properties must be set individually by the calling */
/* routine. */
/* */
/* Return value is a pointer to the newly created arc. */
/*--------------------------------------------------------------*/
arcptr new_arc(objinstptr destinst, int radius, int x, int y)
{
arcptr *newarc;
objectptr destobject;
objinstptr locdestinst;
locdestinst = (destinst == NULL) ? areawin->topinstance : destinst;
destobject = locdestinst->thisobject;
NEW_ARC(newarc, destobject);
arcdefaults(*newarc, x, y);
(*newarc)->radius = (*newarc)->yaxis = radius;
calcarc(*newarc);
calcbboxvalues(locdestinst, (genericptr *)newarc);
updatepagebounds(destobject);
incr_changes(destobject);
return *newarc;
}
/*--------------------------------------------------------------*/
/* Instance constructor: Create a new object instance element */
/* in the object whose instance is "destinst" and return a */
/* pointer to it. */
/* */
/* "destinst" is the destination instance. If NULL, the */
/* top-level instance (areawin->topinstance) is used. */
/* "srcinst" is the source instance of which this is a */
/* copy. */
/* "x" and "y" represents the instance position. */
/* */
/* Other properties must be set individually by the calling */
/* routine. */
/* */
/* Return value is a pointer to the newly created arc. */
/*--------------------------------------------------------------*/
objinstptr new_objinst(objinstptr destinst, objinstptr srcinst, int x, int y)
{
objinstptr *newobjinst;
objectptr destobject;
objinstptr locdestinst;
locdestinst = (destinst == NULL) ? areawin->topinstance : destinst;
destobject = locdestinst->thisobject;
NEW_OBJINST(newobjinst, destobject);
instcopy(*newobjinst, srcinst);
(*newobjinst)->position.x = x;
(*newobjinst)->position.y = y;
calcbboxvalues(locdestinst, (genericptr *)newobjinst);
updatepagebounds(destobject);
incr_changes(destobject);
return *newobjinst;
}
/*--------------------------------------------------------------*/
/* Generic element destructor function */
/* (Note---this function is not being used anywhere. . .) */
/*--------------------------------------------------------------*/
void remove_element(objinstptr destinst, genericptr genelem)
{
objectptr destobject;
objinstptr locdestinst;
locdestinst = (destinst == NULL) ? areawin->topinstance : destinst;
destobject = locdestinst->thisobject;
genelem->type &= REMOVE_TAG;
delete_tagged(locdestinst);
calcbboxvalues(locdestinst, (genericptr *)NULL);
updatepagebounds(destobject);
}
/*-------------------------------------*/
/* Sane values for a new path instance */
/*-------------------------------------*/
void pathdefaults(pathptr newpath, int x, int y)
{
UNUSED(x);
UNUSED(y);
newpath->style = NORMAL;
newpath->width = areawin->linewidth;
newpath->style = areawin->style;
newpath->color = areawin->color;
newpath->parts = 0;
newpath->plist = (genericptr *)NULL;
newpath->passed = NULL;
}
/*---------------------------------------*/
/* Sane values for a new object instance */
/*---------------------------------------*/
void instancedefaults(objinstptr newinst, objectptr thisobj, int x, int y)
{
newinst->position.x = x;
newinst->position.y = y;
newinst->rotation = 0.0;
newinst->scale = 1.0;
newinst->style = LINE_INVARIANT;
newinst->thisobject = thisobj;
newinst->color = areawin->color;
newinst->params = NULL;
newinst->passed = NULL;
newinst->bbox.lowerleft.x = thisobj->bbox.lowerleft.x;
newinst->bbox.lowerleft.y = thisobj->bbox.lowerleft.y;
newinst->bbox.width = thisobj->bbox.width;
newinst->bbox.height = thisobj->bbox.height;
newinst->schembbox = NULL;
}
/*--------------------------------------*/
/* Draw a dot at the current point. */
/*--------------------------------------*/
void drawdot(int xpos, int ypos)
{
arcptr *newarc;
objinstptr *newdot;
objectptr dotobj;
/* Find the object "dot" in the builtin library, or else use an arc */
if ((dotobj = finddot()) != (objectptr)NULL) {
NEW_OBJINST(newdot, topobject);
instancedefaults(*newdot, dotobj, xpos, ypos);
register_for_undo(XCF_Dot, UNDO_DONE, areawin->topinstance, *newdot);
}
else {
NEW_ARC(newarc, topobject);
arcdefaults(*newarc, xpos, ypos);
(*newarc)->radius = 6;
(*newarc)->yaxis = 6;
(*newarc)->width = 1.0;
(*newarc)->style = FILLED | FILLSOLID | NOBORDER;
(*newarc)->passed = NULL;
(*newarc)->cycle = NULL;
calcarc(*newarc);
register_for_undo(XCF_Arc, UNDO_DONE, areawin->topinstance, *newarc);
}
incr_changes(topobject);
}
/*--------------------------------------*/
/* Sane default values for a label */
/*--------------------------------------*/
void labeldefaults(labelptr newlabel, u_char dopin, int x, int y)
{
newlabel->rotation = 0.0;
newlabel->color = areawin->color;
newlabel->scale = areawin->textscale;
newlabel->string = (stringpart *)malloc(sizeof(stringpart));
newlabel->passed = NULL;
newlabel->cycle = NULL;
/* initialize string with font designator */
newlabel->string->type = FONT_NAME;
newlabel->string->data.font = areawin->psfont;
newlabel->string->nextpart = NULL;
newlabel->pin = dopin;
if (dopin == LOCAL) newlabel->color = LOCALPINCOLOR;
else if (dopin == GLOBAL) newlabel->color = GLOBALPINCOLOR;
else if (dopin == INFO) newlabel->color = INFOLABELCOLOR;
newlabel->anchor = areawin->anchor;
newlabel->position.x = x;
newlabel->position.y = y;
}
/*--------------------------------------*/
/* Button handler when creating a label */
/*--------------------------------------*/
void textbutton(u_char dopin, int x, int y)
{
labelptr *newlabel;
XPoint userpt;
short tmpheight, *newselect;
XDefineCursor(dpy, areawin->window, TEXTPTR);
W3printf("Click to end or cancel.");
if (fontcount == 0)
Wprintf("Warning: No fonts available!");
unselect_all();
NEW_LABEL(newlabel, topobject);
newselect = allocselect();
*newselect = topobject->parts - 1;
snap(x, y, &userpt);
labeldefaults(*newlabel, dopin, userpt.x, userpt.y);
tmpheight = (short)(TEXTHEIGHT * (*newlabel)->scale);
userpt.y -= ((*newlabel)->anchor & NOTBOTTOM) ?
(((*newlabel)->anchor & TOP) ? tmpheight : tmpheight / 2) : 0;
areawin->origin.x = userpt.x;
areawin->origin.y = userpt.y;
areawin->textpos = 1; /* Text position is *after* the font declaration */
text_mode_draw(xcDRAW_EDIT, *newlabel);
}
/*----------------------------------------------------------------------*/
/* Report on characters surrounding the current text position */
/*----------------------------------------------------------------------*/
#define MAXCHARS 10
void charreport(labelptr curlabel)
{
int i, locpos, cleft = 149;
stringpart *strptr;
_STR2[0] = '\0';
for (i = areawin->textpos - MAXCHARS; i <= areawin->textpos + MAXCHARS - 1; i++) {
if (i < 0) continue;
strptr = findstringpart(i, &locpos, curlabel->string, areawin->topinstance);
if (i == areawin->textpos) {
strncat(_STR2, "| ", cleft);
cleft -= 2;
}
if (strptr == NULL) break;
if (strptr->type != RETURN || strptr->data.flags == 0) {
charprint(_STR, strptr, locpos);
cleft -= strlen(_STR);
strncat(_STR2, _STR, cleft);
strncat(_STR2, " ", --cleft);
if (cleft <= 0) break;
}
}
W3printf("%s", _STR2);
}
/*----------------------------------------------------------------------*/
/* See if a (pin) label has a copy (at least one) in this drawing. */
/*----------------------------------------------------------------------*/
labelptr findlabelcopy(labelptr curlabel, stringpart *curstring)
{
genericptr *tgen;
labelptr tlab;
for (tgen = topobject->plist; tgen < topobject->plist + topobject->parts; tgen++) {
if (IS_LABEL(*tgen)) {
tlab = TOLABEL(tgen);
if (tlab->pin != LOCAL) continue;
else if (tlab == curlabel) continue; /* Don't count self! */
else if (!stringcomp(tlab->string, curstring)) return tlab;
}
}
return NULL;
}
/*--------------------------------------------------------------*/
/* Interpret string and add to current label. */
/* keypressed is a KeySym */
/* clientdata can pass information for label controls */
/* */
/* Return TRUE if labeltext handled the character, FALSE if the */
/* character was not recognized. */
/*--------------------------------------------------------------*/
Boolean labeltext(int keypressed, char *clientdata)
{
labelptr curlabel;
stringpart *curpos, *labelbuf;
int locpos;
Boolean r = True, do_redraw = False;
short cfont;
TextExtents tmpext;
TechPtr oldtech, newtech;
curlabel = TOLABEL(EDITPART);
if (curlabel == NULL || curlabel->type != LABEL) {
Wprintf("Error: Bad label string");
text_mode_draw(xcDRAW_EMPTY, curlabel);
eventmode = NORMAL_MODE;
return FALSE;
}
/* find text segment of the current position */
curpos = findstringpart(areawin->textpos, &locpos, curlabel->string,
areawin->topinstance);
if (clientdata != NULL && keypressed == TEXT_DELETE) {
if (areawin->textpos > 1) {
int curloc, strpos;
stringpart *strptr;
if (areawin->textend == 0) areawin->textend = areawin->textpos - 1;
undrawtext(curlabel);
for (strpos = areawin->textpos - 1; strpos >= areawin->textend; strpos--) {
strptr = findstringpart(strpos, &curloc, curlabel->string,
areawin->topinstance);
if (curloc >= 0) {
memmove(strptr->data.string + curloc,
strptr->data.string + curloc + 1,
strlen(strptr->data.string + curloc + 1) + 1);
if (strlen(strptr->data.string) == 0)
deletestring(strptr, &curlabel->string, areawin->topinstance);
}
/* Don't delete any parameter boundaries---must use */
/* "unparameterize" command for that. */
else if (strptr != NULL) {
if ((strptr->type != PARAM_START) && (strptr->type != PARAM_END))
deletestring(strptr, &curlabel->string, areawin->topinstance);
else
areawin->textpos++;
}
else
Fprintf(stdout, "Error: Unexpected NULL string part\n");
areawin->textpos--;
}
areawin->textend = 0;
do_redraw = True;
}
}
else if (clientdata != NULL && keypressed == TEXT_DEL_PARAM) {
if (areawin->textpos > 1) {
int curloc, strpos;
stringpart *strptr;
strpos = areawin->textpos - 1;
strptr = findstringpart(strpos, &curloc, curlabel->string,
areawin->topinstance);
if (curloc > 0) strpos -= curloc; /* move to beginning of string */
if ((strptr != NULL) && (strptr->type != PARAM_START) && (strpos > 0)) {
strpos--;
strptr = findstringpart(strpos--, &curloc, curlabel->string,
areawin->topinstance);
}
if ((strptr != NULL) && (strptr->type == PARAM_START)) {
if (strpos >= 0) {
undrawtext(curlabel);
unmakeparam(curlabel, areawin->topinstance, strptr);
do_redraw = True;
}
}
}
}
else if (clientdata != NULL && keypressed == TEXT_RETURN) {
Boolean hasstuff = False; /* Check for null string */
stringpart *tmppos;
for (tmppos = curlabel->string; tmppos != NULL; tmppos = tmppos->nextpart) {
if (tmppos->type == PARAM_START) hasstuff = True;
else if (tmppos->type == TEXT_STRING) hasstuff = True;
}
XDefineCursor(dpy, areawin->window, DEFAULTCURSOR);
W3printf("");
if (hasstuff && (eventmode != ETEXT_MODE && eventmode != CATTEXT_MODE)) {
register_for_undo(XCF_Text, UNDO_MORE, areawin->topinstance,
curlabel);
incr_changes(topobject);
invalidate_netlist(topobject);
}
else if (!hasstuff && (eventmode == ETEXT_MODE)) {
if (*(areawin->selectlist) < topobject->parts) {
/* Force the "delete" undo record to be a continuation of */
/* the undo series containing the edit. That way, "undo" */
/* does not generate a label with null text. */
xobjs.undostack->idx = -xobjs.undostack->idx;
standard_element_delete(NORMAL);
}
else {
/* Label had just been created; just delete it w/o undo */
freelabel(curlabel->string);
free(curlabel);
topobject->parts--;
unselect_all();
}
}
if ((!hasstuff) && (eventmode == CATTEXT_MODE)) { /* can't have null labels! */
undo_action();
redrawtext(curlabel);
areawin->redraw_needed = False; /* ignore previous redraw requests */
text_mode_draw(xcDRAW_FINAL, curlabel);
Wprintf("Object must have a name!");
eventmode = CATALOG_MODE;
}
else if (!hasstuff) {
areawin->redraw_needed = False; /* ignore previous redraw requests */
text_mode_draw(xcDRAW_FINAL, curlabel);
eventmode = NORMAL_MODE;
}
else if (eventmode == CATTEXT_MODE) {
objectptr libobj;
stringpart *oldname;
int page, libnum;
/* Get the library object whose name matches the original string */
oldname = get_original_string(curlabel);
if ((libobj = NameToObject(oldname->nextpart->data.string, NULL, FALSE))
!= NULL) {
/* Set name of object to new string. Don't overwrite the */
/* object's technology *unless* the new string has a */
/* namespace, in which case the object's technology gets */
/* changed. */
char *techptr, *libobjname = libobj->name;
if ((techptr = strstr(libobjname, "::")) != NULL &&
(strstr(curlabel->string->nextpart->data.string, "::")
== NULL))
libobjname = techptr + 2;
/* Save a pointer to the old technology before we overwrite the name */
oldtech = GetObjectTechnology(libobj);
strcpy(libobjname, curlabel->string->nextpart->data.string);
/* If checkname() alters the name, it has to be copied back to */
/* the catalog label for the object. */
if (checkname(libobj)) {
undrawtext(curlabel);
curlabel->string->nextpart->data.string = (char *)realloc(
curlabel->string->nextpart->data.string,
(strlen(libobj->name) + 1) * sizeof(char));
strcpy(curlabel->string->nextpart->data.string, libobj->name);
redrawtext(curlabel);
}
AddObjectTechnology(libobj);
/* If the technology name has changed, then both the old */
/* technology and the new technology need to be marked as */
/* having been modified. */
newtech = GetObjectTechnology(libobj);
if (oldtech != newtech) {
if (oldtech) oldtech->flags |= (u_char)TECH_CHANGED;
if (newtech) newtech->flags |= (u_char)TECH_CHANGED;
}
}
/* Check if we altered a page name */
else if ((libobj = NameToPageObject(oldname->nextpart->data.string,
NULL, &page)) != NULL) {
strcpy(libobj->name, curlabel->string->nextpart->data.string);
renamepage(page);
}
/* Check if we altered a library name */
else if ((libnum = NameToLibrary(oldname->nextpart->data.string)) != -1) {
libobj = xobjs.libtop[libnum + LIBRARY]->thisobject;
strcpy(libobj->name, curlabel->string->nextpart->data.string);
}
else {
Wprintf("Error: Cannot match name to any object, page, or library!");
refresh(NULL, NULL, NULL);
}
areawin->redraw_needed = False; /* ignore previous redraw requests */
text_mode_draw(xcDRAW_FINAL, curlabel);
eventmode = CATALOG_MODE;
}
else { /* (hasstuff && eventmode != CATTEXT_MODE) */
areawin->redraw_needed = False; /* ignore previous redraw requests */
text_mode_draw(xcDRAW_FINAL, curlabel);
eventmode = NORMAL_MODE;
incr_changes(topobject);
if (curlabel->pin != False) invalidate_netlist(topobject);
}
setdefaultfontmarks();
setcolormark(areawin->color);
if (!hasstuff) {
/* Delete empty labels */
standard_element_delete(NORMAL);
}
else if ((labelbuf = get_original_string(curlabel)) != NULL) {
/* If the original label (before modification) is a pin in a */
/* schematic/symbol with a matching symbol/schematic, and the */
/* name is unique, change every pin in the matching symbol/ */
/* schematic to match the new text. */
if ((curlabel->pin == LOCAL) && (topobject->symschem != NULL) &&
(topobject->symschem->schemtype != PRIMARY)) {
if ((findlabelcopy(curlabel, labelbuf) == NULL)
&& (findlabelcopy(curlabel, curlabel->string) == NULL)) {
if (changeotherpins(curlabel, labelbuf) > 0) {
if (topobject->schemtype == PRIMARY ||
topobject->schemtype == SECONDARY)
Wprintf("Changed corresponding pin in associated symbol");
else
Wprintf("Changed corresponding pin in associated schematic");
incr_changes(topobject->symschem);
invalidate_netlist(topobject->symschem);
}
}
}
resolveparams(areawin->topinstance);
updateinstparam(topobject);
setobjecttype(topobject);
}
else
calcbbox(areawin->topinstance);
unselect_all();
return r;
}
else if (clientdata != NULL && keypressed == TEXT_RIGHT) {
if (curpos != NULL) areawin->textpos++;
}
else if (clientdata != NULL && keypressed == TEXT_LEFT) {
if (areawin->textpos > 1) areawin->textpos--;
}
else if (clientdata != NULL && keypressed == TEXT_DOWN) {
while (curpos != NULL) {
areawin->textpos++;
curpos = findstringpart(areawin->textpos, &locpos, curlabel->string,
areawin->topinstance);
if (curpos != NULL)
if (curpos->type == RETURN || curpos->type == MARGINSTOP)
break;
}
}
else if (clientdata != NULL && keypressed == TEXT_UP) {
while (areawin->textpos > 1) {
areawin->textpos--;
curpos = findstringpart(areawin->textpos, &locpos, curlabel->string,
areawin->topinstance);
if (curpos->type == RETURN || curpos->type == MARGINSTOP) {
if (areawin->textpos > 1) areawin->textpos--;
break;
}
}
}
else if (clientdata != NULL && keypressed == TEXT_HOME)
areawin->textpos = 1;
else if (clientdata != NULL && keypressed == TEXT_END)
areawin->textpos = stringlength(curlabel->string, True, areawin->topinstance);
else if (clientdata != NULL && keypressed == TEXT_SPLIT) {
labelptr *newlabel;
XPoint points[4], points1[4], points2[4];
/* Everything after the cursor gets dumped into a new label */
if ((areawin->textpos > 1) && (curpos != NULL)) {
labelbbox(curlabel, points, areawin->topinstance);
undrawtext(curlabel);
NEW_LABEL(newlabel, topobject);
labeldefaults(*newlabel, curlabel->pin, curlabel->position.x,
curlabel->position.y);
if (locpos > 0)
curpos = splitstring(areawin->textpos, &curlabel->string,
areawin->topinstance);
/* move back one position to find end of top part of string */
curpos = splitstring(areawin->textpos - 1, &curlabel->string,
areawin->topinstance);
if (curpos->nextpart->type == FONT_NAME) {
freelabel((*newlabel)->string);
(*newlabel)->string = curpos->nextpart;
}
else {
(*newlabel)->string->data.font = curlabel->string->data.font;
(*newlabel)->string->nextpart = curpos->nextpart;
}
curpos->nextpart = NULL;
/* Adjust position of both labels to retain their original */
/* relative positions. */
labelbbox(curlabel, points1, areawin->topinstance);
labelbbox((*newlabel), points2, areawin->topinstance);
curlabel->position.x += (points[1].x - points1[1].x);
curlabel->position.y += (points[1].y - points1[1].y);
(*newlabel)->position.x += (points[3].x - points2[3].x);
(*newlabel)->position.y += (points[3].y - points2[3].y);
redrawtext(*newlabel);
do_redraw = True;
}
}
/* Write a font designator or other control into the string */
else if (clientdata != NULL) {
oparamptr ops;
stringpart *newpart;
Boolean errcond = False;
TextLinesInfo tlinfo;
/* erase first before redrawing unless the string is empty */
undrawtext(curlabel);
/* Get text width first. Don't back up over spaces; this */
/* allows the margin width to be padded out with spaces. */
if (keypressed == MARGINSTOP) {
tlinfo.dostop = areawin->textpos;
tlinfo.tbreak = NULL;
tlinfo.padding = NULL;
tmpext = ULength(curlabel, areawin->topinstance, &tlinfo);
if (tlinfo.padding != NULL) free(tlinfo.padding);
}
if (locpos > 0) {
if (keypressed == MARGINSTOP) {
/* Move forward by any spaces; if we're at the text */
/* end, move to the next text part; otherwise, */
/* split the string. */
while (*(curpos->data.string + locpos) == ' ') locpos++;
if (*(curpos->data.string + locpos) == '\0') locpos = 0;
}
if (locpos > 0)
curpos = splitstring(areawin->textpos, &curlabel->string,
areawin->topinstance);
curpos = curpos->nextpart;
}
newpart = makesegment(&curlabel->string, curpos);
newpart->type = keypressed;
switch (keypressed) {
case RETURN:
/* Identify this as an explicitly placed line break */
newpart->data.flags = 0;
break;
case FONT_SCALE:
newpart->data.scale = *((float *)clientdata);
break;
case KERN:
newpart->data.kern[0] = *((short *)clientdata);
newpart->data.kern[1] = *((short *)clientdata + 1);
break;
case FONT_COLOR:
newpart->data.color = *((int *)clientdata);
if (newpart->data.color >= number_colors) errcond = True;
break;
case FONT_NAME:
newpart->data.font = *((int *)clientdata);
if (newpart->data.font >= fontcount) errcond = True;
break;
case MARGINSTOP:
/* A margin of 1 or 0 is useless, so such a value */
/* indicates to take the margin from the current */
/* position. */
if (*((int *)clientdata) <= 1)
newpart->data.width = (int)tmpext.width;
else
newpart->data.width = *((int *)clientdata);
CheckMarginStop(curlabel, areawin->topinstance, FALSE);
break;
case PARAM_START:
newpart->data.string = (char *)malloc(1 + strlen(clientdata));
strcpy(newpart->data.string, clientdata);
ops = match_param(topobject, clientdata);
if (ops == NULL) errcond = True;
else {
/* Forward edit cursor position to the end of the parameter */
do {
areawin->textpos++;
curpos = findstringpart(areawin->textpos, &locpos, curlabel->string,
areawin->topinstance);
} while (curpos && (curpos->type != PARAM_END));
}
break;
}
if (errcond == True) {
Wprintf("Error in insertion. Ignoring.");
deletestring(newpart, &curlabel->string, areawin->topinstance);
r = FALSE;
}
else {
areawin->textpos++;
}
do_redraw = True;
}
/* Append the character to the string. If the current label segment is */
/* not text, then create a text segment for it. */
else if (keypressed > 0 && keypressed < 256) {
stringpart *lastpos;
/* erase first. */
undrawtext(curlabel);
/* Current position is not in a text string */
if (locpos < 0) {
/* Find part of string which is immediately in front of areawin->textpos */
lastpos = findstringpart(areawin->textpos - 1, &locpos, curlabel->string,
areawin->topinstance);
/* No text on either side to attach to: make a new text segment */
if (locpos < 0) {
curpos = makesegment(&curlabel->string, curpos);
curpos->type = TEXT_STRING;
curpos->data.string = (u_char *) malloc(2);
curpos->data.string[0] = keypressed;
curpos->data.string[1] = '\0';
}
else { /* append to end of lastpos text string */
int slen = strlen(lastpos->data.string);
lastpos->data.string = (u_char *) realloc(lastpos->data.string,
2 + slen);
*(lastpos->data.string + slen) = keypressed;
*(lastpos->data.string + slen + 1) = '\0';
}
}
else { /* prepend to end of curpos text string */
curpos->data.string = (u_char *) realloc(curpos->data.string,
2 + strlen(curpos->data.string));
memmove(curpos->data.string + locpos + 1, curpos->data.string + locpos,
strlen(curpos->data.string + locpos) + 1);
*(curpos->data.string + locpos) = keypressed;
}
areawin->textpos++; /* move forward to next text position */
do_redraw = True;
r = TRUE;
}
/* Redraw the label */
if (do_redraw) {
/* Generate automatic line breaks if there is a MARGINSTOP directive */
CheckMarginStop(curlabel, areawin->topinstance, TRUE);
redrawtext(curlabel);
}