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;
52 static const char *widtype = NULL;
54 static const char SIG_CLICKED[] = "clicked";
55 static const char SIG_CLICKED_DOUBLE[] = "clicked,double";
56 static const char SIG_GENERATE_ERROR[] = "generate,error";
57 static const char SIG_GENERATE_START[] = "generate,start";
58 static const char SIG_GENERATE_STOP[] = "generate,stop";
59 static const char SIG_LOAD_ERROR[] = "load,error";
60 static const char SIG_PRESS[]= "press";
61 static const Evas_Smart_Cb_Description _signals[] = {
63 {SIG_CLICKED_DOUBLE, ""},
64 {SIG_GENERATE_ERROR, ""},
65 {SIG_GENERATE_START, ""},
66 {SIG_GENERATE_STOP, ""},
72 static const char EDJE_SIGNAL_GENERATE_START[] = "elm,thumb,generate,start";
73 static const char EDJE_SIGNAL_GENERATE_STOP[] = "elm,thumb,generate,stop";
74 static const char EDJE_SIGNAL_GENERATE_ERROR[] = "elm,thumb,generate,error";
75 static const char EDJE_SIGNAL_LOAD_ERROR[] = "elm,thumb,load,error";
76 static const char EDJE_SIGNAL_PULSE_START[] = "elm,state,pulse,start";
77 static const char EDJE_SIGNAL_PULSE_STOP[] = "elm,state,pulse,stop";
79 #ifdef HAVE_ELEMENTARY_ETHUMB
80 Ethumb_Client *_elm_ethumb_client = NULL;
82 Eina_Bool _elm_ethumb_connected = EINA_FALSE;
84 EAPI int ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
87 _del_hook(Evas_Object *obj)
89 Widget_Data *wd = elm_widget_data_get(obj);
91 #ifdef HAVE_ELEMENTARY_ETHUMB
92 if (wd->thumb.id >= 0)
93 ethumb_client_generate_cancel(_elm_ethumb_client, wd->thumb.id,
97 eina_stringshare_del(wd->file);
98 eina_stringshare_del(wd->key);
99 if (wd->eeh) ecore_event_handler_del(wd->eeh);
104 _theme_hook(Evas_Object *obj)
106 Widget_Data *wd = elm_widget_data_get(obj);
107 _elm_theme_object_set(obj, wd->frame, "thumb", "base", elm_widget_style_get(obj));
110 #ifdef HAVE_ELEMENTARY_ETHUMB
112 _mouse_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
114 Widget_Data *wd = data;
115 Evas_Event_Mouse_Down *ev = event_info;
118 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
119 wd->on_hold = EINA_TRUE;
121 wd->on_hold = EINA_FALSE;
122 if (ev->flags & EVAS_BUTTON_DOUBLE_CLICK)
123 evas_object_smart_callback_call(wd->self, SIG_CLICKED_DOUBLE, NULL);
125 evas_object_smart_callback_call(wd->self, SIG_PRESS, NULL);
129 _mouse_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
132 Widget_Data *wd = data;
133 Evas_Event_Mouse_Up *ev = event_info;
136 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
137 wd->on_hold = EINA_TRUE;
139 wd->on_hold = EINA_FALSE;
141 evas_object_smart_callback_call(wd->self, SIG_CLICKED, NULL);
142 wd->on_hold = EINA_FALSE;
146 _finished_thumb(Widget_Data *wd, const char *thumb_path, const char *thumb_key)
148 Eina_Bool new_view = EINA_FALSE;
153 evas = evas_object_evas_get(wd->self);
154 if ((wd->view) && (wd->is_video ^ wd->was_video))
156 evas_object_del(wd->view);
159 wd->was_video = wd->is_video;
161 if ((wd->is_video) &&
162 (ethumb_client_format_get(_elm_ethumb_client) == ETHUMB_THUMB_EET))
166 wd->view = edje_object_add(evas);
169 ERR("could not create edje object");
172 new_view = EINA_TRUE;
175 if (!edje_object_file_set(wd->view, thumb_path, "movie/thumb"))
177 ERR("could not set file=%s key=%s for %s", thumb_path, thumb_key,
186 wd->view = evas_object_image_filled_add(evas);
189 ERR("could not create image object");
192 new_view = EINA_TRUE;
195 evas_object_image_file_set(wd->view, thumb_path, thumb_key);
196 r = evas_object_image_load_error_get(wd->view);
197 if (r != EVAS_LOAD_ERROR_NONE)
199 ERR("%s: %s", thumb_path, evas_load_error_str(r));
204 if (new_view) elm_widget_sub_object_add(wd->self, wd->view);
205 edje_object_part_swallow(wd->frame, "elm.swallow.content", wd->view);
206 edje_object_size_min_get(wd->frame, &mw, &mh);
207 edje_object_size_min_restricted_calc(wd->frame, &mw, &mh, mw, mh);
208 evas_object_size_hint_min_set(wd->self, mw, mh);
209 eina_stringshare_replace(&(wd->thumb.file), thumb_path);
210 eina_stringshare_replace(&(wd->thumb.key), thumb_key);
211 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_STOP, "elm");
212 evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
216 evas_object_del(wd->view);
219 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_LOAD_ERROR, "elm");
220 evas_object_smart_callback_call(wd->self, SIG_LOAD_ERROR, NULL);
224 _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)
226 Widget_Data *wd = data;
228 EINA_SAFETY_ON_FALSE_RETURN(wd->thumb.id == id);
231 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_PULSE_STOP, "elm");
235 _finished_thumb(wd, thumb_path, thumb_key);
239 ERR("could not generate thumbnail for %s (key: %s)", file, key ? key : "");
240 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_ERROR, "elm");
241 evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
245 _thumb_apply(Widget_Data *wd)
247 ethumb_client_file_set(_elm_ethumb_client, wd->file, wd->key);
248 if (ethumb_client_thumb_exists(_elm_ethumb_client))
250 const char *thumb_path, *thumb_key;
252 ethumb_client_thumb_path_get(_elm_ethumb_client, &thumb_path,
254 _finished_thumb(wd, thumb_path, thumb_key);
257 else if ((wd->thumb.id = ethumb_client_generate
258 (_elm_ethumb_client, _finished_thumb_cb, wd, NULL)) != -1)
260 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_PULSE_START,
262 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_START,
264 evas_object_smart_callback_call(wd->self, SIG_GENERATE_START, NULL);
269 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_ERROR,
271 evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
276 _thumb_apply_cb(void *data, int type __UNUSED__, void *ev __UNUSED__)
279 return ECORE_CALLBACK_RENEW;
283 _thumb_show(Widget_Data *wd)
285 evas_object_show(wd->frame);
287 if (elm_thumb_ethumb_client_connected())
294 wd->eeh = ecore_event_handler_add(ELM_ECORE_EVENT_ETHUMB_CONNECT,
295 _thumb_apply_cb, wd);
299 _thumb_show_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
305 _thumb_hide_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
307 Widget_Data *wd = data;
309 evas_object_hide(wd->frame);
311 if (wd->thumb.id >= 0)
313 ethumb_client_generate_cancel
314 (_elm_ethumb_client, wd->thumb.id, NULL, NULL, NULL);
317 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_STOP, "elm");
318 evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
323 ecore_event_handler_del(wd->eeh);
331 static Eina_Bool _elm_need_ethumb = EINA_FALSE;
333 static void _on_die_cb(void *, Ethumb_Client *);
336 _connect_cb(void *data __UNUSED__, Ethumb_Client *c, Eina_Bool success)
340 ethumb_client_on_server_die_callback_set(c, _on_die_cb, NULL, NULL);
341 _elm_ethumb_connected = EINA_TRUE;
342 ecore_event_add(ELM_ECORE_EVENT_ETHUMB_CONNECT, NULL, NULL, NULL);
345 _elm_ethumb_client = NULL;
349 _on_die_cb(void *data __UNUSED__, Ethumb_Client *c __UNUSED__)
351 ethumb_client_disconnect(_elm_ethumb_client);
352 _elm_ethumb_client = NULL;
353 _elm_ethumb_connected = EINA_FALSE;
354 _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
359 _elm_unneed_ethumb(void)
362 if (_elm_need_ethumb)
364 _elm_need_ethumb = 0;
365 ethumb_client_disconnect(_elm_ethumb_client);
366 _elm_ethumb_client = NULL;
367 ethumb_client_shutdown();
368 ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
374 * This must be called before any other function that handle with
375 * elm_thumb objects or ethumb_client instances.
380 elm_need_ethumb(void)
383 if (_elm_need_ethumb)
385 ELM_ECORE_EVENT_ETHUMB_CONNECT = ecore_event_type_new();
386 _elm_need_ethumb = 1;
387 ethumb_client_init();
388 _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
393 * Add a new thumb object to the parent.
395 * @param parent The parent object.
396 * @return The new object or NULL if it cannot be created.
398 * @see elm_thumb_file_set()
399 * @see elm_thumb_ethumb_client_get()
404 elm_thumb_add(Evas_Object *parent)
409 Evas_Coord minw, minh;
411 wd = ELM_NEW(Widget_Data);
412 evas = evas_object_evas_get(parent);
413 obj = elm_widget_add(evas);
414 ELM_SET_WIDTYPE(widtype, "thumb");
415 elm_widget_type_set(obj, "thumb");
416 elm_widget_sub_object_add(parent, obj);
417 elm_widget_data_set(obj, wd);
418 elm_widget_del_hook_set(obj, _del_hook);
419 elm_widget_theme_hook_set(obj, _theme_hook);
421 wd->frame = edje_object_add(evas);
422 _elm_theme_object_set(obj, wd->frame, "thumb", "base", "default");
423 elm_widget_resize_object_set(obj, wd->frame);
425 edje_object_size_min_calc(obj, &minw, &minh);
426 evas_object_size_hint_min_set(obj, minw, minh);
434 wd->on_hold = EINA_FALSE;
435 wd->is_video = EINA_FALSE;
436 wd->was_video = EINA_FALSE;
438 #ifdef HAVE_ELEMENTARY_ETHUMB
439 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_DOWN,
441 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
443 evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW,
445 evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE,
449 // TODO: convert Elementary to subclassing of Evas_Smart_Class
450 // TODO: and save some bytes, making descriptions per-class and not instance!
451 evas_object_smart_callbacks_descriptions_set(obj, _signals);
456 * Set the file that will be used as thumbnail.
458 * The file can be an image or a video (in that case, acceptable extensions are:
459 * avi, mp4, ogv, mov, mpg and wmv). To start the video animation, use the
460 * function elm_thumb_animate().
462 * @param obj The thumb object.
463 * @param file The path to file that will be used as thumb.
464 * @param key The key used in case of an EET file.
466 * @see elm_thumb_file_get()
467 * @see elm_thumb_animate()
472 elm_thumb_file_set(Evas_Object *obj, const char *file, const char *key)
474 ELM_CHECK_WIDTYPE(obj, widtype);
475 Eina_Bool file_replaced, key_replaced;
476 Widget_Data *wd = elm_widget_data_get(obj);
478 file_replaced = eina_stringshare_replace(&(wd->file), file);
479 key_replaced = eina_stringshare_replace(&(wd->key), key);
484 const char **ext, *ptr;
485 static const char *extensions[] = { ".avi", ".mp4", ".ogv", ".mov",
486 ".mpg", ".wmv", NULL };
488 prefix_size = eina_stringshare_strlen(wd->file) - 4;
489 if (prefix_size >= 0)
491 ptr = wd->file + prefix_size;
492 wd->is_video = EINA_FALSE;
493 for (ext = extensions; *ext; ext++)
494 if (!strcasecmp(ptr, *ext))
496 wd->is_video = EINA_TRUE;
502 eina_stringshare_replace(&(wd->thumb.file), NULL);
503 eina_stringshare_replace(&(wd->thumb.key), NULL);
505 #ifdef HAVE_ELEMENTARY_ETHUMB
506 if ((file_replaced || key_replaced) && evas_object_visible_get(obj))
512 * Get the image or video path and key used to generate the thumbnail.
514 * @param obj The thumb object.
515 * @param file Pointer to filename.
516 * @param key Pointer to key.
518 * @see elm_thumb_file_set()
519 * @see elm_thumb_path_get()
520 * @see elm_thumb_animate()
525 elm_thumb_file_get(const Evas_Object *obj, const char **file, const char **key)
527 ELM_CHECK_WIDTYPE(obj, widtype);
528 Widget_Data *wd = elm_widget_data_get(obj);
536 * Get the path and key to the image or video generated by ethumb.
538 * One just need to make sure that the thumbnail was generated before getting
539 * its path; otherwise, the path will be NULL. One way to do that is by asking
540 * for the path when/after the "generate,stop" smart callback is called.
542 * @param obj The thumb object.
543 * @param file Pointer to thumb path.
544 * @param key Pointer to thumb key.
546 * @see elm_thumb_file_get()
551 elm_thumb_path_get(const Evas_Object *obj, const char **file, const char **key)
553 ELM_CHECK_WIDTYPE(obj, widtype);
554 Widget_Data *wd = elm_widget_data_get(obj);
556 *file = wd->thumb.file;
558 *key = wd->thumb.key;
562 * Set the animation state for the thumb object. If its content is an animated
563 * video, you may start/stop the animation or tell it to play continuously and
566 * @param obj The thumb object.
567 * @param setting The animation setting.
569 * @see elm_thumb_file_set()
574 elm_thumb_animate_set(Evas_Object *obj, Elm_Thumb_Animation_Setting setting)
576 ELM_CHECK_WIDTYPE(obj, widtype);
577 Widget_Data *wd = elm_widget_data_get(obj);
579 if (setting < ELM_THUMB_ANIMATION_START ||
580 setting >= ELM_THUMB_ANIMATION_LAST)
585 wd->anim_setting = setting;
586 if (setting == ELM_THUMB_ANIMATION_LOOP)
587 edje_object_signal_emit(wd->view, "animate_loop", "");
588 else if (setting == ELM_THUMB_ANIMATION_START)
589 edje_object_signal_emit(wd->view, "animate", "");
590 else if (setting == ELM_THUMB_ANIMATION_STOP)
591 edje_object_signal_emit(wd->view, "animate_stop", "");
595 * Get the animation state for the thumb object.
597 * @param obj The thumb object.
598 * @return getting The animation setting or @c ELM_THUMB_ANIMATION_LAST,
601 * @see elm_thumb_file_get()
605 EAPI Elm_Thumb_Animation_Setting
606 elm_thumb_animate_get(const Evas_Object *obj)
608 ELM_CHECK_WIDTYPE(obj, widtype) ELM_THUMB_ANIMATION_LAST;
609 Widget_Data *wd = elm_widget_data_get(obj);
611 return wd->anim_setting;
615 * Get the ethumb_client handle so custom configuration can be made.
616 * This must be called before the objects are created to be sure no object is
617 * visible and no generation started.
619 * @return Ethumb_Client instance or NULL.
624 * #include <Elementary.h>
625 * #ifndef ELM_LIB_QUICKLAUNCH
627 * elm_main(int argc, char **argv)
629 * Ethumb_Client *client;
635 * client = elm_thumb_ethumb_client_get();
638 * ERR("could not get ethumb_client");
641 * ethumb_client_size_set(client, 100, 100);
642 * ethumb_client_crop_align_set(client, 0.5, 0.5);
645 * // Create elm_thumb objects here
659 elm_thumb_ethumb_client_get(void)
661 return _elm_ethumb_client;
665 elm_thumb_ethumb_client_get(void)
672 * Get the ethumb_client connection state.
674 * @return EINA_TRUE if the client is connected to the server or
675 * EINA_FALSE otherwise.
678 elm_thumb_ethumb_client_connected(void)
680 return _elm_ethumb_connected;