summaryrefslogtreecommitdiff
path: root/fluid/nodes/Function_Node.cxx
blob: 8ffdbef0175dac64f3a685382683e7fd10f2e336 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
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
//
// C function Node code for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2025 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file.  If this
// file is missing or damaged, see the license at:
//
//     https://www.fltk.org/COPYING.php
//
// Please see the following page on how to report bugs and issues:
//
//     https://www.fltk.org/bugs.php
//

#include "nodes/Function_Node.h"

#include "Fluid.h"
#include "proj/mergeback.h"
#include "proj/undo.h"
#include "io/Project_Reader.h"
#include "io/Project_Writer.h"
#include "io/Code_Writer.h"
#include "nodes/Window_Node.h"
#include "nodes/Group_Node.h"
#include "panels/function_panel.h"
#include "rsrcs/comments.h"
#include "widgets/Node_Browser.h"

#include <FL/fl_string_functions.h>
#include <FL/Fl_File_Chooser.H>
#include <FL/fl_ask.H>
#include "../src/flstring.h"

#include <zlib.h>


/// Set a current class, so that the code of the children is generated correctly.
Class_Node *current_class = nullptr;

/**
 \brief Return 1 if the list contains a function with the given signature at the top level.
 Widget_Node uses this to check if a callback by a certain signature is
 already defined by the user within this file. If not, Widget_Node will
 generate an `extern $sig$;` statement.
 \param[in] rtype return type, can be nullptr to avoid checking (not used by Widget_Node)
 \param[in] sig function signature
 \return 1 if found.
 */
int has_toplevel_function(const char *rtype, const char *sig) {
  Node *child;
  for (child = Fluid.proj.tree.first; child; child = child->next) {
    if (!child->is_in_class() && child->is_a(Type::Function)) {
      const Function_Node *fn = (const Function_Node*)child;
      if (fn->has_signature(rtype, sig))
        return 1;
    }
  }
  return 0;
}


////////////////////////////////////////////////////////////////
// quick check of any C code for legality, returns an error message

static char buffer[128]; // for error messages

/**
 Check a quoted string contains a character.
 This is used to find a matching " or ' in a string.
 \param[inout] c start searching here, return where we found \c type
 \param[in] type find this character
 \return nullptr if the character was found, else a pointer to a static string
    with an error message
 */
const char *_q_check(const char * & c, int type) {
  for (;;) switch (*c++) {
    case '\0':
      sprintf(buffer,"missing %c",type);
      return buffer;
    case '\\':
      if (*c) c++;
      break;
    default:
      if (*(c-1) == type) return nullptr;
  }
}

/**
 Check normal code, match brackets and parenthesis.
 Recursively run a line of code and make sure that
 {, [, ", ', and ( are matched.
 \param[inout] c start searching here, return the end of the search
 \param[in] type find this character match
 \return nullptr if the character was found, else a pointer to a static string
    with an error message
 */
const char *_c_check(const char * & c, int type) {
  const char *d;
  for (;;) switch (*c++) {
    case 0:
      if (!type) return nullptr;
      sprintf(buffer, "missing '%c'", type);
      return buffer;
    case '/':
      // Skip comments as needed...
      if (*c == '/') {
        while (*c != '\n' && *c) c++;
      } else if (*c == '*') {
        c++;
        while ((*c != '*' || c[1] != '/') && *c) c++;
        if (*c == '*') c+=2;
        else {
          return "missing '*/'";
        }
      }
      break;
//    case '#':
//      // treat cpp directives as a comment:
//      // Matt: a '#' character can appear as a concatenation when defining macros
//      // Matt: so instead we just silently ignore the '#'
//      while (*c != '\n' && *c) c++;
//      break;
    case '{':
//      // Matt: C++ does allow {} inside () now
//      if (type==')') goto UNEXPECTED;
      d = _c_check(c,'}');
      if (d) return d;
      break;
    case '(':
      d = _c_check(c,')');
      if (d) return d;
      break;
    case '[':
      d = _c_check(c,']');
      if (d) return d;
      break;
    case '\"':
      d = _q_check(c,'\"');
      if (d) return d;
      break;
    case '\'':
      d = _q_check(c,'\'');
      if (d) return d;
      break;
    case '}':
    case ')':
    case ']':
//    UNEXPECTED:
      if (type == *(c-1)) return nullptr;
      sprintf(buffer, "unexpected '%c'", *(c-1));
      return buffer;
  }
}

/**
 Check legality of c code (sort of) and return error:
 Make sure that {, ", ', and ( are matched.
 \param[in] c start searching here
 \param[in] type find this character match (default is 0)
 \return nullptr if the character was found, else a pointer to a static string
    with an error message
 \note This function checks every conceivable line of code, which is not
    always wanted. It can't differentiate characters in comments, and the
    user may well intend to leave a curly bracket open
    (i.e. namespace { ... } ). We should make this option user selectable.
 */
const char *c_check(const char *c, int type) {
  return _c_check(c,type);
}

// ---- Function_Node implementation

/** \class Function_Node
 Manage a C++ function node in the Fluid design.

 A function can have a signature (name followed by arguments), a return type
 and a comment section. If can be local or global, and it can be declared a C
 or C++ function.
 */

/// Prototype for a function to be used by the factory.
Function_Node Function_Node::prototype;

/**
 Create a new function.
 */
Function_Node::Function_Node() :
  Node(),
  return_type(nullptr),
  public_(0),
  cdecl_(0),
  constructor(0),
  havewidgets(0)
{ }

/**
 Destructor.
 */
Function_Node::~Function_Node() {
  if (return_type) free((void*)return_type);
}

/**
 Create a new function for the widget tree.
 \param[in] strategy add new function after current or as last child
 \return the new node
 */
Node *Function_Node::make(Strategy strategy) {
  Node *anchor = Fluid.proj.tree.current, *p = anchor;
  if (p && (strategy.placement() == Strategy::AFTER_CURRENT))
    p = p->parent;
  while (p && !p->is_decl_block()) {
    anchor = p;
    strategy.placement(Strategy::AFTER_CURRENT);
    p = p->parent;
  }
  Function_Node *o = new Function_Node();
  o->name("make_window()");
  o->return_type = nullptr;
  o->add(anchor, strategy);
  o->factory = this;
  o->public_ = 1;
  o->cdecl_ = 0;
  return o;
}

/**
 Write function specific properties to an .fl file.
  - "private"/"public" indicates the state of the function
  - "C" is written if we want a C signature instead of C++
  - "return_type" is followed by the return type of the function
 */
void Function_Node::write_properties(fld::io::Project_Writer &f) {
  Node::write_properties(f);
  switch (public_) {
    case 0: f.write_string("private"); break;
    case 2: f.write_string("protected"); break;
  }
  if (cdecl_) f.write_string("C");
  if (return_type) {
    f.write_string("return_type");
    f.write_word(return_type);
  }
}

/**
 Read function specific properties fron an .fl file.
 \param[in] c read from this string
 */
void Function_Node::read_property(fld::io::Project_Reader &f, const char *c) {
  if (!strcmp(c,"private")) {
    public_ = 0;
  } else if (!strcmp(c,"protected")) {
    public_ = 2;
  } else if (!strcmp(c,"C")) {
    cdecl_ = 1;
  } else if (!strcmp(c,"return_type")) {
    storestring(f.read_word(),return_type);
  } else {
    Node::read_property(f, c);
  }
}

/**
 Open the function_panel dialog box to edit this function.
 */
