Source

Coin / src / nodekits / SoBaseKit.cpp

Full commit
   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
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
/**************************************************************************\
 * Copyright (c) Kongsberg Oil & Gas Technologies AS
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\**************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H

#ifdef HAVE_NODEKITS

/*!
  \class SoBaseKit SoBaseKit.h Inventor/nodekits/SoBaseKit.h
  \brief The SoBaseKit class is the toplevel superclass for nodekits.
  \ingroup nodekits

  Node kits are collections of nodes and other node kits (from here on
  node kits which are part of some other node kit, will only be referred
  to as nodes or parts, see catalogs and parts), organized in a way
  that is convenient for its use. A node kit inherits SoNode and can
  thus be inserted into a scenegraph as any other node.

  The organizing of the nodes and node kits of some node kit, is done
  through catalogs. A node kit's catalog describes the nodes that can
  be members of the node kit. These members are called parts. Thus a
  node kit has a catalog describing the parts that it offers to the
  user.

  Each part in the catalog has some values saying something about the
  part itself and about the role the part plays in the scenegraph.
  Those values are:

  <dl>
  <dt> Name
  <dd> The name of the part.
  <dt> Type
  <dd> The part's node type.
  <dt> Default Type
  <dd> If the part's type is an abstract superclass, this value will hold
  the default subclass used by this part.
  <dt> Created by Default?
  <dd> Holds \c TRUE if the part should be instantiated when the node kit
  is instantiated, otherwise the part is kept empty until it is set by some
  of the means applicable.
  <dt> Parent Name
  <dd> The name of the part that is this part's parent.
  <dt> Right Sibling
  <dd> The name of the part that is the part immediately to the right of
  this part in the node kit scenegraph.
  <dt> Is it a List?
  <dd> Holds \c TRUE if the part is a list, otherwise it is \c FALSE. See
  SoNodeKitListPart for more info on node kit lists.
  <dt> List Cointainer Type
  <dd> The type of group node used to hold the items if the part is a list.
  <dt> List Element Type
  <dd> The types of nodes that is allowed to be held by this part if the part
  is a list.
  <dt> Is It Public?
  <dd> Holds \c TRUE if the part should be publically available, otherwise
  it holds \c FALSE.
  </dl>

  Node kits use lazy instantiation when it creates it's parts. This means
  that the nodes making up the parts of the nodekit only are created when
  they are needed. If the "Created by Default?" holds TRUE, then the part
  is created when the node kit itself is instantiated. If not, parts are
  created when they are requested through SoBaseKit::getPart() or the
  SO_GET_PART() macro, or created with SoBaseKit::set(). Also, if a part is
  set with SoBaseKit::setPart() or the SO_SET_PART() macro, any previously
  uncreated parts above the set part in the hierarchy, is created
  automatically.

  The advantages of using node kits to represent a scenegraph are many.
  \li Since a node kit collects nodes into a single unit, it becomes
      an extra abstraction layer for the application programmer. Such
      a layer can represent a model of a human being as one unit where
      subunits as arms, legs, textures, etc are contained within. Thus
      we can instantiate a model of a human by creating an instance of
      the node kit, instead of having to create a possibly large
      amount of nodes needed for such a model.
  \li A part of the node kit doesn't have one specific setup. A shape part
      can e.g. be swapped with any other shape, since they are of the same
      type. If the node kit of a human has a part called "head" which is of
      type SoShape, it might default to a sphere. But if the programmer
      thinks that a cube might fit better, one can set the "head" part to
      a cube instead, or maybe a face set representing a complex model of
      a head.
  \li Node kits can have as simple or as complex catalogs as needed. The
      nodes included in the node kit can, if needed, represent the
      whole range of Inventor features. One part can as easily be of a
      node kit type, making it possible to create hierarchies of node kits.
      Having a node kit of a human, it might be feasible to have sub node
      kits describing the different body parts.
  \li Node kits are an efficient way of creating scenegraphs. If some
      part of it isn't needed at the moment of node kit instantiation,
      they aren't created. Thus parts are only created when needed, either
      by the application or some other part.
  \li The application code becomes smaller and easier to read, as the node
      kits provides simple routines for creating and setting parts.
  \li New node kits can be created through subclassing to obtain simple
      setups of scenegraphs best fitted to the application.

  The usage of a node kit is straightforward. Below follows a code
  example showing some simple SoShapeKit usage.

  \code

  #include <Inventor/Qt/SoQt.h>
  #include <Inventor/Qt/viewers/SoQtExaminerViewer.h>
  #include <Inventor/nodekits/SoShapeKit.h>
  #include <Inventor/nodes/SoSeparator.h>
  #include <Inventor/nodes/SoCube.h>

  int
  main(int argc, char ** argv)
  {
    QWidget * window = SoQt::init(argv[0]);

    SoQtExaminerViewer * viewer = new SoQtExaminerViewer(window);

    // Instantiating a shape kit, by default creating a simple sphere.
    SoShapeKit * shapekit = new SoShapeKit;
    // Swapping the sphere with a cube.
    shapekit->setPart("shape", new SoCube);
    // Setting the cube to be rendered in the color red. The shape kit
    // has a SoAppearanceKit as one of it's parts. The "material" part
    // used to set the color of the shape, really belongs the
    // appearance kit. If the SoShapeKit::set() is used, it will
    // check if some of its sub kits has a part with the name given,
    // and delegate the setting to the correct kit.
    shapekit->set("material", "diffuseColor 1 0 0");

    SoSeparator * newroot = new SoSeparator;
    newroot->ref();

    newroot->addChild(shapekit);

    viewer->setSceneGraph(newroot);

    viewer->show();
    SoQt::show(window);

    SoQt::mainLoop();
    delete viewer;

    newroot->unref();
    return 0;
  }
  \endcode

  The above code snippet will produce a viewer with a side view to
  the scene shown below:

  <center>
  <img src="http://doc.coin3d.org/images/Coin/nodekits/basekitexample.png">
  </center>

  Notice that the code needed for creating this simple shape using
  a shape kit, amounts to this:

  \code
   SoShapeKit * shapekit = new SoShapeKit;

   shapekit->setPart("shape", new SoCube);
   shapekit->set("material", "diffuseColor 1 0 0");
  \endcode

  ..while doing it without shape kits amounts to this:

  \code
  SoSeparator * root = new SoSeparator;
  SoMaterial * material = new SoMaterial;
  material->diffuseColor.setValue(1,0,0);
  root->addChild(material);
  root->addChild(new SoCube);
  \endcode

  ..so even for this miniscule mock-up example, you save on code
  verbosity and complexity.


  \TOOLMAKER_REF


  Following is a complete example of a node kit extension. The node
  kit is a kit which automatically scales a shape so it will be the
  same size in screen-pixels, no matter which distance it is from the
  camera. This is useful for marker graphics. The shape defaults to a
  cube, but can be set by the programmer to any shape or scene
  sub-graph.

  The header file:

  \code
  // Copyright (C) Kongsberg Oil & Gas Technologies. All rights reserved.

  #ifndef COIN_SHAPESCALE_H
  #define COIN_SHAPESCALE_H
  #include <Inventor/nodekits/SoSubKit.h>
  #include <Inventor/nodekits/SoBaseKit.h>
  #include <Inventor/fields/SoSFFloat.h>

  class SbViewport;
  class SoState;
  class SbColor;
  class SbVec2s;

  class ShapeScale : public SoBaseKit {
    typedef SoBaseKit inherited;

    SO_KIT_HEADER(ShapeScale);

    SO_KIT_CATALOG_ENTRY_HEADER(topSeparator);
    SO_KIT_CATALOG_ENTRY_HEADER(scale);
    SO_KIT_CATALOG_ENTRY_HEADER(shape);

   public:
    ShapeScale(void);
    static void initClass(void);

    SoSFFloat active;
    SoSFFloat projectedSize;

   protected:
    virtual void GLRender(SoGLRenderAction * action);
    virtual ~ShapeScale();
  };

  #endif // ! SHAPESCALE_H
  \endcode

  The source code for the example:

  \code
  // Copyright (C) Kongsberg Oil & Gas Technologies. All rights reserved.

  //  The ShapeScale class is used for scaling a shape based on
  //  projected size.
  //
  //  This nodekit can be inserted in your scene graph to add for
  //  instance 3D markers that will be of a constant projected size.
  //
  //  The marker shape is stored in the "shape" part. Any kind of node
  //  can be used, even group nodes with several shapes, but the
  //  marker shape should be approximately of unit size, and with a
  //  center position in (0, 0, 0).


  //  SoSFFloat ShapeScale::active
  //  Turns the scaling on/off. Default value is TRUE.


  //  SoSFFloat ShapeScale::projectedSize
  //  The requested projected size of the shape. Default value is 5.0.

  #include "ShapeScale.h"

  #include <Inventor/actions/SoGLRenderAction.h>
  #include <Inventor/nodes/SoShape.h>
  #include <Inventor/nodes/SoScale.h>
  #include <Inventor/nodes/SoCube.h>
  #include <Inventor/nodes/SoSeparator.h>
  #include <Inventor/elements/SoViewVolumeElement.h>
  #include <Inventor/elements/SoViewportRegionElement.h>
  #include <Inventor/elements/SoModelMatrixElement.h>

  SO_KIT_SOURCE(ShapeScale);


  //  Constructor.
  ShapeScale::ShapeScale(void)
  {
    SO_KIT_CONSTRUCTOR(ShapeScale);

    SO_KIT_ADD_FIELD(active, (TRUE));
    SO_KIT_ADD_FIELD(projectedSize, (5.0f));

    SO_KIT_ADD_CATALOG_ENTRY(topSeparator, SoSeparator, FALSE, this, \x0, FALSE);
    SO_KIT_ADD_CATALOG_ABSTRACT_ENTRY(shape, SoNode, SoCube, TRUE, topSeparator, \x0, TRUE);
    SO_KIT_ADD_CATALOG_ENTRY(scale, SoScale, FALSE, topSeparator, shape, FALSE);

    SO_KIT_INIT_INSTANCE();
  }


  // Destructor.
  ShapeScale::~ShapeScale()
  {
  }

  // Initializes this class. Call before using it.

  void
  ShapeScale::initClass(void)
  {
    SO_KIT_INIT_CLASS(ShapeScale, SoBaseKit, "BaseKit");
  }

  static void
  update_scale(SoScale * scale, const SbVec3f & v)
  {
    // only write to field when scaling has changed.
    if (scale->scaleFactor.getValue() != v) {
      scale->scaleFactor = v;
    }
  }

  // Overridden to (re)initialize scaling before rendering marker.
  void
  ShapeScale::GLRender(SoGLRenderAction * action)
  {
    SoState * state = action->getState();

    SoScale * scale = (SoScale*) this->getAnyPart(SbName("scale"), TRUE);
    if (!this->active.getValue()) {
      update_scale(scale, SbVec3f(1.0f, 1.0f, 1.0f));
    }
    else {
      const SbViewportRegion & vp = SoViewportRegionElement::get(state);
      const SbViewVolume & vv = SoViewVolumeElement::get(state);
      SbVec3f center(0.0f, 0.0f, 0.0f);
      float nsize = this->projectedSize.getValue() / float(vp.getViewportSizePixels()[1]);
      SoModelMatrixElement::get(state).multVecMatrix(center, center); // transform to WCS
      float scalefactor = vv.getWorldToScreenScale(center, nsize);
      update_scale(scale, SbVec3f(scalefactor, scalefactor, scalefactor));
    }
    inherited::GLRender(action);
  }
  \endcode

  And a complete example showing how one can use this node kit:

  \code
  // Copyright (C) Kongsberg Oil & Gas Technologies. All rights reserved.

  #include <Inventor/Qt/SoQt.h>
  #include <Inventor/Qt/viewers/SoQtExaminerViewer.h>
  #include <Inventor/SoInput.h>
  #include <Inventor/SoOutput.h>
  #include <Inventor/SoPickedPoint.h>
  #include <Inventor/actions/SoRayPickAction.h>
  #include <Inventor/events/SoMouseButtonEvent.h>
  #include <Inventor/nodes/SoBaseColor.h>
  #include <Inventor/nodes/SoCube.h>
  #include <Inventor/nodes/SoEventCallback.h>
  #include <Inventor/nodes/SoSeparator.h>
  #include <Inventor/nodes/SoSwitch.h>
  #include <Inventor/nodes/SoTranslation.h>
  #include <assert.h>
  #include <stdlib.h>
  #include <time.h>

  #include "ShapeScale.h"

  // Returns random value between 0.0f and 1.0f.
  static float
  normalized_rand(void)
  {
    return float(rand())/float(RAND_MAX);
  }

  static SoSeparator *
  construct_new_marker(const SbVec3f & v)
  {
    SoSeparator * markerroot = new SoSeparator;

    SoTranslation * t = new SoTranslation;
    t->translation = v;
    markerroot->addChild(t);

    ShapeScale * kit = new ShapeScale;
    kit->active = TRUE;
    kit->projectedSize = 5.0f;

    // create the marker
    SoSeparator * markersep = new SoSeparator;

    SoBaseColor * mat = new SoBaseColor;
    mat->rgb.setValue(normalized_rand(), normalized_rand(), normalized_rand());
    markersep->addChild(mat);

    // marker shape should be unit size, with center in (0.0f, 0.0f, 0.0f)
    SoCube * cube = new SoCube;
    cube->width = 1.0f;
    cube->height = 1.0f;
    cube->depth = 1.0f;

    markersep->addChild(cube);
    kit->setPart("shape", markersep);
    markerroot->addChild(kit);

    return markerroot;
  }

  static void
  event_cb(void * ud, SoEventCallback * n)
  {
    const SoMouseButtonEvent * mbe = (SoMouseButtonEvent *)n->getEvent();

    if (mbe->getButton() == SoMouseButtonEvent::BUTTON1 &&
      mbe->getState() == SoButtonEvent::DOWN) {

      SoQtExaminerViewer * viewer = (SoQtExaminerViewer *)ud;

      SoRayPickAction rp(viewer->getViewportRegion());
      rp.setPoint(mbe->getPosition());
      rp.apply(viewer->getSceneManager()->getSceneGraph());

      SoPickedPoint * point = rp.getPickedPoint();
      if (point == NULL) {
        (void)fprintf(stderr, "\n** miss! **\n\n");
        return;
      }

      n->setHandled();

      const SoPath * p = rp.getCurPath();

      for (int i = 0; i < p->getLength(); i++) {
        SoNode * n = p->getNodeFromTail(i);
        if (n->isOfType(SoGroup::getClassTypeId())) {
          SoGroup * g = (SoGroup *)n;
          g->addChild(construct_new_marker(point->getPoint()));
          break;
        }
      }
    }
  }

  void
  show_instructions(void)
  {
    (void)fprintf(stdout,
      "\nThis example program demonstrates the use of the ShapeScale nodekit.\n"
      "\nQuick instructions:\n\n"
      "  * place the marker by clicking on a shape with the left mouse button\n"
      "  * hit ESC to toggle back and forth to view mode\n"
      "  * zoom back and forth to see how the markers stay the same size\n\n");
  }

  int
  main(int argc, char ** argv)
  {
    if (argc != 2) {
      (void) fprintf(stderr,"\nSpecify an Inventor file as argument.\n");
      return -1;
    }

    QWidget * window = SoQt::init(argv[0]);
    ShapeScale::initClass(); // init our extension nodekit

    SoQtExaminerViewer * ex1 = new SoQtExaminerViewer(window);

    SoInput input;
    SbBool ok = input.openFile(argv[1]);
    if (!ok) {
      (void) fprintf(stderr, "Unable to open file: %s\n", argv[1]);
      return -1;
    }

    SoSeparator * root = SoDB::readAll(&input);

    if (root == NULL) {
      (void) fprintf(stderr, "Unable to read file: %s\n", argv[1]);
      return -1;
    }

    show_instructions();

    SoSeparator * newroot = new SoSeparator;
    newroot->ref();

    newroot->addChild(root);

    // create event callback and marker nodes
    SoSeparator * sep = new SoSeparator;
    newroot->addChild(sep);

    SoEventCallback * ecb = new SoEventCallback;
    ecb->addEventCallback(SoMouseButtonEvent::getClassTypeId(), event_cb, ex1);
    sep->addChild(ecb);

    ex1->setSceneGraph(newroot);
    ex1->setTransparencyType(SoGLRenderAction::SORTED_OBJECT_BLEND);
    ex1->setViewing(FALSE);

    ex1->show();
    SoQt::show(window);

    SoQt::mainLoop();
    delete ex1;

    newroot->unref();
    return 0;
  }
  \endcode
*/

