aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/e_widget_config_list.c
blob: 13c8d404dff6c690d103ac907452862fe6ec9cd5 (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
#include "e.h"

typedef struct _E_Widget_Data E_Widget_Data;

/* subsystem functions */
static void _list_cb_change(void *data, Evas_Object *obj);
static void _button_cb_add(void *data, void *obj);
static void _button_cb_remove(void *data, void *obj);
static void _button_cb_up(void *data, void *obj);
static void _button_cb_down(void *data, void *obj);
static void _list_select_num(E_Widget_Data *wd, int indx);
static void _e_wid_disable_hook(Evas_Object *obj);
static void _e_wid_del_hook(Evas_Object *obj);

struct _E_Widget_Data
{
   /* Current Data */
   char *cur_entry;
   int   cur_enabled;

   struct
   {
      Evas_Object *list;   /* Selecting a list entry starts edit*/
      Evas_Object *table;
      Evas_Object *entry;       /* Generic Entry */
      Evas_Object *up;   /* Move selected list entry up */
      Evas_Object *down;   /* Move selected list entry down */
      Evas_Object *add;   /* create and select a new list entry */
      Evas_Object *remove;   /* remove the selected entry */
   } gui;
};

/* Externally accessible functions */
E_API Evas_Object *
e_widget_config_list_add(Evas *evas, Evas_Object * (*func_entry_add)(Evas_Object *, char **val, void (*func)(void *data, void *data2), void *data, void *data2), const char *label, int listspan EINA_UNUSED)
{
   Evas_Object *obj, *o;
   E_Widget_Data *wd;
   Evas_Coord mw, mh;

   obj = e_widget_add(evas);
   e_widget_del_hook_set(obj, _e_wid_del_hook);
   e_widget_disable_hook_set(obj, _e_wid_disable_hook);
   wd = E_NEW(E_Widget_Data, 1);
   e_widget_data_set(obj, wd);

   o = e_widget_table_add(e_win_evas_win_get(evas), 0);
   wd->gui.table = o;
   e_widget_sub_object_add(obj, o);

   o = e_widget_label_add(evas, label);
   e_widget_table_object_append(wd->gui.table, o, 0, 1, 2, 1, 1, 0, 1, 0);

   o = func_entry_add(e_win_evas_win_get(evas), &(wd->cur_entry), NULL, NULL, NULL);
   wd->gui.entry = o;
   e_widget_disabled_set(o, 1);
   e_widget_size_min_set(o, 100, 25);
   e_widget_table_object_append(wd->gui.table, o, 0, 2, 2, 1, 1, 0, 1, 0);

   o = e_widget_button_add(evas, _("Add"), "list-add", _button_cb_add,
                           wd, obj);
   wd->gui.add = o;
   e_widget_disabled_set(o, 1);
   e_widget_table_object_append(wd->gui.table, o, 0, 3, 1, 1, 1, 0, 1, 0);

   o = e_widget_button_add(evas, _("Remove"), "list-remove",
                           _button_cb_remove, wd, obj);
   wd->gui.remove = o;
   e_widget_disabled_set(o, 1);
   e_widget_table_object_append(wd->gui.table, o, 1, 3, 1, 1, 1, 0, 1, 0);

   o = e_widget_ilist_add(evas, 0, 0, NULL);
   wd->gui.list = o;
   e_widget_disabled_set(o, 1);
   e_widget_size_min_set(o, 80, 40);
   e_widget_on_change_hook_set(o, _list_cb_change, wd);
   e_widget_ilist_go(o);
   e_widget_table_object_append(wd->gui.table, o, 0, 4, 2, 1, 1, 1, 1, 1);

   o = e_widget_button_add(evas, _("Up"), "go-up",
                           _button_cb_up, wd, NULL);
   wd->gui.up = o;
   e_widget_disabled_set(o, 1);
   e_widget_table_object_append(wd->gui.table, o, 0, 5, 1, 1, 1, 0, 1, 0);

   o = e_widget_button_add(evas, _("Down"), "go-down",
                           _button_cb_down, wd, NULL);
   wd->gui.down = o;
   e_widget_disabled_set(o, 1);
   e_widget_table_object_append(wd->gui.table, o, 1, 5, 1, 1, 1, 0, 1, 0);

   e_widget_size_min_get(wd->gui.table, &mw, &mh);
   e_widget_size_min_set(obj, mw, mh);
   e_widget_resize_object_set(obj, wd->gui.table);

   return obj;
}

E_API int
e_widget_config_list_count(Evas_Object *obj)
{
   E_Widget_Data *wd;

   wd = e_widget_data_get(obj);
   return e_widget_ilist_count(wd->gui.list);
}

E_API void
e_widget_config_list_clear(Evas_Object *obj)
{
   E_Widget_Data *wd;

   wd = e_widget_data_get(obj);
   e_widget_ilist_clear(wd->gui.list);
   _list_select_num(wd, -1);
}

E_API const char *
e_widget_config_list_nth_get(Evas_Object *obj, int n)
{
   E_Widget_Data *wd;

   wd = e_widget_data_get(obj);
   return e_widget_ilist_nth_label_get(wd->gui.list, n);
}

E_API void
e_widget_config_list_append(Evas_Object *obj, const char *entry)
{
   E_Widget_Data *wd;
   int count;

   wd = e_widget_data_get(obj);
   e_widget_ilist_append(wd->gui.list, NULL, entry, NULL, NULL, NULL);
   e_widget_ilist_go(wd->gui.list);
   count = e_widget_ilist_count(wd->gui.list);
   e_widget_ilist_selected_set(wd->gui.list, count - 1);
}

E_API void
e_widget_config_list_object_append(Evas_Object *obj, Evas_Object *sobj, int col, int row, int colspan, int rowspan, int fill_w, int fill_h, int expand_w, int expand_h)
{
   E_Widget_Data *wd;
   Evas_Coord mw, mh;

   wd = e_widget_data_get(obj);
   e_widget_table_object_append(wd->gui.table, sobj, col, row,
                                colspan, rowspan, fill_w, fill_h,
                                expand_w, expand_h);
   e_widget_size_min_get(wd->gui.table, &mw, &mh);
   e_widget_size_min_set(obj, mw, mh);
}

/* Private Function defs */
static void
_list_cb_change(void *data, Evas_Object *obj EINA_UNUSED)
{
   E_Widget_Data *wd;
   int indx;

   if (!(wd = data)) return;

   indx = e_widget_ilist_selected_get(wd->gui.list);
   _list_select_num(wd, indx);
}

static void
_button_cb_add(void *data, void *obj)
{
   E_Widget_Data *wd;
   Evas_Object *o;

   o = obj;
   if (!o) return;
   if (!(wd = data)) return;
   if ((wd->cur_entry) && (wd->cur_entry[0]))
     {
        int i;

        /* If it already esists just select the existing one */
        for (i = 0; i < e_widget_ilist_count(wd->gui.list); i++)
          {
             const char *label;

             label = e_widget_ilist_nth_label_get(wd->gui.list, i);
             if ((label) && (!strcmp(label, wd->cur_entry)))
               {
                  e_widget_ilist_selected_set(wd->gui.list, i);
                  return;
               }
          }
        e_widget_config_list_append(o, wd->cur_entry);
        e_widget_entry_text_set(wd->gui.entry, "");
     }
}

static void
_button_cb_remove(void *data, void *obj EINA_UNUSED)
{
   E_Widget_Data *wd;
   int indx, count;

   if (!(wd = data)) return;

   indx = e_widget_ilist_selected_get(wd->gui.list);
   count = e_widget_ilist_count(wd->gui.list);

   e_widget_ilist_remove_num(wd->gui.list, indx);

   e_widget_ilist_go(wd->gui.list);
   e_widget_ilist_selected_set(wd->gui.list, indx);
   if (count == 1) _list_select_num(wd, -1);
}

static void
_button_cb_up(void *data, void *obj EINA_UNUSED)
{
   E_Widget_Data *wd;
   int idx_sel;
   const char *label_sel;
   const char *label_rep;

   if (!(wd = data)) return;

   idx_sel = e_widget_ilist_selected_get(wd->gui.list);

   label_sel = e_widget_ilist_nth_label_get(wd->gui.list, idx_sel);
   label_rep = e_widget_ilist_nth_label_get(wd->gui.list, idx_sel - 1);

   e_widget_ilist_nth_label_set(wd->gui.list, idx_sel - 1, label_sel);
   e_widget_ilist_nth_label_set(wd->gui.list, idx_sel, label_rep);

   e_widget_ilist_selected_set(wd->gui.list, idx_sel - 1);
}

static void
_button_cb_down(void *data, void *obj EINA_UNUSED)
{
   E_Widget_Data *wd;
   int idx_sel;
   const char *label_sel;
   const char *label_rep;

   if (!(wd = data)) return;

   idx_sel = e_widget_ilist_selected_get(wd->gui.list);

   label_sel = e_widget_ilist_nth_label_get(wd->gui.list, idx_sel);
   label_rep = e_widget_ilist_nth_label_get(wd->gui.list, idx_sel + 1);

   e_widget_ilist_nth_label_set(wd->gui.list, idx_sel + 1, label_sel);
   e_widget_ilist_nth_label_set(wd->gui.list, idx_sel, label_rep);

   e_widget_ilist_selected_set(wd->gui.list, idx_sel + 1);
}

static void
_list_select_num(E_Widget_Data *wd, int indx)
{
   int count;

   /* Disable selecting the list while we are disabled */
   if (indx >= 0 && !wd->cur_enabled) return;

   count = e_widget_ilist_count(wd->gui.list);

   if ((count == 0) || (indx < 0))
     e_widget_disabled_set(wd->gui.remove, 1);
   else
     e_widget_disabled_set(wd->gui.remove, 0);

   if ((count == 1) || (indx < 0))
     {
        e_widget_disabled_set(wd->gui.up, 1);
        e_widget_disabled_set(wd->gui.down, 1);
     }
   else if (indx == 0)
     {
        e_widget_disabled_set(wd->gui.up, 1);
        e_widget_disabled_set(wd->gui.down, 0);
     }
   else if ((indx + 1) == count)
     {
        e_widget_disabled_set(wd->gui.up, 0);
        e_widget_disabled_set(wd->gui.down, 1);
     }
   else
     {
        e_widget_disabled_set(wd->gui.up, 0);
        e_widget_disabled_set(wd->gui.down, 0);
     }
}

/* Callback Functions */
static void
_e_wid_del_hook(Evas_Object *obj)
{
   E_Widget_Data *wd;

   wd = e_widget_data_get(obj);
   E_FREE(wd->cur_entry);
   E_FREE(wd);
}

static void
_e_wid_disable_hook(Evas_Object *obj)
{
   E_Widget_Data *wd;

   wd = e_widget_data_get(obj);
   if (!wd) return;

   wd->cur_enabled = !e_widget_disabled_get(obj);

   if (wd->cur_enabled)
     {
        e_widget_disabled_set(wd->gui.list, 0);
        e_widget_disabled_set(wd->gui.add, 0);
        e_widget_disabled_set(wd->gui.entry, 0);
     }
   else
     {
        _list_select_num(wd, -1);
        e_widget_disabled_set(wd->gui.list, 1);
        e_widget_disabled_set(wd->gui.entry, 1);
        e_widget_disabled_set(wd->gui.add, 1);
     }
}