summaryrefslogtreecommitdiff
path: root/src/bin/egui_gui/contentview.c
blob: 1e5acdbd9692883dc26d61ac326909bd0f226db7 (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
#include <Elementary.h>
#include <Eolian.h>

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

#include "contentview.h"

#include "elm_widget_container.h"

static Elm_Genlist_Item_Class *_itc_ctn = NULL;

static Eo *ctn_list = NULL;

static ContentViewCbs *_contentview_cbs = NULL;

ContentViewCbs *
contentview_cbs_get()
{
   if (!_contentview_cbs) _contentview_cbs = calloc(1, sizeof(*_contentview_cbs));
   return _contentview_cbs;
}

static Evas_Object *
_content_item_content_get(It_Data *it_data)
{
   const Gui_Widget *wdg = it_data->wdg;

   unsigned int count, i;
   count = db_op_desc_param_number_get(it_data->op_desc);

   Eo *box = eo_add(ELM_BOX_CLASS, ctn_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;

   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 *val = ((it_data->prop) ? prop_value_nth_get(it_data->prop, i) : NULL);
        Eo *obj = props_value_add(box, wdg, val, it_data->op_desc, 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))
               {
                  eo_do(obj, eo_event_callback_add(EVAS_SELECTABLE_INTERFACE_EVENT_SELECTED,
                                                   _contentview_cbs->content_change_cb_func, NULL));
                  eo_do(obj, evas_obj_size_hint_weight_set(0, EVAS_HINT_EXPAND),
                             evas_obj_size_hint_align_set(0, EVAS_HINT_FILL));
               }
             else
               {
                  eo_do(obj, eo_event_callback_add(ELM_ENTRY_EVENT_ACTIVATED, _contentview_cbs->content_change_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(it_data->op_desc));
          }
     }
   return box;
}

void
contentview_build(const Gui_Widget *wdg)
{
   Eina_List *ctn_items = NULL;
   Eina_List *l;
   int i = 0;
   Object_Container_Item *ci;
   It_Data *it_data;

   eo_do(ctn_list, elm_obj_genlist_clear());

   const char *wdg_name = wdg_obj_container_class_name_get(wdg);
   Eina_Bool isbox = (wdg_name &&
                      (!strcmp(wdg_name, DB_DEF_BOX_CLASS) ||
                       !strcmp(wdg_name, DB_DEF_WIN_CLASS)));

   Egui_Layout_Widgets *g = egui_layout_gui_get();
   elm_object_disabled_set(g->main_win->contentview_down_btn, !isbox);
   elm_object_disabled_set(g->main_win->contentview_up_btn, !isbox);

   Eina_List *items_lst = wdg_obj_container_contents_list_get(wdg);

   EINA_LIST_FOREACH(items_lst, l, ci)
     {
        Eid *item_id = obj_container_item_eid_get(ci);
        if (IS_ID_VALID(item_id) && !wdg_get(item_id)) continue;

        Gui_Widget_Property *prop = obj_container_item_prop_get(ci);
        it_data = IT_DATA_BUILD(CONTENT_TYPE, wdg);
        it_data->idx = i;
        it_data->prop = prop;
        it_data->op_desc = (Op_Desc *) prop_op_desc_get(prop);
        i++;

        ctn_items = eina_list_append(ctn_items, it_data);
     }

   /*  Add empty fields. */
   const char *wdg_class_name = wdg_class_name_get(wdg);
   const char *name;
   /* Add empty field for each type of container. */
   EINA_LIST_FOREACH(db_class_inherit_containers_list_get(wdg_class_name), l, name)
     {
        const Op_Desc *op_desc = db_container_desc_op_desc_get(db_container_desc_get(name), CONTAINER_PACK);
        it_data = IT_DATA_BUILD(CONTENT_TYPE, wdg);
        it_data->idx = i;
        it_data->op_desc = op_desc;
        it_data->prop = NULL;

        ctn_items = eina_list_append(ctn_items, it_data);
     }

   EINA_LIST_FREE(ctn_items, it_data)
     elm_genlist_item_append(ctn_list, _itc_ctn, it_data, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
}

static void
_content_select_item(const Gui_Widget_Property *prop)
{
    Elm_Object_Item *it = elm_genlist_first_item_get(ctn_list);
    for (; it; it = elm_genlist_item_next_get(it))
      {
         It_Data *it_data = elm_object_item_data_get(it);
         if (it_data->prop == prop)
           {
              elm_genlist_item_selected_set(it, EINA_TRUE);
              break;
           }
      }
}

Eina_Bool
_contentview_del_btn_clicked(void *data EINA_UNUSED, Eo *obj, const Eo_Event_Description *desc, void *event_info)
{
   const Gui_Widget_Property *prev_prop = NULL;

   Elm_Object_Item *glit = elm_genlist_selected_item_get(ctn_list);
   if (!glit) return EO_CALLBACK_CONTINUE;

   It_Data *it_data = elm_object_item_data_get(glit);
   intptr_t idx = (intptr_t) it_data->idx;

   it_data = elm_object_item_data_get(elm_genlist_item_next_get(glit));
   if (it_data) prev_prop = it_data->prop;
   if (!prev_prop)
     {
        it_data = elm_object_item_data_get(elm_genlist_item_prev_get(glit));
        if (it_data) prev_prop = it_data->prop;
     }

   Eina_Bool ret = _contentview_cbs->content_delete_cb_func((void *)idx, obj, desc, event_info);
   if (prev_prop) _content_select_item(prev_prop);

   return ret;
}

Eina_Bool
_contentview_up_btn_clicked(void *data EINA_UNUSED, Eo *obj, const Eo_Event_Description *desc, void *event_info EINA_UNUSED)
{
   Elm_Object_Item *glit = elm_genlist_selected_item_get(ctn_list);
   if (!glit) return EO_CALLBACK_CONTINUE;

   It_Data *it_data = elm_object_item_data_get(glit);
   const Gui_Widget_Property *prev_prop = it_data->prop;
   intptr_t idx = it_data->idx;

   Eina_Bool ret = _contentview_cbs->content_reorder_cb_func((void *)idx, obj, desc, (void *)(intptr_t)UP);
   _content_select_item(prev_prop);

   return ret;
}

Eina_Bool
_contentview_down_btn_clicked(void *data EINA_UNUSED, Eo *obj, const Eo_Event_Description *desc, void *event_info EINA_UNUSED)
{
    Elm_Object_Item *glit = elm_genlist_selected_item_get(ctn_list);
   if (!glit) return EO_CALLBACK_CONTINUE;

   It_Data *it_data = elm_object_item_data_get(glit);
   const Gui_Widget_Property *prev_prop = it_data->prop;
   intptr_t idx = it_data->idx;

   Eina_Bool ret = _contentview_cbs->content_reorder_cb_func((void *)idx, obj, desc, (void *)(intptr_t)DOWN);
   _content_select_item(prev_prop);

   return ret;
}

static void
_contents_item_del(void *data, Evas_Object *obj EINA_UNUSED)
{
   It_Data *it_data = data;

   if (it_data->prop)
     {
        const char *fullname = db_op_desc_op_name_full_get(it_data->op_desc);
        _mempool_cache_data_put(fullname, it_data->box);
     }

   _it_data_free(it_data);
}

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

   if (!it_data->prop) return _content_item_content_get(it_data);

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

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

   Evas_Object * it_box = _mempool_cache_data_get(fullname);

   if (!it_box)
     {
        it_box = _content_item_content_get(it_data);
        eo_ref(it_box);
     }
   else
     {
        it_data->box = it_box;
        _prop_field_update(it_data->prop, it_box, it_data);
     }

   return it_box;
}

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

   ctn_list = g->main_win->contentview_list;

   if (!_itc_ctn)
     {
        _itc_ctn = elm_genlist_item_class_new();
        _itc_ctn->item_style = "full";
        _itc_ctn->func.content_get = _contents_item_content_get;
        _itc_ctn->func.del = _contents_item_del;
     }
}

void
contentview_shutdown(void)
{
   elm_genlist_item_class_free(_itc_ctn);
   _itc_ctn = NULL;

   eo_do(ctn_list, elm_obj_genlist_clear());
   ctn_list = NULL;
}