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;
53 static const char *widtype = NULL;
55 static const char SIG_CLICKED[] = "clicked";
56 static const char SIG_CLICKED_DOUBLE[] = "clicked,double";
57 static const char SIG_GENERATE_ERROR[] = "generate,error";
58 static const char SIG_GENERATE_START[] = "generate,start";
59 static const char SIG_GENERATE_STOP[] = "generate,stop";
60 static const char SIG_LOAD_ERROR[] = "load,error";
61 static const char SIG_PRESS[]= "press";
62 static const Evas_Smart_Cb_Description _signals[] = {
64 {SIG_CLICKED_DOUBLE, ""},
65 {SIG_GENERATE_ERROR, ""},
66 {SIG_GENERATE_START, ""},
67 {SIG_GENERATE_STOP, ""},
73 static const char EDJE_SIGNAL_GENERATE_START[] = "elm,thumb,generate,start";
74 static const char EDJE_SIGNAL_GENERATE_STOP[] = "elm,thumb,generate,stop";
75 static const char EDJE_SIGNAL_GENERATE_ERROR[] = "elm,thumb,generate,error";
76 static const char EDJE_SIGNAL_LOAD_ERROR[] = "elm,thumb,load,error";
77 static const char EDJE_SIGNAL_PULSE_START[] = "elm,state,pulse,start";
78 static const char EDJE_SIGNAL_PULSE_STOP[] = "elm,state,pulse,stop";
80 #ifdef HAVE_ELEMENTARY_ETHUMB
81 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", elm_widget_style_get(obj));
111 #ifdef HAVE_ELEMENTARY_ETHUMB
113 _mouse_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
115 Widget_Data *wd = data;
116 Evas_Event_Mouse_Down *ev = event_info;
119 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
120 wd->on_hold = EINA_TRUE;
122 wd->on_hold = EINA_FALSE;
123 if (ev->flags & EVAS_BUTTON_DOUBLE_CLICK)
124 evas_object_smart_callback_call(wd->self, SIG_CLICKED_DOUBLE, NULL);
126 evas_object_smart_callback_call(wd->self, SIG_PRESS, NULL);
130 _mouse_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
133 Widget_Data *wd = data;
134 Evas_Event_Mouse_Up *ev = event_info;
137 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
138 wd->on_hold = EINA_TRUE;
140 wd->on_hold = EINA_FALSE;
142 evas_object_smart_callback_call(wd->self, SIG_CLICKED, NULL);
143 wd->on_hold = EINA_FALSE;
147 _finished_thumb(Widget_Data *wd, const char *thumb_path, const char *thumb_key)
149 Eina_Bool new_view = EINA_FALSE;
154 evas = evas_object_evas_get(wd->self);
155 if ((wd->view) && (wd->is_video ^ wd->was_video))
157 evas_object_del(wd->view);
160 wd->was_video = wd->is_video;
162 if ((wd->is_video) &&
163 (ethumb_client_format_get(_elm_ethumb_client) == ETHUMB_THUMB_EET))
167 wd->view = edje_object_add(evas);
170 ERR("could not create edje object");
173 new_view = EINA_TRUE;
176 if (!edje_object_file_set(wd->view, thumb_path, "movie/thumb"))
178 ERR("could not set file=%s key=%s for %s", thumb_path, thumb_key,
187 wd->view = evas_object_image_filled_add(evas);
190 ERR("could not create image object");
193 new_view = EINA_TRUE;
196 evas_object_image_file_set(wd->view, thumb_path, thumb_key);
197 r = evas_object_image_load_error_get(wd->view);
198 if (r != EVAS_LOAD_ERROR_NONE)
200 ERR("%s: %s", thumb_path, evas_load_error_str(r));
205 if (new_view) elm_widget_sub_object_add(wd->self, wd->view);
206 edje_object_part_swallow(wd->frame, "elm.swallow.content", wd->view);
207 edje_object_size_min_get(wd->frame, &mw, &mh);
208 edje_object_size_min_restricted_calc(wd->frame, &mw, &mh, mw, mh);
209 evas_object_size_hint_min_set(wd->self, mw, mh);
210 eina_stringshare_replace(&(wd->thumb.file), thumb_path);
211 eina_stringshare_replace(&(wd->thumb.key), thumb_key);
212 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_STOP, "elm");
213 evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
217 evas_object_del(wd->view);
220 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_LOAD_ERROR, "elm");
221 evas_object_smart_callback_call(wd->self, SIG_LOAD_ERROR, NULL);
225 _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)
227 Widget_Data *wd = data;
229 EINA_SAFETY_ON_FALSE_RETURN(wd->thumb.id == id);
232 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_PULSE_STOP, "elm");
236 _finished_thumb(wd, thumb_path, thumb_key);
240 ERR("could not generate thumbnail for %s (key: %s)", file, key ? key : "");
241 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_ERROR, "elm");
242 evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
246 _thumb_apply(Widget_Data *wd)
248 ethumb_client_file_set(_elm_ethumb_client, wd->file, wd->key);
249 if (ethumb_client_thumb_exists(_elm_ethumb_client))
251 const char *thumb_path, *thumb_key;
253 ethumb_client_thumb_path_get(_elm_ethumb_client, &thumb_path,
255 _finished_thumb(wd, thumb_path, thumb_key);
258 else if ((wd->thumb.id = ethumb_client_generate
259 (_elm_ethumb_client, _finished_thumb_cb, wd, NULL)) != -1)
261 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_PULSE_START,
263 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_START,
265 evas_object_smart_callback_call(wd->self, SIG_GENERATE_START, NULL);
270 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_ERROR,
272 evas_object_smart_callback_call(wd->self, SIG_GENERATE_ERROR, NULL);
277 _thumb_apply_cb(void *data, int type __UNUSED__, void *ev __UNUSED__)
280 return ECORE_CALLBACK_RENEW;
284 _thumb_show(Widget_Data *wd)
286 evas_object_show(wd->frame);
288 if (elm_thumb_ethumb_client_connected())
295 wd->eeh = ecore_event_handler_add(ELM_ECORE_EVENT_ETHUMB_CONNECT,
296 _thumb_apply_cb, wd);
300 _thumb_show_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
306 _thumb_hide_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
308 Widget_Data *wd = data;
310 evas_object_hide(wd->frame);
312 if (wd->thumb.id >= 0)
314 ethumb_client_generate_cancel
315 (_elm_ethumb_client, wd->thumb.id, NULL, NULL, NULL);
318 edje_object_signal_emit(wd->frame, EDJE_SIGNAL_GENERATE_STOP, "elm");
319 evas_object_smart_callback_call(wd->self, SIG_GENERATE_STOP, NULL);
324 ecore_event_handler_del(wd->eeh);
332 static Eina_Bool _elm_need_ethumb = EINA_FALSE;
334 static void _on_die_cb(void *, Ethumb_Client *);
337 _connect_cb(void *data __UNUSED__, Ethumb_Client *c, Eina_Bool success)
341 ethumb_client_on_server_die_callback_set(c, _on_die_cb, NULL, NULL);
342 _elm_ethumb_connected = EINA_TRUE;
343 ecore_event_add(ELM_ECORE_EVENT_ETHUMB_CONNECT, NULL, NULL, NULL);
346 _elm_ethumb_client = NULL;
350 _on_die_cb(void *data __UNUSED__, Ethumb_Client *c __UNUSED__)
352 ethumb_client_disconnect(_elm_ethumb_client);
353 _elm_ethumb_client = NULL;
354 _elm_ethumb_connected = EINA_FALSE;
355 _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
360 _elm_unneed_ethumb(void)
363 if (_elm_need_ethumb)
365 _elm_need_ethumb = 0;
366 ethumb_client_disconnect(_elm_ethumb_client);
367 _elm_ethumb_client = NULL;
368 ethumb_client_shutdown();
369 ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
375 * This must be called before any other function that handle with
376 * elm_thumb objects or ethumb_client instances.
381 elm_need_ethumb(void)
384 if (_elm_need_ethumb)
386 ELM_ECORE_EVENT_ETHUMB_CONNECT = ecore_event_type_new();
387 _elm_need_ethumb = 1;
388 ethumb_client_init();
389 _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
394 * Add a new thumb object to the parent.
396 * @param parent The parent object.
397 * @return The new object or NULL if it cannot be created.
399 * @see elm_thumb_file_set()
400 * @see elm_thumb_ethumb_client_get()
405 elm_thumb_add(Evas_Object *parent)
410 Evas_Coord minw, minh;
412 wd = ELM_NEW(Widget_Data);
413 evas = evas_object_evas_get(parent);
414 obj = elm_widget_add(evas);
415 ELM_SET_WIDTYPE(widtype, "thumb");
416 elm_widget_type_set(obj, "thumb");
417 elm_widget_sub_object_add(parent, obj);
418 elm_widget_data_set(obj, wd);
419 elm_widget_del_hook_set(obj, _del_hook);
420 elm_widget_theme_hook_set(obj, _theme_hook);
422 wd->frame = edje_object_add(evas);
423 _elm_theme_object_set(obj, wd->frame, "thumb", "base", "default");
424 elm_widget_resize_object_set(obj, wd->frame);
426 edje_object_size_min_calc(obj, &minw, &minh);
427 evas_object_size_hint_min_set(obj, minw, minh);
435 wd->on_hold = EINA_FALSE;
436 wd->is_video = EINA_FALSE;
437 wd->was_video = EINA_FALSE;
439 #ifdef HAVE_ELEMENTARY_ETHUMB
440 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_DOWN,
442 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
444 evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW,
446 evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE,
450 // TODO: convert Elementary to subclassing of Evas_Smart_Class
451 // TODO: and save some bytes, making descriptions per-class and not instance!
452 evas_object_smart_callbacks_descriptions_set(obj, _signals);
457 * Set the file that will be used as thumbnail.
459 * The file can be an image or a video (in that case, acceptable extensions are:
460 * avi, mp4, ogv, mov, mpg and wmv). To start the video animation, use the
461 * function elm_thumb_animate().
463 * @param obj The thumb object.
464 * @param file The path to file that will be used as thumb.
465 * @param key The key used in case of an EET file.
467 * @see elm_thumb_file_get()
468 * @see elm_thumb_animate()
473 elm_thumb_file_set(Evas_Object *obj, const char *file, const char *key)
475 ELM_CHECK_WIDTYPE(obj, widtype);
476 Eina_Bool file_replaced, key_replaced;
477 Widget_Data *wd = elm_widget_data_get(obj);
479 file_replaced = eina_stringshare_replace(&(wd->file), file);
480 key_replaced = eina_stringshare_replace(&(wd->key), key);
485 const char **ext, *ptr;
486 static const char *extensions[] = { ".avi", ".mp4", ".ogv", ".mov",
487 ".mpg", ".wmv", NULL };
489 prefix_size = eina_stringshare_strlen(wd->file) - 4;
490 if (prefix_size >= 0)
492 ptr = wd->file + prefix_size;
493 wd->is_video = EINA_FALSE;
494 for (ext = extensions; *ext; ext++)
495 if (!strcasecmp(ptr, *ext))
497 wd->is_video = EINA_TRUE;
503 eina_stringshare_replace(&(wd->thumb.file), NULL);
504 eina_stringshare_replace(&(wd->thumb.key), NULL);
506 #ifdef HAVE_ELEMENTARY_ETHUMB
507 if ((file_replaced || key_replaced) && evas_object_visible_get(obj))
513 * Get the image or video path and key used to generate the thumbnail.
515 * @param obj The thumb object.
516 * @param file Pointer to filename.
517 * @param key Pointer to key.
519 * @see elm_thumb_file_set()
520 * @see elm_thumb_path_get()
521 * @see elm_thumb_animate()
526 elm_thumb_file_get(const Evas_Object *obj, const char **file, const char **key)
528 ELM_CHECK_WIDTYPE(obj, widtype);
529 Widget_Data *wd = elm_widget_data_get(obj);
537 * Get the path and key to the image or video generated by ethumb.
539 * One just need to make sure that the thumbnail was generated before getting
540 * its path; otherwise, the path will be NULL. One way to do that is by asking
541 * for the path when/after the "generate,stop" smart callback is called.
543 * @param obj The thumb object.
544 * @param file Pointer to thumb path.
545 * @param key Pointer to thumb key.
547 * @see elm_thumb_file_get()
552 elm_thumb_path_get(const Evas_Object *obj, const char **file, const char **key)
554 ELM_CHECK_WIDTYPE(obj, widtype);
555 Widget_Data *wd = elm_widget_data_get(obj);
557 *file = wd->thumb.file;
559 *key = wd->thumb.key;
563 * Set the animation state for the thumb object. If its content is an animated
564 * video, you may start/stop the animation or tell it to play continuously and
567 * @param obj The thumb object.
568 * @param setting The animation setting.
570 * @see elm_thumb_file_set()
575 elm_thumb_animate_set(Evas_Object *obj, Elm_Thumb_Animation_Setting setting)
577 ELM_CHECK_WIDTYPE(obj, widtype);
578 Widget_Data *wd = elm_widget_data_get(obj);
580 if (setting < ELM_THUMB_ANIMATION_START ||
581 setting >= ELM_THUMB_ANIMATION_LAST)
586 wd->anim_setting = setting;
587 if (setting == ELM_THUMB_ANIMATION_LOOP)
588 edje_object_signal_emit(wd->view, "animate_loop", "");
589 else if (setting == ELM_THUMB_ANIMATION_START)
590 edje_object_signal_emit(wd->view, "animate", "");
591 else if (setting == ELM_THUMB_ANIMATION_STOP)
592 edje_object_signal_emit(wd->view, "animate_stop", "");
596 * Get the animation state for the thumb object.
598 * @param obj The thumb object.
599 * @return getting The animation setting or @c ELM_THUMB_ANIMATION_LAST,
602 * @see elm_thumb_file_get()
606 EAPI Elm_Thumb_Animation_Setting
607 elm_thumb_animate_get(const Evas_Object *obj)
609 ELM_CHECK_WIDTYPE(obj, widtype) ELM_THUMB_ANIMATION_LAST;
610 Widget_Data *wd = elm_widget_data_get(obj);
612 return wd->anim_setting;
616 * Get the ethumb_client handle so custom configuration can be made.
617 * This must be called before the objects are created to be sure no object is
618 * visible and no generation started.
620 * @return Ethumb_Client instance or NULL.
625 * #include <Elementary.h>
626 * #ifndef ELM_LIB_QUICKLAUNCH
628 * elm_main(int argc, char **argv)
630 * Ethumb_Client *client;
636 * client = elm_thumb_ethumb_client_get();
639 * ERR("could not get ethumb_client");
642 * ethumb_client_size_set(client, 100, 100);
643 * ethumb_client_crop_align_set(client, 0.5, 0.5);
646 * // Create elm_thumb objects here
660 elm_thumb_ethumb_client_get(void)
662 return _elm_ethumb_client;
666 elm_thumb_ethumb_client_get(void)
673 * Get the ethumb_client connection state.
675 * @return EINA_TRUE if the client is connected to the server or
676 * EINA_FALSE otherwise.
679 elm_thumb_ethumb_client_connected(void)
681 return _elm_ethumb_connected;