2013-10-18 20:38:07 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
|
|
local TextBoxWidget = require("ui/widget/textboxwidget")
|
|
|
|
local VerticalScrollBar = require("ui/widget/verticalscrollbar")
|
|
|
|
local Geom = require("ui/geometry")
|
|
|
|
local GestureRange = require("ui/gesturerange")
|
|
|
|
local UIManager = require("ui/uimanager")
|
2016-05-01 07:51:50 +00:00
|
|
|
local Device = require("device")
|
|
|
|
local Screen = Device.screen
|
|
|
|
local Input = Device.input
|
2013-10-22 15:11:31 +00:00
|
|
|
local HorizontalGroup = require("ui/widget/horizontalgroup")
|
|
|
|
local HorizontalSpan = require("ui/widget/horizontalspan")
|
2014-10-22 13:34:11 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
2013-10-18 20:38:07 +00:00
|
|
|
|
|
|
|
--[[
|
|
|
|
Text widget with vertical scroll bar
|
|
|
|
--]]
|
|
|
|
local ScrollTextWidget = InputContainer:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
text = nil,
|
2016-05-15 09:18:38 +00:00
|
|
|
charlist = nil,
|
|
|
|
charpos = nil,
|
|
|
|
editable = false,
|
2014-03-13 13:52:43 +00:00
|
|
|
face = nil,
|
2014-10-22 13:34:11 +00:00
|
|
|
fgcolor = Blitbuffer.COLOR_BLACK,
|
2014-03-13 13:52:43 +00:00
|
|
|
width = 400,
|
|
|
|
height = 20,
|
2014-11-20 22:07:39 +00:00
|
|
|
scroll_bar_width = Screen:scaleBySize(6),
|
|
|
|
text_scroll_span = Screen:scaleBySize(6),
|
2014-03-13 13:52:43 +00:00
|
|
|
dialog = nil,
|
2013-10-18 20:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ScrollTextWidget:init()
|
2014-03-13 13:52:43 +00:00
|
|
|
self.text_widget = TextBoxWidget:new{
|
|
|
|
text = self.text,
|
2016-05-15 09:18:38 +00:00
|
|
|
charlist = self.charlist,
|
|
|
|
charpos = self.charpos,
|
|
|
|
editable = self.editable,
|
2014-03-13 13:52:43 +00:00
|
|
|
face = self.face,
|
|
|
|
fgcolor = self.fgcolor,
|
|
|
|
width = self.width - self.scroll_bar_width - self.text_scroll_span,
|
|
|
|
height = self.height
|
|
|
|
}
|
|
|
|
local visible_line_count = self.text_widget:getVisLineCount()
|
|
|
|
local total_line_count = self.text_widget:getAllLineCount()
|
|
|
|
self.v_scroll_bar = VerticalScrollBar:new{
|
|
|
|
enable = visible_line_count < total_line_count,
|
|
|
|
low = 0,
|
|
|
|
high = visible_line_count/total_line_count,
|
2016-04-21 14:13:10 +00:00
|
|
|
width = self.scroll_bar_width,
|
2014-03-13 13:52:43 +00:00
|
|
|
height = self.height,
|
|
|
|
}
|
|
|
|
local horizontal_group = HorizontalGroup:new{}
|
|
|
|
table.insert(horizontal_group, self.text_widget)
|
2016-04-21 14:13:10 +00:00
|
|
|
table.insert(horizontal_group, HorizontalSpan:new{self.text_scroll_span})
|
2014-03-13 13:52:43 +00:00
|
|
|
table.insert(horizontal_group, self.v_scroll_bar)
|
|
|
|
self[1] = horizontal_group
|
|
|
|
self.dimen = Geom:new(self[1]:getSize())
|
|
|
|
if Device:isTouchDevice() then
|
|
|
|
self.ges_events = {
|
2014-07-24 14:10:28 +00:00
|
|
|
ScrollText = {
|
2014-03-13 13:52:43 +00:00
|
|
|
GestureRange:new{
|
|
|
|
ges = "swipe",
|
2014-07-24 14:10:28 +00:00
|
|
|
range = function() return self.dimen end,
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
2016-05-01 07:51:50 +00:00
|
|
|
if Device:hasKeyboard() or Device:hasKeys() then
|
|
|
|
self.key_events = {
|
|
|
|
ScrollDown = {{Input.group.PgFwd}, doc = "scroll down"},
|
|
|
|
ScrollUp = {{Input.group.PgBack}, doc = "scroll up"},
|
|
|
|
}
|
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
end
|
|
|
|
|
2016-06-25 17:27:23 +00:00
|
|
|
function ScrollTextWidget:unfocus()
|
|
|
|
self.text_widget:unfocus()
|
|
|
|
end
|
|
|
|
|
|
|
|
function ScrollTextWidget:focus()
|
|
|
|
self.text_widget:focus()
|
|
|
|
end
|
|
|
|
|
2016-07-19 05:36:51 +00:00
|
|
|
function ScrollTextWidget:moveCursor(x, y)
|
|
|
|
self.text_widget:moveCursor(x, y)
|
|
|
|
end
|
|
|
|
|
2016-05-01 07:51:50 +00:00
|
|
|
function ScrollTextWidget:scrollText(direction)
|
|
|
|
if direction == 0 then return end
|
2016-05-22 15:59:28 +00:00
|
|
|
local low, high
|
2016-05-01 07:51:50 +00:00
|
|
|
if direction > 0 then
|
2016-04-21 14:13:10 +00:00
|
|
|
low, high = self.text_widget:scrollDown()
|
2016-05-01 07:51:50 +00:00
|
|
|
else
|
2016-04-21 14:13:10 +00:00
|
|
|
low, high = self.text_widget:scrollUp()
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2016-05-22 15:59:28 +00:00
|
|
|
self.v_scroll_bar:set(low, high)
|
2014-11-30 00:12:00 +00:00
|
|
|
UIManager:setDirty(self.dialog, function()
|
|
|
|
return "partial", self.dimen
|
|
|
|
end)
|
2013-10-18 20:38:07 +00:00
|
|
|
end
|
2013-10-23 14:37:55 +00:00
|
|
|
|
2016-05-01 07:51:50 +00:00
|
|
|
function ScrollTextWidget:onScrollText(arg, ges)
|
|
|
|
if ges.direction == "north" then
|
|
|
|
self:scrollText(1)
|
|
|
|
elseif ges.direction == "south" then
|
|
|
|
self:scrollText(-1)
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function ScrollTextWidget:onScrollDown()
|
|
|
|
self:scrollText(1)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function ScrollTextWidget:onScrollUp()
|
|
|
|
self:scrollText(-1)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-10-23 14:37:55 +00:00
|
|
|
return ScrollTextWidget
|