Code Cleanup: style and redundant casts
[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 bool 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         bool 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
1605                 SEQP_BEGIN (ed, seq)
1606                 {
1607                         if (seq->seq1 || seq->seq2 || seq->seq3) {
1608                                 BKE_sequence_calc(scene, seq);
1609                         }
1610                 }
1611                 SEQ_END;
1612
1613                 /* as last: */
1614                 BKE_sequencer_sort(scene);
1615         }
1616
1617         if (changed) {
1618                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1619                 return OPERATOR_FINISHED;
1620         }
1621         else {
1622                 return OPERATOR_CANCELLED;
1623         }
1624 }
1625
1626
1627 static int sequencer_cut_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1628 {
1629         Scene *scene = CTX_data_scene(C);
1630         View2D *v2d = UI_view2d_fromcontext(C);
1631
1632         int cut_side = SEQ_SIDE_BOTH;
1633         int cut_frame = CFRA;
1634
1635         if (ED_operator_sequencer_active(C) && v2d)
1636                 cut_side = mouse_frame_side(v2d, event->mval[0], cut_frame);
1637         
1638         RNA_int_set(op->ptr, "frame", cut_frame);
1639         RNA_enum_set(op->ptr, "side", cut_side);
1640         /*RNA_enum_set(op->ptr, "type", cut_hard); */ /*This type is set from the key shortcut */
1641
1642         return sequencer_cut_exec(C, op);
1643 }
1644
1645
1646 void SEQUENCER_OT_cut(struct wmOperatorType *ot)
1647 {
1648         /* identifiers */
1649         ot->name = "Cut Strips";
1650         ot->idname = "SEQUENCER_OT_cut";
1651         ot->description = "Cut the selected strips";
1652         
1653         /* api callbacks */
1654         ot->invoke = sequencer_cut_invoke;
1655         ot->exec = sequencer_cut_exec;
1656         ot->poll = sequencer_edit_poll;
1657         
1658         /* flags */
1659         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1660         
1661         RNA_def_int(ot->srna, "frame", 0, INT_MIN, INT_MAX, "Frame", "Frame where selected strips will be cut", INT_MIN, INT_MAX);
1662         RNA_def_enum(ot->srna, "type", prop_cut_types, SEQ_CUT_SOFT, "Type", "The type of cut operation to perform on strips");
1663         RNA_def_enum(ot->srna, "side", prop_side_types, SEQ_SIDE_BOTH, "Side", "The side that remains selected after cutting");
1664 }
1665
1666 /* duplicate operator */
1667 static int apply_unique_name_cb(Sequence *seq, void *arg_pt)
1668 {
1669         Scene *scene = (Scene *)arg_pt;
1670         char name[sizeof(seq->name) - 2];
1671
1672         BLI_strncpy_utf8(name, seq->name + 2, sizeof(name));
1673         BKE_sequence_base_unique_name_recursive(&scene->ed->seqbase, seq);
1674         BKE_sequencer_dupe_animdata(scene, name, seq->name + 2);
1675         return 1;
1676
1677 }
1678
1679 static int sequencer_add_duplicate_exec(bContext *C, wmOperator *UNUSED(op))
1680 {
1681         Scene *scene = CTX_data_scene(C);
1682         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1683
1684         ListBase nseqbase = {NULL, NULL};
1685
1686         if (ed == NULL)
1687                 return OPERATOR_CANCELLED;
1688
1689         BKE_sequence_base_dupli_recursive(scene, NULL, &nseqbase, ed->seqbasep, SEQ_DUPE_CONTEXT);
1690
1691         if (nseqbase.first) {
1692                 Sequence *seq = nseqbase.first;
1693                 /* rely on the nseqbase list being added at the end */
1694                 BLI_movelisttolist(ed->seqbasep, &nseqbase);
1695
1696                 for (; seq; seq = seq->next)
1697                         BKE_sequencer_recursive_apply(seq, apply_unique_name_cb, scene);
1698
1699                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1700                 return OPERATOR_FINISHED;
1701         }
1702
1703         return OPERATOR_CANCELLED;
1704 }
1705
1706 void SEQUENCER_OT_duplicate(wmOperatorType *ot)
1707 {
1708         /* identifiers */
1709         ot->name = "Duplicate Strips";
1710         ot->idname = "SEQUENCER_OT_duplicate";
1711         ot->description = "Duplicate the selected strips";
1712         
1713         /* api callbacks */
1714         ot->exec = sequencer_add_duplicate_exec;
1715         ot->poll = ED_operator_sequencer_active;
1716         
1717         /* flags */
1718         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1719         
1720         /* to give to transform */
1721         RNA_def_enum(ot->srna, "mode", transform_mode_types, TFM_TRANSLATION, "Mode", "");
1722 }
1723
1724 /* delete operator */
1725 static int sequencer_delete_exec(bContext *C, wmOperator *UNUSED(op))
1726 {
1727         Scene *scene = CTX_data_scene(C);
1728         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1729         Sequence *seq;
1730         MetaStack *ms;
1731         int nothingSelected = TRUE;
1732
1733         seq = BKE_sequencer_active_get(scene);
1734         if (seq && seq->flag & SELECT) { /* avoid a loop since this is likely to be selected */
1735                 nothingSelected = FALSE;
1736         }
1737         else {
1738                 for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1739                         if (seq->flag & SELECT) {
1740                                 nothingSelected = FALSE;
1741                                 break;
1742                         }
1743                 }
1744         }
1745
1746         if (nothingSelected)
1747                 return OPERATOR_FINISHED;
1748
1749         /* for effects, try to find a replacement input */
1750         for (seq = ed->seqbasep->first; seq; seq = seq->next)
1751                 if ((seq->type & SEQ_TYPE_EFFECT) && !(seq->flag & SELECT))
1752                         del_seq_find_replace_recurs(scene, seq);
1753
1754         /* delete all selected strips */
1755         recurs_del_seq_flag(scene, ed->seqbasep, SELECT, 0);
1756
1757         /* updates lengths etc */
1758         seq = ed->seqbasep->first;
1759         while (seq) {
1760                 BKE_sequence_calc(scene, seq);
1761                 seq = seq->next;
1762         }
1763
1764         /* free parent metas */
1765         ms = ed->metastack.last;
1766         while (ms) {
1767                 BKE_sequence_calc(scene, ms->parseq);
1768                 ms = ms->prev;
1769         }
1770
1771         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1772         
1773         return OPERATOR_FINISHED;
1774 }
1775
1776 static int sequencer_delete_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1777 {
1778         ARegion *ar = CTX_wm_region(C);
1779
1780         if (ar->regiontype == RGN_TYPE_WINDOW) {
1781                 /* bounding box of 30 pixels is used for markers shortcuts,
1782                  * prevent conflict with markers shortcuts here
1783                  */
1784                 if (event->mval[1] <= 30)
1785                         return OPERATOR_PASS_THROUGH;
1786         }
1787
1788         return WM_operator_confirm(C, op, event);
1789 }
1790
1791 void SEQUENCER_OT_delete(wmOperatorType *ot)
1792 {
1793
1794         /* identifiers */
1795         ot->name = "Erase Strips";
1796         ot->idname = "SEQUENCER_OT_delete";
1797         ot->description = "Erase selected strips from the sequencer";
1798         
1799         /* api callbacks */
1800         ot->invoke = sequencer_delete_invoke;
1801         ot->exec = sequencer_delete_exec;
1802         ot->poll = sequencer_edit_poll;
1803         
1804         /* flags */
1805         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1806 }
1807
1808
1809 /* offset clear operator */
1810 static int sequencer_offset_clear_exec(bContext *C, wmOperator *UNUSED(op))
1811 {
1812         Scene *scene = CTX_data_scene(C);
1813         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1814         Sequence *seq;
1815
1816         /* for effects, try to find a replacement input */
1817         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1818                 if ((seq->type & SEQ_TYPE_EFFECT) == 0 && (seq->flag & SELECT)) {
1819                         seq->startofs = seq->endofs = seq->startstill = seq->endstill = 0;
1820                 }
1821         }
1822
1823         /* updates lengths etc */
1824         seq = ed->seqbasep->first;
1825         while (seq) {
1826                 BKE_sequence_calc(scene, seq);
1827                 seq = seq->next;
1828         }
1829
1830         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1831                 if ((seq->type & SEQ_TYPE_EFFECT) == 0 && (seq->flag & SELECT)) {
1832                         if (BKE_sequence_test_overlap(ed->seqbasep, seq)) {
1833                                 BKE_sequence_base_shuffle(ed->seqbasep, seq, scene);
1834                         }
1835                 }
1836         }
1837
1838         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1839
1840         return OPERATOR_FINISHED;
1841 }
1842
1843
1844 void SEQUENCER_OT_offset_clear(wmOperatorType *ot)
1845 {
1846
1847         /* identifiers */
1848         ot->name = "Clear Strip Offset";
1849         ot->idname = "SEQUENCER_OT_offset_clear";
1850         ot->description = "Clear strip offsets from the start and end frames";
1851
1852         /* api callbacks */
1853         ot->exec = sequencer_offset_clear_exec;
1854         ot->poll = sequencer_edit_poll;
1855
1856         /* flags */
1857         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1858 }
1859
1860
1861 /* separate_images operator */
1862 static int sequencer_separate_images_exec(bContext *C, wmOperator *op)
1863 {
1864         Scene *scene = CTX_data_scene(C);
1865         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1866         
1867         Sequence *seq, *seq_new;
1868         Strip *strip_new;
1869         StripElem *se, *se_new;
1870         int start_ofs, cfra, frame_end;
1871         int step = RNA_int_get(op->ptr, "length");
1872
1873         seq = ed->seqbasep->first; /* poll checks this is valid */
1874
1875         while (seq) {
1876                 if ((seq->flag & SELECT) && (seq->type == SEQ_TYPE_IMAGE) && (seq->len > 1)) {
1877                         /* remove seq so overlap tests don't conflict,
1878                          * see seq_free_sequence below for the real free'ing */
1879                         BLI_remlink(ed->seqbasep, seq);
1880                         /* if (seq->ipo) seq->ipo->id.us--; */
1881                         /* XXX, remove fcurve and assign to split image strips */
1882
1883                         start_ofs = cfra = BKE_sequence_tx_get_final_left(seq, 0);
1884                         frame_end = BKE_sequence_tx_get_final_right(seq, 0);
1885
1886                         while (cfra < frame_end) {
1887                                 /* new seq */
1888                                 se = BKE_sequencer_give_stripelem(seq, cfra);
1889
1890                                 seq_new = BKE_sequence_dupli_recursive(scene, scene, seq, SEQ_DUPE_UNIQUE_NAME);
1891                                 BLI_addtail(ed->seqbasep, seq_new);
1892
1893                                 seq_new->start = start_ofs;
1894                                 seq_new->type = SEQ_TYPE_IMAGE;
1895                                 seq_new->len = 1;
1896                                 seq_new->endstill = step - 1;
1897
1898                                 /* new strip */
1899                                 strip_new = seq_new->strip;
1900                                 strip_new->us = 1;
1901
1902                                 /* new stripdata */
1903                                 se_new = strip_new->stripdata;
1904                                 BLI_strncpy(se_new->name, se->name, sizeof(se_new->name));
1905                                 BKE_sequence_calc(scene, seq_new);
1906
1907                                 if (step > 1) {
1908                                         seq_new->flag &= ~SEQ_OVERLAP;
1909                                         if (BKE_sequence_test_overlap(ed->seqbasep, seq_new)) {
1910                                                 BKE_sequence_base_shuffle(ed->seqbasep, seq_new, scene);
1911                                         }
1912                                 }
1913
1914                                 /* XXX, COPY FCURVES */
1915
1916                                 cfra++;
1917                                 start_ofs += step;
1918                         }
1919
1920                         BKE_sequence_free(scene, seq);
1921                         seq = seq->next;
1922                 }
1923                 else {
1924                         seq = seq->next;
1925                 }
1926         }
1927
1928         /* as last: */
1929         BKE_sequencer_sort(scene);
1930         
1931         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1932
1933         return OPERATOR_FINISHED;
1934 }
1935
1936
1937 void SEQUENCER_OT_images_separate(wmOperatorType *ot)
1938 {
1939         /* identifiers */
1940         ot->name = "Separate Images";
1941         ot->idname = "SEQUENCER_OT_images_separate";
1942         ot->description = "On image sequence strips, it returns a strip for each image";
1943         
1944         /* api callbacks */
1945         ot->exec = sequencer_separate_images_exec;
1946         ot->invoke = WM_operator_props_popup;
1947         ot->poll = sequencer_edit_poll;
1948         
1949         /* flags */
1950         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1951
1952         RNA_def_int(ot->srna, "length", 1, 1, INT_MAX, "Length", "Length of each frame", 1, 1000);
1953 }
1954
1955
1956 /* META Operators */
1957
1958 /* separate_meta_toggle operator */
1959 static int sequencer_meta_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1960 {
1961         Scene *scene = CTX_data_scene(C);
1962         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1963         Sequence *last_seq = BKE_sequencer_active_get(scene);
1964         MetaStack *ms;
1965
1966         if (last_seq && last_seq->type == SEQ_TYPE_META && last_seq->flag & SELECT) {
1967                 /* Enter Metastrip */
1968                 ms = MEM_mallocN(sizeof(MetaStack), "metastack");
1969                 BLI_addtail(&ed->metastack, ms);
1970                 ms->parseq = last_seq;
1971                 ms->oldbasep = ed->seqbasep;
1972
1973                 ed->seqbasep = &last_seq->seqbase;
1974
1975                 BKE_sequencer_active_set(scene, NULL);
1976
1977         }
1978         else {
1979                 /* Exit Metastrip (if possible) */
1980
1981                 Sequence *seq;
1982
1983                 if (ed->metastack.first == NULL)
1984                         return OPERATOR_CANCELLED;
1985
1986                 ms = ed->metastack.last;
1987                 BLI_remlink(&ed->metastack, ms);
1988
1989                 ed->seqbasep = ms->oldbasep;
1990
1991                 /* recalc all: the meta can have effects connected to it */
1992                 for (seq = ed->seqbasep->first; seq; seq = seq->next)
1993                         BKE_sequence_calc(scene, seq);
1994
1995                 if (BKE_sequence_test_overlap(ed->seqbasep, ms->parseq))
1996                         BKE_sequence_base_shuffle(ed->seqbasep, ms->parseq, scene);
1997
1998                 BKE_sequencer_active_set(scene, ms->parseq);
1999
2000                 ms->parseq->flag |= SELECT;
2001                 recurs_sel_seq(ms->parseq);
2002
2003                 MEM_freeN(ms);
2004
2005         }
2006
2007         BKE_sequencer_update_muting(ed);
2008         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2009
2010         return OPERATOR_FINISHED;
2011 }
2012
2013 void SEQUENCER_OT_meta_toggle(wmOperatorType *ot)
2014 {
2015         /* identifiers */
2016         ot->name = "Toggle Meta Strip";
2017         ot->idname = "SEQUENCER_OT_meta_toggle";
2018         ot->description = "Toggle a metastrip (to edit enclosed strips)";
2019         
2020         /* api callbacks */
2021         ot->exec = sequencer_meta_toggle_exec;
2022         ot->poll = sequencer_edit_poll;
2023         
2024         /* flags */
2025         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2026 }
2027
2028
2029 /* separate_meta_make operator */
2030 static int sequencer_meta_make_exec(bContext *C, wmOperator *op)
2031 {
2032         Scene *scene = CTX_data_scene(C);
2033         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2034         
2035         Sequence *seq, *seqm, *next, *last_seq = BKE_sequencer_active_get(scene);
2036         int channel_max = 1;
2037
2038         if (BKE_sequence_base_isolated_sel_check(ed->seqbasep) == FALSE) {
2039                 BKE_report(op->reports, RPT_ERROR, "Please select all related strips");
2040                 return OPERATOR_CANCELLED;
2041         }
2042
2043         /* remove all selected from main list, and put in meta */
2044
2045         seqm = BKE_sequence_alloc(ed->seqbasep, 1, 1); /* channel number set later */
2046         strcpy(seqm->name + 2, "MetaStrip");
2047         seqm->type = SEQ_TYPE_META;
2048         seqm->flag = SELECT;
2049
2050         seq = ed->seqbasep->first;
2051         while (seq) {
2052                 next = seq->next;
2053                 if (seq != seqm && (seq->flag & SELECT)) {
2054                         channel_max = max_ii(seq->machine, channel_max);
2055                         BLI_remlink(ed->seqbasep, seq);
2056                         BLI_addtail(&seqm->seqbase, seq);
2057                 }
2058                 seq = next;
2059         }
2060         seqm->machine = last_seq ? last_seq->machine : channel_max;
2061         BKE_sequence_calc(scene, seqm);
2062
2063         seqm->strip = MEM_callocN(sizeof(Strip), "metastrip");
2064         seqm->strip->us = 1;
2065         
2066         BKE_sequencer_active_set(scene, seqm);
2067
2068         if (BKE_sequence_test_overlap(ed->seqbasep, seqm) ) BKE_sequence_base_shuffle(ed->seqbasep, seqm, scene);
2069
2070         BKE_sequencer_update_muting(ed);
2071
2072         BKE_sequence_base_unique_name_recursive(&scene->ed->seqbase, seqm);
2073
2074         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2075
2076         return OPERATOR_FINISHED;
2077 }
2078
2079 void SEQUENCER_OT_meta_make(wmOperatorType *ot)
2080 {
2081         /* identifiers */
2082         ot->name = "Make Meta Strip";
2083         ot->idname = "SEQUENCER_OT_meta_make";
2084         ot->description = "Group selected strips into a metastrip";
2085         
2086         /* api callbacks */
2087         ot->exec = sequencer_meta_make_exec;
2088         ot->poll = sequencer_edit_poll;
2089         
2090         /* flags */
2091         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2092 }
2093
2094
2095 static int seq_depends_on_meta(Sequence *seq, Sequence *seqm)
2096 {
2097         if (seq == seqm) return 1;
2098         else if (seq->seq1 && seq_depends_on_meta(seq->seq1, seqm)) return 1;
2099         else if (seq->seq2 && seq_depends_on_meta(seq->seq2, seqm)) return 1;
2100         else if (seq->seq3 && seq_depends_on_meta(seq->seq3, seqm)) return 1;
2101         else return 0;
2102 }
2103
2104 /* separate_meta_make operator */
2105 static int sequencer_meta_separate_exec(bContext *C, wmOperator *UNUSED(op))
2106 {
2107         Scene *scene = CTX_data_scene(C);
2108         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2109
2110         Sequence *seq, *last_seq = BKE_sequencer_active_get(scene); /* last_seq checks (ed == NULL) */
2111
2112         if (last_seq == NULL || last_seq->type != SEQ_TYPE_META)
2113                 return OPERATOR_CANCELLED;
2114
2115         BLI_movelisttolist(ed->seqbasep, &last_seq->seqbase);
2116
2117         last_seq->seqbase.first = NULL;
2118         last_seq->seqbase.last = NULL;
2119
2120         BLI_remlink(ed->seqbasep, last_seq);
2121         BKE_sequence_free(scene, last_seq);
2122
2123         /* emtpy meta strip, delete all effects depending on it */
2124         for (seq = ed->seqbasep->first; seq; seq = seq->next)
2125                 if ((seq->type & SEQ_TYPE_EFFECT) && seq_depends_on_meta(seq, last_seq))
2126                         seq->flag |= SEQ_FLAG_DELETE;
2127
2128         recurs_del_seq_flag(scene, ed->seqbasep, SEQ_FLAG_DELETE, 0);
2129
2130         /* test for effects and overlap
2131          * don't use SEQP_BEGIN since that would be recursive */
2132         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2133                 if (seq->flag & SELECT) {
2134                         seq->flag &= ~SEQ_OVERLAP;
2135                         if (BKE_sequence_test_overlap(ed->seqbasep, seq)) {
2136                                 BKE_sequence_base_shuffle(ed->seqbasep, seq, scene);
2137                         }
2138                 }
2139         }
2140
2141         BKE_sequencer_sort(scene);
2142         BKE_sequencer_update_muting(ed);
2143
2144         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2145
2146         return OPERATOR_FINISHED;
2147 }
2148
2149 void SEQUENCER_OT_meta_separate(wmOperatorType *ot)
2150 {
2151         /* identifiers */
2152         ot->name = "UnMeta Strip";
2153         ot->idname = "SEQUENCER_OT_meta_separate";
2154         ot->description = "Put the contents of a metastrip back in the sequencer";
2155         
2156         /* api callbacks */
2157         ot->exec = sequencer_meta_separate_exec;
2158         ot->poll = sequencer_edit_poll;
2159         
2160         /* flags */
2161         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2162 }
2163
2164 /* view_all operator */
2165 static int sequencer_view_all_exec(bContext *C, wmOperator *op)
2166 {
2167         ARegion *ar = CTX_wm_region(C);
2168         View2D *v2d = UI_view2d_fromcontext(C);
2169         const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
2170
2171         UI_view2d_smooth_view(C, ar, &v2d->tot, smooth_viewtx);
2172         return OPERATOR_FINISHED;
2173 }
2174
2175 void SEQUENCER_OT_view_all(wmOperatorType *ot)
2176 {
2177         /* identifiers */
2178         ot->name = "View All";
2179         ot->idname = "SEQUENCER_OT_view_all";
2180         ot->description = "View all the strips in the sequencer";
2181         
2182         /* api callbacks */
2183         ot->exec = sequencer_view_all_exec;
2184         ot->poll = ED_operator_sequencer_active;
2185         
2186         /* flags */
2187         ot->flag = OPTYPE_REGISTER;
2188 }
2189
2190 /* view_all operator */
2191 static int sequencer_view_all_preview_exec(bContext *C, wmOperator *UNUSED(op))
2192 {
2193         bScreen *sc = CTX_wm_screen(C);
2194         ScrArea *area = CTX_wm_area(C);
2195 #if 0
2196         ARegion *ar = CTX_wm_region(C);
2197         SpaceSeq *sseq = area->spacedata.first;
2198         Scene *scene = CTX_data_scene(C);
2199 #endif
2200         View2D *v2d = UI_view2d_fromcontext(C);
2201
2202         v2d->cur = v2d->tot;
2203         UI_view2d_curRect_validate(v2d);
2204         UI_view2d_sync(sc, area, v2d, V2D_LOCK_COPY);
2205         
2206 #if 0
2207         /* Like zooming on an image view */
2208         float zoomX, zoomY;
2209         int width, height, imgwidth, imgheight;
2210
2211         width = ar->winx;
2212         height = ar->winy;
2213
2214         seq_reset_imageofs(sseq);
2215
2216         imgwidth = (scene->r.size * scene->r.xsch) / 100;
2217         imgheight = (scene->r.size * scene->r.ysch) / 100;
2218
2219         /* Apply aspect, dosnt need to be that accurate */
2220         imgwidth = (int)(imgwidth * (scene->r.xasp / scene->r.yasp));
2221
2222         if (((imgwidth >= width) || (imgheight >= height)) &&
2223             ((width > 0) && (height > 0)))
2224         {
2225                 /* Find the zoom value that will fit the image in the image space */
2226                 zoomX = ((float)width) / ((float)imgwidth);
2227                 zoomY = ((float)height) / ((float)imgheight);
2228                 sseq->zoom = (zoomX < zoomY) ? zoomX : zoomY;
2229
2230                 sseq->zoom = 1.0f / power_of_2(1 / min_ff(zoomX, zoomY));
2231         }
2232         else {
2233                 sseq->zoom = 1.0f;
2234         }
2235 #endif
2236
2237         ED_area_tag_redraw(CTX_wm_area(C));
2238         return OPERATOR_FINISHED;
2239 }
2240
2241 void SEQUENCER_OT_view_all_preview(wmOperatorType *ot)
2242 {
2243         /* identifiers */
2244         ot->name = "View All";
2245         ot->idname = "SEQUENCER_OT_view_all_preview";
2246         ot->description = "Zoom preview to fit in the area";
2247         
2248         /* api callbacks */
2249         ot->exec = sequencer_view_all_preview_exec;
2250         ot->poll = ED_operator_sequencer_active;
2251         
2252         /* flags */
2253         ot->flag = OPTYPE_REGISTER;
2254 }
2255
2256
2257 static int sequencer_view_zoom_ratio_exec(bContext *C, wmOperator *op)
2258 {
2259         RenderData *rd = &CTX_data_scene(C)->r;
2260         View2D *v2d = UI_view2d_fromcontext(C);
2261
2262         float ratio = RNA_float_get(op->ptr, "ratio");
2263
2264         float winx = (int)(rd->size * rd->xsch) / 100;
2265         float winy = (int)(rd->size * rd->ysch) / 100;
2266
2267         float facx = BLI_rcti_size_x(&v2d->mask) / winx;
2268         float facy = BLI_rcti_size_y(&v2d->mask) / winy;
2269
2270         BLI_rctf_resize(&v2d->cur, (int)(winx * facx * ratio) + 1, (int)(winy * facy * ratio) + 1);
2271
2272         ED_region_tag_redraw(CTX_wm_region(C));
2273
2274         return OPERATOR_FINISHED;
2275 }
2276
2277 void SEQUENCER_OT_view_zoom_ratio(wmOperatorType *ot)
2278 {
2279         /* identifiers */
2280         ot->name = "Sequencer View Zoom Ratio";
2281         ot->idname = "SEQUENCER_OT_view_zoom_ratio";
2282         ot->description = "Change zoom ratio of sequencer preview";
2283
2284         /* api callbacks */
2285         ot->exec = sequencer_view_zoom_ratio_exec;
2286         ot->poll = ED_operator_sequencer_active;
2287
2288         /* properties */
2289         RNA_def_float(ot->srna, "ratio", 1.0f, -FLT_MAX, FLT_MAX,
2290                       "Ratio", "Zoom ratio, 1.0 is 1:1, higher is zoomed in, lower is zoomed out", -FLT_MAX, FLT_MAX);
2291 }
2292
2293
2294 #if 0
2295 static EnumPropertyItem view_type_items[] = {
2296         {SEQ_VIEW_SEQUENCE, "SEQUENCER", ICON_SEQ_SEQUENCER, "Sequencer", ""},
2297         {SEQ_VIEW_PREVIEW,  "PREVIEW", ICON_SEQ_PREVIEW, "Image Preview", ""},
2298         {SEQ_VIEW_SEQUENCE_PREVIEW,  "SEQUENCER_PREVIEW", ICON_SEQ_SEQUENCER, "Sequencer and Image Preview", ""},
2299         {0, NULL, 0, NULL, NULL}
2300 };
2301 #endif
2302
2303 /* view_all operator */
2304 static int sequencer_view_toggle_exec(bContext *C, wmOperator *UNUSED(op))
2305 {
2306         SpaceSeq *sseq = (SpaceSeq *)CTX_wm_space_data(C);
2307
2308         sseq->view++;
2309         if (sseq->view > SEQ_VIEW_SEQUENCE_PREVIEW) sseq->view = SEQ_VIEW_SEQUENCE;
2310
2311         ED_area_tag_refresh(CTX_wm_area(C));
2312
2313         return OPERATOR_FINISHED;
2314 }
2315
2316 void SEQUENCER_OT_view_toggle(wmOperatorType *ot)
2317 {
2318         /* identifiers */
2319         ot->name = "View Toggle";
2320         ot->idname = "SEQUENCER_OT_view_toggle";
2321         ot->description = "Toggle between sequencer views (sequence, preview, both)";
2322         
2323         /* api callbacks */
2324         ot->exec = sequencer_view_toggle_exec;
2325         ot->poll = ED_operator_sequencer_active;
2326         
2327         /* flags */
2328         ot->flag = OPTYPE_REGISTER;
2329 }
2330
2331
2332 /* view_selected operator */
2333 static int sequencer_view_selected_exec(bContext *C, wmOperator *op)
2334 {
2335         Scene *scene = CTX_data_scene(C);
2336         View2D *v2d = UI_view2d_fromcontext(C);
2337         ARegion *ar = CTX_wm_region(C);
2338         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2339         Sequence *last_seq = BKE_sequencer_active_get(scene);
2340         Sequence *seq;
2341         rctf cur_new = v2d->cur;
2342
2343         int xmin =  MAXFRAME * 2;
2344         int xmax = -MAXFRAME * 2;
2345         int ymin =  MAXSEQ + 1;
2346         int ymax = 0;
2347         int orig_height;
2348         int ymid;
2349         int ymargin = 1;
2350         int xmargin = FPS;
2351
2352         if (ed == NULL)
2353                 return OPERATOR_CANCELLED;
2354
2355         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2356                 if ((seq->flag & SELECT) || (seq == last_seq)) {
2357                         xmin = min_ii(xmin, seq->startdisp);
2358                         xmax = max_ii(xmax, seq->enddisp);
2359
2360                         ymin = min_ii(ymin, seq->machine);
2361                         ymax = max_ii(ymax, seq->machine);
2362                 }
2363         }
2364
2365         if (ymax != 0) {
2366                 const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
2367                 
2368                 xmax += xmargin;
2369                 xmin -= xmargin;
2370                 ymax += ymargin;
2371                 ymin -= ymargin;
2372
2373                 orig_height = BLI_rctf_size_y(&cur_new);
2374
2375                 cur_new.xmin = xmin;
2376                 cur_new.xmax = xmax;
2377
2378                 cur_new.ymin = ymin;
2379                 cur_new.ymax = ymax;
2380
2381                 /* only zoom out vertically */
2382                 if (orig_height > BLI_rctf_size_y(&cur_new)) {
2383                         ymid = BLI_rctf_cent_y(&cur_new);
2384
2385                         cur_new.ymin = ymid - (orig_height / 2);
2386                         cur_new.ymax = ymid + (orig_height / 2);
2387                 }
2388
2389                 UI_view2d_smooth_view(C, ar, &cur_new, smooth_viewtx);
2390
2391                 return OPERATOR_FINISHED;
2392         }
2393         else {
2394                 return OPERATOR_CANCELLED;
2395         }
2396         
2397 }
2398
2399 void SEQUENCER_OT_view_selected(wmOperatorType *ot)
2400 {
2401         /* identifiers */
2402         ot->name = "View Selected";
2403         ot->idname = "SEQUENCER_OT_view_selected";
2404         ot->description = "Zoom the sequencer on the selected strips";
2405         
2406         /* api callbacks */
2407         ot->exec = sequencer_view_selected_exec;
2408         ot->poll = ED_operator_sequencer_active;
2409         
2410         /* flags */
2411         ot->flag = OPTYPE_REGISTER;
2412 }
2413
2414
2415 static int find_next_prev_edit(Scene *scene, int cfra,
2416                                const short side,
2417                                const short do_skip_mute, const short do_center)
2418 {
2419         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2420         Sequence *seq, *best_seq = NULL, *frame_seq = NULL;
2421         
2422         int dist, best_dist;
2423         best_dist = MAXFRAME * 2;
2424
2425         if (ed == NULL) return cfra;
2426         
2427         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2428                 int seq_frame;
2429
2430                 if (do_skip_mute && (seq->flag & SEQ_MUTE)) {
2431                         continue;
2432                 }
2433
2434                 if (do_center) {
2435                         seq_frame = (seq->startdisp + seq->enddisp) / 2;
2436                 }
2437                 else {
2438                         seq_frame = seq->startdisp;
2439                 }
2440
2441                 dist = MAXFRAME * 2;
2442                         
2443                 switch (side) {
2444                         case SEQ_SIDE_LEFT:
2445                                 if (seq_frame < cfra) {
2446                                         dist = cfra - seq_frame;
2447                                 }
2448                                 break;
2449                         case SEQ_SIDE_RIGHT:
2450                                 if (seq_frame > cfra) {
2451                                         dist = seq_frame - cfra;
2452                                 }
2453                                 else if (seq_frame == cfra) {
2454                                         frame_seq = seq;
2455                                 }
2456                                 break;
2457                 }
2458
2459                 if (dist < best_dist) {
2460                         best_dist = dist;
2461                         best_seq = seq;
2462                 }
2463         }
2464
2465         /* if no sequence to the right is found and the
2466          * frame is on the start of the last sequence,
2467          * move to the end of the last sequence */
2468         if (frame_seq) {
2469                 if (do_center) {
2470                         cfra = (frame_seq->startdisp + frame_seq->enddisp) / 2;
2471                 }
2472                 else {
2473                         cfra = frame_seq->enddisp;
2474                 }
2475         }
2476
2477         if (best_seq) {
2478                 if (do_center) {
2479                         cfra = (best_seq->startdisp + best_seq->enddisp) / 2;
2480                 }
2481                 else {
2482                         cfra = best_seq->startdisp;
2483                 }
2484         }
2485
2486         return cfra;
2487 }
2488
2489 static bool strip_jump_internal(Scene *scene,
2490                                 const short side,
2491                                 const short do_skip_mute, const short do_center)
2492 {
2493         bool changed = false;
2494         int cfra = CFRA;
2495         int nfra = find_next_prev_edit(scene, cfra, side, do_skip_mute, do_center);
2496         
2497         if (nfra != cfra) {
2498                 CFRA = nfra;
2499                 changed = true;
2500         }
2501
2502         return changed;
2503 }
2504
2505 static int sequencer_strip_jump_poll(bContext *C)
2506 {
2507         /* prevent changes during render */
2508         if (G.is_rendering)
2509                 return 0;
2510
2511         return sequencer_edit_poll(C);
2512 }
2513
2514 /* jump frame to edit point operator */
2515 static int sequencer_strip_jump_exec(bContext *C, wmOperator *op)
2516 {
2517         Scene *scene = CTX_data_scene(C);
2518         short next = RNA_boolean_get(op->ptr, "next");
2519         short center = RNA_boolean_get(op->ptr, "center");
2520
2521         /* currently do_skip_mute is always TRUE */
2522         if (!strip_jump_internal(scene, next ? SEQ_SIDE_RIGHT : SEQ_SIDE_LEFT, TRUE, center)) {
2523                 return OPERATOR_CANCELLED;
2524         }
2525
2526         WM_event_add_notifier(C, NC_SCENE | ND_FRAME, scene);
2527         
2528         return OPERATOR_FINISHED;
2529 }
2530
2531 void SEQUENCER_OT_strip_jump(wmOperatorType *ot)
2532 {
2533         /* identifiers */
2534         ot->name = "Jump to Strip";
2535         ot->idname = "SEQUENCER_OT_strip_jump";
2536         ot->description = "Move frame to previous edit point";
2537
2538         /* api callbacks */
2539         ot->exec = sequencer_strip_jump_exec;
2540         ot->poll = sequencer_strip_jump_poll;
2541
2542         /* flags */
2543         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2544         
2545         /* properties */
2546         RNA_def_boolean(ot->srna, "next", TRUE, "Next Strip", "");
2547         RNA_def_boolean(ot->srna, "center", TRUE, "Use strip center", "");
2548 }
2549
2550 static void swap_sequence(Scene *scene, Sequence *seqa, Sequence *seqb)
2551 {
2552         int gap = seqb->startdisp - seqa->enddisp;
2553         int seq_a_start;
2554         int seq_b_start;
2555
2556         seq_b_start = (seqb->start - seqb->startdisp) + seqa->startdisp;
2557         BKE_sequence_translate(scene, seqb, seq_b_start - seqb->start);
2558         BKE_sequence_calc(scene, seqb);
2559
2560         seq_a_start = (seqa->start - seqa->startdisp) + seqb->enddisp + gap;
2561         BKE_sequence_translate(scene, seqa, seq_a_start - seqa->start);
2562         BKE_sequence_calc(scene, seqa);
2563 }
2564
2565 #if 0
2566 static Sequence *sequence_find_parent(Scene *scene, Sequence *child)
2567 {
2568         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2569         Sequence *parent = NULL;
2570         Sequence *seq;
2571
2572         if (ed == NULL) return NULL;
2573
2574         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2575                 if ((seq != child) && seq_is_parent(seq, child)) {
2576                         parent = seq;
2577                         break;
2578                 }
2579         }
2580
2581         return parent;
2582 }
2583 #endif
2584
2585 static int sequencer_swap_exec(bContext *C, wmOperator *op)
2586 {
2587         Scene *scene = CTX_data_scene(C);
2588         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2589         Sequence *active_seq = BKE_sequencer_active_get(scene);
2590         Sequence *seq, *iseq;
2591         int side = RNA_enum_get(op->ptr, "side");
2592
2593         if (active_seq == NULL) return OPERATOR_CANCELLED;
2594
2595         seq = find_next_prev_sequence(scene, active_seq, side, -1);
2596         
2597         if (seq) {
2598                 
2599                 /* disallow effect strips */
2600                 if (BKE_sequence_effect_get_num_inputs(seq->type) >= 1 && (seq->effectdata || seq->seq1 || seq->seq2 || seq->seq3))
2601                         return OPERATOR_CANCELLED;
2602                 if ((BKE_sequence_effect_get_num_inputs(active_seq->type) >= 1) && (active_seq->effectdata || active_seq->seq1 || active_seq->seq2 || active_seq->seq3))
2603                         return OPERATOR_CANCELLED;
2604
2605                 switch (side) {
2606                         case SEQ_SIDE_LEFT: 
2607                                 swap_sequence(scene, seq, active_seq);
2608                                 break;
2609                         case SEQ_SIDE_RIGHT: 
2610                                 swap_sequence(scene, active_seq, seq);
2611                                 break;
2612                 }
2613
2614                 // XXX - should be a generic function
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                                 BKE_sequence_calc(scene, iseq);
2618                         }
2619                 }
2620
2621                 /* do this in a new loop since both effects need to be calculated first */
2622                 for (iseq = scene->ed->seqbasep->first; iseq; iseq = iseq->next) {
2623                         if ((iseq->type & SEQ_TYPE_EFFECT) && (seq_is_parent(iseq, active_seq) || seq_is_parent(iseq, seq))) {
2624                                 /* this may now overlap */
2625                                 if (BKE_sequence_test_overlap(ed->seqbasep, iseq) ) {
2626                                         BKE_sequence_base_shuffle(ed->seqbasep, iseq, scene);
2627                                 }
2628                         }
2629                 }
2630
2631
2632
2633                 BKE_sequencer_sort(scene);
2634
2635                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2636
2637                 return OPERATOR_FINISHED;
2638         }
2639
2640         return OPERATOR_CANCELLED;
2641 }
2642
2643 void SEQUENCER_OT_swap(wmOperatorType *ot)
2644 {
2645         /* identifiers */
2646         ot->name = "Swap Strip";
2647         ot->idname = "SEQUENCER_OT_swap";
2648         ot->description = "Swap active strip with strip to the right or left";
2649         
2650         /* api callbacks */
2651         ot->exec = sequencer_swap_exec;
2652         ot->poll = sequencer_edit_poll;
2653         
2654         /* flags */
2655         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2656         
2657         /* properties */
2658         RNA_def_enum(ot->srna, "side", prop_side_lr_types, SEQ_SIDE_RIGHT, "Side", "Side of the strip to swap");
2659 }
2660
2661 static int sequencer_rendersize_exec(bContext *C, wmOperator *UNUSED(op))
2662 {
2663         int retval = OPERATOR_CANCELLED;
2664         Scene *scene = CTX_data_scene(C);
2665         Sequence *active_seq = BKE_sequencer_active_get(scene);
2666         StripElem *se = NULL;
2667
2668         if (active_seq == NULL)
2669                 return OPERATOR_CANCELLED;
2670
2671
2672         if (active_seq->strip) {
2673                 switch (active_seq->type) {
2674                         case SEQ_TYPE_IMAGE:
2675                                 se = BKE_sequencer_give_stripelem(active_seq, scene->r.cfra);
2676                                 break;
2677                         case SEQ_TYPE_MOVIE:
2678                                 se = active_seq->strip->stripdata;
2679                                 break;
2680                         case SEQ_TYPE_SCENE:
2681                         case SEQ_TYPE_META:
2682                         case SEQ_TYPE_SOUND_RAM:
2683                         case SEQ_TYPE_SOUND_HD:
2684                         default:
2685                                 break;
2686                 }
2687         }
2688
2689         if (se) {
2690                 // prevent setting the render size if sequence values aren't initialized
2691                 if ((se->orig_width > 0) && (se->orig_height > 0)) {
2692                         scene->r.xsch = se->orig_width;
2693                         scene->r.ysch = se->orig_height;
2694                         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, scene);
2695                         retval = OPERATOR_FINISHED;
2696                 }
2697         }
2698
2699         return retval;
2700 }
2701
2702 void SEQUENCER_OT_rendersize(wmOperatorType *ot)
2703 {
2704         /* identifiers */
2705         ot->name = "Set Render Size";
2706         ot->idname = "SEQUENCER_OT_rendersize";
2707         ot->description = "Set render size and aspect from active sequence";
2708         
2709         /* api callbacks */
2710         ot->exec = sequencer_rendersize_exec;
2711         ot->poll = sequencer_edit_poll;
2712         
2713         /* flags */
2714         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2715         
2716         /* properties */
2717 }
2718
2719 static void seq_copy_del_sound(Scene *scene, Sequence *seq)
2720 {
2721         if (seq->type == SEQ_TYPE_META) {
2722                 Sequence *iseq;
2723                 for (iseq = seq->seqbase.first; iseq; iseq = iseq->next) {
2724                         seq_copy_del_sound(scene, iseq);
2725                 }
2726         }
2727         else if (seq->scene_sound) {
2728                 sound_remove_scene_sound(scene, seq->scene_sound);
2729                 seq->scene_sound = NULL;
2730         }
2731 }
2732
2733 static int sequencer_copy_exec(bContext *C, wmOperator *op)
2734 {
2735         Scene *scene = CTX_data_scene(C);
2736         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2737
2738         ListBase nseqbase = {NULL, NULL};
2739
2740         BKE_sequencer_free_clipboard();
2741
2742         if (BKE_sequence_base_isolated_sel_check(ed->seqbasep) == FALSE) {
2743                 BKE_report(op->reports, RPT_ERROR, "Please select all related strips");
2744                 return OPERATOR_CANCELLED;
2745         }
2746
2747         BKE_sequence_base_dupli_recursive(scene, NULL, &nseqbase, ed->seqbasep, SEQ_DUPE_UNIQUE_NAME);
2748
2749         /* To make sure the copied strips have unique names between each other add
2750          * them temporarily to the end of the original seqbase. (bug 25932)
2751          */
2752         if (nseqbase.first) {
2753                 Sequence *seq, *first_seq = nseqbase.first;
2754                 BLI_movelisttolist(ed->seqbasep, &nseqbase);
2755
2756                 for (seq = first_seq; seq; seq = seq->next)
2757                         BKE_sequencer_recursive_apply(seq, apply_unique_name_cb, scene);
2758
2759                 seqbase_clipboard.first = first_seq;
2760                 seqbase_clipboard.last = ed->seqbasep->last;
2761
2762                 if (first_seq->prev) {
2763                         first_seq->prev->next = NULL;
2764                         ed->seqbasep->last = first_seq->prev;
2765                         first_seq->prev = NULL;
2766                 }
2767         }
2768
2769         seqbase_clipboard_frame = scene->r.cfra;
2770
2771         /* Need to remove anything that references the current scene */
2772         {
2773                 Sequence *seq;
2774                 for (seq = seqbase_clipboard.first; seq; seq = seq->next) {
2775                         seq_copy_del_sound(scene, seq);
2776                 }
2777
2778                 /* duplicate pointers */
2779                 for (seq = seqbase_clipboard.first; seq; seq = seq->next) {
2780                         BKE_sequence_clipboard_pointers_store(seq);
2781                 }
2782         }
2783
2784         return OPERATOR_FINISHED;
2785 }
2786
2787 void SEQUENCER_OT_copy(wmOperatorType *ot)
2788 {
2789         /* identifiers */
2790         ot->name = "Copy";
2791         ot->idname = "SEQUENCER_OT_copy";
2792         ot->description = "";
2793
2794         /* api callbacks */
2795         ot->exec = sequencer_copy_exec;
2796         ot->poll = sequencer_edit_poll;
2797
2798         /* flags */
2799         ot->flag = OPTYPE_REGISTER;
2800
2801         /* properties */
2802 }
2803
2804 static int sequencer_paste_exec(bContext *C, wmOperator *UNUSED(op))
2805 {
2806         Main *bmain = CTX_data_main(C);
2807         Scene *scene = CTX_data_scene(C);
2808         Editing *ed = BKE_sequencer_editing_get(scene, TRUE); /* create if needed */
2809         ListBase nseqbase = {NULL, NULL};
2810         int ofs;
2811         Sequence *iseq, *iseq_first;
2812
2813         ED_sequencer_deselect_all(scene);
2814         ofs = scene->r.cfra - seqbase_clipboard_frame;
2815
2816         BKE_sequence_base_dupli_recursive(scene, NULL, &nseqbase, &seqbase_clipboard, SEQ_DUPE_UNIQUE_NAME);
2817
2818         /* transform pasted strips before adding */
2819         if (ofs) {
2820                 for (iseq = nseqbase.first; iseq; iseq = iseq->next) {
2821                         BKE_sequence_translate(scene, iseq, ofs);
2822                 }
2823         }
2824
2825         for (iseq = nseqbase.first; iseq; iseq = iseq->next) {
2826                 BKE_sequence_clipboard_pointers_restore(iseq, bmain);
2827         }
2828
2829         for (iseq = nseqbase.first; iseq; iseq = iseq->next) {
2830                 BKE_sequence_sound_init(scene, iseq);
2831         }
2832
2833         iseq_first = nseqbase.first;
2834
2835         BLI_movelisttolist(ed->seqbasep, &nseqbase);
2836
2837         /* make sure the pasted strips have unique names between them */
2838         for (iseq = iseq_first; iseq; iseq = iseq->next) {
2839                 BKE_sequencer_recursive_apply(iseq, apply_unique_name_cb, scene);
2840         }
2841
2842         /* ensure pasted strips don't overlap */
2843         for (iseq = iseq_first; iseq; iseq = iseq->next) {
2844                 if (BKE_sequence_test_overlap(ed->seqbasep, iseq)) {
2845                         BKE_sequence_base_shuffle(ed->seqbasep, iseq, scene);
2846                 }
2847         }
2848
2849         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2850
2851         return OPERATOR_FINISHED;
2852 }
2853
2854 void SEQUENCER_OT_paste(wmOperatorType *ot)
2855 {
2856         /* identifiers */
2857         ot->name = "Paste";
2858         ot->idname = "SEQUENCER_OT_paste";
2859         ot->description = "";
2860
2861         /* api callbacks */
2862         ot->exec = sequencer_paste_exec;
2863         ot->poll = ED_operator_sequencer_active;
2864
2865         /* flags */
2866         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2867
2868         /* properties */
2869 }
2870
2871 static int sequencer_swap_data_exec(bContext *C, wmOperator *op)
2872 {
2873         Scene *scene = CTX_data_scene(C);
2874         Sequence *seq_act;
2875         Sequence *seq_other;
2876         const char *error_msg;
2877
2878         if (BKE_sequencer_active_get_pair(scene, &seq_act, &seq_other) == 0) {
2879                 BKE_report(op->reports, RPT_ERROR, "Please select two strips");
2880                 return OPERATOR_CANCELLED;
2881         }
2882
2883         if (BKE_sequence_swap(seq_act, seq_other, &error_msg) == 0) {
2884                 BKE_report(op->reports, RPT_ERROR, error_msg);
2885                 return OPERATOR_CANCELLED;
2886         }
2887
2888         if (seq_act->scene_sound)
2889                 sound_remove_scene_sound(scene, seq_act->scene_sound);
2890
2891         if (seq_other->scene_sound)
2892                 sound_remove_scene_sound(scene, seq_other->scene_sound);
2893
2894         seq_act->scene_sound = NULL;
2895         seq_other->scene_sound = NULL;
2896
2897         BKE_sequence_calc(scene, seq_act);
2898         BKE_sequence_calc(scene, seq_other);
2899
2900         if (seq_act->sound) sound_add_scene_sound_defaults(scene, seq_act);
2901         if (seq_other->sound) sound_add_scene_sound_defaults(scene, seq_other);
2902
2903         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2904
2905         return OPERATOR_FINISHED;
2906 }
2907
2908 void SEQUENCER_OT_swap_data(wmOperatorType *ot)
2909 {
2910         /* identifiers */
2911         ot->name = "Sequencer Swap Data";
2912         ot->idname = "SEQUENCER_OT_swap_data";
2913         ot->description = "Swap 2 sequencer strips";
2914
2915         /* api callbacks */
2916         ot->exec = sequencer_swap_data_exec;
2917         ot->poll = ED_operator_sequencer_active;
2918
2919         /* flags */
2920         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2921
2922         /* properties */
2923 }
2924
2925 /* borderselect operator */
2926 static int view_ghost_border_exec(bContext *C, wmOperator *op)
2927 {
2928         Scene *scene = CTX_data_scene(C);
2929         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2930         View2D *v2d = UI_view2d_fromcontext(C);
2931
2932         rctf rect;
2933
2934         /* convert coordinates of rect to 'tot' rect coordinates */
2935         UI_view2d_region_to_view(v2d, RNA_int_get(op->ptr, "xmin"), RNA_int_get(op->ptr, "ymin"), &rect.xmin, &rect.ymin);
2936         UI_view2d_region_to_view(v2d, RNA_int_get(op->ptr, "xmax"), RNA_int_get(op->ptr, "ymax"), &rect.xmax, &rect.ymax);
2937
2938         if (ed == NULL)
2939                 return OPERATOR_CANCELLED;
2940
2941         rect.xmin /=  fabsf(BLI_rctf_size_x(&v2d->tot));
2942         rect.ymin /=  fabsf(BLI_rctf_size_y(&v2d->tot));
2943
2944         rect.xmax /=  fabsf(BLI_rctf_size_x(&v2d->tot));
2945         rect.ymax /=  fabsf(BLI_rctf_size_y(&v2d->tot));
2946
2947         rect.xmin += 0.5f;
2948         rect.xmax += 0.5f;
2949         rect.ymin += 0.5f;
2950         rect.ymax += 0.5f;
2951
2952         CLAMP(rect.xmin, 0.0f, 1.0f);
2953         CLAMP(rect.ymin, 0.0f, 1.0f);
2954         CLAMP(rect.xmax, 0.0f, 1.0f);
2955         CLAMP(rect.ymax, 0.0f, 1.0f);
2956
2957         scene->ed->over_border = rect;
2958
2959         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2960
2961         return OPERATOR_FINISHED;
2962 }
2963
2964 /* ****** Border Select ****** */
2965 void SEQUENCER_OT_view_ghost_border(wmOperatorType *ot)
2966 {
2967         /* identifiers */
2968         ot->name = "Border Offset View";
2969         ot->idname = "SEQUENCER_OT_view_ghost_border";
2970         ot->description = "Set the boundaries of the border used for offset-view";
2971
2972         /* api callbacks */
2973         ot->invoke = WM_border_select_invoke;
2974         ot->exec = view_ghost_border_exec;
2975         ot->modal = WM_border_select_modal;
2976         ot->poll = sequencer_view_poll;
2977         ot->cancel = WM_border_select_cancel;
2978
2979         /* flags */
2980         ot->flag = 0;
2981
2982         /* rna */
2983         WM_operator_properties_gesture_border(ot, FALSE);
2984 }
2985
2986 /* rebuild_proxy operator */
2987 static int sequencer_rebuild_proxy_exec(bContext *C, wmOperator *UNUSED(op))
2988 {
2989         seq_proxy_build_job(C);
2990
2991         return OPERATOR_FINISHED;
2992 }
2993
2994 void SEQUENCER_OT_rebuild_proxy(wmOperatorType *ot)
2995 {
2996         /* identifiers */
2997         ot->name = "Rebuild Proxy and Timecode Indices";
2998         ot->idname = "SEQUENCER_OT_rebuild_proxy";
2999         ot->description = "Rebuild all selected proxies and timecode indices using the job system";
3000         
3001         /* api callbacks */
3002         ot->exec = sequencer_rebuild_proxy_exec;
3003         ot->poll = ED_operator_sequencer_active;
3004         
3005         /* flags */
3006         ot->flag = OPTYPE_REGISTER;
3007 }
3008
3009 /* change ops */
3010
3011 static EnumPropertyItem prop_change_effect_input_types[] = {
3012         {0, "A_B", 0, "A -> B", ""},
3013         {1, "B_C", 0, "B -> C", ""},
3014         {2, "A_C", 0, "A -> C", ""},
3015         {0, NULL, 0, NULL, NULL}
3016 };
3017
3018 static int sequencer_change_effect_input_exec(bContext *C, wmOperator *op)
3019 {
3020         Scene *scene = CTX_data_scene(C);
3021         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
3022         Sequence *seq = BKE_sequencer_active_get(scene);
3023
3024         Sequence **seq_1, **seq_2;
3025
3026         switch (RNA_enum_get(op->ptr, "swap")) {
3027                 case 0:
3028                         seq_1 = &seq->seq1;
3029                         seq_2 = &seq->seq2;
3030                         break;
3031                 case 1:
3032                         seq_1 = &seq->seq2;
3033                         seq_2 = &seq->seq3;
3034                         break;
3035                 default: /* 2 */
3036                         seq_1 = &seq->seq1;
3037                         seq_2 = &seq->seq3;
3038                         break;
3039         }
3040
3041         if (*seq_1 == NULL || *seq_2 == NULL) {
3042                 BKE_report(op->reports, RPT_ERROR, "One of the effect inputs is unset, cannot swap");
3043                 return OPERATOR_CANCELLED;
3044         }
3045         else {
3046                 SWAP(Sequence *, *seq_1, *seq_2);
3047         }
3048
3049         BKE_sequencer_update_changed_seq_and_deps(scene, seq, 0, 1);
3050
3051         /* important else we don't get the imbuf cache flushed */
3052         BKE_sequencer_free_imbuf(scene, &ed->seqbase, FALSE);
3053
3054         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
3055
3056         return OPERATOR_FINISHED;
3057 }
3058
3059 void SEQUENCER_OT_change_effect_input(struct wmOperatorType *ot)
3060 {
3061         /* identifiers */
3062         ot->name = "Change Effect Input";
3063         ot->idname = "SEQUENCER_OT_change_effect_input";
3064         ot->description = "";
3065
3066         /* api callbacks */
3067         ot->exec = sequencer_change_effect_input_exec;
3068         ot->poll = sequencer_effect_poll;
3069
3070         /* flags */
3071         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
3072
3073         ot->prop = RNA_def_enum(ot->srna, "swap", prop_change_effect_input_types, 0, "Swap", "The effect inputs to swap");
3074 }
3075
3076 static int sequencer_change_effect_type_exec(bContext *C, wmOperator *op)
3077 {
3078         Scene *scene = CTX_data_scene(C);
3079         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
3080         Sequence *seq = BKE_sequencer_active_get(scene);
3081         const int new_type = RNA_enum_get(op->ptr, "type");
3082
3083         /* free previous effect and init new effect */
3084         struct SeqEffectHandle sh;
3085
3086         if ((seq->type & SEQ_TYPE_EFFECT) == 0) {
3087                 return OPERATOR_CANCELLED;
3088         }
3089
3090         /* can someone explain the logic behind only allowing to increase this,
3091          * copied from 2.4x - campbell */
3092         if (BKE_sequence_effect_get_num_inputs(seq->type) <
3093             BKE_sequence_effect_get_num_inputs(new_type))
3094         {
3095                 BKE_report(op->reports, RPT_ERROR, "New effect needs more input strips");
3096                 return OPERATOR_CANCELLED;
3097         }
3098         else {
3099                 sh = BKE_sequence_get_effect(seq);
3100                 sh.free(seq);
3101
3102                 seq->type = new_type;
3103
3104                 sh = BKE_sequence_get_effect(seq);
3105                 sh.init(seq);
3106         }
3107
3108         /* update */
3109         BKE_sequencer_update_changed_seq_and_deps(scene, seq, 0, 1);
3110
3111         /* important else we don't get the imbuf cache flushed */
3112         BKE_sequencer_free_imbuf(scene, &ed->seqbase, FALSE);
3113
3114         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
3115
3116         return OPERATOR_FINISHED;
3117 }
3118
3119 void SEQUENCER_OT_change_effect_type(struct wmOperatorType *ot)
3120 {
3121         /* identifiers */
3122         ot->name = "Change Effect Type";
3123         ot->idname = "SEQUENCER_OT_change_effect_type";
3124         ot->description = "";
3125
3126         /* api callbacks */
3127         ot->exec = sequencer_change_effect_type_exec;
3128         ot->poll = sequencer_effect_poll;
3129
3130         /* flags */
3131         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
3132
3133         ot->prop = RNA_def_enum(ot->srna, "type", sequencer_prop_effect_types, SEQ_TYPE_CROSS, "Type", "Sequencer effect type");
3134 }
3135
3136 static int sequencer_change_path_exec(bContext *C, wmOperator *op)
3137 {
3138         Main *bmain = CTX_data_main(C);
3139         Scene *scene = CTX_data_scene(C);
3140         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
3141         Sequence *seq = BKE_sequencer_active_get(scene);
3142         const int is_relative_path = RNA_boolean_get(op->ptr, "relative_path");
3143
3144         if (seq->type == SEQ_TYPE_IMAGE) {
3145                 char directory[FILE_MAX];
3146                 const int len = RNA_property_collection_length(op->ptr, RNA_struct_find_property(op->ptr, "files"));
3147                 StripElem *se;
3148
3149                 if (len == 0)
3150                         return OPERATOR_CANCELLED;
3151
3152                 RNA_string_get(op->ptr, "directory", directory);
3153                 if (is_relative_path) {
3154                         /* TODO, shouldn't this already be relative from the filesel?
3155                          * (as the 'filepath' is) for now just make relative here,
3156                          * but look into changing after 2.60 - campbell */
3157                         BLI_path_rel(directory, bmain->name);
3158                 }
3159                 BLI_strncpy(seq->strip->dir, directory, sizeof(seq->strip->dir));
3160
3161                 if (seq->strip->stripdata) {
3162                         MEM_freeN(seq->strip->stripdata);
3163                 }
3164                 seq->strip->stripdata = se = MEM_callocN(len * sizeof(StripElem), "stripelem");
3165
3166                 RNA_BEGIN (op->ptr, itemptr, "files")
3167                 {
3168                         char *filename = RNA_string_get_alloc(&itemptr, "name", NULL, 0);
3169                         BLI_strncpy(se->name, filename, sizeof(se->name));
3170                         MEM_freeN(filename);
3171                         se++;
3172                 }
3173                 RNA_END;
3174
3175                 /* reset these else we wont see all the images */
3176                 seq->anim_startofs = seq->anim_endofs = 0;
3177
3178                 /* correct start/end frames so we don't move
3179                  * important not to set seq->len = len; allow the function to handle it */
3180                 BKE_sequence_reload_new_file(scene, seq, TRUE);
3181
3182                 BKE_sequence_calc(scene, seq);
3183
3184                 /* important else we don't get the imbuf cache flushed */
3185                 BKE_sequencer_free_imbuf(scene, &ed->seqbase, FALSE);
3186         }
3187         else {
3188                 /* lame, set rna filepath */
3189                 PointerRNA seq_ptr;
3190                 PropertyRNA *prop;
3191                 char filepath[FILE_MAX];
3192
3193                 RNA_pointer_create(&scene->id, &RNA_Sequence, seq, &seq_ptr);
3194
3195                 RNA_string_get(op->ptr, "filepath", filepath);
3196                 prop = RNA_struct_find_property(&seq_ptr, "filepath");
3197                 RNA_property_string_set(&seq_ptr, prop, filepath);
3198                 RNA_property_update(C, &seq_ptr, prop);
3199         }
3200
3201         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
3202
3203         return OPERATOR_FINISHED;
3204 }
3205
3206 static int sequencer_change_path_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))