big move forward. scrolled entry and entry merge into entry. entry now
[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_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_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    _elm_widget_mirrored_reload(obj);
174    _mirrored_set(obj, elm_widget_mirrored_get(obj));
175
176    _elm_theme_object_set(obj, wd->edje, "fileselector_entry", "base", style);
177    if (elm_object_disabled_get(obj))
178       edje_object_signal_emit(wd->edje, "elm,state,disabled", "elm");
179
180    if (!style) style = "default";
181    snprintf(buf, sizeof(buf), "fileselector_entry/%s", style);
182    elm_widget_style_set(wd->button, buf);
183    elm_widget_style_set(wd->entry, buf);
184
185    edje_object_part_swallow(obj, "elm.swallow.button", wd->button);
186    edje_object_part_swallow(obj, "elm.swallow.entry", wd->entry);
187
188    edje_object_message_signal_process(wd->edje);
189    edje_object_scale_set
190      (wd->edje, elm_widget_scale_get(obj) * _elm_config->scale);
191    _sizing_eval(obj);
192 }
193
194 static void
195 _disable_hook(Evas_Object *obj)
196 {
197    Widget_Data *wd = elm_widget_data_get(obj);
198    Eina_Bool val = elm_widget_disabled_get(obj);
199    if (!wd) return;
200    if (val)
201      edje_object_signal_emit(wd->edje, "elm,state,disabled", "elm");
202    else
203      edje_object_signal_emit(wd->edje, "elm,state,enabled", "elm");
204
205    elm_widget_disabled_set(wd->button, val);
206    elm_widget_disabled_set(wd->entry, val);
207 }
208
209 static void
210 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
211 {
212    _sizing_eval(data);
213 }
214
215 /**
216  * Add a new file selector entry into the parent object.
217  *
218  * @param parent The parent object
219  * @return The new object or NULL if it cannot be created
220  *
221  * @ingroup File_Selector_Entry
222  */
223 EAPI Evas_Object *
224 elm_fileselector_entry_add(Evas_Object *parent)
225 {
226    Evas_Object *obj;
227    Evas *e;
228    Widget_Data *wd;
229
230    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
231
232    ELM_SET_WIDTYPE(widtype, "fileselector_entry");
233    elm_widget_type_set(obj, "fileselector_entry");
234    elm_widget_sub_object_add(parent, obj);
235    elm_widget_data_set(obj, wd);
236    elm_widget_del_hook_set(obj, _del_hook);
237    elm_widget_disable_hook_set(obj, _disable_hook);
238    elm_widget_focus_next_hook_set(obj, _elm_fileselector_entry_focus_next_hook);
239    elm_widget_can_focus_set(obj, EINA_FALSE);
240    elm_widget_theme_hook_set(obj, _theme_hook);
241
242    wd->edje = edje_object_add(e);
243    _elm_theme_object_set(obj, wd->edje, "fileselector_entry", "base", "default");
244    elm_widget_resize_object_set(obj, wd->edje);
245
246    wd->button = elm_fileselector_button_add(obj);
247    elm_widget_mirrored_automatic_set(wd->button, EINA_FALSE);
248    ELM_SET_WIDTYPE(widtype, "fileselector_entry");
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_entry_add(obj);
265    elm_entry_scrollable_set(wd->entry, EINA_TRUE);
266    elm_widget_mirrored_automatic_set(wd->entry, EINA_FALSE);
267    elm_widget_style_set(wd->entry, "fileselector_entry/default");
268    elm_entry_single_line_set(wd->entry, EINA_TRUE);
269    elm_entry_editable_set(wd->entry, EINA_TRUE);
270    edje_object_part_swallow(wd->edje, "elm.swallow.entry", wd->entry);
271    elm_widget_sub_object_add(obj, wd->entry);
272    evas_object_event_callback_add
273       (wd->entry, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
274
275 #define SIG_FWD(name)                                                   \
276    evas_object_smart_callback_add(wd->entry, SIG_##name, _##name##_fwd, obj)
277    SIG_FWD(CHANGED);
278    SIG_FWD(ACTIVATED);
279    SIG_FWD(PRESS);
280    SIG_FWD(LONGPRESSED);
281    SIG_FWD(CLICKED);
282    SIG_FWD(CLICKED_DOUBLE);
283    SIG_FWD(FOCUSED);
284    SIG_FWD(UNFOCUSED);
285    SIG_FWD(SELECTION_PASTE);
286    SIG_FWD(SELECTION_COPY);
287    SIG_FWD(SELECTION_CUT);
288 #undef SIG_FWD
289
290    _mirrored_set(obj, elm_widget_mirrored_get(obj));
291    _sizing_eval(obj);
292
293    // TODO: convert Elementary to subclassing of Evas_Smart_Class
294    // TODO: and save some bytes, making descriptions per-class and not instance!
295    evas_object_smart_callbacks_descriptions_set(obj, _signals);
296    return obj;
297 }
298
299 /**
300  * Set the label used in the file selector entry.
301  *
302  * @param obj The entry object
303  * @param label The text label text to be displayed on the entry
304  *
305  * @ingroup File_Selector_Entry
306  */
307 EAPI void
308 elm_fileselector_entry_button_label_set(Evas_Object *obj, const char *label)
309 {
310    ELM_CHECK_WIDTYPE(obj, widtype);
311    Widget_Data *wd = elm_widget_data_get(obj);
312    if (!wd) return;
313    elm_fileselector_button_label_set(wd->button, label);
314 }
315
316 EAPI const char *
317 elm_fileselector_entry_button_label_get(const Evas_Object *obj)
318 {
319    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
320    Widget_Data *wd = elm_widget_data_get(obj);
321    if (!wd) return NULL;
322    return elm_fileselector_button_label_get(wd->button);
323 }
324
325 /**
326  * Set the path to start the entry's file selector with, when clicked.
327  *
328  * @param obj The entry object
329  * @param path Path to a file/directory
330  *
331  * Default path is "HOME" environment variable's value.
332  *
333  * @ingroup File_Selector_Entry
334  */
335 EAPI void
336 elm_fileselector_entry_selected_set(Evas_Object *obj, const char *path)
337 {
338    ELM_CHECK_WIDTYPE(obj, widtype);
339    Widget_Data *wd = elm_widget_data_get(obj);
340    if (!wd) return;
341    elm_fileselector_button_path_set(wd->button, path);
342 }
343
344 /**
345  * Get the <b>last</b> path which the entry's file selector was set to.
346  *
347  * @param obj The entry object
348  * @param path Path to a file/directory
349  *
350  * Default path is "HOME" environment variable's value.
351  *
352  * @ingroup File_Selector_Entry
353  */
354 EAPI const char *
355 elm_fileselector_entry_selected_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_path_get(wd->button);
361 }
362
363 /**
364  * Set the title of the file selector entry's window.
365  *
366  * @param obj The entry object
367  * @param title The title string
368  *
369  * Note that it will only take any effect if the fileselector entry
370  * not at "inwin mode".
371  *
372  * @ingroup File_Selector_Entry
373  */
374 EAPI void
375 elm_fileselector_entry_window_title_set(Evas_Object *obj, const char *title)
376 {
377    ELM_CHECK_WIDTYPE(obj, widtype);
378    Widget_Data *wd = elm_widget_data_get(obj);
379    if (!wd) return;
380    elm_fileselector_button_window_title_set(wd->button, title);
381 }
382
383 /**
384  * Get the title of the file selector entry's window.
385  *
386  * @param obj The entry object
387  *
388  * @ingroup File_Selector_Entry
389  */
390 EAPI const char *
391 elm_fileselector_entry_window_title_get(const Evas_Object *obj)
392 {
393    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
394    Widget_Data *wd = elm_widget_data_get(obj);
395    if (!wd) return NULL;
396    return elm_fileselector_button_window_title_get(wd->button);
397 }
398
399 /**
400  * Set the size of the file selector entry's window.
401  *
402  * @param obj The entry object
403  * @param width The width
404  * @param height The height
405  *
406  * Note that it will only take any effect if the fileselector entry not at
407  * "inwin mode". Default size for the window (when applicable) is 400x400.
408  *
409  * @ingroup File_Selector_Entry
410  */
411 EAPI void
412 elm_fileselector_entry_window_size_set(Evas_Object *obj, Evas_Coord width, Evas_Coord height)
413 {
414    ELM_CHECK_WIDTYPE(obj, widtype);
415    Widget_Data *wd = elm_widget_data_get(obj);
416    if (!wd) return;
417    elm_fileselector_button_window_size_set(wd->button, width, height);
418 }
419
420 /**
421  * Get the size of the file selector entry's window.
422  *
423  * @param obj The entry object
424  * @param width Pointer into which to store the width value
425  * @param height Pointer into which to store the height value
426  *
427  * @ingroup File_Selector_Entry
428  */
429 EAPI void
430 elm_fileselector_entry_window_size_get(const Evas_Object *obj, Evas_Coord *width, Evas_Coord *height)
431 {
432    ELM_CHECK_WIDTYPE(obj, widtype);
433    Widget_Data *wd = elm_widget_data_get(obj);
434    if (!wd) return;
435    elm_fileselector_button_window_size_get(wd->button, width, height);
436 }
437
438 /**
439  * Set the starting path of the file selector entry's window.
440  *
441  * @param obj The entry object
442  * @param path The path string
443  *
444  * It must be a <b>directory</b> path.
445  *
446  * @ingroup File_Selector_Entry
447  */
448 EAPI void
449 elm_fileselector_entry_path_set(Evas_Object *obj, const char *path)
450 {
451    ELM_CHECK_WIDTYPE(obj, widtype);
452    Widget_Data *wd = elm_widget_data_get(obj);
453    if (!wd) return;
454    elm_fileselector_button_path_set(wd->button, path);
455    elm_entry_entry_set(wd->entry, path);
456 }
457
458 /**
459  * Get the <b>last</b> path of the file selector entry's window.
460  *
461  * @param obj The entry object
462  *
463  * @ingroup File_Selector_Entry
464  */
465 EAPI const char *
466 elm_fileselector_entry_path_get(const Evas_Object *obj)
467 {
468    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
469    Widget_Data *wd = elm_widget_data_get(obj);
470    if (!wd) return NULL;
471    return elm_entry_entry_get(wd->entry);
472 }
473
474 /**
475  * Set whether the entry's file selector is to present itself as an
476  * Elementary Generic List (which will expand its entries for nested
477  * directories) or as canonical list, which will be rendered again
478  * with the contents of each selected directory.
479  *
480  * @param obj The entry object
481  * @param value The expandable flag
482  *
483  * @ingroup File_Selector_Entry
484  */
485 EAPI void
486 elm_fileselector_entry_expandable_set(Evas_Object *obj, Eina_Bool value)
487 {
488    ELM_CHECK_WIDTYPE(obj, widtype);
489    Widget_Data *wd = elm_widget_data_get(obj);
490    if (!wd) return;
491    elm_fileselector_button_expandable_set(wd->button, value);
492 }
493
494 /**
495  * Get the entry's file selector expandable flag.
496  *
497  * @param obj The entry object
498  * @return value The expandable flag
499  *
500  * @ingroup File_Selector_Entry
501  */
502 EAPI Eina_Bool
503 elm_fileselector_entry_expandable_get(const Evas_Object *obj)
504 {
505    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
506    Widget_Data *wd = elm_widget_data_get(obj);
507    if (!wd) return EINA_FALSE;
508    return elm_fileselector_button_expandable_get(wd->button);
509 }
510
511 /**
512  * Set whether the entry's file selector list is to display folders
513  * only or the directory contents, as well.
514  *
515  * @param obj The entry object
516  * @param value The "folder only" flag
517  *
518  * @ingroup File_Selector_Entry
519  */
520 EAPI void
521 elm_fileselector_entry_folder_only_set(Evas_Object *obj, Eina_Bool value)
522 {
523    ELM_CHECK_WIDTYPE(obj, widtype);
524    Widget_Data *wd = elm_widget_data_get(obj);
525    if (!wd) return;
526    elm_fileselector_button_folder_only_set(wd->button, value);
527 }
528
529 /**
530  * Get the entry's file selector "folder only" flag.
531  *
532  * @param obj The entry object
533  * @return value The "folder only" flag
534  *
535  * @ingroup File_Selector_Entry
536  */
537 EAPI Eina_Bool
538 elm_fileselector_entry_folder_only_get(const Evas_Object *obj)
539 {
540    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
541    Widget_Data *wd = elm_widget_data_get(obj);
542    if (!wd) return EINA_FALSE;
543    return elm_fileselector_button_folder_only_get(wd->button);
544 }
545
546 /**
547  * Set whether the entry's file selector has an editable text entry
548  * which will hold its current selection.
549  *
550  * @param obj The entry object
551  * @param value The "is save" flag
552  *
553  * @ingroup File_Selector_Entry
554  */
555 EAPI void
556 elm_fileselector_entry_is_save_set(Evas_Object *obj, Eina_Bool value)
557 {
558    ELM_CHECK_WIDTYPE(obj, widtype);
559    Widget_Data *wd = elm_widget_data_get(obj);
560    if (!wd) return;
561    elm_fileselector_button_is_save_set(wd->button, value);
562 }
563
564 /**
565  * Get the entry's file selector "is save" flag.
566  *
567  * @param obj The entry object
568  * @return value The "is save" flag
569  *
570  * @ingroup File_Selector_Entry
571  */
572 EAPI Eina_Bool
573 elm_fileselector_entry_is_save_get(const Evas_Object *obj)
574 {
575    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
576    Widget_Data *wd = elm_widget_data_get(obj);
577    if (!wd) return EINA_FALSE;
578    return elm_fileselector_button_is_save_get(wd->button);
579 }
580
581 /**
582  * Set whether the entry's file selector will raise an Elementary
583  * Inner Window, instead of a dedicated Elementary Window. By default,
584  * it won't.
585  *
586  * @param obj The entry object
587  * @param value The "inwin mode" flag
588  *
589  * @ingroup File_Selector_Entry
590  */
591 EAPI void
592 elm_fileselector_entry_inwin_mode_set(Evas_Object *obj, Eina_Bool value)
593 {
594    ELM_CHECK_WIDTYPE(obj, widtype);
595    Widget_Data *wd = elm_widget_data_get(obj);
596    if (!wd) return;
597    elm_fileselector_button_inwin_mode_set(wd->button, value);
598 }
599
600 /**
601  * Get the entry's file selector "inwin mode" flag.
602  *
603  * @param obj The entry object
604  * @return value The "inwin mode" flag
605  *
606  * @ingroup File_Selector_Entry
607  */
608 EAPI Eina_Bool
609 elm_fileselector_entry_inwin_mode_get(const Evas_Object *obj)
610 {
611    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
612    Widget_Data *wd = elm_widget_data_get(obj);
613    if (!wd) return EINA_FALSE;
614    return elm_fileselector_button_inwin_mode_get(wd->button);
615 }
616
617 /**
618  * Set the icon used for the entry button
619  *
620  * Once the icon object is set, a previously set one will be deleted.
621  *
622  * @param obj The entry object
623  * @param icon  The image for the entry
624  *
625  * @ingroup File_Selector_Entry
626  */
627 EAPI void
628 elm_fileselector_entry_button_icon_set(Evas_Object *obj, Evas_Object *icon)
629 {
630    ELM_CHECK_WIDTYPE(obj, widtype);
631    Widget_Data *wd = elm_widget_data_get(obj);
632    if (!wd) return;
633    elm_fileselector_button_icon_set(wd->button, icon);
634 }
635
636 /**
637  * Get the icon used for the entry button
638  *
639  * @param obj The entry object
640  * @return The image for the entry
641  *
642  * @ingroup File_Selector_Entry
643  */
644 EAPI Evas_Object *
645 elm_fileselector_entry_button_icon_get(const Evas_Object *obj)
646 {
647    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
648    Widget_Data *wd = elm_widget_data_get(obj);
649    if (!wd) return NULL;
650    return elm_fileselector_button_icon_get(wd->button);
651 }
652
653 /**
654  * Unset the icon used for the entry button
655  *
656  * Unparent and return the icon object which was set for this widget.
657  *
658  * @param obj The entry object
659  * @return The icon object that was being used
660  *
661  * @ingroup File_Selector_Entry
662  */
663 EAPI Evas_Object *
664 elm_fileselector_entry_button_icon_unset(Evas_Object *obj)
665 {
666    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
667    Widget_Data *wd = elm_widget_data_get(obj);
668    if (!wd) return NULL;
669    return elm_fileselector_button_icon_unset(wd->button);
670 }