Elementary: Added ui-mirroring support for all the widgets.
[framework/uifw/elementary.git] / src / lib / elc_fileselector_entry.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4  /**
5  * @defgroup File_Selector_Entry File Selector Entry
6  *
7  * An entry that shows to enter/display path and have an associated
8  * button to allow selecting the file from a dialog.
9  *
10  * The button, when clicked, creates an Elementary window (or inner
11  * window) with an Elementary File Selector within. When a file is
12  * chosen, the (inner) window is closed and the selected file is
13  * exposed as an evas_object_smart_callback_call() of the button.
14  */
15
16 typedef struct _Widget_Data Widget_Data;
17
18 struct _Widget_Data
19 {
20    Evas_Object *edje;
21    Evas_Object *button;
22    Evas_Object *entry;
23 };
24
25 static const char *widtype = NULL;
26
27 static const char SIG_CHANGED[] = "changed";
28 static const char SIG_ACTIVATED[] = "activated";
29 static const char SIG_PRESS[] = "press";
30 static const char SIG_LONGPRESSED[] = "longpressed";
31 static const char SIG_CLICKED[] = "clicked";
32 static const char SIG_CLICKED_DOUBLE[] = "clicked,double";
33 static const char SIG_FOCUSED[] = "focused";
34 static const char SIG_UNFOCUSED[] = "unfocused";
35 static const char SIG_SELECTION_PASTE[] = "selection,paste";
36 static const char SIG_SELECTION_COPY[] = "selection,copy";
37 static const char SIG_SELECTION_CUT[] = "selection,cut";
38 static const char SIG_UNPRESSED[] = "unpressed";
39 static const char SIG_FILE_CHOSEN[] = "file,chosen";
40 static const Evas_Smart_Cb_Description _signals[] =
41 {
42   {SIG_CHANGED, ""},
43   {SIG_ACTIVATED, ""},
44   {SIG_PRESS, ""},
45   {SIG_LONGPRESSED, ""},
46   {SIG_CLICKED, ""},
47   {SIG_CLICKED_DOUBLE, ""},
48   {SIG_FOCUSED, ""},
49   {SIG_UNFOCUSED, ""},
50   {SIG_SELECTION_PASTE, ""},
51   {SIG_SELECTION_COPY, ""},
52   {SIG_SELECTION_CUT, ""},
53   {SIG_UNPRESSED, ""},
54   {SIG_FILE_CHOSEN, "s"},
55   {NULL, NULL}
56 };
57
58 #define SIG_FWD(name)                                                    \
59 static void                                                              \
60 _##name##_fwd(void *data, Evas_Object *obj __UNUSED__, void *event_info) \
61 {                                                                        \
62    evas_object_smart_callback_call(data, SIG_##name, event_info);        \
63 }
64 SIG_FWD(CHANGED)
65 SIG_FWD(PRESS)
66 SIG_FWD(LONGPRESSED)
67 SIG_FWD(CLICKED)
68 SIG_FWD(CLICKED_DOUBLE)
69 SIG_FWD(FOCUSED)
70 SIG_FWD(UNFOCUSED)
71 SIG_FWD(SELECTION_PASTE)
72 SIG_FWD(SELECTION_COPY)
73 SIG_FWD(SELECTION_CUT)
74 SIG_FWD(UNPRESSED)
75 #undef SIG_FWD
76
77 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
78
79 static void
80 _FILE_CHOSEN_fwd(void *data, Evas_Object *obj __UNUSED__, void *event_info)
81 {
82    Widget_Data *wd = elm_widget_data_get(data);
83    const char *file = event_info;
84    elm_scrolled_entry_entry_set(wd->entry, file);
85    evas_object_smart_callback_call(data, SIG_FILE_CHOSEN, event_info);
86 }
87
88 static void
89 _ACTIVATED_fwd(void *data, Evas_Object *obj __UNUSED__, void *event_info)
90 {
91    Widget_Data *wd = elm_widget_data_get(data);
92    const char *file = elm_scrolled_entry_entry_get(wd->entry);
93    elm_fileselector_button_path_set(wd->button, file);
94    evas_object_smart_callback_call(data, SIG_ACTIVATED, event_info);
95 }
96
97 static void
98 _del_hook(Evas_Object *obj)
99 {
100    Widget_Data *wd = elm_widget_data_get(obj);
101    free(wd);
102 }
103
104 static void
105 _sizing_eval(Evas_Object *obj)
106 {
107    Widget_Data *wd = elm_widget_data_get(obj);
108    Evas_Coord minw = -1, minh = -1;
109    if (!wd) return;
110    edje_object_size_min_calc(wd->edje, &minw, &minh);
111    evas_object_size_hint_min_set(obj, minw, minh);
112    evas_object_size_hint_max_set(obj, -1, -1);
113 }
114
115 static Eina_Bool
116 _elm_fileselector_entry_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
117 {
118    Widget_Data *wd = elm_widget_data_get(obj);
119
120    if (!wd)
121      return EINA_FALSE;
122
123    Evas_Object *chain[2];
124
125    /* Direction */
126    if (dir == ELM_FOCUS_PREVIOUS)
127      {
128         chain[0] = wd->button;
129         chain[1] = wd->entry;
130      }
131    else if (dir == ELM_FOCUS_NEXT)
132      {
133         chain[0] = wd->entry;
134         chain[1] = wd->button;
135      }
136    else
137      return EINA_FALSE;
138
139    unsigned char i = elm_widget_focus_get(chain[1]);
140
141    if (elm_widget_focus_next_get(chain[i], dir, next))
142      return EINA_TRUE;
143
144    i = !i;
145
146    Evas_Object *to_focus;
147    if (elm_widget_focus_next_get(chain[i], dir, &to_focus))
148      {
149         *next = to_focus;
150         return !!i;
151      }
152
153    return EINA_FALSE;
154 }
155
156 static void
157 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
158 {
159    Widget_Data *wd = elm_widget_data_get(obj);
160    if (!wd) return;
161    elm_widget_mirrored_set(wd->button, rtl);
162    edje_object_mirrored_set(wd->edje, rtl);
163 }
164
165 static void
166 _theme_hook(Evas_Object *obj)
167 {
168    Widget_Data *wd = elm_widget_data_get(obj);
169    const char *style = elm_widget_style_get(obj);
170    char buf[1024];
171
172    if (!wd) return;
173    _mirrored_set(obj, elm_widget_mirrored_get(obj));
174    _elm_theme_object_set(obj, wd->edje, "fileselector_entry", "base", style);
175    if (elm_object_disabled_get(obj))
176       edje_object_signal_emit(wd->edje, "elm,state,disabled", "elm");
177
178    if (!style) style = "default";
179    snprintf(buf, sizeof(buf), "fileselector_entry/%s", style);
180    elm_widget_style_set(wd->button, buf);
181    elm_widget_style_set(wd->entry, buf);
182
183    edje_object_part_swallow(obj, "elm.swallow.button", wd->button);
184    edje_object_part_swallow(obj, "elm.swallow.entry", wd->entry);
185
186    edje_object_message_signal_process(wd->edje);
187    edje_object_scale_set
188      (wd->edje, elm_widget_scale_get(obj) * _elm_config->scale);
189    _sizing_eval(obj);
190 }
191
192 static void
193 _disable_hook(Evas_Object *obj)
194 {
195    Widget_Data *wd = elm_widget_data_get(obj);
196    Eina_Bool val = elm_widget_disabled_get(obj);
197    if (!wd) return;
198    if (val)
199      edje_object_signal_emit(wd->edje, "elm,state,disabled", "elm");
200    else
201      edje_object_signal_emit(wd->edje, "elm,state,enabled", "elm");
202
203    elm_widget_disabled_set(wd->button, val);
204    elm_widget_disabled_set(wd->entry, val);
205 }
206
207 static void
208 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
209 {
210    _sizing_eval(data);
211 }
212
213 /**
214  * Add a new file selector entry into the parent object.
215  *
216  * @param parent The parent object
217  * @return The new object or NULL if it cannot be created
218  *
219  * @ingroup File_Selector_Entry
220  */
221 EAPI Evas_Object *
222 elm_fileselector_entry_add(Evas_Object *parent)
223 {
224    Evas_Object *obj;
225    Evas *e = evas_object_evas_get(parent);
226    if (!e) return NULL;
227    Widget_Data *wd;
228
229    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
230
231    wd = ELM_NEW(Widget_Data);
232
233    obj = elm_widget_add(e);
234    ELM_SET_WIDTYPE(widtype, "fileselector_entry");
235    elm_widget_type_set(obj, "fileselector_entry");
236    elm_widget_sub_object_add(parent, obj);
237    elm_widget_data_set(obj, wd);
238    elm_widget_del_hook_set(obj, _del_hook);
239    elm_widget_disable_hook_set(obj, _disable_hook);
240    elm_widget_focus_next_hook_set(obj, _elm_fileselector_entry_focus_next_hook);
241    elm_widget_can_focus_set(obj, EINA_FALSE);
242    elm_widget_theme_hook_set(obj, _theme_hook);
243
244    wd->edje = edje_object_add(e);
245    _elm_theme_object_set(obj, wd->edje, "fileselector_entry", "base", "default");
246    elm_widget_resize_object_set(obj, wd->edje);
247
248    wd->button = elm_fileselector_button_add(obj);
249    elm_widget_style_set(wd->button, "fileselector_entry/default");
250    edje_object_part_swallow(wd->edje, "elm.swallow.button", wd->button);
251    elm_widget_sub_object_add(obj, wd->button);
252    evas_object_event_callback_add
253      (wd->button, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
254    elm_fileselector_button_expandable_set(wd->button,
255                                           _elm_config->fileselector_expand_enable);
256
257 #define SIG_FWD(name)                                                   \
258    evas_object_smart_callback_add(wd->button, SIG_##name, _##name##_fwd, obj)
259    SIG_FWD(CLICKED);
260    SIG_FWD(UNPRESSED);
261    SIG_FWD(FILE_CHOSEN);
262 #undef SIG_FWD
263
264    wd->entry = elm_scrolled_entry_add(obj);
265    elm_widget_style_set(wd->entry, "fileselector_entry/default");
266    elm_scrolled_entry_single_line_set(wd->entry, EINA_TRUE);
267    elm_scrolled_entry_editable_set(wd->entry, EINA_TRUE);
268    edje_object_part_swallow(wd->edje, "elm.swallow.entry", wd->entry);
269    elm_widget_sub_object_add(obj, wd->entry);
270    evas_object_event_callback_add
271      (wd->entry, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
272
273 #define SIG_FWD(name)                                                   \
274    evas_object_smart_callback_add(wd->entry, SIG_##name, _##name##_fwd, obj)
275    SIG_FWD(CHANGED);
276    SIG_FWD(ACTIVATED);
277    SIG_FWD(PRESS);
278    SIG_FWD(LONGPRESSED);
279    SIG_FWD(CLICKED);
280    SIG_FWD(CLICKED_DOUBLE);
281    SIG_FWD(FOCUSED);
282    SIG_FWD(UNFOCUSED);
283    SIG_FWD(SELECTION_PASTE);
284    SIG_FWD(SELECTION_COPY);
285    SIG_FWD(SELECTION_CUT);
286 #undef SIG_FWD
287
288    _mirrored_set(obj, elm_widget_mirrored_get(obj));
289    _sizing_eval(obj);
290
291    // TODO: convert Elementary to subclassing of Evas_Smart_Class
292    // TODO: and save some bytes, making descriptions per-class and not instance!
293    evas_object_smart_callbacks_descriptions_set(obj, _signals);
294    return obj;
295 }
296
297 /**
298  * Set the label used in the file selector entry.
299  *
300  * @param obj The entry object
301  * @param label The text label text to be displayed on the entry
302  *
303  * @ingroup File_Selector_Entry
304  */
305 EAPI void
306 elm_fileselector_entry_button_label_set(Evas_Object *obj, const char *label)
307 {
308    ELM_CHECK_WIDTYPE(obj, widtype);
309    Widget_Data *wd = elm_widget_data_get(obj);
310    if (!wd) return;
311    elm_fileselector_button_label_set(wd->button, label);
312 }
313
314 EAPI const char *
315 elm_fileselector_entry_button_label_get(const Evas_Object *obj)
316 {
317    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
318    Widget_Data *wd = elm_widget_data_get(obj);
319    if (!wd) return NULL;
320    return elm_fileselector_button_label_get(wd->button);
321 }
322
323 /**
324  * Set the path to start the entry's file selector with, when clicked.
325  *
326  * @param obj The entry object
327  * @param path Path to a file/directory
328  *
329  * Default path is "HOME" environment variable's value.
330  *
331  * @ingroup File_Selector_Entry
332  */
333 EAPI void
334 elm_fileselector_entry_selected_set(Evas_Object *obj, const char *path)
335 {
336    ELM_CHECK_WIDTYPE(obj, widtype);
337    Widget_Data *wd = elm_widget_data_get(obj);
338    if (!wd) return;
339    elm_fileselector_button_path_set(wd->button, path);
340 }
341
342 /**
343  * Get the <b>last</b> path which the entry's file selector was set to.
344  *
345  * @param obj The entry object
346  * @param path Path to a file/directory
347  *
348  * Default path is "HOME" environment variable's value.
349  *
350  * @ingroup File_Selector_Entry
351  */
352 EAPI const char *
353 elm_fileselector_entry_selected_get(const Evas_Object *obj)
354 {
355    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
356    Widget_Data *wd = elm_widget_data_get(obj);
357    if (!wd) return NULL;
358    return elm_fileselector_button_path_get(wd->button);
359 }
360
361 /**
362  * Set the title of the file selector entry's window.
363  *
364  * @param obj The entry object
365  * @param title The title string
366  *
367  * Note that it will only take any effect if the fileselector entry
368  * not at "inwin mode".
369  *
370  * @ingroup File_Selector_Entry
371  */
372 EAPI void
373 elm_fileselector_entry_window_title_set(Evas_Object *obj, const char *title)
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_title_set(wd->button, title);
379 }
380
381 /**
382  * Get the title of the file selector entry's window.
383  *
384  * @param obj The entry object
385  *
386  * @ingroup File_Selector_Entry
387  */
388 EAPI const char *
389 elm_fileselector_entry_window_title_get(const Evas_Object *obj)
390 {
391    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
392    Widget_Data *wd = elm_widget_data_get(obj);
393    if (!wd) return NULL;
394    return elm_fileselector_button_window_title_get(wd->button);
395 }
396
397 /**
398  * Set the size of the file selector entry's window.
399  *
400  * @param obj The entry object
401  * @param width The width
402  * @param height The height
403  *
404  * Note that it will only take any effect if the fileselector entry not at
405  * "inwin mode". Default size for the window (when applicable) is 400x400.
406  *
407  * @ingroup File_Selector_Entry
408  */
409 EAPI void
410 elm_fileselector_entry_window_size_set(Evas_Object *obj, Evas_Coord width, Evas_Coord height)
411 {
412    ELM_CHECK_WIDTYPE(obj, widtype);
413    Widget_Data *wd = elm_widget_data_get(obj);
414    if (!wd) return;
415    elm_fileselector_button_window_size_set(wd->button, width, height);
416 }
417
418 /**
419  * Get the size of the file selector entry's window.
420  *
421  * @param obj The entry object
422  * @param width Pointer into which to store the width value
423  * @param height Pointer into which to store the height value
424  *
425  * @ingroup File_Selector_Entry
426  */
427 EAPI void
428 elm_fileselector_entry_window_size_get(const Evas_Object *obj, Evas_Coord *width, Evas_Coord *height)
429 {
430    ELM_CHECK_WIDTYPE(obj, widtype);
431    Widget_Data *wd = elm_widget_data_get(obj);
432    if (!wd) return;
433    elm_fileselector_button_window_size_get(wd->button, width, height);
434 }
435
436 /**
437  * Set the starting path of the file selector entry's window.
438  *
439  * @param obj The entry object
440  * @param path The path string
441  *
442  * It must be a <b>directory</b> path.
443  *
444  * @ingroup File_Selector_Entry
445  */
446 EAPI void
447 elm_fileselector_entry_path_set(Evas_Object *obj, const char *path)
448 {
449    ELM_CHECK_WIDTYPE(obj, widtype);
450    Widget_Data *wd = elm_widget_data_get(obj);
451    if (!wd) return;
452    elm_fileselector_button_path_set(wd->button, path);
453    elm_scrolled_entry_entry_set(wd->entry, path);
454 }
455
456 /**
457  * Get the <b>last</b> path of the file selector entry's window.
458  *
459  * @param obj The entry object
460  *
461  * @ingroup File_Selector_Entry
462  */
463 EAPI const char *
464 elm_fileselector_entry_path_get(const Evas_Object *obj)
465 {
466    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
467    Widget_Data *wd = elm_widget_data_get(obj);
468    if (!wd) return NULL;
469    return elm_scrolled_entry_entry_get(wd->entry);
470 }
471
472 /**
473  * Set whether the entry's file selector is to present itself as an
474  * Elementary Generic List (which will expand its entries for nested
475  * directories) or as canonical list, which will be rendered again
476  * with the contents of each selected directory.
477  *
478  * @param obj The entry object
479  * @param value The expandable flag
480  *
481  * @ingroup File_Selector_Entry
482  */
483 EAPI void
484 elm_fileselector_entry_expandable_set(Evas_Object *obj, Eina_Bool value)
485 {
486    ELM_CHECK_WIDTYPE(obj, widtype);
487    Widget_Data *wd = elm_widget_data_get(obj);
488    if (!wd) return;
489    elm_fileselector_button_expandable_set(wd->button, value);
490 }
491
492 /**
493  * Get the entry's file selector expandable flag.
494  *
495  * @param obj The entry object
496  * @return value The expandable flag
497  *
498  * @ingroup File_Selector_Entry
499  */
500 EAPI Eina_Bool
501 elm_fileselector_entry_expandable_get(const Evas_Object *obj)
502 {
503    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
504    Widget_Data *wd = elm_widget_data_get(obj);
505    if (!wd) return EINA_FALSE;
506    return elm_fileselector_button_expandable_get(wd->button);
507 }
508
509 /**
510  * Set whether the entry's file selector list is to display folders
511  * only or the directory contents, as well.
512  *
513  * @param obj The entry object
514  * @param value The "folder only" flag
515  *
516  * @ingroup File_Selector_Entry
517  */
518 EAPI void
519 elm_fileselector_entry_folder_only_set(Evas_Object *obj, Eina_Bool value)
520 {
521    ELM_CHECK_WIDTYPE(obj, widtype);
522    Widget_Data *wd = elm_widget_data_get(obj);
523    if (!wd) return;
524    elm_fileselector_button_folder_only_set(wd->button, value);
525 }
526
527 /**
528  * Get the entry's file selector "folder only" flag.
529  *
530  * @param obj The entry object
531  * @return value The "folder only" flag
532  *
533  * @ingroup File_Selector_Entry
534  */
535 EAPI Eina_Bool
536 elm_fileselector_entry_folder_only_get(const Evas_Object *obj)
537 {
538    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
539    Widget_Data *wd = elm_widget_data_get(obj);
540    if (!wd) return EINA_FALSE;
541    return elm_fileselector_button_folder_only_get(wd->button);
542 }
543
544 /**
545  * Set whether the entry's file selector has an editable text entry
546  * which will hold its current selection.
547  *
548  * @param obj The entry object
549  * @param value The "is save" flag
550  *
551  * @ingroup File_Selector_Entry
552  */
553 EAPI void
554 elm_fileselector_entry_is_save_set(Evas_Object *obj, Eina_Bool value)
555 {
556    ELM_CHECK_WIDTYPE(obj, widtype);
557    Widget_Data *wd = elm_widget_data_get(obj);
558    if (!wd) return;
559    elm_fileselector_button_is_save_set(wd->button, value);
560 }
561
562 /**
563  * Get the entry's file selector "is save" flag.
564  *
565  * @param obj The entry object
566  * @return value The "is save" flag
567  *
568  * @ingroup File_Selector_Entry
569  */
570 EAPI Eina_Bool
571 elm_fileselector_entry_is_save_get(const Evas_Object *obj)
572 {
573    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
574    Widget_Data *wd = elm_widget_data_get(obj);
575    if (!wd) return EINA_FALSE;
576    return elm_fileselector_button_is_save_get(wd->button);
577 }
578
579 /**
580  * Set whether the entry's file selector will raise an Elementary
581  * Inner Window, instead of a dedicated Elementary Window. By default,
582  * it won't.
583  *
584  * @param obj The entry object
585  * @param value The "inwin mode" flag
586  *
587  * @ingroup File_Selector_Entry
588  */
589 EAPI void
590 elm_fileselector_entry_inwin_mode_set(Evas_Object *obj, Eina_Bool value)
591 {
592    ELM_CHECK_WIDTYPE(obj, widtype);
593    Widget_Data *wd = elm_widget_data_get(obj);
594    if (!wd) return;
595    elm_fileselector_button_inwin_mode_set(wd->button, value);
596 }
597
598 /**
599  * Get the entry's file selector "inwin mode" flag.
600  *
601  * @param obj The entry object
602  * @return value The "inwin mode" flag
603  *
604  * @ingroup File_Selector_Entry
605  */
606 EAPI Eina_Bool
607 elm_fileselector_entry_inwin_mode_get(const Evas_Object *obj)
608 {
609    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
610    Widget_Data *wd = elm_widget_data_get(obj);
611    if (!wd) return EINA_FALSE;
612    return elm_fileselector_button_inwin_mode_get(wd->button);
613 }
614
615 /**
616  * Set the icon used for the entry button
617  *
618  * Once the icon object is set, a previously set one will be deleted.
619  *
620  * @param obj The entry object
621  * @param icon  The image for the entry
622  *
623  * @ingroup File_Selector_Entry
624  */
625 EAPI void
626 elm_fileselector_entry_button_icon_set(Evas_Object *obj, Evas_Object *icon)
627 {
628    ELM_CHECK_WIDTYPE(obj, widtype);
629    Widget_Data *wd = elm_widget_data_get(obj);
630    if (!wd) return;
631    elm_fileselector_button_icon_set(wd->button, icon);
632 }
633
634 /**
635  * Get the icon used for the entry button
636  *
637  * @param obj The entry object
638  * @return The image for the entry
639  *
640  * @ingroup File_Selector_Entry
641  */
642 EAPI Evas_Object *
643 elm_fileselector_entry_button_icon_get(const Evas_Object *obj)
644 {
645    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
646    Widget_Data *wd = elm_widget_data_get(obj);
647    if (!wd) return NULL;
648    return elm_fileselector_button_icon_get(wd->button);
649 }
650
651 /**
652  * Unset the icon used for the entry button
653  *
654  * Unparent and return the icon object which was set for this widget.
655  *
656  * @param obj The entry object
657  * @return The icon object that was being used
658  *
659  * @ingroup File_Selector_Entry
660  */
661 EAPI Evas_Object *
662 elm_fileselector_entry_button_icon_unset(Evas_Object *obj)
663 {
664    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
665    Widget_Data *wd = elm_widget_data_get(obj);
666    if (!wd) return NULL;
667    return elm_fileselector_button_icon_unset(wd->button);
668 }