summaryrefslogtreecommitdiff
path: root/elementary/elementary.c_elementary_menu.pxi
blob: 1845227243a69ce69c2ff963e58b69a7a392fa74 (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
488
489
490
491
492
# Copyright (c) 2010 Boris Faure
#
# This file is part of python-elementary.
#
# python-elementary 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-elementary 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 python-elementary.  If not, see <http://www.gnu.org/licenses/>.
#

cdef class MenuItem(ObjectItem):

    """An item for the L{Menu} widget."""

    def __init__(   self,
                    evasObject menu,
                    MenuItem parent = None,
                    label = None,
                    icon = None,
                    callback = None,
                    *args, **kargs):

        cdef Elm_Object_Item *item, *parent_obj = NULL
        cdef Evas_Smart_Cb cb = NULL

        parent_obj = parent.item if parent is not None else NULL

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

        self.params = (callback, args, kargs)
        item = elm_menu_item_add(   menu.obj,
                                    parent_obj,
                                    _cfruni(icon) if icon is not None else NULL,
                                    _cfruni(label) if label is not None else NULL,
                                    cb,
                                    <void*>self)

        if item != NULL:
            self._set_obj(item)
        else:
            Py_DECREF(self)

    def object_get(self):
        """Get the Evas_Object of an Elm_Object_Item

        @warning: Don't manipulate this object!

        @return: The edje object containing the swallowed content

        """
        return Object_from_instance(elm_menu_item_object_get(self.item))

    def icon_name_set(self, icon):
        """Set the icon of a menu item to the standard icon with name C{icon}

        Once this icon is set, any previously set icon will be deleted.

        @param icon: The name of icon object to set for the content of item
        @type icon: string

        """
        elm_menu_item_icon_name_set(self.item, _cfruni(icon))

    def icon_name_get(self):
        """Get the string representation from the icon of a menu item

        @see: L{icon_name_set()}

        @return: The string representation of item's icon or None
        @rtype: string

        """
        return _ctouni(elm_menu_item_icon_name_get(self.item))

    property icon_name:
        """The standard icon name of a menu item

        Once this icon is set, any previously set icon will be deleted.

        @type: string

        """
        def __get__(self):
            return _ctouni(elm_menu_item_icon_name_get(self.item))
        def __set__(self, icon):
            elm_menu_item_icon_name_set(self.item, _cfruni(icon))

    def selected_set(self, selected):
        """Set the selected state of the item.

        @param selected: The selected/unselected state of the item
        @type selected: bool

        """
        elm_menu_item_selected_set(self.item, selected)

    def selected_get(self):
        """Get the selected state of the item.

        @see: L{selected_set()}

        @return: The selected/unselected state of the item
        @rtype: bool

        """
        return elm_menu_item_selected_get(self.item)

    property selected:
        """The selected state of the item.

        @type: bool

        """
        def __get__(self):
            return elm_menu_item_selected_get(self.item)
        def __set__(self, selected):
            elm_menu_item_selected_set(self.item, selected)

    def is_separator(self):
        """is_separator()

        Returns whether the item is a separator.

        @see: L{Menu.item_separator_add()}

        @return: If True, the item is a separator
        @rtype: bool

        """
        return False

    def subitems_get(self):
        """Returns a list of item's subitems.

        @return: A tuple of item's subitems
        @rtype: tuple of L{MenuItem}s

        """
        return _object_item_list_to_python(elm_menu_item_subitems_get(self.item))

    property subitems:
        """A list of item's subitems.

        @type: tuple of L{MenuItem}s

        """
        def __get__(self):
            return _object_item_list_to_python(elm_menu_item_subitems_get(self.item))

    def index_get(self):
        """Get the position of a menu item

        This function returns the index position of a menu item in a menu.
        For a sub-menu, this number is relative to the first item in the sub-menu.

        @note: Index values begin with 0

        @return: The item's index
        @rtype: int

        """
        return elm_menu_item_index_get(self.item)

    property index:
        """Get the position of a menu item

        This function returns the index position of a menu item in a menu.
        For a sub-menu, this number is relative to the first item in the
        sub-menu.

        @note: Index values begin with 0

        @type: int

        """
        def __get__(self):
            return elm_menu_item_index_get(self.item)

    def next_get(self):
        """Get the next item in the menu.

        @return: The item after it, or None
        @rtype: L{MenuItem}

        """
        return _object_item_to_python(elm_menu_item_next_get(self.item))

    property next:
        """Get the next item in the menu.

        @type: L{MenuItem}

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

    def prev_get(self):
        """Get the previous item in the menu.

        @return: The item before it, or None
        @rtype: L{MenuItem}

        """
        return _object_item_to_python(elm_menu_item_prev_get(self.item))

    property prev:
        """Get the previous item in the menu.

        @type: L{MenuItem}

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

cdef class MenuSeparatorItem(ObjectItem):
    def __init__(self, evasObject menu, MenuItem parent):
        cdef Elm_Object_Item *parent_obj = NULL

        if parent:
            parent_obj = parent.item
        item = elm_menu_item_separator_add(menu.obj, parent_obj)
        if not item:
            raise RuntimeError("Error creating separator")

        self._set_obj(item)

    def is_separator(self):
        """Returns whether the item is a separator.

        @see: L{Menu.item_separator_add()}

        @return: If True, the item is a separator
        @rtype: bool

        """
        return True

    def next_get(self):
        """Get the next item in the menu.

        @return: The item after it, or None
        @rtype: L{MenuItem}

        """
        return _object_item_to_python(elm_menu_item_next_get(self.item))

    property next:
        """Get the next item in the menu.

        @type: L{MenuItem}

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

    def prev_get(self):
        """Get the previous item in the menu.

        @return: The item before it, or None
        @rtype: L{MenuItem}

        """
        return _object_item_to_python(elm_menu_item_prev_get(self.item))

    property prev:
        """Get the previous item in the menu.

        @type: L{MenuItem}

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

cdef public class Menu(Object) [object PyElementaryMenu, type PyElementaryMenu_Type]:

    """A menu is a list of items displayed above its parent.

    When the menu is showing its parent is darkened. Each item can have a
    sub-menu. The menu object can be used to display a menu on a right click
    event, in a toolbar, anywhere.

    Signals that you can add callbacks for are:
        - "clicked" - the user clicked the empty space in the menu to dismiss.

    Default content parts of the menu items that you can use for are:
        - "default" - A main content of the menu item

    Default text parts of the menu items that you can use for are:
        - "default" - label in the menu item

    """

    def __init__(self, evasObject parent, obj = None):
        if obj is None:
            Object.__init__(self, parent.evas)
            self._set_obj(elm_menu_add(parent.obj))
        else:
            self._set_obj(<Evas_Object*>obj)

    def parent_set(self, evasObject parent):
        """Set the parent for the given menu widget

        @param parent: The new parent.
        @type parent: L{Object}

        """
        elm_menu_parent_set(self.obj, parent.obj)

    def parent_get(self):
        """Get the parent for the given menu widget

        @see: L{parent_set()}

        @return: The parent.
        @rtype: L{Object}

        """
        return Object_from_instance(elm_menu_parent_get(self.obj))

    property parent:
        """The parent for the given menu widget.

        @type: L{Object}

        """
        def __get__(self):
            return Object_from_instance(elm_menu_parent_get(self.obj))
        def __set__(self, evasObject parent):
            elm_menu_parent_set(self.obj, parent.obj)

    def move(self, x, y):
        """move(x, y)

        Move the menu to a new position

        Sets the top-left position of the menu to (C{x},C{y}).

        @note: C{x} and C{y} coordinates are relative to parent.

        @param x: The new position.
        @type x: Evas_Coord (int)
        @param y: The new position.
        @type y: Evas_Coord (int)

        """
        elm_menu_move(self.obj, x, y)

    def close(self):
        """close()

        Close a opened menu

        Hides the menu and all it's sub-menus.

        """
        elm_menu_close(self.obj)

    def items_get(self):
        """Returns a list of C{item}'s items.

        @return: A tuple of C{item}'s items
        @rtype: tuple of L{Object}s

        """
        return _object_item_list_to_python(elm_menu_items_get(self.obj))

    property items:
        """Returns a list of C{item}'s items.

        @type: tuple of L{Object}s

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

    def item_add(self, parent = None, label = None, icon = None, callback = None, *args, **kwargs):
        """item_add(parent=None, label=None, icon=None, callback=None, *args, **kwargs)

        Add an item at the end of the given menu widget

        @param parent: The parent menu item (optional)
        @type parent: L{Object}
        @param icon: An icon display on the item. The icon will be destroyed by the menu.
        @type icon: string
        @param label: The label of the item.
        @type label: string
        @param callback: Function called when the user select the item.
        @type callback: function

        @return: Returns the new item.
        @rtype: L{MenuItem}

        """
        return MenuItem(self, parent, label, icon, callback, *args, **kwargs)

    def item_separator_add(self, parent = None):
        """item_separator_add(parent=None)

        Add a separator item to menu under C{parent}.

        This item is a L{Separator}.

        @param parent: The item to add the separator under
        @type parent: L{Object}
        @return: The created item or None on failure
        @rtype: L{MenuSeparatorItem}

        """
        return MenuSeparatorItem(self, parent)

    def selected_item_get(self):
        """Get the selected item in the menu

        @see: L{MenuItem.selected_get()}
        @see: L{MenuItem.selected_set()}

        @return: The selected item, or None
        @rtype: L{MenuItem}

        """
        return _object_item_to_python(elm_menu_selected_item_get(self.obj))

    property selected_item:
        """The selected item in the menu

        @see: L{MenuItem.selected}

        @type: L{MenuItem}

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

    def last_item_get(self):
        """Get the last item in the menu

        @return: The last item, or None
        @rtype: L{MenuItem}

        """
        return _object_item_to_python(elm_menu_last_item_get(self.obj))

    property last_item:
        """The last item in the menu

        @type: L{MenuItem}

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

    def first_item_get(self):
        """Get the first item in the menu

        @return: The first item, or None
        @rtype: MenuItem

        """
        return _object_item_to_python(elm_menu_first_item_get(self.obj))

    property first_item:
        """The first item in the menu

        @type: MenuItem

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

    def callback_clicked_add(self, func, *args, **kwargs):
        """The user clicked the empty space in the menu to dismiss."""
        self._callback_add("clicked", func, *args, **kwargs)

    def callback_clicked_del(self, func):
        self._callback_del("clicked", func)

_elm_widget_type_register("menu", Menu)

cdef extern from "Elementary.h": # hack to force type to be known
    cdef PyTypeObject PyElementaryMenu_Type # hack to install metaclass
_install_metaclass(&PyElementaryMenu_Type, ElementaryObjectMeta)