InputMethod.cpp 9.8 KB

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