File: f.gallery.cc

package info (click to toggle)
fotoxx 24.70-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 21,888 kB
  • sloc: cpp: 78,255; makefile: 116; xml: 52
file content (5529 lines) | stat: -rw-r--r-- 222,763 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
/********************************************************************************

   Fotocx - edit photos and manage collections

   Copyright 2007-2024 Michael Cornelison
   source code URL: https://kornelix.net
   contact: mkornelix@gmail.com

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version. See https://www.gnu.org/licenses

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   See the GNU General Public License for more details.

*********************************************************************************

   Fotocx image edit - folder navigation and thumbnail gallery functions

   gallery()                  create/update/search/display an image gallery

   navi::
      gallery_comp            gallery sort compare function
      filename_comp           gallery sort file name compare function
      gallery_paint           paint thumbnail gallery, resizable thumbnails
      gallery_paintmeta       same, metadata report from search function
      gallery_paintmeta2      same, thumbnails with basic metadata
      dir_filecount           paint - get folder subdir and image file counts
      draw_text               paint - write text over thumbnail images
      gallery_navibutts       add folder navigation buttons
      menufuncx               gallery menu: sort/zoom/scroll ...
      gallery_scroll          scroll gallery in rows and pages
      navibutt_clicked        open new gallery from clicked folder
      newtop                  open new gallery from clicked TOP entry
      newalbum                open new gallery from clicked album entry
      mouse_event             process gallery thumbnail clicks
      gallery_dragfile        process thumbnail dragged
      gallery_dropfile        process thumbnail dropped (add new file, arrange album)
      KBaction                process gallery KB actions: zoom, page, top, end

   gallery_memory             save/recall gallery sort and position
   set_gwin_title             update the gallery window title bar
   prev_next_file             get previous/next file from curr_file in current gallery
   prev_next_gallery          get gallery's previous or next gallery
   file_position              get gallery file position

   image_file_type            determine file type (folder, image, RAW, thumbnail, other)
   thumb2imagefile            get corresponding image file for given thumbnail file
   image2thumbfile            get corresponding thumbnail file for given image file
   thumbfile_OK               check if thumbnail file missing or stale
   get_folder_pixbuf          get 'folder' pixbuf image
   get_broken_pixbuf          get 'broken file' pixbuf image
   update_thumbfile           refresh thumbnail file if missing or stale
   thumbfile_set_mod_time     set thumbnail file mod time from image file
   delete_thumbfile           delete thumbnail disk file
   get_thumb_pixbuf           get thumbnail pixbuf for given image file

   gallery_popimage           popup a larger image from a clicked thumbnail
   select_files1              single gallery file selection function and dialog
   select_files               multiple gallery file selection function and dialog

   m_thumbview                gallery thumbnail view
   m_metaview                 gallery metadata view
   m_recentfiles              show gallery of recently viewed files
   add_recent_file            add an image file to the list of recent files
   m_newfiles                 show gallery of newest image files
   m_gallery_sort             sort gallery by file, date, size ...
   m_gallery_screen           screen gallery for newest, oldest, text match
   m_current_folder           set gallery to folder of current image file
   m_recent_folders           set gallery to selected recently used folder
   add_recent_folder          add current folder to top of recent folder list
   m_folder_tree              folder tree view, expand/contract, click for gallery view
   m_select_files             select files for album and batch functions
   m_rename_folder            rename the current folder
   m_add_subfolder            add subfolder to current gallery
   m_bookmarks                goto selected bookmark, edit bookmarks
   m_edit_bookmarks           edit bookmarks

   m_thumbframe               set the video frame for a video thumbnail
   m_show_hidden              dummy menu for KB shortcut "Show Hidden Files"
   m_current_album            dummy menu for KB shortcut "Current Album"

   popup_image                show an image in a small popup window

*********************************************************************************/

#define EX extern                                                                //  disable extern declarations
#include "fotocx.h"                                                              //  (variables in fotocx.h are refs)

/********************************************************************************/

/***                                                gallery index in fotocx.h
      typedef struct {                              current gallery file list in memory
         ch       *file;                            /folder.../filename
         int      folder;                           flag, file is a folder
         ch       fdate[16];                        file date: yyyymmddhhmmss
         ch       pdate[16];                        photo date: yyyymmddhhmmss
         int      fsize;                            file size, bytes
         int      psize;                            image size, pixels
         ch       *mdata;                           metadata (text)
      }  Gindex_t;
***/

namespace navi
{
   #define maxgallerylevs 60                                                     //  max gallery navigation levels
   #define TEXTWIN GTK_TEXT_WINDOW_TEXT                                          //  GDK window of GTK text view
   #define NEVER GTK_POLICY_NEVER
   #define ALWAYS GTK_POLICY_ALWAYS

   #define     thumbxx 5                                                         //  thumbx array size
   int         thumbx[5] = { 512, 360, 256, 180, 128 };                          //  thumbnail sizes
   int         fontx[5] =  {   0,  -1,  -1,  -2,  -3  };                         //  font size wrt appfont
   Gindex_t    *Gindex = 0;                                                      //  gallery index
   int         GFlock = 0;                                                       //  gallery index lock
   int         TClock = 0;                                                       //  thumbnail cache lock
   int         Gfiles = 0;                                                       //  gallery files (incl. subfolders)
   int         Gfolders = 0;                                                     //  gallery subfolder count
   int         Gimages = 0;                                                      //  gallery image file count
   int         preload_trigger = 0;                                              //  kick preload_thumb() thread
   int         preload_start = 0;                                                //  1st file in gallery to check
   int         Gmdrows = 0;                                                      //  text rows in metadata list
   ch          *galleryname = 0;                                                 //  physical folder or file list filename
   GTYPE       gallerytype;                                                      //  gallery type: folder, recent, etc.
   GSORT       gallerysort;                                                      //  gallery sort: f.name, f.date, p.date
   GSEQ        galleryseq;                                                       //  gallery sort: ascending/descending
   int         galleryposn = 0;                                                  //  scroll-to file position (Nth)
   int         Fthumbview = 1;                                                   //  gallery view mode 1/2 = normal/metadata
   GtkWidget   *gallerybutt[60];                                                 //  gallery navi buttons [aaa] [bbb] ...
   ch          *gallerypath[60];                                                 //  corresp. folder names  aaa bbb ...
   GtkWidget   *gallerylabel = 0;                                                //  gallery label (album name ...)
   int         gallerypainted = 0;                                               //  gallery is finished painting
   int         xwinW, xwinH;                                                     //  gallery window current size
   int         thumbsize = 256;                                                  //  initial thumbnail image size
   int         thumbW, thumbH;                                                   //  gallery window thumbnail cell size
   int         fontsize = 10;                                                    //  font size for gallery text
   int         texthh;                                                           //  vertical space req. for metadata text
   int         xrows, xcols;                                                     //  gallery window thumbnail rows, cols
   int         margin = 5;                                                       //  cell margin from left and top edge
   int         genthumbs = 0;                                                    //  count newly generated thumbnails
   int         scrollposn = 0;                                                   //  scroll position
   int         maxscroll;                                                        //  max. scroll position
   ch          *drag_file = 0;                                                   //  selected thumbnail (file)
   int         drag_posn = -1;                                                   //  drag from position
   int         gallery_scrollgoal = -1;                                          //  gallery scroll goal position
   int         gallery_scrollspeed = 0;                                          //  gallery scroll speed pixels/second
   
   zlist_t     *ZLrecent_folders = 0;                                            //  list of recently used folders         24.60

   //  private functions
   int    gallery_comp(ch *rec1, ch *rec2);                                      //  gallery record compare for sort options
   int    filename_comp(ch *file1, ch *file2);                                   //  special file name compare function
   int    gallery_paint(GtkWidget *, cairo_t *);                                 //  gallery window paint, resizable thumbnails
   int    gallery_paintmeta(GtkWidget *Gdrawin, cairo_t *cr);                    //  same, metadata report from search func.
   int    gallery_paintmeta2(GtkWidget *Gdrawin, cairo_t *cr);                   //  same, thumbnails with basic metadata
   void   dir_filecount(ch *dirname, int &ndir, int &nfil);                      //  get subdir and image file counts
   void   draw_text(cairo_t *cr, ch *text, int x, int y, int ww);                //  draw text in gallery window
   void   gallery_navibutts();                                                   //  create navigation buttons in top panel
   void   menufuncx(GtkWidget *, ch *menu);                                      //  function for gallery window buttons
   void   gallery_scroll(int position, int speed);                               //  start gallery slow scroll to position
   int    gallery_scrollfunc(void *);                                            //  gallery scroll timer function
   void   navibutt_clicked(GtkWidget *, int *level);                             //  set gallery via click navigation button
   void   newtop(GtkWidget *widget, GdkEventButton *event);                      //  function for [TOP] button
   void   newtop_menu_event(GtkWidget *, ch *);                                  //  [TOP] menu response function
   void   newalbum(GtkWidget *widget, GdkEventButton *event);                    //  function for [Album] button
   void   newalbum_menu_event(GtkWidget *, ch *);                                //  [Album] menu response function
   int    mouse_event(GtkWidget *widget, GdkEvent *event, void *);               //  gallery window mouse event function
   ch   * gallery_dragfile();                                                    //  start drag-drop, set the file
   void   gallery_dropfile(int mousex, int mousey, ch *file);                    //  accept drag-drop file at position
   int    KBpress(GtkWidget *, GdkEventKey *, void *);                           //  gallery window key press event
}

using namespace zfuncs;
using namespace navi;


/********************************************************************************

   ch * gallery(ch *filez, ch *action, int Nth)

   public function to create/update image gallery (thumbnail window)

   Make a scrolling window of thumbnails for a folder or list of files
   Handle window buttons (up row, down page, open file, etc.)
   Call external functions in response to thumbnail mouse-clicks.

   filez: image file or folder, or file with list of image files

      filez    action   Nth
      -----    ------   ---
      file     init      *       folder gallery, file name sort (folders first, last vers. option)
      file     initF     *       file list gallery, no sort

      *        sort     -2       sort and position via gallery_memory()
      *        sort     -1       sort and position from current params

      file     paint     N       paint, position = file (use N if valid for file)
      0        paint     N       paint, position = N
      0        paint    -1       paint, no change in position

      file     insert    N       add file to gallery at position N (folder or unsorted album)
      *        delete    N       delete Nth file in gallery list
      *        get       N       return file N in gallery list or null if N > last
      file     update    *       update gallery index (Gindex[*]) from image index in memory

   Returned values:
      get:  filespec             returned filespec is subject for zfree().
      getR: filespec reference   no zfree() needed

   thumbnail click functions:
      gallery_Lclick_func()                        default function (open file)
      gallery_Rclick_popup()                       default function (popup menu)
      select_files_Lclick_func()                   select_files active
      select_files_Rclick_func()                   select_files active
      edit_bookmarks_Lclick_func                   edit bookmarks active

*********************************************************************************/

ch * gallery(ch *filez, ch *action, int Nth)
{
   ch          *file, *file2;
   ch          *pp, *pp1, *pp2;
   ch          buff[XFCC];
   ch          **Flist = 0;
   xxrec_t     *xxrec;
   static int  ftf = 1;
   int         err, ii, jj, kk;
   int         cc, cc1, cc2, fposn;
   int         NF, flags;
   FTYPE       ftype;
   FILE        *fid;

   while (! resource_lock(GFlock)) zsleep(0.0001);                               //  lock gallery file list

   if (ftf) {
      ftf = 0;
      cc = maxgallery * sizeof(Gindex_t);
      Gindex = (Gindex_t *) zmalloc(cc,"gallery");                               //  allocate gallery index
      Gfiles = Gfolders = Gimages = 0;                                           //  no files yet
   }

   if (strmatchN(action,"init",4))                                               //  init or initF
   {
      if (! filez && gallerytype == FOLDER) filez = galleryname;                 //  null file: refresh current gallery
      if (! filez) goto return0;

      zstrcopy(galleryname,filez,"gallery");                                     //  new gallery name

      NF = Gfiles;                                                               //  current file count
      Gfiles = Gfolders = Gimages = 0;                                           //  no new files yet

      for (ii = 0; ii < NF; ii++) {                                              //  free prior gallery index
         if (Gindex[ii].file) zfree(Gindex[ii].file);                            //  memory leak
         if (Gindex[ii].mdata1) zfree(Gindex[ii].mdata1);
         if (Gindex[ii].mdata2) zfree(Gindex[ii].mdata2);
         Gindex[ii].file = Gindex[ii].mdata1 = Gindex[ii].mdata2 = 0;
         Gindex[ii].folder = 0;
      }
   }

   if (strmatch(action,"init"))                                                  //  initz. from given file or folder
   {
      if (! dirfile(galleryname)) {                                              //  bad folder? (may be member file)
         pp = (ch *) strrchr(galleryname,'/');                                   //  try parent folder
         if (pp) *pp = 0;
         if (! dirfile(galleryname)) goto return0;                               //  give up, empty file list
      }

      cc = strlen(galleryname) - 1;                                              //  remove trailing '/'
      if (cc > 1 && galleryname[cc] == '/') galleryname[cc] = 0;                 //  but not if root folder '/'

      flags = 1 + 8;                                                             //  images + folders, one level
      if (Fshowhidden) flags += 4;                                               //  include hidden files

      err = find_imagefiles(galleryname,flags,Flist,NF);                         //  find all image files in folder
      if (err) {
         zmessageACK(Mwin,strerror(errno));
         goto return0;
      }

      for (ii = 0; ii < NF; ii++)
      {
         file = Flist[ii];

         pp = strrchr(file,'/');
         if (pp && (strmatch(pp,"/.") || strmatch(pp,"/.."))) {                  //  omit folders  /.  and  /..
            zfree(file);
            continue;
         }

         if (Gfiles == maxgallery) {                                             //  too many files
            zmessageACK(Mwin,"gallery truncated to %d images",maxgallery);
            break;
         }

         ftype = image_file_type(file);

         if (ftype == FDIR)                                                      //  subfolder
         {
            Gindex[Gfiles].file = file;                                          //  add to gallery index
            Gindex[Gfiles].folder = 1;                                           //  mark folder 
            Gindex[Gfiles].fdate[0] = 0;                                         //  no file date
            Gindex[Gfiles].pdate[0] = 0;                                         //  no photo date
            Gindex[Gfiles].psize = 0;                                            //  no image size
            Gfiles++;
            Gfolders++;
         }

         else if (ftype == IMAGE || ftype == RAW || ftype == VIDEO)              //  supported image file type
         {
            for (jj = 0; jj < Nblacklist; jj++)                                  //  omit blacklisted file
               if (MatchWild(blacklist[jj],file) == 0) break;
            if (jj < Nblacklist) {
               zfree(file);
               continue;
            }

            xxrec = get_xxrec(file);
            if (! xxrec) {
               zfree(file);
               continue;
            }

            Gindex[Gfiles].file = file;                                          //  add to gallery index
            Gindex[Gfiles].folder = 0;
            strcpy(Gindex[Gfiles].fdate,xxrec->fdate);
            strcpy(Gindex[Gfiles].pdate,xxrec->pdate);
            Gindex[Gfiles].fsize = xxrec->fsize;                                 //  file size, MB
            Gindex[Gfiles].psize = xxrec->ww * xxrec->hh;                        //  image size, pixels
            Gfiles++;
            Gimages++;
         }

         else zfree(file);                                                       //  thumbnail or other kind of file

         if (Gfiles == maxgallery) {                                             //  too many files
            zmessageACK(Mwin,"gallery truncated to %d images",maxgallery);
            break;
         }
      }

      if (Flist) zfree(Flist);

      gallerytype = FOLDER;                                                      //  gallery type = folder
      gallerysort = FNAME;                                                       //  sort Gindex by file name ascending
      galleryseq = ASCEND;
      galleryposn = 0;
      
      add_recent_folder(galleryname);                                            //  add gallery to recent list            24.60

      if (Gfiles > 1)
         HeapSort((ch *) Gindex, sizeof(Gindex_t), Gfiles, gallery_comp);

      if (Flastversion)                                                          //  keep last versions only (user setting)
      {
         ii = Gfolders;                                                          //  skip folders
         jj = ii + 1;
         kk = 0;

         while (jj < Gfiles)
         {
            pp1 = strrchr(Gindex[jj].file,'.');                                  //  /.../filename.vNN.ext
            if (pp1 && pp1[-4] == '.' && pp1[-3] == 'v') {                       //                |
               cc1 = pp1 - Gindex[jj].file - 3;
               pp2 = strrchr(Gindex[ii].file,'.');
               if (! pp2) cc2 = 0;
               else {
                  cc2 = pp2 - Gindex[ii].file + 1;                               //  /.../filename.ext
                  if (pp2[-4] == '.' && pp2[-3] == 'v') cc2 -= 4;                //                |
               }
               if (cc1 == cc2 && strmatchN(Gindex[jj].file,Gindex[ii].file,cc1))
               {
                  zfree(Gindex[ii].file);                                        //  if match to "/.../filename."
                  Gindex[ii] = Gindex[jj];                                       //    replace with later version
                  jj++;
                  kk++;
                  continue;
               }
            }
            ii++;
            Gindex[ii] = Gindex[jj];
            jj++;
         }

         Gfiles -= kk;
         Gimages -= kk;
      }

      gallerypainted = 0;
      goto return0;
   }

   if (strmatch(action,"initF"))                                                 //  gallery from given file list
   {
      if (gallerytype == TNONE || gallerytype == FOLDER)                         //  gallery type from caller:
         zappcrash("gallery() initF gallerytype %d",gallerytype);                //    SEARCH META RECENT NEWEST ALBUM

      fid = fopen(galleryname,"r");                                              //  open file
      if (! fid) goto return0;

      while (true)                                                               //  read list of files
      {
         file = fgets_trim(buff,XFCC-1,fid,1);
         if (! file) break;

         xxrec = get_xxrec(file);                                                //  get index record
         if (! xxrec) continue;

         Gindex[Gfiles].file = zstrdup(file,"gallery");                          //  add to gallery index
         Gindex[Gfiles].folder = 0;
         strcpy(Gindex[Gfiles].fdate,xxrec->fdate);                              //  file date
         strcpy(Gindex[Gfiles].pdate,xxrec->pdate);                              //  photo date
         Gindex[Gfiles].fsize = xxrec->fsize;                                    //  file size, MB
         Gindex[Gfiles].psize = xxrec->ww * xxrec->hh;                           //  image size, pixels
         Gfiles++;
         if (Gfiles == maxgallery) {                                             //  too many files
            zmessageACK(Mwin,"gallery truncated to %d images",maxgallery);
            break;
         }
      }

      fclose(fid);

      Gimages = Gfiles;                                                          //  no folders
      gallerysort = SNONE;                                                       //  no initial sort
      galleryseq = QNONE;
      galleryposn = 0;
      gallerypainted = 0;
      goto return0;
   }

   if (strmatch(action,"sort"))                                                  //  sort the gallery index
   {
      if (Nth == -2) gallery_memory("get");                                      //  recall prior sort and posn.
      if (Gfiles > 1 && gallerysort != SNONE)                                    //  sort using current or prior params
         HeapSort((ch *) Gindex, sizeof(Gindex_t), Gfiles, gallery_comp);
      gallerypainted = 0;
      goto return0;
   }

   if (strmatch(action,"paint"))                                                 //  paint gallery window
   {
      if (filez) Nth = file_position(filez,Nth);                                 //  use or get valid Nth for filez
      if (Nth >= 0) galleryposn = Nth;                                           //  filez position or caller Nth
      else gallery_memory("get");
      gtk_widget_queue_draw(Gdrawin);                                            //  draw gallery window
      gallerypainted = 0;
      goto return0;
   }

   if (strmatch(action,"insert"))                                                //  insert new file into list
   {
      fposn = Nth;                                                               //  file position from caller
      if (fposn < 0) fposn = 0;                                                  //  limit to allowed range
      if (fposn > Gfiles) fposn = Gfiles;

      if (Gfiles == maxgallery) {                                                //  no room
         zmessageACK(Mwin,"gallery truncated to %d images",maxgallery);
         goto return0;
      }

      xxrec = get_xxrec(filez);
      if (! xxrec) {
         zmessageACK(Mwin,"file not indexed: %s",filez);
         goto return0;
      }

      for (ii = Gfiles; ii > fposn; ii--)                                        //  create hole in gallery index
         Gindex[ii] = Gindex[ii-1];

      Gindex[fposn].file = zstrdup(filez,"gallery");                             //  put new file in hole
      Gindex[Gfiles].folder = 0;
      strcpy(Gindex[fposn].fdate,xxrec->fdate);
      strcpy(Gindex[fposn].pdate,xxrec->pdate);
      Gindex[fposn].fsize = xxrec->fsize;                                        //  file size, MB
      Gindex[fposn].psize = xxrec->ww * xxrec->hh;                               //  image size, pixels
      Gfiles++;
      Gimages++;
      gallerypainted = 0;
      goto return0;
   }

   if (strmatch(action,"delete"))                                                //  delete file from gallery index
   {
      fposn = Nth;                                                               //  file position from caller
      if (fposn < Gfolders || fposn > Gfiles-1) goto return0;                    //  not image file
      Gfiles--;                                                                  //  decr. total files
      Gimages--;                                                                 //  decr. image files
      zfree(Gindex[fposn].file);                                                 //  remove Gindex record
      if (Gindex[fposn].mdata1) zfree(Gindex[fposn].mdata1);                     //     and metadata
      if (Gindex[fposn].mdata2) zfree(Gindex[fposn].mdata2);
      for (ii = fposn; ii < Gfiles; ii++)                                        //  close the hole
         Gindex[ii] = Gindex[ii+1];
      gallerypainted = 0;
      goto return0;
   }

   if (strmatch(action,"get"))                                                   //  return Nth folder/file in gallery index
   {
      fposn = Nth;                                                               //  file position from caller
      if (fposn < 0 || fposn > Gfiles-1) goto return0;
      file2 = zstrdup(Gindex[fposn].file,"gallery");                             //  get Nth file
      if (regfile(file2)) goto return2;                                          //  return file2
      if (dirfile(file2)) goto return2;
      zfree(file2);
      goto return0;
   }

   if (strmatch(action,"getR"))                                                  //  return file reference (no zstrdup)
   {
      fposn = Nth;                                                               //  file position from caller
      if (fposn < 0 || fposn > Gfiles-1) goto return0;
      file2 = Gindex[fposn].file;                                                //  get Nth file
      if (regfile(file2)) goto return2;                                          //  return file2
      if (dirfile(file2)) goto return2;
      goto return0;
   }

   if (strmatch(action,"update"))                                                //  update Gindex file from image index
   {                                                                             //  (from update_image_index())
      for (ii = 0; ii < Gfiles; ii++)
         if (strmatch(filez,Gindex[ii].file)) break;
      if (ii == Gfiles) goto return0;                                            //  not found

      xxrec = get_xxrec(filez);
      if (! xxrec) goto return0;
      strcpy(Gindex[ii].fdate,xxrec->fdate);
      strcpy(Gindex[ii].pdate,xxrec->pdate);
      Gindex[ii].fsize = xxrec->fsize;                                           //  file size, MB
      Gindex[ii].psize = xxrec->ww * xxrec->hh;                                  //  image size, pixels
      gallerypainted = 0;
      goto return0;
   }

   zappcrash("navigate %s",action);                                              //  invalid action

return0:
   resource_unlock(GFlock);                                                      //  unlock gallery file list
   return 0;

return2:
   resource_unlock(GFlock);
   return file2;
}


//  private function, gallery sort compare
//  folders sort first and upper/lower case is ignored

