~ubuntu-branches/ubuntu/hoary/gimp/hoary

« back to all changes in this revision

Viewing changes to plug-ins/pygimp/procbrowser.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-04-04 14:51:23 UTC
  • Revision ID: james.westby@ubuntu.com-20050404145123-9py049eeelfymur8
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; c-basic-offset: 4 -*-
 
2
    Gimp-Python - allows the writing of Gimp plugins in Python.
 
3
    Copyright (C) 2004  Manish Singh <yosh@gimp.org>
 
4
 
 
5
    This program is free software; you can redistribute it and/or modify
 
6
    it under the terms of the GNU General Public License as published by
 
7
    the Free Software Foundation; either version 2 of the License, or
 
8
    (at your option) any later version.
 
9
 
 
10
    This program is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with this program; if not, write to the Free Software
 
17
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
18
    02111-1307, USA.
 
19
 */
 
20
 
 
21
#include <Python.h>
 
22
#include <structseq.h>
 
23
 
 
24
#include <gtk/gtk.h>
 
25
 
 
26
#include <pygobject.h>
 
27
#include <pygtk/pygtk.h>
 
28
 
 
29
#include <libgimp/gimp.h>
 
30
 
 
31
#include <plug-ins/dbbrowser/gimpprocbrowser.h>
 
32
 
 
33
 
 
34
typedef PyObject *(*PyGimpPDBFunctionNew)(const char *name, const char *blurb,
 
35
                                          const char *help, const char *author,
 
36
                                          const char *copyright,
 
37
                                          const char *date,
 
38
                                          GimpPDBProcType proc_type,
 
39
                                          int n_params, int n_return_vals,
 
40
                                          GimpParamDef *params,
 
41
                                          GimpParamDef *return_vals);
 
42
 
 
43
typedef struct
 
44
{
 
45
    PyObject *func;
 
46
    PyObject *data;
 
47
} ProxyData;
 
48
 
 
49
 
 
50
static PyTypeObject *PyGimpPDBFunction_Type;
 
51
static PyGimpPDBFunctionNew pygimp_pdb_function_new;
 
52
 
 
53
 
 
54
static GimpParamDef *
 
55
copy_paramdefs(const GimpParamDef *paramdefs, gint n_params)
 
56
{
 
57
    GimpParamDef *copy;
 
58
 
 
59
    copy = g_new(GimpParamDef, n_params);
 
60
 
 
61
    while (n_params--)
 
62
      {
 
63
        copy[n_params].type = paramdefs[n_params].type;
 
64
        copy[n_params].name = g_strdup(paramdefs[n_params].name);
 
65
        copy[n_params].description = g_strdup(paramdefs[n_params].description);
 
66
      }
 
67
 
 
68
    return copy;
 
69
}
 
70
            
 
71
static void
 
72
proxy_apply_callback(const gchar        *name,
 
73
                     const gchar        *blurb,
 
74
                     const gchar        *help,
 
75
                     const gchar        *author,
 
76
                     const gchar        *copyright,
 
77
                     const gchar        *date,
 
78
                     GimpPDBProcType     proc_type,
 
79
                     gint                n_params,
 
80
                     gint                n_return_vals,
 
81
                     const GimpParamDef *params,
 
82
                     const GimpParamDef *return_vals,
 
83
                     gpointer            user_data)
 
84
{
 
85
    ProxyData *proxy_data = user_data;
 
86
    GimpParamDef *params_copy, *return_vals_copy;
 
87
    PyObject *pdb_func, *ret;
 
88
 
 
89
    params_copy      = copy_paramdefs(params, n_params);
 
90
    return_vals_copy = copy_paramdefs(return_vals, n_return_vals);
 
91
 
 
92
    pdb_func = pygimp_pdb_function_new(name, blurb, help, author, copyright,
 
93
                                       date, proc_type, n_params, n_return_vals,
 
94
                                       params_copy, return_vals_copy);
 
95
 
 
96
    if (pdb_func == NULL) {
 
97
        PyErr_Print();
 
98
        return;
 
99
    }
 
100
 
 
101
    if (proxy_data->data)
 
102
        ret = PyObject_CallFunctionObjArgs(proxy_data->func, pdb_func,
 
103
                                           proxy_data->data, NULL);
 
104
    else
 
105
        ret = PyObject_CallFunctionObjArgs(proxy_data->func, pdb_func, NULL);
 
106
 
 
107
    if (ret)
 
108
        Py_DECREF(ret);
 
109
    else
 
110
        PyErr_Print();
 
111
 
 
112
    Py_DECREF(pdb_func);
 
113
}
 
