2012-06-10 15:52:09 +00:00
|
|
|
require "ui/menu"
|
2012-05-27 21:43:00 +00:00
|
|
|
|
|
|
|
FileChooser = Menu:new{
|
2012-12-08 17:25:32 +00:00
|
|
|
height = Screen:getHeight(),
|
|
|
|
width = Screen:getWidth(),
|
2012-12-12 01:35:49 +00:00
|
|
|
path = lfs.currentdir(),
|
2012-05-27 22:14:08 +00:00
|
|
|
parent = nil,
|
2012-05-27 21:43:00 +00:00
|
|
|
show_hidden = false,
|
|
|
|
filter = function(filename) return true end,
|
|
|
|
}
|
|
|
|
|
|
|
|
function FileChooser:init()
|
|
|
|
self:changeToPath(self.path)
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileChooser:changeToPath(path)
|
|
|
|
local dirs = {}
|
|
|
|
local files = {}
|
2012-05-27 22:14:08 +00:00
|
|
|
self.path = path
|
2012-05-27 21:43:00 +00:00
|
|
|
for f in lfs.dir(self.path) do
|
|
|
|
if self.show_hidden or not string.match(f, "^%.[^.]") then
|
|
|
|
local filename = self.path.."/"..f
|
|
|
|
local filemode = lfs.attributes(filename, "mode")
|
|
|
|
if filemode == "directory" and f ~= "." and f~=".." then
|
2013-02-19 13:30:35 +00:00
|
|
|
if self.dir_filter(filename) then
|
|
|
|
table.insert(dirs, f)
|
|
|
|
end
|
2012-05-27 21:43:00 +00:00
|
|
|
elseif filemode == "file" then
|
2013-02-19 13:30:35 +00:00
|
|
|
if self.file_filter(filename) then
|
2012-05-27 21:43:00 +00:00
|
|
|
table.insert(files, f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(dirs)
|
|
|
|
if self.path ~= "/" then table.insert(dirs, 1, "..") end
|
|
|
|
table.sort(files)
|
|
|
|
|
|
|
|
self.item_table = {}
|
|
|
|
for _, dir in ipairs(dirs) do
|
2012-05-27 22:14:08 +00:00
|
|
|
table.insert(self.item_table, { text = dir.."/", path = self.path.."/"..dir })
|
2012-05-27 21:43:00 +00:00
|
|
|
end
|
|
|
|
for _, file in ipairs(files) do
|
2012-05-27 22:14:08 +00:00
|
|
|
table.insert(self.item_table, { text = file, path = self.path.."/"..file })
|
2012-05-27 21:43:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Menu.init(self) -- call parent's init()
|
|
|
|
end
|
2012-05-27 22:14:08 +00:00
|
|
|
|
2012-06-10 15:36:19 +00:00
|
|
|
function FileChooser:onMenuSelect(item)
|
|
|
|
if lfs.attributes(item.path, "mode") == "directory" then
|
2012-05-27 22:14:08 +00:00
|
|
|
UIManager:close(self)
|
2012-06-10 15:36:19 +00:00
|
|
|
self:changeToPath(item.path)
|
2012-05-27 22:14:08 +00:00
|
|
|
UIManager:show(self)
|
|
|
|
else
|
2012-06-10 15:36:19 +00:00
|
|
|
self:onFileSelect(item.path)
|
2012-05-27 22:14:08 +00:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2012-06-10 15:36:19 +00:00
|
|
|
|
|
|
|
function FileChooser:onFileSelect(file)
|
|
|
|
UIManager:close(self)
|
|
|
|
return true
|
|
|
|
end
|