factory.... working on it. but up and kicking. it's a little unhappy
[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    Eina_Bool eval : 1;
11 };
12
13 static const char *widtype = NULL;
14 static void _del_hook(Evas_Object *obj);
15 static Eina_Bool _focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next);
16 static void _sizing_eval(Evas_Object *obj);
17 static void _eval(Evas_Object *obj);
18 static void _changed(Evas_Object *obj);
19 static void _move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__);
20 static void _resize(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__);
21 static void _child_change(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__);
22 static void _child_del(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__);
23
24 static const char SIG_REALIZE[] = "realize";
25 static const char SIG_UNREALIZE[] = "unrealize";
26
27 static const Evas_Smart_Cb_Description _signals[] = {
28    {SIG_REALIZE, ""},
29    {SIG_UNREALIZE, ""},
30    {NULL, NULL}
31 };
32
33 static void
34 _del_hook(Evas_Object *obj)
35 {
36    Widget_Data *wd = elm_widget_data_get(obj);
37    if (!wd) return;
38    free(wd);
39 }
40
41 static Eina_Bool
42 _focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
43 {
44    Widget_Data *wd = elm_widget_data_get(obj);
45    Evas_Object *cur;
46    
47    if ((!wd) || (!wd->content)) return EINA_FALSE;
48    cur = wd->content;
49    return elm_widget_focus_next_get(cur, dir, next);
50 }
51
52 static void
53 _sizing_eval(Evas_Object *obj)
54 {
55    Widget_Data *wd = elm_widget_data_get(obj);
56    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
57    
58    if (!wd) return;
59    if (!wd->content) return;
60    evas_object_size_hint_min_get(wd->content, &minw, &minh);
61    evas_object_size_hint_max_get(wd->content, &maxw, &maxh);
62    evas_object_size_hint_min_set(obj, minw, minh);
63    evas_object_size_hint_max_set(obj, maxw, maxh);
64    printf("fac: %p, size %ix%i -> %ix%i\n", obj, minw, minh, 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_object_geometry_get(obj, &x, &y, &w, &h);
75    evas_output_viewport_get(evas_object_evas_get(obj), 
76                             &cvx, &cvy, &cvw, &cvh);
77    if ((w < 1) || (h < 1) || (cvw < 1) || (cvh < 1)) return;
78    if (ELM_RECTS_INTERSECT(x, y, w, h, cvx, cvy, cvw, cvh))
79      {
80         if (!wd->content)
81           {
82              printf("intersect: %i %i %ix%i | %i %i %ix%i\n",
83                     x, y, w, h, cvx, cvy, cvw, cvh);
84              evas_object_smart_callback_call(obj, SIG_REALIZE, NULL);
85              if (wd->content)
86                {
87                   if (evas_object_smart_data_get(wd->content))
88                      evas_object_smart_calculate(wd->content);
89                }
90           }
91      }
92    else
93      {
94         if (wd->content)
95            evas_object_smart_callback_call(obj, SIG_UNREALIZE, NULL);
96      }
97 }
98
99 static void
100 _changed(Evas_Object *obj)
101 {
102    Widget_Data *wd = elm_widget_data_get(obj);
103    if (!wd) return;
104    if (wd->eval)
105      {
106         _eval(obj);
107         _sizing_eval(obj);
108         wd->eval = EINA_FALSE;
109      }
110 }
111
112 static void
113 _move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
114 {
115    Widget_Data *wd = elm_widget_data_get(obj);
116    if (!wd) return;
117    wd->eval = EINA_TRUE;
118    evas_object_smart_changed(obj);
119 }
120                
121 static void
122 _resize(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
123 {              
124    Widget_Data *wd = elm_widget_data_get(obj);
125    if (!wd) return;
126    wd->eval = EINA_TRUE;
127    evas_object_smart_changed(obj);
128 }
129
130 static void
131 _child_change(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
132 {
133    Widget_Data *wd = elm_widget_data_get(data);
134    if (!wd) return;
135    wd->eval = EINA_TRUE;
136    evas_object_smart_changed(data);
137 }
138
139 static void
140 _child_del(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
141 {
142    Evas_Object *fobj = data;
143    Widget_Data *wd = elm_widget_data_get(fobj);
144    if (!wd) return;
145    if (wd->content != obj) return;
146    evas_object_event_callback_del_full(wd->content, 
147                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
148                                        _child_change, obj);
149    evas_object_event_callback_del_full(wd->content, 
150                                        EVAS_CALLBACK_DEL,
151                                        _child_del, obj);
152    wd->content = NULL;
153 }
154
155 EAPI Evas_Object *
156 elm_factory_add(Evas_Object *parent)
157 {
158    Evas_Object *obj;
159    Evas *e;
160    Widget_Data *wd;
161
162    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
163
164    ELM_SET_WIDTYPE(widtype, "factory");
165    elm_widget_type_set(obj, "factory");
166    elm_widget_sub_object_add(parent, obj);
167    elm_widget_data_set(obj, wd);
168    elm_widget_del_hook_set(obj, _del_hook);
169    elm_widget_focus_next_hook_set(obj, _focus_next_hook);
170    elm_widget_can_focus_set(obj, EINA_FALSE);
171    elm_widget_changed_hook_set(obj, _changed);
172
173    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _move, NULL);
174    evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE, _resize, NULL);
175
176    evas_object_smart_callbacks_descriptions_set(obj, _signals);
177    
178    wd->obj = obj;
179    return obj;
180 }
181
182 EAPI void
183 elm_factory_content_set(Evas_Object *obj, Evas_Object *content)
184 {
185    ELM_CHECK_WIDTYPE(obj, widtype);
186    Widget_Data *wd = elm_widget_data_get(obj);
187    if (!wd) return;
188    if (wd->content == content) return;
189    if (wd->content) evas_object_del(wd->content);
190    wd->content = content;
191    elm_widget_resize_object_set(obj, wd->content);
192    evas_object_event_callback_add(wd->content, EVAS_CALLBACK_DEL,
193                                   _child_del, obj);
194    evas_object_event_callback_add(wd->content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
195                                   _child_change, obj);
196    wd->eval = EINA_TRUE;
197    evas_object_smart_changed(obj);
198 }
199
200 EAPI Evas_Object *
201 elm_factory_content_get(const Evas_Object *obj)
202 {
203    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
204    Widget_Data *wd = elm_widget_data_get(obj);
205    if (!wd) return NULL;
206    return wd->content;
207 }