Add experimental urwid-based GUI
[mspang/pyceo.git] / pylib / csc / apps / urwid / widgets.py
1 import urwid
2
3 class ButtonText(urwid.Text):
4     def __init__(self, callback, *args, **kwargs):
5         self.callback = callback
6         urwid.Text.__init__(self, *args, **kwargs)
7     def selectable(self):
8         return True
9     def keypress(self, size, key):
10         if key == 'enter':
11             self.callback(self.get_text())
12         else:
13             return key
14
15 class SingleEdit(urwid.Edit):
16     def keypress(self, size, key):
17         if key == 'enter':
18             return urwid.Edit.keypress(self, size, 'down')
19         else:
20             return urwid.Edit.keypress(self, size, key)
21
22 class SingleIntEdit(urwid.IntEdit):
23     def keypress(self, size, key):
24         if key == 'enter':
25             return urwid.Edit.keypress(self, size, 'down')
26         else:
27             return urwid.Edit.keypress(self, size, key)
28
29 class WordEdit(SingleEdit):
30     def valid_char(self, ch):
31         return urwid.Edit.valid_char(self, ch) and ch != ' '
32
33 class PassEdit(SingleEdit):
34     def get_text(self):
35         text = urwid.Edit.get_text(self)
36         return (self.caption + " " * len(self.get_edit_text()), text[1])
37
38 class Wizard(urwid.WidgetWrap):
39     def __init__(self):
40         self.selected = None
41         self.panels = []
42
43         self.panelwrap = urwid.WidgetWrap( urwid.SolidFill() )
44         self.back = urwid.Button("Back", self.back)
45         self.next = urwid.Button("Next", self.next)
46         self.buttons = urwid.Columns( [ self.back, self.next ], dividechars=3, focus_column=1 )
47         pad = urwid.Padding( self.buttons, ('fixed right', 2), 19 )
48         self.pile = urwid.Pile( [self.panelwrap, ('flow', pad)], 0 )
49         urwid.WidgetWrap.__init__(self, self.pile)
50
51     def add_panel(self, panel):
52         self.panels.append( panel )
53         if len(self.panels) == 1:
54             self.select(0)
55     
56     def select(self, panelno, set_focus=True):
57         if 0 <= panelno < len(self.panels):
58             self.selected = panelno
59             self.panelwrap.set_w( self.panels[panelno] )
60             self.panels[panelno].activate()
61
62             if set_focus:
63                 if self.panels[panelno].focusable():
64                     self.pile.set_focus( 0 )
65                 else:
66                     self.pile.set_focus( 1 )
67     
68     def next(self, *args, **kwargs):
69         if self.panels[self.selected].check():
70             self.select( self.selected )
71             return
72         self.select(self.selected + 1)
73     
74     def back(self, *args, **kwargs):
75         self.select(self.selected - 1, False)
76
77 class WizardPanel(urwid.WidgetWrap):
78     def __init__(self, state):
79         self.state = state
80         self.init_widgets()
81         self.box = urwid.ListBox( urwid.SimpleListWalker( self.widgets ) )
82         urwid.WidgetWrap.__init__( self, self.box )
83     def init_widgets(self):
84         self.widgets = []
85     def focus_widget(self, widget):
86         self.box.set_focus( self.widgets.index( widget ) )
87     def focusable(self):
88         return True
89     def check(self):
90         return
91     def activate(self):
92         return