Tyro/src/widgets/FilePane.cpp

169 lines
3.5 KiB
C++
Raw Normal View History

2015-07-07 10:01:17 -04:00
#include "src/widgets/FilePane.h"
2015-07-14 10:40:53 -04:00
enum
{
Icon_File,
Icon_FolderClosed,
Icon_FolderOpened
};
FilePane::FilePane(
wxWindow* parent,
2015-07-07 10:01:17 -04:00
wxWindowID id,
const wxPoint& pos,
const wxSize& size,
long style,
const wxString &name
) : wxTreeListCtrl(parent, id, pos, size, style, name)
{
2015-07-07 10:01:17 -04:00
this->InitImageList();
this->SetImageList(this->img_list);
2015-10-22 11:06:34 -04:00
wxString defaultPath(".");
2019-03-20 16:49:25 -04:00
wxFileName filename(defaultPath);
filename.MakeAbsolute(defaultPath);
2015-07-14 10:40:53 -04:00
wxTreeListItem root = this->GetRootItem();
this->CreateTree(defaultPath, root);
2015-07-07 10:01:17 -04:00
this->AppendColumn("",
wxCOL_WIDTH_AUTOSIZE,
wxALIGN_LEFT,
wxCOL_RESIZABLE | wxCOL_SORTABLE);
2015-07-14 10:40:53 -04:00
}
FilePane::~FilePane()
2015-10-22 11:06:34 -04:00
{
delete this->img_list;
}
void FilePane::BindEvents()
{
this->Bind(wxEVT_TREELIST_ITEM_EXPANDING, &FilePane::OpenFolder, this, wxID_ANY);
2019-03-20 16:49:25 -04:00
this->Bind(wxEVT_TREELIST_ITEM_ACTIVATED, &FilePane::OpenFileInEditor, this, wxID_ANY);
2015-10-22 11:06:34 -04:00
}
void FilePane::OpenFolder(wxTreeListEvent& event)
{
2015-07-07 10:01:17 -04:00
2015-10-22 11:06:34 -04:00
wxTreeListItem item = event.GetItem();
const wxString path = this->GetItemText(item, 0);
wxLogDebug(path);
}
2015-10-22 11:06:34 -04:00
/**
* Iterates through the specified folder and creates the tree view
*
* @access private
* @param wxString &path
* @param wxTreeListItem &root
*/
2019-03-20 16:49:25 -04:00
void FilePane::CreateTree(const wxString &path, wxTreeListItem &root, int level)
2015-07-14 10:40:53 -04:00
{
2019-03-20 16:49:25 -04:00
// So yeah, this doesn't really work right.
// It seems I need to create a tree from the list of file paths,
// after which this should be much simpler.
// @TODO Fix
auto *files = new wxArrayString();
2015-10-22 11:06:34 -04:00
wxDir::GetAllFiles(path, files);
2015-07-14 10:40:53 -04:00
2015-10-22 11:06:34 -04:00
vector<wxString> examined;
vector<wxString>::iterator it;
2015-07-14 10:40:53 -04:00
2019-03-20 16:49:25 -04:00
for (const wxString &item : *files)
2015-07-14 10:40:53 -04:00
{
2019-03-20 16:49:25 -04:00
wxFileName filename(item);
2015-10-22 11:06:34 -04:00
// For loose files, just add directly to the tree
2019-03-20 16:49:25 -04:00
if (filename.GetDirCount() == 1)
2015-10-22 11:06:34 -04:00
{
2019-03-20 16:49:25 -04:00
auto fullFileName = filename.GetFullPath();
auto fileData = new wxStringClientData();
fileData->SetData(fullFileName);
examined.push_back(fullFileName);
this->AppendItem(root, filename.GetFullName(), Icon_File, Icon_File, fileData);
continue;
2015-10-22 11:06:34 -04:00
}
2019-03-20 16:49:25 -04:00
// Remove the directory component closest to the root
filename.RemoveDir(0);
wxArrayString folders = filename.GetDirs();
wxLogDebug(item);
wxTreeListItem newRootNode = root;
for (const wxString &curr_folder: folders)
2015-10-22 11:06:34 -04:00
{
2019-03-20 16:49:25 -04:00
// Check if directory has already been created
2015-10-22 11:06:34 -04:00
it = find(examined.begin(), examined.end(), curr_folder);
2019-03-20 16:49:25 -04:00
2015-10-22 11:06:34 -04:00
if (it != examined.end()) continue;
2019-03-20 16:49:25 -04:00
// Create the directory node if it doesn't exist
auto fileData = new wxStringClientData();
fileData->SetData(curr_folder);
wxTreeListItem current = this->AppendItem(
newRootNode,
curr_folder,
Icon_FolderClosed,
Icon_FolderOpened,
fileData);
2015-10-22 11:06:34 -04:00
examined.push_back(curr_folder);
2019-03-20 16:49:25 -04:00
newRootNode = current;
this->CreateTree(curr_folder, root);
2015-10-22 11:06:34 -04:00
}
2015-07-14 10:40:53 -04:00
}
}
2015-10-22 11:06:34 -04:00
/*void FilePane::CreateFolderTree(StringVector dirList, wxTreeListItem &root)
{
}*/
2019-03-20 16:49:25 -04:00
void FilePane::OpenFileInEditor(wxTreeListEvent& event)
{
wxTreeListItem item = event.GetItem();
auto path = this->GetItemText(item, 0);
wxLogMessage(path);
}
2015-10-22 11:06:34 -04:00
/**
* Create the image list object for the file pane widget
*/
2015-07-07 10:01:17 -04:00
void FilePane::InitImageList()
{
2015-07-07 10:01:17 -04:00
wxSize iconSize = wxArtProvider::GetSizeHint(wxART_LIST);
2015-07-07 10:01:17 -04:00
if (iconSize == wxDefaultSize)
{
iconSize = wxSize(16, 16);
}
2015-07-07 10:01:17 -04:00
this->img_list = new wxImageList(iconSize.x, iconSize.y);
static const char* icons[] =
{
wxART_NORMAL_FILE,
wxART_FOLDER,
wxART_FOLDER_OPEN
};
2019-03-20 16:49:25 -04:00
for (auto icon : icons)
2015-07-07 10:01:17 -04:00
{
this->img_list->Add(
2019-03-20 16:49:25 -04:00
wxArtProvider::GetIcon(icon, wxART_LIST, iconSize)
2015-07-07 10:01:17 -04:00
);
}
}