summaryrefslogtreecommitdiff
path: root/undo.c
blob: 4e3447064e1245a8f8f733657198939c7197001b (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
/*----------------------------------------------------------------------*/
/* 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 nonexistent 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;
	       thisarc->color = snum;
	       break;
	    case SPLINE:
	       thisspline = (splineptr)(thisrecord->undodata);
	       thisrecord->idata = thisspline->color;
	       thisspline->color = snum;
	       break;
	 }
	 areawin->redraw_needed = True;
	 drawarea(areawin->area, NULL, NULL);
	 break;

      case XCF_Rescale:
	 escale = (scaleinfo *)thisrecord->undodata;
	 egen = escale->element;
	 fnum = escale->scale;
	 switch(egen->type) {
	    case PATH:
	       thispath = (pathptr)egen;
	       escale->scale = thispath->width;
	       thispath->width = fnum;
	       break;
	    case POLYGON:
	       thispoly = (polyptr)egen;
	       escale->scale = thispoly->width;
	       thispoly->width = fnum;
	       break;
	    case ARC:
	       thisarc = (arcptr)egen;
	       escale->scale = thisarc->width;
	       thisarc->width = fnum;
	       break;
	    case SPLINE:
	       thisspline = (splineptr)egen;
	       escale->scale = thisspline->width;
	       thisspline->width = fnum;
	       break;
	    case OBJINST:
	       thisinst = (objinstptr)egen;
	       escale->scale = thisinst->scale;
	       thisinst->scale = fnum;
	       break;
	    case GRAPHIC:
	       thisgraphic = (graphicptr)egen;
	       escale->scale = thisgraphic->scale;
	       thisgraphic->scale = fnum;
#ifndef HAVE_CAIRO
	       thisgraphic->valid = FALSE;
#endif /* HAVE_CAIRO */
	       break;
	    case LABEL:
	       thislabel = (labelptr)egen;
	       escale->scale = thislabel->scale;
	       thislabel->scale = fnum;
	       break;
	 }
	 areawin->redraw_needed = True;
	 drawarea(areawin->area, NULL, NULL);
	 break;

      case XCF_Flip_X:
	 position = *((XPoint *)thisrecord->undodata);
	 elementflip(&position);
	 break;

      case XCF_Flip_Y:
	 position = *((XPoint *)thisrecord->undodata);
	 elementvflip(&position);
	 break;

      case XCF_Rotate:
	 position = ((rotateinfo *)thisrecord->undodata)->rotpos;
	 elementrotate(-((rotateinfo *)thisrecord->undodata)->rotation, &position);
	 break;

      case XCF_Move:
	 delta = (XPoint *)thisrecord->undodata;
	 select_connected_pins();
	 placeselects(-(delta->x), -(delta->y), NULL);
	 reset_cycles();
	 areawin->redraw_needed = True;
	 drawarea(areawin->area, NULL, NULL);
	 draw_all_selected();
	 break;

       case XCF_Reorder:
	 reorder_selection(thisrecord);
	 areawin->redraw_needed = True;
	 drawarea(areawin->area, NULL, NULL);
	 break;

      default:
	 Fprintf(stderr, "Undo not implemented for this action!\n");
	 break;
   }

   /* Does this need to be set on a per-event-type basis? */
   switch (savemode) {
      case CATALOG_MODE:
      case CATTEXT_MODE:
	 eventmode = CATALOG_MODE;
	 break;
      default:
	 eventmode = NORMAL_MODE;
	 break;
   }

   /* Diagnostic, to check if all multiple-event undo series are resolved */
   if (thisrecord->idx < 0) {
      Fprintf(stderr, "Warning:  Unfinished undo series in stack!\n");
      thisrecord->idx = -thisrecord->idx;
   }
   areawin = savewindow;
   return thisrecord->idx;
}

/*----------------------------------------------------------------------*/
/* undo_finish_series ---						*/
/*	Complete a possibly incomplete undo series by forcing the	*/
/*	topmost entry to have a positive index.				*/
/*	Note that for "undo series start|end" to work, undo_collect	*/
/*	must be set to 0 prior to calling undo_finish_series().		*/
/*----------------------------------------------------------------------*/

void undo_finish_series()
{
   if (undo_collect == (u_char)0)
      if (xobjs.undostack && xobjs.undostack->idx < 0)
         xobjs.undostack->idx = -xobjs.undostack->idx;
}

/*----------------------------------------------------------------------*/
/* undo_action ---							*/
/*	Play undo record back to the completion of a series.		*/
/*----------------------------------------------------------------------*/

