a4bfd345fc490a11bcfb601e97772566aec659bc
[ede] / libede / src / FileSystemBox.ec
1 public import "ecere"
2
3 #ifdef __WIN32__
4 static char * rootName = "Entire Computer";
5 static char * msNetwork = "Microsoft Windows Network";
6 #else
7 static char * rootName = "File System";
8 #endif
9
10 private:
11 define guiApp = (GuiApplication)((__thisModule).application);
12 define selectionColor = guiApp.currentSkin.selectionColor; //Color { 10, 36, 106 };
13
14 static char * fileIconNames[] = 
15 {
16    "<:ecere>mimeTypes/file.png",         /* none */
17
18    "<:ecere>mimeTypes/file.png",         /* normalFile */
19    "<:ecere>mimeTypes/textEcereWorkspace.png",          /* ewsFile */
20    "<:ecere>mimeTypes/textEcereProject.png",      /* epjFile */
21    "<:ecere>mimeTypes/textEcereSource.png",         /* ecFile */
22    "<:ecere>mimeTypes/textEcereHeader.png",         /* ehFile */
23    "<:ecere>mimeTypes/textCSource.png",          /* cFile */
24    "<:ecere>mimeTypes/textCHeader.png",          /* hFile */
25    "<:ecere>mimeTypes/textC++Source.png",        /* cppFile */
26    "<:ecere>mimeTypes/textC++Header.png",        /* hppFile */
27    "<:ecere>mimeTypes/text.png",         /* textFile */
28    "<:ecere>mimeTypes/textHyperTextMarkup.png",              /* webFile */
29    "<:ecere>mimeTypes/image.png",        /* pictureFile */
30    "<:ecere>status/audioVolumeHigh.png",         /* soundFile */
31    "<:ecere>mimeTypes/package.png",      /* archiveFile */
32    "<:ecere>mimeTypes/packageSoftware.png",     /* packageFile */
33    "<:ecere>mimeTypes/packageOpticalDisc.png", /* opticalMediaImageFile */
34
35    "<:ecere>places/folder.png",                    /* folder */
36    "<:ecere>status/folderOpen.png",               /* folderOpen */
37    "<:ecere>devices/computer.png",                 /* computer */
38    "<:ecere>devices/driveHardDisk.png",           /* drive */
39    "<:ecere>places/driveRemote.png",              /* netDrive */
40    "<:ecere>devices/mediaOptical.png",            /* cdrom */
41    "<:ecere>devices/driveRemovableMedia.png",    /* removable */
42    "<:ecere>devices/mediaFloppy.png",             /* floppy */
43    "<:ecere>places/networkWorkgroup.png",         /* network */
44    "<:ecere>places/networkServer.png",            /* server */
45    "<:ecere>places/folderRemote.png",             /* share */
46
47    "<:ecere>mimeTypes/package.png",      /* treeLoader */
48    "<:ecere>places/startHere.png",                /* lineNumbers */
49    
50    ""
51 };
52
53 public enum _FileType
54 {
55    none,
56    
57    normalFile, ewsFile, epjFile, ecFile, ehFile, cFile, hFile, cppFile, hppFile,
58    textFile, webFile, pictureFile, soundFile,
59    archiveFile, packageFile, opticalMediaImageFile, /* these (all previous) are sort equal */
60    
61    folder, folderOpen, computer,
62    drive, netDrive, cdrom, removable, floppy, network, server, share, // these are sort equal
63    
64    // utilities
65    treeLoader,
66    lineNumbers;
67
68    /*property char * 
69    {
70       set
71       {
72          this = SelectByExtension(value);
73       }
74    }*/
75
76    public property bool isFolder
77    {
78       get { return this >= folder && this <= share; }
79    }
80
81    public property bool isFile
82    {
83       get { return this >= normalFile && this <= opticalMediaImageFile; }
84    }
85
86    _FileType ::SelectByExtension(char * extension)
87    {
88       if(!strcmpi(extension, "ews"))
89          return ewsFile;
90       else if(!strcmpi(extension, "epj"))
91          return epjFile;
92       else if(!strcmpi(extension, "ec"))
93          return ecFile;
94       else if(!strcmpi(extension, "eh"))
95          return ehFile;
96       else if(!strcmpi(extension, "cpp") ||
97             !strcmpi(extension, "cc") || !strcmpi(extension, "cxx"))
98          return cppFile;
99       else if(!strcmpi(extension, "hpp") ||
100             !strcmpi(extension, "hh") || !strcmpi(extension, "hxx"))
101          return hppFile;
102       else if(!strcmpi(extension, "c"))
103          return cFile;
104       else if(!strcmpi(extension, "h"))
105          return hFile;
106       else if(!strcmpi(extension, "txt") || !strcmpi(extension, "text") ||
107             !strcmpi(extension, "nfo") || !strcmpi(extension, "info"))
108          return textFile;
109       else if(!strcmpi(extension, "htm") || !strcmpi(extension, "html") ||
110             !strcmpi(extension, "css") || !strcmpi(extension, "php") ||
111             !strcmpi(extension, "js"))
112          return webFile;
113       else if(!strcmpi(extension, "bmp") || !strcmpi(extension, "pcx") ||
114             !strcmpi(extension, "jpg") || !strcmpi(extension, "jpeg") ||
115             !strcmpi(extension, "gif") || !strcmpi(extension, "png") ||
116             !strcmpi(extension, "ico"))
117          return pictureFile;
118       else if(!strcmpi(extension, "wav") || !strcmpi(extension, "mp3") ||
119             !strcmpi(extension, "ogg") || !strcmpi(extension, "snd"))
120          return soundFile;
121       else if(!strcmpi(extension, "ear") || !strcmpi(extension, "7z") ||
122             !strcmpi(extension, "rar") || !strcmpi(extension, "zip") ||
123             !strcmpi(extension, "gz") || !strcmpi(extension, "bz2") ||
124             !strcmpi(extension, "tar") || !strcmpi(extension, "arj") ||
125             !strcmpi(extension, "lza") || !strcmpi(extension, "lzh") ||
126             !strcmpi(extension, "cpio") || !strcmpi(extension, "z"))
127          return archiveFile;
128       else if(!strcmpi(extension, "cab") || !strcmpi(extension, "deb") ||
129             !strcmpi(extension, "rpm"))
130          return packageFile;
131       else if(!strcmpi(extension, "iso") || !strcmpi(extension, "mds") ||
132             !strcmpi(extension, "cue") || !strcmpi(extension, "bin") ||
133             !strcmpi(extension, "ccd") || !strcmpi(extension, "bwt") ||
134             !strcmpi(extension, "cdi") || !strcmpi(extension, "nrg"))
135          return opticalMediaImageFile;
136       return normalFile;
137    }
138 };
139
140 class FileSystemBoxBits
141 {
142    bool foldersOnly:1, filesOnly:1, details:1, treeBranches:1, previewPictures:1, navigateFolders:1;
143    //bool header:1, freeSelect:1, fullRowSelect:1, multiSelect:1, autoScroll:1, alwaysHL : 1, moveRows:1, resizable:1;
144    //bool moveFields:1, clearHeader:1, alwaysEdit:1, collapse:1, treeBranch:1, rootCollapse:1, heightSet:1;
145    //bool sortable:1, noDragging:1, fillLastField:1, expandOnAdd:1;
146 };
147
148 public class FileSystemBox : Window // should we not derive from ListBox instead?
149 /*
150    this stuff from the listbox would be nicely exposed...
151       fullRowSelect = false;
152       treeBranches = true;
153       collapseControl = true;
154       rootCollapseButton = true;
155       sortable = true;
156 */
157 {
158    borderStyle = deep;
159    hasHorzScroll = false;
160    hasVertScroll = false;
161
162    menu = Menu { };
163
164 public:
165    FileSystemNode root;
166    FileSystemNode selection;
167
168    virtual bool Window::NotifyNodeSelect(FileSystemBox box, FileSystemNode node);
169    virtual bool Window::NotifyNodeOpen(FileSystemBox box, FileSystemNode node);
170    
171    property char * path
172    {
173       set
174       {
175          delete path;
176          if(value && value[0])
177             path = CopyString(value);
178          if(locationBox)
179             locationBox.path = value;
180          if(created)
181             Load();
182       }
183
184       get { return path; }
185       //isset { return path && path[0]; }
186    }
187
188    property bool foldersOnly { set { bits.foldersOnly = value; bits.filesOnly = !value; } get { return bits.foldersOnly; } };
189    property bool filesOnly { set { bits.filesOnly = value; bits.foldersOnly = !value; } get { return bits.filesOnly; } };
190    property bool previewPictures { set { bits.previewPictures = value; } get { return bits.previewPictures; } };
191    property char * extensions { set { delete extensions; if(value && value[0]) extensions = CopyString(value); } get { return extensions; } }
192    property bool details { set { bits.details = value; } get { return bits.details; } };
193    property bool treeBranches
194    {
195       set
196       {
197          bits.treeBranches = value;
198          list.treeBranches = value;
199          list.collapseControl = value;
200          list.rootCollapseButton = value;
201       }
202       get { return bits.treeBranches; }
203    };
204    property bool navigateFolders { set { bits.navigateFolders = value; bits.filesOnly = !value; } get { return bits.navigateFolders; } };
205    property bool multiSelect { set { list.multiSelect = value; } get { return list.multiSelect; } };
206    
207    property FileSystemNode node
208    {
209       get
210       {
211          if(!list)
212             return null;
213          if(!list.currentRow)
214             return null;
215          if(!list.currentRow.tag)
216             return null;
217          return (FileSystemNode)list.currentRow.tag;
218       }
219    }
220
221    PathBox locationBox;
222
223    void Select(FileSystemNode node)
224    {
225       if(node.row)
226       {
227          node.EnsureVisible(false);
228          list.SelectRow(node.row);
229       }
230    }
231
232    FileSystemNode SelectLocation(char * location)
233    {
234       int c;
235       char * temp;
236       char step[MAX_LOCATION];
237
238       //StringArray steps { growingFactor = 4 };
239       Array<String> steps { };
240       FileSystemNode result = null;
241       FileSystemNode node = null;
242
243       temp = CopyString(location);
244       while(temp[0])
245       {
246          GetLastDirectory(temp, step);
247          StripLastDirectory(temp, temp);
248          steps.Add(CopyString(step));
249       }
250
251       for(c = steps.count - 1; c >= 0; c--)
252       {
253          char * t = steps[c];
254          node = Find(steps[c], node);
255          if(!node)
256             break;
257          //Select(node);
258       }
259       if(node)
260       {
261          result = node;
262          Select(result);
263       }
264
265       steps.Free();
266       delete temp;
267       delete steps;
268
269       return result;
270    }
271
272    FileSystemNode Find(const char * name, FileSystemNode parent)
273    {
274       FileSystemNode node = null;
275       FileSystemNode result = null;
276       if(!parent/* && !strcmp(name, "/")*/)
277       {
278          DataRow row;
279          for(row = list.firstRow; row; row = row.next)
280          {
281             node = (FileSystemNode)row.tag;
282             if(node.name && !fstrcmp(node.name, name))
283                break;
284          }
285          if(node)
286             result = node;
287          //result = root;
288       }
289       else
290       {
291          FileSystemNode start = parent ? parent : root;
292          if(!start.loaded || !start.childrenLoaded)
293             LoadTreeNode(start);
294          for(node = start.children.first; node; node = node.next)
295             if(node.name && !fstrcmp(node.name, name))
296                break;
297          if(node)
298             result = node;
299       }
300       return result;
301    }
302
303    void Refresh()
304    {
305       Load();
306    }
307
308 private:
309    FileSystemBoxBits bits;
310
311    char * path;
312    char * extensions;
313
314    BitmapResource fileIcons[_FileType];
315
316    FileSystemBox()
317    {
318       char wd[MAX_LOCATION];
319       GetWorkingDir(wd, sizeof(wd));
320       property::path = wd;
321       
322       InitFileIcons();
323       list.AddField(nameField);
324    }
325    ~FileSystemBox()
326    {
327       delete extensions;
328       delete path;
329    }
330    void InitFileIcons()
331    {
332       _FileType c;
333       for(c = 0; c < _FileType::enumSize; c++)
334       {
335          fileIcons[c] = BitmapResource { fileIconNames[c], alphaBlend = true };
336          AddResource(fileIcons[c]);
337       }
338    }
339
340    DataField nameField { dataType = "FileSystemNode", width = 240, userData = this, freeData = false };
341    DataField typeField { header = "Type", dataType = /*"String"*/ "char *", width = 40, freeData = false };
342    DataField sizeField { header = "Size", dataType = "FileSize", width = 96, alignment = right, freeData = false };
343
344    bool OnPostCreate()
345    {
346       Load();
347       return true;
348    }
349
350    ListBox list
351    {
352       this;
353
354       borderStyle = none;
355       hasHorzScroll = true;
356       hasVertScroll = true;
357       fullRowSelect = false;
358       sortable = true;
359
360       anchor = Anchor { left = 0, top = 0, right = 0, bottom = 0 };
361
362       // WHY is this not working ?
363       /*void OnResize(int width, int height)
364       {
365          if(vertScroll.visible)
366             nameField.width = width - vertScroll.size.w;
367          else
368             nameField.width = width;
369       }*/
370
371       bool NotifyCollapse(ListBox listBox, DataRow row, bool collapsed)
372       {
373          if(row)
374          {
375             FileSystemNode node = (FileSystemNode)row.tag;
376             FileSystemNode child;
377             if(collapsed)
378             {
379                /*
380                for(child = node.children.last; child; child = node.children.last)
381                {
382                   listBox.DeleteRow(child.row);
383                   child.Free();
384                   delete child;
385                }
386                node.childrenLoaded = false;
387                */
388             }
389             else
390             {
391                if(!node.loaded || !node.childrenLoaded)
392                {
393                   LoadTreeNode(node);
394                   //list.Sort(nameField, 1);
395                   //node.
396                }
397                for(child = node.children.first; child && child.next; child = child.next);
398                if(child)
399                   child.EnsureVisible(false);
400             }
401          }
402          return true;
403       }
404       
405       bool NotifyRightClick(ListBox listBox, int x, int y, Modifiers mods)
406       {
407          DataRow row = listBox.currentRow;
408          if(row)
409          {
410             FileSystemNode node = (FileSystemNode)row.tag;
411             if(node)
412             {
413                PopupMenu popup;
414                Menu menu { };
415
416                MenuItem { menu, "Cut\tCtrl+X", t, NotifySelect = null, disabled = false };
417                MenuItem { menu, "Copy\tCtrl+C", c, NotifySelect = null, disabled = false };
418                MenuItem { menu, "Paste\tCtrl+V", p, NotifySelect = null, disabled = false /*!clipboard*/ };
419                MenuItem { menu, "Delete\tDel", d, NotifySelect = null, disabled = false };
420                //MenuDivider { menu };
421
422                popup = PopupMenu
423                   {
424                      master = this, menu = menu,
425                      position = { 
426                         x + clientStart.x + absPosition.x - guiApp.desktop.position.x, 
427                         y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
428                   };
429                popup.Create();
430             }
431          }
432          return true;
433       }
434
435       bool NotifySelect(ListBox listBox, DataRow row, Modifiers mods)
436       {
437          if(row)
438          {
439             FileSystemNode node = (FileSystemNode)row.tag;
440             NotifyNodeSelect(listBox.parent.master, this, node);
441             selection = node;
442          }
443          return true;
444       }
445
446       bool NotifyEditing(ListBox listBox, DataRow row)
447       {
448          if(row)
449          {
450             FileSystemNode node = (FileSystemNode)row.tag;
451          }
452          return true;
453       }
454
455       bool NotifyEdited(ListBox listBox, DataRow row)
456       {
457          if(row)
458          {
459             FileSystemNode node = (FileSystemNode)row.tag;
460          }
461          return true;
462       }
463
464       bool NotifyEditDone(ListBox listBox, DataRow row)
465       {
466          if(row)
467          {
468             FileSystemNode node = (FileSystemNode)row.tag;
469          }
470          return true;
471       }
472
473       bool NotifyDoubleClick(ListBox listBox, int x, int y, Modifiers mods)
474       {
475          OpenNode();
476          return false;
477       }
478
479       bool NotifyKeyDown(ListBox listBox, DataRow row, Key key, unichar ch)
480       {
481          bool result;
482          if((SmartKey)key == enter)
483             result = OpenNode();
484          else
485             result = true;
486          return true;
487       }
488    };
489
490    bool OpenNode()
491    {
492       bool result;
493       if(selection && selection.type.isFolder && bits.navigateFolders)
494       {
495          property::path = selection.path;
496          result = true;
497       }
498       else
499          result = NotifyNodeOpen(this.master, this, selection);
500       return result;
501    }
502
503    // Edit Menu
504    Menu editMenu { menu, "Edit", e };
505    MenuItem itemEditCut
506    {
507       editMenu, "Cut\tCtrl+X", t, disabled = true;
508
509       bool NotifySelect(MenuItem selection, Modifiers mods)
510       {
511          //EditCut();
512          return true;
513       }
514    };
515    MenuItem itemEditCopy
516    {
517       editMenu, "Copy\tCtrl+C", c, disabled = true;
518
519       bool NotifySelect(MenuItem selection, Modifiers mods)
520       {
521          //EditCopy();
522          return true;
523       }
524    };
525    MenuItem itemEditPaste
526    {
527       editMenu, "Paste\tCtrl+V", p;
528    
529       bool NotifySelect(MenuItem selection, Modifiers mods)
530       {
531          //EditPaste();
532          return true;
533       }
534    };
535    MenuItem itemEditDelete
536    {
537       editMenu, "Delete\tDel", d, disabled = true;
538
539       bool NotifySelect(MenuItem selection, Modifiers mods)
540       {
541          //EditDelete();
542          return true;
543       }
544    };
545
546    // WHY is this crashing ? 
547    /*void OnResize(int width, int height)
548    {
549       if(this && nameField)
550          nameField.width = width - 80;
551    }*/
552
553    void Load()
554    {
555       // TODO: fix this!
556       // this is crashing in for designer when details = true // can't save the file, always yields a crash
557       /*if(list && created)
558       {
559          list.ClearFields();
560          list.AddField(nameField);
561          if(bits.details)
562          {
563             list.AddField(typeField);
564             list.AddField(sizeField);
565          }
566       }*/
567       if(bits.treeBranches)
568          LoadTree();
569       else
570          LoadList();
571    }
572
573    void LoadList()
574    {
575       FileListing listing { path, extensions = extensions };
576
577       list.Clear();
578       while(listing.Find())
579       {
580          if((!bits.foldersOnly && !bits.filesOnly) ||
581             (bits.foldersOnly && listing.stats.attribs.isDirectory) ||
582             (bits.filesOnly && listing.stats.attribs.isFile))
583          {
584             FileSystemNode node = MakeFileSystemNode(listing.stats, listing.name, listing.path, bits.previewPictures, displaySystem);
585             DataRow row = list.AddRow();
586             row.tag = (int)node;
587             row.SetData(nameField, node);
588             if(bits.details)
589             {
590                row.SetData(typeField, node.extension);
591                row.SetData(sizeField, (void *)node.stats.size);
592             }
593          }
594       }
595       list.Sort(nameField, 1);
596    }
597
598    void LoadTree()
599    {
600       bool isRoot = !strcmp(path, "/");
601       //char startPath[MAX_LOCATION];
602       FileSystemNode parent;
603       FileSystemNode node;
604       FileListing listing { path, extensions = extensions };
605       
606       /*if(!path)
607          GetWorkingDir(startPath, sizeof(startPath));
608       else
609          strcpy(path, startPath);*/
610       
611       list.Clear();
612
613       delete root;
614    #ifdef __WIN32__
615       if(isRoot)
616       {
617          root = FileSystemNode { loaded = true, childrenLoaded = true };
618          AddTreeNode(root, true, false, null);
619          while(listing.Find())
620          {
621             int len = strlen(listing.name);
622             char info[MAX_LOCATION];
623             char name[MAX_LOCATION];
624             if(listing.stats.attribs.isDrive &&
625                   len > 3 && !strncmp(&listing.name[1], ": [", 3))
626             {
627                strncpy(name, listing.name, 2);
628                name[2] = 0;
629                strncpy(info, &listing.name[4], len - 5);
630                info[len - 5] = 0;
631             }
632             else
633             {
634                strcpy(name, listing.name);
635                info[0] = 0;
636             }
637
638             parent = MakeFileSystemNode(listing.stats, name, listing.path, bits.previewPictures, displaySystem);
639             if(info[0])
640                parent.info = CopyString(info);
641             parent.loaded = true;
642             AddTreeNode(parent, !listing.stats.attribs.isDirectory, listing.stats.attribs.isDirectory, root);
643             if(!listing.stats.attribs.isDirectory)
644                parent.childrenLoaded = true;
645          }
646
647          node = FileSystemNode { name = msNetwork, type = network };
648          AddTreeNode(node, false, true, null);
649          node.row.collapsed = true;
650       }
651       else
652    #endif
653       {
654          root = MakeFileSystemNode(FileStats { attribs = FileExists(path)}, path, path, bits.previewPictures, displaySystem);
655          AddTreeNode(root, false, true, null);
656          LoadTreeNode(root);
657       }
658
659       if(isRoot)
660       {
661          root.type = computer;
662          root.label = rootName;
663       }
664
665       list.Sort(nameField, 1);
666       list.SelectRow(root.row);
667    }
668
669    void LoadTreeNode(FileSystemNode node)
670    {
671       if(!node.loaded)
672       {
673          char path[MAX_LOCATION];
674          node.GetPath(path);
675          {
676             FileListing listing { path, extensions = extensions };
677             if(node.children.count == 1)
678                DeleteNode(node.children.first);
679
680             while(listing.Find())
681             {
682                if(!listing.stats.attribs.isRemovable && ((!bits.foldersOnly && !bits.filesOnly) ||
683                   (bits.foldersOnly && listing.stats.attribs.isDirectory) ||
684                   (bits.filesOnly && listing.stats.attribs.isFile)))
685                {
686                   FileSystemNode child = MakeFileSystemNode(listing.stats, listing.name, listing.path, bits.previewPictures, displaySystem);
687                   AddTreeNode(child, true, false, node);
688                   NodeChildLoad(child, node);
689                }
690             }
691          }
692          node.childrenLoaded = true;
693          node.loaded = true;
694          node.row.SortSubRows(false);
695       }
696       else if(!node.childrenLoaded)
697       {
698          FileSystemNode child;
699          if(node.children.first)
700          {
701             for(child = node.children.first; child; child = child.next)
702             {
703                if(!child.loaded)
704                   LoadTreeNode(child);
705                else if(!child.childrenLoaded)
706                   NodeChildLoad(child, node);
707             }
708             node.childrenLoaded = true;
709             node.row.SortSubRows(false);
710          }
711       }
712    }
713
714    void NodeChildLoad(FileSystemNode parent, FileSystemNode node)
715    {
716       char path[MAX_LOCATION];
717       parent.GetPath(path);
718       {
719          bool added = false;
720          FileListing listing { path, extensions = extensions };
721          while(listing.Find())
722          {
723             if((!bits.foldersOnly && !bits.filesOnly) ||
724                (bits.foldersOnly && listing.stats.attribs.isDirectory) ||
725                (bits.filesOnly && listing.stats.attribs.isFile))
726             {
727                FileSystemNode child = MakeFileSystemNode(listing.stats, listing.name, listing.path, bits.previewPictures, displaySystem);
728                AddTreeNode(child, true, false, parent);
729                added = true;
730             }
731          }
732          if(!added)
733             added = true;
734       }
735       //parent.childrenLoaded = true;
736    }
737
738    void AddTreeNode(FileSystemNode node, bool loaded, bool addLoader, FileSystemNode addTo)
739    {
740       DataRow row = (addTo && addTo.row) ? addTo.row.AddRow() : list.AddRow();
741       if(addTo)
742       {
743          node.parent = addTo;
744          node.indent = addTo.indent + 1;
745          addTo.children.Add(node);
746       }
747       row.tag = (int)node;
748       node.row = row;
749       row.SetData(null, node);
750
751       node.loaded = loaded;
752       if(addLoader)
753          //AddTreeNode(FileSystemNode { }, false, false, node); // why would this create a compile error?
754          AddTreeNode(FileSystemNode { type = none }, false, false, node);
755
756       if(node.indent > 0)
757          row.collapsed = true;
758       else if(node.type == folder)
759          node.type = folderOpen;
760    }
761
762    void DeleteNode(FileSystemNode node)
763    {
764       FileSystemNode child;
765       for(; (child = node.children.first); )
766          DeleteNode(child);
767       list.DeleteRow(node.row);
768       node.Delete();
769    }
770 }
771
772 /*
773 #if 0
774 class ExplorerView : FileSystemBox
775 {
776    borderStyle = none;
777    hasHorzScroll = false;
778    hasVertScroll = false;
779
780    virtual void Load(FileSystemNode parent);
781    virtual void Refresh();
782
783    virtual void LaunchNotifyItemSelect(Window master, ExplorerView view, ExplorerFileItem item, ExplorerFileItemArray selectedItems)
784    {
785       view.NotifyItemSelect(master, view, item, selectedItems);
786    }
787
788    virtual bool Window::NotifyItemSelect(ExplorerView view, ExplorerFileItem item, ExplorerFileItemArray selectedItems);
789    virtual bool Window::NotifyItemOpen(ExplorerView view, ExplorerFileItem item);
790
791    ListBox list
792    {
793       master = master, parent = this;
794       //this, master;
795       borderStyle = none;
796       hasHorzScroll = true;
797       hasVertScroll = true;
798       resizable = true;
799       sortable = true;
800       fullRowSelect = false;
801       multiSelect = true;
802
803       anchor = Anchor { left = 0, top = 0, right = 0, bottom = 0 };
804
805       bool NotifySelect(ListBox listBox, DataRow row, Modifiers mods)
806       {
807          ExplorerView view = (ExplorerView)listBox.parent;
808          if(listBox.currentRow)
809          {
810             DataRow listRow;
811             ExplorerFileItemArray selectedItems { growingFactor = 16 };
812             for(listRow = listBox.firstRow; listRow; listRow = listRow.next)
813                if(listRow.selected)
814                   selectedItems.Add((ExplorerFileItem)listRow.tag);
815             //view.NotifyItemSelect(listBox.parent.master, view, (ExplorerFileItem)listBox.currentRow.tag);
816             view.LaunchNotifyItemSelect(listBox.parent.master, view, (ExplorerFileItem)listBox.currentRow.tag, selectedItems);
817          }
818          return true;
819       }
820
821       bool NotifyDoubleClick(ListBox listBox, int x, int y, Modifiers mods)
822       {
823          ExplorerView view = (ExplorerView)listBox.parent;
824          view.NotifyItemOpen(listBox.parent.master, view, (ExplorerFileItem)listBox.currentRow.tag);
825          return false;
826       }
827
828       bool NotifyKeyDown(ListBox listBox, DataRow row, Key key, unichar ch)
829       {
830          if((SmartKey)key == enter)
831          {
832             ExplorerView view = (ExplorerView)listBox.parent;
833             view.NotifyItemOpen(listBox.parent.master, view, (ExplorerFileItem)listBox.currentRow.tag);
834          }
835          return true;
836       }
837    };
838
839    ExplorerView()
840    {
841    }
842 }
843 #endif
844
845 #if 0
846 class ExplorerViewList : ExplorerView
847 {
848
849    FileSystemNode location;
850
851 public:
852
853    DataField nameField { header = "Name", dataType = "ExplorerFileItem", width = 304, editable = true, userData = this };
854
855    ExplorerViewDetails()
856    {
857       list.AddField(nameField);
858    }
859
860    void Refresh()
861    {
862       Load(location);
863    }
864
865    void Load(FileSystemNode location)
866    {
867       char path[MAX_LOCATION];
868       this.location = location;
869       location.GetPath(path);
870       {
871          FileListing listing { path };
872          
873          ExplorerFileItem item;
874          DataRow row;
875
876          list.Clear();
877
878          while(listing.Find())
879          {
880             item = MakeFileItem(listing.stats.attribs, listing.name, listing.path, previewPictures, displaySystem);
881
882             row = list.AddRow();
883             row.tag = (int)item;
884             row.SetData(nameField, item);
885          }
886          list.Sort(nameField, 1);
887       }
888    }
889 }
890 #endif
891
892 #if 0
893 class ExplorerViewDetails : ExplorerView
894 {
895    list.hasHeader = true;
896    list.moveFields = true;
897    list.resizable = true;
898    list.sortable = true;
899
900    FileSystemNode location;
901
902 public:
903
904    DataField nameField { header = "Name", dataType = "ExplorerFileItem", width = 304, editable = true, userData = this };
905    DataField typeField { header = "Type", dataType = /-*"String"*-/ "char *", width = 40 };
906    DataField sizeField { header = "Size", dataType = "FileSize", width = 96, alignment = right };
907
908    ExplorerViewDetails()
909    {
910       list.AddField(nameField);
911       list.AddField(typeField);
912       list.AddField(sizeField);
913    }
914
915    void Refresh()
916    {
917       Load(location);
918    }
919
920    void Load(FileSystemNode location)
921    {
922       char path[MAX_LOCATION];
923       this.location = location;
924       location.GetPath(path);
925       {
926          FileListing listing { path };
927          
928          ExplorerFileItem item;
929          DataRow row;
930
931          list.Clear();
932
933          while(listing.Find())
934          {
935             item = MakeFileItem(listing.stats.attribs, listing.name, listing.path, previewPictures, displaySystem);
936
937             row = list.AddRow();
938             row.tag = (int)item;
939             row.SetData(nameField, item);
940             row.SetData(typeField, CopyString(item.extension));
941             row.SetData(sizeField, (uint)listing.stats.size);
942          }
943          list.Sort(nameField, 1);
944       }
945    }
946 }
947 #endif
948
949 #if 0
950 class ExplorerViewIcons : ExplorerView
951 {
952
953    FileSystemNode location;
954
955 public:
956
957    DataField nameField { header = "Name", dataType = "ExplorerFileItem", width = 304, editable = true, userData = this };
958
959    ExplorerViewDetails()
960    {
961       list.AddField(nameField);
962    }
963
964    void Refresh()
965    {
966       Load(location);
967    }
968
969    void Load(FileSystemNode location)
970    {
971       char path[MAX_LOCATION];
972       this.location = location;
973       location.GetPath(path);
974       {
975          FileListing listing { path };
976          
977          ExplorerFileItem item;
978          DataRow row;
979
980          list.Clear();
981
982          while(listing.Find())
983          {
984             item = MakeFileItem(listing.stats.attribs, listing.name, listing.path, previewPictures, displaySystem);
985
986             row = list.AddRow();
987             row.tag = (int)item;
988             row.SetData(nameField, item);
989          }
990          list.Sort(nameField, 1);
991       }
992    }
993 }
994 #endif
995
996 #if 0
997 class ExplorerViewCards : ExplorerView
998 {
999
1000    FileSystemNode location;
1001
1002 public:
1003
1004    DataField nameField { header = "Name", dataType = "ExplorerFileItem", width = 304, editable = true, userData = this };
1005
1006    ExplorerViewDetails()
1007    {
1008       list.AddField(nameField);
1009    }
1010
1011    void Refresh()
1012    {
1013       Load(location);
1014    }
1015
1016    void Load(FileSystemNode location)
1017    {
1018       char path[MAX_LOCATION];
1019       this.location = location;
1020       location.GetPath(path);
1021       {
1022          FileListing listing { path };
1023          
1024          ExplorerFileItem item;
1025          DataRow row;
1026
1027          list.Clear();
1028
1029          while(listing.Find())
1030          {
1031             item = MakeFileItem(listing.stats.attribs, listing.name, listing.path, previewPictures, displaySystem);
1032
1033             row = list.AddRow();
1034             row.tag = (int)item;
1035             row.SetData(nameField, item);
1036          }
1037          list.Sort(nameField, 1);
1038       }
1039    }
1040 }
1041 #endif
1042
1043 #if 0
1044 public class BitmapArray : RedjArray
1045 {
1046    type = class(Bitmap);
1047 public:
1048    Bitmap * const _;
1049    Bitmap * Add(Bitmap bitmap)
1050    {
1051       uint pos = _count;
1052       Append(1);
1053       _[pos] = bitmap;
1054       return &_[pos];
1055    }
1056    Bitmap * AddBefore(uint position, Bitmap bitmap)
1057    {
1058       Insert(position, 1);
1059       _[position] = bitmap;
1060       return &_[position];
1061    }
1062    void Clear()
1063    {
1064       int c;
1065       for(c = 0; c < _count; c++)
1066       {
1067          _[c].Free();
1068          delete _[c];
1069       }  
1070       count = 0;
1071       size = 0;
1072    }
1073 }
1074 #endif
1075
1076 #if 0
1077 class ExplorerViewShowcase : ExplorerView
1078 {
1079    list.anchor = Anchor { left = 0, top = 0, bottom = 0 };
1080    list.size = Size { w = 200 };
1081
1082    FileSystemNode location;
1083
1084 public:
1085
1086    DataField nameField { header = "Name", dataType = "ExplorerFileItem", width = 180, editable = true, userData = this };
1087
1088    Bitmap bitmap;
1089    BitmapArray bitmaps { growingFactor = 16 };
1090
1091    Window show
1092    {
1093       this;
1094       borderStyle = none;
1095       anchor = Anchor { top = 0, right = 0, bottom = 0 };
1096
1097       void OnRedraw(Surface surface)
1098       {
1099          ExplorerViewShowcase view = (ExplorerViewShowcase)parent;
1100          if(view.bitmap)
1101          {
1102             int wBmp = view.bitmap.width;
1103             int hBmp = view.bitmap.height;
1104             int wWnd = clientSize.w;
1105             int hWnd = clientSize.h;
1106
1107             int wList = view.list.size.w + view.split.size.w;
1108             
1109             float scale = Min((float)(wWnd - 10) / wBmp, (float)(hWnd - 10) / hBmp);
1110             
1111             int wDraw = (int)(wBmp * scale);
1112             int hDraw = (int)(hBmp * scale);
1113
1114       #ifndef __linux__
1115             surface.Filter(view.bitmap, (wWnd - wDraw) / 2, (hWnd - hDraw) / 2, 0, 0, wDraw, hDraw, wBmp, hBmp);
1116       #else
1117             // Until Filter / Stretch works with X
1118             surface.Blit(view.bitmap, (wWnd - wDraw) / 2, (hWnd - hDraw) / 2, 0, 0, wDraw, hDraw);
1119       #endif
1120          }
1121          else
1122          {
1123             surface.SetForeground(white);
1124             surface.Area(0, 0, view.clientSize.w - 1, view.clientSize.h - 1);
1125          }
1126       }
1127    }
1128
1129    SplitWindow split
1130    {
1131       this;
1132       leftPane = list;
1133       rightPane = show;
1134       split = 200;
1135       tabCycle = true;
1136    };
1137
1138    ExplorerViewDetails()
1139    {
1140       list.AddField(nameField);
1141    }
1142
1143    void LaunchNotifyItemSelect(Window master, ExplorerViewShowcase view, ExplorerFileItem item, ExplorerFileItemArray selectedItems)
1144    {
1145       int pos;
1146       ExplorerFileItem selItem;
1147       if(view.bitmap)
1148          view.bitmap.Free();
1149       delete view.bitmap;
1150       if(item && item.type == pictureFile)
1151       {
1152          view.bitmap = Bitmap { };
1153          view.bitmap.Load(item.path, null, displaySystem);
1154       }
1155
1156       view.bitmaps.Clear();
1157       view.bitmaps = BitmapArray { };
1158       for(pos = 0; pos < selectedItems.count; pos++)
1159       {
1160          Bitmap bitmap { };
1161          selItem = (ExplorerFileItem)selectedItems._[pos]; 
1162          bitmap.Load(selItem.path, null, displaySystem);
1163          //view.bitmaps.Add(bitmap);
1164       }  
1165       if(item && item.type == pictureFile)
1166       {
1167          view.bitmap = Bitmap { };
1168          view.bitmap.Load(item.path, null, displaySystem);
1169       }
1170
1171       view.show.Update(null);
1172       view.NotifyItemSelect(master, view, item, selectedItems);
1173    }
1174
1175    void Refresh()
1176    {
1177       Load(location);
1178    }
1179
1180    void Load(FileSystemNode location)
1181    {
1182       char path[MAX_LOCATION];
1183       this.location = location;
1184       location.GetPath(path);
1185       {
1186          FileListing listing { path };
1187          
1188          ExplorerFileItem item;
1189          DataRow row;
1190
1191          list.Clear();
1192
1193          while(listing.Find())
1194          {
1195             item = MakeFileItem(listing.stats.attribs, listing.name, listing.path, previewPictures, displaySystem);
1196
1197             row = list.AddRow();
1198             row.tag = (int)item;
1199             row.SetData(nameField, item);
1200          }
1201          list.Sort(nameField, 1);
1202       }
1203    }
1204 }
1205 #endif
1206
1207 #if 0
1208 class ExplorerTree : FileSystemBox
1209 {
1210    hasHorzScroll = false;
1211    hasVertScroll = false;
1212
1213    menu = Menu { };
1214
1215 public:
1216
1217    DataField nameField { dataType = "FileSystemNode", width = 240, userData = this };
1218
1219    FileSystemNode root;
1220    FileSystemNode selection;
1221
1222    virtual bool Window::NotifyNodeSelect(ExplorerTree tree, FileSystemNode node);
1223    
1224    property FileSystemNode node
1225    {
1226       get
1227       {
1228          if(!tree)
1229             return null;
1230          if(!tree.currentRow)
1231             return null;
1232          if(!tree.currentRow.tag)
1233             return null;
1234          return (FileSystemNode)tree.currentRow.tag;
1235       }
1236    }
1237
1238    void Select(FileSystemNode node)
1239    {
1240       if(node.row)
1241       {
1242          node.EnsureVisible(false);
1243          tree.SelectRow(node.row);
1244       }
1245    }
1246
1247    FileSystemNode Find(const char * name, FileSystemNode parent)
1248    {
1249       FileSystemNode node;
1250       FileSystemNode start = parent ? parent : root;
1251       if(!start.loaded || !start.childrenLoaded)
1252          LoadTreeNode(start, tree);
1253       for(node = start.children.first; node; node = node.next)
1254          if(node.name && !strcmpi(node.name, name))
1255             return node;
1256       return null;
1257    }
1258
1259    ListBox tree
1260    {
1261       master = master, parent = this;
1262       //this, master;
1263       borderStyle = none;
1264       hasHorzScroll = true;
1265       hasVertScroll = true;
1266       fullRowSelect = false;
1267       treeNodees = true;
1268       collapseControl = true;
1269       rootCollapseButton = true;
1270
1271       anchor = Anchor { left = 0, top = 0, right = 0, bottom = 0 };
1272
1273       // WHY is this not working ?
1274       /-*void OnResize(int width, int height)
1275       {
1276          if(vertScroll.visible)
1277             nameField.width = width - vertScroll.size.w;
1278          else
1279             nameField.width = width;
1280       }*-/
1281
1282       bool NotifyCollapse(ListBox listBox, DataRow row, bool collapsed)
1283       {
1284          if(row)
1285          {
1286             FileSystemNode node = (FileSystemNode)row.tag;
1287             FileSystemNode child;
1288             if(collapsed)
1289             {
1290                /-*
1291                for(child = node.children.last; child; child = node.children.last)
1292                {
1293                   listBox.DeleteRow(child.row);
1294                   child.Free();
1295                   delete child;
1296                }
1297                node.childrenLoaded = false;
1298                *-/
1299             }
1300             else
1301             {
1302                if(!node.loaded || !node.childrenLoaded)
1303                   LoadTreeNode(node, tree);
1304                for(child = node.children.first; child && child.next; child = child.next);
1305                if(child)
1306                   child.EnsureVisible(false);
1307             }
1308          }
1309          return true;
1310       }
1311       
1312       bool NotifyRightClick(ListBox listBox, int x, int y, Modifiers mods)
1313       {
1314          DataRow row = listBox.currentRow;
1315          if(row)
1316          {
1317             FileSystemNode node = (FileSystemNode)row.tag;
1318             if(node)
1319             {
1320                PopupMenu popup;
1321                Menu menu { };
1322
1323                MenuItem { menu, "Cut\tCtrl+X", t, NotifySelect = null, disabled = false };
1324                MenuItem { menu, "Copy\tCtrl+C", c, NotifySelect = null, disabled = false };
1325                MenuItem { menu, "Paste\tCtrl+V", p, NotifySelect = null, disabled = false /-*!clipboard*-/ };
1326                MenuItem { menu, "Delete\tDel", d, NotifySelect = null, disabled = false };
1327                //MenuDivider { menu };
1328
1329                popup = PopupMenu
1330                   {
1331                      master = this, menu = menu,
1332                      position = { 
1333                         x + clientStart.x + absPosition.x - guiApp.desktop.position.x, 
1334                         y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
1335                   };
1336                popup.Create();
1337             }
1338          }
1339          return true;
1340       }
1341
1342       bool NotifySelect(ListBox listBox, DataRow row, Modifiers mods)
1343       {
1344          if(row)
1345          {
1346             FileSystemNode node = (FileSystemNode)row.tag;
1347             NotifyNodeSelect(listBox.parent.master, this, node);
1348             selection = node;
1349          }
1350          return true;
1351       }
1352
1353       bool NotifyEditing(ListBox listBox, DataRow row)
1354       {
1355          if(row)
1356          {
1357             FileSystemNode node = (FileSystemNode)row.tag;
1358          }
1359          return true;
1360       }
1361
1362       bool NotifyEdited(ListBox listBox, DataRow row)
1363       {
1364          if(row)
1365          {
1366             FileSystemNode node = (FileSystemNode)row.tag;
1367          }
1368          return true;
1369       }
1370
1371       bool NotifyEditDone(ListBox listBox, DataRow row)
1372       {
1373          if(row)
1374          {
1375             FileSystemNode node = (FileSystemNode)row.tag;
1376          }
1377          return true;
1378       }
1379    };
1380
1381    // Edit Menu
1382    Menu editMenu { menu, "Edit", e };
1383    MenuItem itemEditCut
1384    {
1385       editMenu, "Cut\tCtrl+X", t, disabled = true;
1386
1387       bool NotifySelect(MenuItem selection, Modifiers mods)
1388       {
1389          //EditCut();
1390          return true;
1391       }
1392    };
1393    MenuItem itemEditCopy
1394    {
1395       editMenu, "Copy\tCtrl+C", c, disabled = true;
1396
1397       bool NotifySelect(MenuItem selection, Modifiers mods)
1398       {
1399          //EditCopy();
1400          return true;
1401       }
1402    };
1403    MenuItem itemEditPaste
1404    {
1405       editMenu, "Paste\tCtrl+V", p;
1406    
1407       bool NotifySelect(MenuItem selection, Modifiers mods)
1408       {
1409          //EditPaste();
1410          return true;
1411       }
1412    };
1413    MenuItem itemEditDelete
1414    {
1415       editMenu, "Delete\tDel", d, disabled = true;
1416
1417       bool NotifySelect(MenuItem selection, Modifiers mods)
1418       {
1419          //EditDelete();
1420          return true;
1421       }
1422    };
1423
1424    // WHY is this crashing ? 
1425    /-*void OnResize(int width, int height)
1426    {
1427       if(this && nameField)
1428          nameField.width = width - 80;
1429    }*-/
1430
1431    ExplorerTree()
1432    {
1433       tree.AddField(nameField);
1434    }
1435
1436    void Load()
1437    {
1438       FileSystemNode parent;
1439       FileSystemNode node;
1440       FileListing listing { "/" };
1441
1442       tree.Clear();
1443
1444       root = FileSystemNode { type = computer, loaded = true, childrenLoaded = true };
1445    #ifdef __WIN32__
1446       root.name = rootName;
1447    #else
1448       root.name = "/";
1449    #endif
1450       AddTreeNode(root, true, false, null, tree);
1451
1452    // How can this make sense for linux? 
1453    #ifdef __WIN32__
1454       while(listing.Find())
1455       {
1456          int len = strlen(listing.name);
1457          char info[MAX_LOCATION];
1458          char name[MAX_LOCATION];
1459          if(listing.stats.attribs.isDrive && 
1460                len > 3 && !strncmp(&listing.name[1], ": [", 3))
1461          {
1462             strncpy(name, listing.name, 2);
1463             name[2] = 0;
1464             strncpy(info, &listing.name[4], len - 5);
1465             info[len - 5] = 0;
1466          }
1467          else
1468          {
1469             strcpy(name, listing.name);
1470             info[0] = 0;
1471          }
1472
1473          parent = MakeFileSystemNode(listing.stats, name);
1474          if(info[0])
1475             parent.info = CopyString(info);
1476          parent.loaded = true;
1477          AddTreeNode(parent, !listing.stats.attribs.isDirectory, listing.stats.attribs.isDirectory, root, tree);
1478          if(!listing.stats.attribs.isDirectory)
1479             parent.childrenLoaded = true;
1480       }
1481    #endif
1482       node = FileSystemNode { name = msNetwork, type = network };
1483       AddTreeNode(node, false, true, null, tree);
1484       node.row.collapsed = true;
1485       tree.Sort(nameField, 1);
1486       tree.SelectRow(root.row);
1487    }
1488 }
1489
1490 #if 0
1491 public class ClipBoardFiles
1492 {
1493
1494 public:
1495
1496    property
1497
1498 }
1499
1500    // CLIPBOARD
1501    void Copy()
1502    {
1503       if(this)
1504       {
1505          int size = SelSize();
1506          if(size)
1507          {
1508             // Try to allocate memory
1509             ClipBoard clipBoard { };
1510             if(clipBoard.Allocate(size+1))
1511             {
1512                GetSel(clipBoard.memory, true);   
1513                // Save clipboard
1514                clipBoard.Save();
1515             }
1516             delete clipBoard;
1517          }
1518       }
1519    }
1520
1521    void Paste()
1522    {
1523       if(this)
1524       {
1525          ClipBoard clipBoard { };
1526          if(clipBoard.Load())
1527             PutS(clipBoard.memory);
1528          delete clipBoard;
1529       }
1530    }
1531
1532    void Cut()
1533    {
1534       if(this)
1535       {
1536          Copy();
1537          DelSel();
1538          SetViewToCursor(true);
1539          Modified();
1540       }
1541    }
1542
1543 /-*
1544 Private Type DROPFILES
1545    pFiles As Long
1546    pt As POINTAPI
1547    fNC As Long
1548    fWide As Long
1549 End Type
1550 For iCounter = 0 To filelist.ListCount - 1
1551   If filelist.Selected(iCounter) = True Then
1552     strFiles = strFiles & FixPath(filelist.Path) & filelist.List(iCounter) & vbNullChar
1553   End If
1554 Next
1555 'all selected items are now put in strFiles
1556
1557 hGlobal = GlobalAlloc(GHND, Len(DF) + Len(strFiles)) 'put all files to a exclusive number
1558 If hGlobal Then 'if the globalalloc worked
1559   lpGlobal = GlobalLock(hGlobal) 'lock the hGlobal
1560   DF.pFiles = Len(DF) 'set the size of the files
1561   
1562   Call CopyMem(ByVal lpGlobal, DF, Len(DF)) 'copy df to the lpglobal
1563   Call CopyMem(ByVal (lpGlobal + Len(DF)), ByVal strFiles, Len(strFiles)) 'copy strfiles to lpglobal
1564   Call GlobalUnlock(hGlobal) 'unlock hglobal again
1565   
1566   SetClipboardData CF_HDROP, hGlobal 'put files to the clipboard
1567 End If
1568 *-/
1569    bool SaveFile(const char * filePath)
1570    {
1571    }
1572 #endif
1573
1574 #if 0
1575 public class FileTreeNodeBSArray : ArrayBinarySorted
1576 {
1577    type = class(FileSystemNode);
1578 public:
1579    FileSystemNode * const _;
1580    BSloc Add(FileSystemNode item)
1581    {
1582       BSloc result = Find(item);
1583       if(!result.valid)
1584       {
1585          Insert(result.pos, 1);
1586          _[result.pos] = item;
1587       }
1588       return result;
1589    }
1590    BSloc Remove(FileSystemNode item)
1591    {
1592       
1593    }
1594 }
1595 #endif
1596
1597 #if 0
1598 public class FileTreeNodeArray : RedjArray
1599 {
1600    type = class(FileSystemNode);
1601 public:
1602    FileSystemNode * const _;
1603    FileSystemNode * Add(FileSystemNode item)
1604    {
1605       uint pos = _count;
1606       Append(1);
1607       _[pos] = item;
1608       return &_[pos];
1609    }
1610    FileSystemNode * AddBefore(uint position, FileSystemNode item)
1611    {
1612       Insert(position, 1);
1613       _[position] = item;
1614       return &_[position];
1615    }
1616 }
1617 #endif
1618
1619 #if 0
1620 public class ExplorerFileItem : struct
1621 {
1622    char * path;
1623    char * name;
1624    char * info;
1625    char * extension;
1626    _FileType type;
1627    int indent;
1628
1629    Bitmap bitmap;
1630
1631    void OnDisplay(Surface surface, int x, int y, int width, FileSystemBox control, Alignment alignment, DataDisplayFlags displayFlags)
1632    {
1633       int indentSize = (displayFlags.dropBox) ? 0 : 10;
1634       int textOffset;
1635       int len;
1636       char label[MAX_FILENAME];
1637
1638       //float scale = Min((float)clientSize.w / (float)bitmap.width, (float)clientSize.h / (float)bitmap.height);
1639       int w = 16; //(int)(bitmap.width * scale);
1640       int h = 16; //(int)(bitmap.height * scale);
1641    
1642       Bitmap icon;
1643
1644       icon = control.fileIcons[type].bitmap;
1645       if(!icon)
1646       {
1647          if(type == folder || type == folderOpen)
1648             surface.SetForeground(red); //Color { 170, 170, 0 } // REDJ What is that color?
1649          indentSize = 8;
1650       }
1651       textOffset = indent * indentSize + (icon ? (icon.width + 6) : 0);
1652       
1653       if(info)
1654          sprintf(label, "%s [%s]", name, info);
1655       else
1656          strcpy(label, name);
1657       len = strlen(label);
1658
1659       surface.WriteTextDots
1660          (alignment, x + textOffset, y + 2, width - textOffset, label, len);
1661       if(type == pictureFile && control.previewPictures && bitmap)
1662       {
1663 #ifndef __linux__
1664          //surface.Filter(bitmap, (clientSize.w - w) / 2,(clientSize.h - h) / 2, 0,0, w, h, bitmap.width, bitmap.height);
1665          surface.Filter(bitmap, x + indent * indentSize + 2, y, 0, 0, w, h, bitmap.width, bitmap.height);
1666 #else
1667          // Until Filter / Stretch works with X
1668          //surface.Blit(bitmap, (clientSize.w - bitmap.width) / 2,(clientSize.h - bitmap.height) / 2, 0,0, bitmap.width, bitmap.height);
1669          surface.blend = true;
1670          surface.Blit(bitmap, x + indent * indentSize + 2, y,0,0, w, h);
1671 #endif
1672          //bitmap.Free();
1673          //delete bitmap;
1674       }
1675       else if(icon)
1676          surface.Blit(icon, x + indent * indentSize + 2, y,0,0, icon.width, icon.height);
1677    }
1678
1679    int OnCompare(ExplorerFileItem b)
1680    {
1681       int result;
1682       if(type == b.type || (type < folder && b.type < folder) || (type >= drive))
1683          result = strcmpi(name, b.name);
1684       else
1685       {
1686          if(type == folder && b.type < folder) result = -1;
1687          else if(type < folder && b.type == folder) result = 1;
1688       }
1689       return result;
1690    }
1691
1692    void OnCopy(ExplorerFileItem newData)
1693    {
1694       type = newData.type;
1695       indent = newData.indent;
1696       if(newData.name)
1697       {
1698          int len = strlen(newData.name) + 1;
1699          name = new char[len];
1700          CopyBytes(name, newData.name, len);
1701       }
1702    }
1703
1704    bool OnGetDataFromString(char * string)
1705    {
1706       int len = strlen(string) + 1;
1707       name = new char[len];
1708       CopyBytes(name, string, len);
1709       return true;
1710    }
1711
1712    void OnFree()
1713    {
1714       delete path;
1715       delete name;
1716       delete info;
1717       delete extension;
1718       if(bitmap)
1719          bitmap.Free();
1720    }
1721
1722    char * OnGetString(char * string, void * fieldData, bool * needClass)
1723    {
1724       return name;
1725    }
1726 };
1727
1728 public class ExplorerFileItemArray : RedjArray
1729 {
1730    type = class(ExplorerFileItem);
1731 public:
1732    ExplorerFileItem * const _;
1733    ExplorerFileItem * Add(ExplorerFileItem item)
1734    {
1735       uint pos = _count;
1736       Append(1);
1737       _[pos] = item;
1738       return &_[pos];
1739    }
1740    ExplorerFileItem * AddBefore(uint position, ExplorerFileItem item)
1741    {
1742       Insert(position, 1);
1743       _[position] = item;
1744       return &_[position];
1745    }
1746    void Clear()
1747    {
1748       int c;
1749       for(c = 0; c < _count; c++)
1750       {
1751          //_[c].Free()
1752          delete _[c];
1753       }  
1754       count = 0;
1755       size = 0;
1756    }
1757 }
1758
1759 ExplorerFileItem MakeFileItem(const FileAttribs attribs, const char * fileName, const char * filePath, const bool previewPicture, const DisplaySystem displaySystem)
1760 {
1761    int len = strlen(fileName);
1762    char info[MAX_LOCATION];
1763    char name[MAX_LOCATION];
1764    char extension[MAX_EXTENSION];
1765    
1766    ExplorerFileItem item { };
1767
1768    //if(stats.attribs.isFile) // -- should work now
1769    if(attribs.isDirectory)
1770    {
1771       extension[0] = 0;
1772
1773       item.type = (attribs.isDrive) ? drive : folder;
1774       if(attribs.isServer)
1775          item.type = server;
1776       if(attribs.isShare)
1777          item.type = share;
1778       if(attribs.isCDROM)
1779          item.type = cdrom;
1780       if(attribs.isRemote)
1781          item.type = netDrive;
1782       if(attribs.isRemovable) 
1783       {
1784          if(fileName[0] == 'A' || fileName[0] == 'B')
1785             item.type = floppy;
1786          else
1787             item.type = removable;
1788       }
1789    }
1790    else
1791    {
1792       GetExtension(fileName, extension);
1793       //strupr(extension);
1794       strlwr(extension);
1795       
1796       item.type = _FileType::SelectByExtension(extension);
1797    }
1798
1799    if(attribs.isDrive && 
1800          len > 3 && !strncmp(&fileName[1], ": [", 3))
1801    {
1802       strncpy(name, fileName, 2);
1803       name[2] = 0;
1804       strncpy(info, &fileName[4], len - 5);
1805       info[len - 5] = 0;
1806    }
1807    else
1808    {
1809       strcpy(name, fileName);
1810       info[0] = 0;
1811    }
1812
1813    item.path = CopyString(filePath);
1814    item.name = CopyString(name);
1815    if(info[0])
1816       item.info = CopyString(info);
1817    item.extension = CopyString(extension);
1818
1819    if(item.type == pictureFile && previewPicture)
1820    {
1821       item.bitmap = Bitmap { };
1822       item.bitmap.Load(filePath, null, displaySystem);
1823    }
1824
1825    return item;
1826 }
1827 #endif
1828 */
1829
1830 public class FileSystemNode : struct
1831 {
1832 public:
1833    /*//LinkElement<FileSystemNode> link;
1834    FileSystemNode parent;
1835
1836    FileSystemNodeType type;
1837
1838    char * name;*/
1839
1840    FileSystemNode prev, next;
1841
1842    bool loaded, childrenLoaded;
1843    int indent;
1844    char * path;
1845    char * name;
1846    char * label;
1847    char * extension;
1848    char * info;
1849    DataRow row;
1850    OldList children;
1851    _FileType type;
1852    FileSystemNode parent;
1853
1854    FileStats stats;
1855
1856    Bitmap bitmap;
1857
1858    void GetPath(String outputPath)
1859    {  
1860       FileSystemNode up;
1861       if(parent)
1862       {
1863          if(name)
1864             strcpy(outputPath, name);
1865          for(up = parent; up; up = up.parent)
1866          {
1867             char temp[MAX_LOCATION];
1868             strcpy(temp, up.name);
1869             PathCat(temp, outputPath);
1870             strcpy(outputPath, temp);
1871          }
1872       }
1873       else
1874 /*#ifdef __WIN32__
1875          strcpy(outputPath, "/");
1876 #else*/
1877          strcpy(outputPath, name);
1878 //#endif
1879
1880    }
1881
1882    bool IsChildOf(FileSystemNode node)
1883    {
1884       FileSystemNode test;
1885       for(test = parent; test; test = test.parent)
1886          if(test == node)
1887             return true;
1888       return false;
1889    }
1890
1891    void DuplicateChildren(bool recursive, bool forceExpanded, FileSystemNode addTo, FileSystemBox fsb)
1892    {
1893       if(children.first)
1894       {
1895          FileSystemNode child;
1896          
1897          for(child = children.first; child; child = child.next)
1898          {
1899             FileSystemNode copy { };
1900             copy.name = CopyString(child.name);
1901             copy.type = child.type;
1902             fsb.AddTreeNode(copy, child.loaded, false, addTo);
1903             if(forceExpanded)
1904                copy.row.collapsed = false;
1905             if(recursive)
1906                child.DuplicateChildren(recursive, forceExpanded, copy, fsb);
1907          }
1908       }
1909    }
1910    
1911    void EnsureVisible(bool expand)
1912    {
1913       if(parent)
1914          parent.EnsureVisible(true);
1915       if(expand)
1916          row.collapsed = false;
1917       // TODO: row.EnsureVisible(); // making the row visible by scrolling
1918    }
1919
1920    void OnFree()
1921    {
1922       //delete name;
1923    }
1924
1925    void Free()
1926    {
1927       FileSystemNode child;
1928       for(; (child = children.first); )
1929       {
1930          child.Free();
1931          children.Delete(child);
1932       }
1933       //if(name)
1934       delete name;
1935       delete label;
1936       delete info;
1937    }
1938
1939    void Delete()
1940    {
1941       Free();
1942       if(parent)
1943          parent.children.Delete(this);
1944    }
1945
1946    void OnDisplay(Surface surface, int x, int y, int width, FileSystemBox fsb, Alignment alignment, DataDisplayFlags displayFlags)
1947    {
1948       //int indentSize = (displayFlags.dropBox) ? 0 : 10;
1949       int indent = 16;
1950       int xStart;
1951       int len;
1952       int w, h;
1953       //int textOffset;
1954       char string[MAX_FILENAME];
1955
1956       Bitmap icon;
1957
1958       if(!this)
1959          return;
1960       
1961       icon = fsb.fileIcons[type].bitmap;
1962       //xStart = indent * indent + x + (icon ? (icon.width + 5) : 0);
1963       xStart = x + (icon ? (icon.width + 5) : 0);
1964
1965       if(!name)
1966          return;
1967
1968       if(info)
1969          sprintf(string, "%s [%s]", label ? label : name, info);
1970       else
1971          strcpy(string, label ? label : name);
1972       len = strlen(string);
1973       
1974       if(!icon)
1975       {
1976          if(type == folder || type == folderOpen)
1977             surface.SetForeground(yellow);
1978          //indentSize = 8;
1979          indent = 8;
1980       }
1981       //textOffset = indent * indentSize + (icon ? (icon.width + 4) : 0);
1982       
1983       surface.TextOpacity(false);
1984       surface.TextExtent(string, len, &w, &h);
1985       h = Max(h, 16);
1986     
1987       // Draw the current row stipple
1988       if(displayFlags.selected)
1989          //surface.Area(xStart - 1, y, xStart - 1, y + h - 1);
1990          //surface.Area(xStart + w - 1, y, xStart + w + 1, y + h - 1);
1991          surface.Area(xStart - 3, y, xStart + w + 1, y + h - 1);
1992       
1993       //surface.WriteTextDots(alignment, x + textOffset, y + 2, width - textOffset, name, strlen(name));
1994       surface.WriteTextDots(alignment, xStart, y + 2, width, string, len);
1995
1996       if(!guiApp.textMode)
1997       {
1998          if(displayFlags.current)
1999          {
2000             if(displayFlags.active)
2001             {
2002                surface.LineStipple(0x5555);
2003                if(displayFlags.selected)
2004                   surface.SetForeground(0xFFFFFF80);
2005                else
2006                   surface.SetForeground(black);
2007             }
2008             else
2009             {
2010                surface.SetForeground(selectionColor);
2011             }
2012             surface.Rectangle(xStart - 3, y, xStart + w + 1, y + h - 1);
2013             surface.LineStipple(0);
2014          }
2015
2016          if(icon)
2017          {
2018             w = icon.width;
2019             h = icon.height;
2020          }
2021          if(type == pictureFile && fsb.previewPictures && bitmap)
2022          {
2023             surface.SetForeground(white);
2024             surface.blend = true;
2025    //#ifndef __linux__
2026             //surface.Filter(bitmap, (clientSize.w - w) / 2,(clientSize.h - h) / 2, 0,0, w, h, bitmap.width, bitmap.height);
2027             //surface.Filter(bitmap, x + indent/* * indentSize*/ + 2, y, 0, 0, w, h, bitmap.width, bitmap.height);
2028             surface.Filter(bitmap, x,y,0,0, w, h, bitmap.width, bitmap.height);
2029    //#else
2030             // Until Filter / Stretch works with X
2031             //surface.Blit(bitmap, (clientSize.w - bitmap.width) / 2,(clientSize.h - bitmap.height) / 2, 0,0, bitmap.width, bitmap.height);
2032    //         surface.blend = true;
2033             //surface.Blit(bitmap, x + indent/* * indentSize*/ + 2, y,0,0, w, h);
2034             //surface.Blit(bitmap, x,y,0,0, bitmap.width, bitmap.height);
2035    //#endif
2036             //bitmap.Free();
2037             //delete bitmap;
2038          }
2039          else if(icon)
2040          {
2041             //surface.blend = true;
2042             //surface.alphaWrite = blend;
2043             surface.SetForeground(white);
2044             //surface.Blit(icon, x + indent * indentSize, y,0,0, icon.width, icon.height);
2045             surface.Blit(icon, x,y,0,0, icon.width, icon.height);
2046          }
2047       }
2048    }
2049
2050    int OnCompare(FileSystemNode b)
2051    {
2052       int result;
2053       FileSystemNode a = this;
2054       if(a.type == b.type || (a.type < folder && b.type < folder) || (a.type >= drive))
2055          result = strcmpi(a.name, b.name);
2056       else
2057       {
2058          if(a.type == folder && b.type < folder) result = -1;
2059          else if(a.type < folder && b.type == folder) result = 1;
2060          else result = 0;
2061       }
2062       return result;
2063    }
2064
2065    /*int OnCompare(FileSystemNode b)
2066    {
2067       int result;
2068       FileSystemNode a = this;
2069       if(a.parent < b.parent) result = -1;
2070       else if(a.parent > b.parent) result = 1;
2071       else
2072          result = fstrcmp(a.name, b.name);
2073       return result;
2074    }*/
2075
2076    char * OnGetString(char * tempString, FileSystemToolWindow fileSysToolWnd, bool * needClass)
2077    {
2078       return name ? name : "";
2079    }
2080 }
2081
2082 /*FileSystemNode MakeFileSystemNode(const FileStats stats, const char * name)
2083 {
2084    FileSystemNode node { stats = stats };
2085    node.name = CopyString(name);
2086    if(!node.name)
2087       node.name = null;
2088    if(stats.attribs.isDirectory)
2089    {
2090       node.type = (stats.attribs.isDrive) ? drive : folder;
2091       if(stats.attribs.isServer) node.type = server;
2092       if(stats.attribs.isShare) node.type = share;
2093       if(stats.attribs.isCDROM) node.type = cdrom;
2094       if(stats.attribs.isRemote) node.type = netDrive;
2095       if(stats.attribs.isRemovable) 
2096       {
2097          if(name[0] == 'A' || name[0] == 'B')
2098             node.type = floppy;
2099          else
2100             node.type = removable;
2101       }
2102    }
2103    else
2104    {
2105       char extension[MAX_EXTENSION];
2106       GetExtension(node.name, extension);
2107       node.type = _FileType::SelectByExtension(extension);
2108    }
2109    return node;
2110 }*/
2111 static FileSystemNode MakeFileSystemNode(const FileStats stats,
2112       const char * fileName, const char * filePath,
2113       const bool previewPicture, const DisplaySystem displaySystem)
2114 {
2115    int len = strlen(fileName);
2116    char info[MAX_LOCATION];
2117    char name[MAX_LOCATION];
2118    char extension[MAX_EXTENSION];
2119    
2120    FileSystemNode node { stats = stats };
2121
2122    //if(stats.attribs.isFile) // TODO fix this in ecere
2123    if(stats.attribs.isDirectory)
2124    {
2125       extension[0] = '\0';
2126
2127       node.type = (stats.attribs.isDrive) ? drive : folder;
2128       if(stats.attribs.isServer) node.type = server;
2129       if(stats.attribs.isShare) node.type = share;
2130       if(stats.attribs.isCDROM) node.type = cdrom;
2131       if(stats.attribs.isRemote) node.type = netDrive;
2132       if(stats.attribs.isRemovable) 
2133       {
2134          if(fileName[0] == 'A' || fileName[0] == 'B')
2135             node.type = floppy;
2136          else
2137             node.type = removable;
2138       }
2139    }
2140    else
2141    {
2142       GetExtension(fileName, extension);
2143       strlwr(extension);
2144       
2145       node.type = _FileType::SelectByExtension(extension);
2146    }
2147
2148    if(stats.attribs.isDrive && 
2149          len > 3 && !strncmp(&fileName[1], ": [", 3))
2150    {
2151       strncpy(name, fileName, 2);
2152       name[2] = 0;
2153       strncpy(info, &fileName[4], len - 5);
2154       info[len - 5] = 0;
2155    }
2156    else
2157    {
2158       strcpy(name, fileName);
2159       info[0] = 0;
2160    }
2161
2162    node.path = CopyString(filePath);
2163    node.name = CopyString(name);
2164    if(info[0])
2165       node.info = CopyString(info);
2166    node.extension = CopyString(extension);
2167
2168    if(node.type == pictureFile && previewPicture)
2169    {
2170       node.bitmap = Bitmap { alphaBlend = true };
2171       node.bitmap.Load(filePath, null, displaySystem);
2172    }
2173
2174    return node;
2175 }