Cleanup: commas at the end of enums
[blender.git] / source / blender / editors / animation / anim_channels_edit.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2009 Blender Foundation, Joshua Leung
19  * All rights reserved.
20  *
21  * Contributor(s): Joshua Leung
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/animation/anim_channels_edit.c
27  *  \ingroup edanimation
28  */
29
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_utildefines.h"
39 #include "BLI_listbase.h"
40
41 #include "DNA_anim_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_key_types.h"
45 #include "DNA_gpencil_types.h"
46 #include "DNA_mask_types.h"
47
48 #include "RNA_access.h"
49 #include "RNA_define.h"
50
51 #include "BKE_animsys.h"
52 #include "BKE_action.h"
53 #include "BKE_depsgraph.h"
54 #include "BKE_fcurve.h"
55 #include "BKE_gpencil.h"
56 #include "BKE_context.h"
57 #include "BKE_mask.h"
58 #include "BKE_global.h"
59 #include "BKE_library.h"
60
61 #include "UI_view2d.h"
62
63 #include "ED_anim_api.h"
64 #include "ED_armature.h"
65 #include "ED_keyframes_edit.h" // XXX move the select modes out of there!
66 #include "ED_object.h"
67 #include "ED_screen.h"
68
69 #include "WM_api.h"
70 #include "WM_types.h"
71
72 /* ************************************************************************** */
73 /* CHANNELS API - Exposed API */
74
75 /* -------------------------- Selection ------------------------------------- */
76
77 /* Set the given animation-channel as the active one for the active context */
78 // TODO: extend for animdata types...
79 void ANIM_set_active_channel(bAnimContext *ac, void *data, eAnimCont_Types datatype, eAnimFilter_Flags filter, void *channel_data, eAnim_ChannelType channel_type)
80 {
81         ListBase anim_data = {NULL, NULL};
82         bAnimListElem *ale;
83
84         /* try to build list of filtered items */
85         ANIM_animdata_filter(ac, &anim_data, filter, data, datatype);
86         if (BLI_listbase_is_empty(&anim_data))
87                 return;
88
89         /* only clear the 'active' flag for the channels of the same type */
90         for (ale = anim_data.first; ale; ale = ale->next) {
91                 /* skip if types don't match */
92                 if (channel_type != ale->type)
93                         continue;
94
95                 /* flag to set depends on type */
96                 switch (ale->type) {
97                         case ANIMTYPE_GROUP:
98                         {
99                                 bActionGroup *agrp = (bActionGroup *)ale->data;
100
101                                 ACHANNEL_SET_FLAG(agrp, ACHANNEL_SETFLAG_CLEAR, AGRP_ACTIVE);
102                                 break;
103                         }
104                         case ANIMTYPE_FCURVE:
105                         case ANIMTYPE_NLACURVE:
106                         {
107                                 FCurve *fcu = (FCurve *)ale->data;
108
109                                 ACHANNEL_SET_FLAG(fcu, ACHANNEL_SETFLAG_CLEAR, FCURVE_ACTIVE);
110                                 break;
111                         }
112                         case ANIMTYPE_NLATRACK:
113                         {
114                                 NlaTrack *nlt = (NlaTrack *)ale->data;
115
116                                 ACHANNEL_SET_FLAG(nlt, ACHANNEL_SETFLAG_CLEAR, NLATRACK_ACTIVE);
117                                 break;
118                         }
119                         case ANIMTYPE_FILLACTD: /* Action Expander */
120                         case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
121                         case ANIMTYPE_DSLAM:
122                         case ANIMTYPE_DSCAM:
123                         case ANIMTYPE_DSCACHEFILE:
124                         case ANIMTYPE_DSCUR:
125                         case ANIMTYPE_DSSKEY:
126                         case ANIMTYPE_DSWOR:
127                         case ANIMTYPE_DSPART:
128                         case ANIMTYPE_DSMBALL:
129                         case ANIMTYPE_DSARM:
130                         case ANIMTYPE_DSMESH:
131                         case ANIMTYPE_DSTEX:
132                         case ANIMTYPE_DSLAT:
133                         case ANIMTYPE_DSLINESTYLE:
134                         case ANIMTYPE_DSSPK:
135                         case ANIMTYPE_DSGPENCIL:
136                         case ANIMTYPE_DSMCLIP:
137                         {
138                                 /* need to verify that this data is valid for now */
139                                 if (ale->adt) {
140                                         ACHANNEL_SET_FLAG(ale->adt, ACHANNEL_SETFLAG_CLEAR, ADT_UI_ACTIVE);
141                                 }
142                                 break;
143                         }
144                         case ANIMTYPE_GPLAYER:
145                         {
146                                 bGPDlayer *gpl = (bGPDlayer *)ale->data;
147
148                                 ACHANNEL_SET_FLAG(gpl, ACHANNEL_SETFLAG_CLEAR, GP_LAYER_ACTIVE);
149                                 break;
150                         }
151                 }
152         }
153
154         /* set active flag */
155         if (channel_data) {
156                 switch (channel_type) {
157                         case ANIMTYPE_GROUP:
158                         {
159                                 bActionGroup *agrp = (bActionGroup *)channel_data;
160                                 agrp->flag |= AGRP_ACTIVE;
161                                 break;
162                         }
163                         case ANIMTYPE_FCURVE:
164                         case ANIMTYPE_NLACURVE:
165                         {
166                                 FCurve *fcu = (FCurve *)channel_data;
167                                 fcu->flag |= FCURVE_ACTIVE;
168                                 break;
169                         }
170                         case ANIMTYPE_NLATRACK:
171                         {
172                                 NlaTrack *nlt = (NlaTrack *)channel_data;
173                                 nlt->flag |= NLATRACK_ACTIVE;
174                                 break;
175                         }
176                         case ANIMTYPE_FILLACTD: /* Action Expander */
177                         case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
178                         case ANIMTYPE_DSLAM:
179                         case ANIMTYPE_DSCAM:
180                         case ANIMTYPE_DSCACHEFILE:
181                         case ANIMTYPE_DSCUR:
182                         case ANIMTYPE_DSSKEY:
183                         case ANIMTYPE_DSWOR:
184                         case ANIMTYPE_DSPART:
185                         case ANIMTYPE_DSMBALL:
186                         case ANIMTYPE_DSARM:
187                         case ANIMTYPE_DSMESH:
188                         case ANIMTYPE_DSLAT:
189                         case ANIMTYPE_DSLINESTYLE:
190                         case ANIMTYPE_DSSPK:
191                         case ANIMTYPE_DSNTREE:
192                         case ANIMTYPE_DSTEX:
193                         case ANIMTYPE_DSGPENCIL:
194                         case ANIMTYPE_DSMCLIP:
195                         {
196                                 /* need to verify that this data is valid for now */
197                                 if (ale && ale->adt) {
198                                         ale->adt->flag |= ADT_UI_ACTIVE;
199                                 }
200                                 break;
201                         }
202
203                         case ANIMTYPE_GPLAYER:
204                         {
205                                 bGPDlayer *gpl = (bGPDlayer *)channel_data;
206                                 gpl->flag |= GP_LAYER_ACTIVE;
207                                 break;
208                         }
209
210                         /* unhandled currently, but may be interesting */
211                         case ANIMTYPE_MASKLAYER:
212                         case ANIMTYPE_SHAPEKEY:
213                         case ANIMTYPE_NLAACTION:
214                                 break;
215
216                         /* other types */
217                         default:
218                                 break;
219                 }
220         }
221
222         /* clean up */
223         ANIM_animdata_freelist(&anim_data);
224 }
225
226 static void select_pchan_for_action_group(bAnimContext *ac, bActionGroup *agrp, bAnimListElem *ale)
227 {
228         /* Armatures-Specific Feature:
229          * See mouse_anim_channels() -> ANIMTYPE_GROUP case for more details (T38737)
230          */
231         if ((ac->ads->filterflag & ADS_FILTER_ONLYSEL) == 0) {
232                 if ((ale->id) && (GS(ale->id->name) == ID_OB)) {
233                         Object *ob = (Object *)ale->id;
234                         if (ob->type == OB_ARMATURE) {
235                                 /* Assume for now that any group with corresponding name is what we want
236                                  * (i.e. for an armature whose location is animated, things would break
237                                  * if the user were to add a bone named "Location").
238                                  *
239                                  * TODO: check the first F-Curve or so to be sure...
240                                  */
241                                 bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, agrp->name);
242                                 if (agrp->flag & AGRP_SELECTED) {
243                                         ED_pose_bone_select(ob, pchan, true);
244                                 }
245                                 else {
246                                         ED_pose_bone_select(ob, pchan, false);
247                                 }
248                         }
249                 }
250         }
251 }
252
253 /* Deselect all animation channels
254  * - data: pointer to datatype, as contained in bAnimContext
255  * - datatype: the type of data that 'data' represents (eAnimCont_Types)
256  * - test: check if deselecting instead of selecting
257  * - sel: eAnimChannels_SetFlag;
258  */
259 void ANIM_deselect_anim_channels(bAnimContext *ac, void *data, eAnimCont_Types datatype, bool test, eAnimChannels_SetFlag sel)
260 {
261         ListBase anim_data = {NULL, NULL};
262         bAnimListElem *ale;
263         int filter;
264
265         /* filter data */
266         /* NOTE: no list visible, otherwise, we get dangling */
267         filter = ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_CHANNELS;
268         ANIM_animdata_filter(ac, &anim_data, filter, data, datatype);
269
270         /* See if we should be selecting or deselecting */
271         if (test) {
272                 for (ale = anim_data.first; ale; ale = ale->next) {
273                         if (sel == 0)
274                                 break;
275
276                         switch (ale->type) {
277                                 case ANIMTYPE_SCENE:
278                                         if (ale->flag & SCE_DS_SELECTED)
279                                                 sel = ACHANNEL_SETFLAG_CLEAR;
280                                         break;
281                                 case ANIMTYPE_OBJECT:
282 #if 0   /* for now, do not take object selection into account, since it gets too annoying */
283                                         if (ale->flag & SELECT)
284                                                 sel = ACHANNEL_SETFLAG_CLEAR;
285 #endif
286                                         break;
287                                 case ANIMTYPE_GROUP:
288                                         if (ale->flag & AGRP_SELECTED)
289                                                 sel = ACHANNEL_SETFLAG_CLEAR;
290                                         break;
291                                 case ANIMTYPE_FCURVE:
292                                 case ANIMTYPE_NLACURVE:
293                                         if (ale->flag & FCURVE_SELECTED)
294                                                 sel = ACHANNEL_SETFLAG_CLEAR;
295                                         break;
296                                 case ANIMTYPE_SHAPEKEY:
297                                         if (ale->flag & KEYBLOCK_SEL)
298                                                 sel = ACHANNEL_SETFLAG_CLEAR;
299                                         break;
300                                 case ANIMTYPE_NLATRACK:
301                                         if (ale->flag & NLATRACK_SELECTED)
302                                                 sel = ACHANNEL_SETFLAG_CLEAR;
303                                         break;
304
305                                 case ANIMTYPE_FILLACTD: /* Action Expander */
306                                 case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
307                                 case ANIMTYPE_DSLAM:
308                                 case ANIMTYPE_DSCAM:
309                                 case ANIMTYPE_DSCACHEFILE:
310                                 case ANIMTYPE_DSCUR:
311                                 case ANIMTYPE_DSSKEY:
312                                 case ANIMTYPE_DSWOR:
313                                 case ANIMTYPE_DSPART:
314                                 case ANIMTYPE_DSMBALL:
315                                 case ANIMTYPE_DSARM:
316                                 case ANIMTYPE_DSMESH:
317                                 case ANIMTYPE_DSNTREE:
318                                 case ANIMTYPE_DSTEX:
319                                 case ANIMTYPE_DSLAT:
320                                 case ANIMTYPE_DSLINESTYLE:
321                                 case ANIMTYPE_DSSPK:
322                                 case ANIMTYPE_DSGPENCIL:
323                                 case ANIMTYPE_DSMCLIP:
324                                 {
325                                         if ((ale->adt) && (ale->adt->flag & ADT_UI_SELECTED))
326                                                 sel = ACHANNEL_SETFLAG_CLEAR;
327                                         break;
328                                 }
329                                 case ANIMTYPE_GPLAYER:
330                                         if (ale->flag & GP_LAYER_SELECT)
331                                                 sel = ACHANNEL_SETFLAG_CLEAR;
332                                         break;
333                                 case ANIMTYPE_MASKLAYER:
334                                         if (ale->flag & MASK_LAYERFLAG_SELECT)
335                                                 sel = ACHANNEL_SETFLAG_CLEAR;
336                                         break;
337                         }
338                 }
339         }
340
341         /* Now set the flags */
342         for (ale = anim_data.first; ale; ale = ale->next) {
343                 switch (ale->type) {
344                         case ANIMTYPE_SCENE:
345                         {
346                                 Scene *scene = (Scene *)ale->data;
347
348                                 ACHANNEL_SET_FLAG(scene, sel, SCE_DS_SELECTED);
349
350                                 if (scene->adt) {
351                                         ACHANNEL_SET_FLAG(scene, sel, ADT_UI_SELECTED);
352                                 }
353                                 break;
354                         }
355                         case ANIMTYPE_OBJECT:
356                         {
357 #if 0   /* for now, do not take object selection into account, since it gets too annoying */
358                                 Base *base = (Base *)ale->data;
359                                 Object *ob = base->object;
360
361                                 ACHANNEL_SET_FLAG(base, sel, SELECT);
362                                 ACHANNEL_SET_FLAG(ob, sel, SELECT);
363
364                                 if (ob->adt) {
365                                         ACHANNEL_SET_FLAG(ob, sel, ADT_UI_SELECTED);
366                                 }
367 #endif
368                                 break;
369                         }
370                         case ANIMTYPE_GROUP:
371                         {
372                                 bActionGroup *agrp = (bActionGroup *)ale->data;
373                                 ACHANNEL_SET_FLAG(agrp, sel, AGRP_SELECTED);
374                                 select_pchan_for_action_group(ac, agrp, ale);
375                                 agrp->flag &= ~AGRP_ACTIVE;
376                                 break;
377                         }
378                         case ANIMTYPE_FCURVE:
379                         case ANIMTYPE_NLACURVE:
380                         {
381                                 FCurve *fcu = (FCurve *)ale->data;
382
383                                 ACHANNEL_SET_FLAG(fcu, sel, FCURVE_SELECTED);
384                                 fcu->flag &= ~FCURVE_ACTIVE;
385                                 break;
386                         }
387                         case ANIMTYPE_SHAPEKEY:
388                         {
389                                 KeyBlock *kb = (KeyBlock *)ale->data;
390
391                                 ACHANNEL_SET_FLAG(kb, sel, KEYBLOCK_SEL);
392                                 break;
393                         }
394                         case ANIMTYPE_NLATRACK:
395                         {
396                                 NlaTrack *nlt = (NlaTrack *)ale->data;
397
398                                 ACHANNEL_SET_FLAG(nlt, sel, NLATRACK_SELECTED);
399                                 nlt->flag &= ~NLATRACK_ACTIVE;
400                                 break;
401                         }
402                         case ANIMTYPE_FILLACTD: /* Action Expander */
403                         case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
404                         case ANIMTYPE_DSLAM:
405                         case ANIMTYPE_DSCAM:
406                         case ANIMTYPE_DSCACHEFILE:
407                         case ANIMTYPE_DSCUR:
408                         case ANIMTYPE_DSSKEY:
409                         case ANIMTYPE_DSWOR:
410                         case ANIMTYPE_DSPART:
411                         case ANIMTYPE_DSMBALL:
412                         case ANIMTYPE_DSARM:
413                         case ANIMTYPE_DSMESH:
414                         case ANIMTYPE_DSNTREE:
415                         case ANIMTYPE_DSTEX:
416                         case ANIMTYPE_DSLAT:
417                         case ANIMTYPE_DSLINESTYLE:
418                         case ANIMTYPE_DSSPK:
419                         case ANIMTYPE_DSGPENCIL:
420                         case ANIMTYPE_DSMCLIP:
421                         {
422                                 /* need to verify that this data is valid for now */
423                                 if (ale->adt) {
424                                         ACHANNEL_SET_FLAG(ale->adt, sel, ADT_UI_SELECTED);
425                                         ale->adt->flag &= ~ADT_UI_ACTIVE;
426                                 }
427                                 break;
428                         }
429                         case ANIMTYPE_GPLAYER:
430                         {
431                                 bGPDlayer *gpl = (bGPDlayer *)ale->data;
432
433                                 ACHANNEL_SET_FLAG(gpl, sel, GP_LAYER_SELECT);
434                                 break;
435                         }
436                         case ANIMTYPE_MASKLAYER:
437                         {
438                                 MaskLayer *masklay = (MaskLayer *)ale->data;
439
440                                 ACHANNEL_SET_FLAG(masklay, sel, MASK_LAYERFLAG_SELECT);
441                                 break;
442                         }
443                 }
444         }
445
446         /* Cleanup */
447         ANIM_animdata_freelist(&anim_data);
448 }
449
450 /* ---------------------------- Graph Editor ------------------------------------- */
451
452 /* Flush visibility (for Graph Editor) changes up/down hierarchy for changes in the given setting
453  * - anim_data: list of the all the anim channels that can be chosen
454  *   -> filtered using ANIMFILTER_CHANNELS only, since if we took VISIBLE too,
455  *      then the channels under closed expanders get ignored...
456  * - ale_setting: the anim channel (not in the anim_data list directly, though occurring there)
457  *   with the new state of the setting that we want flushed up/down the hierarchy
458  * - setting: type of setting to set
459  * - on: whether the visibility setting has been enabled or disabled
460  */
461 void ANIM_flush_setting_anim_channels(bAnimContext *ac, ListBase *anim_data, bAnimListElem *ale_setting, eAnimChannel_Settings setting, eAnimChannels_SetFlag mode)
462 {
463         bAnimListElem *ale, *match = NULL;
464         int prevLevel = 0, matchLevel = 0;
465
466         /* sanity check */
467         if (ELEM(NULL, anim_data, anim_data->first))
468                 return;
469
470         if (setting == ACHANNEL_SETTING_ALWAYS_VISIBLE) {
471                 return;
472         }
473
474         /* find the channel that got changed */
475         for (ale = anim_data->first; ale; ale = ale->next) {
476                 /* compare data, and type as main way of identifying the channel */
477                 if ((ale->data == ale_setting->data) && (ale->type == ale_setting->type)) {
478                         /* we also have to check the ID, this is assigned to, since a block may have multiple users */
479                         /* TODO: is the owner-data more revealing? */
480                         if (ale->id == ale_setting->id) {
481                                 match = ale;
482                                 break;
483                         }
484                 }
485         }
486         if (match == NULL) {
487                 printf("ERROR: no channel matching the one changed was found\n");
488                 return;
489         }
490         else {
491                 const bAnimChannelType *acf = ANIM_channel_get_typeinfo(ale_setting);
492
493                 if (acf == NULL) {
494                         printf("ERROR: no channel info for the changed channel\n");
495                         return;
496                 }
497
498                 /* get the level of the channel that was affected
499                  *   - we define the level as simply being the offset for the start of the channel
500                  */
501                 matchLevel = (acf->get_offset) ? acf->get_offset(ac, ale_setting) : 0;
502                 prevLevel = matchLevel;
503         }
504
505         /* flush up?
506          *
507          * For Visibility:
508          * - only flush up if the current state is now enabled (positive 'on' state is default)
509          *   (otherwise, it's too much work to force the parents to be inactive too)
510          *
511          * For everything else:
512          * - only flush up if the current state is now disabled (negative 'off' state is default)
513          *   (otherwise, it's too much work to force the parents to be active too)
514          */
515         if ( ((setting == ACHANNEL_SETTING_VISIBLE) && (mode != ACHANNEL_SETFLAG_CLEAR)) ||
516              ((setting != ACHANNEL_SETTING_VISIBLE) && (mode == ACHANNEL_SETFLAG_CLEAR)))
517         {
518                 /* go backwards in the list, until the highest-ranking element (by indention has been covered) */
519                 for (ale = match->prev; ale; ale = ale->prev) {
520                         const bAnimChannelType *acf = ANIM_channel_get_typeinfo(ale);
521                         int level;
522
523                         /* if no channel info was found, skip, since this type might not have any useful info */
524                         if (acf == NULL)
525                                 continue;
526
527                         /* get the level of the current channel traversed
528                          *   - we define the level as simply being the offset for the start of the channel
529                          */
530                         level = (acf->get_offset) ? acf->get_offset(ac, ale) : 0;
531
532                         /* if the level is 'less than' (i.e. more important) the level we're matching
533                          * but also 'less than' the level just tried (i.e. only the 1st group above grouped F-Curves,
534                          * when toggling visibility of F-Curves, gets flushed, which should happen if we don't let prevLevel
535                          * get updated below once the first 1st group is found)...
536                          */
537                         if (level < prevLevel) {
538                                 /* flush the new status... */
539                                 ANIM_channel_setting_set(ac, ale, setting, mode);
540
541                                 /* store this level as the 'old' level now */
542                                 prevLevel = level;
543                         }
544                         /* if the level is 'greater than' (i.e. less important) than the previous level... */
545                         else if (level > prevLevel) {
546                                 /* if previous level was a base-level (i.e. 0 offset / root of one hierarchy),
547                                  * stop here
548                                  */
549                                 if (prevLevel == 0)
550                                         break;
551                                 /* otherwise, this level weaves into another sibling hierarchy to the previous one just
552                                  * finished, so skip until we get to the parent of this level
553                                  */
554                                 else
555                                         continue;
556                         }
557                 }
558         }
559
560         /* flush down (always) */
561         {
562                 /* go forwards in the list, until the lowest-ranking element (by indention has been covered) */
563                 for (ale = match->next; ale; ale = ale->next) {
564                         const bAnimChannelType *acf = ANIM_channel_get_typeinfo(ale);
565                         int level;
566
567                         /* if no channel info was found, skip, since this type might not have any useful info */
568                         if (acf == NULL)
569                                 continue;
570
571                         /* get the level of the current channel traversed
572                          *   - we define the level as simply being the offset for the start of the channel
573                          */
574                         level = (acf->get_offset) ? acf->get_offset(ac, ale) : 0;
575
576                         /* if the level is 'greater than' (i.e. less important) the channel that was changed,
577                          * flush the new status...
578                          */
579                         if (level > matchLevel)
580                                 ANIM_channel_setting_set(ac, ale, setting, mode);
581                         /* however, if the level is 'less than or equal to' the channel that was changed,
582                          * (i.e. the current channel is as important if not more important than the changed channel)
583                          * then we should stop, since we've found the last one of the children we should flush
584                          */
585                         else
586                                 break;
587
588                         /* store this level as the 'old' level now */
589                         // prevLevel = level; // XXX: prevLevel is unused
590                 }
591         }
592 }
593
594 /* -------------------------- F-Curves ------------------------------------- */
595
596 /* Delete the given F-Curve from its AnimData block */
597 void ANIM_fcurve_delete_from_animdata(bAnimContext *ac, AnimData *adt, FCurve *fcu)
598 {
599         /* - if no AnimData, we've got nowhere to remove the F-Curve from
600          *   (this doesn't guarantee that the F-Curve is in there, but at least we tried
601          * - if no F-Curve, there is nothing to remove
602          */
603         if (ELEM(NULL, adt, fcu))
604                 return;
605
606         /* remove from whatever list it came from
607          * - Action Group
608          * - Action
609          * - Drivers
610          * - TODO... some others?
611          */
612         if ((ac) && (ac->datatype == ANIMCONT_DRIVERS)) {
613                 /* driver F-Curve */
614                 BLI_remlink(&adt->drivers, fcu);
615         }
616         else if (adt->action) {
617                 bAction *act = adt->action;
618
619                 /* remove from group or action, whichever one "owns" the F-Curve */
620                 if (fcu->grp) {
621                         bActionGroup *agrp = fcu->grp;
622
623                         /* remove F-Curve from group+action */
624                         action_groups_remove_channel(act, fcu);
625
626                         /* if group has no more channels, remove it too,
627                          * otherwise can have many dangling groups [#33541]
628                          */
629                         if (BLI_listbase_is_empty(&agrp->channels)) {
630                                 BLI_freelinkN(&act->groups, agrp);
631                         }
632                 }
633                 else {
634                         BLI_remlink(&act->curves, fcu);
635                 }
636
637                 /* if action has no more F-Curves as a result of this, unlink it from
638                  * AnimData if it did not come from a NLA Strip being tweaked.
639                  *
640                  * This is done so that we don't have dangling Object+Action entries in
641                  * channel list that are empty, and linger around long after the data they
642                  * are for has disappeared (and probably won't come back).
643                  */
644                 if (BLI_listbase_is_empty(&act->curves) && (adt->flag & ADT_NLA_EDIT_ON) == 0) {
645                         id_us_min(&act->id);
646                         adt->action = NULL;
647                 }
648         }
649
650         /* free the F-Curve itself */
651         free_fcurve(fcu);
652 }
653
654 /* ************************************************************************** */
655 /* OPERATORS */
656
657 /* ****************** Operator Utilities ********************************** */
658
659 /* poll callback for being in an Animation Editor channels list region */
660 static bool animedit_poll_channels_active(bContext *C)
661 {
662         ScrArea *sa = CTX_wm_area(C);
663
664         /* channels region test */
665         /* TODO: could enhance with actually testing if channels region? */
666         if (ELEM(NULL, sa, CTX_wm_region(C)))
667                 return 0;
668         /* animation editor test */
669         if (ELEM(sa->spacetype, SPACE_ACTION, SPACE_IPO, SPACE_NLA) == 0)
670                 return 0;
671
672         return 1;
673 }
674
675 /* poll callback for Animation Editor channels list region + not in NLA-tweakmode for NLA */
676 static bool animedit_poll_channels_nla_tweakmode_off(bContext *C)
677 {
678         ScrArea *sa = CTX_wm_area(C);
679         Scene *scene = CTX_data_scene(C);
680
681         /* channels region test */
682         /* TODO: could enhance with actually testing if channels region? */
683         if (ELEM(NULL, sa, CTX_wm_region(C)))
684                 return 0;
685         /* animation editor test */
686         if (ELEM(sa->spacetype, SPACE_ACTION, SPACE_IPO, SPACE_NLA) == 0)
687                 return 0;
688
689         /* NLA TweakMode test */
690         if (sa->spacetype == SPACE_NLA) {
691                 if ((scene == NULL) || (scene->flag & SCE_NLA_EDIT_ON))
692                         return 0;
693         }
694
695         return 1;
696 }
697
698 /* ****************** Rearrange Channels Operator ******************* */
699
700 /* constants for channel rearranging */
701 /* WARNING: don't change existing ones without modifying rearrange func accordingly */
702 typedef enum eRearrangeAnimChan_Mode {
703         REARRANGE_ANIMCHAN_TOP = -2,
704         REARRANGE_ANIMCHAN_UP = -1,
705         REARRANGE_ANIMCHAN_DOWN = 1,
706         REARRANGE_ANIMCHAN_BOTTOM = 2,
707 } eRearrangeAnimChan_Mode;
708
709 /* defines for rearranging channels */
710 static const EnumPropertyItem prop_animchannel_rearrange_types[] = {
711         {REARRANGE_ANIMCHAN_TOP, "TOP", 0, "To Top", ""},
712         {REARRANGE_ANIMCHAN_UP, "UP", 0, "Up", ""},
713         {REARRANGE_ANIMCHAN_DOWN, "DOWN", 0, "Down", ""},
714         {REARRANGE_ANIMCHAN_BOTTOM, "BOTTOM", 0, "To Bottom", ""},
715         {0, NULL, 0, NULL, NULL}
716 };
717
718 /* Reordering "Islands" Defines ----------------------------------- */
719
720 /* Island definition - just a listbase container */
721 typedef struct tReorderChannelIsland {
722         struct tReorderChannelIsland *next, *prev;
723
724         ListBase channels;  /* channels within this region with the same state */
725         int flag;           /* eReorderIslandFlag */
726 } tReorderChannelIsland;
727
728 /* flags for channel reordering islands */
729 typedef enum eReorderIslandFlag {
730         REORDER_ISLAND_SELECTED         = (1 << 0),   /* island is selected */
731         REORDER_ISLAND_UNTOUCHABLE      = (1 << 1),   /* island should be ignored */
732         REORDER_ISLAND_MOVED            = (1 << 2),   /* island has already been moved */
733         REORDER_ISLAND_HIDDEN           = (1 << 3),   /* island is not visible */
734 } eReorderIslandFlag;
735
736
737 /* Rearrange Methods --------------------------------------------- */
738
739 static bool rearrange_island_ok(tReorderChannelIsland *island)
740 {
741         /* island must not be untouchable */
742         if (island->flag & REORDER_ISLAND_UNTOUCHABLE)
743                 return 0;
744
745         /* island should be selected to be moved */
746         return (island->flag & REORDER_ISLAND_SELECTED) && !(island->flag & REORDER_ISLAND_MOVED);
747 }
748
749 /* ............................. */
750
751 static bool rearrange_island_top(ListBase *list, tReorderChannelIsland *island)
752 {
753         if (rearrange_island_ok(island)) {
754                 /* remove from current position */
755                 BLI_remlink(list, island);
756
757                 /* make it first element */
758                 BLI_insertlinkbefore(list, list->first, island);
759
760                 return 1;
761         }
762
763         return 0;
764 }
765
766 static bool rearrange_island_up(ListBase *list, tReorderChannelIsland *island)
767 {
768         if (rearrange_island_ok(island)) {
769                 /* moving up = moving before the previous island, otherwise we're in the same place */
770                 tReorderChannelIsland *prev = island->prev;
771
772                 /* Skip hidden islands! */
773                 while (prev && prev->flag & REORDER_ISLAND_HIDDEN) {
774                         prev = prev->prev;
775                 }
776
777                 if (prev) {
778                         /* remove from current position */
779                         BLI_remlink(list, island);
780
781                         /* push it up */
782                         BLI_insertlinkbefore(list, prev, island);
783
784                         return 1;
785                 }
786         }
787
788         return 0;
789 }
790
791 static bool rearrange_island_down(ListBase *list, tReorderChannelIsland *island)
792 {
793         if (rearrange_island_ok(island)) {
794                 /* moving down = moving after the next island, otherwise we're in the same place */
795                 tReorderChannelIsland *next = island->next;
796
797                 /* Skip hidden islands! */
798                 while (next && next->flag & REORDER_ISLAND_HIDDEN) {
799                         next = next->next;
800                 }
801
802                 if (next) {
803                         /* can only move past if next is not untouchable (i.e. nothing can go after it) */
804                         if ((next->flag & REORDER_ISLAND_UNTOUCHABLE) == 0) {
805                                 /* remove from current position */
806                                 BLI_remlink(list, island);
807
808                                 /* push it down */
809                                 BLI_insertlinkafter(list, next, island);
810
811                                 return true;
812                         }
813                 }
814                 /* else: no next channel, so we're at the bottom already, so can't move */
815         }
816
817         return false;
818 }
819
820 static bool rearrange_island_bottom(ListBase *list, tReorderChannelIsland *island)
821 {
822         if (rearrange_island_ok(island)) {
823                 tReorderChannelIsland *last = list->last;
824
825                 /* remove island from current position */
826                 BLI_remlink(list, island);
827
828                 /* add before or after the last channel? */
829                 if ((last->flag & REORDER_ISLAND_UNTOUCHABLE) == 0) {
830                         /* can add after it */
831                         BLI_addtail(list, island);
832                 }
833                 else {
834                         /* can at most go just before it, since last cannot be moved */
835                         BLI_insertlinkbefore(list, last, island);
836
837                 }
838
839                 return true;
840         }
841
842         return false;
843 }
844
845 /* ............................. */
846
847 /**
848  * typedef for channel rearranging function
849  *
850  * \param list: List of tReorderChannelIsland's that channels belong to
851  * \param island: Island to be moved
852  * \return Whether operation was a success
853  */
854 typedef bool (*AnimChanRearrangeFp)(ListBase *list, tReorderChannelIsland *island);
855
856 /* get rearranging function, given 'rearrange' mode */
857 static AnimChanRearrangeFp rearrange_get_mode_func(eRearrangeAnimChan_Mode mode)
858 {
859         switch (mode) {
860                 case REARRANGE_ANIMCHAN_TOP:
861                         return rearrange_island_top;
862                 case REARRANGE_ANIMCHAN_UP:
863                         return rearrange_island_up;
864                 case REARRANGE_ANIMCHAN_DOWN:
865                         return rearrange_island_down;
866                 case REARRANGE_ANIMCHAN_BOTTOM:
867                         return rearrange_island_bottom;
868                 default:
869                         return NULL;
870         }
871 }
872
873 /* Rearrange Islands Generics ------------------------------------- */
874
875 /* add channel into list of islands */
876 static void rearrange_animchannel_add_to_islands(ListBase *islands, ListBase *srcList,
877                                                  Link *channel, eAnim_ChannelType type,
878                                                  const bool is_hidden)
879 {
880         /* always try to add to last island if possible */
881         tReorderChannelIsland *island = islands->last;
882         bool is_sel = false, is_untouchable = false;
883
884         /* get flags - selected and untouchable from the channel */
885         switch (type) {
886                 case ANIMTYPE_GROUP:
887                 {
888                         bActionGroup *agrp = (bActionGroup *)channel;
889
890                         is_sel = SEL_AGRP(agrp);
891                         is_untouchable = (agrp->flag & AGRP_TEMP) != 0;
892                         break;
893                 }
894                 case ANIMTYPE_FCURVE:
895                 case ANIMTYPE_NLACURVE:
896                 {
897                         FCurve *fcu = (FCurve *)channel;
898
899                         is_sel = SEL_FCU(fcu);
900                         break;
901                 }
902                 case ANIMTYPE_NLATRACK:
903                 {
904                         NlaTrack *nlt = (NlaTrack *)channel;
905
906                         is_sel = SEL_NLT(nlt);
907                         break;
908                 }
909                 case ANIMTYPE_GPLAYER:
910                 {
911                         bGPDlayer *gpl = (bGPDlayer *)channel;
912
913                         is_sel = SEL_GPL(gpl);
914                         break;
915                 }
916                 default:
917                         printf("rearrange_animchannel_add_to_islands(): don't know how to handle channels of type %u\n", type);
918                         return;
919         }
920
921         /* do we need to add to a new island? */
922         if (/* 1) no islands yet */
923             (island == NULL) ||
924             /* 2) unselected islands have single channels only - to allow up/down movement */
925             ((island->flag & REORDER_ISLAND_SELECTED) == 0) ||
926             /* 3) if channel is unselected, stop existing island (it was either wrong sel status, or full already) */
927             (is_sel == 0) ||
928             /* 4) hidden status changes */
929             ((island->flag & REORDER_ISLAND_HIDDEN) != is_hidden)
930             )
931         {
932                 /* create a new island now */
933                 island = MEM_callocN(sizeof(tReorderChannelIsland), "tReorderChannelIsland");
934                 BLI_addtail(islands, island);
935
936                 if (is_sel)
937                         island->flag |= REORDER_ISLAND_SELECTED;
938                 if (is_untouchable)
939                         island->flag |= REORDER_ISLAND_UNTOUCHABLE;
940                 if (is_hidden)
941                         island->flag |= REORDER_ISLAND_HIDDEN;
942         }
943
944         /* add channel to island - need to remove it from its existing list first though */
945         BLI_remlink(srcList, channel);
946         BLI_addtail(&island->channels, channel);
947 }
948
949 /* flatten islands out into a single list again */
950 static void rearrange_animchannel_flatten_islands(ListBase *islands, ListBase *srcList)
951 {
952         tReorderChannelIsland *island, *isn = NULL;
953
954         /* make sure srcList is empty now */
955         BLI_assert(BLI_listbase_is_empty(srcList));
956
957         /* go through merging islands */
958         for (island = islands->first; island; island = isn) {
959                 isn = island->next;
960
961                 /* merge island channels back to main list, then delete the island */
962                 BLI_movelisttolist(srcList, &island->channels);
963                 BLI_freelinkN(islands, island);
964         }
965 }
966
967 /* ............................. */
968
969 /* get a list of all bAnimListElem's of a certain type which are currently visible */
970 static void rearrange_animchannels_filter_visible(ListBase *anim_data_visible, bAnimContext *ac, eAnim_ChannelType type)
971 {
972         ListBase anim_data = {NULL, NULL};
973         bAnimListElem *ale, *ale_next;
974         int filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
975
976         /* get all visible channels */
977         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
978
979         /* now, only keep the ones that are of the types we are interested in */
980         for (ale = anim_data.first; ale; ale = ale_next) {
981                 ale_next = ale->next;
982
983                 if (ale->type != type) {
984                         BLI_freelinkN(&anim_data, ale);
985                 }
986         }
987
988         /* return cleaned up list */
989         *anim_data_visible = anim_data;
990 }
991
992 /* performing rearranging of channels using islands */
993 static bool rearrange_animchannel_islands(ListBase *list, AnimChanRearrangeFp rearrange_func,
994                                           eRearrangeAnimChan_Mode mode, eAnim_ChannelType type,
995                                           ListBase *anim_data_visible)
996 {
997         ListBase islands = {NULL, NULL};
998         Link *channel, *chanNext = NULL;
999         bool done = false;
1000
1001         /* don't waste effort on an empty list */
1002         if (BLI_listbase_is_empty(list))
1003                 return 0;
1004
1005         /* group channels into islands */
1006         for (channel = list->first; channel; channel = chanNext) {
1007                 /* find out whether this channel is present in anim_data_visible or not! */
1008                 const bool is_hidden = (BLI_findptr(anim_data_visible, channel, offsetof(bAnimListElem, data)) == NULL);
1009                 chanNext = channel->next;
1010                 rearrange_animchannel_add_to_islands(&islands, list, channel, type, is_hidden);
1011         }
1012
1013         /* perform moving of selected islands now, but only if there is more than one of 'em so that something will happen
1014          * - scanning of the list is performed in the opposite direction to the direction we're moving things, so that we
1015          *   shouldn't need to encounter items we've moved already
1016          */
1017         if (islands.first != islands.last) {
1018                 tReorderChannelIsland *first = (mode > 0) ? islands.last : islands.first;
1019                 tReorderChannelIsland *island, *isn = NULL;
1020
1021                 for (island = first; island; island = isn) {
1022                         isn = (mode > 0) ? island->prev : island->next;
1023
1024                         /* perform rearranging */
1025                         if (rearrange_func(&islands, island)) {
1026                                 island->flag |= REORDER_ISLAND_MOVED;
1027                                 done = true;
1028                         }
1029                 }
1030         }
1031
1032         /* ungroup islands */
1033         rearrange_animchannel_flatten_islands(&islands, list);
1034
1035         /* did we do anything? */
1036         return done;
1037 }
1038
1039 /* NLA Specific Stuff ----------------------------------------------------- */
1040
1041 /* Change the order NLA Tracks within NLA Stack
1042  * ! NLA tracks are displayed in opposite order, so directions need care
1043  * mode: REARRANGE_ANIMCHAN_*
1044  */
1045 static void rearrange_nla_channels(bAnimContext *ac, AnimData *adt, eRearrangeAnimChan_Mode mode)
1046 {
1047         AnimChanRearrangeFp rearrange_func;
1048         ListBase anim_data_visible = {NULL, NULL};
1049
1050         /* hack: invert mode so that functions will work in right order */
1051         mode *= -1;
1052
1053         /* get rearranging function */
1054         rearrange_func = rearrange_get_mode_func(mode);
1055         if (rearrange_func == NULL)
1056                 return;
1057
1058         /* Filter visible data. */
1059         rearrange_animchannels_filter_visible(&anim_data_visible, ac, ANIMTYPE_NLATRACK);
1060
1061         /* perform rearranging on tracks list */
1062         rearrange_animchannel_islands(&adt->nla_tracks, rearrange_func, mode, ANIMTYPE_NLATRACK, &anim_data_visible);
1063
1064         /* free temp data */
1065         BLI_freelistN(&anim_data_visible);
1066 }
1067
1068 /* Drivers Specific Stuff ------------------------------------------------- */
1069
1070 /* Change the order drivers within AnimData block
1071  * mode: REARRANGE_ANIMCHAN_*
1072  */
1073 static void rearrange_driver_channels(bAnimContext *ac, AnimData *adt, eRearrangeAnimChan_Mode mode)
1074 {
1075         /* get rearranging function */
1076         AnimChanRearrangeFp rearrange_func = rearrange_get_mode_func(mode);
1077         ListBase anim_data_visible = {NULL, NULL};
1078
1079         if (rearrange_func == NULL)
1080                 return;
1081
1082         /* only consider drivers if they're accessible */
1083         if (EXPANDED_DRVD(adt) == 0)
1084                 return;
1085
1086         /* Filter visible data. */
1087         rearrange_animchannels_filter_visible(&anim_data_visible, ac, ANIMTYPE_FCURVE);
1088
1089         /* perform rearranging on drivers list (drivers are really just F-Curves) */
1090         rearrange_animchannel_islands(&adt->drivers, rearrange_func, mode, ANIMTYPE_FCURVE, &anim_data_visible);
1091
1092         /* free temp data */
1093         BLI_freelistN(&anim_data_visible);
1094 }
1095
1096 /* Action Specific Stuff ------------------------------------------------- */
1097
1098 /* make sure all action-channels belong to a group (and clear action's list) */
1099 static void split_groups_action_temp(bAction *act, bActionGroup *tgrp)
1100 {
1101         bActionGroup *agrp;
1102         FCurve *fcu;
1103
1104         if (act == NULL)
1105                 return;
1106
1107         /* Separate F-Curves into lists per group */
1108         for (agrp = act->groups.first; agrp; agrp = agrp->next) {
1109                 if (agrp->channels.first) {
1110                         fcu = agrp->channels.last;
1111                         act->curves.first = fcu->next;
1112
1113                         fcu = agrp->channels.first;
1114                         fcu->prev = NULL;
1115
1116                         fcu = agrp->channels.last;
1117                         fcu->next = NULL;
1118                 }
1119         }
1120
1121         /* Initialize memory for temp-group */
1122         memset(tgrp, 0, sizeof(bActionGroup));
1123         tgrp->flag |= (AGRP_EXPANDED | AGRP_TEMP);
1124         BLI_strncpy(tgrp->name, "#TempGroup", sizeof(tgrp->name));
1125
1126         /* Move any action-channels not already moved, to the temp group */
1127         if (act->curves.first) {
1128                 /* start of list */
1129                 fcu = act->curves.first;
1130                 fcu->prev = NULL;
1131                 tgrp->channels.first = fcu;
1132                 act->curves.first = NULL;
1133
1134                 /* end of list */
1135                 fcu = act->curves.last;
1136                 fcu->next = NULL;
1137                 tgrp->channels.last = fcu;
1138                 act->curves.last = NULL;
1139
1140                 /* ensure that all of these get their group set to this temp group
1141                  * (so that visibility filtering works)
1142                  */
1143                 for (fcu = tgrp->channels.first; fcu; fcu = fcu->next) {
1144                         fcu->grp = tgrp;
1145                 }
1146         }
1147
1148         /* Add temp-group to list */
1149         BLI_addtail(&act->groups, tgrp);
1150 }
1151
1152 /* link lists of channels that groups have */
1153 static void join_groups_action_temp(bAction *act)
1154 {
1155         bActionGroup *agrp;
1156
1157         for (agrp = act->groups.first; agrp; agrp = agrp->next) {
1158                 ListBase tempGroup;
1159
1160                 /* add list of channels to action's channels */
1161                 tempGroup = agrp->channels;
1162                 BLI_movelisttolist(&act->curves, &agrp->channels);
1163                 agrp->channels = tempGroup;
1164
1165                 /* clear moved flag */
1166                 agrp->flag &= ~AGRP_MOVED;
1167
1168                 /* if group was temporary one:
1169                  * - unassign all FCurves which were temporarily added to it
1170                  * - remove from list (but don't free as it's on the stack!)
1171                  */
1172                 if (agrp->flag & AGRP_TEMP) {
1173                         FCurve *fcu;
1174
1175                         for (fcu = agrp->channels.first; fcu; fcu = fcu->next) {
1176                                 fcu->grp = NULL;
1177                         }
1178
1179                         BLI_remlink(&act->groups, agrp);
1180                         break;
1181                 }
1182         }
1183 }
1184
1185 /* Change the order of anim-channels within action
1186  * mode: REARRANGE_ANIMCHAN_*
1187  */
1188 static void rearrange_action_channels(bAnimContext *ac, bAction *act, eRearrangeAnimChan_Mode mode)
1189 {
1190         bActionGroup tgrp;
1191         ListBase anim_data_visible = {NULL, NULL};
1192         bool do_channels;
1193
1194         /* get rearranging function */
1195         AnimChanRearrangeFp rearrange_func = rearrange_get_mode_func(mode);
1196
1197         if (rearrange_func == NULL)
1198                 return;
1199
1200         /* make sure we're only operating with groups (vs a mixture of groups+curves) */
1201         split_groups_action_temp(act, &tgrp);
1202
1203         /* Filter visible data. */
1204         rearrange_animchannels_filter_visible(&anim_data_visible, ac, ANIMTYPE_GROUP);
1205
1206         /* rearrange groups first
1207          * - the group's channels will only get considered if nothing happened when rearranging the groups
1208          *   i.e. the rearrange function returned 0
1209          */
1210         do_channels = (rearrange_animchannel_islands(&act->groups, rearrange_func, mode, ANIMTYPE_GROUP,
1211                                                      &anim_data_visible) == 0);
1212
1213         /* free temp data */
1214         BLI_freelistN(&anim_data_visible);
1215
1216         if (do_channels) {
1217                 bActionGroup *agrp;
1218
1219                 /* Filter visible data. */
1220                 rearrange_animchannels_filter_visible(&anim_data_visible, ac, ANIMTYPE_FCURVE);
1221
1222                 for (agrp = act->groups.first; agrp; agrp = agrp->next) {
1223                         /* only consider F-Curves if they're visible (group expanded) */
1224                         if (EXPANDED_AGRP(ac, agrp)) {
1225                                 rearrange_animchannel_islands(&agrp->channels, rearrange_func, mode, ANIMTYPE_FCURVE,
1226                                                               &anim_data_visible);
1227                         }
1228                 }
1229
1230                 /* free temp data */
1231                 BLI_freelistN(&anim_data_visible);
1232         }
1233
1234         /* assemble lists into one list (and clear moved tags) */
1235         join_groups_action_temp(act);
1236 }
1237
1238 /* ------------------- */
1239
1240 static void rearrange_nla_control_channels(bAnimContext *ac, AnimData *adt, eRearrangeAnimChan_Mode mode)
1241 {
1242         ListBase anim_data_visible = {NULL, NULL};
1243
1244         NlaTrack *nlt;
1245         NlaStrip *strip;
1246
1247         /* get rearranging function */
1248         AnimChanRearrangeFp rearrange_func = rearrange_get_mode_func(mode);
1249
1250         if (rearrange_func == NULL)
1251                 return;
1252
1253         /* skip if these curves aren't being shown */
1254         if (adt->flag & ADT_NLA_SKEYS_COLLAPSED)
1255                 return;
1256
1257         /* Filter visible data. */
1258         rearrange_animchannels_filter_visible(&anim_data_visible, ac, ANIMTYPE_NLACURVE);
1259
1260         /* we cannot rearrange between strips, but within each strip, we can rearrange those curves */
1261         for (nlt = adt->nla_tracks.first; nlt; nlt = nlt->next) {
1262                 for (strip = nlt->strips.first; strip; strip = strip->next) {
1263                         rearrange_animchannel_islands(&strip->fcurves, rearrange_func, mode, ANIMTYPE_NLACURVE,
1264                                                           &anim_data_visible);
1265                 }
1266         }
1267
1268         /* free temp data */
1269         BLI_freelistN(&anim_data_visible);
1270 }
1271
1272 /* ------------------- */
1273
1274 static void rearrange_gpencil_channels(bAnimContext *ac, eRearrangeAnimChan_Mode mode)
1275 {
1276         ListBase anim_data = {NULL, NULL};
1277         bAnimListElem *ale;
1278         int filter;
1279
1280         /* get rearranging function */
1281         AnimChanRearrangeFp rearrange_func = rearrange_get_mode_func(mode);
1282
1283         if (rearrange_func == NULL)
1284                 return;
1285
1286         /* get Grease Pencil datablocks */
1287         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_ANIMDATA);
1288         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
1289
1290         for (ale = anim_data.first; ale; ale = ale->next) {
1291                 ListBase anim_data_visible = {NULL, NULL};
1292                 bGPdata *gpd = ale->data;
1293
1294                 /* only consider layers if this datablock is open */
1295                 BLI_assert(ale->type == ANIMTYPE_GPDATABLOCK);
1296                 if ((gpd->flag & GP_DATA_EXPAND) == 0)
1297                         continue;
1298
1299                 /* Filter visible data. */
1300                 rearrange_animchannels_filter_visible(&anim_data_visible, ac, ANIMTYPE_GPLAYER);
1301
1302                 /* rearrange datablock's layers */
1303                 rearrange_animchannel_islands(&gpd->layers, rearrange_func, mode, ANIMTYPE_GPLAYER, &anim_data_visible);
1304
1305                 /* free visible layers data */
1306                 BLI_freelistN(&anim_data_visible);
1307         }
1308
1309         /* free GPD channel data */
1310         ANIM_animdata_freelist(&anim_data);
1311 }
1312
1313 /* ------------------- */
1314
1315 static int animchannels_rearrange_exec(bContext *C, wmOperator *op)
1316 {
1317         bAnimContext ac;
1318         eRearrangeAnimChan_Mode mode;
1319
1320         /* get editor data */
1321         if (ANIM_animdata_get_context(C, &ac) == 0)
1322                 return OPERATOR_CANCELLED;
1323
1324         /* get mode */
1325         mode = RNA_enum_get(op->ptr, "direction");
1326
1327         /* method to move channels depends on the editor */
1328         if (ac.datatype == ANIMCONT_GPENCIL) {
1329                 /* Grease Pencil channels */
1330                 rearrange_gpencil_channels(&ac, mode);
1331         }
1332         else if (ac.datatype == ANIMCONT_MASK) {
1333                 /* Grease Pencil channels */
1334                 printf("Mask does not supported for moving yet\n");
1335         }
1336         else if (ac.datatype == ANIMCONT_ACTION) {
1337                 /* Directly rearrange action's channels */
1338                 rearrange_action_channels(&ac, ac.data, mode);
1339         }
1340         else {
1341                 ListBase anim_data = {NULL, NULL};
1342                 bAnimListElem *ale;
1343                 int filter;
1344
1345                 /* get animdata blocks */
1346                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_ANIMDATA);
1347                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1348
1349                 for (ale = anim_data.first; ale; ale = ale->next) {
1350                         AnimData *adt = ale->data;
1351
1352                         switch (ac.datatype) {
1353                                 case ANIMCONT_NLA: /* NLA-tracks only */
1354                                         rearrange_nla_channels(&ac, adt, mode);
1355                                         break;
1356
1357                                 case ANIMCONT_DRIVERS: /* Drivers list only */
1358                                         rearrange_driver_channels(&ac, adt, mode);
1359                                         break;
1360
1361                                 case ANIMCONT_ACTION: /* Single Action only... */
1362                                 case ANIMCONT_SHAPEKEY: // DOUBLE CHECK ME...
1363                                 {
1364                                         if (adt->action)
1365                                                 rearrange_action_channels(&ac, adt->action, mode);
1366                                         else if (G.debug & G_DEBUG)
1367                                                 printf("Animdata has no action\n");
1368                                         break;
1369                                 }
1370
1371                                 default: /* DopeSheet/Graph Editor - Some Actions + NLA Control Curves */
1372                                 {
1373                                         /* NLA Control Curves */
1374                                         if (adt->nla_tracks.first)
1375                                                 rearrange_nla_control_channels(&ac, adt, mode);
1376
1377                                         /* Action */
1378                                         if (adt->action)
1379                                                 rearrange_action_channels(&ac, adt->action, mode);
1380                                         else if (G.debug & G_DEBUG)
1381                                                 printf("Animdata has no action\n");
1382                                         break;
1383                                 }
1384                         }
1385                 }
1386
1387                 /* free temp data */
1388                 ANIM_animdata_freelist(&anim_data);
1389         }
1390
1391         /* send notifier that things have changed */
1392         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
1393
1394         return OPERATOR_FINISHED;
1395 }
1396
1397 static void ANIM_OT_channels_move(wmOperatorType *ot)
1398 {
1399         /* identifiers */
1400         ot->name = "Move Channels";
1401         ot->idname = "ANIM_OT_channels_move";
1402         ot->description = "Rearrange selected animation channels";
1403
1404         /* api callbacks */
1405         ot->exec = animchannels_rearrange_exec;
1406         ot->poll = animedit_poll_channels_nla_tweakmode_off;
1407
1408         /* flags */
1409         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1410
1411         /* props */
1412         ot->prop = RNA_def_enum(ot->srna, "direction", prop_animchannel_rearrange_types, REARRANGE_ANIMCHAN_DOWN, "Direction", "");
1413 }
1414
1415 /* ******************** Group Channel Operator ************************ */
1416
1417 static bool animchannels_grouping_poll(bContext *C)
1418 {
1419         ScrArea *sa = CTX_wm_area(C);
1420         SpaceLink *sl;
1421
1422         /* channels region test */
1423         /* TODO: could enhance with actually testing if channels region? */
1424         if (ELEM(NULL, sa, CTX_wm_region(C)))
1425                 return 0;
1426
1427         /* animation editor test - must be suitable modes only */
1428         sl = CTX_wm_space_data(C);
1429
1430         switch (sa->spacetype) {
1431                 /* supported... */
1432                 case SPACE_ACTION:
1433                 {
1434                         SpaceAction *saction = (SpaceAction *)sl;
1435
1436                         /* dopesheet and action only - all others are for other datatypes or have no groups */
1437                         if (ELEM(saction->mode, SACTCONT_ACTION, SACTCONT_DOPESHEET) == 0)
1438                                 return 0;
1439
1440                         break;
1441                 }
1442                 case SPACE_IPO:
1443                 {
1444                         SpaceIpo *sipo = (SpaceIpo *)sl;
1445
1446                         /* drivers can't have groups... */
1447                         if (sipo->mode != SIPO_MODE_ANIMATION)
1448                                 return 0;
1449
1450                         break;
1451                 }
1452                 /* unsupported... */
1453                 default:
1454                         return 0;
1455         }
1456
1457         return 1;
1458 }
1459
1460 /* ----------------------------------------------------------- */
1461
1462 static void animchannels_group_channels(bAnimContext *ac, bAnimListElem *adt_ref, const char name[])
1463 {
1464         AnimData *adt = adt_ref->adt;
1465         bAction *act = adt->action;
1466
1467         if (act) {
1468                 ListBase anim_data = {NULL, NULL};
1469                 int filter;
1470
1471                 /* find selected F-Curves to re-group */
1472                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL);
1473                 ANIM_animdata_filter(ac, &anim_data, filter, adt_ref, ANIMCONT_CHANNEL);
1474
1475                 if (anim_data.first) {
1476                         bActionGroup *agrp;
1477                         bAnimListElem *ale;
1478
1479                         /* create new group, which should now be part of the action */
1480                         agrp = action_groups_add_new(act, name);
1481                         BLI_assert(agrp != NULL);
1482
1483                         /* transfer selected F-Curves across to new group  */
1484                         for (ale = anim_data.first; ale; ale = ale->next) {
1485                                 FCurve *fcu = (FCurve *)ale->data;
1486                                 bActionGroup *grp = fcu->grp;
1487
1488                                 /* remove F-Curve from group, then group too if it is now empty */
1489                                 action_groups_remove_channel(act, fcu);
1490
1491                                 if ((grp) && BLI_listbase_is_empty(&grp->channels)) {
1492                                         BLI_freelinkN(&act->groups, grp);
1493                                 }
1494
1495                                 /* add F-Curve to group */
1496                                 action_groups_add_channel(act, agrp, fcu);
1497                         }
1498                 }
1499
1500                 /* cleanup */
1501                 ANIM_animdata_freelist(&anim_data);
1502         }
1503 }
1504
1505 static int animchannels_group_exec(bContext *C, wmOperator *op)
1506 {
1507         bAnimContext ac;
1508         char name[MAX_NAME];
1509
1510         /* get editor data */
1511         if (ANIM_animdata_get_context(C, &ac) == 0)
1512                 return OPERATOR_CANCELLED;
1513
1514         /* get name for new group */
1515         RNA_string_get(op->ptr, "name", name);
1516
1517         /* XXX: name for group should never be empty... */
1518         if (name[0]) {
1519                 ListBase anim_data = {NULL, NULL};
1520                 bAnimListElem *ale;
1521                 int filter;
1522
1523                 /* handle each animdata block separately, so that the regrouping doesn't flow into blocks  */
1524                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_ANIMDATA | ANIMFILTER_NODUPLIS);
1525                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1526
1527                 for (ale = anim_data.first; ale; ale = ale->next) {
1528                         animchannels_group_channels(&ac, ale, name);
1529                 }
1530
1531                 /* free temp data */
1532                 ANIM_animdata_freelist(&anim_data);
1533
1534                 /* updatss */
1535                 WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
1536         }
1537
1538         return OPERATOR_FINISHED;
1539 }
1540
1541 static void ANIM_OT_channels_group(wmOperatorType *ot)
1542 {
1543         /* identifiers */
1544         ot->name = "Group Channels";
1545         ot->idname = "ANIM_OT_channels_group";
1546         ot->description = "Add selected F-Curves to a new group";
1547
1548         /* callbacks */
1549         ot->invoke = WM_operator_props_popup;
1550         ot->exec = animchannels_group_exec;
1551         ot->poll = animchannels_grouping_poll;
1552
1553         /* flags */
1554         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1555
1556         /* props */
1557         ot->prop = RNA_def_string(ot->srna, "name", "New Group",
1558                                   sizeof(((bActionGroup *)NULL)->name),
1559                                   "Name", "Name of newly created group");
1560         /* XXX: still not too sure about this - keeping same text is confusing... */
1561         // RNA_def_property_flag(ot->prop, PROP_SKIP_SAVE);
1562 }
1563
1564 /* ----------------------------------------------------------- */
1565
1566 static int animchannels_ungroup_exec(bContext *C, wmOperator *UNUSED(op))
1567 {
1568         bAnimContext ac;
1569
1570         ListBase anim_data = {NULL, NULL};
1571         bAnimListElem *ale;
1572         int filter;
1573
1574         /* get editor data */
1575         if (ANIM_animdata_get_context(C, &ac) == 0)
1576                 return OPERATOR_CANCELLED;
1577
1578         /* just selected F-Curves... */
1579         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL | ANIMFILTER_NODUPLIS);
1580         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1581
1582         for (ale = anim_data.first; ale; ale = ale->next) {
1583                 /* find action for this F-Curve... */
1584                 if (ale->adt && ale->adt->action) {
1585                         FCurve  *fcu = (FCurve *)ale->data;
1586                         bAction *act = ale->adt->action;
1587
1588                         /* only proceed to remove if F-Curve is in a group... */
1589                         if (fcu->grp) {
1590                                 bActionGroup *agrp = fcu->grp;
1591
1592                                 /* remove F-Curve from group and add at tail (ungrouped) */
1593                                 action_groups_remove_channel(act, fcu);
1594                                 BLI_addtail(&act->curves, fcu);
1595
1596                                 /* delete group if it is now empty */
1597                                 if (BLI_listbase_is_empty(&agrp->channels)) {
1598                                         BLI_freelinkN(&act->groups, agrp);
1599                                 }
1600                         }
1601                 }
1602         }
1603
1604         /* cleanup */
1605         ANIM_animdata_freelist(&anim_data);
1606
1607         /* updates */
1608         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
1609
1610         return OPERATOR_FINISHED;
1611 }
1612
1613 static void ANIM_OT_channels_ungroup(wmOperatorType *ot)
1614 {
1615         /* identifiers */
1616         ot->name = "Ungroup Channels";
1617         ot->idname = "ANIM_OT_channels_ungroup";
1618         ot->description = "Remove selected F-Curves from their current groups";
1619
1620         /* callbacks */
1621         ot->exec = animchannels_ungroup_exec;
1622         ot->poll = animchannels_grouping_poll;
1623
1624         /* flags */
1625         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1626 }
1627
1628 /* ******************** Delete Channel Operator *********************** */
1629
1630 static int animchannels_delete_exec(bContext *C, wmOperator *UNUSED(op))
1631 {
1632         bAnimContext ac;
1633         ListBase anim_data = {NULL, NULL};
1634         bAnimListElem *ale;
1635         int filter;
1636
1637         /* get editor data */
1638         if (ANIM_animdata_get_context(C, &ac) == 0)
1639                 return OPERATOR_CANCELLED;
1640
1641         /* cannot delete in shapekey */
1642         if (ac.datatype == ANIMCONT_SHAPEKEY)
1643                 return OPERATOR_CANCELLED;
1644
1645
1646         /* do groups only first (unless in Drivers mode, where there are none) */
1647         if (ac.datatype != ANIMCONT_DRIVERS) {
1648                 /* filter data */
1649                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL | ANIMFILTER_LIST_CHANNELS | ANIMFILTER_FOREDIT | ANIMFILTER_NODUPLIS);
1650                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1651
1652                 /* delete selected groups and their associated channels */
1653                 for (ale = anim_data.first; ale; ale = ale->next) {
1654                         /* only groups - don't check other types yet, since they may no-longer exist */
1655                         if (ale->type == ANIMTYPE_GROUP) {
1656                                 bActionGroup *agrp = (bActionGroup *)ale->data;
1657                                 AnimData *adt = ale->adt;
1658                                 FCurve *fcu, *fcn;
1659
1660                                 /* skip this group if no AnimData available, as we can't safely remove the F-Curves */
1661                                 if (adt == NULL)
1662                                         continue;
1663
1664                                 /* delete all of the Group's F-Curves, but no others */
1665                                 for (fcu = agrp->channels.first; fcu && fcu->grp == agrp; fcu = fcn) {
1666                                         fcn = fcu->next;
1667
1668                                         /* remove from group and action, then free */
1669                                         action_groups_remove_channel(adt->action, fcu);
1670                                         free_fcurve(fcu);
1671                                 }
1672
1673                                 /* free the group itself */
1674                                 if (adt->action)
1675                                         BLI_freelinkN(&adt->action->groups, agrp);
1676                                 else
1677                                         MEM_freeN(agrp);
1678                         }
1679                 }
1680
1681                 /* cleanup */
1682                 ANIM_animdata_freelist(&anim_data);
1683         }
1684
1685         /* filter data */
1686         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL | ANIMFILTER_FOREDIT | ANIMFILTER_NODUPLIS);
1687         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1688
1689         /* delete selected data channels */
1690         for (ale = anim_data.first; ale; ale = ale->next) {
1691                 switch (ale->type) {
1692                         case ANIMTYPE_FCURVE:
1693                         {
1694                                 /* F-Curves if we can identify its parent */
1695                                 AnimData *adt = ale->adt;
1696                                 FCurve *fcu = (FCurve *)ale->data;
1697
1698                                 /* try to free F-Curve */
1699                                 ANIM_fcurve_delete_from_animdata(&ac, adt, fcu);
1700                                 break;
1701                         }
1702                         case ANIMTYPE_NLACURVE:
1703                         {
1704                                 /* NLA Control Curve - Deleting it should disable the corresponding setting... */
1705                                 NlaStrip *strip = (NlaStrip *)ale->owner;
1706                                 FCurve *fcu = (FCurve *)ale->data;
1707
1708                                 if (STREQ(fcu->rna_path, "strip_time")) {
1709                                         strip->flag &= ~NLASTRIP_FLAG_USR_TIME;
1710                                 }
1711                                 else if (STREQ(fcu->rna_path, "influence")) {
1712                                         strip->flag &= ~NLASTRIP_FLAG_USR_INFLUENCE;
1713                                 }
1714                                 else {
1715                                         printf("ERROR: Trying to delete NLA Control Curve for unknown property '%s'\n", fcu->rna_path);
1716                                 }
1717
1718                                 /* unlink and free the F-Curve */
1719                                 BLI_remlink(&strip->fcurves, fcu);
1720                                 free_fcurve(fcu);
1721                                 break;
1722                         }
1723                         case ANIMTYPE_GPLAYER:
1724                         {
1725                                 /* Grease Pencil layer */
1726                                 bGPdata *gpd = (bGPdata *)ale->id;
1727                                 bGPDlayer *gpl = (bGPDlayer *)ale->data;
1728
1729                                 /* try to delete the layer's data and the layer itself */
1730                                 BKE_gpencil_free_frames(gpl);
1731                                 BLI_freelinkN(&gpd->layers, gpl);
1732                                 break;
1733                         }
1734                         case ANIMTYPE_MASKLAYER:
1735                         {
1736                                 /* Mask layer */
1737                                 Mask *mask = (Mask *)ale->id;
1738                                 MaskLayer *masklay = (MaskLayer *)ale->data;
1739
1740                                 /* try to delete the layer's data and the layer itself */
1741                                 BKE_mask_layer_remove(mask, masklay);
1742                                 break;
1743                         }
1744                 }
1745         }
1746
1747         /* cleanup */
1748         ANIM_animdata_freelist(&anim_data);
1749
1750         /* send notifier that things have changed */
1751         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
1752         DAG_relations_tag_update(CTX_data_main(C));
1753
1754         return OPERATOR_FINISHED;
1755 }
1756
1757 static void ANIM_OT_channels_delete(wmOperatorType *ot)
1758 {
1759         /* identifiers */
1760         ot->name = "Delete Channels";
1761         ot->idname = "ANIM_OT_channels_delete";
1762         ot->description = "Delete all selected animation channels";
1763
1764         /* api callbacks */
1765         ot->exec = animchannels_delete_exec;
1766         ot->poll = animedit_poll_channels_active;
1767
1768         /* flags */
1769         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1770 }
1771
1772 /* ********************** Set Flags Operator *********************** */
1773
1774 /* defines for setting animation-channel flags */
1775 static const EnumPropertyItem prop_animchannel_setflag_types[] = {
1776         {ACHANNEL_SETFLAG_TOGGLE, "TOGGLE", 0, "Toggle", ""},
1777         {ACHANNEL_SETFLAG_CLEAR, "DISABLE", 0, "Disable", ""},
1778         {ACHANNEL_SETFLAG_ADD, "ENABLE", 0, "Enable", ""},
1779         {ACHANNEL_SETFLAG_INVERT, "INVERT", 0, "Invert", ""},
1780         {0, NULL, 0, NULL, NULL}
1781 };
1782
1783 /* defines for set animation-channel settings */
1784 // TODO: could add some more types, but those are really quite dependent on the mode...
1785 static const EnumPropertyItem prop_animchannel_settings_types[] = {
1786         {ACHANNEL_SETTING_PROTECT, "PROTECT", 0, "Protect", ""},
1787         {ACHANNEL_SETTING_MUTE, "MUTE", 0, "Mute", ""},
1788         {0, NULL, 0, NULL, NULL}
1789 };
1790
1791
1792 /* ------------------- */
1793
1794 /* Set/clear a particular flag (setting) for all selected + visible channels
1795  * setting: the setting to modify
1796  * mode: eAnimChannels_SetFlag
1797  * onlysel: only selected channels get the flag set
1798  */
1799 // TODO: enable a setting which turns flushing on/off?
1800 static void setflag_anim_channels(bAnimContext *ac, eAnimChannel_Settings setting, eAnimChannels_SetFlag mode, bool onlysel, bool flush)
1801 {
1802         ListBase anim_data = {NULL, NULL};
1803         ListBase all_data = {NULL, NULL};
1804         bAnimListElem *ale;
1805         int filter;
1806
1807         /* filter data that we need if flush is on */
1808         if (flush) {
1809                 /* get list of all channels that selection may need to be flushed to
1810                  * - hierarchy visibility needs to be ignored so that settings can get flushed
1811                  *   "down" inside closed containers
1812                  */
1813                 filter = ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_CHANNELS;
1814                 ANIM_animdata_filter(ac, &all_data, filter, ac->data, ac->datatype);
1815         }
1816
1817         /* filter data that we're working on
1818          * - hierarchy matters if we're doing this from the channels region
1819          *   since we only want to apply this to channels we can "see",
1820          *   and have these affect their relatives
1821          * - but for Graph Editor, this gets used also from main region
1822          *   where hierarchy doesn't apply [#21276]
1823          */
1824         if ((ac->spacetype == SPACE_IPO) && (ac->regiontype != RGN_TYPE_CHANNELS)) {
1825                 /* graph editor (case 2) */
1826                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_CHANNELS | ANIMFILTER_CURVE_VISIBLE | ANIMFILTER_NODUPLIS);
1827         }
1828         else {
1829                 /* standard case */
1830                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS | ANIMFILTER_NODUPLIS);
1831         }
1832         if (onlysel) filter |= ANIMFILTER_SEL;
1833         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
1834
1835         /* if toggling, check if disable or enable */
1836         if (mode == ACHANNEL_SETFLAG_TOGGLE) {
1837                 /* default to turn all on, unless we encounter one that's on... */
1838                 mode = ACHANNEL_SETFLAG_ADD;
1839
1840                 /* see if we should turn off instead... */
1841                 for (ale = anim_data.first; ale; ale = ale->next) {
1842                         /* set the setting in the appropriate way (if available) */
1843                         if (ANIM_channel_setting_get(ac, ale, setting) > 0) {
1844                                 mode = ACHANNEL_SETFLAG_CLEAR;
1845                                 break;
1846                         }
1847                 }
1848         }
1849
1850         /* apply the setting */
1851         for (ale = anim_data.first; ale; ale = ale->next) {
1852                 /* skip channel if setting is not available */
1853                 if (ANIM_channel_setting_get(ac, ale, setting) == -1)
1854                         continue;
1855
1856                 /* set the setting in the appropriate way */
1857                 ANIM_channel_setting_set(ac, ale, setting, mode);
1858
1859                 /* if flush status... */
1860                 if (flush)
1861                         ANIM_flush_setting_anim_channels(ac, &all_data, ale, setting, mode);
1862         }
1863
1864         ANIM_animdata_freelist(&anim_data);
1865         BLI_freelistN(&all_data);
1866 }
1867
1868 /* ------------------- */
1869
1870 static int animchannels_setflag_exec(bContext *C, wmOperator *op)
1871 {
1872         bAnimContext ac;
1873         eAnimChannel_Settings setting;
1874         eAnimChannels_SetFlag mode;
1875         bool flush = true;
1876
1877         /* get editor data */
1878         if (ANIM_animdata_get_context(C, &ac) == 0)
1879                 return OPERATOR_CANCELLED;
1880
1881         /* mode (eAnimChannels_SetFlag), setting (eAnimChannel_Settings) */
1882         mode = RNA_enum_get(op->ptr, "mode");
1883         setting = RNA_enum_get(op->ptr, "type");
1884
1885         /* check if setting is flushable */
1886         if (setting == ACHANNEL_SETTING_EXPAND)
1887                 flush = false;
1888
1889         /* modify setting
1890          * - only selected channels are affected
1891          */
1892         setflag_anim_channels(&ac, setting, mode, true, flush);
1893
1894         /* send notifier that things have changed */
1895         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
1896
1897         return OPERATOR_FINISHED;
1898 }
1899
1900 /* duplicate of 'ANIM_OT_channels_setting_toggle' for menu title only, weak! */
1901 static void ANIM_OT_channels_setting_enable(wmOperatorType *ot)
1902 {
1903         PropertyRNA *prop;
1904
1905         /* identifiers */
1906         ot->name = "Enable Channel Setting";
1907         ot->idname = "ANIM_OT_channels_setting_enable";
1908         ot->description = "Enable specified setting on all selected animation channels";
1909
1910         /* api callbacks */
1911         ot->invoke = WM_menu_invoke;
1912         ot->exec = animchannels_setflag_exec;
1913         ot->poll = animedit_poll_channels_active;
1914
1915         /* flags */
1916         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1917
1918         /* props */
1919         /* flag-setting mode */
1920         prop = RNA_def_enum(ot->srna, "mode", prop_animchannel_setflag_types, ACHANNEL_SETFLAG_ADD, "Mode", "");
1921         RNA_def_property_flag(prop, PROP_HIDDEN);
1922         /* setting to set */
1923         ot->prop = RNA_def_enum(ot->srna, "type", prop_animchannel_settings_types, 0, "Type", "");
1924 }
1925 /* duplicate of 'ANIM_OT_channels_setting_toggle' for menu title only, weak! */
1926 static void ANIM_OT_channels_setting_disable(wmOperatorType *ot)
1927 {
1928         PropertyRNA *prop;
1929
1930         /* identifiers */
1931         ot->name = "Disable Channel Setting";
1932         ot->idname = "ANIM_OT_channels_setting_disable";
1933         ot->description = "Disable specified setting on all selected animation channels";
1934
1935         /* api callbacks */
1936         ot->invoke = WM_menu_invoke;
1937         ot->exec = animchannels_setflag_exec;
1938         ot->poll = animedit_poll_channels_active;
1939
1940         /* flags */
1941         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1942
1943         /* props */
1944         /* flag-setting mode */
1945         prop = RNA_def_enum(ot->srna, "mode", prop_animchannel_setflag_types, ACHANNEL_SETFLAG_CLEAR, "Mode", "");
1946         RNA_def_property_flag(prop, PROP_HIDDEN); /* internal hack - don't expose */
1947         /* setting to set */
1948         ot->prop = RNA_def_enum(ot->srna, "type", prop_animchannel_settings_types, 0, "Type", "");
1949 }
1950
1951 static void ANIM_OT_channels_setting_toggle(wmOperatorType *ot)
1952 {
1953         PropertyRNA *prop;
1954
1955         /* identifiers */
1956         ot->name = "Toggle Channel Setting";
1957         ot->idname = "ANIM_OT_channels_setting_toggle";
1958         ot->description = "Toggle specified setting on all selected animation channels";
1959
1960         /* api callbacks */
1961         ot->invoke = WM_menu_invoke;
1962         ot->exec = animchannels_setflag_exec;
1963         ot->poll = animedit_poll_channels_active;
1964
1965         /* flags */
1966         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1967
1968         /* props */
1969         /* flag-setting mode */
1970         prop = RNA_def_enum(ot->srna, "mode", prop_animchannel_setflag_types, ACHANNEL_SETFLAG_TOGGLE, "Mode", "");
1971         RNA_def_property_flag(prop, PROP_HIDDEN); /* internal hack - don't expose */
1972         /* setting to set */
1973         ot->prop = RNA_def_enum(ot->srna, "type", prop_animchannel_settings_types, 0, "Type", "");
1974 }
1975
1976 static void ANIM_OT_channels_editable_toggle(wmOperatorType *ot)
1977 {
1978         PropertyRNA *prop;
1979
1980         /* identifiers */
1981         ot->name = "Toggle Channel Editability";
1982         ot->idname = "ANIM_OT_channels_editable_toggle";
1983         ot->description = "Toggle editability of selected channels";
1984
1985         /* api callbacks */
1986         ot->exec = animchannels_setflag_exec;
1987         ot->poll = animedit_poll_channels_active;
1988
1989         /* flags */
1990         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1991
1992         /* props */
1993         /* flag-setting mode */
1994         RNA_def_enum(ot->srna, "mode", prop_animchannel_setflag_types, ACHANNEL_SETFLAG_TOGGLE, "Mode", "");
1995         /* setting to set */
1996         prop = RNA_def_enum(ot->srna, "type", prop_animchannel_settings_types, ACHANNEL_SETTING_PROTECT, "Type", "");
1997         RNA_def_property_flag(prop, PROP_HIDDEN); /* internal hack - don't expose */
1998 }
1999
2000 /* ********************** Expand Channels Operator *********************** */
2001
2002 static int animchannels_expand_exec(bContext *C, wmOperator *op)
2003 {
2004         bAnimContext ac;
2005         bool onlysel = true;
2006
2007         /* get editor data */
2008         if (ANIM_animdata_get_context(C, &ac) == 0)
2009                 return OPERATOR_CANCELLED;
2010
2011         /* only affect selected channels? */
2012         if (RNA_boolean_get(op->ptr, "all"))
2013                 onlysel = false;
2014
2015         /* modify setting */
2016         setflag_anim_channels(&ac, ACHANNEL_SETTING_EXPAND, ACHANNEL_SETFLAG_ADD, onlysel, false);
2017
2018         /* send notifier that things have changed */
2019         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
2020
2021         return OPERATOR_FINISHED;
2022 }
2023
2024 static void ANIM_OT_channels_expand(wmOperatorType *ot)
2025 {
2026         /* identifiers */
2027         ot->name = "Expand Channels";
2028         ot->idname = "ANIM_OT_channels_expand";
2029         ot->description = "Expand (i.e. open) all selected expandable animation channels";
2030
2031         /* api callbacks */
2032         ot->exec = animchannels_expand_exec;
2033         ot->poll = animedit_poll_channels_active;
2034
2035         /* flags */
2036         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2037
2038         /* props */
2039         ot->prop = RNA_def_boolean(ot->srna, "all", 1, "All", "Expand all channels (not just selected ones)");
2040 }
2041
2042 /* ********************** Collapse Channels Operator *********************** */
2043
2044 static int animchannels_collapse_exec(bContext *C, wmOperator *op)
2045 {
2046         bAnimContext ac;
2047         bool onlysel = true;
2048
2049         /* get editor data */
2050         if (ANIM_animdata_get_context(C, &ac) == 0)
2051                 return OPERATOR_CANCELLED;
2052
2053         /* only affect selected channels? */
2054         if (RNA_boolean_get(op->ptr, "all"))
2055                 onlysel = false;
2056
2057         /* modify setting */
2058         setflag_anim_channels(&ac, ACHANNEL_SETTING_EXPAND, ACHANNEL_SETFLAG_CLEAR, onlysel, false);
2059
2060         /* send notifier that things have changed */
2061         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
2062
2063         return OPERATOR_FINISHED;
2064 }
2065
2066 static void ANIM_OT_channels_collapse(wmOperatorType *ot)
2067 {
2068         /* identifiers */
2069         ot->name = "Collapse Channels";
2070         ot->idname = "ANIM_OT_channels_collapse";
2071         ot->description = "Collapse (i.e. close) all selected expandable animation channels";
2072
2073         /* api callbacks */
2074         ot->exec = animchannels_collapse_exec;
2075         ot->poll = animedit_poll_channels_active;
2076
2077         /* flags */
2078         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2079
2080         /* props */
2081         ot->prop = RNA_def_boolean(ot->srna, "all", true, "All", "Collapse all channels (not just selected ones)");
2082 }
2083
2084 /* ************ Remove All "Empty" AnimData Blocks Operator ********* */
2085 /* We define "empty" AnimData blocks here as those which have all 3 of criteria:
2086  *  1) No active action OR that active actions are empty
2087  *     Assuming that all legitimate entries will have an action,
2088  *     and that empty actions
2089  *  2) No NLA Tracks + NLA Strips
2090  *     Assuming that users haven't set up any of these as "placeholders"
2091  *     for convenience sake, and that most that exist were either unintentional
2092  *     or are no longer wanted
2093  *  3) No drivers
2094  */
2095
2096 static int animchannels_clean_empty_exec(bContext *C, wmOperator *UNUSED(op))
2097 {
2098         bAnimContext ac;
2099
2100         ListBase anim_data = {NULL, NULL};
2101         bAnimListElem *ale;
2102         int filter;
2103
2104         /* get editor data */
2105         if (ANIM_animdata_get_context(C, &ac) == 0)
2106                 return OPERATOR_CANCELLED;
2107
2108         /* get animdata blocks */
2109         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_ANIMDATA);
2110         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
2111
2112         for (ale = anim_data.first; ale; ale = ale->next) {
2113                 ID *id = ale->id;
2114                 AnimData *adt = ale->data;
2115
2116                 bool action_empty  = false;
2117                 bool nla_empty     = false;
2118                 bool drivers_empty = false;
2119
2120                 /* sanity checks */
2121                 BLI_assert((id != NULL) && (adt != NULL));
2122
2123                 /* check if this is "empty" and can be deleted */
2124                 /* (For now, there are only these 3 criteria) */
2125
2126                 /* 1) Active Action is missing or empty */
2127                 if (ELEM(NULL, adt->action, adt->action->curves.first)) {
2128                         action_empty = true;
2129                 }
2130                 else {
2131                         /* TODO: check for keyframe + fmodifier data on these too */
2132                 }
2133
2134                 /* 2) No NLA Tracks and/or NLA Strips */
2135                 if (adt->nla_tracks.first == NULL) {
2136                         nla_empty = true;
2137                 }
2138                 else {
2139                         NlaTrack *nlt;
2140
2141                         /* empty tracks? */
2142                         for (nlt = adt->nla_tracks.first; nlt; nlt = nlt->next) {
2143                                 if (nlt->strips.first) {
2144                                         /* stop searching, as we found one that actually had stuff we don't want lost
2145                                          * NOTE: nla_empty gets reset to false, as a previous track may have been empty
2146                                          */
2147                                         nla_empty = false;
2148                                         break;
2149                                 }
2150                                 else if (nlt->strips.first == NULL) {
2151                                         /* this track is empty, but another one may still have stuff in it, so can't break yet */
2152                                         nla_empty = true;
2153                                 }
2154                         }
2155                 }
2156
2157                 /* 3) Drivers */
2158                 drivers_empty = (adt->drivers.first == NULL);
2159
2160
2161                 /* remove AnimData? */
2162                 if (action_empty && nla_empty && drivers_empty) {
2163                         BKE_animdata_free(id, true);
2164                 }
2165         }
2166
2167         /* free temp data */
2168         ANIM_animdata_freelist(&anim_data);
2169
2170         /* send notifier that things have changed */
2171         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
2172
2173         return OPERATOR_FINISHED;
2174 }
2175
2176 static void ANIM_OT_channels_clean_empty(wmOperatorType *ot)
2177 {
2178         /* identifiers */
2179         ot->name = "Remove Empty Animation Data";
2180         ot->idname = "ANIM_OT_channels_clean_empty";
2181         ot->description = "Delete all empty animation data containers from visible data-blocks";
2182
2183         /* api callbacks */
2184         ot->exec = animchannels_clean_empty_exec;
2185         ot->poll = animedit_poll_channels_nla_tweakmode_off;
2186
2187         /* flags */
2188         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2189 }
2190
2191 /* ******************* Reenable Disabled Operator ******************* */
2192
2193 static bool animchannels_enable_poll(bContext *C)
2194 {
2195         ScrArea *sa = CTX_wm_area(C);
2196
2197         /* channels region test */
2198         /* TODO: could enhance with actually testing if channels region? */
2199         if (ELEM(NULL, sa, CTX_wm_region(C)))
2200                 return 0;
2201
2202         /* animation editor test - Action/Dopesheet/etc. and Graph only */
2203         if (ELEM(sa->spacetype, SPACE_ACTION, SPACE_IPO) == 0)
2204                 return 0;
2205
2206         return 1;
2207 }
2208
2209 static int animchannels_enable_exec(bContext *C, wmOperator *UNUSED(op))
2210 {
2211         bAnimContext ac;
2212
2213         ListBase anim_data = {NULL, NULL};
2214         bAnimListElem *ale;
2215         int filter;
2216
2217         /* get editor data */
2218         if (ANIM_animdata_get_context(C, &ac) == 0)
2219                 return OPERATOR_CANCELLED;
2220
2221         /* filter data */
2222         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_NODUPLIS);
2223         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
2224
2225         /* loop through filtered data and clean curves */
2226         for (ale = anim_data.first; ale; ale = ale->next) {
2227                 FCurve *fcu = (FCurve *)ale->data;
2228
2229                 /* remove disabled flags from F-Curves */
2230                 fcu->flag &= ~FCURVE_DISABLED;
2231
2232                 /* for drivers, let's do the same too */
2233                 if (fcu->driver)
2234                         fcu->driver->flag &= ~DRIVER_FLAG_INVALID;
2235
2236                 /* tag everything for updates - in particular, this is needed to get drivers working again */
2237                 ale->update |= ANIM_UPDATE_DEPS;
2238         }
2239
2240         ANIM_animdata_update(&ac, &anim_data);
2241         ANIM_animdata_freelist(&anim_data);
2242
2243         /* send notifier that things have changed */
2244         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
2245
2246         return OPERATOR_FINISHED;
2247 }
2248
2249 static void ANIM_OT_channels_fcurves_enable(wmOperatorType *ot)
2250 {
2251         /* identifiers */
2252         ot->name = "Revive Disabled F-Curves";
2253         ot->idname = "ANIM_OT_channels_fcurves_enable";
2254         ot->description = "Clears 'disabled' tag from all F-Curves to get broken F-Curves working again";
2255
2256         /* api callbacks */
2257         ot->exec = animchannels_enable_exec;
2258         ot->poll = animchannels_enable_poll;
2259
2260         /* flags */
2261         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2262 }
2263
2264 /* ****************** Find / Set Filter Operator ******************** */
2265
2266 /* XXX: make this generic? */
2267 static bool animchannels_find_poll(bContext *C)
2268 {
2269         ScrArea *sa = CTX_wm_area(C);
2270
2271         if (sa == NULL)
2272                 return 0;
2273
2274         /* animation editor with dopesheet */
2275         return ELEM(sa->spacetype, SPACE_ACTION, SPACE_IPO, SPACE_NLA);
2276 }
2277
2278 /* find_invoke() - Get initial channels */
2279 static int animchannels_find_invoke(bContext *C, wmOperator *op, const wmEvent *evt)
2280 {
2281         bAnimContext ac;
2282
2283         /* get editor data */
2284         if (ANIM_animdata_get_context(C, &ac) == 0)
2285                 return OPERATOR_CANCELLED;
2286
2287         /* set initial filter text, and enable filter */
2288         RNA_string_set(op->ptr, "query", ac.ads->searchstr);
2289
2290         /* defer to popup */
2291         return WM_operator_props_popup(C, op, evt);
2292 }
2293
2294 /* find_exec() -  Called to set the value */
2295 static int animchannels_find_exec(bContext *C, wmOperator *op)
2296 {
2297         bAnimContext ac;
2298
2299         /* get editor data */
2300         if (ANIM_animdata_get_context(C, &ac) == 0)
2301                 return OPERATOR_CANCELLED;
2302
2303         /* update filter text, and ensure that filter is enabled if there's something there
2304          * NOTE: we turn the filter off if there's nothing (this is a quick shortcut for dismissing)
2305          */
2306         RNA_string_get(op->ptr, "query", ac.ads->searchstr);
2307
2308         if (ac.ads->searchstr[0]) {
2309                 ac.ads->filterflag |= ADS_FILTER_BY_FCU_NAME;
2310         }
2311         else {
2312                 ac.ads->filterflag &= ~ADS_FILTER_BY_FCU_NAME;
2313         }
2314
2315         /* redraw */
2316         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
2317
2318         return OPERATOR_FINISHED;
2319 }
2320
2321 static void ANIM_OT_channels_find(wmOperatorType *ot)
2322 {
2323         /* identifiers */
2324         ot->name = "Find Channels";
2325         ot->idname = "ANIM_OT_channels_find";
2326         ot->description = "Filter the set of channels shown to only include those with matching names";
2327
2328         /* callbacks */
2329         ot->invoke = animchannels_find_invoke;
2330         ot->exec = animchannels_find_exec;
2331         ot->poll = animchannels_find_poll;
2332
2333         /* flags */
2334         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2335
2336         /* properties */
2337         ot->prop = RNA_def_string(ot->srna, "query", "Query", sizeof(((bDopeSheet *)NULL)->searchstr), "", "Text to search for in channel names");
2338 }
2339
2340 /* ********************** Select All Operator *********************** */
2341
2342 static int animchannels_deselectall_exec(bContext *C, wmOperator *op)
2343 {
2344         bAnimContext ac;
2345
2346         /* get editor data */
2347         if (ANIM_animdata_get_context(C, &ac) == 0)
2348                 return OPERATOR_CANCELLED;
2349
2350         /* 'standard' behavior - check if selected, then apply relevant selection */
2351         if (RNA_boolean_get(op->ptr, "invert"))
2352                 ANIM_deselect_anim_channels(&ac, ac.data, ac.datatype, false, ACHANNEL_SETFLAG_INVERT);
2353         else
2354                 ANIM_deselect_anim_channels(&ac, ac.data, ac.datatype, true, ACHANNEL_SETFLAG_ADD);
2355
2356         /* send notifier that things have changed */
2357         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_SELECTED, NULL);
2358
2359         return OPERATOR_FINISHED;
2360 }
2361
2362 static void ANIM_OT_channels_select_all_toggle(wmOperatorType *ot)
2363 {
2364         /* identifiers */
2365         ot->name = "Select All";
2366         ot->idname = "ANIM_OT_channels_select_all_toggle";
2367         ot->description = "Toggle selection of all animation channels";
2368
2369         /* api callbacks */
2370         ot->exec = animchannels_deselectall_exec;
2371         ot->poll = animedit_poll_channels_nla_tweakmode_off;
2372
2373         /* flags */
2374         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2375
2376         /* props */
2377         ot->prop = RNA_def_boolean(ot->srna, "invert", false, "Invert", "");
2378 }
2379
2380 /* ******************** Borderselect Operator *********************** */
2381
2382 static void borderselect_anim_channels(bAnimContext *ac, rcti *rect, short selectmode)
2383 {
2384         ListBase anim_data = {NULL, NULL};
2385         bAnimListElem *ale;
2386         int filter;
2387
2388         SpaceNla *snla = (SpaceNla *)ac->sl;
2389         View2D *v2d = &ac->ar->v2d;
2390         rctf rectf;
2391         float ymin, ymax;
2392
2393         /* set initial y extents */
2394         if (ac->datatype == ANIMCONT_NLA) {
2395                 ymin = (float)(-NLACHANNEL_HEIGHT(snla));
2396                 ymax = 0.0f;
2397         }
2398         else {
2399                 ymin = 0.0f;
2400                 ymax = (float)(-ACHANNEL_HEIGHT(ac));
2401         }
2402
2403         /* convert border-region to view coordinates */
2404         UI_view2d_region_to_view(v2d, rect->xmin, rect->ymin + 2, &rectf.xmin, &rectf.ymin);
2405         UI_view2d_region_to_view(v2d, rect->xmax, rect->ymax - 2, &rectf.xmax, &rectf.ymax);
2406
2407         /* filter data */
2408         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
2409         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
2410
2411         /* loop over data, doing border select */
2412         for (ale = anim_data.first; ale; ale = ale->next) {
2413                 if (ac->datatype == ANIMCONT_NLA)
2414                         ymin = ymax - NLACHANNEL_STEP(snla);
2415                 else
2416                         ymin = ymax - ACHANNEL_STEP(ac);
2417
2418                 /* if channel is within border-select region, alter it */
2419                 if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))) {
2420                         /* set selection flags only */
2421                         ANIM_channel_setting_set(ac, ale, ACHANNEL_SETTING_SELECT, selectmode);
2422
2423                         /* type specific actions */
2424                         switch (ale->type) {
2425                                 case ANIMTYPE_GROUP:
2426                                 {
2427                                         bActionGroup *agrp = (bActionGroup *)ale->data;
2428                                         select_pchan_for_action_group(ac, agrp, ale);
2429                                         /* always clear active flag after doing this */
2430                                         agrp->flag &= ~AGRP_ACTIVE;
2431                                         break;
2432                                 }
2433                                 case ANIMTYPE_NLATRACK:
2434                                 {
2435                                         NlaTrack *nlt = (NlaTrack *)ale->data;
2436
2437                                         /* for now, it's easier just to do this here manually, as defining a new type
2438                                          * currently adds complications when doing other stuff
2439                                          */
2440                                         ACHANNEL_SET_FLAG(nlt, selectmode, NLATRACK_SELECTED);
2441                                         break;
2442                                 }
2443                         }
2444                 }
2445
2446                 /* set minimum extent to be the maximum of the next channel */
2447                 ymax = ymin;
2448         }
2449
2450         /* cleanup */
2451         ANIM_animdata_freelist(&anim_data);
2452 }
2453
2454 /* ------------------- */
2455
2456 static int animchannels_borderselect_exec(bContext *C, wmOperator *op)
2457 {
2458         bAnimContext ac;
2459         rcti rect;
2460         short selectmode = 0;
2461         const bool select = !RNA_boolean_get(op->ptr, "deselect");
2462         const bool extend = RNA_boolean_get(op->ptr, "extend");
2463
2464         /* get editor data */
2465         if (ANIM_animdata_get_context(C, &ac) == 0)
2466                 return OPERATOR_CANCELLED;
2467
2468         /* get settings from operator */
2469         WM_operator_properties_border_to_rcti(op, &rect);
2470
2471         if (!extend) {
2472                 ANIM_deselect_anim_channels(&ac, ac.data, ac.datatype, true, ACHANNEL_SETFLAG_CLEAR);
2473         }
2474
2475         if (select) {
2476                 selectmode = ACHANNEL_SETFLAG_ADD;
2477         }
2478         else {
2479                 selectmode = ACHANNEL_SETFLAG_CLEAR;
2480         }
2481
2482         /* apply borderselect animation channels */
2483         borderselect_anim_channels(&ac, &rect, selectmode);
2484
2485         /* send notifier that things have changed */
2486         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_SELECTED, NULL);
2487
2488         return OPERATOR_FINISHED;
2489 }
2490
2491 static void ANIM_OT_channels_select_border(wmOperatorType *ot)
2492 {
2493         /* identifiers */
2494         ot->name = "Border Select";
2495         ot->idname = "ANIM_OT_channels_select_border";
2496         ot->description = "Select all animation channels within the specified region";
2497
2498         /* api callbacks */
2499         ot->invoke = WM_gesture_border_invoke;
2500         ot->exec = animchannels_borderselect_exec;
2501         ot->modal = WM_gesture_border_modal;
2502         ot->cancel = WM_gesture_border_cancel;
2503
2504         ot->poll = animedit_poll_channels_nla_tweakmode_off;
2505
2506         /* flags */
2507         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2508
2509         /* rna */
2510         WM_operator_properties_gesture_border_select(ot);
2511 }
2512
2513 /* ******************* Rename Operator ***************************** */
2514 /* Allow renaming some channels by clicking on them */
2515
2516 static bool rename_anim_channels(bAnimContext *ac, int channel_index)
2517 {
2518         ListBase anim_data = {NULL, NULL};
2519         const bAnimChannelType *acf;
2520         bAnimListElem *ale;
2521         int filter;
2522         bool success = false;
2523
2524         /* get the channel that was clicked on */
2525         /* filter channels */
2526         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
2527         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
2528
2529         /* get channel from index */
2530         ale = BLI_findlink(&anim_data, channel_index);
2531         if (ale == NULL) {
2532                 /* channel not found */
2533                 if (G.debug & G_DEBUG)
2534                         printf("Error: animation channel (index = %d) not found in rename_anim_channels()\n", channel_index);
2535
2536                 ANIM_animdata_freelist(&anim_data);
2537                 return false;
2538         }
2539
2540         /* check that channel can be renamed */
2541         acf = ANIM_channel_get_typeinfo(ale);
2542         if (acf && acf->name_prop) {
2543                 PointerRNA ptr;
2544                 PropertyRNA *prop;
2545
2546                 /* ok if we can get name property to edit from this channel */
2547                 if (acf->name_prop(ale, &ptr, &prop)) {
2548                         /* actually showing the rename textfield is done on redraw,
2549                          * so here we just store the index of this channel in the
2550                          * dopesheet data, which will get utilized when drawing the
2551                          * channel...
2552                          *
2553                          * +1 factor is for backwards compat issues
2554                          */
2555                         if (ac->ads) {
2556                                 ac->ads->renameIndex = channel_index + 1;
2557                                 success = true;
2558                         }
2559                 }
2560         }
2561
2562         /* free temp data and tag for refresh */
2563         ANIM_animdata_freelist(&anim_data);
2564         ED_region_tag_redraw(ac->ar);
2565         return success;
2566 }
2567
2568 static int animchannels_channel_get(bAnimContext *ac, const int mval[2])
2569 {
2570         ARegion *ar;
2571         View2D *v2d;
2572         int channel_index;
2573         float x, y;
2574
2575         /* get useful pointers from animation context data */
2576         ar = ac->ar;
2577         v2d = &ar->v2d;
2578
2579         /* figure out which channel user clicked in
2580          * Note: although channels technically start at (y = ACHANNEL_FIRST), we need to adjust by half a channel's height
2581          *       so that the tops of channels get caught ok. Since ACHANNEL_FIRST is really ACHANNEL_HEIGHT, we simply use
2582          *       ACHANNEL_HEIGHT_HALF.
2583          */
2584         UI_view2d_region_to_view(v2d, mval[0], mval[1], &x, &y);
2585
2586         if (ac->datatype == ANIMCONT_NLA) {
2587                 SpaceNla *snla = (SpaceNla *)ac->sl;
2588                 UI_view2d_listview_view_to_cell(v2d, NLACHANNEL_NAMEWIDTH, NLACHANNEL_STEP(snla), 0, (float)NLACHANNEL_HEIGHT_HALF(snla), x, y, NULL, &channel_index);
2589         }
2590         else {
2591                 UI_view2d_listview_view_to_cell(v2d, ACHANNEL_NAMEWIDTH, ACHANNEL_STEP(ac), 0, (float)ACHANNEL_HEIGHT_HALF(ac), x, y, NULL, &channel_index);
2592         }
2593
2594         return channel_index;
2595 }
2596
2597 static int animchannels_rename_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
2598 {
2599         bAnimContext ac;
2600         int channel_index;
2601
2602         /* get editor data */
2603         if (ANIM_animdata_get_context(C, &ac) == 0)
2604                 return OPERATOR_CANCELLED;
2605
2606         channel_index = animchannels_channel_get(&ac, event->mval);
2607
2608         /* handle click */
2609         if (rename_anim_channels(&ac, channel_index))
2610                 return OPERATOR_FINISHED;
2611         else
2612                 /* allow event to be handled by selectall operator */
2613                 return OPERATOR_PASS_THROUGH;
2614 }
2615
2616 static void ANIM_OT_channels_rename(wmOperatorType *ot)
2617 {
2618         /* identifiers */
2619         ot->name = "Rename Channels";
2620         ot->idname = "ANIM_OT_channels_rename";
2621         ot->description = "Rename animation channel under mouse";
2622
2623         /* api callbacks */
2624         ot->invoke = animchannels_rename_invoke;
2625         ot->poll = animedit_poll_channels_active;
2626 }
2627
2628 /* ******************** Mouse-Click Operator *********************** */
2629 /* Handle selection changes due to clicking on channels. Settings will get caught by UI code... */
2630
2631 static int mouse_anim_channels(bContext *C, bAnimContext *ac, int channel_index, short selectmode)
2632 {
2633         ListBase anim_data = {NULL, NULL};
2634         bAnimListElem *ale;
2635         int filter;
2636         int notifierFlags = 0;
2637
2638         /* get the channel that was clicked on */
2639         /* filter channels */
2640         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
2641         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
2642
2643         /* get channel from index */
2644         ale = BLI_findlink(&anim_data, channel_index);
2645         if (ale == NULL) {
2646                 /* channel not found */
2647                 if (G.debug & G_DEBUG)
2648                         printf("Error: animation channel (index = %d) not found in mouse_anim_channels()\n", channel_index);
2649
2650                 ANIM_animdata_freelist(&anim_data);
2651                 return 0;
2652         }
2653
2654         /* selectmode -1 is a special case for ActionGroups only, which selects all of the channels underneath it only... */
2655         /* TODO: should this feature be extended to work with other channel types too? */
2656         if ((selectmode == -1) && (ale->type != ANIMTYPE_GROUP)) {
2657                 /* normal channels should not behave normally in this case */
2658                 ANIM_animdata_freelist(&anim_data);
2659                 return 0;
2660         }
2661
2662         /* action to take depends on what channel we've got */
2663         /* WARNING: must keep this in sync with the equivalent function in nla_channels.c */
2664         switch (ale->type) {
2665                 case ANIMTYPE_SCENE:
2666                 {
2667                         Scene *sce = (Scene *)ale->data;
2668                         AnimData *adt = sce->adt;
2669
2670                         /* set selection status */
2671                         if (selectmode == SELECT_INVERT) {
2672                                 /* swap select */
2673                                 sce->flag ^= SCE_DS_SELECTED;
2674                                 if (adt) adt->flag ^= ADT_UI_SELECTED;
2675                         }
2676                         else {
2677                                 sce->flag |= SCE_DS_SELECTED;
2678                                 if (adt) adt->flag |= ADT_UI_SELECTED;
2679                         }
2680
2681                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
2682                         break;
2683                 }
2684                 case ANIMTYPE_OBJECT:
2685                 {
2686                         bDopeSheet *ads = (bDopeSheet *)ac->data;
2687                         Scene *sce = (Scene *)ads->source;
2688                         Base *base = (Base *)ale->data;
2689                         Object *ob = base->object;
2690                         AnimData *adt = ob->adt;
2691
2692                         /* set selection status */
2693                         if ((ob->restrictflag & OB_RESTRICT_SELECT) == 0) {
2694                                 if (selectmode == SELECT_INVERT) {
2695                                         /* swap select */
2696                                         base->flag ^= SELECT;
2697                                         ob->flag = base->flag;
2698
2699                                         if (adt) adt->flag ^= ADT_UI_SELECTED;
2700                                 }
2701                                 else {
2702                                         Base *b;
2703
2704                                         /* deselect all */
2705                                         /* TODO: should this deselect all other types of channels too? */
2706                                         for (b = sce->base.first; b; b = b->next) {
2707                                                 b->flag &= ~SELECT;
2708                                                 b->object->flag = b->flag;
2709                                                 if (b->object->adt) b->object->adt->flag &= ~(ADT_UI_SELECTED | ADT_UI_ACTIVE);
2710                                         }
2711
2712                                         /* select object now */
2713                                         base->flag |= SELECT;
2714                                         ob->flag |= SELECT;
2715                                         if (adt) adt->flag |= ADT_UI_SELECTED;
2716                                 }
2717
2718                                 /* change active object - regardless of whether it is now selected [T37883] */
2719                                 ED_base_object_activate(C, base); /* adds notifier */
2720
2721                                 if ((adt) && (adt->flag & ADT_UI_SELECTED))
2722                                         adt->flag |= ADT_UI_ACTIVE;
2723
2724                                 /* ensure we exit editmode on whatever object was active before to avoid getting stuck there - T48747 */
2725                                 if (ob != sce->obedit) {
2726                                         ED_object_editmode_exit(C, EM_FREEDATA | EM_WAITCURSOR);
2727                                 }
2728
2729                                 notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
2730                         }
2731                         break;
2732                 }
2733                 case ANIMTYPE_FILLACTD: /* Action Expander */
2734                 case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
2735                 case ANIMTYPE_DSLAM:
2736                 case ANIMTYPE_DSCAM:
2737                 case ANIMTYPE_DSCACHEFILE:
2738                 case ANIMTYPE_DSCUR:
2739                 case ANIMTYPE_DSSKEY:
2740                 case ANIMTYPE_DSWOR:
2741                 case ANIMTYPE_DSPART:
2742                 case ANIMTYPE_DSMBALL:
2743                 case ANIMTYPE_DSARM:
2744                 case ANIMTYPE_DSMESH:
2745                 case ANIMTYPE_DSNTREE:
2746                 case ANIMTYPE_DSTEX:
2747                 case ANIMTYPE_DSLAT:
2748                 case ANIMTYPE_DSLINESTYLE:
2749                 case ANIMTYPE_DSSPK:
2750                 case ANIMTYPE_DSGPENCIL:
2751                 case ANIMTYPE_DSMCLIP:
2752                 {
2753                         /* sanity checking... */
2754                         if (ale->adt) {
2755                                 /* select/deselect */
2756                                 if (selectmode == SELECT_INVERT) {
2757                                         /* inverse selection status of this AnimData block only */
2758                                         ale->adt->flag ^= ADT_UI_SELECTED;
2759                                 }
2760                                 else {
2761                                         /* select AnimData block by itself */
2762                                         ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2763                                         ale->adt->flag |= ADT_UI_SELECTED;
2764                                 }
2765
2766                                 /* set active? */
2767                                 if ((ale->adt) && (ale->adt->flag & ADT_UI_SELECTED))
2768                                         ale->adt->flag |= ADT_UI_ACTIVE;
2769                         }
2770
2771                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
2772                         break;
2773                 }
2774                 case ANIMTYPE_GROUP:
2775                 {
2776                         bActionGroup *agrp = (bActionGroup *)ale->data;
2777
2778                         Object *ob = NULL;
2779                         bPoseChannel *pchan = NULL;
2780
2781
2782                         /* Armatures-Specific Feature:
2783                          * Since groups are used to collect F-Curves of the same Bone by default
2784                          * (via Keying Sets) so that they can be managed better, we try to make
2785                          * things here easier for animators by mapping group selection to bone
2786                          * selection.
2787                          *
2788                          * Only do this if "Only Selected" dopesheet filter is not active, or else it
2789                          * becomes too unpredictable/tricky to manage
2790                          */
2791                         if ((ac->ads->filterflag & ADS_FILTER_ONLYSEL) == 0) {
2792                                 if ((ale->id) && (GS(ale->id->name) == ID_OB)) {
2793                                         ob = (Object *)ale->id;
2794
2795                                         if (ob->type == OB_ARMATURE) {
2796                                                 /* Assume for now that any group with corresponding name is what we want
2797                                                  * (i.e. for an armature whose location is animated, things would break
2798                                                  * if the user were to add a bone named "Location").
2799                                                  *
2800                                                  * TODO: check the first F-Curve or so to be sure...
2801                                                  */
2802                                                 pchan = BKE_pose_channel_find_name(ob->pose, agrp->name);
2803                                         }
2804                                 }
2805                         }
2806
2807                         /* select/deselect group */
2808                         if (selectmode == SELECT_INVERT) {
2809                                 /* inverse selection status of this group only */
2810                                 agrp->flag ^= AGRP_SELECTED;
2811                         }
2812                         else if (selectmode == -1) {
2813                                 /* select all in group (and deselect everything else) */
2814                                 FCurve *fcu;
2815
2816                                 /* deselect all other channels */
2817                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2818                                 if (pchan) ED_pose_deselect_all(ob, SEL_DESELECT, false);
2819
2820                                 /* only select channels in group and group itself */
2821                                 for (fcu = agrp->channels.first; fcu && fcu->grp == agrp; fcu = fcu->next)
2822                                         fcu->flag |= FCURVE_SELECTED;
2823                                 agrp->flag |= AGRP_SELECTED;
2824                         }
2825                         else {
2826                                 /* select group by itself */
2827                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2828                                 if (pchan) ED_pose_deselect_all(ob, SEL_DESELECT, false);
2829
2830                                 agrp->flag |= AGRP_SELECTED;
2831                         }
2832
2833                         /* if group is selected now, make group the 'active' one in the visible list */
2834                         if (agrp->flag & AGRP_SELECTED) {
2835                                 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, agrp, ANIMTYPE_GROUP);
2836                                 if (pchan) ED_pose_bone_select(ob, pchan, true);
2837                         }
2838                         else {
2839                                 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, NULL, ANIMTYPE_GROUP);
2840                                 if (pchan) ED_pose_bone_select(ob, pchan, false);
2841                         }
2842
2843                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
2844                         break;
2845                 }
2846                 case ANIMTYPE_FCURVE:
2847                 case ANIMTYPE_NLACURVE:
2848                 {
2849                         FCurve *fcu = (FCurve *)ale->data;
2850
2851                         /* select/deselect */
2852                         if (selectmode == SELECT_INVERT) {
2853                                 /* inverse selection status of this F-Curve only */
2854                                 fcu->flag ^= FCURVE_SELECTED;
2855                         }
2856                         else {
2857                                 /* select F-Curve by itself */
2858                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2859                                 fcu->flag |= FCURVE_SELECTED;
2860                         }
2861
2862                         /* if F-Curve is selected now, make F-Curve the 'active' one in the visible list */
2863                         if (fcu->flag & FCURVE_SELECTED)
2864                                 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, fcu, ale->type);
2865
2866                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
2867                         break;
2868                 }
2869                 case ANIMTYPE_SHAPEKEY:
2870                 {
2871                         KeyBlock *kb = (KeyBlock *)ale->data;
2872
2873                         /* select/deselect */
2874                         if (selectmode == SELECT_INVERT) {
2875                                 /* inverse selection status of this ShapeKey only */
2876                                 kb->flag ^= KEYBLOCK_SEL;
2877                         }
2878                         else {
2879                                 /* select ShapeKey by itself */
2880                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2881                                 kb->flag |= KEYBLOCK_SEL;
2882                         }
2883
2884                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
2885                         break;
2886                 }
2887                 case ANIMTYPE_NLACONTROLS:
2888                 {
2889                         AnimData *adt = (AnimData *)ale->data;
2890
2891                         /* toggle expand
2892                          *   - Although the triangle widget already allows this, since there's nothing else that can be done here now,
2893                          *     let's just use it for easier expand/collapse for now
2894                          */
2895                         adt->flag ^= ADT_NLA_SKEYS_COLLAPSED;
2896
2897                         notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
2898                         break;
2899                 }
2900                 case ANIMTYPE_GPDATABLOCK:
2901                 {
2902                         bGPdata *gpd = (bGPdata *)ale->data;
2903
2904                         /* toggle expand
2905                          * - although the triangle widget already allows this, the whole channel can also be used for this purpose
2906                          */
2907                         gpd->flag ^= GP_DATA_EXPAND;
2908
2909                         notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
2910                         break;
2911                 }
2912                 case ANIMTYPE_GPLAYER:
2913                 {
2914                         bGPDlayer *gpl = (bGPDlayer *)ale->data;
2915
2916                         /* select/deselect */
2917                         if (selectmode == SELECT_INVERT) {
2918                                 /* invert selection status of this layer only */
2919                                 gpl->flag ^= GP_LAYER_SELECT;
2920                         }
2921                         else {
2922                                 /* select layer by itself */
2923                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2924                                 gpl->flag |= GP_LAYER_SELECT;
2925                         }
2926
2927                         /* change active layer, if this is selected (since we must always have an active layer) */
2928                         if (gpl->flag & GP_LAYER_SELECT) {
2929                                 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, gpl, ANIMTYPE_GPLAYER);
2930                         }
2931
2932                         WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL); /* Grease Pencil updates */
2933                         notifierFlags |= (ND_ANIMCHAN | NA_EDITED); /* Animation Ediotrs updates */
2934                         break;
2935                 }
2936                 case ANIMTYPE_MASKDATABLOCK:
2937                 {
2938                         Mask *mask = (Mask *)ale->data;
2939
2940                         /* toggle expand
2941                          * - although the triangle widget already allows this, the whole channel can also be used for this purpose
2942                          */
2943                         mask->flag ^= MASK_ANIMF_EXPAND;
2944
2945                         notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
2946                         break;
2947                 }
2948                 case ANIMTYPE_MASKLAYER:
2949                 {
2950                         MaskLayer *masklay = (MaskLayer *)ale->data;
2951
2952                         /* select/deselect */
2953                         if (selectmode == SELECT_INVERT) {
2954                                 /* invert selection status of this layer only */
2955                                 masklay->flag ^= MASK_LAYERFLAG_SELECT;
2956                         }
2957                         else {
2958                                 /* select layer by itself */
2959                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2960                                 masklay->flag |= MASK_LAYERFLAG_SELECT;
2961                         }
2962
2963                         notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
2964                         break;
2965                 }
2966                 default:
2967                         if (G.debug & G_DEBUG)
2968                                 printf("Error: Invalid channel type in mouse_anim_channels()\n");
2969                         break;
2970         }
2971
2972         /* free channels */
2973         ANIM_animdata_freelist(&anim_data);
2974
2975         /* return notifier flags */
2976         return notifierFlags;
2977 }
2978
2979 /* ------------------- */
2980
2981 /* handle clicking */
2982 static int animchannels_mouseclick_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2983 {
2984         bAnimContext ac;
2985         ARegion *ar;
2986         View2D *v2d;
2987         int channel_index;
2988         int notifierFlags = 0;
2989         short selectmode;
2990         float x, y;
2991
2992
2993         /* get editor data */
2994         if (ANIM_animdata_get_context(C, &ac) == 0)
2995                 return OPERATOR_CANCELLED;
2996
2997         /* get useful pointers from animation context data */
2998         ar = ac.ar;
2999         v2d = &ar->v2d;