File: o_complex_basic.nw

package info (click to toggle)
libgeda 20050313-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 5,636 kB
  • ctags: 1,505
  • sloc: ansic: 19,449; sh: 8,627; makefile: 197; perl: 59
file content (2012 lines) | stat: -rw-r--r-- 54,958 bytes parent folder | download
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
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
@c -*- mode: Noweb; noweb-doc-mode: texinfo-mode; noweb-code-mode: c-mode -*-

@node File o_complex_basic.c,,,Top
@chapter File @file{o_complex_basic.c}

@section File header

<<o_complex_basic.c : *>>=
<<o_complex_basic.c : copyright and license>>

/* DO NOT read or edit this file ! Use ../noweb/o_complex_basic.nw instead */

<<o_complex_basic.c : include directives>>
<<o_complex_basic.c : get_complex_bounds()>>
<<o_complex_basic.c : get_complex_bounds_selection()>>
<<o_complex_basic.c : world_get_complex_bounds()>>
<<o_complex_basic.c : add_head()>>
<<o_complex_basic.c : o_complex_is_eligible_attribute()>>
<<o_complex_basic.c : o_complex_is_embedded()>>           /* Done */
<<o_complex_basic.c : o_complex_add()>>
<<o_complex_basic.c : o_complex_add_embedded()>>
<<o_complex_basic.c : o_complex_recalc()>>
<<o_complex_basic.c : o_complex_read()>>
<<o_complex_basic.c : o_complex_save()>>
<<o_complex_basic.c : o_complex_set_filename()>>
<<o_complex_basic.c : o_complex_free_filename()>>
<<o_complex_basic.c : o_complex_translate()>>
<<o_complex_basic.c : o_complex_world_translate()>>
<<o_complex_basic.c : o_complex_world_translate_toplevel()>>
<<o_complex_basic.c : o_complex_copy()>>
<<o_complex_basic.c : o_complex_copy_embedded()>>
<<o_complex_basic.c : o_complex_delete()>>
<<o_complex_basic.c : o_complex_set_color()>>
<<o_complex_basic.c : o_complex_set_color_single()>>
<<o_complex_basic.c : o_complex_set_color_save()>>
<<o_complex_basic.c : o_complex_unset_color()>>
<<o_complex_basic.c : o_complex_unset_color_single()>>
<<o_complex_basic.c : o_complex_set_saved_color_only()>>
<<o_complex_basic.c : o_complex_return_nth_pin()>>
<<o_complex_basic.c : o_complex_rotate_lowlevel()>>
<<o_complex_basic.c : o_complex_mirror_lowlevel()>>
<<o_complex_basic.c : o_complex_return_pin_object()>>
<<o_complex_basic.c : o_complex_check_symversion()>>

@


<<o_complex_basic.c : copyright and license>>=
/* gEDA - GPL Electronic Design Automation
 * libgeda - gEDA's library
 * Copyright (C) 1998-2000 Ales V. Hvezda
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
 */

@ 


<<o_complex_basic.c : include directives>>=
#include <config.h>

#include <stdio.h>
#include <math.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <gtk/gtk.h>
#include <libguile.h>

#include "defines.h"
#include "struct.h"
#include "globals.h"
#include "o_types.h"
#include "colors.h"
#include "funcs.h"

#include "../include/prototype.h"

#ifdef HAVE_LIBDMALLOC
#include <dmalloc.h>
#endif

@ 


@section Function @code{get_complex_bounds()}

@defun get_complex_bounds w_current complex left top right bottom
@end defun

<<o_complex_basic.c : get_complex_bounds()>>=
void
get_complex_bounds(TOPLEVEL *w_current, OBJECT *complex, 
		   int *left, int *top, int *right, int *bottom)
{
  OBJECT *o_current=NULL;
  int rleft, rtop, rright, rbottom;
	
  *left = rleft = 999999;
  *top = rtop = 9999999;
  *right = rright = 0;
  *bottom = rbottom = 0;
	


  o_current = complex;
	
  while ( o_current != NULL ) {
    switch(o_current->type) {
      case(OBJ_LINE):
        get_line_bounds(w_current, o_current->line, &rleft, &rtop, &rright, &rbottom);
        break;

      case(OBJ_NET):
        /* same as a line (diff name)*/
        get_net_bounds(w_current, o_current->line, &rleft, &rtop, &rright, &rbottom);
        break;

      case(OBJ_BUS):
        /* same as a line (diff name)*/
        get_bus_bounds(w_current, o_current->line, &rleft, &rtop, &rright, &rbottom);
        break;
	
      case(OBJ_BOX):
        get_box_bounds(w_current, o_current->box, &rleft, &rtop, &rright, &rbottom);
        break;

      case(OBJ_PICTURE):
#ifndef HAS_GTK12
        get_picture_bounds(w_current, o_current->picture, &rleft, &rtop, &rright, &rbottom);
#endif
        break;

      case(OBJ_CIRCLE):
        get_circle_bounds(w_current, o_current->circle, &rleft, &rtop, &rright, &rbottom);
        break;

      case(OBJ_COMPLEX):
      case(OBJ_PLACEHOLDER):
        /* recursive objects ?*/
        get_complex_bounds(w_current, o_current->complex->prim_objs, &rleft, &rtop, &rright, &rbottom);
        break;

      case(OBJ_TEXT):
        /* only do bounding boxes for visible or doing show_hidden_text*/
        /* you might lose some attrs though */
        if (o_current->visibility == VISIBLE ||
            (o_current->visibility == INVISIBLE && w_current->show_hidden_text)) {
          get_text_bounds(w_current, o_current, &rleft, &rtop, &rright, &rbottom);
        }
        break;

      case(OBJ_PIN):
        get_pin_bounds(w_current, o_current->line, &rleft, &rtop, &rright, &rbottom);
        break;

      case(OBJ_ARC):
        get_arc_bounds(w_current, o_current, &rleft, &rtop, &rright, &rbottom);
        break;

      default:
        break;
    }

    if (rleft < *left) *left = rleft;
    if (rtop < *top) *top = rtop;
    if (rright > *right) *right = rright;
    if (rbottom > *bottom) *bottom = rbottom;
	

    o_current=o_current->next;
  }

}

@ %def get_complex_bounds


@section Function @code{get_complex_bounds_selection()}

@defun get_complex_bounds_selection w_current head left top right bottom
@end defun

