3b6c7fad47104625f0121b58906fe1c5a95c6a89
[blender.git] / source / blender / editors / animation / anim_channels_edit.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2009 Blender Foundation, Joshua Leung
21  * All rights reserved.
22  *
23  * Contributor(s): Joshua Leung
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <math.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <float.h>
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_blenlib.h"
40 #include "BLI_arithb.h"
41
42 #include "DNA_listBase.h"
43 #include "DNA_anim_types.h"
44 #include "DNA_action_types.h"
45 #include "DNA_armature_types.h"
46 #include "DNA_camera_types.h"
47 #include "DNA_curve_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_particle_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_scene_types.h"
52 #include "DNA_space_types.h"
53 #include "DNA_constraint_types.h"
54 #include "DNA_key_types.h"
55 #include "DNA_lamp_types.h"
56 #include "DNA_material_types.h"
57 #include "DNA_meta_types.h"
58 #include "DNA_userdef_types.h"
59 #include "DNA_gpencil_types.h"
60 #include "DNA_windowmanager_types.h"
61 #include "DNA_world_types.h"
62
63 #include "RNA_access.h"
64 #include "RNA_define.h"
65
66 #include "BKE_animsys.h"
67 #include "BKE_action.h"
68 #include "BKE_depsgraph.h"
69 #include "BKE_fcurve.h"
70 #include "BKE_key.h"
71 #include "BKE_material.h"
72 #include "BKE_object.h"
73 #include "BKE_context.h"
74 #include "BKE_utildefines.h"
75
76 #include "UI_interface.h"
77 #include "UI_resources.h"
78 #include "UI_view2d.h"
79
80 #include "ED_anim_api.h"
81 #include "ED_keyframes_edit.h" // XXX move the select modes out of there!
82 #include "ED_screen.h"
83 #include "ED_space_api.h"
84
85 #include "WM_api.h"
86 #include "WM_types.h"
87
88 /* ************************************************************************** */
89 /* CHANNELS API */
90
91 /* -------------------------- Exposed API ----------------------------------- */
92
93 /* Set the given animation-channel as the active one for the active context */
94 // TODO: extend for animdata types...
95 void ANIM_set_active_channel (bAnimContext *ac, void *data, short datatype, int filter, void *channel_data, short channel_type)
96 {
97         ListBase anim_data = {NULL, NULL};
98         bAnimListElem *ale;
99         
100         /* try to build list of filtered items */
101         ANIM_animdata_filter(ac, &anim_data, filter, data, datatype);
102         if (anim_data.first == NULL)
103                 return;
104                 
105         /* only clear the 'active' flag for the channels of the same type */
106         for (ale= anim_data.first; ale; ale= ale->next) {
107                 /* skip if types don't match */
108                 if (channel_type != ale->type)
109                         continue;
110                 
111                 /* flag to set depends on type */
112                 switch (ale->type) {
113                         case ANIMTYPE_GROUP:
114                         {
115                                 bActionGroup *agrp= (bActionGroup *)ale->data;
116                                 
117                                 ACHANNEL_SET_FLAG(agrp, ACHANNEL_SETFLAG_CLEAR, AGRP_ACTIVE);
118                         }
119                                 break;
120                         case ANIMTYPE_FCURVE:
121                         {
122                                 FCurve *fcu= (FCurve *)ale->data;
123                                 
124                                 ACHANNEL_SET_FLAG(fcu, ACHANNEL_SETFLAG_CLEAR, FCURVE_ACTIVE);
125                         }
126                                 break;
127                         case ANIMTYPE_NLATRACK:
128                         {
129                                 NlaTrack *nlt= (NlaTrack *)ale->data;
130                                 
131                                 ACHANNEL_SET_FLAG(nlt, ACHANNEL_SETFLAG_CLEAR, NLATRACK_ACTIVE);
132                         }
133                                 break;
134                         
135                         case ANIMTYPE_FILLACTD: /* Action Expander */
136                         case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
137                         case ANIMTYPE_DSLAM:
138                         case ANIMTYPE_DSCAM:
139                         case ANIMTYPE_DSCUR:
140                         case ANIMTYPE_DSSKEY:
141                         case ANIMTYPE_DSWOR:
142                         case ANIMTYPE_DSPART:
143                         case ANIMTYPE_DSMBALL:
144                         case ANIMTYPE_DSARM:
145                         {
146                                 /* need to verify that this data is valid for now */
147                                 if (ale->adt) {
148                                         ACHANNEL_SET_FLAG(ale->adt, ACHANNEL_SETFLAG_CLEAR, ADT_UI_ACTIVE);
149                                 }
150                         }
151                                 break;
152                 }
153         }
154         
155         /* set active flag */
156         if (channel_data) {
157                 switch (channel_type) {
158                         case ANIMTYPE_GROUP:
159                         {
160                                 bActionGroup *agrp= (bActionGroup *)channel_data;
161                                 agrp->flag |= AGRP_ACTIVE;
162                         }
163                                 break;
164                         case ANIMTYPE_FCURVE:
165                         {
166                                 FCurve *fcu= (FCurve *)channel_data;
167                                 fcu->flag |= FCURVE_ACTIVE;
168                         }
169                                 break;
170                         case ANIMTYPE_NLATRACK:
171                         {
172                                 NlaTrack *nlt= (NlaTrack *)channel_data;
173                                 nlt->flag |= NLATRACK_ACTIVE;
174                         }
175                                 break;
176                                 
177                         case ANIMTYPE_FILLACTD: /* Action Expander */
178                         case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
179                         case ANIMTYPE_DSLAM:
180                         case ANIMTYPE_DSCAM:
181                         case ANIMTYPE_DSCUR:
182                         case ANIMTYPE_DSSKEY:
183                         case ANIMTYPE_DSWOR:
184                         case ANIMTYPE_DSPART:
185                         case ANIMTYPE_DSMBALL:
186                         case ANIMTYPE_DSARM:
187                         {
188                                 /* need to verify that this data is valid for now */
189                                 if (ale->adt)
190                                         ale->adt->flag |= ADT_UI_ACTIVE;
191                         }
192                                 break;
193                 }
194         }
195         
196         /* clean up */
197         BLI_freelistN(&anim_data);
198 }
199
200 /* Deselect all animation channels 
201  *      - data: pointer to datatype, as contained in bAnimContext
202  *      - datatype: the type of data that 'data' represents (eAnimCont_Types)
203  *      - test: check if deselecting instead of selecting
204  *      - sel: eAnimChannels_SetFlag;
205  */
206 void ANIM_deselect_anim_channels (void *data, short datatype, short test, short sel)
207 {
208         ListBase anim_data = {NULL, NULL};
209         bAnimListElem *ale;
210         int filter;
211         
212         /* filter data */
213         filter= ANIMFILTER_VISIBLE|ANIMFILTER_CHANNELS;
214         ANIM_animdata_filter(NULL, &anim_data, filter, data, datatype);
215         
216         /* See if we should be selecting or deselecting */
217         if (test) {
218                 for (ale= anim_data.first; ale; ale= ale->next) {
219                         if (sel == 0) 
220                                 break;
221                         
222                         switch (ale->type) {
223                                 case ANIMTYPE_SCENE:
224                                         if (ale->flag & SCE_DS_SELECTED)
225                                                 sel= ACHANNEL_SETFLAG_CLEAR;
226                                         break;
227                                 case ANIMTYPE_OBJECT:
228                                 #if 0   /* for now, do not take object selection into account, since it gets too annoying */
229                                         if (ale->flag & SELECT)
230                                                 sel= ACHANNEL_SETFLAG_CLEAR;
231                                 #endif
232                                         break;
233                                 case ANIMTYPE_GROUP:
234                                         if (ale->flag & AGRP_SELECTED)
235                                                 sel= ACHANNEL_SETFLAG_CLEAR;
236                                         break;
237                                 case ANIMTYPE_FCURVE:
238                                         if (ale->flag & FCURVE_SELECTED)
239                                                 sel= ACHANNEL_SETFLAG_CLEAR;
240                                         break;
241                                 case ANIMTYPE_NLATRACK:
242                                         if (ale->flag & NLATRACK_SELECTED)
243                                                 sel= ACHANNEL_SETFLAG_CLEAR;
244                                         break;
245                                         
246                                 case ANIMTYPE_FILLACTD: /* Action Expander */
247                                 case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
248                                 case ANIMTYPE_DSLAM:
249                                 case ANIMTYPE_DSCAM:
250                                 case ANIMTYPE_DSCUR:
251                                 case ANIMTYPE_DSSKEY:
252                                 case ANIMTYPE_DSWOR:
253                                 case ANIMTYPE_DSPART:
254                                 case ANIMTYPE_DSMBALL:
255                                 case ANIMTYPE_DSARM:
256                                 {
257                                         if ((ale->adt) && (ale->adt->flag & ADT_UI_SELECTED))
258                                                 sel= ACHANNEL_SETFLAG_CLEAR;
259                                 }
260                                         break;
261                         }
262                 }
263         }
264                 
265         /* Now set the flags */
266         for (ale= anim_data.first; ale; ale= ale->next) {
267                 switch (ale->type) {
268                         case ANIMTYPE_SCENE:
269                         {
270                                 Scene *scene= (Scene *)ale->data;
271                                 
272                                 ACHANNEL_SET_FLAG(scene, sel, SCE_DS_SELECTED);
273                                 
274                                 if (scene->adt) {
275                                         ACHANNEL_SET_FLAG(scene, sel, ADT_UI_SELECTED);
276                                 }
277                         }
278                                 break;
279                         case ANIMTYPE_OBJECT:
280                         #if 0   /* for now, do not take object selection into account, since it gets too annoying */
281                         {
282                                 Base *base= (Base *)ale->data;
283                                 Object *ob= base->object;
284                                 
285                                 ACHANNEL_SET_FLAG(base, sel, SELECT);
286                                 ACHANNEL_SET_FLAG(ob, sel, SELECT);
287                                 
288                                 if (ob->adt) {
289                                         ACHANNEL_SET_FLAG(ob, sel, ADT_UI_SELECTED);
290                                 }
291                         }
292                         #endif
293                                 break;
294                         case ANIMTYPE_GROUP:
295                         {
296                                 bActionGroup *agrp= (bActionGroup *)ale->data;
297                                 
298                                 ACHANNEL_SET_FLAG(agrp, sel, AGRP_SELECTED);
299                                 agrp->flag &= ~AGRP_ACTIVE;
300                         }
301                                 break;
302                         case ANIMTYPE_FCURVE:
303                         {
304                                 FCurve *fcu= (FCurve *)ale->data;
305                                 
306                                 ACHANNEL_SET_FLAG(fcu, sel, FCURVE_SELECTED);
307                                 fcu->flag &= ~FCURVE_ACTIVE;
308                         }
309                                 break;
310                         case ANIMTYPE_NLATRACK:
311                         {
312                                 NlaTrack *nlt= (NlaTrack *)ale->data;
313                                 
314                                 ACHANNEL_SET_FLAG(nlt, sel, NLATRACK_SELECTED);
315                                 nlt->flag &= ~NLATRACK_ACTIVE;
316                         }
317                                 break;
318                                 
319                         case ANIMTYPE_FILLACTD: /* Action Expander */
320                         case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
321                         case ANIMTYPE_DSLAM:
322                         case ANIMTYPE_DSCAM:
323                         case ANIMTYPE_DSCUR:
324                         case ANIMTYPE_DSSKEY:
325                         case ANIMTYPE_DSWOR:
326                         case ANIMTYPE_DSPART:
327                         case ANIMTYPE_DSMBALL:
328                         case ANIMTYPE_DSARM:
329                         {
330                                 /* need to verify that this data is valid for now */
331                                 if (ale->adt) {
332                                         ACHANNEL_SET_FLAG(ale->adt, sel, ADT_UI_SELECTED);
333                                         ale->adt->flag &= ~ADT_UI_ACTIVE;
334                                 }
335                         }
336                                 break;
337                 }
338         }
339         
340         /* Cleanup */
341         BLI_freelistN(&anim_data);
342 }
343
344 /* ************************************************************************** */
345 /* OPERATORS */
346
347 /* ****************** Operator Utilities ********************************** */
348
349 /* poll callback for being in an Animation Editor channels list region */
350 int animedit_poll_channels_active (bContext *C)
351 {
352         ScrArea *sa= CTX_wm_area(C);
353         
354         /* channels region test */
355         // TODO: could enhance with actually testing if channels region?
356         if (ELEM(NULL, sa, CTX_wm_region(C)))
357                 return 0;
358         /* animation editor test */
359         if (ELEM3(sa->spacetype, SPACE_ACTION, SPACE_IPO, SPACE_NLA) == 0)
360                 return 0;
361                 
362         return 1;
363 }
364
365 /* poll callback for Animation Editor channels list region + not in NLA-tweakmode for NLA */
366 int animedit_poll_channels_nla_tweakmode_off (bContext *C)
367 {
368         ScrArea *sa= CTX_wm_area(C);
369         Scene *scene = CTX_data_scene(C);
370         
371         /* channels region test */
372         // TODO: could enhance with actually testing if channels region?
373         if (ELEM(NULL, sa, CTX_wm_region(C)))
374                 return 0;
375         /* animation editor test */
376         if (ELEM3(sa->spacetype, SPACE_ACTION, SPACE_IPO, SPACE_NLA) == 0)
377                 return 0;
378         
379         /* NLA TweakMode test */        
380         if (sa->spacetype == SPACE_NLA) {
381                 if ((scene == NULL) || (scene->flag & SCE_NLA_EDIT_ON))
382                         return 0;
383         }
384                 
385         return 1;
386 }
387
388 /* ****************** Rearrange Channels Operator ******************* */
389 /* This operator only works for Action Editor mode for now, as having it elsewhere makes things difficult */
390
391 #if 0 // XXX old animation system - needs to be updated for new system...
392
393 /* constants for channel rearranging */
394 /* WARNING: don't change exising ones without modifying rearrange func accordingly */
395 enum {
396         REARRANGE_ACTCHAN_TOP= -2,
397         REARRANGE_ACTCHAN_UP= -1,
398         REARRANGE_ACTCHAN_DOWN= 1,
399         REARRANGE_ACTCHAN_BOTTOM= 2
400 };
401
402 /* make sure all action-channels belong to a group (and clear action's list) */
403 static void split_groups_action_temp (bAction *act, bActionGroup *tgrp)
404 {
405         bActionChannel *achan;
406         bActionGroup *agrp;
407         
408         /* Separate action-channels into lists per group */
409         for (agrp= act->groups.first; agrp; agrp= agrp->next) {
410                 if (agrp->channels.first) {
411                         achan= agrp->channels.last;
412                         act->chanbase.first= achan->next;
413                         
414                         achan= agrp->channels.first;
415                         achan->prev= NULL;
416                         
417                         achan= agrp->channels.last;
418                         achan->next= NULL;
419                 }
420         }
421         
422         /* Initialise memory for temp-group */
423         memset(tgrp, 0, sizeof(bActionGroup));
424         tgrp->flag |= (AGRP_EXPANDED|AGRP_TEMP);
425         strcpy(tgrp->name, "#TempGroup");
426                 
427         /* Move any action-channels not already moved, to the temp group */
428         if (act->chanbase.first) {
429                 /* start of list */
430                 achan= act->chanbase.first;
431                 achan->prev= NULL;
432                 tgrp->channels.first= achan;
433                 act->chanbase.first= NULL;
434                 
435                 /* end of list */
436                 achan= act->chanbase.last;
437                 achan->next= NULL;
438                 tgrp->channels.last= achan;
439                 act->chanbase.last= NULL;
440         }
441         
442         /* Add temp-group to list */
443         BLI_addtail(&act->groups, tgrp);
444 }
445
446 /* link lists of channels that groups have */
447 static void join_groups_action_temp (bAction *act)
448 {
449         bActionGroup *agrp;
450         bActionChannel *achan;
451         
452         for (agrp= act->groups.first; agrp; agrp= agrp->next) {
453                 ListBase tempGroup;
454                 
455                 /* add list of channels to action's channels */
456                 tempGroup= agrp->channels;
457                 addlisttolist(&act->chanbase, &agrp->channels);
458                 agrp->channels= tempGroup;
459                 
460                 /* clear moved flag */
461                 agrp->flag &= ~AGRP_MOVED;
462                 
463                 /* if temp-group... remove from list (but don't free as it's on the stack!) */
464                 if (agrp->flag & AGRP_TEMP) {
465                         BLI_remlink(&act->groups, agrp);
466                         break;
467                 }
468         }
469         
470         /* clear "moved" flag from all achans */
471         for (achan= act->chanbase.first; achan; achan= achan->next) 
472                 achan->flag &= ~ACHAN_MOVED;
473 }
474
475
476 static short rearrange_actchannel_is_ok (Link *channel, short type)
477 {
478         if (type == ANIMTYPE_GROUP) {
479                 bActionGroup *agrp= (bActionGroup *)channel;
480                 
481                 if (SEL_AGRP(agrp) && !(agrp->flag & AGRP_MOVED))
482                         return 1;
483         }
484         else if (type == ANIMTYPE_ACHAN) {
485                 bActionChannel *achan= (bActionChannel *)channel;
486                 
487                 if (VISIBLE_ACHAN(achan) && SEL_ACHAN(achan) && !(achan->flag & ACHAN_MOVED))
488                         return 1;
489         }
490         
491         return 0;
492 }
493
494 static short rearrange_actchannel_after_ok (Link *channel, short type)
495 {
496         if (type == ANIMTYPE_GROUP) {
497                 bActionGroup *agrp= (bActionGroup *)channel;
498                 
499                 if (agrp->flag & AGRP_TEMP)
500                         return 0;
501         }
502         
503         return 1;
504 }
505
506
507 static short rearrange_actchannel_top (ListBase *list, Link *channel, short type)
508 {
509         if (rearrange_actchannel_is_ok(channel, type)) {
510                 /* take it out off the chain keep data */
511                 BLI_remlink(list, channel);
512                 
513                 /* make it first element */
514                 BLI_insertlinkbefore(list, list->first, channel);
515                 
516                 return 1;
517         }
518         
519         return 0;
520 }
521
522 static short rearrange_actchannel_up (ListBase *list, Link *channel, short type)
523 {
524         if (rearrange_actchannel_is_ok(channel, type)) {
525                 Link *prev= channel->prev;
526                 
527                 if (prev) {
528                         /* take it out off the chain keep data */
529                         BLI_remlink(list, channel);
530                         
531                         /* push it up */
532                         BLI_insertlinkbefore(list, prev, channel);
533                         
534                         return 1;
535                 }
536         }
537         
538         return 0;
539 }
540
541 static short rearrange_actchannel_down (ListBase *list, Link *channel, short type)
542 {
543         if (rearrange_actchannel_is_ok(channel, type)) {
544                 Link *next = (channel->next) ? channel->next->next : NULL;
545                 
546                 if (next) {
547                         /* take it out off the chain keep data */
548                         BLI_remlink(list, channel);
549                         
550                         /* move it down */
551                         BLI_insertlinkbefore(list, next, channel);
552                         
553                         return 1;
554                 }
555                 else if (rearrange_actchannel_after_ok(list->last, type)) {
556                         /* take it out off the chain keep data */
557                         BLI_remlink(list, channel);
558                         
559                         /* add at end */
560                         BLI_addtail(list, channel);
561                         
562                         return 1;
563                 }
564                 else {
565                         /* take it out off the chain keep data */
566                         BLI_remlink(list, channel);
567                         
568                         /* add just before end */
569                         BLI_insertlinkbefore(list, list->last, channel);
570                         
571                         return 1;
572                 }
573         }
574         
575         return 0;
576 }
577
578 static short rearrange_actchannel_bottom (ListBase *list, Link *channel, short type)
579 {
580         if (rearrange_actchannel_is_ok(channel, type)) {
581                 if (rearrange_actchannel_after_ok(list->last, type)) {
582                         /* take it out off the chain keep data */
583                         BLI_remlink(list, channel);
584                         
585                         /* add at end */
586                         BLI_addtail(list, channel);
587                         
588                         return 1;
589                 }
590         }
591         
592         return 0;
593 }
594
595
596 /* Change the order of action-channels 
597  *      mode: REARRANGE_ACTCHAN_*  
598  */
599 static void rearrange_action_channels (bAnimContext *ac, short mode)
600 {
601         bAction *act;
602         bActionChannel *achan, *chan;
603         bActionGroup *agrp, *grp;
604         bActionGroup tgrp;
605         
606         short (*rearrange_func)(ListBase *, Link *, short);
607         short do_channels = 1;
608         
609         /* Get the active action, exit if none are selected */
610         act= (bAction *)ac->data;
611         
612         /* exit if invalid mode */
613         switch (mode) {
614                 case REARRANGE_ACTCHAN_TOP:
615                         rearrange_func= rearrange_actchannel_top;
616                         break;
617                 case REARRANGE_ACTCHAN_UP:
618                         rearrange_func= rearrange_actchannel_up;
619                         break;
620                 case REARRANGE_ACTCHAN_DOWN:
621                         rearrange_func= rearrange_actchannel_down;
622                         break;
623                 case REARRANGE_ACTCHAN_BOTTOM:
624                         rearrange_func= rearrange_actchannel_bottom;
625                         break;
626                 default:
627                         return;
628         }
629         
630         /* make sure we're only operating with groups */
631         split_groups_action_temp(act, &tgrp);
632         
633         /* rearrange groups first (and then, only consider channels if the groups weren't moved) */
634         #define GET_FIRST(list) ((mode > 0) ? (list.first) : (list.last))
635         #define GET_NEXT(item) ((mode > 0) ? (item->next) : (item->prev))
636         
637         for (agrp= GET_FIRST(act->groups); agrp; agrp= grp) {
638                 /* Get next group to consider */
639                 grp= GET_NEXT(agrp);
640                 
641                 /* try to do group first */
642                 if (rearrange_func(&act->groups, (Link *)agrp, ANIMTYPE_GROUP)) {
643                         do_channels= 0;
644                         agrp->flag |= AGRP_MOVED;
645                 }
646         }
647         
648         if (do_channels) {
649                 for (agrp= GET_FIRST(act->groups); agrp; agrp= grp) {
650                         /* Get next group to consider */
651                         grp= GET_NEXT(agrp);
652                         
653                         /* only consider action-channels if they're visible (group expanded) */
654                         if (EXPANDED_AGRP(agrp)) {
655                                 for (achan= GET_FIRST(agrp->channels); achan; achan= chan) {
656                                         /* Get next channel to consider */
657                                         chan= GET_NEXT(achan);
658                                         
659                                         /* Try to do channel */
660                                         if (rearrange_func(&agrp->channels, (Link *)achan, ANIMTYPE_ACHAN))
661                                                 achan->flag |= ACHAN_MOVED;
662                                 }
663                         }
664                 }
665         }
666         #undef GET_FIRST
667         #undef GET_NEXT
668         
669         /* assemble lists into one list (and clear moved tags) */
670         join_groups_action_temp(act);
671 }
672
673 /* ------------------- */
674
675 static int animchannels_rearrange_exec(bContext *C, wmOperator *op)
676 {
677         bAnimContext ac;
678         short mode;
679         
680         /* get editor data - only for Action Editor (for now) */
681         if (ANIM_animdata_get_context(C, &ac) == 0)
682                 return OPERATOR_CANCELLED;
683         if (ac.datatype != ANIMCONT_ACTION)
684                 return OPERATOR_PASS_THROUGH;
685                 
686         /* get mode, then rearrange channels */
687         mode= RNA_enum_get(op->ptr, "direction");
688         rearrange_action_channels(&ac, mode);
689         
690         /* send notifier that things have changed */
691         WM_event_add_notifier(C, NC_ANIMATION|ND_ANIMCHAN_EDIT, NULL);
692         
693         return OPERATOR_FINISHED;
694 }
695  
696
697 void ANIM_OT_channels_move_up (wmOperatorType *ot)
698 {
699         /* identifiers */
700         ot->name= "Move Channel(s) Up";
701         ot->idname= "ANIM_OT_channels_move_up";
702         
703         /* api callbacks */
704         ot->exec= animchannels_rearrange_exec;
705         ot->poll= ED_operator_areaactive;
706         
707         /* flags */
708         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
709         
710         /* props */
711         RNA_def_enum(ot->srna, "direction", NULL /* XXX add enum for this */, REARRANGE_ACTCHAN_UP, "Direction", "");
712 }
713
714 void ANIM_OT_channels_move_down (wmOperatorType *ot)
715 {
716         /* identifiers */
717         ot->name= "Move Channel(s) Down";
718         ot->idname= "ANIM_OT_channels_move_down";
719         
720         /* api callbacks */
721         ot->exec= animchannels_rearrange_exec;
722         ot->poll= ED_operator_areaactive;
723         
724         /* flags */
725         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
726         
727         /* props */
728         RNA_def_enum(ot->srna, "direction", NULL /* XXX add enum for this */, REARRANGE_ACTCHAN_DOWN, "Direction", "");
729 }
730
731 void ANIM_OT_channels_move_top (wmOperatorType *ot)
732 {
733         /* identifiers */
734         ot->name= "Move Channel(s) to Top";
735         ot->idname= "ANIM_OT_channels_move_to_top";
736         
737         /* api callbacks */
738         ot->exec= animchannels_rearrange_exec;
739         ot->poll= ED_operator_areaactive;
740         
741         /* flags */
742         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
743         
744         /* props */
745         RNA_def_enum(ot->srna, "direction", NULL /* XXX add enum for this */, REARRANGE_ACTCHAN_TOP, "Direction", "");
746 }
747
748 void ANIM_OT_channels_move_bottom (wmOperatorType *ot)
749 {
750         /* identifiers */
751         ot->name= "Move Channel(s) to Bottom";
752         ot->idname= "ANIM_OT_channels_move_to_bottom";
753         
754         /* api callbacks */
755         ot->exec= animchannels_rearrange_exec;
756         ot->poll= ED_operator_areaactive;
757         
758         /* flags */
759         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
760         
761         /* props */
762         RNA_def_enum(ot->srna, "direction", NULL /* XXX add enum for this */, REARRANGE_ACTCHAN_BOTTOM, "Direction", "");
763 }
764
765 #endif // XXX old animation system - needs to be updated for new system...
766
767 /* ******************** Delete Channel Operator *********************** */
768
769 static int animchannels_delete_exec(bContext *C, wmOperator *op)
770 {
771         bAnimContext ac;
772         ListBase anim_data = {NULL, NULL};
773         bAnimListElem *ale;
774         int filter;
775         
776         /* get editor data */
777         if (ANIM_animdata_get_context(C, &ac) == 0)
778                 return OPERATOR_CANCELLED;
779         
780         /* cannot delete in shapekey */
781         if (ac.datatype == ANIMCONT_SHAPEKEY) 
782                 return OPERATOR_CANCELLED;
783                 
784                 
785         /* do groups only first (unless in Drivers mode, where there are none) */
786         if (ac.datatype != ANIMCONT_DRIVERS) {
787                 /* filter data */
788                 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_SEL | ANIMFILTER_CHANNELS | ANIMFILTER_FOREDIT);
789                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
790                 
791                 /* delete selected groups and their associated channels */
792                 for (ale= anim_data.first; ale; ale= ale->next) {
793                         /* only groups - don't check other types yet, since they may no-longer exist */
794                         if (ale->type == ANIMTYPE_GROUP) {
795                                 bActionGroup *agrp= (bActionGroup *)ale->data;
796                                 AnimData *adt= ale->adt;
797                                 FCurve *fcu, *fcn;
798                                 
799                                 /* skip this group if no AnimData available, as we can't safely remove the F-Curves */
800                                 if (adt == NULL)
801                                         continue;
802                                 
803                                 /* delete all of the Group's F-Curves, but no others */
804                                 for (fcu= agrp->channels.first; fcu && fcu->grp==agrp; fcu= fcn) {
805                                         fcn= fcu->next;
806                                         
807                                         /* remove from group and action, then free */
808                                         action_groups_remove_channel(adt->action, fcu);
809                                         free_fcurve(fcu);
810                                 }
811                                 
812                                 /* free the group itself */
813                                 if (adt->action)
814                                         BLI_freelinkN(&adt->action->groups, agrp);
815                                 else
816                                         MEM_freeN(agrp);
817                         }
818                 }
819                 
820                 /* cleanup */
821                 BLI_freelistN(&anim_data);
822         }
823         
824         /* now do F-Curves */
825         if (ac.datatype != ANIMCONT_GPENCIL) {
826                 /* filter data */
827                 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_SEL | ANIMFILTER_FOREDIT);
828                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
829                 
830                 /* delete selected F-Curves */
831                 for (ale= anim_data.first; ale; ale= ale->next) {
832                         /* only F-Curves, and only if we can identify its parent */
833                         if (ale->type == ANIMTYPE_FCURVE) {
834                                 AnimData *adt= ale->adt;
835                                 FCurve *fcu= (FCurve *)ale->data;
836                                 
837                                 /* if no AnimData, we've got nowhere to remove the F-Curve from */
838                                 if (adt == NULL)
839                                         continue;
840                                         
841                                 /* remove from whatever list it came from
842                                  *      - Action Group
843                                  *      - Action
844                                  *      - Drivers
845                                  *      - TODO... some others?
846                                  */
847                                 if (fcu->grp)
848                                         action_groups_remove_channel(adt->action, fcu);
849                                 else if (adt->action)
850                                         BLI_remlink(&adt->action->curves, fcu);
851                                 else if (ac.datatype == ANIMCONT_DRIVERS)
852                                         BLI_remlink(&adt->drivers, fcu);
853                                         
854                                 /* free the F-Curve itself */
855                                 free_fcurve(fcu);
856                         }
857                 }
858                 
859                 /* cleanup */
860                 BLI_freelistN(&anim_data);
861         }
862         
863         /* send notifier that things have changed */
864         WM_event_add_notifier(C, NC_ANIMATION|ND_ANIMCHAN_EDIT, NULL);
865         
866         return OPERATOR_FINISHED;
867 }
868  
869 void ANIM_OT_channels_delete (wmOperatorType *ot)
870 {
871         /* identifiers */
872         ot->name= "Delete Channels";
873         ot->idname= "ANIM_OT_channels_delete";
874         ot->description= "Delete all selected animation channels.";
875         
876         /* api callbacks */
877         ot->exec= animchannels_delete_exec;
878         ot->poll= animedit_poll_channels_active;
879         
880         /* flags */
881         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
882 }
883
884 /* ******************** Toggle Channel Visibility Operator *********************** */
885
886 static int animchannels_visibility_toggle_exec(bContext *C, wmOperator *op)
887 {
888         bAnimContext ac;
889         ListBase anim_data = {NULL, NULL};
890         bAnimListElem *ale;
891         int filter;
892         short vis= ACHANNEL_SETFLAG_ADD;
893         
894         /* get editor data */
895         if (ANIM_animdata_get_context(C, &ac) == 0)
896                 return OPERATOR_CANCELLED;
897                 
898         /* filter data */
899         filter= (ANIMFILTER_VISIBLE | ANIMFILTER_SEL);
900         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
901         
902         /* See if we should be making showing all selected or hiding */
903         for (ale= anim_data.first; ale; ale= ale->next) {
904                 if (vis == ACHANNEL_SETFLAG_CLEAR) 
905                         break;
906                 
907                 /* set the setting in the appropriate way (if available) */
908                 if (ANIM_channel_setting_get(&ac, ale, ACHANNEL_SETTING_VISIBLE))
909                         vis= ACHANNEL_SETFLAG_CLEAR;
910         }
911                 
912         /* Now set the flags */
913         for (ale= anim_data.first; ale; ale= ale->next) {
914                 ANIM_channel_setting_set(&ac, ale, ACHANNEL_SETTING_VISIBLE, vis);
915         }
916         
917         /* cleanup */
918         BLI_freelistN(&anim_data);
919         
920         /* send notifier that things have changed */
921         WM_event_add_notifier(C, NC_ANIMATION|ND_ANIMCHAN_EDIT, NULL);
922         
923         return OPERATOR_FINISHED;
924 }
925  
926 void ANIM_OT_channels_visibility_toggle (wmOperatorType *ot)
927 {
928         /* identifiers */
929         ot->name= "Toggle Visibility";
930         ot->idname= "ANIM_OT_channels_visibility_toggle";
931         ot->description= "Toggle visibility in Graph Editor of all selected animation channels.";
932         
933         /* api callbacks */
934         ot->exec= animchannels_visibility_toggle_exec;
935         ot->poll= ED_operator_ipo_active;
936         
937         /* flags */
938         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
939 }
940
941 /* ********************** Set Flags Operator *********************** */
942
943 /* defines for setting animation-channel flags */
944 EnumPropertyItem prop_animchannel_setflag_types[] = {
945         {ACHANNEL_SETFLAG_CLEAR, "DISABLE", 0, "Disable", ""},
946         {ACHANNEL_SETFLAG_ADD, "ENABLE", 0, "Enable", ""},
947         {ACHANNEL_SETFLAG_TOGGLE, "TOGGLE", 0, "Toggle", ""},
948         {0, NULL, 0, NULL, NULL}
949 };
950
951 /* defines for set animation-channel settings */
952 // TODO: could add some more types, but those are really quite dependent on the mode...
953 EnumPropertyItem prop_animchannel_settings_types[] = {
954         {ACHANNEL_SETTING_PROTECT, "PROTECT", 0, "Protect", ""},
955         {ACHANNEL_SETTING_MUTE, "MUTE", 0, "Mute", ""},
956         {0, NULL, 0, NULL, NULL}
957 };
958
959
960 /* ------------------- */
961
962 /* macro to be used in setflag_anim_channels */
963 #define ASUBCHANNEL_SEL_OK(ale) ( (onlysel == 0) || \
964                 ((ale->id) && (GS(ale->id->name)==ID_OB) && (((Object *)ale->id)->flag & SELECT)) ) 
965
966 /* Set/clear a particular flag (setting) for all selected + visible channels 
967  *      setting: the setting to modify
968  *      mode: eAnimChannels_SetFlag
969  *      onlysel: only selected channels get the flag set
970  */
971 static void setflag_anim_channels (bAnimContext *ac, short setting, short mode, short onlysel)
972 {
973         ListBase anim_data = {NULL, NULL};
974         bAnimListElem *ale;
975         int filter;
976         
977         /* filter data */
978         filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CHANNELS);
979         if (onlysel) filter |= ANIMFILTER_SEL;
980         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
981         
982         /* affect selected channels */
983         for (ale= anim_data.first; ale; ale= ale->next) {
984                 /* set the setting in the appropriate way (if available) */
985                 ANIM_channel_setting_set(ac, ale, setting, mode);
986         }
987         
988         BLI_freelistN(&anim_data);
989 }
990
991 /* ------------------- */
992
993 static int animchannels_setflag_exec(bContext *C, wmOperator *op)
994 {
995         bAnimContext ac;
996         short mode, setting;
997         
998         /* get editor data */
999         if (ANIM_animdata_get_context(C, &ac) == 0)
1000                 return OPERATOR_CANCELLED;
1001                 
1002         /* mode (eAnimChannels_SetFlag), setting (eAnimChannel_Settings) */
1003         mode= RNA_enum_get(op->ptr, "mode");
1004         setting= RNA_enum_get(op->ptr, "type");
1005         
1006         /* modify setting */
1007         setflag_anim_channels(&ac, setting, mode, 1);
1008         
1009         /* send notifier that things have changed */
1010         WM_event_add_notifier(C, NC_ANIMATION|ND_ANIMCHAN_EDIT, NULL);
1011         
1012         return OPERATOR_FINISHED;
1013 }
1014
1015
1016 void ANIM_OT_channels_setting_enable (wmOperatorType *ot)
1017 {
1018         /* identifiers */
1019         ot->name= "Enable Channel Setting";
1020         ot->idname= "ANIM_OT_channels_setting_enable";
1021         ot->description= "Enable specified setting on all selected animation channels.";
1022         
1023         /* api callbacks */
1024         ot->invoke= WM_menu_invoke;
1025         ot->exec= animchannels_setflag_exec;
1026         ot->poll= animedit_poll_channels_active;
1027         
1028         /* flags */
1029         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1030         
1031         /* props */
1032                 /* flag-setting mode */
1033         RNA_def_enum(ot->srna, "mode", prop_animchannel_setflag_types, ACHANNEL_SETFLAG_ADD, "Mode", "");
1034                 /* setting to set */
1035         RNA_def_enum(ot->srna, "type", prop_animchannel_settings_types, 0, "Type", "");
1036 }
1037
1038 void ANIM_OT_channels_setting_disable (wmOperatorType *ot)
1039 {
1040         /* identifiers */
1041         ot->name= "Disable Channel Setting";
1042         ot->idname= "ANIM_OT_channels_setting_disable";
1043         ot->description= "Disable specified setting on all selected animation channels.";
1044         
1045         /* api callbacks */
1046         ot->invoke= WM_menu_invoke;
1047         ot->exec= animchannels_setflag_exec;
1048         ot->poll= animedit_poll_channels_active;
1049         
1050         /* flags */
1051         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1052         
1053         /* props */
1054                 /* flag-setting mode */
1055         RNA_def_enum(ot->srna, "mode", prop_animchannel_setflag_types, ACHANNEL_SETFLAG_CLEAR, "Mode", "");
1056                 /* setting to set */
1057         RNA_def_enum(ot->srna, "type", prop_animchannel_settings_types, 0, "Type", "");
1058 }
1059
1060 void ANIM_OT_channels_setting_toggle (wmOperatorType *ot)
1061 {
1062         /* identifiers */
1063         ot->name= "Toggle Channel Setting";
1064         ot->idname= "ANIM_OT_channels_setting_toggle";
1065         ot->description= "Toggle specified setting on all selected animation channels.";
1066         
1067         /* api callbacks */
1068         ot->invoke= WM_menu_invoke;
1069         ot->exec= animchannels_setflag_exec;
1070         ot->poll= animedit_poll_channels_active;
1071         
1072         /* flags */
1073         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1074         
1075         /* props */
1076                 /* flag-setting mode */
1077         RNA_def_enum(ot->srna, "mode", prop_animchannel_setflag_types, ACHANNEL_SETFLAG_TOGGLE, "Mode", "");
1078                 /* setting to set */
1079         RNA_def_enum(ot->srna, "type", prop_animchannel_settings_types, 0, "Type", "");
1080 }
1081
1082 // XXX currently, this is a separate operator, but perhaps we could in future specify in keymaps whether to call invoke or exec?
1083 void ANIM_OT_channels_editable_toggle (wmOperatorType *ot)
1084 {
1085         /* identifiers */
1086         ot->name= "Toggle Channel Editability";
1087         ot->idname= "ANIM_OT_channels_editable_toggle";
1088         ot->description= "Toggle editability of selected channels.";
1089         
1090         /* api callbacks */
1091         ot->exec= animchannels_setflag_exec;
1092         ot->poll= animedit_poll_channels_active;
1093         
1094         /* flags */
1095         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1096         
1097         /* props */
1098                 /* flag-setting mode */
1099         RNA_def_enum(ot->srna, "mode", prop_animchannel_setflag_types, ACHANNEL_SETFLAG_TOGGLE, "Mode", "");
1100                 /* setting to set */
1101         RNA_def_enum(ot->srna, "type", prop_animchannel_settings_types, ACHANNEL_SETTING_PROTECT, "Type", "");
1102 }
1103
1104 /* ********************** Expand Channels Operator *********************** */
1105
1106 static int animchannels_expand_exec (bContext *C, wmOperator *op)
1107 {
1108         bAnimContext ac;
1109         short onlysel= 1;
1110         
1111         /* get editor data */
1112         if (ANIM_animdata_get_context(C, &ac) == 0)
1113                 return OPERATOR_CANCELLED;
1114                 
1115         /* only affect selected channels? */
1116         if (RNA_boolean_get(op->ptr, "all"))
1117                 onlysel= 0;
1118         
1119         /* modify setting */
1120         setflag_anim_channels(&ac, ACHANNEL_SETTING_EXPAND, ACHANNEL_SETFLAG_ADD, onlysel);
1121         
1122         /* send notifier that things have changed */
1123         WM_event_add_notifier(C, NC_ANIMATION|ND_ANIMCHAN_EDIT, NULL);
1124         
1125         return OPERATOR_FINISHED;
1126 }
1127
1128 void ANIM_OT_channels_expand (wmOperatorType *ot)
1129 {
1130         /* identifiers */
1131         ot->name= "Expand Channels";
1132         ot->idname= "ANIM_OT_channels_expand";
1133         ot->description= "Expand (i.e. open) all selected expandable animation channels.";
1134         
1135         /* api callbacks */
1136         ot->exec= animchannels_expand_exec;
1137         ot->poll= animedit_poll_channels_active;
1138         
1139         /* flags */
1140         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1141         
1142         /* props */
1143         RNA_def_boolean(ot->srna, "all", 0, "All", "Expand all channels (not just selected ones)");
1144 }
1145
1146 /* ********************** Collapse Channels Operator *********************** */
1147
1148 static int animchannels_collapse_exec (bContext *C, wmOperator *op)
1149 {
1150         bAnimContext ac;
1151         short onlysel= 1;
1152         
1153         /* get editor data */
1154         if (ANIM_animdata_get_context(C, &ac) == 0)
1155                 return OPERATOR_CANCELLED;
1156                 
1157         /* only affect selected channels? */
1158         if (RNA_boolean_get(op->ptr, "all"))
1159                 onlysel= 0;
1160         
1161         /* modify setting */
1162         setflag_anim_channels(&ac, ACHANNEL_SETTING_EXPAND, ACHANNEL_SETFLAG_CLEAR, onlysel);
1163         
1164         /* send notifier that things have changed */
1165         WM_event_add_notifier(C, NC_ANIMATION|ND_ANIMCHAN_EDIT, NULL);
1166         
1167         return OPERATOR_FINISHED;
1168 }
1169
1170 void ANIM_OT_channels_collapse (wmOperatorType *ot)
1171 {
1172         /* identifiers */
1173         ot->name= "Collapse Channels";
1174         ot->idname= "ANIM_OT_channels_collapse";
1175         ot->description= "Collapse (i.e. close) all selected expandable animation channels.";
1176         
1177         /* api callbacks */
1178         ot->exec= animchannels_collapse_exec;
1179         ot->poll= animedit_poll_channels_active;
1180         
1181         /* flags */
1182         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1183         
1184         /* props */
1185         RNA_def_boolean(ot->srna, "all", 0, "All", "Collapse all channels (not just selected ones)");
1186 }
1187
1188 /* ********************** Select All Operator *********************** */
1189
1190 static int animchannels_deselectall_exec(bContext *C, wmOperator *op)
1191 {
1192         bAnimContext ac;
1193         
1194         /* get editor data */
1195         if (ANIM_animdata_get_context(C, &ac) == 0)
1196                 return OPERATOR_CANCELLED;
1197                 
1198         /* 'standard' behaviour - check if selected, then apply relevant selection */
1199         if (RNA_boolean_get(op->ptr, "invert"))
1200                 ANIM_deselect_anim_channels(ac.data, ac.datatype, 0, ACHANNEL_SETFLAG_TOGGLE);
1201         else
1202                 ANIM_deselect_anim_channels(ac.data, ac.datatype, 1, ACHANNEL_SETFLAG_ADD);
1203         
1204         /* send notifier that things have changed */
1205         WM_event_add_notifier(C, NC_ANIMATION|ND_ANIMCHAN_SELECT, NULL);
1206         
1207         return OPERATOR_FINISHED;
1208 }
1209  
1210 void ANIM_OT_channels_select_all_toggle (wmOperatorType *ot)
1211 {
1212         /* identifiers */
1213         ot->name= "Select All";
1214         ot->idname= "ANIM_OT_channels_select_all_toggle";
1215         ot->description= "Toggle selection of all animation channels.";
1216         
1217         /* api callbacks */
1218         ot->exec= animchannels_deselectall_exec;
1219         ot->poll= animedit_poll_channels_nla_tweakmode_off;
1220         
1221         /* flags */
1222         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1223         
1224         /* props */
1225         RNA_def_boolean(ot->srna, "invert", 0, "Invert", "");
1226 }
1227
1228 /* ******************** Borderselect Operator *********************** */
1229
1230 static void borderselect_anim_channels (bAnimContext *ac, rcti *rect, short selectmode)
1231 {
1232         ListBase anim_data = {NULL, NULL};
1233         bAnimListElem *ale;
1234         int filter;
1235         
1236         View2D *v2d= &ac->ar->v2d;
1237         rctf rectf;
1238         float ymin=0, ymax=(float)(-ACHANNEL_HEIGHT);
1239         
1240         /* convert border-region to view coordinates */
1241         UI_view2d_region_to_view(v2d, rect->xmin, rect->ymin+2, &rectf.xmin, &rectf.ymin);
1242         UI_view2d_region_to_view(v2d, rect->xmax, rect->ymax-2, &rectf.xmax, &rectf.ymax);
1243         
1244         /* filter data */
1245         filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CHANNELS);
1246         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
1247         
1248         /* loop over data, doing border select */
1249         for (ale= anim_data.first; ale; ale= ale->next) {
1250                 ymin= ymax - ACHANNEL_STEP;
1251                 
1252                 /* if channel is within border-select region, alter it */
1253                 if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))) {
1254                         /* set selection flags only */
1255                         ANIM_channel_setting_set(ac, ale, ACHANNEL_SETTING_SELECT, selectmode);
1256                         
1257                         /* type specific actions */
1258                         switch (ale->type) {
1259                                 case ANIMTYPE_GROUP:
1260                                 {
1261                                         bActionGroup *agrp= (bActionGroup *)ale->data;
1262                                         
1263                                         /* always clear active flag after doing this */
1264                                         agrp->flag &= ~AGRP_ACTIVE;
1265                                 }
1266                                         break;
1267                         }
1268                 }
1269                 
1270                 /* set minimum extent to be the maximum of the next channel */
1271                 ymax= ymin;
1272         }
1273         
1274         /* cleanup */
1275         BLI_freelistN(&anim_data);
1276 }
1277
1278 /* ------------------- */
1279
1280 static int animchannels_borderselect_exec(bContext *C, wmOperator *op)
1281 {
1282         bAnimContext ac;
1283         rcti rect;
1284         short selectmode=0;
1285         int event;
1286         
1287         /* get editor data */
1288         if (ANIM_animdata_get_context(C, &ac) == 0)
1289                 return OPERATOR_CANCELLED;
1290         
1291         /* get settings from operator */
1292         rect.xmin= RNA_int_get(op->ptr, "xmin");
1293         rect.ymin= RNA_int_get(op->ptr, "ymin");
1294         rect.xmax= RNA_int_get(op->ptr, "xmax");
1295         rect.ymax= RNA_int_get(op->ptr, "ymax");
1296                 
1297         event= RNA_int_get(op->ptr, "event_type");
1298         if (event == LEFTMOUSE) // FIXME... hardcoded
1299                 selectmode = ACHANNEL_SETFLAG_ADD;
1300         else
1301                 selectmode = ACHANNEL_SETFLAG_CLEAR;
1302         
1303         /* apply borderselect animation channels */
1304         borderselect_anim_channels(&ac, &rect, selectmode);
1305         
1306         /* send notifier that things have changed */
1307         WM_event_add_notifier(C, NC_ANIMATION|ND_ANIMCHAN_SELECT, NULL);
1308         
1309         return OPERATOR_FINISHED;
1310
1311
1312 void ANIM_OT_channels_select_border(wmOperatorType *ot)
1313 {
1314         /* identifiers */
1315         ot->name= "Border Select";
1316         ot->idname= "ANIM_OT_channels_select_border";
1317         ot->description= "Select all animation channels within the specified region.";
1318         
1319         /* api callbacks */
1320         ot->invoke= WM_border_select_invoke;
1321         ot->exec= animchannels_borderselect_exec;
1322         ot->modal= WM_border_select_modal;
1323         
1324         ot->poll= animedit_poll_channels_nla_tweakmode_off;
1325         
1326         /* flags */
1327         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1328         
1329         /* rna */
1330         RNA_def_int(ot->srna, "event_type", 0, INT_MIN, INT_MAX, "Event Type", "", INT_MIN, INT_MAX);
1331         RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
1332         RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
1333         RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
1334         RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
1335 }
1336
1337 /* ******************** Mouse-Click Operator *********************** */
1338 /* Handle selection changes due to clicking on channels. Settings will get caught by UI code... */
1339
1340 static int mouse_anim_channels (bAnimContext *ac, float x, int channel_index, short selectmode)
1341 {
1342         ListBase anim_data = {NULL, NULL};
1343         bAnimListElem *ale;
1344         int filter;
1345         int notifierFlags = 0;
1346         
1347         /* get the channel that was clicked on */
1348                 /* filter channels */
1349         filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CHANNELS);
1350         filter= ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
1351         
1352                 /* get channel from index */
1353         ale= BLI_findlink(&anim_data, channel_index);
1354         if (ale == NULL) {
1355                 /* channel not found */
1356                 printf("Error: animation channel (index = %d) not found in mouse_anim_channels() \n", channel_index);
1357                 
1358                 BLI_freelistN(&anim_data);
1359                 return 0;
1360         }
1361         
1362         /* selectmode -1 is a special case for ActionGroups only, which selects all of the channels underneath it only... */
1363         // TODO: should this feature be extended to work with other channel types too?
1364         if ((selectmode == -1) && (ale->type != ANIMTYPE_GROUP)) {
1365                 /* normal channels should not behave normally in this case */
1366                 BLI_freelistN(&anim_data);
1367                 return 0;
1368         }
1369         
1370         /* action to take depends on what channel we've got */
1371         // WARNING: must keep this in sync with the equivalent function in nla_channels.c
1372         switch (ale->type) {
1373                 case ANIMTYPE_SCENE:
1374                 {
1375                         Scene *sce= (Scene *)ale->data;
1376                         AnimData *adt= sce->adt;
1377                         
1378                         /* set selection status */
1379                         if (selectmode == SELECT_INVERT) {
1380                                 /* swap select */
1381                                 sce->flag ^= SCE_DS_SELECTED;
1382                                 if (adt) adt->flag ^= ADT_UI_SELECTED;
1383                         }
1384                         else {
1385                                 sce->flag |= SCE_DS_SELECTED;
1386                                 if (adt) adt->flag |= ADT_UI_SELECTED;
1387                         }
1388                         
1389                         notifierFlags |= ND_ANIMCHAN_SELECT;
1390                 }
1391                         break;
1392                 case ANIMTYPE_OBJECT:
1393                 {
1394                         bDopeSheet *ads= (bDopeSheet *)ac->data;
1395                         Scene *sce= (Scene *)ads->source;
1396                         Base *base= (Base *)ale->data;
1397                         Object *ob= base->object;
1398                         AnimData *adt= ob->adt;
1399                         
1400                         /* set selection status */
1401                         if (selectmode == SELECT_INVERT) {
1402                                 /* swap select */
1403                                 base->flag ^= SELECT;
1404                                 ob->flag= base->flag;
1405                                 
1406                                 if (adt) adt->flag ^= ADT_UI_SELECTED;
1407                         }
1408                         else {
1409                                 Base *b;
1410                                 
1411                                 /* deselect all */
1412                                 // TODO: should this deselect all other types of channels too?
1413                                 for (b= sce->base.first; b; b= b->next) {
1414                                         b->flag &= ~SELECT;
1415                                         b->object->flag= b->flag;
1416                                         if (b->object->adt) b->object->adt->flag &= ~(ADT_UI_SELECTED|ADT_UI_ACTIVE);
1417                                 }
1418                                 
1419                                 /* select object now */
1420                                 base->flag |= SELECT;
1421                                 ob->flag |= SELECT;
1422                                 if (adt) adt->flag |= ADT_UI_SELECTED;
1423                         }
1424                         
1425                         /* xxx should be ED_base_object_activate(), but we need context pointer for that... */
1426                         //set_active_base(base);
1427                         if ((adt) && (adt->flag & ADT_UI_SELECTED))
1428                                 adt->flag |= ADT_UI_ACTIVE;
1429                         
1430                         notifierFlags |= ND_ANIMCHAN_SELECT;
1431                 }
1432                         break;
1433                 
1434                 case ANIMTYPE_FILLACTD: /* Action Expander */
1435                 case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
1436                 case ANIMTYPE_DSLAM:
1437                 case ANIMTYPE_DSCAM:
1438                 case ANIMTYPE_DSCUR:
1439                 case ANIMTYPE_DSSKEY:
1440                 case ANIMTYPE_DSWOR:
1441                 case ANIMTYPE_DSPART:
1442                 case ANIMTYPE_DSMBALL:
1443                 case ANIMTYPE_DSARM:
1444                 {
1445                         /* sanity checking... */
1446                         if (ale->adt) {
1447                                 /* select/deselect */
1448                                 if (selectmode == SELECT_INVERT) {
1449                                         /* inverse selection status of this AnimData block only */
1450                                         ale->adt->flag ^= ADT_UI_SELECTED;
1451                                 }
1452                                 else {
1453                                         /* select AnimData block by itself */
1454                                         ANIM_deselect_anim_channels(ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
1455                                         ale->adt->flag |= ADT_UI_SELECTED;
1456                                 }
1457                                 
1458                                 /* set active? */
1459                                 if ((ale->adt) && (ale->adt->flag & ADT_UI_SELECTED))
1460                                         ale->adt->flag |= ADT_UI_ACTIVE;
1461                         }
1462                         
1463                         notifierFlags |= ND_ANIMCHAN_SELECT;
1464                 }       
1465                         break;
1466                 
1467                 case ANIMTYPE_GROUP: 
1468                 {
1469                         bActionGroup *agrp= (bActionGroup *)ale->data;
1470                         
1471                         /* select/deselect group */
1472                         if (selectmode == SELECT_INVERT) {
1473                                 /* inverse selection status of this group only */
1474                                 agrp->flag ^= AGRP_SELECTED;
1475                         }
1476                         else if (selectmode == -1) {
1477                                 /* select all in group (and deselect everthing else) */ 
1478                                 FCurve *fcu;
1479                                 
1480                                 /* deselect all other channels */
1481                                 ANIM_deselect_anim_channels(ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
1482                                 
1483                                 /* only select channels in group and group itself */
1484                                 for (fcu= agrp->channels.first; fcu && fcu->grp==agrp; fcu= fcu->next)
1485                                         fcu->flag |= FCURVE_SELECTED;
1486                                 agrp->flag |= AGRP_SELECTED;                                    
1487                         }
1488                         else {
1489                                 /* select group by itself */
1490                                 ANIM_deselect_anim_channels(ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
1491                                 agrp->flag |= AGRP_SELECTED;
1492                         }
1493                         
1494                         /* if group is selected now, make group the 'active' one in the visible list */
1495                         if (agrp->flag & AGRP_SELECTED)
1496                                 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, agrp, ANIMTYPE_GROUP);
1497                                 
1498                         notifierFlags |= ND_ANIMCHAN_SELECT;
1499                 }
1500                         break;
1501                 case ANIMTYPE_FCURVE: 
1502                 {
1503                         FCurve *fcu= (FCurve *)ale->data;
1504                         
1505                         /* select/deselect */
1506                         if (selectmode == SELECT_INVERT) {
1507                                 /* inverse selection status of this F-Curve only */
1508                                 fcu->flag ^= FCURVE_SELECTED;
1509                         }
1510                         else {
1511                                 /* select F-Curve by itself */
1512                                 ANIM_deselect_anim_channels(ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
1513                                 fcu->flag |= FCURVE_SELECTED;
1514                         }
1515                         
1516                         /* if F-Curve is selected now, make F-Curve the 'active' one in the visible list */
1517                         if (fcu->flag & FCURVE_SELECTED)
1518                                 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, fcu, ANIMTYPE_FCURVE);
1519                                 
1520                         notifierFlags |= ND_ANIMCHAN_SELECT;
1521                 }
1522                         break;
1523                 case ANIMTYPE_GPDATABLOCK:
1524                 {
1525                         bGPdata *gpd= (bGPdata *)ale->data;
1526                         
1527                         /* toggle expand */
1528                         gpd->flag ^= GP_DATA_EXPAND;
1529                         
1530                         notifierFlags |= ND_ANIMCHAN_EDIT;
1531                 }
1532                         break;
1533                 case ANIMTYPE_GPLAYER:
1534                 {
1535 #if 0 // XXX future of this is unclear
1536                         bGPdata *gpd= (bGPdata *)ale->owner;
1537                         bGPDlayer *gpl= (bGPDlayer *)ale->data;
1538                         
1539                         if (x >= (ACHANNEL_NAMEWIDTH-16)) {
1540                                 /* toggle lock */
1541                                 gpl->flag ^= GP_LAYER_LOCKED;
1542                         }
1543                         else if (x >= (ACHANNEL_NAMEWIDTH-32)) {
1544                                 /* toggle hide */
1545                                 gpl->flag ^= GP_LAYER_HIDE;
1546                         }
1547                         else {
1548                                 /* select/deselect */
1549                                 //if (G.qual & LR_SHIFTKEY) {
1550                                         //select_gplayer_channel(gpd, gpl, SELECT_INVERT);
1551                                 //}
1552                                 //else {
1553                                         //deselect_gpencil_layers(data, 0);
1554                                         //select_gplayer_channel(gpd, gpl, SELECT_INVERT);
1555                                 //}
1556                         }
1557 #endif // XXX future of this is unclear
1558                 }
1559                         break;
1560                 case ANIMTYPE_SHAPEKEY:
1561                         /* TODO: shapekey channels cannot be selected atm... */
1562                         break;
1563                 default:
1564                         printf("Error: Invalid channel type in mouse_anim_channels() \n");
1565         }
1566         
1567         /* free channels */
1568         BLI_freelistN(&anim_data);
1569         
1570         /* return notifier flags */
1571         return notifierFlags;
1572 }
1573
1574 /* ------------------- */
1575
1576 /* handle clicking */
1577 static int animchannels_mouseclick_invoke(bContext *C, wmOperator *op, wmEvent *event)
1578 {
1579         bAnimContext ac;
1580         Scene *scene;
1581         ARegion *ar;
1582         View2D *v2d;
1583         int mval[2], channel_index;
1584         int notifierFlags = 0;
1585         short selectmode;
1586         float x, y;
1587         
1588         
1589         /* get editor data */
1590         if (ANIM_animdata_get_context(C, &ac) == 0)
1591                 return OPERATOR_CANCELLED;
1592                 
1593         /* get useful pointers from animation context data */
1594         scene= ac.scene;
1595         ar= ac.ar;
1596         v2d= &ar->v2d;
1597         
1598         /* get mouse coordinates (in region coordinates) */
1599         mval[0]= (event->x - ar->winrct.xmin);
1600         mval[1]= (event->y - ar->winrct.ymin);
1601         
1602         /* select mode is either replace (deselect all, then add) or add/extend */
1603         if (RNA_boolean_get(op->ptr, "extend"))
1604                 selectmode= SELECT_INVERT;
1605         else if (RNA_boolean_get(op->ptr, "children_only"))
1606                 selectmode= -1; /* this is a bit of a special case for ActionGroups only... should it be removed or extended to all instead? */
1607         else
1608                 selectmode= SELECT_REPLACE;
1609         
1610         /* figure out which channel user clicked in 
1611          * Note: although channels technically start at y= ACHANNEL_FIRST, we need to adjust by half a channel's height
1612          *              so that the tops of channels get caught ok. Since ACHANNEL_FIRST is really ACHANNEL_HEIGHT, we simply use
1613          *              ACHANNEL_HEIGHT_HALF.
1614          */
1615         UI_view2d_region_to_view(v2d, mval[0], mval[1], &x, &y);
1616         UI_view2d_listview_view_to_cell(v2d, ACHANNEL_NAMEWIDTH, ACHANNEL_STEP, 0, (float)ACHANNEL_HEIGHT_HALF, x, y, NULL, &channel_index);
1617         
1618         /* handle mouse-click in the relevant channel then */
1619         notifierFlags= mouse_anim_channels(&ac, x, channel_index, selectmode);
1620         
1621         /* set notifier that things have changed */
1622         WM_event_add_notifier(C, NC_ANIMATION|notifierFlags, NULL);
1623         
1624         return OPERATOR_FINISHED;
1625 }
1626  
1627 void ANIM_OT_channels_click (wmOperatorType *ot)
1628 {
1629         /* identifiers */
1630         ot->name= "Mouse Click on Channels";
1631         ot->idname= "ANIM_OT_channels_click";
1632         ot->description= "Handle mouse-clicks over animation channels.";
1633         
1634         /* api callbacks */
1635         ot->invoke= animchannels_mouseclick_invoke;
1636         ot->poll= animedit_poll_channels_active;
1637         
1638         /* flags */
1639         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1640         
1641         /* id-props */
1642         RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", ""); // SHIFTKEY
1643         RNA_def_boolean(ot->srna, "children_only", 0, "Select Children Only", ""); // CTRLKEY|SHIFTKEY
1644 }
1645
1646 /* ************************************************************************** */
1647 /* Operator Registration */
1648
1649 void ED_operatortypes_animchannels(void)
1650 {
1651         WM_operatortype_append(ANIM_OT_channels_select_all_toggle);
1652         WM_operatortype_append(ANIM_OT_channels_select_border);
1653         WM_operatortype_append(ANIM_OT_channels_click);
1654         
1655         WM_operatortype_append(ANIM_OT_channels_setting_enable);
1656         WM_operatortype_append(ANIM_OT_channels_setting_disable);
1657         WM_operatortype_append(ANIM_OT_channels_setting_toggle);
1658         
1659         WM_operatortype_append(ANIM_OT_channels_delete);
1660         
1661                 // XXX does this need to be a separate operator?
1662         WM_operatortype_append(ANIM_OT_channels_editable_toggle);
1663         
1664                 // XXX these need to be updated for new system... todo...
1665         //WM_operatortype_append(ANIM_OT_channels_move_up);
1666         //WM_operatortype_append(ANIM_OT_channels_move_down);
1667         //WM_operatortype_append(ANIM_OT_channels_move_top);
1668         //WM_operatortype_append(ANIM_OT_channels_move_bottom);
1669         
1670         WM_operatortype_append(ANIM_OT_channels_expand);
1671         WM_operatortype_append(ANIM_OT_channels_collapse);
1672         
1673         WM_operatortype_append(ANIM_OT_channels_visibility_toggle);
1674 }
1675
1676 void ED_keymap_animchannels(wmKeyConfig *keyconf)
1677 {
1678         wmKeyMap *keymap = WM_keymap_find(keyconf, "Animation_Channels", 0, 0);
1679         
1680         /* selection */
1681                 /* click-select */
1682                 // XXX for now, only leftmouse.... 
1683         WM_keymap_add_item(keymap, "ANIM_OT_channels_click", LEFTMOUSE, KM_PRESS, 0, 0);
1684         RNA_boolean_set(WM_keymap_add_item(keymap, "ANIM_OT_channels_click", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "extend", 1);
1685         RNA_boolean_set(WM_keymap_add_item(keymap, "ANIM_OT_channels_click", LEFTMOUSE, KM_PRESS, KM_CTRL|KM_SHIFT, 0)->ptr, "children_only", 1);
1686         
1687                 /* deselect all */
1688         WM_keymap_add_item(keymap, "ANIM_OT_channels_select_all_toggle", AKEY, KM_PRESS, 0, 0);
1689         RNA_boolean_set(WM_keymap_add_item(keymap, "ANIM_OT_channels_select_all_toggle", IKEY, KM_PRESS, KM_CTRL, 0)->ptr, "invert", 1);
1690         
1691                 /* borderselect */
1692         WM_keymap_add_item(keymap, "ANIM_OT_channels_select_border", BKEY, KM_PRESS, 0, 0);
1693         
1694         /* delete */
1695         WM_keymap_add_item(keymap, "ANIM_OT_channels_delete", XKEY, KM_PRESS, 0, 0);
1696         WM_keymap_add_item(keymap, "ANIM_OT_channels_delete", DELKEY, KM_PRESS, 0, 0);
1697         
1698         /* settings */
1699         WM_keymap_add_item(keymap, "ANIM_OT_channels_setting_toggle", WKEY, KM_PRESS, KM_SHIFT, 0);
1700         WM_keymap_add_item(keymap, "ANIM_OT_channels_setting_enable", WKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
1701         WM_keymap_add_item(keymap, "ANIM_OT_channels_setting_disable", WKEY, KM_PRESS, KM_ALT, 0);
1702         
1703         /* settings - specialised hotkeys */
1704         WM_keymap_add_item(keymap, "ANIM_OT_channels_editable_toggle", TABKEY, KM_PRESS, 0, 0);
1705         
1706         /* expand/collapse */
1707         WM_keymap_add_item(keymap, "ANIM_OT_channels_expand", PADPLUSKEY, KM_PRESS, 0, 0);
1708         WM_keymap_add_item(keymap, "ANIM_OT_channels_collapse", PADMINUS, KM_PRESS, 0, 0);
1709         
1710         RNA_boolean_set(WM_keymap_add_item(keymap, "ANIM_OT_channels_expand", PADPLUSKEY, KM_PRESS, KM_CTRL, 0)->ptr, "all", 1);
1711         RNA_boolean_set(WM_keymap_add_item(keymap, "ANIM_OT_channels_collapse", PADMINUS, KM_PRESS, KM_CTRL, 0)->ptr, "all", 1);
1712         
1713         /* rearranging - actions only */
1714         //WM_keymap_add_item(keymap, "ANIM_OT_channels_move_up", PAGEUPKEY, KM_PRESS, KM_SHIFT, 0);
1715         //WM_keymap_add_item(keymap, "ANIM_OT_channels_move_down", PAGEDOWNKEY, KM_PRESS, KM_SHIFT, 0);
1716         //WM_keymap_add_item(keymap, "ANIM_OT_channels_move_to_top", PAGEUPKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
1717         //WM_keymap_add_item(keymap, "ANIM_OT_channels_move_to_bottom", PAGEDOWNKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
1718         
1719         /* Graph Editor only */
1720         WM_keymap_add_item(keymap, "ANIM_OT_channels_visibility_toggle", VKEY, KM_PRESS, 0, 0);
1721 }
1722
1723 /* ************************************************************************** */