Bundles / src / view /

view_list.c

  1. /*
  2. * Copyright (c) 2016 Samsung Electronics Co., Ltd
  3. *
  4. * Licensed under the Flora License, Version 1.1 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://floralicense.org/license/
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16.  
  17. #include <app.h>
  18. #include <efl_extension.h>
  19. #include "$(appName).h"
  20. #include "view/view_list.h"
  21.  
  22. static void _tree_item_contracted(void *data, Evas_Object *o, void *event_info);
  23. static void _selection_status_changed_cb(void *data, Evas_Object *obj, void *event_info);
  24.  
  25. /**
  26. * @brief Creates a genlist object.
  27. * @param[in] parent The genlist's parent.
  28. * @return The created genlist or NULL on error.
  29. */
  30. Evas_Object *view_list_create(Evas_Object *parent)
  31. {
  32. Evas_Object *list = elm_genlist_add(parent);
  33. if (!list) {
  34. dlog_print(DLOG_ERROR, LOG_TAG, "[%s:%d] list == NULL", __FILE__, __LINE__);
  35. return NULL;
  36. }
  37.  
  38. evas_object_smart_callback_add(list, "selected", _selection_status_changed_cb, (void*)EINA_TRUE);
  39. elm_scroller_gravity_set(list, 0.0, 1.0);
  40.  
  41. return list;
  42. }
  43.  
  44. /**
  45. * @brief Adds callback functions to a genlist that are used when an item is extended or contracted.
  46. * @param[in] list The genlist.
  47. * @param[in] itc_parent The parent item class (the expanding or contracting item).
  48. * @param[in] itc_child The sub object of the parent object.
  49. * @param[in] expand_callback Callback invoked when an item was expanded.
  50. * @param[in] contract_callback Callback invoked when an item was contracted.
  51. * @note If the contract callback is not provided a default one will be used. If a callback is provided it has to have the following code:
  52. * @code Elm_Object_Item *it_parent = (Elm_Object_Item*) event_info;
  53. elm_genlist_item_subitems_clear(it_parent);
  54. */
  55. void view_list_add_tree_callbacks(Evas_Object *list, Elm_Gen_Item_Class *itc_parent, Elm_Gen_Item_Class *itc_child, Evas_Smart_Cb expand_callback, Evas_Smart_Cb contract_callback)
  56. {
  57. if (!expand_callback) {
  58. dlog_print(DLOG_ERROR, LOG_TAG, "[%s:%d] expand_callback == NULL", __FILE__, __LINE__);
  59. return;
  60. }
  61.  
  62. evas_object_smart_callback_add(list, "expanded", expand_callback, itc_child);
  63.  
  64. if (contract_callback)
  65. evas_object_smart_callback_add(list, "contracted", contract_callback, NULL);
  66. else
  67. evas_object_smart_callback_add(list, "contracted", _tree_item_contracted, NULL);
  68. }
  69.  
  70. /**
  71. * @brief Creates a new item class.
  72. * @param[in] content_get_cb Callback invoked when a new item is going to be realized by the genlist.
  73. * @param[in] content_del_cb Callback invoked when a item is going to be deleted. If NULL is used a default callback will be invoked.
  74. * @return The newly created class or NULL on error.
  75. */
  76. Elm_Genlist_Item_Class *view_list_add_itc(Elm_Gen_Item_Content_Get_Cb content_get_cb, Elm_Gen_Item_Del_Cb content_del_cb)
  77. {
  78. Elm_Genlist_Item_Class *itc = elm_genlist_item_class_new();
  79. if (!itc) {
  80. dlog_print(DLOG_ERROR, LOG_TAG, "[%s:%d] (*itc == NULL", __FILE__, __LINE__);
  81. return false;
  82. }
  83. itc->item_style = "full";
  84. itc->func.text_get = NULL;
  85. itc->func.content_get = content_get_cb;
  86. itc->func.state_get = NULL;
  87. itc->func.del = content_del_cb;
  88.  
  89. return itc;
  90. }
  91.  
  92. /**
  93. * @brief Creates a new item and adds it to the genlist.
  94. * @param[in] list The genlist.
  95. * @param[in] itc The item class to use.
  96. * @param[in] item_type Item type (NONE, TREE, GROUP).
  97. * @param[in] parent_item The parent item.
  98. * @param[in] data The data that will be passed to the content_get of the item class.
  99. * @param[in] add_first True - The item is going to be prepended. False - The item is going to be appended.
  100. * @param[in] mode The selection mode used by the newly created item.
  101. * @param[in] func Callback function invoked when the item is selected.
  102. * @param[in] func_data Data passed to the func callback function.
  103. * @return The newly created list item.
  104. */
  105. Elm_Object_Item *view_list_add_item(Evas_Object *list, Elm_Genlist_Item_Class *itc,
  106. Elm_Genlist_Item_Type item_type, Elm_Object_Item *parent_item,
  107. void *data, bool add_first,
  108. Elm_Object_Select_Mode mode,
  109. Evas_Smart_Cb func, const void *func_data)
  110. {
  111. Elm_Object_Item *item = NULL;
  112.  
  113. if (add_first)
  114. item = elm_genlist_item_prepend(list, itc, data, parent_item, item_type, func, func_data);
  115. else
  116. item = elm_genlist_item_append(list, itc, data, parent_item, item_type, func, func_data);
  117.  
  118. if (!item) {
  119. dlog_print(DLOG_ERROR, LOG_TAG, "[%s:%d] item == NULL", __FILE__, __LINE__);
  120. return NULL;
  121. }
  122.  
  123. elm_genlist_item_select_mode_set(item, mode);
  124.  
  125. return item;
  126. }
  127.  
  128. /**
  129. * @brief Updates the realized items.
  130. * @param[in] list The list to update.
  131. */
  132. void view_list_update(Evas_Object *list)
  133. {
  134. if (!list) {
  135. dlog_print(DLOG_ERROR, LOG_TAG, "[%s:%d] list == NULL", __FILE__, __LINE__);
  136. return;
  137. }
  138.  
  139. elm_genlist_realized_items_update(list);
  140. }
  141.  
  142. /**
  143. * @brief Updates a selected item of the genlist.
  144. * @param[in] item The item to update.
  145. */
  146. void view_list_item_update(Elm_Object_Item *item)
  147. {
  148. if (!item) {
  149. dlog_print(DLOG_ERROR, LOG_TAG, "[%s:%d] item == NULL", __FILE__, __LINE__);
  150. return;
  151. }
  152.  
  153. elm_genlist_item_update(item);
  154. }
  155.  
  156. /**
  157. * @brief Deletes an item from the genlist.
  158. * @param[in] list The genlist.
  159. * @param[in] item The item to remove.
  160. */
  161. void view_list_del_item(Evas_Object *list, Elm_Object_Item *item)
  162. {
  163. if (!list) {
  164. dlog_print(DLOG_ERROR, LOG_TAG, "[%s:%d] list == NULL", __FILE__, __LINE__);
  165. return;
  166. }
  167.  
  168. if (!item) {
  169. dlog_print(DLOG_ERROR, LOG_TAG, "[%s:%d] item == NULL", __FILE__, __LINE__);
  170. return;
  171. }
  172.  
  173. elm_object_item_del(item);
  174. }
  175.  
  176. /**
  177. * @brief Default callback function invoked when a genlist's item is going to be deleted.
  178. * @param[in] data The data added to the item.
  179. * @param[in] obj The genlist object.
  180. */
  181. void view_list_del_item_cb(void *data, Evas_Object *obj)
  182. {
  183. if (data)
  184. free(data);
  185. }
  186.  
  187. /**
  188. * @brief Internal default callback function invoked when a TREE item was contracted.
  189. * @param[in] data User data.
  190. * @param[in] obj The genlist.
  191. * @param[in] event_info The contracted item.
  192. */
  193. static void _tree_item_contracted(void *data, Evas_Object *obj, void *event_info)
  194. {
  195. Elm_Object_Item *it_parent = (Elm_Object_Item*) event_info;
  196. elm_genlist_item_subitems_clear(it_parent);
  197. }
  198.  
  199. /**
  200. * @brief Internal callback function invoked when a TREE item is selected. It is used to change the 'expanded' state of the item.
  201. * @param[in] data User data.
  202. * @param[in] obj The genlist.
  203. * @param[in] event_info The selected item.
  204. */
  205. static void _selection_status_changed_cb(void *data, Evas_Object *obj, void *event_info)
  206. {
  207. Elm_Object_Item *item = (Elm_Object_Item *)event_info;
  208.  
  209. if (elm_genlist_item_type_get(item) == ELM_GENLIST_ITEM_TREE) {
  210. elm_genlist_item_selected_set(item, EINA_FALSE);
  211. elm_genlist_item_expanded_set(item, !elm_genlist_item_expanded_get(item));
  212. }
  213. }