void Function_Node::open() {
  // fill dialog box
  if (!function_panel) make_function_panel();
  f_return_type_input->value(return_type);
  f_name_input->value(name());
  if (is_in_class()) {
    f_public_member_choice->value(public_);
    f_public_member_choice->show();
    f_public_choice->hide();
    f_c_button->hide();
  } else {
    f_public_choice->value(public_);
    f_public_choice->show();
    f_public_member_choice->hide();
    f_c_button->show();
  }
  f_c_button->value(cdecl_);
  const char *c = comment();
  f_comment_input->buffer()->text(c?c:"");
  function_panel->show();
  const char* message = nullptr;
  for (;;) { // repeat as long as there are errors
    // - message loop until OK or cancel is pressed
    for (;;) {
      Fl_Widget* w = Fl::readqueue();
      if (w == f_panel_cancel) goto BREAK2;
      else if (w == f_panel_ok) break;
      else if (!w) Fl::wait();
    }
    // - check syntax
    const char *c = f_name_input->value();
    while (isspace(*c)) c++;
    message = c_check(c);
    if (!message) {
      const char *d = c;
      for (; *d != '('; d++) if (isspace(*d) || !*d) break;
      if (*c && *d != '(')
        message = "must be 'name(arguments)'";
    }
    if (!message) {
      c = f_return_type_input->value();
      message = c_check(c);
    }
    // - alert user
    if (message) {
      int v = fl_choice("Potential syntax error detected: %s",
                        "Continue Editing", "Ignore Error", nullptr, message);
      if (v==0) continue;     // Continue Editing
      //if (v==1) { }         // Ignore Error and close dialog
    }
    // - copy dialog data to target variables
    int mod = 0;
    name(f_name_input->value());
    storestring(f_return_type_input->value(), return_type);
    if (is_in_class()) {
      if (public_ != f_public_member_choice->value()) {
        mod = 1;
        public_ = f_public_member_choice->value();
        redraw_browser();
      }
    } else {
      if (public_ != f_public_choice->value()) {
        mod = 1;
        public_ = f_public_choice->value();
        redraw_browser();
      }
    }
    if (cdecl_ != f_c_button->value()) {
      mod = 1;
      cdecl_ = f_c_button->value();
    }
    c = f_comment_input->buffer()->text();
    if (c && *c) {
      if (!comment() || strcmp(c, comment()))  { Fluid.proj.set_modflag(1); redraw_browser(); }
      comment(c);
    } else {
      if (comment())  { Fluid.proj.set_modflag(1); redraw_browser(); }
      comment(nullptr);
    }
    if (c) free((void*)c);
    if (mod) Fluid.proj.set_modflag(1);
    break;
  }
BREAK2:
  function_panel->hide();
}

/**
 Return 1 if the function is global.
 \return 1 if public, 0 if local.
 */
int Function_Node::is_public() const {
  return public_;
}

static bool fd_isspace(int c) {
  return (c>0 && c<128 && isspace(c));
}

// code duplication: see int is_id(char c) in code.cxx
static bool fd_iskeyword(int c) {
  return (c>0 && c<128 && (isalnum(c) || c=='_'));
}

// remove all function default parameters and `override` keyword
static void clean_function_for_implementation(char *out, const char *function_name) {
  char *sptr = out;
  const char *nptr = function_name;
  int skips=0,skipc=0;
  int nc=0,plevel=0;
  bool arglist_done = false;
  for (;*nptr; nc++,nptr++) {
    if (arglist_done && fd_isspace(nptr[0])) {
      // skip `override` and `FL_OVERRIDE` keywords if they are following the list of arguments
      if (strncmp(nptr+1, "override", 8)==0 && !fd_iskeyword(nptr[9])) { nptr += 8; continue; }
      else if (strncmp(nptr+1, "FL_OVERRIDE", 11)==0 && !fd_iskeyword(nptr[12])) { nptr += 11; continue; }
    }
    if (!skips && *nptr=='(') plevel++;
    else if (!skips && *nptr==')') { plevel--; if (plevel==0) arglist_done = true; }
    if ( *nptr=='"' &&  !(nc &&  *(nptr-1)=='\\') )
      skips = skips ? 0 : 1;
    else if(!skips && *nptr=='\'' &&  !(nc &&  *(nptr-1)=='\\'))
      skipc = skipc ? 0 : 1;
    if(!skips && !skipc && plevel==1 && *nptr =='=' && !(nc && *(nptr-1)=='\'') ) { // ignore '=' case
      while(*++nptr  && (skips || skipc || ( (*nptr!=',' && *nptr!=')') || plevel!=1) )) {
        if ( *nptr=='"' &&  *(nptr-1)!='\\' )
          skips = skips ? 0 : 1;
        else if(!skips && *nptr=='\'' &&  *(nptr-1)!='\\')
          skipc = skipc ? 0 : 1;
        if (!skips && !skipc && *nptr=='(') plevel++;
        else if (!skips && *nptr==')') plevel--;
      }
      if (*nptr==')') if (--plevel==0) arglist_done = true;
    }
    if (sptr < (out + 1024 - 1)) *sptr++ = *nptr;
  }
  *sptr = '\0';
}


/**
 Write the code for the source and the header file.
 This writes the code that goes \b before all children of this class.
 \see write_code2(fld::io::Code_Writer& f)
 */
void Function_Node::write_code1(fld::io::Code_Writer& f) {
  constructor=0;
  havewidgets = 0;
  Node *child;
  // if the function has no children (hence no body), Fluid will not generate
  // the function either. This is great if you decide to implement that function
  // inside another module
  char havechildren = 0;
  for (child = next; child && child->level > level; child = child->next) {
    havechildren = 1;
    if (child->is_widget()) {
      havewidgets = 1;
      break;
    }
  }
  if (havechildren)
    f.write_c("\n");
  if (ismain()) {
    if (havechildren)
      f.write_c("int main(int argc, char **argv) {\n");
  } else {
    const char* rtype = return_type;
    const char* star = "";
    // from matt: let the user type "static " at the start of type
    // in order to declare a static method;
    int is_static = 0;
    int is_virtual = 0;
    if (rtype) {
      if (!strcmp(rtype,"static")) {is_static = 1; rtype = nullptr;}
      else if (!strncmp(rtype, "static ",7)) {is_static = 1; rtype += 7;}
    }
    if (rtype) {
      if (!strcmp(rtype, "virtual")) {is_virtual = 1; rtype = nullptr;}
      else if (!strncmp(rtype, "virtual ",8)) {is_virtual = 1; rtype += 8;}
    }
    if (!rtype) {
      if (havewidgets) {
        rtype = subclassname(child);
        star = "*";
      } else rtype = "void";
    }

    const char* k = class_name(0);
    if (k) {
      f.write_public(public_);
      if (havechildren)
        write_comment_c(f);
      if (name()[0] == '~')
        constructor = 1;
      else {
        size_t n = strlen(k);
        if (!strncmp(name(), k, n) && name()[n] == '(') constructor = 1;
      }
      f.write_h("%s", f.indent(1));
      if (is_static) f.write_h("static ");
      if (is_virtual) f.write_h("virtual ");
      if (!constructor) {
        f.write_h("%s%s ", rtype, star);
        if (havechildren)
          f.write_c("%s%s ", rtype, star);
      }

      // if this is a subclass, only f.write_h() the part before the ':'
      char s[1024], *sptr = s;
      char *nptr = (char *)name();

      while (*nptr) {
        if (*nptr == ':') {
          if (nptr[1] != ':') break;
          // Copy extra ":" for "class::member"...
          *sptr++ = *nptr++;
        }
        *sptr++ = *nptr++;
      }
      *sptr = '\0';

      if (s[strlen(s)-1] == '}') {  // special case for inlined functions
        f.write_h("%s\n", s);
      } else {
        f.write_h("%s;\n", s);
      }
      if (havechildren) {
        clean_function_for_implementation(s, name());
        f.write_c("%s::%s {\n", k, s);
      }
    } else {
      if (havechildren)
        write_comment_c(f);
      if (public_==1) {
        if (cdecl_)
          f.write_h("extern \"C\" { %s%s %s; }\n", rtype, star, name());
        else
          f.write_h("%s%s %s;\n", rtype, star, name());
      } else if (public_==2) {
        // write neither the prototype nor static, the function may be declared elsewhere
      } else {
        if (havechildren)
          f.write_c("static ");
      }

      // write everything but the default parameters (if any)
      char s[1024];
      if (havechildren) {
        clean_function_for_implementation(s, name());
        f.write_c("%s%s %s {\n", rtype, star, s);
      }
    }
  }

  if (havewidgets && child && !child->name())
    f.write_c("%s%s* w;\n", f.indent(1), subclassname(child));
  f.indentation++;
}

/**
 Write the code for the source and the header file.
 This writes the code that goes \b after all children of this class.
 \see write_code1(fld::io::Code_Writer& f)
 */
void Function_Node::write_code2(fld::io::Code_Writer& f) {
  Node *child;
  const char *var = "w";
  char havechildren = 0;
  for (child = next; child && child->level > level; child = child->next) {
    havechildren = 1;
    if (child->is_a(Type::Window) && child->name()) var = child->name();
  }

  if (ismain()) {
    if (havewidgets)
      f.write_c("%s%s->show(argc, argv);\n", f.indent(1), var);
    if (havechildren)
      f.write_c("%sreturn Fl::run();\n", f.indent(1));
  } else if (havewidgets && !constructor && !return_type) {
    f.write_c("%sreturn %s;\n", f.indent(1), var);
  }
  if (havechildren)
    f.write_c("}\n");
  f.indentation = 0;
}

