EFL migration revision 67547
[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_object_text_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_object_text_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 static void
237 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
238 {
239    ELM_CHECK_WIDTYPE(obj, widtype);
240    Widget_Data *wd = elm_widget_data_get(obj);
241    if (!wd) return;
242    if (part && strcmp(part, "button icon")) return;
243    elm_object_part_content_set(wd->button, NULL, content);
244 }
245
246 static Evas_Object *
247 _content_get_hook(const Evas_Object *obj, const char *part)
248 {
249    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
250    Widget_Data *wd = elm_widget_data_get(obj);
251    if (!wd) return NULL;
252    if (part && strcmp(part, "button icon")) return NULL;
253    return elm_object_part_content_get(wd->button, NULL);
254 }
255
256 static Evas_Object *
257 _content_unset_hook(Evas_Object *obj, const char *part)
258 {
259    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
260    Widget_Data *wd = elm_widget_data_get(obj);
261    if (!wd) return NULL;
262    if (part && strcmp(part, "button icon")) return NULL;
263    return elm_object_part_content_unset(wd->button, NULL);
264 }
265
266 EAPI Evas_Object *
267 elm_fileselector_entry_add(Evas_Object *parent)
268 {
269    Evas_Object *obj;
270    Evas *e;
271    Widget_Data *wd;
272
273    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
274
275    ELM_SET_WIDTYPE(widtype, "fileselector_entry");
276    elm_widget_type_set(obj, "fileselector_entry");
277    elm_widget_sub_object_add(parent, obj);
278    elm_widget_data_set(obj, wd);
279    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
280    elm_widget_del_hook_set(obj, _del_hook);
281    elm_widget_disable_hook_set(obj, _disable_hook);
282    elm_widget_focus_next_hook_set(obj, _elm_fileselector_entry_focus_next_hook);
283    elm_widget_can_focus_set(obj, EINA_FALSE);
284    elm_widget_theme_hook_set(obj, _theme_hook);
285    elm_widget_text_set_hook_set(obj, _elm_fileselector_entry_button_label_set);
286    elm_widget_text_get_hook_set(obj, _elm_fileselector_entry_button_label_get);
287    elm_widget_content_set_hook_set(obj, _content_set_hook);
288    elm_widget_content_get_hook_set(obj, _content_get_hook);
289    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
290
291    wd->edje = edje_object_add(e);
292    _elm_theme_object_set(obj, wd->edje, "fileselector_entry", "base", "default");
293    elm_widget_resize_object_set(obj, wd->edje);
294
295    wd->button = elm_fileselector_button_add(obj);
296    elm_widget_mirrored_automatic_set(wd->button, EINA_FALSE);
297    ELM_SET_WIDTYPE(widtype, "fileselector_entry");
298    elm_widget_style_set(wd->button, "fileselector_entry/default");
299    edje_object_part_swallow(wd->edje, "elm.swallow.button", wd->button);
300    elm_widget_sub_object_add(obj, wd->button);
301    evas_object_event_callback_add
302       (wd->button, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
303    elm_fileselector_button_expandable_set(wd->button,
304                                           _elm_config->fileselector_expand_enable);
305
306 #define SIG_FWD(name)                                                   \
307    evas_object_smart_callback_add(wd->button, SIG_##name, _##name##_fwd, obj)
308    SIG_FWD(CLICKED);
309    SIG_FWD(UNPRESSED);
310    SIG_FWD(FILE_CHOSEN);
311 #undef SIG_FWD
312
313    wd->entry = elm_entry_add(obj);
314    elm_entry_scrollable_set(wd->entry, EINA_TRUE);
315    elm_widget_mirrored_automatic_set(wd->entry, EINA_FALSE);
316    elm_widget_style_set(wd->entry, "fileselector_entry/default");
317    elm_entry_single_line_set(wd->entry, EINA_TRUE);
318    elm_entry_editable_set(wd->entry, EINA_TRUE);
319    edje_object_part_swallow(wd->edje, "elm.swallow.entry", wd->entry);
320    elm_widget_sub_object_add(obj, wd->entry);
321    evas_object_event_callback_add
322       (wd->entry, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
323
324 #define SIG_FWD(name)                                                   \
325    evas_object_smart_callback_add(wd->entry, SIG_##name, _##name##_fwd, obj)
326    SIG_FWD(CHANGED);
327    SIG_FWD(ACTIVATED);
328    SIG_FWD(PRESS);
329    SIG_FWD(LONGPRESSED);
330    SIG_FWD(CLICKED);
331    SIG_FWD(CLICKED_DOUBLE);
332    SIG_FWD(FOCUSED);
333    SIG_FWD(UNFOCUSED);
334    SIG_FWD(SELECTION_PASTE);
335    SIG_FWD(SELECTION_COPY);
336    SIG_FWD(SELECTION_CUT);
337 #undef SIG_FWD
338
339    _mirrored_set(obj, elm_widget_mirrored_get(obj));
340    _sizing_eval(obj);
341
342    // TODO: convert Elementary to subclassing of Evas_Smart_Class
343    // TODO: and save some bytes, making descriptions per-class and not instance!
344    evas_object_smart_callbacks_descriptions_set(obj, _signals);
345    return obj;
346 }
347
348 EAPI void
349 elm_fileselector_entry_button_label_set(Evas_Object *obj, const char *label)
350 {
351    _elm_fileselector_entry_button_label_set(obj, NULL, label);
352 }
353
354 EAPI const char *
355 elm_fileselector_entry_button_label_get(const Evas_Object *obj)
356 {
357    return _elm_fileselector_entry_button_label_get(obj, NULL);
358 }
359
360 EAPI void
361 elm_fileselector_entry_selected_set(Evas_Object *obj, const char *path)
362 {
363    ELM_CHECK_WIDTYPE(obj, widtype);
364    Widget_Data *wd = elm_widget_data_get(obj);
365    if (!wd) return;
366    elm_fileselector_button_path_set(wd->button, path);
367 }
368
369 EAPI const char *
370 elm_fileselector_entry_selected_get(const Evas_Object *obj)
371 {
372    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
373    Widget_Data *wd = elm_widget_data_get(obj);
374    if (!wd) return NULL;
375    return elm_fileselector_button_path_get(wd->button);
376 }
377
378 EAPI void
379 elm_fileselector_entry_window_title_set(Evas_Object *obj, const char *title)
380 {
381    ELM_CHECK_WIDTYPE(obj, widtype);
382    Widget_Data *wd = elm_widget_data_get(obj);
383    if (!wd) return;
384    elm_fileselector_button_window_title_set(wd->button, title);
385 }
386
387 EAPI const char *
388 elm_fileselector_entry_window_title_get(const Evas_Object *obj)
389 {
390    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
391    Widget_Data *wd = elm_widget_data_get(obj);
392    if (!wd) return NULL;
393    return elm_fileselector_button_window_title_get(wd->button);
394 }
395
396 EAPI void
397 elm_fileselector_entry_window_size_set(Evas_Object *obj, Evas_Coord width, Evas_Coord height)
398 {
399    ELM_CHECK_WIDTYPE(obj, widtype);
400    Widget_Data *wd = elm_widget_data_get(obj);
401    if (!wd) return;
402    elm_fileselector_button_window_size_set(wd->button, width, height);
403 }
404
405 EAPI void
406 elm_fileselector_entry_window_size_get(const Evas_Object *obj, Evas_Coord *width, Evas_Coord *height)
407 {
408    ELM_CHECK_WIDTYPE(obj, widtype);
409    Widget_Data *wd = elm_widget_data_get(obj);
410    if (!wd) return;
411    elm_fileselector_button_window_size_get(wd->button, width, height);
412 }
413
414 EAPI void
415 elm_fileselector_entry_path_set(Evas_Object *obj, const char *path)
416 {
417    ELM_CHECK_WIDTYPE(obj, widtype);
418    Widget_Data *wd = elm_widget_data_get(obj);
419    if (!wd) return;
420    elm_fileselector_button_path_set(wd->button, path);
421    elm_object_text_set(wd->entry, path);
422 }
423
424 EAPI const char *
425 elm_fileselector_entry_path_get(const Evas_Object *obj)
426 {
427    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
428    Widget_Data *wd = elm_widget_data_get(obj);
429    if (!wd) return NULL;
430    return elm_object_text_get(wd->entry);
431 }
432
433 EAPI void
434 elm_fileselector_entry_expandable_set(Evas_Object *obj, Eina_Bool value)
435 {
436    ELM_CHECK_WIDTYPE(obj, widtype);
437    Widget_Data *wd = elm_widget_data_get(obj);
438    if (!wd) return;
439    elm_fileselector_button_expandable_set(wd->button, value);
440 }
441
442 EAPI Eina_Bool
443 elm_fileselector_entry_expandable_get(const Evas_Object *obj)
444 {
445    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
446    Widget_Data *wd = elm_widget_data_get(obj);
447    if (!wd) return EINA_FALSE;
448    return elm_fileselector_button_expandable_get(wd->button);
449 }
450
451 EAPI void
452 elm_fileselector_entry_folder_only_set(Evas_Object *obj, Eina_Bool value)
453 {
454    ELM_CHECK_WIDTYPE(obj, widtype);
455    Widget_Data *wd = elm_widget_data_get(obj);
456    if (!wd) return;
457    elm_fileselector_button_folder_only_set(wd->button, value);
458 }
459
460 EAPI Eina_Bool
461 elm_fileselector_entry_folder_only_get(const Evas_Object *obj)
462 {
463    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
464    Widget_Data *wd = elm_widget_data_get(obj);
465    if (!wd) return EINA_FALSE;
466    return elm_fileselector_button_folder_only_get(wd->button);
467 }
468
469 EAPI void
470 elm_fileselector_entry_is_save_set(Evas_Object *obj, Eina_Bool value)
471 {
472    ELM_CHECK_WIDTYPE(obj, widtype);
473    Widget_Data *wd = elm_widget_data_get(obj);
474    if (!wd) return;
475    elm_fileselector_button_is_save_set(wd->button, value);
476 }
477
478 EAPI Eina_Bool
479 elm_fileselector_entry_is_save_get(const Evas_Object *obj)
480 {
481    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
482    Widget_Data *wd = elm_widget_data_get(obj);
483    if (!wd) return EINA_FALSE;
484    return elm_fileselector_button_is_save_get(wd->button);
485 }
486
487 EAPI void
488 elm_fileselector_entry_inwin_mode_set(Evas_Object *obj, Eina_Bool value)
489 {
490    ELM_CHECK_WIDTYPE(obj, widtype);
491    Widget_Data *wd = elm_widget_data_get(obj);
492    if (!wd) return;
493    elm_fileselector_button_inwin_mode_set(wd->button, value);
494 }
495
496 EAPI Eina_Bool
497 elm_fileselector_entry_inwin_mode_get(const Evas_Object *obj)
498 {
499    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
500    Widget_Data *wd = elm_widget_data_get(obj);
501    if (!wd) return EINA_FALSE;
502    return elm_fileselector_button_inwin_mode_get(wd->button);
503 }
504
505 EAPI void
506 elm_fileselector_entry_button_icon_set(Evas_Object *obj, Evas_Object *icon)
507 {
508    _content_set_hook(obj, NULL, icon);
509 }
510
511 EAPI Evas_Object *
512 elm_fileselector_entry_button_icon_get(const Evas_Object *obj)
513 {
514    return _content_get_hook(obj, NULL);
515 }
516
517 EAPI Evas_Object *
518 elm_fileselector_entry_button_icon_unset(Evas_Object *obj)
519 {
520    return _content_unset_hook(obj, NULL);
521 }