kuuko pushed a commit to branch master.

http://git.enlightenment.org/bindings/python/python-efl.git/commit/?id=acf2d339691af35d1301d382920a0cb4c9ad2597

commit acf2d339691af35d1301d382920a0cb4c9ad2597
Author: Kai Huuhko <kai.huu...@gmail.com>
Date:   Sun May 3 13:05:15 2015 +0300

    Evas.Textgrid: Fix memleak
    
    Had to change the type of cellrows from list to custom tuple-like type.
---
 efl/evas/efl.evas_object_textgrid.pxi     | 74 ++++++++++++++++++-------------
 examples/elementary/test_evas_textgrid.py | 47 ++++++++++++--------
 2 files changed, 72 insertions(+), 49 deletions(-)

diff --git a/efl/evas/efl.evas_object_textgrid.pxi 
b/efl/evas/efl.evas_object_textgrid.pxi
index 9b93cc4..68e579d 100644
--- a/efl/evas/efl.evas_object_textgrid.pxi
+++ b/efl/evas/efl.evas_object_textgrid.pxi
@@ -42,6 +42,14 @@ cdef class TextgridCell(object):
             self.fg_extended, self.bg_extended,
             self.double_width)
 
+    @staticmethod
+    cdef TextgridCell create(Evas_Textgrid_Cell *cell):
+        if cell == NULL:
+            raise ValueError("Invalid pointer for evas textgrid cell!")
+        cdef TextgridCell ret = TextgridCell.__new__(TextgridCell)
+        ret.cell = cell
+        return ret
+
     property codepoint:
         """the UNICODE value of the character
 
@@ -167,6 +175,35 @@ cdef class TextgridCell(object):
         def __get__(self):
             return <bint>self.cell.double_width
 
+
+cdef class TextgridCellRow:
+
+    cdef:
+        Evas_Textgrid_Cell *row
+        unsigned int rowsize
+
+    @staticmethod
+    cdef TextgridCellRow create(Evas_Textgrid_Cell *row, unsigned int rowsize):
+        if row == NULL:
+            raise ValueError("Invalid pointer for evas textgrid cell row!")
+        cdef TextgridCellRow ret = TextgridCellRow.__new__(TextgridCellRow)
+        ret.row = row
+        ret.rowsize = rowsize
+        return ret
+
+    def __len__(self):
+        return self.rowsize
+
+    def __getitem__(self, x):
+        if x > self.rowsize - 1:
+            raise IndexError
+        if x < 0:
+            x = self.rowsize - x
+        if x < 0:
+            raise IndexError
+        return TextgridCell.create(&self.row[x])
+
+
 cdef class Textgrid(Object):
     """
 
@@ -343,13 +380,13 @@ cdef class Textgrid(Object):
         def __get__(self):
             return evas_object_textgrid_supported_font_styles_get(self.obj)
 
-    def cellrow_set(self, int y, list row not None):
+    def cellrow_set(self, int y, TextgridCellRow row not None):
         """Set the string at the given row.
 
         :param y: The row index of the grid.
         :type y: int
         :param row: The string as a sequence of :class:`TextgridCell`.
-        :type row: list
+        :type row: :class:`TextgridCellRow`
 
         This method allows returning cells to the textgrid, retrieved with
         :py:meth:`cellrow_get`.
@@ -361,26 +398,14 @@ cdef class Textgrid(Object):
             :py:meth:`update_add`
 
         """
-        cdef:
-            TextgridCell cell
-            Evas_Textgrid_Cell **crow
-            int rlen = len(row)
-            int i
-
-        crow = <Evas_Textgrid_Cell **>malloc(rlen * sizeof(Evas_Textgrid_Cell 
*))
-
-        for i in range(rlen):
-            cell = row[i]
-            crow[i] = cell.cell
-
-        evas_object_textgrid_cellrow_set(self.obj, y, crow[0])
+        evas_object_textgrid_cellrow_set(self.obj, y, row.row)
 
     def cellrow_get(self, int y):
         """Get the string at the given row.
 
         :param int y: The row index of the grid.
         :return: A list of :class:`TextgridCell`
-        :rtype: list
+        :rtype: :class:`TextgridCellRow`
 
         This method returns a list of cells in the line **y** of
         the textgrid object. If **y** is not between 0 and the number
@@ -393,21 +418,8 @@ cdef class Textgrid(Object):
             :py:meth:`update_add`
 
         """
