Manually handling margins and padding
Basically, adding margins and padding is done with a series of context pushes and translations. Since margin and padding functions are removed from 4.5.0 and 4.6.0, we can take this chance to handle it ourselves. I chose to go the same route I took with me-widgets. The margin and padding will be stored in Edges (not XYEdges) objects. There are three places you will need to alter:
- getPreferredWidth() and getPreferredHeight()
- layout() or subLayout()
- paint()
- Declare the variables. Keep them final, the objects do not need to be changing, just the values inside them.
private final Edges padding = new Edges(); private final Edges margin = new Edges();
- Define the getters and setters. Notice that there is no direct access to the variables.
public final Edges getPadding() { return new Edges( padding ); } public final void getPadding( Edges edges ) { edges.set( padding ); } public final void setPadding( int top, int right, int bottom, int left ) { padding.set( top, right, bottom, left ); } public final void setPadding( Edges edges ) { padding.set( edges ); } public final Edges getMargin() { return new Edges( margin ); } public final void getMargin( Edges edges ) { edges.set( margin ); } public final void setMargin( int top, int right, int bottom, int left ) { margin.set( top, right, bottom, left ); } public final void setMargin( Edges edges ) { margin.set( edges ); }
- Adjust the preferred width and height.
public int getPreferredWidth() { Edges padding = getPadding(); Edges margin = getMargin(); return super.getPreferredWidth() + margin.getWidth() + padding.getWidth(); } public int getPreferredHeight() { Edges padding = getPadding(); Edges margin = getMargin(); return super.getPreferredHeight() + margin.getHeight() + padding.getHeight(); }
- Set the extent.
protected void layout( int width, int height ) { width = getPreferredWidth(); height = getPreferredHeight(); super.layout( width, height ); setExtent( width, height ); }
- Override paint(). This part is the key, so keep it final. Since paintBackground() is undocumented, we are going to take this opportunity to handle it ourselves also. The two paint functions are replaced with similar functions that will provide the actual width and height you need after margins and padding is calculated.
protected void paintBackground( Graphics graphics, int width, int height ) { // paint background } protected void paint( Graphics graphics, int width, int height ) { // paint something super.paint( graphics ); } protected final void paint( Graphics graphics ) { Edges padding = getPadding(); Edges margin = getMargin(); int width = getPreferredWidth(); int height = getPreferredHeight(); // add margin width -= margin.getWidth(); height -= margin.getHeight(); MobileGraphics.instance().pushContext( graphics, margin.getLeft(), margin.getTop(), width, height ); MobileGraphics.instance().translate( graphics, margin.getLeft(), margin.getTop() ); paintBackground( graphics, width, height ); // add padding width -= padding.getWidth(); height -= padding.getHeight(); MobileGraphics.instance().pushContext( graphics, padding.getLeft(), padding.getTop(), width, height ); MobileGraphics.instance().translate( graphics, padding.getLeft(), padding.getTop() ); paint( graphics, width, height ); // remove padding MobileGraphics.instance().popContext( graphics, -1, -1, -1, -1 ); // remove margin MobileGraphics.instance().popContext( graphics, -1, -1, -1, -1 ); }
