1 # vim:set et sts=4 sw=4:
5 # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this program; if not, write to the
19 # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 # Boston, MA 02111-1307 USA
23 "ATTR_TYPE_UNDERLINE",
24 "ATTR_TYPE_FOREGROUND",
25 "ATTR_TYPE_BACKGROUND",
26 "ATTR_UNDERLINE_NONE",
27 "ATTR_UNDERLINE_SINGLE",
28 "ATTR_UNDERLINE_DOUBLE",
32 "AttributeForeground",
33 "AttributeBackground",
39 from exception import IBusException
40 from serializable import *
42 ATTR_TYPE_UNDERLINE = 1
43 ATTR_TYPE_FOREGROUND = 2
44 ATTR_TYPE_BACKGROUND = 3
46 ATTR_UNDERLINE_NONE = 0
47 ATTR_UNDERLINE_SINGLE = 1
48 ATTR_UNDERLINE_DOUBLE = 2
49 ATTR_UNDERLINE_LOW = 3
51 class Attribute(Serializable):
52 __gtype_name__ = "PYIBusAttribute"
53 __NAME__ = "IBusAttribute"
54 def __init__ (self, type=0, value=0, start_index=0, end_index=0):
55 super(Attribute, self).__init__()
58 self.__start_index = start_index
59 self.__end_index = end_index
67 def get_start_index(self):
68 return self.__start_index
70 def get_end_index(self):
71 return self.__end_index
73 type = property(get_type)
74 value = property(get_value)
75 start_index = property(get_start_index)
76 end_index = property(get_end_index)
78 def serialize(self, struct):
79 super(Attribute, self).serialize(struct)
80 struct.append (dbus.UInt32(self.__type))
81 struct.append (dbus.UInt32(self.__value))
82 struct.append (dbus.UInt32(self.__start_index))
83 struct.append (dbus.UInt32(self.__end_index))
85 def deserialize(self, struct):
86 super(Attribute, self).deserialize(struct)
88 raise IBusException ("Can not deserialize IBusAttribute")
90 self.__type = struct.pop(0)
91 self.__value = struct.pop(0)
92 self.__start_index = struct.pop(0)
93 self.__end_index = struct.pop(0)
95 class AttributeUnderline (Attribute):
96 def __init__(self, value, start_index, end_index):
97 Attribute.__init__ (self, ATTR_TYPE_UNDERLINE, value, start_index, end_index)
99 class AttributeForeground (Attribute):
100 def __init__(self, value, start_index, end_index):
101 Attribute.__init__ (self, ATTR_TYPE_FOREGROUND, value, start_index, end_index)
103 class AttributeBackground (Attribute):
104 def __init__(self, value, start_index, end_index):
105 Attribute.__init__ (self, ATTR_TYPE_BACKGROUND, value, start_index, end_index)
107 def ARGB (a, r, g, b):
108 return ((a & 0xff)<<24) + ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff)
111 return ARGB (255, r, g, b)
113 class AttrList(Serializable):
114 __gtype_name__ = "PYIBusAttrList"
115 __NAME__ = "IBusAttrList"
116 def __init__ (self, attrs = []):
117 super(AttrList, self).__init__()
122 def append (self, attr):
123 assert isinstance (attr, Attribute)
124 self._attrs.append (attr)
126 def serialize (self, struct):
127 super(AttrList, self).serialize (struct)
128 array = map (lambda a: serialize_object(a), self._attrs)
129 array = dbus.Array (array, signature = "v")
132 def deserialize (self, struct):
133 super(AttrList, self).deserialize(struct)
134 attrs = map(lambda v: deserialize_object(v), struct.pop(0))
138 return self._attrs.__iter__ ()
141 attr_list = AttrList()
142 attr_list.append (Attribute())
143 attr_list.append (Attribute())
144 attr_list.append (Attribute())
145 attr_list.append (Attribute())
146 attr_list.append (Attribute())
147 value = serialize_object(attr_list)
148 attr_list = deserialize_object(value)
150 if __name__ == "__main__":