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