<<o_complex_basic.c : get_complex_bounds_selection()>>=
void
get_complex_bounds_selection(TOPLEVEL *w_current, SELECTION *head, 
			     int *left, int *top, int *right, int *bottom)
{
  SELECTION *s_current=NULL;
  OBJECT *o_current=NULL;
  int rleft, rtop, rright, rbottom;
	
  *left = rleft = 999999;
  *top = rtop = 9999999;
  *right = rright = 0;
  *bottom = rbottom = 0;
	
  s_current = head;
	
  while ( s_current != NULL ) {

    o_current = s_current->selected_object;

    if (!o_current) {
      fprintf(stderr, "Got NULL in get_complex_bounds_selection\n");
      exit(-1);
    }

    switch(o_current->type) {
      case(OBJ_LINE):
        get_line_bounds(w_current, o_current->line, &rleft, &rtop, &rright, &rbottom);
        break;

      case(OBJ_NET):
        /* same as a line (diff name)*/
        get_net_bounds(w_current, o_current->line, &rleft, &rtop, &rright, &rbottom);
        break;

      case(OBJ_BUS):
        /* same as a line (diff name)*/
        get_bus_bounds(w_current, o_current->line, &rleft, &rtop, &rright, &rbottom);
        break;
	
      case(OBJ_BOX):
        get_box_bounds(w_current, o_current->box, &rleft, &rtop, &rright, &rbottom);
        break;

      case(OBJ_PICTURE):
#ifndef HAS_GTK12
        get_picture_bounds(w_current, o_current->picture, &rleft, &rtop, &rright, &rbottom);
#endif
        break;

      case(OBJ_CIRCLE):
        get_circle_bounds(w_current, o_current->circle, &rleft, &rtop, &rright, &rbottom);
        break;

      case(OBJ_COMPLEX):
      case(OBJ_PLACEHOLDER):
        /* recursive objects ?*/
        get_complex_bounds(w_current, o_current->complex->prim_objs, &rleft, &rtop, &rright, &rbottom);
        break;

      case(OBJ_TEXT):
        /* only do bounding boxes for visible or doing show hidden text */
        /* you might lose some attrs though */
        if (o_current->visibility == VISIBLE ||
            (o_current->visibility == INVISIBLE && w_current->show_hidden_text)) {
          get_text_bounds(w_current, o_current, &rleft, &rtop, &rright, &rbottom);
        }
        break;

      case(OBJ_PIN):
        get_pin_bounds(w_current, o_current->line, &rleft, &rtop, &rright, &rbottom);
        break;

      case(OBJ_ARC):
        get_arc_bounds(w_current, o_current, &rleft, &rtop, &rright, &rbottom);
        break;

      default:
        break;
    }

    if (rleft < *left) *left = rleft;
    if (rtop < *top) *top = rtop;
    if (rright > *right) *right = rright;
    if (rbottom > *bottom) *bottom = rbottom;
	

    s_current=s_current->next;
  }

}

@ %def get_complex_bounds_selection


@section Function @code{world_get_complex_bounds()}

@defun world_get_complex_bounds w_current complex left top right bottom
@end defun

<<o_complex_basic.c : world_get_complex_bounds()>>=
void
world_get_complex_bounds(TOPLEVEL *w_current, OBJECT *complex, 
			 int *left, int *top, int *right, int *bottom)
{
  OBJECT *o_current=NULL;
  int rleft, rtop, rright, rbottom;
	
  *left = rleft = w_current->init_right;
  *top = rtop = w_current->init_bottom;;
  *right = rright = 0;
  *bottom = rbottom = 0;
	
  o_current = complex;
	
  while ( o_current != NULL ) {
    switch(o_current->type) {
      case(OBJ_LINE):
        world_get_line_bounds(w_current, o_current->line, &rleft, &rtop, &rright, &rbottom);
        break;

      case(OBJ_NET):
        /* same as a line (diff name)*/
        world_get_net_bounds(w_current, o_current->line, &rleft, &rtop, &rright, &rbottom);
        break;

      case(OBJ_BUS):
        /* same as a line (diff name)*/
        world_get_bus_bounds(w_current, o_current->line, &rleft, &rtop, &rright, &rbottom);
        break;
	
	
      case(OBJ_BOX):
        world_get_box_bounds(w_current, o_current->box, &rleft, &rtop, &rright, &rbottom);
        break;

      case(OBJ_PICTURE):
#ifndef HAS_GTK12
        world_get_picture_bounds(w_current, o_current->picture, &rleft, &rtop, &rright, &rbottom);
#endif
        break;

      case(OBJ_CIRCLE):
        world_get_circle_bounds(w_current, o_current->circle, &rleft, &rtop, &rright, &rbottom);
        break;

      case(OBJ_COMPLEX):
      case(OBJ_PLACEHOLDER):
        /* recursive objects ?*/
        world_get_complex_bounds(w_current, o_current->complex->prim_objs, &rleft, &rtop, &rright, &rbottom);
        break;

      case(OBJ_TEXT):
        /* only do bounding boxes for visible or doing show hidden text */
        /* you might lose some attrs though */
        if (o_current->visibility == VISIBLE ||
            (o_current->visibility == INVISIBLE && w_current->show_hidden_text)) {
          world_get_text_bounds(w_current, o_current, &rleft, &rtop, &rright, &rbottom);
        }
        break;

      case(OBJ_PIN):
        world_get_pin_bounds(w_current, o_current->line, &rleft, &rtop, &rright, &rbottom);
        break;

      case(OBJ_ARC):
        world_get_arc_bounds(w_current, o_current, &rleft, &rtop, &rright, &rbottom);
        break;

      default:
        break;
    }

    if (rleft < *left) *left = rleft;
    if (rtop < *top) *top = rtop;
    if (rright > *right) *right = rright;
    if (rbottom > *bottom) *bottom = rbottom;
	

    o_current=o_current->next;
  }

}

@ %def world_get_complex_bounds


@section Function @code{add_head()}

@defun add_head
@end defun

<<o_complex_basic.c : add_head()>>=
OBJECT *
add_head(void)
{
  OBJECT *new_node=NULL;

  new_node = s_basic_init_object("complex_head"); 

  new_node->type = OBJ_HEAD; /* make this of head type hack */

  /* don't need to do this for head nodes */
  /* ret = (OBJECT *) s_basic_link_object(new_node, NULL);*/
  return(new_node);
}

@ %def add_head


@section Function @code{o_complex_is_eligible_attribute()}

@defun o_complex_is_eligible_attribute object promote_invisible
@end defun

<<o_complex_basic.c : o_complex_is_eligible_attribute()>>=
/* The promote_invisible flag is either TRUE or FALSE */
/* It controls if invisible text is promoted (TRUE) or not (FALSE) */
int 
o_complex_is_eligible_attribute (OBJECT *object, int promote_invisible) 
{
  char *ptr;

  if (object->type != OBJ_TEXT || object->attribute || object->attached_to)
  {
    return 0; /* not a text item or is already attached */
  }

  /* Make sure text item is an attribute */
  ptr = strchr(object->text->string, '=');
  if (!ptr || ptr[1] == '\0' || ptr[1] == ' ')
  {
    return 0;  /* not an attribute */
  }

  /* always promote symversion= attribute, even if it is invisible */
  if (strncmp(object->text->string, "symversion=", 11) == 0)
  {
    return 1;
  }

  /* object is invisible and we do not want to promote invisible text */
  if (object->visibility == INVISIBLE && promote_invisible == FALSE)
  {
    return 0; /* attribute not eligible for promotion */
  }

  /* yup, attribute can be promoted */
  return 1;
}

@ %def o_complex_is_eligible_attribute


@section Function @code{o_complex_is_embedded()}

@defun o_complex_is_embedded o_current
This function returns 1 - true - if the [[o_current]] pointed object is an embedded complex, 0 - false - otherwise.
It checks wether [[o_current]] is a COMPLEX by comparing the [[complex]] entry of the OBJECT structure to [[NULL]]. If it is [[NULL]], it returns 0.
@end defun

<<o_complex_basic.c : o_complex_is_embedded()>>=
int
o_complex_is_embedded(OBJECT *o_current)
{
  if(o_current->complex == NULL)
  return 0;

  if (o_current->complex_clib && 
      strncmp(o_current->complex_clib, "EMBEDDED", 8) == 0) {
    return 1;
  } else {
    return 0;
  }

}

