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