Mouse-click in the ipo/action/timeline/.. to change the frame
[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 = strip->scale= 1.0f;
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         strip->scale= 1.0f;
624         
625         deselect_nlachannel_keys(0);
626         
627         /* Link the action to the strip */
628         strip->act = act;
629         calc_action_range(strip->act, &strip->actstart, &strip->actend, 1);
630         strip->start = G.scene->r.cfra;         /* could be mval[0] another time... */
631         strip->end = strip->start + (strip->actend-strip->actstart);
632                 /* simple prevention of zero strips */
633         if(strip->start>strip->end-2) 
634                 strip->end= strip->start+100;
635         
636         strip->flag = ACTSTRIP_SELECT|ACTSTRIP_LOCK_ACTION; //|ACTSTRIP_USEMATCH;
637         
638         if (hold==1)
639                 strip->flag = strip->flag|ACTSTRIP_HOLDLASTFRAME;
640                 
641         if (add==1)
642                 strip->mode = ACTSTRIPMODE_ADD;
643         
644         find_stridechannel(ob, strip);
645         
646         set_active_strip(ob, strip);
647         
648         strip->repeat = repeat;
649         
650         act->id.us++;
651         
652         if(ob->nlastrips.first == NULL)
653                 ob->nlaflag |= OB_NLA_OVERRIDE;
654                 
655         BLI_addtail(&ob->nlastrips, strip);
656 }
657
658 static void add_nla_databrowse_callback(unsigned short val)
659 {
660         /* val is not used, databrowse needs it to optional pass an event */
661         short event;
662         
663         if(OBACT==NULL) return;
664         
665         event= G.snla->menunr;  /* set by databrowse or pupmenu */
666         
667         add_nla_block(event);
668 }
669
670 /* Adds strip to to active Object */
671 void add_nlablock(void)
672 {
673         Object *ob= OBACT;
674         short event;
675         short nr=0;
676         char *str, title[64];
677         
678         if(ob==NULL) {
679                 error("Need active Object to add NLA strips");
680                 return;
681         }
682         
683         sprintf(title, "Add Action strip to: %s", ob->id.name+2);
684         
685         /* Popup action menu */
686         IDnames_to_pupstring(&str, title, NULL, &G.main->action, (ID *)G.scene, &nr);
687         
688         if(nr==-2) {
689                 MEM_freeN(str);
690
691                 activate_databrowse((ID *)NULL, ID_AC, 0, 0, &G.snla->menunr, 
692                                                         add_nla_databrowse_callback );
693                 
694                 return;                 
695         }
696         else {
697                 event = pupmenu_col(str, 20);
698                 MEM_freeN(str);
699                 add_nla_block(event);
700         }
701 }
702
703 /* Creates a new action, and makes a new actionstrip of that */
704 void add_empty_nlablock(void)
705 {
706         Object *ob= OBACT;
707         bAction *act= NULL;
708         bActionStrip *strip;
709         
710         /* check for active object first - will add strip to active object */
711         if (ob == NULL) 
712                 return;
713                 
714         /* make new action */
715         if ((ob->type == OB_ARMATURE) && (ob->flag & OB_POSEMODE))
716                 act= add_empty_action("ObAction");
717         else
718                 act= add_empty_action("Action");
719                 
720         /* make a new strip for it */
721         add_nla_block_by_name(act->id.name, ob, 0, 1, 1.0f);
722         strip= ob->nlastrips.last; 
723         
724         /* change some settings of the strip - try to avoid bad scaling */
725         if ((EFRA-CFRA) < 100) {
726                 strip->flag |= ACTSTRIP_AUTO_BLENDS;
727                 strip->flag &= ~ACTSTRIP_LOCK_ACTION;
728                 strip->actstart = CFRA;
729                 strip->actend = CFRA + 100;
730                 
731                 strip->start = CFRA;
732                 strip->end = CFRA + 100;
733         }
734         else {
735                 strip->flag |= ACTSTRIP_AUTO_BLENDS;
736                 strip->flag &= ~ACTSTRIP_LOCK_ACTION;
737                 strip->actstart = CFRA;
738                 strip->actend = EFRA;
739                 
740                 strip->start = CFRA;
741                 strip->end = EFRA;
742         }
743         
744         BIF_undo_push("Add NLA strip");
745 }
746
747 /* Adds strip to to active Object */
748 static void relink_active_strip(void)
749 {
750         Object *ob= OBACT;
751         bActionStrip *strip;
752         bAction *act;
753         short event;
754         short cur;
755         char *str;
756         
757         if(ob==NULL) return;
758         if(ob->nlaflag & OB_NLA_COLLAPSED) return;
759         
760         for (strip = ob->nlastrips.first; strip; strip=strip->next)
761                 if(strip->flag & ACTSTRIP_ACTIVE)
762                         break;
763         
764         if(strip==NULL) return;
765         
766         /* Popup action menu */
767         IDnames_to_pupstring(&str, "Relink Action strip", NULL, &G.main->action, (ID *)G.scene, NULL);
768         if(str) {
769                 event = pupmenu_col(str, 20);
770                 MEM_freeN(str);
771                 
772                 for (cur = 1, act=G.main->action.first; act; act=act->id.next, cur++){
773                         if (cur==event){
774                                 break;
775                         }
776                 }
777                 
778                 if(act) {
779                         if(strip->act) strip->act->id.us--;
780                         strip->act = act;
781                         id_us_plus(&act->id);
782                         
783                         allqueue(REDRAWVIEW3D, 0);
784                         allqueue(REDRAWACTION, 0);
785                         allqueue(REDRAWNLA, 0);
786                 }
787         }
788 }
789
790
791
792 /* Left hand side of channels display, selects objects */
793 static void mouse_nlachannels(short mval[2])
794 {
795         bActionStrip *strip= NULL;
796         Base    *base;
797         Object *ob=NULL;
798         float   x,y;
799         int             click, obclick=0, actclick=0;
800         int             wsize;
801         
802         wsize = (count_nla_levels ()*(NLACHANNELHEIGHT+NLACHANNELSKIP));
803         wsize+= NLACHANNELHEIGHT/2;
804
805         areamouseco_to_ipoco(G.v2d, mval, &x, &y);
806         click = (int)floor( ((float)wsize - y) / (NLACHANNELHEIGHT+NLACHANNELSKIP));
807         
808         if (click<0)
809                 return;
810
811         for (base = G.scene->base.first; base; base=base->next){
812                 if (nla_filter(base)) {
813                         ob= base->object;
814                         
815                         /* See if this is a base selected */
816                         if (click==0) {
817                                 obclick= 1;
818                                 break;
819                         }
820                         click--;
821                         
822                         /* see if any strips under object */
823                         if ((ob->nlaflag & OB_NLA_COLLAPSED)==0) {
824                                 /* See if this is an action */
825                                 if (ob->action){
826                                         if (click==0) {
827                                                 actclick= 1;
828                                                 break;
829                                         }
830                                         click--;
831                                 }
832
833                                 /* See if this is an nla strip */
834                                 if(ob->nlastrips.first) {
835                                         for (strip = ob->nlastrips.first; strip; strip=strip->next){
836                                                 if (click==0) break;
837                                                 click--;                                
838                                         }
839                                         if (strip && click==0) break;
840                                 }
841                         }
842                 }
843         }
844
845         if (!base)
846                 return;
847
848         /* Handle object strip selection */
849         if (G.qual & LR_SHIFTKEY) {
850                 if (base->flag & SELECT) base->flag &= ~SELECT;
851                 else base->flag |= SELECT;
852         }
853         else {
854                 deselect_nlachannels (0);       // Auto clear
855                 base->flag |= SELECT;
856         }
857         ob->flag= base->flag;
858         
859         if(base!=BASACT) set_active_base(base);
860         
861         if(actclick) /* de-activate all strips */
862                 set_active_strip(ob, NULL);
863         else if(strip) {
864                 if(mval[0] >= (NLAWIDTH-16)) /* toggle strip muting */
865                         strip->flag ^= ACTSTRIP_MUTE;
866                 else /* set action */
867                         set_active_strip(ob, strip);
868         }
869
870         /* icon toggles beside strip */
871         if (obclick && mval[0]<20) {
872                 /* collapse option for NLA object strip */
873                 ob->nlaflag ^= OB_NLA_COLLAPSED;
874         }
875         else if(obclick && mval[0]<36) {
876                 /* override option for NLA */
877                 ob->nlaflag ^= OB_NLA_OVERRIDE;
878         }
879         else if((obclick) && (ob->ipo) && (mval[0] >= (NLAWIDTH-16))) {
880                 /* mute Object IPO-block */
881                 ob->ipo->muteipo = (ob->ipo->muteipo)? 0: 1;
882         }
883         
884         ob->ctime= -1234567.0f; // eveil! 
885         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB|OB_RECALC_DATA);
886
887         allqueue(REDRAWIPO, 0);
888         allqueue(REDRAWVIEW3D, 0);
889         allqueue(REDRAWACTION, 0);
890         allqueue(REDRAWNLA, 0);
891         
892 }
893
894 void deselect_nlachannel_keys (int test)
895 {
896         Base                    *base;
897         int                             sel=1;
898         bActionChannel  *chan;
899         bActionStrip    *strip;
900         bConstraintChannel *conchan;
901         
902         /* Determine if this is selection or deselection */
903         if (test){
904                 for (base=G.scene->base.first; base && sel; base=base->next){           
905                         /* Test object ipos */
906                         if (is_ipo_key_selected(base->object->ipo)){
907                                 sel = 0;
908                                 break;
909                         }
910                         
911                         /* Test object constraint ipos */
912                         if (sel){
913                                 for (conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next){
914                                         if (is_ipo_key_selected(conchan->ipo)){
915                                                 sel=0;
916                                                 break;
917                                         }
918                                 }
919                         }
920                         
921                         /* check if collapsed */
922                         if (base->object->nlaflag & OB_NLA_COLLAPSED)
923                                 continue;
924                         
925                         /* Test action ipos */
926                         if (sel){
927                                 if (base->object->action){
928                                         for (chan=base->object->action->chanbase.first; chan; chan=chan->next){
929                                                 if (is_ipo_key_selected(chan->ipo)){
930                                                         sel=0;
931                                                         break;
932                                                 }
933
934                                                 /* Test action constraints */
935                                                 if (sel){
936                                                         for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
937                                                                 if (is_ipo_key_selected(conchan->ipo)){
938                                                                         sel=0;
939                                                                         break;
940                                                                 }
941                                                         }
942                                                 }
943                                         }
944                                 }
945                         }
946                         
947                         /* Test NLA strips */
948                         if (sel){
949                                 for (strip=base->object->nlastrips.first; strip; strip=strip->next){
950                                         if (strip->flag & ACTSTRIP_SELECT){
951                                                 sel = 0;
952                                                 break;
953                                         }
954                                 }
955                         }
956                 }
957         }
958         else
959                 sel=0;
960         
961         
962         /* Set the flags */
963         for (base=G.scene->base.first; base; base=base->next){          
964                 /* Set the object ipos */
965                 set_ipo_key_selection(base->object->ipo, sel);
966                 
967                 /* Set the object constraint ipos */
968                 for (conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next){
969                         set_ipo_key_selection(conchan->ipo, sel);                       
970                 }
971
972                 /* check if collapsed */
973                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
974                         continue;
975                 
976                 /* Set the action ipos */
977                 if (base->object->action){
978                         for (chan=base->object->action->chanbase.first; chan; chan=chan->next){
979                                 set_ipo_key_selection(chan->ipo, sel);
980                                 /* Set the action constraint ipos */
981                                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
982                                         set_ipo_key_selection(conchan->ipo, sel);
983                         }
984                 }
985                 
986                 /* Set the nlastrips */
987                 for (strip=base->object->nlastrips.first; strip; strip=strip->next){
988                         if (sel)
989                                 strip->flag |= ACTSTRIP_SELECT;
990                         else
991                                 strip->flag &= ~ACTSTRIP_SELECT;
992                 }
993         }
994 }
995
996 /* very bad call! */
997 static void recalc_all_ipos(void)
998 {
999         Ipo *ipo;
1000         IpoCurve *icu;
1001         
1002         /* Go to each ipo */
1003         for (ipo=G.main->ipo.first; ipo; ipo=ipo->id.next){
1004                 for (icu = ipo->curve.first; icu; icu=icu->next){
1005                         sort_time_ipocurve(icu);
1006                         testhandles_ipocurve(icu);
1007                 }
1008         }
1009 }
1010
1011 void transform_nlachannel_keys(int mode, int dummy)
1012 {
1013         switch (mode) {
1014                 case 'g':
1015                 {
1016                         initTransform(TFM_TIME_TRANSLATE, CTX_NONE);
1017                         Transform();
1018                 }
1019                         break;
1020                 case 's':
1021                 {
1022                         initTransform(TFM_TIME_SCALE, CTX_NONE);
1023                         Transform();
1024                 }
1025                         break;
1026                 case 'e':
1027                 {
1028                         initTransform(TFM_TIME_EXTEND, CTX_NONE);
1029                         Transform();
1030                 }
1031                         break;
1032         }
1033 }
1034
1035 void delete_nlachannel_keys(void)
1036 {
1037         Base *base;
1038         bActionChannel *chan;
1039         bConstraintChannel *conchan;
1040         bActionStrip *strip, *nextstrip;
1041                 
1042         for (base = G.scene->base.first; base; base=base->next){
1043                 /* Delete object ipos */
1044                 delete_ipo_keys(base->object->ipo);
1045                 
1046                 /* Delete object constraint keys */
1047                 for(conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next)
1048                         delete_ipo_keys(conchan->ipo);
1049
1050                 /* skip actions and nlastrips if object collapsed */
1051                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
1052                         continue;
1053                         
1054                 /* Delete NLA strips */
1055                 for (strip = base->object->nlastrips.first; strip; strip=nextstrip){
1056                         nextstrip=strip->next;
1057                         if (strip->flag & ACTSTRIP_SELECT){
1058                                 free_actionstrip(strip);
1059                                 BLI_remlink(&base->object->nlastrips, strip);
1060                                 MEM_freeN(strip);
1061                         }
1062                 }
1063                 
1064                 /* Delete action ipos */
1065                 if (base->object->action){
1066                         for (chan=base->object->action->chanbase.first; chan; chan=chan->next){
1067                                 if (EDITABLE_ACHAN(chan))
1068                                         delete_ipo_keys(chan->ipo);
1069                                         
1070                                 /* Delete action constraint keys */
1071                                 for(conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
1072                                         if (EDITABLE_CONCHAN(conchan))
1073                                                 delete_ipo_keys(conchan->ipo);
1074                                 }
1075                         }
1076                 }
1077         }
1078         
1079         recalc_all_ipos();      // bad
1080         synchronize_action_strips();
1081         
1082         BIF_undo_push("Delete NLA keys");
1083         allspace(REMAKEIPO,0);
1084         allqueue (REDRAWVIEW3D, 0);
1085         allqueue(REDRAWNLA, 0);
1086         allqueue(REDRAWIPO, 0);
1087 }
1088
1089 void duplicate_nlachannel_keys(void)
1090 {
1091         Base *base;
1092         bActionChannel *chan;
1093         bConstraintChannel *conchan;
1094         bActionStrip *strip, *laststrip;
1095         
1096         /* Find selected items */
1097         for (base = G.scene->base.first; base; base=base->next){        
1098                 /* Duplicate object keys */
1099                 duplicate_ipo_keys(base->object->ipo);
1100                 
1101                 /* Duplicate object constraint keys */
1102                 for(conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next)
1103                         duplicate_ipo_keys(conchan->ipo);
1104                 
1105                 /* skip actions and nlastrips if object collapsed */
1106                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
1107                         continue;
1108                         
1109                 /* Duplicate nla strips */
1110                 laststrip = base->object->nlastrips.last;
1111                 for (strip=base->object->nlastrips.first; strip; strip=strip->next){
1112                         if (strip->flag & ACTSTRIP_SELECT){
1113                                 bActionStrip *newstrip;
1114                                 
1115                                 copy_actionstrip(&newstrip, &strip);
1116                                 
1117                                 BLI_addtail(&base->object->nlastrips, newstrip);
1118                                 
1119                                 strip->flag &= ~ACTSTRIP_SELECT;
1120                                 newstrip->flag |= ACTSTRIP_SELECT;
1121                                 set_active_strip(base->object, newstrip);
1122
1123                         }
1124                         if (strip==laststrip)
1125                                 break;
1126                 }
1127                 
1128                 /* Duplicate actionchannel keys */
1129                 if (base->object->action){
1130                         for (chan=base->object->action->chanbase.first; chan; chan=chan->next){
1131                                 if (EDITABLE_ACHAN(chan))
1132                                         duplicate_ipo_keys(chan->ipo);
1133                                         
1134                                 /* Duplicate action constraint keys */
1135                                 for(conchan=chan->constraintChannels.first; conchan; conchan=conchan->next) {
1136                                         if (EDITABLE_CONCHAN(conchan))
1137                                                 duplicate_ipo_keys(conchan->ipo);
1138                                 }
1139                         }
1140                 }
1141         }
1142         
1143         BIF_undo_push("Duplicate NLA");
1144         transform_nlachannel_keys ('g', 0);
1145 }
1146
1147 void borderselect_nla(void)
1148
1149         Base *base;
1150         rcti rect;
1151         rctf rectf;
1152         int  val, selectmode;
1153         short   mval[2];
1154         float   ymin, ymax;
1155         bActionStrip *strip;
1156         bConstraintChannel *conchan;
1157         
1158         if ( (val = get_border (&rect, 3)) ){
1159             if (val == LEFTMOUSE)
1160                         selectmode = SELECT_ADD;
1161             else
1162                         selectmode = SELECT_SUBTRACT;
1163           
1164                 mval[0]= rect.xmin;
1165                 mval[1]= rect.ymin+2;
1166                 areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
1167                 mval[0]= rect.xmax;
1168                 mval[1]= rect.ymax-2;
1169                 areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
1170                 
1171                 ymax = count_nla_levels();
1172                 ymax*= (NLACHANNELHEIGHT+NLACHANNELSKIP);
1173                 ymax+= (NLACHANNELHEIGHT+NLACHANNELSKIP)/2;
1174         
1175                 for (base=G.scene->base.first; base; base=base->next){
1176                         if (nla_filter(base)) {
1177                                 
1178                                 ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1179                                 
1180                                 /* Check object ipos */
1181                                 if (base->object->ipo){
1182                                         if (!((ymax < rectf.ymin) || (ymin > rectf.ymax)))
1183                                                 borderselect_ipo_key(base->object->ipo, rectf.xmin, rectf.xmax,
1184                                  selectmode);
1185                                 }
1186                                 /* Check object constraint ipos */
1187                                 for (conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next){
1188                                         if (!((ymax < rectf.ymin) || (ymin > rectf.ymax)))
1189                                                 borderselect_ipo_key(conchan->ipo, rectf.xmin, rectf.xmax,
1190                                  selectmode);
1191                                 }
1192                                 
1193                                 ymax=ymin;
1194                                 
1195                                 /* skip actions and nlastrips if object collapsed */
1196                                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
1197                                         continue;
1198                                 
1199                                 /* Check action ipos */
1200                                 if (base->object->action){
1201                                         bActionChannel *chan;
1202                                         float xmin, xmax;
1203                                         
1204                                         ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1205                                         
1206                                         /* if action is mapped in NLA, it returns a correction */
1207                                         xmin= get_action_frame(base->object, rectf.xmin);
1208                                         xmax= get_action_frame(base->object, rectf.xmax);
1209                                         
1210                                         if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
1211                                                 for (chan=base->object->action->chanbase.first; chan; chan=chan->next){
1212                                                         borderselect_ipo_key(chan->ipo, xmin, xmax, selectmode);
1213                                                         /* Check action constraint ipos */
1214                                                         for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
1215                                                                 borderselect_ipo_key(conchan->ipo, xmin, xmax, selectmode);
1216                                                 }
1217                                         }
1218
1219                                         ymax=ymin;
1220                                 }       /* End of if action */
1221                                 
1222                                 /* Skip nlastrips */
1223                                 for (strip=base->object->nlastrips.first; strip; strip=strip->next){
1224                                         ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1225                                         //
1226                                         if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
1227                                                 if (!((rectf.xmax<strip->start) || (rectf.xmin>strip->end))){
1228                                                         if (val==1)
1229                                                                 strip->flag |= ACTSTRIP_SELECT;
1230                                                         else
1231                                                                 strip->flag &= ~ACTSTRIP_SELECT;
1232                                                 }
1233                                         }
1234                                         
1235                                         ymax=ymin;
1236                                 }
1237                         }
1238                 }       
1239                 BIF_undo_push("Border select NLA");
1240                 allqueue(REDRAWMARKER, 0);
1241         }
1242 }
1243
1244 /* right hand side of window, does ipokeys, actionkeys or strips */
1245 static void mouse_nla(int selectmode)
1246 {
1247         Base *base;
1248         bAction *act;
1249         bActionChannel *chan;
1250         bActionStrip *rstrip;
1251         bConstraintChannel *conchan;
1252         TimeMarker *marker;
1253         float   selx;
1254         short   mval[2];
1255         short sel, isdone=0;
1256         
1257         getmouseco_areawin (mval);
1258         
1259         /* Try object ipo or ob-constraint ipo selection */
1260         base= get_nearest_nlachannel_ob_key(&selx, &sel);
1261         marker=find_nearest_marker(SCE_MARKERS, 1);
1262         if (base) {
1263                 isdone= 1;
1264                 
1265                 if (selectmode == SELECT_REPLACE){
1266                         deselect_nlachannel_keys(0);
1267                         selectmode = SELECT_ADD;
1268                 }
1269                 
1270                 select_ipo_key(base->object->ipo, selx, selectmode);
1271                 
1272                 /* Try object constraint selection */
1273                 for (conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next)
1274                         select_ipo_key(conchan->ipo, selx, selectmode);
1275         }
1276         else if (marker) {
1277                 /* marker */            
1278                 if (selectmode == SELECT_REPLACE) {                     
1279                         deselect_markers(0, 0);
1280                         marker->flag |= SELECT;
1281                 }
1282                 else if (selectmode == SELECT_INVERT) {
1283                         if (marker->flag & SELECT)
1284                                 marker->flag &= ~SELECT;
1285                         else
1286                                 marker->flag |= SELECT;
1287                 }
1288                 else if (selectmode == SELECT_ADD) 
1289                         marker->flag |= SELECT;
1290                 else if (selectmode == SELECT_SUBTRACT)
1291                         marker->flag &= ~SELECT;
1292                 
1293                 std_rmouse_transform(transform_markers);
1294                 
1295                 allqueue(REDRAWMARKER, 0);
1296         }
1297         else {
1298                 /* Try action ipo selection */
1299                 act= get_nearest_nlachannel_ac_key(&selx, &sel);
1300                 if (act) {
1301                         isdone= 1;
1302                         
1303                         if (selectmode == SELECT_REPLACE){
1304                                 deselect_nlachannel_keys(0);
1305                                 selectmode = SELECT_ADD;
1306                         }
1307                         
1308                         for (chan=act->chanbase.first; chan; chan=chan->next) {
1309                                 select_ipo_key(chan->ipo, selx, selectmode);
1310                                 /* Try action constraint selection */
1311                                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
1312                                         select_ipo_key(conchan->ipo, selx, selectmode);
1313                         }
1314                 }
1315                 else {
1316         
1317                         /* Try nla strip selection */
1318                         base= get_nearest_nlastrip(&rstrip, &sel);
1319                         if (base){
1320                                 isdone= 1;
1321                                 
1322                                 if (!(G.qual & LR_SHIFTKEY)){
1323                                         deselect_nlachannel_keys(0);
1324                                         sel = 0;
1325                                 }
1326                                 
1327                                 if (sel)
1328                                         rstrip->flag &= ~ACTSTRIP_SELECT;
1329                                 else
1330                                         rstrip->flag |= ACTSTRIP_SELECT;
1331                                 
1332                                 set_active_strip(base->object, rstrip);
1333                                 
1334                                 if(base!=BASACT) set_active_base(base);
1335                         }
1336                 }
1337         }
1338         
1339         if(isdone) {
1340                 std_rmouse_transform(transform_nlachannel_keys);
1341                 
1342                 allqueue(REDRAWIPO, 0);
1343                 allqueue(REDRAWVIEW3D, 0);
1344                 allqueue(REDRAWNLA, 0);
1345         }
1346 }
1347
1348 /* This function is currently more complicated than it seems like it should be.
1349 * However, this will be needed once the nla strip timeline is more complex */
1350 static Base *get_nearest_nlastrip (bActionStrip **rstrip, short *sel)
1351 {
1352         Base *base, *firstbase=NULL;
1353         bActionStrip *strip, *firststrip=NULL, *foundstrip=NULL;
1354         rctf    rectf;
1355         float ymin, ymax;
1356         short mval[2];
1357         short foundsel = 0;
1358
1359         getmouseco_areawin (mval);
1360         
1361         mval[0]-=7;
1362         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
1363         
1364         mval[0]+=14;
1365         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
1366         
1367         ymax = count_nla_levels();
1368         ymax*=(NLACHANNELHEIGHT + NLACHANNELSKIP);
1369         ymax+= NLACHANNELHEIGHT/2;
1370         
1371         for (base = G.scene->base.first; base; base=base->next){
1372                 if (nla_filter(base)) {
1373                         
1374                         /* Skip object ipos */
1375                         ymax-=(NLACHANNELHEIGHT+NLACHANNELSKIP);
1376                         
1377                         /* check if skip strips if collapsed */
1378                         if (base->object->nlaflag & OB_NLA_COLLAPSED)
1379                                 continue;
1380
1381                         /* Skip action ipos */
1382                         if (base->object->action)
1383                                 ymax-=(NLACHANNELHEIGHT+NLACHANNELSKIP);
1384                         
1385                         /* the strips */
1386                         for (strip=base->object->nlastrips.first; strip; strip=strip->next){
1387                                 ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1388                                 /* Do Ytest */
1389                                 if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
1390                                         /* Do XTest */
1391                                         if (!((rectf.xmax<strip->start) || (rectf.xmin>strip->end))){
1392                                                 if (!firstbase){
1393                                                         firstbase=base;
1394                                                         firststrip=strip;
1395                                                         *sel = strip->flag & ACTSTRIP_SELECT;
1396                                                 }
1397                                                 
1398                                                 if (strip->flag & ACTSTRIP_SELECT){ 
1399                                                         if (!foundsel){
1400                                                                 foundsel=1;
1401                                                                 foundstrip = strip;
1402                                                         }
1403                                                 }
1404                                                 else if (foundsel && strip != foundstrip){
1405                                                         *rstrip=strip;
1406                                                         *sel = 0;
1407                                                         return base;
1408                                                 }
1409                                         }
1410                                 }
1411                                 ymax=ymin;
1412                         }
1413                 }
1414         }
1415         *rstrip=firststrip;
1416         return firstbase;
1417 }
1418
1419 static Base *get_nearest_nlachannel_ob_key (float *index, short *sel)
1420 {
1421         Base *base;
1422         IpoCurve *icu;
1423         Base *firstbase=NULL;
1424         bConstraintChannel *conchan;
1425         int     foundsel=0;
1426         float firstvertx=-1, foundx=-1;
1427         int i;
1428         short mval[2];
1429         float ymin, ymax;
1430         rctf    rectf;
1431         
1432         *index=0;
1433         
1434         getmouseco_areawin (mval);
1435         
1436         mval[0]-=7;
1437         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
1438         
1439         mval[0]+=14;
1440         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
1441         
1442         ymax = count_nla_levels();
1443         
1444         ymax*= (NLACHANNELHEIGHT + NLACHANNELSKIP);
1445         ymax+= NLACHANNELHEIGHT/2;
1446         
1447         *sel=0;
1448         
1449         for (base=G.scene->base.first; base; base=base->next){
1450                 if (nla_filter(base)) {
1451                         
1452                         ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1453                         
1454                         /* Handle object ipo selection */
1455                         if (base->object->ipo){
1456                                 if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
1457                                         for (icu=base->object->ipo->curve.first; icu; icu=icu->next){
1458                                                 for (i=0; i<icu->totvert; i++){
1459                                                         if (icu->bezt[i].vec[1][0] > rectf.xmin && icu->bezt[i].vec[1][0] <= rectf.xmax ){
1460                                                                 if (!firstbase){
1461                                                                         firstbase=base;
1462                                                                         firstvertx=icu->bezt[i].vec[1][0];
1463                                                                         *sel = icu->bezt[i].f2 & 1;     
1464                                                                 }
1465                                                                 
1466                                                                 if (icu->bezt[i].f2 & 1){ 
1467                                                                         if (!foundsel){
1468                                                                                 foundsel=1;
1469                                                                                 foundx = icu->bezt[i].vec[1][0];
1470                                                                         }
1471                                                                 }
1472                                                                 else if (foundsel && icu->bezt[i].vec[1][0] != foundx){
1473                                                                         *index=icu->bezt[i].vec[1][0];
1474                                                                         *sel = 0;
1475                                                                         return base;
1476                                                                 }
1477                                                         }
1478                                                 }
1479                                         }
1480                                 }
1481                         }
1482                         /* Handle object constraint ipos */
1483                         for (conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next){
1484                                 if ( !((ymax < rectf.ymin) || (ymin > rectf.ymax)) && conchan->ipo){
1485                                         for (icu=conchan->ipo->curve.first; icu; icu=icu->next){
1486                                                 for (i=0; i<icu->totvert; i++){
1487                                                         if (icu->bezt[i].vec[1][0] > rectf.xmin && icu->bezt[i].vec[1][0] <= rectf.xmax ){
1488                                                                 if (!firstbase){
1489                                                                         firstbase=base;
1490                                                                         firstvertx=icu->bezt[i].vec[1][0];
1491                                                                         *sel = icu->bezt[i].f2 & 1;     
1492                                                                 }
1493                                                                 
1494                                                                 if (icu->bezt[i].f2 & 1){ 
1495                                                                         if (!foundsel){
1496                                                                                 foundsel=1;
1497                                                                                 foundx = icu->bezt[i].vec[1][0];
1498                                                                         }
1499                                                                 }
1500                                                                 else if (foundsel && icu->bezt[i].vec[1][0] != foundx){
1501                                                                         *index=icu->bezt[i].vec[1][0];
1502                                                                         *sel = 0;
1503                                                                         return base;
1504                                                                 }
1505                                                         }
1506                                                 }
1507                                         }
1508                                 }
1509                         }
1510
1511                         ymax=ymin;
1512                         
1513                         /* Skip actions and nlastrips if object is collapsed */
1514                         if (base->object->nlaflag & OB_NLA_COLLAPSED)
1515                                 continue;
1516                         
1517                         /* Skip action ipos */
1518                         if (base->object->action){
1519                                 ymax-=(NLACHANNELHEIGHT+NLACHANNELSKIP);
1520                         }
1521                         /* Skip nlastrips */
1522                         ymax-=(NLACHANNELHEIGHT+NLACHANNELSKIP)*BLI_countlist(&base->object->nlastrips);
1523                 }
1524         }       
1525         
1526         *index=firstvertx;
1527         return firstbase;
1528 }
1529
1530 static bAction *get_nearest_nlachannel_ac_key (float *index, short *sel)
1531 {
1532         Base *base;
1533         IpoCurve *icu;
1534         bAction *firstact=NULL;
1535         bActionChannel *chan;
1536         bConstraintChannel *conchan;
1537         rctf    rectf;
1538         float firstvert=-1, foundx=-1;
1539         float ymin, ymax, xmin, xmax;
1540         int i;
1541         int     foundsel=0;
1542         short mval[2];
1543         
1544         *index=0;
1545         
1546         getmouseco_areawin (mval);
1547         
1548         mval[0]-=7;
1549         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
1550         
1551         mval[0]+=14;
1552         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
1553         
1554         ymax = count_nla_levels();
1555         
1556         ymax*= (NLACHANNELHEIGHT + NLACHANNELSKIP);
1557         ymax+= NLACHANNELHEIGHT/2;
1558         
1559         *sel=0;
1560         
1561         for (base=G.scene->base.first; base; base=base->next){
1562                 /* Handle object ipo selection */
1563                 if (nla_filter(base)) {
1564                         
1565                         /* Skip object ipo and ob-constraint ipo */
1566                         ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1567                         
1568                         /* skip this object if it is collapsed */
1569                         if (base->object->nlaflag & OB_NLA_COLLAPSED)
1570                                 continue;
1571                         
1572                         ymax=ymin;
1573                         
1574                         /* Handle action ipos */
1575                         if (base->object->action){
1576                                 bAction *act= base->object->action;
1577                                 
1578                                 /* if action is mapped in NLA, it returns a correction */
1579                                 xmin= get_action_frame(base->object, rectf.xmin);
1580                                 xmax= get_action_frame(base->object, rectf.xmax);
1581                                 
1582                                 ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1583                                 if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
1584                                         for (chan=act->chanbase.first; chan; chan=chan->next){
1585                                                 if(chan->ipo) {
1586                                                         for (icu=chan->ipo->curve.first; icu; icu=icu->next){
1587                                                                 for (i=0; i<icu->totvert; i++){
1588                                                                         if (icu->bezt[i].vec[1][0] > xmin && icu->bezt[i].vec[1][0] <= xmax ){
1589                                                                                 if (!firstact){
1590                                                                                         firstact= act;
1591                                                                                         firstvert=icu->bezt[i].vec[1][0];
1592                                                                                         *sel = icu->bezt[i].f2 & 1;     
1593                                                                                 }
1594                                                                                 
1595                                                                                 if (icu->bezt[i].f2 & 1){ 
1596                                                                                         if (!foundsel){
1597                                                                                                 foundsel=1;
1598                                                                                                 foundx = icu->bezt[i].vec[1][0];
1599                                                                                         }
1600                                                                                 }
1601                                                                                 else if (foundsel && icu->bezt[i].vec[1][0] != foundx){
1602                                                                                         *index=icu->bezt[i].vec[1][0];
1603                                                                                         *sel = 0;
1604                                                                                         return act;
1605                                                                                 }
1606                                                                         }
1607                                                                 }
1608                                                         }
1609                                                 }
1610                                                 
1611                                                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
1612                                                         ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1613                                                         if ( !((ymax < rectf.ymin) || (ymin > rectf.ymax)) && conchan->ipo){
1614                                                                 for (icu=conchan->ipo->curve.first; icu; icu=icu->next){
1615                                                                         for (i=0; i<icu->totvert; i++){
1616                                                                                 if (icu->bezt[i].vec[1][0] > xmin && icu->bezt[i].vec[1][0] <= xmax ){
1617                                                                                         if (!firstact){
1618                                                                                                 firstact=base->object->action;
1619                                                                                                 firstvert=icu->bezt[i].vec[1][0];
1620                                                                                                 *sel = icu->bezt[i].f2 & 1;     
1621                                                                                         }
1622                                                                                         
1623                                                                                         if (icu->bezt[i].f2 & 1){ 
1624                                                                                                 if (!foundsel){
1625                                                                                                         foundsel=1;
1626                                                                                                         foundx = icu->bezt[i].vec[1][0];
1627                                                                                                 }
1628                                                                                         }
1629                                                                                         else if (foundsel && icu->bezt[i].vec[1][0] != foundx){
1630                                                                                                 *index=icu->bezt[i].vec[1][0];
1631                                                                                                 *sel = 0;
1632                                                                                                 return base->object->action;
1633                                                                                         }
1634                                                                                 }
1635                                                                         }
1636                                                                 }
1637                                                         }
1638                                                         ymax=ymin;
1639                                                 }
1640                                         
1641                                         
1642                                         }
1643                                 }                       
1644                                 ymax=ymin;
1645                         }
1646                         
1647                         /* Skip nlastrips */
1648                         ymax-=(NLACHANNELHEIGHT+NLACHANNELSKIP)*BLI_countlist(&base->object->nlastrips);
1649                 }
1650         }       
1651         
1652         *index=firstvert;
1653         return firstact;
1654 }
1655
1656 void deselect_nlachannels(int test)
1657 {
1658         Base *base;
1659         int sel = 1;
1660
1661         if (test){
1662                 for (base=G.scene->base.first; base; base=base->next){
1663                         /* Check base flags for previous selection */
1664                         if (base->flag & SELECT){
1665                                 sel=0;
1666                                 break;
1667                         }
1668                 }
1669         }
1670         else
1671                 sel = 0;
1672
1673         /* Select objects */
1674         for (base=G.scene->base.first; base; base=base->next){
1675                 if (sel){
1676                         if (nla_filter(base))
1677                                 base->flag |= SELECT;
1678                 }
1679                 else
1680                         base->flag &= ~SELECT;
1681                 
1682                 base->object->flag= base->flag;
1683         }       
1684 }
1685
1686 static Object *get_object_from_active_strip(void) {
1687
1688         Base *base;
1689         bActionStrip *strip;
1690         
1691         for (base=G.scene->base.first; base; base=base->next) {
1692                 if ((base->object->nlaflag & OB_NLA_COLLAPSED)==0) {
1693                         for (strip = base->object->nlastrips.first; 
1694                                  strip; strip=strip->next){
1695                                 if (strip->flag & ACTSTRIP_SELECT) {
1696                                         return base->object;
1697                                 }
1698                         }
1699                 }
1700         }
1701         return NULL;
1702 }
1703
1704
1705 void winqreadnlaspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
1706 {
1707         unsigned short event= evt->event;
1708         short val= evt->val;
1709         SpaceNla *snla = curarea->spacedata.first;
1710         int doredraw= 0;
1711         short   mval[2];
1712         float dx,dy;
1713         int     cfra;
1714         short mousebut = L_MOUSE;
1715         Object          *ob; //in shift-B / bake
1716         
1717         if (curarea->win==0) return;
1718         if (!snla) return;
1719         
1720         if(val) {
1721                 if( uiDoBlocks(&curarea->uiblocks, event, 1)!=UI_NOTHING ) event= 0;
1722                 
1723                 /* swap mouse buttons based on user preference */
1724                 if (U.flag & USER_LMOUSESELECT) {
1725                         if (event == LEFTMOUSE) {
1726                                 event = RIGHTMOUSE;
1727                                 mousebut = L_MOUSE;
1728                         } else if (event == RIGHTMOUSE) {
1729                                 event = LEFTMOUSE;
1730                                 mousebut = R_MOUSE;
1731                         }
1732                 }
1733                 
1734                 getmouseco_areawin(mval);
1735                 
1736                 switch(event) {
1737                         case UI_BUT_EVENT:
1738                                 do_nlabuts(val); // in drawnla.c
1739                                 break;
1740                                 
1741                         case HOMEKEY:
1742                                 do_nla_buttons(B_NLAHOME);
1743                                 break;
1744                                 
1745                         case EQUALKEY:
1746                                 shift_nlastrips_up();
1747                                 break;
1748                         
1749                         case PAGEUPKEY:
1750                                 if (G.qual & LR_CTRLKEY)
1751                                         shift_nlastrips_up();
1752                                 else {
1753                                         nextprev_marker(1);
1754                                         allqueue(REDRAWMARKER, 0);
1755                                 }                               
1756                                 break;
1757                                 
1758                         case MINUSKEY:
1759                                 shift_nlastrips_down();
1760                                 break;
1761                                 
1762                         case PAGEDOWNKEY:
1763                                 if (G.qual & LR_CTRLKEY)
1764                                         shift_nlastrips_down();
1765                                 else {
1766                                         nextprev_marker(-1);
1767                                         allqueue(REDRAWMARKER, 0);
1768                                 }
1769                                 break;
1770                                 
1771                         case AKEY:
1772                                 if (G.qual & LR_SHIFTKEY){
1773                                         add_nlablock();
1774                                         allqueue (REDRAWNLA, 0);
1775                                         allqueue (REDRAWVIEW3D, 0);
1776                                 }
1777                                 else if (G.qual & LR_CTRLKEY) {
1778                                         deselect_markers(1, 0);
1779                                         allqueue(REDRAWMARKER, 0);
1780                                 }
1781                                 else{
1782                                         if (mval[0]>=NLAWIDTH)
1783                                                 deselect_nlachannel_keys(1);
1784                                         else{
1785                                                 deselect_nlachannels(1);
1786                                                 allqueue (REDRAWVIEW3D, 0);
1787                                         }
1788                                         allqueue (REDRAWNLA, 0);
1789                                         allqueue (REDRAWIPO, 0);
1790                                         BIF_undo_push("(De)select all NLA");
1791                                 }
1792                                 break;
1793                                 
1794                         case BKEY:
1795                                 if (G.qual & LR_SHIFTKEY){
1796                                         bake_all_to_action();
1797                                         allqueue (REDRAWNLA, 0);
1798                                     allqueue (REDRAWVIEW3D, 0);
1799                                     BIF_undo_push("Bake All To Action");
1800                                     ob = get_object_from_active_strip();
1801                                     //build_match_caches(ob);
1802                                 }
1803                                 else if (G.qual & LR_CTRLKEY) 
1804                                         borderselect_markers();
1805                                 else
1806                                         borderselect_nla();
1807                                 break;
1808                                 
1809                         case CKEY:
1810                                 if(G.qual==LR_CTRLKEY) {
1811                                         if(okee("Copy Modifiers"))
1812                                                 copy_action_modifiers();
1813                                 }
1814                                 else convert_nla();
1815                                 break;
1816                                 
1817                         case DKEY:
1818                                 if (G.qual == (LR_CTRLKEY|LR_SHIFTKEY) && mval[0]>=NLAWIDTH) {
1819                                         duplicate_marker();
1820                                 }
1821                                 else if (G.qual & LR_SHIFTKEY && mval[0]>=NLAWIDTH){
1822                                         duplicate_nlachannel_keys();
1823                                         update_for_newframe_muted();
1824                                 }
1825                                 
1826                                 break;
1827                                 
1828                         case EKEY:
1829                                 if (mval[0] >= NLAWIDTH) {
1830                                         transform_nlachannel_keys ('e', 0);
1831                                         update_for_newframe_muted();
1832                                 }
1833                                 break;
1834                                 
1835                         case GKEY:
1836                                 if (mval[0]>=NLAWIDTH) {
1837                                         if (G.qual & LR_CTRLKEY) {
1838                                                 transform_markers('g', 0);
1839                                         }
1840                                         else {
1841                                                 transform_nlachannel_keys ('g', 0);
1842                                                 update_for_newframe_muted();
1843                                         }
1844                                 }
1845                                 break;
1846                         
1847                         case MKEY:
1848                                 /* marker operations */
1849                                 if (G.qual == 0)
1850                                         add_marker(CFRA);
1851                                 else if (G.qual == LR_CTRLKEY)
1852                                         rename_marker();
1853                                 else 
1854                                         break;
1855                                 allqueue(REDRAWMARKER, 0);
1856                                 break;                          
1857                         
1858                         case NKEY:
1859                                 if(G.qual==0) {
1860                                         toggle_blockhandler(curarea, NLA_HANDLER_PROPERTIES, UI_PNL_TO_MOUSE);
1861                                         scrarea_queue_winredraw(curarea);
1862                                 }
1863                                 else if (G.qual & LR_SHIFTKEY) {
1864                                         add_empty_nlablock();
1865                                 }
1866                                 break;
1867                         case LKEY:
1868                                 relink_active_strip();
1869                                 break;
1870                                 
1871                         case PKEY:
1872                                 if (G.qual & LR_CTRLKEY) /* set preview range */
1873                                         anim_previewrange_set();
1874                                 else if (G.qual & LR_ALTKEY) /* clear preview range */
1875                                         anim_previewrange_clear();
1876                                 allqueue(REDRAWMARKER, 0);
1877                                 break;
1878                                 
1879                         case SKEY:
1880                                 if (G.qual==LR_ALTKEY) {
1881                                         val= pupmenu("Action Strip Scale%t|Reset Strip Scale%x1|Remap Action Start/End%x2|Apply Scale%x3");
1882                                         if (val > 0)
1883                                                 reset_action_strips(val);
1884                                 }
1885                                 else if (G.qual & LR_SHIFTKEY) {
1886                                         if (snla->flag & SNLA_DRAWTIME)
1887                                                 val= pupmenu("Snap To%t|Nearest Second%x3|Current Time%x2");
1888                                         else
1889                                                 val= pupmenu("Snap To%t|Nearest Frame%x1|Current Frame%x2");
1890                                         if (ELEM3(val, 1, 2, 3))
1891                                                 snap_action_strips(val);
1892                                 }
1893                                 else {
1894                                         if (mval[0]>=NLAWIDTH)
1895                                                 transform_nlachannel_keys ('s', 0);
1896                                         update_for_newframe_muted();
1897                                 }
1898                                 break;
1899                                 
1900                         case TKEY:
1901                                 if (G.qual & LR_CTRLKEY) {
1902                                         val= pupmenu("Time value%t|Frames %x1|Seconds%x2");
1903                                         
1904                                         if (val > 0) {
1905                                                 if (val == 2) snla->flag |= SNLA_DRAWTIME;
1906                                                 else snla->flag &= ~SNLA_DRAWTIME;
1907                                                 
1908                                                 doredraw= 1;
1909                                         }
1910                                 }                               
1911                                 break;
1912                                 
1913                         case DELKEY:
1914                         case XKEY:
1915                                 if (mval[0]>=NLAWIDTH) {
1916                                         if (okee("Erase selected?")) {
1917                                                 delete_nlachannel_keys();
1918                                                 update_for_newframe_muted();
1919                                                 
1920                                                 remove_marker();
1921                                                 
1922                                                 allqueue(REDRAWMARKER, 0);
1923                                         }
1924                                 }
1925                                 break;
1926                                 
1927                                 /* LEFTMOUSE and RIGHTMOUSE event codes can be swapped above,
1928                                 * based on user preference USER_LMOUSESELECT
1929                                 */
1930                         case LEFTMOUSE:
1931                                 if(view2dmove(LEFTMOUSE))
1932                                         break; // only checks for sliders
1933                                 else if (mval[0]>=snla->v2d.mask.xmin) {
1934                                         do {
1935                                                 getmouseco_areawin(mval);
1936                                                 
1937                                                 areamouseco_to_ipoco(G.v2d, mval, &dx, &dy);
1938                                                 
1939                                                 cfra= (int)(dx+0.5f);
1940                                                 if(cfra< 1) cfra= 1;
1941                                                 
1942                                                 if( cfra!=CFRA ) {
1943                                                         CFRA= cfra;
1944                                                         update_for_newframe();
1945                                                         force_draw_all(0);
1946                                                 }
1947                                                 else PIL_sleep_ms(30);
1948                                                 
1949                                         } while(get_mbut() & mousebut);
1950                                         break;
1951                                 }
1952                                         /* else pass on! */
1953                                 case RIGHTMOUSE:
1954                                         if (mval[0]>=snla->v2d.mask.xmin) {
1955                                                 if(G.qual & LR_SHIFTKEY)
1956                                                         mouse_nla(SELECT_INVERT);
1957                                                 else
1958                                                         mouse_nla(SELECT_REPLACE);
1959                                         }
1960                                         else
1961                                                 mouse_nlachannels(mval);
1962                                         break;
1963                                         
1964                                 case PADPLUSKEY:
1965                                         view2d_zoom(G.v2d, 0.1154, sa->winx, sa->winy);
1966                                         test_view2d(G.v2d, sa->winx, sa->winy);
1967                                         view2d_do_locks(curarea, V2D_LOCK_COPY);
1968                                         doredraw= 1;
1969                                         break;
1970                                 case PADMINUS:
1971                                         view2d_zoom(G.v2d, -0.15, sa->winx, sa->winy);
1972                                         test_view2d(G.v2d, sa->winx, sa->winy);
1973                                         view2d_do_locks(curarea, V2D_LOCK_COPY);
1974                                         doredraw= 1;
1975                                         break;
1976                                 case MIDDLEMOUSE:
1977                                 case WHEELUPMOUSE:
1978                                 case WHEELDOWNMOUSE:
1979                                         view2dmove(event);      /* in drawipo.c */
1980                                         break;
1981                 }
1982         }
1983         
1984         if(doredraw) scrarea_queue_winredraw(curarea);
1985 }
1986
1987 void bake_all_to_action(void)
1988 {
1989         Object          *ob;
1990         bAction         *newAction;
1991         Ipo             *ipo;
1992         ID              *id;
1993         short           hold, add;
1994         float           repeat;
1995
1996         /* burn object-level motion into a new action */
1997         ob = get_object_from_active_strip();
1998         if (ob) {
1999                 if (ob->flag&OB_ARMATURE) {
2000                         //newAction = bake_obIPO_to_action(ob);
2001                         newAction = NULL;
2002                         if (newAction) {
2003                                 /* unlink the object's IPO */
2004                                 ipo=ob->ipo;
2005                                 if (ipo) {
2006                                         id = &ipo->id;
2007                                         if (id->us > 0)
2008                                                 id->us--;
2009                                         ob->ipo = NULL;
2010                                 }
2011                                 
2012                                 /* add the new Action to NLA as a strip */
2013                                 hold=1;
2014                                 add=1;
2015                                 repeat=1.0;
2016                                 printf("about to add nla block...\n");
2017                                 add_nla_block_by_name(newAction->id.name, ob, hold, add, repeat);
2018                                 BIF_undo_push("Add NLA strip");
2019                         }
2020                 }
2021         }
2022 }
2023
2024 void copy_action_modifiers(void)
2025 {
2026         bActionStrip *strip, *actstrip;
2027         Object *ob= OBACT;
2028         
2029         if(ob==NULL)
2030                 return;
2031         
2032         /* active strip */
2033         for (actstrip=ob->nlastrips.first; actstrip; actstrip=actstrip->next)
2034                 if(actstrip->flag & ACTSTRIP_ACTIVE)
2035                         break;
2036         if(actstrip==NULL)
2037                 return;
2038         
2039         /* copy to selected items */
2040         for (strip=ob->nlastrips.first; strip; strip=strip->next){
2041                 if (strip->flag & ACTSTRIP_SELECT) {
2042                         if(strip!=actstrip) {
2043                                 if (strip->modifiers.first)
2044                                         BLI_freelistN(&strip->modifiers);
2045                                 if (actstrip->modifiers.first)
2046                                         duplicatelist (&strip->modifiers, &actstrip->modifiers);
2047                         }
2048                 }
2049         }
2050         
2051         BIF_undo_push("Copy Action Modifiers");
2052         allqueue(REDRAWNLA, 0);
2053         DAG_scene_flush_update(G.scene, screen_view3d_layers());
2054 }
2055