summaryrefslogtreecommitdiff
path: root/src/bin/egui_gui/propview.c
blob: f1827d3c864dc7857b79b652dbd53b7b4ad99318 (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
#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.h"

#include "elm_widget_container.h"

extern Elm_Genlist_Item_Class *_itc_class;
extern Elm_Genlist_Item_Class *_itc_notfound;

static Elm_Genlist_Item_Class *_itc_prop = NULL;

static Eo *prop_list = NULL;

/*
 * Hash table to map op to proplist's item.
 * In order to quickly find item in list, when prop name is known.
 * [Op_Desc]->[Item] */
static Eina_Hash *op_item_table = NULL;

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

static PropViewCbs *_view_cbs = NULL;

PropViewCbs *propview_cbs_get()
{
   if (!_view_cbs) _view_cbs = calloc(1, sizeof(*_view_cbs));
   return _view_cbs;
}

static Eina_Bool
_default_hover_selected_cb(void *data, Eo *obj EINA_UNUSED, const Eo_Event_Description *desc EINA_UNUSED, void *event_info)
{
   const char *choosen_value = elm_object_item_part_text_get(event_info, NULL);
   Eo *entry = data;
   Par_Field_Data *pfd = NULL;
   eo_do(entry, pfd = eo_key_data_get(_STR_EDITOR));
   const Op_Desc *op = pfd->it_data->op_desc;
   Eina_List *def_vals = (Eina_List *) op_param_desc_default_values_list_get(db_op_desc_nth_param_desc_get(op, pfd->par_idx));
   if (def_vals)
     {
        Param_Default_Value *def_val;
        Eina_List *itr;
        EINA_LIST_FOREACH(def_vals, itr, def_val)
          {
             if (!strcmp(choosen_value, param_default_value_name_get(def_val)))
               {
                  eo_do(entry, elm_obj_widget_part_text_set(NULL, param_default_value_get(def_val)));
                  break;
               }
          }
     }
   _view_cbs->property_update_cb_func(NULL, entry, NULL, NULL);
   return EO_CALLBACK_CONTINUE;
}

static Evas_Object *
_property_item_content_get(It_Data *it_data)
{
   Eo *box = eo_add(ELM_BOX_CLASS, prop_list);
   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;

   const Op_Desc *op = it_data->op_desc;
   const Gui_Widget_Property *prop = it_data->prop;
   /* Add label with op name. */

   const char *label = db_op_desc_prop_display_name_get(op);
   if (!label) label = db_op_desc_op_name_get(op);
   props_label_add(box, label);

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

   for (i = 0; i < count; i++)
     {
        /* Get value, for which we want to build field.
         * If it is NULL, field will be build, for nth value in Op_Desc*/
        Gui_Value *value = ((prop && prop_value_get(prop)) ? prop_value_nth_get(prop, i) : NULL);
        Eo *obj = props_value_add(box, it_data->wdg, value, op, i);
        if (obj)
          {
             Par_Field_Data *pfd = calloc (1, sizeof(Par_Field_Data));
             pfd->it_data = it_data;
             pfd->par_idx = i;

             eo_do(obj, eo_key_data_set(_STR_EDITOR, pfd, free));

             if (eo_isa(obj, ELM_HOVERSEL_CLASS))
               {
                  /* pass box as a data, in order to change its color then value changed from default. */
                  eo_do(obj, eo_event_callback_add(EVAS_SELECTABLE_INTERFACE_EVENT_SELECTED, _view_cbs->property_update_cb_func, NULL));
               }
             else if (eo_isa(obj, ELM_CHECK_CLASS))
               {
                  /* pass box as a data, in order to change its color then value changed from default. */
                  eo_do(obj, eo_event_callback_add(ELM_CHECK_EVENT_CHANGED, _view_cbs->property_update_cb_func, NULL));
               }
             else if (eo_isa(obj, ELM_ENTRY_CLASS))
               {
                  /*FIXME: decide how to handle CHANGE ON ACTIVATE*/
                  /* pass box as a data, in order to change its color then value changed from default. */
                  eo_do(obj, eo_event_callback_add(ELM_ENTRY_EVENT_CHANGED_USER, _view_cbs->property_update_cb_func, NULL));

                  /* If there are default values, add Hoversel. */
                  Eina_List *def_vals = (Eina_List *) op_param_desc_default_values_list_get(db_op_desc_nth_param_desc_get(op, i));
                  if (def_vals)
                    {
                       Eo *hover = props_hoversel_enums_add(box, elm_widget_top_get(box), "...", NULL);
                       eo_do(hover, eo_event_callback_add(EVAS_SELECTABLE_INTERFACE_EVENT_SELECTED, _default_hover_selected_cb, obj));
                       Param_Default_Value *def_val;
                       Eina_List *itr;
                       EINA_LIST_FOREACH(def_vals, itr, def_val)
                         {
                            const char *def_val_name = param_default_value_name_get(def_val);
                            eo_do(hover, elm_obj_hoversel_item_add(def_val_name, NULL, ELM_ICON_NONE, NULL, NULL));
                         }
                    }
               }
          }
        else
          {
             /*
             ERR("Can't create field for %dnth parameter in \"%s\", \
                 probably this parameter is NULL pointer", i + 1, db_op_desc_op_name_get(op));
                 */
          }
     }

   return box;
}

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

   if (it_data->box) return it_data->box;

   const char *fullname = db_op_desc_op_name_full_get(it_data->op_desc);

   if (!it_data->prop)
     {
        it_data->prop = prop_create_for_op(it_data->op_desc);
        const Gui_Context *ctx = _active_context_get();
        Gui_Session *session = (Gui_Session *) gui_context_editor_session_get(ctx);
        manager_widget_property_get(session, it_data->wdg, it_data->prop);
     }

   Evas_Object * it_box = _mempool_cache_data_get(fullname);

   if (!it_box)
     {
        it_box = _property_item_content_get(it_data);

        Eo *prop_menu_button = props_button_add(it_box, NULL);
        eo_do(prop_menu_button, evas_obj_size_hint_align_set(1, 0));
        eo_do(prop_menu_button, evas_obj_size_hint_weight_set(0.01, 0));
        eo_do(prop_menu_button, eo_key_data_set(_STR_EDITOR, it_data, NULL));
        eo_do(prop_menu_button, eo_event_callback_add(EVAS_CLICKABLE_INTERFACE_EVENT_CLICKED, _view_cbs->property_ctx_menu_cb_func, NULL));

        Eo *ic = eo_add(ELM_ICON_CLASS, it_box);
        eo_do(ic, efl_file_set(PACKAGE_DATA_DIR"/images/icon_prop_menu2.png", NULL));
        eo_do(prop_menu_button, elm_obj_container_content_set("icon", ic));

        eo_ref(it_box);
     }
   else
     {
        Eina_List *last = eina_list_last(eo_do(it_box, elm_obj_box_children_get()));
        Eo *prop_menu_button  = eina_list_data_get(last);
        eo_do(prop_menu_button, eo_key_data_set(_STR_EDITOR, it_data, NULL));

        it_data->box = it_box;
        _prop_field_update(it_data->prop, it_box, it_data);
     }

   if (!prop_wdg_get(it_data->prop))
     eo_do(it_box, evas_obj_color_set(150, 150, 150, 255));
   else
     eo_do(it_box, evas_obj_color_set(255, 255, 255, 255));

   return it_box;
}

