-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathundo.c
More file actions
1782 lines (1569 loc) · 51.6 KB
/
undo.c
File metadata and controls
1782 lines (1569 loc) · 51.6 KB
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
/*----------------------------------------------------------------------*/
/* undo.c */
/* */
/* The comprehensive "undo" and "redo" command handler */
/* */
/* Copyright (c) 2004 Tim Edwards, Open Circuit Design, Inc., and */
/* MultiGiG, Inc. */
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* written by Tim Edwards, 1/29/04 */
/*----------------------------------------------------------------------*/
#define MODE_UNDO (u_char)0
#define MODE_REDO (u_char)1
#define MAX_UNDO_EVENTS 100
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#ifndef XC_WIN32
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#endif
/*----------------------------------------------------------------------*/
/* Local includes */
/*----------------------------------------------------------------------*/
#ifdef TCL_WRAPPER
#include <tk.h>
#endif
#include "xcircuit.h"
/*----------------------------------------------------------------------*/
/* Function prototype declarations */
/*----------------------------------------------------------------------*/
#include "prototypes.h"
/*----------------------------------------------------------------------*/
/* Local structure definitions for holding undo information */
/*----------------------------------------------------------------------*/
typedef struct {
float angle1;
float angle2;
short radius;
short yaxis;
XPoint position;
} arcinfo;
/* Smaller data structure used when saving paths in the undo stacks. */
/* We don't need the rendering data from points[]. */
typedef struct {
u_short type;
int color;
eparamptr passed;
u_short style;
float width;
XPoint ctrl[4];
} splineinfo;
typedef struct {
int number;
pointlist points;
} pathinfo;
typedef struct editelem {
genericptr element; /* element being edited */
union {
stringpart *string; /* original contents of string, for label */
pointlist points; /* original polygon */
arcinfo *arcspecs; /* original arc values */
pathinfo *pathspecs; /* original parts of a path */
XPoint instpos; /* original position, for an object instance */
} save;
} editelement;
typedef struct {
genericptr element; /* element modified */
float scale; /* old scale value */
} scaleinfo;
typedef struct {
XPoint rotpos; /* original position */
float rotation; /* old rotation value */
} rotateinfo;
u_char undo_collect = (u_char)0;
/*----------------------------------------------------------------------*/
/* Externally declared variables */
/*----------------------------------------------------------------------*/
extern Globaldata xobjs;
extern XCWindowData *areawin;
/*----------------------------------------------------------------------*/
/* Attempt to set the window. If the window exists, set it as current */
/* and return TRUE. Otherwise, return FALSE. This prevents the undo */
/* mechanism from crashing if we close a window and then try to undo */
/* events that were created relative to it. */
/*----------------------------------------------------------------------*/
Boolean setwindow(XCWindowData *trywindow)
{
XCWindowData *chkwin;
for (chkwin = xobjs.windowlist; chkwin != NULL; chkwin = chkwin->next) {
if (chkwin == trywindow) {
areawin = trywindow;
return TRUE;
}
}
return FALSE;
}
/*----------------------------------------------------------------------*/
/* remember_selection --- */
/* */
/* Copy a selection list into a "uselection" record. The uselection */
/* record maintains the order of the elements as well as pointers to */
/* each element, so the original element ordering can be recovered. */
/*----------------------------------------------------------------------*/
uselection *remember_selection(objinstptr topinst, short *slist, int number)
{
int i, idx;
uselection *newlist;
newlist = (uselection *)malloc(sizeof(uselection));
if (number > 0) {
newlist->element = (genericptr *)malloc(number * sizeof(genericptr));
newlist->idx = (short *)malloc(number * sizeof(short));
}
else {
newlist->element = NULL;
newlist->idx = NULL;
}
newlist->number = number;
for (i = 0; i < number; i++) {
idx = *(slist + i);
*(newlist->element + i) = *(topinst->thisobject->plist + idx);
*(newlist->idx + i) = idx;
}
return newlist;
}
/*----------------------------------------------------------------------*/
/* Create a selection list in areawin from the saved uselection */
/* record. */
/*----------------------------------------------------------------------*/
short *regen_selection(objinstptr thisinst, uselection *srec)
{
int i, j, k;
genericptr egen;
objectptr thisobj = thisinst->thisobject;
Boolean reorder = False;
short *slist;
if (srec->number > 0)
slist = (short *)malloc(srec->number * sizeof(short));
k = 0;
for (i = 0; i < srec->number; i++) {
/* Use the element address, not the selection order. */
egen = *(srec->element + i);
if (egen == *(thisobj->plist + *(srec->idx + i)))
j = *(srec->idx + i);
else {
reorder = True;
for (j = 0; j < thisobj->parts; j++) {
if (egen == *(thisobj->plist + j))
break;
}
}
if (j < thisobj->parts) {
*(slist + k) = j;
k++;
}
else
Fprintf(stderr, "Error: element %p in select list but not object\n",
egen);
}
/* If the selection order is different from the order in the object, */
/* then rearrange the object's list to match the selection order. */
if (reorder) {
/* (to be done) */
}
if (k == 0) {
if (srec->number > 0) free(slist);
return NULL;
}
else
return slist;
}
/*----------------------------------------------------------------------*/
/* Use the selection list in the undo record to reorder parts in the */
/* object's part list. Invert the selection list and return it. */
/*----------------------------------------------------------------------*/
void reorder_selection(Undoptr thisrecord)
{
short *slist, *newlist, snum, i;
genericptr *pgen, *plist, egen;
objinstptr thisinst = thisrecord->thisinst;
objectptr thisobj = thisinst->thisobject;
snum = (short)thisrecord->idata;
slist = (short *)thisrecord->undodata;
plist = (genericptr *)malloc(snum * sizeof(genericptr));
newlist = (short *)malloc(snum * sizeof(short));
i = 0;
for (pgen = plist; pgen < plist + snum; pgen++) {
egen = *(thisobj->plist + i);
*(plist + *(slist + i)) = egen;
i++;
}
i = 0;
for (pgen = plist; pgen < plist + snum; pgen++) {
*(thisobj->plist + i) = *pgen;
*(newlist + *(slist + i)) = i;
i++;
}
free(plist);
free(thisrecord->undodata);
thisrecord->undodata = (char *)newlist;
}
/*----------------------------------------------------------------------*/
/* free_editelement --- */
/* */
/* Free memory allocated to an undo record edit element structure. */
/*----------------------------------------------------------------------*/
void free_editelement(Undoptr thisrecord)
{
editelement *erec;
pathinfo *ppi;
erec = (editelement *)thisrecord->undodata;
switch (erec->element->type) {
case LABEL:
freelabel(erec->save.string);
break;
case POLYGON: case SPLINE:
free(erec->save.points);
break;
case ARC:
free(erec->save.arcspecs);
break;
case PATH:
for (ppi = erec->save.pathspecs; ppi < erec->save.pathspecs +
thisrecord->idata; ppi++)
free(ppi->points);
free(erec->save.pathspecs);
break;
}
free(erec);
}
/*----------------------------------------------------------------------*/
/* free_selection --- */
/* */
/* Free memory allocated to an undo record selection list. */
/*----------------------------------------------------------------------*/
void free_selection(uselection *selrec)
{
if (selrec->number > 0) {
free(selrec->element);
free(selrec->idx);
}
free(selrec);
}
/*----------------------------------------------------------------------*/
/* get_original_string --- */
/* */
/* Find the original version of the given label. */
/*----------------------------------------------------------------------*/
stringpart *get_original_string(labelptr thislab)
{
Undoptr chkrecord, thisrecord;
editelement *erec;
labelptr elab;
thisrecord = xobjs.undostack;
if (thisrecord == NULL) return NULL;
for (chkrecord = thisrecord; chkrecord != NULL; chkrecord = chkrecord->next) {
switch (chkrecord->type) {
case XCF_Edit:
erec = (editelement *)(chkrecord->undodata);
elab = (labelptr)(erec->element);
if (elab != thislab) return NULL;
return erec->save.string;
default:
return NULL;
}
}
return NULL; /* yes, this can be reached, if thisrecord->next == NULL */
}
/*----------------------------------------------------------------------*/
/* select_previous --- */
/* */
/* Set the selection to what was previously selected in the undo list. */
/* Return 0 on success, -1 if no previous selection was found. */
/*----------------------------------------------------------------------*/
int select_previous(Undoptr thisrecord)
{
Undoptr chkrecord;
uselection *srec;
clearselects_noundo();
for (chkrecord = thisrecord->next; chkrecord != NULL; chkrecord = chkrecord->next) {
/* Selections may cross page changes, but only if in the same series */
if ((chkrecord->thisinst != thisrecord->thisinst) &&
(chkrecord->idx != thisrecord->idx))
break;
switch (chkrecord->type) {
case XCF_Delete: case XCF_Pop: case XCF_Push:
/* Delete/Push/Pop records imply a canceled selection */
return 0;
case XCF_Copy:
case XCF_Select:
srec = (uselection *)chkrecord->undodata;
areawin->selectlist = regen_selection(thisrecord->thisinst, srec);
areawin->selects = (areawin->selectlist) ? srec->number : 0;
return 0;
}
}
return -1;
}
/*----------------------------------------------------------------------*/
/* This is similar to the above routine, but we just return a pointer */
/* to the index list in the uselection record. This lets the undelete */
/* function restore the original ordering of parts that were deleted. */
/*----------------------------------------------------------------------*/
short *recover_selectlist(Undoptr thisrecord)
{
Undoptr chkrecord;
uselection *srec;
for (chkrecord = thisrecord->next; chkrecord != NULL; chkrecord = chkrecord->next) {
/* Selections may cross page changes, but only if in the same series */
if ((chkrecord->thisinst != thisrecord->thisinst) &&
(chkrecord->idx != thisrecord->idx))
break;
switch (chkrecord->type) {
case XCF_Delete: case XCF_Pop: case XCF_Push:
/* Delete/Push/Pop records imply a canceled selection */
return NULL;
case XCF_Copy:
/* Copy is the same as Select, but the copied objects are */
/* always at the end of the element list. Returning NULL */
/* is the same as declaring that elements should be */
/* appended to the end of the object's element list. */
return NULL;
case XCF_Select:
srec = (uselection *)chkrecord->undodata;
return srec->idx;
}
}
return NULL;
}
/*----------------------------------------------------------------------*/
/* Put element "thiselem" back into object "thisobj". This is only */
/* used in the case where the elements were previously at the end of */
/* the object's element list. */
/*----------------------------------------------------------------------*/
void undelete_one_element(objinstptr thisinst, genericptr thiselem)
{
objectptr thisobj = thisinst->thisobject;
PLIST_INCR(thisobj);
*(thisobj->plist + thisobj->parts) = thiselem;
thisobj->parts++;
}
/*----------------------------------------------------------------------*/
/* register_for_undo --- */
/* */
/* Register an event with the undo handler. This creates a record */
/* based on the type, which is one of the XCF_* bindings (see */
/* xcircuit.h for the list). This is a variable-argument routine, */
/* with the arguments dependent on the command. */
/* */
/* thisinst is the instance of the object in which the event occurred */
/* and is registered for every event type. */
/* */
/* "mode" is UNDO_MORE if one or more undo records are expected to */
/* follow in a series, or UNDO_DONE if this completes a series, or is */
/* as single undo event. The command-line command "undo series start" */
/* forces all events to be UNDO_MORE until "undo series end" is */
/* issued. */
/*----------------------------------------------------------------------*/
void register_for_undo(u_int type, u_char mode, objinstptr thisinst, ...)
{
va_list args;
int drawmode, nval, oval, *idata, snum, deltax, deltay, i;
double dir;
short *slist;
objectptr delobj;
objinstptr newinst;
Undoptr newrecord;
uselection *srec;
editelement *erec;
scaleinfo *escale;
genericptr egen;
XPoint *fpoint;
double scale;
/* Do not register new events while running undo/redo actions! */
if (eventmode == UNDO_MODE) return;
/* This action invalidates everything in the "redo" stack, so flush it */
flush_redo_stack();
/* Create the new record and push it onto the stack */
newrecord = (Undoptr)malloc(sizeof(Undostack));
newrecord->next = xobjs.undostack;
newrecord->last = NULL;
newrecord->type = type;
newrecord->thisinst = thisinst;
newrecord->window = areawin;
newrecord->undodata = (char *)NULL;
newrecord->idata = 0;
if (xobjs.undostack) {
xobjs.undostack->last = newrecord;
if (xobjs.undostack->idx < 0) {
xobjs.undostack->idx = -xobjs.undostack->idx;
newrecord->idx = xobjs.undostack->idx;
}
else
newrecord->idx = xobjs.undostack->idx + 1;
}
else
newrecord->idx = 1;
if (mode == UNDO_MORE || undo_collect > (u_char)0)
newrecord->idx = -newrecord->idx;
xobjs.undostack = newrecord;
va_start(args, thisinst);
switch(type) {
case XCF_Delete:
/* 2 args: */
/* delobj = pointer to object containing deleted entries */
/* drawmode = true if elements should be erased */
delobj = va_arg(args, objectptr);
drawmode = va_arg(args, int);
newrecord->undodata = (char *)delobj;
newrecord->idata = drawmode;
break;
case XCF_Select_Save:
/* 1 arg: */
/* newobj = pointer to instance of new object */
newinst = va_arg(args, objinstptr);
newrecord->undodata = (char *)newinst;
break;
case XCF_Page:
/* 2 args: */
/* oval = original integer value */
/* nval = new integer value */
oval = va_arg(args, int);
nval = va_arg(args, int);
idata = (int *)malloc(sizeof(int));
*idata = nval;
newrecord->undodata = (char *)idata;
newrecord->idata = oval;
break;
case XCF_Pop:
/* No args; instance to pop is the instance passed. */
break;
case XCF_Push:
/* 1 arg: */
/* newinst = object instance to push */
newinst = va_arg(args, objinstptr);
newrecord->undodata = (char *)newinst;
break;
case XCF_Copy:
case XCF_Select:
case XCF_Library_Pop:
/* 2 args: */
/* slist = current selection list (short *) */
/* snum = number of selections (int) */
slist = va_arg(args, short *);
snum = va_arg(args, int);
srec = remember_selection(thisinst, slist, snum);
newrecord->undodata = (char *)srec;
/* Fprintf(stdout, "Undo: registered selection or copy action\n"); */
break;
case XCF_Box: case XCF_Arc: case XCF_Wire: case XCF_Text:
case XCF_Pin_Label: case XCF_Pin_Global: case XCF_Info_Label:
case XCF_Spline: case XCF_Dot: case XCF_Graphic: case XCF_Join:
/* 1 arg: */
/* egen = element just created (genericptr) */
egen = va_arg(args, genericptr);
newrecord->undodata = (char *)egen;
break;
case XCF_Edit:
/* 1 arg: */
/* egen = element to be edited (genericptr) */
egen = va_arg(args, genericptr);
/* Create a copy of the element to save */
erec = (editelement *)malloc(sizeof(editelement));
erec->element = egen;
switch(egen->type) {
case LABEL:
erec->save.string =
stringcopyall(((labelptr)egen)->string,
areawin->topinstance);
newrecord->idata = ((labelptr)egen)->anchor;
break;
case POLYGON:
newrecord->idata = ((polyptr)egen)->number;
erec->save.points = copypoints(((polyptr)egen)->points,
newrecord->idata);
break;
case SPLINE:
erec->save.points =
copypoints((pointlist)((splineptr)egen)->ctrl, 4);
break;
case OBJINST:
erec->save.instpos = ((objinstptr)egen)->position;
break;
case ARC:
erec->save.arcspecs = (arcinfo *)malloc(sizeof(arcinfo));
erec->save.arcspecs->angle1 = ((arcptr)egen)->angle1;
erec->save.arcspecs->angle2 = ((arcptr)egen)->angle2;
erec->save.arcspecs->radius = ((arcptr)egen)->radius;
erec->save.arcspecs->yaxis = ((arcptr)egen)->yaxis;
erec->save.arcspecs->position = ((arcptr)egen)->position;
break;
case PATH:
newrecord->idata = ((pathptr)egen)->parts; /* not needed? */
erec->save.pathspecs = (pathinfo *)malloc(newrecord->idata *
sizeof(pathinfo));
for (i = 0; i < newrecord->idata; i++) {
pathinfo *ppi = erec->save.pathspecs + i;
genericptr *pgen = ((pathptr)egen)->plist + i;
switch (ELEMENTTYPE(*pgen)) {
case POLYGON:
ppi->number = (TOPOLY(pgen))->number;
ppi->points = copypoints((TOPOLY(pgen))->points,
ppi->number);
break;
case SPLINE:
ppi->number = 4;
ppi->points = copypoints((pointlist)
(TOSPLINE(pgen))->ctrl, 4);
break;
}
}
break;
}
newrecord->undodata = (char *)erec;
break;
case XCF_ChangeStyle:
case XCF_Anchor:
case XCF_Color:
/* 2 args: */
/* egen = element that was changed (with new value) */
/* oval = old style value */
egen = va_arg(args, genericptr);
oval = va_arg(args, int);
newrecord->undodata = (char *)egen;
newrecord->idata = oval;
break;
case XCF_Rescale:
/* 2 args: */
/* egen = element that was changed (with new value) */
/* ofloat = old float value */
egen = va_arg(args, genericptr);
scale = va_arg(args, double); /* warning! only takes "double"! */
escale = (scaleinfo *)malloc(sizeof(scaleinfo));
escale->element = egen;
escale->scale = (float)scale;
newrecord->undodata = (char *)escale;
break;
case XCF_Flip_X: case XCF_Flip_Y:
/* 1 arg: */
/* fpoint = point of flip (XPoint *) */
fpoint = va_arg(args, XPoint *);
newrecord->undodata = (char *)malloc(sizeof(XPoint));
((XPoint *)newrecord->undodata)->x = fpoint->x;
((XPoint *)newrecord->undodata)->y = fpoint->y;
break;
case XCF_Rotate:
/* 2 args: */
/* fpoint = point of flip (XPoint *) */
/* dir = direction and amount of rotation (float) */
fpoint = va_arg(args, XPoint *);
dir = va_arg(args, double);
newrecord->undodata = (char *)malloc(sizeof(rotateinfo));
((rotateinfo *)newrecord->undodata)->rotpos.x = fpoint->x;
((rotateinfo *)newrecord->undodata)->rotpos.y = fpoint->y;
((rotateinfo *)newrecord->undodata)->rotation = (float)dir;
break;
case XCF_Move:
/* 2 args: */
/* deltax = change in x position (int) */
/* deltay = change in y position (int) */
deltax = va_arg(args, int);
deltay = va_arg(args, int);
newrecord->undodata = (char *)malloc(sizeof(XPoint));
((XPoint *)newrecord->undodata)->x = deltax;
((XPoint *)newrecord->undodata)->y = deltay;
/* Fprintf(stdout, "Undo: registered move of delta (%d, %d)\n",
deltax, deltay); */
break;
case XCF_Reorder:
/* 2 args: */
/* slist = "before" order of elements */
/* snum = number of elements in list (= # parts) */
slist = va_arg(args, short *);
snum = va_arg(args, int);
newrecord->undodata = (char *)slist;
newrecord->idata = snum;
}
va_end(args);
}
/*----------------------------------------------------------------------*/
/* undo_one_action --- */
/* Play undo record back one in the stack. */
/*----------------------------------------------------------------------*/
short undo_one_action()
{
Undoptr thisrecord; /* , chkrecord; (jdk) */
objectptr thisobj;
objinstptr thisinst;
uselection *srec;
editelement *erec;
scaleinfo *escale;
short *slist;
XPoint *delta, position;
int i, j, snum;
int savemode;
float fnum;
genericptr egen;
labelptr thislabel;
pathptr thispath;
polyptr thispoly;
arcptr thisarc;
splineptr thisspline;
graphicptr thisgraphic;
Boolean need_redraw;
XCWindowData *savewindow = areawin;
/* Undo the recorded action and shift the undo record pointer. */
thisrecord = xobjs.undostack;
if (thisrecord == NULL) {
Fprintf(stderr, "Nothing to undo!\n");
return 0;
}
xobjs.undostack = thisrecord->next;
xobjs.redostack = thisrecord;
/* Set window, if event occurred in a different window */
if (setwindow(thisrecord->window) == FALSE) {
Wprintf("Error: Undo event in nonexistant window! Flushing stack.\n");
flush_undo_stack();
return 0;
}
/* Setting eventmode to UNDO_MODE prevents register_for_undo() from */
/* being called again while executing the event. */
savemode = eventmode;
eventmode = UNDO_MODE;
/* type-dependent part */
switch(thisrecord->type) {
case XCF_Delete:
unselect_all();
thisobj = (objectptr)thisrecord->undodata;
areawin->selects = thisobj->parts;
areawin->selectlist = xc_undelete(thisrecord->thisinst,
thisobj, (short)thisrecord->idata,
recover_selectlist(thisrecord));
srec = remember_selection(thisrecord->thisinst, areawin->selectlist,
areawin->selects);
thisrecord->undodata = (char *)srec;
draw_all_selected();
break;
/* To be finished: Needs to remove the object & instance from */
/* the library and library page. Should keep the empty object */
/* around so we have the name. */
case XCF_Select_Save:
unselect_all();
thisinst = (objinstptr)thisrecord->undodata;
thisobj = thisinst->thisobject;
/* Remove the instance */
i = thisrecord->thisinst->thisobject->parts - 1;
if ((genericptr)thisinst != *(thisrecord->thisinst->thisobject->plist + i)) {
Fprintf(stderr, "Error: No such element!\n");
thisrecord->undodata = NULL;
break;
}
else {
delete_one_element(thisrecord->thisinst, (genericptr)thisinst);
}
/* Put back all the parts */
areawin->selects = thisobj->parts;
areawin->selectlist = xc_undelete(thisrecord->thisinst,
thisobj, (short)thisrecord->idata,
recover_selectlist(thisrecord));
srec = remember_selection(thisrecord->thisinst, areawin->selectlist,
areawin->selects);
thisrecord->undodata = (char *)srec;
draw_all_selected();
break;
case XCF_Push:
popobject(areawin->area, 0, NULL);
break;
case XCF_Pop:
pushobject((objinstptr)thisrecord->thisinst);
break;
case XCF_Page:
newpage(thisrecord->idata);
break;
case XCF_Anchor:
thislabel = (labelptr)(thisrecord->undodata);
snum = thisrecord->idata;
thisrecord->idata = thislabel->anchor;
thislabel->anchor = snum;
areawin->redraw_needed = True;
drawarea(areawin->area, NULL, NULL);
break;
case XCF_Select:
/* If there was a previous selection in the undo list, */
/* revert to it. */
need_redraw = (areawin->selects > 0) ? True : False;
select_previous(thisrecord);
if (need_redraw) {
areawin->redraw_needed = True;
drawarea(areawin->area, NULL, NULL);
}
else
draw_all_selected();
break;
case XCF_Box: case XCF_Arc: case XCF_Wire: case XCF_Text:
case XCF_Pin_Label: case XCF_Pin_Global: case XCF_Info_Label:
case XCF_Spline: case XCF_Dot: case XCF_Graphic: case XCF_Join:
egen = (genericptr)thisrecord->undodata;
i = thisrecord->thisinst->thisobject->parts - 1;
if (egen != *(thisrecord->thisinst->thisobject->plist + i)) {
Fprintf(stderr, "Error: No such element!\n");
thisrecord->undodata = NULL;
}
else {
delete_one_element(thisrecord->thisinst, egen);
areawin->redraw_needed = True;
drawarea(areawin->area, NULL, NULL);
}
break;
case XCF_Edit:
erec = (editelement *)thisrecord->undodata;
switch (erec->element->type) {
case LABEL: {
stringpart *tmpstr;
int tmpanchor;
labelptr elab = (labelptr)(erec->element);
undrawtext(elab);
tmpstr = elab->string;
tmpanchor = (int)elab->anchor;
elab->string = stringcopyback(erec->save.string,
thisrecord->thisinst);
elab->anchor = (short)thisrecord->idata;
erec->save.string = tmpstr;
thisrecord->idata = tmpanchor;
resolveparams(thisrecord->thisinst);
redrawtext(elab);
} break;
case ARC: {
arcinfo tmpinfo;
arcptr earc = (arcptr)(erec->element);
tmpinfo.angle1 = earc->angle1;
tmpinfo.angle2 = earc->angle2;
tmpinfo.radius = earc->radius;
tmpinfo.yaxis = earc->yaxis;
tmpinfo.position = earc->position;
earc->angle1 = erec->save.arcspecs->angle1;
earc->angle2 = erec->save.arcspecs->angle2;
earc->radius = erec->save.arcspecs->radius;
earc->yaxis = erec->save.arcspecs->yaxis;
earc->position = erec->save.arcspecs->position;
*(erec->save.arcspecs) = tmpinfo;
calcarc(earc);
areawin->redraw_needed = True;
drawarea(areawin->area, NULL, NULL);
} break;
case OBJINST: {
XPoint tmppt;
objinstptr einst = (objinstptr)(erec->element);
// Swap instance and saved positions.
tmppt = einst->position;
einst->position = erec->save.instpos;
erec->save.instpos = tmppt;
areawin->redraw_needed = True;
drawarea(areawin->area, NULL, NULL);
} break;
case POLYGON: {
pointlist tmppts;
int tmpnum;
polyptr epoly = (polyptr)(erec->element);
tmppts = epoly->points;
tmpnum = epoly->number;
epoly->points = erec->save.points;
epoly->number = thisrecord->idata;
erec->save.points = tmppts;
thisrecord->idata = tmpnum;
areawin->redraw_needed = True;
drawarea(areawin->area, NULL, NULL);
} break;
case SPLINE: {
pointlist tmppts;
splineptr espline = (splineptr)(erec->element);
tmppts = copypoints((pointlist)espline->ctrl, 4);
for (i = 0; i < 4; i++)
espline->ctrl[i] = *(erec->save.points + i);
free(erec->save.points);
erec->save.points = tmppts;
calcspline(espline);
areawin->redraw_needed = True;
drawarea(areawin->area, NULL, NULL);
} break;
case PATH: {
pointlist tmppts;
int tmpnum;
polyptr epoly;
splineptr espline;
pathptr epath = (pathptr)(erec->element);
for (i = 0; i < epath->parts; i++) {
genericptr ggen = *(epath->plist + i);
switch (ELEMENTTYPE(ggen)) {
case POLYGON:
epoly = (polyptr)ggen;
tmppts = epoly->points;
tmpnum = epoly->number;
epoly->points = (erec->save.pathspecs + i)->points;
epoly->number = (erec->save.pathspecs + i)->number;
(erec->save.pathspecs + i)->points = tmppts;
(erec->save.pathspecs + i)->number = tmpnum;
break;
case SPLINE:
espline = (splineptr)ggen;
tmppts = copypoints((pointlist)espline->ctrl, 4);
for (j = 0; j < 4; j++)
espline->ctrl[j] = *((erec->save.pathspecs + i)->points + j);
free((erec->save.pathspecs + i)->points);
(erec->save.pathspecs + i)->points = tmppts;
calcspline(espline);
break;
}
}
areawin->redraw_needed = True;
drawarea(areawin->area, NULL, NULL);
}
}
break;
case XCF_Library_Pop:
srec = (uselection *)thisrecord->undodata;
slist = regen_selection(thisrecord->thisinst, srec);
thisobj = delete_element(thisrecord->thisinst, slist,
srec->number, DRAW);
free(slist);
thisrecord->undodata = (char *)thisobj;
break;
case XCF_Copy:
clearselects_noundo();
srec = (uselection *)thisrecord->undodata;
slist = regen_selection(thisrecord->thisinst, srec);
thisobj = delete_element(thisrecord->thisinst, slist,
srec->number, DRAW);
free(slist);
thisrecord->undodata = (char *)thisobj;
/* Revert selection to previously selected */
select_previous(thisrecord);
areawin->redraw_needed = True;
drawarea(areawin->area, NULL, NULL);
draw_all_selected();
break;
case XCF_ChangeStyle:
/* Style changes */
egen = (genericptr)thisrecord->undodata;
snum = thisrecord->idata;
switch(egen->type) {
case PATH:
thispath = (pathptr)(thisrecord->undodata);
thisrecord->idata = thispath->style;
thispath->style = snum;
break;
case POLYGON:
thispoly = (polyptr)(thisrecord->undodata);
thisrecord->idata = thispoly->style;
thispoly->style = snum;
break;
case ARC:
thisarc = (arcptr)(thisrecord->undodata);
thisrecord->idata = thisarc->style;
thisarc->style = snum;
break;
case SPLINE:
thisspline = (splineptr)(thisrecord->undodata);
thisrecord->idata = thisspline->style;
thisspline->style = snum;
break;
}
areawin->redraw_needed = True;
drawarea(areawin->area, NULL, NULL);
break;
case XCF_Color:
/* Color changes */
egen = (genericptr)thisrecord->undodata;
snum = thisrecord->idata;
switch(egen->type) {
case PATH:
thispath = (pathptr)(thisrecord->undodata);
thisrecord->idata = thispath->color;
thispath->color = snum;
break;
case POLYGON:
thispoly = (polyptr)(thisrecord->undodata);
thisrecord->idata = thispoly->color;
thispoly->color = snum;
break;
case ARC:
thisarc = (arcptr)(thisrecord->undodata);
thisrecord->idata = thisarc->color;