~ubuntu-branches/ubuntu/trusty/fcitx/trusty-proposed

« back to all changes in this revision

Viewing changes to src/frontend/qt/qfcitxinputcontext.h

  • Committer: Package Import Robot
  • Author(s): Aron Xu
  • Date: 2013-02-10 17:03:56 UTC
  • mfrom: (1.3.18) (33.1.3 experimental)
  • Revision ID: package-import@ubuntu.com-20130210170356-2yuv6xy3ed378kn0
Tags: 1:4.2.7-1
* New upstream release.
* New binary packages:
  - fcitx-libs-gclient: D-Bus client library for Glib
  - fcitx-libs-qt: D-Bus client library for Qt
  - fcitx-module-quickphrase-editor: Quick Phrase editor module

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2011~2012 by CSSlayer                                   *
 
3
 *                                                                         *
 
4
 *   This program is free software; you can redistribute it and/or modify  *
 
5
 *   it under the terms of the GNU General Public License as published by  *
 
6
 *   the Free Software Foundation; either version 2 of the License, or     *
 
7
 *   (at your option) any later version.                                   *
 
8
 *                                                                         *
 
9
 *   This program is distributed in the hope that it will be useful,       *
 
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
12
 *   GNU General Public License for more details.                          *
 
13
 *                                                                         *
 
14
 *   You should have received a copy of the GNU General Public License     *
 
15
 *   along with this program; if not, write to the                         *
 
16
 *   Free Software Foundation, Inc.,                                       *
 
17
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
 
18
 ***************************************************************************/
 
19
 
 
20
#ifndef __FCITX_INPUT_CONTEXT_H_
 
21
#define __FCITX_INPUT_CONTEXT_H_
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include <QInputContext>
 
26
#include <QList>
 
27
#include <QDBusConnection>
 
28
#include <QDir>
 
29
#include <QApplication>
 
30
#include <QWeakPointer>
 
31
 
 
32
#include "fcitx-qt/fcitxqtinputcontextproxy.h"
 
33
#include "fcitx-qt/fcitxqtinputmethodproxy.h"
 
34
#include "fcitx-config/hotkey.h"
 
35
#include "fcitx/ime.h"
 
36
 
 
37
#if defined(Q_WS_X11)
 
38
#include <X11/Xlib.h>
 
39
#include "fcitx/frontend.h"
 
40
 
 
41
struct FcitxQtICData {
 
42
    FcitxQtICData() : capacity(0), proxy(0), surroundingAnchor(-1), surroundingCursor(-1) {}
 
43
    ~FcitxQtICData() {
 
44
        if (proxy && proxy->isValid()) {
 
45
            proxy->DestroyIC();
 
46
            delete proxy;
 
47
        }
 
48
    }
 
49
    QFlags<FcitxCapacityFlags> capacity;
 
50
    QPointer<FcitxQtInputContextProxy> proxy;
 
51
    QRect rect;
 
52
    QString surroundingText;
 
53
    int surroundingAnchor;
 
54
    int surroundingCursor;
 
55
};
 
56
 
 
57
class FcitxQtConnection;
 
58
 
 
59
class ProcessKeyWatcher : public QDBusPendingCallWatcher
 
60
{
 
61
    Q_OBJECT
 
62
public:
 
63
    ProcessKeyWatcher(XEvent* e, KeySym s, const QDBusPendingCall &call, QObject *parent = 0) : QDBusPendingCallWatcher(call, parent) {
 
64
        event = static_cast<XEvent*>(malloc(sizeof(XEvent)));
 
65
        *event = *e;
 
66
        sym = s;
 
67
    }
 
68
 
 
69
    virtual ~ProcessKeyWatcher() {
 
70
        free(event);
 
71
    }
 
72
 
 
73
public slots:
 
74
    void processEvent() {
 
75
        qApp->x11ProcessEvent(event);
 
76
        deleteLater();
 
77
    }
 
78
public:
 
79
    XEvent* event;
 
80
    KeySym sym;
 
81
};
 
82
#endif
 
83
 
 
84
 
 
85
#define FCITX_IDENTIFIER_NAME "fcitx"
 
86
#define FCITX_MAX_COMPOSE_LEN 7
 
87
 
 
88
class QFcitxInputContext : public QInputContext
 