static void
_prop_item_del(void *data, Evas_Object *obj EINA_UNUSED)
{
   It_Data *it_data = data;
   const char *fullname = db_op_desc_op_name_full_get(it_data->op_desc);

   eina_hash_del(op_item_table, NULL, it_data);
   _mempool_cache_data_put(fullname, it_data->box);
   _it_data_free(it_data);
}

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);
}

// Expand a specific class name for its properties children.
static void
_props_classname_table_expand(const Gui_Widget *wdg, const char *classname, Elm_Object_Item *parent)
{
   Eina_List *l1 = NULL;
   Eina_List *l2 = NULL;
   Op_Desc *op;

   const char *filter = _filter_string_get();

   /* iterate over each operation of class. */
   Eina_List *llst = db_class_op_desc_list_get(classname);

   EINA_LIST_FOREACH(llst, l1, op)
     {
        Gui_Widget_Property *prop;
        It_Data *it_data;

        if (db_op_desc_is_container(op)) continue;

        const char *op_display_name = db_op_desc_prop_display_name_get(op);

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

        Eina_List *pitems = NULL;
        Eina_List *wdg_prop_lst = wdg_prop_list_get(wdg);

        // Find which class properties are in widget. Can be more than one.
        EINA_LIST_FOREACH(wdg_prop_lst, l2, prop)
          if (prop_op_desc_get(prop) == op) pitems = eina_list_append(pitems, prop);

        // If none found, its a default one
        if (!eina_list_count(pitems)) pitems = eina_list_append(pitems, NULL);

        EINA_LIST_FREE(pitems, prop)
          {
             it_data = IT_DATA_BUILD(PROPERTY_TYPE, wdg);
             it_data->op_desc = op;
             it_data->prop = prop;
             elm_genlist_item_append(prop_list, _itc_prop, it_data, parent,
                                      ELM_GENLIST_ITEM_NONE, NULL, NULL);
             /* map current item with its prop name. */
             eina_hash_add(op_item_table, db_op_desc_op_name_full_get(op), it_data);
          }
     }
}

static void
_props_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);
   _props_classname_table_expand(cl->wdg, cl->classname, glit);
}

