summaryrefslogtreecommitdiff
path: root/efl/elementary/ctxpopup.pyx
blob: feba3a7aa457c00276a560a667f161a92567db89 (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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# Copyright (C) 2007-2014 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/>.
#

"""

Widget description
------------------

.. image:: /images/ctxpopup-preview.png
    :align: left

A ctxpopup is a widget that, when shown, pops up a list of items. It
automatically chooses an area inside its parent object's view to
optimally fit into it. In the default theme, it will also point an arrow
to it's top left position at the time one shows it. Ctxpopup items have
a label and/or an icon. It is intended for a small number of items
(hence the use of list, not genlist).

Signals that you can add callbacks for are:

- ``dismissed`` - This is called when 1. the outside of ctxpopup was clicked
  or 2. its parent area is changed or 3. the language is changed and also when
  4. the parent object is resized due to the window rotation. Then ctxpopup is
  dismissed.
- ``language,changed`` - This is called when the program's language is
  changed.
- ``focused`` - When the ctxpopup has received focus. (since 1.8)
- ``unfocused`` - When the ctxpopup has lost focus. (since 1.8)

Default content parts of the ctxpopup widget that you can use for are:

- ``default`` - A content of the ctxpopup

Default content parts of the ctxpopup items that you can use for are:

- ``icon`` - An icon in the title area

Default text parts of the ctxpopup items that you can use for are:

- ``default`` - Title label in the title area

.. note::

    Ctxpopup is a specialization of :py:class:`~efl.elementary.hover.Hover`.


Enumerations
------------

.. _Elm_Ctxpopup_Direction:

Ctxpopup arrow directions
=========================

.. data:: ELM_CTXPOPUP_DIRECTION_DOWN

    Arrow is pointing down

.. data:: ELM_CTXPOPUP_DIRECTION_RIGHT

    Arrow is pointing right

.. data:: ELM_CTXPOPUP_DIRECTION_LEFT

    Arrow is pointing left

.. data:: ELM_CTXPOPUP_DIRECTION_UP

    Arrow is pointing up

.. data:: ELM_CTXPOPUP_DIRECTION_UNKNOWN

    Arrow direction is unknown

"""

from cpython cimport PyUnicode_AsUTF8String

from efl.eo cimport _object_mapping_register, object_from_instance
from efl.evas cimport Object as evasObject
from layout_class cimport LayoutClass
from object_item cimport ObjectItem, _object_item_callback, \
    _object_item_callback2, _object_item_to_python, _object_item_list_to_python

cimport enums

ELM_CTXPOPUP_DIRECTION_DOWN = enums.ELM_CTXPOPUP_DIRECTION_DOWN
ELM_CTXPOPUP_DIRECTION_RIGHT = enums.ELM_CTXPOPUP_DIRECTION_RIGHT
ELM_CTXPOPUP_DIRECTION_LEFT = enums.ELM_CTXPOPUP_DIRECTION_LEFT
ELM_CTXPOPUP_DIRECTION_UP = enums.ELM_CTXPOPUP_DIRECTION_UP
ELM_CTXPOPUP_DIRECTION_UNKNOWN = enums.ELM_CTXPOPUP_DIRECTION_UNKNOWN

cdef class CtxpopupItem(ObjectItem):

    """An item for Ctxpopup widget."""

    cdef:
        bytes label
        evasObject icon

    def __init__(self, label = None, evasObject icon = None,
        callback = None, cb_data = None, *args, **kargs):
        """
        .. warning:: Ctxpopup can't hold both an item list and a content at the
            same time. When an item is added, any previous content will be
            removed.

        :param icon: Icon to be set on new item
        :type icon: :py:class:`~efl.evas.Object`
        :param label: The Label of the new item
        :type label: string
        :param func: Convenience function called when item selected
        :type func: function
        :return: The item added or ``None``, on errors
        :rtype: :py:class:`CtxpopupItem`

        """
        if callback is not None:
            if not callable(callback):
                raise TypeError("callback is not callable")

        if isinstance(label, unicode): label = PyUnicode_AsUTF8String(label)
        self.label = label
        self.icon = icon
        self.cb_func = callback
        self.cb_data = cb_data
        self.args = args
        self.kwargs = kargs

    def append_to(self, evasObject ctxpopup):
        """Add a new item to a ctxpopup object.

        .. warning:: Ctxpopup can't hold both an item list and a content at the
            same time. When an item is added, any previous content will be
            removed.

        .. seealso:: :py:attr:`~efl.elementary.object.Object.content`

        :param ctxpopup: The Ctxpopup widget this item is to be appended on
        :type ctxpopup: :py:class:`Ctxpopup`
        :return: The item added or ``None``, on errors
        :rtype: :py:class:`CtxpopupItem`

        """
        cdef Elm_Object_Item *item
        cdef Evas_Smart_Cb cb = NULL

        if self.cb_func is not None:
            cb = _object_item_callback2

        item = elm_ctxpopup_item_append(ctxpopup.obj,
            <const char *>self.label if self.label is not None else NULL,
            self.icon.obj if self.icon is not None else NULL,
            cb, <void*>self)

        if item == NULL:
            raise RuntimeError("The item could not be added to the widget.")

        self._set_obj(item)
        self._set_properties_from_keyword_args(self.kwargs)
        return self

    def prepend_to(self, evasObject ctxpopup):
        """Prepend a new item to a ctxpopup object.

        .. warning:: Ctxpopup can't hold both an item list and a content at the
            same time. When an item is added, any previous content will be
            removed.

        .. seealso:: :py:attr:`~efl.elementary.object.Object.content`

        :param ctxpopup: The Ctxpopup widget this item is to be prepended on
        :type ctxpopup: :py:class:`Ctxpopup`
        :return: The item added or ``None``, on errors
        :rtype: :py:class:`CtxpopupItem`

        ..versionadded:: 1.11

        """
        cdef Elm_Object_Item *item
        cdef Evas_Smart_Cb cb = NULL

        if self.cb_func is not None:
            cb = _object_item_callback2

        item = elm_ctxpopup_item_prepend(ctxpopup.obj,
                <const char *>self.label if self.label is not None else NULL,
                self.icon.obj if self.icon is not None else NULL,
                cb, <void*>self)

        if item == NULL:
            raise RuntimeError("The item could not be added to the widget.")

        self._set_obj(item)
        self._set_properties_from_keyword_args(self.kwargs)
        return self

    property prev:
        """ The previous item.

        :type: :py:class:`CtxpopupItem`

        ..versionadded:: 1.11

        """
        def __get__(self):
            return _object_item_to_python(elm_ctxpopup_item_prev_get(self.item))

    property next:
        """ The next item.

        :type: :py:class:`CtxpopupItem`

        ..versionadded:: 1.11

        """
        def __get__(self):
            return _object_item_to_python(elm_ctxpopup_item_next_get(self.item))

cdef class Ctxpopup(LayoutClass):

    """This is the class that actually implements the widget.

    .. versionchanged:: 1.8
        Inherits from LayoutClass

    """

    def __init__(self, evasObject parent, *args, **kwargs):
        self._set_obj(elm_ctxpopup_add(parent.obj))
        self._set_properties_from_keyword_args(kwargs)

    property hover_parent:
        """Ctxpopup hover's parent

        :type: :py:class:`~efl.evas.Object`

        """
        def __get__(self):
            return object_from_instance(elm_ctxpopup_hover_parent_get(self.obj))

        def __set__(self, evasObject parent):
            elm_ctxpopup_hover_parent_set(self.obj, parent.obj)

    def hover_parent_set(self, evasObject parent):
        elm_ctxpopup_hover_parent_set(self.obj, parent.obj)
    def hover_parent_get(self):
        return object_from_instance(elm_ctxpopup_hover_parent_get(self.obj))

    def clear(self):
        """Clear all items in the given ctxpopup object."""
        elm_ctxpopup_clear(self.obj)

    property horizontal:
        """Ctxpopup objects orientation.

        :type: bool

        """
        def __get__(self):
            return bool(elm_ctxpopup_horizontal_get(self.obj))

        def __set__(self, horizontal):
            elm_ctxpopup_horizontal_set(self.obj, horizontal)

    def horizontal_set(self, horizontal):
        elm_ctxpopup_horizontal_set(self.obj, horizontal)
    def horizontal_get(self):
        return bool(elm_ctxpopup_horizontal_get(self.obj))

    def item_append(self, label, evasObject icon=None,
                    func=None, *args, **kwargs):
        """A constructor for a :py:class:`CtxpopupItem`.

        :see: :py:func:`CtxpopupItem.append_to`

        """
        cdef:
            Elm_Object_Item *item
            Evas_Smart_Cb cb = NULL
            CtxpopupItem ret = CtxpopupItem.__new__(CtxpopupItem)

        if func is not None and callable(func):
            cb = _object_item_callback

        if isinstance(label, unicode): label = PyUnicode_AsUTF8String(label)

        item = elm_ctxpopup_item_append(self.obj,
            <const char *>label if label is not None else NULL,
            icon.obj if icon is not None else NULL,
            cb, <void*>ret)

        if item != NULL:
            ret._set_obj(item)
            ret.cb_func = func
            ret.args = args
            ret.kwargs = kwargs
            return ret
        else:
            return None

    def item_prepend(self, label, evasObject icon=None,
                     func=None, *args, **kwargs):
        """A constructor for a :py:class:`CtxpopupItem`.

        :see: :py:func:`CtxpopupItem.prepend_to`

        ..versionadded:: 1.11

        """
        cdef:
            Elm_Object_Item *item
            Evas_Smart_Cb cb = NULL
            CtxpopupItem ret = CtxpopupItem.__new__(CtxpopupItem)

        if func is not None and callable(func):
            cb = _object_item_callback

        if isinstance(label, unicode): label = PyUnicode_AsUTF8String(label)

        item = elm_ctxpopup_item_prepend(self.obj,
                    <const char *>label if label is not None else NULL,
                    icon.obj if icon is not None else NULL,
                    cb, <void*>ret)

        if item != NULL:
            ret._set_obj(item)
            ret.cb_func = func
            ret.args = args
            ret.kwargs = kwargs
            return ret
        else:
            return None

    property items:
        """ Get the list of items in the ctxpopup widget.

        This list is not to be modified in any way and is only valid until
        the object internal items list is changed. It should be fetched again
        with another call to this function when changes happen.

        :type: list of :py:class:`CtxpopupItem`

        ..versionadded:: 1.11

        """
        def __get__(self):
            return _object_item_list_to_python(elm_ctxpopup_items_get(self.obj))

    def items_get(self):
        return _object_item_list_to_python(elm_ctxpopup_items_get(self.obj))

    property first_item:
        """ The first item of the Ctxpopup.

        :type: :py:class:`CtxpopupItem`

        ..versionadded:: 1.11

        """
        def __get__(self):
            return _object_item_to_python(elm_ctxpopup_first_item_get(self.obj))

    def first_item_get(self):
        return _object_item_to_python(elm_ctxpopup_first_item_get(self.obj))

    property last_item:
        """ The last item of the Ctxpopup.

        :type: :py:class:`CtxpopupItem`

        ..versionadded:: 1.11

        """
        def __get__(self):
            return _object_item_to_python(elm_ctxpopup_last_item_get(self.obj))

    def last_item_get(self):
        return _object_item_to_python(elm_ctxpopup_last_item_get(self.obj))

    property direction_priority:
        """The direction priority order of a ctxpopup.

        This functions gives a chance to user to set the priority of ctxpopup
        showing direction. This doesn't guarantee the ctxpopup will appear
        in the requested direction.

        :type: (first, second, third, fourth) :ref:`Elm_Ctxpopup_Direction`

        """
        def __get__(self):
            cdef Elm_Ctxpopup_Direction first, second, third, fourth
            elm_ctxpopup_direction_priority_get(self.obj, &first, &second, &third, &fourth)
            return (first, second, third, fourth)

        def __set__(self, priority):
            cdef Elm_Ctxpopup_Direction first, second, third, fourth
            first, second, third, fourth = priority
            elm_ctxpopup_direction_priority_set(self.obj, first, second, third, fourth)

    def direction_priority_set(self, first, second, third, fourth):
        elm_ctxpopup_direction_priority_set(self.obj, first, second, third, fourth)
    def direction_priority_get(self):
        cdef Elm_Ctxpopup_Direction first, second, third, fourth
        elm_ctxpopup_direction_priority_get(self.obj, &first, &second, &third, &fourth)
        return (first, second, third, fourth)

    property direction:
        """Get the current direction of a ctxpopup.

        .. warning:: Only once the ctxpopup is shown can the direction be
            determined

        :type: :ref:`Elm_Ctxpopup_Direction`

        """
        def __get__(self):
            return elm_ctxpopup_direction_get(self.obj)

    def direction_get(self):
        return elm_ctxpopup_direction_get(self.obj)

    def dismiss(self):
        """Dismiss a ctxpopup object

        Use this function to simulate clicking outside the ctxpopup to
        dismiss it. In this way, the ctxpopup will be hidden and the
        "clicked" signal will be emitted.

        """
        elm_ctxpopup_dismiss(self.obj)

    property auto_hide_disabled:
        """Set ctxpopup auto hide mode triggered by ctxpopup policy.

        Use this property when you want ctxpopup not to hide automatically.
        By default, ctxpopup is dismissed whenever mouse clicked its background
        area, language is changed, and its parent geometry is updated(changed).

        :type: bool

        .. versionadded:: 1.9

        """
        def __get__(self):
            return bool(elm_ctxpopup_auto_hide_disabled_get(self.obj))

        def __set__(self, disabled):
            elm_ctxpopup_auto_hide_disabled_set(self.obj, disabled)

    def auto_hide_disabled_get(self):
        return bool(elm_ctxpopup_auto_hide_disabled_get(self.obj))
    def auto_hide_disabled_set(self, disabled):
        elm_ctxpopup_auto_hide_disabled_set(self.obj, disabled)

    def callback_dismissed_add(self, func, *args, **kwargs):
        """the ctxpopup was dismissed"""
        self._callback_add("dismissed", func, *args, **kwargs)

    def callback_dismissed_del(self, func):
        self._callback_del("dismissed", func)

    def callback_language_changed_add(self, func, *args, **kwargs):
        """This is called when the program's language is changed."""
        self._callback_add("language,changed", func, *args, **kwargs)

    def callback_language_changed_del(self, func):
        self._callback_del("language,changed", func)

    def callback_focused_add(self, func, *args, **kwargs):
        """When the ctxpopup has received focus.

        .. versionadded:: 1.8
        """
        self._callback_add("focused", func, *args, **kwargs)

    def callback_focused_del(self, func):
        self._callback_del("focused", func)

    def callback_unfocused_add(self, func, *args, **kwargs):
        """When the ctxpopup has lost focus.

        .. versionadded:: 1.8
        """
        self._callback_add("unfocused", func, *args, **kwargs)

    def callback_unfocused_del(self, func):
        self._callback_del("unfocused", func)

_object_mapping_register("Elm_Ctxpopup", Ctxpopup)