1/* -*- Mode: C; c-basic-offset: 4 -*-
2 * pygtk- Python bindings for the GTK toolkit.
3 * Copyright (C) 2006  John Finlay
4 *
5 *   atkrectangle.override: atk.Rectangle overrides
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20 * USA
21 */
22%%
23headers
24
25static gboolean
26pyatk_rectangle_from_pyobject(PyObject *object, AtkRectangle *rectangle)
27{
28    g_return_val_if_fail(rectangle != NULL, FALSE);
29
30    if (pyg_boxed_check(object, ATK_TYPE_RECTANGLE)) {
31        *rectangle = *pyg_boxed_get(object, AtkRectangle);
32        return TRUE;
33    }
34    if (PyArg_ParseTuple(object, "iiii", &rectangle->x, &rectangle->y,
35                                &rectangle->width, &rectangle->height)) {
36        return TRUE;
37    }
38    PyErr_Clear();
39    PyErr_SetString(PyExc_TypeError, "could not convert to AtkRectangle");
40    return FALSE;
41}
42
43static PyObject *
44PyAtkRectangle_from_value(const GValue *value)
45{
46    AtkRectangle *rect = (AtkRectangle *)g_value_get_boxed(value);
47
48    return pyg_boxed_new(ATK_TYPE_RECTANGLE, rect, TRUE, TRUE);
49}
50static int
51PyAtkRectangle_to_value(GValue *value, PyObject *object)
52{
53    AtkRectangle rect;
54
55    if (!pyatk_rectangle_from_pyobject(object, &rect))
56        return -1;
57
58    g_value_set_boxed(value, &rect);
59    return 0;
60}
61
62void
63_pyatk_register_boxed_types(void)
64{
65    pyg_register_boxed_custom(ATK_TYPE_RECTANGLE,
66                              PyAtkRectangle_from_value,
67                              PyAtkRectangle_to_value);
68}
69%%
70override atk_rectangle_new kwargs
71static int
72_wrap_atk_rectangle_new(PyGBoxed *self, PyObject *args, PyObject *kwargs)
73{
74    static char *kwlist[] = { "x", "y", "width", "height", NULL };
75    AtkRectangle rect = {0, 0, 0, 0};
76
77    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
78				     "|iiii:AtkRectangle.__init__",
79				     kwlist, &(rect.x), &(rect.y),
80				     &(rect.width), &(rect.height)))
81	return -1;
82
83    self->boxed =  g_boxed_copy(ATK_TYPE_RECTANGLE, &rect);
84    self->free_on_dealloc = TRUE;
85    self->gtype = ATK_TYPE_RECTANGLE;
86
87    return 0;
88}
89%%
90override-slot AtkRectangle.tp_as_sequence
91static Py_ssize_t
92_wrap_atk_rectangle_length(PyGBoxed *self)
93{
94    return 4;
95}
96static PyObject *
97_wrap_atk_rectangle_getitem(PyGBoxed *self, Py_ssize_t pos)
98{
99    AtkRectangle *rect;
100
101    if (pos < 0) pos += 4;
102    if (pos < 0 || pos >= 4) {
103        PyErr_SetString(PyExc_IndexError, "index out of range");
104        return NULL;
105    }
106    rect = pyg_boxed_get(self, AtkRectangle);
107    switch (pos) {
108    case 0: return PyInt_FromLong(rect->x);
109    case 1: return PyInt_FromLong(rect->y);
110    case 2: return PyInt_FromLong(rect->width);
111    case 3: return PyInt_FromLong(rect->height);
112    default:
113        g_assert_not_reached();
114        return NULL;
115    }
116}
117static int
118_wrap_atk_rectangle_setitem(PyGBoxed *self, Py_ssize_t pos, PyObject *value)
119{
120    AtkRectangle *rect;
121    gint val;
122
123    if (pos < 0) pos += 4;
124    if (pos < 0 || pos >= 4) {
125        PyErr_SetString(PyExc_IndexError, "index out of range");
126        return -1;
127    }
128    rect = pyg_boxed_get(self, AtkRectangle);
129    val = PyInt_AsLong(value);
130    if (PyErr_Occurred())
131        return -1;
132    switch(pos) {
133    case 0: rect->x      = val; break;
134    case 1: rect->y      = val; break;
135    case 2: rect->width  = val; break;
136    case 3: rect->height = val; break;
137    default:
138        g_assert_not_reached();
139        return -1;
140    }
141    return 0;
142}
143static PySequenceMethods _wrap_atk_rectangle_tp_as_sequence = {
144    (lenfunc)_wrap_atk_rectangle_length, /* sq_length */
145    0,                          /* sq_concat */
146    0,                          /* sq_repeat */
147    (ssizeargfunc)_wrap_atk_rectangle_getitem, /* sq_item */
148    0,                          /* sq_slice */
149    (ssizeobjargproc)_wrap_atk_rectangle_setitem, /* sq_ass_item */
150    0,                          /* sq_contains */
151    0,                          /* sq_inplace_concat */
152    0                           /* sq_inplace_repeat */
153};
154%%
155override-attr AtkRectangle.x
156static int
157_wrap_atk_rectangle__set_x(PyGBoxed *self, PyObject *value, void *closure)
158{
159    gint val;
160
161    val = PyInt_AsLong(value);
162    if (PyErr_Occurred())
163        return -1;
164    pyg_boxed_get(self, AtkRectangle)->x = val;
165    return 0;
166}
167%%
168override-attr AtkRectangle.y
169static int
170_wrap_atk_rectangle__set_y(PyGBoxed *self, PyObject *value, void *closure)
171{
172    gint val;
173
174    val = PyInt_AsLong(value);
175    if (PyErr_Occurred())
176        return -1;
177    pyg_boxed_get(self, AtkRectangle)->y = val;
178    return 0;
179}
180%%
181override-attr AtkRectangle.width
182static int
183_wrap_atk_rectangle__set_width(PyGBoxed *self, PyObject *value, void *closure)
184{
185    gint val;
186
187    val = PyInt_AsLong(value);
188    if (PyErr_Occurred())
189        return -1;
190    pyg_boxed_get(self, AtkRectangle)->width = val;
191    return 0;
192}
193%%
194override-attr AtkRectangle.height
195static int
196_wrap_atk_rectangle__set_height(PyGBoxed *self, PyObject *value, void *closure)
197{
198    gint val;
199
200    val = PyInt_AsLong(value);
201    if (PyErr_Occurred())
202        return -1;
203    pyg_boxed_get(self, AtkRectangle)->height = val;
204    return 0;
205}
206%%
207override atk_rectangle_intersect kwargs
208static PyObject *
209_wrap_atk_rectangle_intersect(PyGObject *self, PyObject *args,
210			      PyObject *kwargs)
211{
212    static char *kwlist[] = { "src", NULL };
213    PyObject *py_src;
214    AtkRectangle src, dest = {0, 0, 0, 0};
215
216    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:AtkRectangle.intersect",
217				     kwlist, &py_src)) {
218        return NULL;
219    }
220
221    if (!pyg_boxed_check(py_src, ATK_TYPE_RECTANGLE)) {
222	if (!pyatk_rectangle_from_pyobject(py_src, &src)) {
223	    PyErr_Clear();
224	    PyErr_SetString(PyExc_TypeError,
225			    "src must be a AtkRectangle or 4-tuple");
226	    return NULL;
227	}
228    } else {
229	src = *pyg_boxed_get(py_src, AtkRectangle);
230    }
231
232    atk_rectangle_intersect(pyg_boxed_get(self, AtkRectangle), &src, &dest);
233
234    return pyg_boxed_new(ATK_TYPE_RECTANGLE, &dest, TRUE, TRUE);
235}
236%%
237override atk_rectangle_union kwargs
238static PyObject *
239_wrap_atk_rectangle_union(PyGObject *self, PyObject *args,
240			  PyObject *kwargs)
241{
242    static char *kwlist[] = { "src", NULL };
243    PyObject *py_src;
244    AtkRectangle src, dest;
245
246    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:AtkRectangle.union",
247				     kwlist, &py_src)) {
248        return NULL;
249    }
250
251    if (!pyg_boxed_check(py_src, ATK_TYPE_RECTANGLE)) {
252	if (!pyatk_rectangle_from_pyobject(py_src, &src)) {
253	    PyErr_Clear();
254	    PyErr_SetString(PyExc_TypeError,
255			    "src must be a AtkRectangle or 4-tuple");
256	    return NULL;
257	}
258    } else {
259	src = *pyg_boxed_get(py_src, AtkRectangle);
260    }
261
262    atk_rectangle_union(pyg_boxed_get(self, AtkRectangle), &src, &dest);
263
264    return pyg_boxed_new(ATK_TYPE_RECTANGLE, &dest, TRUE, TRUE);
265}
266