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.
13 typedef struct _Widget_Data Widget_Data;
17 Evas_Object *self, *btn, *fs, *fsw;
18 const char *window_title;
23 Eina_Bool expandable : 1;
24 Eina_Bool folder_only : 1;
25 Eina_Bool is_save : 1;
27 Eina_Bool inwin_mode : 1;
30 #define DEFAULT_WINDOW_TITLE "Select a file"
32 static const char *widtype = NULL;
34 static void _del_hook(Evas_Object *obj);
35 static void _theme_hook(Evas_Object *obj);
36 static void _disable_hook(Evas_Object *obj);
37 static void _sizing_eval(Evas_Object *obj);
38 static void _changed_size_hints(void *data,
42 static void _on_focus_hook(void *data,
44 static void _selection_done(void *data,
47 static void _activate(Widget_Data *wd);
49 static const char SIG_FILE_CHOSEN[] = "file,chosen";
50 static const Evas_Smart_Cb_Description _signals[] = {
51 {SIG_FILE_CHOSEN, "s"},
56 _del_hook(Evas_Object *obj)
61 wd = elm_widget_data_get(obj);
64 if (wd->window_title) eina_stringshare_del(wd->window_title);
65 if (wd->fsd.path) eina_stringshare_del(wd->fsd.path);
68 win = evas_object_data_del(obj, "win");
75 _on_focus_hook(void *data __UNUSED__,
78 Widget_Data *wd = elm_widget_data_get(obj);
80 if (elm_widget_focus_get(obj))
81 elm_widget_focus_steal(wd->btn);
85 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
87 Widget_Data *wd = elm_widget_data_get(obj);
89 elm_widget_mirrored_set(wd->btn, rtl);
90 elm_widget_mirrored_set(wd->fs, rtl);
94 _theme_hook(Evas_Object *obj)
96 Widget_Data *wd = elm_widget_data_get(obj);
99 _elm_widget_mirrored_reload(obj);
100 _mirrored_set(obj, elm_widget_mirrored_get(obj));
102 snprintf(buf, sizeof(buf), "fileselector_button/%s",
103 elm_widget_style_get(obj));
104 elm_object_style_set(wd->btn, buf);
109 _disable_hook(Evas_Object *obj)
111 Widget_Data *wd = elm_widget_data_get(obj);
113 elm_widget_disabled_set(wd->btn, elm_widget_disabled_get(obj));
117 _sizing_eval(Evas_Object *obj)
119 Widget_Data *wd = elm_widget_data_get(obj);
120 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
122 evas_object_size_hint_min_get(wd->btn, &minw, &minh);
123 evas_object_size_hint_max_get(wd->btn, &maxw, &maxh);
124 evas_object_size_hint_min_set(obj, minw, minh);
125 evas_object_size_hint_max_set(obj, maxw, maxh);
129 _changed_size_hints(void *data,
131 Evas_Object *obj __UNUSED__,
132 void *event_info __UNUSED__)
134 Widget_Data *wd = elm_widget_data_get(data);
140 _activate_hook(Evas_Object *obj)
143 wd = elm_widget_data_get(obj);
149 _button_clicked(void *data,
150 Evas_Object *obj __UNUSED__,
151 void *event_info __UNUSED__)
157 _parent_win_get(Evas_Object *obj)
159 while ((obj) && (strcmp(elm_widget_type_get(obj), "win")))
160 obj = elm_object_parent_widget_get(obj);
166 _new_window_add(Widget_Data *wd)
168 Evas_Object *win, *bg;
170 win = elm_win_add(NULL, "fileselector_button", ELM_WIN_DIALOG_BASIC);
171 elm_win_title_set(win, wd->window_title);
172 elm_win_autodel_set(win, EINA_TRUE);
174 bg = elm_bg_add(win);
175 elm_win_resize_object_add(win, bg);
176 evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
177 evas_object_show(bg);
179 evas_object_resize(win, wd->w, wd->h);
184 _activate(Widget_Data *wd)
186 Eina_Bool is_inwin = EINA_FALSE;
192 wd->fsw = _parent_win_get(wd->self);
195 wd->fsw = _new_window_add(wd);
198 wd->fsw = elm_win_inwin_add(wd->fsw);
199 is_inwin = EINA_TRUE;
203 wd->fsw = _new_window_add(wd);
205 wd->fs = elm_fileselector_add(wd->fsw);
206 elm_widget_mirrored_set(wd->fs, elm_widget_mirrored_get(wd->self));
207 elm_widget_mirrored_automatic_set(wd->fs, EINA_FALSE);
208 elm_fileselector_expandable_set(wd->fs, wd->fsd.expandable);
209 elm_fileselector_folder_only_set(wd->fs, wd->fsd.folder_only);
210 elm_fileselector_is_save_set(wd->fs, wd->fsd.is_save);
211 elm_fileselector_selected_set(wd->fs, wd->fsd.path);
212 evas_object_size_hint_weight_set(wd->fs, EVAS_HINT_EXPAND,
214 evas_object_size_hint_align_set(wd->fs, EVAS_HINT_FILL, EVAS_HINT_FILL);
215 evas_object_smart_callback_add(wd->fs, "done", _selection_done, wd);
216 evas_object_show(wd->fs);
220 elm_win_inwin_content_set(wd->fsw, wd->fs);
221 elm_win_inwin_activate(wd->fsw);
225 elm_win_resize_object_add(wd->fsw, wd->fs);
226 evas_object_show(wd->fsw);
231 _selection_done(void *data,
232 Evas_Object *obj __UNUSED__,
235 const char *file = event_info;
236 Widget_Data *wd = data;
240 if (file) eina_stringshare_replace(&wd->fsd.path, file);
245 evas_object_del(del);
247 evas_object_smart_callback_call(wd->self, SIG_FILE_CHOSEN,
248 (void *)wd->fsd.path);
252 * Add a new file selector button into the parent object.
254 * @param parent The parent object
255 * @return The new object or NULL if it cannot be created
257 * @ingroup File_Selector_Button
260 elm_fileselector_button_add(Evas_Object *parent)
266 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
268 ELM_SET_WIDTYPE(widtype, "fileselector_button");
269 elm_widget_type_set(obj, "fileselector_button");
270 elm_widget_sub_object_add(parent, obj);
271 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
272 elm_widget_data_set(obj, wd);
273 elm_widget_del_hook_set(obj, _del_hook);
274 elm_widget_theme_hook_set(obj, _theme_hook);
275 elm_widget_disable_hook_set(obj, _disable_hook);
276 elm_widget_can_focus_set(obj, EINA_TRUE);
277 elm_widget_activate_hook_set(obj, _activate_hook);
280 wd->window_title = eina_stringshare_add(DEFAULT_WINDOW_TITLE);
281 if (getenv("HOME")) wd->fsd.path = eina_stringshare_add(getenv("HOME"));
282 else wd->fsd.path = eina_stringshare_add("/");
283 wd->fsd.expandable = _elm_config->fileselector_expand_enable;
284 wd->inwin_mode = _elm_config->inwin_dialogs_enable;
288 wd->btn = elm_button_add(parent);
289 elm_widget_mirrored_automatic_set(wd->btn, EINA_FALSE);
290 elm_widget_resize_object_set(obj, wd->btn);
291 evas_object_event_callback_add(wd->btn, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
292 _changed_size_hints, obj);
293 evas_object_smart_callback_add(wd->btn, "clicked", _button_clicked, wd);
294 elm_widget_sub_object_add(obj, wd->btn);
297 evas_object_smart_callbacks_descriptions_set(obj, _signals);
302 * Set the label used in the file selector button.
304 * @param obj The button object
305 * @param label The text label text to be displayed on the button
307 * @ingroup File_Selector_Button
310 elm_fileselector_button_label_set(Evas_Object *obj,
313 ELM_CHECK_WIDTYPE(obj, widtype);
314 Widget_Data *wd = elm_widget_data_get(obj);
316 elm_button_label_set(wd->btn, label);
320 * Get the label used in the file selector button.
322 * @param obj The button object
323 * @return The button label
325 * @ingroup File_Selector_Button
328 elm_fileselector_button_label_get(const Evas_Object *obj)
330 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
331 Widget_Data *wd = elm_widget_data_get(obj);
332 if (!wd) return NULL;
333 return elm_button_label_get(wd->btn);
337 * Set the title of the file selector button's window.
339 * @param obj The button object
340 * @param title The title string
342 * Note that it will only take any effect if the fileselector button
343 * not at "inwin mode".
345 * @ingroup File_Selector_Button
348 elm_fileselector_button_window_title_set(Evas_Object *obj,
351 ELM_CHECK_WIDTYPE(obj, widtype);
352 Widget_Data *wd = elm_widget_data_get(obj);
355 eina_stringshare_replace(&wd->window_title, title);
358 elm_win_title_set(wd->fsw, wd->window_title);
362 * Get the title of the file selector button's window.
364 * @param obj The button object
365 * @return Title of the file selector button's window
367 * @ingroup File_Selector_Button
370 elm_fileselector_button_window_title_get(const Evas_Object *obj)
372 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
373 Widget_Data *wd = elm_widget_data_get(obj);
375 if (!wd) return NULL;
376 return wd->window_title;
380 * Set the size of the file selector button's window.
382 * @param obj The button object
383 * @param width The width
384 * @param height The height
386 * Note that it will only take any effect if the fileselector button not at
387 * "inwin mode". Default size for the window (when applicable) is 400x400.
389 * @ingroup File_Selector_Button
392 elm_fileselector_button_window_size_set(Evas_Object *obj,
396 ELM_CHECK_WIDTYPE(obj, widtype);
397 Widget_Data *wd = elm_widget_data_get(obj);
404 evas_object_resize(wd->fsw, wd->w, wd->h);
408 * Get the size of the file selector button's window.
410 * @param obj The button object
411 * @param width Pointer into which to store the width value
412 * @param height Pointer into which to store the height value
414 * @ingroup File_Selector_Button
417 elm_fileselector_button_window_size_get(const Evas_Object *obj,
421 ELM_CHECK_WIDTYPE(obj, widtype);
422 Widget_Data *wd = elm_widget_data_get(obj);
425 if (width) *width = wd->w;
426 if (height) *height = wd->h;
430 * Set the starting path of the file selector button's window.
432 * @param obj The button object
433 * @param path The path string
435 * It must be a <b>directory</b> path.
436 * Default path is "HOME" environment variable's value.
438 * @ingroup File_Selector_Button
441 elm_fileselector_button_path_set(Evas_Object *obj,
444 ELM_CHECK_WIDTYPE(obj, widtype);
445 Widget_Data *wd = elm_widget_data_get(obj);
448 eina_stringshare_replace(&wd->fsd.path, path);
451 elm_fileselector_selected_set(wd->fs, wd->fsd.path);
455 * Get the <b>last</b> path of the file selector button's window.
457 * @param obj The button object
459 * @ingroup File_Selector_Button
462 elm_fileselector_button_path_get(const Evas_Object *obj)
464 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
465 Widget_Data *wd = elm_widget_data_get(obj);
466 if (!wd) return NULL;
471 * Set whether the button's file selector is to present itself as an
472 * Elementary Generic List (which will expand its entries for nested
473 * directories) or as canonical list, which will be rendered again
474 * with the contents of each selected directory.
476 * @param obj The button object
477 * @param value The expandable flag
479 * @ingroup File_Selector_Button
482 elm_fileselector_button_expandable_set(Evas_Object *obj,
485 ELM_CHECK_WIDTYPE(obj, widtype);
486 Widget_Data *wd = elm_widget_data_get(obj);
489 wd->fsd.expandable = value;
492 elm_fileselector_expandable_set(wd->fs, wd->fsd.expandable);
496 * Get the button's file selector expandable flag.
498 * @param obj The button object
499 * @return value The expandable flag
501 * @ingroup File_Selector_Button
504 elm_fileselector_button_expandable_get(const Evas_Object *obj)
506 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
507 Widget_Data *wd = elm_widget_data_get(obj);
509 if (!wd) return EINA_FALSE;
510 return wd->fsd.expandable;
514 * Set whether the button's file selector list is to display folders
515 * only or the directory contents, as well.
517 * @param obj The button object
518 * @param value The "folder only" flag
520 * @ingroup File_Selector_Button
523 elm_fileselector_button_folder_only_set(Evas_Object *obj,
526 ELM_CHECK_WIDTYPE(obj, widtype);
527 Widget_Data *wd = elm_widget_data_get(obj);
530 wd->fsd.folder_only = value;
533 elm_fileselector_folder_only_set(wd->fs, wd->fsd.folder_only);
537 * Get the button's file selector "folder only" flag.
539 * @param obj The button object
540 * @return value The "folder only" flag
542 * @ingroup File_Selector_Button
545 elm_fileselector_button_folder_only_get(const Evas_Object *obj)
547 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
548 Widget_Data *wd = elm_widget_data_get(obj);
550 if (!wd) return EINA_FALSE;
551 return wd->fsd.folder_only;
555 * Set whether the button's file selector has an editable text entry
556 * which will hold its current selection.
558 * @param obj The button object
559 * @param value The "is save" flag
561 * @ingroup File_Selector_Button
564 elm_fileselector_button_is_save_set(Evas_Object *obj,
567 ELM_CHECK_WIDTYPE(obj, widtype);
568 Widget_Data *wd = elm_widget_data_get(obj);
571 wd->fsd.is_save = value;
574 elm_fileselector_is_save_set(wd->fs, wd->fsd.is_save);
578 * Get the button's file selector "is save" flag.
580 * @param obj The button object
581 * @return value The "is save" flag
583 * @ingroup File_Selector_Button
586 elm_fileselector_button_is_save_get(const Evas_Object *obj)
588 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
589 Widget_Data *wd = elm_widget_data_get(obj);
591 if (!wd) return EINA_FALSE;
592 return wd->fsd.is_save;
596 * Set whether the button's file selector will raise an Elementary
597 * Inner Window, instead of a dedicated Elementary Window. By default,
600 * @param obj The button object
601 * @param value The "inwin mode" flag
603 * @ingroup File_Selector_Button
606 elm_fileselector_button_inwin_mode_set(Evas_Object *obj,
609 ELM_CHECK_WIDTYPE(obj, widtype);
610 Widget_Data *wd = elm_widget_data_get(obj);
613 wd->inwin_mode = value;
617 * Get the button's file selector "inwin mode" flag.
619 * @param obj The button object
620 * @return value The "inwin mode" flag
622 * @ingroup File_Selector_Button
625 elm_fileselector_button_inwin_mode_get(const Evas_Object *obj)
627 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
628 Widget_Data *wd = elm_widget_data_get(obj);
630 if (!wd) return EINA_FALSE;
631 return wd->inwin_mode;
635 * Set the icon used for the button
637 * Once the icon object is set, a previously set one will be deleted.
638 * If you want to keep that old content object, use the
639 * elm_fileselector_button_icon_unset() function.
641 * @param obj The button object
642 * @param icon The icon object for the button
644 * @ingroup File_Selector_Button
647 elm_fileselector_button_icon_set(Evas_Object *obj,
650 ELM_CHECK_WIDTYPE(obj, widtype);
651 Widget_Data *wd = elm_widget_data_get(obj);
654 evas_object_del(icon);
657 elm_button_icon_set(wd->btn, icon);
661 * Get the icon used for the button
663 * @param obj The button object
664 * @return The icon object that is being used
666 * @ingroup File_Selector_Button
669 elm_fileselector_button_icon_get(const Evas_Object *obj)
671 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
672 Widget_Data *wd = elm_widget_data_get(obj);
673 if (!wd) return NULL;
674 return elm_button_icon_get(wd->btn);
678 * Unset the icon used for the button
680 * Unparent and return the icon object which was set for this widget.
682 * @param obj The button object
683 * @return The icon object that was being used
685 * @ingroup File_Selector_Button
688 elm_fileselector_button_icon_unset(Evas_Object *obj)
690 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
691 Widget_Data *wd = elm_widget_data_get(obj);
692 if (!wd) return NULL;
693 return elm_button_icon_unset(wd->btn);