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