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