ListBase API: add utility api funcs for clearing and checking empty
[blender-staging.git] / source / blender / editors / space_sequencer / sequencer_edit.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2003-2009
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/space_sequencer/sequencer_edit.c
27  *  \ingroup spseq
28  */
29
30
31 #include <stdlib.h>
32 #include <math.h>
33 #include <string.h>
34
35 #ifndef WIN32
36 #include <unistd.h>
37 #else
38 #include <io.h>
39 #endif
40 #include <sys/types.h>
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_math.h"
46 #include "BLI_utildefines.h"
47 #include "BLI_threads.h"
48
49 #include "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 bool seq_is_parent(Sequence *par, Sequence *seq)
423 {
424         return ((par->seq1 == seq) || (par->seq2 == seq) || (par->seq3 == seq));
425 }
426
427 static bool 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 bool 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 bool 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         const bool 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 (BLI_listbase_is_empty(&ed->metastack))
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         BLI_listbase_clear(&last_seq->seqbase);
2118
2119         BLI_remlink(ed->seqbasep, last_seq);
2120         BKE_sequence_free(scene, last_seq);
2121
2122         /* emtpy meta strip, delete all effects depending on it */
2123         for (seq = ed->seqbasep->first; seq; seq = seq->next)
2124                 if ((seq->type & SEQ_TYPE_EFFECT) && seq_depends_on_meta(seq, last_seq))
2125                         seq->flag |= SEQ_FLAG_DELETE;
2126
2127         recurs_del_seq_flag(scene, ed->seqbasep, SEQ_FLAG_DELETE, 0);
2128
2129         /* test for effects and overlap
2130          * don't use SEQP_BEGIN since that would be recursive */
2131         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2132                 if (seq->flag & SELECT) {
2133                         seq->flag &= ~SEQ_OVERLAP;
2134                         if (BKE_sequence_test_overlap(ed->seqbasep, seq)) {
2135                                 BKE_sequence_base_shuffle(ed->seqbasep, seq, scene);
2136                         }
2137                 }
2138         }
2139
2140         BKE_sequencer_sort(scene);
2141         BKE_sequencer_update_muting(ed);
2142
2143         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2144
2145         return OPERATOR_FINISHED;
2146 }
2147
2148 void SEQUENCER_OT_meta_separate(wmOperatorType *ot)
2149 {
2150         /* identifiers */
2151         ot->name = "UnMeta Strip";
2152         ot->idname = "SEQUENCER_OT_meta_separate";
2153         ot->description = "Put the contents of a metastrip back in the sequencer";
2154         
2155         /* api callbacks */
2156         ot->exec = sequencer_meta_separate_exec;
2157         ot->poll = sequencer_edit_poll;
2158         
2159         /* flags */
2160         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2161 }
2162
2163 /* view_all operator */
2164 static int sequencer_view_all_exec(bContext *C, wmOperator *op)
2165 {
2166         ARegion *ar = CTX_wm_region(C);
2167         View2D *v2d = UI_view2d_fromcontext(C);
2168         const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
2169
2170         UI_view2d_smooth_view(C, ar, &v2d->tot, smooth_viewtx);
2171         return OPERATOR_FINISHED;
2172 }
2173
2174 void SEQUENCER_OT_view_all(wmOperatorType *ot)
2175 {
2176         /* identifiers */
2177         ot->name = "View All";
2178         ot->idname = "SEQUENCER_OT_view_all";
2179         ot->description = "View all the strips in the sequencer";
2180         
2181         /* api callbacks */
2182         ot->exec = sequencer_view_all_exec;
2183         ot->poll = ED_operator_sequencer_active;
2184         
2185         /* flags */
2186         ot->flag = OPTYPE_REGISTER;
2187 }
2188
2189 /* view_all operator */
2190 static int sequencer_view_all_preview_exec(bContext *C, wmOperator *UNUSED(op))
2191 {
2192         bScreen *sc = CTX_wm_screen(C);
2193         ScrArea *area = CTX_wm_area(C);
2194 #if 0
2195         ARegion *ar = CTX_wm_region(C);
2196         SpaceSeq *sseq = area->spacedata.first;
2197         Scene *scene = CTX_data_scene(C);
2198 #endif
2199         View2D *v2d = UI_view2d_fromcontext(C);
2200
2201         v2d->cur = v2d->tot;
2202         UI_view2d_curRect_validate(v2d);
2203         UI_view2d_sync(sc, area, v2d, V2D_LOCK_COPY);
2204         
2205 #if 0
2206         /* Like zooming on an image view */
2207         float zoomX, zoomY;
2208         int width, height, imgwidth, imgheight;
2209
2210         width = ar->winx;
2211         height = ar->winy;
2212
2213         seq_reset_imageofs(sseq);
2214
2215         imgwidth = (scene->r.size * scene->r.xsch) / 100;
2216         imgheight = (scene->r.size * scene->r.ysch) / 100;
2217
2218         /* Apply aspect, dosnt need to be that accurate */
2219         imgwidth = (int)(imgwidth * (scene->r.xasp / scene->r.yasp));
2220
2221         if (((imgwidth >= width) || (imgheight >= height)) &&
2222             ((width > 0) && (height > 0)))
2223         {
2224                 /* Find the zoom value that will fit the image in the image space */
2225                 zoomX = ((float)width) / ((float)imgwidth);
2226                 zoomY = ((float)height) / ((float)imgheight);
2227                 sseq->zoom = (zoomX < zoomY) ? zoomX : zoomY;
2228
2229                 sseq->zoom = 1.0f / power_of_2(1 / min_ff(zoomX, zoomY));
2230         }
2231         else {
2232                 sseq->zoom = 1.0f;
2233         }
2234 #endif
2235
2236         ED_area_tag_redraw(CTX_wm_area(C));
2237         return OPERATOR_FINISHED;
2238 }
2239
2240 void SEQUENCER_OT_view_all_preview(wmOperatorType *ot)
2241 {
2242         /* identifiers */
2243         ot->name = "View All";
2244         ot->idname = "SEQUENCER_OT_view_all_preview";
2245         ot->description = "Zoom preview to fit in the area";
2246         
2247         /* api callbacks */
2248         ot->exec = sequencer_view_all_preview_exec;
2249         ot->poll = ED_operator_sequencer_active;
2250         
2251         /* flags */
2252         ot->flag = OPTYPE_REGISTER;
2253 }
2254
2255
2256 static int sequencer_view_zoom_ratio_exec(bContext *C, wmOperator *op)
2257 {
2258         RenderData *rd = &CTX_data_scene(C)->r;
2259         View2D *v2d = UI_view2d_fromcontext(C);
2260
2261         float ratio = RNA_float_get(op->ptr, "ratio");
2262
2263         float winx = (int)(rd->size * rd->xsch) / 100;
2264         float winy = (int)(rd->size * rd->ysch) / 100;
2265
2266         float facx = BLI_rcti_size_x(&v2d->mask) / winx;
2267         float facy = BLI_rcti_size_y(&v2d->mask) / winy;
2268
2269         BLI_rctf_resize(&v2d->cur, (int)(winx * facx * ratio) + 1, (int)(winy * facy * ratio) + 1);
2270
2271         ED_region_tag_redraw(CTX_wm_region(C));
2272
2273         return OPERATOR_FINISHED;
2274 }
2275
2276 void SEQUENCER_OT_view_zoom_ratio(wmOperatorType *ot)
2277 {
2278         /* identifiers */
2279         ot->name = "Sequencer View Zoom Ratio";
2280         ot->idname = "SEQUENCER_OT_view_zoom_ratio";
2281         ot->description = "Change zoom ratio of sequencer preview";
2282
2283         /* api callbacks */
2284         ot->exec = sequencer_view_zoom_ratio_exec;
2285         ot->poll = ED_operator_sequencer_active;
2286
2287         /* properties */
2288         RNA_def_float(ot->srna, "ratio", 1.0f, -FLT_MAX, FLT_MAX,
2289                       "Ratio", "Zoom ratio, 1.0 is 1:1, higher is zoomed in, lower is zoomed out", -FLT_MAX, FLT_MAX);
2290 }
2291
2292
2293 #if 0
2294 static EnumPropertyItem view_type_items[] = {
2295         {SEQ_VIEW_SEQUENCE, "SEQUENCER", ICON_SEQ_SEQUENCER, "Sequencer", ""},
2296         {SEQ_VIEW_PREVIEW,  "PREVIEW", ICON_SEQ_PREVIEW, "Image Preview", ""},
2297         {SEQ_VIEW_SEQUENCE_PREVIEW,  "SEQUENCER_PREVIEW", ICON_SEQ_SEQUENCER, "Sequencer and Image Preview", ""},
2298         {0, NULL, 0, NULL, NULL}
2299 };
2300 #endif
2301
2302 /* view_all operator */
2303 static int sequencer_view_toggle_exec(bContext *C, wmOperator *UNUSED(op))
2304 {
2305         SpaceSeq *sseq = (SpaceSeq *)CTX_wm_space_data(C);
2306
2307         sseq->view++;
2308         if (sseq->view > SEQ_VIEW_SEQUENCE_PREVIEW) sseq->view = SEQ_VIEW_SEQUENCE;
2309
2310         ED_area_tag_refresh(CTX_wm_area(C));
2311
2312         return OPERATOR_FINISHED;
2313 }
2314
2315 void SEQUENCER_OT_view_toggle(wmOperatorType *ot)
2316 {
2317         /* identifiers */
2318         ot->name = "View Toggle";
2319         ot->idname = "SEQUENCER_OT_view_toggle";
2320         ot->description = "Toggle between sequencer views (sequence, preview, both)";
2321         
2322         /* api callbacks */
2323         ot->exec = sequencer_view_toggle_exec;
2324         ot->poll = ED_operator_sequencer_active;
2325         
2326         /* flags */
2327         ot->flag = OPTYPE_REGISTER;
2328 }
2329
2330
2331 /* view_selected operator */
2332 static int sequencer_view_selected_exec(bContext *C, wmOperator *op)
2333 {
2334         Scene *scene = CTX_data_scene(C);
2335         View2D *v2d = UI_view2d_fromcontext(C);
2336         ARegion *ar = CTX_wm_region(C);
2337         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2338         Sequence *last_seq = BKE_sequencer_active_get(scene);
2339         Sequence *seq;
2340         rctf cur_new = v2d->cur;
2341
2342         int xmin =  MAXFRAME * 2;
2343         int xmax = -MAXFRAME * 2;
2344         int ymin =  MAXSEQ + 1;
2345         int ymax = 0;
2346         int orig_height;
2347         int ymid;
2348         int ymargin = 1;
2349         int xmargin = FPS;
2350
2351         if (ed == NULL)
2352                 return OPERATOR_CANCELLED;
2353
2354         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2355                 if ((seq->flag & SELECT) || (seq == last_seq)) {
2356                         xmin = min_ii(xmin, seq->startdisp);
2357                         xmax = max_ii(xmax, seq->enddisp);
2358
2359                         ymin = min_ii(ymin, seq->machine);
2360                         ymax = max_ii(ymax, seq->machine);
2361                 }
2362         }
2363
2364         if (ymax != 0) {
2365                 const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
2366                 
2367                 xmax += xmargin;
2368                 xmin -= xmargin;
2369                 ymax += ymargin;
2370                 ymin -= ymargin;
2371
2372                 orig_height = BLI_rctf_size_y(&cur_new);
2373
2374                 cur_new.xmin = xmin;
2375                 cur_new.xmax = xmax;
2376
2377                 cur_new.ymin = ymin;
2378                 cur_new.ymax = ymax;
2379
2380                 /* only zoom out vertically */
2381                 if (orig_height > BLI_rctf_size_y(&cur_new)) {
2382                         ymid = BLI_rctf_cent_y(&cur_new);
2383
2384                         cur_new.ymin = ymid - (orig_height / 2);
2385                         cur_new.ymax = ymid + (orig_height / 2);
2386                 }
2387
2388                 UI_view2d_smooth_view(C, ar, &cur_new, smooth_viewtx);
2389
2390                 return OPERATOR_FINISHED;
2391         }
2392         else {
2393                 return OPERATOR_CANCELLED;
2394         }
2395         
2396 }
2397
2398 void SEQUENCER_OT_view_selected(wmOperatorType *ot)
2399 {
2400         /* identifiers */
2401         ot->name = "View Selected";
2402         ot->idname = "SEQUENCER_OT_view_selected";
2403         ot->description = "Zoom the sequencer on the selected strips";
2404         
2405         /* api callbacks */
2406         ot->exec = sequencer_view_selected_exec;
2407         ot->poll = ED_operator_sequencer_active;
2408         
2409         /* flags */
2410         ot->flag = OPTYPE_REGISTER;
2411 }
2412
2413
2414 static int find_next_prev_edit(Scene *scene, int cfra,
2415                                const short side,
2416                                const bool do_skip_mute, const bool do_center)
2417 {
2418         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2419         Sequence *seq, *best_seq = NULL, *frame_seq = NULL;
2420         
2421         int dist, best_dist;
2422         best_dist = MAXFRAME * 2;
2423
2424         if (ed == NULL) return cfra;
2425         
2426         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2427                 int seq_frame;
2428
2429                 if (do_skip_mute && (seq->flag & SEQ_MUTE)) {
2430                         continue;
2431                 }
2432
2433                 if (do_center) {
2434                         seq_frame = (seq->startdisp + seq->enddisp) / 2;
2435                 }
2436                 else {
2437                         seq_frame = seq->startdisp;
2438                 }
2439
2440                 dist = MAXFRAME * 2;
2441                         
2442                 switch (side) {
2443                         case SEQ_SIDE_LEFT:
2444                                 if (seq_frame < cfra) {
2445                                         dist = cfra - seq_frame;
2446                                 }
2447                                 break;
2448                         case SEQ_SIDE_RIGHT:
2449                                 if (seq_frame > cfra) {
2450                                         dist = seq_frame - cfra;
2451                                 }
2452                                 else if (seq_frame == cfra) {
2453                                         frame_seq = seq;
2454                                 }
2455                                 break;
2456                 }
2457
2458                 if (dist < best_dist) {
2459                         best_dist = dist;
2460                         best_seq = seq;
2461                 }
2462         }
2463
2464         /* if no sequence to the right is found and the
2465          * frame is on the start of the last sequence,
2466          * move to the end of the last sequence */
2467         if (frame_seq) {
2468                 if (do_center) {
2469                         cfra = (frame_seq->startdisp + frame_seq->enddisp) / 2;
2470                 }
2471                 else {
2472                         cfra = frame_seq->enddisp;
2473                 }
2474         }
2475
2476         if (best_seq) {
2477                 if (do_center) {
2478                         cfra = (best_seq->startdisp + best_seq->enddisp) / 2;
2479                 }
2480                 else {
2481                         cfra = best_seq->startdisp;
2482                 }
2483         }
2484
2485         return cfra;
2486 }
2487
2488 static bool strip_jump_internal(Scene *scene,
2489                                 const short side,
2490                                 const bool do_skip_mute, const bool do_center)
2491 {
2492         bool changed = false;
2493         int cfra = CFRA;
2494         int nfra = find_next_prev_edit(scene, cfra, side, do_skip_mute, do_center);
2495         
2496         if (nfra != cfra) {
2497                 CFRA = nfra;
2498                 changed = true;
2499         }
2500
2501         return changed;
2502 }
2503
2504 static int sequencer_strip_jump_poll(bContext *C)
2505 {
2506         /* prevent changes during render */
2507         if (G.is_rendering)
2508                 return 0;
2509
2510         return sequencer_edit_poll(C);
2511 }
2512
2513 /* jump frame to edit point operator */
2514 static int sequencer_strip_jump_exec(bContext *C, wmOperator *op)
2515 {
2516         Scene *scene = CTX_data_scene(C);
2517         const bool next = RNA_boolean_get(op->ptr, "next");
2518         const bool center = RNA_boolean_get(op->ptr, "center");
2519
2520         /* currently do_skip_mute is always TRUE */
2521         if (!strip_jump_internal(scene, next ? SEQ_SIDE_RIGHT : SEQ_SIDE_LEFT, TRUE, center)) {
2522                 return OPERATOR_CANCELLED;
2523         }
2524
2525         WM_event_add_notifier(C, NC_SCENE | ND_FRAME, scene);
2526         
2527         return OPERATOR_FINISHED;
2528 }
2529
2530 void SEQUENCER_OT_strip_jump(wmOperatorType *ot)
2531 {
2532         /* identifiers */
2533         ot->name = "Jump to Strip";
2534         ot->idname = "SEQUENCER_OT_strip_jump";
2535         ot->description = "Move frame to previous edit point";
2536
2537         /* api callbacks */
2538         ot->exec = sequencer_strip_jump_exec;
2539         ot->poll = sequencer_strip_jump_poll;
2540
2541         /* flags */
2542         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2543         
2544         /* properties */
2545         RNA_def_boolean(ot->srna, "next", TRUE, "Next Strip", "");
2546         RNA_def_boolean(ot->srna, "center", TRUE, "Use strip center", "");
2547 }
2548
2549 static void swap_sequence(Scene *scene, Sequence *seqa, Sequence *seqb)
2550 {
2551         int gap = seqb->startdisp - seqa->enddisp;
2552         int seq_a_start;
2553         int seq_b_start;
2554
2555         seq_b_start = (seqb->start - seqb->startdisp) + seqa->startdisp;
2556         BKE_sequence_translate(scene, seqb, seq_b_start - seqb->start);
2557         BKE_sequence_calc(scene, seqb);
2558
2559         seq_a_start = (seqa->start - seqa->startdisp) + seqb->enddisp + gap;
2560         BKE_sequence_translate(scene, seqa, seq_a_start - seqa->start);
2561         BKE_sequence_calc(scene, seqa);
2562 }
2563
2564 #if 0
2565 static Sequence *sequence_find_parent(Scene *scene, Sequence *child)
2566 {
2567         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2568         Sequence *parent = NULL;
2569         Sequence *seq;
2570
2571         if (ed == NULL) return NULL;
2572
2573         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2574                 if ((seq != child) && seq_is_parent(seq, child)) {
2575                         parent = seq;
2576                         break;
2577                 }
2578         }
2579
2580         return parent;
2581 }
2582 #endif
2583
2584 static int sequencer_swap_exec(bContext *C, wmOperator *op)
2585 {
2586         Scene *scene = CTX_data_scene(C);
2587         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2588         Sequence *active_seq = BKE_sequencer_active_get(scene);
2589         Sequence *seq, *iseq;
2590         int side = RNA_enum_get(op->ptr, "side");
2591
2592         if (active_seq == NULL) return OPERATOR_CANCELLED;
2593
2594         seq = find_next_prev_sequence(scene, active_seq, side, -1);
2595         
2596         if (seq) {
2597                 
2598                 /* disallow effect strips */
2599                 if (BKE_sequence_effect_get_num_inputs(seq->type) >= 1 && (seq->effectdata || seq->seq1 || seq->seq2 || seq->seq3))
2600                         return OPERATOR_CANCELLED;
2601                 if ((BKE_sequence_effect_get_num_inputs(active_seq->type) >= 1) && (active_seq->effectdata || active_seq->seq1 || active_seq->seq2 || active_seq->seq3))
2602                         return OPERATOR_CANCELLED;
2603
2604                 switch (side) {
2605                         case SEQ_SIDE_LEFT: 
2606                                 swap_sequence(scene, seq, active_seq);
2607                                 break;
2608                         case SEQ_SIDE_RIGHT: 
2609                                 swap_sequence(scene, active_seq, seq);
2610                                 break;
2611                 }
2612
2613                 // XXX - should be a generic function
2614                 for (iseq = scene->ed->seqbasep->first; iseq; iseq = iseq->next) {
2615                         if ((iseq->type & SEQ_TYPE_EFFECT) && (seq_is_parent(iseq, active_seq) || seq_is_parent(iseq, seq))) {
2616                                 BKE_sequence_calc(scene, iseq);
2617                         }
2618                 }
2619
2620                 /* do this in a new loop since both effects need to be calculated first */
2621                 for (iseq = scene->ed->seqbasep->first; iseq; iseq = iseq->next) {
2622                         if ((iseq->type & SEQ_TYPE_EFFECT) && (seq_is_parent(iseq, active_seq) || seq_is_parent(iseq, seq))) {
2623                                 /* this may now overlap */
2624                                 if (BKE_sequence_test_overlap(ed->seqbasep, iseq) ) {
2625                                         BKE_sequence_base_shuffle(ed->seqbasep, iseq, scene);
2626                                 }
2627                         }
2628                 }
2629
2630
2631
2632                 BKE_sequencer_sort(scene);
2633
2634                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2635
2636                 return OPERATOR_FINISHED;
2637         }
2638
2639         return OPERATOR_CANCELLED;
2640 }
2641
2642 void SEQUENCER_OT_swap(wmOperatorType *ot)
2643 {
2644         /* identifiers */
2645         ot->name = "Swap Strip";
2646         ot->idname = "SEQUENCER_OT_swap";
2647         ot->description = "Swap active strip with strip to the right or left";
2648         
2649         /* api callbacks */
2650         ot->exec = sequencer_swap_exec;
2651         ot->poll = sequencer_edit_poll;
2652         
2653         /* flags */
2654         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2655         
2656         /* properties */
2657         RNA_def_enum(ot->srna, "side", prop_side_lr_types, SEQ_SIDE_RIGHT, "Side", "Side of the strip to swap");
2658 }
2659
2660 static int sequencer_rendersize_exec(bContext *C, wmOperator *UNUSED(op))
2661 {
2662         int retval = OPERATOR_CANCELLED;
2663         Scene *scene = CTX_data_scene(C);
2664         Sequence *active_seq = BKE_sequencer_active_get(scene);
2665         StripElem *se = NULL;
2666
2667         if (active_seq == NULL)
2668                 return OPERATOR_CANCELLED;
2669
2670
2671         if (active_seq->strip) {
2672                 switch (active_seq->type) {
2673                         case SEQ_TYPE_IMAGE:
2674                                 se = BKE_sequencer_give_stripelem(active_seq, scene->r.cfra);
2675                                 break;
2676                         case SEQ_TYPE_MOVIE:
2677                                 se = active_seq->strip->stripdata;
2678                                 break;
2679                         case SEQ_TYPE_SCENE:
2680                         case SEQ_TYPE_META:
2681                         case SEQ_TYPE_SOUND_RAM:
2682                         case SEQ_TYPE_SOUND_HD:
2683                         default:
2684                                 break;
2685                 }
2686         }
2687
2688         if (se) {
2689                 // prevent setting the render size if sequence values aren't initialized
2690                 if ((se->orig_width > 0) && (se->orig_height > 0)) {
2691                         scene->r.xsch = se->orig_width;
2692                         scene->r.ysch = se->orig_height;
2693                         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, scene);
2694                         retval = OPERATOR_FINISHED;
2695                 }
2696         }
2697
2698         return retval;
2699 }
2700
2701 void SEQUENCER_OT_rendersize(wmOperatorType *ot)
2702 {
2703         /* identifiers */
2704         ot->name = "Set Render Size";
2705         ot->idname = "SEQUENCER_OT_rendersize";
2706         ot->description = "Set render size and aspect from active sequence";
2707         
2708         /* api callbacks */
2709         ot->exec = sequencer_rendersize_exec;
2710         ot->poll = sequencer_edit_poll;
2711         
2712         /* flags */
2713         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2714         
2715         /* properties */
2716 }
2717
2718 static void seq_copy_del_sound(Scene *scene, Sequence *seq)
2719 {
2720         if (seq->type == SEQ_TYPE_META) {
2721                 Sequence *iseq;
2722                 for (iseq = seq->seqbase.first; iseq; iseq = iseq->next) {
2723                         seq_copy_del_sound(scene, iseq);
2724                 }
2725         }
2726         else if (seq->scene_sound) {
2727                 sound_remove_scene_sound(scene, seq->scene_sound);
2728                 seq->scene_sound = NULL;
2729         }
2730 }
2731
2732 static int sequencer_copy_exec(bContext *C, wmOperator *op)
2733 {
2734         Scene *scene = CTX_data_scene(C);
2735         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2736
2737         ListBase nseqbase = {NULL, NULL};
2738
2739         BKE_sequencer_free_clipboard();
2740
2741         if (BKE_sequence_base_isolated_sel_check(ed->seqbasep) == FALSE) {
2742                 BKE_report(op->reports, RPT_ERROR, "Please select all related strips");
2743                 return OPERATOR_CANCELLED;
2744         }
2745
2746         BKE_sequence_base_dupli_recursive(scene, NULL, &nseqbase, ed->seqbasep, SEQ_DUPE_UNIQUE_NAME);
2747
2748         /* To make sure the copied strips have unique names between each other add
2749          * them temporarily to the end of the original seqbase. (bug 25932)
2750          */
2751         if (nseqbase.first) {
2752                 Sequence *seq, *first_seq = nseqbase.first;
2753                 BLI_movelisttolist(ed->seqbasep, &nseqbase);
2754
2755                 for (seq = first_seq; seq; seq = seq->next)
2756                         BKE_sequencer_recursive_apply(seq, apply_unique_name_cb, scene);
2757
2758                 seqbase_clipboard.first = first_seq;
2759                 seqbase_clipboard.last = ed->seqbasep->last;
2760
2761                 if (first_seq->prev) {
2762                         first_seq->prev->next = NULL;
2763                         ed->seqbasep->last = first_seq->prev;
2764                         first_seq->prev = NULL;
2765                 }
2766         }
2767
2768         seqbase_clipboard_frame = scene->r.cfra;
2769
2770         /* Need to remove anything that references the current scene */
2771         {
2772                 Sequence *seq;
2773                 for (seq = seqbase_clipboard.first; seq; seq = seq->next) {
2774                         seq_copy_del_sound(scene, seq);
2775                 }
2776
2777                 /* duplicate pointers */
2778                 for (seq = seqbase_clipboard.first; seq; seq = seq->next) {
2779                         BKE_sequence_clipboard_pointers_store(seq);
2780                 }
2781         }
2782
2783         return OPERATOR_FINISHED;
2784 }
2785
2786 void SEQUENCER_OT_copy(wmOperatorType *ot)
2787 {
2788         /* identifiers */
2789         ot->name = "Copy";
2790         ot->idname = "SEQUENCER_OT_copy";
2791         ot->description = "";
2792
2793         /* api callbacks */
2794         ot->exec = sequencer_copy_exec;
2795         ot->poll = sequencer_edit_poll;
2796
2797         /* flags */
2798         ot->flag = OPTYPE_REGISTER;
2799
2800         /* properties */
2801 }
2802
2803 static int sequencer_paste_exec(bContext *C, wmOperator *UNUSED(op))
2804 {
2805         Main *bmain = CTX_data_main(C);
2806         Scene *scene = CTX_data_scene(C);
2807         Editing *ed = BKE_sequencer_editing_get(scene, TRUE); /* create if needed */
2808         ListBase nseqbase = {NULL, NULL};
2809         int ofs;
2810         Sequence *iseq, *iseq_first;
2811
2812         ED_sequencer_deselect_all(scene);
2813         ofs = scene->r.cfra - seqbase_clipboard_frame;
2814
2815         BKE_sequence_base_dupli_recursive(scene, NULL, &nseqbase, &seqbase_clipboard, SEQ_DUPE_UNIQUE_NAME);
2816
2817         /* transform pasted strips before adding */
2818         if (ofs) {
2819                 for (iseq = nseqbase.first; iseq; iseq = iseq->next) {
2820                         BKE_sequence_translate(scene, iseq, ofs);
2821                 }
2822         }
2823
2824         for (iseq = nseqbase.first; iseq; iseq = iseq->next) {
2825                 BKE_sequence_clipboard_pointers_restore(iseq, bmain);
2826         }
2827
2828         for (iseq = nseqbase.first; iseq; iseq = iseq->next) {
2829                 BKE_sequence_sound_init(scene, iseq);
2830         }
2831
2832         iseq_first = nseqbase.first;
2833
2834         BLI_movelisttolist(ed->seqbasep, &nseqbase);
2835
2836         /* make sure the pasted strips have unique names between them */
2837         for (iseq = iseq_first; iseq; iseq = iseq->next) {
2838                 BKE_sequencer_recursive_apply(iseq, apply_unique_name_cb, scene);
2839         }
2840
2841         /* ensure pasted strips don't overlap */
2842         for (iseq = iseq_first; iseq; iseq = iseq->next) {
2843                 if (BKE_sequence_test_overlap(ed->seqbasep, iseq)) {
2844                         BKE_sequence_base_shuffle(ed->seqbasep, iseq, scene);
2845                 }
2846         }
2847
2848         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2849
2850         return OPERATOR_FINISHED;
2851 }
2852
2853 void SEQUENCER_OT_paste(wmOperatorType *ot)
2854 {
2855         /* identifiers */
2856         ot->name = "Paste";
2857         ot->idname = "SEQUENCER_OT_paste";
2858         ot->description = "";
2859
2860         /* api callbacks */
2861         ot->exec = sequencer_paste_exec;
2862         ot->poll = ED_operator_sequencer_active;
2863
2864         /* flags */
2865         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2866
2867         /* properties */
2868 }
2869
2870 static int sequencer_swap_data_exec(bContext *C, wmOperator *op)
2871 {
2872         Scene *scene = CTX_data_scene(C);
2873         Sequence *seq_act;
2874         Sequence *seq_other;
2875         const char *error_msg;
2876
2877         if (BKE_sequencer_active_get_pair(scene, &seq_act, &seq_other) == 0) {
2878                 BKE_report(op->reports, RPT_ERROR, "Please select two strips");
2879                 return OPERATOR_CANCELLED;
2880         }
2881
2882         if (BKE_sequence_swap(seq_act, seq_other, &error_msg) == 0) {
2883                 BKE_report(op->reports, RPT_ERROR, error_msg);
2884                 return OPERATOR_CANCELLED;
2885         }
2886
2887         if (seq_act->scene_sound)
2888                 sound_remove_scene_sound(scene, seq_act->scene_sound);
2889
2890         if (seq_other->scene_sound)
2891                 sound_remove_scene_sound(scene, seq_other->scene_sound);
2892
2893         seq_act->scene_sound = NULL;
2894         seq_other->scene_sound = NULL;
2895
2896         BKE_sequence_calc(scene, seq_act);
2897         BKE_sequence_calc(scene, seq_other);
2898
2899         if (seq_act->sound) sound_add_scene_sound_defaults(scene, seq_act);
2900         if (seq_other->sound) sound_add_scene_sound_defaults(scene, seq_other);
2901
2902         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2903
2904         return OPERATOR_FINISHED;
2905 }
2906
2907 void SEQUENCER_OT_swap_data(wmOperatorType *ot)
2908 {
2909         /* identifiers */
2910         ot->name = "Sequencer Swap Data";
2911         ot->idname = "SEQUENCER_OT_swap_data";
2912         ot->description = "Swap 2 sequencer strips";
2913
2914         /* api callbacks */
2915         ot->exec = sequencer_swap_data_exec;
2916         ot->poll = ED_operator_sequencer_active;
2917
2918         /* flags */
2919         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2920
2921         /* properties */
2922 }
2923
2924 /* borderselect operator */
2925 static int view_ghost_border_exec(bContext *C, wmOperator *op)
2926 {
2927         Scene *scene = CTX_data_scene(C);
2928         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2929         View2D *v2d = UI_view2d_fromcontext(C);
2930
2931         rctf rect;
2932
2933         /* convert coordinates of rect to 'tot' rect coordinates */
2934         UI_view2d_region_to_view(v2d, RNA_int_get(op->ptr, "xmin"), RNA_int_get(op->ptr, "ymin"), &rect.xmin, &rect.ymin);
2935         UI_view2d_region_to_view(v2d, RNA_int_get(op->ptr, "xmax"), RNA_int_get(op->ptr, "ymax"), &rect.xmax, &rect.ymax);
2936
2937         if (ed == NULL)
2938                 return OPERATOR_CANCELLED;
2939
2940         rect.xmin /=  fabsf(BLI_rctf_size_x(&v2d->tot));
2941         rect.ymin /=  fabsf(BLI_rctf_size_y(&v2d->tot));
2942
2943         rect.xmax /=  fabsf(BLI_rctf_size_x(&v2d->tot));
2944         rect.ymax /=  fabsf(BLI_rctf_size_y(&v2d->tot));
2945
2946         rect.xmin += 0.5f;
2947         rect.xmax += 0.5f;
2948         rect.ymin += 0.5f;
2949         rect.ymax += 0.5f;
2950
2951         CLAMP(rect.xmin, 0.0f, 1.0f);
2952         CLAMP(rect.ymin, 0.0f, 1.0f);
2953         CLAMP(rect.xmax, 0.0f, 1.0f);
2954         CLAMP(rect.ymax, 0.0f, 1.0f);
2955
2956         scene->ed->over_border = rect;
2957
2958         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2959
2960         return OPERATOR_FINISHED;
2961 }
2962
2963 /* ****** Border Select ****** */
2964 void SEQUENCER_OT_view_ghost_border(wmOperatorType *ot)
2965 {
2966         /* identifiers */
2967         ot->name = "Border Offset View";
2968         ot->idname = "SEQUENCER_OT_view_ghost_border";
2969         ot->description = "Set the boundaries of the border used for offset-view";
2970
2971         /* api callbacks */
2972         ot->invoke = WM_border_select_invoke;
2973         ot->exec = view_ghost_border_exec;
2974         ot->modal = WM_border_select_modal;
2975         ot->poll = sequencer_view_poll;
2976         ot->cancel = WM_border_select_cancel;
2977
2978         /* flags */
2979         ot->flag = 0;
2980
2981         /* rna */
2982         WM_operator_properties_gesture_border(ot, FALSE);
2983 }
2984
2985 /* rebuild_proxy operator */
2986 static int sequencer_rebuild_proxy_exec(bContext *C, wmOperator *UNUSED(op))
2987 {
2988         seq_proxy_build_job(C);
2989
2990         return OPERATOR_FINISHED;
2991 }
2992
2993 void SEQUENCER_OT_rebuild_proxy(wmOperatorType *ot)
2994 {
2995         /* identifiers */
2996         ot->name = "Rebuild Proxy and Timecode Indices";
2997         ot->idname = "SEQUENCER_OT_rebuild_proxy";
2998         ot->description = "Rebuild all selected proxies and timecode indices using the job system";
2999         
3000         /* api callbacks */
3001         ot->exec = sequencer_rebuild_proxy_exec;
3002         ot->poll = ED_operator_sequencer_active;
3003         
3004         /* flags */
3005         ot->flag = OPTYPE_REGISTER;
3006 }
3007
3008 /* change ops */
3009
3010 static EnumPropertyItem prop_change_effect_input_types[] = {
3011         {0, "A_B", 0, "A -> B", ""},
3012         {1, "B_C", 0, "B -> C", ""},
3013         {2, "A_C", 0, "A -> C", ""},
3014         {0, NULL, 0, NULL, NULL}
3015 };
3016
3017 static int sequencer_change_effect_input_exec(bContext *C, wmOperator *op)
3018 {
3019         Scene *scene = CTX_data_scene(C);
3020         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
3021         Sequence *seq = BKE_sequencer_active_get(scene);
3022
3023         Sequence **seq_1, **seq_2;
3024
3025         switch (RNA_enum_get(op->ptr, "swap")) {
3026                 case 0:
3027                         seq_1 = &seq->seq1;
3028                         seq_2 = &seq->seq2;
3029                         break;
3030                 case 1:
3031                         seq_1 = &seq->seq2;
3032                         seq_2 = &seq->seq3;
3033                         break;
3034                 default: /* 2 */
3035                         seq_1 = &seq->seq1;
3036                         seq_2 = &seq->seq3;
3037                         break;
3038         }
3039
3040         if (*seq_1 == NULL || *seq_2 == NULL) {
3041                 BKE_report(op->reports, RPT_ERROR, "One of the effect inputs is unset, cannot swap");
3042                 return OPERATOR_CANCELLED;
3043         }
3044         else {
3045                 SWAP(Sequence *, *seq_1, *seq_2);
3046         }
3047
3048         BKE_sequencer_update_changed_seq_and_deps(scene, seq, 0, 1);
3049
3050         /* important else we don't get the imbuf cache flushed */
3051         BKE_sequencer_free_imbuf(scene, &ed->seqbase, FALSE);
3052
3053         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
3054
3055         return OPERATOR_FINISHED;
3056 }
3057
3058 void SEQUENCER_OT_change_effect_input(struct wmOperatorType *ot)
3059 {
3060         /* identifiers */
3061         ot->name = "Change Effect Input";
3062         ot->idname = "SEQUENCER_OT_change_effect_input";
3063         ot->description = "";
3064
3065         /* api callbacks */
3066         ot->exec = sequencer_change_effect_input_exec;
3067         ot->poll = sequencer_effect_poll;
3068
3069         /* flags */
3070         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
3071
3072         ot->prop = RNA_def_enum(ot->srna, "swap", prop_change_effect_input_types, 0, "Swap", "The effect inputs to swap");
3073 }
3074
3075 static int sequencer_change_effect_type_exec(bContext *C, wmOperator *op)
3076 {
3077         Scene *scene = CTX_data_scene(C);
3078         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
3079         Sequence *seq = BKE_sequencer_active_get(scene);
3080         const int new_type = RNA_enum_get(op->ptr, "type");
3081
3082         /* free previous effect and init new effect */
3083         struct SeqEffectHandle sh;
3084
3085         if ((seq->type & SEQ_TYPE_EFFECT) == 0) {
3086                 return OPERATOR_CANCELLED;
3087         }
3088
3089         /* can someone explain the logic behind only allowing to increase this,
3090          * copied from 2.4x - campbell */
3091         if (BKE_sequence_effect_get_num_inputs(seq->type) <
3092             BKE_sequence_effect_get_num_inputs(new_type))
3093         {
3094                 BKE_report(op->reports, RPT_ERROR, "New effect needs more input strips");
3095                 return OPERATOR_CANCELLED;
3096         }
3097         else {
3098                 sh = BKE_sequence_get_effect(seq);
3099                 sh.free(seq);
3100
3101                 seq->type = new_type;
3102
3103                 sh = BKE_sequence_get_effect(seq);
3104                 sh.init(seq);
3105         }
3106
3107         /* update */
3108         BKE_sequencer_update_changed_seq_and_deps(scene, seq, 0, 1);
3109
3110         /* important else we don't get the imbuf cache flushed */
3111         BKE_sequencer_free_imbuf(scene, &ed->seqbase, FALSE);
3112
3113         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
3114
3115         return OPERATOR_FINISHED;
3116 }
3117
3118 void SEQUENCER_OT_change_effect_type(struct wmOperatorType *ot)
3119 {
3120         /* identifiers */
3121         ot->name = "Change Effect Type";
3122         ot->idname = "SEQUENCER_OT_change_effect_type";
3123         ot->description = "";
3124
3125         /* api callbacks */
3126         ot->exec = sequencer_change_effect_type_exec;
3127         ot->poll = sequencer_effect_poll;
3128
3129         /* flags */
3130         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
3131
3132         ot->prop = RNA_def_enum(ot->srna, "type", sequencer_prop_effect_types, SEQ_TYPE_CROSS, "Type", "Sequencer effect type");