static void
_props_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
propview_build(const Gui_Widget *wdg)
{
   eo_do(prop_list, elm_obj_genlist_clear());

   Eina_List *kl_list = NULL;

   if (wdg)
     {
        /* FIXME: this will be fixed, when Eolian will be used for MRO.
         * Eo will not be needed for MRO. */
        const Gui_Session *session = gui_context_editor_session_get(_active_context_get());
        kl_list = eo_class_mro_get(eo_class_name_get(session_eo_get(session, wdg)));
     }
   const char *kl;

   const char *filter = _filter_string_get();

   EINA_LIST_FREE(kl_list, kl)
     {
        Op_Desc *op;
        Eina_List *llst = db_class_op_desc_list_get(kl);
        Eina_List *l = NULL;

        EINA_LIST_FOREACH(llst, l, op)
          {
             // We dont show container properties. So class with at least one non-container
             // property is good to show.
             if (!db_op_desc_is_container(op))
               {
                  if (filter)
                    {
                       _props_classname_table_expand(wdg, kl, NULL);
                    }
                  else
                    {
                       Classname_Data *cldata = calloc(1, sizeof(Classname_Data));
                       cldata->classname = strdup(kl);
                       cldata->wdg = wdg;

                       Elm_Object_Item *glit = elm_genlist_item_append(prop_list, _itc_class,
                                                                       cldata, NULL,
                                                                       ELM_GENLIST_ITEM_TREE,
                                                                       NULL, NULL);
                       if (eina_hash_find(expanded_classes, kl))
                          elm_genlist_item_expanded_set(glit, EINA_TRUE);
                    }
                  break;
               }
          }
     }

     //if filter search found nothing, yield an appropriate message to properties list
     if (filter)
       {
          if (!elm_genlist_items_count(prop_list))
              elm_genlist_item_append(prop_list, _itc_notfound, NULL, NULL,
                                      ELM_GENLIST_ITEM_NONE, NULL, NULL);
       }
     // Automatically expand the class
     else
       {
          Elm_Object_Item *glit = elm_genlist_first_item_get(prop_list);
          if (glit) elm_genlist_item_expanded_set(glit, EINA_TRUE);
       }
}

/* API to select property in genlist.
 * Op_Desc is used to be able to select empty property. */
void
propview_item_selected_set(const Op_Desc *op_desc)
{
   if (!op_desc) return;
   Elm_Object_Item *it = elm_genlist_first_item_get(prop_list);
   for (; it; it = elm_genlist_item_next_get(it))
     {
        if (elm_genlist_item_item_class_get(it) != _itc_prop)
          continue;

        It_Data *it_data = elm_object_item_data_get(it);
        if (prop_op_desc_get(it_data->prop) == op_desc)
          {
             elm_genlist_item_selected_set(it, EINA_TRUE);
             elm_genlist_item_bring_in(it, ELM_GENLIST_ITEM_SCROLLTO_MIDDLE);
             break;
          }
     }
}

void
propview_item_update(const Gui_Widget_Property *prop)
{
   const char *prop_name;
   Eo *item_box;
   It_Data *it_data = NULL;

   if (!prop) return;
   prop_name = prop_full_name_get(prop);
   /* get item_data from hash. */
   it_data = eina_hash_find(op_item_table, prop_name);
   /* if item of genlist is not visible, box will be NULL, so don't need to update prop.*/
   if (!it_data) return;
   /* Get the item box from item data */
   item_box = it_data->box;
   /* if item box is not realized or any other reason please return.*/
   if (!item_box) return;
   /* If property assigned to widget, make this box white. */
   if (prop_wdg_get(prop))
     eo_do(item_box, evas_obj_color_set(255, 255, 255, 255));

   /* When Undo/Redo only one item is updated.
    * So if this item is doing to be changed from default, this prop must be deleted*/
   if ((it_data->prop) && (!prop_wdg_get(it_data->prop)))
     {
        prop_ref((Gui_Widget_Property *) it_data->prop);
        prop_unref((Gui_Widget_Property *) it_data->prop);
     }

   it_data->prop = prop;

   // Update the fields of the box with properties
   _prop_field_update(prop, item_box, it_data);
}

void
propview_init()
{
   if (!_itc_prop)
     {
        _itc_prop = elm_genlist_item_class_new();
        _itc_prop->item_style = "full";
        _itc_prop->func.content_get = _prop_item_content_get;
        _itc_prop->func.del = _prop_item_del;
     }

   Egui_Layout_Widgets *g = egui_layout_gui_get();

   prop_list = g->main_win->properties_list;

   evas_object_smart_callback_add(prop_list, "expand,request", _expand_request_cb, NULL);
   evas_object_smart_callback_add(prop_list, "contract,request", _contract_request_cb, NULL);
   evas_object_smart_callback_add(prop_list, "expanded", _props_expanded_cb, NULL);
   evas_object_smart_callback_add(prop_list, "contracted", _props_contracted_cb, NULL);

   expanded_classes = eina_hash_string_superfast_new(NULL);
   op_item_table = eina_hash_string_superfast_new(NULL);
}

void
propview_shutdown()
{
   elm_genlist_item_class_free(_itc_prop);
   _itc_prop = NULL;

   eo_do(prop_list, elm_obj_genlist_clear());
   prop_list = NULL;

   eina_hash_free(op_item_table);
   eina_hash_free(expanded_classes);
}