1 #include <Elementary.h>
4 typedef struct _Smart_Data Smart_Data;
13 Eina_Bool fill_inside : 1;
14 Eina_Bool scale_up : 1;
15 Eina_Bool scale_down : 1;
16 Eina_Bool preloading : 1;
20 Eina_Bool aspect_ratio_retained: 1;
21 Elm_Image_Orient orient;
24 /* local subsystem functions */
25 static void _smart_reconfigure(Smart_Data *sd);
26 static void _smart_init(void);
27 static void _smart_add(Evas_Object *obj);
28 static void _smart_del(Evas_Object *obj);
29 static void _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y);
30 static void _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h);
31 static void _smart_show(Evas_Object *obj);
32 static void _smart_hide(Evas_Object *obj);
33 static void _smart_color_set(Evas_Object *obj, int r, int g, int b, int a);
34 static void _smart_clip_set(Evas_Object *obj, Evas_Object * clip);
35 static void _smart_clip_unset(Evas_Object *obj);
37 static void _els_smart_icon_flip_horizontal(Smart_Data *sd);
38 static void _els_smart_icon_flip_vertical(Smart_Data *sd);
39 static void _els_smart_icon_rotate_180(Smart_Data *sd);
40 static Eina_Bool _els_smart_icon_dropcb(void *,Evas_Object *, Elm_Selection_Data *);
42 /* local subsystem globals */
43 static Evas_Smart *_e_smart = NULL;
45 /* externally accessible functions */
47 _els_smart_icon_add(Evas *evas)
50 return evas_object_smart_add(evas, _e_smart);
54 _preloaded(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event __UNUSED__)
56 Smart_Data *sd = data;
58 sd->preloading = EINA_FALSE;
62 evas_object_show(sd->obj);
64 if (sd->prev) evas_object_del(sd->prev);
69 _els_smart_icon_file_key_set(Evas_Object *obj, const char *file, const char *key)
74 sd = evas_object_smart_data_get(obj);
75 if (!sd) return EINA_FALSE;
77 if (sd->prev) evas_object_del(sd->prev);
78 pclip = evas_object_clip_get(sd->obj);
79 if (sd->obj) sd->prev = sd->obj;
80 sd->obj = evas_object_image_add(evas_object_evas_get(obj));
81 evas_object_event_callback_add(sd->obj, EVAS_CALLBACK_IMAGE_PRELOADED,
83 evas_object_smart_member_add(sd->obj, obj);
84 if (sd->prev) evas_object_smart_member_add(sd->prev, obj);
85 evas_object_image_scale_hint_set(sd->obj, EVAS_IMAGE_SCALE_HINT_STATIC);
86 evas_object_clip_set(sd->obj, pclip);
88 sd->edje = EINA_FALSE;
91 evas_object_image_load_size_set(sd->obj, sd->size, sd->size);
92 evas_object_image_file_set(sd->obj, file, key);
93 sd->preloading = EINA_TRUE;
95 evas_object_hide(sd->obj);
96 evas_object_image_preload(sd->obj, EINA_FALSE);
97 if (evas_object_image_load_error_get(sd->obj) != EVAS_LOAD_ERROR_NONE)
99 ERR("Things are going bad for '%s' (%p)", file, sd->obj);
102 _smart_reconfigure(sd);
107 _els_smart_icon_file_edje_set(Evas_Object *obj, const char *file, const char *part)
112 sd = evas_object_smart_data_get(obj);
113 if (!sd) return EINA_FALSE;
114 /* smart code here */
115 if (sd->prev) evas_object_del(sd->prev);
120 pclip = evas_object_clip_get(sd->obj);
121 if (sd->obj) evas_object_del(sd->obj);
122 sd->obj = edje_object_add(evas_object_evas_get(obj));
123 evas_object_smart_member_add(sd->obj, obj);
124 if (sd->show) evas_object_show(sd->obj);
125 evas_object_clip_set(sd->obj, pclip);
127 sd->edje = EINA_TRUE;
128 if (!edje_object_file_set(sd->obj, file, part))
130 _smart_reconfigure(sd);
135 _els_smart_icon_file_get(const Evas_Object *obj, const char **file, const char **key)
137 Smart_Data *sd = evas_object_smart_data_get(obj);
140 edje_object_file_get(sd->obj, file, key);
142 evas_object_image_file_get(sd->obj, file, key);
146 _els_smart_icon_smooth_scale_set(Evas_Object *obj, Eina_Bool smooth)
148 Smart_Data *sd = evas_object_smart_data_get(obj);
152 evas_object_image_smooth_scale_set(sd->obj, smooth);
156 _els_smart_icon_smooth_scale_get(const Evas_Object *obj)
158 Smart_Data *sd = evas_object_smart_data_get(obj);
159 if (!sd) return EINA_FALSE;
162 return evas_object_image_smooth_scale_get(sd->obj);
166 _els_smart_icon_object_get(const Evas_Object *obj)
168 Smart_Data *sd = evas_object_smart_data_get(obj);
169 if (!sd) return NULL;
174 _els_smart_icon_size_get(const Evas_Object *obj, int *w, int *h)
180 sd = evas_object_smart_data_get(obj);
182 if (!strcmp(evas_object_type_get(sd->obj), "edje"))
183 edje_object_size_min_get(sd->obj, &tw, &th);
185 evas_object_image_size_get(sd->obj, &tw, &th);
186 evas_object_geometry_get(sd->obj, NULL, NULL, &cw, &ch);
187 tw = tw > cw ? tw : cw;
188 th = th > ch ? th : ch;
189 tw = ((double)tw) * sd->scale;
190 th = ((double)th) * sd->scale;
196 _els_smart_icon_fill_inside_set(Evas_Object *obj, Eina_Bool fill_inside)
200 sd = evas_object_smart_data_get(obj);
202 if (((sd->fill_inside) && (fill_inside)) ||
203 ((!sd->fill_inside) && (!fill_inside))) return;
204 sd->fill_inside = fill_inside;
205 _smart_reconfigure(sd);
209 _els_smart_icon_fill_inside_get(const Evas_Object *obj)
211 Smart_Data *sd = evas_object_smart_data_get(obj);
212 if (!sd) return EINA_FALSE;
213 return sd->fill_inside;
217 _els_smart_icon_scale_up_set(Evas_Object *obj, Eina_Bool scale_up)
221 sd = evas_object_smart_data_get(obj);
223 if (((sd->scale_up) && (scale_up)) ||
224 ((!sd->scale_up) && (!scale_up))) return;
225 sd->scale_up = scale_up;
226 _smart_reconfigure(sd);
230 _els_smart_icon_scale_up_get(const Evas_Object *obj)
232 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
233 if (!sd) return EINA_FALSE;
238 _els_smart_icon_scale_down_set(Evas_Object *obj, Eina_Bool scale_down)
242 sd = evas_object_smart_data_get(obj);
244 if (((sd->scale_down) && (scale_down)) ||
245 ((!sd->scale_down) && (!scale_down))) return;
246 sd->scale_down = scale_down;
247 _smart_reconfigure(sd);
251 _els_smart_icon_scale_down_get(const Evas_Object *obj)
253 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
254 if (!sd) return EINA_FALSE;
259 _els_smart_icon_scale_size_set(Evas_Object *obj, int size)
263 sd = evas_object_smart_data_get(obj);
266 if (!sd->obj) return;
269 evas_object_image_load_size_set(sd->obj, sd->size, sd->size);
273 _els_smart_icon_scale_size_get(const Evas_Object *obj)
275 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
281 _els_smart_icon_scale_set(Evas_Object *obj, double scale)
283 Smart_Data *sd = evas_object_smart_data_get(obj);
286 _smart_reconfigure(sd);
290 _els_smart_icon_scale_get(const Evas_Object *obj)
292 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
298 _els_smart_icon_orient_set(Evas_Object *obj, Elm_Image_Orient orient)
302 unsigned int *data, *data2, *to, *from;
303 int x, y, w, hw, iw, ih;
304 const char *file, *key;
306 sd = evas_object_smart_data_get(obj);
313 case ELM_IMAGE_FLIP_HORIZONTAL:
314 _els_smart_icon_flip_horizontal(sd);
316 case ELM_IMAGE_FLIP_VERTICAL:
317 _els_smart_icon_flip_vertical(sd);
319 case ELM_IMAGE_ROTATE_180_CW:
320 _els_smart_icon_rotate_180(sd);
326 evas_object_image_size_get(sd->obj, &iw, &ih);
327 evas_object_image_file_get(sd->obj, &file, &key);
328 tmp = evas_object_image_add(evas_object_evas_get(sd->obj));
329 evas_object_image_file_set(tmp, file, key);
330 data2 = evas_object_image_data_get(tmp, EINA_FALSE);
337 evas_object_image_size_set(sd->obj, iw, ih);
338 data = evas_object_image_data_get(sd->obj, EINA_TRUE);
341 case ELM_IMAGE_FLIP_TRANSPOSE:
345 case ELM_IMAGE_FLIP_TRANSVERSE:
350 case ELM_IMAGE_ROTATE_90_CW:
354 case ELM_IMAGE_ROTATE_90_CCW:
360 ERR("unknown orient %d", orient);
361 evas_object_del(tmp);
362 evas_object_image_data_set(sd->obj, data); // give it back
366 for (x = iw; --x >= 0;)
368 for (y = ih; --y >= 0;)
377 evas_object_del(tmp);
378 evas_object_image_data_set(sd->obj, data);
379 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
380 _smart_reconfigure(sd);
384 _els_smart_icon_orient_get(const Evas_Object *obj)
386 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
392 * Turns on editing through drag and drop and copy and paste.
395 _els_smart_icon_edit_set(Evas_Object *obj, Eina_Bool edit, Evas_Object *parent)
397 Smart_Data *sd = evas_object_smart_data_get(obj);
402 printf("No editing edje objects yet (ever)\n");
406 /* Unfortunately eina bool is not a bool, but a char */
407 if (edit == sd->edit) return;
412 elm_drop_target_add(obj, ELM_SEL_FORMAT_IMAGE, _els_smart_icon_dropcb,
415 elm_drop_target_del(obj);
419 _els_smart_icon_edit_get(const Evas_Object *obj)
421 Smart_Data *sd; sd = evas_object_smart_data_get(obj);
422 if (!sd) return EINA_FALSE;
427 _els_smart_icon_edje_get(Evas_Object *obj)
429 Smart_Data *sd = evas_object_smart_data_get(obj);
430 if (!sd) return NULL;
431 if (!sd->edje) return NULL;
436 _els_smart_icon_aspect_ratio_retained_set(Evas_Object *obj, Eina_Bool retained)
440 sd = evas_object_smart_data_get(obj);
443 retained = !!retained;
444 if (sd->aspect_ratio_retained == retained) return;
445 sd->aspect_ratio_retained = retained;
446 _smart_reconfigure(sd);
450 _els_smart_icon_aspect_ratio_retained_get(const Evas_Object *obj)
454 sd = evas_object_smart_data_get(obj);
455 if (!sd) return EINA_FALSE;
456 return sd->aspect_ratio_retained;
459 /* local subsystem globals */
461 _smart_reconfigure(Smart_Data *sd)
463 Evas_Coord x, y, w, h;
465 if (!sd->obj) return;
470 if (!strcmp(evas_object_type_get(sd->obj), "edje"))
474 evas_object_move(sd->obj, x, y);
475 evas_object_resize(sd->obj, w, h);
481 evas_object_image_size_get(sd->obj, &iw, &ih);
483 iw = ((double)iw) * sd->scale;
484 ih = ((double)ih) * sd->scale;
489 if (sd->aspect_ratio_retained)
491 h = ((double)ih * w) / (double)iw;
497 w = ((double)iw * h) / (double)ih;
505 w = ((double)iw * h) / (double)ih;
519 x = sd->x + ((sd->w - w) / 2);
520 y = sd->y + ((sd->h - h) / 2);
521 evas_object_move(sd->obj, x, y);
522 evas_object_image_fill_set(sd->obj, 0, 0, w, h);
523 evas_object_resize(sd->obj, w, h);
530 if (_e_smart) return;
532 static const Evas_Smart_Class sc =
535 EVAS_SMART_CLASS_VERSION,
553 _e_smart = evas_smart_class_new(&sc);
558 _smart_add(Evas_Object *obj)
562 sd = calloc(1, sizeof(Smart_Data));
564 sd->obj = evas_object_image_add(evas_object_evas_get(obj));
566 evas_object_image_scale_hint_set(sd->obj, EVAS_IMAGE_SCALE_HINT_STATIC);
571 sd->fill_inside = EINA_TRUE;
572 sd->scale_up = EINA_TRUE;
573 sd->scale_down = EINA_TRUE;
574 sd->aspect_ratio_retained = EINA_TRUE;
577 evas_object_smart_member_add(sd->obj, obj);
578 evas_object_smart_data_set(obj, sd);
579 evas_object_event_callback_add(sd->obj, EVAS_CALLBACK_IMAGE_PRELOADED,
584 _smart_del(Evas_Object *obj)
588 sd = evas_object_smart_data_get(obj);
590 evas_object_del(sd->obj);
591 if (sd->prev) evas_object_del(sd->prev);
596 _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
600 sd = evas_object_smart_data_get(obj);
602 if ((sd->x == x) && (sd->y == y)) return;
605 _smart_reconfigure(sd);
609 _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
613 sd = evas_object_smart_data_get(obj);
615 if ((sd->w == w) && (sd->h == h)) return;
618 _smart_reconfigure(sd);
622 _smart_show(Evas_Object *obj)
626 sd = evas_object_smart_data_get(obj);
628 sd->show = EINA_TRUE;
631 evas_object_show(sd->obj);
632 if (sd->prev) evas_object_del(sd->prev);
638 _smart_hide(Evas_Object *obj)
642 sd = evas_object_smart_data_get(obj);
644 sd->show = EINA_FALSE;
645 evas_object_hide(sd->obj);
646 if (sd->prev) evas_object_del(sd->prev);
651 _smart_color_set(Evas_Object *obj, int r, int g, int b, int a)
655 sd = evas_object_smart_data_get(obj);
657 evas_object_color_set(sd->obj, r, g, b, a);
658 if (sd->prev) evas_object_color_set(sd->prev, r, g, b, a);
662 _smart_clip_set(Evas_Object *obj, Evas_Object * clip)
666 sd = evas_object_smart_data_get(obj);
668 evas_object_clip_set(sd->obj, clip);
669 if (sd->prev) evas_object_clip_set(sd->prev, clip);
673 _smart_clip_unset(Evas_Object *obj)
677 sd = evas_object_smart_data_get(obj);
679 evas_object_clip_unset(sd->obj);
680 if (sd->prev) evas_object_clip_unset(sd->prev);
684 _els_smart_icon_flip_horizontal(Smart_Data *sd)
687 unsigned int *p1, *p2, tmp;
690 evas_object_image_size_get(sd->obj, &iw, &ih);
691 data = evas_object_image_data_get(sd->obj, EINA_TRUE);
693 for (y = 0; y < ih; y++)
695 p1 = data + (y * iw);
696 p2 = data + ((y + 1) * iw) - 1;
697 for (x = 0; x < (iw >> 1); x++)
707 evas_object_image_data_set(sd->obj, data);
708 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
709 _smart_reconfigure(sd);
713 _els_smart_icon_flip_vertical(Smart_Data *sd)
716 unsigned int *p1, *p2, tmp;
719 evas_object_image_size_get(sd->obj, &iw, &ih);
720 data = evas_object_image_data_get(sd->obj, EINA_TRUE);
722 for (y = 0; y < (ih >> 1); y++)
724 p1 = data + (y * iw);
725 p2 = data + ((ih - 1 - y) * iw);
726 for (x = 0; x < iw; x++)
736 evas_object_image_data_set(sd->obj, data);
737 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
738 _smart_reconfigure(sd);
742 _els_smart_icon_rotate_180(Smart_Data *sd)
745 unsigned int *p1, *p2, tmp;
748 evas_object_image_size_get(sd->obj, &iw, &ih);
749 data = evas_object_image_data_get(sd->obj, 1);
763 evas_object_image_data_set(sd->obj, data);
764 evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
765 _smart_reconfigure(sd);
769 _els_smart_icon_dropcb(void *elmobj,Evas_Object *obj, Elm_Selection_Data *drop)
771 _els_smart_icon_file_key_set(obj, drop->data, NULL);
772 evas_object_smart_callback_call(elmobj, "drop", drop->data);
776 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/