e332a22e01109d4abfa4b9252b0dd423eb23bdfa
[platform/upstream/pygobject2.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 #else
30 #include <pycairo/py3cairo.h>
31 #endif
32
33 Pycairo_CAPI_t *Pycairo_CAPI;
34
35 #include "pygi-foreign.h"
36
37 #include <pyglib-python-compat.h>
38
39 PyObject *
40 cairo_context_to_arg (PyObject        *value,
41                       GIInterfaceInfo *iface_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 PyObject *
59 cairo_context_from_arg (GIInterfaceInfo *iface_info,
60                         gpointer data)
61 {
62     cairo_t *context = (cairo_t*) data;
63
64     cairo_reference (context);
65
66     return PycairoContext_FromContext (context, &PycairoContext_Type, NULL);
67 }
68
69 PyObject *
70 cairo_context_release (GIBaseInfo *base_info,
71                        gpointer    struct_)
72 {
73     cairo_destroy ( (cairo_t*) struct_);
74     Py_RETURN_NONE;
75 }
76
77
78 PyObject *
79 cairo_surface_to_arg (PyObject        *value,
80                       GIInterfaceInfo *iface_info,
81                       GITransfer       transfer,
82                       GIArgument      *arg)
83 {
84     cairo_surface_t *surface;
85
86     g_assert (transfer == GI_TRANSFER_NOTHING);
87
88     surface = ( (PycairoSurface*) value)->surface;
89     if (!surface) {
90         PyErr_SetString (PyExc_ValueError, "Surface instance wrapping a NULL surface");
91         return NULL;
92     }
93
94     arg->v_pointer = surface;
95     Py_RETURN_NONE;
96 }
97
98 PyObject *
99 cairo_surface_from_arg (GIInterfaceInfo *iface_info, 
100                         gpointer         data)
101 {
102     cairo_surface_t *surface = (cairo_surface_t*) data;
103
104     cairo_surface_reference (surface);
105
106     return PycairoSurface_FromSurface (surface, NULL);
107 }
108
109 PyObject *
110 cairo_surface_release (GIBaseInfo *base_info,
111                        gpointer    struct_)
112 {
113     cairo_surface_destroy ( (cairo_surface_t*) struct_);
114     Py_RETURN_NONE;
115 }
116
117 static PyMethodDef _gi_cairo_functions[] = {};
118 PYGLIB_MODULE_START(_gi_cairo, "_gi_cairo")
119 {
120     Pycairo_IMPORT;
121     if (Pycairo_CAPI == NULL)
122         return PYGLIB_MODULE_ERROR_RETURN;
123
124     pygi_register_foreign_struct ("cairo",
125                                   "Context",
126                                   cairo_context_to_arg,
127                                   cairo_context_from_arg,
128                                   cairo_context_release);
129
130     pygi_register_foreign_struct ("cairo",
131                                   "Surface",
132                                   cairo_surface_to_arg,
133                                   cairo_surface_from_arg,
134                                   cairo_surface_release);
135 }
136 PYGLIB_MODULE_END;