Re-build so debian.csclub include library as a dependency
[public/pyceo-broken.git] / ceo / urwid / window.py
1 import urwid
2
3 window_stack = []
4 window_names = []
5
6 header = urwid.Text( "" )
7 footer = urwid.Text( "" )
8
9 ui = urwid.curses_display.Screen()
10
11 ui.register_palette([
12     # name, foreground, background, mono
13     ('banner', 'light gray', 'default', None),
14     ('menu', 'light gray', 'default', 'bold'),
15     ('selected', 'black', 'light gray', 'bold'),
16 ])
17
18 top = urwid.Frame( urwid.SolidFill(), header, footer )
19
20 def push_window( frame, name=None ):
21     window_stack.append( frame )
22     window_names.append( name )
23     update_top()
24
25 def pop_window():
26     if len(window_stack) == 1:
27        return False
28     window_stack.pop()
29     window_names.pop()
30     update_top()
31     clear_status()
32     return True
33
34 def update_top():
35     names = [ n for n in window_names if n ]
36     header.set_text(" - ".join( names ) + "\n")
37     top.set_body( window_stack[-1] )
38
39 def set_status(message):
40     footer.set_text(message)
41
42 def clear_status():
43     footer.set_text("")
44
45 class Abort(Exception):
46     pass
47
48 class Back(Exception):
49     pass
50
51 def raise_abort(*args, **kwargs):
52     raise Abort()
53
54 def raise_back(*args, **kwarg):
55     raise Back()
56
57 def redraw():
58    cols, rows = ui.get_cols_rows()
59    canvas = top.render( (cols, rows), focus=True )
60    ui.draw_screen( (cols, rows), canvas )
61    return cols, rows
62
63 def event_loop(ui):
64     while True:
65         try:
66            cols, rows = redraw()
67
68            keys = ui.get_input()
69            for k in keys:
70               if k == "esc":
71                  if not pop_window():
72                      break
73               elif k == "window resize":
74                  (cols, rows) = ui.get_cols_rows()
75               else:
76                  top.keypress( (cols, rows), k )
77         except Back:
78             pop_window()
79         except (Abort, KeyboardInterrupt):
80             return