1 #include <Elementary.h>
5 * @defgroup Thumb Thumb
7 * A thumb object is used for displaying the thumbnail of an image or video.
8 * You must have compiled Elementary with Ethumb_Client support and the DBus
9 * service must be present and auto-activated in order to have thumbnails to
12 * Signals that you can add callbacks for are:
14 * "clicked" - This is called when a user has clicked the thumb without dragging
16 * "clicked,double" - This is called when a user has double-clicked the thumb.
17 * "press" - This is called when a user has pressed down the thumb.
18 * "generate,start" - The thumbnail generation started.
19 * "generate,stop" - The generation process stopped.
20 * "generate,error" - The generation failed.
21 * "load,error" - The thumbnail image loading failed.
24 typedef struct _Widget_Data Widget_Data;
38 #ifdef HAVE_ELEMENTARY_ETHUMB
39 Ethumb_Exists *exists;
42 Ecore_Event_Handler *eeh;
43 Elm_Thumb_Animation_Setting anim_setting;
44 Eina_Bool on_hold : 1;
45 Eina_Bool is_video : 1;
46 Eina_Bool was_video : 1;
50 static const char *widtype = NULL;
52 static const char SIG_CLICKED[] = "clicked";
53 static const char SIG_CLICKED_DOUBLE[] = "clicked,double";
54 static const char SIG_GENERATE_ERROR[] = "generate,error";
55 static const char SIG_GENERATE_START[] = "generate,start";
56 static const char SIG_GENERATE_STOP[] = "generate,stop";
57 static const char SIG_LOAD_ERROR[] = "load,error";
58 static const char SIG_PRESS[] = "press";
60 static const Evas_Smart_Cb_Description _signals[] =
63 {SIG_CLICKED_DOUBLE, ""},
64 {SIG_GENERATE_ERROR, ""},
65 {SIG_GENERATE_START, ""},
66 {SIG_GENERATE_STOP, ""},
72 #define EDJE_SIGNAL_GENERATE_START "elm,thumb,generate,start"
73 #define EDJE_SIGNAL_GENERATE_STOP "elm,thumb,generate,stop"
74 #define EDJE_SIGNAL_GENERATE_ERROR "elm,thumb,generate,error"
75 #define EDJE_SIGNAL_LOAD_ERROR "elm,thumb,load,error"
76 #define EDJE_SIGNAL_PULSE_START "elm,state,pulse,start"
77 #define EDJE_SIGNAL_PULSE_STOP "elm,state,pulse,stop"
79 struct _Ethumb_Client *_elm_ethumb_client = NULL;
80 Eina_Bool _elm_ethumb_connected = EINA_FALSE;
82 EAPI int ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
85 _del_hook(Evas_Object *obj)
87 Widget_Data *wd = elm_widget_data_get(obj);
89 #ifdef HAVE_ELEMENTARY_ETHUMB
90 if (wd->thumb.id >= 0)
91 ethumb_client_generate_cancel(_elm_ethumb_client, wd->thumb.id,
95 ethumb_client_thumb_exists_cancel(wd->thumb.exists);
96 wd->thumb.exists = NULL;
100 eina_stringshare_del(wd->file);
101 eina_stringshare_del(wd->key);
102 if (wd->eeh) ecore_event_handler_del(wd->eeh);
107 _theme_hook(Evas_Object *obj)
109 Widget_Data *wd = elm_widget_data_get(obj);
110 _elm_theme_object_set(obj, wd->frame, "thumb", "base",
111 elm_widget_style_get(obj));
114 #ifdef HAVE_ELEMENTARY_ETHUMB
116 _mouse_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
118 Widget_Data *wd = data;
119 Evas_Event_Mouse_Down *ev = event_info;
123 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
124 wd->on_hold = EINA_TRUE;
126 wd->on_hold = EINA_FALSE;
127 if (ev->flags & EVAS_BUTTON_DOUBLE_CLICK)
128 evas_object_smart_callback_call(wd->self, SIG_CLICKED_DOUBLE, NULL);
130 evas_object_smart_callback_call(wd->self, SIG_PRESS, NULL);
134 _mouse_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
136 Widget_Data *wd = data;
137 Evas_Event_Mouse_Up *ev = event_info;
141 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
142 wd->on_hold = EINA_TRUE;
144 wd->on_hold = EINA_FALSE;
146 evas_object_smart_callback_call(wd->self, SIG_CLICKED, NULL);
147 wd->on_hold = EINA_FALSE;
151 _finished_thumb(Widget_Data *wd, const char *thumb_path, const char *thumb_key)
153 Eina_Bool new_view = EINA_FALSE;
158 evas = evas_object_evas_get(wd->self);
159 if ((wd->view) && (wd->is_video ^ wd->was_video))
161 evas_object_del(wd->view);
164 wd->was_video = wd->is_video;
166 if ((wd->is_video) &&
167 (ethumb_client_format_get(_elm_ethumb_client) == ETHUMB_THUMB_EET))
171 wd->view = edje_object_add(evas);
174 ERR("could not create edje object");
177 new_view = EINA_TRUE;
180 if (!edje_object_file_set(wd->view, thumb_path, "movie/thumb"))
182 ERR("could not set file=%s key=%s for %s", thumb_path, thumb_key,
191 wd->view = evas_object_image_filled_add(evas);
194 ERR("could not create image object");
197 new_view = EINA_TRUE;
200 evas_object_image_file_set(wd->view, thumb_path, thumb_key);
201 r = evas_object_image_load_error_get(wd->view);
202 if (r != EVAS_LOAD_ERROR_NONE)
204 ERR("%s: %s", thumb_path, evas_load_error_str(r));
209 if (new_view) elm_widget_sub_object_add(wd->self, wd->view);
210 edje_object_part_swallow(wd->frame, "elm.swallow.content", wd->view);
211 edje_object_size_min_get(wd->frame, &mw, &mh);
212 edje_object_size_min_restricted_calc(wd->frame, &mw, &mh, mw, mh);
213 evas_object_size_hint_min_set(wd->self, mw, mh);
214 eina_stringshare_replace(&(wd->thumb.file), thumb_path);
215 eina_stringshare_replace(&(wd->thumb.key), thumb_key);
216 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_STOP, "elm");
217 evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
221 evas_object_del(wd->view);
224 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_LOAD_ERROR, "elm");
225 evas_object_smart_callback_call(wd->self, SIG_LOAD_ERROR, NULL);
229 _finished_thumb_cb(void *data, Ethumb_Client *c __UNUSED__, int id, const char *file, const char *key, const char *thumb_path, const char *thumb_key, Eina_Bool success)
231 Widget_Data *wd = data;
233 EINA_SAFETY_ON_FALSE_RETURN(wd->thumb.id == id);
236 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_PULSE_STOP, "elm");
240 _finished_thumb(wd, thumb_path, thumb_key);
244 ERR("could not generate thumbnail for %s (key: %s)", file, key ? key : "");
245 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_ERROR, "elm");
246 evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
250 _thumb_exists(Ethumb_Client *client __UNUSED__, Ethumb_Exists *thread,
251 Eina_Bool exists, void *data)
253 Widget_Data *wd = data;
255 if (ethumb_client_thumb_exists_check(thread))
260 const char *thumb_path, *thumb_key;
263 ethumb_client_thumb_path_get(_elm_ethumb_client, &thumb_path,
265 _finished_thumb(wd, thumb_path, thumb_key);
268 else if ((wd->thumb.id = ethumb_client_generate
269 (_elm_ethumb_client, _finished_thumb_cb, wd, NULL)) != -1)
271 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_PULSE_START, "elm");
272 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_START, "elm");
273 evas_object_smart_callback_call(wd->self, SIG_GENERATE_START, NULL);
278 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_ERROR, "elm");
279 evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
285 _thumb_apply(Widget_Data *wd)
287 if (wd->thumb.id > 0)
289 ethumb_client_generate_cancel
290 (_elm_ethumb_client, wd->thumb.id, NULL, NULL, NULL);
294 if (wd->thumb.exists)
296 ethumb_client_thumb_exists_cancel(wd->thumb.exists);
297 wd->thumb.exists = NULL;
300 if (!wd->file) return;
302 ethumb_client_file_set(_elm_ethumb_client, wd->file, wd->key);
303 ethumb_client_thumb_exists(_elm_ethumb_client, _thumb_exists, wd);
307 _thumb_apply_cb(void *data, int type __UNUSED__, void *ev __UNUSED__)
310 return ECORE_CALLBACK_RENEW;
314 _thumb_show(Widget_Data *wd)
316 evas_object_show(wd->frame);
318 if (elm_thumb_ethumb_client_connected())
325 wd->eeh = ecore_event_handler_add(ELM_ECORE_EVENT_ETHUMB_CONNECT,
326 _thumb_apply_cb, wd);
330 _thumb_show_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
336 _thumb_hide_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
338 Widget_Data *wd = data;
340 evas_object_hide(wd->frame);
342 if (wd->thumb.id >= 0)
344 ethumb_client_generate_cancel
345 (_elm_ethumb_client, wd->thumb.id, NULL, NULL, NULL);
348 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_STOP, "elm");
349 evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
352 if (wd->thumb.exists)
354 ethumb_client_thumb_exists_cancel(wd->thumb.exists);
355 wd->thumb.exists = NULL;
360 ecore_event_handler_del(wd->eeh);
368 static int _elm_need_ethumb = 0;
370 static void _on_die_cb(void *, Ethumb_Client *);
373 _connect_cb(void *data __UNUSED__, Ethumb_Client *c, Eina_Bool success)
377 ethumb_client_on_server_die_callback_set(c, _on_die_cb, NULL, NULL);
378 _elm_ethumb_connected = EINA_TRUE;
379 ecore_event_add(ELM_ECORE_EVENT_ETHUMB_CONNECT, NULL, NULL, NULL);
382 _elm_ethumb_client = NULL;
386 _on_die_cb(void *data __UNUSED__, Ethumb_Client *c __UNUSED__)
388 ethumb_client_disconnect(_elm_ethumb_client);
389 _elm_ethumb_client = NULL;
390 _elm_ethumb_connected = EINA_FALSE;
391 _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
396 _elm_unneed_ethumb(void)
399 if (--_elm_need_ethumb) return;
401 ethumb_client_disconnect(_elm_ethumb_client);
402 _elm_ethumb_client = NULL;
403 ethumb_client_shutdown();
404 ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
409 _elm_thumb_dropcb(void *data __UNUSED__, Evas_Object *o, Elm_Selection_Data *drop)
411 if ((!o) || (!drop) || (!drop->data)) return EINA_FALSE;
412 elm_thumb_file_set(o, drop->data, NULL);
417 * This must be called before any other function that handle with
418 * elm_thumb objects or ethumb_client instances.
423 elm_need_ethumb(void)
426 if (_elm_need_ethumb++) return EINA_TRUE;
427 ELM_ECORE_EVENT_ETHUMB_CONNECT = ecore_event_type_new();
428 ethumb_client_init();
429 _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
437 * Add a new thumb object to the parent.
439 * @param parent The parent object.
440 * @return The new object or NULL if it cannot be created.
442 * @see elm_thumb_file_set()
443 * @see elm_thumb_ethumb_client_get()
448 elm_thumb_add(Evas_Object *parent)
453 Evas_Coord minw, minh;
455 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
457 ELM_SET_WIDTYPE(widtype, "thumb");
458 elm_widget_type_set(obj, "thumb");
459 elm_widget_sub_object_add(parent, obj);
460 elm_widget_data_set(obj, wd);
461 elm_widget_del_hook_set(obj, _del_hook);
462 elm_widget_theme_hook_set(obj, _theme_hook);
463 elm_widget_can_focus_set(obj, EINA_FALSE);
465 wd->frame = edje_object_add(e);
466 _elm_theme_object_set(obj, wd->frame, "thumb", "base", "default");
467 elm_widget_resize_object_set(obj, wd->frame);
469 edje_object_size_min_calc(obj, &minw, &minh);
470 evas_object_size_hint_min_set(obj, minw, minh);
478 wd->on_hold = EINA_FALSE;
479 wd->is_video = EINA_FALSE;
480 wd->was_video = EINA_FALSE;
482 #ifdef HAVE_ELEMENTARY_ETHUMB
483 wd->thumb.exists = NULL;
484 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_DOWN,
486 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
488 evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW,
490 evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE,
494 // TODO: convert Elementary to subclassing of Evas_Smart_Class
495 // TODO: and save some bytes, making descriptions per-class and not instance!
496 evas_object_smart_callbacks_descriptions_set(obj, _signals);
501 * Reload thumbnail if it was generated before.
503 * This is useful if the ethumb client configuration changed, like its
504 * size, aspect or any other property one set in the handle returned
505 * by elm_thumb_ethumb_client_get().
507 * @param obj The thumb object to reload
509 * @see elm_thumb_file_set()
514 elm_thumb_reload(Evas_Object *obj)
516 ELM_CHECK_WIDTYPE(obj, widtype);
517 Widget_Data *wd = elm_widget_data_get(obj);
519 eina_stringshare_replace(&(wd->thumb.file), NULL);
520 eina_stringshare_replace(&(wd->thumb.key), NULL);
522 #ifdef HAVE_ELEMENTARY_ETHUMB
523 if (evas_object_visible_get(obj))
529 * Set the file that will be used as thumbnail.
531 * The file can be an image or a video (in that case, acceptable extensions are:
532 * avi, mp4, ogv, mov, mpg and wmv). To start the video animation, use the
533 * function elm_thumb_animate().
535 * @param obj The thumb object.
536 * @param file The path to file that will be used as thumb.
537 * @param key The key used in case of an EET file.
539 * @see elm_thumb_file_get()
540 * @see elm_thumb_reload()
541 * @see elm_thumb_animate()
546 elm_thumb_file_set(Evas_Object *obj, const char *file, const char *key)
548 ELM_CHECK_WIDTYPE(obj, widtype);
549 Eina_Bool file_replaced, key_replaced;
550 Widget_Data *wd = elm_widget_data_get(obj);
552 file_replaced = eina_stringshare_replace(&(wd->file), file);
553 key_replaced = eina_stringshare_replace(&(wd->key), key);
558 const char **ext, *ptr;
559 static const char *extensions[] =
561 ".avi", ".mp4", ".ogv", ".mov", ".mpg", ".wmv", NULL
564 prefix_size = eina_stringshare_strlen(wd->file) - 4;
565 if (prefix_size >= 0)
567 ptr = wd->file + prefix_size;
568 wd->is_video = EINA_FALSE;
569 for (ext = extensions; *ext; ext++)
570 if (!strcasecmp(ptr, *ext))
572 wd->is_video = EINA_TRUE;
578 eina_stringshare_replace(&(wd->thumb.file), NULL);
579 eina_stringshare_replace(&(wd->thumb.key), NULL);
581 #ifdef HAVE_ELEMENTARY_ETHUMB
582 if (((file_replaced) || (key_replaced)) && (evas_object_visible_get(obj)))
588 * Get the image or video path and key used to generate the thumbnail.
590 * @param obj The thumb object.
591 * @param file Pointer to filename.
592 * @param key Pointer to key.
594 * @see elm_thumb_file_set()
595 * @see elm_thumb_path_get()
596 * @see elm_thumb_animate()
601 elm_thumb_file_get(const Evas_Object *obj, const char **file, const char **key)
603 ELM_CHECK_WIDTYPE(obj, widtype);
604 Widget_Data *wd = elm_widget_data_get(obj);
613 * Get the path and key to the image or video generated by ethumb.
615 * One just need to make sure that the thumbnail was generated before getting
616 * its path; otherwise, the path will be NULL. One way to do that is by asking
617 * for the path when/after the "generate,stop" smart callback is called.
619 * @param obj The thumb object.
620 * @param file Pointer to thumb path.
621 * @param key Pointer to thumb key.
623 * @see elm_thumb_file_get()
628 elm_thumb_path_get(const Evas_Object *obj, const char **file, const char **key)
630 ELM_CHECK_WIDTYPE(obj, widtype);
631 Widget_Data *wd = elm_widget_data_get(obj);
634 *file = wd->thumb.file;
636 *key = wd->thumb.key;
640 * Set the animation state for the thumb object. If its content is an animated
641 * video, you may start/stop the animation or tell it to play continuously and
644 * @param obj The thumb object.
645 * @param setting The animation setting.
647 * @see elm_thumb_file_set()
652 elm_thumb_animate_set(Evas_Object *obj, Elm_Thumb_Animation_Setting setting)
654 ELM_CHECK_WIDTYPE(obj, widtype);
655 Widget_Data *wd = elm_widget_data_get(obj);
657 EINA_SAFETY_ON_TRUE_RETURN(setting >= ELM_THUMB_ANIMATION_LAST);
659 wd->anim_setting = setting;
660 if (setting == ELM_THUMB_ANIMATION_LOOP)
661 edje_object_signal_emit(wd->view, "animate_loop", "");
662 else if (setting == ELM_THUMB_ANIMATION_START)
663 edje_object_signal_emit(wd->view, "animate", "");
664 else if (setting == ELM_THUMB_ANIMATION_STOP)
665 edje_object_signal_emit(wd->view, "animate_stop", "");
669 * Get the animation state for the thumb object.
671 * @param obj The thumb object.
672 * @return getting The animation setting or @c ELM_THUMB_ANIMATION_LAST,
675 * @see elm_thumb_file_get()
679 EAPI Elm_Thumb_Animation_Setting
680 elm_thumb_animate_get(const Evas_Object *obj)
682 ELM_CHECK_WIDTYPE(obj, widtype) ELM_THUMB_ANIMATION_LAST;
683 Widget_Data *wd = elm_widget_data_get(obj);
685 return wd->anim_setting;
689 * Get the ethumb_client handle so custom configuration can be made.
690 * This must be called before the objects are created to be sure no object is
691 * visible and no generation started.
693 * @return Ethumb_Client instance or NULL.
698 * #include <Elementary.h>
699 * #ifndef ELM_LIB_QUICKLAUNCH
701 * elm_main(int argc, char **argv)
703 * Ethumb_Client *client;
709 * client = elm_thumb_ethumb_client_get();
712 * ERR("could not get ethumb_client");
715 * ethumb_client_size_set(client, 100, 100);
716 * ethumb_client_crop_align_set(client, 0.5, 0.5);
719 * // Create elm_thumb objects here
732 elm_thumb_ethumb_client_get(void)
734 return _elm_ethumb_client;
738 * Get the ethumb_client connection state.
740 * @return EINA_TRUE if the client is connected to the server or
741 * EINA_FALSE otherwise.
744 elm_thumb_ethumb_client_connected(void)
746 return _elm_ethumb_connected;
750 elm_thumb_editable_set(Evas_Object *obj, Eina_Bool edit)
752 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
753 Widget_Data *wd = elm_widget_data_get(obj);
755 if (!wd) return EINA_FALSE;
757 if (wd->edit == edit) return EINA_TRUE;
761 elm_drop_target_add(obj, ELM_SEL_FORMAT_IMAGE,
762 _elm_thumb_dropcb, obj);
764 elm_drop_target_del(obj);
770 elm_thumb_editable_get(const Evas_Object *obj)
772 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
773 Widget_Data *wd = elm_widget_data_get(obj);
775 if (!wd) return EINA_FALSE;
779 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/