Elementary src/lib: Removed all trailing whitespaces.
[framework/uifw/elementary.git] / src / lib / elc_fileselector_button.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup File_Selector_Button File Selector Button
6  *
7  * A button that, when clicked, creates an Elementary window (or inner
8  * window) with an Elementary File Selector within. When a file is
9  * chosen, the (inner) window is closed and the selected file is
10  * exposed as an evas_object_smart_callback_call() of the button.
11  *
12  * Signals that you can add callbacks for are:
13  *
14  * "file,chosen" - the user has selected a path, whose string pointer comes
15  *                 as event info
16  *
17  */
18
19 typedef struct _Widget_Data Widget_Data;
20
21 struct _Widget_Data
22 {
23    Evas_Object *self, *btn, *fs, *fsw;
24    const char  *window_title;
25    Evas_Coord   w, h;
26    struct
27    {
28       const char *path;
29       Eina_Bool   expandable : 1;
30       Eina_Bool   folder_only : 1;
31       Eina_Bool   is_save : 1;
32    } fsd;
33    Eina_Bool inwin_mode : 1;
34 };
35
36 #define DEFAULT_WINDOW_TITLE "Select a file"
37
38 static const char *widtype = NULL;
39
40 static void _del_hook(Evas_Object *obj);
41 static void _theme_hook(Evas_Object *obj);
42 static void _disable_hook(Evas_Object *obj);
43 static void _sizing_eval(Evas_Object *obj);
44 static void _changed_size_hints(void        *data,
45                                 Evas        *e,
46                                 Evas_Object *obj,
47                                 void        *event_info);
48 static void _on_focus_hook(void        *data,
49                            Evas_Object *obj);
50 static void _selection_done(void        *data,
51                             Evas_Object *obj,
52                             void        *event_info);
53 static void _activate(Widget_Data *wd);
54
55 static const char SIG_FILE_CHOSEN[] = "file,chosen";
56 static const Evas_Smart_Cb_Description _signals[] = {
57        {SIG_FILE_CHOSEN, "s"},
58        {NULL, NULL}
59 };
60
61 static void
62 _del_hook(Evas_Object *obj)
63 {
64    Evas_Object *win;
65    Widget_Data *wd;
66
67    wd = elm_widget_data_get(obj);
68    if (!wd) return;
69
70    if (wd->window_title) eina_stringshare_del(wd->window_title);
71    if (wd->fsd.path) eina_stringshare_del(wd->fsd.path);
72    if (wd->fs)
73      {
74         win = evas_object_data_del(obj, "win");
75         evas_object_del(win);
76      }
77    free(wd);
78 }
79
80 static void
81 _on_focus_hook(void *data   __UNUSED__,
82                Evas_Object *obj)
83 {
84    Widget_Data *wd = elm_widget_data_get(obj);
85    if (!wd) return;
86    if (elm_widget_focus_get(obj))
87      elm_widget_focus_steal(wd->btn);
88 }
89
90 static void
91 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
92 {
93    Widget_Data *wd = elm_widget_data_get(obj);
94    if (!wd) return;
95    elm_widget_mirrored_set(wd->btn, rtl);
96    elm_widget_mirrored_set(wd->fs, rtl);
97 }
98
99 static void
100 _theme_hook(Evas_Object *obj)
101 {
102    Widget_Data *wd = elm_widget_data_get(obj);
103    char buf[4096];
104    if (!wd) return;
105    _elm_widget_mirrored_reload(obj);
106    _mirrored_set(obj, elm_widget_mirrored_get(obj));
107
108    snprintf(buf, sizeof(buf), "fileselector_button/%s",
109             elm_widget_style_get(obj));
110    elm_object_style_set(wd->btn, buf);
111    _sizing_eval(obj);
112 }
113
114 static void
115 _disable_hook(Evas_Object *obj)
116 {
117    Widget_Data *wd = elm_widget_data_get(obj);
118    if (!wd) return;
119    elm_widget_disabled_set(wd->btn, elm_widget_disabled_get(obj));
120 }
121
122 static void
123 _sizing_eval(Evas_Object *obj)
124 {
125    Widget_Data *wd = elm_widget_data_get(obj);
126    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
127    if (!wd) return;
128    evas_object_size_hint_min_get(wd->btn, &minw, &minh);
129    evas_object_size_hint_max_get(wd->btn, &maxw, &maxh);
130    evas_object_size_hint_min_set(obj, minw, minh);
131    evas_object_size_hint_max_set(obj, maxw, maxh);
132 }
133
134 static void
135 _changed_size_hints(void            *data,
136                     Evas *e          __UNUSED__,
137                     Evas_Object *obj __UNUSED__,
138                     void *event_info __UNUSED__)
139 {
140    Widget_Data *wd = elm_widget_data_get(data);
141    if (!wd) return;
142    _sizing_eval(data);
143 }
144
145 static void
146 _activate_hook(Evas_Object *obj)
147 {
148    Widget_Data *wd;
149    wd = elm_widget_data_get(obj);
150    if (!wd) return;
151    _activate(wd);
152 }
153
154 static void
155 _button_clicked(void            *data,
156                 Evas_Object *obj __UNUSED__,
157                 void *event_info __UNUSED__)
158 {
159    _activate(data);
160 }
161
162 static Evas_Object *
163 _parent_win_get(Evas_Object *obj)
164 {
165    while ((obj) && (strcmp(elm_widget_type_get(obj), "win")))
166      obj = elm_object_parent_widget_get(obj);
167
168    return obj;
169 }
170
171 static Evas_Object *
172 _new_window_add(Widget_Data *wd)
173 {
174    Evas_Object *win, *bg;
175
176    win = elm_win_add(NULL, "fileselector_button", ELM_WIN_DIALOG_BASIC);
177    elm_win_title_set(win, wd->window_title);
178    elm_win_autodel_set(win, EINA_TRUE);
179
180    bg = elm_bg_add(win);
181    elm_win_resize_object_add(win, bg);
182    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
183    evas_object_show(bg);
184
185    evas_object_resize(win, wd->w, wd->h);
186    return win;
187 }
188
189 static void
190 _activate(Widget_Data *wd)
191 {
192    Eina_Bool is_inwin = EINA_FALSE;
193
194    if (wd->fs) return;
195
196    if (wd->inwin_mode)
197      {
198         wd->fsw = _parent_win_get(wd->self);
199
200         if (!wd->fsw)
201           wd->fsw = _new_window_add(wd);
202         else
203           {
204              wd->fsw = elm_win_inwin_add(wd->fsw);
205              is_inwin = EINA_TRUE;
206           }
207      }
208    else
209      wd->fsw = _new_window_add(wd);
210
211    wd->fs = elm_fileselector_add(wd->fsw);
212    elm_widget_mirrored_set(wd->fs, elm_widget_mirrored_get(wd->self));
213    elm_widget_mirrored_automatic_set(wd->fs, EINA_FALSE);
214    elm_fileselector_expandable_set(wd->fs, wd->fsd.expandable);
215    elm_fileselector_folder_only_set(wd->fs, wd->fsd.folder_only);
216    elm_fileselector_is_save_set(wd->fs, wd->fsd.is_save);
217    elm_fileselector_selected_set(wd->fs, wd->fsd.path);
218    evas_object_size_hint_weight_set(wd->fs, EVAS_HINT_EXPAND,
219                                     EVAS_HINT_EXPAND);
220    evas_object_size_hint_align_set(wd->fs, EVAS_HINT_FILL, EVAS_HINT_FILL);
221    evas_object_smart_callback_add(wd->fs, "done", _selection_done, wd);
222    evas_object_show(wd->fs);
223
224    if (is_inwin)
225      {
226         elm_win_inwin_content_set(wd->fsw, wd->fs);
227         elm_win_inwin_activate(wd->fsw);
228      }
229    else
230      {
231         elm_win_resize_object_add(wd->fsw, wd->fs);
232         evas_object_show(wd->fsw);
233      }
234 }
235
236 static void
237 _selection_done(void            *data,
238                 Evas_Object *obj __UNUSED__,
239                 void            *event_info)
240 {
241    const char *file = event_info;
242    Widget_Data *wd = data;
243    Evas_Object *del;
244    if (!wd) return;
245
246    if (file) eina_stringshare_replace(&wd->fsd.path, file);
247
248    del = wd->fsw;
249    wd->fs = NULL;
250    wd->fsw = NULL;
251    evas_object_del(del);
252
253    evas_object_smart_callback_call(wd->self, SIG_FILE_CHOSEN,
254                                    (void *)wd->fsd.path);
255 }
256
257 /**
258  * Add a new file selector button into the parent object.
259  *
260  * @param parent The parent object
261  * @return The new object or NULL if it cannot be created
262  *
263  * @ingroup File_Selector_Button
264  */
265 EAPI Evas_Object *
266 elm_fileselector_button_add(Evas_Object *parent)
267 {
268    Evas_Object *obj;
269    Evas *e;
270    Widget_Data *wd;
271
272    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
273
274    ELM_SET_WIDTYPE(widtype, "fileselector_button");
275    elm_widget_type_set(obj, "fileselector_button");
276    elm_widget_sub_object_add(parent, obj);
277    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
278    elm_widget_data_set(obj, wd);
279    elm_widget_del_hook_set(obj, _del_hook);
280    elm_widget_theme_hook_set(obj, _theme_hook);
281    elm_widget_disable_hook_set(obj, _disable_hook);
282    elm_widget_can_focus_set(obj, EINA_TRUE);
283    elm_widget_activate_hook_set(obj, _activate_hook);
284
285    wd->self = obj;
286    wd->window_title = eina_stringshare_add(DEFAULT_WINDOW_TITLE);
287    if (getenv("HOME")) wd->fsd.path = eina_stringshare_add(getenv("HOME"));
288    else wd->fsd.path = eina_stringshare_add("/");
289    wd->fsd.expandable = _elm_config->fileselector_expand_enable;
290    wd->inwin_mode = _elm_config->inwin_dialogs_enable;
291    wd->w = 400;
292    wd->h = 400;
293
294    wd->btn = elm_button_add(parent);
295    elm_widget_mirrored_automatic_set(wd->btn, EINA_FALSE);
296    elm_widget_resize_object_set(obj, wd->btn);
297    evas_object_event_callback_add(wd->btn, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
298                                   _changed_size_hints, obj);
299    evas_object_smart_callback_add(wd->btn, "clicked", _button_clicked, wd);
300    elm_widget_sub_object_add(obj, wd->btn);
301
302    _theme_hook(obj);
303    evas_object_smart_callbacks_descriptions_set(obj, _signals);
304    return obj;
305 }
306
307 /**
308  * Set the label used in the file selector button.
309  *
310  * @param obj The button object
311  * @param label The text label text to be displayed on the button
312  *
313  * @ingroup File_Selector_Button
314  */
315 EAPI void
316 elm_fileselector_button_label_set(Evas_Object *obj,
317                                   const char  *label)
318 {
319    ELM_CHECK_WIDTYPE(obj, widtype);
320    Widget_Data *wd = elm_widget_data_get(obj);
321    if (!wd) return;
322    elm_button_label_set(wd->btn, label);
323 }
324
325 /**
326  * Get the label used in the file selector button.
327  *
328  * @param obj The button object
329  * @return The button label
330  *
331  * @ingroup File_Selector_Button
332  */
333 EAPI const char *
334 elm_fileselector_button_label_get(const Evas_Object *obj)
335 {
336    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
337    Widget_Data *wd = elm_widget_data_get(obj);
338    if (!wd) return NULL;
339    return elm_button_label_get(wd->btn);
340 }
341
342 /**
343  * Set the title of the file selector button's window.
344  *
345  * @param obj The button object
346  * @param title The title string
347  *
348  * Note that it will only take any effect if the fileselector button
349  * not at "inwin mode".
350  *
351  * @ingroup File_Selector_Button
352  */
353 EAPI void
354 elm_fileselector_button_window_title_set(Evas_Object *obj,
355                                          const char  *title)
356 {
357    ELM_CHECK_WIDTYPE(obj, widtype);
358    Widget_Data *wd = elm_widget_data_get(obj);
359
360    if (!wd) return;
361    eina_stringshare_replace(&wd->window_title, title);
362
363    if (wd->fsw)
364      elm_win_title_set(wd->fsw, wd->window_title);
365 }
366
367 /**
368  * Get the title of the file selector button's window.
369  *
370  * @param obj The button object
371  * @return Title of the file selector button's window
372  *
373  * @ingroup File_Selector_Button
374  */
375 EAPI const char *
376 elm_fileselector_button_window_title_get(const Evas_Object *obj)
377 {
378    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
379    Widget_Data *wd = elm_widget_data_get(obj);
380
381    if (!wd) return NULL;
382    return wd->window_title;
383 }
384
385 /**
386  * Set the size of the file selector button's window.
387  *
388  * @param obj The button object
389  * @param width The width
390  * @param height The height
391  *
392  * Note that it will only take any effect if the fileselector button not at
393  * "inwin mode". Default size for the window (when applicable) is 400x400.
394  *
395  * @ingroup File_Selector_Button
396  */
397 EAPI void
398 elm_fileselector_button_window_size_set(Evas_Object *obj,
399                                         Evas_Coord   width,
400                                         Evas_Coord   height)
401 {
402    ELM_CHECK_WIDTYPE(obj, widtype);
403    Widget_Data *wd = elm_widget_data_get(obj);
404
405    if (!wd) return;
406    wd->w = width;
407    wd->h = height;
408
409    if (wd->fsw)
410      evas_object_resize(wd->fsw, wd->w, wd->h);
411 }
412
413 /**
414  * Get the size of the file selector button's window.
415  *
416  * @param obj The button object
417  * @param width Pointer into which to store the width value
418  * @param height Pointer into which to store the height value
419  *
420  * @ingroup File_Selector_Button
421  */
422 EAPI void
423 elm_fileselector_button_window_size_get(const Evas_Object *obj,
424                                         Evas_Coord        *width,
425                                         Evas_Coord        *height)
426 {
427    ELM_CHECK_WIDTYPE(obj, widtype);
428    Widget_Data *wd = elm_widget_data_get(obj);
429
430    if (!wd) return;
431    if (width) *width = wd->w;
432    if (height) *height = wd->h;
433 }
434
435 /**
436  * Set the starting path of the file selector button's window.
437  *
438  * @param obj The button object
439  * @param path The path string
440  *
441  * It must be a <b>directory</b> path.
442  * Default path is "HOME" environment variable's value.
443  *
444  * @ingroup File_Selector_Button
445  */
446 EAPI void
447 elm_fileselector_button_path_set(Evas_Object *obj,
448                                  const char  *path)
449 {
450    ELM_CHECK_WIDTYPE(obj, widtype);
451    Widget_Data *wd = elm_widget_data_get(obj);
452
453    if (!wd) return;
454    eina_stringshare_replace(&wd->fsd.path, path);
455
456    if (wd->fs)
457      elm_fileselector_selected_set(wd->fs, wd->fsd.path);
458 }
459
460 /**
461  * Get the <b>last</b> path of the file selector button's window.
462  *
463  * @param obj The button object
464  *
465  * @ingroup File_Selector_Button
466  */
467 EAPI const char *
468 elm_fileselector_button_path_get(const Evas_Object *obj)
469 {
470    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
471    Widget_Data *wd = elm_widget_data_get(obj);
472    if (!wd) return NULL;
473    return wd->fsd.path;
474 }
475
476 /**
477  * Set whether the button's file selector is to present itself as an
478  * Elementary Generic List (which will expand its entries for nested
479  * directories) or as canonical list, which will be rendered again
480  * with the contents of each selected directory.
481  *
482  * @param obj The button object
483  * @param value The expandable flag
484  *
485  * @ingroup File_Selector_Button
486  */
487 EAPI void
488 elm_fileselector_button_expandable_set(Evas_Object *obj,
489                                        Eina_Bool    value)
490 {
491    ELM_CHECK_WIDTYPE(obj, widtype);
492    Widget_Data *wd = elm_widget_data_get(obj);
493
494    if (!wd) return;
495    wd->fsd.expandable = value;
496
497    if (wd->fs)
498      elm_fileselector_expandable_set(wd->fs, wd->fsd.expandable);
499 }
500
501 /**
502  * Get the button's file selector expandable flag.
503  *
504  * @param obj The button object
505  * @return value The expandable flag
506  *
507  * @ingroup File_Selector_Button
508  */
509 EAPI Eina_Bool
510 elm_fileselector_button_expandable_get(const Evas_Object *obj)
511 {
512    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
513    Widget_Data *wd = elm_widget_data_get(obj);
514
515    if (!wd) return EINA_FALSE;
516    return wd->fsd.expandable;
517 }
518
519 /**
520  * Set whether the button's file selector list is to display folders
521  * only or the directory contents, as well.
522  *
523  * @param obj The button object
524  * @param value The "folder only" flag
525  *
526  * @ingroup File_Selector_Button
527  */
528 EAPI void
529 elm_fileselector_button_folder_only_set(Evas_Object *obj,
530                                         Eina_Bool    value)
531 {
532    ELM_CHECK_WIDTYPE(obj, widtype);
533    Widget_Data *wd = elm_widget_data_get(obj);
534
535    if (!wd) return;
536    wd->fsd.folder_only = value;
537
538    if (wd->fs)
539      elm_fileselector_folder_only_set(wd->fs, wd->fsd.folder_only);
540 }
541
542 /**
543  * Get the button's file selector "folder only" flag.
544  *
545  * @param obj The button object
546  * @return value The "folder only" flag
547  *
548  * @ingroup File_Selector_Button
549  */
550 EAPI Eina_Bool
551 elm_fileselector_button_folder_only_get(const Evas_Object *obj)
552 {
553    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
554    Widget_Data *wd = elm_widget_data_get(obj);
555
556    if (!wd) return EINA_FALSE;
557    return wd->fsd.folder_only;
558 }
559
560 /**
561  * Set whether the button's file selector has an editable text entry
562  * which will hold its current selection.
563  *
564  * @param obj The button object
565  * @param value The "is save" flag
566  *
567  * @ingroup File_Selector_Button
568  */
569 EAPI void
570 elm_fileselector_button_is_save_set(Evas_Object *obj,
571                                     Eina_Bool    value)
572 {
573    ELM_CHECK_WIDTYPE(obj, widtype);
574    Widget_Data *wd = elm_widget_data_get(obj);
575
576    if (!wd) return;
577    wd->fsd.is_save = value;
578
579    if (wd->fs)
580      elm_fileselector_is_save_set(wd->fs, wd->fsd.is_save);
581 }
582
583 /**
584  * Get the button's file selector "is save" flag.
585  *
586  * @param obj The button object
587  * @return value The "is save" flag
588  *
589  * @ingroup File_Selector_Button
590  */
591 EAPI Eina_Bool
592 elm_fileselector_button_is_save_get(const Evas_Object *obj)
593 {
594    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
595    Widget_Data *wd = elm_widget_data_get(obj);
596
597    if (!wd) return EINA_FALSE;
598    return wd->fsd.is_save;
599 }
600
601 /**
602  * Set whether the button's file selector will raise an Elementary
603  * Inner Window, instead of a dedicated Elementary Window. By default,
604  * it won't.
605  *
606  * @param obj The button object
607  * @param value The "inwin mode" flag
608  *
609  * @ingroup File_Selector_Button
610  */
611 EAPI void
612 elm_fileselector_button_inwin_mode_set(Evas_Object *obj,
613                                        Eina_Bool    value)
614 {
615    ELM_CHECK_WIDTYPE(obj, widtype);
616    Widget_Data *wd = elm_widget_data_get(obj);
617
618    if (!wd) return;
619    wd->inwin_mode = value;
620 }
621
622 /**
623  * Get the button's file selector "inwin mode" flag.
624  *
625  * @param obj The button object
626  * @return value The "inwin mode" flag
627  *
628  * @ingroup File_Selector_Button
629  */
630 EAPI Eina_Bool
631 elm_fileselector_button_inwin_mode_get(const Evas_Object *obj)
632 {
633    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
634    Widget_Data *wd = elm_widget_data_get(obj);
635
636    if (!wd) return EINA_FALSE;
637    return wd->inwin_mode;
638 }
639
640 /**
641  * Set the icon used for the button
642  *
643  * Once the icon object is set, a previously set one will be deleted.
644  * If you want to keep that old content object, use the
645  * elm_fileselector_button_icon_unset() function.
646  *
647  * @param obj The button object
648  * @param icon  The icon object for the button
649  *
650  * @ingroup File_Selector_Button
651  */
652 EAPI void
653 elm_fileselector_button_icon_set(Evas_Object *obj,
654                                  Evas_Object *icon)
655 {
656    ELM_CHECK_WIDTYPE(obj, widtype);
657    Widget_Data *wd = elm_widget_data_get(obj);
658    if (!wd)
659      {
660         evas_object_del(icon);
661         return;
662      }
663    elm_button_icon_set(wd->btn, icon);
664 }
665
666 /**
667  * Get the icon used for the button
668  *
669  * @param obj The button object
670  * @return The icon object that is being used
671  *
672  * @ingroup File_Selector_Button
673  */
674 EAPI Evas_Object *
675 elm_fileselector_button_icon_get(const Evas_Object *obj)
676 {
677    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
678    Widget_Data *wd = elm_widget_data_get(obj);
679    if (!wd) return NULL;
680    return elm_button_icon_get(wd->btn);
681 }
682
683 /**
684  * Unset the icon used for the button
685  *
686  * Unparent and return the icon object which was set for this widget.
687  *
688  * @param obj The button object
689  * @return The icon object that was being used
690  *
691  * @ingroup File_Selector_Button
692  */
693 EAPI Evas_Object *
694 elm_fileselector_button_icon_unset(Evas_Object *obj)
695 {
696    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
697    Widget_Data *wd = elm_widget_data_get(obj);
698    if (!wd) return NULL;
699    return elm_button_icon_unset(wd->btn);
700 }
701