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);
93 // FIXME : add more codes
100 _sub_del(void *data, Evas_Object *obj, void *event_info)
102 Widget_Data *wd = elm_widget_data_get(obj);
103 //Evas_Object *sub = event_info;
109 _sizing_eval(Evas_Object *obj)
111 Widget_Data *wd = elm_widget_data_get(obj);
116 DBG("[%s]\n", __func__);
118 _colorpalette_object_move(obj, NULL, obj, NULL);
119 _colorpalette_object_resize(obj, NULL, obj, NULL);
123 static void _colorpalette_object_move(void *data, Evas *e, Evas_Object *obj, void *event_info)
133 wd = elm_widget_data_get((Evas_Object *)data);
138 evas_object_geometry_get(wd->lay, &x, &y, NULL, NULL);
143 evas_object_move(wd->lay, x, y);
147 static void _colorpalette_object_resize(void *data, Evas *e, Evas_Object *obj, void *event_info)
152 Colorpalette_Item *item;
154 Evas_Coord tab_w, tab_h;
160 wd = elm_widget_data_get((Evas_Object *)data);
165 evas_object_geometry_get(wd->lay, NULL, NULL, &w, &h);
169 evas_object_geometry_get(wd->tab, NULL, NULL, &tab_w, &tab_h);
170 if (tab_w > 0 && tab_h > 0) {
176 item = wd->items->data;
178 edje_object_part_geometry_get(elm_layout_edje_get(item->lo),"bg" ,NULL, NULL, &wd->rect_w, &wd->rect_h);
180 pad_x = ((double)wd->tab_w - (double)wd->rect_w * (double)wd->col) / (double)(wd->col - 1);
181 pad_y = ((double)wd->tab_h - (double)wd->rect_h * (double)wd->row) / (double)(wd->row - 1);
188 elm_table_padding_set(wd->tab, (int)pad_x , (int)pad_y);
193 evas_object_resize(wd->lay, w, h);
197 static void _colorpalette_object_show(void *data, Evas *e, Evas_Object *obj, void *event_info)
201 Widget_Data *wd = NULL;
206 wd = elm_widget_data_get((Evas_Object *)data);
213 evas_object_show(wd->lay);
217 static void _colorpalette_object_hide(void *data, Evas *e, Evas_Object *obj, void *event_info)
221 Widget_Data *wd = NULL;
226 wd = elm_widget_data_get((Evas_Object *)data);
232 evas_object_hide(wd->lay);
236 static void _color_select_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
238 Colorpalette_Item *item = (Colorpalette_Item *) data;
239 Elm_Colorpalette_Color *color;
241 color = ELM_NEW(Elm_Colorpalette_Color);
247 evas_object_smart_callback_call(item->parent, "clicked", color);
249 edje_object_signal_emit(elm_layout_edje_get(item->lo), "focus_visible", "elm");
253 static void _color_release_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
255 Colorpalette_Item *item = (Colorpalette_Item *) data;
256 edje_object_signal_emit(elm_layout_edje_get(item->lo), "focus_invisible", "elm");
260 static void _color_table_delete(Evas_Object *obj)
262 Widget_Data *wd = NULL;
263 Colorpalette_Item *item;
264 wd = elm_widget_data_get(obj);
269 EINA_LIST_FREE(wd->items, item) {
271 evas_object_del(item->lo);
275 evas_object_del(item->cr);
283 edje_object_part_unswallow(wd->lay, wd->tab);
284 evas_object_del(wd->tab);
289 static void _color_table_update(Evas_Object *obj, int row, int col, int color_num, Elm_Colorpalette_Color *color)
291 Widget_Data *wd = elm_widget_data_get(obj);
292 Colorpalette_Item *item;
303 e = evas_object_evas_get(wd->parent);
305 _color_table_delete(obj);
311 wd->tab = elm_table_add(obj);
313 evas_object_size_hint_weight_set(wd->tab, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
314 evas_object_size_hint_align_set(wd->tab, EVAS_HINT_FILL, EVAS_HINT_FILL);
316 edje_object_part_swallow(wd->lay, "palette", wd->tab);
317 evas_object_show(wd->tab);
319 for ( i = 0 ; i < row ; i++) {
320 for ( j = 0 ; j < col ; j++ ) {
321 item = ELM_NEW(Colorpalette_Item);
323 lo = elm_layout_add(obj);
324 elm_layout_theme_set(lo, "colorpalette", "base", "bg");
325 evas_object_size_hint_weight_set(lo, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
326 evas_object_size_hint_align_set(lo, EVAS_HINT_FILL, EVAS_HINT_FILL);
327 evas_object_show(lo);
328 elm_table_pack(wd->tab, lo, j, i, 1, 1);
333 if (count < color_num){
334 cr = edje_object_add(e);
335 _elm_theme_object_set(obj, cr, "colorpalette", "base", "color");
336 evas_object_color_set(cr, color[count].r, color[count].g, color[count].b, 255);
337 evas_object_size_hint_weight_set(cr, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
338 evas_object_size_hint_align_set(cr, EVAS_HINT_FILL, EVAS_HINT_FILL);
340 evas_object_event_callback_add(cr, EVAS_CALLBACK_MOUSE_DOWN, _color_select_cb, item);
341 evas_object_event_callback_add(cr, EVAS_CALLBACK_MOUSE_UP, _color_release_cb, item);
343 evas_object_show(cr);
344 edje_object_part_swallow(elm_layout_edje_get(lo), "color_rect", cr);
347 item->r = color[count].r;
348 item->g = color[count].g;
349 item->b = color[count].b;
351 wd->items = eina_list_append(wd->items, item);
360 * Add a new colorpalette to the parent.
362 * @param parent The parent object
363 * @return The new object or NULL if it cannot be created
365 * @ingroup Colorpalette
367 EAPI Evas_Object *elm_colorpalette_add(Evas_Object *parent)
369 Evas_Object *obj = NULL;
370 Widget_Data *wd = NULL;
373 e = evas_object_evas_get(parent);
374 if (e == NULL) return NULL;
375 wd = ELM_NEW(Widget_Data);
376 obj = elm_widget_add(e);
377 elm_widget_type_set(obj, "colorpalette");
378 elm_widget_sub_object_add(parent, obj);
379 elm_widget_data_set(obj, wd);
380 elm_widget_del_hook_set(obj, _del_hook);
381 elm_widget_theme_hook_set(obj, _theme_hook);
384 /* load background edj */
385 wd->lay = edje_object_add(e);
386 _elm_theme_object_set(obj, wd->lay, "colorpalette", "bg", "default");
388 evas_object_size_hint_weight_set(wd->lay, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
389 if(wd->lay == NULL) {
390 printf("Cannot load bg edj\n");
394 wd->color = (Elm_Colorpalette_Color*) calloc (10, sizeof(Elm_Colorpalette_Color));
396 wd->color[0].r = 55; wd->color[0].g = 90; wd->color[0].b = 18;
397 wd->color[1].r = 255; wd->color[1].g = 213; wd->color[1].b = 0;
398 wd->color[2].r = 146; wd->color[2].g = 255; wd->color[2].b = 11;
399 wd->color[3].r = 9; wd->color[3].g = 186; wd->color[3].b = 10;
400 wd->color[4].r = 86; wd->color[4].g = 201; wd->color[4].b = 242;
401 wd->color[5].r = 18; wd->color[5].g = 83; wd->color[5].b = 128;
402 wd->color[6].r = 140; wd->color[6].g = 53; wd->color[6].b = 238;
403 wd->color[7].r = 255; wd->color[7].g = 145; wd->color[7].b = 145;
404 wd->color[8].r = 255; wd->color[8].g = 59; wd->color[8].b = 119;
405 wd->color[9].r = 133; wd->color[9].g = 100; wd->color[9].b = 69;
407 _color_table_update(obj, 2, 5, 10, wd->color);
409 elm_widget_resize_object_set(obj, wd->lay);
410 // evas_object_smart_member_add(wd->lay, obj);
411 evas_object_event_callback_add(wd->lay, EVAS_CALLBACK_RESIZE, _colorpalette_object_resize, obj);
412 evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _colorpalette_object_move, obj);
413 evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW, _colorpalette_object_show, obj);
414 evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE, _colorpalette_object_hide, obj);
417 // evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
418 // _sizing_eval(obj);
425 * Set colors to the colorpalette.
427 * @param obj Colorpalette object
428 * @param color_num number of the colors on the colorpalette
429 * @param color Color lists
431 * @ingroup Colorpalette
433 EAPI void elm_colorpalette_color_set(Evas_Object *obj, int color_num, Elm_Colorpalette_Color *color)
435 Widget_Data *wd = elm_widget_data_get(obj);
438 if (color_num > MAX_NUM_COLORS) return;
447 wd->color = (Elm_Colorpalette_Color*) calloc (color_num, sizeof(Elm_Colorpalette_Color));
449 for ( i = 0 ; i < color_num ; i++) {
450 wd->color[i].r = color[i].r;
451 wd->color[i].g = color[i].g;
452 wd->color[i].b = color[i].b;
455 _color_table_update(obj, wd->row, wd->col, color_num, wd->color);
460 * Set row/column value for the colorpalette.
462 * @param obj Colorpalette object
463 * @param row row value for the colorpalette
464 * @param col column value for the colorpalette
466 * @ingroup Colorpalette
468 EAPI void elm_colorpalette_row_column_set(Evas_Object *obj, int row, int col)
470 Widget_Data *wd = elm_widget_data_get(obj);
474 _color_table_update(obj, row, col, wd->num, wd->color);