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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
# !/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 _update_fixed_widgets(self, child):
""" Update list of fixed width/height children """
pass
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_fixed_widgets(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_fixed_widgets(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
self._update_layout()
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
self._update_layout()
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)
self.fixed_width_children = []
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()
if len(self.packed_children) > 0:
# Reserved width for spacing between widgets
reserved_width = self.spacing * (len(self.packed_children) - 1) * \
self.get_widthu()
else:
reserved_width = 0.0
# Update the reserved width given our fixed width children
for child in self.fixed_width_children:
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
self.minimum_width = reserved_width
super(HBox, self)._update_layout()
# 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(self.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:
# Calculate child offset width and add that to the current x pos
if child not in self.fixed_width_children:
offset_width = var_child_width * child.h_offset
else:
offset_width = child.h_offset * child.get_widthu()
current_x += offset_width
# Position children
if self.get_widthu() > 0:
child.natural_x = current_x / self.get_widthu()
# Size variable width children
if child not in self.fixed_width_children:
child.natural_width = (var_child_width - 2 * offset_width) \
/ self.get_widthu()
child._update_layout()
# Add child width, horizontal offset and box spacing to current x
current_x += child.get_widthu()
current_x += child.get_widthu() * child.h_offset
current_x += self.get_widthu() * self.spacing
self.set_clipu(0, 0, self.get_widthu(), self.get_heightu())
def _update_fixed_widgets(self, child):
""" Updates fixed width children list """
super(HBox, self)._update_fixed_widgets(child)
if child in self.fixed_width_children:
self.fixed_width_children.remove(child)
elif child.size_ratio > 0 or child.natural_width == 0:
self.fixed_width_children.append(child)
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)
self.fixed_height_children = []
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()
if len(self.packed_children) > 0:
# Reserved height for spacing between widgets
reserved_height = self.spacing * (len(self.packed_children) - 1) * \
self.get_heightu()
else:
reserved_height = 0.0
# Update the reserved height given our fixed height children
for child in self.fixed_height_children:
child._update_layout()
# Increment reserved height by child height and horizontal
# offset
reserved_height += child.get_heightu() + \
child.v_offset * child.get_heightu() * 2.0
self.minimum_height = reserved_height
super(VBox, self)._update_layout()
# Total height excluding reserved height
free_height = self.get_heightu() - reserved_height
# Height for variable width children
var_height_child_count = len(self.packed_children) - \
len(self.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:
# Calculate child offset height and add that to the current y pos
if child not in self.fixed_height_children:
offset_height = var_child_height * child.v_offset
else:
offset_height = child.v_offset * child.get_heightu()
current_y += offset_height
# Position children
if self.get_heightu() > 0:
child.natural_y = current_y / self.get_heightu()
# Size variable height children
if child not in self.fixed_height_children:
child.natural_height = (var_child_height - 2 * offset_height) \
/ self.get_heightu()
child._update_layout()
# Add child height, vertical offset and box spacing to current y
current_y += child.get_heightu()
current_y += child.get_heightu() * child.v_offset
current_y += self.get_heightu() * 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
def _update_fixed_widgets(self, child):
""" Updates fixed height children list """
super(VBox, self)._update_fixed_widgets(child)
if child in self.fixed_height_children:
self.fixed_height_children.remove(child)
elif child.size_ratio < 0 or child.natural_height == 0:
self.fixed_height_children.append(child)
|