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