File: MyTreeCtrl.cpp

package info (click to toggle)
4pane 7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 10,044 kB
  • sloc: cpp: 38,264; sh: 4,110; ansic: 3,620; makefile: 157; xml: 27
file content (2100 lines) | stat: -rw-r--r-- 81,008 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
/////////////////////////////////////////////////////////////////////////////
// Name:       MyTreeCtrl.cpp
// Purpose:    Tree and Treelistctrl.
// Derived:    The listctrl parts adapted from wxTreeListCtrl (c) Alberto Griggio, Otto Wyss
// See:        http://wxcode.sourceforge.net/components/treelistctrl/
// Part of:    4Pane
// Author:     David Hart
// Copyright:  (c) 2020 David Hart
// Licence:    GPL v3
/////////////////////////////////////////////////////////////////////////////

#include "wx/log.h"
#include "wx/dirctrl.h"
#include "wx/dragimag.h"
#include "wx/renderer.h"
#include "wx/filename.h" 


#ifdef __WXGTK__
  #include <gtk/gtk.h>
#endif

#include "MyGenericDirCtrl.h"
#include "Dup.h"
#include "MyFiles.h"
#include "MyDirs.h"
#include "MyFrame.h"
#include "Filetypes.h"
#include "Redo.h"
#include "Devices.h"
#include "MyTreeCtrl.h"
#include "Accelerators.h"

#include "bitmaps/include/columnheaderdown.xpm"  // // Icons for the fileview columns, selected & selected-reverse-sort 
#include "bitmaps/include/columnheaderup.xpm"

#if wxVERSION_NUMBER < 2900 // to avoid compiler warnings: only defined in 2.9
  #define wxUSE_NEW_DC 0
#endif

class WXDLLEXPORT wxGenericTreeItem;

WX_DEFINE_EXPORTED_ARRAY(wxGenericTreeItem *, wxArrayGenericTreeItems);

enum { ColDownIcon = 0, ColUpIcon };          // These are the indices of the Down/Up column icons within HeaderimageList

class WXDLLEXPORT wxGenericTreeItem
{
public:
    // ctors & dtor
    wxGenericTreeItem() { m_data = NULL; }
    wxGenericTreeItem(wxGenericTreeItem *parent,
                       const wxString& text,
                       int image,
                       int selImage,
                       wxTreeItemData *data);

    ~wxGenericTreeItem();

    // trivial accessors
    wxArrayGenericTreeItems& GetChildren() { return m_children; }

    const wxString& GetText() const { return m_text; }
    int GetImage(wxTreeItemIcon which = wxTreeItemIcon_Normal) const
        { return m_images[which]; }
    wxTreeItemData *GetData() const { return m_data; }

    // returns the current image for the item (depending on its
    // selected/expanded/whatever state)
    int GetCurrentImage() const;

    void SetText(const wxString &text);
    void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
    void SetData(wxTreeItemData *data) { m_data = data; }

    void SetHasPlus(bool has = true) { m_hasPlus = has; }

    void SetBold(bool bold) { m_isBold = bold; }

    int GetX() const { return m_x; }
    int GetY() const { return m_y; }

    void SetX(int x) { m_x = x; }
    void SetY(int y) { m_y = y; }

    int  GetHeight() const { return m_height; }
    int  GetWidth()  const { return m_width; }

    void SetHeight(int h) { m_height = h; }
    void SetWidth(int w) { m_width = w; }

    wxGenericTreeItem *GetParent() const { return m_parent; }

    // operations
    // deletes all children notifying the treectrl about it if !NULL pointer given
    void DeleteChildren(wxGenericTreeCtrl *tree = NULL);

    // get count of all children (and grand children if 'recursively')
    size_t GetChildrenCount(bool recursively = true) const;

    void Insert(wxGenericTreeItem *child, size_t index)
    { m_children.Insert(child, index); }

    void GetSize(int &x, int &y, const wxGenericTreeCtrl*);

    // return the item at given position (or NULL if no item), onButton is
    // true if the point belongs to the item's button, otherwise it lies
    // on the button's label
    wxGenericTreeItem *HitTest(const wxPoint& point,
                                const wxGenericTreeCtrl *,
                                int &flags,
                                int level);

    void Expand() { m_isCollapsed = false; }
    void Collapse() { m_isCollapsed = true; }

    void SetHilight(bool set = true) { m_hasHilight = set; }

    // status inquiries
    bool HasChildren() const { return !m_children.IsEmpty(); }
    bool IsSelected()  const { return m_hasHilight != 0; }
    bool IsExpanded()  const { return !m_isCollapsed; }
    bool HasPlus()     const { return m_hasPlus || HasChildren(); }
    bool IsBold()      const { return m_isBold != 0; }

    // attributes
    // get them - may be NULL
    wxTreeItemAttr *GetAttributes() const { return m_attr; }
    // get them ensuring that the pointer is not NULL
    wxTreeItemAttr& Attr()
    {
        if (!m_attr)
        {
            m_attr = new wxTreeItemAttr;
            m_ownsAttr = true;
        }
        return *m_attr;
    }
        // set them
    void SetAttributes(wxTreeItemAttr *attr)
    {
        if (m_ownsAttr) delete m_attr;
        m_attr = attr;
        m_ownsAttr = false;
    }
        // set them and delete when done
    void AssignAttributes(wxTreeItemAttr *attr)
    {
        SetAttributes(attr);
        m_ownsAttr = true;
    }

private:
    // since there can be very many of these, we save size by chosing
    // the smallest representation for the elements and by ordering
    // the members to avoid padding.
    wxString            m_text;         // label to be rendered for item

    wxTreeItemData     *m_data;         // user-provided data
#if wxVERSION_NUMBER >= 2900
    int                 m_state;        // item state New in 2.9, and causes a crash if it's not there. Presumably because the object size is changed
#endif
#if wxVERSION_NUMBER >= 2811            // Ditto new in 2.8.11
    int                 m_widthText;
    int                 m_heightText;
#endif
    wxArrayGenericTreeItems m_children; // list of children
    wxGenericTreeItem  *m_parent;       // parent of this item

    wxTreeItemAttr     *m_attr;         // attributes???

    // tree ctrl images for the normal, selected, expanded and
    // expanded+selected states
    int                 m_images[wxTreeItemIcon_Max];
    wxCoord             m_x;            // (virtual) offset from top
    wxCoord             m_y;            // (virtual) offset from left
    int                 m_width;        // width of this item
    int                 m_height;       // height of this item
    // use bitfields to save size
    int                 m_isCollapsed :1;
    int                 m_hasHilight  :1; // same as focused
    int                 m_hasPlus     :1; // used for item which doesn't have
                                          // children but has a [+] button
    int                 m_isBold      :1; // render the label in bold font
    int                 m_ownsAttr    :1; // delete attribute when done
};


//-----------------------------------------------------------------------------
//  Originally wxTreeListHeaderWindow  from wxTreeListCtrl 
//-----------------------------------------------------------------------------

#include <wx/arrimpl.cpp>
WX_DEFINE_OBJARRAY(wxArrayTreeListColumnInfo);

IMPLEMENT_DYNAMIC_CLASS(TreeListHeaderWindow,wxWindow);

TreeListHeaderWindow::TreeListHeaderWindow()
{
    Init();

    m_owner = (MyTreeCtrl*) NULL;
    m_resizeCursor = (wxCursor*) NULL;
}

TreeListHeaderWindow::TreeListHeaderWindow(wxWindow *win,
            wxWindowID id,
            MyTreeCtrl* owner,
            const wxPoint& pos,
            const wxSize& size,
            long style,
            const wxString &name)
    : wxWindow(win, id, pos, size, style, name)
{
    Init();

    m_owner = owner;
    m_resizeCursor = new wxCursor(wxCURSOR_SIZEWE);

p_BackgroundColour = wxGetApp().GetBackgroundColourUnSelected(); // //
}

TreeListHeaderWindow::~TreeListHeaderWindow()
{
    delete m_resizeCursor;
}

void TreeListHeaderWindow::Init()
{
    m_currentCursor = (wxCursor*) NULL;
    m_isDragging = false;
    m_dirty = false;
    
m_total_col_width = 0;                      // // (Originally this wasn't given a default)
selectedcolumn = filename;                  // // The next 2 should be set by config but let's give them sensible defaults anyway
reversesort = false;
}

#if !defined(__WXX11__) 
  #if wxVERSION_NUMBER >= 2900 && !defined(__WXGTK20__) 
    #include "wx/gtk1/dcclient.h"
  #else
#if wxVERSION_NUMBER < 2900
    #include "wx/gtk/dc.h"
#endif
  #endif
#endif

