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         /* always try to add to last island if possible */
885         tReorderChannelIsland *island = islands->last;
886         bool is_sel = false, is_untouchable = false;
887
888         /* get flags - selected and untouchable from the channel */
889         switch (type) {
890                 case ANIMTYPE_GROUP:
891                 {
892                         bActionGroup *agrp = (bActionGroup *)channel;
893
894                         is_sel = SEL_AGRP(agrp);
895                         is_untouchable = (agrp->flag & AGRP_TEMP) != 0;
896                         break;
897                 }
898                 case ANIMTYPE_FCURVE:
899                 case ANIMTYPE_NLACURVE:
900                 {
901                         FCurve *fcu = (FCurve *)channel;
902
903                         is_sel = SEL_FCU(fcu);
904                         break;
905                 }
906                 case ANIMTYPE_NLATRACK:
907                 {
908                         NlaTrack *nlt = (NlaTrack *)channel;
909
910                         is_sel = SEL_NLT(nlt);
911                         break;
912                 }
913                 case ANIMTYPE_GPLAYER:
914                 {
915                         bGPDlayer *gpl = (bGPDlayer *)channel;
916
917                         is_sel = SEL_GPL(gpl);
918                         break;
919                 }
920                 default:
921                         printf("rearrange_animchannel_add_to_islands(): don't know how to handle channels of type %u\n", type);
922                         return;
923         }
924
925         /* do we need to add to a new island? */
926         if (/* 1) no islands yet */
927             (island == NULL) ||
928             /* 2) unselected islands have single channels only - to allow up/down movement */
929             ((island->flag & REORDER_ISLAND_SELECTED) == 0) ||
930             /* 3) if channel is unselected, stop existing island (it was either wrong sel status, or full already) */
931             (is_sel == 0) ||
932             /* 4) hidden status changes */
933             ((island->flag & REORDER_ISLAND_HIDDEN) != is_hidden)
934             )
935         {
936                 /* create a new island now */
937                 island = MEM_callocN(sizeof(tReorderChannelIsland), "tReorderChannelIsland");
938                 BLI_addtail(islands, island);
939
940                 if (is_sel)
941                         island->flag |= REORDER_ISLAND_SELECTED;
942                 if (is_untouchable)
943                         island->flag |= REORDER_ISLAND_UNTOUCHABLE;
944                 if (is_hidden)
945                         island->flag |= REORDER_ISLAND_HIDDEN;
946         }
947
948         /* add channel to island - need to remove it from its existing list first though */
949         BLI_remlink(srcList, channel);
950         BLI_addtail(&island->channels, channel);
951 }
952
953 /* flatten islands out into a single list again */
954 static void rearrange_animchannel_flatten_islands(ListBase *islands, ListBase *srcList)
955 {
956         tReorderChannelIsland *island, *isn = NULL;
957
958         /* make sure srcList is empty now */
959         BLI_assert(BLI_listbase_is_empty(srcList));
960
961         /* go through merging islands */
962         for (island = islands->first; island; island = isn) {
963                 isn = island->next;
964
965                 /* merge island channels back to main list, then delete the island */
966                 BLI_movelisttolist(srcList, &island->channels);
967                 BLI_freelinkN(islands, island);
968         }
969 }
970
971 /* ............................. */
972
973 /* get a list of all bAnimListElem's of a certain type which are currently visible */
974 static void rearrange_animchannels_filter_visible(ListBase *anim_data_visible, bAnimContext *ac, eAnim_ChannelType type)
975 {
976         ListBase anim_data = {NULL, NULL};
977         bAnimListElem *ale, *ale_next;
978         int filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
979
980         /* get all visible channels */
981         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
982
983         /* now, only keep the ones that are of the types we are interested in */
984         for (ale = anim_data.first; ale; ale = ale_next) {
985                 ale_next = ale->next;
986
987                 if (ale->type != type) {
988                         BLI_freelinkN(&anim_data, ale);
989                 }
990         }
991
992         /* return cleaned up list */
993         *anim_data_visible = anim_data;
994 }
995
996 /* performing rearranging of channels using islands */
997 static bool rearrange_animchannel_islands(ListBase *list, AnimChanRearrangeFp rearrange_func,
998                                           eRearrangeAnimChan_Mode mode, eAnim_ChannelType type,
999                                           ListBase *anim_data_visible)
1000 {
1001         ListBase islands = {NULL, NULL};
1002         Link *channel, *chanNext = NULL;
1003         bool done = false;
1004
1005         /* don't waste effort on an empty list */
1006         if (BLI_listbase_is_empty(list))
1007                 return 0;
1008
1009         /* group channels into islands */
1010         for (channel = list->first; channel; channel = chanNext) {
1011                 /* find out whether this channel is present in anim_data_visible or not! */
1012                 const bool is_hidden = (BLI_findptr(anim_data_visible, channel, offsetof(bAnimListElem, data)) == NULL);
1013                 chanNext = channel->next;
1014                 rearrange_animchannel_add_to_islands(&islands, list, channel, type, is_hidden);
1015         }
1016
1017         /* perform moving of selected islands now, but only if there is more than one of 'em so that something will happen
1018          * - scanning of the list is performed in the opposite direction to the direction we're moving things, so that we
1019          *   shouldn't need to encounter items we've moved already
1020          */
1021         if (islands.first != islands.last) {
1022                 tReorderChannelIsland *first = (mode > 0) ? islands.last : islands.first;
1023                 tReorderChannelIsland *island, *isn = NULL;
1024
1025                 for (island = first; island; island = isn) {
1026                         isn = (mode > 0) ? island->prev : island->next;
1027
1028                         /* perform rearranging */
1029                         if (rearrange_func(&islands, island)) {
1030                                 island->flag |= REORDER_ISLAND_MOVED;
1031                                 done = true;
1032                         }
1033                 }
1034         }
1035
1036         /* ungroup islands */
1037         rearrange_animchannel_flatten_islands(&islands, list);
1038
1039         /* did we do anything? */
1040         return done;
1041 }
1042
1043 /* NLA Specific Stuff ----------------------------------------------------- */
1044
1045 /* Change the order NLA Tracks within NLA Stack
1046  * ! NLA tracks are displayed in opposite order, so directions need care
1047  * mode: REARRANGE_ANIMCHAN_*
1048  */
1049 static void rearrange_nla_channels(bAnimContext *ac, AnimData *adt, eRearrangeAnimChan_Mode mode)
1050 {
1051         AnimChanRearrangeFp rearrange_func;
1052         ListBase anim_data_visible = {NULL, NULL};
1053
1054         /* hack: invert mode so that functions will work in right order */
1055         mode *= -1;
1056
1057         /* get rearranging function */
1058         rearrange_func = rearrange_get_mode_func(mode);
1059         if (rearrange_func == NULL)
1060                 return;
1061
1062         /* Filter visible data. */
1063         rearrange_animchannels_filter_visible(&anim_data_visible, ac, ANIMTYPE_NLATRACK);
1064
1065         /* perform rearranging on tracks list */
1066         rearrange_animchannel_islands(&adt->nla_tracks, rearrange_func, mode, ANIMTYPE_NLATRACK, &anim_data_visible);
1067
1068         /* free temp data */
1069         BLI_freelistN(&anim_data_visible);
1070 }
1071
1072 /* Drivers Specific Stuff ------------------------------------------------- */
1073
1074 /* Change the order drivers within AnimData block
1075  * mode: REARRANGE_ANIMCHAN_*
1076  */
1077 static void rearrange_driver_channels(bAnimContext *ac, AnimData *adt, eRearrangeAnimChan_Mode mode)
1078 {
1079         /* get rearranging function */
1080         AnimChanRearrangeFp rearrange_func = rearrange_get_mode_func(mode);
1081         ListBase anim_data_visible = {NULL, NULL};
1082
1083         if (rearrange_func == NULL)
1084                 return;
1085
1086         /* only consider drivers if they're accessible */
1087         if (EXPANDED_DRVD(adt) == 0)
1088                 return;
1089
1090         /* Filter visible data. */
1091         rearrange_animchannels_filter_visible(&anim_data_visible, ac, ANIMTYPE_FCURVE);
1092
1093         /* perform rearranging on drivers list (drivers are really just F-Curves) */
1094         rearrange_animchannel_islands(&adt->drivers, rearrange_func, mode, ANIMTYPE_FCURVE, &anim_data_visible);
1095
1096         /* free temp data */
1097         BLI_freelistN(&anim_data_visible);
1098 }
1099
1100 /* Action Specific Stuff ------------------------------------------------- */
1101
1102 /* make sure all action-channels belong to a group (and clear action's list) */
1103 static void split_groups_action_temp(bAction *act, bActionGroup *tgrp)
1104 {
1105         bActionGroup *agrp;
1106         FCurve *fcu;
1107
1108         if (act == NULL)
1109                 return;
1110
1111         /* Separate F-Curves into lists per group */
1112         for (agrp = act->groups.first; agrp; agrp = agrp->next) {
1113                 if (agrp->channels.first) {
1114                         fcu = agrp->channels.last;
1115                         act->curves.first = fcu->next;
1116
1117                         fcu = agrp->channels.first;
1118                         fcu->prev = NULL;
1119
1120                         fcu = agrp->channels.last;
1121                         fcu->next = NULL;
1122                 }
1123         }
1124
1125         /* Initialize memory for temp-group */
1126         memset(tgrp, 0, sizeof(bActionGroup));
1127         tgrp->flag |= (AGRP_EXPANDED | AGRP_TEMP);
1128         BLI_strncpy(tgrp->name, "#TempGroup", sizeof(tgrp->name));
1129
1130         /* Move any action-channels not already moved, to the temp group */
1131         if (act->curves.first) {
1132                 /* start of list */
1133                 fcu = act->curves.first;
1134                 fcu->prev = NULL;
1135                 tgrp->channels.first = fcu;
1136                 act->curves.first = NULL;
1137
1138                 /* end of list */
1139                 fcu = act->curves.last;
1140                 fcu->next = NULL;
1141                 tgrp->channels.last = fcu;
1142                 act->curves.last = NULL;
1143
1144                 /* ensure that all of these get their group set to this temp group
1145                  * (so that visibility filtering works)
1146                  */
1147                 for (fcu = tgrp->channels.first; fcu; fcu = fcu->next) {
1148                         fcu->grp = tgrp;
1149                 }
1150         }
1151
1152         /* Add temp-group to list */
1153         BLI_addtail(&act->groups, tgrp);
1154 }
1155
1156 /* link lists of channels that groups have */
1157 static void join_groups_action_temp(bAction *act)
1158 {
1159         bActionGroup *agrp;
1160
1161         for (agrp = act->groups.first; agrp; agrp = agrp->next) {
1162                 ListBase tempGroup;
1163
1164                 /* add list of channels to action's channels */
1165                 tempGroup = agrp->channels;
1166                 BLI_movelisttolist(&act->curves, &agrp->channels);
1167                 agrp->channels = tempGroup;
1168
1169                 /* clear moved flag */
1170                 agrp->flag &= ~AGRP_MOVED;
1171
1172                 /* if group was temporary one:
1173                  * - unassign all FCurves which were temporarily added to it
1174                  * - remove from list (but don't free as it's on the stack!)
1175                  */
1176                 if (agrp->flag & AGRP_TEMP) {
1177                         FCurve *fcu;
1178
1179                         for (fcu = agrp->channels.first; fcu; fcu = fcu->next) {
1180                                 fcu->grp = NULL;
1181                         }
1182
1183                         BLI_remlink(&act->groups, agrp);
1184                         break;
1185                 }
1186         }
1187 }
1188
1189 /* Change the order of anim-channels within action
1190  * mode: REARRANGE_ANIMCHAN_*
1191  */
1192 static void rearrange_action_channels(bAnimContext *ac, bAction *act, eRearrangeAnimChan_Mode mode)
1193 {
1194         bActionGroup tgrp;
1195         ListBase anim_data_visible = {NULL, NULL};
1196         bool do_channels;
1197
1198         /* get rearranging function */
1199         AnimChanRearrangeFp rearrange_func = rearrange_get_mode_func(mode);
1200
1201         if (rearrange_func == NULL)
1202                 return;
1203
1204         /* make sure we're only operating with groups (vs a mixture of groups+curves) */
1205         split_groups_action_temp(act, &tgrp);
1206
1207         /* Filter visible data. */
1208         rearrange_animchannels_filter_visible(&anim_data_visible, ac, ANIMTYPE_GROUP);
1209
1210         /* rearrange groups first
1211          * - the group's channels will only get considered if nothing happened when rearranging the groups
1212          *   i.e. the rearrange function returned 0
1213          */
1214         do_channels = (rearrange_animchannel_islands(&act->groups, rearrange_func, mode, ANIMTYPE_GROUP,
1215                                                      &anim_data_visible) == 0);
1216
1217         /* free temp data */
1218         BLI_freelistN(&anim_data_visible);
1219
1220         if (do_channels) {
1221                 bActionGroup *agrp;
1222
1223                 /* Filter visible data. */
1224                 rearrange_animchannels_filter_visible(&anim_data_visible, ac, ANIMTYPE_FCURVE);
1225
1226                 for (agrp = act->groups.first; agrp; agrp = agrp->next) {
1227                         /* only consider F-Curves if they're visible (group expanded) */
1228                         if (EXPANDED_AGRP(ac, agrp)) {
1229                                 rearrange_animchannel_islands(&agrp->channels, rearrange_func, mode, ANIMTYPE_FCURVE,
1230                                                               &anim_data_visible);
1231                         }
1232                 }
1233
1234                 /* free temp data */
1235                 BLI_freelistN(&anim_data_visible);
1236         }
1237
1238         /* assemble lists into one list (and clear moved tags) */
1239         join_groups_action_temp(act);
1240 }
1241
1242 /* ------------------- */
1243
1244 static void rearrange_nla_control_channels(bAnimContext *ac, AnimData *adt, eRearrangeAnimChan_Mode mode)
1245 {
1246         ListBase anim_data_visible = {NULL, NULL};
1247
1248         NlaTrack *nlt;
1249         NlaStrip *strip;
1250
1251         /* get rearranging function */
1252         AnimChanRearrangeFp rearrange_func = rearrange_get_mode_func(mode);
1253
1254         if (rearrange_func == NULL)
1255                 return;
1256
1257         /* skip if these curves aren't being shown */
1258         if (adt->flag & ADT_NLA_SKEYS_COLLAPSED)
1259                 return;
1260
1261         /* Filter visible data. */
1262         rearrange_animchannels_filter_visible(&anim_data_visible, ac, ANIMTYPE_NLACURVE);
1263
1264         /* we cannot rearrange between strips, but within each strip, we can rearrange those curves */
1265         for (nlt = adt->nla_tracks.first; nlt; nlt = nlt->next) {
1266                 for (strip = nlt->strips.first; strip; strip = strip->next) {
1267                         rearrange_animchannel_islands(&strip->fcurves, rearrange_func, mode, ANIMTYPE_NLACURVE,
1268                                                           &anim_data_visible);
1269                 }
1270         }
1271
1272         /* free temp data */
1273         BLI_freelistN(&anim_data_visible);
1274 }
1275
1276 /* ------------------- */
1277
1278 static void rearrange_gpencil_channels(bAnimContext *ac, eRearrangeAnimChan_Mode mode)
1279 {
1280         ListBase anim_data = {NULL, NULL};
1281         bAnimListElem *ale;
1282         int filter;
1283
1284         /* get rearranging function */
1285         AnimChanRearrangeFp rearrange_func = rearrange_get_mode_func(mode);
1286
1287         if (rearrange_func == NULL)
1288                 return;
1289
1290         /* get Grease Pencil datablocks */
1291         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_ANIMDATA);
1292         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
1293
1294         for (ale = anim_data.first; ale; ale = ale->next) {
1295                 ListBase anim_data_visible = {NULL, NULL};
1296                 bGPdata *gpd = ale->data;
1297
1298                 /* only consider layers if this datablock is open */
1299                 BLI_assert(ale->type == ANIMTYPE_GPDATABLOCK);
1300                 if ((gpd->flag & GP_DATA_EXPAND) == 0)
1301                         continue;
1302
1303                 /* Filter visible data. */
1304                 rearrange_animchannels_filter_visible(&anim_data_visible, ac, ANIMTYPE_GPLAYER);
1305
1306                 /* rearrange datablock's layers */
1307                 rearrange_animchannel_islands(&gpd->layers, rearrange_func, mode, ANIMTYPE_GPLAYER, &anim_data_visible);
1308
1309                 /* free visible layers data */
1310                 BLI_freelistN(&anim_data_visible);
1311         }
1312
1313         /* free GPD channel data */
1314         ANIM_animdata_freelist(&anim_data);
1315 }
1316
1317 /* ------------------- */
1318
1319 static int animchannels_rearrange_exec(bContext *C, wmOperator *op)
1320 {
1321         bAnimContext ac;
1322         eRearrangeAnimChan_Mode mode;
1323
1324         /* get editor data */
1325         if (ANIM_animdata_get_context(C, &ac) == 0)
1326                 return OPERATOR_CANCELLED;
1327
1328         /* get mode */
1329         mode = RNA_enum_get(op->ptr, "direction");
1330
1331         /* method to move channels depends on the editor */
1332         if (ac.datatype == ANIMCONT_GPENCIL) {
1333                 /* Grease Pencil channels */
1334                 rearrange_gpencil_channels(&ac, mode);
1335         }
1336         else if (ac.datatype == ANIMCONT_MASK) {
1337                 /* Grease Pencil channels */
1338                 printf("Mask does not supported for moving yet\n");
1339         }
1340         else if (ac.datatype == ANIMCONT_ACTION) {
1341                 /* Directly rearrange action's channels */
1342                 rearrange_action_channels(&ac, ac.data, mode);
1343         }
1344         else {
1345                 ListBase anim_data = {NULL, NULL};
1346                 bAnimListElem *ale;
1347                 int filter;
1348
1349                 /* get animdata blocks */
1350                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_ANIMDATA);
1351                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1352
1353                 for (ale = anim_data.first; ale; ale = ale->next) {
1354                         AnimData *adt = ale->data;
1355
1356                         switch (ac.datatype) {
1357                                 case ANIMCONT_NLA: /* NLA-tracks only */
1358                                         rearrange_nla_channels(&ac, adt, mode);
1359                                         DEG_id_tag_update(ale->id, ID_RECALC_ANIMATION | ID_RECALC_COPY_ON_WRITE);
1360                                         break;
1361
1362                                 case ANIMCONT_DRIVERS: /* Drivers list only */
1363                                         rearrange_driver_channels(&ac, adt, mode);
1364                                         break;
1365
1366                                 case ANIMCONT_ACTION: /* Single Action only... */
1367                                 case ANIMCONT_SHAPEKEY: // DOUBLE CHECK ME...
1368                                 {
1369                                         if (adt->action)
1370                                                 rearrange_action_channels(&ac, adt->action, mode);
1371                                         else if (G.debug & G_DEBUG)
1372                                                 printf("Animdata has no action\n");
1373                                         break;
1374                                 }
1375
1376                                 default: /* DopeSheet/Graph Editor - Some Actions + NLA Control Curves */
1377                                 {
1378                                         /* NLA Control Curves */
1379                                         if (adt->nla_tracks.first)
1380                                                 rearrange_nla_control_channels(&ac, adt, mode);
1381
1382                                         /* Action */
1383                                         if (adt->action)
1384                                                 rearrange_action_channels(&ac, adt->action, mode);
1385                                         else if (G.debug & G_DEBUG)
1386                                                 printf("Animdata has no action\n");
1387                                         break;
1388                                 }
1389                         }
1390                 }
1391
1392                 /* free temp data */
1393                 ANIM_animdata_freelist(&anim_data);
1394         }
1395
1396         /* send notifier that things have changed */
1397         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
1398
1399         return OPERATOR_FINISHED;
1400 }
1401
1402 static void ANIM_OT_channels_move(wmOperatorType *ot)
1403 {
1404         /* identifiers */
1405         ot->name = "Move Channels";
1406         ot->idname = "ANIM_OT_channels_move";
1407         ot->description = "Rearrange selected animation channels";
1408
1409         /* api callbacks */
1410         ot->exec = animchannels_rearrange_exec;
1411         ot->poll = animedit_poll_channels_nla_tweakmode_off;
1412
1413         /* flags */
1414         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1415
1416         /* props */
1417         ot->prop = RNA_def_enum(ot->srna, "direction", prop_animchannel_rearrange_types, REARRANGE_ANIMCHAN_DOWN, "Direction", "");
1418 }
1419
1420 /* ******************** Group Channel Operator ************************ */
1421
1422 static bool animchannels_grouping_poll(bContext *C)
1423 {
1424         ScrArea *sa = CTX_wm_area(C);
1425         SpaceLink *sl;
1426
1427         /* channels region test */
1428         /* TODO: could enhance with actually testing if channels region? */
1429         if (ELEM(NULL, sa, CTX_wm_region(C)))
1430                 return 0;
1431
1432         /* animation editor test - must be suitable modes only */
1433         sl = CTX_wm_space_data(C);
1434
1435         switch (sa->spacetype) {
1436                 /* supported... */
1437                 case SPACE_ACTION:
1438                 {
1439                         SpaceAction *saction = (SpaceAction *)sl;
1440
1441                         /* dopesheet and action only - all others are for other datatypes or have no groups */
1442                         if (ELEM(saction->mode, SACTCONT_ACTION, SACTCONT_DOPESHEET) == 0)
1443                                 return 0;
1444
1445                         break;
1446                 }
1447                 case SPACE_IPO:
1448                 {
1449                         SpaceIpo *sipo = (SpaceIpo *)sl;
1450
1451                         /* drivers can't have groups... */
1452                         if (sipo->mode != SIPO_MODE_ANIMATION)
1453                                 return 0;
1454
1455                         break;
1456                 }
1457                 /* unsupported... */
1458                 default:
1459                         return 0;
1460         }
1461
1462         return 1;
1463 }
1464
1465 /* ----------------------------------------------------------- */
1466
1467 static void animchannels_group_channels(bAnimContext *ac, bAnimListElem *adt_ref, const char name[])
1468 {
1469         AnimData *adt = adt_ref->adt;
1470         bAction *act = adt->action;
1471
1472         if (act) {
1473                 ListBase anim_data = {NULL, NULL};
1474                 int filter;
1475
1476                 /* find selected F-Curves to re-group */
1477                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL);
1478                 ANIM_animdata_filter(ac, &anim_data, filter, adt_ref, ANIMCONT_CHANNEL);
1479
1480                 if (anim_data.first) {
1481                         bActionGroup *agrp;
1482                         bAnimListElem *ale;
1483
1484                         /* create new group, which should now be part of the action */
1485                         agrp = action_groups_add_new(act, name);
1486                         BLI_assert(agrp != NULL);
1487
1488                         /* transfer selected F-Curves across to new group  */
1489                         for (ale = anim_data.first; ale; ale = ale->next) {
1490                                 FCurve *fcu = (FCurve *)ale->data;
1491                                 bActionGroup *grp = fcu->grp;
1492
1493                                 /* remove F-Curve from group, then group too if it is now empty */
1494                                 action_groups_remove_channel(act, fcu);
1495
1496                                 if ((grp) && BLI_listbase_is_empty(&grp->channels)) {
1497                                         BLI_freelinkN(&act->groups, grp);
1498                                 }
1499
1500                                 /* add F-Curve to group */
1501                                 action_groups_add_channel(act, agrp, fcu);
1502                         }
1503                 }
1504
1505                 /* cleanup */
1506                 ANIM_animdata_freelist(&anim_data);
1507         }
1508 }
1509
1510 static int animchannels_group_exec(bContext *C, wmOperator *op)
1511 {
1512         bAnimContext ac;
1513         char name[MAX_NAME];
1514
1515         /* get editor data */
1516         if (ANIM_animdata_get_context(C, &ac) == 0)
1517                 return OPERATOR_CANCELLED;
1518
1519         /* get name for new group */
1520         RNA_string_get(op->ptr, "name", name);
1521
1522         /* XXX: name for group should never be empty... */
1523         if (name[0]) {
1524                 ListBase anim_data = {NULL, NULL};
1525                 bAnimListElem *ale;
1526                 int filter;
1527
1528                 /* handle each animdata block separately, so that the regrouping doesn't flow into blocks  */
1529                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_ANIMDATA | ANIMFILTER_NODUPLIS);
1530                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1531
1532                 for (ale = anim_data.first; ale; ale = ale->next) {
1533                         animchannels_group_channels(&ac, ale, name);
1534                 }
1535
1536                 /* free temp data */
1537                 ANIM_animdata_freelist(&anim_data);
1538
1539                 /* updatss */
1540                 WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
1541         }
1542
1543         return OPERATOR_FINISHED;
1544 }
1545
1546 static void ANIM_OT_channels_group(wmOperatorType *ot)
1547 {
1548         /* identifiers */
1549         ot->name = "Group Channels";
1550         ot->idname = "ANIM_OT_channels_group";
1551         ot->description = "Add selected F-Curves to a new group";
1552
1553         /* callbacks */
1554         ot->invoke = WM_operator_props_popup;
1555         ot->exec = animchannels_group_exec;
1556         ot->poll = animchannels_grouping_poll;
1557
1558         /* flags */
1559         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1560
1561         /* props */
1562         ot->prop = RNA_def_string(ot->srna, "name", "New Group",
1563                                   sizeof(((bActionGroup *)NULL)->name),
1564                                   "Name", "Name of newly created group");
1565         /* XXX: still not too sure about this - keeping same text is confusing... */
1566         // RNA_def_property_flag(ot->prop, PROP_SKIP_SAVE);
1567 }
1568
1569 /* ----------------------------------------------------------- */
1570
1571 static int animchannels_ungroup_exec(bContext *C, wmOperator *UNUSED(op))
1572 {
1573         bAnimContext ac;
1574
1575         ListBase anim_data = {NULL, NULL};
1576         bAnimListElem *ale;
1577         int filter;
1578
1579         /* get editor data */
1580         if (ANIM_animdata_get_context(C, &ac) == 0)
1581                 return OPERATOR_CANCELLED;
1582
1583         /* just selected F-Curves... */
1584         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL | ANIMFILTER_NODUPLIS);
1585         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1586
1587         for (ale = anim_data.first; ale; ale = ale->next) {
1588                 /* find action for this F-Curve... */
1589                 if (ale->adt && ale->adt->action) {
1590                         FCurve  *fcu = (FCurve *)ale->data;
1591                         bAction *act = ale->adt->action;
1592
1593                         /* only proceed to remove if F-Curve is in a group... */
1594                         if (fcu->grp) {
1595                                 bActionGroup *agrp = fcu->grp;
1596
1597                                 /* remove F-Curve from group and add at tail (ungrouped) */
1598                                 action_groups_remove_channel(act, fcu);
1599                                 BLI_addtail(&act->curves, fcu);
1600
1601                                 /* delete group if it is now empty */
1602                                 if (BLI_listbase_is_empty(&agrp->channels)) {
1603                                         BLI_freelinkN(&act->groups, agrp);
1604                                 }
1605                         }
1606                 }
1607         }
1608
1609         /* cleanup */
1610         ANIM_animdata_freelist(&anim_data);
1611
1612         /* updates */
1613         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
1614
1615         return OPERATOR_FINISHED;
1616 }
1617
1618 static void ANIM_OT_channels_ungroup(wmOperatorType *ot)
1619 {
1620         /* identifiers */
1621         ot->name = "Ungroup Channels";
1622         ot->idname = "ANIM_OT_channels_ungroup";
1623         ot->description = "Remove selected F-Curves from their current groups";
1624
1625         /* callbacks */
1626         ot->exec = animchannels_ungroup_exec;
1627         ot->poll = animchannels_grouping_poll;
1628
1629         /* flags */
1630         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1631 }
1632
1633 /* ******************** Delete Channel Operator *********************** */
1634
1635 static int animchannels_delete_exec(bContext *C, wmOperator *UNUSED(op))
1636 {
1637         bAnimContext ac;
1638         ListBase anim_data = {NULL, NULL};
1639         bAnimListElem *ale;
1640         int filter;
1641
1642         /* get editor data */
1643         if (ANIM_animdata_get_context(C, &ac) == 0)
1644                 return OPERATOR_CANCELLED;
1645
1646         /* cannot delete in shapekey */
1647         if (ac.datatype == ANIMCONT_SHAPEKEY)
1648                 return OPERATOR_CANCELLED;
1649
1650
1651         /* do groups only first (unless in Drivers mode, where there are none) */
1652         if (ac.datatype != ANIMCONT_DRIVERS) {
1653                 /* filter data */
1654                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL | ANIMFILTER_LIST_CHANNELS | ANIMFILTER_FOREDIT | ANIMFILTER_NODUPLIS);
1655                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1656
1657                 /* delete selected groups and their associated channels */
1658                 for (ale = anim_data.first; ale; ale = ale->next) {
1659                         /* only groups - don't check other types yet, since they may no-longer exist */
1660                         if (ale->type == ANIMTYPE_GROUP) {
1661                                 bActionGroup *agrp = (bActionGroup *)ale->data;
1662                                 AnimData *adt = ale->adt;
1663                                 FCurve *fcu, *fcn;
1664
1665                                 /* skip this group if no AnimData available, as we can't safely remove the F-Curves */
1666                                 if (adt == NULL)
1667                                         continue;
1668
1669                                 /* delete all of the Group's F-Curves, but no others */
1670                                 for (fcu = agrp->channels.first; fcu && fcu->grp == agrp; fcu = fcn) {
1671                                         fcn = fcu->next;
1672
1673                                         /* remove from group and action, then free */
1674                                         action_groups_remove_channel(adt->action, fcu);
1675                                         free_fcurve(fcu);
1676                                 }
1677
1678                                 /* free the group itself */
1679                                 if (adt->action) {
1680                                         BLI_freelinkN(&adt->action->groups, agrp);
1681                                         DEG_id_tag_update_ex(CTX_data_main(C), &adt->action->id, ID_RECALC_COPY_ON_WRITE);
1682                                 }
1683                                 else
1684                                         MEM_freeN(agrp);
1685                         }
1686                 }
1687
1688                 /* cleanup */
1689                 ANIM_animdata_freelist(&anim_data);
1690         }
1691
1692         /* filter data */
1693         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL | ANIMFILTER_FOREDIT | ANIMFILTER_NODUPLIS);
1694         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
1695
1696         /* delete selected data channels */
1697         for (ale = anim_data.first; ale; ale = ale->next) {
1698                 switch (ale->type) {
1699                         case ANIMTYPE_FCURVE:
1700                         {
1701                                 /* F-Curves if we can identify its parent */
1702                                 AnimData *adt = ale->adt;
1703                                 FCurve *fcu = (FCurve *)ale->data;
1704
1705                                 /* try to free F-Curve */
1706                                 ANIM_fcurve_delete_from_animdata(&ac, adt, fcu);
1707                                 ale->update = ANIM_UPDATE_DEPS;
1708                                 break;
1709                         }
1710                         case ANIMTYPE_NLACURVE:
1711                         {
1712                                 /* NLA Control Curve - Deleting it should disable the corresponding setting... */
1713                                 NlaStrip *strip = (NlaStrip *)ale->owner;
1714                                 FCurve *fcu = (FCurve *)ale->data;
1715
1716                                 if (STREQ(fcu->rna_path, "strip_time")) {
1717                                         strip->flag &= ~NLASTRIP_FLAG_USR_TIME;
1718                                 }
1719                                 else if (STREQ(fcu->rna_path, "influence")) {
1720                                         strip->flag &= ~NLASTRIP_FLAG_USR_INFLUENCE;
1721                                 }
1722                                 else {
1723                                         printf("ERROR: Trying to delete NLA Control Curve for unknown property '%s'\n", fcu->rna_path);
1724                                 }
1725
1726                                 /* unlink and free the F-Curve */
1727                                 BLI_remlink(&strip->fcurves, fcu);
1728                                 free_fcurve(fcu);
1729                                 ale->update = ANIM_UPDATE_DEPS;
1730                                 break;
1731                         }
1732                         case ANIMTYPE_GPLAYER:
1733                         {
1734                                 /* Grease Pencil layer */
1735                                 bGPdata *gpd = (bGPdata *)ale->id;
1736                                 bGPDlayer *gpl = (bGPDlayer *)ale->data;
1737
1738                                 /* try to delete the layer's data and the layer itself */
1739                                 BKE_gpencil_layer_delete(gpd, gpl);
1740                                 ale->update = ANIM_UPDATE_DEPS;
1741                                 break;
1742                         }
1743                         case ANIMTYPE_MASKLAYER:
1744                         {
1745                                 /* Mask layer */
1746                                 Mask *mask = (Mask *)ale->id;
1747                                 MaskLayer *masklay = (MaskLayer *)ale->data;
1748
1749                                 /* try to delete the layer's data and the layer itself */
1750                                 BKE_mask_layer_remove(mask, masklay);
1751                                 break;
1752                         }
1753                 }
1754         }
1755
1756         /* cleanup */
1757         ANIM_animdata_update(&ac, &anim_data);
1758         ANIM_animdata_freelist(&anim_data);
1759
1760         /* send notifier that things have changed */
1761         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
1762         DEG_relations_tag_update(CTX_data_main(C));
1763
1764         return OPERATOR_FINISHED;
1765 }
1766
1767 static void ANIM_OT_channels_delete(wmOperatorType *ot)
1768 {
1769         /* identifiers */
1770         ot->name = "Delete Channels";
1771         ot->idname = "ANIM_OT_channels_delete";
1772         ot->description = "Delete all selected animation channels";
1773
1774         /* api callbacks */
1775         ot->exec = animchannels_delete_exec;
1776         ot->poll = animedit_poll_channels_active;
1777
1778         /* flags */
1779         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1780 }
1781
1782 /* ********************** Set Flags Operator *********************** */
1783
1784 /* defines for setting animation-channel flags */
1785 static const EnumPropertyItem prop_animchannel_setflag_types[] = {
1786         {ACHANNEL_SETFLAG_TOGGLE, "TOGGLE", 0, "Toggle", ""},
1787         {ACHANNEL_SETFLAG_CLEAR, "DISABLE", 0, "Disable", ""},
1788         {ACHANNEL_SETFLAG_ADD, "ENABLE", 0, "Enable", ""},
1789         {ACHANNEL_SETFLAG_INVERT, "INVERT", 0, "Invert", ""},
1790         {0, NULL, 0, NULL, NULL}
1791 };
1792
1793 /* defines for set animation-channel settings */
1794 // TODO: could add some more types, but those are really quite dependent on the mode...
1795 static const EnumPropertyItem prop_animchannel_settings_types[] = {
1796         {ACHANNEL_SETTING_PROTECT, "PROTECT", 0, "Protect", ""},
1797         {ACHANNEL_SETTING_MUTE, "MUTE", 0, "Mute", ""},
1798         {0, NULL, 0, NULL, NULL}
1799 };
1800
1801
1802 /* ------------------- */
1803
1804 /* Set/clear a particular flag (setting) for all selected + visible channels
1805  * setting: the setting to modify
1806  * mode: eAnimChannels_SetFlag
1807  * onlysel: only selected channels get the flag set
1808  */
1809 // TODO: enable a setting which turns flushing on/off?
1810 static void setflag_anim_channels(bAnimContext *ac, eAnimChannel_Settings setting, eAnimChannels_SetFlag mode, bool onlysel, bool flush)
1811 {
1812         ListBase anim_data = {NULL, NULL};
1813         ListBase all_data = {NULL, NULL};
1814         bAnimListElem *ale;
1815         int filter;
1816
1817         /* filter data that we need if flush is on */
1818         if (flush) {
1819                 /* get list of all channels that selection may need to be flushed to
1820                  * - hierarchy visibility needs to be ignored so that settings can get flushed
1821                  *   "down" inside closed containers
1822                  */
1823                 filter = ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_CHANNELS;
1824                 ANIM_animdata_filter(ac, &all_data, filter, ac->data, ac->datatype);
1825         }
1826
1827         /* filter data that we're working on
1828          * - hierarchy matters if we're doing this from the channels region
1829          *   since we only want to apply this to channels we can "see",
1830          *   and have these affect their relatives
1831          * - but for Graph Editor, this gets used also from main region
1832          *   where hierarchy doesn't apply [#21276]
1833          */
1834         if ((ac->spacetype == SPACE_IPO) && (ac->regiontype != RGN_TYPE_CHANNELS)) {
1835                 /* graph editor (case 2) */
1836                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_CHANNELS | ANIMFILTER_CURVE_VISIBLE | ANIMFILTER_NODUPLIS);
1837         }
1838         else {
1839                 /* standard case */
1840                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS | ANIMFILTER_NODUPLIS);
1841         }
1842         if (onlysel) filter |= ANIMFILTER_SEL;
1843         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
1844
1845         /* if toggling, check if disable or enable */
1846         if (mode == ACHANNEL_SETFLAG_TOGGLE) {
1847                 /* default to turn all on, unless we encounter one that's on... */
1848                 mode = ACHANNEL_SETFLAG_ADD;
1849
1850                 /* see if we should turn off instead... */
1851                 for (ale = anim_data.first; ale; ale = ale->next) {
1852                         /* set the setting in the appropriate way (if available) */
1853                         if (ANIM_channel_setting_get(ac, ale, setting) > 0) {
1854                                 mode = ACHANNEL_SETFLAG_CLEAR;
1855                                 break;
1856                         }
1857                 }
1858         }
1859
1860         /* apply the setting */
1861         for (ale = anim_data.first; ale; ale = ale->next) {
1862                 /* skip channel if setting is not available */
1863                 if (ANIM_channel_setting_get(ac, ale, setting) == -1)
1864                         continue;
1865
1866                 /* set the setting in the appropriate way */
1867                 ANIM_channel_setting_set(ac, ale, setting, mode);
1868
1869                 /* if flush status... */
1870                 if (flush)
1871                         ANIM_flush_setting_anim_channels(ac, &all_data, ale, setting, mode);
1872         }
1873
1874         ANIM_animdata_freelist(&anim_data);
1875         BLI_freelistN(&all_data);
1876 }
1877
1878 /* ------------------- */
1879
1880 static int animchannels_setflag_exec(bContext *C, wmOperator *op)
1881 {
1882         bAnimContext ac;
1883         eAnimChannel_Settings setting;
1884         eAnimChannels_SetFlag mode;
1885         bool flush = true;
1886
1887         /* get editor data */
1888         if (ANIM_animdata_get_context(C, &ac) == 0)
1889                 return OPERATOR_CANCELLED;
1890
1891         /* mode (eAnimChannels_SetFlag), setting (eAnimChannel_Settings) */
1892         mode = RNA_enum_get(op->ptr, "mode");
1893         setting = RNA_enum_get(op->ptr, "type");
1894
1895         /* check if setting is flushable */
1896         if (setting == ACHANNEL_SETTING_EXPAND)
1897                 flush = false;
1898
1899         /* modify setting
1900          * - only selected channels are affected
1901          */
1902         setflag_anim_channels(&ac, setting, mode, true, flush);
1903
1904         /* send notifier that things have changed */
1905         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
1906
1907         return OPERATOR_FINISHED;
1908 }
1909
1910 /* duplicate of 'ANIM_OT_channels_setting_toggle' for menu title only, weak! */
1911 static void ANIM_OT_channels_setting_enable(wmOperatorType *ot)
1912 {
1913         PropertyRNA *prop;
1914
1915         /* identifiers */
1916         ot->name = "Enable Channel Setting";
1917         ot->idname = "ANIM_OT_channels_setting_enable";
1918         ot->description = "Enable specified setting on all selected animation channels";
1919
1920         /* api callbacks */
1921         ot->invoke = WM_menu_invoke;
1922         ot->exec = animchannels_setflag_exec;
1923         ot->poll = animedit_poll_channels_active;
1924
1925         /* flags */
1926         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1927
1928         /* props */
1929         /* flag-setting mode */
1930         prop = RNA_def_enum(ot->srna, "mode", prop_animchannel_setflag_types, ACHANNEL_SETFLAG_ADD, "Mode", "");
1931         RNA_def_property_flag(prop, PROP_HIDDEN);
1932         /* setting to set */
1933         ot->prop = RNA_def_enum(ot->srna, "type", prop_animchannel_settings_types, 0, "Type", "");
1934 }
1935 /* duplicate of 'ANIM_OT_channels_setting_toggle' for menu title only, weak! */
1936 static void ANIM_OT_channels_setting_disable(wmOperatorType *ot)
1937 {
1938         PropertyRNA *prop;
1939
1940         /* identifiers */
1941         ot->name = "Disable Channel Setting";
1942         ot->idname = "ANIM_OT_channels_setting_disable";
1943         ot->description = "Disable specified setting on all selected animation channels";
1944
1945         /* api callbacks */
1946         ot->invoke = WM_menu_invoke;
1947         ot->exec = animchannels_setflag_exec;
1948         ot->poll = animedit_poll_channels_active;
1949
1950         /* flags */
1951         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1952
1953         /* props */
1954         /* flag-setting mode */
1955         prop = RNA_def_enum(ot->srna, "mode", prop_animchannel_setflag_types, ACHANNEL_SETFLAG_CLEAR, "Mode", "");
1956         RNA_def_property_flag(prop, PROP_HIDDEN); /* internal hack - don't expose */
1957         /* setting to set */
1958         ot->prop = RNA_def_enum(ot->srna, "type", prop_animchannel_settings_types, 0, "Type", "");
1959 }
1960
1961 static void ANIM_OT_channels_setting_toggle(wmOperatorType *ot)
1962 {
1963         PropertyRNA *prop;
1964
1965         /* identifiers */
1966         ot->name = "Toggle Channel Setting";
1967         ot->idname = "ANIM_OT_channels_setting_toggle";
1968         ot->description = "Toggle specified setting on all selected animation channels";
1969
1970         /* api callbacks */
1971         ot->invoke = WM_menu_invoke;
1972         ot->exec = animchannels_setflag_exec;
1973         ot->poll = animedit_poll_channels_active;
1974
1975         /* flags */
1976         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1977
1978         /* props */
1979         /* flag-setting mode */
1980         prop = RNA_def_enum(ot->srna, "mode", prop_animchannel_setflag_types, ACHANNEL_SETFLAG_TOGGLE, "Mode", "");
1981         RNA_def_property_flag(prop, PROP_HIDDEN); /* internal hack - don't expose */
1982         /* setting to set */
1983         ot->prop = RNA_def_enum(ot->srna, "type", prop_animchannel_settings_types, 0, "Type", "");
1984 }
1985
1986 static void ANIM_OT_channels_editable_toggle(wmOperatorType *ot)
1987 {
1988         PropertyRNA *prop;
1989
1990         /* identifiers */
1991         ot->name = "Toggle Channel Editability";
1992         ot->idname = "ANIM_OT_channels_editable_toggle";
1993         ot->description = "Toggle editability of selected channels";
1994
1995         /* api callbacks */
1996         ot->exec = animchannels_setflag_exec;
1997         ot->poll = animedit_poll_channels_active;
1998
1999         /* flags */
2000         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2001
2002         /* props */
2003         /* flag-setting mode */
2004         RNA_def_enum(ot->srna, "mode", prop_animchannel_setflag_types, ACHANNEL_SETFLAG_TOGGLE, "Mode", "");
2005         /* setting to set */
2006         prop = RNA_def_enum(ot->srna, "type", prop_animchannel_settings_types, ACHANNEL_SETTING_PROTECT, "Type", "");
2007         RNA_def_property_flag(prop, PROP_HIDDEN); /* internal hack - don't expose */
2008 }
2009
2010 /* ********************** Expand Channels Operator *********************** */
2011
2012 static int animchannels_expand_exec(bContext *C, wmOperator *op)
2013 {
2014         bAnimContext ac;
2015         bool onlysel = true;
2016
2017         /* get editor data */
2018         if (ANIM_animdata_get_context(C, &ac) == 0)
2019                 return OPERATOR_CANCELLED;
2020
2021         /* only affect selected channels? */
2022         if (RNA_boolean_get(op->ptr, "all"))
2023                 onlysel = false;
2024
2025         /* modify setting */
2026         setflag_anim_channels(&ac, ACHANNEL_SETTING_EXPAND, ACHANNEL_SETFLAG_ADD, onlysel, false);
2027
2028         /* send notifier that things have changed */
2029         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
2030
2031         return OPERATOR_FINISHED;
2032 }
2033
2034 static void ANIM_OT_channels_expand(wmOperatorType *ot)
2035 {
2036         /* identifiers */
2037         ot->name = "Expand Channels";
2038         ot->idname = "ANIM_OT_channels_expand";
2039         ot->description = "Expand (i.e. open) all selected expandable animation channels";
2040
2041         /* api callbacks */
2042         ot->exec = animchannels_expand_exec;
2043         ot->poll = animedit_poll_channels_active;
2044
2045         /* flags */
2046         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2047
2048         /* props */
2049         ot->prop = RNA_def_boolean(ot->srna, "all", 1, "All", "Expand all channels (not just selected ones)");
2050 }
2051
2052 /* ********************** Collapse Channels Operator *********************** */
2053
2054 static int animchannels_collapse_exec(bContext *C, wmOperator *op)
2055 {
2056         bAnimContext ac;
2057         bool onlysel = true;
2058
2059         /* get editor data */
2060         if (ANIM_animdata_get_context(C, &ac) == 0)
2061                 return OPERATOR_CANCELLED;
2062
2063         /* only affect selected channels? */
2064         if (RNA_boolean_get(op->ptr, "all"))
2065                 onlysel = false;
2066
2067         /* modify setting */
2068         setflag_anim_channels(&ac, ACHANNEL_SETTING_EXPAND, ACHANNEL_SETFLAG_CLEAR, onlysel, false);
2069
2070         /* send notifier that things have changed */
2071         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
2072
2073         return OPERATOR_FINISHED;
2074 }
2075
2076 static void ANIM_OT_channels_collapse(wmOperatorType *ot)
2077 {
2078         /* identifiers */
2079         ot->name = "Collapse Channels";
2080         ot->idname = "ANIM_OT_channels_collapse";
2081         ot->description = "Collapse (i.e. close) all selected expandable animation channels";
2082
2083         /* api callbacks */
2084         ot->exec = animchannels_collapse_exec;
2085         ot->poll = animedit_poll_channels_active;
2086
2087         /* flags */
2088         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2089
2090         /* props */
2091         ot->prop = RNA_def_boolean(ot->srna, "all", true, "All", "Collapse all channels (not just selected ones)");
2092 }
2093
2094 /* ************ Remove All "Empty" AnimData Blocks Operator ********* */
2095 /* We define "empty" AnimData blocks here as those which have all 3 of criteria:
2096  *  1) No active action OR that active actions are empty
2097  *     Assuming that all legitimate entries will have an action,
2098  *     and that empty actions
2099  *  2) No NLA Tracks + NLA Strips
2100  *     Assuming that users haven't set up any of these as "placeholders"
2101  *     for convenience sake, and that most that exist were either unintentional
2102  *     or are no longer wanted
2103  *  3) No drivers
2104  */
2105
2106 static int animchannels_clean_empty_exec(bContext *C, wmOperator *UNUSED(op))
2107 {
2108         bAnimContext ac;
2109
2110         ListBase anim_data = {NULL, NULL};
2111         bAnimListElem *ale;
2112         int filter;
2113
2114         /* get editor data */
2115         if (ANIM_animdata_get_context(C, &ac) == 0)
2116                 return OPERATOR_CANCELLED;
2117
2118         /* get animdata blocks */
2119         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_ANIMDATA);
2120         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
2121
2122         for (ale = anim_data.first; ale; ale = ale->next) {
2123                 ID *id = ale->id;
2124                 AnimData *adt = ale->data;
2125
2126                 bool action_empty  = false;
2127                 bool nla_empty     = false;
2128                 bool drivers_empty = false;
2129
2130                 /* sanity checks */
2131                 BLI_assert((id != NULL) && (adt != NULL));
2132
2133                 /* check if this is "empty" and can be deleted */
2134                 /* (For now, there are only these 3 criteria) */
2135
2136                 /* 1) Active Action is missing or empty */
2137                 if (ELEM(NULL, adt->action, adt->action->curves.first)) {
2138                         action_empty = true;
2139                 }
2140                 else {
2141                         /* TODO: check for keyframe + fmodifier data on these too */
2142                 }
2143
2144                 /* 2) No NLA Tracks and/or NLA Strips */
2145                 if (adt->nla_tracks.first == NULL) {
2146                         nla_empty = true;
2147                 }
2148                 else {
2149                         NlaTrack *nlt;
2150
2151                         /* empty tracks? */
2152                         for (nlt = adt->nla_tracks.first; nlt; nlt = nlt->next) {
2153                                 if (nlt->strips.first) {
2154                                         /* stop searching, as we found one that actually had stuff we don't want lost
2155                                          * NOTE: nla_empty gets reset to false, as a previous track may have been empty
2156                                          */
2157                                         nla_empty = false;
2158                                         break;
2159                                 }
2160                                 else if (nlt->strips.first == NULL) {
2161                                         /* this track is empty, but another one may still have stuff in it, so can't break yet */
2162                                         nla_empty = true;
2163                                 }
2164                         }
2165                 }
2166
2167                 /* 3) Drivers */
2168                 drivers_empty = (adt->drivers.first == NULL);
2169
2170
2171                 /* remove AnimData? */
2172                 if (action_empty && nla_empty && drivers_empty) {
2173                         BKE_animdata_free(id, true);
2174                 }
2175         }
2176
2177         /* free temp data */
2178         ANIM_animdata_freelist(&anim_data);
2179
2180         /* send notifier that things have changed */
2181         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
2182
2183         return OPERATOR_FINISHED;
2184 }
2185
2186 static void ANIM_OT_channels_clean_empty(wmOperatorType *ot)
2187 {
2188         /* identifiers */
2189         ot->name = "Remove Empty Animation Data";
2190         ot->idname = "ANIM_OT_channels_clean_empty";
2191         ot->description = "Delete all empty animation data containers from visible data-blocks";
2192
2193         /* api callbacks */
2194         ot->exec = animchannels_clean_empty_exec;
2195         ot->poll = animedit_poll_channels_nla_tweakmode_off;
2196
2197         /* flags */
2198         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2199 }
2200
2201 /* ******************* Reenable Disabled Operator ******************* */
2202
2203 static bool animchannels_enable_poll(bContext *C)
2204 {
2205         ScrArea *sa = CTX_wm_area(C);
2206
2207         /* channels region test */
2208         /* TODO: could enhance with actually testing if channels region? */
2209         if (ELEM(NULL, sa, CTX_wm_region(C)))
2210                 return 0;
2211
2212         /* animation editor test - Action/Dopesheet/etc. and Graph only */
2213         if (ELEM(sa->spacetype, SPACE_ACTION, SPACE_IPO) == 0)
2214                 return 0;
2215
2216         return 1;
2217 }
2218
2219 static int animchannels_enable_exec(bContext *C, wmOperator *UNUSED(op))
2220 {
2221         bAnimContext ac;
2222
2223         ListBase anim_data = {NULL, NULL};
2224         bAnimListElem *ale;
2225         int filter;
2226
2227         /* get editor data */
2228         if (ANIM_animdata_get_context(C, &ac) == 0)
2229                 return OPERATOR_CANCELLED;
2230
2231         /* filter data */
2232         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_NODUPLIS);
2233         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
2234
2235         /* loop through filtered data and clean curves */
2236         for (ale = anim_data.first; ale; ale = ale->next) {
2237                 FCurve *fcu = (FCurve *)ale->data;
2238
2239                 /* remove disabled flags from F-Curves */
2240                 fcu->flag &= ~FCURVE_DISABLED;
2241
2242                 /* for drivers, let's do the same too */
2243                 if (fcu->driver)
2244                         fcu->driver->flag &= ~DRIVER_FLAG_INVALID;
2245
2246                 /* tag everything for updates - in particular, this is needed to get drivers working again */
2247                 ale->update |= ANIM_UPDATE_DEPS;
2248         }
2249
2250         ANIM_animdata_update(&ac, &anim_data);
2251         ANIM_animdata_freelist(&anim_data);
2252
2253         /* send notifier that things have changed */
2254         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
2255
2256         return OPERATOR_FINISHED;
2257 }
2258
2259 static void ANIM_OT_channels_fcurves_enable(wmOperatorType *ot)
2260 {
2261         /* identifiers */
2262         ot->name = "Revive Disabled F-Curves";
2263         ot->idname = "ANIM_OT_channels_fcurves_enable";
2264         ot->description = "Clears 'disabled' tag from all F-Curves to get broken F-Curves working again";
2265
2266         /* api callbacks */
2267         ot->exec = animchannels_enable_exec;
2268         ot->poll = animchannels_enable_poll;
2269
2270         /* flags */
2271         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2272 }
2273
2274 /* ****************** Find / Set Filter Operator ******************** */
2275
2276 /* XXX: make this generic? */
2277 static bool animchannels_find_poll(bContext *C)
2278 {
2279         ScrArea *sa = CTX_wm_area(C);
2280
2281         if (sa == NULL)
2282                 return 0;
2283
2284         /* animation editor with dopesheet */
2285         return ELEM(sa->spacetype, SPACE_ACTION, SPACE_IPO, SPACE_NLA);
2286 }
2287
2288 /* find_invoke() - Get initial channels */
2289 static int animchannels_find_invoke(bContext *C, wmOperator *op, const wmEvent *evt)
2290 {
2291         bAnimContext ac;
2292
2293         /* get editor data */
2294         if (ANIM_animdata_get_context(C, &ac) == 0)
2295                 return OPERATOR_CANCELLED;
2296
2297         /* set initial filter text, and enable filter */
2298         RNA_string_set(op->ptr, "query", ac.ads->searchstr);
2299
2300         /* defer to popup */
2301         return WM_operator_props_popup(C, op, evt);
2302 }
2303
2304 /* find_exec() -  Called to set the value */
2305 static int animchannels_find_exec(bContext *C, wmOperator *op)
2306 {
2307         bAnimContext ac;
2308
2309         /* get editor data */
2310         if (ANIM_animdata_get_context(C, &ac) == 0)
2311                 return OPERATOR_CANCELLED;
2312
2313         /* update filter text */
2314         RNA_string_get(op->ptr, "query", ac.ads->searchstr);
2315
2316         /* redraw */
2317         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
2318
2319         return OPERATOR_FINISHED;
2320 }
2321
2322 static void ANIM_OT_channels_find(wmOperatorType *ot)
2323 {
2324         /* identifiers */
2325         ot->name = "Find Channels";
2326         ot->idname = "ANIM_OT_channels_find";
2327         ot->description = "Filter the set of channels shown to only include those with matching names";
2328
2329         /* callbacks */
2330         ot->invoke = animchannels_find_invoke;
2331         ot->exec = animchannels_find_exec;
2332         ot->poll = animchannels_find_poll;
2333
2334         /* flags */
2335         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2336
2337         /* properties */
2338         ot->prop = RNA_def_string(ot->srna, "query", "Query", sizeof(((bDopeSheet *)NULL)->searchstr), "", "Text to search for in channel names");
2339 }
2340
2341 /* ********************** Select All Operator *********************** */
2342
2343 static int animchannels_deselectall_exec(bContext *C, wmOperator *op)
2344 {
2345         bAnimContext ac;
2346
2347         /* get editor data */
2348         if (ANIM_animdata_get_context(C, &ac) == 0)
2349                 return OPERATOR_CANCELLED;
2350
2351         /* 'standard' behavior - check if selected, then apply relevant selection */
2352         const int action = RNA_enum_get(op->ptr, "action");
2353         switch (action) {
2354                 case SEL_TOGGLE:
2355                         ANIM_deselect_anim_channels(&ac, ac.data, ac.datatype, true, ACHANNEL_SETFLAG_ADD);
2356                         break;
2357                 case SEL_SELECT:
2358                         ANIM_deselect_anim_channels(&ac, ac.data, ac.datatype, false, ACHANNEL_SETFLAG_ADD);
2359                         break;
2360                 case SEL_DESELECT:
2361                         ANIM_deselect_anim_channels(&ac, ac.data, ac.datatype, false, ACHANNEL_SETFLAG_CLEAR);
2362                         break;
2363                 case SEL_INVERT:
2364                         ANIM_deselect_anim_channels(&ac, ac.data, ac.datatype, false, ACHANNEL_SETFLAG_INVERT);
2365                         break;
2366                 default:
2367                         BLI_assert(0);
2368                         break;
2369         }
2370
2371         /* send notifier that things have changed */
2372         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_SELECTED, NULL);
2373
2374         return OPERATOR_FINISHED;
2375 }
2376
2377 static void ANIM_OT_channels_select_all(wmOperatorType *ot)
2378 {
2379         /* identifiers */
2380         ot->name = "Select All";
2381         ot->idname = "ANIM_OT_channels_select_all";
2382         ot->description = "Toggle selection of all animation channels";
2383
2384         /* api callbacks */
2385         ot->exec = animchannels_deselectall_exec;
2386         ot->poll = animedit_poll_channels_nla_tweakmode_off;
2387
2388         /* flags */
2389         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2390
2391         /* properties */
2392         WM_operator_properties_select_all(ot);
2393 }
2394
2395 /* ******************** Box Select Operator *********************** */
2396
2397 static void box_select_anim_channels(bAnimContext *ac, rcti *rect, short selectmode)
2398 {
2399         ListBase anim_data = {NULL, NULL};
2400         bAnimListElem *ale;
2401         int filter;
2402
2403         SpaceNla *snla = (SpaceNla *)ac->sl;
2404         View2D *v2d = &ac->ar->v2d;
2405         rctf rectf;
2406         float ymin, ymax;
2407
2408         /* set initial y extents */
2409         if (ac->datatype == ANIMCONT_NLA) {
2410                 ymin = (float)(-NLACHANNEL_HEIGHT(snla));
2411                 ymax = 0.0f;
2412         }
2413         else {
2414                 ymin = 0.0f;
2415                 ymax = (float)(-ACHANNEL_HEIGHT(ac));
2416         }
2417
2418         /* convert border-region to view coordinates */
2419         UI_view2d_region_to_view(v2d, rect->xmin, rect->ymin + 2, &rectf.xmin, &rectf.ymin);
2420         UI_view2d_region_to_view(v2d, rect->xmax, rect->ymax - 2, &rectf.xmax, &rectf.ymax);
2421
2422         /* filter data */
2423         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
2424         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
2425
2426         /* loop over data, doing box select */
2427         for (ale = anim_data.first; ale; ale = ale->next) {
2428                 if (ac->datatype == ANIMCONT_NLA)
2429                         ymin = ymax - NLACHANNEL_STEP(snla);
2430                 else
2431                         ymin = ymax - ACHANNEL_STEP(ac);
2432
2433                 /* if channel is within border-select region, alter it */
2434                 if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))) {
2435                         /* set selection flags only */
2436                         ANIM_channel_setting_set(ac, ale, ACHANNEL_SETTING_SELECT, selectmode);
2437
2438                         /* type specific actions */
2439                         switch (ale->type) {
2440                                 case ANIMTYPE_GROUP:
2441                                 {
2442                                         bActionGroup *agrp = (bActionGroup *)ale->data;
2443                                         select_pchan_for_action_group(ac, agrp, ale);
2444                                         /* always clear active flag after doing this */
2445                                         agrp->flag &= ~AGRP_ACTIVE;
2446                                         break;
2447                                 }
2448                                 case ANIMTYPE_NLATRACK:
2449                                 {
2450                                         NlaTrack *nlt = (NlaTrack *)ale->data;
2451
2452                                         /* for now, it's easier just to do this here manually, as defining a new type
2453                                          * currently adds complications when doing other stuff
2454                                          */
2455                                         ACHANNEL_SET_FLAG(nlt, selectmode, NLATRACK_SELECTED);
2456                                         break;
2457                                 }
2458                         }
2459                 }
2460
2461                 /* set minimum extent to be the maximum of the next channel */
2462                 ymax = ymin;
2463         }
2464
2465         /* cleanup */
2466         ANIM_animdata_freelist(&anim_data);
2467 }
2468
2469 /* ------------------- */
2470
2471 static int animchannels_box_select_exec(bContext *C, wmOperator *op)
2472 {
2473         bAnimContext ac;
2474         rcti rect;
2475         short selectmode = 0;
2476         const bool select = !RNA_boolean_get(op->ptr, "deselect");
2477         const bool extend = RNA_boolean_get(op->ptr, "extend");
2478
2479         /* get editor data */
2480         if (ANIM_animdata_get_context(C, &ac) == 0)
2481                 return OPERATOR_CANCELLED;
2482
2483         /* get settings from operator */
2484         WM_operator_properties_border_to_rcti(op, &rect);
2485
2486         if (!extend) {
2487                 ANIM_deselect_anim_channels(&ac, ac.data, ac.datatype, true, ACHANNEL_SETFLAG_CLEAR);
2488         }
2489
2490         if (select) {
2491                 selectmode = ACHANNEL_SETFLAG_ADD;
2492         }
2493         else {
2494                 selectmode = ACHANNEL_SETFLAG_CLEAR;
2495         }
2496
2497         /* apply box_select animation channels */
2498         box_select_anim_channels(&ac, &rect, selectmode);
2499
2500         /* send notifier that things have changed */
2501         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_SELECTED, NULL);
2502
2503         return OPERATOR_FINISHED;
2504 }
2505
2506 static void ANIM_OT_channels_select_box(wmOperatorType *ot)
2507 {
2508         /* identifiers */
2509         ot->name = "Box Select";
2510         ot->idname = "ANIM_OT_channels_select_box";
2511         ot->description = "Select all animation channels within the specified region";
2512
2513         /* api callbacks */
2514         ot->invoke = WM_gesture_box_invoke;
2515         ot->exec = animchannels_box_select_exec;
2516         ot->modal = WM_gesture_box_modal;
2517         ot->cancel = WM_gesture_box_cancel;
2518
2519         ot->poll = animedit_poll_channels_nla_tweakmode_off;
2520
2521         /* flags */
2522         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2523
2524         /* rna */
2525         WM_operator_properties_gesture_box_select(ot);
2526 }
2527
2528 /* ******************* Rename Operator ***************************** */
2529 /* Allow renaming some channels by clicking on them */
2530
2531 static bool rename_anim_channels(bAnimContext *ac, int channel_index)
2532 {
2533         ListBase anim_data = {NULL, NULL};
2534         const bAnimChannelType *acf;
2535         bAnimListElem *ale;
2536         int filter;
2537         bool success = false;
2538
2539         /* get the channel that was clicked on */
2540         /* filter channels */
2541         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
2542         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
2543
2544         /* get channel from index */
2545         ale = BLI_findlink(&anim_data, channel_index);
2546         if (ale == NULL) {
2547                 /* channel not found */
2548                 if (G.debug & G_DEBUG)
2549                         printf("Error: animation channel (index = %d) not found in rename_anim_channels()\n", channel_index);
2550
2551                 ANIM_animdata_freelist(&anim_data);
2552                 return false;
2553         }
2554
2555         /* check that channel can be renamed */
2556         acf = ANIM_channel_get_typeinfo(ale);
2557         if (acf && acf->name_prop) {
2558                 PointerRNA ptr;
2559                 PropertyRNA *prop;
2560
2561                 /* ok if we can get name property to edit from this channel */
2562                 if (acf->name_prop(ale, &ptr, &prop)) {
2563                         /* actually showing the rename textfield is done on redraw,
2564                          * so here we just store the index of this channel in the
2565                          * dopesheet data, which will get utilized when drawing the
2566                          * channel...
2567                          *
2568                          * +1 factor is for backwards compat issues
2569                          */
2570                         if (ac->ads) {
2571                                 ac->ads->renameIndex = channel_index + 1;
2572                                 success = true;
2573                         }
2574                 }
2575         }
2576
2577         /* free temp data and tag for refresh */
2578         ANIM_animdata_freelist(&anim_data);
2579         ED_region_tag_redraw(ac->ar);
2580         return success;
2581 }
2582
2583 static int animchannels_channel_get(bAnimContext *ac, const int mval[2])
2584 {
2585         ARegion *ar;
2586         View2D *v2d;
2587         int channel_index;
2588         float x, y;
2589
2590         /* get useful pointers from animation context data */
2591         ar = ac->ar;
2592         v2d = &ar->v2d;
2593
2594         /* figure out which channel user clicked in
2595          * Note: although channels technically start at (y = ACHANNEL_FIRST), we need to adjust by half a channel's height
2596          *       so that the tops of channels get caught ok. Since ACHANNEL_FIRST is really ACHANNEL_HEIGHT, we simply use
2597          *       ACHANNEL_HEIGHT_HALF.
2598          */
2599         UI_view2d_region_to_view(v2d, mval[0], mval[1], &x, &y);
2600
2601         if (ac->datatype == ANIMCONT_NLA) {
2602                 SpaceNla *snla = (SpaceNla *)ac->sl;
2603                 UI_view2d_listview_view_to_cell(v2d, NLACHANNEL_NAMEWIDTH, NLACHANNEL_STEP(snla), 0, (float)NLACHANNEL_HEIGHT_HALF(snla), x, y, NULL, &channel_index);
2604         }
2605         else {
2606                 UI_view2d_listview_view_to_cell(v2d, ACHANNEL_NAMEWIDTH, ACHANNEL_STEP(ac), 0, (float)ACHANNEL_HEIGHT_HALF(ac), x, y, NULL, &channel_index);
2607         }
2608
2609         return channel_index;
2610 }
2611
2612 static int animchannels_rename_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
2613 {
2614         bAnimContext ac;
2615         int channel_index;
2616
2617         /* get editor data */
2618         if (ANIM_animdata_get_context(C, &ac) == 0)
2619                 return OPERATOR_CANCELLED;
2620
2621         channel_index = animchannels_channel_get(&ac, event->mval);
2622
2623         /* handle click */
2624         if (rename_anim_channels(&ac, channel_index))
2625                 return OPERATOR_FINISHED;
2626         else
2627                 /* allow event to be handled by selectall operator */
2628                 return OPERATOR_PASS_THROUGH;
2629 }
2630
2631 static void ANIM_OT_channels_rename(wmOperatorType *ot)
2632 {
2633         /* identifiers */
2634         ot->name = "Rename Channels";
2635         ot->idname = "ANIM_OT_channels_rename";
2636         ot->description = "Rename animation channel under mouse";
2637
2638         /* api callbacks */
2639         ot->invoke = animchannels_rename_invoke;
2640         ot->poll = animedit_poll_channels_active;
2641 }
2642
2643 /* ******************** Mouse-Click Operator *********************** */
2644 /* Handle selection changes due to clicking on channels. Settings will get caught by UI code... */
2645
2646 static int mouse_anim_channels(bContext *C, bAnimContext *ac, int channel_index, short selectmode)
2647 {
2648         ListBase anim_data = {NULL, NULL};
2649         bAnimListElem *ale;
2650         int filter;
2651         int notifierFlags = 0;
2652
2653         /* get the channel that was clicked on */
2654         /* filter channels */
2655         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
2656         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
2657
2658         /* get channel from index */
2659         ale = BLI_findlink(&anim_data, channel_index);
2660         if (ale == NULL) {
2661                 /* channel not found */
2662                 if (G.debug & G_DEBUG)
2663                         printf("Error: animation channel (index = %d) not found in mouse_anim_channels()\n", channel_index);
2664
2665                 ANIM_animdata_freelist(&anim_data);
2666                 return 0;
2667         }
2668
2669         /* selectmode -1 is a special case for ActionGroups only, which selects all of the channels underneath it only... */
2670         /* TODO: should this feature be extended to work with other channel types too? */
2671         if ((selectmode == -1) && (ale->type != ANIMTYPE_GROUP)) {
2672                 /* normal channels should not behave normally in this case */
2673                 ANIM_animdata_freelist(&anim_data);
2674                 return 0;
2675         }
2676
2677         /* action to take depends on what channel we've got */
2678         /* WARNING: must keep this in sync with the equivalent function in nla_channels.c */
2679         switch (ale->type) {
2680                 case ANIMTYPE_SCENE:
2681                 {
2682                         Scene *sce = (Scene *)ale->data;
2683                         AnimData *adt = sce->adt;
2684
2685                         /* set selection status */
2686                         if (selectmode == SELECT_INVERT) {
2687                                 /* swap select */
2688                                 sce->flag ^= SCE_DS_SELECTED;
2689                                 if (adt) adt->flag ^= ADT_UI_SELECTED;
2690                         }
2691                         else {
2692                                 sce->flag |= SCE_DS_SELECTED;
2693                                 if (adt) adt->flag |= ADT_UI_SELECTED;
2694                         }
2695
2696                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
2697                         break;
2698                 }
2699                 case ANIMTYPE_OBJECT:
2700                 {
2701 #if 0
2702                         bDopeSheet *ads = (bDopeSheet *)ac->data;
2703                         Scene *sce = (Scene *)ads->source;
2704 #endif
2705                         ViewLayer *view_layer = ac->view_layer;
2706                         Base *base = (Base *)ale->data;
2707                         Object *ob = base->object;
2708                         AnimData *adt = ob->adt;
2709
2710                         /* set selection status */
2711                         if (base->flag & BASE_SELECTABLE) {
2712                                 if (selectmode == SELECT_INVERT) {
2713                                         /* swap select */
2714                                         ED_object_base_select(base, BA_INVERT);
2715                                         BKE_scene_object_base_flag_sync_from_base(base);
2716
2717                                         if (adt) adt->flag ^= ADT_UI_SELECTED;
2718                                 }
2719                                 else {
2720                                         Base *b;
2721
2722                                         /* deselect all */
2723                                         /* TODO: should this deselect all other types of channels too? */
2724                                         for (b = view_layer->object_bases.first; b; b = b->next) {
2725                                                 ED_object_base_select(b, BA_DESELECT);
2726                                                 BKE_scene_object_base_flag_sync_from_base(b);
2727                                                 if (b->object->adt) b->object->adt->flag &= ~(ADT_UI_SELECTED | ADT_UI_ACTIVE);
2728                                         }
2729
2730                                         /* select object now */
2731                                         ED_object_base_select(base, BA_SELECT);
2732                                         BKE_scene_object_base_flag_sync_from_base(base);
2733                                         if (adt) adt->flag |= ADT_UI_SELECTED;
2734                                 }
2735
2736                                 /* change active object - regardless of whether it is now selected [T37883] */
2737                                 ED_object_base_activate(C, base); /* adds notifier */
2738
2739                                 if ((adt) && (adt->flag & ADT_UI_SELECTED))
2740                                         adt->flag |= ADT_UI_ACTIVE;
2741
2742                                 /* ensure we exit editmode on whatever object was active before to avoid getting stuck there - T48747 */
2743                                 if (ob != CTX_data_edit_object(C)) {
2744                                         ED_object_editmode_exit(C, EM_FREEDATA);
2745                                 }
2746
2747                                 notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
2748                         }
2749                         break;
2750                 }
2751                 case ANIMTYPE_FILLACTD: /* Action Expander */
2752                 case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
2753                 case ANIMTYPE_DSLAM:
2754                 case ANIMTYPE_DSCAM:
2755                 case ANIMTYPE_DSCACHEFILE:
2756                 case ANIMTYPE_DSCUR:
2757                 case ANIMTYPE_DSSKEY:
2758                 case ANIMTYPE_DSWOR:
2759                 case ANIMTYPE_DSPART:
2760                 case ANIMTYPE_DSMBALL:
2761                 case ANIMTYPE_DSARM:
2762                 case ANIMTYPE_DSMESH:
2763                 case ANIMTYPE_DSNTREE:
2764                 case ANIMTYPE_DSTEX:
2765                 case ANIMTYPE_DSLAT:
2766                 case ANIMTYPE_DSLINESTYLE:
2767                 case ANIMTYPE_DSSPK:
2768                 case ANIMTYPE_DSGPENCIL:
2769                 case ANIMTYPE_DSMCLIP:
2770                 {
2771                         /* sanity checking... */
2772                         if (ale->adt) {
2773                                 /* select/deselect */
2774                                 if (selectmode == SELECT_INVERT) {
2775                                         /* inverse selection status of this AnimData block only */
2776                                         ale->adt->flag ^= ADT_UI_SELECTED;
2777                                 }
2778                                 else {
2779                                         /* select AnimData block by itself */
2780                                         ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2781                                         ale->adt->flag |= ADT_UI_SELECTED;
2782                                 }
2783
2784                                 /* set active? */
2785                                 if ((ale->adt) && (ale->adt->flag & ADT_UI_SELECTED))
2786                                         ale->adt->flag |= ADT_UI_ACTIVE;
2787                         }
2788
2789                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
2790                         break;
2791                 }
2792                 case ANIMTYPE_GROUP:
2793                 {
2794                         bActionGroup *agrp = (bActionGroup *)ale->data;
2795
2796                         Object *ob = NULL;
2797                         bPoseChannel *pchan = NULL;
2798
2799
2800                         /* Armatures-Specific Feature:
2801                          * Since groups are used to collect F-Curves of the same Bone by default
2802                          * (via Keying Sets) so that they can be managed better, we try to make
2803                          * things here easier for animators by mapping group selection to bone
2804                          * selection.
2805                          *
2806                          * Only do this if "Only Selected" dopesheet filter is not active, or else it
2807                          * becomes too unpredictable/tricky to manage
2808                          */
2809                         if ((ac->ads->filterflag & ADS_FILTER_ONLYSEL) == 0) {
2810                                 if ((ale->id) && (GS(ale->id->name) == ID_OB)) {
2811                                         ob = (Object *)ale->id;
2812
2813                                         if (ob->type == OB_ARMATURE) {
2814                                                 /* Assume for now that any group with corresponding name is what we want
2815                                                  * (i.e. for an armature whose location is animated, things would break
2816                                                  * if the user were to add a bone named "Location").
2817                                                  *
2818                                                  * TODO: check the first F-Curve or so to be sure...
2819                                                  */
2820                                                 pchan = BKE_pose_channel_find_name(ob->pose, agrp->name);
2821                                         }
2822                                 }
2823                         }
2824
2825                         /* select/deselect group */
2826                         if (selectmode == SELECT_INVERT) {
2827                                 /* inverse selection status of this group only */
2828                                 agrp->flag ^= AGRP_SELECTED;
2829                         }
2830                         else if (selectmode == -1) {
2831                                 /* select all in group (and deselect everything else) */
2832                                 FCurve *fcu;
2833
2834                                 /* deselect all other channels */
2835                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2836                                 if (pchan) ED_pose_deselect_all(ob, SEL_DESELECT, false);
2837
2838                                 /* only select channels in group and group itself */
2839                                 for (fcu = agrp->channels.first; fcu && fcu->grp == agrp; fcu = fcu->next)
2840                                         fcu->flag |= FCURVE_SELECTED;
2841                                 agrp->flag |= AGRP_SELECTED;
2842                         }
2843                         else {
2844                                 /* select group by itself */
2845                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2846                                 if (pchan) ED_pose_deselect_all(ob, SEL_DESELECT, false);
2847
2848                                 agrp->flag |= AGRP_SELECTED;
2849                         }
2850
2851                         /* if group is selected now, make group the 'active' one in the visible list */
2852                         if (agrp->flag & AGRP_SELECTED) {
2853                                 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, agrp, ANIMTYPE_GROUP);
2854                                 if (pchan) ED_pose_bone_select(ob, pchan, true);
2855                         }
2856                         else {
2857                                 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, NULL, ANIMTYPE_GROUP);
2858                                 if (pchan) ED_pose_bone_select(ob, pchan, false);
2859                         }
2860
2861                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
2862                         break;
2863                 }
2864                 case ANIMTYPE_FCURVE:
2865                 case ANIMTYPE_NLACURVE:
2866                 {
2867                         FCurve *fcu = (FCurve *)ale->data;
2868
2869                         /* select/deselect */
2870                         if (selectmode == SELECT_INVERT) {
2871                                 /* inverse selection status of this F-Curve only */
2872                                 fcu->flag ^= FCURVE_SELECTED;
2873                         }
2874                         else {
2875                                 /* select F-Curve by itself */
2876                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2877                                 fcu->flag |= FCURVE_SELECTED;
2878                         }
2879
2880                         /* if F-Curve is selected now, make F-Curve the 'active' one in the visible list */
2881                         if (fcu->flag & FCURVE_SELECTED)
2882                                 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, fcu, ale->type);
2883
2884                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
2885                         break;
2886                 }
2887                 case ANIMTYPE_SHAPEKEY:
2888                 {
2889                         KeyBlock *kb = (KeyBlock *)ale->data;
2890
2891                         /* select/deselect */
2892                         if (selectmode == SELECT_INVERT) {
2893                                 /* inverse selection status of this ShapeKey only */
2894                                 kb->flag ^= KEYBLOCK_SEL;
2895                         }
2896                         else {
2897                                 /* select ShapeKey by itself */
2898                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2899                                 kb->flag |= KEYBLOCK_SEL;
2900                         }
2901
2902                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
2903                         break;
2904                 }
2905                 case ANIMTYPE_NLACONTROLS:
2906                 {
2907                         AnimData *adt = (AnimData *)ale->data;
2908
2909                         /* toggle expand
2910                          *   - Although the triangle widget already allows this, since there's nothing else that can be done here now,
2911                          *     let's just use it for easier expand/collapse for now
2912                          */
2913                         adt->flag ^= ADT_NLA_SKEYS_COLLAPSED;
2914
2915                         notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
2916                         break;
2917                 }
2918                 case ANIMTYPE_GPDATABLOCK:
2919                 {
2920                         bGPdata *gpd = (bGPdata *)ale->data;
2921
2922                         /* toggle expand
2923                          * - although the triangle widget already allows this, the whole channel can also be used for this purpose
2924                          */
2925                         gpd->flag ^= GP_DATA_EXPAND;
2926
2927                         notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
2928                         break;
2929                 }
2930                 case ANIMTYPE_GPLAYER:
2931                 {
2932                         bGPdata *gpd = (bGPdata *)ale->id;
2933                         bGPDlayer *gpl = (bGPDlayer *)ale->data;
2934
2935                         /* select/deselect */
2936                         if (selectmode == SELECT_INVERT) {
2937                                 /* invert selection status of this layer only */
2938                                 gpl->flag ^= GP_LAYER_SELECT;
2939                         }
2940                         else {
2941                                 /* select layer by itself */
2942                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2943                                 gpl->flag |= GP_LAYER_SELECT;
2944                         }
2945
2946                         /* change active layer, if this is selected (since we must always have an active layer) */
2947                         if (gpl->flag & GP_LAYER_SELECT) {
2948                                 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, gpl, ANIMTYPE_GPLAYER);
2949                                 /* update other layer status */
2950                                 BKE_gpencil_layer_setactive(gpd, gpl);
2951                         }
2952
2953                         /* Grease Pencil updates */
2954                         WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED | ND_SPACE_PROPERTIES, NULL);
2955                         notifierFlags |= (ND_ANIMCHAN | NA_EDITED); /* Animation Editors updates */
2956                         break;
2957                 }
2958                 case ANIMTYPE_MASKDATABLOCK:
2959                 {
2960                         Mask *mask = (Mask *)ale->data;
2961
2962                         /* toggle expand
2963                          * - although the triangle widget already allows this, the whole channel can also be used for this purpose
2964                          */
2965                         mask->flag ^= MASK_ANIMF_EXPAND;
2966
2967                         notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
2968                         break;
2969                 }
2970                 case ANIMTYPE_MASKLAYER:
2971                 {
2972                         MaskLayer *masklay = (MaskLayer *)ale->data;
2973
2974                         /* select/deselect */
2975                         if (selectmode == SELECT_INVERT) {
2976                                 /* invert selection status of this layer only */
2977                                 masklay->flag ^= MASK_LAYERFLAG_SELECT;
2978                         }
2979                         else {
2980                                 /* select layer by itself */
2981                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, false, ACHANNEL_SETFLAG_CLEAR);
2982                                 masklay->flag |= MASK_LAYERFLAG_SELECT;
2983                         }
2984
2985                         notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
2986                         break;
2987                 }
2988                 default:
2989                         if (G.debug & G_DEBUG)
2990                                 printf("Error: Invalid channel type in mouse_anim_channels()\n");