a58bec9eeb989f7fda0ca80f50332b8a0a76a371
[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 /* separate_images operator */
1573 static int sequencer_separate_images_exec(bContext *C, wmOperator *op)
1574 {
1575         Scene *scene= CTX_data_scene(C);
1576         Editing *ed= seq_give_editing(scene, FALSE);
1577         
1578         Sequence *seq, *seq_new;
1579         Strip *strip_new;
1580         StripElem *se, *se_new;
1581         int start_ofs, cfra, frame_end;
1582         int step= RNA_int_get(op->ptr, "length");
1583
1584         seq= ed->seqbasep->first; /* poll checks this is valid */
1585
1586         while (seq) {
1587                 if((seq->flag & SELECT) && (seq->type == SEQ_IMAGE) && (seq->len > 1)) {
1588                         /* remove seq so overlap tests dont conflict,
1589                         see seq_free_sequence below for the real free'ing */
1590                         BLI_remlink(ed->seqbasep, seq);
1591                         /* if(seq->ipo) seq->ipo->id.us--; */
1592                         /* XXX, remove fcurve and assign to split image strips */
1593
1594                         start_ofs = cfra = seq_tx_get_final_left(seq, 0);
1595                         frame_end = seq_tx_get_final_right(seq, 0);
1596
1597                         while (cfra < frame_end) {
1598                                 /* new seq */
1599                                 se = give_stripelem(seq, cfra);
1600
1601                                 seq_new= seq_dupli_recursive(scene, scene, seq, SEQ_DUPE_UNIQUE_NAME);
1602                                 BLI_addtail(ed->seqbasep, seq_new);
1603
1604                                 seq_new->start= start_ofs;
1605                                 seq_new->type= SEQ_IMAGE;
1606                                 seq_new->len = 1;
1607                                 seq_new->endstill = step-1;
1608
1609                                 /* new strip */
1610                                 strip_new= seq_new->strip;
1611                                 strip_new->len= 1;
1612                                 strip_new->us= 1;
1613
1614                                 /* new stripdata */
1615                                 se_new= strip_new->stripdata;
1616                                 BLI_strncpy(se_new->name, se->name, sizeof(se_new->name));
1617                                 calc_sequence(scene, seq_new);
1618
1619                                 if(step > 1) {
1620                                         seq_new->flag &= ~SEQ_OVERLAP;
1621                                         if (seq_test_overlap(ed->seqbasep, seq_new)) {
1622                                                 shuffle_seq(ed->seqbasep, seq_new, scene);
1623                                         }
1624                                 }
1625
1626                                 /* XXX, COPY FCURVES */
1627
1628                                 cfra++;
1629                                 start_ofs += step;
1630                         }
1631
1632                         seq_free_sequence(scene, seq);
1633                         seq = seq->next;
1634                 } else {
1635                         seq = seq->next;
1636                 }
1637         }
1638
1639         /* as last: */
1640         sort_seq(scene);
1641         
1642         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1643
1644         return OPERATOR_FINISHED;
1645 }
1646
1647
1648 void SEQUENCER_OT_images_separate(wmOperatorType *ot)
1649 {
1650         /* identifiers */
1651         ot->name= "Separate Images";
1652         ot->idname= "SEQUENCER_OT_images_separate";
1653         ot->description="On image sequences strips, it return a strip for each image";
1654         
1655         /* api callbacks */
1656         ot->exec= sequencer_separate_images_exec;
1657         ot->poll= sequencer_edit_poll;
1658         
1659         /* flags */
1660         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1661
1662         RNA_def_int(ot->srna, "length", 1, 1, 1000, "Length", "Length of each frame", 1, INT_MAX);
1663 }
1664
1665
1666 /* META Operators */
1667
1668 /* separate_meta_toggle operator */
1669 static int sequencer_meta_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1670 {
1671         Scene *scene= CTX_data_scene(C);
1672         Editing *ed= seq_give_editing(scene, FALSE);
1673         Sequence *last_seq= seq_active_get(scene);
1674         MetaStack *ms;
1675
1676         if(last_seq && last_seq->type==SEQ_META && last_seq->flag & SELECT) {
1677                 /* Enter Metastrip */
1678                 ms= MEM_mallocN(sizeof(MetaStack), "metastack");
1679                 BLI_addtail(&ed->metastack, ms);
1680                 ms->parseq= last_seq;
1681                 ms->oldbasep= ed->seqbasep;
1682
1683                 ed->seqbasep= &last_seq->seqbase;
1684
1685                 seq_active_set(scene, NULL);
1686
1687         }
1688         else {
1689                 /* Exit Metastrip (if possible) */
1690
1691                 Sequence *seq;
1692
1693                 if(ed->metastack.first==NULL)
1694                         return OPERATOR_CANCELLED;
1695
1696                 ms= ed->metastack.last;
1697                 BLI_remlink(&ed->metastack, ms);
1698
1699                 ed->seqbasep= ms->oldbasep;
1700
1701                 /* recalc all: the meta can have effects connected to it */
1702                 for(seq= ed->seqbasep->first; seq; seq= seq->next)
1703                         calc_sequence(scene, seq);
1704
1705                 seq_active_set(scene, ms->parseq);
1706
1707                 ms->parseq->flag |= SELECT;
1708                 recurs_sel_seq(ms->parseq);
1709
1710                 MEM_freeN(ms);
1711
1712         }
1713
1714         seq_update_muting(scene, ed);
1715         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1716
1717         return OPERATOR_FINISHED;
1718 }
1719
1720 void SEQUENCER_OT_meta_toggle(wmOperatorType *ot)
1721 {
1722         /* identifiers */
1723         ot->name= "Toggle Meta Strip";
1724         ot->idname= "SEQUENCER_OT_meta_toggle";
1725         ot->description="Toggle a metastrip (to edit enclosed strips)";
1726         
1727         /* api callbacks */
1728         ot->exec= sequencer_meta_toggle_exec;
1729         ot->poll= sequencer_edit_poll;
1730         
1731         /* flags */
1732         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1733 }
1734
1735
1736 /* separate_meta_make operator */
1737 static int sequencer_meta_make_exec(bContext *C, wmOperator *op)
1738 {
1739         Scene *scene= CTX_data_scene(C);
1740         Editing *ed= seq_give_editing(scene, FALSE);
1741         
1742         Sequence *seq, *seqm, *next, *last_seq = seq_active_get(scene);
1743         int channel_max= 1;
1744
1745         if(seqbase_isolated_sel_check(ed->seqbasep)==FALSE) {
1746                 BKE_report(op->reports, RPT_ERROR, "Please select all related strips");
1747                 return OPERATOR_CANCELLED;
1748         }
1749
1750         /* remove all selected from main list, and put in meta */
1751
1752         seqm= alloc_sequence(ed->seqbasep, 1, 1); /* channel number set later */
1753         strcpy(seqm->name+2, "MetaStrip");
1754         seqm->type= SEQ_META;
1755         seqm->flag= SELECT;
1756
1757         seq= ed->seqbasep->first;
1758         while(seq) {
1759                 next= seq->next;
1760                 if(seq!=seqm && (seq->flag & SELECT)) {
1761                         channel_max= MAX2(seq->machine, channel_max);
1762                         BLI_remlink(ed->seqbasep, seq);
1763                         BLI_addtail(&seqm->seqbase, seq);
1764                 }
1765                 seq= next;
1766         }
1767         seqm->machine= last_seq ? last_seq->machine : channel_max;
1768         calc_sequence(scene, seqm);
1769
1770         seqm->strip= MEM_callocN(sizeof(Strip), "metastrip");
1771         seqm->strip->len= seqm->len;
1772         seqm->strip->us= 1;
1773         
1774         seq_active_set(scene, seqm);
1775
1776         if( seq_test_overlap(ed->seqbasep, seqm) ) shuffle_seq(ed->seqbasep, seqm, scene);
1777
1778         seq_update_muting(scene, ed);
1779
1780         seqbase_unique_name_recursive(&scene->ed->seqbase, seqm);
1781
1782         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1783
1784         return OPERATOR_FINISHED;
1785 }
1786
1787 void SEQUENCER_OT_meta_make(wmOperatorType *ot)
1788 {
1789         /* identifiers */
1790         ot->name= "Make Meta Strip";
1791         ot->idname= "SEQUENCER_OT_meta_make";
1792         ot->description="Group selected strips into a metastrip";
1793         
1794         /* api callbacks */
1795         ot->invoke= WM_operator_confirm;
1796         ot->exec= sequencer_meta_make_exec;
1797         ot->poll= sequencer_edit_poll;
1798         
1799         /* flags */
1800         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1801 }
1802
1803
1804 static int seq_depends_on_meta(Sequence *seq, Sequence *seqm)
1805 {
1806         if (seq == seqm) return 1;
1807         else if (seq->seq1 && seq_depends_on_meta(seq->seq1, seqm)) return 1;
1808         else if (seq->seq2 && seq_depends_on_meta(seq->seq2, seqm)) return 1;
1809         else if (seq->seq3 && seq_depends_on_meta(seq->seq3, seqm)) return 1;
1810         else return 0;
1811 }
1812
1813 /* separate_meta_make operator */
1814 static int sequencer_meta_separate_exec(bContext *C, wmOperator *UNUSED(op))
1815 {
1816         Scene *scene= CTX_data_scene(C);
1817         Editing *ed= seq_give_editing(scene, FALSE);
1818
1819         Sequence *seq, *last_seq = seq_active_get(scene); /* last_seq checks ed==NULL */
1820
1821         if(last_seq==NULL || last_seq->type!=SEQ_META)
1822                 return OPERATOR_CANCELLED;
1823
1824         BLI_movelisttolist(ed->seqbasep, &last_seq->seqbase);
1825
1826         last_seq->seqbase.first= NULL;
1827         last_seq->seqbase.last= NULL;
1828
1829         BLI_remlink(ed->seqbasep, last_seq);
1830         seq_free_sequence(scene, last_seq);
1831
1832         /* emtpy meta strip, delete all effects depending on it */
1833         for(seq=ed->seqbasep->first; seq; seq=seq->next)
1834                 if((seq->type & SEQ_EFFECT) && seq_depends_on_meta(seq, last_seq))
1835                         seq->flag |= SEQ_FLAG_DELETE;
1836
1837         recurs_del_seq_flag(scene, ed->seqbasep, SEQ_FLAG_DELETE, 0);
1838
1839         /* test for effects and overlap
1840          * dont use SEQP_BEGIN since that would be recursive */
1841         for(seq= ed->seqbasep->first; seq; seq= seq->next) {
1842                 if(seq->flag & SELECT) {
1843                         seq->flag &= ~SEQ_OVERLAP;
1844                         if(seq_test_overlap(ed->seqbasep, seq)) {
1845                                 shuffle_seq(ed->seqbasep, seq, scene);
1846                         }
1847                 }
1848         }
1849
1850         sort_seq(scene);
1851         seq_update_muting(scene, ed);
1852
1853         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1854
1855         return OPERATOR_FINISHED;
1856 }
1857
1858 void SEQUENCER_OT_meta_separate(wmOperatorType *ot)
1859 {
1860         /* identifiers */
1861         ot->name= "UnMeta Strip";
1862         ot->idname= "SEQUENCER_OT_meta_separate";
1863         ot->description="Put the contents of a metastrip back in the sequencer";
1864         
1865         /* api callbacks */
1866         ot->invoke= WM_operator_confirm;
1867         ot->exec= sequencer_meta_separate_exec;
1868         ot->poll= sequencer_edit_poll;
1869         
1870         /* flags */
1871         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1872 }
1873
1874 /* view_all operator */
1875 static int sequencer_view_all_exec(bContext *C, wmOperator *UNUSED(op))
1876 {
1877         //Scene *scene= CTX_data_scene(C);
1878         bScreen *sc= CTX_wm_screen(C);
1879         ScrArea *area= CTX_wm_area(C);
1880         //ARegion *ar= CTX_wm_region(C);
1881         View2D *v2d= UI_view2d_fromcontext(C);
1882
1883         v2d->cur= v2d->tot;
1884         UI_view2d_curRect_validate(v2d);
1885         UI_view2d_sync(sc, area, v2d, V2D_LOCK_COPY);
1886         
1887         ED_area_tag_redraw(CTX_wm_area(C));
1888         return OPERATOR_FINISHED;
1889 }
1890
1891 void SEQUENCER_OT_view_all(wmOperatorType *ot)
1892 {
1893         /* identifiers */
1894         ot->name= "View All";
1895         ot->idname= "SEQUENCER_OT_view_all";
1896         ot->description="View all the strips in the sequencer";
1897         
1898         /* api callbacks */
1899         ot->exec= sequencer_view_all_exec;
1900         ot->poll= ED_operator_sequencer_active;
1901         
1902         /* flags */
1903         ot->flag= OPTYPE_REGISTER;
1904 }
1905
1906 /* view_all operator */
1907 static int sequencer_view_all_preview_exec(bContext *C, wmOperator *UNUSED(op))
1908 {
1909         bScreen *sc= CTX_wm_screen(C);
1910         ScrArea *area= CTX_wm_area(C);
1911 #if 0
1912         ARegion *ar= CTX_wm_region(C);
1913         SpaceSeq *sseq= area->spacedata.first;
1914         Scene *scene= CTX_data_scene(C);
1915 #endif
1916         View2D *v2d= UI_view2d_fromcontext(C);
1917
1918         v2d->cur= v2d->tot;
1919         UI_view2d_curRect_validate(v2d);
1920         UI_view2d_sync(sc, area, v2d, V2D_LOCK_COPY);
1921         
1922 #if 0
1923         /* Like zooming on an image view */
1924         float zoomX, zoomY;
1925         int width, height, imgwidth, imgheight;
1926
1927         width = ar->winx;
1928         height = ar->winy;
1929
1930         seq_reset_imageofs(sseq);
1931
1932         imgwidth= (scene->r.size*scene->r.xsch)/100;
1933         imgheight= (scene->r.size*scene->r.ysch)/100;
1934
1935         /* Apply aspect, dosnt need to be that accurate */
1936         imgwidth= (int)(imgwidth * (scene->r.xasp / scene->r.yasp));
1937
1938         if (((imgwidth >= width) || (imgheight >= height)) &&
1939                 ((width > 0) && (height > 0))) {
1940
1941                 /* Find the zoom value that will fit the image in the image space */
1942                 zoomX = ((float)width) / ((float)imgwidth);
1943                 zoomY = ((float)height) / ((float)imgheight);
1944                 sseq->zoom= (zoomX < zoomY) ? zoomX : zoomY;
1945
1946                 sseq->zoom = 1.0f / power_of_2(1/ MIN2(zoomX, zoomY) );
1947         }
1948         else {
1949                 sseq->zoom= 1.0f;
1950         }
1951 #endif
1952
1953         ED_area_tag_redraw(CTX_wm_area(C));
1954         return OPERATOR_FINISHED;
1955 }
1956
1957 void SEQUENCER_OT_view_all_preview(wmOperatorType *ot)
1958 {
1959         /* identifiers */
1960         ot->name= "View All";
1961         ot->idname= "SEQUENCER_OT_view_all_preview";
1962         ot->description="Zoom preview to fit in the area";
1963         
1964         /* api callbacks */
1965         ot->exec= sequencer_view_all_preview_exec;
1966         ot->poll= ED_operator_sequencer_active;
1967         
1968         /* flags */
1969         ot->flag= OPTYPE_REGISTER;
1970 }
1971
1972
1973 static int sequencer_view_zoom_ratio_exec(bContext *C, wmOperator *op)
1974 {
1975         RenderData *r= &CTX_data_scene(C)->r;
1976         View2D *v2d= UI_view2d_fromcontext(C);
1977
1978         float ratio= RNA_float_get(op->ptr, "ratio");
1979
1980         float winx= (int)(r->size * r->xsch)/100;
1981         float winy= (int)(r->size * r->ysch)/100;
1982
1983         float facx= (v2d->mask.xmax - v2d->mask.xmin) / winx;
1984         float facy= (v2d->mask.ymax - v2d->mask.ymin) / winy;
1985
1986         BLI_resize_rctf(&v2d->cur, (int)(winx*facx*ratio) + 1, (int)(winy*facy*ratio) + 1);
1987
1988         ED_region_tag_redraw(CTX_wm_region(C));
1989
1990         return OPERATOR_FINISHED;
1991 }
1992
1993 void SEQUENCER_OT_view_zoom_ratio(wmOperatorType *ot)
1994 {
1995         /* identifiers */
1996         ot->name= "Sequencer View Zoom Ratio";
1997         ot->idname= "SEQUENCER_OT_view_zoom_ratio";
1998         ot->description = "Change zoom ratio of sequencer preview";
1999
2000         /* api callbacks */
2001         ot->exec= sequencer_view_zoom_ratio_exec;
2002         ot->poll= ED_operator_sequencer_active;
2003
2004         /* properties */
2005         RNA_def_float(ot->srna, "ratio", 1.0f, 0.0f, FLT_MAX,
2006                 "Ratio", "Zoom ratio, 1.0 is 1:1, higher is zoomed in, lower is zoomed out.", -FLT_MAX, FLT_MAX);
2007 }
2008
2009
2010 #if 0
2011 static EnumPropertyItem view_type_items[] = {
2012                 {SEQ_VIEW_SEQUENCE, "SEQUENCER", ICON_SEQ_SEQUENCER, "Sequencer", ""},
2013                 {SEQ_VIEW_PREVIEW,  "PREVIEW", ICON_SEQ_PREVIEW, "Image Preview", ""},
2014                 {SEQ_VIEW_SEQUENCE_PREVIEW,  "SEQUENCER_PREVIEW", ICON_SEQ_SEQUENCER, "Sequencer and Image Preview", ""},
2015                 {0, NULL, 0, NULL, NULL}};
2016 #endif
2017
2018 /* view_all operator */
2019 static int sequencer_view_toggle_exec(bContext *C, wmOperator *UNUSED(op))
2020 {
2021         SpaceSeq *sseq= (SpaceSeq *)CTX_wm_space_data(C);
2022
2023         sseq->view++;
2024         if (sseq->view > SEQ_VIEW_SEQUENCE_PREVIEW) sseq->view = SEQ_VIEW_SEQUENCE;
2025
2026         ED_sequencer_update_view(C, sseq->view);
2027
2028         return OPERATOR_FINISHED;
2029 }
2030
2031 void SEQUENCER_OT_view_toggle(wmOperatorType *ot)
2032 {
2033         /* identifiers */
2034         ot->name= "View Toggle";
2035         ot->idname= "SEQUENCER_OT_view_toggle";
2036         ot->description="Toggle between sequencer views (sequence, preview, both)";
2037         
2038         /* api callbacks */
2039         ot->exec= sequencer_view_toggle_exec;
2040         ot->poll= ED_operator_sequencer_active;
2041         
2042         /* flags */
2043         ot->flag= OPTYPE_REGISTER;
2044 }
2045
2046
2047 /* view_selected operator */
2048 static int sequencer_view_selected_exec(bContext *C, wmOperator *UNUSED(op))
2049 {
2050         Scene *scene= CTX_data_scene(C);
2051         View2D *v2d= UI_view2d_fromcontext(C);
2052         ScrArea *area= CTX_wm_area(C);
2053         bScreen *sc= CTX_wm_screen(C);
2054         Editing *ed= seq_give_editing(scene, FALSE);
2055         Sequence *seq;
2056
2057         int xmin=  MAXFRAME*2;
2058         int xmax= -MAXFRAME*2;
2059         int ymin=  MAXSEQ+1;
2060         int ymax= 0;
2061         int orig_height;
2062         int ymid;
2063         int ymargin= 1;
2064         int xmargin= FPS;
2065
2066         if(ed==NULL)
2067                 return OPERATOR_CANCELLED;
2068
2069         for(seq=ed->seqbasep->first; seq; seq=seq->next) {
2070                 if(seq->flag & SELECT) {
2071                         xmin= MIN2(xmin, seq->startdisp);
2072                         xmax= MAX2(xmax, seq->enddisp);
2073
2074                         ymin= MIN2(ymin, seq->machine);
2075                         ymax= MAX2(ymax, seq->machine);
2076                 }
2077         }
2078
2079         if (ymax != 0) {
2080                 
2081                 xmax += xmargin;
2082                 xmin -= xmargin;
2083                 ymax += ymargin;
2084                 ymin -= ymargin;
2085
2086                 orig_height= v2d->cur.ymax - v2d->cur.ymin;
2087
2088                 v2d->cur.xmin= xmin;
2089                 v2d->cur.xmax= xmax;
2090
2091                 v2d->cur.ymin= ymin;
2092                 v2d->cur.ymax= ymax;
2093
2094                 /* only zoom out vertically */
2095                 if (orig_height > v2d->cur.ymax - v2d->cur.ymin) {
2096                         ymid= (v2d->cur.ymax + v2d->cur.ymin) / 2;
2097
2098                         v2d->cur.ymin= ymid - (orig_height/2);
2099                         v2d->cur.ymax= ymid + (orig_height/2);
2100                 }
2101
2102                 UI_view2d_curRect_validate(v2d);
2103                 UI_view2d_sync(sc, area, v2d, V2D_LOCK_COPY);
2104
2105                 ED_area_tag_redraw(CTX_wm_area(C));
2106         }
2107         
2108         return OPERATOR_FINISHED;
2109 }
2110
2111 void SEQUENCER_OT_view_selected(wmOperatorType *ot)
2112 {
2113         /* identifiers */
2114         ot->name= "View Selected";
2115         ot->idname= "SEQUENCER_OT_view_selected";
2116         ot->description="Zoom the sequencer on the selected strips";
2117         
2118         /* api callbacks */
2119         ot->exec= sequencer_view_selected_exec;
2120         ot->poll= ED_operator_sequencer_active;
2121         
2122         /* flags */
2123         ot->flag= OPTYPE_REGISTER;
2124 }
2125
2126
2127 static int find_next_prev_edit(Scene *scene, int cfra, int side)
2128 {
2129         Editing *ed= seq_give_editing(scene, FALSE);
2130         Sequence *seq,*best_seq = NULL,*frame_seq = NULL;
2131         
2132         int dist, best_dist;
2133         best_dist = MAXFRAME*2;
2134
2135         if(ed==NULL) return cfra;
2136         
2137         for(seq= ed->seqbasep->first; seq; seq= seq->next) {
2138                 dist = MAXFRAME*2;
2139                         
2140                 switch (side) {
2141                         case SEQ_SIDE_LEFT:
2142                                 if (seq->startdisp < cfra) {
2143                                         dist = cfra - seq->startdisp;
2144                                 }
2145                                 break;
2146                         case SEQ_SIDE_RIGHT:
2147                                 if (seq->startdisp > cfra) {
2148                                         dist = seq->startdisp - cfra;
2149                                 } else if (seq->startdisp == cfra) {
2150                                         frame_seq=seq;
2151                                 }
2152                                 break;
2153                 }
2154
2155                 if (dist < best_dist) {
2156                         best_dist = dist;
2157                         best_seq = seq;
2158                 }
2159         }
2160
2161         /* if no sequence to the right is found and the
2162            frame is on the start of the last sequence,
2163            move to the end of the last sequence */
2164         if (frame_seq) cfra = frame_seq->enddisp;
2165
2166         return best_seq ? best_seq->startdisp : cfra;
2167 }
2168
2169 static int next_prev_edit_internal(Scene *scene, int side)
2170 {
2171         int change=0;
2172         int cfra = CFRA;
2173         int nfra= find_next_prev_edit(scene, cfra, side);
2174         
2175         if (nfra != cfra) {
2176                 CFRA = nfra;
2177                 change= 1;
2178         }
2179
2180         return change;
2181 }
2182
2183 /* move frame to next edit point operator */
2184 static int sequencer_next_edit_exec(bContext *C, wmOperator *UNUSED(op))
2185 {
2186         Scene *scene= CTX_data_scene(C);
2187         
2188         if(!next_prev_edit_internal(scene, SEQ_SIDE_RIGHT))
2189                 return OPERATOR_CANCELLED;
2190
2191         WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene);
2192
2193         return OPERATOR_FINISHED;
2194 }
2195
2196 void SEQUENCER_OT_next_edit(wmOperatorType *ot)
2197 {
2198         /* identifiers */
2199         ot->name= "Next Edit";
2200         ot->idname= "SEQUENCER_OT_next_edit";
2201         ot->description="Move frame to next edit point";
2202         
2203         /* api callbacks */
2204         ot->exec= sequencer_next_edit_exec;
2205         ot->poll= sequencer_edit_poll;
2206         
2207         /* flags */
2208         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2209         
2210         /* properties */
2211 }
2212
2213 /* move frame to previous edit point operator */
2214 static int sequencer_previous_edit_exec(bContext *C, wmOperator *UNUSED(op))
2215 {
2216         Scene *scene= CTX_data_scene(C);
2217         
2218         if(!next_prev_edit_internal(scene, SEQ_SIDE_LEFT))
2219                 return OPERATOR_CANCELLED;
2220
2221         WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene);
2222         
2223         return OPERATOR_FINISHED;
2224 }
2225
2226 void SEQUENCER_OT_previous_edit(wmOperatorType *ot)
2227 {
2228         /* identifiers */
2229         ot->name= "Previous Edit";
2230         ot->idname= "SEQUENCER_OT_previous_edit";
2231         ot->description="Move frame to previous edit point";
2232         
2233         /* api callbacks */
2234         ot->exec= sequencer_previous_edit_exec;
2235         ot->poll= sequencer_edit_poll;
2236         
2237         /* flags */
2238         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2239         
2240         /* properties */
2241 }
2242
2243 static void swap_sequence(Scene* scene, Sequence* seqa, Sequence* seqb)
2244 {
2245         int gap = seqb->startdisp - seqa->enddisp;
2246         seqb->start = (seqb->start - seqb->startdisp) + seqa->startdisp;
2247         calc_sequence(scene, seqb);
2248         seqa->start = (seqa->start - seqa->startdisp) + seqb->enddisp + gap;
2249         calc_sequence(scene, seqa);
2250 }
2251
2252 #if 0
2253 static Sequence* sequence_find_parent(Scene* scene, Sequence* child)
2254 {
2255         Editing *ed= seq_give_editing(scene, FALSE);
2256         Sequence *parent= NULL;
2257         Sequence *seq;
2258
2259         if(ed==NULL) return NULL;
2260
2261         for(seq= ed->seqbasep->first; seq; seq= seq->next) {
2262                 if ( (seq != child) && seq_is_parent(seq, child) ) {
2263                         parent = seq;
2264                         break;
2265                 }
2266         }
2267
2268         return parent;
2269 }
2270 #endif
2271
2272 static int sequencer_swap_exec(bContext *C, wmOperator *op)
2273 {
2274         Scene *scene= CTX_data_scene(C);
2275         Editing *ed= seq_give_editing(scene, FALSE);
2276         Sequence *active_seq = seq_active_get(scene);
2277         Sequence *seq, *iseq;
2278         int side= RNA_enum_get(op->ptr, "side");
2279
2280         if(active_seq==NULL) return OPERATOR_CANCELLED;
2281
2282         seq = find_next_prev_sequence(scene, active_seq, side, -1);
2283         
2284         if(seq) {
2285                 
2286                 /* disallow effect strips */
2287                 if (get_sequence_effect_num_inputs(seq->type) >= 1 && (seq->effectdata || seq->seq1 || seq->seq2 || seq->seq3))
2288                         return OPERATOR_CANCELLED;
2289                 if ((get_sequence_effect_num_inputs(active_seq->type) >= 1) && (active_seq->effectdata || active_seq->seq1 || active_seq->seq2 || active_seq->seq3))
2290                         return OPERATOR_CANCELLED;
2291
2292                 switch (side) {
2293                         case SEQ_SIDE_LEFT: 
2294                                 swap_sequence(scene, seq, active_seq);
2295                                 break;
2296                         case SEQ_SIDE_RIGHT: 
2297                                 swap_sequence(scene, active_seq, seq);
2298                                 break;
2299                 }
2300
2301                 // XXX - should be a generic function
2302                 for(iseq= scene->ed->seqbasep->first; iseq; iseq= iseq->next) {
2303                         if((iseq->type & SEQ_EFFECT) && (seq_is_parent(iseq, active_seq) || seq_is_parent(iseq, seq))) {
2304                                 calc_sequence(scene, iseq);
2305                         }
2306                 }
2307
2308                 /* do this in a new loop since both effects need to be calculated first */
2309                 for(iseq= scene->ed->seqbasep->first; iseq; iseq= iseq->next) {
2310                         if((iseq->type & SEQ_EFFECT) && (seq_is_parent(iseq, active_seq) || seq_is_parent(iseq, seq))) {
2311                                 /* this may now overlap */
2312                                 if( seq_test_overlap(ed->seqbasep, iseq) ) {
2313                                         shuffle_seq(ed->seqbasep, iseq, scene);
2314                                 }
2315                         }
2316                 }
2317
2318
2319
2320                 sort_seq(scene);
2321
2322                 WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
2323
2324                 return OPERATOR_FINISHED;
2325         }
2326
2327         return OPERATOR_CANCELLED;
2328 }
2329
2330 void SEQUENCER_OT_swap(wmOperatorType *ot)
2331 {
2332         /* identifiers */
2333         ot->name= "Swap Strip";
2334         ot->idname= "SEQUENCER_OT_swap";
2335         ot->description="Swap active strip with strip to the left";
2336         
2337         /* api callbacks */
2338         ot->exec= sequencer_swap_exec;
2339         ot->poll= sequencer_edit_poll;
2340         
2341         /* flags */
2342         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2343         
2344         /* properties */
2345         RNA_def_enum(ot->srna, "side", prop_side_lr_types, SEQ_SIDE_RIGHT, "Side", "Side of the strip to swap");
2346 }
2347
2348 static int sequencer_rendersize_exec(bContext *C, wmOperator *UNUSED(op))
2349 {
2350         int retval = OPERATOR_CANCELLED;
2351         Scene *scene= CTX_data_scene(C);
2352         Sequence *active_seq = seq_active_get(scene);
2353         StripElem *se = NULL;
2354
2355         if(active_seq==NULL)
2356                 return OPERATOR_CANCELLED;
2357
2358
2359         if (active_seq->strip) {
2360                 switch (active_seq->type) {
2361                 case SEQ_IMAGE:
2362                         se = give_stripelem(active_seq, scene->r.cfra);
2363                         break;
2364                 case SEQ_MOVIE:
2365                         se = active_seq->strip->stripdata;
2366                         break;
2367                 case SEQ_SCENE:
2368                 case SEQ_META:
2369                 case SEQ_RAM_SOUND:
2370                 case SEQ_HD_SOUND:
2371                 default:
2372                         break;
2373                 }
2374         }
2375
2376         if (se) {
2377                 // prevent setting the render size if sequence values aren't initialized
2378                 if ( (se->orig_width > 0) && (se->orig_height > 0) ) {
2379                         scene->r.xsch= se->orig_width;
2380                         scene->r.ysch= se->orig_height;
2381                         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
2382                         retval = OPERATOR_FINISHED;
2383                 }
2384         }
2385
2386         return retval;
2387 }
2388
2389 void SEQUENCER_OT_rendersize(wmOperatorType *ot)
2390 {
2391         /* identifiers */
2392         ot->name= "Set Render Size";
2393         ot->idname= "SEQUENCER_OT_rendersize";
2394         ot->description="Set render size and aspect from active sequence";
2395         
2396         /* api callbacks */
2397         ot->exec= sequencer_rendersize_exec;
2398         ot->poll= sequencer_edit_poll;
2399         
2400         /* flags */
2401         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2402         
2403         /* properties */
2404 }
2405
2406 static void seq_del_sound(Scene *scene, Sequence *seq)
2407 {
2408         if(seq->type == SEQ_META) {
2409                 Sequence *iseq;
2410                 for(iseq= seq->seqbase.first; iseq; iseq= iseq->next) {
2411                         seq_del_sound(scene, iseq);
2412                 }
2413         }
2414         else if(seq->scene_sound) {
2415                 sound_remove_scene_sound(scene, seq->scene_sound);
2416                 seq->scene_sound = NULL;
2417         }
2418 }
2419
2420 /* TODO, validate scenes */
2421 static int sequencer_copy_exec(bContext *C, wmOperator *op)
2422 {
2423         Scene *scene= CTX_data_scene(C);
2424         Editing *ed= seq_give_editing(scene, FALSE);
2425         Sequence *seq;
2426
2427         ListBase nseqbase= {NULL, NULL};
2428
2429         seq_free_clipboard();
2430
2431         if(seqbase_isolated_sel_check(ed->seqbasep)==FALSE) {
2432                 BKE_report(op->reports, RPT_ERROR, "Please select all related strips");
2433                 return OPERATOR_CANCELLED;
2434         }
2435
2436         seqbase_dupli_recursive(scene, NULL, &nseqbase, ed->seqbasep, SEQ_DUPE_UNIQUE_NAME);
2437
2438         /* To make sure the copied strips have unique names between each other add
2439          * them temporarily to the end of the original seqbase. (bug 25932)
2440          */
2441         if(nseqbase.first) {
2442                 Sequence *seq, *first_seq = nseqbase.first;
2443                 BLI_movelisttolist(ed->seqbasep, &nseqbase);
2444
2445                 for(seq=first_seq; seq; seq=seq->next)
2446                         seq_recursive_apply(seq, apply_unique_name_cb, scene);
2447
2448                 seqbase_clipboard.first = first_seq;
2449                 seqbase_clipboard.last = ed->seqbasep->last;
2450
2451                 if(first_seq->prev) {
2452                         first_seq->prev->next = NULL;
2453                         ed->seqbasep->last = first_seq->prev;
2454                         first_seq->prev = NULL;
2455                 }
2456         }
2457
2458         seqbase_clipboard_frame= scene->r.cfra;
2459
2460         /* Need to remove anything that references the current scene */
2461         for(seq= seqbase_clipboard.first; seq; seq= seq->next) {
2462                 seq_del_sound(scene, seq);
2463         }
2464
2465         return OPERATOR_FINISHED;
2466 }
2467
2468 void SEQUENCER_OT_copy(wmOperatorType *ot)
2469 {
2470         /* identifiers */
2471         ot->name= "Copy";
2472         ot->idname= "SEQUENCER_OT_copy";
2473         ot->description="";
2474
2475         /* api callbacks */
2476         ot->exec= sequencer_copy_exec;
2477         ot->poll= sequencer_edit_poll;
2478
2479         /* flags */
2480         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2481
2482         /* properties */
2483 }
2484
2485 static int sequencer_paste_exec(bContext *C, wmOperator *UNUSED(op))
2486 {
2487         Scene *scene= CTX_data_scene(C);
2488         Editing *ed= seq_give_editing(scene, TRUE); /* create if needed */
2489         ListBase nseqbase = {NULL, NULL};
2490         int ofs;
2491         Sequence *iseq;
2492
2493         deselect_all_seq(scene);
2494         ofs = scene->r.cfra - seqbase_clipboard_frame;
2495
2496         seqbase_dupli_recursive(scene, NULL, &nseqbase, &seqbase_clipboard, SEQ_DUPE_UNIQUE_NAME);
2497
2498         /* transform pasted strips before adding */
2499         if(ofs) {
2500                 for(iseq= nseqbase.first; iseq; iseq= iseq->next) {
2501                         seq_translate(scene, iseq, ofs);
2502                         seq_sound_init(scene, iseq);
2503                 }
2504         }
2505
2506         iseq = nseqbase.first;
2507
2508         BLI_movelisttolist(ed->seqbasep, &nseqbase);
2509
2510         /* make sure the pasted strips have unique names between them */
2511         for(; iseq; iseq=iseq->next)
2512                 seq_recursive_apply(iseq, apply_unique_name_cb, scene);
2513
2514         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
2515
2516         return OPERATOR_FINISHED;
2517 }
2518
2519 void SEQUENCER_OT_paste(wmOperatorType *ot)
2520 {
2521         /* identifiers */
2522         ot->name= "Paste";
2523         ot->idname= "SEQUENCER_OT_paste";
2524         ot->description="";
2525
2526         /* api callbacks */
2527         ot->exec= sequencer_paste_exec;
2528         ot->poll= ED_operator_sequencer_active;
2529
2530         /* flags */
2531         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2532
2533         /* properties */
2534 }
2535
2536 static int sequencer_swap_data_exec(bContext *C, wmOperator *op)
2537 {
2538         Scene *scene= CTX_data_scene(C);
2539         Sequence *seq_act;
2540         Sequence *seq_other;
2541         const char *error_msg;
2542
2543         if(seq_active_pair_get(scene, &seq_act, &seq_other) == 0) {
2544                 BKE_report(op->reports, RPT_ERROR, "Must select 2 strips");
2545                 return OPERATOR_CANCELLED;
2546         }
2547
2548         if(seq_swap(seq_act, seq_other, &error_msg) == 0) {
2549                 BKE_report(op->reports, RPT_ERROR, error_msg);
2550                 return OPERATOR_CANCELLED;
2551         }
2552
2553         sound_remove_scene_sound(scene, seq_act->scene_sound);
2554         sound_remove_scene_sound(scene, seq_other->scene_sound);
2555
2556         seq_act->scene_sound= NULL;
2557         seq_other->scene_sound= NULL;
2558
2559         calc_sequence(scene, seq_act);
2560         calc_sequence(scene, seq_other);
2561
2562         if(seq_act->sound)              sound_add_scene_sound(scene, seq_act, seq_act->startdisp, seq_act->enddisp, seq_act->startofs + seq_act->anim_startofs);
2563         if(seq_other->sound)    sound_add_scene_sound(scene, seq_other, seq_other->startdisp, seq_other->enddisp, seq_other->startofs + seq_other->anim_startofs);
2564
2565         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
2566
2567         return OPERATOR_FINISHED;
2568 }
2569
2570 void SEQUENCER_OT_swap_data(wmOperatorType *ot)
2571 {
2572         /* identifiers */
2573         ot->name= "Sequencer Swap Data";
2574         ot->idname= "SEQUENCER_OT_swap_data";
2575         ot->description="Swap 2 sequencer strips";
2576
2577         /* api callbacks */
2578         ot->exec= sequencer_swap_data_exec;
2579         ot->poll= ED_operator_sequencer_active;
2580
2581         /* flags */
2582         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2583
2584         /* properties */
2585 }
2586
2587 /* borderselect operator */
2588 static int view_ghost_border_exec(bContext *C, wmOperator *op)
2589 {
2590         Scene *scene= CTX_data_scene(C);
2591         Editing *ed= seq_give_editing(scene, FALSE);
2592         View2D *v2d= UI_view2d_fromcontext(C);
2593
2594         rctf rect;
2595
2596         /* convert coordinates of rect to 'tot' rect coordinates */
2597         UI_view2d_region_to_view(v2d, RNA_int_get(op->ptr, "xmin"), RNA_int_get(op->ptr, "ymin"), &rect.xmin, &rect.ymin);
2598         UI_view2d_region_to_view(v2d, RNA_int_get(op->ptr, "xmax"), RNA_int_get(op->ptr, "ymax"), &rect.xmax, &rect.ymax);
2599
2600         if(ed==NULL)
2601                 return OPERATOR_CANCELLED;
2602
2603         rect.xmin /=  (float)(ABS(v2d->tot.xmax - v2d->tot.xmin));
2604         rect.ymin /=  (float)(ABS(v2d->tot.ymax - v2d->tot.ymin));
2605
2606         rect.xmax /=  (float)(ABS(v2d->tot.xmax - v2d->tot.xmin));
2607         rect.ymax /=  (float)(ABS(v2d->tot.ymax - v2d->tot.ymin));
2608
2609         rect.xmin+=0.5f;
2610         rect.xmax+=0.5f;
2611         rect.ymin+=0.5f;
2612         rect.ymax+=0.5f;
2613
2614         CLAMP(rect.xmin, 0.0f, 1.0f);
2615         CLAMP(rect.ymin, 0.0f, 1.0f);
2616         CLAMP(rect.xmax, 0.0f, 1.0f);
2617         CLAMP(rect.ymax, 0.0f, 1.0f);
2618
2619         scene->ed->over_border= rect;
2620
2621         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
2622
2623         return OPERATOR_FINISHED;
2624 }
2625
2626 /* ****** Border Select ****** */
2627 void SEQUENCER_OT_view_ghost_border(wmOperatorType *ot)
2628 {
2629         /* identifiers */
2630         ot->name= "Border Offset View";
2631         ot->idname= "SEQUENCER_OT_view_ghost_border";
2632         ot->description="Enable border select mode";
2633
2634         /* api callbacks */
2635         ot->invoke= WM_border_select_invoke;
2636         ot->exec= view_ghost_border_exec;
2637         ot->modal= WM_border_select_modal;
2638         ot->poll= sequencer_view_poll;
2639         ot->cancel= WM_border_select_cancel;
2640
2641         /* flags */
2642         ot->flag= 0;
2643
2644         /* rna */
2645         WM_operator_properties_gesture_border(ot, FALSE);
2646 }
2647
2648
2649 /* change ops */
2650
2651 static EnumPropertyItem prop_change_effect_input_types[] = {
2652         {0, "A_B", 0, "A -> B", ""},
2653         {1, "B_C", 0, "B -> C", ""},
2654         {2, "A_C", 0, "A -> C", ""},
2655         {0, NULL, 0, NULL, NULL}
2656 };
2657
2658 static int sequencer_change_effect_input_exec(bContext *C, wmOperator *op)
2659 {
2660         Scene *scene= CTX_data_scene(C);
2661         Editing *ed= seq_give_editing(scene, FALSE);
2662         Sequence *seq= seq_active_get(scene);
2663
2664         Sequence **seq_1, **seq_2;
2665
2666         switch(RNA_enum_get(op->ptr, "swap")) {
2667                 case 0:
2668                         seq_1= &seq->seq1;
2669                         seq_2= &seq->seq2;
2670                         break;
2671                 case 1:
2672                         seq_1= &seq->seq2;
2673                         seq_2= &seq->seq3;
2674                         break;
2675                 default: /* 2 */
2676                         seq_1= &seq->seq1;
2677                         seq_2= &seq->seq3;
2678                         break;
2679         }
2680
2681         if(*seq_1 == NULL || *seq_2 == NULL) {
2682                 BKE_report(op->reports, RPT_ERROR, "One of the effect inputs is unset, can't swap");
2683                 return OPERATOR_CANCELLED;
2684         }
2685         else {
2686                 SWAP(Sequence *, *seq_1, *seq_2);
2687         }
2688
2689         update_changed_seq_and_deps(scene, seq, 0, 1);
2690
2691         /* important else we dont get the imbuf cache flushed */
2692         free_imbuf_seq(scene, &ed->seqbase, FALSE, FALSE);
2693
2694         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
2695
2696         return OPERATOR_FINISHED;
2697 }
2698
2699 void SEQUENCER_OT_change_effect_input(struct wmOperatorType *ot)
2700 {
2701         /* identifiers */
2702         ot->name= "Change Effect Input";
2703         ot->idname= "SEQUENCER_OT_change_effect_input";
2704         ot->description="";
2705
2706         /* api callbacks */
2707         ot->exec= sequencer_change_effect_input_exec;
2708         ot->poll= sequencer_effect_poll;
2709
2710         /* flags */
2711         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2712
2713         ot->prop= RNA_def_enum(ot->srna, "swap", prop_change_effect_input_types, 0, "Swap", "The effect inputs to swap");
2714 }
2715
2716 static int sequencer_change_effect_type_exec(bContext *C, wmOperator *op)
2717 {
2718         Scene *scene= CTX_data_scene(C);
2719         Editing *ed= seq_give_editing(scene, FALSE);
2720         Sequence *seq= seq_active_get(scene);
2721         const int new_type= RNA_enum_get(op->ptr, "type");
2722
2723         /* free previous effect and init new effect */
2724         struct SeqEffectHandle sh;
2725
2726         if ((seq->type & SEQ_EFFECT) == 0) {
2727                 return OPERATOR_CANCELLED;
2728         }
2729
2730         /* can someone explain the logic behind only allowing to increse this,
2731          * copied from 2.4x - campbell */
2732         if (get_sequence_effect_num_inputs(seq->type) <
2733                 get_sequence_effect_num_inputs(new_type)
2734         ) {
2735                 BKE_report(op->reports, RPT_ERROR, "New effect needs more input strips");
2736                 return OPERATOR_CANCELLED;
2737         }
2738         else {
2739                 sh = get_sequence_effect(seq);
2740                 sh.free(seq);
2741
2742                 seq->type= new_type;
2743
2744                 sh = get_sequence_effect(seq);
2745                 sh.init(seq);
2746         }
2747
2748         /* update */
2749         update_changed_seq_and_deps(scene, seq, 0, 1);
2750
2751         /* important else we dont get the imbuf cache flushed */
2752         free_imbuf_seq(scene, &ed->seqbase, FALSE, FALSE);
2753
2754         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
2755
2756         return OPERATOR_FINISHED;
2757 }
2758
2759 void SEQUENCER_OT_change_effect_type(struct wmOperatorType *ot)
2760 {
2761         /* identifiers */
2762         ot->name= "Change Effect Type";
2763         ot->idname= "SEQUENCER_OT_change_effect_type";
2764         ot->description="";
2765
2766         /* api callbacks */
2767         ot->exec= sequencer_change_effect_type_exec;
2768         ot->poll= sequencer_effect_poll;
2769
2770         /* flags */
2771         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2772
2773         ot->prop= RNA_def_enum(ot->srna, "type", sequencer_prop_effect_types, SEQ_CROSS, "Type", "Sequencer effect type");
2774 }
2775
2776 static int sequencer_change_path_exec(bContext *C, wmOperator *op)
2777 {
2778         Scene *scene= CTX_data_scene(C);
2779         Editing *ed= seq_give_editing(scene, FALSE);
2780         Sequence *seq= seq_active_get(scene);
2781
2782         if(seq->type == SEQ_IMAGE) {
2783                 char directory[FILE_MAX];
2784                 const int len= RNA_property_collection_length(op->ptr, RNA_struct_find_property(op->ptr, "files"));
2785                 StripElem *se;
2786
2787                 if(len==0)
2788                         return OPERATOR_CANCELLED;
2789
2790                 RNA_string_get(op->ptr, "directory", directory);
2791                 BLI_strncpy(seq->strip->dir, directory, sizeof(seq->strip->dir));
2792
2793                 if(seq->strip->stripdata) {
2794                         MEM_freeN(seq->strip->stripdata);
2795                 }
2796                 seq->strip->stripdata= se= MEM_callocN(len*sizeof(StripElem), "stripelem");
2797
2798                 RNA_BEGIN(op->ptr, itemptr, "files") {
2799                         char *filename= RNA_string_get_alloc(&itemptr, "name", NULL, 0);
2800                         BLI_strncpy(se->name, filename, sizeof(se->name));
2801                         MEM_freeN(filename);
2802                         se++;
2803                 }
2804                 RNA_END;
2805
2806                 /* correct start/end frames so we dont move
2807                  * important not to set seq->len= len; allow the function to handle it */
2808                 reload_sequence_new_file(scene, seq, TRUE);
2809
2810                 calc_sequence(scene, seq);
2811
2812                 /* important else we dont get the imbuf cache flushed */
2813                 free_imbuf_seq(scene, &ed->seqbase, FALSE, FALSE);
2814         }
2815         else {
2816                 /* lame, set rna filepath */
2817                 PointerRNA seq_ptr;
2818                 char filepath[FILE_MAX];
2819
2820                 RNA_pointer_create(&scene->id, &RNA_Sequence, seq, &seq_ptr);
2821
2822                 RNA_string_get(op->ptr, "filepath", filepath);
2823                 RNA_string_set(&seq_ptr, "filepath", filepath);
2824         }
2825
2826         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
2827
2828         return OPERATOR_FINISHED;
2829 }
2830
2831 static int sequencer_change_path_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
2832 {
2833         Scene *scene= CTX_data_scene(C);
2834         Sequence *seq= seq_active_get(scene);
2835
2836         RNA_string_set(op->ptr, "directory", seq->strip->dir);
2837
2838         /* set default display depending on seq type */
2839         if(seq->type == SEQ_IMAGE) {
2840                 RNA_boolean_set(op->ptr, "filter_movie", 0);
2841         }
2842         else {
2843                 RNA_boolean_set(op->ptr, "filter_image", 0);
2844         }
2845
2846         WM_event_add_fileselect(C, op);
2847
2848         return OPERATOR_RUNNING_MODAL;
2849 }
2850
2851 void SEQUENCER_OT_change_path(struct wmOperatorType *ot)
2852 {
2853         /* identifiers */
2854         ot->name= "Change Data/Files";
2855         ot->idname= "SEQUENCER_OT_change_path";
2856         ot->description="";
2857
2858         /* api callbacks */
2859         ot->exec= sequencer_change_path_exec;
2860         ot->invoke= sequencer_change_path_invoke;
2861         ot->poll= sequencer_strip_has_path_poll;
2862
2863         /* flags */
2864         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2865
2866         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_DIRECTORY|WM_FILESEL_RELPATH|WM_FILESEL_FILEPATH|WM_FILESEL_FILES);
2867 }