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