libede:FileSystemBox: code esthetics
[ede] / libede / src / FileSystemBox.ec
1 public import "ecere"
2
3 static char * fileIconNames[] = 
4 {
5    "<:ecere>mimeTypes/file.png",         /* none */
6
7    "<:ecere>mimeTypes/file.png",         /* normalFile */
8    "<:ecere>mimeTypes/textEcereWorkspace.png",          /* ewsFile */
9    "<:ecere>mimeTypes/textEcereProject.png",      /* epjFile */
10    "<:ecere>mimeTypes/textEcereSource.png",         /* ecFile */
11    "<:ecere>mimeTypes/textEcereHeader.png",         /* ehFile */
12    "<:ecere>mimeTypes/textCSource.png",          /* cFile */
13    "<:ecere>mimeTypes/textCHeader.png",          /* hFile */
14    "<:ecere>mimeTypes/textC++Source.png",        /* cppFile */
15    "<:ecere>mimeTypes/textC++Header.png",        /* hppFile */
16    "<:ecere>mimeTypes/text.png",         /* textFile */
17    "<:ecere>mimeTypes/textHyperTextMarkup.png",              /* webFile */
18    "<:ecere>mimeTypes/image.png",        /* pictureFile */
19    "<:ecere>status/audioVolumeHigh.png",         /* soundFile */
20    "<:ecere>mimeTypes/package.png",      /* archiveFile */
21    "<:ecere>mimeTypes/packageSoftware.png",     /* packageFile */
22    "<:ecere>mimeTypes/packageOpticalDisc.png", /* opticalMediaImageFile */
23
24    "<:ecere>places/folder.png",                    /* folder */
25    "<:ecere>status/folderOpen.png",               /* folderOpen */
26    "<:ecere>devices/computer.png",                 /* computer */
27    "<:ecere>devices/driveHardDisk.png",           /* drive */
28    "<:ecere>places/driveRemote.png",              /* netDrive */
29    "<:ecere>devices/mediaOptical.png",            /* cdrom */
30    "<:ecere>devices/driveRemovableMedia.png",    /* removable */
31    "<:ecere>devices/mediaFloppy.png",             /* floppy */
32    "<:ecere>places/networkWorkgroup.png",         /* network */
33    "<:ecere>places/networkServer.png",            /* server */
34    "<:ecere>places/folderRemote.png",             /* share */
35
36    "<:ecere>mimeTypes/package.png",      /* treeLoader */
37    "<:ecere>places/startHere.png",                /* lineNumbers */
38    
39    ""
40 };
41
42 //define guiApp = ((GuiApplication)__thisModule);
43 //define selectionColor = guiApp.currentSkin.selectionColor; //Color { 10, 36, 106 };
44
45 public enum __FileType
46 {
47    none,
48    
49    normalFile, ewsFile, epjFile, ecFile, ehFile, cFile, hFile, cppFile, hppFile,
50    textFile, webFile, pictureFile, soundFile,
51    archiveFile, packageFile, opticalMediaImageFile, /* these (all previous) are sort equal */
52    
53    folder, folderOpen, computer,
54    drive, netDrive, cdrom, removable, floppy, network, server, share, // these are sort equal
55    
56    // utilities
57    treeLoader,
58    lineNumbers;
59
60    /*property char * 
61    {
62       set
63       {
64          this = SelectByExtension(value);
65       }
66    }*/
67
68    public property bool isFolder
69    {
70       get { return this >= folder && this <= share; }
71    }
72
73    public property bool isFile
74    {
75       get { return this >= normalFile && this <= opticalMediaImageFile; }
76    }
77
78    __FileType ::SelectByExtension(char * extension)
79    {
80       if(!strcmpi(extension, "ews"))
81          return ewsFile;
82       else if(!strcmpi(extension, "epj"))
83          return epjFile;
84       else if(!strcmpi(extension, "ec"))
85          return ecFile;
86       else if(!strcmpi(extension, "eh"))
87          return ehFile;
88       else if(!strcmpi(extension, "cpp") ||
89             !strcmpi(extension, "cc") || !strcmpi(extension, "cxx"))
90          return cppFile;
91       else if(!strcmpi(extension, "hpp") ||
92             !strcmpi(extension, "hh") || !strcmpi(extension, "hxx"))
93          return hppFile;
94       else if(!strcmpi(extension, "c"))
95          return cFile;
96       else if(!strcmpi(extension, "h"))
97          return hFile;
98       else if(!strcmpi(extension, "txt") || !strcmpi(extension, "text") ||
99             !strcmpi(extension, "nfo") || !strcmpi(extension, "info"))
100          return textFile;
101       else if(!strcmpi(extension, "htm") || !strcmpi(extension, "html") ||
102             !strcmpi(extension, "css") || !strcmpi(extension, "php") ||
103             !strcmpi(extension, "js"))
104          return webFile;
105       else if(!strcmpi(extension, "bmp") || !strcmpi(extension, "pcx") ||
106             !strcmpi(extension, "jpg") || !strcmpi(extension, "jpeg") ||
107             !strcmpi(extension, "gif") || !strcmpi(extension, "png") ||
108             !strcmpi(extension, "ico"))
109          return pictureFile;
110       else if(!strcmpi(extension, "wav") || !strcmpi(extension, "mp3") ||
111             !strcmpi(extension, "ogg") || !strcmpi(extension, "snd"))
112          return soundFile;
113       else if(!strcmpi(extension, "ear") || !strcmpi(extension, "7z") ||
114             !strcmpi(extension, "rar") || !strcmpi(extension, "zip") ||
115             !strcmpi(extension, "gz") || !strcmpi(extension, "bz2") ||
116             !strcmpi(extension, "tar") || !strcmpi(extension, "arj") ||
117             !strcmpi(extension, "lza") || !strcmpi(extension, "lzh") ||
118             !strcmpi(extension, "cpio") || !strcmpi(extension, "z"))
119          return archiveFile;
120       else if(!strcmpi(extension, "cab") || !strcmpi(extension, "deb") ||
121             !strcmpi(extension, "rpm"))
122          return packageFile;
123       else if(!strcmpi(extension, "iso") || !strcmpi(extension, "mds") ||
124             !strcmpi(extension, "cue") || !strcmpi(extension, "bin") ||
125             !strcmpi(extension, "ccd") || !strcmpi(extension, "bwt") ||
126             !strcmpi(extension, "cdi") || !strcmpi(extension, "nrg"))
127          return opticalMediaImageFile;
128       return normalFile;
129    }
130 };
131
132 //public enum FileSystemNodeType { file, folder, system };
133
134 define guiApp = ((GuiApplication)__thisModule);  // how to do this in a dll?
135 define selectionColor = guiApp.currentSkin.selectionColor; //Color { 10, 36, 106 };
136
137 public class FileSystemNode : struct // : struct
138 {
139 public:
140    /*//LinkElement<FileSystemNode> link;
141    FileSystemNode parent;
142
143    FileSystemNodeType type;
144
145    char * name;*/
146
147    FileSystemNode prev, next;
148
149    bool loaded, childrenLoaded;
150    int indent;
151    char * path;
152    char * name;
153    char * extension;
154    char * info;
155    DataRow row;
156    OldList children;
157    __FileType type;
158    FileSystemNode parent;
159
160    FileStats stats;
161
162    Bitmap bitmap;
163
164    void GetPath(String outputPath)
165    {  
166       FileSystemNode up;
167       if(parent)
168       {
169          strcpy(outputPath, name);
170          for(up = parent; up; up = up.parent)
171          {
172             char temp[MAX_LOCATION];
173             strcpy(temp, up.name);
174             PathCat(temp, outputPath);
175             strcpy(outputPath, temp);
176          }
177       }
178       else
179 #ifdef __WIN32__
180          strcpy(outputPath, "/");
181 #else
182          strcpy(outputPath, name);
183 #endif
184
185    }
186
187    bool IsChildOf(FileSystemNode node)
188    {
189       FileSystemNode test;
190       for(test = parent; test; test = test.parent)
191          if(test == node)
192             return true;
193       return false;
194    }
195
196    void DuplicateChildren(bool recursive, bool forceExpanded, FileSystemNode addTo, FileSystemBox fsb)
197    {
198       if(children.first)
199       {
200          FileSystemNode child;
201          
202          for(child = children.first; child; child = child.next)
203          {
204             FileSystemNode copy { };
205             copy.name = CopyString(child.name);
206             copy.type = child.type;
207             fsb.AddTreeNode(copy, child.loaded, false, addTo);
208             if(forceExpanded)
209                copy.row.collapsed = false;
210             if(recursive)
211                child.DuplicateChildren(recursive, forceExpanded, copy, fsb);
212          }
213       }
214    }
215    
216    void EnsureVisible(bool expand)
217    {
218       if(parent)
219          parent.EnsureVisible(true);
220       if(expand)
221          row.collapsed = false;
222       // TODO: row.EnsureVisible(); // making the row visible by scrolling
223    }
224
225    void OnFree()
226    {
227       //delete name;
228    }
229
230    void Free()
231    {
232       FileSystemNode child;
233       for(; (child = children.first); )
234       {
235          child.Free();
236          children.Delete(child);
237       }
238       //if(name)
239       delete name;
240       delete info;
241    }
242
243    void Delete()
244    {
245       Free();
246       if(parent)
247          parent.children.Delete(this);
248    }
249
250    void OnDisplay(Surface surface, int x, int y, int width, FileSystemBox fsb, Alignment alignment, DataDisplayFlags displayFlags)
251    {
252       //int indentSize = (displayFlags.dropBox) ? 0 : 10;
253       int indent = 16;
254       int xStart;
255       int len;
256       int w, h;
257       //int textOffset;
258       char label[MAX_FILENAME];
259
260       Bitmap icon;
261
262       if(!this)
263          return;
264       
265       icon = fsb.fileIcons[type].bitmap;
266       /*if(type == normalFile)
267          PrintLn("dd");*/
268       //xStart = indent * indent + x + (icon ? (icon.width + 5) : 0);
269       xStart = x + (icon ? (icon.width + 5) : 0);
270
271       if(!name)
272          return;
273
274       if(info)
275          sprintf(label, "%s [%s]", name, info);
276       else
277          strcpy(label, name);
278       len = strlen(label);
279       
280       if(!icon)
281       {
282          if(type == folder || type == folderOpen)
283             surface.SetForeground(yellow);
284          //indentSize = 8;
285          indent = 8;
286       }
287       //textOffset = indent * indentSize + (icon ? (icon.width + 4) : 0);
288       
289       surface.TextOpacity(false);
290       surface.TextExtent(label, len, &w, &h);
291       h = Max(h, 16);
292     
293       // Draw the current row stipple
294       if(displayFlags.selected)
295          //surface.Area(xStart - 1, y, xStart - 1, y + h - 1);
296          //surface.Area(xStart + w - 1, y, xStart + w + 1, y + h - 1);
297          surface.Area(xStart - 3, y, xStart + w + 1, y + h - 1);
298       
299       //surface.WriteTextDots(alignment, x + textOffset, y + 2, width - textOffset, name, strlen(name));
300       surface.WriteTextDots(alignment, xStart, y + 2, width, label, len);
301
302       //if(!guiApp.textMode) -- how to do this in a dll?
303       {
304          if(displayFlags.current)
305          {
306             if(displayFlags.active)
307             {
308                surface.LineStipple(0x5555);
309                if(displayFlags.selected)
310                   surface.SetForeground(0xFFFFFF80);
311                else
312                   surface.SetForeground(black);
313             }
314             else
315             {
316                surface.SetForeground(selectionColor);
317             }
318             surface.Rectangle(xStart - 3, y, xStart + w + 1, y + h - 1);
319             surface.LineStipple(0);
320          }
321
322          if(icon)
323          {
324             //surface.blend = true;
325             //surface.alphaWrite = blend;
326             surface.SetForeground(white);
327             //surface.Blit(icon, x + indent * indentSize, y,0,0, icon.width, icon.height);
328             surface.Blit(icon, x,y,0,0, icon.width, icon.height);
329          }
330       }
331    }
332
333    int OnCompare(FileSystemNode b)
334    {
335       int result;
336       FileSystemNode a = this;
337       if(a.type == b.type || (a.type < folder && b.type < folder) || (a.type >= drive))
338          result = strcmpi(a.name, b.name);
339       else
340       {
341          if(a.type == folder && b.type < folder) result = -1;
342          else if(a.type < folder && b.type == folder) result = 1;
343          else result = 0;
344       }
345       return result;
346    }
347
348    /*int OnCompare(FileSystemNode b)
349    {
350       int result;
351       FileSystemNode a = this;
352       if(a.parent < b.parent) result = -1;
353       else if(a.parent > b.parent) result = 1;
354       else
355          result = fstrcmp(a.name, b.name);
356       return result;
357    }*/
358
359    char * OnGetString(char * tempString, FileSystemToolWindow fileSysToolWnd, bool * needClass)
360    {
361       return name ? name : "";
362    }
363 }
364
365 /*FileSystemNode MakeFileSystemNode(const FileStats stats, const char * name)
366 {
367    FileSystemNode node { stats = stats };
368    node.name = CopyString(name);
369    if(!node.name)
370       node.name = null;
371    if(stats.attribs.isDirectory)
372    {
373       node.type = (stats.attribs.isDrive) ? drive : folder;
374       if(stats.attribs.isServer) node.type = server;
375       if(stats.attribs.isShare) node.type = share;
376       if(stats.attribs.isCDROM) node.type = cdrom;
377       if(stats.attribs.isRemote) node.type = netDrive;
378       if(stats.attribs.isRemovable) 
379       {
380          if(name[0] == 'A' || name[0] == 'B')
381             node.type = floppy;
382          else
383             node.type = removable;
384       }
385    }
386    else
387    {
388       char extension[MAX_EXTENSION];
389       GetExtension(node.name, extension);
390       node.type = __FileType::SelectByExtension(extension);
391    }
392    return node;
393 }*/
394 static FileSystemNode MakeFileSystemNode(const FileStats stats,
395       const char * fileName, const char * filePath,
396       const bool previewPicture, const DisplaySystem displaySystem)
397 {
398    int len = strlen(fileName);
399    char info[MAX_LOCATION];
400    char name[MAX_LOCATION];
401    char extension[MAX_EXTENSION];
402    
403    FileSystemNode node { stats = stats };
404
405    //if(stats.attribs.isFile) // TODO fix this in ecere
406    if(stats.attribs.isDirectory)
407    {
408       extension[0] = '\0';
409
410       node.type = (stats.attribs.isDrive) ? drive : folder;
411       if(stats.attribs.isServer) node.type = server;
412       if(stats.attribs.isShare) node.type = share;
413       if(stats.attribs.isCDROM) node.type = cdrom;
414       if(stats.attribs.isRemote) node.type = netDrive;
415       if(stats.attribs.isRemovable) 
416       {
417          if(fileName[0] == 'A' || fileName[0] == 'B')
418             node.type = floppy;
419          else
420             node.type = removable;
421       }
422    }
423    else
424    {
425       GetExtension(fileName, extension);
426       strlwr(extension);
427       
428       node.type = __FileType::SelectByExtension(extension);
429    }
430
431    if(stats.attribs.isDrive && 
432          len > 3 && !strncmp(&fileName[1], ": [", 3))
433    {
434       strncpy(name, fileName, 2);
435       name[2] = 0;
436       strncpy(info, &fileName[4], len - 5);
437       info[len - 5] = 0;
438    }
439    else
440    {
441       strcpy(name, fileName);
442       info[0] = 0;
443    }
444
445    node.path = CopyString(filePath);
446    node.name = CopyString(name);
447    if(info[0])
448       node.info = CopyString(info);
449    node.extension = CopyString(extension);
450
451    if(node.type == pictureFile && previewPicture)
452    {
453       node.bitmap = Bitmap { };
454       node.bitmap.Load(filePath, null, displaySystem);
455    }
456
457    return node;
458 }
459
460 class FileSystemBoxBits
461 {
462    bool foldersOnly:1, filesOnly:1, details:1, treeBranches:1, previewPictures:1;
463    //bool header:1, freeSelect:1, fullRowSelect:1, multiSelect:1, autoScroll:1, alwaysHL : 1, moveRows:1, resizable:1;
464    //bool moveFields:1, clearHeader:1, alwaysEdit:1, collapse:1, treeBranch:1, rootCollapse:1, heightSet:1;
465    //bool sortable:1, noDragging:1, fillLastField:1, expandOnAdd:1;
466 };
467
468 public class FileSystemBox : Window // should we not derive from ListBox instead?
469 /*
470    this stuff from the listbox would be nicely exposed...
471       fullRowSelect = false;
472       treeBranches = true;
473       collapseControl = true;
474       rootCollapseButton = true;
475       sortable = true;
476 */
477 {
478    borderStyle = deep;
479    hasHorzScroll = false;
480    hasVertScroll = false;
481
482    menu = Menu { };
483
484 public:
485    FileSystemNode root;
486    FileSystemNode selection;
487
488    virtual bool Window::NotifyNodeSelect(FileSystemBox box, FileSystemNode node);
489    
490    property char * path
491    {
492       set
493       {
494          delete path;
495          if(value && value[0])
496             path = CopyString(value);
497          if(created)
498             Load();
499       }
500
501       get { return path; }
502       //isset { return path && path[0]; }
503    }
504
505    property bool foldersOnly { set { bits.foldersOnly = value; bits.filesOnly = !value; } get { return bits.foldersOnly; } };
506    property bool filesOnly { set { bits.filesOnly = value; bits.foldersOnly = !value; } get { return bits.filesOnly; } };
507    property bool previewPictures { set { bits.previewPictures = value; } get { return bits.previewPictures; } };
508    property char * extensions { set { delete extensions; if(value && value[0]) extensions = CopyString(value); } get { return extensions; } }
509    property bool details { set { bits.details = value; } get { return bits.details; } };
510    property bool treeBranches
511    {
512       set
513       {
514          bits.treeBranches = value;
515          list.treeBranches = value;
516          list.collapseControl = value;
517          list.rootCollapseButton = value;
518       }
519       get { return bits.treeBranches; }
520    };
521    
522    property FileSystemNode node
523    {
524       get
525       {
526          if(!list)
527             return null;
528          if(!list.currentRow)
529             return null;
530          if(!list.currentRow.tag)
531             return null;
532          return (FileSystemNode)list.currentRow.tag;
533       }
534    }
535
536    void Select(FileSystemNode node)
537    {
538       if(node.row)
539       {
540          node.EnsureVisible(false);
541          list.SelectRow(node.row);
542       }
543    }
544
545    FileSystemNode Find(const char * name, FileSystemNode parent)
546    {
547       FileSystemNode node;
548       FileSystemNode start = parent ? parent : root;
549       if(!start.loaded || !start.childrenLoaded)
550          LoadTreeNode(start);
551       for(node = start.children.first; node; node = node.next)
552          if(node.name && !strcmpi(node.name, name))
553             return node;
554       return null;
555    }
556
557 private:
558    FileSystemBoxBits bits;
559
560    char * path;
561    char * extensions;
562
563    BitmapResource fileIcons[__FileType];
564
565    FileSystemBox()
566    {
567       char wd[MAX_LOCATION];
568       GetWorkingDir(wd, sizeof(wd));
569       property::path = wd;
570       
571       InitFileIcons();
572       list.AddField(nameField);
573    }
574    ~FileSystemBox()
575    {
576       delete extensions;
577       delete path;
578    }
579    void InitFileIcons()
580    {
581       __FileType c;
582       for(c = 0; c < __FileType::enumSize; c++)
583       {
584          fileIcons[c] = BitmapResource { fileIconNames[c], alphaBlend = true };
585          AddResource(fileIcons[c]);
586       }
587    }
588
589    DataField nameField { dataType = "FileSystemNode", width = 240, userData = this };
590    DataField typeField { header = "Type", dataType = /*"String"*/ "char *", width = 40, freeData = false };
591    DataField sizeField { header = "Size", dataType = "FileSize", width = 96, alignment = right };
592
593    bool OnPostCreate()
594    {
595       Load();
596       return true;
597    }
598
599    ListBox list
600    {
601       this;
602
603       borderStyle = none;
604       hasHorzScroll = true;
605       hasVertScroll = true;
606       fullRowSelect = false;
607       sortable = true;
608
609       anchor = Anchor { left = 0, top = 0, right = 0, bottom = 0 };
610
611       // WHY is this not working ?
612       /*void OnResize(int width, int height)
613       {
614          if(vertScroll.visible)
615             nameField.width = width - vertScroll.size.w;
616          else
617             nameField.width = width;
618       }*/
619
620       bool NotifyCollapse(ListBox listBox, DataRow row, bool collapsed)
621       {
622          if(row)
623          {
624             FileSystemNode node = (FileSystemNode)row.tag;
625             FileSystemNode child;
626             if(collapsed)
627             {
628                /*
629                for(child = node.children.last; child; child = node.children.last)
630                {
631                   listBox.DeleteRow(child.row);
632                   child.Free();
633                   delete child;
634                }
635                node.childrenLoaded = false;
636                */
637             }
638             else
639             {
640                if(!node.loaded || !node.childrenLoaded)
641                {
642                   LoadTreeNode(node);
643                   //list.Sort(nameField, 1);
644                   //node.
645                }
646                for(child = node.children.first; child && child.next; child = child.next);
647                if(child)
648                   child.EnsureVisible(false);
649             }
650          }
651          return true;
652       }
653       
654       bool NotifyRightClick(ListBox listBox, int x, int y, Modifiers mods)
655       {
656          DataRow row = listBox.currentRow;
657          if(row)
658          {
659             FileSystemNode node = (FileSystemNode)row.tag;
660             if(node)
661             {
662                PopupMenu popup;
663                Menu menu { };
664
665                MenuItem { menu, "Cut\tCtrl+X", t, NotifySelect = null, disabled = false };
666                MenuItem { menu, "Copy\tCtrl+C", c, NotifySelect = null, disabled = false };
667                MenuItem { menu, "Paste\tCtrl+V", p, NotifySelect = null, disabled = false /*!clipboard*/ };
668                MenuItem { menu, "Delete\tDel", d, NotifySelect = null, disabled = false };
669                //MenuDivider { menu };
670
671                popup = PopupMenu
672                   {
673                      master = this, menu = menu,
674                      position = { 
675                         x + clientStart.x + absPosition.x - guiApp.desktop.position.x, 
676                         y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
677                   };
678                popup.Create();
679             }
680          }
681          return true;
682       }
683
684       bool NotifySelect(ListBox listBox, DataRow row, Modifiers mods)
685       {
686          if(row)
687          {
688             FileSystemNode node = (FileSystemNode)row.tag;
689             NotifyNodeSelect(listBox.parent.master, this, node);
690             selection = node;
691          }
692          return true;
693       }
694
695       bool NotifyEditing(ListBox listBox, DataRow row)
696       {
697          if(row)
698          {
699             FileSystemNode node = (FileSystemNode)row.tag;
700          }
701          return true;
702       }
703
704       bool NotifyEdited(ListBox listBox, DataRow row)
705       {
706          if(row)
707          {
708             FileSystemNode node = (FileSystemNode)row.tag;
709          }
710          return true;
711       }
712
713       bool NotifyEditDone(ListBox listBox, DataRow row)
714       {
715          if(row)
716          {
717             FileSystemNode node = (FileSystemNode)row.tag;
718          }
719          return true;
720       }
721
722    };
723
724    // Edit Menu
725    Menu editMenu { menu, "Edit", e };
726    MenuItem itemEditCut
727    {
728       editMenu, "Cut\tCtrl+X", t, disabled = true;
729
730       bool NotifySelect(MenuItem selection, Modifiers mods)
731       {
732          //EditCut();
733          return true;
734       }
735    };
736    MenuItem itemEditCopy
737    {
738       editMenu, "Copy\tCtrl+C", c, disabled = true;
739
740       bool NotifySelect(MenuItem selection, Modifiers mods)
741       {
742          //EditCopy();
743          return true;
744       }
745    };
746    MenuItem itemEditPaste
747    {
748       editMenu, "Paste\tCtrl+V", p;
749    
750       bool NotifySelect(MenuItem selection, Modifiers mods)
751       {
752          //EditPaste();
753          return true;
754       }
755    };
756    MenuItem itemEditDelete
757    {
758       editMenu, "Delete\tDel", d, disabled = true;
759
760       bool NotifySelect(MenuItem selection, Modifiers mods)
761       {
762          //EditDelete();
763          return true;
764       }
765    };
766
767    // WHY is this crashing ? 
768    /*void OnResize(int width, int height)
769    {
770       if(this && nameField)
771          nameField.width = width - 80;
772    }*/
773
774    void Load()
775    {
776       // TODO: fix this!
777       // this is crashing in for designer when details = true // can't save the file, always yields a crash
778       /*if(list && created)
779       {
780          list.ClearFields();
781          list.AddField(nameField);
782          if(bits.details)
783          {
784             list.AddField(typeField);
785             list.AddField(sizeField);
786          }
787       }*/
788       if(bits.treeBranches)
789          LoadTree();
790       else
791          LoadList();
792    }
793
794    void LoadList()
795    {
796       FileListing listing { path, extensions = extensions };
797
798       list.Clear();
799       while(listing.Find())
800       {
801          if((!bits.foldersOnly && !bits.filesOnly) ||
802             (bits.foldersOnly && listing.stats.attribs.isDirectory) ||
803             (bits.filesOnly && !listing.stats.attribs.isDirectory/*listing.stats.attribs.isFile*/)) // TOCHECK: isFile broken?
804          {
805             FileSystemNode node = MakeFileSystemNode(listing.stats, listing.name, listing.path, bits.previewPictures, displaySystem);
806             DataRow row = list.AddRow();
807             row.tag = (int)node;
808             row.SetData(nameField, node);
809             if(bits.details)
810             {
811                row.SetData(typeField, node.extension);
812                row.SetData(sizeField, (void *)node.stats.size);
813             }
814          }
815       }
816       list.Sort(nameField, 1);
817    }
818
819    void LoadTree()
820    {
821       //char startPath[MAX_LOCATION];
822       FileSystemNode parent;
823       FileSystemNode node;
824       FileListing listing { path, extensions = extensions };
825       
826       /*if(!path)
827          GetWorkingDir(startPath, sizeof(startPath));
828       else
829          strcpy(path, startPath);*/
830       
831       list.Clear();
832
833       delete root;
834       //root = FileSystemNode { type = computer, loaded = true, childrenLoaded = true };
835       root = MakeFileSystemNode(FileStats { attribs = FileExists(path)}, path, path, bits.previewPictures, displaySystem);
836    #ifdef __WIN32__
837       //root.name = rootName;
838       AddTreeNode(root, true, false, null, list);
839    #else
840       //root.name = "/";
841    #endif
842       AddTreeNode(root, false, true, null);
843
844    // How can this make sense for linux? 
845    #ifdef __WIN32__
846       while(listing.Find())
847       {
848          int len = strlen(listing.name);
849          char info[MAX_LOCATION];
850          char name[MAX_LOCATION];
851          if(listing.stats.attribs.isDrive && 
852                len > 3 && !strncmp(&listing.name[1], ": [", 3))
853          {
854             strncpy(name, listing.name, 2);
855             name[2] = 0;
856             strncpy(info, &listing.name[4], len - 5);
857             info[len - 5] = 0;
858          }
859          else
860          {
861             strcpy(name, listing.name);
862             info[0] = 0;
863          }
864
865          parent = MakeFileSystemNode(listing.stats, name);
866          if(info[0])
867             parent.info = CopyString(info);
868          parent.loaded = true;
869          AddTreeNode(parent, !listing.stats.attribs.isDirectory, listing.stats.attribs.isDirectory, root, this);
870          if(!listing.stats.attribs.isDirectory)
871             parent.childrenLoaded = true;
872       }
873
874       node = FileSystemNode { name = msNetwork, type = network };
875       AddTreeNode(node, false, true, null, this);
876       node.row.collapsed = true;
877       Sort(nameField, 1);
878       SelectRow(root.row);
879    #endif
880       LoadTreeNode(root);
881       list.Sort(nameField, 1);
882    }
883
884    void LoadTreeNode(FileSystemNode node)
885    {
886       if(!node.loaded)
887       {
888          char path[MAX_LOCATION];
889          node.GetPath(path);
890          {
891             FileListing listing { path, extensions = extensions };
892             if(node.children.count == 1)
893             DeleteNode(node.children.first);
894
895             while(listing.Find())
896             {
897                if((!bits.foldersOnly && !bits.filesOnly) ||
898                   (bits.foldersOnly && listing.stats.attribs.isDirectory) ||
899                   (bits.filesOnly && !listing.stats.attribs.isDirectory/*listing.stats.attribs.isFile*/)) // TOCHECK: isFile broken?
900                {
901                   FileSystemNode child = MakeFileSystemNode(listing.stats, listing.name, listing.path, bits.previewPictures, displaySystem);
902                   AddTreeNode(child, true, false, node);
903                   NodeChildLoad(child, node);
904                }
905             }
906          }
907          node.childrenLoaded = true;
908          node.loaded = true;
909          node.row.SortSubRows(false);
910       }
911       else if(!node.childrenLoaded)
912       {
913          FileSystemNode child;
914          if(node.children.first)
915          {
916             for(child = node.children.first; child; child = child.next)
917             {
918                if(!child.loaded)
919                   LoadTreeNode(child);
920                else if(!child.childrenLoaded)
921                   NodeChildLoad(child, node);
922             }
923             node.childrenLoaded = true;
924             node.row.SortSubRows(false);
925          }
926       }
927    }
928
929    void NodeChildLoad(FileSystemNode parent, FileSystemNode node)
930    {
931       char path[MAX_LOCATION];
932       parent.GetPath(path);
933       {
934          bool added = false;
935          FileListing listing { path, extensions = extensions };
936          while(listing.Find())
937          {
938             if((!bits.foldersOnly && !bits.filesOnly) ||
939                (bits.foldersOnly && listing.stats.attribs.isDirectory) ||
940                (bits.filesOnly && !listing.stats.attribs.isDirectory/*listing.stats.attribs.isFile*/)) // TOCHECK: isFile broken?
941             {
942                FileSystemNode child = MakeFileSystemNode(listing.stats, listing.name, listing.path, bits.previewPictures, displaySystem);
943                AddTreeNode(child, true, false, parent);
944                added = true;
945             }
946          }
947          if(!added)
948             added = true;
949       }
950       //parent.childrenLoaded = true;
951    }
952
953    void AddTreeNode(FileSystemNode node, bool loaded, bool addLoader, FileSystemNode addTo)
954    {
955       DataRow row = (addTo && addTo.row) ? addTo.row.AddRow() : list.AddRow();
956       if(addTo)
957       {
958          node.parent = addTo;
959          node.indent = addTo.indent + 1;
960          addTo.children.Add(node);
961       }
962       row.tag = (int)node;
963       node.row = row;
964       row.SetData(null, node);
965
966       node.loaded = loaded;
967       if(addLoader)
968          //AddTreeNode(FileSystemNode { }, false, false, node); // why would this create a compile error?
969          AddTreeNode(FileSystemNode { type = none }, false, false, node);
970
971       if(node.indent > 0)
972          row.collapsed = true;
973       else if(node.type == folder)
974          node.type = folderOpen;
975    }
976
977    void DeleteNode(FileSystemNode node)
978    {
979       FileSystemNode child;
980       for(; (child = node.children.first); )
981          DeleteNode(child);
982       list.DeleteRow(node.row);
983       node.Delete();
984    }
985 /*
986 public class ClipBoardFiles
987 {
988
989 public:
990
991    property
992
993 }
994
995    // CLIPBOARD
996    void Copy()
997    {
998       if(this)
999       {
1000          int size = SelSize();
1001          if(size)
1002          {
1003             // Try to allocate memory
1004             ClipBoard clipBoard { };
1005             if(clipBoard.Allocate(size+1))
1006             {
1007                GetSel(clipBoard.memory, true);   
1008                // Save clipboard
1009                clipBoard.Save();
1010             }
1011             delete clipBoard;
1012          }
1013       }
1014    }
1015
1016    void Paste()
1017    {
1018       if(this)
1019       {
1020          ClipBoard clipBoard { };
1021          if(clipBoard.Load())
1022             PutS(clipBoard.memory);
1023          delete clipBoard;
1024       }
1025    }
1026
1027    void Cut()
1028    {
1029       if(this)
1030       {
1031          Copy();
1032          DelSel();
1033          SetViewToCursor(true);
1034          Modified();
1035       }
1036    }
1037
1038 Private Type DROPFILES
1039    pFiles As Long
1040    pt As POINTAPI
1041    fNC As Long
1042    fWide As Long
1043 End Type
1044 For iCounter = 0 To filelist.ListCount - 1
1045   If filelist.Selected(iCounter) = True Then
1046     strFiles = strFiles & FixPath(filelist.Path) & filelist.List(iCounter) & vbNullChar
1047   End If
1048 Next
1049 'all selected items are now put in strFiles
1050
1051 hGlobal = GlobalAlloc(GHND, Len(DF) + Len(strFiles)) 'put all files to a exclusive number
1052 If hGlobal Then 'if the globalalloc worked
1053   lpGlobal = GlobalLock(hGlobal) 'lock the hGlobal
1054   DF.pFiles = Len(DF) 'set the size of the files
1055   
1056   Call CopyMem(ByVal lpGlobal, DF, Len(DF)) 'copy df to the lpglobal
1057   Call CopyMem(ByVal (lpGlobal + Len(DF)), ByVal strFiles, Len(strFiles)) 'copy strfiles to lpglobal
1058   Call GlobalUnlock(hGlobal) 'unlock hglobal again
1059   
1060   SetClipboardData CF_HDROP, hGlobal 'put files to the clipboard
1061 End If
1062
1063    bool SaveFile(const char * filePath)
1064    {
1065    }
1066 */
1067
1068 }