summaryrefslogtreecommitdiff
path: root/src/bin/egui_gui/cbview.c
blob: 8e4871ecb0bdcfa0df0ed73a843abc69e4445843 (plain) (blame)
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

#include <Elementary.h>
#include <Eolian.h>

#include "ffi_glue.h"
#include "egui_log.h"
#include "props_helper.h"
#include "egui_logic_private.h"
#include "propview_data.h"
#include "cbview.h"

#include "elm_widget_container.h"

static Elm_Genlist_Item_Class *_itc_cb = NULL;
static Elm_Genlist_Item_Class *_itc_class = NULL;
static Eo *cb_list = NULL;
static Eo *propview_table = NULL;

/* Hash table to keep the list of expanded classes */
static Eina_Hash *expanded_classes = NULL;

static void
_callback_item_box_reassign(It_Data *it_data, Eo *box);

static void
_select_callback_item(Dep_Id *cb_dep_id)
{
    Elm_Object_Item *it = elm_genlist_first_item_get(cb_list);
    for (; it; it = elm_genlist_item_next_get(it))
      {
          if (elm_genlist_item_item_class_get(it) != _itc_cb)
            continue;

          It_Data *it_data = elm_object_item_data_get(it);
          if (cb_dep_id_get(it_data->cb) == cb_dep_id)
            {
               /* In case of Undo/Redo can happen, that Callback in Dep_data was changed,
                * but It_Data still holds the old one. So need to change CB in It_Data and update the box. */
               if (it_data->cb != cb_get(cb_dep_id))
                 {
                    it_data->cb = cb_get(cb_dep_id);
                    _callback_item_box_reassign(it_data, it_data->box);
                 }
               elm_genlist_item_selected_set(it, EINA_TRUE);
               elm_genlist_item_bring_in(it, ELM_GENLIST_ITEM_SCROLLTO_MIDDLE);
               break;
            }
      }
}

void
cbview_item_selected_set(Dep_Id *cb_dep_id)
{
   _select_callback_item(cb_dep_id);
}

static Eina_Bool
_cb_sequence_modify(void *data, Eo *obj, const Eo_Event_Description *desc EINA_UNUSED, void *event_info)
{
   It_Data *it_data = data;
   Gui_Widget_Callback *cb_old = it_data->cb, *new_cb;
   Gui_Widget *wdg = (Gui_Widget *) it_data->wdg;
   Gui_Context *ctx = (Gui_Context *) wdg_context_get(wdg);
   Dep_Id *prevseq = cb_sequence_id_get(cb_old);
   const char *seq_name = elm_object_item_part_text_get(event_info, NULL);
   Dep_Id *new_seq_id = gui_context_dep_id_by_name_get(_active_context_get(), seq_name);
   if (prevseq == new_seq_id) return EO_CALLBACK_CONTINUE;
   elm_object_text_set(obj, seq_name);

   Gui_Memento *memento;
   /* if !prevseq - cb is default field, so there is no real callback behind, so create new. */
   if (!prevseq)
     {
        new_cb = cb_new_full(ctx, cb_type_get(cb_old), new_seq_id);
        Callback_Container *cc_new = NULL, *cc_old = NULL;
        cc_old = (Callback_Container *) wdg_cb_container_get(wdg);
        if (!cc_old)
          {
             cc_new = cb_container_new();
          }
        else
          {
             cc_new = cb_container_copy(cc_old);
          }
        /* Add callback to container. */
        cb_container_cb_add(cc_new, new_cb);
        /* Save containers into memento*/
        memento = gui_memento_new(wdg_dep_id_get(wdg), MEMENTO_CALLBACK_ADD_DEL, cc_old, cc_new);
        /* Unset old container from widget */
        wdg_cb_container_unset(wdg);
        wdg_cb_container_set(wdg, cc_new);
     }
   else
     {
        /* When changing existing callback need to copy old one, in order to have the same old Dep_Id. */
        new_cb = cb_copy(cb_old);
        cb_modify(new_cb, cb_type_get(new_cb), new_seq_id);
        memento = gui_memento_new(wdg_dep_id_get(wdg), MEMENTO_CALLBACK, cb_old, new_cb);
        Dep_Data *dep_data = gui_context_dep_data_get(cb_dep_id_get(new_cb));
        dep_data_cb_set(dep_data, new_cb);
     }
   gui_context_memento_add(ctx, memento);
   Egui_Layout_Widgets *g = egui_layout_gui_get();
   elm_object_item_disabled_set(g->main_win->toolbar_redo_it, EINA_TRUE);
   elm_object_item_disabled_set(g->main_win->toolbar_undo_it, EINA_FALSE);

   cbview_build(it_data->wdg);
   _select_callback_item(cb_dep_id_get(new_cb));

   return EO_CALLBACK_CONTINUE;
}

