1 #---------------------------------------------------------------------- 
   2 # Name:        wxPython.lib.sizers.shape 
   3 # Purpose:     A Sizer that preserves the shape (proportions) 
   4 #              of the managed window 
   6 # Created:     7-October-1999 
   9 # Licence:     wxWindows license 
  10 #---------------------------------------------------------------------- 
  12 from sizer       
import wxSizer
 
  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
 
  24 #---------------------------------------------------------------------- 
  26 class wxShapeSizer(wxSizer
): 
  30     This sizer preserves the proportional dimensions of the managed 
  31     window, leaving empty space either in horizontal or vertical 
  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. 
  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.) 
  47     def __init__(self
, anchor 
=wxANCHOR_NONE
): 
  48         wxSizer
.__init
__(self
) 
  51     def Add(self
, widget
): 
  53             raise ValueError("wxShapeSizer can only contain one child.") 
  55         wxSizer
.Add(self
, widget
) 
  58         isSizer
, widget
, width
, height
, borderSize 
= self
.children
[0] 
  61             width
, height 
= widget
.CalcMin() 
  65     def RecalcSizes(self
): 
  66         isSizer
, widget
, width
, height
, borderSize 
= self
.children
[0] 
  67         width 
=self
.size
.width
 
  68         height 
=self
.size
.height
 
  72         # get current dimensions of the managed window 
  75         # in what direction space should be added: 
  79         dir =cmp(ratio 
/width 
*height
, 1) 
  84             if anchor 
& wxANCHOR_EAST
: 
  85                 px 
=px 
+old_width 
-width
 
  86             elif not (anchor 
& wxANCHOR_WEST
): 
  87                 px 
=px 
+(old_width 
-width
) /2 
  92             if anchor 
& wxANCHOR_SOUTH
: 
  93                 py 
=py 
+old_height 
-height
 
  94             elif not (anchor 
& wxANCHOR_NORTH
): 
  95                 py 
=py 
+(old_height 
-height
) /2 
  97         widget
.SetDimensions(px
, py
, width
, height
)