Imported Upstream version 3.3.1
[platform/upstream/pygobject2.git] / gi / _glib / pygmaincontext.c
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * pygtk- Python bindings for the GTK toolkit.
3  * Copyright (C) 1998-2003  James Henstridge
4  *
5  *   pygmaincontext.c: GMainContext wrapper
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
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <Python.h>
28 #include <pythread.h>
29 #include <glib.h>
30 #include "pygmaincontext.h"
31 #include "pyglib.h"
32 #include "pyglib-private.h"
33
34 PYGLIB_DEFINE_TYPE("gi._glib.MainContext", PyGMainContext_Type, PyGMainContext)
35
36 /**
37  * pyg_main_context_new:
38  * @context: a GMainContext.
39  *
40  * Creates a wrapper for a GMainContext.
41  *
42  * Returns: the GMainContext wrapper.
43  */
44 PyObject *
45 pyg_main_context_new(GMainContext *context)
46 {
47     PyGMainContext *self;
48
49     self = (PyGMainContext *)PyObject_NEW(PyGMainContext, &PyGMainContext_Type);
50     if (self == NULL)
51         return NULL;
52
53     self->context = g_main_context_ref(context);
54
55     return (PyObject *)self;
56 }
57
58 static int
59 pyg_main_context_init(PyGMainContext *self)
60 {
61     self->context = g_main_context_new();
62     return 0;
63 }
64
65 static void
66 pyg_main_context_dealloc(PyGMainContext *self)
67 {
68     if (self->context != NULL) {
69         g_main_context_unref(self->context);
70         self->context = NULL;
71     }
72
73     PyObject_Del(self);
74 }
75
76 static PyObject*
77 pyg_main_context_richcompare(PyObject *self, PyObject *other, int op)
78 {
79     if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGMainContext_Type)
80         return _pyglib_generic_ptr_richcompare(((PyGMainContext*)self)->context,
81                                                ((PyGMainContext*)other)->context,
82                                                op);
83     else {
84         Py_INCREF(Py_NotImplemented);
85         return Py_NotImplemented;
86     }
87 }
88
89 static PyObject *
90 _wrap_g_main_context_iteration (PyGMainContext *self, PyObject *args)
91 {
92     gboolean ret, may_block = TRUE;
93     
94     if (!PyArg_ParseTuple(args, "|i:GMainContext.iteration",
95                           &may_block))
96         return NULL;
97
98     pyglib_begin_allow_threads;
99     ret = g_main_context_iteration(self->context, may_block);
100     pyglib_end_allow_threads;
101     
102     return PyBool_FromLong(ret);
103 }
104
105 static PyObject *
106 _wrap_g_main_context_pending (PyGMainContext *self)
107 {
108     return PyBool_FromLong(g_main_context_pending(self->context));
109 }
110
111 static PyMethodDef _PyGMainContext_methods[] = {
112     { "iteration", (PyCFunction)_wrap_g_main_context_iteration, METH_VARARGS },
113     { "pending", (PyCFunction)_wrap_g_main_context_pending, METH_NOARGS },
114     { NULL, NULL, 0 }
115 };
116
117 void
118 pyglib_maincontext_register_types(PyObject *d)
119 {
120     PyGMainContext_Type.tp_dealloc = (destructor)pyg_main_context_dealloc;
121     PyGMainContext_Type.tp_richcompare = pyg_main_context_richcompare;
122     PyGMainContext_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
123     PyGMainContext_Type.tp_methods = _PyGMainContext_methods;
124     PyGMainContext_Type.tp_init = (initproc)pyg_main_context_init;
125     PYGLIB_REGISTER_TYPE(d, PyGMainContext_Type, "MainContext"); 
126 }