summaryrefslogtreecommitdiff
path: root/src/bin/egui_gui/seqview.c
blob: 3e2bba7b0276058104e443fbe5e73c62147848d9 (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
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
#include <Elementary.h>
#include "egui_log.h"
#include "elm_widget.h"
#include "gui_widget.h"
#include "props_helper.h"
#include "egui_logic_private.h"
#include "seqview.h"

static Eo *lst = NULL;

static const Gui_Context *_active_ctx = NULL;

static Elm_Genlist_Item_Class *_seq_itc = NULL;
static Elm_Genlist_Item_Class *_action_itc = NULL;

static char *
_sequence_name_get(void *data, Evas_Object *obj EINA_UNUSED, const char *part EINA_UNUSED)
{
   Gui_Sequence *seq = data;
   if (seq) return strdup(sequence_name_get(seq));
   return NULL;
}

static Eo *
_empty_hoversel_add(Eo *box)
{
   Eo *hoversel = eo_add(ELM_HOVERSEL_CLASS, box);
   elm_hoversel_hover_parent_set(hoversel, elm_widget_top_get(lst));
   evas_object_size_hint_weight_set(hoversel, 0.0, 0.0);
   evas_object_size_hint_align_set(hoversel, 0.5, 0.5);
   eo_do(box, elm_obj_box_pack_end(hoversel));
   eo_do(hoversel, evas_obj_visibility_set(EINA_TRUE));
   return hoversel;
}

static void
_action_type_changed(void *data, Eo *obj EINA_UNUSED, void *event_info)
{
   Gui_Action *action = data;
   if (action)
     {
        action_reset(action);
        if (!action_sequence_get(action))
          {
             Elm_Object_Item *seq_it = elm_genlist_item_parent_get(action_data_get(action));
             Gui_Sequence *seq = elm_object_item_data_get(seq_it);
             action_sequence_set(action, seq);
          }
        const char *type_as_string = elm_object_item_part_text_get(event_info, NULL);
        if (!strcmp(type_as_string, "Creation"))
           action_creation_set(action, 0);
        if (!strcmp(type_as_string, "Modif"))
          {
             action_modif_set(action, 0, NULL);
          }
        if (!strcmp(type_as_string, "User"))
          {
             action_user_set(action, 0, NULL);
          }
        elm_genlist_item_update(action_data_get(action));
     }
}

static void
_action_add(void *data, Eo *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Gui_Action *action_before = data;
   Gui_Action *action = action_new_after(action_before);
   Elm_Object_Item *seq_it = sequence_data_get(action_sequence_get(action));
   Elm_Object_Item *it = elm_genlist_item_append(lst, _action_itc, action, seq_it,
         ELM_GENLIST_ITEM_NONE, NULL, NULL);
   action_data_set(action, it);
}

static void
_action_del(void *data, Eo *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Gui_Action *action = data;
   if (action)
     {
        Gui_Sequence *seq = action_sequence_get(action);
        if (seq)
          {
             Eina_List *actions_list = sequence_actions_list_get(seq);
             Elm_Object_Item *it = action_data_get(action);
             if (eina_list_count(actions_list) > 1)
               {
                  if (it) elm_object_item_del(it);
                  action_del(action);
               }
             else
               {
                  action_reset(action);
                  if (it) elm_genlist_item_update(it);
               }
          }
     }
}

static void
_action_wdg_selected(void *data, Eo *obj EINA_UNUSED, void *event_info)
{
   Gui_Action *action = data;
   Gui_Sequence *seq = action_sequence_get(action);
   const Gui_Context *ctx = sequence_ctx_get(seq);
   if (action)
     {
        const char *type_as_string = elm_object_item_part_text_get(event_info, NULL);
        Dep_Id *wdg_id = gui_context_dep_id_by_name_get(ctx, type_as_string);
        Action_Type act_type = action_type_get(action);

        if (act_type == CREATION_ACTION || act_type == MODIF_ACTION)
          {
             action_property_del(action);
             action_widget_id_set(action, wdg_id);
          }
        if (act_type == CREATION_ACTION)
          {
             /* By default, the window is created in the first action */
             Gui_Sequence *bigbang_seq = sequence_get(gui_context_dep_id_by_name_get(ctx, "BigBang"));
             if (seq != bigbang_seq)
               {
                  Eina_List *itr, *itr2;
                  Gui_Action *action2;
                  EINA_LIST_FOREACH_SAFE(sequence_actions_list_get(bigbang_seq), itr, itr2, action2)
                    {
                       if (action_type_get(action2) == CREATION_ACTION &&
                             (action_widget_id_get(action2) == wdg_id))
                          _action_del(action2, NULL, NULL);
                    }
               }
          }
        elm_genlist_item_update(action_data_get(action));
     }
}

static Eina_Bool
_action_prop_selected(void *data, Eo *obj EINA_UNUSED, const Eo_Event_Description *desc EINA_UNUSED, void *event_info)
{
   Gui_Action *action = data;
   if (action)
     {
        const char *prop_name = elm_object_item_part_text_get(event_info, NULL);
        char prop_namespace[PATH_MAX] = {'\0'};
        unsigned int elements_count = 0;
        char **_split_ret = NULL;

        /* Splitting property name, because it can be: Evas.Object.visibility*/
        _split_ret = eina_str_split_full(prop_name, CLASS_PROP_DELIMETER, 0, &elements_count);
        /* If elements_count > 1, split succeeded, so save prop_namespace and prop_name,
         * overwise property name only. */
        if (elements_count == 1)
          {
             prop_name = _split_ret[0];
          }
        else
          {
             const char *prop_name_tmp = _split_ret[elements_count - 1];
             strncpy(prop_namespace, prop_name, strstr(prop_name, prop_name_tmp) - prop_name - 1);
             prop_name = _split_ret[elements_count - 1];
          }

        Action_Type act_type = action_type_get(action);

        if (act_type == MODIF_ACTION)
          {
             Dep_Id *wdg_id = action_widget_id_get(action);

             Gui_Widget *wdg = wdg_id ? wdg_get(wdg_id) : NULL;
             Op_Desc *op = db_mro_op_desc_get(wdg_class_name_get(wdg), prop_namespace, prop_name);
             Gui_Widget_Property *prop = prop_create_for_op(op);
             action_property_set(action, prop);
          }
        elm_genlist_item_update(action_data_get(action));
        free(_split_ret[0]);
        free(_split_ret);
     }
   return EO_CALLBACK_CONTINUE;
}

static void
_action_prop_update(void *data, Eo *obj, void *event_info)
{
   unsigned int par_idx = (unsigned int) (uintptr_t) data;
   const Eo_Class *kl = eo_class_get(obj);
   Gui_Action *action;
   const Op_Desc *op;
   Gui_Widget_Property *prop;
   Gui_Value *val;

   /* Get data for changed field. */
   eo_do(obj, action = eo_key_data_get("editor"));

   prop = action_property_get(action);
   if (!prop)
     {
        ERR("no property");
        return;
     }
   op = prop_op_desc_get(prop);

   /* Get value which need to be changed.*/
   val = prop_value_nth_get(prop, par_idx);

   /* Get new value from edit field.
    * And update Gui_Value with new value. */
   Gui_Type type = db_op_desc_nth_par_type_get(op, par_idx);
   if (kl == ELM_ENTRY_CLASS)
     {
        const char *entry_data = elm_entry_entry_get(obj);
        if (type == GUI_TYPE_STRING)
          {
             /* FIXME: part_text_set can receive NULL as parameter.
              * But because of this, I can't write "null"*/
             gui_value_string_free(val);
             if (!strcmp(entry_data, "null"))
               {
                  gui_value_string_set(val, NULL);
               }
             else
               {
                  gui_value_string_set(val, strdup(entry_data));
               }
          }
        else if (type == GUI_TYPE_SINT)
          {
             int number = atoi(entry_data);
             gui_value_int_set(val, number);
          }
        else if (type == GUI_TYPE_DOUBLE)
          {
             double number = atof(entry_data);
             gui_value_double_set(val, number);
          }
     }
   if (kl == ELM_CHECK_CLASS)
     {
        Eina_Bool state;
        eo_do(obj, state = elm_obj_check_state_get());
        gui_value_bool_set(val, state);
     }
   if (kl == ELM_HOVERSEL_CLASS)
     {
        const char *new_enum_value = elm_object_item_part_text_get(event_info, NULL);
        /* Set selected string on hover */
        elm_object_text_set(obj, new_enum_value);
        /* Get new value depending on type of edit field.
         * And update value. */
        if (type == GUI_TYPE_ENUM)
          {
             gui_value_string_free(val);
             gui_value_enum_set(val, strdup(new_enum_value));
          }
        else if (type == GUI_TYPE_OBJECT)
          {
             Dep_Id *dep_id = DEP_ID_GET(val);
             Dep_Id *new_name_id = gui_context_dep_id_by_name_get(_active_ctx, new_enum_value);
             Gui_Widget *cur_param_wdg = wdg_get(dep_id);
             Gui_Widget *new_param_wdg = wdg_get(new_name_id);
             /* GUI_TYPE_OBJECT ops depend on other widgets, check that widget exists.
              * or new one not equal old one.*/
             if ((!new_param_wdg) || (cur_param_wdg == new_param_wdg))
               {
                  return;
               }
             gui_value_name_id_set(val, type, new_name_id);
          }
        else if (type == GUI_TYPE_FILE)
          {
             if (!strcmp(new_enum_value, PROP_HOVERSEL_NONE_VALUE))
               {
                  gui_value_name_id_set(val, type, 0);
               }
             else
               {
                  Dep_Id *new_id = gui_context_dep_id_by_name_get(_active_context_get(), new_enum_value);
                  gui_value_name_id_set(val, type, new_id);
               }
          }
     }
}

static Eina_Bool
_action_user_func_update(void *data, Eo *obj, const Eo_Event_Description *desc EINA_UNUSED, void *event_info)
{
   Gui_Action *action = data;
   const char *new_value = elm_object_item_part_text_get(event_info, NULL);
   elm_object_text_set(obj, new_value);
   Dep_Id *dep_id = gui_context_dep_id_by_name_get(_active_ctx, new_value);
   action_user_set(action, dep_id, action_user_data_get(action));
   return EO_CALLBACK_CONTINUE;
}

static void
_action_user_data_update(void *data, Eo *obj, void *event_info EINA_UNUSED)
{
   Gui_Action *action = data;
   const char *data_str = elm_entry_entry_get(obj);
   action_user_set(action, action_user_dep_id_get(action), data_str);
}

static Evas_Object *
_action_content_get(void *data, Evas_Object *obj EINA_UNUSED, const char *part EINA_UNUSED)
{
   Gui_Action *action = data;

   Eo *box = eo_add(ELM_BOX_CLASS, lst);
   eo_do(box, evas_obj_visibility_set(EINA_TRUE),
         elm_obj_box_horizontal_set(EINA_TRUE),
         evas_obj_size_hint_weight_set(EVAS_HINT_EXPAND, EVAS_HINT_EXPAND),
         evas_obj_size_hint_align_set(EVAS_HINT_FILL, EVAS_HINT_FILL),
         evas_obj_size_hint_min_set(32 * elm_config_scale_get(),
            18 * elm_config_scale_get())
        );

   Eo *hoversel = _empty_hoversel_add(box);
   elm_hoversel_item_add(hoversel, "Creation", NULL, ELM_ICON_NONE, NULL, NULL);
   elm_hoversel_item_add(hoversel, "Modif", NULL, ELM_ICON_NONE, NULL, NULL);
   elm_hoversel_item_add(hoversel, "User", NULL, ELM_ICON_NONE, NULL, NULL);
   evas_object_smart_callback_add(hoversel, "selected", _action_type_changed, action);

   if (action)
     {
        Action_Type act_type = action_type_get(action);
        switch (act_type)
          {
           case CREATION_ACTION:
              elm_object_text_set(hoversel, "Creation");
              break;
           case MODIF_ACTION:
              elm_object_text_set(hoversel, "Modif");
              break;
           case USER_ACTION:
              elm_object_text_set(hoversel, "User");
              break;
           default:
              elm_object_text_set(hoversel, "   ---   ");
              break;
          }

        Eo *wdgs_hover = NULL;
        if (act_type == CREATION_ACTION || act_type == MODIF_ACTION)
          {
             const char *wdg_name = "   ---   ";
             Dep_Id *wdg_id = action_widget_id_get(action);
             if (wdg_id)
               {
                  wdg_name = dep_name_get(wdg_id);
               }
             wdgs_hover = props_hoversel_objects_add(box, NULL, elm_widget_top_get(lst), wdg_name, gui_context_main_widgets_get(action_ctx_get(action)), NULL);
             evas_object_smart_callback_add(wdgs_hover, "selected", _action_wdg_selected, action);
          }
        if (act_type == MODIF_ACTION)
          {
             Dep_Id *wdg_id = action_widget_id_get(action);
             Gui_Widget *wdg = wdg_id ? wdg_get(wdg_id) : NULL;
             Gui_Widget_Property *prop = action_property_get(action);
             Eo *props_hover = props_hoversel_object_ops_add(box, elm_widget_top_get(lst), "   ---   ", wdg);
             eo_do(props_hover, eo_event_callback_add(EVAS_SELECTABLE_INTERFACE_EVENT_SELECTED, _action_prop_selected, action));

             if (prop)
               {
                  const Op_Desc *op = prop_op_desc_get(prop);
                  eo_do(props_hover, elm_obj_widget_part_text_set(NULL, db_op_desc_op_name_full_get(op)));

                  unsigned int count, i;
                  count = db_op_desc_param_number_get(op);

                  for (i = 0; i < count; i++)
                    {
                       void *pi = (void *) (uintptr_t) i;
                       Gui_Value *value = ((prop && prop_value_get(prop)) ? prop_value_nth_get(prop, i) : NULL);
                       Eo *new_obj = props_value_add(box, action_ctx_get(action), wdg, value, op, i);
                       if (new_obj)
                         {
                            eo_do(new_obj, eo_key_data_set("editor", action, NULL));
                            if (eo_isa(new_obj, ELM_HOVERSEL_CLASS))
                               evas_object_smart_callback_add(new_obj, "selected", _action_prop_update, pi);
                            else
                              {
                                 /*FIXME: decide how to handle CHANGE ON ACTIVATE*/
#if 0
                                 if (db_op_desc_flag_get(op) & CHANGE_ON_ACTIVATE)
                                   evas_object_smart_callback_add(new_obj, "activated", _action_prop_update, pi);
                                 else
#endif
                                   evas_object_smart_callback_add(new_obj, "changed", _action_prop_update, pi);
                              }
                         }
                       else
                          return EINA_FALSE;
                    }
               }
          }
        if (act_type == USER_ACTION)
          {
             const char *func_str = dep_name_get(action_user_dep_id_get(action));
             const char *data_str = action_user_data_get(action);

             /* Add callbacks from resources. */
             Eo *props_hover = props_hoversel_enums_add(box, elm_widget_top_get(lst), func_str, NULL);
             Eina_List *res_lst = (Eina_List *) gui_context_resource_list_get(action_ctx_get(action), 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);
                  elm_hoversel_item_add(props_hover, resource_name_get(res), NULL, ELM_ICON_NONE, NULL, NULL);
               }
             eo_do(props_hover, eo_event_callback_add(EVAS_SELECTABLE_INTERFACE_EVENT_SELECTED,
                                              _action_user_func_update, action));
             Eo *data_en = props_entry_add(box, data_str, GUI_TYPE_STRING);
             evas_object_smart_callback_add(data_en, "changed", _action_user_data_update, action);
          }
     }
   Eo *add_but = props_button_add(box, "+");
   evas_object_smart_callback_add(add_but, "clicked", _action_add, action);
   evas_object_size_hint_weight_set(add_but, 1.0, 1.0);
   evas_object_size_hint_align_set(add_but, 1.0, -1.0);
   Eo *del_but = props_button_add(box, "-");
   evas_object_smart_callback_add(del_but, "clicked", _action_del, action);
   return box;
}

