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