summaryrefslogtreecommitdiff
path: root/plugins/Skins/SkinLib/V8Wrappers.cpp
blob: 9384e3c8ad7b9a51f8708ea3be97c6c0e969f3ba (plain)
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
#include "globals.h"
#include "V8Wrappers.h"

#include <utf8_helpers.h>


using namespace v8;


#ifdef UNICODE
# define V8_TCHAR uint16_t
#else
# define V8_TCHAR char
#endif



static Handle<Value> IsEmptyCallback(const Arguments& args)
{
	HandleScope scope;
	
	if (args.Length() < 1) 
		return scope.Close( Undefined() );

	for(int i = 0; i < args.Length(); i++)
	{
		Local<Value> arg = args[0];

		if (arg.IsEmpty() || arg->IsNull() || arg->IsUndefined())
		{
			return scope.Close( Boolean::New(true) );
		}
		else if (arg->IsObject())
		{
			Local<Object> self = Local<Object>::Cast(arg);
			if (self->InternalFieldCount() < 1)
				continue;

			Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
			FieldState *field = (FieldState *) wrap->Value();
			if (field == NULL)
				continue;

			if (field->isEmpty())
				return scope.Close( Boolean::New(true) );
		}
		else if (arg->IsString())
		{
			Local<String> str = Local<String>::Cast(arg);
			if (str->Length() <= 0)
				return scope.Close( Boolean::New(true) );
		}
	}

	return scope.Close( Boolean::New(false) );
}

static Handle<Value> RGBCallback(const Arguments& args)
{
	HandleScope scope;
	
	if (args.Length() != 3) 
		return scope.Close( Undefined() );

	COLORREF color = RGB(args[0]->Int32Value(), args[1]->Int32Value(), args[2]->Int32Value());
	return scope.Close( Int32::New(color) );
}

static Handle<Value> AlertCallback(const Arguments& args)
{
	HandleScope scope;

	if (args.Length() < 1) 
		return scope.Close( Boolean::New(false) );

	Local<Value> arg = args[0];
	String::Utf8Value utf8_value(arg->ToDetailString());

	char *tmp = *utf8_value;
// TODO 	MessageBox(NULL, Utf8ToTchar(*utf8_value), _T("Skin alert"), MB_OK);

	return scope.Close( Boolean::New(true) );
}

void V8Wrappers::addGlobalTemplateFields(Handle<ObjectTemplate> &templ)
{
	HandleScope scope;

	templ->Set(String::New("IsEmpty"), FunctionTemplate::New(&IsEmptyCallback));
	templ->Set(String::New("RGB"), FunctionTemplate::New(&RGBCallback));
	templ->Set(String::New("alert"), FunctionTemplate::New(&AlertCallback));
}


Handle<Object> V8Wrappers::newState(FieldType type)
{
	switch(type)
	{
		case SIMPLE_TEXT:
			return newTextFieldState();
		case SIMPLE_IMAGE:
			return newImageFieldState();
		case SIMPLE_ICON:
			return newIconFieldState();
		case CONTROL_LABEL:
			return newLabelFieldState();
		case CONTROL_BUTTON:
			return newButtonFieldState();
		case CONTROL_EDIT:
			return newEditFieldState();
	}
	throw "Unknown type";
}

void V8Wrappers::fillState(Handle<Object> obj, FieldState *state)
{
	switch(state->getField()->getType())
	{
		case SIMPLE_TEXT:
			fillTextFieldState(obj, (TextFieldState *) state);
			break;
		case SIMPLE_IMAGE:
			fillImageFieldState(obj, (ImageFieldState *) state);
			break;
		case SIMPLE_ICON:
			fillIconFieldState(obj, (IconFieldState *) state);
			break;
		case CONTROL_LABEL:
			fillLabelFieldState(obj, (LabelFieldState *) state);
			break;
		case CONTROL_BUTTON:
			fillButtonFieldState(obj, (ButtonFieldState *) state);
			break;
		case CONTROL_EDIT:
			fillEditFieldState(obj, (EditFieldState *) state);
			break;
		default:
			throw "Unknown type";
	}
}

