1 #include <Elementary.h>
5 typedef struct _Smart_Data Smart_Data;
14 Eina_Bool fill_inside : 1;
15 Eina_Bool scale_up : 1;
16 Eina_Bool scale_down : 1;
17 Eina_Bool preloading : 1;
21 Eina_Bool aspect_ratio_retained: 1;
22 Elm_Image_Orient orient;
25 /* local subsystem functions */
26 static void _smart_reconfigure(Smart_Data *sd);
27 static void _smart_init(void);
28 static void _smart_add(Evas_Object *obj);
29 static void _smart_del(Evas_Object *obj);
30 static void _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y);
31 static void _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h);
32 static void _smart_show(Evas_Object *obj);
33 static void _smart_hide(Evas_Object *obj);
34 static void _smart_color_set(Evas_Object *obj, int r, int g, int b, int a);
35 static void _smart_clip_set(Evas_Object *obj, Evas_Object * clip);
36 static void _smart_clip_unset(Evas_Object *obj);
38 static void _els_smart_icon_flip_horizontal(Smart_Data *sd);
39 static void _els_smart_icon_flip_vertical(Smart_Data *sd);
40 static void _els_smart_icon_rotate_180(Smart_Data *sd);
41 static Eina_Bool _els_smart_icon_dropcb(void *,Evas_Object *, Elm_Selection_Data *);
43 /* local subsystem globals */
44 static Evas_Smart *_e_smart = NULL;
46 /* externally accessible functions */
48 _els_smart_icon_add(Evas *evas)
51 return evas_object_smart_add(evas, _e_smart);
55 _preloaded(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event __UNUSED__)
57 Smart_Data *sd = data;
59 sd->preloading = EINA_FALSE;
63 evas_object_show(sd->obj);
65 if (sd->prev) evas_object_del(sd->prev);
70 _els_smart_icon_file_helper(Evas_Object *obj)
75 sd = evas_object_smart_data_get(obj);
77 /* NOTE: Do not merge upstream for the if (sd->edje) { } statements
78 But wonder whether the edje resource icons have no problem. */
81 if (sd->prev) evas_object_del(sd->prev);
82 pclip = evas_object_clip_get(sd->obj);
83 if (sd->obj) sd->prev = sd->obj;
84 sd->obj = evas_object_image_add(evas_object_evas_get(obj));
85 evas_object_event_callback_add(sd->obj,
86 EVAS_CALLBACK_IMAGE_PRELOADED,
88 evas_object_smart_member_add(sd->obj, obj);
89 if (sd->prev) evas_object_smart_member_add(sd->prev, obj);
90 evas_object_image_scale_hint_set(sd->obj,
91 EVAS_IMAGE_SCALE_HINT_STATIC);
92 evas_object_clip_set(sd->obj, pclip);
94 sd->edje = EINA_FALSE;
98 evas_object_image_load_size_set(sd->obj, sd->size, sd->size);
102 _els_smart_icon_memfile_set(Evas_Object *obj, const void *img, size_t size, const char *format, const char *key)
106 sd = evas_object_smart_data_get(obj);
107 if (!sd) return EINA_FALSE;
108 _els_smart_icon_file_helper(obj);
110 evas_object_image_memfile_set(sd->obj, (void*)img, size, (char*)format, (char*)key);
111 sd->preloading = EINA_TRUE;
112 sd->show = EINA_TRUE;
113 evas_object_hide(sd->obj);
114 evas_object_image_preload(sd->obj, EINA_FALSE);
115 if (evas_object_image_load_error_get(sd->obj) != EVAS_LOAD_ERROR_NONE)
117 ERR("Things are going bad for some random %zu byte chunk of memory (%p)", size, sd->obj);
120 _smart_reconfigure(sd);
125 _els_smart_icon_file_key_set(Evas_Object *obj, const char *file, const char *key)
129 sd = evas_object_smart_data_get(obj);
130 if (!sd) return EINA_FALSE;
131 _els_smart_icon_file_helper(obj);
133 evas_object_image_file_set(sd->obj, file, key);
134 // NOTE: Do not merge upstream for sd->preloading.
135 sd->preloading = EINA_FALSE; // by default preload off by seok.j.jeong
136 sd->show = EINA_TRUE;
137 // NOTE: Do not merge upstream for sd->preloading.
139 evas_object_image_preload(sd->obj, EINA_FALSE);
140 // NOTE: Do not merge upstream for sd->preloading.
141 if (sd->preloading) // sd->preloading can be changed by above function. so add "if (sd->preloading)" as below
142 evas_object_hide(sd->obj);
143 if (evas_object_image_load_error_get(sd->obj) != EVAS_LOAD_ERROR_NONE)
145 ERR("Things are going bad for '%s' (%p)", file, sd->obj);
148 _smart_reconfigure(sd);
153 _els_smart_icon_preload_set(Evas_Object *obj, Eina_Bool disable)
157 sd = evas_object_smart_data_get(obj);
158 if ((!sd) || sd->edje) return;
159 evas_object_image_preload(sd->obj, disable);
160 sd->preloading = !disable;
164 _els_smart_icon_file_edje_set(Evas_Object *obj, const char *file, const char *part)
169 sd = evas_object_smart_data_get(obj);
170 if (!sd) return EINA_FALSE;
171 /* smart code here */
172 if (sd->prev) evas_object_del(sd->prev);
177 pclip = evas_object_clip_get(sd->obj);
178 if (sd->obj) evas_object_del(sd->obj);
179 sd->obj = edje_object_add(evas_object_evas_get(obj));
180 evas_object_smart_member_add(sd->obj, obj);
181 if (sd->show) evas_object_show(sd->obj);
182 evas_object_clip_set(sd->obj, pclip);
184 sd->edje = EINA_TRUE;
185 if (!edje_object_file_set(sd->obj, file, part))
187 _smart_reconfigure(sd);
192 _els_smart_icon_file_get(const Evas_Object *obj, const char **file, const char **key)
194 Smart_Data *sd = evas_object_smart_data_get(obj);
197 edje_object_file_get(sd->obj, file, key);
199 evas_object_image_file_get(sd->obj, file, key);
203 _els_smart_icon_smooth_scale_set(Evas_Object *obj, Eina_Bool smooth)
205 Smart_Data *sd = evas_object_smart_data_get(obj);
209 evas_object_image_smooth_scale_set(sd->obj, smooth);
213 _els_smart_icon_smooth_scale_get(const Evas_Object *obj)
215 Smart_Data *sd = evas_object_smart_data_get(obj);
216 if (!sd) return EINA_FALSE;
219 return evas_object_image_smooth_scale_get(sd->obj);
223 _els_smart_icon_object_get(const Evas_Object *obj)
225 Smart_Data *sd = evas_object_smart_data_get(obj);
226 if (!sd) return NULL;
231 _els_smart_icon_size_get(const Evas_Object *obj, int *w, int *h)
238 sd = evas_object_smart_data_get(obj);
240 type = evas_object_type_get(sd->obj);
242 if (!strcmp(type, "edje"))
243 edje_object_size_min_get(sd->obj, &tw, &th);
245 evas_object_image_size_get(sd->obj, &tw, &th);
246 evas_object_geometry_get(sd->obj, NULL, NULL, &cw, &ch);
247 tw = tw > cw ? tw : cw;
248 th = th > ch ? th : ch;
249 tw = ((double)tw) * sd->scale;
250 th = ((double)th) * sd->scale;
256 _els_smart_icon_fill_inside_set(Evas_Object *obj, Eina_Bool fill_inside)
260 sd = evas_object_smart_data_get(obj);
262 if (((sd->fill_inside) && (fill_inside)) ||
263 ((!sd->fill_inside) && (!fill_inside))) return;
264 sd->fill_inside = fill_inside;
265 _smart_reconfigure(sd);
269 _els_smart_icon_fill_inside_get(const Evas_Object *obj)
271 Smart_Data *sd = evas_object_smart_data_get(obj);
272 if (!sd) return EINA_FALSE;
273 return sd->fill_inside;
277 _els_smart_icon_scale_up_set(Evas_Object *obj, Eina_Bool scale_up)
281 sd = evas_object_smart_data_get(obj);
283 if (((sd->scale_up) && (scale_up)) ||
284 ((!sd->scale_up) && (!scale_up))) return;
285 sd->scale_up = scale_up;
286 _smart_reconfigure(sd);
290 _els_smart_icon_scale_up_get(const Evas_Object *obj)
292 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
293 if (!sd) return EINA_FALSE;
298 _els_smart_icon_scale_down_set(Evas_Object *obj, Eina_Bool scale_down)
302 sd = evas_object_smart_data_get(obj);
304 if (((sd->scale_down) && (scale_down)) ||
305 ((!sd->scale_down) && (!scale_down))) return;
306 sd->scale_down = scale_down;
307 _smart_reconfigure(sd);
311 _els_smart_icon_scale_down_get(const Evas_Object *obj)
313 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
314 if (!sd) return EINA_FALSE;
319 _els_smart_icon_scale_size_set(Evas_Object *obj, int size)
323 sd = evas_object_smart_data_get(obj);
326 if (!sd->obj) return;
329 evas_object_image_load_size_set(sd->obj, sd->size, sd->size);
333 _els_smart_icon_scale_size_get(const Evas_Object *obj)
335 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
341 _els_smart_icon_scale_set(Evas_Object *obj, double scale)
343 Smart_Data *sd = evas_object_smart_data_get(obj);
346 _smart_reconfigure(sd);
350 _els_smart_icon_scale_get(const Evas_Object *obj)
352 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
358 _els_smart_icon_orient_set(Evas_Object *obj, Elm_Image_Orient orient)
362 unsigned int *data, *data2, *to, *from;
363 int x, y, w, hw, iw, ih;
364 const char *file, *key;
366 sd = evas_object_smart_data_get(obj);
373 case ELM_IMAGE_FLIP_HORIZONTAL:
374 _els_smart_icon_flip_horizontal(sd);
376 case ELM_IMAGE_FLIP_VERTICAL:
377 _els_smart_icon_flip_vertical(sd);
379 case ELM_IMAGE_ROTATE_180_CW:
380 _els_smart_icon_rotate_180(sd);
386 evas_object_image_size_get(sd->obj, &iw, &ih);
387 evas_object_image_file_get(sd->obj, &file, &key);
388 tmp = evas_object_image_add(evas_object_evas_get(sd->obj));
389 evas_object_image_file_set(tmp, file, key);
390 data2 = evas_object_image_data_get(tmp, EINA_FALSE);
397 evas_object_image_size_set(sd->obj, iw, ih);
398 data = evas_object_image_data_get(sd->obj, EINA_TRUE);
401 case ELM_IMAGE_FLIP_TRANSPOSE:
405 case ELM_IMAGE_FLIP_TRANSVERSE:
410 case ELM_IMAGE_ROTATE_90_CW:
414 case ELM_IMAGE_ROTATE_90_CCW:
420 ERR("unknown orient %d", orient);
421 evas_object_del(tmp);
422 evas_object_image_data_set(sd->obj, data); // give it back
426 for (x = iw; --x >= 0;)
428 for (y = ih; --y >= 0;)
437 evas_object_del(tmp);
438 evas_object_image_data_set(sd->obj, data);
439 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
440 _smart_reconfigure(sd);
444 _els_smart_icon_orient_get(const Evas_Object *obj)
446 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
452 * Turns on editing through drag and drop and copy and paste.
455 _els_smart_icon_edit_set(Evas_Object *obj, Eina_Bool edit, Evas_Object *parent)
457 Smart_Data *sd = evas_object_smart_data_get(obj);
462 printf("No editing edje objects yet (ever)\n");
466 /* Unfortunately eina bool is not a bool, but a char */
467 if (edit == sd->edit) return;
472 elm_drop_target_add(obj, ELM_SEL_FORMAT_IMAGE, _els_smart_icon_dropcb,
475 elm_drop_target_del(obj);
479 _els_smart_icon_edit_get(const Evas_Object *obj)
481 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
482 if (!sd) return EINA_FALSE;
487 _els_smart_icon_edje_get(Evas_Object *obj)
489 Smart_Data *sd = evas_object_smart_data_get(obj);
490 if (!sd) return NULL;
491 if (!sd->edje) return NULL;
496 _els_smart_icon_aspect_ratio_retained_set(Evas_Object *obj, Eina_Bool retained)
500 sd = evas_object_smart_data_get(obj);
503 retained = !!retained;
504 if (sd->aspect_ratio_retained == retained) return;
505 sd->aspect_ratio_retained = retained;
506 _smart_reconfigure(sd);
510 _els_smart_icon_aspect_ratio_retained_get(const Evas_Object *obj)
514 sd = evas_object_smart_data_get(obj);
515 if (!sd) return EINA_FALSE;
516 return sd->aspect_ratio_retained;
519 /* local subsystem globals */
521 _smart_reconfigure(Smart_Data *sd)
523 Evas_Coord x, y, w, h;
526 if (!sd->obj) return;
531 type = evas_object_type_get(sd->obj);
533 if (!strcmp(type, "edje"))
537 evas_object_move(sd->obj, x, y);
538 evas_object_resize(sd->obj, w, h);
544 evas_object_image_size_get(sd->obj, &iw, &ih);
546 iw = ((double)iw) * sd->scale;
547 ih = ((double)ih) * sd->scale;
552 if (sd->aspect_ratio_retained)
554 h = ((double)ih * w) / (double)iw;
560 w = ((double)iw * h) / (double)ih;
568 w = ((double)iw * h) / (double)ih;
582 x = sd->x + ((sd->w - w) / 2);
583 y = sd->y + ((sd->h - h) / 2);
584 evas_object_move(sd->obj, x, y);
585 evas_object_image_fill_set(sd->obj, 0, 0, w, h);
586 evas_object_resize(sd->obj, w, h);
593 if (_e_smart) return;
595 static const Evas_Smart_Class sc =
598 EVAS_SMART_CLASS_VERSION,
616 _e_smart = evas_smart_class_new(&sc);
621 _smart_add(Evas_Object *obj)
625 sd = calloc(1, sizeof(Smart_Data));
627 sd->obj = evas_object_image_add(evas_object_evas_get(obj));
629 evas_object_image_scale_hint_set(sd->obj, EVAS_IMAGE_SCALE_HINT_STATIC);
634 sd->fill_inside = EINA_TRUE;
635 sd->scale_up = EINA_TRUE;
636 sd->scale_down = EINA_TRUE;
637 sd->aspect_ratio_retained = EINA_TRUE;
640 evas_object_smart_member_add(sd->obj, obj);
641 evas_object_smart_data_set(obj, sd);
642 evas_object_event_callback_add(sd->obj, EVAS_CALLBACK_IMAGE_PRELOADED,
647 _smart_del(Evas_Object *obj)
651 sd = evas_object_smart_data_get(obj);
653 evas_object_del(sd->obj);
654 if (sd->prev) evas_object_del(sd->prev);
659 _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
663 sd = evas_object_smart_data_get(obj);
665 if ((sd->x == x) && (sd->y == y)) return;
668 _smart_reconfigure(sd);
672 _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
676 sd = evas_object_smart_data_get(obj);
678 if ((sd->w == w) && (sd->h == h)) return;
681 _smart_reconfigure(sd);
685 _smart_show(Evas_Object *obj)
689 sd = evas_object_smart_data_get(obj);
691 sd->show = EINA_TRUE;
694 evas_object_show(sd->obj);
695 if (sd->prev) evas_object_del(sd->prev);
701 _smart_hide(Evas_Object *obj)
705 sd = evas_object_smart_data_get(obj);
707 sd->show = EINA_FALSE;
708 evas_object_hide(sd->obj);
709 if (sd->prev) evas_object_del(sd->prev);
714 _smart_color_set(Evas_Object *obj, int r, int g, int b, int a)
718 sd = evas_object_smart_data_get(obj);
720 evas_object_color_set(sd->obj, r, g, b, a);
721 if (sd->prev) evas_object_color_set(sd->prev, r, g, b, a);
725 _smart_clip_set(Evas_Object *obj, Evas_Object * clip)
729 sd = evas_object_smart_data_get(obj);
731 evas_object_clip_set(sd->obj, clip);
732 if (sd->prev) evas_object_clip_set(sd->prev, clip);
736 _smart_clip_unset(Evas_Object *obj)
740 sd = evas_object_smart_data_get(obj);
742 evas_object_clip_unset(sd->obj);
743 if (sd->prev) evas_object_clip_unset(sd->prev);
747 _els_smart_icon_flip_horizontal(Smart_Data *sd)
750 unsigned int *p1, *p2, tmp;
753 evas_object_image_size_get(sd->obj, &iw, &ih);
754 data = evas_object_image_data_get(sd->obj, EINA_TRUE);
756 for (y = 0; y < ih; y++)
758 p1 = data + (y * iw);
759 p2 = data + ((y + 1) * iw) - 1;
760 for (x = 0; x < (iw >> 1); x++)
770 evas_object_image_data_set(sd->obj, data);
771 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
772 _smart_reconfigure(sd);
776 _els_smart_icon_flip_vertical(Smart_Data *sd)
779 unsigned int *p1, *p2, tmp;
782 evas_object_image_size_get(sd->obj, &iw, &ih);
783 data = evas_object_image_data_get(sd->obj, EINA_TRUE);
785 for (y = 0; y < (ih >> 1); y++)
787 p1 = data + (y * iw);
788 p2 = data + ((ih - 1 - y) * iw);
789 for (x = 0; x < iw; x++)
799 evas_object_image_data_set(sd->obj, data);
800 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
801 _smart_reconfigure(sd);
805 _els_smart_icon_rotate_180(Smart_Data *sd)
808 unsigned int *p1, *p2, tmp;
811 evas_object_image_size_get(sd->obj, &iw, &ih);
812 data = evas_object_image_data_get(sd->obj, 1);
826 evas_object_image_data_set(sd->obj, data);
827 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
828 _smart_reconfigure(sd);
832 _els_smart_icon_dropcb(void *elmobj,Evas_Object *obj, Elm_Selection_Data *drop)
834 _els_smart_icon_file_key_set(obj, drop->data, NULL);
835 evas_object_smart_callback_call(elmobj, "drop", drop->data);
839 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/