#if wxVERSION_NUMBER < 2900
void TreeListHeaderWindow::DoDrawRect(wxDC *dc, int x, int y, int w, int h)
{
#ifdef __WXGTK__
    GtkStateType state = m_parent->IsEnabled() ? GTK_STATE_NORMAL
                                               : GTK_STATE_INSENSITIVE;

    x = dc->LogicalToDeviceX(x);
#if wxVERSION_NUMBER >= 2701
// // The old-style way of doing things stopped working for gtk1.2 in 2.7.1 and above
// // as GTK_PIZZA(m_wxwindow)->bin_window returns rubbish, leading to a segfault
// // This was taken from wxRendererGTK::DrawHeaderButton in renderer.cpp, and works for both gtk.s
// // The wxUSE_NEW_DC is new in 2.9, only for gtk2 atm
  GdkWindow* gdk_window = NULL;
#if wxUSE_NEW_DC && defined(__WXGTK20__)
    wxDCImpl *impl = dc->GetImpl();
    wxGTKDCImpl *gtk_impl = wxDynamicCast(impl, wxGTKDCImpl);
    if (gtk_impl)
         gdk_window = gtk_impl->GetGDKWindow();
#else // !(wxUSE_NEW_DC && defined(__WXGTK20__))
  #if wxVERSION_NUMBER >= 2900  // This is for gtk1.2 in 2.9
    wxWindowDCImpl * const impl = wxDynamicCast(dc->GetImpl(), wxWindowDCImpl);
    wxCHECK_RET(impl, "must have a window DC");
    gdk_window = impl->GetGDKWindow();
  #else
     gdk_window = dc->GetGDKWindow();
  #endif //wxVERSION_NUMBER >= 2900
#endif //wxUSE_NEW_DC && defined(__WXGTK20__)

    wxASSERT_MSG(gdk_window,
                  wxT("cannot use wxRendererNative on wxDC of this type"));
    gtk_paint_box (m_wxwindow->style, gdk_window,
                   state, GTK_SHADOW_OUT,
                   (GdkRectangle*) NULL, m_wxwindow, "button",
                   x-1, y-1, w+2, h+2);
#else //!wxVERSION_NUMBER >= 2701
    gtk_paint_box (m_wxwindow->style, GTK_PIZZA(m_wxwindow)->bin_window,
                   state, GTK_SHADOW_OUT,
                   (GdkRectangle*) NULL, m_wxwindow, "button",
                   x-1, y-1, w+2, h+2);
#endif // wxVERSION_NUMBER >= 2701

#elif defined(__WXMAC__)
    const int m_corner = 1;

    dc->SetBrush(*wxTRANSPARENT_BRUSH);

    dc->SetPen(wxPen(wxSystemSettings::GetSystemColour(
        wxSYS_COLOUR_BTNSHADOW), 1, wxSOLID));
    dc->DrawLine(x+w-m_corner+1, y, x+w, y+h);  // right (outer)
    dc->DrawRectangle(x, y+h, w+1, 1);          // bottom (outer)

    wxPen pen(wxColour(0x88 , 0x88 , 0x88), 1, wxSOLID);

    dc->SetPen(pen);
    dc->DrawLine(x+w-m_corner, y, x+w-1, y+h);  // right (inner)
    dc->DrawRectangle(x+1, y+h-1, w-2, 1);      // bottom (inner)

    dc->SetPen(*wxWHITE_PEN);
    dc->DrawRectangle(x, y, w-m_corner+1, 1);   // top (outer)
    dc->DrawRectangle(x, y, 1, h);              // left (outer)
    dc->DrawLine(x, y+h-1, x+1, y+h-1);
    dc->DrawLine(x+w-1, y, x+w-1, y+1);
#else // !GTK, !Mac
    const int m_corner = 1;

    dc->SetBrush(*wxTRANSPARENT_BRUSH);

    dc->SetPen(*wxBLACK_PEN);
    dc->DrawLine(x+w-m_corner+1, y, x+w, y+h);  // right (outer)
    dc->DrawRectangle(x, y+h, w+1, 1);          // bottom (outer)

    wxPen pen(wxSystemSettings::GetColour(
      wxSYS_COLOUR_BTNSHADOW), 1, wxSOLID);

    dc->SetPen(pen);
    dc->DrawLine(x+w-m_corner, y, x+w-1, y+h);  // right (inner)
    dc->DrawRectangle(x+1, y+h-1, w-2, 1);      // bottom (inner)

    dc->SetPen(*wxWHITE_PEN);
    dc->DrawRectangle(x, y, w-m_corner+1, 1);   // top (outer)
    dc->DrawRectangle(x, y, 1, h);              // left (outer)
    dc->DrawLine(x, y+h-1, x+1, y+h-1);
    dc->DrawLine(x+w-1, y, x+w-1, y+1);
#endif
}

#endif // wxVERSION_NUMBER < 2900

// shift the DC origin to match the position of the main window horz
// scrollbar: this allows us to always use logical coords
void TreeListHeaderWindow::AdjustDC(wxDC& dc)
{
    int xpix;
    m_owner->GetScrollPixelsPerUnit(&xpix, NULL);

    int x;
    m_owner->GetViewStart(&x, NULL);

    // account for the horz scrollbar offset
    dc.SetDeviceOrigin(-x * xpix, 0);
}

void TreeListHeaderWindow::OnPaint(wxPaintEvent &WXUNUSED(event))
{
    static const int HEADER_OFFSET_X = 1, HEADER_OFFSET_Y = 1;
#ifdef __WXGTK__
    wxClientDC dc(this);
#else
    wxPaintDC dc(this);
#endif

    PrepareDC(dc);
    AdjustDC(dc);

    // //dc.BeginDrawing();

    dc.SetFont(GetFont());

    // width and height of the entire header window
    int w, h;
    GetClientSize(&w, &h);
    m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);

    dc.SetBackgroundMode(wxTRANSPARENT);

    // do *not* use the listctrl colour for headers - one day we will have a
    // function to set it separately
    //dc.SetTextForeground(*wxBLACK);
    dc.SetTextForeground(wxSystemSettings::
                            GetColour(wxSYS_COLOUR_WINDOWTEXT));      // //
  SetBackgroundColour(*p_BackgroundColour); // //
    int x = HEADER_OFFSET_X;

    int numColumns = GetColumnCount();
    for (int i = 0; i < numColumns && x < w; i++)
    {
  wxTreeListColumnInfo& column = GetColumn(i);
        int wCol = column.GetWidth(); 

        // the width of the rect to draw: make it smaller to fit entirely
        // inside the column rect
        int cw = wCol - 2;

        dc.SetPen(*wxWHITE_PEN);
#if wxVERSION_NUMBER >= 2900
  wxRendererNative& renderer = wxRendererNative::Get();
        renderer.DrawHeaderButton(this, dc, wxRect(x, HEADER_OFFSET_Y, cw, h-2));
#else
        DoDrawRect(&dc, x, HEADER_OFFSET_Y, cw, h-2);
#endif
        // if we have an image, draw it on the right of the label  // // except that I want it in the middle --- see below 
        int image = column.GetImage(); //item.m_image;
  int ix = -2, iy = 0;
  wxImageList* imageList = m_owner->HeaderimageList;    // // m_owner->GetImageList();    I've had to make my own list, as my bitmaps are 16*10, not 16*16
        if(image != -1) {
            if(imageList) {
                imageList->GetSize(image, ix, iy);
      }
            //else: ignore the column image
        }

        // extra margins around the text label
  static const int EXTRA_WIDTH = 3;
  static const int EXTRA_HEIGHT = 4;

  int text_width = 0;
  int text_x = x;
  // // int image_offset = cw - ix - 1;
  int image_offset = (cw - ix)/2;                        // // The way to get a central image
  
  switch(column.GetAlignment()) {
  case wxTL_ALIGN_LEFT:
      text_x += EXTRA_WIDTH;
      cw -= ix + 2;
      break;
  case wxTL_ALIGN_RIGHT:
      dc.GetTextExtent(column.GetText(), &text_width, NULL);
      text_x += cw - text_width - EXTRA_WIDTH;
      image_offset = 0;
      break;
  case wxTL_ALIGN_CENTER:
      dc.GetTextExtent(column.GetText(), &text_width, NULL);
      text_x += (cw - text_width)/2 + ix + 2;
      image_offset = (cw - text_width - ix - 2)/2;
      break;
  }

  // draw the image
  if(image != -1 && imageList) {
      imageList->Draw(image, dc, x + image_offset/*cw - ix - 1*/,
          HEADER_OFFSET_Y + (h - 4 - iy)/2,
          wxIMAGELIST_DRAW_TRANSPARENT);
  }
  
        // draw the text clipping it so that it doesn't overwrite the column
        // boundary
        wxDCClipper clipper(dc, x, HEADER_OFFSET_Y, cw, h - 4);

  dc.DrawText(column.GetText(),
                     text_x, HEADER_OFFSET_Y + EXTRA_HEIGHT);

        x += wCol;
    }

    // //dc.EndDrawing();
}

void TreeListHeaderWindow::DrawCurrent()
{
    int x1 = m_currentX;
    int y1 = 0;
    ClientToScreen(&x1, &y1);

    int x2 = m_currentX-1;
#ifdef __WXMSW__
    ++x2; // but why ? 
#endif
    int y2 = 0;
    m_owner->GetClientSize(NULL, &y2);
    m_owner->ClientToScreen(&x2, &y2);

    wxScreenDC dc;
    dc.SetLogicalFunction(wxINVERT);
#if wxVERSION_NUMBER < 2900
    dc.SetPen(wxPen(*wxBLACK, 2, wxSOLID));
#else
    dc.SetPen(wxPen(*wxBLACK, 2, wxPENSTYLE_SOLID));
#endif

    dc.SetBrush(*wxTRANSPARENT_BRUSH);

    AdjustDC(dc);

    dc.DrawLine(x1, y1, x2, y2);

    dc.SetLogicalFunction(wxCOPY);

    dc.SetPen(wxNullPen);
    dc.SetBrush(wxNullBrush);
}

void TreeListHeaderWindow::OnMouse(wxMouseEvent &event)
{
    // we want to work with logical coords
    int x;
    m_owner->CalcUnscrolledPosition(event.GetX(), 0, &x, NULL);
    int y = event.GetY();

    if (m_isDragging)
    {
        SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING,
                      event.GetPosition());

  // we don't draw the line beyond our window, but we allow dragging it
        // there
        int w = 0;
        GetClientSize(&w, NULL);
        m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
        w -= 6;

        // erase the line if it was drawn
        if (m_currentX < w)
            DrawCurrent();

        if (event.ButtonUp())
        {
            ReleaseMouse();
            m_isDragging = false;
            m_dirty = true;
            
size_t newwidth = wxMax((m_currentX - m_minX), 10);             // // Find the new width --- may be negative, so don't let it get too small
int widthchange = newwidth - GetColumnWidth(m_column);
if (m_column == GetLastVisibleCol() && newwidth-current > 0)    // // If we're trying to drag-bigger the last visible column,
  { newwidth += widthchange * 2;                                // //    double the increment as the tiny drag-onto-able space makes it so slow otherwise
    m_columns[m_column].SetDesiredWidth(newwidth);              // //        and make it stick
  }

SetColumnWidth(m_column, newwidth);

  SizeColumns();  // //
            Refresh();
            SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG,
                          event.GetPosition());
        }
        else
        {
            if (x > m_minX + 7)
                m_currentX = x;
            else
                m_currentX = m_minX + 7;

            // draw in the new location
            if (m_currentX < w)
                DrawCurrent();
        }
    }
    else // not dragging
    {
#if wxVERSION_NUMBER > 2503                            // // As ContextMenu events don't seem to get to the headerwindow otherwise
  if (event.RightUp())
    { int xpos = 0, clm = 0;  // find the column where this event occured
      for (size_t col = 0; col < GetColumnCount(); ++col)
        { if (IsHidden(col))   continue;
          xpos += GetColumnWidth(col);
          clm = col;
          if (x < xpos)
              break;       // inside the column
        }
      wxContextMenuEvent conevent(wxEVT_CONTEXT_MENU, clm, ClientToScreen(event.GetPosition()));
      return ShowContextMenu(conevent);
    }
#endif
    m_minX = 0;
        bool hit_border = false;

        // end of the current column
        int xpos = 0;

        // find the column where this event occured
        int countCol = GetColumnCount();
        for (int col = 0; col < countCol; col++)
        {
if (IsHidden(col))   continue;                                     // // I've 'hidden' some cols by giving them widths of zero.  This takes it into account
            xpos += GetColumnWidth(col);
            m_column = col;

            if ((abs(x-xpos) < 3) && (y < (HEADER_HEIGHT-1)))      // // I've changed "y <" comparator to HEADER_HEIGHT-1: originally the figure 22 was given
            {
                // near the column border
                hit_border = true;
                break;
            }

            if (x < xpos)
            {
                // inside the column
                break;
            }

            m_minX = xpos;
        }

        if (event.LeftDown() /*|| event.RightUp()*/)                 // // I've grabbed RtUp earlier to fire the context menu.  What it was doing here anyway?
        {
            if (hit_border/* && event.LeftDown()*/)                  // // See above comment
            {
                m_isDragging = true;
                m_currentX = x;
                DrawCurrent();
                CaptureMouse();
                SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG,
                              event.GetPosition());
            }
            else // click on a column
            {
                SendListEvent(event.LeftDown()
             ? wxEVT_COMMAND_LIST_COL_CLICK
             : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK,
             event.GetPosition());
            }
        }
        else if (event.Moving())
        {
            bool setCursor;
            if (hit_border)
            {
                setCursor = m_currentCursor == wxSTANDARD_CURSOR;
                m_currentCursor = m_resizeCursor;
            }
            else
            {
                setCursor = m_currentCursor != wxSTANDARD_CURSOR;
                m_currentCursor = (wxCursor*)wxSTANDARD_CURSOR;
            }

            if (setCursor)
                SetCursor(*m_currentCursor);
        }
    }
}

