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