]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/sizers/shape.py
fixed bug that caused wxHtmlWindow to segfault on html tables with cell(s) with rowsp...
[wxWidgets.git] / utils / wxPython / lib / sizers / shape.py
1 #----------------------------------------------------------------------
2 # Name: wxPython.lib.sizers.shape
3 # Purpose: A Sizer that preserves the shape (proportions)
4 # of the managed window
5 #
6 # Created: 7-October-1999
7 # RCS-ID: $Id$
8 # Copyright: SIA "ANK"
9 # Licence: wxWindows license
10 #----------------------------------------------------------------------
11
12 from sizer import wxSizer
13
14 wxANCHOR_NONE = 0
15 wxANCHOR_NORTH = 1
16 wxANCHOR_SOUTH = 2
17 wxANCHOR_EAST = 4
18 wxANCHOR_WEST = 8
19 wxANCHOR_NW = wxANCHOR_NORTH | wxANCHOR_WEST
20 wxANCHOR_NE = wxANCHOR_NORTH | wxANCHOR_EAST
21 wxANCHOR_SW = wxANCHOR_SOUTH | wxANCHOR_WEST
22 wxANCHOR_SE = wxANCHOR_SOUTH | wxANCHOR_EAST
23
24 #----------------------------------------------------------------------
25
26 class wxShapeSizer(wxSizer):
27 """
28 wxShapeSizer
29
30 This sizer preserves the proportional dimensions of the managed
31 window, leaving empty space either in horizontal or vertical
32 dimension.
33
34 By default, the managed window is centered within allowed size.
35 You may specify an anchor parameter to leave all of the extra
36 space on one side: wxANCHOR_NORTH and wxANCHOR_SOUTH manage
37 vertical dimension, leaving extra space on the bottom or top side,
38 respectively; wxANCHOR_EAST and wxANCHOR_WEST do the same in
39 horizontal dimension. wxANCHOR_NW, wxANCHOR_NE, wxANCHOR_SW
40 and wxANCHOR_SE are short-cut names for combinations north+west,
41 north+east, south+west, south+east.
42
43 If both anchors are specified in either direction, south and east
44 take precedence over north and west, respectively. (Because of
45 gravity, widgets tend to fall down.)
46 """
47 def __init__(self, anchor =wxANCHOR_NONE):
48 wxSizer.__init__(self)
49 self.anchor =anchor
50
51 def Add(self, widget):
52 if self.children:
53 raise ValueError("wxShapeSizer can only contain one child.")
54
55 wxSizer.Add(self, widget)
56
57 def CalcMin(self):
58 isSizer, widget, width, height, borderSize = self.children[0]
59
60 if isSizer:
61 width, height = widget.CalcMin()
62
63 return width, height
64
65 def RecalcSizes(self):
66 isSizer, widget, width, height, borderSize = self.children[0]
67 width =self.size.width
68 height =self.size.height
69 px =self.origin.x
70 py =self.origin.y
71 anchor =self.anchor
72 # get current dimensions of the managed window
73 w, h =self.CalcMin()
74 ratio =float(w) /h
75 # in what direction space should be added:
76 # -1: horisontal
77 # 1: vertical
78 # 0: shape is ok
79 dir =cmp(ratio /width *height, 1)
80 if dir <0:
81 # recalculate width
82 old_width =width
83 width =height *ratio
84 if anchor & wxANCHOR_EAST:
85 px =px +old_width -width
86 elif not (anchor & wxANCHOR_WEST):
87 px =px +(old_width -width) /2
88 elif dir >0:
89 # recalculate height
90 old_height =height
91 height =width /ratio
92 if anchor & wxANCHOR_SOUTH:
93 py =py +old_height -height
94 elif not (anchor & wxANCHOR_NORTH):
95 py =py +(old_height -height) /2
96
97 widget.SetDimensions(px, py, width, height)