The layout-library is a library of classes, which together allow you to easily make fully resizable and font-sensitive GUI's. It does this by giving standard Be-classes a little more knowledge about themselves, and adding some custom classes that do the actual layout-work.
At the base of the library are two classes: minimax and MView.
The MView class is an abstract class that declares the layout and layoutprefs methods. Through these methods, an MView-derived class is able to tell the world about its preferred sizes, and the world is able to tell the MView-derived class to go resize itself to a certain size and position.
The minimax class is a class that is used to store an MView's minimum and maximum size (hence the name). It also stores the relative weight of the MView. Each MView has a minimax-member called mpm, which reflects the current minimum and maximum size. There is also a minimax-member called ct_mpm which is a copy of the size specified at construction time. The layoutprefs() function will recalculate mpm based on ct_mpm and the
current font-settings. If you want to manually change the size-constraints after construction, you should
therefore alter the ct_mpm member.
All classes that participate in the layout-system must derive from MView, and implement the layout and layoutprefs members. Depending on the function of the class, these implementations can be very complex, or very simple, like so:
minimax MyClass::layoutprefs() { return mpm; } BRect MyClass::layout(BRect rect) { ResizeTo(rect.Width(),rect.Height()); MoveTo(rect.LeftTop()); return rect; }The above would be used to implement the layout and layoutprefs members of a class with no constraints as to its minimum and maximum sizes. Note that the class also derives from BView, from where it obtains the ResizeTo() and MoveTo() members.
Some of the classes in the library have as their only purpose in life to group other classes together. The ones you'll use most frequently are the HGroup and VGroup classes, but there are also grouping classes that group together MViews using tabviews, or merely add a border around MViews.
There is an abstract base-class for grouping classes called MGroup. Although an MGroup simply derives from MView, it is recommended that when you create your own grouping classes, you derive them from MGroup. By doing this, you make sure that other classes recognize your class as being a grouping class. Non grouping classes are usually the controls that you interact with, like buttons, checkboxes and sliders.
To build a fully resizable GUI, you create a tree of objects. At the root of the tree is a grouping class, the nodes in the tree also consist of grouping classes, and the leaves of the tree are the controls you interact with.
A simple example is:
MView *topview=new VGroup ( new HGroup ( new MButton("upper left corner"), new MButton("upper right corner"), 0 ), new MButton("along the bottom"), 0 );The above defines a group of three buttons. Two of those buttons are aranged in a row along the top, with each button getting half of the available space. The third button is placed at the bottom, below the other two buttons.
Some things to keep in mind:
Once you have built the tree, you add it to a window, and call the layoutprefs member of the root-MView. This causes the internal states of all the MViews to be initialised.
When you are ready to display the GUI on screen, you call the layout member of the root-MView. This will cause all grouping classes to determine how much space they have, and layout themselves and their children accordingly.
Note that you only need to call layoutprefs once (unless you add items to the tree later on, in which case you have to call it again), but that you have to call layout everytime the window gets resized.
Typically, you'll use an MWindow-object to do this for you. If you decide not to use MWindow (although I can't imagine why you'd do that), you'll call the layout() function from the FrameResized() of your window.
Copyright © 1997 Marco Nelissen (marcone@xs4all.nl) All rights reserved.
Be, BeOS, BeBox, BeWare, GeekPort, the Be logo, the BeOS logo, roColour, Beatware and Beatware painter are trademarks or registered trademarks of their respective owners.