int navi::gallery_comp(ch *rec1, ch *rec2)
{
   int         nn;
   ch          *pp1, *pp2;
   double      d1, d2;
   Gindex_t    *grec1, *grec2;

   grec1 = (Gindex_t *) rec1;
   grec2 = (Gindex_t *) rec2;

   if (grec1->folder) {                                                          //  folder sort
      if (! grec2->folder) return -1;                                            //  folder :: image file 
      nn = strcasecmp(grec1->file,grec2->file);                                  //  folder :: folder
      if (nn) return nn;
      nn = strcmp(grec1->file,grec2->file);                                      //  if equal, use utf8 compare
      return nn;
   }
   else if (grec2->folder) return +1;                                            //  image file :: folder 

   if (galleryseq == DESCEND) {                                                  //  descending, switch inputs
      grec1 = (Gindex_t *) rec2;
      grec2 = (Gindex_t *) rec1;
   }

   switch (gallerysort) {

      case FNAME: {                                                              //  file name
         nn = strcasecmp(grec1->file,grec2->file);                               //  ignore case
         if (nn) return nn;
         nn = strcmp(grec1->file,grec2->file);                                   //  if equal, use utf8 compare
         return nn;
      }

      case FNUMBER: {                                                            //  file name/number
         nn = filename_comp(grec1->file,grec2->file);
         if (nn) return nn;
         nn = strcmp(grec1->file,grec2->file);                                   //  if equal, use utf8 compare
         return nn;
      }

      case FDATE: {                                                              //  file mod date/time
         nn = strcmp(grec1->fdate,grec2->fdate);
         if (nn) return nn;
         goto tiebreak;
      }

      case PDATE: {                                                              //  photo date/time
         nn = strcmp(grec1->pdate,grec2->pdate);                                 //  (metadata DateTimeOriginal)
         if (nn) return nn;
         goto tiebreak;
      }

      case FSIZE: {                                                              //  file size
         nn = grec1->fsize - grec2->fsize;
         if (nn) return nn;
         goto tiebreak;
      }

      case PSIZE: {                                                              //  image pixel size
         nn = grec1->psize - grec2->psize;
         if (nn) return nn;
         goto tiebreak;
      }

      case MDATA:                                                                //  user selected metadata
      {
         pp1 = grec1->mdata2;                                                    //  (from search function, metadata report)
         pp2 = grec2->mdata2;

         while (true)                                                            //  loop name1: data1 \n name2: data2 ...
         {
            if (! pp1 && ! pp2) goto tiebreak;                                   //  no data
            if (pp1 && ! pp2) return 1;                                          //  something > nothing
            if (pp2 && ! pp1) return -1;

            pp1 = strchr(pp1,':');                                               //  look for metaname: metadata
            pp2 = strchr(pp2,':');
            if (! pp1 && ! pp2) goto tiebreak;                                   //  no data
            if (pp1 && ! pp2) return 1;                                          //  something > nothing
            if (pp2 && ! pp1) return -1;

            d1 = atof(pp1+1);                                                    //  try numeric comparison
            d2 = atof(pp2+1);
            if (d1 > 0 && d2 > 0) {                                              //  success
               if (d1 < d2) return -1;
               if (d1 > d2) return +1;
               goto nextdata;                                                    //  equal
            }

            nn = zstrcmp(pp1,pp2);                                               //  strcmp() but \n terminates like null
            if (nn) return nn;                                                   //  not equal

         nextdata:                                                               //  equal
            pp1 = strchr(pp1,'\n');
            pp2 = strchr(pp2,'\n');                                              //  loop next name: data pair
         }
      }

      default: return 0;

   tiebreak:                                                                     //  tie breaker
      nn = strcasecmp(grec1->file,grec2->file);                                  //  file name without case
      if (nn) return nn;
      nn = strcmp(grec1->file,grec2->file);                                      //  if equal, use utf8 compare
      return nn;
   }
}


/****

   compare function for sorting a list of file names with embedded numbers
      + folder names are compared normally using strcmp()
      + file base names are compared ignoring string case: 'a' = 'A'
      + file base names with embedded numbers starting in the same position
        are compared arithmetically: '5' < '40' < '300' < '1000'

****/

int navi::filename_comp(ch *file1, ch *file2)
{
   ch       *pp1, *pp2;
   ch       ch1, ch2;
   int      nn;
   int64    nn1, nn2;

   pp1 = strrchr(file1,'/');                                                     //  get end of folders
   pp2 = strrchr(file2,'/');

   if (pp1 && pp2)
   {                                                                             //  compare folders
      *pp1 = *pp2 = 0;
      nn = strcmp(file1,file2);
      *pp1 = *pp2 = '/';
      if (nn != 0) return nn;                                                    //  different, done
   }

   else
   {                                                                             //  no folders
      pp1 = file1 - 1;                                                           //  point to base file names -1
      pp2 = file2 - 1;
   }

   while (true)                                                                  //  loop chars. in file names
   {
      pp1++; pp2++;                                                              //  next chars. to compare

      if (*pp1 == *pp2)
      {                                                                          //  chars. are equal
         if (*pp1 == 0) return 0;                                                //  both '0', strings are equal, done
         if (*pp1 < '0' || *pp1 > '9') continue;                                 //  if either is not a number 0-9,
         if (*pp2 < '0' || *pp2 > '9') continue;                                 //    continue to compare next chars.
         goto number;                                                            //  both are numbers 0-9
      }

      if (*pp1 == 0) return -1;                                                  //  chars. are not equal
      if (*pp2 == 0) return +1;                                                  //  if either is '0', done

      if (*pp1 < '0' || *pp1 > '9' || *pp2 < '0' || *pp2 > '9')
      {                                                                          //  either is not a number 0-9
         ch1 = *pp1;
         ch2 = *pp2;
         if (ch1 >= 'A' && ch1 <= 'Z') ch1 = ch1 | 0x20;                         //  make A-Z into a-z to ignore case
         if (ch2 >= 'A' && ch2 <= 'Z') ch2 = ch2 | 0x20;
         if (ch1 > ch2) return +1;                                               //  compare chars.
         if (ch2 > ch1) return -1;                                               //  if not equal, done
         continue;                                                               //  equal, continue to next chars.
      }

   number:

      nn1 = atol(pp1);                                                           //  compare two numbers
      nn2 = atol(pp2);                                                           //  note: '123' and '0123' compare equal
      if (nn1 > nn2) return +1;
      if (nn2 > nn1) return -1;

      while (pp1[1] >= '0' && pp1[1] <= '9') pp1++;                              //  leave pp1/pp2 at last digit 0-9
      while (pp2[1] >= '0' && pp2[1] <= '9') pp2++;

      continue;
   }
}


/********************************************************************************/

//  private function for gallery 'thumb view'
//  paint gallery window - draw thumbnail images and limited metadata

int navi::gallery_paint(GtkWidget *drwin, cairo_t *cr)
{
   GdkRGBA     rgba;
   PIXBUF      *pixbuf;
   FTYPE       ftype;
   GtkWidget   *Ftext;
   xxrec_t     *xxrec;
   double      x1, y1, x2, y2;
   int         ii, Fmark;
   int         nrows, row, col;
   int         textlines;
   int         row1, row2, ww, hh, cc, fsize;
   int         drwingW, drwingH;
   int         thumx, thumy;
   int         ndir, nfil, convdate;
   ch          *pp, *fspec, *fname;
   ch          *ppd, ffolder[60];
   ch          pdt[24], text[200];
   static int  prows = 0;

   gallerypainted = 0;

   if (! galleryname) return 1;                                                  //  no gallery

   set_gwin_title();                                                             //  main window title = gallery name
   gallery_navibutts();                                                          //  set navigation buttons on top panel

   rgba.red = 0.00392 * GBrgb[0];                                                //  window background color
   rgba.green = 0.00392 * GBrgb[1];                                              //  0 - 255  -->  0.0 - 1.0
   rgba.blue  = 0.00392 * GBrgb[2];
   rgba.alpha = 1.0;
   gdk_cairo_set_source_rgba(cr,&rgba);
   cairo_paint(cr);

   if (gallerytype == META) {                                                    //  search, metadata report
      gallery_paintmeta(drwin,cr);
      return 1;
   }

   if (Fthumbview == 2) {                                                        //  thumbnails with basic metadata
      gallery_paintmeta2(drwin,cr);
      return 1;
   }

   for (ii = 0; ii < thumbxx; ii++)                                              //  largest ... smallest thumb size
      if (thumbsize >= thumbx[ii]) break;
   if (ii == thumbxx) ii = thumbxx-1;                                            //  (fix poss. bad parameter)
   thumbsize = thumbx[ii];                                                       //  thumbnail size
   fontsize = appfontsize + fontx[ii];                                           //  corresp. font size

   if (! Findexvalid) textlines = 1;                                             //  file name only
   else textlines = 2;                                                           //  file name + date + wwhh
   if (gallerytype != FOLDER) textlines++;                                       //  add folder name above file name
   texthh = textlines * 1.6 * fontsize + 4;                                      //  vertical space required

   thumbW = thumbsize + 10;                                                      //  thumbnail cell size
   thumbH = thumbsize + texthh + thumbsize/24 + 10;

   xwinW = gtk_widget_get_allocated_width(Gscroll);                              //  drawing window size
   xwinH = gtk_widget_get_allocated_height(Gscroll);

   xrows = int(0.1 + 1.0 * xwinH / thumbH);                                      //  get thumbnail rows and cols that
   xcols = int(0.1 + 1.0 * xwinW / thumbW);                                      //    (almost) fit in window
   if (xrows < 1) xrows = 1;
   if (xcols < 1) xcols = 1;
   nrows = (Gfiles+xcols-1) / xcols;                                             //  total thumbnail rows, 1 or more
   if (nrows < 1) nrows = 1;

   drwingW = xcols * thumbW + margin + 10;                                       //  layout size for entire gallery
   drwingH = (nrows + 1) * thumbH;                                               //  (+ empty row for visual end)
   if (drwingH <= xwinH) drwingH = xwinH + 1;                                    //  at least window size + 1

   if (drwingH < 7000000) prows = 0;                                             //  stay within undocumented limit
   else {
      nrows = 7000000 / thumbH;
      drwingH = nrows * thumbH;
      if (nrows * xcols != prows) {
         zmessage_post(Mwin,"20/20",3,"gallery truncated to %d images",nrows*xcols);
         prows = nrows * xcols;
      }
   }

   gtk_widget_get_size_request(drwin,&ww,&hh);                                   //  current size
   if (ww != drwingW || hh != drwingH)
      gtk_widget_set_size_request(drwin,-1,drwingH);                             //  needs to change

   maxscroll = nrows * thumbH;                                                   //  too far but necessary
   if (maxscroll < xwinH) maxscroll = xwinH;                                     //  compensate GTK bug
   gtk_adjustment_set_upper(Gadjust,maxscroll);

   gtk_adjustment_set_step_increment(Gadjust,thumbH);                            //  scrollbar works in row steps
   gtk_adjustment_set_page_increment(Gadjust,thumbH * xrows);                    //  and in page steps

   if (galleryposn >= 0) {                                                       //  new target file position (Nth)
      scrollposn = galleryposn / xcols * thumbH;                                 //  scroll position for target file
      if (scrollposn > maxscroll) scrollposn = maxscroll;                        //    >> top row of window
      gtk_adjustment_set_value(Gadjust,scrollposn);
   }

   scrollposn = gtk_adjustment_get_value(Gadjust);
   galleryposn = (scrollposn + thumbH/2) / thumbH * xcols;                       //  remember gallery position
   gallery_memory("put");
   galleryposn = -1;                                                             //  disable

   cairo_clip_extents(cr,&x1,&y1,&x2,&y2);                                       //  window region to paint
   row1 = y1 / thumbH;
   row2 = y2 / thumbH;

   for (row = row1; row <= row2; row++)                                          //  draw visible rows
   for (col = 0; col < xcols; col++)                                             //  draw all columns in row
   {
      ii = row * xcols + col;                                                    //  next file
      if (ii >= Gfiles) goto endloops;                                           //  exit 2 nested loops

      fspec = Gindex[ii].file;                                                   //  folder/file name
      pp = strrchr(fspec,'/');                                                   //  get file name only
      if (pp) fname = pp + 1;
      else fname = fspec;

      strcpy(ffolder,"/");
      if (gallerytype != FOLDER) {                                               //  files from mixed folders
         if (pp && pp > fspec) {
            for (ppd = pp-1; ppd >= fspec && *ppd != '/'; ppd--);                //  get last folder level
            cc = pp - ppd + 1;                                                   //  (include null to be added)
            if (cc > 60) cc = 60;
            strncpy0(ffolder,ppd,cc);
         }
      }

      thumx = col * thumbW + margin;                                             //  drawing area position
      thumy = row * thumbH + margin;

      Fmark = 0;

      if (zd_select_files) {                                                     //  gallery select is active
         Ftext = zdialog_gtkwidget(zd_select_files,"files");
         if (textwidget_find(Ftext,fspec,0,0) >= 0) Fmark = 1;                   //  mark selected file
      }

      else if (curr_file && strmatch(fspec,curr_file)) Fmark = 1;                //  if file is current file, mark it

      if (Fmark) {
         cairo_set_source_rgb(cr,1,1,0.5);                                       //  mark file with yellow background
         cairo_rectangle(cr,thumx-3,thumy-3,thumbW-3,texthh);
         cairo_fill(cr);
      }

      ftype = image_file_type(fspec);                                            //  folder/image/RAW file

      if (ftype == FDIR) {                                                       //  folder
         dir_filecount(fspec,ndir,nfil);                                         //  get subdir and file counts
         snprintf(text,200,"%s\n%d + %d images",fname,ndir,nfil);                //  dir name, subdirs + image files
      }

      else                                                                       //  image/RAW/VIDEO file
      {
         xxrec = get_xxrec(fspec);                                               //  get index record
         if (! xxrec) continue;

         convdate = 1;
         if (gallerysort == FDATE) strncpy0(pdt,xxrec->fdate,15);                //  use file or photo date
         else {                                                                  //    based on gallery sort
            convdate = 0;
            strncpy0(pdt,xxrec->pdate,15);                                       //  photo date
            if (xxrec->ww == 0) strcpy(pdt,"not indexed");
            else if (strmatch(pdt,"null")) strcpy(pdt,"undated");
            else convdate = 1;
         }
         if (convdate) {
            memmove(pdt+17,pdt+12,2);                                            //  convert yyyymmddhhmmss
            memmove(pdt+14,pdt+10,2);                                            //    to yyyy-mm-dd hh:mm:ss
            memmove(pdt+11,pdt+8,2);
            memmove(pdt+8,pdt+6,2);
            memmove(pdt+5,pdt+4,2);
            pdt[19] = 0;
            pdt[16] = pdt[13] = ':';
            pdt[10] =' ';
            pdt[4] = pdt[7] = '-';
            if (thumbsize <= 256) pdt[10] = 0;                                   //  truncate date/time to date only
         }

         ww = xxrec->ww;                                                         //  width, height, file size
         hh = xxrec->hh;
         fsize = xxrec->fsize;

         cc = 0;

         if (gallerytype != FOLDER) {                                            //  mixed folders (search results)
            snprintf(text,200,"%s\n",ffolder);                                   //  output folder name + \n
            cc = strlen(text);
         }

         snprintf(text+cc,200-cc,"%s\n",fname);                                  //  output file name + \n
         cc = cc + strlen(fname) + 1;

         if (Findexvalid) {                                                      //  valid index, metadata available
            snprintf(text+cc,200-cc,"%s",pdt);                                   //  output date [ time ]
            cc += strlen(pdt);

            if (ww == 0)                                                         //  file not indexed
               snprintf(text+cc,200-cc,"  %.2fmb",fsize/MEGA);                   //  output file size only
            else {
               if (thumbsize > 180)
                  snprintf(text+cc,200-cc,"  %dx%d  %.2fmb",ww,hh,fsize/MEGA);   //  output width, height, file size
               else if (thumbsize > 128)
                  snprintf(text+cc,200-cc,"  %dx%d",ww,hh);                      //  output only width, height
            }
         }

         draw_text(cr,text,thumx,thumy,thumbW-5);                                //  paint text first
         thumy += texthh;                                                        //  position thumbnail below text
      }

      pixbuf = get_thumb_pixbuf(fspec);                                          //  get thumbnail                         24.10
      if (pixbuf) {
         ww = gdk_pixbuf_get_width(pixbuf);
         ww = (thumbsize - ww) / 4;                                              //  shift margin if smaller width
         gdk_cairo_set_source_pixbuf(cr,pixbuf,thumx+ww,thumy);
         cairo_paint(cr);                                                        //  paint
      }

      if (ftype == FDIR) {                                                       //  folder
         thumy += thumbsize/3 + 10;                                              //  overlay thumbnail with text
         draw_text(cr,text,thumx+ww+thumbW/6,thumy,thumbW-5);
      }
   }

endloops:

   gallerypainted = 1;                                                           //  mark gallery display complete
   preload_start = ii;                                                           //  start file for preload_thread()
   preload_trigger = 1;                                                          //  trigger preload_thread()
   return 1;
}


//  private function for search images 'metadata report'
//  paint metadata report - draw thumbnail images + metadata text

int navi::gallery_paintmeta(GtkWidget *drwin, cairo_t *cr)
{
   PIXBUF      *pixbuf;
   double      x1, y1, x2, y2;
   int         ii, nrows, row;
   int         row1, row2, ww, hh;
   int         cc1, cc2;
   int         drwingW, drwingH;
   int         thumx, thumy, textww;
   ch          *fspec, *mtext;
   static int  prows = 0;

   thumbsize = 360;                                                              //  24.70

   for (ii = 0; ii < thumbxx; ii++)
      if (thumbsize == thumbx[ii]) break;                                        //  tolerate bad parameter value
   if (ii >= thumbxx) thumbsize = thumbx[2];                                     //    inherited from prior release

   fontsize = appfontsize;

   thumbW = thumbsize + 10;                                                      //  thumbnail layout size
   thumbH = thumbsize + 20;

   texthh = Gmdrows * fontsize * 1.8 + 20;                                       //  space for metadata text
   if (texthh > thumbH) thumbH = texthh;

   xwinW = gtk_widget_get_allocated_width(Gscroll);                              //  drawing window size
   xwinH = gtk_widget_get_allocated_height(Gscroll);

   xrows = int(0.1 + 1.0 * xwinH / thumbH);                                      //  get thumbnail rows fitting in window
   if (xrows < 1) xrows = 1;
   xcols = 1;                                                                    //  force cols = 1
   nrows = Gfiles;                                                               //  thumbnail rows
   if (! nrows) nrows = 1;

   drwingW = xwinW;                                                              //  layout size for entire gallery
   drwingH = (nrows + 1) * thumbH;                                               //  last row
   if (drwingH < xwinH) drwingH = xwinH;

   if (drwingH < 7000000) prows = 0;                                             //  stay within undocumented limit
   else {
      nrows = 7000000 / thumbH;
      drwingH = nrows * thumbH;
      if (nrows != prows) {
         zmessage_post(Mwin,"20/20",3,"gallery truncated to %d images",nrows);
         prows = nrows;
      }
   }

   gtk_widget_get_size_request(drwin,&ww,&hh);                                   //  current size
   if (ww != drwingW || hh != drwingH)
      gtk_widget_set_size_request(drwin,-1,drwingH);                             //  needs to change

   maxscroll = nrows * thumbH;                                                   //  too far but necessary
   if (maxscroll < xwinH) maxscroll = xwinH;                                     //  compensate GTK bug
   gtk_adjustment_set_upper(Gadjust,maxscroll);

   gtk_adjustment_set_step_increment(Gadjust,thumbH);                            //  scrollbar works in row steps
   gtk_adjustment_set_page_increment(Gadjust,thumbH * xrows);                    //  and in page steps

   if (galleryposn >= 0) {                                                       //  new target file position (Nth)
      scrollposn = galleryposn / xcols * thumbH;                                 //  scroll position for target file
      if (scrollposn > maxscroll) scrollposn = maxscroll;                        //    >> top row of window
      gtk_adjustment_set_value(Gadjust,scrollposn);
   }

   scrollposn = gtk_adjustment_get_value(Gadjust);                               //  save gallery sort and position
   galleryposn = (scrollposn + thumbH/2) / thumbH * xcols;
   gallery_memory("put");
   galleryposn = -1;                                                             //  disable

   cairo_clip_extents(cr,&x1,&y1,&x2,&y2);                                       //  window region to paint
   row1 = y1 / thumbH;
   row2 = y2 / thumbH;

   textww = drwingW - thumbW - 2 * margin;                                       //  space for text right of thumbnail

   for (row = row1; row <= row2; row++)                                          //  draw file thumbnails + text
   {
      ii = row;                                                                  //  next file
      if (ii >= Gfiles) break;

      fspec = Gindex[ii].file;                                                   //  filename

      thumx = margin;                                                            //  drawing area position
      thumy = row * thumbH + margin;

      pixbuf = get_thumb_pixbuf(fspec);                                          //  get thumbnail                         24.10
      if (pixbuf) {
         gdk_cairo_set_source_pixbuf(cr,pixbuf,thumx,thumy);
         cairo_paint(cr);
      }

      draw_text(cr,fspec,thumbW+margin,thumy,textww);                            //  write filespec to right of thumbnail

      cc1 = cc2 = 0;
      if (Gindex[ii].mdata1) cc1 = strlen(Gindex[ii].mdata1);                    //  standard metadata text
      if (Gindex[ii].mdata2) cc2 = strlen(Gindex[ii].mdata2);                    //  user selected metadata text
      if (cc1 + cc2 == 0) continue;                                              //  no text

      if (cc1 && Gindex[ii].mdata1[cc1-1] == '\n') {                             //  delete sporadic trailing \n
         Gindex[ii].mdata1[cc1-1] = 0;
         cc1--;
      }

      if (cc2 && Gindex[ii].mdata2[cc2-1] == '\n') {                             //  delete sporadic trailing \n
         Gindex[ii].mdata2[cc2-1] = 0;
         cc2--;
      }

      mtext = (ch *) zmalloc(cc1 + cc2 + 4,"paintmeta");                         //  space for both

      if (cc1) strcpy(mtext,Gindex[ii].mdata1);                                  //  standard text

      if (cc2) {
         strcpy(mtext + cc1,"\n\n");
         cc1 += 2;
         strcpy(mtext+cc1,Gindex[ii].mdata2);
      }

      draw_text(cr,mtext,thumbW+margin,thumy+20,textww);                         //  draw text to window
      zfree(mtext);
   }

   gallerypainted = 1;
   return 1;
}


//  private function for 'meta view'
//  paint gallery window: thumbnail, file name, basic metadata,
//  extra indexed metadata (user defined in image index function).

