From f6ad9605d66bb4a0854c11b8d42e76907412aac0 Mon Sep 17 00:00:00 2001 From: Gustavo Lima Chaves Date: Thu, 26 Dec 2013 15:27:39 -0200 Subject: [PATCH] New Elementary API -- elm_multibuttonentry_format_function_set(). Useful when one does not want the default multi button entry counter item's format string. --- .../src/bin/test_multibuttonentry.c | 66 +++++++++++-------- .../elementary/src/lib/elc_multibuttonentry.c | 62 +++++++++++++++-- .../src/lib/elc_multibuttonentry_common.h | 18 +++++ .../src/lib/elc_multibuttonentry_eo.h | 20 ++++++ .../src/lib/elc_multibuttonentry_legacy.h | 21 ++++++ .../src/lib/elm_widget_multibuttonentry.h | 3 + 6 files changed, 157 insertions(+), 33 deletions(-) diff --git a/legacy/elementary/src/bin/test_multibuttonentry.c b/legacy/elementary/src/bin/test_multibuttonentry.c index 6ba265fda0..83da32e476 100644 --- a/legacy/elementary/src/bin/test_multibuttonentry.c +++ b/legacy/elementary/src/bin/test_multibuttonentry.c @@ -3,6 +3,17 @@ #endif #include +static Elm_Multibuttonentry_Format_Cb format_func = NULL; + +static char * +_custom_format(int count, void *data EINA_UNUSED) +{ + char buf[32]; + + if (!snprintf(buf, sizeof(buf), "+ %d rabbits", count)) return NULL; + return strdup(buf); +} + static void _item_selected_cb(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, @@ -122,11 +133,32 @@ _item_filter_cb(Evas_Object *obj EINA_UNUSED, } static void -_button_clicked_cb(void *data EINA_UNUSED, +_format_change_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED) { - printf("%s button is clicked\n", __func__); + Evas_Object *mbe = data; + + if (format_func) format_func = NULL; + else format_func = _custom_format; + + elm_multibuttonentry_format_function_set(mbe, format_func, NULL); + + printf("Changing format function to %p\n", format_func); +} + +static Evas_Object* +_format_change_btn_add(Evas_Object *mbe) +{ + Evas_Object *btn; + + btn = elm_button_add(mbe); + evas_object_smart_callback_add(btn, "clicked", _format_change_cb, mbe); + elm_object_text_set(btn, "Change format function"); + evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); + evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, EVAS_HINT_FILL); + + return btn; } static Evas_Object* @@ -134,6 +166,7 @@ _add_multibuttonentry(Evas_Object *parent) { Evas_Object *scr = NULL; Evas_Object *mbe = NULL; + Evas_Object *btn = NULL; void *data = NULL; scr = elm_scroller_add(parent); @@ -166,39 +199,21 @@ _add_multibuttonentry(Evas_Object *parent) evas_object_smart_callback_add(mbe, "contracted", _contracted_cb, NULL); evas_object_smart_callback_add(mbe, "shrink,state,changed", _shrink_state_changed_cb, NULL); + btn = _format_change_btn_add(mbe); + elm_object_part_content_set(parent, "box", btn); + evas_object_resize(mbe, 220, 300); elm_object_focus_set(mbe, EINA_TRUE); return scr; } -static Evas_Object* -_add_buttons(Evas_Object *parent) -{ - Evas_Object *bx = NULL; - Evas_Object *btn; - - bx = elm_box_add(parent); - elm_box_horizontal_set(bx, EINA_TRUE); - elm_box_homogeneous_set(bx, EINA_TRUE); - - btn = elm_button_add(parent); - evas_object_smart_callback_add(btn, "clicked", _button_clicked_cb, NULL); - elm_object_text_set(btn, "click"); - evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, 0.0); - evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, EVAS_HINT_FILL); - elm_box_pack_end(bx, btn); - evas_object_show(btn); - - return bx; -} - void test_multibuttonentry(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED) { - Evas_Object *win, *sc, *bx; + Evas_Object *win, *sc; Evas_Object *ly; char buf[PATH_MAX]; @@ -215,9 +230,6 @@ test_multibuttonentry(void *data EINA_UNUSED, sc = _add_multibuttonentry(ly); elm_object_part_content_set(ly, "multibuttonentry", sc); - bx = _add_buttons(ly); - elm_object_part_content_set(ly, "box", bx); - evas_object_resize(win, 320, 480); evas_object_show(win); } diff --git a/legacy/elementary/src/lib/elc_multibuttonentry.c b/legacy/elementary/src/lib/elc_multibuttonentry.c index 634e016196..4d163c2720 100644 --- a/legacy/elementary/src/lib/elc_multibuttonentry.c +++ b/legacy/elementary/src/lib/elc_multibuttonentry.c @@ -55,6 +55,15 @@ _elm_multibuttonentry_smart_translate(Eo *obj EINA_UNUSED, void *_pd, va_list *l if (ret) *ret = EINA_TRUE; } +static char * +_format_count(int count, void *data EINA_UNUSED) +{ + char buf[32]; + + if (!snprintf(buf, sizeof(buf), "... + %d", count)) return NULL; + return strdup(buf); +} + static void _elm_multibuttonentry_smart_theme(Eo *obj, void *_pd, va_list *list) { @@ -173,7 +182,7 @@ _shrink_mode_set(Evas_Object *obj, EINA_LIST_FOREACH(sd->items, l, item) { Evas_Coord w_label_count = 0, h = 0; - char buf[MAX_STR]; + char *buf; elm_box_pack_end(sd->box, item->button); evas_object_show(item->button); @@ -185,8 +194,14 @@ _shrink_mode_set(Evas_Object *obj, if (count > 0) { - snprintf(buf, sizeof(buf), "... + %i", count); - edje_object_part_text_escaped_set(sd->end, "elm.text", buf); + buf = sd->format_func(count, (void *)sd->format_func_data); + if (buf) + { + edje_object_part_text_escaped_set + (sd->end, "elm.text", buf); + free(buf); + } + edje_object_size_min_calc(sd->end, &w_label_count, NULL); elm_coords_finger_size_adjust(1, &w_label_count, 1, NULL); } @@ -196,10 +211,16 @@ _shrink_mode_set(Evas_Object *obj, elm_box_unpack(sd->box, item->button); evas_object_hide(item->button); item->visible = EINA_FALSE; - count++; - snprintf(buf, sizeof(buf), "... + %d", count); - edje_object_part_text_escaped_set(sd->end, "elm.text", buf); + + buf = sd->format_func(count, (void *)sd->format_func_data); + if (buf) + { + edje_object_part_text_escaped_set + (sd->end, "elm.text", buf); + free(buf); + } + edje_object_size_min_calc(sd->end, &w_label_count, &h); elm_coords_finger_size_adjust(1, &w_label_count, 1, &h); evas_object_size_hint_min_set @@ -1478,6 +1499,7 @@ _elm_multibuttonentry_smart_add(Eo *obj, void *_pd, va_list *list EINA_UNUSED) priv->last_btn_select = EINA_TRUE; priv->editable = EINA_TRUE; priv->parent = obj; + priv->format_func = _format_count; _view_init(obj, priv); _callbacks_register(obj); @@ -1665,6 +1687,32 @@ _expanded_get(Eo *obj EINA_UNUSED, void *_pd, va_list *list) EINA_FALSE : EINA_TRUE; } +EAPI void +elm_multibuttonentry_format_function_set(Evas_Object *obj, + Elm_Multibuttonentry_Format_Cb f_func, + const void *data) +{ + ELM_MULTIBUTTONENTRY_CHECK(obj); + + eo_do((Eo *) obj, elm_obj_multibuttonentry_format_function_set + (f_func, data)); + +} + +static void +_format_function_set(Eo *obj EINA_UNUSED, void *_pd, va_list *list) +{ + Elm_Multibuttonentry_Smart_Data *sd = _pd; + + sd->format_func = va_arg(*list, Elm_Multibuttonentry_Format_Cb); + + if (!sd->format_func) sd->format_func = _format_count; + + sd->format_func_data = va_arg(*list, void *); + + _view_update(sd); +} + EAPI void elm_multibuttonentry_expanded_set(Evas_Object *obj, Eina_Bool expanded) @@ -2158,6 +2206,7 @@ _class_constructor(Eo_Class *klass) EO_OP_FUNC(ELM_OBJ_MULTIBUTTONENTRY_ID(ELM_OBJ_MULTIBUTTONENTRY_SUB_ID_ITEM_FILTER_APPEND), _item_filter_append), EO_OP_FUNC(ELM_OBJ_MULTIBUTTONENTRY_ID(ELM_OBJ_MULTIBUTTONENTRY_SUB_ID_ITEM_FILTER_PREPEND), _item_filter_prepend), EO_OP_FUNC(ELM_OBJ_MULTIBUTTONENTRY_ID(ELM_OBJ_MULTIBUTTONENTRY_SUB_ID_ITEM_FILTER_REMOVE), _item_filter_remove), + EO_OP_FUNC(ELM_OBJ_MULTIBUTTONENTRY_ID(ELM_OBJ_MULTIBUTTONENTRY_SUB_ID_FORMAT_FUNCTION_SET), _format_function_set), EO_OP_FUNC_SENTINEL }; eo_class_funcs_set(klass, func_desc); @@ -2185,6 +2234,7 @@ static const Eo_Op_Description op_desc[] = { EO_OP_DESCRIPTION(ELM_OBJ_MULTIBUTTONENTRY_SUB_ID_ITEM_FILTER_APPEND, "Append an item filter function for text inserted in the Multibuttonentry."), EO_OP_DESCRIPTION(ELM_OBJ_MULTIBUTTONENTRY_SUB_ID_ITEM_FILTER_PREPEND, "Prepend a filter function for text inserted in the Multibuttonentry."), EO_OP_DESCRIPTION(ELM_OBJ_MULTIBUTTONENTRY_SUB_ID_ITEM_FILTER_REMOVE, "Remove a filter from the list."), + EO_OP_DESCRIPTION(ELM_OBJ_MULTIBUTTONENTRY_SUB_ID_FORMAT_FUNCTION_SET, "Set a function to format the string that will be used to display the hidden items counter."), EO_OP_DESCRIPTION_SENTINEL }; static const Eo_Class_Description class_desc = { diff --git a/legacy/elementary/src/lib/elc_multibuttonentry_common.h b/legacy/elementary/src/lib/elc_multibuttonentry_common.h index ff76c537e4..f7ff17f96b 100644 --- a/legacy/elementary/src/lib/elc_multibuttonentry_common.h +++ b/legacy/elementary/src/lib/elc_multibuttonentry_common.h @@ -13,3 +13,21 @@ */ typedef Eina_Bool (*Elm_Multibuttonentry_Item_Filter_Cb)(Evas_Object *obj, const char *item_label, void *item_data, void *data); +/** + * @typedef Elm_Multibuttonentry_Format_Cb + * + * This callback type is used to format the string that will be used + * to display the hidden items counter, when not in expanded mode. + * + * @param count Number of hidden items + * @param data The (context) data passed in to + * elm_multibuttonentry_format_function_set() + * @return String representing the counter that will be set to + * multibuttonentry's counter item's text. + * + * @see elm_multibuttonentry_format_function_set() + * @see elm_multibuttonentry_expanded_set() + * + * @ingroup Multibuttonentry + */ +typedef char * (*Elm_Multibuttonentry_Format_Cb)(int count, void *data); diff --git a/legacy/elementary/src/lib/elc_multibuttonentry_eo.h b/legacy/elementary/src/lib/elc_multibuttonentry_eo.h index 8813548dd2..81f583083a 100644 --- a/legacy/elementary/src/lib/elc_multibuttonentry_eo.h +++ b/legacy/elementary/src/lib/elc_multibuttonentry_eo.h @@ -23,6 +23,7 @@ enum ELM_OBJ_MULTIBUTTONENTRY_SUB_ID_ITEM_FILTER_APPEND, ELM_OBJ_MULTIBUTTONENTRY_SUB_ID_ITEM_FILTER_PREPEND, ELM_OBJ_MULTIBUTTONENTRY_SUB_ID_ITEM_FILTER_REMOVE, + ELM_OBJ_MULTIBUTTONENTRY_SUB_ID_FORMAT_FUNCTION_SET, ELM_OBJ_MULTIBUTTONENTRY_SUB_ID_LAST }; @@ -282,3 +283,22 @@ enum * @ingroup Multibuttonentry */ #define elm_obj_multibuttonentry_item_filter_remove(func, data) ELM_OBJ_MULTIBUTTONENTRY_ID(ELM_OBJ_MULTIBUTTONENTRY_SUB_ID_ITEM_FILTER_REMOVE), EO_TYPECHECK(Elm_Multibuttonentry_Item_Filter_Cb, func), EO_TYPECHECK(void *, data) + +/** + * @def elm_obj_multibuttonentry_format_function_set + * @since 1.9 + * + * Set a function to format the string that will be used to display + * the hidden items counter. + * + * @param[in] format_function The actual format function + * @param[in] data User data to passed to @a format_function + * + * If @a format_function is @c NULL, the default format will be used, + * which is @c "... + %d". + * + * @see elm_multibuttonentry_format_function_set + * + * @ingroup Multibuttonentry + */ +#define elm_obj_multibuttonentry_format_function_set(format_function, data) ELM_OBJ_MULTIBUTTONENTRY_ID(ELM_OBJ_MULTIBUTTONENTRY_SUB_ID_FORMAT_FUNCTION_SET), EO_TYPECHECK(Elm_Multibuttonentry_Format_Cb, format_function), EO_TYPECHECK(const void *, data) diff --git a/legacy/elementary/src/lib/elc_multibuttonentry_legacy.h b/legacy/elementary/src/lib/elc_multibuttonentry_legacy.h index 2a3943217e..02849ecc97 100644 --- a/legacy/elementary/src/lib/elc_multibuttonentry_legacy.h +++ b/legacy/elementary/src/lib/elc_multibuttonentry_legacy.h @@ -245,6 +245,27 @@ EAPI void elm_multibuttonentry_item_filter_prepend(Evas_Ob */ EAPI void elm_multibuttonentry_item_filter_remove(Evas_Object *obj, Elm_Multibuttonentry_Item_Filter_Cb func, void *data); +/** + * Set a function to format the string that will be used to display + * the hidden items counter. + * + * @param[in] obj The multi button entry to get format function changed + * @param[in] format_function The actual format function + * @param[in] data User data to passed to @a format_function + * + * If @a format_function is @c NULL, the default format will be used, + * which is @c "... + %d". + * + * @see elm_multibuttonentry_format_function_set + * @since 1.9 + * + * @ingroup Multibuttonentry + */ +EAPI void +elm_multibuttonentry_format_function_set(Evas_Object *obj, + Elm_Multibuttonentry_Format_Cb f_func, + const void *data); + /** * Sets if the multibuttonentry is to be editable or not. * diff --git a/legacy/elementary/src/lib/elm_widget_multibuttonentry.h b/legacy/elementary/src/lib/elm_widget_multibuttonentry.h index 62ce0977df..428cafbdc6 100644 --- a/legacy/elementary/src/lib/elm_widget_multibuttonentry.h +++ b/legacy/elementary/src/lib/elm_widget_multibuttonentry.h @@ -82,6 +82,9 @@ struct _Elm_Multibuttonentry_Smart_Data Eina_List *filter_list; Elm_Object_Item *selected_it; /* selected item */ + Elm_Multibuttonentry_Format_Cb format_func; + const void *format_func_data; + const char *label_str, *guide_text_str; int n_str;