InputMethod.cpp 9.9 KB

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