int navi::gallery_paintmeta2(GtkWidget *drwin, cairo_t *cr)
{
   PIXBUF      *pixbuf;
   FTYPE       ftype;
   double      x1, y1, x2, y2;
   int         ii, nn, cc, nrows, row, col;
   int         row1, row2, ww, hh;
   int         drwingW, drwingH;
   int         thumx, thumy, textx, textww;
   int         ndir, nfil;
   ch          *fspec, *fspec2;
   xxrec_t     *xxrec;
   ch          fdate[20], pdate[20], *pp;
   ch          *rating, *tags, *title, *desc, *location, *country;
   ch          wwhh[16], fsize[16];
   ch          text[1000], xtext1[xmetaXcc+xmetamaxkeys], **xtext2;
   static int  prows = 0;

   thumbsize = 360;                                                              //  thumbnail size                        24.70
   fontsize = appfontsize;

   textww = fontsize * 60;                                                       //  text width limit for max. 60 chars.
   if (gallerytype != FOLDER) textww = fontsize * 200;                           //  more if full path is written

   thumbW = thumbsize + 10 + textww;                                             //  thumbnail + text layout size
   thumbH = thumbsize + 20;

   texthh = 15 * fontsize * 1.8 + 20;                                            //  space for 15 lines of metadata text
   if (texthh > thumbH) thumbH = texthh;

   xwinW = gtk_widget_get_allocated_width(Gscroll);                              //  drawing window size
   xwinH = gtk_widget_get_allocated_height(Gscroll);

   xrows = int(0.1 + 1.0 * xwinH / thumbH);                                      //  thumbnail rows that fit in window
   if (xrows < 1) xrows = 1;
   xcols = 1;                                                                    //  1 thumbnail column
   nrows = (Gfiles+xcols-1) / xcols;                                             //  thumbnail rows, 1 or more
   if (nrows < 1) nrows = 1;

   drwingW = xcols * thumbW + margin + 10;                                       //  layout size for entire gallery
   drwingH = (nrows + 1) * thumbH;                                               //  (+ empty row for visual end)
   if (drwingH <= xwinH) drwingH = xwinH + 1;                                    //  at least window size + 1

   if (drwingH < 7000000) prows = 0;                                             //  stay within undocumented limit
   else {
      nrows = 7000000 / thumbH;
      drwingH = nrows * thumbH;
      if (nrows != prows) {
         zmessage_post(Mwin,"20/20",3,"gallery truncated to %d images",nrows);
         prows = nrows;
      }
   }

   gtk_widget_get_size_request(drwin,&ww,&hh);                                   //  current size
   if (ww != drwingW || hh != drwingH)
      gtk_widget_set_size_request(drwin,-1,drwingH);                             //  needs to change

   maxscroll = nrows * thumbH;                                                   //  too far but necessary
   if (maxscroll < xwinH) maxscroll = xwinH;                                     //  compensate GTK bug
   gtk_adjustment_set_upper(Gadjust,maxscroll);

   gtk_adjustment_set_step_increment(Gadjust,thumbH);                            //  scrollbar works in row steps
   gtk_adjustment_set_page_increment(Gadjust,thumbH * xrows);                    //  and in page steps

   if (galleryposn >= 0) {                                                       //  new target file position (Nth)
      scrollposn = galleryposn / xcols * thumbH;                                 //  scroll position for target file
      if (scrollposn > maxscroll) scrollposn = maxscroll;                        //    >> top row of window
      gtk_adjustment_set_value(Gadjust,scrollposn);
   }

   scrollposn = gtk_adjustment_get_value(Gadjust);                               //  save gallery sort and position
   galleryposn = (scrollposn + thumbH/2) / thumbH * xcols;
   gallery_memory("put");
   galleryposn = -1;                                                             //  disable

   cairo_clip_extents(cr,&x1,&y1,&x2,&y2);                                       //  window region to paint
   row1 = y1 / thumbH;
   row2 = y2 / thumbH;

   for (row = row1; row <= row2; row++)                                          //  draw file thumbnails
   for (col = 0; col < xcols; col++)                                             //  draw all columns in row
   {
      ii = row * xcols + col;                                                    //  next file
      if (ii >= Gfiles) goto endloops;                                           //  exit 2 nested loops

      fspec = Gindex[ii].file;
      fspec2 = fspec;
      if (gallerytype == FOLDER) {                                               //  folder gallery, write only
         pp = strrchr(fspec,'/');                                                //    base file names
         if (pp) fspec2 = pp+1;
      }

      thumx = col * thumbW + margin;                                             //  drawing area position
      thumy = row * thumbH + margin;

      pixbuf = get_thumb_pixbuf(fspec);                                          //  get thumbnail                         24.10
      if (pixbuf) {
         gdk_cairo_set_source_pixbuf(cr,pixbuf,thumx,thumy);                     //  paint thumbnail
         cairo_paint(cr);
      }

      textx = thumx + margin + thumbsize;                                        //  text position

      if (curr_file && strmatch(fspec,curr_file)) {                              //  yellow background for curr. file name
         ww = fontsize * strlen(fspec2);
         if (ww > textww) ww = textww;
         cairo_set_source_rgb(cr,1,1,0.5);
         cairo_rectangle(cr,textx,thumy,ww,fontsize*1.6);
         cairo_fill(cr);
      }

      ftype = image_file_type(fspec);                                            //  file type

      if (ftype == FDIR) {                                                       //  subfolder
         dir_filecount(fspec,ndir,nfil);                                         //  get subfolder and file counts
         snprintf(text,1000,"\n %s \n %d folders + %d images \n",                //  paint 2 lines:
                                         fspec2, ndir, nfil);                    //  folder name, file counts
         draw_text(cr,text,textx,thumy,textww);
      }

      else if (Findexvalid)                                                      //  if image index is valid
      {
         xxrec = get_xxrec(fspec);                                               //  get indexed metadata for file
         if (! xxrec) continue;

         strncpy0(fdate,xxrec->fdate,16);                                        //  construct metadata text report
         strncpy0(pdate,xxrec->pdate,16);
         rating = xxrec->rating;
         snprintf(wwhh,16,"%dx%d",xxrec->ww,xxrec->hh);
         snprintf(fsize,16,"%.2fmb",xxrec->fsize/MEGA);
         tags = xxrec->tags;
         title = xxrec->title;
         desc = xxrec->desc;
         location = xxrec->location;
         country = xxrec->country;

         pp = fdate;                                                             //  file date
         memmove(pp+17,pp+12,2);                                                 //  :ss added
         memmove(pp+14,pp+10,2);                                                 //  yyyymmddhhmmss to yyyy-mm-dd hh:mm:ss
         memmove(pp+11,pp+8,2);
         memmove(pp+8,pp+6,2);
         memmove(pp+5,pp+4,2);
         pp[19] = 0;
         pp[16] = pp[13] = ':';
         pp[10] =' ';
         pp[4] = pp[7] = '-';

         pp = pdate;                                                             //  photo date
         if (xxrec->ww == 0) strcpy(pp,"not indexed");
         else if (strmatch(pp,"null")) strcpy(pp,"undated");
         else {
            memmove(pp+17,pp+12,2);                                              //  :ss added
            memmove(pp+14,pp+10,2);                                              //  yyyymmddhhmmss to yyyy-mm-dd hh:mm:ss
            memmove(pp+11,pp+8,2);
            memmove(pp+8,pp+6,2);
            memmove(pp+5,pp+4,2);
            pp[19] = 0;
            pp[16] = pp[13] = ':';
            pp[10] =' ';
            pp[4] = pp[7] = '-';
         }

         if (! strmatchN(xxrec->xmeta,"null",4))                                 //  extra indexed metadata present
         {
            nn = breakup_text(xxrec->xmeta,xtext2,"^",10,60);
            for (ii = cc = 0; ii < nn && ii < 9; ii++) {
               repl_1str(xtext2[ii],xtext1+cc,99,"^"," ");
               cc += strlen(xtext2[ii]);
               strcpy(xtext1+cc,"\n ");
               cc += 2;
            }
            xtext1[cc] = 0;

            for (ii = 0; ii < nn; ii++)
               zfree(xtext2[ii]);
            zfree(xtext2);

            snprintf(text,1000,                                                  //  report basic metadata in 6 lines
               " %s \n photo date: %s  file date: %s \n"
               " rating: %s  size: %s %s \n"
               " location: %s %s \n"                                             //  24.70
               " tags: %s \n title: %s \n description: %s \n"
               " ----------------------- \n %s",                                 //  extra indexed metadata in <10 lines
               fspec2, pdate, fdate, rating, wwhh, fsize,
                       location, country, tags, title, desc, xtext1);
         }

         else
         {
            snprintf(text,1000,                                                  //  report basic metadata in 6 lines
               " %s \n photo date: %s  file date: %s \n"
               " rating: %s  size: %s %s \n"
               " location: %s %s \n"                                             //  24.70
               " tags: %s \n title: %s \n description: %s ",
               fspec2, pdate, fdate, rating, wwhh, fsize,
                       location, country, tags, title, desc);
         }
      }

      else snprintf(text,1000,"\n %s",fspec2);                                   //  no index, no metadata, file name only

      draw_text(cr,text,textx,thumy,textww);                                     //  paint text
   }
   endloops:

   gallerypainted = 1;
   return 1;
}


/********************************************************************************/

//  private function
//  find the number of subdirs and image files within a given folder

void navi::dir_filecount(ch *dirname, int &ndir, int &nfil)
{
   ch       *file, **flist;
   int      ii, cc, err, NF;
   int      dcount = 0, fcount = 0;
   FTYPE    ftype;

   #define NC 1000                                                               //  cache capacity

   static int     ftf = 1;
   static ch      *dirnamecache[NC];                                             //  cache for recent folder data
   static time_t  modtimecache[NC];
   static int     dcountcache[NC];
   static int     fcountcache[NC];
   static int     pcache = 0;
   STATB          statB;

   if (ftf) {
      ftf = 0;
      cc = NC * sizeof(ch *);                                                    //  first call, clear cache
      memset(dirnamecache,0,cc);
   }

   ndir = nfil = 0;

   if (! dirfile(dirname,&statB)) return;

   for (ii = 0; ii < NC; ii++) {                                                 //  look for folder in cache
      if (! dirnamecache[ii]) break;
      if (strmatch(dirname,dirnamecache[ii]) &&
          statB.st_mtime == modtimecache[ii]) {
         ndir = dcountcache[ii];                                                 //  found, not stale
         nfil = fcountcache[ii];
         return;
      }
   }

   err = find_imagefiles(dirname,1+8,flist,NF);                                  //  find image files + folders
   if (err) {
      zmessageACK(Mwin,strerror(errno));
      return;
   }

   for (ii = 0; ii < NF; ii++)
   {
      file = flist[ii];
      ftype = image_file_type(file);
      zfree(file);
      if (ftype == FDIR) dcount++;                                               //  folder count
      else if (ftype == IMAGE || ftype == RAW || ftype == VIDEO) fcount++;       //  image file count
   }

   if (NF) zfree(flist);

   ii = pcache++;                                                                //  add to cache, replace oldest
   if (pcache == NC) pcache = 0;
   if (dirnamecache[ii]) zfree(dirnamecache[ii]);
   dirnamecache[ii] = zstrdup(dirname,"filecount");
   modtimecache[ii] = statB.st_mtime;
   ndir = dcountcache[ii] = dcount;
   nfil = fcountcache[ii] = fcount;
   return;
}


/********************************************************************************/

//  private function
//  write text block at px/py location with width limit ww

void navi::draw_text(cairo_t *cr, ch *text, int px, int py, int ww)
{
   static PangoFontDescription   *pfont = 0;
   static PangoLayout            *playout = 0;

   static int     pfontsize = -1;
   static ch      thumbfont[20] = "";

   if (fontsize != pfontsize) {                                                  //  adjust for curr. font size
      pfontsize = fontsize;
      snprintf(thumbfont,20,"sans bold %d",fontsize);
      if (pfont) pango_font_description_free(pfont);
      pfont = pango_font_description_from_string(thumbfont);
      if (playout) g_object_unref(playout);
      playout = pango_cairo_create_layout(cr);
      pango_layout_set_font_description(playout,pfont);
   }

   pango_layout_set_width(playout,ww*PANGO_SCALE);                               //  limit width to avail. space
   pango_layout_set_ellipsize(playout,PANGO_ELLIPSIZE_END);
   pango_layout_set_text(playout,text,-1);

   cairo_move_to(cr,px,py);
   cairo_set_source_rgb(cr,0,0,0);                                               //  text = black
   pango_cairo_show_layout(cr,playout);
   return;
}


/********************************************************************************/

//  private function
//  create a row of navigation buttons in gallery top panel

void navi::gallery_navibutts()
{
   ch          labtext[100];
   int         ii, cc, max = maxgallerylevs;
   ch          *pp1, *pp2;

   for (ii = 0; ii < max; ii++) {                                                //  clear old navi buttons if any
      if (gallerypath[ii]) {
         zfree(gallerypath[ii]);
         gallerypath[ii] = 0;
         gtk_widget_destroy(gallerybutt[ii]);
      }
   }

   if (gallerylabel) gtk_widget_destroy(gallerylabel);                           //  clear gallery label if any
   gallerylabel = 0;

   sprintf(labtext,"no gallery");
   if (gallerytype == SEARCH) sprintf(labtext,"search results");                 //  search results
   if (gallerytype == META) sprintf(labtext,"search results");                   //  search results (metadata report)
   if (gallerytype == RECENT) sprintf(labtext,"recent images");                  //  recent images
   if (gallerytype == NEWEST) sprintf(labtext,"newest images");                  //  newest images

   if (gallerytype == ALBUM) {                                                   //  album gallery
      pp1 = strrchr(galleryname,'/');
      if (pp1) pp1++;
      else pp1 = galleryname;
      snprintf(labtext,100,"album: %s",pp1);                                     //  album: album-name
   }

   if (gallerytype != FOLDER) {                                                  //  not a folder gallery
      gallerylabel = gtk_label_new(labtext);                                     //  show gallery label
      gtk_box_pack_start(GTK_BOX(Gpanel),gallerylabel,0,0,0);
      gtk_widget_show_all(Gpanel);
      return;
   }

   ii = 0;
   pp1 = galleryname;

   while (pp1 && *pp1)                                                           //  construct new buttons
   {
      pp2 = strchr(pp1+1,'/');                                                   //  /aaaaaa/bbbbbb/cccccc
      if (pp2) cc = pp2 - pp1;                                                   //         |      |
      else cc = strlen(pp1);                                                     //         pp1    pp2
      gallerypath[ii] = (ch *) zmalloc(cc,"navibutts");
      strncpy0(gallerypath[ii],pp1+1,cc);                                        //  bbbbbb
      gallerybutt[ii] = gtk_button_new_with_label(gallerypath[ii]);
      gtk_box_pack_start(GTK_BOX(Gpanel),gallerybutt[ii],0,0,3);
      G_SIGNAL(gallerybutt[ii],"clicked",navibutt_clicked,&Nval[ii]);
      pp1 = pp1 + cc;                                                            //  next folder level /cccccc
      if (! *pp1) break;                                                         //  null = end
      if (*pp1 == '/' && ! *(pp1+1)) break;                                      //  / + null = end
      if (++ii == max) break;                                                    //  limit of folder levels
   }

   gtk_widget_show_all(Gpanel);
   return;
}


/********************************************************************************/

//  private function - menu function for gallery window
//    - scroll window as requested
//    - jump to new file or folder as requested

void navi::menufuncx(GtkWidget *, ch *menu)
{
   int         ii, scroll1, scroll2;

   viewmode('G');

   scrollposn = gtk_adjustment_get_value(Gadjust);                               //  current scroll position

   if (strmatch(menu,"Zoom+"))                                                   //  next bigger thumbnail size
   {
      F1_help_topic = "zoom";
      if (Fthumbview != 1) return;                                               //  not resizable thumb view
      for (ii = 0; ii < thumbxx; ii++)                                           //  set next greater thumbnail size
         if (thumbsize == thumbx[ii]) break;
      if (ii == 0) return;                                                       //  already maximum, no change
      thumbsize = thumbx[ii-1];
      galleryposn = scrollposn / thumbH * xcols;                                 //  keep top row position
      gallery(0,"paint",-1);                                                     //  paint gallery
      return;
   }

   if (strmatch(menu,"Zoom-"))                                                   //  next smaller thumb size
   {
      F1_help_topic = "zoom";
      if (Fthumbview != 1) return;                                               //  not resizable thumb view
      for (ii = 0; ii < thumbxx; ii++)                                           //  set next smaller thumb size
         if (thumbsize == thumbx[ii]) break;
      if (ii >= thumbxx-1) return;                                               //  already minimum, no change
      thumbsize = thumbx[ii+1];                                                  //  next smaller
      galleryposn = scrollposn / thumbH * xcols;                                 //  keep top row position
      gallery(0,"paint",-1);                                                     //  paint gallery
      return;
   }

   if (strmatch(menu,"Row Up")) {                                                //  scroll 1 row up
      scroll1 = scrollposn / thumbH * thumbH;
      scroll2 = scroll1 - thumbH;
      if (scroll2 < 0) scroll2 = 0;
      gallery_scroll(scroll2,2000);                                              //  24.70
      return;
   }

   if (strmatch(menu,"Row Down")) {                                              //  scroll 1 row down
      scroll1 = scrollposn / thumbH * thumbH;
      scroll2 = scroll1 + thumbH;
      if (scroll2 > maxscroll) scroll2 = maxscroll;
      gallery_scroll(scroll2,2000);                                              //  24.70
      return;
   }

   if (strmatch(menu,"Page Up")) {                                               //  scroll 1 page up
      scroll1 = scrollposn / thumbH * thumbH;
      scroll2 = scroll1 - thumbH * xrows;
      if (scroll2 < 0) scroll2 = 0;
      gallery_scroll(scroll2,8000);                                              //  24.70
      return;
   }

   if (strmatch(menu,"Page Down")) {                                             //  scroll 1 page down
      scroll1 = scrollposn / thumbH * thumbH;
      scroll2 = scroll1 + thumbH * xrows;
      if (scroll2 > maxscroll) scroll2 = maxscroll;
      gallery_scroll(scroll2,8000);                                              //  24.70
      return;
   }

   if (strmatch(menu,"Home")) {                                                  //  jump to top
      gallery_scroll(-1,0);                                                      //  stop scrolling
      scrollposn = 0;
      galleryposn = scrollposn / thumbH * xcols;
      gallery(0,"paint",0);
      return;
   }

   if (strmatch(menu,"End")) {                                                   //  jump to bottom
      gallery_scroll(-1,0);                                                      //  stop scrolling
      scrollposn = maxscroll;
      galleryposn = scrollposn / thumbH * xcols;
      gallery(0,"paint",galleryposn);
      return;
   }

   Plog(0,"unknown gallery function: %s \n",menu);
   return;
}


/********************************************************************************/

//  private function
//  scroll gallery page up or down to goal scroll position
//  position:  N  goal scroll position, 0 to maxscroll
//            -1  stop scrolling immediately
//     speed:  N  scroll N pixels/second

#define scrollms 1                                                               //  timer period, millisecs               24.70
#define scrollsec (0.001 * scrollms)

void navi::gallery_scroll(int position, int speed)
{
   if (position < 0) {                                                           //  stop scrolling
      gallery_scrollgoal = -1;
      gallery_scrollspeed = 0;
      return;
   }

   if (gallery_scrollgoal < 0) {                                                 //  start scrolling
      gallery_scrollgoal = position;
      gallery_scrollspeed = speed;
      g_timeout_add(scrollms,gallery_scrollfunc,0);                              //  timer period, milliseconds
      return;
   }

   gallery_scrollgoal = position;                                                //  continue scrolling with
   gallery_scrollspeed = speed;                                                  //    possible goal/speed change
   return;
}


//  private function
//  timer function, runs every 'scrollms' milliseconds

int navi::gallery_scrollfunc(void *)
{
   float    cumscroll = 0;
   int      newscrollposn;

   if (gallery_scrollgoal < 0) return 0;                                         //  stop scrolling

   if (FGM != 'G') {                                                             //  not gallery view
      gallery_scrollgoal = -1;                                                   //  stop scrolling
      return 0;
   }

   if (scrollposn == gallery_scrollgoal) {                                       //  goal reached, stop
      gallery_scrollgoal = -1;
      return 0;
   }

   cumscroll = scrollsec * gallery_scrollspeed;                                  //  based on timer period

   scrollposn = gtk_adjustment_get_value(Gadjust);
   newscrollposn = scrollposn;

   if (scrollposn < gallery_scrollgoal) {                                        //  adjust scroll position
      newscrollposn += cumscroll;
      if (newscrollposn > gallery_scrollgoal)
         newscrollposn = gallery_scrollgoal;
   }

   if (scrollposn > gallery_scrollgoal) {
      newscrollposn -= cumscroll;
      if (newscrollposn < gallery_scrollgoal)
         newscrollposn = gallery_scrollgoal;
   }

   gtk_adjustment_set_value(Gadjust,newscrollposn);
   return 1;
}


/********************************************************************************/

//  private function
//  gallery top panel folder button clicked, open corresponding folder

void navi::navibutt_clicked(GtkWidget *widget, int *lev)
{
   ch     gallerydir[XFCC], *pp;

   gallery_scroll(-1,0);                                                         //  stop scrolling

   pp = gallerydir;

   for (int ii = 0; ii <= *lev; ii++)
   {
      *pp = '/';
      strcpy(pp+1,gallerypath[ii]);
      pp = pp + strlen(pp);
   }

   gallery(gallerydir,"init",0);                                                 //  new gallery
   gallery(0,"sort",-2);                                                         //  recall sort and position
   gallery(0,"paint",-1);                                                        //  paint
   return;
}


/********************************************************************************/

//  private function - [TOP] button: select new top folder

void navi::newtop(GtkWidget *widget, GdkEventButton *event)
{
   static GtkWidget   *popmenu = 0;

   gallery_scroll(-1,0);                                                         //  stop scrolling

   if (popmenu) gtk_widget_destroy(popmenu);
   popmenu = create_popmenu();

   for (int ii = 0; ii < Ntopfolders; ii++)                                      //  insert all top image folders
      add_popmenu_item(popmenu,topfolders[ii],newtop_menu_event,0,0);

   add_popmenu_item(popmenu,"Folder Tree",newtop_menu_event,0,0);
   add_popmenu_item(popmenu,"/",newtop_menu_event,0,0);
   add_popmenu_item(popmenu,"HOME",newtop_menu_event,0,0);
   add_popmenu_item(popmenu,"Desktop",newtop_menu_event,0,0);
   add_popmenu_item(popmenu,"Fotocx home",newtop_menu_event,0,0);
   add_popmenu_item(popmenu,"Saved Areas",newtop_menu_event,0,0);
   add_popmenu_item(popmenu,"recent images",newtop_menu_event,0,0);
   add_popmenu_item(popmenu,"newest images",newtop_menu_event,0,0);

   popup_menu(Mwin,popmenu);
   return;
}

void navi::newtop_menu_event(GtkWidget *, ch *menu)                              //  menu event function
{
   ch     folder[200];

   if (! menu) return;

   strncpy0(folder,menu,200);

   if (strmatch(menu,"Folder Tree")) {
      m_folder_tree(0,0);
      return;
   }

   if (strmatch(menu,"recent images")) {
      m_recentfiles(0,0);
      return;
   }

   if (strmatch(menu,"newest images")) {
      m_newfiles(0,0);
      return;
   }

   if (strmatch(menu,"HOME"))                                                    //  user home folder
      strncpy0(folder,getenv("HOME"),200);

   if (strmatch(menu,"Desktop"))                                                 //  user desktop
      snprintf(folder,200,"%s/%s",getenv("HOME"),desktopname);                   //  locale-specific desktop name

   if (strmatch(menu,"Fotocx home"))                                             //  fotocx home folder
      strncpy0(folder,get_zhomedir(),200);

   if (strmatch(menu,"Saved Areas"))                                             //  saved areas folder
      snprintf(folder,200,"%s/saved_areas",get_zhomedir());

   gallery(folder,"init",0);                                                     //  new gallery
   gallery(0,"sort",-2);                                                         //  recall sort and position
   gallery(0,"paint",-1);                                                        //  paint
   return;
}


/********************************************************************************/

//  private function - [Album] button: select new album

void navi::newalbum(GtkWidget *widget, GdkEventButton *event)
{
   static GtkWidget  *popmenu = 0;

   ch          **flist, *pp;
   ch          findcomm[200];
   ch          *albums[200];
   int         ii, NF, count = 0;

   gallery_scroll(-1,0);                                                         //  stop scrolling

   snprintf(findcomm,200,"%s/*",albums_folder);                                  //  find all album names
   zfind(findcomm,flist,NF);

   for (ii = 0; ii < NF && count < 200; ii++)
   {
      pp = strrchr(flist[ii],'/');
      if (! pp) continue;
      albums[count] = zstrdup(pp+1,"navi-album");
      count++;
   }

   for (int ii = 0; ii < NF; ii++)
      zfree(flist[ii]);
   zfree(flist);

   if (! count) {
      zmessageACK(Mwin,"no albums found");
      return;
   }

   if (count > 1)                                                                //  sort album names
      HeapSort(albums,count);

   if (popmenu) gtk_widget_destroy(popmenu);
   popmenu = create_popmenu();

   add_popmenu_item(popmenu,"Current Album",newalbum_menu_event,0,0);            //  add "current album"

   for (int ii = 0; ii < count; ii++)                                            //  insert all known albums
      add_popmenu_item(popmenu,albums[ii],newalbum_menu_event,0,0);

   popup_menu(widget,popmenu);
   return;
}


void navi::newalbum_menu_event(GtkWidget *, ch *menu)                            //  menu event function
{
   ch  albumfile[200];

   if (! menu || strmatch(menu,"Current Album"))
   {
      if (! curr_album || ! *curr_album) {
         zmessageACK(Mwin,"no current album");
         return;
      }
      album_show();
      return;
   }

   snprintf(albumfile,200,"%s/%s",albums_folder,menu);                           //  show the album gallery
   album_show(albumfile);
   return;
}


/********************************************************************************/

//  private function
//  mouse event function for gallery window - get selected thumbnail and file
//  user function receives clicked file, which is subject for zfree()