static Evas_Object *
_callback_item_content_get(It_Data *it_data)
{
   Eo *box = eo_add(ELM_BOX_CLASS, propview_table);
   eo_do(box, evas_obj_visibility_set(EINA_TRUE),
         elm_obj_box_horizontal_set(EINA_TRUE),
         elm_obj_box_padding_set(5, 0),
         evas_obj_size_hint_weight_set(EVAS_HINT_EXPAND, 0),
         evas_obj_size_hint_align_set(EVAS_HINT_FILL, 0)
        );

   it_data->box = box;

   Gui_Widget_Callback *cb = it_data->cb;

   Eo *label = props_label_add(box, cb_type_get(cb));
   eo_do(label, evas_obj_size_hint_align_set(0, 0.5));
   eo_do(label, evas_obj_size_hint_weight_set(1, 0));

   Dep_Id *dep_id = cb_sequence_id_get(cb);
   const char *default_str = NULL;
   if (dep_id)

   default_str = dep_name_get(dep_id);
   Eo *seqs_list = props_hoversel_seqs_add(box, elm_widget_top_get(propview_table), wdg_context_get(it_data->wdg), default_str, EINA_FALSE);

   eo_do(seqs_list, evas_obj_size_hint_align_set(1, 0));
   eo_do(seqs_list, evas_obj_size_hint_weight_set(1, 0));
   eo_do(seqs_list, eo_event_callback_add(EVAS_SELECTABLE_INTERFACE_EVENT_SELECTED,
                                          _cb_sequence_modify, it_data));

   /* Add callbacks from resources. */
   Eina_List *res_lst = (Eina_List *) gui_context_resource_list_get(wdg_context_get(it_data->wdg), RESOURCE_EO_CALLBACK);
   Eina_List *itr;
   Dep_Id *res_id;
   EINA_LIST_FOREACH(res_lst, itr, res_id)
     {
        const Gui_Resource *res = gui_context_resource_get(res_id);
        if (res) elm_hoversel_item_add(seqs_list, resource_name_get(res), NULL, ELM_ICON_NONE, NULL, NULL);
     }

   return box;
}

static void
_callback_item_box_reassign(It_Data *it_data, Eo *box)
{
   Dep_Id *res_id, *seq_id;
   const Eina_List *itr, *list;

   it_data->box = box;
   const Gui_Context *ctx = wdg_context_get(it_data->wdg);

   Eina_List *contents = elm_box_children_get(box);

   Eo *label = eina_list_nth(contents, 0);
   Eo *seqs_list = eina_list_nth(contents, 1);

   eo_do(label, elm_obj_widget_part_text_set(NULL, cb_type_get(it_data->cb)));

   eo_do(seqs_list, eo_event_callback_add(EVAS_SELECTABLE_INTERFACE_EVENT_SELECTED,
                                          _cb_sequence_modify, it_data));

   eo_do(seqs_list, elm_obj_hoversel_clear());

   list = gui_context_seq_list_get(ctx);
   EINA_LIST_FOREACH(list, itr, seq_id)
     {
        Gui_Sequence *seq = sequence_get(seq_id);
        if (seq) eo_do(seqs_list, elm_obj_hoversel_item_add(sequence_name_get(seq), NULL, ELM_ICON_NONE, NULL, NULL));
     }

   list = gui_context_resource_list_get(ctx, RESOURCE_EO_CALLBACK);
   EINA_LIST_FOREACH(list, itr, res_id)
     {
        const Gui_Resource *res = gui_context_resource_get(res_id);
        if (res) eo_do(seqs_list, elm_obj_hoversel_item_add(resource_name_get(res), NULL, ELM_ICON_NONE, NULL, NULL));
     }

   Dep_Id *seqid = cb_sequence_id_get(it_data->cb);
   const char *txt = (seqid) ? dep_name_get(seqid) : PROP_HOVERSEL_NONE_VALUE;

   eo_do(seqs_list, elm_obj_widget_part_text_set(NULL, txt));

   eina_list_free(contents);
}