#include <Inventor/nodekits/SoBaseKit.h>

#include <stdlib.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>

#include <Inventor/nodekits/SoNodeKitListPart.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoCallback.h>
#include <Inventor/nodes/SoEventCallback.h>
#include <Inventor/misc/SoChildList.h>
#include <Inventor/actions/SoSearchAction.h>
#include <Inventor/actions/SoAudioRenderAction.h>
#include <Inventor/actions/SoGetMatrixAction.h>
#include <Inventor/actions/SoRayPickAction.h>
#include <Inventor/actions/SoGetBoundingBoxAction.h>
#include <Inventor/actions/SoWriteAction.h>
#include <Inventor/SoInput.h>
#include <Inventor/SoOutput.h>
#include <Inventor/details/SoNodeKitDetail.h>
#include <Inventor/SoPickedPoint.h>
#include <Inventor/lists/SoPickedPointList.h>
#include <Inventor/lists/SoNodeList.h>
#include <Inventor/errors/SoReadError.h>
#include <Inventor/C/tidbits.h> // coin_isspace()
#include <Inventor/errors/SoDebugError.h>

#include "coindefs.h" // COIN_OBSOLETED()
#include "io/SoWriterefCounter.h"
#include "nodekits/SoSubKitP.h"

class SoBaseKitP {
public:
  SoBaseKitP(SoBaseKit * kit) : kit(kit) { }

  SoBaseKit * kit;
  SoFieldData * writedata;
  SbBool didcount;

  // This array is a 1-1 mapping of the fields corresponding to the
  // catalog parts. Catalog indices will therefore also be used as
  // indices into this array.
  SbList<SoSFNode*> instancelist;

  void addKitDetail(SoFullPath * path, SoPickedPoint * pp);
  void createWriteData(void);
  void testParentWrite(void);

  void copyParts(const SoBaseKit * srckit, SbList <SoNode*> & partlist,
                 const SbBool copyconnections);

  void setParts(SbList <SoNode*> partlist, const SbBool leafparts);

  SbBool readUnknownFields(SoInput *in, SoFieldData *&unknownFieldData );
};

#define PRIVATE(p) ((p)->pimpl)
#define PUBLIC(p) ((p)->kit)

SbBool SoBaseKit::searchchildren = FALSE;

SO_KIT_SOURCE(SoBaseKit);

/*!
  \fn const SoNodekitCatalog * SoBaseKit::getClassNodekitCatalog(void)
  Returns the nodekit catalog which defines the layout of this
  class' kit.
*/

/*!
  \fn const SoNodekitCatalog * SoBaseKit::getNodekitCatalog(void) const
  Returns the nodekit catalog which defines the layout of this
  class' kit.
*/

/*!
  \fn const SoNodekitCatalog ** SoBaseKit::getClassNodekitCatalogPtr(void)
  Returns the pointer to the pointer of the nodekit catalog
  for this class.
*/


/*!
  \var SoChildList * SoBaseKit::children
  \COININTERNAL
*/
/*!
  \var SbBool SoBaseKit::connectionsSetUp
  \COININTERNAL
*/


/*!
  Constructor.

  This is the top-level superclass of all node kit and dragger
  classes. The catalog structure of SoBaseKit is as follows:

  \verbatim
  CLASS SoBaseKit
  -->"this"
  -->   "callbackList"
  \endverbatim

  \NODEKIT_POST_DIAGRAM

  \NODEKIT_PRE_TABLE

  \verbatim
  CLASS SoBaseKit
  PVT   "this",  SoBaseKit  ---
        "callbackList",  SoNodeKitListPart [ SoCallback, SoEventCallback ]
  \endverbatim

  \NODEKIT_POST_TABLE

  As can be seen from the catalog, all node kits can have a callback
  node in front of all other nodes in the kit. This is handy for
  catching events that should go to application processing.
*/
SoBaseKit::SoBaseKit(void)
{
  PRIVATE(this) = new SoBaseKitP(this);
  PRIVATE(this)->writedata = NULL;

  SO_KIT_INTERNAL_CONSTRUCTOR(SoBaseKit);

  // Can't use ADD_CATALOG_ENTRY macro for the toplevel "this" entry,
  // as we don't want to call SO_NODE_ADD_FIELD(). This is how the
  // invocation would have looked if we could use the macro:
  //
  // SO_KIT_ADD_CATALOG_ENTRY(this, SoBaseKit, TRUE, "", "", FALSE);

  SoBaseKit::classcatalog->addEntry("this",
                                    SoBaseKit::getClassTypeId(),
                                    SoBaseKit::getClassTypeId(),
                                    TRUE,
                                    "",
                                    "",
                                    FALSE,
                                    SoType::badType(),
                                    SoType::badType(),
                                    FALSE);

  SO_KIT_ADD_CATALOG_LIST_ENTRY(callbackList, SoSeparator, TRUE, this, "", SoCallback, TRUE);
  SO_KIT_ADD_LIST_ITEM_TYPE(callbackList, SoEventCallback);

  // this could be created on demand, but will make it more complicated
  this->children = new SoChildList(this);

  this->connectionsSetUp = FALSE;
  SO_KIT_INIT_INSTANCE();
}

/*!
  Destructor.
*/
SoBaseKit::~SoBaseKit()
{
  delete this->children;
  delete PRIVATE(this);
}

// Doc in superclass
void
SoBaseKit::initClass(void)
{
  SO_NODE_INTERNAL_INIT_CLASS(SoBaseKit, SO_FROM_INVENTOR_1);
  // set rayPick method
  SoType type = SoBaseKit::getClassTypeId();
  SoRayPickAction::addMethod(type, SoNode::rayPickS);
  SoAudioRenderAction::addMethod(type,
                                 SoAudioRenderAction::callDoAction);
  SoBaseKit::searchchildren = FALSE;
}

