File: Setting.cpp

package info (click to toggle)
pymol 2.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 43,312 kB
  • sloc: cpp: 480,106; python: 79,860; ansic: 28,343; javascript: 6,792; sh: 47; makefile: 30; csh: 8
file content (3252 lines) | stat: -rw-r--r-- 96,220 bytes parent folder | download | duplicates (2)
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
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252

/* 
A* -------------------------------------------------------------------
B* This file contains source code for the PyMOL computer program
C* copyright 1998-2000 by Warren Lyford Delano of DeLano Scientific. 
D* -------------------------------------------------------------------
E* It is unlawful to modify or remove this copyright notice.
F* -------------------------------------------------------------------
G* Please see the accompanying LICENSE file for further information. 
H* -------------------------------------------------------------------
I* Additional authors of this source file include:
-* 
-* 
-*
Z* -------------------------------------------------------------------
*/
#include"os_python.h"

#include"os_proprietary.h"

#include"os_predef.h"
#include"os_std.h"

#include"Base.h"
#include"OOMac.h"
#include"MemoryDebug.h"
#include"Ortho.h"
#include"Setting.h"
#include"Scene.h"
#include"ButMode.h"
#include"CGO.h"
#include"Executive.h"
#include"Editor.h"
#include"P.h"
#include"Util.h"
#include"main.h"
#include"PConv.h"
#include"Wizard.h"
#include"Seq.h"
#include"PyMOLOptions.h"
#include"OVContext.h"
#include"ShaderMgr.h"
#include"Sphere.h"
#include"Selector.h"
#include"Parse.h"

#ifdef _PYMOL_OPENVR
#include"OpenVRMode.h"
#endif

/*
 * Setting level info table
 *
 * Levels are not hierarchical at the atom/bond level, that's why a simple
 * sorted enumeration is not sufficient.
 *
 * global < object < object-state
 *                   object-state < atom < atom-state
 *                   object-state < bond < bond-state
 */
const SettingLevelInfoType SettingLevelInfo[] = {
  {"unused"        , 0x00}, // 0b00000000
  {"global"        , 0x00}, // 0b00000000
  {"object"        , 0x01}, // 0b00000001
  {"object-state"  , 0x03}, // 0b00000011
  {"atom"          , 0x07}, // 0b00000111
  {"atom-state"    , 0x0F}, // 0b00001111
  {"bond"          , 0x13}, // 0b00010011
  {"bond-state"    , 0x33}, // 0b00110011
  {NULL, 0}
};

// The following defines the static SettingInfo table
#define SETTINGINFO_IMPLEMENTATION
#include "SettingInfo.h"

template <> const char * SettingGet<const char *>(int index, const CSetting * I);

// get level name for setting index (for feedback)
const char * SettingLevelGetName(PyMOLGlobals * G, int index) {
  return SettingLevelInfo[SettingInfo[index].level].name;
}

// check if setting index is valid in given level-mask
bool SettingLevelCheckMask(PyMOLGlobals * G, int index, unsigned char mask) {
  unsigned char validmask = SettingLevelInfo[SettingInfo[index].level].mask;
  return (0 == (mask & ~validmask));
}

// check if setting index is valid in given level
bool SettingLevelCheck(PyMOLGlobals * G, int index, unsigned char level) {
  return SettingLevelCheckMask(G, index, SettingLevelInfo[level].mask);
}

/* ================================================================== */

static CSetting *SettingCopyAll(PyMOLGlobals * G, const CSetting * src, CSetting * dst)
{
  if(!dst) {
    dst = pymol::calloc<CSetting>(1);
  } else {
    SettingPurge(dst);
  }

  SettingInit(G, dst);

  if(dst && src) {

    /* simply overwriting existing data (if any) ... in the future we
       may need to release references etc. before doing this */

    unsigned int size = VLAGetSize(src->info);
    VLACheck(dst->info, SettingRec, size - 1);
    UtilCopyMem(dst->info, src->info, sizeof(SettingRec) * size);
    dst->size = src->size;

    // need to properly copy strings
    for (int index = 0; index < cSetting_INIT; ++index) {
      if (SettingInfo[index].type == cSetting_string
          && src->info[index].str_) {
        dst->info[index].str_ = new std::string(*src->info[index].str_);
      }
    }
  }
  return dst;
}

void SettingStoreDefault(PyMOLGlobals * G)
{
  G->Default = SettingCopyAll(G, G->Setting, G->Default);
}

void SettingPurgeDefault(PyMOLGlobals * G)
{
  if(G->Default) {
    SettingPurge(G->Default);
    FreeP(G->Default);
    G->Default = NULL;
  }
}

void SettingUniqueDetachChain(PyMOLGlobals * G, int unique_id)
{
  CSettingUnique *I = G->SettingUnique;
  OVreturn_word result;
  if(OVreturn_IS_OK(result = OVOneToOne_GetForward(I->id2offset, unique_id))) {
    int offset = result.word;
    int next;

    OVOneToOne_DelForward(I->id2offset, unique_id);

    {
      SettingUniqueEntry *entry;
      while(offset) {
        entry = I->entry + offset;
        next = entry->next;
        entry->next = I->next_free;
        I->next_free = offset;
        offset = next;
      }
    }
  } else {
    /* uncaught error */
  }
}

static void SettingUniqueExpand(PyMOLGlobals * G)
{
  CSettingUnique *I = G->SettingUnique;

  if(!I->next_free) {
    int new_n_alloc = (I->n_alloc * 3) / 2;
    int a;
    VLACheck(I->entry, SettingUniqueEntry, new_n_alloc);
    for(a = I->n_alloc; a < new_n_alloc; a++) {
      I->entry[a].next = I->next_free;
      I->next_free = a;
    }
    I->n_alloc = new_n_alloc;
  }
}

static
SettingUniqueEntry *SettingFindSettingUniqueEntry(PyMOLGlobals * G, int unique_id, int setting_id)
{
  CSettingUnique *I = G->SettingUnique;
  OVreturn_word result;
  if(OVreturn_IS_OK(result = OVOneToOne_GetForward(I->id2offset, unique_id))) {
    SettingUniqueEntry *entry;
    for (int offset = result.word; offset; offset = entry->next) {
      entry = I->entry + offset;
      if(entry->setting_id == setting_id) {
        return entry;
      }
    }
  }
  return NULL;
}

int SettingUniqueCheck(PyMOLGlobals * G, int unique_id, int setting_id)
{
  return SettingFindSettingUniqueEntry(G, unique_id, setting_id) != NULL;
}

/*
 * Return true for convertible types and set int-compatible types to int
 */
inline bool type_upcast(int &type) {
  switch (type) {
    case cSetting_int:
    case cSetting_color:
    case cSetting_boolean:
      type = cSetting_int;
    case cSetting_float:
      return true;
  }
  return false;
}

bool SettingUniqueGetTypedValuePtr(PyMOLGlobals * G, int unique_id, int setting_id,
                                      int setting_type, void * value)
{
  auto entry = SettingFindSettingUniqueEntry(G, unique_id, setting_id);
  if (!entry)
    return false;

  int type_from = SettingInfo[setting_id].type;

  if (type_from != setting_type) {
    if (!type_upcast(type_from) ||
        !type_upcast(setting_type)) {
      PRINTFB(G, FB_Setting, FB_Errors)
        " Setting-Error: type mismatch\n" ENDFB(G);
      return false;
    }
  }

  if (setting_type == cSetting_float3){
    *(const float **) value = entry->value.float3_;
  } else if (setting_type == type_from) {
    *(int *) value = entry->value.int_;
  } else if (setting_type == cSetting_int) {
    *(int *) value = (int) entry->value.float_;
  } else { // setting_type == cSetting_float
    *(float *) value = (float) entry->value.int_;
  }

  return true;
}

/*
 * Warning: Returns colors as (fff) tuple instead of color index
 */
PyObject *SettingUniqueGetPyObject(PyMOLGlobals * G, int unique_id, int index)
{
  int type = SettingGetType(G, index);

  union {
    int val_i;
    float val_f;
    const float * ptr_3f;
  };

  if (SettingUniqueGetTypedValuePtr(G, unique_id, index, type, &ptr_3f)) {
    switch (type) {
    case cSetting_boolean:
      return CPythonVal_New_Boolean(val_i);
    case cSetting_int:
      return CPythonVal_New_Integer(val_i);
    case cSetting_float:
      return CPythonVal_New_Float(val_f);
    case cSetting_color:
#ifdef _PYMOL_NOPY
      return CPythonVal_New_Integer(val_i);
#else
      return PYOBJECT_CALLFUNCTION(G->P_inst->colortype, "i", val_i);
#endif
    case cSetting_float3:
      {
        PyObject *result = PyTuple_New(3);
        PyTuple_SET_ITEM(result, 0, CPythonVal_New_Float(ptr_3f[0]));
        PyTuple_SET_ITEM(result, 1, CPythonVal_New_Float(ptr_3f[1]));
        PyTuple_SET_ITEM(result, 2, CPythonVal_New_Float(ptr_3f[2]));
        return result;
      }
    }
  }

  return NULL;
}

static int SettingUniqueEntry_IsSame(SettingUniqueEntry *entry, int setting_type, const void *value){
  if (SettingInfo[entry->setting_id].type != setting_type){
    return 0;
  }
  if (setting_type == cSetting_float3){
    const float *v = (float*)value, *ev = entry->value.float3_;
    return v[0]==ev[0] && v[1]==ev[1] && v[2]==ev[2];
  } else {
    return (entry->value.int_ == *(int *) value);
  }
}

static void SettingUniqueEntry_Set(SettingUniqueEntry *entry, int value_type, const void *value){
  int setting_type = SettingGetType(entry->setting_id);
  switch (value_type) {
    case cSetting_boolean:
    case cSetting_int:
    case cSetting_color:
      if (setting_type == cSetting_float) {
        entry->value.float_ = *(int*) value;
      } else {
        entry->value.int_ = *(int *) value;
      }
      break;
    case cSetting_float:
      if (setting_type != cSetting_float) {
        entry->value.int_ = *(float *) value;
      } else {
        entry->value.float_ = *(float *) value;
      }
      break;
    case cSetting_float3:
      memcpy(entry->value.float3_, *(const float **) value, sizeof(float) * 3);
      break;
    default:
      printf("SettingUniqueEntry_Set-Error: unsupported type %d\n", value_type);
  }
}

/*
 * Return false if setting was not set (nothing changed)
 */
bool SettingUniqueUnset(PyMOLGlobals * G, int unique_id, int setting_id)
{
  auto I = G->SettingUnique;
  auto result = OVOneToOne_GetForward(I->id2offset, unique_id);

  if (OVreturn_IS_OK(result)) {
    for (int prev = 0, offset = result.word; offset;
        prev = offset, offset = I->entry[offset].next) {
      if (I->entry[offset].setting_id != setting_id)
        continue;

      if(!prev) {           /* if first entry in list */
        OVOneToOne_DelForward(I->id2offset, unique_id);
        if(I->entry[offset].next) {   /* set new list start */
          OVOneToOne_Set(I->id2offset, unique_id, I->entry[offset].next);
        }
      } else {              /* otherwise excise from middle or end */
        I->entry[prev].next = I->entry[offset].next;
      }
      I->entry[offset].next = I->next_free;
      I->next_free = offset;

      return true;
    }
  }
  return false;
}

int SettingUniqueSetTypedValue(PyMOLGlobals * G, int unique_id, int setting_id,
			       int setting_type, const void *value)

/* set value to NULL in order to delete setting */
{
  CSettingUnique *I = G->SettingUnique;
  OVreturn_word result;
  int isset = false;

  if (!value) {
    return SettingUniqueUnset(G, unique_id, setting_id);
  }

  if(OVreturn_IS_OK((result = OVOneToOne_GetForward(I->id2offset, unique_id)))) {       /* setting list exists for atom */
    int offset = result.word;
    int prev = 0;
    int found = false;
    while(offset) {
      SettingUniqueEntry *entry = I->entry + offset;
      if(entry->setting_id == setting_id) {
        found = true;           /* this setting is already defined */
	  if (!SettingUniqueEntry_IsSame(entry, setting_type, value)){
	    SettingUniqueEntry_Set(entry, setting_type, value);
	    isset = true;
	  }
        break;
      }
      prev = offset;
      offset = entry->next;
    }
    if((!found) && value) {     /* setting not found in existing list, so append new value */
      if(!I->next_free)
        SettingUniqueExpand(G);
      if(I->next_free) {
        offset = I->next_free;
        {
          SettingUniqueEntry *entry = I->entry + offset;
          I->next_free = entry->next;
          entry->next = 0;

          if(prev) {            /* append onto existing list */
            I->entry[prev].next = offset;
            entry->setting_id = setting_id;
            SettingUniqueEntry_Set(entry, setting_type, value);
            isset = true;
          } else if(OVreturn_IS_OK(OVOneToOne_Set(I->id2offset, unique_id, offset))) {
            /* create new list */
            entry->setting_id = setting_id;
            SettingUniqueEntry_Set(entry, setting_type, value);
            isset = true;
          }
        }
      }
    }
  } else if(value && (result.status == OVstatus_NOT_FOUND)) {   /* new setting list for atom */
    if(!I->next_free)
      SettingUniqueExpand(G);
    if(I->next_free) {
      int offset = I->next_free;
      SettingUniqueEntry *entry = I->entry + offset;

      if(OVreturn_IS_OK(OVOneToOne_Set(I->id2offset, unique_id, offset))) {
        I->next_free = entry->next;
        entry->setting_id = setting_id;
        entry->next = 0;
        SettingUniqueEntry_Set(entry, setting_type, value);
        isset = true;
      }
    }
  } else {
    /* unhandled error */
  }
  return isset;
}

