| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "webkit/tools/test_shell/text_input_controller.h" | 5 #include "webkit/tools/test_shell/text_input_controller.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "webkit/api/public/WebFrame.h" |
| 9 #include "webkit/api/public/WebRange.h" |
| 10 #include "webkit/api/public/WebString.h" |
| 8 #include "webkit/glue/webview.h" | 11 #include "webkit/glue/webview.h" |
| 9 #include "webkit/glue/webframe.h" | |
| 10 #include "webkit/glue/webtextinput.h" | |
| 11 #include "webkit/tools/test_shell/test_shell.h" | 12 #include "webkit/tools/test_shell/test_shell.h" |
| 12 | 13 |
| 14 using WebKit::WebFrame; |
| 15 using WebKit::WebRange; |
| 16 using WebKit::WebString; |
| 17 |
| 13 TestShell* TextInputController::shell_ = NULL; | 18 TestShell* TextInputController::shell_ = NULL; |
| 14 | 19 |
| 15 TextInputController::TextInputController(TestShell* shell) { | 20 TextInputController::TextInputController(TestShell* shell) { |
| 16 // Set static shell_ variable. Be careful not to assign shell_ to new | 21 // Set static shell_ variable. Be careful not to assign shell_ to new |
| 17 // windows which are temporary. | 22 // windows which are temporary. |
| 18 if (NULL == shell_) | 23 if (NULL == shell_) |
| 19 shell_ = shell; | 24 shell_ = shell; |
| 20 | 25 |
| 21 BindMethod("insertText", &TextInputController::insertText); | 26 BindMethod("insertText", &TextInputController::insertText); |
| 22 BindMethod("doCommand", &TextInputController::doCommand); | 27 BindMethod("doCommand", &TextInputController::doCommand); |
| 23 BindMethod("setMarkedText", &TextInputController::setMarkedText); | 28 BindMethod("setMarkedText", &TextInputController::setMarkedText); |
| 24 BindMethod("unmarkText", &TextInputController::unmarkText); | 29 BindMethod("unmarkText", &TextInputController::unmarkText); |
| 25 BindMethod("hasMarkedText", &TextInputController::hasMarkedText); | 30 BindMethod("hasMarkedText", &TextInputController::hasMarkedText); |
| 26 BindMethod("conversationIdentifier", &TextInputController::conversationIdentif
ier); | 31 BindMethod("conversationIdentifier", &TextInputController::conversationIdentif
ier); |
| 27 BindMethod("substringFromRange", &TextInputController::substringFromRange); | 32 BindMethod("substringFromRange", &TextInputController::substringFromRange); |
| 28 BindMethod("attributedSubstringFromRange", &TextInputController::attributedSub
stringFromRange); | 33 BindMethod("attributedSubstringFromRange", &TextInputController::attributedSub
stringFromRange); |
| 29 BindMethod("markedRange", &TextInputController::markedRange); | 34 BindMethod("markedRange", &TextInputController::markedRange); |
| 30 BindMethod("selectedRange", &TextInputController::selectedRange); | 35 BindMethod("selectedRange", &TextInputController::selectedRange); |
| 31 BindMethod("firstRectForCharacterRange", &TextInputController::firstRectForCha
racterRange); | 36 BindMethod("firstRectForCharacterRange", &TextInputController::firstRectForCha
racterRange); |
| 32 BindMethod("characterIndexForPoint", &TextInputController::characterIndexForPo
int); | 37 BindMethod("characterIndexForPoint", &TextInputController::characterIndexForPo
int); |
| 33 BindMethod("validAttributesForMarkedText", &TextInputController::validAttribut
esForMarkedText); | 38 BindMethod("validAttributesForMarkedText", &TextInputController::validAttribut
esForMarkedText); |
| 34 BindMethod("makeAttributedString", &TextInputController::makeAttributedString)
; | 39 BindMethod("makeAttributedString", &TextInputController::makeAttributedString)
; |
| 35 } | 40 } |
| 36 | 41 |
| 37 /* static */ WebView* TextInputController::webview() { | 42 // static |
| 38 return shell_->webView(); | 43 WebFrame* TextInputController::GetMainFrame() { |
| 39 } | 44 return shell_->webView()->GetMainFrame(); |
| 40 | |
| 41 /* static */ WebTextInput* TextInputController::GetTextInput() { | |
| 42 return webview()->GetMainFrame()->GetTextInput(); | |
| 43 } | 45 } |
| 44 | 46 |
| 45 void TextInputController::insertText( | 47 void TextInputController::insertText( |
| 46 const CppArgumentList& args, CppVariant* result) { | 48 const CppArgumentList& args, CppVariant* result) { |
| 47 result->SetNull(); | 49 result->SetNull(); |
| 48 | 50 |
| 49 WebTextInput* text_input = GetTextInput(); | 51 WebFrame* main_frame = GetMainFrame(); |
| 50 if (!text_input) | 52 if (!main_frame) |
| 51 return; | 53 return; |
| 52 | 54 |
| 53 if (args.size() >= 1 && args[0].isString()) { | 55 if (args.size() >= 1 && args[0].isString()) |
| 54 text_input->InsertText(UTF8ToUTF16(args[0].ToString())); | 56 main_frame->insertText(WebString::fromUTF8(args[0].ToString())); |
| 55 } | |
| 56 } | 57 } |
| 57 | 58 |
| 58 void TextInputController::doCommand( | 59 void TextInputController::doCommand( |
| 59 const CppArgumentList& args, CppVariant* result) { | 60 const CppArgumentList& args, CppVariant* result) { |
| 60 result->SetNull(); | 61 result->SetNull(); |
| 61 | 62 |
| 62 WebTextInput* text_input = GetTextInput(); | 63 WebFrame* main_frame = GetMainFrame(); |
| 63 if (!text_input) | 64 if (!main_frame) |
| 64 return; | 65 return; |
| 65 | 66 |
| 66 if (args.size() >= 1 && args[0].isString()) { | 67 if (args.size() >= 1 && args[0].isString()) |
| 67 text_input->DoCommand(UTF8ToUTF16(args[0].ToString())); | 68 main_frame->executeCommand(WebString::fromUTF8(args[0].ToString())); |
| 68 } | |
| 69 } | 69 } |
| 70 | 70 |
| 71 void TextInputController::setMarkedText( | 71 void TextInputController::setMarkedText( |
| 72 const CppArgumentList& args, CppVariant* result) { | 72 const CppArgumentList& args, CppVariant* result) { |
| 73 result->SetNull(); | 73 result->SetNull(); |
| 74 | 74 |
| 75 WebTextInput* text_input = GetTextInput(); | 75 WebFrame* main_frame = GetMainFrame(); |
| 76 if (!text_input) | 76 if (!main_frame) |
| 77 return; | 77 return; |
| 78 | 78 |
| 79 if (args.size() >= 3 && args[0].isString() | 79 if (args.size() >= 3 && args[0].isString() |
| 80 && args[1].isNumber() && args[2].isNumber()) { | 80 && args[1].isNumber() && args[2].isNumber()) { |
| 81 text_input->SetMarkedText(UTF8ToUTF16(args[0].ToString()), | 81 main_frame->setMarkedText(WebString::fromUTF8(args[0].ToString()), |
| 82 args[1].ToInt32(), | 82 args[1].ToInt32(), |
| 83 args[2].ToInt32()); | 83 args[2].ToInt32()); |
| 84 } | 84 } |
| 85 } | 85 } |
| 86 | 86 |
| 87 void TextInputController::unmarkText( | 87 void TextInputController::unmarkText( |
| 88 const CppArgumentList& args, CppVariant* result) { | 88 const CppArgumentList& args, CppVariant* result) { |
| 89 result->SetNull(); | 89 result->SetNull(); |
| 90 | 90 |
| 91 WebTextInput* text_input = GetTextInput(); | 91 WebFrame* main_frame = GetMainFrame(); |
| 92 if (!text_input) | 92 if (!main_frame) |
| 93 return; | 93 return; |
| 94 | 94 |
| 95 text_input->UnMarkText(); | 95 main_frame->unmarkText(); |
| 96 } | 96 } |
| 97 | 97 |
| 98 void TextInputController::hasMarkedText( | 98 void TextInputController::hasMarkedText( |
| 99 const CppArgumentList& args, CppVariant* result) { | 99 const CppArgumentList& args, CppVariant* result) { |
| 100 result->SetNull(); | 100 result->SetNull(); |
| 101 | 101 |
| 102 WebTextInput* text_input = GetTextInput(); | 102 WebFrame* main_frame = GetMainFrame(); |
| 103 if (!text_input) | 103 if (!main_frame) |
| 104 return; | 104 return; |
| 105 | 105 |
| 106 result->Set(text_input->HasMarkedText()); | 106 result->Set(main_frame->hasMarkedText()); |
| 107 } | 107 } |
| 108 | 108 |
| 109 void TextInputController::conversationIdentifier( | 109 void TextInputController::conversationIdentifier( |
| 110 const CppArgumentList& args, CppVariant* result) { | 110 const CppArgumentList& args, CppVariant* result) { |
| 111 NOTIMPLEMENTED(); |
| 111 result->SetNull(); | 112 result->SetNull(); |
| 112 | |
| 113 WebTextInput* text_input = GetTextInput(); | |
| 114 if (!text_input) | |
| 115 return; | |
| 116 | |
| 117 text_input->ConversationIdentifier(); | |
| 118 } | 113 } |
| 119 | 114 |
| 120 void TextInputController::substringFromRange( | 115 void TextInputController::substringFromRange( |
| 121 const CppArgumentList& args, CppVariant* result) { | 116 const CppArgumentList& args, CppVariant* result) { |
| 117 NOTIMPLEMENTED(); |
| 122 result->SetNull(); | 118 result->SetNull(); |
| 123 | |
| 124 WebTextInput* text_input = GetTextInput(); | |
| 125 if (!text_input) | |
| 126 return; | |
| 127 | |
| 128 if (args.size() >= 2 && args[0].isNumber() && args[1].isNumber()) { | |
| 129 text_input->SubstringFromRange(args[0].ToInt32(), args[1].ToInt32()); | |
| 130 } | |
| 131 } | 119 } |
| 132 | 120 |
| 133 void TextInputController::attributedSubstringFromRange( | 121 void TextInputController::attributedSubstringFromRange( |
| 134 const CppArgumentList& args, CppVariant* result) { | 122 const CppArgumentList& args, CppVariant* result) { |
| 123 NOTIMPLEMENTED(); |
| 135 result->SetNull(); | 124 result->SetNull(); |
| 136 | |
| 137 WebTextInput* text_input = GetTextInput(); | |
| 138 if (!text_input) | |
| 139 return; | |
| 140 | |
| 141 if (args.size() >= 2 && args[0].isNumber() && args[1].isNumber()) { | |
| 142 text_input->AttributedSubstringFromRange(args[0].ToInt32(), | |
| 143 args[1].ToInt32()); | |
| 144 } | |
| 145 } | 125 } |
| 146 | 126 |
| 147 void TextInputController::markedRange( | 127 void TextInputController::markedRange( |
| 148 const CppArgumentList& args, CppVariant* result) { | 128 const CppArgumentList& args, CppVariant* result) { |
| 149 result->SetNull(); | 129 result->SetNull(); |
| 150 | 130 |
| 151 WebTextInput* text_input = GetTextInput(); | 131 WebFrame* main_frame = GetMainFrame(); |
| 152 if (!text_input) | 132 if (!main_frame) |
| 153 return; | 133 return; |
| 154 | 134 |
| 135 WebRange range = main_frame->markedRange(); |
| 136 |
| 155 std::string range_str; | 137 std::string range_str; |
| 156 text_input->MarkedRange(&range_str); | 138 SStringPrintf(&range_str, "%d,%d", range.startOffset(), range.endOffset()); |
| 157 result->Set(range_str); | 139 result->Set(range_str); |
| 158 } | 140 } |
| 159 | 141 |
| 160 void TextInputController::selectedRange( | 142 void TextInputController::selectedRange( |
| 161 const CppArgumentList& args, CppVariant* result) { | 143 const CppArgumentList& args, CppVariant* result) { |
| 162 result->SetNull(); | 144 result->SetNull(); |
| 163 | 145 |
| 164 WebTextInput* text_input = GetTextInput(); | 146 WebFrame* main_frame = GetMainFrame(); |
| 165 if (!text_input) | 147 if (!main_frame) |
| 166 return; | 148 return; |
| 167 | 149 |
| 150 WebRange range = main_frame->selectionRange(); |
| 151 |
| 168 std::string range_str; | 152 std::string range_str; |
| 169 text_input->SelectedRange(&range_str); | 153 SStringPrintf(&range_str, "%d,%d", range.startOffset(), range.endOffset()); |
| 170 result->Set(range_str); | 154 result->Set(range_str); |
| 171 } | 155 } |
| 172 | 156 |
| 173 void TextInputController::firstRectForCharacterRange( | 157 void TextInputController::firstRectForCharacterRange( |
| 174 const CppArgumentList& args, CppVariant* result) { | 158 const CppArgumentList& args, CppVariant* result) { |
| 159 NOTIMPLEMENTED(); |
| 175 result->SetNull(); | 160 result->SetNull(); |
| 176 | |
| 177 WebTextInput* text_input = GetTextInput(); | |
| 178 if (!text_input) | |
| 179 return; | |
| 180 | |
| 181 if (args.size() >= 2 && args[0].isNumber() && args[1].isNumber()) { | |
| 182 text_input->FirstRectForCharacterRange(args[0].ToInt32(), | |
| 183 args[1].ToInt32()); | |
| 184 } | |
| 185 } | 161 } |
| 186 | 162 |
| 187 void TextInputController::characterIndexForPoint( | 163 void TextInputController::characterIndexForPoint( |
| 188 const CppArgumentList& args, CppVariant* result) { | 164 const CppArgumentList& args, CppVariant* result) { |
| 165 NOTIMPLEMENTED(); |
| 189 result->SetNull(); | 166 result->SetNull(); |
| 190 | |
| 191 WebTextInput* text_input = GetTextInput(); | |
| 192 if (!text_input) | |
| 193 return; | |
| 194 | |
| 195 if (args.size() >= 2 && args[0].isDouble() && args[1].isDouble()) { | |
| 196 text_input->CharacterIndexForPoint(args[0].ToDouble(), | |
| 197 args[1].ToDouble()); | |
| 198 } | |
| 199 } | 167 } |
| 200 | 168 |
| 201 void TextInputController::validAttributesForMarkedText( | 169 void TextInputController::validAttributesForMarkedText( |
| 202 const CppArgumentList& args, CppVariant* result) { | 170 const CppArgumentList& args, CppVariant* result) { |
| 203 result->SetNull(); | 171 result->SetNull(); |
| 204 | 172 |
| 205 WebTextInput* text_input = GetTextInput(); | 173 WebFrame* main_frame = GetMainFrame(); |
| 206 if (!text_input) | 174 if (!main_frame) |
| 207 return; | 175 return; |
| 208 | 176 |
| 209 std::string attributes_str; | 177 result->Set("NSUnderline,NSUnderlineColor,NSMarkedClauseSegment," |
| 210 text_input->ValidAttributesForMarkedText(&attributes_str); | 178 "NSTextInputReplacementRangeAttributeName"); |
| 211 result->Set(attributes_str); | |
| 212 } | 179 } |
| 213 | 180 |
| 214 void TextInputController::makeAttributedString( | 181 void TextInputController::makeAttributedString( |
| 215 const CppArgumentList& args, CppVariant* result) { | 182 const CppArgumentList& args, CppVariant* result) { |
| 183 NOTIMPLEMENTED(); |
| 216 result->SetNull(); | 184 result->SetNull(); |
| 217 | |
| 218 WebTextInput* text_input = GetTextInput(); | |
| 219 if (!text_input) | |
| 220 return; | |
| 221 | |
| 222 if (args.size() >= 1 && args[0].isString()) { | |
| 223 text_input->MakeAttributedString(args[0].ToString()); | |
| 224 } | |
| 225 } | 185 } |
| OLD | NEW |