Merge remote-tracking branch 'remotes/origin/upstream'
[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 static void _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content);
30 static Evas_Object *_content_get_hook(const Evas_Object *obj, const char *part);
31 <<<<<<< HEAD
32 =======
33 static Evas_Object *_content_unset_hook(Evas_Object *obj, const char *part);
34 >>>>>>> remotes/origin/upstream
35
36 static const char SIG_REALIZE[] = "realize";
37 static const char SIG_UNREALIZE[] = "unrealize";
38
39 static const Evas_Smart_Cb_Description _signals[] = {
40    {SIG_REALIZE, ""},
41    {SIG_UNREALIZE, ""},
42    {NULL, NULL}
43 };
44
45 static int fac = 0;
46
47 static void
48 _del_hook(Evas_Object *obj)
49 {
50    Widget_Data *wd = elm_widget_data_get(obj);
51    if (!wd) return;
52    if (wd->content)
53      {
54         Evas_Object *o = wd->content;
55
56         evas_object_event_callback_del_full(o,
57                                             EVAS_CALLBACK_CHANGED_SIZE_HINTS,
58                                             _child_change, obj);
59         evas_object_event_callback_del_full(o,
60                                             EVAS_CALLBACK_DEL,
61                                             _child_del, obj);
62         wd->content = NULL;
63         evas_object_del(o);
64         fac--;
65 //        printf("FAC-- = %i\n", fac);
66      }
67    free(wd);
68 }
69
70 static Eina_Bool
71 _focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
72 {
73    Widget_Data *wd = elm_widget_data_get(obj);
74    Evas_Object *cur;
75
76    if ((!wd) || (!wd->content)) return EINA_FALSE;
77    cur = wd->content;
78    return elm_widget_focus_next_get(cur, dir, next);
79 }
80
81 static void
82 _sizing_eval(Evas_Object *obj)
83 {
84    Widget_Data *wd = elm_widget_data_get(obj);
85    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
86
87    if (!wd) return;
88    if (!wd->content) return;
89    evas_object_size_hint_min_get(wd->content, &minw, &minh);
90    evas_object_size_hint_max_get(wd->content, &maxw, &maxh);
91    if (wd->maxmin)
92      {
93         if (minw > wd->maxminw) wd->maxminw = minw;
94         if (minh > wd->maxminh) wd->maxminh = minh;
95         evas_object_size_hint_min_set(obj, wd->maxminw, wd->maxminh);
96      }
97    else
98      {
99         evas_object_size_hint_min_set(obj, minw, minh);
100      }
101    evas_object_size_hint_max_set(obj, maxw, maxh);
102 //   printf("FAC SZ: %i %i | %i %i\n", minw, minh, maxw, maxh);
103 }
104
105 static void
106 _eval(Evas_Object *obj)
107 {
108    Evas_Coord x, y, w, h, cvx, cvy, cvw, cvh;
109    Widget_Data *wd = elm_widget_data_get(obj);
110    if (!wd) return;
111
112    evas_event_freeze(evas_object_evas_get(obj));
113    evas_object_geometry_get(obj, &x, &y, &w, &h);
114    if (w < 1) w = 1;
115    if (h < 1) h = 1;
116    evas_output_viewport_get(evas_object_evas_get(obj),
117                             &cvx, &cvy, &cvw, &cvh);
118    if ((cvw < 1) || (cvh < 1)) return;
119    // need some fuzz value thats beyond the current viewport
120    // for now just make it the viewport * 3 in size (so 1 vp in each direction)
121    /*
122    cvx -= cvw;
123    cvy -= cvh;
124    cvw *= 3;
125    cvh *= 3;
126     */
127    if (ELM_RECTS_INTERSECT(x, y, w, h, cvx, cvy, cvw, cvh))
128      {
129         if (!wd->content)
130           {
131 //             printf("                 + %i %i %ix%i <> %i %i %ix%i\n", x, y, w, h, cvx, cvy, cvw, cvh);
132              evas_object_smart_callback_call(obj, SIG_REALIZE, NULL);
133              if (wd->content)
134                {
135                   if (evas_object_smart_data_get(wd->content))
136                      evas_object_smart_calculate(wd->content);
137                }
138 <<<<<<< HEAD
139              //wd->last_calc_count =
140              //evas_smart_objects_calculate_count_get(evas_object_evas_get(obj));
141 =======
142              wd->last_calc_count =
143                 evas_smart_objects_calculate_count_get(evas_object_evas_get(obj));
144 >>>>>>> remotes/origin/upstream
145           }
146      }
147    else
148      {
149         if (wd->content)
150           {
151 <<<<<<< HEAD
152              //if (wd->last_calc_count !=
153              //evas_smart_objects_calculate_count_get(evas_object_evas_get(obj)))
154 =======
155              if (wd->last_calc_count !=
156                 evas_smart_objects_calculate_count_get(evas_object_evas_get(obj)))
157 >>>>>>> remotes/origin/upstream
158                 evas_object_smart_callback_call(obj, SIG_UNREALIZE, NULL);
159           }
160      }
161    evas_event_thaw(evas_object_evas_get(obj));
162    evas_event_thaw_eval(evas_object_evas_get(obj));
163 }
164
165 static void
166 _changed(Evas_Object *obj)
167 {
168    Widget_Data *wd = elm_widget_data_get(obj);
169    if (!wd) return;
170    if (wd->eval)
171      {
172         _eval(obj);
173         wd->eval = EINA_FALSE;
174      }
175    if (wd->szeval)
176      {
177         _sizing_eval(obj);
178         wd->szeval = EINA_FALSE;
179      }
180 }
181
182 static void
183 _move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
184 {
185    Widget_Data *wd = elm_widget_data_get(obj);
186    if (!wd) return;
187    wd->eval = EINA_TRUE;
188    evas_object_smart_changed(obj);
189 }
190
191 static void
192 _resize(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
193 {
194    Widget_Data *wd = elm_widget_data_get(obj);
195    if (!wd) return;
196    wd->eval = EINA_TRUE;
197    evas_object_smart_changed(obj);
198 }
199
200 static void
201 _child_change(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
202 {
203    Widget_Data *wd = elm_widget_data_get(data);
204    if (!wd) return;
205    wd->eval = EINA_TRUE;
206    wd->szeval = EINA_TRUE;
207    evas_object_smart_changed(data);
208 }
209
210 static void
211 _child_del(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
212 {
213    Evas_Object *fobj = data;
214    Widget_Data *wd = elm_widget_data_get(fobj);
215    if (!wd) return;
216    if (wd->content != obj) return;
217    evas_object_event_callback_del_full(wd->content,
218                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
219                                        _child_change, obj);
220    evas_object_event_callback_del_full(wd->content,
221                                        EVAS_CALLBACK_DEL,
222                                        _child_del, obj);
223    wd->content = NULL;
224    fac--;
225 //   printf("FAC-- = %i\n", fac);
226 }
227
228 <<<<<<< HEAD
229 =======
230 static Evas_Object *
231 _content_unset_hook(Evas_Object *obj, const char *part)
232 {
233    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
234    Widget_Data *wd;
235    Evas_Object *content;
236
237    if (part && strcmp(part, "default")) return NULL;
238    wd = elm_widget_data_get(obj);
239    if (!wd || !wd->content) return NULL;
240
241    content = wd->content;
242    evas_object_event_callback_del_full(content,
243                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
244                                        _child_change, obj);
245    evas_object_event_callback_del_full(content,
246                                        EVAS_CALLBACK_DEL,
247                                        _child_del, obj);
248    wd->content = NULL;
249    fac--;
250 //         printf("FAC-- = %i\n", fac);
251    return content;
252 }
253
254 >>>>>>> remotes/origin/upstream
255 static void
256 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
257 {
258    ELM_CHECK_WIDTYPE(obj, widtype);
259    Widget_Data *wd;
260 <<<<<<< HEAD
261 =======
262    Evas_Object *prev_content;
263 >>>>>>> remotes/origin/upstream
264
265    if (part && strcmp(part, "default")) return;
266    wd = elm_widget_data_get(obj);
267    if (!wd) return;
268    if (wd->content == content) return;
269 <<<<<<< HEAD
270    if (wd->content)
271       {
272          Evas_Object *o = wd->content;
273
274          evas_object_event_callback_del_full(wd->content,
275                                              EVAS_CALLBACK_CHANGED_SIZE_HINTS,
276                                              _child_change, obj);
277          evas_object_event_callback_del_full(wd->content,
278                                              EVAS_CALLBACK_DEL,
279                                              _child_del, obj);
280          wd->content = NULL;
281          evas_object_del(o);
282          fac--;
283 //         printf("FAC-- = %i\n", fac);
284       }
285    wd->content = content;
286    if (wd->content)
287      {
288         fac++;
289 //        printf("FAC++ = %i\n", fac);
290         elm_widget_resize_object_set(obj, wd->content);
291         evas_object_event_callback_add(wd->content, EVAS_CALLBACK_DEL,
292                                        _child_del, obj);
293         evas_object_event_callback_add(wd->content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
294                                        _child_change, obj);
295         wd->eval = EINA_TRUE;
296         wd->szeval = EINA_TRUE;
297         evas_object_smart_changed(obj);
298      }
299 =======
300
301    prev_content = _content_unset_hook(obj, part);
302    if (prev_content) evas_object_del(prev_content);
303
304    wd->content = content;
305    if (!content) return;
306
307    elm_widget_resize_object_set(obj, content);
308    evas_object_event_callback_add(content, EVAS_CALLBACK_DEL, _child_del, obj);
309    evas_object_event_callback_add(content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
310                                   _child_change, obj);
311    wd->eval = EINA_TRUE;
312    wd->szeval = EINA_TRUE;
313    evas_object_smart_changed(obj);
314    fac++;
315 >>>>>>> remotes/origin/upstream
316 }
317
318 static Evas_Object *
319 _content_get_hook(const Evas_Object *obj, const char *part)
320 {
321    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
322    Widget_Data *wd;
323    if (part && strcmp(part, "default")) return NULL;
324    wd = elm_widget_data_get(obj);
325    if (!wd) return NULL;
326    return wd->content;
327 }
328
329 EAPI Evas_Object *
330 elm_factory_add(Evas_Object *parent)
331 {
332    Evas_Object *obj;
333    Evas *e;
334    Widget_Data *wd;
335
336    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
337
338    ELM_SET_WIDTYPE(widtype, "factory");
339    elm_widget_type_set(obj, "factory");
340    elm_widget_sub_object_add(parent, obj);
341    elm_widget_data_set(obj, wd);
342    elm_widget_del_hook_set(obj, _del_hook);
343    elm_widget_focus_next_hook_set(obj, _focus_next_hook);
344    elm_widget_content_set_hook_set(obj, _content_set_hook);
345    elm_widget_content_get_hook_set(obj, _content_get_hook);
346 <<<<<<< HEAD
347 =======
348    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
349 >>>>>>> remotes/origin/upstream
350    elm_widget_can_focus_set(obj, EINA_FALSE);
351    elm_widget_changed_hook_set(obj, _changed);
352
353    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _move, NULL);
354    evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE, _resize, NULL);
355
356    evas_object_smart_callbacks_descriptions_set(obj, _signals);
357
358    wd->obj = obj;
359    wd->last_calc_count = -1;
360    return obj;
361 }
362
363 EAPI void
364 <<<<<<< HEAD
365 elm_factory_content_set(Evas_Object *obj, Evas_Object *content)
366 {
367    _content_set_hook(obj, NULL, content);
368 }
369
370 EAPI Evas_Object *
371 elm_factory_content_get(const Evas_Object *obj)
372 {
373    return _content_get_hook(obj, NULL);
374 }
375
376 EAPI void
377 =======
378 >>>>>>> remotes/origin/upstream
379 elm_factory_maxmin_mode_set(Evas_Object *obj, Eina_Bool enabled)
380 {
381    ELM_CHECK_WIDTYPE(obj, widtype);
382    Widget_Data *wd = elm_widget_data_get(obj);
383    if (!wd) return;
384    wd->maxmin = !!enabled;
385 }
386
387 EAPI Eina_Bool
388 elm_factory_maxmin_mode_get(const Evas_Object *obj)
389 {
390    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
391    Widget_Data *wd = elm_widget_data_get(obj);
392    if (!wd) return EINA_FALSE;
393    return wd->maxmin;
394 }
395
396 EAPI void
397 elm_factory_maxmin_reset_set(Evas_Object *obj)
398 {
399    ELM_CHECK_WIDTYPE(obj, widtype);
400    Widget_Data *wd = elm_widget_data_get(obj);
401    if (!wd) return;
402    wd->maxminw = 0;
403    wd->maxminh = 0;
404    wd->eval = EINA_TRUE;
405    wd->szeval = EINA_TRUE;
406    evas_object_smart_changed(obj);
407 }