#ifndef _PYMOL_NOPY
bool SettingUniqueSetPyObject(PyMOLGlobals * G, int unique_id, int index, PyObject *value)
{
  if (!value)
    return SettingUniqueUnset(G, unique_id, index);

  int type = SettingGetType(G, index);

  float val_3f[3];
  union {
    int val_i;
    float val_f;
    float * ptr_3f;
  };

  switch (type) {
  case cSetting_boolean:
  case cSetting_int:
    ok_assert(1, PConvPyObjectToInt(value, &val_i));
    break;
  case cSetting_float:
    ok_assert(1, PConvPyObjectToFloat(value, &val_f));
    break;
  case cSetting_color:
    if (!PConvPyIntToInt(value, &val_i)) {
      OrthoLineType sval;
      ok_assert(1, PConvPyStrToStr(value, sval, OrthoLineLength));
      val_i = ColorGetIndex(G, sval);
    }
    break;
  case cSetting_float3:
    if (!PConvPyListOrTupleToFloatArrayInPlace(value, val_3f, 3)) {
      OrthoLineType sval;
      ok_assert(1, PConvPyStrToStr(value, sval, OrthoLineLength) &&
          sscanf(sval, "%f%f%f", &val_3f[0], &val_3f[1], &val_3f[2]) == 3);
    }
    ptr_3f = val_3f;
    break;
  default:
    PRINTFB(G, FB_Python, FB_Errors)
      " Python-Error: atom-state-level setting unsupported type=%d\n", type ENDFB(G);
    return false;
  }

  return SettingUniqueSetTypedValue(G, unique_id, index, type, &val_i);

ok_except1:
  PRINTFB(G, FB_Setting, FB_Errors)
    " Setting-Error: type mismatch\n" ENDFB(G);
  return false;
}
#endif

void SettingUniqueResetAll(PyMOLGlobals * G)
{
  CSettingUnique *I = G->SettingUnique;

  OVOneToOne_Reset(I->id2offset);
  {
    int a;
    I->n_alloc = 10;
    VLAFreeP(I->entry);
    I->entry = VLACalloc(SettingUniqueEntry, I->n_alloc);
    /* note: intentially skip index 0  */
    for(a = 2; a < 10; a++) {
      I->entry[a].next = a - 1;
    }
    I->next_free = I->n_alloc - 1;
  }
}

int SettingUniquePrintAll(PyMOLGlobals * G, int src_unique_id)
{
  int ok = true;
  CSettingUnique *I = G->SettingUnique;
  OVreturn_word src_result;
  printf("SettingUniquePrintAll: ");
  if(OVreturn_IS_OK(src_result = OVOneToOne_GetForward(I->id2offset, src_unique_id))) {
    int src_offset = src_result.word;
    SettingUniqueEntry *src_entry;
    while(ok && src_offset) {
      {
	src_entry = I->entry + src_offset;
	{
	  int setting_id = src_entry->setting_id;
	  int setting_type = SettingInfo[setting_id].type;
	  const char * setting_name = SettingInfo[setting_id].name;
	  switch (setting_type) {
	  case cSetting_int:
	  case cSetting_color:
	  case cSetting_boolean:
	    printf("%s:%d:%d:%d ", setting_name, setting_id, setting_type, src_entry->value.int_);
	    break;
	  case cSetting_float:
	    printf("%s:%d:%d:%f ", setting_name, setting_id, setting_type, src_entry->value.float_);
	    break;
	  case cSetting_float3:
	    printf("%s:%d:%d:%f,%f,%f ", setting_name, setting_id, setting_type, src_entry->value.float3_[0],
		   src_entry->value.float3_[1],
		   src_entry->value.float3_[2]);
	    break;
	  case cSetting_string:
	    printf("%s:%d:%d:s%d ", setting_name, setting_id, setting_type, src_entry->value.int_);
	    break;
	  }
	}
      }
      src_offset = I->entry[src_offset].next; /* src_entry invalid, since I->entry may have changed */
    }
  }
  printf("\n");
  return ok;
}

int SettingUniqueCopyAll(PyMOLGlobals * G, int src_unique_id, int dst_unique_id)
{
  int ok = true;
  CSettingUnique *I = G->SettingUnique;
  OVreturn_word dst_result;

  if(OVreturn_IS_OK((dst_result = OVOneToOne_GetForward(I->id2offset, dst_unique_id)))) {       /* setting list exists for atom */
    PRINTFB(G, FB_Setting, FB_Errors)
      " SettingUniqueCopyAll-Bug: merging settings not implemented\n"
      ENDFB(G);
    ok = false;
  } else if(dst_result.status == OVstatus_NOT_FOUND) {  /* new setting list for atom */
    OVreturn_word src_result;
    if(OVreturn_IS_OK(src_result = OVOneToOne_GetForward(I->id2offset, src_unique_id))) {
      int dst_offset = 0;
      for (int src_offset = src_result.word; src_offset;
          src_offset = I->entry[src_offset].next) {
        SettingUniqueExpand(G); // this may reallocate I->entry

        if (!dst_offset) {
          OVOneToOne_Set(I->id2offset, dst_unique_id, I->next_free);
        } else {
          I->entry[dst_offset].next = I->next_free;
        }

        dst_offset = I->next_free;
        I->next_free = I->entry[dst_offset].next;
        I->entry[dst_offset] = I->entry[src_offset];
        I->entry[dst_offset].next = 0;
      }
    }
  } else {
    ok = false;
    /* unhandled error */
  }

  return ok;
}

static void SettingUniqueInit(PyMOLGlobals * G)
{
  CSettingUnique *I = G->SettingUnique;

  if((I = (G->SettingUnique = pymol::calloc<CSettingUnique>(1)))) {
    I->id2offset = OVOneToOne_New(G->Context->heap);
    {
      int a;
      I->n_alloc = 10;
      I->entry = VLACalloc(SettingUniqueEntry, I->n_alloc);
      /* note: intentially skip index 0  */
      for(a = 2; a < 10; a++) {
        I->entry[a].next = a - 1;       /* 1-based linked list with 0 as sentinel */
      }
      I->next_free = I->n_alloc - 1;
    }
  }
}

static void SettingUniqueFree(PyMOLGlobals * G)
{
  CSettingUnique *I = G->SettingUnique;
  VLAFreeP(I->entry);
  OVOneToOne_Del(I->id2offset);
  FreeP(I);
}

/*
 * For unique_id remapping during partial session loading
 */
int SettingUniqueConvertOldSessionID(PyMOLGlobals * G, int old_unique_id)
{
  CSettingUnique *I = G->SettingUnique;
  int unique_id = old_unique_id;
  if(I->old2new) {
    OVreturn_word ret;
    if(OVreturn_IS_OK(ret = OVOneToOne_GetForward(I->old2new, old_unique_id))) {
      unique_id = ret.word;
    } else {
      unique_id = AtomInfoGetNewUniqueID(G);
      OVOneToOne_Set(I->old2new, old_unique_id, unique_id);
    }
  } else {
    AtomInfoReserveUniqueID(G, unique_id);
  }
  return unique_id;
}

/*
 * Return true if the given setting index should not be stored to PSE.
 *
 * Blacklisted are unused and system-dependent settings.
 */
static bool is_session_blacklisted(int index) {
  if (index >= cSetting_INIT ||
      SettingInfo[index].level == cSettingLevel_unused) {
    return true;
  }

  switch (index) {
  case cSetting_antialias_shader:
  case cSetting_ati_bugs:
  case cSetting_cache_max:
  case cSetting_cgo_shader_ub_color:
  case cSetting_cgo_shader_ub_flags:
  case cSetting_cgo_shader_ub_normal:
  case cSetting_colored_feedback:
  case cSetting_cylinder_shader_ff_workaround:
  case cSetting_defer_updates:
  case cSetting_fast_idle:
  case cSetting_internal_feedback:
  case cSetting_internal_gui:
  case cSetting_internal_prompt:
  case cSetting_logging:
  case cSetting_max_threads:
  case cSetting_mouse_grid:
  case cSetting_mouse_scale:
  case cSetting_nb_spheres_use_shader:
  case cSetting_no_idle:
  case cSetting_nvidia_bugs:
  case cSetting_presentation:
  case cSetting_precomputed_lighting:
  case cSetting_render_as_cylinders:
  case cSetting_security:
  case cSetting_session_changed:
  case cSetting_session_file:
  case cSetting_session_migration:
  case cSetting_session_version_check:
  case cSetting_shaders_from_disk:
  case cSetting_show_progress:
  case cSetting_slow_idle:
  case cSetting_stereo:
  case cSetting_stereo_double_pump_mono:
  case cSetting_stereo_mode:
  case cSetting_suspend_deferred:
  case cSetting_suspend_undo:
  case cSetting_suspend_undo_atom_count:
  case cSetting_suspend_updates:
  case cSetting_text:
  case cSetting_trilines:
  case cSetting_use_geometry_shaders:
  case cSetting_use_shaders:
  case cSetting_pick32bit:
  case cSetting_display_scale_factor:
#ifdef _PYMOL_IOS
  case cSetting_cgo_sphere_quality:
  case cSetting_dynamic_measures:
  case cSetting_label_outline_color:
  case cSetting_mouse_selection_mode:
  case cSetting_sphere_mode:
  case cSetting_sphere_quality:
  case cSetting_stick_ball:
  case cSetting_virtual_trackball:
#elif defined(_PYMOL_ACTIVEX)
  case cSetting_async_builds:
#endif
    return true;
  }

  return false;
}

int SettingUniqueFromPyList(PyMOLGlobals * G, PyObject * list, int partial_restore)
{
  int ok = true;
  if(!partial_restore) {
    SettingUniqueResetAll(G);
  }
  if(list)
    if(PyList_Check(list)) {
      ov_size n_id = PyList_Size(list);
      ov_size a;
      for(a = 0; a < n_id; a++) {
        PyObject *id_list = PyList_GetItem(list, a);
        int unique_id;
        if(ok)
          ok = PyList_Check(id_list);
        if(ok)
          ok = (PyList_Size(id_list) > 1);
        if(ok)
          ok = PConvPyIntToInt(PyList_GetItem(id_list, 0), &unique_id);
        if(ok && partial_restore) {
          unique_id = SettingUniqueConvertOldSessionID(G, unique_id);
        }
        if(ok) {
          ov_size n_set = 0;

          PyObject *setting_list = PyList_GetItem(id_list, 1);
          if(ok)
            ok = PyList_Check(setting_list);
          if(ok)
            n_set = PyList_Size(setting_list);
          if(ok) {
            ov_size b;
            for(b = 0; b < n_set; b++) {
              PyObject *entry_list = PyList_GetItem(setting_list, b);
              if(ok)
                ok = PyList_Check(entry_list);
              if(ok)
                ok = (PyList_Size(entry_list) > 2);
              if(ok) {
                int setting_id;
                int setting_type;
                float val_3f[3];
                union {
                  int int_;
                  float float_;
		  float * float3_;
                } value_store;
                if(ok)
                  ok = PConvPyIntToInt(PyList_GetItem(entry_list, 0), &setting_id);
                if(ok)
                  ok = PConvPyIntToInt(PyList_GetItem(entry_list, 1), &setting_type);
                if(ok)
                  switch (setting_type) {

                  case cSetting_int:
                  case cSetting_color:
                  case cSetting_boolean:
                    ok = PConvPyIntToInt(PyList_GetItem(entry_list, 2),
                                         &value_store.int_);
                    break;
                  case cSetting_float:
                    ok = PConvPyFloatToFloat(PyList_GetItem(entry_list, 2),
                                             &value_store.float_);
                    break;
		  case cSetting_float3:
		    {
		      CPythonVal *el = CPythonVal_PyList_GetItem(G, entry_list, 2);
		      value_store.float3_ = val_3f;
		      ok = PConvPyListToFloatArrayInPlace(el, value_store.float3_, 3);
		      CPythonVal_Free(el);
		    }
		    break;
                  }
                if(ok) {
                  SettingUniqueSetTypedValue(G, unique_id, setting_id,
                                             setting_type, &value_store);
                }
              }
            }
          }
        }
      }
    }
  return ok;
}

PyObject *SettingUniqueAsPyList(PyMOLGlobals * G)
{
  PyObject *result = NULL;
  CSettingUnique *I = G->SettingUnique;
  {
    ov_word hidden = 0;
    OVreturn_word ret;
    int n_entry = 0;
    while(1) {
      ret = OVOneToOne_IterateForward(I->id2offset, &hidden);
      if(ret.status != OVstatus_YES)
        break;
      n_entry++;
    }
    result = PyList_New(n_entry);
    if(result) {
      hidden = 0;
      n_entry = 0;
      while(1) {
        PyObject *setting_list = NULL;
        int save_offset, unique_id;
        ret = OVOneToOne_IterateForward(I->id2offset, &hidden);

        if(ret.status != OVstatus_YES)
          break;
        unique_id = ret.word;
        if(OVreturn_IS_OK(ret = OVOneToOne_GetForward(I->id2offset, unique_id))) {
          int offset = ret.word;
          int n_set = 0;

          /* count number of settings for this unique_id */

          SettingUniqueEntry *entry;
          save_offset = offset;
          while(offset) {
            entry = I->entry + offset;
            n_set++;
            offset = entry->next;
          }

          /* create and insert list for each setting */

          setting_list = PyList_New(n_set);
          n_set = 0;
          offset = save_offset;
          while(offset) {
            PyObject *setting_entry = PyList_New(3);
            entry = I->entry + offset;
            int type = SettingInfo[entry->setting_id].type;
            PyList_SetItem(setting_entry, 0, PyInt_FromLong(entry->setting_id));
            PyList_SetItem(setting_entry, 1, PyInt_FromLong(type));
            switch (type) {
            case cSetting_int:
            case cSetting_color:
            case cSetting_boolean:
              PyList_SetItem(setting_entry, 2, PyInt_FromLong(entry->value.int_));
              break;
            case cSetting_float:
              PyList_SetItem(setting_entry, 2,
                             PyFloat_FromDouble(*(float *) &entry->value.float_));
              break;
	    case cSetting_float3:
	      PyList_SetItem(setting_entry, 2,
			     PConvFloatArrayToPyList((float *) &entry->value.float3_, 3));
	      break;
            }
            PyList_SetItem(setting_list, n_set, setting_entry);
            n_set++;
            offset = entry->next;
          }
        }

        /* add this unique_id set into the overall list */

        {
          PyObject *unique_list = PyList_New(2);
          PyList_SetItem(unique_list, 0, PyInt_FromLong(unique_id));
          PyList_SetItem(unique_list, 1, setting_list);
          PyList_SetItem(result, n_entry, unique_list);
        }
        n_entry++;
      }
    }
  }
  return (PConvAutoNone(result));
}

