summaryrefslogtreecommitdiff
path: root/efl/edje/efl.edje_external.pxi
blob: e73886e727660eacf74a7939707c7d53d3f565d3 (plain)
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# Copyright (C) 2007-2013 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/>.


cdef class ExternalParam:
    property name:
        def __get__(self):
            if self.obj == NULL:
                raise ValueError("Object uninitialized")
            return self.obj.name

    property type:
        def __get__(self):
            if self.obj == NULL:
                raise ValueError("Object uninitialized")
            return self.obj.type

    property i:
        def __get__(self):
            if self.obj == NULL:
                raise ValueError("Object uninitialized")
            return self.obj.i

    property d:
        def __get__(self):
            if self.obj == NULL:
                raise ValueError("Object uninitialized")
            return self.obj.d

    property s:
        def __get__(self):
            if self.obj == NULL:
                raise ValueError("Object uninitialized")
            if self.obj.s != NULL:
                return self.obj.s

    property b:
        def __get__(self):
            if self.obj == NULL:
                raise ValueError("Object uninitialized")
            return bool(self.obj.i)

    property value:
        def __get__(self):
            if self.obj == NULL:
                raise ValueError("Object uninitialized")
            if self.obj.type == EDJE_EXTERNAL_PARAM_TYPE_INT:
                return self.obj.i
            elif self.obj.type == EDJE_EXTERNAL_PARAM_TYPE_DOUBLE:
                return self.obj.d
            elif self.obj.type == EDJE_EXTERNAL_PARAM_TYPE_STRING or \
                    self.obj.type == EDJE_EXTERNAL_PARAM_TYPE_CHOICE:
                if self.obj.s != NULL:
                    return self.obj.s
            elif self.obj.type == EDJE_EXTERNAL_PARAM_TYPE_BOOL:
                return bool(self.obj.i)


cdef ExternalParam ExternalParam_from_ptr(Edje_External_Param *param):
    cdef ExternalParam p
    p = ExternalParam()
    p.obj = param
    return p

# XXX: this should be C-only, but it would require edje_edit
# XXX: being able to use it.
def _ExternalParam_from_ptr(uintptr_t ptr):
    return ExternalParam_from_ptr(<Edje_External_Param *>ptr)


cdef class ExternalParamInfo:
    property name:
        def __get__(self):
            if self.obj.name:
                return self.obj.name
            return "(unknown)"

    property translated_name:
        def __get__(self):
            cdef const_char *t
            if self._external_type_obj == NULL or \
               self._external_type_obj.translate == NULL:
                return self.name
            t = self._external_type_obj.translate(self._external_type_obj.data,
                                                  self.obj.name)
            if t == NULL:
                return self.name
            return t

    property type:
        def __get__(self):
            return self.obj.type

    def validate(self, value):
        return True

    cdef _set_external_type(self, t):
        cdef ExternalType ext_type
        self.external_type = t
        if isinstance(t, ExternalType):
            ext_type = t
            self._external_type_obj = ext_type._obj


cdef class ExternalParamInfoInt(ExternalParamInfo):
    property default:
        def __get__(self):
            if self.obj.info.i.default == EDJE_EXTERNAL_INT_UNSET:
                return None
            return self.obj.info.i.default

    property min:
        def __get__(self):
            if self.obj.info.i.min == EDJE_EXTERNAL_INT_UNSET:
                return None
            return self.obj.info.i.min

    property max:
        def __get__(self):
            if self.obj.info.i.max == EDJE_EXTERNAL_INT_UNSET:
                return None
            return self.obj.info.i.max

    property step:
        def __get__(self):
            if self.obj.info.i.step == EDJE_EXTERNAL_INT_UNSET:
                return None
            return self.obj.info.i.step

    def validate(self, value):
        min = self.min
        max = self.max
        if min is not None and value < min:
            return False
        if max is not None and value > max:
            return False
        return True


