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;
45 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 was_video : 1;
53 static const char *widtype = NULL;
55 #define SIG_CLICKED "clicked"
56 #define SIG_CLICKED_DOUBLE "clicked,double"
57 #define SIG_GENERATE_ERROR "generate,error"
58 #define SIG_GENERATE_START "generate,start"
59 #define SIG_GENERATE_STOP "generate,stop"
60 #define SIG_LOAD_ERROR "load,error"
61 #define SIG_PRESS "press"
63 static const Evas_Smart_Cb_Description _signals[] =
66 {SIG_CLICKED_DOUBLE, ""},
67 {SIG_GENERATE_ERROR, ""},
68 {SIG_GENERATE_START, ""},
69 {SIG_GENERATE_STOP, ""},
75 #define EDJE_SIGNAL_GENERATE_START "elm,thumb,generate,start"
76 #define EDJE_SIGNAL_GENERATE_STOP "elm,thumb,generate,stop"
77 #define EDJE_SIGNAL_GENERATE_ERROR "elm,thumb,generate,error"
78 #define EDJE_SIGNAL_LOAD_ERROR "elm,thumb,load,error"
79 #define EDJE_SIGNAL_PULSE_START "elm,state,pulse,start"
80 #define EDJE_SIGNAL_PULSE_STOP "elm,state,pulse,stop"
82 struct _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);
92 #ifdef HAVE_ELEMENTARY_ETHUMB
93 if (wd->thumb.id >= 0)
94 ethumb_client_generate_cancel(_elm_ethumb_client, wd->thumb.id,
98 eina_stringshare_del(wd->file);
99 eina_stringshare_del(wd->key);
100 if (wd->eeh) ecore_event_handler_del(wd->eeh);
105 _theme_hook(Evas_Object *obj)
107 Widget_Data *wd = elm_widget_data_get(obj);
108 _elm_theme_object_set(obj, wd->frame, "thumb", "base",
109 elm_widget_style_get(obj));
112 #ifdef HAVE_ELEMENTARY_ETHUMB
114 _mouse_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
116 Widget_Data *wd = data;
117 Evas_Event_Mouse_Down *ev = event_info;
121 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
122 wd->on_hold = EINA_TRUE;
124 wd->on_hold = EINA_FALSE;
125 if (ev->flags & EVAS_BUTTON_DOUBLE_CLICK)
126 evas_object_smart_callback_call(wd->self, SIG_CLICKED_DOUBLE, NULL);
128 evas_object_smart_callback_call(wd->self, SIG_PRESS, NULL);
132 _mouse_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
134 Widget_Data *wd = data;
135 Evas_Event_Mouse_Up *ev = event_info;
139 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
140 wd->on_hold = EINA_TRUE;
142 wd->on_hold = EINA_FALSE;
144 evas_object_smart_callback_call(wd->self, SIG_CLICKED, NULL);
145 wd->on_hold = EINA_FALSE;
149 _finished_thumb(Widget_Data *wd, const char *thumb_path, const char *thumb_key)
151 Eina_Bool new_view = EINA_FALSE;
156 evas = evas_object_evas_get(wd->self);
157 if ((wd->view) && (wd->is_video ^ wd->was_video))
159 evas_object_del(wd->view);
162 wd->was_video = wd->is_video;
164 if ((wd->is_video) &&
165 (ethumb_client_format_get(_elm_ethumb_client) == ETHUMB_THUMB_EET))
169 wd->view = edje_object_add(evas);
172 ERR("could not create edje object");
175 new_view = EINA_TRUE;
178 if (!edje_object_file_set(wd->view, thumb_path, "movie/thumb"))
180 ERR("could not set file=%s key=%s for %s", thumb_path, thumb_key,
189 wd->view = evas_object_image_filled_add(evas);
192 ERR("could not create image object");
195 new_view = EINA_TRUE;
198 evas_object_image_file_set(wd->view, thumb_path, thumb_key);
199 r = evas_object_image_load_error_get(wd->view);
200 if (r != EVAS_LOAD_ERROR_NONE)
202 ERR("%s: %s", thumb_path, evas_load_error_str(r));
207 if (new_view) elm_widget_sub_object_add(wd->self, wd->view);
208 edje_object_part_swallow(wd->frame, "elm.swallow.content", wd->view);
209 edje_object_size_min_get(wd->frame, &mw, &mh);
210 edje_object_size_min_restricted_calc(wd->frame, &mw, &mh, mw, mh);
211 evas_object_size_hint_min_set(wd->self, mw, mh);
212 eina_stringshare_replace(&(wd->thumb.file), thumb_path);
213 eina_stringshare_replace(&(wd->thumb.key), thumb_key);
214 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_STOP, "elm");
215 evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
219 evas_object_del(wd->view);
222 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_LOAD_ERROR, "elm");
223 evas_object_smart_callback_call(wd->self, SIG_LOAD_ERROR, NULL);
227 _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)
229 Widget_Data *wd = data;
231 EINA_SAFETY_ON_FALSE_RETURN(wd->thumb.id == id);
234 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_PULSE_STOP, "elm");
238 _finished_thumb(wd, thumb_path, thumb_key);
242 ERR("could not generate thumbnail for %s (key: %s)", file, key ? key : "");
243 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_ERROR, "elm");
244 evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
248 _thumb_apply(Widget_Data *wd)
250 if (wd->thumb.id > 0)
252 ethumb_client_generate_cancel
253 (_elm_ethumb_client, wd->thumb.id, NULL, NULL, NULL);
257 if (!wd->file) return;
259 ethumb_client_file_set(_elm_ethumb_client, wd->file, wd->key);
260 if (ethumb_client_thumb_exists(_elm_ethumb_client))
262 const char *thumb_path, *thumb_key;
265 ethumb_client_thumb_path_get(_elm_ethumb_client, &thumb_path,
267 _finished_thumb(wd, thumb_path, thumb_key);
270 else if ((wd->thumb.id = ethumb_client_generate
271 (_elm_ethumb_client, _finished_thumb_cb, wd, NULL)) != -1)
273 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_PULSE_START, "elm");
274 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_START, "elm");
275 evas_object_smart_callback_call(wd->self, SIG_GENERATE_START, NULL);
280 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_ERROR, "elm");
281 evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
286 _thumb_apply_cb(void *data, int type __UNUSED__, void *ev __UNUSED__)
289 return ECORE_CALLBACK_RENEW;
293 _thumb_show(Widget_Data *wd)
295 evas_object_show(wd->frame);
297 if (elm_thumb_ethumb_client_connected())
304 wd->eeh = ecore_event_handler_add(ELM_ECORE_EVENT_ETHUMB_CONNECT,
305 _thumb_apply_cb, wd);
309 _thumb_show_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
315 _thumb_hide_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
317 Widget_Data *wd = data;
319 evas_object_hide(wd->frame);
321 if (wd->thumb.id >= 0)
323 ethumb_client_generate_cancel
324 (_elm_ethumb_client, wd->thumb.id, NULL, NULL, NULL);
327 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_STOP, "elm");
328 evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
333 ecore_event_handler_del(wd->eeh);
341 static int _elm_need_ethumb = 0;
343 static void _on_die_cb(void *, Ethumb_Client *);
346 _connect_cb(void *data __UNUSED__, Ethumb_Client *c, Eina_Bool success)
350 ethumb_client_on_server_die_callback_set(c, _on_die_cb, NULL, NULL);
351 _elm_ethumb_connected = EINA_TRUE;
352 ecore_event_add(ELM_ECORE_EVENT_ETHUMB_CONNECT, NULL, NULL, NULL);
355 _elm_ethumb_client = NULL;
359 _on_die_cb(void *data __UNUSED__, Ethumb_Client *c __UNUSED__)
361 ethumb_client_disconnect(_elm_ethumb_client);
362 _elm_ethumb_client = NULL;
363 _elm_ethumb_connected = EINA_FALSE;
364 _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
369 _elm_unneed_ethumb(void)
372 if (--_elm_need_ethumb) return;
374 ethumb_client_disconnect(_elm_ethumb_client);
375 _elm_ethumb_client = NULL;
376 ethumb_client_shutdown();
377 ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
382 _elm_thumb_dropcb(void *data __UNUSED__, Evas_Object *o, Elm_Selection_Data *drop)
384 if ((!o) || (!drop) || (!drop->data)) return EINA_FALSE;
385 elm_thumb_file_set(o, drop->data, NULL);
390 * This must be called before any other function that handle with
391 * elm_thumb objects or ethumb_client instances.
396 elm_need_ethumb(void)
399 if (_elm_need_ethumb++) return EINA_TRUE;
400 ELM_ECORE_EVENT_ETHUMB_CONNECT = ecore_event_type_new();
401 ethumb_client_init();
402 _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
410 * Add a new thumb object to the parent.
412 * @param parent The parent object.
413 * @return The new object or NULL if it cannot be created.
415 * @see elm_thumb_file_set()
416 * @see elm_thumb_ethumb_client_get()
421 elm_thumb_add(Evas_Object *parent)
426 Evas_Coord minw, minh;
428 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
430 ELM_SET_WIDTYPE(widtype, "thumb");
431 elm_widget_type_set(obj, "thumb");
432 elm_widget_sub_object_add(parent, obj);
433 elm_widget_data_set(obj, wd);
434 elm_widget_del_hook_set(obj, _del_hook);
435 elm_widget_theme_hook_set(obj, _theme_hook);
436 elm_widget_can_focus_set(obj, EINA_FALSE);
438 wd->frame = edje_object_add(e);
439 _elm_theme_object_set(obj, wd->frame, "thumb", "base", "default");
440 elm_widget_resize_object_set(obj, wd->frame);
442 edje_object_size_min_calc(obj, &minw, &minh);
443 evas_object_size_hint_min_set(obj, minw, minh);
451 wd->on_hold = EINA_FALSE;
452 wd->is_video = EINA_FALSE;
453 wd->was_video = EINA_FALSE;
455 #ifdef HAVE_ELEMENTARY_ETHUMB
456 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_DOWN,
458 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
460 evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW,
462 evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE,
466 // TODO: convert Elementary to subclassing of Evas_Smart_Class
467 // TODO: and save some bytes, making descriptions per-class and not instance!
468 evas_object_smart_callbacks_descriptions_set(obj, _signals);
473 * Reload thumbnail if it was generated before.
475 * This is useful if the ethumb client configuration changed, like its
476 * size, aspect or any other property one set in the handle returned
477 * by elm_thumb_ethumb_client_get().
479 * @param obj The thumb object to reload
481 * @see elm_thumb_file_set()
486 elm_thumb_reload(Evas_Object *obj)
488 ELM_CHECK_WIDTYPE(obj, widtype);
489 Widget_Data *wd = elm_widget_data_get(obj);
491 eina_stringshare_replace(&(wd->thumb.file), NULL);
492 eina_stringshare_replace(&(wd->thumb.key), NULL);
494 #ifdef HAVE_ELEMENTARY_ETHUMB
495 if (evas_object_visible_get(obj))
501 * Set the file that will be used as thumbnail.
503 * The file can be an image or a video (in that case, acceptable extensions are:
504 * avi, mp4, ogv, mov, mpg and wmv). To start the video animation, use the
505 * function elm_thumb_animate().
507 * @param obj The thumb object.
508 * @param file The path to file that will be used as thumb.
509 * @param key The key used in case of an EET file.
511 * @see elm_thumb_file_get()
512 * @see elm_thumb_reload()
513 * @see elm_thumb_animate()
518 elm_thumb_file_set(Evas_Object *obj, const char *file, const char *key)
520 ELM_CHECK_WIDTYPE(obj, widtype);
521 Eina_Bool file_replaced, key_replaced;
522 Widget_Data *wd = elm_widget_data_get(obj);
524 file_replaced = eina_stringshare_replace(&(wd->file), file);
525 key_replaced = eina_stringshare_replace(&(wd->key), key);
530 const char **ext, *ptr;
531 static const char *extensions[] =
533 ".avi", ".mp4", ".ogv", ".mov", ".mpg", ".wmv", NULL
536 prefix_size = eina_stringshare_strlen(wd->file) - 4;
537 if (prefix_size >= 0)
539 ptr = wd->file + prefix_size;
540 wd->is_video = EINA_FALSE;
541 for (ext = extensions; *ext; ext++)
542 if (!strcasecmp(ptr, *ext))
544 wd->is_video = EINA_TRUE;
550 eina_stringshare_replace(&(wd->thumb.file), NULL);
551 eina_stringshare_replace(&(wd->thumb.key), NULL);
553 #ifdef HAVE_ELEMENTARY_ETHUMB
554 if (((file_replaced) || (key_replaced)) && (evas_object_visible_get(obj)))
560 * Get the image or video path and key used to generate the thumbnail.
562 * @param obj The thumb object.
563 * @param file Pointer to filename.
564 * @param key Pointer to key.
566 * @see elm_thumb_file_set()
567 * @see elm_thumb_path_get()
568 * @see elm_thumb_animate()
573 elm_thumb_file_get(const Evas_Object *obj, const char **file, const char **key)
575 ELM_CHECK_WIDTYPE(obj, widtype);
576 Widget_Data *wd = elm_widget_data_get(obj);
585 * Get the path and key to the image or video generated by ethumb.
587 * One just need to make sure that the thumbnail was generated before getting
588 * its path; otherwise, the path will be NULL. One way to do that is by asking
589 * for the path when/after the "generate,stop" smart callback is called.
591 * @param obj The thumb object.
592 * @param file Pointer to thumb path.
593 * @param key Pointer to thumb key.
595 * @see elm_thumb_file_get()
600 elm_thumb_path_get(const Evas_Object *obj, const char **file, const char **key)
602 ELM_CHECK_WIDTYPE(obj, widtype);
603 Widget_Data *wd = elm_widget_data_get(obj);
606 *file = wd->thumb.file;
608 *key = wd->thumb.key;
612 * Set the animation state for the thumb object. If its content is an animated
613 * video, you may start/stop the animation or tell it to play continuously and
616 * @param obj The thumb object.
617 * @param setting The animation setting.
619 * @see elm_thumb_file_set()
624 elm_thumb_animate_set(Evas_Object *obj, Elm_Thumb_Animation_Setting setting)
626 ELM_CHECK_WIDTYPE(obj, widtype);
627 Widget_Data *wd = elm_widget_data_get(obj);
629 EINA_SAFETY_ON_TRUE_RETURN(setting >= ELM_THUMB_ANIMATION_LAST);
631 wd->anim_setting = setting;
632 if (setting == ELM_THUMB_ANIMATION_LOOP)
633 edje_object_signal_emit(wd->view, "animate_loop", "");
634 else if (setting == ELM_THUMB_ANIMATION_START)
635 edje_object_signal_emit(wd->view, "animate", "");
636 else if (setting == ELM_THUMB_ANIMATION_STOP)
637 edje_object_signal_emit(wd->view, "animate_stop", "");
641 * Get the animation state for the thumb object.
643 * @param obj The thumb object.
644 * @return getting The animation setting or @c ELM_THUMB_ANIMATION_LAST,
647 * @see elm_thumb_file_get()
651 EAPI Elm_Thumb_Animation_Setting
652 elm_thumb_animate_get(const Evas_Object *obj)
654 ELM_CHECK_WIDTYPE(obj, widtype) ELM_THUMB_ANIMATION_LAST;
655 Widget_Data *wd = elm_widget_data_get(obj);
657 return wd->anim_setting;
661 * Get the ethumb_client handle so custom configuration can be made.
662 * This must be called before the objects are created to be sure no object is
663 * visible and no generation started.
665 * @return Ethumb_Client instance or NULL.
670 * #include <Elementary.h>
671 * #ifndef ELM_LIB_QUICKLAUNCH
673 * elm_main(int argc, char **argv)
675 * Ethumb_Client *client;
681 * client = elm_thumb_ethumb_client_get();
684 * ERR("could not get ethumb_client");
687 * ethumb_client_size_set(client, 100, 100);
688 * ethumb_client_crop_align_set(client, 0.5, 0.5);
691 * // Create elm_thumb objects here
704 elm_thumb_ethumb_client_get(void)
706 return _elm_ethumb_client;
710 * Get the ethumb_client connection state.
712 * @return EINA_TRUE if the client is connected to the server or
713 * EINA_FALSE otherwise.
716 elm_thumb_ethumb_client_connected(void)
718 return _elm_ethumb_connected;
722 elm_thumb_editable_set(Evas_Object *obj, Eina_Bool edit)
724 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
725 Widget_Data *wd = elm_widget_data_get(obj);
727 if (!wd) return EINA_FALSE;
729 if (wd->edit == edit) return EINA_TRUE;
733 elm_drop_target_add(obj, ELM_SEL_FORMAT_IMAGE,
734 _elm_thumb_dropcb, obj);
736 elm_drop_target_del(obj);
742 elm_thumb_editable_get(const Evas_Object *obj)
744 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
745 Widget_Data *wd = elm_widget_data_get(obj);
747 if (!wd) return EINA_FALSE;
751 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/