Reorganize namespace
[mspang/pyceo.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 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     clear_status()
22     return True
23
24 def update_top():
25     names = [ n for n in window_names if n ]
26     header.set_text(" - ".join( names ) + "\n")
27     top.set_body( window_stack[-1] )
28
29 def set_status(message):
30     footer.set_text(message)
31
32 def clear_status():
33     footer.set_text("")
34
35 class Abort(Exception):
36     pass
37
38 class Back(Exception):
39     pass
40
41 def raise_abort(*args, **kwargs):
42     raise Abort()
43
44 def raise_back(*args, **kwarg):
45     raise Back()
46
47 def event_loop(ui):
48     while True:
49         try:
50            cols, rows = ui.get_cols_rows()
51            canvas = top.render( (cols, rows), focus=True )
52            ui.draw_screen( (cols, rows), canvas )
53
54            keys = ui.get_input()
55            for k in keys:
56               if k == "esc":
57                  if not pop_window():
58                      break
59               elif k == "window resize":
60                  (cols, rows) = ui.get_cols_rows()
61               else:
62                  top.keypress( (cols, rows), k )
63         except Back:
64             pop_window()
65         except (Abort, KeyboardInterrupt):
66             return