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