libede:FileSystemSearch: migration to FileSystemBox mostly done
[ede] / libede / src / FileSystemBox.ec
index 4d20d0e..8557aac 100644 (file)
@@ -8,7 +8,7 @@ static char * rootName = "File System";
 #endif
 
 private:
-define guiApp = ((GuiApplication)__thisModule);  // how to do this in a dll?
+define guiApp = (GuiApplication)((__thisModule).application);
 define selectionColor = guiApp.currentSkin.selectionColor; //Color { 10, 36, 106 };
 
 static char * fileIconNames[] = 
@@ -139,7 +139,7 @@ public enum _FileType
 
 class FileSystemBoxBits
 {
-   bool foldersOnly:1, filesOnly:1, details:1, treeBranches:1, previewPictures:1, navigateFolders:1;
+   bool foldersOnly:1, filesOnly:1, details:1, treeBranches:1, previewPictures:1, navigateFolders:1, autoLoad: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;
@@ -201,7 +201,9 @@ public:
       }
       get { return bits.treeBranches; }
    };
-   property bool navigateFolders { set { bits.navigateFolders = value; bits.foldersOnly = !value; } get { return bits.navigateFolders; } };
+   property bool navigateFolders { set { bits.navigateFolders = value; bits.filesOnly = !value; } get { return bits.navigateFolders; } };
+   property bool multiSelect { set { list.multiSelect = value; } get { return list.multiSelect; } };
+   property bool autoLoad { set { bits.autoLoad = value; } get { return bits.autoLoad; } };
    
    property FileSystemNode node
    {
@@ -228,16 +230,75 @@ public:
       }
    }
 
+   FileSystemNode SelectLocation(char * location)
+   {
+      int c;
+      char * temp;
+      char step[MAX_LOCATION];
+
+      //StringArray steps { growingFactor = 4 };
+      Array<String> steps { };
+      FileSystemNode result = null;
+      FileSystemNode node = null;
+
+      temp = CopyString(location);
+      while(temp[0])
+      {
+         GetLastDirectory(temp, step);
+         StripLastDirectory(temp, temp);
+         steps.Add(CopyString(step));
+      }
+
+      for(c = steps.count - 1; c >= 0; c--)
+      {
+         char * t = steps[c];
+         node = Find(steps[c], node);
+         if(!node)
+            break;
+         //Select(node);
+      }
+      if(node)
+      {
+         result = node;
+         Select(result);
+      }
+
+      steps.Free();
+      delete temp;
+      delete steps;
+
+      return result;
+   }
+
    FileSystemNode Find(const char * name, FileSystemNode parent)
    {
-      FileSystemNode node;
-      FileSystemNode 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;
-      return null;
+      FileSystemNode node = null;
+      FileSystemNode result = null;
+      if(!parent/* && !strcmp(name, "/")*/)
+      {
+         DataRow row;
+         for(row = list.firstRow; row; row = row.next)
+         {
+            node = (FileSystemNode)row.tag;
+            if(node.name && !fstrcmp(node.name, name))
+               break;
+         }
+         if(node)
+            result = node;
+         //result = root;
+      }
+      else
+      {
+         FileSystemNode start = parent ? parent : root;
+         if(!start.loaded || !start.childrenLoaded)
+            LoadTreeNode(start);
+         for(node = start.children.first; node; node = node.next)
+            if(node.name && !fstrcmp(node.name, name))
+               break;
+         if(node)
+            result = node;
+      }
+      return result;
    }
 
    void Refresh()
@@ -261,6 +322,7 @@ private:
       
       InitFileIcons();
       list.AddField(nameField);
+      bits.autoLoad = true;
    }
    ~FileSystemBox()
    {
@@ -283,7 +345,8 @@ private:
 
    bool OnPostCreate()
    {
-      Load();
+      if(bits.autoLoad)
+         Load();
       return true;
    }
 
@@ -412,7 +475,9 @@ private:
 
       bool NotifyDoubleClick(ListBox listBox, int x, int y, Modifiers mods)
       {
-         return OpenNode();
+         bool result = !(selection && selection.type.isFolder && bits.navigateFolders);
+         OpenNode();
+         return result;
       }
 
       bool NotifyKeyDown(ListBox listBox, DataRow row, Key key, unichar ch)
@@ -430,12 +495,8 @@ private:
    {
       bool result;
       if(selection && selection.type.isFolder && bits.navigateFolders)
-      {
          property::path = selection.path;
-         result = true;
-      }
-      else
-         result = NotifyNodeOpen(this.master, this, selection);
+      result = NotifyNodeOpen(this.master, this, selection);
       return result;
    }
 
@@ -518,17 +579,10 @@ private:
       {
          if((!bits.foldersOnly && !bits.filesOnly) ||
             (bits.foldersOnly && listing.stats.attribs.isDirectory) ||
-            (bits.filesOnly && !listing.stats.attribs.isDirectory/*listing.stats.attribs.isFile*/)) // TOCHECK: isFile broken?
+            (bits.filesOnly && listing.stats.attribs.isFile))
          {
             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);
-            }
+            AddNode(node);
          }
       }
       list.Sort(nameField, 1);
@@ -598,7 +652,7 @@ private:
       if(isRoot)
       {
          root.type = computer;
-         root.name = rootName;
+         root.label = rootName;
       }
 
       list.Sort(nameField, 1);
