2011-12-08 16:56:07 +00:00
|
|
|
require "rendertext"
|
|
|
|
require "keys"
|
|
|
|
require "graphics"
|
2012-02-16 07:19:56 +00:00
|
|
|
require "fontchooser"
|
2012-02-18 12:32:38 +00:00
|
|
|
require "filesearcher"
|
2012-02-18 10:21:03 +00:00
|
|
|
require "inputbox"
|
2012-02-26 02:42:04 +00:00
|
|
|
require "selectmenu"
|
2011-12-08 16:56:07 +00:00
|
|
|
|
|
|
|
FileChooser = {
|
2011-12-08 18:01:40 +00:00
|
|
|
-- Class vars:
|
2012-02-23 16:16:20 +00:00
|
|
|
-- font for displaying toc item names
|
|
|
|
fsize = 25,
|
|
|
|
face = nil,
|
|
|
|
fhash = nil,
|
|
|
|
--face = freetype.newBuiltinFace("sans", 25),
|
|
|
|
--fhash = "s25",
|
|
|
|
|
2011-12-08 18:01:40 +00:00
|
|
|
-- font for paging display
|
2012-02-23 16:16:20 +00:00
|
|
|
ffsize = 16,
|
|
|
|
fface = nil,
|
|
|
|
ffhash = nil,
|
|
|
|
--sface = freetype.newBuiltinFace("sans", 16),
|
|
|
|
--sfhash = "s16",
|
|
|
|
|
2011-12-08 18:01:40 +00:00
|
|
|
-- spacing between lines
|
|
|
|
spacing = 40,
|
|
|
|
|
|
|
|
-- state buffer
|
2011-12-08 16:56:07 +00:00
|
|
|
dirs = nil,
|
2011-12-08 18:01:40 +00:00
|
|
|
files = nil,
|
|
|
|
items = 0,
|
|
|
|
path = "",
|
|
|
|
page = 1,
|
|
|
|
current = 1,
|
|
|
|
oldcurrent = 0,
|
2011-12-08 16:56:07 +00:00
|
|
|
}
|
|
|
|
|
2011-12-08 18:01:40 +00:00
|
|
|
function FileChooser:readdir()
|
2011-12-08 16:56:07 +00:00
|
|
|
self.dirs = {}
|
|
|
|
self.files = {}
|
2011-12-08 18:01:40 +00:00
|
|
|
for f in lfs.dir(self.path) do
|
|
|
|
if lfs.attributes(self.path.."/"..f, "mode") == "directory" and f ~= "." and not string.match(f, "^%.[^.]") then
|
2011-12-08 16:56:07 +00:00
|
|
|
table.insert(self.dirs, f)
|
|
|
|
elseif string.match(f, ".+%.[pP][dD][fF]$") then
|
|
|
|
table.insert(self.files, f)
|
|
|
|
end
|
|
|
|
end
|
2012-02-17 05:46:40 +00:00
|
|
|
--@TODO make sure .. is sortted to the first item 16.02 2012
|
2011-12-08 16:56:07 +00:00
|
|
|
table.sort(self.dirs)
|
|
|
|
table.sort(self.files)
|
|
|
|
end
|
|
|
|
|
2011-12-08 18:01:40 +00:00
|
|
|
function FileChooser:setPath(newPath)
|
|
|
|
self.path = newPath
|
|
|
|
self:readdir()
|
|
|
|
self.items = #self.dirs + #self.files
|
|
|
|
if self.items == 0 then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
self.page = 1
|
|
|
|
self.current = 1
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2012-02-23 16:16:20 +00:00
|
|
|
function FileChooser:updateFont()
|
|
|
|
if self.fhash ~= FontChooser.cfont..self.fsize then
|
|
|
|
self.face = freetype.newBuiltinFace(FontChooser.cfont, self.fsize)
|
|
|
|
self.fhash = FontChooser.cfont..self.fsize
|
|
|
|
end
|
|
|
|
if self.ffhash ~= FontChooser.ffont..self.ffsize then
|
|
|
|
self.fface = freetype.newBuiltinFace(FontChooser.ffont, self.ffsize)
|
|
|
|
self.ffhash = FontChooser.ffont..self.ffsize
|
|
|
|
end
|
|
|
|
end
|
2012-02-05 03:09:22 +00:00
|
|
|
|
2011-12-08 18:01:40 +00:00
|
|
|
function FileChooser:choose(ypos, height)
|
|
|
|
local perpage = math.floor(height / self.spacing) - 1
|
|
|
|
local pagedirty = true
|
|
|
|
local markerdirty = false
|
2012-02-05 03:09:22 +00:00
|
|
|
|
2012-02-16 07:19:56 +00:00
|
|
|
local prevItem = function ()
|
2012-02-05 03:09:22 +00:00
|
|
|
if self.current == 1 then
|
|
|
|
if self.page > 1 then
|
|
|
|
self.current = perpage
|
|
|
|
self.page = self.page - 1
|
|
|
|
pagedirty = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self.current = self.current - 1
|
|
|
|
markerdirty = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-16 07:19:56 +00:00
|
|
|
local nextItem = function ()
|
2012-02-05 03:09:22 +00:00
|
|
|
if self.current == perpage then
|
|
|
|
if self.page < (self.items / perpage) then
|
|
|
|
self.current = 1
|
|
|
|
self.page = self.page + 1
|
|
|
|
pagedirty = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if self.page ~= math.floor(self.items / perpage) + 1
|
|
|
|
or self.current + (self.page-1)*perpage < self.items then
|
|
|
|
self.current = self.current + 1
|
|
|
|
markerdirty = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-08 16:56:07 +00:00
|
|
|
while true do
|
2012-02-26 02:42:04 +00:00
|
|
|
self:updateFont()
|
2011-12-08 16:56:07 +00:00
|
|
|
if pagedirty then
|
|
|
|
fb.bb:paintRect(0, ypos, fb.bb:getWidth(), height, 0)
|
|
|
|
local c
|
|
|
|
for c = 1, perpage do
|
2011-12-08 18:01:40 +00:00
|
|
|
local i = (self.page - 1) * perpage + c
|
2011-12-08 16:56:07 +00:00
|
|
|
if i <= #self.dirs then
|
2011-12-08 18:01:40 +00:00
|
|
|
-- resembles display in midnight commander: adds "/" prefix for directories
|
|
|
|
renderUtf8Text(fb.bb, 39, ypos + self.spacing*c, self.face, self.fhash, "/", true)
|
|
|
|
renderUtf8Text(fb.bb, 50, ypos + self.spacing*c, self.face, self.fhash, self.dirs[i], true)
|
|
|
|
elseif i <= self.items then
|
|
|
|
renderUtf8Text(fb.bb, 50, ypos + self.spacing*c, self.face, self.fhash, self.files[i-#self.dirs], true)
|
2011-12-08 16:56:07 +00:00
|
|
|
end
|
|
|
|
end
|
2012-02-23 16:16:20 +00:00
|
|
|
renderUtf8Text(fb.bb, 39, ypos + self.spacing * perpage + 32, self.fface, self.ffhash,
|
2011-12-08 18:01:40 +00:00
|
|
|
"Page "..self.page.." of "..(math.floor(self.items / perpage)+1), true)
|
2011-12-08 16:56:07 +00:00
|
|
|
markerdirty = true
|
|
|
|
end
|
|
|
|
if markerdirty then
|
2011-12-08 18:01:40 +00:00
|
|
|
if not pagedirty then
|
|
|
|
if self.oldcurrent > 0 then
|
|
|
|
fb.bb:paintRect(30, ypos + self.spacing*self.oldcurrent + 10, fb.bb:getWidth() - 60, 3, 0)
|
|
|
|
fb:refresh(1, 30, ypos + self.spacing*self.oldcurrent + 10, fb.bb:getWidth() - 60, 3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
fb.bb:paintRect(30, ypos + self.spacing*self.current + 10, fb.bb:getWidth() - 60, 3, 15)
|
|
|
|
if not pagedirty then
|
|
|
|
fb:refresh(1, 30, ypos + self.spacing*self.current + 10, fb.bb:getWidth() - 60, 3)
|
2011-12-08 16:56:07 +00:00
|
|
|
end
|
2011-12-08 18:01:40 +00:00
|
|
|
self.oldcurrent = self.current
|
2011-12-08 16:56:07 +00:00
|
|
|
markerdirty = false
|
|
|
|
end
|
2011-12-08 18:01:40 +00:00
|
|
|
if pagedirty then
|
|
|
|
fb:refresh(0, 0, ypos, fb.bb:getWidth(), height)
|
|
|
|
pagedirty = false
|
|
|
|
end
|
2011-12-08 16:56:07 +00:00
|
|
|
local ev = input.waitForEvent()
|
|
|
|
if ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_PRESS then
|
2012-03-02 05:46:15 +00:00
|
|
|
print("key code:"..ev.code)
|
2012-02-17 06:52:54 +00:00
|
|
|
ev.code = adjustFWKey(ev.code)
|
2012-03-02 05:46:15 +00:00
|
|
|
if ev.code == KEY_SHIFT then
|
|
|
|
Keys.shiftmode = true
|
|
|
|
elseif ev.code == KEY_ALT then
|
|
|
|
Keys.altmode = true
|
|
|
|
elseif ev.code == KEY_FW_UP then
|
2012-02-17 06:52:54 +00:00
|
|
|
prevItem()
|
2011-12-08 16:56:07 +00:00
|
|
|
elseif ev.code == KEY_FW_DOWN then
|
2012-02-17 06:52:54 +00:00
|
|
|
nextItem()
|
2012-02-19 07:29:51 +00:00
|
|
|
elseif ev.code == KEY_F then -- invoke fontchooser menu
|
2012-02-26 02:42:04 +00:00
|
|
|
fonts_menu = SelectMenu:new{
|
|
|
|
menu_title = "Fonts Menu",
|
|
|
|
item_array = FontChooser.fonts,
|
|
|
|
}
|
2012-02-28 15:05:26 +00:00
|
|
|
local re = fonts_menu:choose(0, height)
|
|
|
|
if re then
|
|
|
|
FontChooser.cfont = FontChooser.fonts[re]
|
|
|
|
FontChooser:init()
|
|
|
|
end
|
2012-02-16 07:19:56 +00:00
|
|
|
pagedirty = true
|
2012-02-19 07:29:51 +00:00
|
|
|
elseif ev.code == KEY_S then -- invoke search input
|
2012-02-18 23:21:50 +00:00
|
|
|
keywords = InputBox:input(height-100, 100, "Search:")
|
2012-02-19 06:41:44 +00:00
|
|
|
if keywords then -- display search result according to keywords
|
|
|
|
--[[
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|| uncomment following line and set the correct path if you want
|
|
|
|
|| to test search feature in EMU mode
|
|
|
|
----------------------------------------------------------------
|
|
|
|
--]]
|
2012-02-28 15:07:57 +00:00
|
|
|
--FileSearcher:init("/home/dave/documents/kindle/backup/documents")
|
|
|
|
FileSearcher:init()
|
2012-02-18 23:21:50 +00:00
|
|
|
file = FileSearcher:choose(ypos, height, keywords)
|
|
|
|
if file then
|
|
|
|
return file
|
|
|
|
end
|
|
|
|
end
|
2012-02-17 05:46:40 +00:00
|
|
|
pagedirty = true
|
2011-12-08 16:56:07 +00:00
|
|
|
elseif ev.code == KEY_PGFWD then
|
2011-12-08 18:01:40 +00:00
|
|
|
if self.page < (self.items / perpage) then
|
|
|
|
if self.current + self.page*perpage > self.items then
|
|
|
|
self.current = self.items - self.page*perpage
|
2011-12-08 16:56:07 +00:00
|
|
|
end
|
2011-12-08 18:01:40 +00:00
|
|
|
self.page = self.page + 1
|
2011-12-08 16:56:07 +00:00
|
|
|
pagedirty = true
|
|
|
|
else
|
2011-12-08 18:01:40 +00:00
|
|
|
self.current = self.items - (self.page-1)*perpage
|
2011-12-08 16:56:07 +00:00
|
|
|
markerdirty = true
|
|
|
|
end
|
|
|
|
elseif ev.code == KEY_PGBCK then
|
2011-12-08 18:01:40 +00:00
|
|
|
if self.page > 1 then
|
|
|
|
self.page = self.page - 1
|
2011-12-08 16:56:07 +00:00
|
|
|
pagedirty = true
|
|
|
|
else
|
2011-12-08 18:01:40 +00:00
|
|
|
self.current = 1
|
2011-12-08 16:56:07 +00:00
|
|
|
markerdirty = true
|
|
|
|
end
|
2011-12-08 23:22:09 +00:00
|
|
|
elseif ev.code == KEY_ENTER or ev.code == KEY_FW_PRESS then
|
2011-12-08 18:01:40 +00:00
|
|
|
local newdir = self.dirs[perpage*(self.page-1)+self.current]
|
2011-12-08 16:56:07 +00:00
|
|
|
if newdir == ".." then
|
2011-12-08 18:01:40 +00:00
|
|
|
local path = string.gsub(self.path, "(.*)/[^/]+/?$", "%1")
|
|
|
|
self:setPath(path)
|
2011-12-08 16:56:07 +00:00
|
|
|
elseif newdir then
|
2011-12-08 18:01:40 +00:00
|
|
|
local path = self.path.."/"..newdir
|
|
|
|
self:setPath(path)
|
2011-12-08 16:56:07 +00:00
|
|
|
else
|
2011-12-08 18:01:40 +00:00
|
|
|
return self.path.."/"..self.files[perpage*(self.page-1)+self.current - #self.dirs]
|
2011-12-08 16:56:07 +00:00
|
|
|
end
|
2011-12-08 18:01:40 +00:00
|
|
|
pagedirty = true
|
2011-12-08 16:56:07 +00:00
|
|
|
elseif ev.code == KEY_BACK then
|
|
|
|
return nil
|
|
|
|
end
|
2012-03-02 05:46:15 +00:00
|
|
|
elseif ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_RELEASE
|
|
|
|
and ev.code == KEY_SHIFT then
|
|
|
|
Keys.shiftmode = false
|
|
|
|
elseif ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_RELEASE
|
|
|
|
and ev.code == KEY_ALT then
|
|
|
|
Keys.altmode = false
|
2011-12-08 16:56:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|