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