void undo_action()
{
   short idx;

   // Cannot undo while in the middle of an undo series.  Failsafe.
   if (undo_collect != (u_char)0) return;

   idx = undo_one_action();
   while (xobjs.undostack && xobjs.undostack->idx == idx)
      undo_one_action();
}

/*----------------------------------------------------------------------*/
/* redo_one_action ---							*/
/*	Play undo record forward one in the stack.			*/
/*----------------------------------------------------------------------*/

short redo_one_action()
{
   Undoptr thisrecord;
   objectptr thisobj;
   genericptr egen;
   short *slist;
   XPoint *delta, position;
   uselection *srec;
   editelement *erec;
   scaleinfo *escale;
   int i, j, snum;
   int savemode;
   float fnum;
   labelptr thislabel;
   arcptr thisarc;
   pathptr thispath;
   splineptr thisspline;
   polyptr thispoly;
   graphicptr thisgraphic;
   objinstptr thisinst;
   XCWindowData *savewindow = areawin;

   /* Undo the recorded action and shift the undo record pointer.	*/

   thisrecord = xobjs.redostack;
   if (thisrecord == NULL) {
      Fprintf(stderr, "Nothing to redo!\n");
      return 0;
   }
   xobjs.undostack = thisrecord;
   xobjs.redostack = thisrecord->last;

   /* Set window, if event occurred in a different window */
   if (setwindow(thisrecord->window) == FALSE) {
      Wprintf("Error:  Undo event in nonexistent window!  Flushing stack.\n");
      flush_undo_stack();
      return 0;
   }

   savemode = eventmode;
   eventmode = UNDO_MODE;

   /* type-dependent part */

   switch(thisrecord->type) {
      case XCF_Delete:
	 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;
	 thisrecord->idata = (int)DRAW;
	 unselect_all();
	 areawin->redraw_needed = True;
	 drawarea(areawin->area, NULL, NULL);
	 break;

      /* Unfinished! */
      case XCF_Select_Save:
	 srec = (uselection *)thisrecord->undodata;
	 slist = regen_selection(thisrecord->thisinst, srec);
	 break;

      case XCF_Page:
	 newpage(*((int *)thisrecord->undodata));
	 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_Pop:
	 popobject(areawin->area, 0, NULL);
	 break;

      case XCF_Push:
	 pushobject((objinstptr)thisrecord->undodata);
	 break;

      case XCF_Select:
	 unselect_all();
	 srec = (uselection *)thisrecord->undodata;
	 areawin->selectlist = regen_selection(thisrecord->thisinst, srec);
	 areawin->selects = (areawin->selectlist) ? srec->number : 0;
	 draw_all_selected();
	 break;

      case XCF_Library_Pop:
	 thisobj = (objectptr)thisrecord->undodata;
	 if (thisobj != NULL) {
	    unselect_all();
	    snum = thisobj->parts;
	    slist = xc_undelete(thisrecord->thisinst, thisobj, DRAW, NULL);
	    thisrecord->undodata = (char *)remember_selection(thisrecord->thisinst,
			slist, snum);
	    free(slist);
	 }
	 break;

      case XCF_Copy:
	 thisobj = (objectptr)thisrecord->undodata;
	 if (thisobj != NULL) {
	    unselect_all();
	    areawin->selects = thisobj->parts;
	    areawin->selectlist = xc_undelete(thisrecord->thisinst, thisobj, DRAW,
			NULL);
	    thisrecord->undodata = (char *)remember_selection(thisrecord->thisinst,
			areawin->selectlist, areawin->selects);
	    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;
	 undelete_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 OBJINST: {
	       XPoint tmppt;
	       objinstptr einst = (objinstptr)(erec->element);
	       // Swap instance position and saved position
	       tmppt = einst->position;
	       einst->position = erec->save.instpos;
	       erec->save.instpos = tmppt;
	       areawin->redraw_needed = True;
	       drawarea(areawin->area, NULL, NULL);
	    }  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 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_Flip_X:
	 position = *((XPoint *)thisrecord->undodata);
	 elementflip(&position);
	 break;

      case XCF_Flip_Y:
	 position = *((XPoint *)thisrecord->undodata);
	 elementvflip(&position);
	 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;
	       thisarc->color = snum;
	       break;
	    case SPLINE:
	       thisspline = (splineptr)(thisrecord->undodata);
	       thisrecord->idata = thisspline->color;
	       thisspline->color = snum;
	       break;
	 }
	 areawin->redraw_needed = True;
	 drawarea(areawin->area, NULL, NULL);
	 break;

      case XCF_Rescale:
	 escale = (scaleinfo *)thisrecord->undodata;
	 egen = escale->element;
	 fnum = escale->scale;
	 switch(egen->type) {
	    case PATH:
	       thispath = (pathptr)egen;
	       escale->scale = thispath->width;
	       thispath->width = fnum;
	       break;
	    case POLYGON:
	       thispoly = (polyptr)egen;
	       escale->scale = thispoly->width;
	       thispoly->width = fnum;
	       break;
	    case ARC:
	       thisarc = (arcptr)egen;
	       escale->scale = thisarc->width;
	       thisarc->width = fnum;
	       break;
	    case SPLINE:
	       thisspline = (splineptr)egen;
	       escale->scale = thisspline->width;
	       thisspline->width = fnum;
	       break;
	    case OBJINST:
	       thisinst = (objinstptr)egen;
	       escale->scale = thisinst->scale;
	       thisinst->scale = fnum;
	       break;
	    case GRAPHIC:
	       thisgraphic = (graphicptr)egen;
	       escale->scale = thisgraphic->scale;
	       thisgraphic->scale = fnum;
#ifndef HAVE_CAIRO
	       thisgraphic->valid = FALSE;
#endif /* HAVE_CAIRO */
	       break;
	    case LABEL:
	       thislabel = (labelptr)egen;
	       escale->scale = thislabel->scale;
	       thislabel->scale = fnum;
	       break;
	 }
	 areawin->redraw_needed = True;
	 drawarea(areawin->area, NULL, NULL);
	 break;

      case XCF_Rotate:
	 position = ((rotateinfo *)thisrecord->undodata)->rotpos;
	 elementrotate(((rotateinfo *)thisrecord->undodata)->rotation, &position);
	 break;

      case XCF_Move:
	 delta = (XPoint *)thisrecord->undodata;
	 select_connected_pins();
	 placeselects(delta->x, delta->y, NULL);
	 reset_cycles();
	 areawin->redraw_needed = True;
	 drawarea(areawin->area, NULL, NULL);
	 break;

      case XCF_Reorder:
	 reorder_selection(thisrecord);
	 areawin->redraw_needed = True;
	 drawarea(areawin->area, NULL, NULL);
	 break;

      default:
	 Fprintf(stderr, "Undo not implemented for this action!\n");
	 break;
   }

   /* Does this need to be set on a per-event-type basis? */
   switch (savemode) {
      case CATALOG_MODE:
      case CATTEXT_MODE:
	 eventmode = CATALOG_MODE;
	 break;
      default:
	 eventmode = NORMAL_MODE;
	 break;
   }

   areawin = savewindow;

   return thisrecord->idx;
}

