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
use crate::enums::{Align, Color, FrameType};
use crate::prelude::*;
use crate::utils::FlString;
use crate::widget::Widget;
use fltk_sys::group::*;
use std::{
    ffi::{CStr, CString},
    mem,
    os::raw::c_char,
    sync::atomic::{AtomicBool, Ordering},
};

static DEBUG: AtomicBool = AtomicBool::new(false);

/// Creates a group widget
#[derive(Debug)]
pub struct Group {
    inner: crate::widget::WidgetTracker,
    is_derived: bool,
}

crate::macros::widget::impl_widget_ext!(Group, Fl_Group);
crate::macros::widget::impl_widget_base!(Group, Fl_Group);
crate::macros::widget::impl_widget_default!(Group);
crate::macros::group::impl_group_ext!(Group, Fl_Group);

impl Group {
    #[deprecated(since = "1.2.18", note = "please use `try_current` instead")]
    /// Get the current group
    pub fn current() -> Group {
        unsafe {
            let ptr = Fl_Group_current();
            assert!(!ptr.is_null());
            Group::from_widget_ptr(ptr as _)
        }
    }

    /// Tries to get the current group
    pub fn try_current() -> Option<Group> {
        unsafe {
            let ptr = Fl_Group_current();
            if ptr.is_null() {
                None
            } else {
                Some(Group::from_widget_ptr(ptr as _))
            }
        }
    }

    /// Sets the current GroupExt widget which will take children
    pub fn set_current(grp: Option<&impl GroupExt>) {
        unsafe {
            if let Some(grp) = grp {
                Fl_Group_set_current(grp.as_widget_ptr() as _)
            } else {
                Fl_Group_set_current(std::ptr::null_mut())
            }
        }
    }
}

/// Creates a widget pack
#[derive(Debug)]
pub struct Pack {
    inner: crate::widget::WidgetTracker,
    is_derived: bool,
}

crate::macros::widget::impl_widget_ext!(Pack, Fl_Pack);
crate::macros::widget::impl_widget_base!(Pack, Fl_Pack);
crate::macros::widget::impl_widget_default!(Pack);
crate::macros::group::impl_group_ext!(Pack, Fl_Pack);

/// Defines pack types
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum PackType {
    /// Vertical layout pack
    Vertical = 0,
    /// Horizontal layout pack
    Horizontal = 1,
}

crate::macros::widget::impl_widget_type!(PackType);

impl Pack {
    /// Get the spacing of the pack
    pub fn spacing(&self) -> i32 {
        unsafe { Fl_Pack_spacing(self.inner.widget() as _) }
    }

    /// Set the spacing of the pack
    pub fn set_spacing(&mut self, spacing: i32) {
        unsafe {
            Fl_Pack_set_spacing(self.inner.widget() as _, spacing);
        }
    }

    /// Layout the children of the pack automatically.
    /// Must be called on existing children
    pub fn auto_layout(&mut self) {
        let children = self.children();
        if children == 0 {
            return;
        }
        let spacing = self.spacing() * (children - 1);
        let t = self.get_type::<PackType>();
        let w = (self.width() - spacing) / children;
        let h = (self.height() - spacing) / children;

        for i in 0..children {
            let mut c = self.child(i).unwrap();
            let c_w = c.width();
            let c_h = c.height();
            if t == PackType::Vertical {
                c.set_size(c_w, h);
            } else {
                c.set_size(w, c_h);
            }
        }
    }
}

/// Creates a scroll group
#[derive(Debug)]
pub struct Scroll {
    inner: crate::widget::WidgetTracker,
    is_derived: bool,
}

crate::macros::widget::impl_widget_ext!(Scroll, Fl_Scroll);
crate::macros::widget::impl_widget_base!(Scroll, Fl_Scroll);
crate::macros::widget::impl_widget_default!(Scroll);
crate::macros::group::impl_group_ext!(Scroll, Fl_Scroll);

/// Defines Scroll types
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ScrollType {
    /// Never show bars
    None = 0,
    /// Show vertical bar
    Horizontal = 1,
    /// Show vertical bar
    Vertical = 2,
    /// Show both horizontal and vertical bars
    Both = 3,
    /// Always show bars
    AlwaysOn = 4,
    /// Show horizontal bar always
    HorizontalAlways = 5,
    /// Show vertical bar always
    VerticalAlways = 6,
    /// Always show both horizontal and vertical bars
    BothAlways = 7,
}

crate::macros::widget::impl_widget_type!(ScrollType);

impl Scroll {
    /// Returns the vertical scrollbar
    pub fn scrollbar(&self) -> crate::valuator::Scrollbar {
        unsafe {
            let ptr = Fl_Scroll_scrollbar(self.inner.widget() as _);
            assert!(!ptr.is_null());
            crate::valuator::Scrollbar::from_widget_ptr(ptr as *mut fltk_sys::widget::Fl_Widget)
        }
    }

    /// Returns the horizontal scrollbar
    pub fn hscrollbar(&self) -> crate::valuator::Scrollbar {
        unsafe {
            let ptr = Fl_Scroll_hscrollbar(self.inner.widget() as _);
            assert!(!ptr.is_null());
            crate::valuator::Scrollbar::from_widget_ptr(ptr as *mut fltk_sys::widget::Fl_Widget)
        }
    }

    /// Returns the x position
    pub fn xposition(&self) -> i32 {
        unsafe { Fl_Scroll_xposition(self.inner.widget() as _) }
    }

    /// Returns the y position
    pub fn yposition(&self) -> i32 {
        unsafe { Fl_Scroll_yposition(self.inner.widget() as _) }
    }

    /// Scrolls to `x` and `y`
    pub fn scroll_to(&mut self, x: i32, y: i32) {
        unsafe { Fl_Scroll_scroll_to(self.inner.widget() as _, x, y) }
    }

    /// Gets the scrollbar size
    pub fn scrollbar_size(&self) -> i32 {
        unsafe { Fl_Scroll_scrollbar_size(self.inner.widget() as _) }
    }

    /// Sets the scrollbar size
    pub fn set_scrollbar_size(&mut self, new_size: i32) {
        unsafe { Fl_Scroll_set_scrollbar_size(self.inner.widget() as _, new_size) }
    }
}

/// Defines how Tabs handle overflow
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum TabsOverflow {
    /// Compress tabs
    Compress = 0,
    /// Clip tabs
    Clip,
    /// Create a pulldown
    Pulldown,
    /// Drag tabs
    Drag,
}

/// Creates a tab which can contain widgets
#[derive(Debug)]
pub struct Tabs {
    inner: crate::widget::WidgetTracker,
    is_derived: bool,
}

crate::macros::widget::impl_widget_ext!(Tabs, Fl_Tabs);
crate::macros::widget::impl_widget_base!(Tabs, Fl_Tabs);
crate::macros::widget::impl_widget_default!(Tabs);
crate::macros::group::impl_group_ext!(Tabs, Fl_Tabs);

impl Tabs {
    /// Gets the currently visible group
    pub fn value(&self) -> Option<impl GroupExt> {
        unsafe {
            let ptr = Fl_Tabs_value(self.inner.widget() as _);
            if ptr.is_null() {
                None
            } else {
                Some(Group::from_widget_ptr(
                    ptr as *mut fltk_sys::widget::Fl_Widget,
                ))
            }
        }
    }

    /// Sets the currently visible group
    /// # Errors
    /// Errors when the value can't be set for the group widget
    pub fn set_value<Grp: GroupExt>(&mut self, w: &Grp) -> Result<(), FltkError> {
        unsafe {
            match Fl_Tabs_set_value(
                self.inner.widget() as _,
                w.as_widget_ptr() as *mut fltk_sys::group::Fl_Widget,
            ) {
                0 => Err(FltkError::Internal(FltkErrorKind::FailedOperation)),
                _ => Ok(()),
            }
        }
    }

    /// Returns the tab group for the tab the user has currently down-clicked
    pub fn push(&self) -> Option<impl GroupExt> {
        unsafe {
            let ptr = Fl_Tabs_push(self.inner.widget() as _);
            if ptr.is_null() {
                None
            } else {
                Some(Group::from_widget_ptr(
                    ptr as *mut fltk_sys::widget::Fl_Widget,
                ))
            }
        }
    }

    /// This is called by the tab widget's handle() method to set the tab group widget the user last pushed
    /// # Errors
    /// Errors if `set_push` can't be set for the group widget
    pub fn set_push<Grp: GroupExt>(&mut self, w: &Grp) -> Result<(), FltkError> {
        unsafe {
            match Fl_Tabs_set_push(
                self.inner.widget() as _,
                w.as_widget_ptr() as *mut fltk_sys::group::Fl_Widget,
            ) {
                0 => Err(FltkError::Internal(FltkErrorKind::FailedOperation)),
                _ => Ok(()),
            }
        }
    }

    /// Returns the position and size available to be used by its children
    pub fn client_area(&self) -> (i32, i32, i32, i32) {
        unsafe {
            let mut i1 = 0;
            let mut i2 = 0;
            let mut i3 = 0;
            let mut i4 = 0;
            Fl_Tabs_client_area(self.inner.widget() as _, &mut i1, &mut i2, &mut i3, &mut i4);
            (i1, i2, i3, i4)
        }
    }

    /// Sets the tab label alignment
    pub fn set_tab_align(&mut self, a: Align) {
        unsafe { Fl_Tabs_set_tab_align(self.inner.widget() as _, a.bits()) }
    }

    /// Gets the tab label alignment.
    pub fn tab_align(&self) -> Align {
        unsafe { mem::transmute(Fl_Tabs_tab_align(self.inner.widget() as _)) }
    }

    /// Auto layout a tabs widget
    pub fn auto_layout(&mut self) {
        for c in self.clone().into_iter() {
            if let Some(mut c) = c.as_group() {
                c.resize(self.x(), self.y() + 30, self.w(), self.h() - 30);
            }
        }
        self.resize_callback(|t, x, y, w, h| {
            for c in t.clone().into_iter() {
                if let Some(mut c) = c.as_group() {
                    c.resize(x, y + 30, w, h - 30);
                }
            }
        });
    }

    /// Sets how the Tabs handles overflow
    pub fn handle_overflow(&mut self, ov: TabsOverflow) {
        unsafe { Fl_Tabs_handle_overflow(self.inner.widget() as _, ov as i32) }
    }
}

/// Creates a tile which can contain widgets. For the tiling to work correctly, the children of a Tile must cover the entire area of the widget, but not overlap. This means that all children must touch each other at their edges, and no gaps can be left inside the Tile.
/// More info can be found [here](https://www.fltk.org/doc-1.4/classFl__Tile.html#details)
#[derive(Debug)]
pub struct Tile {
    inner: crate::widget::WidgetTracker,
    is_derived: bool,
}

crate::macros::widget::impl_widget_ext!(Tile, Fl_Tile);
crate::macros::widget::impl_widget_base!(Tile, Fl_Tile);
crate::macros::widget::impl_widget_default!(Tile);
crate::macros::group::impl_group_ext!(Tile, Fl_Tile);

