#define HEIGHT	20
#define WIDTH	30

class vleaf : public leaf<int>
{
	void draw_data(window *,dataiterator<int> &,int,int) const;
public:
	vleaf(long pos):leaf<int>(pos){};
	~vleaf(){};

	int draw(window *,bptree<int,int> &,int &,int,int,int) const;
};

int vleaf::draw(window *pwnd,bptree<int,int> &tree,int &cur_x,int top,int newval,int x_prevleaf) const
{
	if ( _keys == 0 )
		return 0;//must be a root leaf with no data

	int width = _keys*WIDTH;
	int right = cur_x+width;
	int bottom = top-HEIGHT;
	int iret = cur_x+width/2;
	int left = cur_x;

	int x = cur_x;
	pwnd->draw_text(x,bottom,string("%d",pos()),green);
	pwnd->draw_box(cur_x,bottom,right,top,grey1);

	for(int i=0;i<_keys;i++,x+=WIDTH)
	{
		if ( _key[i] == newval )
			pwnd->draw_box(x,bottom,x+WIDTH,top,red);

		pwnd->draw_segment(x,bottom,x,top,white);
		pwnd->draw_ctext(x+WIDTH/2,top-HEIGHT/2 ,string("%d",_key[i]),
			(_key[i]==newval)?white:black);

		
		draw_data(pwnd, dataiterator<int>(_data[i],tree.getdatafile()) ,x,top-2*HEIGHT);
	}
	cur_x += width;

	if ( x_prevleaf )//draw connecting arrow
		pwnd->draw_arrow(point(x_prevleaf,bottom+HEIGHT/2),point(left,bottom+HEIGHT/2),brown);

	return iret;
}

void vleaf::draw_data(window *pwnd,dataiterator<int> &it,int x,int top) const
{
	int count = 0;
	it.last();
	do
	{
		if ( count==4 )//no more can show
		{
			pwnd->draw_ctext(x+WIDTH/2,top-(HEIGHT-4)/2,string("..."),red);
			return;
		}

		pwnd->draw_box(x+2,top-HEIGHT+4,x+WIDTH-2,top,violet);
		pwnd->draw_ctext(x+WIDTH/2,top-(HEIGHT-4)/2,string("%d",it.data()),white);
		top-=HEIGHT-1;
		count++;
	}while(it.prev());
}

class vnode : public node<int>
{
public:
	enum { nomore = -1 };

	vnode(long pos):node<int>(pos){};
	~vnode(){};

	int draw(window *,int,int,int,queue<int> &,int) const;
};

int vnode::draw(window *pwnd,int leftmin,int rightmax,int top,queue<int> &x_arrow,int y_arrow) const
{
	int width = _keys*WIDTH;
	int offset = (rightmax-leftmin-width)/2;
	int left = leftmin + offset;
	int right = rightmax-offset;
	int bottom = top-HEIGHT;

	pwnd->draw_text(left,bottom,string("%d",pos()),green);
	pwnd->draw_box(left,bottom,right,top,grey3);

	int x = left;
	for(int i=0;i<_keys;i++,x+=WIDTH)
	{
//		if (Values[iKey] == g_iEditValue)
//			pWnd->draw_box(xPos,yPos,xPos + KEY_WIDTH,yPos - NODE_HEIGHT,red);

		pwnd->draw_segment(x,bottom,x,top,white);
		pwnd->draw_ctext(x+WIDTH/2,top-HEIGHT/2 ,string("%d",_key[i]),white);
	}

	//draw arrows
	for (int j=0;j<_keys+1;j++)
	{
		assert( !x_arrow.empty() );
		pwnd->draw_arrow(left+j*WIDTH,bottom,x_arrow.pop(),y_arrow,brown);
	}

	return left+width/2;
}