static void
_expand_request_cb(void *data EINA_UNUSED, Evas_Object *o EINA_UNUSED, void *event_info)
{
   Elm_Object_Item *glit = event_info;
   elm_genlist_item_expanded_set(glit, EINA_TRUE);
}

static void
_expanded_cb(void *data EINA_UNUSED, Evas_Object *o EINA_UNUSED, void *event_info)
{
   Gui_Sequence *seq = elm_object_item_data_get(event_info);
   Eina_List *itr;
   Gui_Action *action;
   Eina_List *actions_list = sequence_actions_list_get(seq);
   if (actions_list)
     {
        EINA_LIST_FOREACH(actions_list, itr, action)
          {
             Elm_Object_Item *it = elm_genlist_item_append(lst, _action_itc, action, event_info,
                   ELM_GENLIST_ITEM_NONE, NULL, NULL);
             action_data_set(action, it);
          }
     }
   else
     {
        action = action_new(NULL);
        Elm_Object_Item *it = elm_genlist_item_append(lst, _action_itc, action, event_info,
              ELM_GENLIST_ITEM_NONE, NULL, NULL);
        action_data_set(action, it);
     }
}

static void
_contract_request_cb(void *data EINA_UNUSED, Evas_Object *o EINA_UNUSED, void *event_info)
{
   Elm_Object_Item *glit = event_info;
   elm_genlist_item_expanded_set(glit, EINA_FALSE);
}

