1 #include <Elementary.h>
4 typedef struct _Smart_Data Smart_Data;
12 Eina_Bool fill_inside : 1;
13 Eina_Bool scale_up : 1;
14 Eina_Bool scale_down : 1;
15 Eina_Bool preloading : 1;
19 Eina_Bool aspect_ratio_retained: 1;
20 Elm_Image_Orient orient;
23 /* local subsystem functions */
24 static void _smart_reconfigure(Smart_Data *sd);
25 static void _smart_init(void);
26 static void _smart_add(Evas_Object *obj);
27 static void _smart_del(Evas_Object *obj);
28 static void _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y);
29 static void _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h);
30 static void _smart_show(Evas_Object *obj);
31 static void _smart_hide(Evas_Object *obj);
32 static void _smart_color_set(Evas_Object *obj, int r, int g, int b, int a);
33 static void _smart_clip_set(Evas_Object *obj, Evas_Object * clip);
34 static void _smart_clip_unset(Evas_Object *obj);
36 static void _els_smart_icon_flip_horizontal(Smart_Data *sd);
37 static void _els_smart_icon_flip_vertical(Smart_Data *sd);
38 static void _els_smart_icon_rotate_180(Smart_Data *sd);
39 static Eina_Bool _els_smart_icon_dropcb(void *,Evas_Object *, Elm_Selection_Data *);
41 /* local subsystem globals */
42 static Evas_Smart *_e_smart = NULL;
44 /* externally accessible functions */
46 _els_smart_icon_add(Evas *evas)
49 return evas_object_smart_add(evas, _e_smart);
53 _preloaded(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event __UNUSED__)
55 Smart_Data *sd = data;
57 sd->preloading = EINA_FALSE;
59 evas_object_show(sd->obj);
63 _els_smart_icon_file_key_set(Evas_Object *obj, const char *file, const char *key)
68 sd = evas_object_smart_data_get(obj);
69 if (!sd) return EINA_FALSE;
73 pclip = evas_object_clip_get(sd->obj);
74 if (sd->obj) evas_object_del(sd->obj);
75 sd->obj = evas_object_image_add(evas_object_evas_get(obj));
76 evas_object_image_scale_hint_set(sd->obj, EVAS_IMAGE_SCALE_HINT_STATIC);
77 evas_object_smart_member_add(sd->obj, obj);
78 evas_object_event_callback_add(sd->obj, EVAS_CALLBACK_IMAGE_PRELOADED,
80 evas_object_clip_set(sd->obj, pclip);
81 sd->edje = EINA_FALSE;
84 evas_object_image_load_size_set(sd->obj, sd->size, sd->size);
85 evas_object_image_file_set(sd->obj, file, key);
86 sd->preloading = EINA_TRUE;
88 evas_object_hide(sd->obj);
89 evas_object_image_preload(sd->obj, EINA_FALSE);
90 if (evas_object_image_load_error_get(sd->obj) != EVAS_LOAD_ERROR_NONE)
92 ERR("Things are going bad for '%s' (%p)", file, sd->obj);
95 _smart_reconfigure(sd);
100 _els_smart_icon_file_edje_set(Evas_Object *obj, const char *file, const char *part)
105 sd = evas_object_smart_data_get(obj);
106 if (!sd) return EINA_FALSE;
107 /* smart code here */
110 pclip = evas_object_clip_get(sd->obj);
111 if (sd->obj) evas_object_del(sd->obj);
112 sd->obj = edje_object_add(evas_object_evas_get(obj));
113 evas_object_smart_member_add(sd->obj, obj);
114 if (sd->show) evas_object_show(sd->obj);
115 evas_object_clip_set(sd->obj, pclip);
117 sd->edje = EINA_TRUE;
118 if (!edje_object_file_set(sd->obj, file, part))
120 _smart_reconfigure(sd);
125 _els_smart_icon_file_get(const Evas_Object *obj, const char **file, const char **key)
127 Smart_Data *sd = evas_object_smart_data_get(obj);
130 edje_object_file_get(sd->obj, file, key);
132 evas_object_image_file_get(sd->obj, file, key);
136 _els_smart_icon_smooth_scale_set(Evas_Object *obj, Eina_Bool smooth)
138 Smart_Data *sd = evas_object_smart_data_get(obj);
142 evas_object_image_smooth_scale_set(sd->obj, smooth);
146 _els_smart_icon_smooth_scale_get(const Evas_Object *obj)
148 Smart_Data *sd = evas_object_smart_data_get(obj);
149 if (!sd) return EINA_FALSE;
152 return evas_object_image_smooth_scale_get(sd->obj);
156 _els_smart_icon_object_get(const Evas_Object *obj)
158 Smart_Data *sd = evas_object_smart_data_get(obj);
159 if (!sd) return NULL;
164 _els_smart_icon_size_get(const Evas_Object *obj, int *w, int *h)
170 sd = evas_object_smart_data_get(obj);
172 if (!strcmp(evas_object_type_get(sd->obj), "edje"))
173 edje_object_size_min_get(sd->obj, &tw, &th);
175 evas_object_image_size_get(sd->obj, &tw, &th);
176 evas_object_geometry_get(sd->obj, NULL, NULL, &cw, &ch);
177 tw = tw > cw ? tw : cw;
178 th = th > ch ? th : ch;
179 tw = ((double)tw) * sd->scale;
180 th = ((double)th) * sd->scale;
186 _els_smart_icon_fill_inside_set(Evas_Object *obj, Eina_Bool fill_inside)
190 sd = evas_object_smart_data_get(obj);
192 if (((sd->fill_inside) && (fill_inside)) ||
193 ((!sd->fill_inside) && (!fill_inside))) return;
194 sd->fill_inside = fill_inside;
195 _smart_reconfigure(sd);
199 _els_smart_icon_fill_inside_get(const Evas_Object *obj)
201 Smart_Data *sd = evas_object_smart_data_get(obj);
202 if (!sd) return EINA_FALSE;
203 return sd->fill_inside;
207 _els_smart_icon_scale_up_set(Evas_Object *obj, Eina_Bool scale_up)
211 sd = evas_object_smart_data_get(obj);
213 if (((sd->scale_up) && (scale_up)) ||
214 ((!sd->scale_up) && (!scale_up))) return;
215 sd->scale_up = scale_up;
216 _smart_reconfigure(sd);
220 _els_smart_icon_scale_up_get(const Evas_Object *obj)
222 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
223 if (!sd) return EINA_FALSE;
228 _els_smart_icon_scale_down_set(Evas_Object *obj, Eina_Bool scale_down)
232 sd = evas_object_smart_data_get(obj);
234 if (((sd->scale_down) && (scale_down)) ||
235 ((!sd->scale_down) && (!scale_down))) return;
236 sd->scale_down = scale_down;
237 _smart_reconfigure(sd);
241 _els_smart_icon_scale_down_get(const Evas_Object *obj)
243 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
244 if (!sd) return EINA_FALSE;
249 _els_smart_icon_scale_size_set(Evas_Object *obj, int size)
253 sd = evas_object_smart_data_get(obj);
256 if (!sd->obj) return;
259 evas_object_image_load_size_set(sd->obj, sd->size, sd->size);
263 _els_smart_icon_scale_size_get(const Evas_Object *obj)
265 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
271 _els_smart_icon_scale_set(Evas_Object *obj, double scale)
273 Smart_Data *sd = evas_object_smart_data_get(obj);
276 _smart_reconfigure(sd);
280 _els_smart_icon_scale_get(const Evas_Object *obj)
282 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
288 _els_smart_icon_orient_set(Evas_Object *obj, Elm_Image_Orient orient)
292 unsigned int *data, *data2, *to, *from;
293 int x, y, w, hw, iw, ih;
294 const char *file, *key;
296 sd = evas_object_smart_data_get(obj);
303 case ELM_IMAGE_FLIP_HORIZONTAL:
304 _els_smart_icon_flip_horizontal(sd);
306 case ELM_IMAGE_FLIP_VERTICAL:
307 _els_smart_icon_flip_vertical(sd);
309 case ELM_IMAGE_ROTATE_180_CW:
310 _els_smart_icon_rotate_180(sd);
316 evas_object_image_size_get(sd->obj, &iw, &ih);
317 evas_object_image_file_get(sd->obj, &file, &key);
318 tmp = evas_object_image_add(evas_object_evas_get(sd->obj));
319 evas_object_image_file_set(tmp, file, key);
320 data2 = evas_object_image_data_get(tmp, EINA_FALSE);
327 evas_object_image_size_set(sd->obj, iw, ih);
328 data = evas_object_image_data_get(sd->obj, EINA_TRUE);
331 case ELM_IMAGE_FLIP_TRANSPOSE:
335 case ELM_IMAGE_FLIP_TRANSVERSE:
340 case ELM_IMAGE_ROTATE_90_CW:
344 case ELM_IMAGE_ROTATE_90_CCW:
350 ERR("unknown orient %d", orient);
351 evas_object_del(tmp);
352 evas_object_image_data_set(sd->obj, data); // give it back
356 for (x = iw; --x >= 0;)
358 for (y = ih; --y >= 0;)
367 evas_object_del(tmp);
368 evas_object_image_data_set(sd->obj, data);
369 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
370 _smart_reconfigure(sd);
374 _els_smart_icon_orient_get(const Evas_Object *obj)
376 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
382 * Turns on editing through drag and drop and copy and paste.
385 _els_smart_icon_edit_set(Evas_Object *obj, Eina_Bool edit, Evas_Object *parent)
387 Smart_Data *sd = evas_object_smart_data_get(obj);
392 printf("No editing edje objects yet (ever)\n");
396 /* Unfortunately eina bool is not a bool, but a char */
397 if (edit == sd->edit) return;
402 elm_drop_target_add(obj, ELM_SEL_FORMAT_IMAGE, _els_smart_icon_dropcb,
405 elm_drop_target_del(obj);
409 _els_smart_icon_edit_get(const Evas_Object *obj)
411 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
412 if (!sd) return EINA_FALSE;
417 _els_smart_icon_edje_get(Evas_Object *obj)
419 Smart_Data *sd = evas_object_smart_data_get(obj);
420 if (!sd) return NULL;
421 if (!sd->edje) return NULL;
426 _els_smart_icon_aspect_ratio_retained_set(Evas_Object *obj, Eina_Bool retained)
430 sd = evas_object_smart_data_get(obj);
433 retained = !!retained;
434 if (sd->aspect_ratio_retained == retained) return;
435 sd->aspect_ratio_retained = retained;
436 _smart_reconfigure(sd);
440 _els_smart_icon_aspect_ratio_retained_get(const Evas_Object *obj)
444 sd = evas_object_smart_data_get(obj);
445 if (!sd) return EINA_FALSE;
446 return sd->aspect_ratio_retained;
449 /* local subsystem globals */
451 _smart_reconfigure(Smart_Data *sd)
453 Evas_Coord x, y, w, h;
455 if (!sd->obj) return;
460 if (!strcmp(evas_object_type_get(sd->obj), "edje"))
464 evas_object_move(sd->obj, x, y);
465 evas_object_resize(sd->obj, w, h);
471 evas_object_image_size_get(sd->obj, &iw, &ih);
473 iw = ((double)iw) * sd->scale;
474 ih = ((double)ih) * sd->scale;
479 if (sd->aspect_ratio_retained)
481 h = ((double)ih * w) / (double)iw;
487 w = ((double)iw * h) / (double)ih;
495 w = ((double)iw * h) / (double)ih;
509 x = sd->x + ((sd->w - w) / 2);
510 y = sd->y + ((sd->h - h) / 2);
511 evas_object_move(sd->obj, x, y);
512 evas_object_image_fill_set(sd->obj, 0, 0, w, h);
513 evas_object_resize(sd->obj, w, h);
520 if (_e_smart) return;
522 static const Evas_Smart_Class sc =
525 EVAS_SMART_CLASS_VERSION,
543 _e_smart = evas_smart_class_new(&sc);
548 _smart_add(Evas_Object *obj)
552 sd = calloc(1, sizeof(Smart_Data));
554 sd->obj = evas_object_image_add(evas_object_evas_get(obj));
555 evas_object_image_scale_hint_set(sd->obj, EVAS_IMAGE_SCALE_HINT_STATIC);
560 sd->fill_inside = EINA_TRUE;
561 sd->scale_up = EINA_TRUE;
562 sd->scale_down = EINA_TRUE;
563 sd->aspect_ratio_retained = EINA_TRUE;
566 evas_object_smart_member_add(sd->obj, obj);
567 evas_object_smart_data_set(obj, sd);
568 evas_object_event_callback_add(sd->obj, EVAS_CALLBACK_IMAGE_PRELOADED,
573 _smart_del(Evas_Object *obj)
577 sd = evas_object_smart_data_get(obj);
579 evas_object_del(sd->obj);
584 _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
588 sd = evas_object_smart_data_get(obj);
590 if ((sd->x == x) && (sd->y == y)) return;
593 _smart_reconfigure(sd);
597 _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
601 sd = evas_object_smart_data_get(obj);
603 if ((sd->w == w) && (sd->h == h)) return;
606 _smart_reconfigure(sd);
610 _smart_show(Evas_Object *obj)
614 sd = evas_object_smart_data_get(obj);
616 sd->show = EINA_TRUE;
618 evas_object_show(sd->obj);
622 _smart_hide(Evas_Object *obj)
626 sd = evas_object_smart_data_get(obj);
628 sd->show = EINA_FALSE;
629 evas_object_hide(sd->obj);
633 _smart_color_set(Evas_Object *obj, int r, int g, int b, int a)
637 sd = evas_object_smart_data_get(obj);
639 evas_object_color_set(sd->obj, r, g, b, a);
643 _smart_clip_set(Evas_Object *obj, Evas_Object * clip)
647 sd = evas_object_smart_data_get(obj);
649 evas_object_clip_set(sd->obj, clip);
653 _smart_clip_unset(Evas_Object *obj)
657 sd = evas_object_smart_data_get(obj);
659 evas_object_clip_unset(sd->obj);
663 _els_smart_icon_flip_horizontal(Smart_Data *sd)
666 unsigned int *p1, *p2, tmp;
669 evas_object_image_size_get(sd->obj, &iw, &ih);
670 data = evas_object_image_data_get(sd->obj, EINA_TRUE);
672 for (y = 0; y < ih; y++)
674 p1 = data + (y * iw);
675 p2 = data + ((y + 1) * iw) - 1;
676 for (x = 0; x < (iw >> 1); x++)
686 evas_object_image_data_set(sd->obj, data);
687 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
688 _smart_reconfigure(sd);
692 _els_smart_icon_flip_vertical(Smart_Data *sd)
695 unsigned int *p1, *p2, tmp;
698 evas_object_image_size_get(sd->obj, &iw, &ih);
699 data = evas_object_image_data_get(sd->obj, EINA_TRUE);
701 for (y = 0; y < (ih >> 1); y++)
703 p1 = data + (y * iw);
704 p2 = data + ((ih - 1 - y) * iw);
705 for (x = 0; x < iw; x++)
715 evas_object_image_data_set(sd->obj, data);
716 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
717 _smart_reconfigure(sd);
721 _els_smart_icon_rotate_180(Smart_Data *sd)
724 unsigned int *p1, *p2, tmp;
727 evas_object_image_size_get(sd->obj, &iw, &ih);
728 data = evas_object_image_data_get(sd->obj, 1);
742 evas_object_image_data_set(sd->obj, data);
743 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
744 _smart_reconfigure(sd);
748 _els_smart_icon_dropcb(void *elmobj,Evas_Object *obj, Elm_Selection_Data *drop)
750 _els_smart_icon_file_key_set(obj, drop->data, NULL);
751 evas_object_smart_callback_call(elmobj, "drop", drop->data);
755 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/