9c3191c93d6f7cea93f10ce43ea9c6768c591372
[blender.git] / source / blender / editors / space_sequencer / sequencer_edit.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2003-2009
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <stdlib.h>
29 #include <math.h>
30 #include <string.h>
31
32 #ifndef WIN32
33 #include <unistd.h>
34 #else
35 #include <io.h>
36 #endif
37 #include <sys/types.h>
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_arithb.h"
43 #include "BLI_storage_types.h"
44
45 #include "IMB_imbuf_types.h"
46 #include "IMB_imbuf.h"
47
48 #include "DNA_ipo_types.h"
49 #include "DNA_curve_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_space_types.h"
53 #include "DNA_sequence_types.h"
54 #include "DNA_view2d_types.h"
55 #include "DNA_userdef_types.h"
56 #include "DNA_sound_types.h"
57
58 #include "BKE_context.h"
59 #include "BKE_global.h"
60 #include "BKE_image.h"
61 #include "BKE_library.h"
62 #include "BKE_main.h"
63 #include "BKE_plugin_types.h"
64 #include "BKE_sequence.h"
65 #include "BKE_scene.h"
66 #include "BKE_utildefines.h"
67 #include "BKE_report.h"
68
69 #include "BIF_transform.h"
70
71 #include "WM_api.h"
72 #include "WM_types.h"
73
74 #include "RNA_access.h"
75 #include "RNA_define.h"
76
77 /* for menu/popup icons etc etc*/
78 #include "UI_interface.h"
79 #include "UI_resources.h"
80
81 #include "ED_anim_api.h"
82 #include "ED_space_api.h"
83 #include "ED_types.h"
84 #include "ED_screen.h"
85 #include "ED_util.h"
86
87 #include "UI_interface.h"
88 #include "UI_resources.h"
89 #include "UI_view2d.h"
90
91 /* own include */
92 #include "sequencer_intern.h"
93
94 /* XXX */
95 //static Sequence *_last_seq=0;
96 //static int _last_seq_init=0;
97 /* XXX */
98 static void error() {}
99 static void waitcursor() {}
100 static void activate_fileselect() {}
101 static int pupmenu() {return 0;}
102 static int okee() {return 0;}
103
104
105 /* XXX */
106 /* RNA Enums, used in multiple files */
107 EnumPropertyItem sequencer_prop_effect_types[] = {
108         {SEQ_CROSS, "CROSS", "Crossfade", "Crossfade effect strip type"},
109         {SEQ_ADD, "ADD", "Add", "Add effect strip type"},
110         {SEQ_SUB, "SUBTRACT", "Subtract", "Subtract effect strip type"},
111         {SEQ_ALPHAOVER, "ALPHA_OVER", "Alpha Over", "Alpha Over effect strip type"},
112         {SEQ_ALPHAUNDER, "ALPHA_UNDER", "Alpha Under", "Alpha Under effect strip type"},
113         {SEQ_GAMCROSS, "GAMMA_CROSS", "Gamma Cross", "Gamma Cross effect strip type"},
114         {SEQ_MUL, "MULTIPLY", "Multiply", "Multiply effect strip type"},
115         {SEQ_OVERDROP, "OVER_DROP", "Alpha Over Drop", "Alpha Over Drop effect strip type"},
116         {SEQ_PLUGIN, "PLUGIN", "Plugin", "Plugin effect strip type"},
117         {SEQ_WIPE, "WIPE", "Wipe", "Wipe effect strip type"},
118         {SEQ_GLOW, "GLOW", "Glow", "Glow effect strip type"},
119         {SEQ_TRANSFORM, "TRANSFORM", "Transform", "Transform effect strip type"},
120         {SEQ_COLOR, "COLOR", "Color", "Color effect strip type"},
121         {SEQ_SPEED, "SPEED", "Speed", "Color effect strip type"},
122         {0, NULL, NULL, NULL}
123 };
124
125 /* mute operator */
126 EnumPropertyItem sequencer_prop_operate_types[] = { /* better name? */
127         {SEQ_SELECTED, "SELECTED", "Selected", ""},
128         {SEQ_UNSELECTED, "UNSELECTED", "Unselected ", ""},
129         {0, NULL, NULL, NULL}
130 };
131
132  EnumPropertyItem prop_side_types[] = {
133         {SEQ_SIDE_LEFT, "LEFT", "Left", ""},
134         {SEQ_SIDE_RIGHT, "RIGHT", "Right", ""},
135         {SEQ_SIDE_BOTH, "BOTH", "Both", ""},
136         {0, NULL, NULL, NULL}
137 };
138
139 typedef struct TransSeq {
140         int start, machine;
141         int startstill, endstill;
142         int startdisp, enddisp;
143         int startofs, endofs;
144         int final_left, final_right;
145         int len;
146 } TransSeq;
147
148 Sequence *get_last_seq(Scene *scene)
149 {
150         Editing *ed= seq_give_editing(scene, FALSE);
151         if(ed==NULL) return NULL;
152         return ed->act_seq;
153 }
154
155 void set_last_seq(Scene *scene, Sequence *seq)
156 {
157         Editing *ed= seq_give_editing(scene, FALSE);
158         if(ed==NULL) return;
159         
160         ed->act_seq= seq;
161 }
162
163 Sequence *get_foreground_frame_seq(Scene *scene, int frame)
164 {
165         Editing *ed= seq_give_editing(scene, FALSE);
166         Sequence *seq, *best_seq=NULL;
167         int best_machine = -1;
168         
169         if(!ed) return NULL;
170         
171         for (seq=ed->seqbasep->first; seq; seq= seq->next) {
172                 if(seq->flag & SEQ_MUTE || seq->startdisp > frame || seq->enddisp <= frame)
173                         continue;
174                 /* only use elements you can see - not */
175                 if (ELEM6(seq->type, SEQ_IMAGE, SEQ_META, SEQ_SCENE, SEQ_MOVIE, SEQ_MOVIE_AND_HD_SOUND, SEQ_COLOR)) {
176                         if (seq->machine > best_machine) {
177                                 best_seq = seq;
178                                 best_machine = seq->machine;
179                         }
180                 }
181         }
182         return best_seq;
183 }
184
185 void seq_rectf(Sequence *seq, rctf *rectf)
186 {
187         if(seq->startstill) rectf->xmin= seq->start;
188         else rectf->xmin= seq->startdisp;
189         rectf->ymin= seq->machine+SEQ_STRIP_OFSBOTTOM;
190         if(seq->endstill) rectf->xmax= seq->start+seq->len;
191         else rectf->xmax= seq->enddisp;
192         rectf->ymax= seq->machine+SEQ_STRIP_OFSTOP;
193 }
194
195 static void change_plugin_seq(Scene *scene, char *str)  /* called from fileselect */
196 {
197         Editing *ed= seq_give_editing(scene, FALSE);
198         struct SeqEffectHandle sh;
199         Sequence *last_seq= get_last_seq(scene);
200
201         if(last_seq && last_seq->type != SEQ_PLUGIN) return;
202
203         sh = get_sequence_effect(last_seq);
204         sh.free(last_seq);
205         sh.init_plugin(last_seq, str);
206
207         last_seq->machine = MAX3(last_seq->seq1->machine, 
208                                  last_seq->seq2->machine, 
209                                  last_seq->seq3->machine);
210
211         if( seq_test_overlap(ed->seqbasep, last_seq) ) shuffle_seq(ed->seqbasep, last_seq);
212         
213 }
214
215
216 void boundbox_seq(Scene *scene, rctf *rect)
217 {
218         Sequence *seq;
219         Editing *ed= seq_give_editing(scene, FALSE);
220         float min[2], max[2];
221
222         
223         if(ed==NULL) return;
224
225         min[0]= 0.0;
226         max[0]= EFRA+1;
227         min[1]= 0.0;
228         max[1]= 8.0;
229
230         seq= ed->seqbasep->first;
231         while(seq) {
232
233                 if( min[0] > seq->startdisp-1) min[0]= seq->startdisp-1;
234                 if( max[0] < seq->enddisp+1) max[0]= seq->enddisp+1;
235                 if( max[1] < seq->machine+2.0) max[1]= seq->machine+2.0;
236
237                 seq= seq->next;
238         }
239
240         rect->xmin= min[0];
241         rect->xmax= max[0];
242         rect->ymin= min[1];
243         rect->ymax= max[1];
244
245 }
246
247 int mouse_frame_side(View2D *v2d, short mouse_x, int frame ) 
248 {
249         short mval[2];
250         float mouseloc[2];
251         
252         mval[0]= mouse_x;
253         mval[1]= 0;
254         
255         /* choose the side based on which side of the playhead the mouse is on */
256         UI_view2d_region_to_view(v2d, mval[0], mval[1], &mouseloc[0], &mouseloc[1]);
257         
258         return mouseloc[0] > frame ? SEQ_SIDE_RIGHT : SEQ_SIDE_LEFT;
259 }
260
261
262 Sequence *find_neighboring_sequence(Scene *scene, Sequence *test, int lr, int sel) 
263 {
264         /* sel - 0==unselected, 1==selected, -1==done care*/
265         Sequence *seq;
266         Editing *ed= seq_give_editing(scene, FALSE);
267
268         if(ed==NULL) return NULL;
269
270         if (sel>0) sel = SELECT;
271         
272         for(seq= ed->seqbasep->first; seq; seq= seq->next) {
273                 if(     (seq!=test) &&
274                         (test->machine==seq->machine) &&
275                         ((sel == -1) || (sel && (seq->flag & SELECT)) || (sel==0 && (seq->flag & SELECT)==0)  ))
276                 {
277                         switch (lr) {
278                         case SEQ_SIDE_LEFT:
279                                 if (test->startdisp == (seq->enddisp)) {
280                                         return seq;
281                                 }
282                                 break;
283                         case SEQ_SIDE_RIGHT:
284                                 if (test->enddisp == (seq->startdisp)) {
285                                         return seq;
286                                 }
287                                 break;
288                         }
289                 }
290         }
291         return NULL;
292 }
293
294 Sequence *find_next_prev_sequence(Scene *scene, Sequence *test, int lr, int sel) 
295 {
296         /* sel - 0==unselected, 1==selected, -1==done care*/
297         Sequence *seq,*best_seq = NULL;
298         Editing *ed= seq_give_editing(scene, FALSE);
299         
300         int dist, best_dist;
301         best_dist = MAXFRAME*2;
302
303         
304         if(ed==NULL) return NULL;
305
306         if (sel) sel = SELECT;
307         
308         seq= ed->seqbasep->first;
309         while(seq) {
310                 if(             (seq!=test) &&
311                                 (test->machine==seq->machine) &&
312                                 (test->depth==seq->depth) &&
313                                 ((sel == -1) || (sel==(seq->flag & SELECT))))
314                 {
315                         dist = MAXFRAME*2;
316                         
317                         switch (lr) {
318                         case SEQ_SIDE_LEFT:
319                                 if (seq->enddisp <= test->startdisp) {
320                                         dist = test->enddisp - seq->startdisp;
321                                 }
322                                 break;
323                         case SEQ_SIDE_RIGHT:
324                                 if (seq->startdisp >= test->enddisp) {
325                                         dist = seq->startdisp - test->enddisp;
326                                 }
327                                 break;
328                         }
329                         
330                         if (dist==0) {
331                                 best_seq = seq;
332                                 break;
333                         } else if (dist < best_dist) {
334                                 best_dist = dist;
335                                 best_seq = seq;
336                         }
337                 }
338                 seq= seq->next;
339         }
340         return best_seq; /* can be null */
341 }
342
343
344 Sequence *find_nearest_seq(Scene *scene, View2D *v2d, int *hand, short mval[2])
345 {
346         Sequence *seq;
347         Editing *ed= seq_give_editing(scene, FALSE);
348         float x, y;
349         float pixelx;
350         float handsize;
351         float displen;
352         *hand= SEQ_SIDE_NONE;
353
354         
355         if(ed==NULL) return NULL;
356         
357         pixelx = (v2d->cur.xmax - v2d->cur.xmin)/(v2d->mask.xmax - v2d->mask.xmin);
358
359         UI_view2d_region_to_view(v2d, mval[0], mval[1], &x, &y);
360         
361         seq= ed->seqbasep->first;
362         
363         while(seq) {
364                 if(seq->machine == (int)y) {
365                         /* check for both normal strips, and strips that have been flipped horizontally */
366                         if( ((seq->startdisp < seq->enddisp) && (seq->startdisp<=x && seq->enddisp>=x)) ||
367                                 ((seq->startdisp > seq->enddisp) && (seq->startdisp>=x && seq->enddisp<=x)) )
368                         {
369                                 if(seq_tx_test(seq)) {
370                                         
371                                         /* clamp handles to defined size in pixel space */
372                                         
373                                         handsize = seq->handsize;
374                                         displen = (float)abs(seq->startdisp - seq->enddisp);
375                                         
376                                         if (displen / pixelx > 16) { /* dont even try to grab the handles of small strips */
377                                                 /* Set the max value to handle to 1/3 of the total len when its less then 28.
378                                                 * This is important because otherwise selecting handles happens even when you click in the middle */
379                                                 
380                                                 if ((displen/3) < 30*pixelx) {
381                                                         handsize = displen/3;
382                                                 } else {
383                                                         CLAMP(handsize, 7*pixelx, 30*pixelx);
384                                                 }
385                                                 
386                                                 if( handsize+seq->startdisp >=x )
387                                                         *hand= SEQ_SIDE_LEFT;
388                                                 else if( -handsize+seq->enddisp <=x )
389                                                         *hand= SEQ_SIDE_RIGHT;
390                                         }
391                                 }
392                                 return seq;
393                         }
394                 }
395                 seq= seq->next;
396         }
397         return 0;
398 }
399
400 void update_seq_ipo_rect(Scene *scene, View2D *v2d, Sequence *seq)
401 {
402         float start;
403         float end;
404
405         if (!seq || !seq->ipo) {
406                 return;
407         }
408         start =  -5.0;
409         end   =  105.0;
410
411         
412         /* Adjust IPO window to sequence and 
413            avoid annoying snap-back to startframe 
414            when Lock Time is on */
415         if (0) { // XXX v2d->flag & V2D_VIEWLOCK) {
416                 if ((seq->flag & SEQ_IPO_FRAME_LOCKED) != 0) {
417                         start = -5.0 + seq->startdisp;
418                         end = 5.0 + seq->enddisp;
419                 } else {
420                         start = (float)scene->r.sfra - 0.1;
421                         end = scene->r.efra;
422                 }
423         }
424
425         seq->ipo->cur.xmin= start;
426         seq->ipo->cur.xmax= end;
427 }
428
429 void update_seq_icu_rects(Sequence * seq)
430 {
431         IpoCurve *icu= NULL;
432         struct SeqEffectHandle sh;
433
434         if (!seq || !seq->ipo) {
435                 return;
436         }
437
438         if(!(seq->type & SEQ_EFFECT)) {
439                 return;
440         }
441
442         sh = get_sequence_effect(seq);
443
444         for(icu= seq->ipo->curve.first; icu; icu= icu->next) {
445                 sh.store_icu_yrange(seq, icu->adrcode, &icu->ymin, &icu->ymax);
446         }
447 }
448
449
450 static int seq_is_parent(Sequence *par, Sequence *seq)
451 {
452         return ((par->seq1 == seq) || (par->seq2 == seq) || (par->seq3 == seq));
453 }
454
455 static int seq_is_predecessor(Sequence *pred, Sequence *seq)
456 {
457         if (!pred) return 0;
458         if(pred == seq) return 0;
459         else if(seq_is_parent(pred, seq)) return 1;
460         else if(pred->seq1 && seq_is_predecessor(pred->seq1, seq)) return 1;
461         else if(pred->seq2 && seq_is_predecessor(pred->seq2, seq)) return 1;
462         else if(pred->seq3 && seq_is_predecessor(pred->seq3, seq)) return 1;
463
464         return 0;
465 }
466
467 void deselect_all_seq(Scene *scene)
468 {
469         Sequence *seq;
470         Editing *ed= seq_give_editing(scene, FALSE);
471
472         
473         if(ed==NULL) return;
474
475         SEQP_BEGIN(ed, seq) {
476                 seq->flag &= SEQ_DESEL;
477         }
478         SEQ_END
479                 
480 }
481
482 void recurs_sel_seq(Sequence *seqm)
483 {
484         Sequence *seq;
485
486         seq= seqm->seqbase.first;
487         while(seq) {
488
489                 if(seqm->flag & (SEQ_LEFTSEL+SEQ_RIGHTSEL)) seq->flag &= SEQ_DESEL;
490                 else if(seqm->flag & SELECT) seq->flag |= SELECT;
491                 else seq->flag &= SEQ_DESEL;
492
493                 if(seq->seqbase.first) recurs_sel_seq(seq);
494
495                 seq= seq->next;
496         }
497 }
498
499 Sequence *alloc_sequence(ListBase *lb, int cfra, int machine)
500 {
501         Sequence *seq;
502
503         /*ed= scene->ed;*/
504
505         seq= MEM_callocN( sizeof(Sequence), "addseq");
506         BLI_addtail(lb, seq);
507
508         //set_last_seq(scene, seq); // Probably not a great idea at such a low level anyway - Campbell
509
510         *( (short *)seq->name )= ID_SEQ;
511         seq->name[2]= 0;
512
513         seq->flag= SELECT;
514         seq->start= cfra;
515         seq->machine= machine;
516         seq->mul= 1.0;
517         seq->blend_opacity = 100.0;
518         
519         return seq;
520 }
521
522 int event_to_efftype(int event)
523 {
524         if(event==2) return SEQ_CROSS;
525         if(event==3) return SEQ_GAMCROSS;
526         if(event==4) return SEQ_ADD;
527         if(event==5) return SEQ_SUB;
528         if(event==6) return SEQ_MUL;
529         if(event==7) return SEQ_ALPHAOVER;
530         if(event==8) return SEQ_ALPHAUNDER;
531         if(event==9) return SEQ_OVERDROP;
532         if(event==10) return SEQ_PLUGIN;
533         if(event==13) return SEQ_WIPE;
534         if(event==14) return SEQ_GLOW;
535         if(event==15) return SEQ_TRANSFORM;
536         if(event==16) return SEQ_COLOR;
537         if(event==17) return SEQ_SPEED;
538         return 0;
539 }
540
541 #if 0
542 static void reload_sound_strip(Scene *scene, char *name)
543 {
544         Editing *ed;
545         Sequence *seq, *seqact;
546         SpaceFile *sfile;
547         Sequence *last_seq= get_last_seq(scene);
548
549         ed= scene->ed;
550
551         if(last_seq==0 || last_seq->type!=SEQ_SOUND) return;
552         seqact= last_seq;       /* last_seq changes in alloc_sequence */
553
554         /* search sfile */
555 //      sfile= scrarea_find_space_of_type(curarea, SPACE_FILE);
556         if(sfile==0) return;
557
558         waitcursor(1);
559
560         seq = sfile_to_snd_sequence(sfile, seqact->start, seqact->machine);
561         printf("seq->type: %i\n", seq->type);
562         if(seq && seq!=seqact) {
563                 /* i'm not sure about this one, seems to work without it -- sgefant */
564                 seq_free_strip(seqact->strip);
565
566                 seqact->strip= seq->strip;
567
568                 seqact->len= seq->len;
569                 calc_sequence(seqact);
570
571                 seq->strip= 0;
572                 seq_free_sequence(ed, seq);
573                 BLI_remlink(ed->seqbasep, seq);
574
575                 seq= ed->seqbasep->first;
576
577         }
578
579         waitcursor(0);
580
581 }
582 #endif
583
584 static void reload_image_strip(Scene *scene, char *name)
585 {
586         Editing *ed= seq_give_editing(scene, FALSE);
587         Sequence *seq=NULL, *seqact;
588         SpaceFile *sfile=NULL;
589         Sequence *last_seq= get_last_seq(scene);
590
591
592
593         if(last_seq==0 || last_seq->type!=SEQ_IMAGE) return;
594         seqact= last_seq;       /* last_seq changes in alloc_sequence */
595
596         /* search sfile */
597 //      sfile= scrarea_find_space_of_type(curarea, SPACE_FILE);
598         if(sfile==0) return;
599
600         waitcursor(1);
601
602 //      seq= sfile_to_sequence(scene, sfile, seqact->start, seqact->machine, 1); // XXX ADD BACK
603         if(seq && seq!=seqact) {
604                 seq_free_strip(seqact->strip);
605
606                 seqact->strip= seq->strip;
607
608                 seqact->len= seq->len;
609                 calc_sequence(seqact);
610
611                 seq->strip= 0;
612                 seq_free_sequence(ed, seq);
613                 BLI_remlink(ed->seqbasep, seq);
614
615                 update_changed_seq_and_deps(scene, seqact, 1, 1);
616         }
617         waitcursor(0);
618
619 }
620
621
622 void change_sequence(Scene *scene)
623 {
624         Editing *ed= seq_give_editing(scene, FALSE);
625         Sequence *last_seq= get_last_seq(scene);
626         Scene *sce;
627         short event;
628
629         if(last_seq==0) return;
630
631         if(last_seq->type & SEQ_EFFECT) {
632                 event = pupmenu("Change Effect%t"
633                                 "|Switch A <-> B %x1"
634                                 "|Switch B <-> C %x10"
635                                 "|Plugin%x11"
636                                 "|Recalculate%x12"
637                                 "|Cross%x2"
638                                 "|Gamma Cross%x3"
639                                 "|Add%x4"
640                                 "|Sub%x5"
641                                 "|Mul%x6"
642                                 "|Alpha Over%x7"
643                                 "|Alpha Under%x8"
644                                 "|Alpha Over Drop%x9"
645                                 "|Wipe%x13"
646                                 "|Glow%x14"
647                                 "|Transform%x15"
648                                 "|Color Generator%x16"
649                                 "|Speed Control%x17");
650                 if(event > 0) {
651                         if(event==1) {
652                                 SWAP(Sequence *,last_seq->seq1,last_seq->seq2);
653                         }
654                         else if(event==10) {
655                                 SWAP(Sequence *,last_seq->seq2,last_seq->seq3);
656                         }
657                         else if(event==11) {
658                                 activate_fileselect(
659                                         FILE_SPECIAL, "Select Plugin", 
660                                         U.plugseqdir, change_plugin_seq);
661                         }
662                         else if(event==12);     
663                                 /* recalculate: only new_stripdata */
664                         else {
665                                 /* free previous effect and init new effect */
666                                 struct SeqEffectHandle sh;
667
668                                 if (get_sequence_effect_num_inputs(
669                                             last_seq->type)
670                                     < get_sequence_effect_num_inputs(
671                                             event_to_efftype(event))) {
672                                         error("New effect needs more "
673                                               "input strips!");
674                                 } else {
675                                         sh = get_sequence_effect(last_seq);
676                                         sh.free(last_seq);
677                                         
678                                         last_seq->type 
679                                                 = event_to_efftype(event);
680                                         
681                                         sh = get_sequence_effect(last_seq);
682                                         sh.init(last_seq);
683                                 }
684                         }
685
686                         update_changed_seq_and_deps(scene, last_seq, 0, 1);
687                 }
688         }
689         else if(last_seq->type == SEQ_IMAGE) {
690                 if(okee("Change images")) {
691                         activate_fileselect(FILE_SPECIAL, 
692                                             "Select Images", 
693                                             ed->act_imagedir, 
694                                             reload_image_strip);
695                 }
696         }
697         else if(last_seq->type == SEQ_MOVIE) {
698                 ;
699         }
700         else if(last_seq->type == SEQ_SCENE) {
701                 event= pupmenu("Change Scene%t|Update Start and End");
702
703                 if(event==1) {
704                         sce= last_seq->scene;
705
706                         last_seq->len= sce->r.efra - sce->r.sfra + 1;
707                         last_seq->sfra= sce->r.sfra;
708                         
709                         /* bad code to change seq->len? update_changed_seq_and_deps() expects the strip->len to be OK */
710                         new_tstripdata(last_seq);
711                         
712                         update_changed_seq_and_deps(scene, last_seq, 1, 1);
713
714                 }
715         }
716
717 }
718
719 int seq_effect_find_selected(Scene *scene, Sequence *activeseq, int type, Sequence **selseq1, Sequence **selseq2, Sequence **selseq3, char **error_str)
720 {
721         Editing *ed = seq_give_editing(scene, FALSE);
722         Sequence *seq1= 0, *seq2= 0, *seq3= 0, *seq;
723         
724         *error_str= NULL;
725
726         if (!activeseq)
727                 seq2= get_last_seq(scene);
728
729         for(seq=ed->seqbasep->first; seq; seq=seq->next) {
730                 if(seq->flag & SELECT) {
731                         if (seq->type == SEQ_RAM_SOUND || seq->type == SEQ_HD_SOUND) {
732                                 *error_str= "Can't apply effects to audio sequence strips";
733                                 return 0;
734                         }
735                         if((seq != activeseq) && (seq != seq2)) {
736                                 if(seq2==0) seq2= seq;
737                                 else if(seq1==0) seq1= seq;
738                                 else if(seq3==0) seq3= seq;
739                                 else {
740                                                                         *error_str= "Can't apply effect to more than 3 sequence strips";
741                                                                         return 0;
742                                 }
743                         }
744                 }
745         }
746        
747         /* make sequence selection a little bit more intuitive
748            for 3 strips: the last-strip should be sequence3 */
749         if (seq3 != 0 && seq2 != 0) {
750                 Sequence *tmp = seq2;
751                 seq2 = seq3;
752                 seq3 = tmp;
753         }
754         
755
756         switch(get_sequence_effect_num_inputs(type)) {
757         case 0:
758                 *selseq1 = *selseq2 = *selseq3 = 0;
759                 return 1; /* succsess */
760         case 1:
761                 if(seq2==0)  {
762                         *error_str= "Need at least one selected sequence strip";
763                         return 0;
764                 }
765                 if(seq1==0) seq1= seq2;
766                 if(seq3==0) seq3= seq2;
767         case 2:
768                 if(seq1==0 || seq2==0) {
769                         *error_str= "Need 2 selected sequence strips";
770                         return 0;
771                 }
772                 if(seq3==0) seq3= seq2;
773         }
774         
775         if (seq1==NULL && seq2==NULL && seq3==NULL) {
776                 *error_str= "TODO: in what cases does this happen?";
777                 return 0;
778         }
779         
780         *selseq1= seq1;
781         *selseq2= seq2;
782         *selseq3= seq3;
783
784         return 1;
785 }
786
787 void reassign_inputs_seq_effect(Scene *scene)
788 {
789         Editing *ed= seq_give_editing(scene, FALSE);
790         Sequence *seq1, *seq2, *seq3, *last_seq = get_last_seq(scene);
791         char *error_msg;
792
793         if(last_seq==0 || !(last_seq->type & SEQ_EFFECT)) return;
794         if(ed==NULL) return;
795
796         if(!seq_effect_find_selected(scene, last_seq, last_seq->type, &seq1, &seq2, &seq3, &error_msg)) {
797                 //BKE_report(op->reports, RPT_ERROR, error_msg); // XXX operatorify
798                 return;
799         }
800         /* see reassigning would create a cycle */
801         if(     seq_is_predecessor(seq1, last_seq) ||
802                 seq_is_predecessor(seq2, last_seq) ||
803                 seq_is_predecessor(seq3, last_seq)
804         ) {
805                 //BKE_report(op->reports, RPT_ERROR, "Can't reassign inputs: no cycles allowed"); // XXX operatorify
806                 return;
807         }
808         
809         last_seq->seq1 = seq1;
810         last_seq->seq2 = seq2;
811         last_seq->seq3 = seq3;
812
813         update_changed_seq_and_deps(scene, last_seq, 1, 1);
814
815 }
816
817 static Sequence *del_seq_find_replace_recurs(Scene *scene, Sequence *seq)
818 {
819         Sequence *seq1, *seq2, *seq3;
820
821         /* try to find a replacement input sequence, and flag for later deletion if
822            no replacement can be found */
823
824         if(!seq)
825                 return NULL;
826         else if(!(seq->type & SEQ_EFFECT))
827                 return ((seq->flag & SELECT)? NULL: seq);
828         else if(!(seq->flag & SELECT)) {
829                 /* try to find replacement for effect inputs */
830                 seq1= del_seq_find_replace_recurs(scene, seq->seq1);
831                 seq2= del_seq_find_replace_recurs(scene, seq->seq2);
832                 seq3= del_seq_find_replace_recurs(scene, seq->seq3);
833
834                 if(seq1==seq->seq1 && seq2==seq->seq2 && seq3==seq->seq3);
835                 else if(seq1 || seq2 || seq3) {
836                         seq->seq1= (seq1)? seq1: (seq2)? seq2: seq3;
837                         seq->seq2= (seq2)? seq2: (seq1)? seq1: seq3;
838                         seq->seq3= (seq3)? seq3: (seq1)? seq1: seq2;
839
840                         update_changed_seq_and_deps(scene, seq, 1, 1);
841                 }
842                 else
843                         seq->flag |= SELECT; /* mark for delete */
844         }
845
846         if (seq->flag & SELECT) {
847                 if((seq1 = del_seq_find_replace_recurs(scene, seq->seq1))) return seq1;
848                 if((seq2 = del_seq_find_replace_recurs(scene, seq->seq2))) return seq2;
849                 if((seq3 = del_seq_find_replace_recurs(scene, seq->seq3))) return seq3;
850                 else return NULL;
851         }
852         else
853                 return seq;
854 }
855
856 static void recurs_del_seq_flag(Scene *scene, ListBase *lb, short flag, short deleteall)
857 {
858         Editing *ed= seq_give_editing(scene, FALSE);
859         Sequence *seq, *seqn;
860         Sequence *last_seq = get_last_seq(scene);
861
862         seq= lb->first;
863         while(seq) {
864                 seqn= seq->next;
865                 if((seq->flag & flag) || deleteall) {
866                         if(seq->type==SEQ_RAM_SOUND && seq->sound) 
867                                 seq->sound->id.us--;
868
869                         BLI_remlink(lb, seq);
870                         if(seq==last_seq) set_last_seq(scene, NULL);
871                         if(seq->type==SEQ_META) recurs_del_seq_flag(scene, &seq->seqbase, flag, 1);
872                         if(seq->ipo) seq->ipo->id.us--;
873                         seq_free_sequence(ed, seq);
874                 }
875                 seq= seqn;
876         }
877 }
878
879 static Sequence *dupli_seq(Sequence *seq) 
880 {
881         Sequence *seqn = MEM_dupallocN(seq);
882         // XXX animato: ID *id;
883
884         seq->tmp = seqn;
885                 
886         seqn->strip= MEM_dupallocN(seq->strip);
887
888         // XXX animato
889 #if 0
890         if (seqn->ipo) {
891                 if (U.dupflag & USER_DUP_IPO) {
892                         id= (ID *)seqn->ipo;
893                         seqn->ipo= copy_ipo(seqn->ipo);
894                         /* we don't need to decrease the number
895                          * of the ipo because we never increase it,
896                          * for example, adduplicate need decrease
897                          * the number but only because copy_object
898                          * call id_us_plus for the ipo block and
899                          * single_ipo_users only work if id->us > 1.
900                          *
901                          * need call ipo_idnew here, for drivers ??
902                          * - Diego
903                          */
904                 }
905                 else
906                         seqn->ipo->id.us++;
907         }
908 #endif
909
910         seqn->strip->tstripdata = 0;
911         seqn->strip->tstripdata_startstill = 0;
912         seqn->strip->tstripdata_endstill = 0;
913         seqn->strip->ibuf_startstill = 0;
914         seqn->strip->ibuf_endstill = 0;
915
916         if (seq->strip->crop) {
917                 seqn->strip->crop = MEM_dupallocN(seq->strip->crop);
918         }
919
920         if (seq->strip->transform) {
921                 seqn->strip->transform = MEM_dupallocN(seq->strip->transform);
922         }
923
924         if (seq->strip->proxy) {
925                 seqn->strip->proxy = MEM_dupallocN(seq->strip->proxy);
926         }
927
928         if (seq->strip->color_balance) {
929                 seqn->strip->color_balance 
930                         = MEM_dupallocN(seq->strip->color_balance);
931         }
932         
933         if(seq->type==SEQ_META) {
934                 seqn->strip->stripdata = 0;
935
936                 seqn->seqbase.first= seqn->seqbase.last= 0;
937                 /* WATCH OUT!!! - This metastrip is not recursively duplicated here - do this after!!! */
938                 /* - recurs_dupli_seq(&seq->seqbase,&seqn->seqbase);*/
939         } else if(seq->type == SEQ_SCENE) {
940                 seqn->strip->stripdata = 0;
941         } else if(seq->type == SEQ_MOVIE) {
942                 seqn->strip->stripdata = 
943                                 MEM_dupallocN(seq->strip->stripdata);
944                 seqn->anim= 0;
945         } else if(seq->type == SEQ_RAM_SOUND) {
946                 seqn->strip->stripdata = 
947                                 MEM_dupallocN(seq->strip->stripdata);
948                 seqn->sound->id.us++;
949         } else if(seq->type == SEQ_HD_SOUND) {
950                 seqn->strip->stripdata = 
951                                 MEM_dupallocN(seq->strip->stripdata);
952                 seqn->hdaudio = 0;
953         } else if(seq->type == SEQ_IMAGE) {
954                 seqn->strip->stripdata = 
955                                 MEM_dupallocN(seq->strip->stripdata);
956         } else if(seq->type >= SEQ_EFFECT) {
957                 if(seq->seq1 && seq->seq1->tmp) seqn->seq1= seq->seq1->tmp;
958                 if(seq->seq2 && seq->seq2->tmp) seqn->seq2= seq->seq2->tmp;
959                 if(seq->seq3 && seq->seq3->tmp) seqn->seq3= seq->seq3->tmp;
960
961                 if (seq->type & SEQ_EFFECT) {
962                         struct SeqEffectHandle sh;
963                         sh = get_sequence_effect(seq);
964                         if(sh.copy)
965                                 sh.copy(seq, seqn);
966                 }
967
968                 seqn->strip->stripdata = 0;
969                 
970         } else {
971                 fprintf(stderr, "Aiiiiekkk! sequence type not "
972                                 "handled in duplicate!\nExpect a crash"
973                                                 " now...\n");
974         }
975         
976         return seqn;
977 }
978
979 static Sequence * deep_dupli_seq(Sequence * seq)
980 {
981         Sequence * seqn = dupli_seq(seq);
982         if (seq->type == SEQ_META) {
983                 Sequence * s;
984                 for(s= seq->seqbase.first; s; s = s->next) {
985                         Sequence * n = deep_dupli_seq(s);
986                         if (n) { 
987                                 BLI_addtail(&seqn->seqbase, n);
988                         }
989                 }
990         }
991         return seqn;
992 }
993
994
995 static void recurs_dupli_seq(Scene *scene, ListBase *old, ListBase *new)
996 {
997         Sequence *seq;
998         Sequence *seqn = 0;
999         Sequence *last_seq = get_last_seq(scene);
1000
1001         for(seq= old->first; seq; seq= seq->next) {
1002                 seq->tmp= NULL;
1003                 if(seq->flag & SELECT) {
1004                         seqn = dupli_seq(seq);
1005                         if (seqn) { /*should never fail */
1006                                 seq->flag &= SEQ_DESEL;
1007                                 seqn->flag &= ~(SEQ_LEFTSEL+SEQ_RIGHTSEL+SEQ_LOCK);
1008
1009                                 BLI_addtail(new, seqn);
1010                                 if(seq->type==SEQ_META)
1011                                         recurs_dupli_seq(scene, &seq->seqbase,&seqn->seqbase);
1012                                 
1013                                 if (seq == last_seq) {
1014                                         set_last_seq(scene, seqn);
1015                                 }
1016                         }
1017                 }
1018         }
1019 }
1020
1021 static Sequence *cut_seq_hard(Scene *scene, Sequence * seq, int cutframe)
1022 {
1023         TransSeq ts;
1024         Sequence *seqn = 0;
1025         int skip_dup = FALSE;
1026
1027         /* backup values */
1028         ts.start= seq->start;
1029         ts.machine= seq->machine;
1030         ts.startstill= seq->startstill;
1031         ts.endstill= seq->endstill;
1032         ts.startdisp= seq->startdisp;
1033         ts.enddisp= seq->enddisp;
1034         ts.startofs= seq->anim_startofs;
1035         ts.endofs= seq->anim_endofs;
1036         ts.len= seq->len;
1037         
1038         /* First Strip! */
1039         /* strips with extended stillfames before */
1040         
1041         if ((seq->startstill) && (cutframe <seq->start)) {
1042                 /* don't do funny things with METAs ... */
1043                 if (seq->type == SEQ_META) {
1044                         skip_dup = TRUE;
1045                         seq->startstill = seq->start - cutframe;
1046                 } else {
1047                         seq->start= cutframe -1;
1048                         seq->startstill= cutframe -seq->startdisp -1;
1049                         seq->anim_endofs += seq->len - 1;
1050                         seq->endstill= 0;
1051                 }
1052         }
1053         /* normal strip */
1054         else if ((cutframe >=seq->start)&&(cutframe <=(seq->start+seq->len))) {
1055                 seq->endofs = 0;
1056                 seq->endstill = 0;
1057                 seq->anim_endofs += (seq->start+seq->len) - cutframe;
1058         }
1059         /* strips with extended stillframes after */
1060         else if (((seq->start+seq->len) < cutframe) && (seq->endstill)) {
1061                 seq->endstill -= seq->enddisp - cutframe;
1062                 /* don't do funny things with METAs ... */
1063                 if (seq->type == SEQ_META) {
1064                         skip_dup = TRUE;
1065                 }
1066         }
1067         
1068         reload_sequence_new_file(scene, seq);
1069         calc_sequence(seq);
1070         
1071         if (!skip_dup) {
1072                 /* Duplicate AFTER the first change */
1073                 seqn = deep_dupli_seq(seq);
1074         }
1075         
1076         if (seqn) { 
1077                 seqn->flag |= SELECT;
1078                         
1079                 /* Second Strip! */
1080                 /* strips with extended stillframes before */
1081                 if ((seqn->startstill) && (cutframe == seqn->start + 1)) {
1082                         seqn->start = ts.start;
1083                         seqn->startstill= ts.start- cutframe;
1084                         seqn->anim_endofs = ts.endofs;
1085                         seqn->endstill = ts.endstill;
1086                 }
1087                 
1088                 /* normal strip */
1089                 else if ((cutframe>=seqn->start)&&(cutframe<=(seqn->start+seqn->len))) {
1090                         seqn->start = cutframe;
1091                         seqn->startstill = 0;
1092                         seqn->startofs = 0;
1093                         seqn->anim_startofs += cutframe - ts.start;
1094                         seqn->anim_endofs = ts.endofs;
1095                         seqn->endstill = ts.endstill;
1096                 }                               
1097                 
1098                 /* strips with extended stillframes after */
1099                 else if (((seqn->start+seqn->len) < cutframe) && (seqn->endstill)) {
1100                         seqn->start = cutframe;
1101                         seqn->startofs = 0;
1102                         seqn->anim_startofs += ts.len-1;
1103                         seqn->endstill = ts.enddisp - cutframe -1;
1104                         seqn->startstill = 0;
1105                 }
1106                 
1107                 reload_sequence_new_file(scene, seqn);
1108                 calc_sequence(seqn);
1109         }
1110         return seqn;
1111 }
1112
1113 static Sequence *cut_seq_soft(Scene *scene, Sequence * seq, int cutframe)
1114 {
1115         TransSeq ts;
1116         Sequence *seqn = 0;
1117         int skip_dup = FALSE;
1118
1119         /* backup values */
1120         ts.start= seq->start;
1121         ts.machine= seq->machine;
1122         ts.startstill= seq->startstill;
1123         ts.endstill= seq->endstill;
1124         ts.startdisp= seq->startdisp;
1125         ts.enddisp= seq->enddisp;
1126         ts.startofs= seq->startofs;
1127         ts.endofs= seq->endofs;
1128         ts.len= seq->len;
1129         
1130         /* First Strip! */
1131         /* strips with extended stillfames before */
1132         
1133         if ((seq->startstill) && (cutframe <seq->start)) {
1134                 /* don't do funny things with METAs ... */
1135                 if (seq->type == SEQ_META) {
1136                         skip_dup = TRUE;
1137                         seq->startstill = seq->start - cutframe;
1138                 } else {
1139                         seq->start= cutframe -1;
1140                         seq->startstill= cutframe -seq->startdisp -1;
1141                         seq->endofs = seq->len - 1;
1142                         seq->endstill= 0;
1143                 }
1144         }
1145         /* normal strip */
1146         else if ((cutframe >=seq->start)&&(cutframe <=(seq->start+seq->len))) {
1147                 seq->endofs = (seq->start+seq->len) - cutframe;
1148         }
1149         /* strips with extended stillframes after */
1150         else if (((seq->start+seq->len) < cutframe) && (seq->endstill)) {
1151                 seq->endstill -= seq->enddisp - cutframe;
1152                 /* don't do funny things with METAs ... */
1153                 if (seq->type == SEQ_META) {
1154                         skip_dup = TRUE;
1155                 }
1156         }
1157         
1158         calc_sequence(seq);
1159         
1160         if (!skip_dup) {
1161                 /* Duplicate AFTER the first change */
1162                 seqn = deep_dupli_seq(seq);
1163         }
1164         
1165         if (seqn) { 
1166                 seqn->flag |= SELECT;
1167                         
1168                 /* Second Strip! */
1169                 /* strips with extended stillframes before */
1170                 if ((seqn->startstill) && (cutframe == seqn->start + 1)) {
1171                         seqn->start = ts.start;
1172                         seqn->startstill= ts.start- cutframe;
1173                         seqn->endofs = ts.endofs;
1174                         seqn->endstill = ts.endstill;
1175                 }
1176                 
1177                 /* normal strip */
1178                 else if ((cutframe>=seqn->start)&&(cutframe<=(seqn->start+seqn->len))) {
1179                         seqn->startstill = 0;
1180                         seqn->startofs = cutframe - ts.start;
1181                         seqn->endofs = ts.endofs;
1182                         seqn->endstill = ts.endstill;
1183                 }                               
1184                 
1185                 /* strips with extended stillframes after */
1186                 else if (((seqn->start+seqn->len) < cutframe) && (seqn->endstill)) {
1187                         seqn->start = cutframe - ts.len +1;
1188                         seqn->startofs = ts.len-1;
1189                         seqn->endstill = ts.enddisp - cutframe -1;
1190                         seqn->startstill = 0;
1191                 }
1192                 
1193                 calc_sequence(seqn);
1194         }
1195         return seqn;
1196 }
1197
1198
1199 /* like duplicate, but only duplicate and cut overlapping strips,
1200  * strips to the left of the cutframe are ignored and strips to the right are moved into the new list */
1201 static int cut_seq_list(Scene *scene, ListBase *old, ListBase *new, int cutframe,
1202                         Sequence * (*cut_seq)(Scene *, Sequence *, int))
1203 {
1204         int did_something = FALSE;
1205         Sequence *seq, *seq_next;
1206         
1207         seq= old->first;
1208         
1209         while(seq) {
1210                 seq_next = seq->next; /* we need this because we may remove seq */
1211                 
1212                 seq->tmp= NULL;
1213                 if(seq->flag & SELECT) {
1214                         if(cutframe > seq->startdisp && 
1215                            cutframe < seq->enddisp) {
1216                                 Sequence * seqn = cut_seq(scene, seq, cutframe);
1217                                 if (seqn) {
1218                                         BLI_addtail(new, seqn);
1219                                 }
1220                                 did_something = TRUE;
1221                         } else if (seq->enddisp <= cutframe) {
1222                                 /* do nothing */
1223                         } else if (seq->startdisp >= cutframe) {
1224                                 /* move into new list */
1225                                 BLI_remlink(old, seq);
1226                                 BLI_addtail(new, seq);
1227                         }
1228                 }
1229                 seq = seq_next;
1230         }
1231         return did_something;
1232 }
1233
1234 int insert_gap(Scene *scene, int gap, int cfra)
1235 {
1236         Sequence *seq;
1237         Editing *ed= seq_give_editing(scene, FALSE);
1238         int done=0;
1239
1240         /* all strips >= cfra are shifted */
1241         
1242         if(ed==NULL) return 0;
1243
1244         SEQP_BEGIN(ed, seq) {
1245                 if(seq->startdisp >= cfra) {
1246                         seq->start+= gap;
1247                         calc_sequence(seq);
1248                         done= 1;
1249                 }
1250         }
1251         SEQ_END
1252
1253         return done;
1254 }
1255
1256 void touch_seq_files(Scene *scene)
1257 {
1258         Sequence *seq;
1259         Editing *ed= seq_give_editing(scene, FALSE);
1260         char str[256];
1261
1262         /* touch all strips with movies */
1263         
1264         if(ed==NULL) return;
1265
1266         if(okee("Touch and print selected movies")==0) return;
1267
1268         waitcursor(1);
1269
1270         SEQP_BEGIN(ed, seq) {
1271                 if(seq->flag & SELECT) {
1272                         if(seq->type==SEQ_MOVIE) {
1273                                 if(seq->strip && seq->strip->stripdata) {
1274                                         BLI_make_file_string(G.sce, str, seq->strip->dir, seq->strip->stripdata->name);
1275                                         BLI_touch(seq->name);
1276                                 }
1277                         }
1278
1279                 }
1280         }
1281         SEQ_END
1282
1283         waitcursor(0);
1284 }
1285
1286 void set_filter_seq(Scene *scene)
1287 {
1288         Sequence *seq;
1289         Editing *ed= seq_give_editing(scene, FALSE);
1290
1291         
1292         if(ed==NULL) return;
1293
1294         if(okee("Set Deinterlace")==0) return;
1295
1296         SEQP_BEGIN(ed, seq) {
1297                 if(seq->flag & SELECT) {
1298                         if(seq->type==SEQ_MOVIE) {
1299                                 seq->flag |= SEQ_FILTERY;
1300                                 reload_sequence_new_file(scene, seq);
1301                         }
1302
1303                 }
1304         }
1305         SEQ_END
1306
1307 }
1308
1309 void seq_remap_paths(Scene *scene)
1310 {
1311         Sequence *seq, *last_seq = get_last_seq(scene);
1312         Editing *ed= seq_give_editing(scene, FALSE);
1313         char from[FILE_MAX], to[FILE_MAX], stripped[FILE_MAX];
1314         
1315         
1316         if(last_seq==NULL) 
1317                 return;
1318         
1319         BLI_strncpy(from, last_seq->strip->dir, FILE_MAX);
1320 // XXX  if (0==sbutton(from, 0, sizeof(from)-1, "From: "))
1321 //              return;
1322         
1323         strcpy(to, from);
1324 // XXX  if (0==sbutton(to, 0, sizeof(to)-1, "To: "))
1325 //              return;
1326         
1327         if (strcmp(to, from)==0)
1328                 return;
1329         
1330         SEQP_BEGIN(ed, seq) {
1331                 if(seq->flag & SELECT) {
1332                         if(strncmp(seq->strip->dir, from, strlen(from))==0) {
1333                                 printf("found %s\n", seq->strip->dir);
1334                                 
1335                                 /* strip off the beginning */
1336                                 stripped[0]= 0;
1337                                 BLI_strncpy(stripped, seq->strip->dir + strlen(from), FILE_MAX);
1338                                 
1339                                 /* new path */
1340                                 BLI_strncpy(seq->strip->dir, to, FILE_MAX);
1341                                 strcat(seq->strip->dir, stripped);
1342                                 printf("new %s\n", seq->strip->dir);
1343                         }
1344                 }
1345         }
1346         SEQ_END
1347                 
1348 }
1349
1350
1351 void no_gaps(Scene *scene)
1352 {
1353         Editing *ed= seq_give_editing(scene, FALSE);
1354         int cfra, first= 0, done;
1355
1356         
1357         if(ed==NULL) return;
1358
1359         for(cfra= CFRA; cfra<=EFRA; cfra++) {
1360                 if(first==0) {
1361                         if( evaluate_seq_frame(scene, cfra) ) first= 1;
1362                 }
1363                 else {
1364                         done= 1;
1365                         while( evaluate_seq_frame(scene, cfra) == 0) {
1366                                 done= insert_gap(scene, -1, cfra);
1367                                 if(done==0) break;
1368                         }
1369                         if(done==0) break;
1370                 }
1371         }
1372
1373 }
1374
1375 #if 0
1376 static int seq_get_snaplimit(View2D *v2d)
1377 {
1378         /* fake mouse coords to get the snap value
1379         a bit lazy but its only done once pre transform */
1380         float xmouse, ymouse, x;
1381         short mval[2] = {24, 0}; /* 24 screen px snap */
1382         
1383         UI_view2d_region_to_view(v2d, mval[0], mval[1], &xmouse, &ymouse);
1384         x = xmouse;
1385         mval[0] = 0;
1386         UI_view2d_region_to_view(v2d, mval[0], mval[1], &xmouse, &ymouse);
1387         return (int)(x - xmouse);
1388 }
1389 #endif
1390
1391 void seq_snap(Scene *scene, short event)
1392 {
1393         Editing *ed= seq_give_editing(scene, FALSE);
1394         Sequence *seq;
1395
1396         
1397         if(ed==NULL) return;
1398
1399         /* problem: contents of meta's are all shifted to the same position... */
1400
1401         /* also check metas */
1402         SEQP_BEGIN(ed, seq) {
1403                 if (seq->flag & SELECT && !(seq->depth==0 && seq->flag & SEQ_LOCK) &&
1404                     seq_tx_test(seq)) {
1405                         if((seq->flag & (SEQ_LEFTSEL+SEQ_RIGHTSEL))==0) {
1406                                 seq->start= CFRA-seq->startofs+seq->startstill;
1407                         } else { 
1408                                 if(seq->flag & SEQ_LEFTSEL) {
1409                                         seq_tx_set_final_left(seq, CFRA);
1410                                 } else { /* SEQ_RIGHTSEL */
1411                                         seq_tx_set_final_right(seq, CFRA);
1412                                 }
1413                                 seq_tx_handle_xlimits(seq, seq->flag & SEQ_LEFTSEL, seq->flag & SEQ_RIGHTSEL);
1414                         }
1415                         calc_sequence(seq);
1416                 }
1417         }
1418         SEQ_END
1419
1420         /* test for effects and overlap */
1421         SEQP_BEGIN(ed, seq) {
1422                 if(seq->flag & SELECT && !(seq->depth==0 && seq->flag & SEQ_LOCK)) {
1423                         seq->flag &= ~SEQ_OVERLAP;
1424                         if( seq_test_overlap(ed->seqbasep, seq) ) {
1425                                 shuffle_seq(ed->seqbasep, seq);
1426                         }
1427                 }
1428                 else if(seq->type & SEQ_EFFECT) {
1429                         if(seq->seq1 && (seq->seq1->flag & SELECT)) 
1430                                 calc_sequence(seq);
1431                         else if(seq->seq2 && (seq->seq2->flag & SELECT)) 
1432                                 calc_sequence(seq);
1433                         else if(seq->seq3 && (seq->seq3->flag & SELECT)) 
1434                                 calc_sequence(seq);
1435                 }
1436         }
1437         SEQ_END;
1438
1439         /* as last: */
1440         sort_seq(scene);
1441
1442 }
1443
1444 void seq_snap_menu(Scene *scene)
1445 {
1446         short event;
1447         
1448         event= pupmenu("Snap %t|To Current Frame%x1");
1449         if(event < 1) return;
1450         
1451         seq_snap(scene, event);
1452 }
1453
1454 /* Operator functions */
1455
1456 static int sequencer_mute_exec(bContext *C, wmOperator *op)
1457 {
1458         Scene *scene= CTX_data_scene(C);
1459         Editing *ed= seq_give_editing(scene, FALSE);
1460         Sequence *seq;
1461         int selected;
1462
1463         if(ed==NULL)
1464                 return OPERATOR_CANCELLED;
1465
1466         selected=  RNA_enum_is_equal(op->ptr, "type", "SELECTED");
1467         
1468         
1469         for(seq= ed->seqbasep->first; seq; seq= seq->next) {
1470                 if ((seq->flag & SEQ_LOCK)==0) {
1471                         if(selected){ /* mute unselected */
1472                                 if (seq->flag & SELECT) {
1473                                         seq->flag |= SEQ_MUTE;
1474                                 }
1475                         }
1476                         else {
1477                                 if ((seq->flag & SELECT)==0) {
1478                                         seq->flag |= SEQ_MUTE;
1479                                 }
1480                         }
1481                 }
1482         }
1483         
1484         ED_area_tag_redraw(CTX_wm_area(C));
1485         
1486         return OPERATOR_FINISHED;
1487 }
1488
1489 void SEQUENCER_OT_mute(struct wmOperatorType *ot)
1490 {
1491         /* identifiers */
1492         ot->name= "Mute Strips";
1493         ot->idname= "SEQUENCER_OT_mute";
1494
1495         /* api callbacks */
1496         ot->exec= sequencer_mute_exec;
1497
1498         ot->poll= ED_operator_sequencer_active;
1499         
1500         /* flags */
1501         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1502         
1503         RNA_def_enum(ot->srna, "type", sequencer_prop_operate_types, SEQ_SELECTED, "Type", "");
1504 }
1505
1506
1507 /* unmute operator */
1508 static int sequencer_unmute_exec(bContext *C, wmOperator *op)
1509 {
1510         Scene *scene= CTX_data_scene(C);
1511         Editing *ed= seq_give_editing(scene, FALSE);
1512         Sequence *seq;
1513         int selected;
1514
1515         if(ed==NULL)
1516                 return OPERATOR_CANCELLED;
1517
1518         selected=  RNA_enum_is_equal(op->ptr, "type", "SELECTED");
1519         
1520         
1521         for(seq= ed->seqbasep->first; seq; seq= seq->next) {
1522                 if ((seq->flag & SEQ_LOCK)==0) {
1523                         if(selected){ /* unmute unselected */
1524                                 if (seq->flag & SELECT) {
1525                                         seq->flag &= ~SEQ_MUTE;
1526                                 }
1527                         }
1528                         else {
1529                                 if ((seq->flag & SELECT)==0) {
1530                                         seq->flag &= ~SEQ_MUTE;
1531                                 }
1532                         }
1533                 }
1534         }
1535         
1536         ED_area_tag_redraw(CTX_wm_area(C));
1537         
1538         return OPERATOR_FINISHED;
1539 }
1540
1541 void SEQUENCER_OT_unmute(struct wmOperatorType *ot)
1542 {
1543         /* identifiers */
1544         ot->name= "UnMute Strips";
1545         ot->idname= "SEQUENCER_OT_unmute";
1546
1547         /* api callbacks */
1548         ot->exec= sequencer_unmute_exec;
1549
1550         ot->poll= ED_operator_sequencer_active;
1551         
1552         /* flags */
1553         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1554         
1555         RNA_def_enum(ot->srna, "type", sequencer_prop_operate_types, SEQ_SELECTED, "Type", "");
1556 }
1557
1558
1559 /* lock operator */
1560 static int sequencer_lock_exec(bContext *C, wmOperator *op)
1561 {
1562         Scene *scene= CTX_data_scene(C);
1563         Editing *ed= seq_give_editing(scene, FALSE);
1564         Sequence *seq;
1565
1566         if(ed==NULL)
1567                 return OPERATOR_CANCELLED;
1568
1569         for(seq= ed->seqbasep->first; seq; seq= seq->next) {
1570                 if (seq->flag & SELECT) {
1571                         seq->flag |= SEQ_LOCK;
1572                 }
1573         }
1574
1575         ED_area_tag_redraw(CTX_wm_area(C));
1576
1577         return OPERATOR_FINISHED;
1578 }
1579
1580 void SEQUENCER_OT_lock(struct wmOperatorType *ot)
1581 {
1582         /* identifiers */
1583         ot->name= "Lock Strips";
1584         ot->idname= "SEQUENCER_OT_lock";
1585
1586         /* api callbacks */
1587         ot->exec= sequencer_lock_exec;
1588
1589         ot->poll= ED_operator_sequencer_active;
1590         
1591         /* flags */
1592         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1593 }
1594
1595 /* unlock operator */
1596 static int sequencer_unlock_exec(bContext *C, wmOperator *op)
1597 {
1598         Scene *scene= CTX_data_scene(C);
1599         Editing *ed= seq_give_editing(scene, FALSE);
1600         Sequence *seq;
1601
1602         if(ed==NULL)
1603                 return OPERATOR_CANCELLED;
1604
1605         for(seq= ed->seqbasep->first; seq; seq= seq->next) {
1606                 if (seq->flag & SELECT) {
1607                         seq->flag &= ~SEQ_LOCK;
1608                 }
1609         }
1610
1611         ED_area_tag_redraw(CTX_wm_area(C));
1612
1613         return OPERATOR_FINISHED;
1614 }
1615
1616 void SEQUENCER_OT_unlock(struct wmOperatorType *ot)
1617 {
1618         /* identifiers */
1619         ot->name= "UnLock Strips";
1620         ot->idname= "SEQUENCER_OT_unlock";
1621
1622         /* api callbacks */
1623         ot->exec= sequencer_unlock_exec;
1624
1625         ot->poll= ED_operator_sequencer_active;
1626         
1627         /* flags */
1628         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1629 }
1630
1631 /* reload operator */
1632 static int sequencer_reload_exec(bContext *C, wmOperator *op)
1633 {
1634         Scene *scene= CTX_data_scene(C);
1635         Editing *ed= seq_give_editing(scene, FALSE);
1636         Sequence *seq;
1637
1638         if(ed==NULL)
1639                 return OPERATOR_CANCELLED;
1640
1641         for(seq= ed->seqbasep->first; seq; seq= seq->next) {
1642                 if(seq->flag & SELECT) {
1643                         update_changed_seq_and_deps(scene, seq, 0, 1);
1644                 }
1645         }
1646
1647         ED_area_tag_redraw(CTX_wm_area(C));
1648
1649         return OPERATOR_FINISHED;
1650 }
1651
1652 void SEQUENCER_OT_reload(struct wmOperatorType *ot)
1653 {
1654         /* identifiers */
1655         ot->name= "Reload Strips";
1656         ot->idname= "SEQUENCER_OT_reload";
1657
1658         /* api callbacks */
1659         ot->exec= sequencer_reload_exec;
1660
1661         ot->poll= ED_operator_sequencer_active;
1662         
1663         /* flags */
1664         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1665 }
1666
1667 /* reload operator */
1668 static int sequencer_refresh_all_exec(bContext *C, wmOperator *op)
1669 {
1670         Scene *scene= CTX_data_scene(C);
1671         Editing *ed= seq_give_editing(scene, FALSE);
1672         
1673         if(ed==NULL)
1674                 return OPERATOR_CANCELLED;
1675
1676         free_imbuf_seq(&ed->seqbase);
1677
1678         ED_area_tag_redraw(CTX_wm_area(C));
1679
1680         return OPERATOR_FINISHED;
1681 }
1682
1683 void SEQUENCER_OT_refresh_all(struct wmOperatorType *ot)
1684 {
1685         /* identifiers */
1686         ot->name= "Refresh Sequencer";
1687         ot->idname= "SEQUENCER_OT_refresh_all";
1688
1689         /* api callbacks */
1690         ot->exec= sequencer_refresh_all_exec;
1691
1692         ot->poll= ED_operator_sequencer_active;
1693         
1694         /* flags */
1695         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1696 }
1697
1698 /* cut operator */
1699 static EnumPropertyItem prop_cut_types[] = {
1700         {SEQ_CUT_SOFT, "SOFT", "Soft", ""},
1701         {SEQ_CUT_HARD, "HARD", "Hard", ""},
1702         {0, NULL, NULL, NULL}
1703 };
1704
1705 static int sequencer_cut_exec(bContext *C, wmOperator *op)
1706 {
1707         Scene *scene= CTX_data_scene(C);
1708         Editing *ed= seq_give_editing(scene, FALSE);
1709         int cut_side, cut_hard, cut_frame;
1710
1711         ListBase newlist;
1712         int changed;
1713         
1714         if(ed==NULL)
1715                 return OPERATOR_CANCELLED;
1716
1717         cut_frame= RNA_int_get(op->ptr, "frame");
1718         cut_hard= RNA_enum_get(op->ptr, "type");
1719         cut_side= RNA_enum_get(op->ptr, "side");
1720         
1721         newlist.first= newlist.last= NULL;
1722
1723         if (cut_hard==SEQ_CUT_HARD) {
1724                 changed = cut_seq_list(scene,
1725                         ed->seqbasep, &newlist, cut_frame, cut_seq_hard);
1726         } else {
1727                 changed = cut_seq_list(scene,
1728                         ed->seqbasep, &newlist, cut_frame, cut_seq_soft);
1729         }
1730         
1731         if (newlist.first) { /* got new strips ? */
1732                 Sequence *seq;
1733                 addlisttolist(ed->seqbasep, &newlist);
1734
1735                 if (cut_side != SEQ_SIDE_BOTH) {
1736                         SEQP_BEGIN(ed, seq) {
1737                                 if (cut_side==SEQ_SIDE_LEFT) {
1738                                         if ( seq->startdisp >= cut_frame ) {
1739                                                 seq->flag &= SEQ_DESEL;
1740                                         }
1741                                 } else {
1742                                         if ( seq->enddisp <= cut_frame ) {
1743                                                 seq->flag &= SEQ_DESEL;
1744                                         }
1745                                 }
1746                         }
1747                         SEQ_END;
1748                 }
1749                 /* as last: */
1750                 sort_seq(scene);
1751         }
1752
1753         if (changed) {
1754                 ED_area_tag_redraw(CTX_wm_area(C));
1755         }
1756         
1757         return OPERATOR_FINISHED;
1758 }
1759
1760
1761 static int sequencer_cut_invoke(bContext *C, wmOperator *op, wmEvent *event)
1762 {
1763         Scene *scene = CTX_data_scene(C);
1764         ARegion *ar= CTX_wm_region(C);
1765         View2D *v2d= UI_view2d_fromcontext(C);
1766         
1767         int cut_side, cut_frame;
1768         
1769         cut_frame= CFRA;
1770         cut_side= mouse_frame_side(v2d, event->x - ar->winrct.xmin, cut_frame);
1771         
1772         RNA_int_set(op->ptr, "frame", cut_frame);
1773         RNA_enum_set(op->ptr, "side", cut_side);
1774         /*RNA_enum_set(op->ptr, "type", cut_hard); */ /*This type is set from the key shortcut */
1775
1776         return sequencer_cut_exec(C, op);
1777 }
1778
1779
1780 void SEQUENCER_OT_cut(struct wmOperatorType *ot)
1781 {
1782         /* identifiers */
1783         ot->name= "Cut Strips";
1784         ot->idname= "SEQUENCER_OT_cut";
1785
1786         /* api callbacks */
1787         ot->invoke= sequencer_cut_invoke;
1788         ot->exec= sequencer_cut_exec;
1789
1790         ot->poll= ED_operator_sequencer_active;
1791         
1792         /* flags */
1793         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1794         
1795         RNA_def_int(ot->srna, "frame", 0, INT_MIN, INT_MAX, "Frame", "Frame where selected strips will be cut", INT_MIN, INT_MAX);
1796         RNA_def_enum(ot->srna, "type", prop_cut_types, SEQ_CUT_SOFT, "Type", "the type of cut operation to perform on strips");
1797         RNA_def_enum(ot->srna, "side", prop_side_types, SEQ_SIDE_BOTH, "Side", "The side that remains selected after cutting");
1798 }
1799
1800 /* duplicate operator */
1801 static int sequencer_add_duplicate_exec(bContext *C, wmOperator *op)
1802 {
1803         Scene *scene= CTX_data_scene(C);
1804         Editing *ed= seq_give_editing(scene, FALSE);
1805
1806         ListBase new= {NULL, NULL};
1807
1808         if(ed==NULL)
1809                 return OPERATOR_CANCELLED;
1810
1811         recurs_dupli_seq(scene, ed->seqbasep, &new);
1812         addlisttolist(ed->seqbasep, &new);
1813
1814         ED_area_tag_redraw(CTX_wm_area(C));
1815
1816         return OPERATOR_FINISHED;
1817 }
1818
1819 static int sequencer_add_duplicate_invoke(bContext *C, wmOperator *op, wmEvent *event)
1820 {
1821         sequencer_add_duplicate_exec(C, op);
1822
1823         RNA_int_set(op->ptr, "mode", TFM_TRANSLATION);
1824         WM_operator_name_call(C, "TFM_OT_transform", WM_OP_INVOKE_REGION_WIN, op->ptr);
1825
1826         return OPERATOR_FINISHED;
1827 }
1828
1829 void SEQUENCER_OT_duplicate_add(wmOperatorType *ot)
1830 {
1831
1832         /* identifiers */
1833         ot->name= "Add Duplicate";
1834         ot->idname= "SEQUENCER_OT_duplicate_add";
1835
1836         /* api callbacks */
1837         ot->invoke= sequencer_add_duplicate_invoke;
1838         ot->exec= sequencer_add_duplicate_exec;
1839
1840         ot->poll= ED_operator_sequencer_active;
1841         
1842         /* flags */
1843         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1844         
1845         /* to give to transform */
1846         RNA_def_int(ot->srna, "mode", TFM_TRANSLATION, 0, INT_MAX, "Mode", "", 0, INT_MAX);
1847 }
1848
1849 /* delete operator */
1850 static int sequencer_delete_exec(bContext *C, wmOperator *op)
1851 {
1852         Scene *scene= CTX_data_scene(C);
1853         Editing *ed= seq_give_editing(scene, FALSE);
1854         Sequence *seq;
1855         MetaStack *ms;
1856         int nothingSelected = TRUE;
1857
1858         if(ed==NULL)
1859                 return OPERATOR_CANCELLED;
1860
1861         seq=get_last_seq(scene);
1862         if (seq && seq->flag & SELECT) { /* avoid a loop since this is likely to be selected */
1863                 nothingSelected = FALSE;
1864         } else {
1865                 for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1866                         if (seq->flag & SELECT) {
1867                                 nothingSelected = FALSE;
1868                                 break;
1869                         }
1870                 }
1871         }
1872
1873         if (nothingSelected)
1874                 return OPERATOR_FINISHED;
1875
1876         /* free imbufs of all dependent strips */
1877         for(seq=ed->seqbasep->first; seq; seq=seq->next)
1878                 if(seq->flag & SELECT)
1879                         update_changed_seq_and_deps(scene, seq, 1, 0);
1880
1881         /* for effects, try to find a replacement input */
1882         for(seq=ed->seqbasep->first; seq; seq=seq->next)
1883                 if((seq->type & SEQ_EFFECT) && !(seq->flag & SELECT))
1884                         del_seq_find_replace_recurs(scene, seq);
1885
1886         /* delete all selected strips */
1887         recurs_del_seq_flag(scene, ed->seqbasep, SELECT, 0);
1888
1889         /* updates lengths etc */
1890         seq= ed->seqbasep->first;
1891         while(seq) {
1892                 calc_sequence(seq);
1893                 seq= seq->next;
1894         }
1895
1896         /* free parent metas */
1897         ms= ed->metastack.last;
1898         while(ms) {
1899                 ms->parseq->strip->len= 0;              /* force new alloc */
1900                 calc_sequence(ms->parseq);
1901                 ms= ms->prev;
1902         }
1903
1904         //ED_area_tag_redraw(CTX_wm_area(C));
1905         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, NULL); /* redraw other sequencer views */
1906         
1907         return OPERATOR_FINISHED;
1908 }
1909
1910
1911 void SEQUENCER_OT_delete(wmOperatorType *ot)
1912 {
1913
1914         /* identifiers */
1915         ot->name= "Erase Strips";
1916         ot->idname= "SEQUENCER_OT_delete";
1917
1918         /* api callbacks */
1919         ot->invoke= WM_operator_confirm;
1920         ot->exec= sequencer_delete_exec;
1921
1922         ot->poll= ED_operator_sequencer_active;
1923         
1924         /* flags */
1925         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1926 }
1927
1928
1929 /* separate_images operator */
1930 static int sequencer_separate_images_exec(bContext *C, wmOperator *op)
1931 {
1932         Scene *scene= CTX_data_scene(C);
1933         Editing *ed= seq_give_editing(scene, FALSE);
1934         
1935         Sequence *seq, *seq_new, *seq_next;
1936         Strip *strip_new;
1937         StripElem *se, *se_new;
1938         int start_ofs, cfra, frame_end;
1939         static int step= 1;
1940
1941 //      add_numbut(0, NUM|INT, "Image Duration:", 1, 256, &step, NULL);
1942 //      if (!do_clever_numbuts("Separate Images", 1, REDRAW))
1943 //              return;
1944
1945         if(ed==NULL)
1946                 return OPERATOR_CANCELLED;
1947
1948         seq= ed->seqbasep->first;
1949
1950         while (seq) {
1951                 if((seq->flag & SELECT) && (seq->type == SEQ_IMAGE) && (seq->len > 1)) {
1952                         /* remove seq so overlap tests dont conflict,
1953                         see seq_free_sequence below for the real free'ing */
1954                         seq_next = seq->next;
1955                         BLI_remlink(ed->seqbasep, seq);
1956                         if(seq->ipo) seq->ipo->id.us--;
1957
1958                         start_ofs = cfra = seq_tx_get_final_left(seq, 0);
1959                         frame_end = seq_tx_get_final_right(seq, 0);
1960
1961                         while (cfra < frame_end) {
1962                                 /* new seq */
1963                                 se = give_stripelem(seq, cfra);
1964
1965                                 seq_new= alloc_sequence(ed->seqbasep, start_ofs, seq->machine);
1966                                 seq_new->type= SEQ_IMAGE;
1967                                 seq_new->len = 1;
1968                                 seq_new->endstill = step-1;
1969
1970                                 /* new strip */
1971                                 seq_new->strip= strip_new= MEM_callocN(sizeof(Strip)*1, "strip");
1972                                 strip_new->len= 1;
1973                                 strip_new->us= 1;
1974                                 strncpy(strip_new->dir, seq->strip->dir, FILE_MAXDIR-1);
1975
1976                                 /* new stripdata */
1977                                 strip_new->stripdata= se_new= MEM_callocN(sizeof(StripElem)*1, "stripelem");
1978                                 strncpy(se_new->name, se->name, FILE_MAXFILE-1);
1979                                 calc_sequence(seq_new);
1980                                 seq_new->flag &= ~SEQ_OVERLAP;
1981                                 if (seq_test_overlap(ed->seqbasep, seq_new)) {
1982                                         shuffle_seq(ed->seqbasep, seq_new);
1983                                 }
1984
1985                                 cfra++;
1986                                 start_ofs += step;
1987                         }
1988
1989                         seq_free_sequence(ed, seq);
1990                         seq = seq->next;
1991                 } else {
1992                         seq = seq->next;
1993                 }
1994         }
1995
1996         /* as last: */
1997         sort_seq(scene);
1998         
1999         ED_area_tag_redraw(CTX_wm_area(C));
2000
2001         return OPERATOR_FINISHED;
2002 }
2003
2004
2005 void SEQUENCER_OT_images_separate(wmOperatorType *ot)
2006 {
2007         /* identifiers */
2008         ot->name= "Separate Images";
2009         ot->idname= "SEQUENCER_OT_images_separate";
2010
2011         /* api callbacks */
2012         ot->invoke= WM_operator_confirm;
2013         ot->exec= sequencer_separate_images_exec;
2014
2015         ot->poll= ED_operator_sequencer_active;
2016         
2017         /* flags */
2018         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2019 }
2020
2021
2022 /* META Operators */
2023
2024 /* separate_meta_toggle operator */
2025 static int sequencer_meta_toggle_exec(bContext *C, wmOperator *op)
2026 {
2027         Scene *scene= CTX_data_scene(C);
2028         Editing *ed= seq_give_editing(scene, FALSE);
2029         Sequence *last_seq= get_last_seq(scene);
2030         MetaStack *ms;
2031
2032         if(ed==NULL)
2033                 return OPERATOR_CANCELLED;
2034
2035         if(last_seq && last_seq->type==SEQ_META && last_seq->flag & SELECT) {
2036                 /* Enter Metastrip */
2037                 ms= MEM_mallocN(sizeof(MetaStack), "metastack");
2038                 BLI_addtail(&ed->metastack, ms);
2039                 ms->parseq= last_seq;
2040                 ms->oldbasep= ed->seqbasep;
2041
2042                 ed->seqbasep= &last_seq->seqbase;
2043
2044                 set_last_seq(scene, NULL);
2045
2046         }
2047         else {
2048                 /* Exit Metastrip (if possible) */
2049
2050                 Sequence *seq;
2051
2052                 if(ed->metastack.first==NULL)
2053                         return OPERATOR_CANCELLED;
2054
2055                 ms= ed->metastack.last;
2056                 BLI_remlink(&ed->metastack, ms);
2057
2058                 ed->seqbasep= ms->oldbasep;
2059
2060                 /* recalc all: the meta can have effects connected to it */
2061                 for(seq= ed->seqbasep->first; seq; seq= seq->next)
2062                         calc_sequence(seq);
2063
2064                 set_last_seq(scene, ms->parseq);
2065
2066                 ms->parseq->flag |= SELECT;
2067                 recurs_sel_seq(ms->parseq);
2068
2069                 MEM_freeN(ms);
2070
2071         }
2072
2073         ED_area_tag_redraw(CTX_wm_area(C));
2074         return OPERATOR_FINISHED;
2075 }
2076
2077 void SEQUENCER_OT_meta_toggle(wmOperatorType *ot)
2078 {
2079         /* identifiers */
2080         ot->name= "Toggle Meta Strip";
2081         ot->idname= "SEQUENCER_OT_meta_toggle";
2082
2083         /* api callbacks */
2084         ot->exec= sequencer_meta_toggle_exec;
2085
2086         ot->poll= ED_operator_sequencer_active;
2087         
2088         /* flags */
2089         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2090 }
2091
2092
2093 /* separate_meta_make operator */
2094 static int sequencer_meta_make_exec(bContext *C, wmOperator *op)
2095 {
2096         Scene *scene= CTX_data_scene(C);
2097         Editing *ed= seq_give_editing(scene, FALSE);
2098         
2099         Sequence *seq, *seqm, *next;
2100         
2101         int tot;
2102
2103         if(ed==NULL)
2104                 return OPERATOR_CANCELLED;
2105
2106         /* is there more than 1 select */
2107         tot= 0;
2108         seq= ed->seqbasep->first;
2109         while(seq) {
2110                 if(seq->flag & SELECT) {
2111                         tot++;
2112                         if (seq->type == SEQ_RAM_SOUND) {
2113                                 BKE_report(op->reports, RPT_ERROR, "Can't make Meta Strip from audio");
2114                                 return OPERATOR_CANCELLED;;
2115                         }
2116                 }
2117                 seq= seq->next;
2118         }
2119         if(tot < 1) return OPERATOR_CANCELLED;;
2120
2121
2122         /* test relationships */
2123         seq= ed->seqbasep->first;
2124         while(seq) {
2125                 if(seq->flag & SELECT) {
2126                         if(seq->type & SEQ_EFFECT) {
2127                                 if(seq->seq1 &&
2128                                    (seq->seq1->flag & SELECT)==0) tot= 0;
2129                                 if(seq->seq2 &&
2130                                    (seq->seq2->flag & SELECT)==0) tot= 0;
2131                                 if(seq->seq3 &&
2132                                    (seq->seq3->flag & SELECT)==0) tot= 0;
2133                         }
2134                 }
2135                 else if(seq->type & SEQ_EFFECT) {
2136                         if(seq->seq1 &&
2137                            (seq->seq1->flag & SELECT)) tot= 0;
2138                         if(seq->seq2 &&
2139                            (seq->seq2->flag & SELECT)) tot= 0;
2140                         if(seq->seq3 &&
2141                            (seq->seq3->flag & SELECT)) tot= 0;
2142                 }
2143                 if(tot==0) break;
2144                 seq= seq->next;
2145         }
2146
2147         if(tot==0) {
2148                 BKE_report(op->reports, RPT_ERROR, "Please select all related strips");
2149                 return OPERATOR_CANCELLED;
2150         }
2151
2152         /* remove all selected from main list, and put in meta */
2153
2154         seqm= alloc_sequence(ed->seqbasep, 1, 1);
2155         seqm->type= SEQ_META;
2156         seqm->flag= SELECT;
2157
2158         seq= ed->seqbasep->first;
2159         while(seq) {
2160                 next= seq->next;
2161                 if(seq!=seqm && (seq->flag & SELECT)) {
2162                         BLI_remlink(ed->seqbasep, seq);
2163                         BLI_addtail(&seqm->seqbase, seq);
2164                 }
2165                 seq= next;
2166         }
2167         calc_sequence(seqm);
2168
2169         seqm->strip= MEM_callocN(sizeof(Strip), "metastrip");
2170         seqm->strip->len= seqm->len;
2171         seqm->strip->us= 1;
2172         
2173         set_last_seq(scene, seqm);
2174
2175         if( seq_test_overlap(ed->seqbasep, seqm) ) shuffle_seq(ed->seqbasep, seqm);
2176
2177         ED_area_tag_redraw(CTX_wm_area(C));
2178         return OPERATOR_FINISHED;
2179 }
2180
2181 void SEQUENCER_OT_meta_make(wmOperatorType *ot)
2182 {
2183         /* identifiers */
2184         ot->name= "Make Meta Strip";
2185         ot->idname= "SEQUENCER_OT_meta_make";
2186
2187         /* api callbacks */
2188         ot->invoke= WM_operator_confirm;
2189         ot->exec= sequencer_meta_make_exec;
2190
2191         ot->poll= ED_operator_sequencer_active;
2192         
2193         /* flags */
2194         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2195 }
2196
2197
2198 static int seq_depends_on_meta(Sequence *seq, Sequence *seqm)
2199 {
2200         if (seq == seqm) return 1;
2201         else if (seq->seq1 && seq_depends_on_meta(seq->seq1, seqm)) return 1;
2202         else if (seq->seq2 && seq_depends_on_meta(seq->seq2, seqm)) return 1;
2203         else if (seq->seq3 && seq_depends_on_meta(seq->seq3, seqm)) return 1;
2204         else return 0;
2205 }
2206
2207 /* separate_meta_make operator */
2208 static int sequencer_meta_separate_exec(bContext *C, wmOperator *op)
2209 {
2210         Scene *scene= CTX_data_scene(C);
2211         Editing *ed= seq_give_editing(scene, FALSE);
2212
2213         Sequence *seq, *last_seq = get_last_seq(scene); /* last_seq checks ed==NULL */
2214
2215         if(last_seq==NULL || last_seq->type!=SEQ_META)
2216                 return OPERATOR_CANCELLED;
2217
2218         addlisttolist(ed->seqbasep, &last_seq->seqbase);
2219
2220         last_seq->seqbase.first= 0;
2221         last_seq->seqbase.last= 0;
2222
2223         BLI_remlink(ed->seqbasep, last_seq);
2224         seq_free_sequence(ed, last_seq);
2225
2226         /* emtpy meta strip, delete all effects depending on it */
2227         for(seq=ed->seqbasep->first; seq; seq=seq->next)
2228                 if((seq->type & SEQ_EFFECT) && seq_depends_on_meta(seq, last_seq))
2229                         seq->flag |= SEQ_FLAG_DELETE;
2230
2231         recurs_del_seq_flag(scene, ed->seqbasep, SEQ_FLAG_DELETE, 0);
2232
2233         /* test for effects and overlap */
2234         SEQP_BEGIN(ed, seq) {
2235                 if(seq->flag & SELECT) {
2236                         seq->flag &= ~SEQ_OVERLAP;
2237                         if( seq_test_overlap(ed->seqbasep, seq) ) {
2238                                 shuffle_seq(ed->seqbasep, seq);
2239                         }
2240                 }
2241         }
2242         SEQ_END;
2243
2244         sort_seq(scene);
2245
2246         ED_area_tag_redraw(CTX_wm_area(C));
2247         return OPERATOR_FINISHED;
2248 }
2249
2250 void SEQUENCER_OT_meta_separate(wmOperatorType *ot)
2251 {
2252         /* identifiers */
2253         ot->name= "UnMeta Strip";
2254         ot->idname= "SEQUENCER_OT_meta_separate";
2255
2256         /* api callbacks */
2257         ot->invoke= WM_operator_confirm;
2258         ot->exec= sequencer_meta_separate_exec;
2259
2260         ot->poll= ED_operator_sequencer_active;
2261         
2262         /* flags */
2263         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2264 }
2265
2266 /* view_all operator */
2267 static int sequencer_view_all_exec(bContext *C, wmOperator *op)
2268 {
2269         Scene *scene= CTX_data_scene(C);
2270         bScreen *sc= CTX_wm_screen(C);
2271         ScrArea *area= CTX_wm_area(C);
2272         ARegion *ar= CTX_wm_region(C);
2273         SpaceSeq *sseq= area->spacedata.first;
2274         View2D *v2d= UI_view2d_fromcontext(C);
2275
2276         if (sseq->mainb==SEQ_DRAW_SEQUENCE) {
2277                 v2d->cur= v2d->tot;
2278                 UI_view2d_curRect_validate(v2d);
2279                 UI_view2d_sync(sc, area, v2d, V2D_LOCK_COPY);
2280         } else {
2281                 /* Like zooming on an image view */
2282                 float zoomX, zoomY;
2283                 int width, height, imgwidth, imgheight;
2284
2285                 width = ar->winx;
2286                 height = ar->winy;
2287
2288                 seq_reset_imageofs(sseq);
2289
2290                 imgwidth= (scene->r.size*scene->r.xsch)/100;
2291                 imgheight= (scene->r.size*scene->r.ysch)/100;
2292
2293                 /* Apply aspect, dosnt need to be that accurate */
2294                 imgwidth= (int)(imgwidth * ((float)scene->r.xasp / (float)scene->r.yasp));
2295
2296                 if (((imgwidth >= width) || (imgheight >= height)) &&
2297                         ((width > 0) && (height > 0))) {
2298
2299                         /* Find the zoom value that will fit the image in the image space */
2300                         zoomX = ((float)width) / ((float)imgwidth);
2301                         zoomY = ((float)height) / ((float)imgheight);
2302                         sseq->zoom= (zoomX < zoomY) ? zoomX : zoomY;
2303
2304                         sseq->zoom = 1.0f / power_of_2(1/ MIN2(zoomX, zoomY) );
2305                 }
2306                 else {
2307                         sseq->zoom= 1.0f;
2308                 }
2309         }
2310
2311
2312         ED_area_tag_redraw(CTX_wm_area(C));
2313         return OPERATOR_FINISHED;
2314 }
2315
2316 void SEQUENCER_OT_view_all(wmOperatorType *ot)
2317 {
2318         /* identifiers */
2319         ot->name= "View All";
2320         ot->idname= "SEQUENCER_OT_view_all";
2321
2322         /* api callbacks */
2323         ot->exec= sequencer_view_all_exec;
2324
2325         ot->poll= ED_operator_sequencer_active;
2326         
2327         /* flags */
2328         ot->flag= OPTYPE_REGISTER;
2329 }
2330
2331
2332 /* view_all operator */
2333 static int sequencer_view_selected_exec(bContext *C, wmOperator *op)
2334 {
2335         Scene *scene= CTX_data_scene(C);
2336         View2D *v2d= UI_view2d_fromcontext(C);
2337         ScrArea *area= CTX_wm_area(C);
2338         bScreen *sc= CTX_wm_screen(C);
2339         Editing *ed= seq_give_editing(scene, FALSE);
2340         Sequence *seq;
2341
2342         int xmin=  MAXFRAME*2;
2343         int xmax= -MAXFRAME*2;
2344         int ymin=  MAXSEQ+1;
2345         int ymax= 0;
2346         int orig_height;
2347         int ymid;
2348         int ymargin= 1;
2349         int xmargin= FPS;
2350
2351         if(ed==NULL)
2352                 return OPERATOR_CANCELLED;
2353
2354         for(seq=ed->seqbasep->first; seq; seq=seq->next) {
2355                 if(seq->flag & SELECT) {
2356                         xmin= MIN2(xmin, seq->startdisp);
2357                         xmax= MAX2(xmax, seq->enddisp);
2358
2359                         ymin= MIN2(ymin, seq->machine);
2360                         ymax= MAX2(ymax, seq->machine);
2361                 }
2362         }
2363
2364         if (ymax != 0) {
2365                 
2366                 xmax += xmargin;
2367                 xmin -= xmargin;
2368                 ymax += ymargin;
2369                 ymin -= ymargin;
2370
2371                 orig_height= v2d->cur.ymax - v2d->cur.ymin;
2372
2373                 v2d->cur.xmin= xmin;
2374                 v2d->cur.xmax= xmax;
2375
2376                 v2d->cur.ymin= ymin;
2377                 v2d->cur.ymax= ymax;
2378
2379                 /* only zoom out vertically */
2380                 if (orig_height > v2d->cur.ymax - v2d->cur.ymin) {
2381                         ymid= (v2d->cur.ymax + v2d->cur.ymin) / 2;
2382
2383                         v2d->cur.ymin= ymid - (orig_height/2);
2384                         v2d->cur.ymax= ymid + (orig_height/2);
2385                 }
2386
2387                 UI_view2d_curRect_validate(v2d);
2388                 UI_view2d_sync(sc, area, v2d, V2D_LOCK_COPY);
2389
2390                 ED_area_tag_redraw(CTX_wm_area(C));
2391         }
2392         
2393         return OPERATOR_FINISHED;
2394 }
2395
2396 void SEQUENCER_OT_view_selected(wmOperatorType *ot)
2397 {
2398         /* identifiers */
2399         ot->name= "View Selected";
2400         ot->idname= "SEQUENCER_OT_view_selected";
2401
2402         /* api callbacks */
2403         ot->exec= sequencer_view_selected_exec;
2404
2405         ot->poll= ED_operator_sequencer_active;
2406         
2407         /* flags */
2408         ot->flag= OPTYPE_REGISTER;
2409 }