summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYakov Goldberg <yakov.g@samsung.com>2012-12-04 18:39:32 +0200
committerYakov Goldberg <yakov.g@samsung.com>2012-12-04 18:39:32 +0200
commit6c905f1f8e734066161a5490f41a81c514de163a (patch)
treea486fc3d54d24ba7acbd3e68bc1a7f21a5a5da9f
parent06d5eccf1da69db1a0a537054e3d4ebbce26ce72 (diff)
Added data to python callback
Now: to add callback to python object, callback object needed to be created, which is tuple (func, data) cb_obj = (func, data) Now callback can be added to object or deleted: button.event_callback_priority_add(CLICKED, 0, cb_obj) button.event_callback_del(CLICKED, cb_obj) Case like this: button.event_callback_priority_add(CLICKED, 0, (func, data)) will also work out, because reference to object is incremented. But there will be imposiible to delete such callback. Signed-off-by: Yakov Goldberg <yakov.g@samsung.com>
-rw-r--r--eo_py/eodefault.pyx7
-rw-r--r--eoparser/xmlparser.py6
-rw-r--r--examples/evas_test2.py153
3 files changed, 163 insertions, 3 deletions
diff --git a/eo_py/eodefault.pyx b/eo_py/eodefault.pyx
index 530fe19..2a9ac00 100644
--- a/eo_py/eodefault.pyx
+++ b/eo_py/eodefault.pyx
@@ -150,8 +150,11 @@ cdef Eina_Bool _object_callback(void *data, Eo *o,
150 eodefault.eo_do(o, eodefault.EO_BASE_BASE_ID+eodefault.EO_BASE_SUB_ID_DATA_GET, <const_char_ptr>EoDefault.PY_EO_NAME, &c_py_obj) 150 eodefault.eo_do(o, eodefault.EO_BASE_BASE_ID+eodefault.EO_BASE_SUB_ID_DATA_GET, <const_char_ptr>EoDefault.PY_EO_NAME, &c_py_obj)
151 py_obj = <object>c_py_obj 151 py_obj = <object>c_py_obj
152 152
153 f = <object>data 153 #cb_struct is tuple: (func, data)
154 res = f(py_obj) 154 cb_struct = <object>data
155 f = cb_struct[0]
156 cb_data = cb_struct[1]
157 res = f(py_obj, cb_data)
155 158
156 if res == CALLBACK_STOP: 159 if res == CALLBACK_STOP:
157 return EINA_FALSE 160 return EINA_FALSE
diff --git a/eoparser/xmlparser.py b/eoparser/xmlparser.py
index ea1beeb..7b33b0f 100644
--- a/eoparser/xmlparser.py
+++ b/eoparser/xmlparser.py
@@ -857,9 +857,10 @@ class PyVisitor(Visitor):
857 return 857 return
858 if _o.op_id == "EO_BASE_SUB_ID_EVENT_CALLBACK_PRIORITY_ADD": 858 if _o.op_id == "EO_BASE_SUB_ID_EVENT_CALLBACK_PRIORITY_ADD":
859 function_lines.append("def event_callback_priority_add(self, long _desc, int _priority, object _cb):") 859 function_lines.append("def event_callback_priority_add(self, long _desc, int _priority, object _cb):")
860 function_lines.append(" if not callable(_cb):") 860 function_lines.append(" if not callable(_cb[0]):")
861 function_lines.append(" raise TypeError(\"func must be callable\")") 861 function_lines.append(" raise TypeError(\"func must be callable\")")
862 function_lines.append(" cdef Eo_Event_Cb cb = <Eo_Event_Cb> eodefault._object_callback") 862 function_lines.append(" cdef Eo_Event_Cb cb = <Eo_Event_Cb> eodefault._object_callback")
863 function_lines.append(" Py_INCREF(_cb)")
863 function_lines.append(" eodefault.eo_do(eodefault._eo_instance_get(self), eobase_sub_id(eobase.EO_BASE_SUB_ID_EVENT_CALLBACK_PRIORITY_ADD), _desc, _priority, cb, <void*>_cb)") 864 function_lines.append(" eodefault.eo_do(eodefault._eo_instance_get(self), eobase_sub_id(eobase.EO_BASE_SUB_ID_EVENT_CALLBACK_PRIORITY_ADD), _desc, _priority, cb, <void*>_cb)")
864 if_ret = True 865 if_ret = True
865 866
@@ -867,6 +868,7 @@ class PyVisitor(Visitor):
867 function_lines.append("def event_callback_del(self, long _desc, object _func):") 868 function_lines.append("def event_callback_del(self, long _desc, object _func):")
868 function_lines.append(" cdef Eo_Event_Cb func = <Eo_Event_Cb> eodefault._object_callback") 869 function_lines.append(" cdef Eo_Event_Cb func = <Eo_Event_Cb> eodefault._object_callback")
869 function_lines.append(" eodefault.eo_do(eodefault._eo_instance_get(self), eobase_sub_id(eobase.EO_BASE_SUB_ID_EVENT_CALLBACK_DEL), _desc, func, <void*>_func)") 870 function_lines.append(" eodefault.eo_do(eodefault._eo_instance_get(self), eobase_sub_id(eobase.EO_BASE_SUB_ID_EVENT_CALLBACK_DEL), _desc, func, <void*>_func)")
871 function_lines.append(" Py_DECREF(_func)")
870 872
871 function_lines.append("\n") 873 function_lines.append("\n")
872 if_ret = True 874 if_ret = True
@@ -1075,6 +1077,8 @@ class PyVisitor(Visitor):
1075 parents.append(self.eodefault["name"]) 1077 parents.append(self.eodefault["name"])
1076 l = "from %s import %s"%(self.eodefault["module"], self.eodefault["name"]) 1078 l = "from %s import %s"%(self.eodefault["module"], self.eodefault["name"])
1077 self.pxi.head.append(l + "\n") 1079 self.pxi.head.append(l + "\n")
1080 l = "from cpython cimport Py_INCREF, Py_DECREF"
1081 self.pxi.head.append(l + "\n")
1078 1082
1079 if "EoBase" in parents: 1083 if "EoBase" in parents:
1080 l = "from %s import %s"%("eobase", "EoBase") 1084 l = "from %s import %s"%("eobase", "EoBase")
diff --git a/examples/evas_test2.py b/examples/evas_test2.py
new file mode 100644
index 0000000..4f3080e
--- /dev/null
+++ b/examples/evas_test2.py
@@ -0,0 +1,153 @@
1# -*- coding: UTF-8 -*-
2import sys
3
4from base.eodefault import py_elm_init, elem_run
5from eobase import EoBase
6from evas.py2.evas_py import ElwWin, ElwBox, ElwButton, ElwBoxedbutton, ExEvasObject
7
8
9lst = [56, "elm", 56.76]
10
11print "Initializing Elementary..."
12print "Init res:", py_elm_init(sys.argv)
13print ""
14
15global_obj = None
16
17
18def freeze_me(obj):
19 global global_obj
20 global_obj = obj
21 obj.event_freeze()
22 obj.elw_alert_alert("obl: " + obj.text_get() + "; freezing events; count: " + str(obj.event_freeze_get()))
23
24
25def thaw_global(obj):
26 global global_obj
27 if global_obj.event_freeze_get() == 0:
28 return
29 obj.alert("thawing global")
30 global_obj.event_thaw()
31
32
33######################## setting data ##########################
34def properties_cb(obj, data):
35 a,b,c,d = obj.exevas_obj_color_get()
36 print "color : ", a, b, c, d
37 ww,hh = obj.exevas_obj_size_get()
38 xx,yy = obj.exevas_obj_position_get()
39 print "size : " + str((ww, hh)) + "; pos: " + str((xx, yy))
40
41 obj.exevas_obj_size_set(ww-1, hh)
42 obj.exevas_obj_color_set(a-5, b, c-10, d)
43
44######################## setting data ##########################
45def data_cb(obj, data):
46 print "setting data - int : 5"
47 i = 5
48 obj.data_set("int", i)
49 i = 999999
50
51 f = obj.data_get("int")
52 print "getting data"
53 print f, type(f)
54 f = obj.data_del("int")
55
56#ebb.data_set("list", ["a", "bb", "cc", "asdfasfd", "1234124", "asdf", "123", "12"])
57 #ebb.data_set("list", ["xx", "yy", "zz"])
58 #f = ebb.data_get("list")
59 #print f, type(f)
60
61#l = ["a", "b", "c", "asd", "123"]
62#ebb.data_set("list2", l)
63#l[0] = "12341234124"
64#del l
65# f = obj.data_get("list2")
66# print f, type(f)
67
68 print "setting data - char : string data"
69 obj.data_set("char", "string data")
70 f = obj.data_get("char")
71 print f, type(f)
72
73 obj.data_set("object",pb)
74 f = obj.data_get("object")
75 print "getting data"
76 print f, type(f)
77#f.text_set("neeew name")
78#f = ebb.data_del("object")
79
80
81
82
83def cb_add(o, data):
84 o.elw_alert_alert(" obj: " + o.elw_button_text_get() + "; cb_added")
85
86def cb_del(o, data):
87 o.elw_alert_alert(" obj: " + o.elw_button_text_get() + "; cb_deleted")
88
89def thaw_ba(o, data):
90 print " size:", o.size_get()
91 p_but.event_thaw()
92
93def cb_clicked(o, data):
94 print o
95 o.elw_alert_alert(" obj: " + o.elw_button_text_get())
96 print "=== data ==="
97 d = data
98 print d
99 print "======"
100 lst.append("!")
101
102
103w1 = ElwWin(None)
104w1.exevas_obj_size_set(370, 350)
105w1.exevas_obj_visibility_set(1)
106
107
108w = w1
109
110bt = ElwButton(w)
111bt.exevas_obj_position_set(30, 30)
112bt.exevas_obj_size_set(210, 60)
113bt.exevas_obj_color_set(159, 245, 255, 255)
114bt.elw_button_text_set("Hello")
115bt.exevas_obj_visibility_set(1)
116
117print "\n> adding cb on CALLBACK_ADD"
118cb_obj1 = (cb_add, None)
119bt.event_callback_priority_add(EoBase.CALLBACK_ADD, -100, cb_obj1)
120print "\n> adding cb on CALLBACK_DEL"
121cb_obj2 = (cb_del, None)
122bt.event_callback_priority_add(EoBase.CALLBACK_DEL, 0, cb_obj2)
123print "\n> adding cb on CLICKED"
124
125cb_obj3 = (cb_clicked, None)
126bt.event_callback_priority_add(ElwButton.CLICKED, -100, cb_obj3)
127#bt.event_callback_priority_add(ElwButton.CLICKED, -200, thaw_ba)
128print "\n> adding cb on CLICKED"
129cb_obj4 = (properties_cb, None)
130bt.event_callback_priority_add(ElwButton.CLICKED, -200, cb_obj4)
131
132del w
133w = bt.parent_get()
134print type(w)
135
136ba = ElwButton(w);
137ba.exevas_obj_visibility_set(1)
138ba.exevas_obj_position_set(30, 100)
139ba.exevas_obj_size_set(100, 50)
140ba.exevas_obj_color_set(255, 0, 255, 255)
141ba.elw_button_text_set("(B) ADD cb")
142
143
144
145cb_obj5 = (cb_clicked, lst)
146print cb_obj5
147ba.event_callback_priority_add(ElwButton.CLICKED, 0, cb_obj5)
148ba.event_callback_del(ElwButton.CLICKED, cb_obj5)
149
150print "Running Elementary..."
151
152elem_run()
153