/**
 Check if the return type and signatures match.
 \param[in] rtype function return type
 \param[in] sig function name followed by arguments
 \return 1 if they match, 0 if not
 */
int Function_Node::has_signature(const char *rtype, const char *sig) const {
  if (rtype && !return_type) return 0;
  if (!name()) return 0;
  if ( (rtype==nullptr || strcmp(return_type, rtype)==0)
      && fl_filename_match(name(), sig)) {
    return 1;
  }
  return 0;
}

// ---- Code_Node declaration

/** \class Code_Node
 Manage a block of C++ code in the Fluid design.

 This node manages an arbitrary block of code inside a function that will
 be written into the source code file. Fl_Code_Block has no comment field.
 However, the first line of code will be shown in the widget browser.
 */

/// Prototype for code to be used by the factory.
Code_Node Code_Node::prototype;

/**
 Constructor.
 */
Code_Node::Code_Node() :
  cursor_position_(0),
  code_input_scroll_row(0),
  code_input_scroll_col(0)
{}

/**
 Make a new code node.
 If the parent node is not a function, a message box will pop up and
 the request will be ignored.
 \param[in] strategy add code after current or as last child
 \return new Code node
 */
Node *Code_Node::make(Strategy strategy) {
  Node *anchor = Fluid.proj.tree.current, *p = anchor;
  if (p && (strategy.placement() == Strategy::AFTER_CURRENT))
    p = p->parent;
  while (p && !p->is_code_block()) {
    anchor = p;
    strategy.placement(Strategy::AFTER_CURRENT);
    p = p->parent;
  }
  if (!p) {
    fl_message("Please select a function");
    return nullptr;
  }
  Code_Node *o = new Code_Node();
  o->name("printf(\"Hello, World!\\n\");");
  o->add(anchor, strategy);
  o->factory = this;
  return o;
}

/**
 Open the code_panel or an external editor to edit this code section.
 */
void Code_Node::open() {
  // Using an external code editor? Open it..
  if ( Fluid.use_external_editor && Fluid.external_editor_command[0] ) {
    const char *cmd = Fluid.external_editor_command;
    const char *code = name();
    if (!code) code = "";
    if ( editor_.open_editor(cmd, code) == 0 )
      return;   // return if editor opened ok, fall thru to built-in if not
  }
  // Use built-in code editor..
  if (!code_panel) make_code_panel();
  const char *text = name();
  code_input->buffer()->text( text ? text : "" );
  code_input->insert_position(cursor_position_);
  code_input->scroll(code_input_scroll_row, code_input_scroll_col);
  code_panel->show();
  const char* message = nullptr;
  for (;;) { // repeat as long as there are errors
    for (;;) {
      Fl_Widget* w = Fl::readqueue();
      if (w == code_panel_cancel) goto BREAK2;
      else if (w == code_panel_ok) break;
      else if (!w) Fl::wait();
    }
    char*c = code_input->buffer()->text();
    message = c_check(c);
    if (message) {
      int v = fl_choice("Potential syntax error detected: %s",
                        "Continue Editing", "Ignore Error", nullptr, message);
      if (v==0) continue;     // Continue Editing
      //if (v==1) { }         // Ignore Error and close dialog
    }
    name(c);
    free(c);
    break;
  }
  cursor_position_ = code_input->insert_position();
  code_input_scroll_row = code_input->scroll_row();
  code_input_scroll_col = code_input->scroll_col();
BREAK2:
  code_panel->hide();
}

/**
 Grab changes from an external editor and write this node.
 */
void Code_Node::write(fld::io::Project_Writer &f) {
  // External editor changes? If so, load changes into ram, update mtime/size
  if ( handle_editor_changes() == 1 ) {
    Fluid.main_window->redraw();    // tell fluid to redraw; edits may affect tree's contents
  }
  Node::write(f);
}

/**
 Write the code block with the correct indentation.
 */
void Code_Node::write_code1(fld::io::Code_Writer& f) {
  // External editor changes? If so, load changes into ram, update mtime/size
  if ( handle_editor_changes() == 1 ) {
    Fluid.main_window->redraw();    // tell fluid to redraw; edits may affect tree's contents
  }
  // Matt: disabled f.tag(FD_TAG_GENERIC, 0);
  f.write_c_indented(name(), 0, '\n');
  // Matt: disabled f.tag(FD_TAG_CODE, get_uid());
}

/**
 See if external editor is open.
 */
int Code_Node::is_editing() {
  return editor_.is_editing();
}

/**
 Reap the editor's pid
 \return -2: editor not open
 \return -1: wait failed
 \return 0: process still running
 \return \>0: process finished + reaped (returns pid)
 */
int Code_Node::reap_editor() {
  return editor_.reap_editor();
}

/**
 Handle external editor file modifications.
 If changed, record keeping is updated and file's contents is loaded into ram
 \return 0: file unchanged or not editing
 \return 1: file changed, internal records updated, 'code' has new content
 \return -1: error getting file info (get_ms_errmsg() has reason)
 \todo Figure out how saving a fluid file can be intercepted to grab
    current contents of editor file..
 */
int Code_Node::handle_editor_changes() {
  const char *newcode = nullptr;
  switch ( editor_.handle_changes(&newcode) ) {
    case 1: {            // (1)=changed
      name(newcode);     // update value in ram
      free((void*)newcode);
      return 1;
    }
    case -1: return -1;  // (-1)=error -- couldn't read file (dialog showed reason)
    default: break;      // (0)=no change
  }
  return 0;
}

// ---- CodeBlock_Node implementation

/** \class CodeBlock_Node
 Manage two blocks of C++ code enclosing its children.

 This node manages two lines of code that enclose all children
 of this node. This is usually an if..then clause.

 \todo this node could support multiple lines of code for each block.
 */

/// Prototype for a block of code to be used by the factory.
CodeBlock_Node CodeBlock_Node::prototype;

/**
 Constructor.
 */
CodeBlock_Node::CodeBlock_Node() :
  Node(),
  after(nullptr)
{ }

/**
 Destructor.
 */
CodeBlock_Node::~CodeBlock_Node() {
  if (after)
    free((void*)after);
}

/**
 Make a new code block.
 If the parent node is not a function or another codeblock, a message box will
 pop up and the request will be ignored.
 \param[in] strategy add after current or as last child
 \return new CodeBlock
 */
Node *CodeBlock_Node::make(Strategy strategy) {
  Node *anchor = Fluid.proj.tree.current, *p = anchor;
  if (p && (strategy.placement() == Strategy::AFTER_CURRENT))
    p = p->parent;
  while (p && !p->is_code_block()) {
    anchor = p;
    strategy.placement(Strategy::AFTER_CURRENT);
    p = p->parent;
  }
  if (!p) {
    fl_message("Please select a function");
    return nullptr;
  }
  CodeBlock_Node *o = new CodeBlock_Node();
  o->name("if (test())");
  o->after = nullptr;
  o->add(anchor, strategy);
  o->factory = this;
  return o;
}

/**
 Write the specific properties for this node.
  - "after" is followed by the code that comes after the children
 The "before" code is stored in the name() field.
 */
void CodeBlock_Node::write_properties(fld::io::Project_Writer &f) {
  Node::write_properties(f);
  if (after) {
    f.write_string("after");
    f.write_word(after);
  }
}

/**
 Read the node specific properties.
 */
void CodeBlock_Node::read_property(fld::io::Project_Reader &f, const char *c) {
  if (!strcmp(c,"after")) {
    storestring(f.read_word(),after);
  } else {
    Node::read_property(f, c);
  }
}

/**
 Open the codeblock_panel.
 */
void CodeBlock_Node::open() {
  if (!codeblock_panel) make_codeblock_panel();
  code_before_input->value(name());
  code_after_input->value(after);
  codeblock_panel->show();
  const char* message = nullptr;
  for (;;) { // repeat as long as there are errors
    // event loop
    for (;;) {
      Fl_Widget* w = Fl::readqueue();
      if (w == codeblock_panel_cancel) goto BREAK2;
      else if (w == codeblock_panel_ok) break;
      else if (!w) Fl::wait();
    }
    // check for syntax errors
    message = c_check(code_before_input->value());
    if (!message) {
      message = c_check(code_after_input->value());
    }
    // alert user
    if (message) {
      int v = fl_choice("Potential syntax error detected: %s",
                        "Continue Editing", "Ignore Error", nullptr, message);
      if (v==0) continue;     // Continue Editing
      //if (v==1) { }         // Ignore Error and close dialog
    }
    // write to variables
    name(code_before_input->value());
    storestring(code_after_input->value(), after);
    break;
  }
BREAK2:
  codeblock_panel->hide();
}

