- dont create scene.sequence_editor on read, means data-browser or autocomplete will...
[blender-staging.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
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "DNA_object_types.h"
34 #include "DNA_armature_types.h"
35 #include "DNA_sequence_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_space_types.h"
39 #include "DNA_windowmanager_types.h"
40
41 #include "BLI_utildefines.h"
42
43
44 #include "BKE_context.h"
45 #include "BKE_object.h"
46 #include "BKE_action.h"
47 #include "BKE_armature.h"
48 #include "BKE_sequencer.h"
49
50 #include "RNA_access.h"
51
52 #include "ED_object.h"
53 #include "ED_armature.h"
54
55 #include "WM_api.h"
56 #include "UI_interface.h"
57
58 #include "screen_intern.h"
59
60 const char *screen_context_dir[] = {
61         "scene", "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
62         "selected_objects", "selected_bases",
63         "selected_editable_objects", "selected_editable_bases",
64         "visible_bones", "editable_bones", "selected_bones", "selected_editable_bones",
65         "visible_pose_bones", "selected_pose_bones", "active_bone", "active_pose_bone",
66         "active_base", "active_object", "object", "edit_object",
67         "sculpt_object", "vertex_paint_object", "weight_paint_object",
68         "image_paint_object", "particle_edit_object",
69         "sequences", "selected_sequences", "selected_editable_sequences", /* sequencer */
70         "active_operator",
71         NULL};
72
73 int ed_screen_context(const bContext *C, const char *member, bContextDataResult *result)
74 {
75         bScreen *sc = CTX_wm_screen(C);
76         Scene *scene = sc->scene;
77         Base *base;
78         unsigned int lay = scene->lay;
79
80 #if 0  /* Using the context breaks adding objects in the UI. Need to find out why - campbell */
81         Object *obact = CTX_data_active_object(C);
82         Object *obedit = CTX_data_edit_object(C);
83         base = CTX_data_active_base(C);
84 #else
85         Object *obedit = scene->obedit;
86         Object *obact = OBACT;
87         base = BASACT;
88 #endif
89
90         if (CTX_data_dir(member)) {
91                 CTX_data_dir_set(result, screen_context_dir);
92                 return 1;
93         }
94         else if (CTX_data_equals(member, "scene")) {
95                 CTX_data_id_pointer_set(result, &scene->id);
96                 return 1;
97         }
98         else if (CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
99                 int visible_objects = CTX_data_equals(member, "visible_objects");
100
101                 for (base = scene->base.first; base; base = base->next) {
102                         if (((base->object->restrictflag & OB_RESTRICT_VIEW) == 0) && (base->lay & scene->lay)) {
103                                 if (visible_objects)
104                                         CTX_data_id_list_add(result, &base->object->id);
105                                 else
106                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
107                         }
108                 }
109                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
110                 return 1;
111         }
112         else if (CTX_data_equals(member, "selectable_objects") || CTX_data_equals(member, "selectable_bases")) {
113                 int selectable_objects = CTX_data_equals(member, "selectable_objects");
114
115                 for (base = scene->base.first; base; base = base->next) {
116                         if (base->lay & lay) {
117                                 if ((base->object->restrictflag & OB_RESTRICT_VIEW) == 0 && (base->object->restrictflag & OB_RESTRICT_SELECT) == 0) {
118                                         if (selectable_objects)
119                                                 CTX_data_id_list_add(result, &base->object->id);
120                                         else
121                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
122                                 }
123                         }
124                 }
125                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
126                 return 1;
127         }
128         else if (CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
129                 int selected_objects = CTX_data_equals(member, "selected_objects");
130
131                 for (base = scene->base.first; base; base = base->next) {
132                         if ((base->flag & SELECT) && (base->lay & scene->lay)) {
133                                 if (selected_objects)
134                                         CTX_data_id_list_add(result, &base->object->id);
135                                 else
136                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
137                         }
138                 }
139                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
140                 return 1;
141         }
142         else if (CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
143                 int selected_editable_objects = CTX_data_equals(member, "selected_editable_objects");
144
145                 for (base = scene->base.first; base; base = base->next) {
146                         if ((base->flag & SELECT) && (base->lay & scene->lay)) {
147                                 if ((base->object->restrictflag & OB_RESTRICT_VIEW) == 0) {
148                                         if (0 == BKE_object_is_libdata(base->object)) {
149                                                 if (selected_editable_objects)
150                                                         CTX_data_id_list_add(result, &base->object->id);
151                                                 else
152                                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
153                                         }
154                                 }
155                         }
156                 }
157                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
158                 return 1;
159         }
160         else if (CTX_data_equals(member, "visible_bones") || CTX_data_equals(member, "editable_bones")) {
161                 bArmature *arm = (obedit && obedit->type == OB_ARMATURE) ? obedit->data : NULL;
162                 EditBone *ebone, *flipbone = NULL;
163                 int editable_bones = CTX_data_equals(member, "editable_bones");
164                 
165                 if (arm && arm->edbo) {
166                         /* Attention: X-Axis Mirroring is also handled here... */
167                         for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
168                                 /* first and foremost, bone must be visible and selected */
169                                 if (EBONE_VISIBLE(arm, ebone)) {
170                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
171                                          * so that most users of this data don't need to explicitly check for it themselves.
172                                          * 
173                                          * We need to make sure that these mirrored copies are not selected, otherwise some
174                                          * bones will be operated on twice.
175                                          */
176                                         if (arm->flag & ARM_MIRROR_EDIT)
177                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
178                                         
179                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
180                                         if (editable_bones) {
181                                                 /* only selected + editable */
182                                                 if (EBONE_EDITABLE(ebone)) {
183                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
184                                                 
185                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
186                                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
187                                                 }
188                                         }
189                                         else {
190                                                 /* only include bones if visible */
191                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
192                                                 
193                                                 if ((flipbone) && EBONE_VISIBLE(arm, flipbone) == 0)
194                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
195                                         }
196                                 }
197                         }       
198                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
199                         return 1;
200                 }
201         }
202         else if (CTX_data_equals(member, "selected_bones") || CTX_data_equals(member, "selected_editable_bones")) {
203                 bArmature *arm = (obedit && obedit->type == OB_ARMATURE) ? obedit->data : NULL;
204                 EditBone *ebone, *flipbone = NULL;
205                 int selected_editable_bones = CTX_data_equals(member, "selected_editable_bones");
206                 
207                 if (arm && arm->edbo) {
208                         /* Attention: X-Axis Mirroring is also handled here... */
209                         for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
210                                 /* first and foremost, bone must be visible and selected */
211                                 if (EBONE_VISIBLE(arm, ebone) && (ebone->flag & BONE_SELECTED)) {
212                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
213                                          * so that most users of this data don't need to explicitly check for it themselves.
214                                          * 
215                                          * We need to make sure that these mirrored copies are not selected, otherwise some
216                                          * bones will be operated on twice.
217                                          */
218                                         if (arm->flag & ARM_MIRROR_EDIT)
219                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
220                                         
221                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
222                                         if (selected_editable_bones) {
223                                                 /* only selected + editable */
224                                                 if (EBONE_EDITABLE(ebone)) {
225                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
226                                                 
227                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
228                                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
229                                                 }
230                                         }
231                                         else {
232                                                 /* only include bones if selected */
233                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
234                                                 
235                                                 if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
236                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
237                                         }
238                                 }
239                         }       
240                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
241                         return 1;
242                 }
243         }
244         else if (CTX_data_equals(member, "visible_pose_bones")) {
245                 Object *obpose = BKE_object_pose_armature_get(obact);
246                 bArmature *arm = (obpose) ? obpose->data : NULL;
247                 bPoseChannel *pchan;
248                 
249                 if (obpose && obpose->pose && arm) {
250                         for (pchan = obpose->pose->chanbase.first; pchan; pchan = pchan->next) {
251                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
252                                 if (PBONE_VISIBLE(arm, pchan->bone)) {
253                                         CTX_data_list_add(result, &obpose->id, &RNA_PoseBone, pchan);
254                                 }
255                         }
256                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
257                         return 1;
258                 }
259         }
260         else if (CTX_data_equals(member, "selected_pose_bones")) {
261                 Object *obpose = BKE_object_pose_armature_get(obact);
262                 bArmature *arm = (obpose) ? obpose->data : NULL;
263                 bPoseChannel *pchan;
264                 
265                 if (obpose && obpose->pose && arm) {
266                         for (pchan = obpose->pose->chanbase.first; pchan; pchan = pchan->next) {
267                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
268                                 if (PBONE_VISIBLE(arm, pchan->bone)) {
269                                         if (pchan->bone->flag & BONE_SELECTED)
270                                                 CTX_data_list_add(result, &obpose->id, &RNA_PoseBone, pchan);
271                                 }
272                         }
273                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
274                         return 1;
275                 }
276         }
277         else if (CTX_data_equals(member, "active_bone")) {
278                 if (obact && obact->type == OB_ARMATURE) {
279                         bArmature *arm = obact->data;
280                         if (arm->edbo) {
281                                 if (arm->act_edbone) {
282                                         CTX_data_pointer_set(result, &arm->id, &RNA_EditBone, arm->act_edbone);
283                                         return 1;
284                                 }
285                         }
286                         else {
287                                 if (arm->act_bone) {
288                                         CTX_data_pointer_set(result, &arm->id, &RNA_Bone, arm->act_bone);
289                                         return 1;
290                                 }
291                         }
292                 }
293         }
294         else if (CTX_data_equals(member, "active_pose_bone")) {
295                 bPoseChannel *pchan;
296                 Object *obpose = BKE_object_pose_armature_get(obact);
297                 
298                 pchan = BKE_pose_channel_active(obpose);
299                 if (pchan) {
300                         CTX_data_pointer_set(result, &obpose->id, &RNA_PoseBone, pchan);
301                         return 1;
302                 }
303         }
304         else if (CTX_data_equals(member, "active_base")) {
305                 if (base)
306                         CTX_data_pointer_set(result, &scene->id, &RNA_ObjectBase, base);
307
308                 return 1;
309         }
310         else if (CTX_data_equals(member, "active_object")) {
311                 if (obact)
312                         CTX_data_id_pointer_set(result, &obact->id);
313
314                 return 1;
315         }
316         else if (CTX_data_equals(member, "object")) {
317                 if (obact)
318                         CTX_data_id_pointer_set(result, &obact->id);
319
320                 return 1;
321         }
322         else if (CTX_data_equals(member, "edit_object")) {
323                 /* convenience for now, 1 object per scene in editmode */
324                 if (obedit)
325                         CTX_data_id_pointer_set(result, &obedit->id);
326                 
327                 return 1;
328         }
329         else if (CTX_data_equals(member, "sculpt_object")) {
330                 if (obact && (obact->mode & OB_MODE_SCULPT))
331                         CTX_data_id_pointer_set(result, &obact->id);
332
333                 return 1;
334         }
335         else if (CTX_data_equals(member, "vertex_paint_object")) {
336                 if (obact && (obact->mode & OB_MODE_VERTEX_PAINT))
337                         CTX_data_id_pointer_set(result, &obact->id);
338
339                 return 1;
340         }
341         else if (CTX_data_equals(member, "weight_paint_object")) {
342                 if (obact && (obact->mode & OB_MODE_WEIGHT_PAINT))
343                         CTX_data_id_pointer_set(result, &obact->id);
344
345                 return 1;
346         }
347         else if (CTX_data_equals(member, "image_paint_object")) {
348                 if (obact && (obact->mode & OB_MODE_TEXTURE_PAINT))
349                         CTX_data_id_pointer_set(result, &obact->id);
350
351                 return 1;
352         }
353         else if (CTX_data_equals(member, "particle_edit_object")) {
354                 if (obact && (obact->mode & OB_MODE_PARTICLE_EDIT))
355                         CTX_data_id_pointer_set(result, &obact->id);
356
357                 return 1;
358         }
359         else if (CTX_data_equals(member, "sequences")) {
360                 Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
361                 if (ed) {
362                         Sequence *seq;
363                         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
364                                 CTX_data_list_add(result, &scene->id, &RNA_Sequence, seq);
365                         }
366                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
367                         return 1;
368                 }
369         }
370         else if (CTX_data_equals(member, "selected_sequences")) {
371                 Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
372                 if (ed) {
373                         Sequence *seq;
374                         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
375                                 if (seq->flag & SELECT) {
376                                         CTX_data_list_add(result, &scene->id, &RNA_Sequence, seq);
377                                 }
378                         }
379                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
380                         return 1;
381                 }
382         }
383         else if (CTX_data_equals(member, "selected_editable_sequences")) {
384                 Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
385                 if (ed) {
386                         Sequence *seq;
387                         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
388                                 if (seq->flag & SELECT && !(seq->flag & SEQ_LOCK)) {
389                                         CTX_data_list_add(result, &scene->id, &RNA_Sequence, seq);
390                                 }
391                         }
392                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
393                         return 1;
394                 }
395         }
396         else if (CTX_data_equals(member, "active_operator")) {
397                 wmOperator *op = NULL;
398
399                 SpaceFile *sfile = CTX_wm_space_file(C);
400                 if (sfile) {
401                         op = sfile->op;
402                 }
403                 else if ((op = uiContextActiveOperator(C))) {
404                         /* do nothign */
405                 }
406                 else {
407                         /* note, this checks poll, could be a problem, but this also
408                          * happens for the toolbar */
409                         op = WM_operator_last_redo(C);
410                 }
411                 /* TODO, get the operator from popup's */
412
413                 if (op && op->ptr) {
414                         CTX_data_pointer_set(result, NULL, &RNA_Operator, op);
415                         return 1;
416                 }
417         }
418         else {
419                 return 0; /* not found */
420         }
421
422         return -1; /* found but not available */
423 }
424