@ %def o_complex_is_embedded


@section Function @code{o_complex_add()}

@defun o_complex_add w_current object_list type color x y angle mirror clib basename selectable attribute_promotion
@end defun

<<o_complex_basic.c : o_complex_add()>>=
OBJECT *
o_complex_add(TOPLEVEL *w_current, OBJECT *object_list, char type,
              int color, int x, int y, int angle, int mirror, char *clib,
              char *basename, int selectable, int attribute_promotion)
{
  OBJECT *new_node=NULL;
  OBJECT *prim_objs=NULL;
  OBJECT *temp_tail=NULL;
  OBJECT *temp_parent=NULL;
  int save_adding_sel = 0;
  int loaded_normally = FALSE;
	
  char *filename;

  new_node = s_basic_init_object("complex");
  new_node->type = type;

  new_node->complex_basename = strdup(basename);
  if (clib)
    new_node->complex_clib = strdup(clib);
  else
    new_node->complex_clib = NULL;

  new_node->color = color;
	
  new_node->complex = (COMPLEX *) malloc(sizeof(COMPLEX));
	
  new_node->complex->angle = angle;
  new_node->complex->mirror = mirror;

  new_node->complex->x = x;
  new_node->complex->y = y;
  WORLDtoSCREEN(w_current, x, y, 
                &new_node->complex->screen_x, 
                &new_node->complex->screen_y);    

  /* TODO: questionable caste? */
  new_node->draw_func = (void *) complex_draw_func;  

  if (selectable) { 
    new_node->sel_func = (void *) select_func;
  } else {
    new_node->sel_func = NULL;
  }

  /* this was at the beginning and p_complex was = to complex */
  prim_objs = (OBJECT *) add_head();
	
  /* set the parent field now */
  prim_objs->complex_parent = new_node;

  /* is the bit with the temp and object_tail needed? */
  /* I don't like this at all hack */
  /* careful whenever you select, you get a read from disk */
  /* for the objects, UGG! there foreattribs are being copied */
  /* you need to override them if there are attached ones */
  /* on the main list */
  temp_tail = w_current->page_current->object_tail;
  temp_parent = w_current->page_current->object_parent;	
  w_current->page_current->object_parent = prim_objs;
  /* reason this works is because it has a head, see add_head above */

  /* check for validity */
  if (clib && basename)
    filename = g_strdup_printf("%s%c%s", clib, G_DIR_SEPARATOR, basename);
  else 
    filename = g_strdup("unknown");

  save_adding_sel = w_current->ADDING_SEL;
  w_current->ADDING_SEL = 1;	/* name is hack, don't want to */

  if (access(filename, R_OK)) {

    OBJECT *save_prim_objs;
    char *not_found_text = NULL;
    int left, right, top, bottom;
    int x_offset, y_offset;

    if (clib == NULL) {
      s_log_message("Component library was not found or specified\n");
    } else if (basename == NULL) {
      s_log_message("Basename (component) was not found or specified\n");
    } else {
      s_log_message("Could not open symbol file [%s]\n", filename); 
    } 

    /* filename was NOT found */
    loaded_normally = FALSE;

    /* save the prim_objs pointer, since the following code modifies it */
    save_prim_objs = prim_objs;

    /* Put placeholder into object list.  Changed by SDB on
     * 1.19.2005 to fix problem that symbols were silently
     * deleted by gattrib when RC files were messed up.  */
    new_node->type = OBJ_PLACEHOLDER;

    /* Mark the origin of the missing component */
    prim_objs = o_line_add(w_current, prim_objs, OBJ_LINE, 
                           DETACHED_ATTRIBUTE_COLOR,
                           x - 50, y, x + 50, y);
    prim_objs = o_line_add(w_current, prim_objs, OBJ_LINE, 
                           DETACHED_ATTRIBUTE_COLOR,
                           x, y + 50, x, y - 50); 

    /* Add some useful text */
    not_found_text = 
      g_strdup_printf ("Component not found:\n %s", basename);
    prim_objs = o_text_add(w_current, prim_objs,
                           OBJ_TEXT, DETACHED_ATTRIBUTE_COLOR, 
                           x + NOT_FOUND_TEXT_X, 
                           y + NOT_FOUND_TEXT_Y, LOWER_LEFT, 0, 
                           not_found_text, 8,
                           VISIBLE, SHOW_NAME_VALUE);
    free(not_found_text);

    /* figure out where to put the hazard triangle */
    world_get_text_bounds(w_current, prim_objs,
                          &left, &top, &right, &bottom);
    x_offset = (right - left) / 4;  
    y_offset = bottom - top + 100;  /* 100 is just an additional offset */

    /* add hazard triangle */
    prim_objs = o_line_add(w_current, prim_objs, OBJ_LINE, 
                           DETACHED_ATTRIBUTE_COLOR,
                           x + NOT_FOUND_TEXT_X + x_offset, 
                           y + NOT_FOUND_TEXT_Y + y_offset, 
                           x + NOT_FOUND_TEXT_X + x_offset + 600, 
                           y + NOT_FOUND_TEXT_Y + y_offset); 
    o_set_line_options(w_current, prim_objs, END_ROUND, TYPE_SOLID, 
                       50, -1, -1);
    prim_objs = o_line_add(w_current, prim_objs, OBJ_LINE, 
                           DETACHED_ATTRIBUTE_COLOR,
                           x + NOT_FOUND_TEXT_X + x_offset, 
                           y + NOT_FOUND_TEXT_Y + y_offset, 
                           x + NOT_FOUND_TEXT_X + x_offset + 300, 
                           y + NOT_FOUND_TEXT_Y + y_offset + 500); 
    o_set_line_options(w_current, prim_objs, END_ROUND, TYPE_SOLID, 
                       50, -1, -1);
    prim_objs = o_line_add(w_current, prim_objs, OBJ_LINE, 
                           DETACHED_ATTRIBUTE_COLOR,
                           x + NOT_FOUND_TEXT_X + x_offset + 300, 
                           y + NOT_FOUND_TEXT_Y + y_offset + 500, 
                           x + NOT_FOUND_TEXT_X + x_offset + 600, 
                           y + NOT_FOUND_TEXT_Y + y_offset); 
    o_set_line_options(w_current, prim_objs, END_ROUND, TYPE_SOLID, 
                       50, -1, -1);
    prim_objs = o_text_add(w_current, prim_objs,
                           OBJ_TEXT, DETACHED_ATTRIBUTE_COLOR, 
                           x + NOT_FOUND_TEXT_X + x_offset + 270, 
                           y + NOT_FOUND_TEXT_Y + y_offset + 90, 
                           LOWER_LEFT, 0, "!", 18,
                           VISIBLE, SHOW_NAME_VALUE);
    prim_objs = save_prim_objs;

  } else {

    /* filename was found */
    loaded_normally = TRUE;
    
    /* add connections till translated */
    o_read(w_current, prim_objs, filename);
    
  }
  w_current->ADDING_SEL = save_adding_sel;

  if (w_current->attribute_promotion) { /* controlled through rc file */

    OBJECT *tmp,*next;

    for (tmp=prim_objs->next;tmp;tmp=next) {

      next=tmp->next;

      /* valid floating attrib? */
      if (o_complex_is_eligible_attribute(tmp, w_current->promote_invisible))
      {
        /* Is attribute promotion called for? (passed in parameter) */
        if (attribute_promotion)
        {
          /* remove tmp from the complex list */
          if (tmp->next)
            tmp->next->prev=tmp->prev;
          if (tmp->prev)
            tmp->prev->next=tmp->next;

          /* Isolate tmp completely, now that it's removed from list */
          tmp->next=tmp->prev=NULL;
		
          object_list = (OBJECT *) s_basic_link_object(tmp, object_list);
          o_attrib_attach (w_current, object_list, tmp, new_node);
          o_text_translate_world(w_current, x, y, tmp);

        } else { /* not promoting now, but deal with floating attribs */

          if (w_current->keep_invisible == TRUE) {  
            /* if we are not promoting invisible attribs, keep them */
            /* around */
            tmp->visibility = INVISIBLE;
          } else {
            /* else do the default behavior of deleting the original */
            /* object */
            s_delete(w_current, tmp);
          }

        }
      }
    }
  }

  w_current->page_current->object_tail = temp_tail;
  w_current->page_current->object_parent = temp_parent;

  object_list = (OBJECT *) s_basic_link_object(new_node, object_list);
  object_list->complex->prim_objs = prim_objs;


  /* do not mirror/rotate/translate/connect the primitive objects if the
   * component was not loaded via o_read 
   */
  if (loaded_normally == TRUE) {
    if (mirror) {
      o_complex_mirror_lowlevel(w_current, x, y, object_list);
    } 

    o_complex_rotate_lowlevel(w_current, x, y, angle, angle, object_list); 
    o_complex_world_translate(w_current, x, y, prim_objs);

    if (!w_current->ADDING_SEL) {
     s_conn_update_complex(w_current, prim_objs);
    }
  }

  free(filename);
  return(object_list);
}

