summaryrefslogtreecommitdiff
path: root/src/bin/egui_gui/itemview.c
blob: 0949ccb74e6bdac6435b4014112247a33bff372b (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

#include <Elementary.h>
#include "gui_widget.h"
#include "egui_log.h"

#include "props_helper.h"
#include "itemview.h"
#include "egui_logic_private.h"

#include "elm_widget.h"

static Eo *lst;

static Elm_Genlist_Item_Class *_item_itc = NULL;

static ItemViewCbs *_view_cbs = NULL;

const Gui_Widget*
_editor_wdg_selected_get();

extern Gui_Context *
_active_context_get();

ItemViewCbs *
itemview_cbs_get()
{
   if (!_view_cbs) _view_cbs = calloc(1, sizeof(*_view_cbs));
   return _view_cbs;
}

/* Hash table from widgets to genlist items */
Eina_Hash *_witems_to_items_table = NULL;

static Evas_Object *
_item_list_content_get(void *data, Evas_Object *obj EINA_UNUSED, const char *part EINA_UNUSED)
{
   Eo *box = eo_add(ELM_BOX_CLASS, lst);
   const Gui_Widget *wdg = _editor_wdg_selected_get();
   Item_Container_Item *item = item_container_item_get_from_id((Eid *)data);

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

   It_Data *it_data = IT_DATA_BUILD(ITEM_TYPE, item);
   it_data->prop = item_container_item_prop_get(item);
   it_data->op_desc = (Op_Desc *) prop_op_desc_get(it_data->prop);

   eo_do(box, eo_key_data_set("editor", it_data, _it_data_free));

   Eina_List *itr;
   Gui_Value *val;

   int i = 0;
   EINA_LIST_FOREACH(prop_value_get(it_data->prop), itr, val)
     {
        Eo *o = NULL;
        Gui_Type op_val_type = db_op_desc_nth_par_type_get(it_data->op_desc, i);

        if (op_val_type == GUI_TYPE_CB)
          {
             /* Create hoversel with sequences. */
             const char *cb_name = PROP_HOVERSEL_NONE_VALUE;
             if (gui_value_type_get(val) == GUI_TYPE_CB)
               {
                  Eid *eid = EID_ID_GET(val);
                  if (eid)
                    cb_name = eid_name_get(eid);
               }
             const char *cb_type_name = db_op_desc_nth_par_type_name_get(it_data->op_desc, i);
             /* FIXME: This if is not needed? */
             if (!strcmp(cb_type_name, "Evas_Smart_Cb"))
               o = props_hoversel_resources_add(box, NULL, elm_widget_top_get(box), cb_name, wdg_context_get(wdg), RESOURCE_EO_CALLBACK);
          }
        else
          {
             o = props_value_add(box, wdg, val, it_data->op_desc, i);
          }

        if (o)
          {
             Par_Field_Data *pfd = calloc (1, sizeof(Par_Field_Data));
             pfd->it_data = it_data;
             pfd->par_idx = i;

             eo_do(o, eo_key_data_set("editor", pfd, free));
             const char *tooltip_text = op_param_desc_name_get(db_op_desc_nth_param_desc_get(it_data->op_desc, i));
             elm_object_tooltip_text_set(o, tooltip_text);

             if (eo_isa(o, ELM_HOVERSEL_CLASS))
               {
                  eo_do(o, eo_event_callback_add(EVAS_SELECTABLE_INTERFACE_EVENT_SELECTED,
                                                   _view_cbs->item_update_cb_func, NULL));
               }
             else
               {
                  eo_do(o, eo_event_callback_add(ELM_ENTRY_EVENT_ACTIVATED, _view_cbs->item_update_cb_func, 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));
                 */
          }
        i++;
     }

   props_label_add(box, "public");
   Eo *ck = props_check_add(box, item_container_item_public_get(item));
   eo_do(ck, eo_event_callback_add(ELM_CHECK_EVENT_CHANGED, _view_cbs->item_public_update_cb_func, item));
   elm_object_tooltip_text_set(ck, "Check to make item accissible from code");

   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
_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
_expanded_cb(void *data EINA_UNUSED, Evas_Object *o EINA_UNUSED, void *event_info)
{
   Eina_List *itr;
   /* This is item being expanded. */
   Elm_Object_Item *glit = event_info;

   Item_Container_Item *parent_item = item_container_item_get_from_id((Eid *)elm_object_item_data_get(glit));

   /* Adding child's items into objtree view. */
   Eid *eid;
   EINA_LIST_FOREACH(item_container_item_subitems_get(parent_item), itr, eid)
     {
        Item_Container_Item *wit = item_container_item_get_from_id(eid);

        Elm_Object_Item *nitem;
        Eid *wit_id = item_container_item_eid_get(wit);
        nitem = elm_genlist_item_append(lst, _item_itc, (void *) (uintptr_t) wit_id, glit,
                                        ELM_GENLIST_ITEM_TREE, _view_cbs->item_selection_cb_func, NULL);
//        elm_genlist_item_expanded_set(nitem, EINA_FALSE);
        eina_hash_set(_witems_to_items_table, &wit_id, nitem);
     }
}

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

static void
_item_del_cb(void *data, Eo *obj EINA_UNUSED)
{
   if (data)
     {
        eina_hash_del_by_key(_witems_to_items_table, &data);
     }
}

void
itemview_init()
{
   Egui_Layout_Widgets *g = egui_layout_gui_get();

   lst = g->main_win->itemview_list;

   if (!_item_itc)
     {
        _item_itc = elm_genlist_item_class_new();
        _item_itc->item_style = "full";
        _item_itc->func.text_get = NULL;
        _item_itc->func.content_get = _item_list_content_get;
        _item_itc->func.state_get = NULL;
        _item_itc->func.del = _item_del_cb;
     }
   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
itemview_shutdown()
{
   if (_item_itc)
     {
        elm_genlist_item_class_free(_item_itc);
        _item_itc = NULL;
     }
}

void
itemview_build(const Gui_Widget *wdg)
{
   Eina_List *itr;
   itemview_clear();
#ifdef __LP64__
   _witems_to_items_table = eina_hash_int64_new(NULL);
#else
   _witems_to_items_table = eina_hash_int32_new(NULL);
#endif

   /* During first build, only headed widgets are added. */
   const Item_Container_Item *root_it = wdg_item_container_root_get(wdg);
   Eid *eid_itr;
   EINA_LIST_FOREACH(item_container_item_subitems_get(root_it), itr, eid_itr)
     {
        Item_Container_Item *wit = item_container_item_get_from_id(eid_itr);
        Elm_Object_Item *glit;
        Eid *wit_id = item_container_item_eid_get(wit);
        glit = elm_genlist_item_append(lst, _item_itc, wit_id, NULL,
                                ELM_GENLIST_ITEM_TREE,
                                _view_cbs->item_selection_cb_func, NULL);
        eina_hash_set(_witems_to_items_table, &wit_id, glit);
     }
}

void
itemview_clear()
{
   elm_genlist_clear(lst);
   if (_witems_to_items_table)
     {
        eina_hash_free(_witems_to_items_table);
        _witems_to_items_table = NULL;
     }
}

void
itemview_insert_after(Eid *parent_it_id, Eid *after_id, Eid *wit_id)
{
   Elm_Object_Item *pglit = NULL, *glafter = NULL, *nit;

   /* Looking for parent item. */
   if (parent_it_id)
     {
        pglit = eina_hash_find(_witems_to_items_table, &parent_it_id);
     }
   /* Looking for after item. */
   if (after_id)
     {
        glafter = eina_hash_find(_witems_to_items_table, &after_id);
     }

   /* If item has parent, expand parent, and all items will be added automatically */
   if ((pglit) && (!elm_genlist_item_expanded_get(pglit)))
     {
        elm_genlist_item_expanded_set(pglit, EINA_TRUE);
        return;
     }

   /* If parent already expanded, add items manually. */
   if (glafter)
     {
        /* If new item is added after the item, which has expanded subitems,
         * new item will be added before these items.
         * So need to contract it, and expand after adding new item. */
        elm_genlist_item_expanded_set(glafter, EINA_FALSE);
        nit = elm_genlist_item_insert_after(lst, _item_itc, (void *) (uintptr_t) wit_id, pglit, glafter,
                                            ELM_GENLIST_ITEM_TREE,
                                            _view_cbs->item_selection_cb_func, NULL);
        elm_genlist_item_expanded_set(glafter, EINA_TRUE);
     }
   else
     {
        nit = elm_genlist_item_append(lst, _item_itc, (void *) (uintptr_t) wit_id, pglit,
                                      ELM_GENLIST_ITEM_TREE,
                                      _view_cbs->item_selection_cb_func, NULL);
     }

   eina_hash_set(_witems_to_items_table, &wit_id, nit);
}

/* Return Item_Container_Item for selected Genlist Item. */
Eid *
itemview_item_selected_get()
{
   Elm_Object_Item *it = NULL;
   Eid *wit_id = 0;
   it = elm_genlist_selected_item_get(lst);
   if (it)
     {
        wit_id = elm_object_item_data_get(it);
     }
   return wit_id;
}

void
itemview_item_selected_set(const Item_Container_Item *wit)
{
   Elm_Object_Item *item = NULL;
   if (wit)
     {
        Eina_List *chain = eina_list_append(NULL, wit);
        while ((wit = item_container_item_parent_item_get(wit)))
          {
             /* while will not stop on root item, so add additional check. */
             if (!item_container_item_parent_item_id_get(wit)) break;
             chain = eina_list_prepend(chain, wit);
          }
        Item_Container_Item *witr;
        EINA_LIST_FREE(chain, witr)
          {
             Eid *wit_id = item_container_item_eid_get(witr);
             item = eina_hash_find(_witems_to_items_table, &wit_id);
             elm_genlist_item_expanded_set(item, EINA_TRUE);
          }
        elm_genlist_item_bring_in(item, ELM_GENLIST_ITEM_SCROLLTO_MIDDLE);
        elm_genlist_item_selected_set(item, EINA_TRUE);
     }
}

void
itemview_item_del(const Item_Container_Item *wit)
{
   Eid *wit_id = item_container_item_eid_get(wit);
   Elm_Object_Item *glit = eina_hash_find(_witems_to_items_table, &wit_id);
   if (glit)
     {
        eina_hash_del_by_key(_witems_to_items_table, &wit_id);
        elm_object_item_del(glit);
     }
}

/* This callback is called, when "Add Item" button is pressed. */
Eina_Bool
_itemview_item_add_but_clicked_cb(void *data EINA_UNUSED, Eo *obj EINA_UNUSED, const Eo_Event_Description *desc EINA_UNUSED, void *event_info EINA_UNUSED)
{
   _view_cbs->_itemview_item_do_(ITEM_ADD);
   return EO_CALLBACK_CONTINUE;
}

/* This callback is called, when "Add Subitem" button is pressed. */
Eina_Bool
_itemview_item_nested_add_but_clicked_cb(void *data EINA_UNUSED, Eo *obj EINA_UNUSED, const Eo_Event_Description *desc EINA_UNUSED, void *event_info EINA_UNUSED)
{
   _view_cbs->_itemview_item_do_(ITEM_ADD_NESTED);
   return EO_CALLBACK_CONTINUE;
}

/* This callback is called, when "Add Separator" button is pressed. */
Eina_Bool
_itemview_separator_add_but_clicked_cb(void *data EINA_UNUSED, Eo *obj EINA_UNUSED, const Eo_Event_Description *desc EINA_UNUSED, void *event_info EINA_UNUSED)
{
   _view_cbs->_itemview_item_do_(SEPARATOR_ADD);
   return EO_CALLBACK_CONTINUE;
}

/* This callback is called, when "Delete Item" button is pressed. */
Eina_Bool
_itemview_item_del_but_clicked_cb(void *data EINA_UNUSED, Eo *obj EINA_UNUSED, const Eo_Event_Description *desc EINA_UNUSED, void *event_info EINA_UNUSED)
{
   _view_cbs->_itemview_item_do_(ITEM_DELETE);
   return EO_CALLBACK_CONTINUE;
}