1 #include <Elementary.h>
5 * @defgroup Thumb Thumb
8 * A thumb object is used for displaying the thumbnail of an image or video.
9 * You must have compiled Elementary with Ethumb_Client support and the DBus
10 * service must be present and auto-activated in order to have thumbnails to
13 * Signals that you can add callbacks for are:
15 * "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.
18 * "press" - This is called when a user has pressed down the thumb.
19 * "generate,start" - The thumbnail generation started.
20 * "generate,stop" - The generation process stopped.
21 * "generate,error" - The generation failed.
22 * "load,error" - The thumbnail image loading failed.
25 typedef struct _Widget_Data Widget_Data;
40 Ecore_Event_Handler *eeh;
41 Elm_Thumb_Animation_Setting anim_setting;
42 Eina_Bool on_hold : 1;
43 Eina_Bool is_video : 1;
44 Eina_Bool was_video : 1;
48 static const char *widtype = NULL;
50 #define SIG_CLICKED "clicked"
51 #define SIG_CLICKED_DOUBLE "clicked,double"
52 #define SIG_GENERATE_ERROR "generate,error"
53 #define SIG_GENERATE_START "generate,start"
54 #define SIG_GENERATE_STOP "generate,stop"
55 #define SIG_LOAD_ERROR "load,error"
56 #define SIG_PRESS "press"
58 static const Evas_Smart_Cb_Description _signals[] =
61 {SIG_CLICKED_DOUBLE, ""},
62 {SIG_GENERATE_ERROR, ""},
63 {SIG_GENERATE_START, ""},
64 {SIG_GENERATE_STOP, ""},
70 #define EDJE_SIGNAL_GENERATE_START "elm,thumb,generate,start"
71 #define EDJE_SIGNAL_GENERATE_STOP "elm,thumb,generate,stop"
72 #define EDJE_SIGNAL_GENERATE_ERROR "elm,thumb,generate,error"
73 #define EDJE_SIGNAL_LOAD_ERROR "elm,thumb,load,error"
74 #define EDJE_SIGNAL_PULSE_START "elm,state,pulse,start"
75 #define EDJE_SIGNAL_PULSE_STOP "elm,state,pulse,stop"
77 struct _Ethumb_Client *_elm_ethumb_client = NULL;
78 Eina_Bool _elm_ethumb_connected = EINA_FALSE;
80 EAPI int ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
83 _del_hook(Evas_Object *obj)
85 Widget_Data *wd = elm_widget_data_get(obj);
87 #ifdef HAVE_ELEMENTARY_ETHUMB
88 if (wd->thumb.id >= 0)
89 ethumb_client_generate_cancel(_elm_ethumb_client, wd->thumb.id,
93 eina_stringshare_del(wd->file);
94 eina_stringshare_del(wd->key);
95 if (wd->eeh) ecore_event_handler_del(wd->eeh);
100 _theme_hook(Evas_Object *obj)
102 Widget_Data *wd = elm_widget_data_get(obj);
103 _elm_theme_object_set(obj, wd->frame, "thumb", "base",
104 elm_widget_style_get(obj));
107 #ifdef HAVE_ELEMENTARY_ETHUMB
109 _mouse_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
111 Widget_Data *wd = data;
112 Evas_Event_Mouse_Down *ev = event_info;
116 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
117 wd->on_hold = EINA_TRUE;
119 wd->on_hold = EINA_FALSE;
120 if (ev->flags & EVAS_BUTTON_DOUBLE_CLICK)
121 evas_object_smart_callback_call(wd->self, SIG_CLICKED_DOUBLE, NULL);
123 evas_object_smart_callback_call(wd->self, SIG_PRESS, NULL);
127 _mouse_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
129 Widget_Data *wd = data;
130 Evas_Event_Mouse_Up *ev = event_info;
134 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
135 wd->on_hold = EINA_TRUE;
137 wd->on_hold = EINA_FALSE;
139 evas_object_smart_callback_call(wd->self, SIG_CLICKED, NULL);
140 wd->on_hold = EINA_FALSE;
144 _finished_thumb(Widget_Data *wd, const char *thumb_path, const char *thumb_key)
146 Eina_Bool new_view = EINA_FALSE;
151 evas = evas_object_evas_get(wd->self);
152 if ((wd->view) && (wd->is_video ^ wd->was_video))
154 evas_object_del(wd->view);
157 wd->was_video = wd->is_video;
159 if ((wd->is_video) &&
160 (ethumb_client_format_get(_elm_ethumb_client) == ETHUMB_THUMB_EET))
164 wd->view = edje_object_add(evas);
167 ERR("could not create edje object");
170 new_view = EINA_TRUE;
173 if (!edje_object_file_set(wd->view, thumb_path, "movie/thumb"))
175 ERR("could not set file=%s key=%s for %s", thumb_path, thumb_key,
184 wd->view = evas_object_image_filled_add(evas);
187 ERR("could not create image object");
190 new_view = EINA_TRUE;
193 evas_object_image_file_set(wd->view, thumb_path, thumb_key);
194 r = evas_object_image_load_error_get(wd->view);
195 if (r != EVAS_LOAD_ERROR_NONE)
197 ERR("%s: %s", thumb_path, evas_load_error_str(r));
202 if (new_view) elm_widget_sub_object_add(wd->self, wd->view);
203 edje_object_part_swallow(wd->frame, "elm.swallow.content", wd->view);
204 edje_object_size_min_get(wd->frame, &mw, &mh);
205 edje_object_size_min_restricted_calc(wd->frame, &mw, &mh, mw, mh);
206 evas_object_size_hint_min_set(wd->self, mw, mh);
207 eina_stringshare_replace(&(wd->thumb.file), thumb_path);
208 eina_stringshare_replace(&(wd->thumb.key), thumb_key);
209 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_STOP, "elm");
210 evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
214 evas_object_del(wd->view);
217 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_LOAD_ERROR, "elm");
218 evas_object_smart_callback_call(wd->self, SIG_LOAD_ERROR, NULL);
222 _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)
224 Widget_Data *wd = data;
226 EINA_SAFETY_ON_FALSE_RETURN(wd->thumb.id == id);
229 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_PULSE_STOP, "elm");
233 _finished_thumb(wd, thumb_path, thumb_key);
237 ERR("could not generate thumbnail for %s (key: %s)", file, key ? key : "");
238 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_ERROR, "elm");
239 evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
243 _thumb_apply(Widget_Data *wd)
245 if (wd->thumb.id > 0)
247 ethumb_client_generate_cancel
248 (_elm_ethumb_client, wd->thumb.id, NULL, NULL, NULL);
252 if (!wd->file) return;
254 ethumb_client_file_set(_elm_ethumb_client, wd->file, wd->key);
255 if (ethumb_client_thumb_exists(_elm_ethumb_client))
257 const char *thumb_path, *thumb_key;
260 ethumb_client_thumb_path_get(_elm_ethumb_client, &thumb_path,
262 _finished_thumb(wd, thumb_path, thumb_key);
265 else if ((wd->thumb.id = ethumb_client_generate
266 (_elm_ethumb_client, _finished_thumb_cb, wd, NULL)) != -1)
268 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_PULSE_START, "elm");
269 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_START, "elm");
270 evas_object_smart_callback_call(wd->self, SIG_GENERATE_START, NULL);
275 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_ERROR, "elm");
276 evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
281 _thumb_apply_cb(void *data, int type __UNUSED__, void *ev __UNUSED__)
284 return ECORE_CALLBACK_RENEW;
288 _thumb_show(Widget_Data *wd)
290 evas_object_show(wd->frame);
292 if (elm_thumb_ethumb_client_connected())
299 wd->eeh = ecore_event_handler_add(ELM_ECORE_EVENT_ETHUMB_CONNECT,
300 _thumb_apply_cb, wd);
304 _thumb_show_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
310 _thumb_hide_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
312 Widget_Data *wd = data;
314 evas_object_hide(wd->frame);
316 if (wd->thumb.id >= 0)
318 ethumb_client_generate_cancel
319 (_elm_ethumb_client, wd->thumb.id, NULL, NULL, NULL);
322 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_STOP, "elm");
323 evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
328 ecore_event_handler_del(wd->eeh);
336 static int _elm_need_ethumb = 0;
338 static void _on_die_cb(void *, Ethumb_Client *);
341 _connect_cb(void *data __UNUSED__, Ethumb_Client *c, Eina_Bool success)
345 ethumb_client_on_server_die_callback_set(c, _on_die_cb, NULL, NULL);
346 _elm_ethumb_connected = EINA_TRUE;
347 ecore_event_add(ELM_ECORE_EVENT_ETHUMB_CONNECT, NULL, NULL, NULL);
350 _elm_ethumb_client = NULL;
354 _on_die_cb(void *data __UNUSED__, Ethumb_Client *c __UNUSED__)
356 ethumb_client_disconnect(_elm_ethumb_client);
357 _elm_ethumb_client = NULL;
358 _elm_ethumb_connected = EINA_FALSE;
359 _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
364 _elm_unneed_ethumb(void)
367 if (--_elm_need_ethumb) return;
369 ethumb_client_disconnect(_elm_ethumb_client);
370 _elm_ethumb_client = NULL;
371 ethumb_client_shutdown();
372 ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
377 _elm_thumb_dropcb(void *data __UNUSED__, Evas_Object *o, Elm_Selection_Data *drop)
379 if ((!o) || (!drop) || (!drop->data)) return EINA_FALSE;
380 elm_thumb_file_set(o, drop->data, NULL);
385 * This must be called before any other function that handle with
386 * elm_thumb objects or ethumb_client instances.
391 elm_need_ethumb(void)
394 if (_elm_need_ethumb++) return EINA_TRUE;
395 ELM_ECORE_EVENT_ETHUMB_CONNECT = ecore_event_type_new();
396 ethumb_client_init();
397 _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
405 * Add a new thumb object to the parent.
407 * @param parent The parent object.
408 * @return The new object or NULL if it cannot be created.
410 * @see elm_thumb_file_set()
411 * @see elm_thumb_ethumb_client_get()
416 elm_thumb_add(Evas_Object *parent)
421 Evas_Coord minw, minh;
423 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
425 ELM_SET_WIDTYPE(widtype, "thumb");
426 elm_widget_type_set(obj, "thumb");
427 elm_widget_sub_object_add(parent, obj);
428 elm_widget_data_set(obj, wd);
429 elm_widget_del_hook_set(obj, _del_hook);
430 elm_widget_theme_hook_set(obj, _theme_hook);
431 elm_widget_can_focus_set(obj, EINA_FALSE);
433 wd->frame = edje_object_add(e);
434 _elm_theme_object_set(obj, wd->frame, "thumb", "base", "default");
435 elm_widget_resize_object_set(obj, wd->frame);
437 edje_object_size_min_calc(obj, &minw, &minh);
438 evas_object_size_hint_min_set(obj, minw, minh);
446 wd->on_hold = EINA_FALSE;
447 wd->is_video = EINA_FALSE;
448 wd->was_video = EINA_FALSE;
450 #ifdef HAVE_ELEMENTARY_ETHUMB
451 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_DOWN,
453 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
455 evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW,
457 evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE,
461 // TODO: convert Elementary to subclassing of Evas_Smart_Class
462 // TODO: and save some bytes, making descriptions per-class and not instance!
463 evas_object_smart_callbacks_descriptions_set(obj, _signals);
468 * Reload thumbnail if it was generated before.
470 * This is useful if the ethumb client configuration changed, like its
471 * size, aspect or any other property one set in the handle returned
472 * by elm_thumb_ethumb_client_get().
474 * @param obj The thumb object to reload
476 * @see elm_thumb_file_set()
481 elm_thumb_reload(Evas_Object *obj)
483 ELM_CHECK_WIDTYPE(obj, widtype);
484 Widget_Data *wd = elm_widget_data_get(obj);
486 eina_stringshare_replace(&(wd->thumb.file), NULL);
487 eina_stringshare_replace(&(wd->thumb.key), NULL);
489 #ifdef HAVE_ELEMENTARY_ETHUMB
490 if (evas_object_visible_get(obj))
496 * Set the file that will be used as thumbnail.
498 * The file can be an image or a video (in that case, acceptable extensions are:
499 * avi, mp4, ogv, mov, mpg and wmv). To start the video animation, use the
500 * function elm_thumb_animate().
502 * @param obj The thumb object.
503 * @param file The path to file that will be used as thumb.
504 * @param key The key used in case of an EET file.
506 * @see elm_thumb_file_get()
507 * @see elm_thumb_reload()
508 * @see elm_thumb_animate()
513 elm_thumb_file_set(Evas_Object *obj, const char *file, const char *key)
515 ELM_CHECK_WIDTYPE(obj, widtype);
516 Eina_Bool file_replaced, key_replaced;
517 Widget_Data *wd = elm_widget_data_get(obj);
519 file_replaced = eina_stringshare_replace(&(wd->file), file);
520 key_replaced = eina_stringshare_replace(&(wd->key), key);
525 const char **ext, *ptr;
526 static const char *extensions[] =
528 ".avi", ".mp4", ".ogv", ".mov", ".mpg", ".wmv", NULL
531 prefix_size = eina_stringshare_strlen(wd->file) - 4;
532 if (prefix_size >= 0)
534 ptr = wd->file + prefix_size;
535 wd->is_video = EINA_FALSE;
536 for (ext = extensions; *ext; ext++)
537 if (!strcasecmp(ptr, *ext))
539 wd->is_video = EINA_TRUE;
545 eina_stringshare_replace(&(wd->thumb.file), NULL);
546 eina_stringshare_replace(&(wd->thumb.key), NULL);
548 #ifdef HAVE_ELEMENTARY_ETHUMB
549 if (((file_replaced) || (key_replaced)) && (evas_object_visible_get(obj)))
555 * Get the image or video path and key used to generate the thumbnail.
557 * @param obj The thumb object.
558 * @param file Pointer to filename.
559 * @param key Pointer to key.
561 * @see elm_thumb_file_set()
562 * @see elm_thumb_path_get()
563 * @see elm_thumb_animate()
568 elm_thumb_file_get(const Evas_Object *obj, const char **file, const char **key)
570 ELM_CHECK_WIDTYPE(obj, widtype);
571 Widget_Data *wd = elm_widget_data_get(obj);
580 * Get the path and key to the image or video generated by ethumb.
582 * One just need to make sure that the thumbnail was generated before getting
583 * its path; otherwise, the path will be NULL. One way to do that is by asking
584 * for the path when/after the "generate,stop" smart callback is called.
586 * @param obj The thumb object.
587 * @param file Pointer to thumb path.
588 * @param key Pointer to thumb key.
590 * @see elm_thumb_file_get()
595 elm_thumb_path_get(const Evas_Object *obj, const char **file, const char **key)
597 ELM_CHECK_WIDTYPE(obj, widtype);
598 Widget_Data *wd = elm_widget_data_get(obj);
601 *file = wd->thumb.file;
603 *key = wd->thumb.key;
607 * Set the animation state for the thumb object. If its content is an animated
608 * video, you may start/stop the animation or tell it to play continuously and
611 * @param obj The thumb object.
612 * @param setting The animation setting.
614 * @see elm_thumb_file_set()
619 elm_thumb_animate_set(Evas_Object *obj, Elm_Thumb_Animation_Setting setting)
621 ELM_CHECK_WIDTYPE(obj, widtype);
622 Widget_Data *wd = elm_widget_data_get(obj);
624 EINA_SAFETY_ON_TRUE_RETURN(setting >= ELM_THUMB_ANIMATION_LAST);
626 wd->anim_setting = setting;
627 if (setting == ELM_THUMB_ANIMATION_LOOP)
628 edje_object_signal_emit(wd->view, "animate_loop", "");
629 else if (setting == ELM_THUMB_ANIMATION_START)
630 edje_object_signal_emit(wd->view, "animate", "");
631 else if (setting == ELM_THUMB_ANIMATION_STOP)
632 edje_object_signal_emit(wd->view, "animate_stop", "");
636 * Get the animation state for the thumb object.
638 * @param obj The thumb object.
639 * @return getting The animation setting or @c ELM_THUMB_ANIMATION_LAST,
642 * @see elm_thumb_file_get()
646 EAPI Elm_Thumb_Animation_Setting
647 elm_thumb_animate_get(const Evas_Object *obj)
649 ELM_CHECK_WIDTYPE(obj, widtype) ELM_THUMB_ANIMATION_LAST;
650 Widget_Data *wd = elm_widget_data_get(obj);
652 return wd->anim_setting;
656 * Get the ethumb_client handle so custom configuration can be made.
657 * This must be called before the objects are created to be sure no object is
658 * visible and no generation started.
660 * @return Ethumb_Client instance or NULL.
665 * #include <Elementary.h>
666 * #ifndef ELM_LIB_QUICKLAUNCH
668 * elm_main(int argc, char **argv)
670 * Ethumb_Client *client;
676 * client = elm_thumb_ethumb_client_get();
679 * ERR("could not get ethumb_client");
682 * ethumb_client_size_set(client, 100, 100);
683 * ethumb_client_crop_align_set(client, 0.5, 0.5);
686 * // Create elm_thumb objects here
699 elm_thumb_ethumb_client_get(void)
701 return _elm_ethumb_client;
705 * Get the ethumb_client connection state.
707 * @return EINA_TRUE if the client is connected to the server or
708 * EINA_FALSE otherwise.
711 elm_thumb_ethumb_client_connected(void)
713 return _elm_ethumb_connected;
717 elm_thumb_editable_set(Evas_Object *obj, Eina_Bool edit)
719 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
720 Widget_Data *wd = elm_widget_data_get(obj);
722 if (!wd) return EINA_FALSE;
724 if (wd->edit == edit) return EINA_TRUE;
728 elm_drop_target_add(obj, ELM_SEL_FORMAT_IMAGE,
729 _elm_thumb_dropcb, obj);
731 elm_drop_target_del(obj);
737 elm_thumb_editable_get(const Evas_Object *obj)
739 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
740 Widget_Data *wd = elm_widget_data_get(obj);
742 if (!wd) return EINA_FALSE;
746 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/