impl Tile {
    /**
    Drags the intersection at (\p oldx,\p oldy) to (\p newx,\p newy).

    This redraws all the necessary children.

    If no size ranges are set, the new intersection position is limited to the
    size of the tile group. The resizable() option is not taken into account here.

    If size ranges are set, the actual new position of the intersection will
    depend on the size range of every individual child. No child will be smaller
    than their minw and minh. After the new position is found, move_intersection()
    will call init_sizes(). The resizable() range is ignored.

    \param[in] oldx, oldy move the intersection at this coordinate, pass zero to
        disable drag in that direction.
    \param[in] newx, newy move the intersection as close to this new coordinate
        as possible
    */
    pub fn move_intersection(&mut self, oldx: i32, oldy: i32, newx: i32, newy: i32) {
        unsafe {
            Fl_Tile_move_intersection(self.inner.widget() as _, oldx, oldy, newx, newy);
        }
    }

    /// Set the allowed size range for the child at the given index
    pub fn size_range_by_index(&mut self, idx: i32, minw: i32, minh: i32, maxw: i32, maxh: i32) {
        unsafe {
            Fl_Tile_size_range_by_index(self.inner.widget() as _, idx, minw, minh, maxw, maxh)
        }
    }

    /// Set the allowed size range for the given child widget
    pub fn size_range_by_child<W: WidgetExt>(
        &mut self,
        w: &W,
        minw: i32,
        minh: i32,
        maxw: i32,
        maxh: i32,
    ) {
        unsafe {
            Fl_Tile_size_range_by_child(
                self.inner.widget() as _,
                w.as_widget_ptr() as _,
                minw,
                minh,
                maxw,
                maxh,
            )
        }
    }
}

/// Creates a wizard widget
#[derive(Debug)]
pub struct Wizard {
    inner: crate::widget::WidgetTracker,
    is_derived: bool,
}

crate::macros::widget::impl_widget_ext!(Wizard, Fl_Wizard);
crate::macros::widget::impl_widget_base!(Wizard, Fl_Wizard);
crate::macros::widget::impl_widget_default!(Wizard);
crate::macros::group::impl_group_ext!(Wizard, Fl_Wizard);

impl Wizard {
    /// Gets the next view of the wizard
    pub fn next(&mut self) {
        unsafe { Fl_Wizard_next(self.inner.widget() as _) }
    }

    /// Gets the previous view of the wizard
    pub fn prev(&mut self) {
        unsafe { Fl_Wizard_prev(self.inner.widget() as _) }
    }

    #[deprecated(since = "1.2.18", note = "please use `try_current_widget` instead")]
    /// Gets the underlying widget of the current view
    pub fn current_widget(&self) -> Widget {
        unsafe {
            let ptr = Fl_Wizard_value(self.inner.widget() as _) as *mut fltk_sys::widget::Fl_Widget;
            assert!(!ptr.is_null());
            Widget::from_widget_ptr(ptr)
        }
    }

    /// Gets the underlying widget of the current view
    pub fn try_current_widget(&self) -> Option<impl WidgetExt> {
        unsafe {
            let ptr = Fl_Wizard_value(self.inner.widget() as _) as *mut fltk_sys::widget::Fl_Widget;
            if ptr.is_null() {
                None
            } else {
                Some(Widget::from_widget_ptr(ptr))
            }
        }
    }

    /// Sets the underlying widget of the current view
    pub fn set_current_widget<W: WidgetExt>(&mut self, w: &W) {
        unsafe {
            Fl_Wizard_set_value(
                self.inner.widget() as _,
                w.as_widget_ptr() as *mut fltk_sys::group::Fl_Widget,
            )
        }
    }
}

/// Creates a color chooser widget
#[derive(Debug)]
pub struct ColorChooser {
    inner: crate::widget::WidgetTracker,
    is_derived: bool,
}

crate::macros::widget::impl_widget_ext!(ColorChooser, Fl_Color_Chooser);
crate::macros::widget::impl_widget_base!(ColorChooser, Fl_Color_Chooser);
crate::macros::widget::impl_widget_default!(ColorChooser);
crate::macros::group::impl_group_ext!(ColorChooser, Fl_Color_Chooser);

impl ColorChooser {
    /// Return the rgb color
    pub fn rgb_color(&self) -> (u8, u8, u8) {
        unsafe {
            let r = (Fl_Color_Chooser_r(self.inner.widget() as _) * 255.0) as u8;
            let g = (Fl_Color_Chooser_g(self.inner.widget() as _) * 255.0) as u8;
            let b = (Fl_Color_Chooser_b(self.inner.widget() as _) * 255.0) as u8;
            (r, g, b)
        }
    }

    /// Return the hex color
    pub fn hex_color(&self) -> u32 {
        let (r, g, b) = self.rgb_color();
        crate::utils::rgb2hex(r, g, b)
    }

    /// Set the base color of the ColorChooser. Returns an error on failure to change the color (wrong input)
    pub fn set_rgb(&mut self, r: u8, g: u8, b: u8) -> Result<(), FltkError> {
        unsafe {
            let ret = Fl_Color_Chooser_set_rgb(
                self.inner.widget() as _,
                r as f64 / 255.0,
                g as f64 / 255.0,
                b as f64 / 255.0,
            );
            if ret == 1 {
                Ok(())
            } else {
                Err(FltkError::Internal(FltkErrorKind::FailedOperation))
            }
        }
    }

    /// Set the base color of the ColorChooser. Returns an error on failure to change the color (wrong input)
    pub fn set_tuple_rgb(&mut self, (r, g, b): (u8, u8, u8)) -> Result<(), FltkError> {
        unsafe {
            let ret = Fl_Color_Chooser_set_rgb(
                self.inner.widget() as _,
                r as f64 / 255.0,
                g as f64 / 255.0,
                b as f64 / 255.0,
            );
            if ret == 1 {
                Ok(())
            } else {
                Err(FltkError::Internal(FltkErrorKind::FailedOperation))
            }
        }
    }
}

crate::macros::widget::impl_widget_type!(FlexType);

/// Defines Flex types
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum FlexType {
    /// row direction
    Column = 0,
    /// column direction
    Row,
}

/**
    a Flexbox widget
    # Example
    ```rust,no_run
    use fltk::{prelude::*, *};
    fn main() {
        let a = app::App::default();
        let mut win = window::Window::default().with_size(400, 300);
        let mut col = group::Flex::default().size_of_parent();
        col.set_type(group::FlexType::Column);
        let expanding = button::Button::default().with_label("Expanding");
        let mut normal = button::Button::default().with_label("Normal");
        col.set_size(&mut normal, 30);
        col.end();
        win.end();
        win.show();
        a.run().unwrap();
    }
    ```
*/
#[derive(Debug)]
pub struct Flex {
    inner: crate::widget::WidgetTracker,
    is_derived: bool,
}

crate::macros::widget::impl_widget_ext!(Flex, Fl_Flex);
crate::macros::widget::impl_widget_base!(Flex, Fl_Flex);
crate::macros::widget::impl_widget_default!(Flex);
crate::macros::group::impl_group_ext!(Flex, Fl_Flex);

impl Flex {
    /// Create a new Flex widget.
    /// This code is here for backward compatibility with initial Fl_Flex code, which defaulted to Row instead of Column.
    /// The behavior will be changed in fltk-rs version 2.
    fn new<T: Into<Option<&'static str>>>(
        x: i32,
        y: i32,
        width: i32,
        height: i32,
        title: T,
    ) -> Self {
        let mut f = <Flex as WidgetBase>::new(x, y, width, height, title).row();
        f.set_pad(5);
        f.debug_();
        f
    }
    /// Add a widget to the Flex box
    pub fn add<W: WidgetExt>(&mut self, widget: &W) {
        <Self as GroupExt>::add(self, widget);
        self.recalc();
    }

    /// Add a widget to the Flex box
    pub fn insert<W: WidgetExt>(&mut self, widget: &W, idx: i32) {
        <Self as GroupExt>::insert(self, widget, idx);
        self.recalc();
    }

    /// Set the size of the widget, same as `fixed` (before it was changed in FLTK 1.4)
    #[deprecated(since = "1.4.8", note = "please use `fixed` instead")]
    pub fn set_size<W: WidgetExt>(&mut self, w: &W, size: i32) {
        unsafe { Fl_Flex_set_size(self.inner.widget() as _, w.as_widget_ptr() as _, size) }
    }

    /// Set the size of the widget, same as `set_size`, but more inline with the new FLTK Fl_Flex api
    pub fn fixed<W: WidgetExt>(&mut self, w: &W, size: i32) {
        unsafe { Fl_Flex_set_size(self.inner.widget() as _, w.as_widget_ptr() as _, size) }
    }

    /// Debug the flex layout
    pub fn debug(flag: bool) {
        DEBUG.store(flag, Ordering::Release);
    }

    fn debug_(&mut self) {
        if DEBUG.load(Ordering::Relaxed) {
            self.set_frame(FrameType::BorderBox);
            if self.get_type::<FlexType>() == FlexType::Row {
                self.set_color(Color::from_rgb(200, 0, 0));
            } else {
                self.set_color(Color::from_rgb(0, 0, 200));
            }
        }
    }

    /// Set the type to be a column
    pub fn column(mut self) -> Self {
        self.set_type(FlexType::Column);
        self.debug_();
        self
    }

    /// Set the type to a row
    pub fn row(mut self) -> Self {
        self.set_type(FlexType::Row);
        self.debug_();
        self
    }

    /// Recalculate children's coords and sizes
    pub fn recalc(&self) {
        let mut s = self.clone();
        s.resize(self.x(), self.y(), self.w(), self.h());
        s.redraw();
    }

    /// Recalculate children's coords and sizes
    pub fn layout(&self) {
        self.recalc();
    }

    /// Set the margin
    pub fn set_margin(&mut self, m: i32) {
        unsafe { Fl_Flex_set_margin(self.inner.widget() as _, m) }
    }

    /// Get the margin
    pub fn margin(&self) -> i32 {
        unsafe { Fl_Flex_margin(self.inner.widget() as _) }
    }

    /// Set the padding
    pub fn set_pad(&mut self, p: i32) {
        unsafe { Fl_Flex_set_pad(self.inner.widget() as _, p) }
    }

    /// Get the padding
    pub fn pad(&self) -> i32 {
        unsafe { Fl_Flex_pad(self.inner.widget() as _) }
    }

    /// Set the padding
    pub fn set_spacing(&mut self, p: i32) {
        unsafe { Fl_Flex_set_pad(self.inner.widget() as _, p) }
    }

    /// Get the padding
    pub fn spacing(&self) -> i32 {
        unsafe { Fl_Flex_pad(self.inner.widget() as _) }
    }

    /// Set the margins
    pub fn set_margins(&mut self, left: i32, top: i32, right: i32, bottom: i32) {
        unsafe { Fl_Flex_set_margins(self.inner.widget() as _, left, top, right, bottom) }
    }

    /// Get the margins -> returns (left, top, right, bottom)
    pub fn margins(&self) -> (i32, i32, i32, i32) {
        let mut left = 0;
        let mut top = 0;
        let mut right = 0;
        let mut bottom = 0;
        unsafe {
            Fl_Flex_margins(
                self.inner.widget() as _,
                &mut left,
                &mut top,
                &mut right,
                &mut bottom,
            );
        }
        (left, top, right, bottom)
    }
}

/**
    Defines a Vertical Grid (custom widget).
    Requires setting the params manually using the `set_params` method, which takes the rows, columns and spacing.
    ```rust,no_run
    use fltk::{prelude::*, *};
    fn main() {
        let app = app::App::default();
        let mut win = window::Window::default().with_size(400, 300);
        let mut grid = group::VGrid::new(0, 0, 400, 300, "");
        grid.set_params(3, 3, 5);
        button::Button::default();
        button::Button::default();
        button::Button::default();
        button::Button::default();
        button::Button::default();
        button::Button::default();
        button::Button::default();
        button::Button::default();
        grid.end();
        win.end();
        win.show();
        app.run().unwrap();
    }
    ```
*/
#[derive(Debug, Clone)]
pub struct VGrid {
    vpack: Pack,
    rows: i32,
    cols: i32,
    current: i32,
}

