1 #include <Elementary.h>
6 * @defgroup Colorpalette Colorpalette
9 * Using colorpalette, you can select a color by clicking
10 * a color rectangle on the colorpalette.
14 #define MAX_NUM_COLORS 30
16 typedef struct _Colorpalette_Item Colorpalette_Item;
17 struct _Colorpalette_Item
26 typedef struct _Widget_Data Widget_Data;
33 Evas_Coord x, y, w, h;
34 Evas_Coord tab_w, tab_h;
35 Evas_Coord rect_w, rect_h;
37 unsigned int row, col;
38 Elm_Colorpalette_Color *color;
45 static void _del_hook(Evas_Object *obj);
46 static void _theme_hook(Evas_Object *obj);
47 static void _sizing_eval(Evas_Object *obj);
48 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
51 static void _colorpalette_object_move(void *data, Evas *e, Evas_Object *obj, void *event_info);
52 static void _colorpalette_object_resize(void *data, Evas *e, Evas_Object *obj, void *event_info);
53 static void _colorpalette_object_show(void *data, Evas *e, Evas_Object *obj, void *event_info);
54 static void _colorpalette_object_hide(void *data, Evas *e, Evas_Object *obj, void *event_info);
56 static void _color_select_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
57 static void _color_release_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
58 static void _color_table_delete(Evas_Object *obj);
59 static void _color_table_update(Evas_Object *obj, int row, int col, int color_num, Elm_Colorpalette_Color *color);
62 _del_hook(Evas_Object *obj)
64 Widget_Data *wd = elm_widget_data_get(obj);
68 _color_table_delete(obj);
75 evas_object_smart_member_del(wd->lay);
76 evas_object_del(wd->lay);
83 _theme_hook(Evas_Object *obj)
85 Widget_Data *wd = elm_widget_data_get(obj);
89 _elm_theme_object_set(obj, wd->lay, "colorpalette", "bg", elm_widget_style_get(obj));
90 _color_table_update(obj, wd->row, wd->col, wd->num, wd->color);
97 _sub_del(void *data, Evas_Object *obj, void *event_info)
99 Widget_Data *wd = elm_widget_data_get(obj);
104 _sizing_eval(Evas_Object *obj)
106 Widget_Data *wd = elm_widget_data_get(obj);
111 _colorpalette_object_move(obj, NULL, obj, NULL);
112 _colorpalette_object_resize(obj, NULL, obj, NULL);
116 static void _colorpalette_object_move(void *data, Evas *e, Evas_Object *obj, void *event_info)
126 wd = elm_widget_data_get((Evas_Object *)data);
131 evas_object_geometry_get(wd->lay, &x, &y, NULL, NULL);
136 evas_object_move(wd->lay, x, y);
140 static void _colorpalette_object_resize(void *data, Evas *e, Evas_Object *obj, void *event_info)
143 Colorpalette_Item *item = NULL;
145 Evas_Coord tab_w, tab_h;
151 wd = elm_widget_data_get((Evas_Object *)data);
156 evas_object_geometry_get(wd->lay, NULL, NULL, &w, &h);
160 evas_object_geometry_get(wd->tab, NULL, NULL, &tab_w, &tab_h);
161 if (tab_w > 0 && tab_h > 0) {
167 item = wd->items->data;
169 edje_object_part_geometry_get(elm_layout_edje_get(item->lo),"bg" ,NULL, NULL, &wd->rect_w, &wd->rect_h);
171 pad_x = ((double)wd->tab_w - (double)wd->rect_w * (double)wd->col) / (double)(wd->col - 1);
172 pad_y = ((double)wd->tab_h - (double)wd->rect_h * (double)wd->row) / (double)(wd->row - 1);
179 elm_table_padding_set(wd->tab, (int)pad_x , (int)pad_y);
184 evas_object_resize(wd->lay, w, h);
188 static void _colorpalette_object_show(void *data, Evas *e, Evas_Object *obj, void *event_info)
192 Widget_Data *wd = NULL;
197 wd = elm_widget_data_get((Evas_Object *)data);
204 evas_object_show(wd->lay);
208 static void _colorpalette_object_hide(void *data, Evas *e, Evas_Object *obj, void *event_info)
212 Widget_Data *wd = NULL;
217 wd = elm_widget_data_get((Evas_Object *)data);
223 evas_object_hide(wd->lay);
227 static void _color_select_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
229 Colorpalette_Item *item = (Colorpalette_Item *) data;
230 Elm_Colorpalette_Color *color;
232 color = ELM_NEW(Elm_Colorpalette_Color);
238 evas_object_smart_callback_call(item->parent, "clicked", color);
240 edje_object_signal_emit(elm_layout_edje_get(item->lo), "focus_visible", "elm");
244 static void _color_release_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
246 Colorpalette_Item *item = (Colorpalette_Item *) data;
247 edje_object_signal_emit(elm_layout_edje_get(item->lo), "focus_invisible", "elm");
251 static void _color_table_delete(Evas_Object *obj)
253 Widget_Data *wd = NULL;
254 Colorpalette_Item *item;
255 wd = elm_widget_data_get(obj);
260 EINA_LIST_FREE(wd->items, item) {
262 evas_object_del(item->lo);
266 evas_object_del(item->cr);
274 edje_object_part_unswallow(wd->lay, wd->tab);
275 evas_object_del(wd->tab);
280 static void _color_table_update(Evas_Object *obj, int row, int col, int color_num, Elm_Colorpalette_Color *color)
282 Widget_Data *wd = elm_widget_data_get(obj);
283 Colorpalette_Item *item;
294 e = evas_object_evas_get(wd->parent);
296 _color_table_delete(obj);
302 wd->tab = elm_table_add(obj);
304 evas_object_size_hint_weight_set(wd->tab, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
305 evas_object_size_hint_align_set(wd->tab, EVAS_HINT_FILL, EVAS_HINT_FILL);
307 edje_object_part_swallow(wd->lay, "palette", wd->tab);
308 evas_object_show(wd->tab);
310 for ( i = 0 ; i < row ; i++) {
311 for ( j = 0 ; j < col ; j++ ) {
312 item = ELM_NEW(Colorpalette_Item);
314 lo = elm_layout_add(obj);
315 elm_layout_theme_set(lo, "colorpalette", "base", "bg");
316 evas_object_size_hint_weight_set(lo, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
317 evas_object_size_hint_align_set(lo, EVAS_HINT_FILL, EVAS_HINT_FILL);
318 evas_object_show(lo);
319 elm_table_pack(wd->tab, lo, j, i, 1, 1);
324 if (count < color_num){
325 cr = edje_object_add(e);
326 _elm_theme_object_set(obj, cr, "colorpalette", "base", "color");
327 evas_object_color_set(cr, color[count].r, color[count].g, color[count].b, 255);
328 evas_object_size_hint_weight_set(cr, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
329 evas_object_size_hint_align_set(cr, EVAS_HINT_FILL, EVAS_HINT_FILL);
331 evas_object_event_callback_add(cr, EVAS_CALLBACK_MOUSE_DOWN, _color_select_cb, item);
332 evas_object_event_callback_add(cr, EVAS_CALLBACK_MOUSE_UP, _color_release_cb, item);
334 evas_object_show(cr);
335 edje_object_part_swallow(elm_layout_edje_get(lo), "color_rect", cr);
338 item->r = color[count].r;
339 item->g = color[count].g;
340 item->b = color[count].b;
342 wd->items = eina_list_append(wd->items, item);
351 * Add a new colorpalette to the parent.
353 * @param parent The parent object
354 * @return The new object or NULL if it cannot be created
356 * @ingroup Colorpalette
358 EAPI Evas_Object *elm_colorpalette_add(Evas_Object *parent)
360 Evas_Object *obj = NULL;
361 Widget_Data *wd = NULL;
364 e = evas_object_evas_get(parent);
365 if (e == NULL) return NULL;
366 wd = ELM_NEW(Widget_Data);
367 obj = elm_widget_add(e);
368 elm_widget_type_set(obj, "colorpalette");
369 elm_widget_sub_object_add(parent, obj);
370 elm_widget_data_set(obj, wd);
371 elm_widget_del_hook_set(obj, _del_hook);
372 elm_widget_theme_hook_set(obj, _theme_hook);
375 wd->lay = edje_object_add(e);
376 _elm_theme_object_set(obj, wd->lay, "colorpalette", "bg", "default");
378 evas_object_size_hint_weight_set(wd->lay, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
379 if(wd->lay == NULL) {
380 printf("Cannot load bg edj\n");
384 wd->color = (Elm_Colorpalette_Color*) calloc (10, sizeof(Elm_Colorpalette_Color));
386 wd->color[0].r = 55; wd->color[0].g = 90; wd->color[0].b = 18;
387 wd->color[1].r = 255; wd->color[1].g = 213; wd->color[1].b = 0;
388 wd->color[2].r = 146; wd->color[2].g = 255; wd->color[2].b = 11;
389 wd->color[3].r = 9; wd->color[3].g = 186; wd->color[3].b = 10;
390 wd->color[4].r = 86; wd->color[4].g = 201; wd->color[4].b = 242;
391 wd->color[5].r = 18; wd->color[5].g = 83; wd->color[5].b = 128;
392 wd->color[6].r = 140; wd->color[6].g = 53; wd->color[6].b = 238;
393 wd->color[7].r = 255; wd->color[7].g = 145; wd->color[7].b = 145;
394 wd->color[8].r = 255; wd->color[8].g = 59; wd->color[8].b = 119;
395 wd->color[9].r = 133; wd->color[9].g = 100; wd->color[9].b = 69;
397 _color_table_update(obj, 2, 5, 10, wd->color);
399 elm_widget_resize_object_set(obj, wd->lay);
400 evas_object_event_callback_add(wd->lay, EVAS_CALLBACK_RESIZE, _colorpalette_object_resize, obj);
401 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _colorpalette_object_move, obj);
402 evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW, _colorpalette_object_show, obj);
403 evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE, _colorpalette_object_hide, obj);
410 * Set colors to the colorpalette.
412 * @param obj Colorpalette object
413 * @param color_num number of the colors on the colorpalette
414 * @param color Color lists
416 * @ingroup Colorpalette
418 EAPI void elm_colorpalette_color_set(Evas_Object *obj, int color_num, Elm_Colorpalette_Color *color)
420 Widget_Data *wd = elm_widget_data_get(obj);
423 if (color_num > MAX_NUM_COLORS) return;
432 wd->color = (Elm_Colorpalette_Color*) calloc (color_num, sizeof(Elm_Colorpalette_Color));
434 for ( i = 0 ; i < color_num ; i++) {
435 wd->color[i].r = color[i].r;
436 wd->color[i].g = color[i].g;
437 wd->color[i].b = color[i].b;
440 _color_table_update(obj, wd->row, wd->col, color_num, wd->color);
445 * Set row/column value for the colorpalette.
447 * @param obj Colorpalette object
448 * @param row row value for the colorpalette
449 * @param col column value for the colorpalette
451 * @ingroup Colorpalette
453 EAPI void elm_colorpalette_row_column_set(Evas_Object *obj, int row, int col)
455 Widget_Data *wd = elm_widget_data_get(obj);
459 _color_table_update(obj, row, col, wd->num, wd->color);
463 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/