int navi::mouse_event(GtkWidget *widget, GdkEvent *event, void *)
{
   GdkEventButton *eventB;
   PIXBUF         *pixbuf;
   int            evtype, mousex, mousey, mousebutt;
   int            row, col, nrows, tww, thh, marg;
   int            Nth, poswidth, posheight, err, ftype;
   static int     Fmyclick = 0;
   ch             *filez = 0;
   STATB          statB;

   if (! Gfiles) return 1;                                                       //  empty gallery
   if (! gallerypainted) return 1;                                               //  not initialized

   eventB = (GdkEventButton *) event;
   evtype = eventB->type;
   mousex = int(eventB->x);
   mousey = int(eventB->y);
   mousebutt = eventB->button;
   if (mousex < margin) return 1;
   if (mousey < margin) return 1;

   KBcontrolkey = KBshiftkey = KBaltkey = 0;
   if (eventB->state & GDK_CONTROL_MASK) KBcontrolkey = 1;
   if (eventB->state & GDK_SHIFT_MASK) KBshiftkey = 1;
   if (eventB->state & GDK_MOD1_MASK) KBaltkey = 1;

   if (mousebutt == 1 && KBaltkey) mousebutt = 3;                                //  left butt + ALT key >> right butt

   row = (mousey - margin) / thumbH;                                             //  find selected row, col
   col = (mousex - margin) / thumbW;

   if (thumbsize) {
      poswidth = (mousex - margin) - thumbW * col;                               //  mouse position within thumbnail
      poswidth = 100 * poswidth / thumbsize;                                     //  0-100 = left to right edge
      posheight = (mousey - texthh - margin) - thumbH * row;
      posheight = 100 * posheight / thumbsize;                                   //  0-100 = top to bottom edge
   }
   else poswidth = posheight = 0;

   if (! xcols) return 1;
   nrows = 1 + (Gfiles-1) / xcols;                                               //  total thumbnail rows, 1 or more
   if (col < 0 || col >= xcols) return 1;                                        //  mouse not on a thumbnail
   if (row < 0 || row >= nrows) return 1;
   Nth = xcols * row + col;                                                      //  mouse at this thumbnail (image file)
   if (Nth >= Gfiles) return 1;

   filez = zstrdup(Gindex[Nth].file,"navi-mouse");                               //  file (thumbnail) at mouse posn.

   if (evtype == GDK_BUTTON_PRESS)
   {
      gallery_scroll(-1,0);                                                      //  stop scrolling

      Fmyclick = 1;                                                              //  button press is mine

      if (drag_file) zfree(drag_file);
      drag_file = 0;
      ftype = image_file_type(filez);                                            //  save for poss. drag-drop
      if (ftype == IMAGE || ftype == RAW || ftype == VIDEO) {
         drag_file = zstrdup(filez,"navi-mouse");                                //  save file and position in gallery
         drag_posn = Nth;
      }
      goto cleanup;
   }

   if (evtype == GDK_BUTTON_RELEASE)
   {
      gallery_scroll(-1,0);                                                      //  stop scrolling

      if (! Fmyclick) goto cleanup;                                              //  ignore unmatched button release
      Fmyclick = 0;                                                              //    (from vanished popup window)

      err = stat(filez,&statB);
      if (err) goto cleanup;                                                     //  file is gone?

      if (S_ISDIR(statB.st_mode)) {                                              //  if folder, go there
         gallery(filez,"init",0);                                                //  new gallery
         gallery(0,"sort",-2);                                                   //  recall sort and position
         gallery(0,"paint",-1);                                                  //  paint
         goto cleanup;
      }

      if (clicked_file) zfree(clicked_file);                                     //  save clicked file and gallery position
      clicked_file = zstrdup(filez,"navi-mouse");
      clicked_posn = Nth;
      clicked_width = poswidth;                                                  //  normalized 0-100
      clicked_height = posheight;

      if (thumbsize) {
         pixbuf = get_thumb_pixbuf(filez);                                       //  get thumbnail image                   24.10
         if (pixbuf) {
            tww = gdk_pixbuf_get_width(pixbuf);                                  //  thumbnail width and height
            thh = gdk_pixbuf_get_height(pixbuf);
            marg = (thumbsize - tww) / 4;                                        //  allow for margin offset
            poswidth -= 100 * marg/thumbsize;
            clicked_width = poswidth * thumbsize / tww;                          //  clicked position is relative
            clicked_height = posheight * thumbsize / thh;                        //    to thumbnail dimensions
            if (clicked_width < 0) clicked_width = 0;
            if (clicked_width > 100) clicked_width = 100;
            if (clicked_height < 0) clicked_height = 0;
            if (clicked_height > 100) clicked_height = 100;
         }
      }

      if (mousebutt == 1)                                                        //  left click
      {
         if (zd_select_files) select_files_Lclick_func(Nth);                     //  send to select_files()
         else if (zd_select_files1) select_files1_Lclick_func(Nth);              //  send to select_files1()
         else if (zd_album_update) update_albums_Lclick_func(Nth);               //  send to album_replacefile()
         else if (zd_edit_bookmarks) edit_bookmarks_Lclick_func(Nth);            //  send to bookmarks editor
         else if (zd_ss_imageprefs) ss_imageprefs_Lclick_func(Nth);              //  send to slide show editor
         else if (zd_copymove) m_copy_move(0,0);                                 //  update copy/move dialog
         else if (zd_rename) m_rename(0,0);                                      //  update rename dialog
         else if (zd_permissions) m_permissions(0,0);                            //  update permissions dialog
         else if (zd_deltrash) m_delete_trash(0,0);                              //  update delete/trash dialog
         else if (zd_deletemeta) m_meta_delete(0,0);                             //  update delete metadata dialog
         else if (zd_editmeta || zd_editanymeta || zd_metaview) {                //  (any/all can be active)
            if (zd_editmeta) m_meta_edit_main(0,0);                              //  update edit metadata dialog
            if (zd_editanymeta) m_meta_edit_any(0,0);                            //  update edit any metadata dialog
            if (zd_metaview) meta_view(0);                                       //  update metadata view window
         }
         else if (KBshiftkey) gallery_popimage();                                //  popup large image
         else gallery_Lclick_func(Nth);                                          //  open the file
      }

      if (mousebutt == 2) gallery_popimage();                                    //  middle click, popup large image

      if (mousebutt == 3) {                                                      //  right click
         if (zd_select_files) select_files_Rclick_func(Nth);                     //  send to select_files()
         else gallery_Rclick_popup(Nth);                                         //  send to gallery thumbnail popup menu
      }
   }

cleanup:
   if (filez) zfree(filez);
   return 0;                                                                     //  must be 0 for drag/drop to work
}


/********************************************************************************/

//  this function is called if a drag-drop is initiated from the gallery window

ch * navi::gallery_dragfile()
{
   ch       dragfile[200];
   FILE     *fid;

   snprintf(dragfile,200,"%s/drag_from_folder",get_zhomedir());                  //  save source gallery name
   fid = fopen(dragfile,"w");                                                    //    and position
   if (! fid) {
      zmessageACK(Mwin,strerror(errno));
      return 0;
   }

   fprintf(fid,"%s\n",galleryname);
   fprintf(fid,"%d\n",drag_posn);
   fclose(fid);

   return drag_file;
}


//  this function is called if a drag-drop file is dragged or dropped on the gallery window

void navi::gallery_dropfile(int mousex, int mousey, ch *file)
{
   int      err, top, mpos, speed;
   int      row, col, nrows, Nth, cc;
   int      poswidth, from_posn = -1;
   ch       dragfile[200], buff[200];
   ch       *pp1, *pp2, *from_gallery = 0, *newfile;
   FILE     *fid;

   if (gallerytype != FOLDER && gallerytype != ALBUM) return;                    //  reject others (search, recent ...)

   if (! file)                                                                   //  drag motion underway
   {
      if (! mousex) {                                                            //  drag leave event
         gallery_scroll(-1,0);                                                   //  stop scroll
         return;
      }

      top = gtk_adjustment_get_value(Gadjust);                                   //  mouse vertical posn in window
      mpos = 100 * (mousey - top) / xwinH;                                       //  0 - 100

      if (mpos < 15 && top > 0) {                                                //  mouse position from window top to bottom
         if (mpos < 0) mpos = 0;                                                 //      0 .... 15 .......... 85 .... 100
         speed = 200 * (15 - mpos);                                              //  corresponding scroll speed
         gallery_scroll(0,speed);                                                //    4000 ... 200 ... 0 ... 200 ... 4000
      }                                                                          //    down     down           up      up

      if (mpos > 85 && top < maxscroll) {
         if (mpos >= 100) mpos = 100;
         speed = 200 * (mpos - 85);
         gallery_scroll(maxscroll,speed);
      }

      if (mpos >= 15 && mpos <= 85)                                              //  stop scrolling in mid-window range
         gallery_scroll(-1,0);

      return;
   }

   gallery_scroll(-1,0);                                                         //  stop scrolling

   if (gallerytype == FOLDER)                                                    //  folder gallery, add new file
   {
      err = cp_copy(file,galleryname);
      if (err) {
         zfree(file);
         zmessageACK(Mwin,strerror(err));
         return;
      }

      pp1 = strrchr(file+2,'/');                                                 //  isolate filename.ext
      if (! pp1) return;
      cc = strlen(pp1);
      newfile = zstrdup(galleryname,"navi-dropfile",cc+2);                       //  construct galleryname/filename
      strcat(newfile,pp1);
      load_filemeta(file);                                                       //  update image index
      save_filemeta(newfile);
      zfree(file);
      zfree(newfile);
      if (curr_file) load_filemeta(curr_file);                                   //  restore curr. metadata

      gallery(0,"init",0);                                                       //  refresh gallery
      gallery(0,"sort",-2);                                                      //  sort, keep position
      gallery(0,"paint",-1);                                                     //  paint
      return;
   }

   row = (mousey - margin) / thumbH;                                             //  find drop location: row, col
   col = (mousex - margin) / thumbW;

   if (col < 0) col = 0;
   if (col >= xcols) col = xcols-1;

   if (xcols) nrows = 1 + (Gfiles-1) / xcols;                                    //  total thumbnail rows, 1 or more
   else nrows = 1;
   if (nrows < 1) nrows = 1;
   if (row < 0) row = 0;
   if (row >= nrows) row = nrows-1;

   if (thumbsize) {
      poswidth = (mousex - margin) - thumbW * col;                               //  mouse position within thumbnail
      poswidth = 100 * poswidth / thumbsize;                                     //  0-100 = left to right edge
   }
   else poswidth = 0;

   Nth = xcols * row + col;                                                      //  mouse at this thumbnail (image file)
   if (poswidth > 50) Nth++;                                                     //  drop after this position
   if (Nth > Gfiles) Nth = Gfiles;                                               //  last + 1


   snprintf(dragfile,200,"%s/drag_from_folder",get_zhomedir());                  //  get source gallery name
   fid = fopen(dragfile,"r");                                                    //    and position
   if (! fid) {
      zmessageACK(Mwin,strerror(errno));
      return;
   }

   pp1 = fgets_trim(buff,200,fid);                                               //  source gallery name
   if (pp1) from_gallery = zstrdup(pp1,"navi-dropfile");
   pp2 = fgets_trim(buff,200,fid);                                               //  source gallery position
   if (pp2) from_posn = atoi(pp2);
   fclose(fid);

   if (! pp1 || from_posn < 0) {
      Plog(0,"drag_from data not available \n");
      return;
   }

   if (from_gallery && strmatch(galleryname,from_gallery)) {                     //  drag-drop in same gallery
      album_move_file(from_posn,Nth);                                            //  move file position in gallery
   }
   else album_add_file(file,Nth);                                                //  insert new file at position

   zfree(file);
   if (from_gallery) zfree(from_gallery);

   album_show();
   return;
}


/********************************************************************************/

//  Private function - respond to keyboard navigation keys.
//  KBpress() for main window calls this function when G view is active.

int navi::KBaction(ch *action)
{
   int      ii;
   int      row1, row2, rowf;

   Plog(2,"KBaction: %s \n",action);

   if (strmatch(action,"Open")) {                                                //  goto F-view
      viewmode('F');
      return 1;
   }

   if (strmatch(action,"Zoom+") || strmatch(action,"Zoom-in")) {
      menufuncx(0,"Zoom+");
      return 1;
   }

   if (strmatch(action,"Zoom-") || strmatch(action,"Zoom-out")) {
      menufuncx(0,"Zoom-");
      return 1;
   }

   if (strmatch(action,"Up")) {
      menufuncx(0,"Row Up");
      return 1;
   }

   if (strmatch(action,"Down")) {
      menufuncx(0,"Row Down");
      return 1;
   }

   if (strmatch(action,"Home")) {
      menufuncx(0,"Home");
      return 1;
   }

   if (strmatch(action,"End")) {
      menufuncx(0,"End");
      return 1;
   }

   if (strmatch(action,"Page_Up")) {
      menufuncx(0,"Page Up");
      return 1;
   }

   if (strmatch(action,"Page_Down")) {
      menufuncx(0,"Page Down");
      return 1;
   }

   if (strmatch(action,"Left")) {                                                //  left arrow - previous image
      m_prev(0,0);
      row1 = scrollposn / thumbH;
      rowf = curr_file_posn / xcols;
      if (rowf < row1) menufuncx(0,"Row Up");
      return 1;
   }

   if (strmatch(action,"Right")) {                                               //  right arrow - next image
      m_next(0,0);
      row1 = scrollposn / thumbH;
      row2 = row1 + xwinH / thumbH - 1;
      rowf = curr_file_posn / xcols;
      if (rowf == 0) menufuncx(0,"Home");
      if (rowf > row2) menufuncx(0,"Row Down");
      return 1;
   }

   if (strmatch(action,"Delete")) {                                              //  delete key - delete/trash dialog
      m_delete_trash(0,0);
      return 1;
   }

   if (strmatch(action,"Show Hidden")) {
      Fshowhidden = 1 - Fshowhidden;
      gallery(0,"init",0);
      gallery(0,"paint",-1);
      return 1;
   }

//  check for menu function KB shortcut

   for (ii = 0; ii < Nkbsf; ii++)
      if (strmatchcase(action,kbsftab[ii].menu)) break;

   if (ii == Nkbsf) {
      Plog(0,"shortcut not found: %s \n",action);
      return 1;
   }

   kbsftab[ii].func(0,kbsftab[ii].arg);                                          //  call the menu function
   return 1;
}


/********************************************************************************/

//  save and restore gallery sort and top file position

#define RGmax 100                                                                //  max. recent galleries to remember

typedef struct {                                                                 //  gallery memory data
   int      galleryposn;                                                         //  top file position (scroll position)
   int      gallerysort;                                                         //  sort galleryname/filedate/photodate
   int      galleryseq;                                                          //  sort ascending/descending
   ch       *galleryname;                                                        //  gallery name /.../filename
}
gallerymem_t;

gallerymem_t   gallerymem[RGmax];                                                //  array of gallery memory
gallerymem_t   Tgallerymem;

int            NGmem;                                                            //  current entries <= RGmax


void gallery_memory(ch *action)
{
   FILE        *fid;
   ch          buff[XFCC], *pp;
   int         ii, nn, err;

   if (strmatch(action,"reset"))                                                 //  clear gallery memory data
   {
      NGmem = 0;
      return;
   }

   if (strmatch(action,"load"))                                                  //  load gallery memory from file
   {                                                                             //    at Fotocx startup time
      NGmem = 0;

      fid = fopen(gallerymem_file,"r");                                          //  open gallery memory file
      if (fid)
      {
         for (ii = 0; ii < RGmax; ii++)
         {
            pp = fgets_trim(buff,XFCC,fid);                                      //  read gallery memory record
            if (! pp) break;                                                     //  NNNNNN N N /folder/name/...

            err = convSI(pp,nn,0,999999);                                        //  NNNNNN = gallery top file position
            if (err) break;
            gallerymem[ii].galleryposn = nn;

            err = convSI(pp+7,nn,0,3);                                           //  N = 0/1/2/3 = sort
            if (err) break;                                                      //    none / f.name / f.date / photo-date
            gallerymem[ii].gallerysort = nn;

            err = convSI(pp+9,nn,0,2);                                           //  N = 0/1/2 = none / ascend / descend
            if (err) break;
            gallerymem[ii].galleryseq = nn;

            pp += 11;                                                            //  gallery name (folder)
            if (*pp != '/') break;
            gallerymem[ii].galleryname = zstrdup(pp,"gallery-memory");
         }

         fclose(fid);
         NGmem = ii;                                                             //  memory entry count
      }

      return;
   }

   if (strmatch(action,"save"))                                                  //  save gallery memory at shutdown
   {
      fid = fopen(gallerymem_file,"w");
      if (! fid) return;
      for (ii = 0; ii < NGmem; ii++)
         fprintf(fid,"%06d %1d %1d %s\n",
                     gallerymem[ii].galleryposn, gallerymem[ii].gallerysort,
                     gallerymem[ii].galleryseq, gallerymem[ii].galleryname);
      fclose(fid);
      return;
   }

   if (! galleryname) return;

   if (strmatch(action,"get"))                                                   //  get gallery data from memory
   {
      for (ii = 0; ii < NGmem; ii++)                                             //  search for gallery in memory
         if (strmatch(galleryname,gallerymem[ii].galleryname)) break;

      if (ii < NGmem) {
         galleryposn = gallerymem[ii].galleryposn;                               //  found, restore top file posn and sort
         gallerysort = (GSORT) gallerymem[ii].gallerysort;
         galleryseq = (GSEQ) gallerymem[ii].galleryseq;
      }
      else {                                                                     //  not found, use defaults
         if (gallerytype == FOLDER) {
            gallerysort = FNAME;
            galleryseq = ASCEND;
            galleryposn = 0;
         }
         else {
            gallerysort = SNONE;
            galleryseq = QNONE;
            galleryposn = 0;
         }
      }

      return;
   }

   if (strmatch(action,"put"))                                                   //  save gallery data in memory
   {
      if (! galleryname) return;

      for (ii = 0; ii < NGmem; ii++)                                             //  search for gallery in memory
         if (strmatch(galleryname,gallerymem[ii].galleryname)) break;

      if (NGmem == 0 || ii == NGmem)                                             //  not found
      {
         if (NGmem == RGmax) {                                                   //  gallery memory full
            zfree(gallerymem[ii-1].galleryname);                                 //    remove last entry
            NGmem--;
         }

         for (ii = NGmem; ii > 0; ii--)                                          //  push all entries up
            gallerymem[ii] = gallerymem[ii-1];                                   //    to free entry [0]

         NGmem++;                                                                //  one more entry
         gallerymem[0].galleryname = zstrdup(galleryname,"gallery-memory");      //  entry [0] is mine
      }

      else if (ii > 0)                                                           //  found at entry [ii]
      {
         Tgallerymem = gallerymem[0];                                            //  exchange entries [0] and [ii]
         gallerymem[0] = gallerymem[ii];
         gallerymem[ii] = Tgallerymem;
      }

      gallerymem[0].galleryposn = galleryposn;                                   //  update entry [0]
      gallerymem[0].gallerysort = gallerysort;
      gallerymem[0].galleryseq = galleryseq;

      return;
   }

   zappcrash("gallery_memory() %s",action);                                      //  bad action
   return;
}


/********************************************************************************/

//  set the window title for the gallery window
//  window title = gallery name

void set_gwin_title()
{
   ch     *pp, title[200];

   if (FGM != 'G') return;

   if (gallerytype == FOLDER)
      snprintf(title,200,"%s   FOLDER %s   %d folders  %d files",                //  use Frelease
                           Frelease,galleryname,Gfolders,Gimages);

   else if (gallerytype == SEARCH || gallerytype == META)
      snprintf(title,200,"%s   SEARCH RESULTS   %d files",Frelease,Gimages);

   else if (gallerytype == ALBUM) {
      pp = strrchr(galleryname,'/');
      if (! pp) pp = galleryname;
      else pp++;
      snprintf(title,200,"%s   ALBUM %s   %d files",Frelease,pp,Gimages);
   }

   else if (gallerytype == RECENT)
      snprintf(title,200,"%s   RECENT FILES",Frelease);

   else if (gallerytype == NEWEST)
      snprintf(title,200,"%s   NEWEST FILES",Frelease);

   else snprintf(title,200,"%s   UNKNOWN",Frelease);

   gtk_window_set_title(MWIN,title);
   return;
}


/********************************************************************************/

//  Return previous or next image file from curr_file in the gallery file list.
//  If lastver is set, only the last edit version is returned.
//  (gallery must be sorted by file name (version sequence)).
//  Returns null if no previous/next file found.

ch * prev_next_file(int index, int lastver)
{
   int      Nth;
   ch       *rootname1 = 0, *rootname2 = 0;                                      //  file names without .vNN and .ext
   ch       *file = 0, *filever = 0;

   Nth = curr_file_posn;

   if (index == +1)                                                              //  get next file
   {
      while (true)
      {
         Nth += 1;
         if (Nth >= Gfiles) {                                                    //  no more files this gallery
            if (filever) break;                                                  //  return last file version
            goto retnull;                                                        //  no more files
         }
         file = gallery(0,"getR",Nth);                                           //  get next file 
         if (! file) goto retnull;
         if (! lastver) goto retfile;                                            //  return all versions
         if (! filever) {
            filever = file;                                                      //  potential last version
            file = 0;
            rootname1 = file_rootname(filever);                                  //  save rootname
            continue;
         }
         if (rootname2) zfree(rootname2);
         rootname2 = file_rootname(file);
         if (! strmatch(rootname1,rootname2)) break;                             //  new rootname, filever was last version
         filever = file;                                                         //  save last file with same rootname
         file = 0;
      }

      file = filever;
      goto retfile;
   }

   if (index == -1)                                                              //  get previous file
   {
      if (curr_file) rootname1 = file_rootname(curr_file);                       //  current file rootname
      while (true)
      {
         Nth -= 1;
         if (Nth < Gfolders) goto retnull;                                       //  no more files
         file = gallery(0,"getR",Nth);                                           //  get previous file 
         if (! file) goto retnull;
         if (! lastver) goto retfile;                                            //  return all versions
         if (! rootname1) goto retfile;                                          //  no current file - return previous file
         if (rootname2) zfree(rootname2);
         rootname2 = file_rootname(file);
         if (! strmatch(rootname1,rootname2)) goto retfile;                      //  new rootname, return this file
      }
   }

   retnull:
   file = 0;

   retfile:
   if (rootname1) zfree(rootname1);
   if (rootname2) zfree(rootname2);
   return file;
}


/********************************************************************************/

//  Find the previous or next gallery from the current gallery.
//  (previous/next defined by subfolder sequence in parent folder)
//  returned gallery is subject for zfree().

ch * prev_next_gallery(int index)
{
   int      nn, Nth;
   ch       *parentdir = 0, *olddir = 0, *newdir = 0, *file = 0;

   if (gallerytype != FOLDER) goto errret;                                       //  gallery not a physical folder

   olddir = zstrdup(galleryname,"prev-next-gallery");                            //  olddir = current gallery / folder
   if (! olddir) goto errret;
   nn = strlen(olddir) - 1;
   if (olddir[nn] == '/') olddir[nn] = 0;
   parentdir = zstrdup(olddir,"prev-next-gallery");                              //  get parent folder
   for (NOP; nn && parentdir[nn] != '/'; nn--)
   if (! nn) goto errret;
   parentdir[nn] = 0;
   gallery(parentdir,"init",0);                                                  //  gallery = parent

   for (Nth = 0; Nth < Gfolders; Nth++) {                                        //  find olddir in parent
      if (file) zfree(file);
      file = gallery(0,"get",Nth);
      if (! file) goto errret;
      if (strmatch(file,olddir)) break;
   }

   Nth += index;                                                                 //  previous or next folder
   if (Nth < 0 || Nth >= Gfolders) goto errret;
   newdir = gallery(0,"get",Nth);
   if (newdir) goto okret;

errret:
   if (newdir) zfree(newdir);
   newdir = 0;

okret:
   if (olddir) {
      gallery(olddir,"init",0);                                                  //  restore old folder
      gallery(0,"sort",-2);                                                      //  recall sort and position
      zfree(olddir);
      gallerypainted = 1;                                                        //  no need to paint
   }
   if (parentdir) zfree(parentdir);
   if (file) zfree(file);
   return newdir;
}


/********************************************************************************/

//  Get file position in gallery file list.
//  If Nth position matches file, this is returned.
//  Otherwise the list is searched from position 0.
//  Position 0-last is returned if found, or -1 if not.

int file_position(ch *file, int Nth)
{
   int      ii;

   if (! Gimages) return -1;
   if (! file) return -1;

   if (Nth >= Gfolders && Nth < Gfiles)
      if (strmatch(file,Gindex[Nth].file)) return Nth;

   for (ii = Gfolders; ii < Gfiles; ii++)
      if (strmatch(file,Gindex[ii].file)) break;

   if (ii < Gfiles) return ii;
   return -1;
}


/********************************************************************************/

//  Determine if a file is a folder or a supported image file type

