summaryrefslogtreecommitdiff
path: root/efl/evas/efl.evas_object_textgrid.pxi
blob: 9b93cc4dc53c7a4fc308fa1f09cdf65f1bdf8630 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# Copyright (C) 2007-2015 various contributors (see AUTHORS)
#
# This file is part of Python-EFL.
#
# Python-EFL is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# Python-EFL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this Python-EFL.  If not, see <http://www.gnu.org/licenses/>.

from libc.stdlib cimport malloc


cdef class TextgridCell(object):
    """

    The values that describe each cell.

    .. versionadded:: 1.8

    """

    cdef Evas_Textgrid_Cell *cell

    def __str__(self):
        return "%s" % (self.codepoint,)

    def __repr__(self):
        return "<%s(codepoint = %s, fg = %s, bg = %s, bold = %s, \
            italic = %s, underline = %s, strikethrough = %s, \
            fg_extended = %s, bg_extended = %s, double_width = %s)>" % (
            type(self).__name__, self.codepoint,
            self.fg, self.bg, self.bold, self.italic,
            self.underline, self.strikethrough,
            self.fg_extended, self.bg_extended,
            self.double_width)

    property codepoint:
        """the UNICODE value of the character

        :type: unicode

        """
        def __set__(self, value):
            if not isinstance(value, unicode):
                value = value.decode("UTF-8")

            self.cell.codepoint = <Py_UCS4>value

        def __get__(self):
            return <unicode><Py_UCS4>self.cell.codepoint

    property fg:
        """the index of the palette for the foreground color

        :type: int

        """
        def __set__(self, int value):
            self.cell.fg = value

        def __get__(self):
            return self.cell.fg

    property bg:
        """the index of the palette for the background color

        :type: int

        """
        def __set__(self, int value):
            self.cell.bg = value

        def __get__(self):
            return self.cell.bg

    property bold:
        """whether the character is bold

        :type: bool

        """
        def __set__(self, bint value):
            self.cell.bold = value

        def __get__(self):
            return <bint>self.cell.bold

    property italic:
        """whether the character is oblique

        :type: bool

        """
        def __set__(self, bint value):
            self.cell.italic = value

        def __get__(self):
            return <bint>self.cell.italic

    property underline:
        """whether the character is underlined

        :type: bool

        """
        def __set__(self, bint value):
            self.cell.underline = value

        def __get__(self):
            return <bint>self.cell.underline

    property strikethrough:
        """whether the character is strikethrough'ed

        :type: bool

        """
        def __set__(self, bint value):
            self.cell.strikethrough = value

        def __get__(self):
            return <bint>self.cell.strikethrough

    property fg_extended:
        """whether the extended palette is used for the foreground color

        :type: bool

        """
        def __set__(self, bint value):
            self.cell.fg_extended = value

        def __get__(self):
            return <bint>self.cell.fg_extended

    property bg_extended:
        """whether the extended palette is used for the background color

        :type: bool

        """
        def __set__(self, bint value):
            self.cell.bg_extended = value

        def __get__(self):
            return <bint>self.cell.bg_extended

    property double_width:
        """If the codepoint is merged with the following cell to the right
        visually (cells must be in pairs with 2nd cell being a duplicate in
        all ways except codepoint is 0)

        :type: bool

        """
        def __set__(self, bint value):
            self.cell.double_width = value

        def __get__(self):
            return <bint>self.cell.double_width

