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