int SettingSetSmart_i(PyMOLGlobals * G, CSetting * set1, CSetting * set2, int index,
                      int value)
{
  int dummy;
  if(set1 && SettingGetIfDefined_i(G, set1, index, &dummy)) {
    return SettingSet_i(set1, index, value);
  }
  if(set2 && SettingGetIfDefined_i(G, set2, index, &dummy)) {
    return SettingSet_i(set2, index, value);
  }
  return SettingSetGlobal_i(G, index, value);
}

int SettingSetGlobalsFromPyList(PyMOLGlobals * G, PyObject * list)
{
  int ok = true;

  CSetting *I = G->Setting;

  if(list)
    if(PyList_Check(list))
      ok = SettingFromPyList(I, list);

  /* restore the following settings  */

  if(G->Option->no_quit) {
    SettingSet_b(I, cSetting_presentation_auto_quit, 0);
  }

  ColorUpdateFrontFromSettings(G);
  return (ok);
}

PyObject *SettingGetGlobalsAsPyList(PyMOLGlobals * G)
{
  PyObject *result = NULL;
  CSetting *I = G->Setting;
  result = SettingAsPyList(I);
  return (PConvAutoNone(result));
}

static PyObject *get_list(CSetting * I, int index, bool incl_blacklisted)
{
  PyObject *result = NULL, *value = NULL;
  int setting_type = SettingInfo[index].type;

  if (!incl_blacklisted && is_session_blacklisted(index)) {
    return NULL;
  }

  switch (setting_type) {

  case cSetting_boolean:
  case cSetting_int:
  case cSetting_color:
    value = PyInt_FromLong(I->info[index].int_);
    break;
  case cSetting_float:
    value = PyFloat_FromDouble(I->info[index].float_);
    break;
  case cSetting_float3:
    value = PConvFloatArrayToPyList(I->info[index].float3_, 3);
    break;
  case cSetting_string:
    value = PyString_FromString(SettingGet<const char *>(index, I));
    break;
  }

  if (value) {
    result = PyList_New(3);
    PyList_SetItem(result, 0, PyInt_FromLong(index));
    PyList_SetItem(result, 1, PyInt_FromLong(setting_type));
    PyList_SetItem(result, 2, value);
  }

  return result;
}

PyObject *SettingAsPyList(CSetting * I, bool incl_blacklisted)
{
  PyObject *result = NULL;
  int a;

  if(I) {
    std::vector<PyObject*> list;
    list.reserve(cSetting_INIT);

    for(a = 0; a < cSetting_INIT; a++) {
      if(I->info[a].defined) {
        PyObject * item = get_list(I, a, incl_blacklisted);
        if (item != NULL) {
          list.push_back(item);
        }
      }
    }

    result = PConvToPyObject(list);
  }
  return (PConvAutoNone(result));
}

/*========================================================================*/
static int SettingCheckUseShaders(CSetting * I, int quiet)
{
  PyMOLGlobals * G = I->G;
    if (SettingGetGlobal_i(G, cSetting_use_shaders)){
      if (G->ShaderMgr->IsConfigured() && !G->ShaderMgr->ShadersPresent()){
	SettingSet_b(I, cSetting_use_shaders, 0);
	if (!quiet){
	  PRINTFB(G, FB_Setting, FB_Warnings)
	    "Setting-Error: use_shaders cannot be set when Shaders are not available, setting use_shaders back to false\n"
	    ENDFB(G);
	}
	return 1;
      }
    }
    return 0;
}

/*========================================================================*/
static int set_list(CSetting * I, PyObject * list)
{
  int index = -1;
  int setting_type = -1;

  union {
    int val_i;
    float val_f;
    float val_3f[3];
    const char * val_s;
  };

  if (list == NULL || CPythonVal_IsNone(list))
    return true;

  ok_assert(1, PyList_Check(list));
  ok_assert(1, CPythonVal_PConvPyIntToInt_From_List(I->G, list, 0, &index));
  ok_assert(1, CPythonVal_PConvPyIntToInt_From_List(I->G, list, 1, &setting_type));

  if (is_session_blacklisted(index))
    return true;

  switch (setting_type) {
  case cSetting_boolean:
  case cSetting_int:
  case cSetting_color:
    ok_assert(1, CPythonVal_PConvPyIntToInt_From_List(I->G, list, 2, &val_i));
    if (setting_type == cSetting_color)
      val_i = ColorConvertOldSessionIndex(I->G, val_i);
    SettingSet_i(I, index, val_i);
    break;
  case cSetting_float:
    ok_assert(1, CPythonVal_PConvPyFloatToFloat_From_List(I->G, list, 2, &val_f));
    SettingSet_f(I, index, val_f);
    break;
  case cSetting_float3:
    ok_assert(1, CPythonVal_PConvPyListToFloatArrayInPlaceAutoZero_From_List(I->G, list, 2, val_3f, 3));
    SettingSet_3fv(I, index, val_3f);
    break;
  case cSetting_string:
    ok_assert(1, val_s = PyString_AsString(PyList_GetItem(list, 2)));
    SettingSet_s(I, index, val_s);
    break;
  default:
    ok_raise(1);
  }

  return true;
ok_except1:
  printf(" set_list-Error: i=%d, t=%d\n", index, setting_type);
  return false;
}

/*========================================================================*/
/*
 * Used to set object and object-state level settings from PSEs
 */
CSetting *SettingNewFromPyList(PyMOLGlobals * G, PyObject * list)
{
  int ok = true;
  ov_size size;
  ov_size a;
  CSetting *I = NULL;
  if(ok)
    ok = (list != NULL);
  if(ok)
    ok = PyList_Check(list);
  if(ok) {
    I = SettingNew(G);
    size = PyList_Size(list);
    for(a = 0; a < size; a++) {
      if(ok)
        ok = set_list(I, PyList_GetItem(list, a));
    }
  }
  return (I);
}

/*========================================================================*/
int SettingFromPyList(CSetting * I, PyObject * list)
{
  int ok = true;
  ov_size size;
  ov_size a;

  if(ok)
    ok = (I != NULL);
  if(ok)
    ok = PyList_Check(list);
  if(ok) {
    size = PyList_Size(list);
    for(a = 0; a < size; a++) {
      if(!set_list(I, PyList_GetItem(list, a)))
        ok = false;
    }
  }
  return (ok);
}

/*========================================================================*/
/*
 * Get the indices of all settings that have changed since last calling
 * this function. Resets the "changed" flag.
 *
 * NOTE: assumes blocked interpreter
 *
 * name: object name or NULL/"" for global settings
 * state: object state
 */
std::vector<int> SettingGetUpdateList(PyMOLGlobals * G, const char * name, int state)
{
  CSetting **handle, *I = G->Setting;
  int a;
  int n;
  std::vector<int> result;

  if (name && name[0]) {
    // object-state settings

    CObject *obj = ExecutiveFindObjectByName(G, name);

    if (!obj ||
        !(handle = obj->getSettingHandle(state)) ||
        !(I = *handle))
      // not found -> empty list
      return result;
  }

  n = VLAGetSize(I->info);
  for(a = 0; a < n; a++) {
    if(I->info[a].changed) {
      I->info[a].changed = false;
      result.push_back(a);
    }
  }
  return (result);

}


/*========================================================================*/
void SettingCheckHandle(PyMOLGlobals * G, CSetting ** handle)
{
  if(!*handle)
    *handle = SettingNew(G);
}


/*========================================================================*/
int SettingGetTextValue(PyMOLGlobals * G, const CSetting * set1, const CSetting * set2, int index,
                        char *buffer)
{
  const char * sptr = SettingGetTextPtr(G, set1, set2, index, buffer);
  if(!sptr)
    return 0;

  if (sptr != buffer) {
    if(strlen(sptr) > OrthoLineLength) {
      PRINTFB(G, FB_Setting, FB_Warnings)
        "Setting-Warning: text longer than OrthoLineLength" ENDFB(G);
    }

    strncpy(buffer, sptr, OrthoLineLength);
  }

  return 1;
}

/*========================================================================*/
/*
 * Returns a pointer to the internal string representation if available,
 * or it formats the value into buffer and returns a pointer to buffer.
 */
const char * SettingGetTextPtr(PyMOLGlobals * G, const CSetting * set1, const CSetting * set2,
                               int index, char *buffer)
{
  int type;
  const char *sptr = NULL;
  const float *ptr;
  type = SettingGetType(G, index);
  switch (type) {
  case cSetting_boolean:
    sprintf(buffer, SettingGet_b(G, set1, set2, index) ? "on" : "off");
    break;
  case cSetting_int:
    sprintf(buffer, "%d", SettingGet_i(G, set1, set2, index));
    break;
  case cSetting_float:
    sprintf(buffer, "%1.5f", SettingGet_f(G, set1, set2, index));
    break;
  case cSetting_float3:
    ptr = SettingGet_3fv(G, set1, set2, index);
    sprintf(buffer, "[ %1.5f, %1.5f, %1.5f ]", ptr[0], ptr[1], ptr[2]);
    break;
  case cSetting_color:
    {
      int color = SettingGet_color(G, set1, set2, index);
      switch (color) {
        case cColorAtomic:
          strcpy(buffer, "atomic");
          break;
        case cColorObject:
          strcpy(buffer, "object");
          break;
        case cColorFront:
          strcpy(buffer, "front");
          break;
        case cColorBack:
          strcpy(buffer, "back");
          break;
        case -1:
          strcpy(buffer, "default");
          break;
        default:
          sptr = ColorGetName(G, color);
          if(sptr)
            return sptr;
              strcpy(buffer, "invalid");
      }
    }
    break;
  case cSetting_string:
    return SettingGet_s(G, set1, set2, index);
  default:
    return NULL;
  }
  return buffer;
}


#ifndef _PYMOL_NOPY
/*========================================================================*/
int SettingSetFromTuple(PyMOLGlobals * G, CSetting * I, int index, PyObject * tuple)
/* must have interpret locked to make this call */
{
  PyObject *value;
  int type;
  int ok = true;
  if(!I)
    I = G->Setting;             /* fall back on global settings */

  /* this data structure has been pre-checked at the python level... */

  type = PyInt_AsLong(PyTuple_GetItem(tuple, 0));
  value = PyTuple_GetItem(tuple, 1);
  switch (type) {
  case cSetting_boolean:
  case cSetting_int:
    SettingSet_i(I, index, PyInt_AsLong(value));
    break;
  case cSetting_float:
    SettingSet_f(I, index, (float) PyFloat_AsDouble(value));
    break;
  case cSetting_float3:
    float tmp[3];
    PyArg_ParseTuple(value, "fff", tmp, tmp + 1, tmp + 2);
    SettingSet_3fv(I, index, tmp);
    break;
  case cSetting_color:
    SettingSet_color(I, index, PyString_AsString(value));
    break;
  case cSetting_string:
    SettingSet_s(I, index, PyString_AsString(value));
    break;
  default:
    ok = false;
    break;
  }
  return (ok);
}
#endif

/*========================================================================*/
int SettingStringToTypedValue(PyMOLGlobals * G, int index, const char *st, int *type,
                              int *value)
{
  int ok = true;
  int newvalue ;
  float newfvalue;
  /* this data structure has been pre-checked at the python level... */

  *type = SettingGetType(G, index);

  switch (*type) {
  case cSetting_boolean:
    if((!*st) || (*st == '0') || (*st == 'F') || WordMatchExact(G, st, "on", true)
       || WordMatchExact(G, st, "false", true)){
          newvalue = 0;
      } else {
          newvalue = 1;
      }
      if (newvalue != *value){
          *value = newvalue;
      } else {
          ok = false;
      }
    break;
  case cSetting_int:
    if(sscanf(st, "%d", &newvalue) != 1){
        ok = false;
    } else if (newvalue!=*value){
        *value = newvalue;
    } else {
        ok = false;
    }
    break;
  case cSetting_float:
    if(sscanf(st, "%f", &newfvalue) != 1){
        ok = false;
    } else if (newfvalue != *((float *) value)){
        *(float*)value = newfvalue;
    } else {
        ok = false;
    }
    break;
  case cSetting_color:
    {
      int color_index = ColorGetIndex(G, st);
      if (*(value) != color_index){
          *(value) = color_index;
      } else {
          ok = false;
      }
    }
    break;
  default:
    ok = false;
    break;
  }
  return (ok);
}

