TextInputControllerQt.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
  3. * Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/
  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. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  15. * its contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "config.h"
  30. #include "TextInputControllerQt.h"
  31. #include "DumpRenderTreeSupportQt.h"
  32. #include <QApplication>
  33. #include <QInputMethodEvent>
  34. #include <QKeyEvent>
  35. TextInputController::TextInputController(QWebPage* parent)
  36. : QObject(parent)
  37. {
  38. }
  39. void TextInputController::doCommand(const QString& command)
  40. {
  41. Qt::KeyboardModifiers modifiers = Qt::NoModifier;
  42. int keycode = 0;
  43. if (command == "moveBackwardAndModifySelection:") {
  44. modifiers |= Qt::ShiftModifier;
  45. keycode = Qt::Key_Left;
  46. } else if (command =="moveDown:") {
  47. keycode = Qt::Key_Down;
  48. } else if (command =="moveDownAndModifySelection:") {
  49. modifiers |= Qt::ShiftModifier;
  50. keycode = Qt::Key_Down;
  51. } else if (command =="moveForward:") {
  52. keycode = Qt::Key_Right;
  53. } else if (command =="moveForwardAndModifySelection:") {
  54. modifiers |= Qt::ShiftModifier;
  55. keycode = Qt::Key_Right;
  56. } else if (command =="moveLeft:") {
  57. keycode = Qt::Key_Left;
  58. } else if (command =="moveLeftAndModifySelection:") {
  59. modifiers |= Qt::ShiftModifier;
  60. keycode = Qt::Key_Left;
  61. } else if (command =="moveRight:") {
  62. keycode = Qt::Key_Right;
  63. } else if (command =="moveRightAndModifySelection:") {
  64. modifiers |= Qt::ShiftModifier;
  65. keycode = Qt::Key_Right;
  66. } else if (command =="moveToBeginningOfDocument:") {
  67. modifiers |= Qt::ControlModifier;
  68. keycode = Qt::Key_Home;
  69. } else if (command =="moveToBeginningOfLine:") {
  70. keycode = Qt::Key_Home;
  71. // } else if (command =="moveToBeginningOfParagraph:") {
  72. } else if (command =="moveToEndOfDocument:") {
  73. modifiers |= Qt::ControlModifier;
  74. keycode = Qt::Key_End;
  75. } else if (command =="moveToEndOfLine:") {
  76. keycode = Qt::Key_End;
  77. // } else if (command =="moveToEndOfParagraph:") {
  78. } else if (command =="moveUp:") {
  79. keycode = Qt::Key_Up;
  80. } else if (command =="moveUpAndModifySelection:") {
  81. modifiers |= Qt::ShiftModifier;
  82. keycode = Qt::Key_Up;
  83. } else if (command =="moveWordBackward:") {
  84. modifiers |= Qt::ControlModifier;
  85. keycode = Qt::Key_Up;
  86. } else if (command =="moveWordBackwardAndModifySelection:") {
  87. modifiers |= Qt::ShiftModifier;
  88. modifiers |= Qt::ControlModifier;
  89. keycode = Qt::Key_Left;
  90. } else if (command =="moveWordForward:") {
  91. modifiers |= Qt::ControlModifier;
  92. keycode = Qt::Key_Right;
  93. } else if (command =="moveWordForwardAndModifySelection:") {
  94. modifiers |= Qt::ControlModifier;
  95. modifiers |= Qt::ShiftModifier;
  96. keycode = Qt::Key_Right;
  97. } else if (command =="moveWordLeft:") {
  98. modifiers |= Qt::ControlModifier;
  99. keycode = Qt::Key_Left;
  100. } else if (command =="moveWordRight:") {
  101. modifiers |= Qt::ControlModifier;
  102. keycode = Qt::Key_Left;
  103. } else if (command =="moveWordRightAndModifySelection:") {
  104. modifiers |= Qt::ShiftModifier;
  105. modifiers |= Qt::ControlModifier;
  106. keycode = Qt::Key_Right;
  107. } else if (command =="moveWordLeftAndModifySelection:") {
  108. modifiers |= Qt::ShiftModifier;
  109. modifiers |= Qt::ControlModifier;
  110. keycode = Qt::Key_Left;
  111. } else if (command =="pageDown:") {
  112. keycode = Qt::Key_PageDown;
  113. } else if (command =="pageUp:") {
  114. keycode = Qt::Key_PageUp;
  115. } else if (command == "deleteWordBackward:") {
  116. modifiers |= Qt::ControlModifier;
  117. keycode = Qt::Key_Backspace;
  118. } else if (command == "deleteBackward:") {
  119. keycode = Qt::Key_Backspace;
  120. } else if (command == "deleteForward:") {
  121. keycode = Qt::Key_Delete;
  122. }
  123. QKeyEvent event(QEvent::KeyPress, keycode, modifiers);
  124. QApplication::sendEvent(parent(), &event);
  125. QKeyEvent event2(QEvent::KeyRelease, keycode, modifiers);
  126. QApplication::sendEvent(parent(), &event2);
  127. }
  128. void TextInputController::setMarkedText(const QString& string, int start, int end)
  129. {
  130. QList<QInputMethodEvent::Attribute> attributes;
  131. QInputMethodEvent::Attribute selection(QInputMethodEvent::Selection, start, end, QVariant());
  132. attributes << selection;
  133. QInputMethodEvent event(string, attributes);
  134. QApplication::sendEvent(parent(), &event);
  135. }
  136. void TextInputController::insertText(const QString& string)
  137. {
  138. QList<QInputMethodEvent::Attribute> attributes;
  139. QInputMethodEvent event(string, attributes);
  140. event.setCommitString(string);
  141. QApplication::sendEvent(parent(), &event);
  142. }
  143. QVariantList TextInputController::selectedRange()
  144. {
  145. return DumpRenderTreeSupportQt::selectedRange(qobject_cast<QWebPage*>(parent())->handle());
  146. }
  147. QVariantList TextInputController::firstRectForCharacterRange(int location, int length)
  148. {
  149. return DumpRenderTreeSupportQt::firstRectForCharacterRange(qobject_cast<QWebPage*>(parent())->handle(), location, length);
  150. }
  151. void TextInputController::unmarkText()
  152. {
  153. DumpRenderTreeSupportQt::confirmComposition(qobject_cast<QWebPage*>(parent())->handle(), 0);
  154. }