114
 
 
115
static void
 
116
proxy_cleanup(gpointer data, GObject *obj)
 
117
{
 
118
  ProxyData *proxy_data = data;
 
119
 
 
120
  Py_DECREF(proxy_data->func);
 
121
  Py_XDECREF(proxy_data->data);
 
122
 
 
123
  g_free(proxy_data);
 
124
}
 
125
 
 
126
static PyObject *
 
127
proc_browser_dialog_new(PyObject *self, PyObject *args, PyObject *kwargs)
 
128
{
 
129
    PyObject *py_func = Py_None, *py_data = Py_None;
 
130
    GimpProcBrowserApplyCallback proxy_func = NULL;
 
131
    ProxyData *proxy_data = NULL;
 
132
    GObject *dlg;
 
133
 
 
134
    static char *kwlist[] = { "apply_callback", "data", NULL };
 
135
 
 
136
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO:dialog_new", kwlist,
 
137
                                     &py_func, &py_data))
 
138
        return NULL;
 
139
 
 
140
    if (py_func != Py_None) {
 
141
        if (PyCallable_Check(py_func))
 
142
            proxy_func = proxy_apply_callback;
 
143
        else {
 
144
            PyErr_SetString(PyExc_TypeError,
 
145
                            "apply_callback must be a callable object or None");
 
146
            return NULL;
 
147
        }
 
148
 
 
149
        proxy_data = g_new0(ProxyData, 1);
 
150
 
 
151
        proxy_data->func = py_func;
 
152
        Py_INCREF(py_func);
 
153
 
 
154
        if (py_data != Py_None) {
 
155
            proxy_data->data = py_data;
 
156
            Py_INCREF(py_data);
 
157
        }
 
158
    }
 
159
 
 
160
    dlg = G_OBJECT(gimp_proc_browser_dialog_new(FALSE, proxy_func, proxy_data));
 
161
 
 
162
    if (proxy_data)
 
163
      g_object_weak_ref(dlg, proxy_cleanup, proxy_data);
 
164
 
 
165
    return pygobject_new(dlg);
 
166
}
 
167
 
 
168
/* List of methods defined in the module */
 
169
 
 
170
static struct PyMethodDef procbrowser_methods[] = {
 
171
    {"dialog_new",      (PyCFunction)proc_browser_dialog_new,   METH_VARARGS | METH_KEYWORDS},
 
172
    {NULL,       (PyCFunction)NULL, 0, NULL}            /* sentinel */
 
173
};
 
174
 
 
175
 
 
176
/* Initialization function for the module (*must* be called initgimpprocbrowser) */
 
177
 
 
178
static char procbrowser_doc[] = 
 
179
"This module provides a simple interface for the GIMP PDB Browser"
 
180
;
 
181
 
 
182
void
 
183
initgimpprocbrowser(void)
 
184
{
 
185
    PyObject *m, *pygimp;
 
186
 
 
187
    init_pygobject();
 
188
    init_pygtk();
 
189
 
 
190
    pygimp = PyImport_ImportModule("gimp");
 
191
 
 
192
    if (pygimp) {
 
193
        PyObject *module_dict = PyModule_GetDict(pygimp);
 
194
        PyObject *type_object, *c_object;
 
195
 
 
196
        type_object = PyDict_GetItemString(module_dict, "_PDBFunction");
 
197
        c_object    = PyDict_GetItemString(module_dict, "_pdb_function_new");
 
198
 
 
199
        if (PyType_Check(type_object) && PyCObject_Check(c_object)) {
 
200
            PyGimpPDBFunction_Type = (PyTypeObject*)type_object;
 
201
            pygimp_pdb_function_new = PyCObject_AsVoidPtr(c_object);
 
202
        } else {
 
203
            PyErr_SetString(PyExc_RuntimeError,
 
204
                            "could not find compatible gimp module");
 
205
            return;
 
206
        }
 
207
    } else {
 
208
        PyErr_SetString(PyExc_ImportError, "could not import gimp");
 
209
        return;
 
210
    }
 
211
 
 
212
    /* Create the module and add the functions */
 
213
    m = Py_InitModule3("gimpprocbrowser",
 
214
                       procbrowser_methods, procbrowser_doc);
 
215
 
 
216
    /* Check for errors */
 
217
    if (PyErr_Occurred())
 
218
        Py_FatalError("can't initialize module gimpprocbrowser");
 
219
}