|
1 /* |
|
2 * Copyright (C) 2010 Google Inc. All rights reserved. |
|
3 * |
|
4 * Redistribution and use in source and binary forms, with or without |
|
5 * modification, are permitted provided that the following conditions are |
|
6 * met: |
|
7 * |
|
8 * * Redistributions of source code must retain the above copyright |
|
9 * notice, this list of conditions and the following disclaimer. |
|
10 * * Redistributions in binary form must reproduce the above |
|
11 * copyright notice, this list of conditions and the following disclaimer |
|
12 * in the documentation and/or other materials provided with the |
|
13 * distribution. |
|
14 * * Neither the name of Google Inc. nor the names of its |
|
15 * contributors may be used to endorse or promote products derived from |
|
16 * this software without specific prior written permission. |
|
17 * |
|
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 */ |
|
30 |
|
31 #include "config.h" |
|
32 #include "TextInputController.h" |
|
33 |
|
34 #include "TestShell.h" |
|
35 #include "public/WebFrame.h" |
|
36 #include "public/WebRange.h" |
|
37 #include "public/WebString.h" |
|
38 #include "public/WebView.h" |
|
39 #include <wtf/StringExtras.h> |
|
40 #include <string> |
|
41 |
|
42 using namespace WebKit; |
|
43 using namespace std; |
|
44 |
|
45 TestShell* TextInputController::testShell = 0; |
|
46 |
|
47 TextInputController::TextInputController(TestShell* shell) |
|
48 { |
|
49 // Set static testShell variable. Be careful not to assign testShell to new |
|
50 // windows which are temporary. |
|
51 if (!testShell) |
|
52 testShell = shell; |
|
53 |
|
54 bindMethod("insertText", &TextInputController::insertText); |
|
55 bindMethod("doCommand", &TextInputController::doCommand); |
|
56 bindMethod("setMarkedText", &TextInputController::setMarkedText); |
|
57 bindMethod("unmarkText", &TextInputController::unmarkText); |
|
58 bindMethod("hasMarkedText", &TextInputController::hasMarkedText); |
|
59 bindMethod("conversationIdentifier", &TextInputController::conversationIdentifier); |
|
60 bindMethod("substringFromRange", &TextInputController::substringFromRange); |
|
61 bindMethod("attributedSubstringFromRange", &TextInputController::attributedSubstringFromRange); |
|
62 bindMethod("markedRange", &TextInputController::markedRange); |
|
63 bindMethod("selectedRange", &TextInputController::selectedRange); |
|
64 bindMethod("firstRectForCharacterRange", &TextInputController::firstRectForCharacterRange); |
|
65 bindMethod("characterIndexForPoint", &TextInputController::characterIndexForPoint); |
|
66 bindMethod("validAttributesForMarkedText", &TextInputController::validAttributesForMarkedText); |
|
67 bindMethod("makeAttributedString", &TextInputController::makeAttributedString); |
|
68 } |
|
69 |
|
70 WebFrame* TextInputController::getMainFrame() |
|
71 { |
|
72 return testShell->webView()->mainFrame(); |
|
73 } |
|
74 |
|
75 void TextInputController::insertText(const CppArgumentList& arguments, CppVariant* result) |
|
76 { |
|
77 result->setNull(); |
|
78 |
|
79 WebFrame* mainFrame = getMainFrame(); |
|
80 if (!mainFrame) |
|
81 return; |
|
82 if (arguments.size() < 1 || !arguments[0].isString()) |
|
83 return; |
|
84 |
|
85 if (mainFrame->hasMarkedText()) { |
|
86 mainFrame->unmarkText(); |
|
87 mainFrame->replaceSelection(WebString()); |
|
88 } |
|
89 mainFrame->insertText(WebString::fromUTF8(arguments[0].toString())); |
|
90 } |
|
91 |
|
92 void TextInputController::doCommand(const CppArgumentList& arguments, CppVariant* result) |
|
93 { |
|
94 result->setNull(); |
|
95 |
|
96 WebFrame* mainFrame = getMainFrame(); |
|
97 if (!mainFrame) |
|
98 return; |
|
99 |
|
100 if (arguments.size() >= 1 && arguments[0].isString()) |
|
101 mainFrame->executeCommand(WebString::fromUTF8(arguments[0].toString())); |
|
102 } |
|
103 |
|
104 void TextInputController::setMarkedText(const CppArgumentList& arguments, CppVariant* result) |
|
105 { |
|
106 result->setNull(); |
|
107 |
|
108 WebFrame* mainFrame = getMainFrame(); |
|
109 if (!mainFrame) |
|
110 return; |
|
111 |
|
112 if (arguments.size() >= 3 && arguments[0].isString() |
|
113 && arguments[1].isNumber() && arguments[2].isNumber()) { |
|
114 mainFrame->setMarkedText(WebString::fromUTF8(arguments[0].toString()), |
|
115 arguments[1].toInt32(), |
|
116 arguments[2].toInt32()); |
|
117 } |
|
118 } |
|
119 |
|
120 void TextInputController::unmarkText(const CppArgumentList&, CppVariant* result) |
|
121 { |
|
122 result->setNull(); |
|
123 |
|
124 WebFrame* mainFrame = getMainFrame(); |
|
125 if (!mainFrame) |
|
126 return; |
|
127 |
|
128 mainFrame->unmarkText(); |
|
129 } |
|
130 |
|
131 void TextInputController::hasMarkedText(const CppArgumentList&, CppVariant* result) |
|
132 { |
|
133 result->setNull(); |
|
134 |
|
135 WebFrame* mainFrame = getMainFrame(); |
|
136 if (!mainFrame) |
|
137 return; |
|
138 |
|
139 result->set(mainFrame->hasMarkedText()); |
|
140 } |
|
141 |
|
142 void TextInputController::conversationIdentifier(const CppArgumentList&, CppVariant* result) |
|
143 { |
|
144 // FIXME: Implement this. |
|
145 result->setNull(); |
|
146 } |
|
147 |
|
148 void TextInputController::substringFromRange(const CppArgumentList&, CppVariant* result) |
|
149 { |
|
150 // FIXME: Implement this. |
|
151 result->setNull(); |
|
152 } |
|
153 |
|
154 void TextInputController::attributedSubstringFromRange(const CppArgumentList&, CppVariant* result) |
|
155 { |
|
156 // FIXME: Implement this. |
|
157 result->setNull(); |
|
158 } |
|
159 |
|
160 void TextInputController::markedRange(const CppArgumentList&, CppVariant* result) |
|
161 { |
|
162 result->setNull(); |
|
163 |
|
164 WebFrame* mainFrame = getMainFrame(); |
|
165 if (!mainFrame) |
|
166 return; |
|
167 |
|
168 WebRange range = mainFrame->markedRange(); |
|
169 char buffer[30]; |
|
170 snprintf(buffer, 30, "%d,%d", range.startOffset(), range.endOffset()); |
|
171 result->set(string(buffer)); |
|
172 } |
|
173 |
|
174 void TextInputController::selectedRange(const CppArgumentList&, CppVariant* result) |
|
175 { |
|
176 result->setNull(); |
|
177 |
|
178 WebFrame* mainFrame = getMainFrame(); |
|
179 if (!mainFrame) |
|
180 return; |
|
181 |
|
182 WebRange range = mainFrame->selectionRange(); |
|
183 char buffer[30]; |
|
184 snprintf(buffer, 30, "%d,%d", range.startOffset(), range.endOffset()); |
|
185 result->set(string(buffer)); |
|
186 } |
|
187 |
|
188 void TextInputController::firstRectForCharacterRange(const CppArgumentList&, CppVariant* result) |
|
189 { |
|
190 // FIXME: Implement this. |
|
191 result->setNull(); |
|
192 } |
|
193 |
|
194 void TextInputController::characterIndexForPoint(const CppArgumentList&, CppVariant* result) |
|
195 { |
|
196 // FIXME: Implement this. |
|
197 result->setNull(); |
|
198 } |
|
199 |
|
200 void TextInputController::validAttributesForMarkedText(const CppArgumentList&, CppVariant* result) |
|
201 { |
|
202 result->setNull(); |
|
203 |
|
204 WebFrame* mainFrame = getMainFrame(); |
|
205 if (!mainFrame) |
|
206 return; |
|
207 |
|
208 result->set("NSUnderline,NSUnderlineColor,NSMarkedClauseSegment," |
|
209 "NSTextInputReplacementRangeAttributeName"); |
|
210 } |
|
211 |
|
212 void TextInputController::makeAttributedString(const CppArgumentList&, CppVariant* result) |
|
213 { |
|
214 // FIXME: Implement this. |
|
215 result->setNull(); |
|
216 } |