[pygobject] Add Cairo Pattern foreign struct.



commit 627dd81ac478ede4d29b242482e61e204ce2f896
Author: Renato Florentino Garcia <fgarcia renato gmail com>
Date:   Sun Feb 17 20:45:40 2019 -0300

    Add Cairo Pattern foreign struct.

 gi/pygi-foreign-cairo.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++
 tests/regressextra.c    | 52 ++++++++++++++++++++++++++++++++++++++++++++++++
 tests/regressextra.h    |  8 ++++++++
 tests/test_cairo.py     | 21 ++++++++++++++++++++
 4 files changed, 134 insertions(+)
---
diff --git a/gi/pygi-foreign-cairo.c b/gi/pygi-foreign-cairo.c
index c99847bf..efb62544 100644
--- a/gi/pygi-foreign-cairo.c
+++ b/gi/pygi-foreign-cairo.c
@@ -409,6 +409,53 @@ cairo_scaled_font_from_gvalue (const GValue *value)
  * cairo_pattern_t marshaling
  */
 
+static PyObject *
+cairo_pattern_to_arg (PyObject        *value,
+                      GIInterfaceInfo *interface_info,
+                      GITransfer       transfer,
+                      GIArgument      *arg)
+{
+    cairo_pattern_t *pattern;
+
+    if (!PyObject_TypeCheck (value, &PycairoPattern_Type)) {
+        PyErr_SetString (PyExc_TypeError, "Expected cairo.Pattern");
+        return NULL;
+    }
+
+    pattern = ((PycairoPattern*) value)->pattern;
+    if (!pattern) {
+        PyErr_SetString (PyExc_ValueError, "Pattern instance wrapping a NULL pattern");
+        return NULL;
+    }
+
+    if (transfer != GI_TRANSFER_NOTHING)
+        pattern = cairo_pattern_reference (pattern);
+
+    arg->v_pointer = pattern;
+    Py_RETURN_NONE;
+}
+
+static PyObject *
+cairo_pattern_from_arg (GIInterfaceInfo *interface_info,
+                        GITransfer       transfer,
+                        gpointer         data)
+{
+    cairo_pattern_t *pattern = (cairo_pattern_t*) data;
+
+    if (transfer == GI_TRANSFER_NOTHING)
+        pattern = cairo_pattern_reference (pattern);
+
+    return PycairoPattern_FromPattern (pattern, NULL);
+}
+
+static PyObject *
+cairo_pattern_release (GIBaseInfo *base_info,
+                       gpointer    struct_)
+{
+    cairo_pattern_destroy ( (cairo_pattern_t*) struct_);
+    Py_RETURN_NONE;
+}
+
 static int
 cairo_pattern_to_gvalue (GValue *value, PyObject *obj)
 {
@@ -585,6 +632,12 @@ PYGLIB_MODULE_START(_gi_cairo, "_gi_cairo")
                                   cairo_font_options_from_arg,
                                   cairo_font_options_release);
 
+    pygi_register_foreign_struct ("cairo",
+                                  "Pattern",
+                                  cairo_pattern_to_arg,
+                                  cairo_pattern_from_arg,
+                                  cairo_pattern_release);
+
     pygi_register_foreign_struct ("cairo",
                                   "Region",
                                   cairo_region_to_arg,
diff --git a/tests/regressextra.c b/tests/regressextra.c
index a1426782..d6af90bf 100644
--- a/tests/regressextra.c
+++ b/tests/regressextra.c
@@ -173,6 +173,58 @@ regress_test_cairo_path_full_in_full_return (cairo_path_t *path)
     return path;
 }
 