int TreeListHeaderWindow::GetNextVisibleCol(int currentcolumn)    // // Finds the next column with non-zero width
{
int colcount = GetColumnCount(); if (!colcount || currentcolumn==(colcount-1)) return -1;
for (int col=currentcolumn+1; col < colcount; ++col)
  if (!IsHidden(col)) return col;
  
return -1;
}

int TreeListHeaderWindow::GetLastVisibleCol()    // // Finds the last column with non-zero width.  0 flags failure (but shouldn't happen!)
{
int colcount = GetColumnCount(); if (!colcount) return 0;  // Shouldn't happen as col 0 will always exist & be visible
for (int col=colcount; col > 0; --col)
  if (!IsHidden(col-1)) return col-1;
  
return 0;
}

int TreeListHeaderWindow::GetTotalPreferredWidth()  // // Omits the extra amount added to the last column to fill the whole client-size
{
int lastvisiblecol = GetLastVisibleCol();// if (!lastvisiblecol) return GetWidth();
return GetWidth() - (GetColumnWidth(lastvisiblecol) - GetColumnDesiredWidth(lastvisiblecol));
}

void TreeListHeaderWindow::OnSetFocus(wxFocusEvent &WXUNUSED(event))
{
    m_owner->SetFocus();
}

void TreeListHeaderWindow::ShowIsFocused(bool focus)
{
p_BackgroundColour = (focus ? wxGetApp().GetBackgroundColourSelected(true) : wxGetApp().GetBackgroundColourUnSelected());
Refresh();
}

void TreeListHeaderWindow::SendListEvent(wxEventType type, wxPoint pos,
                                                          int column/*=-1*/)  // // I've added int column, as otherwise we have a problem when the event refers to hiding a col
{
    wxWindow *parent = GetParent();
    wxListEvent le(type, parent->GetId());
    le.SetEventObject(parent);
    le.m_pointDrag = pos;

    // the position should be relative to the parent window, not
    // this one for compatibility with MSW and common sense: the
    // user code doesn't know anything at all about this header
    // window, so why should it get positions relative to it?
    le.m_pointDrag.y -= GetSize().y;

    if (column == -1)                                           // //
    le.m_col = m_column;
   else le.m_col = -1;                                            // // Otherwise pass an intentionally invalid column.  This tells FileGenericDirCtrl::HeaderWindowClicked() not to (re)reverse the column
   
    parent->GetEventHandler()->ProcessEvent(le);
}


void TreeListHeaderWindow::AddColumn(const wxTreeListColumnInfo& col)
{
    m_columns.Add(col);
    m_total_col_width += col.GetWidth();
    //m_owner->GetHeaderWindow()->Refresh();
    //m_dirty = true;
//SizeColumns();                                      // //
    m_owner->AdjustMyScrollbars();
    m_owner->m_dirty = true;
    Refresh();
}

void TreeListHeaderWindow::SetColumnWidth(size_t column, size_t width)
{
    if(column < GetColumnCount()) {
  m_total_col_width -= m_columns[column].GetWidth();
  m_columns[column].SetWidth(width);
  m_total_col_width += width;
  // // m_owner->AdjustMyScrollbars();  (Don't bother:  this is always done afterwards by the calling functions)
  m_owner->m_dirty = true;
  //m_dirty = true;
  Refresh();
    }
}

void TreeListHeaderWindow::InsertColumn(size_t before,
            const wxTreeListColumnInfo& col)
{
    wxCHECK_RET(before < GetColumnCount(), _("Invalid column index"));
    m_columns.Insert(col, before);
    m_total_col_width += col.GetWidth();
    //m_dirty = true;
    //m_owner->GetHeaderWindow()->Refresh();
SizeColumns();                                      // //
    // //m_owner->AdjustMyScrollbars();
    m_owner->m_dirty = true;
    Refresh();
}


void TreeListHeaderWindow::RemoveColumn(size_t column)
{
    wxCHECK_RET(column < GetColumnCount(), _("Invalid column"));
    m_total_col_width -= m_columns[column].GetWidth();
    m_columns.RemoveAt(column);
    //m_dirty = true;
SizeColumns();                                      // //
    // //m_owner->AdjustMyScrollbars();
    m_owner->m_dirty = true;
    Refresh();
}


void TreeListHeaderWindow::SetColumn(size_t column,
               const wxTreeListColumnInfo& info)
{
    wxCHECK_RET(column < GetColumnCount(), _("Invalid column"));
    size_t w = m_columns[column].GetWidth();
    m_columns[column] = info;
    //m_owner->GetHeaderWindow()->Refresh();
    //m_dirty = true;
    if(w != info.GetWidth()) {
  m_total_col_width += info.GetWidth() - w;
  SizeColumns();                                      // //
  // //m_owner->AdjustMyScrollbars();
  m_owner->m_dirty = true;
    }
    Refresh();
}

void TreeListHeaderWindow::ToggleColumn(int col)    // // If it's hidden, show it & vice versa
{
if (IsHidden(col))    ShowColumn(col);
 else ShowColumn(col, false);
}

void TreeListHeaderWindow::ShowColumn(int col, bool show /*=true*/)    // // Show (or hide) the relevant column
{
if (IsHidden(col) == !show)  return;                  // If it's already doing what we want, wave goodbye

int width;
if (show)
  { int last = GetLastVisibleCol();
    if (col > last)                                   // If the newly-shown column will become the last visible, 
      { if (GetColumnDesiredWidth(last) > 0)          //   we must adjust the current last col first, otherwise it'll never revert to its preferred width
          SetColumnWidth(last, GetColumnDesiredWidth(last));
         else
          SetColumnWidth(last, m_owner->GetCharWidth() * DEFAULT_COLUMN_WIDTHS[last]);
      }
    width = GetColumnDesiredWidth(col);               // We need to know how big to make it
    if (width <= 0) width = m_owner->GetCharWidth() * DEFAULT_COLUMN_WIDTHS[col];  // If there's no valid size, use default, adjusted for fontsize
  }
 else 
  { width = 0;                                        // Hiding means zero width
    if (GetSelectedColumn() == col)  
      { SelectColumn(0);                              // If this col is currently selected, select col 0 instead
        SendListEvent(wxEVT_COMMAND_LIST_COL_CLICK,  wxPoint(0,0), -2);  // This tells the fileview not to re-order the files by col, as we just did
      }
  }
  
SetColumnWidth(col, width);                           // This also sets/resets the ishidden flag
if (show) SizeColumns();
  else ((FileGenericDirCtrl*)m_owner->GetParent())->DoSize();
m_dirty = true;                                       // We'll need to refresh the column display, as some names may be differently clipped
}

void TreeListHeaderWindow::SetSelectedColumn(size_t col)  // // Sets the selected column, the one that controls Sorting
{
GetColumn(GetSelectedColumn()).SetImage(-1);          // Turn off the image from the previously-selected column
selectedcolumn = (enum columntype)col;
}

void TreeListHeaderWindow::SelectColumn(int col)
{
ShowColumn(col);                                      // Better make sure it's visible!

SetSortOrder(0);                                      // In case we were previously reverse-sorting, set to normal
SetSelectedColumn(col);
GetColumn(col).SetImage(ColDownIcon);
Refresh();
}

bool TreeListHeaderWindow::SetSortOrder(int order /*=-1*/)    // // Set whether we're reverse-sorting.  If no parameter, negates sort-order
{
switch(order)
  { case 0:  reversesort = false; return reversesort;           // If 0, set order to normal
    case 1:  reversesort = true;  return reversesort;           // If 1, set order to reverse
     default:  reversesort = !reversesort; return reversesort;  // Otherwise reverse the current order
  }
}

void TreeListHeaderWindow::SetSelectedColumnImage(bool order)    // // Sets the correct up/down image according to sort-order parameter
{
GetColumn(GetSelectedColumn()).SetImage(ColDownIcon + order);
Refresh();
}

void TreeListHeaderWindow::ReverseSelectedColumn()
{
SetSelectedColumnImage(SetSortOrder()); // Calling SetSortOrder() to get the parameter has the effect of toggling reverse
}

void TreeListHeaderWindow::SizeColumns()    // // Resize the last visible col to occupy any spare space
{
size_t colcount = GetColumnCount(); if (!colcount) return;

int width, h; GetClientSize(&width, &h);
int diff = width - m_total_col_width;               // Is the new width > or < than the previous value?
if (diff == 0) return;                              // If ISQ, not much to do

int lastvisiblecol = GetLastVisibleCol();
if (diff < 0)                                       // If negative, reduce the last col's width towards its preferred value if currently higher
  { int spare =  GetColumnWidth(lastvisiblecol) - GetColumnDesiredWidth(lastvisiblecol);
    if (spare > 0)
      SetColumnWidth(lastvisiblecol, GetColumnWidth(lastvisiblecol) - wxMin(abs(diff), spare));
  }
 else
  SetColumnWidth(lastvisiblecol, GetColumnWidth(lastvisiblecol) + diff);  // Otherwise just dump the spare space onto the last visible col, to avoid a silly-looking blank

m_owner->AdjustMyScrollbars();
Refresh(); 
}

