1 #include "e.h"
2
3 static void _e_color_dialog_button1_click(void *data, E_Dialog *edia);
4 static void _e_color_dialog_button2_click(void *data, E_Dialog *edia);
5 static void _e_color_dialog_free(E_Color_Dialog *dia);
6 static void _e_color_dialog_dia_del(void *obj);
7 static void _e_color_dialog_cb_csel_change(void *data, Evas_Object *obj, void *ev);
8
9 /**
10 * Create a color selector dialog.
11 *
12 * @param c compositor to display on
13 * @param color color to initialize to (or NULL for black).
14 * @param alpha_enabled if set, uses alpha and let user edit it.
15 */
16 E_Color_Dialog *
e_color_dialog_new(const E_Color * color,Eina_Bool alpha_enabled)17 e_color_dialog_new(const E_Color *color, Eina_Bool alpha_enabled)
18 {
19 E_Color_Dialog *dia;
20 Evas_Object *o, *bx, *re, *fr;
21
22 dia = E_OBJECT_ALLOC(E_Color_Dialog, E_COLOR_DIALOG_TYPE, _e_color_dialog_free);
23 if (!dia) return NULL;
24 dia->dia = e_dialog_new(NULL, "E", "_color_dialog");
25 e_dialog_title_set(dia->dia, _("Color Selector"));
26
27 dia->color = calloc(1, sizeof(E_Color));
28 dia->initial = calloc(1, sizeof(E_Color));
29
30 if (color)
31 e_color_copy(color, dia->color);
32
33 if ((!color) || (!alpha_enabled))
34 dia->color->a = 255;
35
36 e_color_copy(dia->color, dia->initial);
37
38 bx = elm_box_add(dia->dia->win);
39 E_EXPAND(bx);
40 E_FILL(bx);
41
42 o = elm_colorselector_add(bx);
43 elm_colorselector_mode_set(o, ELM_COLORSELECTOR_COMPONENTS);
44 elm_colorselector_color_set(o, dia->color->r, dia->color->g, dia->color->b, dia->color->a);
45 E_EXPAND(o);
46 E_FILL(o);
47 evas_object_show(o);
48 elm_box_pack_end(bx, o);
49
50 fr = elm_frame_add(bx);
51 E_WEIGHT(fr, EVAS_HINT_EXPAND, 0);
52 E_FILL(fr);
53 elm_object_text_set(fr, _("Color Preview"));
54 evas_object_show(fr);
55 elm_box_pack_end(bx, fr);
56
57 re = evas_object_rectangle_add(evas_object_evas_get(dia->dia->win));
58 evas_object_data_set(o, "rect", re);
59 evas_object_size_hint_min_set(re, 1, ELM_SCALE_SIZE(100));
60 evas_object_show(re);
61 elm_object_content_set(fr, re);
62
63 evas_object_smart_callback_add(o, "changed", _e_color_dialog_cb_csel_change, dia);
64 e_dialog_content_set(dia->dia, bx, 250, -1);
65
66 /* buttons at the bottom */
67 e_dialog_button_add(dia->dia, _("Select"), NULL, _e_color_dialog_button1_click, dia);
68 e_dialog_button_add(dia->dia, _("Cancel"), NULL, _e_color_dialog_button2_click, dia);
69 elm_win_center(dia->dia->win, 1, 1);
70
71 dia->dia->data = dia;
72 e_object_del_attach_func_set(E_OBJECT(dia->dia), _e_color_dialog_dia_del);
73
74 return dia;
75 }
76
77 void
e_color_dialog_show(E_Color_Dialog * dia)78 e_color_dialog_show(E_Color_Dialog *dia)
79 {
80 e_dialog_show(dia->dia);
81 e_dialog_border_icon_set(dia->dia, "enlightenment/colors");
82 evas_object_size_hint_min_set(dia->dia->win, 250, -1);
83 }
84
85 void
e_color_dialog_title_set(E_Color_Dialog * dia,const char * title)86 e_color_dialog_title_set(E_Color_Dialog *dia, const char *title)
87 {
88 e_dialog_title_set(dia->dia, title);
89 }
90
91 void
e_color_dialog_select_callback_set(E_Color_Dialog * dia,void (* func)(E_Color_Dialog * dia,E_Color * color,void * data),void * data)92 e_color_dialog_select_callback_set(E_Color_Dialog *dia, void (*func)(E_Color_Dialog *dia, E_Color *color, void *data), void *data)
93 {
94 dia->select_func = func;
95 dia->select_data = data;
96 }
97
98 void
e_color_dialog_cancel_callback_set(E_Color_Dialog * dia,void (* func)(E_Color_Dialog * dia,E_Color * color,void * data),void * data)99 e_color_dialog_cancel_callback_set(E_Color_Dialog *dia, void (*func)(E_Color_Dialog *dia, E_Color *color, void *data), void *data)
100 {
101 dia->cancel_func = func;
102 dia->cancel_data = data;
103 }
104
105 E_API void
e_color_dialog_change_callback_set(E_Color_Dialog * dia,void (* func)(E_Color_Dialog * dia,E_Color * color,void * data),void * data)106 e_color_dialog_change_callback_set(E_Color_Dialog *dia, void (*func)(E_Color_Dialog *dia, E_Color *color, void *data), void *data)
107 {
108 dia->change_func = func;
109 dia->change_data = data;
110 }
111
112 static void
_e_color_dialog_cb_csel_change(void * data,Evas_Object * obj,void * ev EINA_UNUSED)113 _e_color_dialog_cb_csel_change(void *data, Evas_Object *obj, void *ev EINA_UNUSED)
114 {
115 E_Color_Dialog *dia = data;
116 Evas_Object *re;
117 int r, g, b;
118
119 elm_colorselector_color_get(obj, &dia->color->r, &dia->color->g, &dia->color->b, &dia->color->a);
120 r = dia->color->r;
121 g = dia->color->g;
122 b = dia->color->b;
123 re = evas_object_data_get(obj, "rect");
124 evas_color_argb_premul(dia->color->a, &r, &g, &b);
125 evas_object_color_set(re, r, g, b, dia->color->a);
126 if (dia->change_func)
127 dia->change_func(dia, dia->color, dia->change_data);
128 }
129
130 static void
_e_color_dialog_button1_click(void * data,E_Dialog * edia EINA_UNUSED)131 _e_color_dialog_button1_click(void *data, E_Dialog *edia EINA_UNUSED)
132 {
133 E_Color_Dialog *dia;
134
135 dia = data;
136 if (dia->select_func)
137 dia->select_func(dia, dia->color, dia->select_data);
138 _e_color_dialog_free(dia);
139 }
140
141 static void
_e_color_dialog_button2_click(void * data,E_Dialog * edia EINA_UNUSED)142 _e_color_dialog_button2_click(void *data, E_Dialog *edia EINA_UNUSED)
143 {
144 E_Color_Dialog *dia;
145
146 dia = data;
147 if (dia->cancel_func)
148 dia->cancel_func(dia, dia->initial, dia->cancel_data);
149 _e_color_dialog_free(data);
150 }
151
152 static void
_e_color_dialog_free(E_Color_Dialog * dia)153 _e_color_dialog_free(E_Color_Dialog *dia)
154 {
155 if (dia->dia)
156 {
157 e_object_del_attach_func_set(E_OBJECT(dia->dia), NULL);
158 e_object_del(E_OBJECT(dia->dia));
159 dia->dia = NULL;
160 }
161 E_FREE(dia->color);
162 E_FREE(dia);
163 }
164
165 static void
_e_color_dialog_dia_del(void * obj)166 _e_color_dialog_dia_del(void *obj)
167 {
168 E_Dialog *dia = obj;
169 E_Color_Dialog *cdia = dia->data;
170 if (cdia->cancel_func && cdia->initial)
171 cdia->cancel_func(cdia, cdia->initial, cdia->cancel_data);
172
173 cdia->dia = NULL;
174 e_object_del(E_OBJECT(cdia));
175 }
176
177