The Layout Library: Creating your own classes





To create your own classes that will be able to work with the layout-library, your classes must derive from both MView and BView. You must implement the layoutprefs and layout members of the MView class.


Creating your own layoutable classes

Through the layoutprefs-function, you tell the system about the minimum and maximum size of your class.
The current grouping classes guarantee that your object will not be asked to become smaller than the size you give as the minimum size. Note that this guarantee is currently enforced by making the minimum window-size equal to the minimum size of the root-MView. If you do respect the minimum size of a grouping class, then the grouping class will not respect the minimum size of the MViews it contains.

The layoutprefs-function is defined as:

    minimax layoutprefs(void)
and the minimax class is defined as:
class minimax
{
     public:    BPoint  mini;
                BPoint  maxi;
                float   weight;
				
                minimax(int minx=0,int miny=0,
                        int maxx=65536,int maxy=65536,
                        float weight=1);
                minimax(BPoint min,BPoint max,float wght=1);
};
where the mini and maxi members are used to store the minimum and maximum size of the object.
The weight member is used to give each object a weight relative to its siblings. If two objects, A and B, are in a group, and A has a weight of 2, and B has a weight of 1, then A will get twice as much space as B (provided this doesn't conflict with the minimum and maximum sizes). The default weight of an MView is 1, meaning all MViews get an equal share of the available space.


Through the layout function, your class is asked to layout itself. Again, if you have correctly setup the window-limits to be the limits of the root-MView, then it is guaranteed that your object will not be asked to resize to a size that's smaller than its minimum size.

It is possible that your object is asked to resize to a size that is bigger than its maximum size. In this case, the object should center itself within the provided space.

The layout-function is defined as:

   BRect layout(BRect size)
The size parameter determines where the object should be placed in its parent. A simple implementation of the layout function will simply do
BRect MyClass::layout(BRect rect) 
{ 
        ResizeTo(rect.Width(),rect.Height()); 
        MoveTo(rect.LeftTop()); 
        return rect; 
}
The layout function returns the amount of space actually used by the object.


Creating your own grouping classes

To create your own grouping classes, you must take a little more responsibility:

Otherwise, writing a grouping class is just like writing a 'leaf' class.

Copyright © 1997 Marco Nelissen (marcone@xs4all.nl) All rights reserved.

Be is a registered trademark; BeOS, BeBox, BeWare, GeekPort, the Be logo, and the BeOS logo are trademarks of Be, Inc.