static Handle<Value> Get_SkinOption_value(SkinOption *opt) 
{
	HandleScope scope;
	
	if (opt == NULL)
		return scope.Close( Undefined() );

	switch (opt->getType())
	{
		case CHECKBOX:	return scope.Close( Boolean::New(opt->getValueCheckbox()) );
		case NUMBER:	return scope.Close( Int32::New(opt->getValueNumber()) );
		case TEXT:		return scope.Close( String::New((const V8_TCHAR *) opt->getValueText()) );
	}

	return scope.Close( Undefined() );
}

static Handle<Value> Get_Options_Fields(Local<String> aName, const AccessorInfo &info)
{
	HandleScope scope;

	Local<Object> self = info.Holder();
	Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
	if (wrap.IsEmpty())
		return scope.Close( Undefined() );

	SkinOptions *opts = (SkinOptions *) wrap->Value();
	if (opts == NULL)
		return scope.Close( Undefined() );

	String::AsciiValue name(aName);
	if (name.length() <= 0)
		return scope.Close( Undefined() );

	bool configure = self->GetInternalField(1)->BooleanValue();
	if (configure)
	{
		SkinOption * opt = opts->getOption(*name);

		if (opt == NULL)
		{
			opt = new SkinOption(*name);
			opts->addOption(opt);
		}

		wrap = Local<External>::Cast(info.Data());
		if (wrap.IsEmpty())
			return scope.Close( Undefined() );

		V8Wrappers *wrappers = (V8Wrappers *) wrap->Value();
		if (wrappers == NULL)
			return scope.Close( Undefined() );

		Handle<Object> ret = wrappers->newSkinOption();
		wrappers->fillSkinOption(ret, opt);
		return scope.Close( ret );
	}
	else
	{
		SkinOption * opt = opts->getOption(*name);
		return scope.Close( Get_SkinOption_value(opt) );
	}
}

Handle<ObjectTemplate> V8Wrappers::getOptionsTemplate()
{
	HandleScope scope;
	
	if (!optionsTemplate.IsEmpty())
		return optionsTemplate; 
	
	Handle<ObjectTemplate> templ = ObjectTemplate::New();
	templ->SetInternalFieldCount(2);
	templ->SetNamedPropertyHandler(&Get_Options_Fields, 0, 0, 0, 0, External::New(this));
	
	optionsTemplate = Persistent<ObjectTemplate>::New(templ);
	
	return optionsTemplate;
}

Handle<Object> V8Wrappers::newOptions()
{
	HandleScope scope;
	
	Handle<Object> obj = getOptionsTemplate()->NewInstance();
	
	return scope.Close(obj);
}

void V8Wrappers::fillOptions(Handle<Object> v8Obj, SkinOptions *obj, bool configure)
{
	HandleScope scope;
	
	_ASSERT(!v8Obj.IsEmpty());

	v8Obj->SetInternalField(0, External::New(obj));
	v8Obj->SetInternalField(1, Boolean::New(configure));
}


static Handle<Value> Get_SkinOption_value(Local<String> property, const AccessorInfo &info) 
{
	HandleScope scope;
	
	Local<Object> self = info.Holder();
	Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
	if (wrap.IsEmpty())
		return scope.Close( Undefined() );

	return scope.Close( Get_SkinOption_value((SkinOption *) wrap->Value()) );
}