void TreeListHeaderWindow::ShowContextMenu(wxContextMenuEvent& event)  // // Menu to choose which columns to display
{
int columns[] = { SHCUT_SHOW_COL_EXT, SHCUT_SHOW_COL_SIZE, SHCUT_SHOW_COL_TIME, SHCUT_SHOW_COL_PERMS, SHCUT_SHOW_COL_OWNER,
                  SHCUT_SHOW_COL_GROUP, SHCUT_SHOW_COL_LINK, wxID_SEPARATOR, SHCUT_SHOW_COL_ALL, SHCUT_SHOW_COL_CLEAR };
wxMenu colmenu;
colmenu.SetClientData((wxClientData*)this); // In >wx2.8 we must store ourself in the menu, otherwise MyFrame::DoColViewUI can't work out who we are

for (size_t n=0; n < 10; ++n)
  { MyFrame::mainframe->AccelList->AddToMenu(colmenu, columns[n], wxEmptyString,  (n < 7 ? wxITEM_CHECK : wxITEM_NORMAL));
  #ifdef __WXX11__
    if (n < 7) colmenu.Check(columns[n], !IsHidden(n+1)); // The usual updateui way of doing this doesn't work in X11
  #endif
  }

if (event.GetId() == 1) // If the right-click was over the 'ext' menu, add a submenu
  { int extsubmenu[] = { SHCUT_EXT_FIRSTDOT, SHCUT_EXT_MIDDOT, SHCUT_EXT_LASTDOT };
    wxMenu* submenu = new wxMenu(_("An extension starts at the filename's..."));
    for (size_t n=0; n < 3; ++n)
      MyFrame::mainframe->AccelList->AddToMenu(*submenu, extsubmenu[n], wxEmptyString, wxITEM_RADIO);

    MyFrame::mainframe->AccelList->AddToMenu(colmenu, wxID_SEPARATOR);
    colmenu.AppendSubMenu(submenu, _("Extension definition..."));

    submenu->FindItem(extsubmenu[EXTENSION_START])->Check();
  }

colmenu.SetTitle(wxT("Columns to Display"));

wxPoint pt = event.GetPosition();
ScreenToClient(&pt.x, &pt.y);
#ifdef __WXX11__
  m_owner->PopupMenu(&colmenu, pt.x, 0);  // In X11 a headerwindow doesn't seem to be able to maintain a popup window, so use the treectrl
#else
  PopupMenu(&colmenu, pt.x, pt.y);
#endif
}

void TreeListHeaderWindow::OnExtSortMethodChanged(wxCommandEvent& event)  // From the ext-column submenu of the context menu
{
switch(event.GetId() - SHCUT_EXT_FIRSTDOT)
  { case 0:   EXTENSION_START = 0; break;
    case 1:   EXTENSION_START = 1; break;
    case 2:   EXTENSION_START = 2; break;
  }

wxConfigBase::Get()->Write(wxT("/Misc/Display/EXTENSION_START"), (long)EXTENSION_START);

MyFrame::mainframe->RefreshAllTabs(NULL);  // This makes all the fileviews update
}

void TreeListHeaderWindow::OnColumnSelectMenu(wxCommandEvent& event)
{
bool flag;                   // First deal with ShowAll and Clear
if ((flag = (event.GetId() == SHCUT_SHOW_COL_ALL)) ||  (event.GetId() == SHCUT_SHOW_COL_CLEAR))
  { for (size_t col = 1; col < TOTAL_NO_OF_FILEVIEWCOLUMNS; ++col)
        ShowColumn(col, flag);
    return;
  }
                            // Otherwise it's a straight-forward column-toggle
size_t col = event.GetId() - SHCUT_SHOW_COL_EXT + 1;  // +1 because Name column is always visible
if (col > TOTAL_NO_OF_FILEVIEWCOLUMNS) return;        // Sanity check

ToggleColumn(col);
}

BEGIN_EVENT_TABLE(TreeListHeaderWindow,wxWindow)
  EVT_PAINT         (TreeListHeaderWindow::OnPaint)
  EVT_MOUSE_EVENTS  (TreeListHeaderWindow::OnMouse)
  EVT_SET_FOCUS     (TreeListHeaderWindow::OnSetFocus)
  EVT_MENU_RANGE(SHCUT_SHOW_COL_EXT, SHCUT_SHOW_COL_CLEAR, TreeListHeaderWindow::OnColumnSelectMenu)
  EVT_MENU_RANGE(SHCUT_EXT_FIRSTDOT, SHCUT_EXT_LASTDOT, TreeListHeaderWindow::OnExtSortMethodChanged)
  EVT_CONTEXT_MENU(TreeListHeaderWindow::ShowContextMenu)
END_EVENT_TABLE()
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------

MyTreeCtrl::MyTreeCtrl(wxWindow *parentwin, wxWindowID id , const wxPoint& pos, const wxSize& size,
                               long style, const wxValidator &validator, const wxString& name)
                                              :  wxTreeCtrl(parentwin, id, pos, size, style, validator, name), m_dirty(false)
{
headerwindow = NULL;                                  // To flag to premature OnIdle events that there's nothing safe to do

HeaderimageList = new wxImageList(16, 10, true);      // This holds the down & up symbols for the header of the selected fileview column
HeaderimageList->Add(wxIcon(columnheaderdown_xpm));
HeaderimageList->Add(wxIcon(columnheaderup_xpm));

// >2.6.3 the gtk2 default was changed to no lines, and hard-coded into create! So we need to do this here, not by adjusting style
if (SHOW_TREE_LINES) SetWindowStyle(GetWindowStyle() & ~wxTR_NO_LINES);
  else SetWindowStyle(GetWindowStyle() | wxTR_NO_LINES);
  
parent = (MyGenericDirCtrl*)parentwin;

IgnoreRtUp = false;
dragging = false;
startpt.x = startpt.y = 0;

CreateAcceleratorTable();
}

void MyTreeCtrl::CreateAcceleratorTable()
{
wxAcceleratorEntry entries[1];
MyFrame::mainframe->AccelList->FillEntry(entries[0], SHCUT_SELECTALL);  // AcceleratorList supplies the keycode etc
wxAcceleratorTable accel(1, entries);
SetAcceleratorTable(accel);
}

void MyTreeCtrl::RecreateAcceleratorTable()
{
        // It would be nice to be able to empty the old accelerator table before replacing it, but trying caused segfaulting!
CreateAcceleratorTable();                                   // Make a new one, with up-to-date data
}
   
void MyTreeCtrl::OnCtrlA(wxCommandEvent& event)             // Does Select All on Ctrl-A
{
if (parent->fileview==ISLEFT)   return;    // I can't make it work properly for dir-views, & I'm not sure it's a good idea anyway --- select & delete whole filesystem?
if (!m_current) return;                // Don't ask me why, but if there isn't a current selection, SelectItemRange segfaults


wxTreeItemIdValue cookie; 
wxTreeItemId firstItem = GetFirstChild(GetRootItem(), cookie);        // Use FirstChild as 1st item
if (!firstItem.IsOk()) return;
wxGenericTreeItem* first = (wxGenericTreeItem*)firstItem.m_pItem;

wxTreeItemId lastItem = GetLastChild(GetRootItem());                  // & LastChild as last
if (!lastItem.IsOk()) return;
wxGenericTreeItem* last = (wxGenericTreeItem*)lastItem.m_pItem;

SelectItemRange(first, last);

wxArrayString selections; static_cast<MyGenericDirCtrl*>(parent)->GetMultiplePaths(selections);
static_cast<FileGenericDirCtrl*>(parent)->UpdateStatusbarInfo(selections); // Update the statusbar info
}

void MyTreeCtrl::OnReceivingFocus(wxFocusEvent& event)  // This tells grandparent window which of the 2 twin-ctrls is the one to consider active
{
if (!MyFrame::mainframe->SelectionAvailable) return;    // If we're constructing/destructing, do nothing lest we break a non-existent window
parent->ParentTab->SetActivePane(parent);               // This also stores the pane in FocusController

wxString selected = parent->GetPath();
wxArrayString selections; static_cast<MyGenericDirCtrl*>(parent)->GetMultiplePaths(selections);
parent->DisplaySelectionInTBTextCtrl(selected);         // Take the opportunity also to display current selection in textctrl
MyFrame::mainframe->Layout->OnChangeDir(selected);      //  & the Terminal etc if necessary
static_cast<MyGenericDirCtrl*>(parent)->m_StatusbarInfoValid = false;
if (parent->fileview == ISLEFT)                         //  & in the statusbar
  ((DirGenericDirCtrl*)parent)->UpdateStatusbarInfo(selections);
 else    ((FileGenericDirCtrl*)parent)->UpdateStatusbarInfo(selections);

((DirGenericDirCtrl*)parent)->CopyToClipboard(selections, true); // FWIW, copy the filepath(s) to the primary selection too

if (headerwindow)
  { parent->ParentTab->SwitchOffHighlights(); // Start with a blank canvas
    headerwindow->ShowIsFocused(true);
  }

HelpContext = HCunknown;
event.Skip();
}

void MyTreeCtrl::OnLosingFocus(wxFocusEvent& event)   // Unhighlights the headerwindow
{
if (headerwindow)
  headerwindow->ShowIsFocused(false);
}

void MyTreeCtrl::AdjustForNumlock(wxKeyEvent& event)  // The fix for the X11 Numlock problem
{
event.m_metaDown = false;                   // Makes it look as if numlock is off, so Accelerators are properly recognised

if (event.GetKeyCode() == WXK_ESCAPE)       // This is used in DnD, to abort it if ESC pressed
  MyFrame::mainframe->OnESC_Pressed();

event.Skip();
}

bool MyTreeCtrl::QueryIgnoreRtUp()
{
if (IgnoreRtUp) 
    { IgnoreRtUp = false; return true; }    // If we've been Rt-dragging, and now stopped, ignore the event so that we don't have a spurious context menu
 else return false;                         // Otherwise proceed as normal
}

void MyTreeCtrl::OnMiddleDown(wxMouseEvent& event)  // Allows middle-button paste
{
event.Skip();

int flags; wxTreeItemId item = HitTest(event.GetPosition(), flags); // Find out if we're on a tree item
if (item.IsOk()) SelectItem(item);                                  // If so, select it so that any paste gets deposited there, not in any currently selected folder.  That's _probably_ what's expected

wxCommandEvent cevent;
parent->OnShortcutPaste(cevent);
}

void MyTreeCtrl::OnLtDown(wxMouseEvent& event)  // Start DnD pre-processing if we're near a selection
{
event.Skip();                                   // First allow base-class to process it

int flags; wxTreeItemId item = HitTest(event.GetPosition(), flags);  // Find out if we're on a tree item
if (item.IsOk()) OnBeginDrag(event);            // If so, if it's a selection start drag-processing
                                                // If we're NOT near a selection, ignore
}

void MyTreeCtrl::OnMouseMovement(wxMouseEvent& event)
{
if (!event.Dragging())
  { if (dragging)  ResetPreDnD();               // If we had been seeing whether or not to drag, & now here's a non-drag event, cancel start-up

    if (SHOWPREVIEW)
      { if (parent->fileview==ISRIGHT)
          { int flags; wxTreeItemId item = HitTest(event.GetPosition(), flags);
            if (item.IsOk())
              { wxDirItemData* data = (wxDirItemData*)GetItemData(item);  
                PreviewManager::ProcessMouseMove(item, flags, data->m_path, event);
              }
          }
         else PreviewManager::Clear();
      }

    event.Skip();   return;
  }
 else
  PreviewManager::Clear();

                      // If we're dragging but with the Rt button, do a multiple select if wxTR_MULTIPLE is true
if (event.RightIsDown() && !event.LeftIsDown())
  { if (!HasFlag(wxTR_MULTIPLE)) return;        // Can't have multiple items selected so abort
    
    int flags; wxArrayTreeItemIds selection;
    wxTreeItemId item = HitTest(event.GetPosition(), flags);  // Find out if we're on the level of a tree item
    bool alreadyaselection = GetSelections(selection);        //  & whether or not the tree already has >0 items selected
    if (item.IsOk() && !IsSelected(item))       // if we're on an item,  & it isn't already selected
      { if (!alreadyaselection)                 // If there is currently no selection, select it
              SelectItem(item);
          else
              DoSelectItem(item, false, true);  // Otherwise extend the current selection
        IgnoreRtUp = true;
      }
    return; 
  }

if (dragging) OnBeginDrag(event);               // Otherwise, if pre-drag has been initiated, go to OnBeginDrag to continue it
}

