2021-06-09 16:11:48 +00:00
|
|
|
# listbox.py Extension to ugui providing the Listbox class
|
|
|
|
|
|
|
|
# Released under the MIT License (MIT). See LICENSE.
|
|
|
|
# Copyright (c) 2021 Peter Hinch
|
2021-09-12 12:52:57 +00:00
|
|
|
|
|
|
|
# 12 Sep 21 Support for scrolling.
|
|
|
|
|
2021-06-09 16:11:48 +00:00
|
|
|
from gui.core.ugui import Widget, display
|
|
|
|
from gui.core.colors import *
|
|
|
|
|
|
|
|
dolittle = lambda *_ : None
|
|
|
|
|
|
|
|
# Behaviour has issues compared to touch displays because movement between
|
|
|
|
# entries is sequential. This can affect the choice in when the callback runs.
|
|
|
|
# It always runs when select is pressed. See 'also' ctor arg.
|
|
|
|
|
|
|
|
class Listbox(Widget):
|
2021-06-20 15:46:14 +00:00
|
|
|
ON_MOVE = 1 # Also run whenever the currency moves.
|
|
|
|
ON_LEAVE = 2 # Also run on exit from the control.
|
|
|
|
|
2021-09-12 12:52:57 +00:00
|
|
|
# This is used by dropdown.py
|
2021-06-09 16:11:48 +00:00
|
|
|
@staticmethod
|
2021-09-12 12:52:57 +00:00
|
|
|
def dimensions(writer, elements, dlines):
|
|
|
|
# Height of a single entry in list.
|
2021-06-09 16:11:48 +00:00
|
|
|
entry_height = writer.height + 2 # Allow a pixel above and below text
|
2021-09-12 12:52:57 +00:00
|
|
|
# Number of displayable lines
|
|
|
|
dlines = len(elements) if dlines is None else dlines
|
|
|
|
# Height of control
|
|
|
|
height = entry_height * dlines + 2
|
2021-06-09 16:11:48 +00:00
|
|
|
textwidth = max(writer.stringlen(s) for s in elements) + 4
|
2021-09-12 12:52:57 +00:00
|
|
|
return entry_height, height, dlines, textwidth
|
2021-06-09 16:11:48 +00:00
|
|
|
|
2021-09-12 12:52:57 +00:00
|
|
|
def __init__(self, writer, row, col, *,
|
|
|
|
elements,
|
|
|
|
dlines=None, width=None, value=0,
|
|
|
|
fgcolor=None, bgcolor=None, bdcolor=False,
|
|
|
|
fontcolor=None, select_color=DARKBLUE,
|
2021-06-09 16:11:48 +00:00
|
|
|
callback=dolittle, args=[], also=0):
|
|
|
|
|
2021-07-16 13:27:42 +00:00
|
|
|
e0 = elements[0]
|
|
|
|
# Check whether elements specified as (str, str,...) or ([str, callback, args], [...)
|
|
|
|
if isinstance(e0, tuple) or isinstance(e0, list):
|
|
|
|
self.els = elements # Retain original for .despatch
|
|
|
|
self.elements = [x[0] for x in elements] # Copy text component
|
|
|
|
if callback is not dolittle:
|
|
|
|
raise ValueError('Cannot specify callback.')
|
|
|
|
self.cb = self.despatch
|
|
|
|
else:
|
|
|
|
self.cb = callback
|
|
|
|
self.elements = elements
|
|
|
|
if any(not isinstance(s, str) for s in self.elements):
|
|
|
|
raise ValueError('Invalid elements arg.')
|
2021-09-12 12:52:57 +00:00
|
|
|
# Calculate dimensions
|
|
|
|
self.entry_height, height, self.dlines, tw = self.dimensions(
|
|
|
|
writer, self.elements, dlines)
|
2021-06-09 16:11:48 +00:00
|
|
|
if width is None:
|
2021-09-12 12:52:57 +00:00
|
|
|
width = tw # Text width
|
|
|
|
|
|
|
|
self.also = also
|
|
|
|
self.ntop = 0 # Top visible line
|
|
|
|
if not isinstance(value, int):
|
|
|
|
value = 0 # Or ValueError?
|
|
|
|
elif value >= self.dlines: # Must scroll
|
|
|
|
value = min(value, len(elements) - 1)
|
|
|
|
self.ntop = value - self.dlines + 1
|
2021-06-09 16:11:48 +00:00
|
|
|
super().__init__(writer, row, col, height, width, fgcolor, bgcolor, bdcolor, value, True)
|
|
|
|
self.cb_args = args
|
|
|
|
self.select_color = select_color
|
|
|
|
self.fontcolor = fontcolor
|
|
|
|
self._value = value # No callback until user selects
|
2021-09-12 12:52:57 +00:00
|
|
|
self.ev = value # Value change detection
|
2021-06-09 16:11:48 +00:00
|
|
|
|
|
|
|
def show(self):
|
|
|
|
if not super().show(False): # Clear to self.bgcolor
|
|
|
|
return
|
|
|
|
|
|
|
|
x = self.col
|
|
|
|
y = self.row
|
2021-09-12 12:52:57 +00:00
|
|
|
eh = self.entry_height
|
|
|
|
ntop = self.ntop
|
|
|
|
dlines = self.dlines
|
2021-09-19 11:02:09 +00:00
|
|
|
nlines = min(dlines, len(self.elements)) # Displayable lines
|
|
|
|
for n in range(ntop, ntop + nlines):
|
2021-09-29 13:20:15 +00:00
|
|
|
text = self.elements[n]
|
|
|
|
if self.writer.stringlen(text) > self.width: # Clip
|
|
|
|
font = self.writer.font
|
|
|
|
pos = 0
|
|
|
|
nch = 0
|
|
|
|
for ch in text:
|
|
|
|
pos += font.get_ch(ch)[2] # width of current char
|
|
|
|
if pos > self.width:
|
|
|
|
break
|
|
|
|
nch += 1
|
|
|
|
text = text[: nch]
|
2021-06-09 16:11:48 +00:00
|
|
|
if n == self._value:
|
2021-09-12 12:52:57 +00:00
|
|
|
display.fill_rect(x, y + 1, self.width, eh - 1, self.select_color)
|
2021-09-29 13:20:15 +00:00
|
|
|
display.print_left(self.writer, x + 2, y + 1, text, self.fontcolor, self.select_color)
|
2021-06-09 16:11:48 +00:00
|
|
|
else:
|
2021-09-29 13:20:15 +00:00
|
|
|
display.print_left(self.writer, x + 2, y + 1, text, self.fontcolor, self.bgcolor)
|
2021-09-12 12:52:57 +00:00
|
|
|
y += eh
|
|
|
|
# Draw a vertical line to hint at scrolling
|
|
|
|
x = self.col + self.width - 2
|
|
|
|
if ntop:
|
|
|
|
display.vline(x, self.row, eh - 1, self.fgcolor)
|
|
|
|
if ntop + dlines < len(self.elements):
|
|
|
|
y = self.row + (dlines - 1) * eh
|
|
|
|
display.vline(x, y, eh - 1, self.fgcolor)
|
2021-06-09 16:11:48 +00:00
|
|
|
|
|
|
|
def textvalue(self, text=None): # if no arg return current text
|
|
|
|
if text is None:
|
|
|
|
return self.elements[self._value]
|
|
|
|
else: # set value by text
|
|
|
|
try:
|
|
|
|
v = self.elements.index(text)
|
|
|
|
except ValueError:
|
|
|
|
v = None
|
|
|
|
else:
|
|
|
|
if v != self._value:
|
|
|
|
self.value(v)
|
|
|
|
return v
|
|
|
|
|
2021-09-12 12:52:57 +00:00
|
|
|
def _vchange(self, vnew): # A value change is taking place
|
|
|
|
# Handle scrolling
|
|
|
|
if vnew >= self.ntop + self.dlines:
|
|
|
|
self.ntop = vnew - self.dlines + 1
|
|
|
|
elif vnew < self.ntop:
|
|
|
|
self.ntop = vnew
|
|
|
|
self.value(vnew)
|
|
|
|
if (self.also & Listbox.ON_MOVE): # Treat as if select pressed
|
|
|
|
self.do_sel()
|
|
|
|
|
2021-07-04 17:21:37 +00:00
|
|
|
def do_adj(self, _, val):
|
|
|
|
v = self._value
|
|
|
|
if val > 0:
|
|
|
|
if v:
|
2021-09-12 12:52:57 +00:00
|
|
|
self._vchange(v -1)
|
2021-07-04 17:21:37 +00:00
|
|
|
elif val < 0:
|
|
|
|
if v < len(self.elements) - 1:
|
2021-09-12 12:52:57 +00:00
|
|
|
self._vchange(v + 1)
|
2021-06-09 16:11:48 +00:00
|
|
|
|
|
|
|
# Callback runs if select is pressed. Also (if ON_LEAVE) if user changes
|
|
|
|
# list currency and then moves off the control. Otherwise if we have a
|
|
|
|
# callback that refreshes another control, that second control does not
|
|
|
|
# track currency.
|
|
|
|
def do_sel(self): # Select was pushed
|
|
|
|
self.ev = self._value
|
|
|
|
self.cb(self, *self.cb_args)
|
|
|
|
|
|
|
|
def enter(self):
|
|
|
|
self.ev = self._value # Value change detection
|
|
|
|
|
|
|
|
def leave(self):
|
2021-06-20 15:46:14 +00:00
|
|
|
if (self.also & Listbox.ON_LEAVE) and self._value != self.ev:
|
2021-06-09 16:11:48 +00:00
|
|
|
self.do_sel()
|
2021-07-16 13:27:42 +00:00
|
|
|
|
|
|
|
def despatch(self, _): # Run the callback specified in elements
|
|
|
|
x = self.els[self()]
|
|
|
|
x[1](self, *x[2])
|