wrap the mouse within the region while grabbing so on release the current view never...
[blender-staging.git] / source / blender / editors / animation / anim_filter.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation, Joshua Leung
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Joshua Leung (original author)
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /* This file contains a system used to provide a layer of abstraction between sources
30  * of animation data and tools in Animation Editors. The method used here involves 
31  * generating a list of edit structures which enable tools to naively perform the actions 
32  * they require without all the boiler-plate associated with loops within loops and checking 
33  * for cases to ignore. 
34  *
35  * While this is primarily used for the Action/Dopesheet Editor (and its accessory modes),
36  * the Graph Editor also uses this for its channel list and for determining which curves
37  * are being edited. Likewise, the NLA Editor also uses this for its channel list and in
38  * its operators.
39  *
40  * Note: much of the original system this was based on was built before the creation of the RNA
41  * system. In future, it would be interesting to replace some parts of this code with RNA queries,
42  * however, RNA does not eliminate some of the boiler-plate reduction benefits presented by this 
43  * system, so if any such work does occur, it should only be used for the internals used here...
44  *
45  * -- Joshua Leung, Dec 2008 (Last revision July 2009)
46  */
47
48 #include <string.h>
49 #include <stdio.h>
50
51 #include "DNA_listBase.h"
52 #include "DNA_ID.h"
53 #include "DNA_anim_types.h"
54 #include "DNA_action_types.h"
55 #include "DNA_armature_types.h"
56 #include "DNA_constraint_types.h"
57 #include "DNA_camera_types.h"
58 #include "DNA_curve_types.h"
59 #include "DNA_gpencil_types.h"
60 #include "DNA_ipo_types.h"
61 #include "DNA_lamp_types.h"
62 #include "DNA_lattice_types.h"
63 #include "DNA_key_types.h"
64 #include "DNA_material_types.h"
65 #include "DNA_mesh_types.h"
66 #include "DNA_meta_types.h"
67 #include "DNA_object_types.h"
68 #include "DNA_particle_types.h"
69 #include "DNA_space_types.h"
70 #include "DNA_scene_types.h"
71 #include "DNA_screen_types.h"
72 #include "DNA_windowmanager_types.h"
73 #include "DNA_world_types.h"
74
75 #include "MEM_guardedalloc.h"
76
77 #include "BLI_blenlib.h"
78
79 #include "BKE_animsys.h"
80 #include "BKE_action.h"
81 #include "BKE_fcurve.h"
82 #include "BKE_context.h"
83 #include "BKE_global.h"
84 #include "BKE_key.h"
85 #include "BKE_object.h"
86 #include "BKE_material.h"
87 #include "BKE_screen.h"
88 #include "BKE_utildefines.h"
89
90 #include "ED_anim_api.h"
91 #include "ED_types.h"
92 #include "ED_util.h"
93
94 #include "WM_api.h"
95 #include "WM_types.h"
96
97 #include "BIF_gl.h"
98 #include "BIF_glutil.h"
99
100 #include "UI_interface.h"
101 #include "UI_resources.h"
102 #include "UI_view2d.h"
103
104 /* ************************************************************ */
105 /* Blender Context <-> Animation Context mapping */
106
107 /* ----------- Private Stuff - Action Editor ------------- */
108
109 /* Get shapekey data being edited (for Action Editor -> ShapeKey mode) */
110 /* Note: there's a similar function in key.c (ob_get_key) */
111 Key *actedit_get_shapekeys (bAnimContext *ac, SpaceAction *saction) 
112 {
113     Scene *scene= ac->scene;
114     Object *ob;
115     Key *key;
116         
117     ob = OBACT;
118     if (ob == NULL) 
119                 return NULL;
120         
121         /* XXX pinning is not available in 'ShapeKey' mode... */
122         //if (saction->pin) return NULL;
123         
124         /* shapekey data is stored with geometry data */
125         switch (ob->type) {
126                 case OB_MESH:
127                         key= ((Mesh *)ob->data)->key;
128                         break;
129                         
130                 case OB_LATTICE:
131                         key= ((Lattice *)ob->data)->key;
132                         break;
133                         
134                 case OB_CURVE:
135                 case OB_SURF:
136                         key= ((Curve *)ob->data)->key;
137                         break;
138                         
139                 default:
140                         return NULL;
141         }
142         
143         if (key) {
144                 if (key->type == KEY_RELATIVE)
145                         return key;
146         }
147         
148     return NULL;
149 }
150
151 /* Get data being edited in Action Editor (depending on current 'mode') */
152 static short actedit_get_context (bAnimContext *ac, SpaceAction *saction)
153 {
154         /* sync settings with current view status, then return appropriate data */
155         switch (saction->mode) {
156                 case SACTCONT_ACTION: /* 'Action Editor' */
157                         /* if not pinned, sync with active object */
158                         if (saction->pin == 0) {
159                                 if (ac->obact && ac->obact->adt)
160                                         saction->action = ac->obact->adt->action;
161                                 else
162                                         saction->action= NULL;
163                         }
164                         
165                         ac->datatype= ANIMCONT_ACTION;
166                         ac->data= saction->action;
167                         
168                         ac->mode= saction->mode;
169                         return 1;
170                         
171                 case SACTCONT_SHAPEKEY: /* 'ShapeKey Editor' */
172                         ac->datatype= ANIMCONT_SHAPEKEY;
173                         ac->data= actedit_get_shapekeys(ac, saction);
174                         
175                         ac->mode= saction->mode;
176                         return 1;
177                         
178                 case SACTCONT_GPENCIL: /* Grease Pencil */ // XXX review how this mode is handled...
179                         ac->datatype=ANIMCONT_GPENCIL;
180                         //ac->data= CTX_wm_screen(C); // FIXME: add that dopesheet type thing here!
181                         ac->data= NULL; // !!!
182                         
183                         ac->mode= saction->mode;
184                         return 1;
185                         
186                 case SACTCONT_DOPESHEET: /* DopeSheet */
187                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
188                         saction->ads.source= (ID *)ac->scene;
189                         
190                         ac->datatype= ANIMCONT_DOPESHEET;
191                         ac->data= &saction->ads;
192                         
193                         ac->mode= saction->mode;
194                         return 1;
195                 
196                 default: /* unhandled yet */
197                         ac->datatype= ANIMCONT_NONE;
198                         ac->data= NULL;
199                         
200                         ac->mode= -1;
201                         return 0;
202         }
203 }
204
205 /* ----------- Private Stuff - Graph Editor ------------- */
206
207 /* Get data being edited in Graph Editor (depending on current 'mode') */
208 static short graphedit_get_context (bAnimContext *ac, SpaceIpo *sipo)
209 {
210         /* init dopesheet data if non-existant (i.e. for old files) */
211         if (sipo->ads == NULL)
212                 sipo->ads= MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
213         
214         /* set settings for Graph Editor - "Selected = Editable" */
215         if (sipo->flag & SIPO_SELCUVERTSONLY)
216                 sipo->ads->filterflag |= ADS_FILTER_SELEDIT;
217         else
218                 sipo->ads->filterflag &= ~ADS_FILTER_SELEDIT;
219         
220         /* sync settings with current view status, then return appropriate data */
221         switch (sipo->mode) {
222                 case SIPO_MODE_ANIMATION:       /* Animation F-Curve Editor */
223                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
224                         sipo->ads->source= (ID *)ac->scene;
225                         sipo->ads->filterflag &= ~ADS_FILTER_ONLYDRIVERS;
226                         
227                         ac->datatype= ANIMCONT_FCURVES;
228                         ac->data= sipo->ads;
229                         
230                         ac->mode= sipo->mode;
231                         return 1;
232                 
233                 case SIPO_MODE_DRIVERS:         /* Driver F-Curve Editor */
234                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
235                         sipo->ads->source= (ID *)ac->scene;
236                         sipo->ads->filterflag |= ADS_FILTER_ONLYDRIVERS;
237                         
238                         ac->datatype= ANIMCONT_DRIVERS;
239                         ac->data= sipo->ads;
240                         
241                         ac->mode= sipo->mode;
242                         return 1;
243                 
244                 default: /* unhandled yet */
245                         ac->datatype= ANIMCONT_NONE;
246                         ac->data= NULL;
247                         
248                         ac->mode= -1;
249                         return 0;
250         }
251 }
252
253 /* ----------- Private Stuff - NLA Editor ------------- */
254
255 /* Get data being edited in Graph Editor (depending on current 'mode') */
256 static short nlaedit_get_context (bAnimContext *ac, SpaceNla *snla)
257 {
258         /* init dopesheet data if non-existant (i.e. for old files) */
259         if (snla->ads == NULL)
260                 snla->ads= MEM_callocN(sizeof(bDopeSheet), "NlaEdit DopeSheet");
261         
262         /* sync settings with current view status, then return appropriate data */
263         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
264         snla->ads->source= (ID *)ac->scene;
265         snla->ads->filterflag |= ADS_FILTER_ONLYNLA;
266         
267         ac->datatype= ANIMCONT_NLA;
268         ac->data= snla->ads;
269         
270         return 1;
271 }
272
273 /* ----------- Public API --------------- */
274
275 /* Obtain current anim-data context, given that context info from Blender context has already been set 
276  *      - AnimContext to write to is provided as pointer to var on stack so that we don't have
277  *        allocation/freeing costs (which are not that avoidable with channels).
278  */
279 short ANIM_animdata_context_getdata (bAnimContext *ac)
280 {
281         ScrArea *sa= ac->sa;
282         short ok= 0;
283         
284         /* context depends on editor we are currently in */
285         if (sa) {
286                 switch (sa->spacetype) {
287                         case SPACE_ACTION:
288                         {
289                                 SpaceAction *saction= (SpaceAction *)sa->spacedata.first;
290                                 ok= actedit_get_context(ac, saction);
291                         }
292                                 break;
293                                 
294                         case SPACE_IPO:
295                         {
296                                 SpaceIpo *sipo= (SpaceIpo *)sa->spacedata.first;
297                                 ok= graphedit_get_context(ac, sipo);
298                         }
299                                 break;
300                                 
301                         case SPACE_NLA:
302                         {
303                                 SpaceNla *snla= (SpaceNla *)sa->spacedata.first;
304                                 ok= nlaedit_get_context(ac, snla);
305                         }
306                                 break;
307                 }
308         }
309         
310         /* check if there's any valid data */
311         if (ok && ac->data)
312                 return 1;
313         else
314                 return 0;
315 }
316
317 /* Obtain current anim-data context from Blender Context info 
318  *      - AnimContext to write to is provided as pointer to var on stack so that we don't have
319  *        allocation/freeing costs (which are not that avoidable with channels).
320  *      - Clears data and sets the information from Blender Context which is useful
321  */
322 short ANIM_animdata_get_context (const bContext *C, bAnimContext *ac)
323 {
324         ScrArea *sa= CTX_wm_area(C);
325         ARegion *ar= CTX_wm_region(C);
326         Scene *scene= CTX_data_scene(C);
327         
328         /* clear old context info */
329         if (ac == NULL) return 0;
330         memset(ac, 0, sizeof(bAnimContext));
331         
332         /* get useful default context settings from context */
333         ac->scene= scene;
334         if (scene) {
335                 ac->markers= &scene->markers;           
336                 ac->obact= (scene->basact)?  scene->basact->object : NULL;
337         }
338         ac->sa= sa;
339         ac->ar= ar;
340         ac->spacetype= (sa) ? sa->spacetype : 0;
341         ac->regiontype= (ar) ? ar->regiontype : 0;
342         
343         /* get data context info */
344         return ANIM_animdata_context_getdata(ac);
345 }
346
347 /* ************************************************************ */
348 /* Blender Data <-- Filter --> Channels to be operated on */
349
350 /* quick macro to test if AnimData is usable */
351 #define ANIMDATA_HAS_KEYS(id) ((id)->adt && (id)->adt->action)
352
353 /* quick macro to test if AnimData is usable for drivers */
354 #define ANIMDATA_HAS_DRIVERS(id) ((id)->adt && (id)->adt->drivers.first)
355
356 /* quick macro to test if AnimData is usable for NLA */
357 #define ANIMDATA_HAS_NLA(id) ((id)->adt && (id)->adt->nla_tracks.first)
358
359
360 /* Quick macro to test for all three avove usability tests, performing the appropriate provided 
361  * action for each when the AnimData context is appropriate. 
362  *
363  * Priority order for this goes (most important, to least): AnimData blocks, NLA, Drivers, Keyframes.
364  *
365  * For this to work correctly, a standard set of data needs to be available within the scope that this
366  * gets called in: 
367  *      - ListBase anim_data;
368  *      - bDopeSheet *ads;
369  *      - bAnimListElem *ale;
370  *      - int items;
371  *
372  *      - id: ID block which should have an AnimData pointer following it immediately, to use
373  *      - adtOk: line or block of code to execute for AnimData-blocks case (usually ANIMDATA_ADD_ANIMDATA)
374  *      - nlaOk: line or block of code to execute for NLA case
375  *      - driversOk: line or block of code to execute for Drivers case
376  *      - keysOk: line or block of code for Keyframes case
377  */
378 #define ANIMDATA_FILTER_CASES(id, adtOk, nlaOk, driversOk, keysOk) \
379         {\
380                 if ((id)->adt) {\
381                         if (!(filter_mode & ANIMFILTER_CURVEVISIBLE) || !((id)->adt->flag & ADT_CURVES_NOT_VISIBLE)) {\
382                                 if (filter_mode & ANIMFILTER_ANIMDATA) {\
383                                         adtOk\
384                                 }\
385                                 else if (ads->filterflag & ADS_FILTER_ONLYNLA) {\
386                                         if (ANIMDATA_HAS_NLA(id)) {\
387                                                 nlaOk\
388                                         }\
389                                         else if (!(ads->filterflag & ADS_FILTER_NLA_NOACT) && ANIMDATA_HAS_KEYS(id)) {\
390                                                 nlaOk\
391                                         }\
392                                 }\
393                                 else if (ads->filterflag & ADS_FILTER_ONLYDRIVERS) {\
394                                         if (ANIMDATA_HAS_DRIVERS(id)) {\
395                                                 driversOk\
396                                         }\
397                                 }\
398                                 else {\
399                                         if (ANIMDATA_HAS_KEYS(id)) {\
400                                                 keysOk\
401                                         }\
402                                 }\
403                         }\
404                 }\
405         }
406
407
408 /* quick macro to add a pointer to an AnimData block as a channel */
409 #define ANIMDATA_ADD_ANIMDATA(id) \
410         {\
411                 ale= make_new_animlistelem((id)->adt, ANIMTYPE_ANIMDATA, NULL, ANIMTYPE_NONE, (ID *)id);\
412                 if (ale) {\
413                         BLI_addtail(anim_data, ale);\
414                         items++;\
415                 }\
416         }
417         
418 /* quick macro to test if an anim-channel representing an AnimData block is suitably active */
419 #define ANIMCHANNEL_ACTIVEOK(ale) \
420         ( !(filter_mode & ANIMFILTER_ACTIVE) || !(ale->adt) || (ale->adt->flag & ADT_UI_ACTIVE) )
421
422 /* quick macro to test if an anim-channel (F-Curve, Group, etc.) is selected in an acceptable way */
423 #define ANIMCHANNEL_SELOK(test_func) \
424                 ( !(filter_mode & (ANIMFILTER_SEL|ANIMFILTER_UNSEL)) || \
425                   ((filter_mode & ANIMFILTER_SEL) && test_func) || \
426                   ((filter_mode & ANIMFILTER_UNSEL) && test_func==0) ) 
427                   
428 /* quick macro to test if an anim-channel (F-Curve) is selected ok for editing purposes 
429  *      - _SELEDIT means that only selected curves will have visible+editable keyframes
430  */
431 // FIXME: this doesn't work cleanly yet...
432 #define ANIMCHANNEL_SELEDITOK(test_func) \
433                 ( !(filter_mode & ANIMFILTER_SELEDIT) || \
434                   (filter_mode & ANIMFILTER_CHANNELS) || \
435                   (test_func) )
436
437 /* ----------- 'Private' Stuff --------------- */
438
439 /* this function allocates memory for a new bAnimListElem struct for the 
440  * provided animation channel-data. 
441  */
442 bAnimListElem *make_new_animlistelem (void *data, short datatype, void *owner, short ownertype, ID *owner_id)
443 {
444         bAnimListElem *ale= NULL;
445         
446         /* only allocate memory if there is data to convert */
447         if (data) {
448                 /* allocate and set generic data */
449                 ale= MEM_callocN(sizeof(bAnimListElem), "bAnimListElem");
450                 
451                 ale->data= data;
452                 ale->type= datatype;
453                         // XXX what is the point of the owner data?
454                 ale->owner= owner;
455                 ale->ownertype= ownertype;
456                 
457                 ale->id= owner_id;
458                 ale->adt= BKE_animdata_from_id(owner_id);
459                 
460                 /* do specifics */
461                 switch (datatype) {
462                         case ANIMTYPE_SUMMARY:
463                         {
464                                 /* nothing to include for now... this is just a dummy wrappy around all the other channels 
465                                  * in the DopeSheet, and gets included at the start of the list
466                                  */
467                                 ale->key_data= NULL;
468                                 ale->datatype= ALE_ALL;
469                         }
470                                 break;
471                         
472                         case ANIMTYPE_SCENE:
473                         {
474                                 Scene *sce= (Scene *)data;
475                                 
476                                 ale->flag= sce->flag;
477                                 
478                                 ale->key_data= sce;
479                                 ale->datatype= ALE_SCE;
480                                 
481                                 ale->adt= BKE_animdata_from_id(data);
482                         }
483                                 break;
484                         case ANIMTYPE_OBJECT:
485                         {
486                                 Base *base= (Base *)data;
487                                 Object *ob= base->object;
488                                 
489                                 ale->flag= ob->flag;
490                                 
491                                 ale->key_data= ob;
492                                 ale->datatype= ALE_OB;
493                                 
494                                 ale->adt= BKE_animdata_from_id(&ob->id);
495                         }
496                                 break;
497                         case ANIMTYPE_FILLACTD:
498                         {
499                                 bAction *act= (bAction *)data;
500                                 
501                                 ale->flag= act->flag;
502                                 
503                                 ale->key_data= act;
504                                 ale->datatype= ALE_ACT;
505                         }
506                                 break;
507                         case ANIMTYPE_FILLDRIVERS:
508                         {
509                                 AnimData *adt= (AnimData *)data;
510                                 
511                                 ale->flag= adt->flag;
512                                 
513                                         // XXX... drivers don't show summary for now
514                                 ale->key_data= NULL;
515                                 ale->datatype= ALE_NONE;
516                         }
517                                 break;
518                         case ANIMTYPE_FILLMATD:
519                         {
520                                 Object *ob= (Object *)data;
521                                 
522                                 ale->flag= FILTER_MAT_OBJC(ob);
523                                 
524                                 ale->key_data= NULL;
525                                 ale->datatype= ALE_NONE;
526                         }
527                                 break;
528                         case ANIMTYPE_FILLPARTD:
529                         {
530                                 Object *ob= (Object *)data;
531                                 
532                                 ale->flag= FILTER_PART_OBJC(ob);
533                                 
534                                 ale->key_data= NULL;
535                                 ale->datatype= ALE_NONE;
536                         }
537                                 break;
538                         
539                         case ANIMTYPE_DSMAT:
540                         {
541                                 Material *ma= (Material *)data;
542                                 AnimData *adt= ma->adt;
543                                 
544                                 ale->flag= FILTER_MAT_OBJD(ma);
545                                 
546                                 ale->key_data= (adt) ? adt->action : NULL;
547                                 ale->datatype= ALE_ACT;
548                                 
549                                 ale->adt= BKE_animdata_from_id(data);
550                         }
551                                 break;
552                         case ANIMTYPE_DSLAM:
553                         {
554                                 Lamp *la= (Lamp *)data;
555                                 AnimData *adt= la->adt;
556                                 
557                                 ale->flag= FILTER_LAM_OBJD(la);
558                                 
559                                 ale->key_data= (adt) ? adt->action : NULL;
560                                 ale->datatype= ALE_ACT;
561                                 
562                                 ale->adt= BKE_animdata_from_id(data);
563                         }
564                                 break;
565                         case ANIMTYPE_DSCAM:
566                         {
567                                 Camera *ca= (Camera *)data;
568                                 AnimData *adt= ca->adt;
569                                 
570                                 ale->flag= FILTER_CAM_OBJD(ca);
571                                 
572                                 ale->key_data= (adt) ? adt->action : NULL;
573                                 ale->datatype= ALE_ACT;
574                                 
575                                 ale->adt= BKE_animdata_from_id(data);
576                         }
577                                 break;
578                         case ANIMTYPE_DSCUR:
579                         {
580                                 Curve *cu= (Curve *)data;
581                                 AnimData *adt= cu->adt;
582                                 
583                                 ale->flag= FILTER_CUR_OBJD(cu);
584                                 
585                                 ale->key_data= (adt) ? adt->action : NULL;
586                                 ale->datatype= ALE_ACT;
587                                 
588                                 ale->adt= BKE_animdata_from_id(data);
589                         }
590                                 break;
591                         case ANIMTYPE_DSSKEY:
592                         {
593                                 Key *key= (Key *)data;
594                                 AnimData *adt= key->adt;
595                                 
596                                 ale->flag= FILTER_SKE_OBJD(key); 
597                                 
598                                 ale->key_data= (adt) ? adt->action : NULL;
599                                 ale->datatype= ALE_ACT;
600                                 
601                                 ale->adt= BKE_animdata_from_id(data);
602                         }
603                                 break;
604                         case ANIMTYPE_DSWOR:
605                         {
606                                 World *wo= (World *)data;
607                                 AnimData *adt= wo->adt;
608                                 
609                                 ale->flag= FILTER_WOR_SCED(wo); 
610                                 
611                                 ale->key_data= (adt) ? adt->action : NULL;
612                                 ale->datatype= ALE_ACT;
613                                 
614                                 ale->adt= BKE_animdata_from_id(data);
615                         }
616                                 break;
617                         case ANIMTYPE_DSPART:
618                         {
619                                 ParticleSettings *part= (ParticleSettings*)ale->data;
620                                 AnimData *adt= part->adt;
621                                 
622                                 ale->flag= FILTER_PART_OBJD(part); 
623                                 
624                                 ale->key_data= (adt) ? adt->action : NULL;
625                                 ale->datatype= ALE_ACT;
626                                 
627                                 ale->adt= BKE_animdata_from_id(data);
628                         }
629                                 break;
630                                 
631                         case ANIMTYPE_GROUP:
632                         {
633                                 bActionGroup *agrp= (bActionGroup *)data;
634                                 
635                                 ale->flag= agrp->flag;
636                                 
637                                 ale->key_data= NULL;
638                                 ale->datatype= ALE_GROUP;
639                         }
640                                 break;
641                         case ANIMTYPE_FCURVE:
642                         {
643                                 FCurve *fcu= (FCurve *)data;
644                                 
645                                 ale->flag= fcu->flag;
646                                 
647                                 ale->key_data= fcu;
648                                 ale->datatype= ALE_FCURVE;
649                         }
650                                 break;
651                                 
652                         case ANIMTYPE_SHAPEKEY:
653                         {
654                                 KeyBlock *kb= (KeyBlock *)data;
655                                 Key *key= (Key *)ale->id;
656                                 
657                                 ale->flag= kb->flag;
658                                 
659                                 /* whether we have keyframes depends on whether there is a Key block to find it from */
660                                 if (key) {
661                                         /* index of shapekey is defined by place in key's list */
662                                         ale->index= BLI_findindex(&key->block, kb);
663                                         
664                                         /* the corresponding keyframes are from the animdata */
665                                         if (ale->adt && ale->adt->action) {
666                                                 bAction *act= ale->adt->action;
667                                                 char *rna_path = key_get_curValue_rnaPath(key, kb);
668                                                 
669                                                 /* try to find the F-Curve which corresponds to this exactly,
670                                                  * then free the MEM_alloc'd string
671                                                  */
672                                                 if (rna_path) {
673                                                         ale->key_data= (void *)list_find_fcurve(&act->curves, rna_path, 0);
674                                                         MEM_freeN(rna_path);
675                                                 }
676                                         }
677                                         ale->datatype= (ale->key_data)? ALE_FCURVE : ALE_NONE;
678                                 }
679                         }       
680                                 break;
681                         
682                         case ANIMTYPE_GPLAYER:
683                         {
684                                 bGPDlayer *gpl= (bGPDlayer *)data;
685                                 
686                                 ale->flag= gpl->flag;
687                                 
688                                 ale->key_data= NULL;
689                                 ale->datatype= ALE_GPFRAME;
690                         }
691                                 break;
692                                 
693                         case ANIMTYPE_NLATRACK:
694                         {
695                                 NlaTrack *nlt= (NlaTrack *)data;
696                                 
697                                 ale->flag= nlt->flag;
698                                 
699                                         // XXX or should this be done some other way?
700                                 ale->key_data= &nlt->strips;
701                                 ale->datatype= ALE_NLASTRIP;
702                         }
703                                 break;
704                         case ANIMTYPE_NLAACTION:
705                         {
706                                 /* nothing to include for now... nothing editable from NLA-perspective here */
707                                 ale->key_data= NULL;
708                                 ale->datatype= ALE_NONE;
709                         }
710                                 break;
711                 }
712         }
713         
714         /* return created datatype */
715         return ale;
716 }
717  
718 /* ----------------------------------------- */
719
720
721 static int animdata_filter_fcurves (ListBase *anim_data, bDopeSheet *ads, FCurve *first, bActionGroup *grp, void *owner, short ownertype, int filter_mode, ID *owner_id)
722 {
723         bAnimListElem *ale = NULL;
724         FCurve *fcu;
725         int items = 0;
726         
727         /* loop over F-Curves - assume that the caller of this has already checked that these should be included 
728          * NOTE: we need to check if the F-Curves belong to the same group, as this gets called for groups too...
729          */
730         for (fcu= first; ((fcu) && (fcu->grp==grp)); fcu= fcu->next) {
731                 /* special exception for Pose-Channel Based F-Curves:
732                  *      - the 'Only Selected' data filter should be applied to Pose-Channel data too, but those are
733                  *        represented as F-Curves. The way the filter for objects worked was to be the first check
734                  *        after 'normal' visibility, so this is done first here too...
735                  *      - we currently use an 'approximate' method for getting these F-Curves that doesn't require
736                  *        carefully checking the entire path
737                  *      - this will also affect things like Drivers, and also works for Bone Constraints
738                  */
739                 if ( ((ads) && (ads->filterflag & ADS_FILTER_ONLYSEL)) && 
740                          ((owner_id) && (GS(owner_id->name) == ID_OB)) ) 
741                 {
742                         Object *ob= (Object *)owner_id;
743                         
744                         /* only consider if F-Curve involves pose_channels */
745                         if ((fcu->rna_path) && strstr(fcu->rna_path, "pose_channels")) {
746                                 bPoseChannel *pchan;
747                                 char *bone_name;
748                                 
749                                 /* get bone-name, and check if this bone is selected */
750                                 bone_name= BLI_getQuotedStr(fcu->rna_path, "pose_channels[");
751                                 pchan= get_pose_channel(ob->pose, bone_name);
752                                 if (bone_name) MEM_freeN(bone_name);
753                                 
754                                 /* can only add this F-Curve if it is selected */
755                                 if ((pchan) && (pchan->bone) && (pchan->bone->flag & BONE_SELECTED)==0)
756                                         continue;
757                         }
758                 }
759                 
760                 /* only include if visible (Graph Editor check, not channels check) */
761                 if (!(filter_mode & ANIMFILTER_CURVEVISIBLE) || (fcu->flag & FCURVE_VISIBLE)) {
762                         /* only work with this channel and its subchannels if it is editable */
763                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_FCU(fcu)) {
764                                 /* only include this curve if selected in a way consistent with the filtering requirements */
765                                 if ( ANIMCHANNEL_SELOK(SEL_FCU(fcu)) && ANIMCHANNEL_SELEDITOK(SEL_FCU(fcu)) ) {
766                                         /* only include if this curve is active */
767                                         if (!(filter_mode & ANIMFILTER_ACTIVE) || (fcu->flag & FCURVE_ACTIVE)) {
768                                                 ale= make_new_animlistelem(fcu, ANIMTYPE_FCURVE, owner, ownertype, owner_id);
769                                                 
770                                                 if (ale) {
771                                                         BLI_addtail(anim_data, ale);
772                                                         items++;
773                                                 }
774                                         }
775                                 }
776                         }
777                 }
778         }
779         
780         /* return the number of items added to the list */
781         return items;
782 }
783
784 static int animdata_filter_action (ListBase *anim_data, bDopeSheet *ads, bAction *act, int filter_mode, void *owner, short ownertype, ID *owner_id)
785 {
786         bAnimListElem *ale=NULL;
787         bActionGroup *agrp;
788         FCurve *lastchan=NULL;
789         int items = 0;
790         
791         /* loop over groups */
792         // TODO: in future, should we expect to need nested groups?
793         for (agrp= act->groups.first; agrp; agrp= agrp->next) {
794                 /* add this group as a channel first */
795                 if ((filter_mode & ANIMFILTER_CHANNELS) || !(filter_mode & ANIMFILTER_CURVESONLY)) {
796                         /* check if filtering by selection */
797                         if ( ANIMCHANNEL_SELOK(SEL_AGRP(agrp)) ) {
798                                 ale= make_new_animlistelem(agrp, ANIMTYPE_GROUP, NULL, ANIMTYPE_NONE, owner_id);
799                                 if (ale) {
800                                         BLI_addtail(anim_data, ale);
801                                         items++;
802                                 }
803                         }
804                 }
805                 
806                 /* store reference to last channel of group */
807                 if (agrp->channels.last) 
808                         lastchan= agrp->channels.last;
809                 
810                 
811                 /* there are some situations, where only the channels of the action group should get considered */
812                 if (!(filter_mode & ANIMFILTER_ACTGROUPED) || (agrp->flag & AGRP_ACTIVE)) {
813                         /* filters here are a bit convoulted...
814                          *      - groups show a "summary" of keyframes beside their name which must accessable for tools which handle keyframes
815                          *      - groups can be collapsed (and those tools which are only interested in channels rely on knowing that group is closed)
816                          *
817                          * cases when we should include F-Curves inside group:
818                          *      - we don't care about visibility
819                          *      - group is expanded
820                          *      - we just need the F-Curves present
821                          */
822                         if ( (!(filter_mode & ANIMFILTER_VISIBLE) || EXPANDED_AGRP(agrp)) || (filter_mode & ANIMFILTER_CURVESONLY) ) 
823                         {
824                                 /* for the Graph Editor, curves may be set to not be visible in the view to lessen clutter,
825                                  * but to do this, we need to check that the group doesn't have it's not-visible flag set preventing 
826                                  * all its sub-curves to be shown
827                                  */
828                                 if ( !(filter_mode & ANIMFILTER_CURVEVISIBLE) || !(agrp->flag & AGRP_NOTVISIBLE) )
829                                 {
830                                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_AGRP(agrp)) {
831                                                 items += animdata_filter_fcurves(anim_data, ads, agrp->channels.first, agrp, owner, ownertype, filter_mode, owner_id);
832                                         }
833                                 }
834                         }
835                 }
836                 
837                 // TODO: but we still need to deal with the case when the group may be closed, but it has no visible curves too
838         }
839         
840         /* loop over un-grouped F-Curves (only if we're not only considering those channels in the animive group) */
841         if (!(filter_mode & ANIMFILTER_ACTGROUPED))  {
842                 // XXX the 'owner' info here needs review...
843                 items += animdata_filter_fcurves(anim_data, ads, (lastchan)?(lastchan->next):(act->curves.first), NULL, owner, ownertype, filter_mode, owner_id);
844         }
845         
846         /* return the number of items added to the list */
847         return items;
848 }
849
850 /* Include NLA-Data for NLA-Editor:
851  *      - when ANIMFILTER_CHANNELS is used, that means we should be filtering the list for display
852  *        Although the evaluation order is from the first track to the last and then apply the Action on top,
853  *        we present this in the UI as the Active Action followed by the last track to the first so that we 
854  *        get the evaluation order presented as per a stack.
855  *      - for normal filtering (i.e. for editing), we only need the NLA-tracks but they can be in 'normal' evaluation
856  *        order, i.e. first to last. Otherwise, some tools may get screwed up.
857  */
858 static int animdata_filter_nla (ListBase *anim_data, bDopeSheet *ads, AnimData *adt, int filter_mode, void *owner, short ownertype, ID *owner_id)
859 {
860         bAnimListElem *ale;
861         NlaTrack *nlt;
862         NlaTrack *first=NULL, *next=NULL;
863         int items = 0;
864         
865         /* if showing channels, include active action */
866         if (filter_mode & ANIMFILTER_CHANNELS) {
867                 /* there isn't really anything editable here, so skip if need editable */
868                 // TODO: currently, selection isn't checked since it doesn't matter
869                 if ((filter_mode & ANIMFILTER_FOREDIT) == 0) { 
870                         /* just add the action track now (this MUST appear for drawing)
871                          *      - as AnimData may not have an action, we pass a dummy pointer just to get the list elem created, then
872                          *        overwrite this with the real value - REVIEW THIS...
873                          */
874                         ale= make_new_animlistelem((void *)(&adt->action), ANIMTYPE_NLAACTION, owner, ownertype, owner_id);
875                         ale->data= (adt->action) ? adt->action : NULL;
876                                 
877                         if (ale) {
878                                 BLI_addtail(anim_data, ale);
879                                 items++;
880                         }
881                 }
882                 
883                 /* first track to include will be the last one if we're filtering by channels */
884                 first= adt->nla_tracks.last;
885         }
886         else {
887                 /* first track to include will the the first one (as per normal) */
888                 first= adt->nla_tracks.first;
889         }
890         
891         /* loop over NLA Tracks - assume that the caller of this has already checked that these should be included */
892         for (nlt= first; nlt; nlt= next) {
893                 /* 'next' NLA-Track to use depends on whether we're filtering for drawing or not */
894                 if (filter_mode & ANIMFILTER_CHANNELS) 
895                         next= nlt->prev;
896                 else
897                         next= nlt->next;
898                 
899                 /* if we're in NLA-tweakmode, don't show this track if it was disabled (due to tweaking) for now 
900                  *      - active track should still get shown though (even though it has disabled flag set)
901                  */
902                 // FIXME: the channels after should still get drawn, just 'differently', and after an active-action channel
903                 if ((adt->flag & ADT_NLA_EDIT_ON) && (nlt->flag & NLATRACK_DISABLED) && !(nlt->flag & NLATRACK_ACTIVE))
904                         continue;
905                 
906                 /* only work with this channel and its subchannels if it is editable */
907                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_NLT(nlt)) {
908                         /* only include this track if selected in a way consistent with the filtering requirements */
909                         if ( ANIMCHANNEL_SELOK(SEL_NLT(nlt)) ) {
910                                 /* only include if this track is active */
911                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (nlt->flag & NLATRACK_ACTIVE)) {
912                                         ale= make_new_animlistelem(nlt, ANIMTYPE_NLATRACK, owner, ownertype, owner_id);
913                                                 
914                                         if (ale) {
915                                                 BLI_addtail(anim_data, ale);
916                                                 items++;
917                                         }
918                                 }
919                         }
920                 }
921         }
922         
923         /* return the number of items added to the list */
924         return items;
925 }
926
927 /* Include ShapeKey Data for ShapeKey Editor */
928 static int animdata_filter_shapekey (ListBase *anim_data, Key *key, int filter_mode)
929 {
930         bAnimListElem *ale;
931         int items = 0;
932         
933         /* check if channels or only F-Curves */
934         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {
935                 KeyBlock *kb;
936                 
937                 /* loop through the channels adding ShapeKeys as appropriate */
938                 for (kb= key->block.first; kb; kb= kb->next) {
939                         /* skip the first one, since that's the non-animateable basis */
940                         // XXX maybe in future this may become handy?
941                         if (kb == key->block.first) continue;
942                         
943                         /* only work with this channel and its subchannels if it is editable */
944                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_SHAPEKEY(kb)) {
945                                 /* only include this track if selected in a way consistent with the filtering requirements */
946                                 if ( ANIMCHANNEL_SELOK(SEL_SHAPEKEY(kb)) ) {
947                                         // TODO: consider 'active' too?
948                                         
949                                         /* owner-id here must be key so that the F-Curve can be resolved... */
950                                         ale= make_new_animlistelem(kb, ANIMTYPE_SHAPEKEY, NULL, ANIMTYPE_NONE, (ID *)key);
951                                         
952                                         if (ale) {
953                                                 BLI_addtail(anim_data, ale);
954                                                 items++;
955                                         }
956                                 }
957                         }
958                 }
959         }
960         else {
961                 /* just use the action associated with the shapekey */
962                 // FIXME: is owner-id and having no owner/dopesheet really fine?
963                 if (key->adt) {
964                         if (filter_mode & ANIMFILTER_ANIMDATA)
965                                 ANIMDATA_ADD_ANIMDATA(key)
966                         else if (key->adt->action)
967                                 items= animdata_filter_action(anim_data, NULL, key->adt->action, filter_mode, NULL, ANIMTYPE_NONE, (ID *)key);
968                 }
969         }
970         
971         /* return the number of items added to the list */
972         return items;
973 }
974
975 #if 0
976 // FIXME: switch this to use the bDopeSheet...
977 static int animdata_filter_gpencil (ListBase *anim_data, bScreen *sc, int filter_mode)
978 {
979         bAnimListElem *ale;
980         ScrArea *sa, *curarea;
981         bGPdata *gpd;
982         bGPDlayer *gpl;
983         int items = 0;
984         
985         /* check if filtering types are appropriate */
986         {
987                 /* special hack for fullscreen area (which must be this one then):
988                  *      - we use the curarea->full as screen to get spaces from, since the
989                  *        old (pre-fullscreen) screen was stored there...
990                  *      - this is needed as all data would otherwise disappear
991                  */
992                 // XXX need to get new alternative for curarea
993                 if ((curarea->full) && (curarea->spacetype==SPACE_ACTION))
994                         sc= curarea->full;
995                 
996                 /* loop over spaces in current screen, finding gpd blocks (could be slow!) */
997                 for (sa= sc->areabase.first; sa; sa= sa->next) {
998                         /* try to get gp data */
999                         // XXX need to put back grease pencil api...
1000                         gpd= gpencil_data_get_active(sa);
1001                         if (gpd == NULL) continue;
1002                         
1003                         /* add gpd as channel too (if for drawing, and it has layers) */
1004                         if ((filter_mode & ANIMFILTER_CHANNELS) && (gpd->layers.first)) {
1005                                 /* add to list */
1006                                 ale= make_new_animlistelem(gpd, ANIMTYPE_GPDATABLOCK, sa, ANIMTYPE_SPECIALDATA);
1007                                 if (ale) {
1008                                         BLI_addtail(anim_data, ale);
1009                                         items++;
1010                                 }
1011                         }
1012                         
1013                         /* only add layers if they will be visible (if drawing channels) */
1014                         if ( !(filter_mode & ANIMFILTER_VISIBLE) || (EXPANDED_GPD(gpd)) ) {
1015                                 /* loop over layers as the conditions are acceptable */
1016                                 for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
1017                                         /* only if selected */
1018                                         if ( ANIMCHANNEL_SELOK(SEL_GPL(gpl)) ) {
1019                                                 /* only if editable */
1020                                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_GPL(gpl)) {
1021                                                         /* add to list */
1022                                                         ale= make_new_animlistelem(gpl, ANIMTYPE_GPLAYER, gpd, ANIMTYPE_GPDATABLOCK);
1023                                                         if (ale) {
1024                                                                 BLI_addtail(anim_data, ale);
1025                                                                 items++;
1026                                                         }
1027                                                 }
1028                                         }
1029                                 }
1030                         }
1031                 }
1032         }
1033         
1034         /* return the number of items added to the list */
1035         return items;
1036 }
1037 #endif 
1038
1039
1040 static int animdata_filter_dopesheet_mats (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1041 {
1042         ListBase mats = {NULL, NULL};
1043         LinkData *ld;
1044         
1045         bAnimListElem *ale=NULL;
1046         Object *ob= base->object;
1047         int items=0, a=0;
1048         
1049         /* firstly check that we actuallly have some materials, by gathering all materials in a temp list */
1050         for (a=0; a < ob->totcol; a++) {
1051                 Material *ma= give_current_material(ob, a);
1052                 short ok = 0;
1053                 
1054                 /* for now, if no material returned, skip (this shouldn't confuse the user I hope) */
1055                 if (ELEM(NULL, ma, ma->adt)) 
1056                         continue;
1057                 
1058                 /* check if ok */
1059                 ANIMDATA_FILTER_CASES(ma, 
1060                         { /* AnimData blocks - do nothing... */ },
1061                         ok=1;, 
1062                         ok=1;, 
1063                         ok=1;)
1064                 if (ok == 0) continue;
1065                 
1066                 /* make a temp list elem for this */
1067                 ld= MEM_callocN(sizeof(LinkData), "DopeSheet-MaterialCache");
1068                 ld->data= ma;
1069                 BLI_addtail(&mats, ld);
1070         }
1071         
1072         /* if there were no channels found, no need to carry on */
1073         if (mats.first == NULL)
1074                 return 0;
1075         
1076         /* include materials-expand widget? */
1077         if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1078                 ale= make_new_animlistelem(ob, ANIMTYPE_FILLMATD, base, ANIMTYPE_OBJECT, (ID *)ob);
1079                 if (ale) {
1080                         BLI_addtail(anim_data, ale);
1081                         items++;
1082                 }
1083         }
1084         
1085         /* add materials? */
1086         if (FILTER_MAT_OBJC(ob) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1087                 /* for each material in cache, add channels  */
1088                 for (ld= mats.first; ld; ld= ld->next) {
1089                         Material *ma= (Material *)ld->data;
1090                         
1091                         /* include material-expand widget? */
1092                         // hmm... do we need to store the index of this material in the array anywhere?
1093                         if (filter_mode & ANIMFILTER_CHANNELS) {
1094                                 /* check if filtering by active status */
1095                                 if ANIMCHANNEL_ACTIVEOK(ma) {
1096                                         ale= make_new_animlistelem(ma, ANIMTYPE_DSMAT, base, ANIMTYPE_OBJECT, (ID *)ma);
1097                                         if (ale) {
1098                                                 BLI_addtail(anim_data, ale);
1099                                                 items++;
1100                                         }
1101                                 }
1102                         }
1103                         
1104                         /* add material's animation data */
1105                         if (FILTER_MAT_OBJD(ma) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1106                                 ANIMDATA_FILTER_CASES(ma, 
1107                                         { /* AnimData blocks - do nothing... */ },
1108                                         items += animdata_filter_nla(anim_data, ads, ma->adt, filter_mode, ma, ANIMTYPE_DSMAT, (ID *)ma);, 
1109                                         items += animdata_filter_fcurves(anim_data, ads, ma->adt->drivers.first, NULL, ma, ANIMTYPE_DSMAT, filter_mode, (ID *)ma);, 
1110                                         items += animdata_filter_action(anim_data, ads, ma->adt->action, filter_mode, ma, ANIMTYPE_DSMAT, (ID *)ma);)
1111                         }
1112                 }
1113         }
1114         
1115         /* free cache */
1116         BLI_freelistN(&mats);
1117         
1118         /* return the number of items added to the list */
1119         return items;
1120 }
1121
1122 static int animdata_filter_dopesheet_particles (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1123 {
1124         bAnimListElem *ale=NULL;
1125         Object *ob= base->object;
1126         ParticleSystem *psys = ob->particlesystem.first;
1127         int items= 0, first = 1;
1128
1129         for(; psys; psys=psys->next) {
1130                 short ok = 0;
1131
1132                 if(ELEM(NULL, psys->part, psys->part->adt))
1133                         continue;
1134
1135                 ANIMDATA_FILTER_CASES(psys->part,
1136                         { /* AnimData blocks - do nothing... */ },
1137                         ok=1;, 
1138                         ok=1;, 
1139                         ok=1;)
1140                 if (ok == 0) continue;
1141
1142                 /* include particles-expand widget? */
1143                 if (first && (filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1144                         ale= make_new_animlistelem(ob, ANIMTYPE_FILLPARTD, base, ANIMTYPE_OBJECT, (ID *)ob);
1145                         if (ale) {
1146                                 BLI_addtail(anim_data, ale);
1147                                 items++;
1148                         }
1149                         first = 0;
1150                 }
1151                 
1152                 /* add particle settings? */
1153                 if (FILTER_PART_OBJC(ob) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1154                         if ((filter_mode & ANIMFILTER_CHANNELS)) {
1155                                 /* check if filtering by active status */
1156                                 if ANIMCHANNEL_ACTIVEOK(psys->part) {
1157                                         ale = make_new_animlistelem(psys->part, ANIMTYPE_DSPART, base, ANIMTYPE_OBJECT, (ID *)psys->part);
1158                                         if (ale) {
1159                                                 BLI_addtail(anim_data, ale);
1160                                                 items++;
1161                                         }
1162                                 }
1163                         }
1164                         
1165                         if (FILTER_PART_OBJD(psys->part) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1166                                 ANIMDATA_FILTER_CASES(psys->part,
1167                                         { /* AnimData blocks - do nothing... */ },
1168                                         items += animdata_filter_nla(anim_data, ads, psys->part->adt, filter_mode, psys->part, ANIMTYPE_DSPART, (ID *)psys->part);, 
1169                                         items += animdata_filter_fcurves(anim_data, ads, psys->part->adt->drivers.first, NULL, psys->part, ANIMTYPE_DSPART, filter_mode, (ID *)psys->part);, 
1170                                         items += animdata_filter_action(anim_data, ads, psys->part->adt->action, filter_mode, psys->part, ANIMTYPE_DSPART, (ID *)psys->part);)
1171                         }
1172                 }
1173         }
1174         
1175         /* return the number of items added to the list */
1176         return items;
1177 }
1178
1179 static int animdata_filter_dopesheet_obdata (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1180 {
1181         bAnimListElem *ale=NULL;
1182         Object *ob= base->object;
1183         IdAdtTemplate *iat= ob->data;
1184         AnimData *adt= iat->adt;
1185         short type=0, expanded=0;
1186         int items= 0;
1187         
1188         /* get settings based on data type */
1189         switch (ob->type) {
1190                 case OB_CAMERA: /* ------- Camera ------------ */
1191                 {
1192                         Camera *ca= (Camera *)ob->data;
1193                         
1194                         type= ANIMTYPE_DSCAM;
1195                         expanded= FILTER_CAM_OBJD(ca);
1196                 }
1197                         break;
1198                 case OB_LAMP: /* ---------- Lamp ----------- */
1199                 {
1200                         Lamp *la= (Lamp *)ob->data;
1201                         
1202                         type= ANIMTYPE_DSLAM;
1203                         expanded= FILTER_LAM_OBJD(la);
1204                 }
1205                         break;
1206                 case OB_CURVE: /* ------- Curve ---------- */
1207                 {
1208                         Curve *cu= (Curve *)ob->data;
1209                         
1210                         type= ANIMTYPE_DSCUR;
1211                         expanded= FILTER_CUR_OBJD(cu);
1212                 }
1213                         break;
1214                 case OB_MBALL: /* ------- MetaBall ---------- */
1215                 {
1216                         MetaBall *mb= (MetaBall *)ob->data;
1217                         
1218                         type= ANIMTYPE_DSMBALL;
1219                         expanded= FILTER_MBALL_OBJD(mb);
1220                 }
1221                         break;
1222                 case OB_ARMATURE: /* ------- Armature ---------- */
1223                 {
1224                         bArmature *arm= (bArmature *)ob->data;
1225                         
1226                         type= ANIMTYPE_DSARM;
1227                         expanded= FILTER_ARM_OBJD(arm);
1228                 }
1229                         break;
1230         }
1231         
1232         /* special exception for drivers instead of action */
1233         if (ads->filterflag & ADS_FILTER_ONLYDRIVERS)
1234                 expanded= EXPANDED_DRVD(adt);
1235         
1236         /* include data-expand widget? */
1237         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {       
1238                 /* check if filtering by active status */
1239                 if ANIMCHANNEL_ACTIVEOK(iat) {
1240                         ale= make_new_animlistelem(iat, type, base, ANIMTYPE_OBJECT, (ID *)iat);
1241                         if (ale) BLI_addtail(anim_data, ale);
1242                 }
1243         }
1244         
1245         /* add object-data animation channels? */
1246         if ((expanded) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1247                 /* filtering for channels - nla, drivers, keyframes */
1248                 ANIMDATA_FILTER_CASES(iat, 
1249                         { /* AnimData blocks - do nothing... */ },
1250                         items+= animdata_filter_nla(anim_data, ads, iat->adt, filter_mode, iat, type, (ID *)iat);,
1251                         items+= animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, iat, type, filter_mode, (ID *)iat);, 
1252                         items += animdata_filter_action(anim_data, ads, iat->adt->action, filter_mode, iat, type, (ID *)iat);)
1253         }
1254         
1255         /* return the number of items added to the list */
1256         return items;
1257 }
1258
1259 static int animdata_filter_dopesheet_ob (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1260 {
1261         bAnimListElem *ale=NULL;
1262         AnimData *adt = NULL;
1263         Object *ob= base->object;
1264         Key *key= ob_get_key(ob);
1265         short obdata_ok = 0;
1266         int items = 0;
1267         
1268         /* add this object as a channel first */
1269         if ((filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) == 0) {
1270                 /* check if filtering by selection */
1271                 if ANIMCHANNEL_SELOK((base->flag & SELECT)) {
1272                         /* check if filtering by active status */
1273                         if ANIMCHANNEL_ACTIVEOK(ob) {
1274                                 ale= make_new_animlistelem(base, ANIMTYPE_OBJECT, NULL, ANIMTYPE_NONE, (ID *)ob);
1275                                 if (ale) {
1276                                         BLI_addtail(anim_data, ale);
1277                                         items++;
1278                                 }
1279                         }
1280                 }
1281         }
1282         
1283         /* if collapsed, don't go any further (unless adding keyframes only) */
1284         if ( (EXPANDED_OBJC(ob) == 0) && !(filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) )
1285                 return items;
1286         
1287         /* Action, Drivers, or NLA */
1288         if (ob->adt) {
1289                 adt= ob->adt;
1290                 ANIMDATA_FILTER_CASES(ob,
1291                         { /* AnimData blocks - do nothing... */ },
1292                         { /* nla */
1293                                 /* add NLA tracks */
1294                                 items += animdata_filter_nla(anim_data, ads, adt, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1295                         },
1296                         { /* drivers */
1297                                 /* include drivers-expand widget? */
1298                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1299                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLDRIVERS, base, ANIMTYPE_OBJECT, (ID *)ob);
1300                                         if (ale) {
1301                                                 BLI_addtail(anim_data, ale);
1302                                                 items++;
1303                                         }
1304                                 }
1305                                 
1306                                 /* add F-Curve channels (drivers are F-Curves) */
1307                                 if (EXPANDED_DRVD(adt) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1308                                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
1309                                         items += animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, ob, ANIMTYPE_OBJECT, filter_mode, (ID *)ob);
1310                                 }
1311                         },
1312                         { /* action (keyframes) */
1313                                 /* include action-expand widget? */
1314                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1315                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, base, ANIMTYPE_OBJECT, (ID *)ob);
1316                                         if (ale) {
1317                                                 BLI_addtail(anim_data, ale);
1318                                                 items++;
1319                                         }
1320                                 }
1321                                 
1322                                 /* add F-Curve channels? */
1323                                 if (EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1324                                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
1325                                         items += animdata_filter_action(anim_data, ads, adt->action, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob); 
1326                                 }
1327                         }
1328                 );
1329         }
1330         
1331         
1332         /* ShapeKeys? */
1333         if ((key) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS)) {
1334                 adt= key->adt;
1335                 ANIMDATA_FILTER_CASES(key,
1336                         { /* AnimData blocks - do nothing... */ },
1337                         { /* nla */
1338                                 /* include shapekey-expand widget? */
1339                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1340                                         /* check if filtering by active status */
1341                                         if ANIMCHANNEL_ACTIVEOK(key) {
1342                                                 ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1343                                                 if (ale) {
1344                                                         BLI_addtail(anim_data, ale);
1345                                                         items++;
1346                                                 }
1347                                         }
1348                                 }
1349                                 
1350                                 /* add NLA tracks - only if expanded or so */
1351                                 if (FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY))
1352                                         items += animdata_filter_nla(anim_data, ads, adt, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1353                         },
1354                         { /* drivers */
1355                                 /* include shapekey-expand widget? */
1356                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1357                                         ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1358                                         if (ale) {
1359                                                 BLI_addtail(anim_data, ale);
1360                                                 items++;
1361                                         }
1362                                 }
1363                                 
1364                                 /* add channels */
1365                                 if (FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1366                                         items += animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, key, ANIMTYPE_DSSKEY, filter_mode, (ID *)key);
1367                                 }
1368                         },
1369                         { /* action (keyframes) */
1370                                 /* include shapekey-expand widget? */
1371                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1372                                         /* check if filtering by active status */
1373                                         if ANIMCHANNEL_ACTIVEOK(key) {
1374                                                 ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1375                                                 if (ale) {
1376                                                         BLI_addtail(anim_data, ale);
1377                                                         items++;
1378                                                 }
1379                                         }
1380                                 }
1381                                 
1382                                 /* add channels */
1383                                 if (FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1384                                         items += animdata_filter_action(anim_data, ads, adt->action, filter_mode, key, ANIMTYPE_DSSKEY, (ID *)key); 
1385                                 }
1386                         }
1387                 );
1388         }
1389
1390         /* Materials? */
1391         if ((ob->totcol) && !(ads->filterflag & ADS_FILTER_NOMAT))
1392                 items += animdata_filter_dopesheet_mats(anim_data, ads, base, filter_mode);
1393         
1394         /* Object Data */
1395         switch (ob->type) {
1396                 case OB_CAMERA: /* ------- Camera ------------ */
1397                 {
1398                         Camera *ca= (Camera *)ob->data;
1399                         
1400                         if ((ads->filterflag & ADS_FILTER_NOCAM) == 0) {
1401                                 ANIMDATA_FILTER_CASES(ca,
1402                                         { /* AnimData blocks - do nothing... */ },
1403                                         obdata_ok= 1;,
1404                                         obdata_ok= 1;,
1405                                         obdata_ok= 1;)
1406                         }
1407                 }
1408                         break;
1409                 case OB_LAMP: /* ---------- Lamp ----------- */
1410                 {
1411                         Lamp *la= (Lamp *)ob->data;
1412                         
1413                         if ((ads->filterflag & ADS_FILTER_NOLAM) == 0) {
1414                                 ANIMDATA_FILTER_CASES(la,
1415                                         { /* AnimData blocks - do nothing... */ },
1416                                         obdata_ok= 1;,
1417                                         obdata_ok= 1;,
1418                                         obdata_ok= 1;)
1419                         }
1420                 }
1421                         break;
1422                 case OB_CURVE: /* ------- Curve ---------- */
1423                 {
1424                         Curve *cu= (Curve *)ob->data;
1425                         
1426                         if ((ads->filterflag & ADS_FILTER_NOCUR) == 0) {
1427                                 ANIMDATA_FILTER_CASES(cu,
1428                                         { /* AnimData blocks - do nothing... */ },
1429                                         obdata_ok= 1;,
1430                                         obdata_ok= 1;,
1431                                         obdata_ok= 1;)
1432                         }
1433                 }
1434                         break;
1435                 case OB_MBALL: /* ------- MetaBall ---------- */
1436                 {
1437                         MetaBall *mb= (MetaBall *)ob->data;
1438                         
1439                         if ((ads->filterflag & ADS_FILTER_NOMBA) == 0) {
1440                                 ANIMDATA_FILTER_CASES(mb,
1441                                         { /* AnimData blocks - do nothing... */ },
1442                                         obdata_ok= 1;,
1443                                         obdata_ok= 1;,
1444                                         obdata_ok= 1;)
1445                         }
1446                 }
1447                         break;
1448                 case OB_ARMATURE: /* ------- Armature ---------- */
1449                 {
1450                         bArmature *arm= (bArmature *)ob->data;
1451                         
1452                         if ((ads->filterflag & ADS_FILTER_NOARM) == 0) {
1453                                 ANIMDATA_FILTER_CASES(arm,
1454                                         { /* AnimData blocks - do nothing... */ },
1455                                         obdata_ok= 1;,
1456                                         obdata_ok= 1;,
1457                                         obdata_ok= 1;)
1458                         }
1459                 }
1460                         break;
1461         }
1462         if (obdata_ok) 
1463                 items += animdata_filter_dopesheet_obdata(anim_data, ads, base, filter_mode);
1464
1465         /* particles */
1466         if (ob->particlesystem.first && !(ads->filterflag & ADS_FILTER_NOPART))
1467                 items += animdata_filter_dopesheet_particles(anim_data, ads, base, filter_mode);
1468         
1469         /* return the number of items added to the list */
1470         return items;
1471 }       
1472
1473 static int animdata_filter_dopesheet_scene (ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
1474 {
1475         World *wo= sce->world;
1476         AnimData *adt= NULL;
1477         bAnimListElem *ale;
1478         int items = 0;
1479         
1480         /* add scene as a channel first (even if we aren't showing scenes we still need to show the scene's sub-data */
1481         if ((filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) == 0) {
1482                 /* check if filtering by selection */
1483                 if (ANIMCHANNEL_SELOK( (sce->flag & SCE_DS_SELECTED) )) {
1484                         ale= make_new_animlistelem(sce, ANIMTYPE_SCENE, NULL, ANIMTYPE_NONE, NULL);
1485                         if (ale) {
1486                                 BLI_addtail(anim_data, ale);
1487                                 items++;
1488                         }
1489                 }
1490         }
1491         
1492         /* if collapsed, don't go any further (unless adding keyframes only) */
1493         if ( (EXPANDED_SCEC(sce) == 0) && !(filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) )
1494                 return items;
1495                 
1496         /* Action, Drivers, or NLA for Scene */
1497         if ((ads->filterflag & ADS_FILTER_NOSCE) == 0) {
1498                 adt= sce->adt;
1499                 ANIMDATA_FILTER_CASES(sce,
1500                         { /* AnimData blocks - do nothing... */ },
1501                         { /* nla */
1502                                 /* add NLA tracks */
1503                                 items += animdata_filter_nla(anim_data, ads, adt, filter_mode, sce, ANIMTYPE_SCENE, (ID *)sce);
1504                         },
1505                         { /* drivers */
1506                                 /* include drivers-expand widget? */
1507                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1508                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLDRIVERS, sce, ANIMTYPE_SCENE, (ID *)sce);
1509                                         if (ale) {
1510                                                 BLI_addtail(anim_data, ale);
1511                                                 items++;
1512                                         }
1513                                 }
1514                                 
1515                                 /* add F-Curve channels (drivers are F-Curves) */
1516                                 if (EXPANDED_DRVD(adt) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1517                                         items += animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, sce, ANIMTYPE_SCENE, filter_mode, (ID *)sce);
1518                                 }
1519                         },
1520                         { /* action */
1521                                 /* include action-expand widget? */
1522                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1523                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, sce, ANIMTYPE_SCENE, (ID *)sce);
1524                                         if (ale) {
1525                                                 BLI_addtail(anim_data, ale);
1526                                                 items++;
1527                                         }
1528                                 }
1529                                 
1530                                 /* add F-Curve channels? */
1531                                 if (EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1532                                         items += animdata_filter_action(anim_data, ads, adt->action, filter_mode, sce, ANIMTYPE_SCENE, (ID *)sce); 
1533                                 }
1534                         }
1535                 )
1536         }
1537         
1538         /* world */
1539         if ((wo && wo->adt) && !(ads->filterflag & ADS_FILTER_NOWOR)) {
1540                 /* Action, Drivers, or NLA for World */
1541                 adt= wo->adt;
1542                 ANIMDATA_FILTER_CASES(wo,
1543                         { /* AnimData blocks - do nothing... */ },
1544                         { /* nla */
1545                                 /* add NLA tracks */
1546                                 items += animdata_filter_nla(anim_data, ads, adt, filter_mode, wo, ANIMTYPE_DSWOR, (ID *)wo);
1547                         },
1548                         { /* drivers */
1549                                 /* include world-expand widget? */
1550                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1551                                         ale= make_new_animlistelem(wo, ANIMTYPE_DSWOR, sce, ANIMTYPE_SCENE, (ID *)wo);
1552                                         if (ale) {
1553                                                 BLI_addtail(anim_data, ale);
1554                                                 items++;
1555                                         }
1556                                 }
1557                                 
1558                                 /* add F-Curve channels (drivers are F-Curves) */
1559                                 if (FILTER_WOR_SCED(wo)/*EXPANDED_DRVD(adt)*/ || !(filter_mode & ANIMFILTER_CHANNELS)) {
1560                                         // XXX owner info is messed up now...
1561                                         items += animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, wo, ANIMTYPE_DSWOR, filter_mode, (ID *)wo);
1562                                 }
1563                         },
1564                         { /* action */
1565                                 /* include world-expand widget? */
1566                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1567                                         ale= make_new_animlistelem(wo, ANIMTYPE_DSWOR, sce, ANIMTYPE_SCENE, (ID *)sce);
1568                                         if (ale) {
1569                                                 BLI_addtail(anim_data, ale);
1570                                                 items++;
1571                                         }
1572                                 }
1573                                 
1574                                 /* add channels */
1575                                 if (FILTER_WOR_SCED(wo) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1576                                         items += animdata_filter_action(anim_data, ads, adt->action, filter_mode, wo, ANIMTYPE_DSWOR, (ID *)wo); 
1577                                 }
1578                         }
1579                 )
1580         }
1581         
1582         // TODO: scene compositing nodes (these aren't standard node-trees)
1583         
1584         /* return the number of items added to the list */
1585         return items;
1586 }
1587
1588 // TODO: implement pinning... (if and when pinning is done, what we need to do is to provide freeing mechanisms - to protect against data that was deleted)
1589 static int animdata_filter_dopesheet (ListBase *anim_data, bAnimContext *ac, bDopeSheet *ads, int filter_mode)
1590 {
1591         Scene *sce= (Scene *)ads->source;
1592         Base *base;
1593         bAnimListElem *ale;
1594         int items = 0;
1595         
1596         /* check that we do indeed have a scene */
1597         if ((ads->source == NULL) || (GS(ads->source->name)!=ID_SCE)) {
1598                 printf("DopeSheet Error: Not scene! \n");
1599                 return 0;
1600         }
1601         
1602         /* scene-linked animation */
1603         // TODO: sequencer, composite nodes - are we to include those here too?
1604         {
1605                 short sceOk= 0, worOk= 0;
1606                 
1607                 /* check filtering-flags if ok */
1608                 ANIMDATA_FILTER_CASES(sce, 
1609                         {
1610                                 /* for the special AnimData blocks only case, we only need to add
1611                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1612                                  */
1613                                 ANIMDATA_ADD_ANIMDATA(sce);
1614                                 sceOk=0;
1615                         },
1616                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);, 
1617                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);, 
1618                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);)
1619                 if (sce->world) {
1620                         ANIMDATA_FILTER_CASES(sce->world, 
1621                                 {
1622                                         /* for the special AnimData blocks only case, we only need to add
1623                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1624                                          */
1625                                         ANIMDATA_ADD_ANIMDATA(sce->world);
1626                                         worOk=0;
1627                                 },
1628                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);, 
1629                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);, 
1630                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);)
1631                 }
1632                 
1633                 /* if only F-Curves with visible flags set can be shown, check that 
1634                  * datablocks haven't been set to invisible 
1635                  */
1636                 if (filter_mode & ANIMFILTER_CURVEVISIBLE) {
1637                         if ((sce->adt) && (sce->adt->flag & ADT_CURVES_NOT_VISIBLE))
1638                                 sceOk= worOk= 0;
1639                 }
1640                 
1641                 /* check if not all bad (i.e. so there is something to show) */
1642                 if ( !(!sceOk && !worOk) ) {
1643                         /* add scene data to the list of filtered channels */
1644                         items += animdata_filter_dopesheet_scene(anim_data, ads, sce, filter_mode);
1645                 }
1646         }
1647         
1648         
1649         /* loop over all bases in the scene */
1650         for (base= sce->base.first; base; base= base->next) {
1651                 /* check if there's an object (all the relevant checks are done in the ob-function) */
1652                 if (base->object) {
1653                         Object *ob= base->object;
1654                         Key *key= ob_get_key(ob);
1655                         short actOk=1, keyOk=1, dataOk=1, matOk=1, partOk=1;
1656                         
1657                         /* firstly, check if object can be included, by the following factors:
1658                          *      - if only visible, must check for layer and also viewport visibility
1659                          *      - if only selected, must check if object is selected 
1660                          *      - there must be animation data to edit
1661                          */
1662                         // TODO: if cache is implemented, just check name here, and then 
1663                         if (filter_mode & ANIMFILTER_VISIBLE) {
1664                                 /* layer visibility - we check both object and base, since these may not be in sync yet */
1665                                 if ((sce->lay & (ob->lay|base->lay))==0) continue;
1666                                 
1667                                 /* outliner restrict-flag */
1668                                 if (ob->restrictflag & OB_RESTRICT_VIEW) continue;
1669                         }
1670                         
1671                         /* if only F-Curves with visible flags set can be shown, check that 
1672                          * datablock hasn't been set to invisible 
1673                          */
1674                         if (filter_mode & ANIMFILTER_CURVEVISIBLE) {
1675                                 if ((ob->adt) && (ob->adt->flag & ADT_CURVES_NOT_VISIBLE))
1676                                         continue;
1677                         }
1678                         
1679                         /* additionally, dopesheet filtering also affects what objects to consider */
1680                         if (ads->filterflag) {
1681                                 /* check selection and object type filters */
1682                                 if ( (ads->filterflag & ADS_FILTER_ONLYSEL) && !((base->flag & SELECT) || (base == sce->basact)) )  {
1683                                         /* only selected should be shown */
1684                                         continue;
1685                                 }
1686                                 
1687                                 /* check filters for datatypes */
1688                                         /* object */
1689                                 actOk= 0;
1690                                 keyOk= 0;
1691                                 ANIMDATA_FILTER_CASES(ob, 
1692                                         {
1693                                                 /* for the special AnimData blocks only case, we only need to add
1694                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1695                                                  */
1696                                                 ANIMDATA_ADD_ANIMDATA(ob);
1697                                                 actOk=0;
1698                                         },
1699                                         actOk= 1;, 
1700                                         actOk= 1;, 
1701                                         actOk= 1;)
1702                                 if (key) {
1703                                         /* shapekeys */
1704                                         ANIMDATA_FILTER_CASES(key, 
1705                                                 {
1706                                                         /* for the special AnimData blocks only case, we only need to add
1707                                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1708                                                          */
1709                                                         ANIMDATA_ADD_ANIMDATA(key);
1710                                                         keyOk=0;
1711                                                 },
1712                                                 keyOk= 1;, 
1713                                                 keyOk= 1;, 
1714                                                 keyOk= 1;)
1715                                 }
1716                                 
1717                                 /* materials - only for geometric types */
1718                                 matOk= 0; /* by default, not ok... */
1719                                 if ( !(ads->filterflag & ADS_FILTER_NOMAT) && (ob->totcol) && 
1720                                          ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) ) 
1721                                 {
1722                                         int a;
1723                                         
1724                                         /* firstly check that we actuallly have some materials */
1725                                         for (a=0; a < ob->totcol; a++) {
1726                                                 Material *ma= give_current_material(ob, a);
1727                                                 
1728                                                 if (ma) {
1729                                                         /* if material has relevant animation data, break */
1730                                                         ANIMDATA_FILTER_CASES(ma, 
1731                                                                 {
1732                                                                         /* for the special AnimData blocks only case, we only need to add
1733                                                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1734                                                                          */
1735                                                                         ANIMDATA_ADD_ANIMDATA(ma);
1736                                                                         matOk=0;
1737                                                                 },
1738                                                                 matOk= 1;, 
1739                                                                 matOk= 1;, 
1740                                                                 matOk= 1;)
1741                                                 }
1742                                                         
1743                                                 if (matOk) 
1744                                                         break;
1745                                         }
1746                                 }
1747                                 
1748                                 /* data */
1749                                 switch (ob->type) {
1750                                         case OB_CAMERA: /* ------- Camera ------------ */
1751                                         {
1752                                                 Camera *ca= (Camera *)ob->data;
1753                                                 dataOk= 0;
1754                                                 ANIMDATA_FILTER_CASES(ca, 
1755                                                         if ((ads->filterflag & ADS_FILTER_NOCAM)==0) {
1756                                                                 /* for the special AnimData blocks only case, we only need to add
1757                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1758                                                                  */
1759                                                                 ANIMDATA_ADD_ANIMDATA(ca);
1760                                                                 dataOk=0;
1761                                                         },
1762                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);, 
1763                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);, 
1764                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);)
1765                                         }
1766                                                 break;
1767                                         case OB_LAMP: /* ---------- Lamp ----------- */
1768                                         {
1769                                                 Lamp *la= (Lamp *)ob->data;
1770                                                 dataOk= 0;
1771                                                 ANIMDATA_FILTER_CASES(la, 
1772                                                         if ((ads->filterflag & ADS_FILTER_NOLAM)==0) {
1773                                                                 /* for the special AnimData blocks only case, we only need to add
1774                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1775                                                                  */
1776                                                                 ANIMDATA_ADD_ANIMDATA(la);
1777                                                                 dataOk=0;
1778                                                         },
1779                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);, 
1780                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);, 
1781                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);)
1782                                         }
1783                                                 break;
1784                                         case OB_CURVE: /* ------- Curve ---------- */
1785                                         {
1786                                                 Curve *cu= (Curve *)ob->data;
1787                                                 dataOk= 0;
1788                                                 ANIMDATA_FILTER_CASES(cu, 
1789                                                         if ((ads->filterflag & ADS_FILTER_NOCUR)==0) {
1790                                                                 /* for the special AnimData blocks only case, we only need to add
1791                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1792                                                                  */
1793                                                                 ANIMDATA_ADD_ANIMDATA(cu);
1794                                                                 dataOk=0;
1795                                                         },
1796                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCUR);, 
1797                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCUR);, 
1798                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCUR);)
1799                                         }
1800                                                 break;
1801                                         case OB_MBALL: /* ------- MetaBall ---------- */
1802                                         {
1803                                                 MetaBall *mb= (MetaBall *)ob->data;
1804                                                 dataOk= 0;
1805                                                 ANIMDATA_FILTER_CASES(mb, 
1806                                                         if ((ads->filterflag & ADS_FILTER_NOMBA)==0) {
1807                                                                 /* for the special AnimData blocks only case, we only need to add
1808                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1809                                                                  */
1810                                                                 ANIMDATA_ADD_ANIMDATA(mb);
1811                                                                 dataOk=0;
1812                                                         },
1813                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOMBA);, 
1814                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOMBA);, 
1815                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOMBA);)
1816                                         }
1817                                                 break;
1818                                         case OB_ARMATURE: /* ------- Armature ---------- */
1819                                         {
1820                                                 bArmature *arm= (bArmature *)ob->data;
1821                                                 dataOk= 0;
1822                                                 ANIMDATA_FILTER_CASES(arm, 
1823                                                         if ((ads->filterflag & ADS_FILTER_NOARM)==0) {
1824                                                                 /* for the special AnimData blocks only case, we only need to add
1825                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1826                                                                  */
1827                                                                 ANIMDATA_ADD_ANIMDATA(arm);
1828                                                                 dataOk=0;
1829                                                         },
1830                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOARM);, 
1831                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOARM);, 
1832                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOARM);)
1833                                         }
1834                                                 break;
1835                                         default: /* --- other --- */
1836                                                 dataOk= 0;
1837                                                 break;
1838                                 }
1839                                 
1840                                 /* particles */
1841                                 partOk = 0;
1842                                 if (!(ads->filterflag & ADS_FILTER_NOPART) && ob->particlesystem.first) {
1843                                         ParticleSystem *psys = ob->particlesystem.first;
1844                                         for(; psys; psys=psys->next) {
1845                                                 if (psys->part) {
1846                                                         /* if particlesettings has relevant animation data, break */
1847                                                         ANIMDATA_FILTER_CASES(psys->part, 
1848                                                                 {
1849                                                                         /* for the special AnimData blocks only case, we only need to add
1850                                                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1851                                                                          */
1852                                                                         ANIMDATA_ADD_ANIMDATA(psys->part);
1853                                                                         partOk=0;
1854                                                                 },
1855                                                                 partOk= 1;, 
1856                                                                 partOk= 1;, 
1857                                                                 partOk= 1;)
1858                                                 }
1859                                                         
1860                                                 if (partOk) 
1861                                                         break;
1862                                         }
1863                                 }
1864                                 
1865                                 /* check if all bad (i.e. nothing to show) */
1866                                 if (!actOk && !keyOk && !dataOk && !matOk && !partOk)
1867                                         continue;
1868                         }
1869                         else {
1870                                 /* check data-types */
1871                                 actOk= ANIMDATA_HAS_KEYS(ob);
1872                                 keyOk= (key != NULL);
1873                                 
1874                                 /* materials - only for geometric types */
1875                                 matOk= 0; /* by default, not ok... */
1876                                 if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) && (ob->totcol)) 
1877                                 {
1878                                         int a;
1879                                         
1880                                         /* firstly check that we actuallly have some materials */
1881                                         for (a=0; a < ob->totcol; a++) {
1882                                                 Material *ma= give_current_material(ob, a);
1883                                                 
1884                                                 if ((ma) && ANIMDATA_HAS_KEYS(ma)) {
1885                                                         matOk= 1;
1886                                                         break;
1887                                                 }
1888                                         }
1889                                 }
1890                                 
1891                                 /* data */
1892                                 switch (ob->type) {
1893                                         case OB_CAMERA: /* ------- Camera ------------ */
1894                                         {
1895                                                 Camera *ca= (Camera *)ob->data;
1896                                                 dataOk= ANIMDATA_HAS_KEYS(ca);                                          
1897                                         }
1898                                                 break;
1899                                         case OB_LAMP: /* ---------- Lamp ----------- */
1900                                         {
1901                                                 Lamp *la= (Lamp *)ob->data;
1902                                                 dataOk= ANIMDATA_HAS_KEYS(la);  
1903                                         }
1904                                                 break;
1905                                         case OB_CURVE: /* -------- Curve ---------- */
1906                                         {
1907                                                 Curve *cu= (Curve *)ob->data;
1908                                                 dataOk= ANIMDATA_HAS_KEYS(cu);  
1909                                         }
1910                                                 break;
1911                                         case OB_MBALL: /* -------- Metas ---------- */
1912                                         {
1913                                                 MetaBall *mb= (MetaBall *)ob->data;
1914                                                 dataOk= ANIMDATA_HAS_KEYS(mb);  
1915                                         }
1916                                                 break;
1917                                         case OB_ARMATURE: /* -------- Armature ---------- */
1918                                         {
1919                                                 bArmature *arm= (bArmature *)ob->data;
1920                                                 dataOk= ANIMDATA_HAS_KEYS(arm); 
1921                                         }
1922                                                 break;
1923                                         default: /* --- other --- */
1924                                                 dataOk= 0;
1925                                                 break;
1926                                 }
1927                                 
1928                                 /* particles */
1929                                 partOk = 0;
1930                                 if (ob->particlesystem.first) {
1931                                         ParticleSystem *psys = ob->particlesystem.first;
1932                                         for(; psys; psys=psys->next) {
1933                                                 if(psys->part && ANIMDATA_HAS_KEYS(psys->part)) {
1934                                                         partOk = 1;
1935                                                         break;
1936                                                 }
1937                                         }
1938                                 }
1939                                 
1940                                 /* check if all bad (i.e. nothing to show) */
1941                                 if (!actOk && !keyOk && !dataOk && !matOk && !partOk)
1942                                         continue;
1943                         }
1944                         
1945                         /* since we're still here, this object should be usable */
1946                         items += animdata_filter_dopesheet_ob(anim_data, ads, base, filter_mode);
1947                 }
1948         }
1949         
1950         /* return the number of items in the list */
1951         return items;
1952 }
1953
1954 /* Summary track for DopeSheet/Action Editor 
1955  *      - return code is whether the summary lets the other channels get drawn
1956  */
1957 static short animdata_filter_dopesheet_summary (bAnimContext *ac, ListBase *anim_data, int filter_mode, int *items)
1958 {
1959         bDopeSheet *ads = NULL;
1960         
1961         /* get the DopeSheet information to use 
1962          *      - we should only need to deal with the DopeSheet/Action Editor, 
1963          *        since all the other Animation Editors won't have this concept
1964          *        being applicable.
1965          */
1966         if ((ac && ac->sa) && (ac->sa->spacetype == SPACE_ACTION)) {
1967                 SpaceAction *saction= (SpaceAction *)ac->sa->spacedata.first;
1968                 ads= &saction->ads;
1969         }
1970         else {
1971                 /* invalid space type - skip this summary channels */
1972                 return 1;
1973         }
1974         
1975         /* dopesheet summary 
1976          *      - only for drawing and/or selecting keyframes in channels, but not for real editing 
1977          *      - only useful for DopeSheet Editor, where the summary is useful
1978          */
1979         // TODO: we should really check if some other prohibited filters are also active, but that can be for later
1980         if ((filter_mode & ANIMFILTER_CHANNELS) && (ads->filterflag & ADS_FILTER_SUMMARY)) {
1981                 bAnimListElem *ale= make_new_animlistelem(ac, ANIMTYPE_SUMMARY, NULL, ANIMTYPE_NONE, NULL);
1982                 if (ale) {
1983                         BLI_addtail(anim_data, ale);
1984                         (*items)++;
1985                 }
1986                 
1987                 /* if summary is collapsed, don't show other channels beneath this 
1988                  *      - this check is put inside the summary check so that it doesn't interfere with normal operation
1989                  */ 
1990                 if (ads->flag & ADS_FLAG_SUMMARY_COLLAPSED)
1991                         return 0;
1992         }
1993         
1994         /* the other channels beneath this can be shown */
1995         return 1;
1996 }  
1997
1998 /* ----------- Public API --------------- */
1999
2000 /* This function filters the active data source to leave only animation channels suitable for
2001  * usage by the caller. It will return the length of the list 
2002  * 
2003  *      *anim_data: is a pointer to a ListBase, to which the filtered animation channels
2004  *              will be placed for use.
2005  *      filter_mode: how should the data be filtered - bitmapping accessed flags
2006  */
2007 int ANIM_animdata_filter (bAnimContext *ac, ListBase *anim_data, int filter_mode, void *data, short datatype)
2008 {
2009         int items = 0;
2010         
2011         /* only filter data if there's somewhere to put it */
2012         if (data && anim_data) {
2013                 bAnimListElem *ale, *next;
2014                 Object *obact= (ac) ? ac->obact : NULL;
2015                 
2016                 /* firstly filter the data */
2017                 switch (datatype) {
2018                         case ANIMCONT_ACTION:   /* 'Action Editor' */
2019                         {
2020                                 /* the check for the DopeSheet summary is included here since the summary works here too */
2021                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2022                                         items += animdata_filter_action(anim_data, NULL, data, filter_mode, NULL, ANIMTYPE_NONE, (ID *)obact);
2023                         }
2024                                 break;
2025                                 
2026                         case ANIMCONT_SHAPEKEY: /* 'ShapeKey Editor' */
2027                         {
2028                                 /* the check for the DopeSheet summary is included here since the summary works here too */
2029                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2030                                         items= animdata_filter_shapekey(anim_data, data, filter_mode);
2031                         }
2032                                 break;
2033                                 
2034                         case ANIMCONT_GPENCIL:
2035                         {
2036                                 //items= animdata_filter_gpencil(anim_data, data, filter_mode);
2037                         }
2038                                 break;
2039                                 
2040                         case ANIMCONT_DOPESHEET: /* 'DopeSheet Editor' */
2041                         {
2042                                 /* the DopeSheet editor is the primary place where the DopeSheet summaries are useful */
2043                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2044                                         items += animdata_filter_dopesheet(anim_data, ac, data, filter_mode);
2045                         }
2046                                 break;
2047                                 
2048                         case ANIMCONT_FCURVES: /* Graph Editor -> FCurves/Animation Editing */
2049                         case ANIMCONT_DRIVERS: /* Graph Editor -> Drivers Editing */
2050                         case ANIMCONT_NLA: /* NLA Editor */
2051                         {
2052                                 /* all of these editors use the basic DopeSheet data for filtering options, but don't have all the same features */
2053                                 items = animdata_filter_dopesheet(anim_data, ac, data, filter_mode);
2054                         }
2055                                 break;
2056                 }
2057                         
2058                 /* remove any weedy entries */
2059                 // XXX this is weedy code!
2060                 for (ale= anim_data->first; ale; ale= next) {
2061                         next= ale->next;
2062                         
2063                         if (ale->type == ANIMTYPE_NONE) {
2064                                 items--;
2065                                 BLI_freelinkN(anim_data, ale);
2066                         }
2067                 }
2068         }
2069         
2070         /* return the number of items in the list */
2071         return items;
2072 }
2073
2074 /* ************************************************************ */