libede: removed old imports (broken compile, migration)
[ede] / libede / src / FileSystemBox.ec
index 1a32a80..fae4b14 100644 (file)
@@ -1,5 +1,16 @@
 public import "ecere"
 
+#ifdef __WIN32__
+static char * rootName = "Entire Computer";
+static char * msNetwork = "Microsoft Windows Network";
+#else
+static char * rootName = "File System";
+#endif
+
+private:
+define guiApp = ((GuiApplication)__thisModule);
+define selectionColor = guiApp.currentSkin.selectionColor; //Color { 10, 36, 106 };
+
 static char * fileIconNames[] = 
 {
    "<:ecere>mimeTypes/file.png",         /* none */
@@ -39,10 +50,7 @@ static char * fileIconNames[] =
    ""
 };
 
-//define guiApp = ((GuiApplication)__thisModule);
-//define selectionColor = guiApp.currentSkin.selectionColor; //Color { 10, 36, 106 };
-
-public enum __FileType
+public enum ExplorerFileType
 {
    none,
    
@@ -57,6 +65,7 @@ public enum __FileType
    treeLoader,
    lineNumbers;
 
+
    /*property char * 
    {
       set
@@ -65,17 +74,17 @@ public enum __FileType
       }
    }*/
 
-   public property bool isFolder
+   public property bool isFolderType
    {
       get { return this >= folder && this <= share; }
    }
 
-   public property bool isFile
+   public property bool isFileType
    {
       get { return this >= normalFile && this <= opticalMediaImageFile; }
    }
 
-   __FileType ::SelectByExtension(char * extension)
+   ExplorerFileType ::SelectByExtension(char * extension)
    {
       if(!strcmpi(extension, "ews"))
          return ewsFile;
@@ -129,482 +138,505 @@ public enum __FileType
    }
 };
 
-//public enum FileSystemNodeType { file, folder, system };
+class ExplorerControl : Window
+{
+   bool previewPictures;
+
+   BitmapResource fileIcons[ExplorerFileType];
 
-define guiApp = ((GuiApplication)__thisModule);  // how to do this in a dll?
-define selectionColor = guiApp.currentSkin.selectionColor; //Color { 10, 36, 106 };
+   ExplorerControl()
+   {
+      ExplorerFileType c;
+      for(c = 0; c < ExplorerFileType::enumSize; c++)
+      {
+         fileIcons[c] = BitmapResource { fileIconNames[c], alphaBlend = true };
+         AddResource(fileIcons[c]);
+      }
+   }
+}
 
