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
17 * clicked,double - This is called when a user has double-clicked the thumb.
19 * press - This is called when a user has pressed down the thumb.
21 * generate,start - The thumbnail generation started.
23 * generate,stop - The generation process stopped.
25 * generate,error - The generation failed.
27 * load,error - The thumbnail image loading failed.
30 typedef struct _Widget_Data Widget_Data;
44 Ecore_Event_Handler *eeh;
46 Elm_Thumb_Animation_Setting anim_setting;
47 Eina_Bool on_hold : 1;
48 Eina_Bool is_video : 1;
49 Eina_Bool is_generating : 1;
50 Eina_Bool keep_aspect : 1;
53 static const char *widtype = NULL;
55 static const char SIG_CLICKED[] = "clicked";
56 static const char SIG_CLICKED_DOUBLE[] = "clicked,double";
57 static const char SIG_GENERATE_ERROR[] = "generate,error";
58 static const char SIG_GENERATE_START[] = "generate,start";
59 static const char SIG_GENERATE_STOP[] = "generate,stop";
60 static const char SIG_LOAD_ERROR[] = "load,error";
61 static const char SIG_PRESS[]= "press";
62 static const Evas_Smart_Cb_Description _signals[] = {
64 {SIG_CLICKED_DOUBLE, ""},
65 {SIG_GENERATE_ERROR, ""},
66 {SIG_GENERATE_START, ""},
67 {SIG_GENERATE_STOP, ""},
73 static const char EDJE_SIGNAL_GENERATE_START[] = "elm,thumb,generate,start";
74 static const char EDJE_SIGNAL_GENERATE_STOP[] = "elm,thumb,generate,stop";
75 static const char EDJE_SIGNAL_GENERATE_ERROR[] = "elm,thumb,generate,error";
76 static const char EDJE_SIGNAL_LOAD_ERROR[] = "elm,thumb,load,error";
77 static const char EDJE_SIGNAL_PULSE_START[] = "elm,state,pulse,start";
78 static const char EDJE_SIGNAL_PULSE_STOP[] = "elm,state,pulse,stop";
80 #ifdef HAVE_ELEMENTARY_ETHUMB
81 Ethumb_Client *_elm_ethumb_client = NULL;
83 Eina_Bool _elm_ethumb_connected = EINA_FALSE;
85 EAPI int ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
88 _del_hook(Evas_Object *obj)
90 Widget_Data *wd = elm_widget_data_get(obj);
91 eina_stringshare_del(wd->file);
92 eina_stringshare_del(wd->key);
94 ecore_event_handler_del(wd->eeh);
99 _theme_hook(Evas_Object *obj)
101 Widget_Data *wd = elm_widget_data_get(obj);
102 _elm_theme_object_set(obj, wd->children.frm, "thumb", "base", elm_widget_style_get(obj));
105 #ifdef HAVE_ELEMENTARY_ETHUMB
107 _mouse_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
109 Widget_Data *wd = data;
110 Evas_Event_Mouse_Down *ev = event_info;
113 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
114 wd->on_hold = EINA_TRUE;
116 wd->on_hold = EINA_FALSE;
117 if (ev->flags & EVAS_BUTTON_DOUBLE_CLICK)
118 evas_object_smart_callback_call(data, SIG_CLICKED_DOUBLE, NULL);
120 evas_object_smart_callback_call(data, SIG_PRESS, NULL);
124 _mouse_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
127 Widget_Data *wd = data;
128 Evas_Event_Mouse_Up *ev = event_info;
131 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
132 wd->on_hold = EINA_TRUE;
134 wd->on_hold = EINA_FALSE;
136 evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
137 wd->on_hold = EINA_FALSE;
141 _finished_thumb(Widget_Data *wd, int id, const char *thumb_path, const char *thumb_key)
146 evas = evas_object_evas_get(wd->self);
147 if (wd->children.view)
148 evas_object_del(wd->children.view);
149 wd->children.view = NULL;
153 ethumb_client_format_get(_elm_ethumb_client) == ETHUMB_THUMB_EET)
155 wd->children.view = edje_object_add(evas);
156 if (!wd->children.view)
158 ERR("could not create edje object");
161 if (!edje_object_file_set(wd->children.view, thumb_path, "movie/thumb"))
163 ERR("could not set file=%s key=%s for %s", thumb_path, thumb_key,
170 wd->children.view = evas_object_image_filled_add(evas);
171 if (!wd->children.view)
173 ERR("could not create image object");
176 evas_object_image_file_set(wd->children.view, thumb_path, thumb_key);
177 r = evas_object_image_load_error_get(wd->children.view);
178 if (r != EVAS_LOAD_ERROR_NONE)
180 ERR("%s: %s", thumb_path, evas_load_error_str(r));
185 elm_widget_sub_object_add(wd->self, wd->children.view);
186 edje_object_part_swallow(wd->children.frm, "elm.swallow.content",
188 edje_object_signal_emit(wd->children.frm, EDJE_SIGNAL_GENERATE_STOP, "elm");
189 evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
193 evas_object_del(wd->children.view);
194 wd->children.view = NULL;
196 edje_object_signal_emit(wd->children.frm, EDJE_SIGNAL_LOAD_ERROR, "elm");
197 evas_object_smart_callback_call(wd->self, SIG_LOAD_ERROR, NULL);
201 _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)
203 Widget_Data *wd = data;
205 edje_object_signal_emit(wd->children.frm, EDJE_SIGNAL_PULSE_STOP, "elm");
206 wd->is_generating = EINA_FALSE;
210 _finished_thumb(wd, id, thumb_path, thumb_key);
214 ERR("could not generate thumbnail for %s (key: %s)", file, key ? key : "");
215 edje_object_signal_emit(wd->children.frm, EDJE_SIGNAL_GENERATE_ERROR, "elm");
216 evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
220 _thumb_apply(Widget_Data *wd)
222 ethumb_client_file_set(_elm_ethumb_client, wd->file, wd->key);
223 if (ethumb_client_thumb_exists(_elm_ethumb_client))
225 const char *thumb_path, *thumb_key;
226 ethumb_client_thumb_path_get(_elm_ethumb_client, &thumb_path,
228 _finished_thumb(wd, 0, thumb_path, thumb_key);
231 else if (ethumb_client_generate(_elm_ethumb_client, _finished_thumb_cb, wd,
234 edje_object_signal_emit(wd->children.frm, EDJE_SIGNAL_PULSE_START,
236 edje_object_signal_emit(wd->children.frm, EDJE_SIGNAL_GENERATE_START,
238 evas_object_smart_callback_call(wd->self, SIG_GENERATE_START, NULL);
242 edje_object_signal_emit(wd->children.frm, EDJE_SIGNAL_GENERATE_ERROR,
244 evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
246 wd->is_generating = EINA_FALSE;
250 _thumb_apply_cb(void *data, int type __UNUSED__, void *ev __UNUSED__)
253 return ECORE_CALLBACK_RENEW;
257 _thumb_show(Widget_Data *wd)
259 evas_object_show(wd->children.frm);
261 if (elm_thumb_ethumb_client_connected())
268 wd->eeh = ecore_event_handler_add(ELM_ECORE_EVENT_ETHUMB_CONNECT,
269 _thumb_apply_cb, wd);
273 _thumb_show_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
279 _cancel_cb(void *data, Eina_Bool success)
281 Widget_Data *wd = data;
285 wd->is_generating = EINA_FALSE;
286 edje_object_signal_emit(wd->children.frm, EDJE_SIGNAL_GENERATE_STOP,
288 evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
293 _thumb_hide_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
295 Widget_Data *wd = data;
297 evas_object_hide(wd->children.frm);
299 if (wd->is_generating)
300 ethumb_client_generate_cancel(_elm_ethumb_client, wd->id, _cancel_cb, wd, NULL);
303 ecore_event_handler_del(wd->eeh);
311 static Eina_Bool _elm_need_ethumb = 0;
313 static void _on_die_cb(void *, Ethumb_Client *);
316 _connect_cb(void *data __UNUSED__, Ethumb_Client *c, Eina_Bool success)
320 ethumb_client_on_server_die_callback_set(c, _on_die_cb, NULL, NULL);
321 _elm_ethumb_connected = EINA_TRUE;
322 ecore_event_add(ELM_ECORE_EVENT_ETHUMB_CONNECT, NULL, NULL, NULL);
325 _elm_ethumb_client = NULL;
329 _on_die_cb(void *data __UNUSED__, Ethumb_Client *c __UNUSED__)
331 ethumb_client_disconnect(_elm_ethumb_client);
332 _elm_ethumb_client = NULL;
333 _elm_ethumb_connected = EINA_FALSE;
334 _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
339 _elm_unneed_ethumb(void)
342 if (_elm_need_ethumb)
344 _elm_need_ethumb = 0;
345 ethumb_client_disconnect(_elm_ethumb_client);
346 _elm_ethumb_client = NULL;
347 ethumb_client_shutdown();
348 ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
354 * This must be called before any other function that handle with
355 * elm_thumb objects or ethumb_client instances.
360 elm_need_ethumb(void)
363 if (_elm_need_ethumb)
365 ELM_ECORE_EVENT_ETHUMB_CONNECT = ecore_event_type_new();
366 _elm_need_ethumb = 1;
367 ethumb_client_init();
368 _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
373 * Add a new thumb object to the parent.
375 * @param parent The parent object.
376 * @return The new object or NULL if it cannot be created.
378 * @see elm_thumb_file_set()
379 * @see elm_thumb_ethumb_client_get()
380 * @see elm_thumb_keep_aspect_set()
381 * @see elm_thumb_align_set()
382 * @see elm_thumb_align_get()
387 elm_thumb_add(Evas_Object *parent)
392 Evas_Coord minw, minh;
394 wd = ELM_NEW(Widget_Data);
395 evas = evas_object_evas_get(parent);
396 obj = elm_widget_add(evas);
397 ELM_SET_WIDTYPE(widtype, "thumb");
398 elm_widget_type_set(obj, "thumb");
399 elm_widget_sub_object_add(parent, obj);
400 elm_widget_data_set(obj, wd);
401 elm_widget_del_hook_set(obj, _del_hook);
402 elm_widget_theme_hook_set(obj, _theme_hook);
404 wd->children.frm = edje_object_add(evas);
405 _elm_theme_object_set(obj, wd->children.frm, "thumb", "base", "default");
406 elm_widget_resize_object_set(obj, wd->children.frm);
408 edje_object_size_min_calc(obj, &minw, &minh);
409 evas_object_size_hint_min_set(obj, minw, minh);
412 wd->children.view = NULL;
416 wd->children.align.x = 0.5;
417 wd->children.align.y = 0.5;
418 wd->on_hold = EINA_FALSE;
419 wd->is_video = EINA_FALSE;
420 wd->is_generating = EINA_FALSE;
421 wd->keep_aspect = EINA_FALSE;
423 #ifdef HAVE_ELEMENTARY_ETHUMB
424 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_DOWN,
426 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
428 evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW,
430 evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE,
434 // TODO: convert Elementary to subclassing of Evas_Smart_Class
435 // TODO: and save some bytes, making descriptions per-class and not instance!
436 evas_object_smart_callbacks_descriptions_set(obj, _signals);
441 * Set the file that will be used as thumbnail.
443 * The file can be an image or a video (in that case, acceptable extensions are:
444 * avi, mp4, ogv, mov, mpg and wmv). To start the video animation, use the
445 * function elm_thumb_animate().
447 * @param obj The thumb object.
448 * @param file The path to file that will be used as thumb.
449 * @param key The key used in case of an EET file.
451 * @see elm_thumb_file_get()
452 * @see elm_thumb_animate()
457 elm_thumb_file_set(Evas_Object *obj, const char *file, const char *key)
459 ELM_CHECK_WIDTYPE(obj, widtype);
460 Eina_Bool file_replaced, key_replaced;
461 Widget_Data *wd = elm_widget_data_get(obj);
463 file_replaced = eina_stringshare_replace(&(wd->file), file);
464 key_replaced = eina_stringshare_replace(&(wd->key), key);
469 const char **ext, *ptr;
470 static const char *extensions[] = { ".avi", ".mp4", ".ogv", ".mov",
471 ".mpg", ".wmv", NULL };
473 prefix_size = eina_stringshare_strlen(wd->file) - 4;
474 if (prefix_size >= 0)
476 ptr = wd->file + prefix_size;
477 wd->is_video = EINA_FALSE;
478 for (ext = extensions; *ext; ext++)
479 if (!strcasecmp(ptr, *ext))
481 wd->is_video = EINA_TRUE;
487 #ifdef HAVE_ELEMENTARY_ETHUMB
488 if ((file_replaced || key_replaced) && evas_object_visible_get(obj))
494 * Get the image or video path and key used to generate the thumbnail.
496 * @param obj The thumb object.
497 * @param file Pointer to filename.
498 * @param key Pointer to key.
500 * @see elm_thumb_file_set()
501 * @see elm_thumb_animate()
506 elm_thumb_file_get(const Evas_Object *obj, const char **file, const char **key)
508 ELM_CHECK_WIDTYPE(obj, widtype);
509 Widget_Data *wd = elm_widget_data_get(obj);
517 * Set the animation state for the thumb object. If its content is an animated
518 * video, you may start/stop the animation or tell it to play continuously and
521 * @param obj The thumb object.
522 * @param setting The animation setting.
524 * @see elm_thumb_file_set()
529 elm_thumb_animate_set(Evas_Object *obj, Elm_Thumb_Animation_Setting setting)
531 ELM_CHECK_WIDTYPE(obj, widtype);
532 Widget_Data *wd = elm_widget_data_get(obj);
534 if (setting < ELM_THUMB_ANIMATION_START ||
535 setting >= ELM_THUMB_ANIMATION_LAST)
540 wd->anim_setting = setting;
541 if (setting == ELM_THUMB_ANIMATION_LOOP)
542 edje_object_signal_emit(wd->children.view, "animate_loop", "");
543 else if (setting == ELM_THUMB_ANIMATION_START)
544 edje_object_signal_emit(wd->children.view, "animate", "");
545 else if (setting == ELM_THUMB_ANIMATION_STOP)
546 edje_object_signal_emit(wd->children.view, "animate_stop", "");
550 * Get the animation state for the thumb object.
552 * @param obj The thumb object.
553 * @return getting The animation setting or @c ELM_THUMB_ANIMATION_LAST,
556 * @see elm_thumb_file_get()
560 EAPI Elm_Thumb_Animation_Setting
561 elm_thumb_animate_get(const Evas_Object *obj)
563 ELM_CHECK_WIDTYPE(obj, widtype) ELM_THUMB_ANIMATION_LAST;
564 Widget_Data *wd = elm_widget_data_get(obj);
566 return wd->anim_setting;
570 * Set whether the thumbnail exhibition should keep the image or video aspect.
572 * For positioning the image/video within the object use the function
573 * elm_thumb_align_set().
575 * @param obj The thumb object.
576 * @param setting Whether keep or not the aspect.
578 * @see elm_thumb_file_set()
579 * @see elm_thumb_align_set()
584 elm_thumb_keep_aspect_set(Evas_Object *obj, Eina_Bool setting)
586 ELM_CHECK_WIDTYPE(obj, widtype);
587 Widget_Data *wd = elm_widget_data_get(obj);
588 wd->keep_aspect = setting;
592 * Get back the aspect info set with @c elm_thumb_keep_aspect_set().
594 * @param obj The thumb object.
595 * @return Whether the thumb object keeps or not the aspect for its content.
597 * @see elm_thumb_file_get()
598 * @see elm_thumb_align_get()
603 elm_thumb_keep_aspect_get(const Evas_Object *obj)
605 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
606 Widget_Data *wd = elm_widget_data_get(obj);
607 return wd->keep_aspect;
611 * Set image/video's alignment within the thumbnail.
613 * @param obj The thumb object.
614 * @param x_align The x alignment (0 <= x <= 1).
615 * @param y_align The y alignment (0 <= y <= 1).
617 * @see elm_thumb_keep_aspect_set()
618 * @see elm_thumb_align_get()
623 elm_thumb_align_set(Evas_Object *obj, float x_align, float y_align)
625 ELM_CHECK_WIDTYPE(obj, widtype);
626 Widget_Data *wd = elm_widget_data_get(obj);
630 else if (x_align < 0.0)
632 wd->children.align.x = x_align;
636 else if (y_align < 0.0)
638 wd->children.align.y = y_align;
642 * Get the alignenment set for the thumb object.
644 * @param obj The thumb object.
645 * @param x Pointer to x alignenment.
646 * @param y Pointer to y alignenment.
648 * @see elm_thumb_align_set()
653 elm_thumb_align_get(const Evas_Object *obj, float *x, float *y)
655 ELM_CHECK_WIDTYPE(obj, widtype);
656 Widget_Data *wd = elm_widget_data_get(obj);
657 if (x) *x = wd->children.align.x;
658 if (y) *y = wd->children.align.y;
662 * Get the ethumb_client handle so custom configuration can be made.
663 * This must be called before the objects are created to be sure no object is
664 * visible and no generation started.
666 * @return Ethumb_Client instance or NULL.
671 * #include <Elementary.h>
672 * #ifndef ELM_LIB_QUICKLAUNCH
674 * elm_main(int argc, char **argv)
676 * Ethumb_Client *client;
682 * client = elm_thumb_ethumb_client_get();
685 * ERR("could not get ethumb_client");
688 * ethumb_client_size_set(client, 100, 100);
689 * ethumb_client_crop_align_set(client, 0.5, 0.5);
692 * // Create elm_thumb objects here
706 elm_thumb_ethumb_client_get(void)
708 return _elm_ethumb_client;
712 elm_thumb_ethumb_client_get(void)
719 * Get the ethumb_client connection state.
721 * @return EINA_TRUE if the client is connected to the server or
722 * EINA_FALSE otherwise.
725 elm_thumb_ethumb_client_connected(void)
727 return _elm_ethumb_connected;