#if wxVERSION_NUMBER >2503
void MyTreeCtrl::OnRtUp(wxMouseEvent& event)    // In >wxGTK-2.5.3 the wxTreeCtrl has started to interfere with Context Events.  This is a workaround
{
wxContextMenuEvent conevent(wxEVT_CONTEXT_MENU, wxID_ANY, ClientToScreen(event.GetPosition()));
if (parent->fileview == ISLEFT)                 // Send the event to the correct type of parent
  ((DirGenericDirCtrl*)parent)->ShowContextMenu(conevent);
 else  
  ((FileGenericDirCtrl*)parent)->ShowContextMenu(conevent);
}
#endif

void MyTreeCtrl::OnBeginDrag(wxMouseEvent& event)
{
  // I've taken over the detection of dragging from wxTreeCtrl, which tends to be trigger-happy:  the slightest movement 
  // with a mouse-key down sometimes will trigger it. So I've added a distance requirement.  Note that this is asymmetrical, the x axis requirement being less.
  // This is because of the edge effect:  if the initial click is too near the edge of a pane & the mouse is then moved edgewards, it may go into the adjacent pane
  // before the drag commences, whereupon it picks up the selection in THAT pane, not the original.
  // I've also added a time element
  // There was also a problem with selections:  when a LClick arrives here it hasn't had time to establish a Selection, so we have to assume it will, & check later that is was valid
  
static wxDateTime dragstarttime;  
static wxTreeItemId dragitem;      // We need this static, so that we can check that the item that starts the process is actually selected by the time dragging is triggered
static MyTreeCtrl* originaltree;

if (startpt.x == 0 && startpt.y == 0)           // 0,0 means not yet set, so this is what we do the first time thru
  { int flags; dragitem = HitTest(event.GetPosition(), flags);  // Find out if we're near a tree item (& not in the space to the right of it)
    if (!dragitem.IsOk()  || flags & wxTREE_HITTEST_ONITEMRIGHT) 
          return;                // Abort if we're not actually over the selected item.  Otherwise we'll start dragging an old selection even if we're now miles away
    if (dragitem == GetRootItem()) return;      // Or if it's root
    // It's a valid LClick, so get things ready for the next time through
    startpt.x = event.GetPosition().x; startpt.y = event.GetPosition().y;
    dragstarttime = wxDateTime::UNow();         // Reset the time to 'now'
    originaltree = this;                        // In case we change panes before the drag 'catches'
    dragging = true;  return; 
  }
  
if ((abs(startpt.x - event.GetPosition().x)) < DnDXTRIGGERDISTANCE   // If we've not moved far enough in either direction, return
              &&  (abs(startpt.y - event.GetPosition().y)) < DnDYTRIGGERDISTANCE)      return;

wxTimeSpan diff = wxDateTime::UNow() - dragstarttime;
if (diff.GetMilliseconds() < 100)  return;      // If 0.1 sec hasn't passed since dragging was first noticed, return

if (!originaltree->IsSelected(dragitem)) { ResetPreDnD(); return; }  // If we've got this far, we can trigger provided that by now the item has become selected. Otherwise abort the process

                          // If we're here, there's a genuine drag event
ResetPreDnD();                                  // Reset dragging & startpt for the future

if (MyFrame::mainframe->m_TreeDragMutex.TryLock() != wxMUTEX_NO_ERROR) 
  { wxLogTrace(wxT("%s"),wxT("MyTreeCtrl::OnBeginDrag - - - - - - - - - - - - - Mutex was locked")); 
    MyFrame::mainframe->m_TreeDragMutex.Unlock(); // If we don't unlock it here, DnD will never again work.  Surely it will be safe by now . . . .?
    return; 
  }

size_t count;                                   // No of selections (may be multiple, after all) 
wxArrayString paths;                            // Array to store them

count = parent->GetMultiplePaths(paths);        // Fill the array with selected pathname/filenames
if (!count) return;

MyGenericDirCtrl::DnDfilearray = paths;         // Fill the DnD "clipboard" with paths
MyGenericDirCtrl::DnDfilecount = count;         // Store their number
MyGenericDirCtrl::DnDFromID = parent->GetId();  // and the window ID (for refreshing)  Note the reasonable assumption that all selections belong to same pane

MyFrame::mainframe->m_CtrlPressed = event.m_controlDown;  // Store metakey pattern, so that MyFrame::OnBeginDrag will know if we're copying, moving etc 
MyFrame::mainframe->m_ShPressed = event.ShiftDown();
MyFrame::mainframe->m_AltPressed = event.AltDown();

MyFrame::mainframe->OnBeginDrag(this);              // Pass control to main Drag functions
}

void MyTreeCtrl::OnEndDrag(wxTreeEvent& event)
{
wxYieldIfNeeded();
                    // Now we need to find if we've found somewhere to drop onto
wxPoint pt = ScreenToClient(wxGetMousePosition());          // Locate the mouse?  Screen coords so -> client
int flags;
wxTreeItemId item = HitTest(pt, flags);                     // Find out if we're over a valid tree item (& not in the space to the right of it)
if (item.IsOk()  && !(flags & wxTREE_HITTEST_ONITEMRIGHT))
  { wxDirItemData* data = (wxDirItemData*) GetItemData(item);  
    MyGenericDirCtrl::DnDDestfilepath = data->m_path;       //   so get its path
    MyGenericDirCtrl::DnDToID = parent->GetId();            //     and its ID (for refreshing)
    if (parent->fileview == ISRIGHT)                        // However, if we're in a fileview pane, need to check that we're not dropping on top of a file
      { FileData fd(MyGenericDirCtrl::DnDDestfilepath);
        if (fd.IsSymlinktargetADir(true))                   // If we're dropping onto a symlink & its target is a dir
          MyGenericDirCtrl::DnDDestfilepath = fd.GetUltimateDestination(); // replace path with the symlink target. NB If the target isn't a dir, we don't want to deref the symlink at all; just paste into its parent dir as usual
         else
          if (fd.IsValid() && !fd.IsDir()) MyGenericDirCtrl::DnDDestfilepath = fd.GetPath();  // If the overall path isn't a dir, remove the filename, so we drop onto the parent dir
      }
  }
 else
  { MyGenericDirCtrl::DnDDestfilepath = wxEmptyString;      // If HitTest() missed,  mark invalid 
    MyFrame::mainframe->m_TreeDragMutex.Unlock();  
    return;                                                 // and bail out
  }
if (!MyGenericDirCtrl::DnDfilecount) return;

enum myDragResult dragtype = MyFrame::mainframe->ParseMetakeys();  // First find out if we've been moving, copying or linking

bool ClusterWasNeeded = UnRedoManager::StartClusterIfNeeded();
switch (dragtype)
 {
  case myDragCopy:      parent->OnPaste(true); break;
  case myDragMove:      {DevicesStruct* ds = MyFrame::mainframe->Layout->m_notebook->
                                      DeviceMan->QueryAMountpoint(MyGenericDirCtrl::DnDfilearray[0], true); // See if we're trying to drag from a ro device
                        if (ds != NULL && ds->readonly)  parent->OnPaste(true);  // If we _are_ trying to move from a cdrom or similar, silently alter to Paste
                         else parent->OnDnDMove(); break;}                           // Otherwise do a Move as requested
  case myDragHardLink: 
  case myDragSoftLink:  parent->OnLink(true, dragtype); break;
  case myDragNone:      break;
   default:  break;
 }
if (ClusterWasNeeded && !(dragtype==myDragCopy || dragtype==myDragMove)) UnRedoManager::EndCluster(); // Don't end the cluster if threads are involved; it'll be too soon
 
MyFrame::mainframe->m_TreeDragMutex.Unlock();               // Finally, release the tree-mutex lock
}

void MyTreeCtrl::OnPaint(wxPaintEvent& event)  // // Copied from wxTreeCtrl only because it's the caller of the overridden (PaintLevel()->) PaintItem
{
if (parent->fileview == ISLEFT) return wxTreeCtrl::OnPaint(event);    // // If it's a dirview, use the original version

Col0 = STRIPE_0; Col1 = STRIPE_1;  // // Copy these at the beginning of the paint so, if the defaults get changed halfway thru, we don't end up looking stupid
    wxPaintDC dc(this);
    PrepareDC(dc);

    if (!m_anchor)
        return;

    dc.SetFont(m_normalFont);
    dc.SetPen(m_dottedPen);

    // this is now done dynamically
    //if(GetImageList() == NULL)
    // m_lineHeight = (int)(dc.GetCharHeight() + 4);

    int y = 2;
    PaintLevel(m_anchor, dc, 0, y, 0);
}

