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