summaryrefslogtreecommitdiffstats
path: root/ibus/attribute.py
blob: 953ff4d717f67a99a96d8bb330f2b657e885b913 (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
# vim:set et sts=4 sw=4:
#
# ibus - The Input Bus
#
# Copyright (c) 2007-2009 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__ = (
        "ATTR_TYPE_UNDERLINE",
        "ATTR_TYPE_FOREGROUND",
        "ATTR_TYPE_BACKGROUND",
        "ATTR_UNDERLINE_NONE",
        "ATTR_UNDERLINE_SINGLE",
        "ATTR_UNDERLINE_DOUBLE",
        "ATTR_UNDERLINE_LOW",
        "Attribute",
        "AttributeUnderline",
        "AttributeForeground",
        "AttributeBackground",
        "AttrList",
        "ARGB", "RGB"
    )

import dbus
from exception import IBusException
from serializable import *

ATTR_TYPE_UNDERLINE = 1
ATTR_TYPE_FOREGROUND = 2
ATTR_TYPE_BACKGROUND = 3

ATTR_UNDERLINE_NONE = 0
ATTR_UNDERLINE_SINGLE = 1
ATTR_UNDERLINE_DOUBLE = 2
ATTR_UNDERLINE_LOW = 3

class Attribute(Serializable):
    __NAME__ = "IBusAttribute"
    def __init__ (self, type=0, value=0, start_index=0, end_index=0):
        super(Attribute, self).__init__()
        self.__type = type
        self.__value = value
        self.__start_index = start_index
        self.__end_index = end_index

    def get_type(self):
        return self.__type

    def get_value(self):
        return self.__value

    def get_start_index(self):
        return self.__start_index

    def get_end_index(self):
        return self.__end_index

    type        = property(get_type)
    value       = property(get_value)
    start_index = property(get_start_index)
    end_index   = property(get_end_index)

    def serialize(self, struct):
        super(Attribute, self).serialize(struct)
        struct.append (dbus.UInt32(self.__type))
        struct.append (dbus.UInt32(self.__value))
        struct.append (dbus.UInt32(self.__start_index))
        struct.append (dbus.UInt32(self.__end_index))

    def deserialize(self, struct):
        super(Attribute, self).deserialize(struct)
        if len(struct) < 4:
            raise IBusException ("Can not deserialize IBusAttribute")

        self.__type = struct.pop(0)
        self.__value = struct.pop(0)
        self.__start_index = struct.pop(0)
        self.__end_index = struct.pop(0)

serializable_register(Attribute)

class AttributeUnderline (Attribute):
    def __init__(self, value, start_index, end_index):
        Attribute.__init__ (self, ATTR_TYPE_UNDERLINE, value, start_index, end_index)

class AttributeForeground (Attribute):
    def __init__(self, value, start_index, end_index):
        Attribute.__init__ (self, ATTR_TYPE_FOREGROUND, value, start_index, end_index)

class AttributeBackground (Attribute):
    def __init__(self, value, start_index, end_index):
        Attribute.__init__ (self, ATTR_TYPE_BACKGROUND, value, start_index, end_index)

def ARGB (a, r, g, b):
    return ((a & 0xff)<<24) + ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff)

def RGB (r, g, b):
    return ARGB (255, r, g, b)

class AttrList(Serializable):
    __NAME__ = "IBusAttrList"
    def __init__ (self, attrs = []):
        super(AttrList, self).__init__()
        self._attrs = []
        for attr in attrs:
            self.append (attr)

    def append (self, attr):
        assert isinstance (attr, Attribute)
        self._attrs.append (attr)

    def serialize (self, struct):
        super(AttrList, self).serialize (struct)
        array = map (lambda a: serialize_object(a), self._attrs)
        array = dbus.Array (array, signature = "v")
        struct.append(array)

    def deserialize (self, struct):
        super(AttrList, self).deserialize(struct)
        attrs = map(lambda v: deserialize_object(v), struct.pop(0))
        self._attrs = attrs

    def __iter__ (self):
        return self._attrs.__iter__ ()

serializable_register(AttrList)

def test():
    attr_list = AttrList()
    attr_list.append (Attribute())
    attr_list.append (Attribute())
    attr_list.append (Attribute())
    attr_list.append (Attribute())
    attr_list.append (Attribute())
    value = serialize_object(attr_list)
    attr_list = deserialize_object(value)

if __name__ == "__main__":
    test()