added prototypes for bottom_sel_action and friends.
[blender.git] / source / blender / src / editaction.c
index bcff58ae471492eb808370b526586142b4e0505d..e9081b0fbfced0f078261dd721e1d7c7fb4a2324 100644 (file)
 #include <string.h>
 #include <math.h>
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#ifdef WIN32
-#include "BLI_winstuff.h"
-#endif
-
 #include "MEM_guardedalloc.h"
 
 #include "PIL_time.h"
 #include "DNA_constraint_types.h"
 #include "DNA_key_types.h"
 #include "DNA_mesh_types.h"
+#include "DNA_nla_types.h"
 #include "DNA_lattice_types.h"
 
+#include "BKE_action.h"
 #include "BKE_armature.h"
 #include "BKE_constraint.h"
-#include "BKE_displist.h"
 #include "BKE_curve.h"
-#include "BKE_ipo.h"
+#include "BKE_depsgraph.h"
 #include "BKE_global.h"
+#include "BKE_ipo.h"
+#include "BKE_key.h"
 #include "BKE_library.h"
 #include "BKE_main.h"
-#include "BKE_action.h"
-#include "BKE_key.h"
+#include "BKE_utildefines.h"
 
+#include "BIF_butspace.h"
+#include "BIF_editaction.h"
+#include "BIF_editarmature.h"
+#include "BIF_editnla.h"
+#include "BIF_editview.h"
 #include "BIF_gl.h"
+#include "BIF_interface.h"
 #include "BIF_mywindow.h"
-#include "BIF_toolbox.h"
+#include "BIF_poseobject.h"
 #include "BIF_screen.h"
 #include "BIF_space.h"
-#include "BIF_butspace.h"
-#include "BIF_interface.h"
-#include "BIF_editview.h"
-#include "BIF_poseobject.h"
-#include "BIF_editarmature.h"
-#include "BIF_editaction.h"
+#include "BIF_toolbox.h"
 
 #include "BSE_edit.h"
 #include "BSE_drawipo.h"
 #include "BSE_headerbuttons.h"
 #include "BSE_editipo.h"
-#include "BSE_editaction.h"
 #include "BSE_trans_types.h"
-#include "BSE_editaction_types.h"
 
 #include "BDR_editobject.h"
 
 #include "blendef.h"
 #include "nla.h"
 
-static bPose   *g_posebuf=NULL;
 extern int count_action_levels (bAction *act);
+void top_sel_action();
+void up_sel_action();
+void bottom_sel_action();
+void down_sel_action();
 
 #define BEZSELECTED(bezt)   (((bezt)->f1 & 1) || ((bezt)->f2 & 1) || ((bezt)->f3 & 1))
 
-/* Local Function prototypes */
-
-static void insertactionkey(bAction *act, bActionChannel *achan, bPoseChannel *chan, int adrcode, short makecurve, float time);
-static void flip_name (char *name);
-static void mouse_actionchannels(bAction *act, short *mval, 
-                                 short *mvalo, int selectmode);
-static void mouse_action(int selectmode);
-static void mouse_mesh_action(int selectmode, Key *key);
-static bActionChannel *get_nearest_actionchannel_key (float *index, short *sel, bConstraintChannel **conchan);
-static void delete_actionchannels(void);
-static void select_poseelement_by_name (char *name, int select);
+/* Local Function prototypes, are forward needed */
 static void hilight_channel (bAction *act, bActionChannel *chan, short hilight);
-static void set_action_key_time (bAction *act, bPoseChannel *chan, int adrcode, short makecurve, float time);
+
 /* Implementation */
 
 short showsliders = 0;
 short ACTWIDTH = NAMEWIDTH;
 
+/* messy call... */
 static void select_poseelement_by_name (char *name, int select)
 {
-       /* Synchs selection of channels with selection of object elements in posemode */
-
-       Object *ob;
-
-       ob = G.obpose;
-       if (!ob)
+       /* Syncs selection of channels with selection of object elements in posemode */
+       Object *ob= OBACT;
+       bPoseChannel *pchan;
+       
+       if (!ob || ob->type!=OB_ARMATURE)
                return;
-
-       switch (ob->type){
-       case OB_ARMATURE:
-               select_bone_by_name ((bArmature*)ob->data, name, select);
-               break;
-       default:
-               break;
+       
+       if(select==2) {
+               for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next)
+                       pchan->bone->flag &= ~(BONE_ACTIVE);
+       }
+       
+       pchan= get_pose_channel(ob->pose, name);
+       if(pchan) {
+               if(select)
+                       pchan->bone->flag |= (BONE_SELECTED);
+               else 
+                       pchan->bone->flag &= ~(BONE_SELECTED);
+               if(select==2)
+                       pchan->bone->flag |= (BONE_ACTIVE);
        }
 }
-#ifdef __NLA_BAKE
+
 bAction* bake_action_with_client (bAction *act, Object *armob, float tolerance)
 {
+       bArmature               *arm;
        bAction                 *result=NULL;
        bActionChannel *achan;
-       float                   actlen;
+       bAction                 *temp;
+       bPoseChannel    *pchan;
+       ID                              *id;
+       float                   actstart, actend;
+       int                             oldframe;
        int                             curframe;
        char                    newname[64];
-       bArmature               *arm;
-       Bone                    *bone;
-       float                   oldframe;
-       bAction                 *temp;
-       bPoseChannel *pchan;
 
        if (!act)
                return NULL;
-
+       
        arm = get_armature(armob);
 
-       
        if (G.obedit){
                error ("Actions can't be baked in Edit Mode");
                return NULL;
        }
 
-       if (!arm){
+       if (!arm || armob->pose==NULL){
                error ("Select an armature before baking");
                return NULL;
        }
+       
        /* Get a new action */
-       result = add_empty_action();
+       result = add_empty_action(ID_PO);
+       id= (ID *)armob;
 
        /* Assign the new action a unique name */
        sprintf (newname, "%s.BAKED", act->id.name+2);
        rename_id(&result->id, newname);
 
-       actlen = calc_action_end(act);
+       calc_action_range(act, &actstart, &actend);
 
        oldframe = G.scene->r.cfra;
 
        temp = armob->action;
-       armob->action = act;
+       armob->action = result;
        
-       for (curframe=1; curframe<ceil(actlen+1); curframe++){
+       for (curframe=1; curframe<ceil(actend+1.0f); curframe++){
 
                /* Apply the old action */
                
                G.scene->r.cfra = curframe;
 
                /* Apply the object ipo */
-               get_pose_from_action(&armob->pose, act, curframe);
-               apply_pose_armature(arm, armob->pose, 1);
-               clear_object_constraint_status(armob);
-               where_is_armature_time(armob, curframe);
+               extract_pose_from_action(armob->pose, act, curframe);
+
+               where_is_pose(armob);
                
                /* For each channel: set quats and locs if channel is a bone */
                for (pchan=armob->pose->chanbase.first; pchan; pchan=pchan->next){
 
-                       bone = get_named_bone(arm, pchan->name);
-                       if (bone){
-                               float tmp_quat[4], tmp_loc[3], tmp_size[3];
-                               QUATCOPY(tmp_quat, pchan->quat);
-                               VECCOPY(tmp_loc, pchan->loc);
-                               VECCOPY(tmp_size, pchan->size);
-
-                               Mat4ToQuat(pchan->obmat, pchan->quat);
-                               Mat4ToSize(pchan->obmat, pchan->size);
-                               VECCOPY(pchan->loc, pchan->obmat[3]);
-
-                               /* Apply to keys */
-                               set_action_key_time (result, pchan, AC_QUAT_X, 1, curframe);
-                               set_action_key_time (result, pchan, AC_QUAT_Y, 1, curframe);
-                               set_action_key_time (result, pchan, AC_QUAT_Z, 1, curframe);
-                               set_action_key_time (result, pchan, AC_QUAT_W, 1, curframe);
-                               set_action_key_time (result, pchan, AC_LOC_X, 1, curframe);
-                               set_action_key_time (result, pchan, AC_LOC_Y, 1, curframe);
-                               set_action_key_time (result, pchan, AC_LOC_Z, 1, curframe);
-                               set_action_key_time (result, pchan, AC_SIZE_X, 1, curframe);
-                               set_action_key_time (result, pchan, AC_SIZE_Y, 1, curframe);
-                               set_action_key_time (result, pchan, AC_SIZE_Z, 1, curframe);
-
-                               QUATCOPY(pchan->quat, tmp_quat);
-                               VECCOPY(pchan->loc, tmp_loc);
-                               VECCOPY(pchan->size, tmp_size);
-                       }
+                       /* Apply to keys */
+                       insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_X);
+                       insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Y);
+                       insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Z);
+                       insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_X);
+                       insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Y);
+                       insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Z);
+                       insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_W);
+                       insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_X);
+                       insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Y);
+                       insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Z);
                }
        }
 
