File: pydia-display.c

package info (click to toggle)
dia 0.97.3%2Bgit20160930-9
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 54,372 kB
  • sloc: ansic: 155,065; xml: 16,326; python: 6,641; cpp: 4,935; makefile: 3,833; sh: 540; perl: 137; sed: 19
file content (296 lines) | stat: -rw-r--r-- 8,947 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* -*- Mode: C; c-basic-offset: 4 -*- */
/* Python plug-in for dia
 * Copyright (C) 1999  James Henstridge
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <config.h>

#include "pydia-display.h"
#include "pydia-diagram.h"
#include "pydia-object.h" /* for PyObject_HEAD_INIT */

#include <structmember.h> /* PyMemberDef */

PyObject *
PyDiaDisplay_New(DDisplay *disp)
{
    PyDiaDisplay *self;

    self = PyObject_NEW(PyDiaDisplay, &PyDiaDisplay_Type);

    if (!self) return NULL;
    self->disp = disp;
    return (PyObject *)self;
}

static void
PyDiaDisplay_Dealloc(PyDiaDisplay *self)
{
     PyObject_DEL(self);
}

static int
PyDiaDisplay_Compare(PyDiaDisplay *self, PyDiaDisplay *other)
{
    if (self->disp == other->disp) return 0;
    if (self->disp > other->disp) return -1;
    return 1;
}

static long
PyDiaDisplay_Hash(PyDiaDisplay *self)
{
    return (long)self->disp;
}

static PyObject *
PyDiaDisplay_Str(PyDiaDisplay *self)
{
    return PyString_FromString(self->disp->diagram->filename);
}

/* methods here */