@


@section Function @code{o_complex_add_embedded()}

@defun o_complex_add_embedded w_current object_list type color x y angle clib basename selectable
@end defun

<<o_complex_basic.c : o_complex_add_embedded()>>=
OBJECT *
o_complex_add_embedded(TOPLEVEL *w_current, OBJECT *object_list,
					   char type, int color, int x, int y, int angle,
					   char *clib, char *basename, int selectable)
{
  OBJECT *prim_objs=NULL;
  OBJECT *new_node=NULL;

  new_node = s_basic_init_object("complex");
  new_node->type = type;

  new_node->complex = (COMPLEX *) malloc(sizeof(COMPLEX));
  new_node->complex->x = x;
  new_node->complex->y = y;
  WORLDtoSCREEN(w_current, x, y, 
                &new_node->complex->screen_x, 
                &new_node->complex->screen_y);    

  new_node->complex->angle = angle;
  new_node->complex->mirror = 0;
	
  new_node->complex_basename = strdup(basename);
  if (clib)
    new_node->complex_clib = strdup(clib);
  else
    new_node->complex_clib = NULL;

  new_node->color = color;

  /* TODO: questionable cast */
  new_node->draw_func = (void *) complex_draw_func;  

  /* (for a title block) an object that isn't selectable */
  if (selectable) { 
    new_node->sel_func = (void *) select_func;
  } else {
    new_node->sel_func = NULL;
  }

  object_list = (OBJECT *) s_basic_link_object(new_node, object_list);

  /* this was at the beginning and p_complex was = to complex */
  prim_objs = (OBJECT *) add_head();
  object_list->complex->prim_objs = prim_objs;
	
  /* set the parent field now */
  prim_objs->complex_parent = object_list;

  /* don't have to translate/rotate/mirror here at all since the */
  /* object is in place */
  return(object_list);
}

@ %def o_complex_add_embedded


@section Function @code{o_complex_recalc()}

@defun o_complex_recalc w_current o_current
@end defun

<<o_complex_basic.c : o_complex_recalc()>>=
void
o_complex_recalc(TOPLEVEL *w_current, OBJECT *o_current)
{
  int left, right, top, bottom;

  /* realc routine Add this somewhere */
  /* libhack */
  /* o_recalc(w_current, o_current->complex);*/

  get_complex_bounds(w_current, o_current->complex->prim_objs, &left, &top, &right, &bottom);
  o_current->left = left;
  o_current->top = top;
  o_current->right = right;
  o_current->bottom = bottom;

  WORLDtoSCREEN(w_current, 
                o_current->complex->x, 
                o_current->complex->y,
                &o_current->complex->screen_x, 
                &o_current->complex->screen_y);
}

@ %def o_complex_recalc


@section Function @code{o_complex_read()}

@defun o_complex_read w_current object_list buf version
@end defun

<<o_complex_basic.c : o_complex_read()>>=
OBJECT *
o_complex_read(TOPLEVEL *w_current, OBJECT *object_list,
               char buf[], unsigned int release_ver,
               unsigned int fileformat_ver)
{
  char type; 
  int x1, y1;
  int angle;

  char basename[256]; /* hack */
  char *clib=NULL;
	
  int selectable;
  int mirror;

  sscanf(buf, "%c %d %d %d %d %d %s\n",
         &type, &x1, &y1, &selectable, &angle, &mirror, basename);

  switch(angle) {

    case(0):
    case(90):
    case(180):
    case(270):
      break;

    default:
      fprintf(stderr, "Found a component with an invalid rotation [ %c %d %d %d %d %d %s ]\n", type, x1, y1, selectable, angle, mirror, basename); 
      s_log_message("Found a component with an invalid rotation [ %c %d %d %d %d %d %s ]\n", type, x1, y1, selectable, angle, mirror, basename); 
      break;
  }

  switch(mirror) {

    case(0):
    case(1):

    break;
		
    default:
    fprintf(stderr, "Found a component with an invalid mirror flag [ %c %d %d %d %d %d %s ]\n", type, x1, y1, selectable, angle, mirror, basename); 
    s_log_message("Found a component with an invalid mirror flag [ %c %d %d %d %d %d %s ]\n", type, x1, y1, selectable, angle, mirror, basename); 
    break;
  }
  if (strncmp(basename, "EMBEDDED", 8) == 0) {
    <<o_complex_read() : add an embedded complex>>
  } else {
    <<o_complex_read() : add a complex from library>>
  }

  return object_list;
}

@ %def o_complex_read


<<o_complex_read() : add an embedded complex>>=

  object_list = o_complex_add_embedded(w_current, 
                                       object_list, type, 
                                       WHITE, x1, y1, angle,
                                       "EMBEDDED", basename, 
                                       selectable);

@ 

<<o_complex_read() : add a complex from library>>=
const GSList *clibs = s_clib_search_basename (basename);
if (clibs == NULL) {
  s_log_message("Component [%s] was not found in any component library\n", 
                basename);
  fprintf(stderr,
          "Component [%s] was not found in any component library\n", 
          basename);
  clib = NULL;
} else {
  if (g_slist_next (clibs)) {
    s_log_message ("More than one component found with name [%s]\n",
                   basename);
    /* PB: for now, use the first directory in clibs */
    /* PB: maybe open a dialog to select the right one? */
  }
  clib = (gchar*)clibs->data;
}

object_list = o_complex_add(w_current, object_list, type, 
                            WHITE, 
                            x1, y1, 
                            angle, mirror,
                            clib, basename, selectable, FALSE);

@ 


@section Function @code{o_complex_save()}

@defun o_complex_save buf object
@end defun

