From a20c2090799f1282863863646cc7de391e1c5d2c Mon Sep 17 00:00:00 2001 From: Ryuan Choi Date: Wed, 18 Dec 2013 20:05:35 +0900 Subject: [PATCH] fileselector: Add elm_fileselector_sort_method_get/set to select order of file/directory list. Summary: From now, the list of files/directories can be ordered by filename, type, size, modified time with both ascent and descent. Reviewers: seoz Differential Revision: https://phab.enlightenment.org/D367 --- legacy/elementary/src/bin/test_fileselector.c | 63 +++++++++ legacy/elementary/src/lib/elc_fileselector.c | 150 ++++++++++++++++++++- .../elementary/src/lib/elc_fileselector_common.h | 12 ++ legacy/elementary/src/lib/elc_fileselector_eo.h | 26 ++++ .../elementary/src/lib/elc_fileselector_legacy.h | 26 ++++ .../elementary/src/lib/elm_widget_fileselector.h | 4 + 6 files changed, 275 insertions(+), 6 deletions(-) diff --git a/legacy/elementary/src/bin/test_fileselector.c b/legacy/elementary/src/bin/test_fileselector.c index 8d00d5f..d72343a 100644 --- a/legacy/elementary/src/bin/test_fileselector.c +++ b/legacy/elementary/src/bin/test_fileselector.c @@ -334,6 +334,68 @@ _mode_option_create(Evas_Object *parent, Evas_Object *fs) } static void +_sort_selected_cb(void *data, Evas_Object *obj, void *event_info) +{ + Evas_Object *fs = evas_object_data_get(obj, "fileselector"); + const char *selected = elm_object_item_text_get(event_info); + + printf("selected sort method : %s\n", selected); + elm_object_text_set(obj, selected); + elm_fileselector_sort_method_set(fs, (Elm_Fileselector_Sort)data); +} + +static Evas_Object * +_sort_option_create(Evas_Object *parent, Evas_Object *fs) +{ + Evas_Object *frame = NULL, *hbox = NULL, *hoversel; + + frame = elm_frame_add(parent); + evas_object_size_hint_weight_set(frame, EVAS_HINT_EXPAND, 0); + evas_object_size_hint_align_set(frame, EVAS_HINT_FILL, 0); + elm_object_text_set(frame, "Sort Option"); + evas_object_show(frame); + + hbox = elm_box_add(frame); + elm_box_horizontal_set(hbox, EINA_TRUE); + elm_object_content_set(frame, hbox); + evas_object_show(hbox); + + hoversel = elm_hoversel_add(parent); + evas_object_data_set(hoversel, "fileselector", fs); + elm_object_text_set(hoversel, "Choose sort method"); + + elm_hoversel_item_add(hoversel, "File Name(asc)", NULL, ELM_ICON_NONE, + _sort_selected_cb, + (const void *)ELM_FILESELECTOR_SORT_BY_FILENAME_ASC); + elm_hoversel_item_add(hoversel, "File Name(desc)", NULL, ELM_ICON_NONE, + _sort_selected_cb, + (const void *) ELM_FILESELECTOR_SORT_BY_FILENAME_DESC); + elm_hoversel_item_add(hoversel, "Type(asc)", NULL, ELM_ICON_NONE, + _sort_selected_cb, + (const void *) ELM_FILESELECTOR_SORT_BY_TYPE_ASC); + elm_hoversel_item_add(hoversel, "Type(desc)", NULL, ELM_ICON_NONE, + _sort_selected_cb, + (const void *) ELM_FILESELECTOR_SORT_BY_TYPE_DESC); + elm_hoversel_item_add(hoversel, "Size(asc)", NULL, ELM_ICON_NONE, + _sort_selected_cb, + (const void *) ELM_FILESELECTOR_SORT_BY_SIZE_ASC); + elm_hoversel_item_add(hoversel, "Size(desc)", NULL, ELM_ICON_NONE, + _sort_selected_cb, + (const void *) ELM_FILESELECTOR_SORT_BY_SIZE_DESC); + elm_hoversel_item_add(hoversel, "Modified time(asc)", NULL, ELM_ICON_NONE, + _sort_selected_cb, + (const void *) ELM_FILESELECTOR_SORT_BY_MODIFIED_ASC); + elm_hoversel_item_add(hoversel, "Modified time(desc)", NULL, ELM_ICON_NONE, + _sort_selected_cb, + (const void *) ELM_FILESELECTOR_SORT_BY_MODIFIED_DESC); + + elm_box_pack_end(hbox, hoversel); + evas_object_show(hoversel); + + return frame; +} + +static void _small_icon_clicked(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED) @@ -471,6 +533,7 @@ test_fileselector(void *data EINA_UNUSED, elm_box_pack_end(vbox, _option_create(vbox, fs)); elm_box_pack_end(vbox, _getter_option_create(vbox, fs)); elm_box_pack_end(vbox, _mode_option_create(vbox, fs)); + elm_box_pack_end(vbox, _sort_option_create(vbox, fs)); elm_box_pack_end(vbox, _thumbnail_size_option_create(vbox, fs)); evas_object_resize(win, 320, 700); diff --git a/legacy/elementary/src/lib/elc_fileselector.c b/legacy/elementary/src/lib/elc_fileselector.c index 3e4e44a..081a541 100644 --- a/legacy/elementary/src/lib/elc_fileselector.c +++ b/legacy/elementary/src/lib/elc_fileselector.c @@ -342,10 +342,65 @@ _ls_filter_cb(void *data, return EINA_TRUE; } +static const char * +_file_type(const char *a) +{ + char *p = strrchr(a, '.'); + if (!p) return ""; + + return p; +} + +static int +_strcoll_rev(const char *a, const char *b) +{ + return strcoll(b, a); +} + +static int +_strcoll_type(const char *a, const char *b) +{ + return strcoll(_file_type(a), _file_type(b)); +} + +static int +_strcoll_type_rev(const char *a, const char *b) +{ + return _strcoll_type(b, a); +} + +static int +_size_cmp(const char *a, const char *b) +{ + return ecore_file_size(a) - ecore_file_size(b); +} + +static int +_size_cmp_rev(const char *a, const char *b) +{ + return _size_cmp(b, a); +} + +static int +_modified_cmp(const char *a, const char *b) +{ + if (ecore_file_mod_time(a) > ecore_file_mod_time(b)) + return 1; + + return -1; +} + +static int +_modified_cmp_rev(const char *a, const char *b) +{ + return _modified_cmp(b, a); +} + static int -_file_grid_cmp(const void *a, - const void *b) +_file_grid_cmp(const void *a, const void *b) { + Evas_Object *f; + const Elm_Object_Item *ga = a; const Elm_Object_Item *gb = b; const Elm_Gengrid_Item_Class *ca = elm_gengrid_item_item_class_get(ga); @@ -361,13 +416,17 @@ _file_grid_cmp(const void *a, return 1; } - return strcoll(elm_object_item_data_get(ga), elm_object_item_data_get(gb)); + f = evas_object_data_get(elm_object_item_widget_get(ga), "parent"); + ELM_FILESELECTOR_DATA_GET(f, sd); + return sd->sort_method(elm_object_item_data_get(ga), + elm_object_item_data_get(gb)); } static int -_file_list_cmp(const void *a, - const void *b) +_file_list_cmp(const void *a, const void *b) { + Evas_Object *f; + const Elm_Object_Item *la = a; const Elm_Object_Item *lb = b; const Elm_Genlist_Item_Class *ca = elm_genlist_item_item_class_get(la); @@ -383,7 +442,10 @@ _file_list_cmp(const void *a, return 1; } - return strcoll(elm_object_item_data_get(la), elm_object_item_data_get(lb)); + f = evas_object_data_get(elm_object_item_widget_get(la), "parent"); + ELM_FILESELECTOR_DATA_GET(f, sd); + return sd->sort_method(elm_object_item_data_get(la), + elm_object_item_data_get(lb)); } static void @@ -1266,6 +1328,9 @@ _elm_fileselector_smart_add(Eo *obj, void *_pd, va_list *list EINA_UNUSED) priv->thumbnail_size.w = elm_config_finger_size_get() * 2 - GENGRID_PADDING; priv->thumbnail_size.h = priv->thumbnail_size.w; + priv->sort_type = ELM_FILESELECTOR_SORT_BY_FILENAME_ASC; + priv->sort_method = strcoll; + // files_view priv->files_view = _files_list_add(obj); @@ -1994,6 +2059,75 @@ _thumbnail_size_get(Eo *obj EINA_UNUSED, void *_pd, va_list *list EINA_UNUSED) if (h) *h = sd->thumbnail_size.h; } +EAPI void +elm_fileselector_sort_method_set(Evas_Object *obj, Elm_Fileselector_Sort sort) +{ + ELM_FILESELECTOR_CHECK(obj); + eo_do(obj, elm_obj_fileselector_sort_method_set(sort)); +} + +static void +_sort_method_set(Eo *obj __UNUSED__, void *_pd, va_list *list EINA_UNUSED) +{ + Elm_Fileselector_Sort sort = va_arg(*list, Elm_Fileselector_Sort); + Elm_Fileselector_Smart_Data *sd = _pd; + + if (sd->sort_type == sort) return; + sd->sort_type = sort; + + switch (sd->sort_type) + { + case ELM_FILESELECTOR_SORT_BY_FILENAME_ASC: + sd->sort_method = strcoll; + break; + case ELM_FILESELECTOR_SORT_BY_FILENAME_DESC: + sd->sort_method = _strcoll_rev; + break; + case ELM_FILESELECTOR_SORT_BY_TYPE_ASC: + sd->sort_method = _strcoll_type; + break; + case ELM_FILESELECTOR_SORT_BY_TYPE_DESC: + sd->sort_method = _strcoll_type_rev; + break; + case ELM_FILESELECTOR_SORT_BY_SIZE_ASC: + sd->sort_method = _size_cmp; + break; + case ELM_FILESELECTOR_SORT_BY_SIZE_DESC: + sd->sort_method = _size_cmp_rev; + break; + case ELM_FILESELECTOR_SORT_BY_MODIFIED_ASC: + sd->sort_method = _modified_cmp; + break; + case ELM_FILESELECTOR_SORT_BY_MODIFIED_DESC: + sd->sort_method = _modified_cmp_rev; + break; + case ELM_FILESELECTOR_SORT_LAST: + default: + sd->sort_method = strcoll; + } + + _populate(obj, sd->path, NULL, NULL); +} + +EAPI Elm_Fileselector_Sort +elm_fileselector_sort_method_get(const Evas_Object *obj) +{ + ELM_FILESELECTOR_CHECK(obj) ELM_FILESELECTOR_SORT_LAST; + Elm_Fileselector_Sort ret = ELM_FILESELECTOR_SORT_LAST; + eo_do((Eo *) obj, elm_obj_fileselector_sort_method_get(&ret)); + + return ret; +} + +static void +_sort_method_get(Eo *obj EINA_UNUSED, void *_pd, va_list *list EINA_UNUSED) +{ + Elm_Fileselector_Sort *ret = va_arg(*list, Elm_Fileselector_Sort *); + Elm_Fileselector_Smart_Data *sd = _pd; + + if (ret) *ret = sd->sort_type; +} + static void _elm_fileselector_smart_focus_next_manager_is(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list) { @@ -2078,6 +2212,8 @@ _class_constructor(Eo_Class *klass) EO_OP_FUNC(ELM_OBJ_FILESELECTOR_ID(ELM_OBJ_FILESELECTOR_SUB_ID_HIDDEN_VISIBLE_GET), _hidden_visible_get), EO_OP_FUNC(ELM_OBJ_FILESELECTOR_ID(ELM_OBJ_FILESELECTOR_SUB_ID_THUMBNAIL_SIZE_SET), _thumbnail_size_set), EO_OP_FUNC(ELM_OBJ_FILESELECTOR_ID(ELM_OBJ_FILESELECTOR_SUB_ID_THUMBNAIL_SIZE_GET), _thumbnail_size_get), + EO_OP_FUNC(ELM_OBJ_FILESELECTOR_ID(ELM_OBJ_FILESELECTOR_SUB_ID_SORT_METHOD_SET), _sort_method_set), + EO_OP_FUNC(ELM_OBJ_FILESELECTOR_ID(ELM_OBJ_FILESELECTOR_SUB_ID_SORT_METHOD_GET), _sort_method_get), EO_OP_FUNC_SENTINEL }; eo_class_funcs_set(klass, func_desc); @@ -2142,6 +2278,8 @@ static const Eo_Op_Description op_desc[] = { EO_OP_DESCRIPTION(ELM_OBJ_FILESELECTOR_SUB_ID_HIDDEN_VISIBLE_GET, "Get if visibility of hidden files/directories in the file selector widget is enabled or disabled."), EO_OP_DESCRIPTION(ELM_OBJ_FILESELECTOR_SUB_ID_THUMBNAIL_SIZE_SET, "Set the size for the thumbnail of the file selector widget's view, if it's in #ELM_FILESELECTOR_GRID mode"), EO_OP_DESCRIPTION(ELM_OBJ_FILESELECTOR_SUB_ID_THUMBNAIL_SIZE_GET, "Get the size for the thumbnails of a given file selector widget."), + EO_OP_DESCRIPTION(ELM_OBJ_FILESELECTOR_SUB_ID_SORT_METHOD_SET, "Set the sort method of the file selector widget."), + EO_OP_DESCRIPTION(ELM_OBJ_FILESELECTOR_SUB_ID_SORT_METHOD_GET, "Get the sort method of the file selector widget."), EO_OP_DESCRIPTION_SENTINEL }; static const Eo_Class_Description class_desc = { diff --git a/legacy/elementary/src/lib/elc_fileselector_common.h b/legacy/elementary/src/lib/elc_fileselector_common.h index b6831d2..73fd30d 100644 --- a/legacy/elementary/src/lib/elc_fileselector_common.h +++ b/legacy/elementary/src/lib/elc_fileselector_common.h @@ -9,3 +9,15 @@ typedef enum ELM_FILESELECTOR_LAST /**< sentinel (helper) value, not used */ } Elm_Fileselector_Mode; +typedef enum +{ + ELM_FILESELECTOR_SORT_BY_FILENAME_ASC = 0, + ELM_FILESELECTOR_SORT_BY_FILENAME_DESC, + ELM_FILESELECTOR_SORT_BY_TYPE_ASC, + ELM_FILESELECTOR_SORT_BY_TYPE_DESC, + ELM_FILESELECTOR_SORT_BY_SIZE_ASC, + ELM_FILESELECTOR_SORT_BY_SIZE_DESC, + ELM_FILESELECTOR_SORT_BY_MODIFIED_ASC, + ELM_FILESELECTOR_SORT_BY_MODIFIED_DESC, + ELM_FILESELECTOR_SORT_LAST /**< sentinel (helper) value, not used */ +} Elm_Fileselector_Sort; diff --git a/legacy/elementary/src/lib/elc_fileselector_eo.h b/legacy/elementary/src/lib/elc_fileselector_eo.h index c429d79..b0696f8 100644 --- a/legacy/elementary/src/lib/elc_fileselector_eo.h +++ b/legacy/elementary/src/lib/elc_fileselector_eo.h @@ -34,6 +34,8 @@ enum ELM_OBJ_FILESELECTOR_SUB_ID_HIDDEN_VISIBLE_GET, ELM_OBJ_FILESELECTOR_SUB_ID_THUMBNAIL_SIZE_SET, ELM_OBJ_FILESELECTOR_SUB_ID_THUMBNAIL_SIZE_GET, + ELM_OBJ_FILESELECTOR_SUB_ID_SORT_METHOD_SET, + ELM_OBJ_FILESELECTOR_SUB_ID_SORT_METHOD_GET, ELM_OBJ_FILESELECTOR_SUB_ID_LAST }; @@ -329,5 +331,29 @@ enum #define elm_obj_fileselector_thumbnail_size_set(w, h) ELM_OBJ_FILESELECTOR_ID(ELM_OBJ_FILESELECTOR_SUB_ID_THUMBNAIL_SIZE_SET), EO_TYPECHECK(Evas_Coord, w), EO_TYPECHECK(Evas_Coord, h) /** + * @def elm_obj_fileselector_sort_method_get + * @since 1.9 + * + * Get the sort method of the file selector widget. + * + * @param[out] ret + * + * @see elm_fileselector_sort_method_get + */ +#define elm_obj_fileselector_sort_method_get(ret) ELM_OBJ_FILESELECTOR_ID(ELM_OBJ_FILESELECTOR_SUB_ID_SORT_METHOD_GET), EO_TYPECHECK(Elm_Fileselector_Sort *, ret) + +/** + * @def elm_obj_fileselector_sort_method_set + * @since 1.9 + * + * Set the sort method of the file selector widget. + * + * @param[in] sort + * + * @see elm_fileselector_sort_method_set + */ +#define elm_obj_fileselector_sort_method_set(sort) ELM_OBJ_FILESELECTOR_ID(ELM_OBJ_FILESELECTOR_SUB_ID_SORT_METHOD_SET), EO_TYPECHECK(Elm_Fileselector_Sort, sort) + +/** * @} */ diff --git a/legacy/elementary/src/lib/elc_fileselector_legacy.h b/legacy/elementary/src/lib/elc_fileselector_legacy.h index e00244d..a59936e 100644 --- a/legacy/elementary/src/lib/elc_fileselector_legacy.h +++ b/legacy/elementary/src/lib/elc_fileselector_legacy.h @@ -395,3 +395,29 @@ EAPI void elm_fileselector_thumbnail_size_set(Evas_Object *obj, * @ingroup Fileselector */ EAPI void elm_fileselector_thumbnail_size_get(const Evas_Object *obj, Evas_Coord *w, Evas_Coord *h); + +/** + * Get the sort method of the file selector widget. + * + * @param obj The file selector object + * @return The sort method + * + * @see elm_fileselector_sort_method_set() + * + * @since 1.9 + * @ingroup Fileselector + */ +EAPI Elm_Fileselector_Sort elm_fileselector_sort_method_get(const Evas_Object *obj); + +/** + * Set the sort method of the file selector widget. + * + * @param obj The file selector object + * @param sort The sort method + * + * @see elm_fileselector_sort_method_get() + * + * @since 1.9 + * @ingroup Fileselector + */ +EAPI void elm_fileselector_sort_method_set(Evas_Object *obj, Elm_Fileselector_Sort method); diff --git a/legacy/elementary/src/lib/elm_widget_fileselector.h b/legacy/elementary/src/lib/elm_widget_fileselector.h index 0b50130..bcc10bc 100644 --- a/legacy/elementary/src/lib/elm_widget_fileselector.h +++ b/legacy/elementary/src/lib/elm_widget_fileselector.h @@ -53,7 +53,11 @@ struct _Elm_Fileselector_Smart_Data Evas_Coord_Size thumbnail_size; + /* a sort method to decide orders of files/directories */ + int (*sort_method)(const char *, const char *); + Elm_Fileselector_Mode mode; + Elm_Fileselector_Sort sort_type; Eina_Bool only_folder : 1; Eina_Bool expand : 1; -- 2.7.4