Python experimental UI API
[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_util.h"
27 #include "bpy_rna.h" /* for rna buttons */
28 #include "bpy_operator.h" /* for setting button operator properties */
29 #include "bpy_compat.h"
30 #include "WM_types.h" /* for WM_OP_INVOKE_DEFAULT & friends */
31
32 #include "BLI_dynstr.h"
33
34 #include "MEM_guardedalloc.h"
35 #include "BKE_global.h" /* evil G.* */
36 #include "BKE_context.h"
37
38 #include "DNA_screen_types.h"
39 #include "DNA_space_types.h" /* only for SpaceLink */
40 #include "UI_interface.h"
41 #include "WM_api.h"
42
43 static PyObject *Method_pupMenuBegin( PyObject * self, PyObject * args )
44 {
45         char *title; int icon;
46         
47         if( !PyArg_ParseTuple( args, "si:pupMenuBegin", &title, &icon))
48                 return NULL;
49         
50         return PyCObject_FromVoidPtr( uiPupMenuBegin(title, icon), NULL );
51 }
52
53 static PyObject *Method_pupMenuEnd( PyObject * self, PyObject * args )
54 {
55         PyObject *py_context, *py_head;
56         
57         if( !PyArg_ParseTuple( args, "O!O!:pupMenuEnd", &PyCObject_Type, &py_context, &PyCObject_Type, &py_head))
58                 return NULL;
59         
60         uiPupMenuEnd(PyCObject_AsVoidPtr(py_context), PyCObject_AsVoidPtr(py_head));
61         
62         Py_RETURN_NONE;
63 }
64
65 static PyObject *Method_menuItemO( PyObject * self, PyObject * args )
66 {
67         PyObject *py_head;
68         char *opname;
69         int icon;
70         
71         if( !PyArg_ParseTuple( args, "O!is:menuItemO", &PyCObject_Type, &py_head, &icon, &opname))
72                 return NULL;
73         
74         uiMenuItemO(PyCObject_AsVoidPtr(py_head), icon, opname);
75         
76         Py_RETURN_NONE;
77 }
78
79 static PyObject *Method_defButO( PyObject * self, PyObject * args )
80 {
81         uiBut *but;
82         PyObject *py_block, *py_keywords= NULL;
83         char *opname, *butname, *tip;
84         int exec, xco, yco, width, height;
85         
86         if( !PyArg_ParseTuple( args, "O!sisiiiis|O!:defButO", &PyCObject_Type, &py_block, &opname, &exec, &butname, &xco, &yco, &width, &height, &tip, &PyDict_Type, &py_keywords))
87                 return NULL;
88         
89         but= uiDefButO(PyCObject_AsVoidPtr(py_block), BUT, opname, exec, butname, xco, yco, width, height, tip);
90         
91         /* Optional python doctionary used to set python properties, just like how keyword args are used */
92         if (py_keywords && PyDict_Size(py_keywords)) {
93                 if (PYOP_props_from_dict(uiButGetOperatorPtrRNA(but), py_keywords) == -1)
94                         return NULL;
95         }
96         
97         return PyCObject_FromVoidPtr(but, NULL);
98 }
99
100 static PyObject *Method_defAutoButR( PyObject * self, PyObject * args )
101 {
102         PyObject *py_block;
103         BPy_StructRNA *py_rna;
104         char *propname, *butname;
105         int index, xco, yco, width, height;
106         PropertyRNA *prop;
107         
108         if( !PyArg_ParseTuple( args, "O!O!sisiiii:defAutoButR", &PyCObject_Type, &py_block, &pyrna_struct_Type, &py_rna, &propname, &index, &butname, &xco, &yco, &width, &height))
109                 return NULL;
110         
111         // XXX This isnt that nice api, but we dont always have the rna property from python since its converted immediately into a PyObject
112         prop = RNA_struct_find_property(&py_rna->ptr, propname);
113         if (prop==NULL) {
114                 PyErr_SetString(PyExc_ValueError, "rna property not found");
115                 return NULL;
116         }
117         
118         return PyCObject_FromVoidPtr(   uiDefAutoButR(PyCObject_AsVoidPtr(py_block), &py_rna->ptr, prop, index, butname, xco, yco, width, height), NULL);
119 }
120
121
122
123 static uiBlock *py_internal_uiBlockCreateFunc(struct bContext *C, struct ARegion *ar, void *arg1)
124 {
125         PyObject *ret, *args;
126         
127         args = Py_BuildValue("(NN)", PyCObject_FromVoidPtr(C, NULL), PyCObject_FromVoidPtr(ar, NULL));
128         ret = PyObject_CallObject( (PyObject *)arg1, args );
129         Py_DECREF(args);
130         
131         if (ret==NULL) {
132                 PyErr_Print();
133                 return NULL;
134         }
135         if (!PyCObject_Check(ret)) {
136                 printf("invalid return value, not a PyCObject block\n");
137                 return NULL;
138         }
139         
140         return (uiBlock *)PyCObject_AsVoidPtr(ret);
141 }
142
143 static PyObject *Method_pupBlock( PyObject * self, PyObject * args )
144 {
145         PyObject *py_context, *py_func;
146         
147         if( !PyArg_ParseTuple( args, "O!O:pupBlock", &PyCObject_Type, &py_context, &py_func) )
148                 return NULL;
149         
150         if (!PyCallable_Check(py_func)) {
151                 PyErr_SetString(PyExc_ValueError, "arg not callable");
152                 return NULL;
153         }
154         
155         uiPupBlock(PyCObject_AsVoidPtr(py_context), py_internal_uiBlockCreateFunc, (void *)py_func);
156         Py_RETURN_NONE;
157 }
158
159 static PyObject *Method_beginBlock( PyObject * self, PyObject * args ) // XXX missing 2 args - UI_EMBOSS, UI_HELV, do we care?
160 {
161         PyObject *py_context, *py_ar;
162         char *name;
163         
164         if( !PyArg_ParseTuple( args, "O!O!s:beginBlock", &PyCObject_Type, &py_context, &PyCObject_Type, &py_ar, &name) )
165                 return NULL;
166         
167         return PyCObject_FromVoidPtr(uiBeginBlock(PyCObject_AsVoidPtr(py_context), PyCObject_AsVoidPtr(py_ar), name, UI_EMBOSS, UI_HELV), NULL);
168 }
169
170 static PyObject *Method_endBlock( PyObject * self, PyObject * args )
171 {
172         PyObject *py_context, *py_block;
173         
174         if( !PyArg_ParseTuple( args, "O!O!:endBlock", &PyCObject_Type, &py_context, &PyCObject_Type, &py_block) )
175                 return NULL;
176         
177         uiEndBlock(PyCObject_AsVoidPtr(py_context), PyCObject_AsVoidPtr(py_block));
178         Py_RETURN_NONE;
179 }
180
181 static PyObject *Method_drawBlock( PyObject * self, PyObject * args )
182 {
183         PyObject *py_context, *py_block;
184         
185         if( !PyArg_ParseTuple( args, "O!O!:drawBlock", &PyCObject_Type, &py_context, &PyCObject_Type, &py_block) )
186                 return NULL;
187         
188         uiDrawBlock(PyCObject_AsVoidPtr(py_context), PyCObject_AsVoidPtr(py_block));
189         Py_RETURN_NONE;
190 }
191
192 static PyObject *Method_drawPanels( PyObject * self, PyObject * args )
193 {
194         PyObject *py_context;
195         int align;
196         
197         if( !PyArg_ParseTuple( args, "O!i:drawPanels", &PyCObject_Type, &py_context, &align) )
198                 return NULL;
199         
200         uiDrawPanels(PyCObject_AsVoidPtr(py_context), align);
201         Py_RETURN_NONE;
202 }
203
204 static PyObject *Method_matchPanelsView2d( PyObject * self, PyObject * args )
205 {
206         PyObject *py_ar;
207         
208         if( !PyArg_ParseTuple( args, "O!:matchPanelsView2d", &PyCObject_Type, &py_ar) )
209                 return NULL;
210         
211         uiMatchPanelsView2d(PyCObject_AsVoidPtr(py_ar));
212         Py_RETURN_NONE;
213 }
214
215 static PyObject *Method_popupBoundsBlock( PyObject * self, PyObject * args )
216 {
217         PyObject *py_block;
218         int addval, mx, my;
219         
220         if( !PyArg_ParseTuple( args, "O!iii:popupBoundsBlock", &PyCObject_Type, &py_block, &addval, &mx, &my) )
221                 return NULL;
222         
223         uiPopupBoundsBlock(PyCObject_AsVoidPtr(py_block), addval, mx, my);
224         Py_RETURN_NONE;
225 }
226
227 static PyObject *Method_blockBeginAlign( PyObject * self, PyObject * args )
228 {
229         PyObject *py_block;
230         
231         if( !PyArg_ParseTuple( args, "O!:blockBeginAlign", &PyCObject_Type, &py_block) )
232                 return NULL;
233         
234         uiBlockBeginAlign(PyCObject_AsVoidPtr(py_block));
235         Py_RETURN_NONE;
236 }
237
238 static PyObject *Method_blockEndAlign( PyObject * self, PyObject * args )
239 {
240         PyObject *py_block;
241         
242         if( !PyArg_ParseTuple( args, "O!:blockEndAlign", &PyCObject_Type, &py_block))
243                 return NULL;
244         
245         uiBlockEndAlign(PyCObject_AsVoidPtr(py_block));
246         Py_RETURN_NONE;
247 }
248
249 static PyObject *Method_blockSetFlag( PyObject * self, PyObject * args )
250 {
251         PyObject *py_block;
252         int flag; /* Note new py api should not use flags, but for this low level UI api its ok. */
253         
254         if( !PyArg_ParseTuple( args, "O!i:blockSetFlag", &PyCObject_Type, &py_block, &flag))
255                 return NULL;
256         
257         uiBlockSetFlag(PyCObject_AsVoidPtr(py_block), flag);
258         Py_RETURN_NONE;
259 }
260
261 static PyObject *Method_newPanel( PyObject * self, PyObject * args )
262 {
263         PyObject *py_context, *py_area, *py_block;
264         char *panelname, *tabname;
265         int ofsx, ofsy, sizex, sizey;
266         
267         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))
268                 return NULL;
269         
270         return PyLong_FromSize_t(uiNewPanel(PyCObject_AsVoidPtr(py_context), PyCObject_AsVoidPtr(py_area), PyCObject_AsVoidPtr(py_block), panelname, tabname, ofsx, ofsy, sizex, sizey));
271 }
272
273 /* similar to Draw.c */
274 static PyObject *Method_register( PyObject * self, PyObject * args )
275 {
276         PyObject *py_sl, *py_draw_func;
277         SpaceLink *sl;
278         if( !PyArg_ParseTuple( args, "O!O:register", &PyCObject_Type, &py_sl, &py_draw_func) )
279                 return NULL;
280         
281         sl = PyCObject_AsVoidPtr(py_sl);
282         
283         if(sl->spacetype!=SPACE_SCRIPT) { // XXX todo - add a script space when needed
284                 PyErr_SetString(PyExc_ValueError, "can only register in a script space");
285                 return NULL;
286         }
287         else {
288                 SpaceScript *scpt= (SpaceScript *)sl;
289                 char *filename = NULL;
290                 
291                 if (scpt->script==NULL) {
292                         scpt->script = MEM_callocN(sizeof(Script), "ScriptRegister");
293                 }
294                 
295                 BPY_getFileAndNum(&filename, NULL);
296                 
297                 if (filename) {
298                         strncpy(scpt->script->scriptname, filename, sizeof(scpt->script->scriptname));
299 #if 0
300                         char *dot;
301                         dot = strchr(scpt->script->scriptname, '.'); /* remove extension */
302                         if (dot)
303                                 *dot= '\0';
304 #endif
305                         Py_XINCREF( py_draw_func );
306                         scpt->script->py_draw= (void *)py_draw_func;
307                 }
308                 else {
309                         return NULL; /* BPY_getFileAndNum sets the error */
310                 }
311
312                 if (filename==NULL) {
313                         return NULL;
314                 }
315         }
316
317         Py_RETURN_NONE;
318 }
319
320
321
322 /* internal use only */
323 static bContext *get_py_context__internal(void)
324 {
325         PyObject *globals = PyEval_GetGlobals();
326         PyObject *val= PyDict_GetItemString(globals, "__bpy_context__");
327         return PyCObject_AsVoidPtr(val);
328 }
329
330 static PyObject *Method_getRegonPtr( PyObject * self )
331 {
332         bContext *C= get_py_context__internal();
333         
334         ARegion *ar = CTX_wm_region(C);
335         return PyCObject_FromVoidPtr(ar, NULL);
336 }
337
338 static PyObject *Method_getAreaPtr( PyObject * self )
339 {
340         bContext *C= get_py_context__internal();
341         
342         ScrArea *area = CTX_wm_area(C);
343         return PyCObject_FromVoidPtr(area, NULL);
344 }
345
346 static PyObject *Method_getScreenPtr( PyObject * self )
347 {
348         bContext *C= get_py_context__internal();
349         
350         bScreen *screen= CTX_wm_screen(C);
351         return PyCObject_FromVoidPtr(screen, NULL);
352 }
353
354 static PyObject *Method_getSpacePtr( PyObject * self )
355 {
356         bContext *C= get_py_context__internal();
357         
358         SpaceLink *sl= CTX_wm_space_data(C);
359         return PyCObject_FromVoidPtr(sl, NULL);
360 }
361
362 static PyObject *Method_getWindowPtr( PyObject * self )
363 {
364         bContext *C= get_py_context__internal();
365         
366         wmWindow *window= CTX_wm_window(C);
367         return PyCObject_FromVoidPtr(window, NULL);
368 }
369
370 static struct PyMethodDef ui_methods[] = {
371         {"pupMenuBegin", (PyCFunction)Method_pupMenuBegin, METH_VARARGS, ""},
372         {"pupMenuEnd", (PyCFunction)Method_pupMenuEnd, METH_VARARGS, ""},
373         {"menuItemO", (PyCFunction)Method_menuItemO, METH_VARARGS, ""},
374         {"defButO", (PyCFunction)Method_defButO, METH_VARARGS, ""},
375         {"defAutoButR", (PyCFunction)Method_defAutoButR, METH_VARARGS, ""},
376         {"pupBlock", (PyCFunction)Method_pupBlock, METH_VARARGS, ""},
377         {"beginBlock", (PyCFunction)Method_beginBlock, METH_VARARGS, ""},
378         {"endBlock", (PyCFunction)Method_endBlock, METH_VARARGS, ""},
379         {"drawBlock", (PyCFunction)Method_drawBlock, METH_VARARGS, ""},
380         {"popupBoundsBlock", (PyCFunction)Method_popupBoundsBlock, METH_VARARGS, ""},
381         {"blockBeginAlign", (PyCFunction)Method_blockBeginAlign, METH_VARARGS, ""},
382         {"blockEndAlign", (PyCFunction)Method_blockEndAlign, METH_VARARGS, ""},
383         {"blockSetFlag", (PyCFunction)Method_blockSetFlag, METH_VARARGS, ""},
384         {"newPanel", (PyCFunction)Method_newPanel, METH_VARARGS, ""},
385         {"drawPanels", (PyCFunction)Method_drawPanels, METH_VARARGS, ""},
386         {"matchPanelsView2d", (PyCFunction)Method_matchPanelsView2d, METH_VARARGS, ""},
387         
388         {"register", (PyCFunction)Method_register, METH_VARARGS, ""}, // XXX not sure about this - registers current script with the ScriptSpace, like Draw.Register()
389         
390         {"getRegonPtr", (PyCFunction)Method_getRegonPtr,        METH_NOARGS, ""}, // XXX Nasty, we really need to improve dealing with context!
391         {"getAreaPtr", (PyCFunction)Method_getAreaPtr,          METH_NOARGS, ""},
392         {"getScreenPtr", (PyCFunction)Method_getScreenPtr, METH_NOARGS, ""},
393         {"getSpacePtr", (PyCFunction)Method_getSpacePtr, METH_NOARGS, ""},
394         {"getWindowPtr", (PyCFunction)Method_getWindowPtr, METH_NOARGS, ""},
395         {NULL, NULL, 0, NULL}
396 };
397
398 static struct PyModuleDef ui_module = {
399         PyModuleDef_HEAD_INIT,
400         "bpyui",
401         "",
402         -1,/* multiple "initialization" just copies the module dict. */
403         ui_methods,
404         NULL, NULL, NULL, NULL
405 };
406
407 PyObject *BPY_ui_module( void )
408 {
409         PyObject *submodule, *dict;
410 #if PY_VERSION_HEX >= 0x03000000
411         submodule= PyModule_Create(&ui_module);
412 #else /* Py2.x */
413         submodule= Py_InitModule3( "bpyui", ui_methods, "" );
414 #endif
415         
416         /* uiBlock->flag (controls) */
417         PyModule_AddObject( submodule, "UI_BLOCK_LOOP", PyLong_FromSize_t(UI_BLOCK_LOOP) );
418         PyModule_AddObject( submodule, "UI_BLOCK_RET_1", PyLong_FromSize_t(UI_BLOCK_RET_1) );
419         PyModule_AddObject( submodule, "UI_BLOCK_NUMSELECT", PyLong_FromSize_t(UI_BLOCK_NUMSELECT) );
420         PyModule_AddObject( submodule, "UI_BLOCK_ENTER_OK", PyLong_FromSize_t(UI_BLOCK_ENTER_OK) );
421         PyModule_AddObject( submodule, "UI_BLOCK_NOSHADOW", PyLong_FromSize_t(UI_BLOCK_NOSHADOW) );
422         PyModule_AddObject( submodule, "UI_BLOCK_NO_HILITE", PyLong_FromSize_t(UI_BLOCK_NO_HILITE) );
423         PyModule_AddObject( submodule, "UI_BLOCK_MOVEMOUSE_QUIT", PyLong_FromSize_t(UI_BLOCK_MOVEMOUSE_QUIT) );
424         PyModule_AddObject( submodule, "UI_BLOCK_KEEP_OPEN", PyLong_FromSize_t(UI_BLOCK_KEEP_OPEN) );
425         PyModule_AddObject( submodule, "UI_BLOCK_POPUP", PyLong_FromSize_t(UI_BLOCK_POPUP) );
426         
427         /* for executing operators (XXX move elsewhere) */
428         PyModule_AddObject( submodule, "WM_OP_INVOKE_DEFAULT", PyLong_FromSize_t(WM_OP_INVOKE_DEFAULT) );
429         PyModule_AddObject( submodule, "WM_OP_INVOKE_REGION_WIN", PyLong_FromSize_t(WM_OP_INVOKE_REGION_WIN) );
430         PyModule_AddObject( submodule, "WM_OP_INVOKE_AREA", PyLong_FromSize_t(WM_OP_INVOKE_AREA) );
431         PyModule_AddObject( submodule, "WM_OP_INVOKE_SCREEN", PyLong_FromSize_t(WM_OP_INVOKE_SCREEN) );
432         PyModule_AddObject( submodule, "WM_OP_EXEC_DEFAULT", PyLong_FromSize_t(WM_OP_EXEC_DEFAULT) );
433         PyModule_AddObject( submodule, "WM_OP_EXEC_REGION_WIN", PyLong_FromSize_t(WM_OP_EXEC_REGION_WIN) );
434         PyModule_AddObject( submodule, "WM_OP_EXEC_AREA", PyLong_FromSize_t(WM_OP_EXEC_AREA) );
435         PyModule_AddObject( submodule, "WM_OP_EXEC_SCREEN", PyLong_FromSize_t(WM_OP_EXEC_SCREEN) );
436         
437         return submodule;
438 }
439
440