From: ewt Date: Thu, 29 Apr 1999 19:05:47 +0000 (+0000) Subject: added piles of convienence functions X-Git-Tag: r0-50~48 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8eb321ee8116208b4e975bc65c6dcadd8a1a010f;p=thirdparty%2Fnewt.git added piles of convienence functions --- diff --git a/popcorn.py b/popcorn.py index 6827142..32ca07e 100755 --- a/popcorn.py +++ b/popcorn.py @@ -2,7 +2,7 @@ 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") @@ -22,6 +22,13 @@ e.setCallback(lambda: sys.exit(1)) 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) @@ -56,6 +63,7 @@ res = f.run() screen.popWindow() + screen.finish() print "val", e.value() @@ -64,3 +72,6 @@ print "r1", r1.selected() 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 diff --git a/snack.py b/snack.py index 0468328..ee1520e 100644 --- a/snack.py +++ b/snack.py @@ -5,6 +5,8 @@ # the literal classes and make them more object-oriented. import _snack +import types +import string class Widget: @@ -84,6 +86,12 @@ class Textbox(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): @@ -218,7 +226,6 @@ class SnackScreen: def reflow(text, width, flexDown = 5, flexUp = 5): return _snack.reflow(text, width, flexDown, flexUp) - # combo widgets class RadioGroup(Widget): @@ -270,11 +277,15 @@ class ButtonBar(Grid): 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)) @@ -313,7 +324,7 @@ class GridForm(Grid): growx, growy); self.childList.append(widget) - def run_once(self): + def runOnce(self): result = self.run() self.screen.popWindow() return result @@ -327,7 +338,7 @@ class GridForm(Grid): 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: @@ -337,3 +348,83 @@ class GridForm(Grid): 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)) +