Add header for sources.
[ibus.git] / ibus / attribute.py
blob2b8ba0c774369a7cc4b2fb79a67e6cb96c9695ee
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 import dbus
24 ATTR_TYPE_UNDERLINE = 1
25 ATTR_TYPE_FOREGROUND = 2
26 ATTR_TYPE_BACKGROUND = 3
28 class Attribute:
29 def __init__ (self, type, value, start_index, end_index):
30 self._type = type
31 self._value = value
32 self._start_index = start_index
33 self._end_index = end_index
35 def to_dbus_value (self):
36 values = [dbus.UInt32 (self._type),
37 dbus.UInt32 (self._value),
38 dbus.UInt32 (self._start_index),
39 dbus.UInt32 (self._end_index)]
40 return dbus.Array (values, signature="u")
42 def from_dbus_value (self, value):
43 if not isinstance (value, dbus.Array):
44 raise dbus.Exception ("Attribute must be dbus.Array (uuuu)")
46 if len (value) != 4 or not all (map (lambda x: isinstance (x, dbus.UInt32), value)):
47 raise dbus.Exception ("Attribute must be dbus.Array (uuuu)")
49 self._type = value[0]
50 self._value = value[1]
51 self._start_index = value[2]
52 self._end_index = value[3]
54 def attribute_from_dbus_value (value):
55 attribute = Attribute (0, 0, 0, 0)
56 attribute.from_dbus_value (value)
57 return attribute
59 class AttributeUnderline (Attribute):
60 def __init__(self, value, start_index, end_index):
61 Attribute.__init__ (self, ATTR_TYPE_UNDERLINE, value, start_index, end_index)
63 class AttributeForeground (Attribute):
64 def __init__(self, value, start_index, end_index):
65 Attribute.__init__ (self, ATTR_TYPE_FOREGROUND, value, start_index, end_index)
67 class AttributeBackground (Attribute):
68 def __init__(self, value, start_index, end_index):
69 Attribute.__init__ (self, ATTR_TYPE_BACKGROUND, value, start_index, end_index)
71 def ARGB (a, r, g, b):
72 return ((a & 0xff)<<24) + ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff)
74 def RGB (r, g, b):
75 return ARGB (255, r, g, b)
77 class AttrList:
78 def __init__ (self, attrs = []):
79 self._attrs = []
80 for attr in attrs:
81 self.append (attr)
83 def append (self, attr):
84 assert isinstance (attr, Attribute)
85 self._attrs.append (attr)
87 def to_dbus_value (self):
88 array = dbus.Array (signature = "v")
89 for attr in self._attrs:
90 array.append (attr.to_dbus_value ())
91 return array
93 def from_dbus_value (self, value):
94 attrs = []
95 if not isinstance (value, dbus.Array):
96 raise IBusException ("AttrList must from dbus.Array (uuuu)")
98 for v in value:
99 attr = attribute_from_dbus_value (v)
100 attrs.append (attr)
102 self._attrs = attrs
104 def __iter__ (self):
105 return self._attrs.__iter__ ()
107 def attr_list_from_dbus_value (value):
108 attrs = AttrList ()
109 attrs.from_dbus_value (value)
110 return attrs