<<o_complex_basic.c : o_complex_save()>>=
char *
o_complex_save(OBJECT *object)
{
  int selectable;
  char *buf = NULL;

  if (object->sel_func != NULL) 
  selectable = 1;
  else 
  selectable = 0;

  if (object->type == OBJ_COMPLEX) {
    buf = g_strdup_printf("%c %d %d %d %d %d %s", object->type,
	    		  object->complex->x, object->complex->y, selectable, 
			  object->complex->angle, object->complex->mirror, 
			  object->complex_basename);
  } else if (object->type == OBJ_PLACEHOLDER) {
    buf = g_strdup_printf("C %d %d %d %d %d %s",
	    		  object->complex->x, object->complex->y, selectable, 
			  object->complex->angle, object->complex->mirror, 
			  object->complex_basename);  /* write 'C' manually */
  }

  return(buf);
}

@ %def o_complex_save


@section Function @code{o_complex_set_filename()}

@defun o_complex_set_filename w_current clib basename
@end defun

<<o_complex_basic.c : o_complex_set_filename()>>=
void 
o_complex_set_filename(TOPLEVEL *w_current, char *clib, char *basename) 
{
  int len;

  if (basename == NULL) {
    fprintf(stderr, "Got NULL basename in o_complex_set_filename!\n");
    exit(-1);
  }

  if (clib == NULL) {
    fprintf(stderr, "Got NULL clib in o_complex_set_filename!\n");
    exit(-1);
  }

  if (w_current->internal_basename) {
    free(w_current->internal_basename);
  }

  if (w_current->internal_clib) {
    free(w_current->internal_clib);
  }

  len = strlen(basename);
  w_current->internal_basename = (char *) malloc(sizeof(char)*len+1);

  len = strlen(clib) + 1;	
  w_current->internal_clib = (char *) malloc(sizeof(char)*len+1);

  strcpy(w_current->internal_basename, basename);	
  strcpy(w_current->internal_clib, clib);	
} 

@ %def o_complex_set_filename


@section Function @code{o_complex_free_filename()}

@defun o_complex_free_filename w_current
@end defun

<<o_complex_basic.c : o_complex_free_filename()>>=
void
o_complex_free_filename(TOPLEVEL *w_current)
{
  if (w_current->internal_basename) {
    free(w_current->internal_basename);
  }

  if (w_current->internal_clib) {
    free(w_current->internal_clib);
  }
}

@ %def o_complex_free_filename


@section Function @code{o_complex_translate()}

@defun o_complex_translate w_current dx dy object
@end defun

<<o_complex_basic.c : o_complex_translate()>>=
/* now I think it works fine */
/* no there is a bug with snap locking.  Basically if you don't snap/lock in */
/* PCtoW, then this doesn't work... :(  I don't know why yet */
void
o_complex_translate(TOPLEVEL *w_current, int dx, int dy, OBJECT *object)
{
  int x, y;
  int prevx, prevy;

  if (object == NULL) {
    printf("cmpt NO!\n");
    return;
  }

  object->complex->screen_x = object->complex->screen_x + dx;
  object->complex->screen_y = object->complex->screen_y + dy;


  /* this fixing makes me nervious hack */
  SCREENtoWORLD(w_current, object->complex->screen_x,
                object->complex->screen_y, &x, &y);

  prevx = object->complex->x;
  prevy = object->complex->y;
  object->complex->x = snap_grid(w_current, x);
  object->complex->y = snap_grid(w_current, y);

  o_complex_world_translate(w_current, x - prevx,y - prevy, 
                            object->complex->prim_objs);
}

@ %def o_complex_translate


@section Function @code{o_complex_world_translate()}

@defun o_complex_world_translate w_current x1 y1 prim_objs
@end defun

<<o_complex_basic.c : o_complex_world_translate()>>=
/* this needs work remove display stuff */
/* libhack */
/* and recalc stuff */
/* this function takes in a complex list */
void
o_complex_world_translate(TOPLEVEL *w_current, int x1, int y1, 
			  OBJECT *prim_objs)
{
  OBJECT *o_current=NULL;
  OBJECT *one=NULL;
  OBJECT *two=NULL;
  unsigned long temp_color;

  o_current = prim_objs;

  while ( o_current != NULL ) {
    switch(o_current->type) {
      case(OBJ_LINE):
        o_line_translate_world(w_current, x1, y1, o_current);
        break;

      case(OBJ_NET):
				/* same as a line, don't do this */
        o_line_translate_world(w_current, x1, y1, o_current);
        temp_color = w_current->override_color;
        w_current->override_color = -1;
        o_redraw_single(w_current, one); /* trying loop? hack*/
        o_redraw_single(w_current, two);
        w_current->override_color = temp_color;
        break;

      case(OBJ_BUS):
				/* same as a line, don't do this */
        o_line_translate_world(w_current, x1, y1, o_current);
        temp_color = w_current->override_color;
        w_current->override_color = -1;
        o_redraw_single(w_current, one); /* trying loop? hack*/
        o_redraw_single(w_current, two);
        w_current->override_color = temp_color;
        break;
	
      case(OBJ_BOX):
        o_box_translate_world(w_current, x1, y1, o_current);
        break;

      case(OBJ_PICTURE):
        o_picture_translate_world(w_current, x1, y1, o_current);
        break;

      case(OBJ_CIRCLE):
        o_circle_translate_world(w_current, x1, y1, o_current);
        break;
	
      case(OBJ_COMPLEX):
      case(OBJ_PLACEHOLDER):
        o_complex_world_translate_toplevel(w_current, x1, y1, o_current);
        break;

      case(OBJ_TEXT):
        o_text_translate_world(w_current, x1, y1, o_current);
        break;

        /* same note as above */
      case(OBJ_PIN):
        o_pin_translate_world(w_current, x1, y1, o_current);
        break;

      case(OBJ_ARC):
        o_arc_translate_world(w_current, x1, y1, o_current);
        break;
    }
    o_current=o_current->next;
  }
}

@ o_complex_translate_world


@section Function @code{o_complex_world_translate_toplevel()}

@defun o_complex_world_translate_toplevel w_current x1 y1 object
@end defun

<<o_complex_basic.c : o_complex_world_translate_toplevel()>>=
/* this function takes the toplevel object and then also translates the
 * complex */
void
o_complex_world_translate_toplevel(TOPLEVEL *w_current, int x1, int y1, OBJECT *object)
{
  int left, right, top, bottom;

  object->complex->x = object->complex->x + x1;
  object->complex->y = object->complex->y + y1;

  WORLDtoSCREEN(w_current, object->complex->x,
                object->complex->y,
                &object->complex->screen_x,
                &object->complex->screen_y);

  o_complex_world_translate(w_current, x1, y1, 
                            object->complex->prim_objs);

  get_complex_bounds(w_current, object->complex->prim_objs, 
                     &left, &top, &right, &bottom);
	
  object->left = left;
  object->top = top;
  object->right = right;
  object->bottom = bottom;
}

@ %def o_complex_world_translate_toplevel


@section Function @code{o_complex_copy()}

@defun o_complex_copy w_current list_tail o_current
@end defun

