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