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