author | Radek Brich <radek.brich@devl.cz> |
Sun, 22 Feb 2015 09:53:13 +0100 | |
changeset 119 | dd91747504dd |
parent 117 | 8680c2333546 |
permissions | -rw-r--r-- |
88
90d00354dc70
Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
1 |
from tuikit.core.window import WindowManager, Window |
89 | 2 |
from tuikit.core.theme import default_theme |
90
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
3 |
from tuikit.core.timer import Timer |
91
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
90
diff
changeset
|
4 |
from tuikit.core.buffer import ProxyBuffer |
111 | 5 |
from tuikit.core.events import ResizeEvent |
86
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
6 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
7 |
import logging |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
8 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
9 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
10 |
class Application: |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
11 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
12 |
"""Tuikit application helper. |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
13 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
14 |
Joins all required pieces to build complete application |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
15 |
using TUI widgets and event loop. |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
16 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
17 |
""" |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
18 |
|
110
cf3d49cdd6e2
Add cursesw driver, using curses get_wch() for unicode input. It alse has enabled keypad() to let curses interpret control keys and mouse input.
Radek Brich <radek.brich@devl.cz>
parents:
97
diff
changeset
|
19 |
def __init__(self, driver='cursesw'): |
92
b97c4e25ed6d
Configure logging for demos.
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
20 |
self.log = logging.getLogger(__name__) |
86
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
21 |
self.driver = None |
90
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
22 |
self.timer = Timer() |
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
23 |
self.window_manager = WindowManager(timer=self.timer) |
86
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
24 |
self.root_window = Window() |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
25 |
# flags |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
26 |
self._started = False |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
27 |
self._quit = False |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
28 |
# find and initialize driver |
89 | 29 |
self.set_driver(driver) |
30 |
self.set_theme(default_theme) |
|
88
90d00354dc70
Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
31 |
self.window_manager.add(self.root_window) |
117
8680c2333546
Update FixedLayout. Add demo launcher.
Radek Brich <radek.brich@devl.cz>
parents:
112
diff
changeset
|
32 |
# Convenience method |
8680c2333546
Update FixedLayout. Add demo launcher.
Radek Brich <radek.brich@devl.cz>
parents:
112
diff
changeset
|
33 |
self.add = self.root_window.add |
86
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
34 |
|
89 | 35 |
def set_driver(self, driver_name): |
86
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
36 |
"""Select driver to be used for rendering and input. |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
37 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
38 |
`driver_name` should be one of: 'base', 'curses', 'sdl' |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
39 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
40 |
""" |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
41 |
if self._started: |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
42 |
raise Exception('Cannot change driver after starting the application.') |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
43 |
module = __import__('tuikit.driver.' + driver_name, fromlist=['driver_class']) |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
44 |
self.driver = module.driver_class() |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
45 |
|
89 | 46 |
def set_theme(self, theme): |
47 |
self.window_manager.set_theme(theme) |
|
48 |
||
86
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
49 |
def start(self): |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
50 |
"""Start application. Runs main loop.""" |
92
b97c4e25ed6d
Configure logging for demos.
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
51 |
self.log.info('=== Start ===') |
86
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
52 |
with self.driver: |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
53 |
self.main_loop() |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
54 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
55 |
def stop(self): |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
56 |
"""Terminate application.""" |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
57 |
self._quit = True |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
58 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
59 |
def main_loop(self): |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
60 |
"""The main loop.""" |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
61 |
self._started = True |
111 | 62 |
self.window_manager.handle_event(ResizeEvent(*self.driver.size)) |
86
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
63 |
|
91
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
90
diff
changeset
|
64 |
screen = ProxyBuffer(self.driver) |
86
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
65 |
while not self._quit: |
119
dd91747504dd
Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents:
117
diff
changeset
|
66 |
self.window_manager.draw_if_needed(screen) |
112 | 67 |
self.driver.cursor = self.window_manager.get_cursor_if_visible() |
86
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
68 |
self.driver.flush() |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
69 |
|
90
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
70 |
timeout = self.timer.nearest_timeout() |
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
71 |
events = self.driver.getevents(timeout) |
86
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
72 |
|
90
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
73 |
self.timer.process_timeouts() |
88
90d00354dc70
Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
74 |
for event in events: |
111 | 75 |
self.window_manager.handle_event(event) |
90
781774a8d568
Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents:
89
diff
changeset
|
76 |
|
86
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
77 |
self._started = False |
92
b97c4e25ed6d
Configure logging for demos.
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
78 |
self.log.info('=== End ===') |
86
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
79 |