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