Merge "[*][factory] add from upstream elementary"
[framework/uifw/elementary.git] / src / lib / elm_factory.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 // FIXME: handle if canvas resizes
5
6 typedef struct _Widget_Data Widget_Data;
7
8 struct _Widget_Data
9 {
10    Evas_Object *obj;
11    Evas_Object *content;
12    int last_calc_count;
13    Evas_Coord maxminw, maxminh;
14    Eina_Bool eval : 1;
15    Eina_Bool szeval : 1;
16    Eina_Bool maxmin : 1;
17 };
18
19 static const char *widtype = NULL;
20 static void _del_hook(Evas_Object *obj);
21 static Eina_Bool _focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next);
22 static void _sizing_eval(Evas_Object *obj);
23 static void _eval(Evas_Object *obj);
24 static void _changed(Evas_Object *obj);
25 static void _move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__);
26 static void _resize(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__);
27 static void _child_change(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__);
28 static void _child_del(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__);
29
30 static const char SIG_REALIZE[] = "realize";
31 static const char SIG_UNREALIZE[] = "unrealize";
32
33 static const Evas_Smart_Cb_Description _signals[] = {
34    {SIG_REALIZE, ""},
35    {SIG_UNREALIZE, ""},
36    {NULL, NULL}
37 };
38
39 static int fac = 0;
40
41 static void
42 _del_hook(Evas_Object *obj)
43 {
44    Widget_Data *wd = elm_widget_data_get(obj);
45    if (!wd) return;
46    if (wd->content)
47      {
48         Evas_Object *o = wd->content;
49
50         evas_object_event_callback_del_full(o,
51                                             EVAS_CALLBACK_CHANGED_SIZE_HINTS,
52                                             _child_change, obj);
53         evas_object_event_callback_del_full(o,
54                                             EVAS_CALLBACK_DEL,
55                                             _child_del, obj);
56         wd->content = NULL;
57         evas_object_del(o);
58         fac--;
59 //        printf("FAC-- = %i\n", fac);
60      }
61    free(wd);
62 }
63
64 static Eina_Bool
65 _focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
66 {
67    Widget_Data *wd = elm_widget_data_get(obj);
68    Evas_Object *cur;
69
70    if ((!wd) || (!wd->content)) return EINA_FALSE;
71    cur = wd->content;
72    return elm_widget_focus_next_get(cur, dir, next);
73 }
74
75 static void
76 _sizing_eval(Evas_Object *obj)
77 {
78    Widget_Data *wd = elm_widget_data_get(obj);
79    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
80
81    if (!wd) return;
82    if (!wd->content) return;
83    evas_object_size_hint_min_get(wd->content, &minw, &minh);
84    evas_object_size_hint_max_get(wd->content, &maxw, &maxh);
85    if (wd->maxmin)
86      {
87         if (minw > wd->maxminw) wd->maxminw = minw;
88         if (minh > wd->maxminh) wd->maxminh = minh;
89         evas_object_size_hint_min_set(obj, wd->maxminw, wd->maxminh);
90      }
91    else
92      {
93         evas_object_size_hint_min_set(obj, minw, minh);
94      }
95    evas_object_size_hint_max_set(obj, maxw, maxh);
96 //   printf("FAC SZ: %i %i | %i %i\n", minw, minh, maxw, maxh);
97 }
98
99 static void
100 _eval(Evas_Object *obj)
101 {
102    Evas_Coord x, y, w, h, cvx, cvy, cvw, cvh;
103    Widget_Data *wd = elm_widget_data_get(obj);
104    if (!wd) return;
105
106    evas_event_freeze(evas_object_evas_get(obj));
107    evas_object_geometry_get(obj, &x, &y, &w, &h);
108    if (w < 1) w = 1;
109    if (h < 1) h = 1;
110    evas_output_viewport_get(evas_object_evas_get(obj),
111                             &cvx, &cvy, &cvw, &cvh);
112    if ((cvw < 1) || (cvh < 1)) return;
113    // need some fuzz value thats beyond the current viewport
114    // for now just make it the viewport * 3 in size (so 1 vp in each direction)
115    /*
116    cvx -= cvw;
117    cvy -= cvh;
118    cvw *= 3;
119    cvh *= 3;
120     */
121    if (ELM_RECTS_INTERSECT(x, y, w, h, cvx, cvy, cvw, cvh))
122      {
123         if (!wd->content)
124           {
125 //             printf("                 + %i %i %ix%i <> %i %i %ix%i\n", x, y, w, h, cvx, cvy, cvw, cvh);
126              evas_object_smart_callback_call(obj, SIG_REALIZE, NULL);
127              if (wd->content)
128                {
129                   if (evas_object_smart_data_get(wd->content))
130                      evas_object_smart_calculate(wd->content);
131                }
132              wd->last_calc_count =
133                 evas_smart_objects_calculate_count_get(evas_object_evas_get(obj));
134           }
135      }
136    else
137      {
138         if (wd->content)
139           {
140              if (wd->last_calc_count !=
141                 evas_smart_objects_calculate_count_get(evas_object_evas_get(obj)))
142                 evas_object_smart_callback_call(obj, SIG_UNREALIZE, NULL);
143           }
144      }
145    evas_event_thaw(evas_object_evas_get(obj));
146    evas_event_thaw_eval(evas_object_evas_get(obj));
147 }
148
149 static void
150 _changed(Evas_Object *obj)
151 {
152    Widget_Data *wd = elm_widget_data_get(obj);
153    if (!wd) return;
154    if (wd->eval)
155      {
156         _eval(obj);
157         wd->eval = EINA_FALSE;
158      }
159    if (wd->szeval)
160      {
161         _sizing_eval(obj);
162         wd->szeval = EINA_FALSE;
163      }
164 }
165
166 static void
167 _move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
168 {
169    Widget_Data *wd = elm_widget_data_get(obj);
170    if (!wd) return;
171    wd->eval = EINA_TRUE;
172    evas_object_smart_changed(obj);
173 }
174
175 static void
176 _resize(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
177 {
178    Widget_Data *wd = elm_widget_data_get(obj);
179    if (!wd) return;
180    wd->eval = EINA_TRUE;
181    evas_object_smart_changed(obj);
182 }
183
184 static void
185 _child_change(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
186 {
187    Widget_Data *wd = elm_widget_data_get(data);
188    if (!wd) return;
189    wd->eval = EINA_TRUE;
190    wd->szeval = EINA_TRUE;
191    evas_object_smart_changed(data);
192 }
193
194 static void
195 _child_del(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
196 {
197    Evas_Object *fobj = data;
198    Widget_Data *wd = elm_widget_data_get(fobj);
199    if (!wd) return;
200    if (wd->content != obj) return;
201    evas_object_event_callback_del_full(wd->content,
202                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
203                                        _child_change, obj);
204    evas_object_event_callback_del_full(wd->content,
205                                        EVAS_CALLBACK_DEL,
206                                        _child_del, obj);
207    wd->content = NULL;
208    fac--;
209 //   printf("FAC-- = %i\n", fac);
210 }
211
212 EAPI Evas_Object *
213 elm_factory_add(Evas_Object *parent)
214 {
215    Evas_Object *obj;
216    Evas *e;
217    Widget_Data *wd;
218
219    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
220
221    ELM_SET_WIDTYPE(widtype, "factory");
222    elm_widget_type_set(obj, "factory");
223    elm_widget_sub_object_add(parent, obj);
224    elm_widget_data_set(obj, wd);
225    elm_widget_del_hook_set(obj, _del_hook);
226    elm_widget_focus_next_hook_set(obj, _focus_next_hook);
227    elm_widget_can_focus_set(obj, EINA_FALSE);
228    elm_widget_changed_hook_set(obj, _changed);
229
230    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _move, NULL);
231    evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE, _resize, NULL);
232
233    evas_object_smart_callbacks_descriptions_set(obj, _signals);
234
235    wd->obj = obj;
236    wd->last_calc_count = -1;
237    return obj;
238 }
239
240 EAPI void
241 elm_factory_content_set(Evas_Object *obj, Evas_Object *content)
242 {
243    ELM_CHECK_WIDTYPE(obj, widtype);
244    Widget_Data *wd = elm_widget_data_get(obj);
245    if (!wd) return;
246    if (wd->content == content) return;
247    if (wd->content)
248       {
249          Evas_Object *o = wd->content;
250
251          evas_object_event_callback_del_full(wd->content,
252                                              EVAS_CALLBACK_CHANGED_SIZE_HINTS,
253                                              _child_change, obj);
254          evas_object_event_callback_del_full(wd->content,
255                                              EVAS_CALLBACK_DEL,
256                                              _child_del, obj);
257          wd->content = NULL;
258          evas_object_del(o);
259          fac--;
260 //         printf("FAC-- = %i\n", fac);
261       }
262    wd->content = content;
263    if (wd->content)
264      {
265         fac++;
266 //        printf("FAC++ = %i\n", fac);
267         elm_widget_resize_object_set(obj, wd->content);
268         evas_object_event_callback_add(wd->content, EVAS_CALLBACK_DEL,
269                                        _child_del, obj);
270         evas_object_event_callback_add(wd->content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
271                                        _child_change, obj);
272         wd->eval = EINA_TRUE;
273         wd->szeval = EINA_TRUE;
274         evas_object_smart_changed(obj);
275      }
276 }
277
278 EAPI Evas_Object *
279 elm_factory_content_get(const Evas_Object *obj)
280 {
281    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
282    Widget_Data *wd = elm_widget_data_get(obj);
283    if (!wd) return NULL;
284    return wd->content;
285 }
286
287 EAPI void
288 elm_factory_maxmin_mode_set(Evas_Object *obj, Eina_Bool enabled)
289 {
290    ELM_CHECK_WIDTYPE(obj, widtype);
291    Widget_Data *wd = elm_widget_data_get(obj);
292    if (!wd) return;
293    wd->maxmin = !!enabled;
294 }
295
296 EAPI Eina_Bool
297 elm_factory_maxmin_mode_get(const Evas_Object *obj)
298 {
299    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
300    Widget_Data *wd = elm_widget_data_get(obj);
301    if (!wd) return EINA_FALSE;
302    return wd->maxmin;
303 }
304
305 EAPI void
306 elm_factory_maxmin_reset_set(Evas_Object *obj)
307 {
308    ELM_CHECK_WIDTYPE(obj, widtype);
309    Widget_Data *wd = elm_widget_data_get(obj);
310    if (!wd) return;
311    wd->maxminw = 0;
312    wd->maxminh = 0;
313    wd->eval = EINA_TRUE;
314    wd->szeval = EINA_TRUE;
315    evas_object_smart_changed(obj);
316 }