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.
13 * Signals that you can add callbacks for are:
15 * "file,chosen" - the user has selected a path, whose string pointer comes
20 typedef struct _Widget_Data Widget_Data;
24 Evas_Object *self, *btn, *fs, *fsw;
25 const char *window_title;
30 Eina_Bool expandable : 1;
31 Eina_Bool folder_only : 1;
32 Eina_Bool is_save : 1;
34 Eina_Bool inwin_mode : 1;
37 #define DEFAULT_WINDOW_TITLE "Select a file"
39 static const char *widtype = NULL;
41 static void _del_hook(Evas_Object *obj);
42 static void _theme_hook(Evas_Object *obj);
43 static void _disable_hook(Evas_Object *obj);
44 static void _sizing_eval(Evas_Object *obj);
45 static void _changed_size_hints(void *data,
49 static void _on_focus_hook(void *data,
51 static void _selection_done(void *data,
54 static void _activate(Widget_Data *wd);
56 static const char SIG_FILE_CHOSEN[] = "file,chosen";
57 static const Evas_Smart_Cb_Description _signals[] = {
58 {SIG_FILE_CHOSEN, "s"},
63 _del_hook(Evas_Object *obj)
68 wd = elm_widget_data_get(obj);
71 if (wd->window_title) eina_stringshare_del(wd->window_title);
72 if (wd->fsd.path) eina_stringshare_del(wd->fsd.path);
75 win = evas_object_data_del(obj, "win");
82 _on_focus_hook(void *data __UNUSED__,
85 Widget_Data *wd = elm_widget_data_get(obj);
87 if (elm_widget_focus_get(obj))
88 elm_widget_focus_steal(wd->btn);
92 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
94 Widget_Data *wd = elm_widget_data_get(obj);
96 elm_widget_mirrored_set(wd->btn, rtl);
97 elm_widget_mirrored_set(wd->fs, rtl);
101 _theme_hook(Evas_Object *obj)
103 Widget_Data *wd = elm_widget_data_get(obj);
106 _elm_widget_mirrored_reload(obj);
107 _mirrored_set(obj, elm_widget_mirrored_get(obj));
109 snprintf(buf, sizeof(buf), "fileselector_button/%s",
110 elm_widget_style_get(obj));
111 elm_object_style_set(wd->btn, buf);
116 _disable_hook(Evas_Object *obj)
118 Widget_Data *wd = elm_widget_data_get(obj);
120 elm_widget_disabled_set(wd->btn, elm_widget_disabled_get(obj));
124 _sizing_eval(Evas_Object *obj)
126 Widget_Data *wd = elm_widget_data_get(obj);
127 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
129 evas_object_size_hint_min_get(wd->btn, &minw, &minh);
130 evas_object_size_hint_max_get(wd->btn, &maxw, &maxh);
131 evas_object_size_hint_min_set(obj, minw, minh);
132 evas_object_size_hint_max_set(obj, maxw, maxh);
136 _changed_size_hints(void *data,
138 Evas_Object *obj __UNUSED__,
139 void *event_info __UNUSED__)
141 Widget_Data *wd = elm_widget_data_get(data);
147 _activate_hook(Evas_Object *obj)
150 wd = elm_widget_data_get(obj);
156 _button_clicked(void *data,
157 Evas_Object *obj __UNUSED__,
158 void *event_info __UNUSED__)
164 _parent_win_get(Evas_Object *obj)
166 while ((obj) && (strcmp(elm_widget_type_get(obj), "win")))
167 obj = elm_object_parent_widget_get(obj);
173 _new_window_add(Widget_Data *wd)
175 Evas_Object *win, *bg;
177 win = elm_win_add(NULL, "fileselector_button", ELM_WIN_DIALOG_BASIC);
178 elm_win_title_set(win, wd->window_title);
179 elm_win_autodel_set(win, EINA_TRUE);
181 bg = elm_bg_add(win);
182 elm_win_resize_object_add(win, bg);
183 evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
184 evas_object_show(bg);
186 evas_object_resize(win, wd->w, wd->h);
191 _activate(Widget_Data *wd)
193 Eina_Bool is_inwin = EINA_FALSE;
199 wd->fsw = _parent_win_get(wd->self);
202 wd->fsw = _new_window_add(wd);
205 wd->fsw = elm_win_inwin_add(wd->fsw);
206 is_inwin = EINA_TRUE;
210 wd->fsw = _new_window_add(wd);
212 wd->fs = elm_fileselector_add(wd->fsw);
213 elm_widget_mirrored_set(wd->fs, elm_widget_mirrored_get(wd->self));
214 elm_widget_mirrored_automatic_set(wd->fs, EINA_FALSE);
215 elm_fileselector_expandable_set(wd->fs, wd->fsd.expandable);
216 elm_fileselector_folder_only_set(wd->fs, wd->fsd.folder_only);
217 elm_fileselector_is_save_set(wd->fs, wd->fsd.is_save);
218 elm_fileselector_selected_set(wd->fs, wd->fsd.path);
219 evas_object_size_hint_weight_set(wd->fs, EVAS_HINT_EXPAND,
221 evas_object_size_hint_align_set(wd->fs, EVAS_HINT_FILL, EVAS_HINT_FILL);
222 evas_object_smart_callback_add(wd->fs, "done", _selection_done, wd);
223 evas_object_show(wd->fs);
227 elm_win_inwin_content_set(wd->fsw, wd->fs);
228 elm_win_inwin_activate(wd->fsw);
232 elm_win_resize_object_add(wd->fsw, wd->fs);
233 evas_object_show(wd->fsw);
238 _selection_done(void *data,
239 Evas_Object *obj __UNUSED__,
242 const char *file = event_info;
243 Widget_Data *wd = data;
247 if (file) eina_stringshare_replace(&wd->fsd.path, file);
252 evas_object_del(del);
254 evas_object_smart_callback_call(wd->self, SIG_FILE_CHOSEN,
255 (void *)wd->fsd.path);
259 * Add a new file selector button into the parent object.
261 * @param parent The parent object
262 * @return The new object or NULL if it cannot be created
264 * @ingroup File_Selector_Button
267 elm_fileselector_button_add(Evas_Object *parent)
273 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
275 ELM_SET_WIDTYPE(widtype, "fileselector_button");
276 elm_widget_type_set(obj, "fileselector_button");
277 elm_widget_sub_object_add(parent, obj);
278 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
279 elm_widget_data_set(obj, wd);
280 elm_widget_del_hook_set(obj, _del_hook);
281 elm_widget_theme_hook_set(obj, _theme_hook);
282 elm_widget_disable_hook_set(obj, _disable_hook);
283 elm_widget_can_focus_set(obj, EINA_TRUE);
284 elm_widget_activate_hook_set(obj, _activate_hook);
287 wd->window_title = eina_stringshare_add(DEFAULT_WINDOW_TITLE);
288 if (getenv("HOME")) wd->fsd.path = eina_stringshare_add(getenv("HOME"));
289 else wd->fsd.path = eina_stringshare_add("/");
290 wd->fsd.expandable = _elm_config->fileselector_expand_enable;
291 wd->inwin_mode = _elm_config->inwin_dialogs_enable;
295 wd->btn = elm_button_add(parent);
296 elm_widget_mirrored_automatic_set(wd->btn, EINA_FALSE);
297 elm_widget_resize_object_set(obj, wd->btn);
298 evas_object_event_callback_add(wd->btn, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
299 _changed_size_hints, obj);
300 evas_object_smart_callback_add(wd->btn, "clicked", _button_clicked, wd);
301 elm_widget_sub_object_add(obj, wd->btn);
304 evas_object_smart_callbacks_descriptions_set(obj, _signals);
309 * Set the label used in the file selector button.
311 * @param obj The button object
312 * @param label The text label text to be displayed on the button
314 * @ingroup File_Selector_Button
317 elm_fileselector_button_label_set(Evas_Object *obj,
320 ELM_CHECK_WIDTYPE(obj, widtype);
321 Widget_Data *wd = elm_widget_data_get(obj);
323 elm_button_label_set(wd->btn, label);
327 * Get the label used in the file selector button.
329 * @param obj The button object
330 * @return The button label
332 * @ingroup File_Selector_Button
335 elm_fileselector_button_label_get(const Evas_Object *obj)
337 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
338 Widget_Data *wd = elm_widget_data_get(obj);
339 if (!wd) return NULL;
340 return elm_button_label_get(wd->btn);
344 * Set the title of the file selector button's window.
346 * @param obj The button object
347 * @param title The title string
349 * Note that it will only take any effect if the fileselector button
350 * not at "inwin mode".
352 * @ingroup File_Selector_Button
355 elm_fileselector_button_window_title_set(Evas_Object *obj,
358 ELM_CHECK_WIDTYPE(obj, widtype);
359 Widget_Data *wd = elm_widget_data_get(obj);
362 eina_stringshare_replace(&wd->window_title, title);
365 elm_win_title_set(wd->fsw, wd->window_title);
369 * Get the title of the file selector button's window.
371 * @param obj The button object
372 * @return Title of the file selector button's window
374 * @ingroup File_Selector_Button
377 elm_fileselector_button_window_title_get(const Evas_Object *obj)
379 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
380 Widget_Data *wd = elm_widget_data_get(obj);
382 if (!wd) return NULL;
383 return wd->window_title;
387 * Set the size of the file selector button's window.
389 * @param obj The button object
390 * @param width The width
391 * @param height The height
393 * Note that it will only take any effect if the fileselector button not at
394 * "inwin mode". Default size for the window (when applicable) is 400x400.
396 * @ingroup File_Selector_Button
399 elm_fileselector_button_window_size_set(Evas_Object *obj,
403 ELM_CHECK_WIDTYPE(obj, widtype);
404 Widget_Data *wd = elm_widget_data_get(obj);
411 evas_object_resize(wd->fsw, wd->w, wd->h);
415 * Get the size of the file selector button's window.
417 * @param obj The button object
418 * @param width Pointer into which to store the width value
419 * @param height Pointer into which to store the height value
421 * @ingroup File_Selector_Button
424 elm_fileselector_button_window_size_get(const Evas_Object *obj,
428 ELM_CHECK_WIDTYPE(obj, widtype);
429 Widget_Data *wd = elm_widget_data_get(obj);
432 if (width) *width = wd->w;
433 if (height) *height = wd->h;
437 * Set the starting path of the file selector button's window.
439 * @param obj The button object
440 * @param path The path string
442 * It must be a <b>directory</b> path.
443 * Default path is "HOME" environment variable's value.
445 * @ingroup File_Selector_Button
448 elm_fileselector_button_path_set(Evas_Object *obj,
451 ELM_CHECK_WIDTYPE(obj, widtype);
452 Widget_Data *wd = elm_widget_data_get(obj);
455 eina_stringshare_replace(&wd->fsd.path, path);
458 elm_fileselector_selected_set(wd->fs, wd->fsd.path);
462 * Get the <b>last</b> path of the file selector button's window.
464 * @param obj The button object
466 * @ingroup File_Selector_Button
469 elm_fileselector_button_path_get(const Evas_Object *obj)
471 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
472 Widget_Data *wd = elm_widget_data_get(obj);
473 if (!wd) return NULL;
478 * Set whether the button's file selector is to present itself as an
479 * Elementary Generic List (which will expand its entries for nested
480 * directories) or as canonical list, which will be rendered again
481 * with the contents of each selected directory.
483 * @param obj The button object
484 * @param value The expandable flag
486 * @ingroup File_Selector_Button
489 elm_fileselector_button_expandable_set(Evas_Object *obj,
492 ELM_CHECK_WIDTYPE(obj, widtype);
493 Widget_Data *wd = elm_widget_data_get(obj);
496 wd->fsd.expandable = value;
499 elm_fileselector_expandable_set(wd->fs, wd->fsd.expandable);
503 * Get the button's file selector expandable flag.
505 * @param obj The button object
506 * @return value The expandable flag
508 * @ingroup File_Selector_Button
511 elm_fileselector_button_expandable_get(const Evas_Object *obj)
513 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
514 Widget_Data *wd = elm_widget_data_get(obj);
516 if (!wd) return EINA_FALSE;
517 return wd->fsd.expandable;
521 * Set whether the button's file selector list is to display folders
522 * only or the directory contents, as well.
524 * @param obj The button object
525 * @param value The "folder only" flag
527 * @ingroup File_Selector_Button
530 elm_fileselector_button_folder_only_set(Evas_Object *obj,
533 ELM_CHECK_WIDTYPE(obj, widtype);
534 Widget_Data *wd = elm_widget_data_get(obj);
537 wd->fsd.folder_only = value;
540 elm_fileselector_folder_only_set(wd->fs, wd->fsd.folder_only);
544 * Get the button's file selector "folder only" flag.
546 * @param obj The button object
547 * @return value The "folder only" flag
549 * @ingroup File_Selector_Button
552 elm_fileselector_button_folder_only_get(const Evas_Object *obj)
554 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
555 Widget_Data *wd = elm_widget_data_get(obj);
557 if (!wd) return EINA_FALSE;
558 return wd->fsd.folder_only;
562 * Set whether the button's file selector has an editable text entry
563 * which will hold its current selection.
565 * @param obj The button object
566 * @param value The "is save" flag
568 * @ingroup File_Selector_Button
571 elm_fileselector_button_is_save_set(Evas_Object *obj,
574 ELM_CHECK_WIDTYPE(obj, widtype);
575 Widget_Data *wd = elm_widget_data_get(obj);
578 wd->fsd.is_save = value;
581 elm_fileselector_is_save_set(wd->fs, wd->fsd.is_save);
585 * Get the button's file selector "is save" flag.
587 * @param obj The button object
588 * @return value The "is save" flag
590 * @ingroup File_Selector_Button
593 elm_fileselector_button_is_save_get(const Evas_Object *obj)
595 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
596 Widget_Data *wd = elm_widget_data_get(obj);
598 if (!wd) return EINA_FALSE;
599 return wd->fsd.is_save;
603 * Set whether the button's file selector will raise an Elementary
604 * Inner Window, instead of a dedicated Elementary Window. By default,
607 * @param obj The button object
608 * @param value The "inwin mode" flag
610 * @ingroup File_Selector_Button
613 elm_fileselector_button_inwin_mode_set(Evas_Object *obj,
616 ELM_CHECK_WIDTYPE(obj, widtype);
617 Widget_Data *wd = elm_widget_data_get(obj);
620 wd->inwin_mode = value;
624 * Get the button's file selector "inwin mode" flag.
626 * @param obj The button object
627 * @return value The "inwin mode" flag
629 * @ingroup File_Selector_Button
632 elm_fileselector_button_inwin_mode_get(const Evas_Object *obj)
634 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
635 Widget_Data *wd = elm_widget_data_get(obj);
637 if (!wd) return EINA_FALSE;
638 return wd->inwin_mode;
642 * Set the icon used for the button
644 * Once the icon object is set, a previously set one will be deleted.
645 * If you want to keep that old content object, use the
646 * elm_fileselector_button_icon_unset() function.
648 * @param obj The button object
649 * @param icon The icon object for the button
651 * @ingroup File_Selector_Button
654 elm_fileselector_button_icon_set(Evas_Object *obj,
657 ELM_CHECK_WIDTYPE(obj, widtype);
658 Widget_Data *wd = elm_widget_data_get(obj);
661 evas_object_del(icon);
664 elm_button_icon_set(wd->btn, icon);
668 * Get the icon used for the button
670 * @param obj The button object
671 * @return The icon object that is being used
673 * @ingroup File_Selector_Button
676 elm_fileselector_button_icon_get(const Evas_Object *obj)
678 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
679 Widget_Data *wd = elm_widget_data_get(obj);
680 if (!wd) return NULL;
681 return elm_button_icon_get(wd->btn);
685 * Unset the icon used for the button
687 * Unparent and return the icon object which was set for this widget.
689 * @param obj The button object
690 * @return The icon object that was being used
692 * @ingroup File_Selector_Button
695 elm_fileselector_button_icon_unset(Evas_Object *obj)
697 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
698 Widget_Data *wd = elm_widget_data_get(obj);
699 if (!wd) return NULL;
700 return elm_button_icon_unset(wd->btn);