/*!
  Returns a pointer to the node part with \a partname.

  This method calls SoBaseKit::getAnyPart() with \a leafcheck and \a
  publiccheck both set to \c TRUE.

  See the documentation of SoBaseKit::getAnyPart() for information on
  how to use \a partname and \a makeifneeded, and what you can expect
  to get returned from this method.
*/
SoNode *
SoBaseKit::getPart(const SbName & partname, SbBool makeifneeded)
{
  return this->getAnyPart(partname, makeifneeded, TRUE, TRUE);
}

/*!
  Returns the full path name to a catalog part, given the part's
  current item pointer.
*/
SbString
SoBaseKit::getPartString(const SoBase * part)
{
  const SoNodekitCatalog * catalog = this->getNodekitCatalog();
  if (part->isOfType(SoNode::getClassTypeId())) {
    int idx = this->findNodeInThisKit((SoNode *)part);
    if (idx >= 0) {
      return SbString(catalog->getName(idx).getString());
    }
    return SbString();
  }
  else if (part->isOfType(SoPath::getClassTypeId())) {
    SoFullPath * path = (SoFullPath *)part;
    int pathidx = path->findNode(this);
    if (pathidx < 0) return SbString();
    SoBaseKit * kit = this;
    SbString partname;
    int parentnum = 0;
    SoNode * tail = path->getTail();
    SoNode * node = kit;
    while (node != tail) {
      node = path->getNode(++pathidx);
      int partnum = kit->findNodeInThisKit(node, parentnum);
      if (partnum < 0) {
#if COIN_DEBUG
        SoDebugError::postWarning("SoBaseKit::getPartString",
                                  "Illegal path");
#endif // COIN_DEBUG
        return SbString();
      }
      if (catalog->isLeaf(partnum)) {
        if (partname != "") partname += '.';
        partname += catalog->getName(partnum).getString();
      }
      if (node->isOfType(SoNodeKitListPart::getClassTypeId())) {
        // no sense in using SoNodeKitListPart as a non-leaf node, right?
        assert(catalog->isLeaf(partnum));
        SoNodeKitListPart * list = (SoNodeKitListPart *)node;
        pathidx += 2; // // skip container node
        if (pathidx >= path->getLength()) {
#if COIN_DEBUG
          SoDebugError::postWarning("SoBaseKit::getPartString",
                                    "Path too short");
#endif // COIN_DEBUG
          return SbString();
        }
        node = path->getNode(pathidx);
        int childidx = list->findChild(node);
        assert(childidx >= 0);
        partname += '[';
        partname.addIntString(childidx);
        partname += ']';
      }
      if (node->isOfType(SoBaseKit::getClassTypeId())) {
        kit = (SoBaseKit *) node;
        catalog = kit->getNodekitCatalog();
        parentnum = 0;
      }
      else {
        // search more in this kit
        parentnum = partnum;
      }
    }
    return partname;
  }
  return SbString();
}

/*!
  Calls SoBaseKit::createPathToAnyPart() with \a leafcheck \c TRUE,
  and \a publiccheck \c TRUE (and other arguments as given to this
  function).

  See SoBaseKit::createPathToAnyPart() for documentation.
*/
SoNodeKitPath *
SoBaseKit::createPathToPart(const SbName & partname, SbBool makeifneeded, const SoPath * pathtoextend)
{
  return this->createPathToAnyPart(partname, makeifneeded, TRUE, TRUE, pathtoextend);
}

/*!
  Sets the catalog part given by \a partname to the \a from node pointer.
*/
SbBool
SoBaseKit::setPart(const SbName & partname, SoNode * from)
{
  return this->setAnyPart(partname, from, FALSE);
}

static const char *
skip_spaces(const char * ptr)
{
  // ANSI C isspace() takes the current locale into account. Under
  // MSWindows, this can lead to "interesting" artifacts, like a case
  // with RR tracked down and fixed by <thammer@sim.no> where a
  // character (was it �?) with ASCII value > 127 made isspace()
  // return non-nil on a German system. So we're using our own
  // locale-independent isspace() implementation instead.
  while (coin_isspace(*ptr)) ptr++;
  return ptr;
}

static int
find_partname_length(const char * ptr)
{
  int cnt = 0;
  while (ptr[cnt] && !coin_isspace(ptr[cnt]) &&
         ptr[cnt] != '{' && ptr[cnt] != '}') {
    cnt++;
  }
  return cnt;
}

/*!
  Sets nodekit part field values. The input argument string is of the
  format:

  \code
  partname {
    fieldname fieldval
    fieldname fieldval
    [...]
  }
  partname {
    fieldname fieldval
    fieldname fieldval
    [...]
  }
  [...]
  \endcode

  (Whitespace layout is ignored, as always for Inventor format input
  strings.)

  Here's an example, changing several values of the camera part of an
  SoCameraKit instance:

  \code
  kit->set("camera { heightAngle 0.3927  nearDistance 1.1  farDistance 999.9 }");
  \endcode
*/
SbBool
SoBaseKit::set(const char * namevaluepairliststring)
{
  const size_t stringlen = strlen(namevaluepairliststring); // cache this value
  const char * currptr = skip_spaces(namevaluepairliststring);
  SoInput memInput;

  while (*currptr) {
    int partnamelen = find_partname_length(currptr);
    const char * start = skip_spaces(currptr + partnamelen);
    if (*start != '{') { // first non-space after partname should be a {
#if COIN_DEBUG
      SoDebugError::postWarning("SoBaseKit::set",
                                "parse error at byte %d in input string",
                                start-namevaluepairliststring);
#endif // COIN_DEBUG
      return FALSE;
    }
    start++; // skip {
    SbString partname(currptr, 0, partnamelen-1);
    SoBaseKit * kit = this;
    int partNum;
    SbBool isList;
    int listIdx;
    if (!SoBaseKit::findPart(partname, kit, partNum, isList, listIdx, TRUE, NULL, TRUE)) {
#if COIN_DEBUG
      SoDebugError::postWarning("SoBaseKit::set",
                                "part ``%s'' not found",
                                partname.getString());
#endif // COIN_DEBUG
      return FALSE;
    }

    SoNode * node = PRIVATE(kit)->instancelist[partNum]->getValue();
    PRIVATE(kit)->instancelist[partNum]->setDefault(FALSE);

    if (isList) {
      SoNodeKitListPart * list = (SoNodeKitListPart *)node;
      if (listIdx < 0 || listIdx > list->getNumChildren()) {
#if COIN_DEBUG
        SoDebugError::postWarning("SoBaseKit::set",
                                  "index %d out of bounds for part ``%s''",
                                  listIdx, partname.getString());
#endif // COIN_DEBUG
        return FALSE;
      }
      else if (listIdx == list->getNumChildren()) {
        if (!list->canCreateDefaultChild()) {
#if COIN_DEBUG
          SoDebugError::postWarning("SoBaseKit::set",
                                    "Unable to create default child for list-part ``%s''",
                                    partname.getString());
#endif // COIN_DEBUG
          return FALSE;
        }
        node = list->createAndAddDefaultChild();
      }
      else {
        node = list->getChild(listIdx);
      }
    }
    memInput.setBuffer((void *)start, stringlen - (start-namevaluepairliststring));
    SbBool dummy;
    if (!node->getFieldData()->read(&memInput, node, TRUE, dummy)) {
#if COIN_DEBUG
      SoDebugError::postWarning("SoBaseKit::set",
                                "error while parsing data for part ``%s''",
                                partname.getString());
#endif // COIN_DEBUG
      return FALSE;
    }
    currptr = start + (int) memInput.getNumBytesRead();
    if (*currptr == '}') currptr++;
    assert(currptr <= namevaluepairliststring + stringlen);
    currptr = skip_spaces(currptr);
  }
  return TRUE;
}

/*!
  This just overloads the other SoBaseKit::set() method, and provides
  a way to set a part value by using a separate input argument for the
  name of the part and the name of the field (i.e. parameter)
  settings.
*/
SbBool
SoBaseKit::set(const char * partnamestring, const char * parameterstring)
{
  SbString partname(partnamestring);
  int partNum;
  SbBool isList;
  int listIdx;
  SoBaseKit * kit = this;
  if (SoBaseKit::findPart(partname, kit, partNum, isList, listIdx, TRUE, NULL, TRUE)) {
    SoNode * node = PRIVATE(kit)->instancelist[partNum]->getValue();
    PRIVATE(kit)->instancelist[partNum]->setDefault(FALSE);
    assert(node != NULL); // makeifneeded was TRUE in findPart call
    if (isList) {
      assert(node->isOfType(SoNodeKitListPart::getClassTypeId()));
      SoNodeKitListPart * list = (SoNodeKitListPart *) node;
      if (listIdx < 0 || listIdx > list->getNumChildren()) {
#if COIN_DEBUG
        SoDebugError::postWarning("SoBaseKit::set",
                                  "index %d out of bounds for part ``%s''",
                                  listIdx, partnamestring);
#endif // COIN_DEBUG
        return FALSE;
      }
      else if (listIdx == list->getNumChildren()) {
        if (!list->canCreateDefaultChild()) {
#if COIN_DEBUG
          SoDebugError::postWarning("SoBaseKit::set",
                                    "Unable to create default child for list-part ``%s''",
                                    partname.getString());
#endif // COIN_DEBUG
          return FALSE;
        }
        node = list->createAndAddDefaultChild();
      }
      else {
        node = list->getChild(listIdx);
      }
    }
    if (node) {
      SoInput memInput;
      SbBool dummy;
      memInput.setBuffer((void *)parameterstring, strlen(parameterstring));
      const SoFieldData * fielddata = node->getFieldData();
      return fielddata->read(&memInput, node, TRUE, dummy);
    }
  }
  return FALSE;
}

// Doc in superclass.
void
SoBaseKit::doAction(SoAction * action)
{
  int numindices;
  const int * indices;
  if (action->getPathCode(numindices, indices) == SoAction::IN_PATH) {
    this->children->traverseInPath(action, numindices, indices);
  }
  else {
    this->children->traverse(action);
  }
}

// Doc in superclass.
void
SoBaseKit::callback(SoCallbackAction * action)
{
  SoBaseKit::doAction((SoAction *)action);
}

// Doc in superclass.
void
SoBaseKit::GLRender(SoGLRenderAction * action)
{
  SoBaseKit::doAction((SoAction *)action);
}

// Doc in superclass. Overriden to calculate bounding box center.
void
SoBaseKit::getBoundingBox(SoGetBoundingBoxAction * action)
{
  int numindices;
  const int * indices;
  int last = action->getPathCode(numindices, indices) == SoAction::IN_PATH ?
    indices[numindices-1] : this->children->getLength() - 1;

  SbVec3f acccenter(0.0f, 0.0f, 0.0f);
  int numacc = 0;

  for (int i = 0; i <= last; i++) {
    this->children->traverse(action, i, i);
    if (action->isCenterSet()) {
      acccenter += action->getCenter();
      numacc++;
      action->resetCenter();
    }
  }
  if (numacc) action->setCenter(acccenter / float(numacc), FALSE);
}

// Doc in superclass.
void
SoBaseKit::getMatrix(SoGetMatrixAction * action)
{
  // SoBaseKit should be travesed like a normal SoGroup node, and the
  // children should only be traversed if we're IN_PATH or OFF_PATH
  // (SoGetMatrixAction is only applied on a path or on a single node,
  // and we must not calculate when BELOW_PATH or NO_PATH).
  int numindices;
  const int * indices;
  switch (action->getPathCode(numindices, indices)) {
  case SoAction::IN_PATH:
    this->children->traverseInPath(action, numindices, indices);
    break;
  case SoAction::OFF_PATH:
    this->children->traverse(action);
    break;
  default:
    break;
  }
}

