1 from .layout import Layout |
|
2 |
|
3 |
|
4 class AnchorLayout(Layout): |
|
5 |
|
6 """Anchor widgets to borders of container.""" |
|
7 |
|
8 def __init__(self): |
|
9 Layout.__init__(self) |
|
10 self.register_hints( |
|
11 'halign', make_select('left', 'right', 'fill', 'center'), |
|
12 'valign', make_select('top', 'bottom', 'fill', 'center'), |
|
13 'margin', Borders, |
|
14 ) |
|
15 |
|
16 def on_resize(self, ev): |
|
17 for child in self._get_children(): |
|
18 reqw = max(child.sizereq.w, child.sizemin.w) |
|
19 reqh = max(child.sizereq.h, child.sizemin.h) |
|
20 ha = child.hint_value('halign') |
|
21 va = child.hint_value('valign') |
|
22 margin = child.hint_value('margin') |
|
23 if ha == 'left': |
|
24 x = margin.l |
|
25 w = reqw |
|
26 if ha == 'right': |
|
27 x = self.width - margin.r - reqw |
|
28 w = reqw |
|
29 if ha == 'fill': |
|
30 x = margin.l |
|
31 w = self.width - margin.l - margin.r |
|
32 if ha == 'center': |
|
33 x = (self.width - reqw) // 2 |
|
34 w = reqw |
|
35 if va == 'top': |
|
36 y = margin.t |
|
37 h = reqh |
|
38 if va == 'bottom': |
|
39 y = self.height - margin.b - reqh |
|
40 h = reqh |
|
41 if va == 'fill': |
|
42 y = margin.t |
|
43 h = self.height - margin.t - margin.b |
|
44 if va == 'center': |
|
45 y = (self.height - reqh) // 2 |
|
46 h = reqh |
|
47 child._pos.update(x=x, y=y) |
|
48 child._size.update(w=w, h=h) |
|
49 child._view_size.update(w=w, h=h) |
|
50 |
|
51 def move_child(self, child, x=None, y=None): |
|
52 """Move child inside container by adjusting its margin. |
|
53 |
|
54 Operation is supported only for one-side anchors: |
|
55 left, right, top, bottom |
|
56 No move on axis where align is set to |
|
57 center, fill |
|
58 |
|
59 """ |
|
60 if not child in self.children: |
|
61 raise ValueError('AnchorLayout.move(): Cannot move foreign child.') |
|
62 margin = child.hint_value('margin') |
|
63 newx = None |
|
64 if x is not None: |
|
65 ha = child.hint_value('halign') |
|
66 ofsx = x - child.x |
|
67 if ha == 'left': |
|
68 margin.l += ofsx |
|
69 newx = margin.l |
|
70 elif ha == 'right': |
|
71 margin.r -= ofsx |
|
72 newx = self.width - margin.r - child.sizereq.w |
|
73 newy = None |
|
74 if y is not None: |
|
75 va = child.hint_value('valign') |
|
76 ofsy = y - child.y |
|
77 if va == 'top': |
|
78 margin.t += ofsy |
|
79 newy = margin.t |
|
80 elif va == 'bottom': |
|
81 margin.b -= ofsy |
|
82 newy = self.height - margin.b - child.sizereq.h |
|
83 child._pos.update(x=newx, y=newy) |
|