~glitter-team/glitter/trunk

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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# !/usr/bin/python
# -*- coding: utf-8 -*-

# Glitter Toolkit

__authors__ = ["Jan Jokela <janjokela@gmail.com>"]
__licenses__ = ["LICENSE.LGPL"]
__description__ = "Horizontal and vertical box widgets" 

import gobject
import pango
import clutter

from container import Container

class Box(Container):
    """ 
    A box is a special type of container for displaying multiple children in 
    an horizontal or vertical layout.
    
    The 'Box' base widgets offers a common interface to all box widgets
    
    """
    
    def __init__(self):
        """ Initialize box """
        
        super(Box, self).__init__()
        
        self._spacing = 0.0
        self._alignment = 1
        self.packed_children = []
        
        self._update_style(self.style)
        
    def _update_style(self, props=None):
        """ Updates style """
        
        super(Box, self)._update_style(props)
        
        for key, value in props:
            if key == 'spacing':
                self.spacing = value
            elif key == 'alignment':
                self.alignment = value
                
    def _update_layout(self):
        """ Update layout """
        
        super(Box, self)._update_layout() 
        
        self.set_clip(0, 0, self.get_width(), self.get_height())         
                   
    def pack(self, child, order='after'):
        """ Packs a child into the box after or before current items 
        
        child -- (glitter.Widget) A Glitter widget
        order -- (str) 'after' or 'before' currently packed items
        """
        
        if order == 'after': 
            self.packed_children.append(child)
        elif order == 'before':
            self.packed_children.insert(0, child)
        self.add(child)
        self._update_layout()
        
    def unpack(self, child):
        """ Unpacks a child from a box 
        
        child -- (glitter.Widget) A Glitter widget
        """
        
        self.packed_children.remove(child)
        self.remove(child)
        self._update_layout()        
        
    def get_packed_children(self):
        """ Retrieve packed children """
        
        return self.packed_children
                            
    def get_spacing(self):
        """ Retrieve child spacing """
        
        return self._spacing
        
    def set_spacing(self, value):
        """ Sets child spacing
        
        value -- (float) Spacing between children
        """
        
        self._spacing = value
                
    spacing = property(get_spacing, set_spacing)
        
    def get_alignment(self):
        """ Retrieve alignment """
        
        return self._alignment
        
    def set_alignment(self, value):
        """ Sets children alignment 
        
        value -- (str) [ALINGMENT.START, ALIGNMENT.END, ALIGNMENT.CENTER]
        """
        
        self._alignment = value
        
    alignment = property(get_alignment, set_alignment)
        
           
class HBox(Box):
    """ A HBox layouts children in an horizontal fashion """
    
    def __init__(self):
        """ Initialize horizontal box """
        
        super(HBox, self).__init__()
        
        self._update_style(self.style)
        
    def _update_style(self, props=None):
        """ Updates style """
       
        super(HBox, self)._update_style(props)
        
    def _update_layout(self):
        """ Updates layout """
        
        super(HBox, self)._update_layout()
        
        fixed_width_children = []
        # For fixed width children and layouting
        if len(self.packed_children) > 0:
            reserved_width = self.spacing * (len(self.packed_children) - 1) * \
              self.get_widthu()
        else:
            reserved_width = 0.0
        
        # Discover fixed width children and update the reserved height
        for child in self.packed_children:         
            if child.size_ratio > 0 or \
              (child.minimum_width > 0 and child.natural_width == 0):
                fixed_width_children.append(child)
                child._update_layout()
                # Increment reserved width by child height and horizontal 
                # offset
                reserved_width += child.get_widthu() + \
                  child.h_offset * child.get_widthu() * 2.0
        
        # Total width excluding reserved width        
        free_width = self.get_widthu() - reserved_width
        # Width for variable width children
        var_width_child_count = len(self.packed_children) - \
          len(fixed_width_children)
        var_child_width = free_width        
        if var_width_child_count > 0:
            var_child_width /= var_width_child_count        
        
        # Position all children
        current_x = 0.0
        for child in self.packed_children:
            current_x += child.get_widthu() * child.h_offset
            if self.get_widthu() > 0:
                child.natural_x = current_x / self.get_widthu()
                if child not in fixed_width_children:
                    child.natural_width = var_child_width / self.get_widthu()
            child._update_layout()
            current_x += child.get_widthu()
            current_x += child.get_widthu() * child.h_offset
            current_x += self.get_widthu() * self.spacing
          