void MyTreeCtrl::PaintLevel(wxGenericTreeItem *item, wxDC &dc, int level, int &y, int index)  // // I've added index
{
    int x = level*m_indent;
    if (!HasFlag(wxTR_HIDE_ROOT))
    {
        x += m_indent;
    }
    else 
    { if (level == 0)
        {
          // always expand hidden root
          int origY = y;
          wxArrayGenericTreeItems& children = item->GetChildren();
          int count = children.Count();
          if (count > 0)
          {
        int n = 0, oldY;
        do {
            oldY = y;
            PaintLevel(children[n], dc, 1, y, n);
        } while (++n < count);

        if (!HasFlag(wxTR_NO_LINES) && HasFlag(wxTR_LINES_AT_ROOT) && count > 0)
        {
            // draw line down to last child
            origY += GetLineHeight(children[0])>>1;
            oldY += GetLineHeight(children[n-1])>>1;
            dc.DrawLine(3, origY, 3, oldY);
        }
          }
          return;
        }
        else
        {
#if defined(__WXGTK3__)
            // Fiddle-factor for gtk3 (at least in fedora 20, & does no harm elsewhere). Otherwise the left edge of each dir icon is lost
            x += 5;
#endif
        }
    }

    item->SetX(x+m_spacing);
    item->SetY(y);

    int h = GetLineHeight(item);
    int y_top = y;
    int y_mid = y_top + (h>>1);
    y += h;

    int exposed_x = dc.LogicalToDeviceX(0);
    int exposed_y = dc.LogicalToDeviceY(y_top);

    if (IsExposed(exposed_x, exposed_y, 10000, h))  // 10000 = very much
    {
        const wxPen *pen =
#ifndef __WXMAC__
            // don't draw rect outline if we already have the
            // background color under Mac
            (item->IsSelected() && m_hasFocus) ? wxBLACK_PEN :
#endif // !__WXMAC__
            wxTRANSPARENT_PEN;

        wxColour colText;
        if (item->IsSelected())
        {
            colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
        }
        else
        {
            wxTreeItemAttr *attr = item->GetAttributes();
            if (attr && attr->HasTextColour())
                colText = attr->GetTextColour();
            else
                colText = GetForegroundColour();
        }

        // prepare to draw
        dc.SetTextForeground(colText);
        dc.SetPen(*pen);

        // draw
        PaintItem(item, dc, index);

        if (HasFlag(wxTR_ROW_LINES))
        {
            // if the background colour is white, choose a
            // contrasting color for the lines
            dc.SetPen(*((GetBackgroundColour() == *wxWHITE)
                         ? wxMEDIUM_GREY_PEN : wxWHITE_PEN));
            dc.DrawLine(0, y_top, 10000, y_top);
            dc.DrawLine(0, y, 10000, y);
        }

        // restore DC objects
        dc.SetBrush(*wxWHITE_BRUSH);
        dc.SetPen(m_dottedPen);
        dc.SetTextForeground(*wxBLACK);

        if (item->HasPlus() && HasButtons())  // should the item show a button?
        {
            if (!HasFlag(wxTR_NO_LINES))
            {
                if (x > (signed)m_indent)
                    dc.DrawLine(x - m_indent, y_mid, x - 5, y_mid);
                else if (HasFlag(wxTR_LINES_AT_ROOT))
                    dc.DrawLine(3, y_mid, x - 5, y_mid);
                dc.DrawLine(x + 5, y_mid, x + m_spacing, y_mid);
            }

            if (m_imageListButtons != NULL)
            {
                // draw the image button here
                int image_h = 0, image_w = 0, image = wxTreeItemIcon_Normal;
                if (item->IsExpanded()) image = wxTreeItemIcon_Expanded;
                if (item->IsSelected())
                    image += wxTreeItemIcon_Selected - wxTreeItemIcon_Normal;
                m_imageListButtons->GetSize(image, image_w, image_h);
                int xx = x - (image_w>>1);
                int yy = y_mid - (image_h>>1);
                dc.SetClippingRegion(xx, yy, image_w, image_h);
                m_imageListButtons->Draw(image, dc, xx, yy,
                                         wxIMAGELIST_DRAW_TRANSPARENT);
                dc.DestroyClippingRegion();
            }

    }
  }
           else // no custom buttons
            {
                static const int wImage = 9;
                static const int hImage = 9;
                
                int flag = 0;
                if (item->IsExpanded())
                    flag |= wxCONTROL_EXPANDED;
                if (item == m_underMouse)
                    flag |= wxCONTROL_CURRENT;
                                            
                wxRendererNative::Get().DrawTreeItemButton
                                        (
                                            this,
                                            dc,
                                            wxRect(x - wImage/2,
                                                   y_mid - hImage/2,
                                                   wImage, hImage),
                                            flag
                                    );
            }



    if (item->IsExpanded())
    {
        wxArrayGenericTreeItems& children = item->GetChildren();
        int count = children.Count();
        if (count > 0)
        {
            int n = 0, oldY;
            ++level;
            do {
                oldY = y;
                PaintLevel(children[n], dc, level, y);
            } while (++n < count);

            if (!HasFlag(wxTR_NO_LINES) && count > 0)
            {
                // draw line down to last child
                oldY += GetLineHeight(children[n-1])>>1;
                if (HasButtons()) y_mid += 5;

                // Only draw the portion of the line that is visible, in case it is huge
                wxCoord  xOrigin=0, yOrigin=0, width, height;
                dc.GetDeviceOrigin(&xOrigin, &yOrigin);
                yOrigin = abs(yOrigin);
                GetClientSize(&width, &height);

                // Move end points to the begining/end of the view?
                if (y_mid < yOrigin)
                    y_mid = yOrigin;
                if (oldY > yOrigin + height)
                    oldY = yOrigin + height;

                // after the adjustments if y_mid is larger than oldY then the line
                // isn't visible at all so don't draw anything
                if (y_mid < oldY)
                    dc.DrawLine(x, y_mid, x, oldY);
            }
        }
    }
}

void MyTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc, int index)
{
static const int NO_IMAGE = -1;                                  // //
    // TODO implement "state" icon on items

    wxTreeItemAttr *attr = item->GetAttributes();
    if (attr && attr->HasFont())
        dc.SetFont(attr->GetFont());
    else if (item->IsBold())
        dc.SetFont(m_boldFont);

    int text_w = 0, text_h = 0;
    
    dc.GetTextExtent(item->GetText(), &text_w, &text_h);

    int total_h = GetLineHeight(item);

    if (item->IsSelected())
    {
#if wxVERSION_NUMBER < 3104
        dc.SetBrush(*(m_hasFocus ? m_hilightBrush : m_hilightUnfocusedBrush));
#else
        dc.SetBrush(m_hasFocus ? m_hilightBrush : m_hilightUnfocusedBrush);
#endif
    }
    else
    {
        wxColour colBg;
        if (attr && attr->HasBackgroundColour())
            colBg = attr->GetBackgroundColour();
        else
      if (USE_STRIPES)                                          // // If we want the fileview background to be striped with alternate colours
              colBg = (index % 2) ? Col1 : Col0;
       else colBg = m_backgroundColour;

#if wxVERSION_NUMBER < 2900
        dc.SetBrush(wxBrush(colBg, wxSOLID));
#else
        dc.SetBrush(wxBrush(colBg, wxBRUSHSTYLE_SOLID));
#endif
    }

    int offset = HasFlag(wxTR_ROW_LINES) ? 1 : 0;

    dc.DrawRectangle(0, item->GetY()+offset,
         headerwindow->GetWidth(),                                  // //
         total_h-offset);

    dc.SetBackgroundMode(wxTRANSPARENT);
    int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0;
    int extra_offset = 0;

DataBase* stat = NULL;                                            // //
          // // After simplifying (& so probably speeding up) DirGenericDirCtrl::ReloadTree(), I started getting rare Asserts where index==GetCount() during Moving/Pasting
          // // I suspect data is sometimes being added to the treectrl faster than to the array, If a refresh then occurs at the wrong time . . .
if (((FileGenericDirCtrl*)parent)->CombinedFileDataArray.GetCount() <= (size_t)index)  return;  // //
stat = &((FileGenericDirCtrl*)parent)->CombinedFileDataArray.Item(index); // // Get a ptr to the current stat data
if (!stat) return;                                                        // // With USE_FSWATCHER this avoids rare assert, presumably a race

for(size_t i = 0; i < headerwindow->GetColumnCount(); ++i)                // //
 {
if (headerwindow->IsHidden(i)) continue;                                  // // Not a lot to do for a hidden column

  int coord_x = extra_offset, image_x = coord_x;
  int clip_width = headerwindow->GetColumnWidth(i);                       // //
  int image_h = 0, image_w = 0; //2;
  int image = NO_IMAGE;
  
  if(i == 0) {                                                            // //
      image = item->GetCurrentImage();
      coord_x = item->GetX();
          }
  else {
      image = NO_IMAGE;  // //item->GetImage(i);
      }
  
  if(image != NO_IMAGE) 
   {
      if(m_imageListNormal) {
    m_imageListNormal->GetSize(image, image_w, image_h);
    image_w += 4;
                          }
      else {
    image = NO_IMAGE;
          }
   }

  // honor text alignment
  wxString text("?"); // // If the file is corrupt, display _something_
  if (!i)                                                     // // If col 0, do it the standard way
    text = stat->ReallyGetName();                             // // but use stat->ReallyGetName() as this is might hold a useful string even if the file is corrupt

   else                                                       // // The whole 'else' is mine
    { const static time_t YEAR2038(2145916800);
      wxDateTime time;
      if (stat && !headerwindow->IsHidden(i))
        switch(i)
        { case ext:        if (stat->IsValid())
                             {  wxString fname(stat->GetFilename().Mid(1));       // Mid(1) to avoid hidden-file confusion
                                if (EXTENSION_START == 0)                         // Use the first dot. This one's easy: if AfterFirst() fails it returns ""
                                  { text = fname.AfterFirst(wxT('.')); break; }
                                 else
                                  { size_t pos = fname.rfind(wxT('.'));
                                    if (pos != wxString::npos)
                                      { text = fname.Mid(pos+1);                  // We've found a last dot. Store the remaining string
                                        if (EXTENSION_START == 1)                 // and then, if so configured, look for a penultimate one
                                          { pos = fname.rfind(wxT('.'), pos-1);
                                            if (pos != wxString::npos)
                                              text = fname.Mid(pos+1);
                                          }
                                      }
                                      else text.Clear(); // We don't want to display '?' just because there's no ext
                                  }
                              }
                            break;
        
          case filesize:    if (stat->IsValid()) text = stat->GetParsedSize();
                            break;
          case modtime:     if (stat->IsValid() && stat->ModificationTime() < YEAR2038) // If it's > 2038 the datetime is highly likely to be invalid, and will assert below
                              { time.Set(stat->ModificationTime());
                                if (time.IsValid())
                                  text = time.Format("%x %R"); // %x means d/m/y but arranged according to locale. %R is %H:%M
                              }
                             else
                              text = "00/00/00";
                            break;
          case permissions: if (stat->IsValid()) text = stat->PermissionsToText();
                            break;
          case owner:       if (stat->IsValid()) text = stat->GetOwner(); if (text.IsEmpty())  text.Printf(wxT("%u"), stat->OwnerID());
                            break;
          case group:       if (stat->IsValid()) text = stat->GetGroup(); if (text.IsEmpty())  text.Printf(wxT("%u"), stat->GroupID());
                            break;
          case linkage:     if (stat->IsValid() && stat->IsSymlink())
                              { if (stat->GetSymlinkData() && stat->GetSymlinkData()->IsValid())
                                  text.Printf(wxT("-->%s"), stat->GetSymlinkDestination(true).c_str());
                                 else text = _(" *** Broken symlink ***");
                              }
                             else text.Clear(); // We don't want to display '?' just because it's not a link
        }    
    }
    
  switch(headerwindow->GetColumn(i).GetAlignment()) {
  case wxTL_ALIGN_LEFT:
      coord_x += image_w + 2;
      image_x = coord_x - image_w;
      break;
  case wxTL_ALIGN_RIGHT:
      dc.GetTextExtent(text, &text_w, NULL);
      coord_x += clip_width - text_w - image_w - 2;
      image_x = coord_x - image_w;
      break;
  case wxTL_ALIGN_CENTER:
      dc.GetTextExtent(text, &text_w, NULL);
      //coord_x += (clip_width - text_w)/2 + image_w;
      image_x += (clip_width - text_w - image_w)/2 + 2;
      coord_x = image_x + image_w;
  }  

  wxDCClipper clipper(dc, /*coord_x,*/ extra_offset,
          item->GetY() + extraH, clip_width,
          total_h);

  if(image != NO_IMAGE) {
      m_imageListNormal->Draw(image, dc, image_x,
             item->GetY() +((total_h > image_h)?
                ((total_h-image_h)/2):0),
             wxIMAGELIST_DRAW_TRANSPARENT);
  }

  dc.DrawText(text,
         (wxCoord)(coord_x /*image_w + item->GetX()*/),
         (wxCoord)(item->GetY() + extraH));
  extra_offset += headerwindow->GetColumnWidth(i);
 }

    // restore normal font
    dc.SetFont(m_normalFont);
}


