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