kuuko pushed a commit to branch master.

commit e0e2cb81cea9bd6af128bb54145c8a087678a302
Author: Kai Huuhko <[email protected]>
Date:   Sun Apr 21 20:28:50 2013 +0000

    Elementary: Fix Toolbar states.
---
 efl/elementary/object_item.pyx | 19 ++++++++++++++++---
 efl/elementary/toolbar.pyx     | 27 ++++++++++++++++++++++++---
 examples/elementary/test.py    |  1 +
 3 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/efl/elementary/object_item.pyx b/efl/elementary/object_item.pyx
index 0af6a46..84b37ff 100644
--- a/efl/elementary/object_item.pyx
+++ b/efl/elementary/object_item.pyx
@@ -61,9 +61,11 @@ cdef _object_item_list_to_python(const_Eina_List *lst):
     return ret
 
 cdef void _object_item_del_cb(void *data, Evas_Object *o, void *event_info) 
with gil:
-    cdef ObjectItem d = <object>data
-    d.item = NULL
-    Py_DECREF(d)
+    cdef ObjectItem d
+    if data != NULL:
+        d = <object>data
+        d.item = NULL
+        Py_DECREF(d)
 
 cdef void _object_item_callback(void *data, Evas_Object *obj, void 
*event_info) with gil:
     cdef ObjectItem item = <object>data
@@ -115,6 +117,17 @@ cdef class ObjectItem(object):
         Py_INCREF(self)
         return 1
 
+    def __str__(self):
+        return ("ObjectItem(class=%s, obj=%#x, refcount=%d)") % \
+                (type(self).__name__, <unsigned long>self.item,
+                 PY_REFCOUNT(self))
+
+    def __repr__(self):
+        return ("ObjectItem(class=%s, obj=%#x, refcount=%d)") % \
+                (type(self).__name__, <unsigned long>self.item,
+                 PY_REFCOUNT(self))
+
+
     property widget:
         """Get the widget object's handle which contains a given item
 
diff --git a/efl/elementary/toolbar.pyx b/efl/elementary/toolbar.pyx
index 382d248..1e4ca58 100644
--- a/efl/elementary/toolbar.pyx
+++ b/efl/elementary/toolbar.pyx
@@ -174,6 +174,23 @@ ELM_TOOLBAR_ITEM_SCROLLTO_FIRST = 
enums.ELM_TOOLBAR_ITEM_SCROLLTO_FIRST
 ELM_TOOLBAR_ITEM_SCROLLTO_MIDDLE = enums.ELM_TOOLBAR_ITEM_SCROLLTO_MIDDLE
 ELM_TOOLBAR_ITEM_SCROLLTO_LAST = enums.ELM_TOOLBAR_ITEM_SCROLLTO_LAST
 
+
+import traceback
+
+cdef void _toolbar_item_state_callback(void *data, Evas_Object *obj, void 
*event_info) with gil:
+    cdef ToolbarItemState state = <object>data
+    cdef ToolbarItem item = ToolbarItem.__new__(ToolbarItem)
+    item.item = <Elm_Object_Item *>event_info
+    (callback, a, ka) = state.params
+    try:
+        o = object_from_instance(obj)
+        callback(o, item, *a, **ka)
+    except Exception as e:
+        traceback.print_exc()
+
+    # The C item will be freed unless this is done
+    item.item = NULL
+
 cdef class ToolbarItemState(object):
 
     """A state for a :py:class:`ToolbarItem`."""
@@ -181,15 +198,13 @@ cdef class ToolbarItemState(object):
     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
 
         if callback:
             if not callable(callback):
                 raise TypeError("callback is not callable")
-            cb = _object_item_callback
+            cb = _toolbar_item_state_callback
 
         self.params = (callback, args, kwargs)
 
@@ -202,6 +217,12 @@ cdef class ToolbarItemState(object):
         if self.state == NULL:
             Py_DECREF(self)
 
+        Py_INCREF(self)
+
+    def delete(self):
+        self.state = NULL
+        Py_DECREF(self)
+
 
 cdef class ToolbarItem(ObjectItem):
 
diff --git a/examples/elementary/test.py b/examples/elementary/test.py
index 49e55cc..f116252 100755
--- a/examples/elementary/test.py
+++ b/examples/elementary/test.py
@@ -160,6 +160,7 @@ items = [
         ]),
         ("Toolbars", [
             ("Toolbar", "test_toolbar", "toolbar_clicked"),
+            ("Toolbar Item States", "test_toolbar", "toolbar5_clicked"),
         ]),
          ("Web", [
             ("Web", "test_web", "web_clicked"),

-- 

------------------------------------------------------------------------------
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