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