<<o_complex_basic.c : o_complex_copy()>>=
OBJECT *
o_complex_copy(TOPLEVEL *w_current, OBJECT *list_tail, OBJECT *o_current)
{
  OBJECT *new_obj=NULL;
  ATTRIB *a_current;
  int color;
  int selectable;

  if (o_current->saved_color == -1) {
    color = o_current->color;
  } else {
    color = o_current->saved_color;
  }
	
  if (o_current->sel_func) {
    selectable = TRUE;	
  } else {
    selectable = FALSE;	
  }

  new_obj = o_complex_add(w_current, list_tail, o_current->type, color,
                          o_current->complex->x, o_current->complex->y, 
                          o_current->complex->angle, o_current->complex->mirror,
                          o_current->complex_clib, o_current->complex_basename, 
                          selectable, FALSE); 

  o_attrib_slot_copy(w_current, o_current, new_obj);

  /* deal with stuff that has changed */

  /* here you need to create a list of attributes which need to be 
   * connected to the new list, probably make an attribute list and
   * fill it with sid's of the attributes */
  a_current = o_current->attribs;
  if (a_current) {
    while ( a_current ) {

      /* head attrib node has prev = NULL */	
      if (a_current->prev != NULL) {
        a_current->copied_to = new_obj;	
      }
	
      a_current = a_current->next;
    }
  }

  return(new_obj);
}

@ %def o_complex_copy


@section Function @code{o_complex_copy_embedded()}

@defun o_complex_copy_embedded w_current list_tail o_current
@end defun

<<o_complex_basic.c : o_complex_copy_embedded()>>=
OBJECT *
o_complex_copy_embedded(TOPLEVEL *w_current, OBJECT *list_tail, OBJECT *o_current)
{
  OBJECT *new_obj=NULL;
  OBJECT *temp_list;
  ATTRIB *a_current;
  int color;
  int selectable;

  if (o_current->saved_color == -1) {
    color = o_current->color;
  } else {
    color = o_current->saved_color;
  }

  if (o_current->sel_func) {
    selectable = TRUE;	
  } else {
    selectable = FALSE;	
  }

  new_obj = o_complex_add_embedded(w_current, list_tail, o_current->type, 
                                   color,
                                   o_current->complex->x, o_current->complex->y, 
                                   o_current->complex->angle, 
                                   o_current->complex_clib, 
                                   o_current->complex_basename, 
                                   selectable); 
  /* deal with stuff that has changed */
	
  temp_list = o_list_copy_all(w_current,
                              o_current->complex->prim_objs->next,
                              new_obj->complex->prim_objs, 
                              NORMAL_FLAG);
	
  new_obj->complex->prim_objs = return_head(temp_list);

  /* here you need to create a list of attributes which need to be 
   * connected to the new list, probably make an attribute list and
   * fill it with sid's of the attributes */
  a_current = o_current->attribs;
  if (a_current) {
    while ( a_current ) {

      /* head attrib node has prev = NULL */	
      if (a_current->prev != NULL) {
        a_current->copied_to = new_obj;	
      }
	
      a_current = a_current->next;
    }
  }

  return(new_obj);
}

@ %def o_complex_copy_embedded


@section Function @code{o_complex_delete()}

@defun o_complex_delete w_current delete
@end defun

<<o_complex_basic.c : o_complex_delete()>>=
void
o_complex_delete(TOPLEVEL *w_current, OBJECT *delete)
{
  /* first remove complex pointer */
  if (delete->complex) {
    if (delete->complex->prim_objs) {
      s_delete_list_fromstart(w_current, 
                              delete->complex->prim_objs);
    }
    delete->complex->prim_objs = NULL;
  }

  /* then remove the actual node */	
  s_delete(w_current, delete);
  delete = NULL;
  w_current->page_current->object_tail = (OBJECT *) 
  return_tail(w_current->page_current->object_head);
}

@ %def o_complex_delete


@section Function @code{o_complex_set_color()}

@defun o_complex_set_color prim_objs color
@end defun

<<o_complex_basic.c : o_complex_set_color()>>=
void
o_complex_set_color(OBJECT *prim_objs, int color)
{
  OBJECT *o_current=NULL;

  o_current = prim_objs;

  while ( o_current != NULL ) {
    switch(o_current->type) {
      case(OBJ_LINE):
      case(OBJ_NET):
      case(OBJ_BUS):
      case(OBJ_BOX):
      case(OBJ_PICTURE):
      case(OBJ_CIRCLE):
      case(OBJ_PIN):
      case(OBJ_ARC):
        o_current->color = color;
        break;

      case(OBJ_TEXT):
        o_current->color = color;
        o_complex_set_color(o_current->text->prim_objs, color);
        break;

      case(OBJ_COMPLEX):
      case(OBJ_PLACEHOLDER):
        o_current->color = color;
        o_complex_set_color(o_current->complex->prim_objs, color);
        break;

    }
    o_current=o_current->next;
  }
}

@ %def o_complex_set_color


@section Function @code{o_complex_set_color_single()}

@defun o_complex_set_color_single o_current color
@end defun

<<o_complex_basic.c : o_complex_set_color_single()>>=
void
o_complex_set_color_single(OBJECT *o_current, int color)
{
  switch(o_current->type) {
    case(OBJ_LINE):
    case(OBJ_NET):
    case(OBJ_BUS):
    case(OBJ_BOX):
    case(OBJ_PICTURE):
    case(OBJ_CIRCLE):
    case(OBJ_PIN):
    case(OBJ_ARC):
    o_current->color = color;
    break;

    case(OBJ_TEXT):
    o_current->color = color;
    o_complex_set_color(o_current->text->prim_objs, color);
    break;

    case(OBJ_COMPLEX):
    case(OBJ_PLACEHOLDER):
    o_current->color = color;
    o_complex_set_color(o_current->complex->prim_objs, 
                        color);
    break;

  }
}

@ %def o_complex_set_color_single


@section Function @code{o_complex_set_color_save()}

@defun o_complex_set_color_save complex color
@end defun

<<o_complex_basic.c : o_complex_set_color_save()>>=
void
o_complex_set_color_save(OBJECT *complex, int color)
{
  OBJECT *o_current=NULL;

  o_current = complex;

  while ( o_current != NULL ) {
    switch(o_current->type) {
      case(OBJ_LINE):
      case(OBJ_NET):
      case(OBJ_BUS):
      case(OBJ_BOX):
      case(OBJ_PICTURE):
      case(OBJ_CIRCLE):
      case(OBJ_PIN):
      case(OBJ_ARC):
        o_current->saved_color = o_current->color;
        o_current->color = color;
        break;

      case(OBJ_TEXT):
        o_current->saved_color = o_current->color;
        o_current->color = color;
        o_complex_set_color_save(
                                 o_current->text->prim_objs, 
                                 color);
        break;

      case(OBJ_COMPLEX):
      case(OBJ_PLACEHOLDER):
        o_current->saved_color = o_current->color;
        o_current->color = color;
        o_complex_set_color_save(o_current->complex->
                                 prim_objs,
                                 color);
        break;

    }
    o_current=o_current->next;
  }
}

@ %def o_complex_set_color_save


@section Function @code{o_complex_unset_color()}

@defun o_complex_unset_color complex
@end defun

