summaryrefslogtreecommitdiffstats
path: root/friendfinder/vkbd/vkbd.c
blob: de7d093d76730ec1b2cfade14544018e318611c5 (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
#include <stdio.h>
#include <string.h>

#include <Evas.h>
#include <Edje.h>
#include <Edje_Edit.h>
#include "vkbd.h"

static Evas_Smart *get_smart();

static void _kbd_object_add(Evas_Object *o);
static void _kbd_object_del(Evas_Object *o);
static void _kbd_object_move(Evas_Object *o, Evas_Coord x, Evas_Coord y);
static void _kbd_object_resize(Evas_Object *o, Evas_Coord w, Evas_Coord h);
static void _kbd_object_show(Evas_Object *o);
static void _kbd_object_hide(Evas_Object *o);
static void _kbd_object_color_set(Evas_Object *o, int r, int g, int b, int a);
static void _kbd_object_clip_set(Evas_Object *o, Evas_Object *clip);
static void _kbd_object_clip_unset(Evas_Object *o);

#define SMART_KBD_NAME "e_smart_kbd"
#define INPUT_MAX 512
struct smart_data
{
	Evas_Object *obj;
	Evas_Coord x, y, w, h;
	
	Evas_Object *clip;
	Evas_Object *edje;
	char input[INPUT_MAX];
	int input_len;
	void (*kbd_cb)(char *);
};


static void
mouse_down(void *data, Evas_Object *o, const char *emission, const char *source)
{
	int i;
	const char *key;
	struct smart_data *smart = (struct smart_data *)data;
	if(!smart || smart->input_len >= INPUT_MAX - 1)
		return;
	
	int len = strlen(source);
	for(i = len - 2; i >= 0; i--)
	{
		if(source[i] == '-')
			break;
	}
	key = source + i+1;
	if(strlen(key) == 1)
	{
		smart->input[smart->input_len++] = key[0];
		smart->input[smart->input_len] = '\0';
	}
	else if (strlen(key) == 2 && (key[0] & 0xff) == 0xc3)
	{
		smart->input[smart->input_len++] = key[0];
		smart->input[smart->input_len++] = key[1];
		smart->input[smart->input_len] = '\0';
	}
	else
	{
		int len;
		char *val = edje_edit_data_value_get (o, key);
		if(!val)
			return;
		len = INPUT_MAX - smart->input_len - 2;
		len = (strlen(val) > len) ? len : strlen(val); 
		strncat(smart->input + smart->input_len, val, len);
		smart->input_len += len;
	}
	edje_object_part_text_set(o, "field", smart->input);	
}

static void
kbd_cb(void *data, Evas_Object *o, const char *emission, const char *source)
{
	struct smart_data *smart;
	Evas_Object *kbd = (Evas_Object *)data;
	if(!kbd)
		return;
	smart = evas_object_smart_data_get(kbd);
	if(strcmp(source, "hide") == 0)
	{
		if(smart->kbd_cb)
			smart->kbd_cb(strdup(smart->input));
		smart->input[0] = 0;
		smart->input_len = 0;
		smart->kbd_cb = NULL;
	}
	printf("kbd_cb source: %s, emission %s\n", source, emission);
	evas_object_hide(kbd);
}

Evas_Object *vkbd_add(Evas *e)
{
	Evas_Object *o;
	struct smart_data *smart;
	Evas_Object *kbd;
	
	o = evas_object_smart_add(e, get_smart());
	smart = evas_object_smart_data_get(o);
	if(smart == NULL)
	{
		evas_object_del(o);
		return NULL;
	}

	kbd = edje_object_add(e);
	if(!edje_object_file_set(kbd, "vkbd/vkbd.edj", "main"))
	{
		printf("failed loading kbd\n");
		evas_object_del(kbd);
		evas_object_del(o);
		return NULL;
	}
	evas_object_clip_set(kbd, smart->clip);
	edje_object_signal_callback_add(kbd, "mouse,down,1", "*key-bg*", mouse_down, smart);
	edje_object_signal_callback_add(kbd, "kbd", "*", kbd_cb, o);
	evas_object_show(kbd);

	smart->input[0] = '\0';
	smart->input_len = 0;
	smart->kbd_cb = NULL;
	
	smart->edje = kbd;
	return o;
}

void vkbd_show(Evas_Object *o, Evas_Coord w, Evas_Coord h, void (*cb)(char *))
{	
	struct smart_data *smart;
	smart = evas_object_smart_data_get(o);
	smart->kbd_cb = cb;
	evas_object_resize(o, w, h);
	evas_object_show(o);	
}

static void _kbd_object_add(Evas_Object *o)
{
	struct smart_data *smart;

	smart = calloc(1, sizeof(struct smart_data));
	if (smart == NULL)
		return;

	smart->obj = o;
	smart->x = 0;
	smart->y = 0;
	smart->w = 0;
	smart->h = 0;

	smart->clip = evas_object_rectangle_add(evas_object_evas_get(o));
	evas_object_smart_member_add(smart->clip, o);
	evas_object_color_set(smart->clip, 255, 255, 255, 255);
	evas_object_move(smart->clip, smart->x, smart->y);
	evas_object_resize(smart->clip, smart->w, smart->h);
	
	evas_object_smart_data_set(o, smart);
}

static void _kbd_object_del(Evas_Object *o)
{
	struct smart_data *smart;
	smart = evas_object_smart_data_get(o);

	evas_object_del(smart->clip);
	evas_object_del(smart->edje);
	free(smart);
}

static void _kbd_object_move(Evas_Object *o, Evas_Coord x, Evas_Coord y)
{
	struct smart_data *smart;
	smart = evas_object_smart_data_get(o); 

	smart->x = x;
	smart->y = y;
	
	evas_object_move(smart->clip, x, y);
	evas_object_move(smart->edje, x, y);
}

static void _kbd_object_resize(Evas_Object *o, Evas_Coord w, Evas_Coord h)
{
	struct smart_data *smart;
	smart = evas_object_smart_data_get(o);

	smart->w = w;
	smart->h = h;
	evas_object_resize(smart->clip, w, h);
	evas_object_resize(smart->edje, w, h);
}

static void _kbd_object_show(Evas_Object *o)
{
	struct smart_data *smart;
	smart = evas_object_smart_data_get(o);

	evas_object_show(smart->clip);
}

static void _kbd_object_hide(Evas_Object *o)
{
	struct smart_data *smart;
	smart = evas_object_smart_data_get(o);
	evas_object_hide(smart->clip);
}

static void _kbd_object_color_set(Evas_Object *o, int r, int g, int b, int a)
{
	struct smart_data *smart;
	smart = evas_object_smart_data_get(o);
	evas_object_color_set(smart->clip, r, g, b, a);
}

static void _kbd_object_clip_set(Evas_Object *o, Evas_Object *clip)
{
	struct smart_data *smart;
	smart = evas_object_smart_data_get(o);
	
	evas_object_clip_set(smart->clip, clip);
}

static void _kbd_object_clip_unset(Evas_Object *o)
{
	struct smart_data *smart;
	smart = evas_object_smart_data_get(o);

	evas_object_clip_unset(smart->clip);
}

static Evas_Smart *get_smart()
{
	static Evas_Smart *e_smart_kbd = NULL;
	
	if(e_smart_kbd != NULL)
		return e_smart_kbd;

	e_smart_kbd = evas_smart_new(SMART_KBD_NAME,
		_kbd_object_add,
		_kbd_object_del,
		NULL,
		NULL,
		NULL,
		NULL,
		NULL,
		_kbd_object_move,
		_kbd_object_resize,
		_kbd_object_show,
		_kbd_object_hide,
		_kbd_object_color_set,
		_kbd_object_clip_set,
		_kbd_object_clip_unset,
		NULL);
	return e_smart_kbd;
}