Merged changes in the trunk up to revision 45619.
[blender.git] / source / blender / editors / animation / anim_channels_edit.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2009 Blender Foundation, Joshua Leung
19  * All rights reserved.
20  *
21  * Contributor(s): Joshua Leung
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/animation/anim_channels_edit.c
27  *  \ingroup edanimation
28  */
29
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h> 
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_utildefines.h"
39 #include "BKE_library.h"
40
41 #include "DNA_anim_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_key_types.h"
45 #include "DNA_gpencil_types.h"
46
47 #include "RNA_access.h"
48 #include "RNA_define.h"
49
50 #include "BKE_action.h"
51 #include "BKE_fcurve.h"
52 #include "BKE_gpencil.h"
53 #include "BKE_context.h"
54 #include "BKE_global.h"
55
56 #include "UI_view2d.h"
57
58 #include "ED_anim_api.h"
59 #include "ED_keyframes_edit.h" // XXX move the select modes out of there!
60 #include "ED_screen.h"
61
62 #include "WM_api.h"
63 #include "WM_types.h"
64
65 /* ************************************************************************** */
66 /* CHANNELS API - Exposed API */
67
68 /* -------------------------- Selection ------------------------------------- */
69
70 /* Set the given animation-channel as the active one for the active context */
71 // TODO: extend for animdata types...
72 void ANIM_set_active_channel (bAnimContext *ac, void *data, short datatype, int filter, void *channel_data, short channel_type)
73 {
74         ListBase anim_data = {NULL, NULL};
75         bAnimListElem *ale;
76         
77         /* try to build list of filtered items */
78         ANIM_animdata_filter(ac, &anim_data, filter, data, datatype);
79         if (anim_data.first == NULL)
80                 return;
81                 
82         /* only clear the 'active' flag for the channels of the same type */
83         for (ale= anim_data.first; ale; ale= ale->next) {
84                 /* skip if types don't match */
85                 if (channel_type != ale->type)
86                         continue;
87                 
88                 /* flag to set depends on type */
89                 switch (ale->type) {
90                         case ANIMTYPE_GROUP:
91                         {
92                                 bActionGroup *agrp= (bActionGroup *)ale->data;
93                                 
94                                 ACHANNEL_SET_FLAG(agrp, ACHANNEL_SETFLAG_CLEAR, AGRP_ACTIVE);
95                         }
96                                 break;
97                         case ANIMTYPE_FCURVE:
98                         {
99                                 FCurve *fcu= (FCurve *)ale->data;
100                                 
101                                 ACHANNEL_SET_FLAG(fcu, ACHANNEL_SETFLAG_CLEAR, FCURVE_ACTIVE);
102                         }
103                                 break;
104                         case ANIMTYPE_NLATRACK:
105                         {
106                                 NlaTrack *nlt= (NlaTrack *)ale->data;
107                                 
108                                 ACHANNEL_SET_FLAG(nlt, ACHANNEL_SETFLAG_CLEAR, NLATRACK_ACTIVE);
109                         }
110                                 break;
111                         
112                         case ANIMTYPE_FILLACTD: /* Action Expander */
113                         case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
114                         case ANIMTYPE_DSLAM:
115                         case ANIMTYPE_DSCAM:
116                         case ANIMTYPE_DSCUR:
117                         case ANIMTYPE_DSSKEY:
118                         case ANIMTYPE_DSWOR:
119                         case ANIMTYPE_DSPART:
120                         case ANIMTYPE_DSMBALL:
121                         case ANIMTYPE_DSARM:
122                         case ANIMTYPE_DSMESH:
123                         case ANIMTYPE_DSTEX:
124                         case ANIMTYPE_DSLAT:
125                         case ANIMTYPE_DSLINESTYLE:
126                         case ANIMTYPE_DSSPK:
127                         {
128                                 /* need to verify that this data is valid for now */
129                                 if (ale->adt) {
130                                         ACHANNEL_SET_FLAG(ale->adt, ACHANNEL_SETFLAG_CLEAR, ADT_UI_ACTIVE);
131                                 }
132                         }
133                                 break;
134                 }
135         }
136         
137         /* set active flag */
138         if (channel_data) {
139                 switch (channel_type) {
140                         case ANIMTYPE_GROUP:
141                         {
142                                 bActionGroup *agrp= (bActionGroup *)channel_data;
143                                 agrp->flag |= AGRP_ACTIVE;
144                         }
145                                 break;
146                         case ANIMTYPE_FCURVE:
147                         {
148                                 FCurve *fcu= (FCurve *)channel_data;
149                                 fcu->flag |= FCURVE_ACTIVE;
150                         }
151                                 break;
152                         case ANIMTYPE_NLATRACK:
153                         {
154                                 NlaTrack *nlt= (NlaTrack *)channel_data;
155                                 nlt->flag |= NLATRACK_ACTIVE;
156                         }
157                                 break;
158                                 
159                         case ANIMTYPE_FILLACTD: /* Action Expander */
160                         case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
161                         case ANIMTYPE_DSLAM:
162                         case ANIMTYPE_DSCAM:
163                         case ANIMTYPE_DSCUR:
164                         case ANIMTYPE_DSSKEY:
165                         case ANIMTYPE_DSWOR:
166                         case ANIMTYPE_DSPART:
167                         case ANIMTYPE_DSMBALL:
168                         case ANIMTYPE_DSARM:
169                         case ANIMTYPE_DSMESH:
170                         case ANIMTYPE_DSLAT:
171                         case ANIMTYPE_DSLINESTYLE:
172                         case ANIMTYPE_DSSPK:
173                         {
174                                 /* need to verify that this data is valid for now */
175                                 if (ale && ale->adt) {
176                                         ale->adt->flag |= ADT_UI_ACTIVE;
177                                 }
178                         }
179                                 break;
180                 }
181         }
182         
183         /* clean up */
184         BLI_freelistN(&anim_data);
185 }
186
187 /* Deselect all animation channels 
188  *      - data: pointer to datatype, as contained in bAnimContext
189  *      - datatype: the type of data that 'data' represents (eAnimCont_Types)
190  *      - test: check if deselecting instead of selecting
191  *      - sel: eAnimChannels_SetFlag;
192  */
193 void ANIM_deselect_anim_channels (bAnimContext *ac, void *data, short datatype, short test, short sel)
194 {
195         ListBase anim_data = {NULL, NULL};
196         bAnimListElem *ale;
197         int filter;
198         
199         /* filter data */
200         /* NOTE: no list visible, otherwise, we get dangling */
201         filter= ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_CHANNELS;
202         ANIM_animdata_filter(ac, &anim_data, filter, data, datatype);
203         
204         /* See if we should be selecting or deselecting */
205         if (test) {
206                 for (ale= anim_data.first; ale; ale= ale->next) {
207                         if (sel == 0) 
208                                 break;
209                         
210                         switch (ale->type) {
211                                 case ANIMTYPE_SCENE:
212                                         if (ale->flag & SCE_DS_SELECTED)
213                                                 sel= ACHANNEL_SETFLAG_CLEAR;
214                                         break;
215                                 case ANIMTYPE_OBJECT:
216                                 #if 0   /* for now, do not take object selection into account, since it gets too annoying */
217                                         if (ale->flag & SELECT)
218                                                 sel= ACHANNEL_SETFLAG_CLEAR;
219                                 #endif
220                                         break;
221                                 case ANIMTYPE_GROUP:
222                                         if (ale->flag & AGRP_SELECTED)
223                                                 sel= ACHANNEL_SETFLAG_CLEAR;
224                                         break;
225                                 case ANIMTYPE_FCURVE:
226                                         if (ale->flag & FCURVE_SELECTED)
227                                                 sel= ACHANNEL_SETFLAG_CLEAR;
228                                         break;
229                                 case ANIMTYPE_SHAPEKEY:
230                                         if (ale->flag & KEYBLOCK_SEL)
231                                                 sel= ACHANNEL_SETFLAG_CLEAR;
232                                         break;
233                                 case ANIMTYPE_NLATRACK:
234                                         if (ale->flag & NLATRACK_SELECTED)
235                                                 sel= ACHANNEL_SETFLAG_CLEAR;
236                                         break;
237                                         
238                                 case ANIMTYPE_FILLACTD: /* Action Expander */
239                                 case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
240                                 case ANIMTYPE_DSLAM:
241                                 case ANIMTYPE_DSCAM:
242                                 case ANIMTYPE_DSCUR:
243                                 case ANIMTYPE_DSSKEY:
244                                 case ANIMTYPE_DSWOR:
245                                 case ANIMTYPE_DSPART:
246                                 case ANIMTYPE_DSMBALL:
247                                 case ANIMTYPE_DSARM:
248                                 case ANIMTYPE_DSMESH:
249                                 case ANIMTYPE_DSNTREE:
250                                 case ANIMTYPE_DSTEX:
251                                 case ANIMTYPE_DSLAT:
252                                 case ANIMTYPE_DSLINESTYLE:
253                                 case ANIMTYPE_DSSPK:
254                                 {
255                                         if ((ale->adt) && (ale->adt->flag & ADT_UI_SELECTED))
256                                                 sel= ACHANNEL_SETFLAG_CLEAR;
257                                 }
258                                         break;
259                                         
260                                 case ANIMTYPE_GPLAYER:
261                                         if (ale->flag & GP_LAYER_SELECT)
262                                                 sel= ACHANNEL_SETFLAG_CLEAR;
263                                         break;
264                         }
265                 }
266         }
267                 
268         /* Now set the flags */
269         for (ale= anim_data.first; ale; ale= ale->next) {
270                 switch (ale->type) {
271                         case ANIMTYPE_SCENE:
272                         {
273                                 Scene *scene= (Scene *)ale->data;
274                                 
275                                 ACHANNEL_SET_FLAG(scene, sel, SCE_DS_SELECTED);
276                                 
277                                 if (scene->adt) {
278                                         ACHANNEL_SET_FLAG(scene, sel, ADT_UI_SELECTED);
279                                 }
280                         }
281                                 break;
282                         case ANIMTYPE_OBJECT:
283                         #if 0   /* for now, do not take object selection into account, since it gets too annoying */
284                         {
285                                 Base *base= (Base *)ale->data;
286                                 Object *ob= base->object;
287                                 
288                                 ACHANNEL_SET_FLAG(base, sel, SELECT);
289                                 ACHANNEL_SET_FLAG(ob, sel, SELECT);
290                                 
291                                 if (ob->adt) {
292                                         ACHANNEL_SET_FLAG(ob, sel, ADT_UI_SELECTED);
293                                 }
294                         }
295                         #endif
296                                 break;
297                         case ANIMTYPE_GROUP:
298                         {
299                                 bActionGroup *agrp= (bActionGroup *)ale->data;
300                                 
301                                 ACHANNEL_SET_FLAG(agrp, sel, AGRP_SELECTED);
302                                 agrp->flag &= ~AGRP_ACTIVE;
303                         }
304                                 break;
305                         case ANIMTYPE_FCURVE:
306                         {
307                                 FCurve *fcu= (FCurve *)ale->data;
308                                 
309                                 ACHANNEL_SET_FLAG(fcu, sel, FCURVE_SELECTED);
310                                 fcu->flag &= ~FCURVE_ACTIVE;
311                         }
312                                 break;
313                         case ANIMTYPE_SHAPEKEY:
314                         {
315                                 KeyBlock *kb= (KeyBlock *)ale->data;
316                                 
317                                 ACHANNEL_SET_FLAG(kb, sel, KEYBLOCK_SEL);
318                         }
319                                 break;
320                         case ANIMTYPE_NLATRACK:
321                         {
322                                 NlaTrack *nlt= (NlaTrack *)ale->data;
323                                 
324                                 ACHANNEL_SET_FLAG(nlt, sel, NLATRACK_SELECTED);
325                                 nlt->flag &= ~NLATRACK_ACTIVE;
326                         }
327                                 break;
328                                 
329                         case ANIMTYPE_FILLACTD: /* Action Expander */
330                         case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
331                         case ANIMTYPE_DSLAM:
332                         case ANIMTYPE_DSCAM:
333                         case ANIMTYPE_DSCUR:
334                         case ANIMTYPE_DSSKEY:
335                         case ANIMTYPE_DSWOR:
336                         case ANIMTYPE_DSPART:
337                         case ANIMTYPE_DSMBALL:
338                         case ANIMTYPE_DSARM:
339                         case ANIMTYPE_DSMESH:
340                         case ANIMTYPE_DSNTREE:
341                         case ANIMTYPE_DSTEX:
342                         case ANIMTYPE_DSLAT:
343                         case ANIMTYPE_DSLINESTYLE:
344                         case ANIMTYPE_DSSPK:
345                         {
346                                 /* need to verify that this data is valid for now */
347                                 if (ale->adt) {
348                                         ACHANNEL_SET_FLAG(ale->adt, sel, ADT_UI_SELECTED);
349                                         ale->adt->flag &= ~ADT_UI_ACTIVE;
350                                 }
351                         }
352                                 break;
353                                 
354                         case ANIMTYPE_GPLAYER:
355                         {
356                                 bGPDlayer *gpl = (bGPDlayer *)ale->data;
357                                 
358                                 ACHANNEL_SET_FLAG(gpl, sel, GP_LAYER_SELECT);
359                         }
360                                 break;
361                 }
362         }
363         
364         /* Cleanup */
365         BLI_freelistN(&anim_data);
366 }
367
368 /* ---------------------------- Graph Editor ------------------------------------- */
369
370 /* Flush visibility (for Graph Editor) changes up/down hierarchy for changes in the given setting 
371  *      - anim_data: list of the all the anim channels that can be chosen
372  *              -> filtered using ANIMFILTER_CHANNELS only, since if we took VISIBLE too,
373  *                then the channels under closed expanders get ignored...
374  *      - ale_setting: the anim channel (not in the anim_data list directly, though occurring there)
375  *              with the new state of the setting that we want flushed up/down the hierarchy 
376  *      - setting: type of setting to set
377  *      - on: whether the visibility setting has been enabled or disabled 
378  */
379 void ANIM_flush_setting_anim_channels (bAnimContext *ac, ListBase *anim_data, bAnimListElem *ale_setting, int setting, short on)
380 {
381         bAnimListElem *ale, *match=NULL;
382         int prevLevel=0, matchLevel=0;
383         
384         /* sanity check */
385         if (ELEM(NULL, anim_data, anim_data->first))
386                 return;
387         
388         /* find the channel that got changed */
389         for (ale= anim_data->first; ale; ale= ale->next) {
390                 /* compare data, and type as main way of identifying the channel */
391                 if ((ale->data == ale_setting->data) && (ale->type == ale_setting->type)) {
392                         /* we also have to check the ID, this is assigned to, since a block may have multiple users */
393                         // TODO: is the owner-data more revealing?
394                         if (ale->id == ale_setting->id) {
395                                 match= ale;
396                                 break;
397                         }
398                 }
399         }
400         if (match == NULL) {
401                 printf("ERROR: no channel matching the one changed was found\n");
402                 return;
403         }
404         else {
405                 bAnimChannelType *acf= ANIM_channel_get_typeinfo(ale_setting);
406                 
407                 if (acf == NULL) {
408                         printf("ERROR: no channel info for the changed channel\n");
409                         return;
410                 }
411                 
412                 /* get the level of the channel that was affected
413                  *       - we define the level as simply being the offset for the start of the channel
414                  */
415                 matchLevel= (acf->get_offset)? acf->get_offset(ac, ale_setting) : 0;
416                 prevLevel= matchLevel;
417         }
418         
419         /* flush up? 
420          *
421          * For Visibility:
422          *      - only flush up if the current state is now enabled (positive 'on' state is default) 
423          *        (otherwise, it's too much work to force the parents to be inactive too)
424          *
425          * For everything else:
426          *      - only flush up if the current state is now disabled (negative 'off' state is default)
427          *        (otherwise, it's too much work to force the parents to be active too)
428          */
429         if ( ((setting == ACHANNEL_SETTING_VISIBLE) && on) ||
430                  ((setting != ACHANNEL_SETTING_VISIBLE) && on==0) )
431         {
432                 /* go backwards in the list, until the highest-ranking element (by indention has been covered) */
433                 for (ale= match->prev; ale; ale= ale->prev) {
434                         bAnimChannelType *acf= ANIM_channel_get_typeinfo(ale);
435                         int level;
436                         
437                         /* if no channel info was found, skip, since this type might not have any useful info */
438                         if (acf == NULL)
439                                 continue;
440                         
441                         /* get the level of the current channel traversed 
442                          *       - we define the level as simply being the offset for the start of the channel
443                          */
444                         level= (acf->get_offset)? acf->get_offset(ac, ale) : 0;
445                         
446                         /* if the level is 'less than' (i.e. more important) the level we're matching
447                          * but also 'less than' the level just tried (i.e. only the 1st group above grouped F-Curves, 
448                          * when toggling visibility of F-Curves, gets flushed, which should happen if we don't let prevLevel
449                          * get updated below once the first 1st group is found)...
450                          */
451                         if (level < prevLevel) {
452                                 /* flush the new status... */
453                                 ANIM_channel_setting_set(ac, ale, setting, on);
454                                 
455                                 /* store this level as the 'old' level now */
456                                 prevLevel= level;
457                         }       
458                         /* if the level is 'greater than' (i.e. less important) than the previous level... */
459                         else if (level > prevLevel) {
460                                 /* if previous level was a base-level (i.e. 0 offset / root of one hierarchy),
461                                  * stop here
462                                  */
463                                 if (prevLevel == 0)
464                                         break;
465                                 /* otherwise, this level weaves into another sibling hierarchy to the previous one just
466                                  * finished, so skip until we get to the parent of this level 
467                                  */
468                                 else
469                                         continue;
470                         }
471                 }
472         }
473         
474         /* flush down (always) */
475         {
476                 /* go forwards in the list, until the lowest-ranking element (by indention has been covered) */
477                 for (ale= match->next; ale; ale= ale->next) {
478                         bAnimChannelType *acf= ANIM_channel_get_typeinfo(ale);
479                         int level;
480                         
481                         /* if no channel info was found, skip, since this type might not have any useful info */
482                         if (acf == NULL)
483                                 continue;
484                         
485                         /* get the level of the current channel traversed 
486                          *       - we define the level as simply being the offset for the start of the channel
487                          */
488                         level= (acf->get_offset)? acf->get_offset(ac, ale) : 0;
489                         
490                         /* if the level is 'greater than' (i.e. less important) the channel that was changed, 
491                          * flush the new status...
492                          */
493                         if (level > matchLevel)
494                                 ANIM_channel_setting_set(ac, ale, setting, on);
495                         /* however, if the level is 'less than or equal to' the channel that was changed,
496                          * (i.e. the current channel is as important if not more important than the changed channel)
497                          * then we should stop, since we've found the last one of the children we should flush
498                          */
499                         else
500                                 break;
501                         
502                         /* store this level as the 'old' level now */
503                         // prevLevel= level; // XXX: prevLevel is unused
504                 }
505         }
506 }
507
508 /* -------------------------- F-Curves ------------------------------------- */
509
510 /* Delete the given F-Curve from its AnimData block */
511 void ANIM_fcurve_delete_from_animdata (bAnimContext *ac, AnimData *adt, FCurve *fcu)
512 {
513         /* - if no AnimData, we've got nowhere to remove the F-Curve from 
514          *      (this doesn't guarantee that the F-Curve is in there, but at least we tried
515          * - if no F-Curve, there is nothing to remove
516          */
517         if (ELEM(NULL, adt, fcu))
518                 return;
519                 
520         /* remove from whatever list it came from
521          *      - Action Group
522          *      - Action
523          *      - Drivers
524          *      - TODO... some others?
525          */
526         if ((ac) && (ac->datatype == ANIMCONT_DRIVERS)) {
527                 /* driver F-Curve */
528                 BLI_remlink(&adt->drivers, fcu);
529         }
530         else if (adt->action) {
531                 /* remove from group or action, whichever one "owns" the F-Curve */
532                 if (fcu->grp)
533                         action_groups_remove_channel(adt->action, fcu);
534                 else
535                         BLI_remlink(&adt->action->curves, fcu);
536                         
537                 /* if action has no more F-Curves as a result of this, unlink it from
538                  * AnimData if it did not come from a NLA Strip being tweaked.
539                  *
540                  * This is done so that we don't have dangling Object+Action entries in
541                  * channel list that are empty, and linger around long after the data they
542                  * are for has disappeared (and probably won't come back).
543                  */
544                         // XXX: does everybody always want this?
545                         /* XXX: there's a problem where many actions could build up in the file if multiple
546                          * full add/delete cycles are performed on the same objects, but assume that this is rare
547                          */
548                 if ((adt->action->curves.first == NULL) && (adt->flag & ADT_NLA_EDIT_ON)==0) {
549                         id_us_min(&adt->action->id);
550                         adt->action = NULL;
551                 }
552         }
553                 
554         /* free the F-Curve itself */
555         free_fcurve(fcu);
556 }
557
558 /* ************************************************************************** */
559 /* OPERATORS */
560
561 /* ****************** Operator Utilities ********************************** */
562
563 /* poll callback for being in an Animation Editor channels list region */
564 static int animedit_poll_channels_active (bContext *C)
565 {
566         ScrArea *sa= CTX_wm_area(C);
567         
568         /* channels region test */
569         // TODO: could enhance with actually testing if channels region?
570         if (ELEM(NULL, sa, CTX_wm_region(C)))
571                 return 0;
572         /* animation editor test */
573         if (ELEM3(sa->spacetype, SPACE_ACTION, SPACE_IPO, SPACE_NLA) == 0)
574                 return 0;
575                 
576         return 1;
577 }
578
579 /* poll callback for Animation Editor channels list region + not in NLA-tweakmode for NLA */
580 static int animedit_poll_channels_nla_tweakmode_off (bContext *C)
581 {
582         ScrArea *sa= CTX_wm_area(C);
583         Scene *scene = CTX_data_scene(C);
584         
585         /* channels region test */
586         // TODO: could enhance with actually testing if channels region?
587         if (ELEM(NULL, sa, CTX_wm_region(C)))
588                 return 0;
589         /* animation editor test */
590         if (ELEM3(sa->spacetype, SPACE_ACTION, SPACE_IPO, SPACE_NLA) == 0)
591                 return 0;
592         
593         /* NLA TweakMode test */        
594         if (sa->spacetype == SPACE_NLA) {
595                 if ((scene == NULL) || (scene->flag & SCE_NLA_EDIT_ON))
596                         return 0;
597         }
598                 
599         return 1;
600 }
601
602 /* ****************** Rearrange Channels Operator ******************* */
603
604 /* constants for channel rearranging */
605 /* WARNING: don't change exising ones without modifying rearrange func accordingly */
606 enum {
607         REARRANGE_ANIMCHAN_TOP= -2,
608         REARRANGE_ANIMCHAN_UP= -1,
609         REARRANGE_ANIMCHAN_DOWN= 1,
610         REARRANGE_ANIMCHAN_BOTTOM= 2
611 };
612
613 /* defines for rearranging channels */
614 static EnumPropertyItem prop_animchannel_rearrange_types[] = {
615         {REARRANGE_ANIMCHAN_TOP, "TOP", 0, "To Top", ""},
616         {REARRANGE_ANIMCHAN_UP, "UP", 0, "Up", ""},
617         {REARRANGE_ANIMCHAN_DOWN, "DOWN", 0, "Down", ""},
618         {REARRANGE_ANIMCHAN_BOTTOM, "BOTTOM", 0, "To Bottom", ""},
619         {0, NULL, 0, NULL, NULL}
620 };
621
622 /* Reordering "Islands" Defines ----------------------------------- */
623
624 /* Island definition - just a listbase container */
625 typedef struct tReorderChannelIsland {
626         struct tReorderChannelIsland *next, *prev;
627         
628         ListBase channels;      /* channels within this region with the same state */
629         int flag;                       /* eReorderIslandFlag */
630 } tReorderChannelIsland;
631
632 /* flags for channel reordering islands */
633 typedef enum eReorderIslandFlag {
634         REORDER_ISLAND_SELECTED                 = (1<<0),       /* island is selected */
635         REORDER_ISLAND_UNTOUCHABLE              = (1<<1),       /* island should be ignored */
636         REORDER_ISLAND_MOVED                    = (1<<2)        /* island has already been moved */
637 } eReorderIslandFlag;
638
639
640 /* Rearrange Methods --------------------------------------------- */
641
642 static short rearrange_island_ok (tReorderChannelIsland *island)
643 {
644         /* island must not be untouchable */
645         if (island->flag & REORDER_ISLAND_UNTOUCHABLE)
646                 return 0;
647         
648         /* island should be selected to be moved */
649         return (island->flag & REORDER_ISLAND_SELECTED) && !(island->flag & REORDER_ISLAND_MOVED);
650 }
651
652 /* ............................. */
653
654 static short rearrange_island_top (ListBase *list, tReorderChannelIsland *island)
655 {
656         if (rearrange_island_ok(island)) {
657                 /* remove from current position */
658                 BLI_remlink(list, island);
659                 
660                 /* make it first element */
661                 BLI_insertlinkbefore(list, list->first, island);
662                 
663                 return 1;
664         }
665         
666         return 0;
667 }
668
669 static short rearrange_island_up (ListBase *list, tReorderChannelIsland *island)
670 {
671         if (rearrange_island_ok(island)) {
672                 /* moving up = moving before the previous island, otherwise we're in the same place */
673                 tReorderChannelIsland *prev= island->prev;
674                 
675                 if (prev) {
676                         /* remove from current position */
677                         BLI_remlink(list, island);
678                         
679                         /* push it up */
680                         BLI_insertlinkbefore(list, prev, island);
681                         
682                         return 1;
683                 }
684         }
685         
686         return 0;
687 }
688
689 static short rearrange_island_down (ListBase *list, tReorderChannelIsland *island)
690 {
691         if (rearrange_island_ok(island)) {
692                 /* moving down = moving after the next island, otherwise we're in the same place */
693                 tReorderChannelIsland *next = island->next;
694                 
695                 if (next) {
696                         /* can only move past if next is not untouchable (i.e. nothing can go after it) */
697                         if ((next->flag & REORDER_ISLAND_UNTOUCHABLE)==0) {
698                                 /* remove from current position */
699                                 BLI_remlink(list, island);
700                                 
701                                 /* push it down */
702                                 BLI_insertlinkafter(list, next, island);
703                                 
704                                 return 1;
705                         }
706                 }
707                 /* else: no next channel, so we're at the bottom already, so can't move */
708         }
709         
710         return 0;
711 }
712
713 static short rearrange_island_bottom (ListBase *list, tReorderChannelIsland *island)
714 {
715         if (rearrange_island_ok(island)) {
716                 tReorderChannelIsland *last = list->last;
717                 
718                 /* remove island from current position */
719                 BLI_remlink(list, island);
720                 
721                 /* add before or after the last channel? */
722                 if ((last->flag & REORDER_ISLAND_UNTOUCHABLE)==0) {
723                         /* can add after it */
724                         BLI_addtail(list, island);
725                 }
726                 else {
727                         /* can at most go just before it, since last cannot be moved */
728                         BLI_insertlinkbefore(list, last, island);
729                         
730                 }
731                 
732                 return 1;
733         }
734         
735         return 0;
736 }
737
738 /* ............................. */
739
740 /* typedef for channel rearranging function 
741  * < list: list that channels belong to
742  * < island: island to be moved
743  * > return[0]: whether operation was a success
744  */
745 typedef short (*AnimChanRearrangeFp)(ListBase *list, tReorderChannelIsland *island);
746
747 /* get rearranging function, given 'rearrange' mode */
748 static AnimChanRearrangeFp rearrange_get_mode_func (short mode)
749 {
750         switch (mode) {
751                 case REARRANGE_ANIMCHAN_TOP:
752                         return rearrange_island_top;
753                 case REARRANGE_ANIMCHAN_UP:
754                         return rearrange_island_up;
755                 case REARRANGE_ANIMCHAN_DOWN:
756                         return rearrange_island_down;
757                 case REARRANGE_ANIMCHAN_BOTTOM:
758                         return rearrange_island_bottom;
759                 default:
760                         return NULL;
761         }
762 }
763
764 /* Rearrange Islands Generics ------------------------------------- */
765
766 /* add channel into list of islands */
767 static void rearrange_animchannel_add_to_islands (ListBase *islands, ListBase *srcList, Link *channel, short type)
768 {
769         tReorderChannelIsland *island = islands->last;  /* always try to add to last island if possible */
770         short is_sel=0, is_untouchable=0;
771         
772         /* get flags - selected and untouchable from the channel */
773         switch (type) {
774                 case ANIMTYPE_GROUP:
775                 {
776                         bActionGroup *agrp= (bActionGroup *)channel;
777                         
778                         is_sel= SEL_AGRP(agrp);
779                         is_untouchable= (agrp->flag & AGRP_TEMP) != 0;
780                 }
781                         break;
782                 case ANIMTYPE_FCURVE:
783                 {
784                         FCurve *fcu= (FCurve *)channel;
785                         
786                         is_sel= SEL_FCU(fcu);
787                 }       
788                         break;
789                 case ANIMTYPE_NLATRACK:
790                 {
791                         NlaTrack *nlt= (NlaTrack *)channel;
792                         
793                         is_sel= SEL_NLT(nlt);
794                 }
795                         break;
796                         
797                 default:
798                         printf("rearrange_animchannel_add_to_islands(): don't know how to handle channels of type %d\n", type);
799                         return;
800         }
801         
802         /* do we need to add to a new island? */
803         if ((island == NULL) ||                                 /* 1) no islands yet */
804                 ((island->flag & REORDER_ISLAND_SELECTED) == 0) ||  /* 2) unselected islands have single channels only - to allow up/down movement */
805                 (is_sel == 0))                                      /* 3) if channel is unselected, stop existing island (it was either wrong sel status, or full already) */
806         {
807                 /* create a new island now */
808                 island = MEM_callocN(sizeof(tReorderChannelIsland), "tReorderChannelIsland");
809                 BLI_addtail(islands, island);
810                 
811                 if (is_sel)
812                         island->flag |= REORDER_ISLAND_SELECTED;
813                 if (is_untouchable)
814                         island->flag |= REORDER_ISLAND_UNTOUCHABLE;
815         }
816
817         /* add channel to island - need to remove it from its existing list first though */
818         BLI_remlink(srcList, channel);
819         BLI_addtail(&island->channels, channel);
820 }
821
822 /* flatten islands out into a single list again */
823 static void rearrange_animchannel_flatten_islands (ListBase *islands, ListBase *srcList)
824 {
825         tReorderChannelIsland *island, *isn=NULL;
826         
827         /* make sure srcList is empty now */
828         BLI_assert(srcList->first == NULL);
829         
830         /* go through merging islands */
831         for (island = islands->first; island; island = isn) {
832                 isn = island->next;
833                 
834                 /* merge island channels back to main list, then delete the island */
835                 BLI_movelisttolist(srcList, &island->channels);
836                 BLI_freelinkN(islands, island);
837         }
838 }
839
840 /* ............................. */
841
842 /* performing rearranging of channels using islands */
843 static short rearrange_animchannel_islands (ListBase *list, AnimChanRearrangeFp rearrange_func, short mode, short type)
844 {
845         ListBase islands = {NULL, NULL};
846         Link *channel, *chanNext=NULL;
847         short done = 0;
848         
849         /* don't waste effort on an empty list */
850         if (list->first == NULL)
851                 return 0;
852         
853         /* group channels into islands */
854         for (channel = list->first; channel; channel = chanNext) {
855                 chanNext = channel->next;
856                 rearrange_animchannel_add_to_islands(&islands, list, channel, type);
857         }
858         
859         /* perform moving of selected islands now, but only if there is more than one of 'em so that something will happen 
860          *      - scanning of the list is performed in the opposite direction to the direction we're moving things, so that we 
861          *        shouldn't need to encounter items we've moved already
862          */
863         if (islands.first != islands.last) {
864                 tReorderChannelIsland *first = (mode > 0) ? islands.last : islands.first;
865                 tReorderChannelIsland *island, *isn=NULL;
866                 
867                 for (island = first; island; island = isn) {
868                         isn = (mode > 0) ? island->prev : island->next;
869                         
870                         /* perform rearranging */
871                         if (rearrange_func(&islands, island)) {
872                                 island->flag |= REORDER_ISLAND_MOVED;
873                                 done = 1;
874                         }
875                 }
876         }
877         
878         /* ungroup islands */
879         rearrange_animchannel_flatten_islands(&islands, list);
880         
881         /* did we do anything? */
882         return done;
883 }
884
885 /* NLA Specific Stuff ----------------------------------------------------- */
886
887 /* Change the order NLA Tracks within NLA Stack
888  * ! NLA tracks are displayed in opposite order, so directions need care
889  *      mode: REARRANGE_ANIMCHAN_*  
890  */
891 static void rearrange_nla_channels (bAnimContext *UNUSED(ac), AnimData *adt, short mode)
892 {
893         AnimChanRearrangeFp rearrange_func;
894         
895         /* hack: invert mode so that functions will work in right order */
896         mode *= -1;
897         
898         /* get rearranging function */
899         rearrange_func = rearrange_get_mode_func(mode);
900         if (rearrange_func == NULL)
901                 return;
902         
903         /* only consider NLA data if it's accessible */ 
904         //if (EXPANDED_DRVD(adt) == 0)
905         //      return;
906         
907         /* perform rearranging on tracks list */
908         rearrange_animchannel_islands(&adt->nla_tracks, rearrange_func, mode, ANIMTYPE_NLATRACK);
909 }
910
911 /* Drivers Specific Stuff ------------------------------------------------- */
912
913 /* Change the order drivers within AnimData block
914  *      mode: REARRANGE_ANIMCHAN_*  
915  */
916 static void rearrange_driver_channels (bAnimContext *UNUSED(ac), AnimData *adt, short mode)
917 {
918         /* get rearranging function */
919         AnimChanRearrangeFp rearrange_func = rearrange_get_mode_func(mode);
920         
921         if (rearrange_func == NULL)
922                 return;
923         
924         /* only consider drivers if they're accessible */       
925         if (EXPANDED_DRVD(adt) == 0)
926                 return;
927         
928         /* perform rearranging on drivers list (drivers are really just F-Curves) */
929         rearrange_animchannel_islands(&adt->drivers, rearrange_func, mode, ANIMTYPE_FCURVE);
930 }
931
932 /* Action Specific Stuff ------------------------------------------------- */
933
934 /* make sure all action-channels belong to a group (and clear action's list) */
935 static void split_groups_action_temp (bAction *act, bActionGroup *tgrp)
936 {
937         bActionGroup *agrp;
938         FCurve *fcu;
939         
940         if (act == NULL)
941                 return;
942         
943         /* Separate F-Curves into lists per group */
944         for (agrp= act->groups.first; agrp; agrp= agrp->next) {
945                 if (agrp->channels.first) {
946                         fcu= agrp->channels.last;
947                         act->curves.first= fcu->next;
948                         
949                         fcu= agrp->channels.first;
950                         fcu->prev= NULL;
951                         
952                         fcu= agrp->channels.last;
953                         fcu->next= NULL;
954                 }
955         }
956         
957         /* Initialize memory for temp-group */
958         memset(tgrp, 0, sizeof(bActionGroup));
959         tgrp->flag |= (AGRP_EXPANDED|AGRP_TEMP);
960         BLI_strncpy(tgrp->name, "#TempGroup", sizeof(tgrp->name));
961         
962         /* Move any action-channels not already moved, to the temp group */
963         if (act->curves.first) {
964                 /* start of list */
965                 fcu= act->curves.first;
966                 fcu->prev= NULL;
967                 tgrp->channels.first= fcu;
968                 act->curves.first= NULL;
969                 
970                 /* end of list */
971                 fcu= act->curves.last;
972                 fcu->next= NULL;
973                 tgrp->channels.last= fcu;
974                 act->curves.last= NULL;
975         }
976         
977         /* Add temp-group to list */
978         BLI_addtail(&act->groups, tgrp);
979 }
980
981 /* link lists of channels that groups have */
982 static void join_groups_action_temp (bAction *act)
983 {
984         bActionGroup *agrp;
985         
986         for (agrp= act->groups.first; agrp; agrp= agrp->next) {
987                 ListBase tempGroup;
988                 
989                 /* add list of channels to action's channels */
990                 tempGroup= agrp->channels;
991                 BLI_movelisttolist(&act->curves, &agrp->channels);
992                 agrp->channels= tempGroup;
993                 
994                 /* clear moved flag */
995                 agrp->flag &= ~AGRP_MOVED;
996                 
997                 /* if temp-group... remove from list (but don't free as it's on the stack!) */
998                 if (agrp->flag & AGRP_TEMP) {
999                         BLI_remlink(&act->groups, agrp);
1000                         break;
1001                 }
1002         }
1003 }
1004
1005 /* Change the order of anim-channels within action 
1006  *      mode: REARRANGE_ANIMCHAN_*  
1007  */
1008 static void rearrange_action_channels (bAnimContext *ac, bAction *act, short mode)
1009 {
1010         bActionGroup tgrp;
1011         short do_channels;
1012         
1013         /* get rearranging function */
1014         AnimChanRearrangeFp rearrange_func = rearrange_get_mode_func(mode);
1015         
1016         if (rearrange_func == NULL)
1017                 return;
1018         
1019         /* make sure we're only operating with groups (vs a mixture of groups+curves) */
1020         split_groups_action_temp(act, &tgrp);
1021         
1022         /* rearrange groups first 
1023          *      - the group's channels will only get considered if nothing happened when rearranging the groups
1024          *        i.e. the rearrange function returned 0
1025          */
1026         do_channels = rearrange_animchannel_islands(&act->groups, rearrange_func, mode, ANIMTYPE_GROUP) == 0;
1027         
1028         if (do_channels) {
1029                 bActionGroup *agrp;
1030                 
1031                 for (agrp= act->groups.first; agrp; agrp= agrp->next) {
1032                         /* only consider F-Curves if they're visible (group expanded) */
1033                         if (EXPANDED_AGRP(ac, agrp)) {
1034                                 rearrange_animchannel_islands(&agrp->channels, rearrange_func, mode, ANIMTYPE_FCURVE);
1035                         }
1036                 }
1037         }
1038         
1039         /* assemble lists into one list (and clear moved tags) */
1040         join_groups_action_temp(act);
1041 }
1042
1043 /* ------------------- */
1044
1045 static int animchannels_rearrange_exec(bContext *C, wmOperator *op)
1046 {
1047         bAnimContext ac;
1048         short mode;
1049         
1050         /* get editor data */
1051         if (ANIM_animdata_get_context(C, &ac) == 0)
1052                 return OPERATOR_CANCELLED;
1053                 
1054         /* get mode */
1055         mode= RNA_enum_get(op->ptr, "direction");
1056         
1057         /* method to move channels depends on the editor */
1058         if (ac.datatype == ANIMCONT_GPENCIL) {
1059                 /* Grease Pencil channels */
1060                 printf("Grease Pencil not supported for moving yet\n");
1061         }
1062         else if (ac.datatype == ANIMCONT_ACTION) {
1063                 /* Directly rearrange action's channels */
1064                 rearrange_action_channels(&ac, ac.data, mode);
1065         }
1066         else {
1067                 ListBase anim_data = {NULL, NULL};
1068                 bAnimListElem *ale;
1069                 int filter;
1070                 
1071                 /* get animdata blocks */
1072                 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_ANIMDATA);
1073                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1074                 
1075                 for (ale = anim_data.first; ale; ale = ale->next) {
1076                         AnimData *adt= ale->data;
1077                         
1078                         switch (ac.datatype) {
1079                                 case ANIMCONT_NLA: /* NLA-tracks only */
1080                                         rearrange_nla_channels(&ac, adt, mode);
1081                                         break;
1082                                 
1083                                 case ANIMCONT_DRIVERS: /* Drivers list only */
1084                                         rearrange_driver_channels(&ac, adt, mode);
1085                                         break;
1086                                 
1087                                 case ANIMCONT_SHAPEKEY: // DOUBLE CHECK ME...
1088                                         
1089                                 default: /* some collection of actions */
1090                                         if (adt->action)
1091                                                 rearrange_action_channels(&ac, adt->action, mode);
1092                                         else if (G.debug & G_DEBUG)
1093                                                 printf("Animdata has no action\n");
1094                                         break;
1095                         }
1096                 }
1097                 
1098                 /* free temp data */
1099                 BLI_freelistN(&anim_data);
1100         }
1101         
1102         /* send notifier that things have changed */
1103         WM_event_add_notifier(C, NC_ANIMATION|ND_ANIMCHAN|NA_EDITED, NULL);
1104         
1105         return OPERATOR_FINISHED;
1106 }
1107
1108 static void ANIM_OT_channels_move (wmOperatorType *ot)
1109 {
1110         /* identifiers */
1111         ot->name = "Move Channels";
1112         ot->idname = "ANIM_OT_channels_move";
1113         ot->description = "Rearrange selected animation channels";
1114         
1115         /* api callbacks */
1116         ot->exec = animchannels_rearrange_exec;
1117         ot->poll = animedit_poll_channels_nla_tweakmode_off;
1118         
1119         /* flags */
1120         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1121         
1122         /* props */
1123         ot->prop = RNA_def_enum(ot->srna, "direction", prop_animchannel_rearrange_types, REARRANGE_ANIMCHAN_DOWN, "Direction", "");
1124 }
1125
1126 /* ******************** Delete Channel Operator *********************** */
1127
1128 static int animchannels_delete_exec(bContext *C, wmOperator *UNUSED(op))
1129 {
1130         bAnimContext ac;
1131         ListBase anim_data = {NULL, NULL};
1132         bAnimListElem *ale;
1133         int filter;
1134         
1135         /* get editor data */
1136         if (ANIM_animdata_get_context(C, &ac) == 0)
1137                 return OPERATOR_CANCELLED;
1138         
1139         /* cannot delete in shapekey */
1140         if (ac.datatype == ANIMCONT_SHAPEKEY) 
1141                 return OPERATOR_CANCELLED;
1142                 
1143                 
1144         /* do groups only first (unless in Drivers mode, where there are none) */
1145         if (ac.datatype != ANIMCONT_DRIVERS) {
1146                 /* filter data */
1147                 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL | ANIMFILTER_LIST_CHANNELS | ANIMFILTER_FOREDIT | ANIMFILTER_NODUPLIS);
1148                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1149                 
1150                 /* delete selected groups and their associated channels */
1151                 for (ale= anim_data.first; ale; ale= ale->next) {
1152                         /* only groups - don't check other types yet, since they may no-longer exist */
1153                         if (ale->type == ANIMTYPE_GROUP) {
1154                                 bActionGroup *agrp= (bActionGroup *)ale->data;
1155                                 AnimData *adt= ale->adt;
1156                                 FCurve *fcu, *fcn;
1157                                 
1158                                 /* skip this group if no AnimData available, as we can't safely remove the F-Curves */
1159                                 if (adt == NULL)
1160                                         continue;
1161                                 
1162                                 /* delete all of the Group's F-Curves, but no others */
1163                                 for (fcu= agrp->channels.first; fcu && fcu->grp==agrp; fcu= fcn) {
1164                                         fcn= fcu->next;
1165                                         
1166                                         /* remove from group and action, then free */
1167                                         action_groups_remove_channel(adt->action, fcu);
1168                                         free_fcurve(fcu);
1169                                 }
1170                                 
1171                                 /* free the group itself */
1172                                 if (adt->action)
1173                                         BLI_freelinkN(&adt->action->groups, agrp);
1174                                 else
1175                                         MEM_freeN(agrp);
1176                         }
1177                 }
1178                 
1179                 /* cleanup */
1180                 BLI_freelistN(&anim_data);
1181         }
1182         
1183         /* filter data */
1184         filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL | ANIMFILTER_FOREDIT | ANIMFILTER_NODUPLIS);
1185         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1186         
1187         /* delete selected data channels */
1188         for (ale= anim_data.first; ale; ale= ale->next) {
1189                 switch (ale->type) {
1190                         case ANIMTYPE_FCURVE: 
1191                         {
1192                                 /* F-Curves if we can identify its parent */
1193                                 AnimData *adt= ale->adt;
1194                                 FCurve *fcu= (FCurve *)ale->data;
1195                                 
1196                                 /* try to free F-Curve */
1197                                 ANIM_fcurve_delete_from_animdata(&ac, adt, fcu);
1198                         }
1199                                 break;
1200                                 
1201                         case ANIMTYPE_GPLAYER:
1202                         {
1203                                 /* Grease Pencil layer */
1204                                 bGPdata *gpd= (bGPdata *)ale->id;
1205                                 bGPDlayer *gpl= (bGPDlayer *)ale->data;
1206                                 
1207                                 /* try to delete the layer's data and the layer itself */
1208                                 free_gpencil_frames(gpl);
1209                                 BLI_freelinkN(&gpd->layers, gpl);
1210                         }
1211                                 break;
1212                 }
1213         }
1214         
1215         /* cleanup */
1216         BLI_freelistN(&anim_data);
1217         
1218         /* send notifier that things have changed */
1219         WM_event_add_notifier(C, NC_ANIMATION|ND_ANIMCHAN|NA_EDITED, NULL);
1220         
1221         return OPERATOR_FINISHED;
1222 }
1223  
1224 static void ANIM_OT_channels_delete (wmOperatorType *ot)
1225 {
1226         /* identifiers */
1227         ot->name = "Delete Channels";
1228         ot->idname = "ANIM_OT_channels_delete";
1229         ot->description = "Delete all selected animation channels";
1230         
1231         /* api callbacks */
1232         ot->exec = animchannels_delete_exec;
1233         ot->poll = animedit_poll_channels_active;
1234         
1235         /* flags */
1236         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1237 }
1238
1239 /* ******************** Set Channel Visibility Operator *********************** */
1240 /* NOTE: this operator is only valid in the Graph Editor channels region */
1241
1242 static int animchannels_visibility_set_exec(bContext *C, wmOperator *UNUSED(op))
1243 {
1244         bAnimContext ac;
1245         ListBase anim_data = {NULL, NULL};
1246         ListBase all_data = {NULL, NULL};
1247         bAnimListElem *ale;
1248         int filter;
1249         
1250         /* get editor data */
1251         if (ANIM_animdata_get_context(C, &ac) == 0)
1252                 return OPERATOR_CANCELLED;
1253         
1254         /* get list of all channels that selection may need to be flushed to 
1255          * - hierarchy mustn't affect what we have access to here...
1256          */
1257         filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_CHANNELS | ANIMFILTER_NODUPLIS);
1258         ANIM_animdata_filter(&ac, &all_data, filter, ac.data, ac.datatype);
1259                 
1260         /* hide all channels not selected
1261          * - hierarchy matters if we're doing this from the channels region
1262          *   since we only want to apply this to channels we can "see", 
1263          *   and have these affect their relatives
1264          * - but for Graph Editor, this gets used also from main region
1265          *   where hierarchy doesn't apply, as for [#21276]
1266          */
1267         if ((ac.spacetype == SPACE_IPO) && (ac.regiontype != RGN_TYPE_CHANNELS)) {
1268                 /* graph editor (case 2) */
1269                 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_UNSEL | ANIMFILTER_CURVE_VISIBLE | ANIMFILTER_NODUPLIS);
1270         }
1271         else {
1272                 /* standard case */
1273                 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_UNSEL | ANIMFILTER_NODUPLIS);
1274         }
1275         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1276         
1277         for (ale= anim_data.first; ale; ale= ale->next) {
1278                 /* clear setting first */
1279                 ANIM_channel_setting_set(&ac, ale, ACHANNEL_SETTING_VISIBLE, ACHANNEL_SETFLAG_CLEAR);
1280                 
1281                 /* now also flush selection status as appropriate 
1282                  * NOTE: in some cases, this may result in repeat flushing being performed
1283                  */
1284                 ANIM_flush_setting_anim_channels(&ac, &all_data, ale, ACHANNEL_SETTING_VISIBLE, 0);
1285         }
1286         
1287         BLI_freelistN(&anim_data);
1288         
1289         /* make all the selected channels visible */
1290         filter= (ANIMFILTER_SEL | ANIMFILTER_NODUPLIS);
1291         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1292         
1293         for (ale= anim_data.first; ale; ale= ale->next) {
1294                 /* hack: skip object channels for now, since flushing those will always flush everything, but they are always included */
1295                 // TODO: find out why this is the case, and fix that
1296                 if (ale->type == ANIMTYPE_OBJECT)
1297                         continue;
1298                 
1299                 /* enable the setting */
1300                 ANIM_channel_setting_set(&ac, ale, ACHANNEL_SETTING_VISIBLE, ACHANNEL_SETFLAG_ADD);
1301                 
1302                 /* now, also flush selection status up/down as appropriate */
1303                 ANIM_flush_setting_anim_channels(&ac, &all_data, ale, ACHANNEL_SETTING_VISIBLE, 1);
1304         }
1305         
1306         BLI_freelistN(&anim_data);
1307         BLI_freelistN(&all_data);
1308         
1309         
1310         /* send notifier that things have changed */
1311         WM_event_add_notifier(C, NC_ANIMATION|ND_ANIMCHAN|NA_EDITED, NULL);
1312         
1313         return OPERATOR_FINISHED;
1314 }
1315
1316 static void ANIM_OT_channels_visibility_set (wmOperatorType *ot)
1317 {
1318         /* identifiers */
1319         ot->name = "Set Visibility";
1320         ot->idname = "ANIM_OT_channels_visibility_set";
1321         ot->description = "Make only the selected animation channels visible in the Graph Editor";
1322         
1323         /* api callbacks */
1324         ot->exec = animchannels_visibility_set_exec;
1325         ot->poll = ED_operator_graphedit_active;
1326         
1327         /* flags */
1328         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1329 }
1330
1331
1332 /* ******************** Toggle Channel Visibility Operator *********************** */
1333 /* NOTE: this operator is only valid in the Graph Editor channels region */
1334
1335 static int animchannels_visibility_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1336 {
1337         bAnimContext ac;
1338         ListBase anim_data = {NULL, NULL};
1339         ListBase all_data = {NULL, NULL};
1340         bAnimListElem *ale;
1341         int filter;
1342         short vis= ACHANNEL_SETFLAG_ADD;
1343         
1344         /* get editor data */
1345         if (ANIM_animdata_get_context(C, &ac) == 0)
1346                 return OPERATOR_CANCELLED;
1347                 
1348         /* get list of all channels that selection may need to be flushed to 
1349          * - hierarchy mustn't affect what we have access to here...
1350          */
1351         filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_CHANNELS | ANIMFILTER_NODUPLIS);
1352         ANIM_animdata_filter(&ac, &all_data, filter, ac.data, ac.datatype);
1353                 
1354         /* filter data
1355          * - restrict this to only applying on settings we can get to in the list
1356          */
1357         filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL | ANIMFILTER_NODUPLIS);
1358         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1359         
1360         /* See if we should be making showing all selected or hiding */
1361         for (ale= anim_data.first; ale; ale= ale->next) {
1362                 /* set the setting in the appropriate way (if available) */
1363                 if (ANIM_channel_setting_get(&ac, ale, ACHANNEL_SETTING_VISIBLE)) {
1364                         vis= ACHANNEL_SETFLAG_CLEAR;
1365                         break;
1366                 }
1367         }
1368
1369         /* Now set the flags */
1370         for (ale= anim_data.first; ale; ale= ale->next) {
1371                 /* hack: skip object channels for now, since flushing those will always flush everything, but they are always included */
1372                 // TODO: find out why this is the case, and fix that
1373                 if (ale->type == ANIMTYPE_OBJECT)
1374                         continue;
1375                 
1376                 /* change the setting */
1377                 ANIM_channel_setting_set(&ac, ale, ACHANNEL_SETTING_VISIBLE, vis);
1378                 
1379                 /* now, also flush selection status up/down as appropriate */
1380                 ANIM_flush_setting_anim_channels(&ac, &all_data, ale, ACHANNEL_SETTING_VISIBLE, (vis == ACHANNEL_SETFLAG_ADD));
1381         }
1382         
1383         /* cleanup */
1384         BLI_freelistN(&anim_data);
1385         BLI_freelistN(&all_data);
1386         
1387         /* send notifier that things have changed */
1388         WM_event_add_notifier(C, NC_ANIMATION|ND_ANIMCHAN|NA_EDITED, NULL);
1389         
1390         return OPERATOR_FINISHED;
1391 }
1392
1393 static void ANIM_OT_channels_visibility_toggle (wmOperatorType *ot)
1394 {
1395         /* identifiers */
1396         ot->name = "Toggle Visibility";
1397         ot->idname = "ANIM_OT_channels_visibility_toggle";
1398         ot->description = "Toggle visibility in Graph Editor of all selected animation channels";
1399         
1400         /* api callbacks */
1401         ot->exec = animchannels_visibility_toggle_exec;
1402         ot->poll = ED_operator_graphedit_active;
1403         
1404         /* flags */
1405         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1406 }
1407
1408 /* ********************** Set Flags Operator *********************** */
1409
1410 /* defines for setting animation-channel flags */
1411 static EnumPropertyItem prop_animchannel_setflag_types[] = {
1412         {ACHANNEL_SETFLAG_TOGGLE, "TOGGLE", 0, "Toggle", ""},
1413         {ACHANNEL_SETFLAG_CLEAR, "DISABLE", 0, "Disable", ""},
1414         {ACHANNEL_SETFLAG_ADD, "ENABLE", 0, "Enable", ""},
1415         {ACHANNEL_SETFLAG_INVERT, "INVERT", 0, "Invert", ""},
1416         {0, NULL, 0, NULL, NULL}
1417 };
1418
1419 /* defines for set animation-channel settings */
1420 // TODO: could add some more types, but those are really quite dependent on the mode...
1421 static EnumPropertyItem prop_animchannel_settings_types[] = {
1422         {ACHANNEL_SETTING_PROTECT, "PROTECT", 0, "Protect", ""},
1423         {ACHANNEL_SETTING_MUTE, "MUTE", 0, "Mute", ""},
1424         {0, NULL, 0, NULL, NULL}
1425 };
1426
1427
1428 /* ------------------- */
1429
1430 /* Set/clear a particular flag (setting) for all selected + visible channels 
1431  *      setting: the setting to modify
1432  *      mode: eAnimChannels_SetFlag
1433  *      onlysel: only selected channels get the flag set
1434  */
1435 // TODO: enable a setting which turns flushing on/off?
1436 static void setflag_anim_channels (bAnimContext *ac, short setting, short mode, short onlysel, short flush)
1437 {
1438         ListBase anim_data = {NULL, NULL};
1439         ListBase all_data = {NULL, NULL};
1440         bAnimListElem *ale;
1441         int filter;
1442         
1443         /* filter data that we need if flush is on */
1444         if (flush) {
1445                 /* get list of all channels that selection may need to be flushed to 
1446                  * - hierarchy visibility needs to be ignored so that settings can get flushed
1447                  *   "down" inside closed containers
1448                  */
1449                 filter= ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_CHANNELS;
1450                 ANIM_animdata_filter(ac, &all_data, filter, ac->data, ac->datatype);
1451         }
1452         
1453         /* filter data that we're working on 
1454          * - hierarchy matters if we're doing this from the channels region
1455          *   since we only want to apply this to channels we can "see", 
1456          *   and have these affect their relatives
1457          * - but for Graph Editor, this gets used also from main region
1458          *   where hierarchy doesn't apply [#21276]
1459          */
1460         if ((ac->spacetype == SPACE_IPO) && (ac->regiontype != RGN_TYPE_CHANNELS)) {
1461                 /* graph editor (case 2) */
1462                 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_CHANNELS | ANIMFILTER_CURVE_VISIBLE | ANIMFILTER_NODUPLIS);
1463         }
1464         else {
1465                 /* standard case */
1466                 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS | ANIMFILTER_NODUPLIS);
1467         }
1468         if (onlysel) filter |= ANIMFILTER_SEL;
1469         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
1470         
1471         /* if toggling, check if disable or enable */
1472         if (mode == ACHANNEL_SETFLAG_TOGGLE) {
1473                 /* default to turn all on, unless we encounter one that's on... */
1474                 mode= ACHANNEL_SETFLAG_ADD;
1475                 
1476                 /* see if we should turn off instead... */
1477                 for (ale= anim_data.first; ale; ale= ale->next) {
1478                         /* set the setting in the appropriate way (if available) */
1479                         if (ANIM_channel_setting_get(ac, ale, setting) > 0) {
1480                                 mode= ACHANNEL_SETFLAG_CLEAR;
1481                                 break;
1482                         }
1483                 }
1484         }
1485         
1486         /* apply the setting */
1487         for (ale= anim_data.first; ale; ale= ale->next) {
1488                 /* skip channel if setting is not available */
1489                 if (ANIM_channel_setting_get(ac, ale, setting) == -1)
1490                         continue;
1491                 
1492                 /* set the setting in the appropriate way */
1493                 ANIM_channel_setting_set(ac, ale, setting, mode);
1494                 
1495                 /* if flush status... */
1496                 if (flush)
1497                         ANIM_flush_setting_anim_channels(ac, &all_data, ale, setting, mode);
1498         }
1499         
1500         BLI_freelistN(&anim_data);
1501         BLI_freelistN(&all_data);
1502 }
1503
1504 /* ------------------- */
1505
1506 static int animchannels_setflag_exec(bContext *C, wmOperator *op)
1507 {
1508         bAnimContext ac;
1509         short mode, setting;
1510         short flush=1;
1511         
1512         /* get editor data */
1513         if (ANIM_animdata_get_context(C, &ac) == 0)
1514                 return OPERATOR_CANCELLED;
1515                 
1516         /* mode (eAnimChannels_SetFlag), setting (eAnimChannel_Settings) */
1517         mode= RNA_enum_get(op->ptr, "mode");
1518         setting= RNA_enum_get(op->ptr, "type");
1519         
1520         /* check if setting is flushable */
1521         if (setting == ACHANNEL_SETTING_EXPAND)
1522                 flush= 0;
1523         
1524         /* modify setting 
1525          *      - only selected channels are affected
1526          */
1527         setflag_anim_channels(&ac, setting, mode, 1, flush);
1528         
1529         /* send notifier that things have changed */
1530         WM_event_add_notifier(C, NC_ANIMATION|ND_ANIMCHAN|NA_EDITED, NULL);
1531         
1532         return OPERATOR_FINISHED;
1533 }
1534
1535 /* duplicate of 'ANIM_OT_channels_setting_toggle' for menu title only, weak! */
1536 static void ANIM_OT_channels_setting_enable (wmOperatorType *ot)
1537 {
1538         /* identifiers */
1539         ot->name = "Enable Channel Setting";
1540         ot->idname = "ANIM_OT_channels_setting_enable";
1541         ot->description = "Enable specified setting on all selected animation channels";
1542         
1543         /* api callbacks */
1544         ot->invoke = WM_menu_invoke;
1545         ot->exec = animchannels_setflag_exec;
1546         ot->poll = animedit_poll_channels_active;
1547         
1548         /* flags */
1549         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1550         
1551         /* props */
1552                 /* flag-setting mode */
1553         RNA_def_enum(ot->srna, "mode", prop_animchannel_setflag_types, ACHANNEL_SETFLAG_ADD, "Mode", "");
1554                 /* setting to set */
1555         ot->prop = RNA_def_enum(ot->srna, "type", prop_animchannel_settings_types, 0, "Type", "");
1556 }
1557 /* duplicate of 'ANIM_OT_channels_setting_toggle' for menu title only, weak! */
1558 static void ANIM_OT_channels_setting_disable (wmOperatorType *ot)
1559 {
1560         /* identifiers */
1561         ot->name = "Disable Channel Setting";
1562         ot->idname = "ANIM_OT_channels_setting_disable";
1563         ot->description = "Disable specified setting on all selected animation channels";
1564         
1565         /* api callbacks */
1566         ot->invoke = WM_menu_invoke;
1567         ot->exec = animchannels_setflag_exec;
1568         ot->poll = animedit_poll_channels_active;
1569         
1570         /* flags */
1571         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1572         
1573         /* props */
1574                 /* flag-setting mode */
1575         RNA_def_enum(ot->srna, "mode", prop_animchannel_setflag_types, ACHANNEL_SETFLAG_CLEAR, "Mode", "");
1576                 /* setting to set */
1577         ot->prop = RNA_def_enum(ot->srna, "type", prop_animchannel_settings_types, 0, "Type", "");
1578 }
1579
1580 static void ANIM_OT_channels_setting_toggle (wmOperatorType *ot)
1581 {
1582         /* identifiers */
1583         ot->name = "Toggle Channel Setting";
1584         ot->idname = "ANIM_OT_channels_setting_toggle";
1585         ot->description = "Toggle specified setting on all selected animation channels";
1586         
1587         /* api callbacks */
1588         ot->invoke = WM_menu_invoke;
1589         ot->exec = animchannels_setflag_exec;
1590         ot->poll = animedit_poll_channels_active;
1591         
1592         /* flags */
1593         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1594         
1595         /* props */
1596                 /* flag-setting mode */
1597         RNA_def_enum(ot->srna, "mode", prop_animchannel_setflag_types, ACHANNEL_SETFLAG_TOGGLE, "Mode", "");
1598                 /* setting to set */
1599         ot->prop = RNA_def_enum(ot->srna, "type", prop_animchannel_settings_types, 0, "Type", "");
1600 }
1601
1602 static void ANIM_OT_channels_editable_toggle (wmOperatorType *ot)
1603 {
1604         /* identifiers */
1605         ot->name = "Toggle Channel Editability";
1606         ot->idname = "ANIM_OT_channels_editable_toggle";
1607         ot->description = "Toggle editability of selected channels";
1608         
1609         /* api callbacks */
1610         ot->exec = animchannels_setflag_exec;
1611         ot->poll = animedit_poll_channels_active;
1612         
1613         /* flags */
1614         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1615         
1616         /* props */
1617                 /* flag-setting mode */
1618         RNA_def_enum(ot->srna, "mode", prop_animchannel_setflag_types, ACHANNEL_SETFLAG_TOGGLE, "Mode", "");
1619                 /* setting to set */
1620         RNA_def_enum(ot->srna, "type", prop_animchannel_settings_types, ACHANNEL_SETTING_PROTECT, "Type", "");
1621 }
1622
1623 /* ********************** Expand Channels Operator *********************** */
1624
1625 static int animchannels_expand_exec (bContext *C, wmOperator *op)
1626 {
1627         bAnimContext ac;
1628         short onlysel= 1;
1629         
1630         /* get editor data */
1631         if (ANIM_animdata_get_context(C, &ac) == 0)
1632                 return OPERATOR_CANCELLED;
1633                 
1634         /* only affect selected channels? */
1635         if (RNA_boolean_get(op->ptr, "all"))
1636                 onlysel= 0;
1637         
1638         /* modify setting */
1639         setflag_anim_channels(&ac, ACHANNEL_SETTING_EXPAND, ACHANNEL_SETFLAG_ADD, onlysel, 0);
1640         
1641         /* send notifier that things have changed */
1642         WM_event_add_notifier(C, NC_ANIMATION|ND_ANIMCHAN|NA_EDITED, NULL);
1643         
1644         return OPERATOR_FINISHED;
1645 }
1646
1647 static void ANIM_OT_channels_expand (wmOperatorType *ot)
1648 {
1649         /* identifiers */
1650         ot->name = "Expand Channels";
1651         ot->idname = "ANIM_OT_channels_expand";
1652         ot->description = "Expand (i.e. open) all selected expandable animation channels";
1653         
1654         /* api callbacks */
1655         ot->exec = animchannels_expand_exec;
1656         ot->poll = animedit_poll_channels_active;
1657         
1658         /* flags */
1659         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1660         
1661         /* props */
1662         ot->prop = RNA_def_boolean(ot->srna, "all", 1, "All", "Expand all channels (not just selected ones)");
1663 }
1664
1665 /* ********************** Collapse Channels Operator *********************** */
1666
1667 static int animchannels_collapse_exec (bContext *C, wmOperator *op)
1668 {
1669         bAnimContext ac;
1670         short onlysel= 1;
1671         
1672         /* get editor data */
1673         if (ANIM_animdata_get_context(C, &ac) == 0)
1674                 return OPERATOR_CANCELLED;
1675                 
1676         /* only affect selected channels? */
1677         if (RNA_boolean_get(op->ptr, "all"))
1678                 onlysel= 0;
1679         
1680         /* modify setting */
1681         setflag_anim_channels(&ac, ACHANNEL_SETTING_EXPAND, ACHANNEL_SETFLAG_CLEAR, onlysel, 0);
1682         
1683         /* send notifier that things have changed */
1684         WM_event_add_notifier(C, NC_ANIMATION|ND_ANIMCHAN|NA_EDITED, NULL);
1685         
1686         return OPERATOR_FINISHED;
1687 }
1688
1689 static void ANIM_OT_channels_collapse (wmOperatorType *ot)
1690 {
1691         /* identifiers */
1692         ot->name = "Collapse Channels";
1693         ot->idname = "ANIM_OT_channels_collapse";
1694         ot->description = "Collapse (i.e. close) all selected expandable animation channels";
1695         
1696         /* api callbacks */
1697         ot->exec = animchannels_collapse_exec;
1698         ot->poll = animedit_poll_channels_active;
1699         
1700         /* flags */
1701         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1702         
1703         /* props */
1704         ot->prop = RNA_def_boolean(ot->srna, "all", 1, "All", "Collapse all channels (not just selected ones)");
1705 }
1706
1707 /* ******************* Reenable Disabled Operator ******************* */
1708
1709 static int animchannels_enable_poll (bContext *C)
1710 {
1711         ScrArea *sa= CTX_wm_area(C);
1712         
1713         /* channels region test */
1714         // TODO: could enhance with actually testing if channels region?
1715         if (ELEM(NULL, sa, CTX_wm_region(C)))
1716                 return 0;
1717                 
1718         /* animation editor test - Action/Dopesheet/etc. and Graph only */
1719         if (ELEM(sa->spacetype, SPACE_ACTION, SPACE_IPO) == 0)
1720                 return 0;
1721                 
1722         return 1;
1723 }
1724
1725 static int animchannels_enable_exec (bContext *C, wmOperator *UNUSED(op))
1726 {
1727         bAnimContext ac;
1728         
1729         ListBase anim_data = {NULL, NULL};
1730         bAnimListElem *ale;
1731         int filter;
1732         
1733         /* get editor data */
1734         if (ANIM_animdata_get_context(C, &ac) == 0)
1735                 return OPERATOR_CANCELLED;
1736         
1737         /* filter data */
1738         filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_NODUPLIS);
1739         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1740         
1741         /* loop through filtered data and clean curves */
1742         for (ale= anim_data.first; ale; ale= ale->next) {
1743                 FCurve *fcu = (FCurve *)ale->data;
1744                 
1745                 /* remove disabled flags from F-Curves */
1746                 fcu->flag &= ~FCURVE_DISABLED;
1747                 
1748                 /* for drivers, let's do the same too */
1749                 if (fcu->driver)
1750                         fcu->driver->flag &= ~DRIVER_FLAG_INVALID;
1751                         
1752                 /* tag everything for updates - in particular, this is needed to get drivers working again */
1753                 ANIM_list_elem_update(ac.scene, ale);
1754         }
1755         
1756         /* free temp data */
1757         BLI_freelistN(&anim_data);
1758                 
1759         /* send notifier that things have changed */
1760         WM_event_add_notifier(C, NC_ANIMATION|ND_ANIMCHAN|NA_EDITED, NULL);
1761         
1762         return OPERATOR_FINISHED;
1763 }
1764
1765 static void ANIM_OT_channels_fcurves_enable (wmOperatorType *ot)
1766 {
1767         /* identifiers */
1768         ot->name = "Revive Disabled F-Curves";
1769         ot->idname = "ANIM_OT_channels_fcurves_enable";
1770         ot->description = "Clears 'disabled' tag from all F-Curves to get broken F-Curves working again";
1771         
1772         /* api callbacks */
1773         ot->exec = animchannels_enable_exec;
1774         ot->poll = animchannels_enable_poll;
1775         
1776         /* flags */
1777         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1778 }
1779
1780 /* ********************** Select All Operator *********************** */
1781
1782 static int animchannels_deselectall_exec (bContext *C, wmOperator *op)
1783 {
1784         bAnimContext ac;
1785         
1786         /* get editor data */
1787         if (ANIM_animdata_get_context(C, &ac) == 0)
1788                 return OPERATOR_CANCELLED;
1789                 
1790         /* 'standard' behavior - check if selected, then apply relevant selection */
1791         if (RNA_boolean_get(op->ptr, "invert"))
1792                 ANIM_deselect_anim_channels(&ac, ac.data, ac.datatype, 0, ACHANNEL_SETFLAG_TOGGLE);
1793         else
1794                 ANIM_deselect_anim_channels(&ac, ac.data, ac.datatype, 1, ACHANNEL_SETFLAG_ADD);
1795         
1796         /* send notifier that things have changed */
1797         WM_event_add_notifier(C, NC_ANIMATION|ND_ANIMCHAN|NA_SELECTED, NULL);
1798         
1799         return OPERATOR_FINISHED;
1800 }
1801  
1802 static void ANIM_OT_channels_select_all_toggle (wmOperatorType *ot)
1803 {
1804         /* identifiers */
1805         ot->name = "Select All";
1806         ot->idname = "ANIM_OT_channels_select_all_toggle";
1807         ot->description = "Toggle selection of all animation channels";
1808         
1809         /* api callbacks */
1810         ot->exec = animchannels_deselectall_exec;
1811         ot->poll = animedit_poll_channels_nla_tweakmode_off;
1812         
1813         /* flags */
1814         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1815         
1816         /* props */
1817         ot->prop = RNA_def_boolean(ot->srna, "invert", 0, "Invert", "");
1818 }
1819
1820 /* ******************** Borderselect Operator *********************** */
1821
1822 static void borderselect_anim_channels (bAnimContext *ac, rcti *rect, short selectmode)
1823 {
1824         ListBase anim_data = {NULL, NULL};
1825         bAnimListElem *ale;
1826         int filter;
1827         
1828         SpaceNla *snla = (SpaceNla *)ac->sl;
1829         View2D *v2d= &ac->ar->v2d;
1830         rctf rectf;
1831         float ymin, ymax;
1832         
1833         /* set initial y extents */
1834         if (ac->datatype == ANIMCONT_NLA) {
1835                 ymin = (float)(-NLACHANNEL_HEIGHT(snla));
1836                 ymax = 0.0f;
1837         }
1838         else {
1839                 ymin = 0.0f;
1840                 ymax = (float)(-ACHANNEL_HEIGHT);
1841         }
1842         
1843         /* convert border-region to view coordinates */
1844         UI_view2d_region_to_view(v2d, rect->xmin, rect->ymin+2, &rectf.xmin, &rectf.ymin);
1845         UI_view2d_region_to_view(v2d, rect->xmax, rect->ymax-2, &rectf.xmax, &rectf.ymax);
1846         
1847         /* filter data */
1848         filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
1849         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
1850         
1851         /* loop over data, doing border select */
1852         for (ale= anim_data.first; ale; ale= ale->next) {
1853                 if (ac->datatype == ANIMCONT_NLA)
1854                         ymin= ymax - NLACHANNEL_STEP(snla);
1855                 else
1856                         ymin= ymax - ACHANNEL_STEP;
1857                 
1858                 /* if channel is within border-select region, alter it */
1859                 if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))) {
1860                         /* set selection flags only */
1861                         ANIM_channel_setting_set(ac, ale, ACHANNEL_SETTING_SELECT, selectmode);
1862                         
1863                         /* type specific actions */
1864                         switch (ale->type) {
1865                                 case ANIMTYPE_GROUP:
1866                                 {
1867                                         bActionGroup *agrp= (bActionGroup *)ale->data;
1868                                         
1869                                         /* always clear active flag after doing this */
1870                                         agrp->flag &= ~AGRP_ACTIVE;
1871                                 }
1872                                         break;
1873                                 case ANIMTYPE_NLATRACK:
1874                                 {
1875                                         NlaTrack *nlt= (NlaTrack *)ale->data;
1876                                         
1877                                         /* for now, it's easier just to do this here manually, as defining a new type 
1878                                          * currently adds complications when doing other stuff 
1879                                          */
1880                                         ACHANNEL_SET_FLAG(nlt, selectmode, NLATRACK_SELECTED);
1881                                 }
1882                                         break;
1883                         }
1884                 }
1885                 
1886                 /* set minimum extent to be the maximum of the next channel */
1887                 ymax= ymin;
1888         }
1889         
1890         /* cleanup */
1891         BLI_freelistN(&anim_data);
1892 }
1893
1894 /* ------------------- */
1895
1896 static int animchannels_borderselect_exec(bContext *C, wmOperator *op)
1897 {
1898         bAnimContext ac;
1899         rcti rect;
1900         short selectmode=0;
1901         int gesture_mode, extend;
1902         
1903         /* get editor data */
1904         if (ANIM_animdata_get_context(C, &ac) == 0)
1905                 return OPERATOR_CANCELLED;
1906         
1907         /* get settings from operator */
1908         rect.xmin = RNA_int_get(op->ptr, "xmin");
1909         rect.ymin = RNA_int_get(op->ptr, "ymin");
1910         rect.xmax = RNA_int_get(op->ptr, "xmax");
1911         rect.ymax = RNA_int_get(op->ptr, "ymax");
1912         
1913         gesture_mode= RNA_int_get(op->ptr, "gesture_mode");
1914         extend= RNA_boolean_get(op->ptr, "extend");
1915
1916         if (!extend)
1917                 ANIM_deselect_anim_channels(&ac, ac.data, ac.datatype, 1, ACHANNEL_SETFLAG_CLEAR);
1918
1919         if (gesture_mode == GESTURE_MODAL_SELECT)
1920                 selectmode = ACHANNEL_SETFLAG_ADD;
1921         else
1922                 selectmode = ACHANNEL_SETFLAG_CLEAR;
1923         
1924         /* apply borderselect animation channels */
1925         borderselect_anim_channels(&ac, &rect, selectmode);
1926         
1927         /* send notifier that things have changed */
1928         WM_event_add_notifier(C, NC_ANIMATION|ND_ANIMCHAN|NA_SELECTED, NULL);
1929         
1930         return OPERATOR_FINISHED;
1931
1932
1933 static void ANIM_OT_channels_select_border(wmOperatorType *ot)
1934 {
1935         /* identifiers */
1936         ot->name = "Border Select";
1937         ot->idname = "ANIM_OT_channels_select_border";
1938         ot->description = "Select all animation channels within the specified region";
1939         
1940         /* api callbacks */
1941         ot->invoke = WM_border_select_invoke;
1942         ot->exec = animchannels_borderselect_exec;
1943         ot->modal = WM_border_select_modal;
1944         ot->cancel = WM_border_select_cancel;
1945         
1946         ot->poll = animedit_poll_channels_nla_tweakmode_off;
1947         
1948         /* flags */
1949         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1950         
1951         /* rna */
1952         WM_operator_properties_gesture_border(ot, TRUE);
1953 }
1954
1955 /* ******************* Rename Operator ***************************** */
1956 /* Allow renaming some channels by clicking on them */
1957
1958 static void rename_anim_channels (bAnimContext *ac, int channel_index)
1959 {
1960         ListBase anim_data = {NULL, NULL};
1961         bAnimChannelType *acf;
1962         bAnimListElem *ale;
1963         int filter;
1964         
1965         /* get the channel that was clicked on */
1966                 /* filter channels */
1967         filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
1968         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
1969         
1970                 /* get channel from index */
1971         ale= BLI_findlink(&anim_data, channel_index);
1972         if (ale == NULL) {
1973                 /* channel not found */
1974                 if (G.debug & G_DEBUG)
1975                         printf("Error: animation channel (index = %d) not found in rename_anim_channels()\n", channel_index);
1976                 
1977                 BLI_freelistN(&anim_data);
1978                 return;
1979         }
1980         
1981         /* check that channel can be renamed */
1982         acf = ANIM_channel_get_typeinfo(ale);
1983         if (acf && acf->name_prop) {
1984                 PointerRNA ptr;
1985                 PropertyRNA *prop;
1986                 
1987                 /* ok if we can get name property to edit from this channel */
1988                 if (acf->name_prop(ale, &ptr, &prop)) {
1989                         /* actually showing the rename textfield is done on redraw,
1990                          * so here we just store the index of this channel in the 
1991                          * dopesheet data, which will get utilized when drawing the
1992                          * channel...
1993                          *
1994                          * +1 factor is for backwards compat issues
1995                          */
1996                         if (ac->ads) {
1997                                 ac->ads->renameIndex = channel_index + 1;
1998                         }
1999                 }
2000         }
2001         
2002         /* free temp data and tag for refresh */
2003         BLI_freelistN(&anim_data);
2004         ED_region_tag_redraw(ac->ar);
2005 }
2006
2007 static int animchannels_rename_invoke (bContext *C, wmOperator *UNUSED(op), wmEvent *evt)
2008 {
2009         bAnimContext ac;
2010         ARegion *ar;
2011         View2D *v2d;
2012         int channel_index;
2013         float x, y;
2014         
2015         /* get editor data */
2016         if (ANIM_animdata_get_context(C, &ac) == 0)
2017                 return OPERATOR_CANCELLED;
2018                 
2019         /* get useful pointers from animation context data */
2020         ar= ac.ar;
2021         v2d= &ar->v2d;
2022         
2023         /* figure out which channel user clicked in 
2024          * Note: although channels technically start at y= ACHANNEL_FIRST, we need to adjust by half a channel's height
2025          *              so that the tops of channels get caught ok. Since ACHANNEL_FIRST is really ACHANNEL_HEIGHT, we simply use
2026          *              ACHANNEL_HEIGHT_HALF.
2027          */
2028         UI_view2d_region_to_view(v2d, evt->mval[0], evt->mval[1], &x, &y);
2029         
2030         if (ac.datatype == ANIMCONT_NLA) {
2031                 SpaceNla *snla = (SpaceNla *)ac.sl;
2032                 UI_view2d_listview_view_to_cell(v2d, NLACHANNEL_NAMEWIDTH, NLACHANNEL_STEP(snla), 0, (float)NLACHANNEL_HEIGHT_HALF(snla), x, y, NULL, &channel_index);
2033         }
2034         else {
2035                 UI_view2d_listview_view_to_cell(v2d, ACHANNEL_NAMEWIDTH, ACHANNEL_STEP, 0, (float)ACHANNEL_HEIGHT_HALF, x, y, NULL, &channel_index);
2036         }
2037         
2038         /* handle click */
2039         rename_anim_channels(&ac, channel_index);
2040         
2041         return OPERATOR_FINISHED;
2042 }
2043
2044 static void ANIM_OT_channels_rename (wmOperatorType *ot)
2045 {
2046         /* identifiers */
2047         ot->name = "Rename Channels";
2048         ot->idname = "ANIM_OT_channels_rename";
2049         ot->description = "Rename animation channel under mouse";
2050         
2051         /* api callbacks */
2052         ot->invoke = animchannels_rename_invoke;
2053         ot->poll = animedit_poll_channels_active;
2054 }
2055
2056 /* ******************** Mouse-Click Operator *********************** */
2057 /* Handle selection changes due to clicking on channels. Settings will get caught by UI code... */
2058
2059 static int mouse_anim_channels (bAnimContext *ac, float UNUSED(x), int channel_index, short selectmode)
2060 {
2061         ListBase anim_data = {NULL, NULL};
2062         bAnimListElem *ale;
2063         int filter;
2064         int notifierFlags = 0;
2065         
2066         /* get the channel that was clicked on */
2067                 /* filter channels */
2068         filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
2069         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
2070         
2071                 /* get channel from index */
2072         ale= BLI_findlink(&anim_data, channel_index);
2073         if (ale == NULL) {
2074                 /* channel not found */
2075                 if (G.debug & G_DEBUG)
2076                         printf("Error: animation channel (index = %d) not found in mouse_anim_channels()\n", channel_index);
2077                 
2078                 BLI_freelistN(&anim_data);
2079                 return 0;
2080         }
2081         
2082         /* selectmode -1 is a special case for ActionGroups only, which selects all of the channels underneath it only... */
2083         // TODO: should this feature be extended to work with other channel types too?
2084         if ((selectmode == -1) && (ale->type != ANIMTYPE_GROUP)) {
2085                 /* normal channels should not behave normally in this case */
2086                 BLI_freelistN(&anim_data);
2087                 return 0;
2088         }
2089         
2090         /* action to take depends on what channel we've got */
2091         // WARNING: must keep this in sync with the equivalent function in nla_channels.c
2092         switch (ale->type) {
2093                 case ANIMTYPE_SCENE:
2094                 {
2095                         Scene *sce= (Scene *)ale->data;
2096                         AnimData *adt= sce->adt;
2097                         
2098                         /* set selection status */
2099                         if (selectmode == SELECT_INVERT) {
2100                                 /* swap select */
2101                                 sce->flag ^= SCE_DS_SELECTED;
2102                                 if (adt) adt->flag ^= ADT_UI_SELECTED;
2103                         }
2104                         else {
2105                                 sce->flag |= SCE_DS_SELECTED;
2106                                 if (adt) adt->flag |= ADT_UI_SELECTED;
2107                         }
2108                         
2109                         notifierFlags |= (ND_ANIMCHAN|NA_SELECTED);
2110                 }
2111                         break;
2112                 case ANIMTYPE_OBJECT:
2113                 {
2114                         bDopeSheet *ads= (bDopeSheet *)ac->data;
2115                         Scene *sce= (Scene *)ads->source;
2116                         Base *base= (Base *)ale->data;
2117                         Object *ob= base->object;
2118                         AnimData *adt= ob->adt;
2119                         
2120                         /* set selection status */
2121                         if (selectmode == SELECT_INVERT) {
2122                                 /* swap select */
2123                                 base->flag ^= SELECT;
2124                                 ob->flag= base->flag;
2125                                 
2126                                 if (adt) adt->flag ^= ADT_UI_SELECTED;
2127                         }
2128                         else {
2129                                 Base *b;
2130                                 
2131                                 /* deselect all */
2132                                 // TODO: should this deselect all other types of channels too?
2133                                 for (b= sce->base.first; b; b= b->next) {
2134                                         b->flag &= ~SELECT;
2135                                         b->object->flag= b->flag;
2136                                         if (b->object->adt) b->object->adt->flag &= ~(ADT_UI_SELECTED|ADT_UI_ACTIVE);
2137                                 }
2138                                 
2139                                 /* select object now */
2140                                 base->flag |= SELECT;
2141                                 ob->flag |= SELECT;
2142                                 if (adt) adt->flag |= ADT_UI_SELECTED;
2143                         }
2144                         
2145                         if ((adt) && (adt->flag & ADT_UI_SELECTED))
2146                                 adt->flag |= ADT_UI_ACTIVE;
2147                         
2148                         notifierFlags |= (ND_ANIMCHAN|NA_SELECTED);
2149                 }
2150                         break;
2151                 
2152                 case ANIMTYPE_FILLACTD: /* Action Expander */
2153                 case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
2154                 case ANIMTYPE_DSLAM:
2155                 case ANIMTYPE_DSCAM:
2156                 case ANIMTYPE_DSCUR:
2157                 case ANIMTYPE_DSSKEY:
2158                 case ANIMTYPE_DSWOR:
2159                 case ANIMTYPE_DSPART:
2160                 case ANIMTYPE_DSMBALL:
2161                 case ANIMTYPE_DSARM:
2162                 case ANIMTYPE_DSMESH:
2163                 case ANIMTYPE_DSNTREE:
2164                 case ANIMTYPE_DSTEX:
2165                 case ANIMTYPE_DSLAT:
2166                 case ANIMTYPE_DSLINESTYLE:
2167                 case ANIMTYPE_DSSPK:
2168                 {
2169                         /* sanity checking... */
2170                         if (ale->adt) {
2171                                 /* select/deselect */
2172                                 if (selectmode == SELECT_INVERT) {
2173                                         /* inverse selection status of this AnimData block only */
2174                                         ale->adt->flag ^= ADT_UI_SELECTED;
2175                                 }
2176                                 else {
2177                                         /* select AnimData block by itself */
2178                                         ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
2179                                         ale->adt->flag |= ADT_UI_SELECTED;
2180                                 }
2181                                 
2182                                 /* set active? */
2183                                 if ((ale->adt) && (ale->adt->flag & ADT_UI_SELECTED))
2184                                         ale->adt->flag |= ADT_UI_ACTIVE;
2185                         }
2186                         
2187                         notifierFlags |= (ND_ANIMCHAN|NA_SELECTED);
2188                 }       
2189                         break;
2190                 
2191                 case ANIMTYPE_GROUP: 
2192                 {
2193                         bActionGroup *agrp= (bActionGroup *)ale->data;
2194                         
2195                         /* select/deselect group */
2196                         if (selectmode == SELECT_INVERT) {
2197                                 /* inverse selection status of this group only */
2198                                 agrp->flag ^= AGRP_SELECTED;
2199                         }
2200                         else if (selectmode == -1) {
2201                                 /* select all in group (and deselect everthing else) */ 
2202                                 FCurve *fcu;
2203                                 
2204                                 /* deselect all other channels */
2205                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
2206                                 
2207                                 /* only select channels in group and group itself */
2208                                 for (fcu= agrp->channels.first; fcu && fcu->grp==agrp; fcu= fcu->next)
2209                                         fcu->flag |= FCURVE_SELECTED;
2210                                 agrp->flag |= AGRP_SELECTED;                                    
2211                         }
2212                         else {
2213                                 /* select group by itself */
2214                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
2215                                 agrp->flag |= AGRP_SELECTED;
2216                         }
2217                         
2218                         /* if group is selected now, make group the 'active' one in the visible list */
2219                         if (agrp->flag & AGRP_SELECTED)
2220                                 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, agrp, ANIMTYPE_GROUP);
2221                                 
2222                         notifierFlags |= (ND_ANIMCHAN|NA_SELECTED);
2223                 }
2224                         break;
2225                 case ANIMTYPE_FCURVE: 
2226                 {
2227                         FCurve *fcu= (FCurve *)ale->data;
2228                         
2229                         /* select/deselect */
2230                         if (selectmode == SELECT_INVERT) {
2231                                 /* inverse selection status of this F-Curve only */
2232                                 fcu->flag ^= FCURVE_SELECTED;
2233                         }
2234                         else {
2235                                 /* select F-Curve by itself */
2236                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
2237                                 fcu->flag |= FCURVE_SELECTED;
2238                         }
2239                         
2240                         /* if F-Curve is selected now, make F-Curve the 'active' one in the visible list */
2241                         if (fcu->flag & FCURVE_SELECTED)
2242                                 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, fcu, ANIMTYPE_FCURVE);
2243                                 
2244                         notifierFlags |= (ND_ANIMCHAN|NA_SELECTED);
2245                 }
2246                         break;
2247                 case ANIMTYPE_SHAPEKEY: 
2248                 {
2249                         KeyBlock *kb= (KeyBlock *)ale->data;
2250                         
2251                         /* select/deselect */
2252                         if (selectmode == SELECT_INVERT) {
2253                                 /* inverse selection status of this ShapeKey only */
2254                                 kb->flag ^= KEYBLOCK_SEL;
2255                         }
2256                         else {
2257                                 /* select ShapeKey by itself */
2258                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
2259                                 kb->flag |= KEYBLOCK_SEL;
2260                         }
2261                                 
2262                         notifierFlags |= (ND_ANIMCHAN|NA_SELECTED);
2263                 }
2264                         break;
2265                 case ANIMTYPE_GPDATABLOCK:
2266                 {
2267                         bGPdata *gpd= (bGPdata *)ale->data;
2268                         
2269                         /* toggle expand 
2270                          *      - although the triangle widget already allows this, the whole channel can also be used for this purpose
2271                          */
2272                         gpd->flag ^= GP_DATA_EXPAND;
2273                         
2274                         notifierFlags |= (ND_ANIMCHAN|NA_EDITED);
2275                 }
2276                         break;
2277                 case ANIMTYPE_GPLAYER:
2278                 {
2279                         bGPDlayer *gpl= (bGPDlayer *)ale->data;
2280                         
2281                         /* select/deselect */
2282                         if (selectmode == SELECT_INVERT) {
2283                                 /* invert selection status of this layer only */
2284                                 gpl->flag ^= GP_LAYER_SELECT;
2285                         }
2286                         else {  
2287                                 /* select layer by itself */
2288                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
2289                                 gpl->flag |= GP_LAYER_SELECT;
2290                         }
2291                         
2292                         notifierFlags |= (ND_ANIMCHAN|NA_EDITED);
2293                 }
2294                         break;
2295                 default:
2296                         if (G.debug & G_DEBUG)
2297                                 printf("Error: Invalid channel type in mouse_anim_channels()\n");
2298         }
2299         
2300         /* free channels */
2301         BLI_freelistN(&anim_data);
2302         
2303         /* return notifier flags */
2304         return notifierFlags;
2305 }
2306
2307 /* ------------------- */
2308
2309 /* handle clicking */
2310 static int animchannels_mouseclick_invoke(bContext *C, wmOperator *op, wmEvent *event)
2311 {
2312         bAnimContext ac;
2313         ARegion *ar;
2314         View2D *v2d;
2315         int channel_index;
2316         int notifierFlags = 0;
2317         short selectmode;
2318         float x, y;
2319         
2320         
2321         /* get editor data */
2322         if (ANIM_animdata_get_context(C, &ac) == 0)
2323                 return OPERATOR_CANCELLED;
2324                 
2325         /* get useful pointers from animation context data */
2326         ar= ac.ar;
2327         v2d= &ar->v2d;
2328         
2329         /* select mode is either replace (deselect all, then add) or add/extend */
2330         if (RNA_boolean_get(op->ptr, "extend"))
2331                 selectmode= SELECT_INVERT;
2332         else if (RNA_boolean_get(op->ptr, "children_only"))
2333                 selectmode= -1; /* this is a bit of a special case for ActionGroups only... should it be removed or extended to all instead? */
2334         else
2335                 selectmode= SELECT_REPLACE;
2336         
2337         /* figure out which channel user clicked in 
2338          * Note: although channels technically start at y= ACHANNEL_FIRST, we need to adjust by half a channel's height
2339          *              so that the tops of channels get caught ok. Since ACHANNEL_FIRST is really ACHANNEL_HEIGHT, we simply use
2340          *              ACHANNEL_HEIGHT_HALF.
2341          */
2342         UI_view2d_region_to_view(v2d, event->mval[0], event->mval[1], &x, &y);
2343         UI_view2d_listview_view_to_cell(v2d, ACHANNEL_NAMEWIDTH, ACHANNEL_STEP, 0, (float)ACHANNEL_HEIGHT_HALF, x, y, NULL, &channel_index);
2344         
2345         /* handle mouse-click in the relevant channel then */
2346         notifierFlags= mouse_anim_channels(&ac, x, channel_index, selectmode);
2347         
2348         /* set notifier that things have changed */
2349         WM_event_add_notifier(C, NC_ANIMATION|notifierFlags, NULL);
2350         
2351         return OPERATOR_FINISHED;
2352 }
2353  
2354 static void ANIM_OT_channels_click (wmOperatorType *ot)
2355 {
2356         /* identifiers */
2357         ot->name = "Mouse Click on Channels";
2358         ot->idname = "ANIM_OT_channels_click";
2359         ot->description = "Handle mouse-clicks over animation channels";
2360         
2361         /* api callbacks */
2362         ot->invoke = animchannels_mouseclick_invoke;
2363         ot->poll = animedit_poll_channels_active;
2364         
2365         /* flags */
2366         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
2367         
2368         /* id-props */
2369         RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", ""); // SHIFTKEY
2370         RNA_def_boolean(ot->srna, "children_only", 0, "Select Children Only", ""); // CTRLKEY|SHIFTKEY
2371 }
2372
2373 /* ************************************************************************** */
2374 /* Operator Registration */
2375
2376 void ED_operatortypes_animchannels(void)
2377 {
2378         WM_operatortype_append(ANIM_OT_channels_select_all_toggle);
2379         WM_operatortype_append(ANIM_OT_channels_select_border);
2380         
2381         WM_operatortype_append(ANIM_OT_channels_click);
2382         WM_operatortype_append(ANIM_OT_channels_rename);
2383         
2384         WM_operatortype_append(ANIM_OT_channels_setting_enable);
2385         WM_operatortype_append(ANIM_OT_channels_setting_disable);
2386         WM_operatortype_append(ANIM_OT_channels_setting_toggle);
2387         
2388         WM_operatortype_append(ANIM_OT_channels_delete);
2389         
2390                 // XXX does this need to be a separate operator?
2391         WM_operatortype_append(ANIM_OT_channels_editable_toggle);
2392         
2393         WM_operatortype_append(ANIM_OT_channels_move);
2394         
2395         WM_operatortype_append(ANIM_OT_channels_expand);
2396         WM_operatortype_append(ANIM_OT_channels_collapse);
2397         
2398         WM_operatortype_append(ANIM_OT_channels_visibility_toggle);
2399         WM_operatortype_append(ANIM_OT_channels_visibility_set);
2400         
2401         WM_operatortype_append(ANIM_OT_channels_fcurves_enable);
2402 }
2403
2404 // TODO: check on a poll callback for this, to get hotkeys into menus
2405 void ED_keymap_animchannels(wmKeyConfig *keyconf)
2406 {
2407         wmKeyMap *keymap = WM_keymap_find(keyconf, "Animation Channels", 0, 0);
2408         wmKeyMapItem *kmi;
2409
2410         /* selection */
2411                 /* click-select */
2412                 // XXX for now, only leftmouse.... 
2413         WM_keymap_add_item(keymap, "ANIM_OT_channels_click", LEFTMOUSE, KM_PRESS, 0, 0);
2414         RNA_boolean_set(WM_keymap_add_item(keymap, "ANIM_OT_channels_click", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "extend", TRUE);
2415         RNA_boolean_set(WM_keymap_add_item(keymap, "ANIM_OT_channels_click", LEFTMOUSE, KM_PRESS, KM_CTRL|KM_SHIFT, 0)->ptr, "children_only", TRUE);
2416         
2417                 /* rename */
2418         WM_keymap_add_item(keymap, "ANIM_OT_channels_rename", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
2419         
2420                 /* deselect all */
2421         WM_keymap_add_item(keymap, "ANIM_OT_channels_select_all_toggle", AKEY, KM_PRESS, 0, 0);
2422         RNA_boolean_set(WM_keymap_add_item(keymap, "ANIM_OT_channels_select_all_toggle", IKEY, KM_PRESS, KM_CTRL, 0)->ptr, "invert", TRUE);
2423         
2424                 /* borderselect */
2425         WM_keymap_add_item(keymap, "ANIM_OT_channels_select_border", BKEY, KM_PRESS, 0, 0);
2426         WM_keymap_add_item(keymap, "ANIM_OT_channels_select_border", EVT_TWEAK_L, KM_ANY, 0, 0);
2427         
2428         /* delete */
2429         WM_keymap_add_item(keymap, "ANIM_OT_channels_delete", XKEY, KM_PRESS, 0, 0);
2430         WM_keymap_add_item(keymap, "ANIM_OT_channels_delete", DELKEY, KM_PRESS, 0, 0);
2431         
2432         /* settings */
2433         WM_keymap_add_item(keymap, "ANIM_OT_channels_setting_toggle", WKEY, KM_PRESS, KM_SHIFT, 0);
2434         WM_keymap_add_item(keymap, "ANIM_OT_channels_setting_enable", WKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
2435         WM_keymap_add_item(keymap, "ANIM_OT_channels_setting_disable", WKEY, KM_PRESS, KM_ALT, 0);
2436         
2437         /* settings - specialized hotkeys */
2438         WM_keymap_add_item(keymap, "ANIM_OT_channels_editable_toggle", TABKEY, KM_PRESS, 0, 0);
2439         
2440         /* expand/collapse */
2441         WM_keymap_add_item(keymap, "ANIM_OT_channels_expand", PADPLUSKEY, KM_PRESS, 0, 0);
2442         WM_keymap_add_item(keymap, "ANIM_OT_channels_collapse", PADMINUS, KM_PRESS, 0, 0);
2443         
2444         kmi = WM_keymap_add_item(keymap, "ANIM_OT_channels_expand", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
2445         RNA_boolean_set(kmi->ptr, "all", FALSE);
2446         kmi = WM_keymap_add_item(keymap, "ANIM_OT_channels_collapse", PADMINUS, KM_PRESS, KM_CTRL, 0);
2447         RNA_boolean_set(kmi->ptr, "all", FALSE);
2448
2449         /* rearranging */
2450         RNA_enum_set(WM_keymap_add_item(keymap, "ANIM_OT_channels_move", PAGEUPKEY, KM_PRESS, 0, 0)->ptr, "direction", REARRANGE_ANIMCHAN_UP);
2451         RNA_enum_set(WM_keymap_add_item(keymap, "ANIM_OT_channels_move", PAGEDOWNKEY, KM_PRESS, 0, 0)->ptr, "direction", REARRANGE_ANIMCHAN_DOWN);
2452         RNA_enum_set(WM_keymap_add_item(keymap, "ANIM_OT_channels_move", PAGEUPKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "direction", REARRANGE_ANIMCHAN_TOP);
2453         RNA_enum_set(WM_keymap_add_item(keymap, "ANIM_OT_channels_move", PAGEDOWNKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "direction", REARRANGE_ANIMCHAN_BOTTOM);
2454         
2455         /* Graph Editor only */
2456         WM_keymap_add_item(keymap, "ANIM_OT_channels_visibility_set", VKEY, KM_PRESS, 0, 0);
2457         WM_keymap_add_item(keymap, "ANIM_OT_channels_visibility_toggle", VKEY, KM_PRESS, KM_SHIFT, 0);
2458 }
2459
2460 /* ************************************************************************** */