Merge "[elm_colorselector.c]modified theme hook according to review comments to clear...
[framework/uifw/elementary.git] / src / lib / elm_colorselector.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 #define BASE_STEP 360.0
5 #define HUE_STEP 360.0
6 #define SAT_STEP 128.0
7 #define LIG_STEP 256.0
8 #define ALP_STEP 256.0
9
10 typedef enum _Button_State
11 {
12    BUTTON_RELEASED,
13    L_BUTTON_PRESSED,
14    R_BUTTON_PRESSED
15 } Button_State;
16
17 typedef enum _Color_Type
18 {
19    HUE,
20    SATURATION,
21    LIGHTNESS,
22    ALPHA
23 } Color_Type;
24
25 typedef struct _Colorselector_Data Colorselector_Data;
26 struct _Colorselector_Data
27 {
28    Evas_Object *parent;
29    Evas_Object *colorbar;
30    Evas_Object *bar;
31    Evas_Object *lbt;
32    Evas_Object *rbt;
33    Evas_Object *bg_rect;
34    Evas_Object *arrow;
35    Evas_Object *touch_area;
36    Color_Type color_type;
37    Button_State button_state;
38 };
39
40 typedef struct _Widget_Data Widget_Data;
41 struct _Widget_Data
42 {
43    Evas_Object *base;
44    Colorselector_Data *cp[4];
45    Evas_Coord _x, _y, _w, _h;
46    int r, g, b, a;
47    int er, eg, eb;
48    int sr, sg, sb;
49    int lr, lg, lb;
50    double h, s, l;
51    Ecore_Timer *lp_timer;
52    Ecore_Timer *mv_timer;
53 };
54
55 static const char *widtype = NULL;
56
57 static void _del_hook(Evas_Object *obj);
58 static void _theme_hook(Evas_Object *obj);
59 static void _sizing_eval(Evas_Object *obj);
60 static void _rgb_to_hsl(void *data);
61 static void _hsl_to_rgb(void *data);
62 static void _color_with_saturation(void *data);
63 static void _color_with_lightness(void *data);
64 static void _draw_rects(void *data, double x);
65 static void _arrow_cb(void *data, Evas_Object *obj, const char *emission, const char *source);
66 static void _colorbar_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
67 static Eina_Bool _mv_timer(void *data);
68 static Eina_Bool _long_press_timer(void *data);
69 static void _left_button_down_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
70 static void _right_button_down_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
71 static void _left_button_up_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
72 static void _right_button_up_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
73 static void _add_colorbar(Evas_Object *obj);
74 static void _set_color(Evas_Object *obj, int r, int g, int b, int a);
75
76 static const char SIG_CHANGED[] = "changed";
77
78 static const Evas_Smart_Cb_Description _signals[] =
79 {
80      {SIG_CHANGED, ""},
81      {NULL, NULL}
82 };
83
84 static void
85 _del_hook(Evas_Object *obj)
86 {
87    Widget_Data *wd = elm_widget_data_get(obj);
88    int i = 0;
89
90    if (!wd) return;
91    if (wd->lp_timer) ecore_timer_del(wd->lp_timer);
92    if (wd->mv_timer) ecore_timer_del(wd->mv_timer);
93    for (i = 0; i < 4; i++) free(wd->cp[i]);
94    free(wd);
95 }
96
97 static void
98 _theme_hook(Evas_Object *obj)
99 {
100    Widget_Data *wd = elm_widget_data_get(obj);
101    int i;
102
103    if ((!wd) || (!wd->base)) return;
104
105    _elm_theme_object_set(obj, wd->base, "colorselector", "bg",
106                          elm_widget_style_get(obj));
107
108    for (i = 0; i < 4; i++)
109      {
110         evas_object_del(wd->cp[i]->colorbar);
111         wd->cp[i]->colorbar = NULL;
112         evas_object_del(wd->cp[i]->bar);
113         wd->cp[i]->bar = NULL;
114         evas_object_del(wd->cp[i]->lbt);
115         wd->cp[i]->lbt = NULL;
116         evas_object_del(wd->cp[i]->rbt);
117         wd->cp[i]->rbt = NULL;
118         if (i != 0)
119           {
120              evas_object_del(wd->cp[i]->bg_rect);
121              wd->cp[i]->bg_rect = NULL;
122           }
123         evas_object_del(wd->cp[i]->arrow);
124         wd->cp[i]->arrow = NULL;
125         evas_object_del(wd->cp[i]->touch_area);
126         wd->cp[i]->touch_area = NULL;
127      }
128
129    _add_colorbar(obj);
130    elm_colorselector_color_set(obj, wd->r, wd->g, wd->b, wd->a);
131    _sizing_eval(obj);
132 }
133
134 static void
135 _colorselector_set_size_hints(Evas_Object *obj, int timesw, int timesh)
136 {
137    Evas_Coord minw = -1, minh = -1;
138
139    elm_coords_finger_size_adjust(timesw, &minw, timesh, &minh);
140    edje_object_size_min_restricted_calc(obj, &minw, &minh,
141                                         minw, minh);
142    evas_object_size_hint_min_set(obj, minw, minh);
143    evas_object_size_hint_max_set(obj, -1, -1);
144 }
145
146 static void
147 _sizing_eval(Evas_Object *obj)
148 {
149    Widget_Data *wd = elm_widget_data_get(obj);
150    Evas_Coord minw = -1, minh = -1;
151    int i;
152
153    if (!wd) return;
154    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
155    for (i = 0; i < 4; i++)
156      {
157         if (wd->cp[i]->bg_rect)
158           _colorselector_set_size_hints(wd->cp[i]->bg_rect, 1, 1);
159         _colorselector_set_size_hints(wd->cp[i]->bar, 1, 1);
160         _colorselector_set_size_hints(wd->cp[i]->rbt, 1, 1);
161         _colorselector_set_size_hints(wd->cp[i]->lbt, 1, 1);
162
163         _colorselector_set_size_hints(wd->cp[i]->colorbar, 4, 1);
164      }
165
166    elm_coords_finger_size_adjust(4, &minw, 4, &minh);
167    edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
168    evas_object_size_hint_min_set(obj, minw, minh);
169    evas_object_size_hint_max_set(obj, -1, -1);
170 }
171
172 static void
173 _rgb_to_hsl(void *data)
174 {
175    Widget_Data *wd = data;
176    double r, g, b;
177    double v, m, vm;
178    double r2, g2, b2;
179
180    r = wd->r;
181    g = wd->g;
182    b = wd->b;
183
184    r /= 255.0;
185    g /= 255.0;
186    b /= 255.0;
187
188    v = (r > g) ? r : g;
189    v = (v > b) ? v : b;
190
191    m = (r < g) ? r : g;
192    m = (m < b) ? m : b;
193
194    wd->h = 0.0;
195    wd->s = 0.0;
196    wd->l = 0.0;
197
198    wd->l = (m + v) / 2.0;
199
200    if (wd->l <= 0.0) return;
201
202    vm = v - m;
203    wd->s = vm;
204
205    if (wd->s > 0.0) wd->s /= (wd->l <= 0.5) ? (v + m) : (2.0 - v - m);
206    else return;
207
208    r2 = (v - r) / vm;
209    g2 = (v - g) / vm;
210    b2 = (v - b) / vm;
211
212    if (r == v) wd->h = (g == m ? 5.0 + b2 : 1.0 - g2);
213    else if (g == v) wd->h = (b == m ? 1.0 + r2 : 3.0 - b2);
214    else wd->h = (r == m ? 3.0 + g2 : 5.0 - r2);
215
216    wd->h *= 60.0;
217 }
218
219 static void
220 _hsl_to_rgb(void *data)
221 {
222    Widget_Data *wd = data;
223    double r = 0, g = 0, b = 0;
224    double _h, _s, _l;
225    int i = 0;
226    double sv, vsf, f, p, q, t, v;
227
228    _h = wd->h;
229    _s = wd->s;
230    _l = wd->l;
231
232    if (_s == 0.0) r = g = b = _l;
233    else
234      {
235         if (_h == 360.0) _h = 0.0;
236         _h /= 60.0;
237
238         v = (_l <= 0.5) ? (_l * (1.0 + _s)) : (_l + _s - (_l * _s));
239         p = _l + _l - v;
240
241         if (v) sv = (v - p) / v;
242         else sv = 0;
243
244         i = (int)_h;
245         f = _h - i;
246
247         vsf = v * sv * f;
248
249         t = p + vsf;
250         q = v - vsf;
251
252         switch (i)
253           {
254            case 0:
255               r = v;
256               g = t;
257               b = p;
258               break;
259            case 1:
260               r = q;
261               g = v;
262               b = p;
263               break;
264            case 2:
265               r = p;
266               g = v;
267               b = t;
268               break;
269            case 3:
270               r = p;
271               g = q;
272               b = v;
273               break;
274            case 4:
275               r = t;
276               g = p;
277               b = v;
278               break;
279            case 5:
280               r = v;
281               g = p;
282               b = q;
283               break;
284           }
285      }
286    i = (int)(r * 255.0);
287    f = (r * 255.0) - i;
288    wd->r = (f <= 0.5) ? i : (i + 1);
289
290    i = (int)(g * 255.0);
291    f = (g * 255.0) - i;
292    wd->g = (f <= 0.5) ? i : (i + 1);
293
294    i = (int)(b * 255.0);
295    f = (b * 255.0) - i;
296    wd->b = (f <= 0.5) ? i : (i + 1);
297 }
298
299 static void
300 _color_with_saturation(void *data)
301 {
302    Widget_Data *wd = data;
303
304    if (wd->er > 127)
305      wd->sr = 127 + (int)((double)(wd->er - 127) * wd->s);
306    else
307      wd->sr = 127 - (int)((double)(127 - wd->er) * wd->s);
308
309    if (wd->eg > 127)
310      wd->sg = 127 + (int)((double)(wd->eg - 127) * wd->s);
311    else
312      wd->sg = 127 - (int)((double)(127 - wd->eg) * wd->s);
313
314    if (wd->eb > 127)
315      wd->sb = 127 + (int)((double)(wd->eb - 127) * wd->s);
316    else
317      wd->sb = 127 - (int)((double)(127 - wd->eb) * wd->s);
318 }
319
320 static void
321 _color_with_lightness(void *data)
322 {
323    Widget_Data *wd = data;
324
325    if (wd->l > 0.5)
326      {
327         wd->lr = wd->er + (int)((double)(255 - wd->er) * (wd->l - 0.5) * 2.0);
328         wd->lg = wd->eg + (int)((double)(255 - wd->eg) * (wd->l - 0.5) * 2.0);
329         wd->lb = wd->eb + (int)((double)(255 - wd->eb) * (wd->l - 0.5) * 2.0);
330      }
331    else if (wd->l < 0.5)
332      {
333         wd->lr = (double)wd->er * wd->l * 2.0;
334         wd->lg = (double)wd->eg * wd->l * 2.0;
335         wd->lb = (double)wd->eb * wd->l * 2.0;
336      }
337    else
338      {
339         wd->lr = wd->er;
340         wd->lg = wd->eg;
341         wd->lb = wd->eb;
342      }
343 }
344
345 static void
346 _draw_rects(void *data, double x)
347 {
348    Colorselector_Data *cp = data;
349    Widget_Data *wd = elm_widget_data_get(cp->parent);
350    double one_six = 1.0 / 6.0;
351
352    switch (cp->color_type)
353      {
354       case HUE:
355          wd->h = 360.0 * x;
356
357          if (x < one_six)
358            {
359               wd->er = 255;
360               wd->eg = (255.0 * x * 6.0);
361               wd->eb = 0;
362            }
363          else if (x < 2 * one_six)
364            {
365               wd->er = 255 - (int)(255.0 * (x - one_six) * 6.0);
366               wd->eg = 255;
367               wd->eb = 0;
368            }
369          else if (x < 3 * one_six)
370            {
371               wd->er = 0;
372               wd->eg = 255;
373               wd->eb = (int)(255.0 * (x - (2.0 * one_six)) * 6.0);
374            }
375          else if (x < 4 * one_six)
376            {
377               wd->er = 0;
378               wd->eg = 255 - (int)(255.0 * (x - (3.0 * one_six)) * 6.0);
379               wd->eb = 255;
380            }
381          else if (x < 5 * one_six)
382            {
383               wd->er = 255.0 * (x - (4.0 * one_six)) * 6.0;
384               wd->eg = 0;
385               wd->eb = 255;
386            }
387          else
388            {
389               wd->er = 255;
390               wd->eg = 0;
391               wd->eb = 255 - (int)(255.0 * (x - (5.0 * one_six)) * 6.0);
392            }
393
394          evas_object_color_set(wd->cp[0]->arrow, wd->er, wd->eg, wd->eb, 255);
395          evas_object_color_set(wd->cp[1]->bg_rect, wd->er, wd->eg, wd->eb, 255);
396          evas_object_color_set(wd->cp[2]->bg_rect, wd->er, wd->eg, wd->eb, 255);
397          evas_object_color_set(wd->cp[3]->bar, wd->er, wd->eg, wd->eb, 255);
398
399          _color_with_saturation(wd);
400          evas_object_color_set(wd->cp[1]->arrow, wd->sr, wd->sg, wd->sb, 255);
401
402          _color_with_lightness(wd);
403          evas_object_color_set(wd->cp[2]->arrow, wd->lr, wd->lg, wd->lb, 255);
404
405          evas_object_color_set(wd->cp[3]->arrow,
406                                (wd->er * wd->a) / 255,
407                                (wd->eg * wd->a) / 255,
408                                (wd->eb * wd->a) / 255,
409                                wd->a);
410          break;
411       case SATURATION:
412          wd->s = 1.0 - x;
413          _color_with_saturation(wd);
414          evas_object_color_set(wd->cp[1]->arrow, wd->sr, wd->sg, wd->sb, 255);
415          break;
416       case LIGHTNESS:
417          wd->l = x;
418          _color_with_lightness(wd);
419          evas_object_color_set(wd->cp[2]->arrow, wd->lr, wd->lg, wd->lb, 255);
420          break;
421       case ALPHA:
422          wd->a = 255.0 * x;
423          evas_object_color_set(wd->cp[3]->arrow, wd->er, wd->eg, wd->eb, wd->a);
424          break;
425       default:
426          break;
427      }
428    _hsl_to_rgb(wd);
429 }
430
431 static void
432 _arrow_cb(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
433 {
434    Colorselector_Data *cp = data;
435    double x, y;
436
437    edje_object_part_drag_value_get(obj, "elm.arrow", &x, &y);
438    _draw_rects(data, x);
439    evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
440 }
441
442 static void
443 _colorbar_cb(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *event_info)
444 {
445    Colorselector_Data *cp = data;
446    Evas_Event_Mouse_Down *ev = event_info;
447    Evas_Coord x, y, w, h;
448    double arrow_x = 0, arrow_y;
449
450    evas_object_geometry_get(cp->bar, &x, &y, &w, &h);
451    edje_object_part_drag_value_get(cp->colorbar, "elm.arrow",
452                                    &arrow_x, &arrow_y);
453    if (w > 0) arrow_x = (double)(ev->canvas.x - x) / (double)w;
454    if (arrow_x > 1) arrow_x = 1;
455    if (arrow_x < 0) arrow_x = 0;
456    edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", arrow_x, arrow_y);
457    _draw_rects(data, arrow_x);
458    evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
459    evas_event_feed_mouse_cancel(e, 0, NULL);
460    evas_event_feed_mouse_down(e, 1, EVAS_BUTTON_NONE, 0, NULL);
461 }
462
463 static Eina_Bool
464 _mv_timer(void *data)
465 {
466    Colorselector_Data *cp = data;
467    Widget_Data *wd = elm_widget_data_get(cp->parent);
468    double x, y;
469
470    if (!wd) return EINA_FALSE;
471
472    edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
473    if (cp->button_state == L_BUTTON_PRESSED)
474      {
475         x -= 1.0 / BASE_STEP;
476         if (x < 0.0) x = 0.0;
477         edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
478         _draw_rects(data, x);
479         evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
480         return EINA_TRUE;
481      }
482    else if (cp->button_state == R_BUTTON_PRESSED)
483      {
484         x += 1.0 / BASE_STEP;
485         if (x > 1.0) x = 1.0;
486         edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
487         _draw_rects(data, x);
488         evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
489         return EINA_TRUE;
490      }
491    wd->mv_timer = NULL;
492    return EINA_FALSE;
493 }
494
495 static Eina_Bool
496 _long_press_timer(void *data)
497 {
498    Colorselector_Data *cp = data;
499    Widget_Data *wd = elm_widget_data_get(cp->parent);
500
501    if (wd->mv_timer) ecore_timer_del(wd->mv_timer);
502    wd->mv_timer = ecore_timer_add(0.01, _mv_timer, cp);
503
504    wd->lp_timer = NULL;
505    return EINA_FALSE;
506 }
507
508 static void
509 _left_button_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
510 {
511    Colorselector_Data *cp = data;
512    Widget_Data *wd = elm_widget_data_get(cp->parent);
513    double x, y;
514
515    edje_object_signal_emit(cp->lbt, "elm,state,left,button,down",
516                            "left_button");
517    edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
518
519    switch(cp->color_type)
520      {
521       case HUE :
522          x -= 1.0 / HUE_STEP;
523          break;
524       case SATURATION :
525          x -= 1.0 / SAT_STEP;
526          break;
527       case LIGHTNESS :
528          x -= 1.0 / LIG_STEP;
529          break;
530       case ALPHA :
531          x -= 1.0 / ALP_STEP;
532          break;
533       default :
534          break;
535      }
536
537    if (x < 0.0) x = 0.0;
538
539    edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
540    _draw_rects(data, x);
541    evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
542    cp->button_state = L_BUTTON_PRESSED;
543    if (wd->lp_timer) ecore_timer_del(wd->lp_timer);
544    wd->lp_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press_timer, cp);
545 }
546
547 static void
548 _right_button_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
549 {
550    Colorselector_Data *cp = data;
551    Widget_Data *wd = elm_widget_data_get(cp->parent);
552    double x, y;
553
554    edje_object_signal_emit(cp->rbt, "elm,state,right,button,down",
555                            "right_button");
556    edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
557
558    switch(cp->color_type)
559      {
560       case HUE :
561          x += 1.0 / HUE_STEP;
562          break;
563       case SATURATION :
564          x += 1.0 / SAT_STEP;
565          break;
566       case LIGHTNESS :
567          x += 1.0 / LIG_STEP;
568          break;
569       case ALPHA :
570          x += 1.0 / ALP_STEP;
571          break;
572       default :
573          break;
574      }
575
576    if (x > 1.0) x = 1.0;
577
578    edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
579    _draw_rects(data, x);
580    evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
581    cp->button_state = R_BUTTON_PRESSED;
582    wd->lp_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press_timer, cp);
583 }
584
585 static void
586 _left_button_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
587 {
588    Colorselector_Data *cp = data;
589    Widget_Data *wd = elm_widget_data_get(cp->parent);
590
591    if (wd->lp_timer)
592      {
593         ecore_timer_del(wd->lp_timer);
594         wd->lp_timer = NULL;
595      }
596    if (wd->mv_timer)
597      {
598         ecore_timer_del(wd->mv_timer);
599         wd->mv_timer = NULL;
600      }
601
602    cp->button_state = BUTTON_RELEASED;
603    edje_object_signal_emit(cp->lbt, "elm,state,left,button,up", "left_button");
604 }
605
606 static void
607 _right_button_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
608 {
609    Colorselector_Data *cp = data;
610    Widget_Data *wd = elm_widget_data_get(cp->parent);
611
612    if (wd->lp_timer)
613      {
614         ecore_timer_del(wd->lp_timer);
615         wd->lp_timer = NULL;
616      }
617    if (wd->mv_timer)
618      {
619         ecore_timer_del(wd->mv_timer);
620         wd->mv_timer = NULL;
621      }
622
623    cp->button_state = BUTTON_RELEASED;
624    edje_object_signal_emit(cp->rbt, "elm,state,right,button,up",
625                            "right_button");
626 }
627
628 static void
629 _add_colorbar(Evas_Object *obj)
630 {
631    char colorbar_name[128];
632    char colorbar_s[128];
633    Widget_Data *wd;
634    Evas *e;
635    int i = 0;
636
637    wd = elm_widget_data_get(obj);
638    if (!wd) return;
639
640    e = evas_object_evas_get(obj);
641
642    for (i = 0; i < 4; i++)
643      {
644         wd->cp[i] = ELM_NEW(Colorselector_Data);
645         wd->cp[i]->parent = obj;
646         switch(i)
647           {
648            case 0 :
649               wd->cp[i]->color_type = HUE;
650               break;
651            case 1 :
652               wd->cp[i]->color_type = SATURATION;
653               break;
654            case 2 :
655               wd->cp[i]->color_type = LIGHTNESS;
656               break;
657            case 3 :
658               wd->cp[i]->color_type = ALPHA;
659               break;
660            default :
661               break;
662           }
663         /* load colorbar area */
664         wd->cp[i]->colorbar = edje_object_add(e);
665         _elm_theme_object_set(obj, wd->cp[i]->colorbar, "colorselector", "base",
666                               "default");
667         snprintf(colorbar_name, sizeof(colorbar_name), "colorbar_%d", i);
668         snprintf(colorbar_s, sizeof(colorbar_s), "elm.colorbar_%d", i);
669         edje_object_signal_callback_add(wd->cp[i]->colorbar, "drag", "*",
670                                         _arrow_cb, wd->cp[i]);
671         edje_object_part_swallow(wd->base, colorbar_s, wd->cp[i]->colorbar);
672         elm_widget_sub_object_add(obj, wd->cp[i]->colorbar);
673
674         /* load colorbar image */
675         wd->cp[i]->bar = edje_object_add(e);
676         _elm_theme_object_set(obj, wd->cp[i]->bar, "colorselector", "image",
677                               colorbar_name);
678         edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar",
679                                  wd->cp[i]->bar);
680         elm_widget_sub_object_add(obj, wd->cp[i]->bar);
681
682         /* provide expanded touch area */
683         wd->cp[i]->touch_area = evas_object_rectangle_add(e);
684         evas_object_color_set(wd->cp[i]->touch_area, 0, 0, 0, 0);
685         edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_bg",
686                                  wd->cp[i]->touch_area);
687         evas_object_event_callback_add(wd->cp[i]->touch_area,
688                                        EVAS_CALLBACK_MOUSE_DOWN, _colorbar_cb,
689                                        wd->cp[i]);
690         elm_widget_sub_object_add(obj, wd->cp[i]->touch_area);
691
692         /* load background rectangle of the colorbar. used for
693            changing color of the opacity bar */
694         if ((i == 1) || (i == 2))
695           {
696              wd->cp[i]->bg_rect = evas_object_rectangle_add(e);
697              evas_object_color_set(wd->cp[i]->bg_rect, wd->er, wd->eg, wd->eb,
698                                    255);
699              edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
700                                       wd->cp[i]->bg_rect);
701
702              elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
703           }
704         if (i == 3)
705           {
706              wd->cp[i]->bg_rect = edje_object_add(e);
707              _elm_theme_object_set(obj, wd->cp[i]->bg_rect, "colorselector",
708                                    "bg_image", colorbar_name);
709              edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
710                                       wd->cp[i]->bg_rect);
711              elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
712              evas_object_color_set(wd->cp[i]->bar, wd->er, wd->eg, wd->eb, 255);
713           }
714         /* load arrow image, pointing the colorbar */
715         wd->cp[i]->arrow = edje_object_add(e);
716         _elm_theme_object_set(obj, wd->cp[i]->arrow, "colorselector", "image",
717                               "updown");
718         edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_icon",
719                                  wd->cp[i]->arrow);
720         elm_widget_sub_object_add(obj, wd->cp[i]->arrow);
721         if (i == 2)
722           evas_object_color_set(wd->cp[i]->arrow, 0, 0, 0, 255);
723         else
724           evas_object_color_set(wd->cp[i]->arrow, wd->er, wd->eg, wd->eb, 255);
725
726         /* load left button */
727         wd->cp[i]->lbt = edje_object_add(e);
728         _elm_theme_object_set(obj, wd->cp[i]->lbt, "colorselector", "button",
729                               "left");
730         evas_object_event_callback_add(wd->cp[i]->lbt, EVAS_CALLBACK_MOUSE_DOWN,
731                                        _left_button_down_cb, wd->cp[i]);
732         evas_object_event_callback_add(wd->cp[i]->lbt, EVAS_CALLBACK_MOUSE_UP,
733                                        _left_button_up_cb, wd->cp[i]);
734         edje_object_part_swallow(wd->cp[i]->colorbar, "elm.l_button",
735                                  wd->cp[i]->lbt);
736         elm_widget_sub_object_add(obj, wd->cp[i]->lbt);
737
738         /* load right button */
739         wd->cp[i]->rbt = edje_object_add(e);
740         _elm_theme_object_set(obj, wd->cp[i]->rbt, "colorselector", "button",
741                               "right");
742         evas_object_event_callback_add(wd->cp[i]->rbt, EVAS_CALLBACK_MOUSE_DOWN,
743                                        _right_button_down_cb, wd->cp[i]);
744         evas_object_event_callback_add(wd->cp[i]->rbt, EVAS_CALLBACK_MOUSE_UP,
745                                        _right_button_up_cb, wd->cp[i]);
746         edje_object_part_swallow(wd->cp[i]->colorbar, "elm.r_button",
747                                  wd->cp[i]->rbt);
748         elm_widget_sub_object_add(obj, wd->cp[i]->rbt);
749      }
750 }
751
752 static void
753 _set_color(Evas_Object *obj, int r, int g, int b, int a)
754 {
755    Widget_Data *wd = elm_widget_data_get(obj);
756    double x, y;
757
758    wd->r = r;
759    wd->g = g;
760    wd->b = b;
761    wd->a = a;
762
763    _rgb_to_hsl(wd);
764
765    edje_object_part_drag_value_get(wd->cp[0]->colorbar, "elm.arrow", &x, &y);
766    x = wd->h / 360.0;
767    edje_object_part_drag_value_set(wd->cp[0]->colorbar, "elm.arrow", x, y);
768    _draw_rects(wd->cp[0], x);
769
770    edje_object_part_drag_value_get(wd->cp[1]->colorbar, "elm.arrow", &x, &y);
771    x = 1.0 - wd->s;
772    edje_object_part_drag_value_set(wd->cp[1]->colorbar, "elm.arrow", x, y);
773    _draw_rects(wd->cp[1], x);
774
775    edje_object_part_drag_value_get(wd->cp[2]->colorbar, "elm.arrow", &x, &y);
776    x = wd->l;
777    edje_object_part_drag_value_set(wd->cp[2]->colorbar, "elm.arrow", x, y);
778    _draw_rects(wd->cp[2], x);
779
780    edje_object_part_drag_value_get(wd->cp[3]->colorbar, "elm.arrow", &x, &y);
781    x = wd->a / 255.0;
782    edje_object_part_drag_value_set(wd->cp[3]->colorbar, "elm.arrow", x, y);
783    _draw_rects(wd->cp[3], x);
784 }
785
786 EAPI Evas_Object *
787 elm_colorselector_add(Evas_Object *parent)
788 {
789    Evas_Object *obj = NULL;
790    Widget_Data *wd = NULL;
791    Evas *e;
792
793    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
794
795    ELM_SET_WIDTYPE(widtype, "colorselector");
796    elm_widget_type_set(obj, "colorselector");
797    elm_widget_sub_object_add(parent, obj);
798    elm_widget_data_set(obj, wd);
799    elm_widget_del_hook_set(obj, _del_hook);
800    elm_widget_theme_hook_set(obj, _theme_hook);
801
802    /* load background edj */
803    wd->base = edje_object_add(e);
804    _elm_theme_object_set(obj, wd->base, "colorselector", "bg", "default");
805    elm_widget_resize_object_set(obj, wd->base);
806
807    wd->er = 255;
808    wd->eg = 0;
809    wd->eb = 0;
810    wd->h = 0.0;
811    wd->s = 1.0;
812    wd->l = 0.0;
813    wd->a = 255;
814
815    _hsl_to_rgb(wd);
816    _add_colorbar(obj);
817    _sizing_eval(obj);
818
819    evas_object_smart_callbacks_descriptions_set(obj, _signals);
820    return obj;
821 }
822
823 EAPI void
824 elm_colorselector_color_set(Evas_Object *obj, int r, int g, int b, int a)
825 {
826    ELM_CHECK_WIDTYPE(obj, widtype);
827    _set_color(obj, r, g, b, a);
828 }
829
830 EAPI void
831 elm_colorselector_color_get(const Evas_Object *obj, int *r, int *g, int *b, int*a)
832 {
833    Widget_Data *wd = elm_widget_data_get(obj);
834    ELM_CHECK_WIDTYPE(obj, widtype);
835
836    if (r) *r = wd->r;
837    if (g) *g = wd->g;
838    if (b) *b = wd->b;
839    if (a) *a = wd->a;
840 }