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