FTYPE image_file_type(ch *file)
{
   int         err, xcc;
   static int  ftf = 1, tdcc = 0;
   ch          *ppx;
   ch          ppx2[8], *RP;
   STATB       statB;
   static ch   myRAWtypes[1000] = " ";                                           //  room for 200 file types
   static ch   myVIDEOtypes[1000] = " ";                                         //  ditto

   if (ftf) {
      if (thumbfolder && *thumbfolder == '/')                                    //  thumbnail top folder
         tdcc = strlen(thumbfolder);                                             //  save length
      ftf = 0;
   }

   if (! file) return FNF;
   RP = f_realpath(file);                                                        //  use real path
   if (! RP) return FNF;

   err = stat(RP,&statB);                                                        //  file not found
   if (err) {
      zfree(RP);
      return FNF;
   }

   if (S_ISDIR(statB.st_mode)) {                                                 //  folder
      zfree(RP);
      return FDIR;
   }

   if (! S_ISREG(statB.st_mode)) {                                               //  not a regular file
      zfree(RP);
      return OTHER;
   }

   if (tdcc && strmatchN(RP,thumbfolder,tdcc)) {                                 //  a fotocx thumbnail
      zfree(RP);
      return THUMB;
   }

   ppx = strrchr(RP,'.');
   if (! ppx) {                                                                  //  no file .ext
      zfree(RP);
      return OTHER;
   }

   xcc = strlen(ppx);                                                            //  file .ext > 6 chars.
   if (xcc > 6) {
      zfree(RP);
      return OTHER;
   }

   strcpy(ppx2,ppx);                                                             //  add trailing blank: ".ext "
   strcpy(ppx2+xcc," ");

   zfree(RP);

   if (strcasestr(imagefiletypes,ppx2)) return IMAGE;                            //  supported image type
   if (strcasestr(myRAWtypes,ppx2))return RAW;                                   //  one of my RAW types
   if (strcasestr(myVIDEOtypes,ppx2)) return VIDEO;                              //  one of my VIDEO types

   if (strcasestr(RAWfiletypes,ppx2)) {                                          //  found in known RAW types 
      strcat(myRAWtypes,ppx2);                                                   //  add to my RAW types
      return RAW;
   }

   if (! Ffmpeg) return OTHER;                                                   //  missing ffmpeg program

   if (strcasestr(VIDEOfiletypes,ppx2)) {                                        //  found in known VIDEO types
      strcat(myVIDEOtypes,ppx2);                                                 //  add to my VIDEO types
      return VIDEO;
   }

   return OTHER;                                                                 //  not a known image file type
}


/********************************************************************************/

//  Given a thumbnail file, get the corresponding image file.
//  Returns null if no image file found.
//  Returned file is subject for zfree().
//  image file:  /image/folder/file.xxx
//  thumb folder:  /thumb/folder
//  thumb file:  /thumb/folder/image/folder/file.xxx.jpeg

ch * thumb2imagefile(ch *thumbfile)                                              //  simplified
{
   uint        cc;
   ch          *imagefile;
   static int  Fdone = 0;

   if (! thumbfolder || *thumbfolder != '/') {
      if (! Fdone) Plog(0,"%s \n","no thumbnail folder");
      Fdone++;
      return 0;
   }

   cc = strlen(thumbfolder);
   if (cc > strlen(thumbfile) - 12) {                                            //  /thumbfolder/imagefolder/file.xxx.jpeg
      Plog(0,"invalid thumbfile: %s \n",thumbfile);
      return 0;
   }

   imagefile = zstrdup(thumbfile+cc,"thumb2imagefile");                          //  /imagefolder/file.xxx.jpeg
   cc = strlen(imagefile);
   imagefile[cc-5] = 0;                                                          //  /imagefolder/file.xxx
   if (regfile(imagefile)) return imagefile;                                     //  return if exists
   zfree(imagefile);                                                             //  not found
   return 0;
}


//  Given an image file, get the corresponding thumbnail file.
//  The filespec is returned whether or not the file exists.
//  Returned file is subject for zfree().

ch * image2thumbfile(ch *imagefile)                                              //  simplified
{
   int         cc1, cc2;
   ch          *RP, *thumbfile;
   static int  Fdone = 0;

   if (! thumbfolder || *thumbfolder != '/') {
      if (! Fdone++) Plog(0,"%s \n","no thumbnail folder");
      return 0;
   }

   RP = f_realpath(imagefile);                                                   //  use real path
   if (! RP) return 0;
   if (! regfile(RP)) { zfree(RP); return 0; }
   cc1 = strlen(thumbfolder);
   cc2 = strlen(RP);
   thumbfile = (ch *) zmalloc(cc1+cc2+6,"image2thumbfile");
   strcpy(thumbfile,thumbfolder);                                                //  /thumb/folder
   strcpy(thumbfile+cc1,RP);                                                     //  .../image/folder/file.xxx
   strcpy(thumbfile+cc1+cc2,".jpeg");                                            //  .../image/folder/file.xxx.jpeg
   zfree(RP);
   return thumbfile;
}


//  version to avoid use of zmalloc() & zfree()
//  output: ch thumbfile[XFCC]
//  returns 0 if OK, 1 if image file not found

int image2thumbfile2(ch *imagefile, ch *thumbfile)
{
   int         cc1, cc2;
   ch          *RP;
   static int  Fdone = 0;

   *thumbfile = 0;

   if (! thumbfolder || *thumbfolder != '/') {
      if (! Fdone++) Plog(0,"no thumbnail folder\n");
      return 1;
   }

   RP = f_realpath(imagefile);                                                   //  use real path
   if (! RP) return 1;
   if (! regfile(RP)) { zfree(RP); return 1; }
   cc1 = strlen(thumbfolder);
   cc2 = strlen(RP);
   strcpy(thumbfile,thumbfolder);                                                //  /thumb/folder
   strcpy(thumbfile+cc1,RP);                                                     //  .../image/folder/file.xxx
   strcpy(thumbfile+cc1+cc2,".jpeg");                                            //  .../image/folder/file.xxx.jpeg
   zfree(RP);
   return 0;
}


/********************************************************************************/

//  check if thumbnail file is missing or stale.
//  returns 1  new thumbnail NOT needed
//          0  new thumbnail needed

int thumbfile_OK(ch *imagefile)
{
   int         ftype;
   STATB       statF, statB;
   ch          thumbfile[XFCC];

   if (! regfile(imagefile,&statF)) return 1;                                    //  bad image file

   ftype = image_file_type(imagefile);
   if (ftype != IMAGE && ftype != RAW && ftype != VIDEO) return 1;               //  not an image file or RAW file

   image2thumbfile2(imagefile,thumbfile);                                        //  get thumbnail file for image file
   if (! regfile(thumbfile,&statB)) return 0;                                    //  thumbfile FNF
   if (statB.st_mtime != statF.st_mtime) return 0;                               //  thumbnail is stale
   return 1;                                                                     //  thumbnail is up to date
}


/********************************************************************************/

//  get "folder" or "broken" pixbuf using cached pixbuf image
//  caller must avoid gdk_object_unref() of returned pixbuf

PIXBUF * get_folder_pixbuf()
{
   static int        ftf = 1, err;
   static PIXBUF     *bigpixbuf = 0;
   static PIXBUF     *pixbuf = 0;
   static int        psize = -1;
   static ch         thumbfile[300] = "";
   GError            *gerror = 0;
   STATB             statB;

   if (ftf) {
      ftf = 0;
      snprintf(thumbfile,300,"%s/folder.png",get_zhomedir());                    //  look for user folder.png
      err = stat(thumbfile,&statB);
      if (err) snprintf(thumbfile,300,"%s/folder.png",get_zimagedir());          //  no, get package folder.png
      bigpixbuf = gdk_pixbuf_new_from_file(thumbfile,&gerror);
      if (! bigpixbuf) {
         Plog(0,"cannot make folder pixbuf: ");
         if (gerror) Plog(0,"%s \n",gerror->message);
      }
   }

   if (! bigpixbuf) return 0;
   if (thumbsize == psize) return pixbuf;

   pixbuf = gdk_pixbuf_scale_simple(bigpixbuf,thumbsize,thumbsize,BILINEAR);
   psize = thumbsize;
   return pixbuf;
}


PIXBUF * get_broken_pixbuf()
{
   static int        ftf = 1;
   static PIXBUF     *bigpixbuf = 0;
   static PIXBUF     *pixbuf = 0;
   static int        psize = -1;
   static ch         thumbfile[300] = "";
   GError            *gerror = 0;

   if (ftf) {
      ftf = 0;
      strncatv(thumbfile,300,zfuncs::zimagedir,"/broken.png",0);
      bigpixbuf = gdk_pixbuf_new_from_file(thumbfile,&gerror);
      if (! bigpixbuf) {
         Plog(0,"cannot make broken pixbuf: ");
         if (gerror) Plog(0,"%s \n",gerror->message);
      }
   }

   if (! bigpixbuf) return 0;
   if (thumbsize == psize) return pixbuf;

   pixbuf = gdk_pixbuf_scale_simple(bigpixbuf,thumbsize,thumbsize,BILINEAR);
   psize = thumbsize;
   return pixbuf;
}


/********************************************************************************/

//  create or replace thumbnail file if missing or stale.
//  returns  0  thumbnail exists already, nothing was done (OK)
//           1  new thumbnail file created (OK)
//           2  cannot create PXB or make thumbnail file (error)

int update_thumbfile(ch *imagefile)
{
   ch          *pp, *tf, thumbfile[XFCC];
   PXB         *imagepxb = 0, *thumbpxb = 0;
   STATB       statB;
   int         err, size, retstat;
   timeval     thumbtimes[2];

   if (thumbfile_OK(imagefile)) return 0;                                        //  thumbnail file exists, not stale

   Funcbusy(+1);                                                                 //  "working"  24.10

   err = image2thumbfile2(imagefile,thumbfile);                                  //  get thumbnail file for image file
   if (err) { retstat = 2; goto returnx; }

   pp = strrchr(thumbfile+1,'/');                                                //  folder
   if (! pp) { retstat = 2; goto returnx; }
   *pp = 0;
   if (! dirfile(thumbfile)) {                                                   //  create thumbnail folder if needed
      tf = zescape_quotes(thumbfile);
      err = zshell(0,"mkdir -p -m 0750 \"%s\"",tf);
      zfree(tf);
   }
   *pp = '/';
   if (err && errno != EEXIST) {                                                 //  happens, threads
      Plog(0,"thumbnail mkdir failure: %s\n %s\n",thumbfile,strerror(errno));
      retstat = 2;
      goto returnx;
   }

   pp = (ch *) strrchr(imagefile,'.');
   if (! pp) { retstat = 2; goto returnx; }

   size = thumbfilesize;

   if (strcasestr(".jpg .jpeg",pp))
      thumbpxb = JPG_PXB_load(imagefile,size);                                   //  JPEG file to PXB (reduced)

   else if (image_file_type(imagefile) == RAW)                                   //  RAW file
      thumbpxb = RAW_thumb_pxb(imagefile);                                       //  get thumb PXB from embedded image

   if (! thumbpxb) {
      imagepxb = PXB_load(imagefile,0);                                          //  any image file to PXB
      if (imagepxb) thumbpxb = PXB_rescale(imagepxb,size);                       //  rescale PXB to thumbnail size
   }

   if (! thumbpxb) { retstat = 2; goto returnx; }

   err = PXB_JPG_save(thumbpxb,thumbfile,80);                                    //  save to JPEG thumbnail file
   if (err) { retstat = 2; goto returnx; }

   if (! regfile(imagefile,&statB)) { retstat = 2; goto returnx; }               //  get image file mod time

   thumbtimes[0].tv_sec = thumbtimes[1].tv_sec = statB.st_mtim.tv_sec;           //  thumbnail mod time
   thumbtimes[0].tv_usec = thumbtimes[1].tv_usec = 0;                            //    = image file mod time
   utimes(thumbfile,thumbtimes);
   retstat = 1;

returnx:
   if (retstat > 1) Plog(0,"update_thumbfile() failure: %s \n",imagefile);
   if (imagepxb) PXB_free(imagepxb);
   if (thumbpxb) PXB_free(thumbpxb);
   Funcbusy(-1);
   return retstat;
}


/********************************************************************************/

//  Update thumbnail mod time to match corresponding image file
//  (avoid thumbnail refresh when image file metadata is modified)

int thumbfile_set_mod_time(ch *imagefile)
{
   STATB       statB;
   ch          thumbfile[XFCC];
   int         err;
   timeval     thumbtimes[2];

   if (! regfile(imagefile,&statB)) return ENOENT;                               //  get image file mod time

   image2thumbfile2(imagefile,thumbfile);                                        //  get thumbnail file for image file
   if (! regfile(thumbfile)) return ENOENT;

   thumbtimes[0].tv_sec = thumbtimes[1].tv_sec = statB.st_mtim.tv_sec;           //  thumbnail mod time
   thumbtimes[0].tv_usec = thumbtimes[1].tv_usec = 0;                            //    = image file mod time
   err = utimes(thumbfile,thumbtimes);
   return err;
}


/********************************************************************************/

//  Remove thumbnail from disk (for deleted or renamed image file).

void delete_thumbfile(ch *imagefile)
{
   int         err;
   ch          thumbfile[XFCC];

   err = image2thumbfile2(imagefile,thumbfile);                                  //  get thumbnail file for image file
   if (err) return;

   remove(thumbfile);
   return;
}


/********************************************************************************/

//  get thumbnail pixbuf for given image file
//  DO NOT g_object_unref() returned pixbuf.

PIXBUF * get_thumb_pixbuf(ch *imagefile)                                         //  simplify   24.10
{
   ch          thumbfile[XFCC];
   int         ii, err;
   int         ww, hh, size;
   FTYPE       ftype;
   STATB       statB;
   PIXBUF      *tempixbuf;
   static PIXBUF *pixbuf = 0;
   GError      *gerror = 0;

   if (pixbuf) g_object_unref(pixbuf);                                           //  free prior pixbuf memory
   pixbuf = 0;

   if (thumbsize == 0) return 0;                                                 //  should not happen

   if (! regfile(imagefile,&statB))                                              //  check file exists
      if (! dirfile(imagefile,&statB)) return 0;

   ftype = image_file_type(imagefile);

   if (ftype == FDIR)                                                            //  'folder' image
      return get_folder_pixbuf();

   if (ftype != IMAGE && ftype != RAW && ftype != VIDEO)                         //  not an image or RAW or VIDEO file
      return get_broken_pixbuf();                                                //  'broken' image

   ii = update_thumbfile(imagefile);                                             //  make/refresh thumbnail file
   if (ii > 1) return 0;                                                         //  fail

   if (thumbsize <= thumbfilesize) {
      err = image2thumbfile2(imagefile,thumbfile);                               //  small, use thumbnail file
      if (! err) pixbuf = gdk_pixbuf_new_from_file(thumbfile,&gerror);
   }
   else pixbuf = gdk_pixbuf_new_from_file(imagefile,&gerror);                    //  large, use image file
   if (! pixbuf) {
      Plog(0,"cannot make pixbuf: %s %s \n",gerror->message,imagefile);
      return 0;
   }

   ww = gdk_pixbuf_get_width(pixbuf);                                            //  get thumbnail actual size
   hh = gdk_pixbuf_get_height(pixbuf);
   size = ww;
   if (hh > ww) size = hh;
   if (size != thumbsize) {                                                      //  rescale to thumbnail display size
      ww = ww * thumbsize / size;
      hh = hh * thumbsize / size;
      tempixbuf = gdk_pixbuf_scale_simple(pixbuf,ww,hh,BILINEAR);
      g_object_unref(pixbuf);
      pixbuf = tempixbuf;
   }

   return pixbuf;
}


/********************************************************************************/

//  popup a new window with a larger image of a clicked thumbnail

void gallery_popimage()
{
   static int   ftf = 1, ii;
   static ch  *popfiles[20];                                                     //  last 20 files
   int          ftype;

   if (ftf) {
      ftf = 0;                                                                   //  initz. empty file memory
      for (ii = 0; ii < 20; ii++)
         popfiles[ii] = 0;
      ii = 0;
   }

   if (! clicked_file) return;

   ftype = image_file_type(clicked_file);                                        //  exclude RAW etc.
   if (ftype != IMAGE) return;

   ii++;                                                                         //  use next file memory position
   if (ii == 20) ii = 0;
   if (popfiles[ii]) zfree(popfiles[ii]);
   popfiles[ii] = clicked_file;                                                  //  save clicked_file persistently
   clicked_file = 0;                                                             //  reset clicked_file

   popup_image(popfiles[ii],MWIN,1,512);                                         //  popup window with image
   return;
}


/********************************************************************************/

//  select one image file by clicking a gallery thumbnail
//  returned file is subject for zfree()

ch  galsel1_filename[XFCC];

ch * select_files1(ch *gfolder)
{
   zdialog     *zd;
   int         zstat;
   ch          *cfolder;
   ch          fgm;

   fgm = FGM;                                                                    //  save current view mode
   cfolder = zstrdup(navi::galleryname,"gallery-select");                        //  and gallery folder

   if (gfolder) {
      gallery(gfolder,"init",0);                                                 //  switch to caller's gallery
      gallery(0,"sort",-2);                                                      //  recall sort and position
      gallery(0,"paint",-1);                                                     //  paint
   }

   *galsel1_filename = 0;                                                        //  no selection

   viewmode('G');

/***
          _________________________________________
         |             Image File                  |
         |                                         |
         |  Click thumbnail to select file         |
         |                                         |
         | Image File [__________________________] |
         |                                         |
         |                                [OK] [X] |
         |_________________________________________|

***/

   zd = zdialog_new("Image File",Mwin,"OK"," X ",null);                          //  dialog to select a thumbnail
   zd_select_files1 = zd;
   zdialog_add_widget(zd,"label","labtip","dialog","Click thumbnail to select file");
   zdialog_add_widget(zd,"hbox","hbf","dialog",0,"space=3");
   zdialog_add_widget(zd,"label","labf","hbf","Image File","space=3");
   zdialog_add_widget(zd,"zentry","filename","hbf",0,"space=3|expand");

   zdialog_resize(zd,400,0);
   zdialog_run(zd,0,"parent");                                                   //  run dialog and wait for completion
   zstat = zdialog_wait(zd);                                                     //    via mouse click function below

   zdialog_free(zd);
   zd_select_files1 = 0;

   if (cfolder) {
      gallery(cfolder,"init",0);                                                 //  restore view mode
      gallery(0,"sort",-2);
      zfree(cfolder);
   }
   viewmode(fgm);                                                                //  may paint

   if (zstat != 1) return 0;                                                     //  no selection
   if (*galsel1_filename != '/') return 0;
   if (! regfile(galsel1_filename)) return 0;                                    //  FNF

   return f_realpath(galsel1_filename);                                          //  use real path
}


void select_files1_Lclick_func(int Nth)                                          //  called by gallery mouse function
{
   ch       *imagefile = 0, *pp;
   int      ftype;

   if (! zd_select_files1) return;                                               //  should not happen
   if (Nth < 0) return;

   imagefile = gallery(0,"get",Nth);                                             //  get file at clicked position
   if (! imagefile) return;

   ftype = image_file_type(imagefile);                                           //  must be image or RAW file
   if (ftype != IMAGE && ftype != RAW) {
      zfree(imagefile);
      return;
   }

   pp = strrchr(imagefile,'/') + 1;
   zdialog_stuff(zd_select_files1,"filename",pp);                                //  stuff file name in dialog

   strncpy0(galsel1_filename,imagefile,XFCC);                                    //  save full path
   zfree(imagefile);
   return;
}


/********************************************************************************/

//  Select multiple image files from thumbnail gallery window.
//    SelFiles[*]: list of selected image files, SFcount entries.
//  Pre-selected files are passed in the same list, which is updated.
//  The dialog shows the list of files selected and can be edited.

namespace galselnames
{
   int  dialog_event(zdialog *zd, ch *event);
   int  find_file(ch *imagefile);
   void add_file(ch *imagefile);
   void remove_file(ch *imagefile);
   void Xclick_func(int Nth, ch LR);
   void callbackfunc(GtkWidget *textwidget, int line, int posn, int KBkey);
   void showthumb();

   int            Fsetcurr = 0;
   GtkWidget      *drawarea = 0;
   GtkWidget      *Ftext = 0;
   int            currline;
   ch             *imagefile;
   int            GSselect;                                                      //  count of files in select dialog
};


void select_files(int fclear, int fsetcurr)                                      //  add fsetcurr (def. 0)                 24.20
{
   using namespace galselnames;

   int      ii, kk, dups = 0, yn = 0;;

/***
          _________________________________________________________________
         |                  Select Image Files                             |
         |  __________________________________   ________________________  |
         | |                                  | |                        | |
         | |      list of selected files      | | image of current file  | |
         | |                                  | |  selected in file list | |
         | |                                  | |                        | |
         | |                                  | |                        | |
         | |                                  | |                        | |
         | |                                  | |                        | |
         | |                                  | |                        | |
         | |__________________________________| |________________________| |
         |                                                                 |
         | [Remove] [Clear] [Add All] [Add Current]              [Disable] |
         |                                                                 |
         |                                                        [OK] [X] |
         |_________________________________________________________________|

***/

   zdialog *zd = zdialog_new("Select Image Files",Mwin,"OK"," X ",null);
   zd_select_files = zd;
   zdialog_add_widget(zd,"hbox","hb1","dialog",0,"expand|space=3");
   zdialog_add_widget(zd,"scrwin","scrwin","hb1",0,"expand");
   zdialog_add_widget(zd,"text","files","scrwin");
   zdialog_add_widget(zd,"frame","fr12","hb1",0,"space=5");                      //  for thumbnail - added later
   zdialog_add_widget(zd,"hbox","hb2","dialog",0,"space=5");
   zdialog_add_widget(zd,"button","remove","hb2","Remove","space=8");
   zdialog_add_widget(zd,"button","clear","hb2","Clear","space=8");
   zdialog_add_widget(zd,"button","addall","hb2","Add All","space=8");
   zdialog_add_widget(zd,"button","addcurr","hb2","Add Current","space=8");
   zdialog_add_widget(zd,"label","space","hb2",0,"expand");
   zdialog_add_widget(zd,"button","disable","hb2","Disable","space=8");
   zdialog_add_ttip(zd,"remove","remove selected file from list");
   zdialog_add_ttip(zd,"clear","remove all files in list");
   zdialog_add_ttip(zd,"addall","add all gallery files to list");
   zdialog_add_ttip(zd,"addcurr","add current file to list");
   zdialog_add_ttip(zd,"disable","disable thumbnail selection to enable popup menus etc. \n"
                                 "use [Enable] to resume thumbnail selection");

   Ftext = zdialog_gtkwidget(zd,"files");
   textwidget_set_eventfunc(Ftext,callbackfunc);                                 //  set mouse/KB event function

   GtkWidget *frame = zdialog_gtkwidget(zd,"fr12");                              //  drawing area for thumbnail image
   drawarea = gtk_drawing_area_new();
   gtk_widget_set_size_request(drawarea,256,258);
   gtk_container_add(GTK_CONTAINER(frame),drawarea);
   G_SIGNAL(drawarea,"draw",showthumb,0);

   zdialog_resize(zd,600,0);                                                     //  start dialog
   zdialog_run(zd,dialog_event,"save");                                          //  keep relative position

   textwidget_clear(Ftext);
   GSselect = 0;
   currline = -1;
   imagefile = 0;

   if (fclear) {                                                                 //  clear prior list if requested
      for (ii = 0; ii < SFcount; ii++)
         zfree(SelFiles[ii]);
      SFcount = 0;
   }
   
   Fsetcurr = fsetcurr;                                                          //  set current file if requested         24.20

   for (ii = 0; ii < SFcount; ii++)                                              //  move prev. selected files to dialog
   {                                                                             //    and clear selected files list
      textwidget_append(Ftext,0,"%s\n",SelFiles[ii]);
      zfree(SelFiles[ii]);
   }

   GSselect = SFcount;
   SFcount = 0;

   viewmode('G');                                                                //  insure gallery view

   zdialog_wait(zd);                                                             //  wait for dialog completion

   if (zd->zstat != 1)                                                           //  cancel
   {
      zdialog_free(zd);                                                          //  kill dialog - no selectd files
      zd_select_files = 0;
      return;
   }

   for (ii = 0; ii < GSselect; ii++)                                             //  [OK]
   {
      imagefile = textwidget_line(Ftext,ii,1);                                   //  get selected files from dialog
      if (! imagefile || ! *imagefile) continue;
      SelFiles[SFcount++] = imagefile;
   }

   for (ii = kk = 0; ii < SFcount; ii++)                                         //  look for duplicates
      for (kk = ii + 1; kk < SFcount; kk++)
         if (strmatch(SelFiles[ii],SelFiles[kk])) dups++;

   if (dups)
      yn = zmessageYN(Mwin,"remove %d duplicates?",dups);                        //  ask user

   if (yn)
   {
      for (ii = kk = 0; ii < SFcount; ii++) {                                    //  remove duplicates
         if (! SelFiles[ii]) continue;
         for (kk = ii + 1; kk < SFcount; kk++) {
            if (! SelFiles[kk]) continue;
            if (strmatch(SelFiles[ii],SelFiles[kk])) {
               zfree(SelFiles[kk]);
               SelFiles[kk] = 0;
            }
         }
      }

      for (ii = kk = 0; ii < SFcount; ii++)
         if (SelFiles[ii])
            SelFiles[kk++] = SelFiles[ii];
      SFcount = kk;
   }

   zdialog_free(zd);                                                             //  kill dialog
   zd_select_files = 0;
   return;
}


//  gallery getfiles dialog event function

