and improve factory massively.. no more "hangs" while scrolling massiv
[framework/uifw/elementary.git] / src / lib / elm_factory.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 typedef struct _Widget_Data Widget_Data;
5
6 struct _Widget_Data
7 {
8    Evas_Object *obj;
9    Evas_Object *content;
10    int last_calc_count; 
11    Eina_Bool eval : 1;
12 };
13
14 static const char *widtype = NULL;
15 static void _del_hook(Evas_Object *obj);
16 static Eina_Bool _focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next);
17 static void _sizing_eval(Evas_Object *obj);
18 static void _eval(Evas_Object *obj);
19 static void _changed(Evas_Object *obj);
20 static void _move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__);
21 static void _resize(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__);
22 static void _child_change(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__);
23 static void _child_del(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__);
24
25 static const char SIG_REALIZE[] = "realize";
26 static const char SIG_UNREALIZE[] = "unrealize";
27
28 static const Evas_Smart_Cb_Description _signals[] = {
29    {SIG_REALIZE, ""},
30    {SIG_UNREALIZE, ""},
31    {NULL, NULL}
32 };
33
34 static void
35 _del_hook(Evas_Object *obj)
36 {
37    Widget_Data *wd = elm_widget_data_get(obj);
38    if (!wd) return;
39    free(wd);
40 }
41
42 static Eina_Bool
43 _focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
44 {
45    Widget_Data *wd = elm_widget_data_get(obj);
46    Evas_Object *cur;
47
48    if ((!wd) || (!wd->content)) return EINA_FALSE;
49    cur = wd->content;
50    return elm_widget_focus_next_get(cur, dir, next);
51 }
52
53 static void
54 _sizing_eval(Evas_Object *obj)
55 {
56    Widget_Data *wd = elm_widget_data_get(obj);
57    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
58
59    if (!wd) return;
60    if (!wd->content) return;
61    evas_object_size_hint_min_get(wd->content, &minw, &minh);
62    evas_object_size_hint_max_get(wd->content, &maxw, &maxh);
63    evas_object_size_hint_min_set(obj, minw, minh);
64    evas_object_size_hint_max_set(obj, maxw, maxh);
65 }
66
67 static void
68 _eval(Evas_Object *obj)
69 {
70    Evas_Coord x, y, w, h, cvx, cvy, cvw, cvh;
71    Widget_Data *wd = elm_widget_data_get(obj);
72    if (!wd) return;
73
74    evas_event_freeze(evas_object_evas_get(obj));
75    evas_object_geometry_get(obj, &x, &y, &w, &h);
76    if (w < 1) w = 1;
77    if (h < 1) h = 1;
78    evas_output_viewport_get(evas_object_evas_get(obj),
79                             &cvx, &cvy, &cvw, &cvh);
80    if ((cvw < 1) || (cvh < 1)) return;
81    // need some fuzz value thats beyond the current viewport
82    // for now just make it the viewport * 3 in size (so 1 vp in each direction)
83    /*
84    cvx -= cvw;
85    cvy -= cvh;
86    cvw *= 3;
87    cvh *= 3;
88     */
89    if (ELM_RECTS_INTERSECT(x, y, w, h, cvx, cvy, cvw, cvh))
90      {
91         if (!wd->content)
92           {
93              evas_object_smart_callback_call(obj, SIG_REALIZE, NULL);
94              if (wd->content)
95                {
96                   if (evas_object_smart_data_get(wd->content))
97                      evas_object_smart_calculate(wd->content);
98                }
99              wd->last_calc_count = 
100                 evas_smart_objects_calculate_count_get(evas_object_evas_get(obj));
101           }
102      }
103    else
104      {
105         if (wd->content)
106           {
107              if (wd->last_calc_count != 
108                 evas_smart_objects_calculate_count_get(evas_object_evas_get(obj)))
109                 evas_object_smart_callback_call(obj, SIG_UNREALIZE, NULL);
110           }
111      }
112    evas_event_thaw(evas_object_evas_get(obj));
113    evas_event_thaw_eval(evas_object_evas_get(obj));
114 }
115
116 static void
117 _changed(Evas_Object *obj)
118 {
119    Widget_Data *wd = elm_widget_data_get(obj);
120    if (!wd) return;
121    if (wd->eval)
122      {
123         _eval(obj);
124         _sizing_eval(obj);
125         wd->eval = EINA_FALSE;
126      }
127 }
128
129 static void
130 _move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
131 {
132    Widget_Data *wd = elm_widget_data_get(obj);
133    if (!wd) return;
134    wd->eval = EINA_TRUE;
135    evas_object_smart_changed(obj);
136 }
137
138 static void
139 _resize(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
140 {
141    Widget_Data *wd = elm_widget_data_get(obj);
142    if (!wd) return;
143    wd->eval = EINA_TRUE;
144    evas_object_smart_changed(obj);
145 }
146
147 static void
148 _child_change(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
149 {
150    Widget_Data *wd = elm_widget_data_get(data);
151    if (!wd) return;
152    wd->eval = EINA_TRUE;
153    evas_object_smart_changed(data);
154 }
155
156 static void
157 _child_del(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
158 {
159    Evas_Object *fobj = data;
160    Widget_Data *wd = elm_widget_data_get(fobj);
161    if (!wd) return;
162    if (wd->content != obj) return;
163    evas_object_event_callback_del_full(wd->content,
164                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
165                                        _child_change, obj);
166    evas_object_event_callback_del_full(wd->content,
167                                        EVAS_CALLBACK_DEL,
168                                        _child_del, obj);
169    wd->content = NULL;
170 }
171
172 EAPI Evas_Object *
173 elm_factory_add(Evas_Object *parent)
174 {
175    Evas_Object *obj;
176    Evas *e;
177    Widget_Data *wd;
178
179    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
180
181    ELM_SET_WIDTYPE(widtype, "factory");
182    elm_widget_type_set(obj, "factory");
183    elm_widget_sub_object_add(parent, obj);
184    elm_widget_data_set(obj, wd);
185    elm_widget_del_hook_set(obj, _del_hook);
186    elm_widget_focus_next_hook_set(obj, _focus_next_hook);
187    elm_widget_can_focus_set(obj, EINA_FALSE);
188    elm_widget_changed_hook_set(obj, _changed);
189
190    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _move, NULL);
191    evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE, _resize, NULL);
192
193    evas_object_smart_callbacks_descriptions_set(obj, _signals);
194
195    wd->obj = obj;
196    wd->last_calc_count = -1;
197    return obj;
198 }
199
200 EAPI void
201 elm_factory_content_set(Evas_Object *obj, Evas_Object *content)
202 {
203    ELM_CHECK_WIDTYPE(obj, widtype);
204    Widget_Data *wd = elm_widget_data_get(obj);
205    if (!wd) return;
206    if (wd->content == content) return;
207    if (wd->content) evas_object_del(wd->content);
208    wd->content = content;
209    elm_widget_resize_object_set(obj, wd->content);
210    evas_object_event_callback_add(wd->content, EVAS_CALLBACK_DEL,
211                                   _child_del, obj);
212    evas_object_event_callback_add(wd->content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
213                                   _child_change, obj);
214    wd->eval = EINA_TRUE;
215    evas_object_smart_changed(obj);
216 }
217
218 EAPI Evas_Object *
219 elm_factory_content_get(const Evas_Object *obj)
220 {
221    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
222    Widget_Data *wd = elm_widget_data_get(obj);
223    if (!wd) return NULL;
224    return wd->content;
225 }