summaryrefslogtreecommitdiff
path: root/src/bin/egui_gui/props_helper.c
blob: 2a7c0292eb763a770ea6eeb68f6045bfefb6570b (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
#include <Elementary.h>
#include <Eolian.h>
#include "elm_widget.h"
#include "elm_interface_scrollable.h"
#include "props_helper.h"
#include "egui_log.h"
#include "database.h"
#include "egui_logic_private.h"

static Eina_Hash *mempool_cache = NULL;

extern Gui_Context *
_active_context_get();

const char*
_filter_string_get()
{
   Egui_Layout_Widgets *g = egui_layout_gui_get();
   const char *str = elm_entry_entry_get(g->main_win->propsview_filter_entry);
   //Trim from left
   while (*str == ' ') str++;

   return strlen(str) ? str : NULL;
}

// Case insensitive test for a prefix in a string
Eina_Bool
_str_nocase_prefix(const char *str, const char *prefix)
{
   if (!(str && prefix)) return EINA_FALSE;

   while (*prefix)
     {
        if ((*str == '\0') ||
            (tolower(*prefix) != tolower(*str)))
          return EINA_FALSE;
        prefix++;
        str++;
     }
   return EINA_TRUE;
}

void
_mempool_hash_del(void* data)
{
   Eo* obj = NULL;
   Eina_List *list = data;
   EINA_LIST_FREE(list, obj)
     eo_unref(obj);
}

void
_mempool_cache_init()
{
   mempool_cache = eina_hash_string_superfast_new(_mempool_hash_del);
}

void
_mempool_cache_free()
{
   if (mempool_cache) eina_hash_free(mempool_cache);
}

void*
_mempool_cache_data_get(const char *key)
{
   Eina_List *l = eina_hash_find(mempool_cache, key);

   if (!l) return NULL;

   void *ptr = eina_list_data_get(l);
   l = eina_list_remove_list(l, l);
   eina_hash_set(mempool_cache, key, l);

   return ptr;
}

void
_mempool_cache_data_put(const char *key, void *ptr)
{
   Eina_List *l = eina_hash_find(mempool_cache, key);

   l = eina_list_append(l, ptr);
   eina_hash_set(mempool_cache, key, l);
}

/* Wrapper to add entry to property item.
 * Receives initial string and data for propertie's edit field
 * (wdg, op, par idx). */
Eo *
props_entry_add(Eo *box, const char *str, Gui_Type type)
{
   Eo *en = eo_add(ELM_ENTRY_CLASS, box);
   eo_do(en,
         elm_obj_entry_scrollable_set(EINA_TRUE),
         evas_obj_size_hint_weight_set(0.5, 0.0),
         evas_obj_size_hint_align_set(EVAS_HINT_FILL, 0.5),
         elm_obj_widget_part_text_set(NULL, str),
         elm_interface_scrollable_policy_set(ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_OFF),
         elm_obj_entry_single_line_set(EINA_TRUE),
         elm_obj_entry_text_style_user_push("DEFAULT='color=#FFFFFF'"),
         evas_obj_visibility_set(EINA_TRUE));

//   Gui_Type type = db_op_desc_nth_par_type_get(pfd->it_data->op_desc, pfd->par_idx);

   /* Filter to allow digits only. */
   if (type == GUI_TYPE_SINT)
     {
        static Elm_Entry_Filter_Accept_Set digits_filter_data;
        digits_filter_data.accepted = "-0123456789";
        digits_filter_data.rejected = NULL;
        elm_entry_markup_filter_append(en, elm_entry_filter_accept_set, &digits_filter_data);
     }

   eo_do(box, elm_obj_box_pack_end(en));

   return en;
}

Eo *
props_label_add(Eo *box, const char *str)
{
   Eo *label = eo_add(ELM_LABEL_CLASS, box);
   eo_do(label, evas_obj_visibility_set(EINA_TRUE));
   if (str)
     eo_do(label, elm_obj_widget_part_text_set(NULL, str));
   eo_do(box, elm_obj_box_pack_end(label));
   return label;
}

/* Wrapper to add check to property item.
 * Receives initial state and data for propertie's edit field
 * (wdg, op, par idx). */
Eo *
props_check_add(Eo *box, Eina_Bool state)
{
   Eo *ck = eo_add(ELM_CHECK_CLASS, box);
   eo_do(ck,
            evas_obj_visibility_set(EINA_TRUE),
            elm_obj_check_state_set(state));

   eo_do(box, elm_obj_box_pack_end(ck));
   return ck;
}

Eo *
props_button_add(Eo *box, char *text)
{
   Eo *bt = eo_add(ELM_BUTTON_CLASS, box);
   eo_do(bt, evas_obj_visibility_set(EINA_TRUE));
   if (text)
     eo_do(bt, elm_obj_widget_part_text_set(NULL, text));

   eo_do(box, elm_obj_box_pack_end(bt));
   return bt;
}

Eo *
props_hoversel_enums_add(Eo *box, Eo *hover_parent, const char *default_val, const char **enums_lst)
{
   Eo *hoversel = eo_add(ELM_HOVERSEL_CLASS, box);
   eo_do(hoversel, elm_obj_hoversel_hover_parent_set(hover_parent));

   if (!default_val)
     {
        default_val = PROP_HOVERSEL_NONE_VALUE;
     }
   elm_object_text_set(hoversel, default_val);
   /* Add items to Hoversel. */
   if (enums_lst)
     {
        while (*enums_lst != NULL)
          {
             eo_do(hoversel, elm_obj_hoversel_item_add(*enums_lst, NULL, ELM_ICON_NONE, NULL, NULL));
             enums_lst++;
          }
     }

   eo_do(hoversel, evas_obj_size_hint_weight_set(0.0, 0.0));
   eo_do(hoversel, evas_obj_size_hint_align_set(0.5, 0.5));
   eo_do(box, elm_obj_box_pack_end(hoversel));
   eo_do(hoversel, evas_obj_visibility_set(EINA_TRUE));
   return hoversel;
}

Eo *
props_hoversel_objects_add(Eo *box, Eo *hoversel, Eo *hover_parent, const char *default_val, const Eina_List *wdgs_list, const Gui_Widget *wdg)
{
   if (!hoversel)
     {
        hoversel = eo_add(ELM_HOVERSEL_CLASS, box);
        eo_do(hoversel, elm_obj_hoversel_hover_parent_set(hover_parent));
     }

   if ((wdg) &&
       (!strcmp(wdg_class_name_get(wdg), DB_DEF_CTXPOPUP_CLASS) ||
       !strcmp(wdg_class_name_get(wdg), DB_DEF_DISKSELECTOR_CLASS)))
     {
        eo_do(hoversel, elm_obj_hoversel_item_add(PROP_HOVERSEL_NONE_VALUE, NULL, ELM_ICON_NONE, NULL, NULL));
     }

   if (!default_val)
     {
        default_val = PROP_HOVERSEL_NONE_VALUE;
     }
   elm_object_text_set(hoversel, default_val);

   const Eina_List *itr;
   Dep_Id *wid;
   EINA_LIST_FOREACH(wdgs_list, itr, wid)
     {
        Gui_Widget *w = wdg_get(wid);
        if (!w) continue;
        if (!wdg || ((w != wdg) &&
            (wdg_main_wdg_get(wdg) == wdg_main_wdg_get(w)) &&
            (!IS_WIN(w))))
          {
             /* w - iterated widget;
              * wdg - container
              * If
              *  - wdg is direct container for w;
              *  - w is container for wdg;
              *    don't add it to hoversel. */
             if (IS_CONTAINER(wdg))
               {
                  /*check, if wdg is content of w on any level. */
                  Eina_Bool found = EINA_FALSE;
                  if (wdg_is_content_of(w, wdg)) continue;
                  Eina_List *itr2;
                  Object_Container_Item *ci;
                  EINA_LIST_FOREACH(wdg_obj_container_contents_list_get(wdg), itr2, ci)
                    {
                       if (obj_container_item_dep_id_get(ci) == wid) found = EINA_TRUE;
                    }
                  if (found) continue;
               }

             eo_do(hoversel, elm_obj_hoversel_item_add(wdg_name_get(w), NULL, ELM_ICON_NONE, NULL, NULL));
          }
     }

   eo_do(hoversel, evas_obj_size_hint_weight_set(0.0, 0.0));
   eo_do(hoversel, evas_obj_size_hint_align_set(0.5, 0.5));
   if (box) eo_do(box, elm_obj_box_pack_end(hoversel));
   eo_do(hoversel, evas_obj_visibility_set(EINA_TRUE));
   return hoversel;
}

Eo *
props_hoversel_object_ops_add(Eo *box, Eo *hover_parent, const char *default_val, const Gui_Widget *wdg)
{
   Eina_List *kl_list = NULL, *op_itr;
   Eo *hoversel = eo_add(ELM_HOVERSEL_CLASS, box);
   eo_do(hoversel, elm_obj_hoversel_hover_parent_set(hover_parent));

   if (default_val) eo_do(hoversel, elm_obj_widget_part_text_set(NULL, default_val));

   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;
   EINA_LIST_FREE(kl_list, kl)
     {
        Eina_List *kl_ops_lst = db_class_op_desc_list_get(kl);
        Op_Desc *op;
        EINA_LIST_FOREACH(kl_ops_lst, op_itr, op)
          {
             eo_do(hoversel, elm_obj_hoversel_item_add(db_op_desc_op_name_full_get(op), NULL, ELM_ICON_NONE, NULL, NULL));
          }
     }
   eo_do(hoversel, evas_obj_size_hint_weight_set(0.0, 0.0));
   eo_do(hoversel, evas_obj_size_hint_align_set(0.5, 0.5));
   eo_do(box, elm_obj_box_pack_end(hoversel));
   eo_do(hoversel, evas_obj_visibility_set(EINA_TRUE));
   return hoversel;
}

Eo *
props_hoversel_seqs_add(Eo *box, Eo *hover_parent, const Gui_Context *ctx, const char *default_val, Eina_Bool add_empty_field)
{
   Dep_Id *seq_id;
   Eina_List *itr;
   Eo *hoversel = eo_add(ELM_HOVERSEL_CLASS, box);
   eo_do(hoversel, elm_obj_hoversel_hover_parent_set(hover_parent));

   if (add_empty_field)
     eo_do(hoversel, elm_obj_hoversel_item_add(PROP_HOVERSEL_NONE_VALUE, NULL, ELM_ICON_NONE, NULL, NULL));

   if (!default_val)
     {
        default_val = PROP_HOVERSEL_NONE_VALUE;
     }
   eo_do(hoversel, elm_obj_widget_part_text_set(NULL, default_val));
   EINA_LIST_FOREACH(gui_context_seq_list_get(ctx), itr, seq_id)
     {
        Gui_Sequence *seq = sequence_get(seq_id);
        if (seq) eo_do(hoversel, elm_obj_hoversel_item_add(sequence_name_get(seq), NULL, ELM_ICON_NONE, NULL, NULL));
     }
   eo_do(hoversel, evas_obj_size_hint_weight_set(0.0, 0.0));
   eo_do(hoversel, evas_obj_size_hint_align_set(0.5, 0.5));
   eo_do(box, elm_obj_box_pack_end(hoversel));
   eo_do(hoversel, evas_obj_visibility_set(EINA_TRUE));
   return hoversel;
}

Eo *
props_hoversel_resource_files_add(Eo *box, Eo *hoversel, Eo *hover_parent, const char *default_val, const Gui_Context *ctx)
{
   if (!hoversel)
     {
        hoversel = eo_add(ELM_HOVERSEL_CLASS, box);
        eo_do(hoversel, elm_obj_hoversel_hover_parent_set(hover_parent));
     }
   elm_hoversel_item_add(hoversel, PROP_HOVERSEL_NONE_VALUE, NULL, ELM_ICON_NONE, NULL, NULL);

   if (!default_val)
     {
        default_val = PROP_HOVERSEL_NONE_VALUE;
     }
   elm_object_text_set(hoversel, default_val);

   const Eina_List *res_lst, *itr;
   Dep_Id *res_id;
   res_lst = gui_context_resource_list_get(ctx, RESOURCE_IMAGE);
   EINA_LIST_FOREACH(res_lst, itr, res_id)
     {
        const Gui_Resource *res = gui_context_resource_get(res_id);
        elm_hoversel_item_add(hoversel, resource_name_get(res), NULL, ELM_ICON_NONE, NULL, NULL);
     }

   res_lst = gui_context_resource_list_get(ctx, RESOURCE_EDJE);
   EINA_LIST_FOREACH(res_lst, itr, res_id)
     {
        const Gui_Resource *res = gui_context_resource_get(res_id);
        if (res) elm_hoversel_item_add(hoversel, resource_name_get(res), NULL, ELM_ICON_NONE, NULL, NULL);
     }

   eo_do(hoversel, evas_obj_size_hint_weight_set(0.0, 0.0));
   eo_do(hoversel, evas_obj_size_hint_align_set(0.5, 0.5));
   if (box) eo_do(box, elm_obj_box_pack_end(hoversel));
   eo_do(hoversel, evas_obj_visibility_set(EINA_TRUE));
   return hoversel;
}

Eo *
props_value_add(Eo *box, const Gui_Context *ctx, const Gui_Widget *wdg, const Gui_Value *value, const Op_Desc *op, unsigned int par_idx)
{
   Eo *new_obj = NULL;

   Gui_Type t;
   /* If value exists take everything from value.
    * Value doesn't exist, when create field for empty property, so Op_Desc is used*/

   t = value ? gui_value_type_get(value) : db_op_desc_nth_par_type_get(op, par_idx);

   switch (t)
     {
      case GUI_TYPE_SINT:
           {
              char str[15] = {'\0'};
              if (value)
                {
                   int i = INT_GET(value);
                   sprintf(str, "%d", i);
                }
              new_obj = props_entry_add(box, str, t);
              break;
           }
      case GUI_TYPE_STRING:
           {
              char *str = NULL;
              if (value)
                {
                   str = STRING_GET(value);
                }
              new_obj = props_entry_add(box, str, t);
              break;
           }
      case GUI_TYPE_OBJECT:
           {
              const char *default_str = PROP_HOVERSEL_NONE_VALUE;
              if (value)
                {
                   Dep_Id *dep_id = DEP_ID_GET(value);
                   if (dep_id)
                     default_str = dep_name_get(dep_id);
                }
              new_obj = props_hoversel_objects_add(box, NULL, elm_widget_top_get(box), default_str, gui_context_wdg_list_get(ctx), wdg);
              break;
           }
      case GUI_TYPE_DOUBLE:
           {
              char str[30] = {'\0'};
              if (value)
                {
                   double pi;
                   pi = DOUBLE_GET(value);
                   sprintf(str, "%f", pi);
                }
              new_obj = props_entry_add(box, str, t);
              break;
           }
      case GUI_TYPE_BOOL:
           {
              Eina_Bool state = EINA_FALSE;
              if (value)
                {
                   state = BOOL_GET(value);
                }
              new_obj = props_check_add(box, state);
              break;
           }
      case GUI_TYPE_ENUM:
           {
              char *default_str = NULL;
              if (value)
                {
                   default_str = STRING_GET(value);
                }
              const char **enums_list = db_op_desc_nth_par_enum_list_get(op, par_idx);
              new_obj = props_hoversel_enums_add(box, elm_widget_top_get(box), default_str, enums_list);
              break;
           }
      case GUI_TYPE_FILE:
           {
              const char *default_str = PROP_HOVERSEL_NONE_VALUE;
              if (value)
                {
                   Dep_Id *dep_id = DEP_ID_GET(value);
                   if (dep_id) default_str = dep_name_get(dep_id);
                   if (!default_str)
                     {
                        ERR("Looks like resource image was deleted. Handle this.");
                     }
                }

              new_obj = props_hoversel_resource_files_add(box, NULL, elm_widget_top_get(box), default_str, wdg_context_get(wdg));
              break;
           }
      case GUI_TYPE_OBJECT_ITEM:
         /* FIXME: add handler for calback's data if needed*/
      case GUI_TYPE_VOID:
         break;
      default:
         ERR("switch default for type: %s; op_name: %s", db_gui_type_name_get(t), db_op_desc_op_name_get(op));
     }
   return new_obj;
}