2021-06-09 16:11:48 +00:00
|
|
|
# dialog.py Extension to ugui providing the DialogBox class
|
|
|
|
|
|
|
|
# Released under the MIT License (MIT). See LICENSE.
|
|
|
|
# Copyright (c) 2021 Peter Hinch
|
|
|
|
|
|
|
|
from gui.core.ugui import display, Window, Screen
|
|
|
|
from gui.core.colors import *
|
|
|
|
from gui.widgets.label import Label
|
|
|
|
from gui.widgets.buttons import Button, CloseButton
|
|
|
|
|
|
|
|
dolittle = lambda *_ : None
|
|
|
|
|
|
|
|
class DialogBox(Window):
|
|
|
|
def __init__(self, writer, row=20, col=20, *, elements, label=None,
|
|
|
|
bgcolor=DARKGREEN, buttonwidth=25, closebutton=True, callback=dolittle, args=[]):
|
|
|
|
|
|
|
|
def back(button, text): # Callback for normal buttons
|
|
|
|
Window.value(text)
|
|
|
|
callback(Window, *args)
|
|
|
|
Screen.back()
|
|
|
|
|
|
|
|
def backbutton(button, text):
|
|
|
|
Window.value(text)
|
|
|
|
callback(Window, *args)
|
|
|
|
|
|
|
|
height = 80
|
|
|
|
spacing = 10
|
2021-06-24 08:58:57 +00:00
|
|
|
buttonwidth = max(max(writer.stringlen(e[0]) for e in elements) + 14, buttonwidth)
|
2021-06-09 16:11:48 +00:00
|
|
|
buttonheight = max(writer.height, 15)
|
|
|
|
nelements = len(elements)
|
|
|
|
width = spacing + (buttonwidth + spacing) * nelements
|
|
|
|
if label is not None:
|
|
|
|
width = max(width, writer.stringlen(label) + 2 * spacing)
|
|
|
|
super().__init__(row, col, height, width, bgcolor = bgcolor)
|
|
|
|
|
|
|
|
col = spacing # Coordinates relative to window
|
|
|
|
row = self.height - buttonheight - 10
|
|
|
|
gap = 0
|
|
|
|
if nelements > 1:
|
|
|
|
gap = ((width - 2 * spacing) - nelements * buttonwidth) // (nelements - 1)
|
|
|
|
if label is not None:
|
|
|
|
r, c = self.locn(10, col)
|
|
|
|
Label(writer, r, c, label, bgcolor = bgcolor)
|
|
|
|
for text, color in elements:
|
|
|
|
Button(writer, *self.locn(row, col), height = buttonheight, width = buttonwidth,
|
|
|
|
textcolor = BLACK, bgcolor = color,
|
2021-06-22 17:46:28 +00:00
|
|
|
fgcolor = color, bdcolor = color,
|
2021-06-09 16:11:48 +00:00
|
|
|
text = text, shape = RECTANGLE,
|
|
|
|
callback = back, args = (text,))
|
|
|
|
col += buttonwidth + gap
|
|
|
|
|
|
|
|
if closebutton:
|
|
|
|
CloseButton(writer, callback = backbutton, args = ('Close',))
|