Merge "[prevent issues ID:55300,38821,19282,55298 for test_fileselector.c and elc_fil...
[framework/uifw/elementary.git] / src / lib / elc_hoversel.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 *btn, *hover;
9    Evas_Object *hover_parent;
10    Eina_List *items;
11    Eina_Bool horizontal : 1;
12    Eina_Bool expanded   : 1;
13 };
14
15 struct _Elm_Hoversel_Item
16 {
17    ELM_WIDGET_ITEM;
18    const char *label;
19    const char *icon_file;
20    const char *icon_group;
21    Elm_Icon_Type icon_type;
22    Evas_Smart_Cb func;
23 };
24
25 static const char *widtype = NULL;
26 static void _del_pre_hook(Evas_Object *obj);
27 static void _del_hook(Evas_Object *obj);
28 static void _activate(Evas_Object *obj);
29 static void _activate_hook(Evas_Object *obj);
30 static void _disable_hook(Evas_Object *obj);
31 static void _sizing_eval(Evas_Object *obj);
32 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
33 static void _parent_del(void *data, Evas *e, Evas_Object *obj, void *event_info);
34
35 static const char SIG_CLICKED[] = "clicked";
36 static const char SIG_SELECTED[] = "selected";
37 static const char SIG_DISMISSED[] = "dismissed";
38
39 static const Evas_Smart_Cb_Description _signals[] = {
40    {SIG_CLICKED, ""},
41    {SIG_SELECTED, ""},
42    {SIG_DISMISSED, ""},
43    {NULL, NULL}
44 };
45
46 static void
47 _del_pre_hook(Evas_Object *obj)
48 {
49    Elm_Hoversel_Item *item;
50    Widget_Data *wd = elm_widget_data_get(obj);
51    if (!wd) return;
52    elm_hoversel_hover_end(obj);
53    elm_hoversel_hover_parent_set(obj, NULL);
54    EINA_LIST_FREE(wd->items, item)
55      {
56         elm_widget_item_pre_notify_del(item);
57         eina_stringshare_del(item->label);
58         eina_stringshare_del(item->icon_file);
59         eina_stringshare_del(item->icon_group);
60         elm_widget_item_del(item);
61      }
62 }
63
64 static void
65 _del_hook(Evas_Object *obj)
66 {
67    Widget_Data *wd = elm_widget_data_get(obj);
68    if (!wd) return;
69    free(wd);
70 }
71
72 static void
73 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
74 {
75    Widget_Data *wd = elm_widget_data_get(obj);
76    if (!wd) return;
77    elm_widget_mirrored_set(wd->btn, rtl);
78    elm_widget_mirrored_set(wd->hover, rtl);
79 }
80
81 static void
82 _theme_hook(Evas_Object *obj)
83 {
84    Widget_Data *wd = elm_widget_data_get(obj);
85    char buf[4096];
86    if (!wd) return;
87    _elm_widget_mirrored_reload(obj);
88
89    elm_hoversel_hover_end(obj);
90    if (wd->horizontal)
91      snprintf(buf, sizeof(buf), "hoversel_horizontal/%s", elm_widget_style_get(obj));
92    else
93      snprintf(buf, sizeof(buf), "hoversel_vertical/%s", elm_widget_style_get(obj));
94    elm_object_style_set(wd->btn, buf);
95    elm_object_disabled_set(wd->btn, elm_widget_disabled_get(obj));
96    _mirrored_set(obj, elm_widget_mirrored_get(obj));
97 }
98
99 static void
100 _disable_hook(Evas_Object *obj)
101 {
102    Widget_Data *wd = elm_widget_data_get(obj);
103    if (!wd) return;
104    elm_object_disabled_set(wd->btn, elm_widget_disabled_get(obj));
105 }
106
107 static void
108 _sizing_eval(Evas_Object *obj)
109 {
110    Widget_Data *wd = elm_widget_data_get(obj);
111    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
112    if (!wd) return;
113    evas_object_size_hint_min_get(wd->btn, &minw, &minh);
114    evas_object_size_hint_max_get(wd->btn, &maxw, &maxh);
115    evas_object_size_hint_min_set(obj, minw, minh);
116    evas_object_size_hint_max_set(obj, maxw, maxh);
117 }
118
119 static void
120 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
121 {
122    Widget_Data *wd = elm_widget_data_get(obj);
123    if (!wd) return;
124    if (elm_widget_focus_get(obj))
125      elm_widget_focus_steal(wd->btn);
126 }
127
128 static void
129 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
130 {
131    _sizing_eval(data);
132 }
133
134 static void
135 _hover_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
136 {
137    elm_hoversel_hover_end(data);
138 }
139
140 static void
141 _item_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
142 {
143    Elm_Hoversel_Item *item = data;
144    Evas_Object *obj2 = WIDGET(item);
145
146    elm_hoversel_hover_end(obj2);
147    if (item->func) item->func((void *)item->base.data, obj2, item);
148    evas_object_smart_callback_call(obj2, SIG_SELECTED, item);
149 }
150
151 static void
152 _activate(Evas_Object *obj)
153 {
154    Widget_Data *wd = elm_widget_data_get(obj);
155    Evas_Object *bt, *bx, *ic;
156    const Eina_List *l;
157    const Elm_Hoversel_Item *item;
158    char buf[4096];
159
160    if (!wd) return;
161    if (wd->expanded)
162      {
163         elm_hoversel_hover_end(obj);
164         return;
165      }
166    wd->expanded = EINA_TRUE;
167
168    if (elm_widget_disabled_get(obj)) return;
169    wd->hover = elm_hover_add(obj);
170    elm_widget_mirrored_automatic_set(wd->hover, EINA_FALSE);
171    if (wd->horizontal)
172      snprintf(buf, sizeof(buf), "hoversel_horizontal/%s", elm_widget_style_get(obj));
173    else
174      snprintf(buf, sizeof(buf), "hoversel_vertical/%s", elm_widget_style_get(obj));
175    elm_object_style_set(wd->hover, buf);
176    evas_object_smart_callback_add(wd->hover, "clicked", _hover_clicked, obj);
177    elm_hover_parent_set(wd->hover, wd->hover_parent);
178    elm_hover_target_set(wd->hover, wd->btn);
179
180    bx = elm_box_add(wd->hover);
181    elm_widget_mirrored_automatic_set(bx, EINA_FALSE);
182    elm_box_homogeneous_set(bx, 1);
183
184    elm_box_horizontal_set(bx, wd->horizontal);
185
186    if (wd->horizontal)
187      snprintf(buf, sizeof(buf), "hoversel_horizontal_entry/%s",
188               elm_widget_style_get(obj));
189    else
190      snprintf(buf, sizeof(buf), "hoversel_vertical_entry/%s",
191               elm_widget_style_get(obj));
192    EINA_LIST_FOREACH(wd->items, l, item)
193      {
194         bt = elm_button_add(wd->hover);
195         elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
196         elm_widget_mirrored_set(bt, elm_widget_mirrored_get(obj));
197         elm_object_style_set(bt, buf);
198         elm_object_text_set(bt, item->label);
199         if (item->icon_file)
200           {
201              ic = elm_icon_add(obj);
202              elm_icon_scale_set(ic, 0, 1);
203              if (item->icon_type == ELM_ICON_FILE)
204                elm_icon_file_set(ic, item->icon_file, item->icon_group);
205              else if (item->icon_type == ELM_ICON_STANDARD)
206                elm_icon_standard_set(ic, item->icon_file);
207              elm_button_icon_set(bt, ic);
208              evas_object_show(ic);
209           }
210         evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, 0.0);
211         evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
212         elm_box_pack_end(bx, bt);
213         evas_object_smart_callback_add(bt, "clicked", _item_clicked, item);
214         evas_object_show(bt);
215      }
216
217    if (wd->horizontal)
218      elm_hover_content_set(wd->hover,
219                            elm_hover_best_content_location_get(wd->hover,
220                                                                ELM_HOVER_AXIS_HORIZONTAL),
221                            bx);
222    else
223      elm_hover_content_set(wd->hover,
224                            elm_hover_best_content_location_get(wd->hover,
225                                                                ELM_HOVER_AXIS_VERTICAL),
226                            bx);
227    evas_object_show(bx);
228
229    evas_object_show(wd->hover);
230    evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
231
232    //   if (wd->horizontal) evas_object_hide(wd->btn);
233 }
234
235 static void
236 _activate_hook(Evas_Object *obj)
237 {
238    _activate(obj);
239 }
240
241 static void
242 _button_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
243 {
244    _activate(data);
245 }
246
247 static void
248 _parent_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
249 {
250    Widget_Data *wd = elm_widget_data_get(data);
251    if (!wd) return;
252    wd->hover_parent = NULL;
253 }
254
255 static void
256 _elm_hoversel_label_set(Evas_Object *obj, const char *item, const char *label)
257 {
258    ELM_CHECK_WIDTYPE(obj, widtype);
259    Widget_Data *wd = elm_widget_data_get(obj);
260    if (item && strcmp(item, "default")) return;
261    if (!wd) return;
262    elm_object_text_set(wd->btn, label);
263 }
264
265 static const char *
266 _elm_hoversel_label_get(const Evas_Object *obj, const char *item)
267 {
268    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
269    Widget_Data *wd = elm_widget_data_get(obj);
270    if (item && strcmp(item, "default")) return NULL;
271    if ((!wd) || (!wd->btn)) return NULL;
272    return elm_object_text_get(wd->btn);
273 }
274
275 EAPI Evas_Object *
276 elm_hoversel_add(Evas_Object *parent)
277 {
278    Evas_Object *obj;
279    Evas *e;
280    Widget_Data *wd;
281
282    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
283
284    ELM_SET_WIDTYPE(widtype, "hoversel");
285    elm_widget_type_set(obj, "hoversel");
286    elm_widget_sub_object_add(parent, obj);
287    elm_widget_data_set(obj, wd);
288    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
289    elm_widget_del_hook_set(obj, _del_hook);
290    elm_widget_theme_hook_set(obj, _theme_hook);
291    elm_widget_disable_hook_set(obj, _disable_hook);
292    elm_widget_activate_hook_set(obj, _activate_hook);
293    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
294    elm_widget_can_focus_set(obj, EINA_TRUE);
295    elm_widget_text_set_hook_set(obj, _elm_hoversel_label_set);
296    elm_widget_text_get_hook_set(obj, _elm_hoversel_label_get);
297
298    wd->btn = elm_button_add(parent);
299    elm_widget_mirrored_automatic_set(wd->btn, EINA_FALSE);
300    wd->expanded = EINA_FALSE;
301    elm_widget_resize_object_set(obj, wd->btn);
302    evas_object_event_callback_add(wd->btn, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
303                                   _changed_size_hints, obj);
304    evas_object_smart_callback_add(wd->btn, "clicked", _button_clicked, obj);
305    evas_object_smart_callbacks_descriptions_set(obj, _signals);
306
307    elm_widget_sub_object_add(obj, wd->btn);
308
309    elm_hoversel_hover_parent_set(obj, parent);
310    _theme_hook(obj);
311
312    return obj;
313 }
314
315 EAPI void
316 elm_hoversel_hover_parent_set(Evas_Object *obj, Evas_Object *parent)
317 {
318    ELM_CHECK_WIDTYPE(obj, widtype);
319    Widget_Data *wd = elm_widget_data_get(obj);
320    if (!wd) return;
321    if (wd->hover_parent)
322      evas_object_event_callback_del_full(wd->hover_parent, EVAS_CALLBACK_DEL,
323                                          _parent_del, obj);
324    wd->hover_parent = parent;
325    if (wd->hover_parent)
326      evas_object_event_callback_add(wd->hover_parent, EVAS_CALLBACK_DEL,
327                                     _parent_del, obj);
328 }
329
330 EAPI Evas_Object *
331 elm_hoversel_hover_parent_get(const Evas_Object *obj)
332 {
333    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
334    Widget_Data *wd = elm_widget_data_get(obj);
335    if (!wd) return NULL;
336    return wd->hover_parent;
337 }
338
339 EAPI void
340 elm_hoversel_label_set(Evas_Object *obj, const char *label)
341 {
342    _elm_hoversel_label_set(obj, NULL, label);
343 }
344
345 EAPI const char *
346 elm_hoversel_label_get(const Evas_Object *obj)
347 {
348    return _elm_hoversel_label_get(obj, NULL);
349 }
350
351 EAPI void
352 elm_hoversel_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
353 {
354    ELM_CHECK_WIDTYPE(obj, widtype);
355    Widget_Data *wd = elm_widget_data_get(obj);
356    if (!wd) return;
357    wd->horizontal = !!horizontal;
358 }
359
360 EAPI Eina_Bool
361 elm_hoversel_horizontal_get(const Evas_Object *obj)
362 {
363    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
364    Widget_Data *wd = elm_widget_data_get(obj);
365    if (!wd) return EINA_FALSE;
366    return wd->horizontal;
367 }
368
369 EAPI void
370 elm_hoversel_icon_set(Evas_Object *obj, Evas_Object *icon)
371 {
372    ELM_CHECK_WIDTYPE(obj, widtype);
373    Widget_Data *wd = elm_widget_data_get(obj);
374    if (!wd) return;
375    elm_button_icon_set(wd->btn, icon);
376 }
377
378 EAPI Evas_Object *
379 elm_hoversel_icon_get(const Evas_Object *obj)
380 {
381    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
382    Widget_Data *wd = elm_widget_data_get(obj);
383    if ((!wd) || (!wd->btn)) return NULL;
384    return elm_button_icon_get(wd->btn);
385 }
386
387 EAPI Evas_Object *
388 elm_hoversel_icon_unset(Evas_Object *obj)
389 {
390    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
391    Widget_Data *wd = elm_widget_data_get(obj);
392    if ((!wd) || (!wd->btn)) return NULL;
393    return elm_button_icon_unset(wd->btn);
394 }
395
396 EAPI void
397 elm_hoversel_hover_begin(Evas_Object *obj)
398 {
399    ELM_CHECK_WIDTYPE(obj, widtype);
400    Widget_Data *wd = elm_widget_data_get(obj);
401    if (!wd) return;
402    if (wd->hover) return;
403    _activate(obj);
404 }
405
406 EAPI void
407 elm_hoversel_hover_end(Evas_Object *obj)
408 {
409    ELM_CHECK_WIDTYPE(obj, widtype);
410    Widget_Data *wd = elm_widget_data_get(obj);
411    if (!wd) return;
412    if (!wd->hover) return;
413    wd->expanded = EINA_FALSE;
414    evas_object_del(wd->hover);
415    wd->hover = NULL;
416    evas_object_smart_callback_call(obj, SIG_DISMISSED, NULL);
417 }
418
419 EAPI Eina_Bool
420 elm_hoversel_expanded_get(const Evas_Object *obj)
421 {
422    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
423    Widget_Data *wd = elm_widget_data_get(obj);
424    if (!wd) return EINA_FALSE;
425    return (wd->hover) ? EINA_TRUE : EINA_FALSE;
426 }
427
428 EAPI void
429 elm_hoversel_clear(Evas_Object *obj)
430 {
431    Elm_Hoversel_Item *item;
432    Eina_List *l, *ll;
433    ELM_CHECK_WIDTYPE(obj, widtype);
434    Widget_Data *wd = elm_widget_data_get(obj);
435    if (!wd) return;
436    EINA_LIST_FOREACH_SAFE(wd->items, l, ll, item) elm_hoversel_item_del(item);
437 }
438
439 EAPI const Eina_List *
440 elm_hoversel_items_get(const Evas_Object *obj)
441 {
442    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
443    Widget_Data *wd = elm_widget_data_get(obj);
444    if (!wd) return NULL;
445    return wd->items;
446 }
447
448 EAPI Elm_Hoversel_Item *
449 elm_hoversel_item_add(Evas_Object *obj, const char *label, const char *icon_file, Elm_Icon_Type icon_type, Evas_Smart_Cb func, const void *data)
450 {
451    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
452    Widget_Data *wd = elm_widget_data_get(obj);
453    if (!wd) return NULL;
454    Elm_Hoversel_Item *item = elm_widget_item_new(obj, Elm_Hoversel_Item);
455    if (!item) return NULL;
456    wd->items = eina_list_append(wd->items, item);
457    item->label = eina_stringshare_add(label);
458    item->icon_file = eina_stringshare_add(icon_file);
459    item->icon_type = icon_type;
460    item->func = func;
461    item->base.data = data;
462    return item;
463 }
464
465 EAPI void
466 elm_hoversel_item_del(Elm_Hoversel_Item *item)
467 {
468    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
469    Widget_Data *wd = elm_widget_data_get(WIDGET(item));
470    if (!wd) return;
471    elm_hoversel_hover_end(WIDGET(item));
472    wd->items = eina_list_remove(wd->items, item);
473    elm_widget_item_pre_notify_del(item);
474    eina_stringshare_del(item->label);
475    eina_stringshare_del(item->icon_file);
476    eina_stringshare_del(item->icon_group);
477    elm_widget_item_del(item);
478 }
479
480 EAPI void
481 elm_hoversel_item_del_cb_set(Elm_Hoversel_Item *item, Evas_Smart_Cb func)
482 {
483    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
484    elm_widget_item_del_cb_set(item, func);
485 }
486
487 EAPI void *
488 elm_hoversel_item_data_get(const Elm_Hoversel_Item *item)
489 {
490    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
491    return elm_widget_item_data_get(item);
492 }
493
494 EAPI const char *
495 elm_hoversel_item_label_get(const Elm_Hoversel_Item *item)
496 {
497    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
498    return item->label;
499 }
500
501 EAPI void
502 elm_hoversel_item_icon_set(Elm_Hoversel_Item *item, const char *icon_file, const char *icon_group, Elm_Icon_Type icon_type)
503 {
504    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
505    eina_stringshare_replace(&item->icon_file, icon_file);
506    eina_stringshare_replace(&item->icon_group, icon_group);
507    item->icon_type = icon_type;
508 }
509
510 EAPI void
511 elm_hoversel_item_icon_get(const Elm_Hoversel_Item *item, const char **icon_file, const char **icon_group, Elm_Icon_Type *icon_type)
512 {
513    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
514    if (icon_file) *icon_file = item->icon_file;
515    if (icon_group) *icon_group = item->icon_group;
516    if (icon_type) *icon_type = item->icon_type;
517 }
518