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
16 * "clicked,double" - This is called when a user has double-clicked the thumb.
17 * "press" - This is called when a user has pressed down the thumb.
18 * "generate,start" - The thumbnail generation started.
19 * "generate,stop" - The generation process stopped.
20 * "generate,error" - The generation failed.
21 * "load,error" - The thumbnail image loading failed.
24 typedef struct _Widget_Data Widget_Data;
38 Ethumb_Exists *exists;
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 static const char SIG_CLICKED[] = "clicked";
51 static const char SIG_CLICKED_DOUBLE[] = "clicked,double";
52 static const char SIG_GENERATE_ERROR[] = "generate,error";
53 static const char SIG_GENERATE_START[] = "generate,start";
54 static const char SIG_GENERATE_STOP[] = "generate,stop";
55 static const char SIG_LOAD_ERROR[] = "load,error";
56 static const char 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 ethumb_client_thumb_exists_cancel(wd->thumb.exists);
94 wd->thumb.exists = NULL;
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_exists(Ethumb_Client *client __UNUSED__, Ethumb_Exists *thread,
249 Eina_Bool exists, void *data)
251 Widget_Data *wd = data;
253 if (ethumb_client_thumb_exists_check(thread))
258 const char *thumb_path, *thumb_key;
261 ethumb_client_thumb_path_get(_elm_ethumb_client, &thumb_path,
263 _finished_thumb(wd, thumb_path, thumb_key);
266 else if ((wd->thumb.id = ethumb_client_generate
267 (_elm_ethumb_client, _finished_thumb_cb, wd, NULL)) != -1)
269 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_PULSE_START, "elm");
270 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_START, "elm");
271 evas_object_smart_callback_call(wd->self, SIG_GENERATE_START, NULL);
276 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_ERROR, "elm");
277 evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
283 _thumb_apply(Widget_Data *wd)
285 if (wd->thumb.id > 0)
287 ethumb_client_generate_cancel
288 (_elm_ethumb_client, wd->thumb.id, NULL, NULL, NULL);
292 if (wd->thumb.exists)
294 ethumb_client_thumb_exists_cancel(wd->thumb.exists);
295 wd->thumb.exists = NULL;
298 if (!wd->file) return;
300 ethumb_client_file_set(_elm_ethumb_client, wd->file, wd->key);
301 ethumb_client_thumb_exists(_elm_ethumb_client, _thumb_exists, wd);
305 _thumb_apply_cb(void *data, int type __UNUSED__, void *ev __UNUSED__)
308 return ECORE_CALLBACK_RENEW;
312 _thumb_show(Widget_Data *wd)
314 evas_object_show(wd->frame);
316 if (elm_thumb_ethumb_client_connected())
323 wd->eeh = ecore_event_handler_add(ELM_ECORE_EVENT_ETHUMB_CONNECT,
324 _thumb_apply_cb, wd);
328 _thumb_show_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
334 _thumb_hide_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
336 Widget_Data *wd = data;
338 evas_object_hide(wd->frame);
340 if (wd->thumb.id >= 0)
342 ethumb_client_generate_cancel
343 (_elm_ethumb_client, wd->thumb.id, NULL, NULL, NULL);
346 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_STOP, "elm");
347 evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
350 if (wd->thumb.exists)
352 ethumb_client_thumb_exists_cancel(wd->thumb.exists);
353 wd->thumb.exists = NULL;
358 ecore_event_handler_del(wd->eeh);
366 static int _elm_need_ethumb = 0;
368 static void _on_die_cb(void *, Ethumb_Client *);
371 _connect_cb(void *data __UNUSED__, Ethumb_Client *c, Eina_Bool success)
375 ethumb_client_on_server_die_callback_set(c, _on_die_cb, NULL, NULL);
376 _elm_ethumb_connected = EINA_TRUE;
377 ecore_event_add(ELM_ECORE_EVENT_ETHUMB_CONNECT, NULL, NULL, NULL);
380 _elm_ethumb_client = NULL;
384 _on_die_cb(void *data __UNUSED__, Ethumb_Client *c __UNUSED__)
386 ethumb_client_disconnect(_elm_ethumb_client);
387 _elm_ethumb_client = NULL;
388 _elm_ethumb_connected = EINA_FALSE;
389 _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
394 _elm_unneed_ethumb(void)
397 if (--_elm_need_ethumb) return;
399 ethumb_client_disconnect(_elm_ethumb_client);
400 _elm_ethumb_client = NULL;
401 ethumb_client_shutdown();
402 ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
407 _elm_thumb_dropcb(void *data __UNUSED__, Evas_Object *o, Elm_Selection_Data *drop)
409 if ((!o) || (!drop) || (!drop->data)) return EINA_FALSE;
410 elm_thumb_file_set(o, drop->data, NULL);
415 * This must be called before any other function that handle with
416 * elm_thumb objects or ethumb_client instances.
421 elm_need_ethumb(void)
424 if (_elm_need_ethumb++) return EINA_TRUE;
425 ELM_ECORE_EVENT_ETHUMB_CONNECT = ecore_event_type_new();
426 ethumb_client_init();
427 _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
435 * Add a new thumb object to the parent.
437 * @param parent The parent object.
438 * @return The new object or NULL if it cannot be created.
440 * @see elm_thumb_file_set()
441 * @see elm_thumb_ethumb_client_get()
446 elm_thumb_add(Evas_Object *parent)
451 Evas_Coord minw, minh;
453 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
455 ELM_SET_WIDTYPE(widtype, "thumb");
456 elm_widget_type_set(obj, "thumb");
457 elm_widget_sub_object_add(parent, obj);
458 elm_widget_data_set(obj, wd);
459 elm_widget_del_hook_set(obj, _del_hook);
460 elm_widget_theme_hook_set(obj, _theme_hook);
461 elm_widget_can_focus_set(obj, EINA_FALSE);
463 wd->frame = edje_object_add(e);
464 _elm_theme_object_set(obj, wd->frame, "thumb", "base", "default");
465 elm_widget_resize_object_set(obj, wd->frame);
467 edje_object_size_min_calc(obj, &minw, &minh);
468 evas_object_size_hint_min_set(obj, minw, minh);
476 wd->thumb.exists = NULL;
477 wd->on_hold = EINA_FALSE;
478 wd->is_video = EINA_FALSE;
479 wd->was_video = EINA_FALSE;
481 #ifdef HAVE_ELEMENTARY_ETHUMB
482 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_DOWN,
484 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
486 evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW,
488 evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE,
492 // TODO: convert Elementary to subclassing of Evas_Smart_Class
493 // TODO: and save some bytes, making descriptions per-class and not instance!
494 evas_object_smart_callbacks_descriptions_set(obj, _signals);
499 * Reload thumbnail if it was generated before.
501 * This is useful if the ethumb client configuration changed, like its
502 * size, aspect or any other property one set in the handle returned
503 * by elm_thumb_ethumb_client_get().
505 * @param obj The thumb object to reload
507 * @see elm_thumb_file_set()
512 elm_thumb_reload(Evas_Object *obj)
514 ELM_CHECK_WIDTYPE(obj, widtype);
515 Widget_Data *wd = elm_widget_data_get(obj);
517 eina_stringshare_replace(&(wd->thumb.file), NULL);
518 eina_stringshare_replace(&(wd->thumb.key), NULL);
520 #ifdef HAVE_ELEMENTARY_ETHUMB
521 if (evas_object_visible_get(obj))
527 * Set the file that will be used as thumbnail.
529 * The file can be an image or a video (in that case, acceptable extensions are:
530 * avi, mp4, ogv, mov, mpg and wmv). To start the video animation, use the
531 * function elm_thumb_animate().
533 * @param obj The thumb object.
534 * @param file The path to file that will be used as thumb.
535 * @param key The key used in case of an EET file.
537 * @see elm_thumb_file_get()
538 * @see elm_thumb_reload()
539 * @see elm_thumb_animate()
544 elm_thumb_file_set(Evas_Object *obj, const char *file, const char *key)
546 ELM_CHECK_WIDTYPE(obj, widtype);
547 Eina_Bool file_replaced, key_replaced;
548 Widget_Data *wd = elm_widget_data_get(obj);
550 file_replaced = eina_stringshare_replace(&(wd->file), file);
551 key_replaced = eina_stringshare_replace(&(wd->key), key);
556 const char **ext, *ptr;
557 static const char *extensions[] =
559 ".avi", ".mp4", ".ogv", ".mov", ".mpg", ".wmv", NULL
562 prefix_size = eina_stringshare_strlen(wd->file) - 4;
563 if (prefix_size >= 0)
565 ptr = wd->file + prefix_size;
566 wd->is_video = EINA_FALSE;
567 for (ext = extensions; *ext; ext++)
568 if (!strcasecmp(ptr, *ext))
570 wd->is_video = EINA_TRUE;
576 eina_stringshare_replace(&(wd->thumb.file), NULL);
577 eina_stringshare_replace(&(wd->thumb.key), NULL);
579 #ifdef HAVE_ELEMENTARY_ETHUMB
580 if (((file_replaced) || (key_replaced)) && (evas_object_visible_get(obj)))
586 * Get the image or video path and key used to generate the thumbnail.
588 * @param obj The thumb object.
589 * @param file Pointer to filename.
590 * @param key Pointer to key.
592 * @see elm_thumb_file_set()
593 * @see elm_thumb_path_get()
594 * @see elm_thumb_animate()
599 elm_thumb_file_get(const Evas_Object *obj, const char **file, const char **key)
601 ELM_CHECK_WIDTYPE(obj, widtype);
602 Widget_Data *wd = elm_widget_data_get(obj);
611 * Get the path and key to the image or video generated by ethumb.
613 * One just need to make sure that the thumbnail was generated before getting
614 * its path; otherwise, the path will be NULL. One way to do that is by asking
615 * for the path when/after the "generate,stop" smart callback is called.
617 * @param obj The thumb object.
618 * @param file Pointer to thumb path.
619 * @param key Pointer to thumb key.
621 * @see elm_thumb_file_get()
626 elm_thumb_path_get(const Evas_Object *obj, const char **file, const char **key)
628 ELM_CHECK_WIDTYPE(obj, widtype);
629 Widget_Data *wd = elm_widget_data_get(obj);
632 *file = wd->thumb.file;
634 *key = wd->thumb.key;
638 * Set the animation state for the thumb object. If its content is an animated
639 * video, you may start/stop the animation or tell it to play continuously and
642 * @param obj The thumb object.
643 * @param setting The animation setting.
645 * @see elm_thumb_file_set()
650 elm_thumb_animate_set(Evas_Object *obj, Elm_Thumb_Animation_Setting setting)
652 ELM_CHECK_WIDTYPE(obj, widtype);
653 Widget_Data *wd = elm_widget_data_get(obj);
655 EINA_SAFETY_ON_TRUE_RETURN(setting >= ELM_THUMB_ANIMATION_LAST);
657 wd->anim_setting = setting;
658 if (setting == ELM_THUMB_ANIMATION_LOOP)
659 edje_object_signal_emit(wd->view, "animate_loop", "");
660 else if (setting == ELM_THUMB_ANIMATION_START)
661 edje_object_signal_emit(wd->view, "animate", "");
662 else if (setting == ELM_THUMB_ANIMATION_STOP)
663 edje_object_signal_emit(wd->view, "animate_stop", "");
667 * Get the animation state for the thumb object.
669 * @param obj The thumb object.
670 * @return getting The animation setting or @c ELM_THUMB_ANIMATION_LAST,
673 * @see elm_thumb_file_get()
677 EAPI Elm_Thumb_Animation_Setting
678 elm_thumb_animate_get(const Evas_Object *obj)
680 ELM_CHECK_WIDTYPE(obj, widtype) ELM_THUMB_ANIMATION_LAST;
681 Widget_Data *wd = elm_widget_data_get(obj);
683 return wd->anim_setting;
687 * Get the ethumb_client handle so custom configuration can be made.
688 * This must be called before the objects are created to be sure no object is
689 * visible and no generation started.
691 * @return Ethumb_Client instance or NULL.
696 * #include <Elementary.h>
697 * #ifndef ELM_LIB_QUICKLAUNCH
699 * elm_main(int argc, char **argv)
701 * Ethumb_Client *client;
707 * client = elm_thumb_ethumb_client_get();
710 * ERR("could not get ethumb_client");
713 * ethumb_client_size_set(client, 100, 100);
714 * ethumb_client_crop_align_set(client, 0.5, 0.5);
717 * // Create elm_thumb objects here
730 elm_thumb_ethumb_client_get(void)
732 return _elm_ethumb_client;
736 * Get the ethumb_client connection state.
738 * @return EINA_TRUE if the client is connected to the server or
739 * EINA_FALSE otherwise.
742 elm_thumb_ethumb_client_connected(void)
744 return _elm_ethumb_connected;
748 elm_thumb_editable_set(Evas_Object *obj, Eina_Bool edit)
750 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
751 Widget_Data *wd = elm_widget_data_get(obj);
753 if (!wd) return EINA_FALSE;
755 if (wd->edit == edit) return EINA_TRUE;
759 elm_drop_target_add(obj, ELM_SEL_FORMAT_IMAGE,
760 _elm_thumb_dropcb, obj);
762 elm_drop_target_del(obj);
768 elm_thumb_editable_get(const Evas_Object *obj)
770 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
771 Widget_Data *wd = elm_widget_data_get(obj);
773 if (!wd) return EINA_FALSE;
777 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/