@@ -231,18 +208,28 @@ bAction* bake_action_with_client (bAction *act, Object *armob, float tolerance)
        /* Make another pass to ensure all keyframes are set to linear interpolation mode */
        for (achan = result->chanbase.first; achan; achan=achan->next){
                IpoCurve* icu;
-               for (icu = achan->ipo->curve.first; icu; icu=icu->next){
-                       icu->ipo= IPO_LIN;
+               if(achan->ipo) {
+                       for (icu = achan->ipo->curve.first; icu; icu=icu->next){
+                               icu->ipo= IPO_LIN;
+                       }
                }
        }
 
        notice ("Made a new action named \"%s\"", newname);
        G.scene->r.cfra = oldframe;
        armob->action = temp;
+               
+       /* restore */
+       extract_pose_from_action(armob->pose, act, G.scene->r.cfra);
+       where_is_pose(armob);
+       
+       allqueue(REDRAWACTION, 1);
+       
        return result;
 }
-#endif
 
+/* apparently within active object context */
+/* called extern, like on bone selection */
 void select_actionchannel_by_name (bAction *act, char *name, int select)
 {
        bActionChannel *chan;
@@ -252,7 +239,6 @@ void select_actionchannel_by_name (bAction *act, char *name, int select)
 
        for (chan = act->chanbase.first; chan; chan=chan->next){
                if (!strcmp (chan->name, name)){
-                       act->achan = chan;
                        if (select){
                                chan->flag |= ACHAN_SELECTED;
                                hilight_channel (act, chan, 1);
@@ -266,6 +252,7 @@ void select_actionchannel_by_name (bAction *act, char *name, int select)
        }
 }
 
+/* called on changing action ipos or keys */
 void remake_action_ipos(bAction *act)
 {
        bActionChannel *chan;
@@ -288,9 +275,11 @@ void remake_action_ipos(bAction *act)
                        }
                }
        }
+       
+       synchronize_action_strips();
 }
 