class VBox(Box):
    """ A VBox layouts children in a vertical fashion """
    
    def __init__(self):
        """ Initialize vertical box """
        
        super(VBox, self).__init__()
        
        self._update_style(self.style)
        
    def _update_style(self, props=None):
        """ Updates style """
        
        super(VBox, self)._update_style(props)
        
    def _update_layout(self):
        """ Updates layout 
        
        Fixed height children are flagged and will be offered the space they 
        need. Other children will share the remaining space.
        
        Children with minimum height and width->height size ratios qualify as 
        fixed height.
        """
        
        super(VBox, self)._update_layout()
        
        fixed_height_children = []
        # For fixed height children and layouting
        reserved_height = self.spacing * (len(self.packed_children) - 1) * \
          self.get_heightu()
        
        # Discover fixed height children and update the reserved height
        for child in self.packed_children:            
            if child.size_ratio < 0 or \
              (child.minimum_height > 0 and child.natural_height == 0):
                fixed_height_children.append(child)
                child._update_layout()
                # Increment reserved height by child height and vertical 
                # offset
                reserved_height += child.get_heightu() + \
                  child.v_offset * child.get_heightu() * 2.0
        
        # Total height excluding reserved height        
        free_height = self.get_heightu() - reserved_height
        # Height for variable height children
        var_height_child_count = len(self.packed_children) - \
          len(fixed_height_children)
        var_child_height = free_height        
        if var_height_child_count > 0:
            var_child_height /= var_height_child_count        
        
        # Position all children
        current_y = 0.0
        for child in self.packed_children:
            current_y += child.get_heightu() * child.v_offset
            child.natural_y = current_y
            if child not in fixed_height_children:
                child.natural_height = var_child_height
            child._update_layout()
            current_y += child.get_heightu() * child.v_offset
            current_y += self.get_heightu() * self.spacing
        
        """fixed_height_children = []
        
        reserved_height = 0.0 # For fixed height children
        for child in self.packed_children:
            if child.size_ratio < 0 or \
               (child.minimum_height > 0 and child.natural_height == 0):
                fixed_height_children.append(child)
                child._update_layout()
                reserved_height += child.get_heightu() + (child.v_offset * self.get_heightu() * 2.0)
            
        if self.get_heightu() > 0: # Account for zero width
            reserved_height = reserved_height * 1.0 / self.get_heightu()
        available_height = 1.0 - reserved_height
        current_y = 0.0
        max_width = self.minimum_width
        for child in self.packed_children:
            child.set_natural_y(current_y)
            if child not in fixed_height_children:
                child_height = available_height / (len(self.packed_children) - \
                  len(fixed_height_children))
                child.set_natural_height(child_height)
            child._update_layout()
            if child.get_width() + (child.h_offset * self.get_width() * 2) > max_width:
                max_width = child.get_width() + (child.h_offset * self.get_width() * 2)
            if self.get_heightu() > 0:
                current_y += child.get_heightu() * 1.0 / self.get_heightu()
            current_y += self.spacing 

        self.minimum_width = max_width     
        
        super(VBox, self)._update_layout()"""
            
    def get_item(self, i):
        """ Retrieve natural y for packed item i """
        
        if len(self.packed_children) > i:
            return self.packed_children[i]
        else:
            return None

    def get_item_natural_height(self, i):
        """ Retrieve natural height for packed item i """
        
        if len(self.packed_children) > i:
            return self.packed_children[i].natural_height
        else:
            return -1