libede: FileSystemBox: added filesOnly option
[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, const char * fileName, const char * filePath, const bool previewPicture, const DisplaySystem displaySystem)
395 {
396    int len = strlen(fileName);
397    char info[MAX_LOCATION];
398    char name[MAX_LOCATION];
399    char extension[MAX_EXTENSION];
400    
401    FileSystemNode node { stats = stats };
402
403    //if(stats.attribs.isFile) // TODO fix this in ecere
404    if(stats.attribs.isDirectory)
405    {
406       extension[0] = '\0';
407
408       node.type = (stats.attribs.isDrive) ? drive : folder;
409       if(stats.attribs.isServer) node.type = server;
410       if(stats.attribs.isShare) node.type = share;
411       if(stats.attribs.isCDROM) node.type = cdrom;
412       if(stats.attribs.isRemote) node.type = netDrive;
413       if(stats.attribs.isRemovable) 
414       {
415          if(fileName[0] == 'A' || fileName[0] == 'B')
416             node.type = floppy;
417          else
418             node.type = removable;
419       }
420    }
421    else
422    {
423       GetExtension(fileName, extension);
424       strlwr(extension);
425       
426       node.type = __FileType::SelectByExtension(extension);
427    }
428
429    if(stats.attribs.isDrive && 
430          len > 3 && !strncmp(&fileName[1], ": [", 3))
431    {
432       strncpy(name, fileName, 2);
433       name[2] = 0;
434       strncpy(info, &fileName[4], len - 5);
435       info[len - 5] = 0;
436    }
437    else
438    {
439       strcpy(name, fileName);
440       info[0] = 0;
441    }
442
443    node.path = CopyString(filePath);
444    node.name = CopyString(name);
445    if(info[0])
446       node.info = CopyString(info);
447    node.extension = CopyString(extension);
448
449    if(node.type == pictureFile && previewPicture)
450    {
451       node.bitmap = Bitmap { };
452       node.bitmap.Load(filePath, null, displaySystem);
453    }
454
455    return node;
456 }
457
458 class FileSystemBoxBits
459 {
460    bool foldersOnly:1, filesOnly:1, details:1, treeBranches:1, previewPictures:1;
461    //bool header:1, freeSelect:1, fullRowSelect:1, multiSelect:1, autoScroll:1, alwaysHL : 1, moveRows:1, resizable:1;
462    //bool moveFields:1, clearHeader:1, alwaysEdit:1, collapse:1, treeBranch:1, rootCollapse:1, heightSet:1;
463    //bool sortable:1, noDragging:1, fillLastField:1, expandOnAdd:1;
464 };
465
466 public class FileSystemBox : Window // should we not derive from ListBox instead?
467 /*
468    this stuff from the listbox would be nicely exposed...
469       fullRowSelect = false;
470       treeBranches = true;
471       collapseControl = true;
472       rootCollapseButton = true;
473       sortable = true;
474 */
475 {
476    borderStyle = deep;
477    hasHorzScroll = false;
478    hasVertScroll = false;
479
480    menu = Menu { };
481
482 public:
483    FileSystemNode root;
484    FileSystemNode selection;
485
486    virtual bool Window::NotifyNodeSelect(FileSystemBox box, FileSystemNode node);
487    
488    property char * path
489    {
490       set
491       {
492          delete path;
493          if(value && value[0])
494             path = CopyString(value);
495          if(created)
496             Load();
497       }
498
499       get { return path; }
500       //isset { return path && path[0]; }
501    }
502
503    property bool foldersOnly { set { bits.foldersOnly = value; bits.filesOnly = !value; } get { return bits.foldersOnly; } };
504    property bool filesOnly { set { bits.filesOnly = value; bits.foldersOnly = !value; } get { return bits.filesOnly; } };
505    property bool previewPictures { set { bits.previewPictures = value; } get { return bits.previewPictures; } };
506    property char * extensions { set { delete extensions; if(value && value[0]) extensions = CopyString(value); } get { return extensions; } }
507    property bool details { set { bits.details = value; } get { return bits.details; } };
508    property bool treeBranches
509    {
510       set
511       {
512          bits.treeBranches = value;
513          list.treeBranches = value;
514          list.collapseControl = value;
515          list.rootCollapseButton = value;
516       }
517       get { return bits.treeBranches; }
518    };
519    
520    property FileSystemNode node
521    {
522       get
523       {
524          if(!list)
525             return null;
526          if(!list.currentRow)
527             return null;
528          if(!list.currentRow.tag)
529             return null;
530          return (FileSystemNode)list.currentRow.tag;
531       }
532    }
533
534    void Select(FileSystemNode node)
535    {
536       if(node.row)
537       {
538          node.EnsureVisible(false);
539          list.SelectRow(node.row);
540       }
541    }
542
543    FileSystemNode Find(const char * name, FileSystemNode parent)
544    {
545       FileSystemNode node;
546       FileSystemNode start = parent ? parent : root;
547       if(!start.loaded || !start.childrenLoaded)
548          LoadTreeNode(start);
549       for(node = start.children.first; node; node = node.next)
550          if(node.name && !strcmpi(node.name, name))
551             return node;
552       return null;
553    }
554
555 private:
556    FileSystemBoxBits bits;
557
558    char * path;
559    char * extensions;
560
561    BitmapResource fileIcons[__FileType];
562
563    FileSystemBox()
564    {
565       char wd[MAX_LOCATION];
566       GetWorkingDir(wd, sizeof(wd));
567       property::path = wd;
568       
569       InitFileIcons();
570       list.AddField(nameField);
571    }
572    ~FileSystemBox()
573    {
574       delete extensions;
575       delete path;
576    }
577    void InitFileIcons()
578    {
579       __FileType c;
580       for(c = 0; c < __FileType::enumSize; c++)
581       {
582          fileIcons[c] = BitmapResource { fileIconNames[c], alphaBlend = true };
583          AddResource(fileIcons[c]);
584       }
585    }
586
587    DataField nameField { dataType = "FileSystemNode", width = 240, userData = this };
588    DataField typeField { header = "Type", dataType = /*"String"*/ "char *", width = 40, freeData = false };
589    DataField sizeField { header = "Size", dataType = "FileSize", width = 96, alignment = right };
590
591    bool OnPostCreate()
592    {
593       Load();
594       return true;
595    }
596
597    ListBox list
598    {
599       this;
600
601       borderStyle = none;
602       hasHorzScroll = true;
603       hasVertScroll = true;
604       fullRowSelect = false;
605       sortable = true;
606
607       anchor = Anchor { left = 0, top = 0, right = 0, bottom = 0 };
608
609       // WHY is this not working ?
610       /*void OnResize(int width, int height)
611       {
612          if(vertScroll.visible)
613             nameField.width = width - vertScroll.size.w;
614          else
615             nameField.width = width;
616       }*/
617
618       bool NotifyCollapse(ListBox listBox, DataRow row, bool collapsed)
619       {
620          if(row)
621          {
622             FileSystemNode node = (FileSystemNode)row.tag;
623             FileSystemNode child;
624             if(collapsed)
625             {
626                /*
627                for(child = node.children.last; child; child = node.children.last)
628                {
629                   listBox.DeleteRow(child.row);
630                   child.Free();
631                   delete child;
632                }
633                node.childrenLoaded = false;
634                */
635             }
636             else
637             {
638                if(!node.loaded || !node.childrenLoaded)
639                {
640                   LoadTreeNode(node);
641                   //list.Sort(nameField, 1);
642                   //node.
643                }
644                for(child = node.children.first; child && child.next; child = child.next);
645                if(child)
646                   child.EnsureVisible(false);
647             }
648          }
649          return true;
650       }
651       
652       bool NotifyRightClick(ListBox listBox, int x, int y, Modifiers mods)
653       {
654          DataRow row = listBox.currentRow;
655          if(row)
656          {
657             FileSystemNode node = (FileSystemNode)row.tag;
658             if(node)
659             {
660                PopupMenu popup;
661                Menu menu { };
662
663                MenuItem { menu, "Cut\tCtrl+X", t, NotifySelect = null, disabled = false };
664                MenuItem { menu, "Copy\tCtrl+C", c, NotifySelect = null, disabled = false };
665                MenuItem { menu, "Paste\tCtrl+V", p, NotifySelect = null, disabled = false /*!clipboard*/ };
666                MenuItem { menu, "Delete\tDel", d, NotifySelect = null, disabled = false };
667                //MenuDivider { menu };
668
669                popup = PopupMenu
670                   {
671                      master = this, menu = menu,
672                      position = { 
673                         x + clientStart.x + absPosition.x - guiApp.desktop.position.x, 
674                         y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
675                   };
676                popup.Create();
677             }
678          }
679          return true;
680       }
681
682       bool NotifySelect(ListBox listBox, DataRow row, Modifiers mods)
683       {
684          if(row)
685          {
686             FileSystemNode node = (FileSystemNode)row.tag;
687             NotifyNodeSelect(listBox.parent.master, this, node);
688             selection = node;
689          }
690          return true;
691       }
692
693       bool NotifyEditing(ListBox listBox, DataRow row)
694       {
695          if(row)
696          {
697             FileSystemNode node = (FileSystemNode)row.tag;
698          }
699          return true;
700       }
701
702       bool NotifyEdited(ListBox listBox, DataRow row)
703       {
704          if(row)
705          {
706             FileSystemNode node = (FileSystemNode)row.tag;
707          }
708          return true;
709       }
710
711       bool NotifyEditDone(ListBox listBox, DataRow row)
712       {
713          if(row)
714          {
715             FileSystemNode node = (FileSystemNode)row.tag;
716          }
717          return true;
718       }
719
720    };
721
722    // Edit Menu
723    Menu editMenu { menu, "Edit", e };
724    MenuItem itemEditCut
725    {
726       editMenu, "Cut\tCtrl+X", t, disabled = true;
727
728       bool NotifySelect(MenuItem selection, Modifiers mods)
729       {
730          //EditCut();
731          return true;
732       }
733    };
734    MenuItem itemEditCopy
735    {
736       editMenu, "Copy\tCtrl+C", c, disabled = true;
737
738       bool NotifySelect(MenuItem selection, Modifiers mods)
739       {
740          //EditCopy();
741          return true;
742       }
743    };
744    MenuItem itemEditPaste
745    {
746       editMenu, "Paste\tCtrl+V", p;
747    
748       bool NotifySelect(MenuItem selection, Modifiers mods)
749       {
750          //EditPaste();
751          return true;
752       }
753    };
754    MenuItem itemEditDelete
755    {
756       editMenu, "Delete\tDel", d, disabled = true;
757
758       bool NotifySelect(MenuItem selection, Modifiers mods)
759       {
760          //EditDelete();
761          return true;
762       }
763    };
764
765    // WHY is this crashing ? 
766    /*void OnResize(int width, int height)
767    {
768       if(this && nameField)
769          nameField.width = width - 80;
770    }*/
771
772    void Load()
773    {
774       // TODO: fix this!
775       // this is crashing in for designer when details = true // can't save the file, always yields a crash
776       /*if(list && created)
777       {
778          list.ClearFields();
779          list.AddField(nameField);
780          if(bits.details)
781          {
782             list.AddField(typeField);
783             list.AddField(sizeField);
784          }
785       }*/
786       if(bits.treeBranches)
787          LoadTree();
788       else
789          LoadList();
790    }
791
792    void LoadList()
793    {
794       FileListing listing { path, extensions = extensions };
795
796       list.Clear();
797       while(listing.Find())
798       {
799          if((!bits.foldersOnly && !bits.filesOnly) ||
800             (bits.foldersOnly && listing.stats.attribs.isDirectory) ||
801             (bits.filesOnly && !listing.stats.attribs.isDirectory/*listing.stats.attribs.isFile*/)) // TOCHECK: isFile broken?
802          {
803             FileSystemNode node = MakeFileSystemNode(listing.stats, listing.name, listing.path, bits.previewPictures, displaySystem);
804             DataRow row = list.AddRow();
805             row.tag = (int)node;
806             row.SetData(nameField, node);
807             if(bits.details)
808             {
809                row.SetData(typeField, node.extension);
810                row.SetData(sizeField, (void *)node.stats.size);
811             }
812          }
813       }
814       list.Sort(nameField, 1);
815    }
816
817    void LoadTree()
818    {
819       //char startPath[MAX_LOCATION];
820       FileSystemNode parent;
821       FileSystemNode node;
822       FileListing listing { path, extensions = extensions };
823       
824       /*if(!path)
825          GetWorkingDir(startPath, sizeof(startPath));
826       else
827          strcpy(path, startPath);*/
828       
829       list.Clear();
830
831       delete root;
832       //root = FileSystemNode { type = computer, loaded = true, childrenLoaded = true };
833       root = MakeFileSystemNode(FileStats { attribs = FileExists(path)}, path, path, bits.previewPictures, displaySystem);
834    #ifdef __WIN32__
835       //root.name = rootName;
836       AddTreeNode(root, true, false, null, list);
837    #else
838       //root.name = "/";
839    #endif
840       AddTreeNode(root, false, true, null);
841
842    // How can this make sense for linux? 
843    #ifdef __WIN32__
844       while(listing.Find())
845       {
846          int len = strlen(listing.name);
847          char info[MAX_LOCATION];
848          char name[MAX_LOCATION];
849          if(listing.stats.attribs.isDrive && 
850                len > 3 && !strncmp(&listing.name[1], ": [", 3))
851          {
852             strncpy(name, listing.name, 2);
853             name[2] = 0;
854             strncpy(info, &listing.name[4], len - 5);
855             info[len - 5] = 0;
856          }
857          else
858          {
859             strcpy(name, listing.name);
860             info[0] = 0;
861          }
862
863          parent = MakeFileSystemNode(listing.stats, name);
864          if(info[0])
865             parent.info = CopyString(info);
866          parent.loaded = true;
867          AddTreeNode(parent, !listing.stats.attribs.isDirectory, listing.stats.attribs.isDirectory, root, this);
868          if(!listing.stats.attribs.isDirectory)
869             parent.childrenLoaded = true;
870       }
871
872       node = FileSystemNode { name = msNetwork, type = network };
873       AddTreeNode(node, false, true, null, this);
874       node.row.collapsed = true;
875       Sort(nameField, 1);
876       SelectRow(root.row);
877    #endif
878       LoadTreeNode(root);
879       list.Sort(nameField, 1);
880    }
881
882    void LoadTreeNode(FileSystemNode node)
883    {
884       if(!node.loaded)
885       {
886          char path[MAX_LOCATION];
887          node.GetPath(path);
888          {
889             FileListing listing { path, extensions = extensions };
890             if(node.children.count == 1)
891             DeleteNode(node.children.first);
892
893             while(listing.Find())
894             {
895                if((!bits.foldersOnly && !bits.filesOnly) ||
896                   (bits.foldersOnly && listing.stats.attribs.isDirectory) ||
897                   (bits.filesOnly && !listing.stats.attribs.isDirectory/*listing.stats.attribs.isFile*/)) // TOCHECK: isFile broken?
898                {
899                   FileSystemNode child = MakeFileSystemNode(listing.stats, listing.name, listing.path, bits.previewPictures, displaySystem);
900                   AddTreeNode(child, true, false, node);
901                   NodeChildLoad(child, node);
902                }
903             }
904          }
905          node.childrenLoaded = true;
906          node.loaded = true;
907          node.row.SortSubRows(false);
908       }
909       else if(!node.childrenLoaded)
910       {
911          FileSystemNode child;
912          if(node.children.first)
913          {
914             for(child = node.children.first; child; child = child.next)
915             {
916                if(!child.loaded)
917                   LoadTreeNode(child);
918                else if(!child.childrenLoaded)
919                   NodeChildLoad(child, node);
920             }
921             node.childrenLoaded = true;
922             node.row.SortSubRows(false);
923          }
924       }
925    }
926
927    void NodeChildLoad(FileSystemNode parent, FileSystemNode node)
928    {
929       char path[MAX_LOCATION];
930       parent.GetPath(path);
931       {
932          bool added = false;
933          FileListing listing { path, extensions = extensions };
934          while(listing.Find())
935          {
936             if((!bits.foldersOnly && !bits.filesOnly) ||
937                (bits.foldersOnly && listing.stats.attribs.isDirectory) ||
938                (bits.filesOnly && !listing.stats.attribs.isDirectory/*listing.stats.attribs.isFile*/)) // TOCHECK: isFile broken?
939             {
940                FileSystemNode child = MakeFileSystemNode(listing.stats, listing.name, listing.path, bits.previewPictures, displaySystem);
941                AddTreeNode(child, true, false, parent);
942                added = true;
943             }
944          }
945          if(!added)
946             added = true;
947       }
948       //parent.childrenLoaded = true;
949    }
950
951    void AddTreeNode(FileSystemNode node, bool loaded, bool addLoader, FileSystemNode addTo)
952    {
953       DataRow row = (addTo && addTo.row) ? addTo.row.AddRow() : list.AddRow();
954       if(addTo)
955       {
956          node.parent = addTo;
957          node.indent = addTo.indent + 1;
958          addTo.children.Add(node);
959       }
960       row.tag = (int)node;
961       node.row = row;
962       row.SetData(null, node);
963
964       node.loaded = loaded;
965       if(addLoader)
966          //AddTreeNode(FileSystemNode { }, false, false, node); // why would this create a compile error?
967          AddTreeNode(FileSystemNode { type = none }, false, false, node);
968
969       if(node.indent > 0)
970          row.collapsed = true;
971       else if(node.type == folder)
972          node.type = folderOpen;
973    }
974
975    void DeleteNode(FileSystemNode node)
976    {
977       FileSystemNode child;
978       for(; (child = node.children.first); )
979          DeleteNode(child);
980       list.DeleteRow(node.row);
981       node.Delete();
982    }
983 /*
984 public class ClipBoardFiles
985 {
986
987 public:
988
989    property
990
991 }
992
993    // CLIPBOARD
994    void Copy()
995    {
996       if(this)
997       {
998          int size = SelSize();
999          if(size)
1000          {
1001             // Try to allocate memory
1002             ClipBoard clipBoard { };
1003             if(clipBoard.Allocate(size+1))
1004             {
1005                GetSel(clipBoard.memory, true);   
1006                // Save clipboard
1007                clipBoard.Save();
1008             }
1009             delete clipBoard;
1010          }
1011       }
1012    }
1013
1014    void Paste()
1015    {
1016       if(this)
1017       {
1018          ClipBoard clipBoard { };
1019          if(clipBoard.Load())
1020             PutS(clipBoard.memory);
1021          delete clipBoard;
1022       }
1023    }
1024
1025    void Cut()
1026    {
1027       if(this)
1028       {
1029          Copy();
1030          DelSel();
1031          SetViewToCursor(true);
1032          Modified();
1033       }
1034    }
1035
1036 Private Type DROPFILES
1037    pFiles As Long
1038    pt As POINTAPI
1039    fNC As Long
1040    fWide As Long
1041 End Type
1042 For iCounter = 0 To filelist.ListCount - 1
1043   If filelist.Selected(iCounter) = True Then
1044     strFiles = strFiles & FixPath(filelist.Path) & filelist.List(iCounter) & vbNullChar
1045   End If
1046 Next
1047 'all selected items are now put in strFiles
1048
1049 hGlobal = GlobalAlloc(GHND, Len(DF) + Len(strFiles)) 'put all files to a exclusive number
1050 If hGlobal Then 'if the globalalloc worked
1051   lpGlobal = GlobalLock(hGlobal) 'lock the hGlobal
1052   DF.pFiles = Len(DF) 'set the size of the files
1053   
1054   Call CopyMem(ByVal lpGlobal, DF, Len(DF)) 'copy df to the lpglobal
1055   Call CopyMem(ByVal (lpGlobal + Len(DF)), ByVal strFiles, Len(strFiles)) 'copy strfiles to lpglobal
1056   Call GlobalUnlock(hGlobal) 'unlock hglobal again
1057   
1058   SetClipboardData CF_HDROP, hGlobal 'put files to the clipboard
1059 End If
1060
1061    bool SaveFile(const char * filePath)
1062    {
1063    }
1064 */
1065
1066 }