int galselnames::dialog_event(zdialog *zd, ch *event)
{
   using namespace galselnames;

   ch             *file, *ftemp;
   int            Nth;
   FTYPE          ftype;

   if (zd->zstat) {
      if (zd->zstat < 0) {
         zd_select_files = 0;                                                    //  [x] kill dialog
         gallery(0,"paint",-1);
      }
      return 1;
   }

   if (strmatch(event,"disable"))                                                //  toggle disable/enable status
   {
      if (zd_select_files) {
         zdialog_put_data(zd,"disable","Enable");
         zd_select_files = 0;                                                    //  suspend, no capture gallery clicks
      }                                                                          //  (button is the opposite)
      else {
         zdialog_put_data(zd,"disable","Disable");                               //  resume, capture gallery clicks
         zd_select_files = zd;
      }
      return 1;
   }

   if (strmatch(event,"remove"))                                                 //  remove file from list
   {
      if (! GSselect) return 1;
      if (currline < 0) return 1;
      ftemp = textwidget_line(Ftext,currline,0);                                 //  file (text line) to remove (keep \n)
      if (! ftemp) return 1;
      textwidget_delete(Ftext,currline);                                         //  remove from dialog list
      GSselect--;
      if (currline > GSselect-1) currline = GSselect - 1;
      textwidget_highlight_line(Ftext,currline);                                 //  next line
      showthumb();                                                               //  show thumbnail
      gallery(0,"paint",-1);
   }

   if (strmatch(event,"clear")) {                                                //  clear all files
      textwidget_clear(Ftext);
      GSselect = 0;
      currline = -1;
      showthumb();                                                               //  blank thumbnail
      gallery(0,"paint",-1);
   }

   if (strmatch(event,"addall"))                                                 //  insert all files in image gallery
   {
      for (Nth = 0; ; Nth++)
      {
         if (GSselect == SFmax) {
            zmessageACK(Mwin,"exceed %d selected files",SFmax);
            break;
         }

         ftemp = gallery(0,"getR",Nth);                                          //  next file
         if (! ftemp) break;

         ftype = image_file_type(ftemp);                                         //  must be image or RAW file
         if (ftype != IMAGE && ftype != RAW && ftype != VIDEO) continue;

         file = f_realpath(ftemp);                                               //  use real path
         if (! file) continue;

         GSselect++;
         textwidget_append(Ftext,0,"%s\n",file);                                 //  append - could be insert
         zfree(file);
      }

      textwidget_scroll(Ftext,-1);                                               //  scroll to end
      currline = GSselect - 1;                                                   //  position at last file
      showthumb();
      gallery(0,"paint",-1);
   }

   if (strmatch(event,"addcurr"))                                                //  add current file to selection 
      add_file(curr_file);

   return 1;
}


//  See if image file is in the file list already or not.
//  Return the last matching line number or -1 if not found.

int galselnames::find_file(ch *imagefile)
{
   using namespace galselnames;

   int      line;
   ch       *ftemp;

   for (line = GSselect-1; line >= 0; line--)
   {
      ftemp = textwidget_line(Ftext,line,1);                                     //  get file without \n
      if (! ftemp) continue;
      if (strmatch(ftemp,imagefile)) {
         zfree(ftemp);
         return line;
      }
      zfree(ftemp);
   }

   return -1;
}


//  add image file to list, set thumbnail = file

void galselnames::add_file(ch *imagefile)
{
   using namespace galselnames;

   int      ftype;
   ch       *RP;

   if (GSselect == SFmax) {
      zmessageACK(Mwin,"exceed %d selected files",SFmax);
      return;
   }

   ftype = image_file_type(imagefile);                                           //  must be image or RAW file
   if (ftype != IMAGE && ftype != RAW && ftype != VIDEO) return;
   RP = f_realpath(imagefile);                                                   //  use real path
   if (! RP) return;
   textwidget_append2(Ftext,0,"%s\n",RP);                                        //  append file to list
   zfree(RP);
   GSselect++;
   currline = GSselect-1;
   textwidget_highlight_line(Ftext,currline);
   showthumb();                                                                  //  update thumbnail
   if (FGM == 'G') gallery(0,"paint",-1);                                        //  highlight in gallery
   if (Fsetcurr) f_open(imagefile);                                              //  make current                          24.20
   return;
}


//  external access to add a file to the current selection

void add_file_to_selection(ch *imagefile)                                        //  24.20
{
   using namespace galselnames;
   if (! zd_select_files) return;
   add_file(imagefile);
   return;
}


//  remove image file at last position found, set thumbnail = next
//  called when gallery thumbnail is right-clicked

void galselnames::remove_file(ch *imagefile)
{
   using namespace galselnames;

   int      line;

   line = find_file(imagefile);                                                  //  find last instance
   if (line < 0) return;
   currline = line;
   showthumb();
   textwidget_delete(Ftext,currline);
   GSselect--;
   if (currline > GSselect-1) currline = GSselect - 1;
   textwidget_highlight_line(Ftext,currline);
   showthumb();
   gallery(0,"paint",-1);
   return;
}


//  called from image gallery window when a thumbnail is clicked
//  add image file to list at current position, set thumbnail = file

void select_files_Lclick_func(int Nth)                                           //  left click, add
{
   galselnames::Xclick_func(Nth,'L');
   if (clicked_file) zfree(clicked_file);
   clicked_file = 0;
   return;
}

void select_files_Rclick_func(int Nth)                                           //  right click, find and remove
{
   galselnames::Xclick_func(Nth,'R');
   if (clicked_file) zfree(clicked_file);
   clicked_file = 0;
   return;
}

void galselnames::Xclick_func(int Nth, ch LR)
{
   using namespace galselnames;

   ch             *imagefile;
   FTYPE          ftype;
   static int     pNth = -1;                                                     //  previously clicked file
   int            nn, incr;

   if (! zd_select_files) return;                                                //  should not happen
   if (Nth < 0) return;                                                          //  gallery gone ?

   imagefile = gallery(0,"getR",Nth);                                            //  get file at clicked position
   if (! imagefile) {
      pNth = -1;
      return;
   }

   ftype = image_file_type(imagefile);                                           //  must be image or RAW file
   if (ftype != IMAGE && ftype != RAW && ftype != VIDEO) {
      pNth = -1;
      return;
   }

   if (LR == 'R') {                                                              //  right click, unselect
      remove_file(imagefile);
      return;
   }

   if (! KBshiftkey)                                                             //  no shift key
   {
      pNth = Nth;                                                                //  possible start of range
      add_file(imagefile);                                                       //  add to list
      return;
   }

   if (KBshiftkey)                                                               //  shift key, end of range
   {
      if (pNth < 0) return;                                                      //  no start of range, ignore
      if (pNth > Nth) incr = -1;                                                 //  range is descending
      else incr = +1;                                                            //  ascending

      for (nn = pNth+incr; nn != Nth+incr; nn += incr)                           //  add all files from pNth to Nth
      {                                                                          //    excluding pNth (already added)
         imagefile = gallery(0,"get",nn);
         if (! imagefile) continue;
         ftype = image_file_type(imagefile);                                     //  only image and RAW files
         if (ftype != IMAGE && ftype != RAW && ftype != VIDEO) continue;
         add_file(imagefile);
      }
      pNth = -1;                                                                 //  no prior
      return;
   }

   return;
}


//  process mouse or key click in file list
//  set new current position and set thumbnail = clicked file

void galselnames::callbackfunc(GtkWidget *widget, int line, int posn, int KBkey)
{
   if (KBkey == GDK_KEY_F1) {                                                    //  key F1 pressed, show help
      showz_docfile(Mwin,"userguide",F1_help_topic);
      return;
   }

   if (! GSselect) return;                                                       //  no lines

   if (KBkey >= 0xfd00) {
      if (KBkey == GDK_KEY_Up) currline--;                                       //  KB arrow key navigation
      if (KBkey == GDK_KEY_Down) currline++;
      if (KBkey == GDK_KEY_Page_Up) currline -= 10;
      if (KBkey == GDK_KEY_Page_Down) currline += 10;
      if (KBkey == GDK_KEY_Home) currline = 0;
      if (KBkey == GDK_KEY_End) currline = GSselect - 1;
      if (currline < 0) currline = 0;
      if (currline > GSselect-1) currline = GSselect - 1;
      textwidget_highlight_line(Ftext,currline);                                 //  highlight line
      textwidget_scroll(Ftext,currline);
   }

   if (line >= 0) {                                                              //  line clicked
      textwidget_highlight_line(Ftext,line);                                     //  highlight
      currline = line;
      showthumb();
      return;
   }

   showthumb();
   return;
}


//  show thumbnail for file at current position in select list

void galselnames::showthumb()
{
   using namespace galselnames;

   GdkWindow      *gdkwin;
   ch             thumbfile[XFCC];
   PIXBUF         *thumbpxb = 0;
   GError         *gerror = 0;
   int            err;

   static draw_context_t   draw_context;
   cairo_t                 *cr;

   if (! GSselect) return;
   if (currline < 0) return;

   gdkwin = gtk_widget_get_window(drawarea);
   cr = draw_context_create(gdkwin,draw_context);

   imagefile = textwidget_line(Ftext,currline,1);                                //  get curr. image without \n

   if (imagefile) {
      err = image2thumbfile2(imagefile,thumbfile);                               //  use thumbnail file
      if (! err)
         thumbpxb = gdk_pixbuf_new_from_file_at_size(thumbfile,256,256,&gerror);
   }

   if (thumbpxb) {
      cairo_set_source_rgb(cr,1,1,1);                                            //  white background
      cairo_paint(cr);
      gdk_cairo_set_source_pixbuf(cr,thumbpxb,0,0);                              //  + thumbnail
      cairo_paint(cr);
      g_object_unref(thumbpxb);
   }
   else {
      cairo_set_source_rgb(cr,1,1,1);                                            //  white background only
      cairo_paint(cr);
   }

   draw_context_destroy(draw_context);
   return;
}


/********************************************************************************/

//  gallery menu, resizable thumbnail view

void m_thumbview(GtkWidget *, ch *menu)
{
   F1_help_topic = "thumb view";
   Plog(1,"m_thumbview \n");
   Fthumbview = 1;
   if (thumbsize < 256) thumbsize = 256;
   viewmode('G');
   gallery(0,"paint",-1);
   return;
}


//  gallery menu, thumbnail with basic metadata view

void m_metaview(GtkWidget *, ch *menu)
{
   F1_help_topic = "meta view";
   Plog(1,"m_metaview \n");
   Fthumbview = 2;
   viewmode('G');
   gallery(0,"paint",-1);
   return;
}


/********************************************************************************/

//  Show recently viewed image files.

void m_recentfiles(GtkWidget *, ch *menu)
{
   F1_help_topic = "recent files";

   Plog(1,"m_recentfiles \n");

   if (Findexvalid == 0) {
      zmessageACK(Mwin,"image index disabled");                                  //  no image index
      return;
   }

   navi::gallerytype = RECENT;                                                   //  gallery type = recent files
   gallery(recentfiles_file,"initF",0);                                          //  generate gallery of recent files
   gallery(0,"paint",0);
   viewmode('G');
   return;
}


//  add a new file to the list of recent files, first position
//  ( < 1 millisec. typical at 1000 file limit)

void add_recent_file(ch *newfile)
{
   zlist_t  *ZLfiles;
   ch       *pp;
   int      ii;

   ZLfiles = zlist_from_file(recentfiles_file);
   if (! ZLfiles) ZLfiles = zlist_new(0);
   ii = zlist_find(ZLfiles,newfile,0);                                           //  if file already present, remove it
   if (ii >= 0) zlist_remove(ZLfiles,ii);
   zlist_insert(ZLfiles,newfile,0);                                              //  insert in 1st position
   zlist_clear(ZLfiles,1000);                                                    //  truncate to 1000 files                24.30
   zlist_to_file(ZLfiles,recentfiles_file);
   zlist_free(ZLfiles);
   
   pp = strrchr(newfile,'/');                                                    //  update recent folders list            24.70
   if (pp) *pp = 0;
   add_recent_folder(newfile);
   if (pp) *pp = '/';

   return;
}


/********************************************************************************/

//  Report the newest or most recently modified image files,
//  based on metadata photo date or file mod date.

namespace newfiles
{
   struct newfile_t  {                                                           //  new file record
      ch          *file;                                                         //  image file
      ch          fdate[16];                                                     //  date-time, yyyymmddhhmmss
   };
}


//  menu function

void m_newfiles(GtkWidget *, ch *menu)
{
   using namespace newfiles;

   int newfile_comp(ch *rec1, ch *rec2);

   ch   *mess = "Use metadata photo date or \n file modification date?";

   int         ii, jj, cc, sort, Nxrec;
   xxrec_t     *xxrec;
   FILE        *fid;
   newfile_t    *newfile = 0;

   F1_help_topic = "newest files";

   Plog(1,"m_newfiles \n");

   if (Findexvalid == 0) {
      zmessageACK(Mwin,"image index disabled");                                  //  no image index
      return;
   }

   if (Nxxrec == 0) {
      zmessageACK(Mwin,"no files found");
      return;
   }

   cc = Nxxrec * sizeof(newfile_t);                                              //  allocate memory
   newfile = (newfile_t *) zmalloc(cc,"newfiles");

   if (menu && strmatch(menu,"metadata")) sort = 1;
   else if (menu && strmatch(menu,"file")) sort = 2;
   else sort = zdialog_choose(Mwin,"mouse",mess,"metadata","File",null);
   if (sort < 1) return;                                                         //  cancel

   for (ii = jj = 0; ii < Nxxrec; ii++)                                          //  loop image index table
   {
      xxrec = xxrec_tab[ii];
      newfile[jj].file = xxrec->file;                                            //  image file

      if (sort == 1) {
         if (strmatch(xxrec->pdate,"null")) continue;                            //  use metadata photo date
         strncpy0(newfile[jj].fdate,xxrec->pdate,15);                            //  ignore images without photo date
      }
      else strncpy0(newfile[jj].fdate,xxrec->fdate,15);                          //  else use file mod date

      jj++;                                                                      //  selected files
   }

   Nxrec = jj;                                                                   //  final count

   if (Nxrec > 1)                                                                //  sort index recs. by file date
      HeapSort((ch *) newfile, sizeof(newfile_t), Nxrec, newfile_comp);

   fid = fopen(searchresults_file,"w");                                          //  open output file
   if (! fid) {
      zmessageACK(Mwin,"file error: %s",strerror(errno));
      goto cleanup;
   }

   for (ii = 0; ii < 1000 && ii < Nxrec; ii++)                                   //  output newest 1000 image files
      fprintf(fid,"%s\n",newfile[ii].file);

   fclose(fid);

cleanup:

   zfree(newfile);                                                               //  free memory
   navi::gallerytype = NEWEST;                                                   //  newest files
   gallery(searchresults_file,"initF",0);                                        //  generate gallery of files
   gallery(0,"paint",0);
   viewmode('G');

   return;
}


//  Compare 2 newfile records by file date-time
//  return <0 =0 >0  for  rec2 < = > rec1 (descending sequence)

using namespace newfiles;

int newfile_comp(ch *rec1, ch *rec2)
{
   ch *date1 = ((newfile_t *) rec1)->fdate;
   ch *date2 = ((newfile_t *) rec2)->fdate;
   return strcmp(date2,date1);
}


/********************************************************************************/

//  menu function
//  dialog to choose gallery sort order and sort the gallery

void m_gallery_sort(GtkWidget *, ch *menu)
{
   zdialog     *zd;
   int         zstat, nn;
   ch          albumfile[200], *pp, *pp2;

   ch     *resetmess = " Reset all galleries\n to file name ascending";

   F1_help_topic = "gallery sort";

   Plog(1,"m_gallery_sort \n");

/***
          _________________________________
         |         Gallery Sort            |
         |                                 |
         |  (o) File Name                  |
         |  (o) File Name/Number           |
         |  (o) File Mod Date/Time         |
         |  (o) Photo Date/Time (metadata) |
         |  (o) File Size (bytes)          |
         |  (o) Image Size (pixels)        |
         |  (o) Metadata from Search       |
         |  (o) ascending  (o) descending  |
         |                                 |
         |  [x] reset all galleries        |
         |      to file name ascending     |
         |                                 |
         |                     [Apply] [X] |
         |_________________________________|

***/

   zd = zdialog_new("Gallery Sort",Mwin,"Apply"," X ",null);                     //  user dialog
   zdialog_add_widget(zd,"hbox","hb1","dialog");
   zdialog_add_widget(zd,"label","space","hb1",0,"space=2");
   zdialog_add_widget(zd,"vbox","vb1","hb1");
   zdialog_add_widget(zd,"radio","filename","vb1","File Name");
   zdialog_add_widget(zd,"radio","filenumber","vb1","File Name/Number");
   zdialog_add_widget(zd,"radio","filedate","vb1","File Mod Date/Time");
   zdialog_add_widget(zd,"radio","photodate","vb1","Photo Date/Time (metadata)");
   zdialog_add_widget(zd,"radio","filesize","vb1","File Size (bytes)");
   zdialog_add_widget(zd,"radio","pixelsize","vb1","Image Size (pixels)");
   zdialog_add_widget(zd,"radio","metadata","vb1","Metadata from Search Function");
   zdialog_add_widget(zd,"hbox","hb2","dialog",0,"space=5");
   zdialog_add_widget(zd,"radio","ascending","hb2","ascending","space=4");
   zdialog_add_widget(zd,"radio","descending","hb2","descending","space=2");
   zdialog_add_widget(zd,"hbox","hbreset","dialog",0,"space=5");
   zdialog_add_widget(zd,"check","reset","hbreset",resetmess,"space=4");

   zdialog_add_ttip(zd,"metadata","metadata from Search Function, metadata report");

   zdialog_stuff(zd,"filename",0);                                               //  all buttons off
   zdialog_stuff(zd,"filenumber",0);
   zdialog_stuff(zd,"filedate",0);                                               //  GTK radio buttons not reliable
   zdialog_stuff(zd,"photodate",0);                                              //  (vbox works, hbox does not)
   zdialog_stuff(zd,"filesize",0);
   zdialog_stuff(zd,"pixelsize",0);
   zdialog_stuff(zd,"metadata",0);
   zdialog_stuff(zd,"metadata",0);
   zdialog_stuff(zd,"descending",0);
   zdialog_stuff(zd,"reset",0);

   if (gallerysort == FNAME || gallerysort == SNONE)
      zdialog_stuff(zd,"filename",1);

   if (gallerysort == FNUMBER)
      zdialog_stuff(zd,"filenumber",1);

   if (gallerysort == FDATE)
      zdialog_stuff(zd,"filedate",1);

   if (gallerysort == PDATE)
      zdialog_stuff(zd,"photodate",1);

   if (gallerysort == FSIZE)
      zdialog_stuff(zd,"filesize",1);

   if (gallerysort == PSIZE)
      zdialog_stuff(zd,"pixelsize",1);

   if (gallerysort == MDATA)
      zdialog_stuff(zd,"metadata",1);

   if (galleryseq == ASCEND || galleryseq == QNONE)
      zdialog_stuff(zd,"ascending",1);

   if (galleryseq == DESCEND)
      zdialog_stuff(zd,"descending",1);

   zdialog_set_modal(zd);
   zdialog_run(zd,0,"mouse");                                                    //  run dialog, wait for completion
   zstat = zdialog_wait(zd);

   if (zstat != 1) {
      zdialog_free(zd);
      return;
   }

   zdialog_fetch(zd,"filename",nn);                                              //  get user sort type
   if (nn) gallerysort = FNAME;
   zdialog_fetch(zd,"filenumber",nn);
   if (nn) gallerysort = FNUMBER;
   zdialog_fetch(zd,"filedate",nn);
   if (nn) gallerysort = FDATE;
   zdialog_fetch(zd,"photodate",nn);
   if (nn) gallerysort = PDATE;
   zdialog_fetch(zd,"filesize",nn);
   if (nn) gallerysort = FSIZE;
   zdialog_fetch(zd,"pixelsize",nn);
   if (nn) gallerysort = PSIZE;
   zdialog_fetch(zd,"metadata",nn);
   if (nn) gallerysort = MDATA;

   zdialog_fetch(zd,"ascending",nn);                                             //  get ascending/descending
   if (nn) galleryseq = ASCEND;
   else galleryseq = DESCEND;

   zdialog_fetch(zd,"reset",nn);                                                 //  reset all gallery sort memory
   if (nn) {                                                                     //  (revert to file name ascending)
      gallery_memory("reset");
      gallerysort = FNAME;
      galleryseq = ASCEND;
   }

   zdialog_free(zd);

   gallery(0,"sort",-1);                                                         //  sort the gallery
   gallery(0,"paint",0);                                                         //  paint, position = 0

   if (gallerytype == ALBUM)                                                     //  an album was sorted
   {
      pp = strrchr(galleryname,'/');                                             //  get album name
      if (pp) pp++;
      else pp = galleryname;
      pp2 = strstr(pp,"-sorted");                                                //  append "-sorted"
      if (pp2) *pp2 = 0;                                                         //  avoid "-sorted-sorted"
      snprintf(albumfile,200,"%s/%s-sorted",albums_folder,pp);
      album_create_from_gallery(albumfile);                                      //  new album, oldname-sorted
      album_show(albumfile);
   }

   return;
}


/********************************************************************************/

//  screen the current gallery for matching files
//  output matching files to album "gallery_screen"

namespace gallery_screen_names
{
   int      Fall, Forg, Fver, Flast, Frating, Ftags, Ftext;
   int      lorat, hirat;
   ch       tags[100], text[100];
}


//  menu function

