1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 BogDan Vatra <bogdan@kde.org>
4 ** Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com>
5 ** Contact: https://www.qt.io/licensing/
6 **
7 ** This file is part of the plugins of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see https://www.qt.io/terms-conditions. For further
16 ** information use the contact form at https://www.qt.io/contact-us.
17 **
18 ** GNU Lesser General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU Lesser
20 ** General Public License version 3 as published by the Free Software
21 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
22 ** packaging of this file. Please review the following information to
23 ** ensure the GNU Lesser General Public License version 3 requirements
24 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25 **
26 ** GNU General Public License Usage
27 ** Alternatively, this file may be used under the terms of the GNU
28 ** General Public License version 2.0 or (at your option) the GNU General
29 ** Public license version 3 or any later version approved by the KDE Free
30 ** Qt Foundation. The licenses are as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32 ** included in the packaging of this file. Please review the following
33 ** information to ensure the GNU General Public License requirements will
34 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35 ** https://www.gnu.org/licenses/gpl-3.0.html.
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #ifndef ANDROIDINPUTCONTEXT_H
42 #define ANDROIDINPUTCONTEXT_H
43 
44 #include <qpa/qplatforminputcontext.h>
45 #include <functional>
46 #include <jni.h>
47 #include <qevent.h>
48 #include <QTimer>
49 
50 QT_BEGIN_NAMESPACE
51 
52 class QAndroidInputContext: public QPlatformInputContext
53 {
54     Q_OBJECT
55     enum CapsMode
56     {
57         CAP_MODE_CHARACTERS = 0x00001000,
58         CAP_MODE_SENTENCES  = 0x00004000,
59         CAP_MODE_WORDS      = 0x00002000
60     };
61 
62 public:
63     enum EditContext : uint32_t {
64         CutButton       = 1 << 0,
65         CopyButton      = 1 << 1,
66         PasteButton     = 1 << 2,
67         SelectAllButton = 1 << 3,
68         AllButtons      = CutButton | CopyButton | PasteButton | SelectAllButton
69     };
70 
71     enum HandleMode {
72         Hidden        = 0,
73         ShowCursor    = 1,
74         ShowSelection = 2,
75         ShowEditPopup = 0x100
76     };
77     Q_DECLARE_FLAGS(HandleModes, HandleMode)
78 
79     struct ExtractedText
80     {
ExtractedTextExtractedText81         ExtractedText() { clear(); }
82 
clearExtractedText83         void clear()
84         {
85             partialEndOffset = partialStartOffset = selectionEnd = selectionStart = startOffset = -1;
86             text.clear();
87         }
88 
89         int partialEndOffset;
90         int partialStartOffset;
91         int selectionEnd;
92         int selectionStart;
93         int startOffset;
94         QString text;
95     };
96 
97 public:
98     QAndroidInputContext();
99     ~QAndroidInputContext();
100     static QAndroidInputContext * androidInputContext();
isValid()101     bool isValid() const override { return true; }
102 
103     void reset() override;
104     void commit() override;
105     void update(Qt::InputMethodQueries queries) override;
106     void invokeAction(QInputMethod::Action action, int cursorPosition) override;
107     QRectF keyboardRect() const override;
108     bool isAnimating() const override;
109     void showInputPanel() override;
110     void hideInputPanel() override;
111     bool isInputPanelVisible() const override;
112 
113     bool isComposing() const;
114     void clear();
115     void setFocusObject(QObject *object) override;
116     void sendShortcut(const QKeySequence &);
117 
118     //---------------//
119     jboolean beginBatchEdit();
120     jboolean endBatchEdit();
121     jboolean commitText(const QString &text, jint newCursorPosition);
122     jboolean deleteSurroundingText(jint leftLength, jint rightLength);
123     jboolean finishComposingText();
124     jint getCursorCapsMode(jint reqModes);
125     const ExtractedText &getExtractedText(jint hintMaxChars, jint hintMaxLines, jint flags);
126     QString getSelectedText(jint flags);
127     QString getTextAfterCursor(jint length, jint flags);
128     QString getTextBeforeCursor(jint length, jint flags);
129     jboolean setComposingText(const QString &text, jint newCursorPosition);
130     jboolean setComposingRegion(jint start, jint end);
131     jboolean setSelection(jint start, jint end);
132     jboolean selectAll();
133     jboolean cut();
134     jboolean copy();
135     jboolean copyURL();
136     jboolean paste();
137 
138 public slots:
139     void safeCall(const std::function<void()> &func, Qt::ConnectionType conType = Qt::BlockingQueuedConnection);
140     void updateCursorPosition();
141     void updateSelectionHandles();
142     void handleLocationChanged(int handleId, int x, int y);
143     void touchDown(int x, int y);
144     void longPress(int x, int y);
145     void keyDown();
146     void hideSelectionHandles();
147 
148 private slots:
149     void showInputPanelLater(Qt::ApplicationState);
150 
151 private:
152     void sendInputMethodEvent(QInputMethodEvent *event);
153     QSharedPointer<QInputMethodQueryEvent> focusObjectInputMethodQuery(Qt::InputMethodQueries queries = Qt::ImQueryAll);
154     bool focusObjectIsComposing() const;
155     void focusObjectStartComposing();
156     bool focusObjectStopComposing();
157 
158 private:
159     ExtractedText m_extractedText;
160     QString m_composingText;
161     int m_composingTextStart;
162     int m_composingCursor;
163     QMetaObject::Connection m_updateCursorPosConnection;
164     HandleModes m_handleMode;
165     int m_batchEditNestingLevel;
166     QObject *m_focusObject;
167     QTimer m_hideCursorHandleTimer;
168 };
169 Q_DECLARE_OPERATORS_FOR_FLAGS(QAndroidInputContext::HandleModes)
170 QT_END_NAMESPACE
171 
172 #endif // ANDROIDINPUTCONTEXT_H
173