cdef class Textgrid(Object):
    """

    A textgrid object.

    .. versionadded:: 1.8

    """
    def __init__(self, Canvas canvas not None, **kwargs):
        """Textgrid(...)

        :param canvas: Evas canvas for this object
        :type canvas: :py:class:`~efl.evas.Canvas`
        :keyword \**kwargs: All the remaining keyword arguments are interpreted
                            as properties of the instance

        """
        self._set_obj(evas_object_textgrid_add(canvas.obj))
        self._set_properties_from_keyword_args(kwargs)

    property size:
        """The size of the textgrid object.

        The number of lines **h** and the number of columns **w** of the
        textgrid object. Values less than or equal to 0 are ignored.

        :type: (int **w**, int **h**)

        """
        def __set__(self, value):
            cdef int w, h
            w, h = value
            evas_object_textgrid_size_set(self.obj, w, h)

        def __get__(self):
            cdef int w, h
            evas_object_textgrid_size_get(self.obj, &w, &h)
            return (w, h)

    property font_source:
        """The font (source) file used on a given textgrid object.

        This allows the font file to be explicitly set for the textgrid object,
        overriding system lookup, which will first occur in the given file's
        contents. If None or an empty string is assigned, or the same
        font_source has already been set, or on error, this does nothing.

        :type: string

        .. seealso:: :py:attr:`font`

        """
        def __set__(self, font_source):
            a1 = font_source
            if isinstance(a1, unicode): a1 = PyUnicode_AsUTF8String(a1)
            evas_object_textgrid_font_source_set(self.obj,
                <const char *>a1 if a1 is not None else NULL)

        def __get__(self):
            return _ctouni(evas_object_textgrid_font_source_get(self.obj))

    property font:
        """The font family and size on a given textgrid object.

        This property allows the **font_name** and
        **font_size** of the textgrid object to be set. The **font_name**
        string has to follow fontconfig's convention on naming fonts, as
        it's the underlying library used to query system fonts by Evas (see
        the ``fc``-list command's output, on your system, to get an
        idea). It also has to be a monospace font. If **font_name** is
        ``None``, or if it is an empty string, or if **font_size** is less or
        equal than 0, or on error, this function does nothing.

        :type: (string **font_name**, int **font_size**)

        :see: :py:attr:`font_source`

        """
        def __set__(self, value):
            cdef int font_size
            font_name, font_size = value
            a1 = font_name
            if isinstance(a1, unicode): a1 = PyUnicode_AsUTF8String(a1)
            evas_object_textgrid_font_set(self.obj,
                <const char *>a1 if a1 is not None else NULL,
                font_size)

        def __get__(self):
            cdef:
                const char *font_name
                Evas_Font_Size font_size
            evas_object_textgrid_font_get(self.obj, &font_name, &font_size)
            # font_name is owned by Evas, don't free it
            return (_ctouni(font_name), font_size)

    property cell_size:
        """The width and height of a cell in pixels.

        This read-only property has the width and height, in pixels, of a cell
        of the textgrid object. Their value depends on the monospace font used
        for the textgrid object, as well as the style. On error, they are set
        to 0.

        :type: (int **width**, int **height**)

        .. seealso::

            :py:attr:`font`
            :py:attr:`supported_font_styles`

        """
        def __get__(self):
            cdef:
                Evas_Coord w, h
            evas_object_textgrid_cell_size_get(self.obj, &w, &h)
            return (w, h)

    def palette_set(self, Evas_Textgrid_Palette pal, int idx, int r, int g, int b, int a):
        """Set color to the given palette at the given index.

        :param pal: The type of the palette to set the color.
        :type pal: :ref:`Evas_Textgrid_Palette`
        :param int idx: The index of the paletter to which the color is stored.
        :param int r: The red component of the color.
        :param int g: The green component of the color.
        :param int b: The blue component of the color.
        :param int a: The alpha component of the color.

        This function sets the color for the palette of type **pal** at the
        index **idx** of the textgrid object. The ARGB components are given by
        **r**, **g**, **b** and **a**. This color can be used when setting the
        :py:class:`TextgridCell` object. The components must set a pre-
        multiplied color. If pal is EVAS_TEXTGRID_PALETTE_NONE or
        EVAS_TEXTGRID_PALETTE_LAST, or if **idx** is not between 0 and 255, or
        on error, this function does nothing. The color components are clamped
        between 0 and 255. If **idx** is greater than the latest set color, the
        colors between this last index and **idx** - 1 are set to black (0, 0,
        0, 0).

        :see: :py:func:`palette_get`

        """
        evas_object_textgrid_palette_set(self.obj, pal, idx, r, g, b, a)

    def palette_get(self, Evas_Textgrid_Palette pal, int idx):
        """Retrieve color from the given palette at the given index.

        :param pal: The type of the palette to set the color.
        :type pal: :ref:`Evas_Textgrid_Palette`
        :param int idx: The index of the palette to which the color is stored.
        :rtype: (int **r**, int **g**, int **b**, int **a**)

        This method retrieves the color for the palette of type **pal** at
        the index **idx** of the textgrid object. The ARGB components are
        stored in the buffers **r**, **g**, **b** and **a**. If **idx** is not
        between 0 and the index of the latest set color, or if **pal** is
        EVAS_TEXTGRID_PALETTE_NONE or EVAS_TEXTGRID_PALETTE_LAST, the values of
        the components are 0.

        :see: :py:meth:`palette_set`

        """
        cdef:
            int r, g, b, a
        evas_object_textgrid_palette_get(self.obj, pal, idx, &r, &g, &b, &a)
        return (r, g, b, a)


    property supported_font_styles:
        """ TODO: document this """
        def __set__(self, Evas_Textgrid_Font_Style styles):
            evas_object_textgrid_supported_font_styles_set(self.obj, styles)

        def __get__(self):
            return evas_object_textgrid_supported_font_styles_get(self.obj)

    def cellrow_set(self, int y, list 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

        This method allows returning cells to the textgrid, retrieved with
        :py:meth:`cellrow_get`.

        .. seealso::

            :py:meth:`cellrow_get`
            :py:attr:`size`
            :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])

    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

        This method returns a list of cells in the line **y** of
        the textgrid object. If **y** is not between 0 and the number
        of lines of the grid - 1, or on error, this function return ``None``.

        .. seealso::

            :py:meth:`cellrow_set`
            :py:attr:`size`
            :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

    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.

        :param int x: The rect region of cells top-left x (column)
        :param int y: The rect region of cells top-left y (row)
        :param int w: The rect region size in number of cells (columns)
        :param int h: The rect region size in number of cells (rows)

        This method declares to evas that a region of cells was updated by
        code and needs refreshing. An application should modify cells like this
        as an example::

            cells = tg.cellrow_get(row)
            for i in range(width):
                cells[i].codepoint = 'E'
            tg.cellrow_set(row, cells)
            tg.update_add(0, row, width, 1)

        .. seealso::

            :py:meth:`cellrow_set`
            :py:meth:`cellrow_get`
            :py:attr:`size`

        """
        evas_object_textgrid_update_add(self.obj, x, y, w, h)

_object_mapping_register("Evas_Textgrid", Textgrid)