-public class FileSystemNode : struct // : struct
+class ExplorerView : ExplorerControl
 {
-public:
-   /*//LinkElement<FileSystemNode> link;
-   FileSystemNode parent;
+   borderStyle = none;
+   hasHorzScroll = false;
+   hasVertScroll = false;
 
-   FileSystemNodeType type;
+   virtual void Load(ExplorerFileBranch parent);
+   virtual void Refresh();
 
-   char * name;*/
+   virtual void LaunchNotifyItemSelect(Window master, ExplorerView view, ExplorerFileItem item, ExplorerFileItemArray selectedItems)
+   {
+      view.NotifyItemSelect(master, view, item, selectedItems);
+   }
 
-   FileSystemNode prev, next;
+   virtual bool Window::NotifyItemSelect(ExplorerView view, ExplorerFileItem item, ExplorerFileItemArray selectedItems);
+   virtual bool Window::NotifyItemOpen(ExplorerView view, ExplorerFileItem item);
 
-   bool loaded, childrenLoaded;
-   int indent;
-   char * path;
-   char * name;
-   char * extension;
-   char * info;
-   DataRow row;
-   OldList children;
-   __FileType type;
-   FileSystemNode parent;
+   ListBox list
+   {
+      master = master, parent = this;
+      //this, master;
+      borderStyle = none;
+      hasHorzScroll = true;
+      hasVertScroll = true;
+      resizable = true;
+      sortable = true;
+      fullRowSelect = false;
+      multiSelect = true;
 
-   FileStats stats;
+      anchor = Anchor { left = 0, top = 0, right = 0, bottom = 0 };
 
-   Bitmap bitmap;
+      bool NotifySelect(ListBox listBox, DataRow row, Modifiers mods)
+      {
+         ExplorerView view = (ExplorerView)listBox.parent;
+         if(listBox.currentRow)
+         {
+            DataRow listRow;
+            ExplorerFileItemArray selectedItems { growingFactor = 16 };
+            for(listRow = listBox.firstRow; listRow; listRow = listRow.next)
+               if(listRow.selected)
+                  selectedItems.Add((ExplorerFileItem)listRow.tag);
+            //view.NotifyItemSelect(listBox.parent.master, view, (ExplorerFileItem)listBox.currentRow.tag);
+            view.LaunchNotifyItemSelect(listBox.parent.master, view, (ExplorerFileItem)listBox.currentRow.tag, selectedItems);
+         }
+         return true;
+      }
 
-   void GetPath(String outputPath)
-   {  
-      FileSystemNode up;
-      if(parent)
+      bool NotifyDoubleClick(ListBox listBox, int x, int y, Modifiers mods)
       {
-         strcpy(outputPath, name);
-         for(up = parent; up; up = up.parent)
+         ExplorerView view = (ExplorerView)listBox.parent;
+         view.NotifyItemOpen(listBox.parent.master, view, (ExplorerFileItem)listBox.currentRow.tag);
+         return false;
+      }
+
+      bool NotifyKeyDown(ListBox listBox, DataRow row, Key key, unichar ch)
+      {
+         if((SmartKey)key == enter)
          {
-            char temp[MAX_LOCATION];
-            strcpy(temp, up.name);
-            PathCat(temp, outputPath);
-            strcpy(outputPath, temp);
+            ExplorerView view = (ExplorerView)listBox.parent;
+            view.NotifyItemOpen(listBox.parent.master, view, (ExplorerFileItem)listBox.currentRow.tag);
          }
+         return true;
       }
-      else
-#ifdef __WIN32__
-         strcpy(outputPath, "/");
-#else
-         strcpy(outputPath, name);
-#endif
+   };
 
+   ExplorerView()
+   {
    }
+}
+
+class ExplorerViewList : ExplorerView
+{
 
-   bool IsChildOf(FileSystemNode node)
+   ExplorerFileBranch location;
+
+public:
+
+   DataField nameField { header = "Name", dataType = "ExplorerFileItem", width = 304, editable = true, userData = this };
+
+   ExplorerViewDetails()
    {
-      FileSystemNode test;
-      for(test = parent; test; test = test.parent)
-         if(test == node)
-            return true;
-      return false;
+      list.AddField(nameField);
    }
 
-   void DuplicateChildren(bool recursive, bool forceExpanded, FileSystemNode addTo, FileSystemBox fsb)
+   void Refresh()
    {
-      if(children.first)
+      Load(location);
+   }
+
+   void Load(ExplorerFileBranch location)
+   {
+      char path[MAX_LOCATION];
+      this.location = location;
+      location.GetPath(path);
       {
-         FileSystemNode child;
+         FileListing listing { path };
          
-         for(child = children.first; child; child = child.next)
+         ExplorerFileItem item;
+         DataRow row;
+
+         list.Clear();
+
+         while(listing.Find())
          {
-            FileSystemNode copy { };
-            copy.name = CopyString(child.name);
-            copy.type = child.type;
-            fsb.AddTreeNode(copy, child.loaded, false, addTo);
-            if(forceExpanded)
-               copy.row.collapsed = false;
-            if(recursive)
-               child.DuplicateChildren(recursive, forceExpanded, copy, fsb);
+            item = MakeFileItem(listing.stats.attribs, listing.name, listing.path, previewPictures, displaySystem);
+
+            row = list.AddRow();
+            row.tag = (int)item;
+            row.SetData(nameField, item);
          }
+         list.Sort(nameField, 1);
       }
    }
-   
-   void EnsureVisible(bool expand)
+}
+
+class ExplorerViewDetails : ExplorerView
+{
+   list.hasHeader = true;
+   list.moveFields = true;
+   list.resizable = true;
+   list.sortable = true;
+
+   ExplorerFileBranch location;
+
+public:
+
+   DataField nameField { header = "Name", dataType = "ExplorerFileItem", width = 304, editable = true, userData = this };
+   DataField typeField { header = "Type", dataType = /*"String"*/ "char *", width = 40 };
+   DataField sizeField { header = "Size", dataType = "FileSize", width = 96, alignment = right };
+
+   ExplorerViewDetails()
    {
-      if(parent)
-         parent.EnsureVisible(true);
-      if(expand)
-         row.collapsed = false;
-      // TODO: row.EnsureVisible(); // making the row visible by scrolling
+      list.AddField(nameField);
+      list.AddField(typeField);
+      list.AddField(sizeField);
    }
 
-   void OnFree()
+   void Refresh()
    {
-      //delete name;
+      Load(location);
    }
 
-   void Free()
+   void Load(ExplorerFileBranch location)
    {
-      FileSystemNode child;
-      for(; (child = children.first); )
+      char path[MAX_LOCATION];
+      this.location = location;
+      location.GetPath(path);
       {
-         child.Free();
-         children.Delete(child);
+         FileListing listing { path };
+         
+         ExplorerFileItem item;
+         DataRow row;
+
+         list.Clear();
+
+         while(listing.Find())
+         {
+            item = MakeFileItem(listing.stats.attribs, listing.name, listing.path, previewPictures, displaySystem);
+
+            row = list.AddRow();
+            row.tag = (int)item;
+            row.SetData(nameField, item);
+            row.SetData(typeField, CopyString(item.extension));
+            row.SetData(sizeField, (uint)listing.stats.size);
+         }
+         list.Sort(nameField, 1);
       }
-      //if(name)
-      delete name;
-      delete info;
    }
+}
 
-   void Delete()
-   {
-      Free();
-      if(parent)
-         parent.children.Delete(this);
-   }
+class ExplorerViewIcons : ExplorerView
+{
 
-   void OnDisplay(Surface surface, int x, int y, int width, FileSystemBox fsb, Alignment alignment, DataDisplayFlags displayFlags)
-   {
-      //int indentSize = (displayFlags.dropBox) ? 0 : 10;
-      int indent = 16;
-      int xStart;
-      int len;
-      int w, h;
-      //int textOffset;
-      char label[MAX_FILENAME];
+   ExplorerFileBranch location;
 
-      Bitmap icon;
+public:
 
-      if(!this)
-         return;
-      
-      icon = fsb.fileIcons[type].bitmap;
-      /*if(type == normalFile)
-         PrintLn("dd");*/
-      //xStart = indent * indent + x + (icon ? (icon.width + 5) : 0);
-      xStart = x + (icon ? (icon.width + 5) : 0);
+   DataField nameField { header = "Name", dataType = "ExplorerFileItem", width = 304, editable = true, userData = this };
 
-      if(!name)
-         return;
+   ExplorerViewDetails()
+   {
+      list.AddField(nameField);
+   }
 
-      if(info)
-         sprintf(label, "%s [%s]", name, info);
-      else
-         strcpy(label, name);
-      len = strlen(label);
-      
-      if(!icon)
-      {
-         if(type == folder || type == folderOpen)
-            surface.SetForeground(yellow);
-         //indentSize = 8;
-         indent = 8;
-      }
-      //textOffset = indent * indentSize + (icon ? (icon.width + 4) : 0);
-      
-      surface.TextOpacity(false);
-      surface.TextExtent(label, len, &w, &h);
-      h = Max(h, 16);
-    
-      // Draw the current row stipple
-      if(displayFlags.selected)
-         //surface.Area(xStart - 1, y, xStart - 1, y + h - 1);
-         //surface.Area(xStart + w - 1, y, xStart + w + 1, y + h - 1);
-         surface.Area(xStart - 3, y, xStart + w + 1, y + h - 1);
-      
-      //surface.WriteTextDots(alignment, x + textOffset, y + 2, width - textOffset, name, strlen(name));
-      surface.WriteTextDots(alignment, xStart, y + 2, width, label, len);
+   void Refresh()
+   {
+      Load(location);
+   }
 
-      //if(!guiApp.textMode) -- how to do this in a dll?
+   void Load(ExplorerFileBranch location)
+   {
+      char path[MAX_LOCATION];
+      this.location = location;
+      location.GetPath(path);
       {
-         if(displayFlags.current)
-         {
-            if(displayFlags.active)
-            {
-               surface.LineStipple(0x5555);
-               if(displayFlags.selected)
-                  surface.SetForeground(0xFFFFFF80);
-               else
-                  surface.SetForeground(black);
-            }
-            else
-            {
-               surface.SetForeground(selectionColor);
-            }
-            surface.Rectangle(xStart - 3, y, xStart + w + 1, y + h - 1);
-            surface.LineStipple(0);
-         }
+         FileListing listing { path };
+         
+         ExplorerFileItem item;
+         DataRow row;
 
-         if(icon)
+         list.Clear();
+
+         while(listing.Find())
          {
-            //surface.blend = true;
-            //surface.alphaWrite = blend;
-            surface.SetForeground(white);
-            //surface.Blit(icon, x + indent * indentSize, y,0,0, icon.width, icon.height);
-            surface.Blit(icon, x,y,0,0, icon.width, icon.height);
+            item = MakeFileItem(listing.stats.attribs, listing.name, listing.path, previewPictures, displaySystem);
+
+            row = list.AddRow();
+            row.tag = (int)item;
+            row.SetData(nameField, item);
          }
+         list.Sort(nameField, 1);
       }
    }
+}
 
-   int OnCompare(FileSystemNode b)
+class ExplorerViewCards : ExplorerView
+{
+
+   ExplorerFileBranch location;
+
+public:
+
+   DataField nameField { header = "Name", dataType = "ExplorerFileItem", width = 304, editable = true, userData = this };
+
+   ExplorerViewDetails()
    {
-      int result;
-      FileSystemNode a = this;
-      if(a.type == b.type || (a.type < folder && b.type < folder) || (a.type >= drive))
-         result = strcmpi(a.name, b.name);
-      else
-      {
-         if(a.type == folder && b.type < folder) result = -1;
-         else if(a.type < folder && b.type == folder) result = 1;
-         else result = 0;
-      }
-      return result;
+      list.AddField(nameField);
    }
 
-   /*int OnCompare(FileSystemNode b)
+   void Refresh()
    {
-      int result;
-      FileSystemNode a = this;
-      if(a.parent < b.parent) result = -1;
-      else if(a.parent > b.parent) result = 1;
-      else
-         result = fstrcmp(a.name, b.name);
-      return result;
-   }*/
+      Load(location);
+   }
 
-   char * OnGetString(char * tempString, FileSystemToolWindow fileSysToolWnd, bool * needClass)
+   void Load(ExplorerFileBranch location)
    {
-      return name ? name : "";
+      char path[MAX_LOCATION];
+      this.location = location;
+      location.GetPath(path);
+      {
+         FileListing listing { path };
+         
+         ExplorerFileItem item;
+         DataRow row;
+
+         list.Clear();
+
+         while(listing.Find())
+         {
+            item = MakeFileItem(listing.stats.attribs, listing.name, listing.path, previewPictures, displaySystem);
+
+            row = list.AddRow();
+            row.tag = (int)item;
+            row.SetData(nameField, item);
+         }
+         list.Sort(nameField, 1);
+      }
    }
 }
 
-/*FileSystemNode MakeFileSystemNode(const FileStats stats, const char * name)
+public class BitmapArray : RedjArray
 {
-   FileSystemNode node { stats = stats };
-   node.name = CopyString(name);
-   if(!node.name)
-      node.name = null;
-   if(stats.attribs.isDirectory)
+   type = class(Bitmap);
+public:
+   Bitmap * const _;
+   Bitmap * Add(Bitmap bitmap)
    {
-      node.type = (stats.attribs.isDrive) ? drive : folder;
-      if(stats.attribs.isServer) node.type = server;
-      if(stats.attribs.isShare) node.type = share;
-      if(stats.attribs.isCDROM) node.type = cdrom;
-      if(stats.attribs.isRemote) node.type = netDrive;
-      if(stats.attribs.isRemovable) 
-      {
-         if(name[0] == 'A' || name[0] == 'B')
-            node.type = floppy;
-         else
-            node.type = removable;
-      }
+      uint pos = _count;
+      Append(1);
+      _[pos] = bitmap;
+      return &_[pos];
    }
-   else
+   Bitmap * AddBefore(uint position, Bitmap bitmap)
    {
-      char extension[MAX_EXTENSION];
-      GetExtension(node.name, extension);
-      node.type = __FileType::SelectByExtension(extension);
+      Insert(position, 1);
+      _[position] = bitmap;
+      return &_[position];
    }
-   return node;
-}*/
-static FileSystemNode MakeFileSystemNode(const FileStats stats,
-      const char * fileName, const char * filePath,
-      const bool previewPicture, const DisplaySystem displaySystem)
-{
-   int len = strlen(fileName);
-   char info[MAX_LOCATION];
-   char name[MAX_LOCATION];
-   char extension[MAX_EXTENSION];
-   
-   FileSystemNode node { stats = stats };
+   void Clear()
+   {
+      int c;
+      for(c = 0; c < _count; c++)
+      {
+         _[c].Free();
+         delete _[c];
+      }  
+      count = 0;
+      size = 0;
+   }
+}
 
-   //if(stats.attribs.isFile) // TODO fix this in ecere
-   if(stats.attribs.isDirectory)
+class ExplorerViewShowcase : ExplorerView
+{
+   list.anchor = Anchor { left = 0, top = 0, bottom = 0 };
+   list.size = Size { w = 200 };
+
+   ExplorerFileBranch location;
+
+public:
+
+   DataField nameField { header = "Name", dataType = "ExplorerFileItem", width = 180, editable = true, userData = this };
+
+   Bitmap bitmap;
+   BitmapArray bitmaps { growingFactor = 16 };
+
+   Window show
    {
-      extension[0] = '\0';
+      this;
+      borderStyle = none;
+      anchor = Anchor { top = 0, right = 0, bottom = 0 };
 
-      node.type = (stats.attribs.isDrive) ? drive : folder;
-      if(stats.attribs.isServer) node.type = server;
-      if(stats.attribs.isShare) node.type = share;
-      if(stats.attribs.isCDROM) node.type = cdrom;
-      if(stats.attribs.isRemote) node.type = netDrive;
-      if(stats.attribs.isRemovable) 
+      void OnRedraw(Surface surface)
       {
-         if(fileName[0] == 'A' || fileName[0] == 'B')
-            node.type = floppy;
+         ExplorerViewShowcase view = (ExplorerViewShowcase)parent;
+         if(view.bitmap)
+         {
+            int wBmp = view.bitmap.width;
+            int hBmp = view.bitmap.height;
+            int wWnd = clientSize.w;
+            int hWnd = clientSize.h;
+
+            int wList = view.list.size.w + view.split.size.w;
+            
+            float scale = Min((float)(wWnd - 10) / wBmp, (float)(hWnd - 10) / hBmp);
+            
+            int wDraw = (int)(wBmp * scale);
+            int hDraw = (int)(hBmp * scale);
+
+      #ifndef __linux__
+            surface.Filter(view.bitmap, (wWnd - wDraw) / 2, (hWnd - hDraw) / 2, 0, 0, wDraw, hDraw, wBmp, hBmp);
+      #else
+            // Until Filter / Stretch works with X
+            surface.Blit(view.bitmap, (wWnd - wDraw) / 2, (hWnd - hDraw) / 2, 0, 0, wDraw, hDraw);
+      #endif
+         }
          else
-            node.type = removable;
+         {
+            surface.SetForeground(white);
+            surface.Area(0, 0, view.clientSize.w - 1, view.clientSize.h - 1);
+         }
       }
    }
-   else
+
+   SplitWindow split
    {
-      GetExtension(fileName, extension);
-      strlwr(extension);
-      
-      node.type = __FileType::SelectByExtension(extension);
-   }
+      this;
+      leftPane = list;
+      rightPane = show;
+      split = 200;
+      tabCycle = true;
+   };
 
-   if(stats.attribs.isDrive && 
-         len > 3 && !strncmp(&fileName[1], ": [", 3))
+   ExplorerViewDetails()
    {
-      strncpy(name, fileName, 2);
-      name[2] = 0;
-      strncpy(info, &fileName[4], len - 5);
-      info[len - 5] = 0;
+      list.AddField(nameField);
    }
-   else
+
+   void LaunchNotifyItemSelect(Window master, ExplorerViewShowcase view, ExplorerFileItem item, ExplorerFileItemArray selectedItems)
    {
-      strcpy(name, fileName);
-      info[0] = 0;
-   }
+      int pos;
+      ExplorerFileItem selItem;
+      if(view.bitmap)
+         view.bitmap.Free();
+      delete view.bitmap;
+      if(item && item.type == pictureFile)
+      {
+         view.bitmap = Bitmap { };
+         view.bitmap.Load(item.path, null, displaySystem);
+      }
 
-   node.path = CopyString(filePath);
-   node.name = CopyString(name);
-   if(info[0])
-      node.info = CopyString(info);
-   node.extension = CopyString(extension);
+      view.bitmaps.Clear();
+      view.bitmaps = BitmapArray { };
+      for(pos = 0; pos < selectedItems.count; pos++)
+      {
+         Bitmap bitmap { };
+         selItem = (ExplorerFileItem)selectedItems._[pos]; 
+         bitmap.Load(selItem.path, null, displaySystem);
+         //view.bitmaps.Add(bitmap);
+      }  
+      if(item && item.type == pictureFile)
+      {
+         view.bitmap = Bitmap { };
+         view.bitmap.Load(item.path, null, displaySystem);
+      }
 
-   if(node.type == pictureFile && previewPicture)
+      view.show.Update(null);
+      view.NotifyItemSelect(master, view, item, selectedItems);
+   }
+
+   void Refresh()
    {
-      node.bitmap = Bitmap { };
-      node.bitmap.Load(filePath, null, displaySystem);
+      Load(location);
    }
 