impl Default for VGrid {
    fn default() -> Self {
        Self::new(0, 0, 0, 0, None)
    }
}

impl VGrid {
    /// Constructs a widget with the size of its parent
    pub fn default_fill() -> Self {
        Self::default().size_of_parent()
    }

    /// Creates a new vertical grid
    pub fn new<T: Into<Option<&'static str>>>(x: i32, y: i32, w: i32, h: i32, label: T) -> VGrid {
        let vpack = Pack::new(x, y, w, h, label);
        VGrid {
            vpack,
            rows: 1,
            cols: 1,
            current: 0,
        }
    }

    /// Sets the params for the grid
    pub fn set_params(&mut self, rows: i32, cols: i32, spacing: i32) {
        self.vpack.set_spacing(spacing);
        let rows = if rows < 1 { 1 } else { rows };
        let cols = if cols < 1 { 1 } else { cols };
        self.rows = rows;
        self.cols = cols;
        for _ in 0..rows {
            let mut p = Pack::new(0, 0, self.vpack.width(), 0, "");
            p.set_type(PackType::Horizontal);
            p.set_spacing(spacing);
            p.end();
            self.vpack.add(&p);
        }
    }

    /// Adds widgets to the grid
    pub fn add<W: WidgetExt>(&mut self, w: &W) {
        debug_assert!(self.current < self.rows * self.cols);
        let rem = (self.current - 1) / self.cols;
        if rem < self.rows {
            let hpack = self.vpack.child(rem).unwrap();
            let mut hpack = unsafe { Pack::from_widget_ptr(hpack.as_widget_ptr()) };
            hpack.end();
            hpack.add(w);
            hpack.auto_layout();
            self.vpack.auto_layout();
            self.current += 1;
        }
    }

    /// End the grid
    pub fn end(&mut self) {
        use std::collections::VecDeque;
        let children = self.vpack.children();
        self.current = children - self.rows;
        debug_assert!(self.current <= self.rows * self.cols);
        let mut v = VecDeque::new();
        for i in self.rows..children {
            let c = self.vpack.child(i).unwrap();
            v.push_back(c);
        }
        for i in 0..self.rows {
            let hpack = self.vpack.child(i).unwrap();
            let mut hpack = unsafe { Pack::from_widget_ptr(hpack.as_widget_ptr()) };
            hpack.end();
            for _j in 0..self.cols {
                if let Some(w) = v.pop_front() {
                    self.vpack.remove(&w);
                    hpack.add(&w);
                }
                hpack.auto_layout();
            }
        }
        self.vpack.auto_layout();
    }
}

crate::widget_extends!(VGrid, Pack, vpack);

/**
    Defines a Horizontal Grid (custom widget).
    Requires setting the params manually using the `set_params` method, which takes the rows, columns and spacing.
    ```rust,no_run
    use fltk::{prelude::*, *};
    fn main() {
        let app = app::App::default();
        let mut win = window::Window::default().with_size(400, 300);
        let mut grid = group::HGrid::new(0, 0, 400, 300, "");
        grid.set_params(3, 3, 5);
        button::Button::default();
        button::Button::default();
        button::Button::default();
        button::Button::default();
        button::Button::default();
        button::Button::default();
        button::Button::default();
        button::Button::default();
        grid.end();
        win.end();
        win.show();
        app.run().unwrap();
    }
    ```
*/
#[derive(Debug, Clone)]
pub struct HGrid {
    hpack: Pack,
    rows: i32,
    cols: i32,
    current: i32,
}

impl Default for HGrid {
    fn default() -> Self {
        Self::new(0, 0, 0, 0, None)
    }
}

impl HGrid {
    /// Constructs a widget with the size of its parent
    pub fn default_fill() -> Self {
        Self::default().size_of_parent()
    }

    /// Creates a new horizontal grid
    pub fn new<T: Into<Option<&'static str>>>(x: i32, y: i32, w: i32, h: i32, label: T) -> HGrid {
        let mut hpack = Pack::new(x, y, w, h, label);
        hpack.set_type(PackType::Horizontal);
        HGrid {
            hpack,
            rows: 1,
            cols: 1,
            current: 0,
        }
    }

    /// Sets the params for the grid
    pub fn set_params(&mut self, rows: i32, cols: i32, spacing: i32) {
        self.hpack.set_spacing(spacing);
        let rows = if rows < 1 { 1 } else { rows };
        let cols = if cols < 1 { 1 } else { cols };
        self.rows = rows;
        self.cols = cols;
        for _ in 0..cols {
            let mut p = Pack::new(0, 0, 0, self.hpack.height(), "");
            p.set_spacing(spacing);
            p.end();
            self.hpack.add(&p);
        }
    }

    /// Adds widgets to the grid
    pub fn add<W: WidgetExt>(&mut self, w: &W) {
        debug_assert!(self.current < self.rows * self.cols);
        let rem = (self.current - 1) / self.rows;
        if rem < self.cols {
            let vpack = self.hpack.child(rem).unwrap();
            let mut vpack = unsafe { Pack::from_widget_ptr(vpack.as_widget_ptr()) };
            vpack.end();
            vpack.add(w);
            vpack.auto_layout();
            self.hpack.auto_layout();
            self.current += 1;
        }
    }

    /// End the grid
    pub fn end(&mut self) {
        use std::collections::VecDeque;
        let children = self.hpack.children();
        self.current = children - self.cols;
        debug_assert!(self.current <= self.rows * self.cols);
        let mut v = VecDeque::new();
        for i in self.cols..children {
            let c = self.hpack.child(i).unwrap();
            v.push_back(c);
        }
        for i in 0..self.cols {
            let vpack = self.hpack.child(i).unwrap();
            let mut vpack = unsafe { Pack::from_widget_ptr(vpack.as_widget_ptr()) };
            vpack.end();
            for _j in 0..self.rows {
                if let Some(w) = v.pop_front() {
                    self.hpack.remove(&w);
                    vpack.add(&w);
                }
                vpack.auto_layout();
            }
        }
        self.hpack.auto_layout();
    }
}

crate::widget_extends!(HGrid, Pack, hpack);

/// A wrapper around a Flex column
#[derive(Debug, Clone)]
pub struct Column {
    p: Flex,
}

impl Default for Column {
    fn default() -> Self {
        Self::new(0, 0, 0, 0, None)
    }
}

impl Column {
    /// Constructs a widget with the size of its parent
    pub fn default_fill() -> Self {
        Self::default().size_of_parent().center_of_parent()
    }

    /// Create a new column
    pub fn new<T: Into<Option<&'static str>>>(
        x: i32,
        y: i32,
        width: i32,
        height: i32,
        label: T,
    ) -> Column {
        let mut p = Flex::new(x, y, width, height, label);
        p.set_type(FlexType::Column);
        Column { p }
    }

    /// Add a widget to the column with automatic layouting
    pub fn add<W: WidgetExt>(&mut self, w: &W) {
        self.p.add(w);
    }
}

crate::widget_extends!(Column, Flex, p);

/// A wrapper around a Flex row
#[derive(Debug, Clone)]
pub struct Row {
    p: Flex,
}

impl Default for Row {
    fn default() -> Self {
        Self::new(0, 0, 0, 0, None)
    }
}

impl Row {
    /// Constructs a widget with the size of its parent
    pub fn default_fill() -> Self {
        Self::default().size_of_parent().center_of_parent()
    }

    /// Create a new row
    pub fn new<T: Into<Option<&'static str>>>(
        x: i32,
        y: i32,
        width: i32,
        height: i32,
        label: T,
    ) -> Row {
        let mut p = Flex::new(x, y, width, height, label);
        p.set_type(FlexType::Row);
        Row { p }
    }

    /// Add a widget to the row with automatic layouting
    pub fn add<W: WidgetExt>(&mut self, w: &W) {
        self.p.add(w);
    }
}

crate::widget_extends!(Row, Flex, p);

/// Experimental Group widgets, the api might change.
/// # Warning
/// The api might change if changes happen upstream
pub mod experimental {
    use super::*;
    use crate::enums::Font;
    use std::ops::Range;

    /// Grid range
    pub struct GridRange {
        start: usize,
        end: usize,
    }

    impl GridRange {
        /// Check the length of the GridRange
        pub fn len(&self) -> usize {
            self.end - self.start
        }

        /// Check whether the GridRange is empty
        pub fn is_empty(&self) -> bool {
            self.len() == 0
        }
    }

    impl From<Range<usize>> for GridRange {
        fn from(val: Range<usize>) -> Self {
            Self {
                start: val.start,
                end: val.end,
            }
        }
    }

    impl From<usize> for GridRange {
        fn from(val: usize) -> Self {
            (val..val + 1).into()
        }
    }

    bitflags::bitflags! {
        /// Defines alignment rules used by FLTK's Grid
        #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
        pub struct GridAlign: u16 {
            /** Align the widget at the center of the cell. */
            const  CENTER          = 0x0000;
            /** Align the widget at the top of the cell. */
            const  TOP             = 0x0001;
            /** Align the widget at the bottom of the cell. */
            const  BOTTOM          = 0x0002;
            /** Align the widget at the left side of the cell. */
            const  LEFT            = 0x0004;
            /** Align the widget at the right side of the cell. */
            const  RIGHT           = 0x0008;
            /** Stretch the widget horizontally to fill the cell. */
            const  HORIZONTAL      = 0x0010;
            /** Stretch the widget vertically to fill the cell. */
            const  VERTICAL        = 0x0020;
            /** Stretch the widget in both directions to fill the cell. */
            const  FILL            = 0x0030;
            /** Stretch the widget proportionally. */
            const  PROPORTIONAL    = 0x0040;
            /** Align the widget at the top left of the cell. */
            const  TOP_LEFT        =  GridAlign::TOP.bits() |  GridAlign::LEFT.bits();
            /** Align the widget at the top right of the cell. */
            const  TOP_RIGHT       =  GridAlign::TOP.bits() |  GridAlign::RIGHT.bits();
            /** Align the widget at the bottom left of the cell. */
            const  BOTTOM_LEFT     =  GridAlign::BOTTOM.bits() |  GridAlign::LEFT.bits();
            /** Align the widget at the bottom right of the cell. */
            const  BOTTOM_RIGHT    =  GridAlign::BOTTOM.bits() |  GridAlign::RIGHT.bits();
        }
    }
    /// Fltk's grid widget
    #[derive(Debug)]
    pub struct Grid {
        inner: crate::widget::WidgetTracker,
        is_derived: bool,
    }

    crate::macros::widget::impl_widget_ext!(Grid, Fl_Grid);
    crate::macros::widget::impl_widget_base!(Grid, Fl_Grid);
    crate::macros::widget::impl_widget_default!(Grid);
    crate::macros::group::impl_group_ext!(Grid, Fl_Grid);