static void
_callbacks_classname_table_expand(const Gui_Widget *wdg, const char *classname, Elm_Object_Item *parent)
{
   const Eolian_Class *class = eolian_class_get_by_name(classname);
   Eina_Iterator *events = eolian_class_events_get(class);
   Eolian_Event *event;

   const char *filter = _filter_string_get();

   EINA_ITERATOR_FOREACH(events, event)
     {
        const char *event_name = eolian_event_name_get(event);

        // If filter is on and no match, dont add to property list
        if (filter && !_str_nocase_prefix(event_name, filter))
          continue;

        Eina_List *wgdcbs = wdg_cb_list_get(wdg);
        Eina_List *items = NULL;
        Eina_List *l = NULL;
        Gui_Widget_Callback *cb;

        Dep_Id *dep_id;
        EINA_LIST_FOREACH(wgdcbs, l, dep_id)
          {
             cb = cb_get(dep_id);
             if (!strcmp(event_name, cb_type_get(cb)))
               items = eina_list_append(items, cb);
          }

        cb = cb_new(event_name, 0);
        items = eina_list_append(items, cb);

        EINA_LIST_FOREACH(items, l, cb)
          {
             It_Data *it_data = IT_DATA_BUILD(CALLBACK_TYPE, wdg);
             cb_ref(cb);
             it_data->cb = cb;
             elm_genlist_item_append(cb_list, _itc_cb, it_data, parent,
                                     ELM_GENLIST_ITEM_NONE, NULL, NULL);
          }
        eina_list_free(items);
     }
   eina_iterator_free(events);
}

static void
_callbacks_expanded_cb(void *data EINA_UNUSED, Evas_Object *o EINA_UNUSED, void *event_info)
{
   Elm_Object_Item *glit = event_info;
   Classname_Data *cl = elm_object_item_data_get(glit);
   eina_hash_set(expanded_classes, cl->classname, (void *)1);
   _callbacks_classname_table_expand(cl->wdg, cl->classname, glit);
}

static void
_callbacks_contracted_cb(void *data EINA_UNUSED, Evas_Object *o EINA_UNUSED, void *event_info)
{
   Elm_Object_Item *glit = event_info;
   Classname_Data *cl = elm_object_item_data_get(glit);
   eina_hash_del_by_key(expanded_classes, cl->classname);
   elm_genlist_item_subitems_clear(glit);
}

static void
_callbacks_item_del(void *data, Evas_Object *obj EINA_UNUSED)
{
   It_Data *it_data = data;
   cb_unref(it_data->cb);

   // Make box cache ready. Remove the callback event.
   Eina_List *contents = elm_box_children_get(it_data->box);
   Eo *hoversel = eina_list_nth(contents, 1);
   eo_do(hoversel, eo_event_callback_del(EVAS_SELECTABLE_INTERFACE_EVENT_SELECTED,
                                         _cb_sequence_modify, it_data));
   eina_list_free(contents);

   _mempool_cache_data_put(CACHE_CALLBACKS, it_data->box);
   _it_data_free(it_data);
}

static Evas_Object *
_callbacks_item_content_get(void *data, Evas_Object *obj EINA_UNUSED, const char *part EINA_UNUSED)
{
   It_Data *it_data = data;

   // Genlist can call content get without calling delete first
   // so in that case just return what it already has.
   if (it_data->box) return it_data->box;

   Dep_Id *dep_id = cb_sequence_id_get(it_data->cb);

   Eo *it_box = _mempool_cache_data_get(CACHE_CALLBACKS);

   if (it_box)
     {
        _callback_item_box_reassign(it_data, it_box);
     }
   else
     {
        it_box = _callback_item_content_get(it_data);
        eo_ref(it_box);
     }

   if (dep_id)
     eo_do(it_box, evas_obj_color_set(255, 255, 255, 255));
   else
     eo_do(it_box, evas_obj_color_set(150, 150, 150, 255));

   return it_box;
}

