== Action/Pose Groups - Keyframing Integration ==
[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                 float pixelwidth;
1480                 pixelwidth= (G.v2d->cur.xmax-G.v2d->cur.xmin)/(G.v2d->mask.xmax-G.v2d->mask.xmin); /* could make a generic function */
1481                 
1482                 getmouseco_areawin(mval);
1483                 
1484                 areamouseco_to_ipoco(G.v2d, mval, &x, &y);
1485                 actik= 0;
1486                 mindist= 1000.0;
1487                 ik= G.sipo->ipokey.first;
1488                 while(ik) {
1489                         dist= (float)(fabs(ik->val-x));
1490                         if(ik->flag & SELECT) dist+= pixelwidth;
1491                         if(dist < mindist) {
1492                                 actik= ik;
1493                                 mindist= dist;
1494                         }
1495                         ik= ik->next;
1496                 }
1497                 if(actik) {
1498                         oldflag= actik->flag;
1499                         
1500                         if(G.qual & LR_SHIFTKEY);
1501                         else deselectall_editipo();
1502                         
1503                         if(G.qual & LR_SHIFTKEY) {
1504                                 if(oldflag & 1) actik->flag &= ~1;
1505                                 else actik->flag |= 1;
1506                         }
1507                         else {
1508                                 actik->flag |= 1;
1509                         }
1510                 }
1511         }
1512         else if(totipo_edit) {
1513                 
1514                 hand= findnearest_ipovert(&icu, &bezt);
1515                 
1516                 if(G.qual & LR_SHIFTKEY) {
1517                         if(bezt) {
1518                                 if(hand==1) {
1519                                         if(BEZSELECTED(bezt)) {
1520                                                 bezt->f1= bezt->f2= bezt->f3= 0;
1521                                         }
1522                                         else {
1523                                                 bezt->f1= bezt->f2= bezt->f3= SELECT;
1524                                         }
1525                                 }
1526                                 else if(hand==0) {
1527                                         if(bezt->f1 & SELECT) bezt->f1= 0;
1528                                         else bezt->f1= SELECT;
1529                                 }
1530                                 else {
1531                                         if(bezt->f3 & SELECT) bezt->f3= 0;
1532                                         else bezt->f3= SELECT;
1533                                 }
1534                         }                               
1535                 }
1536                 else {
1537                         deselectall_editipo();
1538                         
1539                         if(bezt) {
1540                                 if(hand==1) {
1541                                         bezt->f1|= SELECT; bezt->f2|= SELECT; bezt->f3|= SELECT;
1542                                 }
1543                                 else if(hand==0) bezt->f1 |= SELECT;
1544                                 else bezt->f3 |= SELECT;
1545                         }
1546                 }
1547         }
1548         else if (marker) {
1549                 /* select timeline marker */
1550                 if ((G.qual & LR_SHIFTKEY)==0) {
1551                         oldflag= marker->flag;
1552                         deselect_markers(0, 0);
1553                         
1554                         if (oldflag & SELECT)
1555                                 marker->flag &= ~SELECT;
1556                         else
1557                                 marker->flag |= SELECT;
1558                 }
1559                 else {
1560                         marker->flag |= SELECT;                         
1561                 }               
1562         }
1563         else {
1564                 
1565                 /* vertex keys ? */
1566                 if(G.sipo->blocktype==ID_KE && G.sipo->from) {
1567                         Key *key;
1568                         KeyBlock *kb, *curkb;
1569                         int i, index= 1;
1570                         
1571                         ob= (Object *)G.sipo->from;
1572                         key= ob_get_key(ob);
1573                         curkb= BLI_findlink(&key->block, ob->shapenr-1);
1574                         
1575                         ei= G.sipo->editipo;
1576                         if(key->type==KEY_NORMAL || (ei->flag & IPO_VISIBLE)) {
1577                                 getmouseco_areawin(mval);
1578                                 
1579                                 areamouseco_to_ipoco(G.v2d, mval, &x, &y);
1580                                 /* how much is 20 pixels? */
1581                                 mindist= (float)(20.0*(G.v2d->cur.ymax-G.v2d->cur.ymin)/(float)curarea->winy);
1582                                 
1583                                 for(i=1, kb= key->block.first; kb; kb= kb->next, i++) {
1584                                         dist= (float)(fabs(kb->pos-y));
1585                                         if(kb==curkb) dist+= (float)0.01;
1586                                         if(dist < mindist) {
1587                                                 actkb= kb;
1588                                                 mindist= dist;
1589                                                 index= i;
1590                                         }
1591                                 }
1592                                 if(actkb) {
1593                                         ok= TRUE;
1594                                         if(G.obedit && actkb!=curkb) {
1595                                                 ok= okee("Copy key after leaving Edit Mode");
1596                                         }
1597                                         if(ok) {
1598                                                 /* also does all keypos */
1599                                                 deselectall_editipo();
1600                                                 set_active_key(index);
1601                                                 set_active_editipo(ei+index-1);
1602                                         }
1603                                 }
1604                         }
1605                 }
1606                 
1607                 /* select curve */
1608                 if(actkb==NULL) {
1609                         if(totipo_vis==1) {
1610                                 ei= G.sipo->editipo;
1611                                 for(a=0; a<G.sipo->totipo; a++, ei++) {
1612                                         if(ei->icu) {
1613                                                 if(ei->flag & IPO_VISIBLE) actei= ei;
1614                                         }
1615                                 }
1616                         }
1617                         else if(totipo_vis>1) {
1618                                 actei= select_proj_ipo(0, 0);
1619                         }
1620                         
1621                         if(actei) oldflag= actei->flag;
1622                         
1623                         if(G.qual & LR_SHIFTKEY);
1624                         else deselectall_editipo();
1625                         
1626                         if(actei) {
1627                                 if(G.qual & LR_SHIFTKEY) {
1628                                         if(oldflag & IPO_SELECT) actei->flag &= ~IPO_SELECT;
1629                                         else actei->flag |= IPO_SELECT;
1630                                 }
1631                                 else {
1632                                         actei->flag |= IPO_SELECT;
1633                                 }
1634                                 set_active_editipo(actei);
1635                         }
1636                 }
1637         }
1638         
1639         /* undo mapping of ipo-points for editing if scaled ipo */
1640         if (NLA_IPO_SCALED) {
1641                 actstrip_map_ipo_keys(OBACT, G.sipo->ipo, 1, 0);
1642         }
1643         
1644         update_editipo_flags();
1645         
1646         force_draw(0);
1647         BIF_undo_push("Select Ipo");
1648         
1649         if(G.sipo->showkey && G.sipo->blocktype==ID_OB) {
1650                 ob= OBACT;
1651                 if(ob && (ob->ipoflag & OB_DRAWKEY)) allqueue(REDRAWVIEW3D, 0);
1652         }
1653         /* points inside of curve are drawn selected too */
1654         if(G.sipo->blocktype==ID_CU)
1655                 allqueue(REDRAWVIEW3D, 0);
1656         
1657         getmouseco_areawin(mval);
1658         xo= mval[0]; 
1659         yo= mval[1];
1660         
1661         while(get_mbut()&R_MOUSE) {             
1662                 getmouseco_areawin(mval);
1663                 if(abs(mval[0]-xo)+abs(mval[1]-yo) > 4) {
1664                         
1665                         if (marker) {
1666                                 transform_markers('g', 0);
1667                         }
1668                         else {
1669                                 if(actkb) move_keys(OBACT);
1670                                 else transform_ipo('g');
1671                         }
1672                         
1673                         return;
1674                 }
1675                 BIF_wait_for_statechange();
1676         }
1677 }
1678
1679
1680 /* *********************************** */
1681
1682 /* handling of right-hand channel/curve buttons in ipo window */
1683 void do_ipowin_buts(short event)
1684 {
1685         EditIpo *ei = NULL;
1686         int a;
1687
1688         /* without shift, all other channels are made invisible */
1689         if((G.qual & LR_SHIFTKEY)==0) {
1690                 if(event>G.sipo->totipo) return;
1691                 ei = G.sipo->editipo;
1692                 for(a=0; a<G.sipo->totipo; a++) {
1693                         if(a!=event) ei->flag &= ~IPO_VISIBLE;
1694                         else ei->flag |= IPO_VISIBLE;
1695                         ei++;
1696                 }
1697         }
1698         
1699         /* set active */
1700         if(event>=0 && event<G.sipo->totipo) {
1701                 ei= G.sipo->editipo;    // void pointer...
1702                 set_active_editipo(ei+event);
1703                 set_active_key(event+1);        // only if there's a key, of course
1704         }
1705         scrarea_queue_winredraw(curarea);
1706         
1707         update_editipo_flags();
1708         get_status_editipo();
1709
1710         if(G.sipo->showkey) {
1711                 make_ipokey();
1712                 if(G.sipo->blocktype==ID_OB) allqueue(REDRAWVIEW3D, 0);
1713         }
1714
1715 }
1716
1717 /* the fake buttons to the left of channel names, for select/deselect curves */
1718 void do_ipo_selectbuttons(void)
1719 {
1720         EditIpo *ei, *ei1;
1721         int a, nr;
1722         short mval[2];
1723         
1724         if(G.sipo->showkey) return;
1725         
1726         /* do not allow editipo here: convert editipos to selected */
1727         get_status_editipo();
1728         if(totipo_edit) {
1729                 set_editflag_editipo();
1730         }
1731         
1732         /* which */
1733         getmouseco_areawin(mval);
1734
1735         nr= -(mval[1]-curarea->winy+30-G.sipo->butofs-IPOBUTY)/IPOBUTY;
1736         if(G.sipo->blocktype==ID_KE) nr--;              /* keys show something else in first channel */
1737         
1738         if(nr>=0 && nr<G.sipo->totipo) {
1739                 ei= G.sipo->editipo;
1740                 ei+= nr;
1741                 
1742                 set_active_editipo(ei);
1743                 set_active_key(nr+1);
1744
1745                 if(ei->icu) {
1746                         if((ei->flag & IPO_VISIBLE)==0) {
1747                                 ei->flag |= IPO_VISIBLE|IPO_SELECT;
1748                         }
1749         
1750                         if((G.qual & LR_SHIFTKEY)==0) {
1751                                 ei1= G.sipo->editipo;
1752                                 for(a=0; a<G.sipo->totipo; a++) {
1753                                         ei1->flag &= ~IPO_SELECT;
1754                                         ei1++;
1755                                 }
1756                         }
1757
1758                         if(ei->flag & IPO_SELECT) {
1759                                 ei->flag &= ~IPO_SELECT;
1760                         }
1761                         else {
1762                                 ei->flag |= IPO_SELECT;
1763                         }
1764                         
1765                         update_editipo_flags();
1766                         scrarea_queue_winredraw(curarea);
1767                 }
1768         }
1769         BIF_undo_push("Select Ipo curve");
1770 }
1771
1772 /* ********************************* Inserting keys ********************************************* */
1773
1774 /* depending type, it returns ipo, if needed it creates one */
1775 /* returns either action ipo or "real" ipo */
1776 /* arguments define full context;
1777    - *from has to be set always, to Object in case of Actions
1778    - blocktype defines available channels of Ipo struct (blocktype ID_OB can be in action too)
1779    - if actname, use this to locate actionchannel, and optional constname 
1780    - if bonename, the constname is the ipo to the constraint
1781 */
1782
1783 /* note; check header_ipo.c, spaceipo_assign_ipo() too */
1784 Ipo *verify_ipo(ID *from, short blocktype, char *actname, char *constname, char *bonename)
1785 {
1786
1787         if(from==NULL || from->lib) return NULL;
1788         
1789         /* first check action ipos */
1790         if(actname && actname[0]) {
1791                 Object *ob= (Object *)from;
1792                 bActionChannel *achan;
1793                 
1794                 if(GS(from->name)!=ID_OB) {
1795                         printf("called ipo system for action with wrong base pointer\n");
1796                         return NULL;
1797                 }
1798                 
1799                 if(ob->action==NULL)
1800                         ob->action= add_empty_action("Action");
1801                 
1802                 achan= verify_action_channel(ob->action, actname);
1803                 
1804                 if(achan) {
1805                         /* automatically assign achan to act-group based on pchan's grouping */
1806                         if (blocktype == ID_PO)
1807                         verify_pchan2achan_grouping(ob->action, ob->pose, actname);
1808                         
1809                         /* constraint exception */
1810                         if(blocktype==ID_CO) {
1811                                 bConstraintChannel *conchan= verify_constraint_channel(&achan->constraintChannels, constname);
1812                                 if(conchan->ipo==NULL) {
1813                                         conchan->ipo= add_ipo("CoIpo", ID_CO);  
1814                                 }
1815                                 return conchan->ipo;
1816                         }
1817                         else {
1818                                 if(achan->ipo==NULL) {
1819                                         achan->ipo= add_ipo("ActIpo", blocktype);
1820                                 }
1821                                 
1822                                 return achan->ipo;
1823                         }
1824                 }
1825         }
1826         else {
1827                 
1828                 switch(GS(from->name)) {
1829                 case ID_OB:
1830                         {
1831                                 Object *ob= (Object *)from;
1832                                 
1833                                 /* constraint exception */
1834                                 if(blocktype==ID_CO) {
1835                                         /* check the local constraint ipo */
1836                                         if(bonename && bonename[0] && ob->pose) {
1837                                                 bPoseChannel *pchan= get_pose_channel(ob->pose, bonename);
1838                                                 bConstraint *con;
1839                                                 for(con= pchan->constraints.first; con; con= con->next)
1840                                                         if(strcmp(con->name, constname)==0)
1841                                                                 break;
1842                                                 if(con) {
1843                                                         if(con->ipo==NULL) {
1844                                                                 con->ipo= add_ipo("CoIpo", ID_CO);
1845                                                         }
1846                                                         return con->ipo;
1847                                                 }
1848                                         }
1849                                         else { /* the actionchannel */
1850                                                 bConstraintChannel *conchan= verify_constraint_channel(&ob->constraintChannels, constname);
1851                                                 if(conchan->ipo==NULL) {
1852                                                         conchan->ipo= add_ipo("CoIpo", ID_CO);  
1853                                                 }
1854                                                 return conchan->ipo;
1855                                         }
1856                                 }
1857                                 else if(blocktype==ID_OB) {
1858                                         if(ob->ipo==NULL) {
1859                                                 ob->ipo= add_ipo("ObIpo", ID_OB);
1860                                         }
1861                                         return ob->ipo;
1862                                 }
1863                                 else if(blocktype==ID_KE) {
1864                                         Key *key= ob_get_key((Object *)from);
1865                                         
1866                                         if(key) {
1867                                                 if(key->ipo==NULL) {
1868                                                         key->ipo= add_ipo("KeyIpo", ID_KE);
1869                                                 }
1870                                                 return key->ipo;
1871                                         }
1872                                         return NULL;
1873                                 }
1874                                 else if(blocktype== ID_FLUIDSIM) {
1875                                         Object *ob= (Object *)from;
1876                                         if(ob->fluidsimFlag & OB_FLUIDSIM_ENABLE) {
1877                                                 FluidsimSettings *fss= ob->fluidsimSettings;
1878                                                 if(fss->ipo==NULL) {
1879                                                         fss->ipo= add_ipo("FluidsimIpo", ID_FLUIDSIM);
1880                                                         //fprintf(stderr,"FSIPO NEW!\n");
1881                                                 }
1882                                                 return fss->ipo;
1883                                         }
1884                                 }
1885                                 else if(blocktype== ID_PA){
1886                                         Object *ob= (Object *)from;
1887                                         ParticleSystem *psys= psys_get_current(ob);
1888                                         if(psys){
1889                                                 if(psys->part->ipo==0)
1890                                                         psys->part->ipo= add_ipo("ParticleIpo", ID_PA);
1891                                                 return psys->part->ipo;
1892                                         }
1893                                         return NULL;
1894                                 }
1895                         }
1896                         break;
1897                 case ID_MA:
1898                         {
1899                                 Material *ma= (Material *)from;
1900
1901                                 if(ma->ipo==NULL) {
1902                                         ma->ipo= add_ipo("MatIpo", ID_MA);
1903                                 }
1904                                 return ma->ipo;
1905                         }
1906                         break;
1907                 case ID_TE:
1908                         {
1909                                 Tex *tex= (Tex *)from;
1910
1911                                 if(tex->ipo==NULL) {
1912                                         tex->ipo= add_ipo("TexIpo", ID_TE);
1913                                 }
1914                                 return tex->ipo;
1915                         }
1916                         break;
1917                 case ID_SEQ:
1918                         {
1919                                 Sequence *seq= (Sequence *)from;        /* note, sequence is mimicing Id */
1920
1921                                 if(seq->ipo==NULL) {
1922                                         seq->ipo= add_ipo("SeqIpo", ID_SEQ);
1923                                 }
1924                                 update_seq_ipo_rect(seq);
1925                                 return seq->ipo;
1926                         }
1927                         break;
1928                 case ID_CU:
1929                         {
1930                                 Curve *cu= (Curve *)from;
1931                                 
1932                                 if(cu->ipo==NULL) {
1933                                         cu->ipo= add_ipo("CuIpo", ID_CU);
1934                                 }
1935                                 return cu->ipo;
1936                         }
1937                         break;
1938                 case ID_WO:
1939                         {
1940                                 World *wo= (World *)from;
1941
1942                                 if(wo->ipo==NULL) {
1943                                         wo->ipo= add_ipo("WoIpo", ID_WO);
1944                                 }
1945                                 return wo->ipo;
1946                         }
1947                         break;
1948                 case ID_LA:
1949                         {
1950                                 Lamp *la= (Lamp *)from;
1951                                 
1952                                 if(la->ipo==NULL) {
1953                                         la->ipo= add_ipo("LaIpo", ID_LA);
1954                                 }
1955                                 return la->ipo;
1956                         }
1957                         break;
1958                 case ID_CA:
1959                         {
1960                                 Camera *ca= (Camera *)from;
1961
1962                                 if(ca->ipo==NULL) {
1963                                         ca->ipo= add_ipo("CaIpo", ID_CA);
1964                                 }
1965                                 return ca->ipo;
1966                         }
1967                         break;
1968                 case ID_SO:
1969                         {
1970                                 bSound *snd= (bSound *)from;
1971
1972                                 if(snd->ipo==NULL) {
1973                                         snd->ipo= add_ipo("SndIpo", ID_SO);
1974                                 }
1975                                 return snd->ipo;
1976                         }
1977                 }
1978         }
1979         
1980         return NULL;    
1981 }
1982
1983 /* returns and creates
1984  * Make sure functions check for NULL or they will crash!
1985  *  */
1986 IpoCurve *verify_ipocurve(ID *from, short blocktype, char *actname, char *constname, char *bonename, int adrcode)
1987 {
1988         Ipo *ipo;
1989         IpoCurve *icu= NULL;
1990         
1991         /* return 0 if lib */
1992         /* creates ipo too */
1993         ipo= verify_ipo(from, blocktype, actname, constname, bonename);
1994         
1995         if(ipo && ipo->id.lib==NULL && from->lib==NULL) {
1996                 
1997                 for(icu= ipo->curve.first; icu; icu= icu->next) {
1998                         if(icu->adrcode==adrcode) break;
1999                 }
2000                 if(icu==NULL) {
2001                         icu= MEM_callocN(sizeof(IpoCurve), "ipocurve");
2002
2003                         icu->flag |= IPO_VISIBLE|IPO_AUTO_HORIZ;
2004                         if(ipo->curve.first==NULL) icu->flag |= IPO_ACTIVE;     /* first one added active */
2005                         
2006                         icu->blocktype= blocktype;
2007                         icu->adrcode= adrcode;
2008                         
2009                         set_icu_vars(icu);
2010
2011                         BLI_addtail( &(ipo->curve), icu);
2012
2013                         switch (GS(from->name)) {
2014                         case ID_SEQ: {
2015                                 Sequence *seq= (Sequence *)from;
2016
2017                                 update_seq_icu_rects(seq);
2018                                 break;
2019                         }
2020                         }
2021                 }
2022         }
2023
2024         return icu;
2025 }
2026
2027 /* This function adds a given BezTriple to an IPO-Curve. It will allocate 
2028  * memory for the array if needed, and will insert the BezTriple into a
2029  * suitable place in chronological order.
2030  * 
2031  * NOTE: any recalculate of the IPO-Curve that needs to be done will need to 
2032  *              be done by the caller.
2033  */
2034 int insert_bezt_icu (IpoCurve *icu, BezTriple *bezt)
2035 {
2036         BezTriple *newb, *beztd;
2037         int i= 0;
2038         
2039         if (icu->bezt == NULL) {
2040                 icu->bezt= MEM_callocN(sizeof(BezTriple), "beztriple");
2041                 *(icu->bezt)= *bezt;
2042                 icu->totvert= 1;
2043         }
2044         else {
2045                 beztd= icu->bezt;
2046                 for (i = 0; i <= icu->totvert; i++, beztd++) {
2047                         /* no double points - threshold to determine this should be good enough */
2048                         if ((i < icu->totvert) && IS_EQT(beztd->vec[1][0], bezt->vec[1][0], 0.00001)) {
2049                                 *(beztd)= *bezt;
2050                                 break;
2051                         }
2052                         /* if we've reached the end of the icu array, or bezt is to be pasted before current */
2053                         if (i==icu->totvert || beztd->vec[1][0] > bezt->vec[1][0]) {
2054                                 newb= MEM_callocN( (icu->totvert+1)*sizeof(BezTriple), "beztriple");
2055                                 
2056                                 /* add the beztriples that should occur before the beztriple to be pasted (originally in ei->icu) */
2057                                 if (i > 0) 
2058                                         memcpy(newb, icu->bezt, i*sizeof(BezTriple));
2059                                 
2060                                 /* add beztriple to paste at index j */
2061                                 *(newb+i)= *bezt;
2062                                 
2063                                 /* add the beztriples that occur after the beztriple to be pasted (originally in icu) */
2064                                 if (i < icu->totvert) 
2065                                         memcpy(newb+i+1, icu->bezt+i, (icu->totvert-i)*sizeof(BezTriple));
2066                                 
2067                                 MEM_freeN(icu->bezt);
2068                                 icu->bezt= newb;
2069                                 
2070                                 icu->totvert++;
2071                                 break;
2072                         }
2073                 }
2074         }
2075         
2076         /* we need to return the index, so that some tools which do post-processing can 
2077          * detect where we added the BezTriple in the array
2078          */
2079         return i;
2080 }
2081
2082 /* This function is a wrapper for insert_bezt_icu, and should be used when
2083  * adding a new keyframe to a curve, when the keyframe doesn't exist anywhere
2084  * else yet. 
2085  * 
2086  * 'fast' - is only for the python API where importing BVH's would take an extreamly long time.
2087  */
2088 void insert_vert_icu (IpoCurve *icu, float x, float y, short fast)
2089 {
2090         BezTriple beztr;
2091         int a, h1, h2;
2092         
2093         /* set all three points, for nicer start position */
2094         memset(&beztr, 0, sizeof(BezTriple));
2095         beztr.vec[0][0]= x; 
2096         beztr.vec[0][1]= y;
2097         beztr.vec[1][0]= x;
2098         beztr.vec[1][1]= y;
2099         beztr.vec[2][0]= x;
2100         beztr.vec[2][1]= y;
2101         beztr.hide= IPO_BEZ;
2102         beztr.f1= beztr.f2= beztr.f3= SELECT;
2103         beztr.h1= beztr.h2= HD_AUTO;
2104         
2105         /* add temp beztriple to keyframes */
2106         a= insert_bezt_icu(icu, &beztr);
2107         if (!fast) calchandles_ipocurve(icu);
2108         
2109         /* set handletype */
2110         if (icu->totvert > 2) {
2111                 BezTriple *bezt;
2112                 
2113                 h1= h2= HD_AUTO;
2114                 bezt= (icu->bezt + a);
2115                 
2116                 if (a > 0) h1= (bezt-1)->h2;
2117                 if (a < icu->totvert-1) h2= (bezt+1)->h1;
2118                 
2119                 bezt->h1= h1;
2120                 bezt->h2= h2;
2121                 
2122                 if (!fast) calchandles_ipocurve(icu);
2123         }
2124 }
2125
2126 void add_vert_ipo(void)
2127 {
2128         EditIpo *ei;
2129         float x, y;
2130         int val;
2131         short mval[2];
2132
2133         if(G.sipo->ipo && G.sipo->ipo->id.lib) return;
2134         if(G.sipo->showkey) {
2135                 G.sipo->showkey= 0;
2136                 free_ipokey(&G.sipo->ipokey);
2137         }
2138         
2139         getmouseco_areawin(mval);
2140         
2141         if(mval[0]>G.v2d->mask.xmax) return;
2142         
2143         ei= get_active_editipo();
2144         if(ei==NULL) {
2145                 error("No active Ipo curve");
2146                 return;
2147         }
2148         ei->flag |= IPO_VISIBLE;        /* can happen it is active but not visible */
2149         
2150         areamouseco_to_ipoco(G.v2d, mval, &x, &y);
2151         
2152         /* convert click-time to ipo-time */
2153         if (NLA_IPO_SCALED) {
2154                 x= get_action_frame(OBACT, x);
2155         }
2156         
2157         if(ei->icu==NULL) {
2158                 if(G.sipo->from) {
2159                         ei->icu= verify_ipocurve(G.sipo->from, G.sipo->blocktype, G.sipo->actname, G.sipo->constname, G.sipo->bonename, ei->adrcode);
2160                         if (ei->icu)
2161                                 ei->flag |= ei->icu->flag & IPO_AUTO_HORIZ;     /* new curve could have been added, weak... */
2162                         else
2163                                 error("Cannot create an IPO curve, you may be using libdata");
2164                 }
2165         }
2166         if(ei->icu==NULL) return;
2167
2168         if(ei->disptype==IPO_DISPBITS) {
2169                 ei->icu->vartype= IPO_BITS;
2170                 val= (int)floor(y-0.5);
2171                 if(val<0) val= 0;
2172                 y= (float)(1 << val);
2173         }
2174         
2175         insert_vert_icu(ei->icu, x, y, 0);
2176
2177         /* to be sure: if icu was 0, or only 1 curve visible */
2178         ei->flag |= IPO_SELECT;
2179         ei->icu->flag= ei->flag;
2180         
2181         editipo_changed(G.sipo, 1);
2182         BIF_undo_push("Add Ipo vertex");
2183 }
2184
2185 static void *get_context_ipo_poin(ID *id, int blocktype, char *actname, IpoCurve *icu, int *vartype)
2186 {
2187         if(blocktype==ID_PO) {
2188                 if(GS(id->name)==ID_OB) {
2189                         Object *ob= (Object *)id;
2190                         bPoseChannel *pchan= get_pose_channel(ob->pose, actname);
2191                         
2192                         *vartype= IPO_FLOAT;
2193                         return get_pchan_ipo_poin(pchan, icu->adrcode);
2194                 }
2195                 return NULL;
2196         }
2197         else
2198                 return get_ipo_poin(id, icu, vartype);
2199
2200 }
2201
2202 #define KEYNEEDED_DONTADD       0
2203 #define KEYNEEDED_JUSTADD       1
2204 #define KEYNEEDED_DELPREV       2
2205 #define KEYNEEDED_DELNEXT       3
2206
2207 static int new_key_needed(IpoCurve *icu, float cFrame, float nValue) 
2208 {
2209         /* This function determines whether a new keyframe is needed */
2210         /* Cases where keyframes should not be added:
2211          *      1. Keyframe to be added bewteen two keyframes with similar values
2212          *      2. Keyframe to be added on frame where two keyframes are already situated
2213          *      3. Keyframe lies at point that intersects the linear line between two keyframes
2214          */
2215         
2216         BezTriple *bezt=NULL, *prev=NULL;
2217         int totCount, i;
2218         float valA = 0.0f, valB = 0.0f;
2219         
2220         /* safety checking */
2221         if (!icu) return KEYNEEDED_JUSTADD;
2222         totCount= icu->totvert;
2223         if (totCount==0) return KEYNEEDED_JUSTADD;
2224         
2225         /* loop through checking if any are the same */
2226         bezt= icu->bezt;
2227         for (i=0; i<totCount; i++) {
2228                 float prevPosi=0.0f, prevVal=0.0f;
2229                 float beztPosi=0.0f, beztVal=0.0f;
2230                         
2231                 /* get current time+value */    
2232                 beztPosi= bezt->vec[1][0];
2233                 beztVal= bezt->vec[1][1];
2234                         
2235                 if (prev) {
2236                         /* there is a keyframe before the one currently being examined */               
2237                         
2238                         /* get previous time+value */
2239                         prevPosi= prev->vec[1][0];
2240                         prevVal= prev->vec[1][1];
2241                         
2242                         /* keyframe to be added at point where there are already two similar points? */
2243                         if (IS_EQ(prevPosi, cFrame) && IS_EQ(beztPosi, cFrame) && IS_EQ(beztPosi, prevPosi)) {
2244                                 return KEYNEEDED_DONTADD;
2245                         }
2246                         
2247                         /* keyframe between prev+current points ? */
2248                         if ((prevPosi <= cFrame) && (cFrame <= beztPosi)) {
2249                                 /* is the value of keyframe to be added the same as keyframes on either side ? */
2250                                 if (IS_EQ(prevVal, nValue) && IS_EQ(beztVal, nValue) && IS_EQ(prevVal, beztVal)) {
2251                                         return KEYNEEDED_DONTADD;
2252                                 }
2253                                 else {
2254                                         float realVal;
2255                                         
2256                                         /* get real value of curve at that point */
2257                                         realVal= eval_icu(icu, cFrame);
2258                                 
2259                                         /* compare whether it's the same as proposed */
2260                                         if (IS_EQ(realVal, nValue)) 
2261                                                 return KEYNEEDED_DONTADD;
2262                                         else 
2263                                                 return KEYNEEDED_JUSTADD;
2264                                 }
2265                         }
2266                         
2267                         /* new keyframe before prev beztriple? */
2268                         if (cFrame < prevPosi) {
2269                                 /* A new keyframe will be added. However, whether the previous beztriple
2270                                  * stays around or not depends on whether the values of previous/current
2271                                  * beztriples and new keyframe are the same.
2272                                  */
2273                                 if (IS_EQ(prevVal, nValue) && IS_EQ(beztVal, nValue) && IS_EQ(prevVal, beztVal))
2274                                         return KEYNEEDED_DELNEXT;
2275                                 else 
2276                                         return KEYNEEDED_JUSTADD;
2277                         }
2278                 }
2279                 else {
2280                         /* just add a keyframe if there's only one keyframe 
2281                          * and the new one occurs before the exisiting one does.
2282                          */
2283                         if ((cFrame < beztPosi) && (totCount==1))
2284                                 return KEYNEEDED_JUSTADD;
2285                 }
2286                 
2287                 /* continue. frame to do not yet passed (or other conditions not met) */
2288                 if (i < (totCount-1)) {
2289                         prev= bezt;
2290                         bezt++;
2291                 }
2292                 else
2293                         break;
2294         }
2295         
2296         /* Frame in which to add a new-keyframe occurs after all other keys
2297          * -> If there are at least two existing keyframes, then if the values of the
2298          *       last two keyframes and the new-keyframe match, the last existing keyframe
2299          *       gets deleted as it is no longer required.
2300          * -> Otherwise, a keyframe is just added. 1.0 is added so that fake-2nd-to-last
2301          *       keyframe is not equal to last keyframe.
2302          */
2303         bezt= (icu->bezt + (icu->totvert - 1));
2304         valA= bezt->vec[1][1];
2305         
2306         if (prev)
2307                 valB= prev->vec[1][1];
2308         else 
2309                 valB= bezt->vec[1][1] + 1.0f; 
2310                 
2311         if (IS_EQ(valA, nValue) && IS_EQ(valA, valB)) 
2312                 return KEYNEEDED_DELPREV;
2313         else 
2314                 return KEYNEEDED_JUSTADD;
2315 }
2316
2317 /* a duplicate of insertkey that does not check for routing to insertmatrixkey 
2318         to avoid recursion problems */
2319 static void insertkey_nonrecurs(ID *id, int blocktype, char *actname, char *constname, int adrcode)
2320 {
2321         IpoCurve *icu;
2322         Object *ob;
2323         void *poin= NULL;
2324         float curval, cfra;
2325         int vartype;
2326         int matset=0;
2327         
2328         if (matset==0) {
2329                 icu= verify_ipocurve(id, blocktype, actname, constname, NULL, adrcode);
2330                 
2331                 if(icu) {
2332                         
2333                         poin= get_context_ipo_poin(id, blocktype, actname, icu, &vartype);
2334                         
2335                         if(poin) {
2336                                 curval= read_ipo_poin(poin, vartype);
2337                                 
2338                                 cfra= frame_to_float(CFRA);
2339                                 
2340                                 /* if action is mapped in NLA, it returns a correction */
2341                                 if(actname && actname[0] && GS(id->name)==ID_OB)
2342                                         cfra= get_action_frame((Object *)id, cfra);
2343                                 
2344                                 if( GS(id->name)==ID_OB ) {
2345                                         ob= (Object *)id;
2346                                         if((ob->ipoflag & OB_OFFS_OB) && (give_timeoffset(ob)!=0.0) ) {
2347                                                 /* actually frametofloat calc again! */
2348                                                 cfra-= give_timeoffset(ob)*G.scene->r.framelen;
2349                                         }
2350                                 }
2351                                 
2352                                 insert_vert_icu(icu, cfra, curval, 0);
2353                         }
2354                 }
2355         }
2356 }
2357
2358 int insertmatrixkey(ID *id, int blocktype, char *actname, char *constname, int adrcode)
2359 {
2360         int matindex=0;
2361         /* branch on adrcode and blocktype, generating the proper matrix-based
2362         values to send to insertfloatkey */
2363         if (GS(id->name)==ID_OB) {
2364                 Object *ob= (Object *)id;
2365
2366                 if ( blocktype==ID_OB ){ //working with an object
2367                         if ((ob)&&!(ob->parent)) {
2368                                 if ((adrcode==OB_ROT_X)||(adrcode==OB_ROT_Y)||(adrcode==OB_ROT_Z)) { //get a rotation
2369                                         float eul[3];
2370                                         switch (adrcode) {
2371                                                 case OB_ROT_X:
2372                                                         matindex=0;
2373                                                         break;
2374                                                 case OB_ROT_Y:
2375                                                         matindex=1;
2376                                                         break;
2377                                                 case OB_ROT_Z:
2378                                                         matindex=2;
2379                                                         break;
2380                                         }
2381                                         Mat4ToEul(ob->obmat, eul);
2382                                         insertfloatkey(id, ID_OB, actname, NULL, adrcode, eul[matindex]*(5.72958));
2383                                         return 1;
2384                                 } else if ((adrcode==OB_LOC_X)||(adrcode==OB_LOC_Y)||(adrcode==OB_LOC_Z)) {//get a translation
2385                                         switch (adrcode) {
2386                                                 case OB_LOC_X:
2387                                                         matindex=0;
2388                                                         break;
2389                                                 case OB_LOC_Y:
2390                                                         matindex=1;
2391                                                         break;
2392                                                 case OB_LOC_Z:
2393                                                         matindex=2;
2394                                                         break;
2395                                         }
2396                                         insertfloatkey(id, ID_OB, actname, NULL, adrcode, ob->obmat[3][matindex]);
2397                                         return 1;
2398                                 }
2399                         }
2400                 } else if ( blocktype==ID_PO) { //working with a pose channel
2401                         bPoseChannel *pchan= get_pose_channel(ob->pose, actname);
2402                         if (pchan) {
2403                                 if ((adrcode==AC_LOC_X)||(adrcode==AC_LOC_Y)||(adrcode==AC_LOC_Z)) {
2404                                         switch (adrcode) {
2405                                                 case AC_LOC_X:
2406                                                         matindex=0;
2407                                                         break;
2408                                                 case AC_LOC_Y:
2409                                                         matindex=1;
2410                                                         break;
2411                                                 case AC_LOC_Z:
2412                                                         matindex=2;
2413                                                         break;
2414                                         }
2415                                         if (!(pchan->bone->parent)||((pchan->bone->parent)&&!(pchan->bone->flag&BONE_CONNECTED))) { /* don't use for non-connected child bones */
2416                                                 float delta_mat[4][4]; 
2417                                                 armature_mat_pose_to_delta(delta_mat, pchan->pose_mat, pchan->bone->arm_mat);
2418                                                 insertfloatkey(id, ID_PO, pchan->name, NULL, adrcode, delta_mat[3][matindex]);
2419                                                 return 1;
2420                                         }
2421                                 } else if ((adrcode==AC_QUAT_W)||(adrcode==AC_QUAT_X)||(adrcode==AC_QUAT_Y)||(adrcode==AC_QUAT_Z)) { 
2422                                         float tmat[4][4], trimat[3][3], localQuat[4];
2423                                         
2424                                         switch (adrcode) {
2425                                                 case AC_QUAT_W:
2426                                                         matindex=0;
2427                                                         break;
2428                                                 case AC_QUAT_X:
2429                                                         matindex=1;
2430                                                         break;
2431                                                 case AC_QUAT_Y:
2432                                                         matindex=2;
2433                                                         break;
2434                                                 case AC_QUAT_Z:
2435                                                         matindex=3;
2436                                                         break;
2437                                         }
2438                                         
2439                                         /* it should be reasonable to assume that we are keyframing on the active object, although it is not 
2440                                          * strictly required for this particular space conversion, arg1 must not be null for this to work
2441                                          */
2442                                         Mat4CpyMat4(tmat, pchan->pose_mat);
2443                                         constraint_mat_convertspace(OBACT, pchan, tmat, CONSTRAINT_SPACE_POSE, CONSTRAINT_SPACE_LOCAL);
2444                                         
2445                                         Mat3CpyMat4(trimat, tmat);
2446                                         Mat3ToQuat_is_ok(trimat, localQuat);
2447                                         insertfloatkey(id, ID_PO, pchan->name, NULL, adrcode, localQuat[matindex]);
2448                                         
2449                                         return 1;
2450                                 }
2451                         }
2452                 }
2453         }
2454         /* failed to set a matrix key -- use traditional, but the non-recursing version */
2455         insertkey_nonrecurs(id,blocktype,actname,constname,adrcode);
2456         return 0;
2457 }
2458
2459 static int match_adr_constraint(ID * id, int blocktype, char *actname, int adrcode)
2460 {       /* This function matches constraint blocks with adrcodes to see if the
2461                 visual keying method should be used. For example, an object looking to key
2462                 location and having a CopyLoc constraint would return true. */
2463                 
2464         Object *ob=NULL;
2465         int foundmatch=0;
2466         int searchtype=0;
2467         bConstraint *conref=NULL, *con=NULL;
2468         
2469         /*Retrieve constraint list*/
2470         if( GS(id->name)==ID_OB ) 
2471                 ob= (Object *)id;
2472         if (ob) {
2473                 if (blocktype==ID_PO) {
2474                         bPoseChannel *pchan= get_pose_channel(ob->pose, actname);
2475                         conref=pchan->constraints.first;
2476                 } else if (blocktype==ID_OB) {
2477                         conref=ob->constraints.first;
2478                 }
2479                 
2480                 if (conref) {
2481                         /*Set search type: 1 is for translation contraints, 2 is for rotation*/
2482                         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)) {
2483                                 searchtype=1;
2484                         } 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)) {
2485                                 searchtype=2;
2486                         }
2487                         
2488                         if (searchtype>0) {
2489                                 for (con=conref; (con)&&(foundmatch==0); con=con->next) {
2490                                         switch (con->type) {
2491                                         /* match constraint types to which kinds of keying they would affect */
2492                                                 case CONSTRAINT_TYPE_CHILDOF:
2493                                                         foundmatch=1;
2494                                                         break;
2495                                                 case CONSTRAINT_TYPE_TRACKTO:
2496                                                         if (searchtype==2) foundmatch=1;
2497                                                         break;
2498                                                 case CONSTRAINT_TYPE_FOLLOWPATH:
2499                                                         foundmatch=1;
2500                                                         break;
2501                                                 case CONSTRAINT_TYPE_ROTLIMIT:
2502                                                         if (searchtype==2) foundmatch=1;
2503                                                         break;
2504                                                 case CONSTRAINT_TYPE_LOCLIMIT:
2505                                                         if (searchtype==1) foundmatch=1;
2506                                                         break;
2507                                                 case CONSTRAINT_TYPE_ROTLIKE:
2508                                                         if (searchtype==2) foundmatch=1;
2509                                                         break;
2510                                                 case CONSTRAINT_TYPE_LOCLIKE:
2511                                                         if (searchtype==1) foundmatch=1;
2512                                                         break;
2513                                                 case CONSTRAINT_TYPE_LOCKTRACK:
2514                                                         if (searchtype==2) foundmatch=1;
2515                                                         break;
2516                                                 case CONSTRAINT_TYPE_DISTANCELIMIT:
2517                                                         if (searchtype==1) foundmatch=1;
2518                                                         break;
2519                                                 case CONSTRAINT_TYPE_MINMAX:
2520                                                         if (searchtype==1) foundmatch=1;
2521                                                         break;
2522                                                 case CONSTRAINT_TYPE_TRANSFORM:
2523                                                         foundmatch=1;
2524                                                         break;
2525                                                 default:
2526                                                         break;
2527                                         }
2528                                 }
2529                         }
2530                 }
2531         }
2532         
2533         return foundmatch;
2534                         
2535 }
2536
2537 void insertkey(ID *id, int blocktype, char *actname, char *constname, int adrcode, short fast)
2538 {
2539         IpoCurve *icu;
2540         Object *ob;
2541         void *poin= NULL;
2542         float curval, cfra;
2543         int vartype;
2544         int matset=0;
2545         
2546         if ((IS_AUTOKEY_FLAG(AUTOMATKEY))&&(match_adr_constraint(id, blocktype, actname, adrcode))) {
2547                 matset=insertmatrixkey(id, blocktype, actname, constname, adrcode);
2548         } 
2549         if (matset==0) {
2550                 icu= verify_ipocurve(id, blocktype, actname, constname, NULL, adrcode);
2551                 
2552                 if(icu) {
2553                         
2554                         poin= get_context_ipo_poin(id, blocktype, actname, icu, &vartype);
2555                         
2556                         if(poin) {
2557                                 curval= read_ipo_poin(poin, vartype);
2558                                 
2559                                 cfra= frame_to_float(CFRA);
2560                                 
2561                                 /* if action is mapped in NLA, it returns a correction */
2562                                 if(actname && actname[0] && GS(id->name)==ID_OB)
2563                                         cfra= get_action_frame((Object *)id, cfra);
2564                                 
2565                                 if( GS(id->name)==ID_OB ) {
2566                                         ob= (Object *)id;
2567                                         if((ob->ipoflag & OB_OFFS_OB) && (give_timeoffset(ob)!=0.0) ) {
2568                                                 /* actually frametofloat calc again! */
2569                                                 cfra-= give_timeoffset(ob)*G.scene->r.framelen;
2570                                         }
2571                                 }
2572                                 
2573                                 insert_vert_icu(icu, cfra, curval, fast);
2574                         }
2575                 }
2576         }
2577 }
2578
2579
2580
2581 /* This function is a 'smarter' version of the insert key code.
2582  * It uses an auxilliary function to check whether a keyframe is really needed */
2583 void insertkey_smarter(ID *id, int blocktype, char *actname, char *constname, int adrcode)
2584 {
2585         IpoCurve *icu;
2586         Object *ob;
2587         void *poin= NULL;
2588         float curval, cfra;
2589         int vartype;
2590         int insert_mode;
2591         
2592         icu= verify_ipocurve(id, blocktype, actname, constname, NULL, adrcode);
2593         
2594         if(icu) {
2595                 
2596                 poin= get_context_ipo_poin(id, blocktype, actname, icu, &vartype);
2597                 
2598                 if(poin) {
2599                         curval= read_ipo_poin(poin, vartype);
2600                         
2601                         cfra= frame_to_float(CFRA);
2602                         
2603                         /* if action is mapped in NLA, it returns a correction */
2604                         if(actname && actname[0] && GS(id->name)==ID_OB)
2605                                 cfra= get_action_frame((Object *)id, cfra);
2606                         
2607                         if( GS(id->name)==ID_OB ) {
2608                                 ob= (Object *)id;
2609                                 if((ob->ipoflag & OB_OFFS_OB) && (give_timeoffset(ob)!=0.0) ) {
2610                                         /* actually frametofloat calc again! */
2611                                         cfra-= give_timeoffset(ob)*G.scene->r.framelen;
2612                                 }
2613                         }
2614                         
2615                         /* check whether this curve really needs a new keyframe */
2616                         insert_mode= new_key_needed(icu, cfra, curval);
2617                         
2618                         /* insert new keyframe at current frame */
2619                         if (insert_mode) 
2620                                 insert_vert_icu(icu, cfra, curval, 0);
2621                         
2622                         /* delete keyframe immediately before/after newly added */
2623                         switch (insert_mode) {
2624                                 case KEYNEEDED_DELPREV:
2625                                         delete_icu_key(icu, icu->totvert-2);
2626                                         break;
2627                                 case KEYNEEDED_DELNEXT:
2628                                         delete_icu_key(icu, 1);
2629                                         break;
2630                         }
2631                 }
2632         }
2633 }
2634
2635 /* For inserting keys based on an arbitrary float value */
2636 void insertfloatkey(ID *id, int blocktype, char *actname, char *constname, int adrcode, float floatkey)
2637 {
2638         IpoCurve *icu;
2639         Object *ob;
2640         void *poin= NULL;
2641         float cfra;
2642         int vartype;
2643         
2644         icu= verify_ipocurve(id, blocktype, actname, constname, NULL, adrcode);
2645         
2646         if(icu) {
2647                 
2648                 poin= get_context_ipo_poin(id, blocktype, actname, icu, &vartype);
2649                 
2650                 if(poin) {
2651                         
2652                         cfra= frame_to_float(CFRA);
2653                         
2654                         /* if action is mapped in NLA, it returns a correction */
2655                         if(actname && actname[0] && GS(id->name)==ID_OB)
2656                                 cfra= get_action_frame((Object *)id, cfra);
2657                         
2658                         if( GS(id->name)==ID_OB ) {
2659                                 ob= (Object *)id;
2660                                 if((ob->ipoflag & OB_OFFS_OB) && (give_timeoffset(ob)!=0.0) ) {
2661                                         /* actually frametofloat calc again! */
2662                                         cfra-= give_timeoffset(ob)*G.scene->r.framelen;
2663                                 }
2664                         }
2665                         
2666                         /* insert new keyframe at current frame */
2667                         insert_vert_icu(icu, cfra, floatkey, 0);
2668                 }
2669         }
2670 }
2671
2672 void insertkey_editipo(void)
2673 {
2674         EditIpo *ei;
2675         IpoKey *ik;
2676         ID *id;
2677         float *fp, cfra, *insertvals;
2678         int a, nr, ok, tot;
2679         short event;
2680         
2681         ei= get_active_editipo();
2682         if(ei && ei->icu && ei->icu->driver) 
2683                 event= pupmenu("Insert Curve %t|Default one-to-one mapping %x3");
2684         else if(G.sipo->showkey)
2685                 event= pupmenu("Insert Key Vertices %t|Current Frame %x1|Selected Keys %x2");
2686         else 
2687                 event= pupmenu("Insert Key Vertices %t|Current Frame %x1");
2688         
2689         if(event<1) return;
2690         
2691         if(event==3) {
2692                 IpoDriver *driver= ei->icu->driver;
2693                 
2694                 if(ei->icu->bezt) MEM_freeN(ei->icu->bezt);
2695                 ei->icu->totvert= 0;
2696                 ei->icu->bezt= NULL;
2697                 
2698                 insert_vert_icu(ei->icu, 0.0f, 0.0f, 0);
2699                 
2700                 if(ELEM3(driver->adrcode, OB_ROT_X, OB_ROT_Y, OB_ROT_Z)) {
2701                         if(ei->disptype==IPO_DISPDEGR)
2702                                 insert_vert_icu(ei->icu, 18.0f, 18.0f, 0);
2703                         else
2704                                 insert_vert_icu(ei->icu, 18.0f, 1.0f, 0);
2705                 }
2706                 else
2707                         insert_vert_icu(ei->icu, 1.0f, 1.0f, 0);
2708                 
2709                 ei->flag |= IPO_SELECT|IPO_VISIBLE;
2710                 ei->icu->flag= ei->flag;
2711                 ei->icu->extrap= IPO_DIR;
2712
2713                 do_ipo_buttons(B_IPOHOME);
2714         }
2715         else {
2716                 ei= G.sipo->editipo;
2717                 for(nr=0; nr<G.sipo->totipo; nr++, ei++) {
2718                         if (ISPOIN(ei, flag & IPO_VISIBLE, icu)) {
2719                         
2720                                 ok= 0;
2721                                 if(G.sipo->showkey) ok= 1;
2722                                 else if(ei->flag & IPO_SELECT) ok= 1;
2723
2724                                 if(ok) {
2725                                         /* count amount */
2726                                         if(event==1) tot= 1;
2727                                         else {
2728                                                 ik= G.sipo->ipokey.first;
2729                                                 tot= 0;
2730                                                 while(ik) {
2731                                                         if(ik->flag & 1) tot++;
2732                                                         ik= ik->next;
2733                                                 }
2734                                         }
2735                                         if(tot) {
2736                                         
2737                                                 /* correction for ob timeoffs */
2738                                                 cfra= frame_to_float(CFRA);
2739                                                 id= G.sipo->from;       
2740                                                 if(id && GS(id->name)==ID_OB ) {
2741                                                         Object *ob= (Object *)id;
2742                                                         if((ob->ipoflag & OB_OFFS_OB) && (give_timeoffset(ob)!=0.0) ) {
2743                                                                 cfra-= give_timeoffset(ob)*G.scene->r.framelen;
2744                                                         }
2745                                                 }
2746                                                 else if(id && GS(id->name)==ID_SEQ) {
2747                                                         Sequence *last_seq = get_last_seq();    /* editsequence.c */
2748                                                         
2749                                                         if(last_seq && (last_seq->flag & SEQ_IPO_FRAME_LOCKED) == 0) {
2750                                                                 cfra= (float)(100.0*(cfra-last_seq->startdisp)/((float)(last_seq->enddisp-last_seq->startdisp)));
2751                                                         }
2752                                                 }
2753                                                 
2754                                                 /* convert cfra to ipo-time */
2755                                                 if (NLA_IPO_SCALED) {
2756                                                         cfra= get_action_frame(OBACT, cfra);
2757                                                 }
2758                                 
2759                                                 insertvals= MEM_mallocN(sizeof(float)*2*tot, "insertkey_editipo");
2760                                                 /* make sure icu->curval is correct */
2761                                                 calc_ipo(G.sipo->ipo, cfra);
2762                                                 
2763                                                 if(event==1) {
2764                                                         insertvals[0]= cfra;
2765                                                         
2766                                                         insertvals[1]= ei->icu->curval;
2767                                                 }
2768                                                 else {
2769                                                         fp= insertvals;
2770                                                         ik= G.sipo->ipokey.first;
2771                                                         while(ik) {
2772                                                                 if(ik->flag & 1) {
2773                                                                         calc_ipo(G.sipo->ipo, ik->val);
2774
2775                                                                         fp[0]= ik->val;
2776                                                                         fp[1]= ei->icu->curval;
2777                                                                         fp+= 2;
2778                                                                 }
2779                                                                 ik= ik->next;
2780                                                         }
2781                                                 }
2782                                                 fp= insertvals;
2783                                                 for(a=0; a<tot; a++, fp+=2) {
2784                                                         insert_vert_icu(ei->icu, fp[0], fp[1], 0);
2785                                                 }
2786                                                 
2787                                                 MEM_freeN(insertvals);
2788                                                 calc_ipo(G.sipo->ipo, (float)CFRA);
2789                                         }
2790                                 }
2791                         }
2792                 }
2793         }
2794         BIF_undo_push("Insert Key Ipo");
2795         allqueue (REDRAWACTION, 0);
2796         allqueue(REDRAWNLA, 0);
2797         allqueue(REDRAWIPO, 0);
2798         allspace(REMAKEIPO, 0);
2799 }
2800
2801
2802 void common_insertkey(void)
2803 {
2804         Base *base;
2805         Object *ob;
2806         Material *ma;
2807         ID *id;
2808         IpoCurve *icu;
2809         World *wo;
2810         Lamp *la;
2811         Tex *te;
2812         int tlay, map, event;
2813         char menustr[256];
2814
2815         if(curarea->spacetype==SPACE_IPO) {
2816                 insertkey_editipo();
2817         }
2818         else if(curarea->spacetype==SPACE_ACTION) {
2819                 insertkey_action();
2820         }
2821         else if(curarea->spacetype==SPACE_BUTS) {
2822                 if(G.buts->mainb==CONTEXT_SHADING) {
2823                         int tab= G.buts->tab[CONTEXT_SHADING];
2824
2825                         if(tab==TAB_SHADING_MAT) {
2826                                 ma = G.buts->lockpoin;
2827                                 ma = editnode_get_active_material(ma);
2828                                 id = (ID *)ma;
2829                                 
2830                                 if(id) {
2831                                         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");
2832                                         if(event== -1) return;
2833
2834                                         map= texchannel_to_adrcode(ma->texact);
2835
2836                                         if(event==0 || event==10) {
2837                                                 insertkey(id, ID_MA, NULL, NULL, MA_COL_R, 0);
2838                                                 insertkey(id, ID_MA, NULL, NULL, MA_COL_G, 0);
2839                                                 insertkey(id, ID_MA, NULL, NULL, MA_COL_B, 0);
2840                                         }
2841                                         if(event==1 || event==10) {
2842                                                 insertkey(id, ID_MA, NULL, NULL, MA_ALPHA, 0);
2843                                         }
2844                                         if(event==2 || event==10) {
2845                                                 insertkey(id, ID_MA, NULL, NULL, MA_HASIZE, 0);
2846                                         }
2847                                         if(event==3 || event==10) {
2848                                                 insertkey(id, ID_MA, NULL, NULL, MA_MODE, 0);
2849                                         }
2850                                         if(event==10) {
2851                                                 insertkey(id, ID_MA, NULL, NULL, MA_SPEC_R, 0);
2852                                                 insertkey(id, ID_MA, NULL, NULL, MA_SPEC_G, 0);
2853                                                 insertkey(id, ID_MA, NULL, NULL, MA_SPEC_B, 0);
2854                                                 insertkey(id, ID_MA, NULL, NULL, MA_REF, 0);
2855                                                 insertkey(id, ID_MA, NULL, NULL, MA_EMIT, 0);
2856                                                 insertkey(id, ID_MA, NULL, NULL, MA_AMB, 0);
2857                                                 insertkey(id, ID_MA, NULL, NULL, MA_SPEC, 0);
2858                                                 insertkey(id, ID_MA, NULL, NULL, MA_HARD, 0);
2859                                                 insertkey(id, ID_MA, NULL, NULL, MA_MODE, 0);
2860                                                 insertkey(id, ID_MA, NULL, NULL, MA_TRANSLU, 0);
2861                                                 insertkey(id, ID_MA, NULL, NULL, MA_ADD, 0);
2862                                         }
2863                                         if(event==14) {
2864                                                 insertkey(id, ID_MA, NULL, NULL, MA_RAYM, 0);
2865                                                 insertkey(id, ID_MA, NULL, NULL, MA_FRESMIR, 0);
2866                                                 insertkey(id, ID_MA, NULL, NULL, MA_FRESMIRI, 0);
2867                                                 insertkey(id, ID_MA, NULL, NULL, MA_FRESTRA, 0);
2868                                                 insertkey(id, ID_MA, NULL, NULL, MA_FRESTRAI, 0);
2869                                         }
2870                                         if(event==12 || event==11) {
2871                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_OFS_X, 0);
2872                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_OFS_Y, 0);
2873                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_OFS_Z, 0);
2874                                         }
2875                                         if(event==13 || event==11) {
2876                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_SIZE_X, 0);
2877                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_SIZE_Y, 0);
2878                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_SIZE_Z, 0);
2879                                         }
2880                                         if(event==11) {
2881                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_R, 0);
2882                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_G, 0);
2883                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_B, 0);
2884                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_DVAR, 0);
2885                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_COLF, 0);
2886                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_NORF, 0);
2887                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_VARF, 0);
2888                                                 insertkey(id, ID_MA, NULL, NULL, map+MAP_DISP, 0);
2889                                         }
2890                                 }
2891                         }
2892                         else if(tab==TAB_SHADING_WORLD) {
2893                                 id= G.buts->lockpoin;
2894                                 wo= G.buts->lockpoin;
2895                                 if(id) {
2896                                         event= pupmenu("Insert Key %t|Zenith RGB%x0|Horizon RGB%x1|Mist%x2|Stars %x3|Offset%x12|Size%x13");
2897                                         if(event== -1) return;
2898
2899                                         map= texchannel_to_adrcode(wo->texact);
2900
2901                                         if(event==0) {
2902                                                 insertkey(id, ID_WO, NULL, NULL, WO_ZEN_R, 0);
2903                                                 insertkey(id, ID_WO, NULL, NULL, WO_ZEN_G, 0);
2904                                                 insertkey(id, ID_WO, NULL, NULL, WO_ZEN_B, 0);
2905                                         }
2906                                         if(event==1) {
2907                                                 insertkey(id, ID_WO, NULL, NULL, WO_HOR_R, 0);
2908                                                 insertkey(id, ID_WO, NULL, NULL, WO_HOR_G, 0);
2909                                                 insertkey(id, ID_WO, NULL, NULL, WO_HOR_B, 0);
2910                                         }
2911                                         if(event==2) {
2912                                                 insertkey(id, ID_WO, NULL, NULL, WO_MISI, 0);
2913                                                 insertkey(id, ID_WO, NULL, NULL, WO_MISTDI, 0);
2914                                                 insertkey(id, ID_WO, NULL, NULL, WO_MISTSTA, 0);
2915                                                 insertkey(id, ID_WO, NULL, NULL, WO_MISTHI, 0);
2916                                         }
2917                                         if(event==3) {
2918                                                 insertkey(id, ID_WO, NULL, NULL, WO_STAR_R, 0);
2919                                                 insertkey(id, ID_WO, NULL, NULL, WO_STAR_G, 0);
2920                                                 insertkey(id, ID_WO, NULL, NULL, WO_STAR_B, 0);
2921                                                 insertkey(id, ID_WO, NULL, NULL, WO_STARDIST, 0);
2922                                                 insertkey(id, ID_WO, NULL, NULL, WO_STARSIZE, 0);
2923                                         }
2924                                         if(event==12) {
2925                                                 insertkey(id, ID_WO, NULL, NULL, map+MAP_OFS_X, 0);
2926                                                 insertkey(id, ID_WO, NULL, NULL, map+MAP_OFS_Y, 0);
2927                                                 insertkey(id, ID_WO, NULL, NULL, map+MAP_OFS_Z, 0);
2928                                         }
2929                                         if(event==13) {
2930                                                 insertkey(id, ID_WO, NULL, NULL, map+MAP_SIZE_X, 0);
2931                                                 insertkey(id, ID_WO, NULL, NULL, map+MAP_SIZE_Y, 0);
2932                                                 insertkey(id, ID_WO, NULL, NULL, map+MAP_SIZE_Z, 0);
2933                                         }
2934                                 }
2935                         }
2936                         else if(tab==TAB_SHADING_LAMP) {
2937                                 id= G.buts->lockpoin;
2938                                 la= G.buts->lockpoin;
2939                                 if(id) {
2940                                         event= pupmenu("Insert Key %t|RGB%x0|Energy%x1|Spot Size%x2|Offset%x12|Size%x13");
2941                                         if(event== -1) return;