constraints overviewclasses: wxlayoutconstraints, wxindividuallayoutconstraint. note: constraints are now deprecated and you should use sizers instead. objects of class wxlayoutconstraint can be associated with a window to define the way it is laid out, with respect to its siblings or the parent. the class consists of the following eight constraints of class wxindividuallayoutconstraint, some or all of which should be accessed directly to set the appropriate constraints.
the constraints are initially set to have the relationship wxunconstrained, which means that their values should be calculated by looking at known constraints. to calculate the position and size of the control, the layout algorithm needs to know exactly 4 constraints (as it has 4 numbers to calculate from them), so you should always set exactly 4 of the constraints from the above table. if you want the controls height or width to have the default value, you may use a special value for the constraint: wxasis. if the constraint is wxasis, the dimension will not be changed which is useful for the dialog controls which often have the default size (e.g. the buttons whose size is determined by their label). the constrains calculation is done in wxwindow::layout function which evaluates constraints. to call it you can either call wxwindow::setautolayout if the parent window is a frame, panel or a dialog to tell default onsize handlers to call layout automatically whenever the window size changes, or override onsize and call layout yourself (note that you do have to call layout yourself if the parent window is not a frame, panel or dialog).
constraint layout: more details
constraint layout: more detailsby default, windows do not have a wxlayoutconstraints object. in this case, much layout must be done explicitly, by performing calculations in onsize members, except for the case of frames that have exactly one subwindow (not counting toolbar and statusbar which are also positioned by the frame automatically), where wxframe::onsize takes care of resizing the child to always fill the frame. to avoid the need for these rather awkward calculations, the user can create a wxlayoutconstraints object and associate it with a window with wxwindow::setconstraints. this object contains a constraint for each of the window edges, two for the centre point, and two for the window size. by setting some or all of these constraints appropriately, the user can achieve quite complex layout by defining relationships between windows. in wxwidgets, each window can be constrained relative to either its siblings on the same window, or the parent. the layout algorithm therefore operates in a top-down manner, finding the correct layout for the children of a window, then the layout for the grandchildren, and so on. note that this differs markedly from native motif layout, where constraints can ripple upwards and can eventually change the frame window or dialog box size. we assume in wxwidgets that the user is always 'boss' and specifies the size of the outer window, to which subwindows must conform. obviously, this might be a limitation in some circumstances, but it suffices for most situations, and the simplification avoids some of the nightmarish problems associated with programming motif. when the user sets constraints, many of the constraints for windows edges and dimensions remain unconstrained. for a given window, the wxwindow::layout algorithm first resets all constraints in all children to have unknown edge or dimension values, and then iterates through the constraints, evaluating them. for unconstrained edges and dimensions, it tries to find the value using known relationships that always hold. for example, an unconstrained width may be calculated from the left and right edges, if both are currently known. for edges and dimensions with user-supplied constraints, these constraints are evaluated if the inputs of the constraint are known. the algorithm stops when all child edges and dimension are known (success), or there are unknown edges or dimensions but there has been no change in this cycle (failure). it then sets all the window positions and sizes according to the values it has found. because the algorithm is iterative, the order in which constraints are considered is irrelevant, however you may reduce the number of iterations (and thus speed up the layout calculations) by creating the controls in such order that as many constraints as possible can be calculated during the first iteration. for example, if you have 2 buttons which you'd like to position in the lower right corner, it is slightly more efficient to first create the second button and specify that its right border issameas(parent, wxright) and then create the first one by specifying that it should be leftof() the second one than to do in a more natural left-to-right order.
window layout examples
example 1: subwindow layoutthis example specifies a panel and a window side by side, with a text subwindow below it.
frame->panel = new wxpanel(frame, -1, wxpoint(0, 0), wxsize(1000, 500), 0); frame->scrollwindow = new myscrolledwindow(frame, -1, wxpoint(0, 0), wxsize(400, 400), wxretained); frame->text_window = new mytextwindow(frame, -1, wxpoint(0, 250), wxsize(400, 250)); // set constraints for panel subwindow wxlayoutconstraints *c1 = new wxlayoutconstraints; c1->left.sameas (frame, wxleft); c1->top.sameas (frame, wxtop); c1->right.percentof (frame, wxwidth, 50); c1->height.percentof (frame, wxheight, 50); frame->panel->setconstraints(c1); // set constraints for scrollwindow subwindow wxlayoutconstraints *c2 = new wxlayoutconstraints; c2->left.sameas (frame->panel, wxright); c2->top.sameas (frame, wxtop); c2->right.sameas (frame, wxright); c2->height.percentof (frame, wxheight, 50); frame->scrollwindow->setconstraints(c2); // set constraints for text subwindow wxlayoutconstraints *c3 = new wxlayoutconstraints; c3->left.sameas (frame, wxleft); c3->top.below (frame->panel); c3->right.sameas (frame, wxright); c3->bottom.sameas (frame, wxbottom); frame->text_window->setconstraints(c3); example 2: panel item layoutthis example sizes a button width to 80 percent of the panel width, and centres it horizontally. a listbox and multitext item are placed below it. the listbox takes up 40 percent of the panel width, and the multitext item takes up the remainder of the width. margins of 5 pixels are used.
// create some panel items wxbutton *btn1 = new wxbutton(frame->panel, -1, "a button") ; wxlayoutconstraints *b1 = new wxlayoutconstraints; b1->centrex.sameas (frame->panel, wxcentrex); b1->top.sameas (frame->panel, wxtop, 5); b1->width.percentof (frame->panel, wxwidth, 80); b1->height.percentof (frame->panel, wxheight, 10); btn1->setconstraints(b1); wxlistbox *list = new wxlistbox(frame->panel, -1, "a list", wxpoint(-1, -1), wxsize(200, 100)); wxlayoutconstraints *b2 = new wxlayoutconstraints; b2->top.below (btn1, 5); b2->left.sameas (frame->panel, wxleft, 5); b2->width.percentof (frame->panel, wxwidth, 40); b2->bottom.sameas (frame->panel, wxbottom, 5); list->setconstraints(b2); wxtextctrl *mtext = new wxtextctrl(frame->panel, -1, "multiline text", "some text", wxpoint(-1, -1), wxsize(150, 100), wxte_multiline); wxlayoutconstraints *b3 = new wxlayoutconstraints; b3->top.below (btn1, 5); b3->left.rightof (list, 5); b3->right.sameas (frame->panel, wxright, 5); b3->bottom.sameas (frame->panel, wxbottom, 5); mtext->setconstraints(b3); |