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 33#include <QApplication> 34#include <QInputMethodEvent> 35#include <QKeyEvent> 36 37TextInputController::TextInputController(QWebPage* parent) 38 : QObject(parent) 39{ 40} 41 42void TextInputController::doCommand(const QString& command) 43{ 44 Qt::KeyboardModifiers modifiers = Qt::NoModifier; 45 int keycode = 0; 46 if (command == "moveBackwardAndModifySelection:") { 47 modifiers |= Qt::ShiftModifier; 48 keycode = Qt::Key_Left; 49 } else if (command =="moveDown:") { 50 keycode = Qt::Key_Down; 51 } else if (command =="moveDownAndModifySelection:") { 52 modifiers |= Qt::ShiftModifier; 53 keycode = Qt::Key_Down; 54 } else if (command =="moveForward:") { 55 keycode = Qt::Key_Right; 56 } else if (command =="moveForwardAndModifySelection:") { 57 modifiers |= Qt::ShiftModifier; 58 keycode = Qt::Key_Right; 59 } else if (command =="moveLeft:") { 60 keycode = Qt::Key_Left; 61 } else if (command =="moveLeftAndModifySelection:") { 62 modifiers |= Qt::ShiftModifier; 63 keycode = Qt::Key_Left; 64 } else if (command =="moveRight:") { 65 keycode = Qt::Key_Right; 66 } else if (command =="moveRightAndModifySelection:") { 67 modifiers |= Qt::ShiftModifier; 68 keycode = Qt::Key_Right; 69 } else if (command =="moveToBeginningOfDocument:") { 70 modifiers |= Qt::ControlModifier; 71 keycode = Qt::Key_Home; 72 } else if (command =="moveToBeginningOfLine:") { 73 keycode = Qt::Key_Home; 74// } else if (command =="moveToBeginningOfParagraph:") { 75 } else if (command =="moveToEndOfDocument:") { 76 modifiers |= Qt::ControlModifier; 77 keycode = Qt::Key_End; 78 } else if (command =="moveToEndOfLine:") { 79 keycode = Qt::Key_End; 80// } else if (command =="moveToEndOfParagraph:") { 81 } else if (command =="moveUp:") { 82 keycode = Qt::Key_Up; 83 } else if (command =="moveUpAndModifySelection:") { 84 modifiers |= Qt::ShiftModifier; 85 keycode = Qt::Key_Up; 86 } else if (command =="moveWordBackward:") { 87 modifiers |= Qt::ControlModifier; 88 keycode = Qt::Key_Up; 89 } else if (command =="moveWordBackwardAndModifySelection:") { 90 modifiers |= Qt::ShiftModifier; 91 modifiers |= Qt::ControlModifier; 92 keycode = Qt::Key_Left; 93 } else if (command =="moveWordForward:") { 94 modifiers |= Qt::ControlModifier; 95 keycode = Qt::Key_Right; 96 } else if (command =="moveWordForwardAndModifySelection:") { 97 modifiers |= Qt::ControlModifier; 98 modifiers |= Qt::ShiftModifier; 99 keycode = Qt::Key_Right; 100 } else if (command =="moveWordLeft:") { 101 modifiers |= Qt::ControlModifier; 102 keycode = Qt::Key_Left; 103 } else if (command =="moveWordRight:") { 104 modifiers |= Qt::ControlModifier; 105 keycode = Qt::Key_Left; 106 } else if (command =="moveWordRightAndModifySelection:") { 107 modifiers |= Qt::ShiftModifier; 108 modifiers |= Qt::ControlModifier; 109 keycode = Qt::Key_Right; 110 } else if (command =="moveWordLeftAndModifySelection:") { 111 modifiers |= Qt::ShiftModifier; 112 modifiers |= Qt::ControlModifier; 113 keycode = Qt::Key_Left; 114 } else if (command =="pageDown:") { 115 keycode = Qt::Key_PageDown; 116 } else if (command =="pageUp:") { 117 keycode = Qt::Key_PageUp; 118 } else if (command == "deleteWordBackward:") { 119 modifiers |= Qt::ControlModifier; 120 keycode = Qt::Key_Backspace; 121 } else if (command == "deleteBackward:") { 122 keycode = Qt::Key_Backspace; 123 } else if (command == "deleteForward:") { 124 keycode = Qt::Key_Delete; 125 } 126 127 QKeyEvent event(QEvent::KeyPress, keycode, modifiers); 128 QApplication::sendEvent(parent(), &event); 129 QKeyEvent event2(QEvent::KeyRelease, keycode, modifiers); 130 QApplication::sendEvent(parent(), &event2); 131} 132 133void TextInputController::setMarkedText(const QString& string, int start, int end) 134{ 135 QList<QInputMethodEvent::Attribute> attributes; 136 QInputMethodEvent::Attribute selection(QInputMethodEvent::Selection, start, end, QVariant()); 137 attributes << selection; 138 QInputMethodEvent event(string, attributes); 139 QApplication::sendEvent(parent(), &event); 140} 141 142void TextInputController::insertText(const QString& string) 143{ 144 QList<QInputMethodEvent::Attribute> attributes; 145 QInputMethodEvent event(string, attributes); 146 event.setCommitString(string); 147 QApplication::sendEvent(parent(), &event); 148} 149 150QVariantList TextInputController::selectedRange() 151{ 152 return DumpRenderTreeSupportQt::selectedRange(qobject_cast<QWebPage*>(parent())); 153} 154 155QVariantList TextInputController::firstRectForCharacterRange(int location, int length) 156{ 157 return DumpRenderTreeSupportQt::firstRectForCharacterRange(qobject_cast<QWebPage*>(parent()), location, length); 158} 159