Imported Upstream version 3.7.3
[platform/upstream/python-gobject.git] / gi / pygi-foreign-cairo.c
1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 /*
3  * Copyright (c) 2010  Collabora Ltd. <http://www.collabora.co.uk/>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to
7  * deal in the Software without restriction, including without limitation the
8  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9  * sell copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23
24 #include <cairo.h>
25 #include <Python.h>
26
27 #if PY_VERSION_HEX < 0x03000000
28 #include <pycairo.h>
29 static Pycairo_CAPI_t *Pycairo_CAPI;
30 #else
31 #include <pycairo/py3cairo.h>
32 #endif
33
34
35 #include "pygi-foreign.h"
36
37 #include <pyglib-python-compat.h>
38
39 static PyObject *
40 cairo_context_to_arg (PyObject        *value,
41                       GIInterfaceInfo *interface_info,
42                       GITransfer       transfer,
43                       GIArgument      *arg)
44 {
45     cairo_t *cr;
46
47     g_assert (transfer == GI_TRANSFER_NOTHING);
48
49     cr = PycairoContext_GET (value);
50     if (!cr) {
51         return NULL;
52     }
53
54     arg->v_pointer = cr;
55     Py_RETURN_NONE;
56 }
57
58 static PyObject *
59 cairo_context_from_arg (GIInterfaceInfo *interface_info, gpointer data)
60 {
61     cairo_t *context = (cairo_t*) data;
62
63     cairo_reference (context);
64
65     return PycairoContext_FromContext (context, &PycairoContext_Type, NULL);
66 }
67
68 static PyObject *
69 cairo_context_release (GIBaseInfo *base_info,
70                        gpointer    struct_)
71 {
72     cairo_destroy ( (cairo_t*) struct_);
73     Py_RETURN_NONE;
74 }
75
76
77 static PyObject *
78 cairo_surface_to_arg (PyObject        *value,
79                       GIInterfaceInfo *interface_info,
80                       GITransfer       transfer,
81                       GIArgument      *arg)
82 {
83     cairo_surface_t *surface;
84
85     g_assert (transfer == GI_TRANSFER_NOTHING);
86
87     surface = ( (PycairoSurface*) value)->surface;
88     if (!surface) {
89         PyErr_SetString (PyExc_ValueError, "Surface instance wrapping a NULL surface");
90         return NULL;
91     }
92
93     arg->v_pointer = surface;
94     Py_RETURN_NONE;
95 }
96
97 static PyObject *
98 cairo_surface_from_arg (GIInterfaceInfo *interface_info, gpointer data)
99 {
100     cairo_surface_t *surface = (cairo_surface_t*) data;
101
102     cairo_surface_reference (surface);
103
104     return PycairoSurface_FromSurface (surface, NULL);
105 }
106
107 static PyObject *
108 cairo_surface_release (GIBaseInfo *base_info,
109                        gpointer    struct_)
110 {
111     cairo_surface_destroy ( (cairo_surface_t*) struct_);
112     Py_RETURN_NONE;
113 }
114
115
116 static PyObject *
117 cairo_path_to_arg (PyObject        *value,
118                    GIInterfaceInfo *interface_info,
119                    GITransfer       transfer,
120                    GIArgument      *arg)
121 {
122     cairo_path_t *path;
123
124     g_assert (transfer == GI_TRANSFER_NOTHING);
125
126     path = ( (PycairoPath*) value)->path;
127     if (!path) {
128         PyErr_SetString (PyExc_ValueError, "Path instance wrapping a NULL path");
129         return NULL;
130     }
131
132     arg->v_pointer = path;
133     Py_RETURN_NONE;
134 }
135
136 static PyObject *
137 cairo_path_from_arg (GIInterfaceInfo *interface_info, gpointer data)
138 {
139     cairo_path_t *path = (cairo_path_t*) data;
140
141     return PycairoPath_FromPath (path);
142 }
143
144 static PyObject *
145 cairo_path_release (GIBaseInfo *base_info,
146                     gpointer    struct_)
147 {
148     cairo_path_destroy ( (cairo_path_t*) struct_);
149     Py_RETURN_NONE;
150 }
151
152 static PyObject *
153 cairo_font_options_to_arg (PyObject        *value,
154                            GIInterfaceInfo *interface_info,
155                            GITransfer       transfer,
156                            GIArgument      *arg)
157 {
158     cairo_font_options_t *font_options;
159
160     g_assert (transfer == GI_TRANSFER_NOTHING);
161
162     font_options = ( (PycairoFontOptions*) value)->font_options;
163     if (!font_options) {
164         PyErr_SetString (PyExc_ValueError, "FontOptions instance wrapping a NULL font_options");
165         return NULL;
166     }
167
168     arg->v_pointer = font_options;
169     Py_RETURN_NONE;
170 }
171
172 static PyObject *
173 cairo_font_options_from_arg (GIInterfaceInfo *interface_info, gpointer data)
174 {
175     cairo_font_options_t *font_options = (cairo_font_options_t*) data;
176
177     return PycairoFontOptions_FromFontOptions (cairo_font_options_copy (font_options));
178 }
179
180 static PyObject *
181 cairo_font_options_release (GIBaseInfo *base_info,
182                             gpointer    struct_)
183 {
184     cairo_font_options_destroy ( (cairo_font_options_t*) struct_);
185     Py_RETURN_NONE;
186 }
187
188 static PyMethodDef _gi_cairo_functions[] = { {0,} };
189 PYGLIB_MODULE_START(_gi_cairo, "_gi_cairo")
190 {
191 #if PY_VERSION_HEX < 0x03000000
192     Pycairo_IMPORT;
193 #else
194     import_cairo();
195 #endif
196
197     if (Pycairo_CAPI == NULL)
198         return PYGLIB_MODULE_ERROR_RETURN;
199
200     pygi_register_foreign_struct ("cairo",
201                                   "Context",
202                                   cairo_context_to_arg,
203                                   cairo_context_from_arg,
204                                   cairo_context_release);
205
206     pygi_register_foreign_struct ("cairo",
207                                   "Surface",
208                                   cairo_surface_to_arg,
209                                   cairo_surface_from_arg,
210                                   cairo_surface_release);
211
212     pygi_register_foreign_struct ("cairo",
213                                   "Path",
214                                   cairo_path_to_arg,
215                                   cairo_path_from_arg,
216                                   cairo_path_release);
217
218     pygi_register_foreign_struct ("cairo",
219                                   "FontOptions",
220                                   cairo_font_options_to_arg,
221                                   cairo_font_options_from_arg,
222                                   cairo_font_options_release);
223 }
224 PYGLIB_MODULE_END;