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;
55 static const char *widtype = NULL;
57 static const char SIG_CLICKED[] = "clicked";
58 static const char SIG_CLICKED_DOUBLE[] = "clicked,double";
59 static const char SIG_GENERATE_ERROR[] = "generate,error";
60 static const char SIG_GENERATE_START[] = "generate,start";
61 static const char SIG_GENERATE_STOP[] = "generate,stop";
62 static const char SIG_LOAD_ERROR[] = "load,error";
63 static const char SIG_PRESS[]= "press";
64 static const Evas_Smart_Cb_Description _signals[] = {
66 {SIG_CLICKED_DOUBLE, ""},
67 {SIG_GENERATE_ERROR, ""},
68 {SIG_GENERATE_START, ""},
69 {SIG_GENERATE_STOP, ""},
75 static const char EDJE_SIGNAL_GENERATE_START[] = "elm,thumb,generate,start";
76 static const char EDJE_SIGNAL_GENERATE_STOP[] = "elm,thumb,generate,stop";
77 static const char EDJE_SIGNAL_GENERATE_ERROR[] = "elm,thumb,generate,error";
78 static const char EDJE_SIGNAL_LOAD_ERROR[] = "elm,thumb,load,error";
79 static const char EDJE_SIGNAL_PULSE_START[] = "elm,state,pulse,start";
80 static const char EDJE_SIGNAL_PULSE_STOP[] = "elm,state,pulse,stop";
82 struct _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 int _elm_need_ethumb = 0;
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 != 0) return;
365 ethumb_client_disconnect(_elm_ethumb_client);
366 _elm_ethumb_client = NULL;
367 ethumb_client_shutdown();
368 ELM_ECORE_EVENT_ETHUMB_CONNECT = 0;
373 _elm_thumb_dropcb(void *data __UNUSED__, Evas_Object *o, Elm_Selection_Data *drop)
375 if ((!o) || (!drop) || (!drop->data)) return EINA_FALSE;
376 elm_thumb_file_set(o, drop->data, NULL);
381 * This must be called before any other function that handle with
382 * elm_thumb objects or ethumb_client instances.
387 elm_need_ethumb(void)
390 if (_elm_need_ethumb ++ != 0) return;
391 ELM_ECORE_EVENT_ETHUMB_CONNECT = ecore_event_type_new();
392 ethumb_client_init();
393 _elm_ethumb_client = ethumb_client_connect(_connect_cb, NULL, NULL);
398 * Add a new thumb object to the parent.
400 * @param parent The parent object.
401 * @return The new object or NULL if it cannot be created.
403 * @see elm_thumb_file_set()
404 * @see elm_thumb_ethumb_client_get()
409 elm_thumb_add(Evas_Object *parent)
414 Evas_Coord minw, minh;
416 wd = ELM_NEW(Widget_Data);
417 evas = evas_object_evas_get(parent);
418 obj = elm_widget_add(evas);
419 ELM_SET_WIDTYPE(widtype, "thumb");
420 elm_widget_type_set(obj, "thumb");
421 elm_widget_sub_object_add(parent, obj);
422 elm_widget_data_set(obj, wd);
423 elm_widget_del_hook_set(obj, _del_hook);
424 elm_widget_theme_hook_set(obj, _theme_hook);
426 wd->frame = edje_object_add(evas);
427 _elm_theme_object_set(obj, wd->frame, "thumb", "base", "default");
428 elm_widget_resize_object_set(obj, wd->frame);
430 edje_object_size_min_calc(obj, &minw, &minh);
431 evas_object_size_hint_min_set(obj, minw, minh);
439 wd->on_hold = EINA_FALSE;
440 wd->is_video = EINA_FALSE;
441 wd->was_video = EINA_FALSE;
443 #ifdef HAVE_ELEMENTARY_ETHUMB
444 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_DOWN,
446 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
448 evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW,
450 evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE,
454 // TODO: convert Elementary to subclassing of Evas_Smart_Class
455 // TODO: and save some bytes, making descriptions per-class and not instance!
456 evas_object_smart_callbacks_descriptions_set(obj, _signals);
461 * Set the file that will be used as thumbnail.
463 * The file can be an image or a video (in that case, acceptable extensions are:
464 * avi, mp4, ogv, mov, mpg and wmv). To start the video animation, use the
465 * function elm_thumb_animate().
467 * @param obj The thumb object.
468 * @param file The path to file that will be used as thumb.
469 * @param key The key used in case of an EET file.
471 * @see elm_thumb_file_get()
472 * @see elm_thumb_animate()
477 elm_thumb_file_set(Evas_Object *obj, const char *file, const char *key)
479 ELM_CHECK_WIDTYPE(obj, widtype);
480 Eina_Bool file_replaced, key_replaced;
481 Widget_Data *wd = elm_widget_data_get(obj);
483 file_replaced = eina_stringshare_replace(&(wd->file), file);
484 key_replaced = eina_stringshare_replace(&(wd->key), key);
489 const char **ext, *ptr;
490 static const char *extensions[] = { ".avi", ".mp4", ".ogv", ".mov",
491 ".mpg", ".wmv", NULL };
493 prefix_size = eina_stringshare_strlen(wd->file) - 4;
494 if (prefix_size >= 0)
496 ptr = wd->file + prefix_size;
497 wd->is_video = EINA_FALSE;
498 for (ext = extensions; *ext; ext++)
499 if (!strcasecmp(ptr, *ext))
501 wd->is_video = EINA_TRUE;
507 eina_stringshare_replace(&(wd->thumb.file), NULL);
508 eina_stringshare_replace(&(wd->thumb.key), NULL);
510 #ifdef HAVE_ELEMENTARY_ETHUMB
511 if ((file_replaced || key_replaced) && evas_object_visible_get(obj))
517 * Get the image or video path and key used to generate the thumbnail.
519 * @param obj The thumb object.
520 * @param file Pointer to filename.
521 * @param key Pointer to key.
523 * @see elm_thumb_file_set()
524 * @see elm_thumb_path_get()
525 * @see elm_thumb_animate()
530 elm_thumb_file_get(const Evas_Object *obj, const char **file, const char **key)
532 ELM_CHECK_WIDTYPE(obj, widtype);
533 Widget_Data *wd = elm_widget_data_get(obj);
541 * Get the path and key to the image or video generated by ethumb.
543 * One just need to make sure that the thumbnail was generated before getting
544 * its path; otherwise, the path will be NULL. One way to do that is by asking
545 * for the path when/after the "generate,stop" smart callback is called.
547 * @param obj The thumb object.
548 * @param file Pointer to thumb path.
549 * @param key Pointer to thumb key.
551 * @see elm_thumb_file_get()
556 elm_thumb_path_get(const Evas_Object *obj, const char **file, const char **key)
558 ELM_CHECK_WIDTYPE(obj, widtype);
559 Widget_Data *wd = elm_widget_data_get(obj);
561 *file = wd->thumb.file;
563 *key = wd->thumb.key;
567 * Set the animation state for the thumb object. If its content is an animated
568 * video, you may start/stop the animation or tell it to play continuously and
571 * @param obj The thumb object.
572 * @param setting The animation setting.
574 * @see elm_thumb_file_set()
579 elm_thumb_animate_set(Evas_Object *obj, Elm_Thumb_Animation_Setting setting)
581 ELM_CHECK_WIDTYPE(obj, widtype);
582 Widget_Data *wd = elm_widget_data_get(obj);
584 if (setting < ELM_THUMB_ANIMATION_START ||
585 setting >= ELM_THUMB_ANIMATION_LAST)
590 wd->anim_setting = setting;
591 if (setting == ELM_THUMB_ANIMATION_LOOP)
592 edje_object_signal_emit(wd->view, "animate_loop", "");
593 else if (setting == ELM_THUMB_ANIMATION_START)
594 edje_object_signal_emit(wd->view, "animate", "");
595 else if (setting == ELM_THUMB_ANIMATION_STOP)
596 edje_object_signal_emit(wd->view, "animate_stop", "");
600 * Get the animation state for the thumb object.
602 * @param obj The thumb object.
603 * @return getting The animation setting or @c ELM_THUMB_ANIMATION_LAST,
606 * @see elm_thumb_file_get()
610 EAPI Elm_Thumb_Animation_Setting
611 elm_thumb_animate_get(const Evas_Object *obj)
613 ELM_CHECK_WIDTYPE(obj, widtype) ELM_THUMB_ANIMATION_LAST;
614 Widget_Data *wd = elm_widget_data_get(obj);
616 return wd->anim_setting;
620 * Get the ethumb_client handle so custom configuration can be made.
621 * This must be called before the objects are created to be sure no object is
622 * visible and no generation started.
624 * @return Ethumb_Client instance or NULL.
629 * #include <Elementary.h>
630 * #ifndef ELM_LIB_QUICKLAUNCH
632 * elm_main(int argc, char **argv)
634 * Ethumb_Client *client;
640 * client = elm_thumb_ethumb_client_get();
643 * ERR("could not get ethumb_client");
646 * ethumb_client_size_set(client, 100, 100);
647 * ethumb_client_crop_align_set(client, 0.5, 0.5);
650 * // Create elm_thumb objects here
662 EAPI struct _Ethumb_Client *
663 elm_thumb_ethumb_client_get(void)
665 return _elm_ethumb_client;
669 * Get the ethumb_client connection state.
671 * @return EINA_TRUE if the client is connected to the server or
672 * EINA_FALSE otherwise.
675 elm_thumb_ethumb_client_connected(void)
677 return _elm_ethumb_connected;
682 elm_thumb_editable_set(Evas_Object *obj, Eina_Bool edit)
684 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
685 Widget_Data *wd = elm_widget_data_get(obj);
687 if (!wd) return EINA_FALSE;
689 if (wd->edit == edit) return EINA_TRUE;
693 elm_drop_target_add(obj, ELM_SEL_FORMAT_IMAGE,
694 _elm_thumb_dropcb, obj);
696 elm_drop_target_del(obj);
702 elm_thumb_editable_get(Evas_Object *obj)
704 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
705 Widget_Data *wd = elm_widget_data_get(obj);
706 if (!wd) return EINA_FALSE;
710 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/