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