// Doc in superclass.
void
SoBaseKit::handleEvent(SoHandleEventAction * action)
{
  SoBaseKit::doAction((SoAction *)action);
}

// Doc in superclass.
void
SoBaseKit::rayPick(SoRayPickAction * action)
{
  SoBaseKit::doAction((SoAction *)action);

  const SoPickedPointList & pplist = action->getPickedPointList();
  const int n = pplist.getLength();
  for (int i = 0; i < n; i++) {
    SoPickedPoint * pp = pplist[i];
    SoFullPath * path = (SoFullPath*) pp->getPath();
    if (path->containsNode(this) && pp->getDetail(this) == NULL) {
      PRIVATE(this)->addKitDetail(path, pp);
    }
  }
}

// Doc in superclass.
void
SoBaseKit::search(SoSearchAction * action)
{
  inherited::search(action);
  if (action->isFound() || !SoBaseKit::searchchildren) return;
  SoBaseKit::doAction((SoAction *)action);
}

// Test if node has all fields set to default and if the fields
// contains the default values. If so, we don't need to write it.
static SbBool
is_default_node(SoNode * node)
{
  SoNode * definstance = NULL;
  const SoFieldData * fielddata = node->getFieldData();
  int i, n = fielddata->getNumFields();
  for (i = 0; i < n; i++) {
    SoField * field = fielddata->getField(node, i);
    if (field->isConnectionEnabled() && field->isConnected()) break;
    if (definstance == NULL) {
      definstance = (SoNode *)node->getTypeId().createInstance();
      definstance->ref();
    }
    if (!field->isDefault() &&
        !field->isSame(*fielddata->getField(definstance, i))) break;
  }
  if (definstance) definstance->unref();
  // if all fields were tested, it is a default node.
  return i == n;
}

// Doc in superclass.
void
SoBaseKit::write(SoWriteAction * action)
{
  // debugging code start **************************************************

  // If the below envvar is set, we'll write nodekit's current scene
  // graph instead of writing as a nodekit.
  //
  // Note that if the nodekit is a dragger, the resulting scene graph
  // export may still not look exactly the same as when the exported
  // sub-graph is contained within the dragger, as
  // SoDragger::GLRender() sets a number of elements in the traversal
  // state to non-intrusive "default" values before rendering the
  // dragger geometry. These settings will not be part of the exported
  // iv-file.
  static int dump = -1;
  if (dump == -1) {
    const char * env = coin_getenv("COIN_DEBUG_FLATTEN_NODEKITS_ON_WRITE");
    dump = env && (atoi(env) > 0);
  }
  if (dump) {
    this->children->traverse(action);
    return;
  }

  // debugging code end ****************************************************


  SoOutput * out = action->getOutput();
  if (out->getStage() == SoOutput::COUNT_REFS) {
    this->addWriteReference(out, FALSE);
  }
  else if (out->getStage() == SoOutput::WRITE) {
    if (this->writeHeader(out, FALSE, FALSE)) return; // no more to write
    // FIXME: shouldn't this if() rather be an assert? 20030523 mortene.
    if (PRIVATE(this)->writedata) {
      PRIVATE(this)->writedata->write(out, this);
      // we don't need it any more
      delete PRIVATE(this)->writedata;
      PRIVATE(this)->writedata = NULL;
    }
    this->writeFooter(out);
  }
  else assert(0 && "unknown stage");
}

// documented in superclass
void
SoBaseKit::addWriteReference(SoOutput * out, SbBool isfromfield)
{
  // don't call inherited::addWriteReference(), as we will handle
  // the fields ourselves, using a new fielddata. This is needed to
  // write fields in the correct order.
  SoBase::addWriteReference(out, isfromfield);

  // If first invocation during the reference counting pass, check
  // nodes in our catalog.
  if (!isfromfield && !SoWriterefCounter::instance(out)->hasMultipleWriteRefs(this)) {
    this->countMyFields(out);
  }
}

/*!
  Reference count the write connections to nodes in the catalog.
*/
void
SoBaseKit::countMyFields(SoOutput * out)
{
  assert(out->getStage() == SoOutput::COUNT_REFS);

  // already created?
  //
  // FIXME: could this ever be TRUE without that being an error
  // situation? I have a feeling this should rather be an
  // assert(). Investigate. 20030523 mortene.
  if (PRIVATE(this)->writedata) return;

  // Initialize isDefault() flag on fields that should not be
  // written. This is a virtual method.
  this->setDefaultOnNonWritingFields();

  const SoNodekitCatalog * catalog = this->getNodekitCatalog();

  // PRIVATE(this)->writedata contains a sorted list of fields.
  //
  // FIXME: the pimpl->writedata scheme doesn't look multithread-safe
  // wrt multiple SoWriteAction instances working in parallel over the
  // same scene. 20030521 mortene.
  PRIVATE(this)->createWriteData();

  // test if parent of parts is writing. Then we must write part anyway.
  PRIVATE(this)->testParentWrite();

  // we might count fields that won't be written here, but it
  // doesn't matter, since we're operating on a copy of the fields.

  int i, n = PRIVATE(this)->writedata->getNumFields();
  for (i = 0; i < n; i++) {
    const SbName name = PRIVATE(this)->writedata->getFieldName(i);
    SoField * field = PRIVATE(this)->writedata->getField(this, i);
    int partnum = catalog->getPartNumber(name);
    if (partnum < 0) {
      // field is not a part. Do normal field write.
      if (field->shouldWrite()) {
        field->write(out, name);
      }
    }
    else {
      if (!field->isDefault()) field->write(out, name);
      else {
        SoNode * node = (SoNode*) ((SoSFNode*)field)->getValue();
        if (node) {
          if (node->isOfType(SoBaseKit::getClassTypeId())) {
            SoBaseKit * kit = (SoBaseKit*) node;
            kit->countMyFields(out);
            if (kit->forceChildDrivenWriteRefs(out)) {
              field->setDefault(FALSE);
              // add a write reference on the kit node only. We supply
              // isfromfield TRUE to achieve this
              kit->addWriteReference(out, TRUE);
            }
          }
        }
      }
    }
  }
}

// Note: the following documentation for
// setDefaultOnNonWritingFields() will also be used for nodekit and
// dragger subclasses, so keep it general.
/*!
  (Be aware that this method is unlikely to be of interest to the
  application programmer who does not want to extend the library with
  new custom nodekits or draggers.  If you indeed \e are writing
  extensions, see the information in the SoBaseKit class
  documentation.)

  This is a virtual method, and the code in it should call
  SoField::setDefault() with argument \c TRUE on part fields that
  should not be written upon scenegraph export operations.

  This is typically done when:

  <OL>

  <LI> field value is \c NULL and part is \c NULL by default </LI>

  <LI> it is a leaf SoGroup or SoSeparator node with no children </LI>

  <LI> it is a leaf listpart with no children and an SoGroup or
  SoSeparator container </LI>

  <LI> it is a non-leaf part and it's of SoGroup type and all fields
  are at their default values </LI>

  </OL>

  Subclasses should usually override this to do additional settings
  for new member fields.  From the subclass, do remember to call
  "upwards" to your superclass' setDefaultOnNonWritingFields() method.
*/
void
SoBaseKit::setDefaultOnNonWritingFields(void)
{
  const SoNodekitCatalog * catalog = this->getNodekitCatalog();
  int n = PRIVATE(this)->instancelist.getLength();
  for (int i = 1; i < n; i++) {
    SoSFNode * field = PRIVATE(this)->instancelist[i];
    if (field->isDefault()) { continue; }

    SoNode * node = field->getValue();

    if (node == NULL) {
      // first test listed in API doc above
      if (catalog->isNullByDefault(i)) { field->setDefault(TRUE); }
      continue;
    }

    const SbBool leaf = catalog->isLeaf(i);
    const SoType type = node->getTypeId();

    if (leaf) {
      // second test
      if ((type == SoGroup::getClassTypeId() ||
           type == SoSeparator::getClassTypeId()) &&
          ((SoGroup*)node)->getNumChildren() == 0) {
        field->setDefault(TRUE);
      }
      // third test
      else if (type == SoNodeKitListPart::getClassTypeId()) {
        SoNodeKitListPart * list = (SoNodeKitListPart*) node;
        const SoNode * container = list->getContainerNode();
        if (list->getNumChildren() == 0 && container &&
            (container->getTypeId() == SoSeparator::getClassTypeId() ||
             container->getTypeId() == SoGroup::getClassTypeId())) {
          field->setDefault(TRUE);
        }
      }
    }
    else { // not leaf
      // fourth test
      if (node->isOfType(SoGroup::getClassTypeId()) && is_default_node(node)) {
        field->setDefault(TRUE);
      }
    }
  }
}

/*!
  Returns \c TRUE if kit should write. This happens if shouldWrite()
  returns \c TRUE, or if any of the children (recursively) should
  write.
*/
SbBool
SoBaseKit::forceChildDrivenWriteRefs(SoOutput * out)
{
  if (SoWriterefCounter::instance(out)->shouldWrite(this)) return TRUE;

  // if NULL we already did this test, found that we shouldn't write,
  // deleted writedata and set writedata to NULL.
  if (!PRIVATE(this)->writedata) return FALSE;

  const SoNodekitCatalog * catalog = this->getNodekitCatalog();
  int i, n = PRIVATE(this)->writedata->getNumFields();

  // loop through fields and break as soon as we find a reason
  // to write
  for (i = 0; i < n; i++) {
    SoField * field = PRIVATE(this)->writedata->getField(this, i);
    int partnum = catalog->getPartNumber(PRIVATE(this)->writedata->getFieldName(i));
    if (!field->isDefault()) break;
    else if (partnum < 0 && field->isIgnored()) break;
    else if (partnum > 0) {
      SoSFNode * part = (SoSFNode*) field;
      SoNode * node = part->getValue();
      if (node) {
        if (SoWriterefCounter::instance(out)->shouldWrite(node)) break;
        else if (node->isOfType(SoBaseKit::getClassTypeId())) {
          SoBaseKit * kit = (SoBaseKit*) node;
          // recurse
          if (kit->forceChildDrivenWriteRefs(out)) break;
        }
      }
    }
  }

  if (i < n) { // did we find a reason to write?
    SoBase::addWriteReference(out);
    return TRUE;
  }
  else {
    delete PRIVATE(this)->writedata;
    PRIVATE(this)->writedata = NULL;
    return FALSE;
  }
}


// Documented in superclass.
void
SoBaseKit::getPrimitiveCount(SoGetPrimitiveCountAction * action)
{
  SoBaseKit::doAction((SoAction *)action);
}

// Documented in superclass.
SoChildList *
SoBaseKit::getChildren(void) const
{
  return this->children;
}

