1# Orca
2#
3# Copyright (C) 2014 Igalia, S.L.
4#
5# Author: Joanmarie Diggs <jdiggs@igalia.com>
6#
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.1 of the License, or (at your option) any later version.
11#
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 GNU
15# Lesser General Public License for more details.
16#
17# You should have received a copy of the GNU Lesser General Public
18# License along with this library; if not, write to the
19# Free Software Foundation, Inc., Franklin Street, Fifth Floor,
20# Boston MA  02110-1301 USA.
21
22__id__        = "$Id$"
23__version__   = "$Revision$"
24__date__      = "$Date$"
25__copyright__ = "Copyright (c) 2014 Igalia, S.L."
26__license__   = "LGPL"
27
28import pyatspi
29import re
30
31import orca.script_utilities as script_utilities
32
33class Utilities(script_utilities.Utilities):
34
35    def __init__(self, script):
36        super().__init__(script)
37        self._isTypeahead = {}
38
39    def clearCachedObjects(self):
40        self._isTypeahead = {}
41
42    def isTypeahead(self, obj):
43        if not (obj and obj.getRole() == pyatspi.ROLE_TEXT):
44            return False
45
46        rv = self._isTypeahead.get(hash(obj))
47        if rv is not None:
48            return rv
49
50        parent = obj.parent
51        while parent and self.isLayoutOnly(parent):
52            parent = parent.parent
53
54        rv = parent and parent.getRole() == pyatspi.ROLE_WINDOW
55        self._isTypeahead[hash(obj)] = rv
56        return rv
57
58    def rgbFromString(self, attributeValue):
59        regex = re.compile(r"rgb|[^\w,]", re.IGNORECASE)
60        string = re.sub(regex, "", attributeValue)
61        red, green, blue = string.split(",")
62
63        return int(red) >> 8, int(green) >> 8, int(blue) >> 8
64