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
18 * clicked,double - This is called when a user has double-clicked the thumb.
20 * press - This is called when a user has pressed down the thumb.
22 * generate,start - The thumbnail generation started.
24 * generate,stop - The generation process stopped.
26 * generate,error - The generation failed.
28 * load,error - The thumbnail image loading failed.
31 typedef struct _Widget_Data Widget_Data;
46 Ecore_Event_Handler *eeh;
47 Elm_Thumb_Animation_Setting anim_setting;
48 Eina_Bool on_hold : 1;
49 Eina_Bool is_video : 1;
50 Eina_Bool was_video : 1;
54 static const char *widtype = NULL;
56 #define SIG_CLICKED "clicked"
57 #define SIG_CLICKED_DOUBLE "clicked,double"
58 #define SIG_GENERATE_ERROR "generate,error"
59 #define SIG_GENERATE_START "generate,start"
60 #define SIG_GENERATE_STOP "generate,stop"
61 #define SIG_LOAD_ERROR "load,error"
62 #define SIG_PRESS "press"
64 static const Evas_Smart_Cb_Description _signals[] =
67 {SIG_CLICKED_DOUBLE, ""},
68 {SIG_GENERATE_ERROR, ""},
69 {SIG_GENERATE_START, ""},
70 {SIG_GENERATE_STOP, ""},
76 #define EDJE_SIGNAL_GENERATE_START "elm,thumb,generate,start"
77 #define EDJE_SIGNAL_GENERATE_STOP "elm,thumb,generate,stop"
78 #define EDJE_SIGNAL_GENERATE_ERROR "elm,thumb,generate,error"
79 #define EDJE_SIGNAL_LOAD_ERROR "elm,thumb,load,error"
80 #define EDJE_SIGNAL_PULSE_START "elm,state,pulse,start"
81 #define EDJE_SIGNAL_PULSE_STOP "elm,state,pulse,stop"
83 struct _Ethumb_Client *_elm_ethumb_client = NULL;
84 Eina_Bool _elm_ethumb_connected = EINA_FALSE;
86 EAPI int ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
89 _del_hook(Evas_Object *obj)
91 Widget_Data *wd = elm_widget_data_get(obj);
93 #ifdef HAVE_ELEMENTARY_ETHUMB
94 if (wd->thumb.id >= 0)
95 ethumb_client_generate_cancel(_elm_ethumb_client, wd->thumb.id,
99 eina_stringshare_del(wd->file);
100 eina_stringshare_del(wd->key);
101 if (wd->eeh) ecore_event_handler_del(wd->eeh);
106 _theme_hook(Evas_Object *obj)
108 Widget_Data *wd = elm_widget_data_get(obj);
109 _elm_theme_object_set(obj, wd->frame, "thumb", "base",
110 elm_widget_style_get(obj));
113 #ifdef HAVE_ELEMENTARY_ETHUMB
115 _mouse_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
117 Widget_Data *wd = data;
118 Evas_Event_Mouse_Down *ev = event_info;
122 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
123 wd->on_hold = EINA_TRUE;
125 wd->on_hold = EINA_FALSE;
126 if (ev->flags & EVAS_BUTTON_DOUBLE_CLICK)
127 evas_object_smart_callback_call(wd->self, SIG_CLICKED_DOUBLE, NULL);
129 evas_object_smart_callback_call(wd->self, SIG_PRESS, NULL);
133 _mouse_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
135 Widget_Data *wd = data;
136 Evas_Event_Mouse_Up *ev = event_info;
140 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
141 wd->on_hold = EINA_TRUE;
143 wd->on_hold = EINA_FALSE;
145 evas_object_smart_callback_call(wd->self, SIG_CLICKED, NULL);
146 wd->on_hold = EINA_FALSE;
150 _finished_thumb(Widget_Data *wd, const char *thumb_path, const char *thumb_key)
152 Eina_Bool new_view = EINA_FALSE;
157 evas = evas_object_evas_get(wd->self);
158 if ((wd->view) && (wd->is_video ^ wd->was_video))
160 evas_object_del(wd->view);
163 wd->was_video = wd->is_video;
165 if ((wd->is_video) &&
166 (ethumb_client_format_get(_elm_ethumb_client) == ETHUMB_THUMB_EET))
170 wd->view = edje_object_add(evas);
173 ERR("could not create edje object");
176 new_view = EINA_TRUE;
179 if (!edje_object_file_set(wd->view, thumb_path, "movie/thumb"))
181 ERR("could not set file=%s key=%s for %s", thumb_path, thumb_key,
190 wd->view = evas_object_image_filled_add(evas);
193 ERR("could not create image object");
196 new_view = EINA_TRUE;
199 evas_object_image_file_set(wd->view, thumb_path, thumb_key);
200 r = evas_object_image_load_error_get(wd->view);
201 if (r != EVAS_LOAD_ERROR_NONE)
203 ERR("%s: %s", thumb_path, evas_load_error_str(r));
208 if (new_view) elm_widget_sub_object_add(wd->self, wd->view);
209 edje_object_part_swallow(wd->frame, "elm.swallow.content", wd->view);
210 edje_object_size_min_get(wd->frame, &mw, &mh);
211 edje_object_size_min_restricted_calc(wd->frame, &mw, &mh, mw, mh);
212 evas_object_size_hint_min_set(wd->self, mw, mh);
213 eina_stringshare_replace(&(wd->thumb.file), thumb_path);
214 eina_stringshare_replace(&(wd->thumb.key), thumb_key);
215 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_STOP, "elm");
216 evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
220 evas_object_del(wd->view);
223 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_LOAD_ERROR, "elm");
224 evas_object_smart_callback_call(wd->self, SIG_LOAD_ERROR, NULL);
228 _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)
230 Widget_Data *wd = data;
232 EINA_SAFETY_ON_FALSE_RETURN(wd->thumb.id == id);
235 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_PULSE_STOP, "elm");
239 _finished_thumb(wd, thumb_path, thumb_key);
243 ERR("could not generate thumbnail for %s (key: %s)", file, key ? key : "");
244 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_ERROR, "elm");
245 evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
249 _thumb_apply(Widget_Data *wd)
251 if (wd->thumb.id > 0)
253 ethumb_client_generate_cancel
254 (_elm_ethumb_client, wd->thumb.id, NULL, NULL, NULL);
258 if (!wd->file) return;
260 ethumb_client_file_set(_elm_ethumb_client, wd->file, wd->key);
261 if (ethumb_client_thumb_exists(_elm_ethumb_client))
263 const char *thumb_path, *thumb_key;
266 ethumb_client_thumb_path_get(_elm_ethumb_client, &thumb_path,
268 _finished_thumb(wd, thumb_path, thumb_key);
271 else if ((wd->thumb.id = ethumb_client_generate
272 (_elm_ethumb_client, _finished_thumb_cb, wd, NULL)) != -1)
274 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_PULSE_START, "elm");
275 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_START, "elm");
276 evas_object_smart_callback_call(wd->self, SIG_GENERATE_START, NULL);
281 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_ERROR, "elm");
282 evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
287 _thumb_apply_cb(void *data, int type __UNUSED__, void *ev __UNUSED__)
290 return ECORE_CALLBACK_RENEW;
294 _thumb_show(Widget_Data *wd)
296 evas_object_show(wd->frame);
298 if (elm_thumb_ethumb_client_connected())
305 wd->eeh = ecore_event_handler_add(ELM_ECORE_EVENT_ETHUMB_CONNECT,
306 _thumb_apply_cb, wd);
310 _thumb_show_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
316 _thumb_hide_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
318 Widget_Data *wd = data;
320 evas_object_hide(wd->frame);
322 if (wd->thumb.id >= 0)
324 ethumb_client_generate_cancel
325 (_elm_ethumb_client, wd->thumb.id, NULL, NULL, NULL);
328 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_STOP, "elm");
329 evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
334 ecore_event_handler_del(wd->eeh);
342 static int _elm_need_ethumb = 0;
344 static void _on_die_cb(void *, Ethumb_Client *);
347 _connect_cb(void *data __UNUSED__, Ethumb_Client *c, Eina_Bool success)
351 ethumb_client_on_server_die_callback_set(c, _on_die_cb, NULL, NULL);
352 _elm_ethumb_connected = EINA_TRUE;
353 ecore_event_add(ELM_ECORE_EVENT_ETHUMB_CONNECT, NULL, NULL, NULL);
356 _elm_ethumb_client = NULL;
360 _on_die_cb(void *data __UNUSED__, Ethumb_Client *c __UNUSED__)
362 ethumb_client_disconnect(_elm_ethumb_client);
363 _elm_ethumb_client = NULL;
364 _elm_ethumb_connected = EINA_FALSE;
365 _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
370 _elm_unneed_ethumb(void)
373 if (--_elm_need_ethumb) return;
375 ethumb_client_disconnect(_elm_ethumb_client);
376 _elm_ethumb_client = NULL;
377 ethumb_client_shutdown();
378 ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
383 _elm_thumb_dropcb(void *data __UNUSED__, Evas_Object *o, Elm_Selection_Data *drop)
385 if ((!o) || (!drop) || (!drop->data)) return EINA_FALSE;
386 elm_thumb_file_set(o, drop->data, NULL);
391 * This must be called before any other function that handle with
392 * elm_thumb objects or ethumb_client instances.
397 elm_need_ethumb(void)
400 if (_elm_need_ethumb++) return EINA_TRUE;
401 ELM_ECORE_EVENT_ETHUMB_CONNECT = ecore_event_type_new();
402 ethumb_client_init();
403 _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
411 * Add a new thumb object to the parent.
413 * @param parent The parent object.
414 * @return The new object or NULL if it cannot be created.
416 * @see elm_thumb_file_set()
417 * @see elm_thumb_ethumb_client_get()
422 elm_thumb_add(Evas_Object *parent)
427 Evas_Coord minw, minh;
429 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
431 wd = ELM_NEW(Widget_Data);
432 evas = evas_object_evas_get(parent);
433 if (!evas) return NULL;
434 obj = elm_widget_add(evas);
435 ELM_SET_WIDTYPE(widtype, "thumb");
436 elm_widget_type_set(obj, "thumb");
437 elm_widget_sub_object_add(parent, obj);
438 elm_widget_data_set(obj, wd);
439 elm_widget_del_hook_set(obj, _del_hook);
440 elm_widget_theme_hook_set(obj, _theme_hook);
441 elm_widget_can_focus_set(obj, EINA_FALSE);
443 wd->frame = edje_object_add(evas);
444 _elm_theme_object_set(obj, wd->frame, "thumb", "base", "default");
445 elm_widget_resize_object_set(obj, wd->frame);
447 edje_object_size_min_calc(obj, &minw, &minh);
448 evas_object_size_hint_min_set(obj, minw, minh);
456 wd->on_hold = EINA_FALSE;
457 wd->is_video = EINA_FALSE;
458 wd->was_video = EINA_FALSE;
460 #ifdef HAVE_ELEMENTARY_ETHUMB
461 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_DOWN,
463 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
465 evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW,
467 evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE,
471 // TODO: convert Elementary to subclassing of Evas_Smart_Class
472 // TODO: and save some bytes, making descriptions per-class and not instance!
473 evas_object_smart_callbacks_descriptions_set(obj, _signals);
478 * Reload thumbnail if it was generated before.
480 * This is useful if the ethumb client configuration changed, like its
481 * size, aspect or any other property one set in the handle returned
482 * by elm_thumb_ethumb_client_get().
484 * @param obj The thumb object to reload
486 * @see elm_thumb_file_set()
491 elm_thumb_reload(Evas_Object *obj)
493 ELM_CHECK_WIDTYPE(obj, widtype);
494 Widget_Data *wd = elm_widget_data_get(obj);
496 eina_stringshare_replace(&(wd->thumb.file), NULL);
497 eina_stringshare_replace(&(wd->thumb.key), NULL);
499 #ifdef HAVE_ELEMENTARY_ETHUMB
500 if (evas_object_visible_get(obj))
506 * Set the file that will be used as thumbnail.
508 * The file can be an image or a video (in that case, acceptable extensions are:
509 * avi, mp4, ogv, mov, mpg and wmv). To start the video animation, use the
510 * function elm_thumb_animate().
512 * @param obj The thumb object.
513 * @param file The path to file that will be used as thumb.
514 * @param key The key used in case of an EET file.
516 * @see elm_thumb_file_get()
517 * @see elm_thumb_reload()
518 * @see elm_thumb_animate()
523 elm_thumb_file_set(Evas_Object *obj, const char *file, const char *key)
525 ELM_CHECK_WIDTYPE(obj, widtype);
526 Eina_Bool file_replaced, key_replaced;
527 Widget_Data *wd = elm_widget_data_get(obj);
529 file_replaced = eina_stringshare_replace(&(wd->file), file);
530 key_replaced = eina_stringshare_replace(&(wd->key), key);
535 const char **ext, *ptr;
536 static const char *extensions[] =
538 ".avi", ".mp4", ".ogv", ".mov", ".mpg", ".wmv", NULL
541 prefix_size = eina_stringshare_strlen(wd->file) - 4;
542 if (prefix_size >= 0)
544 ptr = wd->file + prefix_size;
545 wd->is_video = EINA_FALSE;
546 for (ext = extensions; *ext; ext++)
547 if (!strcasecmp(ptr, *ext))
549 wd->is_video = EINA_TRUE;
555 eina_stringshare_replace(&(wd->thumb.file), NULL);
556 eina_stringshare_replace(&(wd->thumb.key), NULL);
558 #ifdef HAVE_ELEMENTARY_ETHUMB
559 if (((file_replaced) || (key_replaced)) && (evas_object_visible_get(obj)))
565 * Get the image or video path and key used to generate the thumbnail.
567 * @param obj The thumb object.
568 * @param file Pointer to filename.
569 * @param key Pointer to key.
571 * @see elm_thumb_file_set()
572 * @see elm_thumb_path_get()
573 * @see elm_thumb_animate()
578 elm_thumb_file_get(const Evas_Object *obj, const char **file, const char **key)
580 ELM_CHECK_WIDTYPE(obj, widtype);
581 Widget_Data *wd = elm_widget_data_get(obj);
590 * Get the path and key to the image or video generated by ethumb.
592 * One just need to make sure that the thumbnail was generated before getting
593 * its path; otherwise, the path will be NULL. One way to do that is by asking
594 * for the path when/after the "generate,stop" smart callback is called.
596 * @param obj The thumb object.
597 * @param file Pointer to thumb path.
598 * @param key Pointer to thumb key.
600 * @see elm_thumb_file_get()
605 elm_thumb_path_get(const Evas_Object *obj, const char **file, const char **key)
607 ELM_CHECK_WIDTYPE(obj, widtype);
608 Widget_Data *wd = elm_widget_data_get(obj);
611 *file = wd->thumb.file;
613 *key = wd->thumb.key;
617 * Set the animation state for the thumb object. If its content is an animated
618 * video, you may start/stop the animation or tell it to play continuously and
621 * @param obj The thumb object.
622 * @param setting The animation setting.
624 * @see elm_thumb_file_set()
629 elm_thumb_animate_set(Evas_Object *obj, Elm_Thumb_Animation_Setting setting)
631 ELM_CHECK_WIDTYPE(obj, widtype);
632 Widget_Data *wd = elm_widget_data_get(obj);
634 EINA_SAFETY_ON_TRUE_RETURN(setting >= ELM_THUMB_ANIMATION_LAST);
636 wd->anim_setting = setting;
637 if (setting == ELM_THUMB_ANIMATION_LOOP)
638 edje_object_signal_emit(wd->view, "animate_loop", "");
639 else if (setting == ELM_THUMB_ANIMATION_START)
640 edje_object_signal_emit(wd->view, "animate", "");
641 else if (setting == ELM_THUMB_ANIMATION_STOP)
642 edje_object_signal_emit(wd->view, "animate_stop", "");
646 * Get the animation state for the thumb object.
648 * @param obj The thumb object.
649 * @return getting The animation setting or @c ELM_THUMB_ANIMATION_LAST,
652 * @see elm_thumb_file_get()
656 EAPI Elm_Thumb_Animation_Setting
657 elm_thumb_animate_get(const Evas_Object *obj)
659 ELM_CHECK_WIDTYPE(obj, widtype) ELM_THUMB_ANIMATION_LAST;
660 Widget_Data *wd = elm_widget_data_get(obj);
662 return wd->anim_setting;
666 * Get the ethumb_client handle so custom configuration can be made.
667 * This must be called before the objects are created to be sure no object is
668 * visible and no generation started.
670 * @return Ethumb_Client instance or NULL.
675 * #include <Elementary.h>
676 * #ifndef ELM_LIB_QUICKLAUNCH
678 * elm_main(int argc, char **argv)
680 * Ethumb_Client *client;
686 * client = elm_thumb_ethumb_client_get();
689 * ERR("could not get ethumb_client");
692 * ethumb_client_size_set(client, 100, 100);
693 * ethumb_client_crop_align_set(client, 0.5, 0.5);
696 * // Create elm_thumb objects here
709 elm_thumb_ethumb_client_get(void)
711 return _elm_ethumb_client;
715 * Get the ethumb_client connection state.
717 * @return EINA_TRUE if the client is connected to the server or
718 * EINA_FALSE otherwise.
721 elm_thumb_ethumb_client_connected(void)
723 return _elm_ethumb_connected;
727 elm_thumb_editable_set(Evas_Object *obj, Eina_Bool edit)
729 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
730 Widget_Data *wd = elm_widget_data_get(obj);
732 if (!wd) return EINA_FALSE;
734 if (wd->edit == edit) return EINA_TRUE;
738 elm_drop_target_add(obj, ELM_SEL_FORMAT_IMAGE,
739 _elm_thumb_dropcb, obj);
741 elm_drop_target_del(obj);
747 elm_thumb_editable_get(const Evas_Object *obj)
749 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
750 Widget_Data *wd = elm_widget_data_get(obj);
752 if (!wd) return EINA_FALSE;
756 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/