/*!
  Print out the full nodekit catalog structure.  Just invokes
  SoBaseKit::printSubDiagram() on the catalog root. Useful for
  debugging.

  Example output:

  \verbatim
  CLASS SoWrapperKit
  -->"this"
        "callbackList"
        "topSeparator"
           "pickStyle"
           "appearance"
           "units"
           "transform"
           "texture2Transform"
           "childList"
  -->      "localTransform"
  -->      "contents"
  \endverbatim

  The arrows denote new entries in the catalog for the particular
  class versus it's superclass. (Apart from the root entry, of
  course.)

  For a more detailed catalog dump, see SoBaseKit::printTable().
*/
void
SoBaseKit::printDiagram(void)
{
  fprintf(stdout, "CLASS So%s\n", this->getTypeId().getName().getString());
  this->printSubDiagram("this", 0);
}

/*!
  Print out the nodekit catalog structure from \a rootname and
  downwards in the catalog tree, with indentation starting at \a
  level.

  \sa printDiagram()
*/
void
SoBaseKit::printSubDiagram(const SbName & rootname, int level)
{
  const SoNodekitCatalog * parentcatalog = NULL;
  if (this->getTypeId() != SoBaseKit::getClassTypeId()) {
    SoType parenttype = this->getTypeId().getParent();
    SoBaseKit * parentobj = (SoBaseKit *)parenttype.createInstance();
    parentcatalog = parentobj->getNodekitCatalog();
    parentobj->ref();
    parentobj->unref();
  }

  const SoNodekitCatalog * thiscat = this->getNodekitCatalog();

  int i = 0;
  if (!parentcatalog ||
      parentcatalog->getPartNumber(rootname) == SO_CATALOG_NAME_NOT_FOUND ||
      parentcatalog->getType(rootname) != thiscat->getType(rootname)) {
    fprintf(stdout, "-->");
    i++;
  }
  for (; i < level+1; i++) fprintf(stdout, "   ");

  fprintf(stdout, "\"%s\"\n", rootname.getString());

  for (int j=0; j < thiscat->getNumEntries(); j++) {
    if (thiscat->getParentName(j) == rootname)
      this->printSubDiagram(thiscat->getName(j), level + 1);
  }
}

/*!
  Write the complete nodekit catalog in table form.

  Example output:

  \verbatim
  CLASS SoWrapperKit
  PVT   "this",  SoWrapperKit  ---
        "callbackList",  SoNodeKitListPart [ SoCallback, SoEventCallback ]
  PVT   "topSeparator",  SoSeparator  ---
        "pickStyle",  SoPickStyle  ---
        "appearance",  SoAppearanceKit  ---
        "units",  SoUnits  ---
        "transform",  SoTransform  ---
        "texture2Transform",  SoTexture2Transform  ---
        "childList",  SoNodeKitListPart [ SoShapeKit, SoSeparatorKit ]
        "localTransform",  SoTransform  ---
        "contents",  SoSeparator  ---
  \endverbatim

  \c PVT denotes that it's a private entry in the catalog, then
  follows the part name and the part type. If the part is a list, the
  allowed node types for the list is given in square brackets, and if
  not there's a triple hyphen. If the part type is abstract, the
  default part type will be listed last (not shown in the example
  output above).
*/
void
SoBaseKit::printTable(void)
{
  fprintf(stdout, "CLASS So%s\n", this->getTypeId().getName().getString());

  const SoNodekitCatalog * thiscat = this->getNodekitCatalog();
  for (int i=0; i < thiscat->getNumEntries(); i++) {
    const SoType t = thiscat->getType(i);
    fprintf(stdout, "%s   \"%s\",  So%s ",
            thiscat->isPublic(i) ? "   " : "PVT",
            thiscat->getName(i).getString(),
            t.getName().getString());
    if (thiscat->isList(i)) {
      SoTypeList tlist = thiscat->getListItemTypes(i);
      fprintf(stdout, "[ ");
      for (int j=0; j < tlist.getLength(); j++) {
        if (j) fprintf(stdout, ", ");
        fprintf(stdout, "So%s", tlist[j].getName().getString());
      }
      fprintf(stdout, " ] ");
    }
    else {
      fprintf(stdout, " --- ");
    }

    if (t != thiscat->getDefaultType(i)) {
      fprintf(stdout, ", (default type = So%s)",
              thiscat->getDefaultType(i).getName().getString());
    }
    fprintf(stdout, "\n");
  }
}

/*!
  Returns the value of the flag indicating whether or not the kit
  parts are searched during SoSearchAction traversal.

  \sa SoBaseKit::setSearchingChildren()
*/
SbBool
SoBaseKit::isSearchingChildren(void)
{
  return SoBaseKit::searchchildren;
}

/*!
  Set whether or not the kit parts should be searched during
  SoSearchAction traversal. The default value is \c FALSE.
*/
void
SoBaseKit::setSearchingChildren(const SbBool newval)
{
  SoBaseKit::searchchildren = newval;
}

// Documented in superclass. Overridden to also recurse on non-null
// part nodes.
SoNode *
SoBaseKit::addToCopyDict(void) const
{
  SoNode * cp = (SoNode*) SoFieldContainer::checkCopy(this);
  if (cp == NULL) { // not copied?
    cp = (SoNode*) this->getTypeId().createInstance();
    assert(cp);
    cp->ref();
    SoFieldContainer::addCopy(this, cp);
    cp->unrefNoDelete();

    int n = PRIVATE(this)->instancelist.getLength();
    for (int i = 1; i < n; i++) {
      SoNode * node = PRIVATE(this)->instancelist[i]->getValue();
      if (node != NULL) node->addToCopyDict();
    }
  }
  return cp;
}

// (Doc in superclass.) Overridden to copy parts correctly.
void
SoBaseKit::copyContents(const SoFieldContainer * fromfc,
                        SbBool copyconnections)
{
  int i;

  // disable connections while copying
  SbBool oldsetup = this->setUpConnections(FALSE);

  // do normal node copy
  inherited::copyContents(fromfc, copyconnections);

  const SoBaseKit * srckit = (const SoBaseKit*) fromfc;

  // convenient reference
  /*const SbList <SoSFNode*> & srcfields =*/ srckit->getCatalogInstances();

  const int n = PRIVATE(this)->instancelist.getLength();

  // use temporary lists to store part node pointers and field
  // default flag, as we will modify the originals.
  SbList <SoNode *> partlist;
  SbList <SbBool> flaglist;

  // part 0 is this
  partlist.append(NULL);
  flaglist.append(FALSE);

  // initialize temporary lists
  for (i = 1; i < n; i++) {
    partlist.append(NULL);
    flaglist.append(PRIVATE(this)->instancelist[i]->isDefault());
  }

  // copy parts, taking care of scene graph
  PRIVATE(this)->copyParts(srckit, partlist, copyconnections);

  // remove all old children before copying parts
  this->getChildren()->truncate(0);

  // reset part fields
  for (i = 1; i < n; i++) {
    PRIVATE(this)->instancelist[i]->setValue(NULL);
    PRIVATE(this)->instancelist[i]->setDefault(TRUE);
  }

  // set non-leaf nodes first
  PRIVATE(this)->setParts(partlist, FALSE);

  // then leaf nodes
  PRIVATE(this)->setParts(partlist, TRUE);

  // do final pass
  for (i = 1; i < n; i++) {
    // restore default flag for fields
    PRIVATE(this)->instancelist[i]->setDefault(flaglist[i]);

    // unref nodes in temporary list as they were ref'ed
    // when inserted
    if (partlist[i]) partlist[i]->unref();
  }

  // enable connections
  if (oldsetup) this->setUpConnections(TRUE);
}

/*!
  Returns a pointer to the group node above an SoNodeKitListPart in
  the catalog given by \a listname.

  If the list part (and its container) was not yet constructed, they
  will be so if \a makeifneeded is \c TRUE (otherwise, \c NULL will be
  returned).
*/
SoGroup *
SoBaseKit::getContainerNode(const SbName & listname, SbBool makeifneeded)
{
  SoBaseKit * kit = this;
  int partNum;
  SbBool isList;
  int listIdx;
  if (SoBaseKit::findPart(SbString(listname.getString()), kit, partNum,
                          isList, listIdx, makeifneeded, NULL, TRUE)) {
    SoNode * node = PRIVATE(kit)->instancelist[partNum]->getValue();
    if (node == NULL) return NULL;
    assert(node->isOfType(SoNodeKitListPart::getClassTypeId()));
    SoNodeKitListPart * list = (SoNodeKitListPart *)node;
    return list->getContainerNode();
  }
  return NULL;
}

/*!
  Returns catalog part of the given \a partname.

  If the \a partname part is not in the nodekit's catalog, return \c
  NULL.

  If the part is specified in the catalog, but has not yet been made,
  the function will either construct the part (if \a makeifneeded is
  \c TRUE) or just return \c NULL (if \a makeifneeded is \c FALSE).

  If \a leafcheck is \c TRUE, a pointer to the part will only be
  returned if it's a leaf in the catalog (otherwise \c NULL is
  returned).

  If \a publiccheck is \c TRUE, a pointer to the part will only be
  returned if it's a public catalog part (otherwise \c NULL is
  returned).


  The \a partname input argument should be given as a \e "path" of
  catalog part names down to the wanted leaf part. The syntax for
  specifiying \a partname "paths" is as follows (given in Backus-Naur
  Form (BNF)):

  \verbatim
  BNF:

  partname = singlename | compoundname
  compoundname = singlename | compoundname.singlename
  singlename = singlepartname | singlelistelementname
  singlelistelementname = singlelistname[idx]

  singlepartname is name of a part ("ordinary", nodekit or list)
  singlelistname is name of a part which is a list
  idx is an integer value
  \endverbatim
*/
SoNode *
SoBaseKit::getAnyPart(const SbName & partname, SbBool makeifneeded,
                      SbBool leafcheck, SbBool publiccheck)
{

  SoBaseKit * kit = this;
  int partNum;
  SbBool isList;
  int listIdx;

  SbString partstring(partname.getString());

  if (SoBaseKit::findPart(partstring, kit, partNum, isList, listIdx,
                          makeifneeded, NULL, TRUE)) {

    if (publiccheck && !kit->getNodekitCatalog()->isPublic(partNum)) {
      SoDebugError::postWarning("SoBaseKit::getAnyPart",
                                "Part ``%s'' found in %s, but access is private.",
                                partname.getString(),
                                this->getTypeId().getName().getString());
      return NULL;
    }

    if (!leafcheck || kit->getNodekitCatalog()->isLeaf(partNum)) {
      if (isList) {
        SoNode * partnode = PRIVATE(kit)->instancelist[partNum]->getValue();
        if (partnode == NULL) return NULL;
        assert(partnode->isOfType(SoNodeKitListPart::getClassTypeId()));
        SoNodeKitListPart * list = (SoNodeKitListPart *) partnode;
        if (listIdx >= 0 && listIdx < list->getNumChildren()) {
          return list->getChild(listIdx);
        }
        else if (makeifneeded && (listIdx == list->getNumChildren())) {
          if (!list->canCreateDefaultChild()) {
#if COIN_DEBUG
            SoDebugError::postWarning("SoBaseKit::getAnyPart",
                                      "Unable to create default child for list-part ``%s''",
                                      partname.getString());
#endif // COIN_DEBUG
          }
          return list->createAndAddDefaultChild();
        }
        else {
#if COIN_DEBUG
          SoDebugError::postWarning("SoBaseKit::getAnyPart",
                                    "index %d out of bounds for part ``%s''",
                                    listIdx, partname.getString());
#endif // COIN_DEBUG
        }
      }
      else {
        return PRIVATE(kit)->instancelist[partNum]->getValue();
      }
    }
  }

  // FIXME:
  // run cleanup?, in case some node has been temporarily created while
  // searching for the part?? pederb, 2000-01-05

#if COIN_DEBUG
  if (makeifneeded) { // user probably expected part to be found, post a warning
    SoDebugError::postWarning("SoBaseKit::getAnyPart",
                              "part ``%s'' not found in %s",
                              partname.getString(),
                              this->getTypeId().getName().getString());
  }
#endif // COIN_DEBUG
  return NULL;
}