cdef class ExternalParamInfoDouble(ExternalParamInfo):
    property default:
        def __get__(self):
            if self.obj.info.d.default == EDJE_EXTERNAL_DOUBLE_UNSET:
                return None
            return self.obj.info.d.default

    property min:
        def __get__(self):
            if self.obj.info.d.min == EDJE_EXTERNAL_DOUBLE_UNSET:
                return None
            return self.obj.info.d.min

    property max:
        def __get__(self):
            if self.obj.info.d.max == EDJE_EXTERNAL_DOUBLE_UNSET:
                return None
            return self.obj.info.d.max

    property step:
        def __get__(self):
            if self.obj.info.d.step == EDJE_EXTERNAL_DOUBLE_UNSET:
                return None
            return self.obj.info.d.step

    def validate(self, value):
        min = self.min
        max = self.max
        if min is not None and value < min:
            return False
        if max is not None and value > max:
            return False
        return True


cdef class ExternalParamInfoString(ExternalParamInfo):
    property default:
        def __get__(self):
            if self.obj.info.s.default == NULL:
                return None
            return self.obj.info.s.default

    property translated_default:
        def __get__(self):
            cdef const_char *t
            if self._external_type_obj == NULL or \
               self._external_type_obj.translate == NULL:
                return self.default
            t = self._external_type_obj.translate(self._external_type_obj.data,
                                                  self.obj.info.s.default)
            if t == NULL:
                return self.default
            return t

    property accept_format:
        def __get__(self):
            if self.obj.info.s.accept_fmt == NULL:
                return None
            return self.obj.info.s.accept_fmt

    property deny_format:
        def __get__(self):
            if self.obj.info.s.deny_fmt == NULL:
                return None
            return self.obj.info.s.deny_fmt


cdef class ExternalParamInfoBool(ExternalParamInfo):
    property default:
        def __get__(self):
            return bool(self.obj.info.b.default)

    property false_string:
        def __get__(self):
            if self.obj.info.b.false_str == NULL:
                return None
            return self.obj.info.b.false_str

    property translated_false_string:
        def __get__(self):
            cdef const_char *t
            if self._external_type_obj == NULL or \
               self._external_type_obj.translate == NULL:
                return self.false_string
            t = self._external_type_obj.translate(self._external_type_obj.data,
                                                  self.obj.info.b.false_str)
            if t == NULL:
                return self.false_string
            return t

    property true_string:
        def __get__(self):
            if self.obj.info.b.true_str == NULL:
                return None
            return self.obj.info.b.true_str

    property translated_true_string:
        def __get__(self):
            cdef const_char *t
            if self._external_type_obj == NULL or \
               self._external_type_obj.translate == NULL:
                return self.true_string
            t = self._external_type_obj.translate(self._external_type_obj.data,
                                                  self.obj.info.b.true_str)
            if t == NULL:
                return self.true_string
            return t


cdef class ExternalParamInfoChoice(ExternalParamInfo):
    property default:
        def __get__(self):
            if self.obj.info.c.default == NULL:
                return None
            return self.obj.info.c.default

    property translated_default:
        def __get__(self):
            cdef const_char *t
            if self._external_type_obj == NULL or \
               self._external_type_obj.translate == NULL:
                return self.default
            t = self._external_type_obj.translate(self._external_type_obj.data,
                                                  self.obj.info.c.default)
            if t == NULL:
                return self.default
            return t

    property choices:
        def __get__(self):
            cdef int i

            if self.obj.info.c.choices == NULL:
                return None

            i = 0
            lst = []
            while self.obj.info.c.choices[i] != NULL:
                lst.append(self.obj.info.c.choices[i])
                i += 1
            return lst

    property translated_choices:
        def __get__(self):
            cdef const_char *t
            if self._external_type_obj == NULL or \
               self._external_type_obj.translate == NULL:
                return self.choices

            orig = self.choices
            ret = []
            for choice in orig:
                ret.append(
                self._external_type_obj.translate(
                        self._external_type_obj.data, choice) or choice)
            return ret


cdef ExternalParamInfo ExternalParamInfo_from_ptr(type, Edje_External_Param_Info *ptr):
    cdef ExternalParamInfo p
    if ptr.type == EDJE_EXTERNAL_PARAM_TYPE_INT:
        p = ExternalParamInfoInt()
    elif ptr.type == EDJE_EXTERNAL_PARAM_TYPE_DOUBLE:
        p = ExternalParamInfoDouble()
    elif ptr.type == EDJE_EXTERNAL_PARAM_TYPE_STRING:
        p = ExternalParamInfoString()
    elif ptr.type == EDJE_EXTERNAL_PARAM_TYPE_BOOL:
        p = ExternalParamInfoBool()
    elif ptr.type == EDJE_EXTERNAL_PARAM_TYPE_CHOICE:
        p = ExternalParamInfoChoice()
    else:
        msg = "Don't know how to convert parameter %s of type %s" % \
              (ptr.name, edje_external_param_type_str(ptr.type))
        warnings.warn(msg, Warning)
        return None
    p.obj = ptr
    p._set_external_type(type)
    return p

