remove unused includes
[blender.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_action.h"
39 #include "BKE_sequencer.h"
40
41 #include "RNA_access.h"
42
43 #include "ED_object.h"
44 #include "ED_armature.h"
45
46 int ed_screen_context(const bContext *C, const char *member, bContextDataResult *result)
47 {
48         bScreen *sc= CTX_wm_screen(C);
49         Scene *scene= sc->scene;
50         Base *base;
51
52 #if 0   /* Using the context breaks adding objects in the UI. Need to find out why - campbell */
53         Object *obact= CTX_data_active_object(C);
54         Object *obedit= CTX_data_edit_object(C);
55         base= CTX_data_active_base(C);
56 #else
57         Object *obedit= scene->obedit; 
58         Object *obact= OBACT;
59         base= BASACT;
60 #endif
61
62         if(CTX_data_dir(member)) {
63                 static const char *dir[] = {
64                         "scene", "visible_objects", "visible_bases", "selected_objects", "selected_bases",
65                         "selected_editable_objects", "selected_editable_bases",
66                         "visible_bones", "editable_bones", "selected_bones", "selected_editable_bones",
67                         "visible_pose_bones", "selected_pose_bones", "active_bone", "active_pose_bone",
68                         "active_base", "active_object", "object", "edit_object",
69                         "sculpt_object", "vertex_paint_object", "weight_paint_object",
70                         "texture_paint_object", "particle_edit_object",
71                         "sequences", "selected_sequences", "selected_editable_sequences", /* sequencer */
72                         NULL};
73
74                 CTX_data_dir_set(result, dir);
75                 return 1;
76         }
77         else if(CTX_data_equals(member, "scene")) {
78                 CTX_data_id_pointer_set(result, &scene->id);
79                 return 1;
80         }
81         else if(CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
82                 int visible_objects= CTX_data_equals(member, "visible_objects");
83
84                 for(base=scene->base.first; base; base=base->next) {
85                         if(((base->object->restrictflag & OB_RESTRICT_VIEW) == 0) && (base->lay & scene->lay)) {
86                                 if(visible_objects)
87                                         CTX_data_id_list_add(result, &base->object->id);
88                                 else
89                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
90                         }
91                 }
92                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
93                 return 1;
94         }
95         else if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
96                 int selected_objects= CTX_data_equals(member, "selected_objects");
97
98                 for(base=scene->base.first; base; base=base->next) {
99                         if((base->flag & SELECT) && (base->lay & scene->lay)) {
100                                 if(selected_objects)
101                                         CTX_data_id_list_add(result, &base->object->id);
102                                 else
103                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
104                         }
105                 }
106                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
107                 return 1;
108         }
109         else if(CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
110                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_objects");
111
112                 for(base=scene->base.first; base; base=base->next) {
113                         if((base->flag & SELECT) && (base->lay & scene->lay)) {
114                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
115                                         if(0==object_is_libdata(base->object)) {
116                                                 if(selected_editable_objects)
117                                                         CTX_data_id_list_add(result, &base->object->id);
118                                                 else
119                                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
120                                         }
121                                 }
122                         }
123                 }
124                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
125                 return 1;
126         }
127         else if(CTX_data_equals(member, "visible_bones") || CTX_data_equals(member, "editable_bones")) {
128                 bArmature *arm= (obedit) ? obedit->data : NULL;
129                 EditBone *ebone, *flipbone=NULL;
130                 int editable_bones= CTX_data_equals(member, "editable_bones");
131                 
132                 if (arm && arm->edbo) {
133                         /* Attention: X-Axis Mirroring is also handled here... */
134                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
135                                 /* first and foremost, bone must be visible and selected */
136                                 if (EBONE_VISIBLE(arm, ebone)) {
137                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
138                                          * so that most users of this data don't need to explicitly check for it themselves.
139                                          * 
140                                          * We need to make sure that these mirrored copies are not selected, otherwise some
141                                          * bones will be operated on twice.
142                                          */
143                                         if (arm->flag & ARM_MIRROR_EDIT)
144                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
145                                         
146                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
147                                         if (editable_bones) {
148                                                 /* only selected + editable */
149                                                 if (EBONE_EDITABLE(ebone)) {
150                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
151                                                 
152                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
153                                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
154                                                 }
155                                         }
156                                         else {
157                                                 /* only include bones if visible */
158                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
159                                                 
160                                                 if ((flipbone) && EBONE_VISIBLE(arm, flipbone)==0)
161                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
162                                         }
163                                 }
164                         }       
165                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
166                         return 1;
167                 }
168         }
169         else if(CTX_data_equals(member, "selected_bones") || CTX_data_equals(member, "selected_editable_bones")) {
170                 bArmature *arm= (obedit) ? obedit->data : NULL;
171                 EditBone *ebone, *flipbone=NULL;
172                 int selected_editable_bones= CTX_data_equals(member, "selected_editable_bones");
173                 
174                 if (arm && arm->edbo) {
175                         /* Attention: X-Axis Mirroring is also handled here... */
176                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
177                                 /* first and foremost, bone must be visible and selected */
178                                 if (EBONE_VISIBLE(arm, ebone) && (ebone->flag & BONE_SELECTED)) {
179                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
180                                          * so that most users of this data don't need to explicitly check for it themselves.
181                                          * 
182                                          * We need to make sure that these mirrored copies are not selected, otherwise some
183                                          * bones will be operated on twice.
184                                          */
185                                         if (arm->flag & ARM_MIRROR_EDIT)
186                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
187                                         
188                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
189                                         if (selected_editable_bones) {
190                                                 /* only selected + editable */
191                                                 if (EBONE_EDITABLE(ebone)) {
192                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
193                                                 
194                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
195                                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
196                                                 }
197                                         }
198                                         else {
199                                                 /* only include bones if selected */
200                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
201                                                 
202                                                 if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
203                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
204                                         }
205                                 }
206                         }       
207                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
208                         return 1;
209                 }
210         }
211         else if(CTX_data_equals(member, "visible_pose_bones")) {
212                 bArmature *arm= (obact) ? obact->data : NULL;
213                 bPoseChannel *pchan;
214                 
215                 if (obact && obact->pose && arm) {
216                         for (pchan= obact->pose->chanbase.first; pchan; pchan= pchan->next) {
217                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
218                                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
219                                         CTX_data_list_add(result, &obact->id, &RNA_PoseBone, pchan);
220                                 }
221                         }
222                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
223                         return 1;
224                 }
225         }
226         else if(CTX_data_equals(member, "selected_pose_bones")) {
227                 bArmature *arm= (obact) ? obact->data : NULL;
228                 bPoseChannel *pchan;
229                 
230                 if (obact && obact->pose && arm) {
231                         for (pchan= obact->pose->chanbase.first; pchan; pchan= pchan->next) {
232                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
233                                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
234                                         if (pchan->bone->flag & BONE_SELECTED || pchan->bone == arm->act_bone)
235                                                 CTX_data_list_add(result, &obact->id, &RNA_PoseBone, pchan);
236                                 }
237                         }
238                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
239                         return 1;
240                 }
241         }
242         else if(CTX_data_equals(member, "active_bone")) {
243                 if(obact && obact->type == OB_ARMATURE) {
244                         bArmature *arm= obact->data;
245                         if(arm->edbo) {
246                                 if(arm->act_edbone) {
247                                         CTX_data_pointer_set(result, &arm->id, &RNA_EditBone, arm->act_edbone);
248                                         return 1;
249                                 }
250                         }
251                         else {
252                                 if(arm->act_bone) {
253                                         CTX_data_pointer_set(result, &arm->id, &RNA_Bone, arm->act_bone);
254                                         return 1;
255                                 }
256                         }
257                 }
258         }
259         else if(CTX_data_equals(member, "active_pose_bone")) {
260                 bPoseChannel *pchan;
261                 
262                 pchan= get_active_posechannel(obact);
263                 if (pchan) {
264                         CTX_data_pointer_set(result, &obact->id, &RNA_PoseBone, pchan);
265                         return 1;
266                 }
267         }
268         else if(CTX_data_equals(member, "active_base")) {
269                 if(base)
270                         CTX_data_pointer_set(result, &scene->id, &RNA_ObjectBase, base);
271
272                 return 1;
273         }
274         else if(CTX_data_equals(member, "active_object")) {
275                 if(obact)
276                         CTX_data_id_pointer_set(result, &obact->id);
277
278                 return 1;
279         }
280         else if(CTX_data_equals(member, "object")) {
281                 if(obact)
282                         CTX_data_id_pointer_set(result, &obact->id);
283
284                 return 1;
285         }
286         else if(CTX_data_equals(member, "edit_object")) {
287                 /* convenience for now, 1 object per scene in editmode */
288                 if(obedit)
289                         CTX_data_id_pointer_set(result, &obedit->id);
290                 
291                 return 1;
292         }
293         else if(CTX_data_equals(member, "sculpt_object")) {
294                 if(obact && (obact->mode & OB_MODE_SCULPT))
295                         CTX_data_id_pointer_set(result, &obact->id);
296
297                 return 1;
298         }
299         else if(CTX_data_equals(member, "vertex_paint_object")) {
300                 if(obact && (obact->mode & OB_MODE_VERTEX_PAINT))
301                         CTX_data_id_pointer_set(result, &obact->id);
302
303                 return 1;
304         }
305         else if(CTX_data_equals(member, "weight_paint_object")) {
306                 if(obact && (obact->mode & OB_MODE_WEIGHT_PAINT))
307                         CTX_data_id_pointer_set(result, &obact->id);
308
309                 return 1;
310         }
311         else if(CTX_data_equals(member, "texture_paint_object")) {
312                 if(obact && (obact->mode & OB_MODE_TEXTURE_PAINT))
313                         CTX_data_id_pointer_set(result, &obact->id);
314
315                 return 1;
316         }
317         else if(CTX_data_equals(member, "particle_edit_object")) {
318                 if(obact && (obact->mode & OB_MODE_PARTICLE_EDIT))
319                         CTX_data_id_pointer_set(result, &obact->id);
320
321                 return 1;
322         }
323         else if(CTX_data_equals(member, "sequences")) {
324                 Editing *ed= seq_give_editing(scene, FALSE);
325                 if(ed) {
326                         Sequence *seq;
327                         for (seq= ed->seqbasep->first; seq; seq= seq->next) {
328                                 CTX_data_list_add(result, &scene->id, &RNA_Sequence, seq);
329                         }
330                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
331                         return 1;
332                 }
333         }
334         else if(CTX_data_equals(member, "selected_sequences")) {
335                 Editing *ed= seq_give_editing(scene, FALSE);
336                 if(ed) {
337                         Sequence *seq;
338                         for (seq= ed->seqbasep->first; seq; seq= seq->next) {
339                                 if (seq->flag & SELECT) {
340                                         CTX_data_list_add(result, &scene->id, &RNA_Sequence, seq);
341                                 }
342                         }
343                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
344                         return 1;
345                 }
346         }
347         else if(CTX_data_equals(member, "selected_editable_sequences")) {
348                 Editing *ed= seq_give_editing(scene, FALSE);
349                 if(ed) {
350                         Sequence *seq;
351                         for (seq= ed->seqbasep->first; seq; seq= seq->next) {
352                                 if (seq->flag & SELECT && !(seq->flag & SEQ_LOCK)) {
353                                         CTX_data_list_add(result, &scene->id, &RNA_Sequence, seq);
354                                 }
355                         }
356                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
357                         return 1;
358                 }
359         }
360         else {
361                 return 0; /* not found */
362         }
363
364         return -1; /* found but not available */
365 }
366