summaryrefslogtreecommitdiffstats
path: root/ibus/inputcontext.py
blob: c9aa634203736918b0e2f4d6af8a94a76f8ba622 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# vim:set et sts=4 sw=4:
#
# ibus - The Input Bus
#
# Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA  02111-1307  USA

__all__ = (
        "InputContext",
    )

import sys
import gobject
import dbus
import dbus.lowlevel
import object
import common
import serializable

class InputContext(object.Object):
    __gsignals__ = {
        "commit-text" : (
            gobject.SIGNAL_RUN_LAST,
            gobject.TYPE_NONE,
            (gobject.TYPE_PYOBJECT, )
        ),
        "update-preedit-text" : (
            gobject.SIGNAL_RUN_LAST,
            gobject.TYPE_NONE,
            (gobject.TYPE_PYOBJECT, gobject.TYPE_UINT, gobject.TYPE_BOOLEAN)
        ),
        "show-preedit-text" : (
            gobject.SIGNAL_RUN_LAST,
            gobject.TYPE_NONE,
            ()
        ),
        "hide-preedit-text" : (
            gobject.SIGNAL_RUN_LAST,
            gobject.TYPE_NONE,
            ()
        ),
        "update-auxiliary-text" : (
            gobject.SIGNAL_RUN_LAST,
            gobject.TYPE_NONE,
            (gobject.TYPE_PYOBJECT, gobject.TYPE_BOOLEAN)
        ),
        "show-auxiliary-text" : (
            gobject.SIGNAL_RUN_LAST,
            gobject.TYPE_NONE,
            ()
        ),
        "hide-auxiliary-text" : (
            gobject.SIGNAL_RUN_LAST,
            gobject.TYPE_NONE,
            ()
        ),
        "update-lookup-table" : (
            gobject.SIGNAL_RUN_LAST,
            gobject.TYPE_NONE,
            (gobject.TYPE_PYOBJECT, gobject.TYPE_BOOLEAN)
        ),
        "show-lookup-table" : (
            gobject.SIGNAL_RUN_LAST,
            gobject.TYPE_NONE,
            ()
        ),
        "hide-lookup-table" : (
            gobject.SIGNAL_RUN_LAST,
            gobject.TYPE_NONE,
            ()
        ),
        "page-up-lookup-table" : (
            gobject.SIGNAL_RUN_LAST,
            gobject.TYPE_NONE,
            ()
        ),
        "page-down-lookup-table" : (
            gobject.SIGNAL_RUN_LAST,
            gobject.TYPE_NONE,
            ()
        ),
        "cursor-up-lookup-table" : (
            gobject.SIGNAL_RUN_LAST,
            gobject.TYPE_NONE,
            ()
        ),
        "cursor-down-lookup-table" : (
            gobject.SIGNAL_RUN_LAST,
            gobject.TYPE_NONE,
            ()
        ),
        "enabled" : (
            gobject.SIGNAL_RUN_LAST,
            gobject.TYPE_NONE,
            ()
        ),
        "disabled" : (
            gobject.SIGNAL_RUN_LAST,
            gobject.TYPE_NONE,
            ()
        ),
    }

    def __init__(self, bus, path):
        super(InputContext, self).__init__()

        self.__bus = bus
        self.__context = bus.get_dbusconn().get_object(common.IBUS_SERVICE_IBUS, path)
        self.__signal_matches = []

        m = self.__context.connect_to_signal("CommitText", self.__commit_text_cb)
        self.__signal_matches.append(m)
        m = self.__context.connect_to_signal("UpdatePreeditText", self.__update_preedit_text_cb)
        self.__signal_matches.append(m)
        m = self.__context.connect_to_signal("UpdateAuxiliaryText", self.__update_auxiliary_text_cb)
        self.__signal_matches.append(m)
        m = self.__context.connect_to_signal("UpdateLookupTable", self.__update_lookup_table_cb)
        self.__signal_matches.append(m)

        m = self.__context.connect_to_signal("Enabled",             lambda *args: self.emit("enabled"))
        self.__signal_matches.append(m)
        m = self.__context.connect_to_signal("Disabled",            lambda *args: self.emit("disabled"))
        self.__signal_matches.append(m)
        m = self.__context.connect_to_signal("ShowPreeditText",     lambda *args: self.emit("show-preedit-text"))
        self.__signal_matches.append(m)
        m = self.__context.connect_to_signal("HidePreeditText",     lambda *args: self.emit("hide-preedit-text"))
        self.__signal_matches.append(m)
        m = self.__context.connect_to_signal("ShowAuxiliaryText",   lambda *args: self.emit("show-auxiliary-text"))
        self.__signal_matches.append(m)
        m = self.__context.connect_to_signal("HideAuxiliaryText",   lambda *args: self.emit("hide-auxiliary-text"))
        self.__signal_matches.append(m)
        m = self.__context.connect_to_signal("ShowLookupTable",     lambda *args: self.emit("show-lookup-table"))
        self.__signal_matches.append(m)
        m = self.__context.connect_to_signal("HideLookupTable",     lambda *argss: self.emit("hide-lookup-table"))
        self.__signal_matches.append(m)
        m = self.__context.connect_to_signal("PageUpLookupTable",   lambda *args: self.emit("page-up-lookup-table"))
        self.__signal_matches.append(m)
        m = self.__context.connect_to_signal("PageDownLookupTable", lambda *args: self.emit("page-down-lookup-table"))
        self.__signal_matches.append(m)
        m = self.__context.connect_to_signal("CursorUpLookupTable", lambda *args: self.emit("cursor-up-lookup-table"))
        self.__signal_matches.append(m)
        m = self.__context.connect_to_signal("CursorDownLookupTable", lambda *args: self.emit("cursor-down-lookup-table"))
        self.__signal_matches.append(m)

    def __commit_text_cb(self, *args):
        text = serializable.deserialize_object(args[0])
        self.emit("commit-text", text)

    def __update_preedit_text_cb(self, *args):
        text = serializable.deserialize_object(args[0])
        cursor_pos = args[1]
        visible = args[2]
        self.emit("update-preedit-text", text, cursor_pos, visible)

    def __update_auxiliary_text_cb(self, *args):
        text = serializable.deserialize_object(args[0])
        visible = args[1]
        self.emit("update-auxiliray-text", text, visible)

    def __update_lookup_table_cb(self, *args):
        table = serializable.deserialize_object(args[0])
        visible = args[1]
        self.emit("update-lookup-table", table, visible)

    def process_key_event(self, keyval, modifiers):
        keyval = dbus.UInt32(keyval)
        modifiers = dbus.UInt32(modifiers)
        return self.__context.ProcessKeyEvent(keyval, modifiers)

    def set_cursor_location(self, x, y, w, h):
        x = dbus.Int32(x)
        y = dbus.Int32(y)
        w = dbus.Int32(w)
        h = dbus.Int32(h)
        return self.__context.SetCursorLocation(x, y, w, h)

    def focus_in(self):
        return self.__context.FocusIn()

    def focus_out(self):
        return self.__context.FocusOut()

    def reset(self):
        return self.__context.Reset()

    def enable(self):
        return self.__context.Enable()

    def disable(self):
        return self.__context.Disable()

    def is_enabled(self):
        return self.__context.IsEnabled()

    def set_capabilities(self, caps):
        caps = dbus.UInt32(caps)
        return self.__context.SetCapabilities(caps)

    def destroy(self):
        self.__context.Destroy()
        super(InputContext, self).destroy()

    def get_engine(self):
        try:
            engine = self.__context.GetEngine()
            engine = serializable.deserialize_object(engine)
            return engine
        except:
            return None

    def set_engine(self, engine):
        return self.__context.SetEngine(engine.name)


