1c570d98744d94d9be97fdb48b1557b72609ce9e
[blender-staging.git] / source / blender / editors / screen / screen_context.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "DNA_object_types.h"
31 #include "DNA_armature_types.h"
32 #include "DNA_sequence_types.h"
33 #include "DNA_scene_types.h"
34 #include "DNA_screen_types.h"
35
36 #include "BKE_context.h"
37 #include "BKE_utildefines.h"
38 #include "BKE_global.h"
39 #include "BKE_action.h"
40 #include "BKE_sequencer.h"
41
42 #include "RNA_access.h"
43
44 #include "ED_object.h"
45 #include "ED_armature.h"
46
47 int ed_screen_context(const bContext *C, const char *member, bContextDataResult *result)
48 {
49         bScreen *sc= CTX_wm_screen(C);
50         Scene *scene= sc->scene;
51         Base *base;
52
53 #if 0   /* Using the context breaks adding objects in the UI. Need to find out why - campbell */
54         Object *obact= CTX_data_active_object(C);
55         Object *obedit= CTX_data_edit_object(C);
56         base= CTX_data_active_base(C);
57 #else
58         Object *obedit= scene->obedit; 
59         Object *obact= OBACT;
60         base= BASACT;
61 #endif
62
63         if(CTX_data_dir(member)) {
64                 static const char *dir[] = {
65                         "scene", "visible_objects", "visible_bases", "selected_objects", "selected_bases",
66                         "selected_editable_objects", "selected_editable_bases",
67                         "visible_bones", "editable_bones", "selected_bones", "selected_editable_bones",
68                         "visible_pose_bones", "selected_pose_bones", "active_bone", "active_pose_bone",
69                         "active_base", "active_object", "object", "edit_object",
70                         "sculpt_object", "vertex_paint_object", "weight_paint_object",
71                         "texture_paint_object", "particle_edit_object",
72                         "sequences", "selected_sequences", "selected_editable_sequences", /* sequencer */
73                         NULL};
74
75                 CTX_data_dir_set(result, dir);
76                 return 1;
77         }
78         else if(CTX_data_equals(member, "scene")) {
79                 CTX_data_id_pointer_set(result, &scene->id);
80                 return 1;
81         }
82         else if(CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
83                 int visible_objects= CTX_data_equals(member, "visible_objects");
84
85                 for(base=scene->base.first; base; base=base->next) {
86                         if(((base->object->restrictflag & OB_RESTRICT_VIEW) == 0) && (base->lay & scene->lay)) {
87                                 if(visible_objects)
88                                         CTX_data_id_list_add(result, &base->object->id);
89                                 else
90                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
91                         }
92                 }
93                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
94                 return 1;
95         }
96         else if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
97                 int selected_objects= CTX_data_equals(member, "selected_objects");
98
99                 for(base=scene->base.first; base; base=base->next) {
100                         if((base->flag & SELECT) && (base->lay & scene->lay)) {
101                                 if(selected_objects)
102                                         CTX_data_id_list_add(result, &base->object->id);
103                                 else
104                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
105                         }
106                 }
107                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
108                 return 1;
109         }
110         else if(CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
111                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_objects");
112
113                 for(base=scene->base.first; base; base=base->next) {
114                         if((base->flag & SELECT) && (base->lay & scene->lay)) {
115                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
116                                         if(0==object_is_libdata(base->object)) {
117                                                 if(selected_editable_objects)
118                                                         CTX_data_id_list_add(result, &base->object->id);
119                                                 else
120                                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
121                                         }
122                                 }
123                         }
124                 }
125                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
126                 return 1;
127         }
128         else if(CTX_data_equals(member, "visible_bones") || CTX_data_equals(member, "editable_bones")) {
129                 bArmature *arm= (obedit) ? obedit->data : NULL;
130                 EditBone *ebone, *flipbone=NULL;
131                 int editable_bones= CTX_data_equals(member, "editable_bones");
132                 
133                 if (arm && arm->edbo) {
134                         /* Attention: X-Axis Mirroring is also handled here... */
135                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
136                                 /* first and foremost, bone must be visible and selected */
137                                 if (EBONE_VISIBLE(arm, ebone)) {
138                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
139                                          * so that most users of this data don't need to explicitly check for it themselves.
140                                          * 
141                                          * We need to make sure that these mirrored copies are not selected, otherwise some
142                                          * bones will be operated on twice.
143                                          */
144                                         if (arm->flag & ARM_MIRROR_EDIT)
145                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
146                                         
147                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
148                                         if (editable_bones) {
149                                                 /* only selected + editable */
150                                                 if (EBONE_EDITABLE(ebone)) {
151                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
152                                                 
153                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
154                                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
155                                                 }
156                                         }
157                                         else {
158                                                 /* only include bones if visible */
159                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
160                                                 
161                                                 if ((flipbone) && EBONE_VISIBLE(arm, flipbone)==0)
162                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
163                                         }
164                                 }
165                         }       
166                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
167                         return 1;
168                 }
169         }
170         else if(CTX_data_equals(member, "selected_bones") || CTX_data_equals(member, "selected_editable_bones")) {
171                 bArmature *arm= (obedit) ? obedit->data : NULL;
172                 EditBone *ebone, *flipbone=NULL;
173                 int selected_editable_bones= CTX_data_equals(member, "selected_editable_bones");
174                 
175                 if (arm && arm->edbo) {
176                         /* Attention: X-Axis Mirroring is also handled here... */
177                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
178                                 /* first and foremost, bone must be visible and selected */
179                                 if (EBONE_VISIBLE(arm, ebone) && (ebone->flag & BONE_SELECTED)) {
180                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
181                                          * so that most users of this data don't need to explicitly check for it themselves.
182                                          * 
183                                          * We need to make sure that these mirrored copies are not selected, otherwise some
184                                          * bones will be operated on twice.
185                                          */
186                                         if (arm->flag & ARM_MIRROR_EDIT)
187                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
188                                         
189                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
190                                         if (selected_editable_bones) {
191                                                 /* only selected + editable */
192                                                 if (EBONE_EDITABLE(ebone)) {
193                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
194                                                 
195                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
196                                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
197                                                 }
198                                         }
199                                         else {
200                                                 /* only include bones if selected */
201                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
202                                                 
203                                                 if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
204                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
205                                         }
206                                 }
207                         }       
208                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
209                         return 1;
210                 }
211         }
212         else if(CTX_data_equals(member, "visible_pose_bones")) {
213                 bArmature *arm= (obact) ? obact->data : NULL;
214                 bPoseChannel *pchan;
215                 
216                 if (obact && obact->pose && arm) {
217                         for (pchan= obact->pose->chanbase.first; pchan; pchan= pchan->next) {
218                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
219                                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
220                                         CTX_data_list_add(result, &obact->id, &RNA_PoseBone, pchan);
221                                 }
222                         }
223                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
224                         return 1;
225                 }
226         }
227         else if(CTX_data_equals(member, "selected_pose_bones")) {
228                 bArmature *arm= (obact) ? obact->data : NULL;
229                 bPoseChannel *pchan;
230                 
231                 if (obact && obact->pose && arm) {
232                         for (pchan= obact->pose->chanbase.first; pchan; pchan= pchan->next) {
233                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
234                                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
235                                         if (pchan->bone->flag & BONE_SELECTED || pchan->bone == arm->act_bone)
236                                                 CTX_data_list_add(result, &obact->id, &RNA_PoseBone, pchan);
237                                 }
238                         }
239                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
240                         return 1;
241                 }
242         }
243         else if(CTX_data_equals(member, "active_bone")) {
244                 if(obact && obact->type == OB_ARMATURE) {
245                         bArmature *arm= obact->data;
246                         if(arm->edbo) {
247                                 if(arm->act_edbone) {
248                                         CTX_data_pointer_set(result, &arm->id, &RNA_EditBone, arm->act_edbone);
249                                         return 1;
250                                 }
251                         }
252                         else {
253                                 if(arm->act_bone) {
254                                         CTX_data_pointer_set(result, &arm->id, &RNA_Bone, arm->act_bone);
255                                         return 1;
256                                 }
257                         }
258                 }
259         }
260         else if(CTX_data_equals(member, "active_pose_bone")) {
261                 bPoseChannel *pchan;
262                 
263                 pchan= get_active_posechannel(obact);
264                 if (pchan) {
265                         CTX_data_pointer_set(result, &obact->id, &RNA_PoseBone, pchan);
266                         return 1;
267                 }
268         }
269         else if(CTX_data_equals(member, "active_base")) {
270                 if(base)
271                         CTX_data_pointer_set(result, &scene->id, &RNA_ObjectBase, base);
272
273                 return 1;
274         }
275         else if(CTX_data_equals(member, "active_object")) {
276                 if(obact)
277                         CTX_data_id_pointer_set(result, &obact->id);
278
279                 return 1;
280         }
281         else if(CTX_data_equals(member, "object")) {
282                 if(obact)
283                         CTX_data_id_pointer_set(result, &obact->id);
284
285                 return 1;
286         }
287         else if(CTX_data_equals(member, "edit_object")) {
288                 /* convenience for now, 1 object per scene in editmode */
289                 if(obedit)
290                         CTX_data_id_pointer_set(result, &obedit->id);
291                 
292                 return 1;
293         }
294         else if(CTX_data_equals(member, "sculpt_object")) {
295                 if(obact && (obact->mode & OB_MODE_SCULPT))
296                         CTX_data_id_pointer_set(result, &obact->id);
297
298                 return 1;
299         }
300         else if(CTX_data_equals(member, "vertex_paint_object")) {
301                 if(obact && (obact->mode & OB_MODE_VERTEX_PAINT))
302                         CTX_data_id_pointer_set(result, &obact->id);
303
304                 return 1;
305         }
306         else if(CTX_data_equals(member, "weight_paint_object")) {
307                 if(obact && (obact->mode & OB_MODE_WEIGHT_PAINT))
308                         CTX_data_id_pointer_set(result, &obact->id);
309
310                 return 1;
311         }
312         else if(CTX_data_equals(member, "texture_paint_object")) {
313                 if(obact && (obact->mode & OB_MODE_TEXTURE_PAINT))
314                         CTX_data_id_pointer_set(result, &obact->id);
315
316                 return 1;
317         }
318         else if(CTX_data_equals(member, "particle_edit_object")) {
319                 if(obact && (obact->mode & OB_MODE_PARTICLE_EDIT))
320                         CTX_data_id_pointer_set(result, &obact->id);
321
322                 return 1;
323         }
324         else if(CTX_data_equals(member, "sequences")) {
325                 Editing *ed= seq_give_editing(scene, FALSE);
326                 if(ed) {
327                         Sequence *seq;
328                         for (seq= ed->seqbasep->first; seq; seq= seq->next) {
329                                 CTX_data_list_add(result, &scene->id, &RNA_Sequence, seq);
330                         }
331                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
332                         return 1;
333                 }
334         }
335         else if(CTX_data_equals(member, "selected_sequences")) {
336                 Editing *ed= seq_give_editing(scene, FALSE);
337                 if(ed) {
338                         Sequence *seq;
339                         for (seq= ed->seqbasep->first; seq; seq= seq->next) {
340                                 if (seq->flag & SELECT) {
341                                         CTX_data_list_add(result, &scene->id, &RNA_Sequence, seq);
342                                 }
343                         }
344                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
345                         return 1;
346                 }
347         }
348         else if(CTX_data_equals(member, "selected_editable_sequences")) {
349                 Editing *ed= seq_give_editing(scene, FALSE);
350                 if(ed) {
351                         Sequence *seq;
352                         for (seq= ed->seqbasep->first; seq; seq= seq->next) {
353                                 if (seq->flag & SELECT && !(seq->flag & SEQ_LOCK)) {
354                                         CTX_data_list_add(result, &scene->id, &RNA_Sequence, seq);
355                                 }
356                         }
357                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
358                         return 1;
359                 }
360         }
361         else {
362                 return 0; /* not found */
363         }
364
365         return -1; /* found but not available */
366 }
367