Window Size Information

It can sometimes be confusing to keep track of the various size-related attributes of a wx.Window, how they relate to each other, and how they interact with sizers. This document will attempt to clear the fog a little, and give some simple explanations of things.

BestSize: The best size of a widget depends on what kind of widget it is, and usually also on the contents of the widget. For example a wx.ListBox's best size will be calculated based on how many items it has, up to a certain limit, or a wx.Button's best size will be calculated based on its label size, but normally won't be smaller than the platform default button size (unless a style flag overrides that). Get the picture? There is a special method in the C++ code called DoGetBestSize() that a class needs to override if it wants to calculate its own best size based on its content. The default DoGetBestSize() is designed for use in container windows, such as wx.Panel, and works something like this:

  1. if the window has a sizer then it is used to calculate the best size.
  2. if the window has layout constraints then that is used to calculate the best size.
  3. if the window has children then the best size is set to be large enough to show all the children.
  4. if there are no children then the window's min size will be used for the best size.
  5. if there is no min size, then the current size is used for the best size.

MinSize: The min size of a widget is a size that is normally explicitly set by the programmer either with the SetMinSize() method or the SetSizeHints() method. Most controls will also set the min size to the size given in the control's constructor if a non-default value is passed. Top-level windows such as wx.Frame will not allow the user to resize the frame below the min size.

Size: The size of a widget can be explicitly set or fetched with the SetSize() or GetSize() methods. This size value is the size that the widget is currently using on screen and is the way to change the size of something that is not being managed by a sizer.

ClientSize: The client size represents the widget's area inside of any borders belonging to the widget and is the area that can be drawn upon in a EVT_PAINT event. If a widget doesn't have a border then its client size is the same as its size.

Initial size: The initial size of a widget is the size given to the constructor of the widget, if any. As mentioned above most controls will also set this size value as the control's min size. If the size passed to the constructor is the default wx.DefaultSize, or if the size is not fully specified (such as "size=(150,-1)") then most controls will fill in the size using the best size and will set the initial size of the control to the resulting size.

AdjustedBestSize: This value is calculated by taking the max of the widget's best size and min size values and returning the result. This has been removed in 2.7.2.

GetEffectiveMinSize: (formerly GetBestFittingSize) A blending of the widget's min size and best size, giving precedence to the min size. For example, if a widget's min size is set to (150, -1) and the best size is (80, 22) then the best fitting size is (150, 22). If the minsize is (50, 20) then the best fitting size is (50, 20). This method is what is called by the sizers when determining what the requirements of each item in the sizer is, and is used for calculating the overall minimum needs of the sizer.

SetInitialSize(size): (formerly SetBestFittingSize) This is a little different than the typical size setters. Rather than just setting an "initial size" attribute it actually sets the minsize to the value passed in, blends that value with the best size, and then sets the size of the widget to be the result. So you can consider this method to be a "Smart SetSize". This method is what is called by the constructor of most controls to set the minsize and initial size of the control.

window.Fit(): The Fit method sets the size of a window to fit around its children. If it has no children then nothing is done, if it does have children then the size of the window is set to the window's best size.

sizer.Fit(window): This sets the size of the window to be large enough to accommodate the minimum size needed by the sizer, (along with a few other constraints...) If the sizer is the one that is assigned to the window then this should be equivalent to window.Fit().

sizer.Layout(): Recalculates the minimum space needed by each item in the sizer, and then lays out the items within the space currently allotted to the sizer.

window.Layout(): If the window has a sizer then it sets the space given to the sizer to the current size of the window, which results in a call to sizer.Layout(). If the window has layout constraints instead of a sizer then the constraints algorithm is run. The Layout method is what is called by the default EVT_SIZE handler for container windows.

WindowSizeInfo (last edited 2008-03-11 10:50:28 by localhost)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.