2 * Copyright (C) 2011 Igalia S.L.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "TextInputController.h"
32 #include "DumpRenderTree.h"
33 #include "WebCoreSupport/DumpRenderTreeSupportGtk.h"
34 #include <GOwnPtrGtk.h>
35 #include <JavaScriptCore/JSObjectRef.h>
36 #include <JavaScriptCore/JSRetainPtr.h>
37 #include <JavaScriptCore/JSStringRef.h>
39 #include <webkit/webkit.h>
41 static JSValueRef setMarkedTextCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
43 WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
45 if (argumentCount < 3)
46 return JSValueMakeUndefined(context);
48 JSStringRef string = JSValueToStringCopy(context, arguments[0], exception);
49 ASSERT(!exception || !*exception);
51 size_t bufferSize = JSStringGetMaximumUTF8CStringSize(string);
52 GOwnPtr<gchar> stringBuffer(static_cast<gchar*>(g_malloc(bufferSize)));
53 JSStringGetUTF8CString(string, stringBuffer.get(), bufferSize);
54 JSStringRelease(string);
56 int start = static_cast<int>(JSValueToNumber(context, arguments[1], exception));
57 ASSERT(!exception || !*exception);
59 int length = static_cast<int>(JSValueToNumber(context, arguments[2], exception));
60 ASSERT(!exception || !*exception);
62 DumpRenderTreeSupportGtk::setComposition(view, stringBuffer.get(), start, length);
63 return JSValueMakeUndefined(context);
66 static JSValueRef hasMarkedTextCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
68 WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
70 return JSValueMakeBoolean(context, DumpRenderTreeSupportGtk::hasComposition(view));
73 static JSValueRef markedRangeCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
75 WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
79 if (!DumpRenderTreeSupportGtk::compositionRange(view, &start, &length))
80 return JSValueMakeUndefined(context);
82 JSValueRef arrayValues[2];
83 arrayValues[0] = JSValueMakeNumber(context, start);
84 arrayValues[1] = JSValueMakeNumber(context, length);
85 JSObjectRef arrayObject = JSObjectMakeArray(context, 2, arrayValues, exception);
86 ASSERT(!exception || !*exception);
90 static JSValueRef insertTextCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
92 WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
95 if (argumentCount < 1)
96 return JSValueMakeUndefined(context);
98 JSStringRef string = JSValueToStringCopy(context, arguments[0], exception);
99 ASSERT(!exception || !*exception);
101 size_t bufferSize = JSStringGetMaximumUTF8CStringSize(string);
102 GOwnPtr<gchar> stringBuffer(static_cast<gchar*>(g_malloc(bufferSize)));
103 JSStringGetUTF8CString(string, stringBuffer.get(), bufferSize);
104 JSStringRelease(string);
106 DumpRenderTreeSupportGtk::confirmComposition(view, stringBuffer.get());
107 return JSValueMakeUndefined(context);
110 static JSValueRef unmarkTextCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
112 WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
115 DumpRenderTreeSupportGtk::confirmComposition(view, 0);
116 return JSValueMakeUndefined(context);
119 static JSValueRef firstRectForCharacterRangeCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
121 WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
123 if (argumentCount < 2)
124 return JSValueMakeUndefined(context);
126 int location = static_cast<int>(JSValueToNumber(context, arguments[0], exception));
127 ASSERT(!exception || !*exception);
129 int length = static_cast<int>(JSValueToNumber(context, arguments[1], exception));
130 ASSERT(!exception || !*exception);
132 cairo_rectangle_int_t rect;
133 if (!DumpRenderTreeSupportGtk::firstRectForCharacterRange(view, location, length, &rect))
134 return JSValueMakeUndefined(context);
136 JSValueRef arrayValues[4];
137 arrayValues[0] = JSValueMakeNumber(context, rect.x);
138 arrayValues[1] = JSValueMakeNumber(context, rect.y);
139 arrayValues[2] = JSValueMakeNumber(context, rect.width);
140 arrayValues[3] = JSValueMakeNumber(context, rect.height);
141 JSObjectRef arrayObject = JSObjectMakeArray(context, 4, arrayValues, exception);
142 ASSERT(!exception || !*exception);
147 static JSValueRef selectedRangeCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
149 WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
153 if (!DumpRenderTreeSupportGtk::selectedRange(view, &start, &length))
154 return JSValueMakeUndefined(context);
156 JSValueRef arrayValues[2];
157 arrayValues[0] = JSValueMakeNumber(context, start);
158 arrayValues[1] = JSValueMakeNumber(context, length);
159 JSObjectRef arrayObject = JSObjectMakeArray(context, 2, arrayValues, exception);
160 ASSERT(!exception || !*exception);
165 static JSValueRef doCommandCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
167 WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
169 if (argumentCount < 1)
170 return JSValueMakeUndefined(context);
172 JSStringRef string = JSValueToStringCopy(context, arguments[0], exception);
173 ASSERT(!exception || !*exception);
175 size_t bufferSize = JSStringGetMaximumUTF8CStringSize(string);
176 GOwnPtr<gchar> stringBuffer(static_cast<gchar*>(g_malloc(bufferSize)));
177 JSStringGetUTF8CString(string, stringBuffer.get(), bufferSize);
178 JSStringRelease(string);
180 DumpRenderTreeSupportGtk::doCommand(view, stringBuffer.get());
181 return JSValueMakeUndefined(context);
184 static JSStaticFunction staticFunctions[] = {
185 { "setMarkedText", setMarkedTextCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
186 { "hasMarkedText", hasMarkedTextCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
187 { "markedRange", markedRangeCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
188 { "insertText", insertTextCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
189 { "unmarkText", unmarkTextCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
190 { "firstRectForCharacterRange", firstRectForCharacterRangeCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
191 { "selectedRange", selectedRangeCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
192 { "doCommand", doCommandCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
196 static JSClassRef getClass(JSContextRef context)
198 static JSClassRef textInputControllerClass = 0;
200 if (!textInputControllerClass) {
201 JSClassDefinition classDefinition = {
203 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
204 classDefinition.staticFunctions = staticFunctions;
206 textInputControllerClass = JSClassCreate(&classDefinition);
209 return textInputControllerClass;
212 JSObjectRef makeTextInputController(JSContextRef context)
214 return JSObjectMake(context, getClass(context), 0);