OSDN Git Service

modified the method to be executed by clicking a script button, and added variables...
authormshio <mshio@54a90f34-5e62-402c-8eae-46c47f0b2e07>
Sun, 20 Feb 2011 12:58:02 +0000 (12:58 +0000)
committermshio <mshio@54a90f34-5e62-402c-8eae-46c47f0b2e07>
Sun, 20 Feb 2011 12:58:02 +0000 (12:58 +0000)
git-svn-id: svn+ssh://svn.osdn.net/svnroot/sawarabi-fonts/trunk@22 54a90f34-5e62-402c-8eae-46c47f0b2e07

chartool/objective-c/Controller.m
chartool/objective-c/JavaScriptEngine.h
chartool/objective-c/JavaScriptEngine.m

index 0620bcc..7f96e3c 100644 (file)
 
 - (IBAction) clickScriptButton: (id) sender {
        int tag = [sender tag];
-       NSString* str = tag == 0 ? [codeField stringValue] : [nameField stringValue];
+       NSString* p = [tag == 0 ? codeField : nameField stringValue];
        NSUInteger m = [NSEvent modifierFlags];
-       NSString* script = [NSString stringWithFormat: 
-                                               @"script[%d]('%@', %d);", tag, str, m];
-       [engine evaluate: script];
+       [engine executeScriptAtIndex: tag withProperty: p AndModifier: m];
 }
 
 - (IBAction) copyGlyphCharacter: (id) sender {
index 50dc49d..2609f1c 100644 (file)
@@ -16,5 +16,6 @@
 + (JavaScriptEngine *) instance;
 - (NSString *) evaluate: (NSString *) script;
 - (NSArray *) getFontsNameArray;
+- (void) executeScriptAtIndex: (int) index withProperty: (NSString*) property AndModifier: (int) modifier;
 
 @end
index f0a5878..f2a9192 100644 (file)
@@ -75,11 +75,21 @@ static JSValueRef jsOpenFile(JSContextRef ctx,
        JSStringRelease(nm);
 }
 
-- (void) setValue: (const char*) value ToVariable: (const char*) name {
-       JSObjectRef object = JSContextGetGlobalObject(context);
+- (void) setString: (const char*) value toVariable: (const char*) name {
+       JSObjectRef obj = JSContextGetGlobalObject(context);
        JSStringRef nm = JSStringCreateWithUTF8CString(name);
-       JSValueRef var = JSValueMakeString(context, JSStringCreateWithUTF8CString(value));
-       JSObjectSetProperty(context, object, nm, var, kJSPropertyAttributeNone, NULL);
+       JSStringRef v = JSStringCreateWithUTF8CString(value);
+       JSValueRef val = JSValueMakeString(context, v);
+       JSObjectSetProperty(context, obj, nm, val, kJSPropertyAttributeNone, NULL);
+       JSStringRelease(nm);
+       JSStringRelease(v);
+}
+
+- (void) setInteger: (int) value toVariable: (const char*) name {
+       JSObjectRef obj = JSContextGetGlobalObject(context);
+       JSStringRef nm = JSStringCreateWithUTF8CString(name);
+       JSValueRef val = JSValueMakeNumber(context, value);
+       JSObjectSetProperty(context, obj, nm, val, kJSPropertyAttributeNone, NULL);
        JSStringRelease(nm);
 }
 
@@ -90,14 +100,24 @@ static JSValueRef jsOpenFile(JSContextRef ctx,
 
 - (void) loadScript {
        NSString* script;
-       NSString* path = [[NSString alloc] initWithFormat: @"%@/.charpalette.js", NSHomeDirectory()];
+       NSString* home = NSHomeDirectory();
+       NSString* path = [[NSString alloc] initWithFormat: @"%@/.charpalette.js", home];
        NSError* error;
        
        script = [NSString stringWithContentsOfFile: path encoding: NSUTF8StringEncoding error: &error];
        if (script) {
                [self evaluateScript: script];
                [self setFunctions];
-               [self setValue: [NSHomeDirectory() UTF8String] ToVariable: "HOME"];
+               [self setString: [home UTF8String] toVariable: "HOME"];
+               [self setInteger: NSAlphaShiftKeyMask toVariable: "NSAlphaShiftKeyMask"];
+               [self setInteger: NSShiftKeyMask toVariable: "NSShiftKeyMask"];
+               [self setInteger: NSControlKeyMask toVariable: "NSControlKeyMask"];
+               [self setInteger: NSAlternateKeyMask toVariable: "NSAlternateKeyMask"];
+               [self setInteger: NSCommandKeyMask toVariable: "NSCommandKeyMask"];
+               [self setInteger: NSNumericPadKeyMask toVariable: "NSNumericPadKeyMask"];
+               [self setInteger: NSHelpKeyMask toVariable: "NSHelpKeyMask"];
+               [self setInteger: NSFunctionKeyMask toVariable: "NSFunctionKeyMask"];
+               [self setInteger: NSDeviceIndependentModifierFlagsMask toVariable: "NSDeviceIndependentModifierFlagsMask"];
        } else {
                NSLog(@"the js file is not found");
        }
@@ -145,4 +165,25 @@ static JSValueRef jsOpenFile(JSContextRef ctx,
        [super release];
 }
 
+- (void) executeScriptAtIndex:(int)index withProperty:(NSString *)property AndModifier:(int)modifier {
+       JSObjectRef object = JSContextGetGlobalObject(context);
+       JSStringRef nm = JSStringCreateWithUTF8CString("script");
+       JSValueRef script = JSObjectGetProperty(context, object, nm, NULL);
+       JSStringRelease(nm);
+
+       JSValueRef val = NULL;
+       if (! JSValueIsUndefined(context, script)) {
+               JSObjectRef array = JSValueToObject(context, script, NULL);
+               val = JSObjectGetPropertyAtIndex(context, array, index, NULL);
+       }
+       if (val != NULL && ! JSValueIsUndefined(context, val)) {
+               JSObjectRef func = JSValueToObject(context, val,  NULL);
+
+               JSStringRef t = JSStringCreateWithCFString((CFStringRef) property);
+               JSValueRef args[] = {JSValueMakeString(context, t), JSValueMakeNumber(context, modifier)};
+               JSObjectCallAsFunction(context, func, object, 2, args, NULL);
+               JSStringRelease(t);
+       }
+}
+
 @end