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