/*!
  Return path with nested SoNodeKit instances down in the catalog
  hierarchy given by \a partname.

  If the trailing part has not been made and \a makeifneeded is \c
  TRUE, make an instance of the part type and insert into the catalog,
  as done in setAnyPart().

  If \a leafcheck is \c TRUE, ignore non-leaf catalog node entries. If
  \a publiccheck is \c TRUE, ignore private catalog entries.

  \a pathtoextend is a path through the nodekit instance catalog
  hierarchy, where we should pick up and continue to create the path
  from where \a pathtoextend terminates. If \a pathtoextend is \c
  NULL, we simply start at the "this" toplevel node.

  Returns \c NULL on failure, for any of the possible reasons
  described above (part ends in non-leaf or private catalog entry,
  part is not syntactically valid or refers to non-existing catalog
  entries).
*/
SoNodeKitPath *
SoBaseKit::createPathToAnyPart(const SbName & partname, SbBool makeifneeded,
                               SbBool leafcheck, SbBool publiccheck,
                               const SoPath * pathtoextend)
{
  SoFullPath * path;
  if (pathtoextend) {
    path = (SoFullPath *)pathtoextend->copy();
    // pop off nodes beyond this kit node
    if (path->containsNode(this)) while (path->getTail() != this && path->getLength()) path->pop();
    else if (path->getLength()) {
      SoNode * node = path->getTail();
      if (!node->getChildren() || node->getChildren()->find(this) < 0) {
#if COIN_DEBUG
        SoDebugError::postWarning("SoBaseKit::createPathToAnyPart",
                                  "pathtoextend is illegal");
#endif // COIN_DEBUG
        return NULL;
      }
      path->append(this); // this should be safe now
    }
  }
  else {
    path = (SoFullPath *)new SoPath(this);
  }
  path->ref();

  SoBaseKit * kit = this;
  int partNum;
  SbBool isList;
  int listIdx;

  if (SoBaseKit::findPart(SbString(partname.getString()), kit, partNum,
                          isList, listIdx, makeifneeded, path)) {
    const SoNodekitCatalog * catalog = kit->getNodekitCatalog();
    if ((leafcheck && ! catalog->isLeaf(partNum)) ||
        (publiccheck && ! catalog->isPublic(partNum))) {
      path->unref();
      return NULL;
    }

    SoNode * node = PRIVATE(kit)->instancelist[partNum]->getValue();
    if (node) {
      path->append(node);
      if (isList) {
        SoNodeKitListPart * list = (SoNodeKitListPart *)node;
        int numlistchildren = list->getNumChildren();
        if (listIdx < 0 || listIdx > numlistchildren || (!makeifneeded && listIdx == numlistchildren)) {
#if COIN_DEBUG
          SoDebugError::postWarning("SoBaseKit::createPathToAnyPart",
                                    "index %d out of bounds for part ``%s''",
                                    listIdx, partname.getString());
#endif // COIN_DEBUG
          path->unref();
          return NULL;
        }
        else if (listIdx == numlistchildren) {
          if (!list->canCreateDefaultChild()) {
#if COIN_DEBUG
            SoDebugError::postWarning("SoBaseKit::createPathToAnyPart",
                                      "Unable to create default child for list-part ``%s''",
                                      partname.getString());
#endif //COIN_DEBUG

          }
          else {
            SoNode * newnode = list->createAndAddDefaultChild();
            path->append(list->getContainerNode());
            path->append(newnode);
          }
        }
        else {
          path->append(list->getContainerNode());
          path->append(list->getChild(listIdx));
        }
      }
      path->unrefNoDelete();
      return (SoNodeKitPath *)path;
    }
  }
  path->unref();
  return NULL;
}

/*!
  \COININTERNAL
*/
SbBool
SoBaseKit::setAnyPart(const SbName & partname, SoNode * from, SbBool anypart)
{
  SoBaseKit * kit = this;
  int partNum;
  SbBool isList;
  int listIdx;

  SbString partstring(partname.getString());

  // FIXME: findPart() really needs another parameter, since we need
  // to create intermediate parts, but not the leaf part. For now we
  // just supply makeifneeded = TRUE, and then immediately overwrite
  // the part here. pederb, 2004-06-07
  if (SoBaseKit::findPart(partstring, kit, partNum, isList, listIdx, TRUE, NULL, TRUE)) {
    if (anypart || kit->getNodekitCatalog()->isPublic(partNum)) {
      if (isList) {
        SoNode * partnode = PRIVATE(kit)->instancelist[partNum]->getValue();
        if (partnode) {
          assert(partnode->isOfType(SoNodeKitListPart::getClassTypeId()));
          SoNodeKitListPart * list = (SoNodeKitListPart *) partnode;
          if (listIdx >= 0 && listIdx <= list->getNumChildren()) {
            if (listIdx == list->getNumChildren())
              list->addChild(from);
            else
              list->replaceChild(listIdx, from);
            return TRUE;
          }
          else {
#if COIN_DEBUG
            SoDebugError::postWarning("SoBaseKit::setAnyPart",
                                      "index %d out of bounds for part ``%s''",
                                      listIdx, partname.getString());
#endif // COIN_DEBUG
          }
        }
      }
      else {
        return kit->setPart(partNum, from);
      }
    }
    else {
#if COIN_DEBUG
      SoDebugError::postWarning("SoBaseKit::setAnyPart",
                                "attempted to set non-public part ``%s''",
                                partname.getString());
#endif // COIN_DEBUG
    }
  }
#if COIN_DEBUG
  else {
    SoDebugError::postWarning("SoBaseKit::setAnyPart",
                              "part '%s' not found in %s",
                              partname.getString(),
                              this->getTypeId().getName().getString());
  }
#endif // COIN_DEBUG

  // FIXME:
  // run cleanup, in case some node has been temporarily created while
  // searching for the part?? pederb, 2000-01-05
  return FALSE;
}

/*!
  Not part of the Coin API.

  It is supposed to create the SoNodekitParts class instance. Since
  this class can only be used by SoBaseKit (all members are private,
  with SoBaseKit as friend), we decided to not support this class, and
  solve the problem of recording which parts are created in an
  alternative way.
*/
void
SoBaseKit::createNodekitPartsList(void)
{
  assert(0 &&
         "SoBaseKit::createNodekitPartsList() should not be used with Coin");
}

/*!
  Replaces the createNodekitPartsList() method.

  Sets up the list of SoSFNode fields with node pointers to the
  instances in our catalog.
*/
void
SoBaseKit::createFieldList(void)
{
  // FIXME:
  // is there any way to make sure this code is only run once, and in
  // the top level constructor. pederb, 2000-01-06
  //
  const SoNodekitCatalog * catalog = this->getNodekitCatalog();
  // only do this if the catalog has been created
  if (catalog) {
    PRIVATE(this)->instancelist.truncate(0);
    PRIVATE(this)->instancelist.append(NULL); // first catalog entry is "this"
    for (int i = 1; i < catalog->getNumEntries(); i++) {
      PRIVATE(this)->instancelist.append((SoSFNode *)this->getField(catalog->getName(i)));
      assert(PRIVATE(this)->instancelist[i] != NULL);
    }
  }
}

/*!
  \COININTERNAL
*/
void
SoBaseKit::createDefaultParts(void)
{
  // FIXME:
  // is there any way to make sure this code is only run once, and in
  // the top level constructor. pederb, 2000-01-06
  //
  const SoNodekitCatalog * catalog = this->getNodekitCatalog();
  // only do this if the catalog has been created
  if (catalog) {
    for (int i = 1; i < PRIVATE(this)->instancelist.getLength(); i++) {
      if (!catalog->isNullByDefault(i)) {
        SoNode * old = PRIVATE(this)->instancelist[i]->getValue();
        if ((old == NULL || ! old->isOfType(catalog->getDefaultType(i)) )) {
          this->makePart(i);
          PRIVATE(this)->instancelist[i]->setDefault(TRUE);
        }
      }
    }
  }
}

/*!
  In Open Inventor, this method returns a pointer to a private class.
  It will always return \c NULL in Coin.

  \sa createNodekitPartsList()
*/
const SoNodekitParts *
SoBaseKit::getNodekitPartsList(void) const
{
  assert(0 &&
         "SoBaseKit::getNodekitPartsList() obsoleted in Coin");
  return NULL;
}

/*!
  \COININTERNAL
*/
const SbList<SoSFNode*> &
SoBaseKit::getCatalogInstances(void) const
{
//    return this->fieldList;
  return PRIVATE(this)->instancelist;
}

/*!
  Obsoleted from the API in Coin.
*/
void
SoBaseKit::catalogError(void)
{
  COIN_OBSOLETED();
}

// Note: the following documentation for setUpConnections() will also
// be visible for subclass nodekits and draggers, so keep it general.
/*!
  Sets up all internal connections for instances of this class.

  (This method will usually not be of interest to the application
  programmer, unless you want to extend the library with new custom
  nodekits or dragger classes.  If so, see the SoBaseKit class
  documentation.)
*/
SbBool
SoBaseKit::setUpConnections(SbBool COIN_UNUSED_ARG(onoff), SbBool COIN_UNUSED_ARG(doitalways))
{
  return this->connectionsSetUp;
}

