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