summaryrefslogtreecommitdiff
path: root/src/bin/options_colors.c
blob: 62b164069a2ba025faeb2d94ce0e073818ad9c0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "private.h"

#include <Elementary.h>
#include <assert.h>
#include "config.h"
#include "termio.h"
#include "options.h"
#include "options_colors.h"

static const char *mapping_names[] =
  {
     gettext_noop("Default"),
     gettext_noop("Black"),
     gettext_noop("Red"),
     gettext_noop("Green"),
     gettext_noop("Yellow"),
     gettext_noop("Blue"),
     gettext_noop("Magenta"),
     gettext_noop("Cyan"),
     gettext_noop("White"),
     gettext_noop("Invisible"),
     gettext_noop("Inverse"),
     gettext_noop("Inverse Background")
  };

typedef struct _Colors_Ctx {
     Elm_Object_Item *colitem[4][12];
     Evas_Object *colorsel;
     Elm_Object_Item *curitem;
     Evas_Object *colpal[4];
     Evas_Object *label;
     Evas_Object *reset;
     Config *config;
     Evas_Object *term;
     Evas_Object *bg;
} Colors_Ctx;

static void
_cb_op_color_item_sel(void *data,
                      Evas_Object *_obj EINA_UNUSED,
                      void *event)
{
   Colors_Ctx *ctx = data;
   Elm_Object_Item *it = event;
   int r = 0, g = 0, b = 0, a = 0;
   int i, j;

   if (ctx->curitem == it)
     return;
   if (ctx->curitem)
     elm_colorselector_palette_item_selected_set(ctx->curitem, EINA_FALSE);
   ctx->curitem = it;
   elm_colorselector_palette_item_color_get(it, &r, &g, &b, &a);
   elm_colorselector_color_set(ctx->colorsel, r, g, b, a);
   for (j = 0; j < 4; j++)
     {
        for (i = 0; i < 12; i++)
          {
             if (i == COL_INVIS)
               continue;
             if (ctx->colitem[j][i] == it)
               elm_object_text_set(ctx->label,
#if ENABLE_NLS
                                   gettext(mapping_names[i])
#else
                                   mapping_names[i]
#endif
                                   );
          }
     }
}

static void
_cb_op_color_chg(void *data,
                 Evas_Object *obj,
                 void *_event EINA_UNUSED)
{
   Colors_Ctx *ctx = data;
   Config *config = ctx->config;
   int r = 0, g = 0, b = 0, a = 0, rr = 0, gg = 0, bb = 0, aa = 0;
   int i, j;

   elm_colorselector_palette_item_color_get(ctx->curitem, &rr, &gg, &bb, &aa);
   elm_colorselector_color_get(obj, &r, &g, &b, &a);
   if ((r != rr) || (g != gg) || (b != bb) || (a != aa))
     {
        if (ctx->curitem)
          elm_colorselector_palette_item_color_set(ctx->curitem, r, g, b, a);
        elm_object_disabled_set(ctx->reset, EINA_FALSE);
        config->colors_use = EINA_TRUE;
        for (j = 0; j < 4; j++)
          {
             for (i = 0; i < 12; i++)
               {
                  if (i == COL_INVIS)
                    continue;
                  if (ctx->colitem[j][i] == ctx->curitem)
                    {
                       config->colors[(j * 12) + i].r = r * a / 256;
                       config->colors[(j * 12) + i].g = g * a / 256;
                       config->colors[(j * 12) + i].b = b * a / 256;
                       config->colors[(j * 12) + i].a = a;
                       termio_config_update(ctx->term);
                       config_save(config, NULL);
                       return;
                    }
               }
          }
     }
}

static void
_reset_config_colors(Colors_Ctx *ctx)
{
   int i, j;

   for (j = 0; j < 4; j++)
     {
        for (i = 0; i < 12; i++)
          {
             int r, g, b, a;
             unsigned char rr = 0, gg = 0, bb = 0, aa = 0;
             char buf[32];

             if (i == COL_INVIS)
               continue;

             snprintf(buf, sizeof(buf) - 1, "c%i", j * 12 + i);
             if (!edje_object_color_class_get(ctx->bg, buf,
                                              &r,
                                              &g,
                                              &b,
                                              &a,
                                              NULL, NULL, NULL, NULL,
                                              NULL, NULL, NULL, NULL))
               {
                  colors_standard_get(j, i, &rr, &gg, &bb, &aa);
               }
             else
               {
                  rr = r;
                  gg = g;
                  bb = b;
                  aa = a;
               }
             ctx->config->colors[(j * 12) + i].r = rr;
             ctx->config->colors[(j * 12) + i].g = gg;
             ctx->config->colors[(j * 12) + i].b = bb;
             ctx->config->colors[(j * 12) + i].a = aa;
          }
     }
}


static void
_cb_op_reset(void *data,
             Evas_Object *_obj EINA_UNUSED,
             void *_event EINA_UNUSED)
{
   Colors_Ctx *ctx = data;
   Evas_Object *term = ctx->term;
   Config *config = ctx->config;
   int r = 0, g = 0, b = 0, a = 0;
   int i, j;

   _reset_config_colors(ctx);
   for (j = 0; j < 4; j++)
     {
        for (i = 0; i < 12; i++)
          {
             if (i == COL_INVIS)
               continue;

             elm_colorselector_palette_item_color_set
               (ctx->colitem[j][i],
                config->colors[(j * 12) + i].r,
                config->colors[(j * 12) + i].g,
                config->colors[(j * 12) + i].b,
                config->colors[(j * 12) + i].a);
          }
     }
   elm_object_disabled_set(ctx->reset, EINA_TRUE);
   config->colors_use = EINA_FALSE;
   elm_colorselector_palette_item_color_get(ctx->curitem, &r, &g, &b, &a);
   elm_colorselector_color_set(ctx->colorsel, r, g, b, a);
   termio_config_update(term);
   config_save(config, NULL);
}