static PyObject *
PyDiaDisplay_AddUpdateAll(PyDiaDisplay *self, PyObject *args)
{
    if (!PyArg_ParseTuple(args, ":Display.add_update_all"))
	return NULL;
    ddisplay_add_update_all(self->disp);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
PyDiaDisplay_Flush(PyDiaDisplay *self, PyObject *args)
{
    if (!PyArg_ParseTuple(args, ":Display.flush"))
	return NULL;
    ddisplay_flush(self->disp);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
PyDiaDisplay_SetOrigion(PyDiaDisplay *self, PyObject *args)
{
    double x, y;

    if (!PyArg_ParseTuple(args, "dd:Display.set_origion", &x, &y))
	return NULL;
    ddisplay_set_origo(self->disp, x, y);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
PyDiaDisplay_Zoom(PyDiaDisplay *self, PyObject *args)
{
    Point p;
    double zoom;

    if (!PyArg_ParseTuple(args, "(dd)d:Display.zoom", &p.x, &p.y, &zoom))
	return NULL;
    ddisplay_zoom(self->disp, &p, zoom);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
PyDiaDisplay_ResizeCanvas(PyDiaDisplay *self, PyObject *args)
{
    int width, height;

    if (!PyArg_ParseTuple(args, "ii:Display.resize_canvas", &width,&height))
	return NULL;
    ddisplay_resize_canvas(self->disp, width, height);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
PyDiaDisplay_Close(PyDiaDisplay *self, PyObject *args)
{
    if (!PyArg_ParseTuple(args, ":Display.close"))
	return NULL;
    ddisplay_close(self->disp);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
PyDiaDisplay_SetTitle(PyDiaDisplay *self, PyObject *args)
{
    gchar *title;

    if (!PyArg_ParseTuple(args, "s:Display.set_title", &title))
	return NULL;
    ddisplay_set_title(self->disp, title);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
PyDiaDisplay_Scroll(PyDiaDisplay *self, PyObject *args)
{
    Point delta;

    if (!PyArg_ParseTuple(args, "dd:Display.scroll", &delta.x, &delta.y))
	return NULL;
    ddisplay_scroll(self->disp, &delta);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
PyDiaDisplay_ScrollUp(PyDiaDisplay *self, PyObject *args)
{
    if (!PyArg_ParseTuple(args, ":Display.scroll_up"))
	return NULL;
    ddisplay_scroll_up(self->disp);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
PyDiaDisplay_ScrollDown(PyDiaDisplay *self, PyObject *args)
{
    if (!PyArg_ParseTuple(args, ":Display.scroll_down"))
	return NULL;
    ddisplay_scroll_down(self->disp);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
PyDiaDisplay_ScrollLeft(PyDiaDisplay *self, PyObject *args)
{
    if (!PyArg_ParseTuple(args, ":Display.scroll_left"))
	return NULL;
    ddisplay_scroll_left(self->disp);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
PyDiaDisplay_ScrollRight(PyDiaDisplay *self, PyObject *args)
{
    if (!PyArg_ParseTuple(args, ":Display.scroll_right"))
	return NULL;
    ddisplay_scroll_right(self->disp);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyMethodDef PyDiaDisplay_Methods[] = {
    {"add_update_all", (PyCFunction)PyDiaDisplay_AddUpdateAll, METH_VARARGS,
     "add_update_all() -> None.  Add the diagram visible area to the update queue."},
    {"flush", (PyCFunction)PyDiaDisplay_Flush, METH_VARARGS,
     "flush() -> None.  If no display update is queued, queue update."},
    {"set_origion", (PyCFunction)PyDiaDisplay_SetOrigion, METH_VARARGS,
     "set_origion(real: x, real, y) -> None."
     "  Move the given diagram point to the top-left corner of the visible area."},
    {"zoom", (PyCFunction)PyDiaDisplay_Zoom, METH_VARARGS,
     "zoom(real[2]: point, real: factor) -> None.  Zoom around the given point."},
    {"resize_canvas", (PyCFunction)PyDiaDisplay_ResizeCanvas, METH_VARARGS,
     "resize_canvas(int: width, int: height) -> None."
     "  BEWARE: Changing the drawing area but not the window size."},
    {"close", (PyCFunction)PyDiaDisplay_Close, METH_VARARGS,
     "close() -> None.  Close the display possibly asking to save."},
    {"set_title", (PyCFunction)PyDiaDisplay_SetTitle, METH_VARARGS,
     "set_title(string: title) -> None.  Temporary change of the diagram title."},
    {"scroll", (PyCFunction)PyDiaDisplay_Scroll, METH_VARARGS,
     "scroll(real: dx, real: dy) -> None.  Scroll the visible area by the given delta."},
    {"scroll_up", (PyCFunction)PyDiaDisplay_ScrollUp, METH_VARARGS,
     "scroll_up() -> None. Scroll upwards by a fixed amount."},
    {"scroll_down", (PyCFunction)PyDiaDisplay_ScrollDown, METH_VARARGS,
     "scroll_down() -> None. Scroll downwards by a fixed amount."},
    {"scroll_left", (PyCFunction)PyDiaDisplay_ScrollLeft, METH_VARARGS,
     "scroll_left() -> None. Scroll to the left by a fixed amount."},
    {"scroll_right", (PyCFunction)PyDiaDisplay_ScrollRight, METH_VARARGS,
     "scroll_right() -> None. Scroll to the right by a fixed amount."},
    {NULL, 0, 0, NULL}
};

#define T_INVALID -1 /* can't allow direct access due to pyobject->data indirection */
static PyMemberDef PyDiaDisplay_Members[] = {
    { "diagram", T_INVALID, 0, RESTRICTED|READONLY, /* can't allow direct access due to pyobject->disp indirection */
      "Diagram displayed." },
    { "origin", T_INVALID, 0, RESTRICTED|READONLY,
      "Point in diagram coordinates of the top-left corner of the visible area."},
    { "zoom_factor", T_INVALID, 0, RESTRICTED|READONLY,
      "Real: zoom-factor"},
    { "visible", T_INVALID, 0, RESTRICTED|READONLY,
      "Tuple(real: top, real: left, real: bottom, real: right) : visible area"},
    { NULL }
};

static PyObject *
PyDiaDisplay_GetAttr(PyDiaDisplay *self, gchar *attr)
{
    if (!strcmp(attr, "__members__"))
	return Py_BuildValue("[ssss]", "diagram", "origin", "visible",
			     "zoom_factor");
    else if (!strcmp(attr, "diagram"))
	return PyDiaDiagram_New(self->disp->diagram);
    /* FIXME: shouldn't it have only one name */
    else if (!strcmp(attr, "origo") || !strcmp(attr, "origion") || !strcmp(attr, "origin"))
	return Py_BuildValue("(dd)", self->disp->origo.x, self->disp->origo.y);
    else if (!strcmp(attr, "zoom_factor"))
	return PyFloat_FromDouble(self->disp->zoom_factor);
    else if (!strcmp(attr, "visible"))
	return Py_BuildValue("(dddd)", self->disp->visible.top,
			     self->disp->visible.left,
			     self->disp->visible.bottom,
			     self->disp->visible.right);

    return Py_FindMethod(PyDiaDisplay_Methods, (PyObject *)self, attr);
}

PyTypeObject PyDiaDisplay_Type = {
    PyObject_HEAD_INIT(&PyType_Type)
    0,
    "dia.Display",
    sizeof(PyDiaDisplay),
    0,
    (destructor)PyDiaDisplay_Dealloc,
    (printfunc)0,
    (getattrfunc)PyDiaDisplay_GetAttr,
    (setattrfunc)0,
    (cmpfunc)PyDiaDisplay_Compare,
    (reprfunc)0,
    0,
    0,
    0,
    (hashfunc)PyDiaDisplay_Hash,
    (ternaryfunc)0,
    (reprfunc)PyDiaDisplay_Str,
    (getattrofunc)0,
    (setattrofunc)0,
    (PyBufferProcs *)0,
    0L, /* Flags */
    "A Diagram can have multiple displays but every Display has just one Diagram.",
    (traverseproc)0,
    (inquiry)0,
    (richcmpfunc)0,
    0, /* tp_weakliszoffset */
    (getiterfunc)0,
    (iternextfunc)0,
    PyDiaDisplay_Methods, /* tp_methods */
    PyDiaDisplay_Members, /* tp_members */
    0
};