void MyTreeCtrl::OnIdle(wxIdleEvent& event)  // // Makes sure any change in column width is reflected within
{
if (headerwindow==NULL) { event.Skip(); return; }   // Necessary as event may be called before there IS a headerwindow  

if (headerwindow->m_dirty)                          // Although MyTreeCtrl has an m_dirty too, that's just there because TreeListHeaderWindow writes to it!
  { headerwindow->m_dirty = false;                  // The one that is changed when columns are resized is the TreeListHeaderWindow one
    Refresh();
  }

event.Skip();                                       // Without this, gtk1 has focus problems; and in X11 the scrollbars get stuck in the top-left corner on creation!
}

void MyTreeCtrl::OnSize(wxSizeEvent& event)  // // Keeps the treectrl columns in step with the headerwindows
{
if (headerwindow) headerwindow->m_dirty = true;     // In >=2.5.1, the headerwindows don't otherwise seem to get refreshed on splitter dragging

AdjustMyScrollbars();

event.Skip();
}

void MyTreeCtrl::ScrollToOldPosition(const wxTreeItemId& item) // An altered ScrollTo() that tries as much as possible to maintain what the user had seen
{
wxCHECK_RET(!IsFrozen(), wxT("DoDirtyProcessing() won't work when frozen, at least in 2.9"));

    if (!item.IsOk()) return;
    if (m_dirty)
        DoDirtyProcessing();

    wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
    int item_y = gitem->GetY();

static const int PIXELS_PER_UNIT = 10;    
const int fiddlefactor = 1;
    int start_x = 0; int start_y = 0;
    GetViewStart( &start_x, &start_y );

#if wxVERSION_NUMBER < 2900
    start_y *= PIXELS_PER_UNIT;

    int client_h = 0; int client_w = 0;
    GetClientSize( &client_w, &client_h );

    //if (item_y < start_y+3)
    {
        // going down
        int x = 0;
        int y = 0;
        m_anchor->GetSize( x, y, this );
        y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
        x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
        int x_pos = GetScrollPos( wxHORIZONTAL );
        // Item should appear at top
        SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, (item_y/PIXELS_PER_UNIT) + fiddlefactor);
    }
/*    else if (item_y+GetLineHeight(gitem) > start_y+client_h)
    {
        // going up
        int x = 0;
        int y = 0;
        m_anchor->GetSize( x, y, this );
        y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
        x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
        item_y += PIXELS_PER_UNIT+2;
        int x_pos = GetScrollPos( wxHORIZONTAL );
        // Item should appear at bottom
        SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, (item_y+GetLineHeight(gitem)-client_h)/PIXELS_PER_UNIT );
    }*/
    
#else // 2.9

    const int clientHeight = GetClientSize().y;
    const int itemHeight = GetLineHeight(gitem) + 2;

    if ( item_y + itemHeight > start_y*PIXELS_PER_UNIT + clientHeight )
    {
        // need to scroll up by enough to show this item fully
        item_y += itemHeight - clientHeight;
    }
/*    else if ( itemY > start_y*PIXELS_PER_UNIT )
    {
        // item is already fully visible, don't do anything
        return;
    }*/
    //else: scroll down to make this item the top one displayed

    Scroll(-1, (item_y/PIXELS_PER_UNIT) + fiddlefactor);
#endif
}

void MyTreeCtrl::AdjustMyScrollbars()  // // Taken from src/generic/treectlg.cpp, because of the headerwindow line
{
const int PIXELS_PER_UNIT = 10;  // // This was set in src/generic/treectlg.cpp

if (m_anchor)
    {
        int x = 0, y = 0;
        m_anchor->GetSize(x, y, this);
    if (headerwindow != NULL)  x = headerwindow->GetTotalPreferredWidth();  // // This means we get the column width, not just that of m_anchor's name

    y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
      // //  x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels                This actually makes things worse
        int x_pos = GetScrollPos(wxHORIZONTAL);
        int y_pos = GetScrollPos(wxVERTICAL);
        SetScrollbars(PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos);
    }
 else
    {
        SetScrollbars(0, 0, 0, 0);
    }
}

void MyTreeCtrl::ScrollToMidScreen(const wxTreeItemId& item, const wxString& START_DIR) // Improved version of wxGenericTreeCtrl::ScrollTo (which seems to show the item at the top/bottom)
{
if (!item.IsOk())
  return;

// const int PIXELS_PER_UNIT = 10;  // // This was set in src/generic/treectlg.cpp but the method below is more future-proof
int xScrollUnits, PIXELS_PER_UNIT;
GetScrollPixelsPerUnit(&xScrollUnits, &PIXELS_PER_UNIT);


wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
int itemY = gitem->GetY();

int viewstart_x = 0;
int viewstart_y = 0;
GetViewStart( &viewstart_x, &viewstart_y );

const int clientHeight = GetClientSize().y;
int ScrollPageSize = GetScrollPageSize(wxVERTICAL);
const int itemHeight = GetLineHeight(gitem) + 2;

if ( itemY + itemHeight > viewstart_y*PIXELS_PER_UNIT + clientHeight )
{ // ** I've left this branch here but, for our use-case, it's never entered **
    // need to scroll down by enough to show this item fully
    itemY += itemHeight - (clientHeight/2);

    // because itemY below will be divided by PIXELS_PER_UNIT it may
    // be rounded down, with the result of the item still only being
    // partially visible, so make sure we are rounding up
    itemY += PIXELS_PER_UNIT - 1;

    Scroll(-1, (itemY - (clientHeight/2))/PIXELS_PER_UNIT);
}
/*else if ( itemY > viewstart_y*PIXELS_PER_UNIT )
{
    // item is already fully visible, don't do anything
   // return;
}*/

 else
  Scroll(-1, (viewstart_y + (ScrollPageSize/2)));
}


void MyTreeCtrl::OnScroll(wxScrollWinEvent& event)  // // Keeps the headerwindows in step with the treectrl colums
{
if (headerwindow != NULL) headerwindow->Refresh();
event.Skip();
}

#if !defined(__WXGTK3__)
void MyTreeCtrl::OnEraseBackground(wxEraseEvent& event)  // This is only needed in gtk2 under kde and brain-dead theme-manager, but can cause a blackout in some gtk3(themes?)
{
wxClientDC* clientDC = NULL;  // We may or may not get a valid dc from the event, so be prepared
if (!event.GetDC()) clientDC = new wxClientDC(this);
wxDC* dc = clientDC ? clientDC : event.GetDC();

wxColour colBg = GetBackgroundColour();  // Use the chosen background if there is one
if (!colBg.Ok()) colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);

#if wxVERSION_NUMBER < 2900
  dc->SetBackground(wxBrush(colBg, wxSOLID));
#else
  dc->SetBackground(wxBrush(colBg, wxBRUSHSTYLE_SOLID));
#endif
dc->Clear();

if (clientDC) delete clientDC;
}
#endif

#if wxVERSION_NUMBER > 3102
void MyTreeCtrl::Expand(const wxTreeItemId& itemId)  // Needed in wx3.2 as otherwise non-fulltree dirview roots don't get children added. See wx git a6b92cb313 and https://trac.wxwidgets.org/ticket/13886
{
wxGenericTreeItem *item = (wxGenericTreeItem*)itemId.m_pItem;

wxCHECK_RET( item, wxT("invalid item in wxGenericTreeCtrl::Expand") );
wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT) || itemId != GetRootItem(),
              wxT("can't expand hidden root") );

if (!item->HasPlus())    return;

if (item->IsExpanded())  return;

wxTreeEvent event(wxEVT_TREE_ITEM_EXPANDING, this, item);
if (GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed())    return;    // cancelled by program

item->Expand();
if (!IsFrozen())
  { CalculatePositions();
    RefreshSubtree(item);
  }
else m_dirty = true;  // frozen

event.SetEventType(wxEVT_TREE_ITEM_EXPANDED);
GetEventHandler()->ProcessEvent(event);
}

#endif

BEGIN_EVENT_TABLE(MyTreeCtrl,wxTreeCtrl)
    EVT_KEY_DOWN(MyTreeCtrl::AdjustForNumlock)
    EVT_SET_FOCUS(MyTreeCtrl::OnReceivingFocus)
    EVT_KILL_FOCUS(MyTreeCtrl::OnLosingFocus)
    EVT_MENU(SHCUT_SELECTALL,MyTreeCtrl::OnCtrlA)
    EVT_RIGHT_DOWN(MyTreeCtrl::OnRtDown)            // Just swallows the event, to stop it triggering an unwanted context menu in >GTK-2.5.3
    EVT_RIGHT_UP(MyTreeCtrl::OnRtUp)                // Takes over the context menu generation
    EVT_LEFT_DOWN(MyTreeCtrl::OnLtDown)
    EVT_MOTION(MyTreeCtrl::OnMouseMovement)
    EVT_MIDDLE_DOWN(MyTreeCtrl::OnMiddleDown)       // Allows middle-button paste
    EVT_LEAVE_WINDOW(MyTreeCtrl::OnLeavingWindow)
    EVT_PAINT          (MyTreeCtrl::OnPaint)
    #if !defined(__WXGTK3__)
      EVT_ERASE_BACKGROUND(MyTreeCtrl::OnEraseBackground)
    #endif
    EVT_IDLE(MyTreeCtrl::OnIdle)
    EVT_SIZE(MyTreeCtrl::OnSize)
    EVT_SCROLLWIN(MyTreeCtrl::OnScroll)
END_EVENT_TABLE()

//---------------------------------------------------------------------------

#include <wx/mimetype.h>
#include <wx/txtstrm.h>
#include <wx/wfstream.h>

wxTreeItemId PreviewManager::m_LastItem = wxTreeItemId();
PreviewManagerTimer* PreviewManager::m_Timer = NULL;
wxWindow* PreviewManager::m_Tree = NULL;
wxString PreviewManager::m_Filepath;
wxPoint PreviewManager::m_InitialPos = wxPoint();
PreviewPopup* PreviewManager::m_Popup = NULL;
size_t PreviewManager::m_DwellTime;
int PreviewManager::MAX_PREVIEW_IMAGE_HT;
int PreviewManager::MAX_PREVIEW_IMAGE_WT;
int PreviewManager::MAX_PREVIEW_TEXT_HT;
int PreviewManager::MAX_PREVIEW_TEXT_WT;