    impl Grid {
        /// Set the layout of the grid, along with the margin and gap
        pub fn set_layout_ext(&mut self, rows: i32, cols: i32, margin: i32, gap: i32) {
            unsafe { Fl_Grid_set_layout(self.inner.widget() as _, rows, cols, margin, gap) }
        }
        /// Set the layout of the grid
        pub fn set_layout(&mut self, rows: i32, cols: i32) {
            unsafe { Fl_Grid_set_layout(self.inner.widget() as _, rows, cols, -1, -1) }
        }
        /// Layout the grid
        pub fn layout(&mut self) {
            unsafe { Fl_Grid_layout(self.inner.widget() as _) }
        }
        /// Clear the layout
        pub fn clear_layout(&mut self) {
            unsafe { Fl_Grid_clear_layout(self.inner.widget() as _) }
        }
        /// Set whether the Grid needs layout
        pub fn set_need_layout(&mut self, set: bool) {
            unsafe { Fl_Grid_set_need_layout(self.inner.widget() as _, set as _) }
        }
        /// Get whether the Grid needs layout
        pub fn need_layout(&self) -> bool {
            unsafe { Fl_Grid_need_layout(self.inner.widget() as _) != 0 }
        }
        /// Set the grid's margin
        pub fn set_margin(&mut self, left: i32, top: i32, right: i32, bottom: i32) {
            unsafe { Fl_Grid_set_margin(self.inner.widget() as _, left, top, right, bottom) }
        }
        /// Set the grid's gap
        pub fn set_gap(&mut self, row_gap: i32, col_gap: i32) {
            unsafe { Fl_Grid_set_gap(self.inner.widget() as _, row_gap, col_gap) }
        }
        #[allow(dead_code)]
        /// Set the widget at row/column and alignment
        pub fn set_widget_<W: WidgetExt>(
            &mut self,
            wi: &mut W,
            row: i32,
            col: i32,
            align: GridAlign,
        ) -> *mut () {
            unsafe {
                Fl_Grid_set_widget(
                    self.inner.widget() as _,
                    wi.as_widget_ptr() as _,
                    row,
                    col,
                    align.bits(),
                ) as _
            }
        }
        /// Set the widget at row/column using ranges
        pub fn set_widget<W: 'static + Clone + WidgetExt>(
            &mut self,
            widget: &mut W,
            row: impl Into<GridRange>,
            col: impl Into<GridRange>,
        ) -> Result<(), FltkError> {
            let row = row.into();
            let col = col.into();
            self.set_widget_ext(widget, row, col, GridAlign::FILL)
        }
        /// Set the widget at row/column along with row span and column span and alignment
        fn set_widget_ext_<W: WidgetExt>(
            &mut self,
            wi: &mut W,
            row: i32,
            col: i32,
            rowspan: i32,
            colspan: i32,
            align: GridAlign,
        ) -> *mut () {
            unsafe {
                Fl_Grid_set_widget_ext(
                    self.inner.widget() as _,
                    wi.as_widget_ptr() as _,
                    row,
                    col,
                    rowspan,
                    colspan,
                    align.bits(),
                ) as _
            }
        }
        /// Set the widget at row/column using ranges along with the alignment
        pub fn set_widget_ext<W: 'static + Clone + WidgetExt>(
            &mut self,
            widget: &mut W,
            row: impl Into<GridRange>,
            col: impl Into<GridRange>,
            align: GridAlign,
        ) -> Result<(), FltkError> {
            let row = row.into();
            let col = col.into();
            let e = self.set_widget_ext_(
                widget,
                row.start as _,
                col.start as _,
                row.len() as _,
                col.len() as _,
                align,
            );
            if e.is_null() {
                Err(FltkError::Internal(FltkErrorKind::FailedGridSetWidget))
            } else {
                Ok(())
            }
        }
        /// Set the column width
        pub fn set_col_width(&mut self, col: i32, value: i32) {
            unsafe { Fl_Grid_set_col_width(self.inner.widget() as _, col, value) }
        }
        /// Set the column weight
        pub fn set_col_weight(&mut self, col: i32, value: i32) {
            unsafe { Fl_Grid_set_col_weight(self.inner.widget() as _, col, value) }
        }
        /// Set the column gap
        pub fn set_col_gap(&mut self, col: i32, value: i32) {
            unsafe { Fl_Grid_set_col_gap(self.inner.widget() as _, col, value) }
        }
        /// Set the row height
        pub fn set_row_height(&mut self, row: i32, value: i32) {
            unsafe { Fl_Grid_set_row_height(self.inner.widget() as _, row, value) }
        }
        /// Set the row weight
        pub fn set_row_weight(&mut self, row: i32, value: i32) {
            unsafe { Fl_Grid_set_row_weight(self.inner.widget() as _, row, value) }
        }
        /// Set the row gap
        pub fn set_row_gap(&mut self, row: i32, value: i32) {
            unsafe { Fl_Grid_set_row_gap(self.inner.widget() as _, row, value) }
        }
        /// Show the grid
        pub fn show_grid(&mut self, set: bool) {
            unsafe { Fl_Grid_show_grid(self.inner.widget() as _, set as _) }
        }
        /// Show the grid with a certain color
        pub fn show_grid_with_color(&mut self, set: bool, col: Color) {
            unsafe { Fl_Grid_show_grid_with_color(self.inner.widget() as _, set as _, col.bits()) }
        }
        /// Debug the grid
        pub fn debug(&mut self, level: i32) {
            unsafe { Fl_Grid_debug(self.inner.widget() as _, level) }
        }
    }

    /// Creates a scrollable display widget to handle terminal-like behavior, such as
    /// logging events or debug information.
    /// Replaces SimpleTerminal widget
    ///
    #[derive(Debug)]
    pub struct Terminal {
        inner: crate::widget::WidgetTracker,
        is_derived: bool,
    }

    crate::macros::widget::impl_widget_ext!(Terminal, Fl_Terminal);
    crate::macros::widget::impl_widget_base!(Terminal, Fl_Terminal);
    crate::macros::widget::impl_widget_default!(Terminal);
    crate::macros::group::impl_group_ext!(Terminal, Fl_Terminal);

    ///   Determines when Fl_Terminal calls redraw() if new text is added.
    /// RATE_LIMITED is the recommended setting, using redraw_rate(float) to determine
    /// the maximum rate of redraws.
    /// see redraw_style(), redraw_rate()
    #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
    pub struct RedrawStyle {
        bits: u32,
    }

    impl RedrawStyle {
        /// App must call redraw() as needed to update text to screen
        pub const NoRedraw: RedrawStyle = RedrawStyle { bits: 0x0000 };
        /// timer controlled redraws. (DEFAULT)
        pub const RateLimited: RedrawStyle = RedrawStyle { bits: 0x0001 };
        /// redraw triggered after *every* append() / printf() / etc. operation
        pub const PerWrite: RedrawStyle = RedrawStyle { bits: 0x0002 };

        /// Gets the inner representation
        pub const fn bits(&self) -> u32 {
            self.bits
        }
        /// Build a RedrawStyle enum with an arbitrary value.
        pub const fn new(val: u32) -> Self {
            RedrawStyle { bits: val }
        }
    }

    bitflags::bitflags! {
        /// Bits for the per-character attributes, which control text features
        /// such as italic, bold, underlined text, etc.
        /// Can be combined with | operator
        #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
        pub struct Attrib: u8 {
            /// all attributes off
            const Normal =  0x00 ;
            /// bold text: uses bold font, color brighter than normal
            const Bold = 0x01 ;
            /// dim text; color slightly darker than normal
            const Dim =  0x02 ;
            /// italic font text
            const Italic =  0x04 ;
            /// underlined text
            const Underline =  0x08 ;
            /// <EM>(reserved for internal future use)</EM>
            const _Reserved1 =   0x10 ;
            /// inverse text; fg/bg color are swapped
            const Inverse =   0x20 ;
            /// <EM>(reserved for internal future use)</EM>
            const _Reserved2 = 0x40 ;
            /// strikeout text
            const Strikeout = 0x80 ;
        }
    }

    bitflags::bitflags! {
        /// Output translation flags for special control character translations.
        #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
        pub struct OutFlags: u8 {
            ///< no output translation
            const OFF        = 0x00;
            ///< carriage return generates a vertical line-feed (\\r -> \\n)
            const CR_TO_LF   = 0x01;
            ///< line-feed generates a carriage return (\\n -> \\r)
            const LF_TO_CR   = 0x02;
            ///< line-feed generates a carriage return line-feed (\\n -> \\r\\n)
            const LF_TO_CRLF = 0x04;
        }
    }

    ///    'xterm color' values, used in set_text_fg_color_xterm and set_text_bg_color_xterm
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(u8)]
    #[allow(missing_docs)] // These color names are self-documenting
    #[non_exhaustive]
    pub enum XtermColor {
        Black = 0,
        Red = 1,
        Green = 2,
        Yellow = 3,
        Blue = 4,
        Magenta = 5,
        Cyan = 6,
        White = 7,
    }

    bitflags::bitflags! {
        /// Per-character 8 bit flags (u8) used to manage special states for characters.
        #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
        pub struct CharFlags: u8 {
            /// No flags
            const NONE   = 0x00;
            /// this char's fg color is an XTERM color; can be affected by Dim+Bold
            const FG_XTERM   = 0x01;
            /// this char's bg color is an XTERM color; can be affected by Dim+Bold
            const BG_XTERM   = 0x02;
            /// used internally for line re-wrap during screen resizing
            const _EOL        = 0x04;
            /// Reserved
            const _RESV_A     = 0x08;
            /// Reserved
            const _RESV_B     = 0x10;
            /// Reserved
            const _RESV_C     = 0x20;
            /// Reserved
            const _RESV_D     = 0x40;
            /// Reserved
            const _RESV_E     = 0x80;
        }
    }

    ///    Class to manage the terminal's individual UTF-8 characters.
    ///    Includes fg/bg color, attributes (BOLD, UNDERLINE..)
    /// *This is a low-level "protected" class in the fltk library*
    pub struct Utf8Char {
        inner: *const Fl_Terminal_Utf8Char, // This points to a C++ Fl_Terminal::Utf8Char structure
    }

    impl std::fmt::Debug for Utf8Char {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            let x = self.text_utf8();
            write!(
                f,
                "Utf8Char {:?} '{}'  fg:{} bg:{} {:?}",
                x,
                std::str::from_utf8(x).unwrap(),
                self.fgcolor(),
                self.bgcolor(),
                self.attrib()
            )
        }
    }

    ///    Class to read characters from the terminal's buffer rows.
    ///    Includes indexing access and iterators
    ///    *This is a low-level "protected" class*
    pub struct BuffRow<'a> {
        inner: *const Fl_Terminal_Utf8Char, // This points to an array of Fl_Terminal::Utf8Char
        /// Parent terminal widget that owns this buffer
        _parent: &'a Terminal,
        /// Number of characters in the row
        pub length: usize,
        /// sizeof(Fl_Terminal::Utf8Char)
        pub char_size: usize,
    }

    impl Terminal {
        /// Returns whether the terminal is in ANSI mode.
        pub fn ansi(&self) -> bool {
            unsafe { Fl_Terminal_ansi(self.inner.widget() as _) != 0 }
        }

        /// Enable/disable ANSI mode. If true, ANSI and VT100/xterm codes will be processed.
        /// If false, these codes won't be processed and will either be ignored or print the
        /// error character "¿", depending on the value of show_unknown().
        pub fn set_ansi(&mut self, arg1: bool) {
            unsafe { Fl_Terminal_set_ansi(self.inner.widget() as _, arg1 as i32) }
        }

        /// Appends text to the terminal at current cursor position using the current text color/attributes.
        /// Redraws are managed automatically by default; see redraw_style()
        pub fn append(&mut self, s: &str) {
            let raw_s = CString::safe_new(s).into_raw();
            unsafe {
                Fl_Terminal_append(self.inner.widget() as _, raw_s as _);
                // Take ownership of raw_s back so it will be dropped
                let _raw_s = CString::from_raw(raw_s);
            }
        }

        /// Appends data to the terminal at current cursor position using the current text color/attributes
        /// Redraws are managed automatically by default; see redraw_style()
        pub fn append_u8(&mut self, s: &[u8]) {
            unsafe {
                Fl_Terminal_append_u8(self.inner.widget() as _, s.as_ptr() as _, s.len() as _)
            }
        }

        /// Appends text to the terminal at current cursor position using the current text color/attributes.
        /// Slightly more efficient than append_utf8
        /// Redraws are managed automatically by default; see redraw_style()
        pub fn append_ascii(&mut self, s: &str) {
            let raw_s = CString::safe_new(s).into_raw();
            unsafe {
                Fl_Terminal_append_ascii(self.inner.widget() as _, raw_s as _);
                // Take ownership of raw_s back so it will be dropped
                let _raw_s = CString::from_raw(raw_s);
            }
        }

        /// Appends text to the terminal at current cursor position using the current text color/attributes.
        /// Handles UTF-8 chars split across calls
        /// Redraws are managed automatically by default; see redraw_style()
        pub fn append_utf8(&mut self, s: &str) {
            let raw_s = CString::safe_new(s).into_raw();
            unsafe {
                Fl_Terminal_append_utf8(self.inner.widget() as _, raw_s as _);
                // Take ownership of raw_s back so it will be dropped
                let _raw_s = CString::from_raw(raw_s);
            }
        }

        /// Appends data to the terminal at current cursor position using the current text color/attributes
        /// Handles UTF-8 chars split across calls
        /// Redraws are managed automatically by default; see redraw_style()
        pub fn append_utf8_u8(&mut self, s: &[u8]) {
            unsafe {
                Fl_Terminal_append_utf8_u8(self.inner.widget() as _, s.as_ptr() as _, s.len() as _)
            }
        }

        /// Clears the screen to the current `textbgcolor()`, and homes the cursor.
        pub fn clear(&mut self) {
            unsafe { Fl_Terminal_clear(self.inner.widget() as _) }
        }

        /// Clear any current mouse selection.
        pub fn clear_mouse_selection(&mut self) {
            unsafe { Fl_Terminal_clear_mouse_selection(self.inner.widget() as _) }
        }

        ///  Clears the screen to a specific color `val` and homes the cursor.
        /// Does not affect the value of text_bg_color or text_bg_color_default
        pub fn clear_to_color(&mut self, val: Color) {
            unsafe { Fl_Terminal_clear_to_color(self.inner.widget() as _, val.bits()) }
        }

        ///   Clear the terminal screen only; does not affect the cursor position.
        ///
        /// Also clears the current mouse selection.
        ///
        /// If `scroll_to_hist` is true, the screen is cleared by scrolling the
        /// contents into the scrollback history, where it can be retrieved with the
        /// scrollbar. If false, the screen is cleared
        /// and the scrollback history is unchanged.
        ///
        /// Similar to the escape sequence `\<ESC\>[2J`.
        pub fn clear_screen(&mut self, arg1: bool) {
            unsafe { Fl_Terminal_clear_screen(self.inner.widget() as _, arg1 as i32) }
        }

        ///   Clear the terminal screen and home the cursor
        ///
        /// Also clears the current mouse selection.
        ///
        /// If `scroll_to_hist` is true, the screen is cleared by scrolling the
        /// contents into the scrollback history, where it can be retrieved with the
        /// scrollbar. If false, the screen is cleared
        /// and the scrollback history is unchanged.
        ///
        /// Similar to the escape sequence `\<ESC\>[2J\<ESC\>[H`.
        pub fn clear_screen_home(&mut self, arg1: bool) {
            unsafe { Fl_Terminal_clear_screen_home(self.inner.widget() as _, arg1 as i32) }
        }

        /// Clears the scroll history buffer and adjusts scrollbar, forcing it to redraw()
        pub fn clear_history(&mut self) {
            unsafe { Fl_Terminal_clear_history(self.inner.widget() as _) }
        }

        /// Get the background color for the terminal's Fl_Group::box().
        pub fn color(&self) -> Color {
            Color::from_rgbi(unsafe { Fl_Terminal_color(self.inner.widget() as _) })
        }

        /// Sets the background color for the terminal's Fl_Group::box().
        ///
        /// If the textbgcolor() and textbgcolor_default() are set to the special
        /// "see through" color 0xffffffff when any text was added, changing color()
        /// affects the color that shows through behind that existing text.
        ///
        /// Otherwise, whatever specific background color was set for existing text will
        ///  persist after changing color().
        ///
        /// To see the effects of a change to color(), follow up with a call to redraw().
        ///
        /// The default value is 0x0.
        pub fn set_color(&mut self, color: Color) {
            unsafe { Fl_Terminal_set_color(self.inner.widget() as _, color.bits()) }
        }

        /// Return the cursor's current column position on the screen.
        pub fn cursor_col(&self) -> i32 {
            unsafe { Fl_Terminal_cursor_col(self.inner.widget() as _) }
        }

        /// Set the cursor's current column position on the screen.
        /// *This is a low-level "protected" function of the fltk library*
        pub fn set_cursor_col(&mut self, val: i32) {
            unsafe { Fl_Terminal_set_cursor_col(self.inner.widget() as _, val) }
        }

        /// Return the cursor's current row position on the screen.
        pub fn cursor_row(&self) -> i32 {
            unsafe { Fl_Terminal_cursor_row(self.inner.widget() as _) }
        }

        /// Set the cursor's current row position on the screen.
        /// *This is a low-level "protected" function of the fltk library*
        pub fn set_cursor_row(&mut self, val: i32) {
            unsafe { Fl_Terminal_set_cursor_row(self.inner.widget() as _, val) }
        }

        /// Moves cursor up `count` lines.
        ///  If cursor hits screen top, it either stops (does not wrap) if `do_scroll`
        ///  is false, or scrolls down if `do_scroll` is true.
        /// *This is a low-level "protected" function of the fltk library*
        pub fn cursor_up(&mut self, count: i32, do_scroll: bool) {
            unsafe { Fl_Terminal_cursor_up(self.inner.widget() as _, count, do_scroll as i32) }
        }

        /// Moves cursor down `count` lines.
        ///  If cursor hits screen bottom, it either stops (does not wrap) if `do_scroll`
        ///  is false, or wraps and scrolls up if `do_scroll` is true.
        /// *This is a low-level "protected" function of the fltk library*
        pub fn cursor_down(&mut self, count: i32, do_scroll: bool) {
            unsafe { Fl_Terminal_cursor_down(self.inner.widget() as _, count, do_scroll as i32) }
        }

        /// Moves cursor left `count` columns, and cursor stops (does not wrap) if it hits screen edge.
        /// *This is a low-level "protected" function of the fltk library*
        pub fn cursor_left(&mut self, count: i32) {
            unsafe { Fl_Terminal_cursor_left(self.inner.widget() as _, count) }
        }

        /// Moves cursor right `count` columns. If cursor hits right edge of screen,
        ///  it either stops (does not wrap) if `do_scroll` is false, or wraps and
        ///  scrolls up one line if `do_scroll` is true.
        /// *This is a low-level "protected" function of the fltk library*
        pub fn cursor_right(&mut self, count: i32, do_scroll: bool) {
            unsafe { Fl_Terminal_cursor_right(self.inner.widget() as _, count, do_scroll as i32) }
        }

        /// Scroll the selection up(+)/down(-) number of rows
        /// *This is a low-level "protected" function of the fltk library*
        pub fn scroll(&mut self, count: i32) {
            unsafe { Fl_Terminal_scroll(self.inner.widget() as _, count) }
        }

        /// Clear from cursor to End Of Display (EOD), like "`<ESC>[J<ESC>[0J`".
        pub fn clear_eod(&mut self) {
            unsafe { Fl_Terminal_clear_eod(self.inner.widget() as _) }
        }

        /// Clear from cursor to End Of Line (EOL), like "`<ESC>[K`".
        pub fn clear_eol(&mut self) {
            unsafe { Fl_Terminal_clear_eol(self.inner.widget() as _) }
        }

        /// Clear entire line cursor is currently on.
        pub fn clear_cur_line(&mut self) {
            unsafe { Fl_Terminal_clear_cur_line(self.inner.widget() as _) }
        }

        /// Clear entire line for specified row.
        pub fn clear_line(&mut self, drow: i32) {
            unsafe { Fl_Terminal_clear_line(self.inner.widget() as _, drow) }
        }

        /// Clear from cursor to Start Of Display (EOD), like "`<ESC>[1J`".
        pub fn clear_sod(&mut self) {
            unsafe { Fl_Terminal_clear_sod(self.inner.widget() as _) }
        }

        /// Clear from cursor to Start Of Line (SOL), like "`<ESC>[1K`".
        pub fn clear_sol(&mut self) {
            unsafe { Fl_Terminal_clear_sol(self.inner.widget() as _) }
        }

        ///   Insert char `c` at the current cursor position for `rep`` times.
        ///   Works only for single-byte characters, `c` can't be multi-byte UTF-8.
        ///   Does not wrap; characters at end of line are lost.
        /// *This is a low-level "protected" function of the fltk library*
        pub fn insert_char(&mut self, c: char, rep: i32) {
            let c = if c.len_utf8() > 1 { b' ' } else { c as u8 };
            unsafe { Fl_Terminal_insert_char(self.inner.widget() as _, c as c_char, rep) }
        }

        /// Insert char `c` for `rep` times at display row `drow` and column `dcol`.
        ///   Works only for single-byte characters, `c` can't be multi-byte UTF-8.
        ///   Does not wrap; characters at end of line are lost.
        /// *This is a low-level "protected" function of the fltk library*
        pub fn insert_char_eol(&mut self, c: char, drow: i32, dcol: i32, rep: i32) {
            let c = if c.len_utf8() > 1 { b' ' } else { c as u8 };
            unsafe {
                Fl_Terminal_insert_char_eol(self.inner.widget() as _, c as c_char, drow, dcol, rep)
            }
        }

        /// Insert `count` rows at current cursor position.
        ///  Causes rows below to scroll down, and empty lines created.
        ///  Lines deleted by scroll down are NOT moved into the scroll history.
        /// *This is a low-level "protected" function of the fltk library*
        pub fn insert_rows(&mut self, count: i32) {
            unsafe { Fl_Terminal_insert_rows(self.inner.widget() as _, count) }
        }

        /// Delete char(s) at (`drow`,`dcol`) for `count` times.
        pub fn delete_chars(&mut self, drow: i32, dcol: i32, count: i32) {
            unsafe { Fl_Terminal_delete_chars(self.inner.widget() as _, drow, dcol, count) }
        }

        /// Delete char(s) at cursor position for `count` times.
        pub fn delete_cur_chars(&mut self, count: i32) {
            unsafe { Fl_Terminal_delete_cur_chars(self.inner.widget() as _, count) }
        }

        ///  Delete `count` rows at cursor position.
        ///   Causes rows to scroll up, and empty lines created at bottom of screen.
        ///    Lines deleted by scroll up are NOT moved into the scroll history.
        /// *This is a low-level "protected" function of the fltk library*
        pub fn delete_rows(&mut self, count: i32) {
            unsafe { Fl_Terminal_delete_rows(self.inner.widget() as _, count) }
        }

        /// Get the cursor's background color used for the cursor itself.
        pub fn cursor_bg_color(&self) -> Color {
            Color::from_rgbi(unsafe { Fl_Terminal_cursor_bg_color(self.inner.widget() as _) })
        }

        /// Set the cursor's background color used for the cursor itself.
        pub fn set_cursor_bg_color(&mut self, color: Color) {
            unsafe { Fl_Terminal_set_cursor_bg_color(self.inner.widget() as _, color.bits()) }
        }

        /// Get the cursor's foreground color used for the cursor itself.
        pub fn cursor_fg_color(&self) -> Color {
            Color::from_rgbi(unsafe { Fl_Terminal_cursor_fg_color(self.inner.widget() as _) })
        }

        /// Set the cursor's foreground color used for the cursor itself.
        pub fn set_cursor_fg_color(&mut self, color: Color) {
            unsafe { Fl_Terminal_set_cursor_fg_color(self.inner.widget() as _, color.bits()) }
        }

        /// Get the current mouse selection. Returns `None` if no selection, or `Some([srow, scol, erow, ecol])` if there is a selection,
        ///   where row and col represent start/end positions in the ring buffer.
        /// *This is a low-level "protected" function of the fltk library*
        pub fn get_selection(&self) -> Option<[i32; 4]> {
            let mut retval: [i32; 4] = [0; 4];
            let ret =
                unsafe { Fl_Terminal_get_selection(self.inner.widget() as _, retval.as_mut_ptr()) };
            if ret != 0 {
                Some(retval)
            } else {
                None
            }
        }

        /// Move cursor to the home position (top/left).
        pub fn cursor_home(&mut self) {
            unsafe { Fl_Terminal_cursor_home(self.inner.widget() as _) }
        }

        /// Return terminal's display width in columns of text characters.
        pub fn display_columns(&self) -> i32 {
            unsafe { Fl_Terminal_display_columns(self.inner.widget() as _) }
        }

        /// Set terminal's display width in columns of text characters.
        pub fn set_display_columns(&mut self, val: i32) {
            unsafe { Fl_Terminal_set_display_columns(self.inner.widget() as _, val) }
        }

        /// Return terminal's display height in lines of text.
        pub fn display_rows(&self) -> i32 {
            unsafe { Fl_Terminal_display_rows(self.inner.widget() as _) }
        }

        /// Set terminal's display height in lines of text.
        pub fn set_display_rows(&mut self, val: i32) {
            unsafe { Fl_Terminal_set_display_rows(self.inner.widget() as _, val) }
        }

        /// Sets the number of lines of screen history.
        pub fn set_history_lines(&mut self, arg1: i32) {
            unsafe { Fl_Terminal_set_history_lines(self.inner.widget() as _, arg1) }
        }

        /// Gets the number of lines of screen history.
        pub fn history_lines(&self) -> i32 {
            unsafe { Fl_Terminal_history_lines(self.inner.widget() as _) }
        }

        /// Sets the terminal's scrollback history buffer size in lines of text (rows).
        pub fn set_history_rows(&mut self, arg1: i32) {
            unsafe { Fl_Terminal_set_history_rows(self.inner.widget() as _, arg1) }
        }

        /// Gets the terminal's scrollback history buffer size in lines of text (rows).
        pub fn history_rows(&self) -> i32 {
            unsafe { Fl_Terminal_history_rows(self.inner.widget() as _) }
        }

        /// Returns how many lines are "in use" by the screen history buffer.
        pub fn history_use(&self) -> i32 {
            unsafe { Fl_Terminal_history_use(self.inner.widget() as _) }
        }

        /// Set the bottom margin
        pub fn set_margin_bottom(&mut self, arg1: i32) {
            unsafe { Fl_Terminal_set_margin_bottom(self.inner.widget() as _, arg1) }
        }

        /// Return the bottom margin
        pub fn margin_bottom(&self) -> i32 {
            unsafe { Fl_Terminal_margin_bottom(self.inner.widget() as _) }
        }

        /// Set the left margin
        pub fn set_margin_left(&mut self, arg1: i32) {
            unsafe { Fl_Terminal_set_margin_left(self.inner.widget() as _, arg1) }
        }

        /// Return the left margin
        pub fn margin_left(&self) -> i32 {
            unsafe { Fl_Terminal_margin_left(self.inner.widget() as _) }
        }

        /// Set the right margin
        pub fn set_margin_right(&mut self, arg1: i32) {
            unsafe { Fl_Terminal_set_margin_right(self.inner.widget() as _, arg1) }
        }

        /// Return the right margin
        pub fn margin_right(&self) -> i32 {
            unsafe { Fl_Terminal_margin_right(self.inner.widget() as _) }
        }

        /// Set the top margin
        pub fn set_margin_top(&mut self, arg1: i32) {
            unsafe { Fl_Terminal_set_margin_top(self.inner.widget() as _, arg1) }
        }

        /// Return the top margin
        pub fn margin_top(&self) -> i32 {
            unsafe { Fl_Terminal_margin_top(self.inner.widget() as _) }
        }

        /// Sets the combined output translation flags to `val`.
        ///
        /// `val` can be sensible combinations of the OutFlags bit flags.
        ///
        /// The default is LF_TO_CRLF, so that \\n will generate both carriage-return (CR)
        /// and line-feed (LF).
        ///
        /// For \\r and \\n to be handled literally, use output_translate(Terminal::OutFlags::OFF);
        /// To disable all output translations, use 0 or Terminal::OutFlags::OFF.
        pub fn set_output_translate(&mut self, val: OutFlags) {
            unsafe { Fl_Terminal_set_output_translate(self.inner.widget() as _, val.bits() as u32) }
        }

        /// Return the current combined output translation flags.
        pub fn output_translate(&self) -> OutFlags {
            let result = unsafe { Fl_Terminal_output_translate(self.inner.widget() as _) as i32 };
            OutFlags::from_bits(result as u8).unwrap_or_else(|| {
                panic!("Unknown OutFlags value {} from output_translate", result)
            })
        }

        /// Prints single ASCII char `c` at current cursor position, and advances the cursor.
        /// - `c` must be ASCII, not utf-8
        /// - Does not trigger redraws
        pub fn print_char(&mut self, c: char) {
            unsafe { Fl_Terminal_print_char(self.inner.widget() as _, c as std::os::raw::c_char) }
        }

        ///   Prints single UTF-8 char `c` at current cursor position, and advances the cursor if the character
        ///   is printable. Handles ASCII and control codes (CR, LF, etc).
        ///
        ///  The character is displayed at the current cursor position
        ///  using the current text color/attributes.
        ///
        /// Handles control codes and can be used to construct ANSI/XTERM escape sequences.
        /// - `c` must be a single char only (whether UTF-8 or ASCII)
        /// - `c` can be an ASCII character, though not as efficent as print_char()
        /// - Invalid UTF-8 chars show the error character (¿) depending on show_unknown(bool).
        /// - Does not trigger redraws
        pub fn print_char_utf8(&mut self, c: char) {
            let txt = c.to_string();
            unsafe {
                Fl_Terminal_print_char_utf8(
                    self.inner.widget() as _,
                    txt.as_ptr() as _,
                    txt.len() as _,
                )
            }
        }

        /// Print the ASCII character `c` at the terminal's display position `(drow,dcol)`.
        ///   The character MUST be printable (in range 0x20 - 0x7e), and is displayed
        ///   using the current text color/attributes. Characters outside that range are either
        ///   ignored or print the error character (¿), depending on show_unknown(bool).
        ///
        /// No range checking is done on drow,dcol:
        /// - drow must be in range `0..(display_rows()-1)`
        /// - dcol must be in range `0..(display_columns()-1)`
        /// - Does not trigger redraws
        /// - Does NOT handle control codes, ANSI or XTERM escape sequences.
        pub fn plot_char(&mut self, c: char, row: i32, col: i32) {
            unsafe {
                Fl_Terminal_plot_char(
                    self.inner.widget() as _,
                    c as std::os::raw::c_char,
                    row,
                    col,
                )
            }
        }

        /// Print a single UTF-8 character len at display position `(drow,dcol)`.
        /// The character is displayed using the current text color/attributes.
        ///
        /// This is a very low level method.
        /// No range checking is done on drow,dcol:
        /// -  drow must be in range `0..(display_rows()-1)`
        /// -  dcol must be in range `0..(display_columns()-1)`
        /// - Does not trigger redraws
        /// - Does not handle ANSI or XTERM escape sequences
        /// - Invalid UTF-8 chars show the error character (¿) depending on show_unknown(bool).
        pub fn plot_char_utf8(&mut self, c: char, drow: i32, dcol: i32) {
            let txt = c.to_string();
            unsafe {
                Fl_Terminal_plot_char_utf8(
                    self.inner.widget() as _,
                    txt.as_ptr() as _,
                    txt.len() as _,
                    drow,
                    dcol,
                )
            }
        }

        /// Set the maximum rate redraw speed in floating point seconds if redraw_style() is set to RATE_LIMITED.
        pub fn set_redraw_rate(&mut self, set: f32) {
            unsafe { Fl_Terminal_set_redraw_rate(self.inner.widget() as _, set) }
        }

        /// Get max rate redraw speed in floating point seconds.
        pub fn redraw_rate(&self) -> f32 {
            unsafe { Fl_Terminal_redraw_rate(self.inner.widget() as _) }
        }

        /// Set how Terminal manages screen redrawing.
        pub fn set_redraw_style(&mut self, set: RedrawStyle) {
            unsafe { Fl_Terminal_set_redraw_style(self.inner.widget() as _, set.bits() as i32) }
        }

        /// Get the redraw style.
        pub fn redraw_style(&self) -> RedrawStyle {
            let result = unsafe { Fl_Terminal_redraw_style(self.inner.widget() as _) as u32 };
            RedrawStyle::new(result) // Construct a style with the given value
        }

        /// Resets terminal to default colors, clears screen, history and mouse selection, homes cursor, resets tabstops.
        pub fn reset_terminal(&mut self) {
            unsafe { Fl_Terminal_reset_terminal(self.inner.widget() as _) }
        }

        /// Returns the scrollbar's actual size; actual width for vertical scrollbars, actual height for horizontal scrollbars.
        pub fn scrollbar_actual_size(&self) -> i32 {
            unsafe { Fl_Terminal_scrollbar_actual_size(self.inner.widget() as _) }
        }

        /// Get the current size of the scrollbar's trough, in pixels.
        /// If this value is zero (default), this widget is using fltk's
        /// master scrollbar_size() value
        pub fn scrollbar_size(&self) -> i32 {
            unsafe { Fl_Terminal_scrollbar_size(self.inner.widget() as _) }
        }

        /// Set the width of the scrollbar's trough to val, in pixels.
        /// If this value is zero (default), this widget will use fltk's
        /// master scrollbar_size() value
        pub fn set_scrollbar_size(&mut self, val: i32) {
            unsafe { Fl_Terminal_set_scrollbar_size(self.inner.widget() as _, val) }
        }

        /// Get mouse selection background color.
        pub fn selection_bg_color(&self) -> Color {
            Color::from_rgbi(unsafe { Fl_Terminal_selection_bg_color(self.inner.widget() as _) })
        }

        /// Set mouse selection background color.
        pub fn set_selection_bg_color(&mut self, color: Color) {
            unsafe { Fl_Terminal_set_selection_bg_color(self.inner.widget() as _, color.bits()) }
        }

        /// Get mouse selection foreground color.
        pub fn selection_fg_color(&self) -> Color {
            Color::from_rgbi(unsafe { Fl_Terminal_selection_fg_color(self.inner.widget() as _) })
        }

        /// Set mouse selection foreground color.
        pub fn set_selection_fg_color(&mut self, color: Color) {
            unsafe { Fl_Terminal_set_selection_fg_color(self.inner.widget() as _, color.bits()) }
        }

        /// Return the "show unknown" flag. if true, show unknown chars as '¿'
        pub fn show_unknown(&self) -> bool {
            unsafe { Fl_Terminal_show_unknown(self.inner.widget() as _) != 0 }
        }

        /// Set the "show unknown" flag. if true, show unknown chars as '¿' (default off)
        pub fn set_show_unknown(&mut self, arg1: bool) {
            unsafe { Fl_Terminal_set_show_unknown(self.inner.widget() as _, arg1 as i32) }
        }

        /// Return the text attribute bits (underline, inverse, etc) for subsequent appends.
        pub fn text_attrib(&self) -> Attrib {
            // Attrib::from_bits( unsafe { Fl_Terminal_text_attrib(self.inner.widget()) as _ } ).unwrap()
            let result = unsafe { Fl_Terminal_text_attrib(self.inner.widget() as _) };
            Attrib::from_bits(result).unwrap_or_else(|| panic!("Unknown Attrib value {}", result))
        }

        /// Set text attribute bits (underline, inverse, etc) for subsequent appends.
        pub fn set_text_attrib(&mut self, arg1: Attrib) {
            unsafe { Fl_Terminal_set_text_attrib(self.inner.widget() as _, arg1.bits()) }
        }

        /// Set text background color to fltk color val.
        /// Use this for temporary color changes, similar to \<ESC\>[48;2;{R};{G};{B}m
        ///
        /// This setting does not affect the 'default' text colors used by \<ESC\>[0m, \<ESC\>c, reset_terminal(), etc.
        /// To change both the current and default bg color, also use text_bg_color_default(Fl_Color).
        pub fn set_text_bg_color(&mut self, color: Color) {
            unsafe { Fl_Terminal_set_text_bg_color(self.inner.widget() as _, color.bits()) }
        }

        /// Get the text background color.
        pub fn text_bg_color(&self) -> Color {
            Color::from_rgbi(unsafe { Fl_Terminal_text_bg_color(self.inner.widget() as _) })
        }

        /// Set the default text background color used by \<ESC\>c, \<ESC\>[0m, and reset_terminal().
        /// Does not affect the 'current' text fg color; use set_text_bg_color(Fl_Color) to set that.
        pub fn set_text_bg_color_default(&mut self, color: Color) {
            unsafe { Fl_Terminal_set_text_bg_color_default(self.inner.widget() as _, color.bits()) }
        }

        /// Return the default text background color.
        pub fn text_bg_color_default(&self) -> Color {
            Color::from_rgbi(unsafe { Fl_Terminal_text_bg_color_default(self.inner.widget() as _) })
        }

        /// Sets the background text color as one of the 8 'xterm color' values.
        ///
        /// This will be the background color used for all newly printed text, similar to the \<ESC\>[#m escape sequence, where # is between 40 and 47.
        ///
        /// This color will be reset to the default bg color if reset_terminal() is called, or by \<ESC\>c, \<ESC\>[0m, etc.
        ///
        /// The xterm color intensity values can be influenced by the Dim/Bold/Normal modes (which can be set with e.g. \<ESC\>[1m, textattrib(), etc), so the actual RGB values of these colors allow room for Dim/Bold to influence their brightness. For instance, "Normal Red" is not full brightness to allow "Bold Red" to be brighter. This goes for all colors except 'Black', which is not influenced by Dim or Bold; Black is always Black.
        ///
        /// These background colors are slightly dimmer than the corresponding xterm foregroumd colors.
        ///
        /// The 8 color xterm values are:
        /// 0 = Black, 1 = Red, 2 = Green, 3 = Yellow, 4 = Blue,5 = Magenta, 6 = Cyan, 7 = White
        pub fn set_text_bg_color_xterm(&mut self, color: XtermColor) {
            unsafe { Fl_Terminal_set_text_bg_color_xterm(self.inner.widget() as _, color as u8) }
        }
        ///  Set the text color for the terminal.
        ///  This is a convenience method that sets *both* textfgcolor() and textfgcolor_default(),
        ///  ensuring both are set to the same value.
        pub fn set_text_color(&mut self, color: Color) {
            unsafe { Fl_Terminal_set_text_color(self.inner.widget() as _, color.bits()) }
        }
        /// Set text foreground drawing color to fltk color val.
        /// Use this for temporary color changes, similar to \<ESC\>[38;2;{R};{G};{B}m
        ///
        /// This setting does not affect the 'default' text colors used by \<ESC\>[0m, \<ESC\>c, reset_terminal(), etc.
        /// To change both the current and default fg color, also use textfgcolor_default(Fl_Color)
        pub fn set_text_fg_color(&mut self, color: Color) {
            unsafe { Fl_Terminal_set_text_fg_color(self.inner.widget() as _, color.bits()) }
        }

        /// Get the text foreground color.
        pub fn text_fg_color(&self) -> Color {
            Color::from_rgbi(unsafe { Fl_Terminal_text_fg_color(self.inner.widget() as _) })
        }

        /// Set the default text foreground color used by \<ESC\>c, \<ESC\>[0m, and reset_terminal().
        /// Does not affect the 'current' text fg color; use set_text_fg_color(Fl_Color) to set that.
        pub fn set_text_fg_color_default(&mut self, color: Color) {
            unsafe { Fl_Terminal_set_text_fg_color_default(self.inner.widget() as _, color.bits()) }
        }

        /// Return the default text foreground color.
        pub fn text_fg_color_default(&self) -> Color {
            Color::from_rgbi(unsafe { Fl_Terminal_text_fg_color_default(self.inner.widget() as _) })
        }

        /// Sets the foreground text color as one of the 8 'xterm color' values.
        ///
        /// This will be the foreground color used for all newly printed text, similar to the \<ESC\>[#m escape sequence, where # is between 30 and 37.
        ///
        /// This color will be reset to the default bg color if reset_terminal() is called, or by \<ESC\>c, \<ESC\>[0m, etc.
        ///
        /// The xterm color intensity values can be influenced by the Dim/Bold/Normal modes (which can be set with e.g. \<ESC\>[1m, textattrib(), etc), so the actual RGB values of these colors allow room for Dim/Bold to influence their brightness. For instance, "Normal Red" is not full brightness to allow "Bold Red" to be brighter. This goes for all colors except 'Black', which is not influenced by Dim or Bold; Black is always Black.
        ///
        /// The 8 color xterm values are:
        /// 0 = Black, 1 = Red, 2 = Green, 3 = Yellow, 4 = Blue,5 = Magenta, 6 = Cyan, 7 = White
        pub fn set_text_fg_color_xterm(&mut self, color: XtermColor) {
            unsafe { Fl_Terminal_set_text_fg_color_xterm(self.inner.widget() as _, color as u8) }
        }

        /// Get the text font
        pub fn text_font(&self) -> Font {
            Font::by_index(unsafe { Fl_Terminal_text_font(self.inner.widget() as _) } as usize)
        }

        /// Sets the font used for all text displayed in the terminal.
        /// This affects all existing text (in display and history) as well as any newly printed text.
        /// Only monospace fonts are recommended.
        pub fn set_text_font(&mut self, font: Font) {
            unsafe { Fl_Terminal_set_text_font(self.inner.widget() as _, font.bits()) }
        }

        /// Gets the text size
        pub fn text_size(&self) -> i32 {
            unsafe { Fl_Terminal_text_size(self.inner.widget() as _) }
        }

        /// Sets the font size used for all text displayed in the terminal.
        /// This affects all existing text (in display and history) as well as any newly printed text.
        /// Changing this will affect the display_rows() and display_columns().
        pub fn set_text_size(&mut self, val: i32) {
            unsafe { Fl_Terminal_set_text_size(self.inner.widget() as _, val) }
        }

        /// Gets the selection text
        /// *This is a low-level "protected" function of the fltk library*
        pub fn selection_text(&self) -> String {
            assert!(self.is_derived);
            unsafe {
                let ptr = Fl_Terminal_selection_text(self.inner.widget() as _);
                if ptr.is_null() {
                    String::new()
                } else {
                    CStr::from_ptr(ptr).to_string_lossy().to_string()
                }
            }
        }

        // Various methods to access the ring buffer

        ///  Return the ending row# in the display area.
        pub fn disp_erow(&self) -> i32 {
            unsafe { Fl_Terminal_disp_erow(self.inner.widget() as _) }
        }

        /// Return the number of rows in the display area.
        pub fn disp_rows(&self) -> i32 {
            unsafe { Fl_Terminal_disp_rows(self.inner.widget() as _) }
        }

        /// Return the number of columns in the display area (always the same as ring_cols())
        pub fn disp_cols(&self) -> i32 {
            unsafe { Fl_Terminal_disp_cols(self.inner.widget() as _) }
        }

        /// Return the starting row# in the display area.
        pub fn disp_srow(&self) -> i32 {
            unsafe { Fl_Terminal_disp_srow(self.inner.widget() as _) }
        }

        /// Return the number of columns in the scrollback history (always the same as ring_cols())
        pub fn hist_cols(&self) -> i32 {
            unsafe { Fl_Terminal_hist_cols(self.inner.widget() as _) }
        }

        /// Return the ending row# of the scrollback history.
        pub fn hist_erow(&self) -> i32 {
            unsafe { Fl_Terminal_hist_erow(self.inner.widget() as _) }
        }

        /// Return the number of rows in the scrollback history.
        pub fn hist_rows(&self) -> i32 {
            unsafe { Fl_Terminal_hist_rows(self.inner.widget() as _) }
        }

        /// Return the starting row# of the scrollback history.
        pub fn hist_srow(&self) -> i32 {
            unsafe { Fl_Terminal_hist_srow(self.inner.widget() as _) }
        }

        /// Return number of rows in use by the scrollback history.
        pub fn hist_use(&self) -> i32 {
            unsafe { Fl_Terminal_hist_use(self.inner.widget() as _) }
        }

        /// Return the starting row of the \"in use\" scrollback history.
        pub fn hist_use_srow(&self) -> i32 {
            unsafe { Fl_Terminal_hist_use_srow(self.inner.widget() as _) }
        }

        /// Is global row/column inside the current mouse selection?
        /// *This is a low-level "protected" function of the fltk library*
        pub fn is_inside_selection(&self, row: i32, col: i32) -> bool {
            unsafe { Fl_Terminal_is_inside_selection(self.inner.widget() as _, row, col) != 0 }
        }

        /// Returns true if there's a mouse selection.
        pub fn is_selection(&self) -> bool {
            unsafe { Fl_Terminal_is_selection(self.inner.widget() as _) != 0 }
        }

        /// Returns the current offset into the ring buffer.
        pub fn offset(&self) -> i32 {
            unsafe { Fl_Terminal_offset(self.inner.widget() as _) }
        }

        /// Return the number of columns in the ring buffer.
        pub fn ring_cols(&self) -> i32 {
            unsafe { Fl_Terminal_ring_cols(self.inner.widget() as _) }
        }

        /// Return the ending row# in the ring buffer (Always ring_rows()-1)
        pub fn ring_erow(&self) -> i32 {
            unsafe { Fl_Terminal_ring_erow(self.inner.widget() as _) }
        }

        /// Return the starting row# in the ring buffer (Always 0)
        pub fn ring_srow(&self) -> i32 {
            unsafe { Fl_Terminal_ring_srow(self.inner.widget() as _) }
        }

        /// Return the number of rows in the ring buffer.
        pub fn ring_rows(&self) -> i32 {
            unsafe { Fl_Terminal_ring_rows(self.inner.widget() as _) }
        }

        /// Return the Utf8Char for character under cursor.
        pub fn u8c_cursor(&self) -> Utf8Char {
            unsafe {
                let x = self.inner.widget();
                let utf8_p = Fl_Terminal_u8c_cursor(x as _);
                Utf8Char { inner: utf8_p }
            }
        }

        /// Return u8c for beginning of row drow of the display.
        /// *This is a low-level "protected" function of the fltk library*
        pub fn u8c_disp_row(&self, drow: i32) -> BuffRow {
            // Fl_Terminal_u8c_disp_row returns pointer to the first C++ Utf8Char object,
            //  which becomes the `inner` element in the Rust BuffRow object
            let row_p = unsafe { Fl_Terminal_u8c_disp_row(self.inner.widget() as _, drow) };
            BuffRow::new(row_p, self)
        }

        /// Return u8c for beginning of row hrow inside the scrollback history.
        /// *This is a low-level "protected" function of the fltk library*
        pub fn u8c_hist_row(&self, hrow: i32) -> BuffRow {
            // Fl_Terminal_u8c_hist_row returns pointer to the first C++ Utf8Char object,
            //  which becomes the `inner` element in the Rust BuffRow object
            let row_p = unsafe { Fl_Terminal_u8c_hist_row(self.inner.widget() as _, hrow) };
            BuffRow::new(row_p, self)
        }

        /// Return u8c for beginning of row hurow inside the 'in use' part of the\n scrollback history.
        /// *This is a low-level "protected" function of the fltk library*
        pub fn u8c_hist_use_row(&self, hurow: i32) -> BuffRow {
            // Fl_Terminal_u8c_hist_use_row returns pointer to the first  C++ Utf8Char object,
            //  which becomes the `inner` element in the Rust BuffRow object
            let row_p = unsafe { Fl_Terminal_u8c_hist_use_row(self.inner.widget() as _, hurow) };
            BuffRow::new(row_p, self)
        }

        /// Return u8c for beginning of row grow in the ring buffer.
        /// *This is a low-level "protected" function of the fltk library*
        pub fn u8c_ring_row(&self, grow: i32) -> BuffRow {
            // Fl_Terminal_u8c_ring_use_row returns pointer to the first  C++ Utf8Char object,
            //  which becomes the `inner` element in the Rust BuffRow object
            let row_p = unsafe { Fl_Terminal_u8c_ring_row(self.inner.widget() as _, grow) };
            BuffRow::new(row_p, self)
        }
    }

    // So far only implementing "getter" methods. Todo: methods to modify Utf8Char
    impl Utf8Char {
        /// Construct a new Utf8Char, single-byte only. This is really only useful for testing.
        ///  'c' must be "printable" ASCII in the range (0x20 <= c <= 0x7e).
        ///     Anything outside of that is silently ignored.
        ///
        /// Allocated Utf8Char will never be deleted.
        pub fn new(c: u8) -> Self {
            unsafe {
                let u8c = Fl_Terminal_Utf8Char_new_obj(c);
                Utf8Char { inner: u8c }
            }
        }

        /// Return the actual displayed color of char `u8c` possibly influenced by BOLD or DIM if the char is from Xterm.
        ///    BG color will be derived from the widget color if a widget is specified and the color is `TransparentBg`,
        ///    and that won't be influenced by charflag attributes.
        pub fn attr_bgcolor(&self, term: Option<&Terminal>) -> Color {
            Color::from_rgbi(match term {
                None => unsafe { Fl_Terminal_Utf8Char_attr_bgcolor(self.inner, std::ptr::null()) },
                Some(t) => unsafe {
                    Fl_Terminal_Utf8Char_attr_bgcolor(self.inner, t.inner.widget() as _)
                },
            })
        }

        // /// Return the actual displayed color of char `u8c` possibly influenced by BOLD or DIM if the char is from Xterm.
        // ///    If a `grp` widget is specified (i.e. not `None`), don't let the color be
        // ///    influenced by the attribute bits *if* it matches the `grp` widget's own `color()`.
        // pub fn attr_color(&self, grp: Option<*const Fl_Widget>) -> Color {
        //     Color::from_rgbi(match grp {
        //         None => unsafe { Fl_Terminal_Utf8Char_attr_color(self.inner, std::ptr::null()) },
        //         Some(g) => unsafe { Fl_Terminal_Utf8Char_attr_color(self.inner, g) },
        //     })
        // }

        /// Return the actual displayed fg color of char `u8c` possibly influenced by BOLD or DIM if the char is from Xterm.
        ///    If a `term` widget is specified (i.e. not `None`), don't let the color be
        ///    influenced by the attribute bits *if* it matches the `term` widget's own `color()`.
        pub fn attr_fgcolor(&self, term: Option<&Terminal>) -> Color {
            Color::from_rgbi(match term {
                None => unsafe { Fl_Terminal_Utf8Char_attr_fgcolor(self.inner, std::ptr::null()) },
                Some(t) => unsafe {
                    Fl_Terminal_Utf8Char_attr_fgcolor(self.inner, t.inner.widget() as _)
                },
            })
        }

        /// Return the attributes for this character.
        pub fn attrib(&self) -> Attrib {
            let result = unsafe { Fl_Terminal_Utf8Char_attrib(self.inner) };
            Attrib::from_bits(result).unwrap_or_else(|| panic!("Unknown Attrib value {}", result))
        }

        /// Return the background color for this character.
        pub fn bgcolor(&self) -> Color {
            Color::from_rgbi(unsafe { Fl_Terminal_Utf8Char_bgcolor(self.inner) })
        }

        /// Return the foreground color for this character.
        pub fn fgcolor(&self) -> Color {
            let result = unsafe { Fl_Terminal_Utf8Char_fgcolor(self.inner) };
            Color::from_rgbi(result)
        }

        /// Return the xterm CharFlags bits
        pub fn charflags(&self) -> CharFlags {
            let result = unsafe { Fl_Terminal_Utf8Char_charflags(self.inner) as i32 };
            CharFlags::from_bits(result as u8)
                .unwrap_or_else(|| panic!("Unknown CharFlags value {}", result))
        }

        /// Returns true if the character text in this struct matches the given ASCII character
        pub fn is_char(&self, c: u8) -> bool {
            let result = unsafe { Fl_Terminal_Utf8Char_is_char(self.inner, c as c_char) as i32 };
            result != 0
        }

        /// Return the length of this character in bytes (UTF-8 can be multibyte)
        pub fn length(&self) -> usize {
            unsafe { Fl_Terminal_Utf8Char_length(self.inner) as usize }
        }

        /// Return the maximum length in bytes of a UTF-8 character
        pub fn max_utf8(&self) -> usize {
            unsafe { Fl_Terminal_Utf8Char_max_utf8(self.inner) as usize }
        }

        /// Return the width of this character in floating point pixels.
        ///
        ///    WARNING: Uses current font, so assumes font and font_size
        ///             have already been set to current font!
        pub fn pwidth(&self) -> f64 {
            unsafe { Fl_Terminal_Utf8Char_pwidth(self.inner) as f64 }
        }

        /// Return the width of this character in integer pixels.
        ///
        ///    WARNING: Uses current font, so assumes font and font_size
        ///             have already been set to current font!
        pub fn pwidth_int(&self) -> usize {
            unsafe { Fl_Terminal_Utf8Char_pwidth_int(self.inner) as usize }
        }

        /// Return the UTF-8 text string for this character.
        pub fn text_utf8(&self) -> &[u8] {
            unsafe {
                let ptr = Fl_Terminal_Utf8Char_text_utf8(self.inner);
                let len = Fl_Terminal_Utf8Char_length(self.inner);
                std::slice::from_raw_parts(ptr, len as usize)
            }
        }

        /// Return the size of a Utf8Char object in the underlying C++ code
        pub fn size() -> usize {
            unsafe { Fl_Terminal_Utf8Char_size() as usize }
        }
    }

    impl<'a> BuffRow<'a> {
        /// Generate a new BuffRow object based on a pointer from C++ Fl_Terminal
        pub fn new(ptr: *const Fl_Terminal_Utf8Char, parent: &'a Terminal) -> Self {
            unsafe {
                BuffRow {
                    // inner is the pointer to the first C++ Utf8Char in the row
                    inner: ptr,
                    _parent: parent,
                    // length: (i + 1) as usize,
                    length: parent.ring_cols() as usize,
                    char_size: Fl_Terminal_Utf8Char_size() as usize,
                }
            }
        }

        /// Trim trailing blanks off of BuffRow object.
        /// Does not affect the data in the RingBuff, just this object's access.
        pub fn trim(mut self) -> Self {
            unsafe {
                let mut last_char = self.inner.add((self.length - 1) * self.char_size);
                let c = Utf8Char { inner: last_char };
                // If the last character is a blank, trim the length back.
                if c.text_utf8() == b" " {
                    // Record the attributes etc of the last character
                    let attr = c.attrib();
                    let fg = c.fgcolor();
                    let bg = c.bgcolor();
                    self.length -= 1; // Already checked the last character
                    while self.length > 0 {
                        last_char = last_char.sub(self.char_size);
                        let c = Utf8Char { inner: last_char };
                        if c.text_utf8() != b" "
                            || c.attrib() != attr
                            || c.fgcolor() != fg
                            || c.bgcolor() != bg
                        {
                            break; // Found a non-blank character or one with attrib changes
                        }
                        self.length -= 1;
                    }
                }
            }
            self
        }

        /// Index into row array of Utf8Char
        pub fn col(&self, idx: usize) -> Utf8Char {
            if idx > self.length {
                panic!("Index {} out of range", idx);
            }
            unsafe {
                let base = self.inner;
                Utf8Char {
                    inner: base.add(idx * self.char_size),
                }
            }
        }

        /// Iterator object to step through a sequence of Utf8Char in a BuffRow
        pub fn iter(&self) -> BuffRowIter {
            BuffRowIter::new(self, self.length)
        }
    }

    /// Iterator object to step through a sequence of Utf8Char in a BuffRow
    pub struct BuffRowIter<'a> {
        parent: &'a BuffRow<'a>,
        ptr: *const Fl_Terminal_Utf8Char, // This points to an array of Fl_Terminal::Utf8Char
        end: *const Fl_Terminal_Utf8Char, // points just past the ptr array end
    }

    impl<'a> BuffRowIter<'a> {
        fn new(parent: &'a BuffRow, len: usize) -> BuffRowIter<'a> {
            unsafe {
                BuffRowIter {
                    parent,
                    ptr: parent.inner,
                    end: parent.inner.add(len * parent.char_size),
                }
            }
        }
    }

    impl<'a> Iterator for BuffRowIter<'a> {
        type Item = Utf8Char;
        fn next(&mut self) -> Option<Self::Item> {
            if self.ptr < self.end {
                let result = Utf8Char { inner: self.ptr };
                unsafe {
                    self.ptr = self.ptr.add(self.parent.char_size);
                }
                Some(result)
            } else {
                None
            }
        }
    }
}