/**
 Write the "before" code.
 */
void CodeBlock_Node::write_code1(fld::io::Code_Writer& f) {
  const char* c = name();
  f.write_c("%s%s {\n", f.indent(), c ? c : "");
  f.indentation++;
}

/**
 Write the "after" code.
 */
void CodeBlock_Node::write_code2(fld::io::Code_Writer& f) {
  f.indentation--;
  if (after) f.write_c("%s} %s\n", f.indent(), after);
  else f.write_c("%s}\n", f.indent());
}

// ---- Decl_Node declaration

/** \class Decl_Node
 Manage the C/C++ declaration of a variable.

 This node manages a single line of code that can be in the header or the source
 code, and can be made static.

 \todo this node could support multiple lines.
 */

/// Prototype for a declaration to be used by the factory.
Decl_Node Decl_Node::prototype;

/**
 Constructor.
 */
Decl_Node::Decl_Node() :
  public_(0),
  static_(1)
{ }

/**
 Return 1 if this declaration and its parents are public.
 */
int Decl_Node::is_public() const
{
  Node *p = parent;
  while (p && !p->is_decl_block()) p = p->parent;
  if(p && p->is_public() && public_)
    return public_;
  else if(!p)
    return public_;
  return 0;
}

/**
 Make a new declaration.
 \param[in] strategy add after current or as last child
 \return new Declaration node
 */
Node *Decl_Node::make(Strategy strategy) {
  Node *anchor = Fluid.proj.tree.current, *p = anchor;
  if (p && (strategy.placement() == Strategy::AFTER_CURRENT))
    p = p->parent;
  while (p && !p->is_decl_block()) {
    anchor = p;
    strategy.placement(Strategy::AFTER_CURRENT);
    p = p->parent;
  }
  Decl_Node *o = new Decl_Node();
  o->public_ = 0;
  o->static_ = 1;
  o->name("int x;");
  o->add(anchor, strategy);
  o->factory = this;
  return o;
}

/**
 Write the specific properties.
  - "private"/"public"/"protected"
  - "local"/"global" if this is static or not
 */
void Decl_Node::write_properties(fld::io::Project_Writer &f) {
  Node::write_properties(f);
  switch (public_) {
    case 0: f.write_string("private"); break;
    case 1: f.write_string("public"); break;
    case 2: f.write_string("protected"); break;
  }
  if (static_)
    f.write_string("local");
  else
    f.write_string("global");
}

/**
 Read the specific properties.
 */
void Decl_Node::read_property(fld::io::Project_Reader &f, const char *c) {
  if (!strcmp(c,"public")) {
    public_ = 1;
  } else if (!strcmp(c,"private")) {
    public_ = 0;
  } else if (!strcmp(c,"protected")) {
    public_ = 2;
  } else if (!strcmp(c,"local")) {
    static_ = 1;
  } else if (!strcmp(c,"global")) {
    static_ = 0;
  } else {
    Node::read_property(f, c);
  }
}

/**
 Open the decl_panel to edit this node.
 */
void Decl_Node::open() {
  if (!decl_panel) make_decl_panel();
  decl_input->buffer()->text(name());
  if (is_in_class()) {
    decl_class_choice->value(public_);
    decl_class_choice->show();
    decl_choice->hide();
  } else {
    decl_choice->value((public_&1)|((static_&1)<<1));
    decl_choice->show();
    decl_class_choice->hide();
  }
  const char *c = comment();
  decl_comment_input->buffer()->text(c?c:"");
  decl_panel->show();
  const char* message = nullptr;
  for (;;) { // repeat as long as there are errors
    // event loop
    for (;;) {
      Fl_Widget* w = Fl::readqueue();
      if (w == decl_panel_cancel) goto BREAK2;
      else if (w == decl_panel_ok) break;
      else if (!w) Fl::wait();
    }
    // check values
    const char*c = decl_input->buffer()->text();
    while (isspace(*c)) c++;
    message = c_check(c&&c[0]=='#' ? c+1 : c);
    // alert user
    if (message) {
      int v = fl_choice("Potential syntax error detected: %s",
                        "Continue Editing", "Ignore Error", nullptr, message);
      if (v==0) continue;     // Continue Editing
      //if (v==1) { }         // Ignore Error and close dialog
    }
    // copy vlaues
    name(c);
    if (is_in_class()) {
      if (public_!=decl_class_choice->value()) {
        Fluid.proj.set_modflag(1);
        public_ = decl_class_choice->value();
      }
    } else {
      if (public_!=(decl_choice->value()&1)) {
        Fluid.proj.set_modflag(1);
        public_ = (decl_choice->value()&1);
      }
      if (static_!=((decl_choice->value()>>1)&1)) {
        Fluid.proj.set_modflag(1);
        static_ = ((decl_choice->value()>>1)&1);
      }
    }
    c = decl_comment_input->buffer()->text();
    if (c && *c) {
      if (!comment() || strcmp(c, comment()))  { Fluid.proj.set_modflag(1); redraw_browser(); }
      comment(c);
    } else {
      if (comment())  { Fluid.proj.set_modflag(1); redraw_browser(); }
      comment(nullptr);
    }
    if (c) free((void*)c);
    break;
  }
BREAK2:
  decl_panel->hide();
}

/**
 Write the code to the source and header files.
 \todo There are a lot of side effect in this node depending on the given text
    and the parent node. They need to be understood and documented.
 */
void Decl_Node::write_code1(fld::io::Code_Writer& f) {
  const char* c = name();
  if (!c) return;
  // handle a few keywords differently if inside a class
  if (is_in_class() && (   (!strncmp(c,"class",5) && isspace(c[5]))
                        || (!strncmp(c,"typedef",7) && isspace(c[7]))
                        || (!strncmp(c,"FL_EXPORT",9) && isspace(c[9]))
                        || (!strncmp(c,"struct",6) && isspace(c[6]))
                        || (!strncmp(c,"enum",4) && isspace(c[4]))
                        ) ) {
    f.write_public(public_);
    write_comment_h(f, f.indent(1));
    f.write_h("%s%s\n", f.indent(1), c);
    return;
  }
  // handle putting #include, extern, using or typedef into decl:
  if (   (!isalpha(*c) && *c != '~')
      || (!strncmp(c,"extern",6) && isspace(c[6]))
      || (!strncmp(c,"class",5) && isspace(c[5]))
      || (!strncmp(c,"typedef",7) && isspace(c[7]))
      || (!strncmp(c,"using",5) && isspace(c[5]))
      || (!strncmp(c,"FL_EXPORT",9) && isspace(c[9]))
      //    || !strncmp(c,"struct",6) && isspace(c[6])
      ) {
    if (public_) {
      write_comment_h(f);
      f.write_h("%s\n", c);
    } else {
      write_comment_c(f);
      f.write_c("%s\n", c);
    }
    return;
  }
  // find the first C++ style comment
  const char* e = c+strlen(c), *csc = c;
  while (csc<e && (csc[0]!='/' || csc[1]!='/')) csc++;
  if (csc!=e) e = csc; // comment found
  // lose spaces between text and comment, if any
  while (e>c && e[-1]==' ') e--;
  if (class_name(1)) {
    f.write_public(public_);
    write_comment_h(f, f.indent(1));
    f.write_hc(f.indent(1), int(e-c), c, csc);
  } else {
    if (public_) {
      if (static_)
        f.write_h("extern ");
      else
        write_comment_h(f);
      f.write_hc("", int(e-c), c, csc);

      if (static_) {
        write_comment_c(f);
        f.write_cc("", int(e-c), c, csc);
      }
    } else {
      write_comment_c(f);
      if (static_)
        f.write_c("static ");
      f.write_cc("", int(e-c), c, csc);
    }
  }
}

// ---- Data_Node declaration

/** \class Data_Node
 Manage data from an external arbitrary file.

 The content of the file will be stored in binary inside the generated
 code. This can be used to store images inline in the source code,
 */

/// Prototype for a data node to be used by the factory.
Data_Node Data_Node::prototype;

/**
 Constructor.
 */
Data_Node::Data_Node() :
  Decl_Node(),
  filename_(nullptr),
  text_mode_(0)
{ }

/**
 Destructor.
 */
Data_Node::~Data_Node() {
  if (filename_)
    free((void*)filename_);
}

/**
 Create an empty inline data node.
 \param[in] strategy add after current or as last child
 \return new inline data node
 */