static void Set_SkinOption_value(Local<String> property, Local<Value> value, const AccessorInfo& info) 
{
	HandleScope scope;
	
	Local<Object> self = info.Holder();
	Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
	if (wrap.IsEmpty())
		return;

	SkinOption *opt = (SkinOption *) wrap->Value();
	if (opt == NULL)
		return;

	switch (opt->getType())
	{
		case CHECKBOX:	
			if (!value.IsEmpty() && value->IsBoolean())
				opt->setValueCheckbox(value->BooleanValue());
			break;
		case NUMBER:
			if (!value.IsEmpty() && value->IsNumber())
				opt->setValueNumber(value->Int32Value());
			break;
		case TEXT:		
			if (!value.IsEmpty() && value->IsString())
				opt->setValueText(Utf8ToTchar(*String::Utf8Value(value)));
			break;
	}
}

void V8Wrappers::addSkinOptionTemplateFields(Handle<ObjectTemplate> &templ)
{
	HandleScope scope;
	
	templ->SetAccessor(String::New("value"), Get_SkinOption_value, Set_SkinOption_value);
}


static Handle<Value> Get_DialogInfo_Fields(Local<String> aName, const AccessorInfo &info)
{
	HandleScope scope;

	Local<Object> self = info.Holder();
	Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
	if (wrap.IsEmpty())
		return scope.Close( Undefined() );

	DialogInfo *dialogInfo = (DialogInfo *) wrap->Value();
	if (dialogInfo == NULL)
		return scope.Close( Undefined() );

	String::AsciiValue name(aName);
	if (name.length() <= 0)
		return scope.Close( Undefined() );

	wrap = Local<External>::Cast(info.Data());
	if (wrap.IsEmpty())
		return scope.Close( Undefined() );

	V8Wrappers *wrappers = (V8Wrappers *) wrap->Value();
	if (wrappers == NULL)
		return scope.Close( Undefined() );

	Local<String> aPrefix = Local<String>::Cast(self->GetInternalField(1));
	if (aPrefix.IsEmpty())
		return scope.Close( Undefined() );

	String::AsciiValue prefix(aPrefix);
	
	std::string var;
	var += *prefix;
	var += *name;

	switch(dialogInfo->getType(var.c_str()))
	{
		case TYPE_VARIABLE:
		{
			var += '.';

			Handle<Object> ret = wrappers->newDialogInfo();
			wrappers->fillDialogInfo(ret, dialogInfo, var.c_str());
			return scope.Close( ret );
		}
		case TYPE_INT:
			return scope.Close( Int32::New(dialogInfo->getAsInt(var.c_str())) );
		case TYPE_DOUBLE:
			return scope.Close( Number::New(dialogInfo->getAsDouble(var.c_str())) );
		case TYPE_BOOL:
			return scope.Close( Boolean::New(dialogInfo->getAsBool(var.c_str())) );
		case TYPE_STRING:
			return scope.Close( String::New((const V8_TCHAR *) dialogInfo->getAsString(var.c_str())) );
		case UNKNOWN:
		default:
			return scope.Close( Undefined() );
	}
}

Handle<ObjectTemplate> V8Wrappers::getDialogInfoTemplate()
{
	HandleScope scope;
	
	if (!dialogInfoTemplate.IsEmpty())
		return dialogInfoTemplate; 
	
	Handle<ObjectTemplate> templ = ObjectTemplate::New();
	templ->SetInternalFieldCount(2);
	templ->SetNamedPropertyHandler(&Get_DialogInfo_Fields, 0, 0, 0, 0, External::New(this));
	
	dialogInfoTemplate = Persistent<ObjectTemplate>::New(templ);
	
	return dialogInfoTemplate;
}

Handle<Object> V8Wrappers::newDialogInfo()
{
	HandleScope scope;
	
	Handle<Object> obj = getDialogInfoTemplate()->NewInstance();
	
	return scope.Close(obj);
}

void V8Wrappers::fillDialogInfo(Handle<Object> v8Obj, DialogInfo *obj, const char *prefix)
{
	HandleScope scope;
	
	_ASSERT(!v8Obj.IsEmpty());

	v8Obj->SetInternalField(0, External::New(obj));

	v8Obj->SetInternalField(1, String::New(prefix != NULL ? prefix : ""));
}