scroll Label text when height exceeds viewrect

develop
Myk Taylor 2020-11-04 18:06:50 -08:00 committed by myk002
parent 462208cad3
commit 2797061b04
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
1 changed files with 39 additions and 15 deletions

@ -235,9 +235,13 @@ end
function render_text(obj,dc,x0,y0,pen,dpen,disabled)
local width = 0
for iline,line in ipairs(obj.text_lines) do
if dc and obj.start_line_num and iline < obj.start_line_num then
goto continue
end
local x = 0
if dc then
dc:seek(x+x0,y0+iline-1)
local offset = (obj.start_line_num or 1) - 1
dc:seek(x+x0,y0+iline-offset-1)
end
for _,token in ipairs(line) do
token.line = iline
@ -323,6 +327,7 @@ function render_text(obj,dc,x0,y0,pen,dpen,disabled)
token.x2 = x
end
width = math.max(width, x)
::continue::
end
obj.text_width = width
end
@ -340,6 +345,13 @@ end
Label = defclass(Label, Widget)
STANDARDSCROLL = {
STANDARDSCROLL_UP = -1,
STANDARDSCROLL_DOWN = 1,
STANDARDSCROLL_PAGEUP = '-page',
STANDARDSCROLL_PAGEDOWN = '+page',
}
Label.ATTRS{
text_pen = COLOR_WHITE,
text_dpen = COLOR_DARKGREY, -- disabled
@ -350,9 +362,11 @@ Label.ATTRS{
auto_width = false,
on_click = DEFAULT_NIL,
on_rclick = DEFAULT_NIL,
scroll_keys = STANDARDSCROLL,
}
function Label:init(args)
self.start_line_num = 1
self:setText(args.text)
if not self.text_hpen then
self.text_hpen = ((tonumber(self.text_pen) or tonumber(self.text_pen.fg) or 0) + 8) % 16
@ -399,16 +413,33 @@ function Label:onRenderBody(dc)
render_text(self,dc,0,0,text_pen,self.text_dpen,is_disabled(self))
end
function Label:scroll(nlines)
local n = self.start_line_num + nlines
n = math.min(n, self:getTextHeight() - self.frame_body.height)
n = math.max(n, 1)
self.start_line_num = n
end
function Label:onInput(keys)
if not is_disabled(self) then
if keys._MOUSE_L_DOWN and self:getMousePos() and self.on_click then
self:on_click()
end
if keys._MOUSE_R_DOWN and self:getMousePos() and self.on_rclick then
self:on_rclick()
if is_disabled(self) then return false end
if keys._MOUSE_L_DOWN and self:getMousePos() and self.on_click then
self:on_click()
end
if keys._MOUSE_R_DOWN and self:getMousePos() and self.on_rclick then
self:on_rclick()
end
for k,v in pairs(self.scroll_keys) do
if keys[k] then
if v == '+page' then
v = self.frame_body.height
elseif v == '-page' then
v = -self.frame_body.height
end
self:scroll(v)
return true
end
return check_text_keys(self, keys)
end
return check_text_keys(self, keys)
end
----------
@ -417,13 +448,6 @@ end
List = defclass(List, Widget)
STANDARDSCROLL = {
STANDARDSCROLL_UP = -1,
STANDARDSCROLL_DOWN = 1,
STANDARDSCROLL_PAGEUP = '-page',
STANDARDSCROLL_PAGEDOWN = '+page',
}
SECONDSCROLL = {
SECONDSCROLL_UP = -1,
SECONDSCROLL_DOWN = 1,