Node *Data_Node::make(Strategy strategy) {
  Node *anchor = Fluid.proj.tree.current, *p = anchor;
  if (p && (strategy.placement() == Strategy::AFTER_CURRENT))
    p = p->parent;
  while (p && !p->is_decl_block()) {
    anchor = p;
    strategy.placement(Strategy::AFTER_CURRENT);
    p = p->parent;
  }
  Data_Node *o = new Data_Node();
  o->public_ = 1;
  o->static_ = 1;
  o->filename_ = nullptr;
  o->text_mode_ = 0;
  o->name("myInlineData");
  o->add(anchor, strategy);
  o->factory = this;
  return o;
}

/**
 Write additional properties.
  - "filename" followed by the filename of the file to inline
  - "textmode" if data is written in ASCII vs. binary
 */
void Data_Node::write_properties(fld::io::Project_Writer &f) {
  Decl_Node::write_properties(f);
  if (filename_) {
    f.write_string("filename");
    f.write_word(filename_);
  }
  if (text_mode_ == 1) {
    f.write_string("textmode");
  }
  if (text_mode_ == 2) {
    f.write_string("compressed");
  }
}

/**
 Read specific properties.
 */
void Data_Node::read_property(fld::io::Project_Reader &f, const char *c) {
  if (!strcmp(c,"filename")) {
    storestring(f.read_word(), filename_, 1);
  } else if (!strcmp(c,"textmode")) {
    text_mode_ = 1;
  } else if (!strcmp(c,"compressed")) {
    text_mode_ = 2;
  } else {
    Decl_Node::read_property(f, c);
  }
}

/**
 Open the data_panel to edit this node.
 */
void Data_Node::open() {
  if (!data_panel) make_data_panel();
  data_input->value(name());
  if (is_in_class()) {
    data_class_choice->value(public_);
    data_class_choice->show();
    data_choice->hide();
  } else {
    data_choice->value((public_&1)|((static_&1)<<1));
    data_choice->show();
    data_class_choice->hide();
  }
  data_mode->value(text_mode_);
  data_filename->value(filename_?filename_:"");
  const char *c = comment();
  data_comment_input->buffer()->text(c?c:"");
  data_panel->show();
  for (;;) { // repeat as long as there are errors
    for (;;) {
      Fl_Widget* w = Fl::readqueue();
      if (w == data_panel_cancel) goto BREAK2;
      else if (w == data_panel_ok) break;
      else if (w == data_filebrowser) {
        Fluid.proj.enter_project_dir();
        const char *fn = fl_file_chooser("Load Inline Data", nullptr, data_filename->value(), 1);
        Fluid.proj.leave_project_dir();
        if (fn) {
          if (strcmp(fn, data_filename->value()))
            Fluid.proj.set_modflag(1);
          data_filename->value(fn);
        }
      }
      else if (!w) Fl::wait();
    }
    // store the variable name:
    const char*c = data_input->value();
    char *s = fl_strdup(c), *p = s, *q, *n;
    for (;;++p) { // remove leading spaces
      if (!isspace((unsigned char)(*p))) break;
    }
    n = p;
    if ( (!isalpha((unsigned char)(*p))) && ((*p)!='_') && ((*p)!=':') ) goto OOPS;
    ++p;
    for (;;++p) {
      if ( (!isalnum((unsigned char)(*p))) && ((*p)!='_') && ((*p)!=':') ) break;
    }
    q = p;
    for (;;++q) {
      if (!*q) break;
      if (!isspace((unsigned char)(*q))) goto OOPS;
    }
    *p = 0; // remove trailing spaces
    if (n==q) {
    OOPS:
      int v = fl_choice("%s",
                        "Continue Editing", "Ignore Error", nullptr,
                        "Variable name must be a C identifier");
      if (v==0) { free(s); continue; }    // Continue Editing
      //if (v==1) { }                     // Ignore Error and close dialog
    }
    Fluid.proj.undo.checkpoint();
    name(n);
    free(s);
    // store flags
    if (is_in_class()) {
      if (public_!=data_class_choice->value()) {
        Fluid.proj.set_modflag(1);
        public_ = data_class_choice->value();
      }
    } else {
      if (public_!=(data_choice->value()&1)) {
        Fluid.proj.set_modflag(1);
        public_ = (data_choice->value()&1);
      }
      if (static_!=((data_choice->value()>>1)&1)) {
        Fluid.proj.set_modflag(1);
        static_ = ((data_choice->value()>>1)&1);
      }
    }
    text_mode_ = data_mode->value();
    if (text_mode_ < 0) text_mode_ = 0;
    if (text_mode_ > 2) text_mode_ = 2;
    // store the filename
    c = data_filename->value();
    if (filename_ && strcmp(filename_, data_filename->value()))
      Fluid.proj.set_modflag(1);
    else if (!filename_ && *c)
      Fluid.proj.set_modflag(1);
    if (filename_) { free((void*)filename_); filename_ = nullptr; }
    if (c && *c) filename_ = fl_strdup(c);
    // store the comment
    c = data_comment_input->buffer()->text();
    if (c && *c) {
      if (!comment() || strcmp(c, comment()))  { Fluid.proj.set_modflag(1); redraw_browser(); }
      comment(c);
    } else {
      if (comment())  { Fluid.proj.set_modflag(1); redraw_browser(); }
      comment(nullptr);
    }
    if (c) free((void*)c);
    Fluid.proj.set_modflag(1);
    break;
  }
BREAK2:
  data_panel->hide();
}

/**
 Write the content of the external file inline into the source code.
 */
void Data_Node::write_code1(fld::io::Code_Writer& f) {
  const char *message = nullptr;
  const char *c = name();
  if (!c) return;
  const char *fn = filename_;
  char *data = nullptr;
  int nData = -1;
  int uncompressedDataSize = 0;
  // path should be set correctly already
  if (filename_ && !f.write_codeview) {
    Fluid.proj.enter_project_dir();
    FILE *f = fl_fopen(filename_, "rb");
    Fluid.proj.leave_project_dir();
    if (!f) {
      message = "Can't include data from file. Can't open";
    } else {
      fseek(f, 0, SEEK_END);
      nData = (int)ftell(f);
      fseek(f, 0, SEEK_SET);
      if (nData) {
        data = (char*)calloc(nData, 1);
        if (fread(data, nData, 1, f)==0) { /* use default */ }
        if (text_mode_ == 2) {
          uncompressedDataSize = nData;
          uLong nzData = compressBound(nData);
          Bytef *zdata = (Bytef*)::malloc(nzData);
          if (compress(zdata, &nzData, (Bytef*)data, nData) != Z_OK) { /* error */ }
          ::free(data);
          data = (char*)zdata;
          nData = (int)nzData;
        }
      }
      fclose(f);
    }
  } else {
    fn = filename_ ? filename_ : "<no filename>";
  }
  if (is_in_class()) {
    f.write_public(public_);
    if (text_mode_ == 1) {
      f.write_h("%sstatic const char *%s;\n", f.indent(1), c);
      f.write_c("\n");
      write_comment_c(f);
      f.write_c("const char *%s::%s = /* text inlined from %s */\n", class_name(1), c, fn);
      if (message) f.write_c("#error %s %s\n", message, fn);
      f.write_cstring(data, nData);
    } else if (text_mode_ == 2) {
      f.write_h("%sstatic int %s_size;\n", f.indent(1), c);
      f.write_h("%sstatic unsigned char %s[%d];\n", f.indent(1), c, nData);
      f.write_c("\n");
      write_comment_c(f);
      f.write_c("int %s::%s_size = %d;\n", class_name(1), c, uncompressedDataSize);
      f.write_c("unsigned char %s::%s[%d] = /* data compressed and inlined from %s */\n", class_name(1), c, nData, fn);
      if (message) f.write_c("#error %s %s\n", message, fn);
      f.write_cdata(data, nData);
    } else {
      f.write_h("%sstatic unsigned char %s[%d];\n", f.indent(1), c, nData);
      f.write_c("\n");
      write_comment_c(f);
      f.write_c("unsigned char %s::%s[%d] = /* data inlined from %s */\n", class_name(1), c, nData, fn);
      if (message) f.write_c("#error %s %s\n", message, fn);
      f.write_cdata(data, nData);
    }
    f.write_c(";\n");
  } else {
    // the "header only" option does not apply here!
    if (public_) {
      if (static_) {
        if (text_mode_ == 1) {
          f.write_h("extern const char *%s;\n", c);
          f.write_c("\n");
          write_comment_c(f);
          f.write_c("const char *%s = /* text inlined from %s */\n", c, fn);
          if (message) f.write_c("#error %s %s\n", message, fn);
          f.write_cstring(data, nData);
        } else if (text_mode_ == 2) {
          f.write_h("extern int %s_size;\n", c);
          f.write_h("extern unsigned char %s[%d];\n", c, nData);
          f.write_c("\n");
          write_comment_c(f);
          f.write_c("int %s_size = %d;\n", c, uncompressedDataSize);
          f.write_c("unsigned char %s[%d] = /* data compressed and inlined from %s */\n", c, nData, fn);
          if (message) f.write_c("#error %s %s\n", message, fn);
          f.write_cdata(data, nData);
        } else {
          f.write_h("extern unsigned char %s[%d];\n", c, nData);
          f.write_c("\n");
          write_comment_c(f);
          f.write_c("unsigned char %s[%d] = /* data inlined from %s */\n", c, nData, fn);
          if (message) f.write_c("#error %s %s\n", message, fn);
          f.write_cdata(data, nData);
        }
        f.write_c(";\n");
      } else {
        write_comment_h(f);
        f.write_h("#error Unsupported declaration loading inline data %s\n", fn);
        if (text_mode_ == 1)
          f.write_h("const char *%s = \"abc...\";\n", c);
        else
          f.write_h("unsigned char %s[3] = { 1, 2, 3 };\n", c);
      }
    } else {
      f.write_c("\n");
      write_comment_c(f);
      if (static_)
        f.write_c("static ");
      if (text_mode_ == 1) {
        f.write_c("const char *%s = /* text inlined from %s */\n", c, fn);
        if (message) f.write_c("#error %s %s\n", message, fn);
        f.write_cstring(data, nData);
      } else if (text_mode_ == 2) {
        f.write_c("int %s_size = %d;\n", c, uncompressedDataSize);
        if (static_) f.write_c("static ");
        f.write_c("unsigned char %s[%d] = /* data compressed and inlined from %s */\n", c, nData, fn);
        if (message) f.write_c("#error %s %s\n", message, fn);
        f.write_cdata(data, nData);
      } else {
        f.write_c("unsigned char %s[%d] = /* data inlined from %s */\n", c, nData, fn);
        if (message) f.write_c("#error %s %s\n", message, fn);
        f.write_cdata(data, nData);
      }
      f.write_c(";\n");
    }
  }
  // if we are in interactive mode, we pop up a warning dialog
  // giving the error: (Fluid.batch_mode && !write_codeview) ???
  if (message && !f.write_codeview) {
    if (Fluid.batch_mode)
      fprintf(stderr, "FLUID ERROR: %s %s\n", message, fn);
    else
      fl_alert("%s\n%s\n", message, fn);
  }
  if (data) free(data);
}

