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