Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tutorius/gtkutils.py
blob: c96a73f61e5dffd8e60030d850cea5c8a4f3a74d (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Copyright (C) 2009, Tutorius.org
# Copyright (C) 2009, Vincent Vinet <vince.vinet@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
"""
Utility classes and functions that are gtk related
"""
import gtk

def raddr_lookup(widget):
    name = []
    child = widget
    parent = widget.parent
    while parent:
        name.append(str(parent.get_children().index(child)))
        child = parent
        parent = child.parent

    name.append("0") # root object itself
    name.reverse()
    return ".".join(name)


def find_widget(base, target_fqdn, ignore_errors=True):
    """Find a widget by digging into a parent widget's children tree
    @param base the parent widget
    @param target_fqdn fqdn-style target object name

    @return widget found

    The object should normally be the activity widget, as it is the root
    widget for activities. The target_fqdn is a dot separated list of 
    indexes used in widget.get_children and should start with a 0 which is
    the base widget itself,

    Example Usage:
    find_widget(activity,"0.0.0.1.0.0.2")
    """
    path = target_fqdn.split(".")
    #We select the first object and pop the first zero
    obj = base
    path.pop(0)

    while len(path) > 0:
        try:
            obj = get_children(obj)[int(path.pop(0))]
        except:
            if ignore_errors:
                break
            return None

    return obj

EVENTS = (
    "focus",
    "button-press-event",
    "enter-notify-event",
    "leave-notify-event",
    "key-press-event",
    "text-selected",
    "clicked",
)

IGNORED_WIDGETS = (
    "GtkVBox",
    "GtkHBox",
    "GtkAlignment",
    "GtkNotebook",
    "GtkButton",
    "GtkToolItem",
    "GtkToolbar",
)

def register_signals_numbered(target, handler, prefix="0", max_depth=None, events=None):
    """
        Recursive function to register event handlers on an target
        and it's children. The event handler is called with an extra
        argument which is a two-tuple containing the signal name and
        the FQDN-style name of the target that triggered the event.

        This function registers all of the events listed in 
        EVENTS

        Example arg tuple added:
            ("focus", "1.1.2")
        Side effects:
            -Handlers connected on the various targets

        @param target the target to recurse on
        @param handler the handler function to connect
        @param prefix name prepended to the target name to form a chain
        @param max_depth maximum recursion depth, None for infinity

        @returns list of (object, handler_id)
    """
    ret = []
    evts = events or EVENTS
    #Gtk Containers have a get_children() function
    children = get_children(target)
    for i in range(len(children)):
        child = children[i]
        if max_depth is None or max_depth > 0:
            #Recurse with a prefix on all children
            pre = ".".join( \
                [p for p in (prefix, str(i)) if not p is None]
            )
            if max_depth is None:
                dep = None
            else:
                dep = max_depth - 1
            ret+=register_signals_numbered(child, handler, pre, dep, evts)
    #register events on the target if a widget XXX necessary to check this?
    if isinstance(target, gtk.Widget):
        for sig in evts:
            try:
                ret.append( \
                    (target, target.connect(sig, handler, (sig, prefix) ))\
                )
            except TypeError:
                pass

    return ret

def register_signals(target, handler, prefix=None, max_depth=None, events=None):
    """
        Recursive function to register event handlers on an target
        and it's children. The event handler is called with an extra
        argument which is a two-tuple containing the signal name and
        the FQDN-style name of the target that triggered the event.

        This function registers all of the events listed in 
        EVENTS and omits widgets with a name matching
        IGNORED_WIDGETS from the name hierarchy.

        Example arg tuple added:
            ("focus", "Activity.Toolbox.Bold")
        Side effects:
            -Handlers connected on the various targets

        @param target the target to recurse on
        @param handler the handler function to connect
        @param prefix name prepended to the target name to form a chain
        @param max_depth maximum recursion depth, None for infinity

        @returns list of (object, handler_id)
    """
    ret = []
    evts = events or EVENTS
    #Gtk Containers have a get_children() function
    for child in get_children(target):
        if max_depth is None or max_depth > 0:
            #Recurse with a prefix on all children
            pre = ".".join( \
                [p for p in (prefix, target.get_name()) \
                if not (p is None or p in IGNORED_WIDGETS)] \
            )
            if max_depth is None:
                dep = None
            else:
                dep = max_depth - 1
            ret += register_signals(child, handler, pre, dep, evts)
    name = ".".join( \
        [p for p in (prefix, target.get_name()) \
        if not (p is None or p in IGNORED_WIDGETS)] \
    )
    #register events on the target if a widget XXX necessary to check this?
    if isinstance(target, gtk.Widget):
        for sig in evts:
            try:
                ret.append( \
                    (target, target.connect(sig, handler, (sig, name) )) \
                )
            except TypeError:
                pass

    return ret
    
def get_children(widget):
    """Lists widget's children"""
    #widgets with multiple children
    try:
        return widget.get_children()
    except (AttributeError,TypeError):
        pass

    #widgets with a single child
    try:
        return [widget.get_child(),]
    except (AttributeError,TypeError):
        pass

    #otherwise return empty list
    return []