def external_param_info_get(char *type_name):
    cdef const_Edje_External_Type *ext_type
    cdef ExternalType t

    warnings.warn("Use ExternalType.parameters_info_get() instead!",
                  DeprecationWarning)

    ext_type = edje_external_type_get(type_name)
    if ext_type == NULL:
        return tuple()

    t = ExternalType()
    t._name = type_name
    t._obj = ext_type
    return t.parameters_info_get()


cdef class ExternalType:
    property name:
        def __get__(self):
            return self._name

    property module:
        def __get__(self):
            if self._obj.module == NULL:
                return None
            return self._obj.module

    property module_name:
        def __get__(self):
            if self._obj.module_name == NULL:
                return None
            return self._obj.module_name

    def label_get(self):
        cdef const_char *l
        if self._obj.label_get == NULL:
            return None
        l = self._obj.label_get(self._obj.data)
        if l == NULL:
            return None
        ret = l
        return ret

    def description_get(self):
        cdef const_char *l
        if self._obj.description_get == NULL:
            return None
        l = self._obj.description_get(self._obj.data)
        if l == NULL:
            return None
        ret = l
        return ret

    def translate(self, char *text):
        """Used to translate text originated from this module.

        Usually this is only required for static text, like the
        parameters. label_get() and description_get() may not require
        it. Note that ExternalParamInfo provides translated_* versions
        of its worth translating strings!

        It will always return a string, on errors the parameter text
        is returned untranslated.
        """
        cdef const_char *l
        if self._obj.translate == NULL:
            return text
        l = self._obj.translate(self._obj.data, text)
        if l == NULL:
            return text
        return l

    def icon_add(self, Canvas canvas not None):
        cdef Evas_Object *icon
        if self._obj.icon_add == NULL:
            return None
        icon = self._obj.icon_add(self._obj.data, canvas.obj)
        if icon == NULL:
            return None
        return object_from_instance(icon)

    def preview_add(self, Canvas canvas not None):
        cdef Evas_Object *preview
        if self._obj.preview_add == NULL:
            return None
        preview = self._obj.preview_add(self._obj.data, canvas.obj)
        if preview == NULL:
            return None
        return object_from_instance(preview)

    def parameters_info_get(self):
        cdef Edje_External_Param_Info *params
        cdef int i

        if self._parameters_info:
            return self._parameters_info

        lst = []
        params = self._obj.parameters_info
        if params == NULL:
            self._parameters_info = tuple()

        i = 0
        while params[i].name != NULL:
            lst.append(ExternalParamInfo_from_ptr(self, &params[i]))
            i += 1
        self._parameters_info = tuple(lst)
        return self._parameters_info

    property parameters_info:
        def __get__(self):
            return self.parameters_info_get()


cdef class ExternalIterator:
    cdef Eina_Iterator *obj

    def __init__(self):
        cdef Eina_Iterator *it
        it = edje_external_iterator_get()
        self._set_obj(it)

    cdef _set_obj(self, Eina_Iterator *ptr):
        self.obj = ptr

    def __iter__(self):
        return self

    def __next__(self):
        cdef const_Eina_Hash_Tuple *tuple
        cdef ExternalType t
        if eina_iterator_next(self.obj, <void **>&tuple):
            t = ExternalType()
            t._name = <char*>tuple.key
            t._obj = <const_Edje_External_Type*>tuple.data
            return t
        else:
            raise StopIteration

    def __del__(self):
        if self.obj:
            eina_iterator_free(self.obj)

def external_type_get(char *type_name):
    "Gets the instance that represents an ExternalType of the given name."
    cdef const_Edje_External_Type *obj = edje_external_type_get(type_name)
    cdef ExternalType ret
    if obj == NULL:
        return None
    ret = ExternalType()
    ret._name = type_name
    ret._obj = obj
    return ret