2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * Contributor(s): Campbell Barton
20 * ***** END GPL LICENSE BLOCK *****
23 /** \file blender/python/intern/bpy_rna_callback.c
24 * \ingroup pythonintern
26 * This file currently exposes callbacks for interface regions but may be
33 #include "RNA_types.h"
35 #include "BLI_utildefines.h"
38 #include "bpy_rna_callback.h"
41 #include "DNA_space_types.h"
42 #include "DNA_screen_types.h"
44 #include "RNA_access.h"
45 #include "RNA_enum_types.h"
47 #include "BKE_context.h"
48 #include "BKE_screen.h"
50 #include "ED_space_api.h"
52 /* use this to stop other capsules from being mis-used */
53 #define RNA_CAPSULE_ID "RNA_HANDLE"
54 #define RNA_CAPSULE_ID_INVALID "RNA_HANDLE_REMOVED"
56 static EnumPropertyItem region_draw_mode_items[] = {
57 {REGION_DRAW_POST_PIXEL, "POST_PIXEL", 0, "Post Pixel", ""},
58 {REGION_DRAW_POST_VIEW, "POST_VIEW", 0, "Post View", ""},
59 {REGION_DRAW_PRE_VIEW, "PRE_VIEW", 0, "Pre View", ""},
60 {0, NULL, 0, NULL, NULL}
63 static void cb_region_draw(const bContext *C, ARegion *UNUSED(ar), void *customdata)
65 PyObject *cb_func, *cb_args, *result;
66 PyGILState_STATE gilstate;
68 bpy_context_set((bContext *)C, &gilstate);
70 cb_func = PyTuple_GET_ITEM((PyObject *)customdata, 1);
71 cb_args = PyTuple_GET_ITEM((PyObject *)customdata, 2);
72 result = PyObject_CallObject(cb_func, cb_args);
82 bpy_context_clear((bContext *)C, &gilstate);
86 PyObject *pyrna_callback_add(BPy_StructRNA *self, PyObject *args)
90 PyObject *cb_func, *cb_args;
91 char *cb_event_str = NULL;
94 if (!PyArg_ParseTuple(args, "OO!|s:bpy_struct.callback_add", &cb_func, &PyTuple_Type, &cb_args, &cb_event_str))
97 if (!PyCallable_Check(cb_func)) {
98 PyErr_SetString(PyExc_TypeError, "callback_add(): first argument isn't callable");
102 if (RNA_struct_is_a(self->ptr.type, &RNA_Region)) {
104 if (pyrna_enum_value_from_id(region_draw_mode_items, cb_event_str, &cb_event, "bpy_struct.callback_add()") == -1) {
109 cb_event = REGION_DRAW_POST_PIXEL;
112 handle = ED_region_draw_cb_activate(((ARegion *)self->ptr.data)->type, cb_region_draw, (void *)args, cb_event);
116 PyErr_SetString(PyExc_TypeError, "callback_add(): type does not support callbacks");
120 return PyCapsule_New((void *)handle, RNA_CAPSULE_ID, NULL);
123 PyObject *pyrna_callback_remove(BPy_StructRNA *self, PyObject *args)
129 if (!PyArg_ParseTuple(args, "O!:callback_remove", &PyCapsule_Type, &py_handle))
132 handle = PyCapsule_GetPointer(py_handle, RNA_CAPSULE_ID);
134 if (handle == NULL) {
135 PyErr_SetString(PyExc_ValueError, "callback_remove(handle): NULL handle given, invalid or already removed");
139 if (RNA_struct_is_a(self->ptr.type, &RNA_Region)) {
140 customdata = ED_region_draw_cb_customdata(handle);
141 Py_DECREF((PyObject *)customdata);
143 ED_region_draw_cb_exit(((ARegion *)self->ptr.data)->type, handle);
146 PyErr_SetString(PyExc_TypeError, "callback_remove(): type does not support callbacks");
150 /* don't allow reuse */
151 PyCapsule_SetName(py_handle, RNA_CAPSULE_ID_INVALID);
157 /* reverse of rna_Space_refine() */
158 static eSpace_Type rna_Space_refine_reverse(StructRNA *srna)
160 if (srna == &RNA_SpaceView3D) return SPACE_VIEW3D;
161 if (srna == &RNA_SpaceGraphEditor) return SPACE_IPO;
162 if (srna == &RNA_SpaceOutliner) return SPACE_OUTLINER;
163 if (srna == &RNA_SpaceProperties) return SPACE_BUTS;
164 if (srna == &RNA_SpaceFileBrowser) return SPACE_FILE;
165 if (srna == &RNA_SpaceImageEditor) return SPACE_IMAGE;
166 if (srna == &RNA_SpaceInfo) return SPACE_INFO;
167 if (srna == &RNA_SpaceSequenceEditor) return SPACE_SEQ;
168 if (srna == &RNA_SpaceTextEditor) return SPACE_TEXT;
169 if (srna == &RNA_SpaceDopeSheetEditor) return SPACE_ACTION;
170 if (srna == &RNA_SpaceNLA) return SPACE_NLA;
171 if (srna == &RNA_SpaceTimeline) return SPACE_TIME;
172 if (srna == &RNA_SpaceNodeEditor) return SPACE_NODE;
173 if (srna == &RNA_SpaceLogicEditor) return SPACE_LOGIC;
174 if (srna == &RNA_SpaceConsole) return SPACE_CONSOLE;
175 if (srna == &RNA_SpaceUserPreferences) return SPACE_USERPREF;
176 if (srna == &RNA_SpaceClipEditor) return SPACE_CLIP;
180 PyObject *pyrna_callback_classmethod_add(PyObject *UNUSED(self), PyObject *args)
184 PyObject *cb_func, *cb_args;
185 const char *cb_regiontype_str;
186 const char *cb_event_str;
191 if (PyTuple_GET_SIZE(args) < 2) {
192 PyErr_SetString(PyExc_ValueError, "handler_add(handle): expected at least 2 args");
196 cls = PyTuple_GET_ITEM(args, 0);
197 if (!(srna = pyrna_struct_as_srna(cls, false, "handler_add"))) {
200 cb_func = PyTuple_GET_ITEM(args, 1);
201 if (!PyCallable_Check(cb_func)) {
202 PyErr_SetString(PyExc_TypeError, "first argument isn't callable");
206 /* class specific callbacks */
207 if (RNA_struct_is_a(srna, &RNA_Space)) {
208 if (!PyArg_ParseTuple(args, "OOO!ss:Space.draw_handler_add",
209 &cls, &cb_func, /* already assigned, no matter */
210 &PyTuple_Type, &cb_args, &cb_regiontype_str, &cb_event_str))
215 if (pyrna_enum_value_from_id(region_draw_mode_items, cb_event_str, &cb_event, "bpy_struct.callback_add()") == -1) {
218 else if (pyrna_enum_value_from_id(region_type_items, cb_regiontype_str, &cb_regiontype, "bpy_struct.callback_add()") == -1) {
222 const eSpace_Type spaceid = rna_Space_refine_reverse(srna);
224 PyErr_Format(PyExc_TypeError, "unknown space type '%.200s'", RNA_struct_identifier(srna));
228 SpaceType *st = BKE_spacetype_from_id(spaceid);
229 ARegionType *art = BKE_regiontype_from_id(st, cb_regiontype);
231 handle = ED_region_draw_cb_activate(art, cb_region_draw, (void *)args, cb_event);
237 PyErr_SetString(PyExc_TypeError, "callback_add(): type does not support callbacks");
241 return PyCapsule_New((void *)handle, RNA_CAPSULE_ID, NULL);
244 PyObject *pyrna_callback_classmethod_remove(PyObject *UNUSED(self), PyObject *args)
251 const char *cb_regiontype_str;
254 if (PyTuple_GET_SIZE(args) < 2) {
255 PyErr_SetString(PyExc_ValueError, "callback_remove(handle): expected at least 2 args");
259 cls = PyTuple_GET_ITEM(args, 0);
260 if (!(srna = pyrna_struct_as_srna(cls, false, "callback_remove"))) {
263 py_handle = PyTuple_GET_ITEM(args, 1);
264 handle = PyCapsule_GetPointer(py_handle, RNA_CAPSULE_ID);
265 if (handle == NULL) {
266 PyErr_SetString(PyExc_ValueError, "callback_remove(handle): NULL handle given, invalid or already removed");
270 if (RNA_struct_is_a(srna, &RNA_Space)) {
271 if (!PyArg_ParseTuple(args, "OO!s:Space.draw_handler_remove",
272 &cls, &PyCapsule_Type, &py_handle, /* already assigned, no matter */
278 customdata = ED_region_draw_cb_customdata(handle);
279 Py_DECREF((PyObject *)customdata);
281 if (pyrna_enum_value_from_id(region_type_items, cb_regiontype_str, &cb_regiontype, "bpy_struct.callback_remove()") == -1) {
285 const eSpace_Type spaceid = rna_Space_refine_reverse(srna);
287 PyErr_Format(PyExc_TypeError, "unknown space type '%.200s'", RNA_struct_identifier(srna));
291 SpaceType *st = BKE_spacetype_from_id(spaceid);
292 ARegionType *art = BKE_regiontype_from_id(st, cb_regiontype);
294 ED_region_draw_cb_exit(art, handle);
299 PyErr_SetString(PyExc_TypeError, "callback_remove(): type does not support callbacks");
303 /* don't allow reuse */
304 PyCapsule_SetName(py_handle, RNA_CAPSULE_ID_INVALID);