<<o_complex_basic.c : o_complex_unset_color()>>=
void
o_complex_unset_color(OBJECT *complex)
{
  OBJECT *o_current=NULL;

  o_current = complex;

  while ( o_current != NULL ) {
    switch(o_current->type) {
      case(OBJ_LINE):
      case(OBJ_NET):
      case(OBJ_BUS):
      case(OBJ_BOX):
      case(OBJ_PICTURE):
      case(OBJ_CIRCLE):
      case(OBJ_PIN):
      case(OBJ_ARC):
        o_current->color = o_current->saved_color;
        o_current->saved_color = -1;
        break;

      case(OBJ_TEXT):
        o_current->color = o_current->saved_color;
        o_current->saved_color = -1;
        o_complex_unset_color(o_current->text->prim_objs);
        break;

      case(OBJ_COMPLEX):
      case(OBJ_PLACEHOLDER):
        o_current->color = o_current->saved_color;
        o_current->saved_color = -1;
        o_complex_unset_color(o_current->complex->
                              prim_objs);

        break;

    }
    o_current=o_current->next;
  }
}

@ %def o_complex_unset_color


@section Function @code{o_complex_unset_color_single()}

@defun o_complex_unset_color_single o_current
@end defun

<<o_complex_basic.c : o_complex_unset_color_single()>>=
void
o_complex_unset_color_single(OBJECT *o_current)
{
  switch(o_current->type) {
    case(OBJ_LINE):
    case(OBJ_NET):
    case(OBJ_BUS):
    case(OBJ_BOX):
    case(OBJ_PICTURE):
    case(OBJ_CIRCLE):
    case(OBJ_PIN):
    case(OBJ_ARC):
    o_current->color = o_current->saved_color;
    o_current->saved_color = -1;
    break;

    case(OBJ_TEXT):
    o_current->color = o_current->saved_color;
    o_current->saved_color = -1;
    o_complex_unset_color(o_current->text->prim_objs);
    break;

    case(OBJ_COMPLEX):
    case(OBJ_PLACEHOLDER):
    o_current->color = o_current->saved_color;
    o_current->saved_color = -1;
    o_complex_unset_color(o_current->complex->prim_objs);

    break;
  }
}

@ %def o_complex_unset_color_single


@section Function @code{o_complex_set_saved_color_only()}

@defun o_complex_set_saved_color_only complex color
@end defun

<<o_complex_basic.c : o_complex_set_saved_color_only()>>=
void
o_complex_set_saved_color_only(OBJECT *complex, int color)
{
  OBJECT *o_current=NULL;

  o_current = complex;

  while ( o_current != NULL ) {
    switch(o_current->type) {
      case(OBJ_LINE):
      case(OBJ_NET):
      case(OBJ_BUS):
      case(OBJ_BOX):
      case(OBJ_PICTURE):
      case(OBJ_CIRCLE):
      case(OBJ_PIN):
      case(OBJ_ARC):
        o_current->saved_color = color;
        break;

      case(OBJ_TEXT):
        o_current->saved_color = color;
        o_complex_set_saved_color_only(o_current->text->prim_objs, color);
        break;

      case(OBJ_COMPLEX):
      case(OBJ_PLACEHOLDER):
        o_current->saved_color = color;
        o_complex_set_saved_color_only(o_current->complex->prim_objs, color);
        break;

    }
    o_current=o_current->next;
  }
}

@ %def o_complex_set_saved_color_only


@section Function @code{o_complex_return_nth_pin()}

@defun o_complex_return_nth_pin o_lits counter
@end defun

<<o_complex_basic.c : o_complex_return_nth_pin()>>=
/* returns the counter'th pin in o_list */
/* NULL if there is no more pins */
OBJECT *
o_complex_return_nth_pin(OBJECT *o_list, int counter)
{
  OBJECT *o_current;
  int internal_counter=0;
	
  o_current = o_list;
	
  while (o_current != NULL) {
    if (o_current->type == OBJ_PIN) {
			
      if (counter == internal_counter) {
        return(o_current);
      } else {
        internal_counter++;	
      }
    }	
    o_current = o_current->next;
  }
	
  return(NULL);
}

@ %def o_complex_return_nth_pin


@section Function @code{o_complex_rotate_lowlevel()}

@defun o_complex_rotate_lowlevel w_current world_centerx world_centery angle angle_change object
@end defun

<<o_complex_basic.c : o_complex_rotate_lowlevel()>>=
/* pass in top level object */
void
o_complex_rotate_lowlevel(TOPLEVEL *w_current, int world_centerx, 
	int world_centery, 
	int angle,
	int angle_change,
	OBJECT *object)
{
  OBJECT *o_current=NULL;

#if DEBUG 
  printf("------- a %d ac %d\n", angle, angle_change);
#endif

  /* do individual complex objects */
  o_current = object->complex->prim_objs;

  while ( o_current != NULL ) {
    switch(o_current->type) {
      case(OBJ_LINE):
        o_line_rotate_world(w_current, 0, 0, angle_change, o_current);
        break;

      case(OBJ_NET):
        o_net_rotate_world(w_current, 0, 0, angle_change, o_current);
        break;

      case(OBJ_BUS):
        o_bus_rotate_world(w_current, 0, 0, angle_change, o_current);
        break;
	
      case(OBJ_BOX):
        o_box_rotate_world(w_current, 0, 0, angle_change, o_current);
        break;

      case(OBJ_PICTURE):
#ifndef HAS_GTK12
        o_picture_rotate_world(w_current, 0, 0, angle_change, o_current);
#endif
        break;

      case(OBJ_CIRCLE):
        o_circle_rotate_world(w_current, 0, 0, angle_change, o_current);
        break;

      case(OBJ_PIN):
        o_pin_rotate_world(w_current, 0, 0, angle_change, o_current);
        break;

      case(OBJ_ARC):
        o_arc_rotate_world(w_current, 0, 0, angle_change, o_current);
        break;

#if 0	/* complex within a complex? not right now */
      case(OBJ_COMPLEX):
      case(OBJ_PLACEHOLDER):
        break;
#endif

      case(OBJ_TEXT):
        o_text_rotate_world(w_current, 0, 0, angle, angle_change, o_current);
        break;

    }
    o_current=o_current->next;
  }
}

@ %def o_complex_rotate_lowlevel


@section Function @code{o_complex_mirror_lowlevel()}

@defun o_complex_mirror_lowlevel w_current world_centerx world_centery object
@end defun

<<o_complex_basic.c : o_complex_mirror_lowlevel()>>=
/* pass in top level object */
void
o_complex_mirror_lowlevel(TOPLEVEL *w_current, 
	int world_centerx, int world_centery, OBJECT *object)
{
  OBJECT *o_current=NULL;

  /* do individual complex objects */
  o_current = object->complex->prim_objs;

  while ( o_current != NULL ) {
    switch(o_current->type) {
      case(OBJ_LINE):
        o_line_mirror_world(w_current, 0, 0, o_current);
        break;

      case(OBJ_NET):
        o_net_mirror_world(w_current, 0, 0, o_current);
        break;

      case(OBJ_BUS):
        o_bus_mirror_world(w_current, 0, 0, o_current);
        break;
	
      case(OBJ_BOX):
        o_box_mirror_world(w_current, 0, 0, o_current);
        break;

      case(OBJ_PICTURE):
#ifndef HAS_GTK12
        o_picture_mirror_world(w_current, 0, 0, o_current);
#endif
        break;

      case(OBJ_CIRCLE):
        o_circle_mirror_world(w_current, 0, 0, o_current);
        break;

      case(OBJ_PIN):
        o_pin_mirror_world(w_current, 0, 0, o_current);
        break;

      case(OBJ_ARC):
        o_arc_mirror_world(w_current, 0, 0, o_current);
        break;

#if 0	/* complex within a complex? not right now */
      case(OBJ_COMPLEX):
      case(OBJ_PLACEHOLDER):
        break;
#endif

      case(OBJ_TEXT):
        o_text_mirror_world(w_current, 0, 0, o_current);
        break;

    }
    o_current=o_current->next;
  }

  /* mirror origin point */
  /*	object->x = -object->x;*/
}

