1 #include <Elementary.h>
4 #define BASE_STEP 360.0
9 #define DEFAULT_HOR_PAD 10
10 #define DEFAULT_VER_PAD 10
12 typedef enum _Color_Type
20 typedef struct _Colorselector_Data Colorselector_Data;
21 struct _Colorselector_Data
24 Evas_Object *colorbar;
30 Evas_Object *touch_area;
31 Color_Type color_type;
34 typedef struct _Widget_Data Widget_Data;
35 typedef struct _Elm_Color_Item Elm_Color_Item;
42 Colorselector_Data *cp[4];
43 Ecore_Timer *longpress_timer;
44 const char *palette_name;
45 Evas_Coord _x, _y, _w, _h;
51 Elm_Colorselector_Mode mode;
52 Eina_Bool longpressed : 1;
53 Eina_Bool config_load: 1;
56 struct _Elm_Color_Item
59 Evas_Object *color_obj;
60 Elm_Color_RGBA *color;
63 static const char *widtype = NULL;
65 static void _del_hook(Evas_Object *obj);
66 static void _theme_hook(Evas_Object *obj);
67 static void _sizing_eval(Evas_Object *obj);
68 static void _resize_cb(void *data, Evas *a, Evas_Object *obj, void *event_info);
69 static void _rgb_to_hsl(void *data);
70 static void _hsl_to_rgb(void *data);
71 static void _color_with_saturation(void *data);
72 static void _color_with_lightness(void *data);
73 static void _draw_rects(void *data, double x);
74 static void _arrow_cb(void *data, Evas_Object *obj, const char *emission,
76 static void _colorbar_cb(void *data, Evas *e, Evas_Object *obj,
78 static void _left_button_clicked_cb(void *data, Evas_Object * obj,
80 static void _left_button_repeat_cb(void *data, Evas_Object * obj,
82 static void _right_button_clicked_cb(void *data, Evas_Object * obj,
84 static void _right_button_repeat_cb(void *data, Evas_Object * obj,
86 static void _add_colorbar(Evas_Object *obj);
87 static void _set_color(Evas_Object *obj, int r, int g, int b, int a);
88 static Elm_Color_Item *_item_new(Evas_Object *obj);
89 static void _item_sizing_eval(Elm_Color_Item *item);
90 static void _item_highlight(void *data, Evas *e, Evas_Object *obj, void *event_info);
91 static void _item_unhighlight(void *data, Evas *e, Evas_Object *obj, void *event_info);
92 static Eina_Bool _long_press(void *data);
93 static void _remove_items(Widget_Data *wd);
94 static void _colors_remove(Evas_Object *obj);
95 static void _colors_save(Evas_Object *obj);
96 static void _colors_load_apply(Evas_Object *obj);
98 static const char SIG_CHANGED[] = "changed";
99 static const char SIG_COLOR_ITEM_SELECTED[] = "color,item,selected";
100 static const char SIG_COLOR_ITEM_LONGPRESSED[] = "color,item,longpressed";
102 static const Evas_Smart_Cb_Description _signals[] =
104 {SIG_COLOR_ITEM_SELECTED, ""},
105 {SIG_COLOR_ITEM_LONGPRESSED, ""},
111 _del_hook(Evas_Object *obj)
113 Widget_Data *wd = elm_widget_data_get(obj);
117 if (wd->longpress_timer) ecore_timer_del(wd->longpress_timer);
118 if (wd->palette_name) eina_stringshare_del(wd->palette_name);
120 for (i = 0; i < 4; i++) free(wd->cp[i]);
125 _theme_hook(Evas_Object *obj)
127 Widget_Data *wd = elm_widget_data_get(obj);
129 Elm_Color_Item *item;
131 const char *hpadstr, *vpadstr;
132 unsigned int h_pad = DEFAULT_HOR_PAD;
133 unsigned int v_pad = DEFAULT_VER_PAD;
135 if ((!wd) || (!wd->sel)) return;
137 _elm_theme_object_set(obj, wd->base, "colorselector", "palette",
138 elm_widget_style_get(obj));
139 _elm_theme_object_set(obj, wd->sel, "colorselector", "bg",
140 elm_widget_style_get(obj));
141 hpadstr = edje_object_data_get(wd->base, "horizontal_pad");
142 if (hpadstr) h_pad = atoi(hpadstr);
143 vpadstr = edje_object_data_get(wd->base, "vertical_pad");
144 if (vpadstr) v_pad = atoi(vpadstr);
145 elm_box_padding_set(wd->box, (Evas_Coord)(h_pad * elm_widget_scale_get(obj) * _elm_config->scale),
146 (Evas_Coord)(v_pad * elm_widget_scale_get(obj) *_elm_config->scale));
147 EINA_LIST_FOREACH(wd->items, elist, item)
149 elm_layout_theme_set(VIEW(item), "colorselector", "item", elm_widget_style_get(obj));
150 _elm_theme_object_set(obj, item->color_obj, "colorselector", "item/color", elm_widget_style_get(obj));
152 for (i = 0; i < 4; i++)
154 evas_object_del(wd->cp[i]->colorbar);
155 wd->cp[i]->colorbar = NULL;
156 evas_object_del(wd->cp[i]->bar);
157 wd->cp[i]->bar = NULL;
158 evas_object_del(wd->cp[i]->lbt);
159 wd->cp[i]->lbt = NULL;
160 evas_object_del(wd->cp[i]->rbt);
161 wd->cp[i]->rbt = NULL;
164 evas_object_del(wd->cp[i]->bg_rect);
165 wd->cp[i]->bg_rect = NULL;
167 evas_object_del(wd->cp[i]->arrow);
168 wd->cp[i]->arrow = NULL;
169 evas_object_del(wd->cp[i]->touch_area);
170 wd->cp[i]->touch_area = NULL;
174 elm_colorselector_color_set(obj, wd->r, wd->g, wd->b, wd->a);
179 _colorselector_set_size_hints(Evas_Object *obj, int timesw, int timesh)
181 Evas_Coord minw = -1, minh = -1;
183 elm_coords_finger_size_adjust(timesw, &minw, timesh, &minh);
184 edje_object_size_min_restricted_calc(obj, &minw, &minh,
186 evas_object_size_hint_min_set(obj, minw, minh);
187 evas_object_size_hint_max_set(obj, -1, -1);
191 _item_sizing_eval(Elm_Color_Item *item)
193 Evas_Coord minw = -1, minh = -1;
197 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
198 edje_object_size_min_restricted_calc(VIEW(item), &minw, &minh, minw,
200 evas_object_size_hint_min_set(VIEW(item), minw, minh);
203 static void _resize_cb(void *data, Evas *a __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
209 _sizing_eval_palette(Evas_Object *obj)
211 Widget_Data *wd = elm_widget_data_get(obj);
213 Elm_Color_Item *item;
214 Evas_Coord bw = 0, bh = 0;
215 Evas_Coord w = 0, h = 0;
218 EINA_LIST_FOREACH(wd->items, elist, item)
220 _item_sizing_eval(item);
222 evas_object_size_hint_min_get(wd->box, &bw, &bh);
223 evas_object_size_hint_min_set(obj, bw, bh);
224 evas_object_size_hint_max_set(obj, -1, -1);
225 evas_object_geometry_get(obj, NULL, NULL, &w, &h);
228 evas_object_resize(obj, w, h);
232 _sizing_eval_selector(Evas_Object *obj)
234 Widget_Data *wd = elm_widget_data_get(obj);
235 Evas_Coord minw = -1, minh = -1;
236 Evas_Coord w = 0, h = 0;
240 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
241 for (i = 0; i < 4; i++)
243 if (wd->cp[i]->bg_rect)
244 _colorselector_set_size_hints(wd->cp[i]->bg_rect, 1, 1);
245 _colorselector_set_size_hints(wd->cp[i]->bar, 1, 1);
246 _colorselector_set_size_hints(wd->cp[i]->rbt, 1, 1);
247 _colorselector_set_size_hints(wd->cp[i]->lbt, 1, 1);
249 _colorselector_set_size_hints(wd->cp[i]->colorbar, 4, 1);
252 elm_coords_finger_size_adjust(4, &minw, 4, &minh);
253 edje_object_size_min_restricted_calc(wd->sel, &minw, &minh, minw, minh);
254 evas_object_size_hint_min_set(obj, minw, minh);
255 evas_object_size_hint_max_set(obj, -1, -1);
256 evas_object_geometry_get(obj, NULL, NULL, &w, &h);
257 if (w < minw) w = minw;
258 if (h < minh) h = minh;
259 evas_object_resize(obj, w, h);
263 _sizing_eval_palette_selector(Evas_Object *obj)
265 Widget_Data *wd = elm_widget_data_get(obj);
266 Evas_Coord minw = -1, minh = -1;
267 Evas_Coord bw = 0, bh = 0;
268 Evas_Coord w = 0, h = 0;
271 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
272 for (i = 0; i < 4; i++)
274 if (wd->cp[i]->bg_rect)
275 _colorselector_set_size_hints(wd->cp[i]->bg_rect, 1, 1);
276 _colorselector_set_size_hints(wd->cp[i]->bar, 1, 1);
277 _colorselector_set_size_hints(wd->cp[i]->rbt, 1, 1);
278 _colorselector_set_size_hints(wd->cp[i]->lbt, 1, 1);
280 _colorselector_set_size_hints(wd->cp[i]->colorbar, 4, 1);
283 elm_coords_finger_size_adjust(4, &minw, 4, &minh);
284 edje_object_size_min_restricted_calc(wd->sel, &minw, &minh, minw, minh);
285 evas_object_size_hint_min_get(wd->box, &bw, &bh);
286 evas_object_size_hint_min_set(obj, minw, minh+bh);
287 evas_object_size_hint_max_set(obj, -1, -1);
288 evas_object_geometry_get(obj, NULL, NULL, &w, &h);
289 if (w < minw) w = minw;
290 if (h < (minh+bh)) h = (minh+bh);
291 evas_object_resize(obj, w, h);
295 _sizing_eval(Evas_Object *obj)
297 Widget_Data *wd = elm_widget_data_get(obj);
301 case ELM_COLORSELECTOR_PALETTE:
302 _sizing_eval_palette(obj);
304 case ELM_COLORSELECTOR_COMPONENTS:
305 _sizing_eval_selector(obj);
307 case ELM_COLORSELECTOR_BOTH:
308 _sizing_eval_palette_selector(obj);
316 _long_press(void *data)
318 Elm_Color_Item *item = (Elm_Color_Item *) data;
319 Widget_Data *wd = elm_widget_data_get(WIDGET(item));
320 if (!wd) return ECORE_CALLBACK_CANCEL;
321 wd->longpress_timer = NULL;
322 wd->longpressed = EINA_TRUE;
323 evas_object_smart_callback_call(WIDGET(item), SIG_COLOR_ITEM_LONGPRESSED, item);
324 return ECORE_CALLBACK_CANCEL;
328 _item_highlight(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
330 Elm_Color_Item *item = (Elm_Color_Item *) data;
331 Evas_Event_Mouse_Down *ev = event_info;
333 Widget_Data *wd = elm_widget_data_get(WIDGET(item));
335 if (ev->button != 1) return;
336 elm_object_signal_emit(VIEW(item), "elm,state,selected", "elm");
337 wd->longpressed = EINA_FALSE;
338 if (wd->longpress_timer) ecore_timer_del(wd->longpress_timer);
339 wd->longpress_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press, data);
343 _item_unhighlight(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
345 Elm_Color_Item *item = (Elm_Color_Item *) data;
346 Evas_Event_Mouse_Down *ev = event_info;
348 Widget_Data *wd = elm_widget_data_get(WIDGET(item));
350 if (ev->button != 1) return;
351 if (wd->longpress_timer)
353 ecore_timer_del(wd->longpress_timer);
354 wd->longpress_timer = NULL;
356 elm_object_signal_emit(VIEW(item), "elm,state,unselected", "elm");
357 if (!wd->longpressed)
359 evas_object_smart_callback_call(WIDGET(item), SIG_COLOR_ITEM_SELECTED, item);
360 elm_colorselector_color_set(WIDGET(item), item->color->r, item->color->g, item->color->b, item->color->a);
365 _remove_items(Widget_Data *wd)
367 Elm_Color_Item *item;
369 if (!wd->items) return;
371 EINA_LIST_FREE(wd->items, item)
374 elm_widget_item_free(item);
380 static Elm_Color_Item*
381 _item_new(Evas_Object *obj)
383 Elm_Color_Item *item;
386 wd = elm_widget_data_get(obj);
387 if (!wd) return NULL;
389 item = elm_widget_item_new(obj, Elm_Color_Item);
390 if (!item) return NULL;
392 VIEW(item) = elm_layout_add(obj);
393 elm_layout_theme_set(VIEW(item), "colorselector", "item", elm_widget_style_get(obj));
394 evas_object_size_hint_weight_set(VIEW(item), EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
395 evas_object_size_hint_align_set(VIEW(item), EVAS_HINT_FILL, EVAS_HINT_FILL);
396 item->color_obj = edje_object_add(evas_object_evas_get(obj));
397 _elm_theme_object_set(obj, item->color_obj, "colorselector", "item/color", elm_widget_style_get(obj));
398 evas_object_size_hint_weight_set(item->color_obj, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
399 evas_object_size_hint_align_set(item->color_obj, EVAS_HINT_FILL, EVAS_HINT_FILL);
400 evas_object_event_callback_add(item->color_obj, EVAS_CALLBACK_MOUSE_DOWN, _item_highlight, item);
401 evas_object_event_callback_add(item->color_obj, EVAS_CALLBACK_MOUSE_UP, _item_unhighlight, item);
402 elm_object_part_content_set(VIEW(item), "color_obj", item->color_obj);
403 _item_sizing_eval(item);
404 evas_object_show(VIEW(item));
410 _colors_remove(Evas_Object *obj)
412 Widget_Data *wd = elm_widget_data_get(obj);
415 _elm_config_colors_free(wd->palette_name);
418 static void _colors_save(Evas_Object *obj)
421 Widget_Data *wd = elm_widget_data_get(obj);
422 Elm_Color_Item *item;
423 _elm_config_colors_free(wd->palette_name);
424 EINA_LIST_FOREACH(wd->items, elist, item)
426 _elm_config_color_set(wd->palette_name, item->color->r, item->color->g,
427 item->color->b, item->color->a);
432 _colors_load_apply(Evas_Object *obj)
434 Elm_Color_RGBA *color;
436 Eina_List *color_list;
437 Elm_Color_Item *item;
438 Widget_Data *wd = elm_widget_data_get(obj);
439 color_list = _elm_config_color_list_get(wd->palette_name);
440 if (!color_list) return;
441 EINA_LIST_FOREACH(color_list, elist, color)
443 item = _item_new(obj);
445 item->color = ELM_NEW(Elm_Color_RGBA);
446 if (!item->color) return;
447 item->color->r = color->r;
448 item->color->g = color->g;
449 item->color->b = color->b;
450 item->color->a = color->a;
451 elm_box_pack_end(wd->box, VIEW(item));
452 evas_object_color_set(item->color_obj, item->color->r, item->color->g,
453 item->color->b, item->color->a);
454 wd->items = eina_list_append(wd->items, item);
455 _sizing_eval_palette(obj);
457 wd->config_load = EINA_TRUE;
461 _rgb_to_hsl(void *data)
463 Widget_Data *wd = data;
486 wd->l = (m + v) / 2.0;
488 if (wd->l <= 0.0) return;
493 if (wd->s > 0.0) wd->s /= (wd->l <= 0.5) ? (v + m) : (2.0 - v - m);
500 if (r == v) wd->h = (g == m ? 5.0 + b2 : 1.0 - g2);
501 else if (g == v) wd->h = (b == m ? 1.0 + r2 : 3.0 - b2);
502 else wd->h = (r == m ? 3.0 + g2 : 5.0 - r2);
508 _hsl_to_rgb(void *data)
510 Widget_Data *wd = data;
511 double r = 0, g = 0, b = 0;
514 double sv, vsf, f, p, q, t, v;
520 if (_s == 0.0) r = g = b = _l;
523 if (_h == 360.0) _h = 0.0;
526 v = (_l <= 0.5) ? (_l * (1.0 + _s)) : (_l + _s - (_l * _s));
529 if (v) sv = (v - p) / v;
574 i = (int)(r * 255.0);
576 wd->r = (f <= 0.5) ? i : (i + 1);
578 i = (int)(g * 255.0);
580 wd->g = (f <= 0.5) ? i : (i + 1);
582 i = (int)(b * 255.0);
584 wd->b = (f <= 0.5) ? i : (i + 1);
588 _color_with_saturation(void *data)
590 Widget_Data *wd = data;
593 wd->sr = 127 + (int)((double)(wd->er - 127) * wd->s);
595 wd->sr = 127 - (int)((double)(127 - wd->er) * wd->s);
598 wd->sg = 127 + (int)((double)(wd->eg - 127) * wd->s);
600 wd->sg = 127 - (int)((double)(127 - wd->eg) * wd->s);
603 wd->sb = 127 + (int)((double)(wd->eb - 127) * wd->s);
605 wd->sb = 127 - (int)((double)(127 - wd->eb) * wd->s);
609 _color_with_lightness(void *data)
611 Widget_Data *wd = data;
615 wd->lr = wd->er + (int)((double)(255 - wd->er) * (wd->l - 0.5) * 2.0);
616 wd->lg = wd->eg + (int)((double)(255 - wd->eg) * (wd->l - 0.5) * 2.0);
617 wd->lb = wd->eb + (int)((double)(255 - wd->eb) * (wd->l - 0.5) * 2.0);
619 else if (wd->l < 0.5)
621 wd->lr = (double)wd->er * wd->l * 2.0;
622 wd->lg = (double)wd->eg * wd->l * 2.0;
623 wd->lb = (double)wd->eb * wd->l * 2.0;
634 _draw_rects(void *data, double x)
636 Colorselector_Data *cp = data;
637 Widget_Data *wd = elm_widget_data_get(cp->parent);
638 double one_six = 1.0 / 6.0;
640 switch (cp->color_type)
648 wd->eg = (255.0 * x * 6.0);
651 else if (x < 2 * one_six)
653 wd->er = 255 - (int)(255.0 * (x - one_six) * 6.0);
657 else if (x < 3 * one_six)
661 wd->eb = (int)(255.0 * (x - (2.0 * one_six)) * 6.0);
663 else if (x < 4 * one_six)
666 wd->eg = 255 - (int)(255.0 * (x - (3.0 * one_six)) * 6.0);
669 else if (x < 5 * one_six)
671 wd->er = 255.0 * (x - (4.0 * one_six)) * 6.0;
679 wd->eb = 255 - (int)(255.0 * (x - (5.0 * one_six)) * 6.0);
682 evas_object_color_set(wd->cp[0]->arrow, wd->er, wd->eg, wd->eb, 255);
683 evas_object_color_set(wd->cp[1]->bg_rect, wd->er, wd->eg, wd->eb, 255);
684 evas_object_color_set(wd->cp[2]->bg_rect, wd->er, wd->eg, wd->eb, 255);
685 evas_object_color_set(wd->cp[3]->bar, wd->er, wd->eg, wd->eb, 255);
687 _color_with_saturation(wd);
688 evas_object_color_set(wd->cp[1]->arrow, wd->sr, wd->sg, wd->sb, 255);
690 _color_with_lightness(wd);
691 evas_object_color_set(wd->cp[2]->arrow, wd->lr, wd->lg, wd->lb, 255);
693 evas_object_color_set(wd->cp[3]->arrow,
694 (wd->er * wd->a) / 255,
695 (wd->eg * wd->a) / 255,
696 (wd->eb * wd->a) / 255,
701 _color_with_saturation(wd);
702 evas_object_color_set(wd->cp[1]->arrow, wd->sr, wd->sg, wd->sb, 255);
706 _color_with_lightness(wd);
707 evas_object_color_set(wd->cp[2]->arrow, wd->lr, wd->lg, wd->lb, 255);
711 evas_object_color_set(wd->cp[3]->arrow,
712 (wd->er * wd->a) / 255,
713 (wd->eg * wd->a) / 255,
714 (wd->eb * wd->a) / 255,
724 _arrow_cb(void *data, Evas_Object *obj, const char *emission __UNUSED__,
725 const char *source __UNUSED__)
727 Colorselector_Data *cp = data;
730 edje_object_part_drag_value_get(obj, "elm.arrow", &x, &y);
731 _draw_rects(data, x);
732 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
736 _colorbar_cb(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *event_info)
738 Colorselector_Data *cp = data;
739 Evas_Event_Mouse_Down *ev = event_info;
740 Evas_Coord x, y, w, h;
741 double arrow_x = 0, arrow_y;
743 evas_object_geometry_get(cp->bar, &x, &y, &w, &h);
744 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow",
746 if (w > 0) arrow_x = (double)(ev->canvas.x - x) / (double)w;
747 if (arrow_x > 1) arrow_x = 1;
748 if (arrow_x < 0) arrow_x = 0;
749 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", arrow_x, arrow_y);
750 _draw_rects(data, arrow_x);
751 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
752 evas_event_feed_mouse_cancel(e, 0, NULL);
753 evas_event_feed_mouse_down(e, 1, EVAS_BUTTON_NONE, 0, NULL);
757 _left_button_clicked_cb(void *data, Evas_Object * obj __UNUSED__,
758 void *event_info __UNUSED__)
760 Colorselector_Data *cp = data;
763 edje_object_signal_emit(cp->lbt, "elm,state,left,button,down",
765 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
767 switch(cp->color_type)
785 if (x < 0.0) x = 0.0;
787 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
788 _draw_rects(data, x);
789 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
793 _left_button_repeat_cb(void *data, Evas_Object * obj __UNUSED__,
794 void *event_info __UNUSED__)
796 Colorselector_Data *cp = data;
799 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
800 x -= 1.0 / BASE_STEP;
801 if (x < 0.0) x = 0.0;
802 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
803 _draw_rects(data, x);
804 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
809 _right_button_clicked_cb(void *data, Evas_Object * obj __UNUSED__,
810 void *event_info __UNUSED__)
812 Colorselector_Data *cp = data;
815 edje_object_signal_emit(cp->rbt, "elm,state,right,button,down",
817 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
819 switch(cp->color_type)
837 if (x > 1.0) x = 1.0;
839 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
840 _draw_rects(data, x);
841 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
845 _right_button_repeat_cb(void *data, Evas_Object * obj __UNUSED__,
846 void *event_info __UNUSED__)
848 Colorselector_Data *cp = data;
851 edje_object_part_drag_value_get(cp->colorbar, "elm.arrow", &x, &y);
852 x += 1.0 / BASE_STEP;
853 if (x > 1.0) x = 1.0;
854 edje_object_part_drag_value_set(cp->colorbar, "elm.arrow", x, y);
855 _draw_rects(data, x);
856 evas_object_smart_callback_call(cp->parent, SIG_CHANGED, NULL);
860 _add_colorbar(Evas_Object *obj)
862 char colorbar_name[128];
863 char colorbar_s[128];
869 wd = elm_widget_data_get(obj);
872 e = evas_object_evas_get(obj);
874 for (i = 0; i < 4; i++)
876 wd->cp[i] = ELM_NEW(Colorselector_Data);
877 wd->cp[i]->parent = obj;
881 wd->cp[i]->color_type = HUE;
884 wd->cp[i]->color_type = SATURATION;
887 wd->cp[i]->color_type = LIGHTNESS;
890 wd->cp[i]->color_type = ALPHA;
895 /* load colorbar area */
896 wd->cp[i]->colorbar = edje_object_add(e);
897 _elm_theme_object_set(obj, wd->cp[i]->colorbar, "colorselector", "base",
898 elm_widget_style_get(obj));
899 snprintf(colorbar_name, sizeof(colorbar_name), "colorbar_%d", i);
900 snprintf(colorbar_s, sizeof(colorbar_s), "elm.colorbar_%d", i);
901 edje_object_signal_callback_add(wd->cp[i]->colorbar, "drag", "*",
902 _arrow_cb, wd->cp[i]);
903 edje_object_part_swallow(wd->sel, colorbar_s, wd->cp[i]->colorbar);
904 elm_widget_sub_object_add(obj, wd->cp[i]->colorbar);
906 /* load colorbar image */
907 wd->cp[i]->bar = edje_object_add(e);
908 snprintf(buf, sizeof(buf), "%s/%s", colorbar_name,
909 elm_widget_style_get(obj));
910 _elm_theme_object_set(obj, wd->cp[i]->bar, "colorselector", "image",
912 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar",
914 elm_widget_sub_object_add(obj, wd->cp[i]->bar);
916 /* provide expanded touch area */
917 wd->cp[i]->touch_area = evas_object_rectangle_add(e);
918 evas_object_color_set(wd->cp[i]->touch_area, 0, 0, 0, 0);
919 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_bg",
920 wd->cp[i]->touch_area);
921 evas_object_event_callback_add(wd->cp[i]->touch_area,
922 EVAS_CALLBACK_MOUSE_DOWN, _colorbar_cb,
924 elm_widget_sub_object_add(obj, wd->cp[i]->touch_area);
926 /* load background rectangle of the colorbar. used for
927 changing color of the opacity bar */
928 if ((i == 1) || (i == 2))
930 wd->cp[i]->bg_rect = evas_object_rectangle_add(e);
931 evas_object_color_set(wd->cp[i]->bg_rect, wd->er, wd->eg, wd->eb,
933 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
936 elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
940 wd->cp[i]->bg_rect = edje_object_add(e);
941 snprintf(buf, sizeof(buf), "%s/%s", colorbar_name,
942 elm_widget_style_get(obj));
943 _elm_theme_object_set(obj, wd->cp[i]->bg_rect, "colorselector",
945 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.bar_bg",
947 elm_widget_sub_object_add(obj, wd->cp[i]->bg_rect);
948 evas_object_color_set(wd->cp[i]->bar, wd->er, wd->eg, wd->eb, 255);
950 /* load arrow image, pointing the colorbar */
951 wd->cp[i]->arrow = edje_object_add(e);
952 _elm_theme_object_set(obj, wd->cp[i]->arrow, "colorselector", "arrow",
953 elm_widget_style_get(obj));
954 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.arrow_icon",
956 elm_widget_sub_object_add(obj, wd->cp[i]->arrow);
958 evas_object_color_set(wd->cp[i]->arrow, 0, 0, 0, 255);
960 evas_object_color_set(wd->cp[i]->arrow, wd->er, wd->eg, wd->eb, 255);
962 /* load left button */
963 wd->cp[i]->lbt = elm_button_add(obj);
964 snprintf(buf, sizeof(buf), "colorselector/left/%s",
965 elm_widget_style_get(obj));
966 elm_object_style_set(wd->cp[i]->lbt, buf);
967 elm_widget_sub_object_add(obj, wd->cp[i]->lbt);
968 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.l_button",
970 evas_object_smart_callback_add(wd->cp[i]->lbt, "clicked",
971 _left_button_clicked_cb, wd->cp[i]);
972 elm_button_autorepeat_set(wd->cp[i]->lbt, EINA_TRUE);
973 elm_button_autorepeat_initial_timeout_set(wd->cp[i]->lbt,
974 _elm_config->longpress_timeout);
975 elm_button_autorepeat_gap_timeout_set(wd->cp[i]->lbt,
976 (1.0 / _elm_config->fps));
977 evas_object_smart_callback_add(wd->cp[i]->lbt, "repeated",
978 _left_button_repeat_cb, wd->cp[i]);
980 /* load right button */
981 wd->cp[i]->rbt = elm_button_add(obj);
982 snprintf(buf, sizeof(buf), "colorselector/right/%s",
983 elm_widget_style_get(obj));
984 elm_object_style_set(wd->cp[i]->rbt, buf);
985 elm_widget_sub_object_add(obj, wd->cp[i]->rbt);
986 edje_object_part_swallow(wd->cp[i]->colorbar, "elm.r_button",
988 evas_object_smart_callback_add(wd->cp[i]->rbt, "clicked",
989 _right_button_clicked_cb, wd->cp[i]);
990 elm_button_autorepeat_set(wd->cp[i]->rbt, EINA_TRUE);
991 elm_button_autorepeat_initial_timeout_set(wd->cp[i]->rbt,
992 _elm_config->longpress_timeout);
993 elm_button_autorepeat_gap_timeout_set(wd->cp[i]->rbt,
994 (1.0 / _elm_config->fps));
995 evas_object_smart_callback_add(wd->cp[i]->rbt, "repeated",
996 _right_button_repeat_cb, wd->cp[i]);
1001 _set_color(Evas_Object *obj, int r, int g, int b, int a)
1003 Widget_Data *wd = elm_widget_data_get(obj);
1013 edje_object_part_drag_value_get(wd->cp[0]->colorbar, "elm.arrow", &x, &y);
1015 edje_object_part_drag_value_set(wd->cp[0]->colorbar, "elm.arrow", x, y);
1016 _draw_rects(wd->cp[0], x);
1018 edje_object_part_drag_value_get(wd->cp[1]->colorbar, "elm.arrow", &x, &y);
1020 edje_object_part_drag_value_set(wd->cp[1]->colorbar, "elm.arrow", x, y);
1021 _draw_rects(wd->cp[1], x);
1023 edje_object_part_drag_value_get(wd->cp[2]->colorbar, "elm.arrow", &x, &y);
1025 edje_object_part_drag_value_set(wd->cp[2]->colorbar, "elm.arrow", x, y);
1026 _draw_rects(wd->cp[2], x);
1028 edje_object_part_drag_value_get(wd->cp[3]->colorbar, "elm.arrow", &x, &y);
1030 edje_object_part_drag_value_set(wd->cp[3]->colorbar, "elm.arrow", x, y);
1031 _draw_rects(wd->cp[3], x);
1035 elm_colorselector_add(Evas_Object *parent)
1037 Evas_Object *obj = NULL;
1038 Widget_Data *wd = NULL;
1040 const char *hpadstr, *vpadstr;
1041 unsigned int h_pad = DEFAULT_HOR_PAD;
1042 unsigned int v_pad = DEFAULT_VER_PAD;
1044 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
1046 ELM_SET_WIDTYPE(widtype, "colorselector");
1047 elm_widget_type_set(obj, "colorselector");
1048 elm_widget_sub_object_add(parent, obj);
1049 elm_widget_data_set(obj, wd);
1050 elm_widget_del_hook_set(obj, _del_hook);
1051 elm_widget_theme_hook_set(obj, _theme_hook);
1052 evas_object_smart_callbacks_descriptions_set(obj, _signals);
1054 /* load background edj */
1055 wd->base = edje_object_add(e);
1056 _elm_theme_object_set(obj, wd->base, "colorselector", "palette", "default");
1057 elm_widget_resize_object_set(obj, wd->base);
1058 evas_object_event_callback_add(wd->base, EVAS_CALLBACK_RESIZE,
1061 wd->box = elm_box_add(obj);
1062 elm_box_layout_set(wd->box, evas_object_box_layout_flow_horizontal,
1064 elm_box_horizontal_set(wd->box, EINA_TRUE);
1065 evas_object_size_hint_weight_set(wd->box, EVAS_HINT_EXPAND,
1067 evas_object_size_hint_align_set(wd->box, EVAS_HINT_FILL, 0);
1068 elm_box_homogeneous_set(wd->box, EINA_TRUE);
1069 hpadstr = edje_object_data_get(wd->base, "horizontal_pad");
1070 if (hpadstr) h_pad = atoi(hpadstr);
1071 vpadstr = edje_object_data_get(wd->base, "vertical_pad");
1072 if (vpadstr) v_pad = atoi(vpadstr);
1073 elm_box_padding_set(wd->box, (Evas_Coord)(h_pad * elm_widget_scale_get(obj) * _elm_config->scale),
1074 (Evas_Coord)(v_pad * elm_widget_scale_get(obj) *_elm_config->scale));
1075 elm_box_align_set(wd->box, 0.5, 0.5);
1076 elm_widget_sub_object_add(obj, wd->box);
1077 evas_object_show(wd->box);
1078 edje_object_part_swallow(wd->base, "palette", wd->box);
1079 wd->palette_name = eina_stringshare_add("default");
1080 _colors_load_apply(obj);
1082 /* load background edj */
1083 wd->sel = edje_object_add(e);
1084 _elm_theme_object_set(obj, wd->sel, "colorselector", "bg", "default");
1085 edje_object_part_swallow(wd->base, "selector", wd->sel);
1086 elm_widget_sub_object_add(obj, wd->sel);
1088 wd->mode = ELM_COLORSELECTOR_BOTH;
1105 elm_colorselector_color_set(Evas_Object *obj, int r, int g, int b, int a)
1107 ELM_CHECK_WIDTYPE(obj, widtype);
1108 _set_color(obj, r, g, b, a);
1112 elm_colorselector_color_get(const Evas_Object *obj, int *r, int *g, int *b, int *a)
1114 ELM_CHECK_WIDTYPE(obj, widtype);
1115 Widget_Data *wd = elm_widget_data_get(obj);
1124 elm_colorselector_mode_set(Evas_Object *obj, Elm_Colorselector_Mode mode)
1126 ELM_CHECK_WIDTYPE(obj, widtype);
1127 Widget_Data *wd = elm_widget_data_get(obj);
1129 if (wd->mode == mode) return;
1133 case ELM_COLORSELECTOR_PALETTE:
1134 if (edje_object_part_swallow_get(wd->base, "selector"))
1136 edje_object_part_unswallow(wd->base, wd->sel);
1137 evas_object_hide(wd->sel);
1139 if (!edje_object_part_swallow_get(wd->base, "palette"))
1141 edje_object_part_swallow(wd->base, "palette", wd->box);
1142 evas_object_show(wd->box);
1145 case ELM_COLORSELECTOR_COMPONENTS:
1146 if (edje_object_part_swallow_get(wd->base, "palette"))
1148 edje_object_part_unswallow(wd->base, wd->box);
1149 evas_object_hide(wd->box);
1151 if (!edje_object_part_swallow_get(wd->base, "selector"))
1153 edje_object_part_swallow(wd->base, "selector", wd->sel);
1154 evas_object_show(wd->sel);
1157 case ELM_COLORSELECTOR_BOTH:
1158 if (!edje_object_part_swallow_get(wd->base, "palette"))
1160 edje_object_part_swallow(wd->base, "palette", wd->box);
1161 evas_object_show(wd->box);
1163 if (!edje_object_part_swallow_get(wd->base, "selector"))
1165 edje_object_part_swallow(wd->base, "selector", wd->sel);
1166 evas_object_show(wd->sel);
1175 EAPI Elm_Colorselector_Mode
1176 elm_colorselector_mode_get(const Evas_Object *obj)
1178 ELM_CHECK_WIDTYPE(obj, widtype) ELM_COLORSELECTOR_BOTH;
1179 Widget_Data *wd = elm_widget_data_get(obj);
1180 if (!wd) return ELM_COLORSELECTOR_BOTH;
1185 elm_colorselector_palette_item_color_get(const Elm_Object_Item *it, int *r __UNUSED__, int *g __UNUSED__, int *b __UNUSED__, int*a __UNUSED__)
1187 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
1188 Elm_Color_Item *item;
1189 item = (Elm_Color_Item *) it;
1192 if(r) *r = item->color->r;
1193 if(g) *g = item->color->g;
1194 if(b) *b = item->color->b;
1195 if(a) *a = item->color->a;
1200 elm_colorselector_palette_item_color_set(Elm_Object_Item *it, int r __UNUSED__, int g __UNUSED__, int b __UNUSED__, int a __UNUSED__)
1202 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
1203 Elm_Color_Item *item;
1204 item = (Elm_Color_Item *) it;
1209 evas_object_color_set(item->color_obj, item->color->r, item->color->g, item->color->b, item->color->a);
1210 _colors_save(WIDGET(it));
1213 EAPI Elm_Object_Item *
1214 elm_colorselector_palette_color_add(Evas_Object *obj, int r, int g, int b, int a)
1216 Elm_Color_Item *item;
1217 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1218 Widget_Data *wd = elm_widget_data_get(obj);
1219 if (!wd) return NULL;
1220 if (wd->config_load)
1222 _colors_remove(obj);
1223 wd->config_load = EINA_FALSE;
1225 item = _item_new(obj);
1226 if (!item) return NULL;
1227 item->color = ELM_NEW(Elm_Color_RGBA);
1228 if (!item->color) return NULL;
1233 _elm_config_color_set(wd->palette_name, item->color->r, item->color->g,
1234 item->color->b, item->color->a);
1235 elm_box_pack_end(wd->box, VIEW(item));
1236 evas_object_color_set(item->color_obj, item->color->r, item->color->g,
1237 item->color->b, item->color->a);
1238 wd->items = eina_list_append(wd->items, item);
1240 return (Elm_Object_Item *) item;
1244 elm_colorselector_palette_clear(Evas_Object *obj)
1246 ELM_CHECK_WIDTYPE(obj, widtype);
1247 Widget_Data *wd = elm_widget_data_get(obj);
1249 _colors_remove(obj);
1253 elm_colorselector_palette_name_set(Evas_Object *obj, const char *palette_name)
1255 ELM_CHECK_WIDTYPE(obj, widtype);
1256 Widget_Data *wd = elm_widget_data_get(obj);
1258 if (!strcmp(wd->palette_name, palette_name)) return;
1261 _colors_remove(obj);
1262 eina_stringshare_replace(&wd->palette_name, palette_name);
1263 _colors_load_apply(obj);
1268 elm_colorselector_palette_name_get(const Evas_Object *obj)
1270 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1271 Widget_Data *wd = elm_widget_data_get(obj);
1272 if (!wd) return NULL;
1273 return wd->palette_name;