%%
headers
/* vim: syntax=c
 */
#include <Python.h>
#define NO_IMPORT_PYGOBJECT 1
#define NO_IMPORT_PYGTK 1
#include <pygobject.h>
#include <pygtk/pygtk.h>
#include <diacanvas/dia-geometry.h>
#include <diacanvas/diatypebuiltins.h>

%%
ignore-glob
  *_get_type
  _*
%%
override dia_rectangle_add_point kwargs
static PyObject *
_wrap_dia_rectangle_add_point(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "rect", "point", NULL };
    PyObject *py_rect, *py_point;
    GValue v_rect = { 0, }, v_point = {0, };
    DiaPoint *point = NULL;
    DiaRectangle *rect = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO:rectangle_add_point", kwlist, &py_rect, &py_point))
        return NULL;

    g_value_init (&v_point, DIA_TYPE_POINT);
    g_value_init (&v_rect, DIA_TYPE_RECTANGLE);

    if (pyg_value_from_pyobject(&v_rect, py_rect) != 0) {
        PyErr_SetString(PyExc_TypeError, "rect should be a tuple of 4 floats");
        return NULL;
    }
    if (pyg_value_from_pyobject(&v_point, py_point) != 0) {
        PyErr_SetString(PyExc_TypeError, "point should be a tuple of 2 floats");
        return NULL;
    }
    rect = g_value_get_boxed (&v_rect);
    point = g_value_get_boxed (&v_point);

    dia_rectangle_add_point(rect, point);
    py_rect = pyg_value_as_pyobject(&v_rect, TRUE);

    g_value_unset (&v_rect);
    g_value_unset (&v_point);

    return py_rect;
}
%%
override dia_distance_point_point kwargs
static PyObject *
_wrap_dia_distance_point_point(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "p1", "p2", NULL };
    PyObject *py_p1, *py_p2;
    GValue v_p1 = { 0, }, v_p2 = {0, };
    double ret;
    DiaPoint *p1 = NULL, *p2 = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO:distance_point_point", kwlist, &py_p1, &py_p2))
        return NULL;

    g_value_init (&v_p1, DIA_TYPE_POINT);
    g_value_init (&v_p2, DIA_TYPE_POINT);

    if (pyg_value_from_pyobject(&v_p1, py_p1) != 0) {
        PyErr_SetString(PyExc_TypeError, "p1 should be a tuple of 2 floats");
        return NULL;
    }
    if (pyg_value_from_pyobject(&v_p2, py_p2) != 0) {
        PyErr_SetString(PyExc_TypeError, "p2 should be a tuple of 2 floats");
        return NULL;
    }
    p1 = g_value_get_boxed (&v_p1);
    p2 = g_value_get_boxed (&v_p2);

    ret = dia_distance_point_point(p1, p2);

    g_value_unset (&v_p1);
    g_value_unset (&v_p2);

    return PyFloat_FromDouble(ret);
}
%%
override dia_distance_point_point_manhattan kwargs
static PyObject *
_wrap_dia_distance_point_point_manhattan(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "p1", "p2", NULL };
    PyObject *py_p1, *py_p2;
    GValue v_p1 = { 0, }, v_p2 = {0, };
    double ret;
    DiaPoint *p1 = NULL, *p2 = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO:distance_point_point_manhattan", kwlist, &py_p1, &py_p2))
        return NULL;

    g_value_init (&v_p1, DIA_TYPE_POINT);
    g_value_init (&v_p2, DIA_TYPE_POINT);

    if (pyg_value_from_pyobject(&v_p1, py_p1) != 0) {
        PyErr_SetString(PyExc_TypeError, "p1 should be a tuple of 2 floats");
        return NULL;
    }
    if (pyg_value_from_pyobject(&v_p2, py_p2) != 0) {
        PyErr_SetString(PyExc_TypeError, "p2 should be a tuple of 2 floats");
        return NULL;
    }
    p1 = g_value_get_boxed (&v_p1);
    p2 = g_value_get_boxed (&v_p2);

    ret = dia_distance_point_point_manhattan(p1, p2);

    g_value_unset (&v_p1);
    g_value_unset (&v_p2);

    return PyFloat_FromDouble(ret);
}
%%
override dia_distance_rectangle_point kwargs
static PyObject *
_wrap_dia_distance_rectangle_point(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "rect", "point", NULL };
    PyObject *py_rect, *py_point;
    GValue v_rect = { 0, }, v_point = {0, };
    double ret;
    DiaPoint *point = NULL;
    DiaRectangle *rect = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO:distance_rectangle_point", kwlist, &py_rect, &py_point))
        return NULL;
    g_value_init (&v_point, DIA_TYPE_POINT);
    g_value_init (&v_rect, DIA_TYPE_RECTANGLE);

    if (pyg_value_from_pyobject(&v_rect, py_rect) != 0) {
        PyErr_SetString(PyExc_TypeError, "rect should be a tuple of 4 floats");
        return NULL;
    }
    if (pyg_value_from_pyobject(&v_point, py_point) != 0) {
        PyErr_SetString(PyExc_TypeError, "point should be a tuple of 2 floats");
        return NULL;
    }
    rect = g_value_get_boxed (&v_rect);
    point = g_value_get_boxed (&v_point);

    ret = dia_distance_rectangle_point(rect, point);

    g_value_unset (&v_rect);
    g_value_unset (&v_point);

    return PyFloat_FromDouble(ret);
}
%%
override dia_distance_line_point kwargs
static PyObject *
_wrap_dia_distance_line_point(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "line_start", "line_end", "point", "line_width", "style", NULL };
    PyObject *py_line_start, *py_line_end, *py_point, *py_style = NULL;
    GValue v_line_start = { 0, }, v_line_end = { 0, }, v_point = { 0, };
    double line_width, ret;
    DiaPoint *line_start = NULL, *line_end = NULL, *point = NULL, point_on_line;
    DiaCapStyle style;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOOdO:distance_line_point", kwlist, &py_line_start, &py_line_end, &py_point, &line_width, &py_style))
        return NULL;

    g_value_init (&v_line_start, DIA_TYPE_POINT);
    g_value_init (&v_line_end, DIA_TYPE_POINT);
    g_value_init (&v_point, DIA_TYPE_POINT);

    if (pyg_value_from_pyobject(&v_line_start, py_line_start) != 0) {
        PyErr_SetString(PyExc_TypeError, "line_start should be a tuple of 2 floats");
        return NULL;
    }
    if (pyg_value_from_pyobject(&v_line_end, py_line_end) != 0) {
        PyErr_SetString(PyExc_TypeError, "line_end should be a tuple of 2 floats");
        return NULL;
    }
    if (pyg_value_from_pyobject(&v_point, py_point) != 0) {
        PyErr_SetString(PyExc_TypeError, "point should be a tuple of 2 floats");
        return NULL;
    }
    if (pyg_enum_get_value(DIA_TYPE_CAP_STYLE, py_style, (gint *)&style))
        return NULL;

    line_start = g_value_get_boxed (&v_line_start);
    line_end = g_value_get_boxed (&v_line_end);
    point = g_value_get_boxed (&v_point);

    ret = dia_distance_line_point(line_start, line_end, point, line_width, style, &point_on_line);

    g_value_unset (&v_line_start);
    g_value_unset (&v_line_end);
    g_value_unset (&v_point);

    return Py_BuildValue ("(d(dd))", ret, point_on_line.x, point_on_line.y);
}
%%
override dia_intersection_line_line kwargs
static PyObject *
_wrap_dia_intersection_line_line(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "start1", "end1", "start2", "end2", NULL };
    PyObject *py_start1, *py_end1, *py_start2, *py_end2;
    GValue v_start1 = { 0, }, v_end1 = { 0, }, v_start2 = { 0, }, v_end2 = { 0, };
    int ret;
    DiaPoint *start1 = NULL, *end1 = NULL, *start2 = NULL, *end2 = NULL, intersect;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOOO:intersection_line_line", kwlist, &py_start1, &py_end1, &py_start2, &py_end2))
        return NULL;

    g_value_init (&v_start1, DIA_TYPE_POINT);
    g_value_init (&v_end1, DIA_TYPE_POINT);
    g_value_init (&v_start2, DIA_TYPE_POINT);
    g_value_init (&v_end2, DIA_TYPE_POINT);

    if (pyg_value_from_pyobject(&v_start1, py_start1) != 0) {
        PyErr_SetString(PyExc_TypeError, "start1 should be a tuple of 2 floats");
        return NULL;
    }
    if (pyg_value_from_pyobject(&v_end1, py_end1) != 0) {
        PyErr_SetString(PyExc_TypeError, "end1 should be a tuple of 2 floats");
        return NULL;
    }
    if (pyg_value_from_pyobject(&v_start2, py_start2) != 0) {
        PyErr_SetString(PyExc_TypeError, "start2 should be a tuple of 2 floats");
        return NULL;
    }
    if (pyg_value_from_pyobject(&v_end2, py_end2) != 0) {
        PyErr_SetString(PyExc_TypeError, "end2 should be a tuple of 2 floats");
        return NULL;
    }
    
    start1 = g_value_get_boxed (&v_start1);
    end1 = g_value_get_boxed (&v_end1);
    start2 = g_value_get_boxed (&v_start2);
    end2 = g_value_get_boxed (&v_end2);

    ret = dia_intersection_line_line(start1, end1, start2, end2, &intersect);

    g_value_unset (&v_start1);
    g_value_unset (&v_end1);
    g_value_unset (&v_start2);
    g_value_unset (&v_end2);

    if (ret)
	return Py_BuildValue("(dd)", intersect.x, intersect.y);

    Py_INCREF (Py_None);
    return Py_None;
}
%%
override dia_intersection_line_rectangle kwargs
static PyObject *
_wrap_dia_intersection_line_rectangle(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "start", "end", "rect", NULL };
    PyObject *py_start, *py_end, *py_rect;
    GValue v_start = { 0, }, v_end = { 0, }, v_rect = { 0, };
    int ret;
    DiaPoint *start = NULL, *end = NULL, intersect[2];
    DiaRectangle *rect = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOO:intersection_line_rectangle", kwlist, &py_start, &py_end, &py_rect))
        return NULL;

    g_value_init (&v_start, DIA_TYPE_POINT);
    g_value_init (&v_end, DIA_TYPE_POINT);
    g_value_init (&v_rect, DIA_TYPE_RECTANGLE);

    if (pyg_value_from_pyobject(&v_start, py_start) != 0) {
        PyErr_SetString(PyExc_TypeError, "start should be a tuple of 2 floats");
        return NULL;
    }
    if (pyg_value_from_pyobject(&v_end, py_end) != 0) {
        PyErr_SetString(PyExc_TypeError, "end should be a tuple of 2 floats");
        return NULL;
    }
    if (pyg_value_from_pyobject(&v_rect, py_rect) != 0) {
        PyErr_SetString(PyExc_TypeError, "rect should be a tuple of 4 floats");
        return NULL;
    }
    
    start = g_value_get_boxed (&v_start);
    end = g_value_get_boxed (&v_end);
    rect = g_value_get_boxed (&v_rect);

    ret = dia_intersection_line_rectangle(start, end, rect, intersect);

    g_value_unset (&v_start);
    g_value_unset (&v_end);
    g_value_unset (&v_rect);

    if (ret == 1)
	return Py_BuildValue("((dd)O)", intersect[0].x, intersect[0].y, Py_None);

    if (ret == 2)
	return Py_BuildValue("((dd)(dd))", intersect[0].x, intersect[0].y, intersect[1].x, intersect[1].y);

    return Py_BuildValue ("(OO)", Py_None, Py_None);
}
%%
override dia_intersection_rectangle_rectangle kwargs
static PyObject *
_wrap_dia_intersection_rectangle_rectangle(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "r1", "r2", NULL };
    PyObject *py_r1, *py_r2;
    GValue v_r1 = { 0, }, v_r2 = { 0, };
    int ret;
    DiaRectangle *r1 = NULL, *r2 = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO:intersection_rectangle_rectangle", kwlist, &py_r1, &py_r2))
        return NULL;

    g_value_init (&v_r1, DIA_TYPE_RECTANGLE);
    g_value_init (&v_r2, DIA_TYPE_RECTANGLE);

    if (pyg_value_from_pyobject(&v_r1, py_r1) != 0) {
        PyErr_SetString(PyExc_TypeError, "r1 should be a tuple of 4 floats");
        return NULL;
    }
    if (pyg_value_from_pyobject(&v_r2, py_r2) != 0) {
        PyErr_SetString(PyExc_TypeError, "r2 should be a tuple of 4 floats");
        return NULL;
    }

    r1 = g_value_get_boxed (&v_r1);
    r2 = g_value_get_boxed (&v_r2);

    ret = dia_intersection_rectangle_rectangle(r1, r2);
    
    g_value_unset (&v_r1);
    g_value_unset (&v_r2);

    return PyInt_FromLong(ret);
}
