~gdesklets-desklet-team/gdesklets/0.36

« back to all changes in this revision

Viewing changes to display/ContainerTarget.py

  • Committer: Robert Pastierovic
  • Date: 2007-10-07 10:08:42 UTC
  • Revision ID: pastierovic@gmail.com-20071007100842-fdvp2vzmqgh1j87k
merged 0.3x branch and basic documentation and some other changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from layout import Unit
 
2
from utils import dialog
 
3
from utils.datatypes import *
 
4
from DisplayTarget import DisplayTarget
 
5
 
 
6
import utils
 
7
 
 
8
import gobject
 
9
import gtk
 
10
 
 
11
 
 
12
#
 
13
# Abstract class for targets that can contain others.
 
14
#
 
15
class ContainerTarget(DisplayTarget):
 
16
 
 
17
    __slots__ = ('__children', '__children_id', '__ids', '__relatives')
 
18
 
 
19
    def __init__(self, name, parent):
 
20
 
 
21
        # the list is necessary for preserving order
 
22
        self.__children = []
 
23
 
 
24
        # mapping: id -> child
 
25
        self.__children_id = {}
 
26
 
 
27
        # mapping: child -> id
 
28
        self.__ids = {}
 
29
 
 
30
        # the relative-relations: id_of_relative -> [child]
 
31
        self.__relatives = {}
 
32
 
 
33
        DisplayTarget.__init__(self, name, parent)
 
34
 
 
35
 
 
36
 
 
37
    #
 
38
    # Observer callback for watching children.
 
39
    #
 
40
    def child_observer(self, src, cmd): pass
 
41
 
 
42
 
 
43
 
 
44
    #
 
45
    # Creates a new child of the given type.
 
46
    #
 
47
    def new_child(self, childtype, settings, children):
 
48
 
 
49
        import targetregistry
 
50
        cid = settings["id"]
 
51
 
 
52
        child = targetregistry.create(childtype, self)
 
53
        only_one = targetregistry.one_child(self.get_name())
 
54
 
 
55
        if (only_one and len(self.__children) > 0):
 
56
            log("Warning: The %s container accepts only one child."
 
57
                % self.get_name(),
 
58
                is_warning = True)
 
59
        self._register_child(child, cid)
 
60
        child.get_widget().show()
 
61
 
 
62
        for t, s, c in children:
 
63
            child.new_child(t, s, c)
 
64
 
 
65
        for key, value in settings.items():
 
66
            child.set_xml_prop(key, value)
 
67
 
 
68
        return child
 
69
 
 
70
 
 
71
 
 
72
    def _register_child(self, child, cid):
 
73
 
 
74
        self.__children_id[cid] = child
 
75
        self.__ids[child] = cid
 
76
        self.__children.append(child)
 
77
        self._get_display().add_target_to_script(cid, child)
 
78
 
 
79
 
 
80
 
 
81
    def _unregister_child(self, child):
 
82
 
 
83
        cid = self.__ids[child]
 
84
 
 
85
        try:
 
86
            del self.__children_id[cid]
 
87
        except KeyError:
 
88
            pass
 
89
 
 
90
        try:
 
91
            del self.__ids[child]
 
92
        except KeyError:
 
93
            pass
 
94
 
 
95
        self.__children.remove(child)
 
96
 
 
97
 
 
98
 
 
99
    def _get_children(self): return self.__children[:]
 
100
 
 
101
 
 
102
 
 
103
    def _get_child(self):
 
104
 
 
105
        if (not self.__children): return
 
106
        else: return self.__children[0]
 
107
 
 
108
 
 
109
 
 
110
    def get_child_by_id(self, ident): return self.__children_id.get(ident)
 
111
    def get_id_by_child(self, child): return self.__ids.get(child)
 
112
 
 
113
 
 
114
 
 
115
    def delete(self):
 
116
 
 
117
        for c in self._get_children():
 
118
            c.delete()
 
119
            self._unregister_child(c)
 
120
            del c
 
121
        self.__children = []
 
122
        self.__children_id.clear()
 
123
        self.__ids.clear()
 
124
        DisplayTarget.delete(self)
 
125
 
 
126
 
 
127
 
 
128
    #
 
129
    # Override this method if the container size differs from the target size.
 
130
    #
 
131
    def get_container_geometry(self):
 
132
 
 
133
        return self.get_geometry()
 
134
 
 
135
 
 
136
 
 
137
    #
 
138
    # Returns the border size of this container. Override this method to return
 
139
    # the sizes of the four borders (left, top, right, bottom).
 
140
    #
 
141
    def get_border_size(self):
 
142
 
 
143
        return (Unit.ZERO, Unit.ZERO, Unit.ZERO, Unit.ZERO)
 
144
 
 
145
 
 
146
 
 
147
    #
 
148
    # Containers which set index values for the children override this method.
 
149
    #
 
150
    def get_next_child_index(self): return -1
 
151
 
 
152
 
 
153
 
 
154
    def detect_leave(self, stamp):
 
155
 
 
156
        if (not self._is_active()): return
 
157
        DisplayTarget.detect_leave(self, stamp)
 
158
        for c in self.__children:
 
159
            c.detect_leave(stamp)
 
160
 
 
161
 
 
162
 
 
163
    def unlock_geometry(self):
 
164
 
 
165
        self._geometry_lock = False
 
166
        for c in self._get_children(): c.unlock_geometry()
 
167