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