summaryrefslogtreecommitdiffstats
path: root/ibus/attribute.py
blob: 31f32404ba0ab73d96ed75b8ed391d495effc74a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# vim:set et sts=4 sw=4:
#
# ibus - The Input Bus
#
# Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA  02111-1307  USA

__all__ = (
        "ATTR_TYPE_UNDERLINE",
        "ATTR_TYPE_FOREGROUND",
        "ATTR_TYPE_BACKGROUND",
        "ATTR_UNDERLINE_NONE",
        "ATTR_UNDERLINE_SINGLE",
        "ATTR_UNDERLINE_DOUBLE",
        "ATTR_UNDERLINE_LOW",
        "Attribute",
        "AttributeUnderline",
        "AttributeForeground",
        "AttributeBackground",
        "AttrList",
        "attribute_from_dbus_value",
        "attr_list_from_dbus_value",
        "ARGB", "RGB"
    )

import dbus

ATTR_TYPE_UNDERLINE = 1
ATTR_TYPE_FOREGROUND = 2
ATTR_TYPE_BACKGROUND = 3

ATTR_UNDERLINE_NONE = 0
ATTR_UNDERLINE_SINGLE = 1
ATTR_UNDERLINE_DOUBLE = 2
ATTR_UNDERLINE_LOW = 3

class Attribute:
    def __init__ (self, type, value, start_index, end_index):
        self.__type = type
        self.__value = value
        self.__start_index = start_index
        self.__end_index = end_index

    def get_type(self):
        return self.__type

    def get_value(self):
        return self.__value

    def get_start_index(self):
        return self.__start_index

    def get_end_index(self):
        return self.__end_index

    type        = property(get_type)
    value       = property(get_value)
    start_index = property(get_start_index)
    end_index   = property(get_end_index)

    def to_dbus_value (self):
        values = [dbus.UInt32 (self.__type),
                dbus.UInt32 (self.__value),
                dbus.UInt32 (self.__start_index),
                dbus.UInt32 (self.__end_index)]
        return dbus.Array (values, signature="u")

    def from_dbus_value (self, value):
        if not isinstance (value, dbus.Array):
            raise dbus.Exception ("Attribute must be dbus.Array (uuuu)")

        if len (value) != 4 or not all (map (lambda x: isinstance (x, dbus.UInt32), value)):
            raise dbus.Exception ("Attribute must be dbus.Array (uuuu)")

        self.__type = value[0]
        self.__value = value[1]
        self.__start_index = value[2]
        self.__end_index = value[3]

def attribute_from_dbus_value (value):
    attribute = Attribute (0, 0, 0, 0)
    attribute.from_dbus_value (value)
    return attribute
        
class AttributeUnderline (Attribute):
    def __init__(self, value, start_index, end_index):
        Attribute.__init__ (self, ATTR_TYPE_UNDERLINE, value, start_index, end_index)

class AttributeForeground (Attribute):
    def __init__(self, value, start_index, end_index):
        Attribute.__init__ (self, ATTR_TYPE_FOREGROUND, value, start_index, end_index)

class AttributeBackground (Attribute):
    def __init__(self, value, start_index, end_index):
        Attribute.__init__ (self, ATTR_TYPE_BACKGROUND, value, start_index, end_index)

def ARGB (a, r, g, b):
    return ((a & 0xff)<<24) + ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff)

def RGB (r, g, b):
    return ARGB (255, r, g, b)

class AttrList:
    def __init__ (self, attrs = []):
        self._attrs = []
        for attr in attrs:
            self.append (attr)

    def append (self, attr):
        assert isinstance (attr, Attribute)
        self._attrs.append (attr)

    def to_dbus_value (self):
        array = dbus.Array (signature = "v")
        for attr in self._attrs:
            array.append (attr.to_dbus_value ())
        return array

    def from_dbus_value (self, value):
        attrs = []
        if not isinstance (value, dbus.Array):
            raise IBusException ("AttrList must from dbus.Array (uuuu)")

        for v in value:
            attr = attribute_from_dbus_value (v)
            attrs.append (attr)

        self._attrs = attrs

    def __iter__ (self):
        return self._attrs.__iter__ ()

def attr_list_from_dbus_value (value):
    if len(value) == 0:
        return None
    attrs = AttrList ()
    attrs.from_dbus_value (value)
    return attrs