| 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 | #---------------------------------------------------------------------- |
| 13 | # 11/30/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
| 14 | # |
| 15 | # o Updated for wx namespace |
| 16 | # o Tested with updated demo |
| 17 | # |
| 18 | |
| 19 | import wx |
| 20 | |
| 21 | class LayoutAnchors(wx.LayoutConstraints): |
| 22 | """ |
| 23 | A class that implements Delphi's Anchors with wx.LayoutConstraints. |
| 24 | |
| 25 | Anchored sides maintain the distance from the edge of the control |
| 26 | to the same edge of the parent. When neither side is selected, |
| 27 | the control keeps the same relative position to both sides. |
| 28 | |
| 29 | The current position and size of the control and it's parent is |
| 30 | used when setting up the constraints. To change the size or |
| 31 | position of an already anchored control, set the constraints to |
| 32 | None, reposition or resize and reapply the anchors. |
| 33 | |
| 34 | Examples:: |
| 35 | |
| 36 | Let's anchor the right and bottom edge of a control and |
| 37 | resize it's parent. |
| 38 | |
| 39 | ctrl.SetConstraints(LayoutAnchors(ctrl, left=0, top=0, right=1, bottom=1)) |
| 40 | |
| 41 | +=========+ +===================+ |
| 42 | | +-----+ | | | |
| 43 | | | * | -> | | |
| 44 | | +--*--+ | | +-----+ | |
| 45 | +---------+ | | * | |
| 46 | | +--*--+ | |
| 47 | +-------------------+ |
| 48 | * = anchored edge |
| 49 | |
| 50 | When anchored on both sides the control will stretch horizontally. |
| 51 | |
| 52 | ctrl.SetConstraints(LayoutAnchors(ctrl, 1, 0, 1, 1)) |
| 53 | |
| 54 | +=========+ +===================+ |
| 55 | | +-----+ | | | |
| 56 | | * * | -> | | |
| 57 | | +--*--+ | | +---------------+ | |
| 58 | +---------+ | * ctrl * | |
| 59 | | +-------*-------+ | |
| 60 | +-------------------+ |
| 61 | * = anchored edge |
| 62 | |
| 63 | """ |
| 64 | def __init__(self, control, left=1, top=1, right=0, bottom=0): |
| 65 | wx.LayoutConstraints.__init__(self) |
| 66 | parent = control.GetParent() |
| 67 | if not parent: return |
| 68 | |
| 69 | pPos, pSize = parent.GetPosition(), parent.GetClientSize() |
| 70 | cPos, cSize = control.GetPosition(), control.GetSize() |
| 71 | |
| 72 | self.setConstraintSides(self.left, wx.Left, left, |
| 73 | self.right, wx.Right, right, |
| 74 | self.width, wx.Width, self.centreX, |
| 75 | cPos.x, cSize.width, pSize.width, parent) |
| 76 | |
| 77 | self.setConstraintSides(self.top, wx.Top, top, |
| 78 | self.bottom, wx.Bottom, bottom, |
| 79 | self.height, wx.Height, self.centreY, |
| 80 | cPos.y, cSize.height, pSize.height, parent) |
| 81 | |
| 82 | def setConstraintSides(self, side1, side1Edge, side1Anchor, |
| 83 | side2, side2Edge, side2Anchor, |
| 84 | size, sizeEdge, centre, |
| 85 | cPos, cSize, pSize, parent): |
| 86 | if side2Anchor: |
| 87 | side2.SameAs(parent, side2Edge, pSize - (cPos + cSize)) |
| 88 | |
| 89 | if side1Anchor: |
| 90 | side1.SameAs(parent, side1Edge, cPos) |
| 91 | |
| 92 | if not side2Anchor: |
| 93 | size.AsIs() |
| 94 | else: |
| 95 | size.AsIs() |
| 96 | |
| 97 | if not side2Anchor: |
| 98 | centre.PercentOf(parent, sizeEdge, |
| 99 | int(((cPos + cSize / 2.0) / pSize)*100)) |
| 100 | |