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",
28 "AttributeForeground",
29 "AttributeBackground",
31 "attribute_from_dbus_value",
32 "attr_list_from_dbus_value",
38 ATTR_TYPE_UNDERLINE
= 1
39 ATTR_TYPE_FOREGROUND
= 2
40 ATTR_TYPE_BACKGROUND
= 3
43 def __init__ (self
, type, value
, start_index
, end_index
):
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)")
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
)
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)
89 return ARGB (255, r
, g
, b
)
92 def __init__ (self
, attrs
= []):
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 ())
107 def from_dbus_value (self
, value
):
109 if not isinstance (value
, dbus
.Array
):
110 raise IBusException ("AttrList must from dbus.Array (uuuu)")
113 attr
= attribute_from_dbus_value (v
)
119 return self
._attrs
.__iter
__ ()
121 def attr_list_from_dbus_value (value
):
123 attrs
.from_dbus_value (value
)