1 #include <Elementary.h>
5 * @defgroup File_Selector_Button File Selector Button
8 * A button that, when clicked, creates an Elementary window (or inner
9 * window) with an Elementary File Selector within. When a file is
10 * chosen, the (inner) window is closed and the selected file is
11 * exposed as an evas_object_smart_callback_call() of the button.
14 typedef struct _Widget_Data Widget_Data;
18 Evas_Object *self, *btn, *fs, *fsw;
19 const char *window_title;
24 Eina_Bool expandable : 1;
25 Eina_Bool folder_only : 1;
26 Eina_Bool is_save : 1;
28 Eina_Bool inwin_mode : 1;
31 #define DEFAULT_WINDOW_TITLE "Select a file"
33 static const char *widtype = NULL;
35 static void _del_hook(Evas_Object *obj);
36 static void _theme_hook(Evas_Object *obj);
37 static void _disable_hook(Evas_Object *obj);
38 static void _sizing_eval(Evas_Object *obj);
39 static void _changed_size_hints(void *data,
43 static void _on_focus_hook(void *data,
45 static void _selection_done(void *data,
48 static void _activate(Widget_Data *wd);
50 static const char SIG_FILE_CHOSEN[] = "file,chosen";
51 static const Evas_Smart_Cb_Description _signals[] = {
52 {SIG_FILE_CHOSEN, "s"},
57 _del_hook(Evas_Object *obj)
62 wd = elm_widget_data_get(obj);
65 if (wd->window_title) eina_stringshare_del(wd->window_title);
66 if (wd->fsd.path) eina_stringshare_del(wd->fsd.path);
69 win = evas_object_data_del(obj, "win");
76 _on_focus_hook(void *data __UNUSED__,
79 Widget_Data *wd = elm_widget_data_get(obj);
81 if (elm_widget_focus_get(obj))
82 elm_widget_focus_steal(wd->btn);
86 _theme_hook(Evas_Object *obj)
88 Widget_Data *wd = elm_widget_data_get(obj);
91 snprintf(buf, sizeof(buf), "fileselector_button/%s",
92 elm_widget_style_get(obj));
93 elm_object_style_set(wd->btn, buf);
98 _disable_hook(Evas_Object *obj)
100 Widget_Data *wd = elm_widget_data_get(obj);
102 elm_widget_disabled_set(wd->btn, elm_widget_disabled_get(obj));
106 _sizing_eval(Evas_Object *obj)
108 Widget_Data *wd = elm_widget_data_get(obj);
109 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
111 evas_object_size_hint_min_get(wd->btn, &minw, &minh);
112 evas_object_size_hint_max_get(wd->btn, &maxw, &maxh);
113 evas_object_size_hint_min_set(obj, minw, minh);
114 evas_object_size_hint_max_set(obj, maxw, maxh);
118 _changed_size_hints(void *data,
120 Evas_Object *obj __UNUSED__,
121 void *event_info __UNUSED__)
123 Widget_Data *wd = elm_widget_data_get(data);
129 _activate_hook(Evas_Object *obj)
132 wd = elm_widget_data_get(obj);
138 _button_clicked(void *data,
139 Evas_Object *obj __UNUSED__,
140 void *event_info __UNUSED__)
146 _parent_win_get(Evas_Object *obj)
148 while ((obj) && (strcmp(elm_widget_type_get(obj), "win")))
149 obj = elm_object_parent_widget_get(obj);
155 _new_window_add(Widget_Data *wd)
157 Evas_Object *win, *bg;
159 win = elm_win_add(NULL, "fileselector_button", ELM_WIN_DIALOG_BASIC);
160 elm_win_title_set(win, wd->window_title);
161 elm_win_autodel_set(win, EINA_TRUE);
163 bg = elm_bg_add(win);
164 elm_win_resize_object_add(win, bg);
165 evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
166 evas_object_show(bg);
168 evas_object_resize(win, wd->w, wd->h);
173 _activate(Widget_Data *wd)
175 Eina_Bool is_inwin = EINA_FALSE;
181 wd->fsw = _parent_win_get(wd->self);
184 wd->fsw = _new_window_add(wd);
187 wd->fsw = elm_win_inwin_add(wd->fsw);
188 is_inwin = EINA_TRUE;
192 wd->fsw = _new_window_add(wd);
194 wd->fs = elm_fileselector_add(wd->fsw);
195 elm_fileselector_expandable_set(wd->fs, wd->fsd.expandable);
196 elm_fileselector_folder_only_set(wd->fs, wd->fsd.folder_only);
197 elm_fileselector_is_save_set(wd->fs, wd->fsd.is_save);
198 elm_fileselector_selected_set(wd->fs, wd->fsd.path);
199 evas_object_size_hint_weight_set(wd->fs, EVAS_HINT_EXPAND,
201 evas_object_size_hint_align_set(wd->fs, EVAS_HINT_FILL, EVAS_HINT_FILL);
202 evas_object_smart_callback_add(wd->fs, "done", _selection_done, wd);
203 evas_object_show(wd->fs);
207 elm_win_inwin_content_set(wd->fsw, wd->fs);
208 elm_win_inwin_activate(wd->fsw);
212 elm_win_resize_object_add(wd->fsw, wd->fs);
213 evas_object_show(wd->fsw);
218 _selection_done(void *data,
219 Evas_Object *obj __UNUSED__,
222 const char *file = event_info;
223 Widget_Data *wd = data;
227 if (file) eina_stringshare_replace(&wd->fsd.path, file);
232 evas_object_del(del);
234 evas_object_smart_callback_call(wd->self, SIG_FILE_CHOSEN,
235 (void *)wd->fsd.path);
239 * Add a new file selector button into the parent object.
241 * @param parent The parent object
242 * @return The new object or NULL if it cannot be created
244 * @ingroup File_Selector_Button
247 elm_fileselector_button_add(Evas_Object *parent)
253 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
255 wd = ELM_NEW(Widget_Data);
256 wd->window_title = eina_stringshare_add(DEFAULT_WINDOW_TITLE);
257 wd->fsd.path = eina_stringshare_add(getenv("HOME"));
258 wd->fsd.expandable = _elm_config->fileselector_expand_enable;
259 wd->inwin_mode = _elm_config->inwin_dialogs_enable;
263 e = evas_object_evas_get(parent);
265 obj = elm_widget_add(e);
266 ELM_SET_WIDTYPE(widtype, "fileselector_button");
267 elm_widget_type_set(obj, "fileselector_button");
268 elm_widget_sub_object_add(parent, obj);
270 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
271 elm_widget_data_set(obj, wd);
272 elm_widget_del_hook_set(obj, _del_hook);
273 elm_widget_theme_hook_set(obj, _theme_hook);
274 elm_widget_disable_hook_set(obj, _disable_hook);
275 elm_widget_can_focus_set(obj, EINA_TRUE);
276 elm_widget_activate_hook_set(obj, _activate_hook);
278 wd->btn = elm_button_add(parent);
279 elm_widget_resize_object_set(obj, wd->btn);
280 evas_object_event_callback_add(wd->btn, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
281 _changed_size_hints, obj);
282 evas_object_smart_callback_add(wd->btn, "clicked", _button_clicked, wd);
283 elm_widget_sub_object_add(obj, wd->btn);
286 evas_object_smart_callbacks_descriptions_set(obj, _signals);
291 * Set the label used in the file selector button.
293 * @param obj The button object
294 * @param label The text label text to be displayed on the button
296 * @ingroup File_Selector_Button
299 elm_fileselector_button_label_set(Evas_Object *obj,
302 ELM_CHECK_WIDTYPE(obj, widtype);
303 Widget_Data *wd = elm_widget_data_get(obj);
305 elm_button_label_set(wd->btn, label);
309 * Get the label used in the file selector button.
311 * @param obj The button object
312 * @return The button label
314 * @ingroup File_Selector_Button
317 elm_fileselector_button_label_get(const Evas_Object *obj)
319 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
320 Widget_Data *wd = elm_widget_data_get(obj);
321 if (!wd) return NULL;
322 return elm_button_label_get(wd->btn);
326 * Set the title of the file selector button's window.
328 * @param obj The button object
329 * @param title The title string
331 * Note that it will only take any effect if the fileselector button
332 * not at "inwin mode".
334 * @ingroup File_Selector_Button
337 elm_fileselector_button_window_title_set(Evas_Object *obj,
340 ELM_CHECK_WIDTYPE(obj, widtype);
341 Widget_Data *wd = elm_widget_data_get(obj);
344 eina_stringshare_replace(&wd->window_title, title);
347 elm_win_title_set(wd->fsw, wd->window_title);
351 * Get the title of the file selector button's window.
353 * @param obj The button object
354 * @return Title of the file selector button's window
356 * @ingroup File_Selector_Button
359 elm_fileselector_button_window_title_get(const Evas_Object *obj)
361 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
362 Widget_Data *wd = elm_widget_data_get(obj);
364 if (!wd) return NULL;
365 return wd->window_title;
369 * Set the size of the file selector button's window.
371 * @param obj The button object
372 * @param width The width
373 * @param height The height
375 * Note that it will only take any effect if the fileselector button not at
376 * "inwin mode". Default size for the window (when applicable) is 400x400.
378 * @ingroup File_Selector_Button
381 elm_fileselector_button_window_size_set(Evas_Object *obj,
385 ELM_CHECK_WIDTYPE(obj, widtype);
386 Widget_Data *wd = elm_widget_data_get(obj);
393 evas_object_resize(wd->fsw, wd->w, wd->h);
397 * Get the size of the file selector button's window.
399 * @param obj The button object
400 * @param width Pointer into which to store the width value
401 * @param height Pointer into which to store the height value
403 * @ingroup File_Selector_Button
406 elm_fileselector_button_window_size_get(const Evas_Object *obj,
410 ELM_CHECK_WIDTYPE(obj, widtype);
411 Widget_Data *wd = elm_widget_data_get(obj);
414 if (width) *width = wd->w;
415 if (height) *height = wd->h;
419 * Set the starting path of the file selector button's window.
421 * @param obj The button object
422 * @param path The path string
424 * It must be a <b>directory</b> path.
425 * Default path is "HOME" environment variable's value.
427 * @ingroup File_Selector_Button
430 elm_fileselector_button_path_set(Evas_Object *obj,
433 ELM_CHECK_WIDTYPE(obj, widtype);
434 Widget_Data *wd = elm_widget_data_get(obj);
437 eina_stringshare_replace(&wd->fsd.path, path);
440 elm_fileselector_selected_set(wd->fs, wd->fsd.path);
444 * Get the <b>last</b> path of the file selector button's window.
446 * @param obj The button object
448 * @ingroup File_Selector_Button
451 elm_fileselector_button_path_get(const Evas_Object *obj)
453 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
454 Widget_Data *wd = elm_widget_data_get(obj);
455 if (!wd) return NULL;
460 * Set whether the button's file selector is to present itself as an
461 * Elementary Generic List (which will expand its entries for nested
462 * directories) or as canonical list, which will be rendered again
463 * with the contents of each selected directory.
465 * @param obj The button object
466 * @param value The expandable flag
468 * @ingroup File_Selector_Button
471 elm_fileselector_button_expandable_set(Evas_Object *obj,
474 ELM_CHECK_WIDTYPE(obj, widtype);
475 Widget_Data *wd = elm_widget_data_get(obj);
478 wd->fsd.expandable = value;
481 elm_fileselector_expandable_set(wd->fs, wd->fsd.expandable);
485 * Get the button's file selector expandable flag.
487 * @param obj The button object
488 * @return value The expandable flag
490 * @ingroup File_Selector_Button
493 elm_fileselector_button_expandable_get(const Evas_Object *obj)
495 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
496 Widget_Data *wd = elm_widget_data_get(obj);
498 if (!wd) return EINA_FALSE;
499 return wd->fsd.expandable;
503 * Set whether the button's file selector list is to display folders
504 * only or the directory contents, as well.
506 * @param obj The button object
507 * @param value The "folder only" flag
509 * @ingroup File_Selector_Button
512 elm_fileselector_button_folder_only_set(Evas_Object *obj,
515 ELM_CHECK_WIDTYPE(obj, widtype);
516 Widget_Data *wd = elm_widget_data_get(obj);
519 wd->fsd.folder_only = value;
522 elm_fileselector_folder_only_set(wd->fs, wd->fsd.folder_only);
526 * Get the button's file selector "folder only" flag.
528 * @param obj The button object
529 * @return value The "folder only" flag
531 * @ingroup File_Selector_Button
534 elm_fileselector_button_folder_only_get(const Evas_Object *obj)
536 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
537 Widget_Data *wd = elm_widget_data_get(obj);
539 if (!wd) return EINA_FALSE;
540 return wd->fsd.folder_only;
544 * Set whether the button's file selector has an editable text entry
545 * which will hold its current selection.
547 * @param obj The button object
548 * @param value The "is save" flag
550 * @ingroup File_Selector_Button
553 elm_fileselector_button_is_save_set(Evas_Object *obj,
556 ELM_CHECK_WIDTYPE(obj, widtype);
557 Widget_Data *wd = elm_widget_data_get(obj);
560 wd->fsd.is_save = value;
563 elm_fileselector_is_save_set(wd->fs, wd->fsd.is_save);
567 * Get the button's file selector "is save" flag.
569 * @param obj The button object
570 * @return value The "is save" flag
572 * @ingroup File_Selector_Button
575 elm_fileselector_button_is_save_get(const Evas_Object *obj)
577 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
578 Widget_Data *wd = elm_widget_data_get(obj);
580 if (!wd) return EINA_FALSE;
581 return wd->fsd.is_save;
585 * Set whether the button's file selector will raise an Elementary
586 * Inner Window, instead of a dedicated Elementary Window. By default,
589 * @param obj The button object
590 * @param value The "inwin mode" flag
592 * @ingroup File_Selector_Button
595 elm_fileselector_button_inwin_mode_set(Evas_Object *obj,
598 ELM_CHECK_WIDTYPE(obj, widtype);
599 Widget_Data *wd = elm_widget_data_get(obj);
602 wd->inwin_mode = value;
606 * Get the button's file selector "inwin mode" flag.
608 * @param obj The button object
609 * @return value The "inwin mode" flag
611 * @ingroup File_Selector_Button
614 elm_fileselector_button_inwin_mode_get(const Evas_Object *obj)
616 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
617 Widget_Data *wd = elm_widget_data_get(obj);
619 if (!wd) return EINA_FALSE;
620 return wd->inwin_mode;
624 * Set the icon used for the button
626 * Once the icon object is set, a previously set one will be deleted.
627 * If you want to keep that old content object, use the
628 * elm_fileselector_button_icon_unset() function.
630 * @param obj The button object
631 * @param icon The icon object for the button
633 * @ingroup File_Selector_Button
636 elm_fileselector_button_icon_set(Evas_Object *obj,
639 ELM_CHECK_WIDTYPE(obj, widtype);
640 Widget_Data *wd = elm_widget_data_get(obj);
643 evas_object_del(icon);
646 elm_button_icon_set(wd->btn, icon);
650 * Get the icon used for the button
652 * @param obj The button object
653 * @return The icon object that is being used
655 * @ingroup File_Selector_Button
658 elm_fileselector_button_icon_get(const Evas_Object *obj)
660 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
661 Widget_Data *wd = elm_widget_data_get(obj);
662 if (!wd) return NULL;
663 return elm_button_icon_get(wd->btn);
667 * Unset the icon used for the button
669 * Unparent and return the icon object which was set for this widget.
671 * @param obj The button object
672 * @return The icon object that was being used
674 * @ingroup File_Selector_Button
677 elm_fileselector_button_icon_unset(Evas_Object *obj)
679 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
680 Widget_Data *wd = elm_widget_data_get(obj);
681 if (!wd) return NULL;
682 return elm_button_icon_unset(wd->btn);