Development: wxWiki Documentation Example
From WxWiki
Below is an example of how wxWiki-hosted documentation could be used. This is only an example whose contents are out of date, and should not be used for actual reference.
There are two separate concepts for how this could be implemented:
- Authoring and hosting all of the official documentation on this wiki itself.
- This would significantly limit our options for publishing the documentation to other formats.
- We would be required to keep documentation for older versions of wxWidgets on the pages even for newer versions regardless of whether the given functionality has been deprecated (basically, we have no method of "tagging" versions in the wiki).
- If we want to allow user contributed notes, we would need to also allow edits to the official documentation from anyone (the wxWiki currently allows edits even from unregistered users).
- Pulling the official documentation from SVN sources and attaching them to the wiki pages hosting the corresponding user contributed notes.
- This requires that the source documentation needs to be written with semantic markup of some kind.
Both concepts will result in pages that look very similar to what's shown below (with the exception that the first concept would not easily allow for showing alternate versions of documentation on different classes as illustrated below, the second would make it possible).
[edit] wxSizer
| Official Classes | http://wx.ibaku.net/wiki/wxwiki-blocks-small.png | Archive • Containers • Controls • Data Structures • Database • Date & Time • Debug • Device Contexts • Dialogs • Document & Views • Drag & Drop • Events • Filesystem • Frames • Graphics • Grid Cell • Help • HTML • Logging • Miscellaneous • Networking • Printing • Sizers • Streams • Threading • Windows |
[edit] Official Documentation
| Version: 2.6.3 Other Versions: 2.4.2, 2.6.0, 2.6.1, 2.6.2, CVS HEAD |
wxSizer is the abstract base class used for laying out subwindows in a window. You cannot use wxSizer directly; instead, you will have to use one of the sizer classes derived from it. Currently there are wxBoxSizer, wxStaticBoxSizer, wxGridSizer, wxFlexGridSizer and wxGridBagSizer.
The layout algorithm used by sizers in wxWidgets is closely related to layout in other GUI toolkits, such as Java's AWT, the GTK toolkit or the Qt toolkit. It is based upon the idea of the individual subwindows reporting their minimal required size and their ability to get stretched if the size of the parent window has changed. This will most often mean that the programmer does not set the original size of a dialog in the beginning, rather the dialog will be assigned a sizer and this sizer will be queried about the recommended size. The sizer in turn will query its children, which can be normal windows, empty space or other sizers, so that a hierarchy of sizers can be constructed. Note that wxSizer does not derive from wxWindow and thus does not interfere with tab ordering and requires very little resources compared to a real window on screen.
What makes sizers so well fitted for use in wxWidgets is the fact that every control reports its own minimal size and the algorithm can handle differences in font sizes or different window (dialog item) sizes on different platforms without problems. If e.g. the standard font as well as the overall design of Motif widgets requires more space than on Windows, the initial dialog size will automatically be bigger on Motif than on Windows.
Sizers may also be used to control the layout of custom drawn items on the window. The Add, Insert, and Prepend functions return a pointer to the newly added wxSizerItem. Just add empty space of the desired size and attributes, and then use the wxSizerItem::GetRect method to determine where the drawing operations should take place.
wxPython Note: If you wish to create a sizer class in wxPython you should derive the class from wxPySizer in order to get Python-aware capabilities for the various virtual methods.
Derived From:
Include Files:
- <wx/sizer.h>
See Also:
Members:
- wxSizer::wxSizer
- wxSizer::~wxSizer
- wxSizer::Add
- wxSizer::AddSpacer
- wxSizer::AddStretchSpacer
- wxSizer::CalcMin
- wxSizer::Detach
- wxSizer::Fit
- wxSizer::FitInside
- wxSizer::GetItem
- wxSizer::GetSize
- wxSizer::GetPosition
- wxSizer::GetMinSize
- wxSizer::Insert
- wxSizer::InsertSpacer
- wxSizer::InsertStretchSpacer
- wxSizer::Layout
- wxSizer::Prepend
- wxSizer::PrependSpacer
- wxSizer::PrependStretchSpacer
- wxSizer::RecalcSizes
- wxSizer::Remove
- wxSizer::SetDimension
- wxSizer::SetMinSize
- wxSizer::SetItemMinSize
- wxSizer::SetSizeHints
- wxSizer::SetVirtualSizeHints
- wxSizer::Show
wxSizer::wxSizer
The constructor. Note that wxSizer is an abstract base class and may not be instantiated.
wxSizer::~wxSizer
The destructor.
wxSizer::Add
Appends a child to the sizer. wxSizer itself is an abstract class, but the parameters are equivalent in the derived classes that you will instantiate to use it so they are described here:
| window | |||||||||||
| The window to be added to the sizer. Its initial size (either set explicitly by the user or calculated internally when using wxDefaultSize) is interpreted as the minimal and in many cases also the initial size. This is particularly useful in connection with SetSizeHints. | |||||||||||
| sizer | |||||||||||
| The (child-)sizer to be added to the sizer. This allows placing a child sizer in a sizer and thus to create hierarchies of sizers (typically a vertical box as the top sizer and several horizontal boxes on the level beneath). | |||||||||||
| width and height | |||||||||||
| The dimension of a spacer to be added to the sizer. Adding spacers to sizers gives more flexibility in the design of dialogs; imagine for example a horizontal box with two buttons at the bottom of a dialog: you might want to insert a space between the two buttons and make that space stretchable using the proportion flag and the result will be that the left button will be aligned with the left side of the dialog and the right button with the right side - the space in between will shrink and grow with the dialog. | |||||||||||
| proportion | |||||||||||
| Although the meaning of this parameter is undefined in wxSizer, it is used in wxBoxSizer to indicate if a child of a sizer can change its size in the main orientation of the wxBoxSizer - where 0 stands for not changeable and a value of more than zero is interpreted relative to the value of other children of the same wxBoxSizer. For example, you might have a horizontal wxBoxSizer with three children, two of which are supposed to change their size with the sizer. Then the two stretchable windows would get a value of 1 each to make them grow and shrink equally with the sizer's horizontal dimension. | |||||||||||
| flag | |||||||||||
This parameter can be used to set a number of flags which can be combined using the binary OR operator |. Two main behaviours are defined using these flags. One is the border around a window: the border parameter determines the border width whereas the flags given here determine which side(s) of the item that the border will be added. The other flags determine how the sizer item behaves when the space allotted to the sizer changes, and is somewhat dependent on the specific kind of sizer used.
| |||||||||||
| border | |||||||||||
| Determines the border width, if the flag parameter is set to include any border flag. | |||||||||||
| userData | |||||||||||
| Allows an extra object to be attached to the sizer item, for use in derived classes when sizing information is more complex than the proportion and flag will allow for. | |||||||||||
| flags | |||||||||||
| A wxSizerFlags object that enables you to specify most of the above parameters more conveniently. | |||||||||||
wxSizer::AddSpacer
Adds non-stretchable space to the sizer. More readable way of calling Add(size, size, 0).
wxSizer::AddStretchSpacer
Adds stretchable space to the sizer. More readable way of calling Add(0, 0, prop).
wxSizer::CalcMin
This method is abstract and has to be overwritten by any derived class. Here, the sizer will do the actual calculation of its children minimal sizes.
wxSizer::Detach
Detach a child from the sizer without destroying it. window is the window to be detached, sizer is the equivalent sizer and index is the position of the child in the sizer, typically 0 for the first item. This method does not cause any layout or resizing to take place, call wxSizer::Layout to update the layout "on screen" after detaching a child from the sizer.
Returns true if the child item was found and detached, false otherwise.
See Also:
wxSizer::Fit
Tell the sizer to resize the window to match the sizer's minimal size. This is commonly done in the constructor of the window itself, see sample in the description of wxBoxSizer. Returns the new size.
For a top level window this is the total window size, not client size.
wxSizer::FitInside
Tell the sizer to resize the virtual size of the window to match the sizer's minimal size. This will not alter the on screen size of the window, but may cause the addition/removal/alteration of scrollbars required to view the virtual area in windows which manage it.
See Also:
wxSizer::GetItem
Finds item of the sizer which holds given window, sizer or is located in sizer at position index. Use parameter recursive to search in subsizers too.
Returns pointer to item or NULL.
wxSizer::GetSize
Returns the current size of the sizer.
wxSizer::GetPosition
Returns the current position of the sizer.
wxSizer::GetMinSize
Returns the minimal size of the sizer. This is either the combined minimal size of all the children and their borders or the minimal size set by wxSizer::SetMinSize, depending on which is bigger.
wxSizer::Insert
Insert a child into the sizer before any existing item at index.
| index | |
| The position this child should assume in the sizer. | |
See wxSizer::Add for the meaning of the other parameters.
wxSizer::InsertSpacer
Inserts non-stretchable space to the sizer. More readable way of calling Insert(size, size, 0).
wxSizer::InsertStretchSpacer
Inserts stretchable space to the sizer. More readable way of calling Insert(0, 0, prop).
wxSizer::Layout
Call this to force layout of the children anew, e.g. after having added a child to or removed a child (window, other sizer or space) from the sizer while keeping the current dimension.
wxSizer::Prepend
Same as wxSizer::Add, but prepends the items to the beginning of the list of items (windows, subsizers or spaces) owned by this sizer.
wxSizer::PrependSpacer
Prepends non-stretchable space to the sizer. More readable way of calling Prepend(size, size, 0).
wxSizer::PrependStretchSpacer
Prepends stretchable space to the sizer. More readable way of calling Prepend(0, 0, prop).
wxSizer::RecalcSizes
This method is abstract and has to be overwritten by any derived class. Here, the sizer will do the actual calculation of its children's positions and sizes.
wxSizer::Remove
Removes a child from the sizer and destroys it. sizer is the wxSizer to be removed, index is the position of the child in the sizer, typically 0 for the first item. This method does not cause any layout or resizing to take place, call wxSizer::Layout to update the layout "on screen" after removing a child from the sizer.
NB: The method taking a wxWindow* parameter is deprecated. For historical reasons it does not destroy the window as would usually be expected from Remove. You should use wxSizer::Detach in new code instead. There is currently no wxSizer method that will both detach and destroy a wxWindow item.
Returns true if the child item was found and removed, false otherwise.
wxSizer::SetDimension
Call this to force the sizer to take the given dimension and thus force the items owned by the sizer to resize themselves according to the rules defined by the parameter in the Add and Prepend methods.
wxSizer::SetMinSize
Call this to give the sizer a minimal size. Normally, the sizer will calculate its minimal size based purely on how much space its children need. After calling this method GetMinSize will return either the minimal size as requested by its children or the minimal size set here, depending on which is bigger.
wxSizer::SetItemMinSize
Set an item's minimum size by window, sizer, or position. The item will be found recursively in the sizer's descendants. This function enables an application to set the size of an item after initial creation.
wxSizer::SetSizeHints
Tell the sizer to set (and Fit) the minimal size of the window to match the sizer's minimal size. This is commonly done in the constructor of the window itself, see sample in the description of wxBoxSizer if the window is resizable (as are many dialogs under Unix and frames on probably all platforms).
wxSizer::SetVirtualSizeHints
Tell the sizer to set the minimal size of the window virtual area to match the sizer's minimal size. For windows with managed scrollbars this will set them appropriately.
See Also:
wxSizer::Show
Shows or hides the window, sizer, or item at index. To make a sizer item disappear or reappear, use Show() followed by Layout(). Use parameter recursive to show or hide elements found in subsizers.
Returns true if the child item was found, false otherwise.
Note that this only works with wxBoxSizer and wxFlexGridSizer, since they are the only two sizer classes that can size rows/columns independently.
[edit] User Contributed Notes
See Also:
[edit] Some Member Functions
Some functions that might be a bit confusing at first:
[edit] Layout()
This one really positions the controls, i.e. after its call they have their correct size and positions. Normally called from OnSize() and so doesn't have to be called explicitly -- but only for top-level windows (e.g., wxFrame and wxDialog), and only if you use SetSizer or SetSizerAndFit. Otherwise, you should call Layout() (the wxWindow function, not the sizer function of the same name) within your window's OnSize() handler.
[edit] SetSizeHints()
Says that the top level window shouldn't shrink beyond its minimal size. Since recently, also calls Fit() to make the window of the right size initially, so now you only have to call SetSizeHints() and not Fit().
[edit] SetAutoLayout(true)
Tells OnSize() to call Layout() automatically. In 2.4.0 this is done when you do SetSizer() so you don't have to call it anymore.
[edit] RecalcSizes()
Shouldn't be called directly, probably
[edit] Nesting panels/sizers
It's generally considered a bad idea to have panels within panels while using sizers - just using sizers within sizers usually does the job and is much more flexible.
[edit] Spacing
If you want to add some manual spacing between two children of a sizer, you might want to use the Sizer::Add member function that allows you to specify a rectangular size.
[edit] Gotchas
[edit] Small Window Sizes
If you have a sizer for your frame that you want to add a panel to, make sure that you add it after you add everything to the panel and call panel->SetSizerAndFit( panelsizer ). If you first add the panel, then add controls to it, the window will not size correctly.
Or you can call panel->GetSizer()->Fit(panel) after you add the panels controls to re-fit the sizer.
[edit] Custom Control or Window AutoLayout
If you have a custom window class that doesn't resize its children properly, you must implement an OnSize() event handler, tie it into your event table (through EVT_SIZE), and within that event handler, call Layout().
Background on why this is a problem: usually sizers are used in top-level windows, like wxFrame and wxDialog. Both of these inherit internally from wxTopLevelWindow (I'm looking at the 2.5.2 CVS source). wxTopLevelWindow has its own OnSize handler, which looks something like this:
if(GetAutoLayout()) { Layout(); } else { /* if there's only a single child window, fill it to the full size */ }
So AutoLayout only works automatically in top-level windows. If you want it to work in your own window, you need to handle OnSize() yourself and call Layout.
[edit] Everything Shown at Top-Left
Call SetSizerAndFit(MySizer)
(older versions: Try calling SetAutoLayout(TRUE) for the frame of the top sizer (if it doesn't work, keep a pointer on your topsizer (m_topsizer for instance), and call m_topsizer->Layout() inside the OnSize(wxSizeEvent& event) method of the frame of the top sizer))
[edit] Resizing Windows to Smaller Sizes
You have a dialog that you laid out with sizers. You built the dialog and did all the usual SetAutoLayout(), SetSizeHints() stuff. Now you want to allow functionality of resizing the dialog to be smaller. For example, you want a button on the dialog called "Details" that will toggle between either showing or "collapsing" the bottom half of the dialog.
As of wx 2.3.4, you can use wxSizer::Show()/Hide() to collapse and expand sizers with less trouble.
For earlier versions, you still need to manually reset the min and max values for the size (buried down as members of the wxWidgets class unfortunately), otherwise the size constraints will prevent you from making the dialog smaller than it was. For example, if you wanted to include a function in your sizer-laid-out-dialog to set its height to a certain height in pixels:
void MyCollapsingDialog::UpdateMyHeight( int desired_dialog_height ) { // We have to reset min size constraints or would never reduce the // dialog size when collapsing it and we have to reset max constraint // because it wouldn't expand it otherwise. Note: these are members of the // wxWindow class. m_minHeight = -1; m_maxHeight = -1; // Set the new size hints of the dialog, before we try to set the size otherwise // won't be able to set the size, since it won't let us set to a size that is // not within the range of size hints. Note that we set m_maxHeight to -1 earlier // in this function. SetSizeHints( GetSize().x, desired_dialog_height, m_maxWidth, m_maxHeight ); wxLogDebug( "SetSizeHints=%d,%d,%d,%d", GetSize().x, desired_dialog_height, m_maxWidth, m_maxHeight ); // Set the new size of the dialog. The -1 parameter means leave the width to // whatever it was before. SetSize( -1, desired_dialog_height ); #ifdef __WXGTK__ // Vaclav Slavic: This Show(TRUE) is necessary in order to force dialog redraw under // WindowMaker or fvwm2 (and probably other broken WMs). Otherwise, the newly exposed // parts of the dialog won't be displayed. Show( TRUE ); #endif // wxGTK }
[edit] Size of a wxToolBar
In wxWidgets 2.4.0, if you change the size of a ToolBar using
wxToolBar::SetToolBitmapSize()
The sizer hierarchy, if it was set up with default settings, will not be aware of this size change. To ensure that the toolbar does resize, use the flag wxADJUST_MINSIZE when adding the wxToolBar into the sizer of its parent window.
[edit] Two Hierarchies
There are TWO hierarchies involved:
- The windows hierarchy - which window is a parent of each window.
- The sizer hierarchy - which sizer is a parent of each sizer or window.
So if you have a dialog with loads of controls in it, these will all have the dialog window as their window's parent though there may be several sizers between each of them and the dialog window.
[edit] wxStaticBoxSizer
This section of the article may no longer be relevant judging by this note in the manual from 2.4.2:
Please note that a static box should not be used as the parent for the controls it contains, instead they should be siblings of each other. Although using a static box as a parent might work in some versions of wxWidgets, it results in a crash under, for example, wxGTK.
wxStaticBoxSizer has a design problem, which can sometimes be worked around.
The problem is that a wxStaticBox should be a parent of the controls inside it - otherwise its background area can sometimes get drawn on top of them. However, if you use a wxStaticBoxSizer these controls must have the same parent as the wxStaticBox for otherwise the postioning information will be wrong.
You can often get away with it. For example if you have a single control that fills the wxStaticBox interior you can safely make it a child of the wxStaticBox.
If your controls repaint themselves entirely each time, for example buttons and edit boxes, but not wxListCtrl in report mode, you can make them a child of wxStaticBox's parent.
Suggestion: wxWidgets could be made more consistent if wxStaticBoxSizer were deprecated and instead wxStaticBox was modified so that it could accept a wxBoxSizer using SetSizer().
