elm factory... in, genscroller out.
[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    Ecore_Job *eval_job;
11 };
12
13 static const char *widtype = NULL;
14 static void _del_hook(Evas_Object *obj);
15 static void _sizing_eval(Evas_Object *obj);
16 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
17
18 static const char SIG_REALIZE[] = "realize";
19 static const char SIG_UNREALIZE[] = "unrealize";
20
21 static const Evas_Smart_Cb_Description _signals[] = {
22    {SIG_REALIZE, ""},
23    {SIG_UNREALIZE, ""},
24    {NULL, NULL}
25 };
26
27 static void
28 _del_hook(Evas_Object *obj)
29 {
30    Widget_Data *wd = elm_widget_data_get(obj);
31    if (!wd) return;
32    if (wd->eval_job) ecore_job_del(wd->eval_job);
33    free(wd);
34 }
35
36 static Eina_Bool
37 _focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
38 {
39    Widget_Data *wd = elm_widget_data_get(obj);
40    Evas_Object *cur;
41    
42    if ((!wd) || (!wd->content)) return EINA_FALSE;
43    cur = wd->content;
44    return elm_widget_focus_next_get(cur, dir, next);
45 }
46
47 static void
48 _sizing_eval(Evas_Object *obj)
49 {
50    Widget_Data *wd = elm_widget_data_get(obj);
51    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
52    
53    if (!wd) return;
54    if (!wd->content) return;
55    evas_object_size_hint_min_get(wd->content, &minw, &minh);
56    evas_object_size_hint_max_get(wd->content, &maxw, &maxh);
57    evas_object_size_hint_min_set(obj, minw, minh);
58    evas_object_size_hint_max_set(obj, maxw, maxh);
59 }
60
61 static void
62 _eval_do(void *data)
63 {
64    Evas_Object *obj = data;
65    Evas_Coord x, y, w, h, cvx, cvy, cvw, cvh;
66    Widget_Data *wd = elm_widget_data_get(obj);
67    if (!wd) return;
68    
69    wd->eval_job = NULL;
70    evas_object_geometry_get(obj, &x, &y, &w, &h);
71    evas_output_viewport_get(evas_object_evas_get(obj), 
72                             &cvx, &cvy, &cvw, &cvh);
73    if (ELM_RECTS_INTERSECT(x, y, w, h, cvx, cvy, cvw, cvh))
74      {
75         if (!wd->content)
76            evas_object_smart_callback_call(obj, SIG_REALIZE, NULL);
77      }
78    else
79      {
80         if (wd->content)
81            evas_object_smart_callback_call(obj, SIG_UNREALIZE, NULL);
82      }
83 }
84
85 static void
86 _eval(Evas_Object *obj)
87 {
88    Widget_Data *wd = elm_widget_data_get(obj);
89    if (!wd) return;
90    
91    if (wd->eval_job) ecore_job_del(wd->eval_job);
92    wd->eval_job = ecore_job_add(_eval_do, obj);
93 }
94
95 static void
96 _move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
97 {
98    _eval(obj);
99 }
100                
101 static void
102 _resize(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
103 {              
104    _eval(obj);
105 }
106
107 static void
108 _child_change(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
109 {
110    _eval(obj);
111 }
112
113 static void
114 _child_del(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
115 {
116    Evas_Object *fobj = data;
117    Widget_Data *wd = elm_widget_data_get(fobj);
118    if (!wd) return;
119    if (wd->content != obj) return;
120    evas_object_event_callback_del_full(wd->content, 
121                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
122                                        _child_change, obj);
123    evas_object_event_callback_del_full(wd->content, 
124                                        EVAS_CALLBACK_DEL,
125                                        _child_del, obj);
126    wd->content = NULL;
127 }
128
129 EAPI Evas_Object *
130 elm_factory_add(Evas_Object *parent)
131 {
132    Evas_Object *obj;
133    Evas *e;
134    Widget_Data *wd;
135
136    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
137
138    ELM_SET_WIDTYPE(widtype, "factory");
139    elm_widget_type_set(obj, "factory");
140    elm_widget_sub_object_add(parent, obj);
141    elm_widget_data_set(obj, wd);
142    elm_widget_del_hook_set(obj, _del_hook);
143    elm_widget_focus_next_hook_set(obj, _focus_next_hook);
144    elm_widget_can_focus_set(obj, EINA_FALSE);
145
146    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _move, NULL);
147    evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE, _resize, NULL);
148
149    evas_object_smart_callbacks_descriptions_set(obj, _signals);
150    
151    wd->obj = obj;
152    return obj;
153 }
154
155 EAPI void
156 elm_factory_content_set(Evas_Object *obj, Evas_Object *content)
157 {
158    ELM_CHECK_WIDTYPE(obj, widtype);
159    Widget_Data *wd = elm_widget_data_get(obj);
160    if (!wd) return;
161    if (wd->content == content) return;
162    if (wd->content) evas_object_del(wd->content);
163    wd->content = content;
164    elm_widget_resize_object_set(obj, wd->content);
165    evas_object_event_callback_add(wd->content, EVAS_CALLBACK_DEL,
166                                   _child_del, obj);
167    evas_object_event_callback_add(wd->content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
168                                   _child_change, obj);
169     _sizing_eval(obj);
170 }
171
172 EAPI Evas_Object *
173 elm_factory_content_get(const Evas_Object *obj)
174 {
175    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
176    Widget_Data *wd = elm_widget_data_get(obj);
177    if (!wd) return NULL;
178    return wd->content;
179 }