@ %def o_complex_mirror_lowlevel


@section Function @code{o_complex_return_pin_object()}

@defun o_complex_return_pin_object object pin
@end defun

<<o_complex_basic.c : o_complex_return_pin_object()>>=
/* pass in top level object */
OBJECT *
o_complex_return_pin_object(OBJECT *object, char *pin) 
{
  OBJECT *o_current=NULL;
  OBJECT *found;

  /* go inside complex objects */
  o_current = object->complex->prim_objs;

  while ( o_current != NULL ) {
    switch(o_current->type) {
      case(OBJ_PIN):

        /* Search for the pin making sure that */
        /* any found attribute starts with "pinnumber" */
        found = o_attrib_search_attrib_value(o_current->attribs, pin,
                                             "pinnumber", 0);
        if (found) {
#if DEBUG
          printf("%s: %s\n", found->name,
                 found->text->string);
#endif
          return(o_current);
        }
        break;
    }
    o_current=o_current->next;
  }
  return(NULL);
}

@ %def o_complex_return_pin_object



@section Function @code{o_complex_check_symversion()}

@defun o_complex_check_symversion w_current object
@end defun

<<o_complex_basic.c : o_complex_check_symversion()>>=
void
o_complex_check_symversion(TOPLEVEL* w_current, OBJECT* object) 
{
  char *inside = NULL;
  char *outside = NULL;
  char *refdes = NULL;
  double inside_value = -1.0;
  double outside_value = -1.0;
  char *err_check = NULL;
  int inside_present = FALSE;
  int outside_present = FALSE;
  double inside_major, inside_minor;
  double outside_major, outside_minor;
  
  if (object->type != OBJ_COMPLEX && object->type != OBJ_PLACEHOLDER)
  {
    return;
  }

  /* first look on the inside for the symversion= attribute */
  inside = o_attrib_search_name(object->complex->prim_objs, "symversion", 0);

  /* now look for the symversion= attached to object */
  outside = o_attrib_search_attrib_name(object->attribs, "symversion", 0);

  /* get the uref for future use */
  refdes = o_attrib_search_attrib_name(object->attribs, "refdes", 0);
  if (!refdes)
  {
    refdes = g_strdup ("unknown");
  }
  
  if (inside)
  {
    inside_value = strtod(inside, &err_check);
    if (inside_value == 0 && inside == err_check)
    {
      if (inside)
      { 
        s_log_message("WARNING: Symbol version parse error on refdes %s:\n"
                    "\tCould not parse symbol file symversion=%s\n",
                     refdes, inside);
      } else {
        s_log_message("WARNING: Symbol version parse error on refdes %s:\n"
                    "\tCould not parse symbol file symversion=\n",
                     refdes);
      }
      goto done;
    }
    inside_present = TRUE;
  } else {
    inside_present = FALSE;  /* attribute not inside */
  }

  if (outside)
  {
    outside_value = strtod(outside, &err_check);
    if (outside_value == 0 && outside == err_check)
    {
      s_log_message("WARNING: Symbol version parse error on refdes %s:\n"
                    "\tCould not parse attached symversion=%s\n",
                    refdes, outside);
      goto done;
    }
    outside_present = TRUE; 
  } else {
    outside_present = FALSE;  /* attribute not outside */
  }

#if DEBUG
  printf("%s:\n\tinside: %.1f outside: %.1f\n\n", object->name,
         inside_value, outside_value);
#endif
  
  /* symversion= is not present anywhere */
  if (!inside_present && !outside_present)
  {
    /* symbol is legacy and versioned okay */
    goto done;
  }

  /* No symversion inside, but a version is outside, this is a weird case */
  if (!inside_present && outside_present)
  {
    s_log_message("WARNING: Symbol version oddity on refdes %s:\n"
                  "\tsymversion=%s attached to instantiated symbol, "
                  "but no symversion= inside symbol file\n",
                  refdes, outside);
    goto done;
  }

  /* inside & not outside is a valid case, means symbol in library is newer */
  /* also if inside_value is greater than outside_value, then symbol in */
  /* library is newer */
  if ((inside_present && !outside_present) ||
      ((inside_present && outside_present) && (inside_value > outside_value)))
  {
    
    fprintf(stderr, "WARNING: Symbol version mismatch on refdes %s (%s):\n"
            "\tSymbol in library is newer than "
            "instantiated symbol\n",
            refdes, object->complex_basename);
    s_log_message("WARNING: Symbol version mismatch on refdes %s (%s):\n"
                  "\tSymbol in library is newer than "
                  "instantiated symbol\n",
                  refdes, object->complex_basename);

    /* break up the version values into major.minor numbers */
    inside_major = floor(inside_value);
    inside_minor = inside_value - inside_major;

    if (outside_present)
    {
      outside_major = floor(outside_value);
      outside_minor = outside_value - outside_major;
    } else {
      /* symversion was not attached to the symbol, set all to zero */
      outside_major = 0.0;
      outside_minor = 0.0;
      outside_value = 0.0;
    }

#if DEBUG
    printf("i: %f %f %f\n", inside_value, inside_major, inside_minor);
    printf("o: %f %f %f\n", outside_value, outside_major, outside_minor);
#endif
    
    if (inside_major > outside_major)
    {
      char* refdes_copy;
      fprintf(stderr, "\tMAJOR VERSION CHANGE (file %.3f, "
              "instantiated %.3f, %s)!\n",
              inside_value, outside_value, refdes);
      s_log_message("\tMAJOR VERSION CHANGE (file %.3f, "
                    "instantiated %.3f)!\n",
                    inside_value, outside_value);

      /* add the refdes to the major_changed_refdes GList */
      /* make sure refdes_copy is freed somewhere */
      refdes_copy = g_strconcat (refdes, " (",
                                 object->complex_basename,
                                 ")", NULL);
      w_current->major_changed_refdes =
        g_list_append(w_current->major_changed_refdes, refdes_copy);

      /* don't bother checking minor changes if there are major ones*/
      goto done; 
    }

    if (inside_minor > outside_minor)
    {
      fprintf(stderr, "\tMinor version change (file %.3f, "
              "instantiated %.3f)\n",
              inside_value, outside_value);
      s_log_message("\tMinor version change (file %.3f, "
                    "instantiated %.3f)\n",
                    inside_value, outside_value);
    }

    goto done;
  }

  /* outside value is greater than inside value, this is weird case */
  if ((inside_present && outside_present) && (outside_value > inside_value))
  {
    s_log_message("WARNING: Symbol version oddity on refdes %s:\n"
                  "\tInstanciated symbol is newer than "
                  "symbol in library\n",
                  refdes);
    goto done;
  }

  /* if inside_value and outside_value match, then symbol versions are okay */

done:
  if (inside) free(inside);
  if (outside) free(outside);
  if (refdes) free(refdes);
}

@ %def o_complex_check_symversion