1 #include <Elementary.h>
4 #include "els_scroller.h"
6 typedef struct _Widget_Data Widget_Data;
14 Ecore_Timer *longtimer;
16 #ifdef HAVE_ELEMENTARY_ETHUMB
27 Ethumb_Exists *exists;
29 Ecore_Event_Handler *eeh;
31 Ethumb_Thumb_Format format;
39 #ifdef HAVE_ELEMENTARY_ETHUMB
40 static Eina_List *_elm_icon_retry = NULL;
41 static int _icon_pending_request = 0;
43 static void _icon_thumb_exists(void *data, Ethumb_Client *client __UNUSED__, Ethumb_Exists *thread, Eina_Bool exists);
44 static void _icon_thumb_stop(Widget_Data *wd, void *ethumbd);
47 static const char *widtype = NULL;
48 static void _del_hook(Evas_Object *obj);
49 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
50 static void _theme_hook(Evas_Object *obj);
51 static void _sizing_eval(Evas_Object *obj);
52 static void _mouse_up(void *data, Evas *e, Evas_Object *obj, void *event_info);
53 static void _mouse_move(void *data, Evas *e, Evas_Object *obj, void *event_info);
55 static const char SIG_CLICKED[] = "clicked";
56 static const char SIG_DRAG_START[] = "drag,start";
57 static const char SIG_DRAG_END[] = "drag,end";
59 static const Evas_Smart_Cb_Description _signals[] = {
68 _del_hook(Evas_Object *obj)
70 Widget_Data *wd = elm_widget_data_get(obj);
71 #ifdef HAVE_ELEMENTARY_ETHUMB
72 Ethumb_Client *ethumbd;
76 #ifdef HAVE_ELEMENTARY_ETHUMB
77 ethumbd = elm_thumb_ethumb_client_get();
78 _icon_thumb_stop(wd, ethumbd);
80 eina_stringshare_del(wd->thumb.file.path);
81 eina_stringshare_del(wd->thumb.file.key);
82 eina_stringshare_del(wd->thumb.thumb.path);
83 eina_stringshare_del(wd->thumb.thumb.key);
86 ecore_event_handler_del(wd->thumb.eeh);
93 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
95 Widget_Data *wd = elm_widget_data_get(obj);
97 edje_object_mirrored_set(wd->frm, rtl);
101 _theme_hook(Evas_Object *obj)
103 Widget_Data *wd = elm_widget_data_get(obj);
105 _elm_widget_mirrored_reload(obj);
106 _mirrored_set(wd->frm, elm_widget_mirrored_get(obj));
107 _elm_theme_object_set(obj, wd->frm, "photo", "base",
108 elm_widget_style_get(obj));
109 edje_object_part_swallow(wd->frm, "elm.swallow.content", wd->img);
110 edje_object_scale_set(wd->frm, elm_widget_scale_get(obj) *
116 _sizing_eval(Evas_Object *obj)
118 Widget_Data *wd = elm_widget_data_get(obj);
119 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
126 scale = (wd->size * elm_widget_scale_get(obj) * _elm_config->scale);
127 evas_object_size_hint_min_set(wd->img, scale, scale);
128 edje_object_part_swallow(wd->frm, "elm.swallow.content", wd->img);
129 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
130 edje_object_size_min_restricted_calc(wd->frm, &minw, &minh, minw, minh);
131 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
134 evas_object_size_hint_min_set(obj, minw, minh);
135 evas_object_size_hint_max_set(obj, maxw, maxh);
140 _icon_move_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
143 Widget_Data *wd = elm_widget_data_get(data);
148 Edje_Message_Int_Set *msg;
149 Evas_Object *icon = _els_smart_icon_object_get(wd->img);
151 evas_object_geometry_get(icon, NULL, NULL, &w, &h);
152 msg = alloca(sizeof(Edje_Message_Int_Set) + (sizeof(int)));
154 msg->val[0] = (int)w;
155 msg->val[1] = (int)h;
157 edje_object_message_send(wd->frm, EDJE_MESSAGE_INT_SET, 0, msg);
160 #ifdef HAVE_ELEMENTARY_ETHUMB
161 if (wd->thumb.file.path)
162 elm_photo_thumb_set(data, wd->thumb.file.path, wd->thumb.file.key);
168 _drag_done_cb(void *unused __UNUSED__, Evas_Object *obj)
170 elm_object_scroll_freeze_pop(obj);
171 evas_object_smart_callback_call(obj, SIG_DRAG_END, NULL);
175 _longpress(void *objv)
177 Widget_Data *wd = elm_widget_data_get(objv);
181 DBG("Long press: start drag!");
182 wd->longtimer = NULL; /* clear: must return NULL now */
183 evas_object_event_callback_del(objv, EVAS_CALLBACK_MOUSE_MOVE, _mouse_move);
185 tmp = _els_smart_icon_object_get(wd->img);
187 evas_object_image_file_get(tmp,&file,NULL);
191 /* FIXME: Deal with relative paths; use PATH_MAX */
192 snprintf(buf, sizeof(buf), "file://%s", file);
193 if (elm_drag_start(objv, ELM_SEL_FORMAT_IMAGE,
194 buf, _drag_done_cb, NULL))
196 elm_object_scroll_freeze_push(objv);
197 evas_object_smart_callback_call(objv, SIG_DRAG_START, NULL);
201 return 0; /* Don't call again */
205 _mouse_move(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event)
207 Widget_Data *wd = elm_widget_data_get(data);
208 Evas_Event_Mouse_Move *move = event;
213 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_MOVE, _mouse_move);
217 /* if the event is held, stop waiting */
218 if (move->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
220 /* Moved too far: No longpress for you! */
221 ecore_timer_del(wd->longtimer);
222 wd->longtimer = NULL;
223 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_MOVE,
229 _mouse_down(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
231 Widget_Data *wd = elm_widget_data_get(data);
233 if (wd->longtimer) ecore_timer_del(wd->longtimer);
235 /* FIXME: Hard coded timeout */
236 wd->longtimer = ecore_timer_add(0.7, _longpress, data);
237 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_MOVE,
242 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
244 Widget_Data *wd = elm_widget_data_get(data);
246 if ((wd) && (wd->longtimer))
248 ecore_timer_del(wd->longtimer);
249 wd->longtimer = NULL;
252 evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
256 _icon_size_min_get(Evas_Object *icon)
259 _els_smart_icon_size_get(icon, &size, NULL);
260 return (size < 32) ? 32 : size;
264 #ifdef HAVE_ELEMENTARY_ETHUMB
266 _icon_thumb_stop(Widget_Data *wd, void *ethumbd)
268 if (wd->thumb.id >= 0)
270 ethumb_client_generate_cancel(ethumbd, wd->thumb.id, NULL, NULL, NULL);
272 _icon_pending_request--;
275 if (wd->thumb.exists)
277 ethumb_client_thumb_exists_cancel(wd->thumb.exists);
278 wd->thumb.exists = NULL;
279 _icon_pending_request--;
284 _elm_icon_retry = eina_list_remove(_elm_icon_retry, wd);
285 wd->thumb.retry = EINA_FALSE;
290 _icon_thumb_display(Widget_Data *wd)
292 Eina_Bool ret = EINA_FALSE;
294 if (wd->thumb.format == ETHUMB_THUMB_EET)
296 static const char *extensions[] = {
297 ".avi", ".mp4", ".ogv", ".mov", ".mpg", ".wmv", NULL
299 const char **ext, *ptr;
301 Eina_Bool video = EINA_FALSE;
303 prefix_size = eina_stringshare_strlen(wd->thumb.file.path) - 4;
304 if (prefix_size >= 0)
306 ptr = wd->thumb.file.path + prefix_size;
307 for (ext = extensions; *ext; ++ext)
308 if (!strcasecmp(ptr, *ext))
316 ret = _els_smart_icon_file_edje_set(wd->img, wd->thumb.thumb.path, wd->thumb.thumb.key);
320 ret = _els_smart_icon_file_key_set(wd->img, wd->thumb.thumb.path, wd->thumb.thumb.key);
326 _icon_thumb_retry(Widget_Data *wd)
328 return _icon_thumb_display(wd);
332 _icon_thumb_cleanup(Ethumb_Client *ethumbd)
337 EINA_LIST_FOREACH_SAFE(_elm_icon_retry, l, ll, wd)
338 if (_icon_thumb_retry(wd))
340 _elm_icon_retry = eina_list_remove_list(_elm_icon_retry, l);
341 wd->thumb.retry = EINA_FALSE;
344 if (_icon_pending_request == 0)
345 EINA_LIST_FREE(_elm_icon_retry, wd)
346 _icon_thumb_stop(wd, ethumbd);
350 _icon_thumb_finish(Widget_Data *wd, Ethumb_Client *ethumbd)
352 const char *file = NULL, *group = NULL;
355 _els_smart_icon_file_get(wd->img, &file, &group);
356 file = eina_stringshare_ref(file);
357 group = eina_stringshare_ref(group);
359 ret = _icon_thumb_display(wd);
365 if (!wd->thumb.retry)
367 _elm_icon_retry = eina_list_append(_elm_icon_retry, wd);
368 wd->thumb.retry = EINA_TRUE;
371 /* Back to previous image */
372 if (((p = strrchr(file, '.'))) && (!strcasecmp(p, ".edj")))
373 _els_smart_icon_file_edje_set(wd->img, file, group);
375 _els_smart_icon_file_key_set(wd->img, file, group);
378 _icon_thumb_cleanup(ethumbd);
380 eina_stringshare_del(file);
381 eina_stringshare_del(group);
385 _icon_thumb_cb(void *data,
386 Ethumb_Client *ethumbd,
388 const char *file __UNUSED__,
389 const char *key __UNUSED__,
390 const char *thumb_path,
391 const char *thumb_key,
394 Widget_Data *wd = data;
396 EINA_SAFETY_ON_FALSE_RETURN(wd->thumb.id == id);
399 _icon_pending_request--;
403 eina_stringshare_replace(&wd->thumb.thumb.path, thumb_path);
404 eina_stringshare_replace(&wd->thumb.thumb.key, thumb_key);
405 wd->thumb.format = ethumb_client_format_get(ethumbd);
407 _icon_thumb_finish(wd, ethumbd);
411 ERR("could not generate thumbnail for %s (key: %s)", file, key);
412 _icon_thumb_cleanup(ethumbd);
417 _icon_thumb_exists(void *data, Ethumb_Client *client __UNUSED__, Ethumb_Exists *thread, Eina_Bool exists)
419 Widget_Data *wd = data;
420 Ethumb_Client *ethumbd;
422 if (ethumb_client_thumb_exists_check(thread))
425 wd->thumb.exists = NULL;
427 ethumbd = elm_thumb_ethumb_client_get();
431 const char *thumb_path, *thumb_key;
433 _icon_pending_request--;
434 ethumb_client_thumb_path_get(ethumbd, &thumb_path, &thumb_key);
435 eina_stringshare_replace(&wd->thumb.thumb.path, thumb_path);
436 eina_stringshare_replace(&wd->thumb.thumb.key, thumb_key);
437 wd->thumb.format = ethumb_client_format_get(ethumbd);
439 _icon_thumb_finish(wd, ethumbd);
441 else if ((wd->thumb.id = ethumb_client_generate(ethumbd, _icon_thumb_cb, wd, NULL)) == -1)
443 ERR("Generate was unable to start !");
444 /* Failed to generate thumbnail */
445 _icon_pending_request--;
450 _icon_thumb_apply(Widget_Data *wd)
452 Ethumb_Client *ethumbd;
454 ethumbd = elm_thumb_ethumb_client_get();
456 _icon_thumb_stop(wd, ethumbd);
458 if (!wd->thumb.file.path) return ;
460 _icon_pending_request++;
461 if (!ethumb_client_file_set(ethumbd, wd->thumb.file.path, wd->thumb.file.key)) return ;
462 ethumb_client_size_set(ethumbd, _icon_size_min_get(wd->img), _icon_size_min_get(wd->img));
463 wd->thumb.exists = ethumb_client_thumb_exists(ethumbd, _icon_thumb_exists, wd);
467 _icon_thumb_apply_cb(void *data, int type __UNUSED__, void *ev __UNUSED__)
469 Widget_Data *wd = data;
471 _icon_thumb_apply(wd);
472 return ECORE_CALLBACK_RENEW;
477 elm_photo_add(Evas_Object *parent)
484 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
486 ELM_SET_WIDTYPE(widtype, "photo");
487 elm_widget_type_set(obj, "photo");
488 elm_widget_sub_object_add(parent, obj);
489 elm_widget_data_set(obj, wd);
490 elm_widget_del_hook_set(obj, _del_hook);
491 elm_widget_theme_hook_set(obj, _theme_hook);
492 elm_widget_can_focus_set(obj, EINA_FALSE);
494 wd->frm = edje_object_add(e);
495 _elm_theme_object_set(obj, wd->frm, "photo", "base", "default");
496 elm_widget_resize_object_set(obj, wd->frm);
498 wd->img = _els_smart_icon_add(e);
499 _els_smart_icon_scale_up_set(wd->img, 1);
500 _els_smart_icon_scale_down_set(wd->img, 1);
501 _els_smart_icon_smooth_scale_set(wd->img, 1);
502 _els_smart_icon_fill_inside_set(wd->img, 0);
503 _els_smart_icon_scale_size_set(wd->img, 0);
504 wd->fill = EINA_FALSE;
505 _els_smart_icon_scale_set(wd->img,
506 elm_widget_scale_get(obj) * _elm_config->scale);
507 evas_object_event_callback_add(wd->img, EVAS_CALLBACK_MOUSE_UP,
509 evas_object_event_callback_add(wd->img, EVAS_CALLBACK_MOUSE_DOWN,
511 evas_object_repeat_events_set(wd->img, 1);
512 edje_object_part_swallow(wd->frm, "elm.swallow.content", wd->img);
513 evas_object_show(wd->img);
514 elm_widget_sub_object_add(obj, wd->img);
516 wd->longtimer = NULL;
518 icon = _els_smart_icon_object_get(wd->img);
519 evas_object_event_callback_add(icon, EVAS_CALLBACK_MOVE,
520 _icon_move_resize, obj);
521 evas_object_event_callback_add(icon, EVAS_CALLBACK_RESIZE,
522 _icon_move_resize, obj);
524 evas_object_smart_callbacks_descriptions_set(obj, _signals);
526 #ifdef HAVE_ELEMENTARY_ETHUMB
530 _mirrored_set(obj, elm_widget_mirrored_get(obj));
536 elm_photo_file_set(Evas_Object *obj, const char *file)
538 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
539 Widget_Data *wd = elm_widget_data_get(obj);
541 if (!wd) return EINA_FALSE;
542 if (!_els_smart_icon_file_key_set(wd->img, file, NULL))
550 elm_photo_size_set(Evas_Object *obj, int size)
552 ELM_CHECK_WIDTYPE(obj, widtype);
553 Widget_Data *wd = elm_widget_data_get(obj);
556 wd->size = (size > 0) ? size : 0;
558 _els_smart_icon_scale_size_set(wd->img, wd->size);
564 elm_photo_fill_inside_set(Evas_Object *obj, Eina_Bool fill)
566 ELM_CHECK_WIDTYPE(obj, widtype);
567 Widget_Data *wd = elm_widget_data_get(obj);
570 _els_smart_icon_fill_inside_set(wd->img, fill);
576 elm_photo_editable_set(Evas_Object *obj, Eina_Bool set)
578 ELM_CHECK_WIDTYPE(obj, widtype);
579 Widget_Data *wd = elm_widget_data_get(obj);
582 _els_smart_icon_edit_set(wd->img, set, obj);
586 elm_photo_thumb_set(const Evas_Object *obj, const char *file, const char *group)
588 ELM_CHECK_WIDTYPE(obj, widtype);
589 Widget_Data *wd = elm_widget_data_get(obj);
592 #ifdef HAVE_ELEMENTARY_ETHUMB
593 eina_stringshare_replace(&wd->thumb.file.path, file);
594 eina_stringshare_replace(&wd->thumb.file.key, group);
596 if (elm_thumb_ethumb_client_connected_get())
598 _icon_thumb_apply(wd);
604 wd->thumb.eeh = ecore_event_handler_add(ELM_ECORE_EVENT_ETHUMB_CONNECT, _icon_thumb_apply_cb, wd);
614 elm_photo_aspect_fixed_set(Evas_Object *obj, Eina_Bool fixed)
616 ELM_CHECK_WIDTYPE(obj, widtype);
617 Widget_Data *wd = elm_widget_data_get(obj);
619 return _els_smart_icon_aspect_fixed_set(wd->img, fixed);
623 elm_photo_aspect_fixed_get(const Evas_Object *obj)
625 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
626 Widget_Data *wd = elm_widget_data_get(obj);
627 if (!wd) return EINA_FALSE;
628 return _els_smart_icon_aspect_fixed_get(wd->img);
633 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/