Add rpm build scripts.
[ibus.git] / ibus / attribute.py
blobfad9abe2f38eaebb2464db805db76fda4b57c34b
1 # vim:set noet ts=4:
3 # ibus - The Input Bus
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
22 __all__ = (
23 "ATTR_TYPE_UNDERLINE",
24 "ATTR_TYPE_FOREGROUND",
25 "ATTR_TYPE_BACKGROUND",
26 "Attribute",
27 "AttributeUnderline",
28 "AttributeForeground",
29 "AttributeBackground",
30 "AttrList",
31 "attribute_from_dbus_value",
32 "attr_list_from_dbus_value",
33 "ARGB", "RGB"
36 import dbus
38 ATTR_TYPE_UNDERLINE = 1
39 ATTR_TYPE_FOREGROUND = 2
40 ATTR_TYPE_BACKGROUND = 3
42 class Attribute:
43 def __init__ (self, type, value, start_index, end_index):
44 self._type = type
45 self._value = value
46 self._start_index = start_index
47 self._end_index = end_index
49 def to_dbus_value (self):
50 values = [dbus.UInt32 (self._type),
51 dbus.UInt32 (self._value),
52 dbus.UInt32 (self._start_index),
53 dbus.UInt32 (self._end_index)]
54 return dbus.Array (values, signature="u")
56 def from_dbus_value (self, value):
57 if not isinstance (value, dbus.Array):
58 raise dbus.Exception ("Attribute must be dbus.Array (uuuu)")
60 if len (value) != 4 or not all (map (lambda x: isinstance (x, dbus.UInt32), value)):
61 raise dbus.Exception ("Attribute must be dbus.Array (uuuu)")
63 self._type = value[0]
64 self._value = value[1]
65 self._start_index = value[2]
66 self._end_index = value[3]
68 def attribute_from_dbus_value (value):
69 attribute = Attribute (0, 0, 0, 0)
70 attribute.from_dbus_value (value)
71 return attribute
73 class AttributeUnderline (Attribute):
74 def __init__(self, value, start_index, end_index):
75 Attribute.__init__ (self, ATTR_TYPE_UNDERLINE, value, start_index, end_index)
77 class AttributeForeground (Attribute):
78 def __init__(self, value, start_index, end_index):
79 Attribute.__init__ (self, ATTR_TYPE_FOREGROUND, value, start_index, end_index)
81 class AttributeBackground (Attribute):
82 def __init__(self, value, start_index, end_index):
83 Attribute.__init__ (self, ATTR_TYPE_BACKGROUND, value, start_index, end_index)
85 def ARGB (a, r, g, b):
86 return ((a & 0xff)<<24) + ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff)
88 def RGB (r, g, b):
89 return ARGB (255, r, g, b)
91 class AttrList:
92 def __init__ (self, attrs = []):
93 self._attrs = []
94 for attr in attrs:
95 self.append (attr)
97 def append (self, attr):
98 assert isinstance (attr, Attribute)
99 self._attrs.append (attr)
101 def to_dbus_value (self):
102 array = dbus.Array (signature = "v")
103 for attr in self._attrs:
104 array.append (attr.to_dbus_value ())
105 return array
107 def from_dbus_value (self, value):
108 attrs = []
109 if not isinstance (value, dbus.Array):
110 raise IBusException ("AttrList must from dbus.Array (uuuu)")
112 for v in value:
113 attr = attribute_from_dbus_value (v)
114 attrs.append (attr)
116 self._attrs = attrs
118 def __iter__ (self):
119 return self._attrs.__iter__ ()
121 def attr_list_from_dbus_value (value):
122 attrs = AttrList ()
123 attrs.from_dbus_value (value)
124 return attrs