370b6c12480cbcd508a44169a267e1c698c1d2dc
[public/pyceo-broken.git] / pylib / csc / apps / urwid / window.py
1 import urwid
2
3 window_stack = []
4 window_names = []
5
6 header = urwid.Text( "" )
7 footer = urwid.Text( "" )
8 top = urwid.Frame( urwid.SolidFill(), header, footer )
9
10 def push_window( frame, name=None ):
11     window_stack.append( frame )
12     window_names.append( name )
13     update_top()
14
15 def pop_window():
16     if len(window_stack) == 1:
17        return False
18     window_stack.pop()
19     window_names.pop()
20     update_top()
21     return True
22
23 def update_top():
24     names = [ n for n in window_names if n ]
25     header.set_text(" - ".join( names ) + "\n")
26     top.set_body( window_stack[-1] )
27
28 def set_status(message):
29     footer.set_text(message)
30
31 def clear_status():
32     footer.set_text("")
33
34 class Abort(Exception):
35     pass
36
37 class Back(Exception):
38     pass
39
40 def raise_abort(*args, **kwargs):
41     raise Abort()
42
43 def raise_back(*args, **kwarg):
44     raise Back()
45
46 def event_loop(ui):
47     while True:
48         try:
49            cols, rows = ui.get_cols_rows()
50            canvas = top.render( (cols, rows), focus=True )
51            ui.draw_screen( (cols, rows), canvas )
52
53            keys = ui.get_input()
54            for k in keys:
55               if k == "esc":
56                  if not pop_window():
57                      break   
58               elif k == "window resize":
59                  (cols, rows) = ui.get_cols_rows()
60               else:
61                  top.keypress( (cols, rows), k )
62         except Back:
63             pop_window()
64         except (Abort, KeyboardInterrupt):
65             return