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 _theme_hook(Evas_Object *obj)
87 Widget_Data *wd = elm_widget_data_get(obj);
90 snprintf(buf, sizeof(buf), "fileselector_button/%s",
91 elm_widget_style_get(obj));
92 elm_object_style_set(wd->btn, buf);
97 _disable_hook(Evas_Object *obj)
99 Widget_Data *wd = elm_widget_data_get(obj);
101 elm_widget_disabled_set(wd->btn, elm_widget_disabled_get(obj));
105 _sizing_eval(Evas_Object *obj)
107 Widget_Data *wd = elm_widget_data_get(obj);
108 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
110 evas_object_size_hint_min_get(wd->btn, &minw, &minh);
111 evas_object_size_hint_max_get(wd->btn, &maxw, &maxh);
112 evas_object_size_hint_min_set(obj, minw, minh);
113 evas_object_size_hint_max_set(obj, maxw, maxh);
117 _changed_size_hints(void *data,
119 Evas_Object *obj __UNUSED__,
120 void *event_info __UNUSED__)
122 Widget_Data *wd = elm_widget_data_get(data);
128 _activate_hook(Evas_Object *obj)
131 wd = elm_widget_data_get(obj);
137 _button_clicked(void *data,
138 Evas_Object *obj __UNUSED__,
139 void *event_info __UNUSED__)
145 _parent_win_get(Evas_Object *obj)
147 while ((obj) && (strcmp(elm_widget_type_get(obj), "win")))
148 obj = elm_object_parent_widget_get(obj);
154 _new_window_add(Widget_Data *wd)
156 Evas_Object *win, *bg;
158 win = elm_win_add(NULL, "fileselector_button", ELM_WIN_DIALOG_BASIC);
159 elm_win_title_set(win, wd->window_title);
160 elm_win_autodel_set(win, EINA_TRUE);
162 bg = elm_bg_add(win);
163 elm_win_resize_object_add(win, bg);
164 evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
165 evas_object_show(bg);
167 evas_object_resize(win, wd->w, wd->h);
172 _activate(Widget_Data *wd)
174 Eina_Bool is_inwin = EINA_FALSE;
180 wd->fsw = _parent_win_get(wd->self);
183 wd->fsw = _new_window_add(wd);
186 wd->fsw = elm_win_inwin_add(wd->fsw);
187 is_inwin = EINA_TRUE;
191 wd->fsw = _new_window_add(wd);
193 wd->fs = elm_fileselector_add(wd->fsw);
194 elm_fileselector_expandable_set(wd->fs, wd->fsd.expandable);
195 elm_fileselector_folder_only_set(wd->fs, wd->fsd.folder_only);
196 elm_fileselector_is_save_set(wd->fs, wd->fsd.is_save);
197 elm_fileselector_selected_set(wd->fs, wd->fsd.path);
198 evas_object_size_hint_weight_set(wd->fs, EVAS_HINT_EXPAND,
200 evas_object_size_hint_align_set(wd->fs, EVAS_HINT_FILL, EVAS_HINT_FILL);
201 evas_object_smart_callback_add(wd->fs, "done", _selection_done, wd);
202 evas_object_show(wd->fs);
206 elm_win_inwin_content_set(wd->fsw, wd->fs);
207 elm_win_inwin_activate(wd->fsw);
211 elm_win_resize_object_add(wd->fsw, wd->fs);
212 evas_object_show(wd->fsw);
217 _selection_done(void *data,
218 Evas_Object *obj __UNUSED__,
221 const char *file = event_info;
222 Widget_Data *wd = data;
226 if (file) eina_stringshare_replace(&wd->fsd.path, file);
231 evas_object_del(del);
233 evas_object_smart_callback_call(wd->self, SIG_FILE_CHOSEN,
234 (void *)wd->fsd.path);
238 * Add a new file selector button into the parent object.
240 * @param parent The parent object
241 * @return The new object or NULL if it cannot be created
243 * @ingroup File_Selector_Button
246 elm_fileselector_button_add(Evas_Object *parent)
252 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
254 wd = ELM_NEW(Widget_Data);
255 wd->window_title = eina_stringshare_add(DEFAULT_WINDOW_TITLE);
256 wd->fsd.path = eina_stringshare_add(getenv("HOME"));
257 wd->fsd.expandable = _elm_config->fileselector_expand_enable;
258 wd->inwin_mode = _elm_config->inwin_dialogs_enable;
262 e = evas_object_evas_get(parent);
264 obj = elm_widget_add(e);
265 ELM_SET_WIDTYPE(widtype, "fileselector_button");
266 elm_widget_type_set(obj, "fileselector_button");
267 elm_widget_sub_object_add(parent, obj);
269 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
270 elm_widget_data_set(obj, wd);
271 elm_widget_del_hook_set(obj, _del_hook);
272 elm_widget_theme_hook_set(obj, _theme_hook);
273 elm_widget_disable_hook_set(obj, _disable_hook);
274 elm_widget_can_focus_set(obj, EINA_TRUE);
275 elm_widget_activate_hook_set(obj, _activate_hook);
277 wd->btn = elm_button_add(parent);
278 elm_widget_resize_object_set(obj, wd->btn);
279 evas_object_event_callback_add(wd->btn, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
280 _changed_size_hints, obj);
281 evas_object_smart_callback_add(wd->btn, "clicked", _button_clicked, wd);
282 elm_widget_sub_object_add(obj, wd->btn);
285 evas_object_smart_callbacks_descriptions_set(obj, _signals);
290 * Set the label used in the file selector button.
292 * @param obj The button object
293 * @param label The text label text to be displayed on the button
295 * @ingroup File_Selector_Button
298 elm_fileselector_button_label_set(Evas_Object *obj,
301 ELM_CHECK_WIDTYPE(obj, widtype);
302 Widget_Data *wd = elm_widget_data_get(obj);
304 elm_button_label_set(wd->btn, label);
308 * Get the label used in the file selector button.
310 * @param obj The button object
311 * @return The button label
313 * @ingroup File_Selector_Button
316 elm_fileselector_button_label_get(const Evas_Object *obj)
318 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
319 Widget_Data *wd = elm_widget_data_get(obj);
320 if (!wd) return NULL;
321 return elm_button_label_get(wd->btn);
325 * Set the title of the file selector button's window.
327 * @param obj The button object
328 * @param title The title string
330 * Note that it will only take any effect if the fileselector button
331 * not at "inwin mode".
333 * @ingroup File_Selector_Button
336 elm_fileselector_button_window_title_set(Evas_Object *obj,
339 ELM_CHECK_WIDTYPE(obj, widtype);
340 Widget_Data *wd = elm_widget_data_get(obj);
343 eina_stringshare_replace(&wd->window_title, title);
346 elm_win_title_set(wd->fsw, wd->window_title);
350 * Get the title of the file selector button's window.
352 * @param obj The button object
353 * @return Title of the file selector button's window
355 * @ingroup File_Selector_Button
358 elm_fileselector_button_window_title_get(const Evas_Object *obj)
360 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
361 Widget_Data *wd = elm_widget_data_get(obj);
363 if (!wd) return NULL;
364 return wd->window_title;
368 * Set the size of the file selector button's window.
370 * @param obj The button object
371 * @param width The width
372 * @param height The height
374 * Note that it will only take any effect if the fileselector button not at
375 * "inwin mode". Default size for the window (when applicable) is 400x400.
377 * @ingroup File_Selector_Button
380 elm_fileselector_button_window_size_set(Evas_Object *obj,
384 ELM_CHECK_WIDTYPE(obj, widtype);
385 Widget_Data *wd = elm_widget_data_get(obj);
392 evas_object_resize(wd->fsw, wd->w, wd->h);
396 * Get the size of the file selector button's window.
398 * @param obj The button object
399 * @param width Pointer into which to store the width value
400 * @param height Pointer into which to store the height value
402 * @ingroup File_Selector_Button
405 elm_fileselector_button_window_size_get(const Evas_Object *obj,
409 ELM_CHECK_WIDTYPE(obj, widtype);
410 Widget_Data *wd = elm_widget_data_get(obj);
413 if (width) *width = wd->w;
414 if (height) *height = wd->h;
418 * Set the starting path of the file selector button's window.
420 * @param obj The button object
421 * @param path The path string
423 * It must be a <b>directory</b> path.
424 * Default path is "HOME" environment variable's value.
426 * @ingroup File_Selector_Button
429 elm_fileselector_button_path_set(Evas_Object *obj,
432 ELM_CHECK_WIDTYPE(obj, widtype);
433 Widget_Data *wd = elm_widget_data_get(obj);
436 eina_stringshare_replace(&wd->fsd.path, path);
439 elm_fileselector_selected_set(wd->fs, wd->fsd.path);
443 * Get the <b>last</b> path of the file selector button's window.
445 * @param obj The button object
447 * @ingroup File_Selector_Button
450 elm_fileselector_button_path_get(const Evas_Object *obj)
452 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
453 Widget_Data *wd = elm_widget_data_get(obj);
454 if (!wd) return NULL;
459 * Set whether the button's file selector is to present itself as an
460 * Elementary Generic List (which will expand its entries for nested
461 * directories) or as canonical list, which will be rendered again
462 * with the contents of each selected directory.
464 * @param obj The button object
465 * @param value The expandable flag
467 * @ingroup File_Selector_Button
470 elm_fileselector_button_expandable_set(Evas_Object *obj,
473 ELM_CHECK_WIDTYPE(obj, widtype);
474 Widget_Data *wd = elm_widget_data_get(obj);
477 wd->fsd.expandable = value;
480 elm_fileselector_expandable_set(wd->fs, wd->fsd.expandable);
484 * Get the button's file selector expandable flag.
486 * @param obj The button object
487 * @return value The expandable flag
489 * @ingroup File_Selector_Button
492 elm_fileselector_button_expandable_get(const Evas_Object *obj)
494 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
495 Widget_Data *wd = elm_widget_data_get(obj);
497 if (!wd) return EINA_FALSE;
498 return wd->fsd.expandable;
502 * Set whether the button's file selector list is to display folders
503 * only or the directory contents, as well.
505 * @param obj The button object
506 * @param value The "folder only" flag
508 * @ingroup File_Selector_Button
511 elm_fileselector_button_folder_only_set(Evas_Object *obj,
514 ELM_CHECK_WIDTYPE(obj, widtype);
515 Widget_Data *wd = elm_widget_data_get(obj);
518 wd->fsd.folder_only = value;
521 elm_fileselector_folder_only_set(wd->fs, wd->fsd.folder_only);
525 * Get the button's file selector "folder only" flag.
527 * @param obj The button object
528 * @return value The "folder only" flag
530 * @ingroup File_Selector_Button
533 elm_fileselector_button_folder_only_get(const Evas_Object *obj)
535 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
536 Widget_Data *wd = elm_widget_data_get(obj);
538 if (!wd) return EINA_FALSE;
539 return wd->fsd.folder_only;
543 * Set whether the button's file selector has an editable text entry
544 * which will hold its current selection.
546 * @param obj The button object
547 * @param value The "is save" flag
549 * @ingroup File_Selector_Button
552 elm_fileselector_button_is_save_set(Evas_Object *obj,
555 ELM_CHECK_WIDTYPE(obj, widtype);
556 Widget_Data *wd = elm_widget_data_get(obj);
559 wd->fsd.is_save = value;
562 elm_fileselector_is_save_set(wd->fs, wd->fsd.is_save);
566 * Get the button's file selector "is save" flag.
568 * @param obj The button object
569 * @return value The "is save" flag
571 * @ingroup File_Selector_Button
574 elm_fileselector_button_is_save_get(const Evas_Object *obj)
576 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
577 Widget_Data *wd = elm_widget_data_get(obj);
579 if (!wd) return EINA_FALSE;
580 return wd->fsd.is_save;
584 * Set whether the button's file selector will raise an Elementary
585 * Inner Window, instead of a dedicated Elementary Window. By default,
588 * @param obj The button object
589 * @param value The "inwin mode" flag
591 * @ingroup File_Selector_Button
594 elm_fileselector_button_inwin_mode_set(Evas_Object *obj,
597 ELM_CHECK_WIDTYPE(obj, widtype);
598 Widget_Data *wd = elm_widget_data_get(obj);
601 wd->inwin_mode = value;
605 * Get the button's file selector "inwin mode" flag.
607 * @param obj The button object
608 * @return value The "inwin mode" flag
610 * @ingroup File_Selector_Button
613 elm_fileselector_button_inwin_mode_get(const Evas_Object *obj)
615 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
616 Widget_Data *wd = elm_widget_data_get(obj);
618 if (!wd) return EINA_FALSE;
619 return wd->inwin_mode;
623 * Set the icon used for the button
625 * Once the icon object is set, a previously set one will be deleted.
626 * If you want to keep that old content object, use the
627 * elm_fileselector_button_icon_unset() function.
629 * @param obj The button object
630 * @param icon The icon object for the button
632 * @ingroup File_Selector_Button
635 elm_fileselector_button_icon_set(Evas_Object *obj,
638 ELM_CHECK_WIDTYPE(obj, widtype);
639 Widget_Data *wd = elm_widget_data_get(obj);
642 evas_object_del(icon);
645 elm_button_icon_set(wd->btn, icon);
649 * Get the icon used for the button
651 * @param obj The button object
652 * @return The icon object that is being used
654 * @ingroup File_Selector_Button
657 elm_fileselector_button_icon_get(const Evas_Object *obj)
659 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
660 Widget_Data *wd = elm_widget_data_get(obj);
661 if (!wd) return NULL;
662 return elm_button_icon_get(wd->btn);
666 * Unset the icon used for the button
668 * Unparent and return the icon object which was set for this widget.
670 * @param obj The button object
671 * @return The icon object that was being used
673 * @ingroup File_Selector_Button
676 elm_fileselector_button_icon_unset(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_unset(wd->btn);