+public import "ecere"
+import "SplitWindow"
+import "ArrayFactoredGrowth"
+
+//import "ArrayFactoredGrowth"
+//import "ArrayBinarySorted"
+
+#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 */
+
+ "<:ecere>mimetypes/file.png", /* normalFile */
+ "<:ecere>mimetypes/textEcereWorkspace.png", /* ewsFile */
+ "<:ecere>mimetypes/textEcereProject.png", /* epjFile */
+ "<:ecere>mimetypes/textEcereSource.png", /* ecFile */
+ "<:ecere>mimetypes/textEcereHeader.png", /* ehFile */
+ "<:ecere>mimetypes/textCSource.png", /* cFile */
+ "<:ecere>mimetypes/textCHeader.png", /* hFile */
+ "<:ecere>mimetypes/textC++Source.png", /* cppFile */
+ "<:ecere>mimetypes/textC++Header.png", /* hppFile */
+ "<:ecere>mimetypes/text.png", /* textFile */
+ "<:ecere>mimetypes/textHyperTextMarkup.png", /* webFile */
+ "<:ecere>mimetypes/image.png", /* pictureFile */
+ "<:ecere>status/audioVolumeHigh.png", /* soundFile */
+ "<:ecere>mimetypes/package.png", /* archiveFile */
+ "<:ecere>mimetypes/packageSoftware.png", /* packageFile */
+ "<:ecere>mimetypes/packageOpticalDisc.png", /* opticalMediaImageFile */
+
+ "<:ecere>places/folder.png", /* folder */
+ "<:ecere>status/folderOpen.png", /* folderOpen */
+ "<:ecere>devices/computer.png", /* computer */
+ "<:ecere>devices/driveHardDisk.png", /* drive */
+ "<:ecere>places/driveRemote.png", /* netDrive */
+ "<:ecere>devices/mediaOptical.png", /* cdrom */
+ "<:ecere>devices/driveRemovableMedia.png", /* removable */
+ "<:ecere>devices/mediaFloppy.png", /* floppy */
+ "<:ecere>places/networkWorkgroup.png", /* network */
+ "<:ecere>places/networkServer.png", /* server */
+ "<:ecere>places/folderRemote.png", /* share */
+
+ "<:ecere>mimetypes/package.png", /* treeLoader */
+ "<:ecere>places/startHere.png", /* lineNumbers */
+
+ ""
+};
+
+public enum ExplorerFileType
+{
+ none,
+
+ normalFile, ewsFile, epjFile, ecFile, ehFile, cFile, hFile, cppFile, hppFile,
+ textFile, webFile, pictureFile, soundFile,
+ archiveFile, packageFile, opticalMediaImageFile, /* these (all previous) are sort equal */
+
+ folder, folderOpen, computer,
+ drive, netDrive, cdrom, removable, floppy, network, server, share, // these are sort equal
+
+ // utilities
+ treeLoader,
+ lineNumbers;
+
+
+ /*property char *
+ {
+ set
+ {
+ this = SelectByExtension(value);
+ }
+ }*/
+
+ public property bool isFolderType
+ {
+ get { return this >= folder && this <= share; }
+ }
+
+ public property bool isFileType
+ {
+ get { return this >= normalFile && this <= opticalMediaImageFile; }
+ }
+
+ ExplorerFileType ::SelectByExtension(char * extension)
+ {
+ if(!strcmpi(extension, "ews"))
+ return ewsFile;
+ else if(!strcmpi(extension, "epj"))
+ return epjFile;
+ else if(!strcmpi(extension, "ec"))
+ return ecFile;
+ else if(!strcmpi(extension, "eh"))
+ return ehFile;
+ else if(!strcmpi(extension, "cpp") ||
+ !strcmpi(extension, "cc") || !strcmpi(extension, "cxx"))
+ return cppFile;
+ else if(!strcmpi(extension, "hpp") ||
+ !strcmpi(extension, "hh") || !strcmpi(extension, "hxx"))
+ return hppFile;
+ else if(!strcmpi(extension, "c"))
+ return cFile;
+ else if(!strcmpi(extension, "h"))
+ return hFile;
+ else if(!strcmpi(extension, "txt") || !strcmpi(extension, "text") ||
+ !strcmpi(extension, "nfo") || !strcmpi(extension, "info"))
+ return textFile;
+ else if(!strcmpi(extension, "htm") || !strcmpi(extension, "html") ||
+ !strcmpi(extension, "css") || !strcmpi(extension, "php") ||
+ !strcmpi(extension, "js"))
+ return webFile;
+ else if(!strcmpi(extension, "bmp") || !strcmpi(extension, "pcx") ||
+ !strcmpi(extension, "jpg") || !strcmpi(extension, "jpeg") ||
+ !strcmpi(extension, "gif") || !strcmpi(extension, "png") ||
+ !strcmpi(extension, "ico"))
+ return pictureFile;
+ else if(!strcmpi(extension, "wav") || !strcmpi(extension, "mp3") ||
+ !strcmpi(extension, "ogg") || !strcmpi(extension, "snd"))
+ return soundFile;
+ else if(!strcmpi(extension, "ear") || !strcmpi(extension, "7z") ||
+ !strcmpi(extension, "rar") || !strcmpi(extension, "zip") ||
+ !strcmpi(extension, "gz") || !strcmpi(extension, "bz2") ||
+ !strcmpi(extension, "tar") || !strcmpi(extension, "arj") ||
+ !strcmpi(extension, "lza") || !strcmpi(extension, "lzh") ||
+ !strcmpi(extension, "cpio") || !strcmpi(extension, "z"))
+ return archiveFile;
+ else if(!strcmpi(extension, "cab") || !strcmpi(extension, "deb") ||
+ !strcmpi(extension, "rpm"))
+ return packageFile;
+ else if(!strcmpi(extension, "iso") || !strcmpi(extension, "mds") ||
+ !strcmpi(extension, "cue") || !strcmpi(extension, "bin") ||
+ !strcmpi(extension, "ccd") || !strcmpi(extension, "bwt") ||
+ !strcmpi(extension, "cdi") || !strcmpi(extension, "nrg"))
+ return opticalMediaImageFile;
+ return normalFile;
+ }
+};
+
+class ExplorerControl : Window
+{
+ bool previewPictures;
+
+ BitmapResource fileIcons[ExplorerFileType];
+
+ ExplorerControl()
+ {
+ ExplorerFileType c;
+ for(c = 0; c < ExplorerFileType::enumSize; c++)
+ {
+ fileIcons[c] = BitmapResource { fileIconNames[c], alphaBlend = true };
+ AddResource(fileIcons[c]);
+ }
+ }
+}
+
+class ExplorerView : ExplorerControl
+{
+ borderStyle = none;
+ hasHorzScroll = false;
+ hasVertScroll = false;
+
+ virtual void Load(ExplorerFileBranch parent);
+ virtual void Refresh();
+
+ virtual void LaunchNotifyItemSelect(Window master, ExplorerView view, ExplorerFileItem item, ExplorerFileItemArray selectedItems)
+ {
+ view.NotifyItemSelect(master, view, item, selectedItems);
+ }
+
+ virtual bool Window::NotifyItemSelect(ExplorerView view, ExplorerFileItem item, ExplorerFileItemArray selectedItems);
+ virtual bool Window::NotifyItemOpen(ExplorerView view, ExplorerFileItem item);
+
+ ListBox list
+ {
+ master = master, parent = this;
+ //this, master;
+ borderStyle = none;
+ hasHorzScroll = true;
+ hasVertScroll = true;
+ resizable = true;
+ sortable = true;
+ fullRowSelect = false;
+ multiSelect = true;
+
+ anchor = Anchor { left = 0, top = 0, right = 0, bottom = 0 };
+
+ 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;
+ }
+
+ bool NotifyDoubleClick(ListBox listBox, int x, int y, Modifiers mods)
+ {
+ 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)
+ {
+ ExplorerView view = (ExplorerView)listBox.parent;
+ view.NotifyItemOpen(listBox.parent.master, view, (ExplorerFileItem)listBox.currentRow.tag);
+ }
+ return true;
+ }
+ };
+
+ ExplorerView()
+ {
+ }
+}
+
+class ExplorerViewList : ExplorerView
+{
+
+ ExplorerFileBranch location;
+
+public:
+
+ DataField nameField { header = "Name", dataType = "ExplorerFileItem", width = 304, editable = true, userData = this };
+
+ ExplorerViewDetails()
+ {
+ list.AddField(nameField);
+ }
+
+ void Refresh()
+ {
+ Load(location);
+ }
+
+ void Load(ExplorerFileBranch location)
+ {
+ 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);
+ }
+ }
+}
+
+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()
+ {
+ list.AddField(nameField);
+ list.AddField(typeField);
+ list.AddField(sizeField);
+ }
+
+ void Refresh()
+ {
+ Load(location);
+ }
+
+ void Load(ExplorerFileBranch location)
+ {
+ 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);
+ row.SetData(typeField, CopyString(item.extension));
+ row.SetData(sizeField, (void *)listing.stats.size);
+ }
+ list.Sort(nameField, 1);
+ }
+ }
+}
+
+class ExplorerViewIcons : ExplorerView
+{
+
+ ExplorerFileBranch location;
+
+public:
+
+ DataField nameField { header = "Name", dataType = "ExplorerFileItem", width = 304, editable = true, userData = this };
+
+ ExplorerViewDetails()
+ {
+ list.AddField(nameField);
+ }
+
+ void Refresh()
+ {
+ Load(location);
+ }
+
+ void Load(ExplorerFileBranch location)
+ {
+ 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);
+ }
+ }
+}
+
+class ExplorerViewCards : ExplorerView
+{
+
+ ExplorerFileBranch location;
+
+public:
+
+ DataField nameField { header = "Name", dataType = "ExplorerFileItem", width = 304, editable = true, userData = this };
+
+ ExplorerViewDetails()
+ {
+ list.AddField(nameField);
+ }
+
+ void Refresh()
+ {
+ Load(location);
+ }
+
+ void Load(ExplorerFileBranch location)
+ {
+ 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);
+ }
+ }
+}
+
+public class BitmapArray : RedjArray
+{
+ type = class(Bitmap);
+public:
+ Bitmap * const _;
+ Bitmap * Add(Bitmap bitmap)
+ {
+ uint pos = _count;
+ Append(1);
+ _[pos] = bitmap;
+ return &_[pos];
+ }
+ Bitmap * AddBefore(uint position, Bitmap bitmap)
+ {
+ Insert(position, 1);
+ _[position] = bitmap;
+ return &_[position];
+ }
+ void Clear()
+ {
+ int c;
+ for(c = 0; c < _count; c++)
+ {
+ _[c].Free();
+ delete _[c];
+ }
+ count = 0;
+ size = 0;
+ }
+}
+
+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
+ {
+ this;
+ borderStyle = none;
+ anchor = Anchor { top = 0, right = 0, bottom = 0 };
+
+ void OnRedraw(Surface surface)
+ {
+ 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
+ {
+ surface.SetForeground(white);
+ surface.Area(0, 0, view.clientSize.w - 1, view.clientSize.h - 1);
+ }
+ }
+ }
+
+ SplitWindow split
+ {
+ this;
+ leftPane = list;
+ rightPane = show;
+ split = 200;
+ tabCycle = true;
+ };
+
+ ExplorerViewDetails()
+ {
+ list.AddField(nameField);
+ }
+
+ void LaunchNotifyItemSelect(Window master, ExplorerViewShowcase view, ExplorerFileItem item, ExplorerFileItemArray selectedItems)
+ {
+ 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);
+ }
+
+ 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);
+ }
+
+ view.show.Update(null);
+ view.NotifyItemSelect(master, view, item, selectedItems);
+ }
+
+ void Refresh()
+ {
+ Load(location);
+ }
+
+ void Load(ExplorerFileBranch location)
+ {
+ 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);
+ }
+ }
+}
+
+class ExplorerTree : ExplorerControl
+{
+ hasHorzScroll = false;
+ hasVertScroll = false;
+
+ menu = Menu { };
+
+public:
+
+ DataField nameField { dataType = "ExplorerFileBranch", width = 240, userData = this };
+
+ ExplorerFileBranch root;
+ ExplorerFileBranch selection;
+
+ virtual bool Window::NotifyBranchSelect(ExplorerTree tree, ExplorerFileBranch branch);
+
+ property ExplorerFileBranch branch
+ {
+ get
+ {
+ if(!tree)
+ return null;
+ if(!tree.currentRow)
+ return null;
+ if(!tree.currentRow.tag)
+ return null;
+ return (ExplorerFileBranch)tree.currentRow.tag;
+ }
+ }
+
+ void Select(ExplorerFileBranch branch)
+ {
+ if(branch.row)
+ {
+ branch.EnsureVisible(false);
+ tree.SelectRow(branch.row);
+ }
+ }
+
+ ExplorerFileBranch Find(const char * name, ExplorerFileBranch parent)
+ {
+ ExplorerFileBranch branch;
+ ExplorerFileBranch start = parent ? parent : root;
+ if(!start.loaded || !start.childrenLoaded)
+ BranchLoad(start, tree);
+ for(branch = start.children.first; branch; branch = branch.next)
+ if(branch.name && !strcmpi(branch.name, name))
+ return branch;
+ return null;
+ }
+
+ ListBox tree
+ {
+ master = master, parent = this;
+ //this, master;
+ borderStyle = none;
+ hasHorzScroll = true;
+ hasVertScroll = true;
+ fullRowSelect = false;
+ treeBranches = true;
+ collapseControl = true;
+ rootCollapseButton = true;
+
+ anchor = Anchor { left = 0, top = 0, right = 0, bottom = 0 };
+
+ // WHY is this not working ?
+ /*void OnResize(int width, int height)
+ {
+ if(vertScroll.visible)
+ nameField.width = width - vertScroll.size.w;
+ else
+ nameField.width = width;
+ }*/
+
+ bool NotifyCollapse(ListBox listBox, DataRow row, bool collapsed)
+ {
+ if(row)
+ {
+ ExplorerFileBranch branch = (ExplorerFileBranch)row.tag;
+ ExplorerFileBranch child;
+ if(collapsed)
+ {
+ /*
+ for(child = branch.children.last; child; child = branch.children.last)
+ {
+ listBox.DeleteRow(child.row);
+ child.Free();
+ delete child;
+ }
+ branch.childrenLoaded = false;
+ */
+ }
+ else
+ {
+ if(!branch.loaded || !branch.childrenLoaded)
+ BranchLoad(branch, tree);
+ for(child = branch.children.first; child && child.next; child = child.next);
+ if(child)
+ child.EnsureVisible(false);
+ }
+ }
+ return true;
+ }
+
+ bool NotifyRightClick(ListBox listBox, int x, int y, Modifiers mods)
+ {
+ DataRow row = listBox.currentRow;
+ if(row)
+ {
+ ExplorerFileBranch branch = (ExplorerFileBranch)row.tag;
+ if(branch)
+ {
+ PopupMenu popup;
+ Menu menu { };
+
+ MenuItem { menu, "Cut\tCtrl+X", t, NotifySelect = null, disabled = false };
+ MenuItem { menu, "Copy\tCtrl+C", c, NotifySelect = null, disabled = false };
+ MenuItem { menu, "Paste\tCtrl+V", p, NotifySelect = null, disabled = false /*!clipboard*/ };
+ MenuItem { menu, "Delete\tDel", d, NotifySelect = null, disabled = false };
+ //MenuDivider { menu };
+
+ popup = PopupMenu
+ {
+ master = this, menu = menu,
+ position = {
+ x + clientStart.x + absPosition.x - guiApp.desktop.position.x,
+ y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
+ };
+ popup.Create();
+ }
+ }
+ return true;
+ }
+
+ bool NotifySelect(ListBox listBox, DataRow row, Modifiers mods)
+ {
+ if(row)
+ {
+ ExplorerFileBranch branch = (ExplorerFileBranch)row.tag;
+ NotifyBranchSelect(listBox.parent.master, this, branch);
+ selection = branch;
+ }
+ return true;
+ }
+
+ bool NotifyEditing(ListBox listBox, DataRow row)
+ {
+ if(row)
+ {
+ ExplorerFileBranch branch = (ExplorerFileBranch)row.tag;
+ }
+ return true;
+ }
+
+ bool NotifyEdited(ListBox listBox, DataRow row)
+ {
+ if(row)
+ {
+ ExplorerFileBranch branch = (ExplorerFileBranch)row.tag;
+ }
+ return true;
+ }
+
+ bool NotifyEditDone(ListBox listBox, DataRow row)
+ {
+ if(row)
+ {
+ ExplorerFileBranch branch = (ExplorerFileBranch)row.tag;
+ }
+ return true;
+ }
+ };
+
+ // Edit Menu
+ Menu editMenu { menu, "Edit", e };
+ MenuItem itemEditCut
+ {
+ editMenu, "Cut\tCtrl+X", t, disabled = true;
+
+ bool NotifySelect(MenuItem selection, Modifiers mods)
+ {
+ //EditCut();
+ return true;
+ }
+ };
+ MenuItem itemEditCopy
+ {
+ editMenu, "Copy\tCtrl+C", c, disabled = true;
+
+ bool NotifySelect(MenuItem selection, Modifiers mods)
+ {
+ //EditCopy();
+ return true;
+ }
+ };
+ MenuItem itemEditPaste
+ {
+ editMenu, "Paste\tCtrl+V", p;
+
+ bool NotifySelect(MenuItem selection, Modifiers mods)
+ {
+ //EditPaste();
+ return true;
+ }
+ };
+ MenuItem itemEditDelete
+ {
+ editMenu, "Delete\tDel", d, disabled = true;
+
+ bool NotifySelect(MenuItem selection, Modifiers mods)
+ {
+ //EditDelete();
+ return true;
+ }
+ };
+
+ // WHY is this crashing ?
+ /*void OnResize(int width, int height)
+ {
+ if(this && nameField)
+ nameField.width = width - 80;
+ }*/
+
+ ExplorerTree()
+ {
+ tree.AddField(nameField);
+ }
+
+ void Load()
+ {
+ ExplorerFileBranch parent;
+ ExplorerFileBranch branch;
+ FileListing listing { "/" };
+
+ tree.Clear();
+
+ root = ExplorerFileBranch { type = computer, loaded = true, childrenLoaded = true };
+ #ifdef __WIN32__
+ root.name = rootName;
+ #else
+ root.name = "/";
+ #endif
+ AddBranch(root, true, false, null, tree);
+
+ // How can this make sense for linux?
+ #ifdef __WIN32__
+ while(listing.Find())
+ {
+ int len = strlen(listing.name);
+ char info[MAX_LOCATION];
+ char name[MAX_LOCATION];
+ if(listing.stats.attribs.isDrive &&
+ len > 3 && !strncmp(&listing.name[1], ": [", 3))
+ {
+ strncpy(name, listing.name, 2);
+ name[2] = 0;
+ strncpy(info, &listing.name[4], len - 5);
+ info[len - 5] = 0;
+ }
+ else
+ {
+ strcpy(name, listing.name);
+ info[0] = 0;
+ }
+
+ parent = MakeFileBranch(listing.stats, name);
+ if(info[0])
+ parent.info = CopyString(info);
+ parent.loaded = true;
+ AddBranch(parent, !listing.stats.attribs.isDirectory, listing.stats.attribs.isDirectory, root, tree);
+ if(!listing.stats.attribs.isDirectory)
+ parent.childrenLoaded = true;
+ }
+
+ 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
+ }
+
+/*
+public class ClipBoardFiles
+{
+
+public:
+
+ property
+
+}
+
+ // CLIPBOARD
+ void Copy()
+ {
+ if(this)
+ {
+ int size = SelSize();
+ if(size)
+ {
+ // Try to allocate memory
+ ClipBoard clipBoard { };
+ if(clipBoard.Allocate(size+1))
+ {
+ GetSel(clipBoard.memory, true);
+ // Save clipboard
+ clipBoard.Save();
+ }
+ delete clipBoard;
+ }
+ }
+ }
+
+ void Paste()
+ {
+ if(this)
+ {
+ ClipBoard clipBoard { };
+ if(clipBoard.Load())
+ PutS(clipBoard.memory);
+ delete clipBoard;
+ }
+ }
+
+ void Cut()
+ {
+ if(this)
+ {
+ Copy();
+ DelSel();
+ SetViewToCursor(true);
+ Modified();
+ }
+ }
+
+Private Type DROPFILES
+ pFiles As Long
+ pt As POINTAPI
+ fNC As Long
+ fWide As Long
+End Type
+For iCounter = 0 To filelist.ListCount - 1
+ If filelist.Selected(iCounter) = True Then
+ strFiles = strFiles & FixPath(filelist.Path) & filelist.List(iCounter) & vbNullChar
+ End If
+Next
+'all selected items are now put in strFiles
+
+hGlobal = GlobalAlloc(GHND, Len(DF) + Len(strFiles)) 'put all files to a exclusive number
+If hGlobal Then 'if the globalalloc worked
+ lpGlobal = GlobalLock(hGlobal) 'lock the hGlobal
+ DF.pFiles = Len(DF) 'set the size of the files
+
+ Call CopyMem(ByVal lpGlobal, DF, Len(DF)) 'copy df to the lpglobal
+ Call CopyMem(ByVal (lpGlobal + Len(DF)), ByVal strFiles, Len(strFiles)) 'copy strfiles to lpglobal
+ Call GlobalUnlock(hGlobal) 'unlock hglobal again
+
+ SetClipboardData CF_HDROP, hGlobal 'put files to the clipboard
+End If
+
+ bool SaveFile(const char * filePath)
+ {
+ }
+*/
+
+}
+
+/*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();
+}
+