void m_gallery_screen(GtkWidget *, ch *menu)                                     //  revamped 24.60
{
   using namespace gallery_screen_names;

   zdialog  *zd;
   int      zstat, ii, kk1, kk2, Nfiles, Fkeep;
   ch       albumfile[AFCC];
   ch       *file, *pp1, *pp2;
   FTYPE    ftype;
   FILE     *fid;
   xxrec_t  *xxrec;
   int      rating;

   int gallery_screen_dialog_event(zdialog *zd, ch *event);

   F1_help_topic = "gallery screen";

   Plog(1,"m_gallery_screen \n");

   if (Gfiles < 1) {
      zmessageACK(Mwin,"gallery is empty");
      return;
   }

/***
       _________________________________________________________________
      |                     Gallery Screen                              |
      |                                                                 |
      |  [_] all files  [_] originals  [_] versions  [_] last versions  |
      |                                                                 |
      |  [_] rating range [__] [__]                                     |     inlcude if rating within range
      |  [_] tag names [______________________________________________] |     include if any tag present (comma separated)
      |  [_] filename text [__________________________________________] |     include if any text present (comma separated) 
      |                                                                 |
      |                                                        [OK] [X] |
      |_________________________________________________________________|

***/

   zd = zdialog_new("Gallery Screen",Mwin,"OK"," X ",null);

   zdialog_add_widget(zd,"hbox","hbfiles","dialog",0,"space=3");
   zdialog_add_widget(zd,"check","ckall","hbfiles","all files");
   zdialog_add_widget(zd,"check","ckorg","hbfiles","originals");
   zdialog_add_widget(zd,"check","ckver","hbfiles","versions");
   zdialog_add_widget(zd,"check","cklast","hbfiles","last versions");

   zdialog_add_widget(zd,"hbox","hbrat","dialog");
   zdialog_add_widget(zd,"check","ckrat","hbrat","rating range");
   zdialog_add_widget(zd,"zspin","lorat","hbrat","0|5|1|0","size=3|space=5");
   zdialog_add_widget(zd,"zspin","hirat","hbrat","0|5|1|5","size=3");

   zdialog_add_widget(zd,"hbox","hbtags","dialog");
   zdialog_add_widget(zd,"check","cktags","hbtags","tag names");
   zdialog_add_widget(zd,"zentry","tags","hbtags",0,"space=5|expand");

   zdialog_add_widget(zd,"hbox","hbtext","dialog");
   zdialog_add_widget(zd,"check","cktext","hbtext","filename text");
   zdialog_add_widget(zd,"zentry","text","hbtext",0,"space=5|expand");

   zdialog_add_ttip(zd,"ckall","include all files");
   zdialog_add_ttip(zd,"ckorg","include original files");
   zdialog_add_ttip(zd,"ckver","include edited versions");
   zdialog_add_ttip(zd,"cklast","include last versions only");
   zdialog_add_ttip(zd,"ckrat","get images within rating range");
   zdialog_add_ttip(zd,"cktags","get images with matching tags (comma separated)");
   zdialog_add_ttip(zd,"cktext","get images having text in filename (comma separated)");

   zdialog_load_inputs(zd);
   zdialog_resize(zd,400,0);
   zdialog_run(zd,gallery_screen_dialog_event,"mouse");                          //  start dialog
   zdialog_send_event(zd,"init");
   zstat = zdialog_wait(zd);                                                     //  wait

   if (zstat != 1) {                                                             //  cancel
      zdialog_free(zd);
      return;
   }

   zdialog_fetch(zd,"ckall",Fall);                                               //  get all inputs
   zdialog_fetch(zd,"ckorg",Forg);  
   zdialog_fetch(zd,"ckver",Fver);  
   zdialog_fetch(zd,"cklast",Flast);
   zdialog_fetch(zd,"ckrat",Frating); 
   zdialog_fetch(zd,"cktags",Ftags);
   zdialog_fetch(zd,"cktext",Ftext);
   zdialog_fetch(zd,"lorat",lorat);
   zdialog_fetch(zd,"hirat",hirat);
   zdialog_fetch(zd,"tags",tags,100);
   zdialog_fetch(zd,"text",text,100);

   zdialog_free(zd);

   snprintf(albumfile,AFCC,"%s/gallery_screen",albums_folder);                   //  output to album "gallery_screen"

   fid = fopen(albumfile,"w");                                                   //  open/write album file
   if (! fid) {
      zmessageACK(Mwin,strerror(errno));
      return;
   }

   Nfiles = 0;

   for (ii = 0; ii < Gfiles; ii++)                                               //  loop files in current gallery
   {
      file = gallery(0,"getR",ii);
      if (! file) break;
      ftype = image_file_type(file);                                             //  must be image type file
      if (ftype != IMAGE && ftype != RAW && ftype != VIDEO) continue;
      
      Fkeep = 0;
      
      if (Fall) Fkeep = 1;                                                       //  keep all files
      
      if (! Fkeep && Forg) {
         pp1 = strrchr(file,'/');                                                //  /.../filename.vNN.jpg
         if (pp1) pp1 = strrchr(pp1,'.');                                        //                   |
         if (pp1 && ! strmatchN(pp1-4,".v",2)) Fkeep = 1;                        //                   pp1
      }

      if (! Fkeep && Fver) {
         pp1 = strrchr(file,'/');                                                //  /.../filename.vNN.jpg
         if (pp1) pp1 = strrchr(pp1,'.');                                        //                   |
         if (pp1 && strmatchN(pp1-4,".v",2)) Fkeep = 1;                          //                   pp1
      }
      
      if (! Fkeep && Flast) {
         pp1 = file_newest_version(file);
         if (strmatch(pp1,file)) Fkeep = 1;
      }
      
      if (! Fkeep) continue;
      
      if (Frating) {
         xxrec = get_xxrec(file);                                                //  get image rating
         if (! xxrec) {
            Plog(1,"file not indexed: %s \n",file);
            continue;
         }
         rating = xxrec->rating[0] - '0';                                        //  numeric 0-5
         if (rating < lorat || rating > hirat) continue;                         //  test within limits
      }

      if (Ftags) {
         xxrec = get_xxrec(file);                                                //  get image tags
         if (! xxrec) {
            Plog(1,"file not indexed: %s \n",file);
            continue;
         }

         for (kk1 = 1; ; kk1++)                                                  //  loop tags to search
         {
            pp1 = substring(tags,',',kk1);
            if (! pp1) goto nomatch;                                             //  no match found

            for (kk2 = 1; ; kk2++) {                                             //  loop image tags
               pp2 = substring(xxrec->tags,',',kk2);
               if (! pp2) break;
               if (strmatchcase(pp1,pp2)) goto tagmatch;
            }
         }
      }
      tagmatch:

      if (Ftext) {
         for (kk1 = 1; ; kk1++) {                                                //  get text strings to match
            pp1 = substring(text,',',kk1);
            if (! pp1) goto nomatch;                                             //  no match found
            pp2 = strrchr(file,'/');
            if (strcasestr(pp2,pp1)) goto textmatch;
         }
      }
      textmatch:

      fprintf(fid,"%s\n",file);                                                  //  screened output file

      Nfiles++;
      if (Nfiles == maxalbumfiles) {
         zmessageACK(Mwin,"max. album files reached, results truncated");
         break;
      }
      
      nomatch: continue;                                                         
   }


   fclose(fid);
   
   if (Nfiles) {
      zstrcopy(curr_album,albumfile,"albums");                                   //  make current album
      zmessage_post(Mwin,"20/20",3,"new album created");
      album_show();
   }
   else {
      zmessageACK(Mwin,"no matching files found");
      remove(albumfile);
      return;
   }

   return;
}


//  dialog event and completion function

int gallery_screen_dialog_event(zdialog *zd, ch *event)
{
   using namespace gallery_screen_names;
   
   if (strmatch(event,"ckall")) {                                                //  if all, clear org, ver, last
      zdialog_fetch(zd,"ckall",Fall);
      if (Fall) Forg = Fver = Flast = 0;
   }

   if (strmatch(event,"ckorg")) {                                                //  if org, clear all
      zdialog_fetch(zd,"ckorg",Forg);
      if (Forg) Fall = 0;
   }

   if (strmatch(event,"ckver")) {                                                //  if ver, clear all, last
      zdialog_fetch(zd,"ckver",Fver);
      if (Fver) Fall = Flast = 0;
   }
   
   if (strmatch(event,"cklast")) {                                               //  if last, clear all, ver
      zdialog_fetch(zd,"cklast",Flast);
      if (Flast) Fall = Fver = 0;
   }

   if (Forg && Fver) {                                                           //  if org and ver, clear both
      Fall = 1;                                                                  //    and set all
      Forg = Fver = 0;
   }
   
   if (! Forg && ! Fver && ! Flast) Fall = 1;                                    //  default
   
   zdialog_stuff(zd,"ckall",Fall);                                               //  update dialog 
   zdialog_stuff(zd,"ckorg",Forg);
   zdialog_stuff(zd,"ckver",Fver);
   zdialog_stuff(zd,"cklast",Flast);
   zdialog_fetch(zd,"ckrat",Frating);
   zdialog_fetch(zd,"cktags",Ftags);
   zdialog_fetch(zd,"cktext",Ftext);
   zdialog_fetch(zd,"lorat",lorat);
   zdialog_fetch(zd,"hirat",hirat);
   zdialog_fetch(zd,"tags",tags,100);
   zdialog_fetch(zd,"text",text,100);

   return 1;
}


/********************************************************************************/

//  set the gallery from the current image file folder

void m_current_folder(GtkWidget*, ch *menu)
{
   F1_help_topic = "current folder";
   Plog(1,"m_current_folder \n");

   if (! curr_file) {
      zmessageACK(Mwin,"no current file");
      return;
   }

   gallery(curr_file,"init",0);                                                  //  new gallery
   gallery(curr_file,"paint",0);                                                 //  position at curr. file
   curr_file_posn = file_position(curr_file,0);                                  //  file position in gallery list
   viewmode('G');
   return;
}


/********************************************************************************/

//  set the gallery from a recently used file folder

void m_recent_folders(GtkWidget*, ch *menu)                                      //  24.60
{
   void  recent_folders_choose(GtkWidget *, ch *);

   int         nf = 0;
   ch          *folder;

   F1_help_topic = "recent folders";
   Plog(1,"m_recent_folders \n");
   
   if (! ZLrecent_folders) ZLrecent_folders = zlist_from_file(recent_folders_file);
   if (ZLrecent_folders) nf = zlist_count(ZLrecent_folders);
   if (! nf) {
      zmessageACK(Mwin,"no saved folders");
      return;
   }

   folder = zlist_choose(ZLrecent_folders);
   if (! folder) return;

   gallery(folder,"init",0);                                                     //  set new gallery
   gallery(0,"sort",-2);                                                         //  last sort and position
   gallery(0,"paint",-1);                                                        //  show
   viewmode('G');

   return;
}


//  add a new folder to the list of recent folders, first position
//  execution time is under 1 milllisecond 

void add_recent_folder(ch *foldername)                                           //  24.60
{
   int      ii;

   if (! ZLrecent_folders) ZLrecent_folders = zlist_from_file(recent_folders_file);
   if (! ZLrecent_folders) ZLrecent_folders = zlist_new(0);

   ii = zlist_find(ZLrecent_folders,foldername,0);
   if (ii >= 0) zlist_remove(ZLrecent_folders,ii);
   zlist_insert(ZLrecent_folders,foldername,0); 
   zlist_clear(ZLrecent_folders,10);
   zlist_to_file(ZLrecent_folders,recent_folders_file);
   
   return;
}


/********************************************************************************/

//  generate a clickable list of all image folders
//  show gallery for any folder clicked

namespace folder_tree
{
   #define  maxdirs 10000

   int         Nlines = 0, Fall = 0;
   int         currline = 0;
   zdialog     *zdpop;

   typedef struct {
      ch       *name;                                 //  /dir1/dir2/.../dirN       folder name
      int      Nsubs;                                 //  subfolder count           0-N
      int8     lev;                                   //  folder level              0-N, top/sub/sub ...
      int8     exp;                                   //  folder status             0/1 = collapsed/expanded
      int16    line;                                  //  textwidget line           -1 = not displayed
   }  dlist_t;

   dlist_t  *dlist;
   int      drecl = sizeof(dlist_t);

   int  compfunc(ch *rec1, ch *rec2);
   void callbackfunc(GtkWidget *, int line, int pos, int kbkey);
   void writetext();
}


//  menu function

void m_folder_tree(GtkWidget *, ch *)
{
   using namespace folder_tree;

   int         ii, jj, cc, pcc, NF, err;
   ch          *dir, *pdir, **Flist;

   F1_help_topic = "folder tree";

   Plog(1,"m_folder_tree \n");

/***
          ________________________________
         |       Folder Tree              |
         |  ____________________________  |
         | |                            | |
         | | [+] topdir1                | |
         | | [-] topdir2                | |
         | |         subdir1            | |
         | |     [+] subdir2            | |
         | |         subdir3            | |
         | | [+] topdir3                | |
         | |         ...                | |
         | |____________________________| |
         |                                |
         |                           [OK] |
         |________________________________|

***/

   if (dlist) goto report;                                                       //  already done

   cc = drecl * maxdirs;
   dlist = (dlist_t *) zmalloc(cc,"folder_tree");                                //  memory for folder list
   Fall = 0;

   for (ii = 0; ii < Ntopfolders; ii++)                                          //  loop all top image folders
   {
      dlist[Fall].name = topfolders[ii];
      Fall++;
      if (Fall == maxdirs) break;

      err = find_imagefiles(topfolders[ii],8+16,Flist,NF);                       //  folders, all levels
      if (err) {
         zmessageACK(Mwin,strerror(errno));
         continue;
      }

      for (jj = 0; jj < NF; jj++)                                                //  add to folders list
      {
         dlist[Fall].name = zstrdup(Flist[jj],"folder_tree");
         Fall++;
         if (Fall == maxdirs) break;
      }

      if (NF) zfree(Flist);
      if (Fall == maxdirs) break;
   }

   if (Fall > 1)
      HeapSort((ch *) dlist,drecl,Fall,compfunc);                                //  sort alphabetically

   for (ii = 0; ii < Fall; ii++)                                                 //  loop all folders
   {
      dir = dlist[ii].name;                                                      //  this folder name
      for (jj = ii-1; jj >= 0; jj--) {                                           //  search backwards for parent
         pdir = dlist[jj].name;                                                  //  previous folder name
         pcc = strlen(pdir);
         if (strmatchN(dir,pdir,pcc) && dir[pcc] == '/') break;                  //  this dir = prev dir + /...
      }
      if (jj >= 0) {                                                             //  parent found
         dlist[ii].lev = dlist[jj].lev + 1;                                      //  level = parent level + 1
         dlist[jj].Nsubs++;                                                      //  add parent subdir count
      }
      else dlist[ii].lev = 0;                                                    //  no parent, level = 0
   }

   for (ii = 0; ii < Fall; ii++)                                                 //  loop all folders
      dlist[ii].exp = 0;                                                         //  expand = no

report:

   zdpop = popup_report_open("Folders",Mwin,300,400,0,0,callbackfunc,"X",0);     //  open report window

   writetext();                                                                  //  write top folders to window
   currline = 0;                                                                 //  first entry

   return;
}


//  sort compare function

int folder_tree::compfunc(ch *rec1, ch *rec2)
{
   dlist_t   *dir1 = (dlist_t *) rec1;
   dlist_t   *dir2 = (dlist_t *) rec2;
   int         nn;

   nn = strcasecmp(dir1->name,dir2->name);
   if (nn) return nn;
   nn = strcmp(dir1->name,dir2->name);
   return nn;
}


//  folder list mouse function

void folder_tree::callbackfunc(GtkWidget *textwidget, int line, int pos, int kbkey)
{
   using namespace folder_tree;

   int         ii;
   ch          *pline, *pp;
   static int  Fbusy = 0;

   if (Fbusy++) return;                                                          //  stop re-entry

   if (line < 0)                                                                 //  KB key
   {
      if (kbkey == GDK_KEY_F1) {                                                 //  key F1 pressed, show help
         showz_docfile(Mwin,"userguide",F1_help_topic);
         goto returnx;
      }

      line = currline;
      if (kbkey == GDK_KEY_Up) line--;                                           //  KB arrow key navigation
      if (kbkey == GDK_KEY_Down) line++;
      if (line < 0) line = 0;
      if (line > Nlines-1) line = Nlines - 1;
      zmainsleep(0.1);                                                           //  restrain to 10/sec.
   }

   pline = textwidget_line(textwidget,line,1);                                   //  textwidget line: ... [x] dirname
   if (! pline || ! *pline) goto returnx;
   textwidget_highlight_line(textwidget,line);
   textwidget_scroll(textwidget,line);
   currline = line;

   for (ii = 0; ii < Fall; ii++)                                                 //  find dlist[] rec corresponding
      if (line == dlist[ii].line) break;                                         //    to textwidget line clicked
   if (ii == Fall) goto returnx;

   if (kbkey == GDK_KEY_Return) {                                                //  Enter key, look for [+] or [-]
      pp = strchr(pline,'[');
      if (pp) pos = pp - pline;                                                  //  simulate click on [*]
   }

   if (pos > 0)                                                                  //  clicked position
   {
      pp = strchr(pline,'[');
      if (pp) {                                                                  //  [+] or [-] button present
         if (pos < (pp-pline)) goto returnx;                                     //  click position before button
         if (pos >= (pp-pline) && pos <= (pp+2-pline)) {                         //  on the button
            if (pp[1] == '+') dlist[ii].exp = 1;                                 //  set expand flag
            if (pp[1] == '-') dlist[ii].exp = 0;
            writetext();                                                         //  refresh textwidget
            textwidget_highlight_line(textwidget,line);
            textwidget_scroll(textwidget,line);
            goto returnx;
         }
      }
   }

   viewmode('G');
   gallery(dlist[ii].name,"init",0);                                             //  folder name clicked
   gallery(0,"sort",-2);                                                         //  recall sort and position
   gallery(0,"paint",-1);                                                        //  show gallery

returnx:
   Fbusy = 0;
   return;
}


//  write all visible folders to text window

void folder_tree::writetext()
{
   using namespace folder_tree;

   int      ii = 0, jj, line = 0;
   ch       *pp, indent[100];
   ch       *expbutt;

   memset(indent,' ',100);

   popup_report_clear(zdpop);

   while (ii < Fall)                                                             //  loop all folders
   {
      jj = dlist[ii].lev * 4;                                                    //  indent 4 blanks per folder level
      if (jj > 99) jj = 99;
      indent[jj] = 0;
      if (dlist[ii].Nsubs == 0) expbutt = "   ";                                 //  no subdirs, no expand button
      else if (dlist[ii].exp) expbutt = "[-]";                                   //  prepare [+] or [-]
      else expbutt = "[+]";
      pp = strrchr(dlist[ii].name,'/');
      if (! pp) continue;
      popup_report_write2(zdpop,0,"%s %s %s \n",indent,expbutt,pp+1);            //  ... [x] dirname
      indent[jj] = ' ';
      dlist[ii].line = line;                                                     //  text line for this folder
      line++;

      if (dlist[ii].exp) {                                                       //  if folder expanded, continue
         ii++;
         continue;
      }

      for (jj = ii + 1; jj < Fall; jj++) {                                       //  not expanded, find next folder
         if (dlist[jj].lev <= dlist[ii].lev) break;                              //    at same or lower level
         dlist[jj].line = -1;                                                    //  no text line for skipped folders
      }
      ii = jj;
   }

   Nlines = line;
   return;
}


/********************************************************************************/

//  menu function to pre-select files for feeding album, batch and script functions

void m_select_files(GtkWidget *, ch *)
{
   ch       albumfile[200];
   FILE     *fid;
   int      ii;

   F1_help_topic = "select image files";

   Plog(1,"m_select_files \n");

   snprintf(albumfile,200,"%s/selected_files",albums_folder);                    //  "selected files" album

   select_files(0,1);                                                            //  curr file = last selection            24.20
   if (SFcount == 0) {
      remove(albumfile);                                                         //  nothing selected
      return;
   }

   fid = fopen(albumfile,"w");                                                   //  create "selected files" album
   if (! fid) {
      zmessageACK(Mwin,strerror(errno));
      return;
   }

   for (ii = 0; ii < SFcount; ii++)                                              //  fill with selected files
      fprintf(fid,"%s\n",SelFiles[ii]);

   fclose(fid);
   return;
}


/********************************************************************************/

//  rename the current folder

void m_rename_folder(GtkWidget *, ch *menu)
{
   int  rename_folder_dialog_event(zdialog *zd, ch *event);

   zdialog  *zd;
   int      zstat, err;
   ch       oldname[XFCC], newname[XFCC];

   F1_help_topic = "rename folder";

   Plog(1,"m_rename_folder \n");

   if (! galleryname) {
      zmessageACK(Mwin,"no current gallery");
      return;
   }

   if (gallerytype != FOLDER) {
      zmessageACK(Mwin,"current gallery is not a folder (file directory)");
      return;
   }

   strcpy(oldname,galleryname);
   strcpy(newname,galleryname);

/***
       _________________________________________
      |             Rename Folder               |
      |                                         |
      |  current folder [_____________________] |
      |      new name   [_____________________] |
      |                                         |
      |                             [Apply] [X] |
      |_________________________________________|

***/

   zd = zdialog_new("Rename Folder",Mwin,"Apply"," X ",null);
   zdialog_add_widget(zd,"hbox","hbcf","dialog",0,"space=3");
   zdialog_add_widget(zd,"label","labcf","hbcf","current folder:","space=3");
   zdialog_add_widget(zd,"label","oldname","hbcf",0,"space=3");
   zdialog_add_widget(zd,"hbox","hbnn","dialog",0,"space=3");
   zdialog_add_widget(zd,"label","labnn","hbnn","new name","space=3");
   zdialog_add_widget(zd,"zentry","newname","hbnn",0,"space=3|expand");

   zdialog_stuff(zd,"oldname",oldname);                                          //  stuff current folder name
   zdialog_stuff(zd,"newname",newname);                                          //  also basis for new name

   zdialog_run(zd,rename_folder_dialog_event);

   zstat = zdialog_wait(zd);

   zdialog_fetch(zd,"newname",newname,XFCC);                                     //  get new folder name
   zdialog_free(zd);
   if (zstat != 1) return;

   if (strmatch(oldname,newname)) {
      zmessageACK(Mwin,"name not changed");
      return;
   }

   err = rename(oldname,newname);                                                //  do folder rename
   if (err) {
      zmessageACK(Mwin,"rename failed: %s",strerror(errno));
      return;
   }

   zmessageACK(Mwin,"folder renamed: \n %s",newname);

   zmessage_post(Mwin,"parent",3,"updating index and albums");

   m_quick_index(0,0);                                                           //  re-index after folder rename

   album_folder_rename(oldname,newname);                                         //  update filenames in album files

   gallery(newname,"init",0);                                                    //  refresh gallery
   gallery(0,"paint",-1);
   return;
}


//  zdialog event and completion function

int rename_folder_dialog_event(zdialog *zd, ch *event)
{
   if (zd->zstat) zdialog_destroy(zd);
   return 1;
}


/********************************************************************************/

//  create a new subfolder under the current folder

void m_add_subfolder(GtkWidget *, ch *menu)
{
   int  add_subfolder_dialog_event(zdialog *zd, ch *event);

   zdialog  *zd;
   int      zstat, err;
   ch       pathname[XFCC], subfolder[100];

   F1_help_topic = "add subfolder";

   Plog(1,"m_add_subfolder \n");

   if (! galleryname) {
      zmessageACK(Mwin,"no current gallery");
      return;
   }

   if (gallerytype != FOLDER) {
      zmessageACK(Mwin,"current gallery is not a folder (file directory)");
      return;
   }

/***
       _________________________________________
      |             Add Subfolder               |
      |                                         |
      |  current folder [_____________________] |
      |   new subfolder [_____________________] |
      |                                         |
      |                            [Create] [X] |
      |_________________________________________|

***/

   zd = zdialog_new("Add Subfolder",Mwin,"Create"," X ",null);
   zdialog_add_widget(zd,"hbox","hbcf","dialog",0,"space=3");
   zdialog_add_widget(zd,"label","labcf","hbcf","current folder:","space=3");
   zdialog_add_widget(zd,"label","currfol","hbcf",0,"space=3");
   zdialog_add_widget(zd,"hbox","hbnsf","dialog",0,"space=3");
   zdialog_add_widget(zd,"label","labnsf","hbnsf","new subfolder","space=3");
   zdialog_add_widget(zd,"zentry","subfolder","hbnsf",0,"space=3|expand");

   zdialog_stuff(zd,"currfol",galleryname);

   zdialog_run(zd,add_subfolder_dialog_event);
   zstat = zdialog_wait(zd);

   zdialog_fetch(zd,"subfolder",subfolder,100);
   zdialog_free(zd);
   if (zstat != 1) return;

   if (*subfolder < ' ' || strchr(subfolder,'/')) {
      zmessageACK(Mwin,"subfolder name is invalid");
      return;
   }

   *pathname = 0;
   strncatv(pathname,XFCC,galleryname,"/",subfolder,0);

   err = mkdir(pathname,0764);
   if (err) {
      zmessageACK(Mwin,"create folder failed: %s",strerror(errno));
      return;
   }

   gallery(galleryname,"init",0);
   gallery(0,"paint",-1);
   zmessageACK(Mwin,"new subfolder created: \n %s",pathname);

   return;
}


//  zdialog event and completion function

int add_subfolder_dialog_event(zdialog *zd, ch *event)
{
   if (zd->zstat) zdialog_destroy(zd);
   return 1;
}


/********************************************************************************/

namespace bookmarknames
{
   #define     maxbmks 50
   ch          *bookmarks[maxbmks];                                              //  bookmark names and files
   int         Nbmks;                                                            //  count of entries
   int         bmkposn;                                                          //  current entry, 0-last
   zdialog     *zd_bookmark;
   GtkWidget   *textwidget;
}

void bookmarks_load();
void bookmarks_refresh();


//  select a bookmark and jump gallery to selected bookmark thumbnail

void m_bookmarks(GtkWidget *, ch *)
{
   using namespace bookmarknames;

   int  bookmarks_dialog_event(zdialog *zd, ch *event);
   void bookmarks_callbackfunc(GtkWidget *, int line, int pos, int kbkey);

   zdialog     *zd;

/***
          _______________________________________________
         |                Bookmarks                      |
         |-----------------------------------------------|
         | bookmarkname1      /topdir/.../filename1.jpg  |
         | bookmarkname2      /topdir/.../filename2.jpg  |
         | bookmarkname3      /topdir/.../filename3.jpg  |
         | bookmarkname4      /topdir/.../filename4.jpg  |
         | bookmarkname5      /topdir/.../filename5.jpg  |
         | bookmarkname6      /topdir/.../filename6.jpg  |
         |                                               |
         |                          [Edit Bookmarks] [X] |
         |_______________________________________________|
***/

   F1_help_topic = "bookmarks";

   Plog(1,"m_bookmarks \n");

   if (zd_edit_bookmarks) return;                                                //  already busy
   if (zd_bookmark) return;

   zd = zdialog_new("Bookmarks",Mwin,"Edit Bookmarks"," X ",null);
   zd_bookmark = zd;
   zdialog_add_widget(zd,"frame","frame","dialog",0,"space=5|expand");
   zdialog_add_widget(zd,"scrwin","scrwin","frame");
   zdialog_add_widget(zd,"text","bmklist","scrwin");

   textwidget = zdialog_gtkwidget(zd,"bmklist");                                 //  set mouse/KB event function
   textwidget_set_eventfunc(textwidget,bookmarks_callbackfunc);

   bookmarks_load();                                                             //  get bookmarks from bookmarks file
   bookmarks_refresh();                                                          //  update bookmarks list in dialog

   zdialog_resize(zd,400,300);
   zdialog_run(zd,bookmarks_dialog_event,"mouse");                               //  run dialog
   return;
}


