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