]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/sizers/sizer.py
Some cleanup
[wxWidgets.git] / utils / wxPython / lib / sizers / sizer.py
1 #----------------------------------------------------------------------
2 # Name: wxPython.lib.sizers.sizer
3 # Purpose: General purpose sizer/layout managers for wxPython
4 #
5 # Author: Robin Dunn and Dirk Holtwick
6 #
7 # Created: 17-May-1999
8 # RCS-ID: $Id$
9 # Copyright: (c) 1998 by Total Control Software
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------
12
13 from wxPython.wx import wxPoint, wxSize
14
15 #----------------------------------------------------------------------
16
17 class wxSizer:
18 """
19 wxSizer
20
21 An abstract base sizer class. A sizer is able to manage the size and
22 layout of windows and/or child sizers.
23
24 Derived classes should implement CalcMin, and RecalcSizes.
25
26 A window or sizer is added to this sizer with the Add method:
27
28 def Add(self, widget, opt=0)
29
30 The meaning of the opt parameter is different for each type of
31 sizer. It may be a single value or a collection of values.
32 """
33 def __init__(self, size = None):
34 self.children = []
35 self.origin = wxPoint(0, 0)
36 if not size:
37 size = wxSize(0,0)
38 self.size = size
39
40 def Add(self, widget, opt=0):
41 """
42 Add a window or a sizer to this sizer. The meaning of the opt
43 parameter is different for each type of sizer. It may be a single
44 value or a collection of values.
45 """
46 size = widget.GetSize()
47 isSizer = isinstance(widget, wxSizer)
48 self.children.append( (isSizer, widget, size.width, size.height, opt) )
49
50
51 def AddMany(self, widgets):
52 """
53 Add a sequence (list, tuple, etc.) of widgets to this sizer. The
54 items in the sequence should be tuples containing valid args for
55 the Add method.
56 """
57 for childinfo in widgets:
58 if type(childinfo) != type(()):
59 childinfo = (childinfo, )
60 apply(self.Add, childinfo)
61
62
63 def SetDimensions(self, x, y, width, height):
64 self.origin = wxPoint(x, y)
65 self.size = wxSize(width, height)
66 self.RecalcSizes()
67
68 def GetSize(self):
69 return self.size
70
71 def GetPosition(self):
72 return self.origin
73
74 def CalcMin(self):
75 raise NotImplementedError("Derived class should implement CalcMin")
76
77 def RecalcSizes(self):
78 raise NotImplementedError("Derived class should implement RecalcSizes")
79
80
81
82 def __getMinWindowSize(self, win):
83 """
84 Calculate the best size window to hold this sizer, taking into
85 account the difference between client size and window size.
86 """
87 min = self.GetMinSize()
88 a1,a2 = win.GetSizeTuple()
89 b1,b2 = win.GetClientSizeTuple()
90 w = min.width + (a1 - b1)
91 h = min.height + (a2 - b2)
92 return (w, h)
93
94
95 def GetMinSize(self):
96 minWidth, minHeight = self.CalcMin()
97 return wxSize(minWidth, minHeight)
98
99 def SetWindowSizeHints(self, win):
100 w, h = self.__getMinWindowSize(win)
101 win.SetSizeHints(w,h)
102
103 def FitWindow(self, win):
104 w, h = self.__getMinWindowSize(win)
105 win.SetSize(wxSize(w,h))
106
107 def Layout(self, size):
108 self.CalcMin()
109 self.SetDimensions(self.origin.x, self.origin.y,
110 size.width, size.height)
111
112 #----------------------------------------------------------------------