-   return node;
-}
+   void Load(ExplorerFileBranch location)
+   {
+      char path[MAX_LOCATION];
+      this.location = location;
+      location.GetPath(path);
+      {
+         FileListing listing { path };
+         
+         ExplorerFileItem item;
+         DataRow row;
 
-class FileSystemBoxBits
-{
-   bool foldersOnly:1, filesOnly:1, details:1, treeBranches:1, previewPictures:1;
-   //bool header:1, freeSelect:1, fullRowSelect:1, multiSelect:1, autoScroll:1, alwaysHL : 1, moveRows:1, resizable:1;
-   //bool moveFields:1, clearHeader:1, alwaysEdit:1, collapse:1, treeBranch:1, rootCollapse:1, heightSet:1;
-   //bool sortable:1, noDragging:1, fillLastField:1, expandOnAdd:1;
-};
+         list.Clear();
 
-public class FileSystemBox : Window // should we not derive from ListBox instead?
-/*
-   this stuff from the listbox would be nicely exposed...
-      fullRowSelect = false;
-      treeBranches = true;
-      collapseControl = true;
-      rootCollapseButton = true;
-      sortable = true;
-*/
+         while(listing.Find())
+         {
+            item = MakeFileItem(listing.stats.attribs, listing.name, listing.path, previewPictures, displaySystem);
+
+            row = list.AddRow();
+            row.tag = (int)item;
+            row.SetData(nameField, item);
+         }
+         list.Sort(nameField, 1);
+      }
+   }
+}
+
+class ExplorerTree : ExplorerControl
 {
-   borderStyle = deep;
    hasHorzScroll = false;
    hasVertScroll = false;
 
    menu = Menu { };
 
 public:
-   FileSystemNode root;
-   FileSystemNode selection;
 
-   virtual bool Window::NotifyNodeSelect(FileSystemBox box, FileSystemNode node);
-   
-   property char * path
-   {
-      set
-      {
-         delete path;
-         if(value && value[0])
-            path = CopyString(value);
-         if(created)
-            Load();
-      }
+   DataField nameField { dataType = "ExplorerFileBranch", width = 240, userData = this };
 
-      get { return path; }
-      //isset { return path && path[0]; }
-   }
+   ExplorerFileBranch root;
+   ExplorerFileBranch selection;
 
-   property bool foldersOnly { set { bits.foldersOnly = value; bits.filesOnly = !value; } get { return bits.foldersOnly; } };
-   property bool filesOnly { set { bits.filesOnly = value; bits.foldersOnly = !value; } get { return bits.filesOnly; } };
-   property bool previewPictures { set { bits.previewPictures = value; } get { return bits.previewPictures; } };
-   property char * extensions { set { delete extensions; if(value && value[0]) extensions = CopyString(value); } get { return extensions; } }
-   property bool details { set { bits.details = value; } get { return bits.details; } };
-   property bool treeBranches
-   {
-      set
-      {
-         bits.treeBranches = value;
-         list.treeBranches = value;
-         list.collapseControl = value;
-         list.rootCollapseButton = value;
-      }
-      get { return bits.treeBranches; }
-   };
+   virtual bool Window::NotifyBranchSelect(ExplorerTree tree, ExplorerFileBranch branch);
    
-   property FileSystemNode node
+   property ExplorerFileBranch branch
    {
       get
       {
-         if(!list)
+         if(!tree)
             return null;
-         if(!list.currentRow)
+         if(!tree.currentRow)
             return null;
-         if(!list.currentRow.tag)
+         if(!tree.currentRow.tag)
             return null;
-         return (FileSystemNode)list.currentRow.tag;
+         return (ExplorerFileBranch)tree.currentRow.tag;
       }
    }
 
-   void Select(FileSystemNode node)
+   void Select(ExplorerFileBranch branch)
    {
-      if(node.row)
+      if(branch.row)
       {
-         node.EnsureVisible(false);
-         list.SelectRow(node.row);
+         branch.EnsureVisible(false);
+         tree.SelectRow(branch.row);
       }
    }
 
-   FileSystemNode Find(const char * name, FileSystemNode parent)
+   ExplorerFileBranch Find(const char * name, ExplorerFileBranch parent)
    {
-      FileSystemNode node;
-      FileSystemNode start = parent ? parent : root;
+      ExplorerFileBranch branch;
+      ExplorerFileBranch start = parent ? parent : root;
       if(!start.loaded || !start.childrenLoaded)
-         LoadTreeNode(start);
-      for(node = start.children.first; node; node = node.next)
-         if(node.name && !strcmpi(node.name, name))
-            return node;
+         BranchLoad(start, tree);
+      for(branch = start.children.first; branch; branch = branch.next)
+         if(branch.name && !strcmpi(branch.name, name))
+            return branch;
       return null;
    }
 
-private:
-   FileSystemBoxBits bits;
-
-   char * path;
-   char * extensions;
-
-   BitmapResource fileIcons[__FileType];
-
-   FileSystemBox()
-   {
-      char wd[MAX_LOCATION];
-      GetWorkingDir(wd, sizeof(wd));
-      property::path = wd;
-      
-      InitFileIcons();
-      list.AddField(nameField);
-   }
-   ~FileSystemBox()
-   {
-      delete extensions;
-      delete path;
-   }
-   void InitFileIcons()
-   {
-      __FileType c;
-      for(c = 0; c < __FileType::enumSize; c++)
-      {
-         fileIcons[c] = BitmapResource { fileIconNames[c], alphaBlend = true };
-         AddResource(fileIcons[c]);
-      }
-   }
-
-   DataField nameField { dataType = "FileSystemNode", width = 240, userData = this };
-   DataField typeField { header = "Type", dataType = /*"String"*/ "char *", width = 40, freeData = false };
-   DataField sizeField { header = "Size", dataType = "FileSize", width = 96, alignment = right };
-
-   bool OnPostCreate()
-   {
-      Load();
-      return true;
-   }
-
-   ListBox list
+   ListBox tree
    {
-      this;
-
+      master = master, parent = this;
+      //this, master;
       borderStyle = none;
       hasHorzScroll = true;
       hasVertScroll = true;
       fullRowSelect = false;
-      sortable = true;
+      treeBranches = true;
+      collapseControl = true;
+      rootCollapseButton = true;
 
       anchor = Anchor { left = 0, top = 0, right = 0, bottom = 0 };
 
@@ -621,29 +653,25 @@ private:
       {
          if(row)
          {
-            FileSystemNode node = (FileSystemNode)row.tag;
-            FileSystemNode child;
+            ExplorerFileBranch branch = (ExplorerFileBranch)row.tag;
+            ExplorerFileBranch child;
             if(collapsed)
             {
                /*
-               for(child = node.children.last; child; child = node.children.last)
+               for(child = branch.children.last; child; child = branch.children.last)
                {
                   listBox.DeleteRow(child.row);
                   child.Free();
                   delete child;
                }
-               node.childrenLoaded = false;
+               branch.childrenLoaded = false;
                */
             }
             else
             {
-               if(!node.loaded || !node.childrenLoaded)
-               {
-                  LoadTreeNode(node);
-                  //list.Sort(nameField, 1);
-                  //node.
-               }
-               for(child = node.children.first; child && child.next; child = child.next);
+               if(!branch.loaded || !branch.childrenLoaded)
+                  BranchLoad(branch, tree);
+               for(child = branch.children.first; child && child.next; child = child.next);
                if(child)
                   child.EnsureVisible(false);
             }
@@ -656,8 +684,8 @@ private:
          DataRow row = listBox.currentRow;
          if(row)
          {
-            FileSystemNode node = (FileSystemNode)row.tag;
-            if(node)
+            ExplorerFileBranch branch = (ExplorerFileBranch)row.tag;
+            if(branch)
             {
                PopupMenu popup;
                Menu menu { };
@@ -685,9 +713,9 @@ private:
       {
          if(row)
          {
-            FileSystemNode node = (FileSystemNode)row.tag;
-            NotifyNodeSelect(listBox.parent.master, this, node);
-            selection = node;
+            ExplorerFileBranch branch = (ExplorerFileBranch)row.tag;
+            NotifyBranchSelect(listBox.parent.master, this, branch);
+            selection = branch;
          }
          return true;
       }
@@ -696,7 +724,7 @@ private:
       {
          if(row)
          {
-            FileSystemNode node = (FileSystemNode)row.tag;
+            ExplorerFileBranch branch = (ExplorerFileBranch)row.tag;
          }
          return true;
       }
@@ -705,7 +733,7 @@ private:
       {
          if(row)
          {
-            FileSystemNode node = (FileSystemNode)row.tag;
+            ExplorerFileBranch branch = (ExplorerFileBranch)row.tag;
          }
          return true;
       }
@@ -714,11 +742,10 @@ private:
       {
          if(row)
          {
-            FileSystemNode node = (FileSystemNode)row.tag;
+            ExplorerFileBranch branch = (ExplorerFileBranch)row.tag;
          }
          return true;
       }
-
    };
 
    // Edit Menu
@@ -771,75 +798,26 @@ private:
          nameField.width = width - 80;
    }*/
 
-   void Load()
+   ExplorerTree()
    {
-      // TODO: fix this!
-      // this is crashing in for designer when details = true // can't save the file, always yields a crash
-      /*if(list && created)
-      {
-         list.ClearFields();
-         list.AddField(nameField);
-         if(bits.details)
-         {
-            list.AddField(typeField);
-            list.AddField(sizeField);
-         }
-      }*/
-      if(bits.treeBranches)
-         LoadTree();
-      else
-         LoadList();
+      tree.AddField(nameField);
    }
 
-   void LoadList()
+   void Load()
    {
-      FileListing listing { path, extensions = extensions };
-
-      list.Clear();
-      while(listing.Find())
-      {
-         if((!bits.foldersOnly && !bits.filesOnly) ||
-            (bits.foldersOnly && listing.stats.attribs.isDirectory) ||
-            (bits.filesOnly && !listing.stats.attribs.isDirectory/*listing.stats.attribs.isFile*/)) // TOCHECK: isFile broken?
-         {
-            FileSystemNode node = MakeFileSystemNode(listing.stats, listing.name, listing.path, bits.previewPictures, displaySystem);
-            DataRow row = list.AddRow();
-            row.tag = (int)node;
-            row.SetData(nameField, node);
-            if(bits.details)
-            {
-               row.SetData(typeField, node.extension);
-               row.SetData(sizeField, (void *)node.stats.size);
-            }
-         }
-      }
-      list.Sort(nameField, 1);
-   }
+      ExplorerFileBranch parent;
+      ExplorerFileBranch branch;
+      FileListing listing { "/" };
 
-   void LoadTree()
-   {
-      //char startPath[MAX_LOCATION];
-      FileSystemNode parent;
-      FileSystemNode node;
-      FileListing listing { path, extensions = extensions };
-      
-      /*if(!path)
-         GetWorkingDir(startPath, sizeof(startPath));
-      else
-         strcpy(path, startPath);*/
-      
-      list.Clear();
+      tree.Clear();
 
-      delete root;
-      //root = FileSystemNode { type = computer, loaded = true, childrenLoaded = true };
-      root = MakeFileSystemNode(FileStats { attribs = FileExists(path)}, path, path, bits.previewPictures, displaySystem);
+      root = ExplorerFileBranch { type = computer, loaded = true, childrenLoaded = true };
    #ifdef __WIN32__
-      //root.name = rootName;
-      AddTreeNode(root, true, false, null, list);
+      root.name = rootName;
    #else
-      //root.name = "/";
+      root.name = "/";
    #endif
-      AddTreeNode(root, false, true, null);
+      AddBranch(root, true, false, null, tree);
 
    // How can this make sense for linux? 
    #ifdef __WIN32__
@@ -862,126 +840,23 @@ private:
             info[0] = 0;
          }
 
-         parent = MakeFileSystemNode(listing.stats, name);
+         parent = MakeFileBranch(listing.stats, name);
          if(info[0])
             parent.info = CopyString(info);
          parent.loaded = true;
-         AddTreeNode(parent, !listing.stats.attribs.isDirectory, listing.stats.attribs.isDirectory, root, this);
+         AddBranch(parent, !listing.stats.attribs.isDirectory, listing.stats.attribs.isDirectory, root, tree);
          if(!listing.stats.attribs.isDirectory)
             parent.childrenLoaded = true;
       }
 
-      node = FileSystemNode { name = msNetwork, type = network };
-      AddTreeNode(node, false, true, null, this);
-      node.row.collapsed = true;
-      Sort(nameField, 1);
-      SelectRow(root.row);
+      branch = ExplorerFileBranch { name = msNetwork, type = network };
+      AddBranch(branch, false, true, null, tree);
+      branch.row.collapsed = true;
+      tree.Sort(nameField, 1);
+      tree.SelectRow(root.row);
    #endif
-      LoadTreeNode(root);
-      list.Sort(nameField, 1);
-   }
-
-   void LoadTreeNode(FileSystemNode node)
-   {
-      if(!node.loaded)
-      {
-         char path[MAX_LOCATION];
-         node.GetPath(path);
-         {
-            FileListing listing { path, extensions = extensions };
-            if(node.children.count == 1)
-            DeleteNode(node.children.first);
-
-            while(listing.Find())
-            {
-               if((!bits.foldersOnly && !bits.filesOnly) ||
-                  (bits.foldersOnly && listing.stats.attribs.isDirectory) ||
-                  (bits.filesOnly && !listing.stats.attribs.isDirectory/*listing.stats.attribs.isFile*/)) // TOCHECK: isFile broken?
-               {
-                  FileSystemNode child = MakeFileSystemNode(listing.stats, listing.name, listing.path, bits.previewPictures, displaySystem);
-                  AddTreeNode(child, true, false, node);
-                  NodeChildLoad(child, node);
-               }
-            }
-         }
-         node.childrenLoaded = true;
-         node.loaded = true;
-         node.row.SortSubRows(false);
-      }
-      else if(!node.childrenLoaded)
-      {
-         FileSystemNode child;
-         if(node.children.first)
-         {
-            for(child = node.children.first; child; child = child.next)
-            {
-               if(!child.loaded)
-                  LoadTreeNode(child);
-               else if(!child.childrenLoaded)
-                  NodeChildLoad(child, node);
-            }
-            node.childrenLoaded = true;
-            node.row.SortSubRows(false);
-         }
-      }
    }
 
-   void NodeChildLoad(FileSystemNode parent, FileSystemNode node)
-   {
-      char path[MAX_LOCATION];
-      parent.GetPath(path);
-      {
-         bool added = false;
-         FileListing listing { path, extensions = extensions };
-         while(listing.Find())
-         {
-            if((!bits.foldersOnly && !bits.filesOnly) ||
-               (bits.foldersOnly && listing.stats.attribs.isDirectory) ||
-               (bits.filesOnly && !listing.stats.attribs.isDirectory/*listing.stats.attribs.isFile*/)) // TOCHECK: isFile broken?
-            {
-               FileSystemNode child = MakeFileSystemNode(listing.stats, listing.name, listing.path, bits.previewPictures, displaySystem);
-               AddTreeNode(child, true, false, parent);
-               added = true;
-            }
-         }
-         if(!added)
-            added = true;
-      }
-      //parent.childrenLoaded = true;
-   }
-
-   void AddTreeNode(FileSystemNode node, bool loaded, bool addLoader, FileSystemNode addTo)
-   {
-      DataRow row = (addTo && addTo.row) ? addTo.row.AddRow() : list.AddRow();
-      if(addTo)
-      {
-         node.parent = addTo;
-         node.indent = addTo.indent + 1;
-         addTo.children.Add(node);
-      }
-      row.tag = (int)node;
-      node.row = row;
-      row.SetData(null, node);
-
-      node.loaded = loaded;
-      if(addLoader)
-         //AddTreeNode(FileSystemNode { }, false, false, node); // why would this create a compile error?
-         AddTreeNode(FileSystemNode { type = none }, false, false, node);
-
-      if(node.indent > 0)
-         row.collapsed = true;
-      else if(node.type == folder)
-         node.type = folderOpen;
-   }
-
-   void DeleteNode(FileSystemNode node)
-   {
-      FileSystemNode child;
-      for(; (child = node.children.first); )
-         DeleteNode(child);
-      list.DeleteRow(node.row);
-      node.Delete();
-   }
 /*
 public class ClipBoardFiles
 {
@@ -1066,3 +941,582 @@ End If
 */
 
 }
+
+/*public class FileTreeBranchBSArray : ArrayBinarySorted
+{
+   type = class(ExplorerFileBranch);
+public:
+   ExplorerFileBranch * const _;
+   BSloc Add(ExplorerFileBranch item)
+   {
+      BSloc result = Find(item);
+      if(!result.valid)
+      {
+         Insert(result.pos, 1);
+         _[result.pos] = item;
+      }
+      return result;
+   }
+   BSloc Remove(ExplorerFileBranch item)
+   {
+      
+   }
+}*/
+
+/*public class FileTreeBranchArray : RedjArray
+{
+   type = class(ExplorerFileBranch);
+public:
+   ExplorerFileBranch * const _;
+   ExplorerFileBranch * Add(ExplorerFileBranch item)
+   {
+      uint pos = _count;
+      Append(1);
+      _[pos] = item;
+      return &_[pos];
+   }
+   ExplorerFileBranch * AddBefore(uint position, ExplorerFileBranch item)
+   {
+      Insert(position, 1);
+      _[position] = item;
+      return &_[position];
+   }
+}*/
+
+public class ExplorerFileItem : struct
+{
+   char * path;
+   char * name;
+   char * info;
+   char * extension;
+   ExplorerFileType type;
+   int indent;
+
+   Bitmap bitmap;
+
+   void OnDisplay(Surface surface, int x, int y, int width, ExplorerControl control, Alignment alignment, DataDisplayFlags displayFlags)
+   {
+      int indentSize = (displayFlags.dropBox) ? 0 : 10;
+      int textOffset;
+      int len;
+      char label[MAX_FILENAME];
+
+      //float scale = Min((float)clientSize.w / (float)bitmap.width, (float)clientSize.h / (float)bitmap.height);
+      int w = 16; //(int)(bitmap.width * scale);
+      int h = 16; //(int)(bitmap.height * scale);
+   
+      Bitmap icon;
+
+      icon = control.fileIcons[type].bitmap;
+      if(!icon)
+      {
+         if(type == folder || type == folderOpen)
+            surface.SetForeground(red); //Color { 170, 170, 0 } // REDJ What is that color?
+         indentSize = 8;
+      }
+      textOffset = indent * indentSize + (icon ? (icon.width + 6) : 0);
+      
+      if(info)
+         sprintf(label, "%s [%s]", name, info);
+      else
+         strcpy(label, name);
+      len = strlen(label);
+
+      surface.WriteTextDots
+         (alignment, x + textOffset, y + 2, width - textOffset, label, len);
+      if(type == pictureFile && control.previewPictures && bitmap)
+      {
+#ifndef __linux__
+         //surface.Filter(bitmap, (clientSize.w - w) / 2,(clientSize.h - h) / 2, 0,0, w, h, bitmap.width, bitmap.height);
+         surface.Filter(bitmap, x + indent * indentSize + 2, y, 0, 0, w, h, bitmap.width, bitmap.height);
+#else
+         // Until Filter / Stretch works with X
+         //surface.Blit(bitmap, (clientSize.w - bitmap.width) / 2,(clientSize.h - bitmap.height) / 2, 0,0, bitmap.width, bitmap.height);
+         surface.blend = true;
+         surface.Blit(bitmap, x + indent * indentSize + 2, y,0,0, w, h);
+#endif
+         //bitmap.Free();
+         //delete bitmap;
+      }
+      else if(icon)
+         surface.Blit(icon, x + indent * indentSize + 2, y,0,0, icon.width, icon.height);
+   }
+
+   int OnCompare(ExplorerFileItem b)
+   {
+      int result;
+      if(type == b.type || (type < folder && b.type < folder) || (type >= drive))
+         result = strcmpi(name, b.name);
+      else
+      {
+         if(type == folder && b.type < folder) result = -1;
+         else if(type < folder && b.type == folder) result = 1;
+      }
+      return result;
+   }
+
+   void OnCopy(ExplorerFileItem newData)
+   {
+      type = newData.type;
+      indent = newData.indent;
+      if(newData.name)
+      {
+         int len = strlen(newData.name) + 1;
+         name = new char[len];
+         CopyBytes(name, newData.name, len);
+      }
+   }
+
+   bool OnGetDataFromString(char * string)
+   {
+      int len = strlen(string) + 1;
+      name = new char[len];
+      CopyBytes(name, string, len);
+      return true;
+   }
+
+   void OnFree()
+   {
+      delete path;
+      delete name;
+      delete info;
+      delete extension;
+      if(bitmap)
+         bitmap.Free();
+   }
+
+   char * OnGetString(char * string, void * fieldData, bool * needClass)
+   {
+      return name;
+   }
+};
+
+public class ExplorerFileItemArray : RedjArray
+{
+   type = class(ExplorerFileItem);
+public:
+   ExplorerFileItem * const _;
+   ExplorerFileItem * Add(ExplorerFileItem item)
+   {
+      uint pos = _count;
+      Append(1);
+      _[pos] = item;
+      return &_[pos];
+   }
+   ExplorerFileItem * AddBefore(uint position, ExplorerFileItem item)
+   {
+      Insert(position, 1);
+      _[position] = item;
+      return &_[position];
+   }
+   void Clear()
+   {
+      int c;
+      for(c = 0; c < _count; c++)
+      {
+         //_[c].Free()
+         delete _[c];
+      }  
+      count = 0;
+      size = 0;
+   }
+}
+
+ExplorerFileItem MakeFileItem(const FileAttribs attribs, const char * fileName, const char * filePath, const bool previewPicture, const DisplaySystem displaySystem)
+{
+   int len = strlen(fileName);
+   char info[MAX_LOCATION];
+   char name[MAX_LOCATION];
+   char extension[MAX_EXTENSION];
+   
+   ExplorerFileItem item { };
+
+   //if(attribs.isFile) // TODO fix this in ecere
+   if(attribs.isDirectory)
+   {
+      extension[0] = 0;
+
+      item.type = (attribs.isDrive) ? drive : folder;
+      if(attribs.isServer)
+         item.type = server;
+      if(attribs.isShare)
+         item.type = share;
+      if(attribs.isCDROM)
+         item.type = cdrom;
+      if(attribs.isRemote)
+         item.type = netDrive;
+      if(attribs.isRemovable) 
+      {
+         if(fileName[0] == 'A' || fileName[0] == 'B')
+            item.type = floppy;
+         else
+            item.type = removable;
+      }
+   }
+   else
+   {
+      GetExtension(fileName, extension);
+      //strupr(extension);
+      strlwr(extension);
+      
+      item.type = ExplorerFileType::SelectByExtension(extension);
+   }
+
+   if(attribs.isDrive && 
+         len > 3 && !strncmp(&fileName[1], ": [", 3))
+   {
+      strncpy(name, fileName, 2);
+      name[2] = 0;
+      strncpy(info, &fileName[4], len - 5);
+      info[len - 5] = 0;
+   }
+   else
+   {
+      strcpy(name, fileName);
+      info[0] = 0;
+   }
+
+   item.path = CopyString(filePath);
+   item.name = CopyString(name);
+   if(info[0])
+      item.info = CopyString(info);
+   item.extension = CopyString(extension);
+
+   if(item.type == pictureFile && previewPicture)
+   {
+      item.bitmap = Bitmap { };
+      item.bitmap.Load(filePath, null, displaySystem);
+   }
+
+   return item;
+}
+
+public class ExplorerFileBranch : struct
+{
+   ExplorerFileBranch prev, next;
+
+   bool loaded, childrenLoaded;
+   int indent;
+   char * name;
+   char * info;
+   DataRow row;
+   OldList children;
+   ExplorerFileType type;
+   ExplorerFileBranch parent;
+
+   FileStats stats;
+
+   void GetPath(String outputPath)
+   {  
+      ExplorerFileBranch up;
+      if(parent)
+      {
+         strcpy(outputPath, name);
+         for(up = parent; up; up = up.parent)
+         {
+            char temp[MAX_LOCATION];
+            strcpy(temp, up.name);
+            PathCat(temp, outputPath);
+            strcpy(outputPath, temp);
+         }
+      }
+      else
+#ifdef __WIN32__
+         strcpy(outputPath, "/");
+#else
+         strcpy(outputPath, name);
+#endif
+
+   }
+
+   bool IsChildOf(ExplorerFileBranch branch)
+   {
+      ExplorerFileBranch test;
+      for(test = parent; test; test = test.parent)
+         if(test == branch)
+            return true;
+      return false;
+   }
+
+   void DuplicateChildren(bool recursive, bool forceExpanded, ExplorerFileBranch addTo, ListBox tree)
+   {
+      if(children.first)
+      {
+         ExplorerFileBranch child;
+         
+         for(child = children.first; child; child = child.next)
+         {
+            ExplorerFileBranch copy { };
+            copy.name = CopyString(child.name);
+            copy.type = child.type;
+            AddBranch(copy, child.loaded, false, addTo, tree);
+            if(forceExpanded)
+               copy.row.collapsed = false;
+            if(recursive)
+               child.DuplicateChildren(recursive, forceExpanded, copy, tree);
+         }
+      }
+   }
+   
+   void EnsureVisible(bool expand)
+   {
+      if(parent)
+         parent.EnsureVisible(true);
+      if(expand)
+         row.collapsed = false;
+      // TODO: row.EnsureVisible(); // making the row visible by scrolling
+   }
+
+   void OnFree()
+   {
+      //delete name;
+   }
+
+   void Free()
+   {
+      ExplorerFileBranch child;
+      for(; (child = children.first); )
+      {
+         child.Free();
+         children.Delete(child);
+      }
+      //if(name)
+      delete name;
+      delete info;
+   }
+
+   void Delete()
+   {
+      Free();
+      if(parent)
+         parent.children.Delete(this);
+   }
+
+   void OnDisplay(Surface surface, int x, int y, int width, ExplorerControl control, Alignment alignment, DataDisplayFlags displayFlags)
+   {
+      //int indentSize = (displayFlags.dropBox) ? 0 : 10;
+      int indent = 16;
+      int xStart;
+      int len;
+      int w, h;
+      //int textOffset;
+      char label[MAX_FILENAME];
+
+      Bitmap icon;
+
+      if(!this)
+         return;
+      
+      icon = control.fileIcons[type].bitmap;
+      //xStart = indent * indent + x + (icon ? (icon.width + 5) : 0);
+      xStart = x + (icon ? (icon.width + 5) : 0);
+
+      if(!name)
+         return;
+
+      if(info)
+         sprintf(label, "%s [%s]", name, info);
+      else
+         strcpy(label, name);
+      len = strlen(label);
+      
+      if(!icon)
+      {
+         if(type == folder || type == folderOpen)
+            surface.SetForeground(yellow);
+         //indentSize = 8;
+         indent = 8;
+      }
+      //textOffset = indent * indentSize + (icon ? (icon.width + 4) : 0);
+      
+      surface.TextOpacity(false);
+      surface.TextExtent(label, len, &w, &h);
+      h = Max(h, 16);
+    
+      // Draw the current row stipple
+      if(displayFlags.selected)
+         //surface.Area(xStart - 1, y, xStart - 1, y + h - 1);
+         //surface.Area(xStart + w - 1, y, xStart + w + 1, y + h - 1);
+         surface.Area(xStart - 3, y, xStart + w + 1, y + h - 1);
+      
+      //surface.WriteTextDots(alignment, x + textOffset, y + 2, width - textOffset, name, strlen(name));
+      surface.WriteTextDots(alignment, xStart, y + 2, width, label, len);
+
+      if(!guiApp.textMode)
+      {
+         if(displayFlags.current)
+         {
+            if(displayFlags.active)
+            {
+               surface.LineStipple(0x5555);
+               if(displayFlags.selected)
+                  surface.SetForeground(0xFFFFFF80);
+               else
+                  surface.SetForeground(black);
+            }
+            else
+            {
+               surface.SetForeground(selectionColor);
+            }
+            surface.Rectangle(xStart - 3, y, xStart + w + 1, y + h - 1);
+            surface.LineStipple(0);
+         }
+
+         if(icon)
+         {
+            //surface.blend = true;
+            //surface.alphaWrite = blend;
+            surface.SetForeground(white);
+            //surface.Blit(icon, x + indent * indentSize, y,0,0, icon.width, icon.height);
+            surface.Blit(icon, x,y,0,0, icon.width, icon.height);
+         }
+      }
+   }
+
+   int OnCompare(ExplorerFileBranch b)
+   {
+      int result;
+      if(type == b.type || (type < folder && b.type < folder) || (type >= drive))
+         result = strcmpi(name, b.name);
+      else
+      {
+         if(type == folder && b.type < folder) result = -1;
+         else if(type < folder && b.type == folder) result = 1;
+      }
+      return result;
+   }
+
+   char * OnGetString(char * tempString, FileSystemToolWindow fileSysToolWnd, bool * needClass)
+   {
+      return name ? name : "";
+   }
+};
+
+ExplorerFileBranch MakeFileBranch(const FileStats stats, const char * name)
+{
+   ExplorerFileBranch fileTreeBranch { stats = stats };
+   fileTreeBranch.name = CopyString(name);
+   if(!fileTreeBranch.name)
+      fileTreeBranch.name = null;
+   if(stats.attribs.isDirectory)
+   {
+      fileTreeBranch.type = (stats.attribs.isDrive) ? drive : folder;
+      if(stats.attribs.isServer) fileTreeBranch.type = server;
+      if(stats.attribs.isShare) fileTreeBranch.type = share;
+      if(stats.attribs.isCDROM) fileTreeBranch.type = cdrom;
+      if(stats.attribs.isRemote) fileTreeBranch.type = netDrive;
+      if(stats.attribs.isRemovable) 
+      {
+         if(name[0] == 'A' || name[0] == 'B')
+            fileTreeBranch.type = floppy;
+         else
+            fileTreeBranch.type = removable;
+      }
+   }
+   else
+   {
+      char extension[MAX_EXTENSION];
+      GetExtension(fileTreeBranch.name, extension);
+      fileTreeBranch.type = ExplorerFileType::SelectByExtension(extension);
+   }
+   return fileTreeBranch;
+}
+
+void AddBranch(ExplorerFileBranch branch, bool loaded, bool addLoader, ExplorerFileBranch addTo, ListBox tree)
+{
+   DataRow row = (addTo && addTo.row) ? addTo.row.AddRow() : tree.AddRow();
+   if(addTo)
+   {
+      branch.parent = addTo;
+      branch.indent = addTo.indent + 1;
+      addTo.children.Add(branch);
+   }
+   row.tag = (int)branch;
+   branch.row = row;
+   row.SetData(null, branch);
+
+   branch.loaded = loaded;
+   if(addLoader)
+      //AddBranch(ExplorerFileBranch { }, false, false, branch, tree); // why would this create a compile error?
+      AddBranch(ExplorerFileBranch { type = none }, false, false, branch, tree);
+
+   if(branch.indent > 0)
+      row.collapsed = true;
+   else if(branch.type == folder)
+      branch.type = folderOpen;
+}
+
+void BranchLoad(ExplorerFileBranch branch, ListBox tree)
+{
+   if(!branch.loaded)
+   {
+      char path[MAX_LOCATION];
+      branch.GetPath(path);
+      {
+         FileListing listing { path };
+         if(branch.children.count == 1)
+         DeleteBranch(branch.children.first, tree);
+
+         while(listing.Find())
+         {
+            if(listing.stats.attribs.isDirectory)
+            {
+               ExplorerFileBranch child = MakeFileBranch(listing.stats, listing.name);
+               AddBranch(child, true, false, branch, tree);
+               BranchChildLoad(child, branch, tree);
+            }
+         }
+      }
+      branch.childrenLoaded = true;
+      branch.loaded = true;
+      branch.row.SortSubRows(false);
+   }
+   else if(!branch.childrenLoaded)
+   {
+      ExplorerFileBranch child;
+      if(branch.children.first)
+      {
+         for(child = branch.children.first; child; child = child.next)
+         {
+            if(!child.loaded)
+               BranchLoad(child, tree);
+            else if(!child.childrenLoaded)
+               BranchChildLoad(child, branch, tree);
+         }
+         branch.childrenLoaded = true;
+         branch.row.SortSubRows(false);
+      }
+   }
+}
+
+static void BranchChildLoad(ExplorerFileBranch parent, ExplorerFileBranch branch, ListBox tree)
+{
+   char path[MAX_LOCATION];
+   parent.GetPath(path);
+   {
+      bool added = false;
+      FileListing listing { path };
+      while(listing.Find())
+      {
+         if(listing.stats.attribs.isDirectory)
+         {
+            ExplorerFileBranch child = MakeFileBranch(listing.stats, listing.name);
+            AddBranch(child, true, false, parent, tree);
+            added = true;
+         }
+      }
+      if(!added)
+         added = true;
+   }
+   //parent.childrenLoaded = true;
+}
+
+void DeleteBranch(ExplorerFileBranch branch, ListBox tree)
+{
+   ExplorerFileBranch child;
+   for(; (child = branch.children.first); )
+      DeleteBranch(child, tree);
+   tree.DeleteRow(branch.row);
+   branch.Delete();
+}
+