-void remake_meshaction_ipos(Ipo *ipo)
+static void remake_meshaction_ipos(Ipo *ipo)
 {
        /* this puts the bezier triples in proper
         * order and makes sure the bezier handles
@@ -307,9 +296,9 @@ void remake_meshaction_ipos(Ipo *ipo)
 static void meshkey_do_redraw(Key *key)
 {
        remake_meshaction_ipos(key->ipo);
-    do_all_ipos();
-    do_spec_key(key);
 
+       DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
+       
        allspace(REMAKEIPO, 0);
        allqueue(REDRAWACTION, 0);
        allqueue(REDRAWIPO, 0);
@@ -341,46 +330,58 @@ void duplicate_actionchannel_keys(void)
                        duplicate_ipo_keys(conchan->ipo);
        }
 
-       transform_actionchannel_keys ('g');
+       transform_actionchannel_keys ('g', 0);
 }
 
-static bActionChannel *get_nearest_actionchannel_key (float *index, short *sel, bConstraintChannel **rchan){
+static bActionChannel *get_nearest_actionchannel_key (float *index, short *sel, bConstraintChannel **rchan)
+{
        bAction *act;
        bActionChannel *chan;
        IpoCurve *icu;
        bActionChannel *firstchan=NULL;
        bConstraintChannel *conchan, *firstconchan=NULL;
-       int     foundsel=0;
+       rctf    rectf;
        float firstvert=-1, foundx=-1;
-               int i;
+       float ymin, ymax, xmin, xmax;
+       int i;
+       int     foundsel=0;
        short mval[2];
-       float ymin, ymax;
-       rctf    rectf;
+       
        *index=0;
 
        *rchan=NULL;
-       act=G.saction->action; /* We presume that we are only called during a valid action */
+       act= G.saction->action; /* We presume that we are only called during a valid action */
        
        getmouseco_areawin (mval);
 
        mval[0]-=7;
        areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
-
        mval[0]+=14;
        areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
 
-       ymax = count_action_levels (act) * (CHANNELHEIGHT + CHANNELSKIP);
-
+       ymax = count_action_levels(act) * (CHANNELHEIGHT + CHANNELSKIP);
+       ymax += CHANNELHEIGHT/2;
+       
+       /* if action is mapped in NLA, it returns a correction */
+       if(G.saction->pin==0 && OBACT) {
+               xmin= get_action_frame(OBACT, rectf.xmin);
+               xmax= get_action_frame(OBACT, rectf.xmax);
+       }
+       else {
+               xmin= rectf.xmin;
+               xmax= rectf.xmax;
+       }
+       
        *sel=0;
 
        for (chan=act->chanbase.first; chan; chan=chan->next){
 
                /* Check action channel */
-               ymin=ymax-(CHANNELHEIGHT+CHANNELSKIP);
-               if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
+               ymin= ymax-(CHANNELHEIGHT+CHANNELSKIP);
+               if (!((ymax < rectf.ymin) || (ymin > rectf.ymax)) && chan->ipo){
                        for (icu=chan->ipo->curve.first; icu; icu=icu->next){
                                for (i=0; i<icu->totvert; i++){
-                                       if (icu->bezt[i].vec[1][0] > rectf.xmin && icu->bezt[i].vec[1][0] <= rectf.xmax ){
+                                       if (icu->bezt[i].vec[1][0] > xmin && icu->bezt[i].vec[1][0] <= xmax ){
                                                if (!firstchan){
                                                        firstchan=chan;
                                                        firstvert=icu->bezt[i].vec[1][0];
@@ -407,10 +408,10 @@ static bActionChannel *get_nearest_actionchannel_key (float *index, short *sel,
                /* Check constraint channels */
                for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
                        ymin=ymax-(CHANNELHEIGHT+CHANNELSKIP);
-                       if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
+                       if (!((ymax < rectf.ymin) || (ymin > rectf.ymax)) && conchan->ipo) {
                                for (icu=conchan->ipo->curve.first; icu; icu=icu->next){
                                        for (i=0; i<icu->totvert; i++){
-                                               if (icu->bezt[i].vec[1][0] > rectf.xmin && icu->bezt[i].vec[1][0] <= rectf.xmax ){
+                                               if (icu->bezt[i].vec[1][0] > xmin && icu->bezt[i].vec[1][0] <= xmax ){
                                                        if (!firstchan){
                                                                firstchan=chan;
                                                                firstconchan=conchan;
@@ -472,6 +473,7 @@ static IpoCurve *get_nearest_meshchannel_key (float *index, short *sel)
     areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
 
     ybase = key->totkey * (CHANNELHEIGHT + CHANNELSKIP);
+       ybase += CHANNELHEIGHT/2;
     *sel=0;
 
     /* lets loop through the IpoCurves trying to find the closest
@@ -548,6 +550,7 @@ static IpoCurve *get_nearest_meshchannel_key (float *index, short *sel)
     return firsticu;
 }
 
+/* apparently within active object context */
 static void mouse_action(int selectmode)
 {
        bAction *act;
@@ -567,15 +570,14 @@ static void mouse_action(int selectmode)
 
        if (chan){
                if (selectmode == SELECT_REPLACE) {
-                       if (sel == 0)
-                               selectmode = SELECT_ADD;
-                       else
-                               selectmode = SELECT_SUBTRACT;
+                       selectmode = SELECT_ADD;
+                       
                        deselect_actionchannel_keys(act, 0);
                        deselect_actionchannels(act, 0);
-                       act->achan = chan;
+                       
                        chan->flag |= ACHAN_SELECTED;
                        hilight_channel (act, chan, 1);
+                       select_poseelement_by_name(chan->name, 2);      /* 2 is activate */
                }
                
                if (conchan)
@@ -583,11 +585,15 @@ static void mouse_action(int selectmode)
                else
                        select_ipo_key(chan->ipo, selx, selectmode);
 
+               
+               std_rmouse_transform(transform_actionchannel_keys);
+               
                allqueue(REDRAWIPO, 0);
                allqueue(REDRAWVIEW3D, 0);
                allqueue(REDRAWACTION, 0);
                allqueue(REDRAWNLA, 0);
-
+               allqueue(REDRAWOOPS, 0);
+               allqueue(REDRAWBUTSALL, 0);
        }
 }
 
@@ -640,6 +646,7 @@ static void mouse_mesh_action(int selectmode, Key *key)
                 */
                select_icu_key(icu, selx, selectmode);
 
+               BIF_undo_push("Select Action key");
         allqueue(REDRAWIPO, 0);
         allqueue(REDRAWVIEW3D, 0);
         allqueue(REDRAWACTION, 0);
@@ -678,13 +685,21 @@ void borderselect_action(void)
                mval[1]= rect.ymax-2;
                areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
                
-               ymax=count_action_levels(act) * (CHANNELHEIGHT+CHANNELSKIP);
+               /* if action is mapped in NLA, it returns a correction */
+               if(G.saction->pin==0 && OBACT) {
+                       rectf.xmin= get_action_frame(OBACT, rectf.xmin);
+                       rectf.xmax= get_action_frame(OBACT, rectf.xmax);
+               }
+               
+               ymax= count_action_levels(act) * (CHANNELHEIGHT+CHANNELSKIP);
+               ymax += CHANNELHEIGHT/2;
+               
                for (chan=act->chanbase.first; chan; chan=chan->next){
                        
                        /* Check action */
                        ymin=ymax-(CHANNELHEIGHT+CHANNELSKIP);
                        if (!((ymax < rectf.ymin) || (ymin > rectf.ymax)))
-          borderselect_ipo_key(chan->ipo, rectf.xmin, rectf.xmax,
+                               borderselect_ipo_key(chan->ipo, rectf.xmin, rectf.xmax,
                                selectmode);
 
                        ymax=ymin;
@@ -699,6 +714,7 @@ void borderselect_action(void)
                                ymax=ymin;
                        }
                }       
+               BIF_undo_push("Border Select Action");
                allqueue(REDRAWNLA, 0);
                allqueue(REDRAWACTION, 0);
                allqueue(REDRAWIPO, 0);
@@ -742,24 +758,43 @@ void borderselect_mesh(Key *key)
                /* Lets loop throug the IpoCurves and do borderselect
                 * on the curves with adrcodes in our selected range.
                 */
-               for (icu = key->ipo->curve.first; icu ; icu = icu->next) {
-                       /* lets not deal with the "speed" Ipo
-                        */
-                       if (!icu->adrcode) continue;
-                       if ( (icu->adrcode >= adrcodemin) && 
-                                (icu->adrcode <= adrcodemax) ) {
-                               borderselect_icu_key(icu, xmin, xmax, select_function);
+               if(key->ipo) {
+                       for (icu = key->ipo->curve.first; icu ; icu = icu->next) {
+                               /* lets not deal with the "speed" Ipo
+                                */
+                               if (!icu->adrcode) continue;
+                               if ( (icu->adrcode >= adrcodemin) && 
+                                        (icu->adrcode <= adrcodemax) ) {
+                                       borderselect_icu_key(icu, xmin, xmax, select_function);
+                               }
                        }
                }
-
                /* redraw stuff */
+               BIF_undo_push("Border select Action Key");
                allqueue(REDRAWNLA, 0);
                allqueue(REDRAWACTION, 0);
                allqueue(REDRAWIPO, 0);
        }
 }
 
+/* ******************** action API ***************** */
+
+/* generic get current action call, for action window context */
+bAction *ob_get_action(Object *ob)
+{
+       bActionStrip *strip;
+       
+       if(ob->action)
+               return ob->action;
+       
+       for (strip=ob->nlastrips.first; strip; strip=strip->next){
+               if (strip->flag & ACTSTRIP_SELECT)
+                       return strip->act;
+       }
+       return NULL;
+}
 
+/* used by ipo, outliner, buttons to find the active channel */
 bActionChannel* get_hilighted_action_channel(bAction* action)
 {
        bActionChannel *chan;
@@ -783,347 +818,40 @@ void set_exprap_action(int mode)
        error ("Not yet implemented!");
 }
 
-void free_posebuf(void) 
-{
-       if (g_posebuf){
-               clear_pose(g_posebuf);
-               MEM_freeN (g_posebuf);
-       }
-       g_posebuf=NULL;
-}
-
-void copy_posebuf (void)
-{
-       Object *ob;
-
-       free_posebuf();
-
-       ob=G.obpose;
-       if (!ob){
-               error ("Copy buffer is empty");
-               return;
-       }
-
-       filter_pose_keys();
-       copy_pose(&g_posebuf, ob->pose, 0);
-
-}
-
-static void flip_name (char *name)
-{
-
-       char    prefix[128]={""};       /* The part before the facing */
-       char    suffix[128]={""};       /* The part after the facing */
-       char    replace[128]={""};      /* The replacement string */
-
-       char    *index=NULL;
-       /* Find the last period */
-
-       strcpy (prefix, name);
-
-       /* Check for an instance of .Right */
-       if (!index){
-               index = strstr (prefix, "Right");
-               if (index){
-                       *index=0;
-                       strcpy (replace, "Left");
-                       strcpy (suffix, index+6);
-               }
-       }
-
-       /* Che ck for an instance of .RIGHT */
-       if (!index){
-               index = strstr (prefix, "RIGHT");
-               if (index){
-                       *index=0;
-                       strcpy (replace, "LEFT");
-                       strcpy (suffix, index+6);
-               }
-       }
-
-
-       /* Check for an instance of .right */
-       if (!index){
-               index = strstr (prefix, "right");
-               if (index){
-                       *index=0;
-                       strcpy (replace, "left");
-                       strcpy (suffix, index+6);
-               }
-       }
-
-       /* Check for an instance of .left */
-       if (!index){
-               index = strstr (prefix, "left");
-               if (index){
-                       *index=0;
-                       strcpy (replace, "right");
-                       strcpy (suffix, index+5);
-               }
-       }
-
-       /* Check for an instance of .LEFT */
-       if (!index){
-               index = strstr (prefix, "LEFT");
-               if (index){
-                       *index=0;
-                       strcpy (replace, "RIGHT");
-                       strcpy (suffix, index+5);
-               }
-       }
-
-       /* Check for an instance of .Left */
-       if (!index){
-               index = strstr (prefix, "Left");
-               if (index){
-                       *index=0;
-                       strcpy (replace, "Right");
-                       strcpy (suffix, index+5);
-               }
-       }
-
-       /* check for an instance of .L */
-       if (!index){
-               index = strstr (prefix, ".L");
-               if (index){
-                       *index=0;
-                       strcpy (replace, ".R");
-                       strcpy (suffix, index+2);
-               }
-       }
-
-       /* check for an instance of .l */
-       if (!index){
-               index = strstr (prefix, ".l");
-               if (index){
-                       *index=0;
-                       strcpy (replace, ".r");
-                       strcpy (suffix, index+2);
-               }
-       }
-
-       /* Checl for an instance of .R */
-       if (!index){
-               index = strstr (prefix, ".R");
-               if (index){
-                       *index=0;
-                       strcpy (replace, ".L");
-                       strcpy (suffix, index+2);
-               }
-       }
-
-       /* Checl for an instance of .r */
-       if (!index){
-               index = strstr (prefix, ".r");
-               if (index){
-                       *index=0;
-                       strcpy (replace, ".l");
-                       strcpy (suffix, index+2);
-               }
-       }
-
-       sprintf (name, "%s%s%s", prefix, replace, suffix);
-}
-
-void paste_posebuf (int flip){
-       Object *ob;
-       bPoseChannel *temp, *chan;
-       float eul[4];
-       Base    *base;
-       int             newchan = 0;
-
-       ob=G.obpose;
-       if (!ob)
-               return;
-
-       if (!g_posebuf){
-               error ("Copy buffer is empty");
-               return;
-       };
-       
-       collect_pose_garbage(ob);
-
-       /* Safely merge all of the channels in this pose into
-       any existing pose */
-       if (ob->pose){
-               if (U.uiflag & USER_KEYINSERTACT){
-                       /* Display "Avail, all" dialog */
-               }
-               for (chan=g_posebuf->chanbase.first; chan; chan=chan->next){
-                       if (chan->flag & POSE_KEY){
-                               temp = copy_pose_channel (chan);
-                               if (flip){
-                                       flip_name (temp->name);
-                                       temp->loc[0]*=-1;
-
-                                       QuatToEul(temp->quat, eul);
-                                       eul[1]*=-1;
-                                       eul[2]*=-1;
-                                       EulToQuat(eul, temp->quat);
-                               }
-
-                               temp = set_pose_channel (ob->pose, temp);
-
-                               if (U.uiflag & USER_KEYINSERTACT){
-                                       /* Set keys on pose */
-                                       if (chan->flag & POSE_ROT){
-                                               set_action_key(ob->action, temp, AC_QUAT_X, newchan);
-                                               set_action_key(ob->action, temp, AC_QUAT_Y, newchan);
-                                               set_action_key(ob->action, temp, AC_QUAT_Z, newchan);
-                                               set_action_key(ob->action, temp, AC_QUAT_W, newchan);
-                                       };
-                                       if (chan->flag & POSE_SIZE){
-                                               set_action_key(ob->action, temp, AC_SIZE_X, newchan);
-                                               set_action_key(ob->action, temp, AC_SIZE_Y, newchan);
-                                               set_action_key(ob->action, temp, AC_SIZE_Z, newchan);
-                                       };
-                                       if (chan->flag & POSE_LOC){
-                                               set_action_key(ob->action, temp, AC_LOC_X, newchan);
-                                               set_action_key(ob->action, temp, AC_LOC_Y, newchan);
-                                               set_action_key(ob->action, temp, AC_LOC_Z, newchan);
-                                       };                                      
-                               }
-                       }
-               }
-
-               if (U.uiflag & USER_KEYINSERTACT){
-                       remake_action_ipos(ob->action);
-                       allqueue (REDRAWIPO, 0);
-                       allqueue (REDRAWVIEW3D, 0);
-                       allqueue (REDRAWACTION, 0);             
-                       allqueue(REDRAWNLA, 0);
-               }
-
-               /* Update deformation children */
-               if (G.obpose->type == OB_ARMATURE){
-                       for (base= FIRSTBASE; base; base= base->next){
-                               if (G.obpose==base->object->parent){
-                                       if (base->object->partype==PARSKEL)
-                                               makeDispList(base->object);
-                               }
-                       }
-               }
-       }
-}
-
-void set_action_key (struct bAction *act, struct bPoseChannel *chan, int adrcode, short makecurve)
-{
-       set_action_key_time (act, chan, adrcode, makecurve, frame_to_float(CFRA));
-}
-
-static void set_action_key_time (bAction *act, bPoseChannel *chan, int adrcode, short makecurve, float time)
-{
-       bActionChannel  *achan;
-       char    ipstr[256];
-
-       if (!act)
-               return;
-
-       if (!chan)
-               return;
-       /* See if this action channel exists already */ 
-       for (achan=act->chanbase.first; achan; achan=achan->next){
-               if (!strcmp (chan->name, achan->name))
-                       break;
-       }
-
-       if (!achan){
-               if (!makecurve)
-                       return;
-               achan = MEM_callocN (sizeof(bActionChannel), "actionChannel");
-               strcpy (achan->name, chan->name);
-               BLI_addtail (&act->chanbase, achan);
-       }
-
-       /* Ensure the channel appears selected in the action window */
-       /* ton: added flag hilighted, for display in ipowin. dunno what the difference is between select/hilite */
-       achan->flag |= ACHAN_SELECTED|ACHAN_HILIGHTED;
-
-       /* Ensure this action channel has a valid Ipo */
-       if (!achan->ipo){
-               sprintf (ipstr, "%s.%s", act->id.name+2, chan->name);
-               ipstr[23]=0;
-               achan->ipo=     add_ipo(ipstr, ID_AC);  
-       }
-
-       insertactionkey(act, achan, chan, adrcode, makecurve, time);
-
-}
-
-static void insertactionkey(bAction *act, bActionChannel *achan, bPoseChannel *chan, int adrcode, short makecurve, float cfra)
+bAction *add_empty_action(int blocktype)
 {
-       IpoCurve *icu;
-       void *poin;
-       float curval;
-       int type;
-       ID *id;
-       
-       if (!act){
-               return;
-       }
-       if (act->id.lib){
-               error ("Can't pose library actions");
-               return;
-       }
-       act->achan=achan;
-       act->pchan=chan;
-
-       id=(ID*) act;
-
-       /* First see if this curve exists */
-       if (!makecurve){
-               if (!achan->ipo)
-                       return;
-
-               for (icu = achan->ipo->curve.first; icu; icu=icu->next){
-                       if (icu->adrcode == adrcode)
-                               break;
-               }
-               if (!icu)
-                       return;
-       }
-
+       bAction *act;
+       char *str= "Action";
        
-       icu = get_ipocurve (id, GS(id->name), adrcode, achan->ipo);
-
-       if(icu) {
-               poin= get_ipo_poin(id, icu, &type);
-               if(poin) {
-                       curval= read_ipo_poin(poin, type);
-       //              cfra= frame_to_float(CFRA);
-                       insert_vert_ipo(icu, cfra, curval);
-               }
-       }
+       if(blocktype==ID_OB)
+               str= "ObAction";
+       else if(blocktype==ID_KE)
+               str= "ShapeAction";
        
-}
-
-bAction *add_empty_action(void)
-{
-       bAction *act;
-
-       act= alloc_libblock(&G.main->action, ID_AC, "Action");
+       act= alloc_libblock(&G.main->action, ID_AC, str);
        act->id.flag |= LIB_FAKEUSER;
        act->id.us++;
        return act;
 }
 
-void transform_actionchannel_keys(char mode)
+void transform_actionchannel_keys(int mode, int dummy)
 {
        bAction *act;
        TransVert *tv;
-       int /*sel=0,*/  i;
+       Object *ob= OBACT;
+       bConstraintChannel *conchan;
        bActionChannel  *chan;
-       short   mvals[2], mvalc[2], cent[2];
+       float   deltax, startx;
+       float   minx, maxx, cenf[2];
        float   sval[2], cval[2], lastcval[2];
-       short   cancel=0;
-       float   fac=0.0F;
+       float   fac=0.0f;
        int             loop=1;
        int             tvtot=0;
-       float   deltax, startx;
-       float   cenf[2];
        int             invert=0, firsttime=1;
+       int             i;
+       short   cancel=0;
+       short   mvals[2], mvalc[2], cent[2];
        char    str[256];
-       bConstraintChannel *conchan;
 
        act=G.saction->action;
 
@@ -1142,6 +870,7 @@ void transform_actionchannel_keys(char mode)
        
        /* Build the transvert structure */
        tv = MEM_callocN (sizeof(TransVert) * tvtot, "transVert");
+       
        tvtot=0;
        for (chan=act->chanbase.first; chan; chan=chan->next){
                /* Add the actionchannel */
@@ -1149,7 +878,14 @@ void transform_actionchannel_keys(char mode)
                for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
                        tvtot = add_trans_ipo_keys(conchan->ipo, tv, tvtot);
        }
-
+       
+       /* min max, only every other three */
+       minx= maxx= tv[1].loc[0];
+       for (i=1; i<tvtot; i+=3){
+               if(minx>tv[i].loc[0]) minx= tv[i].loc[0];
+               if(maxx<tv[i].loc[0]) maxx= tv[i].loc[0];
+       }
+       
        /* Do the event loop */
        cent[0] = curarea->winx + (G.saction->v2d.hor.xmax)/2;
        cent[1] = curarea->winy + (G.saction->v2d.hor.ymax)/2;
@@ -1158,8 +894,21 @@ void transform_actionchannel_keys(char mode)
        getmouseco_areawin (mvals);
        areamouseco_to_ipoco(G.v2d, mvals, &sval[0], &sval[1]);
 
+       if(G.saction->pin==0 && OBACT)
+               sval[0]= get_action_frame(OBACT, sval[0]);
+       
+       /* used for drawing */
+       if(mode=='t') {
+               G.saction->flag |= SACTION_MOVING;
+               G.saction->timeslide= sval[0];
+       }
+       
        startx=sval[0];
        while (loop) {
+               
+               if(mode=='t' && minx==maxx)
+                       break;
+               
                /*              Get the input */
                /*              If we're cancelling, reset transformations */
                /*                      Else calc new transformation */
@@ -1197,14 +946,36 @@ void transform_actionchannel_keys(char mode)
                } else {
                        getmouseco_areawin (mvalc);
                        areamouseco_to_ipoco(G.v2d, mvalc, &cval[0], &cval[1]);
+                       
+                       if(G.saction->pin==0 && OBACT)
+                               cval[0]= get_action_frame(OBACT, cval[0]);
 
+                       if(mode=='t')
+                               G.saction->timeslide= cval[0];
+                       
                        if (!firsttime && lastcval[0]==cval[0] && lastcval[1]==cval[1]) {
                                PIL_sleep_ms(1);
                        } else {
+                               
                                for (i=0; i<tvtot; i++){
                                        tv[i].loc[0]=tv[i].oldloc[0];
 
                                        switch (mode){
+                                       case 't':
+                                               if( sval[0] > minx && sval[0] < maxx) {
+                                                       float timefac, cvalc= CLAMPIS(cval[0], minx, maxx);
+                                                       
+                                                       /* left half */
+                                                       if(tv[i].oldloc[0] < sval[0]) {
+                                                               timefac= ( sval[0] - tv[i].oldloc[0])/(sval[0] - minx);
+                                                               tv[i].loc[0]= cvalc - timefac*( cvalc - minx);
+                                                       }
+                                                       else {
+                                                               timefac= (tv[i].oldloc[0] - sval[0])/(maxx - sval[0]);
+                                                               tv[i].loc[0]= cvalc + timefac*(maxx- cvalc);
+                                                       }
+                                               }
+                                               break;
                                        case 'g':
                                                deltax = cval[0]-sval[0];
                                                fac= deltax;
@@ -1231,7 +1002,9 @@ void transform_actionchannel_keys(char mode)
                                                        fac*=-1;
                                                }
                                                startx= (G.scene->r.cfra);
-                                       
+                                               if(G.saction->pin==0 && OBACT)
+                                                       startx= get_action_frame(OBACT, startx);
+                                                       
                                                tv[i].loc[0]-= startx;
                                                tv[i].loc[0]*=fac;
                                                tv[i].loc[0]+= startx;
@@ -1239,28 +1012,35 @@ void transform_actionchannel_keys(char mode)
                                                break;
                                        }
                                }
-                       }
        
-                       if (mode=='s'){
-                               sprintf(str, "sizeX: %.3f", fac);
-                               headerprint(str);
-                       }
-                       else if (mode=='g'){
-                               sprintf(str, "deltaX: %.3f", fac);
-                               headerprint(str);
-                       }
-       
-                       if (G.saction->lock){
-                               do_all_actions();
-                               allqueue (REDRAWVIEW3D, 0);
-                               allqueue (REDRAWACTION, 0);
-                               allqueue (REDRAWIPO, 0);
-                               allqueue(REDRAWNLA, 0);
-                               force_draw_all();
-                       }
-                       else {
-                               addqueue (curarea->win, REDRAWALL, 0);
-                               force_draw ();
+                               if (mode=='s'){
+                                       sprintf(str, "sizeX: %.3f", fac);
+                                       headerprint(str);
+                               }
+                               else if (mode=='g'){
+                                       sprintf(str, "deltaX: %.3f", fac);
+                                       headerprint(str);
+                               }
+                               else if (mode=='t') {
+                                       float fac= 2.0*(cval[0]-sval[0])/(maxx-minx);
+                                       CLAMP(fac, -1.0f, 1.0f);
+                                       sprintf(str, "TimeSlide: %.3f", fac);
+                                       headerprint(str);
+                               }
+               
+                               if (G.saction->lock) {
+                                       if(ob) {
+                                               ob->ctime= -1234567.0f;
+                                               if(ob->pose || ob_get_key(ob))
+                                                       DAG_object_flush_update(G.scene, ob, OB_RECALC);
+                                               else
+                                                       DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
+                                       }
+                                       force_draw_plus(SPACE_VIEW3D, 0);
+                               }
+                               else {
+                                       force_draw(0);
+                               }
                        }
                }
                
@@ -1272,8 +1052,20 @@ void transform_actionchannel_keys(char mode)
        /*              Update the curve */
        /*              Depending on the lock status, draw necessary views */
 
-       do_all_actions();
+       if(ob) {
+               ob->ctime= -1234567.0f;
+
+               if(ob->pose || ob_get_key(ob))
+                       DAG_object_flush_update(G.scene, ob, OB_RECALC);
+               else
+                       DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
+       }
+       
        remake_action_ipos(act);
+
+       G.saction->flag &= ~SACTION_MOVING;
+       
+       if(cancel==0) BIF_undo_push("Transform Action");
        allqueue (REDRAWVIEW3D, 0);
        allqueue (REDRAWACTION, 0);
        allqueue(REDRAWNLA, 0);
@@ -1437,16 +1229,16 @@ void transform_meshchannel_keys(char mode, Key *key)
                                 * future
                                 */
                                 
-                do_all_actions();
+                               DAG_object_flush_update(G.scene, OBACT, OB_RECALC_OB|OB_RECALC_DATA);
                 allqueue (REDRAWVIEW3D, 0);
                 allqueue (REDRAWACTION, 0);
                 allqueue (REDRAWIPO, 0);
                 allqueue(REDRAWNLA, 0);
-                force_draw_all();
+                force_draw_all(0);
             }
             else {
                 addqueue (curarea->win, REDRAWALL, 0);
-                force_draw ();
+                force_draw(0);
             }
         }
                
@@ -1458,6 +1250,7 @@ void transform_meshchannel_keys(char mode, Key *key)
        /* fix up the Ipocurves and redraw stuff
         */
     meshkey_do_redraw(key);
+       BIF_undo_push("Transform Action Keys");
 
     MEM_freeN (tv);
 
@@ -1466,7 +1259,6 @@ void transform_meshchannel_keys(char mode, Key *key)
         */
 }
 
-
 void deselect_actionchannel_keys (bAction *act, int test)
 {
        bActionChannel  *chan;
@@ -1529,6 +1321,7 @@ void deselect_meshchannel_keys (Key *key, int test)
     set_ipo_key_selection(key->ipo, sel);
 }
 
+/* apparently within active object context */
 void deselect_actionchannels (bAction *act, int test)
 {
        bActionChannel *chan;
@@ -1601,8 +1394,11 @@ static void hilight_channel (bAction *act, bActionChannel *chan, short select)
  *             = SELECT_INVERT
  */
 
-static int select_channel(bAction *act, bActionChannel *chan,
-                          int selectmode) {
+/* exported for outliner (ton) */
+/* apparently within active object context */
+int select_channel(bAction *act, bActionChannel *chan,
+                          int selectmode) 
+{
        /* Select the channel based on the selection mode
         */
        int flag;
@@ -1649,7 +1445,7 @@ static int select_constraint_channel(bAction *act,
        return flag;
 }
 
-
+/* lefthand side */
 static void mouse_actionchannels(bAction *act, short *mval,
                                  short *mvalo, int selectmode) {
        /* Select action channels, based on mouse values.
@@ -1660,7 +1456,7 @@ static void mouse_actionchannels(bAction *act, short *mval,
         * a rectangle.
         */
        bActionChannel *chan;
-       float   click;
+       float   click, x,y;
        int   clickmin, clickmax;
        int             wsize, sel;
        bConstraintChannel *conchan;
@@ -1677,23 +1473,20 @@ static void mouse_actionchannels(bAction *act, short *mval,
         * needed to draw all of the action channels and constraint
         * channels.
         */
-       wsize = (count_action_levels (act)*(CHANNELHEIGHT+CHANNELSKIP));
+       wsize =  count_action_levels(act)*(CHANNELHEIGHT+CHANNELSKIP);
+       wsize += CHANNELHEIGHT/2;
 
-       click = (wsize-(mval[1]+G.v2d->cur.ymin));
-       click += CHANNELHEIGHT/2;
-       click /= (CHANNELHEIGHT+CHANNELSKIP);
+    areamouseco_to_ipoco(G.v2d, mval, &x, &y);
+    clickmin = (int) ((wsize - y) / (CHANNELHEIGHT+CHANNELSKIP));
        
-       clickmin = (int) click;
-
        /* Only one click */
        if (mvalo == NULL) {
                clickmax = clickmin;
        }
        /* Two click values (i.e., border select */
        else {
-               click = (wsize-(mvalo[1]+G.v2d->cur.ymin));
-               click += CHANNELHEIGHT/2;
-               click /= (CHANNELHEIGHT+CHANNELSKIP);
+               areamouseco_to_ipoco(G.v2d, mvalo, &x, &y);
+               click = ((wsize - y) / (CHANNELHEIGHT+CHANNELSKIP));
 
                if ( ((int) click) < clickmin) {
                        clickmax = clickmin;
@@ -1728,9 +1521,10 @@ static void mouse_actionchannels(bAction *act, short *mval,
                         * active channel for the action
                         */
                        sel = (chan->flag & ACHAN_SELECTED);
-                       if ( select_channel(act, chan, selectmode) && !sel ) {
-                               act->achan = chan;
-                       }
+                       select_channel(act, chan, selectmode);
+                       /* messy... */
+                       select_poseelement_by_name(chan->name, 2);
+                       
                }
                --clickmin;
                --clickmax;
@@ -1751,6 +1545,8 @@ static void mouse_actionchannels(bAction *act, short *mval,
        allqueue (REDRAWVIEW3D, 0);
        allqueue (REDRAWACTION, 0);
        allqueue (REDRAWNLA, 0);
+       allqueue (REDRAWOOPS, 0);
+       allqueue (REDRAWBUTSALL, 0);
 }
 
 void delete_meshchannel_keys(Key *key)
@@ -1758,6 +1554,7 @@ void delete_meshchannel_keys(Key *key)
        if (!okee("Erase selected keys"))
                return;
 
+       BIF_undo_push("Delete Action keys");
        delete_ipo_keys(key->ipo);
 
        meshkey_do_redraw(key);
@@ -1787,12 +1584,14 @@ void delete_actionchannel_keys(void)
        }
 
        remake_action_ipos (act);
+       BIF_undo_push("Delete Action keys");
        allspace(REMAKEIPO, 0);
        allqueue(REDRAWACTION, 0);
        allqueue(REDRAWIPO, 0);
        allqueue(REDRAWNLA, 0);
 
 }
+
 static void delete_actionchannels (void)
 {
        bConstraintChannel *conchan=NULL, *nextconchan;
@@ -1849,6 +1648,7 @@ static void delete_actionchannels (void)
 
        }
 
+       BIF_undo_push("Delete Action channels");
        allqueue (REDRAWACTION, 0);
        allqueue(REDRAWNLA, 0);
 
@@ -1856,8 +1656,10 @@ static void delete_actionchannels (void)
 
 void sethandles_meshchannel_keys(int code, Key *key)
 {
+       
     sethandles_ipo_keys(key->ipo, code);
 
+       BIF_undo_push("Set handles Action keys");
        meshkey_do_redraw(key);
 }
 
@@ -1882,13 +1684,15 @@ void sethandles_actionchannel_keys(int code)
        /* Clean up and redraw stuff
         */
        remake_action_ipos (act);
+       BIF_undo_push("Set handles Action channel");
        allspace(REMAKEIPO, 0);
        allqueue(REDRAWACTION, 0);
        allqueue(REDRAWIPO, 0);
        allqueue(REDRAWNLA, 0);
 }
 
-void set_ipotype_actionchannels(int ipotype) {
+void set_ipotype_actionchannels(int ipotype) 
+{
 
        bAction *act; 
        bActionChannel *chan;
@@ -1927,13 +1731,104 @@ void set_ipotype_actionchannels(int ipotype) {
        /* Clean up and redraw stuff
         */
        remake_action_ipos (act);
+       BIF_undo_push("Set Ipo type Action channel");
+       allspace(REMAKEIPO, 0);
+       allqueue(REDRAWACTION, 0);
+       allqueue(REDRAWIPO, 0);
+       allqueue(REDRAWNLA, 0);
+}
+
+void set_extendtype_actionchannels(int extendtype)
+{
+       bAction *act; 
+       bActionChannel *chan;
+       short event;
+
+       /* Get the selected action, exit if none are selected 
+        */
+       act = G.saction->action;
+       if (!act)
+               return;
+
+       if (extendtype == SET_EXTEND_POPUP) {
+               /* Present a popup menu asking the user what type
+                * of IPO curve he/she/GreenBTH wants. ;)
+                */
+               event
+                       =  pupmenu("Channel Extending Type %t|"
+                                          "Constant %x1|"
+                                          "Extrapolation %x2|"
+                                          "Cyclic %x3|"
+                                          "Cyclic extrapolation %x4");
+               if(event < 1) return;
+               extendtype = event;
+       }
+       
+       /* Loop through the channels and for the selected ones set
+        * the type for each Ipo curve in the channel Ipo (based on
+        * the value from the popup).
+        */
+       for (chan = act->chanbase.first; chan; chan=chan->next){
+               if (chan->flag & ACHAN_SELECTED) {
+                       if (chan->ipo) {
+                               switch (extendtype) {
+                               case SET_EXTEND_CONSTANT:
+                                       setexprap_ipoloop(chan->ipo, IPO_HORIZ);
+                                       break;
+                               case SET_EXTEND_EXTRAPOLATION:
+                                       setexprap_ipoloop(chan->ipo, IPO_DIR);
+                                       break;
+                               case SET_EXTEND_CYCLIC:
+                                       setexprap_ipoloop(chan->ipo, IPO_CYCL);
+                                       break;
+                               case SET_EXTEND_CYCLICEXTRAPOLATION:
+                                       setexprap_ipoloop(chan->ipo, IPO_CYCLX);
+                                       break;
+                               }
+                       }
+               }
+       }
+
+       /* Clean up and redraw stuff
+        */
+       remake_action_ipos (act);
+       BIF_undo_push("Set Ipo type Action channel");
        allspace(REMAKEIPO, 0);
        allqueue(REDRAWACTION, 0);
        allqueue(REDRAWIPO, 0);
        allqueue(REDRAWNLA, 0);
 }
 
-void select_all_keys_frames(bAction *act, short *mval, 
+void set_snap_actionchannels(void) 
+{
+       
+       bAction *act; 
+       bActionChannel *chan;
+       
+       /* Get the selected action, exit if none are selected 
+               */
+       act = G.saction->action;
+       if (!act)
+               return;
+       
+       /* Loop through the channels */
+       for (chan = act->chanbase.first; chan; chan=chan->next){
+               if (chan->ipo) {
+                       snap_ipo_keys(chan->ipo);
+               }
+       }
+       
+       /* Clean up and redraw stuff */
+       remake_action_ipos (act);
+       BIF_undo_push("Snap Ipo Action channel");
+       allspace(REMAKEIPO, 0);
+       allqueue(REDRAWACTION, 0);
+       allqueue(REDRAWIPO, 0);
+       allqueue(REDRAWNLA, 0);
+}
+
+
+static void select_all_keys_frames(bAction *act, short *mval, 
                                                        short *mvalo, int selectmode) {
        
        /* This function tries to select all action keys in
@@ -2002,16 +1897,17 @@ void select_all_keys_frames(bAction *act, short *mval,
                                                                 selectmode);
                }
        }       
+
        allqueue(REDRAWNLA, 0);
        allqueue(REDRAWACTION, 0);
        allqueue(REDRAWIPO, 0);
 }
 
 
-void select_all_keys_channels(bAction *act, short *mval, 
+static void select_all_keys_channels(bAction *act, short *mval, 
                               short *mvalo, int selectmode) {
        bActionChannel    *chan;
-       float              click;
+       float              click, x,y;
        int                clickmin, clickmax;
        int                wsize;
        bConstraintChannel *conchan;
@@ -2039,24 +1935,23 @@ void select_all_keys_channels(bAction *act, short *mval,
         * needed to draw all of the action channels and constraint
         * channels.
         */
-       wsize = (count_action_levels (act)*(CHANNELHEIGHT+CHANNELSKIP));
-
-       click = (wsize-(mval[1]+G.v2d->cur.ymin));
-       click += CHANNELHEIGHT/2;
-       click /= (CHANNELHEIGHT+CHANNELSKIP);
-
-       clickmin = (int) click;
 
+       wsize =  count_action_levels(act)*(CHANNELHEIGHT+CHANNELSKIP);
+       wsize += CHANNELHEIGHT/2;
+       
+    areamouseco_to_ipoco(G.v2d, mval, &x, &y);
+    clickmin = (int) ((wsize - y) / (CHANNELHEIGHT+CHANNELSKIP));
+       
        /* Only one click */
        if (mvalo == NULL) {
                clickmax = clickmin;
        }
        /* Two click values (i.e., border select) */
        else {
-               click = (wsize-(mvalo[1]+G.v2d->cur.ymin));
-               click += CHANNELHEIGHT/2;
-               click /= (CHANNELHEIGHT+CHANNELSKIP);
 
+               areamouseco_to_ipoco(G.v2d, mvalo, &x, &y);
+               click = ((wsize - y) / (CHANNELHEIGHT+CHANNELSKIP));
+               
                if ( ((int) click) < clickmin) {
                        clickmax = clickmin;
                        clickmin = (int) click;
@@ -2145,6 +2040,7 @@ static void borderselect_function(void (*select_func)(bAction *act,
                else if (val == RIGHTMOUSE)
                        select_func(act, mval, mvalo, SELECT_SUBTRACT);
        }
+       BIF_undo_push("Border select Action");
        
 }
 
@@ -2237,10 +2133,10 @@ void winqreadactionspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
                
                /* swap mouse buttons based on user preference */
                if (U.flag & USER_LMOUSESELECT) {
-                       if (evt->event == LEFTMOUSE) {
+                       if (event == LEFTMOUSE) {
                                event = RIGHTMOUSE;
                                mousebut = L_MOUSE;
-                       } else if (evt->event == RIGHTMOUSE) {
+                       } else if (event == RIGHTMOUSE) {
                                event = LEFTMOUSE;
                                mousebut = R_MOUSE;
                        }
@@ -2305,6 +2201,7 @@ void winqreadactionspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
                                 */
                                if (mval[0]<NAMEWIDTH){
                                        borderselect_function(mouse_actionchannels);
+                                       BIF_undo_push("Select Action");
                                }
                                else if (mval[0]>ACTWIDTH){
 
@@ -2360,26 +2257,26 @@ void winqreadactionspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
                                        transform_meshchannel_keys('g', key);
                                }
                                else if (act) {
-                                       transform_actionchannel_keys ('g');
+                                       transform_actionchannel_keys ('g', 0);
                                }
                        }
                        break;
 
                case HKEY:
-                       if (key) {
-                               if(G.qual & LR_SHIFTKEY) {
-                                       sethandles_meshchannel_keys(HD_AUTO, key);
-                               }
-                               else { 
-                                       sethandles_meshchannel_keys(HD_ALIGN, key);
+                       if(G.qual & LR_SHIFTKEY) {
+                               if(okee("Set Keys to Auto Handle")) {
+                                       if (key)
+                                               sethandles_meshchannel_keys(HD_AUTO, key);
+                                       else
+                                               sethandles_actionchannel_keys(HD_AUTO);
                                }
                        }
                        else {
-                               if(G.qual & LR_SHIFTKEY) {
-                                       sethandles_actionchannel_keys(HD_AUTO);
-                               }
-                               else { 
-                                       sethandles_actionchannel_keys(HD_ALIGN);
+                               if(okee("Toggle Keys Aligned Handle")) {
+                                       if (key)
+                                               sethandles_meshchannel_keys(HD_ALIGN, key);
+                                       else
+                                               sethandles_actionchannel_keys(HD_ALIGN);
                                }
                        }
                        break;
@@ -2396,11 +2293,15 @@ void winqreadactionspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
                        
                case SKEY: 
                        if (mval[0]>=ACTWIDTH) {
-                               if (key) {
-                                       transform_meshchannel_keys('s', key);
+                               if(G.qual & LR_SHIFTKEY) {
+                                       if(okee("Snap to frame"))
+                                               set_snap_actionchannels();
                                }
-                               else if (act) {
-                                       transform_actionchannel_keys ('s');
+                               else {
+                                       if (key)
+                                               transform_meshchannel_keys('s', key);
+                                       else if (act)
+                                               transform_actionchannel_keys ('s', 0);
                                }
                        }
                        break;
@@ -2411,20 +2312,50 @@ void winqreadactionspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
                                /* to do */
                        }
                        else {
-                               set_ipotype_actionchannels(SET_IPO_POPUP);
+                               if(G.qual & LR_SHIFTKEY)
+                                       set_ipotype_actionchannels(SET_IPO_POPUP);
+                               else
+                                       transform_actionchannel_keys ('t', 0);
                        }
                        break;
 
                case VKEY:
+                       if(okee("Set Keys to Vector Handle")) {
+                               if (key)
+                                       sethandles_meshchannel_keys(HD_VECT, key);
+                               else
+                                       sethandles_actionchannel_keys(HD_VECT);
+                       }
+                       break;
+
+               case PAGEUPKEY:
                        if (key) {
-                               sethandles_meshchannel_keys(HD_VECT, key);
                                /* to do */
                        }
                        else {
-                               sethandles_actionchannel_keys(HD_VECT);
+                               if(G.qual & LR_SHIFTKEY) {
+                                       top_sel_action();
+                               }
+                               else
+                               {
+                                       up_sel_action();
+                               }
+                               
+                       }
+                       break;
+               case PAGEDOWNKEY:
+                       if (key) {
+                               /* to do */
+                       }
+                       else {
+                               if(G.qual & LR_SHIFTKEY) {
+                                       bottom_sel_action();
+                               }
+                               else
+                               down_sel_action();
+                               
                        }
                        break;
-
                case DELKEY:
                case XKEY:
                        if (key) {
@@ -2441,7 +2372,9 @@ void winqreadactionspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
                 * based on user preference USER_LMOUSESELECT
                 */
                case LEFTMOUSE:
-                       if (mval[0]>ACTWIDTH){
+                       if(view2dmove(LEFTMOUSE)) // only checks for sliders
+                               break;
+                       else if (mval[0]>ACTWIDTH){
                                do {
                                        getmouseco_areawin(mval);
                                        
@@ -2453,14 +2386,13 @@ void winqreadactionspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
                                        if( cfra!=CFRA ) {
                                                CFRA= cfra;
                                                update_for_newframe();
-                                               force_draw_plus(SPACE_VIEW3D);
-                                               force_draw_plus(SPACE_IPO);
-                                               force_draw_plus(SPACE_BUTS);
-                                       }
+                                               force_draw_all(0);                                      }
+                                       else PIL_sleep_ms(30);
                                        
                                } while(get_mbut() & mousebut);
+                               break;
                        }
-                       break;
+                       /* passed on as selection */
                case RIGHTMOUSE:
                        /* Clicking in the channel area selects the
                         * channel or constraint channel
@@ -2471,6 +2403,8 @@ void winqreadactionspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
                                                mouse_actionchannels(act, mval, NULL,  SELECT_INVERT);
                                        else
                                                mouse_actionchannels(act, mval, NULL,  SELECT_REPLACE);
+                                       
+                                       BIF_undo_push("Select Action");
                                }
                                else numbuts_action();
                        }
@@ -2499,6 +2433,7 @@ void winqreadactionspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
                                        else
                                                select_all_keys_frames(act, mval, NULL, 
                                                                                           SELECT_REPLACE);
+                                       BIF_undo_push("Select all Action");
                                }
                                
                                /* Clicking in the main area of the action window
@@ -2520,6 +2455,20 @@ void winqreadactionspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
                                }
                        }
                        break;
+               case PADPLUSKEY:
+                       view2d_zoom(G.v2d, 0.1154, sa->winx, sa->winy);
+                       test_view2d(G.v2d, sa->winx, sa->winy);
+                       view2d_do_locks(curarea, V2D_LOCK_COPY);
+
+                       doredraw= 1;
+                       break;
+               case PADMINUS:
+                       view2d_zoom(G.v2d, -0.15, sa->winx, sa->winy);
+                       test_view2d(G.v2d, sa->winx, sa->winy);
+                       view2d_do_locks(curarea, V2D_LOCK_COPY);
+                       
+                       doredraw= 1;
+                       break;
                case MIDDLEMOUSE:
                case WHEELUPMOUSE:
                case WHEELDOWNMOUSE:
@@ -2532,7 +2481,8 @@ void winqreadactionspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
        
 }
 
-Key *get_action_mesh_key(void) {
+Key *get_action_mesh_key(void) 
+{
        /* gets the key data from the currently selected
         * mesh/lattice. If a mesh is not selected, or does not have
         * key data, then we return NULL (currently only
@@ -2575,9 +2525,162 @@ int get_nearest_key_num(Key *key, short *mval, float *x) {
     areamouseco_to_ipoco(G.v2d, mval, x, &y);
 
     ybase = key->totkey * (CHANNELHEIGHT + CHANNELSKIP);
-    num = (int) ((ybase - y) / (CHANNELHEIGHT+CHANNELSKIP));
+    num = (int) ((ybase - y + CHANNELHEIGHT/2) / (CHANNELHEIGHT+CHANNELSKIP));
 
     return (num + 1);
 }
 
 
+
+void top_sel_action()
+{
+       bAction *act;
+       bActionChannel *chan;
+       
+       /* Get the selected action, exit if none are selected */
+       act = G.saction->action;
+       if (!act) return;
+       
+       for (chan=act->chanbase.first; chan; chan=chan->next){
+               if ((chan->flag & ACHAN_SELECTED) && !(chan->flag & ACHAN_MOVED)){
+                       /* take it out off the chain keep data */
+                       BLI_remlink (&act->chanbase, chan);
+                       /* make it first element */
+                       BLI_insertlinkbefore(&act->chanbase,act->chanbase.first, chan);
+                       chan->flag |= ACHAN_MOVED;
+                       /* restart with rest of list */
+                       chan=chan->next;
+               }
+       }
+    /* clear temp flags */
+       for (chan=act->chanbase.first; chan; chan=chan->next){
+               chan->flag = chan->flag & ~ACHAN_MOVED;
+       }
+       
+       /* Clean up and redraw stuff */
+       remake_action_ipos (act);
+       BIF_undo_push("Top Action channel");
+       allspace(REMAKEIPO, 0);
+       allqueue(REDRAWACTION, 0);
+       allqueue(REDRAWIPO, 0);
+       allqueue(REDRAWNLA, 0);
+}
+
+void up_sel_action()
+{
+       bAction *act;
+       bActionChannel *chan;
+       bActionChannel *prev;
+       
+       /* Get the selected action, exit if none are selected */
+       act = G.saction->action;
+       if (!act) return;
+       
+       for (chan=act->chanbase.first; chan; chan=chan->next){
+               if ((chan->flag & ACHAN_SELECTED) && !(chan->flag & ACHAN_MOVED)){
+                       prev = chan->prev;
+                       if (prev){
+                               /* take it out off the chain keep data */
+                               BLI_remlink (&act->chanbase, chan);
+                               /* push it up */
+                               BLI_insertlinkbefore(&act->chanbase,prev, chan);
+                               chan->flag |= ACHAN_MOVED;
+                               /* restart with rest of list */
+                               chan=chan->next;
+                       }
+               }
+       }
+       /* clear temp flags */
+       for (chan=act->chanbase.first; chan; chan=chan->next){
+               chan->flag = chan->flag & ~ACHAN_MOVED;
+       }
+       
+       /* Clean up and redraw stuff
+       */
+       remake_action_ipos (act);
+       BIF_undo_push("Up Action channel");
+       allspace(REMAKEIPO, 0);
+       allqueue(REDRAWACTION, 0);
+       allqueue(REDRAWIPO, 0);
+       allqueue(REDRAWNLA, 0);
+}
+
+void down_sel_action()
+{
+       bAction *act;
+       bActionChannel *chan;
+       bActionChannel *next;
+       
+       /* Get the selected action, exit if none are selected */
+       act = G.saction->action;
+       if (!act) return;
+       
+       for (chan=act->chanbase.last; chan; chan=chan->prev){
+               if ((chan->flag & ACHAN_SELECTED) && !(chan->flag & ACHAN_MOVED)){
+                       next = chan->next;
+                       if (next) next = next->next;
+                       if (next){
+                               /* take it out off the chain keep data */
+                               BLI_remlink (&act->chanbase, chan);
+                               /* move it down */
+                               BLI_insertlinkbefore(&act->chanbase,next, chan);
+                               chan->flag |= ACHAN_MOVED;
+                       }
+                       else {
+                               /* take it out off the chain keep data */
+                               BLI_remlink (&act->chanbase, chan);
+                               /* add at end */
+                               BLI_addtail(&act->chanbase,chan);
+                               chan->flag |= ACHAN_MOVED;
+                       }
+
+               }
+       }
+       /* clear temp flags */
+       for (chan=act->chanbase.first; chan; chan=chan->next){
+               chan->flag = chan->flag & ~ACHAN_MOVED;
+       }
+       
+       /* Clean up and redraw stuff
+       */
+       remake_action_ipos (act);
+       BIF_undo_push("Down Action channel");
+       allspace(REMAKEIPO, 0);
+       allqueue(REDRAWACTION, 0);
+       allqueue(REDRAWIPO, 0);
+       allqueue(REDRAWNLA, 0);
+}
+
+void bottom_sel_action()
+{
+       bAction *act;
+       bActionChannel *chan;
+       
+       /* Get the selected action, exit if none are selected */
+       act = G.saction->action;
+       if (!act) return;
+       
+       for (chan=act->chanbase.last; chan; chan=chan->prev){
+               if ((chan->flag & ACHAN_SELECTED) && !(chan->flag & ACHAN_MOVED)) {
+                       /* take it out off the chain keep data */
+                       BLI_remlink (&act->chanbase, chan);
+                       /* add at end */
+                       BLI_addtail(&act->chanbase,chan);
+                       chan->flag |= ACHAN_MOVED;
+               }
+               
+       }
+       /* clear temp flags */
+       for (chan=act->chanbase.first; chan; chan=chan->next){
+               chan->flag = chan->flag & ~ACHAN_MOVED;
+       }
+       
+       /* Clean up and redraw stuff
+       */
+       remake_action_ipos (act);
+       BIF_undo_push("Bottom Action channel");
+       allspace(REMAKEIPO, 0);
+       allqueue(REDRAWACTION, 0);
+       allqueue(REDRAWIPO, 0);
+       allqueue(REDRAWNLA, 0);
+}