author | Radek Brich <radek.brich@devl.cz> |
Tue, 08 Jan 2013 23:59:55 +0100 | |
changeset 59 | 729fcdfe6b57 |
parent 57 | 911927edbdde |
child 62 | 2f61931520c9 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
2 |
||
3 |
import logging |
|
4 |
import time |
|
5 |
||
18
e6c3a5ee91aa
Eliminate relative imports.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
6 |
from tuikit.container import Container |
0 | 7 |
|
8 |
||
59 | 9 |
class Timer: |
10 |
def __init__(self): |
|
11 |
self.timeouts = [] |
|
12 |
self.timelast = None |
|
13 |
||
14 |
def add_timeout(self, delay, callback): |
|
15 |
"""Register callback to be called after delay seconds. |
|
16 |
||
17 |
delay -- in seconds, float |
|
18 |
callback -- function to be called with no parameters |
|
19 |
||
20 |
""" |
|
21 |
if not len(self.timeouts): |
|
22 |
self.timelast = time.time() |
|
23 |
self.timeouts += [[delay, callback]] |
|
24 |
||
25 |
def remove_timeout(self, callback): |
|
26 |
"""Unregister callback previously registered with add_timeout.""" |
|
27 |
for timeout in self.timeouts[:]: |
|
28 |
if timeout[1] == callback: |
|
29 |
self.timeouts.remove(timeout) |
|
30 |
||
31 |
def has_timeout(self): |
|
32 |
return len(self.timeouts) > 0 |
|
33 |
||
34 |
def nearest_timeout(self): |
|
35 |
if not self.has_timeout(): |
|
36 |
return None |
|
37 |
return min(self.timeouts)[0] |
|
38 |
||
39 |
def process_timeouts(self): |
|
40 |
if not self.has_timeout(): |
|
41 |
return |
|
42 |
now = time.time() |
|
43 |
lasted = now - self.timelast |
|
44 |
self.timelast = now |
|
45 |
for timeout in self.timeouts[:]: |
|
46 |
adjusted_delay = timeout[0] - lasted |
|
47 |
if adjusted_delay <= 0.0: |
|
48 |
timeout[1]() |
|
49 |
self.timeouts.remove(timeout) |
|
50 |
else: |
|
51 |
timeout[0] = adjusted_delay |
|
52 |
||
53 |
||
0 | 54 |
class TopWindow(Container): |
32
088b92ffb119
Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
55 |
|
13
19ebde2fd594
Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
56 |
'''Top window of an application. Covers entire screen.''' |
32
088b92ffb119
Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
57 |
|
0 | 58 |
def __init__(self): |
13
19ebde2fd594
Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
59 |
'''Create top window.''' |
0 | 60 |
Container.__init__(self) |
61 |
self.top = self |
|
59 | 62 |
self.timer = Timer() |
0 | 63 |
|
46
2b43a7f38c34
Minor updates. Replace super() with direct class reference. Add demo_colors.
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
64 |
def draw(self, driver, x=0, y=0): |
36 | 65 |
"""Draw the top window and its children. |
0 | 66 |
|
36 | 67 |
This is entry point for full screen redraw. |
68 |
||
69 |
""" |
|
46
2b43a7f38c34
Minor updates. Replace super() with direct class reference. Add demo_colors.
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
70 |
driver.erase() |
2b43a7f38c34
Minor updates. Replace super() with direct class reference. Add demo_colors.
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
71 |
Container.draw(self, driver, x, y) |
0 | 72 |
|
73 |
||
74 |
class Application: |
|
32
088b92ffb119
Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
75 |
|
13
19ebde2fd594
Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
76 |
'''Application class. Defines main loop.''' |
32
088b92ffb119
Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
77 |
|
49
1611c462c3e3
Update sdlterm: Optimize commit() - use two cell buffers, redraw only dirty cells, not everything. Fix keypress event, filter mousemove.
Radek Brich <radek.brich@devl.cz>
parents:
46
diff
changeset
|
78 |
def __init__(self, driver = 'sdl'): |
13
19ebde2fd594
Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
79 |
'''Create application.''' |
32
088b92ffb119
Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
80 |
|
23
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
81 |
self.top = TopWindow() |
13
19ebde2fd594
Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
82 |
'''Top window.''' |
32
088b92ffb119
Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
83 |
|
0 | 84 |
self.quit = False |
32
088b92ffb119
Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
85 |
|
29
c0cdef06fd16
Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
86 |
self.driver = self.get_driver_instance(driver) |
c0cdef06fd16
Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
87 |
'''Driver class instance (render + input), e.g. DriverCurses.''' |
32
088b92ffb119
Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
88 |
|
0 | 89 |
self.log = logging.getLogger('tuikit') |
90 |
self.log.setLevel(logging.DEBUG) |
|
91 |
handler = logging.FileHandler('./tuikit.log') |
|
15 | 92 |
formatter = logging.Formatter('%(asctime)s %(levelname)-5s %(message)s', '%y-%m-%d %H:%M:%S') |
93 |
handler.setFormatter(formatter) |
|
0 | 94 |
self.log.addHandler(handler) |
15 | 95 |
self.log.info('=== start ===') |
0 | 96 |
|
97 |
def start(self): |
|
13
19ebde2fd594
Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
98 |
'''Start application. Runs main loop.''' |
23
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
99 |
self.driver.start(self.mainloop) |
0 | 100 |
|
101 |
def terminate(self): |
|
13
19ebde2fd594
Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
102 |
'''Terminate application.''' |
0 | 103 |
self.quit = True |
104 |
||
23
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
105 |
def mainloop(self): |
13
19ebde2fd594
Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
106 |
'''The main loop.''' |
7
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
1
diff
changeset
|
107 |
self.applytheme() |
32
088b92ffb119
Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
108 |
self.top.size = self.driver.size # link top widget size to screen size |
34
e3beacd5e536
Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
109 |
self.top.emit('resize') |
59 | 110 |
timer = self.top.timer |
0 | 111 |
|
112 |
while True: |
|
23
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
113 |
self.top.draw(self.driver) |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
114 |
self.driver.commit() |
0 | 115 |
|
59 | 116 |
timeout = timer.nearest_timeout() |
24
b248ef500557
Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
117 |
events = self.driver.getevents(timeout) |
59 | 118 |
timer.process_timeouts() |
0 | 119 |
|
120 |
for event in events: |
|
24
b248ef500557
Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
121 |
if event[0] == 'quit': |
b248ef500557
Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
122 |
self.quit = True |
b248ef500557
Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
123 |
else: |
b248ef500557
Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
124 |
self.top.emit(event[0], *event[1:]) |
0 | 125 |
|
126 |
if self.quit: |
|
127 |
break |
|
128 |
||
7
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
1
diff
changeset
|
129 |
|
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
1
diff
changeset
|
130 |
def applytheme(self): |
52
50a1857557da
Update SDL driver: Enlarge char, attr to 32 bits, 64 bits per terminal cell. Colors and attributes are complete, only blink does not work.
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
131 |
#TODO: allow custom colors: |
50a1857557da
Update SDL driver: Enlarge char, attr to 32 bits, 64 bits per terminal cell. Colors and attributes are complete, only blink does not work.
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
132 |
# e.g. "blue (#2020FF) on black (#101010), underline" |
50a1857557da
Update SDL driver: Enlarge char, attr to 32 bits, 64 bits per terminal cell. Colors and attributes are complete, only blink does not work.
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
133 |
# color in brackets is used when driver supports custom colors |
23
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
134 |
driver = self.driver |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
135 |
driver.setcolor('normal', 'white on black') |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
136 |
driver.setcolor('strong', 'white on black, bold') |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
137 |
driver.setcolor('active', 'black on cyan') |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
138 |
driver.setcolor('window:normal', 'white on blue') |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
139 |
driver.setcolor('window:controls', 'white on blue, bold') |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
140 |
driver.setcolor('window:controls-active', 'cyan on blue, bold') |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
141 |
driver.setcolor('button', 'black on white') |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
142 |
driver.setcolor('button-active', 'black on cyan') |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
143 |
driver.setcolor('menu', 'black on cyan') |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
144 |
driver.setcolor('menu-active', 'white on cyan, bold') |
30
05500124d7fb
Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
145 |
driver.setcolor('combo:normal', 'black on green') |
13
19ebde2fd594
Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
146 |
|
29
c0cdef06fd16
Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
147 |
|
c0cdef06fd16
Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
148 |
def get_driver_instance(self, name): |
c0cdef06fd16
Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
149 |
module = __import__('tuikit.driver_' + name, fromlist=['driverclass']) |
c0cdef06fd16
Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
150 |
return module.driverclass() |
c0cdef06fd16
Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
151 |