== Action Editor ==
[blender-staging.git] / source / blender / src / editaction.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): 2007, Joshua Leung (major rewrite of Action Editor)
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <string.h>
34 #include <math.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "PIL_time.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_arithb.h"
42
43 #include "DNA_action_types.h"
44 #include "DNA_armature_types.h"
45 #include "DNA_curve_types.h"
46 #include "DNA_ipo_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_screen_types.h"
50 #include "DNA_space_types.h"
51 #include "DNA_userdef_types.h"
52 #include "DNA_constraint_types.h"
53 #include "DNA_key_types.h"
54 #include "DNA_mesh_types.h"
55 #include "DNA_nla_types.h"
56 #include "DNA_lattice_types.h"
57
58 #include "BKE_action.h"
59 #include "BKE_armature.h"
60 #include "BKE_constraint.h"
61 #include "BKE_curve.h"
62 #include "BKE_depsgraph.h"
63 #include "BKE_global.h"
64 #include "BKE_ipo.h"
65 #include "BKE_key.h"
66 #include "BKE_library.h"
67 #include "BKE_main.h"
68 #include "BKE_utildefines.h"
69 #include "BKE_object.h" /* for where_is_object in obanim -> action baking */
70
71 #include "BIF_butspace.h"
72 #include "BIF_editaction.h"
73 #include "BIF_editarmature.h"
74 #include "BIF_editnla.h"
75 #include "BIF_editview.h"
76 #include "BIF_gl.h"
77 #include "BIF_interface.h"
78 #include "BIF_mywindow.h"
79 #include "BIF_poseobject.h"
80 #include "BIF_screen.h"
81 #include "BIF_space.h"
82 #include "BIF_toolbox.h"
83
84 #include "BSE_edit.h"
85 #include "BSE_drawipo.h"
86 #include "BSE_headerbuttons.h"
87 #include "BSE_editaction_types.h"
88 #include "BSE_editipo.h"
89 #include "BSE_time.h"
90 #include "BSE_trans_types.h"
91
92 #include "BDR_drawaction.h"
93 #include "BDR_editobject.h"
94
95 #include "mydevice.h"
96 #include "blendef.h"
97 #include "nla.h"
98
99 /* **************************************************** */
100 /* ACTION API */
101
102 /* this function adds a new Action block */
103 bAction *add_empty_action (char *name)
104 {
105         bAction *act;
106         
107         act= alloc_libblock(&G.main->action, ID_AC, name);
108         act->id.flag |= LIB_FAKEUSER;
109         act->id.us++;
110         
111         return act;
112 }
113
114 /* generic get current action call, for action window context */
115 bAction *ob_get_action (Object *ob)
116 {
117         bActionStrip *strip;
118         
119         if(ob->action)
120                 return ob->action;
121         
122         for (strip=ob->nlastrips.first; strip; strip=strip->next) {
123                 if (strip->flag & ACTSTRIP_SELECT)
124                         return strip->act;
125         }
126         return NULL;
127 }
128
129 /* used by ipo, outliner, buttons to find the active channel */
130 bActionChannel *get_hilighted_action_channel (bAction *action)
131 {
132         bActionChannel *achan;
133
134         if (!action)
135                 return NULL;
136
137         for (achan= action->chanbase.first; achan; achan= achan->next) {
138                 if(VISIBLE_ACHAN(achan)) {
139                         if (SEL_ACHAN(achan) && (achan->flag & ACHAN_HILIGHTED))
140                                 return achan;
141                 }
142         }
143
144         return NULL;
145 }
146
147 /* ----------------------------------------- */
148
149 void remake_action_ipos (bAction *act)
150 {
151         bActionChannel *achan;
152         bConstraintChannel *conchan;
153         IpoCurve *icu;
154
155         for (achan= act->chanbase.first; achan; achan= achan->next) {
156                 if (achan->ipo) {
157                         for (icu = achan->ipo->curve.first; icu; icu=icu->next) {
158                                 sort_time_ipocurve(icu);
159                                 testhandles_ipocurve(icu);
160                         }
161                 }
162                 for (conchan=achan->constraintChannels.first; conchan; conchan=conchan->next){
163                         if (conchan->ipo) {
164                                 for (icu = conchan->ipo->curve.first; icu; icu=icu->next){
165                                         sort_time_ipocurve(icu);
166                                         testhandles_ipocurve(icu);
167                                 }
168                         }
169                 }
170         }
171         
172         synchronize_action_strips();
173 }
174
175 static void remake_meshaction_ipos (Ipo *ipo)
176 {
177         /* this puts the bezier triples in proper
178          * order and makes sure the bezier handles
179          * aren't too strange.
180          */
181         IpoCurve *icu;
182
183         for (icu = ipo->curve.first; icu; icu=icu->next) {
184                 sort_time_ipocurve(icu);
185                 testhandles_ipocurve(icu);
186         }
187 }
188
189 static void meshkey_do_redraw (Key *key)
190 {
191         if(key->ipo)
192                 remake_meshaction_ipos(key->ipo);
193
194         DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
195         
196         allspace(REMAKEIPO, 0);
197         allqueue(REDRAWACTION, 0);
198         allqueue(REDRAWIPO, 0);
199         allqueue(REDRAWNLA, 0);
200
201 }
202
203 /* **************************************************** */
204 /* FILTER->EDIT STRUCTURES */
205 /* 
206  * This method involves generating a list of edit structures which enable
207  * tools to naively perform the actions they require without all the boiler-plate
208  * associated with loops within loops and checking for cases to ignore. 
209  */
210
211 /* this function allocates memory for a new bActListElem struct for the 
212  * provided action channel-data. 
213  */
214 bActListElem *make_new_actlistelem (void *data, short datatype, void *owner, short ownertype)
215 {
216         bActListElem *ale= NULL;
217         
218         /* only allocate memory if there is data to convert */
219         if (data) {
220                 /* allocate and set generic data */
221                 ale= MEM_callocN(sizeof(bActListElem), "bActListElem");
222                 
223                 ale->data= data;
224                 ale->type= datatype;
225                 ale->owner= owner;
226                 ale->ownertype= ownertype;
227                 
228                 /* do specifics */
229                 switch (datatype) {
230                         case ACTTYPE_ACHAN:
231                         {
232                                 bActionChannel *achan= (bActionChannel *)data;
233                                 
234                                 ale->flag= achan->flag;
235                                 
236                                 if (achan->ipo) {
237                                         ale->key_data= achan->ipo;
238                                         ale->datatype= ALE_IPO;
239                                 }
240                                 else {
241                                         ale->key_data= NULL;
242                                         ale->datatype= ALE_NONE;
243                                 }
244                         }       
245                                 break;
246                         case ACTTYPE_CONCHAN:
247                         {
248                                 bConstraintChannel *conchan= (bConstraintChannel *)data;
249                                 
250                                 ale->flag= conchan->flag;
251                                 
252                                 if (conchan->ipo && conchan->ipo->curve.first) {
253                                         /* we assume that constraint ipo blocks only have 1 curve:
254                                          * INFLUENCE, so we pretend that a constraint channel is 
255                                          * really just a Ipo-Curve channel instead.
256                                          */
257                                         ale->key_data= conchan->ipo->curve.first;
258                                         ale->datatype= ALE_ICU;
259                                 }
260                                 else {
261                                         ale->key_data= NULL;
262                                         ale->datatype= ALE_NONE;
263                                 }
264                         }
265                                 break;
266                         case ACTTYPE_ICU:
267                         {
268                                 IpoCurve *icu= (IpoCurve *)data;
269                                 
270                                 ale->flag= icu->flag;
271                                 ale->key_data= icu;
272                                 ale->datatype= ALE_ICU;
273                         }
274                                 break;
275                                 
276                         case ACTTYPE_FILLIPO:
277                         case ACTTYPE_FILLCON:
278                         {
279                                 bActionChannel *achan= (bActionChannel *)data;
280                                 
281                                 if (datatype == ACTTYPE_FILLIPO)
282                                         ale->flag= FILTER_IPO_ACHAN(achan);
283                                 else
284                                         ale->flag= FILTER_CON_ACHAN(achan);
285                                         
286                                 ale->key_data= NULL;
287                                 ale->datatype= ALE_NONE;
288                         }
289                                 break;
290                         case ACTTYPE_IPO:
291                         {
292                                 ale->flag= 0;
293                                 ale->key_data= data;
294                                 ale->datatype= ALE_IPO;
295                         }
296                                 break;
297                 }
298         }
299         
300         /* return created datatype */
301         return ale;
302 }
303  
304 /* ----------------------------------------- */
305  
306 static void actdata_filter_action (ListBase *act_data, bAction *act, int filter_mode)
307 {
308         bActListElem *ale;
309         bActionChannel *achan;
310         bConstraintChannel *conchan;
311         IpoCurve *icu;
312         
313         /* loop over action channels, performing the necessary checks */
314         for (achan= act->chanbase.first; achan; achan= achan->next) {
315                 /* only work with this channel and its subchannels if it is visible */
316                 if (!(filter_mode & ACTFILTER_VISIBLE) || VISIBLE_ACHAN(achan)) {
317                         /* only work with this channel and its subchannels if it is editable */
318                         if (!(filter_mode & ACTFILTER_FOREDIT) || EDITABLE_ACHAN(achan)) {
319                                 /* check if this achan should only be included if it is selected */
320                                 if (!(filter_mode & ACTFILTER_SEL) || SEL_ACHAN(achan)) {
321                                         /* are we only interested in the ipo-curves? */
322                                         if ((filter_mode & ACTFILTER_ONLYICU)==0) {
323                                                 ale= make_new_actlistelem(achan, ACTTYPE_ACHAN, achan, ACTTYPE_ACHAN);
324                                                 if (ale) BLI_addtail(act_data, ale);
325                                         }
326                                 }
327                                 
328                                 /* check if expanded - if not, continue on to next action channel */
329                                 if (EXPANDED_ACHAN(achan) == 0 && (filter_mode & ACTFILTER_ONLYICU)==0) 
330                                         continue;
331                                         
332                                 /* ipo channels */
333                                 if (achan->ipo) {
334                                         /* include ipo-expand widget? */
335                                         if ((filter_mode & ACTFILTER_CHANNELS) && (filter_mode & ACTFILTER_ONLYICU)==0) {
336                                                 ale= make_new_actlistelem(achan, ACTTYPE_FILLIPO, achan, ACTTYPE_ACHAN);
337                                                 if (ale) BLI_addtail(act_data, ale);
338                                         }
339                                         
340                                         /* add ipo-curve channels? */
341                                         if (FILTER_IPO_ACHAN(achan) || (filter_mode & ACTFILTER_ONLYICU)) {
342                                                 /* loop through ipo-curve channels, adding them */
343                                                 for (icu= achan->ipo->curve.first; icu; icu=icu->next) {
344                                                         ale= make_new_actlistelem(icu, ACTTYPE_ICU, achan, ACTTYPE_ACHAN);
345                                                         if (ale) BLI_addtail(act_data, ale); 
346                                                 }
347                                         }
348                                 }
349                                 
350                                 /* constraint channels */
351                                 if (achan->constraintChannels.first) {
352                                         /* include constraint-expand widget? */
353                                         if ((filter_mode & ACTFILTER_CHANNELS) && (filter_mode & ACTFILTER_ONLYICU)==0) {
354                                                 ale= make_new_actlistelem(achan, ACTTYPE_FILLCON, achan, ACTTYPE_ACHAN);
355                                                 if (ale) BLI_addtail(act_data, ale);
356                                         }
357                                         
358                                         /* add constaint channels? */
359                                         if (FILTER_CON_ACHAN(achan)) {
360                                                 /* loop through constraint channels, checking and adding them */
361                                                 for (conchan=achan->constraintChannels.first; conchan; conchan=conchan->next) {
362                                                         /* only work with this channel and its subchannels if it is editable */
363                                                         if (!(filter_mode & ACTFILTER_FOREDIT) || EDITABLE_CONCHAN(conchan)) {
364                                                                 /* check if this conchan should only be included if it is selected */
365                                                                 if (!(filter_mode & ACTFILTER_SEL) || SEL_CONCHAN(conchan)) {
366                                                                         if ((filter_mode & ACTFILTER_ONLYICU)==0) {
367                                                                                 ale= make_new_actlistelem(conchan, ACTTYPE_CONCHAN, achan, ACTTYPE_ACHAN);
368                                                                                 if (ale) BLI_addtail(act_data, ale); 
369                                                                         }
370                                                                 }
371                                                         }
372                                                 }
373                                         }
374                                 }
375                         }               
376                 }
377         }
378 }
379
380 static void actdata_filter_shapekey (ListBase *act_data, Key *key, int filter_mode)
381 {
382         bActListElem *ale;
383         IpoCurve *icu;
384         
385         /* loop over ipo curves if present */
386         if (key->ipo) {
387                 if (filter_mode & ACTFILTER_IPOKEYS) {
388                         ale= make_new_actlistelem(key->ipo, ACTTYPE_IPO, key, ACTTYPE_SHAPEKEY);
389                         if (ale) BLI_addtail(act_data, ale);
390                 }
391                 else {
392                         for (icu= key->ipo->curve.first; icu; icu=icu->next) {
393                                 ale= make_new_actlistelem(icu, ACTTYPE_ICU, key, ACTTYPE_SHAPEKEY);
394                                 if (ale) BLI_addtail(act_data, ale);
395                         }
396                 }
397         }
398 }
399  
400 /* This function filters the active data source to leave only the desired
401  * data types. 'Public' api call.
402  *      *act_data: is a pointer to a ListBase, to which the filtered action data 
403  *              will be placed for use.
404  *      filter_mode: how should the data be filtered - bitmapping accessed flags
405  */
406 void actdata_filter (ListBase *act_data, int filter_mode, void *data, short datatype)
407 {
408         /* only filter data if there's somewhere to put it */
409         if (data && act_data) {
410                 bActListElem *ale, *next;
411                 
412                 /* firstly filter the data */
413                 switch (datatype) {
414                         case ACTCONT_ACTION:
415                                 actdata_filter_action(act_data, data, filter_mode);
416                                 break;
417                         case ACTCONT_SHAPEKEY:
418                                 actdata_filter_shapekey(act_data, data, filter_mode);
419                                 break;
420                 }
421                         
422                 /* remove any weedy entries */
423                 for (ale= act_data->first; ale; ale= next) {
424                         next= ale->next;
425                         
426                         if (ale->type == ACTTYPE_NONE)
427                                 BLI_freelinkN(act_data, ale);
428                         
429                         if (filter_mode & ACTFILTER_IPOKEYS) {
430                                 if (ale->datatype != ALE_IPO)
431                                         BLI_freelinkN(act_data, ale);
432                                 else if (ale->key_data == NULL)
433                                         BLI_freelinkN(act_data, ale);
434                         }
435                 }
436         }
437 }
438
439 /* **************************************************** */
440 /* GENERAL ACTION TOOLS */
441
442
443 /* gets the key data from the currently selected
444  * mesh/lattice. If a mesh is not selected, or does not have
445  * key data, then we return NULL (currently only
446  * returns key data for RVK type meshes). If there
447  * is an action that is pinned, return null
448  */
449 Key *get_action_mesh_key(void) 
450 {
451     Object *ob;
452     Key    *key;
453
454     ob = OBACT;
455     if (!ob) return NULL;
456
457         if (G.saction->pin) return NULL;
458
459     if (ob->type==OB_MESH ) {
460                 key = ((Mesh *)ob->data)->key;
461     }
462         else if (ob->type==OB_LATTICE ) {
463                 key = ((Lattice *)ob->data)->key;
464         }
465         else return NULL;
466
467         if (key) {
468                 if (key->type == KEY_RELATIVE)
469                         return key;
470         }
471
472     return NULL;
473 }
474
475 /* TODO: kill this! */
476 int get_nearest_key_num (Key *key, short *mval, float *x) 
477 {
478         /* returns the key num that cooresponds to the
479          * y value of the mouse click. Does not check
480          * if this is a valid keynum. Also gives the Ipo
481          * x coordinate.
482          */
483     int num;
484     float y;
485
486     areamouseco_to_ipoco(G.v2d, mval, x, &y);
487     num = (int) ((CHANNELHEIGHT/2 - y) / (CHANNELHEIGHT+CHANNELSKIP));
488
489     return (num + 1);
490 }
491
492 /* this function is used to get a pointer to an action or shapekey 
493  * datablock, thus simplying that process.
494  */
495 /* this function is intended for use */
496 void *get_nearest_act_channel (short mval[], short *ret_type)
497 {
498         ListBase act_data = {NULL, NULL};
499         bActListElem *ale;
500         void *data;
501         short datatype;
502         int filter;
503         
504         int clickmin, clickmax;
505         float x,y;
506         
507         /* determine what type of data we are operating on */
508         data = get_action_context(&datatype);
509         if (data == NULL) {
510                 *ret_type= ACTTYPE_NONE;
511                 return NULL;
512         }
513         
514     areamouseco_to_ipoco(G.v2d, mval, &x, &y);
515         clickmin = (int) (((CHANNELHEIGHT/2) - y) / (CHANNELHEIGHT+CHANNELSKIP));
516         clickmax = clickmin;
517         
518         if (clickmax < 0) {
519                 *ret_type= ACTTYPE_NONE;
520                 return NULL;
521         }
522         
523         /* filter data */
524         filter= (ACTFILTER_VISIBLE | ACTFILTER_CHANNELS);
525         actdata_filter(&act_data, filter, data, datatype);
526         
527         for (ale= act_data.first; ale; ale= ale->next) {
528                 if (clickmax < 0) 
529                         break;
530                 if (clickmin <= 0) {
531                         /* found match */
532                         *ret_type= ale->type;
533                         data= ale->data;
534                         
535                         BLI_freelistN(&act_data);
536                         
537                         return data;
538                 }
539                 --clickmin;
540                 --clickmax;
541         }
542         
543         /* cleanup */
544         BLI_freelistN(&act_data);
545         
546         *ret_type= ACTTYPE_NONE;
547         return NULL;
548 }
549
550 /* helper for get_nearest_[action,mesh]channel_key */
551 static IpoCurve *get_nearest_icu_key (IpoCurve *curve, float *selx, short *sel, float xrange[])
552 {
553         /* try to find first beztriple in bounds that is selected */
554         IpoCurve *icu, *firsticu=NULL;
555     int      foundsel=0;
556     float    firstvert=-1, foundx=-1;
557         int      i;
558         
559         if (curve == NULL)
560                 return NULL;
561         
562     /* lets loop through the IpoCurves trying to find the closest bezier */
563         for (icu= curve; icu ; icu= icu->next) {
564                 /* loop through the beziers in the curve */
565                 for (i=0; i<icu->totvert; i++) {
566                         /* Is this bezier in the right area? */
567                         if (icu->bezt[i].vec[1][0] > xrange[0] && 
568                                 icu->bezt[i].vec[1][0] <= xrange[1] ) {
569
570                                 /* if no other curves have been picked ... */
571                                 if (firsticu==NULL) {
572                                         /* mark this curve/bezier as the first selected */
573                                         firsticu= icu;
574                                         firstvert= icu->bezt[i].vec[1][0];
575
576                                         /* sel = (is the bezier is already selected) ? 1 : 0; */
577                                         *sel = (icu->bezt[i].f2 & 1);   
578                                 }
579
580                                 /* if the bezier is selected ... */
581                                 if (icu->bezt[i].f2 & 1) { 
582                                         /* if we haven't found a selected one yet ... */
583                                         if (!foundsel) {
584                                                 /* record the found x value */
585                                                 foundsel=1;
586                                                 foundx = icu->bezt[i].vec[1][0];
587                                                 
588                                         }
589                                 }
590
591                                 /* if the bezier is unselected and not at the x
592                                  * position of a previous found selected bezier ...
593                                  */
594                                 else if (foundsel && icu->bezt[i].vec[1][0] != foundx){
595                                         /* lets return this found curve/bezier */
596                                         *sel = 0;
597                                         *selx= icu->bezt[i].vec[1][0];
598                                         return icu;
599                                 }
600                         }
601                 }
602         }
603         
604     /* return what we've found */
605     *selx=firstvert;
606     return firsticu;
607 }
608
609 /* used only by mouse_action. It is used to find the location of the nearest 
610  * keyframe to where the mouse clicked, 
611  */
612 static void *get_nearest_action_key (float *selx, short *sel, short *ret_type, bActionChannel **par)
613 {
614         ListBase act_data = {NULL, NULL};
615         bActListElem *ale;
616         void *data;
617         short datatype;
618         int filter;
619         
620         IpoCurve *icu;
621         rctf rectf;
622         float xmin, xmax, x, y;
623         float xrange[2];
624         int clickmin, clickmax;
625         short mval[2];
626                 
627         getmouseco_areawin (mval);
628
629         /* action-channel */
630         *par= NULL;
631         
632         /* determine what type of data we are operating on */
633         data = get_action_context(&datatype);
634         if (data == NULL) {
635                 *ret_type= ACTTYPE_NONE;
636                 return NULL;
637         }
638
639     areamouseco_to_ipoco(G.v2d, mval, &x, &y);
640     clickmin = (int) (((CHANNELHEIGHT/2) - y) / (CHANNELHEIGHT+CHANNELSKIP));
641         clickmax = clickmin;
642         
643         mval[0]-=7;
644         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
645         mval[0]+=14;
646         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
647
648         /* if action is mapped in NLA, it returns a correction */
649         if (G.saction->pin==0 && OBACT && datatype==ACTCONT_ACTION) {
650                 xmin= get_action_frame(OBACT, rectf.xmin);
651                 xmax= get_action_frame(OBACT, rectf.xmax);
652         }
653         else {
654                 xmin= rectf.xmin;
655                 xmax= rectf.xmax;
656         }
657         
658         if (clickmax < 0) {
659                 *ret_type= ACTTYPE_NONE;
660                 return NULL;
661         }
662         
663         xrange[0]= xmin;
664         xrange[1]= xmax;
665         
666         /* filter data */
667         filter= (ACTFILTER_VISIBLE | ACTFILTER_CHANNELS);
668         actdata_filter(&act_data, filter, data, datatype);
669         
670         for (ale= act_data.first; ale; ale= ale->next) {
671                 if (clickmax < 0) 
672                         break;
673                 if (clickmin <= 0) {
674                         /* found match */
675                         *ret_type= ale->type;
676                         
677                         /* find location of keyframe (if applicable) */
678                         if (ale->key_data) {
679                                 switch (ale->datatype) {
680                                         case ALE_IPO:
681                                         {
682                                                 Ipo *ipo= (Ipo *)ale->key_data;
683                                                 icu= get_nearest_icu_key(ipo->curve.first, selx, sel, xrange);
684                                         }
685                                                 break;
686                                         case ALE_ICU:
687                                         {
688                                                 icu= (IpoCurve *)ale->key_data;
689                                                 icu= get_nearest_icu_key(icu, selx, sel, xrange);
690                                         }
691                                                 break;
692                                 }
693                         }
694                         
695                         /* figure out what to return */
696                         if (datatype == ACTCONT_ACTION)
697                                 *par= ale->owner; /* assume that this is an action channel */
698                         data = ale->data;
699                         
700                         BLI_freelistN(&act_data);
701                         
702                         return data;
703                 }
704                 --clickmin;
705                 --clickmax;
706         }
707         
708         /* cleanup */
709         BLI_freelistN(&act_data);
710         
711         *ret_type= ACTTYPE_NONE;
712         return NULL;
713 }
714
715 void *get_action_context (short *datatype)
716 {
717         bAction *act;
718         Key *key;
719         
720         /* get pointers to active action/shapekey blocks */
721         act = (G.saction)? G.saction->action: NULL;
722         key = get_action_mesh_key();
723         
724         if (act) {
725                 *datatype= ACTCONT_ACTION;
726                 return act;
727         }
728         else if (key) {
729                 *datatype= ACTCONT_SHAPEKEY;
730                 return key;
731         }
732         else {
733                 *datatype= ACTCONT_NONE;
734                 return NULL;
735         }
736 }
737
738 /* **************************************************** */
739 /* TRANSFORM TOOLS */
740
741 /* initialise the transform data - create transverts */
742 static TransVert *transform_action_init (int *tvtot, float *minx, float *maxx)
743 {
744         ListBase act_data = {NULL, NULL};
745         bActListElem *ale;
746         TransVert *tv;
747         void *data;
748         short datatype;
749         int filter;
750         int count= 0;
751         float min= 0, max= 0;
752         int i;
753         
754         /* initialise the values being passed by reference */
755         *tvtot = *minx = *maxx = 0;
756         
757         /* determine what type of data we are operating on */
758         data = get_action_context(&datatype);
759         if (data == NULL) return NULL;
760         
761         /* filter data */
762         filter= (ACTFILTER_VISIBLE | ACTFILTER_FOREDIT | ACTFILTER_IPOKEYS);
763         actdata_filter(&act_data, filter, data, datatype);
764         
765         /* loop 1: fully select ipo-keys and count how many BezTriples are selected */
766         for (ale= act_data.first; ale; ale= ale->next)
767                 count += fullselect_ipo_keys(ale->key_data);
768         
769         /* stop if trying to build list if nothing selected */
770         if (count == 0) {
771                 /* cleanup temp list */
772                 BLI_freelistN(&act_data);
773                 return NULL;
774         }
775                 
776         /* Build the transvert structure */
777         tv = MEM_callocN (sizeof(TransVert) * count, "transVert");
778         
779         /* loop 2: build transvert structure */
780         for (ale= act_data.first; ale; ale= ale->next)
781                 *tvtot = add_trans_ipo_keys(ale->key_data, tv, *tvtot);
782                 
783         /* min max, only every other three */
784         min= max= tv[1].loc[0];
785         for (i=1; i<count; i+=3){
786                 if(min>tv[i].loc[0]) min= tv[i].loc[0];
787                 if(max<tv[i].loc[0]) max= tv[i].loc[0];
788         }
789         *minx= min;
790         *maxx= max;
791         
792         /* cleanup temp list */
793         BLI_freelistN(&act_data);
794         
795         /* return created transverts */
796         return tv;
797
798
799 /* main transform loop for action editor */
800 static short transform_action_loop (TransVert *tv, int tvtot, char mode, short context, float minx, float maxx)
801 {
802         Object *ob= OBACT;
803         float deltax, startx;
804         float cenf[2];
805         float sval[2], cval[2], lastcval[2]={0,0};
806         float fac=0.0f;
807         int     loop=1, invert=0;
808         int     i;
809         short cancel=0, firsttime=1;
810         short mvals[2], mvalc[2], cent[2];
811         char str[256];
812         
813         /* Do the event loop */
814         cent[0] = curarea->winx + (G.saction->v2d.hor.xmax)/2;
815         cent[1] = curarea->winy + (G.saction->v2d.hor.ymax)/2;
816         areamouseco_to_ipoco(G.v2d, cent, &cenf[0], &cenf[1]);
817
818         getmouseco_areawin (mvals);
819         areamouseco_to_ipoco(G.v2d, mvals, &sval[0], &sval[1]);
820
821         if(G.saction->pin==0 && OBACT)
822                 sval[0]= get_action_frame(OBACT, sval[0]);
823         
824         /* used for drawing */
825         if(mode=='t') {
826                 G.saction->flag |= SACTION_MOVING;
827                 G.saction->timeslide= sval[0];
828         }
829         
830         startx=sval[0];
831         while (loop) {
832                 if(mode=='t' && minx==maxx)
833                         break;
834                 
835                 /*              Get the input */
836                 /*              If we're cancelling, reset transformations */
837                 /*                      Else calc new transformation */
838                 /*              Perform the transformations */
839                 while (qtest()) {
840                         short val;
841                         unsigned short event= extern_qread(&val);
842
843                         if (val) {
844                                 switch (event) {
845                                 case LEFTMOUSE:
846                                 case SPACEKEY:
847                                 case RETKEY:
848                                         loop=0;
849                                         break;
850                                 case XKEY:
851                                         break;
852                                 case ESCKEY:
853                                 case RIGHTMOUSE:
854                                         cancel=1;
855                                         loop=0;
856                                         break;
857                                 default:
858                                         arrows_move_cursor(event);
859                                         break;
860                                 };
861                         }
862                 }
863
864                 if (cancel) {
865                         for (i=0; i<tvtot; i++) {
866                                 tv[i].loc[0]=tv[i].oldloc[0];
867                                 tv[i].loc[1]=tv[i].oldloc[1];
868                         }
869                 } 
870                 else {
871                         getmouseco_areawin (mvalc);
872                         areamouseco_to_ipoco(G.v2d, mvalc, &cval[0], &cval[1]);
873                         
874                         if(G.saction->pin==0 && OBACT)
875                                 cval[0]= get_action_frame(OBACT, cval[0]);
876
877                         if(mode=='t')
878                                 G.saction->timeslide= cval[0];
879                         
880                         if (!firsttime && lastcval[0]==cval[0] && lastcval[1]==cval[1]) {
881                                 PIL_sleep_ms(1);
882                         } 
883                         else {
884                                 short autosnap= 0;
885                                 
886                                 /* determine mode of keyframe snapping/autosnap */
887                                 if (mode != 't') {
888                                         switch (G.saction->autosnap) {
889                                         case SACTSNAP_OFF:
890                                                 if (G.qual == LR_CTRLKEY) 
891                                                         autosnap= SACTSNAP_STEP;
892                                                 else if (G.qual == LR_SHIFTKEY)
893                                                         autosnap= SACTSNAP_FRAME;
894                                                 else
895                                                         autosnap= SACTSNAP_OFF;
896                                                 break;
897                                         case SACTSNAP_STEP:
898                                                 autosnap= (G.qual==LR_CTRLKEY)? SACTSNAP_OFF: SACTSNAP_STEP;
899                                                 break;
900                                         case SACTSNAP_FRAME:
901                                                 autosnap= (G.qual==LR_SHIFTKEY)? SACTSNAP_OFF: SACTSNAP_FRAME;
902                                                 break;
903                                         }
904                                 }
905                                 
906                                 for (i=0; i<tvtot; i++) {
907                                         tv[i].loc[0]=tv[i].oldloc[0];
908
909                                         switch (mode) {
910                                         case 't':
911                                                 if( sval[0] > minx && sval[0] < maxx) {
912                                                         float timefac, cvalc= CLAMPIS(cval[0], minx, maxx);
913                                                         
914                                                         /* left half */
915                                                         if(tv[i].oldloc[0] < sval[0]) {
916                                                                 timefac= ( sval[0] - tv[i].oldloc[0])/(sval[0] - minx);
917                                                                 tv[i].loc[0]= cvalc - timefac*( cvalc - minx);
918                                                         }
919                                                         else {
920                                                                 timefac= (tv[i].oldloc[0] - sval[0])/(maxx - sval[0]);
921                                                                 tv[i].loc[0]= cvalc + timefac*(maxx- cvalc);
922                                                         }
923                                                 }
924                                                 break;
925                                         case 'g':
926                                                 if (G.saction->pin==0 && OBACT && context==ACTCONT_ACTION) {
927                                                         deltax = get_action_frame_inv(OBACT, cval[0]);
928                                                         deltax -= get_action_frame_inv(OBACT, sval[0]);
929                                                         
930                                                         if (autosnap == SACTSNAP_STEP) 
931                                                                 deltax= 1.0f*floor(deltax/1.0f + 0.5f);
932                                                         
933                                                         fac = get_action_frame_inv(OBACT, tv[i].loc[0]);
934                                                         fac += deltax;
935                                                         tv[i].loc[0] = get_action_frame(OBACT, fac);
936                                                 }
937                                                 else {
938                                                         deltax = cval[0] - sval[0];
939                                                         fac= deltax;
940                                                         
941                                                         if (autosnap == SACTSNAP_STEP)
942                                                                 fac= 1.0f*floor(fac/1.0f + 0.5f);
943                                                         
944                                                         tv[i].loc[0]+=fac;
945                                                 }
946                                                 break;
947                                         case 's':
948                                                 startx=mvals[0]-(ACTWIDTH/2+(curarea->winrct.xmax-curarea->winrct.xmin)/2);
949                                                 deltax=mvalc[0]-(ACTWIDTH/2+(curarea->winrct.xmax-curarea->winrct.xmin)/2);
950                                                 fac= fabs(deltax/startx);
951                                                 
952                                                 if (autosnap == SACTSNAP_STEP) {
953                                                         fac= 1.0f*floor(fac/1.0f + 0.5f);
954                                                 }
955                                                 
956                                                 if (invert){
957                                                         if (i % 03 == 0){
958                                                                 memcpy (tv[i].loc, tv[i].oldloc, sizeof(tv[i+2].oldloc));
959                                                         }
960                                                         if (i % 03 == 2){
961                                                                 memcpy (tv[i].loc, tv[i].oldloc, sizeof(tv[i-2].oldloc));
962                                                         }
963         
964                                                         fac*=-1;
965                                                 }
966                                                 startx= (G.scene->r.cfra);
967                                                 if(G.saction->pin==0 && OBACT && context==ACTCONT_ACTION)
968                                                         startx= get_action_frame(OBACT, startx);
969                                                         
970                                                 tv[i].loc[0]-= startx;
971                                                 tv[i].loc[0]*=fac;
972                                                 tv[i].loc[0]+= startx;
973                 
974                                                 break;
975                                         }
976                                         
977                                         /* snap key to nearest frame? */
978                                         if (autosnap == SACTSNAP_FRAME) {
979                                                 float snapval;
980                                                 
981                                                 /* convert frame to nla-action time (if needed) */
982                                                 if (G.saction->pin==0 && OBACT && context==ACTCONT_ACTION) 
983                                                         snapval= get_action_frame_inv(OBACT, tv[i].loc[0]);
984                                                 else
985                                                         snapval= tv[i].loc[0];
986                                                 
987                                                 /* snap to nearest frame */
988                                                 snapval= (float)(floor(snapval+0.5));
989                                                         
990                                                 /* convert frame out of nla-action time */
991                                                 if (G.saction->pin==0 && OBACT && context==ACTCONT_ACTION)
992                                                         tv[i].loc[0]= get_action_frame(OBACT, snapval);
993                                                 else
994                                                         tv[i].loc[0]= snapval;
995                                         }
996                                 }
997         
998                                 if (mode=='s') {
999                                         sprintf(str, "scaleX: %.3f", fac);
1000                                         headerprint(str);
1001                                 }
1002                                 else if (mode=='g') {
1003                                         if(G.saction->pin==0 && OBACT && context==ACTCONT_ACTION) {
1004                                                 /* recalculate the delta based on 'visual' times */
1005                                                 fac = get_action_frame_inv(OBACT, cval[0]);
1006                                                 fac -= get_action_frame_inv(OBACT, sval[0]);
1007                                                 
1008                                                 if (autosnap == SACTSNAP_STEP) 
1009                                                         fac= 1.0f*floor(fac/1.0f + 0.5f);
1010                                         }
1011                                         sprintf(str, "deltaX: %.3f", fac);
1012                                         headerprint(str);
1013                                 }
1014                                 else if (mode=='t') {
1015                                         float fac= 2.0*(cval[0]-sval[0])/(maxx-minx);
1016                                         CLAMP(fac, -1.0f, 1.0f);
1017                                         sprintf(str, "TimeSlide: %.3f", fac);
1018                                         headerprint(str);
1019                                 }
1020                 
1021                                 if (G.saction->lock) {
1022                                         if (context == ACTCONT_ACTION) {
1023                                                 if(ob) {
1024                                                         ob->ctime= -1234567.0f;
1025                                                         if(ob->pose || ob_get_key(ob))
1026                                                                 DAG_object_flush_update(G.scene, ob, OB_RECALC);
1027                                                         else
1028                                                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
1029                                                 }
1030                                                 force_draw_plus(SPACE_VIEW3D, 0);
1031                                         }
1032                                         else if (context == ACTCONT_SHAPEKEY) {
1033                                                 DAG_object_flush_update(G.scene, OBACT, OB_RECALC_OB|OB_RECALC_DATA);
1034                                 allqueue (REDRAWVIEW3D, 0);
1035                                 allqueue (REDRAWACTION, 0);
1036                                 allqueue (REDRAWIPO, 0);
1037                                 allqueue(REDRAWNLA, 0);
1038                                                 allqueue(REDRAWTIME, 0);
1039                                 force_draw_all(0);
1040                                         }
1041                                 }
1042                                 else {
1043                                         force_draw(0);
1044                                 }
1045                         }
1046                 }
1047                 
1048                 lastcval[0]= cval[0];
1049                 lastcval[1]= cval[1];
1050                 firsttime= 0;
1051         }
1052         
1053         return cancel;
1054 }
1055
1056 /* main call to start transforming keyframes */
1057 /*      NOTE: someday, this should be integrated with the transform system
1058  *              instead of relying on our own methods
1059  */
1060 void transform_action_keys (int mode, int dummy)
1061 {
1062         Object *ob= OBACT;
1063         TransVert *tv;
1064         int tvtot= 0;
1065         short cancel;
1066         float minx, maxx;
1067         
1068         void *data;
1069         short datatype;
1070         
1071         /* determine what type of data we are operating on */
1072         data = get_action_context(&datatype);
1073         if (data == NULL) return;
1074         
1075         /* initialise transform */
1076         tv= transform_action_init(&tvtot, &minx, &maxx);
1077         if (tv == NULL) return;
1078         
1079         /* do transform loop */
1080         cancel= transform_action_loop(tv, tvtot, mode, datatype, minx, maxx); 
1081         
1082         /* cleanup */
1083         if (datatype == ACTCONT_ACTION) {
1084                 /* Update the curve */
1085                 /* Depending on the lock status, draw necessary views */
1086                 if(ob) {
1087                         ob->ctime= -1234567.0f;
1088
1089                         if(ob->pose || ob_get_key(ob))
1090                                 DAG_object_flush_update(G.scene, ob, OB_RECALC);
1091                         else
1092                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
1093                 }
1094                 
1095                 remake_action_ipos((bAction *)data);
1096
1097                 G.saction->flag &= ~SACTION_MOVING;
1098                 
1099                 if (cancel==0) BIF_undo_push("Transform Action"); // does it have to be here?
1100                 allqueue(REDRAWVIEW3D, 0);
1101                 allqueue(REDRAWACTION, 0);
1102                 allqueue(REDRAWNLA, 0);
1103                 allqueue(REDRAWIPO, 0);
1104                 allqueue(REDRAWTIME, 0);
1105         }
1106         else if (datatype == ACTCONT_SHAPEKEY) {
1107                 /* fix up the Ipocurves and redraw stuff */
1108             meshkey_do_redraw((Key *)data);
1109                 if (cancel==0) BIF_undo_push("Transform Action");
1110         }
1111         
1112         MEM_freeN(tv);
1113 }
1114
1115 /* ----------------------------------------- */
1116
1117 /* duplicate keyframes */
1118 void duplicate_action_keys (void)
1119 {
1120         ListBase act_data = {NULL, NULL};
1121         bActListElem *ale;
1122         void *data;
1123         short datatype;
1124         int filter;
1125         
1126         /* determine what type of data we are operating on */
1127         data = get_action_context(&datatype);
1128         if (data == NULL) return;
1129         
1130         /* filter data */
1131         filter= (ACTFILTER_VISIBLE | ACTFILTER_FOREDIT | ACTFILTER_IPOKEYS);
1132         actdata_filter(&act_data, filter, data, datatype);
1133         
1134         /* loop through filtered data and duplicate selected keys */
1135         for (ale= act_data.first; ale; ale= ale->next) {
1136                 duplicate_ipo_keys((Ipo *)ale->key_data);
1137         }
1138         
1139         /* free filtered list */
1140         BLI_freelistN(&act_data);
1141         
1142         /* now, go into transform-grab mode, to move keys */
1143         transform_action_keys('g', 0);
1144 }
1145
1146 /* this function is responsible for snapping keyframes to frame-times */
1147 void snap_action_keys(short mode) 
1148 {
1149         ListBase act_data = {NULL, NULL};
1150         bActListElem *ale;
1151         int filter;
1152         void *data;
1153         short datatype;
1154         char str[32];
1155                 
1156         /* get data */
1157         data= get_action_context(&datatype);
1158         if (data == NULL) return;
1159         
1160         /* determine mode */
1161         switch (mode) {
1162                 case 1:
1163                         strcpy(str, "Snap Keys To Nearest Frame");
1164                         break;
1165                 case 2:
1166                         strcpy(str, "Snap Keys To Current Frame");
1167                         break;
1168                 case 3:
1169                         strcpy(str, "Snap Keys To Nearest Marker");
1170                         break;
1171                 default:
1172                         return;
1173         }
1174         
1175         /* filter data */
1176         filter= (ACTFILTER_VISIBLE | ACTFILTER_FOREDIT | ACTFILTER_IPOKEYS);
1177         actdata_filter(&act_data, filter, data, datatype);
1178         
1179         /* snap to frame */
1180         for (ale= act_data.first; ale; ale= ale->next) {
1181                 if (datatype==ACTCONT_ACTION && G.saction->pin==0 && OBACT) {
1182                         actstrip_map_ipo_keys(OBACT, ale->key_data, 0, 1); 
1183                         snap_ipo_keys(ale->key_data, mode);
1184                         actstrip_map_ipo_keys(OBACT, ale->key_data, 1, 1);
1185                 }
1186                 else 
1187                         snap_ipo_keys(ale->key_data, mode);
1188         }
1189         BLI_freelistN(&act_data);
1190         
1191         if (datatype == ACTCONT_ACTION)
1192                 remake_action_ipos(data);
1193         
1194         BIF_undo_push(str);
1195         allspace(REMAKEIPO, 0);
1196         allqueue(REDRAWACTION, 0);
1197         allqueue(REDRAWIPO, 0);
1198         allqueue(REDRAWNLA, 0);
1199 }
1200
1201 /* this function is responsible for snapping keyframes to frame-times */
1202 void mirror_action_keys(short mode) 
1203 {
1204         ListBase act_data = {NULL, NULL};
1205         bActListElem *ale;
1206         int filter;
1207         void *data;
1208         short datatype;
1209         char str[32];
1210                 
1211         /* get data */
1212         data= get_action_context(&datatype);
1213         if (data == NULL) return;
1214         
1215         /* determine mode */
1216         switch (mode) {
1217                 case 1:
1218                         strcpy(str, "Mirror Keys Over Current Frame");
1219                         break;
1220                 case 2:
1221                         strcpy(str, "Mirror Keys Over Y-Axis");
1222                         break;
1223                 case 3:
1224                         strcpy(str, "Mirror Keys Over X-Axis");
1225                         break;
1226                 case 4:
1227                         strcpy(str, "Mirror Keys Over Marker");
1228                         break;
1229                 default:
1230                         return;
1231         }
1232         
1233         /* filter data */
1234         filter= (ACTFILTER_VISIBLE | ACTFILTER_FOREDIT | ACTFILTER_IPOKEYS);
1235         actdata_filter(&act_data, filter, data, datatype);
1236         
1237         /* mirror */
1238         for (ale= act_data.first; ale; ale= ale->next) {
1239                 if (datatype==ACTCONT_ACTION && G.saction->pin==0 && OBACT) {
1240                         actstrip_map_ipo_keys(OBACT, ale->key_data, 0, 1); 
1241                         mirror_ipo_keys(ale->key_data, mode);
1242                         actstrip_map_ipo_keys(OBACT, ale->key_data, 1, 1);
1243                 }
1244                 else 
1245                         mirror_ipo_keys(ale->key_data, mode);
1246         }
1247         BLI_freelistN(&act_data);
1248         
1249         if (datatype == ACTCONT_ACTION)
1250                 remake_action_ipos(data);
1251         
1252         BIF_undo_push(str);
1253         allspace(REMAKEIPO, 0);
1254         allqueue(REDRAWACTION, 0);
1255         allqueue(REDRAWIPO, 0);
1256         allqueue(REDRAWNLA, 0);
1257 }
1258
1259 /* **************************************************** */
1260 /* ADD/REMOVE KEYFRAMES */
1261
1262 /* This function allows the user to insert keyframes on the current
1263  * frame from the Action Editor, using the current values of the channels
1264  * to be keyframed.  
1265  */
1266 void insertkey_action(void)
1267 {
1268         void *data;
1269         short datatype;
1270         
1271         Object *ob= OBACT;
1272         short mode;
1273         float cfra;
1274         
1275         /* get data */
1276         data= get_action_context(&datatype);
1277         if (data == NULL) return;
1278         cfra = frame_to_float(CFRA);
1279                 
1280         if (datatype == ACTCONT_ACTION) {
1281                 ListBase act_data = {NULL, NULL};
1282                 bActListElem *ale;
1283                 int filter;
1284         
1285                 /* ask user what to keyframe */
1286                 mode = pupmenu("Insert Key%t|All Channels%x1|Only Selected Channels%x2");
1287                 if (mode == 0) return;
1288                 
1289                 /* filter data */
1290                 filter= (ACTFILTER_VISIBLE | ACTFILTER_FOREDIT | ACTFILTER_ONLYICU );
1291                 if (mode == 2) filter |= ACTFILTER_SEL;
1292                 
1293                 actdata_filter(&act_data, filter, data, datatype);
1294                 
1295                 /* loop through ipo curves retrieved */
1296                 for (ale= act_data.first; ale; ale= ale->next) {
1297                         /* verify that this is indeed an ipo curve */
1298                         if (ale->key_data && ale->owner) {
1299                                 bActionChannel *achan= (bActionChannel *)ale->owner;
1300                                 IpoCurve *icu= (IpoCurve *)ale->key_data;
1301                                 
1302                                 if (ob)
1303                                         insertkey((ID *)ob, icu->blocktype, achan->name, NULL, icu->adrcode);
1304                                 else
1305                                         insert_vert_ipo(icu, cfra, icu->curval);
1306                         }
1307                 }
1308                 
1309                 /* cleanup */
1310                 BLI_freelistN(&act_data);
1311         }
1312         else if (datatype == ACTCONT_SHAPEKEY) {
1313                 Key *key= (Key *)data;
1314                 IpoCurve *icu;
1315                 
1316                 /* ask user if they want to insert a keyframe */
1317                 mode = okee("Insert Keyframe?");
1318                 if (mode == 0) return;
1319                 
1320                 if (key->ipo) {
1321                         for (icu= key->ipo->curve.first; icu; icu=icu->next) {
1322                                 insert_vert_ipo(icu, cfra, icu->curval);
1323                         }
1324                 }
1325         }
1326         
1327         BIF_undo_push("Insert Key");
1328         allspace(REMAKEIPO, 0);
1329         allqueue(REDRAWACTION, 0);
1330         allqueue(REDRAWIPO, 0);
1331         allqueue(REDRAWNLA, 0);
1332 }
1333
1334 /* delete selected keyframes */
1335 void delete_action_keys (void)
1336 {
1337         ListBase act_data = {NULL, NULL};
1338         bActListElem *ale;
1339         void *data;
1340         short datatype;
1341         int filter;
1342         
1343         /* determine what type of data we are operating on */
1344         data = get_action_context(&datatype);
1345         if (data == NULL) return;
1346         
1347         /* filter data */
1348         filter= (ACTFILTER_VISIBLE | ACTFILTER_FOREDIT | ACTFILTER_IPOKEYS);
1349         actdata_filter(&act_data, filter, data, datatype);
1350         
1351         /* loop through filtered data and delete selected keys */
1352         for (ale= act_data.first; ale; ale= ale->next) {
1353                 delete_ipo_keys((Ipo *)ale->key_data);
1354         }
1355         
1356         /* free filtered list */
1357         BLI_freelistN(&act_data);
1358         
1359         if (datatype == ACTCONT_ACTION)
1360                 remake_action_ipos(data);
1361         
1362         BIF_undo_push("Delete Action Keys");
1363         allspace(REMAKEIPO, 0);
1364         allqueue(REDRAWACTION, 0);
1365         allqueue(REDRAWIPO, 0);
1366         allqueue(REDRAWNLA, 0);
1367 }
1368
1369 /* delete selected keyframes */
1370 void delete_action_channels (void)
1371 {
1372         ListBase act_data = {NULL, NULL};
1373         bActListElem *ale, *next;
1374         bAction *act;
1375         void *data;
1376         short datatype;
1377         int filter;
1378         
1379         /* determine what type of data we are operating on */
1380         data = get_action_context(&datatype);
1381         if (data == NULL) return;
1382         if (datatype != ACTCONT_ACTION) return;
1383         act= (bAction *)data;
1384         
1385         /* filter data */
1386         filter= (ACTFILTER_VISIBLE | ACTFILTER_FOREDIT | ACTFILTER_CHANNELS | ACTFILTER_SEL);
1387         actdata_filter(&act_data, filter, data, datatype);
1388         
1389         /* remove irrelevant entries */
1390         for (ale= act_data.first; ale; ale= next) {
1391                 next= ale->next;
1392                 
1393                 if (ale->type != ACTTYPE_ACHAN)
1394                         BLI_freelinkN(&act_data, ale);
1395         }
1396         
1397         /* clean up action channels */
1398         for (ale= act_data.first; ale; ale= next) {
1399                 bActionChannel *achan= (bActionChannel *)ale->data;
1400                 bConstraintChannel *conchan, *cnext;
1401                 next= ale->next;
1402                 
1403                 /* release reference to ipo users */
1404                 if (achan->ipo)
1405                         achan->ipo->id.us--;
1406                         
1407                 for (conchan= achan->constraintChannels.first; conchan; conchan=cnext) {
1408                         cnext= conchan->next;
1409                         
1410                         if (conchan->ipo)
1411                                 conchan->ipo->id.us--;
1412                 }
1413                 
1414                 /* free memory */
1415                 BLI_freelistN(&achan->constraintChannels);
1416                 BLI_freelinkN(&act->chanbase, achan);
1417                 BLI_freelinkN(&act_data, ale);
1418         }
1419                 
1420         remake_action_ipos(data);
1421         
1422         BIF_undo_push("Delete Action Channels");
1423         allspace(REMAKEIPO, 0);
1424         allqueue(REDRAWACTION, 0);
1425         allqueue(REDRAWIPO, 0);
1426         allqueue(REDRAWNLA, 0);
1427 }
1428
1429 /* 'Clean' IPO curves - remove any unnecessary keyframes */
1430 void clean_action (void)
1431 {       
1432         ListBase act_data = {NULL, NULL};
1433         bActListElem *ale;
1434         int filter;
1435         void *data;
1436         short datatype, ok;
1437         
1438         /* don't proceed any further if nothing to work on or user refuses */
1439         data= get_action_context(&datatype);
1440         ok= fbutton(&G.scene->toolsettings->clean_thresh, 
1441                                 0.0000001f, 1.0, 0.001, 0.1,
1442                                 "Clean Threshold");
1443         if (!ok) return;
1444         
1445         /* filter data */
1446         filter= (ACTFILTER_VISIBLE | ACTFILTER_FOREDIT | ACTFILTER_SEL | ACTFILTER_ONLYICU);
1447         actdata_filter(&act_data, filter, data, datatype);
1448         
1449         /* loop through filtered data and clean curves */
1450         for (ale= act_data.first; ale; ale= ale->next) {
1451                 clean_ipo_curve((IpoCurve *)ale->key_data);
1452         }
1453         
1454         /* admin and redraws */
1455         BLI_freelistN(&act_data);
1456         
1457         BIF_undo_push("Clean Action");
1458         allqueue(REMAKEIPO, 0);
1459         allqueue(REDRAWIPO, 0);
1460         allqueue(REDRAWACTION, 0);
1461         allqueue(REDRAWNLA, 0);
1462 }
1463
1464 /* **************************************************** */
1465 /* VARIOUS SETTINGS */
1466
1467 /* this function combines several features related to setting 
1468  * various ipo extrapolation/interpolation
1469  */
1470 void action_set_ipo_flags (int mode)
1471 {
1472         ListBase act_data = {NULL, NULL};
1473         bActListElem *ale;
1474         void *data;
1475         short datatype;
1476         int filter, event;
1477         
1478         /* determine what type of data we are operating on */
1479         data = get_action_context(&datatype);
1480         if (data == NULL) return;
1481         
1482         /* determine which set of processing we are doing */
1483         switch (mode) {
1484                 case SET_EXTEND_POPUP:
1485                 {
1486                         /* present popup menu for ipo extrapolation type */
1487                         event
1488                                 =  pupmenu("Channel Extending Type %t|"
1489                                                    "Constant %x11|"
1490                                                    "Extrapolation %x12|"
1491                                                    "Cyclic %x13|"
1492                                                    "Cyclic extrapolation %x14");
1493                         if(event < 1) return;
1494                 }
1495                         break;
1496                 case SET_IPO_POPUP:
1497                 {
1498                         /* present popup menu for ipo interpolation type */
1499                         event
1500                                 =  pupmenu("Channel Ipo Type %t|"
1501                                                    "Constant %x1|"
1502                                                    "Linear %x2|"
1503                                                    "Bezier %x3");
1504                         if(event < 1) return;
1505                 }
1506                         break;
1507                         
1508                 default: /* weird, unhandled case */
1509                         return;
1510         }
1511         
1512         /* filter data */
1513         filter= (ACTFILTER_VISIBLE | ACTFILTER_FOREDIT | ACTFILTER_IPOKEYS);
1514         actdata_filter(&act_data, filter, data, datatype);
1515         
1516         /* loop through setting flags */
1517         for (ale= act_data.first; ale; ale= ale->next) {
1518                 Ipo *ipo= (Ipo *)ale->key_data; 
1519         
1520                 /* depending on the mode */
1521                 switch (mode) {
1522                         case SET_EXTEND_POPUP: /* extrapolation */
1523                         {
1524                                 switch (event) {
1525                                         case SET_EXTEND_CONSTANT:
1526                                                 setexprap_ipoloop(ipo, IPO_HORIZ);
1527                                                 break;
1528                                         case SET_EXTEND_EXTRAPOLATION:
1529                                                 setexprap_ipoloop(ipo, IPO_DIR);
1530                                                 break;
1531                                         case SET_EXTEND_CYCLIC:
1532                                                 setexprap_ipoloop(ipo, IPO_CYCL);
1533                                                 break;
1534                                         case SET_EXTEND_CYCLICEXTRAPOLATION:
1535                                                 setexprap_ipoloop(ipo, IPO_CYCLX);
1536                                                 break;
1537                                 }
1538                         }
1539                                 break;
1540                         case SET_IPO_POPUP: /* interpolation */
1541                         {
1542                                 setipotype_ipo(ipo, event);
1543                         }
1544                                 break;
1545                 }
1546         }
1547         
1548         /* cleanup */
1549         BLI_freelistN(&act_data);
1550         
1551         if (datatype == ACTCONT_ACTION)
1552                 remake_action_ipos(data);
1553         
1554         BIF_undo_push("Set Ipo Type");
1555         allspace(REMAKEIPO, 0);
1556         allqueue(REDRAWACTION, 0);
1557         allqueue(REDRAWIPO, 0);
1558         allqueue(REDRAWNLA, 0);
1559 }
1560
1561 /* this function sets the handles on keyframes */
1562 void sethandles_action_keys (int code)
1563 {
1564         ListBase act_data = {NULL, NULL};
1565         bActListElem *ale;
1566         void *data;
1567         short datatype;
1568         int filter;
1569         
1570         /* determine what type of data we are operating on */
1571         data = get_action_context(&datatype);
1572         if (data == NULL) return;
1573         
1574         /* filter data */
1575         filter= (ACTFILTER_VISIBLE | ACTFILTER_FOREDIT | ACTFILTER_IPOKEYS);
1576         actdata_filter(&act_data, filter, data, datatype);
1577         
1578         /* loop through setting flags */
1579         for (ale= act_data.first; ale; ale= ale->next) {
1580                 sethandles_ipo_keys((Ipo *)ale->key_data, code);
1581         }
1582         
1583         /* cleanup */
1584         BLI_freelistN(&act_data);
1585         if (datatype == ACTCONT_ACTION)
1586                 remake_action_ipos(data);
1587         
1588         BIF_undo_push("Set Handle Type");
1589         allspace(REMAKEIPO, 0);
1590         allqueue(REDRAWACTION, 0);
1591         allqueue(REDRAWIPO, 0);
1592         allqueue(REDRAWNLA, 0);
1593 }
1594
1595 /* ----------------------------------------- */
1596
1597 static void clever_keyblock_names (Key *key, short *mval)
1598 {
1599     KeyBlock   *kb;
1600         int        but=0, i, keynum;
1601     char       str[64];
1602         float      x;
1603         
1604         /* get the keynum cooresponding to the y value
1605          * of the mouse pointer, return if this is
1606          * an invalid key number (and we don't deal
1607          * with the speed ipo).
1608          */
1609
1610     keynum = get_nearest_key_num(key, mval, &x);
1611     if ( (keynum < 1) || (keynum >= key->totkey) )
1612         return;
1613
1614         kb= key->block.first;
1615         for (i=0; i<keynum; ++i) kb = kb->next; 
1616
1617         if (kb->name[0] == '\0') {
1618                 sprintf(str, "Key %d", keynum);
1619         }
1620         else {
1621                 strcpy(str, kb->name);
1622         }
1623
1624         if ( (kb->slidermin >= kb->slidermax) ) {
1625                 kb->slidermin = 0.0;
1626                 kb->slidermax = 1.0;
1627         }
1628
1629     add_numbut(but++, TEX, "KB: ", 0, 24, str, 
1630                "Does this really need a tool tip?");
1631         add_numbut(but++, NUM|FLO, "Slider Min:", 
1632                            -10000, kb->slidermax, &kb->slidermin, 0);
1633         add_numbut(but++, NUM|FLO, "Slider Max:", 
1634                            kb->slidermin, 10000, &kb->slidermax, 0);
1635
1636     if (do_clever_numbuts(str, but, REDRAW)) {
1637                 strcpy(kb->name, str);
1638         allqueue (REDRAWACTION, 0);
1639                 allspace(REMAKEIPO, 0);
1640         allqueue (REDRAWIPO, 0);
1641         }
1642 }
1643
1644 static void clever_achannel_names (short *mval)
1645 {
1646         void *act_channel;
1647         bActionChannel *achan= NULL;
1648         bConstraintChannel *conchan= NULL;
1649         IpoCurve *icu= NULL;
1650         
1651         int but=0;
1652     char str[64];
1653         short chantype;
1654         short expand, protect, mute;
1655         float slidermin, slidermax;
1656         
1657         /* figure out what is under cursor */
1658         act_channel= get_nearest_act_channel(mval, &chantype);
1659         
1660         /* create items for clever-numbut */
1661         if (chantype == ACTTYPE_ACHAN) {
1662                 achan= (bActionChannel *)act_channel;
1663                 
1664                 strcpy(str, achan->name);
1665                 protect= (achan->flag & ACHAN_PROTECTED);
1666                 expand = (achan->flag & ACHAN_EXPANDED);
1667                 mute = (achan->ipo)? (achan->ipo->muteipo): 0;
1668
1669                 add_numbut(but++, TEX, "ActChan: ", 0, 31, str, "Name of Action Channel");
1670                 add_numbut(but++, TOG|SHO, "Expanded", 0, 24, &expand, "Action Channel is Expanded");
1671                 add_numbut(but++, TOG|SHO, "Muted", 0, 24, &mute, "Channel is Muted");
1672                 add_numbut(but++, TOG|SHO, "Protected", 0, 24, &protect, "Channel is Protected");
1673         }
1674         else if (chantype == ACTTYPE_CONCHAN) {
1675                 conchan= (bConstraintChannel *)act_channel;
1676                 
1677                 strcpy(str, conchan->name);
1678                 protect= (conchan->flag & CONSTRAINT_CHANNEL_PROTECTED);
1679                 mute = (conchan->ipo)? (conchan->ipo->muteipo): 0;
1680                 
1681                 add_numbut(but++, TEX, "ConChan: ", 0, 29, str, "Name of Constraint Channel");
1682                 add_numbut(but++, TOG|SHO, "Muted", 0, 24, &mute, "Channel is Muted");
1683                 add_numbut(but++, TOG|SHO, "Protected", 0, 24, &protect, "Channel is Protected");
1684         }
1685         else if (chantype == ACTTYPE_ICU) {
1686                 icu= (IpoCurve *)act_channel;
1687                 
1688                 strcpy(str, getname_ipocurve(icu));
1689                 
1690                 if (IS_EQ(icu->slide_max, icu->slide_min)) {
1691                         if (IS_EQ(icu->ymax, icu->ymin)) {
1692                                 icu->slide_min= -100.0;
1693                                 icu->slide_max= 100.0;
1694                         }
1695                         else {
1696                                 icu->slide_min= icu->ymin;
1697                                 icu->slide_max= icu->ymax;
1698                         }
1699                 }
1700                 slidermin= icu->slide_min;
1701                 slidermax= icu->slide_max;
1702                 
1703                 //protect= (icu->flag & IPO_PROTECT);
1704                 mute = (icu->flag & IPO_MUTE);
1705                 
1706                 add_numbut(but++, NUM|FLO, "Slider Min:", -10000, slidermax, &slidermin, 0);
1707                 add_numbut(but++, NUM|FLO, "Slider Max:", slidermin, 10000, &slidermax, 0);
1708                 add_numbut(but++, TOG|SHO, "Muted", 0, 24, &mute, "Channel is Muted");
1709                 //add_numbut(but++, TOG|SHO, "Protected", 0, 24, &protect, "Channel is Protected");
1710         }
1711         else {
1712                 /* nothing under-cursor */
1713                 return;
1714         }
1715         
1716         /* draw clever-numbut */
1717     if (do_clever_numbuts(str, but, REDRAW)) {
1718                 /* restore settings based on type */
1719                 if (icu) {
1720                         icu->slide_min= slidermin;
1721                         icu->slide_max= slidermax;
1722                         
1723                         //if (protect) icu->flag |= IPO_PROTECT;
1724                         //else icu->flag &= ~IPO_PROTECT;
1725                         if (mute) icu->flag |= IPO_MUTE;
1726                         else icu->flag &= ~IPO_MUTE;
1727                 }
1728                 else if (conchan) {
1729                         strcpy(conchan->name, str);
1730                         
1731                         if (protect) conchan->flag |= CONSTRAINT_CHANNEL_PROTECTED;
1732                         else conchan->flag &= ~CONSTRAINT_CHANNEL_PROTECTED;
1733                         
1734                         if (conchan->ipo)
1735                                 conchan->ipo->muteipo = mute;
1736                 }
1737                 else if (achan) {
1738                         strcpy(achan->name, str);
1739                         
1740                         if (expand) achan->flag |= ACHAN_EXPANDED;
1741                         else achan->flag &= ~ACHAN_EXPANDED;
1742                         
1743                         if (protect) achan->flag |= ACHAN_PROTECTED;
1744                         else achan->flag &= ~ACHAN_PROTECTED;
1745                         
1746                         if (achan->ipo)
1747                                 achan->ipo->muteipo = mute;
1748                 }
1749                 
1750         allqueue (REDRAWACTION, 0);
1751                 allqueue (REDRAWVIEW3D, 0);
1752         }
1753 }
1754
1755 /* this gets called when nkey is pressed (no Transform Properties panel yet) */
1756 static void numbuts_action (void)
1757 {
1758         /* now called from action window event loop, plus reacts on mouseclick */
1759         /* removed Hos grunts for that reason! :) (ton) */
1760         void *data;
1761         short datatype;
1762     short mval[2];
1763         
1764         /* determine what type of data we are operating on */
1765         data = get_action_context(&datatype);
1766         getmouseco_areawin(mval);
1767         
1768         if (mval[0] < NAMEWIDTH) {
1769                 switch (datatype) {
1770                         case ACTCONT_ACTION:
1771                                 clever_achannel_names(mval);
1772                                 break;
1773                         case ACTCONT_SHAPEKEY:
1774                                 clever_keyblock_names(data, mval);
1775                                 break;
1776                 }
1777         }
1778 }
1779
1780
1781 /* **************************************************** */
1782 /* CHANNEL SELECTION */
1783
1784 static void hilight_channel(bAction *act, bActionChannel *achan, short select)
1785 {
1786         bActionChannel *curchan;
1787
1788         if (!act)
1789                 return;
1790
1791         for (curchan=act->chanbase.first; curchan; curchan=curchan->next) {
1792                 if (curchan==achan && select)
1793                         curchan->flag |= ACHAN_HILIGHTED;
1794                 else
1795                         curchan->flag &= ~ACHAN_HILIGHTED;
1796         }
1797 }
1798
1799 /* messy call... */
1800 static void select_poseelement_by_name(char *name, int select)
1801 {
1802         /* Syncs selection of channels with selection of object elements in posemode */
1803         Object *ob= OBACT;
1804         bPoseChannel *pchan;
1805         
1806         if (!ob || ob->type!=OB_ARMATURE)
1807                 return;
1808         
1809         if(select==2) {
1810                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next)
1811                         pchan->bone->flag &= ~(BONE_ACTIVE);
1812         }
1813         
1814         pchan= get_pose_channel(ob->pose, name);
1815         if(pchan) {
1816                 if(select)
1817                         pchan->bone->flag |= (BONE_SELECTED);
1818                 else 
1819                         pchan->bone->flag &= ~(BONE_SELECTED);
1820                 if(select==2)
1821                         pchan->bone->flag |= (BONE_ACTIVE);
1822         }
1823 }
1824
1825 /* apparently within active object context */
1826 /* called extern, like on bone selection */
1827 void select_actionchannel_by_name (bAction *act, char *name, int select)
1828 {
1829         bActionChannel *achan;
1830
1831         if (!act)
1832                 return;
1833
1834         for (achan = act->chanbase.first; achan; achan= achan->next) {
1835                 if (!strcmp(achan->name, name)) {
1836                         if (select) {
1837                                 achan->flag |= ACHAN_SELECTED;
1838                                 hilight_channel(act, achan, 1);
1839                         }
1840                         else {
1841                                 achan->flag &= ~ACHAN_SELECTED;
1842                                 hilight_channel(act, achan, 0);
1843                         }
1844                         return;
1845                 }
1846         }
1847 }
1848
1849 /* select_mode = SELECT_REPLACE
1850  *             = SELECT_ADD
1851  *             = SELECT_SUBTRACT
1852  *             = SELECT_INVERT
1853  */
1854
1855 /* exported for outliner (ton) */
1856 /* apparently within active object context */
1857 int select_channel(bAction *act, bActionChannel *achan, int selectmode) 
1858 {
1859         /* Select the channel based on the selection mode */
1860         int flag;
1861
1862         switch (selectmode) {
1863         case SELECT_ADD:
1864                 achan->flag |= ACHAN_SELECTED;
1865                 break;
1866         case SELECT_SUBTRACT:
1867                 achan->flag &= ~ACHAN_SELECTED;
1868                 break;
1869         case SELECT_INVERT:
1870                 achan->flag ^= ACHAN_SELECTED;
1871                 break;
1872         }
1873         flag = (achan->flag & ACHAN_SELECTED) ? 1 : 0;
1874
1875         hilight_channel(act, achan, flag);
1876         select_poseelement_by_name(achan->name, flag);
1877
1878         return flag;
1879 }
1880
1881 static int select_constraint_channel(bAction *act, 
1882                                      bConstraintChannel *conchan, 
1883                                      int selectmode) 
1884 {
1885         /* Select the constraint channel based on the selection mode */
1886         int flag;
1887
1888         switch (selectmode) {
1889         case SELECT_ADD:
1890                 conchan->flag |= CONSTRAINT_CHANNEL_SELECT;
1891                 break;
1892         case SELECT_SUBTRACT:
1893                 conchan->flag &= ~CONSTRAINT_CHANNEL_SELECT;
1894                 break;
1895         case SELECT_INVERT:
1896                 conchan->flag ^= CONSTRAINT_CHANNEL_SELECT;
1897                 break;
1898         }
1899         flag = (conchan->flag & CONSTRAINT_CHANNEL_SELECT) ? 1 : 0;
1900
1901         return flag;
1902 }
1903
1904 int select_icu_channel(bAction *act, IpoCurve *icu, int selectmode) 
1905 {
1906         /* Select the channel based on the selection mode */
1907         int flag;
1908
1909         switch (selectmode) {
1910         case SELECT_ADD:
1911                 icu->flag |= IPO_SELECT;
1912                 break;
1913         case SELECT_SUBTRACT:
1914                 icu->flag &= ~IPO_SELECT;
1915                 break;
1916         case SELECT_INVERT:
1917                 icu->flag ^= IPO_SELECT;
1918                 break;
1919         }
1920         flag = (icu->flag & IPO_SELECT) ? 1 : 0;
1921         return flag;
1922 }
1923
1924 /* ----------------------------------------- */
1925
1926 /* deselects action channels in given action */
1927 void deselect_actionchannels (bAction *act, short test)
1928 {
1929         ListBase act_data = {NULL, NULL};
1930         bActListElem *ale;
1931         int filter, sel=1;
1932         
1933         /* filter data */
1934         filter= ACTFILTER_VISIBLE;
1935         actdata_filter(&act_data, filter, act, ACTCONT_ACTION);
1936         
1937         /* See if we should be selecting or deselecting */
1938         if (test) {
1939                 for (ale= act_data.first; ale; ale= ale->next) {
1940                         if (sel == 0) 
1941                                 break;
1942                         
1943                         switch (ale->type) {
1944                                 case ACTTYPE_ACHAN:
1945                                         if (ale->flag & ACHAN_SELECTED) 
1946                                                 sel= 0;
1947                                         break;
1948                                 case ACTTYPE_CONCHAN:
1949                                         if (ale->flag & CONSTRAINT_CHANNEL_SELECT) 
1950                                                 sel=0;
1951                                         break;
1952                                 case ACTTYPE_ICU:
1953                                         if (ale->flag & IPO_SELECT)
1954                                                 sel=0;
1955                                         break;
1956                         }
1957                 }
1958         }
1959         else
1960                 sel= 0;
1961                 
1962         /* Now set the flags */
1963         for (ale= act_data.first; ale; ale= ale->next) {
1964                 switch (ale->type) {
1965                         case ACTTYPE_ACHAN:
1966                         {
1967                                 bActionChannel *achan= (bActionChannel *)ale->data;
1968                                 
1969                                 if (sel)
1970                                         achan->flag |= ACHAN_SELECTED;
1971                                 else
1972                                         achan->flag &= ~ACHAN_SELECTED;
1973                                 select_poseelement_by_name(achan->name, sel);
1974                         }
1975                                 break;
1976                         case ACTTYPE_CONCHAN:
1977                         {
1978                                 bConstraintChannel *conchan= (bConstraintChannel *)ale->data;
1979                                 
1980                                 if (sel)
1981                                         conchan->flag |= CONSTRAINT_CHANNEL_SELECT;
1982                                 else
1983                                         conchan->flag &= ~CONSTRAINT_CHANNEL_SELECT;
1984                         }
1985                                 break;
1986                         case ACTTYPE_ICU:
1987                         {
1988                                 IpoCurve *icu= (IpoCurve *)ale->data;
1989                                 
1990                                 if (sel)
1991                                         icu->flag |= IPO_SELECT;
1992                                 else
1993                                         icu->flag &= ~IPO_SELECT;
1994                         }
1995                                 break;
1996                 }
1997         }
1998         
1999         /* Cleanup */
2000         BLI_freelistN(&act_data);
2001 }
2002
2003 /* deselects channels in the action editor */
2004 void deselect_action_channels (short test)
2005 {
2006         void *data;
2007         short datatype;
2008         
2009         /* determine what type of data we are operating on */
2010         data = get_action_context(&datatype);
2011         if (data == NULL) return;
2012         
2013         /* based on type */
2014         if (datatype == ACTCONT_ACTION)
2015                 deselect_actionchannels(data, test);
2016         // should shapekey channels be allowed to do this? 
2017 }
2018
2019 /* deselects keyframes in the action editor */
2020 void deselect_action_keys (short test, short sel)
2021 {
2022         ListBase act_data = {NULL, NULL};
2023         bActListElem *ale;
2024         int filter;
2025         void *data;
2026         short datatype;
2027         
2028         /* determine what type of data we are operating on */
2029         data = get_action_context(&datatype);
2030         if (data == NULL) return;
2031                 
2032         /* filter data */
2033         filter= (ACTFILTER_VISIBLE | ACTFILTER_IPOKEYS);
2034         actdata_filter(&act_data, filter, data, datatype);
2035         
2036         /* See if we should be selecting or deselecting */
2037         if (test) {
2038                 for (ale= act_data.first; ale; ale= ale->next) {
2039                         if (is_ipo_key_selected(ale->key_data)) {
2040                                 sel= 0;
2041                                 break;
2042                         }
2043                 }
2044         }
2045                 
2046         /* Now set the flags */
2047         for (ale= act_data.first; ale; ale= ale->next) {
2048                 set_ipo_key_selection(ale->key_data, sel);
2049         }
2050         
2051         /* Cleanup */
2052         BLI_freelistN(&act_data);
2053 }
2054
2055 /* selects all keyframes in the action editor - per channel or time 
2056  *      mode = 0: all in channel; mode = 1: all in frame
2057  */
2058 void selectall_action_keys (short mval[], short mode, short select_mode)
2059 {
2060         void *data;
2061         short datatype;
2062         
2063         /* determine what type of data we are operating on */
2064         data = get_action_context(&datatype);
2065         if (data == NULL) return;
2066                 
2067         if (select_mode == SELECT_REPLACE) {
2068                 deselect_action_keys(0, 0);
2069                 select_mode = SELECT_ADD;
2070         }
2071                 
2072         /* depending on mode */
2073         switch (mode) {
2074                 case 0: /* all in channel*/
2075                 {
2076                         void *act_channel;
2077                         short chantype;
2078                         
2079                         /* get channel, and act according to type */
2080                         act_channel= get_nearest_act_channel(mval, &chantype);
2081                         switch (chantype) {
2082                                 case ACTTYPE_ACHAN:
2083                                 {
2084                                         bActionChannel *achan= (bActionChannel *)act_channel;
2085                                         select_ipo_bezier_keys(achan->ipo, select_mode);
2086                                 }
2087                                         break;
2088                                 case ACTTYPE_CONCHAN:
2089                                 {
2090                                         bConstraintChannel *conchan= (bConstraintChannel *)act_channel;
2091                                         select_ipo_bezier_keys(conchan->ipo, select_mode);
2092                                 }
2093                                         break;
2094                                 case ACTTYPE_ICU:
2095                                 {
2096                                         IpoCurve *icu= (IpoCurve *)act_channel;
2097                                         select_icu_bezier_keys(icu, select_mode);
2098                                 }
2099                                         break;
2100                         }
2101                 }
2102                         break;
2103                 case 1: /* all in frame */
2104                 {
2105                         ListBase act_data = {NULL, NULL};
2106                         bActListElem *ale;
2107                         int filter;
2108                         rcti rect;
2109                         rctf rectf;
2110                         
2111                         /* use bounding box to find kframe */
2112                         rect.xmin = rect.xmax = mval[0];
2113                         rect.ymin = rect.ymax = mval[1];
2114                         
2115                         mval[0]= rect.xmin;
2116                         mval[1]= rect.ymin+2;
2117                         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
2118                         rectf.xmax= rectf.xmin;
2119                         rectf.ymax= rectf.ymin;
2120                         
2121                         rectf.xmin = rectf.xmin - 0.5;
2122                         rectf.xmax = rectf.xmax + 0.5;
2123                         
2124                         /* filter data */
2125                         filter= (ACTFILTER_VISIBLE | ACTFILTER_IPOKEYS);
2126                         actdata_filter(&act_data, filter, data, datatype);
2127                                 
2128                         /* Now set the flags */
2129                         for (ale= act_data.first; ale; ale= ale->next)
2130                                 borderselect_ipo_key(ale->key_data, rectf.xmin, rectf.xmax, select_mode);
2131                         
2132                         /* Cleanup */
2133                         BLI_freelistN(&act_data);
2134                 }
2135                 break;
2136         }
2137         
2138         allqueue(REDRAWNLA, 0);
2139         allqueue(REDRAWACTION, 0);
2140         allqueue(REDRAWIPO, 0);
2141 }
2142
2143 void markers_selectkeys_between (void)
2144 {
2145         ListBase act_data = {NULL, NULL};
2146         bActListElem *ale;
2147         int filter;
2148         void *data;
2149         short datatype;
2150         float min, max;
2151         
2152         /* determine what type of data we are operating on */
2153         data = get_action_context(&datatype);
2154         if (data == NULL) return;
2155         
2156         /* get extreme markers */
2157         get_minmax_markers(1, &min, &max);
2158         if (min==max) return;
2159         min -= 0.5f;
2160         max += 0.5f;
2161         
2162         /* filter data */
2163         filter= (ACTFILTER_VISIBLE | ACTFILTER_IPOKEYS);
2164         actdata_filter(&act_data, filter, data, datatype);
2165                 
2166         /* select keys in-between */
2167         for (ale= act_data.first; ale; ale= ale->next) {
2168                 if(G.saction->pin==0 && OBACT && datatype==ACTCONT_ACTION) {
2169                         actstrip_map_ipo_keys(OBACT, ale->key_data, 0, 1);
2170                         borderselect_ipo_key(ale->key_data, min, max, SELECT_ADD);
2171                         actstrip_map_ipo_keys(OBACT, ale->key_data, 1, 1);
2172                 }
2173                 else {
2174                         borderselect_ipo_key(ale->key_data, min, max, SELECT_ADD);
2175                 }
2176         }
2177         
2178         /* Cleanup */
2179         BLI_freelistN(&act_data);
2180 }
2181
2182 /* ----------------------------------------- */
2183
2184 /* This function makes a list of the selected keyframes
2185  * in the ipo curves it has been passed
2186  */
2187 static void make_sel_cfra_list(Ipo *ipo, ListBase *elems)
2188 {
2189         IpoCurve *icu;
2190         
2191         if (ipo == NULL) return;
2192         
2193         for(icu= ipo->curve.first; icu; icu= icu->next) {
2194                 BezTriple *bezt;
2195                 int a= 0;
2196                 
2197                 for (bezt=icu->bezt; a<icu->totvert; a++, bezt++) {
2198                         if (bezt && BEZSELECTED(bezt))
2199                                 add_to_cfra_elem(elems, bezt);
2200                 }
2201         }
2202 }
2203
2204 /* This function selects all key frames in the same column(s) as a already selected key(s)
2205  * or marker(s), or all the keyframes on a particular frame (triggered by a RMB on x-scrollbar)
2206  */
2207 void column_select_action_keys(int mode)
2208 {
2209         ListBase elems= {NULL, NULL};
2210         CfraElem *ce;
2211         IpoCurve *icu;
2212         ListBase act_data = {NULL, NULL};
2213         bActListElem *ale;
2214         int filter;
2215         void *data;
2216         short datatype;
2217         
2218         /* determine what type of data we are operating on */
2219         data = get_action_context(&datatype);
2220         if (data == NULL) return;
2221         
2222         /* build list of columns */
2223         switch (mode) {
2224                 case 1: /* list of selected keys */
2225                         filter= (ACTFILTER_VISIBLE | ACTFILTER_IPOKEYS);
2226                         actdata_filter(&act_data, filter, data, datatype);
2227                         
2228                         for (ale= act_data.first; ale; ale= ale->next)
2229                                 make_sel_cfra_list(ale->key_data, &elems);
2230                         
2231                         BLI_freelistN(&act_data);
2232                         break;
2233                 case 2: /* list of selected markers */
2234                         make_marker_cfra_list(&elems, 1);
2235                         
2236                         /* apply scaled action correction if needed */
2237                         if (G.saction->pin==0 && OBACT && datatype==ACTCONT_ACTION) {
2238                                 for (ce= elems.first; ce; ce= ce->next) 
2239                                         ce->cfra= get_action_frame(OBACT, ce->cfra);
2240                         }
2241                         break;
2242         }
2243         
2244         /* loop through all of the keys and select additional keyframes
2245          * based on the keys found to be selected above
2246          */
2247         filter= (ACTFILTER_VISIBLE | ACTFILTER_ONLYICU);
2248         actdata_filter(&act_data, filter, data, datatype);
2249         
2250         for (ale= act_data.first; ale; ale= ale->next) {
2251                 for(ce= elems.first; ce; ce= ce->next) {
2252                         for (icu= ale->key_data; icu; icu= icu->next) {
2253                                 BezTriple *bezt;
2254                                 int verts = 0;
2255                                 
2256                                 for (bezt=icu->bezt; verts<icu->totvert; bezt++, verts++) {
2257                                         if (bezt) {
2258                                                 if( (int)(ce->cfra) == (int)(bezt->vec[1][0]) )
2259                                                         bezt->f2 |= 1;
2260                                         }
2261                                 }
2262                         }
2263                 }
2264         }
2265         
2266         BLI_freelistN(&act_data);
2267         BLI_freelistN(&elems);
2268 }
2269
2270 /* borderselect: for keyframes only */
2271 void borderselect_action (void)
2272 {
2273         ListBase act_data = {NULL, NULL};
2274         bActListElem *ale;
2275         int filter;
2276         void *data;
2277         short datatype;
2278         
2279         rcti rect;
2280         rctf rectf;
2281         int val, selectmode;
2282         int (*select_function)(BezTriple *);
2283         short   mval[2];
2284         float   ymin, ymax;
2285         
2286         /* determine what type of data we are operating on */
2287         data = get_action_context(&datatype);
2288         if (data == NULL) return;
2289         
2290         /* draw and handle the borderselect stuff (ui) and get the select rect */
2291         if ( (val = get_border(&rect, 3)) ) {
2292                 if (val == LEFTMOUSE) {
2293                         selectmode = SELECT_ADD;
2294                         select_function = select_bezier_add;
2295                 }
2296                 else {
2297                         selectmode = SELECT_SUBTRACT;
2298                         select_function = select_bezier_subtract;
2299                 }
2300                 
2301                 mval[0]= rect.xmin;
2302                 mval[1]= rect.ymin+2;
2303                 areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
2304                 mval[0]= rect.xmax;
2305                 mval[1]= rect.ymax-2;
2306                 areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
2307                 
2308                 /* if action is mapped in NLA, it returns a correction */
2309                 if (G.saction->pin==0 && OBACT && datatype==ACTCONT_ACTION) {
2310                         rectf.xmin= get_action_frame(OBACT, rectf.xmin);
2311                         rectf.xmax= get_action_frame(OBACT, rectf.xmax);
2312                 }
2313                 
2314                 ymax = CHANNELHEIGHT/2;
2315                 
2316                 /* filter data */
2317                 filter= (ACTFILTER_VISIBLE | ACTFILTER_CHANNELS);
2318                 actdata_filter(&act_data, filter, data, datatype);
2319                 
2320                 /* loop over data, doing border select */
2321                 for (ale= act_data.first; ale; ale= ale->next) {
2322                         ymin=ymax-(CHANNELHEIGHT+CHANNELSKIP);
2323                         if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))) {
2324                                 if (ale->key_data) {
2325                                         if (ale->datatype == ALE_IPO)
2326                                                 borderselect_ipo_key(ale->key_data, rectf.xmin, rectf.xmax, selectmode);
2327                                         else if (ale->datatype == ALE_ICU)
2328                                                 borderselect_icu_key(ale->key_data, rectf.xmin, rectf.xmax, select_function);
2329                                 }
2330                         }
2331                         ymax=ymin;
2332                 }
2333                 
2334                 /* cleanup */
2335                 BLI_freelistN(&act_data);
2336                 
2337                 BIF_undo_push("Border Select Action");
2338                 allqueue(REDRAWIPO, 0);
2339                 allqueue(REDRAWACTION, 0);
2340                 allqueue(REDRAWNLA, 0);
2341         }
2342 }
2343
2344 /* **************************************************** */
2345 /* MOUSE-HANDLING */
2346
2347 /* right-hand side - mouse click */
2348 static void mouse_action (int selectmode)
2349 {
2350         void *data;
2351         short datatype;
2352         bAction *act= NULL;
2353         bActionChannel *achan= NULL;
2354         bConstraintChannel *conchan= NULL;
2355         IpoCurve *icu= NULL;
2356         TimeMarker *marker;
2357         void *act_channel;
2358         short sel, act_type;
2359         float selx;
2360         
2361         /* determine what type of data we are operating on */
2362         data = get_action_context(&datatype);
2363         if (data == NULL) return;
2364         if (datatype == ACTCONT_ACTION) act= (bAction *)data;
2365
2366         act_channel= get_nearest_action_key(&selx, &sel, &act_type, &achan);
2367         marker=find_nearest_marker(1);
2368                 
2369         if (act_channel) {
2370                 switch (act_type) {
2371                         case ACTTYPE_ICU:
2372                                 icu= (IpoCurve *)act_channel;
2373                                 break;
2374                         case ACTTYPE_CONCHAN:
2375                                 conchan= (bConstraintChannel *)act_channel;
2376                                 break;
2377                         default:
2378                                 achan= (bActionChannel *)act_channel;
2379                 }
2380                 
2381                 if (selectmode == SELECT_REPLACE) {
2382                         selectmode = SELECT_ADD;
2383                         
2384                         deselect_action_keys(0, 0);
2385                         
2386                         if (datatype == ACTCONT_ACTION) {
2387                                 deselect_action_channels(0);
2388                                 
2389                                 achan->flag |= ACHAN_SELECTED;
2390                                 hilight_channel(act, achan, 1);
2391                                 select_poseelement_by_name(achan->name, 2);     /* 2 is activate */
2392                         }
2393                 }
2394                 
2395                 if (icu)
2396                         select_icu_key(icu, selx, selectmode);
2397                 else if (conchan)
2398                         select_ipo_key(conchan->ipo, selx, selectmode);
2399                 else
2400                         select_ipo_key(achan->ipo, selx, selectmode);
2401                 
2402                 std_rmouse_transform(transform_action_keys);
2403                 
2404                 allqueue(REDRAWIPO, 0);
2405                 allqueue(REDRAWVIEW3D, 0);
2406                 allqueue(REDRAWACTION, 0);
2407                 allqueue(REDRAWNLA, 0);
2408                 allqueue(REDRAWOOPS, 0);
2409                 allqueue(REDRAWBUTSALL, 0);
2410         }
2411         else if (marker) {
2412                 /* not channel, so maybe marker */              
2413                 if (selectmode == SELECT_REPLACE) {                     
2414                         deselect_markers(0, 0);
2415                         marker->flag |= SELECT;
2416                 }
2417                 else if (selectmode == SELECT_INVERT) {
2418                         if (marker->flag & SELECT)
2419                                 marker->flag &= ~SELECT;
2420                         else
2421                                 marker->flag |= SELECT;
2422                 }
2423                 else if (selectmode == SELECT_ADD) 
2424                         marker->flag |= SELECT;
2425                 else if (selectmode == SELECT_SUBTRACT)
2426                         marker->flag &= ~SELECT;
2427                 
2428                 std_rmouse_transform(transform_markers);
2429                 
2430                 allqueue(REDRAWTIME, 0);
2431                 allqueue(REDRAWIPO, 0);
2432                 allqueue(REDRAWACTION, 0);
2433                 allqueue(REDRAWNLA, 0);
2434                 allqueue(REDRAWSOUND, 0);
2435         }
2436 }
2437
2438 /* lefthand side - mouse-click  */
2439 static void mouse_actionchannels (short mval[])
2440 {
2441         bAction *act= G.saction->action;
2442         void *data, *act_channel;
2443         short datatype, chantype;
2444         
2445         /* determine what type of data we are operating on */
2446         data = get_action_context(&datatype);
2447         if (data == NULL) return;
2448         
2449         /* get channel to work on */
2450         act_channel= get_nearest_act_channel(mval, &chantype);
2451         
2452         /* action to take depends on what channel we've got */
2453         switch (chantype) {
2454                 case ACTTYPE_ACHAN:
2455                         {
2456                                 bActionChannel *achan= (bActionChannel *)act_channel;
2457                                 
2458                                 if (mval[0] >= (NAMEWIDTH-16)) {
2459                                         /* toggle protect */
2460                                         achan->flag ^= ACHAN_PROTECTED;
2461                                 }
2462                                 else if ((mval[0] >= (NAMEWIDTH-32)) && (achan->ipo)) {
2463                                         /* toggle mute */
2464                                         achan->ipo->muteipo = (achan->ipo->muteipo)? 0: 1;
2465                                 }
2466                                 else if (mval[0] <= 17) {
2467                                         /* toggle expand */
2468                                         achan->flag ^= ACHAN_EXPANDED;
2469                                 }                               
2470                                 else {
2471                                         /* select/deselect achan */             
2472                                         if (G.qual & LR_SHIFTKEY) {
2473                                                 select_channel(act, achan, SELECT_INVERT);
2474                                         }
2475                                         else {
2476                                                 deselect_actionchannels(act, 0);
2477                                                 select_channel(act, achan, SELECT_ADD);
2478                                         }
2479                                         
2480                                         /* messy... set active bone */
2481                                         select_poseelement_by_name(achan->name, 2);
2482                                 }
2483                         }
2484                                 break;
2485                 case ACTTYPE_FILLIPO:
2486                         {
2487                                 bActionChannel *achan= (bActionChannel *)act_channel;
2488                                 
2489                                 achan->flag ^= ACHAN_SHOWIPO;
2490                                 
2491                                 if ((mval[0] > 24) && (achan->flag & ACHAN_SHOWIPO)) {
2492                                         /* select+make active achan */          
2493                                         deselect_actionchannels(act, 0);
2494                                         select_channel(act, achan, SELECT_ADD);
2495                                         
2496                                         /* messy... set active bone */
2497                                         select_poseelement_by_name(achan->name, 2);
2498                                 }       
2499                         }
2500                         break;
2501                 case ACTTYPE_FILLCON:
2502                         {
2503                                 bActionChannel *achan= (bActionChannel *)act_channel;
2504                                 
2505                                 achan->flag ^= ACHAN_SHOWCONS;
2506                                 
2507                                 if ((mval[0] > 24) && (achan->flag & ACHAN_SHOWCONS)) {
2508                                         /* select+make active achan */          
2509                                         deselect_actionchannels(act, 0);
2510                                         select_channel(act, achan, SELECT_ADD);
2511                                         
2512                                         /* messy... set active bone */
2513                                         select_poseelement_by_name(achan->name, 2);
2514                                 }       
2515                         }
2516                         break;
2517                 case ACTTYPE_ICU: 
2518                         {
2519                                 IpoCurve *icu= (IpoCurve *)act_channel;
2520                                 
2521 #if 0 /* disabled until all ipo tools support this ------->  */
2522                                 if (mval[0] >= (NAMEWIDTH-16)) {
2523                                         /* toggle protection */
2524                                         icu->flag ^= IPO_PROTECT;
2525                                 }
2526 #endif /* <------- end of disabled code */
2527                                 if (mval[0] >= (NAMEWIDTH-16)) {
2528                                         /* toggle mute */
2529                                         icu->flag ^= IPO_MUTE;
2530                                 }
2531                                 else {
2532                                         /* select/deselect */
2533                                         select_icu_channel(act, icu, SELECT_INVERT);
2534                                 }
2535                         }
2536                         break;
2537                 case ACTTYPE_CONCHAN:
2538                         {
2539                                 bConstraintChannel *conchan= (bConstraintChannel *)act_channel;
2540                                 
2541                                 if (mval[0] >= (NAMEWIDTH-16)) {
2542                                         /* toggle protection */
2543                                         conchan->flag ^= CONSTRAINT_CHANNEL_PROTECTED;
2544                                 }
2545                                 else if ((mval[0] >= (NAMEWIDTH-32)) && (conchan->ipo)) {
2546                                         /* toggle mute */
2547                                         conchan->ipo->muteipo = (conchan->ipo->muteipo)? 0: 1;
2548                                 }
2549                                 else {
2550                                         /* select/deselect */
2551                                         select_constraint_channel(act, conchan, SELECT_INVERT);
2552                                 }
2553                         }
2554                                 break;
2555                 default:
2556                         return;
2557         }
2558         
2559         allqueue (REDRAWIPO, 0);
2560         allqueue (REDRAWVIEW3D, 0);
2561         allqueue (REDRAWACTION, 0);
2562         allqueue (REDRAWNLA, 0);
2563         allqueue (REDRAWOOPS, 0);
2564         allqueue (REDRAWBUTSALL, 0);
2565 }
2566
2567 /* **************************************************** */
2568 /* ACTION CHANNEL RE-ORDERING */
2569
2570 void top_sel_action ()
2571 {
2572         bAction *act;
2573         bActionChannel *achan;
2574         
2575         /* Get the selected action, exit if none are selected */
2576         act = G.saction->action;
2577         if (!act) return;
2578         
2579         for (achan= act->chanbase.first; achan; achan= achan->next){
2580                 if (VISIBLE_ACHAN(achan)) {
2581                         if (SEL_ACHAN(achan) && !(achan->flag & ACHAN_MOVED)){
2582                                 /* take it out off the chain keep data */
2583                                 BLI_remlink (&act->chanbase, achan);
2584                                 /* make it first element */
2585                                 BLI_insertlinkbefore(&act->chanbase, act->chanbase.first, achan);
2586                                 achan->flag |= ACHAN_MOVED;
2587                                 /* restart with rest of list */
2588                                 achan= achan->next;
2589                         }
2590                 }
2591         }
2592     /* clear temp flags */
2593         for (achan= act->chanbase.first; achan; achan= achan->next){
2594                 achan->flag = achan->flag & ~ACHAN_MOVED;
2595         }
2596         
2597         /* Clean up and redraw stuff */
2598         remake_action_ipos (act);
2599         BIF_undo_push("Top Action channel");
2600         allspace(REMAKEIPO, 0);
2601         allqueue(REDRAWACTION, 0);
2602         allqueue(REDRAWIPO, 0);
2603         allqueue(REDRAWNLA, 0);
2604 }
2605
2606 void up_sel_action ()
2607 {
2608         bAction *act;
2609         bActionChannel *achan, *prev;
2610         
2611         /* Get the selected action, exit if none are selected */
2612         act = G.saction->action;
2613         if (!act) return;
2614         
2615         for (achan=act->chanbase.first; achan; achan= achan->next) {
2616                 if (VISIBLE_ACHAN(achan)) {
2617                         if (SEL_ACHAN(achan) && !(achan->flag & ACHAN_MOVED)){
2618                                 prev = achan->prev;
2619                                 if (prev) {
2620                                         /* take it out off the chain keep data */
2621                                         BLI_remlink (&act->chanbase, achan);
2622                                         /* push it up */
2623                                         BLI_insertlinkbefore(&act->chanbase, prev, achan);
2624                                         achan->flag |= ACHAN_MOVED;
2625                                         /* restart with rest of list */
2626                                         achan= achan->next;
2627                                 }
2628                         }
2629                 }
2630         }
2631         /* clear temp flags */
2632         for (achan=act->chanbase.first; achan; achan= achan->next){
2633                 achan->flag = achan->flag & ~ACHAN_MOVED;
2634         }
2635         
2636         /* Clean up and redraw stuff */
2637         remake_action_ipos (act);
2638         BIF_undo_push("Up Action channel");
2639         allspace(REMAKEIPO, 0);
2640         allqueue(REDRAWACTION, 0);
2641         allqueue(REDRAWIPO, 0);
2642         allqueue(REDRAWNLA, 0);
2643 }
2644
2645 void down_sel_action ()
2646 {
2647         bAction *act;
2648         bActionChannel *achan, *next;
2649         
2650         /* Get the selected action, exit if none are selected */
2651         act = G.saction->action;
2652         if (!act) return;
2653         
2654         for (achan= act->chanbase.last; achan; achan= achan->prev) {
2655                 if (VISIBLE_ACHAN(achan)) {
2656                         if (SEL_ACHAN(achan) && !(achan->flag & ACHAN_MOVED)){
2657                                 next = achan->next;
2658                                 if (next) next = next->next;
2659                                 if (next) {
2660                                         /* take it out off the chain keep data */
2661                                         BLI_remlink (&act->chanbase, achan);
2662                                         /* move it down */
2663                                         BLI_insertlinkbefore(&act->chanbase, next, achan);
2664                                         achan->flag |= ACHAN_MOVED;
2665                                 }
2666                                 else {
2667                                         /* take it out off the chain keep data */
2668                                         BLI_remlink (&act->chanbase, achan);
2669                                         /* add at end */
2670                                         BLI_addtail(&act->chanbase, achan);
2671                                         achan->flag |= ACHAN_MOVED;
2672                                 }
2673                         }
2674                 }
2675         }
2676         /* clear temp flags */
2677         for (achan= act->chanbase.first; achan; achan= achan->next){
2678                 achan->flag = achan->flag & ~ACHAN_MOVED;
2679         }
2680         
2681         /* Clean up and redraw stuff */
2682         remake_action_ipos (act);
2683         BIF_undo_push("Down Action channel");
2684         allspace(REMAKEIPO, 0);
2685         allqueue(REDRAWACTION, 0);
2686         allqueue(REDRAWIPO, 0);
2687         allqueue(REDRAWNLA, 0);
2688 }
2689
2690 void bottom_sel_action ()
2691 {
2692         bAction *act;
2693         bActionChannel *achan;
2694         
2695         /* Get the selected action, exit if none are selected */
2696         act = G.saction->action;
2697         if (!act) return;
2698         
2699         for (achan=act->chanbase.last; achan; achan= achan->prev) {
2700                 if (VISIBLE_ACHAN(achan)) {
2701                         if (SEL_ACHAN(achan) && !(achan->flag & ACHAN_MOVED)) {
2702                                 /* take it out off the chain keep data */
2703                                 BLI_remlink (&act->chanbase, achan);
2704                                 /* add at end */
2705                                 BLI_addtail(&act->chanbase, achan);
2706                                 achan->flag |= ACHAN_MOVED;
2707                         }
2708                 }               
2709         }
2710         /* clear temp flags */
2711         for (achan=act->chanbase.first; achan; achan= achan->next) {
2712                 achan->flag = achan->flag & ~ACHAN_MOVED;
2713         }
2714         
2715         /* Clean up and redraw stuff */
2716         remake_action_ipos (act);
2717         BIF_undo_push("Bottom Action channel");
2718         allspace(REMAKEIPO, 0);
2719         allqueue(REDRAWACTION, 0);
2720         allqueue(REDRAWIPO, 0);
2721         allqueue(REDRAWNLA, 0);
2722 }
2723
2724 /* **************************************************** */
2725 /* EVENT HANDLING */
2726
2727 void winqreadactionspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
2728 {
2729         extern void do_actionbuts(unsigned short event); // drawaction.c
2730         SpaceAction *saction;
2731         void *data;
2732         short datatype;
2733         float dx, dy;
2734         int doredraw= 0;
2735         int     cfra;
2736         short mval[2];
2737         unsigned short event= evt->event;
2738         short val= evt->val;
2739         short mousebut = L_MOUSE;
2740
2741         if(curarea->win==0) return;
2742
2743         saction= curarea->spacedata.first;
2744         if (!saction)
2745                 return;
2746
2747         data= get_action_context(&datatype);
2748         
2749         if (val) {
2750                 if ( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
2751                 
2752                 /* swap mouse buttons based on user preference */
2753                 if (U.flag & USER_LMOUSESELECT) {
2754                         if (event == LEFTMOUSE) {
2755                                 event = RIGHTMOUSE;
2756                                 mousebut = L_MOUSE;
2757                         } else if (event == RIGHTMOUSE) {
2758                                 event = LEFTMOUSE;
2759                                 mousebut = R_MOUSE;
2760                         }
2761                 }
2762                 
2763                 getmouseco_areawin(mval);
2764                 
2765                 switch(event) {
2766                 case UI_BUT_EVENT:
2767                         do_actionbuts(val);     // window itself
2768                         break;
2769                 
2770                 case HOMEKEY:
2771                         do_action_buttons(B_ACTHOME);   // header
2772                         break;
2773
2774                 case AKEY:
2775                         if (mval[0]<NAMEWIDTH) {
2776                                 deselect_action_channels (1);
2777                                 allqueue (REDRAWVIEW3D, 0);
2778                                 allqueue (REDRAWACTION, 0);
2779                                 allqueue(REDRAWNLA, 0);
2780                                 allqueue (REDRAWIPO, 0);
2781                         }
2782                         else if (mval[0]>ACTWIDTH) {
2783                                 if (G.qual == LR_CTRLKEY) {
2784                                         deselect_markers (1, 0);
2785                                         allqueue(REDRAWTIME, 0);
2786                                         allqueue(REDRAWIPO, 0);
2787                                         allqueue(REDRAWACTION, 0);
2788                                         allqueue(REDRAWNLA, 0);
2789                                         allqueue(REDRAWSOUND, 0);
2790                                 }
2791                                 else {
2792                                         deselect_action_keys (1, 1);
2793                                         allqueue (REDRAWACTION, 0);
2794                                         allqueue(REDRAWNLA, 0);
2795                                         allqueue (REDRAWIPO, 0);
2796                                 }
2797                         }
2798                         break;
2799
2800                 case BKEY:
2801                         if (G.qual & LR_CTRLKEY) {
2802                                 borderselect_markers();
2803                         }
2804                         else {
2805                                 if (mval[0]>ACTWIDTH)
2806                                         borderselect_action();
2807                         }
2808                         break;
2809
2810                 case CKEY:
2811                         /* scroll the window so the current
2812                          * frame is in the center.
2813                          */
2814                         center_currframe();
2815                         break;
2816
2817                 case DKEY:
2818                         if (mval[0]>ACTWIDTH) {
2819                                 if (G.qual == (LR_CTRLKEY|LR_SHIFTKEY))
2820                                         duplicate_marker();
2821                                 else if (G.qual == LR_SHIFTKEY)
2822                                         duplicate_action_keys();
2823                         }
2824                         break;
2825
2826                 case GKEY:
2827                         if (G.qual & LR_CTRLKEY) {
2828                                 transform_markers('g', 0);
2829                         }
2830                         else {
2831                                 if (mval[0]>=ACTWIDTH)
2832                                         transform_action_keys('g', 0);
2833                         }
2834                         break;
2835                 
2836                 case HKEY:
2837                         if(G.qual & LR_SHIFTKEY) {
2838                                 if(okee("Set Keys to Auto Handle"))
2839                                         sethandles_action_keys(HD_AUTO);
2840                         }
2841                         else {
2842                                 if(okee("Toggle Keys Aligned Handle"))
2843                                         sethandles_action_keys(HD_ALIGN);
2844                         }
2845                         break;
2846                 
2847                 case KKEY:
2848                         if (G.qual & LR_CTRLKEY) {
2849                                 markers_selectkeys_between();
2850                         }
2851                         else {
2852                                 val= (G.qual & LR_SHIFTKEY) ? 2 : 1;
2853                                 column_select_action_keys(val);
2854                         }
2855                         
2856                         allqueue(REDRAWTIME, 0);
2857                         allqueue(REDRAWIPO, 0);
2858                         allqueue(REDRAWACTION, 0);
2859                         allqueue(REDRAWNLA, 0);
2860                         break;
2861                         
2862                 case MKEY:
2863                         if (G.qual & LR_SHIFTKEY) {
2864                                 /* mirror keyframes */
2865                                 if (data) {
2866                                         val = pupmenu("Mirror Keys Over%t|Current Frame%x1|Vertical Axis%x2|Horizontal Axis %x3|Selected Marker %x4");
2867                                         mirror_action_keys(val);
2868                                 }
2869                         }
2870                         else {
2871                                 /* marker operations */
2872                                 if (G.qual == 0)
2873                                         add_marker(CFRA);
2874                                 else if (G.qual == LR_CTRLKEY)
2875                                         rename_marker();
2876                                 else 
2877                                         break;
2878                                 allqueue(REDRAWTIME, 0);
2879                                 allqueue(REDRAWIPO, 0);
2880                                 allqueue(REDRAWACTION, 0);
2881                                 allqueue(REDRAWNLA, 0);
2882                                 allqueue(REDRAWSOUND, 0);
2883                         }
2884                         break;
2885                         
2886                 case NKEY:
2887                         if(G.qual==0) {
2888                                 numbuts_action();
2889                                 
2890                                 /* no panel (yet). current numbuts are not easy to put in panel... */
2891                                 //add_blockhandler(curarea, ACTION_HANDLER_PROPERTIES, UI_PNL_TO_MOUSE);
2892                                 //scrarea_queue_winredraw(curarea);
2893                         }
2894                         break;
2895                         
2896                 case OKEY:
2897                         clean_action();
2898                         break;
2899                         
2900                 case PKEY:
2901                         if (G.qual & LR_CTRLKEY) /* set preview range */
2902                                 anim_previewrange_set();
2903                         else if (G.qual & LR_ALTKEY) /* clear preview range */
2904                                 anim_previewrange_clear();
2905                         allqueue(REDRAWTIME, 0);
2906                         allqueue(REDRAWBUTSALL, 0);
2907                         allqueue(REDRAWACTION, 0);
2908                         allqueue(REDRAWNLA, 0);
2909                         allqueue(REDRAWIPO, 0);
2910                         break;
2911                         
2912                 case SKEY: 
2913                         if (mval[0]>=ACTWIDTH) {
2914                                 if(G.qual & LR_SHIFTKEY) {
2915                                         if (data) {
2916                                                 val = pupmenu("Snap Keys To%t|Nearest Frame%x1|Current Frame%x2|Nearest Marker %x3");
2917                                                 snap_action_keys(val);
2918                                         }
2919                                 }
2920                                 else
2921                                         transform_action_keys ('s', 0); 
2922                         }
2923                         break;
2924                 
2925                 case TKEY:
2926                         if(G.qual & LR_SHIFTKEY)
2927                                 action_set_ipo_flags(SET_IPO_POPUP);
2928                         else
2929                                 transform_action_keys ('t', 0);
2930                         break;
2931
2932                 case VKEY:
2933                         if(okee("Set Keys to Vector Handle"))
2934                                 sethandles_action_keys(HD_VECT);
2935                         break;
2936
2937                 case PAGEUPKEY:
2938                         if (datatype == ACTCONT_ACTION) {
2939                                 if(G.qual & LR_SHIFTKEY)
2940                                         top_sel_action();
2941                                 else if (G.qual & LR_CTRLKEY)
2942                                         up_sel_action();
2943                                 else
2944                                         nextprev_marker(1);
2945                         }
2946                         else if (datatype == ACTCONT_SHAPEKEY) {
2947                                 /* only jump to markers possible (key channels can't be moved yet) */
2948                                 nextprev_marker(1);
2949                         }
2950                         break;
2951                 case PAGEDOWNKEY:
2952                         if (datatype == ACTCONT_ACTION) {
2953                                 if(G.qual & LR_SHIFTKEY)
2954                                         bottom_sel_action();
2955                                 else if (G.qual & LR_CTRLKEY) 
2956                                         down_sel_action();
2957                                 else
2958                                         nextprev_marker(-1);
2959                         }
2960                         else if (datatype == ACTCONT_SHAPEKEY) {