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. */
85 if (!sd->edje) goto out;
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, EVAS_CALLBACK_IMAGE_PRELOADED,
93 evas_object_smart_member_add(sd->obj, obj);
94 if (sd->prev) evas_object_smart_member_add(sd->prev, obj);
95 evas_object_image_scale_hint_set(sd->obj, EVAS_IMAGE_SCALE_HINT_STATIC);
96 evas_object_clip_set(sd->obj, pclip);
98 sd->edje = EINA_FALSE;
102 evas_object_image_load_size_set(sd->obj, sd->size, sd->size);
106 _els_smart_icon_memfile_set(Evas_Object *obj, const void *img, size_t size, const char *format, const char *key)
110 sd = evas_object_smart_data_get(obj);
111 if (!sd) return EINA_FALSE;
112 _els_smart_icon_file_helper(obj);
114 evas_object_image_memfile_set(sd->obj, (void*)img, size, (char*)format, (char*)key);
115 sd->preloading = EINA_TRUE;
116 sd->show = EINA_TRUE;
117 evas_object_hide(sd->obj);
118 evas_object_image_preload(sd->obj, EINA_FALSE);
119 if (evas_object_image_load_error_get(sd->obj) != EVAS_LOAD_ERROR_NONE)
121 ERR("Things are going bad for some random " FMT_SIZE_T " byte chunk of memory (%p)", size, sd->obj);
124 _smart_reconfigure(sd);
129 _els_smart_icon_file_key_set(Evas_Object *obj, const char *file, const char *key)
133 sd = evas_object_smart_data_get(obj);
134 if (!sd) return EINA_FALSE;
135 _els_smart_icon_file_helper(obj);
137 evas_object_image_file_set(sd->obj, file, key);
138 // NOTE: Do not merge upstream for sd->preloading.
139 sd->preloading = EINA_FALSE; // by default preload off by seok.j.jeong
140 sd->show = EINA_TRUE;
141 // NOTE: Do not merge upstream for sd->preloading.
143 evas_object_image_preload(sd->obj, EINA_FALSE);
144 // NOTE: Do not merge upstream for sd->preloading.
145 if (sd->preloading) // sd->preloading can be changed by above function. so add "if (sd->preloading)" as below
146 evas_object_hide(sd->obj);
147 if (evas_object_image_load_error_get(sd->obj) != EVAS_LOAD_ERROR_NONE)
149 ERR("Things are going bad for '%s' (%p)", file, sd->obj);
152 _smart_reconfigure(sd);
157 _els_smart_icon_preload_set(Evas_Object *obj, Eina_Bool disable)
161 sd = evas_object_smart_data_get(obj);
162 if ((!sd) || sd->edje) return;
163 evas_object_image_preload(sd->obj, disable);
164 sd->preloading = !disable;
168 _els_smart_icon_file_edje_set(Evas_Object *obj, const char *file, const char *part)
173 sd = evas_object_smart_data_get(obj);
174 if (!sd) return EINA_FALSE;
175 /* smart code here */
176 if (sd->prev) evas_object_del(sd->prev);
181 pclip = evas_object_clip_get(sd->obj);
182 if (sd->obj) evas_object_del(sd->obj);
183 sd->obj = edje_object_add(evas_object_evas_get(obj));
184 evas_object_smart_member_add(sd->obj, obj);
185 if (sd->show) evas_object_show(sd->obj);
186 evas_object_clip_set(sd->obj, pclip);
188 sd->edje = EINA_TRUE;
189 if (!edje_object_file_set(sd->obj, file, part))
191 _smart_reconfigure(sd);
196 _els_smart_icon_file_get(const Evas_Object *obj, const char **file, const char **key)
198 Smart_Data *sd = evas_object_smart_data_get(obj);
201 edje_object_file_get(sd->obj, file, key);
203 evas_object_image_file_get(sd->obj, file, key);
207 _els_smart_icon_smooth_scale_set(Evas_Object *obj, Eina_Bool smooth)
209 Smart_Data *sd = evas_object_smart_data_get(obj);
213 evas_object_image_smooth_scale_set(sd->obj, smooth);
217 _els_smart_icon_smooth_scale_get(const Evas_Object *obj)
219 Smart_Data *sd = evas_object_smart_data_get(obj);
220 if (!sd) return EINA_FALSE;
223 return evas_object_image_smooth_scale_get(sd->obj);
227 _els_smart_icon_object_get(const Evas_Object *obj)
229 Smart_Data *sd = evas_object_smart_data_get(obj);
230 if (!sd) return NULL;
235 _els_smart_icon_size_get(const Evas_Object *obj, int *w, int *h)
242 sd = evas_object_smart_data_get(obj);
244 type = evas_object_type_get(sd->obj);
246 if (!strcmp(type, "edje"))
247 edje_object_size_min_get(sd->obj, &tw, &th);
249 evas_object_image_size_get(sd->obj, &tw, &th);
250 evas_object_geometry_get(sd->obj, NULL, NULL, &cw, &ch);
251 tw = tw > cw ? tw : cw;
252 th = th > ch ? th : ch;
253 tw = ((double)tw) * sd->scale;
254 th = ((double)th) * sd->scale;
260 _els_smart_icon_fill_inside_set(Evas_Object *obj, Eina_Bool fill_inside)
264 sd = evas_object_smart_data_get(obj);
266 if (((sd->fill_inside) && (fill_inside)) ||
267 ((!sd->fill_inside) && (!fill_inside))) return;
268 sd->fill_inside = fill_inside;
269 _smart_reconfigure(sd);
273 _els_smart_icon_fill_inside_get(const Evas_Object *obj)
275 Smart_Data *sd = evas_object_smart_data_get(obj);
276 if (!sd) return EINA_FALSE;
277 return sd->fill_inside;
281 _els_smart_icon_scale_up_set(Evas_Object *obj, Eina_Bool scale_up)
285 sd = evas_object_smart_data_get(obj);
287 if (((sd->scale_up) && (scale_up)) ||
288 ((!sd->scale_up) && (!scale_up))) return;
289 sd->scale_up = scale_up;
290 _smart_reconfigure(sd);
294 _els_smart_icon_scale_up_get(const Evas_Object *obj)
296 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
297 if (!sd) return EINA_FALSE;
302 _els_smart_icon_scale_down_set(Evas_Object *obj, Eina_Bool scale_down)
306 sd = evas_object_smart_data_get(obj);
308 if (((sd->scale_down) && (scale_down)) ||
309 ((!sd->scale_down) && (!scale_down))) return;
310 sd->scale_down = scale_down;
311 _smart_reconfigure(sd);
315 _els_smart_icon_scale_down_get(const Evas_Object *obj)
317 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
318 if (!sd) return EINA_FALSE;
323 _els_smart_icon_scale_size_set(Evas_Object *obj, int size)
327 sd = evas_object_smart_data_get(obj);
330 if (!sd->obj) return;
333 evas_object_image_load_size_set(sd->obj, sd->size, sd->size);
337 _els_smart_icon_scale_size_get(const Evas_Object *obj)
339 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
345 _els_smart_icon_scale_set(Evas_Object *obj, double scale)
347 Smart_Data *sd = evas_object_smart_data_get(obj);
350 _smart_reconfigure(sd);
354 _els_smart_icon_scale_get(const Evas_Object *obj)
356 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
362 _els_smart_icon_orient_set(Evas_Object *obj, Elm_Image_Orient orient)
365 unsigned int *data, *data2 = NULL, *to, *from;
366 int x, y, w, hw, iw, ih;
368 sd = evas_object_smart_data_get(obj);
375 case ELM_IMAGE_FLIP_HORIZONTAL:
376 _els_smart_icon_flip_horizontal(sd);
378 case ELM_IMAGE_FLIP_VERTICAL:
379 _els_smart_icon_flip_vertical(sd);
381 case ELM_IMAGE_ROTATE_180_CW:
382 _els_smart_icon_rotate_180(sd);
388 evas_object_image_size_get(sd->obj, &iw, &ih);
389 /* we need separate destination memory if we want to rotate 90 or 270 degree */
390 evas_object_image_data_copy_set(sd->obj, data2);
398 evas_object_image_size_set(sd->obj, iw, ih);
399 data = evas_object_image_data_get(sd->obj, EINA_TRUE);
403 case ELM_IMAGE_FLIP_TRANSPOSE:
407 case ELM_IMAGE_FLIP_TRANSVERSE:
412 case ELM_IMAGE_ROTATE_90_CW:
416 case ELM_IMAGE_ROTATE_90_CCW:
422 ERR("unknown orient %d", orient);
423 evas_object_image_data_set(sd->obj, data); // give it back
424 if (data2) free(data2);
428 for (x = iw; --x >= 0;)
430 for (y = ih; --y >= 0;)
439 if (data2) free(data2);
440 evas_object_image_data_set(sd->obj, data);
441 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
442 _smart_reconfigure(sd);
446 _els_smart_icon_orient_get(const Evas_Object *obj)
448 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
454 * Turns on editing through drag and drop and copy and paste.
457 _els_smart_icon_edit_set(Evas_Object *obj, Eina_Bool edit, Evas_Object *parent)
459 Smart_Data *sd = evas_object_smart_data_get(obj);
464 printf("No editing edje objects yet (ever)\n");
468 /* Unfortunately eina bool is not a bool, but a char */
469 if (edit == sd->edit) return;
474 elm_drop_target_add(obj, ELM_SEL_FORMAT_IMAGE, _els_smart_icon_dropcb,
477 elm_drop_target_del(obj);
481 _els_smart_icon_edit_get(const Evas_Object *obj)
483 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
484 if (!sd) return EINA_FALSE;
489 _els_smart_icon_edje_get(Evas_Object *obj)
491 Smart_Data *sd = evas_object_smart_data_get(obj);
492 if (!sd) return NULL;
493 if (!sd->edje) return NULL;
498 _els_smart_icon_aspect_ratio_retained_set(Evas_Object *obj, Eina_Bool retained)
502 sd = evas_object_smart_data_get(obj);
505 retained = !!retained;
506 if (sd->aspect_ratio_retained == retained) return;
507 sd->aspect_ratio_retained = retained;
508 _smart_reconfigure(sd);
512 _els_smart_icon_aspect_ratio_retained_get(const Evas_Object *obj)
516 sd = evas_object_smart_data_get(obj);
517 if (!sd) return EINA_FALSE;
518 return sd->aspect_ratio_retained;
521 /* local subsystem globals */
523 _smart_reconfigure(Smart_Data *sd)
525 Evas_Coord x, y, w, h;
528 if (!sd->obj) return;
533 type = evas_object_type_get(sd->obj);
535 if (!strcmp(type, "edje"))
539 evas_object_move(sd->obj, x, y);
540 evas_object_resize(sd->obj, w, h);
546 evas_object_image_size_get(sd->obj, &iw, &ih);
548 iw = ((double)iw) * sd->scale;
549 ih = ((double)ih) * sd->scale;
554 if (sd->aspect_ratio_retained)
556 h = ((double)ih * w) / (double)iw;
562 w = ((double)iw * h) / (double)ih;
570 w = ((double)iw * h) / (double)ih;
584 x = sd->x + ((sd->w - w) / 2);
585 y = sd->y + ((sd->h - h) / 2);
586 evas_object_move(sd->obj, x, y);
587 evas_object_image_fill_set(sd->obj, 0, 0, w, h);
588 evas_object_resize(sd->obj, w, h);
595 if (_e_smart) return;
597 static const Evas_Smart_Class sc =
600 EVAS_SMART_CLASS_VERSION,
618 _e_smart = evas_smart_class_new(&sc);
623 _smart_add(Evas_Object *obj)
627 sd = calloc(1, sizeof(Smart_Data));
629 sd->obj = evas_object_image_add(evas_object_evas_get(obj));
631 evas_object_image_scale_hint_set(sd->obj, EVAS_IMAGE_SCALE_HINT_STATIC);
636 sd->fill_inside = EINA_TRUE;
637 sd->scale_up = EINA_TRUE;
638 sd->scale_down = EINA_TRUE;
639 sd->aspect_ratio_retained = EINA_TRUE;
642 evas_object_smart_member_add(sd->obj, obj);
643 evas_object_smart_data_set(obj, sd);
644 evas_object_event_callback_add(sd->obj, EVAS_CALLBACK_IMAGE_PRELOADED,
649 _smart_del(Evas_Object *obj)
653 sd = evas_object_smart_data_get(obj);
655 evas_object_del(sd->obj);
656 if (sd->prev) evas_object_del(sd->prev);
661 _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
665 sd = evas_object_smart_data_get(obj);
667 if ((sd->x == x) && (sd->y == y)) return;
670 _smart_reconfigure(sd);
674 _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
678 sd = evas_object_smart_data_get(obj);
680 if ((sd->w == w) && (sd->h == h)) return;
683 _smart_reconfigure(sd);
687 _smart_show(Evas_Object *obj)
691 sd = evas_object_smart_data_get(obj);
693 sd->show = EINA_TRUE;
696 evas_object_show(sd->obj);
697 if (sd->prev) evas_object_del(sd->prev);
703 _smart_hide(Evas_Object *obj)
707 sd = evas_object_smart_data_get(obj);
709 sd->show = EINA_FALSE;
710 evas_object_hide(sd->obj);
711 if (sd->prev) evas_object_del(sd->prev);
716 _smart_color_set(Evas_Object *obj, int r, int g, int b, int a)
720 sd = evas_object_smart_data_get(obj);
722 evas_object_color_set(sd->obj, r, g, b, a);
723 if (sd->prev) evas_object_color_set(sd->prev, r, g, b, a);
727 _smart_clip_set(Evas_Object *obj, Evas_Object * clip)
731 sd = evas_object_smart_data_get(obj);
733 evas_object_clip_set(sd->obj, clip);
734 if (sd->prev) evas_object_clip_set(sd->prev, clip);
738 _smart_clip_unset(Evas_Object *obj)
742 sd = evas_object_smart_data_get(obj);
744 evas_object_clip_unset(sd->obj);
745 if (sd->prev) evas_object_clip_unset(sd->prev);
749 _els_smart_icon_flip_horizontal(Smart_Data *sd)
752 unsigned int *p1, *p2, tmp;
755 evas_object_image_size_get(sd->obj, &iw, &ih);
756 data = evas_object_image_data_get(sd->obj, EINA_TRUE);
758 for (y = 0; y < ih; y++)
760 p1 = data + (y * iw);
761 p2 = data + ((y + 1) * iw) - 1;
762 for (x = 0; x < (iw >> 1); x++)
772 evas_object_image_data_set(sd->obj, data);
773 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
774 _smart_reconfigure(sd);
778 _els_smart_icon_flip_vertical(Smart_Data *sd)
781 unsigned int *p1, *p2, tmp;
784 evas_object_image_size_get(sd->obj, &iw, &ih);
785 data = evas_object_image_data_get(sd->obj, EINA_TRUE);
787 for (y = 0; y < (ih >> 1); y++)
789 p1 = data + (y * iw);
790 p2 = data + ((ih - 1 - y) * iw);
791 for (x = 0; x < iw; x++)
801 evas_object_image_data_set(sd->obj, data);
802 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
803 _smart_reconfigure(sd);
807 _els_smart_icon_rotate_180(Smart_Data *sd)
810 unsigned int *p1, *p2, tmp;
813 evas_object_image_size_get(sd->obj, &iw, &ih);
814 data = evas_object_image_data_get(sd->obj, 1);
828 evas_object_image_data_set(sd->obj, data);
829 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
830 _smart_reconfigure(sd);
834 _els_smart_icon_dropcb(void *elmobj,Evas_Object *obj, Elm_Selection_Data *drop)
836 _els_smart_icon_file_key_set(obj, drop->data, NULL);
837 evas_object_smart_callback_call(elmobj, "drop", drop->data);
841 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/