// doc in super
SbBool
SoBaseKit::readInstance(SoInput * in, unsigned short COIN_UNUSED_ARG(flags))
{
  int i;

  SbBool oldnotify = this->enableNotify(FALSE);
  SbBool oldsetup = this->setUpConnections(FALSE);

  // store old part values to find which parts are read
  SoNodeList nodelist;
  SbList <SbBool> defaultlist;

  const SoNodekitCatalog * cat = this->getNodekitCatalog();

  // Dummy first element to get indices to match instancelist (where
  // the dummy "this" catalog entry is first).
  nodelist.append(NULL);
  defaultlist.append(FALSE);

  // copy all parts into nodelist, and then set all parts to NULL
  // and default before reading
  for (i = 1; i < PRIVATE(this)->instancelist.getLength(); i++) {
    nodelist.append(PRIVATE(this)->instancelist[i]->getValue());
    defaultlist.append(PRIVATE(this)->instancelist[i]->isDefault());
    PRIVATE(this)->instancelist[i]->setValue(NULL);
    PRIVATE(this)->instancelist[i]->setDefault(TRUE);
  }

  // reset the node kit by removing all children. We will restore it
  // by setting the parts again later
  this->getChildren()->truncate(0);

  // actually read the nodekit.
  // Use readUnknownFields instead to read fields not part of catalog
  // SbBool ret = inherited::readInstance(in, flags);

  // Fields that's not part of catalog is read as a SoSFNode, and stored
  // in unknownfielddata. Later they'll be put in nodekit using setAnyPart.
  SbBool ret = TRUE;
  SoFieldData * unknownfielddata = new SoFieldData;
  if (!PRIVATE(this)->readUnknownFields(in, unknownfielddata))
    ret = FALSE;

  if (ret) {
    // loop through fields and copy the read parts into nodelist
    for (i = 1; i < PRIVATE(this)->instancelist.getLength(); i++) {
      if (!PRIVATE(this)->instancelist[i]->isDefault()) { // we've read a part
        nodelist.set(i, PRIVATE(this)->instancelist[i]->getValue());
        defaultlist[i] = FALSE;
        // set to NULL again so that setPart() will not get confused
        PRIVATE(this)->instancelist[i]->setValue(NULL);
      }
    }

    // restore the nodekit with all old and read parts
    for (i = 1; i < PRIVATE(this)->instancelist.getLength(); i++) {
      if (!cat->isLeaf(i) && nodelist[i]) {
        // if not leaf, remove all children. They will be re-added
        // later when the children parts are set.
        assert(nodelist[i]->isOfType(SoGroup::getClassTypeId()));
        SoGroup * g = (SoGroup*) nodelist[i];
        g->removeAllChildren();
      }
      this->setPart(i, nodelist[i]);
      PRIVATE(this)->instancelist[i]->setDefault(defaultlist[i]);
    }

    // put the unknown fields into nodekit using setAnyPart
    SbName partname;
    SoNode * pnode;
    SoSFNode * pfield;
    for (i = 0; i < unknownfielddata->getNumFields(); i++) {
      partname = unknownfielddata->getFieldName(i);
      pfield = (SoSFNode *) unknownfielddata->getField(this, i);
      pnode = pfield->getValue();
      this->setAnyPart(partname, pnode);
    }
  }

  delete unknownfielddata;

  (void) this->setUpConnections(oldsetup);
  (void) this->enableNotify(oldnotify);

  return ret;
}

//
// recurse until not possible to split string any more, and return information
// about part and the kit the part is found in.
// Remember to set kit=this before calling this method, also remember that
// kit might change during this search.
//
// compoundname parts are created during this search, so it might be necessary
// to do a nodekit cleanup if part is not public, or if part is set to NULL.
//
//
// if path != NULL, kit-nodes will be appended to the path during the search
// The actual part is not added to the path. The head of the path should
// be set to the kit-node performing the search.
//
SbBool
SoBaseKit::findPart(const SbString & partname, SoBaseKit *& kit, int & partnum,
                    SbBool & islist, int & listidx, const SbBool makeifneeded,
                    SoPath * path, const SbBool recsearch)
{
  // BNF:
  //
  // partname = singlename | compoundname
  // compoundname = singlename | compoundname.singlename
  // singlename = singlepartname | singlelistelementname
  // singlelistelementname = singlelistname[idx]
  //
  // singlepartname is name of a part ("ordinary", nodekit or list)
  // singlelistname is name of a part which is a list
  // idx is an integer value

  if (partname == "this") {
    islist = FALSE;
    partnum = 0;
    return TRUE;
  }

  const char * stringptr = partname.getString();
  const char * periodptr = strchr(stringptr, '.'); // find first period
  const char * startbracket = strchr(stringptr, '[');

  if (periodptr && (startbracket > periodptr))
    startbracket = NULL; // will handle later

  islist = FALSE; // set to FALSE first
  SbString firstpartname;
  if (startbracket) { // get index
    long int listindex = strtol(startbracket+1, NULL, 10);
    if (listindex == LONG_MIN || listindex == LONG_MAX) {
#if COIN_DEBUG
      SoDebugError::postWarning("SoBaseKit::findPart",
                                "list index not properly specified");
#endif // COIN_DEBUG
      return FALSE;
    }
    const ptrdiff_t endidx = startbracket - stringptr - 1;
    firstpartname = partname.getSubString(0, (int)endidx);
    listidx = (int) listindex;
    islist = TRUE;
  }
  else if (periodptr) {
    const ptrdiff_t endidx = periodptr - stringptr - 1;
    firstpartname = partname.getSubString(0, (int)endidx);
  }
  else firstpartname = partname;

  partnum = kit->getNodekitCatalog()->getPartNumber(firstpartname);
  if (partnum == SO_CATALOG_NAME_NOT_FOUND) {
    if (recsearch) { // search leaf nodekits for this part?
      SoBaseKit * orgkit = kit;
      assert(path == NULL); // should not do recsearch when creating path
      const SoNodekitCatalog * catalog = orgkit->getNodekitCatalog();
      for (int i = 1; i < PRIVATE(orgkit)->instancelist.getLength(); i++) {
        if (catalog->isLeaf(i) &&
            catalog->getType(i).isDerivedFrom(SoBaseKit::getClassTypeId())) {
          kit = (SoBaseKit *)PRIVATE(orgkit)->instancelist[i]->getValue();
          SbBool didexist = kit != NULL;
          if (!didexist) {
            orgkit->makePart(i);
            kit = (SoBaseKit *)PRIVATE(orgkit)->instancelist[i]->getValue();
          }
          if (SoBaseKit::findPart(partname, kit, partnum, islist, listidx,
                                  makeifneeded, path, recsearch)) {
            return TRUE;
          }
          else if (!didexist) {
            // we created this part, remove it
            orgkit->setPart(i, NULL);
          }
        }
      }
      kit = orgkit; // return with an error in this kit
    }
    // nope, not found
    return FALSE;
  }

  assert(partnum < PRIVATE(kit)->instancelist.getLength());
  SoSFNode * nodefield = PRIVATE(kit)->instancelist[partnum];
  assert(nodefield);

  if (makeifneeded && nodefield->getValue() == NULL) {
    kit->makePart(partnum);
  }

  if (path) {
    const SoNodekitCatalog * catalog = kit->getNodekitCatalog();
    SbList <SoNode*> nodestopart;
    int parent = catalog->getParentPartNumber(partnum);
    while (parent > 0) {
      SoNode * node = PRIVATE(kit)->instancelist[parent]->getValue();
      if (node == NULL) {
        assert(makeifneeded == FALSE);
        break;
      }
      nodestopart.push(node);
      parent = catalog->getParentPartNumber(parent);
    }
    assert(parent == 0 || !makeifneeded);
    while (nodestopart.getLength()) {
      SoNode * node = nodestopart.pop();
      path->append(node);
    }
  }

  if (periodptr == NULL) {
    // singlename or singlelistname found, do not recurse any more
    return TRUE; // all info has been found, just return TRUE
  }
  else { // recurse
    SoNode * node = nodefield->getValue();
    if (node == NULL) return FALSE;
    const ptrdiff_t startidx = periodptr - stringptr + 1;
    SbString newpartname = partname.getSubString((int)startidx);
    if (islist) {
      SoNodeKitListPart * list = (SoNodeKitListPart *) node;
      int numlistchildren = list->getNumChildren();
      if (listidx < 0 || listidx > numlistchildren || (!makeifneeded && listidx == numlistchildren)) {
#if COIN_DEBUG
        SoDebugError::postWarning("SoBaseKit::findPart",
                                  "index %d out of bounds for part ``%s''",
                                  listidx,
                                  firstpartname.getString());
#endif // COIN_DEBUG
        return FALSE;
      }
      else if (listidx == numlistchildren) {
        (void) list->createAndAddDefaultChild();
      }
      SoNode * partnode = list->getChild(listidx);
      assert(partnode && partnode->isOfType(SoBaseKit::getClassTypeId()));
      kit = (SoBaseKit *)partnode;

      if (path) {
        path->append(list);
        path->append(list->getContainerNode());
      }
    }
    else {
      assert(node->isOfType(SoBaseKit::getClassTypeId()));
      kit = (SoBaseKit *)node;
    }
    if (path) path->append(kit);
    return SoBaseKit::findPart(newpartname, kit, partnum, islist,
                               listidx, makeifneeded, path, recsearch);
  }
}

//
// makes part, makes sure node is connected in the scene
//
SbBool
SoBaseKit::makePart(const int partnum)
{
  assert(partnum > 0 && partnum < PRIVATE(this)->instancelist.getLength());
  const SoNodekitCatalog * catalog = this->getNodekitCatalog();
  assert(catalog);

  SoNode * node = (SoNode *)catalog->getDefaultType(partnum).createInstance();
  if (catalog->isList(partnum)) {
    SoNodeKitListPart * list = (SoNodeKitListPart *) node;
    if (catalog->getListContainerType(partnum) != SoGroup::getClassTypeId()) {
      list->setContainerType(catalog->getListContainerType(partnum));
    }
    const SoTypeList & typelist = catalog->getListItemTypes(partnum);
    for (int i = 0; i < typelist.getLength(); i++) {
      list->addChildType(typelist[i]);
    }
    list->lockTypes();
  }
  return this->setPart(partnum, node);
}

/*!
  Sets parts, updates nodekit scene graph, and makes sure graph is
  valid with respect to right siblings and parent.  This method is
  virtual to enable subclasses to detect when a part changes value.

  This method is not part of the original SGI Open Inventor API, but
  is an extension specific to Coin.
*/
SbBool
SoBaseKit::setPart(const int partnum, SoNode * node)
{
  assert(partnum > 0 && partnum < PRIVATE(this)->instancelist.getLength());
  const SoNodekitCatalog * catalog = this->getNodekitCatalog();
  assert(catalog);

  if (node && !node->getTypeId().isDerivedFrom(catalog->getType(partnum))) {
#if COIN_DEBUG
    SoDebugError::postWarning("SoBaseKit::setPart",
                              "Attempted to set part ``%s'' "
                              "to wrong type. Expected ``%s'', got ``%s''",
                              catalog->getName(partnum).getString(),
                              catalog->getType(partnum).getName().getString(),
                              node->getTypeId().getName().getString());
#endif // COIN_DEBUG
    return FALSE;
  }
  int parentIdx = catalog->getParentPartNumber(partnum);
  assert(parentIdx >= 0 && parentIdx < PRIVATE(this)->instancelist.getLength());
  SoNode * parent = NULL;
  if (parentIdx == 0) parent = this;
  else parent = PRIVATE(this)->instancelist[parentIdx]->getValue();
  if (parent == NULL) {
    this->makePart(parentIdx);
    parent = PRIVATE(this)->instancelist[parentIdx]->getValue();
  }
  assert(parent != NULL);
  SoChildList * childlist = parent->getChildren();
  assert(childlist != NULL);

  // if parent is a node derived from SoGroup, use the SoGroup access
  // functions to add/remove/insert children instead of SoChildList
  // directly. This is needed for VRML group nodes to work properly
  // inside node kits. pederb, 2004-06-23
  SoGroup * parentgroup = NULL;
  if (parent->isOfType(SoGroup::getClassTypeId())) {
    parentgroup = (SoGroup*) parent;
  }

  SoNode * oldnode = PRIVATE(this)->instancelist[partnum]->getValue();
  if (oldnode == node) return TRUE; // part is already inserted

  if (childlist->find(node) >= 0) {
    // FIXME: should really allow this, but since it's a bit complex
    // (we need to somehow keep better track of which SoGroup child
    // indices belong to which catalog parts), we just disallow it for
    // now. 20020808 mortene.
    SoDebugError::postWarning("SoBaseKit::setPart",
                              "Node pointer (%p, '%s', '%s') is already used under the same group node in the catalog "
                              "as a child of part '%s' -- this is not allowed",
                              node,
                              node->getName().getString(),
                              node->getTypeId().getName().getString(),
                              catalog->getName(parentIdx).getString());
    return FALSE;
  }

  if (oldnode != NULL) { // part exists, replace
    int oldIdx = childlist->find(oldnode);
    assert(oldIdx >= 0);

    if (parentgroup) {
      if (node) parentgroup->replaceChild(oldIdx, node);
      else parentgroup->removeChild(oldIdx);
    }
    else {
      if (node) childlist->set(oldIdx, node);
      else childlist->remove(oldIdx);
    }
  }
  else if (node) { // find where to insert in parent childlist
    int rightSibling = this->getRightSiblingIndex(partnum);
    if (rightSibling >= 0) { // part has right sibling, insert before
      int idx = childlist->find(PRIVATE(this)->instancelist[rightSibling]->getValue());
      assert(idx >= 0);
      if (parentgroup) {
        parentgroup->insertChild(node, idx);
      }
      else {
        childlist->insert(node, idx);
      }
    }
    else {
      if (parentgroup) {
        parentgroup->addChild(node);
      }
      else {
        childlist->append(node);
      }
    }
  }

  // set part field value
  PRIVATE(this)->instancelist[partnum]->setValue(node);
  return TRUE;
}

