elm: Use appropriate add/del_full for evas event callbacks.
[framework/uifw/elementary.git] / src / lib / elc_fileselector_entry.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 *edje;
9    Evas_Object *button;
10    Evas_Object *entry;
11 };
12
13 static const char *widtype = NULL;
14
15 static const char SIG_CHANGED[] = "changed";
16 static const char SIG_ACTIVATED[] = "activated";
17 static const char SIG_PRESS[] = "press";
18 static const char SIG_LONGPRESSED[] = "longpressed";
19 static const char SIG_CLICKED[] = "clicked";
20 static const char SIG_CLICKED_DOUBLE[] = "clicked,double";
21 static const char SIG_FOCUSED[] = "focused";
22 static const char SIG_UNFOCUSED[] = "unfocused";
23 static const char SIG_SELECTION_PASTE[] = "selection,paste";
24 static const char SIG_SELECTION_COPY[] = "selection,copy";
25 static const char SIG_SELECTION_CUT[] = "selection,cut";
26 static const char SIG_UNPRESSED[] = "unpressed";
27 static const char SIG_FILE_CHOSEN[] = "file,chosen";
28 static const Evas_Smart_Cb_Description _signals[] =
29 {
30   {SIG_CHANGED, ""},
31   {SIG_ACTIVATED, ""},
32   {SIG_PRESS, ""},
33   {SIG_LONGPRESSED, ""},
34   {SIG_CLICKED, ""},
35   {SIG_CLICKED_DOUBLE, ""},
36   {SIG_FOCUSED, ""},
37   {SIG_UNFOCUSED, ""},
38   {SIG_SELECTION_PASTE, ""},
39   {SIG_SELECTION_COPY, ""},
40   {SIG_SELECTION_CUT, ""},
41   {SIG_UNPRESSED, ""},
42   {SIG_FILE_CHOSEN, "s"},
43   {NULL, NULL}
44 };
45
46 #define SIG_FWD(name)                                                    \
47 static void                                                              \
48 _##name##_fwd(void *data, Evas_Object *obj __UNUSED__, void *event_info) \
49 {                                                                        \
50    evas_object_smart_callback_call(data, SIG_##name, event_info);        \
51 }
52 SIG_FWD(CHANGED)
53 SIG_FWD(PRESS)
54 SIG_FWD(LONGPRESSED)
55 SIG_FWD(CLICKED)
56 SIG_FWD(CLICKED_DOUBLE)
57 SIG_FWD(FOCUSED)
58 SIG_FWD(UNFOCUSED)
59 SIG_FWD(SELECTION_PASTE)
60 SIG_FWD(SELECTION_COPY)
61 SIG_FWD(SELECTION_CUT)
62 SIG_FWD(UNPRESSED)
63 #undef SIG_FWD
64
65 static void _del_pre_hook(Evas_Object *obj);
66 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
67 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
68
69 static void
70 _FILE_CHOSEN_fwd(void *data, Evas_Object *obj __UNUSED__, void *event_info)
71 {
72    Widget_Data *wd = elm_widget_data_get(data);
73    const char *file = event_info;
74    elm_entry_entry_set(wd->entry, file);
75    evas_object_smart_callback_call(data, SIG_FILE_CHOSEN, event_info);
76 }
77
78 static void
79 _ACTIVATED_fwd(void *data, Evas_Object *obj __UNUSED__, void *event_info)
80 {
81    Widget_Data *wd = elm_widget_data_get(data);
82    const char *file = elm_entry_entry_get(wd->entry);
83    elm_fileselector_button_path_set(wd->button, file);
84    evas_object_smart_callback_call(data, SIG_ACTIVATED, event_info);
85 }
86
87 static void
88 _del_pre_hook(Evas_Object *obj)
89 {
90    Widget_Data *wd = elm_widget_data_get(obj);
91    if (!wd) return;
92    evas_object_event_callback_del_full
93       (wd->button, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
94    evas_object_event_callback_del_full
95       (wd->entry, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
96 }
97
98 static void
99 _del_hook(Evas_Object *obj)
100 {
101    Widget_Data *wd = elm_widget_data_get(obj);
102    free(wd);
103 }
104
105 static void
106 _sizing_eval(Evas_Object *obj)
107 {
108    Widget_Data *wd = elm_widget_data_get(obj);
109    Evas_Coord minw = -1, minh = -1;
110    if (!wd) return;
111    edje_object_size_min_calc(wd->edje, &minw, &minh);
112    evas_object_size_hint_min_set(obj, minw, minh);
113    evas_object_size_hint_max_set(obj, -1, -1);
114 }
115
116 static Eina_Bool
117 _elm_fileselector_entry_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
118 {
119    Widget_Data *wd = elm_widget_data_get(obj);
120
121    if (!wd)
122      return EINA_FALSE;
123
124    Evas_Object *chain[2];
125
126    /* Direction */
127    if (dir == ELM_FOCUS_PREVIOUS)
128      {
129         chain[0] = wd->button;
130         chain[1] = wd->entry;
131      }
132    else if (dir == ELM_FOCUS_NEXT)
133      {
134         chain[0] = wd->entry;
135         chain[1] = wd->button;
136      }
137    else
138      return EINA_FALSE;
139
140    unsigned char i = elm_widget_focus_get(chain[1]);
141
142    if (elm_widget_focus_next_get(chain[i], dir, next))
143      return EINA_TRUE;
144
145    i = !i;
146
147    Evas_Object *to_focus;
148    if (elm_widget_focus_next_get(chain[i], dir, &to_focus))
149      {
150         *next = to_focus;
151         return !!i;
152      }
153
154    return EINA_FALSE;
155 }
156
157 static void
158 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
159 {
160    Widget_Data *wd = elm_widget_data_get(obj);
161    if (!wd) return;
162    elm_widget_mirrored_set(wd->button, rtl);
163    edje_object_mirrored_set(wd->edje, rtl);
164 }
165
166 static void
167 _theme_hook(Evas_Object *obj)
168 {
169    Widget_Data *wd = elm_widget_data_get(obj);
170    const char *style = elm_widget_style_get(obj);
171    char buf[1024];
172
173    if (!wd) return;
174    _elm_widget_mirrored_reload(obj);
175    _mirrored_set(obj, elm_widget_mirrored_get(obj));
176
177    _elm_theme_object_set(obj, wd->edje, "fileselector_entry", "base", style);
178    if (elm_object_disabled_get(obj))
179       edje_object_signal_emit(wd->edje, "elm,state,disabled", "elm");
180
181    if (!style) style = "default";
182    snprintf(buf, sizeof(buf), "fileselector_entry/%s", style);
183    elm_widget_style_set(wd->button, buf);
184    elm_widget_style_set(wd->entry, buf);
185
186    edje_object_part_swallow(obj, "elm.swallow.button", wd->button);
187    edje_object_part_swallow(obj, "elm.swallow.entry", wd->entry);
188
189    edje_object_message_signal_process(wd->edje);
190    edje_object_scale_set
191      (wd->edje, elm_widget_scale_get(obj) * _elm_config->scale);
192    _sizing_eval(obj);
193 }
194
195 static void
196 _disable_hook(Evas_Object *obj)
197 {
198    Widget_Data *wd = elm_widget_data_get(obj);
199    Eina_Bool val = elm_widget_disabled_get(obj);
200    if (!wd) return;
201    if (val)
202      edje_object_signal_emit(wd->edje, "elm,state,disabled", "elm");
203    else
204      edje_object_signal_emit(wd->edje, "elm,state,enabled", "elm");
205
206    elm_widget_disabled_set(wd->button, val);
207    elm_widget_disabled_set(wd->entry, val);
208 }
209
210 static void
211 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
212 {
213    _sizing_eval(data);
214 }
215
216 static void
217 _elm_fileselector_entry_button_label_set(Evas_Object *obj, const char *item, const char *label)
218 {
219    ELM_CHECK_WIDTYPE(obj, widtype);
220    Widget_Data *wd = elm_widget_data_get(obj);
221    if (item && strcmp(item, "default")) return;
222    if (!wd) return;
223    elm_object_text_set(wd->button, label);
224 }
225
226 static const char *
227 _elm_fileselector_entry_button_label_get(const Evas_Object *obj, const char *item)
228 {
229    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
230    Widget_Data *wd = elm_widget_data_get(obj);
231    if (item && strcmp(item, "default")) return NULL;
232    if (!wd) return NULL;
233    return elm_object_text_get(wd->button);
234 }
235
236 EAPI Evas_Object *
237 elm_fileselector_entry_add(Evas_Object *parent)
238 {
239    Evas_Object *obj;
240    Evas *e;
241    Widget_Data *wd;
242
243    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
244
245    ELM_SET_WIDTYPE(widtype, "fileselector_entry");
246    elm_widget_type_set(obj, "fileselector_entry");
247    elm_widget_sub_object_add(parent, obj);
248    elm_widget_data_set(obj, wd);
249    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
250    elm_widget_del_hook_set(obj, _del_hook);
251    elm_widget_disable_hook_set(obj, _disable_hook);
252    elm_widget_focus_next_hook_set(obj, _elm_fileselector_entry_focus_next_hook);
253    elm_widget_can_focus_set(obj, EINA_FALSE);
254    elm_widget_theme_hook_set(obj, _theme_hook);
255    elm_widget_text_set_hook_set(obj, _elm_fileselector_entry_button_label_set);
256    elm_widget_text_get_hook_set(obj, _elm_fileselector_entry_button_label_get);
257
258    wd->edje = edje_object_add(e);
259    _elm_theme_object_set(obj, wd->edje, "fileselector_entry", "base", "default");
260    elm_widget_resize_object_set(obj, wd->edje);
261
262    wd->button = elm_fileselector_button_add(obj);
263    elm_widget_mirrored_automatic_set(wd->button, EINA_FALSE);
264    ELM_SET_WIDTYPE(widtype, "fileselector_entry");
265    elm_widget_style_set(wd->button, "fileselector_entry/default");
266    edje_object_part_swallow(wd->edje, "elm.swallow.button", wd->button);
267    elm_widget_sub_object_add(obj, wd->button);
268    evas_object_event_callback_add
269       (wd->button, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
270    elm_fileselector_button_expandable_set(wd->button,
271                                           _elm_config->fileselector_expand_enable);
272
273 #define SIG_FWD(name)                                                   \
274    evas_object_smart_callback_add(wd->button, SIG_##name, _##name##_fwd, obj)
275    SIG_FWD(CLICKED);
276    SIG_FWD(UNPRESSED);
277    SIG_FWD(FILE_CHOSEN);
278 #undef SIG_FWD
279
280    wd->entry = elm_entry_add(obj);
281    elm_entry_scrollable_set(wd->entry, EINA_TRUE);
282    elm_widget_mirrored_automatic_set(wd->entry, EINA_FALSE);
283    elm_widget_style_set(wd->entry, "fileselector_entry/default");
284    elm_entry_single_line_set(wd->entry, EINA_TRUE);
285    elm_entry_editable_set(wd->entry, EINA_TRUE);
286    edje_object_part_swallow(wd->edje, "elm.swallow.entry", wd->entry);
287    elm_widget_sub_object_add(obj, wd->entry);
288    evas_object_event_callback_add
289       (wd->entry, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
290
291 #define SIG_FWD(name)                                                   \
292    evas_object_smart_callback_add(wd->entry, SIG_##name, _##name##_fwd, obj)
293    SIG_FWD(CHANGED);
294    SIG_FWD(ACTIVATED);
295    SIG_FWD(PRESS);
296    SIG_FWD(LONGPRESSED);
297    SIG_FWD(CLICKED);
298    SIG_FWD(CLICKED_DOUBLE);
299    SIG_FWD(FOCUSED);
300    SIG_FWD(UNFOCUSED);
301    SIG_FWD(SELECTION_PASTE);
302    SIG_FWD(SELECTION_COPY);
303    SIG_FWD(SELECTION_CUT);
304 #undef SIG_FWD
305
306    _mirrored_set(obj, elm_widget_mirrored_get(obj));
307    _sizing_eval(obj);
308
309    // TODO: convert Elementary to subclassing of Evas_Smart_Class
310    // TODO: and save some bytes, making descriptions per-class and not instance!
311    evas_object_smart_callbacks_descriptions_set(obj, _signals);
312    return obj;
313 }
314
315 EAPI void
316 elm_fileselector_entry_button_label_set(Evas_Object *obj, const char *label)
317 {
318    _elm_fileselector_entry_button_label_set(obj, NULL, label);
319 }
320
321 EAPI const char *
322 elm_fileselector_entry_button_label_get(const Evas_Object *obj)
323 {
324    return _elm_fileselector_entry_button_label_get(obj, NULL);
325 }
326
327 EAPI void
328 elm_fileselector_entry_selected_set(Evas_Object *obj, const char *path)
329 {
330    ELM_CHECK_WIDTYPE(obj, widtype);
331    Widget_Data *wd = elm_widget_data_get(obj);
332    if (!wd) return;
333    elm_fileselector_button_path_set(wd->button, path);
334 }
335
336 EAPI const char *
337 elm_fileselector_entry_selected_get(const Evas_Object *obj)
338 {
339    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
340    Widget_Data *wd = elm_widget_data_get(obj);
341    if (!wd) return NULL;
342    return elm_fileselector_button_path_get(wd->button);
343 }
344
345 EAPI void
346 elm_fileselector_entry_window_title_set(Evas_Object *obj, const char *title)
347 {
348    ELM_CHECK_WIDTYPE(obj, widtype);
349    Widget_Data *wd = elm_widget_data_get(obj);
350    if (!wd) return;
351    elm_fileselector_button_window_title_set(wd->button, title);
352 }
353
354 EAPI const char *
355 elm_fileselector_entry_window_title_get(const Evas_Object *obj)
356 {
357    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
358    Widget_Data *wd = elm_widget_data_get(obj);
359    if (!wd) return NULL;
360    return elm_fileselector_button_window_title_get(wd->button);
361 }
362
363 EAPI void
364 elm_fileselector_entry_window_size_set(Evas_Object *obj, Evas_Coord width, Evas_Coord height)
365 {
366    ELM_CHECK_WIDTYPE(obj, widtype);
367    Widget_Data *wd = elm_widget_data_get(obj);
368    if (!wd) return;
369    elm_fileselector_button_window_size_set(wd->button, width, height);
370 }
371
372 EAPI void
373 elm_fileselector_entry_window_size_get(const Evas_Object *obj, Evas_Coord *width, Evas_Coord *height)
374 {
375    ELM_CHECK_WIDTYPE(obj, widtype);
376    Widget_Data *wd = elm_widget_data_get(obj);
377    if (!wd) return;
378    elm_fileselector_button_window_size_get(wd->button, width, height);
379 }
380
381 EAPI void
382 elm_fileselector_entry_path_set(Evas_Object *obj, const char *path)
383 {
384    ELM_CHECK_WIDTYPE(obj, widtype);
385    Widget_Data *wd = elm_widget_data_get(obj);
386    if (!wd) return;
387    elm_fileselector_button_path_set(wd->button, path);
388    elm_entry_entry_set(wd->entry, path);
389 }
390
391 EAPI const char *
392 elm_fileselector_entry_path_get(const Evas_Object *obj)
393 {
394    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
395    Widget_Data *wd = elm_widget_data_get(obj);
396    if (!wd) return NULL;
397    return elm_entry_entry_get(wd->entry);
398 }
399
400 EAPI void
401 elm_fileselector_entry_expandable_set(Evas_Object *obj, Eina_Bool value)
402 {
403    ELM_CHECK_WIDTYPE(obj, widtype);
404    Widget_Data *wd = elm_widget_data_get(obj);
405    if (!wd) return;
406    elm_fileselector_button_expandable_set(wd->button, value);
407 }
408
409 EAPI Eina_Bool
410 elm_fileselector_entry_expandable_get(const Evas_Object *obj)
411 {
412    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
413    Widget_Data *wd = elm_widget_data_get(obj);
414    if (!wd) return EINA_FALSE;
415    return elm_fileselector_button_expandable_get(wd->button);
416 }
417
418 EAPI void
419 elm_fileselector_entry_folder_only_set(Evas_Object *obj, Eina_Bool value)
420 {
421    ELM_CHECK_WIDTYPE(obj, widtype);
422    Widget_Data *wd = elm_widget_data_get(obj);
423    if (!wd) return;
424    elm_fileselector_button_folder_only_set(wd->button, value);
425 }
426
427 EAPI Eina_Bool
428 elm_fileselector_entry_folder_only_get(const Evas_Object *obj)
429 {
430    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
431    Widget_Data *wd = elm_widget_data_get(obj);
432    if (!wd) return EINA_FALSE;
433    return elm_fileselector_button_folder_only_get(wd->button);
434 }
435
436 EAPI void
437 elm_fileselector_entry_is_save_set(Evas_Object *obj, Eina_Bool value)
438 {
439    ELM_CHECK_WIDTYPE(obj, widtype);
440    Widget_Data *wd = elm_widget_data_get(obj);
441    if (!wd) return;
442    elm_fileselector_button_is_save_set(wd->button, value);
443 }
444
445 EAPI Eina_Bool
446 elm_fileselector_entry_is_save_get(const Evas_Object *obj)
447 {
448    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
449    Widget_Data *wd = elm_widget_data_get(obj);
450    if (!wd) return EINA_FALSE;
451    return elm_fileselector_button_is_save_get(wd->button);
452 }
453
454 EAPI void
455 elm_fileselector_entry_inwin_mode_set(Evas_Object *obj, Eina_Bool value)
456 {
457    ELM_CHECK_WIDTYPE(obj, widtype);
458    Widget_Data *wd = elm_widget_data_get(obj);
459    if (!wd) return;
460    elm_fileselector_button_inwin_mode_set(wd->button, value);
461 }
462
463 EAPI Eina_Bool
464 elm_fileselector_entry_inwin_mode_get(const Evas_Object *obj)
465 {
466    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
467    Widget_Data *wd = elm_widget_data_get(obj);
468    if (!wd) return EINA_FALSE;
469    return elm_fileselector_button_inwin_mode_get(wd->button);
470 }
471
472 EAPI void
473 elm_fileselector_entry_button_icon_set(Evas_Object *obj, Evas_Object *icon)
474 {
475    ELM_CHECK_WIDTYPE(obj, widtype);
476    Widget_Data *wd = elm_widget_data_get(obj);
477    if (!wd) return;
478    elm_fileselector_button_icon_set(wd->button, icon);
479 }
480
481 EAPI Evas_Object *
482 elm_fileselector_entry_button_icon_get(const Evas_Object *obj)
483 {
484    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
485    Widget_Data *wd = elm_widget_data_get(obj);
486    if (!wd) return NULL;
487    return elm_fileselector_button_icon_get(wd->button);
488 }
489
490 EAPI Evas_Object *
491 elm_fileselector_entry_button_icon_unset(Evas_Object *obj)
492 {
493    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
494    Widget_Data *wd = elm_widget_data_get(obj);
495    if (!wd) return NULL;
496    return elm_fileselector_button_icon_unset(wd->button);
497 }