package typo;

import java.awt.Graphics;

public class Hbox extends Group {

	public void doDraw(Graphics g, double x, double y, double w) {
		double dw = w - getMinimalWidth();
		if (dw < 0) {
			System.out.println("overfull hbox: " + (-dw) + "pixels too wide");
			dw = 0;
		}
		double stretch = getStretchingCapacity() == 0. ? 0. : dw / getStretchingCapacity();
		for (Box b : contents) {
			double dy = ascent - b.getAscent();
			double wb = b.getMinimalWidth() + b.getStretchingCapacity() * stretch;
			b.draw(g, x, y + dy, wb);
			x += wb;
		}
	}

	public void add(Box b) {
		super.add(b);
		ascent = Math.max(ascent, b.getAscent());
		descent = Math.max(descent, b.getDescent());
		minimalWidth += b.getMinimalWidth(); 
		stretchingCapacity += b.getStretchingCapacity();
	}

	public String toString() {
		return "Hbox" + super.toString();
	}


}

