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