Modify some interface.
[ibus.git] / ibus / attribute.py
bloba8af1f2df5d34a067c0671da686f948f51430065
1 import dbus
3 ATTR_TYPE_UNDERLINE = 1
4 ATTR_TYPE_FOREGROUND = 2
5 ATTR_TYPE_BACKGROUND = 3
7 class Attribute:
8 def __init__ (self, type, value, start_index, end_index):
9 self._type = type
10 self._value = value
11 self._start_index = start_index
12 self._end_index = end_index
14 def to_dbus_value (self):
15 values = [dbus.UInt32 (self._type),
16 dbus.UInt32 (self._value),
17 dbus.UInt32 (self._start_index),
18 dbus.UInt32 (self._end_index)]
19 return dbus.Array (values, signature="u")
21 def from_dbus_value (self, value):
22 if not isinstance (value, dbus.Array):
23 raise dbus.Exception ("Attribute must be dbus.Array (uuuu)")
25 if len (value) != 4 or not all (map (lambda x: isinstance (x, dbus.UInt32), value)):
26 raise dbus.Exception ("Attribute must be dbus.Array (uuuu)")
28 self._type = value[0]
29 self._value = value[1]
30 self._start_index = value[2]
31 self._end_index = value[3]
33 def attribute_from_dbus_value (value):
34 attribute = Attribute (0, 0, 0, 0)
35 attribute.from_dbus_value (value)
36 return attribute
38 class AttributeUnderline (Attribute):
39 def __init__(self, value, start_index, end_index):
40 Attribute.__init__ (self, ATTR_TYPE_UNDERLINE, value, start_index, end_index)
42 class AttributeForeground (Attribute):
43 def __init__(self, value, start_index, end_index):
44 Attribute.__init__ (self, ATTR_TYPE_FOREGROUND, value, start_index, end_index)
46 class AttributeBackground (Attribute):
47 def __init__(self, value, start_index, end_index):
48 Attribute.__init__ (self, ATTR_TYPE_BACKGROUND, value, start_index, end_index)
50 def ARGB (a, r, g, b):
51 return ((a & 0xff)<<24) + ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff)
53 def RGB (r, g, b):
54 return ARGB (255, r, g, b)
56 class AttrList:
57 def __init__ (self, attrs = []):
58 self._attrs = []
59 for attr in attrs:
60 self.append (attr)
62 def append (self, attr):
63 assert isinstance (attr, Attribute)
64 self._attrs.append (attr)
66 def to_dbus_value (self):
67 array = dbus.Array (signature = "v")
68 for attr in self._attrs:
69 array.append (attr.to_dbus_value ())
70 return array
72 def from_dbus_value (self, value):
73 attrs = []
74 if not isinstance (value, dbus.Array):
75 raise IBusException ("AttrList must from dbus.Array (uuuu)")
77 for v in value:
78 attr = attribute_from_dbus_value (v)
79 attrs.append (attr)
81 self._attrs = attrs
83 def __iter__ (self):
84 return self._attrs.__iter__ ()
86 def attr_list_from_dbus_value (value):
87 attrs = AttrList ()
88 attrs.from_dbus_value (value)
89 return attrs