/*----------------------------------------------------------------------*/
/* redo_action ---							*/
/*	Play undo record forward to the completion of a series.		*/
/*----------------------------------------------------------------------*/

void redo_action()
{
   short idx;

   // Cannot redo while in the middle of an undo series.  Failsafe.
   if (undo_collect != (u_char)0) return;

   idx = redo_one_action();
   while (xobjs.redostack && xobjs.redostack->idx == idx)
      redo_one_action();
}

/*----------------------------------------------------------------------*/
/* flush_redo_stack ---							*/
/*	Free all memory allocated to the redo stack due to the		*/
/* 	insertion of a new undo record.					*/
/*----------------------------------------------------------------------*/

void flush_redo_stack()
{
   Undoptr thisrecord, nextrecord;

   if (xobjs.redostack == NULL) return;	/* no redo stack */

   thisrecord = xobjs.redostack;

   while (thisrecord != NULL) {
      nextrecord = thisrecord->last;
      free_redo_record(thisrecord);
      thisrecord = nextrecord;
   }
   xobjs.redostack = NULL;

   if (xobjs.undostack)
      xobjs.undostack->last = NULL;
}

/*----------------------------------------------------------------------*/
/* flush_undo_stack ---							*/
/*	Free all memory allocated to the undo and redo stacks.		*/
/*----------------------------------------------------------------------*/

void flush_undo_stack()
{
   Undoptr thisrecord, nextrecord;

   flush_redo_stack();

   thisrecord = xobjs.undostack;

   while (thisrecord != NULL) {
      nextrecord = thisrecord->next;
      free_undo_record(thisrecord);
      thisrecord = nextrecord;
   }
   xobjs.undostack = NULL;
}

