1 #include <Elementary.h>
6 # define FMT_SIZE_T "%Iu"
8 # define FMT_SIZE_T "%zu"
11 typedef struct _Smart_Data Smart_Data;
15 Evas_Coord x, y, w, h;
20 Eina_Bool fill_inside : 1;
21 Eina_Bool scale_up : 1;
22 Eina_Bool scale_down : 1;
23 Eina_Bool preloading : 1;
27 Eina_Bool aspect_ratio_retained: 1;
28 Elm_Image_Orient orient;
31 /* local subsystem functions */
32 static void _smart_reconfigure(Smart_Data *sd);
33 static void _smart_init(void);
34 static void _smart_add(Evas_Object *obj);
35 static void _smart_del(Evas_Object *obj);
36 static void _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y);
37 static void _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h);
38 static void _smart_show(Evas_Object *obj);
39 static void _smart_hide(Evas_Object *obj);
40 static void _smart_color_set(Evas_Object *obj, int r, int g, int b, int a);
41 static void _smart_clip_set(Evas_Object *obj, Evas_Object * clip);
42 static void _smart_clip_unset(Evas_Object *obj);
44 static void _els_smart_icon_flip_horizontal(Smart_Data *sd);
45 static void _els_smart_icon_flip_vertical(Smart_Data *sd);
46 static void _els_smart_icon_rotate_180(Smart_Data *sd);
47 static Eina_Bool _els_smart_icon_dropcb(void *,Evas_Object *, Elm_Selection_Data *);
49 /* local subsystem globals */
50 static Evas_Smart *_e_smart = NULL;
52 /* externally accessible functions */
54 _els_smart_icon_add(Evas *evas)
57 return evas_object_smart_add(evas, _e_smart);
61 _preloaded(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event __UNUSED__)
63 Smart_Data *sd = data;
65 sd->preloading = EINA_FALSE;
69 evas_object_show(sd->obj);
71 if (sd->prev) evas_object_del(sd->prev);
76 _els_smart_icon_file_helper(Evas_Object *obj)
81 sd = evas_object_smart_data_get(obj);
83 /* NOTE: Do not merge upstream for the if (sd->edje) { } statements
84 But wonder whether the edje resource icons have no problem. */
87 if (sd->prev) evas_object_del(sd->prev);
88 pclip = evas_object_clip_get(sd->obj);
89 if (sd->obj) sd->prev = sd->obj;
90 sd->obj = evas_object_image_add(evas_object_evas_get(obj));
91 evas_object_event_callback_add(sd->obj,
92 EVAS_CALLBACK_IMAGE_PRELOADED,
94 evas_object_smart_member_add(sd->obj, obj);
95 if (sd->prev) evas_object_smart_member_add(sd->prev, obj);
96 evas_object_image_scale_hint_set(sd->obj,
97 EVAS_IMAGE_SCALE_HINT_STATIC);
98 evas_object_clip_set(sd->obj, pclip);
100 sd->edje = EINA_FALSE;
104 evas_object_image_load_size_set(sd->obj, sd->size, sd->size);
108 _els_smart_icon_memfile_set(Evas_Object *obj, const void *img, size_t size, const char *format, const char *key)
112 sd = evas_object_smart_data_get(obj);
113 if (!sd) return EINA_FALSE;
114 _els_smart_icon_file_helper(obj);
116 evas_object_image_memfile_set(sd->obj, (void*)img, size, (char*)format, (char*)key);
117 sd->preloading = EINA_TRUE;
118 sd->show = EINA_TRUE;
119 evas_object_hide(sd->obj);
120 evas_object_image_preload(sd->obj, EINA_FALSE);
121 if (evas_object_image_load_error_get(sd->obj) != EVAS_LOAD_ERROR_NONE)
123 ERR("Things are going bad for some random " FMT_SIZE_T " byte chunk of memory (%p)", size, sd->obj);
126 _smart_reconfigure(sd);
131 _els_smart_icon_file_key_set(Evas_Object *obj, const char *file, const char *key)
135 sd = evas_object_smart_data_get(obj);
136 if (!sd) return EINA_FALSE;
137 _els_smart_icon_file_helper(obj);
139 evas_object_image_file_set(sd->obj, file, key);
140 // NOTE: Do not merge upstream for sd->preloading.
141 sd->preloading = EINA_FALSE; // by default preload off by seok.j.jeong
142 sd->show = EINA_TRUE;
143 // NOTE: Do not merge upstream for sd->preloading.
145 evas_object_image_preload(sd->obj, EINA_FALSE);
146 // NOTE: Do not merge upstream for sd->preloading.
147 if (sd->preloading) // sd->preloading can be changed by above function. so add "if (sd->preloading)" as below
148 evas_object_hide(sd->obj);
149 if (evas_object_image_load_error_get(sd->obj) != EVAS_LOAD_ERROR_NONE)
151 ERR("Things are going bad for '%s' (%p)", file, sd->obj);
154 _smart_reconfigure(sd);
159 _els_smart_icon_preload_set(Evas_Object *obj, Eina_Bool disable)
163 sd = evas_object_smart_data_get(obj);
164 if ((!sd) || sd->edje) return;
165 evas_object_image_preload(sd->obj, disable);
166 sd->preloading = !disable;
170 _els_smart_icon_file_edje_set(Evas_Object *obj, const char *file, const char *part)
175 sd = evas_object_smart_data_get(obj);
176 if (!sd) return EINA_FALSE;
177 /* smart code here */
178 if (sd->prev) evas_object_del(sd->prev);
183 pclip = evas_object_clip_get(sd->obj);
184 if (sd->obj) evas_object_del(sd->obj);
185 sd->obj = edje_object_add(evas_object_evas_get(obj));
186 evas_object_smart_member_add(sd->obj, obj);
187 if (sd->show) evas_object_show(sd->obj);
188 evas_object_clip_set(sd->obj, pclip);
190 sd->edje = EINA_TRUE;
191 if (!edje_object_file_set(sd->obj, file, part))
193 _smart_reconfigure(sd);
198 _els_smart_icon_file_get(const Evas_Object *obj, const char **file, const char **key)
200 Smart_Data *sd = evas_object_smart_data_get(obj);
203 edje_object_file_get(sd->obj, file, key);
205 evas_object_image_file_get(sd->obj, file, key);
209 _els_smart_icon_smooth_scale_set(Evas_Object *obj, Eina_Bool smooth)
211 Smart_Data *sd = evas_object_smart_data_get(obj);
215 evas_object_image_smooth_scale_set(sd->obj, smooth);
219 _els_smart_icon_smooth_scale_get(const Evas_Object *obj)
221 Smart_Data *sd = evas_object_smart_data_get(obj);
222 if (!sd) return EINA_FALSE;
225 return evas_object_image_smooth_scale_get(sd->obj);
229 _els_smart_icon_object_get(const Evas_Object *obj)
231 Smart_Data *sd = evas_object_smart_data_get(obj);
232 if (!sd) return NULL;
237 _els_smart_icon_size_get(const Evas_Object *obj, int *w, int *h)
244 sd = evas_object_smart_data_get(obj);
246 type = evas_object_type_get(sd->obj);
248 if (!strcmp(type, "edje"))
249 edje_object_size_min_get(sd->obj, &tw, &th);
251 evas_object_image_size_get(sd->obj, &tw, &th);
252 evas_object_geometry_get(sd->obj, NULL, NULL, &cw, &ch);
253 tw = tw > cw ? tw : cw;
254 th = th > ch ? th : ch;
255 tw = ((double)tw) * sd->scale;
256 th = ((double)th) * sd->scale;
262 _els_smart_icon_fill_inside_set(Evas_Object *obj, Eina_Bool fill_inside)
266 sd = evas_object_smart_data_get(obj);
268 if (((sd->fill_inside) && (fill_inside)) ||
269 ((!sd->fill_inside) && (!fill_inside))) return;
270 sd->fill_inside = fill_inside;
271 _smart_reconfigure(sd);
275 _els_smart_icon_fill_inside_get(const Evas_Object *obj)
277 Smart_Data *sd = evas_object_smart_data_get(obj);
278 if (!sd) return EINA_FALSE;
279 return sd->fill_inside;
283 _els_smart_icon_scale_up_set(Evas_Object *obj, Eina_Bool scale_up)
287 sd = evas_object_smart_data_get(obj);
289 if (((sd->scale_up) && (scale_up)) ||
290 ((!sd->scale_up) && (!scale_up))) return;
291 sd->scale_up = scale_up;
292 _smart_reconfigure(sd);
296 _els_smart_icon_scale_up_get(const Evas_Object *obj)
298 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
299 if (!sd) return EINA_FALSE;
304 _els_smart_icon_scale_down_set(Evas_Object *obj, Eina_Bool scale_down)
308 sd = evas_object_smart_data_get(obj);
310 if (((sd->scale_down) && (scale_down)) ||
311 ((!sd->scale_down) && (!scale_down))) return;
312 sd->scale_down = scale_down;
313 _smart_reconfigure(sd);
317 _els_smart_icon_scale_down_get(const Evas_Object *obj)
319 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
320 if (!sd) return EINA_FALSE;
325 _els_smart_icon_scale_size_set(Evas_Object *obj, int size)
329 sd = evas_object_smart_data_get(obj);
332 if (!sd->obj) return;
335 evas_object_image_load_size_set(sd->obj, sd->size, sd->size);
339 _els_smart_icon_scale_size_get(const Evas_Object *obj)
341 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
347 _els_smart_icon_scale_set(Evas_Object *obj, double scale)
349 Smart_Data *sd = evas_object_smart_data_get(obj);
352 _smart_reconfigure(sd);
356 _els_smart_icon_scale_get(const Evas_Object *obj)
358 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
364 _els_smart_icon_orient_set(Evas_Object *obj, Elm_Image_Orient orient)
368 unsigned int *data, *data2, *to, *from;
369 int x, y, w, hw, iw, ih;
370 const char *file, *key;
372 sd = evas_object_smart_data_get(obj);
379 case ELM_IMAGE_FLIP_HORIZONTAL:
380 _els_smart_icon_flip_horizontal(sd);
382 case ELM_IMAGE_FLIP_VERTICAL:
383 _els_smart_icon_flip_vertical(sd);
385 case ELM_IMAGE_ROTATE_180_CW:
386 _els_smart_icon_rotate_180(sd);
392 evas_object_image_size_get(sd->obj, &iw, &ih);
393 evas_object_image_file_get(sd->obj, &file, &key);
394 tmp = evas_object_image_add(evas_object_evas_get(sd->obj));
395 evas_object_image_file_set(tmp, file, key);
396 data2 = evas_object_image_data_get(tmp, EINA_FALSE);
403 evas_object_image_size_set(sd->obj, iw, ih);
404 data = evas_object_image_data_get(sd->obj, EINA_TRUE);
407 case ELM_IMAGE_FLIP_TRANSPOSE:
411 case ELM_IMAGE_FLIP_TRANSVERSE:
416 case ELM_IMAGE_ROTATE_90_CW:
420 case ELM_IMAGE_ROTATE_90_CCW:
426 ERR("unknown orient %d", orient);
427 evas_object_del(tmp);
428 evas_object_image_data_set(sd->obj, data); // give it back
432 for (x = iw; --x >= 0;)
434 for (y = ih; --y >= 0;)
443 evas_object_del(tmp);
444 evas_object_image_data_set(sd->obj, data);
445 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
446 _smart_reconfigure(sd);
450 _els_smart_icon_orient_get(const Evas_Object *obj)
452 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
458 * Turns on editing through drag and drop and copy and paste.
461 _els_smart_icon_edit_set(Evas_Object *obj, Eina_Bool edit, Evas_Object *parent)
463 Smart_Data *sd = evas_object_smart_data_get(obj);
468 printf("No editing edje objects yet (ever)\n");
472 /* Unfortunately eina bool is not a bool, but a char */
473 if (edit == sd->edit) return;
478 elm_drop_target_add(obj, ELM_SEL_FORMAT_IMAGE, _els_smart_icon_dropcb,
481 elm_drop_target_del(obj);
485 _els_smart_icon_edit_get(const Evas_Object *obj)
487 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
488 if (!sd) return EINA_FALSE;
493 _els_smart_icon_edje_get(Evas_Object *obj)
495 Smart_Data *sd = evas_object_smart_data_get(obj);
496 if (!sd) return NULL;
497 if (!sd->edje) return NULL;
502 _els_smart_icon_aspect_ratio_retained_set(Evas_Object *obj, Eina_Bool retained)
506 sd = evas_object_smart_data_get(obj);
509 retained = !!retained;
510 if (sd->aspect_ratio_retained == retained) return;
511 sd->aspect_ratio_retained = retained;
512 _smart_reconfigure(sd);
516 _els_smart_icon_aspect_ratio_retained_get(const Evas_Object *obj)
520 sd = evas_object_smart_data_get(obj);
521 if (!sd) return EINA_FALSE;
522 return sd->aspect_ratio_retained;
525 /* local subsystem globals */
527 _smart_reconfigure(Smart_Data *sd)
529 Evas_Coord x, y, w, h;
532 if (!sd->obj) return;
537 type = evas_object_type_get(sd->obj);
539 if (!strcmp(type, "edje"))
543 evas_object_move(sd->obj, x, y);
544 evas_object_resize(sd->obj, w, h);
550 evas_object_image_size_get(sd->obj, &iw, &ih);
552 iw = ((double)iw) * sd->scale;
553 ih = ((double)ih) * sd->scale;
558 if (sd->aspect_ratio_retained)
560 h = ((double)ih * w) / (double)iw;
566 w = ((double)iw * h) / (double)ih;
574 w = ((double)iw * h) / (double)ih;
588 x = sd->x + ((sd->w - w) / 2);
589 y = sd->y + ((sd->h - h) / 2);
590 evas_object_move(sd->obj, x, y);
591 evas_object_image_fill_set(sd->obj, 0, 0, w, h);
592 evas_object_resize(sd->obj, w, h);
599 if (_e_smart) return;
601 static const Evas_Smart_Class sc =
604 EVAS_SMART_CLASS_VERSION,
622 _e_smart = evas_smart_class_new(&sc);
627 _smart_add(Evas_Object *obj)
631 sd = calloc(1, sizeof(Smart_Data));
633 sd->obj = evas_object_image_add(evas_object_evas_get(obj));
635 evas_object_image_scale_hint_set(sd->obj, EVAS_IMAGE_SCALE_HINT_STATIC);
640 sd->fill_inside = EINA_TRUE;
641 sd->scale_up = EINA_TRUE;
642 sd->scale_down = EINA_TRUE;
643 sd->aspect_ratio_retained = EINA_TRUE;
646 evas_object_smart_member_add(sd->obj, obj);
647 evas_object_smart_data_set(obj, sd);
648 evas_object_event_callback_add(sd->obj, EVAS_CALLBACK_IMAGE_PRELOADED,
653 _smart_del(Evas_Object *obj)
657 sd = evas_object_smart_data_get(obj);
659 evas_object_del(sd->obj);
660 if (sd->prev) evas_object_del(sd->prev);
665 _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
669 sd = evas_object_smart_data_get(obj);
671 if ((sd->x == x) && (sd->y == y)) return;
674 _smart_reconfigure(sd);
678 _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
682 sd = evas_object_smart_data_get(obj);
684 if ((sd->w == w) && (sd->h == h)) return;
687 _smart_reconfigure(sd);
691 _smart_show(Evas_Object *obj)
695 sd = evas_object_smart_data_get(obj);
697 sd->show = EINA_TRUE;
700 evas_object_show(sd->obj);
701 if (sd->prev) evas_object_del(sd->prev);
707 _smart_hide(Evas_Object *obj)
711 sd = evas_object_smart_data_get(obj);
713 sd->show = EINA_FALSE;
714 evas_object_hide(sd->obj);
715 if (sd->prev) evas_object_del(sd->prev);
720 _smart_color_set(Evas_Object *obj, int r, int g, int b, int a)
724 sd = evas_object_smart_data_get(obj);
726 evas_object_color_set(sd->obj, r, g, b, a);
727 if (sd->prev) evas_object_color_set(sd->prev, r, g, b, a);
731 _smart_clip_set(Evas_Object *obj, Evas_Object * clip)
735 sd = evas_object_smart_data_get(obj);
737 evas_object_clip_set(sd->obj, clip);
738 if (sd->prev) evas_object_clip_set(sd->prev, clip);
742 _smart_clip_unset(Evas_Object *obj)
746 sd = evas_object_smart_data_get(obj);
748 evas_object_clip_unset(sd->obj);
749 if (sd->prev) evas_object_clip_unset(sd->prev);
753 _els_smart_icon_flip_horizontal(Smart_Data *sd)
756 unsigned int *p1, *p2, tmp;
759 evas_object_image_size_get(sd->obj, &iw, &ih);
760 data = evas_object_image_data_get(sd->obj, EINA_TRUE);
762 for (y = 0; y < ih; y++)
764 p1 = data + (y * iw);
765 p2 = data + ((y + 1) * iw) - 1;
766 for (x = 0; x < (iw >> 1); x++)
776 evas_object_image_data_set(sd->obj, data);
777 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
778 _smart_reconfigure(sd);
782 _els_smart_icon_flip_vertical(Smart_Data *sd)
785 unsigned int *p1, *p2, tmp;
788 evas_object_image_size_get(sd->obj, &iw, &ih);
789 data = evas_object_image_data_get(sd->obj, EINA_TRUE);
791 for (y = 0; y < (ih >> 1); y++)
793 p1 = data + (y * iw);
794 p2 = data + ((ih - 1 - y) * iw);
795 for (x = 0; x < iw; x++)
805 evas_object_image_data_set(sd->obj, data);
806 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
807 _smart_reconfigure(sd);
811 _els_smart_icon_rotate_180(Smart_Data *sd)
814 unsigned int *p1, *p2, tmp;
817 evas_object_image_size_get(sd->obj, &iw, &ih);
818 data = evas_object_image_data_get(sd->obj, 1);
832 evas_object_image_data_set(sd->obj, data);
833 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
834 _smart_reconfigure(sd);
838 _els_smart_icon_dropcb(void *elmobj,Evas_Object *obj, Elm_Selection_Data *drop)
840 _els_smart_icon_file_key_set(obj, drop->data, NULL);
841 evas_object_smart_callback_call(elmobj, "drop", drop->data);
845 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/