svn merge -r 12716:12856 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / src / editnla.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL 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  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL *****
28  *
29  * This file is a horrible mess: An attmept to cram some
30  * final functionality into blender before it is too late.
31  *
32  * Hopefully it can be tidied up at a later date...
33 */
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include <math.h>
38
39 #include "PIL_time.h"
40
41 #include "MEM_guardedalloc.h"
42
43 #include "BLI_blenlib.h"
44
45 #include "DNA_action_types.h"
46 #include "DNA_constraint_types.h"
47 #include "DNA_curve_types.h"
48 #include "DNA_ipo_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_nla_types.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_space_types.h"
53 #include "DNA_scene_types.h"
54 #include "DNA_userdef_types.h"
55
56 #include "BKE_action.h"
57 #include "BKE_blender.h"
58 #include "BKE_depsgraph.h"
59 #include "BKE_group.h"
60 #include "BKE_global.h"
61 #include "BKE_ipo.h"
62 #include "BKE_library.h"
63 #include "BKE_main.h"
64 #include "BKE_nla.h"
65 #include "BKE_utildefines.h"
66
67 #include "BIF_screen.h"
68 #include "BIF_interface.h"
69 #include "BIF_butspace.h"
70 #include "BIF_space.h"
71 #include "BIF_mywindow.h"
72 #include "BIF_editview.h"
73 #include "BIF_toolbox.h"
74 #include "BIF_editnla.h"
75 #include "BIF_editaction.h"
76 #include "BIF_transform.h"
77
78 #include "BSE_editipo.h"
79 #include "BSE_editnla_types.h"
80 #include "BSE_headerbuttons.h"
81 #include "BSE_drawipo.h"
82 #include "BSE_editaction_types.h"
83 #include "BSE_trans_types.h"
84 #include "BSE_edit.h"
85 #include "BSE_filesel.h"
86 #include "BDR_editobject.h"
87 #include "BSE_drawnla.h"
88 #include "BSE_time.h"
89
90 #include "blendef.h"
91 #include "mydevice.h"
92
93 #ifdef HAVE_CONFIG_H
94 #include <config.h>
95 #endif
96
97 /* Note: A lot of these pretty much duplicate the behaviour of the
98 action windows.  The functions should be shared, not copy-pasted */
99
100 static void mouse_nla(int selectmode);
101 static Base *get_nearest_nlachannel_ob_key (float *index, short *sel);
102 static bAction *get_nearest_nlachannel_ac_key (float *index, short *sel);
103 static Base *get_nearest_nlastrip (bActionStrip **rstrip, short *sel);
104 static void mouse_nlachannels(short mval[2]);
105
106 /* ******************** SPACE: NLA ********************** */
107
108 void shift_nlastrips_up(void) {
109         
110         Base *base;
111         bActionStrip *strip, *prevstrip;
112
113         for (base=G.scene->base.first; base; base=base->next) {
114                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
115                         continue;
116                 
117                 for (strip = base->object->nlastrips.first; 
118                          strip; strip=strip->next){
119                         if (strip->flag & ACTSTRIP_SELECT) {
120                                 if ( (prevstrip = strip->prev) ) {
121                                         if (prevstrip->prev)
122                                                 prevstrip->prev->next = strip;
123                                         if (strip->next)
124                                                 strip->next->prev = prevstrip;
125                                         strip->prev = prevstrip->prev;
126                                         prevstrip->next = strip->next;
127                                         strip->next = prevstrip;
128                                         prevstrip->prev = strip;
129
130                                         if (prevstrip == base->object->nlastrips.first)
131                                                 base->object->nlastrips.first = strip;
132                                         if (strip == base->object->nlastrips.last)
133                                                 base->object->nlastrips.last = prevstrip;
134
135                                         strip = prevstrip;
136                                 }
137                                 else {
138                                         break;
139                                 }
140                         }
141                 }
142         }
143         BIF_undo_push("Shift NLA strip");
144         allqueue (REDRAWNLA, 0);
145
146 }
147
148 void shift_nlastrips_down(void) {
149         
150         Base *base;
151         bActionStrip *strip, *nextstrip;
152
153         for (base=G.scene->base.first; base; base=base->next) {
154                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
155                         continue;
156                 
157                 for (strip = base->object->nlastrips.last; 
158                          strip; strip=strip->prev){
159                         if (strip->flag & ACTSTRIP_SELECT) {
160                                 if ( (nextstrip = strip->next) ) {
161                                         if (nextstrip->next)
162                                                 nextstrip->next->prev = strip;
163                                         if (strip->prev)
164                                                 strip->prev->next = nextstrip;
165                                         strip->next = nextstrip->next;
166                                         nextstrip->prev = strip->prev;
167                                         strip->prev = nextstrip;
168                                         nextstrip->next = strip;
169
170                                         if (nextstrip == base->object->nlastrips.last)
171                                                 base->object->nlastrips.last = strip;
172                                         if (strip == base->object->nlastrips.first)
173                                                 base->object->nlastrips.first = nextstrip;
174
175                                         strip = nextstrip;
176                                 }
177                                 else {
178                                         break;
179                                 }
180                         }
181                 }
182         }
183         
184         BIF_undo_push("Shift NLA strips");
185         allqueue (REDRAWNLA, 0);
186 }
187
188 void synchronize_action_strips(void)
189 {
190         Base *base;
191         bActionStrip *strip;
192         
193         for (base=G.scene->base.first; base; base=base->next) {
194                 /* step 1: adjust strip-lengths */
195                 for (strip = base->object->nlastrips.last; strip; strip=strip->prev) {
196                         if (strip->flag & ACTSTRIP_LOCK_ACTION) {
197                                 float actstart, actend;
198                                 
199                                 calc_action_range(strip->act, &actstart, &actend, 1);
200                                 
201                                 if ((strip->actstart!=actstart) || (strip->actend!=actend)) {           
202                                         float offset = strip->scale * (actstart - strip->actstart);
203                                         float actlen = actend - actstart;
204                                         
205                                         strip->start += offset;
206                                         strip->end = (strip->scale * strip->repeat * actlen) + strip->start;
207                                         
208                                         strip->actstart= actstart;
209                                         strip->actend= actend;
210                                 }
211                         }
212                 }
213                 
214                 /* step 2: adjust blendin/out values for each strip if option is turned on */
215                 for (strip= base->object->nlastrips.first; strip; strip=strip->next) {
216                         if (strip->flag & ACTSTRIP_AUTO_BLENDS) {
217                                 bActionStrip *prev= strip->prev;
218                                 bActionStrip *next= strip->next;
219                                 float pr[2], nr[2];
220                                 
221                                 strip->blendin = 0.0f;
222                                 strip->blendout = 0.0f;
223                                 
224                                 /* setup test ranges */
225                                 if (prev && next) {
226                                         /* init range for previous strip */
227                                         pr[0]= prev->start;
228                                         pr[1]= prev->end;
229                                         
230                                         /* init range for next strip */
231                                         nr[0]= next->start;
232                                         nr[1]= next->end;
233                                 }
234                                 else if (prev) {
235                                         /* next strip's range is same as previous strip's range  */
236                                         pr[0] = nr[0] = prev->start;
237                                         pr[1] = nr[1] = prev->end;
238                                 }
239                                 else if (next) {
240                                         /* previous strip's range is same as next strip's range */
241                                         pr[0] = nr[0] = next->start;
242                                         pr[1] = nr[1] = next->end;
243                                 }
244                                 else {
245                                         /* there shouldn't be any more strips to loop through for this operation */
246                                         break;
247                                 }
248                                 
249                                 /* test various cases */
250                                 if ( IN_RANGE(pr[1], strip->start, strip->end) && 
251                                         (IN_RANGE(pr[0], strip->start, strip->end)==0) )
252                                 {
253                                         /* previous strip intersects start of current */
254                                         
255                                         if ( IN_RANGE(nr[1], strip->start, strip->end) && 
256                                                 (IN_RANGE(nr[0], strip->start, strip->end)==0) )
257                                         {
258                                                 /* next strip also intersects start of current */
259                                                 if (nr[1] < pr[1])
260                                                         strip->blendin= nr[1] - strip->start;
261                                                 else
262                                                         strip->blendin= pr[1] - strip->start;
263                                         }
264                                         else if (IN_RANGE(nr[0], strip->start, strip->end) && 
265                                                         (IN_RANGE(nr[1], strip->start, strip->end)==0))
266                                         {
267                                                 /* next strip intersects end of current */
268                                                 strip->blendout= strip->end - nr[0];
269                                                 strip->blendin= pr[1] - strip->start;
270                                         }
271                                         else {
272                                                 /* only previous strip intersects current */
273                                                 strip->blendin= pr[1] - strip->start;
274                                         }
275                                 }
276                                 else if (IN_RANGE(pr[0], strip->start, strip->end) && 
277                                                 (IN_RANGE(pr[1], strip->start, strip->end)==0) )
278                                 {
279                                         /* previous strip intersects end of current */
280                                         
281                                         if ( IN_RANGE(nr[0], strip->start, strip->end) && 
282                                                 (IN_RANGE(nr[1], strip->start, strip->end)==0) )
283                                         {
284                                                 /* next strip also intersects end of current */
285                                                 if (nr[1] > pr[1])
286                                                         strip->blendout= strip->end - nr[0];
287                                                 else
288                                                         strip->blendout= strip->end - pr[0];
289                                         }
290                                         else if (IN_RANGE(nr[1], strip->start, strip->end) && 
291                                                         (IN_RANGE(nr[0], strip->start, strip->end)==0))
292                                         {
293                                                 /* next strip intersects start of current */
294                                                 strip->blendin= nr[1] - strip->start;
295                                                 strip->blendout= strip->end - pr[0];
296                                         }
297                                         else {
298                                                 /* only previous strip intersects current */
299                                                 strip->blendout= strip->end - pr[0];
300                                         }
301                                 }
302                                 else if (IN_RANGE(nr[1], strip->start, strip->end) && 
303                                                 (IN_RANGE(nr[0], strip->start, strip->end)==0) )
304                                 {
305                                         /* next strip intersects start of current */
306                                         
307                                         if ( IN_RANGE(pr[1], strip->start, strip->end) && 
308                                                 (IN_RANGE(pr[0], strip->start, strip->end)==0) )
309                                         {
310                                                 /* previous strip also intersects start of current */
311                                                 if (pr[1] < nr[1])
312                                                         strip->blendin= pr[1] - strip->start;
313                                                 else
314                                                         strip->blendin= nr[1] - strip->start;
315                                         }
316                                         else if (IN_RANGE(pr[0], strip->start, strip->end) && 
317                                                         (IN_RANGE(pr[1], strip->start, strip->end)==0))
318                                         {
319                                                 /* previous strip intersects end of current */
320                                                 strip->blendout= strip->end - pr[0];
321                                                 strip->blendin= nr[1] - strip->start;
322                                         }
323                                         else {
324                                                 /* only next strip intersects current */
325                                                 strip->blendin= nr[1] - strip->start;
326                                         }
327                                 }
328                                 else if (IN_RANGE(nr[0], strip->start, strip->end) && 
329                                                 (IN_RANGE(nr[1], strip->start, strip->end)==0) )
330                                 {
331                                         /* next strip intersects end of current */
332                                         
333                                         if ( IN_RANGE(pr[0], strip->start, strip->end) && 
334                                                 (IN_RANGE(pr[1], strip->start, strip->end)==0) )
335                                         {
336                                                 /* previous strip also intersects end of current */
337                                                 if (pr[1] > nr[1])
338                                                         strip->blendout= strip->end - pr[0];
339                                                 else
340                                                         strip->blendout= strip->end - nr[0];
341                                         }
342                                         else if (IN_RANGE(pr[1], strip->start, strip->end) && 
343                                                         (IN_RANGE(pr[0], strip->start, strip->end)==0))
344                                         {
345                                                 /* previous strip intersects start of current */
346                                                 strip->blendin= pr[1] - strip->start;
347                                                 strip->blendout= strip->end - nr[0];
348                                         }
349                                         else {
350                                                 /* only next strip intersects current */
351                                                 strip->blendout= strip->end - nr[0];
352                                         }
353                                 }
354                                 
355                                 /* make sure blending stays in ranges */
356                                 CLAMP(strip->blendin, 0, (strip->end-strip->start));
357                                 CLAMP(strip->blendout, 0, (strip->end-strip->start));
358                         }
359                 }
360         }
361         
362 }
363
364 void reset_action_strips(int val)
365 {
366         Base *base;
367         bActionStrip *strip;
368         
369         for (base=G.scene->base.first; base; base=base->next) {
370                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
371                         continue;
372                 
373                 for (strip = base->object->nlastrips.last; strip; strip=strip->prev) {
374                         if (strip->flag & ACTSTRIP_SELECT) {
375                                 switch (val) {
376                                         case 1:
377                                         {
378                                                 /* clear scaling - reset to 1.0 without touching keys */
379                                                 float actlen= (strip->actend - strip->actstart);
380                                                 
381                                                 strip->scale= 1.0f;
382                                                 strip->end= (strip->repeat * actlen) + strip->start;
383                                         }
384                                                 break;
385                                         case 2:
386                                         {
387                                                 /* reset action-range */
388                                                 calc_action_range(strip->act, &strip->actstart, &strip->actend, 1);
389                                         }
390                                                 break;
391                                         case 3:
392                                         {
393                                                 /* apply scale to keys - scale is reset to 1.0f, but keys stay at the same times */
394                                                 bActionChannel *achan;
395                                                 
396                                                 if (strip->act) {
397                                                         for (achan= strip->act->chanbase.first; achan; achan= achan->next) {
398                                                                 actstrip_map_ipo_keys(base->object, achan->ipo, 0, 0);
399                                                         }
400                                                         
401                                                         /* now we can reset scale */
402                                                         calc_action_range(strip->act, &strip->actstart, &strip->actend, 1);
403                                                         strip->scale= 1.0f;
404                                                         strip->end = (strip->repeat * (strip->actend - strip->actstart)) + strip->start;
405                                                 }
406                                         }
407                                                 break;
408                                 }
409                                 base->object->ctime= -1234567.0f;       // evil! 
410                                 DAG_object_flush_update(G.scene, base->object, OB_RECALC_OB|OB_RECALC_DATA);
411                         }
412                 }
413         }
414         BIF_undo_push("Reset NLA strips");
415         allqueue (REDRAWVIEW3D, 0);
416         allqueue (REDRAWACTION, 0);
417         allqueue (REDRAWNLA, 0);
418 }
419
420 void snap_action_strips(int snap_mode)
421 {
422         Base *base;
423         bActionStrip *strip;
424         
425         for (base=G.scene->base.first; base; base=base->next) {
426                 /* object has ipo - these keyframes should be able to be snapped, even if strips are collapsed */
427                 if (base->object->ipo) {
428                         snap_ipo_keys(base->object->ipo, snap_mode);
429                 }
430                 
431                 /* object is collapsed - action and nla strips not shown/editable */
432                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
433                         continue;
434                 
435                 /* snap action strips */
436                 for (strip = base->object->nlastrips.last; strip; strip=strip->prev) {
437                         if (strip->flag & ACTSTRIP_SELECT) {
438                                 if (snap_mode==1) {
439                                         /* nearest frame */
440                                         strip->start= floor(strip->start+0.5);
441                                         strip->end= floor(strip->end+0.5);
442                                 }
443                                 else if (snap_mode==2) {
444                                         /* current frame */
445                                         float diff;
446                                         if (CFRA < strip->start) {
447                                                 diff = (strip->start - CFRA);
448                                                 strip->start -= diff;
449                                                 strip->end -= diff;
450                                         }
451                                         else {
452                                                 diff = (CFRA - strip->start);
453                                                 strip->start += diff;
454                                                 strip->end += diff;
455                                         }
456                                 }
457                                 else if (snap_mode==3) {
458                                         /* nearest second */
459                                         float secf = FPS;
460                                         strip->start= (float)(floor(strip->start/secf + 0.5f) * secf);
461                                         strip->end= (float)(floor(strip->end/secf + 0.5f) * secf);
462                                 }
463                         }
464                 }
465                 
466                 /* object has action */
467                 if (base->object->action) {
468                         ListBase act_data = {NULL, NULL};
469                         bActListElem *ale;
470                         int filter;
471                         
472                         /* filter action data */
473                         filter= (ACTFILTER_VISIBLE | ACTFILTER_FOREDIT | ACTFILTER_IPOKEYS);
474                         actdata_filter(&act_data, filter, base->object->action, ACTCONT_ACTION);
475                         
476                         /* snap to frame */
477                         for (ale= act_data.first; ale; ale= ale->next) {
478                                 actstrip_map_ipo_keys(base->object, ale->key_data, 0, 1); 
479                                 snap_ipo_keys(ale->key_data, snap_mode);
480                                 actstrip_map_ipo_keys(base->object, ale->key_data, 1, 1);
481                         }
482                         BLI_freelistN(&act_data);
483                         
484                         remake_action_ipos(base->object->action);
485                 }
486         }
487         BIF_undo_push("Snap NLA strips");
488         allqueue (REDRAWVIEW3D, 0);
489         allqueue (REMAKEIPO, 0);
490         allqueue (REDRAWIPO, 0);
491         allqueue (REDRAWACTION, 0);
492         allqueue (REDRAWNLA, 0);
493 }
494
495 static void set_active_strip(Object *ob, bActionStrip *act)
496 {
497         bActionStrip *strip;
498         
499         for (strip = ob->nlastrips.first; strip; strip=strip->next)
500                 strip->flag &= ~ACTSTRIP_ACTIVE;
501         
502         if(act) {
503                 act->flag |= ACTSTRIP_ACTIVE;
504         
505                 if(ob->action!=act->act) {
506                         if(ob->action) ob->action->id.us--;
507                         if(act->act->id.lib) {
508                                 ob->action= NULL;
509                         }
510                         else {
511                                 ob->action= act->act;
512                                 id_us_plus(&ob->action->id);
513                         }                       
514                         allqueue(REDRAWIPO, 0);
515                         allqueue(REDRAWVIEW3D, 0);
516                         allqueue(REDRAWACTION, 0);
517                         allqueue(REDRAWNLA, 0);
518                         ob->ctime= -1234567.0f; // eveil! 
519                         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB|OB_RECALC_DATA);
520                 }
521         }       
522 }
523
524 void convert_nla(void)
525 {
526         bActionStrip *strip;
527         Object *ob= OBACT;
528         char str[128];
529         short event;
530         
531         if ((ob==NULL)||(ob->action==NULL)) {
532                 error("Need active Object to convert Action to NLA Strip");
533                 return;
534         }
535         
536         sprintf(str, "Convert Action%%t|%s to NLA Strip%%x1", ob->action->id.name+2);
537         event = pupmenu(str);
538         
539         if (event==1) {
540                 if (ob->action) {
541                         deselect_nlachannel_keys(0);
542                         strip = convert_action_to_strip(ob); //creates a new NLA strip from the action in given object
543                         set_active_strip(ob, strip);
544                         BIF_undo_push("Convert NLA");
545                         allqueue (REDRAWNLA, 0);
546                 }
547         }
548 }
549
550 static void add_nla_block(short event)
551 {
552         Object *ob= OBACT;
553         bAction *act=NULL;
554         bActionStrip *strip;
555         int             cur;
556
557         if (event!=-1){
558                 for (cur = 1, act=G.main->action.first; act; act=act->id.next, cur++){
559                         if (cur==event){
560                                 break;
561                         }
562                 }
563         }
564         
565         /* Bail out if no action was chosen */
566         if (!act){
567                 return;
568         }
569         
570         /* Initialize the new action block */
571         strip = MEM_callocN(sizeof(bActionStrip), "bActionStrip");
572         
573         deselect_nlachannel_keys(0);
574         
575         /* Link the action to the strip */
576         strip->act = act;
577         id_us_plus(&act->id);
578         calc_action_range(strip->act, &strip->actstart, &strip->actend, 1);
579         strip->start = G.scene->r.cfra;         /* could be mval[0] another time... */
580         strip->end = strip->start + (strip->actend-strip->actstart);
581                 /* simple prevention of zero strips */
582         if(strip->start>strip->end-2) 
583                 strip->end= strip->start+100;
584         strip->repeat = 1.0;
585         
586         strip->flag = ACTSTRIP_SELECT|ACTSTRIP_LOCK_ACTION;
587         
588         find_stridechannel(ob, strip);
589         set_active_strip(ob, strip);
590         strip->object= group_get_member_with_action(ob->dup_group, act);
591         if(strip->object)
592                 id_lib_extern(&strip->object->id);      /* checks lib data, sets correct flag for saving then */
593
594         if(ob->nlastrips.first == NULL)
595                 ob->nlaflag |= OB_NLA_OVERRIDE;
596         
597         BLI_addtail(&ob->nlastrips, strip);
598
599         BIF_undo_push("Add NLA strip");
600 }
601
602 static void add_nla_block_by_name(char name[32], Object *ob, short hold, short add, float repeat)
603 {
604         bAction *act=NULL;
605         bActionStrip *strip;
606         int             cur;
607
608         if (name){
609                 for (cur = 1, act=G.main->action.first; act; act=act->id.next, cur++){
610                         if (strcmp(name,act->id.name)==0) {
611                                 break;
612                         }
613                 }
614         }
615         
616         /* Bail out if no action was chosen */
617         if (!act){
618                 return;
619         }
620         
621         /* Initialize the new action block */
622         strip = MEM_callocN(sizeof(bActionStrip), "bActionStrip");
623         
624         deselect_nlachannel_keys(0);
625         
626         /* Link the action to the strip */
627         strip->act = act;
628         calc_action_range(strip->act, &strip->actstart, &strip->actend, 1);
629         strip->start = G.scene->r.cfra;         /* could be mval[0] another time... */
630         strip->end = strip->start + (strip->actend-strip->actstart);
631                 /* simple prevention of zero strips */
632         if(strip->start>strip->end-2) 
633                 strip->end= strip->start+100;
634         
635         strip->flag = ACTSTRIP_SELECT|ACTSTRIP_LOCK_ACTION; //|ACTSTRIP_USEMATCH;
636         
637         if (hold==1)
638                 strip->flag = strip->flag|ACTSTRIP_HOLDLASTFRAME;
639                 
640         if (add==1)
641                 strip->mode = ACTSTRIPMODE_ADD;
642         
643         find_stridechannel(ob, strip);
644         
645         set_active_strip(ob, strip);
646         
647         strip->repeat = repeat;
648         
649         act->id.us++;
650         
651         if(ob->nlastrips.first == NULL)
652                 ob->nlaflag |= OB_NLA_OVERRIDE;
653                 
654         BLI_addtail(&ob->nlastrips, strip);
655 }
656
657 static void add_nla_databrowse_callback(unsigned short val)
658 {
659         /* val is not used, databrowse needs it to optional pass an event */
660         short event;
661         
662         if(OBACT==NULL) return;
663         
664         event= G.snla->menunr;  /* set by databrowse or pupmenu */
665         
666         add_nla_block(event);
667 }
668
669 /* Adds strip to to active Object */
670 void add_nlablock(void)
671 {
672         Object *ob= OBACT;
673         short event;
674         short nr=0;
675         char *str, title[64];
676         
677         if(ob==NULL) {
678                 error("Need active Object to add NLA strips");
679                 return;
680         }
681         
682         sprintf(title, "Add Action strip to: %s", ob->id.name+2);
683         
684         /* Popup action menu */
685         IDnames_to_pupstring(&str, title, NULL, &G.main->action, (ID *)G.scene, &nr);
686         
687         if(nr==-2) {
688                 MEM_freeN(str);
689
690                 activate_databrowse((ID *)NULL, ID_AC, 0, 0, &G.snla->menunr, 
691                                                         add_nla_databrowse_callback );
692                 
693                 return;                 
694         }
695         else {
696                 event = pupmenu_col(str, 20);
697                 MEM_freeN(str);
698                 add_nla_block(event);
699         }
700 }
701
702 /* Creates a new action, and makes a new actionstrip of that */
703 void add_empty_nlablock(void)
704 {
705         Object *ob= OBACT;
706         bAction *act= NULL;
707         bActionStrip *strip;
708         
709         /* check for active object first - will add strip to active object */
710         if (ob == NULL) 
711                 return;
712                 
713         /* make new action */
714         if ((ob->type == OB_ARMATURE) && (ob->flag & OB_POSEMODE))
715                 act= add_empty_action("ObAction");
716         else
717                 act= add_empty_action("Action");
718                 
719         /* make a new strip for it */
720         add_nla_block_by_name(act->id.name, ob, 0, 1, 1.0f);
721         strip= ob->nlastrips.last; 
722         
723         /* change some settings of the strip - try to avoid bad scaling */
724         if ((EFRA-CFRA) < 100) {
725                 strip->flag |= ACTSTRIP_AUTO_BLENDS;
726                 strip->flag &= ~ACTSTRIP_LOCK_ACTION;
727                 strip->actstart = CFRA;
728                 strip->actend = CFRA + 100;
729                 
730                 strip->start = CFRA;
731                 strip->end = CFRA + 100;
732         }
733         else {
734                 strip->flag |= ACTSTRIP_AUTO_BLENDS;
735                 strip->flag &= ~ACTSTRIP_LOCK_ACTION;
736                 strip->actstart = CFRA;
737                 strip->actend = EFRA;
738                 
739                 strip->start = CFRA;
740                 strip->end = EFRA;
741         }
742         
743         BIF_undo_push("Add NLA strip");
744 }
745
746 /* Adds strip to to active Object */
747 static void relink_active_strip(void)
748 {
749         Object *ob= OBACT;
750         bActionStrip *strip;
751         bAction *act;
752         short event;
753         short cur;
754         char *str;
755         
756         if(ob==NULL) return;
757         if(ob->nlaflag & OB_NLA_COLLAPSED) return;
758         
759         for (strip = ob->nlastrips.first; strip; strip=strip->next)
760                 if(strip->flag & ACTSTRIP_ACTIVE)
761                         break;
762         
763         if(strip==NULL) return;
764         
765         /* Popup action menu */
766         IDnames_to_pupstring(&str, "Relink Action strip", NULL, &G.main->action, (ID *)G.scene, NULL);
767         if(str) {
768                 event = pupmenu_col(str, 20);
769                 MEM_freeN(str);
770                 
771                 for (cur = 1, act=G.main->action.first; act; act=act->id.next, cur++){
772                         if (cur==event){
773                                 break;
774                         }
775                 }
776                 
777                 if(act) {
778                         if(strip->act) strip->act->id.us--;
779                         strip->act = act;
780                         id_us_plus(&act->id);
781                         
782                         allqueue(REDRAWVIEW3D, 0);
783                         allqueue(REDRAWACTION, 0);
784                         allqueue(REDRAWNLA, 0);
785                 }
786         }
787 }
788
789
790
791 /* Left hand side of channels display, selects objects */
792 static void mouse_nlachannels(short mval[2])
793 {
794         bActionStrip *strip= NULL;
795         Base    *base;
796         Object *ob=NULL;
797         float   x,y;
798         int             click, obclick=0, actclick=0;
799         int             wsize;
800         
801         wsize = (count_nla_levels ()*(NLACHANNELHEIGHT+NLACHANNELSKIP));
802         wsize+= NLACHANNELHEIGHT/2;
803
804         areamouseco_to_ipoco(G.v2d, mval, &x, &y);
805         click = (int)floor( ((float)wsize - y) / (NLACHANNELHEIGHT+NLACHANNELSKIP));
806         
807         if (click<0)
808                 return;
809
810         for (base = G.scene->base.first; base; base=base->next){
811                 if (nla_filter(base)) {
812                         ob= base->object;
813                         
814                         /* See if this is a base selected */
815                         if (click==0) {
816                                 obclick= 1;
817                                 break;
818                         }
819                         click--;
820                         
821                         /* see if any strips under object */
822                         if ((ob->nlaflag & OB_NLA_COLLAPSED)==0) {
823                                 /* See if this is an action */
824                                 if (ob->action){
825                                         if (click==0) {
826                                                 actclick= 1;
827                                                 break;
828                                         }
829                                         click--;
830                                 }
831
832                                 /* See if this is an nla strip */
833                                 if(ob->nlastrips.first) {
834                                         for (strip = ob->nlastrips.first; strip; strip=strip->next){
835                                                 if (click==0) break;
836                                                 click--;                                
837                                         }
838                                         if (strip && click==0) break;
839                                 }
840                         }
841                 }
842         }
843
844         if (!base)
845                 return;
846
847         /* Handle object strip selection */
848         if (G.qual & LR_SHIFTKEY) {
849                 if (base->flag & SELECT) base->flag &= ~SELECT;
850                 else base->flag |= SELECT;
851         }
852         else {
853                 deselect_nlachannels (0);       // Auto clear
854                 base->flag |= SELECT;
855         }
856         ob->flag= base->flag;
857         
858         if(base!=BASACT) set_active_base(base);
859         
860         if(actclick) /* de-activate all strips */
861                 set_active_strip(ob, NULL);
862         else if(strip) {
863                 if(mval[0] >= (NLAWIDTH-16)) /* toggle strip muting */
864                         strip->flag ^= ACTSTRIP_MUTE;
865                 else /* set action */
866                         set_active_strip(ob, strip);
867         }
868
869         /* icon toggles beside strip */
870         if (obclick && mval[0]<20) {
871                 /* collapse option for NLA object strip */
872                 ob->nlaflag ^= OB_NLA_COLLAPSED;
873         }
874         else if(obclick && mval[0]<36) {
875                 /* override option for NLA */
876                 ob->nlaflag ^= OB_NLA_OVERRIDE;
877         }
878         else if((obclick) && (ob->ipo) && (mval[0] >= (NLAWIDTH-16))) {
879                 /* mute Object IPO-block */
880                 ob->ipo->muteipo = (ob->ipo->muteipo)? 0: 1;
881         }
882         
883         ob->ctime= -1234567.0f; // eveil! 
884         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB|OB_RECALC_DATA);
885
886         allqueue(REDRAWIPO, 0);
887         allqueue(REDRAWVIEW3D, 0);
888         allqueue(REDRAWACTION, 0);
889         allqueue(REDRAWNLA, 0);
890         
891 }
892
893 void deselect_nlachannel_keys (int test)
894 {
895         Base                    *base;
896         int                             sel=1;
897         bActionChannel  *chan;
898         bActionStrip    *strip;
899         bConstraintChannel *conchan;
900         
901         /* Determine if this is selection or deselection */
902         if (test){
903                 for (base=G.scene->base.first; base && sel; base=base->next){           
904                         /* Test object ipos */
905                         if (is_ipo_key_selected(base->object->ipo)){
906                                 sel = 0;
907                                 break;
908                         }
909                         
910                         /* Test object constraint ipos */
911                         if (sel){
912                                 for (conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next){
913                                         if (is_ipo_key_selected(conchan->ipo)){
914                                                 sel=0;
915                                                 break;
916                                         }
917                                 }
918                         }
919                         
920                         /* check if collapsed */
921                         if (base->object->nlaflag & OB_NLA_COLLAPSED)
922                                 continue;
923                         
924                         /* Test action ipos */
925                         if (sel){
926                                 if (base->object->action){
927                                         for (chan=base->object->action->chanbase.first; chan; chan=chan->next){
928                                                 if (is_ipo_key_selected(chan->ipo)){
929                                                         sel=0;
930                                                         break;
931                                                 }
932
933                                                 /* Test action constraints */
934                                                 if (sel){
935                                                         for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
936                                                                 if (is_ipo_key_selected(conchan->ipo)){
937                                                                         sel=0;
938                                                                         break;
939                                                                 }
940                                                         }
941                                                 }
942                                         }
943                                 }
944                         }
945                         
946                         /* Test NLA strips */
947                         if (sel){
948                                 for (strip=base->object->nlastrips.first; strip; strip=strip->next){
949                                         if (strip->flag & ACTSTRIP_SELECT){
950                                                 sel = 0;
951                                                 break;
952                                         }
953                                 }
954                         }
955                 }
956         }
957         else
958                 sel=0;
959         
960         
961         /* Set the flags */
962         for (base=G.scene->base.first; base; base=base->next){          
963                 /* Set the object ipos */
964                 set_ipo_key_selection(base->object->ipo, sel);
965                 
966                 /* Set the object constraint ipos */
967                 for (conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next){
968                         set_ipo_key_selection(conchan->ipo, sel);                       
969                 }
970
971                 /* check if collapsed */
972                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
973                         continue;
974                 
975                 /* Set the action ipos */
976                 if (base->object->action){
977                         for (chan=base->object->action->chanbase.first; chan; chan=chan->next){
978                                 set_ipo_key_selection(chan->ipo, sel);
979                                 /* Set the action constraint ipos */
980                                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
981                                         set_ipo_key_selection(conchan->ipo, sel);
982                         }
983                 }
984                 
985                 /* Set the nlastrips */
986                 for (strip=base->object->nlastrips.first; strip; strip=strip->next){
987                         if (sel)
988                                 strip->flag |= ACTSTRIP_SELECT;
989                         else
990                                 strip->flag &= ~ACTSTRIP_SELECT;
991                 }
992         }
993 }
994
995 /* very bad call! */
996 static void recalc_all_ipos(void)
997 {
998         Ipo *ipo;
999         IpoCurve *icu;
1000         
1001         /* Go to each ipo */
1002         for (ipo=G.main->ipo.first; ipo; ipo=ipo->id.next){
1003                 for (icu = ipo->curve.first; icu; icu=icu->next){
1004                         sort_time_ipocurve(icu);
1005                         testhandles_ipocurve(icu);
1006                 }
1007         }
1008 }
1009
1010 void transform_nlachannel_keys(int mode, int dummy)
1011 {
1012         switch (mode) {
1013                 case 'g':
1014                 {
1015                         initTransform(TFM_TIME_TRANSLATE, CTX_NONE);
1016                         Transform();
1017                 }
1018                         break;
1019                 case 's':
1020                 {
1021                         initTransform(TFM_TIME_SCALE, CTX_NONE);
1022                         Transform();
1023                 }
1024                         break;
1025                 case 'e':
1026                 {
1027                         initTransform(TFM_TIME_EXTEND, CTX_NONE);
1028                         Transform();
1029                 }
1030                         break;
1031         }
1032 }
1033
1034 void delete_nlachannel_keys(void)
1035 {
1036         Base *base;
1037         bActionChannel *chan;
1038         bConstraintChannel *conchan;
1039         bActionStrip *strip, *nextstrip;
1040                 
1041         for (base = G.scene->base.first; base; base=base->next){
1042                 /* Delete object ipos */
1043                 delete_ipo_keys(base->object->ipo);
1044                 
1045                 /* Delete object constraint keys */
1046                 for(conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next)
1047                         delete_ipo_keys(conchan->ipo);
1048
1049                 /* skip actions and nlastrips if object collapsed */
1050                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
1051                         continue;
1052                         
1053                 /* Delete NLA strips */
1054                 for (strip = base->object->nlastrips.first; strip; strip=nextstrip){
1055                         nextstrip=strip->next;
1056                         if (strip->flag & ACTSTRIP_SELECT){
1057                                 free_actionstrip(strip);
1058                                 BLI_remlink(&base->object->nlastrips, strip);
1059                                 MEM_freeN(strip);
1060                         }
1061                 }
1062                 
1063                 /* Delete action ipos */
1064                 if (base->object->action){
1065                         for (chan=base->object->action->chanbase.first; chan; chan=chan->next){
1066                                 if (EDITABLE_ACHAN(chan))
1067                                         delete_ipo_keys(chan->ipo);
1068                                         
1069                                 /* Delete action constraint keys */
1070                                 for(conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
1071                                         if (EDITABLE_CONCHAN(conchan))
1072                                                 delete_ipo_keys(conchan->ipo);
1073                                 }
1074                         }
1075                 }
1076         }
1077         
1078         recalc_all_ipos();      // bad
1079         synchronize_action_strips();
1080         
1081         BIF_undo_push("Delete NLA keys");
1082         allspace(REMAKEIPO,0);
1083         allqueue (REDRAWVIEW3D, 0);
1084         allqueue(REDRAWNLA, 0);
1085         allqueue(REDRAWIPO, 0);
1086 }
1087
1088 void duplicate_nlachannel_keys(void)
1089 {
1090         Base *base;
1091         bActionChannel *chan;
1092         bConstraintChannel *conchan;
1093         bActionStrip *strip, *laststrip;
1094         
1095         /* Find selected items */
1096         for (base = G.scene->base.first; base; base=base->next){        
1097                 /* Duplicate object keys */
1098                 duplicate_ipo_keys(base->object->ipo);
1099                 
1100                 /* Duplicate object constraint keys */
1101                 for(conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next)
1102                         duplicate_ipo_keys(conchan->ipo);
1103                 
1104                 /* skip actions and nlastrips if object collapsed */
1105                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
1106                         continue;
1107                         
1108                 /* Duplicate nla strips */
1109                 laststrip = base->object->nlastrips.last;
1110                 for (strip=base->object->nlastrips.first; strip; strip=strip->next){
1111                         if (strip->flag & ACTSTRIP_SELECT){
1112                                 bActionStrip *newstrip;
1113                                 
1114                                 copy_actionstrip(&newstrip, &strip);
1115                                 
1116                                 BLI_addtail(&base->object->nlastrips, newstrip);
1117                                 
1118                                 strip->flag &= ~ACTSTRIP_SELECT;
1119                                 newstrip->flag |= ACTSTRIP_SELECT;
1120                                 set_active_strip(base->object, newstrip);
1121
1122                         }
1123                         if (strip==laststrip)
1124                                 break;
1125                 }
1126                 
1127                 /* Duplicate actionchannel keys */
1128                 if (base->object->action){
1129                         for (chan=base->object->action->chanbase.first; chan; chan=chan->next){
1130                                 if (EDITABLE_ACHAN(chan))
1131                                         duplicate_ipo_keys(chan->ipo);
1132                                         
1133                                 /* Duplicate action constraint keys */
1134                                 for(conchan=chan->constraintChannels.first; conchan; conchan=conchan->next) {
1135                                         if (EDITABLE_CONCHAN(conchan))
1136                                                 duplicate_ipo_keys(conchan->ipo);
1137                                 }
1138                         }
1139                 }
1140         }
1141         
1142         BIF_undo_push("Duplicate NLA");
1143         transform_nlachannel_keys ('g', 0);
1144 }
1145
1146 void borderselect_nla(void)
1147
1148         Base *base;
1149         rcti rect;
1150         rctf rectf;
1151         int  val, selectmode;
1152         short   mval[2];
1153         float   ymin, ymax;
1154         bActionStrip *strip;
1155         bConstraintChannel *conchan;
1156         
1157         if ( (val = get_border (&rect, 3)) ){
1158             if (val == LEFTMOUSE)
1159                         selectmode = SELECT_ADD;
1160             else
1161                         selectmode = SELECT_SUBTRACT;
1162           
1163                 mval[0]= rect.xmin;
1164                 mval[1]= rect.ymin+2;
1165                 areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
1166                 mval[0]= rect.xmax;
1167                 mval[1]= rect.ymax-2;
1168                 areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
1169                 
1170                 ymax = count_nla_levels();
1171                 ymax*= (NLACHANNELHEIGHT+NLACHANNELSKIP);
1172                 ymax+= (NLACHANNELHEIGHT+NLACHANNELSKIP)/2;
1173         
1174                 for (base=G.scene->base.first; base; base=base->next){
1175                         if (nla_filter(base)) {
1176                                 
1177                                 ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1178                                 
1179                                 /* Check object ipos */
1180                                 if (base->object->ipo){
1181                                         if (!((ymax < rectf.ymin) || (ymin > rectf.ymax)))
1182                                                 borderselect_ipo_key(base->object->ipo, rectf.xmin, rectf.xmax,
1183                                  selectmode);
1184                                 }
1185                                 /* Check object constraint ipos */
1186                                 for (conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next){
1187                                         if (!((ymax < rectf.ymin) || (ymin > rectf.ymax)))
1188                                                 borderselect_ipo_key(conchan->ipo, rectf.xmin, rectf.xmax,
1189                                  selectmode);
1190                                 }
1191                                 
1192                                 ymax=ymin;
1193                                 
1194                                 /* skip actions and nlastrips if object collapsed */
1195                                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
1196                                         continue;
1197                                 
1198                                 /* Check action ipos */
1199                                 if (base->object->action){
1200                                         bActionChannel *chan;
1201                                         float xmin, xmax;
1202                                         
1203                                         ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1204                                         
1205                                         /* if action is mapped in NLA, it returns a correction */
1206                                         xmin= get_action_frame(base->object, rectf.xmin);
1207                                         xmax= get_action_frame(base->object, rectf.xmax);
1208                                         
1209                                         if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
1210                                                 for (chan=base->object->action->chanbase.first; chan; chan=chan->next){
1211                                                         borderselect_ipo_key(chan->ipo, xmin, xmax, selectmode);
1212                                                         /* Check action constraint ipos */
1213                                                         for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
1214                                                                 borderselect_ipo_key(conchan->ipo, xmin, xmax, selectmode);
1215                                                 }
1216                                         }
1217
1218                                         ymax=ymin;
1219                                 }       /* End of if action */
1220                                 
1221                                 /* Skip nlastrips */
1222                                 for (strip=base->object->nlastrips.first; strip; strip=strip->next){
1223                                         ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1224                                         //
1225                                         if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
1226                                                 if (!((rectf.xmax<strip->start) || (rectf.xmin>strip->end))){
1227                                                         if (val==1)
1228                                                                 strip->flag |= ACTSTRIP_SELECT;
1229                                                         else
1230                                                                 strip->flag &= ~ACTSTRIP_SELECT;
1231                                                 }
1232                                         }
1233                                         
1234                                         ymax=ymin;
1235                                 }
1236                         }
1237                 }       
1238                 BIF_undo_push("Border select NLA");
1239                 allqueue(REDRAWMARKER, 0);
1240         }
1241 }
1242
1243 /* right hand side of window, does ipokeys, actionkeys or strips */
1244 static void mouse_nla(int selectmode)
1245 {
1246         Base *base;
1247         bAction *act;
1248         bActionChannel *chan;
1249         bActionStrip *rstrip;
1250         bConstraintChannel *conchan;
1251         TimeMarker *marker;
1252         float   selx;
1253         short   mval[2];
1254         short sel, isdone=0;
1255         
1256         getmouseco_areawin (mval);
1257         
1258         /* Try object ipo or ob-constraint ipo selection */
1259         base= get_nearest_nlachannel_ob_key(&selx, &sel);
1260         marker=find_nearest_marker(1);
1261         if (base) {
1262                 isdone= 1;
1263                 
1264                 if (selectmode == SELECT_REPLACE){
1265                         deselect_nlachannel_keys(0);
1266                         selectmode = SELECT_ADD;
1267                 }
1268                 
1269                 select_ipo_key(base->object->ipo, selx, selectmode);
1270                 
1271                 /* Try object constraint selection */
1272                 for (conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next)
1273                         select_ipo_key(conchan->ipo, selx, selectmode);
1274         }
1275         else if (marker) {
1276                 /* marker */            
1277                 if (selectmode == SELECT_REPLACE) {                     
1278                         deselect_markers(0, 0);
1279                         marker->flag |= SELECT;
1280                 }
1281                 else if (selectmode == SELECT_INVERT) {
1282                         if (marker->flag & SELECT)
1283                                 marker->flag &= ~SELECT;
1284                         else
1285                                 marker->flag |= SELECT;
1286                 }
1287                 else if (selectmode == SELECT_ADD) 
1288                         marker->flag |= SELECT;
1289                 else if (selectmode == SELECT_SUBTRACT)
1290                         marker->flag &= ~SELECT;
1291                 
1292                 std_rmouse_transform(transform_markers);
1293                 
1294                 allqueue(REDRAWMARKER, 0);
1295         }
1296         else {
1297                 /* Try action ipo selection */
1298                 act= get_nearest_nlachannel_ac_key(&selx, &sel);
1299                 if (act) {
1300                         isdone= 1;
1301                         
1302                         if (selectmode == SELECT_REPLACE){
1303                                 deselect_nlachannel_keys(0);
1304                                 selectmode = SELECT_ADD;
1305                         }
1306                         
1307                         for (chan=act->chanbase.first; chan; chan=chan->next) {
1308                                 select_ipo_key(chan->ipo, selx, selectmode);
1309                                 /* Try action constraint selection */
1310                                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
1311                                         select_ipo_key(conchan->ipo, selx, selectmode);
1312                         }
1313                 }
1314                 else {
1315         
1316                         /* Try nla strip selection */
1317                         base= get_nearest_nlastrip(&rstrip, &sel);
1318                         if (base){
1319                                 isdone= 1;
1320                                 
1321                                 if (!(G.qual & LR_SHIFTKEY)){
1322                                         deselect_nlachannel_keys(0);
1323                                         sel = 0;
1324                                 }
1325                                 
1326                                 if (sel)
1327                                         rstrip->flag &= ~ACTSTRIP_SELECT;
1328                                 else
1329                                         rstrip->flag |= ACTSTRIP_SELECT;
1330                                 
1331                                 set_active_strip(base->object, rstrip);
1332                                 
1333                                 if(base!=BASACT) set_active_base(base);
1334                         }
1335                 }
1336         }
1337         
1338         if(isdone) {
1339                 std_rmouse_transform(transform_nlachannel_keys);
1340                 
1341                 allqueue(REDRAWIPO, 0);
1342                 allqueue(REDRAWVIEW3D, 0);
1343                 allqueue(REDRAWNLA, 0);
1344         }
1345 }
1346
1347 /* This function is currently more complicated than it seems like it should be.
1348 * However, this will be needed once the nla strip timeline is more complex */
1349 static Base *get_nearest_nlastrip (bActionStrip **rstrip, short *sel)
1350 {
1351         Base *base, *firstbase=NULL;
1352         bActionStrip *strip, *firststrip=NULL, *foundstrip=NULL;
1353         rctf    rectf;
1354         float ymin, ymax;
1355         short mval[2];
1356         short foundsel = 0;
1357
1358         getmouseco_areawin (mval);
1359         
1360         mval[0]-=7;
1361         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
1362         
1363         mval[0]+=14;
1364         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
1365         
1366         ymax = count_nla_levels();
1367         ymax*=(NLACHANNELHEIGHT + NLACHANNELSKIP);
1368         ymax+= NLACHANNELHEIGHT/2;
1369         
1370         for (base = G.scene->base.first; base; base=base->next){
1371                 if (nla_filter(base)) {
1372                         
1373                         /* Skip object ipos */
1374                         ymax-=(NLACHANNELHEIGHT+NLACHANNELSKIP);
1375                         
1376                         /* check if skip strips if collapsed */
1377                         if (base->object->nlaflag & OB_NLA_COLLAPSED)
1378                                 continue;
1379
1380                         /* Skip action ipos */
1381                         if (base->object->action)
1382                                 ymax-=(NLACHANNELHEIGHT+NLACHANNELSKIP);
1383                         
1384                         /* the strips */
1385                         for (strip=base->object->nlastrips.first; strip; strip=strip->next){
1386                                 ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1387                                 /* Do Ytest */
1388                                 if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
1389                                         /* Do XTest */
1390                                         if (!((rectf.xmax<strip->start) || (rectf.xmin>strip->end))){
1391                                                 if (!firstbase){
1392                                                         firstbase=base;
1393                                                         firststrip=strip;
1394                                                         *sel = strip->flag & ACTSTRIP_SELECT;
1395                                                 }
1396                                                 
1397                                                 if (strip->flag & ACTSTRIP_SELECT){ 
1398                                                         if (!foundsel){
1399                                                                 foundsel=1;
1400                                                                 foundstrip = strip;
1401                                                         }
1402                                                 }
1403                                                 else if (foundsel && strip != foundstrip){
1404                                                         *rstrip=strip;
1405                                                         *sel = 0;
1406                                                         return base;
1407                                                 }
1408                                         }
1409                                 }
1410                                 ymax=ymin;
1411                         }
1412                 }
1413         }
1414         *rstrip=firststrip;
1415         return firstbase;
1416 }
1417
1418 static Base *get_nearest_nlachannel_ob_key (float *index, short *sel)
1419 {
1420         Base *base;
1421         IpoCurve *icu;
1422         Base *firstbase=NULL;
1423         bConstraintChannel *conchan;
1424         int     foundsel=0;
1425         float firstvertx=-1, foundx=-1;
1426         int i;
1427         short mval[2];
1428         float ymin, ymax;
1429         rctf    rectf;
1430         
1431         *index=0;
1432         
1433         getmouseco_areawin (mval);
1434         
1435         mval[0]-=7;
1436         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
1437         
1438         mval[0]+=14;
1439         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
1440         
1441         ymax = count_nla_levels();
1442         
1443         ymax*= (NLACHANNELHEIGHT + NLACHANNELSKIP);
1444         ymax+= NLACHANNELHEIGHT/2;
1445         
1446         *sel=0;
1447         
1448         for (base=G.scene->base.first; base; base=base->next){
1449                 if (nla_filter(base)) {
1450                         
1451                         ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1452                         
1453                         /* Handle object ipo selection */
1454                         if (base->object->ipo){
1455                                 if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
1456                                         for (icu=base->object->ipo->curve.first; icu; icu=icu->next){
1457                                                 for (i=0; i<icu->totvert; i++){
1458                                                         if (icu->bezt[i].vec[1][0] > rectf.xmin && icu->bezt[i].vec[1][0] <= rectf.xmax ){
1459                                                                 if (!firstbase){
1460                                                                         firstbase=base;
1461                                                                         firstvertx=icu->bezt[i].vec[1][0];
1462                                                                         *sel = icu->bezt[i].f2 & 1;     
1463                                                                 }
1464                                                                 
1465                                                                 if (icu->bezt[i].f2 & 1){ 
1466                                                                         if (!foundsel){
1467                                                                                 foundsel=1;
1468                                                                                 foundx = icu->bezt[i].vec[1][0];
1469                                                                         }
1470                                                                 }
1471                                                                 else if (foundsel && icu->bezt[i].vec[1][0] != foundx){
1472                                                                         *index=icu->bezt[i].vec[1][0];
1473                                                                         *sel = 0;
1474                                                                         return base;
1475                                                                 }
1476                                                         }
1477                                                 }
1478                                         }
1479                                 }
1480                         }
1481                         /* Handle object constraint ipos */
1482                         for (conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next){
1483                                 if ( !((ymax < rectf.ymin) || (ymin > rectf.ymax)) && conchan->ipo){
1484                                         for (icu=conchan->ipo->curve.first; icu; icu=icu->next){
1485                                                 for (i=0; i<icu->totvert; i++){
1486                                                         if (icu->bezt[i].vec[1][0] > rectf.xmin && icu->bezt[i].vec[1][0] <= rectf.xmax ){
1487                                                                 if (!firstbase){
1488                                                                         firstbase=base;
1489                                                                         firstvertx=icu->bezt[i].vec[1][0];
1490                                                                         *sel = icu->bezt[i].f2 & 1;     
1491                                                                 }
1492                                                                 
1493                                                                 if (icu->bezt[i].f2 & 1){ 
1494                                                                         if (!foundsel){
1495                                                                                 foundsel=1;
1496                                                                                 foundx = icu->bezt[i].vec[1][0];
1497                                                                         }
1498                                                                 }
1499                                                                 else if (foundsel && icu->bezt[i].vec[1][0] != foundx){
1500                                                                         *index=icu->bezt[i].vec[1][0];
1501                                                                         *sel = 0;
1502                                                                         return base;
1503                                                                 }
1504                                                         }
1505                                                 }
1506                                         }
1507                                 }
1508                         }
1509
1510                         ymax=ymin;
1511                         
1512                         /* Skip actions and nlastrips if object is collapsed */
1513                         if (base->object->nlaflag & OB_NLA_COLLAPSED)
1514                                 continue;
1515                         
1516                         /* Skip action ipos */
1517                         if (base->object->action){
1518                                 ymax-=(NLACHANNELHEIGHT+NLACHANNELSKIP);
1519                         }
1520                         /* Skip nlastrips */
1521                         ymax-=(NLACHANNELHEIGHT+NLACHANNELSKIP)*BLI_countlist(&base->object->nlastrips);
1522                 }
1523         }       
1524         
1525         *index=firstvertx;
1526         return firstbase;
1527 }
1528
1529 static bAction *get_nearest_nlachannel_ac_key (float *index, short *sel)
1530 {
1531         Base *base;
1532         IpoCurve *icu;
1533         bAction *firstact=NULL;
1534         bActionChannel *chan;
1535         bConstraintChannel *conchan;
1536         rctf    rectf;
1537         float firstvert=-1, foundx=-1;
1538         float ymin, ymax, xmin, xmax;
1539         int i;
1540         int     foundsel=0;
1541         short mval[2];
1542         
1543         *index=0;
1544         
1545         getmouseco_areawin (mval);
1546         
1547         mval[0]-=7;
1548         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
1549         
1550         mval[0]+=14;
1551         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
1552         
1553         ymax = count_nla_levels();
1554         
1555         ymax*= (NLACHANNELHEIGHT + NLACHANNELSKIP);
1556         ymax+= NLACHANNELHEIGHT/2;
1557         
1558         *sel=0;
1559         
1560         for (base=G.scene->base.first; base; base=base->next){
1561                 /* Handle object ipo selection */
1562                 if (nla_filter(base)) {
1563                         
1564                         /* Skip object ipo and ob-constraint ipo */
1565                         ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1566                         
1567                         /* skip this object if it is collapsed */
1568                         if (base->object->nlaflag & OB_NLA_COLLAPSED)
1569                                 continue;
1570                         
1571                         ymax=ymin;
1572                         
1573                         /* Handle action ipos */
1574                         if (base->object->action){
1575                                 bAction *act= base->object->action;
1576                                 
1577                                 /* if action is mapped in NLA, it returns a correction */
1578                                 xmin= get_action_frame(base->object, rectf.xmin);
1579                                 xmax= get_action_frame(base->object, rectf.xmax);
1580                                 
1581                                 ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1582                                 if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
1583                                         for (chan=act->chanbase.first; chan; chan=chan->next){
1584                                                 if(chan->ipo) {
1585                                                         for (icu=chan->ipo->curve.first; icu; icu=icu->next){
1586                                                                 for (i=0; i<icu->totvert; i++){
1587                                                                         if (icu->bezt[i].vec[1][0] > xmin && icu->bezt[i].vec[1][0] <= xmax ){
1588                                                                                 if (!firstact){
1589                                                                                         firstact= act;
1590                                                                                         firstvert=icu->bezt[i].vec[1][0];
1591                                                                                         *sel = icu->bezt[i].f2 & 1;     
1592                                                                                 }
1593                                                                                 
1594                                                                                 if (icu->bezt[i].f2 & 1){ 
1595                                                                                         if (!foundsel){
1596                                                                                                 foundsel=1;
1597                                                                                                 foundx = icu->bezt[i].vec[1][0];
1598                                                                                         }
1599                                                                                 }
1600                                                                                 else if (foundsel && icu->bezt[i].vec[1][0] != foundx){
1601                                                                                         *index=icu->bezt[i].vec[1][0];
1602                                                                                         *sel = 0;
1603                                                                                         return act;
1604                                                                                 }
1605                                                                         }
1606                                                                 }
1607                                                         }
1608                                                 }
1609                                                 
1610                                                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
1611                                                         ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1612                                                         if ( !((ymax < rectf.ymin) || (ymin > rectf.ymax)) && conchan->ipo){
1613                                                                 for (icu=conchan->ipo->curve.first; icu; icu=icu->next){
1614                                                                         for (i=0; i<icu->totvert; i++){
1615                                                                                 if (icu->bezt[i].vec[1][0] > xmin && icu->bezt[i].vec[1][0] <= xmax ){
1616                                                                                         if (!firstact){
1617                                                                                                 firstact=base->object->action;
1618                                                                                                 firstvert=icu->bezt[i].vec[1][0];
1619                                                                                                 *sel = icu->bezt[i].f2 & 1;     
1620                                                                                         }
1621                                                                                         
1622                                                                                         if (icu->bezt[i].f2 & 1){ 
1623                                                                                                 if (!foundsel){
1624                                                                                                         foundsel=1;
1625                                                                                                         foundx = icu->bezt[i].vec[1][0];
1626                                                                                                 }
1627                                                                                         }
1628                                                                                         else if (foundsel && icu->bezt[i].vec[1][0] != foundx){
1629                                                                                                 *index=icu->bezt[i].vec[1][0];
1630                                                                                                 *sel = 0;
1631                                                                                                 return base->object->action;
1632                                                                                         }
1633                                                                                 }
1634                                                                         }
1635                                                                 }
1636                                                         }
1637                                                         ymax=ymin;
1638                                                 }
1639                                         
1640                                         
1641                                         }
1642                                 }                       
1643                                 ymax=ymin;
1644                         }
1645                         
1646                         /* Skip nlastrips */
1647                         ymax-=(NLACHANNELHEIGHT+NLACHANNELSKIP)*BLI_countlist(&base->object->nlastrips);
1648                 }
1649         }       
1650         
1651         *index=firstvert;
1652         return firstact;
1653 }
1654
1655 void deselect_nlachannels(int test)
1656 {
1657         Base *base;
1658         int sel = 1;
1659
1660         if (test){
1661                 for (base=G.scene->base.first; base; base=base->next){
1662                         /* Check base flags for previous selection */
1663                         if (base->flag & SELECT){
1664                                 sel=0;
1665                                 break;
1666                         }
1667                 }
1668         }
1669         else
1670                 sel = 0;
1671
1672         /* Select objects */
1673         for (base=G.scene->base.first; base; base=base->next){
1674                 if (sel){
1675                         if (nla_filter(base))
1676                                 base->flag |= SELECT;
1677                 }
1678                 else
1679                         base->flag &= ~SELECT;
1680                 
1681                 base->object->flag= base->flag;
1682         }       
1683 }
1684
1685 static Object *get_object_from_active_strip(void) {
1686
1687         Base *base;
1688         bActionStrip *strip;
1689         
1690         for (base=G.scene->base.first; base; base=base->next) {
1691                 if ((base->object->nlaflag & OB_NLA_COLLAPSED)==0) {
1692                         for (strip = base->object->nlastrips.first; 
1693                                  strip; strip=strip->next){
1694                                 if (strip->flag & ACTSTRIP_SELECT) {
1695                                         return base->object;
1696                                 }
1697                         }
1698                 }
1699         }
1700         return NULL;
1701 }
1702
1703
1704 void winqreadnlaspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
1705 {
1706         unsigned short event= evt->event;
1707         short val= evt->val;
1708         SpaceNla *snla = curarea->spacedata.first;
1709         int doredraw= 0;
1710         short   mval[2];
1711         float dx,dy;
1712         int     cfra;
1713         short mousebut = L_MOUSE;
1714         Object          *ob; //in shift-B / bake
1715         
1716         if (curarea->win==0) return;
1717         if (!snla) return;
1718         
1719         if(val) {
1720                 if( uiDoBlocks(&curarea->uiblocks, event, 1)!=UI_NOTHING ) event= 0;
1721                 
1722                 /* swap mouse buttons based on user preference */
1723                 if (U.flag & USER_LMOUSESELECT) {
1724                         if (event == LEFTMOUSE) {
1725                                 event = RIGHTMOUSE;
1726                                 mousebut = L_MOUSE;
1727                         } else if (event == RIGHTMOUSE) {
1728                                 event = LEFTMOUSE;
1729                                 mousebut = R_MOUSE;
1730                         }
1731                 }
1732                 
1733                 getmouseco_areawin(mval);
1734                 
1735                 switch(event) {
1736                         case UI_BUT_EVENT:
1737                                 do_nlabuts(val); // in drawnla.c
1738                                 break;
1739                                 
1740                         case HOMEKEY:
1741                                 do_nla_buttons(B_NLAHOME);
1742                                 break;
1743                                 
1744                         case EQUALKEY:
1745                                 shift_nlastrips_up();
1746                                 break;
1747                         
1748                         case PAGEUPKEY:
1749                                 if (G.qual & LR_CTRLKEY)
1750                                         shift_nlastrips_up();
1751                                 else {
1752                                         nextprev_marker(1);
1753                                         allqueue(REDRAWMARKER, 0);
1754                                 }                               
1755                                 break;
1756                                 
1757                         case MINUSKEY:
1758                                 shift_nlastrips_down();
1759                                 break;
1760                                 
1761                         case PAGEDOWNKEY:
1762                                 if (G.qual & LR_CTRLKEY)
1763                                         shift_nlastrips_down();
1764                                 else {
1765                                         nextprev_marker(-1);
1766                                         allqueue(REDRAWMARKER, 0);
1767                                 }
1768                                 break;
1769                                 
1770                         case AKEY:
1771                                 if (G.qual & LR_SHIFTKEY){
1772                                         add_nlablock();
1773                                         allqueue (REDRAWNLA, 0);
1774                                         allqueue (REDRAWVIEW3D, 0);
1775                                 }
1776                                 else if (G.qual & LR_CTRLKEY) {
1777                                         deselect_markers(1, 0);
1778                                         allqueue(REDRAWMARKER, 0);
1779                                 }
1780                                 else{
1781                                         if (mval[0]>=NLAWIDTH)
1782                                                 deselect_nlachannel_keys(1);
1783                                         else{
1784                                                 deselect_nlachannels(1);
1785                                                 allqueue (REDRAWVIEW3D, 0);
1786                                         }
1787                                         allqueue (REDRAWNLA, 0);
1788                                         allqueue (REDRAWIPO, 0);
1789                                         BIF_undo_push("(De)select all NLA");
1790                                 }
1791                                 break;
1792                                 
1793                         case BKEY:
1794                                 if (G.qual & LR_SHIFTKEY){
1795                                         bake_all_to_action();
1796                                         allqueue (REDRAWNLA, 0);
1797                                     allqueue (REDRAWVIEW3D, 0);
1798                                     BIF_undo_push("Bake All To Action");
1799                                     ob = get_object_from_active_strip();
1800                                     //build_match_caches(ob);
1801                                 }
1802                                 else if (G.qual & LR_CTRLKEY) 
1803                                         borderselect_markers();
1804                                 else
1805                                         borderselect_nla();
1806                                 break;
1807                                 
1808                         case CKEY:
1809                                 if(G.qual==LR_CTRLKEY) {
1810                                         if(okee("Copy Modifiers"))
1811                                                 copy_action_modifiers();
1812                                 }
1813                                 else convert_nla();
1814                                 break;
1815                                 
1816                         case DKEY:
1817                                 if (G.qual == (LR_CTRLKEY|LR_SHIFTKEY) && mval[0]>=NLAWIDTH) {
1818                                         duplicate_marker();
1819                                 }
1820                                 else if (G.qual & LR_SHIFTKEY && mval[0]>=NLAWIDTH){
1821                                         duplicate_nlachannel_keys();
1822                                         update_for_newframe_muted();
1823                                 }
1824                                 
1825                                 break;
1826                                 
1827                         case EKEY:
1828                                 if (mval[0] >= NLAWIDTH) {
1829                                         transform_nlachannel_keys ('e', 0);
1830                                         update_for_newframe_muted();
1831                                 }
1832                                 break;
1833                                 
1834                         case GKEY:
1835                                 if (mval[0]>=NLAWIDTH) {
1836                                         if (G.qual & LR_CTRLKEY) {
1837                                                 transform_markers('g', 0);
1838                                         }
1839                                         else {
1840                                                 transform_nlachannel_keys ('g', 0);
1841                                                 update_for_newframe_muted();
1842                                         }
1843                                 }
1844                                 break;
1845                         
1846                         case MKEY:
1847                                 /* marker operations */
1848                                 if (G.qual == 0)
1849                                         add_marker(CFRA);
1850                                 else if (G.qual == LR_CTRLKEY)
1851                                         rename_marker();
1852                                 else 
1853                                         break;
1854                                 allqueue(REDRAWMARKER, 0);
1855                                 break;                          
1856                         
1857                         case NKEY:
1858                                 if(G.qual==0) {
1859                                         toggle_blockhandler(curarea, NLA_HANDLER_PROPERTIES, UI_PNL_TO_MOUSE);
1860                                         scrarea_queue_winredraw(curarea);
1861                                 }
1862                                 else if (G.qual & LR_SHIFTKEY) {
1863                                         add_empty_nlablock();
1864                                 }
1865                                 break;
1866                         case LKEY:
1867                                 relink_active_strip();
1868                                 break;
1869                                 
1870                         case PKEY:
1871                                 if (G.qual & LR_CTRLKEY) /* set preview range */
1872                                         anim_previewrange_set();
1873                                 else if (G.qual & LR_ALTKEY) /* clear preview range */
1874                                         anim_previewrange_clear();
1875                                 allqueue(REDRAWMARKER, 0);
1876                                 break;
1877                                 
1878                         case SKEY:
1879                                 if (G.qual==LR_ALTKEY) {
1880                                         val= pupmenu("Action Strip Scale%t|Reset Strip Scale%x1|Remap Action Start/End%x2|Apply Scale%x3");
1881                                         if (val > 0)
1882                                                 reset_action_strips(val);
1883                                 }
1884                                 else if (G.qual & LR_SHIFTKEY) {
1885                                         if (snla->flag & SNLA_DRAWTIME)
1886                                                 val= pupmenu("Snap To%t|Nearest Second%x3|Current Time%x2");
1887                                         else
1888                                                 val= pupmenu("Snap To%t|Nearest Frame%x1|Current Frame%x2");
1889                                         if (ELEM3(val, 1, 2, 3))
1890                                                 snap_action_strips(val);
1891                                 }
1892                                 else {
1893                                         if (mval[0]>=NLAWIDTH)
1894                                                 transform_nlachannel_keys ('s', 0);
1895                                         update_for_newframe_muted();
1896                                 }
1897                                 break;
1898                                 
1899                         case TKEY:
1900                                 if (G.qual & LR_CTRLKEY) {
1901                                         val= pupmenu("Time value%t|Frames %x1|Seconds%x2");
1902                                         
1903                                         if (val > 0) {
1904                                                 if (val == 2) snla->flag |= SNLA_DRAWTIME;
1905                                                 else snla->flag &= ~SNLA_DRAWTIME;
1906                                                 
1907                                                 doredraw= 1;
1908                                         }
1909                                 }                               
1910                                 break;
1911                                 
1912                         case DELKEY:
1913                         case XKEY:
1914                                 if (mval[0]>=NLAWIDTH) {
1915                                         if (okee("Erase selected?")) {
1916                                                 delete_nlachannel_keys();
1917                                                 update_for_newframe_muted();
1918                                                 
1919                                                 remove_marker();
1920                                                 
1921                                                 allqueue(REDRAWMARKER, 0);
1922                                         }
1923                                 }
1924                                 break;
1925                                 
1926                                 /* LEFTMOUSE and RIGHTMOUSE event codes can be swapped above,
1927                                 * based on user preference USER_LMOUSESELECT
1928                                 */
1929                         case LEFTMOUSE:
1930                                 if(view2dmove(LEFTMOUSE))
1931                                         break; // only checks for sliders
1932                                 else if (mval[0]>=snla->v2d.mask.xmin) {
1933                                         do {
1934                                                 getmouseco_areawin(mval);
1935                                                 
1936                                                 areamouseco_to_ipoco(G.v2d, mval, &dx, &dy);
1937                                                 
1938                                                 cfra= (int)dx;
1939                                                 if(cfra< 1) cfra= 1;
1940                                                 
1941                                                 if( cfra!=CFRA ) {
1942                                                         CFRA= cfra;
1943                                                         update_for_newframe();
1944                                                         force_draw_all(0);
1945                                                 }
1946                                                 else PIL_sleep_ms(30);
1947                                                 
1948                                         } while(get_mbut() & mousebut);
1949                                         break;
1950                                 }
1951                                         /* else pass on! */
1952                                 case RIGHTMOUSE:
1953                                         if (mval[0]>=snla->v2d.mask.xmin) {
1954                                                 if(G.qual & LR_SHIFTKEY)
1955                                                         mouse_nla(SELECT_INVERT);
1956                                                 else
1957                                                         mouse_nla(SELECT_REPLACE);
1958                                         }
1959                                         else
1960                                                 mouse_nlachannels(mval);
1961                                         break;
1962                                         
1963                                 case PADPLUSKEY:
1964                                         view2d_zoom(G.v2d, 0.1154, sa->winx, sa->winy);
1965                                         test_view2d(G.v2d, sa->winx, sa->winy);
1966                                         view2d_do_locks(curarea, V2D_LOCK_COPY);
1967                                         doredraw= 1;
1968                                         break;
1969                                 case PADMINUS:
1970                                         view2d_zoom(G.v2d, -0.15, sa->winx, sa->winy);
1971                                         test_view2d(G.v2d, sa->winx, sa->winy);
1972                                         view2d_do_locks(curarea, V2D_LOCK_COPY);
1973                                         doredraw= 1;
1974                                         break;
1975                                 case MIDDLEMOUSE:
1976                                 case WHEELUPMOUSE:
1977                                 case WHEELDOWNMOUSE:
1978                                         view2dmove(event);      /* in drawipo.c */
1979                                         break;
1980                 }
1981         }
1982         
1983         if(doredraw) scrarea_queue_winredraw(curarea);
1984 }
1985
1986 void bake_all_to_action(void)
1987 {
1988         Object          *ob;
1989         bAction         *newAction;
1990         Ipo             *ipo;
1991         ID              *id;
1992         short           hold, add;
1993         float           repeat;
1994
1995         /* burn object-level motion into a new action */
1996         ob = get_object_from_active_strip();
1997         if (ob) {
1998                 if (ob->flag&OB_ARMATURE) {
1999                         //newAction = bake_obIPO_to_action(ob);
2000                         newAction = NULL;
2001                         if (newAction) {
2002                                 /* unlink the object's IPO */
2003                                 ipo=ob->ipo;
2004                                 if (ipo) {
2005                                         id = &ipo->id;
2006                                         if (id->us > 0)
2007                                                 id->us--;
2008                                         ob->ipo = NULL;
2009                                 }
2010                                 
2011                                 /* add the new Action to NLA as a strip */
2012                                 hold=1;
2013                                 add=1;
2014                                 repeat=1.0;
2015                                 printf("about to add nla block...\n");
2016                                 add_nla_block_by_name(newAction->id.name, ob, hold, add, repeat);
2017                                 BIF_undo_push("Add NLA strip");
2018                         }
2019                 }
2020         }
2021 }
2022
2023 void copy_action_modifiers(void)
2024 {
2025         bActionStrip *strip, *actstrip;
2026         Object *ob= OBACT;
2027         
2028         if(ob==NULL)
2029                 return;
2030         
2031         /* active strip */
2032         for (actstrip=ob->nlastrips.first; actstrip; actstrip=actstrip->next)
2033                 if(actstrip->flag & ACTSTRIP_ACTIVE)
2034                         break;
2035         if(actstrip==NULL)
2036                 return;
2037         
2038         /* copy to selected items */
2039         for (strip=ob->nlastrips.first; strip; strip=strip->next){
2040                 if (strip->flag & ACTSTRIP_SELECT) {
2041                         if(strip!=actstrip) {
2042                                 if (strip->modifiers.first)
2043                                         BLI_freelistN(&strip->modifiers);
2044                                 if (actstrip->modifiers.first)
2045                                         duplicatelist (&strip->modifiers, &actstrip->modifiers);
2046                         }
2047                 }
2048         }
2049         
2050         BIF_undo_push("Copy Action Modifiers");
2051         allqueue(REDRAWNLA, 0);
2052         DAG_scene_flush_update(G.scene, screen_view3d_layers());
2053 }
2054