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