//
// returns part number of existing right sibling or -1 if none exists
//
int
SoBaseKit::getRightSiblingIndex(const int partnum)
{
  assert(partnum > 0 && partnum < PRIVATE(this)->instancelist.getLength());
  const SoNodekitCatalog * catalog = this->getNodekitCatalog();

  int sibling = catalog->getRightSiblingPartNumber(partnum);

  // iterate until no more siblings or until we find an existing one
  while (sibling >= 0 && PRIVATE(this)->instancelist[sibling]->getValue() == NULL) {
    sibling = catalog->getRightSiblingPartNumber(sibling);
  }
  return sibling;
}

//
// Searches the field list to find of a node is in this kit.
// Returns catalog index, -1 if not found
//
// parentnum is checked if >= 0
//
int
SoBaseKit::findNodeInThisKit(SoNode * node, const int parentnum) const
{
  const SoNodekitCatalog * catalog = this->getNodekitCatalog();
  if (node == (SoNode *)this) return 0;
  int n = PRIVATE(this)->instancelist.getLength();
  for (int i = 1; i < n; i++) {
    if (PRIVATE(this)->instancelist[i]->getValue() == node &&
        (parentnum < 0 || catalog->getParentPartNumber(i) == parentnum))
      return i;
  }
  return -1;
}

// ******* methods in SoBaseKitP are below ******************************

//
// copy the fields in kit into a new fielddata. This is done to get
// the correct write order: non-part fields first, then leaf parts,
// then non-leaf parts.
//
void
SoBaseKitP::createWriteData(void)
{
  this->writedata = new SoFieldData;
  const SoNodekitCatalog * catalog = this->kit->getNodekitCatalog();
  const SoFieldData * fielddata = kit->getFieldData();

  int n = fielddata->getNumFields();
  for (int pass = 0; pass < 3; pass++) {
    for (int i = 0; i < n; i++) {
      int part = catalog->getPartNumber(fielddata->getFieldName(i));
      // NB: earlier (before 2003-03-26) we did not write private
      // parts.  However, several users have reported that SGI/TGS
      // Inventor do this so we have to write them too.
      // pederb, 2003-03-26
      if ((pass == 0 && part < 0) ||
          (pass == 1 && part > 0 && catalog->isLeaf(part)) ||
          (pass == 2 && part > 0 && !catalog->isLeaf(part))) {
        this->writedata->addField(this->kit,
                                  fielddata->getFieldName(i).getString(),
                                  fielddata->getField(this->kit, i));
      }
    }
  }
}

//
// test if parent part of a part is going to write, and if so
// write part even if isDefault()
//
void
SoBaseKitP::testParentWrite(void)
{
  const SoNodekitCatalog * catalog = this->kit->getNodekitCatalog();
  int n = this->instancelist.getLength();
  for (int i = 1; i < n; i++) {
    SoSFNode * field = this->instancelist[i];
    if (field->isDefault()) { // we might not write
      SoNode * node = field->getValue();
      // don't write if NULL, of course
      if (node) {
        int parent = catalog->getParentPartNumber(i);
        if (parent > 0) {
          assert(this->writedata);
          SbName dummy;
          SoNode * parentnode = this->instancelist[parent]->getValue();
          // we must write if parent is going to write
          if (parentnode &&
              !this->instancelist[parent]->isDefault()) {
            field->setDefault(FALSE);
          }
        }
      }
    }
  }
}

// Copy parts into 'partlist'. All parts have already been copied, but
// we need to update the parts that have a parent as a part, since the
// part node has already been copied by the parent, and we need to use
// that child node pointer, not the copied part.
void
SoBaseKitP::copyParts(const SoBaseKit * srckit, SbList <SoNode*> & partlist,
                      const SbBool COIN_UNUSED_ARG(copyconnections))
{
  int i;
  const int n = this->instancelist.getLength();
  const SoNodekitCatalog * catalog = this->kit->getNodekitCatalog();

  // convenient reference
  const SbList <SoSFNode*> & srcfields = srckit->getCatalogInstances();

  // copy parts that do not have a parent as a part
  for (i = 1; i < n; i++) {
    SoNode * dstnode = this->instancelist[i]->getValue();
    if (dstnode && catalog->getParentPartNumber(i) == 0) {
      SoNode * srcnode = srcfields[i]->getValue();
      assert(dstnode != srcnode);
      assert(srcnode != NULL);
      assert(srcnode->getTypeId() == dstnode->getTypeId());
      srcnode->assertAlive();
      dstnode->assertAlive();
      // the node has been copied since we called
      // SoNode::copyContents() . We just need to store the pointer
      dstnode->ref(); // ref before inserting into list
      if (partlist[i]) partlist[i]->unref();
      partlist[i] = dstnode;
    }
  }
  // copy parts where parent is a part. These parts will already
  // have been copied, but we need to figure out the parent part node,
  // and use the correct child node as the part node instead of the
  // already copied part node.
  for (i = 1; i < n; i++) {
    int parent = catalog->getParentPartNumber(i);
    if (parent > 0 && this->instancelist[i]->getValue()) {
      SoNode * srcgroup = srcfields[parent]->getValue();
      assert(srcgroup);
      SoNode * dstgroup = partlist[parent];
      assert(dstgroup);
      assert(dstgroup->getChildren());
      assert(srcgroup->getChildren());

      // find child index in src kit
      int childidx = srcgroup->getChildren()->find(srcfields[i]->getValue());
      assert(childidx >= 0);

      // use the already copied child as part node
      assert(childidx < dstgroup->getChildren()->getLength());
      SoNode * child = (*(dstgroup->getChildren()))[childidx];
      child->ref(); // ref before inserting
      if (partlist[i]) partlist[i]->unref(); // unref old node in list
      partlist[i] = child;
    }
  }
}

void
SoBaseKitP::setParts(SbList <SoNode*> partlist, const SbBool leafparts)
{
  const int n = this->instancelist.getLength();
  const SoNodekitCatalog * catalog = this->kit->getNodekitCatalog();

  for (int i = 1; i < n; i++) {
    SoNode * node = partlist[i];
    if (node) {
      SbBool leaftst = catalog->isLeaf(i);
      if (leaftst == leafparts) { // correct pass ?
        if (!leaftst) {
          // if it's not a leaf, remove children as the correct children
          // will be added  when children parts are set.
          assert(node->getChildren());
          node->getChildren()->truncate(0);
        }
        this->kit->setPart(i, node);
      }
    }
  }
}

//
// Adds a SoNodekitDetail to the picked point. path should
// contain this kit.
//
void
SoBaseKitP::addKitDetail(SoFullPath * path, SoPickedPoint * pp)
{
  const SoNodekitCatalog * catalog = this->kit->getNodekitCatalog();

  assert(path->findNode(this->kit) >= 0);

  const int n = path->getLength();
  for (int i = path->findNode(this->kit) + 1; i < n; i++) {
    SoNode * node = path->getNode(i);
    int idx = this->kit->findNodeInThisKit(node, -1);
    if (idx > 0 && catalog->isLeaf(idx)) {
      SoNodeKitDetail * detail = new SoNodeKitDetail;
      detail->setNodeKit(this->kit);
      detail->setPart(node);
      SbString partname(catalog->getName(idx));
      // check if node is a SoNodeKitListPart, and if the
      // path extends into the children. Supply index in partname
      // if this is the case.
      if (node->isOfType(SoNodeKitListPart::getClassTypeId()) &&
          path->getLength() >= i + 2) {
        SbString str;
        str.sprintf("%s[%d]",
                    partname.getString(),
                    path->getIndex(i+2));
        partname = SbName(str.getString());
      }
      detail->setPartName(partname);
      pp->setDetail(detail, this->kit);
      // finished
      break;
    }
  }
}

//  Reading in parts of nested nodekits does not allow certain shortcuts
//  that are specified by the Inventor Mentor. The Mentor specifies that
//  within nested nodekits intermediary kits can be left out and will be
//  created automatically. Reported by Gerhard Reitmayr.
SbBool 
SoBaseKitP::readUnknownFields(SoInput *in, SoFieldData *&unknownfielddata)
{
  const SoFieldData * fd = PUBLIC(this)->getFieldData();

  // Binary format
  if (in->isBinary()) {
    SbBool notbuiltin;
    return fd->read(in, PUBLIC(this), TRUE, notbuiltin);
  }

  SbBool firstfield = TRUE;
  SbName fielddescriptionsmarker("fields");

  // ASCII format
  // keep reading fields until we hit close bracket
  while (TRUE) {
    // read first character - if none, EOF
    char c;
    if (!in->read(c))
      return FALSE;
    in->putBack(c);

    if (c == '}')
      return TRUE;

    // read fieldname with no identifier, to be able to read names like
    // appearance.material
    SbName fieldname;
    if (!in->read(fieldname, FALSE))
      return TRUE;
    
    // if this is the first field we try to read, it might be the
    // field descriptions for extension node kits. Detect and read.
    if (firstfield) {
      firstfield = FALSE;
      if (fieldname == fielddescriptionsmarker) {
        if (!fd->readFieldDescriptions(in, PUBLIC(this), 0, FALSE)) {
          return FALSE;
        }
        continue; // read next field
      }
    }

    // try to read data into one of the fields in this nodekit first.
    // SoFieldData::read() will return TRUE and set foundname to FALSE
    // if the field isn't part of the node(kit)
    SbBool foundname;
    if (!fd->read(in, PUBLIC(this), fieldname, foundname))
      return FALSE;
    
    if (!foundname) {
      // add a node pointer field with this name to the unknownFieldData,
      // and read it
      unknownfielddata->addField(PUBLIC(this), fieldname.getString(),
                                 new SoSFNode);
      if (!unknownfielddata->read(in, PUBLIC(this), fieldname, foundname))
        return FALSE;
    }
  }
  // Will never be reached, but functions with a return value other than 
  // void must return *something* by default. At least gcc-4.0.0
  // (Apple snapshot 20041026, default in Mac OS 10.4) will warn.
  return TRUE;
}

#undef PRIVATE
#undef PUBLIC

#endif // HAVE_NODEKITS