Bugfix - Ipo-Editor Scaling:
[blender.git] / source / blender / src / editipo.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL 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.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2005. Full recode.
24  * Roland Hess, 2007. Visual Key refactor.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29
30 /* this code feels over-complex, mostly because I choose in the past to devise a system
31   that converts the Ipo blocks (linked to Object, Material, etc), into a copy of that
32   data which is being worked on;  the 'editipo'.
33   The editipo then can have 'ipokey' data, which is optimized for editing curves as if
34   it were key positions. This is still a great feature to work with, which makes ipo editing
35   in Blender still valuable. However, getting this beast under control was hard, even
36   for me... (ton) */
37
38 #include <stdlib.h>
39 #include <string.h>
40 #include <math.h>
41
42 #ifndef WIN32
43 #include <unistd.h>
44 #else
45 #include <io.h>
46 #endif   
47 #include "MEM_guardedalloc.h"
48 #include "PIL_time.h"
49
50 #include "BLI_blenlib.h"
51 #include "BLI_arithb.h"
52
53 #include "DNA_constraint_types.h"
54 #include "DNA_action_types.h"
55 #include "DNA_armature_types.h"
56 #include "DNA_camera_types.h"
57 #include "DNA_curve_types.h"
58 #include "DNA_group_types.h"
59 #include "DNA_ipo_types.h"
60 #include "DNA_key_types.h"
61 #include "DNA_lamp_types.h"
62 #include "DNA_material_types.h"
63 #include "DNA_object_types.h"
64 #include "DNA_object_fluidsim.h"
65 #include "DNA_screen_types.h"
66 #include "DNA_scene_types.h"
67 #include "DNA_space_types.h"
68 #include "DNA_sequence_types.h"
69 #include "DNA_sound_types.h"
70 #include "DNA_texture_types.h"
71 #include "DNA_userdef_types.h"
72 #include "DNA_view3d_types.h"
73 #include "DNA_world_types.h"
74
75 #include "BKE_action.h"
76 #include "BKE_armature.h"
77 #include "BKE_anim.h"
78 #include "BKE_constraint.h"
79 #include "BKE_depsgraph.h"
80 #include "BKE_global.h"
81 #include "BKE_group.h"
82 #include "BKE_ipo.h"
83 #include "BKE_key.h"
84 #include "BKE_material.h"
85 #include "BKE_texture.h"
86 #include "BKE_utildefines.h"
87
88 #include "BIF_butspace.h"
89 #include "BIF_editaction.h"
90 #include "BIF_editconstraint.h"
91 #include "BIF_editkey.h"
92 #include "BIF_editnla.h"
93 #include "BIF_editseq.h"
94 #include "BIF_editview.h"
95 #include "BIF_interface.h"
96 #include "BIF_mywindow.h"
97 #include "BIF_poseobject.h"
98 #include "BIF_screen.h"
99 #include "BIF_space.h"
100 #include "BIF_toolbox.h"
101 #include "BIF_poseobject.h"
102
103 #include "BDR_drawobject.h"
104 #include "BDR_editobject.h"
105 #include "BDR_editcurve.h"      // for bezt_compare 
106
107 #include "BSE_trans_types.h"
108 #include "BSE_editipo_types.h"
109 #include "BSE_drawipo.h"
110 #include "BSE_editipo.h"
111 #include "BSE_edit.h"
112 #include "BSE_drawview.h"
113 #include "BSE_headerbuttons.h"
114 #include "BSE_node.h"
115 #include "BSE_sequence.h"
116 #include "BSE_time.h"
117
118 #include "blendef.h"
119 #include "mydevice.h"
120 #include "transform.h"
121
122 extern int ob_ar[];
123 extern int ma_ar[];
124 extern int seq_ar[];
125 extern int cu_ar[];
126 extern int wo_ar[];
127 extern int la_ar[];
128 extern int cam_ar[];
129 extern int snd_ar[];
130 extern int ac_ar[];
131 extern int co_ar[];
132 extern int te_ar[];
133 extern int fluidsim_ar[]; // NT
134
135 /* forwards */
136 #define IPOTHRESH       0.9
137
138 /* tests if only one editipo is active */
139 static void check_active_editipo(void)
140 {
141         EditIpo *ei, *actei;
142         int a;
143         
144         actei= G.sipo->editipo;
145         if(actei) {
146                 for(a=0; a<G.sipo->totipo; a++, actei++) {
147                         if(actei->flag & IPO_ACTIVE) 
148                                 break;
149                 }
150                 if(actei==NULL) {
151                         /* set visible active */
152                         for(a=0, ei=G.sipo->editipo; a<G.sipo->totipo; a++, ei++) {
153                                 if(ei->flag & IPO_VISIBLE)
154                                         break;
155                         }
156                         if(ei==NULL) ei=G.sipo->editipo;
157                         ei->flag |= IPO_ACTIVE;
158                         if(ei->icu) ei->icu->flag |= IPO_ACTIVE;
159                 }
160                 else {
161                         /* make sure no others are active */
162                         for(a=0, ei=G.sipo->editipo; a<G.sipo->totipo; a++, ei++) {
163                                 if(ei!=actei) {
164                                         ei->flag &= ~IPO_ACTIVE;
165                                         if(ei->icu) ei->icu->flag &= ~IPO_ACTIVE;
166                                 }
167                         }
168                 }
169         }
170 }
171
172 /* sets this ei channel active */
173 static void set_active_editipo(EditIpo *actei)
174 {
175         EditIpo *ei;
176         int a;
177         
178         for(a=0, ei=G.sipo->editipo; a<G.sipo->totipo; a++, ei++) {
179                 ei->flag &= ~IPO_ACTIVE;
180                 if(ei->icu) ei->icu->flag &= ~IPO_ACTIVE;
181         }
182         actei->flag |= IPO_ACTIVE;
183         if(actei->icu) actei->icu->flag |= IPO_ACTIVE;
184 }
185
186 EditIpo *get_active_editipo(void)
187 {
188         EditIpo *ei;
189         int a;
190         
191         if(G.sipo==NULL)
192                 return NULL;
193         
194         /* prevent confusing situations, like for sequencer */
195         if(G.sipo->totipo==1) {
196                 ei= G.sipo->editipo;
197                 ei->flag |= IPO_ACTIVE;
198                 return ei;
199         }
200         for(a=0, ei=G.sipo->editipo; a<G.sipo->totipo; a++, ei++)
201                 if(ei->flag & IPO_ACTIVE)
202                         return ei;
203         
204         return NULL;
205 }
206
207 static void set_active_key(int index)
208 {
209         if(G.sipo->blocktype==ID_KE && G.sipo->from) {
210                 Object *ob= (Object *)G.sipo->from;
211                 Key *key= ob_get_key(ob);
212                 
213                 if(key) {
214                         KeyBlock *curkb;
215                         
216                         curkb= BLI_findlink(&key->block, index-1);
217                         if(curkb) {
218                                 ob->shapenr= index;
219                                 ob->shapeflag |= OB_SHAPE_TEMPLOCK;
220                                 
221                                 /* calc keypos */
222                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
223                                 allqueue(REDRAWVIEW3D, 0);
224                                 allqueue(REDRAWBUTSEDIT, 0);
225                         }
226                 }
227         }                       
228 }
229
230 void editipo_changed(SpaceIpo *si, int doredraw)
231 {
232         EditIpo *ei;
233         View2D *v2d;
234         Key *key;
235         KeyBlock *kb;
236         int a, first=1;
237
238         ei= si->editipo;
239         if(ei==0)
240                 return;
241         
242         for(a=0; a<si->totipo; a++, ei++) {
243                 
244                 if(ei->icu) {
245                         
246                                 /* twice because of ittererating new autohandle */
247                         calchandles_ipocurve(ei->icu);
248                         calchandles_ipocurve(ei->icu);
249                         
250                         if(ei->flag & IPO_VISIBLE) {
251                 
252                                 boundbox_ipocurve(ei->icu, 0);
253                                 sort_time_ipocurve(ei->icu);
254                                 if(first) {
255                                         si->v2d.tot= ei->icu->totrct;
256                                         first= 0;
257                                 }
258                                 else BLI_union_rctf(&(si->v2d.tot), &(ei->icu->totrct));
259                         }
260                 }
261         }
262         
263
264         v2d= &(si->v2d);        
265
266         /* keylines? */
267         if(si->blocktype==ID_KE) {
268                 key= ob_get_key((Object *)G.sipo->from);
269                 if(key && key->block.first) {
270                         kb= key->block.first;
271                         if(kb->pos < v2d->tot.ymin) v2d->tot.ymin= kb->pos;
272                         kb= key->block.last;
273                         if(kb->pos > v2d->tot.ymax) v2d->tot.ymax= kb->pos;
274                 }
275         }
276         
277         /* is there no curve? */
278         if(first) {
279                 v2d->tot.xmin= 0.0;
280                 v2d->tot.xmax= EFRA;
281                 v2d->tot.ymin= (float)-0.1;
282                 v2d->tot.ymax= (float)1.1;
283         
284                 if(si->blocktype==ID_SEQ) {
285                         v2d->tot.xmin= -5.0;
286                         v2d->tot.xmax= 105.0;
287                         v2d->tot.ymin= (float)-0.1;
288                         v2d->tot.ymax= (float)1.1;
289                 }
290         }
291         
292         si->tot= v2d->tot;      
293         
294         if(doredraw) {
295                 /* if you always call do_ipo: you get problems with insertkey, for example
296                  * when inserting only a 'loc' the 'ob->rot' value then is changed.
297                  */
298
299                 if(si->blocktype==ID_OB) {                      
300                                 /* clear delta loc,rot,size (when free/delete ipo) */
301                         clear_delta_obipo(si->ipo);
302                         
303                 }
304         
305                 do_ipo(si->ipo);
306
307                 allqueue(REDRAWIPO, 0);
308                 allqueue(REDRAWACTION, 0);
309                 allqueue(REDRAWTIME, 0);
310                 allqueue(REDRAWNLA, 0);
311                 allqueue(REDRAWBUTSOBJECT, 0);
312                 
313                 if(si->blocktype==ID_OB) {
314                         Object *ob= (Object *)si->from;                 
315                         if(ob) DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
316                         allqueue(REDRAWVIEW3D, 0);
317                         allqueue(REDRAWNLA, 0);
318                 }
319
320                 else if(si->blocktype==ID_MA) allqueue(REDRAWBUTSSHADING, 0);
321                 else if(si->blocktype==ID_TE) allqueue(REDRAWBUTSSHADING, 0);
322                 else if(si->blocktype==ID_WO) allqueue(REDRAWBUTSSHADING, 0);
323                 else if(si->blocktype==ID_LA) allqueue(REDRAWBUTSSHADING, 0);
324 //              else if(si->blocktype==ID_SO) allqueue(REDRAWBUTSSOUND, 0);
325                 else if(si->blocktype==ID_CA) {
326                         allqueue(REDRAWBUTSEDIT, 0);
327                         allqueue(REDRAWVIEW3D, 0);
328                 }
329                 else if(si->blocktype==ID_SEQ) free_imbuf_seq_with_ipo(si->ipo);
330                 else if(si->blocktype==ID_PO) {
331                         Object *ob= OBACT;
332                         if(ob && ob->pose) {
333                                 DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
334                         }
335                         allqueue(REDRAWVIEW3D, 0);
336                         allqueue(REDRAWACTION, 0);
337                         allqueue(REDRAWNLA, 0);
338                 }
339                 else if(si->blocktype==ID_KE) {
340                         DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
341                         allqueue(REDRAWVIEW3D, 0);
342                 }
343                 else if(si->blocktype==ID_CU) {
344                         DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
345                         allqueue(REDRAWVIEW3D, 0);
346                 }
347         }
348
349         if(si->showkey) make_ipokey();
350         
351         if(si->actname[0])
352                 synchronize_action_strips();
353 }
354
355 void scale_editipo(void)
356 {
357         /* comes from buttons, scale with G.sipo->tot rect */
358         
359         EditIpo *ei;
360         BezTriple *bezt;
361         float facx, facy;
362         int a, b;       
363         
364         facx= (G.sipo->tot.xmax-G.sipo->tot.xmin)/(G.sipo->v2d.tot.xmax-G.sipo->v2d.tot.xmin);
365         facy= (G.sipo->tot.ymax-G.sipo->tot.ymin)/(G.sipo->v2d.tot.ymax-G.sipo->v2d.tot.ymin);
366
367         ei= G.sipo->editipo;
368         if(ei==0) return;
369         for(a=0; a<G.sipo->totipo; a++, ei++) {
370                 if (ISPOIN(ei, flag & IPO_VISIBLE, icu)) {
371                         bezt= ei->icu->bezt;
372                         b= ei->icu->totvert;
373                         while(b--) {
374                                 
375                                 bezt->vec[0][0]= facx*(bezt->vec[0][0] - G.sipo->v2d.tot.xmin) + G.sipo->tot.xmin;
376                                 bezt->vec[1][0]= facx*(bezt->vec[1][0] - G.sipo->v2d.tot.xmin) + G.sipo->tot.xmin;
377                                 bezt->vec[2][0]= facx*(bezt->vec[2][0] - G.sipo->v2d.tot.xmin) + G.sipo->tot.xmin;
378                         
379                                 bezt->vec[0][1]= facy*(bezt->vec[0][1] - G.sipo->v2d.tot.ymin) + G.sipo->tot.ymin;
380                                 bezt->vec[1][1]= facy*(bezt->vec[1][1] - G.sipo->v2d.tot.ymin) + G.sipo->tot.ymin;
381                                 bezt->vec[2][1]= facy*(bezt->vec[2][1] - G.sipo->v2d.tot.ymin) + G.sipo->tot.ymin;
382
383                                 bezt++;
384                         }
385                 }
386         }
387
388         editipo_changed(G.sipo, 1);
389
390         BIF_undo_push("Scale Edit Ipo");
391         allqueue(REDRAWNLA, 0);
392         allqueue (REDRAWACTION, 0);
393         allqueue(REDRAWIPO, 0);
394 }
395
396 static void make_ob_editipo(Object *ob, SpaceIpo *si)
397 {
398         EditIpo *ei;
399         int a, len, colipo=0;
400         char *name;
401         
402         if(ob->type==OB_MESH) colipo= 1;
403
404         ei= si->editipo= MEM_callocN(OB_TOTIPO*sizeof(EditIpo), "editipo");
405         
406         si->totipo= OB_TOTIPO;
407         
408         for(a=0; a<OB_TOTIPO; a++) {
409                 name = getname_ob_ei(ob_ar[a], colipo);
410                 strcpy(ei->name, name);
411                 ei->adrcode= ob_ar[a];
412                 
413                 if ELEM6(ei->adrcode, OB_ROT_X, OB_ROT_Y, OB_ROT_Z, OB_DROT_X, OB_DROT_Y, OB_DROT_Z) ei->disptype= IPO_DISPDEGR;
414                 else if(ei->adrcode==OB_LAY) ei->disptype= IPO_DISPBITS;
415                 else if(ei->adrcode==OB_TIME) ei->disptype= IPO_DISPTIME;
416
417                 ei->col= ipo_rainbow(a, OB_TOTIPO);
418
419                 if(colipo) {
420                         len= strlen(ei->name);
421                         if(len) {
422                                 if( ei->name[ len-1 ]=='R') ei->col= 0x5050FF;
423                                 else if( ei->name[ len-1 ]=='G') ei->col= 0x50FF50;
424                                 else if( ei->name[ len-1 ]=='B') ei->col= 0xFF7050;
425                         }
426                 }
427                 
428                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
429                 if(ei->icu) {
430                         ei->flag= ei->icu->flag;
431                 }
432                 
433                 
434                 ei++;
435         }
436         //fprintf(stderr,"FSIMAKE_OPBJ call %d \n", si->totipo);
437 }
438
439 // copied from make_seq_editipo
440 static void make_fluidsim_editipo(SpaceIpo *si) // NT
441 {
442         EditIpo *ei;
443         int a;
444         char *name;
445         ei= si->editipo= MEM_callocN(FLUIDSIM_TOTIPO*sizeof(EditIpo), "fluidsim_editipo");
446         si->totipo = FLUIDSIM_TOTIPO;
447         for(a=0; a<FLUIDSIM_TOTIPO; a++) {
448                 //fprintf(stderr,"FSINAME %d %d \n",a,fluidsim_ar[a], (int)(getname_fluidsim_ei(fluidsim_ar[a]))  );
449                 name = getname_fluidsim_ei(fluidsim_ar[a]);
450                 strcpy(ei->name, name);
451                 ei->adrcode= fluidsim_ar[a];
452                 ei->col= ipo_rainbow(a, FLUIDSIM_TOTIPO);
453                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
454                 if(ei->icu) {
455                         ei->flag = ei->icu->flag;
456                 } 
457                 //else { ei->flag |= IPO_VISIBLE; }
458                 //fprintf(stderr,"FSIMAKE eif%d,icuf%d icu%d %d|%d\n", ei->flag,ei->icu->flag, (int)ei->icu, IPO_VISIBLE,IPO_SELECT);
459                 //fprintf(stderr,"FSIMAKE eif%d icu%d %d|%d\n", ei->flag, (int)ei->icu, IPO_VISIBLE,IPO_SELECT);
460                 ei++;
461         }
462 }
463
464 static void make_seq_editipo(SpaceIpo *si)
465 {
466         EditIpo *ei;
467         int a;
468         char *name;
469         
470         ei= si->editipo= MEM_callocN(SEQ_TOTIPO*sizeof(EditIpo), "editipo");
471         
472         si->totipo= SEQ_TOTIPO;
473         
474         
475         for(a=0; a<SEQ_TOTIPO; a++) {
476                 name = getname_seq_ei(seq_ar[a]);
477                 strcpy(ei->name, name);
478                 ei->adrcode= seq_ar[a];
479                 
480                 ei->col= ipo_rainbow(a, SEQ_TOTIPO);
481                 
482                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
483                 if(ei->icu) {
484                         ei->flag= ei->icu->flag;
485                 }
486                 else ei->flag |= IPO_VISIBLE;
487                 
488                 ei++;
489         }
490 }
491
492 static void make_cu_editipo(SpaceIpo *si)
493 {
494         EditIpo *ei;
495         int a;
496         char *name;
497         
498         ei= si->editipo= MEM_callocN(CU_TOTIPO*sizeof(EditIpo), "editipo");
499         
500         si->totipo= CU_TOTIPO;
501         
502         
503         for(a=0; a<CU_TOTIPO; a++) {
504                 name = getname_cu_ei(cu_ar[a]);
505                 strcpy(ei->name, name);
506                 ei->adrcode= cu_ar[a];
507                 
508                 ei->col= ipo_rainbow(a, CU_TOTIPO);
509                 
510                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
511                 if(ei->icu) {
512                         ei->flag= ei->icu->flag;
513                 }
514                 else ei->flag |= IPO_VISIBLE;
515                  
516                 ei++;
517         }
518 }
519
520 static void make_key_editipo(SpaceIpo *si)
521 {
522         Key *key;
523         KeyBlock *kb=NULL;
524         EditIpo *ei;
525         int a;
526         
527         key= ob_get_key((Object *)G.sipo->from);
528         if(key==NULL) return;
529         
530         si->totipo= BLI_countlist(&key->block);
531         ei= si->editipo= MEM_callocN(si->totipo*sizeof(EditIpo), "editipo");
532         
533         for(a=0, kb= key->block.first; a<si->totipo; a++, ei++, kb= kb->next) {
534                 
535                 if(kb->name[0] != 0) strncpy(ei->name, kb->name, 31);   // length both same
536                 ei->adrcode= kb->adrcode;
537                 
538                 ei->col= ipo_rainbow(a, KEY_TOTIPO);
539                 
540                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
541                 if(ei->icu) {
542                         ei->flag= ei->icu->flag;
543                 }
544                 else if(a==0) 
545                         if(key && key->type==KEY_NORMAL)
546                                 ei->flag |= IPO_VISIBLE;
547                 
548                 /* active ipo is tied to active shape  */
549                 {
550                         Object *ob= OBACT;
551                         if(a==ob->shapenr-1)
552                                 set_active_editipo(ei);
553                 }
554         }
555         
556         ei= si->editipo;
557         if(key && key->type==KEY_RELATIVE) {
558                 strcpy(ei->name, "----");
559         }
560         else {
561                 ei->flag |= IPO_VISIBLE;
562         }
563 }
564
565 static void make_mat_editipo(SpaceIpo *si)
566 {
567         EditIpo *ei;
568         int a, len;
569         char *name;
570         
571         if(si->from==0) return;
572         
573         ei= si->editipo= MEM_callocN(MA_TOTIPO*sizeof(EditIpo), "editipo");
574         
575         si->totipo= MA_TOTIPO;
576         
577         for(a=0; a<MA_TOTIPO; a++) {
578                 name = getname_mat_ei(ma_ar[a]);
579                 strcpy(ei->name, name);
580                 ei->adrcode= ma_ar[a];
581                 
582                 if(ei->adrcode & MA_MAP1) {
583                         ei->adrcode-= MA_MAP1;
584                         ei->adrcode |= texchannel_to_adrcode(si->channel);
585                 }
586                 else {
587                         if(ei->adrcode==MA_MODE) ei->disptype= IPO_DISPBITS;
588                 }
589                 
590                 ei->col= ipo_rainbow(a, MA_TOTIPO);
591                 
592                 len= strlen(ei->name);
593                 if(len) {
594                         if( ei->name[ len-1 ]=='R') ei->col= 0x5050FF;
595                         else if( ei->name[ len-1 ]=='G') ei->col= 0x50FF50;
596                         else if( ei->name[ len-1 ]=='B') ei->col= 0xFF7050;
597                 }
598                 
599                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
600                 if(ei->icu) {
601                         ei->flag= ei->icu->flag;
602                 }
603                 
604                 ei++;
605         }
606 }
607
608 static void make_texture_editipo(SpaceIpo *si)
609 {
610         EditIpo *ei;
611         int a, len;
612         char *name;
613         
614         if(si->from==0) return;    
615         
616         ei= si->editipo= MEM_callocN(TE_TOTIPO*sizeof(EditIpo), "editipo");
617         
618         si->totipo= TE_TOTIPO;
619         
620         for(a=0; a<TE_TOTIPO; a++) {
621                 name = getname_tex_ei(te_ar[a]);
622                 strcpy(ei->name, name);
623                         ei->adrcode= te_ar[a];
624
625                 ei->col= ipo_rainbow(a, TE_TOTIPO);
626         
627                 len= strlen(ei->name);
628                 if(len) {
629                         if( ei->name[ len-1 ]=='R') ei->col= 0x5050FF;
630                         else if( ei->name[ len-1 ]=='G') ei->col= 0x50FF50;
631                         else if( ei->name[ len-1 ]=='B') ei->col= 0xFF7050;
632                 }       
633                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
634                 if(ei->icu) {
635                         ei->flag= ei->icu->flag;
636                 }
637                 
638                 ei++;
639         }
640 }
641
642 static void make_world_editipo(SpaceIpo *si)
643 {
644         EditIpo *ei;
645         int a, len;
646         char *name;
647         
648         if(si->from==0) return;
649         
650         ei= si->editipo= MEM_callocN(WO_TOTIPO*sizeof(EditIpo), "editipo");
651         
652         si->totipo= WO_TOTIPO;
653         
654         for(a=0; a<WO_TOTIPO; a++) {
655                 name = getname_world_ei(wo_ar[a]);
656                 
657                 strcpy(ei->name, name); 
658                 ei->adrcode= wo_ar[a];
659                 
660                 if(ei->adrcode & MA_MAP1) {
661                         ei->adrcode-= MA_MAP1;
662                         ei->adrcode |= texchannel_to_adrcode(si->channel);
663                 }
664                 else {
665                         if(ei->adrcode==MA_MODE) ei->disptype= IPO_DISPBITS;
666                 }
667                 
668                 ei->col= ipo_rainbow(a, WO_TOTIPO);
669                 
670                 len= strlen(ei->name);
671                 if(len) {
672                         if( ei->name[ len-1 ]=='R') ei->col= 0x5050FF;
673                         else if( ei->name[ len-1 ]=='G') ei->col= 0x50FF50;
674                         else if( ei->name[ len-1 ]=='B') ei->col= 0xFF7050;
675                 }
676                 
677                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
678                 if(ei->icu) {
679                         ei->flag= ei->icu->flag;
680                 }
681                 
682                 ei++;
683         }
684 }
685
686 static void make_lamp_editipo(SpaceIpo *si)
687 {
688         EditIpo *ei;
689         int a;
690         char *name;
691         
692         ei= si->editipo= MEM_callocN(LA_TOTIPO*sizeof(EditIpo), "editipo");
693         
694         si->totipo= LA_TOTIPO;
695         
696         for(a=0; a<LA_TOTIPO; a++) {
697                 name = getname_la_ei(la_ar[a]);
698                 strcpy(ei->name, name);
699                 ei->adrcode= la_ar[a];
700
701                 if(ei->adrcode & MA_MAP1) {
702                         ei->adrcode-= MA_MAP1;
703                         ei->adrcode |= texchannel_to_adrcode(si->channel);
704                 }
705
706                 ei->col= ipo_rainbow(a, LA_TOTIPO);
707                 
708                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
709                 if(ei->icu) {
710                         ei->flag= ei->icu->flag;
711                 }
712                 
713                 ei++;
714         }
715 }
716
717 static void make_camera_editipo(SpaceIpo *si)
718 {
719         EditIpo *ei;
720         int a;
721         char *name;
722         
723         ei= si->editipo= MEM_callocN(CAM_TOTIPO*sizeof(EditIpo), "editipo");
724         
725         si->totipo= CAM_TOTIPO;
726         
727         
728         for(a=0; a<CAM_TOTIPO; a++) {
729                 name = getname_cam_ei(cam_ar[a]);
730                 strcpy(ei->name, name);
731                 ei->adrcode= cam_ar[a];
732
733                 ei->col= ipo_rainbow(a, CAM_TOTIPO);
734                 
735                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
736                 if(ei->icu) {
737                         ei->flag= ei->icu->flag;
738                 }
739                 
740                 ei++;
741         }
742 }
743
744 static int make_constraint_editipo(Ipo *ipo, EditIpo **si)
745 {
746         EditIpo *ei;
747         int a;
748         char *name;
749         
750         ei= *si= MEM_callocN(CO_TOTIPO*sizeof(EditIpo), "editipo");
751         
752         for(a=0; a<CO_TOTIPO; a++) {
753                 name = getname_co_ei(co_ar[a]);
754                 strcpy(ei->name, name);
755                 ei->adrcode= co_ar[a];
756
757                 ei->col= ipo_rainbow(a, CO_TOTIPO);
758                 
759                 ei->icu= find_ipocurve(ipo, ei->adrcode);
760                 if(ei->icu) {
761                         ei->flag= ei->icu->flag;
762                 }
763                 
764                 ei++;
765         }
766
767         return CO_TOTIPO;
768 }
769
770 static int make_bone_editipo(Ipo *ipo, EditIpo **si)
771 {
772         EditIpo *ei;
773         int a;
774         char *name;
775         
776         ei= *si= MEM_callocN(AC_TOTIPO*sizeof(EditIpo), "editipo");
777         
778         for(a=0; a<AC_TOTIPO; a++) {
779                 name = getname_ac_ei(ac_ar[a]);
780                 strcpy(ei->name, name);
781                 ei->adrcode= ac_ar[a];
782
783                 ei->col= ipo_rainbow(a, AC_TOTIPO);
784                 
785                 ei->icu= find_ipocurve(ipo, ei->adrcode);
786                 if(ei->icu) {
787                         ei->flag= ei->icu->flag;
788                 }
789                 
790                 ei++;
791         }
792
793         return AC_TOTIPO;
794 }
795
796 static void make_sound_editipo(SpaceIpo *si)
797 {
798         EditIpo *ei;
799         int a;
800         char *name;
801         
802         ei= si->editipo= MEM_callocN(SND_TOTIPO*sizeof(EditIpo), "editipo");
803         
804         si->totipo= SND_TOTIPO;
805         
806         
807         for(a=0; a<SND_TOTIPO; a++) {
808                 name = getname_snd_ei(snd_ar[a]);
809                 strcpy(ei->name, name);
810                 ei->adrcode= snd_ar[a];
811
812                 ei->col= ipo_rainbow(a, SND_TOTIPO);
813                 
814                 ei->icu= find_ipocurve(si->ipo, ei->adrcode);
815                 if(ei->icu) {
816                         ei->flag= ei->icu->flag;
817                 }
818                 
819                 ei++;
820         }
821 }
822
823 /* only called in test_editipo() below */
824 static void make_editipo(void)
825 {
826         EditIpo *ei;
827         Object *ob;
828         rctf *rf;
829         int a;
830
831         if(G.sipo->editipo)
832                 MEM_freeN(G.sipo->editipo);
833         
834         G.sipo->editipo= NULL;
835         G.sipo->totipo= 0;
836         
837         if(G.sipo->from==NULL) return;
838         
839         ob= OBACT;
840
841         if(G.sipo->ipo) G.sipo->showkey= G.sipo->ipo->showkey;
842
843         if(G.sipo->blocktype==ID_SEQ) {
844                 make_seq_editipo(G.sipo);
845         }
846         else if(G.sipo->blocktype==ID_WO) {
847                 make_world_editipo(G.sipo);
848         } 
849         else if(G.sipo->blocktype==ID_OB) {
850                 if (ob) {
851                         ob->ipowin= ID_OB;
852                         make_ob_editipo(ob, G.sipo);
853                 }
854         }
855         else if(G.sipo->blocktype==ID_MA) {
856                 if (ob) {
857                         ob->ipowin= ID_MA;
858                         make_mat_editipo(G.sipo);
859                 }
860         }
861         else if(G.sipo->blocktype==ID_CU) {
862                 if (ob) {
863                         ob->ipowin= ID_CU;
864                         make_cu_editipo(G.sipo);
865                 }
866         }
867         else if(G.sipo->blocktype==ID_KE) {
868                 if (ob) {
869                         ob->ipowin= ID_KE;
870                         make_key_editipo(G.sipo);
871                 }
872         }
873         else if(G.sipo->blocktype==ID_LA) {
874                 if (ob) {
875                         ob->ipowin= ID_LA;
876                         make_lamp_editipo(G.sipo);
877                 }
878         }
879         else if(G.sipo->blocktype==ID_TE) {
880                 if (ob) {
881                         ob->ipowin= ID_TE;
882                         make_texture_editipo(G.sipo);
883                 }
884         }
885         else if(G.sipo->blocktype==ID_CA) {
886                 if (ob) {
887                         ob->ipowin= ID_CA;
888                         make_camera_editipo(G.sipo);
889                 }
890         }
891         else if(G.sipo->blocktype==ID_SO) {
892                 if (ob) {
893                         ob->ipowin= ID_SO;
894                         make_sound_editipo(G.sipo);
895                 }
896         }
897         else if(G.sipo->blocktype==ID_CO){
898                 G.sipo->totipo = make_constraint_editipo(G.sipo->ipo, (EditIpo**)&G.sipo->editipo);
899                 if (ob) {
900                         ob->ipowin= ID_CO;
901                 }
902         }
903         else if(G.sipo->blocktype==ID_PO) {
904
905                 G.sipo->totipo = make_bone_editipo(G.sipo->ipo, (EditIpo**)&G.sipo->editipo);
906                 if (ob) {
907                         ob->ipowin= ID_PO;
908                 }
909         }
910         else if(G.sipo->blocktype==ID_FLUIDSIM) {
911                 if (ob) { // NT
912                         ob->ipowin= ID_FLUIDSIM;
913                         make_fluidsim_editipo(G.sipo);
914                 }
915         }
916
917         if(G.sipo->editipo==0) return;
918         
919         ei= G.sipo->editipo;
920         for(a=0; a<G.sipo->totipo; a++, ei++) {
921                 if(ei->icu) ei->icu->flag= ei->flag;
922         }
923         editipo_changed(G.sipo, 0);
924         
925         /* sets globals, bad stuff but we need these variables in other parts of code */
926         get_status_editipo();
927         
928         if(G.sipo->ipo) {
929
930                 if (G.sipo->pin)
931                         rf= &(G.sipo->v2d.cur);
932                 else
933                         rf= &(G.sipo->ipo->cur);
934                 
935                 if(rf->xmin<rf->xmax && rf->ymin<rf->ymax) G.v2d->cur= *rf;
936                 else ipo_default_v2d_cur(G.sipo->blocktype, &G.v2d->cur);
937         }
938         else {
939                 ipo_default_v2d_cur(G.sipo->blocktype, &G.v2d->cur);
940         }
941         
942         view2d_do_locks(curarea, V2D_LOCK_COPY);
943 }
944
945 /* evaluates context in the current UI */
946 /* blocktype is type of ipo */
947 /* from is the base pointer to find data to change (ob in case of action or pose) */
948 static void get_ipo_context(short blocktype, ID **from, Ipo **ipo, char *actname, char *constname)
949 {
950         Object *ob= OBACT;
951         
952         *from= NULL;
953         *ipo= NULL;
954         
955         if(blocktype==ID_CO) {
956                 if (ob) {
957                         bConstraintChannel *chan;
958                         bConstraint *con= get_active_constraint(ob);
959                         
960                         if(con) {
961                                 BLI_strncpy(constname, con->name, 32);
962                                 
963                                 chan= get_active_constraint_channel(ob);
964                                 if(chan) {
965                                         *ipo= chan->ipo;
966                                         BLI_strncpy(constname, con->name, 32);
967                                 }
968                                 
969                                 *from= &ob->id;
970                                 
971                                 /* set actname if in posemode */
972                                 if(ob->action) {
973                                         if(ob->flag & OB_POSEMODE) {
974                                                 bPoseChannel *pchan= get_active_posechannel(ob);
975                                                 if(pchan)
976                                                         BLI_strncpy(actname, pchan->name, 32);
977                                         }
978                                         else if(ob->ipoflag & OB_ACTION_OB)
979                                                 strcpy(actname, "Object");
980                                 }
981                         }
982                 }
983         }
984         else if(blocktype==ID_PO) {
985                 if (ob && ob->action && ob->type==OB_ARMATURE) {
986                         bPoseChannel *pchan= get_active_posechannel(ob);
987                         
988                         *from= (ID *)ob;
989                         if (pchan) {
990                                 bActionChannel *achan;
991                                 
992                                 BLI_strncpy(actname, pchan->name, 32);  /* also set when no channel yet */
993                                 
994                                 achan= get_action_channel(ob->action, pchan->name);
995                                 if(achan)
996                                         *ipo= achan->ipo;
997                         }
998                 } 
999                 
1000         }
1001         else if(blocktype==ID_OB) {
1002                 if(ob) {
1003                         *from= (ID *)ob;
1004                         if(ob->ipoflag & OB_ACTION_OB) {
1005                                 if (ob->action) {
1006                                         bActionChannel *achan= get_action_channel(ob->action, "Object");
1007                                         if(achan) {
1008                                                 *ipo= achan->ipo;
1009                                                 BLI_strncpy(actname, achan->name, 32);
1010                                         }
1011                                 }
1012                         }
1013                         else {
1014                                 *ipo= ob->ipo;
1015                         }
1016                 }
1017         }
1018         else if(blocktype==ID_SEQ) {
1019                 Sequence *last_seq = get_last_seq();
1020                 
1021                 if(last_seq && ((last_seq->type & SEQ_EFFECT)||(last_seq->type == SEQ_HD_SOUND)||(last_seq->type == SEQ_RAM_SOUND))) {
1022                         *from= (ID *)last_seq;
1023                         *ipo= last_seq->ipo;
1024                 }
1025         }
1026         else if(blocktype==ID_WO) {
1027                 World *wo= G.scene->world;
1028                 *from= (ID *)wo;
1029                 if(wo) *ipo= wo->ipo;
1030         }
1031         else if(blocktype==ID_TE) {
1032                 if(ob) {
1033                         Tex *tex= give_current_texture(ob, ob->actcol);
1034                         *from= (ID *)tex;
1035                         if(tex) *ipo= tex->ipo;
1036                 }
1037         }
1038         else if(blocktype==ID_MA) {
1039                 if(ob) {
1040                         Material *ma= give_current_material(ob, ob->actcol);
1041                         ma= editnode_get_active_material(ma);
1042                         *from= (ID *)ma;
1043                         if(ma) *ipo= ma->ipo;
1044                 }
1045         }
1046         else if(blocktype==ID_KE) {
1047                 if(ob) {
1048                         Key *key= ob_get_key(ob);
1049                         
1050                         if(ob->ipoflag & OB_ACTION_KEY) {
1051                                 if (ob->action) {
1052                                         bActionChannel *achan= get_action_channel(ob->action, "Shape");
1053                                         if(achan) {
1054                                                 *ipo= achan->ipo;
1055                                                 BLI_strncpy(actname, achan->name, 32);
1056                                         }
1057                                 }
1058                         }
1059                         else if(key) *ipo= key->ipo;
1060                         
1061                         *from= (ID *)ob;
1062                 }
1063         }
1064         else if(blocktype==ID_CU) {
1065                 if(ob && ob->type==OB_CURVE) {
1066                         Curve *cu= ob->data;
1067                         *from= (ID *)cu;
1068                         *ipo= cu->ipo;
1069                 }
1070         }
1071         else if(blocktype==ID_LA) {
1072                 if(ob && ob->type==OB_LAMP) {
1073                         Lamp *la= ob->data;
1074                         *from= (ID *)la;
1075                         *ipo= la->ipo;
1076                 }
1077         }
1078         else if(blocktype==ID_CA) {
1079                 if(ob && ob->type==OB_CAMERA) {
1080                         Camera *ca= ob->data;
1081                         *from= (ID *)ca;
1082                         if(ca) *ipo= ca->ipo;
1083                 }
1084         }
1085         else if(blocktype==ID_SO) {
1086                 
1087                 //              if (G.buts && G.buts->mainb == BUTS_SOUND) {
1088                 //                      bSound *sound = G.buts->lockpoin;
1089                 //                      *from= (ID *)sound;
1090                 //                      if(sound) *ipo= sound->ipo;
1091                 //              }
1092         }
1093         else if(blocktype==ID_FLUIDSIM) {
1094                 if(ob && ( ob->fluidsimFlag & OB_FLUIDSIM_ENABLE)) {
1095                         FluidsimSettings *fss= ob->fluidsimSettings;
1096                         *from= (ID *)ob;
1097                         if(fss) *ipo= fss->ipo;
1098                 }
1099         }
1100 }
1101
1102 /* called on each redraw, check if editipo data has to be remade */
1103 /* if doit already set, it always makes (in case no ipo exists, we need to see the channels */
1104 void test_editipo(int doit)
1105 {
1106         
1107         if(G.sipo->pin==0) {
1108                 Ipo *ipo;
1109                 ID *from;
1110                 char actname[32]="", constname[32]="";
1111                 
1112                 get_ipo_context(G.sipo->blocktype, &from, &ipo, actname, constname);
1113                 
1114                 if(G.sipo->ipo != ipo) {
1115                         G.sipo->ipo= ipo;
1116                         if(ipo) G.v2d->cur= ipo->cur;
1117                         doit= 1;
1118                 }
1119                 if(G.sipo->from != from) {
1120                         G.sipo->from= from;
1121                         doit= 1;
1122                 }
1123                 if( strcmp(G.sipo->actname, actname)) {
1124                         BLI_strncpy(G.sipo->actname, actname, 32);
1125                         doit= 1;
1126                 }
1127                 if( strcmp(G.sipo->constname, constname)) {
1128                         BLI_strncpy(G.sipo->constname, constname, 32);
1129                         doit= 1;
1130                 }
1131                 
1132                 if(G.sipo->ipo)
1133                         G.sipo->ipo->cur = G.v2d->cur;
1134                 
1135         }
1136                 
1137         if(G.sipo->editipo==NULL || doit) {
1138                 make_editipo();
1139         }
1140 }
1141
1142 /* ****************** EditIpo ************************ */
1143
1144 int totipo_edit=0, totipo_sel=0, totipo_curve=0, totipo_vis=0, totipo_vert=0, totipo_vertsel=0, totipo_key=0, totipo_keysel=0;
1145
1146 void get_status_editipo(void)
1147 {
1148         EditIpo *ei;
1149         IpoKey *ik;
1150         BezTriple *bezt;
1151         int a, b;
1152         
1153         totipo_vis= 0;
1154         totipo_curve= 0;
1155         totipo_sel= 0;
1156         totipo_edit= 0;
1157         totipo_vert= 0;
1158         totipo_vertsel= 0;
1159         totipo_key= 0;
1160         totipo_keysel= 0;
1161         
1162         if(G.sipo->ipo && G.sipo->ipo->id.lib) return;
1163         
1164         ei= G.sipo->editipo;
1165         if(ei==0) return;
1166         for(a=0; a<G.sipo->totipo; a++) {
1167                 if( ei->flag & IPO_VISIBLE ) {
1168                         totipo_vis++;
1169                         if(ei->flag & IPO_SELECT) totipo_sel++;
1170                         if(ei->icu && ei->icu->totvert) totipo_curve++;
1171                         if(G.sipo->showkey || (ei->flag & IPO_EDIT)) {
1172                                 
1173                                 /* if showkey: do count the vertices (for grab) */
1174                                 if(G.sipo->showkey==0) totipo_edit++;
1175                                 
1176                                 if(ei->icu) {
1177                                         if(ei->icu->bezt) {
1178                                                 bezt= ei->icu->bezt;
1179                                                 b= ei->icu->totvert;
1180                                                 while(b--) {
1181                                                         if(ei->icu->ipo==IPO_BEZ) {
1182                                                                 if(bezt->f1 & 1) totipo_vertsel++;
1183                                                                 if(bezt->f3 & 1) totipo_vertsel++;
1184                                                                 totipo_vert+= 2;
1185                                                         }
1186                                                         if(bezt->f2 & 1) totipo_vertsel++;
1187                                                         
1188                                                         totipo_vert++;
1189                                                         bezt++;
1190                                                 }
1191                                         }
1192                                 }
1193                         }
1194                 }
1195                 ei++;
1196         }
1197         
1198         if(G.sipo->showkey) {
1199                 ik= G.sipo->ipokey.first;
1200                 while(ik) {
1201                         totipo_key++;
1202                         if(ik->flag & 1) totipo_keysel++;
1203                         ik= ik->next;
1204                 }
1205         }
1206 }
1207
1208 /* synchronize editipo flag with icu flag and ipokey flags */
1209 void update_editipo_flags(void)
1210 {
1211         EditIpo *ei;
1212         IpoKey *ik;
1213         int a;
1214         
1215         ei= G.sipo->editipo;
1216         if(ei) {
1217                 for(a=0; a<G.sipo->totipo; a++, ei++) {
1218                         if(ei->icu) ei->icu->flag= ei->flag;
1219                 }
1220         }
1221         if(G.sipo->showkey) {
1222                 ik= G.sipo->ipokey.first;
1223                 while(ik) {
1224                         for(a=0; a<G.sipo->totipo; a++) {
1225                                 if(ik->data[a]) {
1226                                         if(ik->flag & 1) {
1227                                                 ik->data[a]->f1 |= 1;
1228                                                 ik->data[a]->f2 |= 1;
1229                                                 ik->data[a]->f3 |= 1;
1230                                         }
1231                                         else {
1232                                                 ik->data[a]->f1 &= ~1;
1233                                                 ik->data[a]->f2 &= ~1;
1234                                                 ik->data[a]->f3 &= ~1;
1235                                         }
1236                                 }
1237                         }
1238                         ik= ik->next;
1239                 }
1240         }
1241 }
1242
1243 /* sort of enter/leave editmode for curves */
1244 void set_editflag_editipo(void)
1245 {
1246         EditIpo *ei;
1247         int a; /*  , tot= 0, ok= 0; */
1248         
1249         /* after showkey immediately go to editing of selected points */
1250         if(G.sipo->showkey) {
1251                 G.sipo->showkey= 0;
1252                 if(G.sipo->ipo) G.sipo->ipo->showkey= 0;
1253                 ei= G.sipo->editipo;
1254                 for(a=0; a<G.sipo->totipo; a++, ei++) ei->flag |= IPO_SELECT;
1255                 scrarea_queue_headredraw(curarea);
1256                 allqueue(REDRAWVIEW3D, 0);
1257         }
1258         
1259         get_status_editipo();
1260         
1261         if(G.sipo->ipo && G.sipo->ipo->id.lib) return;
1262         
1263         ei= G.sipo->editipo;
1264         for(a=0; a<G.sipo->totipo; a++, ei++) {         
1265                 if(ei->icu) {
1266                         if(ei->flag & IPO_VISIBLE) {
1267                                 
1268                                 if(totipo_edit==0 && (ei->flag & IPO_SELECT)) {
1269                                         ei->flag |= IPO_EDIT;
1270                                         ei->icu->flag= ei->flag;
1271                                 }
1272                                 else if(totipo_edit && (ei->flag & IPO_EDIT)) {
1273                                         ei->flag -= IPO_EDIT;
1274                                         ei->icu->flag= ei->flag;
1275                                 }
1276                                 else if(totipo_vis==1) {
1277                                         if(ei->flag & IPO_EDIT) ei->flag -= IPO_EDIT;
1278                                         else ei->flag |= IPO_EDIT;
1279                                         ei->icu->flag= ei->flag;
1280                                 }
1281                         }
1282                 }
1283         }
1284         scrarea_queue_headredraw(curarea);
1285         scrarea_queue_winredraw(curarea);
1286 }
1287
1288 static short findnearest_ipovert(IpoCurve **icu, BezTriple **bezt)
1289 {
1290         /* selected verts get a disadvantage */
1291         /* in icu and (bezt or bp) the nearest is written */
1292         /* return 0 1 2: handlepunt */
1293         EditIpo *ei;
1294         BezTriple *bezt1;
1295         int dist= 100, temp, a, b;
1296         short mval[2], hpoint=0, sco[3][2];
1297
1298         *icu= 0;
1299         *bezt= 0;
1300
1301         getmouseco_areawin(mval);
1302
1303         ei= G.sipo->editipo;
1304         for(a=0; a<G.sipo->totipo; a++, ei++) {
1305                 if (ISPOIN3(ei, flag & IPO_VISIBLE, flag & IPO_EDIT, icu)) {
1306                         
1307                         if(ei->icu->bezt) {
1308                                 bezt1= ei->icu->bezt;
1309                                 b= ei->icu->totvert;
1310                                 while(b--) {
1311
1312                                         ipoco_to_areaco_noclip(G.v2d, bezt1->vec[0], sco[0]);
1313                                         ipoco_to_areaco_noclip(G.v2d, bezt1->vec[1], sco[1]);
1314                                         ipoco_to_areaco_noclip(G.v2d, bezt1->vec[2], sco[2]);
1315                                                                                 
1316                                         if(ei->disptype==IPO_DISPBITS) {
1317                                                 temp= abs(mval[0]- sco[1][0]);
1318                                         }
1319                                         else temp= abs(mval[0]- sco[1][0])+ abs(mval[1]- sco[1][1]);
1320
1321                                         if( bezt1->f2 & 1) temp+=5;
1322                                         if(temp<dist) { 
1323                                                 hpoint= 1; 
1324                                                 *bezt= bezt1; 
1325                                                 dist= temp; 
1326                                                 *icu= ei->icu; 
1327                                         }
1328                                         
1329                                         if(ei->disptype!=IPO_DISPBITS && ei->icu->ipo==IPO_BEZ) {
1330                                                 /* middle points get an advantage */
1331                                                 temp= -3+abs(mval[0]- sco[0][0])+ abs(mval[1]- sco[0][1]);
1332                                                 if( bezt1->f1 & 1) temp+=5;
1333                                                 if(temp<dist) { 
1334                                                         hpoint= 0; 
1335                                                         *bezt= bezt1; 
1336                                                         dist= temp; 
1337                                                         *icu= ei->icu; 
1338                                                 }
1339                 
1340                                                 temp= abs(mval[0]- sco[2][0])+ abs(mval[1]- sco[2][1]);
1341                                                 if( bezt1->f3 & 1) temp+=5;
1342                                                 if(temp<dist) { 
1343                                                         hpoint= 2; 
1344                                                         *bezt=bezt1; 
1345                                                         dist= temp; 
1346                                                         *icu= ei->icu; 
1347                                                 }
1348                                         }
1349                                         bezt1++;
1350                                 }
1351                         }
1352                 }
1353         }
1354
1355         return hpoint;
1356 }
1357
1358 void mouse_select_ipo(void)
1359 {
1360         Object *ob;
1361         KeyBlock *actkb=NULL;
1362         EditIpo *ei, *actei= 0;
1363         IpoCurve *icu;
1364         IpoKey *ik, *actik;
1365         BezTriple *bezt;
1366         TimeMarker *marker;
1367         float x, y, dist, mindist;
1368         int a, oldflag = 0, hand, ok;
1369         short mval[2], xo, yo;
1370         
1371         if(G.sipo->editipo==0) return;
1372         
1373         get_status_editipo();
1374         marker=find_nearest_marker(1);
1375         
1376         /* map ipo-points for editing if scaled ipo */
1377         if (NLA_IPO_SCALED) {
1378                 actstrip_map_ipo_keys(OBACT, G.sipo->ipo, 0, 0);
1379         }
1380         
1381         if(G.sipo->showkey) {
1382                 getmouseco_areawin(mval);
1383                 
1384                 areamouseco_to_ipoco(G.v2d, mval, &x, &y);
1385                 actik= 0;
1386                 mindist= 1000.0;
1387                 ik= G.sipo->ipokey.first;
1388                 while(ik) {
1389                         dist= (float)(fabs(ik->val-x));
1390                         if(ik->flag & 1) dist+= 1.0;
1391                         if(dist < mindist) {
1392                                 actik= ik;
1393                                 mindist= dist;
1394                         }
1395                         ik= ik->next;
1396                 }
1397                 if(actik) {
1398                         oldflag= actik->flag;
1399                         
1400                         if(G.qual & LR_SHIFTKEY);
1401                         else deselectall_editipo();
1402                         
1403                         if(G.qual & LR_SHIFTKEY) {
1404                                 if(oldflag & 1) actik->flag &= ~1;
1405                                 else actik->flag |= 1;
1406                         }
1407                         else {
1408                                 actik->flag |= 1;
1409                         }
1410                 }
1411         }
1412         else if(totipo_edit) {
1413                 
1414                 hand= findnearest_ipovert(&icu, &bezt);
1415                 
1416                 if(G.qual & LR_SHIFTKEY) {
1417                         if(bezt) {
1418                                 if(hand==1) {
1419                                         if(BEZSELECTED(bezt)) {
1420                                                 bezt->f1= bezt->f2= bezt->f3= 0;
1421                                         }
1422                                         else {
1423                                                 bezt->f1= bezt->f2= bezt->f3= 1;
1424                                         }
1425                                 }
1426                                 else if(hand==0) {
1427                                         if(bezt->f1 & 1) bezt->f1= 0;
1428                                         else bezt->f1= 1;
1429                                 }
1430                                 else {
1431                                         if(bezt->f3 & 1) bezt->f3= 0;
1432                                         else bezt->f3= 1;
1433                                 }
1434                         }                               
1435                 }
1436                 else {
1437                         deselectall_editipo();
1438                         
1439                         if(bezt) {
1440                                 if(hand==1) {
1441                                         bezt->f1|= 1; bezt->f2|= 1; bezt->f3|= 1;
1442                                 }
1443                                 else if(hand==0) bezt->f1|= 1;
1444                                 else bezt->f3|= 1;
1445                         }
1446                 }
1447         }
1448         else if (marker) {
1449                 /* select timeline marker */
1450                 if ((G.qual & LR_SHIFTKEY)==0) {
1451                         oldflag= marker->flag;
1452                         deselect_markers(0, 0);
1453                         
1454                         if (oldflag & SELECT)
1455                                 marker->flag &= ~SELECT;
1456                         else
1457                                 marker->flag |= SELECT;
1458                 }
1459                 else {
1460                         marker->flag |= SELECT;                         
1461                 }               
1462         }
1463         else {
1464                 
1465                 /* vertex keys ? */
1466                 if(G.sipo->blocktype==ID_KE && G.sipo->from) {
1467                         Key *key;
1468                         KeyBlock *kb, *curkb;
1469                         int i, index= 1;
1470                         
1471                         ob= (Object *)G.sipo->from;
1472                         key= ob_get_key(ob);
1473                         curkb= BLI_findlink(&key->block, ob->shapenr-1);
1474                         
1475                         ei= G.sipo->editipo;
1476                         if(key->type==KEY_NORMAL || (ei->flag & IPO_VISIBLE)) {
1477                                 getmouseco_areawin(mval);
1478                                 
1479                                 areamouseco_to_ipoco(G.v2d, mval, &x, &y);
1480                                 /* how much is 20 pixels? */
1481                                 mindist= (float)(20.0*(G.v2d->cur.ymax-G.v2d->cur.ymin)/(float)curarea->winy);
1482                                 
1483                                 for(i=1, kb= key->block.first; kb; kb= kb->next, i++) {
1484                                         dist= (float)(fabs(kb->pos-y));
1485                                         if(kb==curkb) dist+= (float)0.01;
1486                                         if(dist < mindist) {
1487                                                 actkb= kb;
1488                                                 mindist= dist;
1489                                                 index= i;
1490                                         }
1491                                 }
1492                                 if(actkb) {
1493                                         ok= TRUE;
1494                                         if(G.obedit && actkb!=curkb) {
1495                                                 ok= okee("Copy key after leaving Edit Mode");
1496                                         }
1497                                         if(ok) {
1498                                                 /* also does all keypos */
1499                                                 deselectall_editipo();
1500                                                 set_active_key(index);
1501                                                 set_active_editipo(ei+index-1);
1502                                         }
1503                                 }
1504                         }
1505                 }
1506                 
1507                 /* select curve */
1508                 if(actkb==NULL) {
1509                         if(totipo_vis==1) {
1510                                 ei= G.sipo->editipo;
1511                                 for(a=0; a<G.sipo->totipo; a++, ei++) {
1512                                         if(ei->icu) {
1513                                                 if(ei->flag & IPO_VISIBLE) actei= ei;
1514                                         }
1515                                 }
1516                         }
1517                         else if(totipo_vis>1) {
1518                                 actei= select_proj_ipo(0, 0);
1519                         }
1520                         
1521                         if(actei) oldflag= actei->flag;
1522                         
1523                         if(G.qual & LR_SHIFTKEY);
1524                         else deselectall_editipo();
1525                         
1526                         if(actei) {
1527                                 if(G.qual & LR_SHIFTKEY) {
1528                                         if(oldflag & IPO_SELECT) actei->flag &= ~IPO_SELECT;
1529                                         else actei->flag |= IPO_SELECT;
1530                                 }
1531                                 else {
1532                                         actei->flag |= IPO_SELECT;
1533                                 }
1534                                 set_active_editipo(actei);
1535                         }
1536                 }
1537         }
1538         
1539         /* undo mapping of ipo-points for editing if scaled ipo */
1540         if (NLA_IPO_SCALED) {
1541                 actstrip_map_ipo_keys(OBACT, G.sipo->ipo, 1, 0);
1542         }
1543         
1544         update_editipo_flags();
1545         
1546         force_draw(0);
1547         BIF_undo_push("Select Ipo");
1548         
1549         if(G.sipo->showkey && G.sipo->blocktype==ID_OB) {
1550                 ob= OBACT;
1551                 if(ob && (ob->ipoflag & OB_DRAWKEY)) allqueue(REDRAWVIEW3D, 0);
1552         }
1553         /* points inside of curve are drawn selected too */
1554         if(G.sipo->blocktype==ID_CU)
1555                 allqueue(REDRAWVIEW3D, 0);
1556         
1557         getmouseco_areawin(mval);
1558         xo= mval[0]; 
1559         yo= mval[1];
1560         
1561         while(get_mbut()&R_MOUSE) {             
1562                 getmouseco_areawin(mval);
1563                 if(abs(mval[0]-xo)+abs(mval[1]-yo) > 4) {
1564                         
1565                         if (marker) {
1566                                 transform_markers('g', 0);
1567                         }
1568                         else {
1569                                 if(actkb) move_keys(OBACT);
1570                                 else transform_ipo('g');
1571                         }
1572                         
1573                         return;
1574                 }
1575                 BIF_wait_for_statechange();
1576         }
1577 }
1578
1579
1580 /* *********************************** */
1581
1582 /* handling of right-hand channel/curve buttons in ipo window */
1583 void do_ipowin_buts(short event)
1584 {
1585         EditIpo *ei = NULL;
1586         int a;
1587
1588         /* without shift, all other channels are made invisible */
1589         if((G.qual & LR_SHIFTKEY)==0) {
1590                 if(event>G.sipo->totipo) return;
1591                 ei = G.sipo->editipo;
1592                 for(a=0; a<G.sipo->totipo; a++) {
1593                         if(a!=event) ei->flag &= ~IPO_VISIBLE;
1594                         else ei->flag |= IPO_VISIBLE;
1595                         ei++;
1596                 }
1597         }
1598         
1599         /* set active */
1600         if(event>=0 && event<G.sipo->totipo) {
1601                 ei= G.sipo->editipo;    // void pointer...
1602                 set_active_editipo(ei+event);
1603                 set_active_key(event+1);        // only if there's a key, of course
1604         }
1605         scrarea_queue_winredraw(curarea);
1606         
1607         update_editipo_flags();
1608         get_status_editipo();
1609
1610         if(G.sipo->showkey) {
1611                 make_ipokey();
1612                 if(G.sipo->blocktype==ID_OB) allqueue(REDRAWVIEW3D, 0);
1613         }
1614
1615 }
1616
1617 /* the fake buttons to the left of channel names, for select/deselect curves */
1618 void do_ipo_selectbuttons(void)
1619 {
1620         EditIpo *ei, *ei1;
1621         int a, nr;
1622         short mval[2];
1623         
1624         if(G.sipo->showkey) return;
1625         
1626         /* do not allow editipo here: convert editipos to selected */
1627         get_status_editipo();
1628         if(totipo_edit) {
1629                 set_editflag_editipo();
1630         }
1631         
1632         /* which */
1633         getmouseco_areawin(mval);
1634
1635         nr= -(mval[1]-curarea->winy+30-G.sipo->butofs-IPOBUTY)/IPOBUTY;
1636         if(G.sipo->blocktype==ID_KE) nr--;              /* keys show something else in first channel */
1637         
1638         if(nr>=0 && nr<G.sipo->totipo) {
1639                 ei= G.sipo->editipo;
1640                 ei+= nr;
1641                 
1642                 set_active_editipo(ei);
1643                 set_active_key(nr+1);
1644
1645                 if(ei->icu) {
1646                         if((ei->flag & IPO_VISIBLE)==0) {
1647                                 ei->flag |= IPO_VISIBLE|IPO_SELECT;
1648                         }
1649         
1650                         if((G.qual & LR_SHIFTKEY)==0) {
1651                                 ei1= G.sipo->editipo;
1652                                 for(a=0; a<G.sipo->totipo; a++) {
1653                                         ei1->flag &= ~IPO_SELECT;
1654                                         ei1++;
1655                                 }
1656                         }
1657
1658                         if(ei->flag & IPO_SELECT) {
1659                                 ei->flag &= ~IPO_SELECT;
1660                         }
1661                         else {
1662                                 ei->flag |= IPO_SELECT;
1663                         }
1664                         
1665                         update_editipo_flags();
1666                         scrarea_queue_winredraw(curarea);
1667                 }
1668         }
1669         BIF_undo_push("Select Ipo curve");
1670 }
1671
1672 /* ********************************* Inserting keys ********************************************* */
1673
1674 /* depending type, it returns ipo, if needed it creates one */
1675 /* returns either action ipo or "real" ipo */
1676 /* arguments define full context;
1677    - *from has to be set always, to Object in case of Actions
1678    - blocktype defines available channels of Ipo struct (blocktype ID_OB can be in action too)
1679    - if actname, use this to locate action, and optional constname to find the channel 
1680 */
1681
1682 /* note; check header_ipo.c, spaceipo_assign_ipo() too */
1683 Ipo *verify_ipo(ID *from, short blocktype, char *actname, char *constname)
1684 {
1685
1686         if(from==NULL || from->lib) return NULL;
1687         
1688         /* first check action ipos */
1689         if(actname && actname[0]) {
1690                 Object *ob= (Object *)from;
1691                 bActionChannel *achan;
1692                 
1693                 if(GS(from->name)!=ID_OB) {
1694                         printf("called ipo system for action with wrong base pointer\n");
1695                         return NULL;
1696                 }
1697                 
1698                 if(ob->action==NULL)
1699                         ob->action= add_empty_action("Action");
1700                 
1701                 achan= verify_action_channel(ob->action, actname);
1702                 
1703                 if(achan) {
1704                         /* constraint exception */
1705                         if(blocktype==ID_CO) {
1706                                 bConstraintChannel *conchan= verify_constraint_channel(&achan->constraintChannels, constname);
1707                                 if(conchan->ipo==NULL) {
1708                                         conchan->ipo= add_ipo("CoIpo", ID_CO);  
1709                                 }
1710                                 return conchan->ipo;
1711                         }
1712                         else {
1713                                 if(achan->ipo==NULL) {
1714                                         achan->ipo= add_ipo("ActIpo", blocktype);
1715                                 }
1716                                 
1717                                 return achan->ipo;
1718                         }
1719                 }
1720         }
1721         else {
1722                 
1723                 switch(GS(from->name)) {
1724                 case ID_OB:
1725                         {
1726                                 Object *ob= (Object *)from;
1727                                 /* constraint exception */
1728                                 if(blocktype==ID_CO) {
1729                                         bConstraintChannel *conchan= verify_constraint_channel(&ob->constraintChannels, constname);
1730                                         if(conchan->ipo==NULL) {
1731                                                 conchan->ipo= add_ipo("CoIpo", ID_CO);  
1732                                         }
1733                                         return conchan->ipo;
1734                                 }
1735                                 else if(blocktype==ID_OB) {
1736                                         if(ob->ipo==NULL) {
1737                                                 ob->ipo= add_ipo("ObIpo", ID_OB);
1738                                         }
1739                                         return ob->ipo;
1740                                 }
1741                                 else if(blocktype==ID_KE) {
1742                                         Key *key= ob_get_key((Object *)from);
1743                                         
1744                                         if(key) {
1745                                                 if(key->ipo==NULL) {
1746                                                         key->ipo= add_ipo("KeyIpo", ID_KE);
1747                                                 }
1748                                                 return key->ipo;
1749                                         }
1750                                         return NULL;
1751                                 }
1752                                 else if(blocktype== ID_FLUIDSIM) {
1753                                         Object *ob= (Object *)from;
1754                                         if(ob->fluidsimFlag & OB_FLUIDSIM_ENABLE) {
1755                                                 FluidsimSettings *fss= ob->fluidsimSettings;
1756                                                 if(fss->ipo==NULL) {
1757                                                         fss->ipo= add_ipo("FluidsimIpo", ID_FLUIDSIM);
1758                                                         //fprintf(stderr,"FSIPO NEW!\n");
1759                                                 }
1760                                                 return fss->ipo;
1761                                         }
1762                                 }
1763                         }
1764                         break;
1765                 case ID_MA:
1766                         {
1767                                 Material *ma= (Material *)from;
1768
1769                                 if(ma->ipo==NULL) {
1770                                         ma->ipo= add_ipo("MatIpo", ID_MA);
1771                                 }
1772                                 return ma->ipo;
1773                         }
1774                         break;
1775                 case ID_TE:
1776                         {
1777                                 Tex *tex= (Tex *)from;
1778
1779                                 if(tex->ipo==NULL) {
1780                                         tex->ipo= add_ipo("TexIpo", ID_TE);
1781                                 }
1782                                 return tex->ipo;
1783                         }
1784                         break;
1785                 case ID_SEQ:
1786                         {
1787                                 Sequence *seq= (Sequence *)from;        /* note, sequence is mimicing Id */
1788
1789                                 if((seq->type & SEQ_EFFECT)||
1790                                    (seq->type == SEQ_RAM_SOUND)||
1791                                    (seq->type == SEQ_HD_SOUND)) {
1792                                         if(seq->ipo==NULL) {
1793                                                 seq->ipo= add_ipo("SeqIpo", ID_SEQ);
1794                                         }
1795                                         update_seq_ipo_rect(seq);
1796                                         return seq->ipo;
1797                                 }
1798                         }
1799                         break;
1800                 case ID_CU:
1801                         {
1802                                 Curve *cu= (Curve *)from;
1803                                 
1804                                 if(cu->ipo==NULL) {
1805                                         cu->ipo= add_ipo("CuIpo", ID_CU);
1806                                 }
1807                                 return cu->ipo;
1808                         }
1809                         break;
1810                 case ID_WO:
1811                         {
1812                                 World *wo= (World *)from;
1813
1814                                 if(wo->ipo==NULL) {
1815                                         wo->ipo= add_ipo("WoIpo", ID_WO);
1816                                 }
1817                                 return wo->ipo;
1818                         }
1819                         break;
1820                 case ID_LA:
1821                         {
1822                                 Lamp *la= (Lamp *)from;
1823                                 
1824                                 if(la->ipo==NULL) {
1825                                         la->ipo= add_ipo("LaIpo", ID_LA);
1826                                 }
1827                                 return la->ipo;
1828                         }
1829                         break;
1830                 case ID_CA:
1831                         {
1832                                 Camera *ca= (Camera *)from;
1833
1834                                 if(ca->ipo==NULL) {
1835                                         ca->ipo= add_ipo("CaIpo", ID_CA);
1836                                 }
1837                                 return ca->ipo;
1838                         }
1839                         break;
1840                 case ID_SO:
1841                         {
1842                                 bSound *snd= (bSound *)from;
1843
1844                                 if(snd->ipo==NULL) {
1845                                         snd->ipo= add_ipo("SndIpo", ID_SO);
1846                                 }
1847                                 return snd->ipo;
1848                         }
1849                 }
1850         }
1851         
1852         return NULL;    
1853 }
1854
1855 /* returns and creates
1856  * Make sure functions check for NULL or they will crash!
1857  *  */
1858 IpoCurve *verify_ipocurve(ID *from, short blocktype, char *actname, char *constname, int adrcode)
1859 {
1860         Ipo *ipo;
1861         IpoCurve *icu= NULL;
1862         
1863         /* return 0 if lib */
1864         /* creates ipo too */
1865         ipo= verify_ipo(from, blocktype, actname, constname);
1866         
1867         if(ipo && ipo->id.lib==NULL && from->lib==NULL) {
1868                 
1869                 for(icu= ipo->curve.first; icu; icu= icu->next) {
1870                         if(icu->adrcode==adrcode) break;
1871                 }
1872                 if(icu==NULL) {
1873                         icu= MEM_callocN(sizeof(IpoCurve), "ipocurve");
1874
1875                         icu->flag |= IPO_VISIBLE|IPO_AUTO_HORIZ;
1876                         icu->blocktype= blocktype;
1877                         icu->adrcode= adrcode;
1878                         
1879                         set_icu_vars(icu);
1880
1881                         BLI_addtail( &(ipo->curve), icu);
1882
1883                         switch (GS(from->name)) {
1884                         case ID_SEQ: {
1885                                 Sequence *seq= (Sequence *)from;
1886
1887                                 update_seq_icu_rects(seq);
1888                                 break;
1889                         }
1890                         }
1891                 }
1892         }
1893
1894         return icu;
1895 }
1896
1897 /* This function adds a given BezTriple to an IPO-Curve. It will allocate 
1898  * memory for the array if needed, and will insert the BezTriple into a
1899  * suitable place in chronological order.
1900  * 
1901  * NOTE: any recalculate of the IPO-Curve that needs to be done will need to 
1902  *              be done by the caller.
1903  */
1904 int insert_bezt_icu (IpoCurve *icu, BezTriple *bezt)
1905 {
1906         BezTriple *newb, *beztd;
1907         int i= 0;
1908         
1909         if (icu->bezt == NULL) {
1910                 icu->bezt= MEM_callocN(sizeof(BezTriple), "beztriple");
1911                 *(icu->bezt)= *bezt;
1912                 icu->totvert= 1;
1913         }
1914         else {
1915                 beztd= icu->bezt;
1916                 for (i = 0; i <= icu->totvert; i++, beztd++) {
1917                         /* no double points - threshold to determine this should be good enough */
1918                         if ((i < icu->totvert) && IS_EQT(beztd->vec[1][0], bezt->vec[1][0], 0.00001)) {
1919                                 *(beztd)= *bezt;
1920                                 break;
1921                         }
1922                         /* if we've reached the end of the icu array, or bezt is to be pasted before current */
1923                         if (i==icu->totvert || beztd->vec[1][0] > bezt->vec[1][0]) {
1924                                 newb= MEM_callocN( (icu->totvert+1)*sizeof(BezTriple), "beztriple");
1925                                 
1926                                 /* add the beztriples that should occur before the beztriple to be pasted (originally in ei->icu) */
1927                                 if (i > 0) 
1928                                         memcpy(newb, icu->bezt, i*sizeof(BezTriple));
1929                                 
1930                                 /* add beztriple to paste at index j */
1931                                 *(newb+i)= *bezt;
1932                                 
1933                                 /* add the beztriples that occur after the beztriple to be pasted (originally in icu) */
1934                                 if (i < icu->totvert) 
1935                                         memcpy(newb+i+1, icu->bezt+i, (icu->totvert-i)*sizeof(BezTriple));
1936                                 
1937                                 MEM_freeN(icu->bezt);
1938                                 icu->bezt= newb;
1939                                 
1940                                 icu->totvert++;
1941                                 break;
1942                         }
1943                 }
1944         }
1945         
1946         /* we need to return the index, so that some tools which do post-processing can 
1947          * detect where we added the BezTriple in the array
1948          */
1949         return i;
1950 }
1951
1952 /* This function is a wrapper for insert_bezt_icu, and should be used when
1953  * adding a new keyframe to a curve, when the keyframe doesn't exist anywhere
1954  * else yet. 
1955  * 
1956  * 'fast' - is only for the python API where importing BVH's would take an extreamly long time.
1957  */
1958 void insert_vert_icu (IpoCurve *icu, float x, float y, short fast)
1959 {
1960         BezTriple beztr;
1961         int a, h1, h2;
1962         
1963         /* set all three points, for nicer start position */
1964         memset(&beztr, 0, sizeof(BezTriple));
1965         beztr.vec[0][0]= x; 
1966         beztr.vec[0][1]= y;
1967         beztr.vec[1][0]= x;
1968         beztr.vec[1][1]= y;
1969         beztr.vec[2][0]= x;
1970         beztr.vec[2][1]= y;
1971         beztr.hide= IPO_BEZ;
1972         beztr.f1= beztr.f2= beztr.f3= SELECT;
1973         beztr.h1= beztr.h2= HD_AUTO;
1974         
1975         /* add temp beztriple to keyframes */
1976         a= insert_bezt_icu(icu, &beztr);
1977         if (!fast) calchandles_ipocurve(icu);
1978         
1979         /* set handletype */
1980         if (icu->totvert > 2) {
1981                 BezTriple *bezt;
1982                 
1983                 h1= h2= HD_AUTO;
1984                 bezt= (icu->bezt + a);
1985                 
1986                 if (a > 0) h1= (bezt-1)->h2;
1987                 if (a < icu->totvert-1) h2= (bezt+1)->h1;
1988                 
1989                 bezt->h1= h1;
1990                 bezt->h2= h2;
1991                 
1992                 if (!fast) calchandles_ipocurve(icu);
1993         }
1994 }
1995
1996 void add_vert_ipo(void)
1997 {
1998         EditIpo *ei;
1999         float x, y;
2000         int val;
2001         short mval[2];
2002
2003         if(G.sipo->ipo && G.sipo->ipo->id.lib) return;
2004         if(G.sipo->showkey) {
2005                 G.sipo->showkey= 0;
2006                 free_ipokey(&G.sipo->ipokey);
2007         }
2008         
2009         getmouseco_areawin(mval);
2010         
2011         if(mval[0]>G.v2d->mask.xmax) return;
2012         
2013         ei= get_active_editipo();
2014         if(ei==NULL) {
2015                 error("No active Ipo curve");
2016                 return;
2017         }
2018         ei->flag |= IPO_VISIBLE;        /* can happen it is active but not visible */
2019         
2020         areamouseco_to_ipoco(G.v2d, mval, &x, &y);
2021         
2022         /* convert click-time to ipo-time */
2023         if (NLA_IPO_SCALED) {
2024                 x= get_action_frame(OBACT, x);
2025         }
2026         
2027         if(ei->icu==NULL) {
2028                 if(G.sipo->from) {
2029                         ei->icu= verify_ipocurve(G.sipo->from, G.sipo->blocktype, G.sipo->actname, G.sipo->constname, ei->adrcode);
2030                         if (ei->icu)
2031                                 ei->flag |= ei->icu->flag & IPO_AUTO_HORIZ;     /* new curve could have been added, weak... */
2032                         else
2033                                 error("Cannot create an IPO curve, you may be using libdata");
2034                 }
2035         }
2036         if(ei->icu==NULL) return;
2037
2038         if(ei->disptype==IPO_DISPBITS) {
2039                 ei->icu->vartype= IPO_BITS;
2040                 val= (int)floor(y-0.5);
2041                 if(val<0) val= 0;
2042                 y= (float)(1 << val);
2043         }
2044         
2045         insert_vert_icu(ei->icu, x, y, 0);
2046
2047         /* to be sure: if icu was 0, or only 1 curve visible */
2048         ei->flag |= IPO_SELECT;
2049         ei->icu->flag= ei->flag;
2050         
2051         editipo_changed(G.sipo, 1);
2052         BIF_undo_push("Add Ipo vertex");
2053 }
2054
2055 static void *get_context_ipo_poin(ID *id, int blocktype, char *actname, IpoCurve *icu, int *vartype)
2056 {
2057         if(blocktype==ID_PO) {
2058                 if(GS(id->name)==ID_OB) {
2059                         Object *ob= (Object *)id;
2060                         bPoseChannel *pchan= get_pose_channel(ob->pose, actname);
2061                         
2062                         *vartype= IPO_FLOAT;
2063                         return get_pchan_ipo_poin(pchan, icu->adrcode);
2064                 }
2065                 return NULL;
2066         }
2067         else
2068                 return get_ipo_poin(id, icu, vartype);
2069
2070 }
2071
2072 #define KEYNEEDED_DONTADD       0
2073 #define KEYNEEDED_JUSTADD       1
2074 #define KEYNEEDED_DELPREV       2
2075 #define KEYNEEDED_DELNEXT       3
2076
2077 static int new_key_needed(IpoCurve *icu, float cFrame, float nValue) 
2078 {
2079         /* This function determines whether a new keyframe is needed */
2080         /* Cases where keyframes should not be added:
2081          *      1. Keyframe to be added bewteen two keyframes with similar values
2082          *      2. Keyframe to be added on frame where two keyframes are already situated
2083          *      3. Keyframe lies at point that intersects the linear line between two keyframes
2084          */
2085         
2086         BezTriple *bezt=NULL, *prev=NULL;
2087         int totCount, i;
2088         float valA = 0.0f, valB = 0.0f;
2089         
2090         /* safety checking */
2091         if (!icu) return KEYNEEDED_JUSTADD;
2092         totCount= icu->totvert;
2093         if (totCount==0) return KEYNEEDED_JUSTADD;
2094         
2095         /* loop through checking if any are the same */
2096         bezt= icu->bezt;
2097         for (i=0; i<totCount; i++) {
2098                 float prevPosi=0.0f, prevVal=0.0f;
2099                 float beztPosi=0.0f, beztVal=0.0f;
2100                         
2101                 /* get current time+value */    
2102                 beztPosi= bezt->vec[1][0];
2103                 beztVal= bezt->vec[1][1];
2104                         
2105                 if (prev) {
2106                         /* there is a keyframe before the one currently being examined */               
2107                         
2108                         /* get previous time+value */
2109                         prevPosi= prev->vec[1][0];
2110                         prevVal= prev->vec[1][1];
2111                         
2112                         /* keyframe to be added at point where there are already two similar points? */
2113                         if (IS_EQ(prevPosi, cFrame) && IS_EQ(beztPosi, cFrame) && IS_EQ(beztPosi, prevPosi)) {
2114                                 return KEYNEEDED_DONTADD;
2115                         }
2116                         
2117                         /* keyframe between prev+current points ? */
2118                         if ((prevPosi <= cFrame) && (cFrame <= beztPosi)) {
2119                                 /* is the value of keyframe to be added the same as keyframes on either side ? */
2120                                 if (IS_EQ(prevVal, nValue) && IS_EQ(beztVal, nValue) && IS_EQ(prevVal, beztVal)) {
2121                                         return KEYNEEDED_DONTADD;
2122                                 }
2123                                 else {
2124                                         float realVal;
2125                                         
2126                                         /* get real value of curve at that point */
2127                                         realVal= eval_icu(icu, cFrame);
2128                                 
2129                                         /* compare whether it's the same as proposed */
2130                                         if (IS_EQ(realVal, nValue)) 
2131                                                 return KEYNEEDED_DONTADD;
2132                                         else 
2133                                                 return KEYNEEDED_JUSTADD;
2134                                 }
2135                         }
2136                         
2137                         /* new keyframe before prev beztriple? */
2138                         if (cFrame < prevPosi) {
2139                                 /* A new keyframe will be added. However, whether the previous beztriple
2140                                  * stays around or not depends on whether the values of previous/current
2141                                  * beztriples and new keyframe are the same.
2142                                  */
2143                                 if (IS_EQ(prevVal, nValue) && IS_EQ(beztVal, nValue) && IS_EQ(prevVal, beztVal))
2144                                         return KEYNEEDED_DELNEXT;
2145                                 else 
2146                                         return KEYNEEDED_JUSTADD;
2147                         }
2148                 }
2149                 else {
2150                         /* just add a keyframe if there's only one keyframe 
2151                          * and the new one occurs before the exisiting one does.
2152                          */
2153                         if ((cFrame < beztPosi) && (totCount==1))
2154                                 return KEYNEEDED_JUSTADD;
2155                 }
2156                 
2157                 /* continue. frame to do not yet passed (or other conditions not met) */
2158                 if (i < (totCount-1)) {
2159                         prev= bezt;
2160                         bezt++;
2161                 }
2162                 else
2163                         break;
2164         }
2165         
2166         /* Frame in which to add a new-keyframe occurs after all other keys
2167          * -> If there are at least two existing keyframes, then if the values of the
2168          *       last two keyframes and the new-keyframe match, the last existing keyframe
2169          *       gets deleted as it is no longer required.
2170          * -> Otherwise, a keyframe is just added. 1.0 is added so that fake-2nd-to-last
2171          *       keyframe is not equal to last keyframe.
2172          */
2173         bezt= (icu->bezt + (icu->totvert - 1));
2174         valA= bezt->vec[1][1];
2175         
2176         if (prev)
2177                 valB= prev->vec[1][1];
2178         else 
2179                 valB= bezt->vec[1][1] + 1.0f; 
2180                 
2181         if (IS_EQ(valA, nValue) && IS_EQ(valA, valB)) 
2182                 return KEYNEEDED_DELPREV;
2183         else 
2184                 return KEYNEEDED_JUSTADD;
2185 }
2186
2187 /* a duplicate of insertkey that does not check for routing to insertmatrixkey 
2188         to avoid recursion problems */
2189 static void insertkey_nonrecurs(ID *id, int blocktype, char *actname, char *constname, int adrcode)
2190 {
2191         IpoCurve *icu;
2192         Object *ob;
2193         void *poin= NULL;
2194         float curval, cfra;
2195         int vartype;
2196         int matset=0;
2197         
2198         if (matset==0) {
2199                 icu= verify_ipocurve(id, blocktype, actname, constname, adrcode);
2200                 
2201                 if(icu) {
2202                         
2203                         poin= get_context_ipo_poin(id, blocktype, actname, icu, &vartype);
2204                         
2205                         if(poin) {
2206                                 curval= read_ipo_poin(poin, vartype);
2207                                 
2208                                 cfra= frame_to_float(CFRA);
2209                                 
2210                                 /* if action is mapped in NLA, it returns a correction */
2211                                 if(actname && actname[0] && GS(id->name)==ID_OB)
2212                                         cfra= get_action_frame((Object *)id, cfra);
2213                                 
2214                                 if( GS(id->name)==ID_OB ) {
2215                                         ob= (Object *)id;
2216                                         if(ob->sf!=0.0 && (ob->ipoflag & OB_OFFS_OB) ) {
2217                                                 /* actually frametofloat calc again! */
2218                                                 cfra-= ob->sf*G.scene->r.framelen;
2219                                         }
2220                                 }
2221                                 
2222                                 insert_vert_icu(icu, cfra, curval, 0);
2223                         }
2224                 }
2225         }
2226 }
2227
2228 int insertmatrixkey(ID *id, int blocktype, char *actname, char *constname, int adrcode)
2229 {
2230         int matindex=0;
2231         /* branch on adrcode and blocktype, generating the proper matrix-based
2232         values to send to insertfloatkey */
2233         if (GS(id->name)==ID_OB) {
2234                 Object *ob= (Object *)id;
2235
2236                 if ( blocktype==ID_OB ){ //working with an object
2237                         if ((ob)&&!(ob->parent)) {
2238                                 if ((adrcode==OB_ROT_X)||(adrcode==OB_ROT_Y)||(adrcode==OB_ROT_Z)) { //get a rotation
2239                                         float eul[3];
2240                                         switch (adrcode) {
2241                                                 case OB_ROT_X:
2242                                                         matindex=0;
2243                                                         break;
2244                                                 case OB_ROT_Y:
2245                                                         matindex=1;
2246                                                         break;
2247                                                 case OB_ROT_Z:
2248                                                         matindex=2;
2249                                                         break;
2250                                         }
2251                                         Mat4ToEul(ob->obmat, eul);
2252                                         insertfloatkey(id, ID_OB, actname, NULL, adrcode, eul[matindex]*(5.72958));
2253                                         return 1;
2254                                 } else if ((adrcode==OB_LOC_X)||(adrcode==OB_LOC_Y)||(adrcode==OB_LOC_Z)) {//get a translation
2255                                         switch (adrcode) {
2256                                                 case OB_LOC_X:
2257                                                         matindex=0;
2258                                                         break;
2259                                                 case OB_LOC_Y:
2260                                                         matindex=1;
2261                                                         break;
2262                                                 case OB_LOC_Z:
2263                                                         matindex=2;
2264                                                         break;
2265                                         }
2266                                         insertfloatkey(id, ID_OB, actname, NULL, adrcode, ob->obmat[3][matindex]);
2267                                         return 1;
2268                                 }
2269                         }
2270                 } else if ( blocktype==ID_PO) { //working with a pose channel
2271                         bPoseChannel *pchan= get_pose_channel(ob->pose, actname);
2272                         if (pchan) {
2273                                 if ((adrcode==AC_LOC_X)||(adrcode==AC_LOC_Y)||(adrcode==AC_LOC_Z)) {
2274                                         switch (adrcode) {
2275                                                 case AC_LOC_X:
2276                                                         matindex=0;
2277                                                         break;
2278                                                 case AC_LOC_Y:
2279                                                         matindex=1;
2280                                                         break;
2281                                                 case AC_LOC_Z:
2282                                                         matindex=2;
2283                                                         break;
2284                                         }
2285                                         if (!(pchan->bone->parent)||((pchan->bone->parent)&&!(pchan->bone->flag&BONE_CONNECTED))) { /* don't use for non-connected child bones */
2286                                                 float delta_mat[4][4]; 
2287                                                 armature_mat_pose_to_delta(delta_mat, pchan->pose_mat, pchan->bone->arm_mat);
2288                                                 insertfloatkey(id, ID_PO, pchan->name, NULL, adrcode, delta_mat[3][matindex]);
2289                                                 return 1;
2290                                         }
2291                                 } else if ((adrcode==AC_QUAT_W)||(adrcode==AC_QUAT_X)||(adrcode==AC_QUAT_Y)||(adrcode==AC_QUAT_Z)) { 
2292                                         float tmat[4][4], trimat[3][3], localQuat[4];
2293                                         
2294                                         switch (adrcode) {
2295                                                 case AC_QUAT_W:
2296                                                         matindex=0;
2297                                                         break;
2298                                                 case AC_QUAT_X:
2299                                                         matindex=1;
2300                                                         break;
2301                                                 case AC_QUAT_Y:
2302                                                         matindex=2;
2303                                                         break;
2304                                                 case AC_QUAT_Z:
2305                                                         matindex=3;
2306                                                         break;
2307                                         }
2308                                         
2309                                         /* it should be reasonable to assume that we are keyframing on the active object, although it is not 
2310                                          * strictly required for this particular space conversion, arg1 must not be null for this to work
2311                                          */
2312                                         Mat4CpyMat4(tmat, pchan->pose_mat);
2313                                         constraint_mat_convertspace(OBACT, pchan, tmat, CONSTRAINT_SPACE_POSE, CONSTRAINT_SPACE_LOCAL);
2314                                         
2315                                         Mat3CpyMat4(trimat, tmat);
2316                                         Mat3ToQuat_is_ok(trimat, localQuat);
2317                                         insertfloatkey(id, ID_PO, pchan->name, NULL, adrcode, localQuat[matindex]);
2318                                         
2319                                         return 1;
2320                                 }
2321                         }
2322                 }
2323         }
2324         /* failed to set a matrix key -- use traditional, but the non-recursing version */
2325         insertkey_nonrecurs(id,blocktype,actname,constname,adrcode);
2326         return 0;
2327 }
2328
2329 static int match_adr_constraint(ID * id, int blocktype, char *actname, int adrcode)
2330 {       /* This function matches constraint blocks with adrcodes to see if the
2331                 visual keying method should be used. For example, an object looking to key
2332                 location and having a CopyLoc constraint would return true. */
2333                 
2334         Object *ob=NULL;
2335         int foundmatch=0;
2336         int searchtype=0;
2337         bConstraint *conref=NULL, *con=NULL;
2338         
2339         /*Retrieve constraint list*/
2340         if( GS(id->name)==ID_OB ) 
2341                 ob= (Object *)id;
2342         if (ob) {
2343                 if (blocktype==ID_PO) {
2344                         bPoseChannel *pchan= get_pose_channel(ob->pose, actname);
2345                         conref=pchan->constraints.first;
2346                 } else if (blocktype==ID_OB) {
2347                         conref=ob->constraints.first;
2348                 }
2349                 
2350                 if (conref) {
2351                         /*Set search type: 1 is for translation contraints, 2 is for rotation*/
2352                         if ((adrcode==OB_LOC_X)||(adrcode==OB_LOC_Y)||(adrcode==OB_LOC_Z)||(adrcode==AC_LOC_X)||(adrcode==AC_LOC_Y)||(adrcode==AC_LOC_Z)) {
2353                                 searchtype=1;
2354                         } else if ((adrcode==OB_ROT_X)||(adrcode==OB_ROT_Y)||(adrcode==OB_ROT_Z)||(adrcode==AC_QUAT_W)||(adrcode==AC_QUAT_X)||(adrcode==AC_QUAT_Y)||(adrcode==AC_QUAT_Z)) {
2355                                 searchtype=2;
2356                         }
2357                         
2358                         if (searchtype>0) {
2359                                 for (con=conref; (con)&&(foundmatch==0); con=con->next) {
2360                                         switch (con->type) {
2361                                         /* match constraint types to which kinds of keying they would affect */
2362                                                 case CONSTRAINT_TYPE_CHILDOF:
2363                                                         foundmatch=1;
2364                                                         break;
2365                                                 case CONSTRAINT_TYPE_TRACKTO:
2366                                                         if (searchtype==2) foundmatch=1;
2367                                                         break;
2368                                                 case CONSTRAINT_TYPE_FOLLOWPATH:
2369                                                         foundmatch=1;
2370                                                         break;
2371                                                 case CONSTRAINT_TYPE_ROTLIMIT:
2372                                                         if (searchtype==2) foundmatch=1;
2373                                                         break;
2374                                                 case CONSTRAINT_TYPE_LOCLIMIT:
2375                                                         if (searchtype==1) foundmatch=1;
2376                                                         break;
2377                                                 case CONSTRAINT_TYPE_ROTLIKE:
2378                                                         if (searchtype==2) foundmatch=1;
2379                                                         break;
2380                                                 case CONSTRAINT_TYPE_LOCLIKE:
2381                                                         if (searchtype==1) foundmatch=1;
2382                                                         break;
2383                                                 case CONSTRAINT_TYPE_LOCKTRACK:
2384                                                         if (searchtype==2) foundmatch=1;
2385                                                         break;
2386                                                 case CONSTRAINT_TYPE_DISTANCELIMIT:
2387                                                         if (searchtype==1) foundmatch=1;
2388                                                         break;
2389                                                 case CONSTRAINT_TYPE_MINMAX:
2390                                                         if (searchtype==1) foundmatch=1;
2391                                                         break;
2392                                                 case CONSTRAINT_TYPE_TRANSFORM:
2393                                                         foundmatch=1;
2394                                                         break;
2395                                                 default:
2396                                                         break;
2397                                         }
2398                                 }
2399                         }
2400                 }
2401         }
2402         
2403         return foundmatch;
2404                         
2405 }
2406
2407 void insertkey(ID *id, int blocktype, char *actname, char *constname, int adrcode, short fast)
2408 {
2409         IpoCurve *icu;
2410         Object *ob;
2411         void *poin= NULL;
2412         float curval, cfra;
2413         int vartype;
2414         int matset=0;
2415         
2416         if ((G.flags&G_AUTOMATKEYS)&&(match_adr_constraint(id, blocktype, actname, adrcode))) {
2417                 matset=insertmatrixkey(id, blocktype, actname, constname, adrcode);
2418         } 
2419         if (matset==0) {
2420                 icu= verify_ipocurve(id, blocktype, actname, constname, adrcode);
2421                 
2422                 if(icu) {
2423                         
2424                         poin= get_context_ipo_poin(id, blocktype, actname, icu, &vartype);
2425                         
2426                         if(poin) {
2427                                 curval= read_ipo_poin(poin, vartype);
2428                                 
2429                                 cfra= frame_to_float(CFRA);
2430                                 
2431                                 /* if action is mapped in NLA, it returns a correction */
2432                                 if(actname && actname[0] && GS(id->name)==ID_OB)
2433                                         cfra= get_action_frame((Object *)id, cfra);
2434                                 
2435                                 if( GS(id->name)==ID_OB ) {
2436                                         ob= (Object *)id;
2437                                         if(ob->sf!=0.0 && (ob->ipoflag & OB_OFFS_OB) ) {
2438                                                 /* actually frametofloat calc again! */
2439                                                 cfra-= ob->sf*G.scene->r.framelen;
2440                                         }
2441                                 }
2442                                 
2443                                 insert_vert_icu(icu, cfra, curval, fast);
2444                         }
2445                 }
2446         }
2447 }
2448
2449
2450
2451 /* This function is a 'smarter' version of the insert key code.
2452  * It uses an auxilliary function to check whether a keyframe is really needed */
2453 void insertkey_smarter(ID *id, int blocktype, char *actname, char *constname, int adrcode)
2454 {
2455         IpoCurve *icu;
2456         Object *ob;
2457         void *poin= NULL;
2458         float curval, cfra;
2459         int vartype;
2460         int insert_mode;
2461         
2462         icu= verify_ipocurve(id, blocktype, actname, constname, adrcode);
2463         
2464         if(icu) {
2465                 
2466                 poin= get_context_ipo_poin(id, blocktype, actname, icu, &vartype);
2467                 
2468                 if(poin) {
2469                         curval= read_ipo_poin(poin, vartype);
2470                         
2471                         cfra= frame_to_float(CFRA);
2472                         
2473                         /* if action is mapped in NLA, it returns a correction */
2474                         if(actname && actname[0] && GS(id->name)==ID_OB)
2475                                 cfra= get_action_frame((Object *)id, cfra);
2476                         
2477                         if( GS(id->name)==ID_OB ) {
2478                                 ob= (Object *)id;
2479                                 if(ob->sf!=0.0 && (ob->ipoflag & OB_OFFS_OB) ) {
2480                                         /* actually frametofloat calc again! */
2481                                         cfra-= ob->sf*G.scene->r.framelen;
2482                                 }
2483                         }
2484                         
2485                         /* check whether this curve really needs a new keyframe */
2486                         insert_mode= new_key_needed(icu, cfra, curval);
2487                         
2488                         /* insert new keyframe at current frame */
2489                         if (insert_mode) 
2490                                 insert_vert_icu(icu, cfra, curval, 0);
2491                         
2492                         /* delete keyframe immediately before/after newly added */
2493                         switch (insert_mode) {
2494                                 case KEYNEEDED_DELPREV:
2495                                         delete_icu_key(icu, icu->totvert-2);
2496                                         break;
2497                                 case KEYNEEDED_DELNEXT:
2498                                         delete_icu_key(icu, 1);
2499                                         break;
2500                         }
2501                 }
2502         }
2503 }
2504
2505 /* For inserting keys based on an arbitrary float value */
2506 void insertfloatkey(ID *id, int blocktype, char *actname, char *constname, int adrcode, float floatkey)
2507 {
2508         IpoCurve *icu;
2509         Object *ob;
2510         void *poin= NULL;
2511         float cfra;
2512         int vartype;
2513         
2514         icu= verify_ipocurve(id, blocktype, actname, constname, adrcode);
2515         
2516         if(icu) {
2517                 
2518                 poin= get_context_ipo_poin(id, blocktype, actname, icu, &vartype);
2519                 
2520                 if(poin) {
2521                         
2522                         cfra= frame_to_float(CFRA);
2523                         
2524                         /* if action is mapped in NLA, it returns a correction */
2525                         if(actname && actname[0] && GS(id->name)==ID_OB)
2526                                 cfra= get_action_frame((Object *)id, cfra);
2527                         
2528                         if( GS(id->name)==ID_OB ) {
2529                                 ob= (Object *)id;
2530                                 if(ob->sf!=0.0 && (ob->ipoflag & OB_OFFS_OB) ) {
2531                                         /* actually frametofloat calc again! */
2532                                         cfra-= ob->sf*G.scene->r.framelen;
2533                                 }
2534                         }
2535                         
2536                         /* insert new keyframe at current frame */
2537                         insert_vert_icu(icu, cfra, floatkey, 0);
2538                 }
2539         }
2540 }
2541
2542 void insertkey_editipo(void)
2543 {
2544         EditIpo *ei;
2545         IpoKey *ik;
2546         ID *id;
2547         float *fp, cfra, *insertvals;
2548         int a, nr, ok, tot;
2549         short event;
2550         
2551         ei= get_active_editipo();
2552         if(ei && ei->icu && ei->icu->driver) 
2553                 event= pupmenu("Insert Curve %t|Default one-to-one mapping %x3");
2554         else if(G.sipo->showkey)
2555                 event= pupmenu("Insert Key Vertices %t|Current Frame %x1|Selected Keys %x2");
2556         else 
2557                 event= pupmenu("Insert Key Vertices %t|Current Frame %x1");
2558         
2559         if(event<1) return;
2560         
2561         if(event==3) {
2562                 IpoDriver *driver= ei->icu->driver;
2563                 
2564                 if(ei->icu->bezt) MEM_freeN(ei->icu->bezt);
2565                 ei->icu->totvert= 0;
2566                 ei->icu->bezt= NULL;
2567                 
2568                 insert_vert_icu(ei->icu, 0.0f, 0.0f, 0);
2569                 
2570                 if(ELEM3(driver->adrcode, OB_ROT_X, OB_ROT_Y, OB_ROT_Z)) {
2571                         if(ei->disptype==IPO_DISPDEGR)
2572                                 insert_vert_icu(ei->icu, 18.0f, 18.0f, 0);
2573                         else
2574                                 insert_vert_icu(ei->icu, 18.0f, 1.0f, 0);
2575                 }
2576                 else
2577                         insert_vert_icu(ei->icu, 1.0f, 1.0f, 0);
2578                 
2579                 ei->flag |= IPO_SELECT|IPO_VISIBLE;
2580                 ei->icu->flag= ei->flag;
2581                 ei->icu->extrap= IPO_DIR;
2582
2583                 do_ipo_buttons(B_IPOHOME);
2584         }
2585         else {
2586                 ei= G.sipo->editipo;
2587                 for(nr=0; nr<G.sipo->totipo; nr++, ei++) {
2588                         if (ISPOIN(ei, flag & IPO_VISIBLE, icu)) {
2589                         
2590                                 ok= 0;
2591                                 if(G.sipo->showkey) ok= 1;
2592                                 else if(ei->flag & IPO_SELECT) ok= 1;
2593
2594                                 if(ok) {
2595                                         /* count amount */
2596                                         if(event==1) tot= 1;
2597                                         else {
2598                                                 ik= G.sipo->ipokey.first;
2599                                                 tot= 0;
2600                                                 while(ik) {
2601                                                         if(ik->flag & 1) tot++;
2602                                                         ik= ik->next;
2603                                                 }
2604                                         }
2605                                         if(tot) {
2606                                         
2607                                                 /* correction for ob timeoffs */
2608                                                 cfra= frame_to_float(CFRA);
2609                                                 id= G.sipo->from;       
2610                                                 if(id && GS(id->name)==ID_OB ) {
2611                                                         Object *ob= (Object *)id;
2612                                                         if(ob->sf!=0.0 && (ob->ipoflag & OB_OFFS_OB) ) {
2613                                                                 cfra-= ob->sf*G.scene->r.framelen;
2614                                                         }
2615                                                 }
2616                                                 else if(id && GS(id->name)==ID_SEQ) {
2617                                                         Sequence *last_seq = get_last_seq();    /* editsequence.c */
2618                                                         
2619                                                         if(last_seq && (last_seq->flag & SEQ_IPO_FRAME_LOCKED) == 0) {
2620                                                                 cfra= (float)(100.0*(cfra-last_seq->startdisp)/((float)(last_seq->enddisp-last_seq->startdisp)));
2621                                                         }
2622                                                 }
2623                                                 
2624                                                 /* convert cfra to ipo-time */
2625                                                 if (NLA_IPO_SCALED) {
2626                                                         cfra= get_action_frame(OBACT, cfra);
2627                                                 }
2628                                 
2629                                                 insertvals= MEM_mallocN(sizeof(float)*2*tot, "insertkey_editipo");
2630                                                 /* make sure icu->curval is correct */
2631                                                 calc_ipo(G.sipo->ipo, cfra);
2632                                                 
2633                                                 if(event==1) {
2634                                                         insertvals[0]= cfra;
2635                                                         
2636                                                         insertvals[1]= ei->icu->curval;
2637                                                 }
2638                                                 else {
2639                                                         fp= insertvals;
2640                                                         ik= G.sipo->ipokey.first;
2641                                                         while(ik) {
2642                                                                 if(ik->flag & 1) {
2643                                                                         calc_ipo(G.sipo->ipo, ik->val);
2644
2645                                                                         fp[0]= ik->val;
2646                                                                         fp[1]= ei->icu->curval;
2647                                                                         fp+= 2;
2648                                                                 }
2649                                                                 ik= ik->next;
2650                                                         }
2651                                                 }
2652                                                 fp= insertvals;
2653                                                 for(a=0; a<tot; a++, fp+=2) {
2654                                                         insert_vert_icu(ei->icu, fp[0], fp[1], 0);
2655                                                 }
2656                                                 
2657                                                 MEM_freeN(insertvals);
2658                                                 calc_ipo(G.sipo->ipo, (float)CFRA);
2659                                         }
2660                                 }
2661                         }
2662                 }
2663         }
2664         BIF_undo_push("Insert Key Ipo");
2665         allqueue (REDRAWACTION, 0);
2666         allqueue(REDRAWNLA, 0);
2667         allqueue(REDRAWIPO, 0);
2668         allspace(REMAKEIPO, 0);
2669 }
2670
2671
2672 void common_insertkey(void)
2673 {
2674         Base *base;
2675         Object *ob;
2676         Material *ma;
2677         ID *id;
2678         IpoCurve *icu;
2679         World *wo;
2680         Lamp *la;
2681         Tex *te;
2682         int tlay, map, event;
2683         char menustr[256];
2684
2685         if(curarea->spacetype==SPACE_IPO) {
2686                 insertkey_editipo();
2687         }
2688         else if(curarea->spacetype==SPACE_ACTION) {
2689                 insertkey_action();
2690         }
2691         else if(curarea->spacetype==SPACE_BUTS) {
2692                 if(G.buts->mainb==CONTEXT_SHADING) {
2693                         int tab= G.buts->tab[CONTEXT_SHADING];
2694
2695                         if(tab==TAB_SHADING_MAT) {
2696                                 ma = G.buts->lockpoin;
2697                                 ma = editnode_get_active_material(ma);
2698                                 id = (ID *)ma;
2699                                 
2700                                 if(id) {
2701                                         event= pupmenu("Insert Key %t|RGB%x0|Alpha%x1|Halo Size%x2|Mode %x3|All Color%x10|All Mirror%x14|Ofs%x12|Size%x13|All Mapping%x11");
2702                                         if(event== -1) return;
2703
2704                                         map= texchannel_to_adrcode(ma->texact);
2705
2706                                         if(event==0 || event==10) {
2707                                                 insertkey(id, ID_MA, NULL, NULL, MA_COL_R, 0);
2708                                                 insertkey(id, ID_MA, NULL, NULL, MA_COL_G, 0);
2709                                                 insertkey(id, ID_MA, NULL, NULL, MA_COL_B, 0);
2710                                         }
2711                                         if(event==1 || event==10) {
2712                                                 insertkey(id, ID_MA, NULL, NULL, MA_ALPHA, 0);
2713                                         }
2714                                         if(event==2 || event==10) {
2715                                                 insertkey(id, ID_MA, NULL, NULL, MA_HASIZE, 0);
2716                                         }
2717                                         if(event==3 || event==10) {
2718                                                 insertkey(id, ID_MA, NULL, NULL, MA_MODE, 0);
2719                                         }
2720                                         if(event==10) {
2721                                                 insertkey(id, ID_MA, NULL, NULL, MA_SPEC_R, 0);
2722                                                 insertkey(id, ID_MA, NULL, NULL, MA_SPEC_G, 0);
2723                                                 insertkey(id, ID_MA, NULL, NULL, MA_SPEC_B, 0);
2724                                                 insertkey(id, ID_MA, NULL, NULL, MA_REF, 0);
2725                                                 insertkey(id, ID_MA, NULL, NULL, MA_EMIT, 0);
2726                                                 insertkey(id, ID_MA, NULL, NULL, MA_AMB, 0);
2727                                                 insertkey(id, ID_MA, NULL, NULL, MA_SPEC, 0);
2728                                                 insertkey(id, ID_MA, NULL, NULL, MA_HARD, 0);
2729                                                 insertkey(id, ID_MA, NULL, NULL, MA_MODE, 0);
2730                                                 insertkey(id, ID_MA, NULL, NULL, MA_TRANSLU, 0);
2731                                                 insertkey(id, ID_MA, NULL, NULL, MA_ADD, 0);
2732                                         }
2733                                         if(event==14) {
2734                                                 insertkey(id, ID_MA, NULL, NULL, MA_RAYM, 0);
2735                                                 insertkey(id, ID_MA, NULL, NULL, MA_FRESMIR, 0);
2736                                                 insertkey(id, ID_MA, NULL, NULL, MA_FRESMIRI, 0);
2737                                                 insertkey(id, ID_MA, NULL, NULL, MA_FRESTRA, 0);
2738                                                 insertkey(id, ID_MA, NULL, NULL, MA_FRESTRAI, 0);
2739                                         }
2740                                         if(event==12 || event==11) {
2741                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_OFS_X, 0);
2742                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_OFS_Y, 0);
2743                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_OFS_Z, 0);
2744                                         }
2745                                         if(event==13 || event==11) {
2746                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_SIZE_X, 0);
2747                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_SIZE_Y, 0);
2748                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_SIZE_Z, 0);
2749                                         }
2750                                         if(event==11) {
2751                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_R, 0);
2752                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_G, 0);
2753                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_B, 0);
2754                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_DVAR, 0);
2755                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_COLF, 0);
2756                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_NORF, 0);
2757                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_VARF, 0);
2758                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_DISP, 0);
2759                                         }
2760                                 }
2761                         }
2762                         else if(tab==TAB_SHADING_WORLD) {
2763                                 id= G.buts->lockpoin;
2764                                 wo= G.buts->lockpoin;
2765                                 if(id) {
2766                                         event= pupmenu("Insert Key %t|Zenith RGB%x0|Horizon RGB%x1|Mist%x2|Stars %x3|Offset%x12|Size%x13");
2767                                         if(event== -1) return;
2768
2769                                         map= texchannel_to_adrcode(wo->texact);
2770
2771                                         if(event==0) {
2772                                                 insertkey(id, ID_WO, NULL, NULL, WO_ZEN_R, 0);
2773                                                 insertkey(id, ID_WO, NULL, NULL, WO_ZEN_G, 0);
2774                                                 insertkey(id, ID_WO, NULL, NULL, WO_ZEN_B, 0);
2775                                         }
2776                                         if(event==1) {
2777                                                 insertkey(id, ID_WO, NULL, NULL, WO_HOR_R, 0);
2778                                                 insertkey(id, ID_WO, NULL, NULL, WO_HOR_G, 0);
2779                                                 insertkey(id, ID_WO, NULL, NULL, WO_HOR_B, 0);
2780                                         }
2781                                         if(event==2) {
2782                                                 insertkey(id, ID_WO, NULL, NULL, WO_MISI, 0);
2783                                                 insertkey(id, ID_WO, NULL, NULL, WO_MISTDI, 0);
2784                                                 insertkey(id, ID_WO, NULL, NULL, WO_MISTSTA, 0);
2785                                                 insertkey(id, ID_WO, NULL, NULL, WO_MISTHI, 0);
2786                                         }
2787                                         if(event==3) {
2788                                                 insertkey(id, ID_WO, NULL, NULL, WO_STAR_R, 0);
2789                                                 insertkey(id, ID_WO, NULL, NULL, WO_STAR_G, 0);
2790                                                 insertkey(id, ID_WO, NULL, NULL, WO_STAR_B, 0);
2791                                                 insertkey(id, ID_WO, NULL, NULL, WO_STARDIST, 0);
2792                                                 insertkey(id, ID_WO, NULL, NULL, WO_STARSIZE, 0);
2793                                         }
2794                                         if(event==12) {
2795                                                 insertkey(id, ID_WO, NULL, NULL, map+MAP_OFS_X, 0);
2796                                                 insertkey(id, ID_WO, NULL, NULL, map+MAP_OFS_Y, 0);
2797                                                 insertkey(id, ID_WO, NULL, NULL, map+MAP_OFS_Z, 0);
2798                                         }
2799                                         if(event==13) {
2800                                                 insertkey(id, ID_WO, NULL, NULL, map+MAP_SIZE_X, 0);
2801                                                 insertkey(id, ID_WO, NULL, NULL, map+MAP_SIZE_Y, 0);
2802                                                 insertkey(id, ID_WO, NULL, NULL, map+MAP_SIZE_Z, 0);
2803                                         }
2804                                 }
2805                         }
2806                         else if(tab==TAB_SHADING_LAMP) {
2807                                 id= G.buts->lockpoin;
2808                                 la= G.buts->lockpoin;
2809                                 if(id) {
2810                                         event= pupmenu("Insert Key %t|RGB%x0|Energy%x1|Spot Size%x2|Offset%x12|Size%x13");
2811                                         if(event== -1) return;
2812
2813                                         map= texchannel_to_adrcode(la->texact);
2814
2815                                         if(event==0) {
2816                                                 insertkey(id, ID_LA, NULL, NULL, LA_COL_R, 0);
2817                                                 insertkey(id, ID_LA, NULL, NULL, LA_COL_G, 0);
2818                                                 insertkey(id, ID_LA, NULL, NULL, LA_COL_B, 0);
2819                                         }
2820                                         if(event==1) {
2821                                                 insertkey(id, ID_LA, NULL, NULL, LA_ENERGY, 0);
2822                                         }
2823                                         if(event==2) {
2824                                                 insertkey(id, ID_LA, NULL, NULL, LA_SPOTSI, 0);
2825                                         }
2826                                         if(event==12) {
2827                                                 insertkey(id, ID_LA, NULL, NULL, map+MAP_OFS_X, 0);
2828                                                 insertkey(id, ID_LA, NULL, NULL, map+MAP_OFS_Y, 0);
2829                                                 insertkey(id, ID_LA, NULL, NULL, map+MAP_OFS_Z, 0);
2830                                         }
2831                                         if(event==13) {
2832                                                 insertkey(id, ID_LA, NULL, NULL, map+MAP_SIZE_X, 0);
2833                                                 insertkey(id, ID_LA, NULL, NULL, map+MAP_SIZE_Y, 0);
2834                                                 insertkey(id, ID_LA, NULL, NULL, map+MAP_SIZE_Z, 0);
2835                                         }
2836
2837                                 }
2838                         }
2839                         else if(tab==TAB_SHADING_TEX) {
2840                                 id= G.buts->lockpoin;
2841                                 te= G.buts->lockpoin;
2842                                 if(id) {
2843                                         event= pupmenu("Insert Key %t|Clouds%x0|Marble%x1|Stucci%x2|Wood%x3|Magic%x4|Blend%x5|Musgrave%x6|Voronoi%x7|DistortedNoise%x8|ColorFilter%x9");
2844                                         if(event== -1) return;
2845
2846                                         if(event==0) {
2847                                                 insertkey(id, ID_TE, NULL, NULL, TE_NSIZE, 0);
2848                                                 insertkey(id, ID_TE, NULL, NULL, TE_NDEPTH, 0);
2849                                                 insertkey(id, ID_TE, NULL, NULL, TE_NTYPE, 0);
2850                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_TYP, 0);
2851                                                 insertkey(id, ID_TE, NULL, NULL, TE_N_BAS1, 0);
2852                                         }
2853                                         if(event==1) {
2854                                                 insertkey(id, ID_TE, NULL, NULL, TE_NSIZE, 0);
2855                                                 insertkey(id, ID_TE, NULL, NULL, TE_NDEPTH, 0);
2856                                                 insertkey(id, ID_TE, NULL, NULL, TE_NTYPE, 0);
2857                                                 insertkey(id, ID_TE, NULL, NULL, TE_TURB, 0);
2858                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_TYP, 0);
2859                                                 insertkey(id, ID_TE, NULL, NULL, TE_N_BAS1, 0);
2860                                                 insertkey(id, ID_TE, NULL, NULL, TE_N_BAS2, 0);
2861                                         }
2862                                         if(event==2) {
2863                                                 insertkey(id, ID_TE, NULL, NULL, TE_NSIZE, 0);
2864                                                 insertkey(id, ID_TE, NULL, NULL, TE_NTYPE, 0);
2865                                                 insertkey(id, ID_TE, NULL, NULL, TE_TURB, 0);
2866                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_TYP, 0);
2867                                                 insertkey(id, ID_TE, NULL, NULL, TE_N_BAS1, 0);
2868                                         }
2869                                         if(event==3) {
2870                                                 insertkey(id, ID_TE, NULL, NULL, TE_NSIZE, 0);
2871                                                 insertkey(id, ID_TE, NULL, NULL, TE_NTYPE, 0);
2872                                                 insertkey(id, ID_TE, NULL, NULL, TE_TURB, 0);
2873                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_TYP, 0);
2874                                                 insertkey(id, ID_TE, NULL, NULL, TE_N_BAS1, 0);
2875                                                 insertkey(id, ID_TE, NULL, NULL, TE_N_BAS2, 0);
2876                                         }
2877                                         if(event==4) {
2878                                                 insertkey(id, ID_TE, NULL, NULL, TE_NDEPTH, 0);
2879                                                 insertkey(id, ID_TE, NULL, NULL, TE_TURB, 0);
2880                                         }
2881                                         if(event==5) {
2882                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_TYP, 0);
2883                                         } 
2884                                         if(event==6) {
2885                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_TYP, 0);
2886                                                 insertkey(id, ID_TE, NULL, NULL, TE_MGH, 0);
2887                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_LAC, 0);
2888                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_OCT, 0);
2889                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_OFF, 0);
2890                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_GAIN, 0);
2891                                         }
2892                                         if(event==7) {
2893                                                 insertkey(id, ID_TE, NULL, NULL, TE_VNW1, 0);
2894                                                 insertkey(id, ID_TE, NULL, NULL, TE_VNW2, 0);
2895                                                 insertkey(id, ID_TE, NULL, NULL, TE_VNW3, 0);
2896                                                 insertkey(id, ID_TE, NULL, NULL, TE_VNW4, 0);
2897                                                 insertkey(id, ID_TE, NULL, NULL, TE_VNMEXP, 0);
2898                                                 insertkey(id, ID_TE, NULL, NULL, TE_VN_DISTM, 0);
2899                                                 insertkey(id, ID_TE, NULL, NULL, TE_VN_COLT, 0);
2900                                                 insertkey(id, ID_TE, NULL, NULL, TE_ISCA, 0);
2901                                                 insertkey(id, ID_TE, NULL, NULL, TE_NSIZE, 0);
2902                                         }
2903                                         if(event==8) {
2904                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_OCT, 0);
2905                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_OFF, 0);
2906                                                 insertkey(id, ID_TE, NULL, NULL, TE_MG_GAIN, 0);
2907                                                 insertkey(id, ID_TE, NULL, NULL, TE_DISTA, 0);
2908                                         }
2909                                         if(event==9) {
2910                                                 insertkey(id, ID_TE, NULL, NULL, TE_COL_R, 0);
2911                                                 insertkey(id, ID_TE, NULL, NULL, TE_COL_G, 0);
2912                                                 insertkey(id, ID_TE, NULL, NULL, TE_COL_B, 0);
2913                                                 insertkey(id, ID_TE, NULL, NULL, TE_BRIGHT,&n