void
cbview_build(const Gui_Widget *wdg)
{
   eo_do(cb_list, elm_obj_genlist_clear());

   Eina_List *classes = eo_class_mro_get(wdg_class_name_get(wdg));
   const char *classname;
   Elm_Object_Item *glit = NULL;

   const char *filter = _filter_string_get();

   EINA_LIST_FREE(classes, classname)
     {
        const Eolian_Class *class = eolian_class_get_by_name(classname);
        Eina_Iterator *events = eolian_class_events_get(class);
        if (events)
          {
             if (filter)
               {
                  _callbacks_classname_table_expand(wdg, classname, NULL);
               }
             else
               {
                  Classname_Data *cl = calloc(1, sizeof(Classname_Data));
                  cl->classname = strdup(classname);
                  cl->wdg = wdg;
                  glit = elm_genlist_item_append(cb_list, _itc_class, cl,
                                                 NULL, ELM_GENLIST_ITEM_TREE, NULL,
                                                 NULL);
                  if (eina_hash_find(expanded_classes, classname))
                    elm_genlist_item_expanded_set(glit, EINA_TRUE);
               }
          }
        eina_iterator_free(events);
     }
   //if filter search found nothing, yield an appropriate message to callbacks list
   if (filter && !elm_genlist_items_count(cb_list))
     elm_genlist_item_append(cb_list, _itc_class, NULL, NULL,
                             ELM_GENLIST_ITEM_NONE, NULL, NULL);
}

static void
_expand_request_cb(void *data EINA_UNUSED, Evas_Object *o EINA_UNUSED, void *event_info)
{
   Elm_Object_Item *glit = event_info;
   Classname_Data *cl = elm_object_item_data_get(glit);
   eina_hash_set(expanded_classes, cl->classname, (void *)1);
   elm_genlist_item_expanded_set(glit, EINA_TRUE);
}

static void
_contract_request_cb(void *data EINA_UNUSED, Evas_Object *o EINA_UNUSED, void *event_info)
{
   Elm_Object_Item *glit = event_info;
   Classname_Data *cl = elm_object_item_data_get(glit);
   eina_hash_del_by_key(expanded_classes, cl->classname);
   elm_genlist_item_expanded_set(glit, EINA_FALSE);
}

static void
_selected_callback_move(int dir)
{
   Elm_Object_Item *sel = elm_genlist_selected_item_get(cb_list);
   if ((sel) && (elm_genlist_item_item_class_get(sel) == _itc_cb))
     {
        It_Data *it_data = elm_object_item_data_get(sel);
        if (cb_sequence_id_get(it_data->cb))
          {
             Callback_Container *cc_new = NULL, *cc_old = NULL;
             Gui_Widget_Callback *cb = it_data->cb;
             const Gui_Widget *wdg = it_data->wdg;
             cc_old = (Callback_Container *) wdg_cb_container_get(wdg);
             cc_new = cb_container_copy(cc_old);
             if (cb_container_cb_move(cc_new, cb, dir))
               {
                  Gui_Memento *memento = gui_memento_new(wdg_dep_id_get(wdg), MEMENTO_CALLBACK_ADD_DEL, cc_old, cc_new);
                  gui_context_memento_add((Gui_Context *) wdg_context_get(wdg), memento);

                  wdg_cb_container_unset((Gui_Widget *) wdg);
                  wdg_cb_container_set((Gui_Widget *) wdg, cc_new);

                  Egui_Layout_Widgets *g = egui_layout_gui_get();
                  elm_object_item_disabled_set(g->main_win->toolbar_redo_it, EINA_TRUE);
                  elm_object_item_disabled_set(g->main_win->toolbar_undo_it, EINA_FALSE);

                  cbview_build(wdg);
                  _select_callback_item(cb_dep_id_get(cb));
               }
             /* If nothing wasn't move, delete Callback_Container */
             else
               {
                  cb_container_ref(cc_new);
                  cb_container_unref(cc_new);
               }
          }
     }
}

