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