~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
# !/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 = []
        
        reserved_width = 0 # For fixed width children
        for child in self.packed_children:
            if child.size_ratio > 0:
                fixed_width_children.append(child)
                child._update_layout()
                reserved_width += child.get_widthu()
   
        if self.get_widthu() > 0: # Account for zero width
            reserved_width = reserved_width * 1.0 / self.get_widthu()
        available_width = 1.0 - reserved_width
        current_x = 0.0
        for child in self.packed_children:
            child.set_natural_x(current_x)
            if child not in fixed_width_children:
                child_width = available_width / (len(self.packed_children) - \
                  len(fixed_width_children))
                child.set_natural_width(child_width)
            child._update_layout()
            if self.get_widthu() > 0:
                current_x += child.get_widthu() * 1.0 / self.get_widthu()
            current_x += 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 """
        
        super(VBox, self)._update_layout()
        
        fixed_height_children = []
        
        reserved_height = 0 # For fixed width children
        for child in self.packed_children:
            if child.size_ratio < 0:
                fixed_height_children.append(child)
                child._update_layout()
                reserved_height += child.get_heightu()
   
        if self.get_heightu() > 0:
            reserved_height = (reserved_height * 1.0 / self.get_heightu())
        available_height = 1.0 - reserved_height
        current_y = 0.0
        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 self.get_heightu() > 0:
                current_y += child.get_heightu() * 1.0 / self.get_heightu()
            current_y += self.spacing      
            
    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