kuuko pushed a commit to branch master.

commit e164c111a2c7f1ce8ab3b9fac358e5f0e15dcae9
Author: Kai Huuhko <[email protected]>
Date:   Fri Apr 19 09:48:15 2013 +0000

    Elementary: Implement Toolbar item states, which create an issue with
    the item data making the item_del_cb crash.
---
 efl/elementary/toolbar.pxd          |  17 ++--
 efl/elementary/toolbar.pyx          |  64 ++++++++-----
 examples/elementary/test_toolbar.py | 183 +++++++++++++++++++++++++++++++-----
 3 files changed, 209 insertions(+), 55 deletions(-)

diff --git a/efl/elementary/toolbar.pxd b/efl/elementary/toolbar.pxd
index ea91d61..914faf2 100644
--- a/efl/elementary/toolbar.pxd
+++ b/efl/elementary/toolbar.pxd
@@ -7,10 +7,7 @@ from libc.string cimport const_char
 cdef extern from "Elementary.h":
 
     ctypedef struct Elm_Toolbar_Item_State:
-        char *icon
-        char *label
-        Evas_Smart_Cb func
-        void *data
+        pass
 
     Evas_Object             *elm_toolbar_add(Evas_Object *parent)
     void                     elm_toolbar_icon_size_set(Evas_Object *obj, int 
icon_size)
@@ -53,12 +50,12 @@ cdef extern from "Elementary.h":
     void                     elm_toolbar_item_menu_set(Elm_Object_Item *item, 
Eina_Bool menu)
     Evas_Object             *elm_toolbar_item_menu_get(Elm_Object_Item *item)
     Elm_Toolbar_Item_State  *elm_toolbar_item_state_add(Elm_Object_Item *item, 
const_char *icon, const_char *label, Evas_Smart_Cb func, void *data)
-    # TODO: Eina_Bool                
elm_toolbar_item_state_del(Elm_Object_Item *item, Elm_Toolbar_Item_State *state)
-    # TODO: Eina_Bool                
elm_toolbar_item_state_set(Elm_Object_Item *item, Elm_Toolbar_Item_State *state)
-    # TODO: void                     
elm_toolbar_item_state_unset(Elm_Object_Item *item)
-    # TODO: Elm_Toolbar_Item_State  
*elm_toolbar_item_state_get(Elm_Object_Item *item)
-    # TODO: Elm_Toolbar_Item_State  
*elm_toolbar_item_state_next(Elm_Object_Item *item)
-    # TODO: Elm_Toolbar_Item_State  
*elm_toolbar_item_state_prev(Elm_Object_Item *item)
+    Eina_Bool                elm_toolbar_item_state_del(Elm_Object_Item *item, 
Elm_Toolbar_Item_State *state)
+    Eina_Bool                elm_toolbar_item_state_set(Elm_Object_Item *item, 
Elm_Toolbar_Item_State *state)
+    void                     elm_toolbar_item_state_unset(Elm_Object_Item 
*item)
+    Elm_Toolbar_Item_State  *elm_toolbar_item_state_get(Elm_Object_Item *item)
+    Elm_Toolbar_Item_State  *elm_toolbar_item_state_next(Elm_Object_Item *item)
+    Elm_Toolbar_Item_State  *elm_toolbar_item_state_prev(Elm_Object_Item *item)
     void                     elm_toolbar_horizontal_set(Evas_Object *obj, 
Eina_Bool horizontal)
     Eina_Bool                elm_toolbar_horizontal_get(Evas_Object *obj)
     unsigned int             elm_toolbar_items_count(Evas_Object *obj)
diff --git a/efl/elementary/toolbar.pyx b/efl/elementary/toolbar.pyx
index 5352026..330b734 100644
--- a/efl/elementary/toolbar.pyx
+++ b/efl/elementary/toolbar.pyx
@@ -172,9 +172,11 @@ cdef class ToolbarItemState(object):
 
     """A state for a :py:class:`ToolbarItem`."""
 
-    cdef Elm_Toolbar_Item_State *obj
+    cdef Elm_Toolbar_Item_State *state
     cdef object params
 
+    # XXX: Item states mess up the item data somehow, making obj_it_del_cb 
crash.
+
     def __init__(self, ToolbarItem it, icon = None, label = None, callback = 
None, *args, **kwargs):
         cdef Evas_Smart_Cb cb = NULL
 
@@ -187,25 +189,20 @@ cdef class ToolbarItemState(object):
 
         if isinstance(icon, unicode): icon = PyUnicode_AsUTF8String(icon)
         if isinstance(label, unicode): label = PyUnicode_AsUTF8String(label)
