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