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