Fix some inconsistencies in object visibility/selectability tests.
[blender.git] / source / blender / editors / screen / screen_context.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/editors/screen/screen_context.c
26  *  \ingroup edscr
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_object_types.h"
36 #include "DNA_armature_types.h"
37 #include "DNA_brush_types.h"
38 #include "DNA_gpencil_types.h"
39 #include "DNA_sequence_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_space_types.h"
43 #include "DNA_windowmanager_types.h"
44 #include "DNA_workspace_types.h"
45
46 #include "BLI_utildefines.h"
47
48 #include "BKE_brush.h"
49 #include "BKE_context.h"
50 #include "BKE_object.h"
51 #include "BKE_action.h"
52 #include "BKE_armature.h"
53 #include "BKE_paint.h"
54 #include "BKE_main.h"
55 #include "BKE_gpencil.h"
56 #include "BKE_layer.h"
57 #include "BKE_screen.h"
58 #include "BKE_sequencer.h"
59 #include "BKE_workspace.h"
60
61 #include "DEG_depsgraph.h"
62
63 #include "RNA_access.h"
64
65 #include "ED_armature.h"
66 #include "ED_gpencil.h"
67 #include "ED_anim_api.h"
68 #include "ED_uvedit.h"
69
70 #include "WM_api.h"
71 #include "UI_interface.h"
72
73 #include "screen_intern.h"
74
75 const char *screen_context_dir[] = {
76         "scene", "view_layer", "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
77         "selected_objects", "selected_bases",
78         "editable_objects", "editable_bases",
79         "selected_editable_objects", "selected_editable_bases",
80         "objects_in_mode", "objects_in_mode_unique_data",
81         "visible_bones", "editable_bones", "selected_bones", "selected_editable_bones",
82         "visible_pose_bones", "selected_pose_bones", "selected_pose_bones_from_active_object",
83         "active_bone", "active_pose_bone",
84         "active_base", "active_object", "object", "edit_object",
85         "sculpt_object", "vertex_paint_object", "weight_paint_object",
86         "image_paint_object", "particle_edit_object", "uv_sculpt_object",
87         "sequences", "selected_sequences", "selected_editable_sequences", /* sequencer */
88         "gpencil_data", "gpencil_data_owner", /* grease pencil data */
89         "visible_gpencil_layers", "editable_gpencil_layers", "editable_gpencil_strokes",
90         "active_gpencil_layer", "active_gpencil_frame",
91         "active_operator", "selected_editable_fcurves",
92         NULL};
93
94 int ed_screen_context(const bContext *C, const char *member, bContextDataResult *result)
95 {
96         wmWindow *win = CTX_wm_window(C);
97         View3D *v3d = CTX_wm_view3d(C); /* This may be NULL in a lot of cases. */
98         bScreen *sc = CTX_wm_screen(C);
99         ScrArea *sa = CTX_wm_area(C);
100         Scene *scene = WM_window_get_active_scene(win);
101         ViewLayer *view_layer = WM_window_get_active_view_layer(win);
102         Object *obact = (view_layer && view_layer->basact) ? view_layer->basact->object : NULL;
103         Object *obedit = view_layer ? OBEDIT_FROM_VIEW_LAYER(view_layer) : NULL;
104
105         if (CTX_data_dir(member)) {
106                 CTX_data_dir_set(result, screen_context_dir);
107                 return 1;
108         }
109         else if (CTX_data_equals(member, "scene")) {
110                 CTX_data_id_pointer_set(result, &scene->id);
111                 return 1;
112         }
113         else if (CTX_data_equals(member, "visible_objects")) {
114                 FOREACH_VISIBLE_OBJECT_BEGIN(view_layer, v3d, ob)
115                 {
116                         CTX_data_id_list_add(result, &ob->id);
117                 }
118                 FOREACH_VISIBLE_BASE_END;
119                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
120                 return 1;
121         }
122         else if (CTX_data_equals(member, "selectable_objects")) {
123                 for (Base *base = view_layer->object_bases.first; base; base = base->next) {
124                         if (BASE_SELECTABLE_BGMODE(v3d, base)) {
125                                 CTX_data_id_list_add(result, &base->object->id);
126                         }
127                 }
128                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
129                 return 1;
130         }
131         else if (CTX_data_equals(member, "selected_objects")) {
132                 FOREACH_SELECTED_OBJECT_BEGIN(view_layer, v3d, ob)
133                 {
134                         CTX_data_id_list_add(result, &ob->id);
135                 }
136                 FOREACH_SELECTED_OBJECT_END;
137                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
138                 return 1;
139         }
140         else if (CTX_data_equals(member, "selected_editable_objects")) {
141                 FOREACH_SELECTED_OBJECT_BEGIN(view_layer, v3d, ob)
142                 {
143                         if (0 == BKE_object_is_libdata(ob)) {
144                                 CTX_data_id_list_add(result, &ob->id);
145                         }
146                 }
147                 FOREACH_SELECTED_OBJECT_END;
148                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
149                 return 1;
150         }
151         else if (CTX_data_equals(member, "editable_objects")) {
152                 /* Visible + Editable, but not necessarily selected */
153                 FOREACH_VISIBLE_OBJECT_BEGIN(view_layer, v3d, ob)
154                 {
155                         if (0 == BKE_object_is_libdata(ob)) {
156                                 CTX_data_id_list_add(result, &ob->id);
157                         }
158                 }
159                 FOREACH_VISIBLE_OBJECT_END;
160                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
161                 return 1;
162         }
163         else if ( CTX_data_equals(member, "visible_bases")) {
164                 FOREACH_VISIBLE_BASE_BEGIN(view_layer, v3d, base)
165                 {
166                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
167                 }
168                 FOREACH_VISIBLE_BASE_END;
169                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
170                 return 1;
171         }
172         else if (CTX_data_equals(member, "selectable_bases")) {
173                 for (Base *base = view_layer->object_bases.first; base; base = base->next) {
174                         if (BASE_SELECTABLE_BGMODE(v3d, base)) {
175                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
176                         }
177                 }
178                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
179                 return 1;
180         }
181         else if (CTX_data_equals(member, "selected_bases")) {
182                 for (Base *base = view_layer->object_bases.first; base; base = base->next) {
183                         if (BASE_VISIBLE_BGMODE(v3d, base) && (base->flag & BASE_SELECTED) != 0) {
184                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
185                         }
186                 }
187                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
188                 return 1;
189         }
190         else if (CTX_data_equals(member, "selected_editable_bases")) {
191                 for (Base *base = view_layer->object_bases.first; base; base = base->next) {
192                         if (BASE_VISIBLE_BGMODE(v3d, base) && (base->flag & BASE_SELECTED) != 0) {
193                                 if (0 == BKE_object_is_libdata(base->object)) {
194                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
195                                 }
196                         }
197                 }
198                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
199                 return 1;
200         }
201         else if (CTX_data_equals(member, "editable_bases")) {
202                 /* Visible + Editable, but not necessarily selected */
203                 for (Base *base = view_layer->object_bases.first; base; base = base->next) {
204                         if (BASE_VISIBLE_BGMODE(v3d, base)) {
205                                 if (0 == BKE_object_is_libdata(base->object)) {
206                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
207                                 }
208                         }
209                 }
210                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
211                 return 1;
212         }
213         else if (CTX_data_equals(member, "objects_in_mode")) {
214                 if (obact && (obact->mode != OB_MODE_OBJECT)) {
215                         FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, obact->type, obact->mode, ob_iter) {
216                                 CTX_data_id_list_add(result, &ob_iter->id);
217                         } FOREACH_OBJECT_IN_MODE_END;
218                 }
219                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
220                 return 1;
221         }
222         else if (CTX_data_equals(member, "objects_in_mode_unique_data")) {
223                 if (obact && (obact->mode != OB_MODE_OBJECT)) {
224                         FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, obact->type, obact->mode, ob_iter) {
225                                 ob_iter->id.tag |= LIB_TAG_DOIT;
226                         } FOREACH_OBJECT_IN_MODE_END;
227                         FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, obact->type, obact->mode, ob_iter) {
228                                 if (ob_iter->id.tag & LIB_TAG_DOIT) {
229                                         ob_iter->id.tag &= ~LIB_TAG_DOIT;
230                                         CTX_data_id_list_add(result, &ob_iter->id);
231                                 }
232                         } FOREACH_OBJECT_IN_MODE_END;
233                 }
234                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
235                 return 1;
236         }
237         else if (CTX_data_equals(member, "visible_bones") || CTX_data_equals(member, "editable_bones")) {
238                 bArmature *arm = (obedit && obedit->type == OB_ARMATURE) ? obedit->data : NULL;
239                 EditBone *ebone, *flipbone = NULL;
240                 const bool editable_bones = CTX_data_equals(member, "editable_bones");
241
242                 if (arm && arm->edbo) {
243                         uint objects_len;
244                         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, CTX_wm_view3d(C), &objects_len);
245                         for (uint i = 0; i < objects_len; i++) {
246                                 Object *ob = objects[i];
247                                 arm = ob->data;
248
249                                 /* Attention: X-Axis Mirroring is also handled here... */
250                                 for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
251                                         /* first and foremost, bone must be visible and selected */
252                                         if (EBONE_VISIBLE(arm, ebone)) {
253                                                 /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
254                                                  * so that most users of this data don't need to explicitly check for it themselves.
255                                                  *
256                                                  * We need to make sure that these mirrored copies are not selected, otherwise some
257                                                  * bones will be operated on twice.
258                                                  */
259                                                 if (arm->flag & ARM_MIRROR_EDIT)
260                                                         flipbone = ED_armature_ebone_get_mirrored(arm->edbo, ebone);
261
262                                                 /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
263                                                 if (editable_bones) {
264                                                         /* only selected + editable */
265                                                         if (EBONE_EDITABLE(ebone)) {
266                                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
267
268                                                                 if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
269                                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
270                                                         }
271                                                 }
272                                                 else {
273                                                         /* only include bones if visible */
274                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
275
276                                                         if ((flipbone) && EBONE_VISIBLE(arm, flipbone) == 0)
277                                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
278                                                 }
279                                         }
280                                 }
281                         }
282                         MEM_freeN(objects);
283
284                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
285                         return 1;
286                 }
287         }
288         else if (CTX_data_equals(member, "selected_bones") || CTX_data_equals(member, "selected_editable_bones")) {
289                 bArmature *arm = (obedit && obedit->type == OB_ARMATURE) ? obedit->data : NULL;
290                 EditBone *ebone, *flipbone = NULL;
291                 const bool selected_editable_bones = CTX_data_equals(member, "selected_editable_bones");
292
293                 if (arm && arm->edbo) {
294                         uint objects_len;
295                         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, CTX_wm_view3d(C), &objects_len);
296                         for (uint i = 0; i < objects_len; i++) {
297                                 Object *ob = objects[i];
298                                 arm = ob->data;
299
300                                 /* Attention: X-Axis Mirroring is also handled here... */
301                                 for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
302                                         /* first and foremost, bone must be visible and selected */
303                                         if (EBONE_VISIBLE(arm, ebone) && (ebone->flag & BONE_SELECTED)) {
304                                                 /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
305                                                  * so that most users of this data don't need to explicitly check for it themselves.
306                                                  *
307                                                  * We need to make sure that these mirrored copies are not selected, otherwise some
308                                                  * bones will be operated on twice.
309                                                  */
310                                                 if (arm->flag & ARM_MIRROR_EDIT)
311                                                         flipbone = ED_armature_ebone_get_mirrored(arm->edbo, ebone);
312
313                                                 /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
314                                                 if (selected_editable_bones) {
315                                                         /* only selected + editable */
316                                                         if (EBONE_EDITABLE(ebone)) {
317                                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
318
319                                                                 if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
320                                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
321                                                         }
322                                                 }
323                                                 else {
324                                                         /* only include bones if selected */
325                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
326
327                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
328                                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
329                                                 }
330                                         }
331                                 }
332                         }
333                         MEM_freeN(objects);
334
335                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
336                         return 1;
337                 }
338         }
339         else if (CTX_data_equals(member, "visible_pose_bones")) {
340                 Object *obpose = BKE_object_pose_armature_get(obact);
341                 if (obpose && obpose->pose && obpose->data) {
342                         if (obpose != obact) {
343                                 FOREACH_PCHAN_VISIBLE_IN_OBJECT_BEGIN (obpose, pchan) {
344                                         CTX_data_list_add(result, &obpose->id, &RNA_PoseBone, pchan);
345                                 } FOREACH_PCHAN_SELECTED_IN_OBJECT_END;
346                         }
347                         else if (obact->mode & OB_MODE_POSE) {
348                                 FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, OB_ARMATURE, OB_MODE_POSE, ob_iter) {
349                                         FOREACH_PCHAN_VISIBLE_IN_OBJECT_BEGIN (ob_iter, pchan) {
350                                                 CTX_data_list_add(result, &ob_iter->id, &RNA_PoseBone, pchan);
351                                         } FOREACH_PCHAN_VISIBLE_IN_OBJECT_END;
352                                 } FOREACH_OBJECT_IN_MODE_END;
353                         }
354                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
355                         return 1;
356                 }
357         }
358         else if (CTX_data_equals(member, "selected_pose_bones")) {
359                 Object *obpose = BKE_object_pose_armature_get(obact);
360                 if (obpose && obpose->pose && obpose->data) {
361                         if (obpose != obact) {
362                                 FOREACH_PCHAN_SELECTED_IN_OBJECT_BEGIN (obpose, pchan) {
363                                         CTX_data_list_add(result, &obpose->id, &RNA_PoseBone, pchan);
364                                 } FOREACH_PCHAN_SELECTED_IN_OBJECT_END;
365                         }
366                         else if (obact->mode & OB_MODE_POSE) {
367                                 FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, OB_ARMATURE, OB_MODE_POSE, ob_iter) {
368                                         FOREACH_PCHAN_SELECTED_IN_OBJECT_BEGIN (ob_iter, pchan) {
369                                                 CTX_data_list_add(result, &ob_iter->id, &RNA_PoseBone, pchan);
370                                         } FOREACH_PCHAN_SELECTED_IN_OBJECT_END;
371                                 } FOREACH_OBJECT_IN_MODE_END;
372                         }
373                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
374                         return 1;
375                 }
376         }
377         else if (CTX_data_equals(member, "selected_pose_bones_from_active_object")) {
378                 Object *obpose = BKE_object_pose_armature_get(obact);
379                 if (obpose && obpose->pose && obpose->data) {
380                         if (obpose != obact) {
381                                 FOREACH_PCHAN_SELECTED_IN_OBJECT_BEGIN (obpose, pchan) {
382                                         CTX_data_list_add(result, &obpose->id, &RNA_PoseBone, pchan);
383                                 } FOREACH_PCHAN_SELECTED_IN_OBJECT_END;
384                         }
385                         else if (obact->mode & OB_MODE_POSE) {
386                                 FOREACH_PCHAN_SELECTED_IN_OBJECT_BEGIN (obact, pchan) {
387                                         CTX_data_list_add(result, &obact->id, &RNA_PoseBone, pchan);
388                                 } FOREACH_PCHAN_SELECTED_IN_OBJECT_END;
389                         }
390                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
391                         return 1;
392                 }
393         }
394         else if (CTX_data_equals(member, "active_bone")) {
395                 if (obact && obact->type == OB_ARMATURE) {
396                         bArmature *arm = obact->data;
397                         if (arm->edbo) {
398                                 if (arm->act_edbone) {
399                                         CTX_data_pointer_set(result, &arm->id, &RNA_EditBone, arm->act_edbone);
400                                         return 1;
401                                 }
402                         }
403                         else {
404                                 if (arm->act_bone) {
405                                         CTX_data_pointer_set(result, &arm->id, &RNA_Bone, arm->act_bone);
406                                         return 1;
407                                 }
408                         }
409                 }
410         }
411         else if (CTX_data_equals(member, "active_pose_bone")) {
412                 bPoseChannel *pchan;
413                 Object *obpose = BKE_object_pose_armature_get(obact);
414
415                 pchan = BKE_pose_channel_active(obpose);
416                 if (pchan) {
417                         CTX_data_pointer_set(result, &obpose->id, &RNA_PoseBone, pchan);
418                         return 1;
419                 }
420         }
421         else if (CTX_data_equals(member, "active_base")) {
422                 if (view_layer->basact)
423                         CTX_data_pointer_set(result, &scene->id, &RNA_ObjectBase, view_layer->basact);
424
425                 return 1;
426         }
427         else if (CTX_data_equals(member, "active_object")) {
428                 if (obact)
429                         CTX_data_id_pointer_set(result, &obact->id);
430
431                 return 1;
432         }
433         else if (CTX_data_equals(member, "object")) {
434                 if (obact)
435                         CTX_data_id_pointer_set(result, &obact->id);
436
437                 return 1;
438         }
439         else if (CTX_data_equals(member, "edit_object")) {
440                 /* convenience for now, 1 object per scene in editmode */
441                 if (obedit)
442                         CTX_data_id_pointer_set(result, &obedit->id);
443
444                 return 1;
445         }
446         else if (CTX_data_equals(member, "sculpt_object")) {
447                 if (obact && (obact->mode & OB_MODE_SCULPT))
448                         CTX_data_id_pointer_set(result, &obact->id);
449
450                 return 1;
451         }
452         else if (CTX_data_equals(member, "vertex_paint_object")) {
453                 if (obact && (obact->mode & OB_MODE_VERTEX_PAINT))
454                         CTX_data_id_pointer_set(result, &obact->id);
455
456                 return 1;
457         }
458         else if (CTX_data_equals(member, "weight_paint_object")) {
459                 if (obact && (obact->mode & OB_MODE_WEIGHT_PAINT))
460                         CTX_data_id_pointer_set(result, &obact->id);
461
462                 return 1;
463         }
464         else if (CTX_data_equals(member, "image_paint_object")) {
465                 if (obact && (obact->mode & OB_MODE_TEXTURE_PAINT))
466                         CTX_data_id_pointer_set(result, &obact->id);
467
468                 return 1;
469         }
470         else if (CTX_data_equals(member, "particle_edit_object")) {
471                 if (obact && (obact->mode & OB_MODE_PARTICLE_EDIT))
472                         CTX_data_id_pointer_set(result, &obact->id);
473
474                 return 1;
475         }
476         else if (CTX_data_equals(member, "uv_sculpt_object")) {
477                 /* TODO(campbell): most likely we change rules for uv_sculpt. */
478                 if (obact && (obact->mode & OB_MODE_EDIT)) {
479                         const ToolSettings *ts = scene->toolsettings;
480                         if (ts->use_uv_sculpt) {
481                                 if (ED_uvedit_test(obedit)) {
482                                         WorkSpace *workspace = CTX_wm_workspace(C);
483                                         if ((workspace->tools_space_type == SPACE_IMAGE) &&
484                                             (workspace->tools_mode == SI_MODE_UV))
485                                         {
486                                                 CTX_data_id_pointer_set(result, &obact->id);
487                                         }
488                                 }
489                         }
490                 }
491                 return 1;
492         }
493         else if (CTX_data_equals(member, "sequences")) {
494                 Editing *ed = BKE_sequencer_editing_get(scene, false);
495                 if (ed) {
496                         Sequence *seq;
497                         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
498                                 CTX_data_list_add(result, &scene->id, &RNA_Sequence, seq);
499                         }
500                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
501                         return 1;
502                 }
503         }
504         else if (CTX_data_equals(member, "selected_sequences")) {
505                 Editing *ed = BKE_sequencer_editing_get(scene, false);
506                 if (ed) {
507                         Sequence *seq;
508                         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
509                                 if (seq->flag & SELECT) {
510                                         CTX_data_list_add(result, &scene->id, &RNA_Sequence, seq);
511                                 }
512                         }
513                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
514                         return 1;
515                 }
516         }
517         else if (CTX_data_equals(member, "selected_editable_sequences")) {
518                 Editing *ed = BKE_sequencer_editing_get(scene, false);
519                 if (ed) {
520                         Sequence *seq;
521                         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
522                                 if (seq->flag & SELECT && !(seq->flag & SEQ_LOCK)) {
523                                         CTX_data_list_add(result, &scene->id, &RNA_Sequence, seq);
524                                 }
525                         }
526                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
527                         return 1;
528                 }
529         }
530         else if (CTX_data_equals(member, "gpencil_data")) {
531                 /* FIXME: for some reason, CTX_data_active_object(C) returns NULL when called from these situations
532                  * (as outlined above - see Campbell's #ifdefs). That causes the get_active function to fail when
533                  * called from context. For that reason, we end up using an alternative where we pass everything in!
534                  */
535                 bGPdata *gpd = ED_gpencil_data_get_active_direct((ID *)sc, sa, scene, obact);
536
537                 if (gpd) {
538                         CTX_data_id_pointer_set(result, &gpd->id);
539                         return 1;
540                 }
541         }
542         else if (CTX_data_equals(member, "gpencil_data_owner")) {
543                 /* pointer to which data/datablock owns the reference to the Grease Pencil data being used (as gpencil_data)
544                  * XXX: see comment for gpencil_data case...
545                  */
546                 bGPdata **gpd_ptr = NULL;
547                 PointerRNA ptr;
548
549                 /* get pointer to Grease Pencil Data */
550                 gpd_ptr = ED_gpencil_data_get_pointers_direct((ID *)sc, sa, scene, obact, &ptr);
551
552                 if (gpd_ptr) {
553                         CTX_data_pointer_set(result, ptr.id.data, ptr.type, ptr.data);
554                         return 1;
555                 }
556         }
557         else if (CTX_data_equals(member, "active_gpencil_layer")) {
558                 /* XXX: see comment for gpencil_data case... */
559                 bGPdata *gpd = ED_gpencil_data_get_active_direct((ID *)sc, sa, scene, obact);
560
561                 if (gpd) {
562                         bGPDlayer *gpl = BKE_gpencil_layer_getactive(gpd);
563
564                         if (gpl) {
565                                 CTX_data_pointer_set(result, &gpd->id, &RNA_GPencilLayer, gpl);
566                                 return 1;
567                         }
568                 }
569         }
570         else if (CTX_data_equals(member, "active_gpencil_frame")) {
571                 /* XXX: see comment for gpencil_data case... */
572                 bGPdata *gpd = ED_gpencil_data_get_active_direct((ID *)sc, sa, scene, obact);
573
574                 if (gpd) {
575                         bGPDlayer *gpl = BKE_gpencil_layer_getactive(gpd);
576
577                         if (gpl) {
578                                 CTX_data_pointer_set(result, &gpd->id, &RNA_GPencilLayer, gpl->actframe);
579                                 return 1;
580                         }
581                 }
582         }
583         else if (CTX_data_equals(member, "visible_gpencil_layers")) {
584                 /* XXX: see comment for gpencil_data case... */
585                 bGPdata *gpd = ED_gpencil_data_get_active_direct((ID *)sc, sa, scene, obact);
586
587                 if (gpd) {
588                         bGPDlayer *gpl;
589
590                         for (gpl = gpd->layers.first; gpl; gpl = gpl->next) {
591                                 if ((gpl->flag & GP_LAYER_HIDE) == 0) {
592                                         CTX_data_list_add(result, &gpd->id, &RNA_GPencilLayer, gpl);
593                                 }
594                         }
595                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
596                         return 1;
597                 }
598         }
599         else if (CTX_data_equals(member, "editable_gpencil_layers")) {
600                 /* XXX: see comment for gpencil_data case... */
601                 bGPdata *gpd = ED_gpencil_data_get_active_direct((ID *)sc, sa, scene, obact);
602
603                 if (gpd) {
604                         bGPDlayer *gpl;
605
606                         for (gpl = gpd->layers.first; gpl; gpl = gpl->next) {
607                                 if (gpencil_layer_is_editable(gpl)) {
608                                         CTX_data_list_add(result, &gpd->id, &RNA_GPencilLayer, gpl);
609                                 }
610                         }
611                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
612                         return 1;
613                 }
614         }
615         else if (CTX_data_equals(member, "editable_gpencil_strokes")) {
616                 /* XXX: see comment for gpencil_data case... */
617                 bGPdata *gpd = ED_gpencil_data_get_active_direct((ID *)sc, sa, scene, obact);
618                 const bool is_multiedit = (bool)GPENCIL_MULTIEDIT_SESSIONS_ON(gpd);
619
620                 if (gpd) {
621                         bGPDlayer *gpl;
622
623                         for (gpl = gpd->layers.first; gpl; gpl = gpl->next) {
624                                 if (gpencil_layer_is_editable(gpl) && (gpl->actframe)) {
625                                         bGPDframe *gpf;
626                                         bGPDstroke *gps;
627                                         bGPDframe *init_gpf = gpl->actframe;
628                                         if (is_multiedit) {
629                                                 init_gpf = gpl->frames.first;
630                                         }
631
632                                         for (gpf = init_gpf; gpf; gpf = gpf->next) {
633                                                 if ((gpf == gpl->actframe) || ((gpf->flag & GP_FRAME_SELECT) && (is_multiedit))) {
634                                                         for (gps = gpf->strokes.first; gps; gps = gps->next) {
635                                                                 if (ED_gpencil_stroke_can_use_direct(sa, gps)) {
636                                                                         /* check if the color is editable */
637                                                                         if (ED_gpencil_stroke_color_use(obact, gpl, gps) == false) {
638                                                                                 continue;
639                                                                         }
640
641                                                                         CTX_data_list_add(result, &gpd->id, &RNA_GPencilStroke, gps);
642                                                                 }
643                                                         }
644                                                 }
645                                                 /* if not multiedit out of loop */
646                                                 if (!is_multiedit) {
647                                                         break;
648                                                 }
649                                         }
650                                 }
651                         }
652                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
653                         return 1;
654                 }
655         }
656         else if (CTX_data_equals(member, "active_operator")) {
657                 wmOperator *op = NULL;
658
659                 SpaceFile *sfile = CTX_wm_space_file(C);
660                 if (sfile) {
661                         op = sfile->op;
662                 }
663                 else if ((op = UI_context_active_operator_get(C))) {
664                         /* do nothing */
665                 }
666                 else {
667                         /* note, this checks poll, could be a problem, but this also
668                          * happens for the toolbar */
669                         op = WM_operator_last_redo(C);
670                 }
671                 /* TODO, get the operator from popup's */
672
673                 if (op && op->ptr) {
674                         CTX_data_pointer_set(result, NULL, &RNA_Operator, op);
675                         return 1;
676                 }
677         }
678         else if (CTX_data_equals(member, "selected_editable_fcurves")) {
679                 bAnimContext ac;
680
681                 if (ANIM_animdata_get_context(C, &ac) && ELEM(ac.spacetype, SPACE_ACTION, SPACE_IPO)) {
682                         bAnimListElem *ale;
683                         ListBase anim_data = {NULL, NULL};
684
685                         int filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_NODUPLIS | ANIMFILTER_SEL) |
686                                      (ac.spacetype == SPACE_IPO ? ANIMFILTER_CURVE_VISIBLE : ANIMFILTER_LIST_VISIBLE);
687
688                         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
689
690                         for (ale = anim_data.first; ale; ale = ale->next) {
691                                 if (ale->type == ANIMTYPE_FCURVE)
692                                         CTX_data_list_add(result, ale->id, &RNA_FCurve, ale->data);
693                         }
694
695                         ANIM_animdata_freelist(&anim_data);
696
697                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
698                         return 1;
699                 }
700         }
701         else {
702                 return 0; /* not found */
703         }
704
705         return -1; /* found but not available */
706 }