summaryrefslogtreecommitdiff
blob: e417a06157166ee3602bd20b91706db91113438b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
https://github.com/fcitx/fcitx/commit/14faccfbb0d87e06c25d182ae842808d18be3dc7
https://github.com/fcitx/fcitx/commit/216a09e3ec056f272eebfbe82809b803d86012cb

--- /src/frontend/qt/qfcitxinputcontext.cpp
+++ /src/frontend/qt/qfcitxinputcontext.cpp
@@ -232,6 +232,20 @@
                         anchor = var2.toInt();
                     else
                         anchor = cursor;
+
+                    // adjust it to real character size
+                    // QTBUG-25536;
+                    QVector<uint> tempUCS4 = text.leftRef(cursor).toUcs4();
+                    while (!tempUCS4.empty() && tempUCS4.last() == 0) {
+                        tempUCS4.pop_back();
+                    }
+                    cursor = tempUCS4.size();
+                    tempUCS4 = text.leftRef(anchor).toUcs4();
+                    while (!tempUCS4.empty() && tempUCS4.last() == 0) {
+                        tempUCS4.pop_back();
+                    }
+                    anchor = tempUCS4.size();
+
                     if (data->surroundingText != text) {
                         data->surroundingText = text;
                         proxy->SetSurroundingText(text, cursor, anchor);
@@ -581,6 +595,7 @@
             delete data->proxy;
         }
         data->proxy = new FcitxQtInputContextProxy(m_connection->serviceName(), path, *m_connection->connection(), this);
+        data->proxy->setProperty("icData", qVariantFromValue(static_cast<void*>(data)));
         connect(data->proxy, SIGNAL(CommitString(QString)), this, SLOT(commitString(QString)));
         connect(data->proxy, SIGNAL(ForwardKey(uint, uint, int)), this, SLOT(forwardKey(uint, uint, int)));
         connect(data->proxy, SIGNAL(UpdateFormattedPreedit(FcitxQtFormattedPreeditList,int)), this, SLOT(updateFormattedPreedit(FcitxQtFormattedPreeditList,int)));
@@ -680,11 +695,57 @@
     sendEvent(event);
 }
 
-void QFcitxInputContext::deleteSurroundingText(int offset, uint nchar)
+void QFcitxInputContext::deleteSurroundingText(int offset, uint _nchar)
 {
     QInputMethodEvent event;
-    event.setCommitString("", offset, nchar);
-    sendEvent(event);
+
+    FcitxQtInputContextProxy *proxy = qobject_cast<FcitxQtInputContextProxy*>(sender());
+    if (!proxy) {
+        return;
+    }
+
+    FcitxQtICData *data = static_cast<FcitxQtICData*>(proxy->property("icData").value<void *>());
+    QVector<uint> ucsText = data->surroundingText.toUcs4();
+
+    // QTBUG-25536
+    while (!ucsText.empty() && ucsText.last() == 0) {
+        ucsText.pop_back();
+    }
+
+    int cursor = data->surroundingCursor;
+    // make nchar signed so we are safer
+    int nchar = _nchar;
+    // Qt's reconvert semantics is different from gtk's. It doesn't count the current
+    // selection. Discard selection from nchar.
+    if (data->surroundingAnchor < data->surroundingCursor) {
+        nchar -= data->surroundingCursor - data->surroundingAnchor;
+        offset += data->surroundingCursor - data->surroundingAnchor;
+        cursor = data->surroundingAnchor;
+    } else if (data->surroundingAnchor > data->surroundingCursor) {
+        nchar -= data->surroundingAnchor - data->surroundingCursor;
+        cursor = data->surroundingCursor;
+    }
+
+    // validates
+    if (nchar >= 0 && cursor + offset >= 0 && cursor + offset + nchar < ucsText.size()) {
+        // order matters
+        QVector<uint> replacedChars = ucsText.mid(cursor + offset, nchar);
+        nchar = QString::fromUcs4(replacedChars.data(), replacedChars.size()).size();
+
+        int start, len;
+        if (offset >= 0) {
+            start = cursor;
+            len = offset;
+        } else {
+            start = cursor;
+            len = -offset;
+        }
+
+        QVector<uint> prefixedChars = ucsText.mid(start, len);
+        offset = QString::fromUcs4(prefixedChars.data(), prefixedChars.size()).size() * (offset >= 0 ? 1 : -1);
+        event.setCommitString("", offset, nchar);
+        sendEvent(event);
+    }
 }
 
 void QFcitxInputContext::forwardKey(uint keyval, uint state, int type)