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