/* make color palettes wrap back. :) works with elm git. */
static void
_cb_op_scroller_resize(void *data,
                       Evas *_e EINA_UNUSED,
                       Evas_Object *_obj EINA_UNUSED,
                       void *_event EINA_UNUSED)
{
   Colors_Ctx *ctx = data;
   int i;

   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
options_colors(Evas_Object *opbox, Evas_Object *term, Evas_Object *bg)
{
   Config *config = termio_config_get(term);
   Evas_Object *o, *fr, *bx, *sc, *bx2, *bx3, *bx4;
   int i, j;
   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;
   ctx->bg = bg;

   fr = o = elm_frame_add(opbox);
   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);
   elm_object_text_set(o, _("Colors"));
   elm_box_pack_end(opbox, o);
   evas_object_show(o);

   evas_object_event_callback_add(fr, EVAS_CALLBACK_DEL,
                                  _parent_del_cb, ctx);

   bx = o = elm_box_add(opbox);
   elm_box_horizontal_set(o, EINA_TRUE);
   evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0);
   evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.0);
   elm_object_content_set(fr, o);
   evas_object_show(o);

   sc = o = elm_scroller_add(opbox);
   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);
   elm_box_pack_end(bx, o);
   evas_object_show(o);
   evas_object_event_callback_add(o, EVAS_CALLBACK_RESIZE,
                                  _cb_op_scroller_resize, ctx);

   bx3 = o = elm_box_add(opbox);
   evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0);
   evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.0);
   elm_object_content_set(sc, o);
   evas_object_show(o);

   if (!config->colors_use)
     {
        _reset_config_colors(ctx);
     }
   for (j = 0; j < 4; j++)
     {
        o = elm_label_add(opbox);
        if (j == 0)
          elm_object_text_set(o, _("Normal"));
        else if (j == 1)
          elm_object_text_set(o, _("Bright/Bold"));
        else if (j == 2)
          elm_object_text_set(o, _("Intense"));
        else if (j == 3)
          elm_object_text_set(o, _("Intense Bright/Bold"));
        evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0);
        evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.5);
        elm_box_pack_end(bx3, o);
        evas_object_show(o);

        ctx->colpal[j] = o = elm_colorselector_add(opbox);
        evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0);
        evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.5);
        elm_colorselector_mode_set(o, ELM_COLORSELECTOR_PALETTE);
        for (i = 0; i < 12; i++)
          {
             Elm_Object_Item *it;

             if (i == COL_INVIS)
               continue;

             it = elm_colorselector_palette_color_add
               (o,
                config->colors[(j * 12) + i].r,
                config->colors[(j * 12) + i].g,
                config->colors[(j * 12) + i].b,
                config->colors[(j * 12) + i].a);
             ctx->colitem[j][i] = it;
             if (i == 0 && j == 0)
               {
                  ctx->curitem = ctx->colitem[0][0];
                  elm_colorselector_palette_item_selected_set(ctx->curitem, EINA_TRUE);
               }
          }
        elm_box_pack_end(bx3, o);
        evas_object_show(o);
        evas_object_smart_callback_add(o, "color,item,selected",
                                       _cb_op_color_item_sel, ctx);
        if (j == 1)
          {
             o = elm_separator_add(opbox);
             evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0);
             evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.5);
             elm_separator_horizontal_set(o, EINA_TRUE);
             elm_box_pack_end(bx3, o);
             evas_object_show(o);
          }
     }

   bx2 = o = elm_box_add(opbox);
   evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0);
   evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.0);
   elm_box_pack_end(bx, o);
   evas_object_show(o);

   ctx->label = o = elm_label_add(opbox);
   elm_object_text_set(o,
#if ENABLE_NLS
                       gettext(mapping_names[0])
#else
                       mapping_names[0]
#endif
                       );
   evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0);
   evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.5);
   elm_box_pack_end(bx2, o);
   evas_object_show(o);

   ctx->colorsel = o = elm_colorselector_add(opbox);
   elm_colorselector_palette_item_color_get(ctx->curitem, &r, &g, &b, &a);
   elm_colorselector_color_set(o, r, g, b, a);
   elm_colorselector_mode_set(o, ELM_COLORSELECTOR_COMPONENTS);
   evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0);
   evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.5);
   elm_box_pack_end(bx2, o);
   evas_object_show(o);
   evas_object_smart_callback_add(o, "changed", _cb_op_color_chg, ctx);

   bx4 = o = elm_box_add(opbox);
   elm_box_horizontal_set(o, EINA_TRUE);
   evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0);
   evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.0);
   elm_box_pack_end(bx2, o);
   evas_object_show(o);

   ctx->reset = o = elm_button_add(opbox);
   elm_object_disabled_set(o, !config->colors_use);
   evas_object_size_hint_weight_set(o, 1.0, 0.0);
   evas_object_size_hint_align_set(o, 1.0, 0.5);
   elm_object_text_set(o, _("Reset all the colors"));
   elm_box_pack_end(bx4, o);
   evas_object_show(o);
   evas_object_smart_callback_add(o, "clicked", _cb_op_reset, ctx);
}