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