89
{
 
90
    Q_OBJECT
 
91
public:
 
92
    QFcitxInputContext();
 
93
    ~QFcitxInputContext();
 
94
 
 
95
    virtual QString identifierName();
 
96
    virtual QString language();
 
97
    virtual void reset();
 
98
    virtual bool isComposing() const;
 
99
    virtual void update();
 
100
    virtual void setFocusWidget(QWidget *w);
 
101
 
 
102
    virtual void widgetDestroyed(QWidget *w);
 
103
 
 
104
#if defined(Q_WS_X11) && defined(ENABLE_X11)
 
105
    virtual bool x11FilterEvent(QWidget *keywidget, XEvent *event);
 
106
#endif // Q_WS_X11
 
107
    virtual bool filterEvent(const QEvent* event);
 
108
    virtual void mouseHandler(int x, QMouseEvent* event);
 
109
 
 
110
private Q_SLOTS:
 
111
    void connected();
 
112
    void createInputContext(WId w);
 
113
    void cleanUp();
 
114
    void commitString(const QString& str);
 
115
    void updateFormattedPreedit(const FcitxQtFormattedPreeditList& preeditList, int cursorPos);
 
116
    void forwardKey(uint keyval, uint state, int type);
 
117
    void deleteSurroundingText(int offset, uint nchar);
 
118
    void createInputContextFinished(QDBusPendingCallWatcher* watcher);
 
119
    void updateCursor();
 
120
#if defined(Q_WS_X11) && defined(ENABLE_X11)
 
121
    void x11ProcessKeyEventCallback(QDBusPendingCallWatcher* watcher);
 
122
#endif
 
123
private:
 
124
    QWidget* validFocusWidget();
 
125
    bool processCompose(uint keyval, uint state, FcitxKeyEventType event);
 
126
    bool checkAlgorithmically();
 
127
    bool checkCompactTable(const struct _FcitxComposeTableCompact *table);
 
128
#if defined(Q_WS_X11) && defined(ENABLE_X11)
 
129
    bool x11FilterEventFallback(XEvent *event, KeySym sym);
 
130
    XEvent* createXEvent(Display* dpy, WId wid, uint keyval, uint state, int type);
 
131
#endif // Q_WS_X11
 
132
    QKeyEvent* createKeyEvent(uint keyval, uint state, int type);
 
133
    bool isValid();
 
134
    FcitxQtInputContextProxy* validIC();
 
135
    FcitxQtInputContextProxy* validICByWidget(QWidget* w);
 
136
 
 
137
    void addCapacity(FcitxQtICData* data, QFlags<FcitxCapacityFlags> capacity, bool forceUpdate = false)
 
138
    {
 
139
        QFlags< FcitxCapacityFlags > newcaps = data->capacity | capacity;
 
140
        if (data->capacity != newcaps || forceUpdate) {
 
141
            data->capacity = newcaps;
 
142
            updateCapacity(data);
 
143
        }
 
144
    }
 
145
 
 
146
    void removeCapacity(FcitxQtICData* data, QFlags<FcitxCapacityFlags> capacity, bool forceUpdate = false)
 
147
    {
 
148
        QFlags< FcitxCapacityFlags > newcaps = data->capacity & (~capacity);
 
149
        if (data->capacity != newcaps || forceUpdate) {
 
150
            data->capacity = newcaps;
 
151
            updateCapacity(data);
 
152
        }
 
153
    }
 
154
 
 
155
    void updateCapacity(FcitxQtICData* data);
 
156
    void commitPreedit();
 
157
    void createICData(QWidget* w);
 
158
 
 
159
    FcitxQtInputMethodProxy* m_improxy;
 
160
    uint m_compose_buffer[FCITX_MAX_COMPOSE_LEN + 1];
 
161
    int m_n_compose;
 
162
    QString m_preedit;
 
163
    QString m_commitPreedit;
 
164
    FcitxQtFormattedPreeditList m_preeditList;
 
165
    int m_cursorPos;
 
166
    bool m_useSurroundingText;
 
167
    bool m_syncMode;
 
168
    FcitxQtConnection* m_connection;
 
169
    QHash<WId, FcitxQtICData*> m_icMap;
 
170
};
 
171
 
 
172
#endif //__FCITX_INPUT_CONTEXT_H_
 
173
 
 
174
// kate: indent-mode cstyle; space-indent on; indent-width 0;