Drop AnchorLayout, merge its features into FixedLayout.
authorRadek Brich <radek.brich@devl.cz>
Sun, 15 Feb 2015 17:50:24 +0100
changeset 116 165b5d65e1cb
parent 115 b4ff7392003a
child 117 8680c2333546
Drop AnchorLayout, merge its features into FixedLayout.
.hgignore
INSPIRATION
tuikit/core/container.py
tuikit/layouts/anchor.py
tuikit/layouts/fixed.py
tuikit/layouts/layout.py
--- a/.hgignore	Sun Feb 15 12:52:46 2015 +0100
+++ b/.hgignore	Sun Feb 15 17:50:24 2015 +0100
@@ -8,6 +8,7 @@
 ^sdlterm/font
 ^sdlterm/cython/sdlterm.cpp$
 ^sdlterm/test_
+^sdlterm\..*\.so$
 ^(.*/)?\.c?project$
 ^(.*/)?\.pydevproject$
 ^\.settings
--- a/INSPIRATION	Sun Feb 15 12:52:46 2015 +0100
+++ b/INSPIRATION	Sun Feb 15 17:50:24 2015 +0100
@@ -1,3 +1,6 @@
+Ncurses
+  http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/mouse.html
+
 Wikipedia:
   http://en.wikipedia.org/wiki/Event_loop
 
--- a/tuikit/core/container.py	Sun Feb 15 12:52:46 2015 +0100
+++ b/tuikit/core/container.py	Sun Feb 15 17:50:24 2015 +0100
@@ -23,13 +23,13 @@
         self.trap_focus = False
         self.layout = layout_class()
 
-    def add(self, widget):
+    def add(self, widget, *args, **kwargs):
         """Add widget into container."""
         self._widgets.append(widget)
         widget.parent = self
         widget.window = self.window
         widget.set_theme(self.theme)
-        self.layout.add(widget)
+        self.layout.add(widget, *args, **kwargs)
         if self.focus_widget is None and widget.can_focus():
             self.focus_widget = widget
 
--- a/tuikit/layouts/anchor.py	Sun Feb 15 12:52:46 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,83 +0,0 @@
-from .layout import Layout
-
-
-class AnchorLayout(Layout):
-
-    """Anchor widgets to borders of container."""
-
-    def __init__(self):
-        Layout.__init__(self)
-        self.register_hints(
-            'halign', make_select('left', 'right', 'fill', 'center'),
-            'valign', make_select('top', 'bottom', 'fill', 'center'),
-            'margin', Borders,
-            )
-
-    def on_resize(self, ev):
-        for child in self._get_children():
-            reqw = max(child.sizereq.w, child.sizemin.w)
-            reqh = max(child.sizereq.h, child.sizemin.h)
-            ha = child.hint_value('halign')
-            va = child.hint_value('valign')
-            margin = child.hint_value('margin')
-            if ha == 'left':
-                x = margin.l
-                w = reqw
-            if ha == 'right':
-                x = self.width - margin.r - reqw
-                w = reqw
-            if ha == 'fill':
-                x = margin.l
-                w = self.width - margin.l - margin.r
-            if ha == 'center':
-                x = (self.width - reqw) // 2
-                w = reqw
-            if va == 'top':
-                y = margin.t
-                h = reqh
-            if va == 'bottom':
-                y = self.height - margin.b - reqh
-                h = reqh
-            if va == 'fill':
-                y = margin.t
-                h = self.height - margin.t - margin.b
-            if va == 'center':
-                y = (self.height - reqh) // 2
-                h = reqh
-            child._pos.update(x=x, y=y)
-            child._size.update(w=w, h=h)
-            child._view_size.update(w=w, h=h)
-
-    def move_child(self, child, x=None, y=None):
-        """Move child inside container by adjusting its margin.
-
-        Operation is supported only for one-side anchors:
-            left, right, top, bottom
-        No move on axis where align is set to
-            center, fill
-
-        """
-        if not child in self.children:
-            raise ValueError('AnchorLayout.move(): Cannot move foreign child.')
-        margin = child.hint_value('margin')
-        newx = None
-        if x is not None:
-            ha = child.hint_value('halign')
-            ofsx = x - child.x
-            if ha == 'left':
-                margin.l += ofsx
-                newx = margin.l
-            elif ha == 'right':
-                margin.r -= ofsx
-                newx = self.width - margin.r - child.sizereq.w
-        newy = None
-        if y is not None:
-            va = child.hint_value('valign')
-            ofsy = y - child.y
-            if va == 'top':
-                margin.t += ofsy
-                newy = margin.t
-            elif va == 'bottom':
-                margin.b -= ofsy
-                newy = self.height - margin.b - child.sizereq.h
-        child._pos.update(x=newx, y=newy)
--- a/tuikit/layouts/fixed.py	Sun Feb 15 12:52:46 2015 +0100
+++ b/tuikit/layouts/fixed.py	Sun Feb 15 17:50:24 2015 +0100
@@ -3,7 +3,38 @@
 
 class FixedLayout(Layout):
 
-    def update(self, _w, _h):
+    """Widgets are placed on fixed position as specified in `sizereq`.
+
+    Align hints (kwargs to :meth:`add`):
+        * halign = 'left' (default) | 'right' | 'fill' | 'center'
+        * valign = 'top' (default) | 'bottom' | 'fill' | 'center'
+
+    """
+
+    def __init__(self):
+        Layout.__init__(self)
+        self._widget_hints = {}  # Widget : (halign, valign)
+
+    def add(self, widget, *args, **kwargs):
+        Layout.add(self, widget)
+        assert len(args) == 0, \
+            "FixedLayout does not support positional hint args: %s" % args
+        assert all(key in ('halign', 'valign') for key in kwargs.keys()), \
+            "Unsupported hints: %s" % tuple(kwargs.keys())
+        halign = kwargs.get('halign', 'left')
+        valign = kwargs.get('valign', 'top')
+        self._widget_hints[widget] = (halign, valign)
+
+    def update(self, w, h):
         for widget in self._managed_widgets:
-            widget.resize(*widget.sizereq)
-            widget.pos.update(*widget.posreq)
+            halign, valign = self._widget_hints[widget]
+            px, py = widget.posreq
+            sw, sh = widget.sizereq
+            if halign == 'right':   px = w - px
+            if valign == 'bottom':  py = h - py
+            if halign == 'fill':    px = 0; sw = w
+            if valign == 'fill':    py = 0; sh = h
+            if halign == 'center':  px = (w - sw) // 2
+            if valign == 'center':  py = (h - sh) // 2
+            widget.resize(sw, sh)
+            widget.pos.update(px, py)
--- a/tuikit/layouts/layout.py	Sun Feb 15 12:52:46 2015 +0100
+++ b/tuikit/layouts/layout.py	Sun Feb 15 17:50:24 2015 +0100
@@ -3,7 +3,12 @@
     def __init__(self):
         self._managed_widgets = []
 
-    def add(self, widget, **_kwargs):
+    def add(self, widget, *args, **kwargs):
+        """Add widget to layout.
+
+        Additional arguments may be used by specialized layouts as hints.
+
+        """
         self._managed_widgets.append(widget)
 
     def update(self, w, h):