]>
Commit | Line | Data |
---|---|---|
d14a1e28 RD |
1 | #---------------------------------------------------------------------- |
2 | # Name: wxPython.lib.anchors | |
3 | # Purpose: A class that provides an easy to use interface over layout | |
4 | # constraints for anchored layout. | |
5 | # | |
6 | # Author: Riaan Booysen | |
7 | # | |
8 | # Created: 15-Dec-2000 | |
9 | # RCS-ID: $Id$ | |
10 | # Copyright: (c) 2000 by Total Control Software | |
11 | # Licence: wxWindows license | |
12 | #---------------------------------------------------------------------- | |
1fded56b | 13 | |
d14a1e28 RD |
14 | from wxPython.wx import wxLayoutConstraints, wxTop, wxLeft, wxBottom, wxRight, \ |
15 | wxHeight, wxWidth | |
1fded56b | 16 | |
d14a1e28 RD |
17 | class LayoutAnchors(wxLayoutConstraints): |
18 | """ A class that implements Delphi's Anchors with wxLayoutConstraints. | |
19 | ||
20 | Anchored sides maintain the distance from the edge of the | |
21 | control to the same edge of the parent. | |
22 | When neither side is selected, the control keeps the same | |
23 | relative position to both sides. | |
24 | ||
25 | The current position and size of the control and it's parent | |
26 | is used when setting up the constraints. To change the size or | |
27 | position of an already anchored control, set the constraints to | |
28 | None, reposition or resize and reapply the anchors. | |
29 | ||
30 | Examples: | |
31 | ||
32 | Let's anchor the right and bottom edge of a control and | |
33 | resize it's parent. | |
34 | ||
35 | ctrl.SetConstraints(LayoutAnchors(ctrl, left=0, top=0, right=1, bottom=1)) | |
36 | ||
37 | +=========+ +===================+ | |
38 | | +-----+ | | | | |
39 | | | * | -> | | | |
40 | | +--*--+ | | +-----+ | | |
41 | +---------+ | | * | | |
42 | | +--*--+ | | |
43 | +-------------------+ | |
44 | * = anchored edge | |
45 | ||
46 | When anchored on both sides the control will stretch horizontally. | |
47 | ||
48 | ctrl.SetConstraints(LayoutAnchors(ctrl, 1, 0, 1, 1)) | |
49 | ||
50 | +=========+ +===================+ | |
51 | | +-----+ | | | | |
52 | | * * | -> | | | |
53 | | +--*--+ | | +---------------+ | | |
54 | +---------+ | * ctrl * | | |
55 | | +-------*-------+ | | |
56 | +-------------------+ | |
57 | * = anchored edge | |
58 | """ | |
59 | def __init__(self, control, left = 1, top = 1, right = 0, bottom = 0): | |
60 | wxLayoutConstraints.__init__(self) | |
61 | parent = control.GetParent() | |
62 | if not parent: return | |
63 | ||
64 | pPos, pSize = parent.GetPosition(), parent.GetClientSize() | |
65 | cPos, cSize = control.GetPosition(), control.GetSize() | |
66 | ||
67 | self.setConstraintSides(self.left, wxLeft, left, | |
68 | self.right, wxRight, right, | |
69 | self.width, wxWidth, self.centreX, | |
70 | cPos.x, cSize.x, pSize.x, parent) | |
71 | ||
72 | self.setConstraintSides(self.top, wxTop, top, | |
73 | self.bottom, wxBottom, bottom, | |
74 | self.height, wxHeight, self.centreY, | |
75 | cPos.y, cSize.y, pSize.y, parent) | |
76 | ||
77 | def setConstraintSides(self, side1, side1Edge, side1Anchor, | |
78 | side2, side2Edge, side2Anchor, | |
79 | size, sizeEdge, centre, | |
80 | cPos, cSize, pSize, parent): | |
81 | if side2Anchor: | |
82 | side2.SameAs(parent, side2Edge, pSize - (cPos + cSize)) | |
83 | if side1Anchor: | |
84 | side1.SameAs(parent, side1Edge, cPos) | |
85 | if not side2Anchor: | |
86 | size.AsIs() | |
87 | else: | |
88 | size.AsIs() | |
89 | if not side2Anchor: | |
90 | centre.PercentOf(parent, sizeEdge, | |
91 | int(((cPos + cSize / 2.0) / pSize)*100)) |