EditorClientManx.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. * Copyright (C) 2008 Apple Computer, Inc. All rights reserved.
  3. * Copyright (C) 2012 Sony Computer Entertainment Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  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. *
  14. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  15. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  22. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "config.h"
  27. #include "EditorClientManx.h"
  28. #include "EditCommand.h"
  29. #include "Editor.h"
  30. #include "Frame.h"
  31. #include "KeyboardEvent.h"
  32. #include "NotImplemented.h"
  33. #include "PlatformKeyboardEvent.h"
  34. #include "Settings.h"
  35. #include "WebViewPrivate.h"
  36. #include <manx/KeyboardCodes.h>
  37. #include <webkit/WebView.h>
  38. // #define MYTRACE printf("+++ EditorClientManx::%s() +++ :L%d\n", __FUNCTION__, __LINE__);
  39. #define MYTRACE
  40. namespace WebCore {
  41. EditorClientManx::EditorClientManx(WebKit::WebViewPrivate* webView)
  42. : m_webView(webView)
  43. {
  44. }
  45. EditorClientManx::~EditorClientManx()
  46. {
  47. }
  48. void EditorClientManx::pageDestroyed()
  49. {
  50. MYTRACE;
  51. delete this;
  52. }
  53. void EditorClientManx::frameWillDetachPage(Frame*)
  54. {
  55. }
  56. bool EditorClientManx::shouldDeleteRange(Range*)
  57. {
  58. MYTRACE;
  59. notImplemented();
  60. return true;
  61. }
  62. bool EditorClientManx::shouldShowDeleteInterface(HTMLElement*)
  63. {
  64. MYTRACE;
  65. notImplemented();
  66. return false;
  67. }
  68. bool EditorClientManx::smartInsertDeleteEnabled()
  69. {
  70. MYTRACE;
  71. notImplemented();
  72. return true;
  73. }
  74. bool EditorClientManx::isSelectTrailingWhitespaceEnabled()
  75. {
  76. MYTRACE;
  77. notImplemented();
  78. return true;
  79. }
  80. bool EditorClientManx::isContinuousSpellCheckingEnabled()
  81. {
  82. MYTRACE;
  83. notImplemented();
  84. return false;
  85. }
  86. void EditorClientManx::toggleContinuousSpellChecking()
  87. {
  88. MYTRACE;
  89. notImplemented();
  90. }
  91. bool EditorClientManx::isGrammarCheckingEnabled()
  92. {
  93. MYTRACE;
  94. notImplemented();
  95. return false;
  96. }
  97. void EditorClientManx::toggleGrammarChecking()
  98. {
  99. MYTRACE;
  100. notImplemented();
  101. }
  102. int EditorClientManx::spellCheckerDocumentTag()
  103. {
  104. MYTRACE;
  105. notImplemented();
  106. return 0;
  107. }
  108. bool EditorClientManx::selectWordBeforeMenuEvent()
  109. {
  110. MYTRACE;
  111. notImplemented();
  112. return true;
  113. }
  114. bool EditorClientManx::isEditable()
  115. {
  116. MYTRACE;
  117. notImplemented();
  118. return false;
  119. }
  120. bool EditorClientManx::shouldBeginEditing(Range*)
  121. {
  122. MYTRACE;
  123. notImplemented();
  124. return true;
  125. }
  126. bool EditorClientManx::shouldEndEditing(Range*)
  127. {
  128. MYTRACE;
  129. notImplemented();
  130. return true;
  131. }
  132. bool EditorClientManx::shouldInsertNode(Node*, Range*,
  133. EditorInsertAction)
  134. {
  135. MYTRACE;
  136. notImplemented();
  137. return true;
  138. }
  139. bool EditorClientManx::shouldInsertText(const String& str, Range*,
  140. EditorInsertAction)
  141. {
  142. MYTRACE;
  143. // printf(" InsertText: {%s}\n", str.utf8().data());
  144. return true;
  145. }
  146. bool EditorClientManx::shouldApplyStyle(StylePropertySet*,
  147. Range*)
  148. {
  149. MYTRACE;
  150. notImplemented();
  151. return true;
  152. }
  153. bool EditorClientManx::shouldMoveRangeAfterDelete(Range*, Range*)
  154. {
  155. MYTRACE;
  156. notImplemented();
  157. return true;
  158. }
  159. bool EditorClientManx::shouldChangeSelectedRange(Range* fromRange, Range* toRange,
  160. EAffinity, bool stillSelecting)
  161. {
  162. MYTRACE;
  163. notImplemented();
  164. return true;
  165. }
  166. void EditorClientManx::didBeginEditing()
  167. {
  168. MYTRACE;
  169. notImplemented();
  170. }
  171. void EditorClientManx::didEndEditing()
  172. {
  173. MYTRACE;
  174. notImplemented();
  175. }
  176. void EditorClientManx::willWriteSelectionToPasteboard(WebCore::Range*)
  177. {
  178. }
  179. void EditorClientManx::getClientPasteboardDataForRange(WebCore::Range*, Vector<String>&, Vector<RefPtr<WebCore::SharedBuffer> >&)
  180. {
  181. }
  182. void EditorClientManx::didWriteSelectionToPasteboard()
  183. {
  184. MYTRACE;
  185. notImplemented();
  186. }
  187. void EditorClientManx::didSetSelectionTypesForPasteboard()
  188. {
  189. MYTRACE;
  190. notImplemented();
  191. }
  192. void EditorClientManx::respondToChangedContents()
  193. {
  194. MYTRACE;
  195. notImplemented();
  196. }
  197. void EditorClientManx::respondToChangedSelection(Frame*)
  198. {
  199. MYTRACE;
  200. notImplemented();
  201. }
  202. void EditorClientManx::registerUndoStep(PassRefPtr<UndoStep>)
  203. {
  204. MYTRACE;
  205. notImplemented();
  206. }
  207. void EditorClientManx::registerRedoStep(PassRefPtr<UndoStep>)
  208. {
  209. MYTRACE;
  210. notImplemented();
  211. }
  212. void EditorClientManx::clearUndoRedoOperations()
  213. {
  214. MYTRACE;
  215. notImplemented();
  216. }
  217. bool EditorClientManx::canCopyCut(Frame*, bool defaultValue) const
  218. {
  219. MYTRACE;
  220. notImplemented();
  221. return defaultValue;
  222. }
  223. bool EditorClientManx::canPaste(Frame*, bool defaultValue) const
  224. {
  225. MYTRACE;
  226. notImplemented();
  227. return defaultValue;
  228. }
  229. bool EditorClientManx::canUndo() const
  230. {
  231. MYTRACE;
  232. notImplemented();
  233. return false;
  234. }
  235. bool EditorClientManx::canRedo() const
  236. {
  237. MYTRACE;
  238. notImplemented();
  239. return false;
  240. }
  241. void EditorClientManx::undo()
  242. {
  243. MYTRACE;
  244. notImplemented();
  245. }
  246. void EditorClientManx::redo()
  247. {
  248. MYTRACE;
  249. notImplemented();
  250. }
  251. static const unsigned CtrlKey = 1 << 0;
  252. static const unsigned AltKey = 1 << 1;
  253. static const unsigned ShiftKey = 1 << 2;
  254. struct KeyDownEntry {
  255. unsigned virtualKey;
  256. unsigned modifiers;
  257. const char* name;
  258. };
  259. struct KeyPressEntry {
  260. unsigned charCode;
  261. unsigned modifiers;
  262. const char* name;
  263. };
  264. static const KeyDownEntry keyDownEntries[] = {
  265. { Manx::VK_LEFT, 0, "MoveLeft" },
  266. { Manx::VK_LEFT, ShiftKey, "MoveLeftAndModifySelection" },
  267. { Manx::VK_LEFT, CtrlKey, "MoveWordLeft" },
  268. { Manx::VK_LEFT, CtrlKey | ShiftKey, "MoveWordLeftAndModifySelection" },
  269. { Manx::VK_RIGHT, 0, "MoveRight" },
  270. { Manx::VK_RIGHT, ShiftKey, "MoveRightAndModifySelection" },
  271. { Manx::VK_RIGHT, CtrlKey, "MoveWordRight" },
  272. { Manx::VK_RIGHT, CtrlKey | ShiftKey, "MoveWordRightAndModifySelection" },
  273. { Manx::VK_UP, 0, "MoveUp" },
  274. { Manx::VK_UP, ShiftKey, "MoveUpAndModifySelection" },
  275. { Manx::VK_PRIOR, ShiftKey, "MovePageUpAndModifySelection" },
  276. { Manx::VK_DOWN, 0, "MoveDown" },
  277. { Manx::VK_DOWN, ShiftKey, "MoveDownAndModifySelection" },
  278. { Manx::VK_NEXT, ShiftKey, "MovePageDownAndModifySelection" },
  279. { Manx::VK_PRIOR, 0, "MovePageUp" },
  280. { Manx::VK_NEXT, 0, "MovePageDown" },
  281. { Manx::VK_HOME, 0, "MoveToBeginningOfLine" },
  282. { Manx::VK_HOME, ShiftKey, "MoveToBeginningOfLineAndModifySelection" },
  283. { Manx::VK_HOME, CtrlKey, "MoveToBeginningOfDocument" },
  284. { Manx::VK_HOME, CtrlKey | ShiftKey, "MoveToBeginningOfDocumentAndModifySelection" },
  285. { Manx::VK_END, 0, "MoveToEndOfLine" },
  286. { Manx::VK_END, ShiftKey, "MoveToEndOfLineAndModifySelection" },
  287. { Manx::VK_END, CtrlKey, "MoveToEndOfDocument" },
  288. { Manx::VK_END, CtrlKey | ShiftKey, "MoveToEndOfDocumentAndModifySelection" },
  289. { Manx::VK_BACK, 0, "DeleteBackward" },
  290. { Manx::VK_BACK, ShiftKey, "DeleteBackward" },
  291. { Manx::VK_DELETE, 0, "DeleteForward" },
  292. { Manx::VK_BACK, CtrlKey, "DeleteWordBackward" },
  293. { Manx::VK_DELETE, CtrlKey, "DeleteWordForward" },
  294. { 'B', CtrlKey, "ToggleBold" },
  295. { 'I', CtrlKey, "ToggleItalic" },
  296. { Manx::VK_ESCAPE, 0, "Cancel" },
  297. { Manx::VK_OEM_PERIOD, CtrlKey, "Cancel" },
  298. { Manx::VK_TAB, 0, "InsertTab" },
  299. { Manx::VK_TAB, ShiftKey, "InsertBacktab" },
  300. { Manx::VK_RETURN, 0, "InsertNewline" },
  301. { Manx::VK_RETURN, CtrlKey, "InsertNewline" },
  302. { Manx::VK_RETURN, AltKey, "InsertNewline" },
  303. { Manx::VK_RETURN, AltKey | ShiftKey, "InsertNewline" },
  304. { Manx::VKX_PS_CARET_LEFT, 0, "MoveLeft" },
  305. { Manx::VKX_PS_CARET_LEFT, ShiftKey, "MoveLeftAndModifySelection" },
  306. { Manx::VKX_PS_CARET_RIGHT, 0, "MoveRight" },
  307. { Manx::VKX_PS_CARET_RIGHT, ShiftKey, "MoveRightAndModifySelection" },
  308. { Manx::VKX_PS_CARET_UP, 0, "MoveUp" },
  309. { Manx::VKX_PS_CARET_UP, ShiftKey, "MoveUpAndModifySelection" },
  310. { Manx::VKX_PS_CARET_DOWN, 0, "MoveDown" },
  311. { Manx::VKX_PS_CARET_DOWN, ShiftKey, "MoveDownAndModifySelection" },
  312. };
  313. static const KeyPressEntry keyPressEntries[] = {
  314. { '\t', 0, "InsertTab" },
  315. { '\t', ShiftKey, "InsertBacktab" },
  316. { '\r', 0, "InsertNewline" },
  317. { '\r', CtrlKey, "InsertNewline" },
  318. { '\r', AltKey, "InsertNewline" },
  319. { '\r', AltKey | ShiftKey, "InsertNewline" },
  320. };
  321. #define NUM_ELEMENTS(array) (sizeof(array) / sizeof(array[0]))
  322. static const char* interpretEditorCommandKeyEvent(const KeyboardEvent* evt)
  323. {
  324. MYTRACE;
  325. ASSERT(evt->type() == eventNames().keydownEvent || evt->type() == eventNames().keypressEvent);
  326. static HashMap<int, const char*>* keyDownCommandsMap = 0;
  327. static HashMap<int, const char*>* keyPressCommandsMap = 0;
  328. if (!keyDownCommandsMap) {
  329. keyDownCommandsMap = new HashMap<int, const char*>;
  330. keyPressCommandsMap = new HashMap<int, const char*>;
  331. for (unsigned i = 0; i < NUM_ELEMENTS(keyDownEntries); i++)
  332. keyDownCommandsMap->set(keyDownEntries[i].modifiers << 16 | keyDownEntries[i].virtualKey, keyDownEntries[i].name);
  333. for (unsigned i = 0; i < NUM_ELEMENTS(keyPressEntries); i++)
  334. keyPressCommandsMap->set(keyPressEntries[i].modifiers << 16 | keyPressEntries[i].charCode, keyPressEntries[i].name);
  335. }
  336. unsigned modifiers = 0;
  337. if (evt->shiftKey())
  338. modifiers |= ShiftKey;
  339. if (evt->altKey())
  340. modifiers |= AltKey;
  341. if (evt->ctrlKey())
  342. modifiers |= CtrlKey;
  343. if (evt->type() == eventNames().keydownEvent) {
  344. int mapKey = modifiers << 16 | evt->keyCode();
  345. return mapKey ? keyDownCommandsMap->get(mapKey) : 0;
  346. }
  347. int mapKey = modifiers << 16 | evt->charCode();
  348. return mapKey ? keyPressCommandsMap->get(mapKey) : 0;
  349. }
  350. void EditorClientManx::handleKeyboardEvent(KeyboardEvent* event)
  351. {
  352. MYTRACE;
  353. Node* node = event->target()->toNode();
  354. ASSERT(node);
  355. Frame* frame = node->document()->frame();
  356. ASSERT(frame);
  357. const PlatformKeyboardEvent* platformEvent = event->keyEvent();
  358. if (!platformEvent)
  359. return;
  360. // Don't allow editor commands or text insertion for nodes that
  361. // cannot edit, unless we are in caret mode.
  362. if (!frame->editor().canEdit() && !(frame->settings() && frame->settings()->caretBrowsingEnabled()))
  363. return;
  364. const char* editorCommandString = interpretEditorCommandKeyEvent(event);
  365. if (editorCommandString) {
  366. Editor::Command command = frame->editor().command(editorCommandString);
  367. // On editor commands from key down events, we only want to let the event bubble up to
  368. // the DOM if it inserts text. If it doesn't insert text (e.g. Tab that changes focus)
  369. // we just want WebKit to handle it immediately without a DOM event.
  370. if (platformEvent->type() == PlatformKeyboardEvent::RawKeyDown) {
  371. if (!command.isTextInsertion() && command.execute(event))
  372. event->setDefaultHandled();
  373. return;
  374. }
  375. if (command.execute(event)) {
  376. event->setDefaultHandled();
  377. return;
  378. }
  379. }
  380. // This is just a normal text insertion, so wait to execute the insertion
  381. // until a keypress event happens. This will ensure that the insertion will not
  382. // be reflected in the contents of the field until the keyup DOM event.
  383. if (event->type() == eventNames().keypressEvent) {
  384. // Don't insert null or control characters as they can result in unexpected behaviour
  385. if (event->charCode() < ' ')
  386. return;
  387. // Don't insert anything if a modifier is pressed
  388. if (platformEvent->ctrlKey() || platformEvent->altKey())
  389. return;
  390. if (frame->editor().insertText(platformEvent->text(), event))
  391. event->setDefaultHandled();
  392. }
  393. }
  394. void EditorClientManx::handleInputMethodKeydown(KeyboardEvent* event)
  395. {
  396. MYTRACE;
  397. // NOTE: we don't currently need to handle this. When key events occur,
  398. // both this method and handleKeyboardEvent get a chance at handling them.
  399. // We might use this method later on for IME-specific handling.
  400. }
  401. void EditorClientManx::textFieldDidBeginEditing(Element*)
  402. {
  403. MYTRACE;
  404. notImplemented();
  405. }
  406. void EditorClientManx::textFieldDidEndEditing(Element*)
  407. {
  408. MYTRACE;
  409. notImplemented();
  410. }
  411. void EditorClientManx::textDidChangeInTextField(Element*)
  412. {
  413. MYTRACE;
  414. notImplemented();
  415. }
  416. bool EditorClientManx::doTextFieldCommandFromEvent(Element*, KeyboardEvent*)
  417. {
  418. MYTRACE;
  419. return false;
  420. }
  421. void EditorClientManx::textWillBeDeletedInTextField(Element*)
  422. {
  423. MYTRACE;
  424. notImplemented();
  425. }
  426. void EditorClientManx::textDidChangeInTextArea(Element*)
  427. {
  428. MYTRACE;
  429. notImplemented();
  430. }
  431. void EditorClientManx::ignoreWordInSpellDocument(const String&)
  432. {
  433. MYTRACE;
  434. notImplemented();
  435. }
  436. bool EditorClientManx::shouldEraseMarkersAfterChangeSelection(TextCheckingType) const
  437. {
  438. return true;
  439. }
  440. void EditorClientManx::learnWord(const String&)
  441. {
  442. MYTRACE;
  443. notImplemented();
  444. }
  445. void EditorClientManx::checkSpellingOfString(const UChar*, int length, int* misspellingLocation, int* misspellingLength)
  446. {
  447. MYTRACE;
  448. notImplemented();
  449. }
  450. void EditorClientManx::checkGrammarOfString(const UChar*, int length, Vector<GrammarDetail>&, int* badGrammarLocation, int* badGrammarLength)
  451. {
  452. MYTRACE;
  453. notImplemented();
  454. }
  455. void EditorClientManx::updateSpellingUIWithGrammarString(const String&, const GrammarDetail& detail)
  456. {
  457. MYTRACE;
  458. notImplemented();
  459. }
  460. void EditorClientManx::updateSpellingUIWithMisspelledWord(const String&)
  461. {
  462. MYTRACE;
  463. notImplemented();
  464. }
  465. void EditorClientManx::showSpellingUI(bool show)
  466. {
  467. MYTRACE;
  468. notImplemented();
  469. }
  470. bool EditorClientManx::spellingUIIsShowing()
  471. {
  472. MYTRACE;
  473. notImplemented();
  474. return false;
  475. }
  476. void EditorClientManx::getGuessesForWord(const String&, const String&, Vector<String>&)
  477. {
  478. MYTRACE;
  479. notImplemented();
  480. }
  481. String EditorClientManx::getAutoCorrectSuggestionForMisspelledWord(const WTF::String&)
  482. {
  483. MYTRACE;
  484. notImplemented();
  485. return String();
  486. }
  487. void EditorClientManx::willSetInputMethodState()
  488. {
  489. MYTRACE;
  490. m_webView->confirmComposition();
  491. }
  492. void EditorClientManx::setInputMethodState(bool enabled)
  493. {
  494. m_webView->client().setInputMethodState(enabled);
  495. }
  496. void EditorClientManx::requestCheckingOfString(PassRefPtr<TextCheckingRequest>)
  497. {
  498. }
  499. }