6 //*** Strong suggetion for mac users to overwrite Intrinsic.h ***
7 //*** Define Boolean as unsigned char ***
9 void functionLoader_Gui(JSGlobalContextRef ctx, JSObjectRef jobjGlobal);
11 JSContextRef jsGlobalCtx;
12 const XtOrientation xawOrientationDirection[]={XtorientHorizontal, XtorientVertical};
17 volatile jsCallbackInfo *myCallBacks;
19 unsigned long getColorFromStr(Display *display, char *color){
22 cmap = DefaultColormap(display,0);
23 XAllocNamedColor(display,cmap,color,&c1,&c0);
27 void backgroundColorChanged(Widget w,char*colorStr){
28 Pixel pixel=getColorFromStr(XtDisplay(w),colorStr);
29 XtVaSetValues(w, XtNbackground,pixel, NULL );
35 #pragma GCC optimization_level 0
36 void xawJsCallbackFunction( Widget w, XtPointer client, XtPointer called )
38 //JSObjectCallAsFunction((*(jsCallbackInfo*)client).Context, (*(jsCallbackInfo*)client).Function, NULL, 0,resources,NULL);
40 #pragma GCC optimization_level reset
42 static JSValueRef jsXawWindowGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
44 JSValueRef result = NULL;
46 size_t len = JSStringGetMaximumUTF8CStringSize(propertyName);
47 char* pname = (char*)malloc(len);
49 JSStringGetUTF8CString(propertyName, pname, len);
50 //printf("%s\n",pname);
52 if (!strcmp(pname, "test")) {
53 result = JSValueMakeNumber(ctx, 1.23);
59 static bool jsXawWindowSetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef jsPropertyName, JSValueRef value, JSValueRef* exception)
62 bool setSuccess = FALSE;
63 size_t len = JSStringGetMaximumUTF8CStringSize(jsPropertyName);
64 char* propertyName = (char*)malloc(len);
65 JSStringGetUTF8CString(jsPropertyName, propertyName, len);
68 if(!strcmp( propertyName, "direction" ))
79 static JSValueRef jsXawWindowShow(
84 const JSObjectRef args[],
89 const int parentObjectID = (int)JSObjectGetPrivate(jobjThis);
91 XtRealizeWidget( myWidgets[parentObjectID] );
92 XtAppMainLoop( app_con );
95 return JSValueMakeUndefined(ctx);
98 static JSObjectRef jsXawWindowConstructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
100 myWidgets = (Widget *)realloc( (void*)myWidgets, sizeof(Widget)*(objIDc+1) );
101 if( myWidgets == NULL )//ポインタが空っぽだったら実行停止。
103 printf( "Memory alloc failed!\n" );
108 /********************************************************************************\
109 Create a whole new class definition for to create new window object with get-set.
110 \********************************************************************************/
112 JSClassDefinition classDefinition = kJSClassDefinitionEmpty;
113 classDefinition.className = "Window";
114 classDefinition.attributes = kJSClassAttributeNoAutomaticPrototype;
115 classDefinition.getProperty = (JSObjectGetPropertyCallback)jsXawWindowGetProperty;
116 classDefinition.setProperty = (JSObjectSetPropertyCallback)jsXawWindowSetProperty;
118 /********************************************************************************\
119 Call XAW to put xwindow instance up.
120 var mywindow = new xawWindow("form", 0);
121 \********************************************************************************/
123 char* windowName = "Untitled Window";
124 double windowDirection = 0;
127 JSStringRef jstrArg = JSValueToStringCopy(ctx, arguments[0], exception);
128 size_t len = JSStringGetMaximumUTF8CStringSize(jstrArg);
129 windowName = (char*)malloc(len);
131 JSStringGetUTF8CString(jstrArg, windowName, len);
132 JSStringRelease(jstrArg);
135 myWidgets[objIDc] = XtVaAppInitialize( &app_con, windowName, NULL, 0, &g_argc, g_argv, NULL, NULL);
137 //Construct a class and create object out of it
138 JSObjectRef windowObj = JSObjectMake(ctx, JSClassCreate(&classDefinition), NULL);
139 functionWrapper(ctx, constructor, "Show", (JSObjectCallAsFunctionCallback)jsXawWindowShow);
140 JSObjectSetPrivate(windowObj, (void*)objIDc);
148 static JSValueRef jsXawFormGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
151 size_t len = JSStringGetMaximumUTF8CStringSize(propertyName);
152 char* pname = (char*)malloc(len);
154 JSStringGetUTF8CString(propertyName, pname, len);
155 printf("%s\n",pname);
157 return JSValueMakeNumber(ctx, 1.23);
159 static bool jsXawFormSetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef jsPropertyName, JSValueRef value, JSValueRef* exception)
162 bool setSuccess = FALSE;
163 size_t len = JSStringGetMaximumUTF8CStringSize(jsPropertyName);
164 char* propertyName = (char*)malloc(len);
165 JSStringGetUTF8CString(jsPropertyName, propertyName, len);
168 if(!strcmp( propertyName, "direction" ))
177 static JSObjectRef jsXawFormConstructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
179 const int parentObjectID = (int)JSObjectGetPrivate(JSValueToObject(ctx,arguments[0],exception));
180 //myWidgets[parentObjectID] is the way of access of parental Widget of the Widget this function is about to create
181 //printf("recieved:%d\n", parentObjectID);
182 //printf("FormConstructor: arg count = %d\n", argumentCount);
184 //Add heap area to accomodate a new Widget
185 myWidgets = (Widget *)realloc( (void*)myWidgets, sizeof(Widget)*(objIDc+1) );
186 if( myWidgets == NULL )//stop if empty pointer for some reason
188 printf( "Memory alloc failed!\n" );
189 return JSValueToObject(ctx, JSValueMakeNull(ctx), exception);
192 /********************************************************************************\
193 Create a whole new class definition for to create new window object with get-set.
194 \********************************************************************************/
196 JSClassDefinition classDefinition = kJSClassDefinitionEmpty;
197 classDefinition.className = "Form";
198 classDefinition.attributes = kJSClassAttributeNoAutomaticPrototype;
199 classDefinition.getProperty = (JSObjectGetPropertyCallback)jsXawFormGetProperty;
200 classDefinition.setProperty = (JSObjectSetPropertyCallback)jsXawFormSetProperty;
203 /********************************************************************************\
204 Call XAW to put xwindow instance up.
205 var form = new xawForm(mywindow, "form", 0);
206 \********************************************************************************/
209 char* formName = "Untitled form";
210 double formDirection = 0;
213 if (argumentCount >= 2) {
214 JSStringRef jstrArg = JSValueToStringCopy(ctx, arguments[1], exception);
215 size_t len = JSStringGetMaximumUTF8CStringSize(jstrArg);
216 formName = (char*)malloc(len);
218 JSStringGetUTF8CString(jstrArg, formName, len);
219 JSStringRelease(jstrArg);
222 XtOrientation o = XtorientVertical;
223 if (argumentCount == 3) {
224 formDirection = JSValueToNumber(ctx, arguments[2], exception);
225 o = (formDirection==0)?XtorientVertical:XtorientHorizontal;
228 myWidgets[objIDc] = XtVaCreateManagedWidget(formName, formWidgetClass, myWidgets[parentObjectID], XtNorientation, o, NULL);
230 //Construct a class and create object out of it, send it back to JavaScript.
231 JSObjectRef formObj = JSObjectMake(ctx, JSClassCreate(&classDefinition), NULL);
232 JSObjectSetPrivate(formObj, (void*)objIDc);
239 static JSValueRef jsXawLabelGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef jsPropertyName, JSValueRef* exception)
241 int objectID = (int)JSObjectGetPrivate(object);
243 JSValueRef result = JSValueMakeUndefined(ctx);
245 size_t len = JSStringGetMaximumUTF8CStringSize(jsPropertyName);
246 char* propertyName = (char*)malloc(len);
248 JSStringGetUTF8CString(jsPropertyName, propertyName, len);
250 if(!strcmp( propertyName, "Text" ))
254 XtSetArg(args[0],XtNlabel,(XtArgVal)&str);
255 XtGetValues(myWidgets[objectID],args,1);
256 JSStringRef mystring = JSStringCreateWithUTF8CString(str);
257 result = JSValueMakeString(ctx, mystring);
265 static bool jsXawLabelSetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef jsPropertyName, JSValueRef value, JSValueRef* exception)
267 int objectID = (int)JSObjectGetPrivate(object);
268 //printf("jsXawWidget[%d].Set.", objectID );
269 bool setSuccess = FALSE;
270 size_t len = JSStringGetMaximumUTF8CStringSize(jsPropertyName);
271 char* propertyName = (char*)malloc(len);
272 JSStringGetUTF8CString(jsPropertyName, propertyName, len);
278 if(!strcmp( propertyName, "direction" ))
281 }else if(!strcmp( propertyName, "Text" ))
283 JSStringRef strOfSetValueTo = JSValueToStringCopy(ctx, value, *exception);
284 len = JSStringGetMaximumUTF8CStringSize(strOfSetValueTo);
285 char* strOfSetTo = (char*)malloc(len);
286 JSStringGetUTF8CString(strOfSetValueTo, strOfSetTo, len);
289 XtSetArg(args[0],XtNlabel,(XtArgVal)strOfSetTo);
290 XtSetValues(myWidgets[objectID],args,1);
291 //XClearArea(XtDisplay(myWidgets[objectID]), XtWindow(myWidgets[objectID]), 0, 0, 0, 0, True);//NG. BadWindow
293 }else if((!strcmp( propertyName, "Left" )) && (JSValueIsNumber(ctx, value)))
296 XtSetArg(args[0],XtNhorizDistance,(XtArgVal)((int)JSValueToNumber(ctx, value, *exception)));
297 XtSetValues(myWidgets[objectID],args,1);
299 }else if((!strcmp( propertyName, "Top" )) && (JSValueIsNumber(ctx, value)))
302 XtSetArg(args[0],XtNvertDistance,(XtArgVal)((int)JSValueToNumber(ctx, value, *exception)));
303 XtSetValues(myWidgets[objectID],args,1);
305 }else if(!strcmp( propertyName, "Location" ))
314 static JSObjectRef jsXawWidgetConstructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
316 const int parentObjectID = (int)JSObjectGetPrivate(JSValueToObject(ctx,arguments[0],exception));
317 //myWidgets[parentObjectID] is the way of access of parental Widget of the Widget this function is about to create
318 //printf("recieved:%d\n", parentObjectID);
319 //printf("WidgetConstructor: arg count = %d\n", argumentCount);
321 //Add heap area to accomodate a new Widget
322 myWidgets = (Widget *)realloc( (void*)myWidgets, sizeof(Widget)*(objIDc+1) );
323 if( myWidgets == NULL )//stop if empty pointer for some reason
325 printf( "Memory alloc failed!\n" );
326 return JSValueToObject(ctx, JSValueMakeNull(ctx), exception);
329 if (argumentCount >= 2)
331 char widgetName[8]={'W','i','d','g','e','t','_',(char)objIDc+48};
332 printf("Untitled Widget%d on %d. Initializing...\n", objIDc, parentObjectID);
333 double widgetDirection = 0;
335 /********************************************************************************\
336 Create a whole new class definition for to create new window object with get-set.
337 \********************************************************************************/
339 JSClassDefinition classDefinition = kJSClassDefinitionEmpty;
340 classDefinition.className = "Widget";
341 classDefinition.attributes = kJSClassAttributeNoAutomaticPrototype;
342 JSObjectRef widgetObj = NULL;
344 /********************************************************************************\
345 Determine what kind of widget is required to be made
346 \********************************************************************************/
348 if((argumentCount == 2) && JSValueIsString(ctx, arguments[1]))
350 printf("Label selected for widget %d on %d.\n", objIDc,parentObjectID);
353 classDefinition.getProperty = (JSObjectGetPropertyCallback)jsXawLabelGetProperty;
354 classDefinition.setProperty = (JSObjectSetPropertyCallback)jsXawLabelSetProperty;
356 /********************************************************************************\
357 Call XAW to put xwindow instance up.
358 var Widget = new xawWidget(mywindow, "Widget", 0);
359 \********************************************************************************/
362 JSStringRef jstrArg = JSValueToStringCopy(ctx, arguments[1], exception);
363 size_t len = JSStringGetMaximumUTF8CStringSize(jstrArg);
364 char* text = (char*)malloc(len);
366 JSStringGetUTF8CString(jstrArg, text, len);
367 JSStringRelease(jstrArg);
370 myWidgets[objIDc] = XtVaCreateManagedWidget(widgetName, labelWidgetClass, myWidgets[parentObjectID], XtNlabel, text, NULL);
372 //Construct a class and create object out of it, send it back to JavaScript.
373 widgetObj = JSObjectMake(ctx, JSClassCreate(&classDefinition), NULL);
374 JSObjectSetPrivate(widgetObj, (void*)objIDc);
378 }else if((argumentCount == 3) && JSValueIsString(ctx, arguments[1]))
380 printf("Button selected for widget %d on %d.\n", objIDc,parentObjectID);
383 classDefinition.getProperty = (JSObjectGetPropertyCallback)jsXawLabelGetProperty;
384 classDefinition.setProperty = (JSObjectSetPropertyCallback)jsXawLabelSetProperty;
386 /********************************************************************************\
387 Call XAW to put xwindow instance up.
388 var Widget = new xawWidget(mywindow, "Widget", 0);
389 \********************************************************************************/
392 JSStringRef jstrArg = JSValueToStringCopy(ctx, arguments[1], exception);
393 size_t len = JSStringGetMaximumUTF8CStringSize(jstrArg);
394 char* text = (char*)malloc(len);
396 JSStringGetUTF8CString(jstrArg, text, len);
397 JSStringRelease(jstrArg);
400 myWidgets[objIDc] = XtVaCreateManagedWidget(widgetName, commandWidgetClass, myWidgets[parentObjectID], XtNlabel, text, NULL);
402 /*memmove(timeOutCallback, &((jsCallbackInfo){ctx, arguments[2]}), sizeof(jsCallbackInfo));
404 JSObjectCallAsFunction(ctx, arguments[2], NULL, 0,NULL,NULL);
406 XtAppAddTimeOut(app_con, (unsigned long)JSValueToNumber( ctx, args[1], jobjExp), (XtTimerCallbackProc)jsXawAppAddTimeOutCallback,(XtPointer)timeOutCallback);
409 //Construct a class and create object out of it, send it back to JavaScript.
410 widgetObj = JSObjectMake(ctx, JSClassCreate(&classDefinition), NULL);
411 JSObjectSetPrivate(widgetObj, (void*)objIDc);
416 printf("Nothing selected for widget %d on %d.\n", objIDc,parentObjectID);
422 //XtAddCallback( myWidgets[objIDc], XtNcallback, xawJsCallbackFunction, callinfo );
429 #pragma GCC optimization_level 0
430 void jsXawAppAddTimeOutCallback(XtPointer client)
432 jsCallbackInfo* timeOutCallback = (jsCallbackInfo*)client;
433 assert(((*timeOutCallback).Context != NULL) && ((*timeOutCallback).Function != NULL) );
434 //printf("timeout.\n");
435 JSContextRef ctx = jsGlobalCtx;//timeOutCallback->Context does not work.
436 const JSObjectRef f = timeOutCallback->Function;
438 if (JSValueIsString(ctx, f)) {
439 JSStringRef jstrSource = JSValueToStringCopy(ctx, f, NULL);
440 JSEvaluateScript(ctx, jstrSource, NULL, NULL, 1, NULL);
441 JSStringRelease(jstrSource);
443 JSObjectCallAsFunction(jsGlobalCtx, timeOutCallback->Function , NULL, 0,NULL,NULL);
446 free(timeOutCallback);
447 timeOutCallback = NULL;
450 #pragma GCC optimization_level reset
452 static JSValueRef jsXawAppAddTimeOut(
455 JSObjectRef jobjThis,
457 const JSObjectRef args[],
460 if ( argLen == 2 && JSValueIsNumber(ctx,args[1]) ) {
461 jsCallbackInfo* timeOutCallback = (jsCallbackInfo *)malloc( sizeof(jsCallbackInfo));
462 if( timeOutCallback == NULL )//stop if empty pointer for some reason
464 printf( "Memory alloc failed!\n" );
465 return JSValueToObject(ctx, JSValueMakeNull(ctx), jobjExp);
468 memmove(timeOutCallback, &((jsCallbackInfo){ctx, args[0]}), sizeof(jsCallbackInfo));
469 XtAppAddTimeOut(app_con, (unsigned long)JSValueToNumber( ctx, args[1], jobjExp), (XtTimerCallbackProc)jsXawAppAddTimeOutCallback,(XtPointer)timeOutCallback);
472 return JSValueMakeUndefined(ctx);
476 void functionLoader_Gui(JSGlobalContextRef ctx, JSObjectRef jobjGlobal)
478 assert(sizeof( ( (void*)1 ) ) >= sizeof( (int)1 ) );
479 JSObjectRef Window, Form;
480 classWrapper(ctx, jobjGlobal, "xawWindow", &Window, jsXawWindowConstructor);\
481 classWrapper(ctx, jobjGlobal, "xawForm", &Form, jsXawFormConstructor);
482 classWrapper(ctx, jobjGlobal, "xawWidget", &Form, jsXawWidgetConstructor);
484 functionWrapper(ctx, jobjGlobal, "setTimeout", (JSObjectCallAsFunctionCallback)jsXawAppAddTimeOut);