From 4b00fd6571bd7af12e2a84aa0edf21547b47e98e Mon Sep 17 00:00:00 2001 From: Stephen Anthony Date: Sat, 17 Aug 2019 18:13:15 -0230 Subject: [PATCH] Fixed selection of previous dir in FileListWidget. I was going to have it remember descending directories too, but it's just too much fooling around for a minor feature. Maybe someone else can take a stab at it ... --- src/common/LinkedObjectPool.hxx | 1 - src/common/Stack.hxx | 13 +++++++++++ src/gui/FileListWidget.cxx | 40 ++++++++++++++++++++++++--------- src/gui/FileListWidget.hxx | 9 +++++--- 4 files changed, 49 insertions(+), 14 deletions(-) diff --git a/src/common/LinkedObjectPool.hxx b/src/common/LinkedObjectPool.hxx index 0da278bca..158bb6564 100644 --- a/src/common/LinkedObjectPool.hxx +++ b/src/common/LinkedObjectPool.hxx @@ -266,7 +266,6 @@ class LinkedObjectPool for(const auto& i: p.myList) os << i << (p.current() == i ? "* " : " "); return os; - } private: diff --git a/src/common/Stack.hxx b/src/common/Stack.hxx index 6146b428d..b0d9c55f8 100644 --- a/src/common/Stack.hxx +++ b/src/common/Stack.hxx @@ -49,6 +49,13 @@ class FixedStack T pop() { return std::move(_stack[--_size]); } uInt32 size() const { return _size; } + // Reverse the contents of the stack + // This operation isn't needed very often, but it's handy to have + void reverse() { + for(uInt32 i = 0, j = _size - 1; i < j; ++i, --j) + std::swap(_stack[i], _stack[j]); + } + // Apply the given function to every item in the stack // We do it this way so the stack API can be preserved, // and no access to individual elements is allowed outside @@ -58,6 +65,12 @@ class FixedStack func(_stack[i]); } + friend ostream& operator<<(ostream& os, const FixedStack& s) { + for(uInt32 pos = 0; pos < s._size; ++pos) + os << s._stack[pos] << " "; + return os; + } + private: // Following constructors and assignment operators not supported FixedStack(const FixedStack&) = delete; diff --git a/src/gui/FileListWidget.cxx b/src/gui/FileListWidget.cxx index 920470e89..613f80ce2 100644 --- a/src/gui/FileListWidget.cxx +++ b/src/gui/FileListWidget.cxx @@ -50,6 +50,31 @@ void FileListWidget::setDirectory(const FilesystemNode& node, string select) _node = _node.getParent(); } + // Initialize history + FilesystemNode tmp = _node; + while(tmp.hasParent()) + { + string name = tmp.getName(); + if(name.back() == '/' || name.back() == '\\') + name.pop_back(); + if(!BSPF::startsWithIgnoreCase(name, " [")) + name = " [" + name + "]"; + + _history.push(name); + tmp = tmp.getParent(); + } + // History is in reverse order; we need to fix that + _history.reverse(); + + // Finally, go to this location + setLocation(_node, select); +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void FileListWidget::setLocation(const FilesystemNode& node, const string& select) +{ + _node = node; + // Read in the data from the file system (start with an empty list) _fileList.clear(); _fileList.reserve(512); @@ -67,9 +92,10 @@ void FileListWidget::setDirectory(const FilesystemNode& node, string select) } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void FileListWidget::setLocation(const FilesystemNode& node, string select) +void FileListWidget::selectDirectory() { - setDirectory(node, select); + _history.push(selected().getName()); + setLocation(selected()); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -77,13 +103,7 @@ void FileListWidget::selectParent() { if(_node.hasParent()) { - // Make sure 'selected' has the proper directory naming scheme - string select = _node.getName(); - if(select.back() == '/' || select.back() == '\\') - select.pop_back(); - if(!BSPF::startsWithIgnoreCase(select, " [")) - select = " [" + select + "]"; - + const string& select = !_history.empty() ? _history.pop() : EmptyString; setLocation(_node.getParent(), select); } } @@ -115,7 +135,7 @@ void FileListWidget::handleCommand(CommandSender* sender, int cmd, int data, int if(selected().isDirectory()) { cmd = ItemChanged; - setLocation(selected(), ""); + selectDirectory(); } else cmd = ItemActivated; diff --git a/src/gui/FileListWidget.hxx b/src/gui/FileListWidget.hxx index aa3f26daa..cebfa6464 100644 --- a/src/gui/FileListWidget.hxx +++ b/src/gui/FileListWidget.hxx @@ -21,7 +21,7 @@ class CommandSender; #include "FSNode.hxx" -#include "LinkedObjectPool.hxx" +#include "Stack.hxx" #include "StringListWidget.hxx" /** @@ -74,7 +74,10 @@ class FileListWidget : public StringListWidget private: /** Very similar to setDirectory(), but also updates the history */ - void setLocation(const FilesystemNode& node, string select); + void setLocation(const FilesystemNode& node, const string& select = EmptyString); + + /** Descend into currently selected directory */ + void selectDirectory(); void handleCommand(CommandSender* sender, int cmd, int data, int id) override; @@ -83,7 +86,7 @@ class FileListWidget : public StringListWidget FilesystemNode _node; FSList _fileList; - Common::LinkedObjectPool _history; + Common::FixedStack _history; string _extension; uInt32 _selected;