1 #include "e.h"
2 
3 struct _E_Radio_Group
4 {
5    int       *valptr;
6    Eina_List *radios;
7 };
8 
9 typedef struct _E_Widget_Data E_Widget_Data;
10 struct _E_Widget_Data
11 {
12    E_Radio_Group *group;
13    Evas_Object   *o_radio;
14    Evas_Object   *o_icon;
15    int            valnum;
16 };
17 
18 static void _e_wid_del_hook(Evas_Object *obj);
19 static void _e_wid_focus_hook(Evas_Object *obj);
20 static void _e_wid_do(Evas_Object *obj);
21 static void _e_wid_activate_hook(Evas_Object *obj);
22 static void _e_wid_disable_hook(Evas_Object *obj);
23 static void _e_wid_signal_cb1(void *data, Evas_Object *obj, const char *emission, const char *source);
24 static void _e_wid_focus_steal(void *data, Evas *e, Evas_Object *obj, void *event_info);
25 /* local subsystem functions */
26 
27 /* externally accessible functions */
28 E_API E_Radio_Group *
e_widget_radio_group_new(int * val)29 e_widget_radio_group_new(int *val)
30 {
31    E_Radio_Group *group;
32 
33    group = calloc(1, sizeof(E_Radio_Group));
34    group->valptr = val;
35    return group;
36 }
37 
38 E_API Evas_Object *
e_widget_radio_add(Evas * evas,const char * label,int valnum,E_Radio_Group * group)39 e_widget_radio_add(Evas *evas, const char *label, int valnum, E_Radio_Group *group)
40 {
41    Evas_Object *obj, *o;
42    E_Widget_Data *wd;
43    Evas_Coord mw, mh;
44 
45    obj = e_widget_add(evas);
46 
47    e_widget_del_hook_set(obj, _e_wid_del_hook);
48    e_widget_focus_hook_set(obj, _e_wid_focus_hook);
49    e_widget_activate_hook_set(obj, _e_wid_activate_hook);
50    e_widget_disable_hook_set(obj, _e_wid_disable_hook);
51 
52    wd = calloc(1, sizeof(E_Widget_Data));
53    wd->group = group;
54    wd->valnum = valnum;
55    e_widget_data_set(obj, wd);
56 
57    o = edje_object_add(evas);
58    wd->o_radio = o;
59    e_theme_edje_object_set(o, "base/theme/widgets",
60                            "e/widgets/radio");
61    edje_object_signal_callback_add(o, "e,action,toggle", "*",
62                                    _e_wid_signal_cb1, obj);
63    edje_object_part_text_set(o, "e.text.label", label);
64    evas_object_show(o);
65    edje_object_size_min_calc(o, &mw, &mh);
66    e_widget_size_min_set(obj, mw, mh);
67    if ((wd->group) && (wd->group->valptr))
68      {
69         if (*(wd->group->valptr) == valnum)
70           edje_object_signal_emit(o, "e,state,on", "e");
71      }
72    if (wd->group)
73      {
74         wd->group->radios = eina_list_append(wd->group->radios, obj);
75      }
76 
77    e_widget_sub_object_add(obj, o);
78    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN,
79                                   _e_wid_focus_steal, obj);
80    e_widget_resize_object_set(obj, o);
81 
82    return obj;
83 }
84 
85 E_API Evas_Object *
e_widget_radio_icon_add(Evas * evas,const char * label,const char * icon,int icon_w,int icon_h,int valnum,E_Radio_Group * group)86 e_widget_radio_icon_add(Evas *evas, const char *label, const char *icon, int icon_w, int icon_h, int valnum, E_Radio_Group *group)
87 {
88    Evas_Object *obj, *o, *o2;
89    E_Widget_Data *wd;
90    Evas_Coord mw, mh;
91 
92    obj = e_widget_add(evas);
93 
94    e_widget_del_hook_set(obj, _e_wid_del_hook);
95    e_widget_focus_hook_set(obj, _e_wid_focus_hook);
96    e_widget_activate_hook_set(obj, _e_wid_activate_hook);
97    e_widget_disable_hook_set(obj, _e_wid_disable_hook);
98 
99    wd = calloc(1, sizeof(E_Widget_Data));
100    wd->group = group;
101    wd->valnum = valnum;
102    e_widget_data_set(obj, wd);
103 
104    o = edje_object_add(evas);
105    wd->o_radio = o;
106    e_theme_edje_object_set(o, "base/theme/widgets",
107                            "e/widgets/radio_icon");
108    edje_object_signal_callback_add(o, "e,action,toggle", "*",
109                                    _e_wid_signal_cb1, obj);
110    edje_object_part_text_set(o, "e.text.label", label);
111    evas_object_show(o);
112 
113    if (label)
114      {
115         edje_object_signal_emit(o, "e,state,labeled", "e");
116         edje_object_message_signal_process(o);
117      }
118 
119    if (icon)
120      {
121         o2 = e_icon_add(evas);
122         wd->o_icon = o2;
123         e_util_icon_theme_set(o2, icon);
124         evas_object_size_hint_min_set(o2, icon_w, icon_h);
125         edje_object_part_swallow(wd->o_radio, "e.swallow.icon", o2);
126         evas_object_show(o2);
127         e_widget_sub_object_add(obj, o2);
128      }
129 
130    edje_object_size_min_calc(o, &mw, &mh);
131    e_widget_size_min_set(obj, mw, mh);
132    if ((wd->group) && (wd->group->valptr))
133      {
134         if (*(wd->group->valptr) == valnum)
135           edje_object_signal_emit(o, "e,state,on", "e");
136      }
137    if (wd->group)
138      wd->group->radios = eina_list_append(wd->group->radios, obj);
139 
140    e_widget_sub_object_add(obj, o);
141    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN,
142                                   _e_wid_focus_steal, obj);
143    e_widget_resize_object_set(obj, o);
144 
145    return obj;
146 }
147 
148 E_API void
e_widget_radio_toggle_set(Evas_Object * obj,int toggle)149 e_widget_radio_toggle_set(Evas_Object *obj, int toggle)
150 {
151    E_Widget_Data *wd;
152 
153    if (!(wd = e_widget_data_get(obj))) return;
154 
155    if (toggle)
156      {
157         Eina_List *l;
158         Evas_Object *sobj;
159 
160         EINA_LIST_FOREACH(wd->group->radios, l, sobj)
161           {
162              if (sobj != obj)
163                {
164                   wd = e_widget_data_get(sobj);
165                   if (wd->valnum == *(wd->group->valptr))
166                     {
167                        edje_object_signal_emit(wd->o_radio, "e,state,off", "e");
168                        break;
169                     }
170                }
171           }
172         wd = e_widget_data_get(obj);
173         *(wd->group->valptr) = wd->valnum;
174         edje_object_signal_emit(wd->o_radio, "e,state,on", "e");
175      }
176    else
177      edje_object_signal_emit(wd->o_radio, "e,state,off", "e");
178    evas_object_smart_callback_call(obj, "changed", NULL);
179 }
180 
181 static void
_e_wid_del_hook(Evas_Object * obj)182 _e_wid_del_hook(Evas_Object *obj)
183 {
184    E_Widget_Data *wd;
185 
186    wd = e_widget_data_get(obj);
187    if (wd->group)
188      {
189         wd->group->radios = eina_list_remove(wd->group->radios, obj);
190         if (!wd->group->radios) free(wd->group);
191      }
192    free(wd);
193 }
194 
195 static void
_e_wid_focus_hook(Evas_Object * obj)196 _e_wid_focus_hook(Evas_Object *obj)
197 {
198    E_Widget_Data *wd;
199 
200    wd = e_widget_data_get(obj);
201    if (e_widget_focus_get(obj))
202      {
203         edje_object_signal_emit(wd->o_radio, "e,state,focused", "e");
204         evas_object_focus_set(wd->o_radio, 1);
205      }
206    else
207      {
208         edje_object_signal_emit(wd->o_radio, "e,state,unfocused", "e");
209         evas_object_focus_set(wd->o_radio, 0);
210      }
211 }
212 
213 static void
_e_wid_do(Evas_Object * obj)214 _e_wid_do(Evas_Object *obj)
215 {
216    E_Widget_Data *wd;
217 
218    if (e_widget_disabled_get(obj)) return;
219 
220    wd = e_widget_data_get(obj);
221    if ((wd->group) && (wd->group->valptr))
222      {
223         Eina_List *l;
224         Evas_Object *sobj;
225         int toggled = 0;
226 
227         EINA_LIST_FOREACH(wd->group->radios, l, sobj)
228           {
229              if (sobj != obj)
230                {
231                   wd = e_widget_data_get(sobj);
232                   if (wd->valnum == *(wd->group->valptr))
233                     {
234                        edje_object_signal_emit(wd->o_radio, "e,state,off", "e");
235                        toggled = 1;
236                        break;
237                     }
238                }
239           }
240         wd = e_widget_data_get(obj);
241         if (!toggled)
242           {
243              if (wd->valnum == *(wd->group->valptr)) return;
244           }
245         *(wd->group->valptr) = wd->valnum;
246         edje_object_signal_emit(wd->o_radio, "e,state,on", "e");
247      }
248    evas_object_smart_callback_call(obj, "changed", NULL);
249 }
250 
251 static void
_e_wid_activate_hook(Evas_Object * obj)252 _e_wid_activate_hook(Evas_Object *obj)
253 {
254    _e_wid_do(obj);
255 }
256 
257 static void
_e_wid_disable_hook(Evas_Object * obj)258 _e_wid_disable_hook(Evas_Object *obj)
259 {
260    E_Widget_Data *wd;
261 
262    wd = e_widget_data_get(obj);
263    if (e_widget_disabled_get(obj))
264      edje_object_signal_emit(wd->o_radio, "e,state,disabled", "e");
265    else
266      edje_object_signal_emit(wd->o_radio, "e,state,enabled", "e");
267 }
268 
269 static void
_e_wid_signal_cb1(void * data,Evas_Object * obj EINA_UNUSED,const char * emission EINA_UNUSED,const char * source EINA_UNUSED)270 _e_wid_signal_cb1(void *data, Evas_Object *obj EINA_UNUSED, const char *emission EINA_UNUSED, const char *source EINA_UNUSED)
271 {
272    _e_wid_do(data);
273    e_widget_change(data);
274 }
275 
276 static void
_e_wid_focus_steal(void * data,Evas * e EINA_UNUSED,Evas_Object * obj EINA_UNUSED,void * event_info EINA_UNUSED)277 _e_wid_focus_steal(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
278 {
279    e_widget_focus_steal(data);
280 }
281 
282