def test():
    import gtk
    import gtk.gdk
    from bus import Bus
    import modifier
    import text
    import attribute
    import property
    import lookuptable
    import factory

    class TestWindow(gtk.Window):
        def __init__(self):
            super(TestWindow,self).__init__()

            self.__bus = Bus()
            print self.__bus.get_name()
            self.__bus.connect("disconnected", gtk.main_quit)
            context_path = self.__bus.create_input_context("Test")
            print context_path
            self.__context = InputContext(self.__bus, context_path)
            self.__context.set_capabilities (9)

            self.__context.connect("commit-text", self.__commit_text_cb)
            self.__context.connect("update-preedit-text", self.__update_preedit_text_cb)
            self.__context.connect("show-preedit-text", self.__show_preedit_text_cb)
            self.__context.connect("update-auxiliary-text", self.__update_auxiliary_text_cb)
            self.__context.connect("update-lookup-table", self.__update_lookup_table_cb)
            self.__context.connect("enabled", self.__enabled_cb)
            self.__context.connect("disabled", self.__disabled_cb)

            self.set_events(gtk.gdk.KEY_PRESS_MASK | gtk.gdk.KEY_RELEASE_MASK | gtk.gdk.FOCUS_CHANGE_MASK)

            self.connect("key-press-event", self.__key_press_event_cb)
            self.connect("key-release-event", self.__key_release_event_cb)
            self.connect("delete-event", gtk.main_quit)
            self.connect("focus-in-event", lambda *args: self.__context.focus_in())
            self.connect("focus-out-event", lambda *args: self.__context.focus_out())

            self.show_all()

        def __commit_text_cb(self, context, text):
            print "commit-text:", text.text

        def __update_preedit_text_cb(self, context, text, cursor_pos, visible):
            print "preedit-text:", text.text, cursor_pos, visible

        def __show_preedit_text_cb(self, context):
            print "show-preedit-text"

        def __hide_preedit_text_cb(self, context):
            print "hide-preedit-text"

        def __update_auxiliary_text_cb(self, context, text, visible):
            print "auxiliary-text:", text.text, visible

        def __update_lookup_table_cb(self, context, table, visible):
            print "update-lookup-table:", visible

        def __enabled_cb(self, context):
            print "enabled"
            info = context.get_factory_info()
            print "factory = %s" % info.name

        def __disabled_cb(self, context):
            print "disabled"

        def __key_press_event_cb(self, widget, event):
            self.__context.process_key_event(event.keyval, event.state)

        def __key_release_event_cb(self, widget, event):
            self.__context.process_key_event(event.keyval, event.state | modifier.RELEASE_MASK)

    w = TestWindow()
    gtk.main()

if __name__ == "__main__":
    test()