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
24 ATTR_TYPE_UNDERLINE
= 1
25 ATTR_TYPE_FOREGROUND
= 2
26 ATTR_TYPE_BACKGROUND
= 3
29 def __init__ (self
, type, value
, start_index
, end_index
):
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)")
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
)
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)
75 return ARGB (255, r
, g
, b
)
78 def __init__ (self
, attrs
= []):
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 ())
93 def from_dbus_value (self
, value
):
95 if not isinstance (value
, dbus
.Array
):
96 raise IBusException ("AttrList must from dbus.Array (uuuu)")
99 attr
= attribute_from_dbus_value (v
)
105 return self
._attrs
.__iter
__ ()
107 def attr_list_from_dbus_value (value
):
109 attrs
.from_dbus_value (value
)