Blender V2.61 - r43446
|
00001 /* 00002 * ***** BEGIN GPL LICENSE BLOCK ***** 00003 * 00004 * This program is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU General Public License 00006 * as published by the Free Software Foundation; either version 2 00007 * of the License, or (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software Foundation, 00016 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00017 * 00018 * Contributor(s): Campbell Barton 00019 * 00020 * ***** END GPL LICENSE BLOCK ***** 00021 */ 00022 00031 #include <Python.h> 00032 00033 #include "RNA_types.h" 00034 00035 #include "bpy_rna.h" 00036 #include "bpy_rna_callback.h" 00037 #include "bpy_util.h" 00038 00039 #include "BLI_utildefines.h" 00040 00041 #include "DNA_screen_types.h" 00042 00043 #include "RNA_access.h" 00044 00045 #include "BKE_context.h" 00046 #include "ED_space_api.h" 00047 00048 /* use this to stop other capsules from being mis-used */ 00049 #define RNA_CAPSULE_ID "RNA_HANDLE" 00050 #define RNA_CAPSULE_ID_INVALID "RNA_HANDLE_REMOVED" 00051 00052 static void cb_region_draw(const bContext *C, ARegion *UNUSED(ar), void *customdata) 00053 { 00054 PyObject *cb_func, *cb_args, *result; 00055 PyGILState_STATE gilstate; 00056 00057 bpy_context_set((bContext *)C, &gilstate); 00058 00059 cb_func = PyTuple_GET_ITEM((PyObject *)customdata, 0); 00060 cb_args = PyTuple_GET_ITEM((PyObject *)customdata, 1); 00061 result = PyObject_CallObject(cb_func, cb_args); 00062 00063 if (result) { 00064 Py_DECREF(result); 00065 } 00066 else { 00067 PyErr_Print(); 00068 PyErr_Clear(); 00069 } 00070 00071 bpy_context_clear((bContext *)C, &gilstate); 00072 } 00073 00074 PyObject *pyrna_callback_add(BPy_StructRNA *self, PyObject *args) 00075 { 00076 void *handle; 00077 00078 PyObject *cb_func, *cb_args; 00079 char *cb_event_str = NULL; 00080 int cb_event; 00081 00082 if (!PyArg_ParseTuple(args, "OO!|s:bpy_struct.callback_add", &cb_func, &PyTuple_Type, &cb_args, &cb_event_str)) 00083 return NULL; 00084 00085 if (!PyCallable_Check(cb_func)) { 00086 PyErr_SetString(PyExc_TypeError, "callback_add(): first argument isn't callable"); 00087 return NULL; 00088 } 00089 00090 if (RNA_struct_is_a(self->ptr.type, &RNA_Region)) { 00091 if (cb_event_str) { 00092 static EnumPropertyItem region_draw_mode_items[] = { 00093 {REGION_DRAW_POST_PIXEL, "POST_PIXEL", 0, "Post Pixel", ""}, 00094 {REGION_DRAW_POST_VIEW, "POST_VIEW", 0, "Post View", ""}, 00095 {REGION_DRAW_PRE_VIEW, "PRE_VIEW", 0, "Pre View", ""}, 00096 {0, NULL, 0, NULL, NULL}}; 00097 00098 if (pyrna_enum_value_from_id(region_draw_mode_items, cb_event_str, &cb_event, "bpy_struct.callback_add()") < 0) { 00099 return NULL; 00100 } 00101 } 00102 else { 00103 cb_event = REGION_DRAW_POST_PIXEL; 00104 } 00105 00106 handle = ED_region_draw_cb_activate(((ARegion *)self->ptr.data)->type, cb_region_draw, (void *)args, cb_event); 00107 Py_INCREF(args); 00108 } 00109 else { 00110 PyErr_SetString(PyExc_TypeError, "callback_add(): type does not suppport callbacks"); 00111 return NULL; 00112 } 00113 00114 return PyCapsule_New((void *)handle, RNA_CAPSULE_ID, NULL); 00115 } 00116 00117 PyObject *pyrna_callback_remove(BPy_StructRNA *self, PyObject *args) 00118 { 00119 PyObject *py_handle; 00120 void *handle; 00121 void *customdata; 00122 00123 if (!PyArg_ParseTuple(args, "O!:callback_remove", &PyCapsule_Type, &py_handle)) 00124 return NULL; 00125 00126 handle = PyCapsule_GetPointer(py_handle, RNA_CAPSULE_ID); 00127 00128 if (handle == NULL) { 00129 PyErr_SetString(PyExc_ValueError, "callback_remove(handle): NULL handle given, invalid or already removed"); 00130 return NULL; 00131 } 00132 00133 if (RNA_struct_is_a(self->ptr.type, &RNA_Region)) { 00134 customdata = ED_region_draw_cb_customdata(handle); 00135 Py_DECREF((PyObject *)customdata); 00136 00137 ED_region_draw_cb_exit(((ARegion *)self->ptr.data)->type, handle); 00138 } 00139 00140 /* dont allow reuse */ 00141 PyCapsule_SetName(py_handle, RNA_CAPSULE_ID_INVALID); 00142 00143 Py_RETURN_NONE; 00144 }