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