//  dialog event and completion function

int bookmarks_dialog_event(zdialog *zd, ch *event)
{
   using namespace bookmarknames;

   int      zstat;

   zstat = zd->zstat;
   if (! zstat) return 1;                                                        //  wait for completion
   zdialog_free(zd);
   zd_bookmark = 0;
   if (zstat == 1) m_edit_bookmarks(0,0);                                        //  [edit bookmarks] button
   return 1;
}


//  mouse click function to receive clicked bookmarks

void bookmarks_callbackfunc(GtkWidget *, int line, int pos, int kbkey)
{
   using namespace bookmarknames;

   ch     *file;

   if (kbkey == GDK_KEY_F1) {                                                    //  key F1 pressed, show help
      showz_docfile(Mwin,"userguide",F1_help_topic);
      return;
   }

   if (! zd_bookmark) return;

   bmkposn = line;                                                               //  get clicked line
   if (bmkposn < 0 || bmkposn > Nbmks-1) return;
   file = bookmarks[bmkposn] + 32;
   if (! regfile(file)) {
      zmessageACK(Mwin,"file not found");
      return;
   }

   f_open(file,0);                                                               //  open file (for yellow highlight)
   gallery(file,"init",0);                                                       //  go to gallery and file position
   gallery(file,"paint",0);
   viewmode('G');

   return;
}


/********************************************************************************/

//  edit bookmarks

void m_edit_bookmarks(GtkWidget *, ch *)
{
   using namespace bookmarknames;

   int edit_bookmarks_dialog_event(zdialog *zd, ch *event);
   void edit_bookmarks_callbackfunc(GtkWidget *, int line, int pos, int kbkey);

   zdialog     *zd;
   ch          *bmk_add = "Add bookmark: click on a gallery thumbnail. \n"       //  24.10
                          "Rename bookmark: click it and press [Rename]";

/***
          _______________________________________________
         |           Edit Bookmarks                      |
         |                                               |
         | Click list position. Click thumbnail to add.  |
         |-----------------------------------------------|
         | bookmarkname1      /topdir/.../filename1.jpg  |
         | bookmarkname2      /topdir/.../filename2.jpg  |
         | bookmarkname3      /topdir/.../filename3.jpg  |
         | bookmarkname4      /topdir/.../filename4.jpg  |
         | bookmarkname5      /topdir/.../filename5.jpg  |
         | bookmarkname6      /topdir/.../filename6.jpg  |
         |-----------------------------------------------|
         | [bookmarkname...] [rename] [delete]           |
         |                                      [OK] [X] |
         |_______________________________________________|

***/

   F1_help_topic = "bookmarks";

   Plog(1,"m_edit_bookmarks \n");

   if (zd_edit_bookmarks) return;                                                //  already busy

   zd = zdialog_new("Edit Bookmarks",Mwin,"OK"," X ",null);
   zd_edit_bookmarks = zd;
   zdialog_add_widget(zd,"hbox","hbtip","dialog",0,"space=3");
   zdialog_add_widget(zd,"label","labtip","hbtip",bmk_add,"space=5");
   zdialog_add_widget(zd,"scrwin","scrwin","dialog",0,"expand");
   zdialog_add_widget(zd,"text","bmklist","scrwin");
   zdialog_add_widget(zd,"hbox","hbname","dialog",0,"space=5");
   zdialog_add_widget(zd,"zentry","bmkname","hbname",0,"space=5|size=30");
   zdialog_add_widget(zd,"button","rename","hbname","Rename","space=5");
   zdialog_add_widget(zd,"button","delete","hbname","Delete","space=5");

   textwidget = zdialog_gtkwidget(zd,"bmklist");                                 //  set mouse/KB event function
   textwidget_set_eventfunc(textwidget,edit_bookmarks_callbackfunc);

   bookmarks_load();                                                             //  load bookmarks from bookmarks file
   bookmarks_refresh();                                                          //  update bookmarks list in dialog

   zdialog_resize(zd,500,400);
   zdialog_run(zd,edit_bookmarks_dialog_event,"save");                           //  run dialog, parallel

   viewmode('G');                                                                //  show current gallery
   return;
}


//  load bookmarks list from bookmarks file

void bookmarks_load()
{
   using namespace bookmarknames;

   ch          buff[XFCC];
   ch          *pp, *pp2;
   FILE        *fid;

   Nbmks = 0;
   fid = fopen(bookmarks_file,"r");
   if (fid) {
      while (true) {
         pp = fgets_trim(buff,XFCC,fid,1);                                       //  next bookmark rec.
         if (! pp) break;
         if (strlen(pp) < 40) continue;
         pp2 = strchr(pp+32,'/');                                                //  verify bookmark
         if (! pp2) continue;
         if (! regfile(pp2)) continue;
         bookmarks[Nbmks] = zstrdup(pp,"bookmarks");                             //  fill bookmark list
         if (++Nbmks == maxbmks) break;
      }
      fclose(fid);
   }

   bmkposn = Nbmks;                                                              //  next free position
   return;
}


//  mouse click function to select existing bookmark from list

void edit_bookmarks_callbackfunc(GtkWidget *, int line, int pos, int kbkey)
{
   using namespace bookmarknames;

   ch     bookmarkname[32];

   if (kbkey == GDK_KEY_F1) {                                                    //  key F1 pressed, show help
      showz_docfile(Mwin,"userguide",F1_help_topic);
      return;
   }

   if (! zd_edit_bookmarks) return;
   if (Nbmks < 1) return;
   if (line < 0) line = 0;
   if (line > Nbmks-1) line = Nbmks-1;
   bmkposn = line;
   strncpy0(bookmarkname,bookmarks[bmkposn],31);
   strTrim(bookmarkname);
   zdialog_stuff(zd_edit_bookmarks,"bmkname",bookmarkname);
   return;
}


//  mouse click function to receive clicked thumbnails for new/revised bookmarks

void edit_bookmarks_Lclick_func(int Nth)
{
   using namespace bookmarknames;

   ch       *imagefile, *newbookmark;
   ch       *pp, bookmarkname[32];
   int      cc;

   if (! zd_edit_bookmarks) return;
   if (Nth < 0) return;                                                          //  gallery gone ?
   imagefile = gallery(0,"getR",Nth);                                            //  get file at clicked position
   if (! imagefile) return;

   pp = strrchr(imagefile,'/');                                                  //  get file name or last subfolder name
   if (! pp) return;                                                             //    to use as default bookmark name
   strncpy0(bookmarkname,pp+1,31);                                               //  max. 30 chars. + null

   cc = strlen(imagefile) + 34;                                                  //  construct bookmark record:
   newbookmark = (ch *) zmalloc(cc,"bookmarks");                                 //    filename  /folders.../filename
   snprintf(newbookmark,cc,"%-30s  %s",bookmarkname,imagefile);

   if (Nbmks == maxbmks) {                                                       //  if list full, remove first
      zfree(bookmarks[0]);
      Nbmks--;
      for (int ii = 0; ii < Nbmks; ii++)
         bookmarks[ii] = bookmarks[ii+1];
   }

   if (Nbmks == 0) bmkposn = 0;                                                  //  1st bookmark --> 0
   else bmkposn++;                                                               //  else clicked position + 1
   if (bmkposn < 0) bmkposn = 0;
   if (bmkposn > Nbmks) bmkposn = Nbmks;

   for (int ii = Nbmks; ii > bmkposn; ii--)                                      //  make hole to insert new bookmark
      bookmarks[ii] = bookmarks[ii-1];

   bookmarks[bmkposn] = newbookmark;                                             //  insert
   Nbmks++;

   bookmarks_refresh();                                                          //  update bookmarks list in dialog

   zdialog_stuff(zd_edit_bookmarks,"bmkname",bookmarkname);

   return;
}


//  dialog event and completion function

int edit_bookmarks_dialog_event(zdialog *zd, ch *event)
{
   using namespace bookmarknames;

   ch          bookmarkname[32];
   FILE        *fid;
   int         cc;

   if (strmatch(event,"delete"))                                                 //  delete bookmark at position
   {
      if (bmkposn < 0 || bmkposn > Nbmks-1) return 1;
      for (int ii = bmkposn; ii < Nbmks-1; ii++)
         bookmarks[ii] = bookmarks[ii+1];
      Nbmks--;
      zdialog_stuff(zd,"bmkname","");                                            //  clear name field
      bookmarks_refresh();                                                       //  update bookmarks list in dialog
   }

   if (strmatch(event,"rename"))                                                 //  apply new name to bookmark
   {
      if (bmkposn < 0 || bmkposn > Nbmks-1) return 1;
      zdialog_fetch(zd,"bmkname",bookmarkname,31);                               //  get name from dialog
      cc = strlen(bookmarkname);
      if (cc < 30) memset(bookmarkname+cc,' ',30-cc);                            //  blank pad to 30 chars.
      bookmarkname[30] = 0;
      memcpy(bookmarks[bmkposn],bookmarkname,30);                                //  replace name in bookmarks list
      bookmarks_refresh();                                                       //  update bookmarks list in dialog
   }

   if (! zd->zstat) return 1;                                                    //  wait for completion

   if (zd->zstat == 1)                                                           //  done
   {
      fid = fopen(bookmarks_file,"w");                                           //  write bookmarks file
      if (! fid)
         zmessageACK(Mwin,"unable to save bookmarks file");
      else {
         for (int ii = 0; ii < Nbmks; ii++)
            fprintf(fid,"%s\n",bookmarks[ii]);
         fclose(fid);
      }
   }

   for (int ii = 0; ii < Nbmks; ii++)                                            //  free memory
      zfree(bookmarks[ii]);

   zdialog_free(zd);
   zd_edit_bookmarks = 0;

   return 1;
}


//  private function to update dialog widget with new bookmarks list

void bookmarks_refresh()
{
   using namespace bookmarknames;

   ch       bookmarkline[XFCC+32];
   ch       blanks[33] = "                                ";
   int      cc;

   if (! zd_edit_bookmarks && ! zd_bookmark) return;
   textwidget_clear(textwidget);                                                 //  clear bookmarks list
   for (int ii = 0; ii < Nbmks; ii++) {                                          //  write bookmarks list
      strncpy0(bookmarkline,bookmarks[ii],31);
      cc = utf8len(bookmarkline);                                                //  compensate multibyte chars.
      strncat(bookmarkline,blanks,32-cc);
      strcat(bookmarkline,bookmarks[ii]+32);
      textwidget_append(textwidget,0,"%s\n",bookmarkline);
   }
   return;
}


/********************************************************************************/

//  set the video frame for a video thumbnail
//  (video file popup menu)

void m_thumbframe(GtkWidget *, ch *)
{
   zdialog  *zd;
   STATB    statB;
   int      ftype, zstat, err;
   int      minutes, seconds;
   ch       *pp, framefile[200];
   ch       *vf, *tf;
   ch       *videofile = 0, thumbfile[XFCC];
   PXB      *framepxb = 0, *thumbpxb = 0;
   timeval  thumbtimes[2];

   ch          *tip = "Play video and stop at desired frame \n"
                      "Note playback time in minutes and seconds";

   F1_help_topic = "video files";

   Plog(1,"m_thumbframe \n");

   if (clicked_file) {                                                           //  use clicked file if present
      videofile = clicked_file;
      clicked_file = 0;
   }
   else if (curr_file)                                                           //  else current file
      videofile = zstrdup(curr_file,"thumbframe");
   else return;

   ftype = image_file_type(videofile);
   if (ftype != VIDEO) goto cleanup;

/**
       ____________________________________________
      |         Set Video Thumbnail Frame          |
      |                                            |
      | Play video and stop at desired frame.      |
      | Playback time: minutes [__] seconds [__]   |
      |                                            |
      |                                   [OK] [X] |
      |____________________________________________|

**/

   zd = zdialog_new("Set Video Thumbnail Frame",Mwin,"OK"," X ",null);
   zdialog_add_widget(zd,"label","labtip","dialog",tip);
   zdialog_add_widget(zd,"hbox","hbtime","dialog",0,"space=3");
   zdialog_add_widget(zd,"label","labtime","hbtime","Playback time:","space=3");
   zdialog_add_widget(zd,"label","space","hbtime",0,"space=3");
   zdialog_add_widget(zd,"label","labmin","hbtime","minutes","space=3");
   zdialog_add_widget(zd,"zspin","minutes","hbtime","0|999|1|0","space=3|size=3");
   zdialog_add_widget(zd,"label","space","hbtime",0,"space=3");
   zdialog_add_widget(zd,"label","labsec","hbtime","seconds","space=3");
   zdialog_add_widget(zd,"zspin","seconds","hbtime","0|59|1|0","space=3|size=3");

   zdialog_run(zd,0,"parent");                                                   //  run dialog and wait for completion
   zstat = zdialog_wait(zd);

   if (zstat != 1) {                                                             //  cancel
      zdialog_free(zd);
      goto cleanup;
   }

   zdialog_fetch(zd,"minutes",minutes);                                          //  get playback time inputs
   zdialog_fetch(zd,"seconds",seconds);
   seconds = 60 * minutes + seconds;
   zdialog_free(zd);

   snprintf(framefile,200,"%s/videoframe.jpg",temp_folder);                      //  get frame file from ffmpeg

   vf = zescape_quotes(videofile);
   err = zshell("ack","ffmpeg -ss %d -i \"%s\" -v 8 -frames 1 -y \"%s\" ",
                                                   seconds, vf, framefile);
   zfree(vf);

   if (err) goto cleanup;

   if (! regfile(framefile)) {
      zmessageACK(Mwin,"cannot get video frame: %s",strerror(errno));
      goto cleanup;
   }

   err = image2thumbfile2(videofile,thumbfile);                                  //  get thumbnail file for video file
   if (err) {
      zmessageACK(Mwin,"cannot create thumbnail file");
      goto cleanup;
   }

   pp = strrchr(thumbfile+1,'/');
   *pp = 0;
   if (! dirfile(thumbfile)) {                                                   //  create thumbnail folder if needed
      tf = zescape_quotes(thumbfile);
      err = zshell("ack","mkdir -p -m 0750 \"%s\"",tf);
      zfree(tf);
      if (err) goto cleanup;
   }
   *pp = '/';

   framepxb = PXB_load(framefile,1);                                             //  video frame file >> PXB image
   if (! framepxb) goto cleanup;

   thumbpxb = PXB_rescale(framepxb,thumbfilesize);                               //  rescale PXB to thumbnail size
   if (! thumbpxb) goto cleanup;

   err = PXB_JPG_save(thumbpxb,thumbfile,80);                                    //  save to JPEG thumbnail file
   if (err) goto cleanup;

   if (! regfile(videofile,&statB)) goto cleanup;                                //  get video file mod time

   thumbtimes[0].tv_sec = thumbtimes[1].tv_sec = statB.st_mtim.tv_sec;           //  thumbnail mod time
   thumbtimes[0].tv_usec = thumbtimes[1].tv_usec = 0;                            //    = video file mod time
   utimes(thumbfile,thumbtimes);

   gallery(0,"paint",-1);                                                        //  repaint gallery

cleanup:
   remove(framefile);
   if (videofile) zfree(videofile);
   if (framepxb) PXB_free(framepxb);
   if (thumbpxb) PXB_free(thumbpxb);
   return;
}


/********************************************************************************/

//  dummy menu entry as target for "Show Hidden Files" KB shortcut

void m_show_hidden(GtkWidget *, ch *)
{
   Plog(1,"m_show_hidden \n");
   KBaction("Show Hidden");
   return;
}


/********************************************************************************/

//  dummy menu entry as target for "Current Album" KB shortcut

void m_current_album(GtkWidget *, ch *)
{
   Plog(1,"m_current_album \n");
   navi::newalbum_menu_event(0,0);
   return;
}


/********************************************************************************/

//  Show an image file in a popup window at mouse position.
//  Re-use most recent window or create a new one if Fnewin != 0.
//  Returns 0 if OK, +N otherwise.

namespace popup_image_names
{
   GtkWidget  *window[10], *drawarea[10];                                        //  up to 10 popup windows open
   ch         *filex[10], reqfull[10], isfull[10];
   float      winc = 1.26, wdec = 0.80;                                          //  window upsize/downsize ratios
   int        Nw = 0;
}


int popup_image(ch *file, GtkWindow *parent, int Fnewin, int size)
{
   using namespace popup_image_names;

   static int  ftf = 1;
   ch         *pp;
   ch         *tipmess("zoom via mouse wheel or Keys +/=/-/↑/↓");

   int popup_image_draw(GtkWidget *, cairo_t *, int &Nw);
   int popup_image_scroll(GtkWidget *, GdkEvent *event, int &Nw);
   int popup_image_KBevent(GtkWidget *, GdkEventKey *event, int &Nw);
   int popup_image_mousebutt(GtkWidget *, GdkEvent *event, int &Nw);
   int popup_image_state_event(GtkWidget *, GdkEvent *, int &Nw);

   if (! main_thread()) zappcrash("popup_image() called from thread");

   if (ftf) {
      ftf = 0;
      poptext_mouse(tipmess,0,0,0,8);
   }

   if (Fnewin)
      if (++Nw == 10) Nw = 0;                                                    //  new window, re-use oldest up to 10
   if (! Fnewin)
      while (Nw > 0 && window[Nw] == 0) Nw--;                                    //  else re-use latest still active

   if (window[Nw]) {
      gtk_widget_destroy(drawarea[Nw]);
      drawarea[Nw] = 0;
      zfree(filex[Nw]);
      filex[Nw] = 0;
   }
   else {
      window[Nw] = gtk_window_new(GTK_WINDOW_TOPLEVEL);                          //  create new popup window
      if (! window[Nw]) return 1;
      if (! size) size = 512;
      gtk_window_set_default_size(GTK_WINDOW(window[Nw]),size,size);
      pp = strrchr(file,'/');                                                    //  window title = file name
      gtk_window_set_title(GTK_WINDOW(window[Nw]),pp+1);

      if (parent) {
         gtk_window_set_transient_for(GTK_WINDOW(window[Nw]),parent);
         gtk_window_set_destroy_with_parent(GTK_WINDOW(window[Nw]),1);
      }
      gtk_window_set_position(GTK_WINDOW(window[Nw]),GTK_WIN_POS_MOUSE);
   }

   filex[Nw] = zstrdup(file,"popup_image");
   drawarea[Nw] = gtk_drawing_area_new();                                        //  new drawing area always required
   if (! drawarea[Nw]) return 2;
   gtk_container_add(GTK_CONTAINER(window[Nw]),drawarea[Nw]);
   reqfull[Nw] = isfull[Nw] = 0;                                                 //  not fullscreen

   gtk_widget_add_events(window[Nw],GDK_SCROLL_MASK);
   gtk_widget_add_events(window[Nw],GDK_KEY_PRESS_MASK);
   gtk_widget_add_events(window[Nw],GDK_BUTTON_RELEASE_MASK);

   G_SIGNAL(window[Nw],"destroy",gtk_widget_destroyed,&window[Nw]);              //  set window = null if destroyed
   G_SIGNAL(window[Nw],"draw",popup_image_draw,&Nval[Nw]);
   G_SIGNAL(window[Nw],"scroll-event",popup_image_scroll,&Nval[Nw]);             //  connect events
   G_SIGNAL(window[Nw],"key-press-event",popup_image_KBevent,&Nval[Nw]);
   G_SIGNAL(window[Nw],"button-release-event",popup_image_mousebutt,&Nval[Nw]);
   G_SIGNAL(window[Nw],"window-state-event",popup_image_state_event,&Nval[Nw]);

   gtk_widget_show_all(window[Nw]);

   return 0;
}


//  rescale image and repaint window to fit

int popup_image_draw(GtkWidget *window, cairo_t *cr, int &nn)
{
   using namespace popup_image_names;

   PXB         *pxb1, *pxb2;
   int         ww1, hh1, ww2, hh2;
   double      area;
   ch          *file;

   file = filex[nn];
   if (! file) return 1;

   pxb1 = PXB_load(file,1);
   if (! pxb1) return 1;

   ww1 = pxb1->ww;                                                               //  image dimensions
   hh1 = pxb1->hh;

   gtk_window_get_size(GTK_WINDOW(window),&ww2,&hh2);                            //  current window dimensions

   area = ww2 * hh2;
   ww2 = sqrt(area * ww1 / hh1);                                                 //  fit window to image, keeping same area
   hh2 = area / ww2;

   pxb2 = PXB_rescale(pxb1,ww2,hh2);                                             //  rescale image to window

   gtk_window_resize(GTK_WINDOW(window),ww2,hh2);

   gdk_cairo_set_source_pixbuf(cr,pxb2->pixbuf,0,0);                             //  draw image
   cairo_paint(cr);

   PXB_free(pxb1);
   PXB_free(pxb2);

   return 1;
}


//  respond to mouse scroll button and zoom window larger or smaller

int popup_image_scroll(GtkWidget *window, GdkEvent *event, int &nn)
{
   using namespace popup_image_names;

   int         scroll, ww, hh;
   double      ff = 1.0;

   if (event->type == GDK_SCROLL) {                                              //  mouse wheel event
      scroll = ((GdkEventScroll *) event)->direction;
      if (scroll == GDK_SCROLL_UP) ff = winc;
      if (scroll == GDK_SCROLL_DOWN) ff = wdec;
   }

   gtk_window_get_size(GTK_WINDOW(window),&ww,&hh);                              //  current window dimensions
   ww *= ff;                                                                     //  new dimensions
   hh *= ff;

   if (ww > monitor_ww || hh > monitor_hh) {                                     //  request > screen size, fullscreen
      reqfull[nn] = 1;
      gtk_window_fullscreen(GTK_WINDOW(window));
      return 1;
   }

   reqfull[nn] = 0;
   gtk_window_unfullscreen(GTK_WINDOW(window));

   if (ww + hh > 512)
      gtk_window_resize(GTK_WINDOW(window),ww,hh);                               //  rescale up or down
   else
      gtk_widget_destroy(window);                                                //  if very small, delete window

   return 1;
}


//  respond to KB events F11 (fullscreen/unfullscreen)
//                       Escape (destroy)
//                       +/= or up-arrow  make image larger
//                       - or down-arrow  make image smaller

int popup_image_KBevent(GtkWidget *window, GdkEventKey *event, int &nn)
{
   using namespace popup_image_names;

   int      KBkey = event->keyval;
   int      ww, hh;
   double   ff = 0;

   if (KBkey == GDK_KEY_Escape) gtk_widget_destroy(window);

   if (KBkey == GDK_KEY_F11)
   {
      if (reqfull[nn]) {
         reqfull[nn] = 0;
         gtk_window_unfullscreen(GTK_WINDOW(window));
      }
      else {
         reqfull[nn] = 1;
         gtk_window_fullscreen(GTK_WINDOW(window));
      }
   }

   if (KBkey == GDK_KEY_plus || KBkey == GDK_KEY_KP_Add || KBkey == GDK_KEY_equal || KBkey == GDK_KEY_Up)
      ff = winc;

   if (KBkey == GDK_KEY_minus || KBkey == GDK_KEY_KP_Subtract || KBkey == GDK_KEY_Down)
      ff = wdec;

   if (ff)
   {
      gtk_window_get_size(GTK_WINDOW(window),&ww,&hh);                           //  current window dimensions
      ww *= ff;                                                                  //  new dimensions
      hh *= ff;

      if (ww > monitor_ww || hh > monitor_hh) {                                  //  request > screen size, fullscreen
         reqfull[nn] = 1;
         gtk_window_fullscreen(GTK_WINDOW(window));
         return 1;
      }

      reqfull[nn] = 0;
      gtk_window_unfullscreen(GTK_WINDOW(window));

      if (ww + hh > 512)
         gtk_window_resize(GTK_WINDOW(window),ww,hh);                            //  rescale up or down
      else
         gtk_widget_destroy(window);                                             //  if very small, delete window
   }

   return 1;
}


//  respond to mouse button - destroy window

int popup_image_mousebutt(GtkWidget *window, GdkEvent *event, int &nn)
{
   gtk_widget_destroy(window);
   return 1;
}


//  track window fullscreen state

int popup_image_state_event(GtkWidget *window, GdkEvent *event, int &nn)
{
   using namespace popup_image_names;

   int state = ((GdkEventWindowState *) event)->new_window_state;
   if (state & GDK_WINDOW_STATE_FULLSCREEN) isfull[nn] = 1;
   else isfull[nn] = 0;

   if (isfull[nn] != reqfull[nn]) {                                              //  compensate GTK bug:    FIXME
      if (reqfull[nn]) gtk_window_fullscreen(GTK_WINDOW(window));                //   the window fullscreens itself after
      else gtk_window_unfullscreen(GTK_WINDOW(window));                          //    being requested to unfullscreen
   }

   return 1;
}