2013-10-18 20:38:07 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
|
|
local CenterContainer = require("ui/widget/container/centercontainer")
|
2014-02-12 12:32:23 +00:00
|
|
|
local GestureRange = require("ui/gesturerange")
|
2013-10-18 20:38:07 +00:00
|
|
|
local Menu = require("ui/widget/menu")
|
2014-02-12 12:32:23 +00:00
|
|
|
local Geom = require("ui/geometry")
|
2013-10-18 20:38:07 +00:00
|
|
|
local Screen = require("ui/screen")
|
|
|
|
local Device = require("ui/device")
|
|
|
|
local UIManager = require("ui/uimanager")
|
2013-10-22 15:11:31 +00:00
|
|
|
local Event = require("ui/event")
|
2014-06-10 13:32:49 +00:00
|
|
|
local Font = require("ui/font")
|
2014-06-10 13:13:21 +00:00
|
|
|
local DEBUG = require("dbg")
|
2013-10-18 20:38:07 +00:00
|
|
|
local _ = require("gettext")
|
|
|
|
|
|
|
|
local ReaderToc = InputContainer:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
toc = nil,
|
|
|
|
toc_menu_title = _("Table of contents"),
|
2012-05-27 11:47:22 +00:00
|
|
|
}
|
|
|
|
|
2012-12-31 00:13:46 +00:00
|
|
|
function ReaderToc:init()
|
2014-03-13 13:52:43 +00:00
|
|
|
if Device:hasKeyboard() then
|
|
|
|
self.key_events = {
|
|
|
|
ShowToc = {
|
|
|
|
{ "T" },
|
2014-06-05 06:58:53 +00:00
|
|
|
doc = "show Table of Content menu" },
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
if Device:isTouchDevice() then
|
|
|
|
self.ges_events = {
|
|
|
|
ShowToc = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "two_finger_swipe",
|
|
|
|
range = Geom:new{
|
|
|
|
x = 0, y = 0,
|
|
|
|
w = Screen:getWidth(),
|
|
|
|
h = Screen:getHeight(),
|
|
|
|
},
|
|
|
|
direction = "east"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
self.ui.menu:registerToMainMenu(self)
|
2012-12-31 00:13:46 +00:00
|
|
|
end
|
|
|
|
|
2012-05-27 11:47:22 +00:00
|
|
|
function ReaderToc:cleanUpTocTitle(title)
|
2014-03-13 13:52:43 +00:00
|
|
|
return (title:gsub("\13", ""))
|
2012-05-27 11:47:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderToc:onSetDimensions(dimen)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.dimen = dimen
|
2012-05-27 11:47:22 +00:00
|
|
|
end
|
|
|
|
|
2013-01-09 08:53:32 +00:00
|
|
|
function ReaderToc:onUpdateToc()
|
2014-03-13 13:52:43 +00:00
|
|
|
self.toc = nil
|
|
|
|
return true
|
2013-01-09 08:53:32 +00:00
|
|
|
end
|
|
|
|
|
2014-04-04 15:02:29 +00:00
|
|
|
function ReaderToc:onPageUpdate(pageno)
|
|
|
|
self.pageno = pageno
|
|
|
|
end
|
|
|
|
|
2013-01-01 19:37:36 +00:00
|
|
|
function ReaderToc:fillToc()
|
2014-03-13 13:52:43 +00:00
|
|
|
self.toc = self.ui.document:getToc()
|
2013-01-01 19:37:36 +00:00
|
|
|
end
|
2012-05-27 13:24:33 +00:00
|
|
|
|
2013-01-01 19:37:36 +00:00
|
|
|
-- _getTocTitleByPage wrapper, so specific reader
|
2012-05-27 13:24:33 +00:00
|
|
|
-- can tranform pageno according its need
|
2013-01-01 19:37:36 +00:00
|
|
|
function ReaderToc:getTocTitleByPage(pn_or_xp)
|
2014-03-13 13:52:43 +00:00
|
|
|
local page = pn_or_xp
|
|
|
|
if type(pn_or_xp) == "string" then
|
|
|
|
page = self.ui.document:getPageFromXPointer(pn_or_xp)
|
|
|
|
end
|
|
|
|
return self:_getTocTitleByPage(page)
|
2012-05-27 13:24:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderToc:_getTocTitleByPage(pageno)
|
2014-03-13 13:52:43 +00:00
|
|
|
if not self.toc then
|
|
|
|
-- build toc when needed.
|
|
|
|
self:fillToc()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- no table of content
|
|
|
|
if #self.toc == 0 then
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
|
|
|
|
local pre_entry = self.toc[1]
|
|
|
|
for _k,_v in ipairs(self.toc) do
|
|
|
|
if _v.page > pageno then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
pre_entry = _v
|
|
|
|
end
|
|
|
|
return self:cleanUpTocTitle(pre_entry.title)
|
2012-05-27 13:24:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderToc:getTocTitleOfCurrentPage()
|
2014-03-13 13:52:43 +00:00
|
|
|
return self:getTocTitleByPage(self.pageno)
|
2012-05-27 13:24:33 +00:00
|
|
|
end
|
|
|
|
|
2012-05-27 11:47:22 +00:00
|
|
|
function ReaderToc:onShowToc()
|
2014-03-13 13:52:43 +00:00
|
|
|
if not self.toc then
|
|
|
|
self:fillToc()
|
|
|
|
end
|
|
|
|
-- build menu items
|
|
|
|
if #self.toc > 0 and not self.toc[1].text then
|
|
|
|
for _,v in ipairs(self.toc) do
|
2014-06-08 06:08:18 +00:00
|
|
|
v.text = (" "):rep(v.depth-1)..self:cleanUpTocTitle(v.title)
|
2014-03-13 13:52:43 +00:00
|
|
|
v.mandatory = v.page
|
|
|
|
end
|
|
|
|
end
|
2014-04-04 15:02:29 +00:00
|
|
|
-- update current entry
|
|
|
|
if #self.toc > 0 then
|
|
|
|
for i=1, #self.toc do
|
|
|
|
v = self.toc[i]
|
|
|
|
if v.page > self.pageno then
|
|
|
|
self.toc.current = i > 1 and i - 1 or 1
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
|
|
|
|
local toc_menu = Menu:new{
|
|
|
|
title = _("Table of Contents"),
|
|
|
|
item_table = self.toc,
|
|
|
|
ui = self.ui,
|
2014-06-10 13:32:49 +00:00
|
|
|
is_borderless = true,
|
2014-04-04 15:37:50 +00:00
|
|
|
width = Screen:getWidth(),
|
|
|
|
height = Screen:getHeight(),
|
2014-06-10 13:32:49 +00:00
|
|
|
cface = Font:getFace("cfont", 20),
|
2014-06-10 13:13:21 +00:00
|
|
|
on_close_ges = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "two_finger_swipe",
|
|
|
|
range = Geom:new{
|
|
|
|
x = 0, y = 0,
|
|
|
|
w = Screen:getWidth(),
|
|
|
|
h = Screen:getHeight(),
|
|
|
|
},
|
|
|
|
direction = "west"
|
|
|
|
}
|
|
|
|
}
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
|
|
|
|
2014-04-04 15:37:50 +00:00
|
|
|
local menu_container = CenterContainer:new{
|
|
|
|
dimen = Screen:getSize(),
|
|
|
|
toc_menu,
|
|
|
|
}
|
2014-03-13 13:52:43 +00:00
|
|
|
|
|
|
|
function toc_menu:onMenuChoice(item)
|
|
|
|
self.ui:handleEvent(Event:new("PageUpdate", item.page))
|
|
|
|
end
|
|
|
|
|
|
|
|
toc_menu.close_callback = function()
|
2014-05-31 12:43:44 +00:00
|
|
|
UIManager:close(menu_container)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
|
2014-05-31 12:43:44 +00:00
|
|
|
toc_menu.show_parent = menu_container
|
|
|
|
|
|
|
|
UIManager:show(menu_container)
|
2014-05-30 14:11:46 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
return true
|
2012-05-27 11:47:22 +00:00
|
|
|
end
|
|
|
|
|
2013-03-14 05:58:42 +00:00
|
|
|
function ReaderToc:addToMainMenu(tab_item_table)
|
2014-03-13 13:52:43 +00:00
|
|
|
-- insert table to main reader menu
|
|
|
|
table.insert(tab_item_table.navi, {
|
|
|
|
text = self.toc_menu_title,
|
|
|
|
callback = function()
|
|
|
|
self:onShowToc()
|
|
|
|
end,
|
|
|
|
})
|
2012-12-31 00:13:46 +00:00
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
|
|
|
return ReaderToc
|