int SettingSetFromString(PyMOLGlobals * G, CSetting * I, int index, const char *st)
{
  int type;
  int ok = true;
  if(!I)
    I = G->Setting;             /* fall back on global settings */

  /* this data structure has been pre-checked at the python level... */

  type = SettingGetType(G, index);

  switch (type) {
  case cSetting_boolean:
    if((!*st) || (*st == '0') || (*st == 'F') || WordMatchExact(G, st, "on", true)
       || WordMatchExact(G, st, "false", true))
      SettingSet_b(I, index, 0);
    else
      SettingSet_b(I, index, 1);
    break;
  case cSetting_int:
    {
      int tmp;
      if(sscanf(st, "%d", &tmp) == 1)
        SettingSet_i(I, index, tmp);
      else
        ok = false;
    }
    break;
  case cSetting_float:
    {
      float tmp;
      if(sscanf(st, "%f", &tmp) == 1)
        SettingSet_f(I, index, tmp);
      else
        ok = false;
    }
    break;
  case cSetting_float3:
    {
      float tmp[3];
      if(sscanf(st, "%f%f%f", tmp, tmp + 1, tmp + 2) == 3)
        SettingSet_3fv(I, index, tmp);
      else
        ok = false;
    }
    break;
  case cSetting_color:
    SettingSet_color(I, index, st);
    break;
  case cSetting_string:
    SettingSet_s(I, index, st);
    break;
  default:
    ok = false;
    break;
  }
  return (ok);
}


/*========================================================================*/
#ifndef _PYMOL_NOPY
/*
 * Warning: Returns colors as (fff) tuple instead of color index
 */
PyObject *SettingGetPyObject(PyMOLGlobals * G, const CSetting * set1, const CSetting * set2, int index)
{                               /* assumes blocked python interpeter */
  PyObject *result = NULL;
  const float *ptr;
  int type = SettingGetType(G, index);

  switch (type) {
  case cSetting_boolean:
    result = CPythonVal_New_Boolean(SettingGet_b(G, set1, set2, index));
    break;
  case cSetting_int:
    result = CPythonVal_New_Integer(SettingGet_i(G, set1, set2, index));
    break;
  case cSetting_float:
    result = CPythonVal_New_Float(SettingGet_f(G, set1, set2, index));
    break;
  case cSetting_float3:
    ptr = SettingGet_3fv(G, set1, set2, index);
    result = Py_BuildValue("(fff)", pymol::pretty_f2d(ptr[0]),
        pymol::pretty_f2d(ptr[1]), pymol::pretty_f2d(ptr[2]));
    break;
  case cSetting_color:
    {
      int retcol = SettingGet_color(G, set1, set2, index);
      if (retcol > 0){
	const float *col;
	col = ColorGet(G, retcol);
	result = Py_BuildValue("(fff)", col[0], col[1], col[2]);
      }
    }
    break;
  case cSetting_string:
    result = PyString_FromString(SettingGet_s(G, set1, set2, index));
    break;
  }
  return result;
}

PyObject *SettingGetTuple(PyMOLGlobals * G, const CSetting * set1, const CSetting * set2, int index)
{                               /* assumes blocked python interpeter */
  PyObject *result = NULL;
  const float *ptr;
  int type = SettingGetType(G, index);

  switch (type) {
  case cSetting_boolean:
  case cSetting_int:
  case cSetting_color:
    result = Py_BuildValue("ii", type, SettingGet_i(G, set1, set2, index));
    break;
  case cSetting_float:
    result = Py_BuildValue(
        "if", type, pymol::pretty_f2d(SettingGet_f(G, set1, set2, index)));
    break;
  case cSetting_float3:
    ptr = SettingGet_3fv(G, set1, set2, index);
    result = Py_BuildValue("i(fff)", type, pymol::pretty_f2d(ptr[0]),
        pymol::pretty_f2d(ptr[1]), pymol::pretty_f2d(ptr[2]));
    break;
  case cSetting_string:
    result = Py_BuildValue("is", type, SettingGet_s(G, set1, set2, index));
    break;
  default:
    result = PConvAutoNone(Py_None);
    break;
  }
  return result;
}
#endif


/*========================================================================*/
CSetting *SettingNew(PyMOLGlobals * G)
{
  OOAlloc(G, CSetting);
  SettingInit(G, I);
  return (I);
}


/*========================================================================*/
void SettingPurge(CSetting * I)
{
  if(I) {
    // need to free strings
    for(int index = 0; index < cSetting_INIT; ++index) {
      if (SettingInfo[index].type == cSetting_string) {
        I->info[index].delete_s();
      }
    }

    VLAFreeP(I->info);
    I->size = 0;
  }
}


/*========================================================================*/
void SettingFreeP(CSetting * I)
{
  if(I)
    SettingPurge(I);
  OOFreeP(I);
}


/*========================================================================*/
void SettingInit(PyMOLGlobals * G, CSetting * I)
{
  I->G = G;
  I->size = sizeof(int);        /* insures offset is never zero, except when undef */
  I->info = (SettingRec*) VLAMalloc(cSetting_INIT, sizeof(SettingRec), 5, 1); /* auto-zero */
}


/*========================================================================*/
/*
 * Return false if type is numeric and default value is non-zero.
 */
bool SettingIsDefaultZero(int index)
{
  switch (SettingInfo[index].type) {
    case cSetting_boolean:
    case cSetting_int:
    case cSetting_float:
      if (SettingInfo[index].value.i[0] == 0)
        return true;
      return false;
  }

  return true;
}


/*========================================================================*/
/*
 * Restore the default value from `src` or `SettingInfo`
 */
void SettingRestoreDefault(CSetting * I, int index, const CSetting * src)
{
  // 1) from stored default if provided
  if (src) {
    UtilCopyMem(I->info + index, src->info + index, sizeof(SettingRec));

    // need to properly copy strings
    if (SettingInfo[index].type == cSetting_string && src->info[index].str_) {
      I->info[index].str_ = new std::string(*src->info[index].str_);
    }

    return;
  }

  // 2) from SettingInfo
  auto &rec = SettingInfo[index];

  switch (rec.type) {
    case cSetting_blank:
      break;
    case cSetting_boolean:
    case cSetting_int:
      I->info[index].set_i(rec.value.i[0]);
      break;
    case cSetting_float:
      I->info[index].set_f(rec.value.f[0]);
      break;
    case cSetting_float3:
      I->info[index].set_3f(rec.value.f);
      break;
    case cSetting_string:
      I->info[index].delete_s();
      break;
    case cSetting_color:
      SettingSet_color(I, index, rec.value.s);
      break;
    default:
      // coding error
      printf(" ERROR: unknown type\n");
  };

  I->info[index].defined = false;
}


/*========================================================================*/
int SettingUnset(CSetting * I, int index)
{
  if(I) {
    SettingRec *sr = I->info + index;
    if (!sr->defined) {
      return false;
    }
    sr->defined = false;
    sr->changed = true;
  }
  return true;
}


/*========================================================================*/
int SettingGetType(int index)
{
  return (SettingInfo[index].type);
}


/*========================================================================*/
template <>
int SettingGet<int>(int index, const CSetting * I)
{
  PyMOLGlobals *G = I->G;
  int result;
  switch (SettingInfo[index].type) {
  case cSetting_boolean:
  case cSetting_int:
  case cSetting_color:
    result = I->info[index].int_;
    break;
  case cSetting_float:
    result = (int) I->info[index].float_;
    break;
  default:
    PRINTFB(G, FB_Setting, FB_Errors)
      "Setting-Error: type read mismatch (int) %d\n", index ENDFB(G);
    result = 0;
    break;
  }
  return (result);
}


/*========================================================================*/
template <>
bool SettingGet<bool>(int index, const CSetting * I)
{
  PyMOLGlobals *G = I->G;
  switch (SettingInfo[index].type) {
  case cSetting_boolean:
  case cSetting_int:
  case cSetting_float:
    return I->info[index].int_ != 0;
  default:
    PRINTFB(G, FB_Setting, FB_Errors)
      "Setting-Error: type read mismatch (boolean) %d\n", index ENDFB(G);
    return false;
  }
}


/*========================================================================*/
template <>
float SettingGet<float>(int index, const CSetting * I)
{
  float result;
  PyMOLGlobals *G = I->G;
  switch (SettingInfo[index].type) {
  case cSetting_color:
    PRINTFB(G, FB_Setting, FB_Warnings)
      " Setting-Warning: type read mismatch (float/color) %d\n", index ENDFB(G);
  case cSetting_boolean:
  case cSetting_int:
    result = (float) I->info[index].int_;
    break;
  case cSetting_float:
    result = I->info[index].float_;
    break;
  default:
    PRINTFB(G, FB_Setting, FB_Errors)
      "Setting-Error: type read mismatch (float) %d\n", index ENDFB(G);
    result = 0.0F;
  }
  return (result);
}


/*========================================================================*/
template <>
const char * SettingGet<const char *>(int index, const CSetting * I)
{
  const char *result;
  PyMOLGlobals *G = I->G;
  switch (SettingInfo[index].type) {
  case cSetting_string:
    if(I->info[index].str_) {
      result = I->info[index].str_->c_str();
    } else {
      result = SettingInfo[index].value.s;
    }
    break;
  default:
    PRINTFB(G, FB_Setting, FB_Errors)
      "Setting-Error: type read mismatch (string) %d\n", index ENDFB(G);
    result = NULL;
  }
  return (char*) result;
}


/*========================================================================*/
template <>
const float * SettingGet<const float *>(int index, const CSetting * I)
{
  if (SettingInfo[index].type != cSetting_float3) {
    PyMOLGlobals *G = I->G;
    PRINTFB(G, FB_Setting, FB_Errors)
      " Setting-Error: type read mismatch (float3) %d\n", index ENDFB(G);
    return NULL;
  }
  return I->info[index].float3_;
}


/*========================================================================*/
int SettingSet_i(CSetting * I, int index, int value)
{
  int ok = true;
  if(I) {
    PyMOLGlobals *G = I->G;
    {
      int setting_type = SettingInfo[index].type;
      switch (setting_type) {
      case cSetting_boolean:
      case cSetting_int:
      case cSetting_color:
        I->info[index].set_i(value);
	break;
      case cSetting_float:
        I->info[index].set_f((float) value);
	break;
      default:
	PRINTFB(G, FB_Setting, FB_Errors)
	  "Setting-Error: type set mismatch (integer) %d\n", index ENDFB(G);
	ok = false;
      }
    }
  } else {
    ok = false;
  }
  return (ok);
}


/*========================================================================*/
static int SettingSet_color_from_3f(CSetting * I, int index, const float * vector)
{
  int color_index;
  float vals[3];
  copy3f(vector, vals);
  clamp3f(vals);
  color_index = Color3fToInt(I->G, vals);
  return SettingSet_i(I, index, color_index);
}

int SettingSet_color(CSetting * I, int index, const char *value)
{
  int ok = true;
  int color_index;
  if(I) {
    PyMOLGlobals *G = I->G;
    color_index = ColorGetIndex(G, value);
    if((color_index == -1) && (strcmp(value, "-1") &&
                               strcmp(value, "-2") &&
                               strcmp(value, "-3") &&
                               strcmp(value, "-4") &&
                               strcmp(value, "-5") && strcmp(value, "default"))) {
      float vals[3];
      ok = ParseFloat3List(value, vals);
      if (ok){
	clamp3f(vals);
	color_index = cColor_TRGB_Bits | 
	  ((int) (255 * vals[0] + 0.49999F)) << 16 | 
	  ((int) (255 * vals[1] + 0.49999F)) << 8 | 
	  ((int) (255 * vals[2] + 0.49999F));
      } else {
	PRINTFB(G, FB_Setting, FB_Errors)
	  "Setting-Error: unknown color '%s'\n", value ENDFB(G);
      }
    }
    if (ok){
      SettingSet_i(I, index, color_index);
    }
  }
  return (ok);
}


/*========================================================================*/
int SettingSet_f(CSetting * I, int index, float value)
{
  int ok = true;
  if(I) {
    PyMOLGlobals *G = I->G;
    {
      int setting_type = SettingInfo[index].type;
      switch (setting_type) {
      case cSetting_boolean:
      case cSetting_int:
      case cSetting_color:
        I->info[index].set_i((int) value);
	break;
      case cSetting_float:
        I->info[index].set_f(value);
	break;
      default:
	PRINTFB(G, FB_Setting, FB_Errors)
	  "Setting-Error: type set mismatch (float) %d\n", index ENDFB(G);
	ok = false;
      }
    }
  } else {
    ok = false;
  }
  return (ok);
}


/*========================================================================*/
int SettingSet_s(CSetting * I, int index, const char *value)
{
  int ok = true;
  if(I) {
    PyMOLGlobals *G = I->G;
    {
      int setting_type = SettingInfo[index].type;
      switch (setting_type) {
      case cSetting_string:
        I->info[index].set_s(value);
	break;
      case cSetting_color:
        return SettingSet_color(I, index, value);
      default:
	PRINTFB(G, FB_Setting, FB_Errors)
	  "Setting-Error: type set mismatch (string) %d\n", index ENDFB(G);
	ok = false;
      }
    }
  } else {
    ok = false;
  }
  return (ok);
}


/*========================================================================*/
int SettingSet_3fv(CSetting * I, int index, const float *vector)
{
  switch (SettingInfo[index].type) {
  case cSetting_float3:
    I->info[index].set_3f(vector);
    return true;
  case cSetting_color:
    return SettingSet_color_from_3f(I, index, vector);
  default:
    PyMOLGlobals *G = I->G;
    PRINTFB(G, FB_Setting, FB_Errors)
      "Setting-Error: type set mismatch (float3) %d\n", index ENDFB(G);
    return false;
  }
}


/*========================================================================*/
int SettingGetIndex(PyMOLGlobals * G, const char *name)
{
  OVreturn_word result = get_setting_id(G->PyMOL, name);

  if (OVreturn_IS_OK(result))
    return result.word;

  return -1;
}