/*----------------------------------------------------------------------*/
/* free_undo_data ---							*/
/*	Free memory allocated to the "undodata" part of the undo	*/
/*	record, based on the record type.				*/
/*									*/
/* "mode" specifies whether this is for an "undo" or a "redo" event.	*/
/*									*/
/*	Note that the action taken for a specific record may *NOT* be	*/
/*	the same for a record in the undo stack as it is for a record	*/
/*	in the redo stack, because the data types are changed when	*/
/*	moving from one record to the next.				*/
/*----------------------------------------------------------------------*/

void free_undo_data(Undoptr thisrecord, u_char mode)
{
   u_int type;
   objectptr uobj;
   uselection *srec;
   editelement *erec;

   type = thisrecord->type;
   switch (type) {
      case XCF_Delete:
	 if (mode == MODE_UNDO) {
	    uobj = (objectptr)thisrecord->undodata;
	    reset(uobj, DESTROY);
	 }
	 else { /* MODE_REDO */
	    srec = (uselection *)thisrecord->undodata;
	    free_selection(srec);
	 }
	 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:
	 /* if MODE_UNDO, the element is on the page, so don't destroy it! */
	 if (mode == MODE_REDO)
	    free(thisrecord->undodata);
	 break;

      case XCF_Edit:
	 erec = (editelement *)thisrecord->undodata;
	 free_editelement(thisrecord);
	 break;

      case XCF_Copy:
      case XCF_Library_Pop:
	 if (mode == MODE_UNDO) {
	    srec = (uselection *)thisrecord->undodata;
	    free_selection(srec);
	 }
	 else { /* MODE_REDO */
	    uobj = (objectptr)thisrecord->undodata;
	    reset(uobj, DESTROY);
	 }
	 break;

      case XCF_Push:
      case XCF_ChangeStyle:
      case XCF_Anchor:
      case XCF_Color:
	 /* Do nothing --- undodata points to a valid element */
	 break;

      case XCF_Select:
	 srec = (uselection *)thisrecord->undodata;
	 free_selection(srec);
	 break;

      default:
	 if (thisrecord->undodata != NULL)
	    free(thisrecord->undodata);
	 break;
   }
   thisrecord->undodata = NULL;
}


/*----------------------------------------------------------------------*/
/* free_undo_record ---							*/
/*	Free allocated memory for one record in the undo stack.		*/
/*----------------------------------------------------------------------*/

void free_undo_record(Undoptr thisrecord)
{
  /* Undoptr nextrecord, lastrecord; (jdk) */

   /* Reset the master list pointers */

   if (xobjs.undostack == thisrecord)
      xobjs.undostack = thisrecord->next;

   /* Relink the stack pointers */

   if (thisrecord->last)
      thisrecord->last->next = thisrecord->next;

   if (thisrecord->next)
      thisrecord->next->last = thisrecord->last;

   /* Free memory allocated to the record */

   free_undo_data(thisrecord, MODE_UNDO);
   free(thisrecord);
}

/*----------------------------------------------------------------------*/
/* free_redo_record ---							*/
/*	Free allocated memory for one record in the redo stack.		*/
/*----------------------------------------------------------------------*/

void free_redo_record(Undoptr thisrecord)
{
  /* Undoptr nextrecord, lastrecord; (jdk) */

   /* Reset the master list pointers */

   if (xobjs.redostack == thisrecord)
      xobjs.redostack = thisrecord->last;

   /* Relink the stack pointers */

   if (thisrecord->next)
      thisrecord->next->last = thisrecord->last;

   if (thisrecord->last)
      thisrecord->last->next = thisrecord->next;

   /* Free memory allocated to the record */

   free_undo_data(thisrecord, MODE_REDO);
   free(thisrecord);
}

/*----------------------------------------------------------------------*/
/* truncate_undo_stack ---						*/
/*	If the limit MAX_UNDO_EVENTS has been reached, discard the	*/
/*	last undo series on the stack (index = 1) and renumber the	*/
/*	others by decrementing.						*/
/*----------------------------------------------------------------------*/

void truncate_undo_stack()
{
   Undoptr thisrecord, nextrecord;

   thisrecord = xobjs.undostack;
   while (thisrecord != NULL) {
      nextrecord = thisrecord->next;
      if (thisrecord->idx > 1)
	 thisrecord->idx--;
      else
         free_undo_record(thisrecord);
      thisrecord = nextrecord;
   }
}

#ifndef TCL_WRAPPER

/* Calls from the Xt menus (see menus.h)				*/
/* These are wrappers for undo_action and redo_action			*/

void undo_call(xcWidget button, caddr_t clientdata, caddr_t calldata)
{
   undo_action();
}

void redo_call(xcWidget button, caddr_t clientdata, caddr_t calldata)
{
   redo_action();
}

#endif
/*----------------------------------------------------------------------*/