from snack import *
-t = Textbox(25, 1, "Some text")
+t = TextboxReflowed(25, "Some text which needs to be wrapped at a good place.")
li = Listbox(5, width = 20, returnExit = 1)
li.append("First", "f")
li.append("Second", "s")
screen = SnackScreen()
+foo = EntryWindow(screen, 'Title', 'This is some text for the entry window',
+ ['prompt', 'more', 'info'])
+
+lbcw = ListboxChoiceWindow(screen, 'Title 2',
+ 'Choose one item from the list below:',
+ ('One', 'Two', 'Three', 'Four', 'Five'))
+
sg = Grid(2, 3)
sg.setField(b, 0, 0, anchorLeft = 1)
sg.setField(e, 1, 0, (1, 0, 0, 0), anchorLeft = 1, anchorTop = 1)
screen.popWindow()
+
screen.finish()
print "val", e.value()
print "listbox", li.current()
# returns a tuple of the wrapped text, the actual width, and the actual height
print res
+
+print foo
+print 'lbcw', lbcw
# the literal classes and make them more object-oriented.
import _snack
+import types
+import string
class Widget:
def __init__(self, width, height, text, scroll = 0):
self.w = _snack.textbox(width, height, text, scroll)
+class TextboxReflowed(Textbox):
+
+ def __init__(self, width, text, flexDown = 5, flexUp = 10):
+ (newtext, width, height) = reflow(text, width, flexDown, flexUp)
+ Textbox.__init__(self, width, height, newtext, 0)
+
class Label(Widget):
def __init__(self, text):
def reflow(text, width, flexDown = 5, flexUp = 5):
return _snack.reflow(text, width, flexDown, flexUp)
-
# combo widgets
class RadioGroup(Widget):
self.item = 0
Grid.__init__(self, len(buttonlist), 1)
for blist in buttonlist:
- if len(blist) == 2:
+ if (type(blist) == types.StringType):
+ title = blist
+ value = string.lower(blist)
+ elif len(blist) == 2:
(title, value) = blist
else:
(title, value, hotkey) = blist
self.hotkeys[hotkey] = value
+
b = Button(title)
self.list.append(b, value)
self.setField(b, self.item, 0, (1, 0, 1, 0))
growx, growy);
self.childList.append(widget)
- def run_once(self):
+ def runOnce(self):
result = self.run()
self.screen.popWindow()
return result
self.form_created = 1
return self.form.run()
- def run_popup(self):
+ def runPopup(self):
if not self.form_created:
self.place(1,1)
for child in self.childList:
result = self.form.run()
self.screen.popWindow()
return result
+
+def ListboxChoiceWindow(screen, title, text, items,
+ buttons = ('Ok', 'Cancel'),
+ width = 40, scroll = 0, height = -1):
+ if (height == -1): height = len(items)
+
+ bb = ButtonBar(screen, buttons)
+ t = TextboxReflowed(width, text)
+ l = Listbox(height, scroll = scroll, returnExit = 1)
+ count = 0
+ for item in items:
+ if (type(item) == types.TupleType):
+ (text, key) = item
+ else:
+ text = item
+ key = count
+
+ l.append(text, key)
+ count = count + 1
+
+ g = GridForm(screen, title, 1, 3)
+ g.add(t, 0, 0)
+ g.add(l, 0, 1, padding = (0, 1, 0, 1))
+ g.add(bb, 0, 2, growx = 1)
+
+ rc = g.runOnce()
+
+ return (bb.buttonPressed(rc), l.current())
+
+def ButtonChoiceWindow(screen, title, text,
+ buttons = [ 'Ok', 'Cancel' ],
+ width = 40):
+ bb = ButtonBar(screen, buttons)
+ t = TextboxReflowed(width, text)
+
+ g = GridForm(screen, title, 1, 2)
+ g.add(t, 0, 0, padding = (0, 0, 0, 1))
+ g.add(bb, 0, 1, growx = 1)
+ return bb.buttonPressed(g.runOnce())
+
+def EntryWindow(screen, title, text, prompts, allowCancel = 1, width = 40,
+ entryWidth = 20):
+ bb = ButtonBar(screen, [ 'Ok', 'Cancel' ]);
+ t = TextboxReflowed(width, text)
+
+ count = 0
+ for n in prompts:
+ count = count + 1
+
+ sg = Grid(2, count)
+
+ count = 0
+ entryList = []
+ for n in prompts:
+ if (type(n) == types.TupleType):
+ (n, e) = n
+ else:
+ e = Entry(entryWidth)
+
+ sg.setField(Label(n), 0, count, padding = (0, 0, 1, 0), anchorLeft = 1)
+ sg.setField(e, 1, count, anchorLeft = 1)
+ count = count + 1
+ entryList.append(e)
+
+ g = GridForm(screen, title, 1, 3)
+
+ g.add(t, 0, 0, padding = (0, 0, 0, 1))
+ g.add(sg, 0, 1, padding = (0, 0, 0, 1))
+ g.add(bb, 0, 2, growx = 1)
+
+ result = g.runOnce()
+
+ entryValues = []
+ count = 0
+ for n in prompts:
+ entryValues.append(entryList[count].value())
+ count = count + 1
+
+ return (bb.buttonPressed(result), tuple(entryValues))
+