// ---- DeclBlock_Node declaration

/** \class DeclBlock_Node
 Manage a declaration block.

 Declaration blocks have two text field that are written before and after
 the children of this block. This block is located at the top level and
 is written to the source file, and to the header file, if declared public.
 */

/// Prototype for a declaration block to be used by the factory.
DeclBlock_Node DeclBlock_Node::prototype;

/**
 Constructor.
 */
DeclBlock_Node::DeclBlock_Node() :
  Node(),
  after(nullptr),
  write_map_(CODE_IN_SOURCE)
{ }

/**
 Destructor.
 */
DeclBlock_Node::~DeclBlock_Node() {
  if (after)
    ::free((void*)after);
}

/**
 Return 1 if this block is public.
 */
int DeclBlock_Node::is_public() const {
  return ((write_map_&CODE_IN_HEADER) != 0);
}

/**
 Create a new declaration block.
 \param[in] strategy add after current or as last child
 \return new Declaration Block node
 */
Node *DeclBlock_Node::make(Strategy strategy) {
  Node *anchor = Fluid.proj.tree.current, *p = anchor;
  if (p && (strategy.placement() == Strategy::AFTER_CURRENT)) p = p->parent;
  while (p && !p->is_decl_block()) {
    anchor = p;
    strategy.placement(Strategy::AFTER_CURRENT);
    p = p->parent;
  }
  DeclBlock_Node *o = new DeclBlock_Node();
  o->name("#if 1");
  o->write_map_ = CODE_IN_SOURCE;
  o->after = fl_strdup("#endif");
  o->add(anchor, strategy);
  o->factory = this;
  return o;
}

/**
 Write the specific properties.
  - "public"/"protected"
  - "after" followed by the second code block.
 */
void DeclBlock_Node::write_properties(fld::io::Project_Writer &f) {
  Node::write_properties(f);
  // deprecated
  if (is_public()) f.write_string("public");
  // new way to map declaration block to various parts of the generated code
  if (write_map_ != CODE_IN_SOURCE)
    f.write_string("map %d", write_map_);
  f.write_string("after");
  f.write_word(after);
}

/**
 Read the specific properties.
 */
void DeclBlock_Node::read_property(fld::io::Project_Reader &f, const char *c) {
  if(!strcmp(c,"public")) {
    write_map_ |= CODE_IN_HEADER;
  } else if(!strcmp(c,"protected")) {
    //
  } else if(!strcmp(c,"map")) {
    write_map_ = (int)atol(f.read_word());
  } else  if (!strcmp(c,"after")) {
    storestring(f.read_word(),after);
  } else {
    Node::read_property(f, c);
  }
}

/**
 Open the declblock_panel to edit this node.
 */
void DeclBlock_Node::open() {
  // build dialog box
  if (!declblock_panel) make_declblock_panel();
  // preset all values
  declblock_before_input->value(name());
  declblock_after_input->value(after);
  declblock_static_header->value(write_map_ & STATIC_IN_HEADER);
  declblock_static_source->value(write_map_ & STATIC_IN_SOURCE);
  declblock_code_header->value(write_map_ & CODE_IN_HEADER);
  declblock_code_source->value(write_map_ & CODE_IN_SOURCE);
  const char *c = comment();
  declblock_comment_input->buffer()->text(c?c:"");
  // show modal dialog and loop until satisfied
  declblock_panel->show();
  const char* message = nullptr;
  for (;;) { // repeat as long as there are errors
    for (;;) {
      Fl_Widget* w = Fl::readqueue();
      if (w == declblock_panel_cancel) goto BREAK2;
      else if (w == declblock_panel_ok) break;
      else if (!w) Fl::wait();
    }
    // verify user input
    const char* a = declblock_before_input->value();
    while (isspace(*a)) a++;
    const char* b = declblock_after_input->value();
    while (isspace(*b)) b++;
    message = c_check(a&&a[0]=='#' ? a+1 : a);
    if (!message)
      message = c_check(b&&b[0]=='#' ? b+1 : b);
    if (message) {
      int v = fl_choice("Potential syntax error detected: %s",
                        "Continue Editing", "Ignore Error", nullptr, message);
      if (v==0) continue;     // Continue Editing
      //if (v==1) { }         // Ignore Error and close dialog
    }
    // store user choices in data structure
    name(a);
    storestring(b, after);
    if (write_map_ & STATIC_IN_HEADER) {
      if (declblock_static_header->value()==0) {
        write_map_ &= ~STATIC_IN_HEADER;
        Fluid.proj.set_modflag(1);
      }
    } else {
      if (declblock_static_header->value()) {
        write_map_ |= STATIC_IN_HEADER;
        Fluid.proj.set_modflag(1);
      }
    }
    if (write_map_ & STATIC_IN_SOURCE) {
      if (declblock_static_source->value()==0) {
        write_map_ &= ~STATIC_IN_SOURCE;
        Fluid.proj.set_modflag(1);
      }
    } else {
      if (declblock_static_source->value()) {
        write_map_ |= STATIC_IN_SOURCE;
        Fluid.proj.set_modflag(1);
      }
    }
    if (write_map_ & CODE_IN_HEADER) {
      if (declblock_code_header->value()==0) {
        write_map_ &= ~CODE_IN_HEADER;
        Fluid.proj.set_modflag(1);
      }
    } else {
      if (declblock_code_header->value()) {
        write_map_ |= CODE_IN_HEADER;
        Fluid.proj.set_modflag(1);
      }
    }
    if (write_map_ & CODE_IN_SOURCE) {
      if (declblock_code_source->value()==0) {
        write_map_ &= ~CODE_IN_SOURCE;
        Fluid.proj.set_modflag(1);
      }
    } else {
      if (declblock_code_source->value()) {
        write_map_ |= CODE_IN_SOURCE;
        Fluid.proj.set_modflag(1);
      }
    }
    c = declblock_comment_input->buffer()->text();
    if (c && *c) {
      if (!comment() || strcmp(c, comment())) { Fluid.proj.set_modflag(1); redraw_browser(); }
      comment(c);
    } else {
      if (comment()) { Fluid.proj.set_modflag(1); redraw_browser(); }
      comment(nullptr);
    }
    if (c) free((void*)c);
    break;
  }
BREAK2:
  declblock_panel->hide();
}

