%%
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-canvas.h>
#include <diacanvas/dia-shape.h>
#include <diacanvas/dia-geometry.h>
#include <diacanvas/diatypebuiltins.h>


/* This might look evil, but it avoid unnessesary warnings...
 *
 * In Python the DiaShape class is split up in separate classes, one
 * for each shape type. Since shapes have some common operations we have
 * to add them to all python classes seperately.
 */

#define DiaShapePath DiaShape
#define dia_shape_path_set_color(s, c) dia_shape_set_color((DiaShape*)s, c)
#define dia_shape_path_request_update(s) dia_shape_request_update((DiaShape*)s)
#define dia_shape_path_need_update(s) dia_shape_need_update((DiaShape*)s)
#define dia_shape_path_set_visibility(s,v) dia_shape_set_visibility((DiaShape*)s,v)
#define dia_shape_path_get_bounds(s,b) dia_shape_get_bounds((DiaShape*)s,b)

#define DiaShapeBezier DiaShape
#define dia_shape_bezier_set_color(s, c) dia_shape_set_color((DiaShape*)s, c)
#define dia_shape_bezier_request_update(s) dia_shape_request_update((DiaShape*)s)
#define dia_shape_bezier_need_update(s) dia_shape_need_update((DiaShape*)s)
#define dia_shape_bezier_set_visibility(s,v) dia_shape_set_visibility((DiaShape*)s,v)
#define dia_shape_bezier_get_bounds(s,b) dia_shape_get_bounds((DiaShape*)s,b)

#define DiaShapeEllipse DiaShape
#define dia_shape_ellipse_set_color(s, c) dia_shape_set_color((DiaShape*)s, c)
#define dia_shape_ellipse_request_update(s) dia_shape_request_update((DiaShape*)s)
#define dia_shape_ellipse_need_update(s) dia_shape_need_update((DiaShape*)s)
#define dia_shape_ellipse_set_visibility(s,v) dia_shape_set_visibility((DiaShape*)s,v)
#define dia_shape_ellipse_get_bounds(s,b) dia_shape_get_bounds((DiaShape*)s,b)

#define DiaShapeText DiaShape
#define dia_shape_text_set_color(s, c) dia_shape_set_color((DiaShape*)s, c)
#define dia_shape_text_request_update(s) dia_shape_request_update((DiaShape*)s)
#define dia_shape_text_need_update(s) dia_shape_need_update((DiaShape*)s)
#define dia_shape_text_set_visibility(s,v) dia_shape_set_visibility((DiaShape*)s,v)
#define dia_shape_text_get_bounds(s,b) dia_shape_get_bounds((DiaShape*)s,b)

#define DiaShapeImage DiaShape
#define dia_shape_image_request_update(s) dia_shape_request_update((DiaShape*)s)
#define dia_shape_image_need_update(s) dia_shape_need_update((DiaShape*)s)
#define dia_shape_image_set_visibility(s,v) dia_shape_set_visibility((DiaShape*)s,v)
#define dia_shape_image_get_bounds(s,b) dia_shape_get_bounds((DiaShape*)s,b)

/* Return the number of points in **points. -1 is returned in case of an error.
 */
static gint
sequence_to_point_array (PyObject *seq, DiaPoint **points)
{
	gint len, i;
	GValue v = { 0, };
	DiaPoint *p = NULL;
	PyObject *sitem;
	
	len = PySequence_Length (seq);
	if (len < 0) {
		PyErr_SetString(PyExc_TypeError,
				"points should be a tuple of points");
		return -1;
	}

	p = g_new (DiaPoint, len);
	g_value_init (&v, DIA_TYPE_POINT);
	for (i = 0; i < len; i++) {
		sitem = PySequence_GetItem(seq, i);
		if (pyg_value_from_pyobject (&v, sitem)) {
			PyErr_SetString(PyExc_TypeError,
					"points should be a tuple of points");
			Py_XDECREF (sitem);
			g_free (p);
			return -1;
		}
		p[i] = *((DiaPoint*) g_value_get_boxed (&v));
		Py_DECREF(sitem);
		g_value_reset (&v);
	}
	g_value_unset (&v);

	*points = p;

	return len;
}