/*========================================================================*/
int SettingGetName(PyMOLGlobals * G, int index, SettingName name)
{
  UtilNCopy(name, SettingInfo[index].name, sizeof(SettingName));
  return (name[0] != 0);
}

/*========================================================================*/
const char * SettingGetName(int index)
{
  return SettingInfo[index].name;
}

/*========================================================================*/
void SettingGenerateSideEffects(PyMOLGlobals * G, int index, const char *sele, int state, int quiet)
{
  const char *inv_sele = (sele && sele[0]) ? sele : cKeywordAll;
  auto &rec = SettingInfo[index];

  if (rec.level == cSettingLevel_unused) {
    const char * name = rec.name;

    if (!quiet && name && name[0]){
      PRINTFB(G, FB_Setting, FB_Warnings)
        " Setting-Warning: '%s' is no longer used\n", name
        ENDFB(G);
    }

    return;
  }

  // range check for int (global only)
  if (rec.type == cSetting_int && rec.hasMinMax() && !(sele && sele[0])) {
    int value = SettingGetGlobal_i(G, index);
    bool clamped = true;

    if (value < rec.value.i[1]) {
      value = rec.value.i[1];
    } else if (value > rec.value.i[2]) {
      value = rec.value.i[2];
    } else {
      clamped = false;
    }

    if (clamped) {
      PRINTFB(G, FB_Setting, FB_Warnings)
        " Setting-Warning: %s range = [%d,%d]; setting to %d.\n",
        rec.name, rec.value.i[1], rec.value.i[2], value ENDFB(G);
      SettingSetGlobal_i(G, index, value);
    }
  }

  switch (index) {
  case cSetting_stereo:
    SceneUpdateStereo(G);
    G->ShaderMgr->Set_Reload_Bits(RELOAD_VARIABLES);
    break;
  case cSetting_pick_surface:
    if (SettingGetGlobal_b(G, cSetting_use_shaders)){
      SceneInvalidatePicking(G); // right now, when pick_surface is off, wipes each CGO's pickColor array
    }
    ExecutiveInvalidateRep(G, inv_sele, cRepSurface, cRepInvColor);
    break;
  case cSetting_pickable:
    ExecutiveInvalidateRep(G, inv_sele, cRepAll, cRepInvAll);
    SceneChanged(G);
    break;
  case cSetting_grid_mode:
    if (!SettingGetGlobal_i(G, cSetting_grid_mode))
      G->ShaderMgr->ResetUniformSet();
  case cSetting_grid_slot:
    ExecutiveInvalidateGroups(G, false);
    SceneChanged(G);
    break;
  case cSetting_grid_max:
    SceneChanged(G);
    break;
  case cSetting_defer_builds_mode:
    ExecutiveRebuildAll(G);
    break;
  case cSetting_seq_view:
  case cSetting_seq_view_label_spacing:
  case cSetting_seq_view_label_mode:
  case cSetting_seq_view_label_start:
  case cSetting_seq_view_format:
  case cSetting_seq_view_color:
  case cSetting_seq_view_unaligned_mode:
  case cSetting_seq_view_gap_mode:
    SeqChanged(G);
    break;
  case cSetting_seq_view_fill_color:
  case cSetting_seq_view_fill_char:
  case cSetting_seq_view_label_color:
    OrthoDirty(G);
    break;
  case cSetting_show_frame_rate:
    OrthoDirty(G);
    break;
  case cSetting_group_full_member_names:
  case cSetting_group_arrow_prefix:
    OrthoDirty(G);
    break;
  case cSetting_static_singletons:
    SeqChanged(G);
    break;
  case cSetting_seq_view_location:
    PParse(G, "cmd.viewport(-1,-1)");
    SeqChanged(G);
    break;
  case cSetting_seq_view_overlay:
    PParse(G, "cmd.viewport(-1,-1)");
    break;
  case cSetting_stereo_mode:
  case cSetting_anaglyph_mode:
    G->ShaderMgr->Set_Reload_Bits(RELOAD_VARIABLES);
    SceneUpdateStereoMode(G);
    OrthoInvalidateDoDraw(G);
    OrthoDirty(G);
    PyMOL_NeedRedisplay(G->PyMOL);
    break;
  case cSetting_precomputed_lighting:
    G->ShaderMgr->Set_Reload_Bits(RELOAD_VARIABLES);
  case cSetting_light_count:
  case cSetting_spec_count:
    G->ShaderMgr->Set_Reload_Bits(RELOAD_CALLCOMPUTELIGHTING);
  case cSetting_dot_lighting:
  case cSetting_mesh_lighting:
  case cSetting_cgo_lighting:
  case cSetting_field_of_view:
  case cSetting_fog_start:
  case cSetting_two_sided_lighting:
  case cSetting_transparency_global_sort:
  case cSetting_dot_normals:
  case cSetting_mesh_normals:
    SceneInvalidate(G);
    break;
  case cSetting_spec_power:
    if (!quiet){
      PRINTFB(G, FB_Setting, FB_Debugging)
	"Setting-Details: spec_power is depreciated in PyMOL 1.5.  This option will not work in future versions. Please set shininess to set the specular exponent for movable light sources.\n"
	ENDFB(G);
    }
    SceneInvalidate(G);
    break;
  case cSetting_light:
  case cSetting_light2:
  case cSetting_light3:
  case cSetting_light4:
  case cSetting_light5:
  case cSetting_light6:
  case cSetting_light7:
  case cSetting_reflect:
  case cSetting_direct:
  case cSetting_ambient:
  case cSetting_specular:
  case cSetting_specular_intensity:
  case cSetting_shininess:
  case cSetting_spec_reflect:
  case cSetting_spec_direct:
  case cSetting_spec_direct_power:
  case cSetting_power:
  case cSetting_reflect_power:
    if (SettingGetGlobal_b(G, cSetting_precomputed_lighting))
      G->ShaderMgr->Set_Reload_Bits(RELOAD_CALLCOMPUTELIGHTING);
      SceneInvalidate(G);
    break;
  case cSetting_use_display_lists:
  case cSetting_simplify_display_lists:
  case cSetting_excl_display_lists_shaders:
    if (!quiet){
      PRINTFB(G, FB_Setting, FB_Debugging)
	"Setting-Details: display lists were depreciated in PyMOL 1.7.x.  The settings use_display_lists, simplify_display_lists, and excl_display_lists_shaders no longer work.\n"
	ENDFB(G);
    }
  case cSetting_use_geometry_shaders:
    if (SettingGetGlobal_i(G, cSetting_use_geometry_shaders) &&
        G->ShaderMgr->IsConfigured() && !G->ShaderMgr->GeometryShadersPresent()) {
      SettingSet_b(G->Setting, cSetting_use_geometry_shaders, 0);
      if (!quiet){
        PRINTFB(G, FB_Setting, FB_Warnings)
          "Setting-Error: geometry shaders not available\n" ENDFB(G);
      }
      return;
    }

    G->ShaderMgr->Set_Reload_Bits(RELOAD_VARIABLES);
    ExecutiveInvalidateRep(G, inv_sele, cRepLabel, cRepInvRep);
    {
      // check if lines need to be invalidated
      bool line_as_cylinders = SettingGetGlobal_b(G, cSetting_use_shaders) &&
                               SettingGetGlobal_b(G, cSetting_render_as_cylinders) &&
                               SettingGetGlobal_b(G, cSetting_line_as_cylinders);
      if (!line_as_cylinders && !SettingGetGlobal_b(G, cSetting_trilines)){
        ExecutiveInvalidateRep(G, inv_sele, cRepLine, cRepInvRep);
      }
    }
    break;
  case cSetting_shaders_from_disk:
    G->ShaderMgr->Set_Reload_Bits(RELOAD_ALL_SHADERS);
    SceneInvalidate(G);
    break;
  case cSetting_use_shaders:
    {
      short changed = 0;
      if (SettingGetGlobal_b(G, cSetting_use_shaders)){
	if (SettingCheckUseShaders(G->Setting, quiet)){
	  return;
	}
      }
      SceneInvalidate(G);
      if (SettingGetGlobal_b(G, cSetting_sphere_use_shader)){
	ExecutiveInvalidateRep(G, inv_sele, cRepSphere, cRepInvRep);
	changed = 1;
      }
      if (SettingGetGlobal_b(G, cSetting_ribbon_use_shader)){
	ExecutiveInvalidateRep(G, inv_sele, cRepRibbon, cRepInvRep);
	changed = 1;
      }
      if (SettingGetGlobal_b(G, cSetting_nonbonded_use_shader)){
	ExecutiveInvalidateRep(G, inv_sele, cRepNonbonded, cRepInvRep);
	changed = 1;
      }
      if (SettingGetGlobal_i(G, cSetting_nb_spheres_use_shader)){
	changed = 1;
      }
      if (SettingGetGlobal_b(G, cSetting_dash_use_shader)){
	ExecutiveInvalidateRep(G, inv_sele, cRepAngle, cRepInvRep);
	ExecutiveInvalidateRep(G, inv_sele, cRepDihedral, cRepInvRep);
	ExecutiveInvalidateRep(G, inv_sele, cRepDash, cRepInvRep);
	changed = 1;
      }
      if (SettingGetGlobal_b(G, cSetting_line_use_shader)){
	ExecutiveInvalidateRep(G, inv_sele, cRepLine, cRepInvRep);
	changed = 1;
      }
      if (SettingGetGlobal_b(G, cSetting_cartoon_use_shader)){
	ExecutiveInvalidateRep(G, inv_sele, cRepCartoon, cRepInvRep);
	changed = 1;
      }
      if (SettingGetGlobal_b(G, cSetting_cgo_use_shader) ||
          SettingGetGlobal_b(G, cSetting_stick_as_cylinders)){
	ExecutiveInvalidateRep(G, inv_sele, cRepCGO, cRepInvRep);
	changed = 1;
      }
      if (SettingGetGlobal_b(G, cSetting_stick_use_shader) ||
          SettingGetGlobal_b(G, cSetting_stick_as_cylinders) ||
	  (SettingGetGlobal_b(G, cSetting_stick_ball) && 
	   SettingGetGlobal_b(G, cSetting_valence))){
	ExecutiveInvalidateRep(G, inv_sele, cRepCyl, cRepInvRep);
	changed = 1;
      }
      if (SettingGetGlobal_b(G, cSetting_surface_use_shader) ||
	  SettingGetGlobal_b(G, cSetting_dot_use_shader) || 
	  SettingGetGlobal_b(G, cSetting_mesh_use_shader)){
	changed = 1;
      }
      if (changed){
	SceneChanged(G);
      }
    }
    break;
  case cSetting_stereo_shift:
  case cSetting_stereo_angle:
  case cSetting_stereo_dynamic_strength:
    SceneInvalidate(G);
    break;
  case cSetting_scene_buttons:
  case cSetting_scene_buttons_mode:
    SceneInvalidate(G);
    OrthoInvalidateDoDraw(G);
    break;
  case cSetting_dash_round_ends:
  case cSetting_dash_color:
    if (SettingGetGlobal_b(G, cSetting_use_shaders) && SettingGetGlobal_b(G, cSetting_dash_use_shader)){
      ExecutiveInvalidateRep(G, "all", cRepDash, cRepInvRep);
    }
    SceneInvalidate(G);
    break;
  case cSetting_angle_color:
    if (SettingGetGlobal_b(G, cSetting_use_shaders) && SettingGetGlobal_b(G, cSetting_dash_use_shader)){
      ExecutiveInvalidateRep(G, "all", cRepAngle, cRepInvRep);
    }
    SceneInvalidate(G);
    break;
  case cSetting_dihedral_color:
    if (SettingGetGlobal_b(G, cSetting_use_shaders) && SettingGetGlobal_b(G, cSetting_dash_use_shader)){
      ExecutiveInvalidateRep(G, "all", cRepDihedral, cRepInvRep);
    }
    SceneInvalidate(G);
    break;
  case cSetting_mouse_selection_mode:
    OrthoDirty(G);
    break;
  case cSetting_internal_gui_control_size:
    WizardRefresh(G);
    OrthoDirty(G);
    break;
  case cSetting_hide_underscore_names:
    ExecutiveInvalidateGroups(G, false);
    OrthoDirty(G);
    break;
  case cSetting_gradient_spacing:
  case cSetting_gradient_max_length:
  case cSetting_gradient_min_length:
  case cSetting_gradient_normal_min_dot:
  case cSetting_gradient_step_size:
  case cSetting_gradient_min_slope:
  case cSetting_gradient_symmetry:
    ExecutiveInvalidateRep(G, inv_sele, cRepMesh, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_min_mesh_spacing:
  case cSetting_mesh_grid_max:
  case cSetting_mesh_cutoff:
  case cSetting_mesh_carve_state:
  case cSetting_mesh_carve_cutoff:
  case cSetting_mesh_carve_selection:
  case cSetting_mesh_clear_state:
  case cSetting_mesh_clear_cutoff:
  case cSetting_mesh_clear_selection:
  case cSetting_mesh_mode:
  case cSetting_mesh_type:
  case cSetting_mesh_solvent:
  case cSetting_mesh_quality:
  case cSetting_mesh_skip:
    ExecutiveInvalidateRep(G, inv_sele, cRepMesh, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_valence:
  case cSetting_valence_mode:
  case cSetting_valence_size:
  case cSetting_valence_zero_mode:
  case cSetting_valence_zero_scale:
  case cSetting_half_bonds:
  case cSetting_line_stick_helper:
  case cSetting_hide_long_bonds:
    ExecutiveInvalidateRep(G, inv_sele, cRepLine, cRepInvRep);
    ExecutiveInvalidateRep(G, inv_sele, cRepCyl, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_stick_transparency:
  case cSetting_stick_debug:
  case cSetting_stick_round_nub:
  case cSetting_stick_as_cylinders:
  case cSetting_stick_good_geometry:
    ExecutiveInvalidateRep(G, inv_sele, cRepCyl, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_line_use_shader:
    if (SettingGetGlobal_b(G, cSetting_use_shaders)){
      ExecutiveInvalidateRep(G, inv_sele, cRepRibbon, cRepInvRep);
      SceneChanged(G);
    }
    break;
  case cSetting_ribbon_use_shader:
    if (SettingGetGlobal_b(G, cSetting_use_shaders)){
      ExecutiveInvalidateRep(G, inv_sele, cRepRibbon, cRepInvRep);
      SceneChanged(G);
    }
    break;
  case cSetting_dot_as_spheres:
    SceneInvalidate(G);
    SceneChanged(G);
    break;
  case cSetting_dot_use_shader:
    if (SettingGetGlobal_b(G, cSetting_use_shaders)){
      ExecutiveInvalidateRep(G, inv_sele, cRepDot, cRepInvRep);
      SceneChanged(G);
    }
    break;
  case cSetting_nonbonded_use_shader:
    if (SettingGetGlobal_b(G, cSetting_use_shaders)){
      ExecutiveInvalidateRep(G, inv_sele, cRepNonbonded, cRepInvRep);
      SceneChanged(G);
    }
    break;
  case cSetting_nb_spheres_size:
    ExecutiveInvalidateRep(G, inv_sele, cRepNonbondedSphere, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_nb_spheres_use_shader:
    if (SettingGetGlobal_b(G, cSetting_use_shaders)){
      SceneChanged(G);
    }
    break;
  case cSetting_render_as_cylinders:
    ExecutiveInvalidateRep(G, inv_sele, cRepCyl, cRepInvRep);
  case cSetting_mesh_as_cylinders:
  case cSetting_line_as_cylinders:
  case cSetting_ribbon_as_cylinders:
  case cSetting_dash_as_cylinders:
    if (index == cSetting_dash_as_cylinders) {
      ExecutiveInvalidateRep(G, inv_sele, cRepAngle, cRepInvRep);
      ExecutiveInvalidateRep(G, inv_sele, cRepDihedral, cRepInvRep);
      ExecutiveInvalidateRep(G, inv_sele, cRepDash, cRepInvRep);
    }
  case cSetting_nonbonded_as_cylinders:
  case cSetting_alignment_as_cylinders:
  case cSetting_cartoon_nucleic_acid_as_cylinders:
    if (SettingGetGlobal_b(G, cSetting_render_as_cylinders)){
      if (G->ShaderMgr->shaders_present && !G->ShaderMgr->ShaderPrgExists("cylinder")){
	SettingSet_b(G->Setting, cSetting_render_as_cylinders, 0);
	if (!quiet){
	  PRINTFB(G, FB_Setting, FB_Warnings)
	    "Setting-Error: render_as_cylinders cannot be set when the Cylinder Shader is not available, setting render_as_cylinder back to false\n"
	    ENDFB(G);
	}
	return;
      }
      switch (index){
      case cSetting_render_as_cylinders:
      case cSetting_cartoon_nucleic_acid_as_cylinders:
      case cSetting_use_shaders:
	if (SettingGetGlobal_b(G, cSetting_use_shaders) && SettingGetGlobal_b(G, cSetting_render_as_cylinders)){
	  ExecutiveInvalidateRep(G, inv_sele, cRepCartoon, cRepInvRep);
	}
      }
      SceneChanged(G);
    }
    break;
  case cSetting_mesh_use_shader:
    if (SettingGetGlobal_b(G, cSetting_use_shaders)){
      SceneChanged(G);
    }
    break;
  case cSetting_slice_height_scale:
  case cSetting_slice_height_map:
  case cSetting_slice_grid:
  case cSetting_slice_dynamic_grid:
  case cSetting_slice_dynamic_grid_resolution:
    ExecutiveInvalidateRep(G, inv_sele, cRepSlice, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_label_font_id:
  case cSetting_label_size:
    ExecutiveInvalidateRep(G, inv_sele, cRepLabel, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_retain_order:
  case cSetting_pdb_hetatm_sort:
  case cSetting_pdb_insertions_go_first:
    ExecutiveSort(G, inv_sele);
    break;
  case cSetting_roving_lines:
  case cSetting_roving_sticks:
  case cSetting_roving_spheres:
  case cSetting_roving_labels:
  case cSetting_roving_selection:
  case cSetting_roving_ribbon:
  case cSetting_roving_cartoon:
  case cSetting_roving_polar_contacts:
  case cSetting_roving_polar_cutoff:
  case cSetting_roving_nonbonded:
  case cSetting_roving_nb_spheres:
  case cSetting_roving_map1_level:
  case cSetting_roving_map2_level:
  case cSetting_roving_map3_level:
  case cSetting_roving_map1_name:
  case cSetting_roving_map2_name:
  case cSetting_roving_map3_name:
  case cSetting_roving_isosurface:
  case cSetting_roving_isomesh:
    SceneRovingChanged(G);
    break;
  case cSetting_roving_byres:
  case cSetting_roving_detail:
    SceneRovingDirty(G);
    break;
  case cSetting_dash_transparency:
  case cSetting_dash_length:
  case cSetting_dash_gap:
  case cSetting_dash_radius:
  case cSetting_dash_width:
  case cSetting_angle_size:
  case cSetting_label_digits:
  case cSetting_label_distance_digits:
  case cSetting_label_angle_digits:
  case cSetting_label_dihedral_digits:
  case cSetting_angle_label_position:
  case cSetting_dihedral_size:
  case cSetting_dihedral_label_position:
    ExecutiveRebuildAllObjectDist(G);
    SceneChanged(G);
    break;
  case cSetting_button_mode:
    EditorMouseInvalid(G);
    OrthoDirty(G);
    break;
  case cSetting_stick_radius:
  case cSetting_stick_h_scale:
    ExecutiveInvalidateRep(G, inv_sele, cRepCyl, cRepInvRep);
    ExecutiveInvalidateRep(G, inv_sele, cRepCartoon, cRepInvRep);       /* base width */
    SceneChanged(G);
    break;
  case cSetting_nb_spheres_quality:
    {
      ExecutiveInvalidateRep(G, inv_sele, cRepNonbondedSphere, cRepInvRep);
      SceneChanged(G);
    }
  case cSetting_cgo_sphere_quality:
  case cSetting_cgo_debug:
    {
      ExecutiveInvalidateRep(G, inv_sele, cRepCGO, cRepInvRep);
      SceneChanged(G);
      break;
    }
  case cSetting_stick_quality:
  case cSetting_stick_ball:
  case cSetting_stick_nub:
  case cSetting_stick_ball_ratio:
  case cSetting_stick_ball_color:
  case cSetting_stick_fixed_radius:
  case cSetting_stick_valence_scale:
  case cSetting_stick_overlap:
  case cSetting_stick_color:
  case cSetting_stick_use_shader:
    ExecutiveInvalidateRep(G, inv_sele, cRepCyl, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_clamp_colors:
  case cSetting_ramp_blend_nearby_colors:
    ExecutiveInvalidateRep(G, inv_sele, cRepAll, cRepInvColor);
    SceneChanged(G);
    break;
  case cSetting_label_color:
  case cSetting_label_outline_color:
  case cSetting_label_position:
    ExecutiveRebuildAllObjectDist(G);
    ExecutiveInvalidateRep(G, inv_sele, cRepLabel, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_cartoon_color:
    ExecutiveInvalidateRep(G, inv_sele, cRepCartoon, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_ribbon_color:
    ExecutiveInvalidateRep(G, inv_sele, cRepRibbon, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_cgo_line_width:
  case cSetting_line_width:    
    {
      GLfloat *range = G->ShaderMgr->GetLineWidthRange();
      float line_width = SettingGetGlobal_f(G, index);
      {
	if (line_width <= 0.f){
	  PRINTFB(G, FB_Setting, FB_Warnings)
	    " Setting-Warning: %s is set incorrectly (%f), setting to 1\n",
            rec.name, line_width ENDFB(G);
	  SettingSetGlobal_f(G, index, 1.f);
	} else if (G->HaveGUI && range[1] > 0.f && line_width > range[1]) {
	  PRINTFB(G, FB_Setting, FB_Warnings)
            " Setting-Warning: %s is out of range of the graphics card's "
            "capability (range: %f-%f), lines might not be rendered correctly\n",
            rec.name, range[0], range[1] ENDFB(G);
	}
      }
    }
  case cSetting_line_color:
  case cSetting_line_radius:
    ExecutiveInvalidateRep(G, inv_sele, cRepLine, cRepInvRep);
    ExecutiveInvalidateRep(G, inv_sele, cRepNonbonded, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_scenes_changed:
    {
      int scene_buttons = SettingGetGlobal_i(G, cSetting_scene_buttons);
      if (scene_buttons)
	OrthoInvalidateDoDraw(G);
    }
    break;
  case cSetting_button_mode_name:
    {
      int internal_gui_mode = SettingGetGlobal_i(G, cSetting_internal_gui_mode);
      if (internal_gui_mode){
	OrthoInvalidateDoDraw(G);
      }
    }
      break;
  case cSetting_scene_current_name:
    SceneRestartFrameTimer(G);
    break;
  case cSetting_mesh_width:
    ExecutiveInvalidateRep(G, inv_sele, cRepMesh, cRepInvColor);
    SceneChanged(G);
    break;
  case cSetting_ellipsoid_probability:
  case cSetting_ellipsoid_scale:
  case cSetting_ellipsoid_color:
  case cSetting_ellipsoid_transparency:
    ExecutiveInvalidateRep(G, inv_sele, cRepEllipsoid, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_ellipsoid_quality:
  case cSetting_cgo_ellipsoid_quality:
    ExecutiveInvalidateRep(G, inv_sele, cRepCGO, cRepInvRep);
    ExecutiveInvalidateRep(G, inv_sele, cRepEllipsoid, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_mesh_color:
  case cSetting_mesh_negative_color:
    ExecutiveInvalidateRep(G, inv_sele, cRepMesh, cRepInvColor);
    SceneChanged(G);
    break;
  case cSetting_ray_color_ramps:
    ExecutiveInvalidateRep(G, inv_sele, cRepAll, cRepInvColor);
    SceneChanged(G);
    break;
  case cSetting_sphere_mode:
    ExecutiveInvalidateRep(G, inv_sele, cRepSphere, cRepInvRep);
    SceneInvalidate(G);
    break;
  case cSetting_cull_spheres:
  case cSetting_sphere_scale:
  case cSetting_sphere_transparency:
  case cSetting_sphere_solvent:
  case cSetting_sphere_point_max_size:
  case cSetting_sphere_point_size:
  case cSetting_sphere_use_shader:
    ExecutiveInvalidateRep(G, inv_sele, cRepSphere, cRepInvRep);
    SceneInvalidate(G);
    break;
  case cSetting_sphere_quality:
    ExecutiveInvalidateRep(G, inv_sele, cRepCyl, cRepInvRep);
    ExecutiveInvalidateRep(G, inv_sele, cRepNonbondedSphere, cRepInvRep);
    ExecutiveInvalidateRep(G, inv_sele, cRepSphere, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_nonbonded_size:
  case cSetting_nonbonded_transparency:
    ExecutiveInvalidateRep(G, inv_sele, cRepNonbonded, cRepInvRep);
    ExecutiveInvalidateRep(G, inv_sele, cRepNonbondedSphere, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_mesh_radius:
    ExecutiveInvalidateRep(G, inv_sele, cRepMesh, cRepInvColor);
    SceneChanged(G);
    break;
  case cSetting_ambient_occlusion_scale:
    SceneChanged(G);
    break;
  case cSetting_ambient_occlusion_mode:
    ExecutiveInvalidateRep(G, inv_sele, cRepSurface, cRepInvColor);
    switch (SettingGetGlobal_i(G, cSetting_ambient_occlusion_mode) % 4){
    case 1:
      SettingSetGlobal_i(G, cSetting_ambient_occlusion_smooth, 10);
      SettingSetGlobal_f(G, cSetting_ambient_occlusion_scale, 25.f);
      break;
    case 2:
      SettingSetGlobal_i(G, cSetting_ambient_occlusion_smooth, 5);
      SettingSetGlobal_f(G, cSetting_ambient_occlusion_scale, .9f);
      break;
    case 3:
      SettingSetGlobal_i(G, cSetting_ambient_occlusion_smooth, 5);
      SettingSetGlobal_f(G, cSetting_ambient_occlusion_scale, 1.1f);
      break;
    }
    SceneChanged(G);
    break;
  case cSetting_ambient_occlusion_smooth:
  case cSetting_surface_negative_color:
  case cSetting_surface_color:
  case cSetting_surface_ramp_above_mode:
  case cSetting_transparency:
    ExecutiveInvalidateRep(G, inv_sele, cRepSurface, cRepInvColor);
    SceneChanged(G);
    break;
  case cSetting_dot_color:
    ExecutiveInvalidateRep(G, inv_sele, cRepDot, cRepInvColor);
    SceneChanged(G);
    break;
  case cSetting_sphere_color:
    ExecutiveInvalidateRep(G, inv_sele, cRepSphere, cRepInvColor);
    SceneChanged(G);
    break;

  case cSetting_surface_quality:
  case cSetting_surface_mode:
  case cSetting_surface_normal:
  case cSetting_surface_type:
  case cSetting_surface_carve_state:
  case cSetting_surface_carve_cutoff:
  case cSetting_surface_carve_selection:
  case cSetting_surface_carve_normal_cutoff:
  case cSetting_surface_clear_state:
  case cSetting_surface_clear_cutoff:
  case cSetting_surface_clear_selection:
  case cSetting_surface_trim_cutoff:
  case cSetting_surface_trim_factor:
  case cSetting_surface_circumscribe:
  case cSetting_surface_solvent:
  case cSetting_surface_proximity:
  case cSetting_surface_cavity_mode:   
  case cSetting_surface_cavity_radius:
  case cSetting_surface_cavity_cutoff:   
  case cSetting_cavity_cull:
  case cSetting_surface_smooth_edges:
    ExecutiveInvalidateRep(G, inv_sele, cRepSurface, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_surface_use_shader:
    SceneChanged(G);
    break;
  case cSetting_surface_negative_visible:
    ExecutiveInvalidateRep(G, inv_sele, cRepSurface, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_mesh_negative_visible:
    ExecutiveInvalidateRep(G, inv_sele, cRepMesh, cRepInvAll);
    SceneChanged(G);
    break;
  case cSetting_solvent_radius:
    ExecutiveInvalidateRep(G, inv_sele, cRepSurface, cRepInvRep);
    ExecutiveInvalidateRep(G, inv_sele, cRepMesh, cRepInvRep);
    ExecutiveInvalidateRep(G, inv_sele, cRepDot, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_trace_atoms_mode:
    ExecutiveInvalidateRep(G, inv_sele, cRepRibbon, cRepInvRep);
    ExecutiveInvalidateRep(G, inv_sele, cRepCartoon, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_ribbon_smooth:
  case cSetting_ribbon_power:
  case cSetting_ribbon_power_b:
  case cSetting_ribbon_sampling:
  case cSetting_ribbon_radius:
  case cSetting_ribbon_width:
  case cSetting_ribbon_throw:
  case cSetting_ribbon_trace_atoms:
  case cSetting_ribbon_transparency:
    ExecutiveInvalidateRep(G, inv_sele, cRepRibbon, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_draw_mode:
    ExecutiveInvalidateRep(G, inv_sele, cRepCyl, cRepInvRep);
    ExecutiveInvalidateRep(G, inv_sele, cRepSphere, cRepInvRep);
    break;
  case cSetting_cartoon_side_chain_helper:
  case cSetting_cartoon_nucleic_acid_mode:
    ExecutiveInvalidateRep(G, inv_sele, cRepCartoon, cRepInvRep);
    ExecutiveInvalidateRep(G, inv_sele, cRepLine, cRepInvRep);
    ExecutiveInvalidateRep(G, inv_sele, cRepCyl, cRepInvRep);
    ExecutiveInvalidateRep(G, inv_sele, cRepSphere, cRepInvRep);
    ExecutiveInvalidateRep(G, inv_sele, cRepEllipsoid, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_ribbon_side_chain_helper:
  case cSetting_ribbon_nucleic_acid_mode:
    ExecutiveInvalidateRep(G, inv_sele, cRepRibbon, cRepInvRep);
    ExecutiveInvalidateRep(G, inv_sele, cRepLine, cRepInvRep);
    ExecutiveInvalidateRep(G, inv_sele, cRepCyl, cRepInvRep);
    ExecutiveInvalidateRep(G, inv_sele, cRepSphere, cRepInvRep);
    ExecutiveInvalidateRep(G, inv_sele, cRepEllipsoid, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_ray_trace_mode:        /* affects loop quality */
    G->ShaderMgr->Set_Reload_Bits(RELOAD_VARIABLES);
  case cSetting_cartoon_transparency:
  case cSetting_cartoon_ring_transparency:
  case cSetting_cartoon_trace_atoms:
  case cSetting_cartoon_refine:
  case cSetting_cartoon_nucleic_acid_color:
  case cSetting_cartoon_ring_mode:
  case cSetting_cartoon_ring_finder:
  case cSetting_cartoon_ring_width:
  case cSetting_cartoon_ring_color:
  case cSetting_cartoon_ladder_mode:
  case cSetting_cartoon_ladder_radius:
  case cSetting_cartoon_ladder_color:
  case cSetting_cartoon_sampling:
  case cSetting_cartoon_loop_quality:
  case cSetting_cartoon_loop_radius:
  case cSetting_cartoon_loop_cap:
  case cSetting_cartoon_tube_quality:
  case cSetting_cartoon_tube_radius:
  case cSetting_cartoon_tube_cap:
  case cSetting_cartoon_putty_quality:
  case cSetting_cartoon_putty_radius:
  case cSetting_cartoon_putty_range:
  case cSetting_cartoon_putty_scale_min:
  case cSetting_cartoon_putty_scale_max:
  case cSetting_cartoon_putty_scale_power:
  case cSetting_cartoon_putty_transform:
  case cSetting_cartoon_power:
  case cSetting_cartoon_power_b:
  case cSetting_cartoon_ring_radius:
  case cSetting_cartoon_rect_length:
  case cSetting_cartoon_rect_width:
  case cSetting_cartoon_oval_length:
  case cSetting_cartoon_oval_width:
  case cSetting_cartoon_oval_quality:
  case cSetting_cartoon_round_helices:
  case cSetting_cartoon_flat_sheets:
  case cSetting_cartoon_refine_normals:
  case cSetting_cartoon_smooth_loops:
  case cSetting_cartoon_dumbbell_width:
  case cSetting_cartoon_dumbbell_length:
  case cSetting_cartoon_dumbbell_radius:
  case cSetting_cartoon_fancy_helices:
  case cSetting_cartoon_fancy_sheets:
  case cSetting_cartoon_cylindrical_helices:
  case cSetting_cartoon_refine_tips:
  case cSetting_cartoon_helix_radius:
  case cSetting_cartoon_throw:
  case cSetting_cartoon_debug:
  case cSetting_cartoon_highlight_color:
  case cSetting_cartoon_discrete_colors:
  case cSetting_cartoon_smooth_first:
  case cSetting_cartoon_smooth_last:
  case cSetting_cartoon_smooth_cycles:
  case cSetting_cartoon_flat_cycles:
  case cSetting_cartoon_gap_cutoff:
  case cSetting_cartoon_all_alt:
    ExecutiveInvalidateRep(G, inv_sele, cRepCartoon, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_cartoon_use_shader:
    if (SettingGetGlobal_b(G, cSetting_use_shaders)){
      ExecutiveInvalidateRep(G, inv_sele, cRepCartoon, cRepInvRep);
      SceneChanged(G);
    }
    break;
  case cSetting_cgo_use_shader:
    if (SettingGetGlobal_b(G, cSetting_use_shaders)){
      ExecutiveInvalidateRep(G, inv_sele, cRepCGO, cRepInvRep);
      SceneChanged(G);
    }
    break;
  case cSetting_cgo_shader_ub_flags:
    if (SettingGetGlobal_b(G, cSetting_use_shaders) && SettingGetGlobal_b(G, cSetting_cgo_use_shader)){
      ExecutiveInvalidateRep(G, inv_sele, cRepSphere, cRepInvRep);
      SceneChanged(G);
    }
    break;
  case cSetting_cgo_shader_ub_color:
    if (SettingGetGlobal_b(G, cSetting_use_shaders) && SettingGetGlobal_b(G, cSetting_cgo_use_shader)){
      ExecutiveInvalidateRep(G, inv_sele, cRepCGO, cRepInvRep);
      ExecutiveInvalidateRep(G, inv_sele, cRepCartoon, cRepInvRep);
      ExecutiveInvalidateRep(G, inv_sele, cRepSphere, cRepInvRep);
      SceneChanged(G);
    }
    break;
  case cSetting_cgo_shader_ub_normal:
    if (SettingGetGlobal_b(G, cSetting_use_shaders) && SettingGetGlobal_b(G, cSetting_cgo_use_shader)){
      ExecutiveInvalidateRep(G, inv_sele, cRepCGO, cRepInvRep);
      ExecutiveInvalidateRep(G, inv_sele, cRepCartoon, cRepInvRep);
      // if spheres rendered with geometry, then normals are used
      // this should really only invalidate spheres that use normals (sphere_mode=0, not sure about other modes)
      // but invalidating all spheres for now
      ExecutiveInvalidateRep(G, inv_sele, cRepSphere, cRepInvRep); 
      SceneChanged(G);
    }
    break;
  case cSetting_trilines:
     ExecutiveInvalidateRep(G, inv_sele, cRepLine, cRepInvRep);
     ExecutiveInvalidateRep(G, inv_sele, cRepCyl, cRepInvRep);
     ExecutiveInvalidateRep(G, inv_sele, cRepRibbon, cRepInvRep);
     ExecutiveInvalidateRep(G, inv_sele, cRepCartoon, cRepInvRep);
     ExecutiveInvalidateRep(G, inv_sele, cRepDash, cRepInvRep);
     ExecutiveInvalidateRep(G, inv_sele, cRepNonbonded, cRepInvRep);
     SceneChanged(G);
     break;
  case cSetting_dot_width:
  case cSetting_dot_radius:
  case cSetting_dot_density:
  case cSetting_dot_mode:
  case cSetting_dot_solvent:
  case cSetting_dot_hydrogens:
  case cSetting_trim_dots:
    ExecutiveInvalidateRep(G, inv_sele, cRepDot, cRepInvRep);
    SceneChanged(G);
    break;
  case cSetting_bg_gradient:
      ColorUpdateFrontFromSettings(G);
      ExecutiveInvalidateRep(G, inv_sele, cRepAll, cRepInvColor);
    G->ShaderMgr->Set_Reload_Bits(RELOAD_VARIABLES);
    SceneChanged(G);
    break;
  case cSetting_bg_image_mode:
  case cSetting_bg_image_filename:
    G->ShaderMgr->Set_Reload_Bits(RELOAD_VARIABLES);
  case cSetting_bg_image_linear:
  case cSetting_bg_image_tilesize:
    OrthoBackgroundTextureNeedsUpdate(G);
    SceneChanged(G);
    break;
  case cSetting_bg_rgb_top:
  case cSetting_bg_rgb_bottom:
    {
      /* clamp this value */
      const char * bg_image_filename = SettingGet_s(G, NULL, NULL, cSetting_bg_image_filename);
      if(!(bg_image_filename && bg_image_filename[0]) &&
          SettingGetGlobal_b(G, cSetting_bg_gradient) && !OrthoBackgroundDataIsSet(*G->Ortho)) {
        ColorUpdateFrontFromSettings(G);
	ExecutiveInvalidateRep(G, inv_sele, cRepAll, cRepInvColor);
	OrthoBackgroundTextureNeedsUpdate(G);
      }
    }
    SceneChanged(G);
    break;
  case cSetting_bg_rgb:
    {
      /* clamp this value */
      const float *v = ColorGet(G, SettingGet_color(G, NULL, NULL, cSetting_bg_rgb));
      {
        const char * bg_image_filename = SettingGet_s(G, NULL, NULL, cSetting_bg_image_filename);
        if(!(bg_image_filename && bg_image_filename[0]) && !OrthoBackgroundDataIsSet(*G->Ortho)) {
	  ColorUpdateFront(G, v);
	  ExecutiveInvalidateRep(G, inv_sele, cRepAll, cRepInvColor);
	}
      }
    }
    SceneChanged(G);
    break;
  case cSetting_selection_width:
  case cSetting_selection_width_scale:
  case cSetting_selection_width_max:
  case cSetting_selection_round_points:
    ExecutiveInvalidateSelectionIndicatorsCGO(G);
  case cSetting_line_smooth:
  case cSetting_ortho:
  case cSetting_chromadepth:
  case cSetting_transparency_mode:
  if (index == cSetting_transparency_mode)
#ifdef _WEBGL
#endif
    ExecutiveInvalidateRep(G, inv_sele, cRepCGO, cRepInvAll);
  case cSetting_depth_cue:
  case cSetting_fog:
  case cSetting_ray_transparency_oblique:
    G->ShaderMgr->Set_Reload_Bits(RELOAD_VARIABLES);
  case cSetting_ray_transparency_oblique_power:
    SceneInvalidate(G);
    break;
  case cSetting_sculpting:
    OrthoDirty(G);
    break;
  case cSetting_auto_overlay:
    OrthoRemoveAutoOverlay(G); /* always start clean */
    break;
  case cSetting_overlay:
  case cSetting_overlay_lines:
  case cSetting_text:
    OrthoDirty(G);
    break;
  case cSetting_internal_gui_mode:
  case cSetting_internal_gui_width:
  case cSetting_internal_gui:
  case cSetting_internal_feedback:
  case cSetting_mouse_grid:
  case cSetting_movie_panel_row_height:
  case cSetting_movie_panel:
    if(!SettingGetGlobal_b(G, cSetting_suspend_updates)) {
      OrthoCommandIn(G, "viewport");
    }
    break;
  case cSetting_suspend_updates:
    if(!SettingGetGlobal_b(G, cSetting_suspend_updates)) {
      SceneChanged(G);          /* force big update upon resumption */
      OrthoDirty(G);
    }
    break;
  case cSetting_security:
    G->Security = SettingGetGlobal_i(G, cSetting_security);
    break;
  case cSetting_state:
    if (SettingGet<int>(G, index) < 0 /* all */) {
      PRINTFB(G, FB_Setting, FB_Warnings)
        " Setting-Warning: state can't be less than 0.\n" ENDFB(G);
    }
  case cSetting_all_states:
  case cSetting_frame:
    ExecutiveInvalidateSelectionIndicatorsCGO(G);
    SceneInvalidatePicking(G);
    SceneChanged(G);
    break;
  case cSetting_dynamic_width:
  case cSetting_dynamic_width_factor:
  case cSetting_dynamic_width_min:
  case cSetting_dynamic_width_max:
    SceneChanged(G);
    break;
  case cSetting_rock:
  case cSetting_sweep_mode:
  case cSetting_sweep_phase:
  case cSetting_sweep_angle:
  case cSetting_sweep_speed:
    SceneRestartSweepTimer(G);
    break;
  case cSetting_motion_power:
  case cSetting_motion_bias:
  case cSetting_motion_simple:
  case cSetting_motion_linear:
  case cSetting_motion_hand:
  case cSetting_movie_loop:
    if(SettingGetGlobal_b(G, cSetting_movie_auto_interpolate)) {
      ExecutiveMotionReinterpolate(G);
    }
    break;
  case cSetting_volume_bit_depth:
    ExecutiveInvalidateRep(G, inv_sele, cRepVolume, cRepInvAll);
    SceneInvalidate(G);
    break;
  case cSetting_volume_layers:
    ExecutiveInvalidateRep(G, inv_sele, cRepVolume, cRepInvColor);
    SceneInvalidate(G);
    break;
  case cSetting_cgo_transparency:
    SceneInvalidate(G);
    SceneChanged(G);
    break;
  case cSetting_label_connector_mode:
    {
      int lc_mode = SettingGetGlobal_i(G, cSetting_label_connector_mode);
      if(lc_mode < 0 || lc_mode > 4){
	if (!quiet){
	  PRINTFB(G, FB_Setting, FB_Warnings)
	    "Setting-Warning: label_connector_mode range = [0,4]"
	    ENDFB(G);
	}
      }	
    }
  case cSetting_float_labels:
  case cSetting_label_z_target:
  case cSetting_label_connector:
  case cSetting_label_connector_color:
  case cSetting_label_connector_width:
  case cSetting_label_connector_ext_length:
  case cSetting_label_bg_color:
  case cSetting_label_placement_offset:
  case cSetting_label_relative_mode:
  case cSetting_label_screen_point:
  case cSetting_label_multiline_spacing:
  case cSetting_label_multiline_justification:
  case cSetting_label_padding:
  case cSetting_label_bg_transparency:
  case cSetting_label_bg_outline:
  case cSetting_ray_label_connector_flat:
    ExecutiveInvalidateRep(G, inv_sele, cRepLabel, cRepInvAll );
    break;
  case cSetting_surface_color_smoothing:
  case cSetting_surface_color_smoothing_threshold:
    ExecutiveInvalidateRep(G, inv_sele, cRepSurface, cRepInvColor);    
    SceneChanged(G);
    break;
  case cSetting_smooth_half_bonds:
    SceneChanged(G);
    break;    
  case cSetting_antialias_shader:
  case cSetting_atom_type_format:
  case cSetting_colored_feedback:
  case cSetting_load_atom_props_default:
  case cSetting_load_object_props_default:
  case cSetting_suspend_undo:
  case cSetting_volume_mode:
    PRINTFB(G, FB_Setting, FB_Warnings)
      " Setting-Warning: %s is not supported in Open-Source version of PyMOL\n",
      SettingInfo[index].name
      ENDFB(G);
    break;
    break;
  case cSetting_surface_debug:
    if (SettingGetGlobal_b(G, cSetting_use_shaders)){
      ExecutiveInvalidateRep(G, inv_sele, cRepSurface, cRepInvColor);
    }
    break;
  case cSetting_pick32bit:
    SceneInvalidatePicking(G);
    break;
  case cSetting_display_scale_factor:
  {
    int scaleFactor = SettingGetGlobal_i(G, cSetting_display_scale_factor);
    if (scaleFactor > 0) {
      _gScaleFactor = scaleFactor;
      ExecutiveInvalidateRep(G, NULL, cRepLabel, cRepInvRep);
      OrthoCommandIn(G, "viewport");
    } else {
      SettingSetGlobal_i(G, cSetting_display_scale_factor, 1);
      PRINTFB(G, FB_Setting, FB_Warnings)
        "Setting-Error: Cannot set a display scale factor of 0\n"
        ENDFB(G);
    }
    break;
  }
#ifdef _PYMOL_OPENVR
  case cSetting_openvr_gui_distance:
  case cSetting_openvr_gui_fov:
  // case cSetting_openvr_gui_alpha:
  // case cSetting_openvr_gui_use_alpha:
  case cSetting_openvr_gui_scene_color:
  case cSetting_openvr_gui_scene_alpha:
  case cSetting_openvr_gui_back_color:
  case cSetting_openvr_gui_back_alpha:
  // case cSetting_openvr_gui_use_backdrop:
  // case cSetting_openvr_gui_overlay:
  // case cSetting_openvr_gui_text:
    OpenVRMenuSettingsChanged(G);
    break;
  case cSetting_openvr_disable_clipping:
    OpenVRClippingChanged(G);
    break;
  case cSetting_openvr_laser_width:
    OpenVRLaserWidthChanged(G); 
    break;
#endif
  default:
    break;
  }
}


/*========================================================================*/
void SettingFreeGlobal(PyMOLGlobals * G)
{
  CSetting *I = G->Setting;
  SettingUniqueFree(G);
  SettingPurge(I);
  if(G->Default) {
    SettingPurge(G->Default);
    FreeP(G->Default);
  }
  FreeP(G->Setting);
}


/*========================================================================*/
void SettingInitGlobal(PyMOLGlobals * G, int alloc, int reset_gui, int use_default)
{
  CSetting *I = G->Setting;

  /* use function pointers to prevent the compiler from inlining every
     call in this block (a waste of RAM and time) */

  int (*set_i) (CSetting * I, int index, int value) = SettingSet_i;
  int (*set_b) (CSetting * I, int index, int value) = SettingSet_b;

  if(alloc || !I) {
    I = (G->Setting = pymol::calloc<CSetting>(1));
    SettingUniqueInit(G);
    SettingInit(G, I);
  }

  if(G->Default && use_default) {

    SettingCopyAll(G, G->Default, G->Setting);

  } else {

    // copy defaults from SettingInfo table
    for(int index = 0; index < cSetting_INIT; ++index) {
      if (!reset_gui) switch (index) {
        case cSetting_internal_gui_width:
        case cSetting_internal_gui:
          continue;
      }

      SettingRestoreDefault(I, index);
    }

    // open-source has no volume_mode=1
    set_i(I, cSetting_volume_mode, 0);

    // command line arguments overwrites
    set_b(I, cSetting_auto_show_lines, G->Option->sphere_mode < 0);
    set_i(I, cSetting_auto_zoom, G->Option->zoom_mode);
    set_b(I, cSetting_auto_show_nonbonded, G->Option->sphere_mode < 0);
    set_b(I, cSetting_presentation, G->Option->presentation);
    set_i(I, cSetting_defer_builds_mode, G->Option->defer_builds_mode);
    set_b(I, cSetting_presentation_auto_quit, !G->Option->no_quit);
    set_b(I, cSetting_auto_show_spheres, G->Option->sphere_mode >= 0);
    set_i(I, cSetting_internal_feedback, G->Option->internal_feedback);

    if(G->Option->stereo_mode) {
      set_i(I, cSetting_stereo_mode, G->Option->stereo_mode);
    } else if(G->StereoCapable || G->Option->blue_line) {
      set_i(I, cSetting_stereo_mode, cStereo_quadbuffer);      /* quadbuffer if we can */
    }

    /* In order to get electrostatic potentials in kT from the Coulomb equation... 

       PyMOL charges: Q, in e
       PyMOL distances: r, in Angstrom
       Coulomb Constant: K = 8.987552e9 ((J*m)/(C^2))
       Angstrom Convertor: 1 A = 1e-10 m
       Coulomb Convertor: 1 e = 1.60217733e-19 C
       Angstrom Convertor: 1 A = 10e-10 m
       Dielectric Constant: D (unitless)

       ePot = (KQ)/(Dr) = 

       8.987552e9 J*m     1.6021773e-19 C   1.6021773e-19 C      1 A        Q  
       ---------------- * --------------- * --------------- * ---------- * --- =
       C^2              1 e               1 e            1e-10 m     Dr

       2.3070795237e-18 J*A      Q
       = ---------------------- * ---
       e^2               Dr

       Boltzmann Constant: k = 1.380658e-23 (J/K)
       Temperature: 300 Kelvin

       kT = 1.380658e-23 * 300 = 4.141974e-21 J

       2.3070795237e-18 J*A         1 kT             Q
       ePot = --------------------- * ------------------ * ---
       e^2              4.141974e-21 J       Dr

       557.00000 kT*A    Q
       ePot = -------------- * --- which will give kT/e units when applied
       e^2           Dr
     */

#ifdef WIN32
    /* BEGIN PROPRIETARY CODE SEGMENT (see disclaimer in "os_proprietary.h") */
    set_b(I, cSetting_cache_display, 0);

    // PYMOL-3143/PYMOL-3181 Intel HD Graphics
    // 4.5.0 - Build *.20.100.*
    // Driver update breaks lighting
    set_b(I, cSetting_precomputed_lighting, 1);

#ifndef _PYMOL_ACTIVEX
    {
      SYSTEM_INFO SysInfo;
      GetSystemInfo(&SysInfo);
      {
        DWORD count = SysInfo.dwNumberOfProcessors;
        if(count > 1) {
          set_i(I, cSetting_max_threads, count);
        }
      }
    }
    /* END PROPRIETARY CODE SEGMENT */
#endif
#endif

#ifndef _PYMOL_FREETYPE
    set_i(I, cSetting_label_font_id, 0);
#endif

#ifdef _PYMOL_IOS
#endif
  }
  G->ShaderMgr->Set_Reload_Bits(RELOAD_ALL_SHADERS);
}

int SettingCheckFontID(PyMOLGlobals * G, CSetting * set1, CSetting * set2, int font_id){
  int ret = font_id;
  if (font_id < 5){  // we are no longer supporting GLUT labels since they are not resizeable
    PRINTFB(G, FB_Setting, FB_Warnings)
      "RepLabel-Warning: GLUT labels (label_font_id 0-4) are no longer available for labelling\n    the scene since they are not resizeable label_font_id=%d setting back to 5 (default) \n",
      font_id  ENDFB(G);
    if (SettingGet_i(G, set1, NULL, cSetting_label_font_id) == font_id && SettingSet_i(set1, cSetting_label_font_id, 5)){
    } else if (SettingGet_i(G, set2, NULL, cSetting_label_font_id) == font_id && SettingSet_i(set2, cSetting_label_font_id, 5)){
    } else if (SettingGetGlobal_i(G, cSetting_label_font_id) == font_id){
      SettingSetGlobal_i(G, cSetting_label_font_id, 5);
    };
    ret = 5;
  }
  return ret;
}

/*
 * State index iterator constructor, see Setting.h for documentation.
 */
StateIterator::StateIterator(PyMOLGlobals * G, CSetting * set, int state_, int nstate) {
  if(state_ == -2) {
    // current state
    state_ = SettingGet_i(G, set, NULL, cSetting_state) - 1;
  }

  if(state_ == -1) {
    // all states
    state = 0;
    end = nstate;
  } else {
    // given state or static singleton
    state = (state_ > 0 && nstate == 1
        && SettingGet_b(G, set, NULL, cSetting_static_singletons)) ? 0 : state_;
    end = state + 1;
  }

  if (state < 0)
    state = 0;

  if (end > nstate)
    end = nstate;

  state--;
}

StateIterator::StateIterator(CObject* obj, int state_)
    : StateIterator(obj->G, obj->Setting, state_, obj->getNFrame())
{
}

/*
 * Helper function to init CPyMOL.Setting, a (name: index) dictionary.
 * Called in PyMOL_InitAPI
 */
bool CPyMOLInitSetting(OVLexicon * Lex, OVOneToOne * Setting) {
  for(int index = 0; index < cSetting_INIT; ++index) {
    auto &rec = SettingInfo[index];

    if (rec.level == cSettingLevel_unused)
      continue;

    OVreturn_word result = OVLexicon_GetFromCString(Lex, rec.name);

    if( !OVreturn_IS_OK(result) ||
        !OVreturn_IS_OK(OVOneToOne_Set(Setting, result.word, index)))
      return false;
  }

  return true;
}

#ifndef _PYMOL_NOPY
/*
 * Export the settings names to Python a as (name: index) dictionary.
 * Replacement for pymol.settings.SettingIndex
 */
PyObject * SettingGetSettingIndices() {
  PyObject * val;
  PyObject * dict = PyDict_New();

  for(int index = 0; index < cSetting_INIT; ++index) {
    auto &rec = SettingInfo[index];

    if (rec.level == cSettingLevel_unused)
      continue;

    if ((val = PyInt_FromLong(index))) {
      PyDict_SetItemString(dict, rec.name, val);
      Py_DECREF(val);
    }
  }

  return dict;
}

/*
 * Return a list of all setting indices for the given unique id
 */
PyObject * SettingUniqueGetIndicesAsPyList(PyMOLGlobals * G, int unique_id)
{
  CSettingUnique *I = G->SettingUnique;
  PyObject * list = PyList_New(0);
  OVreturn_word result;

  if(unique_id && OVreturn_IS_OK(result = OVOneToOne_GetForward(I->id2offset, unique_id))) {
    SettingUniqueEntry *entry;
    for (int offset = result.word; offset; offset = entry->next) {
      entry = I->entry + offset;
      PyObject *item = PyInt_FromLong(entry->setting_id);
      PyList_Append(list, item);
      Py_DECREF(item);
    }
  }

  return list;
}
#endif

/*
 * Getters for templatted programming
 */

const CSetting * _SettingGetFirstDefined(int index,
    PyMOLGlobals * G,
    const CSetting * set1,
    const CSetting * set2) {
  if (set1 && set1->info[index].defined)
    return set1;
  if (set2 && set2->info[index].defined)
    return set2;
  return G->Setting;
}