Commits

Henning Schröder committed 2baa01e

add spacing

Comments (0)

Files changed (4)

layouts.py

-from tgui import TWidget, TPushButton, TApplication
-from tgui.layout import TVBoxLayout, THBoxLayout
-
-
-app = TApplication()
-container = TWidget(Pos=(20, 10), Size=(20, 10), WindowTitle="Tools")
-vb = TVBoxLayout()
-container.setLayout(vb)
-b1 = TPushButton(container, Text="Button 1", Pos=(1, 0))
-vb.addWidget(b1)
-b2 = TPushButton(container, Text="Button 2", Pos=(20, 0))
-vb.addWidget(b2)
-hb = THBoxLayout()
-vb.addLayout(hb)
-
-b3 = TPushButton(container, Text="Button 3")
-hb.addWidget(b3)
-b4 = TPushButton(container, Text="Button 4")
-hb.addWidget(b4)
-
-
-app.run()
 app = TApplication()
 container = TWidget(Pos=(20, 10), Size=(25, 10), WindowTitle="Tools")
 
-hb = THBoxLayout()
+hb = THBoxLayout(Spacing=3)
 container.Layout = hb
 
 b3 = TPushButton(container, Text="Button 3")
+from tgui import TWidget, TPushButton, TApplication
+from tgui.layout import TVBoxLayout, THBoxLayout
+
+
+app = TApplication()
+container = TWidget(Pos=(20, 10), Size=(20, 10), WindowTitle="Tools")
+vb = TVBoxLayout()
+container.setLayout(vb)
+b1 = TPushButton(container, Text="Button 1", Pos=(1, 0))
+vb.addWidget(b1)
+b2 = TPushButton(container, Text="Button 2", Pos=(20, 0))
+vb.addWidget(b2)
+hb = THBoxLayout()
+vb.addLayout(hb)
+
+b3 = TPushButton(container, Text="Button 3")
+hb.addWidget(b3)
+b4 = TPushButton(container, Text="Button 4")
+hb.addWidget(b4)
+hb.setSpacing(3)
+
+app.run()
-from tcore import TObject, TPoint, TSize, TRect
+from tcore import TObject, TPoint, TSize, TRect, Property
 from tgui import TWidget
 
 
 class TLayout(TObject):
 
-    def __init__(self, parent=None):
-        super(TLayout, self).__init__(parent)
+    def __init__(self, parent=None, **kwargs):
+        super(TLayout, self).__init__(parent, **kwargs)
         self.parent = parent
         self.items = []
         self._width = 1
     pass
 
 class TBoxLayout(TLayout):
-    pass
+
+    def __init__(self, parent=None, **kwargs):
+        self._spacing = (0, 0)
+        super(TBoxLayout, self).__init__(parent, **kwargs)
+
+    def spacing(self):
+        return self._spacing
+
+    def setSpacing(self, spacing):
+        if not isinstance(spacing, tuple):
+            spacing = (spacing, spacing)
+        self._spacing = spacing
+
+    Spacing = Property(spacing, setSpacing)
 
 
 class TVBoxLayout(TBoxLayout):
         row = start_row
         width = 1
         height = 1
+        (col_spacing, row_spacing) = self.spacing()
         for item in self.items:
             if isinstance(item, TWidget):
                 item.move(start_col, row)
                 width = max(width, item.width())
-                row += item.height()
+                row += item.height() + row_spacing
             else:
                 assert isinstance(item, TLayout)
                 item.invalidate(start_col, row)
                 item_size = item.size()
-                row += item_size.height
+                row += item_size.height + row_spacing
                 width = max(width, item_size.width)
             height = row
         self._width = width
         col = start_col
         width = 1
         height = 1
+        (col_spacing, row_spacing) = self.spacing()
         for item in self.items:
             if isinstance(item, TWidget):
                 item.move(col, start_row)
-                col += item.width()
+                col += item.width() + col_spacing
                 height = max(item.height(), height)
             else:
                 assert isinstance(item, TLayout)
                 item.invalidate(col, start_row)
                 item_size = item.size()
                 height = max(item_size.height, height)
-                col += item_size.width
+                col += item_size.width + col_spacing
             width = col
         self._width = width
         self._height = height