%%
import gtk.gdk.Drawable as PyGdkDrawable_Type
import gtk.gdk.Pixbuf as PyGdkPixbuf_Type
import pango.Layout as PyPangoLayout_Type
%%
ignore-glob
  *_get_type
  _*
  dia_shape_*_get_bounds
%%
override dia_shape_line kwargs
static PyObject *
_wrap_dia_shape_line(PyObject *self, PyObject *args, PyObject *kwargs)
{
	static char *kwlist[] = { "points", NULL };
	PyObject *py_points;
	DiaPoint *points = NULL;
	gint len;
	
	if (!PyArg_ParseTupleAndKeywords(args, kwargs,
					 "O:DiaShapePath.line",
					 kwlist, &py_points))
		return NULL;

	len = sequence_to_point_array (py_points, &points);
	if (len < 0)
		return NULL;

	dia_shape_polyline(pyg_boxed_get(self, DiaShape), len, points);

	g_free (points);

	Py_INCREF(Py_None);
	return Py_None;
}
%%
override dia_shape_polygon kwargs
static PyObject *
_wrap_dia_shape_polygon(PyObject *self, PyObject *args, PyObject *kwargs)
{
	static char *kwlist[] = { "points", NULL };
	PyObject *py_points;
	DiaPoint *points = NULL;
	gint len;
	
	if (!PyArg_ParseTupleAndKeywords(args, kwargs,
					 "O:DiaShapePath.line",
					 kwlist, &py_points))
		return NULL;

	len = sequence_to_point_array (py_points, &points);
	if (len < 0)
		return NULL;

	dia_shape_polygon (pyg_boxed_get(self, DiaShape), len, points);

	g_free (points);

	Py_INCREF(Py_None);
	return Py_None;
}
%%
override dia_shape_rectangle kwargs
static PyObject *
_wrap_dia_shape_rectangle(PyObject *self, PyObject *args, PyObject *kwargs)
{
	static char *kwlist[] = { "upper_left", "lower_right", NULL };
	PyObject *py_upper_left, *py_lower_right;
	GValue v_upper_left = { 0, }, v_lower_right = { 0, };
	DiaPoint *upper_left, *lower_right;

	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO:DiaShapePath.rectangle", kwlist, &py_upper_left, &py_lower_right))
		return NULL;

	g_value_init (&v_upper_left, DIA_TYPE_POINT);
	g_value_init (&v_lower_right, DIA_TYPE_POINT);

	if (pyg_value_from_pyobject (&v_upper_left, py_upper_left))
		return NULL;

	if (pyg_value_from_pyobject (&v_lower_right, py_lower_right))
		return NULL;

	upper_left = g_value_get_boxed (&v_upper_left);
	lower_right = g_value_get_boxed (&v_lower_right);

	dia_shape_rectangle(pyg_boxed_get(self, DiaShapePath), upper_left, lower_right);

	g_value_unset (&v_upper_left);
	g_value_unset (&v_lower_right);

	Py_INCREF(Py_None);
	return Py_None;
}
%%
override dia_shape_arc kwargs
static PyObject *
_wrap_dia_shape_arc(PyObject *self, PyObject *args, PyObject *kwargs)
{
	static char *kwlist[] = { "begin", "middle", "end", NULL };
	PyObject *py_begin, *py_middle, *py_end;
	GValue v_begin = { 0, }, v_middle = { 0, }, v_end = { 0, };
	DiaPoint *begin, *middle, *end;

	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOO:DiaShapePath.arc", kwlist, &py_begin, &py_middle, &py_end))
		return NULL;

	g_value_init (&v_begin, DIA_TYPE_POINT);
	g_value_init (&v_middle, DIA_TYPE_POINT);
	g_value_init (&v_end, DIA_TYPE_POINT);

	begin = g_value_get_boxed (&v_begin);
	middle = g_value_get_boxed (&v_middle);
	end = g_value_get_boxed (&v_end);

	dia_shape_arc(pyg_boxed_get(self, DiaShapePath), begin, middle, end);

	g_value_unset (&v_begin);
	g_value_unset (&v_middle);
	g_value_unset (&v_end);

	Py_INCREF(Py_None);
	return Py_None;
}
%%
override dia_shape_path_set_dash kwargs
static PyObject*
_wrap_dia_shape_path_set_dash (PyGObject *self, PyObject *args, PyObject *kwargs)
{
	static char *kwlist[] = { "offset", "dash", NULL };
	PyObject *py_dash;
	double offset;
	GValue vdash = { 0, };
	DiaDashStyle *dash;

	if (!PyArg_ParseTupleAndKeywords(args, kwargs,
				         "dO:DiaShapePath.set_dash",
					 kwlist, &offset, &py_dash))
		return NULL;

	g_value_init (&vdash, DIA_TYPE_DASH_STYLE);

	if (pyg_value_from_pyobject (&vdash, py_dash)) {
		return NULL;
	}

	dash = g_value_get_boxed (&vdash);

	dia_shape_path_set_dash (pyg_boxed_get (self, DiaShape),
				 offset, dash->n_dash, dash->dash);

	g_value_unset (&vdash);

	Py_INCREF(Py_None);
	return Py_None;
}
%%
override dia_shape_bezier kwargs
static PyObject *
_wrap_dia_shape_bezier(PyObject *self, PyObject *args, PyObject *kwargs)
{
	static char *kwlist[] = { "points", NULL };
	PyObject *py_points;
	int len;
	DiaPoint *points = NULL;

	if (!PyArg_ParseTupleAndKeywords (args, kwargs,
					  "O:DiaShapeBezier.bezier", kwlist,
					  &py_points))
		return NULL;

	if (!PySequence_Check (py_points)) {
		PyErr_SetString (PyExc_TypeError,
				 "points should be a sequence of points");
		return NULL;
	}

	len = PySequence_Size (py_points);
	if (len <= 0 && len % 3 != 1) {
		PyErr_SetString (PyExc_TypeError,
				 "the length of points should be a multiple "
				 "of 3 plus 1 (e.g. 4, 7)");
		return NULL;
	}

	len = sequence_to_point_array (py_points, &points);
	if (len < 0)
		return NULL;

	if (len > 0)
		dia_shape_bezier (pyg_boxed_get(self, DiaShapeBezier),
				  &points[0], len - 1, &points[1]);
	g_free (points);
	Py_INCREF (Py_None);
	return Py_None;
}
%%
override dia_shape_ellipse kwargs
static PyObject *
_wrap_dia_shape_ellipse(PyObject *self, PyObject *args, PyObject *kwargs)
{
	static char *kwlist[] = { "center", "width", "height", NULL };
	PyObject *py_center;
	GValue v_center = { 0, };
	DiaPoint *center;
	double width, height;

	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Odd:DiaShapeEllipse.ellipse", kwlist, &py_center, &width, &height))
		return NULL;

	g_value_init (&v_center, DIA_TYPE_POINT);

	if (pyg_value_from_pyobject (&v_center, py_center))
		return NULL;

	center = g_value_get_boxed (&v_center);

	dia_shape_ellipse(pyg_boxed_get(self, DiaShapeEllipse), center, width, height);

	g_value_unset (&v_center);

	Py_INCREF(Py_None);
	return Py_None;
}
%%
override dia_shape_text_cursor_from_pos kwargs
static PyObject*
_wrap_dia_shape_text_cursor_from_pos (PyGObject *self, PyObject *args, PyObject *kwargs)
{
	static char *kwlist[] = { "pos", NULL };
	PyObject *py_pos;
	gint cursor;
	gboolean in;
	GValue vpos = { 0, };
	DiaPoint *pos;

	if (!PyArg_ParseTupleAndKeywords(args, kwargs,
				         "O:DiaShapeText.cursor_from_pos",
					 kwlist, &py_pos))
		return NULL;

	g_value_init (&vpos, DIA_TYPE_POINT);

	if (pyg_value_from_pyobject (&vpos, py_pos)) {
		return NULL;
	}

	pos = g_value_get_boxed (&vpos);

	in = dia_shape_text_cursor_from_pos (pyg_boxed_get (self, DiaShape),
					     pos, &cursor);

	g_value_unset (&vpos);

	return Py_BuildValue ("(ii)", in, cursor);
}
%%
override dia_shape_text_set_affine kwargs
static PyObject*
_wrap_dia_shape_text_set_affine (PyGObject *self, PyObject *args, PyObject *kwargs)
{
	static char *kwlist[] = { "affine", NULL };
	PyObject *py_affine;
	GValue vaffine = { 0, };
	gdouble *affine;

	if (!PyArg_ParseTupleAndKeywords(args, kwargs,
				         "O:DiaShapeText.set_affine",
					 kwlist, &py_affine))
		return NULL;

	g_value_init (&vaffine, DIA_TYPE_AFFINE);

	if (pyg_value_from_pyobject (&vaffine, py_affine)) {
		return NULL;
	}

	affine = g_value_get_boxed (&vaffine);

	dia_shape_text_set_affine (pyg_boxed_get (self, DiaShape),
				   affine);

	g_value_unset (&vaffine);

	Py_INCREF (Py_None);
	return Py_None;
}
%%
override dia_shape_text_set_pos kwargs
static PyObject *
_wrap_dia_shape_text_set_pos(PyObject *self, PyObject *args, PyObject *kwargs)
{
	static char *kwlist[] = { "pos", NULL };
	PyObject *py_pos;
	GValue v_pos = { 0, };
	DiaPoint *pos;

	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:DiaShapeText.set_pos", kwlist, &py_pos))
		return NULL;

	g_value_init (&v_pos, DIA_TYPE_POINT);

	if (pyg_value_from_pyobject (&v_pos, py_pos))
		return NULL;

	pos = g_value_get_boxed (&v_pos);

	dia_shape_text_set_pos(pyg_boxed_get(self, DiaShapeText), pos);

	g_value_unset (&v_pos);

	Py_INCREF(Py_None);
	return Py_None;
}
%%
override dia_shape_image_set_affine kwargs
static PyObject*
_wrap_dia_shape_image_set_affine (PyGObject *self, PyObject *args, PyObject *kwargs)
{
	static char *kwlist[] = { "affine", NULL };
	PyObject *py_affine;
	GValue vaffine = { 0, };
	gdouble *affine;

	if (!PyArg_ParseTupleAndKeywords(args, kwargs,
				         "O:DiaShapeText.set_affine",
					 kwlist, &py_affine))
		return NULL;

	g_value_init (&vaffine, DIA_TYPE_AFFINE);

	if (pyg_value_from_pyobject (&vaffine, py_affine)) {
		return NULL;
	}

	affine = g_value_get_boxed (&vaffine);

	dia_shape_image_set_affine (pyg_boxed_get (self, DiaShape),
				   affine);

	g_value_unset (&vaffine);

	Py_INCREF (Py_None);
	return Py_None;
}
%%
override dia_shape_image_set_pos kwargs
static PyObject *
_wrap_dia_shape_image_set_pos(PyObject *self, PyObject *args, PyObject *kwargs)
{
	static char *kwlist[] = { "pos", NULL };
	PyObject *py_pos;
	GValue v_pos = { 0, };
	DiaPoint *pos;

	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:DiaShapeImage.set_pos", kwlist, &py_pos))
        return NULL;

	g_value_init (&v_pos, DIA_TYPE_POINT);

	if (pyg_value_from_pyobject (&v_pos, py_pos))
		return NULL;

	pos = g_value_get_boxed (&v_pos);

	dia_shape_image_set_pos(pyg_boxed_get(self, DiaShapeImage), pos);

	g_value_unset (&v_pos);

	Py_INCREF(Py_None);
	return Py_None;
}
