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 _elm_fileselector_button_label_set(Evas_Object *obj, const char *item,
261 ELM_CHECK_WIDTYPE(obj, widtype);
263 Widget_Data *wd = elm_widget_data_get(obj);
265 elm_object_text_set(wd->btn, label);
269 _elm_fileselector_button_label_get(const Evas_Object *obj, const char *item)
271 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
272 Widget_Data *wd = elm_widget_data_get(obj);
273 if (item) return NULL;
274 if (!wd) return NULL;
275 return elm_object_text_get(wd->btn);
279 * Add a new file selector button into the parent object.
281 * @param parent The parent object
282 * @return The new object or NULL if it cannot be created
284 * @ingroup File_Selector_Button
287 elm_fileselector_button_add(Evas_Object *parent)
293 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
295 ELM_SET_WIDTYPE(widtype, "fileselector_button");
296 elm_widget_type_set(obj, "fileselector_button");
297 elm_widget_sub_object_add(parent, obj);
298 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
299 elm_widget_data_set(obj, wd);
300 elm_widget_del_hook_set(obj, _del_hook);
301 elm_widget_theme_hook_set(obj, _theme_hook);
302 elm_widget_disable_hook_set(obj, _disable_hook);
303 elm_widget_can_focus_set(obj, EINA_TRUE);
304 elm_widget_activate_hook_set(obj, _activate_hook);
305 elm_widget_text_set_hook_set(obj, _elm_fileselector_button_label_set);
306 elm_widget_text_get_hook_set(obj, _elm_fileselector_button_label_get);
309 wd->window_title = eina_stringshare_add(DEFAULT_WINDOW_TITLE);
310 if (getenv("HOME")) wd->fsd.path = eina_stringshare_add(getenv("HOME"));
311 else wd->fsd.path = eina_stringshare_add("/");
312 wd->fsd.expandable = _elm_config->fileselector_expand_enable;
313 wd->inwin_mode = _elm_config->inwin_dialogs_enable;
317 wd->btn = elm_button_add(parent);
318 elm_widget_mirrored_automatic_set(wd->btn, EINA_FALSE);
319 elm_widget_resize_object_set(obj, wd->btn);
320 evas_object_event_callback_add(wd->btn, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
321 _changed_size_hints, obj);
322 evas_object_smart_callback_add(wd->btn, "clicked", _button_clicked, wd);
323 elm_widget_sub_object_add(obj, wd->btn);
326 evas_object_smart_callbacks_descriptions_set(obj, _signals);
331 * Set the label used in the file selector button.
333 * @param obj The button object
334 * @param label The text label text to be displayed on the button
336 * @ingroup File_Selector_Button
337 * @deprecated use elm_object_text_set() instead.
340 elm_fileselector_button_label_set(Evas_Object *obj,
343 _elm_fileselector_button_label_set(obj, NULL, label);
347 * Get the label used in the file selector button.
349 * @param obj The button object
350 * @return The button label
352 * @ingroup File_Selector_Button
353 * @deprecated use elm_object_text_set() instead.
356 elm_fileselector_button_label_get(const Evas_Object *obj)
358 return _elm_fileselector_button_label_get(obj, NULL);
362 * Set the title of the file selector button's window.
364 * @param obj The button object
365 * @param title The title string
367 * Note that it will only take any effect if the fileselector button
368 * not at "inwin mode".
370 * @ingroup File_Selector_Button
373 elm_fileselector_button_window_title_set(Evas_Object *obj,
376 ELM_CHECK_WIDTYPE(obj, widtype);
377 Widget_Data *wd = elm_widget_data_get(obj);
380 eina_stringshare_replace(&wd->window_title, title);
383 elm_win_title_set(wd->fsw, wd->window_title);
387 * Get the title of the file selector button's window.
389 * @param obj The button object
390 * @return Title of the file selector button's window
392 * @ingroup File_Selector_Button
395 elm_fileselector_button_window_title_get(const Evas_Object *obj)
397 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
398 Widget_Data *wd = elm_widget_data_get(obj);
400 if (!wd) return NULL;
401 return wd->window_title;
405 * Set the size of the file selector button's window.
407 * @param obj The button object
408 * @param width The width
409 * @param height The height
411 * Note that it will only take any effect if the fileselector button not at
412 * "inwin mode". Default size for the window (when applicable) is 400x400.
414 * @ingroup File_Selector_Button
417 elm_fileselector_button_window_size_set(Evas_Object *obj,
421 ELM_CHECK_WIDTYPE(obj, widtype);
422 Widget_Data *wd = elm_widget_data_get(obj);
429 evas_object_resize(wd->fsw, wd->w, wd->h);
433 * Get the size of the file selector button's window.
435 * @param obj The button object
436 * @param width Pointer into which to store the width value
437 * @param height Pointer into which to store the height value
439 * @ingroup File_Selector_Button
442 elm_fileselector_button_window_size_get(const Evas_Object *obj,
446 ELM_CHECK_WIDTYPE(obj, widtype);
447 Widget_Data *wd = elm_widget_data_get(obj);
450 if (width) *width = wd->w;
451 if (height) *height = wd->h;
455 * Set the starting path of the file selector button's window.
457 * @param obj The button object
458 * @param path The path string
460 * It must be a <b>directory</b> path.
461 * Default path is "HOME" environment variable's value.
463 * @ingroup File_Selector_Button
466 elm_fileselector_button_path_set(Evas_Object *obj,
469 ELM_CHECK_WIDTYPE(obj, widtype);
470 Widget_Data *wd = elm_widget_data_get(obj);
473 eina_stringshare_replace(&wd->fsd.path, path);
476 elm_fileselector_selected_set(wd->fs, wd->fsd.path);
480 * Get the <b>last</b> path of the file selector button's window.
482 * @param obj The button object
484 * @ingroup File_Selector_Button
487 elm_fileselector_button_path_get(const Evas_Object *obj)
489 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
490 Widget_Data *wd = elm_widget_data_get(obj);
491 if (!wd) return NULL;
496 * Set whether the button's file selector is to present itself as an
497 * Elementary Generic List (which will expand its entries for nested
498 * directories) or as canonical list, which will be rendered again
499 * with the contents of each selected directory.
501 * @param obj The button object
502 * @param value The expandable flag
504 * @ingroup File_Selector_Button
507 elm_fileselector_button_expandable_set(Evas_Object *obj,
510 ELM_CHECK_WIDTYPE(obj, widtype);
511 Widget_Data *wd = elm_widget_data_get(obj);
514 wd->fsd.expandable = value;
517 elm_fileselector_expandable_set(wd->fs, wd->fsd.expandable);
521 * Get the button's file selector expandable flag.
523 * @param obj The button object
524 * @return value The expandable flag
526 * @ingroup File_Selector_Button
529 elm_fileselector_button_expandable_get(const Evas_Object *obj)
531 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
532 Widget_Data *wd = elm_widget_data_get(obj);
534 if (!wd) return EINA_FALSE;
535 return wd->fsd.expandable;
539 * Set whether the button's file selector list is to display folders
540 * only or the directory contents, as well.
542 * @param obj The button object
543 * @param value The "folder only" flag
545 * @ingroup File_Selector_Button
548 elm_fileselector_button_folder_only_set(Evas_Object *obj,
551 ELM_CHECK_WIDTYPE(obj, widtype);
552 Widget_Data *wd = elm_widget_data_get(obj);
555 wd->fsd.folder_only = value;
558 elm_fileselector_folder_only_set(wd->fs, wd->fsd.folder_only);
562 * Get the button's file selector "folder only" flag.
564 * @param obj The button object
565 * @return value The "folder only" flag
567 * @ingroup File_Selector_Button
570 elm_fileselector_button_folder_only_get(const Evas_Object *obj)
572 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
573 Widget_Data *wd = elm_widget_data_get(obj);
575 if (!wd) return EINA_FALSE;
576 return wd->fsd.folder_only;
580 * Set whether the button's file selector has an editable text entry
581 * which will hold its current selection.
583 * @param obj The button object
584 * @param value The "is save" flag
586 * @ingroup File_Selector_Button
589 elm_fileselector_button_is_save_set(Evas_Object *obj,
592 ELM_CHECK_WIDTYPE(obj, widtype);
593 Widget_Data *wd = elm_widget_data_get(obj);
596 wd->fsd.is_save = value;
599 elm_fileselector_is_save_set(wd->fs, wd->fsd.is_save);
603 * Get the button's file selector "is save" flag.
605 * @param obj The button object
606 * @return value The "is save" flag
608 * @ingroup File_Selector_Button
611 elm_fileselector_button_is_save_get(const Evas_Object *obj)
613 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
614 Widget_Data *wd = elm_widget_data_get(obj);
616 if (!wd) return EINA_FALSE;
617 return wd->fsd.is_save;
621 * Set whether the button's file selector will raise an Elementary
622 * Inner Window, instead of a dedicated Elementary Window. By default,
625 * @param obj The button object
626 * @param value The "inwin mode" flag
628 * @ingroup File_Selector_Button
631 elm_fileselector_button_inwin_mode_set(Evas_Object *obj,
634 ELM_CHECK_WIDTYPE(obj, widtype);
635 Widget_Data *wd = elm_widget_data_get(obj);
638 wd->inwin_mode = value;
642 * Get the button's file selector "inwin mode" flag.
644 * @param obj The button object
645 * @return value The "inwin mode" flag
647 * @ingroup File_Selector_Button
650 elm_fileselector_button_inwin_mode_get(const Evas_Object *obj)
652 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
653 Widget_Data *wd = elm_widget_data_get(obj);
655 if (!wd) return EINA_FALSE;
656 return wd->inwin_mode;
660 * Set the icon used for the button
662 * Once the icon object is set, a previously set one will be deleted.
663 * If you want to keep that old content object, use the
664 * elm_fileselector_button_icon_unset() function.
666 * @param obj The button object
667 * @param icon The icon object for the button
669 * @ingroup File_Selector_Button
672 elm_fileselector_button_icon_set(Evas_Object *obj,
675 ELM_CHECK_WIDTYPE(obj, widtype);
676 Widget_Data *wd = elm_widget_data_get(obj);
679 evas_object_del(icon);
682 elm_button_icon_set(wd->btn, icon);
686 * Get the icon used for the button
688 * @param obj The button object
689 * @return The icon object that is being used
691 * @ingroup File_Selector_Button
694 elm_fileselector_button_icon_get(const 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_get(wd->btn);
703 * Unset the icon used for the button
705 * Unparent and return the icon object which was set for this widget.
707 * @param obj The button object
708 * @return The icon object that was being used
710 * @ingroup File_Selector_Button
713 elm_fileselector_button_icon_unset(Evas_Object *obj)
715 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
716 Widget_Data *wd = elm_widget_data_get(obj);
717 if (!wd) return NULL;
718 return elm_button_icon_unset(wd->btn);