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