summaryrefslogtreecommitdiff
path: root/filtEr.c
blob: 7985d6c8dfd2989821b354ca9d2db792524c659e (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
#define EFL_EO_API_SUPPORT
#define EFL_BETA_API_SUPPORT
#include <Elementary.h>

#define WEIGHT evas_object_size_hint_weight_set
#define ALIGN evas_object_size_hint_align_set
#define EXPAND(X) WEIGHT((X), EVAS_HINT_EXPAND, EVAS_HINT_EXPAND)
#define FILL(X) ALIGN((X), EVAS_HINT_FILL, EVAS_HINT_FILL)

#define COLOR_CHANGED(F) (((F)->color.r > -1) || ((F)->color.g > -1) || ((F)->color.b > -1))

typedef enum
{
   FILTER_TYPE_BLEND, 
   FILTER_TYPE_BLUR, 
   FILTER_TYPE_GROW, 
   FILTER_TYPE_CURVE, 
   FILTER_TYPE_FILL, 
   FILTER_TYPE_MASK, 
   FILTER_TYPE_BUMP, 
   FILTER_TYPE_DISPLACE, 
   FILTER_TYPE_TRANSFORM,
   FILTER_TYPE_LAST
} Filter_Type;

static const char *group_names[] =
{
   [FILTER_TYPE_BLEND] = "blend",
   [FILTER_TYPE_BLUR] = "blur",
   [FILTER_TYPE_GROW] = "grow",
   [FILTER_TYPE_CURVE] = "curve",
   [FILTER_TYPE_FILL] = "fill",
   [FILTER_TYPE_MASK] = "mask",
   [FILTER_TYPE_BUMP] = "bump",
   [FILTER_TYPE_DISPLACE] = "displace",
   [FILTER_TYPE_TRANSFORM] = "transform",
   NULL
};

static const char *type_names[] =
{
   [FILTER_TYPE_BLEND] = "FILTER_TYPE_BLEND",
   [FILTER_TYPE_BLUR] = "FILTER_TYPE_BLUR",
   [FILTER_TYPE_GROW] = "FILTER_TYPE_GROW",
   [FILTER_TYPE_CURVE] = "FILTER_TYPE_CURVE",
   [FILTER_TYPE_FILL] = "FILTER_TYPE_FILL",
   [FILTER_TYPE_MASK] = "FILTER_TYPE_MASK",
   [FILTER_TYPE_BUMP] = "FILTER_TYPE_BUMP",
   [FILTER_TYPE_DISPLACE] = "FILTER_TYPE_DISPLACE",
   [FILTER_TYPE_TRANSFORM] = "FILTER_TYPE_TRANSFORM",
   NULL
};

typedef enum
{
   FILTER_FILL_MODE_NONE,
   FILTER_FILL_MODE_STRETCH_X,
   FILTER_FILL_MODE_STRETCH_Y,
   FILTER_FILL_MODE_REPEAT_X,
   FILTER_FILL_MODE_REPEAT_Y,
   FILTER_FILL_MODE_REPEAT_X_STRETCH_Y,
   FILTER_FILL_MODE_REPEAT_Y_STRETCH_X,
   FILTER_FILL_MODE_REPEAT_XY,
   FILTER_FILL_MODE_STRETCH_XY,
   FILTER_FILL_MODE_LAST
} Filter_Fill_Mode;

static const char *fillmode_names[] =
{
   [FILTER_FILL_MODE_NONE] = "FILLMODE_NONE",
   [FILTER_FILL_MODE_STRETCH_X] = "FILLMODE_STRETCH_X",
   [FILTER_FILL_MODE_STRETCH_Y] = "FILLMODE_STRETCH_Y",
   [FILTER_FILL_MODE_REPEAT_X] = "FILLMODE_REPEAT_X",
   [FILTER_FILL_MODE_REPEAT_Y] = "FILLMODE_REPEAT_Y",
   [FILTER_FILL_MODE_REPEAT_X_STRETCH_Y] = "FILLMODE_RX_SY",
   [FILTER_FILL_MODE_REPEAT_Y_STRETCH_X] = "FILLMODE_RY_SX",
   [FILTER_FILL_MODE_REPEAT_XY] = "FILLMODE_REPEAT_XY",
   [FILTER_FILL_MODE_STRETCH_XY] = "FILLMODE_STRETCH_XY"
};

static const char *fillmode_vals[] =
{
   [FILTER_FILL_MODE_NONE] = "none",
   [FILTER_FILL_MODE_STRETCH_X] = "stretch_x",
   [FILTER_FILL_MODE_STRETCH_Y] = "stretch_y",
   [FILTER_FILL_MODE_REPEAT_X] = "repeat_x",
   [FILTER_FILL_MODE_REPEAT_Y] = "repeat_y",
   [FILTER_FILL_MODE_REPEAT_X_STRETCH_Y] = "repeat_x_stretch_y",
   [FILTER_FILL_MODE_REPEAT_Y_STRETCH_X] = "repeat_y_stretch_x",
   [FILTER_FILL_MODE_REPEAT_XY] = "repeat_xy",
   [FILTER_FILL_MODE_STRETCH_XY] = "stretch_xy",
};

typedef enum
{
   BLUR_TYPE_DEFAULT,
   BLUR_TYPE_BOX,
   BLUR_TYPE_GAUSSIAN,
   BLUR_TYPE_LAST,
} Blur_Type;

static const char *blur_names[] =
{
   [BLUR_TYPE_DEFAULT] = "BLUR_DEFAULT",
   [BLUR_TYPE_BOX] = "BLUR_BOX",
   [BLUR_TYPE_GAUSSIAN] = "BLUR_GAUSSIAN",
};

static const char *blur_vals[] =
{
   [BLUR_TYPE_DEFAULT] = "default",
   [BLUR_TYPE_BOX] = "box",
   [BLUR_TYPE_GAUSSIAN] = "gaussian",
};

static Elm_Genlist_Item_Class filter_itc[FILTER_TYPE_LAST];

typedef struct Buffer
{
   EINA_INLIST;
   Eina_Stringshare *name;
   Elm_Object_Item *it;
   Eina_Inlist *filters;
   unsigned int filter_count;
   Eina_Bool hid : 1;
   Eina_Bool rgba : 1;
} Buffer;

typedef struct Color
{
   int r;
   int g;
   int b;
} Color;

typedef struct Filter
{
   EINA_INLIST;
   Filter_Type type;
   Buffer *b;
   Eina_Stringshare *name;
   Elm_Object_Item *it;
   Eina_Bool hid : 1;
   Color color;
   Filter_Fill_Mode fillmode;
   Evas_Point offset;
   union
   {
      struct
      {
         unsigned int radius;
         Eina_Bool smooth;
      } grow;
      struct
      {
         Evas_Point radii;
         Blur_Type type;
         unsigned int count;
         Evas_Object *cobj;
         Evas_Object *tobj;
      } blur;
   } data;
} Filter;

static Evas_Object *win;
static Evas_Object *list;
static Evas_Object *text;
static Eina_Inlist *buffers;
static unsigned int buffer_count;

static void
ctxpopup_hid(void *data EINA_UNUSED, Evas *e EINA_UNUSED, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   evas_object_del(obj);
}

static Evas_Object *
button_add(const char *icon, const char *text, const char *style, void *cb, void *data)
{
   Evas_Object *o, *ic;

   o = elm_button_add(win);
   if (style)
     elm_object_style_set(o, style);
   elm_object_focus_allow_set(o, EINA_FALSE);
   EXPAND(o);
   FILL(o);
   ic = elm_icon_add(win);
   elm_icon_order_lookup_set(ic, ELM_ICON_LOOKUP_THEME_FDO);
   elm_icon_standard_set(ic, icon);
   elm_object_part_content_set(o, "icon", ic);
   if (text)
     elm_object_text_set(o, text);
   evas_object_show(ic);
   evas_object_show(o);
   evas_object_smart_callback_add(o, "clicked", cb, data);
   return o;
}

static inline void
update_append(Eina_Strbuf *buf, int *n)
{
   if ((*n)++)
     eina_strbuf_append(buf, ", ");
}

static inline void
update_color(Eina_Strbuf *buf, Color *color, int *n)
{
   update_append(buf, n);
   eina_strbuf_append_printf(buf, "color = '#%.2x%.2x%.2x'", color->r, color->g, color->b);
}

static inline void
update_offset(Eina_Strbuf *buf, Evas_Point *offset, int *n)
{
   if (offset->x)
     {
        update_append(buf, n);
        eina_strbuf_append_printf(buf, "ox = %d", offset->x);
     }
   if (offset->y)
     {
        update_append(buf, n);
        eina_strbuf_append_printf(buf, "oy = %d", offset->y);
     }
}

static inline void
update_radii(Eina_Strbuf *buf, Evas_Point *offset, int *n)
{
   if (offset->x)
     {
        update_append(buf, n);
        eina_strbuf_append_printf(buf, "rx = %d", offset->x);
     }
   if (offset->y)
     {
        update_append(buf, n);
        eina_strbuf_append_printf(buf, "ry = %d", offset->y);
     }
}

static inline void
update_fillmode(Eina_Strbuf *buf, Filter_Fill_Mode fillmode, int *n)
{
   if (!fillmode) return;
   update_append(buf, n);
   eina_strbuf_append_printf(buf, "fillmode = %s", fillmode_vals[fillmode]);
}

static void
update()
{
   Eina_Strbuf *buf;
   Buffer *b;

   buf = eina_strbuf_new();
   EINA_INLIST_FOREACH(buffers, b)
     {
        Filter *f;

        if (!b->filters) continue;
        eina_strbuf_append_printf(buf, "%s = buffer('%s') ", b->name, b->rgba ? "rgba" : "alpha");
        EINA_INLIST_FOREACH(b->filters, f)
          {
             int n = 0;

             if (f->hid) continue;
             switch (f->type)
               {
                case FILTER_TYPE_BLEND:
                  eina_strbuf_append(buf, "blend (");
                  while (f->offset.x || f->offset.y || COLOR_CHANGED(f) || f->fillmode)
                    {
                       eina_strbuf_append_char(buf, '{');
                       update_offset(buf, &f->offset, &n);
                       if (COLOR_CHANGED(f))
                         update_color(buf, &f->color, &n);
                       update_fillmode(buf, f->fillmode, &n);
                       eina_strbuf_append_char(buf, '}');
                       break;
                    }
                  eina_strbuf_append(buf, ") ");
                  break;
                case FILTER_TYPE_BLUR:
                  eina_strbuf_append(buf, "blur ({");
                  update_radii(buf, &f->data.blur.radii, &n);
                  update_append(buf, &n);
                  eina_strbuf_append_printf(buf, "type = '%s'", blur_vals[f->data.blur.type]);
                  update_offset(buf, &f->offset, &n);
                  if (COLOR_CHANGED(f))
                    update_color(buf, &f->color, &n);
                  if (f->data.blur.type == BLUR_TYPE_BOX)
                    {
                       update_append(buf, &n);
                       eina_strbuf_append_printf(buf, "count = '%u'", f->data.blur.count);
                    }
                  eina_strbuf_append(buf, "})");
                  break;
                case FILTER_TYPE_GROW:
                  if (f->data.grow.radius)
                    eina_strbuf_append_printf(buf, "grow ({%u, smooth = %s, dst = %s}) ",
                      f->data.grow.radius,
                      f->data.grow.smooth ? "true" : "false",
                      f->b->name);
                  break;
                case FILTER_TYPE_CURVE:
                case FILTER_TYPE_FILL:
                case FILTER_TYPE_MASK:
                case FILTER_TYPE_BUMP:
                case FILTER_TYPE_DISPLACE:
                case FILTER_TYPE_TRANSFORM:
                default: break;
               }
          }
     }
   fprintf(stderr, "%s\n", eina_strbuf_string_get(buf));
   if (eina_strlen_bounded(eina_strbuf_string_get(buf), 2))
     eo_do(text, evas_obj_text_filter_program_set(eina_strbuf_string_get(buf)));
   eina_strbuf_free(buf);
}

static void
filter_del(Filter *f, Evas_Object *obj EINA_UNUSED)
{
   f->b->filters = eina_inlist_remove(f->b->filters, EINA_INLIST_GET(f));
   eina_stringshare_del(f->name);
   free(f);
   f->b->filter_count--;
}

static void
filter_toggle(Filter *f, ...)
{
    f->hid = !f->hid;
    if (f->hid)
      elm_object_item_signal_emit(f->it, "filter,hidden", "filter");
    else
      elm_object_item_signal_emit(f->it, "filter,visible", "filter");
    update();
}

static void
filter_offset_changed(int *coord, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   *coord = elm_spinner_value_get(obj);
   update();
}

static void
filter_color_changed(Filter *f, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   int r, g, b, a;

   elm_colorselector_color_get(obj, &r, &g, &b, &a);
   evas_color_argb_premul(a, &r, &g, &b);
   f->color.r = r;
   f->color.g = g;
   f->color.b = b;
   update();
}

static void
filter_fillmode_changed(Filter *f, Evas_Object *obj, Elm_Object_Item *it)
{
   long mode = (intptr_t)elm_object_item_data_get(it);

   f->fillmode = mode;
   elm_ctxpopup_dismiss(obj);
   update();
}

static void
filter_fillmode_pre(Filter *f, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Evas_Object *ctx;
   long i;

   ctx = elm_ctxpopup_add(win);
   evas_object_event_callback_add(ctx, EVAS_CALLBACK_HIDE, ctxpopup_hid, NULL);
   elm_ctxpopup_hover_parent_set(ctx, win);
   for (i = 0; i < FILTER_FILL_MODE_LAST; i++)
     {
        Elm_Object_Item *it;

        it = elm_ctxpopup_item_append(ctx, fillmode_names[i], NULL, (Evas_Smart_Cb)filter_fillmode_changed, f);
        elm_object_item_data_set(it, (void*)i);
     }
   evas_object_show(ctx);
}

static void
filter_blur_changed(Filter *f, Evas_Object *obj, Elm_Object_Item *it)
{
   long mode = (intptr_t)elm_object_item_data_get(it);

   f->data.blur.type = mode;
   elm_object_text_set(f->data.blur.tobj, blur_names[mode]);
   elm_object_disabled_set(f->data.blur.cobj, mode != BLUR_TYPE_BOX);
   elm_ctxpopup_dismiss(obj);
   update();
}

static void
filter_blur_pre(Filter *f, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Evas_Object *ctx;
   long i;

   ctx = elm_ctxpopup_add(win);
   evas_object_event_callback_add(ctx, EVAS_CALLBACK_HIDE, ctxpopup_hid, NULL);
   elm_ctxpopup_hover_parent_set(ctx, win);
   for (i = 0; i < BLUR_TYPE_LAST; i++)
     {
        Elm_Object_Item *it;

        it = elm_ctxpopup_item_append(ctx, blur_names[i], NULL, (Evas_Smart_Cb)filter_blur_changed, f);
        elm_object_item_data_set(it, (void*)i);
     }
   evas_object_show(ctx);
}

static void
filter_radius_changed(Filter *f, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   f->data.grow.radius = elm_slider_value_get(obj);
   update();
}

static void
buffer_colorspace_changed(Buffer *b, Evas_Object *obj, ...)
{
   b->rgba = !b->rgba;
   if (b->rgba)
     elm_object_text_set(obj, "RGBA");
   else
     elm_object_text_set(obj, "ALPHA");
   update();
}

static Evas_Object *
filter_spinners_create(const char *title, int *x, int *y)
{
   Evas_Object *box, *o;

   box = elm_box_add(win);
   elm_box_homogeneous_set(box, 1);
   EXPAND(box);
   FILL(box);
   evas_object_show(box);

   o = elm_spinner_add(win);
   elm_spinner_editable_set(o, 1);
   elm_spinner_label_format_set(o, "%1.0f px horizontal");
   elm_spinner_step_set(o, 1);
   elm_spinner_min_max_set(o, -25.0, 25.0);
   EXPAND(o);
   FILL(o);
   evas_object_smart_callback_add(o, "changed", (Evas_Smart_Cb)filter_offset_changed, x);
   elm_box_pack_end(box, o);
   evas_object_show(o);

   o = elm_spinner_add(win);
   elm_spinner_editable_set(o, 1);
   elm_spinner_label_format_set(o, "%1.0f px vertical");
   elm_spinner_step_set(o, 1);
   elm_spinner_min_max_set(o, -25.0, 25.0);
   EXPAND(o);
   FILL(o);
   evas_object_smart_callback_add(o, "changed", (Evas_Smart_Cb)filter_offset_changed, y);
   elm_box_pack_end(box, o);
   evas_object_show(o);

   o = elm_frame_add(win);
   elm_object_text_set(o, title);
   elm_object_content_set(o, box);

   return o;
}

static Evas_Object *
filter_content_get(Filter *f, Evas_Object *obj EINA_UNUSED, const char *part)
{
   Evas_Object *o;

   if (!strcmp(part, "filter.swallow.visibility"))
     {
        o = elm_icon_add(win);
        elm_icon_order_lookup_set(o, ELM_ICON_LOOKUP_THEME_FDO);
        elm_icon_standard_set(o, "visibility-on");
        evas_object_size_hint_min_set(o, 48, 48);
        evas_object_size_hint_max_set(o, 48, 48);
        return o;
     }
   if (!strcmp(part, "filter.swallow.smooth"))
     {
        o = elm_check_add(win);
        elm_check_state_pointer_set(o, &f->data.grow.smooth);
        elm_object_text_set(o, "Smooth");
        evas_object_smart_callback_add(o, "changed", (Evas_Smart_Cb)update, NULL);
        return o;
     }
   if (!strcmp(part, "filter.swallow.radius"))
     {
        o = elm_slider_add(win);
        elm_slider_horizontal_set(o, 1);
        elm_slider_min_max_set(o, 1, 50);
        elm_slider_unit_format_set(o, "%1.0f px");
        elm_slider_value_set(o, 1);
        evas_object_smart_callback_add(o, "changed", (Evas_Smart_Cb)filter_radius_changed, f);
        return o;
     }
   if (!strcmp(part, "filter.swallow.offsets"))
     return filter_spinners_create("Offsets", &f->offset.x, &f->offset.y);
   if (!strcmp(part, "filter.swallow.color"))
     {
        o = elm_colorselector_add(win);
        elm_colorselector_color_set(o, 0, 0, 160, 255);
        elm_colorselector_mode_set(o, ELM_COLORSELECTOR_COMPONENTS);
        evas_object_smart_callback_add(o, "changed", (Evas_Smart_Cb)filter_color_changed, f);
        return o;
     }
   if (!strcmp(part, "filter.swallow.fillmode"))
     return button_add(NULL, "FILLMODE_NONE", NULL, (Evas_Smart_Cb)filter_fillmode_pre, f);
   if (!strcmp(part, "filter.swallow.type"))
     return f->data.blur.tobj = button_add(NULL, "BLUR_DEFAULT", NULL, (Evas_Smart_Cb)filter_blur_pre, f);
   if (!strcmp(part, "filter.swallow.radii"))
     return filter_spinners_create("Radius", &f->data.blur.radii.x, &f->data.blur.radii.y);
   if (!strcmp(part, "filter.swallow.count"))
     {
        f->data.blur.cobj = o = elm_spinner_add(win);
        elm_spinner_editable_set(o, 1);
        elm_spinner_label_format_set(o, "%1.0f blurs");
        elm_spinner_step_set(o, 1);
        elm_spinner_min_max_set(o, 1, 25.0);
        evas_object_smart_callback_add(o, "changed", (Evas_Smart_Cb)filter_offset_changed, &f->data.blur.count);
        return o;
     }
   return NULL;
}

static char *
filter_text_get(Filter *f, Evas_Object *obj EINA_UNUSED, const char *part EINA_UNUSED)
{
   return strdup(f->name);
}

static void
filter_new(Buffer *b, Evas_Object *obj, Elm_Object_Item *it)
{
   char buf[32];
   Filter *f;
   long type = (intptr_t)elm_object_item_data_get(it);

   f = calloc(1, sizeof(Filter));
   f->b = b;
   f->type = type;
   snprintf(buf, sizeof(buf), "%s%d", group_names[f->type], f->b->filter_count++);
   f->name = eina_stringshare_add(buf);
   b->filters = eina_inlist_append(b->filters, EINA_INLIST_GET(f));
   f->color.r = f->color.g = f->color.b = -1;
   f->it = elm_genlist_item_append(list, &filter_itc[f->type], f, f->b->it, ELM_GENLIST_ITEM_NONE, NULL, NULL);
   elm_object_item_signal_callback_add(f->it, "filter,toggle", "filter", (Elm_Object_Item_Signal_Cb)filter_toggle, f);
   elm_ctxpopup_dismiss(obj);
}

static void
filter_new_pre(Buffer *b, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Evas_Object *ctx;
   long i;

   ctx = elm_ctxpopup_add(win);
   evas_object_event_callback_add(ctx, EVAS_CALLBACK_HIDE, ctxpopup_hid, NULL);
   elm_ctxpopup_hover_parent_set(ctx, win);
   for (i = 0; i < FILTER_TYPE_LAST; i++)
     {
        Elm_Object_Item *it;

        it = elm_ctxpopup_item_append(ctx, type_names[i], NULL, (Evas_Smart_Cb)filter_new, b);
        elm_object_item_data_set(it, (void*)i);
     }
   evas_object_show(ctx);
}

static Evas_Object *
buffer_content_get(Buffer *b, Evas_Object *obj EINA_UNUSED, const char *part)
{
   Evas_Object *o;

   if (!strcmp(part, "filter.swallow.visibility"))
     {
        o = elm_icon_add(win);
        elm_icon_order_lookup_set(o, ELM_ICON_LOOKUP_THEME_FDO);
        elm_icon_standard_set(o, "visibility-on");
        evas_object_size_hint_min_set(o, 48, 48);
        evas_object_size_hint_max_set(o, 48, 48);
        return o;
     }
   if (!strcmp(part, "filter.swallow.colorspace"))
     {
        return button_add(NULL, "ALPHA", NULL, (Evas_Smart_Cb)buffer_colorspace_changed, b);
     }
   o = button_add("list-add", NULL, NULL, filter_new_pre, b);
   evas_object_size_hint_min_set(o, 24, -1);
   return o;
}

static char *
buffer_text_get(Buffer *b, Evas_Object *obj EINA_UNUSED, const char *part EINA_UNUSED)
{
   return strdup(b->name);
}

static void
buffer_del(Buffer *b, Evas_Object *obj EINA_UNUSED)
{
   buffers = eina_inlist_remove(buffers, EINA_INLIST_GET(b));
   eina_stringshare_del(b->name);
   free(b);
   buffer_count--;
}

static void
buffer_toggle(Buffer *b, ...)
{
    b->hid = !b->hid;
    if (b->hid)
      elm_object_item_signal_emit(b->it, "filter,hidden", "filter");
    else
      elm_object_item_signal_emit(b->it, "filter,visible", "filter");
    update();
}

static void
buffer_new()
{
   static Elm_Genlist_Item_Class buffer_itc =
   {
      .item_style = "buffer",
      .func = {
           .content_get = (Elm_Genlist_Item_Content_Get_Cb)buffer_content_get,
           .text_get = (Elm_Genlist_Item_Text_Get_Cb)buffer_text_get,
           .del = (Elm_Genlist_Item_Del_Cb)buffer_del
      },
      .version = ELM_GENLIST_ITEM_CLASS_VERSION
   };
   char buf[32];
   Buffer *b;

   snprintf(buf, sizeof(buf), "buffer%d", buffer_count++);
   b = calloc(1, sizeof(Buffer));
   b->name = eina_stringshare_add(buf);
   b->it = elm_genlist_item_append(list, &buffer_itc, b, NULL, ELM_GENLIST_ITEM_GROUP, NULL, NULL);
   elm_object_item_signal_callback_add(b->it, "filter,toggle", "filter", (Elm_Object_Item_Signal_Cb)buffer_toggle, b);
   buffers = eina_inlist_append(buffers, EINA_INLIST_GET(b));
}

int
main(int argc, char *argv[])
{
   Evas_Object *box, *hbox, *bt, *scr;
   int i;

   for (i = 0; i < FILTER_TYPE_LAST; i++)
     {
        filter_itc[i].item_style = group_names[i];
        filter_itc[i].func.content_get = (Elm_Genlist_Item_Content_Get_Cb)filter_content_get;
        filter_itc[i].func.text_get = (Elm_Genlist_Item_Text_Get_Cb)filter_text_get;
        filter_itc[i].func.del = (Elm_Genlist_Item_Del_Cb)filter_del;
        filter_itc[i].version = ELM_GENLIST_ITEM_CLASS_VERSION;
     }

   elm_init(argc, (char**)argv);
   elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
   elm_theme_extension_add(NULL, "./filtEr.edj");

   win = elm_win_util_standard_add("filtEr", "Font Filters");
   evas_font_hinting_set(evas_object_evas_get(win), EVAS_FONT_HINTING_AUTO);
   elm_win_autodel_set(win, 1);


   hbox = elm_box_add(win);
   EXPAND(hbox);
   FILL(hbox);
   elm_box_horizontal_set(hbox, 1);
   elm_win_resize_object_add(win, hbox);
   evas_object_show(hbox);

   scr = elm_scroller_add(win);
   elm_scroller_bounce_set(scr, 0, 0);
   elm_scroller_policy_set(scr, ELM_SCROLLER_POLICY_AUTO, ELM_SCROLLER_POLICY_AUTO);
   EXPAND(scr);
   FILL(scr);
   elm_box_pack_end(hbox, scr);
   evas_object_show(scr);

   text = evas_object_text_add(evas_object_evas_get(win));
   evas_object_text_font_set(text, "Sans:style=bold", 24);
   evas_object_text_text_set(text, "click to change text!");
   evas_object_show(text);
   evas_object_size_hint_min_set(text, evas_object_text_horiz_advance_get(text), -1);
   elm_object_content_set(scr, text);

   box = elm_box_add(win);
   elm_box_pack_end(hbox, box);
   WEIGHT(box, 0.3, EVAS_HINT_EXPAND);
   FILL(box);
   evas_object_show(box);

   list = elm_genlist_add(win);
   EXPAND(list);
   FILL(list);
   elm_box_pack_end(box, list);
   elm_genlist_homogeneous_set(list, 0);
   evas_object_show(list);

   hbox = elm_box_add(win);
   WEIGHT(hbox, EVAS_HINT_EXPAND, 0);
   FILL(hbox);
   elm_box_pack_end(box, hbox);
   elm_box_homogeneous_set(hbox, 1);
   elm_box_horizontal_set(hbox, 1);
   bt = button_add("list-add", "Add buffer", NULL, buffer_new, NULL);
   elm_box_pack_end(hbox, bt);
   evas_object_show(hbox);

   evas_object_resize(win, 500, 400);
   evas_object_show(win);

   ecore_main_loop_begin();
   

   return 0;
}