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