+/**
+ * regress_test_cairo_pattern_full_in:
+ * @pattern: (transfer full):
+ */
+void
+regress_test_cairo_pattern_full_in (cairo_pattern_t *pattern)
+{
+    cairo_pattern_destroy (pattern);
+}
+
+/**
+ * regress_test_cairo_pattern_none_in:
+ * @pattern: (transfer none):
+ */
+void
+regress_test_cairo_pattern_none_in (cairo_pattern_t *pattern)
+{
+    cairo_t *cr = regress_test_cairo_context_full_return ();
+    cairo_set_source (cr, pattern);
+    g_assert (cairo_status (cr) == CAIRO_STATUS_SUCCESS);
+    cairo_destroy (cr);
+}
+
+/**
+ * regress_test_cairo_pattern_none_return:
+ *
+ * Returns: (transfer none):
+ */
+cairo_pattern_t*
+regress_test_cairo_pattern_none_return (void)
+{
+    static cairo_pattern_t *pattern;
+
+    if (pattern == NULL) {
+        pattern = cairo_pattern_create_rgb(0.1, 0.2, 0.3);
+    }
+
+    return pattern;
+}
+
+/**
+ * regress_test_cairo_pattern_full_return:
+ *
+ * Returns: (transfer full):
+ */
+cairo_pattern_t *
+regress_test_cairo_pattern_full_return (void)
+{
+    cairo_pattern_t *pattern = cairo_pattern_create_rgb(0.5, 0.6, 0.7);
+    return pattern;
+}
+
 /**
  * regress_test_cairo_region_full_in:
  * @region: (transfer full):
diff --git a/tests/regressextra.h b/tests/regressextra.h
index 6f5d2f56..0b40ab04 100644
--- a/tests/regressextra.h
+++ b/tests/regressextra.h
@@ -34,6 +34,14 @@ void regress_test_cairo_path_none_in (cairo_path_t *path);
 _GI_TEST_EXTERN
 cairo_path_t * regress_test_cairo_path_full_in_full_return (cairo_path_t *path);
 _GI_TEST_EXTERN
+void regress_test_cairo_pattern_full_in (cairo_pattern_t *pattern);
+_GI_TEST_EXTERN
+void regress_test_cairo_pattern_none_in (cairo_pattern_t *pattern);
+_GI_TEST_EXTERN
+cairo_pattern_t* regress_test_cairo_pattern_none_return (void);
+_GI_TEST_EXTERN
+cairo_pattern_t * regress_test_cairo_pattern_full_return (void);
+_GI_TEST_EXTERN
 cairo_font_options_t *regress_test_cairo_font_options_full_return (void);
 _GI_TEST_EXTERN
 cairo_font_options_t *regress_test_cairo_font_options_none_return (void);
diff --git a/tests/test_cairo.py b/tests/test_cairo.py
index f4d0d7c9..d03e85df 100644
--- a/tests/test_cairo.py
+++ b/tests/test_cairo.py
@@ -115,6 +115,27 @@ class Test(unittest.TestCase):
         options = cairo.FontOptions()
         Regress.test_cairo_font_options_none_in(options)
 
+    def test_cairo_pattern_full_in(self):
+        pattern = cairo.SolidPattern(1, 1, 1, 1)
+        Regress.test_cairo_pattern_full_in(pattern)
+
+        with pytest.raises(TypeError):
+            Regress.test_cairo_pattern_full_in(object())
+
+    def test_cairo_pattern_none_in(self):
+        pattern = cairo.SolidPattern(1, 1, 1, 1)
+        Regress.test_cairo_pattern_none_in(pattern)
+
+    def test_cairo_pattern_full_return(self):
+        pattern = Regress.test_cairo_pattern_full_return()
+        self.assertTrue(isinstance(pattern, cairo.Pattern))
+        self.assertTrue(isinstance(pattern, cairo.SolidPattern))
+
+    def test_cairo_pattern_none_return(self):
+        pattern = Regress.test_cairo_pattern_none_return()
+        self.assertTrue(isinstance(pattern, cairo.Pattern))
+        self.assertTrue(isinstance(pattern, cairo.SolidPattern))
+
     def test_cairo_region_full_in(self):
         region = cairo.Region()
         Regress.test_cairo_region_full_in(region)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]