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