Remove chfn and chsh and allow shell changes in the gui
[public/pyceo-broken.git] / ceo / urwid / widgets.py
1 import urwid
2 from ceo.urwid.ldapfilter import *
3 from ceo.urwid.window import raise_back, push_window
4
5 def menu_items(items):
6     return [ urwid.AttrWrap( ButtonText( cb, data, txt ), 'menu', 'selected') for (txt, cb, data) in items ]
7
8 def push_wizard(name, pages, dimensions=(50, 10)):
9     state = {}
10     wiz = Wizard()
11     for page in pages:
12         if type(page) != tuple:
13             page = (page, )
14         wiz.add_panel( page[0](state, *page[1:]) )
15     push_window( urwid.Filler( urwid.Padding(
16         urwid.LineBox(wiz), 'center', dimensions[0]),
17         'middle', dimensions[1] ), name )
18
19 class ButtonText(urwid.Text):
20     def __init__(self, callback, data, *args, **kwargs):
21         self.callback = callback
22         self.data = data
23         urwid.Text.__init__(self, *args, **kwargs)
24     def selectable(self):
25         return True
26     def keypress(self, size, key):
27         if key == 'enter' and self.callback:
28             self.callback(self.data)
29         else:
30             return key
31
32 class SingleEdit(urwid.Edit):
33     def keypress(self, size, key):
34         if key == 'enter':
35             return urwid.Edit.keypress(self, size, 'down')
36         else:
37             return urwid.Edit.keypress(self, size, key)
38
39 class SingleIntEdit(urwid.IntEdit):
40     def keypress(self, size, key):
41         if key == 'enter':
42             return urwid.Edit.keypress(self, size, 'down')
43         else:
44             return urwid.Edit.keypress(self, size, key)
45
46 class WordEdit(SingleEdit):
47     def valid_char(self, ch):
48         return urwid.Edit.valid_char(self, ch) and ch != ' '
49
50 class LdapFilterWordEdit(LdapFilter, WordEdit):
51     def __init__(self, *args):
52         LdapFilter.__init__(self, WordEdit)
53         WordEdit.__init__(self, *args)
54
55 class PassEdit(SingleEdit):
56     def get_text(self):
57         text = urwid.Edit.get_text(self)
58         return (self.caption + " " * len(self.get_edit_text()), text[1])
59
60 class Wizard(urwid.WidgetWrap):
61     def __init__(self):
62         self.selected = None
63         self.panels = []
64
65         self.panelwrap = urwid.WidgetWrap( urwid.SolidFill() )
66         self.back = urwid.Button("Back", self.back)
67         self.next = urwid.Button("Next", self.next)
68         self.buttons = urwid.Columns( [ self.back, self.next ], dividechars=3, focus_column=1 )
69         pad = urwid.Padding( self.buttons, ('fixed right', 2), 19 )
70         self.pile = urwid.Pile( [self.panelwrap, ('flow', pad)], 0 )
71         urwid.WidgetWrap.__init__(self, self.pile)
72
73     def add_panel(self, panel):
74         self.panels.append( panel )
75         if len(self.panels) == 1:
76             self.select(0)
77
78     def select(self, panelno, set_focus=True):
79         if 0 <= panelno < len(self.panels):
80             self.selected = panelno
81             self.panelwrap.set_w( self.panels[panelno] )
82             self.panels[panelno].activate()
83
84             if set_focus:
85                 if self.panels[panelno].focusable():
86                     self.pile.set_focus( 0 )
87                 else:
88                     self.pile.set_focus( 1 )
89
90     def next(self, *args, **kwargs):
91         if self.panels[self.selected].check():
92             self.select( self.selected )
93             return
94         self.select(self.selected + 1)
95
96     def back(self, *args, **kwargs):
97         if self.selected == 0:
98             raise_back()
99         self.select(self.selected - 1, False)
100
101 class WizardPanel(urwid.WidgetWrap):
102     def __init__(self, state):
103         self.state = state
104         self.init_widgets()
105         self.box = urwid.ListBox( urwid.SimpleListWalker( self.widgets ) )
106         urwid.WidgetWrap.__init__( self, self.box )
107     def init_widgets(self):
108         self.widgets = []
109     def focus_widget(self, widget):
110         self.box.set_focus( self.widgets.index( widget ) )
111     def focusable(self):
112         return True
113     def check(self):
114         return
115     def activate(self):
116         return