1 #include <Elementary.h>
5 * @defgroup File_Selector_Button File Selector Button
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.
12 * Signals that you can add callbacks for are:
14 * "file,chosen" - the user has selected a path, whose string pointer comes
19 typedef struct _Widget_Data Widget_Data;
23 Evas_Object *self, *btn, *fs, *fsw;
24 const char *window_title;
29 Eina_Bool expandable : 1;
30 Eina_Bool folder_only : 1;
31 Eina_Bool is_save : 1;
33 Eina_Bool inwin_mode : 1;
36 #define DEFAULT_WINDOW_TITLE "Select a file"
38 static const char *widtype = NULL;
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,
48 static void _on_focus_hook(void *data,
50 static void _selection_done(void *data,
53 static void _activate(Widget_Data *wd);
55 static const char SIG_FILE_CHOSEN[] = "file,chosen";
56 static const Evas_Smart_Cb_Description _signals[] = {
57 {SIG_FILE_CHOSEN, "s"},
62 _del_hook(Evas_Object *obj)
67 wd = elm_widget_data_get(obj);
70 if (wd->window_title) eina_stringshare_del(wd->window_title);
71 if (wd->fsd.path) eina_stringshare_del(wd->fsd.path);
74 win = evas_object_data_del(obj, "win");
81 _on_focus_hook(void *data __UNUSED__,
84 Widget_Data *wd = elm_widget_data_get(obj);
86 if (elm_widget_focus_get(obj))
87 elm_widget_focus_steal(wd->btn);
91 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
93 Widget_Data *wd = elm_widget_data_get(obj);
95 elm_widget_mirrored_set(wd->btn, rtl);
96 elm_widget_mirrored_set(wd->fs, rtl);
100 _theme_hook(Evas_Object *obj)
102 Widget_Data *wd = elm_widget_data_get(obj);
105 _elm_widget_mirrored_reload(obj);
106 _mirrored_set(obj, elm_widget_mirrored_get(obj));
108 snprintf(buf, sizeof(buf), "fileselector_button/%s",
109 elm_widget_style_get(obj));
110 elm_object_style_set(wd->btn, buf);
115 _disable_hook(Evas_Object *obj)
117 Widget_Data *wd = elm_widget_data_get(obj);
119 elm_widget_disabled_set(wd->btn, elm_widget_disabled_get(obj));
123 _sizing_eval(Evas_Object *obj)
125 Widget_Data *wd = elm_widget_data_get(obj);
126 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
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);
135 _changed_size_hints(void *data,
137 Evas_Object *obj __UNUSED__,
138 void *event_info __UNUSED__)
140 Widget_Data *wd = elm_widget_data_get(data);
146 _activate_hook(Evas_Object *obj)
149 wd = elm_widget_data_get(obj);
155 _button_clicked(void *data,
156 Evas_Object *obj __UNUSED__,
157 void *event_info __UNUSED__)
163 _parent_win_get(Evas_Object *obj)
165 while ((obj) && (strcmp(elm_widget_type_get(obj), "win")))
166 obj = elm_object_parent_widget_get(obj);
172 _new_window_add(Widget_Data *wd)
174 Evas_Object *win, *bg;
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);
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);
185 evas_object_resize(win, wd->w, wd->h);
190 _activate(Widget_Data *wd)
192 Eina_Bool is_inwin = EINA_FALSE;
198 wd->fsw = _parent_win_get(wd->self);
201 wd->fsw = _new_window_add(wd);
204 wd->fsw = elm_win_inwin_add(wd->fsw);
205 is_inwin = EINA_TRUE;
209 wd->fsw = _new_window_add(wd);
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,
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);
226 elm_win_inwin_content_set(wd->fsw, wd->fs);
227 elm_win_inwin_activate(wd->fsw);
231 elm_win_resize_object_add(wd->fsw, wd->fs);
232 evas_object_show(wd->fsw);
237 _selection_done(void *data,
238 Evas_Object *obj __UNUSED__,
241 const char *file = event_info;
242 Widget_Data *wd = data;
246 if (file) eina_stringshare_replace(&wd->fsd.path, file);
251 evas_object_del(del);
253 evas_object_smart_callback_call(wd->self, SIG_FILE_CHOSEN,
254 (void *)wd->fsd.path);
258 * Add a new file selector button into the parent object.
260 * @param parent The parent object
261 * @return The new object or NULL if it cannot be created
263 * @ingroup File_Selector_Button
266 elm_fileselector_button_add(Evas_Object *parent)
272 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
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);
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;
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);
303 evas_object_smart_callbacks_descriptions_set(obj, _signals);
308 * Set the label used in the file selector button.
310 * @param obj The button object
311 * @param label The text label text to be displayed on the button
313 * @ingroup File_Selector_Button
316 elm_fileselector_button_label_set(Evas_Object *obj,
319 ELM_CHECK_WIDTYPE(obj, widtype);
320 Widget_Data *wd = elm_widget_data_get(obj);
322 elm_button_label_set(wd->btn, label);
326 * Get the label used in the file selector button.
328 * @param obj The button object
329 * @return The button label
331 * @ingroup File_Selector_Button
334 elm_fileselector_button_label_get(const Evas_Object *obj)
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);
343 * Set the title of the file selector button's window.
345 * @param obj The button object
346 * @param title The title string
348 * Note that it will only take any effect if the fileselector button
349 * not at "inwin mode".
351 * @ingroup File_Selector_Button
354 elm_fileselector_button_window_title_set(Evas_Object *obj,
357 ELM_CHECK_WIDTYPE(obj, widtype);
358 Widget_Data *wd = elm_widget_data_get(obj);
361 eina_stringshare_replace(&wd->window_title, title);
364 elm_win_title_set(wd->fsw, wd->window_title);
368 * Get the title of the file selector button's window.
370 * @param obj The button object
371 * @return Title of the file selector button's window
373 * @ingroup File_Selector_Button
376 elm_fileselector_button_window_title_get(const Evas_Object *obj)
378 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
379 Widget_Data *wd = elm_widget_data_get(obj);
381 if (!wd) return NULL;
382 return wd->window_title;
386 * Set the size of the file selector button's window.
388 * @param obj The button object
389 * @param width The width
390 * @param height The height
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.
395 * @ingroup File_Selector_Button
398 elm_fileselector_button_window_size_set(Evas_Object *obj,
402 ELM_CHECK_WIDTYPE(obj, widtype);
403 Widget_Data *wd = elm_widget_data_get(obj);
410 evas_object_resize(wd->fsw, wd->w, wd->h);
414 * Get the size of the file selector button's window.
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
420 * @ingroup File_Selector_Button
423 elm_fileselector_button_window_size_get(const Evas_Object *obj,
427 ELM_CHECK_WIDTYPE(obj, widtype);
428 Widget_Data *wd = elm_widget_data_get(obj);
431 if (width) *width = wd->w;
432 if (height) *height = wd->h;
436 * Set the starting path of the file selector button's window.
438 * @param obj The button object
439 * @param path The path string
441 * It must be a <b>directory</b> path.
442 * Default path is "HOME" environment variable's value.
444 * @ingroup File_Selector_Button
447 elm_fileselector_button_path_set(Evas_Object *obj,
450 ELM_CHECK_WIDTYPE(obj, widtype);
451 Widget_Data *wd = elm_widget_data_get(obj);
454 eina_stringshare_replace(&wd->fsd.path, path);
457 elm_fileselector_selected_set(wd->fs, wd->fsd.path);
461 * Get the <b>last</b> path of the file selector button's window.
463 * @param obj The button object
465 * @ingroup File_Selector_Button
468 elm_fileselector_button_path_get(const Evas_Object *obj)
470 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
471 Widget_Data *wd = elm_widget_data_get(obj);
472 if (!wd) return NULL;
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.
482 * @param obj The button object
483 * @param value The expandable flag
485 * @ingroup File_Selector_Button
488 elm_fileselector_button_expandable_set(Evas_Object *obj,
491 ELM_CHECK_WIDTYPE(obj, widtype);
492 Widget_Data *wd = elm_widget_data_get(obj);
495 wd->fsd.expandable = value;
498 elm_fileselector_expandable_set(wd->fs, wd->fsd.expandable);
502 * Get the button's file selector expandable flag.
504 * @param obj The button object
505 * @return value The expandable flag
507 * @ingroup File_Selector_Button
510 elm_fileselector_button_expandable_get(const Evas_Object *obj)
512 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
513 Widget_Data *wd = elm_widget_data_get(obj);
515 if (!wd) return EINA_FALSE;
516 return wd->fsd.expandable;
520 * Set whether the button's file selector list is to display folders
521 * only or the directory contents, as well.
523 * @param obj The button object
524 * @param value The "folder only" flag
526 * @ingroup File_Selector_Button
529 elm_fileselector_button_folder_only_set(Evas_Object *obj,
532 ELM_CHECK_WIDTYPE(obj, widtype);
533 Widget_Data *wd = elm_widget_data_get(obj);
536 wd->fsd.folder_only = value;
539 elm_fileselector_folder_only_set(wd->fs, wd->fsd.folder_only);
543 * Get the button's file selector "folder only" flag.
545 * @param obj The button object
546 * @return value The "folder only" flag
548 * @ingroup File_Selector_Button
551 elm_fileselector_button_folder_only_get(const Evas_Object *obj)
553 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
554 Widget_Data *wd = elm_widget_data_get(obj);
556 if (!wd) return EINA_FALSE;
557 return wd->fsd.folder_only;
561 * Set whether the button's file selector has an editable text entry
562 * which will hold its current selection.
564 * @param obj The button object
565 * @param value The "is save" flag
567 * @ingroup File_Selector_Button
570 elm_fileselector_button_is_save_set(Evas_Object *obj,
573 ELM_CHECK_WIDTYPE(obj, widtype);
574 Widget_Data *wd = elm_widget_data_get(obj);
577 wd->fsd.is_save = value;
580 elm_fileselector_is_save_set(wd->fs, wd->fsd.is_save);
584 * Get the button's file selector "is save" flag.
586 * @param obj The button object
587 * @return value The "is save" flag
589 * @ingroup File_Selector_Button
592 elm_fileselector_button_is_save_get(const Evas_Object *obj)
594 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
595 Widget_Data *wd = elm_widget_data_get(obj);
597 if (!wd) return EINA_FALSE;
598 return wd->fsd.is_save;
602 * Set whether the button's file selector will raise an Elementary
603 * Inner Window, instead of a dedicated Elementary Window. By default,
606 * @param obj The button object
607 * @param value The "inwin mode" flag
609 * @ingroup File_Selector_Button
612 elm_fileselector_button_inwin_mode_set(Evas_Object *obj,
615 ELM_CHECK_WIDTYPE(obj, widtype);
616 Widget_Data *wd = elm_widget_data_get(obj);
619 wd->inwin_mode = value;
623 * Get the button's file selector "inwin mode" flag.
625 * @param obj The button object
626 * @return value The "inwin mode" flag
628 * @ingroup File_Selector_Button
631 elm_fileselector_button_inwin_mode_get(const Evas_Object *obj)
633 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
634 Widget_Data *wd = elm_widget_data_get(obj);
636 if (!wd) return EINA_FALSE;
637 return wd->inwin_mode;
641 * Set the icon used for the button
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.
647 * @param obj The button object
648 * @param icon The icon object for the button
650 * @ingroup File_Selector_Button
653 elm_fileselector_button_icon_set(Evas_Object *obj,
656 ELM_CHECK_WIDTYPE(obj, widtype);
657 Widget_Data *wd = elm_widget_data_get(obj);
660 evas_object_del(icon);
663 elm_button_icon_set(wd->btn, icon);
667 * Get the icon used for the button
669 * @param obj The button object
670 * @return The icon object that is being used
672 * @ingroup File_Selector_Button
675 elm_fileselector_button_icon_get(const Evas_Object *obj)
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);
684 * Unset the icon used for the button
686 * Unparent and return the icon object which was set for this widget.
688 * @param obj The button object
689 * @return The icon object that was being used
691 * @ingroup File_Selector_Button
694 elm_fileselector_button_icon_unset(Evas_Object *obj)
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);