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