#include<stk.h>
#include<strstream.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/layout/Layouts.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/graph/Graph.h>

static PRIMITIVE GridLayoutCmd(SCM g, SCM rows, SCM cols)
{
        Graph *gr;
        if (!(gr = getGraph(g)))
                Err("grid-layout!: bad arg 1, graph expected", g);
        if (!INTEGERP(rows))
                Err("grid-layout!: bad arg 2, #rows expected", rows);
        if (!INTEGERP(cols))
                Err("grid-layout!: bad arg 2, #cols expected", cols);
        GridLayout(gr, INTEGER(rows), INTEGER(cols));

        return UNDEFINED;
}

static PRIMITIVE GridLayoutAnimCmd(SCM gparms, int len)
{
        SCM g, gv, rows, cols;

        if (len < 4)
                return UNDEFINED;
        g = CAR(gparms);
        gv = CAR(CDR(gparms));
        rows = CAR(CDR(CDR(gparms)));
        cols = CAR(CDR(CDR(CDR(gparms))));

	Graph *gr;
        if (!(gr = getGraph(g)))
                Err("grid-layout!: bad arg 1, graph expected", g);
	char *vname = getViewName(gv);
        if (!INTEGERP(rows))
                Err("grid-layout!: bad arg 2, #rows expected", rows);
        if (!INTEGERP(cols))
                Err("grid-layout!: bad arg 2, #cols expected", rows);
        GridLayout(gr, INTEGER(rows), INTEGER(cols));
	path_draw(vname);
	layoutVertices(vname);
        return UNDEFINED;
}

void gen_GridLayout_Wrapper()
{
        STk_add_new_cpp_primitive("grid-layout", tc_subr_3,
                               (PRIMITIVE (*)(...))GridLayoutCmd);
        STk_add_new_cpp_primitive("grid-layout*", tc_lsubr, 
                               (PRIMITIVE (*)(...))GridLayoutAnimCmd);
}
