| 
									
										
										
										
											2011-02-23 10:52:22 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2010-03-21 01:14:04 +00:00
										 |  |  |  * $Id$ | 
					
						
							| 
									
										
										
										
											2010-02-27 13:27:06 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * ***** BEGIN GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Contributor(s): Campbell Barton | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ***** END GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-27 20:10:08 +00:00
										 |  |  | /** \file blender/python/intern/bpy_rna_callback.c
 | 
					
						
							|  |  |  |  *  \ingroup pythonintern | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-14 04:15:25 +00:00
										 |  |  | #include <Python.h>
 | 
					
						
							| 
									
										
										
										
											2010-02-27 13:27:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-02 04:51:43 +00:00
										 |  |  | #include "RNA_types.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-27 13:27:06 +00:00
										 |  |  | #include "bpy_rna.h"
 | 
					
						
							| 
									
										
										
										
											2011-02-13 10:52:18 +00:00
										 |  |  | #include "bpy_rna_callback.h"
 | 
					
						
							| 
									
										
										
										
											2010-02-27 13:27:06 +00:00
										 |  |  | #include "bpy_util.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-07 18:36:47 +00:00
										 |  |  | #include "BLI_utildefines.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-27 13:27:06 +00:00
										 |  |  | #include "DNA_screen_types.h"
 | 
					
						
							| 
									
										
										
										
											2011-01-07 19:18:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-02 04:51:43 +00:00
										 |  |  | #include "RNA_access.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-27 13:27:06 +00:00
										 |  |  | #include "BKE_context.h"
 | 
					
						
							|  |  |  | #include "ED_space_api.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-28 09:36:02 +00:00
										 |  |  | /* use this to stop other capsules from being mis-used */ | 
					
						
							|  |  |  | #define RNA_CAPSULE_ID "RNA_HANDLE"
 | 
					
						
							|  |  |  | #define RNA_CAPSULE_ID_INVALID "RNA_HANDLE_REMOVED"
 | 
					
						
							| 
									
										
										
										
											2010-02-27 13:27:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-13 10:52:18 +00:00
										 |  |  | static void cb_region_draw(const bContext *C, ARegion *UNUSED(ar), void *customdata) | 
					
						
							| 
									
										
										
										
											2010-02-27 13:27:06 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyObject *cb_func, *cb_args, *result; | 
					
						
							|  |  |  | 	PyGILState_STATE gilstate; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bpy_context_set((bContext *)C, &gilstate); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cb_func= PyTuple_GET_ITEM((PyObject *)customdata, 0); | 
					
						
							|  |  |  | 	cb_args= PyTuple_GET_ITEM((PyObject *)customdata, 1); | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	result= PyObject_CallObject(cb_func, cb_args); | 
					
						
							| 
									
										
										
										
											2010-02-27 13:27:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if(result) { | 
					
						
							|  |  |  | 		Py_DECREF(result); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		PyErr_Print(); | 
					
						
							|  |  |  | 		PyErr_Clear(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bpy_context_clear((bContext *)C, &gilstate); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject *pyrna_callback_add(BPy_StructRNA *self, PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	void *handle; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	PyObject *cb_func, *cb_args; | 
					
						
							| 
									
										
										
										
											2010-02-27 15:28:34 +00:00
										 |  |  | 	char *cb_event_str= NULL; | 
					
						
							|  |  |  | 	int cb_event; | 
					
						
							| 
									
										
										
										
											2010-02-27 13:27:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-09 02:29:06 +00:00
										 |  |  | 	if (!PyArg_ParseTuple(args, "OO!|s:bpy_struct.callback_add", &cb_func, &PyTuple_Type, &cb_args, &cb_event_str)) | 
					
						
							| 
									
										
										
										
											2010-02-27 13:27:06 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2010-08-09 02:29:06 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	if(!PyCallable_Check(cb_func)) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_TypeError, "callback_add(): first argument isn't callable"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-02-27 13:27:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if(RNA_struct_is_a(self->ptr.type, &RNA_Region)) { | 
					
						
							| 
									
										
										
										
											2010-08-09 02:29:06 +00:00
										 |  |  | 		if(cb_event_str) { | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 			static EnumPropertyItem region_draw_mode_items[]= { | 
					
						
							| 
									
										
										
										
											2010-08-09 02:29:06 +00:00
										 |  |  | 				{REGION_DRAW_POST_PIXEL, "POST_PIXEL", 0, "Post Pixel", ""}, | 
					
						
							|  |  |  | 				{REGION_DRAW_POST_VIEW, "POST_VIEW", 0, "Post View", ""}, | 
					
						
							|  |  |  | 				{REGION_DRAW_PRE_VIEW, "PRE_VIEW", 0, "Pre View", ""}, | 
					
						
							|  |  |  | 				{0, NULL, 0, NULL, NULL}}; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 			if(pyrna_enum_value_from_id(region_draw_mode_items, cb_event_str, &cb_event, "bpy_struct.callback_add()") < 0) | 
					
						
							|  |  |  | 				return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			cb_event= REGION_DRAW_POST_PIXEL; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2010-02-27 13:27:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-27 15:28:34 +00:00
										 |  |  | 		handle= ED_region_draw_cb_activate(((ARegion *)self->ptr.data)->type, cb_region_draw, (void *)args, cb_event); | 
					
						
							| 
									
										
										
										
											2010-02-27 13:27:06 +00:00
										 |  |  | 		Py_INCREF(args); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2010-02-28 09:36:02 +00:00
										 |  |  | 		PyErr_SetString(PyExc_TypeError, "callback_add(): type does not suppport callbacks"); | 
					
						
							| 
									
										
										
										
											2010-02-27 13:27:06 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-28 09:36:02 +00:00
										 |  |  | 	return PyCapsule_New((void *)handle, RNA_CAPSULE_ID, NULL); | 
					
						
							| 
									
										
										
										
											2010-02-27 13:27:06 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject *pyrna_callback_remove(BPy_StructRNA *self, PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyObject *py_handle; | 
					
						
							|  |  |  | 	void *handle; | 
					
						
							|  |  |  | 	void *customdata; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!PyArg_ParseTuple(args, "O!:callback_remove", &PyCapsule_Type, &py_handle)) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-28 09:36:02 +00:00
										 |  |  | 	handle= PyCapsule_GetPointer(py_handle, RNA_CAPSULE_ID); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(handle==NULL) { | 
					
						
							| 
									
										
										
										
											2010-11-23 16:45:17 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, "callback_remove(handle): NULL handle given, invalid or already removed"); | 
					
						
							| 
									
										
										
										
											2010-02-28 09:36:02 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-02-27 13:27:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if(RNA_struct_is_a(self->ptr.type, &RNA_Region)) { | 
					
						
							|  |  |  | 		customdata= ED_region_draw_cb_customdata(handle); | 
					
						
							|  |  |  | 		Py_DECREF((PyObject *)customdata); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ED_region_draw_cb_exit(((ARegion *)self->ptr.data)->type, handle); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-28 09:36:02 +00:00
										 |  |  | 	/* dont allow reuse */ | 
					
						
							|  |  |  | 	PyCapsule_SetName(py_handle, RNA_CAPSULE_ID_INVALID); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-27 13:27:06 +00:00
										 |  |  | 	Py_RETURN_NONE; | 
					
						
							|  |  |  | } |