]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/boxsizer.tex
compilation/build fixes for wxUniv
[wxWidgets.git] / docs / latex / wx / boxsizer.tex
CommitLineData
8fe05782
VZ
1\section{\class{wxBoxSizer}}\label{wxboxsizer}
2
be2577e4 3The basic idea behind a box sizer is that windows will most often be laid out in rather
1912918c 4simple basic geometry, typically in a row or a column or several hierarchies of either.
515da557 5
f6bcfd97 6As an example, we will construct a dialog that will contain a text field at the top and
515da557 7two buttons at the bottom. This can be seen as a top-hierarchy column with the text at
1912918c
RL
8the top and buttons at the bottom and a low-hierarchy row with an OK button to the left
9and a Cancel button to the right. In many cases (particularly dialogs under Unix and
515da557
RR
10normal frames) the main window will be resizable by the user and this change of size
11will have to get propagated to its children. In our case, we want the text area to grow
12with the dialog, whereas the button shall have a fixed size. In addition, there will be
13a thin border around all controls to make the dialog look nice and - to make matter worse -
14the buttons shall be centred as the width of the dialog changes.
15
16It is the unique feature of a box sizer, that it can grow in both directions (height and
f6bcfd97 17width) but can distribute its growth in the main direction (horizontal for a row) {\it unevenly}
515da557 18among its children. In our example case, the vertical sizer is supposed to propagate all its
7e9a386e
JS
19height changes to only the text area, not to the button area. This is determined by the {\it option} parameter
20when adding a window (or another sizer) to a sizer. It is interpreted
515da557
RR
21as a weight factor, i.e. it can be zero, indicating that the window may not be resized
22at all, or above zero. If several windows have a value above zero, the value is interpreted
23relative to the sum of all weight factors of the sizer, so when adding two windows with
be2577e4 24a value of 1, they will both get resized equally much and each half as much as the sizer
515da557 25owning them. Then what do we do when a column sizer changes its width? This behaviour is
be2577e4 26controlled by {\it flags} (the second parameter of the Add() function): Zero or no flag
f6bcfd97
BP
27indicates that the window will preserve it is original size, wxGROW flag (same as wxEXPAND)
28forces the window to grow with the sizer, and wxSHAPED flag tells the window to change it is
be2577e4
RD
29size proportionally, preserving original aspect ratio. When wxGROW flag is not used,
30the item can be aligned within available space. wxALIGN\_LEFT, wxALIGN\_TOP, wxALIGN\_RIGHT,
31wxALIGN\_BOTTOM, wxALIGN\_CENTER\_HORIZONTAL and wxALIGN\_CENTER\_VERTICAL do what they say.
32wxALIGN\_CENTRE (same as wxALIGN\_CENTER) is defined as (wxALIGN\_CENTER\_HORIZONTAL |
33wxALIGN\_CENTER\_VERTICAL). Default alignment is wxALIGN\_LEFT | wxALIGN\_TOP.
515da557
RR
34
35As mentioned above, any window belonging to a sizer may have border, and it can be specified
36which of the four sides may have this border, using the wxTOP, wxLEFT, wxRIGHT and wxBOTTOM
37constants or wxALL for all directions (and you may also use wxNORTH, wxWEST etc instead). These
f6bcfd97 38flags can be used in combination with the alignment flags above as the second parameter of the
515da557
RR
39Add() method using the binary or operator |. The sizer of the border also must be made known,
40and it is the third parameter in the Add() method. This means, that the entire behaviour of
41a sizer and its children can be controlled by the three parameters of the Add() method.
42
43\begin{verbatim}
44// we want to get a dialog that is stretchable because it
45// has a text ctrl at the top and two buttons at the bottom
46
eb750029
VZ
47MyDialog::MyDialog(wxFrame *parent, wxWindowID id, const wxString &title )
48 : wxDialog(parent, id, title, wxDefaultPosition, wxDefaultSize,
49 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
515da557
RR
50{
51 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
be2577e4 52
515da557 53 // create text ctrl with minimal size 100x60
be2577e4 54 topsizer->Add(
515da557
RR
55 new wxTextCtrl( this, -1, "My text.", wxDefaultPosition, wxSize(100,60), wxTE_MULTILINE),
56 1, // make vertically stretchable
57 wxEXPAND | // make horizontally stretchable
58 wxALL, // and make border all around
59 10 ); // set border width to 10
60
61
62 wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
63 button_sizer->Add(
be2577e4 64 new wxButton( this, wxID_OK, "OK" ),
515da557
RR
65 0, // make horizontally unstretchable
66 wxALL, // make border all around (implicit top alignment)
67 10 ); // set border width to 10
68 button_sizer->Add(
be2577e4 69 new wxButton( this, wxID_CANCEL, "Cancel" ),
515da557
RR
70 0, // make horizontally unstretchable
71 wxALL, // make border all around (implicit top alignment)
72 10 ); // set border width to 10
be2577e4
RD
73
74 topsizer->Add(
515da557 75 button_sizer,
be2577e4
RD
76 0, // make vertically unstretchable
77 wxALIGN_CENTER ); // no border and centre horizontally
515da557
RR
78
79 SetAutoLayout( TRUE ); // tell dialog to use sizer
80 SetSizer( topsizer ); // actually set the sizer
81
82 topsizer->Fit( this ); // set size to minimum size as calculated by the sizer
83 topsizer->SetSizeHints( this ); // set size hints to honour mininum size
515da557
RR
84}
85\end{verbatim}
8fe05782 86
8fe05782
VZ
87\wxheading{Derived from}
88
36edded9 89\helpref{wxSizer}{wxsizer}\\
4130b487 90\helpref{wxObject}{wxobject}
8fe05782
VZ
91
92\membersection{wxBoxSizer::wxBoxSizer}\label{wxboxsizerwxboxsizer}
93
94\func{}{wxBoxSizer}{\param{int }{orient}}
95
515da557
RR
96Constructor for a wxBoxSizer. {\it orient} may be either of wxVERTICAL
97or wxHORIZONTAL for creating either a column sizer or a row sizer.
8fe05782
VZ
98
99\membersection{wxBoxSizer::RecalcSizes}\label{wxboxsizerrecalcsizes}
100
101\func{void}{RecalcSizes}{\void}
102
9c884972 103Implements the calculation of a box sizer's dimensions and then sets
fa482912 104the size of its its children (calling \helpref{wxWindow::SetSize}{wxwindowsetsize}
be2577e4 105if the child is a window). It is used internally only and must not be called
fa482912 106by the user. Documented for information.
8fe05782
VZ
107
108\membersection{wxBoxSizer::CalcMin}\label{wxboxsizercalcmin}
109
110\func{wxSize}{CalcMin}{\void}
111
9c884972 112Implements the calculation of a box sizer's minimal. It is used internally
fa482912 113only and must not be called by the user. Documented for information.
8fe05782
VZ
114
115\membersection{wxBoxSizer::GetOrientation}\label{wxboxsizergetorientation}
116
117\func{int}{GetOrientation}{\void}
118
fa482912 119Returns the orientation of the box sizer, either wxVERTICAL
9c884972
RR
120or wxHORIZONTAL.
121