OSDN Git Service

Wrote attribute get-er of Label. Some progress on button as well.
authorYoshinobu Date <admin@hackerzlife.com>
Wed, 15 Jun 2011 20:18:01 +0000 (06:18 +1000)
committerYoshinobu Date <admin@hackerzlife.com>
Wed, 15 Jun 2011 20:18:01 +0000 (06:18 +1000)
modified:   xaw.c

test.js
xaw.c

diff --git a/test.js b/test.js
index 6ea0347..bf27e39 100644 (file)
--- a/test.js
+++ b/test.js
@@ -18,6 +18,7 @@ var chtxt = function (){
 var mywindow = new xawWindow("Test");
 var form = new xawForm(mywindow, "form", 0);
 var label = new xawWidget(form, "Hello world!\n"+d);
+var button = new xawWidget(form, "Klick!", "chtxt()");
 print("objects ready.\n");
 count();
 mywindow.Show();
diff --git a/xaw.c b/xaw.c
index 2787f51..f1d3f72 100644 (file)
--- a/xaw.c
+++ b/xaw.c
@@ -237,16 +237,31 @@ static JSObjectRef jsXawFormConstructor(JSContextRef ctx, JSObjectRef constructo
        return formObj;
 }
 
-static JSValueRef jsXawLabelGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
+static JSValueRef jsXawLabelGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef jsPropertyName, JSValueRef* exception)
 {
-       printf("Get:");
-       size_t          len     = JSStringGetMaximumUTF8CStringSize(propertyName);
-       char* pname   =  (char*)malloc(len);
+       int objectID = (int)JSObjectGetPrivate(object);
        
-       JSStringGetUTF8CString(propertyName, pname, len);
-       printf("%s\n",pname);
-       free(pname);
-       return JSValueMakeNumber(ctx, 1.23);
+       JSValueRef result = JSValueMakeUndefined(ctx);
+       
+       size_t          len     = JSStringGetMaximumUTF8CStringSize(jsPropertyName);
+       char* propertyName   =  (char*)malloc(len);
+       
+       JSStringGetUTF8CString(jsPropertyName, propertyName, len);
+
+       if(!strcmp( propertyName, "Text" ))
+       {
+               char *str;
+               Arg args[1];
+               XtSetArg(args[0],XtNlabel,(XtArgVal)&str);
+               XtGetValues(myWidgets[objectID],args,1);
+               JSStringRef mystring = JSStringCreateWithUTF8CString(str);
+               result = JSValueMakeString(ctx, mystring);
+               free(str);
+       }
+       
+       
+       free(propertyName);
+       return result;
 }
 static bool jsXawLabelSetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef jsPropertyName, JSValueRef value, JSValueRef* exception)
 {
@@ -297,7 +312,7 @@ static JSObjectRef jsXawWidgetConstructor(JSContextRef ctx, JSObjectRef construc
        if (argumentCount >= 2)
        {
                char widgetName[8]={'W','i','d','g','e','t','_',(char)objIDc+48};
-               printf("Untitled Widget%d on %d", objIDc, parentObjectID);
+               printf("Untitled Widget%d on %d. Initializing...\n", objIDc, parentObjectID);
                double widgetDirection = 0;
                
                /********************************************************************************\
@@ -340,6 +355,40 @@ static JSObjectRef jsXawWidgetConstructor(JSContextRef ctx, JSObjectRef construc
                        //Construct a class and create object out of it, send it back to JavaScript.
                        widgetObj = JSObjectMake(ctx, JSClassCreate(&classDefinition), NULL);
                        JSObjectSetPrivate(widgetObj, (void*)objIDc);
+                       free(text);
+                       text = NULL;
+                       
+               }else if((argumentCount == 3) && JSValueIsString(ctx, arguments[1]))
+               {//Button
+                       printf("Button selected for widget %d on %d.\n", objIDc,parentObjectID);
+                       
+                       
+                       classDefinition.getProperty = (JSObjectGetPropertyCallback)jsXawLabelGetProperty;
+                       classDefinition.setProperty = (JSObjectSetPropertyCallback)jsXawLabelSetProperty;
+                       
+                       /********************************************************************************\
+                        Call XAW to put xwindow instance up.
+                        var Widget = new xawWidget(mywindow, "Widget", 0);
+                        \********************************************************************************/
+                       
+                       
+                       JSStringRef     jstrArg = JSValueToStringCopy(ctx, arguments[1], exception);
+                       size_t          len     = JSStringGetMaximumUTF8CStringSize(jstrArg);
+                       char* text   =  (char*)malloc(len);
+                       
+                       JSStringGetUTF8CString(jstrArg, text, len);
+                       JSStringRelease(jstrArg);
+                       
+                       
+                       myWidgets[objIDc] = XtVaCreateManagedWidget(widgetName, commandWidgetClass, myWidgets[parentObjectID], XtNlabel, text, 
+                                                                                                               XtNhorizDistance,50,
+                                                                                                               XtNvertDistance,50, NULL);
+                       
+                       //Construct a class and create object out of it, send it back to JavaScript.
+                       widgetObj = JSObjectMake(ctx, JSClassCreate(&classDefinition), NULL);
+                       JSObjectSetPrivate(widgetObj, (void*)objIDc);
+                       free(text);
+                       text = NULL;
                        
                }else {
                        printf("Nothing selected for widget %d on %d.\n", objIDc,parentObjectID);
@@ -386,10 +435,8 @@ static JSValueRef jsXawAppAddTimeOut(
                                                                         const JSObjectRef   args[],
                                                                         JSValueRef*         jobjExp)
 {
-       
-       //Add heap area to accomodate a new Widget
        jsCallbackInfo* timeOutCallback = (jsCallbackInfo *)malloc( sizeof(jsCallbackInfo));
-       if( myWidgets == NULL )//stop if empty pointer for some reason
+       if( timeOutCallback == NULL )//stop if empty pointer for some reason
        {
                printf( "Memory alloc failed!\n" );
                return JSValueToObject(ctx, JSValueMakeNull(ctx), jobjExp);