Merge branch 'master' into blender2.8
[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_fcurve.h"
54 #include "BKE_gpencil.h"
55 #include "BKE_context.h"
56 #include "BKE_library.h"
57 #include "BKE_mask.h"
58 #include "BKE_global.h"
59 #include "BKE_scene.h"
60
61 #include "DEG_depsgraph_build.h"
62
63 #include "UI_view2d.h"
64
65 #include "ED_anim_api.h"
66 #include "ED_armature.h"
67 #include "ED_keyframes_edit.h" // XXX move the select modes out of there!
68 #include "ED_object.h"
69 #include "ED_screen.h"
70
71 #include "WM_api.h"
72 #include "WM_types.h"
73
74 /* ************************************************************************** */
75 /* CHANNELS API - Exposed API */
76
77 /* -------------------------- Selection ------------------------------------- */
78
79 /* Set the given animation-channel as the active one for the active context */
80 // TODO: extend for animdata types...
81 void ANIM_set_active_channel(bAnimContext *ac, void *data, eAnimCont_Types datatype, eAnimFilter_Flags filter, void *channel_data, eAnim_ChannelType channel_type)
82 {
83         ListBase anim_data = {NULL, NULL};
84         bAnimListElem *ale;
85
86         /* try to build list of filtered items */
87         ANIM_animdata_filter(ac, &anim_data, filter, data, datatype);
88         if (BLI_listbase_is_empty(&anim_data))
89                 return;
90
91         /* only clear the 'active' flag for the channels of the same type */
92         for (ale = anim_data.first; ale; ale = ale->next) {
93                 /* skip if types don't match */
94                 if (channel_type != ale->type)
95                         continue;
96
97                 /* flag to set depends on type */
98                 switch (ale->type) {
99                         case ANIMTYPE_GROUP:
100                         {
101                                 bActionGroup *agrp = (bActionGroup *)ale->data;
102
103                                 ACHANNEL_SET_FLAG(agrp, ACHANNEL_SETFLAG_CLEAR, AGRP_ACTIVE);
104                                 break;
105                         }
106                         case ANIMTYPE_FCURVE:
107                         case ANIMTYPE_NLACURVE:
108                         {
109                                 FCurve *fcu = (FCurve *)ale->data;
110
111                                 ACHANNEL_SET_FLAG(fcu, ACHANNEL_SETFLAG_CLEAR, FCURVE_ACTIVE);
112                                 break;
113                         }
114                         case ANIMTYPE_NLATRACK:
115                         {
116                                 NlaTrack *nlt = (NlaTrack *)ale->data;
117
118                                 ACHANNEL_SET_FLAG(nlt, ACHANNEL_SETFLAG_CLEAR, NLATRACK_ACTIVE);
119                                 break;
120                         }
121                         case ANIMTYPE_FILLACTD: /* Action Expander */
122                         case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
123                         case ANIMTYPE_DSLAM:
124                         case ANIMTYPE_DSCAM:
125                         case ANIMTYPE_DSCACHEFILE:
126                         case ANIMTYPE_DSCUR:
127                         case ANIMTYPE_DSSKEY:
128                         case ANIMTYPE_DSWOR:
129                         case ANIMTYPE_DSPART:
130                         case ANIMTYPE_DSMBALL:
131                         case ANIMTYPE_DSARM:
132                         case ANIMTYPE_DSMESH:
133                         case ANIMTYPE_DSTEX:
134                         case ANIMTYPE_DSLAT:
135                         case ANIMTYPE_DSLINESTYLE:
136                         case ANIMTYPE_DSSPK:
137                         case ANIMTYPE_DSGPENCIL:
138                         case ANIMTYPE_DSMCLIP:
139                         {
140                                 /* need to verify that this data is valid for now */
141                                 if (ale->adt) {
142                                         ACHANNEL_SET_FLAG(ale->adt, ACHANNEL_SETFLAG_CLEAR, ADT_UI_ACTIVE);
143                                 }
144                                 break;
145                         }
146                         case ANIMTYPE_GPLAYER:
147                         {
148                                 bGPDlayer *gpl = (bGPDlayer *)ale->data;
149
150                                 ACHANNEL_SET_FLAG(gpl, ACHANNEL_SETFLAG_CLEAR, GP_LAYER_ACTIVE);
151                                 break;
152                         }
153                 }
154         }
155
156         /* set active flag */
157         if (channel_data) {
158                 switch (channel_type) {
159                         case ANIMTYPE_GROUP:
160                         {
161                                 bActionGroup *agrp = (bActionGroup *)channel_data;
162                                 agrp->flag |= AGRP_ACTIVE;
163                                 break;
164                         }
165                         case ANIMTYPE_FCURVE:
166                         case ANIMTYPE_NLACURVE:
167                         {
168                                 FCurve *fcu = (FCurve *)channel_data;
169                                 fcu->flag |= FCURVE_ACTIVE;
170                                 break;
171                         }
172                         case ANIMTYPE_NLATRACK:
173                         {
174                                 NlaTrack *nlt = (NlaTrack *)channel_data;
175                                 nlt->flag |= NLATRACK_ACTIVE;
176                                 break;
177                         }
178                         case ANIMTYPE_FILLACTD: /* Action Expander */
179                         case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
180                         case ANIMTYPE_DSLAM:
181                         case ANIMTYPE_DSCAM:
182                         case ANIMTYPE_DSCACHEFILE:
183                         case ANIMTYPE_DSCUR:
184                         case ANIMTYPE_DSSKEY:
185                         case ANIMTYPE_DSWOR:
186                         case ANIMTYPE_DSPART:
187                         case ANIMTYPE_DSMBALL:
188                         case ANIMTYPE_DSARM:
189                         case ANIMTYPE_DSMESH:
190                         case ANIMTYPE_DSLAT:
191                         case ANIMTYPE_DSLINESTYLE:
192                         case ANIMTYPE_DSSPK:
193                         case ANIMTYPE_DSNTREE:
194                         case ANIMTYPE_DSTEX:
195                         case ANIMTYPE_DSGPENCIL:
196                         case ANIMTYPE_DSMCLIP:
197                         {
198                                 /* need to verify that this data is valid for now */
199                                 if (ale && ale->adt) {
200                                         ale->adt->flag |= ADT_UI_ACTIVE;
201                                 }
202                                 break;
203                         }
204
205                         case ANIMTYPE_GPLAYER:
206                         {
207                                 bGPDlayer *gpl = (bGPDlayer *)channel_data;
208                                 gpl->flag |= GP_LAYER_ACTIVE;
209                                 break;
210                         }
211
212                         /* unhandled currently, but may be interesting */
213                         case ANIMTYPE_MASKLAYER:
214                         case ANIMTYPE_SHAPEKEY:
215                         case ANIMTYPE_NLAACTION:
216                                 break;
217
218                         /* other types */
219                         default:
220                                 break;
221                 }
222         }
223
224         /* clean up */
225         ANIM_animdata_freelist(&anim_data);
226 }
227
228 static void select_pchan_for_action_group(bAnimContext *ac, bActionGroup *agrp, bAnimListElem *ale)
229 {
230         /* Armatures-Specific Feature:
231          * See mouse_anim_channels() -> ANIMTYPE_GROUP case for more details (T38737)
232          */
233         if ((ac->ads->filterflag & ADS_FILTER_ONLYSEL) == 0) {
234                 if ((ale->id) && (GS(ale->id->name) == ID_OB)) {
235                         Object *ob = (Object *)ale->id;
236                         if (ob->type == OB_ARMATURE) {
237                                 /* Assume for now that any group with corresponding name is what we want
238                                  * (i.e. for an armature whose location is animated, things would break
239                                  * if the user were to add a bone named "Location").
240                                  *
241                                  * TODO: check the first F-Curve or so to be sure...
242                                  */
243                                 bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, agrp->name);
244                                 if (agrp->flag & AGRP_SELECTED) {
245                                         ED_pose_bone_select(ob, pchan, true);
246                                 }
247                                 else {
248                                         ED_pose_bone_select(ob, pchan, false);
249                                 }
250                         }
251                 }
252         }
253 }
254
255 /* Deselect all animation channels
256  *      - data: pointer to datatype, as contained in bAnimContext
257  *      - datatype: the type of data that 'data' represents (eAnimCont_Types)
258  *      - test: check if deselecting instead of selecting
259  *      - sel: eAnimChannels_SetFlag;
260  */
261 void ANIM_deselect_anim_channels(bAnimContext *ac, void *data, eAnimCont_Types datatype, bool test, eAnimChannels_SetFlag sel)
262 {
263         ListBase anim_data = {NULL, NULL};
264         bAnimListElem *ale;
265         int filter;
266
267         /* filter data */
268         /* NOTE: no list visible, otherwise, we get dangling */
269         filter = ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_CHANNELS;
270         ANIM_animdata_filter(ac, &anim_data, filter, data, datatype);
271
272         /* See if we should be selecting or deselecting */
273         if (test) {
274                 for (ale = anim_data.first; ale; ale = ale->next) {
275                         if (sel == 0)
276                                 break;
277
278                         switch (ale->type) {
279                                 case ANIMTYPE_SCENE:
280                                         if (ale->flag & SCE_DS_SELECTED)
281                                                 sel = ACHANNEL_SETFLAG_CLEAR;
282                                         break;
283                                 case ANIMTYPE_OBJECT:
284 #if 0   /* for now, do not take object selection into account, since it gets too annoying */
285                                         if (ale->flag & SELECT)
286                                                 sel = ACHANNEL_SETFLAG_CLEAR;
287 #endif
288                                         break;
289                                 case ANIMTYPE_GROUP:
290                                         if (ale->flag & AGRP_SELECTED)
291                                                 sel = ACHANNEL_SETFLAG_CLEAR;
292                                         break;
293                                 case ANIMTYPE_FCURVE:
294                                 case ANIMTYPE_NLACURVE:
295                                         if (ale->flag & FCURVE_SELECTED)
296                                                 sel = ACHANNEL_SETFLAG_CLEAR;
297                                         break;
298                                 case ANIMTYPE_SHAPEKEY:
299                                         if (ale->flag & KEYBLOCK_SEL)
300                                                 sel = ACHANNEL_SETFLAG_CLEAR;
301                                         break;
302                                 case ANIMTYPE_NLATRACK:
303                                         if (ale->flag & NLATRACK_SELECTED)
304                                                 sel = ACHANNEL_SETFLAG_CLEAR;
305                                         break;
306
307                                 case ANIMTYPE_FILLACTD: /* Action Expander */
308                                 case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
309                                 case ANIMTYPE_DSLAM:
310                                 case ANIMTYPE_DSCAM:
311                                 case ANIMTYPE_DSCACHEFILE:
312                                 case ANIMTYPE_DSCUR:
313                                 case ANIMTYPE_DSSKEY:
314                                 case ANIMTYPE_DSWOR:
315                                 case ANIMTYPE_DSPART:
316                                 case ANIMTYPE_DSMBALL:
317                                 case ANIMTYPE_DSARM:
318                                 case ANIMTYPE_DSMESH:
319                                 case ANIMTYPE_DSNTREE:
320                                 case ANIMTYPE_DSTEX:
321                                 case ANIMTYPE_DSLAT:
322                                 case ANIMTYPE_DSLINESTYLE:
323                                 case ANIMTYPE_DSSPK:
324                                 case ANIMTYPE_DSGPENCIL:
325                                 case ANIMTYPE_DSMCLIP:
326                                 {
327                                         if ((ale->adt) && (ale->adt->flag & ADT_UI_SELECTED))
328                                                 sel = ACHANNEL_SETFLAG_CLEAR;
329                                         break;
330                                 }
331                                 case ANIMTYPE_GPLAYER:
332                                         if (ale->flag & GP_LAYER_SELECT)
333                                                 sel = ACHANNEL_SETFLAG_CLEAR;
334                                         break;
335                                 case ANIMTYPE_MASKLAYER:
336                                         if (ale->flag & MASK_LAYERFLAG_SELECT)
337                                                 sel = ACHANNEL_SETFLAG_CLEAR;
338                                         break;
339                         }
340                 }
341         }
342
343         /* Now set the flags */
344         for (ale = anim_data.first; ale; ale = ale->next) {
345                 switch (ale->type) {
346                         case ANIMTYPE_SCENE:
347                         {
348                                 Scene *scene = (Scene *)ale->data;
349
350                                 ACHANNEL_SET_FLAG(scene, sel, SCE_DS_SELECTED);
351
352                                 if (scene->adt) {
353                                         ACHANNEL_SET_FLAG(scene, sel, ADT_UI_SELECTED);
354                                 }
355                                 break;
356                         }
357                         case ANIMTYPE_OBJECT:
358                         {
359 #if 0   /* for now, do not take object selection into account, since it gets too annoying */
360                                 Base *base = (Base *)ale->data;
361                                 Object *ob = base->object;
362
363                                 ACHANNEL_SET_FLAG(base, sel, SELECT);
364                                 ACHANNEL_SET_FLAG(ob, sel, SELECT);
365
366                                 if (ob->adt) {
367                                         ACHANNEL_SET_FLAG(ob, sel, ADT_UI_SELECTED);
368                                 }
369 #endif
370                                 break;
371                         }
372                         case ANIMTYPE_GROUP:
373                         {
374                                 bActionGroup *agrp = (bActionGroup *)ale->data;
375                                 ACHANNEL_SET_FLAG(agrp, sel, AGRP_SELECTED);
376                                 select_pchan_for_action_group(ac, agrp, ale);
377                                 agrp->flag &= ~AGRP_ACTIVE;
378                                 break;
379                         }
380                         case ANIMTYPE_FCURVE:
381                         case ANIMTYPE_NLACURVE:
382                         {
383                                 FCurve *fcu = (FCurve *)ale->data;
384
385                                 ACHANNEL_SET_FLAG(fcu, sel, FCURVE_SELECTED);
386                                 fcu->flag &= ~FCURVE_ACTIVE;
387                                 break;
388                         }
389                         case ANIMTYPE_SHAPEKEY:
390                         {
391                                 KeyBlock *kb = (KeyBlock *)ale->data;
392
393                                 ACHANNEL_SET_FLAG(kb, sel, KEYBLOCK_SEL);
394                                 break;
395                         }
396                         case ANIMTYPE_NLATRACK:
397                         {
398                                 NlaTrack *nlt = (NlaTrack *)ale->data;
399
400                                 ACHANNEL_SET_FLAG(nlt, sel, NLATRACK_SELECTED);
401                                 nlt->flag &= ~NLATRACK_ACTIVE;
402                                 break;
403                         }
404                         case ANIMTYPE_FILLACTD: /* Action Expander */
405                         case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
406                         case ANIMTYPE_DSLAM:
407                         case ANIMTYPE_DSCAM:
408                         case ANIMTYPE_DSCACHEFILE:
409                         case ANIMTYPE_DSCUR:
410                         case ANIMTYPE_DSSKEY:
411                         case ANIMTYPE_DSWOR:
412                         case ANIMTYPE_DSPART:
413                         case ANIMTYPE_DSMBALL:
414                         case ANIMTYPE_DSARM:
415                         case ANIMTYPE_DSMESH:
416                         case ANIMTYPE_DSNTREE:
417                         case ANIMTYPE_DSTEX:
418                         case ANIMTYPE_DSLAT:
419                         case ANIMTYPE_DSLINESTYLE:
420                         case ANIMTYPE_DSSPK:
421                         case ANIMTYPE_DSGPENCIL:
422                         case ANIMTYPE_DSMCLIP:
423                         {
424                                 /* need to verify that this data is valid for now */
425                                 if (ale->adt) {
426                                         ACHANNEL_SET_FLAG(ale->adt, sel, ADT_UI_SELECTED);
427                                         ale->adt->flag &= ~ADT_UI_ACTIVE;
428                                 }
429                                 break;
430                         }
431                         case ANIMTYPE_GPLAYER:
432                         {
433                                 bGPDlayer *gpl = (bGPDlayer *)ale->data;
434
435                                 ACHANNEL_SET_FLAG(gpl, sel, GP_LAYER_SELECT);
436                                 break;
437                         }
438                         case ANIMTYPE_MASKLAYER:
439                         {
440                                 MaskLayer *masklay = (MaskLayer *)ale->data;
441
442                                 ACHANNEL_SET_FLAG(masklay, sel, MASK_LAYERFLAG_SELECT);
443                                 break;
444                         }
445                 }
446         }
447
448         /* Cleanup */
449         ANIM_animdata_freelist(&anim_data);
450 }
451
452 /* ---------------------------- Graph Editor ------------------------------------- */
453
454 /* Flush visibility (for Graph Editor) changes up/down hierarchy for changes in the given setting
455  *      - anim_data: list of the all the anim channels that can be chosen
456  *              -> filtered using ANIMFILTER_CHANNELS only, since if we took VISIBLE too,
457  *                then the channels under closed expanders get ignored...
458  *      - ale_setting: the anim channel (not in the anim_data list directly, though occurring there)
459  *              with the new state of the setting that we want flushed up/down the hierarchy
460  *      - setting: type of setting to set
461  *      - on: whether the visibility setting has been enabled or disabled
462  */
463 void ANIM_flush_setting_anim_channels(bAnimContext *ac, ListBase *anim_data, bAnimListElem *ale_setting, eAnimChannel_Settings setting, eAnimChannels_SetFlag mode)
464 {
465         bAnimListElem *ale, *match = NULL;
466         int prevLevel = 0, matchLevel = 0;
467
468         /* sanity check */
469         if (ELEM(NULL, anim_data, anim_data->first))
470                 return;
471
472         if (setting == ACHANNEL_SETTING_ALWAYS_VISIBLE) {
473                 return;
474         }
475
476         /* find the channel that got changed */
477         for (ale = anim_data->first; ale; ale = ale->next) {
478                 /* compare data, and type as main way of identifying the channel */
479                 if ((ale->data == ale_setting->data) && (ale->type == ale_setting->type)) {
480                         /* we also have to check the ID, this is assigned to, since a block may have multiple users */
481                         /* TODO: is the owner-data more revealing? */
482                         if (ale->id == ale_setting->id) {
483                                 match = ale;
484                                 break;
485                         }
486                 }
487         }
488         if (match == NULL) {
489                 printf("ERROR: no channel matching the one changed was found\n");
490                 return;
491         }
492         else {
493                 const bAnimChannelType *acf = ANIM_channel_get_typeinfo(ale_setting);
494
495                 if (acf == NULL) {
496                         printf("ERROR: no channel info for the changed channel\n");
497                         return;
498                 }
499
500                 /* get the level of the channel that was affected
501                  *   - we define the level as simply being the offset for the start of the channel
502                  */
503                 matchLevel = (acf->get_offset) ? acf->get_offset(ac, ale_setting) : 0;
504                 prevLevel = matchLevel;
505         }
506
507         /* flush up?
508          *
509          * For Visibility:
510          *      - only flush up if the current state is now enabled (positive 'on' state is default)
511          *        (otherwise, it's too much work to force the parents to be inactive too)
512          *
513          * For everything else:
514          *      - only flush up if the current state is now disabled (negative 'off' state is default)
515          *        (otherwise, it's too much work to force the parents to be active too)
516          */
517         if ( ((setting == ACHANNEL_SETTING_VISIBLE) && (mode != ACHANNEL_SETFLAG_CLEAR)) ||
518              ((setting != ACHANNEL_SETTING_VISIBLE) && (mode == ACHANNEL_SETFLAG_CLEAR)))
519         {
520                 /* go backwards in the list, until the highest-ranking element (by indention has been covered) */
521                 for (ale = match->prev; ale; ale = ale->prev) {
522                         const bAnimChannelType *acf = ANIM_channel_get_typeinfo(ale);
523                         int level;
524
525                         /* if no channel info was found, skip, since this type might not have any useful info */
526                         if (acf == NULL)
527                                 continue;
528
529                         /* get the level of the current channel traversed
530                          *   - we define the level as simply being the offset for the start of the channel
531                          */
532                         level = (acf->get_offset) ? acf->get_offset(ac, ale) : 0;
533
534                         /* if the level is 'less than' (i.e. more important) the level we're matching
535                          * but also 'less than' the level just tried (i.e. only the 1st group above grouped F-Curves,
536                          * when toggling visibility of F-Curves, gets flushed, which should happen if we don't let prevLevel
537                          * get updated below once the first 1st group is found)...
538                          */
539                         if (level < prevLevel) {
540                                 /* flush the new status... */
541                                 ANIM_channel_setting_set(ac, ale, setting, mode);
542
543                                 /* store this level as the 'old' level now */
544                                 prevLevel = level;
545                         }
546                         /* if the level is 'greater than' (i.e. less important) than the previous level... */
547                         else if (level > prevLevel) {
548                                 /* if previous level was a base-level (i.e. 0 offset / root of one hierarchy),
549                                  * stop here
550                                  */
551                                 if (prevLevel == 0)
552                                         break;
553                                 /* otherwise, this level weaves into another sibling hierarchy to the previous one just
554                                  * finished, so skip until we get to the parent of this level
555                                  */
556                                 else
557                                         continue;
558                         }
559                 }
560         }
561
562         /* flush down (always) */
563         {
564                 /* go forwards in the list, until the lowest-ranking element (by indention has been covered) */
565                 for (ale = match->next; ale; ale = ale->next) {
566                         const bAnimChannelType *acf = ANIM_channel_get_typeinfo(ale);
567                         int level;
568
569                         /* if no channel info was found, skip, since this type might not have any useful info */
570                         if (acf == NULL)
571                                 continue;
572
573                         /* get the level of the current channel traversed
574                          *   - we define the level as simply being the offset for the start of the channel
575                          */
576                         level = (acf->get_offset) ? acf->get_offset(ac, ale) : 0;
577
578                         /* if the level is 'greater than' (i.e. less important) the channel that was changed,
579                          * flush the new status...
580                          */
581                         if (level > matchLevel)
582                                 ANIM_channel_setting_set(ac, ale, setting, mode);
583                         /* however, if the level is 'less than or equal to' the channel that was changed,
584                          * (i.e. the current channel is as important if not more important than the changed channel)
585                          * then we should stop, since we've found the last one of the children we should flush
586                          */
587                         else
588                                 break;
589
590                         /* store this level as the 'old' level now */
591                         // prevLevel = level; // XXX: prevLevel is unused
592                 }
593         }
594 }
595
596 /* -------------------------- F-Curves ------------------------------------- */
597
598 /* Delete the given F-Curve from its AnimData block */
599 void ANIM_fcurve_delete_from_animdata(bAnimContext *ac, AnimData *adt, FCurve *fcu)
600 {
601         /* - if no AnimData, we've got nowhere to remove the F-Curve from
602          *      (this doesn't guarantee that the F-Curve is in there, but at least we tried
603          * - if no F-Curve, there is nothing to remove
604          */
605         if (ELEM(NULL, adt, fcu))
606                 return;
607
608         /* remove from whatever list it came from
609          *      - Action Group
610          *      - Action
611          *      - Drivers
612          *      - TODO... some others?
613          */
614         if ((ac) && (ac->datatype == ANIMCONT_DRIVERS)) {
615                 /* driver F-Curve */
616                 BLI_remlink(&adt->drivers, fcu);
617         }
618         else if (adt->action) {
619                 bAction *act = adt->action;
620
621                 /* remove from group or action, whichever one "owns" the F-Curve */
622                 if (fcu->grp) {
623                         bActionGroup *agrp = fcu->grp;
624
625                         /* remove F-Curve from group+action */
626                         action_groups_remove_channel(act, fcu);
627
628                         /* if group has no more channels, remove it too,
629                          * otherwise can have many dangling groups [#33541]
630                          */
631                         if (BLI_listbase_is_empty(&agrp->channels)) {
632                                 BLI_freelinkN(&act->groups, agrp);
633                         }
634                 }
635                 else {
636                         BLI_remlink(&act->curves, fcu);
637                 }
638
639                 /* if action has no more F-Curves as a result of this, unlink it from
640                  * AnimData if it did not come from a NLA Strip being tweaked.
641                  *
642                  * This is done so that we don't have dangling Object+Action entries in
643                  * channel list that are empty, and linger around long after the data they
644                  * are for has disappeared (and probably won't come back).
645                  */
646                 if (BLI_listbase_is_empty(&act->curves) && (adt->flag & ADT_NLA_EDIT_ON) == 0) {
647                         id_us_min(&act->id);
648                         adt->action = NULL;
649                 }
650         }
651
652         /* free the F-Curve itself */
653         free_fcurve(fcu);
654 }
655
656 /* ************************************************************************** */
657 /* OPERATORS */
658
659 /* ****************** Operator Utilities ********************************** */
660
661 /* poll callback for being in an Animation Editor channels list region */
662 static int animedit_poll_channels_active(bContext *C)
663 {
664         ScrArea *sa = CTX_wm_area(C);
665
666         /* channels region test */
667         /* TODO: could enhance with actually testing if channels region? */
668         if (ELEM(NULL, sa, CTX_wm_region(C)))
669                 return 0;
670         /* animation editor test */
671         if (ELEM(sa->spacetype, SPACE_ACTION, SPACE_IPO, SPACE_NLA) == 0)
672                 return 0;
673
674         return 1;
675 }
676
677 /* poll callback for Animation Editor channels list region + not in NLA-tweakmode for NLA */
678 static int animedit_poll_channels_nla_tweakmode_off(bContext *C)
679 {
680         ScrArea *sa = CTX_wm_area(C);
681         Scene *scene = CTX_data_scene(C);
682
683         /* channels region test */
684         /* TODO: could enhance with actually testing if channels region? */
685         if (ELEM(NULL, sa, CTX_wm_region(C)))
686                 return 0;
687         /* animation editor test */
688         if (ELEM(sa->spacetype, SPACE_ACTION, SPACE_IPO, SPACE_NLA) == 0)
689                 return 0;
690
691         /* NLA TweakMode test */
692         if (sa->spacetype == SPACE_NLA) {
693                 if ((scene == NULL) || (scene->flag & SCE_NLA_EDIT_ON))
694                         return 0;
695         }
696
697         return 1;
698 }
699
700 /* ****************** Rearrange Channels Operator ******************* */
701
702 /* constants for channel rearranging */
703 /* WARNING: don't change existing ones without modifying rearrange func accordingly */
704 typedef enum eRearrangeAnimChan_Mode {
705         REARRANGE_ANIMCHAN_TOP = -2,
706         REARRANGE_ANIMCHAN_UP = -1,
707         REARRANGE_ANIMCHAN_DOWN = 1,
708         REARRANGE_ANIMCHAN_BOTTOM = 2
709 } eRearrangeAnimChan_Mode;
710
711 /* defines for rearranging channels */
712 static const EnumPropertyItem prop_animchannel_rearrange_types[] = {
713         {REARRANGE_ANIMCHAN_TOP, "TOP", 0, "To Top", ""},
714         {REARRANGE_ANIMCHAN_UP, "UP", 0, "Up", ""},
715         {REARRANGE_ANIMCHAN_DOWN, "DOWN", 0, "Down", ""},
716         {REARRANGE_ANIMCHAN_BOTTOM, "BOTTOM", 0, "To Bottom", ""},
717         {0, NULL, 0, NULL, NULL}
718 };
719
720 /* Reordering "Islands" Defines ----------------------------------- */
721
722 /* Island definition - just a listbase container */
723 typedef struct tReorderChannelIsland {
724         struct tReorderChannelIsland *next, *prev;
725
726         ListBase channels;  /* channels within this region with the same state */
727         int flag;           /* eReorderIslandFlag */
728 } tReorderChannelIsland;
729
730 /* flags for channel reordering islands */
731 typedef enum eReorderIslandFlag {
732         REORDER_ISLAND_SELECTED         = (1 << 0),   /* island is selected */
733         REORDER_ISLAND_UNTOUCHABLE      = (1 << 1),   /* island should be ignored */
734         REORDER_ISLAND_MOVED            = (1 << 2),   /* island has already been moved */
735         REORDER_ISLAND_HIDDEN           = (1 << 3),   /* island is not visible */
736 } eReorderIslandFlag;
737
738
739 /* Rearrange Methods --------------------------------------------- */
740
741 static bool rearrange_island_ok(tReorderChannelIsland *island)
742 {
743         /* island must not be untouchable */
744         if (island->flag & REORDER_ISLAND_UNTOUCHABLE)
745                 return 0;
746
747         /* island should be selected to be moved */
748         return (island->flag & REORDER_ISLAND_SELECTED) && !(island->flag & REORDER_ISLAND_MOVED);
749 }
750
751 /* ............................. */
752
753 static bool rearrange_island_top(ListBase *list, tReorderChannelIsland *island)
754 {
755         if (rearrange_island_ok(island)) {
756                 /* remove from current position */
757                 BLI_remlink(list, island);
758
759                 /* make it first element */
760                 BLI_insertlinkbefore(list, list->first, island);
761
762                 return 1;
763         }
764
765         return 0;
766 }
767
768 static bool rearrange_island_up(ListBase *list, tReorderChannelIsland *island)
769 {
770         if (rearrange_island_ok(island)) {
771                 /* moving up = moving before the previous island, otherwise we're in the same place */
772                 tReorderChannelIsland *prev = island->prev;
773
774                 /* Skip hidden islands! */
775                 while (prev && prev->flag & REORDER_ISLAND_HIDDEN) {
776                         prev = prev->prev;
777                 }
778
779                 if (prev) {
780                         /* remove from current position */
781                         BLI_remlink(list, island);
782
783                         /* push it up */
784                         BLI_insertlinkbefore(list, prev, island);
785
786                         return 1;
787                 }
788         }
789
790         return 0;
791 }
792
793 static bool rearrange_island_down(ListBase *list, tReorderChannelIsland *island)
794 {
795         if (rearrange_island_ok(island)) {
796                 /* moving down = moving after the next island, otherwise we're in the same place */
797                 tReorderChannelIsland *next = island->next;
798
799                 /* Skip hidden islands! */
800                 while (next && next->flag & REORDER_ISLAND_HIDDEN) {
801                         next = next->next;
802                 }
803
804                 if (next) {
805                         /* can only move past if next is not untouchable (i.e. nothing can go after it) */
806                         if ((next->flag & REORDER_ISLAND_UNTOUCHABLE) == 0) {
807                                 /* remove from current position */
808                                 BLI_remlink(list, island);
809
810                                 /* push it down */
811                                 BLI_insertlinkafter(list, next, island);
812
813                                 return true;
814                         }
815                 }
816                 /* else: no next channel, so we're at the bottom already, so can't move */
817         }
818
819         return false;
820 }
821
822 static bool rearrange_island_bottom(ListBase *list, tReorderChannelIsland *island)
823 {
824         if (rearrange_island_ok(island)) {
825                 tReorderChannelIsland *last = list->last;
826
827                 /* remove island from current position */
828                 BLI_remlink(list, island);
829
830                 /* add before or after the last channel? */
831                 if ((last->flag & REORDER_ISLAND_UNTOUCHABLE) == 0) {
832                         /* can add after it */
833                         BLI_addtail(list, island);
834                 }
835                 else {
836                         /* can at most go just before it, since last cannot be moved */
837                         BLI_insertlinkbefore(list, last, island);
838
839                 }
840
841                 return true;
842         }
843
844         return false;
845 }
846
847 /* ............................. */
848
849 /**
850  * typedef for channel rearranging function
851  *
852  * \param list List of tReorderChannelIsland's that channels belong to
853  * \param island Island to be moved
854  * \return Whether operation was a success
855  */
856 typedef bool (*AnimChanRearrangeFp)(ListBase *list, tReorderChannelIsland *island);
857
858 /* get rearranging function, given 'rearrange' mode */
859 static AnimChanRearrangeFp rearrange_get_mode_func(eRearrangeAnimChan_Mode mode)
860 {
861         switch (mode) {
862                 case REARRANGE_ANIMCHAN_TOP:
863                         return rearrange_island_top;
864                 case REARRANGE_ANIMCHAN_UP:
865                         return rearrange_island_up;
866                 case REARRANGE_ANIMCHAN_DOWN:
867                         return rearrange_island_down;
868                 case REARRANGE_ANIMCHAN_BOTTOM:
869                         return rearrange_island_bottom;
870                 default:
871                         return NULL;
872         }
873 }
874
875 /* Rearrange Islands Generics ------------------------------------- */
876
877 /* add channel into list of islands */
878 static void rearrange_animchannel_add_to_islands(ListBase *islands, ListBase *srcList,
879                                                  Link *channel, eAnim_ChannelType type,
880                                                  const bool is_hidden)
881 {
882         tReorderChannelIsland *island = islands->last;  /* always try to add to last island if possible */
883         bool is_sel = false, is_untouchable = false;
884
885         /* get flags - selected and untouchable from the channel */
886         switch (type) {
887                 case ANIMTYPE_GROUP:
888                 {
889                         bActionGroup *agrp = (bActionGroup *)channel;
890
891                         is_sel = SEL_AGRP(agrp);
892                         is_untouchable = (agrp->flag & AGRP_TEMP) != 0;
893                         break;
894                 }
895                 case ANIMTYPE_FCURVE:
896                 case ANIMTYPE_NLACURVE:
897                 {
898                         FCurve *fcu = (FCurve *)channel;
899
900                         is_sel = SEL_FCU(fcu);
901                         break;
902                 }
903                 case ANIMTYPE_NLATRACK:
904                 {
905                         NlaTrack *nlt = (NlaTrack *)channel;
906
907                         is_sel = SEL_NLT(nlt);
908                         break;
909                 }
910                 case ANIMTYPE_GPLAYER:
911                 {
912                         bGPDlayer *gpl = (bGPDlayer *)channel;
913
914                         is_sel = SEL_GPL(gpl);
915                         break;
916                 }
917                 default:
918                         printf("rearrange_animchannel_add_to_islands(): don't know how to handle channels of type %u\n", type);
919                         return;
920         }
921
922         /* do we need to add to a new island? */
923         if (/* 1) no islands yet */
924             (island == NULL) ||
925             /* 2) unselected islands have single channels only - to allow up/down movement */
926             ((island->flag & REORDER_ISLAND_SELECTED) == 0) ||
927             /* 3) if channel is unselected, stop existing island (it was either wrong sel status, or full already) */
928             (is_sel == 0) ||
929             /* 4) hidden status changes */
930             ((island->flag & REORDER_ISLAND_HIDDEN) != is_hidden)
931             )
932         {
933                 /* create a new island now */
934                 island = MEM_callocN(sizeof(tReorderChannelIsland), "tReorderChannelIsland");
935                 BLI_addtail(islands, island);
936
937                 if (is_sel)
938                         island->flag |= REORDER_ISLAND_SELECTED;
939                 if (is_untouchable)
940                         island->flag |= REORDER_ISLAND_UNTOUCHABLE;
941                 if (is_hidden)
942                         island->flag |= REORDER_ISLAND_HIDDEN;
943         }
944
945         /* add channel to island - need to remove it from its existing list first though */
946         BLI_remlink(srcList, channel);
947         BLI_addtail(&island->channels, channel);
948 }
949
950 /* flatten islands out into a single list again */
951 static void rearrange_animchannel_flatten_islands(ListBase *islands, ListBase *srcList)
952 {
953         tReorderChannelIsland *island, *isn = NULL;
954
955         /* make sure srcList is empty now */
956         BLI_assert(BLI_listbase_is_empty(srcList));
957
958         /* go through merging islands */
959         for (island = islands->first; island; island = isn) {
960                 isn = island->next;
961
962                 /* merge island channels back to main list, then delete the island */
963                 BLI_movelisttolist(srcList, &island->channels);
964                 BLI_freelinkN(islands, island);
965         }
966 }
967
968 /* ............................. */
969
970 /* get a list of all bAnimListElem's of a certain type which are currently visible */
971 static void rearrange_animchannels_filter_visible(ListBase *anim_data_visible, bAnimContext *ac, eAnim_ChannelType type)
972 {
973         ListBase anim_data = {NULL, NULL};
974         bAnimListElem *ale, *ale_next;
975         int filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
976
977         /* get all visible channels */
978         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
979
980         /* now, only keep the ones that are of the types we are interested in */
981         for (ale = anim_data.first; ale; ale = ale_next) {
982                 ale_next = ale->next;
983
984                 if (ale->type != type) {
985                         BLI_freelinkN(&anim_data, ale);
986                 }
987         }
988
989         /* return cleaned up list */
990         *anim_data_visible = anim_data;
991 }
992
993 /* performing rearranging of channels using islands */
994 static bool rearrange_animchannel_islands(ListBase *list, AnimChanRearrangeFp rearrange_func,
995                                           eRearrangeAnimChan_Mode mode, eAnim_ChannelType type,
996                                           ListBase *anim_data_visible)
997 {
998         ListBase islands = {NULL, NULL};
999         Link *channel, *chanNext = NULL;
1000         bool done = false;
1001
1002         /* don't waste effort on an empty list */
1003         if (BLI_listbase_is_empty(list))
1004                 return 0;
1005
1006         /* group channels into islands */
1007         for (channel = list->first; channel; channel = chanNext) {
1008                 /* find out whether this channel is present in anim_data_visible or not! */
1009                 const bool is_hidden = (BLI_findptr(anim_data_visible, channel, offsetof(bAnimListElem, data)) == NULL);
1010                 chanNext = channel->next;
1011                 rearrange_animchannel_add_to_islands(&islands, list, channel, type, is_hidden);
1012         }
1013
1014         /* perform moving of selected islands now, but only if there is more than one of 'em so that something will happen
1015          *      - scanning of the list is performed in the opposite direction to the direction we're moving things, so that we
1016          *        shouldn't need to encounter items we've moved already
1017          */
1018         if (islands.first != islands.last) {
1019                 tReorderChannelIsland *first = (mode > 0) ? islands.last : islands.first;
1020                 tReorderChannelIsland *island, *isn = NULL;
1021
1022                 for (island = first; island; island = isn) {
1023                         isn = (mode > 0) ? island->prev : island->next;
1024
1025                         /* perform rearranging */
1026                         if (rearrange_func(&islands, island)) {
1027                                 island->flag |= REORDER_ISLAND_MOVED;
1028                                 done = true;
1029                         }
1030                 }
1031         }
1032
1033         /* ungroup islands */
1034         rearrange_animchannel_flatten_islands(&islands, list);
1035
1036         /* did we do anything? */
1037         return done;
1038 }
1039
1040 /* NLA Specific Stuff ----------------------------------------------------- */
1041
1042 /* Change the order NLA Tracks within NLA Stack
1043  * ! NLA tracks are displayed in opposite order, so directions need care
1044  *      mode: REARRANGE_ANIMCHAN_*
1045  */
1046 static void rearrange_nla_channels(bAnimContext *ac, AnimData *adt, eRearrangeAnimChan_Mode mode)
1047 {
1048         AnimChanRearrangeFp rearrange_func;
1049         ListBase anim_data_visible = {NULL, NULL};
1050
1051         /* hack: invert mode so that functions will work in right order */
1052         mode *= -1;
1053
1054         /* get rearranging function */
1055         rearrange_func = rearrange_get_mode_func(mode);
1056         if (rearrange_func == NULL)
1057                 return;
1058
1059         /* Filter visible data. */
1060         rearrange_animchannels_filter_visible(&anim_data_visible, ac, ANIMTYPE_NLATRACK);
1061
1062         /* perform rearranging on tracks list */
1063         rearrange_animchannel_islands(&adt->nla_tracks, rearrange_func, mode, ANIMTYPE_NLATRACK, &anim_data_visible);
1064
1065         /* free temp data */
1066         BLI_freelistN(&anim_data_visible);
1067 }
1068
1069 /* Drivers Specific Stuff ------------------------------------------------- */
1070
1071 /* Change the order drivers within AnimData block
1072  *      mode: REARRANGE_ANIMCHAN_*
1073  */
1074 static void rearrange_driver_channels(bAnimContext *ac, AnimData *adt, eRearrangeAnimChan_Mode mode)
1075 {
1076         /* get rearranging function */
1077         AnimChanRearrangeFp rearrange_func = rearrange_get_mode_func(mode);
1078         ListBase anim_data_visible = {NULL, NULL};
1079
1080         if (rearrange_func == NULL)
1081                 return;
1082
1083         /* only consider drivers if they're accessible */
1084         if (EXPANDED_DRVD(adt) == 0)
1085                 return;
1086
1087         /* Filter visible data. */
1088         rearrange_animchannels_filter_visible(&anim_data_visible, ac, ANIMTYPE_FCURVE);
1089
1090         /* perform rearranging on drivers list (drivers are really just F-Curves) */
1091         rearrange_animchannel_islands(&adt->drivers, rearrange_func, mode, ANIMTYPE_FCURVE, &anim_data_visible);
1092
1093         /* free temp data */
1094         BLI_freelistN(&anim_data_visible);
1095 }
1096
1097 /* Action Specific Stuff ------------------------------------------------- */
1098
1099 /* make sure all action-channels belong to a group (and clear action's list) */
1100 static void split_groups_action_temp(bAction *act, bActionGroup *tgrp)
1101 {
1102         bActionGroup *agrp;
1103         FCurve *fcu;
1104
1105         if (act == NULL)
1106                 return;
1107
1108         /* Separate F-Curves into lists per group */
1109         for (agrp = act->groups.first; agrp; agrp = agrp->next) {
1110                 if (agrp->channels.first) {
1111                         fcu = agrp->channels.last;
1112                         act->curves.first = fcu->next;
1113
1114                         fcu = agrp->channels.first;
1115                         fcu->prev = NULL;
1116
1117                         fcu = agrp->channels.last;
1118                         fcu->next = NULL;
1119                 }
1120         }
1121
1122         /* Initialize memory for temp-group */
1123         memset(tgrp, 0, sizeof(bActionGroup));
1124         tgrp->flag |= (AGRP_EXPANDED | AGRP_TEMP);
1125         BLI_strncpy(tgrp->name, "#TempGroup", sizeof(tgrp->name));
1126
1127         /* Move any action-channels not already moved, to the temp group */
1128         if (act->curves.first) {
1129                 /* start of list */
1130                 fcu = act->curves.first;
1131                 fcu->prev = NULL;
1132                 tgrp->channels.first = fcu;
1133                 act->curves.first = NULL;
1134
1135                 /* end of list */
1136                 fcu = act->curves.last;
1137                 fcu->next = NULL;
1138                 tgrp->channels.last = fcu;
1139                 act->curves.last = NULL;
1140
1141                 /* ensure that all of these get their group set to this temp group
1142                  * (so that visibility filtering works)
1143                  */
1144                 for (fcu = tgrp->channels.first; fcu; fcu = fcu->next) {
1145                         fcu->grp = tgrp;
1146                 }
1147         }
1148
1149         /* Add temp-group to list */
1150         BLI_addtail(&act->groups, tgrp);
1151 }
1152
1153 /* link lists of channels that groups have */
1154 static void join_groups_action_temp(bAction *act)
1155 {
1156         bActionGroup *agrp;
1157
1158         for (agrp = act->groups.first; agrp; agrp = agrp->next) {
1159                 ListBase tempGroup;
1160
1161                 /* add list of channels to action's channels */
1162                 tempGroup = agrp->channels;
1163                 BLI_movelisttolist(&act->curves, &agrp->channels);
1164                 agrp->channels = tempGroup;
1165
1166                 /* clear moved flag */
1167                 agrp->flag &= ~AGRP_MOVED;
1168
1169                 /* if group was temporary one:
1170                  * - unassign all FCurves which were temporarily added to it
1171                  * - remove from list (but don't free as it's on the stack!)
1172                  */
1173                 if (agrp->flag & AGRP_TEMP) {
1174                         FCurve *fcu;
1175
1176                         for (fcu = agrp->channels.first; fcu; fcu = fcu->next) {
1177                                 fcu->grp = NULL;
1178                         }
1179
1180                         BLI_remlink(&act->groups, agrp);
1181                         break;
1182                 }
1183         }
1184 }
1185
1186 /* Change the order of anim-channels within action
1187  *      mode: REARRANGE_ANIMCHAN_*
1188  */
1189 static void rearrange_action_channels(bAnimContext *ac, bAction *act, eRearrangeAnimChan_Mode mode)
1190 {
1191         bActionGroup tgrp;
1192         ListBase anim_data_visible = {NULL, NULL};
1193         bool do_channels;
1194
1195         /* get rearranging function */
1196         AnimChanRearrangeFp rearrange_func = rearrange_get_mode_func(mode);
1197
1198         if (rearrange_func == NULL)
1199                 return;
1200
1201         /* make sure we're only operating with groups (vs a mixture of groups+curves) */
1202         split_groups_action_temp(act, &tgrp);
1203
1204         /* Filter visible data. */
1205         rearrange_animchannels_filter_visible(&anim_data_visible, ac, ANIMTYPE_GROUP);
1206
1207         /* rearrange groups first
1208          *      - the group's channels will only get considered if nothing happened when rearranging the groups
1209          *        i.e. the rearrange function returned 0
1210          */
1211         do_channels = (rearrange_animchannel_islands(&act->groups, rearrange_func, mode, ANIMTYPE_GROUP,
1212                                                      &anim_data_visible) == 0);
1213
1214         /* free temp data */
1215         BLI_freelistN(&anim_data_visible);
1216
1217         if (do_channels) {
1218                 bActionGroup *agrp;
1219
1220                 /* Filter visible data. */
1221                 rearrange_animchannels_filter_visible(&anim_data_visible, ac, ANIMTYPE_FCURVE);
1222
1223                 for (agrp = act->groups.first; agrp; agrp = agrp->next) {
1224                         /* only consider F-Curves if they're visible (group expanded) */
1225                         if (EXPANDED_AGRP(ac, agrp)) {
1226                                 rearrange_animchannel_islands(&agrp->channels, rearrange_func, mode, ANIMTYPE_FCURVE,
1227                                                               &anim_data_visible);
1228                         }
1229                 }
1230
1231                 /* free temp data */
1232                 BLI_freelistN(&anim_data_visible);
1233         }
1234
1235         /* assemble lists into one list (and clear moved tags) */
1236         join_groups_action_temp(act);
1237 }
1238
1239 /* ------------------- */
1240
1241 static void rearrange_nla_control_channels(bAnimContext *ac, AnimData *adt, eRearrangeAnimChan_Mode mode)
1242 {
1243         ListBase anim_data_visible = {NULL, NULL};
1244
1245         NlaTrack *nlt;
1246         NlaStrip *strip;
1247
1248         /* get rearranging function */
1249         AnimChanRearrangeFp rearrange_func = rearrange_get_mode_func(mode);
1250
1251         if (rearrange_func == NULL)
1252                 return;
1253
1254         /* skip if these curves aren't being shown */
1255         if (adt->flag & ADT_NLA_SKEYS_COLLAPSED)
1256                 return;
1257
1258         /* Filter visible data. */
1259         rearrange_animchannels_filter_visible(&anim_data_visible, ac, ANIMTYPE_NLACURVE);
1260
1261         /* we cannot rearrange between strips, but within each strip, we can rearrange those curves */
1262         for (nlt = adt->nla_tracks.first; nlt; nlt = nlt->next) {
1263                 for (strip = nlt->strips.first; strip; strip = strip->next) {
1264                         rearrange_animchannel_islands(&strip->fcurves, rearrange_func, mode, ANIMTYPE_NLACURVE,
1265                                                           &anim_data_visible);
1266                 }
1267         }
1268
1269         /* free temp data */
1270         BLI_freelistN(&anim_data_visible);
1271 }
1272
1273 /* ------------------- */
1274
1275 static void rearrange_gpencil_channels(bAnimContext *ac, eRearrangeAnimChan_Mode mode)
1276 {
1277         ListBase anim_data = {NULL, NULL};
1278         bAnimListElem *ale;
1279         int filter;
1280
1281         /* get rearranging function */
1282         AnimChanRearrangeFp rearrange_func = rearrange_get_mode_func(mode);
1283
1284         if (rearrange_func == NULL)
1285                 return;
1286
1287         /* get Grease Pencil datablocks */
1288         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_ANIMDATA);
1289         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
1290
1291         for (ale = anim_data.first; ale; ale = ale->next) {
1292                 ListBase anim_data_visible = {NULL, NULL};
1293                 bGPdata *gpd = ale->data;
1294
1295                 /* only consider layers if this datablock is open */
1296                 BLI_assert(ale->type == ANIMTYPE_GPDATABLOCK);
1297                 if ((gpd->flag & GP_DATA_EXPAND) == 0)
1298                         continue;
1299
1300                 /* Filter visible data. */
1301                 rearrange_animchannels_filter_visible(&anim_data_visible, ac, ANIMTYPE_GPLAYER);
1302
1303                 /* rearrange datablock's layers */
1304                 rearrange_animchannel_islands(&gpd->layers, rearrange_func, mode, ANIMTYPE_GPLAYER, &anim_data_visible);
1305
1306                 /* free visible layers data */
1307                 BLI_freelistN(&anim_data_visible);
1308         }
1309
1310         /* free GPD channel data */
1311         ANIM_animdata_freelist(&anim_data);
1312 }
1313
1314 /* ------------------- */
1315
1316 static int animchannels_rearrange_exec(bContext *C, wmOperator *op)
1317 {
1318         bAnimContext ac;
1319         eRearrangeAnimChan_Mode mode;
1320
1321         /* get editor data */
1322         if (ANIM_animdata_get_context(C, &ac) == 0)
1323                 return OPERATOR_CANCELLED;
1324
1325         /* get mode */
1326         mode = RNA_enum_get(op->ptr, "direction");
1327
1328         /* method to move channels depends on the editor */
1329         if (ac.datatype == ANIMCONT_GPENCIL) {
1330                 /* Grease Pencil channels */
1331                 rearrange_gpencil_channels(&ac, mode);
1332         }
1333         else if (ac.datatype == ANIMCONT_MASK) {
1334                 /* Grease Pencil channels */
1335                 printf("Mask does not supported for moving yet\n");
1336         }
1337         else if (ac.datatype == ANIMCONT_ACTION) {
1338                 /* Directly rearrange action's channels */
1339                 rearrange_action_channels(&ac, ac.data, mode);
1340         }
1341         else {
1342                 ListBase anim_data = {NULL, NULL};
1343                 bAnimListElem *ale;
1344                 int filter;
1345
1346                 /* get animdata blocks */
1347                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_ANIMDATA);
1348                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1349
1350                 for (ale = anim_data.first; ale; ale = ale->next) {
1351                         AnimData *adt = ale->data;
1352
1353                         switch (ac.datatype) {
1354                                 case ANIMCONT_NLA: /* NLA-tracks only */
1355                                         rearrange_nla_channels(&ac, adt, mode);
1356                                         break;
1357
1358                                 case ANIMCONT_DRIVERS: /* Drivers list only */
1359                                         rearrange_driver_channels(&ac, adt, mode);
1360                                         break;
1361
1362                                 case ANIMCONT_ACTION: /* Single Action only... */
1363                                 case ANIMCONT_SHAPEKEY: // DOUBLE CHECK ME...
1364                                 {
1365                                         if (adt->action)
1366                                                 rearrange_action_channels(&ac, adt->action, mode);
1367                                         else if (G.debug & G_DEBUG)
1368                                                 printf("Animdata has no action\n");
1369                                         break;
1370                                 }
1371
1372                                 default: /* DopeSheet/Graph Editor - Some Actions + NLA Control Curves */
1373                                 {
1374                                         /* NLA Control Curves */
1375                                         if (adt->nla_tracks.first)
1376                                                 rearrange_nla_control_channels(&ac, adt, mode);
1377
1378                                         /* Action */
1379                                         if (adt->action)
1380                                                 rearrange_action_channels(&ac, adt->action, mode);
1381                                         else if (G.debug & G_DEBUG)
1382                                                 printf("Animdata has no action\n");
1383                                         break;
1384                                 }
1385                         }
1386                 }
1387
1388                 /* free temp data */
1389                 ANIM_animdata_freelist(&anim_data);
1390         }
1391
1392         /* send notifier that things have changed */
1393         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
1394
1395         return OPERATOR_FINISHED;
1396 }
1397
1398 static void ANIM_OT_channels_move(wmOperatorType *ot)
1399 {
1400         /* identifiers */
1401         ot->name = "Move Channels";
1402         ot->idname = "ANIM_OT_channels_move";
1403         ot->description = "Rearrange selected animation channels";
1404
1405         /* api callbacks */
1406         ot->exec = animchannels_rearrange_exec;
1407         ot->poll = animedit_poll_channels_nla_tweakmode_off;
1408
1409         /* flags */
1410         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1411
1412         /* props */
1413         ot->prop = RNA_def_enum(ot->srna, "direction", prop_animchannel_rearrange_types, REARRANGE_ANIMCHAN_DOWN, "Direction", "");
1414 }
1415
1416 /* ******************** Group Channel Operator ************************ */
1417
1418 static int animchannels_grouping_poll(bContext *C)
1419 {
1420         ScrArea *sa = CTX_wm_area(C);
1421         SpaceLink *sl;
1422
1423         /* channels region test */
1424         /* TODO: could enhance with actually testing if channels region? */
1425         if (ELEM(NULL, sa, CTX_wm_region(C)))
1426                 return 0;
1427
1428         /* animation editor test - must be suitable modes only */
1429         sl = CTX_wm_space_data(C);
1430
1431         switch (sa->spacetype) {
1432                 /* supported... */
1433                 case SPACE_ACTION:
1434                 {
1435                         SpaceAction *saction = (SpaceAction *)sl;
1436
1437                         /* dopesheet and action only - all others are for other datatypes or have no groups */
1438                         if (ELEM(saction->mode, SACTCONT_ACTION, SACTCONT_DOPESHEET) == 0)
1439                                 return 0;
1440
1441                         break;
1442                 }
1443                 case SPACE_IPO:
1444                 {
1445                         SpaceIpo *sipo = (SpaceIpo *)sl;
1446
1447                         /* drivers can't have groups... */
1448                         if (sipo->mode != SIPO_MODE_ANIMATION)
1449                                 return 0;
1450
1451                         break;
1452                 }
1453                 /* unsupported... */
1454                 default:
1455                         return 0;
1456         }
1457
1458         return 1;
1459 }
1460
1461 /* ----------------------------------------------------------- */
1462
1463 static void animchannels_group_channels(bAnimContext *ac, bAnimListElem *adt_ref, const char name[])
1464 {
1465         AnimData *adt = adt_ref->adt;
1466         bAction *act = adt->action;
1467
1468         if (act) {
1469                 ListBase anim_data = {NULL, NULL};
1470                 int filter;
1471
1472                 /* find selected F-Curves to re-group */
1473                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL);
1474                 ANIM_animdata_filter(ac, &anim_data, filter, adt_ref, ANIMCONT_CHANNEL);
1475
1476                 if (anim_data.first) {
1477                         bActionGroup *agrp;
1478                         bAnimListElem *ale;
1479
1480                         /* create new group, which should now be part of the action */
1481                         agrp = action_groups_add_new(act, name);
1482                         BLI_assert(agrp != NULL);
1483
1484                         /* transfer selected F-Curves across to new group  */
1485                         for (ale = anim_data.first; ale; ale = ale->next) {
1486                                 FCurve *fcu = (FCurve *)ale->data;
1487                                 bActionGroup *grp = fcu->grp;
1488
1489                                 /* remove F-Curve from group, then group too if it is now empty */
1490                                 action_groups_remove_channel(act, fcu);
1491
1492                                 if ((grp) && BLI_listbase_is_empty(&grp->channels)) {
1493                                         BLI_freelinkN(&act->groups, grp);
1494                                 }
1495
1496                                 /* add F-Curve to group */
1497                                 action_groups_add_channel(act, agrp, fcu);
1498                         }
1499                 }
1500
1501                 /* cleanup */
1502                 ANIM_animdata_freelist(&anim_data);
1503         }
1504 }
1505
1506 static int animchannels_group_exec(bContext *C, wmOperator *op)
1507 {
1508         bAnimContext ac;
1509         char name[MAX_NAME];
1510
1511         /* get editor data */
1512         if (ANIM_animdata_get_context(C, &ac) == 0)
1513                 return OPERATOR_CANCELLED;
1514
1515         /* get name for new group */
1516         RNA_string_get(op->ptr, "name", name);
1517
1518         /* XXX: name for group should never be empty... */
1519         if (name[0]) {
1520                 ListBase anim_data = {NULL, NULL};
1521                 bAnimListElem *ale;
1522                 int filter;
1523
1524                 /* handle each animdata block separately, so that the regrouping doesn't flow into blocks  */
1525                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_ANIMDATA | ANIMFILTER_NODUPLIS);
1526                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1527
1528                 for (ale = anim_data.first; ale; ale = ale->next) {
1529                         animchannels_group_channels(&ac, ale, name);
1530                 }
1531
1532                 /* free temp data */
1533                 ANIM_animdata_freelist(&anim_data);
1534
1535                 /* updatss */
1536                 WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
1537         }
1538
1539         return OPERATOR_FINISHED;
1540 }
1541
1542 static void ANIM_OT_channels_group(wmOperatorType *ot)
1543 {
1544         /* identifiers */
1545         ot->name = "Group Channels";
1546         ot->idname = "ANIM_OT_channels_group";
1547         ot->description = "Add selected F-Curves to a new group";
1548
1549         /* callbacks */
1550         ot->invoke = WM_operator_props_popup;
1551         ot->exec = animchannels_group_exec;
1552         ot->poll = animchannels_grouping_poll;
1553
1554         /* flags */
1555         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1556
1557         /* props */
1558         ot->prop = RNA_def_string(ot->srna, "name", "New Group",
1559                                   sizeof(((bActionGroup *)NULL)->name),
1560                                   "Name", "Name of newly created group");
1561         /* RNA_def_property_flag(ot->prop, PROP_SKIP_SAVE); */ /* XXX: still not too sure about this - keeping same text is confusing... */
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         DEG_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 int 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 int 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 #if 0
2687                         bDopeSheet *ads = (bDopeSheet *)ac->data;
2688                         Scene *sce = (Scene *)ads->source;
2689 #endif
2690                         ViewLayer *view_layer = ac->view_layer;
2691                         Base *base = (Base *)ale->data;
2692                         Object *ob = base->object;
2693                         AnimData *adt = ob->adt;
2694
2695                         /* set selection status */
2696                         if (base->flag & BASE_SELECTABLED) {
2697                                 if (selectmode == SELECT_INVERT) {
2698                                         /* swap select */
2699                                         ED_object_base_select(base, BA_INVERT);
2700                                         BKE_scene_object_base_flag_sync_from_base(base);
2701
2702                                         if (adt) adt->flag ^= ADT_UI_SELECTED;
2703                                 }
2704                                 else {
2705                                         Base *b;
2706
2707                                         /* deselect all */
2708                                         /* TODO: should this deselect all other types of channels too? */
2709                                         for (b = view_layer->object_bases.first; b; b = b->next) {
2710                                                 ED_object_base_select(b, BA_DESELECT);
2711                                                 BKE_scene_object_base_flag_sync_from_base(b);
2712                                                 if (b->object->adt) b->object->adt->flag &= ~(ADT_UI_SELECTED | ADT_UI_ACTIVE);
2713                                         }
2714
2715                                         /* select object now */
2716                                         ED_object_base_select(base, BA_SELECT);
2717                                         BKE_scene_object_base_flag_sync_from_base(base);
2718                                         if (adt) adt->flag |= ADT_UI_SELECTED;
2719                                 }
2720
2721                                 /* change active object - regardless of whether it is now selected [T37883] */
2722                                 ED_object_base_activate(C, base); /* adds notifier */
2723
2724                                 if ((adt) && (adt->flag & ADT_UI_SELECTED))
2725                                         adt->flag |= ADT_UI_ACTIVE;
2726
2727                                 /* ensure we exit editmode on whatever object was active before to avoid getting stuck there - T48747 */
2728                                 if (ob != CTX_data_edit_object(C)) {
2729                                         ED_object_editmode_exit(C, EM_FREEDATA | EM_WAITCURSOR);
2730                                 }
2731
2732                                 notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
2733                         }
2734                         break;
2735                 }
2736                 case ANIMTYPE_FILLACTD: /* Action Expander */
2737                 case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
2738                 case ANIMTYPE_DSLAM:
2739                 case ANIMTYPE_DSCAM:
2740                 case ANIMTYPE_DSCACHEFILE:
2741                 case ANIMTYPE_DSCUR:
2742                 case ANIMTYPE_DSSKEY:
2743                 case ANIMTYPE_DSWOR:
2744                 case ANIMTYPE_DSPART:
2745                 case ANIMTYPE_DSMBALL:
2746                 case ANIMTYPE_DSARM:
2747                 case ANIMTYPE_DSMESH:
2748                 case ANIMTYPE_DSNTREE:
2749                 case ANIMTYPE_DSTEX:
2750                 case ANIMTYPE_DSLAT:
2751                 case ANIMTYPE_DSLINESTYLE:
2752                 case ANIMTYPE_DSSPK:
2753                 case ANIMTYPE_DSGPENCIL:
2754                 case ANIMTYPE_DSMCLIP:
2755                 {
2756                         /* sanity checking... */
2757                         if (ale->adt) {
2758                                 /* select/deselect */
2759                                 if (selectmode == SELECT_INVERT) {
2760                                         /* inverse selection status of this AnimData block only */
2761                                         ale->adt->flag ^= ADT_UI_SELECTED;
2762                                 }
2763                                 else {
2764                                         /* select AnimData block by itself */
2765                                         ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2766                                         ale->adt->flag |= ADT_UI_SELECTED;
2767                                 }
2768
2769                                 /* set active? */
2770                                 if ((ale->adt) && (ale->adt->flag & ADT_UI_SELECTED))
2771                                         ale->adt->flag |= ADT_UI_ACTIVE;
2772                         }
2773
2774                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
2775                         break;
2776                 }
2777                 case ANIMTYPE_GROUP:
2778                 {
2779                         bActionGroup *agrp = (bActionGroup *)ale->data;
2780
2781                         Object *ob = NULL;
2782                         bPoseChannel *pchan = NULL;
2783
2784
2785                         /* Armatures-Specific Feature:
2786                          * Since groups are used to collect F-Curves of the same Bone by default
2787                          * (via Keying Sets) so that they can be managed better, we try to make
2788                          * things here easier for animators by mapping group selection to bone
2789                          * selection.
2790                          *
2791                          * Only do this if "Only Selected" dopesheet filter is not active, or else it
2792                          * becomes too unpredictable/tricky to manage
2793                          */
2794                         if ((ac->ads->filterflag & ADS_FILTER_ONLYSEL) == 0) {
2795                                 if ((ale->id) && (GS(ale->id->name) == ID_OB)) {
2796                                         ob = (Object *)ale->id;
2797
2798                                         if (ob->type == OB_ARMATURE) {
2799                                                 /* Assume for now that any group with corresponding name is what we want
2800                                                  * (i.e. for an armature whose location is animated, things would break
2801                                                  * if the user were to add a bone named "Location").
2802                                                  *
2803                                                  * TODO: check the first F-Curve or so to be sure...
2804                                                  */
2805                                                 pchan = BKE_pose_channel_find_name(ob->pose, agrp->name);
2806                                         }
2807                                 }
2808                         }
2809
2810                         /* select/deselect group */
2811                         if (selectmode == SELECT_INVERT) {
2812                                 /* inverse selection status of this group only */
2813                                 agrp->flag ^= AGRP_SELECTED;
2814                         }
2815                         else if (selectmode == -1) {
2816                                 /* select all in group (and deselect everthing else) */
2817                                 FCurve *fcu;
2818
2819                                 /* deselect all other channels */
2820                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2821                                 if (pchan) ED_pose_deselect_all(ob, SEL_DESELECT, false);
2822
2823                                 /* only select channels in group and group itself */
2824                                 for (fcu = agrp->channels.first; fcu && fcu->grp == agrp; fcu = fcu->next)
2825                                         fcu->flag |= FCURVE_SELECTED;
2826                                 agrp->flag |= AGRP_SELECTED;
2827                         }
2828                         else {
2829                                 /* select group by itself */
2830                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2831                                 if (pchan) ED_pose_deselect_all(ob, SEL_DESELECT, false);
2832
2833                                 agrp->flag |= AGRP_SELECTED;
2834                         }
2835
2836                         /* if group is selected now, make group the 'active' one in the visible list */
2837                         if (agrp->flag & AGRP_SELECTED) {
2838                                 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, agrp, ANIMTYPE_GROUP);
2839                                 if (pchan) ED_pose_bone_select(ob, pchan, true);
2840                         }
2841                         else {
2842                                 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, NULL, ANIMTYPE_GROUP);
2843                                 if (pchan) ED_pose_bone_select(ob, pchan, false);
2844                         }
2845
2846                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
2847                         break;
2848                 }
2849                 case ANIMTYPE_FCURVE:
2850                 case ANIMTYPE_NLACURVE:
2851                 {
2852                         FCurve *fcu = (FCurve *)ale->data;
2853
2854                         /* select/deselect */
2855                         if (selectmode == SELECT_INVERT) {
2856                                 /* inverse selection status of this F-Curve only */
2857                                 fcu->flag ^= FCURVE_SELECTED;
2858                         }
2859                         else {
2860                                 /* select F-Curve by itself */
2861                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2862                                 fcu->flag |= FCURVE_SELECTED;
2863                         }
2864
2865                         /* if F-Curve is selected now, make F-Curve the 'active' one in the visible list */
2866                         if (fcu->flag & FCURVE_SELECTED)
2867                                 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, fcu, ale->type);
2868
2869                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
2870                         break;
2871                 }
2872                 case ANIMTYPE_SHAPEKEY:
2873                 {
2874                         KeyBlock *kb = (KeyBlock *)ale->data;
2875
2876                         /* select/deselect */
2877                         if (selectmode == SELECT_INVERT) {
2878                                 /* inverse selection status of this ShapeKey only */
2879                                 kb->flag ^= KEYBLOCK_SEL;
2880                         }
2881                         else {
2882                                 /* select ShapeKey by itself */
2883                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2884                                 kb->flag |= KEYBLOCK_SEL;
2885                         }
2886
2887                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
2888                         break;
2889                 }
2890                 case ANIMTYPE_NLACONTROLS:
2891                 {
2892                         AnimData *adt = (AnimData *)ale->data;
2893
2894                         /* toggle expand
2895                          *   - Although the triangle widget already allows this, since there's nothing else that can be done here now,
2896                          *     let's just use it for easier expand/collapse for now
2897                          */
2898                         adt->flag ^= ADT_NLA_SKEYS_COLLAPSED;
2899
2900                         notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
2901                         break;
2902                 }
2903                 case ANIMTYPE_GPDATABLOCK:
2904                 {
2905                         bGPdata *gpd = (bGPdata *)ale->data;
2906
2907                         /* toggle expand
2908                          *      - although the triangle widget already allows this, the whole channel can also be used for this purpose
2909                          */
2910                         gpd->flag ^= GP_DATA_EXPAND;
2911
2912                         notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
2913                         break;
2914                 }
2915                 case ANIMTYPE_GPLAYER:
2916                 {
2917                         bGPDlayer *gpl = (bGPDlayer *)ale->data;
2918
2919                         /* select/deselect */
2920                         if (selectmode == SELECT_INVERT) {
2921                                 /* invert selection status of this layer only */
2922                                 gpl->flag ^= GP_LAYER_SELECT;
2923                         }
2924                         else {
2925                                 /* select layer by itself */
2926                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2927                                 gpl->flag |= GP_LAYER_SELECT;
2928                         }
2929
2930                         /* change active layer, if this is selected (since we must always have an active layer) */
2931                         if (gpl->flag & GP_LAYER_SELECT) {
2932                                 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, gpl, ANIMTYPE_GPLAYER);
2933                         }
2934
2935                         WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL); /* Grease Pencil updates */
2936                         notifierFlags |= (ND_ANIMCHAN | NA_EDITED); /* Animation Ediotrs updates */
2937                         break;
2938                 }
2939                 case ANIMTYPE_MASKDATABLOCK:
2940                 {
2941                         Mask *mask = (Mask *)ale->data;
2942
2943                         /* toggle expand
2944                          *      - although the triangle widget already allows this, the whole channel can also be used for this purpose
2945                          */
2946                         mask->flag ^= MASK_ANIMF_EXPAND;
2947
2948                         notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
2949                         break;
2950                 }
2951                 case ANIMTYPE_MASKLAYER:
2952                 {
2953                         MaskLayer *masklay = (MaskLayer *)ale->data;
2954
2955                         /* select/deselect */
2956                         if (selectmode == SELECT_INVERT) {
2957                                 /* invert selection status of this layer only */
2958                                 masklay->flag ^= MASK_LAYERFLAG_SELECT;
2959                         }
2960                         else {
2961                                 /* select layer by itself */
2962                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2963                                 masklay->flag |= MASK_LAYERFLAG_SELECT;
2964                         }
2965
2966                         notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
2967                         break;
2968                 }
2969                 default:
2970                         if (G.debug & G_DEBUG)
2971                                 printf("Error: Invalid channel type in mouse_anim_channels()\n");
2972                         break;
2973         }
2974
2975         /* free channels */
2976         ANIM_animdata_freelist(&anim_data);
2977
2978         /* return notifier flags */
2979         return notifierFlags;
2980 }
2981
2982 /* ------------------- */
2983
2984 /* handle clicking */
2985 static int animchannels_mouseclick_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2986 {
2987         bAnimContext ac;
2988         ARegion *ar;
2989         View2D *v2d;
2990         int channel_index;
2991         int notifierFlags = 0;
2992         short selectmode;
2993         float x, y;
2994
2995