Merge "[NoContents] On Style set, text should be visible."
[framework/uifw/elementary.git] / src / lib / els_icon.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 typedef struct _Smart_Data Smart_Data;
5
6 struct _Smart_Data
7 {
8    Evas_Coord   x, y, w, h;
9    Evas_Object *obj;
10    int          size;
11    double       scale;
12    Eina_Bool fill_inside : 1;
13    Eina_Bool scale_up : 1;
14    Eina_Bool scale_down : 1;
15    Eina_Bool preloading : 1;
16    Eina_Bool show : 1;
17    Eina_Bool edit : 1;
18    Eina_Bool edje : 1;
19    Elm_Image_Orient orient;
20 };
21
22 /* local subsystem functions */
23 static void _smart_reconfigure(Smart_Data *sd);
24 static void _smart_init(void);
25 static void _smart_add(Evas_Object *obj);
26 static void _smart_del(Evas_Object *obj);
27 static void _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y);
28 static void _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h);
29 static void _smart_show(Evas_Object *obj);
30 static void _smart_hide(Evas_Object *obj);
31 static void _smart_color_set(Evas_Object *obj, int r, int g, int b, int a);
32 static void _smart_clip_set(Evas_Object *obj, Evas_Object * clip);
33 static void _smart_clip_unset(Evas_Object *obj);
34
35 static void _els_smart_icon_flip_horizontal(Smart_Data *sd);
36 static void _els_smart_icon_flip_vertical(Smart_Data *sd);
37 static void _els_smart_icon_rotate_180(Smart_Data *sd);
38 static Eina_Bool _els_smart_icon_dropcb(void *,Evas_Object *, Elm_Selection_Data *);
39
40 /* local subsystem globals */
41 static Evas_Smart *_e_smart = NULL;
42
43 /* externally accessible functions */
44 Evas_Object *
45 _els_smart_icon_add(Evas *evas)
46 {
47    _smart_init();
48    return evas_object_smart_add(evas, _e_smart);
49 }
50
51 static void
52 _preloaded(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event __UNUSED__)
53 {
54    Smart_Data *sd = data;
55
56    sd->preloading = EINA_FALSE;
57    if (sd->show)
58      evas_object_show(sd->obj);
59 }
60
61 Eina_Bool
62 _els_smart_icon_file_key_set(Evas_Object *obj, const char *file, const char *key)
63 {
64    Smart_Data *sd;
65    Evas_Object *pclip;
66
67    sd = evas_object_smart_data_get(obj);
68    if (!sd) return EINA_FALSE;
69    /* smart code here */
70    if (sd->edje)
71      {
72         pclip = evas_object_clip_get(sd->obj);
73         if (sd->obj) evas_object_del(sd->obj);
74         sd->obj = evas_object_image_add(evas_object_evas_get(obj));
75         evas_object_image_scale_hint_set(sd->obj, EVAS_IMAGE_SCALE_HINT_STATIC);
76         evas_object_smart_member_add(sd->obj, obj);
77         evas_object_event_callback_add(sd->obj, EVAS_CALLBACK_IMAGE_PRELOADED, 
78                                        _preloaded, sd);
79         evas_object_clip_set(sd->obj, pclip);
80         sd->edje = EINA_FALSE;
81      }
82    if (!sd->size)
83      evas_object_image_load_size_set(sd->obj, sd->size, sd->size);
84    evas_object_image_file_set(sd->obj, file, key);
85    /* by default preload off by seok.j.jeong */
86    sd->preloading = EINA_FALSE;
87    sd->show = EINA_TRUE;
88    if (sd->preloading)
89      evas_object_image_preload(sd->obj, EINA_FALSE);
90    if (sd->preloading) 
91      evas_object_hide(sd->obj);
92    if (evas_object_image_load_error_get(sd->obj) != EVAS_LOAD_ERROR_NONE)
93      return EINA_FALSE;
94    _smart_reconfigure(sd);
95    return EINA_TRUE;
96 }
97
98 Eina_Bool
99 _els_smart_icon_file_edje_set(Evas_Object *obj, const char *file, const char *part)
100 {
101    Smart_Data *sd;
102    Evas_Object *pclip;
103
104    sd = evas_object_smart_data_get(obj);
105    if (!sd) return EINA_FALSE;
106    /* smart code here */
107    if (!sd->edje)
108      {
109         pclip = evas_object_clip_get(sd->obj);
110         if (sd->obj) evas_object_del(sd->obj);
111         sd->obj = edje_object_add(evas_object_evas_get(obj));
112         evas_object_smart_member_add(sd->obj, obj);
113         if (sd->show) evas_object_show(sd->obj);
114         evas_object_clip_set(sd->obj, pclip);
115      }
116    sd->edje = EINA_TRUE;
117    if (!edje_object_file_set(sd->obj, file, part))
118      return EINA_FALSE;
119    _smart_reconfigure(sd);
120    return EINA_TRUE;
121 }
122
123 void
124 _els_smart_icon_file_get(const Evas_Object *obj, const char **file, const char **key)
125 {
126    Smart_Data *sd = evas_object_smart_data_get(obj);
127    if (!sd) return;
128    if (sd->edje)
129      edje_object_file_get(sd->obj, file, key);
130    else
131      evas_object_image_file_get(sd->obj, file, key);
132 }
133
134 void
135 _els_smart_icon_smooth_scale_set(Evas_Object *obj, Eina_Bool smooth)
136 {
137    Smart_Data *sd = evas_object_smart_data_get(obj);
138    if (!sd) return;
139    if (sd->edje)
140      return;
141    evas_object_image_smooth_scale_set(sd->obj, smooth);
142 }
143
144 Eina_Bool
145 _els_smart_icon_smooth_scale_get(const Evas_Object *obj)
146 {
147    Smart_Data *sd = evas_object_smart_data_get(obj);
148    if (!sd) return EINA_FALSE;
149    if (sd->edje)
150      return EINA_FALSE;
151    return evas_object_image_smooth_scale_get(sd->obj);
152 }
153
154 Evas_Object *
155 _els_smart_icon_object_get(const Evas_Object *obj)
156 {
157    Smart_Data *sd = evas_object_smart_data_get(obj);
158    if (!sd) return NULL;
159    return sd->obj;
160 }
161
162 void
163 _els_smart_icon_size_get(const Evas_Object *obj, int *w, int *h)
164 {
165    Smart_Data *sd;
166    int tw, th;
167
168    sd = evas_object_smart_data_get(obj);
169    if (!sd) return;
170
171    const char *type = evas_object_type_get(sd->obj);
172    if ((type) && !strcmp(type, "edje"))
173      edje_object_size_min_get(sd->obj, &tw, &th);
174    else
175      evas_object_image_size_get(sd->obj, &tw, &th);
176    tw = ((double)tw) * sd->scale;
177    th = ((double)th) * sd->scale;
178    if (w) *w = tw;
179    if (h) *h = th;
180 }
181
182 void
183 _els_smart_icon_fill_inside_set(Evas_Object *obj, Eina_Bool fill_inside)
184 {
185    Smart_Data *sd;
186
187    sd = evas_object_smart_data_get(obj);
188    if (!sd) return;
189    if (((sd->fill_inside) && (fill_inside)) ||
190        ((!sd->fill_inside) && (!fill_inside))) return;
191    sd->fill_inside = fill_inside;
192    _smart_reconfigure(sd);
193 }
194
195 Eina_Bool
196 _els_smart_icon_fill_inside_get(const Evas_Object *obj)
197 {
198    Smart_Data *sd = evas_object_smart_data_get(obj);
199    if (!sd) return EINA_FALSE;
200    return sd->fill_inside;
201 }
202
203 void
204 _els_smart_icon_scale_up_set(Evas_Object *obj, Eina_Bool scale_up)
205 {
206    Smart_Data *sd;
207
208    sd = evas_object_smart_data_get(obj);
209    if (!sd) return;
210    if (((sd->scale_up) && (scale_up)) ||
211        ((!sd->scale_up) && (!scale_up))) return;
212    sd->scale_up = scale_up;
213    _smart_reconfigure(sd);
214 }
215
216 Eina_Bool
217 _els_smart_icon_scale_up_get(const Evas_Object *obj)
218 {
219    Smart_Data *sd; sd = evas_object_smart_data_get(obj);
220    if (!sd) return EINA_FALSE;
221    return sd->scale_up;
222 }
223
224 void
225 _els_smart_icon_scale_down_set(Evas_Object *obj, Eina_Bool scale_down)
226 {
227    Smart_Data *sd;
228
229    sd = evas_object_smart_data_get(obj);
230    if (!sd) return;
231    if (((sd->scale_down) && (scale_down)) ||
232        ((!sd->scale_down) && (!scale_down))) return;
233    sd->scale_down = scale_down;
234    _smart_reconfigure(sd);
235 }
236
237 Eina_Bool
238 _els_smart_icon_scale_down_get(const Evas_Object *obj)
239 {
240    Smart_Data *sd; sd = evas_object_smart_data_get(obj);
241    if (!sd) return EINA_FALSE;
242    return sd->scale_up;
243 }
244
245 void
246 _els_smart_icon_scale_size_set(Evas_Object *obj, int size)
247 {
248    Smart_Data *sd;
249
250    sd = evas_object_smart_data_get(obj);
251    if (!sd) return;
252    sd->size = size;
253    if (!sd->obj) return;
254    if (sd->edje)
255      return;
256    evas_object_image_load_size_set(sd->obj, sd->size, sd->size);
257 }
258
259 int
260 _els_smart_icon_scale_size_get(const Evas_Object *obj)
261 {
262    Smart_Data *sd; sd = evas_object_smart_data_get(obj);
263    if (!sd) return 0;
264    return sd->size;
265 }
266
267 void
268 _els_smart_icon_scale_set(Evas_Object *obj, double scale)
269 {
270    Smart_Data *sd = evas_object_smart_data_get(obj);
271    if (!sd) return;
272    sd->scale = scale;
273    _smart_reconfigure(sd);
274 }
275
276 double
277 _els_smart_icon_scale_get(const Evas_Object *obj)
278 {
279    Smart_Data *sd; sd = evas_object_smart_data_get(obj);
280    if (!sd) return 0.0;
281    return sd->scale;
282 }
283
284 void
285 _els_smart_icon_orient_set(Evas_Object *obj, Elm_Image_Orient orient)
286 {
287    Smart_Data   *sd;
288    Evas_Object  *tmp;
289    unsigned int *data, *data2, *to, *from;
290    int           x, y, w, hw, iw, ih;
291    const char   *file, *key;
292
293    sd = evas_object_smart_data_get(obj);
294    if (!sd) return;
295    if (sd->edje)
296      return;
297
298    switch (orient)
299      {
300       case ELM_IMAGE_FLIP_HORIZONTAL:
301          _els_smart_icon_flip_horizontal(sd);
302          return;
303       case ELM_IMAGE_FLIP_VERTICAL:
304          _els_smart_icon_flip_vertical(sd);
305          return;
306       case ELM_IMAGE_ROTATE_180_CW:
307          _els_smart_icon_rotate_180(sd);
308          return;
309       default:
310          break;
311      }
312
313    evas_object_image_size_get(sd->obj, &iw, &ih);
314    evas_object_image_file_get(sd->obj, &file, &key);
315    tmp = evas_object_image_add(evas_object_evas_get(sd->obj));
316    evas_object_image_file_set(tmp, file, key);
317    data2 = evas_object_image_data_get(tmp, EINA_FALSE);
318
319    w = ih;
320    ih = iw;
321    iw = w;
322    hw = w * ih;
323
324    evas_object_image_size_set(sd->obj, iw, ih);
325    data = evas_object_image_data_get(sd->obj, EINA_TRUE);
326    switch (orient)
327      {
328       case ELM_IMAGE_FLIP_TRANSPOSE:
329          to = data;
330          hw = -hw + 1;
331          break;
332       case ELM_IMAGE_FLIP_TRANSVERSE:
333          to = data + hw - 1;
334          w = -w;
335          hw = hw - 1;
336          break;
337       case ELM_IMAGE_ROTATE_90_CW:
338          to = data + w - 1;
339          hw = -hw - 1;
340          break;
341       case ELM_IMAGE_ROTATE_90_CCW:
342          to = data + hw - w;
343          w = -w;
344          hw = hw + 1;
345          break;
346       default:
347          ERR("unknown orient %d", orient);
348          evas_object_del(tmp);
349          evas_object_image_data_set(sd->obj, data); // give it back
350          return;
351      }
352    from = data2;
353    for (x = iw; --x >= 0;)
354      {
355         for (y = ih; --y >= 0;)
356           {
357              *to = *from;
358              from++;
359              to += w;
360           }
361         to += hw;
362      }
363    sd->orient = orient;
364    evas_object_del(tmp);
365    evas_object_image_data_set(sd->obj, data);
366    evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
367    _smart_reconfigure(sd);
368 }
369
370 Elm_Image_Orient
371 _els_smart_icon_orient_get(const Evas_Object *obj)
372 {
373    Smart_Data *sd; sd = evas_object_smart_data_get(obj);
374    if (!sd) return 0;
375    return sd->orient;
376 }
377
378 /**
379  * Turns on editing through drag and drop and copy and paste.
380  */
381 void
382 _els_smart_icon_edit_set(Evas_Object *obj, Eina_Bool edit, Evas_Object *parent)
383 {
384    Smart_Data   *sd = evas_object_smart_data_get(obj);
385    if (!sd) return;
386
387    if (sd->edje)
388      {
389         printf("No editing edje objects yet (ever)\n");
390         return;
391      }
392
393    /* Unfortunately eina bool is not a bool, but a char */
394    if (edit == sd->edit) return;
395
396    sd->edit = edit;
397
398    if (sd->edit)
399      elm_drop_target_add(obj, ELM_SEL_FORMAT_IMAGE, _els_smart_icon_dropcb,
400                          parent);
401    else
402      elm_drop_target_del(obj);
403 }
404
405 Eina_Bool
406 _els_smart_icon_edit_get(const Evas_Object *obj)
407 {
408    Smart_Data *sd; sd = evas_object_smart_data_get(obj);
409    if (!sd) return EINA_FALSE;
410    return sd->edit;
411 }
412
413 Evas_Object *
414 _els_smart_icon_edje_get(Evas_Object *obj)
415 {
416    Smart_Data *sd = evas_object_smart_data_get(obj);
417    if (!sd) return NULL;
418    if (!sd->edje) return NULL;
419    return sd->obj;
420 }
421
422 /* local subsystem globals */
423 static void
424 _smart_reconfigure(Smart_Data *sd)
425 {
426    Evas_Coord x, y, w, h;
427
428    if (!sd->obj) return;
429
430    const char *type = evas_object_type_get(sd->obj);
431    if ((type) && !strcmp(type, "edje"))
432      {
433         w = sd->w;
434         h = sd->h;
435         x = sd->x;
436         y = sd->y;
437         evas_object_move(sd->obj, x, y);
438         evas_object_resize(sd->obj, w, h);
439      }
440    else
441      {
442         int iw = 0, ih = 0;
443
444         evas_object_image_size_get(sd->obj, &iw, &ih);
445
446         iw = ((double)iw) * sd->scale;
447         ih = ((double)ih) * sd->scale;
448
449         if (iw < 1) iw = 1;
450         if (ih < 1) ih = 1;
451
452         if (sd->fill_inside)
453           {
454              w = sd->w;
455              h = ((double)ih * w) / (double)iw;
456              if (h > sd->h)
457                {
458                   h = sd->h;
459                   w = ((double)iw * h) / (double)ih;
460                }
461           }
462         else
463           {
464              w = sd->w;
465              h = ((double)ih * w) / (double)iw;
466              if (h < sd->h)
467                {
468                   h = sd->h;
469                   w = ((double)iw * h) / (double)ih;
470                }
471           }
472         if (!sd->scale_up)
473           {
474              if ((w > iw) || (h > ih))
475                {
476                   w = iw;
477                   h = ih;
478                }
479           }
480         if (!sd->scale_down)
481           {
482              if ((w < iw) || (h < ih))
483                {
484                   w = iw;
485                   h = ih;
486                }
487           }
488         x = sd->x + ((sd->w - w) / 2);
489         y = sd->y + ((sd->h - h) / 2);
490         evas_object_move(sd->obj, x, y);
491         evas_object_image_fill_set(sd->obj, 0, 0, w, h);
492         evas_object_resize(sd->obj, w, h);
493      }
494 }
495
496 static void
497 _smart_init(void)
498 {
499    if (_e_smart) return;
500      {
501         static const Evas_Smart_Class sc =
502           {
503              "e_icon",
504                EVAS_SMART_CLASS_VERSION,
505                _smart_add,
506                _smart_del,
507                _smart_move,
508                _smart_resize,
509                _smart_show,
510                _smart_hide,
511                _smart_color_set,
512                _smart_clip_set,
513                _smart_clip_unset,
514                NULL,
515                NULL,
516                NULL,
517                NULL,
518                NULL,
519                NULL,
520                NULL
521           };
522         _e_smart = evas_smart_class_new(&sc);
523      }
524 }
525
526 static void
527 _smart_add(Evas_Object *obj)
528 {
529    Smart_Data *sd;
530
531    sd = calloc(1, sizeof(Smart_Data));
532    if (!sd) return;
533    sd->obj = evas_object_image_add(evas_object_evas_get(obj));
534    evas_object_image_scale_hint_set(sd->obj, EVAS_IMAGE_SCALE_HINT_STATIC);
535    sd->x = 0;
536    sd->y = 0;
537    sd->w = 0;
538    sd->h = 0;
539    sd->fill_inside = EINA_TRUE;
540    sd->scale_up = EINA_TRUE;
541    sd->scale_down = EINA_TRUE;
542    sd->size = 64;
543    sd->scale = 1.0;
544    evas_object_smart_member_add(sd->obj, obj);
545    evas_object_smart_data_set(obj, sd);
546    evas_object_event_callback_add(sd->obj, EVAS_CALLBACK_IMAGE_PRELOADED,
547                                   _preloaded, sd);
548 }
549
550 static void
551 _smart_del(Evas_Object *obj)
552 {
553    Smart_Data *sd;
554
555    sd = evas_object_smart_data_get(obj);
556    if (!sd) return;
557    evas_object_del(sd->obj);
558    free(sd);
559 }
560
561 static void
562 _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
563 {
564    Smart_Data *sd;
565
566    sd = evas_object_smart_data_get(obj);
567    if (!sd) return;
568    if ((sd->x == x) && (sd->y == y)) return;
569    sd->x = x;
570    sd->y = y;
571    _smart_reconfigure(sd);
572 }
573
574 static void
575 _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
576 {
577    Smart_Data *sd;
578
579    sd = evas_object_smart_data_get(obj);
580    if (!sd) return;
581    if ((sd->w == w) && (sd->h == h)) return;
582    sd->w = w;
583    sd->h = h;
584    _smart_reconfigure(sd);
585 }
586
587 static void
588 _smart_show(Evas_Object *obj)
589 {
590    Smart_Data *sd;
591
592    sd = evas_object_smart_data_get(obj);
593    if (!sd) return;
594    sd->show = EINA_TRUE;
595    if (!sd->preloading)
596      evas_object_show(sd->obj);
597 }
598
599 static void
600 _smart_hide(Evas_Object *obj)
601 {
602    Smart_Data *sd;
603
604    sd = evas_object_smart_data_get(obj);
605    if (!sd) return;
606    sd->show = EINA_FALSE;
607    evas_object_hide(sd->obj);
608 }
609
610 static void
611 _smart_color_set(Evas_Object *obj, int r, int g, int b, int a)
612 {
613    Smart_Data *sd;
614
615    sd = evas_object_smart_data_get(obj);
616    if (!sd) return;
617    evas_object_color_set(sd->obj, r, g, b, a);
618 }
619
620 static void
621 _smart_clip_set(Evas_Object *obj, Evas_Object * clip)
622 {
623    Smart_Data *sd;
624
625    sd = evas_object_smart_data_get(obj);
626    if (!sd) return;
627    evas_object_clip_set(sd->obj, clip);
628 }
629
630 static void
631 _smart_clip_unset(Evas_Object *obj)
632 {
633    Smart_Data *sd;
634
635    sd = evas_object_smart_data_get(obj);
636    if (!sd) return;
637    evas_object_clip_unset(sd->obj);
638 }
639
640 static void
641 _els_smart_icon_flip_horizontal(Smart_Data *sd)
642 {
643    unsigned int   *data;
644    unsigned int   *p1, *p2, tmp;
645    int             x, y, iw, ih;
646
647    evas_object_image_size_get(sd->obj, &iw, &ih);
648    data = evas_object_image_data_get(sd->obj, EINA_TRUE);
649
650    for (y = 0; y < ih; y++)
651      {
652         p1 = data + (y * iw);
653         p2 = data + ((y + 1) * iw) - 1;
654         for (x = 0; x < (iw >> 1); x++)
655           {
656              tmp = *p1;
657              *p1 = *p2;
658              *p2 = tmp;
659              p1++;
660              p2--;
661           }
662      }
663
664    evas_object_image_data_set(sd->obj, data);
665    evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
666    _smart_reconfigure(sd);
667 }
668
669 static void
670 _els_smart_icon_flip_vertical(Smart_Data *sd)
671 {
672    unsigned int   *data;
673    unsigned int   *p1, *p2, tmp;
674    int             x, y, iw, ih;
675
676    evas_object_image_size_get(sd->obj, &iw, &ih);
677    data = evas_object_image_data_get(sd->obj, EINA_TRUE);
678
679    for (y = 0; y < (ih >> 1); y++)
680      {
681         p1 = data + (y * iw);
682         p2 = data + ((ih - 1 - y) * iw);
683         for (x = 0; x < iw; x++)
684           {
685              tmp = *p1;
686              *p1 = *p2;
687              *p2 = tmp;
688              p1++;
689              p2++;
690           }
691      }
692
693    evas_object_image_data_set(sd->obj, data);
694    evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
695    _smart_reconfigure(sd);
696 }
697
698 static void
699 _els_smart_icon_rotate_180(Smart_Data *sd)
700 {
701    unsigned int   *data;
702    unsigned int   *p1, *p2, tmp;
703    int             x, hw, iw, ih;
704
705    evas_object_image_size_get(sd->obj, &iw, &ih);
706    data = evas_object_image_data_get(sd->obj, 1);
707
708    hw = iw * ih;
709    x = (hw / 2);
710    p1 = data;
711    p2 = data + hw - 1;
712    for (; --x > 0;)
713      {
714         tmp = *p1;
715         *p1 = *p2;
716         *p2 = tmp;
717         p1++;
718         p2--;
719      }
720    evas_object_image_data_set(sd->obj, data);
721    evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
722    _smart_reconfigure(sd);
723 }
724
725 static Eina_Bool
726 _els_smart_icon_dropcb(void *elmobj,Evas_Object *obj, Elm_Selection_Data *drop)
727 {
728    _els_smart_icon_file_key_set(obj, drop->data, NULL);
729    evas_object_smart_callback_call(elmobj, "drop", drop->data);
730
731    return EINA_TRUE;
732 }
733 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/