/**
 Write the \b before static code to the source file, and to the header file if declared public.
 The before code is stored in the name() field.
 */
void DeclBlock_Node::write_static(fld::io::Code_Writer& f) {
  const char* c = name();
  if (c && *c) {
    if (write_map_ & STATIC_IN_HEADER)
      f.write_h("%s\n", c);
    if (write_map_ & STATIC_IN_SOURCE)
      f.write_c("%s\n", c);
  }
}

/**
 Write the \b after static code to the source file, and to the header file if declared public.
 */
void DeclBlock_Node::write_static_after(fld::io::Code_Writer& f) {
  const char* c = after;
  if (c && *c) {
    if (write_map_ & STATIC_IN_HEADER)
      f.write_h("%s\n", c);
    if (write_map_ & STATIC_IN_SOURCE)
      f.write_c("%s\n", c);
  }
}

/**
 Write the \b before code to the source file, and to the header file if declared public.
 The before code is stored in the name() field.
 */
void DeclBlock_Node::write_code1(fld::io::Code_Writer& f) {
  const char* c = name();
  if (c && *c) {
    if (write_map_ & CODE_IN_HEADER)
      f.write_h("%s\n", c);
    if (write_map_ & CODE_IN_SOURCE)
      f.write_c("%s\n", c);
  }
}

/**
 Write the \b after code to the source file, and to the header file if declared public.
 */
void DeclBlock_Node::write_code2(fld::io::Code_Writer& f) {
  const char* c = after;
  if (c && *c) {
    if (write_map_ & CODE_IN_HEADER)
      f.write_h("%s\n", c);
    if (write_map_ & CODE_IN_SOURCE)
      f.write_c("%s\n", c);
  }
}

// ---- Comment_Node declaration

/** \class Comment_Node
 Manage a comment node.

 The comment field takes one or more lines of ASCII text. If the text starts
 with a '/' and a '*', Fluid assumes that the text is already formatted. If not,
 every line will be preceded with "// ".
 */

/// Prototype for a comment node to be used by the factory.
Comment_Node Comment_Node::prototype;

/**
 Constructor.
 */
Comment_Node::Comment_Node() :
  in_c_(1),
  in_h_(1),
  style_(0)
{ }

/**
 Make a new comment node.
 \param[in] strategy add after current or as last child
 \return new Comment node
 */
Node *Comment_Node::make(Strategy strategy) {
  Node *anchor = Fluid.proj.tree.current, *p = anchor;
  if (p && (strategy.placement() == Strategy::AFTER_CURRENT))
    p = p->parent;
  while (p && !p->is_code_block()) {
    anchor = p;
    strategy.placement(Strategy::AFTER_CURRENT);
    p = p->parent;
  }
  Comment_Node *o = new Comment_Node();
  o->in_c_ = 1;
  o->in_h_ = 1;
  o->style_ = 0;
  o->name("my comment");
  o->add(anchor, strategy);
  o->factory = this;
  return o;
}

/**
 Write respective properties.
  - "in_source"/"not_in_source" if the comment will be written to the source code
  - "in_header"/"not_in_header" if the comment will be written to the header file
 */
void Comment_Node::write_properties(fld::io::Project_Writer &f) {
  Node::write_properties(f);
  if (in_c_) f.write_string("in_source"); else f.write_string("not_in_source");
  if (in_h_) f.write_string("in_header"); else f.write_string("not_in_header");
}

/**
 Read extra properties.
 */
void Comment_Node::read_property(fld::io::Project_Reader &f, const char *c) {
  if (!strcmp(c,"in_source")) {
    in_c_ = 1;
  } else if (!strcmp(c,"not_in_source")) {
    in_c_ = 0;
  } else if (!strcmp(c,"in_header")) {
    in_h_ = 1;
  } else if (!strcmp(c,"not_in_header")) {
    in_h_ = 0;
  } else {
    Node::read_property(f, c);
  }
}

/**
 Load available preset comments.
 Fluid comes with GPL and LGPL preset for comments. Users can
 add their own presets which are stored per user in a separate
 preferences database.
 */
static void load_comments_preset(Fl_Preferences &menu) {
  static const char * const predefined_comment[] = {
    "GNU Public License v3/GPL Header",  "GNU Public License v3/GPL Footer",
    "GNU Public License v3/LGPL Header", "GNU Public License v3/LGPL Footer",
    "FLTK/Header" };
  int i, n;
  menu.get("n", n, -1);
  if (n == -1) menu.set("n", 5);
  menu.set("version", 10400);
  Fl_Preferences db(Fl_Preferences::USER_L, "fltk.org", "fluid_comments");
  for (i=0; i<5; i++) {
    menu.set(Fl_Preferences::Name(i), predefined_comment[i]);
    db.set(predefined_comment[i], comment_text[i]);
  }
}

/**
 Open the comment_panel to edit this node.
 */
void Comment_Node::open() {
  if (!comment_panel) make_comment_panel();
  const char *text = name();
  {
    int i=0, n=0, version = 0;
    Fl_Preferences menu(Fl_Preferences::USER_L, "fltk.org", "fluid_comments_menu");
    comment_predefined->clear();
    comment_predefined->add("_Edit/Add current comment...");
    comment_predefined->add("_Edit/Remove last selection...");
    menu.get("version", version, -1);
    if (version < 10400) load_comments_preset(menu);
    menu.get("n", n, 0);
    for (i=0;i<n;i++) {
      char *text;
      menu.get(Fl_Preferences::Name(i), text, "");
      comment_predefined->add(text);
      free(text);
    }
  }
  comment_input->buffer()->text( text ? text : "" );
  comment_in_source->value(in_c_);
  comment_in_header->value(in_h_);
  comment_panel->show();
  char itempath[FL_PATH_MAX]; itempath[0] = 0;
  int last_selected_item = 0;
  for (;;) { // repeat as long as there are errors
    for (;;) {
      Fl_Widget* w = Fl::readqueue();
      if (w == comment_panel_cancel) goto BREAK2;
      else if (w == comment_panel_ok) break;
      else if (w == comment_predefined) {
        if (comment_predefined->value()==1) {
          // add the current comment to the database
          const char *xname = fl_input(
                                       "Please enter a name to reference the current\ncomment in your database.\n\n"
                                       "Use forward slashes '/' to create submenus.",
                                       "My Comment");
          if (xname) {
            char *name = fl_strdup(xname);
            for (char*s=name;*s;s++) if (*s==':') *s = ';';
            int n;
            Fl_Preferences db(Fl_Preferences::USER_L, "fltk.org", "fluid_comments");
            db.set(name, comment_input->buffer()->text());
            Fl_Preferences menu(Fl_Preferences::USER_L, "fltk.org", "fluid_comments_menu");
            menu.get("n", n, 0);
            menu.set(Fl_Preferences::Name(n), name);
            menu.set("n", ++n);
            comment_predefined->add(name);
            free(name);
          }
        } else if (comment_predefined->value()==2) {
          // remove the last selected comment from the database
          if (itempath[0]==0 || last_selected_item==0) {
            fl_message("Please select an entry from this menu first.");
          } else if (fl_choice("Are you sure that you want to delete the entry\n"
                               "\"%s\"\nfrom the database?", "Cancel", "Delete",
                               nullptr, itempath)) {
            Fl_Preferences db(Fl_Preferences::USER_L, "fltk.org", "fluid_comments");
            db.deleteEntry(itempath);
            comment_predefined->remove(last_selected_item);
            Fl_Preferences menu(Fl_Preferences::USER_L, "fltk.org", "fluid_comments_menu");
            int i, n;
            for (i=4, n=0; i<comment_predefined->size(); i++) {
              const Fl_Menu_Item *mi = comment_predefined->menu()+i;
              if (comment_predefined->item_pathname(itempath, 255, mi)==0) {
                if (itempath[0]=='/') memmove(itempath, itempath+1, 255);
                if (itempath[0]) menu.set(Fl_Preferences::Name(n++), itempath);
              }
            }
            menu.set("n", n);
          }
        } else {
          // load the selected comment from the database
          if (comment_predefined->item_pathname(itempath, 255)==0) {
            if (itempath[0]=='/') memmove(itempath, itempath+1, 255);
            Fl_Preferences db(Fl_Preferences::USER_L, "fltk.org", "fluid_comments");
            char *text;
            db.get(itempath, text, "(no text found in data base)");
            comment_input->buffer()->text(text);
            free(text);
            last_selected_item = comment_predefined->value();
          }
        }
      }
      else if (w == comment_load) {
        // load a comment from disk
        fl_file_chooser_ok_label("Use File");
        const char *fname = fl_file_chooser("Pick a comment", nullptr, nullptr);
        fl_file_chooser_ok_label(nullptr);
        if (fname) {
          if (comment_input->buffer()->loadfile(fname)) {
            fl_alert("Error loading file\n%s", fname);
          }
        }
      }
      else if (!w) Fl::wait();
    }
    char*c = comment_input->buffer()->text();
    name(c);
    free(c);
    int mod = 0;
    if (in_c_ != comment_in_source->value()) {
      in_c_ = comment_in_source->value();
      mod = 1;
    }
    if (in_h_ != comment_in_header->value()) {
      in_h_ = comment_in_header->value();
      mod = 1;
    }
    if (mod) Fluid.proj.set_modflag(1);
    break;
  }
BREAK2:
  comment_panel->hide();
}