-        cdef:
-            Evas_Textgrid_Cell *row = 
evas_object_textgrid_cellrow_get(self.obj, y)
-            int i
-            list ret = []
-            TextgridCell cell
-
-        if row == NULL:
-            return None
-
-        for i in range(self.size[0]):
-            cell = TextgridCell.__new__(TextgridCell)
-            cell.cell = &row[i]
-            ret.append(cell)
-
-        return ret
+        return TextgridCellRow.create(
+            evas_object_textgrid_cellrow_get(self.obj, y), self.size[0])
 
     def update_add(self, int x, int y, int w, int h):
         """Indicate for evas that part of a textgrid region (cells) has been 
updated.
diff --git a/examples/elementary/test_evas_textgrid.py 
b/examples/elementary/test_evas_textgrid.py
index 5fce834..8a281a8 100644
--- a/examples/elementary/test_evas_textgrid.py
+++ b/examples/elementary/test_evas_textgrid.py
@@ -5,34 +5,46 @@ from efl import evas
 from efl.evas import Textgrid, EVAS_TEXTGRID_PALETTE_STANDARD
 from efl import elementary
 from efl.elementary.window import StandardWindow
-from efl.elementary.background import Background
+
+if "unichr" not in dir(__builtins__):
+    unichr = chr
 
 
 def evas_textgrid_clicked(obj, item=None):
-    win = StandardWindow("evastextgrid", "Evas Textgrid Test", autodel=True,
-        size=(320, 320))
+    win = StandardWindow(
+        "evastextgrid", "Evas Textgrid Test", autodel=True)
     if obj is None:
         win.callback_delete_request_add(lambda o: elementary.exit())
 
-    tg = Textgrid(win.evas)
-    tg.size = 15, 1
-    tg.size_hint_weight_set(1.0, 1.0)
+    W = 80
+    H = 26
+
+    tg = Textgrid(
+        win.evas, size_hint_weight=(1.0, 1.0), size=(W, H),
+        font=("monospace", 14))
     win.resize_object_add(tg)
-    tg.font = "Courier", 20
     tg.palette_set(EVAS_TEXTGRID_PALETTE_STANDARD, 0, 0, 0, 0, 255)
-    tg.palette_set(EVAS_TEXTGRID_PALETTE_STANDARD, 1, 255, 255, 255, 255)
 
-    row = tg.cellrow_get(0)
-    for cell in row:
-        cell.codepoint="รถ"
-        cell.fg = 1
-        cell.bg = 0
-    tg.cellrow_set(0, row)
+    win.size_step = tg.cell_size
 
-    tg.show()
-    tg.update_add(0, 0, 10, 1)
+    # XXX: Add 1 to size, else the last row/col won't fit. Unknown reason.
+    win.size = (W * tg.cell_size[0] + 1, H * tg.cell_size[1] + 1)
 
-    rowback = tg.cellrow_get(0)
+    for i in range(H):
+        ci = i + 1
+        cv = ci * 9
+        tg.palette_set(
+            EVAS_TEXTGRID_PALETTE_STANDARD, ci, cv, cv, cv, 255)
+        row = tg.cellrow_get(i)
+        if row is not None:
+            for cell in row:
+                cell.codepoint = unichr(1000 + i)
+                cell.bg = 0
+                cell.fg = ci
+            tg.cellrow_set(i, row)
+
+    tg.show()
+    tg.update_add(0, 0, 80, 26)
 
     win.show()
 
@@ -46,4 +58,3 @@ if __name__ == "__main__":
     elementary.run()
     elementary.shutdown()
     evas.shutdown()
-

-- 


Reply via email to