@@ -614,13 +668,13 @@ private:
          {
             FileListing listing { path, extensions = extensions };
             if(node.children.count == 1)
-            DeleteNode(node.children.first);
+               DeleteNode(node.children.first);
 
             while(listing.Find())
             {
-               if((!bits.foldersOnly && !bits.filesOnly) ||
+               if(!listing.stats.attribs.isRemovable && ((!bits.foldersOnly && !bits.filesOnly) ||
                   (bits.foldersOnly && listing.stats.attribs.isDirectory) ||
-                  (bits.filesOnly && !listing.stats.attribs.isDirectory/*listing.stats.attribs.isFile*/)) // TOCHECK: isFile broken?
+                  (bits.filesOnly && listing.stats.attribs.isFile)))
                {
                   FileSystemNode child = MakeFileSystemNode(listing.stats, listing.name, listing.path, bits.previewPictures, displaySystem);
                   AddTreeNode(child, true, false, node);
@@ -661,17 +715,29 @@ private:
          {
             if((!bits.foldersOnly && !bits.filesOnly) ||
                (bits.foldersOnly && listing.stats.attribs.isDirectory) ||
-               (bits.filesOnly && !listing.stats.attribs.isDirectory/*listing.stats.attribs.isFile*/)) // TOCHECK: isFile broken?
+               (bits.filesOnly && listing.stats.attribs.isFile))
             {
                FileSystemNode child = MakeFileSystemNode(listing.stats, listing.name, listing.path, bits.previewPictures, displaySystem);
-               AddTreeNode(child, true, false, parent);
+               AddTreeNode(child, listing.stats.attribs.isFile, !listing.stats.attribs.isFile, parent);
                added = true;
             }
          }
          if(!added)
             added = true;
       }
-      //parent.childrenLoaded = true;
+      parent.childrenLoaded = true;
+   }
+
+   void AddNode(FileSystemNode node)
+   {
+      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);
+      }
    }
 
    void AddTreeNode(FileSystemNode node, bool loaded, bool addLoader, FileSystemNode addTo)
@@ -686,6 +752,11 @@ private:
       row.tag = (int)node;
       node.row = row;
       row.SetData(null, node);
+      if(bits.details)
+      {
+         row.SetData(typeField, node.extension);
+         row.SetData(sizeField, (void *)node.stats.size);
+      }
 
       node.loaded = loaded;
       if(addLoader)
@@ -1704,7 +1775,7 @@ ExplorerFileItem MakeFileItem(const FileAttribs attribs, const char * fileName,
    
    ExplorerFileItem item { };
 
-   //if(attribs.isFile) // TODO fix this in ecere
+   //if(stats.attribs.isFile) // -- should work now
    if(attribs.isDirectory)
    {
       extension[0] = 0;
@@ -1782,6 +1853,7 @@ public:
    int indent;
    char * path;
    char * name;
+   char * label;
    char * extension;
    char * info;
    DataRow row;
@@ -1798,7 +1870,8 @@ public:
       FileSystemNode up;
       if(parent)
       {
-         strcpy(outputPath, name);
+         if(name)
+            strcpy(outputPath, name);
          for(up = parent; up; up = up.parent)
          {
             char temp[MAX_LOCATION];
@@ -1808,12 +1881,15 @@ public:
          }
       }
       else
+      {
 /*#ifdef __WIN32__
          strcpy(outputPath, "/");
 #else*/
-         strcpy(outputPath, name);
+         //strcpy(outputPath, name);
+         strcpy(outputPath, path);
+         PathCat(outputPath, name);
 //#endif
-
+      }
    }
 
    bool IsChildOf(FileSystemNode node)
@@ -1869,6 +1945,7 @@ public:
       }
       //if(name)
       delete name;
+      delete label;
       delete info;
    }
 
@@ -1887,7 +1964,7 @@ public:
       int len;
       int w, h;
       //int textOffset;
-      char label[MAX_FILENAME];
+      char string[MAX_FILENAME];
 
       Bitmap icon;
 
@@ -1902,10 +1979,10 @@ public:
          return;
 
       if(info)
-         sprintf(label, "%s [%s]", name, info);
+         sprintf(string, "%s [%s]", label ? label : name, info);
       else
-         strcpy(label, name);
-      len = strlen(label);
+         strcpy(string, label ? label : name);
+      len = strlen(string);
       
       if(!icon)
       {
@@ -1917,7 +1994,7 @@ public:
       //textOffset = indent * indentSize + (icon ? (icon.width + 4) : 0);
       
       surface.TextOpacity(false);
-      surface.TextExtent(label, len, &w, &h);
+      surface.TextExtent(string, len, &w, &h);
       h = Max(h, 16);
     
       // Draw the current row stipple
@@ -1927,9 +2004,9 @@ public:
          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);
+      surface.WriteTextDots(alignment, xStart, y + 2, width, string, len);
 
-      //if(!guiApp.textMode) -- how to do this in a dll?
+      if(!guiApp.textMode)
       {
          if(displayFlags.current)
          {
@@ -2044,7 +2121,8 @@ public:
    }
    return node;
 }*/
-static FileSystemNode MakeFileSystemNode(const FileStats stats,
+
+FileSystemNode MakeFileSystemNode(const FileStats stats,
       const char * fileName, const char * filePath,
       const bool previewPicture, const DisplaySystem displaySystem)
 {