6dad0f8eb88ec423b6e16df2d19971b3539b5afb
[blender.git] / source / blender / src / editaction.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <string.h>
34 #include <math.h>
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include "MEM_guardedalloc.h"
41
42 #include "PIL_time.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_arithb.h"
46
47 #include "DNA_action_types.h"
48 #include "DNA_armature_types.h"
49 #include "DNA_curve_types.h"
50 #include "DNA_ipo_types.h"
51 #include "DNA_object_types.h"
52 #include "DNA_scene_types.h"
53 #include "DNA_screen_types.h"
54 #include "DNA_userdef_types.h"
55 #include "DNA_constraint_types.h"
56 #include "DNA_key_types.h"
57 #include "DNA_mesh_types.h"
58 #include "DNA_lattice_types.h"
59
60 #include "BKE_action.h"
61 #include "BKE_armature.h"
62 #include "BKE_constraint.h"
63 #include "BKE_curve.h"
64 #include "BKE_depsgraph.h"
65 #include "BKE_global.h"
66 #include "BKE_ipo.h"
67 #include "BKE_key.h"
68 #include "BKE_library.h"
69 #include "BKE_main.h"
70 #include "BKE_utildefines.h"
71
72 #include "BIF_butspace.h"
73 #include "BIF_editaction.h"
74 #include "BIF_editview.h"
75 #include "BIF_editarmature.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_editipo.h"
88 #include "BSE_trans_types.h"
89
90 #include "BDR_editobject.h"
91
92 #include "mydevice.h"
93 #include "blendef.h"
94 #include "nla.h"
95
96 extern int count_action_levels (bAction *act);
97
98 #define BEZSELECTED(bezt)   (((bezt)->f1 & 1) || ((bezt)->f2 & 1) || ((bezt)->f3 & 1))
99
100 /* Local Function prototypes, are forward needed */
101 static void insertactionkey(bAction *act, bActionChannel *achan, bPoseChannel *chan, int adrcode, short makecurve, float time);
102 static void hilight_channel (bAction *act, bActionChannel *chan, short hilight);
103 static void set_action_key_time (bAction *act, bPoseChannel *chan, int adrcode, short makecurve, float time);
104
105 static void up_sel_action(void);
106 static void down_sel_action(void);
107 static void top_sel_action(void);
108 static void bottom_sel_action(void);
109
110 /* Implementation */
111
112 short showsliders = 0;
113 short ACTWIDTH = NAMEWIDTH;
114
115 static void select_poseelement_by_name (char *name, int select)
116 {
117         /* Synchs selection of channels with selection of object elements in posemode */
118
119         Object *ob= OBACT;
120
121         if (!ob)
122                 return;
123
124         switch (ob->type){
125         case OB_ARMATURE:
126                 select_bone_by_name ((bArmature*)ob->data, name, select);
127                 break;
128         default:
129                 break;
130         }
131 }
132
133 bAction* bake_action_with_client (bAction *act, Object *armob, float tolerance)
134 {
135         bArmature               *arm;
136         bAction                 *result=NULL;
137         bActionChannel *achan;
138         bAction                 *temp;
139         bPoseChannel    *pchan;
140         float                   actlen;
141         int                             oldframe;
142         int                             curframe;
143         char                    newname[64];
144
145         if (!act)
146                 return NULL;
147
148         arm = get_armature(armob);
149
150         if (G.obedit){
151                 error ("Actions can't be baked in Edit Mode");
152                 return NULL;
153         }
154
155         if (!arm || armob->pose==NULL){
156                 error ("Select an armature before baking");
157                 return NULL;
158         }
159         
160         /* Get a new action */
161         result = add_empty_action();
162
163         /* Assign the new action a unique name */
164         sprintf (newname, "%s.BAKED", act->id.name+2);
165         rename_id(&result->id, newname);
166
167         actlen = calc_action_end(act);
168
169         oldframe = G.scene->r.cfra;
170
171         temp = armob->action;
172         armob->action = act;
173         
174         for (curframe=1; curframe<ceil(actlen+1); curframe++){
175
176                 /* Apply the old action */
177                 
178                 G.scene->r.cfra = curframe;
179
180                 /* Apply the object ipo */
181                 extract_pose_from_action(armob->pose, act, curframe);
182
183                 where_is_pose(armob);
184                 
185                 /* For each channel: set quats and locs if channel is a bone */
186                 for (pchan=armob->pose->chanbase.first; pchan; pchan=pchan->next){
187
188                         /* Apply to keys */
189                         set_action_key_time (result, pchan, AC_QUAT_X, 1, curframe);
190                         set_action_key_time (result, pchan, AC_QUAT_Y, 1, curframe);
191                         set_action_key_time (result, pchan, AC_QUAT_Z, 1, curframe);
192                         set_action_key_time (result, pchan, AC_QUAT_W, 1, curframe);
193                         set_action_key_time (result, pchan, AC_LOC_X, 1, curframe);
194                         set_action_key_time (result, pchan, AC_LOC_Y, 1, curframe);
195                         set_action_key_time (result, pchan, AC_LOC_Z, 1, curframe);
196                         set_action_key_time (result, pchan, AC_SIZE_X, 1, curframe);
197                         set_action_key_time (result, pchan, AC_SIZE_Y, 1, curframe);
198                         set_action_key_time (result, pchan, AC_SIZE_Z, 1, curframe);
199                 }
200         }
201
202
203         /* Make another pass to ensure all keyframes are set to linear interpolation mode */
204         for (achan = result->chanbase.first; achan; achan=achan->next){
205                 IpoCurve* icu;
206                 for (icu = achan->ipo->curve.first; icu; icu=icu->next){
207                         icu->ipo= IPO_LIN;
208                 }
209         }
210
211         notice ("Made a new action named \"%s\"", newname);
212         G.scene->r.cfra = oldframe;
213         armob->action = temp;
214                 
215         /* restore */
216         extract_pose_from_action(armob->pose, act, G.scene->r.cfra);
217         where_is_pose(armob);
218         
219         allqueue(REDRAWACTION, 1);
220         
221         return result;
222 }
223
224 /* apparently within active object context */
225 void select_actionchannel_by_name (bAction *act, char *name, int select)
226 {
227         bActionChannel *chan;
228
229         if (!act)
230                 return;
231
232         for (chan = act->chanbase.first; chan; chan=chan->next){
233                 if (!strcmp (chan->name, name)){
234                         act->achan = chan;
235                         if (select){
236                                 chan->flag |= ACHAN_SELECTED;
237                                 hilight_channel (act, chan, 1);
238                                 select_poseelement_by_name(chan->name, 1);
239                         }
240                         else{
241                                 chan->flag &= ~ACHAN_SELECTED;
242                                 hilight_channel (act, chan, 0);
243                                 select_poseelement_by_name(chan->name, 0);
244                         }
245                         return;
246                 }
247         }
248 }
249
250 void remake_action_ipos(bAction *act)
251 {
252         bActionChannel *chan;
253         bConstraintChannel *conchan;
254         IpoCurve                *icu;
255
256         for (chan= act->chanbase.first; chan; chan=chan->next){
257                 if (chan->ipo){
258                         for (icu = chan->ipo->curve.first; icu; icu=icu->next){
259                                 sort_time_ipocurve(icu);
260                                 testhandles_ipocurve(icu);
261                         }
262                 }
263                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
264                         if (conchan->ipo){
265                                 for (icu = conchan->ipo->curve.first; icu; icu=icu->next){
266                                         sort_time_ipocurve(icu);
267                                         testhandles_ipocurve(icu);
268                                 }
269                         }
270                 }
271         }
272 }
273
274 static void remake_meshaction_ipos(Ipo *ipo)
275 {
276         /* this puts the bezier triples in proper
277          * order and makes sure the bezier handles
278          * aren't too strange.
279          */
280         IpoCurve *icu;
281
282         for (icu = ipo->curve.first; icu; icu=icu->next){
283                 sort_time_ipocurve(icu);
284                 testhandles_ipocurve(icu);
285         }
286 }
287
288 static void meshkey_do_redraw(Key *key)
289 {
290         remake_meshaction_ipos(key->ipo);
291
292         DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
293         
294         allspace(REMAKEIPO, 0);
295         allqueue(REDRAWACTION, 0);
296         allqueue(REDRAWIPO, 0);
297         allqueue(REDRAWNLA, 0);
298
299 }
300
301 void duplicate_meshchannel_keys(Key *key)
302 {
303         duplicate_ipo_keys(key->ipo);
304         transform_meshchannel_keys ('g', key);
305 }
306
307
308 void duplicate_actionchannel_keys(void)
309 {
310         bAction *act;
311         bActionChannel *chan;
312         bConstraintChannel *conchan;
313
314         act=G.saction->action;
315         if (!act)
316                 return;
317
318         /* Find selected items */
319         for (chan = act->chanbase.first; chan; chan=chan->next){
320                 duplicate_ipo_keys(chan->ipo);
321                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
322                         duplicate_ipo_keys(conchan->ipo);
323         }
324
325         transform_actionchannel_keys ('g');
326 }
327
328 static bActionChannel *get_nearest_actionchannel_key (float *index, short *sel, bConstraintChannel **rchan){
329         bAction *act;
330         bActionChannel *chan;
331         IpoCurve *icu;
332         bActionChannel *firstchan=NULL;
333         bConstraintChannel *conchan, *firstconchan=NULL;
334         int     foundsel=0;
335         float firstvert=-1, foundx=-1;
336                 int i;
337         short mval[2];
338         float ymin, ymax;
339         rctf    rectf;
340         *index=0;
341
342         *rchan=NULL;
343         act=G.saction->action; /* We presume that we are only called during a valid action */
344         
345         getmouseco_areawin (mval);
346
347         mval[0]-=7;
348         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
349         mval[0]+=14;
350         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
351
352         ymax = count_action_levels(act) * (CHANNELHEIGHT + CHANNELSKIP);
353         ymax += CHANNELHEIGHT/2;
354         
355         *sel=0;
356
357         for (chan=act->chanbase.first; chan; chan=chan->next){
358
359                 /* Check action channel */
360                 ymin= ymax-(CHANNELHEIGHT+CHANNELSKIP);
361                 if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
362                         for (icu=chan->ipo->curve.first; icu; icu=icu->next){
363                                 for (i=0; i<icu->totvert; i++){
364                                         if (icu->bezt[i].vec[1][0] > rectf.xmin && icu->bezt[i].vec[1][0] <= rectf.xmax ){
365                                                 if (!firstchan){
366                                                         firstchan=chan;
367                                                         firstvert=icu->bezt[i].vec[1][0];
368                                                         *sel = icu->bezt[i].f2 & 1;     
369                                                 }
370                                                 
371                                                 if (icu->bezt[i].f2 & 1){ 
372                                                         if (!foundsel){
373                                                                 foundsel=1;
374                                                                 foundx = icu->bezt[i].vec[1][0];
375                                                         }
376                                                 }
377                                                 else if (foundsel && icu->bezt[i].vec[1][0] != foundx){
378                                                         *index=icu->bezt[i].vec[1][0];
379                                                         *sel = 0;
380                                                         return chan;
381                                                 }
382                                         }
383                                 }
384                         }
385                 }
386                 ymax=ymin;
387                 
388                 /* Check constraint channels */
389                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
390                         ymin=ymax-(CHANNELHEIGHT+CHANNELSKIP);
391                         if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
392                                 for (icu=conchan->ipo->curve.first; icu; icu=icu->next){
393                                         for (i=0; i<icu->totvert; i++){
394                                                 if (icu->bezt[i].vec[1][0] > rectf.xmin && icu->bezt[i].vec[1][0] <= rectf.xmax ){
395                                                         if (!firstchan){
396                                                                 firstchan=chan;
397                                                                 firstconchan=conchan;
398                                                                 firstvert=icu->bezt[i].vec[1][0];
399                                                                 *sel = icu->bezt[i].f2 & 1;     
400                                                         }
401                                                         
402                                                         if (icu->bezt[i].f2 & 1){ 
403                                                                 if (!foundsel){
404                                                                         foundsel=1;
405                                                                         foundx = icu->bezt[i].vec[1][0];
406                                                                 }
407                                                         }
408                                                         else if (foundsel && icu->bezt[i].vec[1][0] != foundx){
409                                                                 *index=icu->bezt[i].vec[1][0];
410                                                                 *sel = 0;
411                                                                 *rchan = conchan;
412                                                                 return chan;
413                                                         }
414                                                 }
415                                         }
416                                 }
417                         }
418                         ymax=ymin;
419                 }
420         }       
421         
422         *rchan = firstconchan;
423         *index=firstvert;
424         return firstchan;
425 }
426
427 static IpoCurve *get_nearest_meshchannel_key (float *index, short *sel)
428 {
429         /* This function tries to find the RVK key that is
430          * closest to the user's mouse click
431          */
432     Key      *key;
433     IpoCurve *icu; 
434     IpoCurve *firsticu=NULL;
435     int      foundsel=0;
436     float    firstvert=-1, foundx=-1;
437         int      i;
438     short    mval[2];
439     float    ymin, ymax, ybase;
440     rctf     rectf;
441
442     *index=0;
443
444     key = get_action_mesh_key();
445         
446     /* lets get the mouse position and process it so 
447      * we can start testing selections
448      */
449     getmouseco_areawin (mval);
450     mval[0]-=7;
451     areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
452     mval[0]+=14;
453     areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
454
455     ybase = key->totkey * (CHANNELHEIGHT + CHANNELSKIP);
456         ybase += CHANNELHEIGHT/2;
457     *sel=0;
458
459     /* lets loop through the IpoCurves trying to find the closest
460      * bezier
461      */
462         if (!key->ipo) return NULL;
463     for (icu = key->ipo->curve.first; icu ; icu = icu->next) {
464         /* lets not deal with the "speed" Ipo
465          */
466         if (!icu->adrcode) continue;
467
468         ymax = ybase    - (CHANNELHEIGHT+CHANNELSKIP)*(icu->adrcode-1);
469         ymin=ymax-(CHANNELHEIGHT+CHANNELSKIP);
470
471         /* Does this curve coorespond to the right
472          * strip?
473          */
474         if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
475                         
476             /* loop through the beziers in the curve
477              */
478             for (i=0; i<icu->totvert; i++){
479
480                 /* Is this bezier in the right area?
481                  */
482                 if (icu->bezt[i].vec[1][0] > rectf.xmin && 
483                     icu->bezt[i].vec[1][0] <= rectf.xmax ){
484
485                     /* if no other curves have been picked ...
486                      */
487                     if (!firsticu){
488                         /* mark this curve/bezier as the first
489                          * selected
490                          */
491                         firsticu=icu;
492                         firstvert=icu->bezt[i].vec[1][0];
493
494                         /* sel = (is the bezier is already selected) ? 1 : 0;
495                          */
496                         *sel = icu->bezt[i].f2 & 1;     
497                     }
498
499                     /* if the bezier is selected ...
500                      */
501                     if (icu->bezt[i].f2 & 1){ 
502                         /* if we haven't found a selected one yet ...
503                          */
504                         if (!foundsel){
505                             /* record the found x value
506                              */
507                             foundsel=1;
508                             foundx = icu->bezt[i].vec[1][0];
509                         }
510                     }
511
512                     /* if the bezier is unselected and not at the x
513                      * position of a previous found selected bezier ...
514                      */
515                     else if (foundsel && icu->bezt[i].vec[1][0] != foundx){
516                         /* lets return this found curve/bezier
517                          */
518                         *index=icu->bezt[i].vec[1][0];
519                         *sel = 0;
520                         return icu;
521                     }
522                 }
523             }
524         }
525         }
526         
527     /* return what we've found
528      */
529     *index=firstvert;
530     return firsticu;
531 }
532
533 /* apparently within active object context */
534 static void mouse_action(int selectmode)
535 {
536         bAction *act;
537         short sel;
538         float   selx;
539         bActionChannel *chan;
540         bConstraintChannel *conchan;
541         short   mval[2];
542
543         act=G.saction->action;
544         if (!act)
545                 return;
546
547         getmouseco_areawin (mval);
548
549         chan=get_nearest_actionchannel_key(&selx, &sel, &conchan);
550
551         if (chan){
552                 if (selectmode == SELECT_REPLACE) {
553                         if (sel == 0)
554                                 selectmode = SELECT_ADD;
555                         else
556                                 selectmode = SELECT_SUBTRACT;
557                         deselect_actionchannel_keys(act, 0);
558                         deselect_actionchannels(act, 0);
559                         act->achan = chan;
560                         chan->flag |= ACHAN_SELECTED;
561                         hilight_channel (act, chan, 1);
562                         select_poseelement_by_name(chan->name, 1);
563                 }
564                 
565                 if (conchan)
566                         select_ipo_key(conchan->ipo, selx, selectmode);
567                 else
568                         select_ipo_key(chan->ipo, selx, selectmode);
569
570                 BIF_undo_push("Select Action");
571                 allqueue(REDRAWIPO, 0);
572                 allqueue(REDRAWVIEW3D, 0);
573                 allqueue(REDRAWACTION, 0);
574                 allqueue(REDRAWNLA, 0);
575
576         }
577 }
578
579 static void mouse_mesh_action(int selectmode, Key *key)
580 {
581         /* Handle a right mouse click selection in an
582          * action window displaying RVK data
583          */
584
585     IpoCurve *icu;
586     short  sel;
587     float  selx;
588     short  mval[2];
589
590     /* going to assume that the only reason 
591      * we got here is because it has been 
592      * determined that we are a mesh with
593      * the right properties (i.e., have key
594      * data, etc)
595      */
596
597         /* get the click location, and the cooresponding
598          * ipo curve and selection time value
599          */
600     getmouseco_areawin (mval);
601     icu = get_nearest_meshchannel_key(&selx, &sel);
602
603     if (icu){
604         if (selectmode == SELECT_REPLACE) {
605                         /* if we had planned to replace the
606                          * selection, then we will first deselect
607                          * all of the keys, and if the clicked on
608                          * key had been unselected, we will select 
609                          * it, otherwise, we are done.
610                          */
611             deselect_meshchannel_keys(key, 0);
612
613             if (sel == 0)
614                 selectmode = SELECT_ADD;
615             else
616                                 /* the key is selected so we should
617                                  * deselect -- but everything is now deselected
618                                  * so we are done.
619                                  */
620                                 return;
621         }
622                 
623                 /* select the key using the given mode
624                  * and redraw as mush stuff as needed.
625                  */
626                 select_icu_key(icu, selx, selectmode);
627
628                 BIF_undo_push("Select Action key");
629         allqueue(REDRAWIPO, 0);
630         allqueue(REDRAWVIEW3D, 0);
631         allqueue(REDRAWACTION, 0);
632         allqueue(REDRAWNLA, 0);
633
634     }
635 }
636
637 void borderselect_action(void)
638
639         rcti rect;
640         rctf rectf;
641         int val, selectmode;            
642         short   mval[2];
643         bActionChannel *chan;
644         bConstraintChannel *conchan;
645         bAction *act;
646         float   ymin, ymax;
647
648         act=G.saction->action;
649
650
651         if (!act)
652                 return;
653
654         if ( (val = get_border(&rect, 3)) ){
655     if (val == LEFTMOUSE)
656       selectmode = SELECT_ADD;
657     else
658       selectmode = SELECT_SUBTRACT;
659
660                 mval[0]= rect.xmin;
661                 mval[1]= rect.ymin+2;
662                 areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
663                 mval[0]= rect.xmax;
664                 mval[1]= rect.ymax-2;
665                 areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
666                 
667                 ymax= count_action_levels(act) * (CHANNELHEIGHT+CHANNELSKIP);
668                 ymax += CHANNELHEIGHT/2;
669                 
670                 for (chan=act->chanbase.first; chan; chan=chan->next){
671                         
672                         /* Check action */
673                         ymin=ymax-(CHANNELHEIGHT+CHANNELSKIP);
674                         if (!((ymax < rectf.ymin) || (ymin > rectf.ymax)))
675           borderselect_ipo_key(chan->ipo, rectf.xmin, rectf.xmax,
676                                selectmode);
677
678                         ymax=ymin;
679
680                         /* Check constraints */
681                         for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
682                                 ymin=ymax-(CHANNELHEIGHT+CHANNELSKIP);
683                                 if (!((ymax < rectf.ymin) || (ymin > rectf.ymax)))
684                                         borderselect_ipo_key(conchan->ipo, rectf.xmin, rectf.xmax,
685                                selectmode);
686                                 
687                                 ymax=ymin;
688                         }
689                 }       
690                 BIF_undo_push("Border Select Action");
691                 allqueue(REDRAWNLA, 0);
692                 allqueue(REDRAWACTION, 0);
693                 allqueue(REDRAWIPO, 0);
694         }
695 }
696
697 void borderselect_mesh(Key *key)
698
699         rcti     rect;
700         int      val, adrcodemax, adrcodemin;
701         short    mval[2];
702         float    xmin, xmax;
703         int      (*select_function)(BezTriple *);
704         IpoCurve *icu;
705
706         if ( (val = get_border(&rect, 3)) ){
707                 /* set the selection function based on what
708                  * mouse button had been used in the border
709                  * select
710                  */
711                 if (val == LEFTMOUSE)
712                         select_function = select_bezier_add;
713                 else
714                         select_function = select_bezier_subtract;
715
716                 /* get the minimum and maximum adrcode numbers
717                  * for the IpoCurves (this is the number that
718                  * relates an IpoCurve to the keyblock it
719                  * controls).
720                  */
721                 mval[0]= rect.xmin;
722                 mval[1]= rect.ymin+2;
723                 adrcodemax = get_nearest_key_num(key, mval, &xmin);
724                 adrcodemax = (adrcodemax >= key->totkey) ? key->totkey : adrcodemax;
725
726                 mval[0]= rect.xmax;
727                 mval[1]= rect.ymax-2;
728                 adrcodemin = get_nearest_key_num(key, mval, &xmax);
729                 adrcodemin = (adrcodemin < 1) ? 1 : adrcodemin;
730
731                 /* Lets loop throug the IpoCurves and do borderselect
732                  * on the curves with adrcodes in our selected range.
733                  */
734                 for (icu = key->ipo->curve.first; icu ; icu = icu->next) {
735                         /* lets not deal with the "speed" Ipo
736                          */
737                         if (!icu->adrcode) continue;
738                         if ( (icu->adrcode >= adrcodemin) && 
739                                  (icu->adrcode <= adrcodemax) ) {
740                                 borderselect_icu_key(icu, xmin, xmax, select_function);
741                         }
742                 }
743
744                 /* redraw stuff */
745                 BIF_undo_push("Border select Action Key");
746                 allqueue(REDRAWNLA, 0);
747                 allqueue(REDRAWACTION, 0);
748                 allqueue(REDRAWIPO, 0);
749         }
750 }
751
752 /* used by ipo, outliner, buttons to find the active channel */
753 bActionChannel* get_hilighted_action_channel(bAction* action)
754 {
755         bActionChannel *chan;
756
757         if (!action)
758                 return NULL;
759
760         for (chan=action->chanbase.first; chan; chan=chan->next){
761                 if (chan->flag & ACHAN_SELECTED && chan->flag & ACHAN_HILIGHTED)
762                         return chan;
763         }
764
765         return NULL;
766
767 }
768
769 /* sets action->achan to active channel, also adds if needed */
770 void verify_active_action_channel(Object *ob)
771 {
772         if(ob) {
773                 bPoseChannel *pchan;
774                 bActionChannel *achan;
775                 
776                 if(ob->action==NULL) return;
777                 
778                 pchan= get_active_posechannel(ob);
779                 if(pchan) {
780                         /* See if this action channel exists already */ 
781                         for (achan=ob->action->chanbase.first; achan; achan=achan->next){
782                                 if (!strcmp (pchan->name, achan->name))
783                                         break;
784                         }
785                         
786                         if (!achan){
787                                 achan = MEM_callocN (sizeof(bActionChannel), "actionChannel");
788                                 strcpy (achan->name, pchan->name);
789                                 BLI_addtail (&ob->action->chanbase, achan);
790                         }
791                         
792                         ob->action->achan= achan;
793                         ob->action->pchan= pchan;
794                         
795                         for (achan=ob->action->chanbase.first; achan; achan=achan->next)
796                                 achan->flag &= ~(ACHAN_SELECTED|ACHAN_HILIGHTED);
797                         
798                         ob->action->achan->flag |= ACHAN_SELECTED|ACHAN_HILIGHTED;
799
800                 }
801         }               
802 }
803
804 void set_exprap_action(int mode)
805 {
806         if(G.saction->action && G.saction->action->id.lib) return;
807
808         error ("Not yet implemented!");
809 }
810
811 void set_action_key (struct bAction *act, struct bPoseChannel *chan, int adrcode, short makecurve)
812 {
813         set_action_key_time (act, chan, adrcode, makecurve, frame_to_float(CFRA));
814 }
815
816 static void set_action_key_time (bAction *act, bPoseChannel *chan, int adrcode, short makecurve, float time)
817 {
818         bActionChannel  *achan;
819         char    ipstr[256];
820
821         if (!act)
822                 return;
823
824         if (!chan)
825                 return;
826         
827         /* See if this action channel exists already */ 
828         for (achan=act->chanbase.first; achan; achan=achan->next){
829                 if (!strcmp (chan->name, achan->name))
830                         break;
831         }
832
833         if (!achan){
834                 if (!makecurve)
835                         return;
836                 achan = MEM_callocN (sizeof(bActionChannel), "actionChannel");
837                 strcpy (achan->name, chan->name);
838                 BLI_addtail (&act->chanbase, achan);
839         }
840
841         /* Ensure the channel appears selected in the action window */
842         /* ton: added flag hilighted, for display in ipowin. dunno what the difference is between select/hilite */
843         achan->flag |= ACHAN_SELECTED|ACHAN_HILIGHTED;
844
845         /* Ensure this action channel has a valid Ipo */
846         if (!achan->ipo){
847                 sprintf (ipstr, "%s.%s", act->id.name+2, chan->name);
848                 ipstr[23]=0;
849                 achan->ipo=     add_ipo(ipstr, ID_AC);  
850         }
851
852         insertactionkey(act, achan, chan, adrcode, makecurve, time);
853
854 }
855
856 static void insertactionkey(bAction *act, bActionChannel *achan, bPoseChannel *chan, int adrcode, short makecurve, float cfra)
857 {
858         IpoCurve *icu;
859         void *poin;
860         float curval;
861         int type;
862         ID *id;
863         
864         if (!act){
865                 return;
866         }
867         if (act->id.lib){
868                 error ("Can't pose library actions");
869                 return;
870         }
871         act->achan=achan;
872         act->pchan=chan;
873
874         id=(ID*) act;
875
876         /* First see if this curve exists */
877         if (!makecurve){
878                 if (!achan->ipo)
879                         return;
880
881                 for (icu = achan->ipo->curve.first; icu; icu=icu->next){
882                         if (icu->adrcode == adrcode)
883                                 break;
884                 }
885                 if (!icu)
886                         return;
887         }
888
889         
890         icu = get_ipocurve (id, GS(id->name), adrcode, achan->ipo);
891
892         if(icu) {
893                 poin= get_ipo_poin(id, icu, &type);
894                 if(poin) {
895                         curval= read_ipo_poin(poin, type);
896         //              cfra= frame_to_float(CFRA);
897                         insert_vert_ipo(icu, cfra, curval);
898                 }
899         }
900         
901 }
902
903 bAction *add_empty_action(void)
904 {
905         bAction *act;
906
907         act= alloc_libblock(&G.main->action, ID_AC, "Action");
908         act->id.flag |= LIB_FAKEUSER;
909         act->id.us++;
910         return act;
911 }
912
913 void transform_actionchannel_keys(char mode)
914 {
915         bAction *act;
916         TransVert *tv;
917         Object *ob= OBACT;
918         bConstraintChannel *conchan;
919         bActionChannel  *chan;
920         float   deltax, startx;
921         float   cenf[2];
922         float   sval[2], cval[2], lastcval[2];
923         float   fac=0.0F;
924         int             loop=1;
925         int             tvtot=0;
926         int             invert=0, firsttime=1;
927         int             i;
928         short   cancel=0;
929         short   mvals[2], mvalc[2], cent[2];
930         char    str[256];
931
932         act=G.saction->action;
933
934         /* Ensure that partial selections result in beztriple selections */
935         for (chan=act->chanbase.first; chan; chan=chan->next){
936                 tvtot+=fullselect_ipo_keys(chan->ipo);
937
938                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
939                         tvtot+=fullselect_ipo_keys(conchan->ipo);
940         }
941         
942         /* If nothing is selected, bail out */
943         if (!tvtot)
944                 return;
945         
946         
947         /* Build the transvert structure */
948         tv = MEM_callocN (sizeof(TransVert) * tvtot, "transVert");
949         tvtot=0;
950         for (chan=act->chanbase.first; chan; chan=chan->next){
951                 /* Add the actionchannel */
952                 tvtot = add_trans_ipo_keys(chan->ipo, tv, tvtot);
953                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
954                         tvtot = add_trans_ipo_keys(conchan->ipo, tv, tvtot);
955         }
956
957         /* Do the event loop */
958         cent[0] = curarea->winx + (G.saction->v2d.hor.xmax)/2;
959         cent[1] = curarea->winy + (G.saction->v2d.hor.ymax)/2;
960         areamouseco_to_ipoco(G.v2d, cent, &cenf[0], &cenf[1]);
961
962         getmouseco_areawin (mvals);
963         areamouseco_to_ipoco(G.v2d, mvals, &sval[0], &sval[1]);
964
965         startx=sval[0];
966         while (loop) {
967                 /*              Get the input */
968                 /*              If we're cancelling, reset transformations */
969                 /*                      Else calc new transformation */
970                 /*              Perform the transformations */
971                 while (qtest()) {
972                         short val;
973                         unsigned short event= extern_qread(&val);
974
975                         if (val) {
976                                 switch (event) {
977                                 case LEFTMOUSE:
978                                 case SPACEKEY:
979                                 case RETKEY:
980                                         loop=0;
981                                         break;
982                                 case XKEY:
983                                         break;
984                                 case ESCKEY:
985                                 case RIGHTMOUSE:
986                                         cancel=1;
987                                         loop=0;
988                                         break;
989                                 default:
990                                         arrows_move_cursor(event);
991                                         break;
992                                 };
993                         }
994                 }
995
996                 if (cancel) {
997                         for (i=0; i<tvtot; i++) {
998                                 tv[i].loc[0]=tv[i].oldloc[0];
999                                 tv[i].loc[1]=tv[i].oldloc[1];
1000                         }
1001                 } else {
1002                         getmouseco_areawin (mvalc);
1003                         areamouseco_to_ipoco(G.v2d, mvalc, &cval[0], &cval[1]);
1004
1005                         if (!firsttime && lastcval[0]==cval[0] && lastcval[1]==cval[1]) {
1006                                 PIL_sleep_ms(1);
1007                         } else {
1008                                 for (i=0; i<tvtot; i++){
1009                                         tv[i].loc[0]=tv[i].oldloc[0];
1010
1011                                         switch (mode){
1012                                         case 'g':
1013                                                 deltax = cval[0]-sval[0];
1014                                                 fac= deltax;
1015                                                 
1016                                                 apply_keyb_grid(&fac, 0.0, 1.0, 0.1, U.flag & USER_AUTOGRABGRID);
1017
1018                                                 tv[i].loc[0]+=fac;
1019                                                 break;
1020                                         case 's':
1021                                                 startx=mvals[0]-(ACTWIDTH/2+(curarea->winrct.xmax-curarea->winrct.xmin)/2);
1022                                                 deltax=mvalc[0]-(ACTWIDTH/2+(curarea->winrct.xmax-curarea->winrct.xmin)/2);
1023                                                 fac= fabs(deltax/startx);
1024                                                 
1025                                                 apply_keyb_grid(&fac, 0.0, 0.2, 0.1, U.flag & USER_AUTOSIZEGRID);
1026                 
1027                                                 if (invert){
1028                                                         if (i % 03 == 0){
1029                                                                 memcpy (tv[i].loc, tv[i].oldloc, sizeof(tv[i+2].oldloc));
1030                                                         }
1031                                                         if (i % 03 == 2){
1032                                                                 memcpy (tv[i].loc, tv[i].oldloc, sizeof(tv[i-2].oldloc));
1033                                                         }
1034         
1035                                                         fac*=-1;
1036                                                 }
1037                                                 startx= (G.scene->r.cfra);
1038                                         
1039                                                 tv[i].loc[0]-= startx;
1040                                                 tv[i].loc[0]*=fac;
1041                                                 tv[i].loc[0]+= startx;
1042                 
1043                                                 break;
1044                                         }
1045                                 }
1046                         }
1047         
1048                         if (mode=='s'){
1049                                 sprintf(str, "sizeX: %.3f", fac);
1050                                 headerprint(str);
1051                         }
1052                         else if (mode=='g'){
1053                                 sprintf(str, "deltaX: %.3f", fac);
1054                                 headerprint(str);
1055                         }
1056         
1057                         if (G.saction->lock){
1058                                 if(ob && ob->pose) {
1059                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1060                                 }
1061                                 force_draw_plus(SPACE_VIEW3D, 0);
1062                         }
1063                         else {
1064                                 force_draw(0);
1065                         }
1066                 }
1067                 
1068                 lastcval[0]= cval[0];
1069                 lastcval[1]= cval[1];
1070                 firsttime= 0;
1071         }
1072         
1073         /*              Update the curve */
1074         /*              Depending on the lock status, draw necessary views */
1075
1076         if(ob && ob->pose) {
1077                 DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
1078         }
1079         remake_action_ipos(act);
1080
1081         if(cancel==0) BIF_undo_push("Transform Action");
1082         allqueue (REDRAWVIEW3D, 0);
1083         allqueue (REDRAWACTION, 0);
1084         allqueue(REDRAWNLA, 0);
1085         allqueue (REDRAWIPO, 0);
1086         MEM_freeN (tv);
1087 }
1088
1089 void transform_meshchannel_keys(char mode, Key *key)
1090 {
1091         /* this is the function that determines what happens
1092          * to those little blocky rvk key things you have selected 
1093          * after you press a 'g' or an 's'. I'd love to say that
1094          * I have an intimate knowledge of all of what this function
1095          * is doing, but instead I'm just going to pretend.
1096          */
1097     TransVert *tv;
1098     int /*sel=0,*/  i;
1099     short       mvals[2], mvalc[2], cent[2];
1100     float       sval[2], cval[2], lastcval[2];
1101     short       cancel=0;
1102     float       fac=0.0F;
1103     int         loop=1;
1104     int         tvtot=0;
1105     float       deltax, startx;
1106     float       cenf[2];
1107     int         invert=0, firsttime=1;
1108     char        str[256];
1109
1110         /* count all of the selected beziers, and
1111          * set all 3 control handles to selected
1112          */
1113     tvtot=fullselect_ipo_keys(key->ipo);
1114     
1115     /* If nothing is selected, bail out 
1116          */
1117     if (!tvtot)
1118         return;
1119         
1120         
1121     /* Build the transvert structure 
1122          */
1123     tv = MEM_callocN (sizeof(TransVert) * tvtot, "transVert");
1124     tvtot=0;
1125
1126     tvtot = add_trans_ipo_keys(key->ipo, tv, tvtot);
1127
1128     /* Do the event loop 
1129          */
1130     cent[0] = curarea->winx + (G.saction->v2d.hor.xmax)/2;
1131     cent[1] = curarea->winy + (G.saction->v2d.hor.ymax)/2;
1132     areamouseco_to_ipoco(G.v2d, cent, &cenf[0], &cenf[1]);
1133
1134     getmouseco_areawin (mvals);
1135     areamouseco_to_ipoco(G.v2d, mvals, &sval[0], &sval[1]);
1136
1137     startx=sval[0];
1138     while (loop) {
1139         /* Get the input
1140                  * If we're cancelling, reset transformations
1141                  * Else calc new transformation
1142                  * Perform the transformations 
1143                  */
1144         while (qtest()) {
1145             short val;
1146             unsigned short event= extern_qread(&val);
1147
1148             if (val) {
1149                 switch (event) {
1150                 case LEFTMOUSE:
1151                 case SPACEKEY:
1152                 case RETKEY:
1153                     loop=0;
1154                     break;
1155                 case XKEY:
1156                     break;
1157                 case ESCKEY:
1158                 case RIGHTMOUSE:
1159                     cancel=1;
1160                     loop=0;
1161                     break;
1162                 default:
1163                     arrows_move_cursor(event);
1164                     break;
1165                 };
1166             }
1167         }
1168         
1169         if (cancel) {
1170             for (i=0; i<tvtot; i++) {
1171                 tv[i].loc[0]=tv[i].oldloc[0];
1172                 tv[i].loc[1]=tv[i].oldloc[1];
1173             }
1174         } 
1175                 else {
1176             getmouseco_areawin (mvalc);
1177             areamouseco_to_ipoco(G.v2d, mvalc, &cval[0], &cval[1]);
1178                         
1179             if (!firsttime && lastcval[0]==cval[0] && lastcval[1]==cval[1]) {
1180                 PIL_sleep_ms(1);
1181             } else {
1182                 for (i=0; i<tvtot; i++){
1183                     tv[i].loc[0]=tv[i].oldloc[0];
1184
1185                     switch (mode){
1186                     case 'g':
1187                         deltax = cval[0]-sval[0];
1188                         fac= deltax;
1189                                                 
1190                         apply_keyb_grid(&fac, 0.0, 1.0, 0.1, 
1191                                         U.flag & USER_AUTOGRABGRID);
1192
1193                         tv[i].loc[0]+=fac;
1194                         break;
1195                     case 's':
1196                         startx=mvals[0]-(ACTWIDTH/2+(curarea->winrct.xmax
1197                                                      -curarea->winrct.xmin)/2);
1198                         deltax=mvalc[0]-(ACTWIDTH/2+(curarea->winrct.xmax
1199                                                      -curarea->winrct.xmin)/2);
1200                         fac= fabs(deltax/startx);
1201                                                 
1202                         apply_keyb_grid(&fac, 0.0, 0.2, 0.1, 
1203                                         U.flag & USER_AUTOSIZEGRID);
1204                 
1205                         if (invert){
1206                             if (i % 03 == 0){
1207                                 memcpy (tv[i].loc, tv[i].oldloc, 
1208                                         sizeof(tv[i+2].oldloc));
1209                             }
1210                             if (i % 03 == 2){
1211                                 memcpy (tv[i].loc, tv[i].oldloc, 
1212                                         sizeof(tv[i-2].oldloc));
1213                             }
1214                                                         
1215                             fac*=-1;
1216                         }
1217                         startx= (G.scene->r.cfra);
1218                         
1219                         tv[i].loc[0]-= startx;
1220                         tv[i].loc[0]*=fac;
1221                         tv[i].loc[0]+= startx;
1222                 
1223                         break;
1224                     }
1225                 }
1226             }
1227                         /* Display a message showing the magnitude of
1228                          * the grab/scale we are performing
1229                          */
1230             if (mode=='s'){
1231                 sprintf(str, "sizeX: %.3f", fac);
1232                 headerprint(str);
1233             }
1234             else if (mode=='g'){
1235                 sprintf(str, "deltaX: %.3f", fac);
1236                 headerprint(str);
1237             }
1238         
1239             if (G.saction->lock){
1240                                 /* doubt any of this code ever gets
1241                                  * executed, but it might in the
1242                                  * future
1243                                  */
1244                                  
1245                                 DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
1246                 allqueue (REDRAWVIEW3D, 0);
1247                 allqueue (REDRAWACTION, 0);
1248                 allqueue (REDRAWIPO, 0);
1249                 allqueue(REDRAWNLA, 0);
1250                 force_draw_all(0);
1251             }
1252             else {
1253                 addqueue (curarea->win, REDRAWALL, 0);
1254                 force_draw(0);
1255             }
1256         }
1257                 
1258         lastcval[0]= cval[0];
1259         lastcval[1]= cval[1];
1260         firsttime= 0;
1261     }
1262         
1263         /* fix up the Ipocurves and redraw stuff
1264          */
1265     meshkey_do_redraw(key);
1266         BIF_undo_push("Transform Action Keys");
1267
1268     MEM_freeN (tv);
1269
1270         /* did you understand all of that? I pretty much understand
1271          * what it does, but the specifics seem a little weird and crufty.
1272          */
1273 }
1274
1275
1276 void deselect_actionchannel_keys (bAction *act, int test)
1277 {
1278         bActionChannel  *chan;
1279         bConstraintChannel *conchan;
1280         int             sel=1;;
1281
1282         if (!act)
1283                 return;
1284
1285         /* Determine if this is selection or deselection */
1286         
1287         if (test){
1288                 for (chan=act->chanbase.first; chan; chan=chan->next){
1289                         /* Test the channel ipos */
1290                         if (is_ipo_key_selected(chan->ipo)){
1291                                 sel = 0;
1292                                 break;
1293                         }
1294
1295                         /* Test the constraint ipos */
1296                         for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
1297                                 if (is_ipo_key_selected(conchan->ipo)){
1298                                         sel = 0;
1299                                         break;
1300                                 }
1301                         }
1302
1303                         if (sel == 0)
1304                                 break;
1305                 }
1306         }
1307         else
1308                 sel=0;
1309         
1310         /* Set the flags */
1311         for (chan=act->chanbase.first; chan; chan=chan->next){
1312                 set_ipo_key_selection(chan->ipo, sel);
1313                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
1314                         set_ipo_key_selection(conchan->ipo, sel);
1315         }
1316 }
1317
1318 void deselect_meshchannel_keys (Key *key, int test)
1319 {
1320         /* should deselect the rvk keys
1321          */
1322     int         sel=1;
1323
1324     /* Determine if this is selection or deselection */
1325     if (test){
1326         if (is_ipo_key_selected(key->ipo)){
1327             sel = 0;
1328         }
1329     }
1330     else {
1331         sel=0;
1332     }
1333         
1334     /* Set the flags */
1335     set_ipo_key_selection(key->ipo, sel);
1336 }
1337
1338 /* apparently within active object context */
1339 void deselect_actionchannels (bAction *act, int test)
1340 {
1341         bActionChannel *chan;
1342         bConstraintChannel *conchan;
1343         int                     sel=1;  
1344
1345         if (!act)
1346                 return;
1347
1348         /* See if we should be selecting or deselecting */
1349         if (test){
1350                 for (chan=act->chanbase.first; chan; chan=chan->next){
1351                         if (!sel)
1352                                 break;
1353
1354                         if (chan->flag & ACHAN_SELECTED){
1355                                 sel=0;
1356                                 break;
1357                         }
1358                         if (sel){
1359                                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
1360                                         if (conchan->flag & CONSTRAINT_CHANNEL_SELECT){
1361                                                 sel=0;
1362                                                 break;
1363                                         }
1364                                 }
1365                         }
1366                 }
1367         }
1368         else
1369                 sel=0;
1370
1371         /* Now set the flags */
1372         for (chan=act->chanbase.first; chan; chan=chan->next){
1373                 select_poseelement_by_name(chan->name, sel);
1374
1375                 if (sel)
1376                         chan->flag |= ACHAN_SELECTED;
1377                 else
1378                         chan->flag &= ~ACHAN_SELECTED;
1379
1380                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
1381                         if (sel)
1382                                 conchan->flag |= CONSTRAINT_CHANNEL_SELECT;
1383                         else
1384                                 conchan->flag &= ~CONSTRAINT_CHANNEL_SELECT;
1385                 }
1386         }
1387
1388 }
1389
1390 static void hilight_channel (bAction *act, bActionChannel *chan, short select)
1391 {
1392         bActionChannel *curchan;
1393
1394         if (!act)
1395                 return;
1396
1397         for (curchan=act->chanbase.first; curchan; curchan=curchan->next){
1398                 if (curchan==chan && select)
1399                         curchan->flag |= ACHAN_HILIGHTED;
1400                 else
1401                         curchan->flag &= ~ACHAN_HILIGHTED;
1402         }
1403 }
1404
1405 /* select_mode = SELECT_REPLACE
1406  *             = SELECT_ADD
1407  *             = SELECT_SUBTRACT
1408  *             = SELECT_INVERT
1409  */
1410
1411 /* exported for outliner (ton) */
1412 /* apparently within active object context */
1413 int select_channel(bAction *act, bActionChannel *chan,
1414                           int selectmode) 
1415 {
1416         /* Select the channel based on the selection mode
1417          */
1418         int flag;
1419
1420         switch (selectmode) {
1421         case SELECT_ADD:
1422                 chan->flag |= ACHAN_SELECTED;
1423                 break;
1424         case SELECT_SUBTRACT:
1425                 chan->flag &= ~ACHAN_SELECTED;
1426                 break;
1427         case SELECT_INVERT:
1428                 chan->flag ^= ACHAN_SELECTED;
1429                 break;
1430         }
1431         flag = (chan->flag & ACHAN_SELECTED) ? 1 : 0;
1432
1433         hilight_channel(act, chan, flag);
1434         select_poseelement_by_name(chan->name, flag);
1435
1436         return flag;
1437 }
1438
1439 static int select_constraint_channel(bAction *act, 
1440                                      bConstraintChannel *conchan, 
1441                                      int selectmode) {
1442         /* Select the constraint channel based on the selection mode
1443          */
1444         int flag;
1445
1446         switch (selectmode) {
1447         case SELECT_ADD:
1448                 conchan->flag |= CONSTRAINT_CHANNEL_SELECT;
1449                 break;
1450         case SELECT_SUBTRACT:
1451                 conchan->flag &= ~CONSTRAINT_CHANNEL_SELECT;
1452                 break;
1453         case SELECT_INVERT:
1454                 conchan->flag ^= CONSTRAINT_CHANNEL_SELECT;
1455                 break;
1456         }
1457         flag = (conchan->flag & CONSTRAINT_CHANNEL_SELECT) ? 1 : 0;
1458
1459         return flag;
1460 }
1461
1462
1463 static void mouse_actionchannels(bAction *act, short *mval,
1464                                  short *mvalo, int selectmode) {
1465         /* Select action channels, based on mouse values.
1466          * If mvalo is NULL we assume it is a one click
1467          * action, other wise we treat it like it is a
1468          * border select with mval[0],mval[1] and
1469          * mvalo[0], mvalo[1] forming the corners of
1470          * a rectangle.
1471          */
1472         bActionChannel *chan;
1473         float   click, x,y;
1474         int   clickmin, clickmax;
1475         int             wsize, sel;
1476         bConstraintChannel *conchan;
1477
1478         if (!act)
1479                 return;
1480   
1481         if (selectmode == SELECT_REPLACE) {
1482                 deselect_actionchannels (act, 0);
1483                 selectmode = SELECT_ADD;
1484         }
1485
1486         /* wsize is the greatest possible height (in pixels) that would be
1487          * needed to draw all of the action channels and constraint
1488          * channels.
1489          */
1490         wsize =  count_action_levels(act)*(CHANNELHEIGHT+CHANNELSKIP);
1491         wsize += CHANNELHEIGHT/2;
1492
1493     areamouseco_to_ipoco(G.v2d, mval, &x, &y);
1494     clickmin = (int) ((wsize - y) / (CHANNELHEIGHT+CHANNELSKIP));
1495         
1496         /* Only one click */
1497         if (mvalo == NULL) {
1498                 clickmax = clickmin;
1499         }
1500         /* Two click values (i.e., border select */
1501         else {
1502                 areamouseco_to_ipoco(G.v2d, mvalo, &x, &y);
1503                 click = ((wsize - y) / (CHANNELHEIGHT+CHANNELSKIP));
1504
1505                 if ( ((int) click) < clickmin) {
1506                         clickmax = clickmin;
1507                         clickmin = (int) click;
1508                 }
1509                 else {
1510                         clickmax = (int) click;
1511                 }
1512         }
1513
1514         if (clickmax < 0) {
1515                 return;
1516         }
1517
1518         /* clickmin and clickmax now coorespond to indices into
1519          * the collection of channels and constraint channels.
1520          * What we need to do is apply the selection mode on all
1521          * channels and constraint channels between these indices.
1522          * This is done by traversing the channels and constraint
1523          * channels, for each item decrementing clickmin and clickmax.
1524          * When clickmin is less than zero we start selecting stuff,
1525          * until clickmax is less than zero or we run out of channels
1526          * and constraint channels.
1527          */
1528
1529         for (chan = act->chanbase.first; chan; chan=chan->next){
1530                 if (clickmax < 0) break;
1531
1532                 if ( clickmin <= 0) {
1533                         /* Select the channel with the given mode. If the
1534                          * channel is freshly selected then set it to the
1535                          * active channel for the action
1536                          */
1537                         sel = (chan->flag & ACHAN_SELECTED);
1538                         if ( select_channel(act, chan, selectmode) && !sel ) {
1539                                 act->achan = chan;
1540                         }
1541                 }
1542                 --clickmin;
1543                 --clickmax;
1544
1545                 /* Check for click in a constraint */
1546                 for (conchan=chan->constraintChannels.first; 
1547                          conchan; conchan=conchan->next){
1548                         if (clickmax < 0) break;
1549                         if ( clickmin <= 0) {
1550                                 select_constraint_channel(act, conchan, selectmode);
1551                         }
1552                         --clickmin;
1553                         --clickmax;
1554                 }
1555         }
1556
1557         allqueue (REDRAWIPO, 0);
1558         allqueue (REDRAWVIEW3D, 0);
1559         allqueue (REDRAWACTION, 0);
1560         allqueue (REDRAWNLA, 0);
1561 }
1562
1563 void delete_meshchannel_keys(Key *key)
1564 {
1565         if (!okee("Erase selected keys"))
1566                 return;
1567
1568         BIF_undo_push("Delete Action keys");
1569         delete_ipo_keys(key->ipo);
1570
1571         meshkey_do_redraw(key);
1572 }
1573
1574 void delete_actionchannel_keys(void)
1575 {
1576         bAction *act;
1577         bActionChannel *chan;
1578         bConstraintChannel *conchan;
1579
1580         act = G.saction->action;
1581         if (!act)
1582                 return;
1583
1584         if (!okee("Erase selected keys"))
1585                 return;
1586
1587         for (chan = act->chanbase.first; chan; chan=chan->next){
1588
1589                 /* Check action channel keys*/
1590                 delete_ipo_keys(chan->ipo);
1591
1592                 /* Delete constraint channel keys */
1593                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
1594                         delete_ipo_keys(conchan->ipo);
1595         }
1596
1597         remake_action_ipos (act);
1598         BIF_undo_push("Delete Action keys");
1599         allspace(REMAKEIPO, 0);
1600         allqueue(REDRAWACTION, 0);
1601         allqueue(REDRAWIPO, 0);
1602         allqueue(REDRAWNLA, 0);
1603
1604 }
1605 static void delete_actionchannels (void)
1606 {
1607         bConstraintChannel *conchan=NULL, *nextconchan;
1608         bActionChannel *chan, *next;
1609         bAction *act;
1610         int freechan;
1611
1612         act=G.saction->action;
1613
1614         if (!act)
1615                 return;
1616
1617         for (chan=act->chanbase.first; chan; chan=chan->next){
1618                 if (chan->flag & ACHAN_SELECTED)
1619                         break;
1620                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
1621                 {
1622                         if (conchan->flag & CONSTRAINT_CHANNEL_SELECT){
1623                                 chan=act->chanbase.last;
1624                                 break;
1625                         }
1626                 }
1627         }
1628
1629         if (!chan && !conchan)
1630                 return;
1631
1632         if (!okee("Erase selected channels"))
1633                 return;
1634
1635         for (chan=act->chanbase.first; chan; chan=next){
1636                 freechan = 0;
1637                 next=chan->next;
1638                 
1639                 /* Remove action channels */
1640                 if (chan->flag & ACHAN_SELECTED){
1641                         if (chan->ipo)
1642                                 chan->ipo->id.us--;     /* Release the ipo */
1643                         freechan = 1;
1644                 }
1645                 
1646                 /* Remove constraint channels */
1647                 for (conchan=chan->constraintChannels.first; conchan; conchan=nextconchan){
1648                         nextconchan=conchan->next;
1649                         if (freechan || conchan->flag & CONSTRAINT_CHANNEL_SELECT){
1650                                 if (conchan->ipo)
1651                                         conchan->ipo->id.us--;
1652                                 BLI_freelinkN(&chan->constraintChannels, conchan);
1653                         }
1654                 }
1655                 
1656                 if (freechan)
1657                         BLI_freelinkN (&act->chanbase, chan);
1658
1659         }
1660
1661         BIF_undo_push("Delete Action channels");
1662         allqueue (REDRAWACTION, 0);
1663         allqueue(REDRAWNLA, 0);
1664
1665 }
1666
1667 void sethandles_meshchannel_keys(int code, Key *key)
1668 {
1669     sethandles_ipo_keys(key->ipo, code);
1670
1671         BIF_undo_push("Set handles Action keys");
1672         meshkey_do_redraw(key);
1673 }
1674
1675 void sethandles_actionchannel_keys(int code)
1676 {
1677         bAction *act;
1678         bActionChannel *chan;
1679
1680         /* Get the selected action, exit if none are selected 
1681          */
1682         act = G.saction->action;
1683         if (!act)
1684                 return;
1685
1686         /* Loop through the channels and set the beziers
1687          * of the selected keys based on the integer code
1688          */
1689         for (chan = act->chanbase.first; chan; chan=chan->next){
1690                 sethandles_ipo_keys(chan->ipo, code);
1691         }
1692
1693         /* Clean up and redraw stuff
1694          */
1695         remake_action_ipos (act);
1696         BIF_undo_push("Set handles Action channel");
1697         allspace(REMAKEIPO, 0);
1698         allqueue(REDRAWACTION, 0);
1699         allqueue(REDRAWIPO, 0);
1700         allqueue(REDRAWNLA, 0);
1701 }
1702
1703 void set_ipotype_actionchannels(int ipotype) {
1704
1705         bAction *act; 
1706         bActionChannel *chan;
1707         short event;
1708
1709         /* Get the selected action, exit if none are selected 
1710          */
1711         act = G.saction->action;
1712         if (!act)
1713                 return;
1714
1715         if (ipotype == SET_IPO_POPUP) {
1716                 /* Present a popup menu asking the user what type
1717                  * of IPO curve he/she/GreenBTH wants. ;)
1718                  */
1719                 event
1720                         =  pupmenu("Channel Ipo Type %t|"
1721                                            "Constant %x1|"
1722                                            "Linear %x2|"
1723                                            "Bezier %x3");
1724                 if(event < 1) return;
1725                 ipotype = event;
1726         }
1727         
1728         /* Loop through the channels and for the selected ones set
1729          * the type for each Ipo curve in the channel Ipo (based on
1730          * the value from the popup).
1731          */
1732         for (chan = act->chanbase.first; chan; chan=chan->next){
1733                 if (chan->flag & ACHAN_SELECTED){
1734                         if (chan->ipo)
1735                                 setipotype_ipo(chan->ipo, ipotype);
1736                 }
1737         }
1738
1739         /* Clean up and redraw stuff
1740          */
1741         remake_action_ipos (act);
1742         BIF_undo_push("Set Ipo type Action channel");
1743         allspace(REMAKEIPO, 0);
1744         allqueue(REDRAWACTION, 0);
1745         allqueue(REDRAWIPO, 0);
1746         allqueue(REDRAWNLA, 0);
1747 }
1748
1749 static void select_all_keys_frames(bAction *act, short *mval, 
1750                                                         short *mvalo, int selectmode) {
1751         
1752         /* This function tries to select all action keys in
1753          * every channel for a given range of keyframes that
1754          * are within the mouse values mval and mvalo (usually
1755          * the result of a border select). If mvalo is passed as
1756          * NULL then the selection is treated as a one-click and
1757          * the function tries to select all keys within half a
1758          * frame of the click point.
1759          */
1760         
1761         rcti rect;
1762         rctf rectf;
1763         bActionChannel *chan;
1764         bConstraintChannel *conchan;
1765
1766         if (!act)
1767                 return;
1768
1769         if (selectmode == SELECT_REPLACE) {
1770                 deselect_actionchannel_keys(act, 0);
1771                 selectmode = SELECT_ADD;
1772         }
1773
1774         if (mvalo == NULL) {
1775                 rect.xmin = rect.xmax = mval[0];
1776                 rect.ymin = rect.ymax = mval[1];
1777         }
1778         else {
1779                 if (mval[0] < mvalo[0] ) {
1780                         rect.xmin = mval[0];
1781                         rect.xmax = mvalo[0];
1782                 }
1783                 else {
1784                         rect.xmin = mvalo[0];
1785                         rect.xmax = mval[0];
1786                 }
1787                 if (mval[1] < mvalo[1] ) {
1788                         rect.ymin = mval[1];
1789                         rect.ymax = mvalo[1];
1790                 }
1791                 else {
1792                         rect.ymin = mvalo[1];
1793                         rect.ymax = mval[1];
1794                 }
1795         }
1796
1797         mval[0]= rect.xmin;
1798         mval[1]= rect.ymin+2;
1799         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
1800         mval[0]= rect.xmax;
1801         mval[1]= rect.ymax-2;
1802         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
1803
1804         if (mvalo == NULL) {
1805                 rectf.xmin = rectf.xmin - 0.5;
1806                 rectf.xmax = rectf.xmax + 0.5;
1807         }
1808     
1809         for (chan=act->chanbase.first; chan; chan=chan->next){
1810                 borderselect_ipo_key(chan->ipo, rectf.xmin, rectf.xmax,
1811                                                          selectmode);
1812                 for (conchan=chan->constraintChannels.first; conchan; 
1813                          conchan=conchan->next){
1814                         borderselect_ipo_key(conchan->ipo, rectf.xmin, rectf.xmax,
1815                                                                  selectmode);
1816                 }
1817         }       
1818
1819         allqueue(REDRAWNLA, 0);
1820         allqueue(REDRAWACTION, 0);
1821         allqueue(REDRAWIPO, 0);
1822 }
1823
1824
1825 static void select_all_keys_channels(bAction *act, short *mval, 
1826                               short *mvalo, int selectmode) {
1827         bActionChannel    *chan;
1828         float              click, x,y;
1829         int                clickmin, clickmax;
1830         int                wsize;
1831         bConstraintChannel *conchan;
1832
1833         /* This function selects all the action keys that
1834          * are in the mouse selection range defined by
1835          * the ordered pairs mval and mvalo (usually
1836          * these 2 are obtained from a border select).
1837          * If mvalo is NULL, then the selection is
1838          * treated like a one-click action, and at most
1839          * one channel is selected.
1840          */
1841
1842         /* If the action is null then abort
1843          */
1844         if (!act)
1845                 return;
1846
1847         if (selectmode == SELECT_REPLACE) {
1848                 deselect_actionchannel_keys(act, 0);
1849                 selectmode = SELECT_ADD;
1850         }
1851
1852         /* wsize is the greatest possible height (in pixels) that would be
1853          * needed to draw all of the action channels and constraint
1854          * channels.
1855          */
1856
1857         wsize =  count_action_levels(act)*(CHANNELHEIGHT+CHANNELSKIP);
1858         wsize += CHANNELHEIGHT/2;
1859         
1860     areamouseco_to_ipoco(G.v2d, mval, &x, &y);
1861     clickmin = (int) ((wsize - y) / (CHANNELHEIGHT+CHANNELSKIP));
1862         
1863         /* Only one click */
1864         if (mvalo == NULL) {
1865                 clickmax = clickmin;
1866         }
1867         /* Two click values (i.e., border select) */
1868         else {
1869
1870                 areamouseco_to_ipoco(G.v2d, mvalo, &x, &y);
1871                 click = ((wsize - y) / (CHANNELHEIGHT+CHANNELSKIP));
1872                 
1873                 if ( ((int) click) < clickmin) {
1874                         clickmax = clickmin;
1875                         clickmin = (int) click;
1876                 }
1877                 else {
1878                         clickmax = (int) click;
1879                 }
1880         }
1881
1882         if (clickmax < 0) {
1883                 return;
1884         }
1885
1886         for (chan = act->chanbase.first; chan; chan=chan->next){
1887                 if (clickmax < 0) break;
1888
1889                 if ( clickmin <= 0) {
1890                         /* Select the channel with the given mode. If the
1891                          * channel is freshly selected then set it to the
1892                          * active channel for the action
1893                          */
1894                         select_ipo_bezier_keys(chan->ipo, selectmode);
1895                 }
1896                 --clickmin;
1897                 --clickmax;
1898
1899                 /* Check for click in a constraint */
1900                 for (conchan=chan->constraintChannels.first; 
1901                          conchan; conchan=conchan->next){
1902                         if (clickmax < 0) break;
1903                         if ( clickmin <= 0) {
1904                                 select_ipo_bezier_keys(chan->ipo, selectmode);
1905                         }
1906                         --clickmin;
1907                         --clickmax;
1908                 }
1909         }
1910   
1911         allqueue (REDRAWIPO, 0);
1912         allqueue (REDRAWVIEW3D, 0);
1913         allqueue (REDRAWACTION, 0);
1914         allqueue (REDRAWNLA, 0);
1915   
1916 }
1917
1918 static void borderselect_function(void (*select_func)(bAction *act, 
1919                                                      short *mval, 
1920                                                      short *mvalo, 
1921                                                      int selectmode)) {
1922         /* This function executes an arbitrary selection
1923          * function as part of a border select. This
1924          * way the same function that is used for
1925          * right click selection points can generally
1926          * be used as the argument to this function
1927          */
1928         rcti rect;
1929         short   mval[2], mvalo[2];
1930         bAction *act;
1931         int val;                
1932
1933         /* Get the selected action, exit if none are selected 
1934          */
1935         act=G.saction->action;
1936         if (!act)
1937                 return;
1938
1939         /* Let the user draw a border (or abort)
1940          */
1941         if ( (val=get_border (&rect, 3)) ) {
1942                 mval[0]= rect.xmin;
1943                 mval[1]= rect.ymin+2;
1944                 mvalo[0]= rect.xmax;
1945                 mvalo[1]= rect.ymax-2;
1946
1947                 /* if the left mouse was used, do an additive
1948                  * selection with the user defined selection
1949                  * function.
1950                  */
1951                 if (val == LEFTMOUSE)
1952                         select_func(act, mval, mvalo, SELECT_ADD);
1953                 
1954                 /* if the right mouse was used, do a subtractive
1955                  * selection with the user defined selection
1956                  * function.
1957                  */
1958                 else if (val == RIGHTMOUSE)
1959                         select_func(act, mval, mvalo, SELECT_SUBTRACT);
1960         }
1961         BIF_undo_push("Border select Action");
1962         
1963 }
1964
1965 static void clever_keyblock_names(Key *key, short* mval){
1966     int        but=0, i, keynum;
1967     char       str[64];
1968         float      x;
1969         KeyBlock   *kb;
1970         /* get the keynum cooresponding to the y value
1971          * of the mouse pointer, return if this is
1972          * an invalid key number (and we don't deal
1973          * with the speed ipo).
1974          */
1975
1976     keynum = get_nearest_key_num(key, mval, &x);
1977     if ( (keynum < 1) || (keynum >= key->totkey) )
1978         return;
1979
1980         kb= key->block.first;
1981         for (i=0; i<keynum; ++i) kb = kb->next; 
1982
1983         if (kb->name[0] == '\0') {
1984                 sprintf(str, "Key %d", keynum);
1985         }
1986         else {
1987                 strcpy(str, kb->name);
1988         }
1989
1990         if ( (kb->slidermin >= kb->slidermax) ) {
1991                 kb->slidermin = 0.0;
1992                 kb->slidermax = 1.0;
1993         }
1994
1995     add_numbut(but++, TEX, "KB: ", 0, 24, str, 
1996                "Does this really need a tool tip?");
1997         add_numbut(but++, NUM|FLO, "Slider Min:", 
1998                            -10000, kb->slidermax, &kb->slidermin, 0);
1999         add_numbut(but++, NUM|FLO, "Slider Max:", 
2000                            kb->slidermin, 10000, &kb->slidermax, 0);
2001
2002     if (do_clever_numbuts(str, but, REDRAW)) {
2003                 strcpy(kb->name, str);
2004         allqueue (REDRAWACTION, 0);
2005                 allspace(REMAKEIPO, 0);
2006         allqueue (REDRAWIPO, 0);
2007         }
2008
2009         
2010 }
2011
2012 static void numbuts_action(void)
2013 {
2014         /* now called from action window event loop, plus reacts on mouseclick */
2015         /* removed Hos grunts for that reason! :) (ton) */
2016     Key *key;
2017     short mval[2];
2018
2019     if ( (key = get_action_mesh_key()) ) {
2020         getmouseco_areawin (mval);
2021                 if (mval[0]<NAMEWIDTH) {
2022                         clever_keyblock_names(key, mval);
2023                 }
2024     }
2025 }
2026
2027 void winqreadactionspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
2028 {
2029         extern void do_actionbuts(unsigned short event); // drawaction.c
2030         SpaceAction *saction;
2031         bAction *act;
2032         Key *key;
2033         float dx,dy;
2034         int doredraw= 0;
2035         int     cfra;
2036         short   mval[2];
2037         unsigned short event= evt->event;
2038         short val= evt->val;
2039         short mousebut = L_MOUSE;
2040
2041         if(curarea->win==0) return;
2042
2043         saction= curarea->spacedata.first;
2044         if (!saction)
2045                 return;
2046
2047         act=saction->action;
2048         if(val) {
2049                 
2050                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
2051                 
2052                 /* swap mouse buttons based on user preference */
2053                 if (U.flag & USER_LMOUSESELECT) {
2054                         if (event == LEFTMOUSE) {
2055                                 event = RIGHTMOUSE;
2056                                 mousebut = L_MOUSE;
2057                         } else if (event == RIGHTMOUSE) {
2058                                 event = LEFTMOUSE;
2059                                 mousebut = R_MOUSE;
2060                         }
2061                 }
2062                 
2063                 getmouseco_areawin(mval);
2064
2065                 key = get_action_mesh_key();
2066
2067                 switch(event) {
2068                 case UI_BUT_EVENT:
2069                         do_actionbuts(val);     // window itself
2070                         break;
2071                 
2072                 case HOMEKEY:
2073                         do_action_buttons(B_ACTHOME);   // header
2074                         break;
2075
2076                 case AKEY:
2077                         if (key) {
2078                                 if (mval[0]<ACTWIDTH){
2079                                         /* to do ??? */
2080                                 }
2081                                 else{
2082                                         deselect_meshchannel_keys(key, 1);
2083                                         allqueue (REDRAWACTION, 0);
2084                                         allqueue(REDRAWNLA, 0);
2085                                         allqueue (REDRAWIPO, 0);
2086                                 }
2087                         }
2088                         else {
2089                                 if (mval[0]<NAMEWIDTH){
2090                                         deselect_actionchannels (act, 1);
2091                                         allqueue (REDRAWVIEW3D, 0);
2092                                         allqueue (REDRAWACTION, 0);
2093                                         allqueue(REDRAWNLA, 0);
2094                                         allqueue (REDRAWIPO, 0);
2095                                 }
2096                                 else if (mval[0]>ACTWIDTH){
2097                                         deselect_actionchannel_keys (act, 1);
2098                                         allqueue (REDRAWACTION, 0);
2099                                         allqueue(REDRAWNLA, 0);
2100                                         allqueue (REDRAWIPO, 0);
2101                                 }
2102                         }
2103                         break;
2104
2105                 case BKEY:
2106                         if (key) {
2107                                 if (mval[0]<ACTWIDTH){
2108                                         /* to do?? */
2109                                 }
2110                                 else {
2111                                         borderselect_mesh(key);
2112                                 }
2113                         }
2114                         else {
2115
2116                                 /* If the border select is initiated in the
2117                                  * part of the action window where the channel
2118                                  * names reside, then select the channels
2119                                  */
2120                                 if (mval[0]<NAMEWIDTH){
2121                                         borderselect_function(mouse_actionchannels);
2122                                         BIF_undo_push("Select Action");
2123                                 }
2124                                 else if (mval[0]>ACTWIDTH){
2125
2126                                         /* If the border select is initiated in the
2127                                          * vertical scrollbar, then (de)select all keys
2128                                          * for the channels in the selection region
2129                                          */
2130                                         if (IN_2D_VERT_SCROLL(mval)) {
2131                                                 borderselect_function(select_all_keys_channels);
2132                                         }
2133
2134                                         /* If the border select is initiated in the
2135                                          * horizontal scrollbar, then (de)select all keys
2136                                          * for the keyframes in the selection region
2137                                          */
2138                                         else if (IN_2D_HORIZ_SCROLL(mval)) {
2139                                                 borderselect_function(select_all_keys_frames);
2140                                         }
2141
2142                                         /* Other wise, select the action keys
2143                                          */
2144                                         else {
2145                                                 borderselect_action();
2146                                         }
2147                                 }
2148                         }
2149                         break;
2150
2151                 case CKEY:
2152                         /* scroll the window so the current
2153                          * frame is in the center.
2154                          */
2155                         center_currframe();
2156                         break;
2157
2158                 case DKEY:
2159                         if (key) {
2160                                 if (G.qual & LR_SHIFTKEY && mval[0]>ACTWIDTH) {
2161                                         duplicate_meshchannel_keys(key);
2162                                 }
2163                         }
2164                         else if (act) {
2165                                 if (G.qual & LR_SHIFTKEY && mval[0]>ACTWIDTH){
2166                                         duplicate_actionchannel_keys();
2167                                         remake_action_ipos(act);
2168                                 }
2169                         }
2170                         break;
2171
2172                 case GKEY:
2173                         if (mval[0]>=ACTWIDTH) {
2174                                 if (key) {
2175                                         transform_meshchannel_keys('g', key);
2176                                 }
2177                                 else if (act) {
2178                                         transform_actionchannel_keys ('g');
2179                                 }
2180                         }
2181                         break;
2182
2183                 case HKEY:
2184                         if (key) {
2185                                 if(G.qual & LR_SHIFTKEY) {
2186                                         sethandles_meshchannel_keys(HD_AUTO, key);
2187                                 }
2188                                 else { 
2189                                         sethandles_meshchannel_keys(HD_ALIGN, key);
2190                                 }
2191                         }
2192                         else {
2193                                 if(G.qual & LR_SHIFTKEY) {
2194                                         sethandles_actionchannel_keys(HD_AUTO);
2195                                 }
2196                                 else { 
2197                                         sethandles_actionchannel_keys(HD_ALIGN);
2198                                 }
2199                         }
2200                         break;
2201                 
2202                 case NKEY:
2203                         if(G.qual==0) {
2204                                 numbuts_action();
2205                                 
2206                                 /* no panel (yet). current numbuts are not easy to put in panel... */
2207                                 //add_blockhandler(curarea, ACTION_HANDLER_PROPERTIES, UI_PNL_TO_MOUSE);
2208                                 //scrarea_queue_winredraw(curarea);
2209                         }
2210                         break;
2211                         
2212                 case SKEY: 
2213                         if (mval[0]>=ACTWIDTH) {
2214                                 if (key) {
2215                                         transform_meshchannel_keys('s', key);
2216                                 }
2217                                 else if (act) {
2218                                         transform_actionchannel_keys ('s');
2219                                 }
2220                         }
2221                         break;
2222
2223                         /*** set the Ipo type  ***/
2224                 case TKEY:
2225                         if (key) {
2226                                 /* to do */
2227                         }
2228                         else {
2229                                 set_ipotype_actionchannels(SET_IPO_POPUP);
2230                         }
2231                         break;
2232
2233                 case VKEY:
2234                         if (key) {
2235                                 sethandles_meshchannel_keys(HD_VECT, key);
2236                                 /* to do */
2237                         }
2238                         else {
2239                                 sethandles_actionchannel_keys(HD_VECT);
2240                         }
2241                         break;
2242
2243                 case PAGEUPKEY:
2244                         if (key) {
2245                                 /* to do */
2246                         }
2247                         else {
2248                                 if(G.qual & LR_SHIFTKEY) {
2249                                         top_sel_action();
2250                                 }
2251                                 else
2252                                 {
2253                                         up_sel_action();
2254                                 }
2255                                 
2256                         }
2257                         break;
2258                 case PAGEDOWNKEY:
2259                         if (key) {
2260                                 /* to do */
2261                         }
2262                         else {
2263                                 if(G.qual & LR_SHIFTKEY) {
2264                                         bottom_sel_action();
2265                                 }
2266                                 else
2267                                 down_sel_action();
2268                                 
2269                         }
2270                         break;
2271                 case DELKEY:
2272                 case XKEY:
2273                         if (key) {
2274                                 delete_meshchannel_keys(key);
2275                         }
2276                         else {
2277                                 if (mval[0]<NAMEWIDTH)
2278                                         delete_actionchannels ();
2279                                 else
2280                                         delete_actionchannel_keys ();
2281                         }
2282                         break;
2283                 /* LEFTMOUSE and RIGHTMOUSE event codes can be swapped above,
2284                  * based on user preference USER_LMOUSESELECT
2285                  */
2286                 case LEFTMOUSE:
2287                         if(view2dmove(LEFTMOUSE)); // only checks for sliders
2288                         else if (mval[0]>ACTWIDTH){
2289                                 do {
2290                                         getmouseco_areawin(mval);
2291                                         
2292                                         areamouseco_to_ipoco(G.v2d, mval, &dx, &dy);
2293                                         
2294                                         cfra= (int)dx;
2295                                         if(cfra< 1) cfra= 1;
2296                                         
2297                                         if( cfra!=CFRA ) {
2298                                                 CFRA= cfra;
2299                                                 update_for_newframe();
2300                                                 force_draw_all(0);                                      }
2301                                         else PIL_sleep_ms(30);
2302                                         
2303                                 } while(get_mbut() & mousebut);
2304                         }
2305                         break;
2306                 case RIGHTMOUSE:
2307                         /* Clicking in the channel area selects the
2308                          * channel or constraint channel
2309                          */
2310                         if (mval[0]<NAMEWIDTH) {
2311                                 if(act) {
2312                                         if(G.qual & LR_SHIFTKEY)
2313                                                 mouse_actionchannels(act, mval, NULL,  SELECT_INVERT);
2314                                         else
2315                                                 mouse_actionchannels(act, mval, NULL,  SELECT_REPLACE);
2316                                         
2317                                         BIF_undo_push("Select Action");
2318                                 }
2319                                 else numbuts_action();
2320                         }
2321                         else if (mval[0]>ACTWIDTH) {
2322                 
2323                                 /* Clicking in the vertical scrollbar selects
2324                                  * all of the keys for that channel at that height
2325                                  */
2326                                 if (IN_2D_VERT_SCROLL(mval)) {
2327                                         if(G.qual & LR_SHIFTKEY)
2328                                                 select_all_keys_channels(act, mval, NULL, 
2329                                                                                                  SELECT_INVERT);
2330                                         else
2331                                                 select_all_keys_channels(act, mval, NULL, 
2332                                                                                                  SELECT_REPLACE);
2333                                 }
2334                 
2335                                 /* Clicking in the horizontal scrollbar selects
2336                                  * all of the keys within 0.5 of the nearest integer
2337                                  * frame
2338                                  */
2339                                 else if (IN_2D_HORIZ_SCROLL(mval)) {
2340                                         if(G.qual & LR_SHIFTKEY)
2341                                                 select_all_keys_frames(act, mval, NULL, 
2342                                                                                            SELECT_INVERT);
2343                                         else
2344                                                 select_all_keys_frames(act, mval, NULL, 
2345                                                                                            SELECT_REPLACE);
2346                                         BIF_undo_push("Select all Action");
2347                                 }
2348                                 
2349                                 /* Clicking in the main area of the action window
2350                                  * selects keys
2351                                  */
2352                                 else {
2353                                         if (key) {
2354                                                 if(G.qual & LR_SHIFTKEY)
2355                                                         mouse_mesh_action(SELECT_INVERT, key);
2356                                                 else
2357                                                         mouse_mesh_action(SELECT_REPLACE, key);
2358                                         }
2359                                         else {
2360                                                 if(G.qual & LR_SHIFTKEY)
2361                                                         mouse_action(SELECT_INVERT);
2362                                                 else
2363                                                         mouse_action(SELECT_REPLACE);
2364                                         }
2365                                 }
2366                         }
2367                         break;
2368                 case PADPLUSKEY:
2369                         view2d_zoom(G.v2d, 0.1154, sa->winx, sa->winy);
2370                         test_view2d(G.v2d, sa->winx, sa->winy);
2371                         doredraw= 1;
2372                         break;
2373                 case PADMINUS:
2374                         view2d_zoom(G.v2d, -0.15, sa->winx, sa->winy);
2375                         test_view2d(G.v2d, sa->winx, sa->winy);
2376                         doredraw= 1;
2377                         break;
2378                 case MIDDLEMOUSE:
2379                 case WHEELUPMOUSE:
2380                 case WHEELDOWNMOUSE:
2381                         view2dmove(event);      /* in drawipo.c */
2382                         break;
2383                 }
2384         }
2385
2386         if(doredraw) addqueue(curarea->win, REDRAW, 1);
2387         
2388 }
2389
2390 Key *get_action_mesh_key(void) 
2391 {
2392         /* gets the key data from the currently selected
2393          * mesh/lattice. If a mesh is not selected, or does not have
2394          * key data, then we return NULL (currently only
2395          * returns key data for RVK type meshes). If there
2396          * is an action that is pinned, return null
2397          */
2398     Object *ob;
2399     Key    *key;
2400
2401     ob = OBACT;
2402     if (!ob) return NULL;
2403
2404         if (G.saction->pin) return NULL;
2405
2406     if (ob->type==OB_MESH ) {
2407                 key = ((Mesh *)ob->data)->key;
2408     }
2409         else if (ob->type==OB_LATTICE ) {
2410                 key = ((Lattice *)ob->data)->key;
2411         }
2412         else return NULL;
2413
2414         if (key) {
2415                 if (key->type == KEY_RELATIVE)
2416                         return key;
2417         }
2418
2419     return NULL;
2420 }
2421
2422 int get_nearest_key_num(Key *key, short *mval, float *x) {
2423         /* returns the key num that cooresponds to the
2424          * y value of the mouse click. Does not check
2425          * if this is a valid keynum. Also gives the Ipo
2426          * x coordinate.
2427          */
2428     int num;
2429     float ybase, y;
2430
2431     areamouseco_to_ipoco(G.v2d, mval, x, &y);
2432
2433     ybase = key->totkey * (CHANNELHEIGHT + CHANNELSKIP);
2434     num = (int) ((ybase - y + CHANNELHEIGHT/2) / (CHANNELHEIGHT+CHANNELSKIP));
2435
2436     return (num + 1);
2437 }
2438
2439
2440
2441 void top_sel_action()
2442 {
2443         bAction *act;
2444         bActionChannel *chan;
2445         
2446         /* Get the selected action, exit if none are selected */
2447         act = G.saction->action;
2448         if (!act) return;
2449         
2450         for (chan=act->chanbase.first; chan; chan=chan->next){
2451                 if ((chan->flag & ACHAN_SELECTED) && !(chan->flag & ACHAN_MOVED)){
2452                         /* take it out off the chain keep data */
2453                         BLI_remlink (&act->chanbase, chan);
2454                         /* make it first element */
2455                         BLI_insertlinkbefore(&act->chanbase,act->chanbase.first, chan);
2456                         chan->flag |= ACHAN_MOVED;
2457                         /* restart with rest of list */
2458                         chan=chan->next;
2459                 }
2460         }
2461     /* clear temp flags */
2462         for (chan=act->chanbase.first; chan; chan=chan->next){
2463                 chan->flag = chan->flag & ~ACHAN_MOVED;
2464         }
2465         
2466         /* Clean up and redraw stuff */
2467         remake_action_ipos (act);
2468         BIF_undo_push("Top Action channel");
2469         allspace(REMAKEIPO, 0);
2470         allqueue(REDRAWACTION, 0);
2471         allqueue(REDRAWIPO, 0);
2472         allqueue(REDRAWNLA, 0);
2473 }
2474
2475 void up_sel_action()
2476 {
2477         bAction *act;
2478         bActionChannel *chan;
2479         bActionChannel *prev;
2480         
2481         /* Get the selected action, exit if none are selected */
2482         act = G.saction->action;
2483         if (!act) return;
2484         
2485         for (chan=act->chanbase.first; chan; chan=chan->next){
2486                 if ((chan->flag & ACHAN_SELECTED) && !(chan->flag & ACHAN_MOVED)){
2487                         prev = chan->prev;
2488                         if (prev){
2489                                 /* take it out off the chain keep data */
2490                                 BLI_remlink (&act->chanbase, chan);
2491                                 /* push it up */
2492                                 BLI_insertlinkbefore(&act->chanbase,prev, chan);
2493                                 chan->flag |= ACHAN_MOVED;
2494                                 /* restart with rest of list */
2495                                 chan=chan->next;
2496                         }
2497                 }
2498         }
2499         /* clear temp flags */
2500         for (chan=act->chanbase.first; chan; chan=chan->next){
2501                 chan->flag = chan->flag & ~ACHAN_MOVED;
2502         }
2503         
2504         /* Clean up and redraw stuff
2505         */
2506         remake_action_ipos (act);
2507         BIF_undo_push("Up Action channel");
2508         allspace(REMAKEIPO, 0);
2509         allqueue(REDRAWACTION, 0);
2510         allqueue(REDRAWIPO, 0);
2511         allqueue(REDRAWNLA, 0);
2512 }
2513
2514 void down_sel_action()
2515 {
2516         bAction *act;
2517         bActionChannel *chan;
2518         bActionChannel *next;
2519         
2520         /* Get the selected action, exit if none are selected */
2521         act = G.saction->action;
2522         if (!act) return;
2523         
2524         for (chan=act->chanbase.last; chan; chan=chan->prev){
2525                 if ((chan->flag & ACHAN_SELECTED) && !(chan->flag & ACHAN_MOVED)){
2526                         next = chan->next;
2527                         if (next) next = next->next;
2528                         if (next){
2529                                 /* take it out off the chain keep data */
2530                                 BLI_remlink (&act->chanbase, chan);
2531                                 /* move it down */
2532                                 BLI_insertlinkbefore(&act->chanbase,next, chan);
2533                                 chan->flag |= ACHAN_MOVED;
2534                         }
2535                         else {
2536                                 /* take it out off the chain keep data */
2537                                 BLI_remlink (&act->chanbase, chan);
2538                                 /* add at end */
2539                                 BLI_addtail(&act->chanbase,chan);
2540                                 chan->flag |= ACHAN_MOVED;
2541                         }
2542
2543                 }
2544         }
2545         /* clear temp flags */
2546         for (chan=act->chanbase.first; chan; chan=chan->next){
2547                 chan->flag = chan->flag & ~ACHAN_MOVED;
2548         }
2549         
2550         /* Clean up and redraw stuff
2551         */
2552         remake_action_ipos (act);
2553         BIF_undo_push("Down Action channel");
2554         allspace(REMAKEIPO, 0);
2555         allqueue(REDRAWACTION, 0);
2556         allqueue(REDRAWIPO, 0);
2557         allqueue(REDRAWNLA, 0);
2558 }
2559
2560 void bottom_sel_action()
2561 {
2562         bAction *act;
2563         bActionChannel *chan;
2564         
2565         /* Get the selected action, exit if none are selected */
2566         act = G.saction->action;
2567         if (!act) return;
2568         
2569         for (chan=act->chanbase.last; chan; chan=chan->prev){
2570                 if ((chan->flag & ACHAN_SELECTED) && !(chan->flag & ACHAN_MOVED)) {
2571                         /* take it out off the chain keep data */
2572                         BLI_remlink (&act->chanbase, chan);
2573                         /* add at end */
2574                         BLI_addtail(&act->chanbase,chan);
2575                         chan->flag |= ACHAN_MOVED;
2576                 }
2577                 
2578         }
2579         /* clear temp flags */
2580         for (chan=act->chanbase.first; chan; chan=chan->next){
2581                 chan->flag = chan->flag & ~ACHAN_MOVED;
2582         }
2583         
2584         /* Clean up and redraw stuff
2585         */
2586         remake_action_ipos (act);
2587         BIF_undo_push("Bottom Action channel");
2588         allspace(REMAKEIPO, 0);
2589         allqueue(REDRAWACTION, 0);
2590         allqueue(REDRAWIPO, 0);
2591         allqueue(REDRAWNLA, 0);
2592 }