Orange's buglist!
[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   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         tvtot=0;
879         for (chan=act->chanbase.first; chan; chan=chan->next){
880                 /* Add the actionchannel */
881                 tvtot = add_trans_ipo_keys(chan->ipo, tv, tvtot);
882                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
883                         tvtot = add_trans_ipo_keys(conchan->ipo, tv, tvtot);
884         }
885
886         /* Do the event loop */
887         cent[0] = curarea->winx + (G.saction->v2d.hor.xmax)/2;
888         cent[1] = curarea->winy + (G.saction->v2d.hor.ymax)/2;
889         areamouseco_to_ipoco(G.v2d, cent, &cenf[0], &cenf[1]);
890
891         getmouseco_areawin (mvals);
892         areamouseco_to_ipoco(G.v2d, mvals, &sval[0], &sval[1]);
893
894         startx=sval[0];
895         while (loop) {
896                 /*              Get the input */
897                 /*              If we're cancelling, reset transformations */
898                 /*                      Else calc new transformation */
899                 /*              Perform the transformations */
900                 while (qtest()) {
901                         short val;
902                         unsigned short event= extern_qread(&val);
903
904                         if (val) {
905                                 switch (event) {
906                                 case LEFTMOUSE:
907                                 case SPACEKEY:
908                                 case RETKEY:
909                                         loop=0;
910                                         break;
911                                 case XKEY:
912                                         break;
913                                 case ESCKEY:
914                                 case RIGHTMOUSE:
915                                         cancel=1;
916                                         loop=0;
917                                         break;
918                                 default:
919                                         arrows_move_cursor(event);
920                                         break;
921                                 };
922                         }
923                 }
924
925                 if (cancel) {
926                         for (i=0; i<tvtot; i++) {
927                                 tv[i].loc[0]=tv[i].oldloc[0];
928                                 tv[i].loc[1]=tv[i].oldloc[1];
929                         }
930                 } else {
931                         getmouseco_areawin (mvalc);
932                         areamouseco_to_ipoco(G.v2d, mvalc, &cval[0], &cval[1]);
933
934                         if (!firsttime && lastcval[0]==cval[0] && lastcval[1]==cval[1]) {
935                                 PIL_sleep_ms(1);
936                         } else {
937                                 for (i=0; i<tvtot; i++){
938                                         tv[i].loc[0]=tv[i].oldloc[0];
939
940                                         switch (mode){
941                                         case 'g':
942                                                 deltax = cval[0]-sval[0];
943                                                 fac= deltax;
944                                                 
945                                                 apply_keyb_grid(&fac, 0.0, 1.0, 0.1, U.flag & USER_AUTOGRABGRID);
946
947                                                 tv[i].loc[0]+=fac;
948                                                 break;
949                                         case 's':
950                                                 startx=mvals[0]-(ACTWIDTH/2+(curarea->winrct.xmax-curarea->winrct.xmin)/2);
951                                                 deltax=mvalc[0]-(ACTWIDTH/2+(curarea->winrct.xmax-curarea->winrct.xmin)/2);
952                                                 fac= fabs(deltax/startx);
953                                                 
954                                                 apply_keyb_grid(&fac, 0.0, 0.2, 0.1, U.flag & USER_AUTOSIZEGRID);
955                 
956                                                 if (invert){
957                                                         if (i % 03 == 0){
958                                                                 memcpy (tv[i].loc, tv[i].oldloc, sizeof(tv[i+2].oldloc));
959                                                         }
960                                                         if (i % 03 == 2){
961                                                                 memcpy (tv[i].loc, tv[i].oldloc, sizeof(tv[i-2].oldloc));
962                                                         }
963         
964                                                         fac*=-1;
965                                                 }
966                                                 startx= (G.scene->r.cfra);
967                                         
968                                                 tv[i].loc[0]-= startx;
969                                                 tv[i].loc[0]*=fac;
970                                                 tv[i].loc[0]+= startx;
971                 
972                                                 break;
973                                         }
974                                 }
975                         }
976         
977                         if (mode=='s'){
978                                 sprintf(str, "sizeX: %.3f", fac);
979                                 headerprint(str);
980                         }
981                         else if (mode=='g'){
982                                 sprintf(str, "deltaX: %.3f", fac);
983                                 headerprint(str);
984                         }
985         
986                         if (G.saction->lock) {
987                                 if(ob) {
988                                         ob->ctime= -1234567.0f;
989                                         if(ob->pose || ob_get_key(ob))
990                                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
991                                         else
992                                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
993                                 }
994                                 force_draw_plus(SPACE_VIEW3D, 0);
995                         }
996                         else {
997                                 force_draw(0);
998                         }
999                 }
1000                 
1001                 lastcval[0]= cval[0];
1002                 lastcval[1]= cval[1];
1003                 firsttime= 0;
1004         }
1005         
1006         /*              Update the curve */
1007         /*              Depending on the lock status, draw necessary views */
1008
1009         if(ob) {
1010                 ob->ctime= -1234567.0f;
1011
1012                 if(ob->pose || ob_get_key(ob))
1013                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1014                 else
1015                         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
1016         }
1017         remake_action_ipos(act);
1018
1019         if(cancel==0) BIF_undo_push("Transform Action");
1020         allqueue (REDRAWVIEW3D, 0);
1021         allqueue (REDRAWACTION, 0);
1022         allqueue(REDRAWNLA, 0);
1023         allqueue (REDRAWIPO, 0);
1024         MEM_freeN (tv);
1025 }
1026
1027 void transform_meshchannel_keys(char mode, Key *key)
1028 {
1029         /* this is the function that determines what happens
1030          * to those little blocky rvk key things you have selected 
1031          * after you press a 'g' or an 's'. I'd love to say that
1032          * I have an intimate knowledge of all of what this function
1033          * is doing, but instead I'm just going to pretend.
1034          */
1035     TransVert *tv;
1036     int /*sel=0,*/  i;
1037     short       mvals[2], mvalc[2], cent[2];
1038     float       sval[2], cval[2], lastcval[2];
1039     short       cancel=0;
1040     float       fac=0.0F;
1041     int         loop=1;
1042     int         tvtot=0;
1043     float       deltax, startx;
1044     float       cenf[2];
1045     int         invert=0, firsttime=1;
1046     char        str[256];
1047
1048         /* count all of the selected beziers, and
1049          * set all 3 control handles to selected
1050          */
1051     tvtot=fullselect_ipo_keys(key->ipo);
1052     
1053     /* If nothing is selected, bail out 
1054          */
1055     if (!tvtot)
1056         return;
1057         
1058         
1059     /* Build the transvert structure 
1060          */
1061     tv = MEM_callocN (sizeof(TransVert) * tvtot, "transVert");
1062     tvtot=0;
1063
1064     tvtot = add_trans_ipo_keys(key->ipo, tv, tvtot);
1065
1066     /* Do the event loop 
1067          */
1068     cent[0] = curarea->winx + (G.saction->v2d.hor.xmax)/2;
1069     cent[1] = curarea->winy + (G.saction->v2d.hor.ymax)/2;
1070     areamouseco_to_ipoco(G.v2d, cent, &cenf[0], &cenf[1]);
1071
1072     getmouseco_areawin (mvals);
1073     areamouseco_to_ipoco(G.v2d, mvals, &sval[0], &sval[1]);
1074
1075     startx=sval[0];
1076     while (loop) {
1077         /* Get the input
1078                  * If we're cancelling, reset transformations
1079                  * Else calc new transformation
1080                  * Perform the transformations 
1081                  */
1082         while (qtest()) {
1083             short val;
1084             unsigned short event= extern_qread(&val);
1085
1086             if (val) {
1087                 switch (event) {
1088                 case LEFTMOUSE:
1089                 case SPACEKEY:
1090                 case RETKEY:
1091                     loop=0;
1092                     break;
1093                 case XKEY:
1094                     break;
1095                 case ESCKEY:
1096                 case RIGHTMOUSE:
1097                     cancel=1;
1098                     loop=0;
1099                     break;
1100                 default:
1101                     arrows_move_cursor(event);
1102                     break;
1103                 };
1104             }
1105         }
1106         
1107         if (cancel) {
1108             for (i=0; i<tvtot; i++) {
1109                 tv[i].loc[0]=tv[i].oldloc[0];
1110                 tv[i].loc[1]=tv[i].oldloc[1];
1111             }
1112         } 
1113                 else {
1114             getmouseco_areawin (mvalc);
1115             areamouseco_to_ipoco(G.v2d, mvalc, &cval[0], &cval[1]);
1116                         
1117             if (!firsttime && lastcval[0]==cval[0] && lastcval[1]==cval[1]) {
1118                 PIL_sleep_ms(1);
1119             } else {
1120                 for (i=0; i<tvtot; i++){
1121                     tv[i].loc[0]=tv[i].oldloc[0];
1122
1123                     switch (mode){
1124                     case 'g':
1125                         deltax = cval[0]-sval[0];
1126                         fac= deltax;
1127                                                 
1128                         apply_keyb_grid(&fac, 0.0, 1.0, 0.1, 
1129                                         U.flag & USER_AUTOGRABGRID);
1130
1131                         tv[i].loc[0]+=fac;
1132                         break;
1133                     case 's':
1134                         startx=mvals[0]-(ACTWIDTH/2+(curarea->winrct.xmax
1135                                                      -curarea->winrct.xmin)/2);
1136                         deltax=mvalc[0]-(ACTWIDTH/2+(curarea->winrct.xmax
1137                                                      -curarea->winrct.xmin)/2);
1138                         fac= fabs(deltax/startx);
1139                                                 
1140                         apply_keyb_grid(&fac, 0.0, 0.2, 0.1, 
1141                                         U.flag & USER_AUTOSIZEGRID);
1142                 
1143                         if (invert){
1144                             if (i % 03 == 0){
1145                                 memcpy (tv[i].loc, tv[i].oldloc, 
1146                                         sizeof(tv[i+2].oldloc));
1147                             }
1148                             if (i % 03 == 2){
1149                                 memcpy (tv[i].loc, tv[i].oldloc, 
1150                                         sizeof(tv[i-2].oldloc));
1151                             }
1152                                                         
1153                             fac*=-1;
1154                         }
1155                         startx= (G.scene->r.cfra);
1156                         
1157                         tv[i].loc[0]-= startx;
1158                         tv[i].loc[0]*=fac;
1159                         tv[i].loc[0]+= startx;
1160                 
1161                         break;
1162                     }
1163                 }
1164             }
1165                         /* Display a message showing the magnitude of
1166                          * the grab/scale we are performing
1167                          */
1168             if (mode=='s'){
1169                 sprintf(str, "sizeX: %.3f", fac);
1170                 headerprint(str);
1171             }
1172             else if (mode=='g'){
1173                 sprintf(str, "deltaX: %.3f", fac);
1174                 headerprint(str);
1175             }
1176         
1177             if (G.saction->lock){
1178                                 /* doubt any of this code ever gets
1179                                  * executed, but it might in the
1180                                  * future
1181                                  */
1182                                  
1183                                 DAG_object_flush_update(G.scene, OBACT, OB_RECALC_OB|OB_RECALC_DATA);
1184                 allqueue (REDRAWVIEW3D, 0);
1185                 allqueue (REDRAWACTION, 0);
1186                 allqueue (REDRAWIPO, 0);
1187                 allqueue(REDRAWNLA, 0);
1188                 force_draw_all(0);
1189             }
1190             else {
1191                 addqueue (curarea->win, REDRAWALL, 0);
1192                 force_draw(0);
1193             }
1194         }
1195                 
1196         lastcval[0]= cval[0];
1197         lastcval[1]= cval[1];
1198         firsttime= 0;
1199     }
1200         
1201         /* fix up the Ipocurves and redraw stuff
1202          */
1203     meshkey_do_redraw(key);
1204         BIF_undo_push("Transform Action Keys");
1205
1206     MEM_freeN (tv);
1207
1208         /* did you understand all of that? I pretty much understand
1209          * what it does, but the specifics seem a little weird and crufty.
1210          */
1211 }
1212
1213 void deselect_actionchannel_keys (bAction *act, int test)
1214 {
1215         bActionChannel  *chan;
1216         bConstraintChannel *conchan;
1217         int             sel=1;;
1218
1219         if (!act)
1220                 return;
1221
1222         /* Determine if this is selection or deselection */
1223         
1224         if (test){
1225                 for (chan=act->chanbase.first; chan; chan=chan->next){
1226                         /* Test the channel ipos */
1227                         if (is_ipo_key_selected(chan->ipo)){
1228                                 sel = 0;
1229                                 break;
1230                         }
1231
1232                         /* Test the constraint ipos */
1233                         for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
1234                                 if (is_ipo_key_selected(conchan->ipo)){
1235                                         sel = 0;
1236                                         break;
1237                                 }
1238                         }
1239
1240                         if (sel == 0)
1241                                 break;
1242                 }
1243         }
1244         else
1245                 sel=0;
1246         
1247         /* Set the flags */
1248         for (chan=act->chanbase.first; chan; chan=chan->next){
1249                 set_ipo_key_selection(chan->ipo, sel);
1250                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
1251                         set_ipo_key_selection(conchan->ipo, sel);
1252         }
1253 }
1254
1255 void deselect_meshchannel_keys (Key *key, int test)
1256 {
1257         /* should deselect the rvk keys
1258          */
1259     int         sel=1;
1260
1261     /* Determine if this is selection or deselection */
1262     if (test){
1263         if (is_ipo_key_selected(key->ipo)){
1264             sel = 0;
1265         }
1266     }
1267     else {
1268         sel=0;
1269     }
1270         
1271     /* Set the flags */
1272     set_ipo_key_selection(key->ipo, sel);
1273 }
1274
1275 /* apparently within active object context */
1276 void deselect_actionchannels (bAction *act, int test)
1277 {
1278         bActionChannel *chan;
1279         bConstraintChannel *conchan;
1280         int                     sel=1;  
1281
1282         if (!act)
1283                 return;
1284
1285         /* See if we should be selecting or deselecting */
1286         if (test){
1287                 for (chan=act->chanbase.first; chan; chan=chan->next){
1288                         if (!sel)
1289                                 break;
1290
1291                         if (chan->flag & ACHAN_SELECTED){
1292                                 sel=0;
1293                                 break;
1294                         }
1295                         if (sel){
1296                                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
1297                                         if (conchan->flag & CONSTRAINT_CHANNEL_SELECT){
1298                                                 sel=0;
1299                                                 break;
1300                                         }
1301                                 }
1302                         }
1303                 }
1304         }
1305         else
1306                 sel=0;
1307
1308         /* Now set the flags */
1309         for (chan=act->chanbase.first; chan; chan=chan->next){
1310                 select_poseelement_by_name(chan->name, sel);
1311
1312                 if (sel)
1313                         chan->flag |= ACHAN_SELECTED;
1314                 else
1315                         chan->flag &= ~ACHAN_SELECTED;
1316
1317                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
1318                         if (sel)
1319                                 conchan->flag |= CONSTRAINT_CHANNEL_SELECT;
1320                         else
1321                                 conchan->flag &= ~CONSTRAINT_CHANNEL_SELECT;
1322                 }
1323         }
1324
1325 }
1326
1327 static void hilight_channel (bAction *act, bActionChannel *chan, short select)
1328 {
1329         bActionChannel *curchan;
1330
1331         if (!act)
1332                 return;
1333
1334         for (curchan=act->chanbase.first; curchan; curchan=curchan->next){
1335                 if (curchan==chan && select)
1336                         curchan->flag |= ACHAN_HILIGHTED;
1337                 else
1338                         curchan->flag &= ~ACHAN_HILIGHTED;
1339         }
1340 }
1341
1342 /* select_mode = SELECT_REPLACE
1343  *             = SELECT_ADD
1344  *             = SELECT_SUBTRACT
1345  *             = SELECT_INVERT
1346  */
1347
1348 /* exported for outliner (ton) */
1349 /* apparently within active object context */
1350 int select_channel(bAction *act, bActionChannel *chan,
1351                           int selectmode) 
1352 {
1353         /* Select the channel based on the selection mode
1354          */
1355         int flag;
1356
1357         switch (selectmode) {
1358         case SELECT_ADD:
1359                 chan->flag |= ACHAN_SELECTED;
1360                 break;
1361         case SELECT_SUBTRACT:
1362                 chan->flag &= ~ACHAN_SELECTED;
1363                 break;
1364         case SELECT_INVERT:
1365                 chan->flag ^= ACHAN_SELECTED;
1366                 break;
1367         }
1368         flag = (chan->flag & ACHAN_SELECTED) ? 1 : 0;
1369
1370         hilight_channel(act, chan, flag);
1371         select_poseelement_by_name(chan->name, flag);
1372
1373         return flag;
1374 }
1375
1376 static int select_constraint_channel(bAction *act, 
1377                                      bConstraintChannel *conchan, 
1378                                      int selectmode) {
1379         /* Select the constraint channel based on the selection mode
1380          */
1381         int flag;
1382
1383         switch (selectmode) {
1384         case SELECT_ADD:
1385                 conchan->flag |= CONSTRAINT_CHANNEL_SELECT;
1386                 break;
1387         case SELECT_SUBTRACT:
1388                 conchan->flag &= ~CONSTRAINT_CHANNEL_SELECT;
1389                 break;
1390         case SELECT_INVERT:
1391                 conchan->flag ^= CONSTRAINT_CHANNEL_SELECT;
1392                 break;
1393         }
1394         flag = (conchan->flag & CONSTRAINT_CHANNEL_SELECT) ? 1 : 0;
1395
1396         return flag;
1397 }
1398
1399 /* lefthand side */
1400 static void mouse_actionchannels(bAction *act, short *mval,
1401                                  short *mvalo, int selectmode) {
1402         /* Select action channels, based on mouse values.
1403          * If mvalo is NULL we assume it is a one click
1404          * action, other wise we treat it like it is a
1405          * border select with mval[0],mval[1] and
1406          * mvalo[0], mvalo[1] forming the corners of
1407          * a rectangle.
1408          */
1409         bActionChannel *chan;
1410         float   click, x,y;
1411         int   clickmin, clickmax;
1412         int             wsize, sel;
1413         bConstraintChannel *conchan;
1414
1415         if (!act)
1416                 return;
1417   
1418         if (selectmode == SELECT_REPLACE) {
1419                 deselect_actionchannels (act, 0);
1420                 selectmode = SELECT_ADD;
1421         }
1422
1423         /* wsize is the greatest possible height (in pixels) that would be
1424          * needed to draw all of the action channels and constraint
1425          * channels.
1426          */
1427         wsize =  count_action_levels(act)*(CHANNELHEIGHT+CHANNELSKIP);
1428         wsize += CHANNELHEIGHT/2;
1429
1430     areamouseco_to_ipoco(G.v2d, mval, &x, &y);
1431     clickmin = (int) ((wsize - y) / (CHANNELHEIGHT+CHANNELSKIP));
1432         
1433         /* Only one click */
1434         if (mvalo == NULL) {
1435                 clickmax = clickmin;
1436         }
1437         /* Two click values (i.e., border select */
1438         else {
1439                 areamouseco_to_ipoco(G.v2d, mvalo, &x, &y);
1440                 click = ((wsize - y) / (CHANNELHEIGHT+CHANNELSKIP));
1441
1442                 if ( ((int) click) < clickmin) {
1443                         clickmax = clickmin;
1444                         clickmin = (int) click;
1445                 }
1446                 else {
1447                         clickmax = (int) click;
1448                 }
1449         }
1450
1451         if (clickmax < 0) {
1452                 return;
1453         }
1454
1455         /* clickmin and clickmax now coorespond to indices into
1456          * the collection of channels and constraint channels.
1457          * What we need to do is apply the selection mode on all
1458          * channels and constraint channels between these indices.
1459          * This is done by traversing the channels and constraint
1460          * channels, for each item decrementing clickmin and clickmax.
1461          * When clickmin is less than zero we start selecting stuff,
1462          * until clickmax is less than zero or we run out of channels
1463          * and constraint channels.
1464          */
1465
1466         for (chan = act->chanbase.first; chan; chan=chan->next){
1467                 if (clickmax < 0) break;
1468
1469                 if ( clickmin <= 0) {
1470                         /* Select the channel with the given mode. If the
1471                          * channel is freshly selected then set it to the
1472                          * active channel for the action
1473                          */
1474                         sel = (chan->flag & ACHAN_SELECTED);
1475                         select_channel(act, chan, selectmode);
1476                         /* messy... */
1477                         select_poseelement_by_name(chan->name, 2);
1478                         
1479                 }
1480                 --clickmin;
1481                 --clickmax;
1482
1483                 /* Check for click in a constraint */
1484                 for (conchan=chan->constraintChannels.first; 
1485                          conchan; conchan=conchan->next){
1486                         if (clickmax < 0) break;
1487                         if ( clickmin <= 0) {
1488                                 select_constraint_channel(act, conchan, selectmode);
1489                         }
1490                         --clickmin;
1491                         --clickmax;
1492                 }
1493         }
1494
1495         allqueue (REDRAWIPO, 0);
1496         allqueue (REDRAWVIEW3D, 0);
1497         allqueue (REDRAWACTION, 0);
1498         allqueue (REDRAWNLA, 0);
1499         allqueue (REDRAWOOPS, 0);
1500         allqueue (REDRAWBUTSALL, 0);
1501 }
1502
1503 void delete_meshchannel_keys(Key *key)
1504 {
1505         if (!okee("Erase selected keys"))
1506                 return;
1507
1508         BIF_undo_push("Delete Action keys");
1509         delete_ipo_keys(key->ipo);
1510
1511         meshkey_do_redraw(key);
1512 }
1513
1514 void delete_actionchannel_keys(void)
1515 {
1516         bAction *act;
1517         bActionChannel *chan;
1518         bConstraintChannel *conchan;
1519
1520         act = G.saction->action;
1521         if (!act)
1522                 return;
1523
1524         if (!okee("Erase selected keys"))
1525                 return;
1526
1527         for (chan = act->chanbase.first; chan; chan=chan->next){
1528
1529                 /* Check action channel keys*/
1530                 delete_ipo_keys(chan->ipo);
1531
1532                 /* Delete constraint channel keys */
1533                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
1534                         delete_ipo_keys(conchan->ipo);
1535         }
1536
1537         remake_action_ipos (act);
1538         BIF_undo_push("Delete Action keys");
1539         allspace(REMAKEIPO, 0);
1540         allqueue(REDRAWACTION, 0);
1541         allqueue(REDRAWIPO, 0);
1542         allqueue(REDRAWNLA, 0);
1543
1544 }
1545 static void delete_actionchannels (void)
1546 {
1547         bConstraintChannel *conchan=NULL, *nextconchan;
1548         bActionChannel *chan, *next;
1549         bAction *act;
1550         int freechan;
1551
1552         act=G.saction->action;
1553
1554         if (!act)
1555                 return;
1556
1557         for (chan=act->chanbase.first; chan; chan=chan->next){
1558                 if (chan->flag & ACHAN_SELECTED)
1559                         break;
1560                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
1561                 {
1562                         if (conchan->flag & CONSTRAINT_CHANNEL_SELECT){
1563                                 chan=act->chanbase.last;
1564                                 break;
1565                         }
1566                 }
1567         }
1568
1569         if (!chan && !conchan)
1570                 return;
1571
1572         if (!okee("Erase selected channels"))
1573                 return;
1574
1575         for (chan=act->chanbase.first; chan; chan=next){
1576                 freechan = 0;
1577                 next=chan->next;
1578                 
1579                 /* Remove action channels */
1580                 if (chan->flag & ACHAN_SELECTED){
1581                         if (chan->ipo)
1582                                 chan->ipo->id.us--;     /* Release the ipo */
1583                         freechan = 1;
1584                 }
1585                 
1586                 /* Remove constraint channels */
1587                 for (conchan=chan->constraintChannels.first; conchan; conchan=nextconchan){
1588                         nextconchan=conchan->next;
1589                         if (freechan || conchan->flag & CONSTRAINT_CHANNEL_SELECT){
1590                                 if (conchan->ipo)
1591                                         conchan->ipo->id.us--;
1592                                 BLI_freelinkN(&chan->constraintChannels, conchan);
1593                         }
1594                 }
1595                 
1596                 if (freechan)
1597                         BLI_freelinkN (&act->chanbase, chan);
1598
1599         }
1600
1601         BIF_undo_push("Delete Action channels");
1602         allqueue (REDRAWACTION, 0);
1603         allqueue(REDRAWNLA, 0);
1604
1605 }
1606
1607 void sethandles_meshchannel_keys(int code, Key *key)
1608 {
1609         
1610     sethandles_ipo_keys(key->ipo, code);
1611
1612         BIF_undo_push("Set handles Action keys");
1613         meshkey_do_redraw(key);
1614 }
1615
1616 void sethandles_actionchannel_keys(int code)
1617 {
1618         bAction *act;
1619         bActionChannel *chan;
1620
1621         /* Get the selected action, exit if none are selected 
1622          */
1623         act = G.saction->action;
1624         if (!act)
1625                 return;
1626
1627         /* Loop through the channels and set the beziers
1628          * of the selected keys based on the integer code
1629          */
1630         for (chan = act->chanbase.first; chan; chan=chan->next){
1631                 sethandles_ipo_keys(chan->ipo, code);
1632         }
1633
1634         /* Clean up and redraw stuff
1635          */
1636         remake_action_ipos (act);
1637         BIF_undo_push("Set handles Action channel");
1638         allspace(REMAKEIPO, 0);
1639         allqueue(REDRAWACTION, 0);
1640         allqueue(REDRAWIPO, 0);
1641         allqueue(REDRAWNLA, 0);
1642 }
1643
1644 void set_ipotype_actionchannels(int ipotype) {
1645
1646         bAction *act; 
1647         bActionChannel *chan;
1648         short event;
1649
1650         /* Get the selected action, exit if none are selected 
1651          */
1652         act = G.saction->action;
1653         if (!act)
1654                 return;
1655
1656         if (ipotype == SET_IPO_POPUP) {
1657                 /* Present a popup menu asking the user what type
1658                  * of IPO curve he/she/GreenBTH wants. ;)
1659                  */
1660                 event
1661                         =  pupmenu("Channel Ipo Type %t|"
1662                                            "Constant %x1|"
1663                                            "Linear %x2|"
1664                                            "Bezier %x3");
1665                 if(event < 1) return;
1666                 ipotype = event;
1667         }
1668         
1669         /* Loop through the channels and for the selected ones set
1670          * the type for each Ipo curve in the channel Ipo (based on
1671          * the value from the popup).
1672          */
1673         for (chan = act->chanbase.first; chan; chan=chan->next){
1674                 if (chan->flag & ACHAN_SELECTED){
1675                         if (chan->ipo)
1676                                 setipotype_ipo(chan->ipo, ipotype);
1677                 }
1678         }
1679
1680         /* Clean up and redraw stuff
1681          */
1682         remake_action_ipos (act);
1683         BIF_undo_push("Set Ipo type Action channel");
1684         allspace(REMAKEIPO, 0);
1685         allqueue(REDRAWACTION, 0);
1686         allqueue(REDRAWIPO, 0);
1687         allqueue(REDRAWNLA, 0);
1688 }
1689
1690 static void select_all_keys_frames(bAction *act, short *mval, 
1691                                                         short *mvalo, int selectmode) {
1692         
1693         /* This function tries to select all action keys in
1694          * every channel for a given range of keyframes that
1695          * are within the mouse values mval and mvalo (usually
1696          * the result of a border select). If mvalo is passed as
1697          * NULL then the selection is treated as a one-click and
1698          * the function tries to select all keys within half a
1699          * frame of the click point.
1700          */
1701         
1702         rcti rect;
1703         rctf rectf;
1704         bActionChannel *chan;
1705         bConstraintChannel *conchan;
1706
1707         if (!act)
1708                 return;
1709
1710         if (selectmode == SELECT_REPLACE) {
1711                 deselect_actionchannel_keys(act, 0);
1712                 selectmode = SELECT_ADD;
1713         }
1714
1715         if (mvalo == NULL) {
1716                 rect.xmin = rect.xmax = mval[0];
1717                 rect.ymin = rect.ymax = mval[1];
1718         }
1719         else {
1720                 if (mval[0] < mvalo[0] ) {
1721                         rect.xmin = mval[0];
1722                         rect.xmax = mvalo[0];
1723                 }
1724                 else {
1725                         rect.xmin = mvalo[0];
1726                         rect.xmax = mval[0];
1727                 }
1728                 if (mval[1] < mvalo[1] ) {
1729                         rect.ymin = mval[1];
1730                         rect.ymax = mvalo[1];
1731                 }
1732                 else {
1733                         rect.ymin = mvalo[1];
1734                         rect.ymax = mval[1];
1735                 }
1736         }
1737
1738         mval[0]= rect.xmin;
1739         mval[1]= rect.ymin+2;
1740         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
1741         mval[0]= rect.xmax;
1742         mval[1]= rect.ymax-2;
1743         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
1744
1745         if (mvalo == NULL) {
1746                 rectf.xmin = rectf.xmin - 0.5;
1747                 rectf.xmax = rectf.xmax + 0.5;
1748         }
1749     
1750         for (chan=act->chanbase.first; chan; chan=chan->next){
1751                 borderselect_ipo_key(chan->ipo, rectf.xmin, rectf.xmax,
1752                                                          selectmode);
1753                 for (conchan=chan->constraintChannels.first; conchan; 
1754                          conchan=conchan->next){
1755                         borderselect_ipo_key(conchan->ipo, rectf.xmin, rectf.xmax,
1756                                                                  selectmode);
1757                 }
1758         }       
1759
1760         allqueue(REDRAWNLA, 0);
1761         allqueue(REDRAWACTION, 0);
1762         allqueue(REDRAWIPO, 0);
1763 }
1764
1765
1766 static void select_all_keys_channels(bAction *act, short *mval, 
1767                               short *mvalo, int selectmode) {
1768         bActionChannel    *chan;
1769         float              click, x,y;
1770         int                clickmin, clickmax;
1771         int                wsize;
1772         bConstraintChannel *conchan;
1773
1774         /* This function selects all the action keys that
1775          * are in the mouse selection range defined by
1776          * the ordered pairs mval and mvalo (usually
1777          * these 2 are obtained from a border select).
1778          * If mvalo is NULL, then the selection is
1779          * treated like a one-click action, and at most
1780          * one channel is selected.
1781          */
1782
1783         /* If the action is null then abort
1784          */
1785         if (!act)
1786                 return;
1787
1788         if (selectmode == SELECT_REPLACE) {
1789                 deselect_actionchannel_keys(act, 0);
1790                 selectmode = SELECT_ADD;
1791         }
1792
1793         /* wsize is the greatest possible height (in pixels) that would be
1794          * needed to draw all of the action channels and constraint
1795          * channels.
1796          */
1797
1798         wsize =  count_action_levels(act)*(CHANNELHEIGHT+CHANNELSKIP);
1799         wsize += CHANNELHEIGHT/2;
1800         
1801     areamouseco_to_ipoco(G.v2d, mval, &x, &y);
1802     clickmin = (int) ((wsize - y) / (CHANNELHEIGHT+CHANNELSKIP));
1803         
1804         /* Only one click */
1805         if (mvalo == NULL) {
1806                 clickmax = clickmin;
1807         }
1808         /* Two click values (i.e., border select) */
1809         else {
1810
1811                 areamouseco_to_ipoco(G.v2d, mvalo, &x, &y);
1812                 click = ((wsize - y) / (CHANNELHEIGHT+CHANNELSKIP));
1813                 
1814                 if ( ((int) click) < clickmin) {
1815                         clickmax = clickmin;
1816                         clickmin = (int) click;
1817                 }
1818                 else {
1819                         clickmax = (int) click;
1820                 }
1821         }
1822
1823         if (clickmax < 0) {
1824                 return;
1825         }
1826
1827         for (chan = act->chanbase.first; chan; chan=chan->next){
1828                 if (clickmax < 0) break;
1829
1830                 if ( clickmin <= 0) {
1831                         /* Select the channel with the given mode. If the
1832                          * channel is freshly selected then set it to the
1833                          * active channel for the action
1834                          */
1835                         select_ipo_bezier_keys(chan->ipo, selectmode);
1836                 }
1837                 --clickmin;
1838                 --clickmax;
1839
1840                 /* Check for click in a constraint */
1841                 for (conchan=chan->constraintChannels.first; 
1842                          conchan; conchan=conchan->next){
1843                         if (clickmax < 0) break;
1844                         if ( clickmin <= 0) {
1845                                 select_ipo_bezier_keys(chan->ipo, selectmode);
1846                         }
1847                         --clickmin;
1848                         --clickmax;
1849                 }
1850         }
1851   
1852         allqueue (REDRAWIPO, 0);
1853         allqueue (REDRAWVIEW3D, 0);
1854         allqueue (REDRAWACTION, 0);
1855         allqueue (REDRAWNLA, 0);
1856   
1857 }
1858
1859 static void borderselect_function(void (*select_func)(bAction *act, 
1860                                                      short *mval, 
1861                                                      short *mvalo, 
1862                                                      int selectmode)) {
1863         /* This function executes an arbitrary selection
1864          * function as part of a border select. This
1865          * way the same function that is used for
1866          * right click selection points can generally
1867          * be used as the argument to this function
1868          */
1869         rcti rect;
1870         short   mval[2], mvalo[2];
1871         bAction *act;
1872         int val;                
1873
1874         /* Get the selected action, exit if none are selected 
1875          */
1876         act=G.saction->action;
1877         if (!act)
1878                 return;
1879
1880         /* Let the user draw a border (or abort)
1881          */
1882         if ( (val=get_border (&rect, 3)) ) {
1883                 mval[0]= rect.xmin;
1884                 mval[1]= rect.ymin+2;
1885                 mvalo[0]= rect.xmax;
1886                 mvalo[1]= rect.ymax-2;
1887
1888                 /* if the left mouse was used, do an additive
1889                  * selection with the user defined selection
1890                  * function.
1891                  */
1892                 if (val == LEFTMOUSE)
1893                         select_func(act, mval, mvalo, SELECT_ADD);
1894                 
1895                 /* if the right mouse was used, do a subtractive
1896                  * selection with the user defined selection
1897                  * function.
1898                  */
1899                 else if (val == RIGHTMOUSE)
1900                         select_func(act, mval, mvalo, SELECT_SUBTRACT);
1901         }
1902         BIF_undo_push("Border select Action");
1903         
1904 }
1905
1906 static void clever_keyblock_names(Key *key, short* mval){
1907     int        but=0, i, keynum;
1908     char       str[64];
1909         float      x;
1910         KeyBlock   *kb;
1911         /* get the keynum cooresponding to the y value
1912          * of the mouse pointer, return if this is
1913          * an invalid key number (and we don't deal
1914          * with the speed ipo).
1915          */
1916
1917     keynum = get_nearest_key_num(key, mval, &x);
1918     if ( (keynum < 1) || (keynum >= key->totkey) )
1919         return;
1920
1921         kb= key->block.first;
1922         for (i=0; i<keynum; ++i) kb = kb->next; 
1923
1924         if (kb->name[0] == '\0') {
1925                 sprintf(str, "Key %d", keynum);
1926         }
1927         else {
1928                 strcpy(str, kb->name);
1929         }
1930
1931         if ( (kb->slidermin >= kb->slidermax) ) {
1932                 kb->slidermin = 0.0;
1933                 kb->slidermax = 1.0;
1934         }
1935
1936     add_numbut(but++, TEX, "KB: ", 0, 24, str, 
1937                "Does this really need a tool tip?");
1938         add_numbut(but++, NUM|FLO, "Slider Min:", 
1939                            -10000, kb->slidermax, &kb->slidermin, 0);
1940         add_numbut(but++, NUM|FLO, "Slider Max:", 
1941                            kb->slidermin, 10000, &kb->slidermax, 0);
1942
1943     if (do_clever_numbuts(str, but, REDRAW)) {
1944                 strcpy(kb->name, str);
1945         allqueue (REDRAWACTION, 0);
1946                 allspace(REMAKEIPO, 0);
1947         allqueue (REDRAWIPO, 0);
1948         }
1949
1950         
1951 }
1952
1953 static void numbuts_action(void)
1954 {
1955         /* now called from action window event loop, plus reacts on mouseclick */
1956         /* removed Hos grunts for that reason! :) (ton) */
1957     Key *key;
1958     short mval[2];
1959
1960     if ( (key = get_action_mesh_key()) ) {
1961         getmouseco_areawin (mval);
1962                 if (mval[0]<NAMEWIDTH) {
1963                         clever_keyblock_names(key, mval);
1964                 }
1965     }
1966 }
1967
1968 void winqreadactionspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
1969 {
1970         extern void do_actionbuts(unsigned short event); // drawaction.c
1971         SpaceAction *saction;
1972         bAction *act;
1973         Key *key;
1974         float dx,dy;
1975         int doredraw= 0;
1976         int     cfra;
1977         short   mval[2];
1978         unsigned short event= evt->event;
1979         short val= evt->val;
1980         short mousebut = L_MOUSE;
1981
1982         if(curarea->win==0) return;
1983
1984         saction= curarea->spacedata.first;
1985         if (!saction)
1986                 return;
1987
1988         act=saction->action;
1989         if(val) {
1990                 
1991                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
1992                 
1993                 /* swap mouse buttons based on user preference */
1994                 if (U.flag & USER_LMOUSESELECT) {
1995                         if (event == LEFTMOUSE) {
1996                                 event = RIGHTMOUSE;
1997                                 mousebut = L_MOUSE;
1998                         } else if (event == RIGHTMOUSE) {
1999                                 event = LEFTMOUSE;
2000                                 mousebut = R_MOUSE;
2001                         }
2002                 }
2003                 
2004                 getmouseco_areawin(mval);
2005
2006                 key = get_action_mesh_key();
2007
2008                 switch(event) {
2009                 case UI_BUT_EVENT:
2010                         do_actionbuts(val);     // window itself
2011                         break;
2012                 
2013                 case HOMEKEY:
2014                         do_action_buttons(B_ACTHOME);   // header
2015                         break;
2016
2017                 case AKEY:
2018                         if (key) {
2019                                 if (mval[0]<ACTWIDTH){
2020                                         /* to do ??? */
2021                                 }
2022                                 else{
2023                                         deselect_meshchannel_keys(key, 1);
2024                                         allqueue (REDRAWACTION, 0);
2025                                         allqueue(REDRAWNLA, 0);
2026                                         allqueue (REDRAWIPO, 0);
2027                                 }
2028                         }
2029                         else {
2030                                 if (mval[0]<NAMEWIDTH){
2031                                         deselect_actionchannels (act, 1);
2032                                         allqueue (REDRAWVIEW3D, 0);
2033                                         allqueue (REDRAWACTION, 0);
2034                                         allqueue(REDRAWNLA, 0);
2035                                         allqueue (REDRAWIPO, 0);
2036                                 }
2037                                 else if (mval[0]>ACTWIDTH){
2038                                         deselect_actionchannel_keys (act, 1);
2039                                         allqueue (REDRAWACTION, 0);
2040                                         allqueue(REDRAWNLA, 0);
2041                                         allqueue (REDRAWIPO, 0);
2042                                 }
2043                         }
2044                         break;
2045
2046                 case BKEY:
2047                         if (key) {
2048                                 if (mval[0]<ACTWIDTH){
2049                                         /* to do?? */
2050                                 }
2051                                 else {
2052                                         borderselect_mesh(key);
2053                                 }
2054                         }
2055                         else {
2056
2057                                 /* If the border select is initiated in the
2058                                  * part of the action window where the channel
2059                                  * names reside, then select the channels
2060                                  */
2061                                 if (mval[0]<NAMEWIDTH){
2062                                         borderselect_function(mouse_actionchannels);
2063                                         BIF_undo_push("Select Action");
2064                                 }
2065                                 else if (mval[0]>ACTWIDTH){
2066
2067                                         /* If the border select is initiated in the
2068                                          * vertical scrollbar, then (de)select all keys
2069                                          * for the channels in the selection region
2070                                          */
2071                                         if (IN_2D_VERT_SCROLL(mval)) {
2072                                                 borderselect_function(select_all_keys_channels);
2073                                         }
2074
2075                                         /* If the border select is initiated in the
2076                                          * horizontal scrollbar, then (de)select all keys
2077                                          * for the keyframes in the selection region
2078                                          */
2079                                         else if (IN_2D_HORIZ_SCROLL(mval)) {
2080                                                 borderselect_function(select_all_keys_frames);
2081                                         }
2082
2083                                         /* Other wise, select the action keys
2084                                          */
2085                                         else {
2086                                                 borderselect_action();
2087                                         }
2088                                 }
2089                         }
2090                         break;
2091
2092                 case CKEY:
2093                         /* scroll the window so the current
2094                          * frame is in the center.
2095                          */
2096                         center_currframe();
2097                         break;
2098
2099                 case DKEY:
2100                         if (key) {
2101                                 if (G.qual & LR_SHIFTKEY && mval[0]>ACTWIDTH) {
2102                                         duplicate_meshchannel_keys(key);
2103                                 }
2104                         }
2105                         else if (act) {
2106                                 if (G.qual & LR_SHIFTKEY && mval[0]>ACTWIDTH){
2107                                         duplicate_actionchannel_keys();
2108                                         remake_action_ipos(act);
2109                                 }
2110                         }
2111                         break;
2112
2113                 case GKEY:
2114                         if (mval[0]>=ACTWIDTH) {
2115                                 if (key) {
2116                                         transform_meshchannel_keys('g', key);
2117                                 }
2118                                 else if (act) {
2119                                         transform_actionchannel_keys ('g', 0);
2120                                 }
2121                         }
2122                         break;
2123
2124                 case HKEY:
2125                         if(G.qual & LR_SHIFTKEY) {
2126                                 if(okee("Set Keys to Auto Handle")) {
2127                                         if (key)
2128                                                 sethandles_meshchannel_keys(HD_AUTO, key);
2129                                         else
2130                                                 sethandles_actionchannel_keys(HD_AUTO);
2131                                 }
2132                         }
2133                         else {
2134                                 if(okee("Toggle Keys Aligned Handle")) {
2135                                         if (key)
2136                                                 sethandles_meshchannel_keys(HD_ALIGN, key);
2137                                         else
2138                                                 sethandles_actionchannel_keys(HD_ALIGN);
2139                                 }
2140                         }
2141                         break;
2142                 
2143                 case NKEY:
2144                         if(G.qual==0) {
2145                                 numbuts_action();
2146                                 
2147                                 /* no panel (yet). current numbuts are not easy to put in panel... */
2148                                 //add_blockhandler(curarea, ACTION_HANDLER_PROPERTIES, UI_PNL_TO_MOUSE);
2149                                 //scrarea_queue_winredraw(curarea);
2150                         }
2151                         break;
2152                         
2153                 case SKEY: 
2154                         if (mval[0]>=ACTWIDTH) {
2155                                 if (key) {
2156                                         transform_meshchannel_keys('s', key);
2157                                 }
2158                                 else if (act) {
2159                                         transform_actionchannel_keys ('s', 0);
2160                                 }
2161                         }
2162                         break;
2163
2164                         /*** set the Ipo type  ***/
2165                 case TKEY:
2166                         if (key) {
2167                                 /* to do */
2168                         }
2169                         else {
2170                                 set_ipotype_actionchannels(SET_IPO_POPUP);
2171                         }
2172                         break;
2173
2174                 case VKEY:
2175                         if(okee("Set Keys to Vector Handle")) {
2176                                 if (key)
2177                                         sethandles_meshchannel_keys(HD_VECT, key);
2178                                 else
2179                                         sethandles_actionchannel_keys(HD_VECT);
2180                         }
2181                         break;
2182
2183                 case PAGEUPKEY:
2184                         if (key) {
2185                                 /* to do */
2186                         }
2187                         else {
2188                                 if(G.qual & LR_SHIFTKEY) {
2189                                         top_sel_action();
2190                                 }
2191                                 else
2192                                 {
2193                                         up_sel_action();
2194                                 }
2195                                 
2196                         }
2197                         break;
2198                 case PAGEDOWNKEY:
2199                         if (key) {
2200                                 /* to do */
2201                         }
2202                         else {
2203                                 if(G.qual & LR_SHIFTKEY) {
2204                                         bottom_sel_action();
2205                                 }
2206                                 else
2207                                 down_sel_action();
2208                                 
2209                         }
2210                         break;
2211                 case DELKEY:
2212                 case XKEY:
2213                         if (key) {
2214                                 delete_meshchannel_keys(key);
2215                         }
2216                         else {
2217                                 if (mval[0]<NAMEWIDTH)
2218                                         delete_actionchannels ();
2219                                 else
2220                                         delete_actionchannel_keys ();
2221                         }
2222                         break;
2223                 /* LEFTMOUSE and RIGHTMOUSE event codes can be swapped above,
2224                  * based on user preference USER_LMOUSESELECT
2225                  */
2226                 case LEFTMOUSE:
2227                         if(view2dmove(LEFTMOUSE)) // only checks for sliders
2228                                 break;
2229                         else if (mval[0]>ACTWIDTH){
2230                                 do {
2231                                         getmouseco_areawin(mval);
2232                                         
2233                                         areamouseco_to_ipoco(G.v2d, mval, &dx, &dy);
2234                                         
2235                                         cfra= (int)dx;
2236                                         if(cfra< 1) cfra= 1;
2237                                         
2238                                         if( cfra!=CFRA ) {
2239                                                 CFRA= cfra;
2240                                                 update_for_newframe();
2241                                                 force_draw_all(0);                                      }
2242                                         else PIL_sleep_ms(30);
2243                                         
2244                                 } while(get_mbut() & mousebut);
2245                                 break;
2246                         }
2247                         /* passed on as selection */
2248                 case RIGHTMOUSE:
2249                         /* Clicking in the channel area selects the
2250                          * channel or constraint channel
2251                          */
2252                         if (mval[0]<NAMEWIDTH) {
2253                                 if(act) {
2254                                         if(G.qual & LR_SHIFTKEY)
2255                                                 mouse_actionchannels(act, mval, NULL,  SELECT_INVERT);
2256                                         else
2257                                                 mouse_actionchannels(act, mval, NULL,  SELECT_REPLACE);
2258                                         
2259                                         BIF_undo_push("Select Action");
2260                                 }
2261                                 else numbuts_action();
2262                         }
2263                         else if (mval[0]>ACTWIDTH) {
2264                 
2265                                 /* Clicking in the vertical scrollbar selects
2266                                  * all of the keys for that channel at that height
2267                                  */
2268                                 if (IN_2D_VERT_SCROLL(mval)) {
2269                                         if(G.qual & LR_SHIFTKEY)
2270                                                 select_all_keys_channels(act, mval, NULL, 
2271                                                                                                  SELECT_INVERT);
2272                                         else
2273                                                 select_all_keys_channels(act, mval, NULL, 
2274                                                                                                  SELECT_REPLACE);
2275                                 }
2276                 
2277                                 /* Clicking in the horizontal scrollbar selects
2278                                  * all of the keys within 0.5 of the nearest integer
2279                                  * frame
2280                                  */
2281                                 else if (IN_2D_HORIZ_SCROLL(mval)) {
2282                                         if(G.qual & LR_SHIFTKEY)
2283                                                 select_all_keys_frames(act, mval, NULL, 
2284                                                                                            SELECT_INVERT);
2285                                         else
2286                                                 select_all_keys_frames(act, mval, NULL, 
2287                                                                                            SELECT_REPLACE);
2288                                         BIF_undo_push("Select all Action");
2289                                 }
2290                                 
2291                                 /* Clicking in the main area of the action window
2292                                  * selects keys
2293                                  */
2294                                 else {
2295                                         if (key) {
2296                                                 if(G.qual & LR_SHIFTKEY)
2297                                                         mouse_mesh_action(SELECT_INVERT, key);
2298                                                 else
2299                                                         mouse_mesh_action(SELECT_REPLACE, key);
2300                                         }
2301                                         else {
2302                                                 if(G.qual & LR_SHIFTKEY)
2303                                                         mouse_action(SELECT_INVERT);
2304                                                 else
2305                                                         mouse_action(SELECT_REPLACE);
2306                                         }
2307                                 }
2308                         }
2309                         break;
2310                 case PADPLUSKEY:
2311                         view2d_zoom(G.v2d, 0.1154, sa->winx, sa->winy);
2312                         test_view2d(G.v2d, sa->winx, sa->winy);
2313                         view2d_do_locks(curarea, V2D_LOCK_COPY);
2314
2315                         doredraw= 1;
2316                         break;
2317                 case PADMINUS:
2318                         view2d_zoom(G.v2d, -0.15, sa->winx, sa->winy);
2319                         test_view2d(G.v2d, sa->winx, sa->winy);
2320                         view2d_do_locks(curarea, V2D_LOCK_COPY);
2321                         
2322                         doredraw= 1;
2323                         break;
2324                 case MIDDLEMOUSE:
2325                 case WHEELUPMOUSE:
2326                 case WHEELDOWNMOUSE:
2327                         view2dmove(event);      /* in drawipo.c */
2328                         break;
2329                 }
2330         }
2331
2332         if(doredraw) addqueue(curarea->win, REDRAW, 1);
2333         
2334 }
2335
2336 Key *get_action_mesh_key(void) 
2337 {
2338         /* gets the key data from the currently selected
2339          * mesh/lattice. If a mesh is not selected, or does not have
2340          * key data, then we return NULL (currently only
2341          * returns key data for RVK type meshes). If there
2342          * is an action that is pinned, return null
2343          */
2344     Object *ob;
2345     Key    *key;
2346
2347     ob = OBACT;
2348     if (!ob) return NULL;
2349
2350         if (G.saction->pin) return NULL;
2351
2352     if (ob->type==OB_MESH ) {
2353                 key = ((Mesh *)ob->data)->key;
2354     }
2355         else if (ob->type==OB_LATTICE ) {
2356                 key = ((Lattice *)ob->data)->key;
2357         }
2358         else return NULL;
2359
2360         if (key) {
2361                 if (key->type == KEY_RELATIVE)
2362                         return key;
2363         }
2364
2365     return NULL;
2366 }
2367
2368 int get_nearest_key_num(Key *key, short *mval, float *x) {
2369         /* returns the key num that cooresponds to the
2370          * y value of the mouse click. Does not check
2371          * if this is a valid keynum. Also gives the Ipo
2372          * x coordinate.
2373          */
2374     int num;
2375     float ybase, y;
2376
2377     areamouseco_to_ipoco(G.v2d, mval, x, &y);
2378
2379     ybase = key->totkey * (CHANNELHEIGHT + CHANNELSKIP);
2380     num = (int) ((ybase - y + CHANNELHEIGHT/2) / (CHANNELHEIGHT+CHANNELSKIP));
2381
2382     return (num + 1);
2383 }
2384
2385
2386
2387 void top_sel_action()
2388 {
2389         bAction *act;
2390         bActionChannel *chan;
2391         
2392         /* Get the selected action, exit if none are selected */
2393         act = G.saction->action;
2394         if (!act) return;
2395         
2396         for (chan=act->chanbase.first; chan; chan=chan->next){
2397                 if ((chan->flag & ACHAN_SELECTED) && !(chan->flag & ACHAN_MOVED)){
2398                         /* take it out off the chain keep data */
2399                         BLI_remlink (&act->chanbase, chan);
2400                         /* make it first element */
2401                         BLI_insertlinkbefore(&act->chanbase,act->chanbase.first, chan);
2402                         chan->flag |= ACHAN_MOVED;
2403                         /* restart with rest of list */
2404                         chan=chan->next;
2405                 }
2406         }
2407     /* clear temp flags */
2408         for (chan=act->chanbase.first; chan; chan=chan->next){
2409                 chan->flag = chan->flag & ~ACHAN_MOVED;
2410         }
2411         
2412         /* Clean up and redraw stuff */
2413         remake_action_ipos (act);
2414         BIF_undo_push("Top Action channel");
2415         allspace(REMAKEIPO, 0);
2416         allqueue(REDRAWACTION, 0);
2417         allqueue(REDRAWIPO, 0);
2418         allqueue(REDRAWNLA, 0);
2419 }
2420
2421 void up_sel_action()
2422 {
2423         bAction *act;
2424         bActionChannel *chan;
2425         bActionChannel *prev;
2426         
2427         /* Get the selected action, exit if none are selected */
2428         act = G.saction->action;
2429         if (!act) return;
2430         
2431         for (chan=act->chanbase.first; chan; chan=chan->next){
2432                 if ((chan->flag & ACHAN_SELECTED) && !(chan->flag & ACHAN_MOVED)){
2433                         prev = chan->prev;
2434                         if (prev){
2435                                 /* take it out off the chain keep data */
2436                                 BLI_remlink (&act->chanbase, chan);
2437                                 /* push it up */
2438                                 BLI_insertlinkbefore(&act->chanbase,prev, chan);
2439                                 chan->flag |= ACHAN_MOVED;
2440                                 /* restart with rest of list */
2441                                 chan=chan->next;
2442                         }
2443                 }
2444         }
2445         /* clear temp flags */
2446         for (chan=act->chanbase.first; chan; chan=chan->next){
2447                 chan->flag = chan->flag & ~ACHAN_MOVED;
2448         }
2449         
2450         /* Clean up and redraw stuff
2451         */
2452         remake_action_ipos (act);
2453         BIF_undo_push("Up Action channel");
2454         allspace(REMAKEIPO, 0);
2455         allqueue(REDRAWACTION, 0);
2456         allqueue(REDRAWIPO, 0);
2457         allqueue(REDRAWNLA, 0);
2458 }
2459
2460 void down_sel_action()
2461 {
2462         bAction *act;
2463         bActionChannel *chan;
2464         bActionChannel *next;
2465         
2466         /* Get the selected action, exit if none are selected */
2467         act = G.saction->action;
2468         if (!act) return;
2469         
2470         for (chan=act->chanbase.last; chan; chan=chan->prev){
2471                 if ((chan->flag & ACHAN_SELECTED) && !(chan->flag & ACHAN_MOVED)){
2472                         next = chan->next;
2473                         if (next) next = next->next;
2474                         if (next){
2475                                 /* take it out off the chain keep data */
2476                                 BLI_remlink (&act->chanbase, chan);
2477                                 /* move it down */
2478                                 BLI_insertlinkbefore(&act->chanbase,next, chan);
2479                                 chan->flag |= ACHAN_MOVED;
2480                         }
2481                         else {
2482                                 /* take it out off the chain keep data */
2483                                 BLI_remlink (&act->chanbase, chan);
2484                                 /* add at end */
2485                                 BLI_addtail(&act->chanbase,chan);
2486                                 chan->flag |= ACHAN_MOVED;
2487                         }
2488
2489                 }
2490         }
2491         /* clear temp flags */
2492         for (chan=act->chanbase.first; chan; chan=chan->next){
2493                 chan->flag = chan->flag & ~ACHAN_MOVED;
2494         }
2495         
2496         /* Clean up and redraw stuff
2497         */
2498         remake_action_ipos (act);
2499         BIF_undo_push("Down Action channel");
2500         allspace(REMAKEIPO, 0);
2501         allqueue(REDRAWACTION, 0);
2502         allqueue(REDRAWIPO, 0);
2503         allqueue(REDRAWNLA, 0);
2504 }
2505
2506 void bottom_sel_action()
2507 {
2508         bAction *act;
2509         bActionChannel *chan;
2510         
2511         /* Get the selected action, exit if none are selected */
2512         act = G.saction->action;
2513         if (!act) return;
2514         
2515         for (chan=act->chanbase.last; chan; chan=chan->prev){
2516                 if ((chan->flag & ACHAN_SELECTED) && !(chan->flag & ACHAN_MOVED)) {
2517                         /* take it out off the chain keep data */
2518                         BLI_remlink (&act->chanbase, chan);
2519                         /* add at end */
2520                         BLI_addtail(&act->chanbase,chan);
2521                         chan->flag |= ACHAN_MOVED;
2522                 }
2523                 
2524         }
2525         /* clear temp flags */
2526         for (chan=act->chanbase.first; chan; chan=chan->next){
2527                 chan->flag = chan->flag & ~ACHAN_MOVED;
2528         }
2529         
2530         /* Clean up and redraw stuff
2531         */
2532         remake_action_ipos (act);
2533         BIF_undo_push("Bottom Action channel");
2534         allspace(REMAKEIPO, 0);
2535         allqueue(REDRAWACTION, 0);
2536         allqueue(REDRAWIPO, 0);
2537         allqueue(REDRAWNLA, 0);
2538 }