tuikit/core/application.py
changeset 86 0978fb755d31
child 88 90d00354dc70
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tuikit/core/application.py	Mon Mar 17 20:40:04 2014 +0100
@@ -0,0 +1,69 @@
+from tuikit.core.window import Window
+
+import logging
+
+
+class Application:
+
+    """Tuikit application helper.
+
+    Joins all required pieces to build complete application
+    using TUI widgets and event loop.
+
+    """
+
+    def __init__(self, driver='curses'):
+        # logger
+        self.log = logging.getLogger('tuikit')
+        # Driver
+        self.driver = None
+        # root Buffer and Window
+        self.root_window = Window()
+        # flags
+        self._started = False
+        self._quit = False
+        # find and initialize driver
+        self.use_driver(driver)
+
+    def use_driver(self, driver_name):
+        """Select driver to be used for rendering and input.
+
+        `driver_name` should be one of: 'base', 'curses', 'sdl'
+
+        """
+        if self._started:
+            raise Exception('Cannot change driver after starting the application.')
+        module = __import__('tuikit.driver.' + driver_name, fromlist=['driver_class'])
+        self.driver = module.driver_class()
+
+    def start(self):
+        """Start application. Runs main loop."""
+        self.log.info('=== start ===')
+        with self.driver:
+            self.main_loop()
+
+    def stop(self):
+        """Terminate application."""
+        self._quit = True
+
+    def main_loop(self):
+        """The main loop."""
+        self._started = True
+        self.root_window.resize(*self.driver.size)
+#        timer = self._timer
+        self.root_window.buffer.frame()
+
+        while not self._quit:
+            self.root_window.draw(self.driver)
+            self.driver.flush()
+
+            #timeout = timer.nearest_timeout()
+            events = self.driver.getevents()#timeout)
+            self._quit = True
+            #timer.process_timeouts()
+
+#            for event in events:
+ #               self._top.emit(event[0], *event[1:])
+        self._started = False
+        self.log.info('=== quit ===')
+