Merge branch 'master' of juyung.seo@165.213.180.234:/git/slp2.0/slp2.0-pkgs/EFL-pkgs...
[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    unsigned char fill_inside : 1;
13    unsigned char scale_up : 1;
14    unsigned char scale_down : 1;
15 };
16
17 /* local subsystem functions */
18 static void _smart_reconfigure(Smart_Data *sd);
19 static void _smart_init(void);
20 static void _smart_add(Evas_Object *obj);
21 static void _smart_del(Evas_Object *obj);
22 static void _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y);
23 static void _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h);
24 static void _smart_show(Evas_Object *obj);
25 static void _smart_hide(Evas_Object *obj);
26 static void _smart_color_set(Evas_Object *obj, int r, int g, int b, int a);
27 static void _smart_clip_set(Evas_Object *obj, Evas_Object * clip);
28 static void _smart_clip_unset(Evas_Object *obj);
29
30 static void _els_smart_icon_flip_horizontal(Smart_Data *sd);
31 static void _els_smart_icon_flip_vertical(Smart_Data *sd);
32 static void _els_smart_icon_rotate_180(Smart_Data *sd);
33
34 /* local subsystem globals */
35 static Evas_Smart *_e_smart = NULL;
36
37 /* externally accessible functions */
38 Evas_Object *
39 _els_smart_icon_add(Evas *evas)
40 {
41    _smart_init();
42    return evas_object_smart_add(evas, _e_smart);
43 }
44
45 Eina_Bool
46 _els_smart_icon_file_key_set(Evas_Object *obj, const char *file, const char *key)
47 {
48    Smart_Data *sd;
49
50    sd = evas_object_smart_data_get(obj);
51    if (!sd) return EINA_FALSE;
52    /* smart code here */
53    if (sd->size != 0)
54      evas_object_image_load_size_set(sd->obj, sd->size, sd->size);
55    evas_object_image_file_set(sd->obj, file, key);
56    if (evas_object_image_load_error_get(sd->obj) != EVAS_LOAD_ERROR_NONE)
57      return EINA_FALSE;
58    _smart_reconfigure(sd);
59    return EINA_TRUE;
60 }
61
62 Eina_Bool
63 _els_smart_icon_file_edje_set(Evas_Object *obj, const char *file, const char *part)
64 {
65    Smart_Data *sd;
66
67    sd = evas_object_smart_data_get(obj);
68    if (!sd) return EINA_FALSE;
69    /* smart code here */
70    if (sd->obj) evas_object_del(sd->obj);
71    sd->obj = edje_object_add(evas_object_evas_get(obj));
72    evas_object_smart_member_add(sd->obj, obj);
73    if (!edje_object_file_set(sd->obj, file, part))
74      return EINA_FALSE;
75    _smart_reconfigure(sd);
76    return EINA_TRUE;
77 }
78
79 void
80 _els_smart_icon_smooth_scale_set(Evas_Object *obj, int smooth)
81 {
82    Smart_Data *sd;
83
84    sd = evas_object_smart_data_get(obj);
85    if (!sd) return;
86    if (!strcmp(evas_object_type_get(sd->obj), "edje"))
87      return;
88    evas_object_image_smooth_scale_set(sd->obj, smooth);
89 }
90
91 void
92 _els_smart_icon_size_get(Evas_Object *obj, int *w, int *h)
93 {
94    Smart_Data *sd;
95    int tw, th;
96
97    sd = evas_object_smart_data_get(obj);
98    if (!sd) return;
99    if (!strcmp(evas_object_type_get(sd->obj), "edje"))
100      edje_object_size_min_get(sd->obj, &tw, &th);
101    else
102      evas_object_image_size_get(sd->obj, &tw, &th);
103    tw = ((double)tw) * sd->scale;
104    th = ((double)th) * sd->scale;
105    if (w) *w = tw;
106    if (h) *h = th;
107 }
108
109 void
110 _els_smart_icon_fill_inside_set(Evas_Object *obj, int fill_inside)
111 {
112    Smart_Data *sd;
113
114    sd = evas_object_smart_data_get(obj);
115    if (!sd) return;
116    if (((sd->fill_inside) && (fill_inside)) ||
117        ((!sd->fill_inside) && (!fill_inside))) return;
118    sd->fill_inside = fill_inside;
119    _smart_reconfigure(sd);
120 }
121
122 void
123 _els_smart_icon_scale_up_set(Evas_Object *obj, int scale_up)
124 {
125    Smart_Data *sd;
126
127    sd = evas_object_smart_data_get(obj);
128    if (!sd) return;
129    if (((sd->scale_up) && (scale_up)) ||
130        ((!sd->scale_up) && (!scale_up))) return;
131    sd->scale_up = scale_up;
132    _smart_reconfigure(sd);
133 }
134
135 void
136 _els_smart_icon_scale_down_set(Evas_Object *obj, int scale_down)
137 {
138    Smart_Data *sd;
139
140    sd = evas_object_smart_data_get(obj);
141    if (!sd) return;
142    if (((sd->scale_down) && (scale_down)) ||
143        ((!sd->scale_down) && (!scale_down))) return;
144    sd->scale_down = scale_down;
145    _smart_reconfigure(sd);
146 }
147
148 void
149 _els_smart_icon_scale_size_set(Evas_Object *obj, int size)
150 {
151    Smart_Data *sd;
152
153    sd = evas_object_smart_data_get(obj);
154    if (!sd) return;
155    sd->size = size;
156    if (!sd->obj) return;
157    if (!strcmp(evas_object_type_get(sd->obj), "edje"))
158      return;
159    evas_object_image_load_size_set(sd->obj, sd->size, sd->size);
160 }
161
162 void
163 _els_smart_icon_scale_set(Evas_Object *obj, double scale)
164 {
165    Smart_Data *sd;
166
167    sd = evas_object_smart_data_get(obj);
168    if (!sd) return;
169    sd->scale = scale;
170    _smart_reconfigure(sd);
171 }
172
173 void
174 _els_smart_icon_orient_set(Evas_Object *obj, Elm_Image_Orient orient)
175 {
176    Smart_Data *sd;
177    Evas_Object    *tmp;
178    unsigned int   *data, *data2, *to, *from;
179    int             x, y, w, hw, iw, ih;
180    const char     *file, *key;
181
182    sd = evas_object_smart_data_get(obj);
183    if (!sd) return;
184    if (!strcmp(evas_object_type_get(sd->obj), "edje"))
185      return;
186
187    switch (orient)
188      {
189       case ELM_IMAGE_FLIP_HORIZONTAL:
190          _els_smart_icon_flip_horizontal(sd);
191          return;
192       case ELM_IMAGE_FLIP_VERTICAL:
193          _els_smart_icon_flip_vertical(sd);
194          return;
195       case ELM_IMAGE_ROTATE_180_CW:
196          _els_smart_icon_rotate_180(sd);
197          return;
198      default:
199         return;
200      }
201
202    evas_object_image_size_get(sd->obj, &iw, &ih);
203    evas_object_image_file_get(sd->obj, &file, &key);
204    tmp = evas_object_image_add(evas_object_evas_get(sd->obj));
205    evas_object_image_file_set(tmp, file, key);
206    data2 = evas_object_image_data_get(tmp, 0);
207
208    w = ih;
209    ih = iw;
210    iw = w;
211    hw = w * ih;
212
213    evas_object_image_size_set(sd->obj, iw, ih);
214    data = evas_object_image_data_get(sd->obj, 1);
215    switch (orient)
216      {
217       case ELM_IMAGE_FLIP_TRANSPOSE:
218          to = data;
219          hw = -hw + 1;
220          break;
221       case ELM_IMAGE_FLIP_TRANSVERSE:
222          to = data + hw - 1;
223          w = -w;
224          hw = hw - 1;
225          break;
226       case ELM_IMAGE_ROTATE_90_CW:
227          to = data + w - 1;
228          hw = -hw - 1;
229          break;
230       case ELM_IMAGE_ROTATE_90_CCW:
231          to = data + hw - w;
232          w = -w;
233          hw = hw + 1;
234          break;
235       default:
236          ERR("unknown orient %d", orient);
237          evas_object_del(tmp);
238          evas_object_image_data_set(sd->obj, data); // give it back
239          return;
240      }
241    from = data2;
242    for (x = iw; --x >= 0;)
243      {
244         for (y = ih; --y >= 0;)
245           {
246              *to = *from;
247              from++;
248              to += w;
249           }
250         to += hw;
251      }
252    evas_object_del(tmp);
253    evas_object_image_data_set(sd->obj, data);
254    evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
255    _smart_reconfigure(sd);
256 }
257
258 /* local subsystem globals */
259 static void
260 _smart_reconfigure(Smart_Data *sd)
261 {
262    int iw, ih;
263    Evas_Coord x, y, w, h;
264
265    if (!sd->obj) return;
266    if (!strcmp(evas_object_type_get(sd->obj), "edje"))
267      {
268         w = sd->w;
269         h = sd->h;
270         x = sd->x;
271         y = sd->y;
272         evas_object_move(sd->obj, x, y);
273         evas_object_resize(sd->obj, w, h);
274      }
275    else
276      {
277         iw = 0;
278         ih = 0;
279         evas_object_image_size_get(sd->obj, &iw, &ih);
280
281         iw = ((double)iw) * sd->scale;
282         ih = ((double)ih) * sd->scale;
283
284         if (iw < 1) iw = 1;
285         if (ih < 1) ih = 1;
286
287         if (sd->fill_inside)
288           {
289              w = sd->w;
290              h = ((double)ih * w) / (double)iw;
291              if (h > sd->h)
292                {
293                   h = sd->h;
294                   w = ((double)iw * h) / (double)ih;
295                }
296           }
297         else
298           {
299              w = sd->w;
300              h = ((double)ih * w) / (double)iw;
301              if (h < sd->h)
302                {
303                   h = sd->h;
304                   w = ((double)iw * h) / (double)ih;
305                }
306           }
307         if (!sd->scale_up)
308           {
309              if ((w > iw) || (h > ih))
310                {
311                   w = iw;
312                   h = ih;
313                }
314           }
315         if (!sd->scale_down)
316           {
317              if ((w < iw) || (h < ih))
318                {
319                   w = iw;
320                   h = ih;
321                }
322           }
323         x = sd->x + ((sd->w - w) / 2);
324         y = sd->y + ((sd->h - h) / 2);
325         evas_object_move(sd->obj, x, y);
326         evas_object_image_fill_set(sd->obj, 0, 0, w, h);
327         evas_object_resize(sd->obj, w, h);
328      }
329 }
330
331 static void
332 _smart_init(void)
333 {
334    if (_e_smart) return;
335      {
336         static const Evas_Smart_Class sc =
337           {
338              "e_icon",
339                EVAS_SMART_CLASS_VERSION,
340                _smart_add,
341                _smart_del,
342                _smart_move,
343                _smart_resize,
344                _smart_show,
345                _smart_hide,
346                _smart_color_set,
347                _smart_clip_set,
348                _smart_clip_unset,
349                NULL,
350                NULL,
351                NULL,
352                NULL,
353                NULL,
354                NULL,
355                NULL
356           };
357         _e_smart = evas_smart_class_new(&sc);
358      }
359 }
360
361 static void
362 _smart_add(Evas_Object *obj)
363 {
364    Smart_Data *sd;
365
366    sd = calloc(1, sizeof(Smart_Data));
367    if (!sd) return;
368    sd->obj = evas_object_image_add(evas_object_evas_get(obj));
369    evas_object_image_scale_hint_set(sd->obj, EVAS_IMAGE_SCALE_HINT_STATIC);
370    sd->x = 0;
371    sd->y = 0;
372    sd->w = 0;
373    sd->h = 0;
374    sd->fill_inside = 1;
375    sd->scale_up = 1;
376    sd->scale_down = 1;
377    sd->size = 64;
378    sd->scale = 1.0;
379    evas_object_smart_member_add(sd->obj, obj);
380    evas_object_smart_data_set(obj, sd);
381 }
382
383 static void
384 _smart_del(Evas_Object *obj)
385 {
386    Smart_Data *sd;
387
388    sd = evas_object_smart_data_get(obj);
389    if (!sd) return;
390    evas_object_del(sd->obj);
391    free(sd);
392 }
393
394 static void
395 _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
396 {
397    Smart_Data *sd;
398
399    sd = evas_object_smart_data_get(obj);
400    if (!sd) return;
401    if ((sd->x == x) && (sd->y == y)) return;
402    sd->x = x;
403    sd->y = y;
404    _smart_reconfigure(sd);
405 }
406
407 static void
408 _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
409 {
410    Smart_Data *sd;
411
412    sd = evas_object_smart_data_get(obj);
413    if (!sd) return;
414    if ((sd->w == w) && (sd->h == h)) return;
415    sd->w = w;
416    sd->h = h;
417    _smart_reconfigure(sd);
418 }
419
420 static void
421 _smart_show(Evas_Object *obj)
422 {
423    Smart_Data *sd;
424
425    sd = evas_object_smart_data_get(obj);
426    if (!sd) return;
427    evas_object_show(sd->obj);
428 }
429
430 static void
431 _smart_hide(Evas_Object *obj)
432 {
433    Smart_Data *sd;
434
435    sd = evas_object_smart_data_get(obj);
436    if (!sd) return;
437    evas_object_hide(sd->obj);
438 }
439
440 static void
441 _smart_color_set(Evas_Object *obj, int r, int g, int b, int a)
442 {
443    Smart_Data *sd;
444
445    sd = evas_object_smart_data_get(obj);
446    if (!sd) return;
447    evas_object_color_set(sd->obj, r, g, b, a);
448 }
449
450 static void
451 _smart_clip_set(Evas_Object *obj, Evas_Object * clip)
452 {
453    Smart_Data *sd;
454
455    sd = evas_object_smart_data_get(obj);
456    if (!sd) return;
457    evas_object_clip_set(sd->obj, clip);
458 }
459
460 static void
461 _smart_clip_unset(Evas_Object *obj)
462 {
463    Smart_Data *sd;
464
465    sd = evas_object_smart_data_get(obj);
466    if (!sd) return;
467    evas_object_clip_unset(sd->obj);
468 }
469
470 static void
471 _els_smart_icon_flip_horizontal(Smart_Data *sd)
472 {
473    unsigned int   *data;
474    unsigned int   *p1, *p2, tmp;
475    int             x, y, iw, ih;
476
477    evas_object_image_size_get(sd->obj, &iw, &ih);
478    data = evas_object_image_data_get(sd->obj, 1);
479
480    for (y = 0; y < ih; y++)
481      {
482         p1 = data + (y * iw);
483         p2 = data + ((y + 1) * iw) - 1;
484         for (x = 0; x < (iw >> 1); x++)
485           {
486              tmp = *p1;
487              *p1 = *p2;
488              *p2 = tmp;
489              p1++;
490              p2--;
491           }
492      }
493
494    evas_object_image_data_set(sd->obj, data);
495    evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
496    _smart_reconfigure(sd);
497 }
498
499 static void
500 _els_smart_icon_flip_vertical(Smart_Data *sd)
501 {
502    unsigned int   *data;
503    unsigned int   *p1, *p2, tmp;
504    int             x, y, iw, ih;
505
506    evas_object_image_size_get(sd->obj, &iw, &ih);
507    data = evas_object_image_data_get(sd->obj, 1);
508
509    for (y = 0; y < (ih >> 1); y++)
510      {
511         p1 = data + (y * iw);
512         p2 = data + ((ih - 1 - y) * iw);
513         for (x = 0; x < iw; x++)
514           {
515              tmp = *p1;
516              *p1 = *p2;
517              *p2 = tmp;
518              p1++;
519              p2++;
520           }
521      }
522
523    evas_object_image_data_set(sd->obj, data);
524    evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
525    _smart_reconfigure(sd);
526 }
527
528 static void
529 _els_smart_icon_rotate_180(Smart_Data *sd)
530 {
531    unsigned int   *data;
532    unsigned int   *p1, *p2, tmp;
533    int             x, hw, iw, ih;
534
535    evas_object_image_size_get(sd->obj, &iw, &ih);
536    data = evas_object_image_data_get(sd->obj, 1);
537
538    hw = iw * ih;
539    x = (hw / 2);
540    p1 = data;
541    p2 = data + hw - 1;
542    for (; --x > 0;)
543      {
544         tmp = *p1;
545         *p1 = *p2;
546         *p2 = tmp;
547         p1++;
548         p2--;
549      }
550    evas_object_image_data_set(sd->obj, data);
551    evas_object_image_data_update_add(sd->obj, 0, 0, iw, ih);
552    _smart_reconfigure(sd);
553 }
554