-        self.obj = elm_toolbar_item_state_add(it.item,
+        self.state = elm_toolbar_item_state_add(it.item,
             <const_char *>icon if icon is not None else NULL,
             <const_char *>label if label is not None else NULL,
             cb, <void*>self)
-        if self.obj == NULL:
+        if self.state == NULL:
             Py_DECREF(self)
 
-cdef class ToolbarItem(ObjectItem):
-
-    """
 
-    An item for the toolbar.
+cdef class ToolbarItem(ObjectItem):
 
-    """
+    """An item for the toolbar."""
 
     def __init__(self, evasObject toolbar not None, icon = None, label = None,
                  callback = None, *args, **kargs):
-
-        cdef Evas_Object *ic = NULL
         cdef Evas_Smart_Cb cb = NULL
 
         if callback is not None:
@@ -475,26 +472,45 @@ cdef class ToolbarItem(ObjectItem):
         return object_from_instance(elm_toolbar_item_menu_get(self.item))
 
 
-    #TODO def state_add(self, icon, label, func, data):
-        #return elm_toolbar_item_state_add(self.item, icon, label, func, data)
+    def state_add(self, icon=None, label=None, func=None, *args, **kwargs):
+        return ToolbarItemState(self, icon, label, func, *args, **kwargs)
+
+    def state_del(self, ToolbarItemState state):
+        if not elm_toolbar_item_state_del(self.item, state.state):
+            raise RuntimeError("Could not delete state.")
+
+    property state:
+        """An item state."""
+        def __set__(self, value):
+            self.state_set(value)
+
+        def __del__(self):
+            self.state_unset()
 
-    #TODO def state_del(self, state):
-        #return bool(elm_toolbar_item_state_del(self.item, state))
+        def __get__(self):
+            return self.state_get()
 
-    #TODO def state_set(self, state):
-        #return bool(elm_toolbar_item_state_set(self.item, state))
+    cpdef state_set(self, ToolbarItemState state):
+        if not elm_toolbar_item_state_set(self.item, state.state):
+            raise RuntimeError("Could not set state")
 
-    #TODO def state_unset(self):
-        #elm_toolbar_item_state_unset(self.item)
+    cpdef state_unset(self):
+        elm_toolbar_item_state_unset(self.item)
 
-    #TODO def state_get(self):
-        #return elm_toolbar_item_state_get(self.item)
+    cpdef state_get(self):
+        cdef ToolbarItemState ret = ToolbarItemState.__new__(ToolbarItemState)
+        ret.state = elm_toolbar_item_state_get(self.item)
+        return ret if ret.state != NULL else None
 
-    #TODO def state_next(self):
-        #return elm_toolbar_item_state_next(self.item)
+    def state_next(self):
+        cdef ToolbarItemState ret = ToolbarItemState.__new__(ToolbarItemState)
+        ret.state = elm_toolbar_item_state_next(self.item)
+        return ret if ret.state != NULL else None
 
-    #TODO def state_prev(self):
-        #return elm_toolbar_item_state_prev(self.item)
+    def state_prev(self):
+        cdef ToolbarItemState ret = ToolbarItemState.__new__(ToolbarItemState)
+        ret.state = elm_toolbar_item_state_prev(self.item)
+        return ret if ret.state != NULL else None
 
     def show(self, Elm_Toolbar_Item_Scrollto_Type scrollto_type):
         """Show this item, when the toolbar can be scrolled.
diff --git a/examples/elementary/test_toolbar.py 
b/examples/elementary/test_toolbar.py
index 1236d2b..de7ab29 100644
--- a/examples/elementary/test_toolbar.py
+++ b/examples/elementary/test_toolbar.py
@@ -1,14 +1,18 @@
 #!/usr/bin/env python
 # encoding: utf-8
 
-from efl import evas
 from efl import elementary
-from efl.elementary.window import Window
-from efl.elementary.background import Background
+from efl.elementary.window import StandardWindow
 from efl.elementary.box import Box
 from efl.elementary.photo import Photo
 from efl.elementary.table import Table
-from efl.elementary.toolbar import Toolbar
+from efl.elementary.toolbar import Toolbar, ELM_TOOLBAR_SHRINK_MENU, \
+    ELM_OBJECT_SELECT_MODE_NONE
+from efl.elementary.frame import Frame
+from efl.elementary.label import Label
+from efl.elementary.list import List
+
+from efl.evas import EVAS_HINT_EXPAND, EVAS_HINT_FILL
 
 
 def tb_1(obj, it, ph):
@@ -20,33 +24,38 @@ def tb_2(obj, it, ph):
 def tb_3(obj, it, ph):
     ph.file = "images/wood_01.jpg"
 
+def tb_3a(obj, it, ph):
+    tb_3(obj, it, ph)
+    it.state = it.state_next()
+
+def tb_3b(obj, it, ph):
+    tb_3(obj, it, ph)
+    del it.state
+
 def tb_4(obj, it, ph):
     ph.file = "images/sky_03.jpg"
 
+def tb_4a(obj, it, ph):
+    it.state = it.state_prev()
+
 def tb_5(obj, it, ph):
     ph.file = None
 
-def toolbar_clicked(obj):
-    win = Window("toolbar", elementary.ELM_WIN_BASIC)
-    win.title = "Toolbar"
+def toolbar_clicked(obj, item=None):
+    win = StandardWindow("toolbar", "Toolbar")
     win.autodel = True
     if obj is None:
         win.callback_delete_request_add(lambda o: elementary.exit())
 
-    bg = Background(win)
-    win.resize_object_add(bg)
-    bg.size_hint_weight = evas.EVAS_HINT_EXPAND, evas.EVAS_HINT_EXPAND
-    bg.show()
-
     bx = Box(win)
     win.resize_object_add(bx)
-    bx.size_hint_weight = evas.EVAS_HINT_EXPAND, evas.EVAS_HINT_EXPAND
+    bx.size_hint_weight = EVAS_HINT_EXPAND, EVAS_HINT_EXPAND
     bx.show()
 
     tb = Toolbar(win)
     tb.homogeneous = False
     tb.size_hint_weight = 0.0, 0.0
-    tb.size_hint_align = evas.EVAS_HINT_FILL, 0.0
+    tb.size_hint_align = EVAS_HINT_FILL, 0.0
 
     ph1 = Photo(win)
     ph2 = Photo(win)
@@ -78,32 +87,32 @@ def toolbar_clicked(obj):
     tb.show()
 
     tb = Table(win)
-    tb.size_hint_weight = 0.0, evas.EVAS_HINT_EXPAND
-    tb.size_hint_align = evas.EVAS_HINT_FILL, evas.EVAS_HINT_FILL
+    tb.size_hint_weight = 0.0, EVAS_HINT_EXPAND
+    tb.size_hint_align = EVAS_HINT_FILL, EVAS_HINT_FILL
 
     ph1.size = 40
     ph1.file = "images/plant_01.jpg"
-    ph1.size_hint_weight = evas.EVAS_HINT_EXPAND, evas.EVAS_HINT_EXPAND
+    ph1.size_hint_weight = EVAS_HINT_EXPAND, EVAS_HINT_EXPAND
     ph1.size_hint_align = 0.5, 0.5
     tb.pack(ph1, 0, 0, 1, 1)
     ph1.show()
 
     ph2.size = 80
-    ph2.size_hint_weight = evas.EVAS_HINT_EXPAND, evas.EVAS_HINT_EXPAND
+    ph2.size_hint_weight = EVAS_HINT_EXPAND, EVAS_HINT_EXPAND
     ph2.size_hint_align = 0.5, 0.5
     tb.pack(ph2, 1, 0, 1, 1)
     ph2.show()
 
     ph3.size = 40
     ph3.file = "images/sky_01.jpg"
-    ph3.size_hint_weight = evas.EVAS_HINT_EXPAND, evas.EVAS_HINT_EXPAND
+    ph3.size_hint_weight = EVAS_HINT_EXPAND, EVAS_HINT_EXPAND
     ph3.size_hint_align = 0.5, 0.5
     tb.pack(ph3, 0, 1, 1, 1)
     ph3.show()
 
     ph4.size = 60
     ph4.file = "images/sky_02.jpg"
-    ph4.size_hint_weight = evas.EVAS_HINT_EXPAND, evas.EVAS_HINT_EXPAND
+    ph4.size_hint_weight = EVAS_HINT_EXPAND, EVAS_HINT_EXPAND
     ph4.size_hint_align = 0.5, 0.5
     tb.pack(ph4, 1, 1, 1, 1)
     ph4.show()
@@ -115,10 +124,142 @@ def toolbar_clicked(obj):
     win.show()
 
 
+# Toolbar with multiple state buttons
+def toolbar5_clicked(obj, item=None):
+    win = StandardWindow("toolbar5", "Toolbar 5")
+    win.autodel = True
+
+    bx = Box(win)
+    bx.size_hint_weight = EVAS_HINT_EXPAND, EVAS_HINT_EXPAND
+    win.resize_object_add(bx)
+    bx.show()
+
+    tb = Toolbar(win)
+    tb.homogeneous = 0
+    tb.shrink_mode = ELM_TOOLBAR_SHRINK_MENU
+    tb.size_hint_weight = 0.0, 0.0
+    tb.size_hint_align = EVAS_HINT_FILL, 0.0
+    tb.select_mode = ELM_OBJECT_SELECT_MODE_NONE
+
+    ph1 = Photo(win)
+    ph2 = Photo(win)
+    ph3 = Photo(win)
+    ph4 = Photo(win)
+
+    tb_it = tb.item_append("document-print", "Hello", tb_1, ph1)
+    tb_it.disabled = True
+    tb_it.priority = 100
+
+    tb_it = tb.item_append("images/icon_04.png", "World", tb_2, ph1)
+    tb_it.priority = -100
+
+    tb_it = tb.item_append("object-rotate-right", "H", tb_3a, ph4)
+    tb_it.state_add("object-rotate-left", "H2", tb_3b, ph4)
+    tb_it.priority = 150
+
+    tb_it = tb.item_append("mail-send", "Comes", tb_4a, ph4)
+    tb_it.state_add("emptytrash", "Comes2", tb_4a, ph4)
+    tb_it.state_add("trashcan_full", "Comes3", tb_4a, ph4)
+    tb_it.priority = 0
+
+    tb_it = tb.item_append("clock", "Elementary", tb_5, ph4)
+    tb_it.priority = -200
+
+    tb_it = tb.item_append("refresh", "Menu")
+    tb_it.menu = True
+    tb_it.priority = -9999
+    tb.menu_parent = win
+    menu = tb_it.menu
+
+    menu.item_add(None, "edit-cut", "Shrink", tb_3, ph4)
+    menu_it = menu.item_add(None, "edit-copy", "Mode", tb_4, ph4)
+    menu.item_add(menu_it, "edit-paste", "is set to", tb_4, ph4)
+    menu.item_add(None, "edit-delete", "Menu", tb_5, ph4)
+
+    bx.pack_end(tb)
+    tb.show()
+
+    tb = Table(win)
+    tb.size_hint_weight = 0.0, EVAS_HINT_EXPAND
+    tb.size_hint_align = EVAS_HINT_FILL, EVAS_HINT_FILL
+
+    ph = ph1
+    ph.size = 40
+    ph.file = "images/plant_01.jpg"
+    ph.size_hint_weight = EVAS_HINT_EXPAND, EVAS_HINT_EXPAND
+    ph.size_hint_align = 0.5, 0.5
+    tb.pack(ph, 0, 0, 1, 1)
+    ph.show()
+
+    ph = ph2
+    ph.size = 80
+    ph.size_hint_weight = EVAS_HINT_EXPAND, EVAS_HINT_EXPAND
+    ph.size_hint_align = 0.5, 0.5
+    tb.pack(ph, 1, 0, 1, 1)
+    ph.show()
+
+    ph = ph3
+    ph.size = 20
+    ph.file = "images/sky_01.jpg"
+    ph.size_hint_weight = EVAS_HINT_EXPAND, EVAS_HINT_EXPAND
+    ph.size_hint_align = 0.5, 0.5
+    tb.pack(ph, 0, 1, 1, 1)
+    ph.show()
+
+    ph = ph4
+    ph.size = 60
+    ph.file = "images/sky_02.jpg"
+    ph.size_hint_weight = EVAS_HINT_EXPAND, EVAS_HINT_EXPAND
+    ph.size_hint_align = 0.5, 0.5
+    tb.pack(ph, 1, 1, 1, 1)
+    ph.show()
+
+    bx.pack_end(tb)
+    tb.show()
+
+    win.size = 320, 300
+    win.show()
+
+
 if __name__ == "__main__":
     elementary.init()
+    win = StandardWindow("test", "python-elementary test application")
+    win.callback_delete_request_add(lambda o: elementary.exit())
+
+    box0 = Box(win)
+    box0.size_hint_weight_set(EVAS_HINT_EXPAND, EVAS_HINT_EXPAND)
+    win.resize_object_add(box0)
+    box0.show()
 
-    toolbar_clicked(None)
+    fr = Frame(win)
+    fr.text_set("Information")
+    box0.pack_end(fr)
+    fr.show()
 
+    lb = Label(win)
+    lb.text_set("Please select a test from the list below<br>"
+                 "by clicking the test button to show the<br>"
+                 "test window.")
+    fr.content_set(lb)
+    lb.show()
+
+    items = [
+        ("Toolbar", toolbar_clicked),
+        ("Toolbar Item States", toolbar5_clicked),
+    ]
+
+    li = List(win)
+    li.size_hint_weight_set(EVAS_HINT_EXPAND, EVAS_HINT_EXPAND)
+    li.size_hint_align_set(EVAS_HINT_FILL, EVAS_HINT_FILL)
+    box0.pack_end(li)
+    li.show()
+
+    for item in items:
+        li.item_append(item[0], callback=item[1])
+
+    li.go()
+
+    win.resize(320,520)
+    win.show()
     elementary.run()
     elementary.shutdown()

-- 

------------------------------------------------------------------------------
Precog is a next-generation analytics platform capable of advanced
analytics on semi-structured data. The platform includes APIs for building
apps and a phenomenal toolset for data science. Developers can use
our toolset for easy data analysis & visualization. Get a free account!
http://www2.precog.com/precogplatform/slashdotnewsletter

Reply via email to