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 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
430 wd = ELM_NEW(Widget_Data);
431 evas = evas_object_evas_get(parent);
432 if (!evas) return NULL;
433 obj = elm_widget_add(evas);
434 ELM_SET_WIDTYPE(widtype, "thumb");
435 elm_widget_type_set(obj, "thumb");
436 elm_widget_sub_object_add(parent, obj);
437 elm_widget_data_set(obj, wd);
438 elm_widget_del_hook_set(obj, _del_hook);
439 elm_widget_theme_hook_set(obj, _theme_hook);
440 elm_widget_can_focus_set(obj, EINA_FALSE);
442 wd->frame = edje_object_add(evas);
443 _elm_theme_object_set(obj, wd->frame, "thumb", "base", "default");
444 elm_widget_resize_object_set(obj, wd->frame);
446 edje_object_size_min_calc(obj, &minw, &minh);
447 evas_object_size_hint_min_set(obj, minw, minh);
455 wd->on_hold = EINA_FALSE;
456 wd->is_video = EINA_FALSE;
457 wd->was_video = EINA_FALSE;
459 #ifdef HAVE_ELEMENTARY_ETHUMB
460 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_DOWN,
462 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
464 evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW,
466 evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE,
470 // TODO: convert Elementary to subclassing of Evas_Smart_Class
471 // TODO: and save some bytes, making descriptions per-class and not instance!
472 evas_object_smart_callbacks_descriptions_set(obj, _signals);
477 * Reload thumbnail if it was generated before.
479 * This is useful if the ethumb client configuration changed, like its
480 * size, aspect or any other property one set in the handle returned
481 * by elm_thumb_ethumb_client_get().
483 * @param obj The thumb object to reload
485 * @see elm_thumb_file_set()
490 elm_thumb_reload(Evas_Object *obj)
492 ELM_CHECK_WIDTYPE(obj, widtype);
493 Widget_Data *wd = elm_widget_data_get(obj);
495 eina_stringshare_replace(&(wd->thumb.file), NULL);
496 eina_stringshare_replace(&(wd->thumb.key), NULL);
498 #ifdef HAVE_ELEMENTARY_ETHUMB
499 if (evas_object_visible_get(obj))
505 * Set the file that will be used as thumbnail.
507 * The file can be an image or a video (in that case, acceptable extensions are:
508 * avi, mp4, ogv, mov, mpg and wmv). To start the video animation, use the
509 * function elm_thumb_animate().
511 * @param obj The thumb object.
512 * @param file The path to file that will be used as thumb.
513 * @param key The key used in case of an EET file.
515 * @see elm_thumb_file_get()
516 * @see elm_thumb_reload()
517 * @see elm_thumb_animate()
522 elm_thumb_file_set(Evas_Object *obj, const char *file, const char *key)
524 ELM_CHECK_WIDTYPE(obj, widtype);
525 Eina_Bool file_replaced, key_replaced;
526 Widget_Data *wd = elm_widget_data_get(obj);
528 file_replaced = eina_stringshare_replace(&(wd->file), file);
529 key_replaced = eina_stringshare_replace(&(wd->key), key);
534 const char **ext, *ptr;
535 static const char *extensions[] =
537 ".avi", ".mp4", ".ogv", ".mov", ".mpg", ".wmv", NULL
540 prefix_size = eina_stringshare_strlen(wd->file) - 4;
541 if (prefix_size >= 0)
543 ptr = wd->file + prefix_size;
544 wd->is_video = EINA_FALSE;
545 for (ext = extensions; *ext; ext++)
546 if (!strcasecmp(ptr, *ext))
548 wd->is_video = EINA_TRUE;
554 eina_stringshare_replace(&(wd->thumb.file), NULL);
555 eina_stringshare_replace(&(wd->thumb.key), NULL);
557 #ifdef HAVE_ELEMENTARY_ETHUMB
558 if (((file_replaced) || (key_replaced)) && (evas_object_visible_get(obj)))
564 * Get the image or video path and key used to generate the thumbnail.
566 * @param obj The thumb object.
567 * @param file Pointer to filename.
568 * @param key Pointer to key.
570 * @see elm_thumb_file_set()
571 * @see elm_thumb_path_get()
572 * @see elm_thumb_animate()
577 elm_thumb_file_get(const Evas_Object *obj, const char **file, const char **key)
579 ELM_CHECK_WIDTYPE(obj, widtype);
580 Widget_Data *wd = elm_widget_data_get(obj);
589 * Get the path and key to the image or video generated by ethumb.
591 * One just need to make sure that the thumbnail was generated before getting
592 * its path; otherwise, the path will be NULL. One way to do that is by asking
593 * for the path when/after the "generate,stop" smart callback is called.
595 * @param obj The thumb object.
596 * @param file Pointer to thumb path.
597 * @param key Pointer to thumb key.
599 * @see elm_thumb_file_get()
604 elm_thumb_path_get(const Evas_Object *obj, const char **file, const char **key)
606 ELM_CHECK_WIDTYPE(obj, widtype);
607 Widget_Data *wd = elm_widget_data_get(obj);
610 *file = wd->thumb.file;
612 *key = wd->thumb.key;
616 * Set the animation state for the thumb object. If its content is an animated
617 * video, you may start/stop the animation or tell it to play continuously and
620 * @param obj The thumb object.
621 * @param setting The animation setting.
623 * @see elm_thumb_file_set()
628 elm_thumb_animate_set(Evas_Object *obj, Elm_Thumb_Animation_Setting setting)
630 ELM_CHECK_WIDTYPE(obj, widtype);
631 Widget_Data *wd = elm_widget_data_get(obj);
633 EINA_SAFETY_ON_TRUE_RETURN(setting >= ELM_THUMB_ANIMATION_LAST);
635 wd->anim_setting = setting;
636 if (setting == ELM_THUMB_ANIMATION_LOOP)
637 edje_object_signal_emit(wd->view, "animate_loop", "");
638 else if (setting == ELM_THUMB_ANIMATION_START)
639 edje_object_signal_emit(wd->view, "animate", "");
640 else if (setting == ELM_THUMB_ANIMATION_STOP)
641 edje_object_signal_emit(wd->view, "animate_stop", "");
645 * Get the animation state for the thumb object.
647 * @param obj The thumb object.
648 * @return getting The animation setting or @c ELM_THUMB_ANIMATION_LAST,
651 * @see elm_thumb_file_get()
655 EAPI Elm_Thumb_Animation_Setting
656 elm_thumb_animate_get(const Evas_Object *obj)
658 ELM_CHECK_WIDTYPE(obj, widtype) ELM_THUMB_ANIMATION_LAST;
659 Widget_Data *wd = elm_widget_data_get(obj);
661 return wd->anim_setting;
665 * Get the ethumb_client handle so custom configuration can be made.
666 * This must be called before the objects are created to be sure no object is
667 * visible and no generation started.
669 * @return Ethumb_Client instance or NULL.
674 * #include <Elementary.h>
675 * #ifndef ELM_LIB_QUICKLAUNCH
677 * elm_main(int argc, char **argv)
679 * Ethumb_Client *client;
685 * client = elm_thumb_ethumb_client_get();
688 * ERR("could not get ethumb_client");
691 * ethumb_client_size_set(client, 100, 100);
692 * ethumb_client_crop_align_set(client, 0.5, 0.5);
695 * // Create elm_thumb objects here
708 elm_thumb_ethumb_client_get(void)
710 return _elm_ethumb_client;
714 * Get the ethumb_client connection state.
716 * @return EINA_TRUE if the client is connected to the server or
717 * EINA_FALSE otherwise.
720 elm_thumb_ethumb_client_connected(void)
722 return _elm_ethumb_connected;
726 elm_thumb_editable_set(Evas_Object *obj, Eina_Bool edit)
728 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
729 Widget_Data *wd = elm_widget_data_get(obj);
731 if (!wd) return EINA_FALSE;
733 if (wd->edit == edit) return EINA_TRUE;
737 elm_drop_target_add(obj, ELM_SEL_FORMAT_IMAGE,
738 _elm_thumb_dropcb, obj);
740 elm_drop_target_del(obj);
746 elm_thumb_editable_get(const Evas_Object *obj)
748 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
749 Widget_Data *wd = elm_widget_data_get(obj);
751 if (!wd) return EINA_FALSE;
755 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/