World Clock Widget / src /

view.c

  1. /*
  2. * Copyright (c) 2015 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 <tizen.h>
  18. #include <widget_app.h>
  19. #include <widget_app_efl.h>
  20. #include <dlog.h>
  21. #include "log.h"
  22. #include "view.h"
  23.  
  24. #define FILE_CONTENT "edje/content.edj"
  25. #define GROUP_NAME "content"
  26.  
  27. static Evas_Object *_create_win(widget_context_h context, int w, int h);
  28. static Evas_Object *_create_content(Evas_Object *win);
  29. static void _get_resource(const char *file_in, char *path_out, int path_max);
  30. static void _widget_pressed_cb(void *data, Evas *evas, Evas_Object *obj, void *event_info);
  31. static void _widget_released_cb(void *data, Evas *evas, Evas_Object *obj, void *event_info);
  32.  
  33. /*
  34. * @brief: Set image to given part
  35. * @param[user_data]: data needed in this function (widget_instance_data)
  36. * @param[part]: part name to which you want to set this image
  37. * @param[file_name]: name of image file
  38. */
  39. void view_set_image(void *user_data, const char *part, const char *file_name)
  40. {
  41. widget_instance_data_s *wid = NULL;
  42. Evas_Object *img = NULL;
  43.  
  44. wid = user_data;
  45. if (wid == NULL) {
  46. dlog_print(DLOG_ERROR, LOG_TAG, "failed to widget data.");
  47. return;
  48. }
  49.  
  50. char full_path[PATH_MAX] = {0, };
  51.  
  52. img = elm_image_add(wid->win);
  53. if (img == NULL) {
  54. dlog_print(DLOG_ERROR, LOG_TAG, "failed to add a image.");
  55. return;
  56. }
  57.  
  58. _get_resource(file_name, full_path, sizeof(full_path));
  59. elm_image_file_set(img, full_path, NULL);
  60. evas_object_size_hint_weight_set(img, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
  61. elm_win_resize_object_add(wid->win, img);
  62. evas_object_show(img);
  63.  
  64. elm_object_part_content_set(wid->content, part, img);
  65. }
  66.  
  67. /*
  68. * @brief: Set text to the part
  69. * @param[user_data]: data needed in this function (widget_instance_data)
  70. * @param[part]: Part name to which you want to set text
  71. * @param[text]: text you want to set to the part
  72. */
  73. void view_set_text(void *user_data, const char *part, const char *text)
  74. {
  75. widget_instance_data_s *wid = NULL;
  76.  
  77. wid = user_data;
  78. if (wid == NULL) {
  79. dlog_print(DLOG_ERROR, LOG_TAG, "failed to get widget data.");
  80. return;
  81. }
  82.  
  83. elm_object_part_text_set(wid->content, part, text);
  84. }
  85.  
  86. /*
  87. * @brief: Create essential object window, content and so on
  88. * @param[context]: context for widget instance
  89. * @param[w]: basic size of width for widget
  90. * @param[h]: basic size of height for widget
  91. */
  92. widget_instance_data_s *view_create(widget_context_h context, int w, int h)
  93. {
  94.  
  95. widget_instance_data_s *wid = malloc(sizeof(widget_instance_data_s));
  96.  
  97. wid->win = _create_win(context, w, h);
  98. if (wid->win == NULL) {
  99. dlog_print(DLOG_ERROR, LOG_TAG, "failed to create a window.");
  100. free(wid);
  101. return NULL;
  102. }
  103.  
  104. wid->content = _create_content(wid->win);
  105. if (wid->content == NULL) {
  106. dlog_print(DLOG_ERROR, LOG_TAG, "failed to create a content.");
  107. evas_object_del(wid->win);
  108. free(wid);
  109. return NULL;
  110. }
  111.  
  112. wid->root_width = w;
  113. wid->root_height = h;
  114.  
  115. return wid;
  116. }
  117.  
  118. /*
  119. * @brief: Destroy window and free important data to finish this application
  120. * @param[user_data]: data needed in this function (widget_instance_data)
  121. */
  122. void view_destroy(void *user_data)
  123. {
  124. widget_instance_data_s *wid = NULL;
  125.  
  126. wid = user_data;
  127. if (wid == NULL) {
  128. dlog_print(DLOG_ERROR, LOG_TAG, "failed to destroy data.");
  129. return;
  130. }
  131.  
  132. evas_object_del(wid->win);
  133.  
  134. free(wid);
  135. }
  136.  
  137. /*
  138. * @brief: Changes the size of the given Evas object
  139. * @param[user_data]: data needed in this function (widget_instance_data)
  140. * @param[w]: width to change size of widget
  141. * @param[h]: height to change size of widget
  142. */
  143. void view_resize(void *user_data, int w, int h)
  144. {
  145. widget_instance_data_s *wid = NULL;
  146.  
  147. wid = user_data;
  148. if (wid == NULL) {
  149. dlog_print(DLOG_ERROR, LOG_TAG, "failed to widget data.");
  150. return;
  151. }
  152.  
  153. evas_object_resize(wid->win, w, h);
  154. }
  155.  
  156. /*
  157. * @brief: Set event callback to Evas Object that is a part of a given container widget
  158. * @param[user_data]: data needed in this function (widget_instance_data)
  159. * @param[part_name]: container's part name to get
  160. * @param[type]: widget event type
  161. * @param[func]: callback function
  162. */
  163. void view_set_event_callback(void *user_data, const char *part_name, widget_event_type type, void *func)
  164. {
  165. widget_instance_data_s *wid = NULL;
  166. Evas_Object *obj = NULL;
  167.  
  168. wid = user_data;
  169. if (wid == NULL) {
  170. dlog_print(DLOG_ERROR, LOG_TAG, "failed to widget data.");
  171. return;
  172. }
  173.  
  174. obj = elm_object_part_content_get(wid->content, part_name);
  175.  
  176. if (!obj) {
  177. obj = (Evas_Object*) edje_object_part_object_get(elm_layout_edje_get(wid->content), part_name);
  178. }
  179.  
  180. evas_object_data_set(obj, "callback_func_addr", (void*) func);
  181. evas_object_data_set(obj, "widget_event_type", (void*) type);
  182.  
  183. if (type == WIDGET_EVENT_PRESSED) {
  184. evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_DOWN, _widget_pressed_cb, wid);
  185. } else if ((type == WIDGET_EVENT_RELEASED) || (type == WIDGET_EVENT_CLICKED)) {
  186. evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP, _widget_released_cb, wid);
  187. }
  188. }
  189.  
  190.  
  191. /*
  192. * @note
  193. * Below functions are static functions.
  194. */
  195.  
  196. /*
  197. * @brief: Get path of resource.
  198. * @param[file_in]: file name
  199. * @param[path_out]: the point to which save full path of the resource
  200. * @param[path_max]: size of file name include path
  201. */
  202. static void _get_resource(const char *file_in, char *path_out, int path_max)
  203. {
  204. char *res_path = app_get_resource_path();
  205. if (res_path) {
  206. snprintf(path_out, path_max, "%s/%s", res_path, file_in);
  207. free(res_path);
  208. }
  209. }
  210.  
  211. /*
  212. * @brief: Create window for widget
  213. * @param[context]: context for widget instance
  214. * @param[w]: basic size of width for widget
  215. * @param[h]: basic size of height for widget
  216. */
  217. static Evas_Object *_create_win(widget_context_h context, int w, int h)
  218. {
  219. Evas_Object *win = NULL;
  220. int ret;
  221.  
  222. if (context == NULL) {
  223. dlog_print(DLOG_ERROR, LOG_TAG, "failed to get context.");
  224. return NULL;
  225. }
  226.  
  227. ret = widget_app_get_elm_win(context, &win);
  228. if (ret != WIDGET_ERROR_NONE) {
  229. dlog_print(DLOG_ERROR, LOG_TAG, "failed to get window. err = %d", ret);
  230. return NULL;
  231. }
  232. evas_object_resize(win, w, h);
  233. evas_object_show(win);
  234.  
  235. return win;
  236. }
  237.  
  238. /*
  239. * @brief: Make a content(layout) with edje file
  240. * @param[win]: The object to which you want to add this layout
  241. */
  242. static Evas_Object *_create_content(Evas_Object *win)
  243. {
  244. Evas_Object *content = NULL;
  245. char full_path[PATH_MAX] = { 0, };
  246.  
  247. content = elm_layout_add(win);
  248. if (content == NULL) {
  249. dlog_print(DLOG_ERROR, LOG_TAG, "failed to add a content.");
  250. return NULL;
  251. }
  252.  
  253. _get_resource(FILE_CONTENT, full_path, sizeof(full_path));
  254. elm_layout_file_set(content, full_path, GROUP_NAME);
  255. evas_object_size_hint_weight_set(content, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
  256. elm_win_resize_object_add(win, content);
  257. evas_object_show(content);
  258.  
  259. return content;
  260. }
  261.  
  262. /*
  263. * @brief: This function will be operated when the widget is pressed
  264. * @param[data]: data will be the same value passed to evas_object_event_callback_add() as the data parameter
  265. * @param[evas]: e lets the user know what evas canvas the event occurred on
  266. * @param[obj]: object handles on which the event occurred
  267. * @param[event_info]: event_info is a pointer to a data structure about event
  268. */
  269. static void _widget_pressed_cb(void *data, Evas *evas, Evas_Object *obj, void *event_info)
  270. {
  271. widget_instance_data_s *wid = data;
  272. if (!wid) {
  273. return;
  274. }
  275.  
  276. widget_event_cb func = evas_object_data_get(obj, "callback_func_addr");
  277.  
  278. func(wid);
  279. }
  280.  
  281. /*
  282. * @brief: This function will be operated when the widget is released
  283. * @param[data]: data will be the same value passed to evas_object_event_callback_add() as the data parameter
  284. * @param[evas]: e lets the user know what evas canvas the event occurred on
  285. * @param[obj]: object handles on which the event occurred
  286. * @param[event_info]: event_info is a pointer to a data structure about event
  287. */
  288. static void _widget_released_cb(void *data, Evas *evas, Evas_Object *obj, void *event_info)
  289. {
  290. widget_instance_data_s *wid = data;
  291. Evas_Event_Mouse_Up *ev = event_info;
  292. Evas_Coord x, y, w, h;
  293.  
  294. if (!wid) {
  295. return;
  296. }
  297.  
  298. widget_event_cb func = (widget_event_cb) evas_object_data_get(obj, "callback_func_addr");
  299. widget_event_type type = (widget_event_type) evas_object_data_get(obj, "widget_event_type");
  300.  
  301. if (type == WIDGET_EVENT_RELEASED) {
  302. if (func) {
  303. func(wid);
  304. }
  305. } else if (type == WIDGET_EVENT_CLICKED) {
  306. evas_object_geometry_get(obj, &x, &y, &w, &h);
  307.  
  308. if (!ELM_RECTS_POINT_OUT(x, y, w, h, ev->canvas.x, ev->canvas.y)) {
  309. if (func) {
  310. func(wid);
  311. }
  312. }
  313. }
  314. }
  315.  
  316. /* End of file */