summaryrefslogtreecommitdiff
path: root/src/modules/prefs/elm_entry.c
blob: 301842d414d0b75d0c0a1f147e35a5a253e2c734 (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
#include "private.h"
#include "elm_widget.h"
#include <sys/types.h>
#include <regex.h>

#define BLINK_INTERVAL 0.1

static Elm_Prefs_Item_Type supported_types[] =
{
   ELM_PREFS_TYPE_TEXT,
   ELM_PREFS_TYPE_TEXTAREA,
   ELM_PREFS_TYPE_UNKNOWN
};

static Eina_Bool
_color_change_do(void *data)
{
   Evas_Object *obj = data;
   int color;

   color = (int)(uintptr_t)evas_object_data_get(obj, "current_color");
   switch (color)
     {
      case 0:
        evas_object_data_set(obj, "current_color", (void *)1);
        evas_object_color_set(obj, 255, 0, 0, 255);    /* 1st red */
        goto renew;

      case 1:
        evas_object_data_set(obj, "current_color", (void *)2);
        evas_object_color_set(obj, 255, 255, 255, 255);    /* 2nd white */
        goto renew;

      case 2:
        evas_object_data_set(obj, "current_color", (void *)3);
        evas_object_color_set(obj, 255, 0, 0, 255);    /* 2nd red */
        goto renew;

      case 3:
      default:
        evas_object_data_set(obj, "current_color", (void *)0);
        evas_object_color_set(obj, 255, 255, 255, 255);    /* back to white */
        goto end;
     }

renew:
   return ECORE_CALLBACK_RENEW;

end:
   evas_object_data_del(obj, "timer");
   return ECORE_CALLBACK_CANCEL;
}

static Eina_Bool
elm_prefs_entry_value_validate(Evas_Object *obj)
{
   Ecore_Timer *timer;
   const char *val;
   regex_t *regex;
   size_t min;

   val = elm_entry_entry_get(obj);

   regex = evas_object_data_get(obj, "accept_regex");
   if (regex)
     {
        if (regexec(regex, val, 0, NULL, 0)) goto mismatch;
     }

   regex = evas_object_data_get(obj, "deny_regex");
   if (regex)
     {
        /* we want tokens *out* of the deny language */
        if (!regexec(regex, val, 0, NULL, 0)) goto mismatch;
     }

   min = (size_t) evas_object_data_get(obj, "min_size");
   if (min)
     {
        if (strlen(val) < min) goto mismatch;
     }

   return EINA_TRUE;

mismatch:
   evas_object_color_set(obj, 255, 0, 0, 255);

   timer = evas_object_data_get(obj, "timer");
   if (timer) ecore_timer_del(timer);

   evas_object_data_set
     (obj, "timer", ecore_timer_add(BLINK_INTERVAL, _color_change_do, obj));

   return EINA_FALSE;
}

static Eina_Bool
_item_changed_cb(void *data,
                 Eo *obj, const Eo_Event_Description *desc EINA_UNUSED,
                 void *event_info EINA_UNUSED)
{
   Elm_Prefs_Item_Changed_Cb prefs_it_changed_cb = data;

   prefs_it_changed_cb(obj);
   return EINA_TRUE;
}

static void
_entry_del_cb(void *data EINA_UNUSED,
              Evas *evas EINA_UNUSED,
              Evas_Object *obj,
              void *event_info EINA_UNUSED)
{
   regex_t *regex;
   Ecore_Timer *timer;

   regex = evas_object_data_del(obj, "accept_regex");
   if (regex) regfree(regex);

   regex = evas_object_data_del(obj, "deny_regex");
   if (regex) regfree(regex);

   timer = evas_object_data_del(obj, "timer");
   if (timer) ecore_timer_del(timer);

   evas_object_data_del(obj, "min_size");
}

static Evas_Object *
elm_prefs_entry_add(const Elm_Prefs_Item_Iface *iface EINA_UNUSED,
                    Evas_Object *prefs,
                    const Elm_Prefs_Item_Type type,
                    const Elm_Prefs_Item_Spec spec,
                    Elm_Prefs_Item_Changed_Cb cb)
{
   Evas_Object *obj = elm_entry_add(prefs);
   regex_t *regex = NULL;
   char buf[256];

   Elm_Entry_Filter_Limit_Size limit = {
        .max_char_count = spec.s.length.max
   };

   evas_object_data_set(obj, "prefs_type", (void *)type);

   /* FIXME: have this warning animation on the theme, later */

   /* 0: orig. white; 1: 1st red; 2: 2nd white; 3: 2o red */
   evas_object_data_set(obj, "current_color", 0);

   /* FIXME: is it worth to ERR with the item's name, too, here? */

   eo_do(obj, eo_event_callback_add
     (ELM_ENTRY_EVENT_ACTIVATED, _item_changed_cb, cb));
   eo_do(obj, eo_event_callback_add
     (ELM_WIDGET_EVENT_UNFOCUSED, _item_changed_cb, cb));
   if (spec.s.accept)
     {
        int ret;

        regex = calloc(1, sizeof(regex_t));
        ret = regcomp(regex, spec.s.accept, REG_EXTENDED | REG_NOSUB);

        if (ret)
          {
             regerror(ret, regex, buf, sizeof(buf));
             regfree(regex);
             ERR("bad regular expression (%s) on item's 'accept' tag (%s)."
                 " Because of that, the 'accept' tag will be dropped for the "
                 "item.", spec.s.accept, buf);
          }
        else
          evas_object_data_set(obj, "accept_regex", regex);
     }

   if (spec.s.deny)
     {
        int ret;

        regex = calloc(1, sizeof(regex_t));
        ret = regcomp(regex, spec.s.deny, REG_EXTENDED | REG_NOSUB);

        if (ret)
          {
             regerror(ret, regex, buf, sizeof(buf));
             regfree(regex);
             ERR("bad regular expression (%s) on item's 'deny' tag (%s)."
                 " Because of that, the 'deny' tag will be dropped for the "
                 "item.", spec.s.deny, buf);
          }
        else
          evas_object_data_set(obj, "deny_regex", regex);
     }

   if (spec.s.length.min) /* zero makes no sense */
     {
        size_t min = (size_t) spec.s.length.min;
        evas_object_data_set(obj, "min_size", (void *) min);
     }

   evas_object_event_callback_add
     (obj, EVAS_CALLBACK_DEL, _entry_del_cb, NULL);

   elm_entry_scrollable_set(obj, EINA_TRUE);

   if (type == ELM_PREFS_TYPE_TEXT)
     elm_entry_single_line_set(obj, EINA_TRUE);

   elm_entry_markup_filter_append(obj, elm_entry_filter_limit_size, &limit);
   elm_layout_text_set(obj, NULL, spec.s.placeholder);

   return obj;
}

/* already expects an EINA_VALUE_TYPE_STRINGSHARE one */
static Eina_Bool
elm_prefs_entry_value_set(Evas_Object *obj,
                          Eina_Value *value)
{
   const char *val;

   eina_value_get(value, &val);

   return elm_layout_text_set(obj, NULL, val);
}

static Eina_Bool
elm_prefs_entry_value_get(Evas_Object *obj,
                          Eina_Value *value)
{
   const char *val;

   val = elm_layout_text_get(obj, NULL);

   if (!eina_value_setup(value, EINA_VALUE_TYPE_STRINGSHARE))
     return EINA_FALSE;
   if (!eina_value_set(value, val)) return EINA_FALSE;

   return EINA_TRUE;
}

static Eina_Bool
elm_prefs_entry_icon_set(Evas_Object *obj,
                         const char *icon)
{
   Evas_Object *ic = elm_icon_add(obj);

   elm_icon_standard_set(ic, icon);

   return elm_layout_content_set(obj, "icon", ic);
}

static Eina_Bool
elm_prefs_entry_editable_set(Evas_Object *obj,
                             Eina_Bool editable)
{
   elm_entry_editable_set(obj, editable);

   return EINA_TRUE;
}

static Eina_Bool
elm_prefs_entry_editable_get(Evas_Object *obj)
{
   return elm_entry_editable_get(obj);
}

static Eina_Bool
elm_prefs_entry_expand_want(Evas_Object *obj EINA_UNUSED)
{
   return EINA_TRUE;
}

PREFS_ITEM_WIDGET_ADD(entry,
                      supported_types,
                      elm_prefs_entry_value_set,
                      elm_prefs_entry_value_get,
                      elm_prefs_entry_value_validate,
                      NULL,
                      elm_prefs_entry_icon_set,
                      elm_prefs_entry_editable_set,
                      elm_prefs_entry_editable_get,
                      elm_prefs_entry_expand_want);