options_colors: be able to have multiple instances

This commit is contained in:
Boris Faure 2018-01-14 12:52:46 +01:00
parent 64502fa8f3
commit a66225dc87
1 changed files with 96 additions and 61 deletions

View File

@ -1,6 +1,7 @@
#include <Elementary.h>
#include "private.h" #include "private.h"
#include <Elementary.h>
#include <assert.h>
#include "config.h" #include "config.h"
#include "termio.h" #include "termio.h"
#include "options.h" #include "options.h"
@ -23,49 +24,59 @@ static const char *mapping_names[] =
gettext_noop("Inverse Base") gettext_noop("Inverse Base")
}; };
static Elm_Object_Item *colitem[4][11] = { { NULL } }; typedef struct _Colors_Ctx {
static Evas_Object *colorsel = NULL; Elm_Object_Item *colitem[4][11];
static Elm_Object_Item *curitem = NULL; Evas_Object *colorsel;
static Evas_Object *colpal[4] = { NULL }; Elm_Object_Item *curitem;
static Evas_Object *label = NULL, *reset = NULL; Evas_Object *colpal[4];
Evas_Object *label;
Evas_Object *reset;
Config *config;
Evas_Object *term;
} Colors_Ctx;
static void static void
_cb_op_use_custom_chg(void *data, _cb_op_use_custom_chg(void *data,
Evas_Object *obj, Evas_Object *obj,
void *_event EINA_UNUSED) void *_event EINA_UNUSED)
{ {
Evas_Object *term = data; Colors_Ctx *ctx = data;
Config *config = termio_config_get(term); Evas_Object *term = ctx->term;
Config *config = ctx->config;
Eina_Bool state = EINA_FALSE; Eina_Bool state = EINA_FALSE;
int i; int i;
state = elm_check_state_get(obj); state = elm_check_state_get(obj);
elm_object_disabled_set(colorsel, !state); elm_object_disabled_set(ctx->colorsel, !state);
for (i = 0; i < 4; i++) elm_object_disabled_set(colpal[i], !state);
elm_object_disabled_set(label, !state); for (i = 0; i < 4; i++)
elm_object_disabled_set(ctx->colpal[i], !state);
elm_object_disabled_set(ctx->label, !state);
config->colors_use = state; config->colors_use = state;
termio_config_update(term); termio_config_update(term);
config_save(config, NULL); config_save(config, NULL);
} }
static void static void
_cb_op_color_item_sel(void *_data EINA_UNUSED, _cb_op_color_item_sel(void *data,
Evas_Object *_obj EINA_UNUSED, Evas_Object *_obj EINA_UNUSED,
void *event) void *event)
{ {
Colors_Ctx *ctx = data;
Elm_Object_Item *it = event; Elm_Object_Item *it = event;
int r = 0, g = 0, b = 0, a = 0; int r = 0, g = 0, b = 0, a = 0;
int i, j; int i, j;
curitem = it; ctx->curitem = it;
elm_colorselector_palette_item_color_get(it, &r, &g, &b, &a); elm_colorselector_palette_item_color_get(it, &r, &g, &b, &a);
elm_colorselector_color_set(colorsel, r, g, b, a); elm_colorselector_color_set(ctx->colorsel, r, g, b, a);
for (j = 0; j < 4; j++) for (j = 0; j < 4; j++)
{ {
for (i = 0; i < 11; i++) for (i = 0; i < 11; i++)
{ {
if (colitem[j][i] == it) if (ctx->colitem[j][i] == it)
elm_object_text_set(label, elm_object_text_set(ctx->label,
#if HAVE_GETTEXT && ENABLE_NLS #if HAVE_GETTEXT && ENABLE_NLS
gettext(mapping_names[i]) gettext(mapping_names[i])
#else #else
@ -81,29 +92,29 @@ _cb_op_color_chg(void *data,
Evas_Object *obj, Evas_Object *obj,
void *_event EINA_UNUSED) void *_event EINA_UNUSED)
{ {
Evas_Object *term = data; Colors_Ctx *ctx = data;
Config *config = termio_config_get(term); Config *config = ctx->config;
int r = 0, g = 0, b = 0, a = 0, rr = 0, gg = 0, bb = 0, aa = 0; int r = 0, g = 0, b = 0, a = 0, rr = 0, gg = 0, bb = 0, aa = 0;
int i, j; int i, j;
elm_colorselector_palette_item_color_get(curitem, &rr, &gg, &bb, &aa); elm_colorselector_palette_item_color_get(ctx->curitem, &rr, &gg, &bb, &aa);
elm_colorselector_color_get(obj, &r, &g, &b, &a); elm_colorselector_color_get(obj, &r, &g, &b, &a);
if ((r != rr) || (g != gg) || (b != bb) || (a != aa)) if ((r != rr) || (g != gg) || (b != bb) || (a != aa))
{ {
if (curitem) if (ctx->curitem)
elm_colorselector_palette_item_color_set(curitem, r, g, b, a); elm_colorselector_palette_item_color_set(ctx->curitem, r, g, b, a);
elm_object_disabled_set(reset, EINA_FALSE); elm_object_disabled_set(ctx->reset, EINA_FALSE);
for (j = 0; j < 4; j++) for (j = 0; j < 4; j++)
{ {
for (i = 0; i < 11; i++) for (i = 0; i < 11; i++)
{ {
if (colitem[j][i] == curitem) if (ctx->colitem[j][i] == ctx->curitem)
{ {
config->colors[(j * 12) + mapping[i]].r = r; config->colors[(j * 12) + mapping[i]].r = r;
config->colors[(j * 12) + mapping[i]].g = g; config->colors[(j * 12) + mapping[i]].g = g;
config->colors[(j * 12) + mapping[i]].b = b; config->colors[(j * 12) + mapping[i]].b = b;
config->colors[(j * 12) + mapping[i]].a = a; config->colors[(j * 12) + mapping[i]].a = a;
termio_config_update(term); termio_config_update(ctx->term);
config_save(config, NULL); config_save(config, NULL);
return; return;
} }
@ -117,17 +128,18 @@ _cb_op_reset(void *data,
Evas_Object *_obj EINA_UNUSED, Evas_Object *_obj EINA_UNUSED,
void *_event EINA_UNUSED) void *_event EINA_UNUSED)
{ {
Evas_Object *term = data; Colors_Ctx *ctx = data;
Config *config = termio_config_get(term); Evas_Object *term = ctx->term;
Config *config = ctx->config;
int r = 0, g = 0, b = 0, a = 0; int r = 0, g = 0, b = 0, a = 0;
int i, j; int i, j;
for (j = 0; j < 4; j++) for (j = 0; j < 4; j++)
{ {
for (i = 0; i < 12; i++) for (i = 0; i < 12; i++)
{ {
unsigned char rr = 0, gg = 0, bb = 0, aa = 0; unsigned char rr = 0, gg = 0, bb = 0, aa = 0;
colors_standard_get(j, i, &rr, &gg, &bb, &aa); colors_standard_get(j, i, &rr, &gg, &bb, &aa);
config->colors[(j * 12) + i].r = rr; config->colors[(j * 12) + i].r = rr;
config->colors[(j * 12) + i].g = gg; config->colors[(j * 12) + i].g = gg;
@ -137,30 +149,43 @@ _cb_op_reset(void *data,
for (i = 0; i < 11; i++) for (i = 0; i < 11; i++)
{ {
elm_colorselector_palette_item_color_set elm_colorselector_palette_item_color_set
(colitem[j][i], (ctx->colitem[j][i],
config->colors[(j * 12) + mapping[i]].r, config->colors[(j * 12) + mapping[i]].r,
config->colors[(j * 12) + mapping[i]].g, config->colors[(j * 12) + mapping[i]].g,
config->colors[(j * 12) + mapping[i]].b, config->colors[(j * 12) + mapping[i]].b,
config->colors[(j * 12) + mapping[i]].a); config->colors[(j * 12) + mapping[i]].a);
} }
} }
elm_object_disabled_set(reset, EINA_TRUE); elm_object_disabled_set(ctx->reset, EINA_TRUE);
elm_colorselector_palette_item_color_get(curitem, &r, &g, &b, &a); elm_colorselector_palette_item_color_get(ctx->curitem, &r, &g, &b, &a);
elm_colorselector_color_set(colorsel, r, g, b, a); elm_colorselector_color_set(ctx->colorsel, r, g, b, a);
termio_config_update(term); termio_config_update(term);
config_save(config, NULL); config_save(config, NULL);
} }
/* make color palettes wrap back. :) works with elm git. */
static void static void
_cb_op_scroller_resize(void *_data EINA_UNUSED, _cb_op_scroller_resize(void *data,
Evas *_e EINA_UNUSED, Evas *_e EINA_UNUSED,
Evas_Object *_obj EINA_UNUSED, Evas_Object *_obj EINA_UNUSED,
void *_event EINA_UNUSED) void *_event EINA_UNUSED)
{ {
// make color palettes wrap back. :) works with elm git. Colors_Ctx *ctx = data;
int i; int i;
for (i = 0; i < 4; i++) evas_object_resize(colpal[i], 1, 1); for (i = 0; i < 4; i++)
evas_object_resize(ctx->colpal[i], 1, 1);
}
static void
_parent_del_cb(void *data,
Evas *_e EINA_UNUSED,
Evas_Object *_obj EINA_UNUSED,
void *_event_info EINA_UNUSED)
{
Colors_Ctx *ctx = data;
free(ctx);
} }
void void
@ -170,29 +195,39 @@ options_colors(Evas_Object *opbox, Evas_Object *term)
Evas_Object *o, *fr, *bx, *sc, *bx2, *bx3, *bx4; Evas_Object *o, *fr, *bx, *sc, *bx2, *bx3, *bx4;
int i, j; int i, j;
int r = 0, g = 0, b = 0, a = 0; int r = 0, g = 0, b = 0, a = 0;
Colors_Ctx *ctx;
ctx = calloc(1, sizeof(*ctx));
assert(ctx);
ctx->config = config;
ctx->term = term;
fr = o = elm_frame_add(opbox); fr = o = elm_frame_add(opbox);
evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(o, EVAS_HINT_FILL, EVAS_HINT_FILL); evas_object_size_hint_align_set(o, EVAS_HINT_FILL, EVAS_HINT_FILL);
elm_object_text_set(o, _("Colors")); elm_object_text_set(o, _("Colors"));
elm_box_pack_end(opbox, o); elm_box_pack_end(opbox, o);
evas_object_show(o); evas_object_show(o);
evas_object_event_callback_add(fr, EVAS_CALLBACK_DEL,
_parent_del_cb, ctx);
bx = o = elm_box_add(opbox); bx = o = elm_box_add(opbox);
elm_box_horizontal_set(o, EINA_TRUE); elm_box_horizontal_set(o, EINA_TRUE);
evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0); evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0);
evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.0); evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.0);
elm_object_content_set(fr, o); elm_object_content_set(fr, o);
evas_object_show(o); evas_object_show(o);
sc = o = elm_scroller_add(opbox); sc = o = elm_scroller_add(opbox);
evas_object_event_callback_add(o, EVAS_CALLBACK_RESIZE, evas_object_event_callback_add(o, EVAS_CALLBACK_RESIZE,
_cb_op_scroller_resize, NULL); _cb_op_scroller_resize, ctx);
evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(o, EVAS_HINT_FILL, EVAS_HINT_FILL); evas_object_size_hint_align_set(o, EVAS_HINT_FILL, EVAS_HINT_FILL);
elm_box_pack_end(bx, o); elm_box_pack_end(bx, o);
evas_object_show(o); evas_object_show(o);
bx3 = o = elm_box_add(opbox); bx3 = o = elm_box_add(opbox);
evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0); evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0);
evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.0); evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.0);
@ -210,25 +245,25 @@ options_colors(Evas_Object *opbox, Evas_Object *term)
evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.5); evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.5);
elm_box_pack_end(bx3, o); elm_box_pack_end(bx3, o);
evas_object_show(o); evas_object_show(o);
colpal[j] = o = elm_colorselector_add(opbox); ctx->colpal[j] = o = elm_colorselector_add(opbox);
evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0); evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0);
evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.5); evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.5);
elm_colorselector_mode_set(o, ELM_COLORSELECTOR_PALETTE); elm_colorselector_mode_set(o, ELM_COLORSELECTOR_PALETTE);
for (i = 0; i < 11; i++) for (i = 0; i < 11; i++)
{ {
Elm_Object_Item *it; Elm_Object_Item *it;
it = elm_colorselector_palette_color_add it = elm_colorselector_palette_color_add
(o, (o,
config->colors[(j * 12) + mapping[i]].r, config->colors[(j * 12) + mapping[i]].r,
config->colors[(j * 12) + mapping[i]].g, config->colors[(j * 12) + mapping[i]].g,
config->colors[(j * 12) + mapping[i]].b, config->colors[(j * 12) + mapping[i]].b,
config->colors[(j * 12) + mapping[i]].a); config->colors[(j * 12) + mapping[i]].a);
colitem[j][i] = it; ctx->colitem[j][i] = it;
} }
evas_object_smart_callback_add(o, "color,item,selected", evas_object_smart_callback_add(o, "color,item,selected",
_cb_op_color_item_sel, term); _cb_op_color_item_sel, ctx);
elm_box_pack_end(bx3, o); elm_box_pack_end(bx3, o);
evas_object_show(o); evas_object_show(o);
if (j == 1) if (j == 1)
@ -242,15 +277,15 @@ options_colors(Evas_Object *opbox, Evas_Object *term)
} }
} }
curitem = colitem[0][0]; ctx->curitem = ctx->colitem[0][0];
bx2 = o = elm_box_add(opbox); bx2 = o = elm_box_add(opbox);
evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0); evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0);
evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.0); evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.0);
elm_box_pack_end(bx, o); elm_box_pack_end(bx, o);
evas_object_show(o); evas_object_show(o);
label = o = elm_label_add(opbox); ctx->label = o = elm_label_add(opbox);
elm_object_text_set(o, elm_object_text_set(o,
#if HAVE_GETTEXT && ENABLE_NLS #if HAVE_GETTEXT && ENABLE_NLS
gettext(mapping_names[0]) gettext(mapping_names[0])
@ -262,16 +297,16 @@ options_colors(Evas_Object *opbox, Evas_Object *term)
evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.5); evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.5);
elm_box_pack_end(bx2, o); elm_box_pack_end(bx2, o);
evas_object_show(o); evas_object_show(o);
colorsel = o = elm_colorselector_add(opbox); ctx->colorsel = o = elm_colorselector_add(opbox);
elm_colorselector_palette_item_color_get(colitem[0][0], &r, &g, &b, &a); elm_colorselector_palette_item_color_get(ctx->colitem[0][0], &r, &g, &b, &a);
elm_colorselector_color_set(o, r, g, b, a); elm_colorselector_color_set(o, r, g, b, a);
elm_colorselector_mode_set(o, ELM_COLORSELECTOR_COMPONENTS); elm_colorselector_mode_set(o, ELM_COLORSELECTOR_COMPONENTS);
evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0); evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0);
evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.5); evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.5);
elm_box_pack_end(bx2, o); elm_box_pack_end(bx2, o);
evas_object_show(o); evas_object_show(o);
evas_object_smart_callback_add(o, "changed", _cb_op_color_chg, term); evas_object_smart_callback_add(o, "changed", _cb_op_color_chg, ctx);
bx4 = o = elm_box_add(opbox); bx4 = o = elm_box_add(opbox);
elm_box_horizontal_set(o, EINA_TRUE); elm_box_horizontal_set(o, EINA_TRUE);
@ -279,7 +314,7 @@ options_colors(Evas_Object *opbox, Evas_Object *term)
evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.0); evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.0);
elm_box_pack_end(bx2, o); elm_box_pack_end(bx2, o);
evas_object_show(o); evas_object_show(o);
o = elm_check_add(opbox); o = elm_check_add(opbox);
evas_object_size_hint_weight_set(o, 1.0, 0.0); evas_object_size_hint_weight_set(o, 1.0, 0.0);
evas_object_size_hint_align_set(o, 0.0, 0.5); evas_object_size_hint_align_set(o, 0.0, 0.5);
@ -287,14 +322,14 @@ options_colors(Evas_Object *opbox, Evas_Object *term)
elm_check_state_set(o, config->colors_use); elm_check_state_set(o, config->colors_use);
elm_box_pack_end(bx4, o); elm_box_pack_end(bx4, o);
evas_object_show(o); evas_object_show(o);
evas_object_smart_callback_add(o, "changed", _cb_op_use_custom_chg, term); evas_object_smart_callback_add(o, "changed", _cb_op_use_custom_chg, ctx);
reset = o = elm_button_add(opbox); ctx->reset = o = elm_button_add(opbox);
elm_object_disabled_set(o, EINA_TRUE); elm_object_disabled_set(o, EINA_TRUE);
evas_object_size_hint_weight_set(o, 1.0, 0.0); evas_object_size_hint_weight_set(o, 1.0, 0.0);
evas_object_size_hint_align_set(o, 1.0, 0.5); evas_object_size_hint_align_set(o, 1.0, 0.5);
elm_object_text_set(o, _("Reset")); elm_object_text_set(o, _("Reset"));
elm_box_pack_end(bx4, o); elm_box_pack_end(bx4, o);
evas_object_show(o); evas_object_show(o);
evas_object_smart_callback_add(o, "clicked", _cb_op_reset, term); evas_object_smart_callback_add(o, "clicked", _cb_op_reset, ctx);
} }