update to build with python 3.0.1 which removed Py_InitModule3, added richcompare...
[blender-staging.git] / source / blender / python / intern / bpy_ui.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * Contributor(s): Campbell Barton
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 #include "bpy_ui.h"
26 #include "bpy_compat.h"
27
28 #include "BLI_dynstr.h"
29
30 #include "MEM_guardedalloc.h"
31 #include "BKE_global.h" /* evil G.* */
32 #include "BKE_context.h"
33
34 #include "DNA_screen_types.h"
35 #include "UI_interface.h"
36 #include "WM_api.h"
37
38 static PyObject *Method_pupMenuBegin( PyObject * self, PyObject * args )
39 {
40         char *title; int icon;
41         
42         if( !PyArg_ParseTuple( args, "si:pupMenuBegin", &title, &icon))
43                 return NULL;
44         
45         return PyCObject_FromVoidPtr( uiPupMenuBegin(title, icon), NULL );
46 }
47
48 static PyObject *Method_pupMenuEnd( PyObject * self, PyObject * args )
49 {
50         PyObject *py_context, *py_head;
51         
52         if( !PyArg_ParseTuple( args, "O!O!:pupMenuEnd", &PyCObject_Type, &py_context, &PyCObject_Type, &py_head))
53                 return NULL;
54         
55         uiPupMenuEnd(PyCObject_AsVoidPtr(py_context), PyCObject_AsVoidPtr(py_head));
56         
57         Py_RETURN_NONE;
58 }
59
60 static PyObject *Method_menuItemO( PyObject * self, PyObject * args )
61 {
62         PyObject *py_head;
63         char *opname;
64         int icon;
65         
66         if( !PyArg_ParseTuple( args, "O!is:menuItemO", &PyCObject_Type, &py_head, &icon, &opname))
67                 return NULL;
68         
69         uiMenuItemO(PyCObject_AsVoidPtr(py_head), icon, opname);
70         
71         Py_RETURN_NONE;
72 }
73
74 static PyObject *Method_defButO( PyObject * self, PyObject * args )
75 {
76         PyObject *py_block;
77         char *opname, *butname, *tip;
78         int exec, xco, yco, width, height;
79         
80         if( !PyArg_ParseTuple( args, "O!sisiiiis:defButO", &PyCObject_Type, &py_block, &opname, &exec, &butname, &xco, &yco, &width, &height, &tip))
81                 return NULL;
82         
83         return PyCObject_FromVoidPtr(uiDefButO(PyCObject_AsVoidPtr(py_block), BUT, opname, exec, butname, xco, yco, width, height, tip), NULL );
84 }
85
86 static uiBlock *py_internal_uiBlockCreateFunc(struct bContext *C, struct ARegion *ar, void *arg1)
87 {
88         PyObject *ret, *args;
89         
90         args = Py_BuildValue("(NN)", PyCObject_FromVoidPtr(C, NULL), PyCObject_FromVoidPtr(ar, NULL));
91         ret = PyObject_CallObject( (PyObject *)arg1, args );
92         Py_DECREF(args);
93         
94         if (ret==NULL) {
95                 PyErr_Print();
96                 return NULL;
97         }
98         if (!PyCObject_Check(ret)) {
99                 printf("invalid return value, not a PyCObject block\n");
100                 return NULL;
101         }
102         
103         return (uiBlock *)PyCObject_AsVoidPtr(ret);
104 }
105
106 static PyObject *Method_pupBlock( PyObject * self, PyObject * args )
107 {
108         PyObject *py_context, *py_func;
109         
110         if( !PyArg_ParseTuple( args, "O!O:pupBlock", &PyCObject_Type, &py_context, &py_func) )
111                 return NULL;
112         
113         if (!PyCallable_Check(py_func)) {
114                 PyErr_SetString(PyExc_ValueError, "arg not callable");
115                 return NULL;
116         }
117         
118         uiPupBlock(PyCObject_AsVoidPtr(py_context), py_internal_uiBlockCreateFunc, (void *)py_func);
119         Py_RETURN_NONE;
120 }
121
122 static PyObject *Method_beginBlock( PyObject * self, PyObject * args ) // XXX missing 2 args - UI_EMBOSS, UI_HELV, do we care?
123 {
124         PyObject *py_context, *py_ar;
125         char *name;
126         
127         if( !PyArg_ParseTuple( args, "O!O!s:beginBlock", &PyCObject_Type, &py_context, &PyCObject_Type, &py_ar, &name) )
128                 return NULL;
129         
130         return PyCObject_FromVoidPtr(uiBeginBlock(PyCObject_AsVoidPtr(py_context), PyCObject_AsVoidPtr(py_ar), name, UI_EMBOSS, UI_HELV), NULL);
131 }
132
133 static PyObject *Method_endBlock( PyObject * self, PyObject * args )
134 {
135         PyObject *py_context, *py_block;
136         
137         if( !PyArg_ParseTuple( args, "O!O!:endBlock", &PyCObject_Type, &py_context, &PyCObject_Type, &py_block) )
138                 return NULL;
139         
140         uiEndBlock(PyCObject_AsVoidPtr(py_context), PyCObject_AsVoidPtr(py_block));
141         Py_RETURN_NONE;
142 }
143
144 static PyObject *Method_popupBoundsBlock( PyObject * self, PyObject * args )
145 {
146         PyObject *py_block;
147         int addval, mx, my;
148         
149         if( !PyArg_ParseTuple( args, "O!iii:popupBoundsBlock", &PyCObject_Type, &py_block, &addval, &mx, &my) )
150                 return NULL;
151         
152         uiPopupBoundsBlock(PyCObject_AsVoidPtr(py_block), addval, mx, my);
153         Py_RETURN_NONE;
154 }
155
156 static PyObject *Method_blockBeginAlign( PyObject * self, PyObject * args )
157 {
158         PyObject *py_block;
159         
160         if( !PyArg_ParseTuple( args, "O!:blockBeginAlign", &PyCObject_Type, &py_block) )
161                 return NULL;
162         
163         uiBlockBeginAlign(PyCObject_AsVoidPtr(py_block));
164         Py_RETURN_NONE;
165 }
166
167 static PyObject *Method_blockEndAlign( PyObject * self, PyObject * args )
168 {
169         PyObject *py_block;
170         
171         if( !PyArg_ParseTuple( args, "O!:blockEndAlign", &PyCObject_Type, &py_block))
172                 return NULL;
173         
174         uiBlockEndAlign(PyCObject_AsVoidPtr(py_block));
175         Py_RETURN_NONE;
176 }
177
178 static PyObject *Method_newPanel( PyObject * self, PyObject * args )
179 {
180         PyObject *py_context, *py_area, *py_block;
181         char *panelname, *tabname;
182         int ofsx, ofsy, sizex, sizey;
183         
184         if( !PyArg_ParseTuple( args, "O!O!O!ssiiii:newPanel", &PyCObject_Type, &py_context, &PyCObject_Type, &py_area, &PyCObject_Type, &py_block, &panelname, &tabname, &ofsx, &ofsy, &sizex, &sizey))
185                 return NULL;
186         
187         return PyLong_FromSize_t(uiNewPanel(PyCObject_AsVoidPtr(py_context), PyCObject_AsVoidPtr(py_area), PyCObject_AsVoidPtr(py_block), panelname, tabname, ofsx, ofsy, sizex, sizey));
188 }
189
190 /* internal use only */
191 static bContext *get_py_context__internal(void)
192 {
193         PyObject *globals = PyEval_GetGlobals();
194         PyObject *val= PyDict_GetItemString(globals, "__bpy_context__");
195         return PyCObject_AsVoidPtr(val);
196 }
197
198 static PyObject *Method_getRegonPtr( PyObject * self )
199 {
200         bContext *C= get_py_context__internal();
201         
202         ARegion *ar = CTX_wm_region(C);
203         return PyCObject_FromVoidPtr(ar, NULL);
204 }
205
206 static PyObject *Method_getAreaPtr( PyObject * self )
207 {
208         bContext *C= get_py_context__internal();
209         
210         ScrArea *area = CTX_wm_area(C);
211         return PyCObject_FromVoidPtr(area, NULL);
212 }
213
214 static PyObject *Method_getScreenPtr( PyObject * self )
215 {
216         bContext *C= get_py_context__internal();
217         
218         bScreen *screen= CTX_wm_screen(C);
219         return PyCObject_FromVoidPtr(screen, NULL);
220 }
221
222 static PyObject *Method_getWindowPtr( PyObject * self )
223 {
224         bContext *C= get_py_context__internal();
225         
226         wmWindow *window= CTX_wm_window(C);
227         return PyCObject_FromVoidPtr(window, NULL);
228 }
229
230 static struct PyMethodDef ui_methods[] = {
231         {"pupMenuBegin", (PyCFunction)Method_pupMenuBegin, METH_VARARGS, ""},
232         {"pupMenuEnd", (PyCFunction)Method_pupMenuEnd, METH_VARARGS, ""},
233         {"menuItemO", (PyCFunction)Method_menuItemO, METH_VARARGS, ""},
234         {"defButO", (PyCFunction)Method_defButO, METH_VARARGS, ""},
235         {"pupBlock", (PyCFunction)Method_pupBlock, METH_VARARGS, ""},
236         {"beginBlock", (PyCFunction)Method_beginBlock, METH_VARARGS, ""},
237         {"endBlock", (PyCFunction)Method_endBlock, METH_VARARGS, ""},
238         {"popupBoundsBlock", (PyCFunction)Method_popupBoundsBlock, METH_VARARGS, ""},
239         {"blockBeginAlign", (PyCFunction)Method_blockBeginAlign, METH_VARARGS, ""},
240         {"blockEndAlign", (PyCFunction)Method_blockEndAlign, METH_VARARGS, ""},
241         {"newPanel", (PyCFunction)Method_newPanel, METH_VARARGS, ""},
242         
243         {"getRegonPtr", (PyCFunction)Method_getRegonPtr,        METH_NOARGS, ""}, // XXX Nasty, we really need to improve dealing with context!
244         {"getAreaPtr", (PyCFunction)Method_getAreaPtr,          METH_NOARGS, ""},
245         {"getScreenPtr", (PyCFunction)Method_getScreenPtr, METH_NOARGS, ""},
246         {"getWindowPtr", (PyCFunction)Method_getWindowPtr, METH_NOARGS, ""},
247         {NULL, NULL, 0, NULL}
248 };
249
250 #if PY_VERSION_HEX >= 0x03000000
251 static struct PyModuleDef ui_module = {
252         PyModuleDef_HEAD_INIT,
253         "bpyui",
254         "",
255         -1,/* multiple "initialization" just copies the module dict. */
256         ui_methods,
257         NULL, NULL, NULL, NULL
258 };
259
260 PyObject *BPY_ui_module( void )
261 {
262         return PyModule_Create(&ui_module);
263 }
264 #else /* Py2.x */
265 PyObject *BPY_ui_module( void )
266 {
267         return Py_InitModule3( "bpyui", ui_methods, "" );
268 }
269 #endif