Bundles / src /

view.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_defines.h"
  21. #include "view.h"
  22. #include "view/view_common.h"
  23. #include "view/view_source.h"
  24. #include "view/view_sink.h"
  25.  
  26. #define CHAR_BUFFER_SIZE 256
  27.  
  28. static struct view_info {
  29. Evas_Object *win;
  30. Evas_Object *conform;
  31. Evas_Object *scroller;
  32. Evas_Object *layout;
  33. } s_info = {
  34. .win = NULL,
  35. .conform = NULL,
  36. .scroller = NULL,
  37. .layout = NULL,
  38. };
  39.  
  40. static void _delete_win_request_cb(void *data, Evas_Object *obj, void *event_info);
  41. static bool _create_main_view(void);
  42. static void _switch_view_cb(void *data, Evas_Object *obj, const char *emission, const char *source);
  43. static bool _create_scroller(void);
  44. static void _back_button_clicked_cb(void *data, Evas_Object *obj, void *event_info);
  45.  
  46. /**
  47. * @brief Creates essential objects: window, conformant and layout.
  48. * @param[in] user_data User data.
  49. * @return EINA_TRUE on success or EINA_FALSE on error.
  50. */
  51. Eina_Bool view_create(void *user_data)
  52. {
  53. /* Create the window */
  54. s_info.win = view_create_win(PACKAGE);
  55. if (s_info.win == NULL) {
  56. dlog_print(DLOG_ERROR, LOG_TAG, "failed to create a window.");
  57. return EINA_FALSE;
  58. }
  59.  
  60. /* Create the conformant */
  61. s_info.conform = view_create_conformant(s_info.win);
  62. if (s_info.conform == NULL)
  63. return EINA_FALSE;
  64.  
  65. if (!_create_scroller()) {
  66. dlog_print(DLOG_ERROR, LOG_TAG, "[%s:%d] Function _create_scroller() failed", __FILE__, __LINE__);
  67. return EINA_FALSE;
  68. }
  69.  
  70. if (!_create_main_view())
  71. return EINA_FALSE;
  72.  
  73. if (!view_source_create(s_info.layout))
  74. return EINA_FALSE;
  75.  
  76. if (!view_sink_create(s_info.layout))
  77. return EINA_FALSE;
  78.  
  79. elm_layout_content_set(s_info.layout, PART_VIEW, view_source_get());
  80.  
  81. evas_object_show(s_info.win);
  82. return EINA_TRUE;
  83. }
  84.  
  85. /**
  86. * @brief Creates a basic window named package
  87. * @param[in] pkg_name Name of the window
  88. * @return The function returns window object if it was created successfully,
  89. * otherwise 'NULL' is returned.
  90. */
  91. Evas_Object *view_create_win(const char *pkg_name)
  92. {
  93. Evas_Object *win = NULL;
  94.  
  95. /*
  96. * Window
  97. * Create and initialize elm_win.
  98. * elm_win is mandatory to manipulate the window.
  99. */
  100. win = elm_win_util_standard_add(pkg_name, pkg_name);
  101. elm_win_conformant_set(win, EINA_TRUE);
  102. elm_win_indicator_mode_set(win, ELM_WIN_INDICATOR_SHOW);
  103. elm_win_indicator_opacity_set(win, ELM_WIN_INDICATOR_OPAQUE);
  104. elm_win_autodel_set(win, EINA_TRUE);
  105.  
  106. evas_object_smart_callback_add(win, "delete,request", _delete_win_request_cb, NULL);
  107. eext_object_event_callback_add(win, EEXT_CALLBACK_BACK, _back_button_clicked_cb, NULL);
  108.  
  109.  
  110. return win;
  111. }
  112.  
  113. /**
  114. * @brief Creates a conformant object for parent object.
  115. * @param[in] parent The parent object for conformant object.
  116. * @return The function returns conformant object if it was created successfully,
  117. * otherwise 'NULL' is returned.
  118. */
  119. Evas_Object *view_create_conformant(Evas_Object *parent)
  120. {
  121. Evas_Object *conform = NULL;
  122.  
  123. if (!parent) {
  124. dlog_print(DLOG_ERROR, LOG_TAG, "Wrong input arguments.");
  125. return NULL;
  126. }
  127.  
  128. conform = elm_conformant_add(parent);
  129. if (!conform) {
  130. dlog_print(DLOG_ERROR, LOG_TAG, "Function elm_conformant_add() failed.");
  131. return NULL;
  132. }
  133.  
  134. evas_object_size_hint_weight_set(conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
  135. elm_win_resize_object_add(parent, conform);
  136. elm_object_signal_emit(conform, "elm,state,indicator,overlap", "elm");
  137. evas_object_show(conform);
  138.  
  139. return conform;
  140. }
  141.  
  142. /**
  143. * @brief Internal function that creates a scroller and ads it to the conformant.
  144. * @return true on success or false on fail.
  145. */
  146. static bool _create_scroller(void)
  147. {
  148. s_info.scroller = elm_scroller_add(s_info.conform);
  149. if (!s_info.scroller) {
  150. dlog_print(DLOG_ERROR, LOG_TAG, "[%s:%d] s_info.scroller == NULL", __FILE__, __LINE__);
  151. return false;
  152. }
  153.  
  154. elm_scroller_bounce_set(s_info.scroller, EINA_FALSE, EINA_TRUE);
  155. elm_scroller_policy_set(s_info.scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
  156. elm_scroller_movement_block_set(s_info.scroller, ELM_SCROLLER_MOVEMENT_BLOCK_VERTICAL);
  157.  
  158. elm_object_content_set(s_info.conform, s_info.scroller);
  159. return true;
  160. }
  161.  
  162. /**
  163. * @brief Internal function that creates the main view layout.
  164. * @return true on success or false on fail.
  165. */
  166. static bool _create_main_view(void)
  167. {
  168. s_info.layout = view_create_layout(s_info.conform, EDJ_MAIN_FILE_NAME, GROUP_MAIN);
  169. if (!s_info.layout) {
  170. dlog_print(DLOG_ERROR, LOG_TAG, "[%s:%d] s_info.layout == NULL", __FILE__, __LINE__);
  171. return false;
  172. }
  173.  
  174. elm_layout_signal_callback_add(s_info.layout, SIGNAL_SWITCH_SOURCE_VIEW, "", _switch_view_cb, (void *)false);
  175. elm_layout_signal_callback_add(s_info.layout, SIGNAL_SWITCH_SINK_VIEW, "", _switch_view_cb, (void *)true);
  176. evas_object_size_hint_weight_set(s_info.layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
  177.  
  178. elm_object_content_set(s_info.scroller, s_info.layout);
  179. return true;
  180. }
  181.  
  182. /**
  183. * @brief Destroys window and frees its resources.
  184. */
  185. void view_destroy(void)
  186. {
  187. if (s_info.win == NULL)
  188. return;
  189.  
  190. view_source_finalize();
  191. evas_object_del(s_info.win);
  192. }
  193.  
  194. /**
  195. * @brief Internal callback function invoked when the main window needs to be destroyed.
  196. * @param[in] data The user data passed to the evas_object_smart_callback_add() function.
  197. * @param[in] obj The object invoking this callback function.
  198. * @param[in] event_info The structure containing the information on this event.
  199. */
  200. static void _delete_win_request_cb(void *data, Evas_Object *obj, void *event_info)
  201. {
  202. ui_app_exit();
  203. }
  204.  
  205. /**
  206. * @brief Internal callback function invoked when the user clicks on one of the top buttons. It is used to switch the displayed view.
  207. * @param[in] data The user data (true - switch to sink view, false switch to source view).
  208. * @param[in] obj The main layout.
  209. * @param[in] emission The emitted signal.
  210. * @param[in] source The signal's source.
  211. */
  212. static void _switch_view_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
  213. {
  214. bool to_sink = (bool)data;
  215.  
  216. Evas_Object *content = elm_layout_content_get(s_info.layout, PART_VIEW);
  217. Evas_Object *switch_to = NULL;
  218.  
  219. if (content) {
  220. elm_layout_content_unset(s_info.layout, PART_VIEW);
  221. evas_object_hide(content);
  222. }
  223.  
  224. if (to_sink)
  225. switch_to = view_sink_get();
  226. else
  227. switch_to = view_source_get();
  228.  
  229. elm_layout_content_set(s_info.layout, PART_VIEW, switch_to);
  230. }
  231.  
  232. /**
  233. * @brief Internal callback function invoked when the back button is pressed. It is used to minimalize the application's window.
  234. * @param[in] data User data
  235. * @param[in] obj The window object.
  236. * @param[in] event_info Event information.
  237. */
  238. static void _back_button_clicked_cb(void *data, Evas_Object *obj, void *event_info)
  239. {
  240. elm_win_lower(s_info.win);
  241. }