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