/**
 Write the comment to the files.
 */
void Comment_Node::write_code1(fld::io::Code_Writer& f) {
  const char* c = name();
  if (!c) return;
  if (!in_c_ && !in_h_) return;
  // find out if there is already a valid comment:
  const char *s = c;
  while (isspace(*s)) s++;
  // if this seems to be a C style comment, copy the block as is
  // (it's up to the user to correctly close the comment)
  if (s[0]=='/' && s[1]=='*') {
    if (in_h_) f.write_h("%s\n", c);
    if (in_c_) f.write_c("%s\n", c);
    return;
  }
  // copy the comment line by line, add the double slash if needed
  char *txt = fl_strdup(c);
  char *b = txt, *e = txt;
  for (;;) {
    // find the end of the line and set it to NUL
    while (*e && *e!='\n') e++;
    char eol = *e;
    *e = 0;
    // check if there is a C++ style comment at the beginning of the line
    char *s = b;
    while (isspace(*s)) s++;
    if (s!=e && ( s[0]!='/' || s[1]!='/') ) {
      // if no comment marker was found, we add one ourselves
      if (in_h_) f.write_h("// ");
      if (in_c_) f.write_c("// ");
    }
    // now copy the rest of the line
    if (in_h_) f.write_h("%s\n", b);
    if (in_c_) f.write_c("%s\n", b);
    if (eol==0) break;
    *e++ = eol;
    b = e;
  }
  free(txt);
}

// ---- Class_Node declaration

/** \class Class_Node
 Manage a class declaration and implementation.
 */

/// Prototype for a class node to be used by the factory.
Class_Node Class_Node::prototype;

/**
 Constructor.
 */
Class_Node::Class_Node() :
  Node(),
  subclass_of(nullptr),
  public_(1),
  class_prefix(nullptr)
{ }

/**
 Destructor.
 */
Class_Node::~Class_Node() {
  if (subclass_of)
    free((void*)subclass_of);
  if (class_prefix)
    free((void*)class_prefix);
}

/**
 Return 1 if this class is marked public.
 */
int Class_Node::is_public() const {
  return public_;
}

/**
 Set the prefixx string.
 */
void Class_Node::prefix(const char*p) {
  free((void*) class_prefix);
  class_prefix=fl_strdup(p ? p : "" );
}

/**
 Make a new class node.
 \param[in] strategy add after current or as last child
 \return new Class node
 */
Node *Class_Node::make(Strategy strategy) {
  Node *anchor = Fluid.proj.tree.current, *p = anchor;
  if (p && (strategy.placement() == Strategy::AFTER_CURRENT))
    p = p->parent;
  while (p && !p->is_decl_block()) {
    anchor = p;
    strategy.placement(Strategy::AFTER_CURRENT);
    p = p->parent;
  }
  Class_Node *o = new Class_Node();
  o->name("UserInterface");
  o->class_prefix = nullptr;
  o->subclass_of = nullptr;
  o->public_ = 1;
  o->add(anchor, strategy);
  o->factory = this;
  return o;
}

/**
 Write the respective properties.
  - ":" followed by the super class
  - "private"/"protected"
 */
void Class_Node::write_properties(fld::io::Project_Writer &f) {
  Node::write_properties(f);
  if (subclass_of) {
    f.write_string(":");
    f.write_word(subclass_of);
  }
  switch (public_) {
    case 0: f.write_string("private"); break;
    case 2: f.write_string("protected"); break;
  }
}

/**
 Read additional properties.
 */
void Class_Node::read_property(fld::io::Project_Reader &f, const char *c) {
  if (!strcmp(c,"private")) {
    public_ = 0;
  } else if (!strcmp(c,"protected")) {
    public_ = 2;
  } else if (!strcmp(c,":")) {
    storestring(f.read_word(), subclass_of);
  } else {
    Node::read_property(f, c);
  }
}

/**
 Open the class_panel to edit the class name and superclass name.
 */
void Class_Node::open() {
  if (!class_panel) make_class_panel();
  char fullname[FL_PATH_MAX]="";
  if (prefix() && strlen(prefix()))
    sprintf(fullname,"%s %s",prefix(),name());
  else
    strcpy(fullname, name());
  c_name_input->value(fullname);
  c_subclass_input->value(subclass_of);
  c_public_button->value(public_);
  const char *c = comment();
  c_comment_input->buffer()->text(c?c:"");
  class_panel->show();
  const char* message = nullptr;

  char *na=nullptr,*pr=nullptr,*p=nullptr; // name and prefix substrings

  for (;;) { // repeat as long as there are errors
    // we don;t give the option to ignore this error here because code depends
    // on this being a C++ identifier
    if (message) fl_alert("%s", message);
    for (;;) {
      Fl_Widget* w = Fl::readqueue();
      if (w == c_panel_cancel) goto BREAK2;
      else if (w == c_panel_ok) break;
      else if (!w) Fl::wait();
    }
    const char*c = c_name_input->value();
    char *s = fl_strdup(c);
    size_t len = strlen(s);
    if (!*s) goto OOPS;
    p = (char*) (s+len-1);
    while (p>=s && isspace(*p)) *(p--)='\0';
    if (p<s) goto OOPS;
    while (p>=s && is_id(*p)) p--;
    if ( (p<s && !is_id(*(p+1))) || !*(p+1) ) {
    OOPS: message = "class name must be C++ identifier";
      free((void*)s);
      continue;
    }
    na=p+1; // now we have the name
    if(p>s) *p--='\0';
    while (p>=s && isspace(*p)) *(p--)='\0';
    while (p>=s && is_id(*p))   p--;
    if (p<s)                    p++;
    if (is_id(*p) && p<na)      pr=p; // prefix detected
    c = c_subclass_input->value();
    message = c_check(c);
    if (message) { free((void*)s);continue;}
    name(na);
    prefix(pr);
    free((void*)s);
    storestring(c, subclass_of);
    if (public_ != c_public_button->value()) {
      public_ = c_public_button->value();
      Fluid.proj.set_modflag(1);
    }
    c = c_comment_input->buffer()->text();
    if (c && *c) {
      if (!comment() || strcmp(c, comment()))  { Fluid.proj.set_modflag(1); redraw_browser(); }
      comment(c);
    } else {
      if (comment())  { Fluid.proj.set_modflag(1); redraw_browser(); }
      comment(nullptr);
    }
    if (c) free((void*)c);
    break;
  }
BREAK2:
  class_panel->hide();
}

/**
 Write the header code that declares this class.
 */
void Class_Node::write_code1(fld::io::Code_Writer& f) {
  parent_class = current_class;
  current_class = this;
  write_public_state = 0;
  f.write_h("\n");
  write_comment_h(f);
  if (prefix() && strlen(prefix()))
    f.write_h("class %s %s ", prefix(), name());
  else
    f.write_h("class %s ", name());
  if (subclass_of) f.write_h(": %s ", subclass_of);
  f.write_h("{\n");
}

/**
 Write the header code that ends the declaration of this class.
 */
void Class_Node::write_code2(fld::io::Code_Writer& f) {
  f.write_h("};\n");
  current_class = parent_class;
}

/**
 Return 1 if this class contains a function with the given signature.
 */
int Node::has_function(const char *rtype, const char *sig) const {
  Node *child;
  for (child = next; child && child->level > level; child = child->next) {
    if (child->level == level+1 && child->is_a(Type::Function)) {
      const Function_Node *fn = (const Function_Node*)child;
      if (fn->has_signature(rtype, sig))
        return 1;
    }
  }
  return 0;
}