static void
_contracted_cb(void *data EINA_UNUSED, Evas_Object *o EINA_UNUSED, void *event_info)
{
   Elm_Object_Item *glit = event_info;
   elm_genlist_item_subitems_clear(glit);
}

void
seqview_fill(const Gui_Context *ctx)
{
   elm_genlist_clear(lst);
   _active_ctx = ctx;

   if (!ctx) return;
   Eina_List *itr;
   Dep_Id *seq_id;
   EINA_LIST_FOREACH(gui_context_seq_list_get(ctx), itr, seq_id)
     {
        Gui_Sequence *seq =sequence_get(seq_id);
        Elm_Object_Item *it = elm_genlist_item_append(lst, _seq_itc, seq, NULL,
              ELM_GENLIST_ITEM_TREE, NULL, NULL);
        sequence_data_set(seq, it);
        elm_genlist_item_expanded_set(it, EINA_FALSE);
     }
}

Eina_Bool
_action_or_seq_del(void *data EINA_UNUSED, Eo *obj EINA_UNUSED, const Eo_Event_Description *desc EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Elm_Object_Item *item = elm_genlist_selected_item_get(lst);
   if (item)
     {
        const Elm_Genlist_Item_Class *itc = elm_genlist_item_item_class_get(item);
        if (itc == _seq_itc)
          {
             Gui_Sequence *seq = elm_object_item_data_get(item);
             elm_object_item_del(item);
             sequence_del(seq);
          }
        else
          {
             Gui_Action *action = elm_object_item_data_get(item);
             _action_del(action, NULL, NULL);
          }
     }
   return EINA_TRUE;
}

