|
| 1 | +// Copyright (c) 2021 Riverbank Computing Limited |
| 2 | +// Copyright (c) 2011 Archaeopteryx Software, Inc. |
| 3 | +// Copyright (c) 1990-2011, Scientific Toolworks, Inc. |
| 4 | +// |
| 5 | +// The License.txt file describes the conditions under which this software may |
| 6 | +// be distributed. |
| 7 | + |
| 8 | + |
| 9 | +#include <qglobal.h> |
| 10 | + |
| 11 | +#include <QColor> |
| 12 | +#include <QFont> |
| 13 | +#include <QInputMethodEvent> |
| 14 | +#include <QRect> |
| 15 | +#include <QTextCharFormat> |
| 16 | +#include <QTextFormat> |
| 17 | +#include <QVariant> |
| 18 | +#include <QVarLengthArray> |
| 19 | + |
| 20 | +#include "Qsci/qsciscintillabase.h" |
| 21 | +#include "ScintillaQt.h" |
| 22 | + |
| 23 | + |
| 24 | +#define INDIC_INPUTMETHOD 24 |
| 25 | + |
| 26 | +#define MAXLENINPUTIME 200 |
| 27 | +#define SC_INDICATOR_INPUT INDIC_IME |
| 28 | +#define SC_INDICATOR_TARGET INDIC_IME+1 |
| 29 | +#define SC_INDICATOR_CONVERTED INDIC_IME+2 |
| 30 | +#define SC_INDICATOR_UNKNOWN INDIC_IME_MAX |
| 31 | + |
| 32 | + |
| 33 | +static bool IsHangul(const QChar qchar) |
| 34 | +{ |
| 35 | + int unicode = (int)qchar.unicode(); |
| 36 | + // Korean character ranges used for preedit chars. |
| 37 | + // http://www.programminginkorean.com/programming/hangul-in-unicode/ |
| 38 | + const bool HangulJamo = (0x1100 <= unicode && unicode <= 0x11FF); |
| 39 | + const bool HangulCompatibleJamo = (0x3130 <= unicode && unicode <= 0x318F); |
| 40 | + const bool HangulJamoExtendedA = (0xA960 <= unicode && unicode <= 0xA97F); |
| 41 | + const bool HangulJamoExtendedB = (0xD7B0 <= unicode && unicode <= 0xD7FF); |
| 42 | + const bool HangulSyllable = (0xAC00 <= unicode && unicode <= 0xD7A3); |
| 43 | + return HangulJamo || HangulCompatibleJamo || HangulSyllable || |
| 44 | + HangulJamoExtendedA || HangulJamoExtendedB; |
| 45 | +} |
| 46 | + |
| 47 | +static void MoveImeCarets(QsciScintillaQt *sqt, int offset) |
| 48 | +{ |
| 49 | + // Move carets relatively by bytes |
| 50 | + for (size_t r=0; r < sqt->sel.Count(); r++) { |
| 51 | + int positionInsert = sqt->sel.Range(r).Start().Position(); |
| 52 | + sqt->sel.Range(r).caret.SetPosition(positionInsert + offset); |
| 53 | + sqt->sel.Range(r).anchor.SetPosition(positionInsert + offset); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +static void DrawImeIndicator(QsciScintillaQt *sqt, int indicator, int len) |
| 58 | +{ |
| 59 | + // Emulate the visual style of IME characters with indicators. |
| 60 | + // Draw an indicator on the character before caret by the character bytes of len |
| 61 | + // so it should be called after AddCharUTF(). |
| 62 | + // It does not affect caret positions. |
| 63 | + if (indicator < 8 || indicator > INDIC_MAX) { |
| 64 | + return; |
| 65 | + } |
| 66 | + sqt->pdoc->DecorationSetCurrentIndicator(indicator); |
| 67 | + for (size_t r=0; r< sqt-> sel.Count(); r++) { |
| 68 | + int positionInsert = sqt->sel.Range(r).Start().Position(); |
| 69 | + sqt->pdoc->DecorationFillRange(positionInsert - len, 1, len); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +static int GetImeCaretPos(QInputMethodEvent *event) |
| 74 | +{ |
| 75 | + foreach (QInputMethodEvent::Attribute attr, event->attributes()) { |
| 76 | + if (attr.type == QInputMethodEvent::Cursor) |
| 77 | + return attr.start; |
| 78 | + } |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
| 82 | +static std::vector<int> MapImeIndicators(QInputMethodEvent *event) |
| 83 | +{ |
| 84 | + std::vector<int> imeIndicator(event->preeditString().size(), SC_INDICATOR_UNKNOWN); |
| 85 | + foreach (QInputMethodEvent::Attribute attr, event->attributes()) { |
| 86 | + if (attr.type == QInputMethodEvent::TextFormat) { |
| 87 | + QTextFormat format = attr.value.value<QTextFormat>(); |
| 88 | + QTextCharFormat charFormat = format.toCharFormat(); |
| 89 | + |
| 90 | + int indicator = SC_INDICATOR_UNKNOWN; |
| 91 | + switch (charFormat.underlineStyle()) { |
| 92 | + case QTextCharFormat::NoUnderline: // win32, linux |
| 93 | + indicator = SC_INDICATOR_TARGET; |
| 94 | + break; |
| 95 | + case QTextCharFormat::SingleUnderline: // osx |
| 96 | + case QTextCharFormat::DashUnderline: // win32, linux |
| 97 | + indicator = SC_INDICATOR_INPUT; |
| 98 | + break; |
| 99 | + case QTextCharFormat::DotLine: |
| 100 | + case QTextCharFormat::DashDotLine: |
| 101 | + case QTextCharFormat::WaveUnderline: |
| 102 | + case QTextCharFormat::SpellCheckUnderline: |
| 103 | + indicator = SC_INDICATOR_CONVERTED; |
| 104 | + break; |
| 105 | + |
| 106 | + default: |
| 107 | + indicator = SC_INDICATOR_UNKNOWN; |
| 108 | + } |
| 109 | + |
| 110 | + if (format.hasProperty(QTextFormat::BackgroundBrush)) // win32, linux |
| 111 | + indicator = SC_INDICATOR_TARGET; |
| 112 | + |
| 113 | +#ifdef Q_OS_OSX |
| 114 | + if (charFormat.underlineStyle() == QTextCharFormat::SingleUnderline) { |
| 115 | + QColor uc = charFormat.underlineColor(); |
| 116 | + if (uc.lightness() < 2) { // osx |
| 117 | + indicator = SC_INDICATOR_TARGET; |
| 118 | + } |
| 119 | + } |
| 120 | +#endif |
| 121 | + |
| 122 | + for (int i = attr.start; i < attr.start+attr.length; i++) { |
| 123 | + imeIndicator[i] = indicator; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + return imeIndicator; |
| 128 | +} |
| 129 | + |
| 130 | +void QsciScintillaBase::inputMethodEvent(QInputMethodEvent *event) |
| 131 | +{ |
| 132 | + // Copy & paste by johnsonj with a lot of helps of Neil |
| 133 | + // Great thanks for my forerunners, jiniya and BLUEnLIVE |
| 134 | + |
| 135 | + if (sci->pdoc->IsReadOnly() || sci->SelectionContainsProtected()) { |
| 136 | + // Here, a canceling and/or completing composition function is needed. |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + if (sci->pdoc->TentativeActive()) { |
| 141 | + sci->pdoc->TentativeUndo(); |
| 142 | + } else { |
| 143 | + // No tentative undo means start of this composition so |
| 144 | + // Fill in any virtual spaces. |
| 145 | + sci->ClearBeforeTentativeStart(); |
| 146 | + } |
| 147 | + |
| 148 | + sci->view.imeCaretBlockOverride = false; |
| 149 | + |
| 150 | + if (!event->commitString().isEmpty()) { |
| 151 | + const QString commitStr = event->commitString(); |
| 152 | + const unsigned int commitStrLen = commitStr.length(); |
| 153 | + |
| 154 | + for (unsigned int i = 0; i < commitStrLen;) { |
| 155 | + const unsigned int ucWidth = commitStr.at(i).isHighSurrogate() ? 2 : 1; |
| 156 | + const QString oneCharUTF16 = commitStr.mid(i, ucWidth); |
| 157 | + const QByteArray oneChar = textAsBytes(oneCharUTF16); |
| 158 | + const int oneCharLen = oneChar.length(); |
| 159 | + |
| 160 | + sci->AddCharUTF(oneChar.data(), oneCharLen); |
| 161 | + i += ucWidth; |
| 162 | + } |
| 163 | + |
| 164 | + } else if (!event->preeditString().isEmpty()) { |
| 165 | + const QString preeditStr = event->preeditString(); |
| 166 | + const unsigned int preeditStrLen = preeditStr.length(); |
| 167 | + if ((preeditStrLen == 0) || (preeditStrLen > MAXLENINPUTIME)) { |
| 168 | + sci->ShowCaretAtCurrentPosition(); |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + sci->pdoc->TentativeStart(); // TentativeActive() from now on. |
| 173 | + |
| 174 | + std::vector<int> imeIndicator = MapImeIndicators(event); |
| 175 | + |
| 176 | + const bool recording = sci->recordingMacro; |
| 177 | + sci->recordingMacro = false; |
| 178 | + for (unsigned int i = 0; i < preeditStrLen;) { |
| 179 | + const unsigned int ucWidth = preeditStr.at(i).isHighSurrogate() ? 2 : 1; |
| 180 | + const QString oneCharUTF16 = preeditStr.mid(i, ucWidth); |
| 181 | + const QByteArray oneChar = textAsBytes(oneCharUTF16); |
| 182 | + const int oneCharLen = oneChar.length(); |
| 183 | + |
| 184 | + sci->AddCharUTF(oneChar.data(), oneCharLen); |
| 185 | + |
| 186 | + DrawImeIndicator(sci, imeIndicator[i], oneCharLen); |
| 187 | + i += ucWidth; |
| 188 | + } |
| 189 | + sci->recordingMacro = recording; |
| 190 | + |
| 191 | + // Move IME carets. |
| 192 | + int imeCaretPos = GetImeCaretPos(event); |
| 193 | + int imeEndToImeCaretU16 = imeCaretPos - preeditStrLen; |
| 194 | + int imeCaretPosDoc = sci->pdoc->GetRelativePositionUTF16(sci->CurrentPosition(), imeEndToImeCaretU16); |
| 195 | + |
| 196 | + MoveImeCarets(sci, - sci->CurrentPosition() + imeCaretPosDoc); |
| 197 | + |
| 198 | + if (IsHangul(preeditStr.at(0))) { |
| 199 | +#ifndef Q_OS_WIN |
| 200 | + if (imeCaretPos > 0) { |
| 201 | + int oneCharBefore = sci->pdoc->GetRelativePosition(sci->CurrentPosition(), -1); |
| 202 | + MoveImeCarets(sci, - sci->CurrentPosition() + oneCharBefore); |
| 203 | + } |
| 204 | +#endif |
| 205 | + sci->view.imeCaretBlockOverride = true; |
| 206 | + } |
| 207 | + |
| 208 | + // Set candidate box position for Qt::ImCursorRectangle. |
| 209 | + preeditPos = sci->CurrentPosition(); |
| 210 | + sci->EnsureCaretVisible(); |
| 211 | + updateMicroFocus(); |
| 212 | + } |
| 213 | + sci->ShowCaretAtCurrentPosition(); |
| 214 | +} |
| 215 | + |
| 216 | +QVariant QsciScintillaBase::inputMethodQuery(Qt::InputMethodQuery query) const |
| 217 | +{ |
| 218 | + int pos = SendScintilla(SCI_GETCURRENTPOS); |
| 219 | + int line = SendScintilla(SCI_LINEFROMPOSITION, pos); |
| 220 | + |
| 221 | + switch (query) { |
| 222 | + case Qt::ImHints: |
| 223 | + return QWidget::inputMethodQuery(query); |
| 224 | + |
| 225 | + case Qt::ImCursorRectangle: |
| 226 | + { |
| 227 | + int startPos = (preeditPos >= 0) ? preeditPos : pos; |
| 228 | + Scintilla::Point pt = sci->LocationFromPosition(startPos); |
| 229 | + int width = SendScintilla(SCI_GETCARETWIDTH); |
| 230 | + int height = SendScintilla(SCI_TEXTHEIGHT, line); |
| 231 | + return QRect(pt.x, pt.y, width, height); |
| 232 | + } |
| 233 | + |
| 234 | + case Qt::ImFont: |
| 235 | + { |
| 236 | + char fontName[64]; |
| 237 | + int style = SendScintilla(SCI_GETSTYLEAT, pos); |
| 238 | + int len = SendScintilla(SCI_STYLEGETFONT, style, (sptr_t)fontName); |
| 239 | + int size = SendScintilla(SCI_STYLEGETSIZE, style); |
| 240 | + bool italic = SendScintilla(SCI_STYLEGETITALIC, style); |
| 241 | + int weight = SendScintilla(SCI_STYLEGETBOLD, style) ? QFont::Bold : -1; |
| 242 | + return QFont(QString::fromUtf8(fontName, len), size, weight, italic); |
| 243 | + } |
| 244 | + |
| 245 | + case Qt::ImCursorPosition: |
| 246 | + { |
| 247 | + int paraStart = sci->pdoc->ParaUp(pos); |
| 248 | + return pos - paraStart; |
| 249 | + } |
| 250 | + |
| 251 | + case Qt::ImSurroundingText: |
| 252 | + { |
| 253 | + int paraStart = sci->pdoc->ParaUp(pos); |
| 254 | + int paraEnd = sci->pdoc->ParaDown(pos); |
| 255 | + QVarLengthArray<char,1024> buffer(paraEnd - paraStart + 1); |
| 256 | + |
| 257 | + Sci_CharacterRange charRange; |
| 258 | + charRange.cpMin = paraStart; |
| 259 | + charRange.cpMax = paraEnd; |
| 260 | + |
| 261 | + Sci_TextRange textRange; |
| 262 | + textRange.chrg = charRange; |
| 263 | + textRange.lpstrText = buffer.data(); |
| 264 | + |
| 265 | + SendScintilla(SCI_GETTEXTRANGE, 0, (sptr_t)&textRange); |
| 266 | + |
| 267 | + return bytesAsText(buffer.constData()); |
| 268 | + } |
| 269 | + |
| 270 | + case Qt::ImCurrentSelection: |
| 271 | + { |
| 272 | + QVarLengthArray<char,1024> buffer(SendScintilla(SCI_GETSELTEXT)); |
| 273 | + SendScintilla(SCI_GETSELTEXT, 0, (sptr_t)buffer.data()); |
| 274 | + |
| 275 | + return bytesAsText(buffer.constData()); |
| 276 | + } |
| 277 | + |
| 278 | + default: |
| 279 | + return QVariant(); |
| 280 | + } |
| 281 | +} |
0 commit comments