PreviewManager::~PreviewManager()
{
Clear();
delete m_Timer;  m_Timer = NULL;
}

//static
void PreviewManager::Init()
{
wxConfigBase* config = wxConfigBase::Get();

m_DwellTime = (size_t)config->Read(wxT("/Misc/Display/Preview/dwelltime"), 1000l);
MAX_PREVIEW_IMAGE_WT = (int)config->Read(wxT("/Misc/Display/Preview/MAX_PREVIEW_IMAGE_WT"), 100l);
MAX_PREVIEW_IMAGE_HT = (int)config->Read(wxT("/Misc/Display/Preview/MAX_PREVIEW_IMAGE_HT"), 100l);
MAX_PREVIEW_TEXT_WT = (int)config->Read(wxT("/Misc/Display/Preview/MAX_PREVIEW_TEXT_WT"), 300l);
MAX_PREVIEW_TEXT_HT = (int)config->Read(wxT("/Misc/Display/Preview/MAX_PREVIEW_TEXT_HT"), 300l);
}

//static
void PreviewManager::Save(int Iwt, int Iht, int Twt, int Tht, size_t dwell)
{
wxConfigBase* config = wxConfigBase::Get();

config->Write(wxT("/Misc/Display/Preview/dwelltime"), (long)dwell);
config->Write(wxT("/Misc/Display/Preview/MAX_PREVIEW_IMAGE_WT"), (long)Iwt);
config->Write(wxT("/Misc/Display/Preview/MAX_PREVIEW_IMAGE_HT"), (long)Iht);
config->Write(wxT("/Misc/Display/Preview/MAX_PREVIEW_TEXT_WT"), (long)Twt);
config->Write(wxT("/Misc/Display/Preview/MAX_PREVIEW_TEXT_HT"), (long)Tht);

Init(); // We get here when the user has changed the values; so reload to keep the vars current
}

//static
void PreviewManager::ProcessMouseMove(wxTreeItemId item, int flags, const wxString& filepath, wxMouseEvent& event)
{
if (item.IsOk() && !(flags & wxTREE_HITTEST_ONITEMRIGHT)) // Find out if we're near a tree item (& not in the space to the right of it)
  { if (item != m_LastItem)
      { Clear();
        m_LastItem = item; m_Tree = (wxWindow*)event.GetEventObject(); m_InitialPos = wxGetMousePosition();

        bool InArchive = false;                           // We might be trying to extract from an archive into this editor
        MyGenericDirCtrl* active = MyFrame::mainframe->GetActivePane();
        if (active != NULL && active->arcman != NULL && active->arcman->IsArchive())   InArchive = true;

        if (InArchive)
          { FakeFiledata ffd(filepath); if (!ffd.IsValid()) return;
            wxArrayString arr; arr.Add(filepath);
            wxArrayString tempfilepaths = active->arcman->ExtractToTempfile(arr, false, true);  // This extracts filepath to a temporary file, which it returns. The bool params mean not-dirsonly, files-only
            if (tempfilepaths.IsEmpty()) return;          // Empty == failed
            
            FileData tfp(tempfilepaths[0]); tfp.DoChmod(0444);  // Make the file readonly, as we won't be saving any alterations and exec.ing it is unlikely to be sensible
            m_Filepath = tempfilepaths[0];
          }
         else
          { FileData fd(filepath);
            if (fd.IsDir() || fd.IsSymlinktargetADir() || !fd.IsValid()) return;
            m_Filepath = fd.GetUltimateDestination();
          }

        if (!m_Timer) m_Timer = new PreviewManagerTimer();
        m_Timer->Start(m_DwellTime, true);
      }
  }

 else Clear();
}

//static
void PreviewManager::Clear()
{
if (m_Timer) m_Timer->Stop();
m_LastItem = wxTreeItemId();
m_Filepath.Clear();
m_InitialPos = wxPoint();

if (m_Popup) 
  { m_Popup->Dismiss();
    m_Popup->Destroy();
    m_Popup = NULL;
  }
}

//static
void PreviewManager::ClearIfNotInside()
{
if (!m_Popup) 
  return;

  // If we're here, it's because the mouse moved out of the treectrl. However we don't want to close if it's actually _inside_ the popup
wxPoint pt = wxGetMousePosition();
if (!m_Popup->GetRect().Contains(pt)) 
  Clear();
}

//static
void PreviewManager::OnTimer()
{
wxPoint pt = wxGetMousePosition();
if (pt.y - m_InitialPos.y > 30)
  { Clear(); return; } // We must originally have been called on the bottom item, and now we're some way below it. So abort

delete m_Popup;
m_Popup = new PreviewPopup(m_Tree);
if (!m_Popup->GetCanDisplay()) return; // Not an image/txtfile*

wxSize ps = m_Popup->GetPanelSize(); // Ensure the popup will fit on the screen
if (pt.y < ps.GetHeight()) pt.y = ps.GetHeight() + 2;
if (pt.x + ps.GetWidth() >= wxGetClientDisplayRect().GetWidth()) 
  pt.x -= (pt.x + ps.GetWidth() - wxGetClientDisplayRect().GetWidth() + 2);

pt.x += 1; // +1 to prevent very small bitmaps from touching the pointer and immediately deleting
m_Popup->Position(pt, wxSize(0, -ps.GetHeight()));

m_Popup->Popup(m_Tree);
m_Popup->SetSize(m_Popup->GetPanelSize()); // Otherwise it doesn't seem to know its size
}

PreviewPopup::PreviewPopup(wxWindow* parent) : wxPopupTransientWindow(parent), m_CanDisplay(false), m_Size(wxSize())
{
wxString filepath = PreviewManager::GetFilepath();
wxCHECK_RET(!filepath.empty(), wxT("PreviewManager has an empty filepath"));

if (IsText(filepath)) // Try for text first: *.c files seem to return true from wxImage::CanRead :/
  { DisplayText(filepath); m_CanDisplay = true; }
 else if (IsImage(filepath))
  { DisplayImage(filepath); m_CanDisplay = true; }

Bind(wxEVT_LEAVE_WINDOW, &PreviewPopup::OnLeavingWindow, this);
}

bool PreviewPopup::IsImage(const wxString& filepath)
{
wxCHECK_MSG(!filepath.empty(), false, wxT("An empty filepath passed to IsImage()"));
wxLogNull NoErrorMessages;

if (filepath.Right(4) == ".svg") return true;
return (wxImage::CanRead(filepath));
}

bool PreviewPopup::IsText(const wxString& filepath)
{
wxCHECK_MSG(!filepath.empty(), false, wxT("An empty filepath passed to IsText()"));
wxLogNull NoErrorMessages;
wxString mt, ext = filepath.AfterLast(wxT('.')), filename = filepath.AfterLast(wxT('/')).Lower();

  // Do some specials first: not recognised by wxTheMimeTypesManager, but can usefully be previewed
  // Do 'txt' too, as wxTheMimeTypesManager doesn't spot this in some distros :/
if (ext == wxT("txt") || ext == wxT("sh") || ext == wxT("xrc") || ext == wxT("in") || ext == wxT("bkl") || filename == wxT("makefile") || filename == wxT("readme"))
  return true;

wxFileType* ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
if (!ft) return false;

bool result(false);
if (ft->GetMimeType(&mt))
  { if (mt.StartsWith(wxT("text")))
      result = true;
  }

delete ft;
return result;
}

void PreviewPopup::DisplayImage(const wxString& fpath)
{
wxLogNull NoErrorMessages;
wxString filepath(fpath);
wxString pngfilepath;
wxImage image;

if (filepath.Right(4) == ".svg")
  { void* handle = wxGetApp().GetRsvgHandle();
    if (!handle) return; // Presumably librsvg is not available at present
    
    wxFileName fn(filepath); // Create a filepath in /tmp/ to store the .png
    pngfilepath = "/tmp/" + fn.GetName() + ".png";
    if (SvgToPng(filepath, pngfilepath, handle))
      image = wxImage(pngfilepath);
    wxRemoveFile(pngfilepath);
  }
 else
   image = wxImage(filepath);


if (!image.IsOk()) return;

int ht = wxMin(image.GetHeight(), PreviewManager::MAX_PREVIEW_IMAGE_HT);
int wt = wxMin(image.GetWidth(), PreviewManager::MAX_PREVIEW_IMAGE_WT);
if (ht != image.GetHeight() || wt != image.GetWidth())
  { if (image.GetHeight() != image.GetWidth())
      { bool wider = image.GetWidth() > image.GetHeight(); // Scale retaining the aspect ratio
        if (wider) ht = (int)(ht * ((float)image.GetHeight()/(float)image.GetWidth()));
         else      wt = (int)(wt * ((float)image.GetWidth()/(float)image.GetHeight()));
      }
  
    image.Rescale(wt, ht);
  }

wxPanel* panel = new wxPanel(this, wxID_ANY);
panel->SetBackgroundColour(*wxLIGHT_GREY);

wxStaticBitmap* bitmap = new wxStaticBitmap(panel, wxID_ANY, wxBitmap(image));

wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
topSizer->Add(bitmap, 0);

panel->SetClientSize(wxSize(wt,ht));
panel->SetSizer(topSizer);

m_Size = wxSize(wt,ht);
}

void PreviewPopup::DisplayText(const wxString& filepath)
{
wxLogNull NoErrorMessages;
wxPanel* toppanel = new wxPanel(this, wxID_ANY);
toppanel->SetBackgroundColour(*wxLIGHT_GREY);

wxPanel* whitepanel = new wxPanel(toppanel, wxID_ANY);
whitepanel->SetBackgroundColour(*wxWHITE);

wxString previewstring;
wxFileInputStream fis(filepath);
if (!fis.IsOk()) return;
wxTextInputStream tis(fis);
for (size_t n=0; n < 30; ++n)
  { if (!fis.CanRead()) break;
    previewstring << tis.ReadLine() << wxT('\n');
  }

wxStaticText* text = new wxStaticText(whitepanel, wxID_ANY, previewstring);
wxBoxSizer* whitesizer = new wxBoxSizer(wxVERTICAL);
whitesizer->Add(text, 1, wxEXPAND);
whitepanel->SetSizer(whitesizer);
whitepanel->SetClientSize(PreviewManager::MAX_PREVIEW_TEXT_WT-4, PreviewManager::MAX_PREVIEW_TEXT_HT-4);

wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
topSizer->Add(whitepanel, 1, wxEXPAND | wxALL, 2);

toppanel->SetSizer(topSizer);
toppanel->SetClientSize(wxSize(PreviewManager::MAX_PREVIEW_TEXT_WT,PreviewManager::MAX_PREVIEW_TEXT_HT));

m_Size = wxSize(PreviewManager::MAX_PREVIEW_TEXT_WT, PreviewManager::MAX_PREVIEW_TEXT_HT);
}

void PreviewPopup::OnLeavingWindow(wxMouseEvent& event)
{
PreviewManager::ClearIfNotInside();
}