static void
_action_item_del(void *data, Eo *obj EINA_UNUSED)
{
   Gui_Action *action = data;
   action_data_set(action, NULL);
}

void
seqview_init(Eo *list)
{
   lst = list;

   if (!_seq_itc)
     {
        _seq_itc = elm_genlist_item_class_new();
        _seq_itc->item_style = "default";
        _seq_itc->func.text_get = _sequence_name_get;
        _seq_itc->func.content_get = NULL;
        _seq_itc->func.state_get = NULL;
        _seq_itc->func.del = NULL;
     }
   if (!_action_itc)
     {
        _action_itc = elm_genlist_item_class_new();
        _action_itc->item_style = "full";
        _action_itc->func.text_get = NULL;
        _action_itc->func.content_get = _action_content_get;
        _action_itc->func.state_get = NULL;
        _action_itc->func.del = _action_item_del;
     }
   evas_object_smart_callback_add(lst, "expand,request", _expand_request_cb, lst);
   evas_object_smart_callback_add(lst, "contract,request", _contract_request_cb, lst);
   evas_object_smart_callback_add(lst, "expanded", _expanded_cb, lst);
   evas_object_smart_callback_add(lst, "contracted", _contracted_cb, lst);
}

void
seqview_shutdown()
{
   elm_genlist_clear(lst);
   lst = NULL;
   if (_seq_itc) elm_genlist_item_class_free(_seq_itc);
   _seq_itc = NULL;
   if (_action_itc) elm_genlist_item_class_free(_action_itc);
   _action_itc = NULL;
}