Add WindowManager, basic event handling.
authorRadek Brich <radek.brich@devl.cz>
Tue, 18 Mar 2014 22:39:21 +0100
changeset 88 90d00354dc70
parent 87 ee5ea9671f28
child 89 94f5baef19ac
Add WindowManager, basic event handling.
tuikit/core/application.py
tuikit/core/window.py
tuikit/driver/curses.py
--- a/tuikit/core/application.py	Mon Mar 17 23:44:18 2014 +0100
+++ b/tuikit/core/application.py	Tue Mar 18 22:39:21 2014 +0100
@@ -1,4 +1,4 @@
-from tuikit.core.window import Window
+from tuikit.core.window import WindowManager, Window
 
 import logging
 
@@ -17,13 +17,15 @@
         self.log = logging.getLogger('tuikit')
         # Driver
         self.driver = None
-        # root Buffer and Window
+        # Window Manager and root Window
+        self.window_manager = WindowManager()
         self.root_window = Window()
         # flags
         self._started = False
         self._quit = False
         # find and initialize driver
         self.use_driver(driver)
+        self.window_manager.add(self.root_window)
 
     def use_driver(self, driver_name):
         """Select driver to be used for rendering and input.
@@ -49,21 +51,19 @@
     def main_loop(self):
         """The main loop."""
         self._started = True
-        self.root_window.resize(*self.driver.size)
+        self.window_manager.handle_event('resize', *self.driver.size)
 #        timer = self._timer
-        self.root_window.buffer.frame()
 
         while not self._quit:
-            self.root_window.draw(self.driver)
+            self.window_manager.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:])
+            for event in events:
+                self.window_manager.handle_event(event[0], *event[1:])
         self._started = False
         self.log.info('=== quit ===')
 
--- a/tuikit/core/window.py	Mon Mar 17 23:44:18 2014 +0100
+++ b/tuikit/core/window.py	Tue Mar 18 22:39:21 2014 +0100
@@ -1,6 +1,7 @@
 from tuikit.core.buffer import Buffer
 from tuikit.core.signal import Signal
 from tuikit.core.container import Container
+from tuikit.core.coords import Point
 
 
 class Window(Container):
@@ -15,12 +16,20 @@
         """New buffer for the window will be created unless given existing
         `buffer` as parameter."""
         Container.__init__(self)
+        self.pos = Point()
         self._buffer = None
-        self._size = None
         self.sig_resized = Signal()
         self.buffer = buffer or Buffer()
 
     @property
+    def x(self):
+        return self.pos.x
+
+    @property
+    def y(self):
+        return self.pos.y
+
+    @property
     def buffer(self):
         return self._buffer
 
@@ -31,19 +40,49 @@
             self.sig_resized.disconnect(self._buffer.resize)
         # replace the buffer
         self._buffer = buffer
-        self._size = buffer.size
         # resize buffer when window gets resized
         self.sig_resized.connect(buffer.resize)
 
     @property
     def size(self):
-        return self._size.readonly()
+        return self.buffer.size.readonly()
 
     def resize(self, w, h):
-        self._buffer.resize(w, h)
+        self.buffer.resize(w, h)
         self.sig_resized(w, h)
+        self.redraw()
+
+    def redraw(self):
+        Container.draw(self, self.buffer)
+        self.buffer.puts(10, 5, '{0.w} {0.h}'.format(self.size))
+        self.buffer.frame()
 
     def draw(self, buffer, x=0, y=0):
-        """Draw this window into buffer at x, y coords."""
-        Container.draw(self, self.buffer)
+        """Draw this window into `buffer` at x/y coords."""
         buffer.draw(self.buffer, x, y)
+
+
+class WindowManager:
+
+    def __init__(self):
+        self.windows = []
+
+    def add(self, window):
+        self.windows.append(window)
+
+    def resize(self, w, h):
+        self.windows[0].resize(w, h)
+
+    def draw(self, buffer, x=0, y=0):
+        for window in self.windows:
+            window.draw(buffer,
+                        x + window.x,
+                        y + window.y)
+
+    def handle_event(self, event_name, *args):
+        """Handle input event to managed windows."""
+        handler = getattr(self, event_name, None)
+        if handler:
+            handler(*args)
+        else:
+            raise Exception('Unknown event: %r %r' % (event_name, args))
--- a/tuikit/driver/curses.py	Mon Mar 17 23:44:18 2014 +0100
+++ b/tuikit/driver/curses.py	Tue Mar 18 22:39:21 2014 +0100
@@ -215,7 +215,7 @@
 
             elif c == curses.KEY_RESIZE:
                 self.size.h, self.size.w = self.stdscr.getmaxyx()
-                res.append(('resize',))
+                res.append(('resize', self.size.w, self.size.h))
 
             elif curses.ascii.isctrl(c):
                 self._inputqueue_unget(c)