notes
[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 }
65
66 static void
67 _eval(Evas_Object *obj)
68 {
69    Evas_Coord x, y, w, h, cvx, cvy, cvw, cvh;
70    Widget_Data *wd = elm_widget_data_get(obj);
71    if (!wd) return;
72    
73    evas_object_geometry_get(obj, &x, &y, &w, &h);
74    evas_output_viewport_get(evas_object_evas_get(obj), 
75                             &cvx, &cvy, &cvw, &cvh);
76    // this is a hack to get things sane for now.
77    if ((w < 10) || (h < 10) || (cvw < 1) || (cvh < 1)) return;
78    // need some fuzz value thats beyond the current viewport
79    if (ELM_RECTS_INTERSECT(x, y, w, h, cvx, cvy, cvw, cvh))
80      {
81         if (!wd->content)
82           {
83              printf("intersect: %i %i %ix%i | %i %i %ix%i\n",
84                     x, y, w, h, cvx, cvy, cvw, cvh);
85              evas_object_smart_callback_call(obj, SIG_REALIZE, NULL);
86              if (wd->content)
87                {
88                   if (evas_object_smart_data_get(wd->content))
89                      evas_object_smart_calculate(wd->content);
90                }
91           }
92      }
93    else
94      {
95         if (wd->content)
96            evas_object_smart_callback_call(obj, SIG_UNREALIZE, NULL);
97      }
98 }
99
100 static void
101 _changed(Evas_Object *obj)
102 {
103    Widget_Data *wd = elm_widget_data_get(obj);
104    if (!wd) return;
105    if (wd->eval)
106      {
107         _eval(obj);
108         _sizing_eval(obj);
109         wd->eval = EINA_FALSE;
110      }
111 }
112
113 static void
114 _move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
115 {
116    Widget_Data *wd = elm_widget_data_get(obj);
117    if (!wd) return;
118    wd->eval = EINA_TRUE;
119    evas_object_smart_changed(obj);
120 }
121                
122 static void
123 _resize(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
124 {              
125    Widget_Data *wd = elm_widget_data_get(obj);
126    if (!wd) return;
127    wd->eval = EINA_TRUE;
128    evas_object_smart_changed(obj);
129 }
130
131 static void
132 _child_change(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
133 {
134    Widget_Data *wd = elm_widget_data_get(data);
135    if (!wd) return;
136    wd->eval = EINA_TRUE;
137    evas_object_smart_changed(data);
138 }
139
140 static void
141 _child_del(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
142 {
143    Evas_Object *fobj = data;
144    Widget_Data *wd = elm_widget_data_get(fobj);
145    if (!wd) return;
146    if (wd->content != obj) return;
147    evas_object_event_callback_del_full(wd->content, 
148                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
149                                        _child_change, obj);
150    evas_object_event_callback_del_full(wd->content, 
151                                        EVAS_CALLBACK_DEL,
152                                        _child_del, obj);
153    wd->content = NULL;
154 }
155
156 EAPI Evas_Object *
157 elm_factory_add(Evas_Object *parent)
158 {
159    Evas_Object *obj;
160    Evas *e;
161    Widget_Data *wd;
162
163    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
164
165    ELM_SET_WIDTYPE(widtype, "factory");
166    elm_widget_type_set(obj, "factory");
167    elm_widget_sub_object_add(parent, obj);
168    elm_widget_data_set(obj, wd);
169    elm_widget_del_hook_set(obj, _del_hook);
170    elm_widget_focus_next_hook_set(obj, _focus_next_hook);
171    elm_widget_can_focus_set(obj, EINA_FALSE);
172    elm_widget_changed_hook_set(obj, _changed);
173
174    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _move, NULL);
175    evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE, _resize, NULL);
176
177    evas_object_smart_callbacks_descriptions_set(obj, _signals);
178    
179    wd->obj = obj;
180    return obj;
181 }
182
183 EAPI void
184 elm_factory_content_set(Evas_Object *obj, Evas_Object *content)
185 {
186    ELM_CHECK_WIDTYPE(obj, widtype);
187    Widget_Data *wd = elm_widget_data_get(obj);
188    if (!wd) return;
189    if (wd->content == content) return;
190    if (wd->content) evas_object_del(wd->content);
191    wd->content = content;
192    elm_widget_resize_object_set(obj, wd->content);
193    evas_object_event_callback_add(wd->content, EVAS_CALLBACK_DEL,
194                                   _child_del, obj);
195    evas_object_event_callback_add(wd->content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
196                                   _child_change, obj);
197    wd->eval = EINA_TRUE;
198    evas_object_smart_changed(obj);
199 }
200
201 EAPI Evas_Object *
202 elm_factory_content_get(const Evas_Object *obj)
203 {
204    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
205    Widget_Data *wd = elm_widget_data_get(obj);
206    if (!wd) return NULL;
207    return wd->content;
208 }