4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * Contributor(s): Campbell Barton
22 * ***** END GPL LICENSE BLOCK *****
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 */
32 #include "BLI_dynstr.h"
34 #include "MEM_guardedalloc.h"
35 #include "BKE_global.h" /* evil G.* */
36 #include "BKE_context.h"
38 #include "DNA_screen_types.h"
39 #include "DNA_space_types.h" /* only for SpaceLink */
40 #include "UI_interface.h"
43 static PyObject *Method_pupMenuBegin( PyObject * self, PyObject * args )
45 char *title; int icon;
47 if( !PyArg_ParseTuple( args, "si:pupMenuBegin", &title, &icon))
50 return PyCObject_FromVoidPtr( uiPupMenuBegin(title, icon), NULL );
53 static PyObject *Method_pupMenuEnd( PyObject * self, PyObject * args )
55 PyObject *py_context, *py_head;
57 if( !PyArg_ParseTuple( args, "O!O!:pupMenuEnd", &PyCObject_Type, &py_context, &PyCObject_Type, &py_head))
60 uiPupMenuEnd(PyCObject_AsVoidPtr(py_context), PyCObject_AsVoidPtr(py_head));
65 static PyObject *Method_menuItemO( PyObject * self, PyObject * args )
71 if( !PyArg_ParseTuple( args, "O!is:menuItemO", &PyCObject_Type, &py_head, &icon, &opname))
74 uiMenuItemO(PyCObject_AsVoidPtr(py_head), icon, opname);
79 static PyObject *Method_defButO( PyObject * self, PyObject * args )
82 PyObject *py_block, *py_keywords= NULL;
83 char *opname, *butname, *tip;
84 int exec, xco, yco, width, height;
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))
89 but= uiDefButO(PyCObject_AsVoidPtr(py_block), BUT, opname, exec, butname, xco, yco, width, height, tip);
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)
97 return PyCObject_FromVoidPtr(but, NULL);
100 static PyObject *Method_defAutoButR( PyObject * self, PyObject * args )
103 BPy_StructRNA *py_rna;
104 char *propname, *butname;
105 int index, xco, yco, width, height;
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))
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);
114 PyErr_SetString(PyExc_ValueError, "rna property not found");
118 return PyCObject_FromVoidPtr( uiDefAutoButR(PyCObject_AsVoidPtr(py_block), &py_rna->ptr, prop, index, butname, xco, yco, width, height), NULL);
123 static uiBlock *py_internal_uiBlockCreateFunc(struct bContext *C, struct ARegion *ar, void *arg1)
125 PyObject *ret, *args;
127 args = Py_BuildValue("(NN)", PyCObject_FromVoidPtr(C, NULL), PyCObject_FromVoidPtr(ar, NULL));
128 ret = PyObject_CallObject( (PyObject *)arg1, args );
135 if (!PyCObject_Check(ret)) {
136 printf("invalid return value, not a PyCObject block\n");
140 return (uiBlock *)PyCObject_AsVoidPtr(ret);
143 static PyObject *Method_pupBlock( PyObject * self, PyObject * args )
145 PyObject *py_context, *py_func;
147 if( !PyArg_ParseTuple( args, "O!O:pupBlock", &PyCObject_Type, &py_context, &py_func) )
150 if (!PyCallable_Check(py_func)) {
151 PyErr_SetString(PyExc_ValueError, "arg not callable");
155 uiPupBlock(PyCObject_AsVoidPtr(py_context), py_internal_uiBlockCreateFunc, (void *)py_func);
159 static PyObject *Method_beginBlock( PyObject * self, PyObject * args ) // XXX missing 2 args - UI_EMBOSS, UI_HELV, do we care?
161 PyObject *py_context, *py_ar;
164 if( !PyArg_ParseTuple( args, "O!O!s:beginBlock", &PyCObject_Type, &py_context, &PyCObject_Type, &py_ar, &name) )
167 return PyCObject_FromVoidPtr(uiBeginBlock(PyCObject_AsVoidPtr(py_context), PyCObject_AsVoidPtr(py_ar), name, UI_EMBOSS, UI_HELV), NULL);
170 static PyObject *Method_endBlock( PyObject * self, PyObject * args )
172 PyObject *py_context, *py_block;
174 if( !PyArg_ParseTuple( args, "O!O!:endBlock", &PyCObject_Type, &py_context, &PyCObject_Type, &py_block) )
177 uiEndBlock(PyCObject_AsVoidPtr(py_context), PyCObject_AsVoidPtr(py_block));
181 static PyObject *Method_drawBlock( PyObject * self, PyObject * args )
183 PyObject *py_context, *py_block;
185 if( !PyArg_ParseTuple( args, "O!O!:drawBlock", &PyCObject_Type, &py_context, &PyCObject_Type, &py_block) )
188 uiDrawBlock(PyCObject_AsVoidPtr(py_context), PyCObject_AsVoidPtr(py_block));
192 static PyObject *Method_drawPanels( PyObject * self, PyObject * args )
194 PyObject *py_context;
197 if( !PyArg_ParseTuple( args, "O!i:drawPanels", &PyCObject_Type, &py_context, &align) )
200 uiDrawPanels(PyCObject_AsVoidPtr(py_context), align);
204 static PyObject *Method_matchPanelsView2d( PyObject * self, PyObject * args )
208 if( !PyArg_ParseTuple( args, "O!:matchPanelsView2d", &PyCObject_Type, &py_ar) )
211 uiMatchPanelsView2d(PyCObject_AsVoidPtr(py_ar));
215 static PyObject *Method_popupBoundsBlock( PyObject * self, PyObject * args )
220 if( !PyArg_ParseTuple( args, "O!iii:popupBoundsBlock", &PyCObject_Type, &py_block, &addval, &mx, &my) )
223 uiPopupBoundsBlock(PyCObject_AsVoidPtr(py_block), addval, mx, my);
227 static PyObject *Method_blockBeginAlign( PyObject * self, PyObject * args )
231 if( !PyArg_ParseTuple( args, "O!:blockBeginAlign", &PyCObject_Type, &py_block) )
234 uiBlockBeginAlign(PyCObject_AsVoidPtr(py_block));
238 static PyObject *Method_blockEndAlign( PyObject * self, PyObject * args )
242 if( !PyArg_ParseTuple( args, "O!:blockEndAlign", &PyCObject_Type, &py_block))
245 uiBlockEndAlign(PyCObject_AsVoidPtr(py_block));
249 static PyObject *Method_blockSetFlag( PyObject * self, PyObject * args )
252 int flag; /* Note new py api should not use flags, but for this low level UI api its ok. */
254 if( !PyArg_ParseTuple( args, "O!i:blockSetFlag", &PyCObject_Type, &py_block, &flag))
257 uiBlockSetFlag(PyCObject_AsVoidPtr(py_block), flag);
261 static PyObject *Method_newPanel( PyObject * self, PyObject * args )
263 PyObject *py_context, *py_area, *py_block;
264 char *panelname, *tabname;
265 int ofsx, ofsy, sizex, sizey;
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))
270 return PyLong_FromSize_t(uiNewPanel(PyCObject_AsVoidPtr(py_context), PyCObject_AsVoidPtr(py_area), PyCObject_AsVoidPtr(py_block), panelname, tabname, ofsx, ofsy, sizex, sizey));
273 /* similar to Draw.c */
274 static PyObject *Method_register( PyObject * self, PyObject * args )
276 PyObject *py_sl, *py_draw_func;
278 if( !PyArg_ParseTuple( args, "O!O:register", &PyCObject_Type, &py_sl, &py_draw_func) )
281 sl = PyCObject_AsVoidPtr(py_sl);
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");
288 SpaceScript *scpt= (SpaceScript *)sl;
289 char *filename = NULL;
291 if (scpt->script==NULL) {
292 scpt->script = MEM_callocN(sizeof(Script), "ScriptRegister");
295 BPY_getFileAndNum(&filename, NULL);
298 strncpy(scpt->script->scriptname, filename, sizeof(scpt->script->scriptname));
301 dot = strchr(scpt->script->scriptname, '.'); /* remove extension */
305 Py_XINCREF( py_draw_func );
306 scpt->script->py_draw= (void *)py_draw_func;
309 return NULL; /* BPY_getFileAndNum sets the error */
312 if (filename==NULL) {
322 /* internal use only */
323 static bContext *get_py_context__internal(void)
325 PyObject *globals = PyEval_GetGlobals();
326 PyObject *val= PyDict_GetItemString(globals, "__bpy_context__");
327 return PyCObject_AsVoidPtr(val);
330 static PyObject *Method_getRegonPtr( PyObject * self )
332 bContext *C= get_py_context__internal();
334 ARegion *ar = CTX_wm_region(C);
335 return PyCObject_FromVoidPtr(ar, NULL);
338 static PyObject *Method_getAreaPtr( PyObject * self )
340 bContext *C= get_py_context__internal();
342 ScrArea *area = CTX_wm_area(C);
343 return PyCObject_FromVoidPtr(area, NULL);
346 static PyObject *Method_getScreenPtr( PyObject * self )
348 bContext *C= get_py_context__internal();
350 bScreen *screen= CTX_wm_screen(C);
351 return PyCObject_FromVoidPtr(screen, NULL);
354 static PyObject *Method_getSpacePtr( PyObject * self )
356 bContext *C= get_py_context__internal();
358 SpaceLink *sl= CTX_wm_space_data(C);
359 return PyCObject_FromVoidPtr(sl, NULL);
362 static PyObject *Method_getWindowPtr( PyObject * self )
364 bContext *C= get_py_context__internal();
366 wmWindow *window= CTX_wm_window(C);
367 return PyCObject_FromVoidPtr(window, NULL);
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, ""},
388 {"register", (PyCFunction)Method_register, METH_VARARGS, ""}, // XXX not sure about this - registers current script with the ScriptSpace, like Draw.Register()
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}
398 static struct PyModuleDef ui_module = {
399 PyModuleDef_HEAD_INIT,
402 -1,/* multiple "initialization" just copies the module dict. */
404 NULL, NULL, NULL, NULL
407 PyObject *BPY_ui_module( void )
409 PyObject *submodule, *dict;
410 #if PY_VERSION_HEX >= 0x03000000
411 submodule= PyModule_Create(&ui_module);
413 submodule= Py_InitModule3( "bpyui", ui_methods, "" );
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) );
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) );