Eina_Bool
_callbacksview_up_btn_clicked(void *data EINA_UNUSED, Eo *obj EINA_UNUSED, const Eo_Event_Description *desc EINA_UNUSED, void *event_info EINA_UNUSED)
{
  _selected_callback_move(-1);
  return EO_CALLBACK_CONTINUE;
}

Eina_Bool
_callbacksview_down_btn_clicked(void *data EINA_UNUSED, Eo *obj EINA_UNUSED, const Eo_Event_Description *desc EINA_UNUSED, void *event_info EINA_UNUSED)
{
   _selected_callback_move(1);
   return EO_CALLBACK_CONTINUE;
}

Eina_Bool
_callbacksview_del_btn_clicked(void *data EINA_UNUSED, Eo *obj EINA_UNUSED, const Eo_Event_Description *desc EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Elm_Object_Item *sel = elm_genlist_selected_item_get(cb_list);
   if ((sel) && (elm_genlist_item_item_class_get(sel) == _itc_cb))
     {
        It_Data *it_data = elm_object_item_data_get(sel);
        if (cb_sequence_id_get(it_data->cb))
          {
             const Gui_Widget *wdg = it_data->wdg;
             Callback_Container *cc_new = NULL, *cc_old = NULL;
             cc_old = (Callback_Container *) wdg_cb_container_get(wdg);
             cc_new = cb_container_copy(cc_old);

             cb_container_cb_remove(cc_new, it_data->cb);
             elm_object_item_del(sel);

             Gui_Memento *memento = gui_memento_new(wdg_dep_id_get(wdg), MEMENTO_CALLBACK_ADD_DEL, cc_old, cc_new);
             gui_context_memento_add((Gui_Context *) wdg_context_get(wdg), memento);

             wdg_cb_container_unset((Gui_Widget *) wdg);
             wdg_cb_container_set((Gui_Widget *) wdg, cc_new);

             Egui_Layout_Widgets *g = egui_layout_gui_get();
             elm_object_item_disabled_set(g->main_win->toolbar_redo_it, EINA_TRUE);
             elm_object_item_disabled_set(g->main_win->toolbar_undo_it, EINA_FALSE);
          }
     }
   return EO_CALLBACK_CONTINUE;
}

static char *
_class_item_label_get(void *data, Evas_Object *obj EINA_UNUSED, const char *part EINA_UNUSED)
{
   Classname_Data *cl = data;
   if (cl) return strdup(cl->classname);
   else return strdup("No search results");
}

static void
_class_item_del(void *data, Evas_Object *obj EINA_UNUSED)
{
   Classname_Data *cl = data;
   free(cl->classname);
   free(cl);
}

void
cbview_init()
{
   Egui_Layout_Widgets *g = egui_layout_gui_get();
   if (!_itc_class)
     {
        _itc_class = elm_genlist_item_class_new();
        _itc_class->item_style = "default";
        _itc_class->func.text_get = _class_item_label_get;
        _itc_class->func.del = _class_item_del;
     }
   if (!_itc_cb)
     {
        _itc_cb = elm_genlist_item_class_new();
        _itc_cb->item_style = "full";
        _itc_cb->func.content_get = _callbacks_item_content_get;
        _itc_cb->func.del = _callbacks_item_del;
     }
   cb_list = g->main_win->callbacksview_list;
   propview_table = g->main_win->propsview_table;

   evas_object_smart_callback_add(cb_list, "expand,request", _expand_request_cb, NULL);
   evas_object_smart_callback_add(cb_list, "contract,request", _contract_request_cb, NULL);
   evas_object_smart_callback_add(cb_list, "expanded", _callbacks_expanded_cb, NULL);
   evas_object_smart_callback_add(cb_list, "contracted", _callbacks_contracted_cb, NULL);

   expanded_classes = eina_hash_string_superfast_new(NULL);
}

void
cbview_shutdown()
{
   eo_do(cb_list, elm_obj_genlist_clear());
   cb_list = NULL;

   elm_genlist_item_class_free(_itc_cb);
   _itc_cb = NULL;

   elm_genlist_item_class_free(_itc_class);
   _itc_class = NULL;

   eina_hash_free(expanded_classes);
}